David Sterba | c1d7c51 | 2018-04-03 19:23:33 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Chris Mason | 6cbd557 | 2007-06-12 09:07:21 -0400 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2007 Oracle. All rights reserved. |
Chris Mason | 6cbd557 | 2007-06-12 09:07:21 -0400 | [diff] [blame] | 4 | */ |
| 5 | |
Herbert Xu | 7999096 | 2020-06-12 16:57:37 +1000 | [diff] [blame] | 6 | #include <crypto/hash.h> |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 7 | #include <linux/kernel.h> |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 8 | #include <linux/bio.h> |
Sage Weil | f2eb0a2 | 2008-05-02 14:43:14 -0400 | [diff] [blame] | 9 | #include <linux/file.h> |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10 | #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 Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 16 | #include <linux/backing-dev.h> |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 17 | #include <linux/writeback.h> |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 18 | #include <linux/compat.h> |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 19 | #include <linux/xattr.h> |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 20 | #include <linux/posix_acl.h> |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 21 | #include <linux/falloc.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 22 | #include <linux/slab.h> |
David Sterba | 7a36dde | 2011-05-06 15:33:15 +0200 | [diff] [blame] | 23 | #include <linux/ratelimit.h> |
Filipe Brandenburger | 55e301f | 2013-01-29 06:04:50 +0000 | [diff] [blame] | 24 | #include <linux/btrfs.h> |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 25 | #include <linux/blkdev.h> |
Josef Bacik | f23b5a5 | 2013-06-19 10:16:26 -0400 | [diff] [blame] | 26 | #include <linux/posix_acl_xattr.h> |
Christoph Hellwig | e2e40f2 | 2015-02-22 08:58:50 -0800 | [diff] [blame] | 27 | #include <linux/uio.h> |
Josef Bacik | 69fe2d7 | 2017-10-19 14:15:57 -0400 | [diff] [blame] | 28 | #include <linux/magic.h> |
Jeff Layton | ae5e165 | 2018-01-29 06:41:30 -0500 | [diff] [blame] | 29 | #include <linux/iversion.h> |
Omar Sandoval | ed46ff3 | 2016-11-03 10:28:14 -0700 | [diff] [blame] | 30 | #include <linux/swap.h> |
Roman Gushchin | f8e6608 | 2020-03-04 16:57:35 -0800 | [diff] [blame] | 31 | #include <linux/migrate.h> |
Nikolay Borisov | b1c16ac | 2019-04-01 11:29:57 +0300 | [diff] [blame] | 32 | #include <linux/sched/mm.h> |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 33 | #include <linux/iomap.h> |
David Sterba | 92d3217 | 2018-04-16 21:10:14 +0200 | [diff] [blame] | 34 | #include <asm/unaligned.h> |
Boris Burkov | 1460540 | 2021-06-30 13:01:49 -0700 | [diff] [blame] | 35 | #include <linux/fsverity.h> |
David Sterba | 602cbe9 | 2019-08-21 18:48:25 +0200 | [diff] [blame] | 36 | #include "misc.h" |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 37 | #include "ctree.h" |
| 38 | #include "disk-io.h" |
| 39 | #include "transaction.h" |
| 40 | #include "btrfs_inode.h" |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 41 | #include "print-tree.h" |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 42 | #include "ordered-data.h" |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 43 | #include "xattr.h" |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 44 | #include "tree-log.h" |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 45 | #include "volumes.h" |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 46 | #include "compression.h" |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 47 | #include "locking.h" |
Josef Bacik | dc89e98 | 2011-01-28 17:05:48 -0500 | [diff] [blame] | 48 | #include "free-space-cache.h" |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 49 | #include "props.h" |
Dongsheng Yang | 3119321 | 2014-12-12 16:44:35 +0800 | [diff] [blame] | 50 | #include "qgroup.h" |
Josef Bacik | 8673634 | 2019-06-19 15:12:00 -0400 | [diff] [blame] | 51 | #include "delalloc-space.h" |
Josef Bacik | aac0023 | 2019-06-20 15:37:44 -0400 | [diff] [blame] | 52 | #include "block-group.h" |
Filipe Manana | 467dc47 | 2020-05-27 11:16:07 +0100 | [diff] [blame] | 53 | #include "space-info.h" |
Naohiro Aota | d8e3fb1 | 2021-02-04 19:22:05 +0900 | [diff] [blame] | 54 | #include "zoned.h" |
Qu Wenruo | b945a46 | 2021-05-31 16:50:46 +0800 | [diff] [blame] | 55 | #include "subpage.h" |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 56 | |
| 57 | struct btrfs_iget_args { |
David Sterba | 0202e83 | 2020-05-15 19:35:59 +0200 | [diff] [blame] | 58 | u64 ino; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 59 | struct btrfs_root *root; |
| 60 | }; |
| 61 | |
Filipe Manana | f28a492 | 2015-12-08 19:23:20 +0000 | [diff] [blame] | 62 | struct btrfs_dio_data { |
Filipe Manana | f28a492 | 2015-12-08 19:23:20 +0000 | [diff] [blame] | 63 | u64 reserve; |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 64 | loff_t length; |
| 65 | ssize_t submitted; |
| 66 | struct extent_changeset *data_reserved; |
Filipe Manana | f28a492 | 2015-12-08 19:23:20 +0000 | [diff] [blame] | 67 | }; |
| 68 | |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 69 | static const struct inode_operations btrfs_dir_inode_operations; |
| 70 | static const struct inode_operations btrfs_symlink_inode_operations; |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 71 | static const struct inode_operations btrfs_special_inode_operations; |
| 72 | static const struct inode_operations btrfs_file_inode_operations; |
Alexey Dobriyan | 7f09410 | 2009-09-21 17:01:10 -0700 | [diff] [blame] | 73 | static const struct address_space_operations btrfs_aops; |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 74 | static const struct file_operations btrfs_dir_file_operations; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 75 | |
| 76 | static struct kmem_cache *btrfs_inode_cachep; |
| 77 | struct kmem_cache *btrfs_trans_handle_cachep; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 78 | struct kmem_cache *btrfs_path_cachep; |
Josef Bacik | dc89e98 | 2011-01-28 17:05:48 -0500 | [diff] [blame] | 79 | struct kmem_cache *btrfs_free_space_cachep; |
Christophe Leroy | 3acd485 | 2019-08-21 15:05:55 +0000 | [diff] [blame] | 80 | struct kmem_cache *btrfs_free_space_bitmap_cachep; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 81 | |
Eric Sandeen | 3972f26 | 2013-01-12 02:57:22 +0000 | [diff] [blame] | 82 | static int btrfs_setsize(struct inode *inode, struct iattr *attr); |
Filipe Manana | 213e8c5 | 2018-02-06 20:40:31 +0000 | [diff] [blame] | 83 | static int btrfs_truncate(struct inode *inode, bool skip_writeback); |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 84 | static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent); |
Nikolay Borisov | 6e26c44 | 2020-06-03 08:55:14 +0300 | [diff] [blame] | 85 | static noinline int cow_file_range(struct btrfs_inode *inode, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 86 | struct page *locked_page, |
Nikolay Borisov | 74e9194 | 2019-07-17 16:18:16 +0300 | [diff] [blame] | 87 | u64 start, u64 end, int *page_started, |
Nikolay Borisov | 330a582 | 2019-07-17 16:18:17 +0300 | [diff] [blame] | 88 | unsigned long *nr_written, int unlock); |
Nikolay Borisov | 4b67c11 | 2020-06-03 08:55:05 +0300 | [diff] [blame] | 89 | static struct extent_map *create_io_em(struct btrfs_inode *inode, u64 start, |
| 90 | u64 len, u64 orig_start, u64 block_start, |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 91 | u64 block_len, u64 orig_block_len, |
| 92 | u64 ram_bytes, int compress_type, |
| 93 | int type); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 94 | |
Nikolay Borisov | b672b5c | 2020-06-03 08:55:24 +0300 | [diff] [blame] | 95 | static void __endio_write_update_ordered(struct btrfs_inode *inode, |
Qu Wenruo | 52427260 | 2017-03-08 10:25:52 +0800 | [diff] [blame] | 96 | const u64 offset, const u64 bytes, |
| 97 | const bool uptodate); |
| 98 | |
| 99 | /* |
Goldwyn Rodrigues | a14b78a | 2020-09-24 11:39:16 -0500 | [diff] [blame] | 100 | * btrfs_inode_lock - lock inode i_rwsem based on arguments passed |
| 101 | * |
| 102 | * ilock_flags can have the following bit set: |
| 103 | * |
| 104 | * BTRFS_ILOCK_SHARED - acquire a shared lock on the inode |
| 105 | * BTRFS_ILOCK_TRY - try to acquire the lock, if fails on first attempt |
| 106 | * return -EAGAIN |
Josef Bacik | 8318ba7 | 2021-02-10 17:14:33 -0500 | [diff] [blame] | 107 | * BTRFS_ILOCK_MMAP - acquire a write lock on the i_mmap_lock |
Goldwyn Rodrigues | a14b78a | 2020-09-24 11:39:16 -0500 | [diff] [blame] | 108 | */ |
| 109 | int btrfs_inode_lock(struct inode *inode, unsigned int ilock_flags) |
| 110 | { |
| 111 | if (ilock_flags & BTRFS_ILOCK_SHARED) { |
| 112 | if (ilock_flags & BTRFS_ILOCK_TRY) { |
| 113 | if (!inode_trylock_shared(inode)) |
| 114 | return -EAGAIN; |
| 115 | else |
| 116 | return 0; |
| 117 | } |
| 118 | inode_lock_shared(inode); |
| 119 | } else { |
| 120 | if (ilock_flags & BTRFS_ILOCK_TRY) { |
| 121 | if (!inode_trylock(inode)) |
| 122 | return -EAGAIN; |
| 123 | else |
| 124 | return 0; |
| 125 | } |
| 126 | inode_lock(inode); |
| 127 | } |
Josef Bacik | 8318ba7 | 2021-02-10 17:14:33 -0500 | [diff] [blame] | 128 | if (ilock_flags & BTRFS_ILOCK_MMAP) |
| 129 | down_write(&BTRFS_I(inode)->i_mmap_lock); |
Goldwyn Rodrigues | a14b78a | 2020-09-24 11:39:16 -0500 | [diff] [blame] | 130 | return 0; |
| 131 | } |
| 132 | |
| 133 | /* |
| 134 | * btrfs_inode_unlock - unock inode i_rwsem |
| 135 | * |
| 136 | * ilock_flags should contain the same bits set as passed to btrfs_inode_lock() |
| 137 | * to decide whether the lock acquired is shared or exclusive. |
| 138 | */ |
| 139 | void btrfs_inode_unlock(struct inode *inode, unsigned int ilock_flags) |
| 140 | { |
Josef Bacik | 8318ba7 | 2021-02-10 17:14:33 -0500 | [diff] [blame] | 141 | if (ilock_flags & BTRFS_ILOCK_MMAP) |
| 142 | up_write(&BTRFS_I(inode)->i_mmap_lock); |
Goldwyn Rodrigues | a14b78a | 2020-09-24 11:39:16 -0500 | [diff] [blame] | 143 | if (ilock_flags & BTRFS_ILOCK_SHARED) |
| 144 | inode_unlock_shared(inode); |
| 145 | else |
| 146 | inode_unlock(inode); |
| 147 | } |
| 148 | |
| 149 | /* |
Qu Wenruo | 52427260 | 2017-03-08 10:25:52 +0800 | [diff] [blame] | 150 | * Cleanup all submitted ordered extents in specified range to handle errors |
Andrea Gelmini | 52042d8 | 2018-11-28 12:05:13 +0100 | [diff] [blame] | 151 | * from the btrfs_run_delalloc_range() callback. |
Qu Wenruo | 52427260 | 2017-03-08 10:25:52 +0800 | [diff] [blame] | 152 | * |
| 153 | * NOTE: caller must ensure that when an error happens, it can not call |
| 154 | * extent_clear_unlock_delalloc() to clear both the bits EXTENT_DO_ACCOUNTING |
| 155 | * and EXTENT_DELALLOC simultaneously, because that causes the reserved metadata |
| 156 | * to be released, which we want to happen only when finishing the ordered |
Nikolay Borisov | d1051d6 | 2018-11-21 17:10:52 +0200 | [diff] [blame] | 157 | * extent (btrfs_finish_ordered_io()). |
Qu Wenruo | 52427260 | 2017-03-08 10:25:52 +0800 | [diff] [blame] | 158 | */ |
Nikolay Borisov | 64e1db5 | 2020-06-03 08:55:25 +0300 | [diff] [blame] | 159 | static inline void btrfs_cleanup_ordered_extents(struct btrfs_inode *inode, |
Nikolay Borisov | d1051d6 | 2018-11-21 17:10:52 +0200 | [diff] [blame] | 160 | struct page *locked_page, |
| 161 | u64 offset, u64 bytes) |
Qu Wenruo | 52427260 | 2017-03-08 10:25:52 +0800 | [diff] [blame] | 162 | { |
Naohiro Aota | 63d71450c | 2017-09-01 17:58:47 +0900 | [diff] [blame] | 163 | unsigned long index = offset >> PAGE_SHIFT; |
| 164 | unsigned long end_index = (offset + bytes - 1) >> PAGE_SHIFT; |
Nikolay Borisov | d1051d6 | 2018-11-21 17:10:52 +0200 | [diff] [blame] | 165 | u64 page_start = page_offset(locked_page); |
| 166 | u64 page_end = page_start + PAGE_SIZE - 1; |
| 167 | |
Naohiro Aota | 63d71450c | 2017-09-01 17:58:47 +0900 | [diff] [blame] | 168 | struct page *page; |
| 169 | |
| 170 | while (index <= end_index) { |
Qu Wenruo | 968f256 | 2021-05-18 15:09:41 +0800 | [diff] [blame] | 171 | /* |
| 172 | * For locked page, we will call end_extent_writepage() on it |
| 173 | * in run_delalloc_range() for the error handling. That |
| 174 | * end_extent_writepage() function will call |
| 175 | * btrfs_mark_ordered_io_finished() to clear page Ordered and |
| 176 | * run the ordered extent accounting. |
| 177 | * |
| 178 | * Here we can't just clear the Ordered bit, or |
| 179 | * btrfs_mark_ordered_io_finished() would skip the accounting |
| 180 | * for the page range, and the ordered extent will never finish. |
| 181 | */ |
| 182 | if (index == (page_offset(locked_page) >> PAGE_SHIFT)) { |
| 183 | index++; |
| 184 | continue; |
| 185 | } |
Nikolay Borisov | 64e1db5 | 2020-06-03 08:55:25 +0300 | [diff] [blame] | 186 | page = find_get_page(inode->vfs_inode.i_mapping, index); |
Naohiro Aota | 63d71450c | 2017-09-01 17:58:47 +0900 | [diff] [blame] | 187 | index++; |
| 188 | if (!page) |
| 189 | continue; |
Qu Wenruo | 968f256 | 2021-05-18 15:09:41 +0800 | [diff] [blame] | 190 | |
| 191 | /* |
| 192 | * Here we just clear all Ordered bits for every page in the |
| 193 | * range, then __endio_write_update_ordered() will handle |
| 194 | * the ordered extent accounting for the range. |
| 195 | */ |
Qu Wenruo | b945a46 | 2021-05-31 16:50:46 +0800 | [diff] [blame] | 196 | btrfs_page_clamp_clear_ordered(inode->root->fs_info, page, |
| 197 | offset, bytes); |
Naohiro Aota | 63d71450c | 2017-09-01 17:58:47 +0900 | [diff] [blame] | 198 | put_page(page); |
| 199 | } |
Nikolay Borisov | d1051d6 | 2018-11-21 17:10:52 +0200 | [diff] [blame] | 200 | |
Qu Wenruo | b945a46 | 2021-05-31 16:50:46 +0800 | [diff] [blame] | 201 | /* The locked page covers the full range, nothing needs to be done */ |
| 202 | if (bytes + offset <= page_offset(locked_page) + PAGE_SIZE) |
| 203 | return; |
Nikolay Borisov | d1051d6 | 2018-11-21 17:10:52 +0200 | [diff] [blame] | 204 | /* |
| 205 | * In case this page belongs to the delalloc range being instantiated |
| 206 | * then skip it, since the first page of a range is going to be |
| 207 | * properly cleaned up by the caller of run_delalloc_range |
| 208 | */ |
| 209 | if (page_start >= offset && page_end <= (offset + bytes - 1)) { |
Qu Wenruo | b945a46 | 2021-05-31 16:50:46 +0800 | [diff] [blame] | 210 | bytes = offset + bytes - page_offset(locked_page) - PAGE_SIZE; |
| 211 | offset = page_offset(locked_page) + PAGE_SIZE; |
Nikolay Borisov | d1051d6 | 2018-11-21 17:10:52 +0200 | [diff] [blame] | 212 | } |
| 213 | |
Nikolay Borisov | 64e1db5 | 2020-06-03 08:55:25 +0300 | [diff] [blame] | 214 | return __endio_write_update_ordered(inode, offset, bytes, false); |
Qu Wenruo | 52427260 | 2017-03-08 10:25:52 +0800 | [diff] [blame] | 215 | } |
| 216 | |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 217 | static int btrfs_dirty_inode(struct inode *inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 218 | |
Yan, Zheng | f34f57a | 2009-11-12 09:35:27 +0000 | [diff] [blame] | 219 | static int btrfs_init_inode_security(struct btrfs_trans_handle *trans, |
Eric Paris | 2a7dba3 | 2011-02-01 11:05:39 -0500 | [diff] [blame] | 220 | struct inode *inode, struct inode *dir, |
| 221 | const struct qstr *qstr) |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 222 | { |
| 223 | int err; |
| 224 | |
Yan, Zheng | f34f57a | 2009-11-12 09:35:27 +0000 | [diff] [blame] | 225 | err = btrfs_init_acl(trans, inode, dir); |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 226 | if (!err) |
Eric Paris | 2a7dba3 | 2011-02-01 11:05:39 -0500 | [diff] [blame] | 227 | err = btrfs_xattr_security_init(trans, inode, dir, qstr); |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 228 | return err; |
| 229 | } |
| 230 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 231 | /* |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 232 | * this does all the hard work for inserting an inline extent into |
| 233 | * the btree. The caller should have done a btrfs_drop_extents so that |
| 234 | * no overlapping inline items exist in the btree |
| 235 | */ |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 236 | static int insert_inline_extent(struct btrfs_trans_handle *trans, |
Filipe Manana | 5893dfb | 2020-11-04 11:07:32 +0000 | [diff] [blame] | 237 | struct btrfs_path *path, bool extent_inserted, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 238 | struct btrfs_root *root, struct inode *inode, |
| 239 | u64 start, size_t size, size_t compressed_size, |
Li Zefan | fe3f566 | 2011-03-28 08:30:38 +0000 | [diff] [blame] | 240 | int compress_type, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 241 | struct page **compressed_pages) |
| 242 | { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 243 | struct extent_buffer *leaf; |
| 244 | struct page *page = NULL; |
| 245 | char *kaddr; |
| 246 | unsigned long ptr; |
| 247 | struct btrfs_file_extent_item *ei; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 248 | int ret; |
| 249 | size_t cur_size = size; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 250 | unsigned long offset; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 251 | |
Jia-Ju Bai | 982f1f5d | 2019-07-27 16:51:13 +0800 | [diff] [blame] | 252 | ASSERT((compressed_size > 0 && compressed_pages) || |
| 253 | (compressed_size == 0 && !compressed_pages)); |
| 254 | |
Li Zefan | fe3f566 | 2011-03-28 08:30:38 +0000 | [diff] [blame] | 255 | if (compressed_size && compressed_pages) |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 256 | cur_size = compressed_size; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 257 | |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 258 | if (!extent_inserted) { |
| 259 | struct btrfs_key key; |
| 260 | size_t datasize; |
| 261 | |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 262 | key.objectid = btrfs_ino(BTRFS_I(inode)); |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 263 | key.offset = start; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 264 | key.type = BTRFS_EXTENT_DATA_KEY; |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 265 | |
| 266 | datasize = btrfs_file_extent_calc_inline_size(cur_size); |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 267 | ret = btrfs_insert_empty_item(trans, root, path, &key, |
| 268 | datasize); |
David Sterba | 79b4f4c | 2017-06-15 19:09:51 +0200 | [diff] [blame] | 269 | if (ret) |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 270 | goto fail; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 271 | } |
| 272 | leaf = path->nodes[0]; |
| 273 | ei = btrfs_item_ptr(leaf, path->slots[0], |
| 274 | struct btrfs_file_extent_item); |
| 275 | btrfs_set_file_extent_generation(leaf, ei, trans->transid); |
| 276 | btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE); |
| 277 | btrfs_set_file_extent_encryption(leaf, ei, 0); |
| 278 | btrfs_set_file_extent_other_encoding(leaf, ei, 0); |
| 279 | btrfs_set_file_extent_ram_bytes(leaf, ei, size); |
| 280 | ptr = btrfs_file_extent_inline_start(ei); |
| 281 | |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 282 | if (compress_type != BTRFS_COMPRESS_NONE) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 283 | struct page *cpage; |
| 284 | int i = 0; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 285 | while (compressed_size > 0) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 286 | cpage = compressed_pages[i]; |
Chris Mason | 5b050f0 | 2008-11-11 09:34:41 -0500 | [diff] [blame] | 287 | cur_size = min_t(unsigned long, compressed_size, |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 288 | PAGE_SIZE); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 289 | |
David Sterba | 4c2bf27 | 2021-06-15 17:15:38 +0200 | [diff] [blame] | 290 | kaddr = page_address(cpage); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 291 | write_extent_buffer(leaf, kaddr, ptr, cur_size); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 292 | |
| 293 | i++; |
| 294 | ptr += cur_size; |
| 295 | compressed_size -= cur_size; |
| 296 | } |
| 297 | btrfs_set_file_extent_compression(leaf, ei, |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 298 | compress_type); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 299 | } else { |
| 300 | page = find_get_page(inode->i_mapping, |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 301 | start >> PAGE_SHIFT); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 302 | btrfs_set_file_extent_compression(leaf, ei, 0); |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 303 | kaddr = kmap_atomic(page); |
Johannes Thumshirn | 7073017 | 2018-12-05 15:23:03 +0100 | [diff] [blame] | 304 | offset = offset_in_page(start); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 305 | write_extent_buffer(leaf, kaddr + offset, ptr, size); |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 306 | kunmap_atomic(kaddr); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 307 | put_page(page); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 308 | } |
| 309 | btrfs_mark_buffer_dirty(leaf); |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 310 | btrfs_release_path(path); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 311 | |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 312 | /* |
Josef Bacik | 9ddc959 | 2020-01-17 09:02:22 -0500 | [diff] [blame] | 313 | * We align size to sectorsize for inline extents just for simplicity |
| 314 | * sake. |
| 315 | */ |
| 316 | size = ALIGN(size, root->fs_info->sectorsize); |
| 317 | ret = btrfs_inode_set_file_extent_range(BTRFS_I(inode), start, size); |
| 318 | if (ret) |
| 319 | goto fail; |
| 320 | |
| 321 | /* |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 322 | * we're an inline extent, so nobody can |
| 323 | * extend the file past i_size without locking |
| 324 | * a page we already have locked. |
| 325 | * |
| 326 | * We must do any isize and inode updates |
| 327 | * before we unlock the pages. Otherwise we |
| 328 | * could end up racing with unlink. |
| 329 | */ |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 330 | BTRFS_I(inode)->disk_i_size = inode->i_size; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 331 | fail: |
David Sterba | 79b4f4c | 2017-06-15 19:09:51 +0200 | [diff] [blame] | 332 | return ret; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 333 | } |
| 334 | |
| 335 | |
| 336 | /* |
| 337 | * conditionally insert an inline extent into the file. This |
| 338 | * does the checks required to make sure the data is small enough |
| 339 | * to fit as an inline extent. |
| 340 | */ |
Nikolay Borisov | a034940 | 2020-06-03 08:55:12 +0300 | [diff] [blame] | 341 | static noinline int cow_file_range_inline(struct btrfs_inode *inode, u64 start, |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 342 | u64 end, size_t compressed_size, |
| 343 | int compress_type, |
| 344 | struct page **compressed_pages) |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 345 | { |
Filipe Manana | 5893dfb | 2020-11-04 11:07:32 +0000 | [diff] [blame] | 346 | struct btrfs_drop_extents_args drop_args = { 0 }; |
Nikolay Borisov | a034940 | 2020-06-03 08:55:12 +0300 | [diff] [blame] | 347 | struct btrfs_root *root = inode->root; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 348 | struct btrfs_fs_info *fs_info = root->fs_info; |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 349 | struct btrfs_trans_handle *trans; |
Nikolay Borisov | a034940 | 2020-06-03 08:55:12 +0300 | [diff] [blame] | 350 | u64 isize = i_size_read(&inode->vfs_inode); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 351 | u64 actual_end = min(end + 1, isize); |
| 352 | u64 inline_len = actual_end - start; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 353 | u64 aligned_end = ALIGN(end, fs_info->sectorsize); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 354 | u64 data_len = inline_len; |
| 355 | int ret; |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 356 | struct btrfs_path *path; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 357 | |
| 358 | if (compressed_size) |
| 359 | data_len = compressed_size; |
| 360 | |
| 361 | if (start > 0 || |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 362 | actual_end > fs_info->sectorsize || |
| 363 | data_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info) || |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 364 | (!compressed_size && |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 365 | (actual_end & (fs_info->sectorsize - 1)) == 0) || |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 366 | end + 1 < isize || |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 367 | data_len > fs_info->max_inline) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 368 | return 1; |
| 369 | } |
| 370 | |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 371 | path = btrfs_alloc_path(); |
| 372 | if (!path) |
| 373 | return -ENOMEM; |
| 374 | |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 375 | trans = btrfs_join_transaction(root); |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 376 | if (IS_ERR(trans)) { |
| 377 | btrfs_free_path(path); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 378 | return PTR_ERR(trans); |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 379 | } |
Nikolay Borisov | a034940 | 2020-06-03 08:55:12 +0300 | [diff] [blame] | 380 | trans->block_rsv = &inode->block_rsv; |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 381 | |
Filipe Manana | 5893dfb | 2020-11-04 11:07:32 +0000 | [diff] [blame] | 382 | drop_args.path = path; |
| 383 | drop_args.start = start; |
| 384 | drop_args.end = aligned_end; |
| 385 | drop_args.drop_cache = true; |
| 386 | drop_args.replace_extent = true; |
| 387 | |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 388 | if (compressed_size && compressed_pages) |
Filipe Manana | 5893dfb | 2020-11-04 11:07:32 +0000 | [diff] [blame] | 389 | drop_args.extent_item_size = btrfs_file_extent_calc_inline_size( |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 390 | compressed_size); |
| 391 | else |
Filipe Manana | 5893dfb | 2020-11-04 11:07:32 +0000 | [diff] [blame] | 392 | drop_args.extent_item_size = btrfs_file_extent_calc_inline_size( |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 393 | inline_len); |
| 394 | |
Filipe Manana | 5893dfb | 2020-11-04 11:07:32 +0000 | [diff] [blame] | 395 | ret = btrfs_drop_extents(trans, root, inode, &drop_args); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 396 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 397 | btrfs_abort_transaction(trans, ret); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 398 | goto out; |
| 399 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 400 | |
| 401 | if (isize > actual_end) |
| 402 | inline_len = min_t(u64, isize, actual_end); |
Filipe Manana | 5893dfb | 2020-11-04 11:07:32 +0000 | [diff] [blame] | 403 | ret = insert_inline_extent(trans, path, drop_args.extent_inserted, |
Nikolay Borisov | a034940 | 2020-06-03 08:55:12 +0300 | [diff] [blame] | 404 | root, &inode->vfs_inode, start, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 405 | inline_len, compressed_size, |
Li Zefan | fe3f566 | 2011-03-28 08:30:38 +0000 | [diff] [blame] | 406 | compress_type, compressed_pages); |
Josef Bacik | 2adcac1 | 2012-05-23 16:10:14 -0400 | [diff] [blame] | 407 | if (ret && ret != -ENOSPC) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 408 | btrfs_abort_transaction(trans, ret); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 409 | goto out; |
Josef Bacik | 2adcac1 | 2012-05-23 16:10:14 -0400 | [diff] [blame] | 410 | } else if (ret == -ENOSPC) { |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 411 | ret = 1; |
| 412 | goto out; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 413 | } |
Josef Bacik | 2adcac1 | 2012-05-23 16:10:14 -0400 | [diff] [blame] | 414 | |
Filipe Manana | 2766ff6 | 2020-11-04 11:07:34 +0000 | [diff] [blame] | 415 | btrfs_update_inode_bytes(inode, inline_len, drop_args.bytes_found); |
Nikolay Borisov | 9a56fcd | 2020-11-02 16:48:59 +0200 | [diff] [blame] | 416 | ret = btrfs_update_inode(trans, root, inode); |
Filipe Manana | 2766ff6 | 2020-11-04 11:07:34 +0000 | [diff] [blame] | 417 | if (ret && ret != -ENOSPC) { |
| 418 | btrfs_abort_transaction(trans, ret); |
| 419 | goto out; |
| 420 | } else if (ret == -ENOSPC) { |
| 421 | ret = 1; |
| 422 | goto out; |
| 423 | } |
| 424 | |
Nikolay Borisov | a034940 | 2020-06-03 08:55:12 +0300 | [diff] [blame] | 425 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &inode->runtime_flags); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 426 | out: |
Qu Wenruo | 94ed938 | 2015-09-08 17:25:56 +0800 | [diff] [blame] | 427 | /* |
| 428 | * Don't forget to free the reserved space, as for inlined extent |
| 429 | * it won't count as data extent, free them directly here. |
| 430 | * And at reserve time, it's always aligned to page size, so |
| 431 | * just free one page here. |
| 432 | */ |
Nikolay Borisov | a034940 | 2020-06-03 08:55:12 +0300 | [diff] [blame] | 433 | btrfs_qgroup_free_data(inode, NULL, 0, PAGE_SIZE); |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 434 | btrfs_free_path(path); |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 435 | btrfs_end_transaction(trans); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 436 | return ret; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 437 | } |
| 438 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 439 | struct async_extent { |
| 440 | u64 start; |
| 441 | u64 ram_size; |
| 442 | u64 compressed_size; |
| 443 | struct page **pages; |
| 444 | unsigned long nr_pages; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 445 | int compress_type; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 446 | struct list_head list; |
| 447 | }; |
| 448 | |
Nikolay Borisov | 97db120 | 2019-03-12 17:20:24 +0200 | [diff] [blame] | 449 | struct async_chunk { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 450 | struct inode *inode; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 451 | struct page *locked_page; |
| 452 | u64 start; |
| 453 | u64 end; |
Liu Bo | f82b735 | 2017-10-23 23:18:16 -0600 | [diff] [blame] | 454 | unsigned int write_flags; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 455 | struct list_head extents; |
Chris Mason | ec39f76 | 2019-07-10 12:28:17 -0700 | [diff] [blame] | 456 | struct cgroup_subsys_state *blkcg_css; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 457 | struct btrfs_work work; |
Nikolay Borisov | 97db120 | 2019-03-12 17:20:24 +0200 | [diff] [blame] | 458 | atomic_t *pending; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 459 | }; |
| 460 | |
Nikolay Borisov | 97db120 | 2019-03-12 17:20:24 +0200 | [diff] [blame] | 461 | struct async_cow { |
| 462 | /* Number of chunks in flight; must be first in the structure */ |
| 463 | atomic_t num_chunks; |
| 464 | struct async_chunk chunks[]; |
| 465 | }; |
| 466 | |
| 467 | static noinline int add_async_extent(struct async_chunk *cow, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 468 | u64 start, u64 ram_size, |
| 469 | u64 compressed_size, |
| 470 | struct page **pages, |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 471 | unsigned long nr_pages, |
| 472 | int compress_type) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 473 | { |
| 474 | struct async_extent *async_extent; |
| 475 | |
| 476 | async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 477 | BUG_ON(!async_extent); /* -ENOMEM */ |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 478 | async_extent->start = start; |
| 479 | async_extent->ram_size = ram_size; |
| 480 | async_extent->compressed_size = compressed_size; |
| 481 | async_extent->pages = pages; |
| 482 | async_extent->nr_pages = nr_pages; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 483 | async_extent->compress_type = compress_type; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 484 | list_add_tail(&async_extent->list, &cow->extents); |
| 485 | return 0; |
| 486 | } |
| 487 | |
Qu Wenruo | 42c16da | 2019-07-01 05:12:46 +0000 | [diff] [blame] | 488 | /* |
| 489 | * Check if the inode has flags compatible with compression |
| 490 | */ |
Nikolay Borisov | 99c88dc | 2020-06-03 08:55:26 +0300 | [diff] [blame] | 491 | static inline bool inode_can_compress(struct btrfs_inode *inode) |
Qu Wenruo | 42c16da | 2019-07-01 05:12:46 +0000 | [diff] [blame] | 492 | { |
Qu Wenruo | 95ea048 | 2021-07-26 14:35:06 +0800 | [diff] [blame] | 493 | /* Subpage doesn't support compression yet */ |
| 494 | if (inode->root->fs_info->sectorsize < PAGE_SIZE) |
| 495 | return false; |
Nikolay Borisov | 99c88dc | 2020-06-03 08:55:26 +0300 | [diff] [blame] | 496 | if (inode->flags & BTRFS_INODE_NODATACOW || |
| 497 | inode->flags & BTRFS_INODE_NODATASUM) |
Qu Wenruo | 42c16da | 2019-07-01 05:12:46 +0000 | [diff] [blame] | 498 | return false; |
| 499 | return true; |
| 500 | } |
| 501 | |
| 502 | /* |
| 503 | * Check if the inode needs to be submitted to compression, based on mount |
| 504 | * options, defragmentation, properties or heuristics. |
| 505 | */ |
Nikolay Borisov | 808a129 | 2020-06-03 08:55:27 +0300 | [diff] [blame] | 506 | static inline int inode_need_compress(struct btrfs_inode *inode, u64 start, |
| 507 | u64 end) |
Wang Shilong | f79707b | 2014-07-17 11:44:09 +0800 | [diff] [blame] | 508 | { |
Nikolay Borisov | 808a129 | 2020-06-03 08:55:27 +0300 | [diff] [blame] | 509 | struct btrfs_fs_info *fs_info = inode->root->fs_info; |
Wang Shilong | f79707b | 2014-07-17 11:44:09 +0800 | [diff] [blame] | 510 | |
Nikolay Borisov | 808a129 | 2020-06-03 08:55:27 +0300 | [diff] [blame] | 511 | if (!inode_can_compress(inode)) { |
Qu Wenruo | 42c16da | 2019-07-01 05:12:46 +0000 | [diff] [blame] | 512 | WARN(IS_ENABLED(CONFIG_BTRFS_DEBUG), |
| 513 | KERN_ERR "BTRFS: unexpected compression for ino %llu\n", |
Nikolay Borisov | 808a129 | 2020-06-03 08:55:27 +0300 | [diff] [blame] | 514 | btrfs_ino(inode)); |
Qu Wenruo | 42c16da | 2019-07-01 05:12:46 +0000 | [diff] [blame] | 515 | return 0; |
| 516 | } |
Wang Shilong | f79707b | 2014-07-17 11:44:09 +0800 | [diff] [blame] | 517 | /* force compress */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 518 | if (btrfs_test_opt(fs_info, FORCE_COMPRESS)) |
Wang Shilong | f79707b | 2014-07-17 11:44:09 +0800 | [diff] [blame] | 519 | return 1; |
David Sterba | eec63c6 | 2017-07-17 19:41:31 +0200 | [diff] [blame] | 520 | /* defrag ioctl */ |
Nikolay Borisov | 808a129 | 2020-06-03 08:55:27 +0300 | [diff] [blame] | 521 | if (inode->defrag_compress) |
David Sterba | eec63c6 | 2017-07-17 19:41:31 +0200 | [diff] [blame] | 522 | return 1; |
Wang Shilong | f79707b | 2014-07-17 11:44:09 +0800 | [diff] [blame] | 523 | /* bad compression ratios */ |
Nikolay Borisov | 808a129 | 2020-06-03 08:55:27 +0300 | [diff] [blame] | 524 | if (inode->flags & BTRFS_INODE_NOCOMPRESS) |
Wang Shilong | f79707b | 2014-07-17 11:44:09 +0800 | [diff] [blame] | 525 | return 0; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 526 | if (btrfs_test_opt(fs_info, COMPRESS) || |
Nikolay Borisov | 808a129 | 2020-06-03 08:55:27 +0300 | [diff] [blame] | 527 | inode->flags & BTRFS_INODE_COMPRESS || |
| 528 | inode->prop_compress) |
| 529 | return btrfs_compress_heuristic(&inode->vfs_inode, start, end); |
Wang Shilong | f79707b | 2014-07-17 11:44:09 +0800 | [diff] [blame] | 530 | return 0; |
| 531 | } |
| 532 | |
Nikolay Borisov | 6158e1c | 2017-02-20 13:50:43 +0200 | [diff] [blame] | 533 | static inline void inode_should_defrag(struct btrfs_inode *inode, |
Anand Jain | 26d30f8 | 2016-12-19 19:09:06 +0800 | [diff] [blame] | 534 | u64 start, u64 end, u64 num_bytes, u64 small_write) |
| 535 | { |
| 536 | /* If this is a small write inside eof, kick off a defrag */ |
| 537 | if (num_bytes < small_write && |
Nikolay Borisov | 6158e1c | 2017-02-20 13:50:43 +0200 | [diff] [blame] | 538 | (start > 0 || end + 1 < inode->disk_i_size)) |
Anand Jain | 26d30f8 | 2016-12-19 19:09:06 +0800 | [diff] [blame] | 539 | btrfs_add_inode_defrag(NULL, inode); |
| 540 | } |
| 541 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 542 | /* |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 543 | * we create compressed extents in two phases. The first |
| 544 | * phase compresses a range of pages that have already been |
| 545 | * locked (both pages and state bits are locked). |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 546 | * |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 547 | * This is done inside an ordered work queue, and the compression |
| 548 | * is spread across many cpus. The actual IO submission is step |
| 549 | * two, and the ordered work queue takes care of making sure that |
| 550 | * happens in the same order things were put onto the queue by |
| 551 | * writepages and friends. |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 552 | * |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 553 | * If this code finds it can't get good compression, it puts an |
| 554 | * entry onto the work queue to write the uncompressed bytes. This |
| 555 | * makes sure that both compressed inodes and uncompressed inodes |
Artem Bityutskiy | b257031 | 2012-07-25 18:12:06 +0300 | [diff] [blame] | 556 | * are written in the same order that the flusher thread sent them |
| 557 | * down. |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 558 | */ |
Nikolay Borisov | ac3e993 | 2019-07-17 14:41:44 +0300 | [diff] [blame] | 559 | static noinline int compress_file_range(struct async_chunk *async_chunk) |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 560 | { |
Nikolay Borisov | 1368c6d | 2019-03-12 17:20:27 +0200 | [diff] [blame] | 561 | struct inode *inode = async_chunk->inode; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 562 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 563 | u64 blocksize = fs_info->sectorsize; |
Nikolay Borisov | 1368c6d | 2019-03-12 17:20:27 +0200 | [diff] [blame] | 564 | u64 start = async_chunk->start; |
| 565 | u64 end = async_chunk->end; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 566 | u64 actual_end; |
Josef Bacik | d98da49 | 2019-10-11 09:03:54 -0400 | [diff] [blame] | 567 | u64 i_size; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 568 | int ret = 0; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 569 | struct page **pages = NULL; |
| 570 | unsigned long nr_pages; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 571 | unsigned long total_compressed = 0; |
| 572 | unsigned long total_in = 0; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 573 | int i; |
| 574 | int will_compress; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 575 | int compress_type = fs_info->compress_type; |
Nikolay Borisov | ac3e993 | 2019-07-17 14:41:44 +0300 | [diff] [blame] | 576 | int compressed_extents = 0; |
Chris Mason | 4adaa61 | 2013-03-26 13:07:00 -0400 | [diff] [blame] | 577 | int redirty = 0; |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 578 | |
Nikolay Borisov | 6158e1c | 2017-02-20 13:50:43 +0200 | [diff] [blame] | 579 | inode_should_defrag(BTRFS_I(inode), start, end, end - start + 1, |
| 580 | SZ_16K); |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 581 | |
Josef Bacik | d98da49 | 2019-10-11 09:03:54 -0400 | [diff] [blame] | 582 | /* |
| 583 | * We need to save i_size before now because it could change in between |
| 584 | * us evaluating the size and assigning it. This is because we lock and |
| 585 | * unlock the page in truncate and fallocate, and then modify the i_size |
| 586 | * later on. |
| 587 | * |
| 588 | * The barriers are to emulate READ_ONCE, remove that once i_size_read |
| 589 | * does that for us. |
| 590 | */ |
| 591 | barrier(); |
| 592 | i_size = i_size_read(inode); |
| 593 | barrier(); |
| 594 | actual_end = min_t(u64, i_size, end + 1); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 595 | again: |
| 596 | will_compress = 0; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 597 | nr_pages = (end >> PAGE_SHIFT) - (start >> PAGE_SHIFT) + 1; |
David Sterba | 069eac7 | 2017-02-14 19:36:54 +0100 | [diff] [blame] | 598 | BUILD_BUG_ON((BTRFS_MAX_COMPRESSED % PAGE_SIZE) != 0); |
| 599 | nr_pages = min_t(unsigned long, nr_pages, |
| 600 | BTRFS_MAX_COMPRESSED / PAGE_SIZE); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 601 | |
Chris Mason | f03d9301f | 2009-02-04 09:31:06 -0500 | [diff] [blame] | 602 | /* |
| 603 | * we don't want to send crud past the end of i_size through |
| 604 | * compression, that's just a waste of CPU time. So, if the |
| 605 | * end of the file is before the start of our current |
| 606 | * requested range of bytes, we bail out to the uncompressed |
| 607 | * cleanup code that can deal with all of this. |
| 608 | * |
| 609 | * It isn't really the fastest way to fix things, but this is a |
| 610 | * very uncommon corner. |
| 611 | */ |
| 612 | if (actual_end <= start) |
| 613 | goto cleanup_and_bail_uncompressed; |
| 614 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 615 | total_compressed = actual_end - start; |
| 616 | |
Shilong Wang | 4bcbb33 | 2014-10-07 18:44:35 -0400 | [diff] [blame] | 617 | /* |
| 618 | * skip compression for a small file range(<=blocksize) that |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 619 | * isn't an inline extent, since it doesn't save disk space at all. |
Shilong Wang | 4bcbb33 | 2014-10-07 18:44:35 -0400 | [diff] [blame] | 620 | */ |
| 621 | if (total_compressed <= blocksize && |
| 622 | (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size)) |
| 623 | goto cleanup_and_bail_uncompressed; |
| 624 | |
David Sterba | 069eac7 | 2017-02-14 19:36:54 +0100 | [diff] [blame] | 625 | total_compressed = min_t(unsigned long, total_compressed, |
| 626 | BTRFS_MAX_UNCOMPRESSED); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 627 | total_in = 0; |
| 628 | ret = 0; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 629 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 630 | /* |
| 631 | * we do compression for mount -o compress and when the |
| 632 | * inode has not been flagged as nocompress. This flag can |
| 633 | * change at any time if we discover bad compression ratios. |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 634 | */ |
David Sterba | f216562 | 2021-06-14 12:45:18 +0200 | [diff] [blame] | 635 | if (nr_pages > 1 && inode_need_compress(BTRFS_I(inode), start, end)) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 636 | WARN_ON(pages); |
David Sterba | 31e818f | 2015-02-20 18:00:26 +0100 | [diff] [blame] | 637 | pages = kcalloc(nr_pages, sizeof(struct page *), GFP_NOFS); |
Li Zefan | 560f7d7 | 2011-09-08 10:22:01 +0800 | [diff] [blame] | 638 | if (!pages) { |
| 639 | /* just bail out to the uncompressed code */ |
Filipe Manana | 3527a01 | 2018-10-13 00:37:25 +0100 | [diff] [blame] | 640 | nr_pages = 0; |
Li Zefan | 560f7d7 | 2011-09-08 10:22:01 +0800 | [diff] [blame] | 641 | goto cont; |
| 642 | } |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 643 | |
David Sterba | eec63c6 | 2017-07-17 19:41:31 +0200 | [diff] [blame] | 644 | if (BTRFS_I(inode)->defrag_compress) |
| 645 | compress_type = BTRFS_I(inode)->defrag_compress; |
| 646 | else if (BTRFS_I(inode)->prop_compress) |
David Sterba | b52aa8c | 2017-07-17 19:17:20 +0200 | [diff] [blame] | 647 | compress_type = BTRFS_I(inode)->prop_compress; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 648 | |
Chris Mason | 4adaa61 | 2013-03-26 13:07:00 -0400 | [diff] [blame] | 649 | /* |
| 650 | * we need to call clear_page_dirty_for_io on each |
| 651 | * page in the range. Otherwise applications with the file |
| 652 | * mmap'd can wander in and change the page contents while |
| 653 | * we are compressing them. |
| 654 | * |
| 655 | * If the compression fails for any reason, we set the pages |
| 656 | * dirty again later on. |
Timofey Titovets | e9679de | 2017-10-24 01:29:48 +0300 | [diff] [blame] | 657 | * |
| 658 | * Note that the remaining part is redirtied, the start pointer |
| 659 | * has moved, the end is the original one. |
Chris Mason | 4adaa61 | 2013-03-26 13:07:00 -0400 | [diff] [blame] | 660 | */ |
Timofey Titovets | e9679de | 2017-10-24 01:29:48 +0300 | [diff] [blame] | 661 | if (!redirty) { |
| 662 | extent_range_clear_dirty_for_io(inode, start, end); |
| 663 | redirty = 1; |
| 664 | } |
David Sterba | f51d2b5 | 2017-09-15 17:36:57 +0200 | [diff] [blame] | 665 | |
| 666 | /* Compression level is applied here and only here */ |
| 667 | ret = btrfs_compress_pages( |
| 668 | compress_type | (fs_info->compress_level << 4), |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 669 | inode->i_mapping, start, |
David Sterba | 38c3146 | 2017-02-14 19:04:07 +0100 | [diff] [blame] | 670 | pages, |
David Sterba | 4d3a800 | 2017-02-14 19:04:07 +0100 | [diff] [blame] | 671 | &nr_pages, |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 672 | &total_in, |
David Sterba | e5d7490 | 2017-02-14 19:45:05 +0100 | [diff] [blame] | 673 | &total_compressed); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 674 | |
| 675 | if (!ret) { |
Johannes Thumshirn | 7073017 | 2018-12-05 15:23:03 +0100 | [diff] [blame] | 676 | unsigned long offset = offset_in_page(total_compressed); |
David Sterba | 4d3a800 | 2017-02-14 19:04:07 +0100 | [diff] [blame] | 677 | struct page *page = pages[nr_pages - 1]; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 678 | |
| 679 | /* zero the tail end of the last page, we might be |
| 680 | * sending it down to disk |
| 681 | */ |
Ira Weiny | d048b9c | 2021-05-04 18:40:07 -0700 | [diff] [blame] | 682 | if (offset) |
| 683 | memzero_page(page, offset, PAGE_SIZE - offset); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 684 | will_compress = 1; |
| 685 | } |
| 686 | } |
Li Zefan | 560f7d7 | 2011-09-08 10:22:01 +0800 | [diff] [blame] | 687 | cont: |
Qu Wenruo | 7367253 | 2021-07-26 14:34:59 +0800 | [diff] [blame] | 688 | /* |
| 689 | * Check cow_file_range() for why we don't even try to create inline |
| 690 | * extent for subpage case. |
| 691 | */ |
| 692 | if (start == 0 && fs_info->sectorsize == PAGE_SIZE) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 693 | /* lets try to make an inline extent */ |
Timofey Titovets | 6018ba0 | 2017-09-15 01:57:26 +0300 | [diff] [blame] | 694 | if (ret || total_in < actual_end) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 695 | /* we didn't compress the entire range, try |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 696 | * to make an uncompressed inline extent. |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 697 | */ |
Nikolay Borisov | a034940 | 2020-06-03 08:55:12 +0300 | [diff] [blame] | 698 | ret = cow_file_range_inline(BTRFS_I(inode), start, end, |
| 699 | 0, BTRFS_COMPRESS_NONE, |
| 700 | NULL); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 701 | } else { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 702 | /* try making a compressed inline extent */ |
Nikolay Borisov | a034940 | 2020-06-03 08:55:12 +0300 | [diff] [blame] | 703 | ret = cow_file_range_inline(BTRFS_I(inode), start, end, |
Li Zefan | fe3f566 | 2011-03-28 08:30:38 +0000 | [diff] [blame] | 704 | total_compressed, |
| 705 | compress_type, pages); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 706 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 707 | if (ret <= 0) { |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 708 | unsigned long clear_flags = EXTENT_DELALLOC | |
Josef Bacik | 8b62f87 | 2017-10-19 14:15:55 -0400 | [diff] [blame] | 709 | EXTENT_DELALLOC_NEW | EXTENT_DEFRAG | |
| 710 | EXTENT_DO_ACCOUNTING; |
Filipe Manana | e6eb431 | 2014-10-10 10:45:12 +0100 | [diff] [blame] | 711 | unsigned long page_error_op; |
| 712 | |
Filipe Manana | e6eb431 | 2014-10-10 10:45:12 +0100 | [diff] [blame] | 713 | page_error_op = ret < 0 ? PAGE_SET_ERROR : 0; |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 714 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 715 | /* |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 716 | * inline extent creation worked or returned error, |
| 717 | * we don't need to create any more async work items. |
| 718 | * Unlock and free up our temp pages. |
Josef Bacik | 8b62f87 | 2017-10-19 14:15:55 -0400 | [diff] [blame] | 719 | * |
| 720 | * We use DO_ACCOUNTING here because we need the |
| 721 | * delalloc_release_metadata to be done _after_ we drop |
| 722 | * our outstanding extent for clearing delalloc for this |
| 723 | * range. |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 724 | */ |
Nikolay Borisov | ad7ff17 | 2020-06-03 08:55:06 +0300 | [diff] [blame] | 725 | extent_clear_unlock_delalloc(BTRFS_I(inode), start, end, |
| 726 | NULL, |
Nikolay Borisov | 74e9194 | 2019-07-17 16:18:16 +0300 | [diff] [blame] | 727 | clear_flags, |
Qu Wenruo | ba8b04c | 2016-07-19 16:50:36 +0800 | [diff] [blame] | 728 | PAGE_UNLOCK | |
Qu Wenruo | 6869b0a | 2021-01-26 16:33:45 +0800 | [diff] [blame] | 729 | PAGE_START_WRITEBACK | |
Filipe Manana | e6eb431 | 2014-10-10 10:45:12 +0100 | [diff] [blame] | 730 | page_error_op | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 731 | PAGE_END_WRITEBACK); |
Nikolay Borisov | cecc8d9 | 2019-07-17 14:41:45 +0300 | [diff] [blame] | 732 | |
Qu Wenruo | 1e6e238 | 2020-07-28 16:39:26 +0800 | [diff] [blame] | 733 | /* |
| 734 | * Ensure we only free the compressed pages if we have |
| 735 | * them allocated, as we can still reach here with |
| 736 | * inode_need_compress() == false. |
| 737 | */ |
| 738 | if (pages) { |
| 739 | for (i = 0; i < nr_pages; i++) { |
| 740 | WARN_ON(pages[i]->mapping); |
| 741 | put_page(pages[i]); |
| 742 | } |
| 743 | kfree(pages); |
Nikolay Borisov | cecc8d9 | 2019-07-17 14:41:45 +0300 | [diff] [blame] | 744 | } |
Nikolay Borisov | cecc8d9 | 2019-07-17 14:41:45 +0300 | [diff] [blame] | 745 | return 0; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 746 | } |
| 747 | } |
| 748 | |
| 749 | if (will_compress) { |
| 750 | /* |
| 751 | * we aren't doing an inline extent round the compressed size |
| 752 | * up to a block size boundary so the allocator does sane |
| 753 | * things |
| 754 | */ |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 755 | total_compressed = ALIGN(total_compressed, blocksize); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 756 | |
| 757 | /* |
| 758 | * one last check to make sure the compression is really a |
Timofey Titovets | 170607e | 2017-06-06 14:41:15 +0300 | [diff] [blame] | 759 | * win, compare the page count read with the blocks on disk, |
| 760 | * compression must free at least one sector size |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 761 | */ |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 762 | total_in = ALIGN(total_in, PAGE_SIZE); |
Timofey Titovets | 170607e | 2017-06-06 14:41:15 +0300 | [diff] [blame] | 763 | if (total_compressed + blocksize <= total_in) { |
Nikolay Borisov | ac3e993 | 2019-07-17 14:41:44 +0300 | [diff] [blame] | 764 | compressed_extents++; |
Ashish Samant | c8bb0c8 | 2016-03-25 19:01:33 -0700 | [diff] [blame] | 765 | |
| 766 | /* |
| 767 | * The async work queues will take care of doing actual |
| 768 | * allocation on disk for these compressed pages, and |
| 769 | * will submit them to the elevator. |
| 770 | */ |
Nikolay Borisov | b532627 | 2019-03-12 17:20:25 +0200 | [diff] [blame] | 771 | add_async_extent(async_chunk, start, total_in, |
David Sterba | 4d3a800 | 2017-02-14 19:04:07 +0100 | [diff] [blame] | 772 | total_compressed, pages, nr_pages, |
Ashish Samant | c8bb0c8 | 2016-03-25 19:01:33 -0700 | [diff] [blame] | 773 | compress_type); |
| 774 | |
Timofey Titovets | 1170862 | 2017-10-03 18:06:01 +0300 | [diff] [blame] | 775 | if (start + total_in < end) { |
| 776 | start += total_in; |
Ashish Samant | c8bb0c8 | 2016-03-25 19:01:33 -0700 | [diff] [blame] | 777 | pages = NULL; |
| 778 | cond_resched(); |
| 779 | goto again; |
| 780 | } |
Nikolay Borisov | ac3e993 | 2019-07-17 14:41:44 +0300 | [diff] [blame] | 781 | return compressed_extents; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 782 | } |
| 783 | } |
Ashish Samant | c8bb0c8 | 2016-03-25 19:01:33 -0700 | [diff] [blame] | 784 | if (pages) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 785 | /* |
| 786 | * the compression code ran but failed to make things smaller, |
| 787 | * free any pages it allocated and our page pointer array |
| 788 | */ |
David Sterba | 4d3a800 | 2017-02-14 19:04:07 +0100 | [diff] [blame] | 789 | for (i = 0; i < nr_pages; i++) { |
Chris Mason | 70b99e6 | 2008-10-31 12:46:39 -0400 | [diff] [blame] | 790 | WARN_ON(pages[i]->mapping); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 791 | put_page(pages[i]); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 792 | } |
| 793 | kfree(pages); |
| 794 | pages = NULL; |
| 795 | total_compressed = 0; |
David Sterba | 4d3a800 | 2017-02-14 19:04:07 +0100 | [diff] [blame] | 796 | nr_pages = 0; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 797 | |
| 798 | /* flag the file so we don't compress in the future */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 799 | if (!btrfs_test_opt(fs_info, FORCE_COMPRESS) && |
David Sterba | b52aa8c | 2017-07-17 19:17:20 +0200 | [diff] [blame] | 800 | !(BTRFS_I(inode)->prop_compress)) { |
Chris Mason | a555f81 | 2010-01-28 16:18:15 -0500 | [diff] [blame] | 801 | BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS; |
Chris Mason | 1e701a3 | 2010-03-11 09:42:04 -0500 | [diff] [blame] | 802 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 803 | } |
Chris Mason | f03d9301f | 2009-02-04 09:31:06 -0500 | [diff] [blame] | 804 | cleanup_and_bail_uncompressed: |
Ashish Samant | c8bb0c8 | 2016-03-25 19:01:33 -0700 | [diff] [blame] | 805 | /* |
| 806 | * No compression, but we still need to write the pages in the file |
| 807 | * we've been given so far. redirty the locked page if it corresponds |
| 808 | * to our extent and set things up for the async work queue to run |
| 809 | * cow_file_range to do the normal delalloc dance. |
| 810 | */ |
Chris Mason | 1d53c9e | 2019-07-10 12:28:16 -0700 | [diff] [blame] | 811 | if (async_chunk->locked_page && |
| 812 | (page_offset(async_chunk->locked_page) >= start && |
| 813 | page_offset(async_chunk->locked_page)) <= end) { |
Nikolay Borisov | 1368c6d | 2019-03-12 17:20:27 +0200 | [diff] [blame] | 814 | __set_page_dirty_nobuffers(async_chunk->locked_page); |
Ashish Samant | c8bb0c8 | 2016-03-25 19:01:33 -0700 | [diff] [blame] | 815 | /* unlocked later on in the async handlers */ |
Chris Mason | 1d53c9e | 2019-07-10 12:28:16 -0700 | [diff] [blame] | 816 | } |
Ashish Samant | c8bb0c8 | 2016-03-25 19:01:33 -0700 | [diff] [blame] | 817 | |
| 818 | if (redirty) |
| 819 | extent_range_redirty_for_io(inode, start, end); |
Nikolay Borisov | b532627 | 2019-03-12 17:20:25 +0200 | [diff] [blame] | 820 | add_async_extent(async_chunk, start, end - start + 1, 0, NULL, 0, |
Ashish Samant | c8bb0c8 | 2016-03-25 19:01:33 -0700 | [diff] [blame] | 821 | BTRFS_COMPRESS_NONE); |
Nikolay Borisov | ac3e993 | 2019-07-17 14:41:44 +0300 | [diff] [blame] | 822 | compressed_extents++; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 823 | |
Nikolay Borisov | ac3e993 | 2019-07-17 14:41:44 +0300 | [diff] [blame] | 824 | return compressed_extents; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 825 | } |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 826 | |
Filipe Manana | 40ae837 | 2014-10-06 22:14:24 +0100 | [diff] [blame] | 827 | static void free_async_extent_pages(struct async_extent *async_extent) |
| 828 | { |
| 829 | int i; |
| 830 | |
| 831 | if (!async_extent->pages) |
| 832 | return; |
| 833 | |
| 834 | for (i = 0; i < async_extent->nr_pages; i++) { |
| 835 | WARN_ON(async_extent->pages[i]->mapping); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 836 | put_page(async_extent->pages[i]); |
Filipe Manana | 40ae837 | 2014-10-06 22:14:24 +0100 | [diff] [blame] | 837 | } |
| 838 | kfree(async_extent->pages); |
| 839 | async_extent->nr_pages = 0; |
| 840 | async_extent->pages = NULL; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 841 | } |
| 842 | |
| 843 | /* |
| 844 | * phase two of compressed writeback. This is the ordered portion |
| 845 | * of the code, which only gets called in the order the work was |
| 846 | * queued. We walk all the async extents created by compress_file_range |
| 847 | * and send them down to the disk. |
| 848 | */ |
Nikolay Borisov | b532627 | 2019-03-12 17:20:25 +0200 | [diff] [blame] | 849 | static noinline void submit_compressed_extents(struct async_chunk *async_chunk) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 850 | { |
Nikolay Borisov | a0ff10d | 2020-06-03 08:55:17 +0300 | [diff] [blame] | 851 | struct btrfs_inode *inode = BTRFS_I(async_chunk->inode); |
| 852 | struct btrfs_fs_info *fs_info = inode->root->fs_info; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 853 | struct async_extent *async_extent; |
| 854 | u64 alloc_hint = 0; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 855 | struct btrfs_key ins; |
| 856 | struct extent_map *em; |
Nikolay Borisov | a0ff10d | 2020-06-03 08:55:17 +0300 | [diff] [blame] | 857 | struct btrfs_root *root = inode->root; |
| 858 | struct extent_io_tree *io_tree = &inode->io_tree; |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 859 | int ret = 0; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 860 | |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 861 | again: |
Nikolay Borisov | b532627 | 2019-03-12 17:20:25 +0200 | [diff] [blame] | 862 | while (!list_empty(&async_chunk->extents)) { |
| 863 | async_extent = list_entry(async_chunk->extents.next, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 864 | struct async_extent, list); |
| 865 | list_del(&async_extent->list); |
| 866 | |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 867 | retry: |
Nikolay Borisov | 7447555 | 2019-03-12 17:20:30 +0200 | [diff] [blame] | 868 | lock_extent(io_tree, async_extent->start, |
| 869 | async_extent->start + async_extent->ram_size - 1); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 870 | /* did the compression code fall back to uncompressed IO? */ |
| 871 | if (!async_extent->pages) { |
| 872 | int page_started = 0; |
| 873 | unsigned long nr_written = 0; |
| 874 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 875 | /* allocate blocks */ |
Nikolay Borisov | a0ff10d | 2020-06-03 08:55:17 +0300 | [diff] [blame] | 876 | ret = cow_file_range(inode, async_chunk->locked_page, |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 877 | async_extent->start, |
| 878 | async_extent->start + |
| 879 | async_extent->ram_size - 1, |
Nikolay Borisov | 330a582 | 2019-07-17 16:18:17 +0300 | [diff] [blame] | 880 | &page_started, &nr_written, 0); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 881 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 882 | /* JDM XXX */ |
| 883 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 884 | /* |
| 885 | * if page_started, cow_file_range inserted an |
| 886 | * inline extent and took care of all the unlocking |
| 887 | * and IO for us. Otherwise, we need to submit |
| 888 | * all those pages down to the drive. |
| 889 | */ |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 890 | if (!page_started && !ret) |
Nikolay Borisov | a0ff10d | 2020-06-03 08:55:17 +0300 | [diff] [blame] | 891 | extent_write_locked_range(&inode->vfs_inode, |
Nikolay Borisov | 5e3ee23 | 2017-12-08 15:55:58 +0200 | [diff] [blame] | 892 | async_extent->start, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 893 | async_extent->start + |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 894 | async_extent->ram_size - 1, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 895 | WB_SYNC_ALL); |
Chris Mason | 1d53c9e | 2019-07-10 12:28:16 -0700 | [diff] [blame] | 896 | else if (ret && async_chunk->locked_page) |
Nikolay Borisov | b532627 | 2019-03-12 17:20:25 +0200 | [diff] [blame] | 897 | unlock_page(async_chunk->locked_page); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 898 | kfree(async_extent); |
| 899 | cond_resched(); |
| 900 | continue; |
| 901 | } |
| 902 | |
Wang Xiaoguang | 1851309 | 2016-07-25 15:51:40 +0800 | [diff] [blame] | 903 | ret = btrfs_reserve_extent(root, async_extent->ram_size, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 904 | async_extent->compressed_size, |
| 905 | async_extent->compressed_size, |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 906 | 0, alloc_hint, &ins, 1, 1); |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 907 | if (ret) { |
Filipe Manana | 40ae837 | 2014-10-06 22:14:24 +0100 | [diff] [blame] | 908 | free_async_extent_pages(async_extent); |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 909 | |
Josef Bacik | fdf8e2e | 2013-06-14 16:58:23 -0400 | [diff] [blame] | 910 | if (ret == -ENOSPC) { |
| 911 | unlock_extent(io_tree, async_extent->start, |
| 912 | async_extent->start + |
| 913 | async_extent->ram_size - 1); |
Liu Bo | ce62003 | 2014-07-24 22:48:05 +0800 | [diff] [blame] | 914 | |
| 915 | /* |
| 916 | * we need to redirty the pages if we decide to |
| 917 | * fallback to uncompressed IO, otherwise we |
| 918 | * will not submit these pages down to lower |
| 919 | * layers. |
| 920 | */ |
Nikolay Borisov | a0ff10d | 2020-06-03 08:55:17 +0300 | [diff] [blame] | 921 | extent_range_redirty_for_io(&inode->vfs_inode, |
Liu Bo | ce62003 | 2014-07-24 22:48:05 +0800 | [diff] [blame] | 922 | async_extent->start, |
| 923 | async_extent->start + |
| 924 | async_extent->ram_size - 1); |
| 925 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 926 | goto retry; |
Josef Bacik | fdf8e2e | 2013-06-14 16:58:23 -0400 | [diff] [blame] | 927 | } |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 928 | goto out_free; |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 929 | } |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 930 | /* |
| 931 | * here we're doing allocation and writeback of the |
| 932 | * compressed pages |
| 933 | */ |
Nikolay Borisov | a0ff10d | 2020-06-03 08:55:17 +0300 | [diff] [blame] | 934 | em = create_io_em(inode, async_extent->start, |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 935 | async_extent->ram_size, /* len */ |
| 936 | async_extent->start, /* orig_start */ |
| 937 | ins.objectid, /* block_start */ |
| 938 | ins.offset, /* block_len */ |
| 939 | ins.offset, /* orig_block_len */ |
| 940 | async_extent->ram_size, /* ram_bytes */ |
| 941 | async_extent->compress_type, |
| 942 | BTRFS_ORDERED_COMPRESSED); |
| 943 | if (IS_ERR(em)) |
| 944 | /* ret value is not necessary due to void function */ |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 945 | goto out_free_reserve; |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 946 | free_extent_map(em); |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 947 | |
Nikolay Borisov | a0ff10d | 2020-06-03 08:55:17 +0300 | [diff] [blame] | 948 | ret = btrfs_add_ordered_extent_compress(inode, |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 949 | async_extent->start, |
| 950 | ins.objectid, |
| 951 | async_extent->ram_size, |
| 952 | ins.offset, |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 953 | async_extent->compress_type); |
Filipe Manana | d9f8596 | 2014-08-25 10:43:00 +0100 | [diff] [blame] | 954 | if (ret) { |
Nikolay Borisov | a0ff10d | 2020-06-03 08:55:17 +0300 | [diff] [blame] | 955 | btrfs_drop_extent_cache(inode, async_extent->start, |
Filipe Manana | d9f8596 | 2014-08-25 10:43:00 +0100 | [diff] [blame] | 956 | async_extent->start + |
| 957 | async_extent->ram_size - 1, 0); |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 958 | goto out_free_reserve; |
Filipe Manana | d9f8596 | 2014-08-25 10:43:00 +0100 | [diff] [blame] | 959 | } |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 960 | btrfs_dec_block_group_reservations(fs_info, ins.objectid); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 961 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 962 | /* |
| 963 | * clear dirty, set writeback and unlock the pages. |
| 964 | */ |
Nikolay Borisov | a0ff10d | 2020-06-03 08:55:17 +0300 | [diff] [blame] | 965 | extent_clear_unlock_delalloc(inode, async_extent->start, |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 966 | async_extent->start + |
| 967 | async_extent->ram_size - 1, |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 968 | NULL, EXTENT_LOCKED | EXTENT_DELALLOC, |
Qu Wenruo | 6869b0a | 2021-01-26 16:33:45 +0800 | [diff] [blame] | 969 | PAGE_UNLOCK | PAGE_START_WRITEBACK); |
Nikolay Borisov | a0ff10d | 2020-06-03 08:55:17 +0300 | [diff] [blame] | 970 | if (btrfs_submit_compressed_write(inode, async_extent->start, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 971 | async_extent->ram_size, |
| 972 | ins.objectid, |
| 973 | ins.offset, async_extent->pages, |
Liu Bo | f82b735 | 2017-10-23 23:18:16 -0600 | [diff] [blame] | 974 | async_extent->nr_pages, |
Chris Mason | ec39f76 | 2019-07-10 12:28:17 -0700 | [diff] [blame] | 975 | async_chunk->write_flags, |
| 976 | async_chunk->blkcg_css)) { |
Filipe Manana | fce2a4e | 2014-10-06 22:14:23 +0100 | [diff] [blame] | 977 | struct page *p = async_extent->pages[0]; |
| 978 | const u64 start = async_extent->start; |
| 979 | const u64 end = start + async_extent->ram_size - 1; |
| 980 | |
Nikolay Borisov | a0ff10d | 2020-06-03 08:55:17 +0300 | [diff] [blame] | 981 | p->mapping = inode->vfs_inode.i_mapping; |
Qu Wenruo | 38a39ac7 | 2021-04-08 20:32:27 +0800 | [diff] [blame] | 982 | btrfs_writepage_endio_finish_ordered(inode, p, start, |
David Sterba | 25c1252 | 2021-07-26 14:15:08 +0200 | [diff] [blame] | 983 | end, false); |
Nikolay Borisov | 7087a9d | 2018-11-01 14:09:48 +0200 | [diff] [blame] | 984 | |
Filipe Manana | fce2a4e | 2014-10-06 22:14:23 +0100 | [diff] [blame] | 985 | p->mapping = NULL; |
Nikolay Borisov | a0ff10d | 2020-06-03 08:55:17 +0300 | [diff] [blame] | 986 | extent_clear_unlock_delalloc(inode, start, end, NULL, 0, |
Filipe Manana | fce2a4e | 2014-10-06 22:14:23 +0100 | [diff] [blame] | 987 | PAGE_END_WRITEBACK | |
| 988 | PAGE_SET_ERROR); |
Filipe Manana | 40ae837 | 2014-10-06 22:14:24 +0100 | [diff] [blame] | 989 | free_async_extent_pages(async_extent); |
Filipe Manana | fce2a4e | 2014-10-06 22:14:23 +0100 | [diff] [blame] | 990 | } |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 991 | alloc_hint = ins.objectid + ins.offset; |
| 992 | kfree(async_extent); |
| 993 | cond_resched(); |
| 994 | } |
Filipe Manana | dec8f17 | 2014-10-06 22:14:26 +0100 | [diff] [blame] | 995 | return; |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 996 | out_free_reserve: |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 997 | btrfs_dec_block_group_reservations(fs_info, ins.objectid); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 998 | btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 999 | out_free: |
Nikolay Borisov | a0ff10d | 2020-06-03 08:55:17 +0300 | [diff] [blame] | 1000 | extent_clear_unlock_delalloc(inode, async_extent->start, |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 1001 | async_extent->start + |
| 1002 | async_extent->ram_size - 1, |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1003 | NULL, EXTENT_LOCKED | EXTENT_DELALLOC | |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 1004 | EXTENT_DELALLOC_NEW | |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 1005 | EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING, |
Qu Wenruo | 6869b0a | 2021-01-26 16:33:45 +0800 | [diff] [blame] | 1006 | PAGE_UNLOCK | PAGE_START_WRITEBACK | |
| 1007 | PAGE_END_WRITEBACK | PAGE_SET_ERROR); |
Filipe Manana | 40ae837 | 2014-10-06 22:14:24 +0100 | [diff] [blame] | 1008 | free_async_extent_pages(async_extent); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1009 | kfree(async_extent); |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 1010 | goto again; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1011 | } |
| 1012 | |
Nikolay Borisov | 43c6984 | 2020-06-03 08:55:02 +0300 | [diff] [blame] | 1013 | static u64 get_extent_allocation_hint(struct btrfs_inode *inode, u64 start, |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 1014 | u64 num_bytes) |
| 1015 | { |
Nikolay Borisov | 43c6984 | 2020-06-03 08:55:02 +0300 | [diff] [blame] | 1016 | struct extent_map_tree *em_tree = &inode->extent_tree; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 1017 | struct extent_map *em; |
| 1018 | u64 alloc_hint = 0; |
| 1019 | |
| 1020 | read_lock(&em_tree->lock); |
| 1021 | em = search_extent_mapping(em_tree, start, num_bytes); |
| 1022 | if (em) { |
| 1023 | /* |
| 1024 | * if block start isn't an actual block number then find the |
| 1025 | * first block in this inode and use that as a hint. If that |
| 1026 | * block is also bogus then just don't worry about it. |
| 1027 | */ |
| 1028 | if (em->block_start >= EXTENT_MAP_LAST_BYTE) { |
| 1029 | free_extent_map(em); |
| 1030 | em = search_extent_mapping(em_tree, 0, 0); |
| 1031 | if (em && em->block_start < EXTENT_MAP_LAST_BYTE) |
| 1032 | alloc_hint = em->block_start; |
| 1033 | if (em) |
| 1034 | free_extent_map(em); |
| 1035 | } else { |
| 1036 | alloc_hint = em->block_start; |
| 1037 | free_extent_map(em); |
| 1038 | } |
| 1039 | } |
| 1040 | read_unlock(&em_tree->lock); |
| 1041 | |
| 1042 | return alloc_hint; |
| 1043 | } |
| 1044 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1045 | /* |
| 1046 | * when extent_io.c finds a delayed allocation range in the file, |
| 1047 | * the call backs end up in this code. The basic idea is to |
| 1048 | * allocate extents on disk for the range, and create ordered data structs |
| 1049 | * in ram to track those extents. |
| 1050 | * |
| 1051 | * locked_page is the page that writepage had locked already. We use |
| 1052 | * it to make sure we don't do extra locks or unlocks. |
| 1053 | * |
| 1054 | * *page_started is set to one if we unlock locked_page and do everything |
| 1055 | * required to start IO on it. It may be clean and already done with |
| 1056 | * IO when we return. |
| 1057 | */ |
Nikolay Borisov | 6e26c44 | 2020-06-03 08:55:14 +0300 | [diff] [blame] | 1058 | static noinline int cow_file_range(struct btrfs_inode *inode, |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 1059 | struct page *locked_page, |
Nikolay Borisov | 74e9194 | 2019-07-17 16:18:16 +0300 | [diff] [blame] | 1060 | u64 start, u64 end, int *page_started, |
Nikolay Borisov | 330a582 | 2019-07-17 16:18:17 +0300 | [diff] [blame] | 1061 | unsigned long *nr_written, int unlock) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1062 | { |
Nikolay Borisov | 6e26c44 | 2020-06-03 08:55:14 +0300 | [diff] [blame] | 1063 | struct btrfs_root *root = inode->root; |
| 1064 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1065 | u64 alloc_hint = 0; |
| 1066 | u64 num_bytes; |
| 1067 | unsigned long ram_size; |
Filipe Manana | a315e68 | 2017-03-06 23:04:20 +0000 | [diff] [blame] | 1068 | u64 cur_alloc_size = 0; |
Filipe Manana | 432cd2a | 2020-06-08 13:32:55 +0100 | [diff] [blame] | 1069 | u64 min_alloc_size; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1070 | u64 blocksize = fs_info->sectorsize; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1071 | struct btrfs_key ins; |
| 1072 | struct extent_map *em; |
Filipe Manana | a315e68 | 2017-03-06 23:04:20 +0000 | [diff] [blame] | 1073 | unsigned clear_bits; |
| 1074 | unsigned long page_ops; |
| 1075 | bool extent_reserved = false; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1076 | int ret = 0; |
| 1077 | |
Nikolay Borisov | 6e26c44 | 2020-06-03 08:55:14 +0300 | [diff] [blame] | 1078 | if (btrfs_is_free_space_inode(inode)) { |
Josef Bacik | 02ecd2c | 2013-10-25 16:19:08 -0400 | [diff] [blame] | 1079 | WARN_ON_ONCE(1); |
Josef Bacik | 29bce2f | 2014-02-07 12:21:23 -0500 | [diff] [blame] | 1080 | ret = -EINVAL; |
| 1081 | goto out_unlock; |
Josef Bacik | 02ecd2c | 2013-10-25 16:19:08 -0400 | [diff] [blame] | 1082 | } |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1083 | |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 1084 | num_bytes = ALIGN(end - start + 1, blocksize); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1085 | num_bytes = max(blocksize, num_bytes); |
Anand Jain | 566b176 | 2018-02-15 18:07:59 +0800 | [diff] [blame] | 1086 | ASSERT(num_bytes <= btrfs_super_total_bytes(fs_info->super_copy)); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1087 | |
Nikolay Borisov | 6e26c44 | 2020-06-03 08:55:14 +0300 | [diff] [blame] | 1088 | inode_should_defrag(inode, start, end, num_bytes, SZ_64K); |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1089 | |
Qu Wenruo | 7367253 | 2021-07-26 14:34:59 +0800 | [diff] [blame] | 1090 | /* |
| 1091 | * Due to the page size limit, for subpage we can only trigger the |
| 1092 | * writeback for the dirty sectors of page, that means data writeback |
| 1093 | * is doing more writeback than what we want. |
| 1094 | * |
| 1095 | * This is especially unexpected for some call sites like fallocate, |
| 1096 | * where we only increase i_size after everything is done. |
| 1097 | * This means we can trigger inline extent even if we didn't want to. |
| 1098 | * So here we skip inline extent creation completely. |
| 1099 | */ |
| 1100 | if (start == 0 && fs_info->sectorsize == PAGE_SIZE) { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1101 | /* lets try to make an inline extent */ |
Nikolay Borisov | 6e26c44 | 2020-06-03 08:55:14 +0300 | [diff] [blame] | 1102 | ret = cow_file_range_inline(inode, start, end, 0, |
Nikolay Borisov | d02c0e2 | 2018-03-02 09:43:15 +0200 | [diff] [blame] | 1103 | BTRFS_COMPRESS_NONE, NULL); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1104 | if (ret == 0) { |
Josef Bacik | 8b62f87 | 2017-10-19 14:15:55 -0400 | [diff] [blame] | 1105 | /* |
| 1106 | * We use DO_ACCOUNTING here because we need the |
| 1107 | * delalloc_release_metadata to be run _after_ we drop |
| 1108 | * our outstanding extent for clearing delalloc for this |
| 1109 | * range. |
| 1110 | */ |
Qu Wenruo | 4750af3 | 2021-05-31 16:50:48 +0800 | [diff] [blame] | 1111 | extent_clear_unlock_delalloc(inode, start, end, |
| 1112 | locked_page, |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1113 | EXTENT_LOCKED | EXTENT_DELALLOC | |
Josef Bacik | 8b62f87 | 2017-10-19 14:15:55 -0400 | [diff] [blame] | 1114 | EXTENT_DELALLOC_NEW | EXTENT_DEFRAG | |
| 1115 | EXTENT_DO_ACCOUNTING, PAGE_UNLOCK | |
Qu Wenruo | 6869b0a | 2021-01-26 16:33:45 +0800 | [diff] [blame] | 1116 | PAGE_START_WRITEBACK | PAGE_END_WRITEBACK); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1117 | *nr_written = *nr_written + |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1118 | (end - start + PAGE_SIZE) / PAGE_SIZE; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1119 | *page_started = 1; |
Qu Wenruo | 4750af3 | 2021-05-31 16:50:48 +0800 | [diff] [blame] | 1120 | /* |
| 1121 | * locked_page is locked by the caller of |
| 1122 | * writepage_delalloc(), not locked by |
| 1123 | * __process_pages_contig(). |
| 1124 | * |
| 1125 | * We can't let __process_pages_contig() to unlock it, |
| 1126 | * as it doesn't have any subpage::writers recorded. |
| 1127 | * |
| 1128 | * Here we manually unlock the page, since the caller |
| 1129 | * can't use page_started to determine if it's an |
| 1130 | * inline extent or a compressed extent. |
| 1131 | */ |
| 1132 | unlock_page(locked_page); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1133 | goto out; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1134 | } else if (ret < 0) { |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1135 | goto out_unlock; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1136 | } |
| 1137 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1138 | |
Nikolay Borisov | 6e26c44 | 2020-06-03 08:55:14 +0300 | [diff] [blame] | 1139 | alloc_hint = get_extent_allocation_hint(inode, start, num_bytes); |
| 1140 | btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 1141 | |
Filipe Manana | 432cd2a | 2020-06-08 13:32:55 +0100 | [diff] [blame] | 1142 | /* |
| 1143 | * Relocation relies on the relocated extents to have exactly the same |
| 1144 | * size as the original extents. Normally writeback for relocation data |
| 1145 | * extents follows a NOCOW path because relocation preallocates the |
| 1146 | * extents. However, due to an operation such as scrub turning a block |
| 1147 | * group to RO mode, it may fallback to COW mode, so we must make sure |
| 1148 | * an extent allocated during COW has exactly the requested size and can |
| 1149 | * not be split into smaller extents, otherwise relocation breaks and |
| 1150 | * fails during the stage where it updates the bytenr of file extent |
| 1151 | * items. |
| 1152 | */ |
| 1153 | if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID) |
| 1154 | min_alloc_size = num_bytes; |
| 1155 | else |
| 1156 | min_alloc_size = fs_info->sectorsize; |
| 1157 | |
Anand Jain | 3752d22 | 2018-02-15 12:29:38 +0800 | [diff] [blame] | 1158 | while (num_bytes > 0) { |
| 1159 | cur_alloc_size = num_bytes; |
Wang Xiaoguang | 1851309 | 2016-07-25 15:51:40 +0800 | [diff] [blame] | 1160 | ret = btrfs_reserve_extent(root, cur_alloc_size, cur_alloc_size, |
Filipe Manana | 432cd2a | 2020-06-08 13:32:55 +0100 | [diff] [blame] | 1161 | min_alloc_size, 0, alloc_hint, |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 1162 | &ins, 1, 1); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 1163 | if (ret < 0) |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1164 | goto out_unlock; |
Filipe Manana | a315e68 | 2017-03-06 23:04:20 +0000 | [diff] [blame] | 1165 | cur_alloc_size = ins.offset; |
| 1166 | extent_reserved = true; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1167 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1168 | ram_size = ins.offset; |
Nikolay Borisov | 6e26c44 | 2020-06-03 08:55:14 +0300 | [diff] [blame] | 1169 | em = create_io_em(inode, start, ins.offset, /* len */ |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 1170 | start, /* orig_start */ |
| 1171 | ins.objectid, /* block_start */ |
| 1172 | ins.offset, /* block_len */ |
| 1173 | ins.offset, /* orig_block_len */ |
| 1174 | ram_size, /* ram_bytes */ |
| 1175 | BTRFS_COMPRESS_NONE, /* compress_type */ |
Liu Bo | 1af4a0a | 2017-02-13 15:35:09 -0800 | [diff] [blame] | 1176 | BTRFS_ORDERED_REGULAR /* type */); |
Su Yue | 090a127a | 2018-05-30 16:48:56 +0800 | [diff] [blame] | 1177 | if (IS_ERR(em)) { |
| 1178 | ret = PTR_ERR(em); |
Liu Bo | ace68ba | 2013-04-22 10:53:47 +0000 | [diff] [blame] | 1179 | goto out_reserve; |
Su Yue | 090a127a | 2018-05-30 16:48:56 +0800 | [diff] [blame] | 1180 | } |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 1181 | free_extent_map(em); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1182 | |
Nikolay Borisov | 6e26c44 | 2020-06-03 08:55:14 +0300 | [diff] [blame] | 1183 | ret = btrfs_add_ordered_extent(inode, start, ins.objectid, |
Qu Wenruo | 3c198fe | 2021-01-21 14:13:54 +0800 | [diff] [blame] | 1184 | ram_size, cur_alloc_size, |
| 1185 | BTRFS_ORDERED_REGULAR); |
Liu Bo | ace68ba | 2013-04-22 10:53:47 +0000 | [diff] [blame] | 1186 | if (ret) |
Filipe Manana | d9f8596 | 2014-08-25 10:43:00 +0100 | [diff] [blame] | 1187 | goto out_drop_extent_cache; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1188 | |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1189 | if (root->root_key.objectid == |
| 1190 | BTRFS_DATA_RELOC_TREE_OBJECTID) { |
Nikolay Borisov | 6e26c44 | 2020-06-03 08:55:14 +0300 | [diff] [blame] | 1191 | ret = btrfs_reloc_clone_csums(inode, start, |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1192 | cur_alloc_size); |
Qu Wenruo | 4dbd80f | 2017-03-08 10:25:51 +0800 | [diff] [blame] | 1193 | /* |
| 1194 | * Only drop cache here, and process as normal. |
| 1195 | * |
| 1196 | * We must not allow extent_clear_unlock_delalloc() |
| 1197 | * at out_unlock label to free meta of this ordered |
| 1198 | * extent, as its meta should be freed by |
| 1199 | * btrfs_finish_ordered_io(). |
| 1200 | * |
| 1201 | * So we must continue until @start is increased to |
| 1202 | * skip current ordered extent. |
| 1203 | */ |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 1204 | if (ret) |
Nikolay Borisov | 6e26c44 | 2020-06-03 08:55:14 +0300 | [diff] [blame] | 1205 | btrfs_drop_extent_cache(inode, start, |
Qu Wenruo | 4dbd80f | 2017-03-08 10:25:51 +0800 | [diff] [blame] | 1206 | start + ram_size - 1, 0); |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1207 | } |
| 1208 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1209 | btrfs_dec_block_group_reservations(fs_info, ins.objectid); |
Filipe Manana | 9cfa3e3 | 2016-04-26 15:39:32 +0100 | [diff] [blame] | 1210 | |
Qu Wenruo | f57ad93 | 2021-04-07 19:22:13 +0800 | [diff] [blame] | 1211 | /* |
| 1212 | * We're not doing compressed IO, don't unlock the first page |
| 1213 | * (which the caller expects to stay locked), don't clear any |
| 1214 | * dirty bits and don't set any writeback bits |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 1215 | * |
Qu Wenruo | f57ad93 | 2021-04-07 19:22:13 +0800 | [diff] [blame] | 1216 | * Do set the Ordered (Private2) bit so we know this page was |
| 1217 | * properly setup for writepage. |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1218 | */ |
Filipe Manana | a315e68 | 2017-03-06 23:04:20 +0000 | [diff] [blame] | 1219 | page_ops = unlock ? PAGE_UNLOCK : 0; |
Qu Wenruo | f57ad93 | 2021-04-07 19:22:13 +0800 | [diff] [blame] | 1220 | page_ops |= PAGE_SET_ORDERED; |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 1221 | |
Nikolay Borisov | 6e26c44 | 2020-06-03 08:55:14 +0300 | [diff] [blame] | 1222 | extent_clear_unlock_delalloc(inode, start, start + ram_size - 1, |
Nikolay Borisov | 74e9194 | 2019-07-17 16:18:16 +0300 | [diff] [blame] | 1223 | locked_page, |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1224 | EXTENT_LOCKED | EXTENT_DELALLOC, |
Filipe Manana | a315e68 | 2017-03-06 23:04:20 +0000 | [diff] [blame] | 1225 | page_ops); |
Anand Jain | 3752d22 | 2018-02-15 12:29:38 +0800 | [diff] [blame] | 1226 | if (num_bytes < cur_alloc_size) |
| 1227 | num_bytes = 0; |
Qu Wenruo | 4dbd80f | 2017-03-08 10:25:51 +0800 | [diff] [blame] | 1228 | else |
Anand Jain | 3752d22 | 2018-02-15 12:29:38 +0800 | [diff] [blame] | 1229 | num_bytes -= cur_alloc_size; |
Chris Mason | c59f895 | 2007-12-17 20:14:04 -0500 | [diff] [blame] | 1230 | alloc_hint = ins.objectid + ins.offset; |
| 1231 | start += cur_alloc_size; |
Filipe Manana | a315e68 | 2017-03-06 23:04:20 +0000 | [diff] [blame] | 1232 | extent_reserved = false; |
Qu Wenruo | 4dbd80f | 2017-03-08 10:25:51 +0800 | [diff] [blame] | 1233 | |
| 1234 | /* |
| 1235 | * btrfs_reloc_clone_csums() error, since start is increased |
| 1236 | * extent_clear_unlock_delalloc() at out_unlock label won't |
| 1237 | * free metadata of current ordered extent, we're OK to exit. |
| 1238 | */ |
| 1239 | if (ret) |
| 1240 | goto out_unlock; |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 1241 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1242 | out: |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1243 | return ret; |
Miao Xie | b7d5b0a | 2012-11-01 07:32:18 +0000 | [diff] [blame] | 1244 | |
Filipe Manana | d9f8596 | 2014-08-25 10:43:00 +0100 | [diff] [blame] | 1245 | out_drop_extent_cache: |
Nikolay Borisov | 6e26c44 | 2020-06-03 08:55:14 +0300 | [diff] [blame] | 1246 | btrfs_drop_extent_cache(inode, start, start + ram_size - 1, 0); |
Liu Bo | ace68ba | 2013-04-22 10:53:47 +0000 | [diff] [blame] | 1247 | out_reserve: |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1248 | btrfs_dec_block_group_reservations(fs_info, ins.objectid); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 1249 | btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1250 | out_unlock: |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 1251 | clear_bits = EXTENT_LOCKED | EXTENT_DELALLOC | EXTENT_DELALLOC_NEW | |
| 1252 | EXTENT_DEFRAG | EXTENT_CLEAR_META_RESV; |
Qu Wenruo | 6869b0a | 2021-01-26 16:33:45 +0800 | [diff] [blame] | 1253 | page_ops = PAGE_UNLOCK | PAGE_START_WRITEBACK | PAGE_END_WRITEBACK; |
Filipe Manana | a315e68 | 2017-03-06 23:04:20 +0000 | [diff] [blame] | 1254 | /* |
| 1255 | * If we reserved an extent for our delalloc range (or a subrange) and |
| 1256 | * failed to create the respective ordered extent, then it means that |
| 1257 | * when we reserved the extent we decremented the extent's size from |
| 1258 | * the data space_info's bytes_may_use counter and incremented the |
| 1259 | * space_info's bytes_reserved counter by the same amount. We must make |
| 1260 | * sure extent_clear_unlock_delalloc() does not try to decrement again |
| 1261 | * the data space_info's bytes_may_use counter, therefore we do not pass |
| 1262 | * it the flag EXTENT_CLEAR_DATA_RESV. |
| 1263 | */ |
| 1264 | if (extent_reserved) { |
Nikolay Borisov | 6e26c44 | 2020-06-03 08:55:14 +0300 | [diff] [blame] | 1265 | extent_clear_unlock_delalloc(inode, start, |
Filipe Manana | e2c8e92 | 2020-05-27 11:15:53 +0100 | [diff] [blame] | 1266 | start + cur_alloc_size - 1, |
Filipe Manana | a315e68 | 2017-03-06 23:04:20 +0000 | [diff] [blame] | 1267 | locked_page, |
| 1268 | clear_bits, |
| 1269 | page_ops); |
| 1270 | start += cur_alloc_size; |
| 1271 | if (start >= end) |
| 1272 | goto out; |
| 1273 | } |
Nikolay Borisov | 6e26c44 | 2020-06-03 08:55:14 +0300 | [diff] [blame] | 1274 | extent_clear_unlock_delalloc(inode, start, end, locked_page, |
Filipe Manana | a315e68 | 2017-03-06 23:04:20 +0000 | [diff] [blame] | 1275 | clear_bits | EXTENT_CLEAR_DATA_RESV, |
| 1276 | page_ops); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1277 | goto out; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1278 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1279 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1280 | /* |
| 1281 | * work queue call back to started compression on a file and pages |
| 1282 | */ |
| 1283 | static noinline void async_cow_start(struct btrfs_work *work) |
| 1284 | { |
Nikolay Borisov | b532627 | 2019-03-12 17:20:25 +0200 | [diff] [blame] | 1285 | struct async_chunk *async_chunk; |
Nikolay Borisov | ac3e993 | 2019-07-17 14:41:44 +0300 | [diff] [blame] | 1286 | int compressed_extents; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1287 | |
Nikolay Borisov | b532627 | 2019-03-12 17:20:25 +0200 | [diff] [blame] | 1288 | async_chunk = container_of(work, struct async_chunk, work); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1289 | |
Nikolay Borisov | ac3e993 | 2019-07-17 14:41:44 +0300 | [diff] [blame] | 1290 | compressed_extents = compress_file_range(async_chunk); |
| 1291 | if (compressed_extents == 0) { |
Nikolay Borisov | b532627 | 2019-03-12 17:20:25 +0200 | [diff] [blame] | 1292 | btrfs_add_delayed_iput(async_chunk->inode); |
| 1293 | async_chunk->inode = NULL; |
Josef Bacik | 8180ef8 | 2012-06-08 15:16:12 -0400 | [diff] [blame] | 1294 | } |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1295 | } |
| 1296 | |
| 1297 | /* |
| 1298 | * work queue call back to submit previously compressed pages |
| 1299 | */ |
| 1300 | static noinline void async_cow_submit(struct btrfs_work *work) |
| 1301 | { |
Nikolay Borisov | c5a68ae | 2019-03-12 17:20:26 +0200 | [diff] [blame] | 1302 | struct async_chunk *async_chunk = container_of(work, struct async_chunk, |
| 1303 | work); |
| 1304 | struct btrfs_fs_info *fs_info = btrfs_work_owner(work); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1305 | unsigned long nr_pages; |
| 1306 | |
Nikolay Borisov | b532627 | 2019-03-12 17:20:25 +0200 | [diff] [blame] | 1307 | nr_pages = (async_chunk->end - async_chunk->start + PAGE_SIZE) >> |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1308 | PAGE_SHIFT; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1309 | |
Nikolay Borisov | 4546d17 | 2019-01-03 10:50:03 +0200 | [diff] [blame] | 1310 | /* |
Nikolay Borisov | b532627 | 2019-03-12 17:20:25 +0200 | [diff] [blame] | 1311 | * ->inode could be NULL if async_chunk_start has failed to compress, |
Nikolay Borisov | 4546d17 | 2019-01-03 10:50:03 +0200 | [diff] [blame] | 1312 | * in which case we don't have anything to submit, yet we need to |
| 1313 | * always adjust ->async_delalloc_pages as its paired with the init |
| 1314 | * happening in cow_file_range_async |
| 1315 | */ |
Nikolay Borisov | b532627 | 2019-03-12 17:20:25 +0200 | [diff] [blame] | 1316 | if (async_chunk->inode) |
| 1317 | submit_compressed_extents(async_chunk); |
Josef Bacik | ac98141 | 2021-07-14 14:47:17 -0400 | [diff] [blame] | 1318 | |
| 1319 | /* atomic_sub_return implies a barrier */ |
| 1320 | if (atomic_sub_return(nr_pages, &fs_info->async_delalloc_pages) < |
| 1321 | 5 * SZ_1M) |
| 1322 | cond_wake_up_nomb(&fs_info->async_submit_wait); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1323 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1324 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1325 | static noinline void async_cow_free(struct btrfs_work *work) |
| 1326 | { |
Nikolay Borisov | b532627 | 2019-03-12 17:20:25 +0200 | [diff] [blame] | 1327 | struct async_chunk *async_chunk; |
Nikolay Borisov | 97db120 | 2019-03-12 17:20:24 +0200 | [diff] [blame] | 1328 | |
Nikolay Borisov | b532627 | 2019-03-12 17:20:25 +0200 | [diff] [blame] | 1329 | async_chunk = container_of(work, struct async_chunk, work); |
| 1330 | if (async_chunk->inode) |
| 1331 | btrfs_add_delayed_iput(async_chunk->inode); |
Chris Mason | ec39f76 | 2019-07-10 12:28:17 -0700 | [diff] [blame] | 1332 | if (async_chunk->blkcg_css) |
| 1333 | css_put(async_chunk->blkcg_css); |
Nikolay Borisov | 97db120 | 2019-03-12 17:20:24 +0200 | [diff] [blame] | 1334 | /* |
| 1335 | * Since the pointer to 'pending' is at the beginning of the array of |
Nikolay Borisov | b532627 | 2019-03-12 17:20:25 +0200 | [diff] [blame] | 1336 | * async_chunk's, freeing it ensures the whole array has been freed. |
Nikolay Borisov | 97db120 | 2019-03-12 17:20:24 +0200 | [diff] [blame] | 1337 | */ |
Nikolay Borisov | b532627 | 2019-03-12 17:20:25 +0200 | [diff] [blame] | 1338 | if (atomic_dec_and_test(async_chunk->pending)) |
Nikolay Borisov | b1c16ac | 2019-04-01 11:29:57 +0300 | [diff] [blame] | 1339 | kvfree(async_chunk->pending); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1340 | } |
| 1341 | |
Nikolay Borisov | 751b643 | 2020-06-03 08:55:22 +0300 | [diff] [blame] | 1342 | static int cow_file_range_async(struct btrfs_inode *inode, |
Chris Mason | ec39f76 | 2019-07-10 12:28:17 -0700 | [diff] [blame] | 1343 | struct writeback_control *wbc, |
| 1344 | struct page *locked_page, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1345 | u64 start, u64 end, int *page_started, |
David Sterba | fac07d2 | 2019-10-29 18:28:57 +0100 | [diff] [blame] | 1346 | unsigned long *nr_written) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1347 | { |
Nikolay Borisov | 751b643 | 2020-06-03 08:55:22 +0300 | [diff] [blame] | 1348 | struct btrfs_fs_info *fs_info = inode->root->fs_info; |
Chris Mason | ec39f76 | 2019-07-10 12:28:17 -0700 | [diff] [blame] | 1349 | struct cgroup_subsys_state *blkcg_css = wbc_blkcg_css(wbc); |
Nikolay Borisov | 97db120 | 2019-03-12 17:20:24 +0200 | [diff] [blame] | 1350 | struct async_cow *ctx; |
| 1351 | struct async_chunk *async_chunk; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1352 | unsigned long nr_pages; |
| 1353 | u64 cur_end; |
Nikolay Borisov | 97db120 | 2019-03-12 17:20:24 +0200 | [diff] [blame] | 1354 | u64 num_chunks = DIV_ROUND_UP(end - start, SZ_512K); |
| 1355 | int i; |
| 1356 | bool should_compress; |
Nikolay Borisov | b1c16ac | 2019-04-01 11:29:57 +0300 | [diff] [blame] | 1357 | unsigned nofs_flag; |
David Sterba | fac07d2 | 2019-10-29 18:28:57 +0100 | [diff] [blame] | 1358 | const unsigned int write_flags = wbc_to_write_flags(wbc); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1359 | |
Nikolay Borisov | 751b643 | 2020-06-03 08:55:22 +0300 | [diff] [blame] | 1360 | unlock_extent(&inode->io_tree, start, end); |
Nikolay Borisov | 97db120 | 2019-03-12 17:20:24 +0200 | [diff] [blame] | 1361 | |
Nikolay Borisov | 751b643 | 2020-06-03 08:55:22 +0300 | [diff] [blame] | 1362 | if (inode->flags & BTRFS_INODE_NOCOMPRESS && |
Nikolay Borisov | 97db120 | 2019-03-12 17:20:24 +0200 | [diff] [blame] | 1363 | !btrfs_test_opt(fs_info, FORCE_COMPRESS)) { |
| 1364 | num_chunks = 1; |
| 1365 | should_compress = false; |
| 1366 | } else { |
| 1367 | should_compress = true; |
| 1368 | } |
| 1369 | |
Nikolay Borisov | b1c16ac | 2019-04-01 11:29:57 +0300 | [diff] [blame] | 1370 | nofs_flag = memalloc_nofs_save(); |
| 1371 | ctx = kvmalloc(struct_size(ctx, chunks, num_chunks), GFP_KERNEL); |
| 1372 | memalloc_nofs_restore(nofs_flag); |
| 1373 | |
Nikolay Borisov | 97db120 | 2019-03-12 17:20:24 +0200 | [diff] [blame] | 1374 | if (!ctx) { |
| 1375 | unsigned clear_bits = EXTENT_LOCKED | EXTENT_DELALLOC | |
| 1376 | EXTENT_DELALLOC_NEW | EXTENT_DEFRAG | |
| 1377 | EXTENT_DO_ACCOUNTING; |
Qu Wenruo | 6869b0a | 2021-01-26 16:33:45 +0800 | [diff] [blame] | 1378 | unsigned long page_ops = PAGE_UNLOCK | PAGE_START_WRITEBACK | |
| 1379 | PAGE_END_WRITEBACK | PAGE_SET_ERROR; |
Nikolay Borisov | 97db120 | 2019-03-12 17:20:24 +0200 | [diff] [blame] | 1380 | |
Nikolay Borisov | 751b643 | 2020-06-03 08:55:22 +0300 | [diff] [blame] | 1381 | extent_clear_unlock_delalloc(inode, start, end, locked_page, |
| 1382 | clear_bits, page_ops); |
Nikolay Borisov | 97db120 | 2019-03-12 17:20:24 +0200 | [diff] [blame] | 1383 | return -ENOMEM; |
| 1384 | } |
| 1385 | |
| 1386 | async_chunk = ctx->chunks; |
| 1387 | atomic_set(&ctx->num_chunks, num_chunks); |
| 1388 | |
| 1389 | for (i = 0; i < num_chunks; i++) { |
| 1390 | if (should_compress) |
| 1391 | cur_end = min(end, start + SZ_512K - 1); |
| 1392 | else |
| 1393 | cur_end = end; |
| 1394 | |
Nikolay Borisov | bd4691a | 2019-01-03 10:50:01 +0200 | [diff] [blame] | 1395 | /* |
| 1396 | * igrab is called higher up in the call chain, take only the |
| 1397 | * lightweight reference for the callback lifetime |
| 1398 | */ |
Nikolay Borisov | 751b643 | 2020-06-03 08:55:22 +0300 | [diff] [blame] | 1399 | ihold(&inode->vfs_inode); |
Nikolay Borisov | 97db120 | 2019-03-12 17:20:24 +0200 | [diff] [blame] | 1400 | async_chunk[i].pending = &ctx->num_chunks; |
Nikolay Borisov | 751b643 | 2020-06-03 08:55:22 +0300 | [diff] [blame] | 1401 | async_chunk[i].inode = &inode->vfs_inode; |
Nikolay Borisov | 97db120 | 2019-03-12 17:20:24 +0200 | [diff] [blame] | 1402 | async_chunk[i].start = start; |
| 1403 | async_chunk[i].end = cur_end; |
Nikolay Borisov | 97db120 | 2019-03-12 17:20:24 +0200 | [diff] [blame] | 1404 | async_chunk[i].write_flags = write_flags; |
| 1405 | INIT_LIST_HEAD(&async_chunk[i].extents); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1406 | |
Chris Mason | 1d53c9e | 2019-07-10 12:28:16 -0700 | [diff] [blame] | 1407 | /* |
| 1408 | * The locked_page comes all the way from writepage and its |
| 1409 | * the original page we were actually given. As we spread |
| 1410 | * this large delalloc region across multiple async_chunk |
| 1411 | * structs, only the first struct needs a pointer to locked_page |
| 1412 | * |
| 1413 | * This way we don't need racey decisions about who is supposed |
| 1414 | * to unlock it. |
| 1415 | */ |
| 1416 | if (locked_page) { |
Chris Mason | ec39f76 | 2019-07-10 12:28:17 -0700 | [diff] [blame] | 1417 | /* |
| 1418 | * Depending on the compressibility, the pages might or |
| 1419 | * might not go through async. We want all of them to |
| 1420 | * be accounted against wbc once. Let's do it here |
| 1421 | * before the paths diverge. wbc accounting is used |
| 1422 | * only for foreign writeback detection and doesn't |
| 1423 | * need full accuracy. Just account the whole thing |
| 1424 | * against the first page. |
| 1425 | */ |
| 1426 | wbc_account_cgroup_owner(wbc, locked_page, |
| 1427 | cur_end - start); |
Chris Mason | 1d53c9e | 2019-07-10 12:28:16 -0700 | [diff] [blame] | 1428 | async_chunk[i].locked_page = locked_page; |
| 1429 | locked_page = NULL; |
| 1430 | } else { |
| 1431 | async_chunk[i].locked_page = NULL; |
| 1432 | } |
| 1433 | |
Chris Mason | ec39f76 | 2019-07-10 12:28:17 -0700 | [diff] [blame] | 1434 | if (blkcg_css != blkcg_root_css) { |
| 1435 | css_get(blkcg_css); |
| 1436 | async_chunk[i].blkcg_css = blkcg_css; |
| 1437 | } else { |
| 1438 | async_chunk[i].blkcg_css = NULL; |
| 1439 | } |
| 1440 | |
Omar Sandoval | a0cac0e | 2019-09-16 11:30:57 -0700 | [diff] [blame] | 1441 | btrfs_init_work(&async_chunk[i].work, async_cow_start, |
| 1442 | async_cow_submit, async_cow_free); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1443 | |
Nikolay Borisov | 97db120 | 2019-03-12 17:20:24 +0200 | [diff] [blame] | 1444 | nr_pages = DIV_ROUND_UP(cur_end - start, PAGE_SIZE); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1445 | atomic_add(nr_pages, &fs_info->async_delalloc_pages); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1446 | |
Nikolay Borisov | 97db120 | 2019-03-12 17:20:24 +0200 | [diff] [blame] | 1447 | btrfs_queue_work(fs_info->delalloc_workers, &async_chunk[i].work); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1448 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1449 | *nr_written += nr_pages; |
| 1450 | start = cur_end + 1; |
| 1451 | } |
| 1452 | *page_started = 1; |
| 1453 | return 0; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1454 | } |
| 1455 | |
Naohiro Aota | 42c0110 | 2021-02-04 19:22:07 +0900 | [diff] [blame] | 1456 | static noinline int run_delalloc_zoned(struct btrfs_inode *inode, |
| 1457 | struct page *locked_page, u64 start, |
| 1458 | u64 end, int *page_started, |
| 1459 | unsigned long *nr_written) |
| 1460 | { |
| 1461 | int ret; |
| 1462 | |
| 1463 | ret = cow_file_range(inode, locked_page, start, end, page_started, |
| 1464 | nr_written, 0); |
| 1465 | if (ret) |
| 1466 | return ret; |
| 1467 | |
| 1468 | if (*page_started) |
| 1469 | return 0; |
| 1470 | |
| 1471 | __set_page_dirty_nobuffers(locked_page); |
| 1472 | account_page_redirty(locked_page); |
| 1473 | extent_write_locked_range(&inode->vfs_inode, start, end, WB_SYNC_ALL); |
| 1474 | *page_started = 1; |
| 1475 | |
| 1476 | return 0; |
| 1477 | } |
| 1478 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 1479 | static noinline int csum_exist_in_range(struct btrfs_fs_info *fs_info, |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1480 | u64 bytenr, u64 num_bytes) |
| 1481 | { |
| 1482 | int ret; |
| 1483 | struct btrfs_ordered_sum *sums; |
| 1484 | LIST_HEAD(list); |
| 1485 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1486 | ret = btrfs_lookup_csums_range(fs_info->csum_root, bytenr, |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 1487 | bytenr + num_bytes - 1, &list, 0); |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1488 | if (ret == 0 && list_empty(&list)) |
| 1489 | return 0; |
| 1490 | |
| 1491 | while (!list_empty(&list)) { |
| 1492 | sums = list_entry(list.next, struct btrfs_ordered_sum, list); |
| 1493 | list_del(&sums->list); |
| 1494 | kfree(sums); |
| 1495 | } |
Liu Bo | 5811375 | 2018-01-31 17:09:13 -0700 | [diff] [blame] | 1496 | if (ret < 0) |
| 1497 | return ret; |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1498 | return 1; |
| 1499 | } |
| 1500 | |
Nikolay Borisov | 8ba96f3 | 2020-06-03 08:55:20 +0300 | [diff] [blame] | 1501 | static int fallback_to_cow(struct btrfs_inode *inode, struct page *locked_page, |
Filipe Manana | 467dc47 | 2020-05-27 11:16:07 +0100 | [diff] [blame] | 1502 | const u64 start, const u64 end, |
| 1503 | int *page_started, unsigned long *nr_written) |
| 1504 | { |
Nikolay Borisov | 8ba96f3 | 2020-06-03 08:55:20 +0300 | [diff] [blame] | 1505 | const bool is_space_ino = btrfs_is_free_space_inode(inode); |
| 1506 | const bool is_reloc_ino = (inode->root->root_key.objectid == |
Filipe Manana | 6bd335b | 2020-06-08 13:33:05 +0100 | [diff] [blame] | 1507 | BTRFS_DATA_RELOC_TREE_OBJECTID); |
Filipe Manana | 2166e5e | 2020-05-27 11:16:19 +0100 | [diff] [blame] | 1508 | const u64 range_bytes = end + 1 - start; |
Nikolay Borisov | 8ba96f3 | 2020-06-03 08:55:20 +0300 | [diff] [blame] | 1509 | struct extent_io_tree *io_tree = &inode->io_tree; |
Filipe Manana | 467dc47 | 2020-05-27 11:16:07 +0100 | [diff] [blame] | 1510 | u64 range_start = start; |
| 1511 | u64 count; |
| 1512 | |
| 1513 | /* |
| 1514 | * If EXTENT_NORESERVE is set it means that when the buffered write was |
| 1515 | * made we had not enough available data space and therefore we did not |
| 1516 | * reserve data space for it, since we though we could do NOCOW for the |
| 1517 | * respective file range (either there is prealloc extent or the inode |
| 1518 | * has the NOCOW bit set). |
| 1519 | * |
| 1520 | * However when we need to fallback to COW mode (because for example the |
| 1521 | * block group for the corresponding extent was turned to RO mode by a |
| 1522 | * scrub or relocation) we need to do the following: |
| 1523 | * |
| 1524 | * 1) We increment the bytes_may_use counter of the data space info. |
| 1525 | * If COW succeeds, it allocates a new data extent and after doing |
| 1526 | * that it decrements the space info's bytes_may_use counter and |
| 1527 | * increments its bytes_reserved counter by the same amount (we do |
| 1528 | * this at btrfs_add_reserved_bytes()). So we need to increment the |
| 1529 | * bytes_may_use counter to compensate (when space is reserved at |
| 1530 | * buffered write time, the bytes_may_use counter is incremented); |
| 1531 | * |
| 1532 | * 2) We clear the EXTENT_NORESERVE bit from the range. We do this so |
| 1533 | * that if the COW path fails for any reason, it decrements (through |
| 1534 | * extent_clear_unlock_delalloc()) the bytes_may_use counter of the |
| 1535 | * data space info, which we incremented in the step above. |
Filipe Manana | 2166e5e | 2020-05-27 11:16:19 +0100 | [diff] [blame] | 1536 | * |
| 1537 | * If we need to fallback to cow and the inode corresponds to a free |
Filipe Manana | 6bd335b | 2020-06-08 13:33:05 +0100 | [diff] [blame] | 1538 | * space cache inode or an inode of the data relocation tree, we must |
| 1539 | * also increment bytes_may_use of the data space_info for the same |
| 1540 | * reason. Space caches and relocated data extents always get a prealloc |
Filipe Manana | 2166e5e | 2020-05-27 11:16:19 +0100 | [diff] [blame] | 1541 | * extent for them, however scrub or balance may have set the block |
Filipe Manana | 6bd335b | 2020-06-08 13:33:05 +0100 | [diff] [blame] | 1542 | * group that contains that extent to RO mode and therefore force COW |
| 1543 | * when starting writeback. |
Filipe Manana | 467dc47 | 2020-05-27 11:16:07 +0100 | [diff] [blame] | 1544 | */ |
Filipe Manana | 2166e5e | 2020-05-27 11:16:19 +0100 | [diff] [blame] | 1545 | count = count_range_bits(io_tree, &range_start, end, range_bytes, |
Filipe Manana | 467dc47 | 2020-05-27 11:16:07 +0100 | [diff] [blame] | 1546 | EXTENT_NORESERVE, 0); |
Filipe Manana | 6bd335b | 2020-06-08 13:33:05 +0100 | [diff] [blame] | 1547 | if (count > 0 || is_space_ino || is_reloc_ino) { |
| 1548 | u64 bytes = count; |
Nikolay Borisov | 8ba96f3 | 2020-06-03 08:55:20 +0300 | [diff] [blame] | 1549 | struct btrfs_fs_info *fs_info = inode->root->fs_info; |
Filipe Manana | 467dc47 | 2020-05-27 11:16:07 +0100 | [diff] [blame] | 1550 | struct btrfs_space_info *sinfo = fs_info->data_sinfo; |
| 1551 | |
Filipe Manana | 6bd335b | 2020-06-08 13:33:05 +0100 | [diff] [blame] | 1552 | if (is_space_ino || is_reloc_ino) |
| 1553 | bytes = range_bytes; |
| 1554 | |
Filipe Manana | 467dc47 | 2020-05-27 11:16:07 +0100 | [diff] [blame] | 1555 | spin_lock(&sinfo->lock); |
Filipe Manana | 2166e5e | 2020-05-27 11:16:19 +0100 | [diff] [blame] | 1556 | btrfs_space_info_update_bytes_may_use(fs_info, sinfo, bytes); |
Filipe Manana | 467dc47 | 2020-05-27 11:16:07 +0100 | [diff] [blame] | 1557 | spin_unlock(&sinfo->lock); |
| 1558 | |
Filipe Manana | 2166e5e | 2020-05-27 11:16:19 +0100 | [diff] [blame] | 1559 | if (count > 0) |
| 1560 | clear_extent_bit(io_tree, start, end, EXTENT_NORESERVE, |
| 1561 | 0, 0, NULL); |
Filipe Manana | 467dc47 | 2020-05-27 11:16:07 +0100 | [diff] [blame] | 1562 | } |
| 1563 | |
Nikolay Borisov | 8ba96f3 | 2020-06-03 08:55:20 +0300 | [diff] [blame] | 1564 | return cow_file_range(inode, locked_page, start, end, page_started, |
| 1565 | nr_written, 1); |
Filipe Manana | 467dc47 | 2020-05-27 11:16:07 +0100 | [diff] [blame] | 1566 | } |
| 1567 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1568 | /* |
| 1569 | * when nowcow writeback call back. This checks for snapshots or COW copies |
| 1570 | * of the extents that exist in the file, and COWs the file as required. |
| 1571 | * |
| 1572 | * If no cow copies or snapshots exist, we write directly to the existing |
| 1573 | * blocks on disk |
| 1574 | */ |
Nikolay Borisov | 968322c | 2020-06-03 08:55:21 +0300 | [diff] [blame] | 1575 | static noinline int run_delalloc_nocow(struct btrfs_inode *inode, |
Chris Mason | 7f366cf | 2009-03-12 20:12:45 -0400 | [diff] [blame] | 1576 | struct page *locked_page, |
Nikolay Borisov | 3e02484 | 2019-08-21 10:42:03 +0300 | [diff] [blame] | 1577 | const u64 start, const u64 end, |
Goldwyn Rodrigues | 6e65ae7 | 2021-03-04 09:06:25 -0600 | [diff] [blame] | 1578 | int *page_started, |
Nikolay Borisov | 3e02484 | 2019-08-21 10:42:03 +0300 | [diff] [blame] | 1579 | unsigned long *nr_written) |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1580 | { |
Nikolay Borisov | 968322c | 2020-06-03 08:55:21 +0300 | [diff] [blame] | 1581 | struct btrfs_fs_info *fs_info = inode->root->fs_info; |
| 1582 | struct btrfs_root *root = inode->root; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1583 | struct btrfs_path *path; |
Nikolay Borisov | 3e02484 | 2019-08-21 10:42:03 +0300 | [diff] [blame] | 1584 | u64 cow_start = (u64)-1; |
| 1585 | u64 cur_offset = start; |
Robbie Ko | 8ecebf4d | 2018-08-06 10:30:30 +0800 | [diff] [blame] | 1586 | int ret; |
Nikolay Borisov | 3e02484 | 2019-08-21 10:42:03 +0300 | [diff] [blame] | 1587 | bool check_prev = true; |
Nikolay Borisov | 968322c | 2020-06-03 08:55:21 +0300 | [diff] [blame] | 1588 | const bool freespace_inode = btrfs_is_free_space_inode(inode); |
| 1589 | u64 ino = btrfs_ino(inode); |
Nikolay Borisov | 762bf09 | 2019-08-22 17:24:20 +0300 | [diff] [blame] | 1590 | bool nocow = false; |
| 1591 | u64 disk_bytenr = 0; |
Goldwyn Rodrigues | 6e65ae7 | 2021-03-04 09:06:25 -0600 | [diff] [blame] | 1592 | const bool force = inode->flags & BTRFS_INODE_NODATACOW; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1593 | |
| 1594 | path = btrfs_alloc_path(); |
Josef Bacik | 17ca04a | 2012-05-31 15:58:55 -0400 | [diff] [blame] | 1595 | if (!path) { |
Nikolay Borisov | 968322c | 2020-06-03 08:55:21 +0300 | [diff] [blame] | 1596 | extent_clear_unlock_delalloc(inode, start, end, locked_page, |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1597 | EXTENT_LOCKED | EXTENT_DELALLOC | |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 1598 | EXTENT_DO_ACCOUNTING | |
| 1599 | EXTENT_DEFRAG, PAGE_UNLOCK | |
Qu Wenruo | 6869b0a | 2021-01-26 16:33:45 +0800 | [diff] [blame] | 1600 | PAGE_START_WRITEBACK | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1601 | PAGE_END_WRITEBACK); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 1602 | return -ENOMEM; |
Josef Bacik | 17ca04a | 2012-05-31 15:58:55 -0400 | [diff] [blame] | 1603 | } |
Li Zefan | 82d5902 | 2011-04-20 10:33:24 +0800 | [diff] [blame] | 1604 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1605 | while (1) { |
Nikolay Borisov | 3e02484 | 2019-08-21 10:42:03 +0300 | [diff] [blame] | 1606 | struct btrfs_key found_key; |
| 1607 | struct btrfs_file_extent_item *fi; |
| 1608 | struct extent_buffer *leaf; |
| 1609 | u64 extent_end; |
| 1610 | u64 extent_offset; |
Nikolay Borisov | 3e02484 | 2019-08-21 10:42:03 +0300 | [diff] [blame] | 1611 | u64 num_bytes = 0; |
| 1612 | u64 disk_num_bytes; |
Nikolay Borisov | 3e02484 | 2019-08-21 10:42:03 +0300 | [diff] [blame] | 1613 | u64 ram_bytes; |
| 1614 | int extent_type; |
Nikolay Borisov | 762bf09 | 2019-08-22 17:24:20 +0300 | [diff] [blame] | 1615 | |
| 1616 | nocow = false; |
Nikolay Borisov | 3e02484 | 2019-08-21 10:42:03 +0300 | [diff] [blame] | 1617 | |
Liu Bo | e4c3b2d | 2017-01-30 12:25:28 -0800 | [diff] [blame] | 1618 | ret = btrfs_lookup_file_extent(NULL, root, path, ino, |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1619 | cur_offset, 0); |
Josef Bacik | d788a34 | 2013-10-25 16:55:08 -0400 | [diff] [blame] | 1620 | if (ret < 0) |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1621 | goto error; |
Nikolay Borisov | a6bd9cd | 2019-08-21 10:42:57 +0300 | [diff] [blame] | 1622 | |
| 1623 | /* |
| 1624 | * If there is no extent for our range when doing the initial |
| 1625 | * search, then go back to the previous slot as it will be the |
| 1626 | * one containing the search offset |
| 1627 | */ |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1628 | if (ret > 0 && path->slots[0] > 0 && check_prev) { |
| 1629 | leaf = path->nodes[0]; |
| 1630 | btrfs_item_key_to_cpu(leaf, &found_key, |
| 1631 | path->slots[0] - 1); |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 1632 | if (found_key.objectid == ino && |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1633 | found_key.type == BTRFS_EXTENT_DATA_KEY) |
| 1634 | path->slots[0]--; |
| 1635 | } |
Nikolay Borisov | 3e02484 | 2019-08-21 10:42:03 +0300 | [diff] [blame] | 1636 | check_prev = false; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1637 | next_slot: |
Nikolay Borisov | a6bd9cd | 2019-08-21 10:42:57 +0300 | [diff] [blame] | 1638 | /* Go to next leaf if we have exhausted the current one */ |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1639 | leaf = path->nodes[0]; |
| 1640 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { |
| 1641 | ret = btrfs_next_leaf(root, path); |
Liu Bo | e891669 | 2018-01-25 11:02:50 -0700 | [diff] [blame] | 1642 | if (ret < 0) { |
| 1643 | if (cow_start != (u64)-1) |
| 1644 | cur_offset = cow_start; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1645 | goto error; |
Liu Bo | e891669 | 2018-01-25 11:02:50 -0700 | [diff] [blame] | 1646 | } |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1647 | if (ret > 0) |
| 1648 | break; |
| 1649 | leaf = path->nodes[0]; |
| 1650 | } |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1651 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1652 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1653 | |
Nikolay Borisov | a6bd9cd | 2019-08-21 10:42:57 +0300 | [diff] [blame] | 1654 | /* Didn't find anything for our INO */ |
Filipe Manana | 1d512cb | 2015-11-09 00:33:58 +0000 | [diff] [blame] | 1655 | if (found_key.objectid > ino) |
| 1656 | break; |
Nikolay Borisov | a6bd9cd | 2019-08-21 10:42:57 +0300 | [diff] [blame] | 1657 | /* |
| 1658 | * Keep searching until we find an EXTENT_ITEM or there are no |
| 1659 | * more extents for this inode |
| 1660 | */ |
Filipe Manana | 1d512cb | 2015-11-09 00:33:58 +0000 | [diff] [blame] | 1661 | if (WARN_ON_ONCE(found_key.objectid < ino) || |
| 1662 | found_key.type < BTRFS_EXTENT_DATA_KEY) { |
| 1663 | path->slots[0]++; |
| 1664 | goto next_slot; |
| 1665 | } |
Nikolay Borisov | a6bd9cd | 2019-08-21 10:42:57 +0300 | [diff] [blame] | 1666 | |
| 1667 | /* Found key is not EXTENT_DATA_KEY or starts after req range */ |
Filipe Manana | 1d512cb | 2015-11-09 00:33:58 +0000 | [diff] [blame] | 1668 | if (found_key.type > BTRFS_EXTENT_DATA_KEY || |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1669 | found_key.offset > end) |
| 1670 | break; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1671 | |
Nikolay Borisov | a6bd9cd | 2019-08-21 10:42:57 +0300 | [diff] [blame] | 1672 | /* |
| 1673 | * If the found extent starts after requested offset, then |
| 1674 | * adjust extent_end to be right before this extent begins |
| 1675 | */ |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1676 | if (found_key.offset > cur_offset) { |
| 1677 | extent_end = found_key.offset; |
Chris Mason | e9061e2 | 2009-10-09 09:57:45 -0400 | [diff] [blame] | 1678 | extent_type = 0; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1679 | goto out_check; |
| 1680 | } |
Chris Mason | c31f883 | 2008-01-08 15:46:31 -0500 | [diff] [blame] | 1681 | |
Nikolay Borisov | a6bd9cd | 2019-08-21 10:42:57 +0300 | [diff] [blame] | 1682 | /* |
| 1683 | * Found extent which begins before our range and potentially |
| 1684 | * intersect it |
| 1685 | */ |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1686 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 1687 | struct btrfs_file_extent_item); |
| 1688 | extent_type = btrfs_file_extent_type(leaf, fi); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1689 | |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 1690 | ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1691 | if (extent_type == BTRFS_FILE_EXTENT_REG || |
| 1692 | extent_type == BTRFS_FILE_EXTENT_PREALLOC) { |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1693 | disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1694 | extent_offset = btrfs_file_extent_offset(leaf, fi); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1695 | extent_end = found_key.offset + |
| 1696 | btrfs_file_extent_num_bytes(leaf, fi); |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 1697 | disk_num_bytes = |
| 1698 | btrfs_file_extent_disk_num_bytes(leaf, fi); |
Nikolay Borisov | a6bd9cd | 2019-08-21 10:42:57 +0300 | [diff] [blame] | 1699 | /* |
Filipe Manana | de7999a | 2019-12-11 09:01:40 +0000 | [diff] [blame] | 1700 | * If the extent we got ends before our current offset, |
| 1701 | * skip to the next extent. |
Nikolay Borisov | a6bd9cd | 2019-08-21 10:42:57 +0300 | [diff] [blame] | 1702 | */ |
Filipe Manana | de7999a | 2019-12-11 09:01:40 +0000 | [diff] [blame] | 1703 | if (extent_end <= cur_offset) { |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1704 | path->slots[0]++; |
| 1705 | goto next_slot; |
| 1706 | } |
Nikolay Borisov | a6bd9cd | 2019-08-21 10:42:57 +0300 | [diff] [blame] | 1707 | /* Skip holes */ |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1708 | if (disk_bytenr == 0) |
| 1709 | goto out_check; |
Nikolay Borisov | a6bd9cd | 2019-08-21 10:42:57 +0300 | [diff] [blame] | 1710 | /* Skip compressed/encrypted/encoded extents */ |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1711 | if (btrfs_file_extent_compression(leaf, fi) || |
| 1712 | btrfs_file_extent_encryption(leaf, fi) || |
| 1713 | btrfs_file_extent_other_encoding(leaf, fi)) |
| 1714 | goto out_check; |
Ethan Lien | 78d4295 | 2018-05-17 14:58:29 +0800 | [diff] [blame] | 1715 | /* |
Nikolay Borisov | a6bd9cd | 2019-08-21 10:42:57 +0300 | [diff] [blame] | 1716 | * If extent is created before the last volume's snapshot |
| 1717 | * this implies the extent is shared, hence we can't do |
| 1718 | * nocow. This is the same check as in |
| 1719 | * btrfs_cross_ref_exist but without calling |
| 1720 | * btrfs_search_slot. |
Ethan Lien | 78d4295 | 2018-05-17 14:58:29 +0800 | [diff] [blame] | 1721 | */ |
Nikolay Borisov | 3e02484 | 2019-08-21 10:42:03 +0300 | [diff] [blame] | 1722 | if (!freespace_inode && |
Lu Fengqi | 27a7ff5 | 2018-11-29 17:31:32 +0800 | [diff] [blame] | 1723 | btrfs_file_extent_generation(leaf, fi) <= |
Ethan Lien | 78d4295 | 2018-05-17 14:58:29 +0800 | [diff] [blame] | 1724 | btrfs_root_last_snapshot(&root->root_item)) |
| 1725 | goto out_check; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1726 | if (extent_type == BTRFS_FILE_EXTENT_REG && !force) |
| 1727 | goto out_check; |
Filipe Manana | c65ca98 | 2020-11-18 11:00:17 +0000 | [diff] [blame] | 1728 | |
| 1729 | /* |
| 1730 | * The following checks can be expensive, as they need to |
| 1731 | * take other locks and do btree or rbtree searches, so |
| 1732 | * release the path to avoid blocking other tasks for too |
| 1733 | * long. |
| 1734 | */ |
| 1735 | btrfs_release_path(path); |
| 1736 | |
Liu Bo | 5811375 | 2018-01-31 17:09:13 -0700 | [diff] [blame] | 1737 | ret = btrfs_cross_ref_exist(root, ino, |
| 1738 | found_key.offset - |
Boris Burkov | a84d5d4 | 2020-08-18 11:00:05 -0700 | [diff] [blame] | 1739 | extent_offset, disk_bytenr, false); |
Liu Bo | 5811375 | 2018-01-31 17:09:13 -0700 | [diff] [blame] | 1740 | if (ret) { |
| 1741 | /* |
| 1742 | * ret could be -EIO if the above fails to read |
| 1743 | * metadata. |
| 1744 | */ |
| 1745 | if (ret < 0) { |
| 1746 | if (cow_start != (u64)-1) |
| 1747 | cur_offset = cow_start; |
| 1748 | goto error; |
| 1749 | } |
| 1750 | |
Nikolay Borisov | 3e02484 | 2019-08-21 10:42:03 +0300 | [diff] [blame] | 1751 | WARN_ON_ONCE(freespace_inode); |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1752 | goto out_check; |
Liu Bo | 5811375 | 2018-01-31 17:09:13 -0700 | [diff] [blame] | 1753 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1754 | disk_bytenr += extent_offset; |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1755 | disk_bytenr += cur_offset - found_key.offset; |
| 1756 | num_bytes = min(end + 1, extent_end) - cur_offset; |
| 1757 | /* |
Nikolay Borisov | a6bd9cd | 2019-08-21 10:42:57 +0300 | [diff] [blame] | 1758 | * If there are pending snapshots for this root, we |
| 1759 | * fall into common COW way |
Wang Shilong | e9894fd | 2014-03-27 11:12:25 +0800 | [diff] [blame] | 1760 | */ |
Nikolay Borisov | 3e02484 | 2019-08-21 10:42:03 +0300 | [diff] [blame] | 1761 | if (!freespace_inode && atomic_read(&root->snapshot_force_cow)) |
Robbie Ko | 8ecebf4d | 2018-08-06 10:30:30 +0800 | [diff] [blame] | 1762 | goto out_check; |
Wang Shilong | e9894fd | 2014-03-27 11:12:25 +0800 | [diff] [blame] | 1763 | /* |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1764 | * force cow if csum exists in the range. |
| 1765 | * this ensure that csum for a given extent are |
| 1766 | * either valid or do not exist. |
| 1767 | */ |
Liu Bo | 5811375 | 2018-01-31 17:09:13 -0700 | [diff] [blame] | 1768 | ret = csum_exist_in_range(fs_info, disk_bytenr, |
| 1769 | num_bytes); |
| 1770 | if (ret) { |
Liu Bo | 5811375 | 2018-01-31 17:09:13 -0700 | [diff] [blame] | 1771 | /* |
| 1772 | * ret could be -EIO if the above fails to read |
| 1773 | * metadata. |
| 1774 | */ |
| 1775 | if (ret < 0) { |
| 1776 | if (cow_start != (u64)-1) |
| 1777 | cur_offset = cow_start; |
| 1778 | goto error; |
| 1779 | } |
Nikolay Borisov | 3e02484 | 2019-08-21 10:42:03 +0300 | [diff] [blame] | 1780 | WARN_ON_ONCE(freespace_inode); |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1781 | goto out_check; |
Robbie Ko | 91e1f56 | 2016-10-07 10:01:29 +0800 | [diff] [blame] | 1782 | } |
Filipe Manana | 2090303 | 2021-02-05 12:55:36 +0000 | [diff] [blame] | 1783 | /* If the extent's block group is RO, we must COW */ |
Robbie Ko | 8ecebf4d | 2018-08-06 10:30:30 +0800 | [diff] [blame] | 1784 | if (!btrfs_inc_nocow_writers(fs_info, disk_bytenr)) |
Filipe Manana | f78c436 | 2016-05-09 13:15:41 +0100 | [diff] [blame] | 1785 | goto out_check; |
Nikolay Borisov | 3e02484 | 2019-08-21 10:42:03 +0300 | [diff] [blame] | 1786 | nocow = true; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1787 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
Nikolay Borisov | e8e2100 | 2019-08-22 17:25:23 +0300 | [diff] [blame] | 1788 | extent_end = found_key.offset + ram_bytes; |
| 1789 | extent_end = ALIGN(extent_end, fs_info->sectorsize); |
Nikolay Borisov | 922f051 | 2019-08-05 17:47:06 +0300 | [diff] [blame] | 1790 | /* Skip extents outside of our requested range */ |
| 1791 | if (extent_end <= start) { |
| 1792 | path->slots[0]++; |
| 1793 | goto next_slot; |
| 1794 | } |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1795 | } else { |
Nikolay Borisov | e8e2100 | 2019-08-22 17:25:23 +0300 | [diff] [blame] | 1796 | /* If this triggers then we have a memory corruption */ |
Arnd Bergmann | 290342f | 2019-03-25 14:02:25 +0100 | [diff] [blame] | 1797 | BUG(); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1798 | } |
| 1799 | out_check: |
Nikolay Borisov | a6bd9cd | 2019-08-21 10:42:57 +0300 | [diff] [blame] | 1800 | /* |
| 1801 | * If nocow is false then record the beginning of the range |
| 1802 | * that needs to be COWed |
| 1803 | */ |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1804 | if (!nocow) { |
| 1805 | if (cow_start == (u64)-1) |
| 1806 | cow_start = cur_offset; |
| 1807 | cur_offset = extent_end; |
| 1808 | if (cur_offset > end) |
| 1809 | break; |
Filipe Manana | c65ca98 | 2020-11-18 11:00:17 +0000 | [diff] [blame] | 1810 | if (!path->nodes[0]) |
| 1811 | continue; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1812 | path->slots[0]++; |
| 1813 | goto next_slot; |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1814 | } |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1815 | |
Nikolay Borisov | a6bd9cd | 2019-08-21 10:42:57 +0300 | [diff] [blame] | 1816 | /* |
| 1817 | * COW range from cow_start to found_key.offset - 1. As the key |
| 1818 | * will contain the beginning of the first extent that can be |
| 1819 | * NOCOW, following one which needs to be COW'ed |
| 1820 | */ |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1821 | if (cow_start != (u64)-1) { |
Nikolay Borisov | 968322c | 2020-06-03 08:55:21 +0300 | [diff] [blame] | 1822 | ret = fallback_to_cow(inode, locked_page, |
Nikolay Borisov | 8ba96f3 | 2020-06-03 08:55:20 +0300 | [diff] [blame] | 1823 | cow_start, found_key.offset - 1, |
Filipe Manana | 467dc47 | 2020-05-27 11:16:07 +0100 | [diff] [blame] | 1824 | page_started, nr_written); |
Josef Bacik | 230ed39 | 2020-07-06 09:14:12 -0400 | [diff] [blame] | 1825 | if (ret) |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1826 | goto error; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1827 | cow_start = (u64)-1; |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1828 | } |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1829 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1830 | if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) { |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 1831 | u64 orig_start = found_key.offset - extent_offset; |
Nikolay Borisov | 3e02484 | 2019-08-21 10:42:03 +0300 | [diff] [blame] | 1832 | struct extent_map *em; |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 1833 | |
Nikolay Borisov | 968322c | 2020-06-03 08:55:21 +0300 | [diff] [blame] | 1834 | em = create_io_em(inode, cur_offset, num_bytes, |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 1835 | orig_start, |
| 1836 | disk_bytenr, /* block_start */ |
| 1837 | num_bytes, /* block_len */ |
| 1838 | disk_num_bytes, /* orig_block_len */ |
| 1839 | ram_bytes, BTRFS_COMPRESS_NONE, |
| 1840 | BTRFS_ORDERED_PREALLOC); |
| 1841 | if (IS_ERR(em)) { |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 1842 | ret = PTR_ERR(em); |
| 1843 | goto error; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1844 | } |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 1845 | free_extent_map(em); |
Nikolay Borisov | 968322c | 2020-06-03 08:55:21 +0300 | [diff] [blame] | 1846 | ret = btrfs_add_ordered_extent(inode, cur_offset, |
Nikolay Borisov | bb55f62 | 2019-08-05 17:47:05 +0300 | [diff] [blame] | 1847 | disk_bytenr, num_bytes, |
| 1848 | num_bytes, |
| 1849 | BTRFS_ORDERED_PREALLOC); |
Nikolay Borisov | 762bf09 | 2019-08-22 17:24:20 +0300 | [diff] [blame] | 1850 | if (ret) { |
Nikolay Borisov | 968322c | 2020-06-03 08:55:21 +0300 | [diff] [blame] | 1851 | btrfs_drop_extent_cache(inode, cur_offset, |
Nikolay Borisov | 762bf09 | 2019-08-22 17:24:20 +0300 | [diff] [blame] | 1852 | cur_offset + num_bytes - 1, |
| 1853 | 0); |
| 1854 | goto error; |
| 1855 | } |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1856 | } else { |
Nikolay Borisov | 968322c | 2020-06-03 08:55:21 +0300 | [diff] [blame] | 1857 | ret = btrfs_add_ordered_extent(inode, cur_offset, |
Nikolay Borisov | bb55f62 | 2019-08-05 17:47:05 +0300 | [diff] [blame] | 1858 | disk_bytenr, num_bytes, |
| 1859 | num_bytes, |
| 1860 | BTRFS_ORDERED_NOCOW); |
Nikolay Borisov | 762bf09 | 2019-08-22 17:24:20 +0300 | [diff] [blame] | 1861 | if (ret) |
| 1862 | goto error; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1863 | } |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1864 | |
Filipe Manana | f78c436 | 2016-05-09 13:15:41 +0100 | [diff] [blame] | 1865 | if (nocow) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1866 | btrfs_dec_nocow_writers(fs_info, disk_bytenr); |
Nikolay Borisov | 762bf09 | 2019-08-22 17:24:20 +0300 | [diff] [blame] | 1867 | nocow = false; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1868 | |
Yan, Zheng | efa5646 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 1869 | if (root->root_key.objectid == |
Qu Wenruo | 4dbd80f | 2017-03-08 10:25:51 +0800 | [diff] [blame] | 1870 | BTRFS_DATA_RELOC_TREE_OBJECTID) |
| 1871 | /* |
| 1872 | * Error handled later, as we must prevent |
| 1873 | * extent_clear_unlock_delalloc() in error handler |
| 1874 | * from freeing metadata of created ordered extent. |
| 1875 | */ |
Nikolay Borisov | 968322c | 2020-06-03 08:55:21 +0300 | [diff] [blame] | 1876 | ret = btrfs_reloc_clone_csums(inode, cur_offset, |
Yan, Zheng | efa5646 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 1877 | num_bytes); |
Yan, Zheng | efa5646 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 1878 | |
Nikolay Borisov | 968322c | 2020-06-03 08:55:21 +0300 | [diff] [blame] | 1879 | extent_clear_unlock_delalloc(inode, cur_offset, |
Nikolay Borisov | 74e9194 | 2019-07-17 16:18:16 +0300 | [diff] [blame] | 1880 | cur_offset + num_bytes - 1, |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1881 | locked_page, EXTENT_LOCKED | |
Wang Xiaoguang | 1851309 | 2016-07-25 15:51:40 +0800 | [diff] [blame] | 1882 | EXTENT_DELALLOC | |
| 1883 | EXTENT_CLEAR_DATA_RESV, |
Qu Wenruo | f57ad93 | 2021-04-07 19:22:13 +0800 | [diff] [blame] | 1884 | PAGE_UNLOCK | PAGE_SET_ORDERED); |
Wang Xiaoguang | 1851309 | 2016-07-25 15:51:40 +0800 | [diff] [blame] | 1885 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1886 | cur_offset = extent_end; |
Qu Wenruo | 4dbd80f | 2017-03-08 10:25:51 +0800 | [diff] [blame] | 1887 | |
| 1888 | /* |
| 1889 | * btrfs_reloc_clone_csums() error, now we're OK to call error |
| 1890 | * handler, as metadata for created ordered extent will only |
| 1891 | * be freed by btrfs_finish_ordered_io(). |
| 1892 | */ |
| 1893 | if (ret) |
| 1894 | goto error; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1895 | if (cur_offset > end) |
| 1896 | break; |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1897 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1898 | btrfs_release_path(path); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1899 | |
Robbie Ko | 506481b | 2018-10-30 18:04:04 +0800 | [diff] [blame] | 1900 | if (cur_offset <= end && cow_start == (u64)-1) |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1901 | cow_start = cur_offset; |
Josef Bacik | 17ca04a | 2012-05-31 15:58:55 -0400 | [diff] [blame] | 1902 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1903 | if (cow_start != (u64)-1) { |
Robbie Ko | 506481b | 2018-10-30 18:04:04 +0800 | [diff] [blame] | 1904 | cur_offset = end; |
Nikolay Borisov | 968322c | 2020-06-03 08:55:21 +0300 | [diff] [blame] | 1905 | ret = fallback_to_cow(inode, locked_page, cow_start, end, |
| 1906 | page_started, nr_written); |
Josef Bacik | d788a34 | 2013-10-25 16:55:08 -0400 | [diff] [blame] | 1907 | if (ret) |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1908 | goto error; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1909 | } |
| 1910 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1911 | error: |
Nikolay Borisov | 762bf09 | 2019-08-22 17:24:20 +0300 | [diff] [blame] | 1912 | if (nocow) |
| 1913 | btrfs_dec_nocow_writers(fs_info, disk_bytenr); |
| 1914 | |
Josef Bacik | 17ca04a | 2012-05-31 15:58:55 -0400 | [diff] [blame] | 1915 | if (ret && cur_offset < end) |
Nikolay Borisov | 968322c | 2020-06-03 08:55:21 +0300 | [diff] [blame] | 1916 | extent_clear_unlock_delalloc(inode, cur_offset, end, |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1917 | locked_page, EXTENT_LOCKED | |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 1918 | EXTENT_DELALLOC | EXTENT_DEFRAG | |
| 1919 | EXTENT_DO_ACCOUNTING, PAGE_UNLOCK | |
Qu Wenruo | 6869b0a | 2021-01-26 16:33:45 +0800 | [diff] [blame] | 1920 | PAGE_START_WRITEBACK | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1921 | PAGE_END_WRITEBACK); |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1922 | btrfs_free_path(path); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1923 | return ret; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1924 | } |
| 1925 | |
Goldwyn Rodrigues | 6e65ae7 | 2021-03-04 09:06:25 -0600 | [diff] [blame] | 1926 | static bool should_nocow(struct btrfs_inode *inode, u64 start, u64 end) |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 1927 | { |
Goldwyn Rodrigues | 6e65ae7 | 2021-03-04 09:06:25 -0600 | [diff] [blame] | 1928 | if (inode->flags & (BTRFS_INODE_NODATACOW | BTRFS_INODE_PREALLOC)) { |
| 1929 | if (inode->defrag_bytes && |
| 1930 | test_range_bit(&inode->io_tree, start, end, EXTENT_DEFRAG, |
| 1931 | 0, NULL)) |
| 1932 | return false; |
| 1933 | return true; |
| 1934 | } |
| 1935 | return false; |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 1936 | } |
| 1937 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1938 | /* |
Nikolay Borisov | 5eaad97 | 2018-11-01 14:09:46 +0200 | [diff] [blame] | 1939 | * Function to process delayed allocation (create CoW) for ranges which are |
| 1940 | * being touched for the first time. |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1941 | */ |
Nikolay Borisov | 98456b9 | 2020-06-03 08:55:29 +0300 | [diff] [blame] | 1942 | int btrfs_run_delalloc_range(struct btrfs_inode *inode, struct page *locked_page, |
Nikolay Borisov | 5eaad97 | 2018-11-01 14:09:46 +0200 | [diff] [blame] | 1943 | u64 start, u64 end, int *page_started, unsigned long *nr_written, |
| 1944 | struct writeback_control *wbc) |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1945 | { |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1946 | int ret; |
Naohiro Aota | 42c0110 | 2021-02-04 19:22:07 +0900 | [diff] [blame] | 1947 | const bool zoned = btrfs_is_zoned(inode->root->fs_info); |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1948 | |
Goldwyn Rodrigues | 6e65ae7 | 2021-03-04 09:06:25 -0600 | [diff] [blame] | 1949 | if (should_nocow(inode, start, end)) { |
Naohiro Aota | 42c0110 | 2021-02-04 19:22:07 +0900 | [diff] [blame] | 1950 | ASSERT(!zoned); |
Nikolay Borisov | 98456b9 | 2020-06-03 08:55:29 +0300 | [diff] [blame] | 1951 | ret = run_delalloc_nocow(inode, locked_page, start, end, |
Goldwyn Rodrigues | 6e65ae7 | 2021-03-04 09:06:25 -0600 | [diff] [blame] | 1952 | page_started, nr_written); |
Nikolay Borisov | 98456b9 | 2020-06-03 08:55:29 +0300 | [diff] [blame] | 1953 | } else if (!inode_can_compress(inode) || |
| 1954 | !inode_need_compress(inode, start, end)) { |
Naohiro Aota | 42c0110 | 2021-02-04 19:22:07 +0900 | [diff] [blame] | 1955 | if (zoned) |
| 1956 | ret = run_delalloc_zoned(inode, locked_page, start, end, |
| 1957 | page_started, nr_written); |
| 1958 | else |
| 1959 | ret = cow_file_range(inode, locked_page, start, end, |
| 1960 | page_started, nr_written, 1); |
Josef Bacik | 7ddf5a4 | 2012-06-08 15:26:47 -0400 | [diff] [blame] | 1961 | } else { |
Nikolay Borisov | 98456b9 | 2020-06-03 08:55:29 +0300 | [diff] [blame] | 1962 | set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, &inode->runtime_flags); |
| 1963 | ret = cow_file_range_async(inode, wbc, locked_page, start, end, |
David Sterba | fac07d2 | 2019-10-29 18:28:57 +0100 | [diff] [blame] | 1964 | page_started, nr_written); |
Josef Bacik | 7ddf5a4 | 2012-06-08 15:26:47 -0400 | [diff] [blame] | 1965 | } |
Qu Wenruo | 7361b4a | 2021-07-28 14:05:05 +0800 | [diff] [blame] | 1966 | ASSERT(ret <= 0); |
Qu Wenruo | 52427260 | 2017-03-08 10:25:52 +0800 | [diff] [blame] | 1967 | if (ret) |
Nikolay Borisov | 98456b9 | 2020-06-03 08:55:29 +0300 | [diff] [blame] | 1968 | btrfs_cleanup_ordered_extents(inode, locked_page, start, |
Nikolay Borisov | d1051d6 | 2018-11-21 17:10:52 +0200 | [diff] [blame] | 1969 | end - start + 1); |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 1970 | return ret; |
| 1971 | } |
| 1972 | |
Nikolay Borisov | abbb55f | 2018-11-01 14:09:53 +0200 | [diff] [blame] | 1973 | void btrfs_split_delalloc_extent(struct inode *inode, |
| 1974 | struct extent_state *orig, u64 split) |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1975 | { |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1976 | u64 size; |
| 1977 | |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1978 | /* not delalloc, ignore it */ |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1979 | if (!(orig->state & EXTENT_DELALLOC)) |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 1980 | return; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1981 | |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1982 | size = orig->end - orig->start + 1; |
| 1983 | if (size > BTRFS_MAX_EXTENT_SIZE) { |
David Sterba | 823bb20 | 2017-01-04 11:09:51 +0100 | [diff] [blame] | 1984 | u32 num_extents; |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1985 | u64 new_size; |
| 1986 | |
| 1987 | /* |
Nikolay Borisov | 5c84819 | 2018-11-01 14:09:52 +0200 | [diff] [blame] | 1988 | * See the explanation in btrfs_merge_delalloc_extent, the same |
Josef Bacik | ba11721 | 2015-03-13 15:01:24 -0400 | [diff] [blame] | 1989 | * applies here, just in reverse. |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1990 | */ |
| 1991 | new_size = orig->end - split + 1; |
David Sterba | 823bb20 | 2017-01-04 11:09:51 +0100 | [diff] [blame] | 1992 | num_extents = count_max_extents(new_size); |
Josef Bacik | ba11721 | 2015-03-13 15:01:24 -0400 | [diff] [blame] | 1993 | new_size = split - orig->start; |
David Sterba | 823bb20 | 2017-01-04 11:09:51 +0100 | [diff] [blame] | 1994 | num_extents += count_max_extents(new_size); |
| 1995 | if (count_max_extents(size) >= num_extents) |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1996 | return; |
| 1997 | } |
| 1998 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1999 | spin_lock(&BTRFS_I(inode)->lock); |
Josef Bacik | 8b62f87 | 2017-10-19 14:15:55 -0400 | [diff] [blame] | 2000 | btrfs_mod_outstanding_extents(BTRFS_I(inode), 1); |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 2001 | spin_unlock(&BTRFS_I(inode)->lock); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 2002 | } |
| 2003 | |
| 2004 | /* |
Nikolay Borisov | 5c84819 | 2018-11-01 14:09:52 +0200 | [diff] [blame] | 2005 | * Handle merged delayed allocation extents so we can keep track of new extents |
| 2006 | * that are just merged onto old extents, such as when we are doing sequential |
| 2007 | * writes, so we can properly account for the metadata space we'll need. |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 2008 | */ |
Nikolay Borisov | 5c84819 | 2018-11-01 14:09:52 +0200 | [diff] [blame] | 2009 | void btrfs_merge_delalloc_extent(struct inode *inode, struct extent_state *new, |
| 2010 | struct extent_state *other) |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 2011 | { |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 2012 | u64 new_size, old_size; |
David Sterba | 823bb20 | 2017-01-04 11:09:51 +0100 | [diff] [blame] | 2013 | u32 num_extents; |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 2014 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 2015 | /* not delalloc, ignore it */ |
| 2016 | if (!(other->state & EXTENT_DELALLOC)) |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 2017 | return; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 2018 | |
Josef Bacik | 8461a3d | 2015-03-13 15:12:08 -0400 | [diff] [blame] | 2019 | if (new->start > other->start) |
| 2020 | new_size = new->end - other->start + 1; |
| 2021 | else |
| 2022 | new_size = other->end - new->start + 1; |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 2023 | |
| 2024 | /* we're not bigger than the max, unreserve the space and go */ |
| 2025 | if (new_size <= BTRFS_MAX_EXTENT_SIZE) { |
| 2026 | spin_lock(&BTRFS_I(inode)->lock); |
Josef Bacik | 8b62f87 | 2017-10-19 14:15:55 -0400 | [diff] [blame] | 2027 | btrfs_mod_outstanding_extents(BTRFS_I(inode), -1); |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 2028 | spin_unlock(&BTRFS_I(inode)->lock); |
| 2029 | return; |
| 2030 | } |
| 2031 | |
| 2032 | /* |
Josef Bacik | ba11721 | 2015-03-13 15:01:24 -0400 | [diff] [blame] | 2033 | * We have to add up either side to figure out how many extents were |
| 2034 | * accounted for before we merged into one big extent. If the number of |
| 2035 | * extents we accounted for is <= the amount we need for the new range |
| 2036 | * then we can return, otherwise drop. Think of it like this |
| 2037 | * |
| 2038 | * [ 4k][MAX_SIZE] |
| 2039 | * |
| 2040 | * So we've grown the extent by a MAX_SIZE extent, this would mean we |
| 2041 | * need 2 outstanding extents, on one side we have 1 and the other side |
| 2042 | * we have 1 so they are == and we can return. But in this case |
| 2043 | * |
| 2044 | * [MAX_SIZE+4k][MAX_SIZE+4k] |
| 2045 | * |
| 2046 | * Each range on their own accounts for 2 extents, but merged together |
| 2047 | * they are only 3 extents worth of accounting, so we need to drop in |
| 2048 | * this case. |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 2049 | */ |
Josef Bacik | ba11721 | 2015-03-13 15:01:24 -0400 | [diff] [blame] | 2050 | old_size = other->end - other->start + 1; |
David Sterba | 823bb20 | 2017-01-04 11:09:51 +0100 | [diff] [blame] | 2051 | num_extents = count_max_extents(old_size); |
Josef Bacik | ba11721 | 2015-03-13 15:01:24 -0400 | [diff] [blame] | 2052 | old_size = new->end - new->start + 1; |
David Sterba | 823bb20 | 2017-01-04 11:09:51 +0100 | [diff] [blame] | 2053 | num_extents += count_max_extents(old_size); |
| 2054 | if (count_max_extents(new_size) >= num_extents) |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 2055 | return; |
| 2056 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 2057 | spin_lock(&BTRFS_I(inode)->lock); |
Josef Bacik | 8b62f87 | 2017-10-19 14:15:55 -0400 | [diff] [blame] | 2058 | btrfs_mod_outstanding_extents(BTRFS_I(inode), -1); |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 2059 | spin_unlock(&BTRFS_I(inode)->lock); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 2060 | } |
| 2061 | |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 2062 | static void btrfs_add_delalloc_inodes(struct btrfs_root *root, |
| 2063 | struct inode *inode) |
| 2064 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2065 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
| 2066 | |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 2067 | spin_lock(&root->delalloc_lock); |
| 2068 | if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) { |
| 2069 | list_add_tail(&BTRFS_I(inode)->delalloc_inodes, |
| 2070 | &root->delalloc_inodes); |
| 2071 | set_bit(BTRFS_INODE_IN_DELALLOC_LIST, |
| 2072 | &BTRFS_I(inode)->runtime_flags); |
| 2073 | root->nr_delalloc_inodes++; |
| 2074 | if (root->nr_delalloc_inodes == 1) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2075 | spin_lock(&fs_info->delalloc_root_lock); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 2076 | BUG_ON(!list_empty(&root->delalloc_root)); |
| 2077 | list_add_tail(&root->delalloc_root, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2078 | &fs_info->delalloc_roots); |
| 2079 | spin_unlock(&fs_info->delalloc_root_lock); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 2080 | } |
| 2081 | } |
| 2082 | spin_unlock(&root->delalloc_lock); |
| 2083 | } |
| 2084 | |
Nikolay Borisov | 2b87733 | 2018-04-27 12:21:51 +0300 | [diff] [blame] | 2085 | |
| 2086 | void __btrfs_del_delalloc_inode(struct btrfs_root *root, |
| 2087 | struct btrfs_inode *inode) |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 2088 | { |
David Sterba | 3ffbd68 | 2018-06-29 10:56:42 +0200 | [diff] [blame] | 2089 | struct btrfs_fs_info *fs_info = root->fs_info; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2090 | |
Nikolay Borisov | 9e3e97f | 2017-02-20 13:51:07 +0200 | [diff] [blame] | 2091 | if (!list_empty(&inode->delalloc_inodes)) { |
| 2092 | list_del_init(&inode->delalloc_inodes); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 2093 | clear_bit(BTRFS_INODE_IN_DELALLOC_LIST, |
Nikolay Borisov | 9e3e97f | 2017-02-20 13:51:07 +0200 | [diff] [blame] | 2094 | &inode->runtime_flags); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 2095 | root->nr_delalloc_inodes--; |
| 2096 | if (!root->nr_delalloc_inodes) { |
Nikolay Borisov | 7c8a0d3 | 2018-04-27 12:21:52 +0300 | [diff] [blame] | 2097 | ASSERT(list_empty(&root->delalloc_inodes)); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2098 | spin_lock(&fs_info->delalloc_root_lock); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 2099 | BUG_ON(list_empty(&root->delalloc_root)); |
| 2100 | list_del_init(&root->delalloc_root); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2101 | spin_unlock(&fs_info->delalloc_root_lock); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 2102 | } |
| 2103 | } |
Nikolay Borisov | 2b87733 | 2018-04-27 12:21:51 +0300 | [diff] [blame] | 2104 | } |
| 2105 | |
| 2106 | static void btrfs_del_delalloc_inode(struct btrfs_root *root, |
| 2107 | struct btrfs_inode *inode) |
| 2108 | { |
| 2109 | spin_lock(&root->delalloc_lock); |
| 2110 | __btrfs_del_delalloc_inode(root, inode); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 2111 | spin_unlock(&root->delalloc_lock); |
| 2112 | } |
| 2113 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2114 | /* |
Nikolay Borisov | e06a1fc | 2018-11-01 14:09:50 +0200 | [diff] [blame] | 2115 | * Properly track delayed allocation bytes in the inode and to maintain the |
| 2116 | * list of inodes that have pending delalloc work to be done. |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2117 | */ |
Nikolay Borisov | e06a1fc | 2018-11-01 14:09:50 +0200 | [diff] [blame] | 2118 | void btrfs_set_delalloc_extent(struct inode *inode, struct extent_state *state, |
| 2119 | unsigned *bits) |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 2120 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2121 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
| 2122 | |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 2123 | if ((*bits & EXTENT_DEFRAG) && !(*bits & EXTENT_DELALLOC)) |
| 2124 | WARN_ON(1); |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 2125 | /* |
| 2126 | * set_bit and clear bit hooks normally require _irqsave/restore |
Sergei Trofimovich | 27160b6 | 2011-05-20 20:20:32 +0000 | [diff] [blame] | 2127 | * but in this case, we are only testing for the DELALLOC |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 2128 | * bit, which is only set or cleared with irqs on |
| 2129 | */ |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 2130 | if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) { |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 2131 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 2132 | u64 len = state->end + 1 - state->start; |
Josef Bacik | 8b62f87 | 2017-10-19 14:15:55 -0400 | [diff] [blame] | 2133 | u32 num_extents = count_max_extents(len); |
Nikolay Borisov | 70ddc55 | 2017-02-20 13:50:35 +0200 | [diff] [blame] | 2134 | bool do_list = !btrfs_is_free_space_inode(BTRFS_I(inode)); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 2135 | |
Josef Bacik | 8b62f87 | 2017-10-19 14:15:55 -0400 | [diff] [blame] | 2136 | spin_lock(&BTRFS_I(inode)->lock); |
| 2137 | btrfs_mod_outstanding_extents(BTRFS_I(inode), num_extents); |
| 2138 | spin_unlock(&BTRFS_I(inode)->lock); |
Josef Bacik | 287a0ab | 2010-03-19 18:07:23 +0000 | [diff] [blame] | 2139 | |
Josef Bacik | 6a3891c | 2015-03-16 17:38:52 -0400 | [diff] [blame] | 2140 | /* For sanity tests */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2141 | if (btrfs_is_testing(fs_info)) |
Josef Bacik | 6a3891c | 2015-03-16 17:38:52 -0400 | [diff] [blame] | 2142 | return; |
| 2143 | |
Nikolay Borisov | 104b4e5 | 2017-06-20 21:01:20 +0300 | [diff] [blame] | 2144 | percpu_counter_add_batch(&fs_info->delalloc_bytes, len, |
| 2145 | fs_info->delalloc_batch); |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 2146 | spin_lock(&BTRFS_I(inode)->lock); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 2147 | BTRFS_I(inode)->delalloc_bytes += len; |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 2148 | if (*bits & EXTENT_DEFRAG) |
| 2149 | BTRFS_I(inode)->defrag_bytes += len; |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 2150 | if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST, |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 2151 | &BTRFS_I(inode)->runtime_flags)) |
| 2152 | btrfs_add_delalloc_inodes(root, inode); |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 2153 | spin_unlock(&BTRFS_I(inode)->lock); |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 2154 | } |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 2155 | |
| 2156 | if (!(state->state & EXTENT_DELALLOC_NEW) && |
| 2157 | (*bits & EXTENT_DELALLOC_NEW)) { |
| 2158 | spin_lock(&BTRFS_I(inode)->lock); |
| 2159 | BTRFS_I(inode)->new_delalloc_bytes += state->end + 1 - |
| 2160 | state->start; |
| 2161 | spin_unlock(&BTRFS_I(inode)->lock); |
| 2162 | } |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 2163 | } |
| 2164 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2165 | /* |
Nikolay Borisov | a36bb5f | 2018-11-01 14:09:51 +0200 | [diff] [blame] | 2166 | * Once a range is no longer delalloc this function ensures that proper |
| 2167 | * accounting happens. |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2168 | */ |
Nikolay Borisov | a36bb5f | 2018-11-01 14:09:51 +0200 | [diff] [blame] | 2169 | void btrfs_clear_delalloc_extent(struct inode *vfs_inode, |
| 2170 | struct extent_state *state, unsigned *bits) |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 2171 | { |
Nikolay Borisov | a36bb5f | 2018-11-01 14:09:51 +0200 | [diff] [blame] | 2172 | struct btrfs_inode *inode = BTRFS_I(vfs_inode); |
| 2173 | struct btrfs_fs_info *fs_info = btrfs_sb(vfs_inode->i_sb); |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 2174 | u64 len = state->end + 1 - state->start; |
David Sterba | 823bb20 | 2017-01-04 11:09:51 +0100 | [diff] [blame] | 2175 | u32 num_extents = count_max_extents(len); |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 2176 | |
Filipe Manana | 4a4b964 | 2017-07-27 19:52:55 +0100 | [diff] [blame] | 2177 | if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG)) { |
| 2178 | spin_lock(&inode->lock); |
Nikolay Borisov | 6fc0ef6 | 2017-02-20 13:51:03 +0200 | [diff] [blame] | 2179 | inode->defrag_bytes -= len; |
Filipe Manana | 4a4b964 | 2017-07-27 19:52:55 +0100 | [diff] [blame] | 2180 | spin_unlock(&inode->lock); |
| 2181 | } |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 2182 | |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 2183 | /* |
| 2184 | * set_bit and clear bit hooks normally require _irqsave/restore |
Sergei Trofimovich | 27160b6 | 2011-05-20 20:20:32 +0000 | [diff] [blame] | 2185 | * but in this case, we are only testing for the DELALLOC |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 2186 | * bit, which is only set or cleared with irqs on |
| 2187 | */ |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 2188 | if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) { |
Nikolay Borisov | 6fc0ef6 | 2017-02-20 13:51:03 +0200 | [diff] [blame] | 2189 | struct btrfs_root *root = inode->root; |
Liu Bo | 83eea1f | 2012-07-10 05:28:39 -0600 | [diff] [blame] | 2190 | bool do_list = !btrfs_is_free_space_inode(inode); |
Chris Mason | bcbfce8 | 2008-04-22 13:26:47 -0400 | [diff] [blame] | 2191 | |
Josef Bacik | 8b62f87 | 2017-10-19 14:15:55 -0400 | [diff] [blame] | 2192 | spin_lock(&inode->lock); |
| 2193 | btrfs_mod_outstanding_extents(inode, -num_extents); |
| 2194 | spin_unlock(&inode->lock); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 2195 | |
Josef Bacik | b6d08f0 | 2013-09-27 14:57:43 -0400 | [diff] [blame] | 2196 | /* |
| 2197 | * We don't reserve metadata space for space cache inodes so we |
Andrea Gelmini | 52042d8 | 2018-11-28 12:05:13 +0100 | [diff] [blame] | 2198 | * don't need to call delalloc_release_metadata if there is an |
Josef Bacik | b6d08f0 | 2013-09-27 14:57:43 -0400 | [diff] [blame] | 2199 | * error. |
| 2200 | */ |
Filipe Manana | a315e68 | 2017-03-06 23:04:20 +0000 | [diff] [blame] | 2201 | if (*bits & EXTENT_CLEAR_META_RESV && |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2202 | root != fs_info->tree_root) |
Qu Wenruo | 43b1859 | 2017-12-12 15:34:32 +0800 | [diff] [blame] | 2203 | btrfs_delalloc_release_metadata(inode, len, false); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 2204 | |
Josef Bacik | 6a3891c | 2015-03-16 17:38:52 -0400 | [diff] [blame] | 2205 | /* For sanity tests. */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2206 | if (btrfs_is_testing(fs_info)) |
Josef Bacik | 6a3891c | 2015-03-16 17:38:52 -0400 | [diff] [blame] | 2207 | return; |
| 2208 | |
Filipe Manana | a315e68 | 2017-03-06 23:04:20 +0000 | [diff] [blame] | 2209 | if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID && |
| 2210 | do_list && !(state->state & EXTENT_NORESERVE) && |
| 2211 | (*bits & EXTENT_CLEAR_DATA_RESV)) |
Nikolay Borisov | 9db5d51 | 2020-06-03 08:55:38 +0300 | [diff] [blame] | 2212 | btrfs_free_reserved_data_space_noquota(fs_info, len); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 2213 | |
Nikolay Borisov | 104b4e5 | 2017-06-20 21:01:20 +0300 | [diff] [blame] | 2214 | percpu_counter_add_batch(&fs_info->delalloc_bytes, -len, |
| 2215 | fs_info->delalloc_batch); |
Nikolay Borisov | 6fc0ef6 | 2017-02-20 13:51:03 +0200 | [diff] [blame] | 2216 | spin_lock(&inode->lock); |
| 2217 | inode->delalloc_bytes -= len; |
| 2218 | if (do_list && inode->delalloc_bytes == 0 && |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 2219 | test_bit(BTRFS_INODE_IN_DELALLOC_LIST, |
Nikolay Borisov | 9e3e97f | 2017-02-20 13:51:07 +0200 | [diff] [blame] | 2220 | &inode->runtime_flags)) |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 2221 | btrfs_del_delalloc_inode(root, inode); |
Nikolay Borisov | 6fc0ef6 | 2017-02-20 13:51:03 +0200 | [diff] [blame] | 2222 | spin_unlock(&inode->lock); |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 2223 | } |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 2224 | |
| 2225 | if ((state->state & EXTENT_DELALLOC_NEW) && |
| 2226 | (*bits & EXTENT_DELALLOC_NEW)) { |
| 2227 | spin_lock(&inode->lock); |
| 2228 | ASSERT(inode->new_delalloc_bytes >= len); |
| 2229 | inode->new_delalloc_bytes -= len; |
Filipe Manana | 2766ff6 | 2020-11-04 11:07:34 +0000 | [diff] [blame] | 2230 | if (*bits & EXTENT_ADD_INODE_BYTES) |
| 2231 | inode_add_bytes(&inode->vfs_inode, len); |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 2232 | spin_unlock(&inode->lock); |
| 2233 | } |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 2234 | } |
| 2235 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2236 | /* |
Nikolay Borisov | da12fe5 | 2018-11-27 20:57:58 +0200 | [diff] [blame] | 2237 | * btrfs_bio_fits_in_stripe - Checks whether the size of the given bio will fit |
| 2238 | * in a chunk's stripe. This function ensures that bios do not span a |
| 2239 | * stripe/chunk |
Liu Bo | 6f034ec | 2016-06-22 18:31:49 -0700 | [diff] [blame] | 2240 | * |
Nikolay Borisov | da12fe5 | 2018-11-27 20:57:58 +0200 | [diff] [blame] | 2241 | * @page - The page we are about to add to the bio |
| 2242 | * @size - size we want to add to the bio |
| 2243 | * @bio - bio we want to ensure is smaller than a stripe |
| 2244 | * @bio_flags - flags of the bio |
| 2245 | * |
| 2246 | * return 1 if page cannot be added to the bio |
| 2247 | * return 0 if page can be added to the bio |
Liu Bo | 6f034ec | 2016-06-22 18:31:49 -0700 | [diff] [blame] | 2248 | * return error otherwise |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2249 | */ |
Nikolay Borisov | da12fe5 | 2018-11-27 20:57:58 +0200 | [diff] [blame] | 2250 | int btrfs_bio_fits_in_stripe(struct page *page, size_t size, struct bio *bio, |
| 2251 | unsigned long bio_flags) |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 2252 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2253 | struct inode *inode = page->mapping->host; |
| 2254 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
David Sterba | 1201b58 | 2020-11-26 15:41:27 +0100 | [diff] [blame] | 2255 | u64 logical = bio->bi_iter.bi_sector << 9; |
Qu Wenruo | 1a0b5c4 | 2021-04-13 18:00:47 +0800 | [diff] [blame] | 2256 | u32 bio_len = bio->bi_iter.bi_size; |
Michal Rostecki | 4203431 | 2021-01-27 14:57:27 +0100 | [diff] [blame] | 2257 | struct extent_map *em; |
Michal Rostecki | 4203431 | 2021-01-27 14:57:27 +0100 | [diff] [blame] | 2258 | int ret = 0; |
Nikolay Borisov | 89b798a | 2019-06-03 12:05:05 +0300 | [diff] [blame] | 2259 | struct btrfs_io_geometry geom; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 2260 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 2261 | if (bio_flags & EXTENT_BIO_COMPRESSED) |
| 2262 | return 0; |
| 2263 | |
Qu Wenruo | 1a0b5c4 | 2021-04-13 18:00:47 +0800 | [diff] [blame] | 2264 | em = btrfs_get_chunk_map(fs_info, logical, fs_info->sectorsize); |
Michal Rostecki | 4203431 | 2021-01-27 14:57:27 +0100 | [diff] [blame] | 2265 | if (IS_ERR(em)) |
| 2266 | return PTR_ERR(em); |
Qu Wenruo | 43c0d1a | 2021-04-13 17:58:48 +0800 | [diff] [blame] | 2267 | ret = btrfs_get_io_geometry(fs_info, em, btrfs_op(bio), logical, &geom); |
Liu Bo | 6f034ec | 2016-06-22 18:31:49 -0700 | [diff] [blame] | 2268 | if (ret < 0) |
Michal Rostecki | 4203431 | 2021-01-27 14:57:27 +0100 | [diff] [blame] | 2269 | goto out; |
Nikolay Borisov | 89b798a | 2019-06-03 12:05:05 +0300 | [diff] [blame] | 2270 | |
Qu Wenruo | 1a0b5c4 | 2021-04-13 18:00:47 +0800 | [diff] [blame] | 2271 | if (geom.len < bio_len + size) |
Michal Rostecki | 4203431 | 2021-01-27 14:57:27 +0100 | [diff] [blame] | 2272 | ret = 1; |
| 2273 | out: |
| 2274 | free_extent_map(em); |
| 2275 | return ret; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 2276 | } |
| 2277 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2278 | /* |
| 2279 | * in order to insert checksums into the metadata in large chunks, |
| 2280 | * we wait until bio submission time. All the pages in the bio are |
| 2281 | * checksummed and sums are attached onto the ordered extent record. |
| 2282 | * |
| 2283 | * At IO completion time the cums attached on the ordered extent record |
| 2284 | * are inserted into the btree |
| 2285 | */ |
Qu Wenruo | 8896a08 | 2020-10-21 14:24:53 +0800 | [diff] [blame] | 2286 | static blk_status_t btrfs_submit_bio_start(struct inode *inode, struct bio *bio, |
Qu Wenruo | 1941b64 | 2020-12-02 14:47:57 +0800 | [diff] [blame] | 2287 | u64 dio_file_offset) |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 2288 | { |
Johannes Thumshirn | c965d64 | 2020-07-28 20:25:41 +0900 | [diff] [blame] | 2289 | return btrfs_csum_one_bio(BTRFS_I(inode), bio, 0, 0); |
Chris Mason | 4a69a41 | 2008-11-06 22:03:00 -0500 | [diff] [blame] | 2290 | } |
Chris Mason | e015640 | 2008-04-16 11:15:20 -0400 | [diff] [blame] | 2291 | |
Naohiro Aota | abb99cf | 2021-06-28 17:57:28 +0900 | [diff] [blame] | 2292 | /* |
| 2293 | * Split an extent_map at [start, start + len] |
| 2294 | * |
| 2295 | * This function is intended to be used only for extract_ordered_extent(). |
| 2296 | */ |
| 2297 | static int split_zoned_em(struct btrfs_inode *inode, u64 start, u64 len, |
| 2298 | u64 pre, u64 post) |
| 2299 | { |
| 2300 | struct extent_map_tree *em_tree = &inode->extent_tree; |
| 2301 | struct extent_map *em; |
| 2302 | struct extent_map *split_pre = NULL; |
| 2303 | struct extent_map *split_mid = NULL; |
| 2304 | struct extent_map *split_post = NULL; |
| 2305 | int ret = 0; |
Naohiro Aota | abb99cf | 2021-06-28 17:57:28 +0900 | [diff] [blame] | 2306 | unsigned long flags; |
| 2307 | |
| 2308 | /* Sanity check */ |
| 2309 | if (pre == 0 && post == 0) |
| 2310 | return 0; |
| 2311 | |
| 2312 | split_pre = alloc_extent_map(); |
| 2313 | if (pre) |
| 2314 | split_mid = alloc_extent_map(); |
| 2315 | if (post) |
| 2316 | split_post = alloc_extent_map(); |
| 2317 | if (!split_pre || (pre && !split_mid) || (post && !split_post)) { |
| 2318 | ret = -ENOMEM; |
| 2319 | goto out; |
| 2320 | } |
| 2321 | |
| 2322 | ASSERT(pre + post < len); |
| 2323 | |
| 2324 | lock_extent(&inode->io_tree, start, start + len - 1); |
| 2325 | write_lock(&em_tree->lock); |
| 2326 | em = lookup_extent_mapping(em_tree, start, len); |
| 2327 | if (!em) { |
| 2328 | ret = -EIO; |
| 2329 | goto out_unlock; |
| 2330 | } |
| 2331 | |
| 2332 | ASSERT(em->len == len); |
| 2333 | ASSERT(!test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)); |
| 2334 | ASSERT(em->block_start < EXTENT_MAP_LAST_BYTE); |
Naohiro Aota | 63fb587 | 2021-08-09 09:29:18 +0900 | [diff] [blame] | 2335 | ASSERT(test_bit(EXTENT_FLAG_PINNED, &em->flags)); |
| 2336 | ASSERT(!test_bit(EXTENT_FLAG_LOGGING, &em->flags)); |
| 2337 | ASSERT(!list_empty(&em->list)); |
Naohiro Aota | abb99cf | 2021-06-28 17:57:28 +0900 | [diff] [blame] | 2338 | |
| 2339 | flags = em->flags; |
| 2340 | clear_bit(EXTENT_FLAG_PINNED, &em->flags); |
Naohiro Aota | abb99cf | 2021-06-28 17:57:28 +0900 | [diff] [blame] | 2341 | |
| 2342 | /* First, replace the em with a new extent_map starting from * em->start */ |
| 2343 | split_pre->start = em->start; |
| 2344 | split_pre->len = (pre ? pre : em->len - post); |
| 2345 | split_pre->orig_start = split_pre->start; |
| 2346 | split_pre->block_start = em->block_start; |
| 2347 | split_pre->block_len = split_pre->len; |
| 2348 | split_pre->orig_block_len = split_pre->block_len; |
| 2349 | split_pre->ram_bytes = split_pre->len; |
| 2350 | split_pre->flags = flags; |
| 2351 | split_pre->compress_type = em->compress_type; |
| 2352 | split_pre->generation = em->generation; |
| 2353 | |
Naohiro Aota | 63fb587 | 2021-08-09 09:29:18 +0900 | [diff] [blame] | 2354 | replace_extent_mapping(em_tree, em, split_pre, 1); |
Naohiro Aota | abb99cf | 2021-06-28 17:57:28 +0900 | [diff] [blame] | 2355 | |
| 2356 | /* |
| 2357 | * Now we only have an extent_map at: |
| 2358 | * [em->start, em->start + pre] if pre != 0 |
| 2359 | * [em->start, em->start + em->len - post] if pre == 0 |
| 2360 | */ |
| 2361 | |
| 2362 | if (pre) { |
| 2363 | /* Insert the middle extent_map */ |
| 2364 | split_mid->start = em->start + pre; |
| 2365 | split_mid->len = em->len - pre - post; |
| 2366 | split_mid->orig_start = split_mid->start; |
| 2367 | split_mid->block_start = em->block_start + pre; |
| 2368 | split_mid->block_len = split_mid->len; |
| 2369 | split_mid->orig_block_len = split_mid->block_len; |
| 2370 | split_mid->ram_bytes = split_mid->len; |
| 2371 | split_mid->flags = flags; |
| 2372 | split_mid->compress_type = em->compress_type; |
| 2373 | split_mid->generation = em->generation; |
Naohiro Aota | 63fb587 | 2021-08-09 09:29:18 +0900 | [diff] [blame] | 2374 | add_extent_mapping(em_tree, split_mid, 1); |
Naohiro Aota | abb99cf | 2021-06-28 17:57:28 +0900 | [diff] [blame] | 2375 | } |
| 2376 | |
| 2377 | if (post) { |
| 2378 | split_post->start = em->start + em->len - post; |
| 2379 | split_post->len = post; |
| 2380 | split_post->orig_start = split_post->start; |
| 2381 | split_post->block_start = em->block_start + em->len - post; |
| 2382 | split_post->block_len = split_post->len; |
| 2383 | split_post->orig_block_len = split_post->block_len; |
| 2384 | split_post->ram_bytes = split_post->len; |
| 2385 | split_post->flags = flags; |
| 2386 | split_post->compress_type = em->compress_type; |
| 2387 | split_post->generation = em->generation; |
Naohiro Aota | 63fb587 | 2021-08-09 09:29:18 +0900 | [diff] [blame] | 2388 | add_extent_mapping(em_tree, split_post, 1); |
Naohiro Aota | abb99cf | 2021-06-28 17:57:28 +0900 | [diff] [blame] | 2389 | } |
| 2390 | |
| 2391 | /* Once for us */ |
| 2392 | free_extent_map(em); |
| 2393 | /* Once for the tree */ |
| 2394 | free_extent_map(em); |
| 2395 | |
| 2396 | out_unlock: |
| 2397 | write_unlock(&em_tree->lock); |
| 2398 | unlock_extent(&inode->io_tree, start, start + len - 1); |
| 2399 | out: |
| 2400 | free_extent_map(split_pre); |
| 2401 | free_extent_map(split_mid); |
| 2402 | free_extent_map(split_post); |
| 2403 | |
| 2404 | return ret; |
| 2405 | } |
| 2406 | |
Naohiro Aota | d22002f | 2021-02-04 19:22:00 +0900 | [diff] [blame] | 2407 | static blk_status_t extract_ordered_extent(struct btrfs_inode *inode, |
| 2408 | struct bio *bio, loff_t file_offset) |
| 2409 | { |
| 2410 | struct btrfs_ordered_extent *ordered; |
Naohiro Aota | d22002f | 2021-02-04 19:22:00 +0900 | [diff] [blame] | 2411 | u64 start = (u64)bio->bi_iter.bi_sector << SECTOR_SHIFT; |
Naohiro Aota | abb99cf | 2021-06-28 17:57:28 +0900 | [diff] [blame] | 2412 | u64 file_len; |
Naohiro Aota | d22002f | 2021-02-04 19:22:00 +0900 | [diff] [blame] | 2413 | u64 len = bio->bi_iter.bi_size; |
| 2414 | u64 end = start + len; |
| 2415 | u64 ordered_end; |
| 2416 | u64 pre, post; |
| 2417 | int ret = 0; |
| 2418 | |
| 2419 | ordered = btrfs_lookup_ordered_extent(inode, file_offset); |
| 2420 | if (WARN_ON_ONCE(!ordered)) |
| 2421 | return BLK_STS_IOERR; |
| 2422 | |
| 2423 | /* No need to split */ |
| 2424 | if (ordered->disk_num_bytes == len) |
| 2425 | goto out; |
| 2426 | |
| 2427 | /* We cannot split once end_bio'd ordered extent */ |
| 2428 | if (WARN_ON_ONCE(ordered->bytes_left != ordered->disk_num_bytes)) { |
| 2429 | ret = -EINVAL; |
| 2430 | goto out; |
| 2431 | } |
| 2432 | |
| 2433 | /* We cannot split a compressed ordered extent */ |
| 2434 | if (WARN_ON_ONCE(ordered->disk_num_bytes != ordered->num_bytes)) { |
| 2435 | ret = -EINVAL; |
| 2436 | goto out; |
| 2437 | } |
| 2438 | |
| 2439 | ordered_end = ordered->disk_bytenr + ordered->disk_num_bytes; |
| 2440 | /* bio must be in one ordered extent */ |
| 2441 | if (WARN_ON_ONCE(start < ordered->disk_bytenr || end > ordered_end)) { |
| 2442 | ret = -EINVAL; |
| 2443 | goto out; |
| 2444 | } |
| 2445 | |
| 2446 | /* Checksum list should be empty */ |
| 2447 | if (WARN_ON_ONCE(!list_empty(&ordered->list))) { |
| 2448 | ret = -EINVAL; |
| 2449 | goto out; |
| 2450 | } |
| 2451 | |
Naohiro Aota | abb99cf | 2021-06-28 17:57:28 +0900 | [diff] [blame] | 2452 | file_len = ordered->num_bytes; |
Naohiro Aota | d22002f | 2021-02-04 19:22:00 +0900 | [diff] [blame] | 2453 | pre = start - ordered->disk_bytenr; |
| 2454 | post = ordered_end - end; |
| 2455 | |
| 2456 | ret = btrfs_split_ordered_extent(ordered, pre, post); |
| 2457 | if (ret) |
| 2458 | goto out; |
Naohiro Aota | abb99cf | 2021-06-28 17:57:28 +0900 | [diff] [blame] | 2459 | ret = split_zoned_em(inode, file_offset, file_len, pre, post); |
Naohiro Aota | d22002f | 2021-02-04 19:22:00 +0900 | [diff] [blame] | 2460 | |
| 2461 | out: |
Naohiro Aota | d22002f | 2021-02-04 19:22:00 +0900 | [diff] [blame] | 2462 | btrfs_put_ordered_extent(ordered); |
| 2463 | |
| 2464 | return errno_to_blk_status(ret); |
| 2465 | } |
| 2466 | |
Chris Mason | 4a69a41 | 2008-11-06 22:03:00 -0500 | [diff] [blame] | 2467 | /* |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 2468 | * extent_io.c submission hook. This does the right thing for csum calculation |
Liu Bo | 4c274bc | 2017-11-01 17:19:27 -0600 | [diff] [blame] | 2469 | * on write, or reading the csums from the tree before a read. |
| 2470 | * |
| 2471 | * Rules about async/sync submit, |
| 2472 | * a) read: sync submit |
| 2473 | * |
| 2474 | * b) write without checksum: sync submit |
| 2475 | * |
| 2476 | * c) write with checksum: |
| 2477 | * c-1) if bio is issued by fsync: sync submit |
| 2478 | * (sync_writers != 0) |
| 2479 | * |
| 2480 | * c-2) if root is reloc root: sync submit |
| 2481 | * (only in case of buffered IO) |
| 2482 | * |
| 2483 | * c-3) otherwise: async submit |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2484 | */ |
Nikolay Borisov | 908930f | 2020-09-18 16:34:37 +0300 | [diff] [blame] | 2485 | blk_status_t btrfs_submit_data_bio(struct inode *inode, struct bio *bio, |
| 2486 | int mirror_num, unsigned long bio_flags) |
Nikolay Borisov | 50489a5 | 2019-04-10 19:46:04 +0300 | [diff] [blame] | 2487 | |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 2488 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2489 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 2490 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chandan Rajendra | 0d51e28 | 2015-07-27 15:26:43 +0530 | [diff] [blame] | 2491 | enum btrfs_wq_endio_type metadata = BTRFS_WQ_ENDIO_DATA; |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 2492 | blk_status_t ret = 0; |
Chris Mason | 19b9bdb | 2008-10-30 14:23:13 -0400 | [diff] [blame] | 2493 | int skip_sum; |
Josef Bacik | b812ce2 | 2012-11-16 13:56:32 -0500 | [diff] [blame] | 2494 | int async = !atomic_read(&BTRFS_I(inode)->sync_writers); |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 2495 | |
Josef Bacik | 42437a6 | 2020-10-16 11:29:18 -0400 | [diff] [blame] | 2496 | skip_sum = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM) || |
| 2497 | !fs_info->csum_root; |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 2498 | |
Nikolay Borisov | 70ddc55 | 2017-02-20 13:50:35 +0200 | [diff] [blame] | 2499 | if (btrfs_is_free_space_inode(BTRFS_I(inode))) |
Chandan Rajendra | 0d51e28 | 2015-07-27 15:26:43 +0530 | [diff] [blame] | 2500 | metadata = BTRFS_WQ_ENDIO_FREE_SPACE; |
Jeff Mahoney | 0417341 | 2011-10-03 23:23:12 -0400 | [diff] [blame] | 2501 | |
Naohiro Aota | d22002f | 2021-02-04 19:22:00 +0900 | [diff] [blame] | 2502 | if (bio_op(bio) == REQ_OP_ZONE_APPEND) { |
| 2503 | struct page *page = bio_first_bvec_all(bio)->bv_page; |
| 2504 | loff_t file_offset = page_offset(page); |
| 2505 | |
| 2506 | ret = extract_ordered_extent(BTRFS_I(inode), bio, file_offset); |
| 2507 | if (ret) |
| 2508 | goto out; |
| 2509 | } |
| 2510 | |
Naohiro Aota | cfe9444 | 2021-02-04 19:21:59 +0900 | [diff] [blame] | 2511 | if (btrfs_op(bio) != BTRFS_MAP_WRITE) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2512 | ret = btrfs_bio_wq_end_io(fs_info, bio, metadata); |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2513 | if (ret) |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 2514 | goto out; |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2515 | |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 2516 | if (bio_flags & EXTENT_BIO_COMPRESSED) { |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 2517 | ret = btrfs_submit_compressed_read(inode, bio, |
| 2518 | mirror_num, |
| 2519 | bio_flags); |
| 2520 | goto out; |
Josef Bacik | 334c16d | 2020-10-16 11:29:14 -0400 | [diff] [blame] | 2521 | } else { |
| 2522 | /* |
| 2523 | * Lookup bio sums does extra checks around whether we |
| 2524 | * need to csum or not, which is why we ignore skip_sum |
| 2525 | * here. |
| 2526 | */ |
Qu Wenruo | 6275193 | 2020-12-02 14:48:06 +0800 | [diff] [blame] | 2527 | ret = btrfs_lookup_bio_sums(inode, bio, NULL); |
Tsutomu Itoh | c2db107 | 2011-03-01 06:48:31 +0000 | [diff] [blame] | 2528 | if (ret) |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 2529 | goto out; |
Tsutomu Itoh | c2db107 | 2011-03-01 06:48:31 +0000 | [diff] [blame] | 2530 | } |
Chris Mason | 4d1b5fb | 2008-08-20 09:44:52 -0400 | [diff] [blame] | 2531 | goto mapit; |
Josef Bacik | b812ce2 | 2012-11-16 13:56:32 -0500 | [diff] [blame] | 2532 | } else if (async && !skip_sum) { |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 2533 | /* csum items have already been cloned */ |
| 2534 | if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID) |
| 2535 | goto mapit; |
Chris Mason | 19b9bdb | 2008-10-30 14:23:13 -0400 | [diff] [blame] | 2536 | /* we're doing a write, do the async checksumming */ |
Qu Wenruo | 8896a08 | 2020-10-21 14:24:53 +0800 | [diff] [blame] | 2537 | ret = btrfs_wq_submit_bio(inode, bio, mirror_num, bio_flags, |
| 2538 | 0, btrfs_submit_bio_start); |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 2539 | goto out; |
Josef Bacik | b812ce2 | 2012-11-16 13:56:32 -0500 | [diff] [blame] | 2540 | } else if (!skip_sum) { |
Nikolay Borisov | bd242a0 | 2020-06-03 08:55:07 +0300 | [diff] [blame] | 2541 | ret = btrfs_csum_one_bio(BTRFS_I(inode), bio, 0, 0); |
Josef Bacik | b812ce2 | 2012-11-16 13:56:32 -0500 | [diff] [blame] | 2542 | if (ret) |
| 2543 | goto out; |
Chris Mason | 19b9bdb | 2008-10-30 14:23:13 -0400 | [diff] [blame] | 2544 | } |
| 2545 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2546 | mapit: |
Chris Mason | 08635ba | 2019-07-10 12:28:14 -0700 | [diff] [blame] | 2547 | ret = btrfs_map_bio(fs_info, bio, mirror_num); |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 2548 | |
| 2549 | out: |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 2550 | if (ret) { |
| 2551 | bio->bi_status = ret; |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 2552 | bio_endio(bio); |
| 2553 | } |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 2554 | return ret; |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 2555 | } |
Chris Mason | 6885f30 | 2008-02-20 16:11:05 -0500 | [diff] [blame] | 2556 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2557 | /* |
| 2558 | * given a list of ordered sums record them in the inode. This happens |
| 2559 | * at IO completion time based on sums calculated at bio submission time. |
| 2560 | */ |
Nikolay Borisov | 510f85e | 2020-09-18 12:15:52 +0300 | [diff] [blame] | 2561 | static int add_pending_csums(struct btrfs_trans_handle *trans, |
| 2562 | struct list_head *list) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2563 | { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2564 | struct btrfs_ordered_sum *sum; |
Nikolay Borisov | ac01f26 | 2018-01-08 10:59:43 +0200 | [diff] [blame] | 2565 | int ret; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2566 | |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 2567 | list_for_each_entry(sum, list, list) { |
David Sterba | 7c2871a | 2017-11-08 01:07:43 +0100 | [diff] [blame] | 2568 | trans->adding_csums = true; |
Nikolay Borisov | 510f85e | 2020-09-18 12:15:52 +0300 | [diff] [blame] | 2569 | ret = btrfs_csum_file_blocks(trans, trans->fs_info->csum_root, sum); |
David Sterba | 7c2871a | 2017-11-08 01:07:43 +0100 | [diff] [blame] | 2570 | trans->adding_csums = false; |
Nikolay Borisov | ac01f26 | 2018-01-08 10:59:43 +0200 | [diff] [blame] | 2571 | if (ret) |
| 2572 | return ret; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2573 | } |
| 2574 | return 0; |
| 2575 | } |
| 2576 | |
Filipe Manana | c334730 | 2020-11-04 11:07:31 +0000 | [diff] [blame] | 2577 | static int btrfs_find_new_delalloc_bytes(struct btrfs_inode *inode, |
| 2578 | const u64 start, |
| 2579 | const u64 len, |
| 2580 | struct extent_state **cached_state) |
| 2581 | { |
| 2582 | u64 search_start = start; |
| 2583 | const u64 end = start + len - 1; |
| 2584 | |
| 2585 | while (search_start < end) { |
| 2586 | const u64 search_len = end - search_start + 1; |
| 2587 | struct extent_map *em; |
| 2588 | u64 em_len; |
| 2589 | int ret = 0; |
| 2590 | |
| 2591 | em = btrfs_get_extent(inode, NULL, 0, search_start, search_len); |
| 2592 | if (IS_ERR(em)) |
| 2593 | return PTR_ERR(em); |
| 2594 | |
| 2595 | if (em->block_start != EXTENT_MAP_HOLE) |
| 2596 | goto next; |
| 2597 | |
| 2598 | em_len = em->len; |
| 2599 | if (em->start < search_start) |
| 2600 | em_len -= search_start - em->start; |
| 2601 | if (em_len > search_len) |
| 2602 | em_len = search_len; |
| 2603 | |
| 2604 | ret = set_extent_bit(&inode->io_tree, search_start, |
| 2605 | search_start + em_len - 1, |
Nikolay Borisov | 1cab5e7 | 2020-11-05 11:08:00 +0200 | [diff] [blame] | 2606 | EXTENT_DELALLOC_NEW, 0, NULL, cached_state, |
| 2607 | GFP_NOFS, NULL); |
Filipe Manana | c334730 | 2020-11-04 11:07:31 +0000 | [diff] [blame] | 2608 | next: |
| 2609 | search_start = extent_map_end(em); |
| 2610 | free_extent_map(em); |
| 2611 | if (ret) |
| 2612 | return ret; |
| 2613 | } |
| 2614 | return 0; |
| 2615 | } |
| 2616 | |
Nikolay Borisov | c2566f2 | 2020-06-03 08:55:35 +0300 | [diff] [blame] | 2617 | int btrfs_set_extent_delalloc(struct btrfs_inode *inode, u64 start, u64 end, |
Filipe Manana | e3b8a48 | 2017-11-04 00:16:59 +0000 | [diff] [blame] | 2618 | unsigned int extra_bits, |
Nikolay Borisov | 330a582 | 2019-07-17 16:18:17 +0300 | [diff] [blame] | 2619 | struct extent_state **cached_state) |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 2620 | { |
Johannes Thumshirn | fdb1e12 | 2018-12-05 15:23:04 +0100 | [diff] [blame] | 2621 | WARN_ON(PAGE_ALIGNED(end)); |
Filipe Manana | c334730 | 2020-11-04 11:07:31 +0000 | [diff] [blame] | 2622 | |
| 2623 | if (start >= i_size_read(&inode->vfs_inode) && |
| 2624 | !(inode->flags & BTRFS_INODE_PREALLOC)) { |
| 2625 | /* |
| 2626 | * There can't be any extents following eof in this case so just |
| 2627 | * set the delalloc new bit for the range directly. |
| 2628 | */ |
| 2629 | extra_bits |= EXTENT_DELALLOC_NEW; |
| 2630 | } else { |
| 2631 | int ret; |
| 2632 | |
| 2633 | ret = btrfs_find_new_delalloc_bytes(inode, start, |
| 2634 | end + 1 - start, |
| 2635 | cached_state); |
| 2636 | if (ret) |
| 2637 | return ret; |
| 2638 | } |
| 2639 | |
Nikolay Borisov | c2566f2 | 2020-06-03 08:55:35 +0300 | [diff] [blame] | 2640 | return set_extent_delalloc(&inode->io_tree, start, end, extra_bits, |
| 2641 | cached_state); |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 2642 | } |
| 2643 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2644 | /* see btrfs_writepage_start_hook for details on why this is required */ |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2645 | struct btrfs_writepage_fixup { |
| 2646 | struct page *page; |
Josef Bacik | f4b1363 | 2020-01-21 14:34:52 -0500 | [diff] [blame] | 2647 | struct inode *inode; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2648 | struct btrfs_work work; |
| 2649 | }; |
| 2650 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 2651 | static void btrfs_writepage_fixup_worker(struct btrfs_work *work) |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2652 | { |
| 2653 | struct btrfs_writepage_fixup *fixup; |
| 2654 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 2655 | struct extent_state *cached_state = NULL; |
Qu Wenruo | 364ecf3 | 2017-02-27 15:10:38 +0800 | [diff] [blame] | 2656 | struct extent_changeset *data_reserved = NULL; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2657 | struct page *page; |
Nikolay Borisov | 65d87f7 | 2020-06-05 10:51:51 +0300 | [diff] [blame] | 2658 | struct btrfs_inode *inode; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2659 | u64 page_start; |
| 2660 | u64 page_end; |
Chris Mason | 25f3c50 | 2020-01-21 11:51:42 -0500 | [diff] [blame] | 2661 | int ret = 0; |
Josef Bacik | f4b1363 | 2020-01-21 14:34:52 -0500 | [diff] [blame] | 2662 | bool free_delalloc_space = true; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2663 | |
| 2664 | fixup = container_of(work, struct btrfs_writepage_fixup, work); |
| 2665 | page = fixup->page; |
Nikolay Borisov | 65d87f7 | 2020-06-05 10:51:51 +0300 | [diff] [blame] | 2666 | inode = BTRFS_I(fixup->inode); |
Josef Bacik | f4b1363 | 2020-01-21 14:34:52 -0500 | [diff] [blame] | 2667 | page_start = page_offset(page); |
| 2668 | page_end = page_offset(page) + PAGE_SIZE - 1; |
| 2669 | |
| 2670 | /* |
| 2671 | * This is similar to page_mkwrite, we need to reserve the space before |
| 2672 | * we take the page lock. |
| 2673 | */ |
Nikolay Borisov | 65d87f7 | 2020-06-05 10:51:51 +0300 | [diff] [blame] | 2674 | ret = btrfs_delalloc_reserve_space(inode, &data_reserved, page_start, |
| 2675 | PAGE_SIZE); |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 2676 | again: |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2677 | lock_page(page); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2678 | |
Chris Mason | 25f3c50 | 2020-01-21 11:51:42 -0500 | [diff] [blame] | 2679 | /* |
| 2680 | * Before we queued this fixup, we took a reference on the page. |
| 2681 | * page->mapping may go NULL, but it shouldn't be moved to a different |
| 2682 | * address space. |
| 2683 | */ |
Josef Bacik | f4b1363 | 2020-01-21 14:34:52 -0500 | [diff] [blame] | 2684 | if (!page->mapping || !PageDirty(page) || !PageChecked(page)) { |
| 2685 | /* |
| 2686 | * Unfortunately this is a little tricky, either |
| 2687 | * |
| 2688 | * 1) We got here and our page had already been dealt with and |
| 2689 | * we reserved our space, thus ret == 0, so we need to just |
| 2690 | * drop our space reservation and bail. This can happen the |
| 2691 | * first time we come into the fixup worker, or could happen |
| 2692 | * while waiting for the ordered extent. |
| 2693 | * 2) Our page was already dealt with, but we happened to get an |
| 2694 | * ENOSPC above from the btrfs_delalloc_reserve_space. In |
| 2695 | * this case we obviously don't have anything to release, but |
| 2696 | * because the page was already dealt with we don't want to |
| 2697 | * mark the page with an error, so make sure we're resetting |
| 2698 | * ret to 0. This is why we have this check _before_ the ret |
| 2699 | * check, because we do not want to have a surprise ENOSPC |
| 2700 | * when the page was already properly dealt with. |
| 2701 | */ |
| 2702 | if (!ret) { |
Nikolay Borisov | 65d87f7 | 2020-06-05 10:51:51 +0300 | [diff] [blame] | 2703 | btrfs_delalloc_release_extents(inode, PAGE_SIZE); |
| 2704 | btrfs_delalloc_release_space(inode, data_reserved, |
Josef Bacik | f4b1363 | 2020-01-21 14:34:52 -0500 | [diff] [blame] | 2705 | page_start, PAGE_SIZE, |
| 2706 | true); |
| 2707 | } |
| 2708 | ret = 0; |
Chris Mason | 25f3c50 | 2020-01-21 11:51:42 -0500 | [diff] [blame] | 2709 | goto out_page; |
Josef Bacik | f4b1363 | 2020-01-21 14:34:52 -0500 | [diff] [blame] | 2710 | } |
Chris Mason | 25f3c50 | 2020-01-21 11:51:42 -0500 | [diff] [blame] | 2711 | |
| 2712 | /* |
Josef Bacik | f4b1363 | 2020-01-21 14:34:52 -0500 | [diff] [blame] | 2713 | * We can't mess with the page state unless it is locked, so now that |
| 2714 | * it is locked bail if we failed to make our space reservation. |
Chris Mason | 25f3c50 | 2020-01-21 11:51:42 -0500 | [diff] [blame] | 2715 | */ |
Josef Bacik | f4b1363 | 2020-01-21 14:34:52 -0500 | [diff] [blame] | 2716 | if (ret) |
| 2717 | goto out_page; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2718 | |
Nikolay Borisov | 65d87f7 | 2020-06-05 10:51:51 +0300 | [diff] [blame] | 2719 | lock_extent_bits(&inode->io_tree, page_start, page_end, &cached_state); |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 2720 | |
| 2721 | /* already ordered? We're done */ |
Qu Wenruo | f57ad93 | 2021-04-07 19:22:13 +0800 | [diff] [blame] | 2722 | if (PageOrdered(page)) |
Josef Bacik | f4b1363 | 2020-01-21 14:34:52 -0500 | [diff] [blame] | 2723 | goto out_reserved; |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 2724 | |
Nikolay Borisov | 65d87f7 | 2020-06-05 10:51:51 +0300 | [diff] [blame] | 2725 | ordered = btrfs_lookup_ordered_range(inode, page_start, PAGE_SIZE); |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 2726 | if (ordered) { |
Nikolay Borisov | 65d87f7 | 2020-06-05 10:51:51 +0300 | [diff] [blame] | 2727 | unlock_extent_cached(&inode->io_tree, page_start, page_end, |
| 2728 | &cached_state); |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 2729 | unlock_page(page); |
Nikolay Borisov | c0a4360 | 2020-09-18 12:15:53 +0300 | [diff] [blame] | 2730 | btrfs_start_ordered_extent(ordered, 1); |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 2731 | btrfs_put_ordered_extent(ordered); |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 2732 | goto again; |
| 2733 | } |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2734 | |
Nikolay Borisov | 65d87f7 | 2020-06-05 10:51:51 +0300 | [diff] [blame] | 2735 | ret = btrfs_set_extent_delalloc(inode, page_start, page_end, 0, |
Nikolay Borisov | 330a582 | 2019-07-17 16:18:17 +0300 | [diff] [blame] | 2736 | &cached_state); |
Chris Mason | 25f3c50 | 2020-01-21 11:51:42 -0500 | [diff] [blame] | 2737 | if (ret) |
Filipe Manana | 5368700 | 2019-10-09 17:43:59 +0100 | [diff] [blame] | 2738 | goto out_reserved; |
Nikolay Borisov | f3038ee | 2017-12-05 09:29:19 +0200 | [diff] [blame] | 2739 | |
Chris Mason | 25f3c50 | 2020-01-21 11:51:42 -0500 | [diff] [blame] | 2740 | /* |
| 2741 | * Everything went as planned, we're now the owner of a dirty page with |
| 2742 | * delayed allocation bits set and space reserved for our COW |
| 2743 | * destination. |
| 2744 | * |
| 2745 | * The page was dirty when we started, nothing should have cleaned it. |
| 2746 | */ |
| 2747 | BUG_ON(!PageDirty(page)); |
Josef Bacik | f4b1363 | 2020-01-21 14:34:52 -0500 | [diff] [blame] | 2748 | free_delalloc_space = false; |
Filipe Manana | 5368700 | 2019-10-09 17:43:59 +0100 | [diff] [blame] | 2749 | out_reserved: |
Nikolay Borisov | 65d87f7 | 2020-06-05 10:51:51 +0300 | [diff] [blame] | 2750 | btrfs_delalloc_release_extents(inode, PAGE_SIZE); |
Josef Bacik | f4b1363 | 2020-01-21 14:34:52 -0500 | [diff] [blame] | 2751 | if (free_delalloc_space) |
Nikolay Borisov | 65d87f7 | 2020-06-05 10:51:51 +0300 | [diff] [blame] | 2752 | btrfs_delalloc_release_space(inode, data_reserved, page_start, |
| 2753 | PAGE_SIZE, true); |
| 2754 | unlock_extent_cached(&inode->io_tree, page_start, page_end, |
David Sterba | e43bbe5 | 2017-12-12 21:43:52 +0100 | [diff] [blame] | 2755 | &cached_state); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2756 | out_page: |
Chris Mason | 25f3c50 | 2020-01-21 11:51:42 -0500 | [diff] [blame] | 2757 | if (ret) { |
| 2758 | /* |
| 2759 | * We hit ENOSPC or other errors. Update the mapping and page |
| 2760 | * to reflect the errors and clean the page. |
| 2761 | */ |
| 2762 | mapping_set_error(page->mapping, ret); |
| 2763 | end_extent_writepage(page, ret, page_start, page_end); |
| 2764 | clear_page_dirty_for_io(page); |
| 2765 | SetPageError(page); |
| 2766 | } |
| 2767 | ClearPageChecked(page); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2768 | unlock_page(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2769 | put_page(page); |
Miao Xie | b897abe | 2011-01-26 16:19:22 +0800 | [diff] [blame] | 2770 | kfree(fixup); |
Qu Wenruo | 364ecf3 | 2017-02-27 15:10:38 +0800 | [diff] [blame] | 2771 | extent_changeset_free(data_reserved); |
Josef Bacik | f4b1363 | 2020-01-21 14:34:52 -0500 | [diff] [blame] | 2772 | /* |
| 2773 | * As a precaution, do a delayed iput in case it would be the last iput |
| 2774 | * that could need flushing space. Recursing back to fixup worker would |
| 2775 | * deadlock. |
| 2776 | */ |
Nikolay Borisov | 65d87f7 | 2020-06-05 10:51:51 +0300 | [diff] [blame] | 2777 | btrfs_add_delayed_iput(&inode->vfs_inode); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2778 | } |
| 2779 | |
| 2780 | /* |
| 2781 | * There are a few paths in the higher layers of the kernel that directly |
| 2782 | * set the page dirty bit without asking the filesystem if it is a |
| 2783 | * good idea. This causes problems because we want to make sure COW |
| 2784 | * properly happens and the data=ordered rules are followed. |
| 2785 | * |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2786 | * In our case any range that doesn't have the ORDERED bit set |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2787 | * hasn't been properly setup for IO. We kick off an async process |
| 2788 | * to fix it up. The async helper will wait for ordered extents, set |
| 2789 | * the delalloc bit and make it safe to write the page. |
| 2790 | */ |
Qu Wenruo | a129ffb | 2021-07-27 13:41:32 +0800 | [diff] [blame] | 2791 | int btrfs_writepage_cow_fixup(struct page *page) |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2792 | { |
| 2793 | struct inode *inode = page->mapping->host; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2794 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2795 | struct btrfs_writepage_fixup *fixup; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2796 | |
Qu Wenruo | f57ad93 | 2021-04-07 19:22:13 +0800 | [diff] [blame] | 2797 | /* This page has ordered extent covering it already */ |
| 2798 | if (PageOrdered(page)) |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2799 | return 0; |
| 2800 | |
Chris Mason | 25f3c50 | 2020-01-21 11:51:42 -0500 | [diff] [blame] | 2801 | /* |
| 2802 | * PageChecked is set below when we create a fixup worker for this page, |
| 2803 | * don't try to create another one if we're already PageChecked() |
| 2804 | * |
| 2805 | * The extent_io writepage code will redirty the page if we send back |
| 2806 | * EAGAIN. |
| 2807 | */ |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2808 | if (PageChecked(page)) |
| 2809 | return -EAGAIN; |
| 2810 | |
| 2811 | fixup = kzalloc(sizeof(*fixup), GFP_NOFS); |
| 2812 | if (!fixup) |
| 2813 | return -EAGAIN; |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 2814 | |
Josef Bacik | f4b1363 | 2020-01-21 14:34:52 -0500 | [diff] [blame] | 2815 | /* |
| 2816 | * We are already holding a reference to this inode from |
| 2817 | * write_cache_pages. We need to hold it because the space reservation |
| 2818 | * takes place outside of the page lock, and we can't trust |
| 2819 | * page->mapping outside of the page lock. |
| 2820 | */ |
| 2821 | ihold(inode); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2822 | SetPageChecked(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2823 | get_page(page); |
Omar Sandoval | a0cac0e | 2019-09-16 11:30:57 -0700 | [diff] [blame] | 2824 | btrfs_init_work(&fixup->work, btrfs_writepage_fixup_worker, NULL, NULL); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2825 | fixup->page = page; |
Josef Bacik | f4b1363 | 2020-01-21 14:34:52 -0500 | [diff] [blame] | 2826 | fixup->inode = inode; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2827 | btrfs_queue_work(fs_info->fixup_workers, &fixup->work); |
Chris Mason | 25f3c50 | 2020-01-21 11:51:42 -0500 | [diff] [blame] | 2828 | |
| 2829 | return -EAGAIN; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2830 | } |
| 2831 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2832 | static int insert_reserved_file_extent(struct btrfs_trans_handle *trans, |
Nikolay Borisov | c553f94 | 2020-06-03 08:55:19 +0300 | [diff] [blame] | 2833 | struct btrfs_inode *inode, u64 file_pos, |
Qu Wenruo | 9729f10 | 2020-06-10 09:04:41 +0800 | [diff] [blame] | 2834 | struct btrfs_file_extent_item *stack_fi, |
Filipe Manana | 2766ff6 | 2020-11-04 11:07:34 +0000 | [diff] [blame] | 2835 | const bool update_inode_bytes, |
Qu Wenruo | 9729f10 | 2020-06-10 09:04:41 +0800 | [diff] [blame] | 2836 | u64 qgroup_reserved) |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2837 | { |
Nikolay Borisov | c553f94 | 2020-06-03 08:55:19 +0300 | [diff] [blame] | 2838 | struct btrfs_root *root = inode->root; |
Filipe Manana | 2766ff6 | 2020-11-04 11:07:34 +0000 | [diff] [blame] | 2839 | const u64 sectorsize = root->fs_info->sectorsize; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2840 | struct btrfs_path *path; |
| 2841 | struct extent_buffer *leaf; |
| 2842 | struct btrfs_key ins; |
Qu Wenruo | 203f44c5 | 2020-06-10 09:04:40 +0800 | [diff] [blame] | 2843 | u64 disk_num_bytes = btrfs_stack_file_extent_disk_num_bytes(stack_fi); |
| 2844 | u64 disk_bytenr = btrfs_stack_file_extent_disk_bytenr(stack_fi); |
| 2845 | u64 num_bytes = btrfs_stack_file_extent_num_bytes(stack_fi); |
| 2846 | u64 ram_bytes = btrfs_stack_file_extent_ram_bytes(stack_fi); |
Filipe Manana | 5893dfb | 2020-11-04 11:07:32 +0000 | [diff] [blame] | 2847 | struct btrfs_drop_extents_args drop_args = { 0 }; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2848 | int ret; |
| 2849 | |
| 2850 | path = btrfs_alloc_path(); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 2851 | if (!path) |
| 2852 | return -ENOMEM; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2853 | |
Chris Mason | a1ed835 | 2009-09-11 12:27:37 -0400 | [diff] [blame] | 2854 | /* |
| 2855 | * we may be replacing one extent in the tree with another. |
| 2856 | * The new extent is pinned in the extent map, and we don't want |
| 2857 | * to drop it from the cache until it is completely in the btree. |
| 2858 | * |
| 2859 | * So, tell btrfs_drop_extents to leave this extent in the cache. |
| 2860 | * the caller is expected to unpin it and allow it to be merged |
| 2861 | * with the others. |
| 2862 | */ |
Filipe Manana | 5893dfb | 2020-11-04 11:07:32 +0000 | [diff] [blame] | 2863 | drop_args.path = path; |
| 2864 | drop_args.start = file_pos; |
| 2865 | drop_args.end = file_pos + num_bytes; |
| 2866 | drop_args.replace_extent = true; |
| 2867 | drop_args.extent_item_size = sizeof(*stack_fi); |
| 2868 | ret = btrfs_drop_extents(trans, root, inode, &drop_args); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2869 | if (ret) |
| 2870 | goto out; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2871 | |
Filipe Manana | 5893dfb | 2020-11-04 11:07:32 +0000 | [diff] [blame] | 2872 | if (!drop_args.extent_inserted) { |
Nikolay Borisov | c553f94 | 2020-06-03 08:55:19 +0300 | [diff] [blame] | 2873 | ins.objectid = btrfs_ino(inode); |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 2874 | ins.offset = file_pos; |
| 2875 | ins.type = BTRFS_EXTENT_DATA_KEY; |
| 2876 | |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 2877 | ret = btrfs_insert_empty_item(trans, root, path, &ins, |
Qu Wenruo | 203f44c5 | 2020-06-10 09:04:40 +0800 | [diff] [blame] | 2878 | sizeof(*stack_fi)); |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 2879 | if (ret) |
| 2880 | goto out; |
| 2881 | } |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2882 | leaf = path->nodes[0]; |
Qu Wenruo | 203f44c5 | 2020-06-10 09:04:40 +0800 | [diff] [blame] | 2883 | btrfs_set_stack_file_extent_generation(stack_fi, trans->transid); |
| 2884 | write_extent_buffer(leaf, stack_fi, |
| 2885 | btrfs_item_ptr_offset(leaf, path->slots[0]), |
| 2886 | sizeof(struct btrfs_file_extent_item)); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 2887 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2888 | btrfs_mark_buffer_dirty(leaf); |
Josef Bacik | ce19533 | 2012-09-25 15:26:16 -0400 | [diff] [blame] | 2889 | btrfs_release_path(path); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2890 | |
Filipe Manana | 2766ff6 | 2020-11-04 11:07:34 +0000 | [diff] [blame] | 2891 | /* |
| 2892 | * If we dropped an inline extent here, we know the range where it is |
| 2893 | * was not marked with the EXTENT_DELALLOC_NEW bit, so we update the |
David Sterba | 1a9fd41 | 2021-05-21 17:42:23 +0200 | [diff] [blame] | 2894 | * number of bytes only for that range containing the inline extent. |
Filipe Manana | 2766ff6 | 2020-11-04 11:07:34 +0000 | [diff] [blame] | 2895 | * The remaining of the range will be processed when clearning the |
| 2896 | * EXTENT_DELALLOC_BIT bit through the ordered extent completion. |
| 2897 | */ |
| 2898 | if (file_pos == 0 && !IS_ALIGNED(drop_args.bytes_found, sectorsize)) { |
| 2899 | u64 inline_size = round_down(drop_args.bytes_found, sectorsize); |
| 2900 | |
| 2901 | inline_size = drop_args.bytes_found - inline_size; |
| 2902 | btrfs_update_inode_bytes(inode, sectorsize, inline_size); |
| 2903 | drop_args.bytes_found -= inline_size; |
| 2904 | num_bytes -= sectorsize; |
| 2905 | } |
| 2906 | |
| 2907 | if (update_inode_bytes) |
| 2908 | btrfs_update_inode_bytes(inode, num_bytes, drop_args.bytes_found); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2909 | |
| 2910 | ins.objectid = disk_bytenr; |
| 2911 | ins.offset = disk_num_bytes; |
| 2912 | ins.type = BTRFS_EXTENT_ITEM_KEY; |
Qu Wenruo | a12b877 | 2017-02-27 15:10:37 +0800 | [diff] [blame] | 2913 | |
Nikolay Borisov | c553f94 | 2020-06-03 08:55:19 +0300 | [diff] [blame] | 2914 | ret = btrfs_inode_set_file_extent_range(inode, file_pos, ram_bytes); |
Josef Bacik | 9ddc959 | 2020-01-17 09:02:22 -0500 | [diff] [blame] | 2915 | if (ret) |
| 2916 | goto out; |
| 2917 | |
Nikolay Borisov | c553f94 | 2020-06-03 08:55:19 +0300 | [diff] [blame] | 2918 | ret = btrfs_alloc_reserved_file_extent(trans, root, btrfs_ino(inode), |
Qu Wenruo | 9729f10 | 2020-06-10 09:04:41 +0800 | [diff] [blame] | 2919 | file_pos, qgroup_reserved, &ins); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2920 | out: |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2921 | btrfs_free_path(path); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 2922 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2923 | return ret; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2924 | } |
| 2925 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2926 | static void btrfs_release_delalloc_bytes(struct btrfs_fs_info *fs_info, |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 2927 | u64 start, u64 len) |
| 2928 | { |
David Sterba | 32da5386 | 2019-10-29 19:20:18 +0100 | [diff] [blame] | 2929 | struct btrfs_block_group *cache; |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 2930 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2931 | cache = btrfs_lookup_block_group(fs_info, start); |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 2932 | ASSERT(cache); |
| 2933 | |
| 2934 | spin_lock(&cache->lock); |
| 2935 | cache->delalloc_bytes -= len; |
| 2936 | spin_unlock(&cache->lock); |
| 2937 | |
| 2938 | btrfs_put_block_group(cache); |
| 2939 | } |
| 2940 | |
Qu Wenruo | 203f44c5 | 2020-06-10 09:04:40 +0800 | [diff] [blame] | 2941 | static int insert_ordered_extent_file_extent(struct btrfs_trans_handle *trans, |
Qu Wenruo | 203f44c5 | 2020-06-10 09:04:40 +0800 | [diff] [blame] | 2942 | struct btrfs_ordered_extent *oe) |
| 2943 | { |
| 2944 | struct btrfs_file_extent_item stack_fi; |
| 2945 | u64 logical_len; |
Filipe Manana | 2766ff6 | 2020-11-04 11:07:34 +0000 | [diff] [blame] | 2946 | bool update_inode_bytes; |
Qu Wenruo | 203f44c5 | 2020-06-10 09:04:40 +0800 | [diff] [blame] | 2947 | |
| 2948 | memset(&stack_fi, 0, sizeof(stack_fi)); |
| 2949 | btrfs_set_stack_file_extent_type(&stack_fi, BTRFS_FILE_EXTENT_REG); |
| 2950 | btrfs_set_stack_file_extent_disk_bytenr(&stack_fi, oe->disk_bytenr); |
| 2951 | btrfs_set_stack_file_extent_disk_num_bytes(&stack_fi, |
| 2952 | oe->disk_num_bytes); |
| 2953 | if (test_bit(BTRFS_ORDERED_TRUNCATED, &oe->flags)) |
| 2954 | logical_len = oe->truncated_len; |
| 2955 | else |
| 2956 | logical_len = oe->num_bytes; |
| 2957 | btrfs_set_stack_file_extent_num_bytes(&stack_fi, logical_len); |
| 2958 | btrfs_set_stack_file_extent_ram_bytes(&stack_fi, logical_len); |
| 2959 | btrfs_set_stack_file_extent_compression(&stack_fi, oe->compress_type); |
| 2960 | /* Encryption and other encoding is reserved and all 0 */ |
| 2961 | |
Filipe Manana | 2766ff6 | 2020-11-04 11:07:34 +0000 | [diff] [blame] | 2962 | /* |
| 2963 | * For delalloc, when completing an ordered extent we update the inode's |
| 2964 | * bytes when clearing the range in the inode's io tree, so pass false |
| 2965 | * as the argument 'update_inode_bytes' to insert_reserved_file_extent(), |
| 2966 | * except if the ordered extent was truncated. |
| 2967 | */ |
| 2968 | update_inode_bytes = test_bit(BTRFS_ORDERED_DIRECT, &oe->flags) || |
| 2969 | test_bit(BTRFS_ORDERED_TRUNCATED, &oe->flags); |
| 2970 | |
Nikolay Borisov | 3c38c87 | 2020-09-18 12:15:51 +0300 | [diff] [blame] | 2971 | return insert_reserved_file_extent(trans, BTRFS_I(oe->inode), |
| 2972 | oe->file_offset, &stack_fi, |
Filipe Manana | 2766ff6 | 2020-11-04 11:07:34 +0000 | [diff] [blame] | 2973 | update_inode_bytes, oe->qgroup_rsv); |
Qu Wenruo | 203f44c5 | 2020-06-10 09:04:40 +0800 | [diff] [blame] | 2974 | } |
| 2975 | |
| 2976 | /* |
| 2977 | * As ordered data IO finishes, this gets called so we can finish |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2978 | * an ordered extent if the range of bytes in the file it covers are |
| 2979 | * fully written. |
| 2980 | */ |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2981 | static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2982 | { |
Nikolay Borisov | 72e7e6e | 2020-11-02 16:48:56 +0200 | [diff] [blame] | 2983 | struct btrfs_inode *inode = BTRFS_I(ordered_extent->inode); |
| 2984 | struct btrfs_root *root = inode->root; |
| 2985 | struct btrfs_fs_info *fs_info = root->fs_info; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 2986 | struct btrfs_trans_handle *trans = NULL; |
Nikolay Borisov | 72e7e6e | 2020-11-02 16:48:56 +0200 | [diff] [blame] | 2987 | struct extent_io_tree *io_tree = &inode->io_tree; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 2988 | struct extent_state *cached_state = NULL; |
Omar Sandoval | bffe633 | 2019-12-02 17:34:19 -0800 | [diff] [blame] | 2989 | u64 start, end; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 2990 | int compress_type = 0; |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 2991 | int ret = 0; |
Omar Sandoval | bffe633 | 2019-12-02 17:34:19 -0800 | [diff] [blame] | 2992 | u64 logical_len = ordered_extent->num_bytes; |
Nikolay Borisov | 8d51012 | 2019-10-08 20:43:06 +0300 | [diff] [blame] | 2993 | bool freespace_inode; |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 2994 | bool truncated = false; |
Josef Bacik | 49940bd | 2018-10-11 15:54:21 -0400 | [diff] [blame] | 2995 | bool clear_reserved_extent = true; |
Filipe Manana | 2766ff6 | 2020-11-04 11:07:34 +0000 | [diff] [blame] | 2996 | unsigned int clear_bits = EXTENT_DEFRAG; |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 2997 | |
Omar Sandoval | bffe633 | 2019-12-02 17:34:19 -0800 | [diff] [blame] | 2998 | start = ordered_extent->file_offset; |
| 2999 | end = start + ordered_extent->num_bytes - 1; |
| 3000 | |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 3001 | if (!test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) && |
| 3002 | !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags) && |
| 3003 | !test_bit(BTRFS_ORDERED_DIRECT, &ordered_extent->flags)) |
Filipe Manana | 2766ff6 | 2020-11-04 11:07:34 +0000 | [diff] [blame] | 3004 | clear_bits |= EXTENT_DELALLOC_NEW; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3005 | |
Nikolay Borisov | 72e7e6e | 2020-11-02 16:48:56 +0200 | [diff] [blame] | 3006 | freespace_inode = btrfs_is_free_space_inode(inode); |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 3007 | |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 3008 | if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) { |
| 3009 | ret = -EIO; |
| 3010 | goto out; |
| 3011 | } |
| 3012 | |
Christoph Hellwig | c7c3a6d | 2021-07-22 09:53:59 +0200 | [diff] [blame] | 3013 | if (ordered_extent->bdev) |
Naohiro Aota | d8e3fb1 | 2021-02-04 19:22:05 +0900 | [diff] [blame] | 3014 | btrfs_rewrite_logical_zoned(ordered_extent); |
| 3015 | |
Nikolay Borisov | 72e7e6e | 2020-11-02 16:48:56 +0200 | [diff] [blame] | 3016 | btrfs_free_io_failure_record(inode, start, end); |
Miao Xie | f612496 | 2014-09-12 18:44:04 +0800 | [diff] [blame] | 3017 | |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 3018 | if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) { |
| 3019 | truncated = true; |
| 3020 | logical_len = ordered_extent->truncated_len; |
| 3021 | /* Truncated the entire extent, don't bother adding */ |
| 3022 | if (!logical_len) |
| 3023 | goto out; |
| 3024 | } |
| 3025 | |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 3026 | if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) { |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3027 | BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */ |
Qu Wenruo | 94ed938 | 2015-09-08 17:25:56 +0800 | [diff] [blame] | 3028 | |
Nikolay Borisov | 72e7e6e | 2020-11-02 16:48:56 +0200 | [diff] [blame] | 3029 | btrfs_inode_safe_disk_i_size_write(inode, 0); |
Nikolay Borisov | 8d51012 | 2019-10-08 20:43:06 +0300 | [diff] [blame] | 3030 | if (freespace_inode) |
| 3031 | trans = btrfs_join_transaction_spacecache(root); |
Josef Bacik | 6c760c0 | 2012-11-09 10:53:21 -0500 | [diff] [blame] | 3032 | else |
| 3033 | trans = btrfs_join_transaction(root); |
| 3034 | if (IS_ERR(trans)) { |
| 3035 | ret = PTR_ERR(trans); |
| 3036 | trans = NULL; |
| 3037 | goto out; |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 3038 | } |
Nikolay Borisov | 72e7e6e | 2020-11-02 16:48:56 +0200 | [diff] [blame] | 3039 | trans->block_rsv = &inode->block_rsv; |
Nikolay Borisov | 729f796 | 2020-11-02 16:49:06 +0200 | [diff] [blame] | 3040 | ret = btrfs_update_inode_fallback(trans, root, inode); |
Josef Bacik | 6c760c0 | 2012-11-09 10:53:21 -0500 | [diff] [blame] | 3041 | if (ret) /* -ENOMEM or corruption */ |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3042 | btrfs_abort_transaction(trans, ret); |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 3043 | goto out; |
| 3044 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3045 | |
Filipe Manana | 2766ff6 | 2020-11-04 11:07:34 +0000 | [diff] [blame] | 3046 | clear_bits |= EXTENT_LOCKED; |
Omar Sandoval | bffe633 | 2019-12-02 17:34:19 -0800 | [diff] [blame] | 3047 | lock_extent_bits(io_tree, start, end, &cached_state); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3048 | |
Nikolay Borisov | 8d51012 | 2019-10-08 20:43:06 +0300 | [diff] [blame] | 3049 | if (freespace_inode) |
| 3050 | trans = btrfs_join_transaction_spacecache(root); |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 3051 | else |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 3052 | trans = btrfs_join_transaction(root); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3053 | if (IS_ERR(trans)) { |
| 3054 | ret = PTR_ERR(trans); |
| 3055 | trans = NULL; |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 3056 | goto out; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3057 | } |
Chris Mason | a79b7d4 | 2014-05-22 16:18:52 -0700 | [diff] [blame] | 3058 | |
Nikolay Borisov | 72e7e6e | 2020-11-02 16:48:56 +0200 | [diff] [blame] | 3059 | trans->block_rsv = &inode->block_rsv; |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 3060 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 3061 | if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags)) |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 3062 | compress_type = ordered_extent->compress_type; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 3063 | if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) { |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 3064 | BUG_ON(compress_type); |
Nikolay Borisov | 72e7e6e | 2020-11-02 16:48:56 +0200 | [diff] [blame] | 3065 | ret = btrfs_mark_extent_written(trans, inode, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 3066 | ordered_extent->file_offset, |
| 3067 | ordered_extent->file_offset + |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 3068 | logical_len); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 3069 | } else { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3070 | BUG_ON(root == fs_info->tree_root); |
Nikolay Borisov | 3c38c87 | 2020-09-18 12:15:51 +0300 | [diff] [blame] | 3071 | ret = insert_ordered_extent_file_extent(trans, ordered_extent); |
Josef Bacik | 49940bd | 2018-10-11 15:54:21 -0400 | [diff] [blame] | 3072 | if (!ret) { |
| 3073 | clear_reserved_extent = false; |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3074 | btrfs_release_delalloc_bytes(fs_info, |
Omar Sandoval | bffe633 | 2019-12-02 17:34:19 -0800 | [diff] [blame] | 3075 | ordered_extent->disk_bytenr, |
| 3076 | ordered_extent->disk_num_bytes); |
Josef Bacik | 49940bd | 2018-10-11 15:54:21 -0400 | [diff] [blame] | 3077 | } |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 3078 | } |
Nikolay Borisov | 72e7e6e | 2020-11-02 16:48:56 +0200 | [diff] [blame] | 3079 | unpin_extent_cache(&inode->extent_tree, ordered_extent->file_offset, |
Omar Sandoval | bffe633 | 2019-12-02 17:34:19 -0800 | [diff] [blame] | 3080 | ordered_extent->num_bytes, trans->transid); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3081 | if (ret < 0) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3082 | btrfs_abort_transaction(trans, ret); |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 3083 | goto out; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3084 | } |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 3085 | |
Nikolay Borisov | 510f85e | 2020-09-18 12:15:52 +0300 | [diff] [blame] | 3086 | ret = add_pending_csums(trans, &ordered_extent->list); |
Nikolay Borisov | ac01f26 | 2018-01-08 10:59:43 +0200 | [diff] [blame] | 3087 | if (ret) { |
| 3088 | btrfs_abort_transaction(trans, ret); |
| 3089 | goto out; |
| 3090 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3091 | |
Filipe Manana | 2766ff6 | 2020-11-04 11:07:34 +0000 | [diff] [blame] | 3092 | /* |
| 3093 | * If this is a new delalloc range, clear its new delalloc flag to |
| 3094 | * update the inode's number of bytes. This needs to be done first |
| 3095 | * before updating the inode item. |
| 3096 | */ |
| 3097 | if ((clear_bits & EXTENT_DELALLOC_NEW) && |
| 3098 | !test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) |
Nikolay Borisov | 72e7e6e | 2020-11-02 16:48:56 +0200 | [diff] [blame] | 3099 | clear_extent_bit(&inode->io_tree, start, end, |
Filipe Manana | 2766ff6 | 2020-11-04 11:07:34 +0000 | [diff] [blame] | 3100 | EXTENT_DELALLOC_NEW | EXTENT_ADD_INODE_BYTES, |
| 3101 | 0, 0, &cached_state); |
| 3102 | |
Nikolay Borisov | 72e7e6e | 2020-11-02 16:48:56 +0200 | [diff] [blame] | 3103 | btrfs_inode_safe_disk_i_size_write(inode, 0); |
Nikolay Borisov | 729f796 | 2020-11-02 16:49:06 +0200 | [diff] [blame] | 3104 | ret = btrfs_update_inode_fallback(trans, root, inode); |
Josef Bacik | 6c760c0 | 2012-11-09 10:53:21 -0500 | [diff] [blame] | 3105 | if (ret) { /* -ENOMEM or corruption */ |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3106 | btrfs_abort_transaction(trans, ret); |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 3107 | goto out; |
Josef Bacik | 1ef30be | 2011-04-05 19:25:36 -0400 | [diff] [blame] | 3108 | } |
| 3109 | ret = 0; |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 3110 | out: |
Nikolay Borisov | 72e7e6e | 2020-11-02 16:48:56 +0200 | [diff] [blame] | 3111 | clear_extent_bit(&inode->io_tree, start, end, clear_bits, |
Omar Sandoval | bffe633 | 2019-12-02 17:34:19 -0800 | [diff] [blame] | 3112 | (clear_bits & EXTENT_LOCKED) ? 1 : 0, 0, |
Omar Sandoval | 313facc | 2019-12-02 17:34:18 -0800 | [diff] [blame] | 3113 | &cached_state); |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 3114 | |
Miao Xie | a698d075 | 2012-09-20 01:51:59 -0600 | [diff] [blame] | 3115 | if (trans) |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 3116 | btrfs_end_transaction(trans); |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 3117 | |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 3118 | if (ret || truncated) { |
Omar Sandoval | bffe633 | 2019-12-02 17:34:19 -0800 | [diff] [blame] | 3119 | u64 unwritten_start = start; |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 3120 | |
Josef Bacik | d61bec0 | 2021-05-19 09:38:27 -0400 | [diff] [blame] | 3121 | /* |
| 3122 | * If we failed to finish this ordered extent for any reason we |
| 3123 | * need to make sure BTRFS_ORDERED_IOERR is set on the ordered |
| 3124 | * extent, and mark the inode with the error if it wasn't |
| 3125 | * already set. Any error during writeback would have already |
| 3126 | * set the mapping error, so we need to set it if we're the ones |
| 3127 | * marking this ordered extent as failed. |
| 3128 | */ |
| 3129 | if (ret && !test_and_set_bit(BTRFS_ORDERED_IOERR, |
| 3130 | &ordered_extent->flags)) |
| 3131 | mapping_set_error(ordered_extent->inode->i_mapping, -EIO); |
| 3132 | |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 3133 | if (truncated) |
Omar Sandoval | bffe633 | 2019-12-02 17:34:19 -0800 | [diff] [blame] | 3134 | unwritten_start += logical_len; |
| 3135 | clear_extent_uptodate(io_tree, unwritten_start, end, NULL); |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 3136 | |
| 3137 | /* Drop the cache for the part of the extent we didn't write. */ |
Nikolay Borisov | 72e7e6e | 2020-11-02 16:48:56 +0200 | [diff] [blame] | 3138 | btrfs_drop_extent_cache(inode, unwritten_start, end, 0); |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 3139 | |
Josef Bacik | 0bec9ef | 2013-01-31 14:58:00 -0500 | [diff] [blame] | 3140 | /* |
| 3141 | * If the ordered extent had an IOERR or something else went |
| 3142 | * wrong we need to return the space for this ordered extent |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 3143 | * back to the allocator. We only free the extent in the |
| 3144 | * truncated case if we didn't write out the extent at all. |
Josef Bacik | 49940bd | 2018-10-11 15:54:21 -0400 | [diff] [blame] | 3145 | * |
| 3146 | * If we made it past insert_reserved_file_extent before we |
| 3147 | * errored out then we don't need to do this as the accounting |
| 3148 | * has already been done. |
Josef Bacik | 0bec9ef | 2013-01-31 14:58:00 -0500 | [diff] [blame] | 3149 | */ |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 3150 | if ((ret || !logical_len) && |
Josef Bacik | 49940bd | 2018-10-11 15:54:21 -0400 | [diff] [blame] | 3151 | clear_reserved_extent && |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 3152 | !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) && |
Nikolay Borisov | 4eaaec2 | 2019-11-21 14:03:29 +0200 | [diff] [blame] | 3153 | !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) { |
| 3154 | /* |
| 3155 | * Discard the range before returning it back to the |
| 3156 | * free space pool |
| 3157 | */ |
Dennis Zhou | 46b27f5 | 2019-12-13 16:22:11 -0800 | [diff] [blame] | 3158 | if (ret && btrfs_test_opt(fs_info, DISCARD_SYNC)) |
Nikolay Borisov | 4eaaec2 | 2019-11-21 14:03:29 +0200 | [diff] [blame] | 3159 | btrfs_discard_extent(fs_info, |
Omar Sandoval | bffe633 | 2019-12-02 17:34:19 -0800 | [diff] [blame] | 3160 | ordered_extent->disk_bytenr, |
| 3161 | ordered_extent->disk_num_bytes, |
| 3162 | NULL); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3163 | btrfs_free_reserved_extent(fs_info, |
Omar Sandoval | bffe633 | 2019-12-02 17:34:19 -0800 | [diff] [blame] | 3164 | ordered_extent->disk_bytenr, |
| 3165 | ordered_extent->disk_num_bytes, 1); |
Nikolay Borisov | 4eaaec2 | 2019-11-21 14:03:29 +0200 | [diff] [blame] | 3166 | } |
Josef Bacik | 0bec9ef | 2013-01-31 14:58:00 -0500 | [diff] [blame] | 3167 | } |
| 3168 | |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 3169 | /* |
Liu Bo | 8bad3c0 | 2012-06-18 12:14:23 +0800 | [diff] [blame] | 3170 | * This needs to be done to make sure anybody waiting knows we are done |
| 3171 | * updating everything for this ordered extent. |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 3172 | */ |
Nikolay Borisov | 72e7e6e | 2020-11-02 16:48:56 +0200 | [diff] [blame] | 3173 | btrfs_remove_ordered_extent(inode, ordered_extent); |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 3174 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3175 | /* once for us */ |
| 3176 | btrfs_put_ordered_extent(ordered_extent); |
| 3177 | /* once for the tree */ |
| 3178 | btrfs_put_ordered_extent(ordered_extent); |
| 3179 | |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 3180 | return ret; |
| 3181 | } |
| 3182 | |
| 3183 | static void finish_ordered_fn(struct btrfs_work *work) |
| 3184 | { |
| 3185 | struct btrfs_ordered_extent *ordered_extent; |
| 3186 | ordered_extent = container_of(work, struct btrfs_ordered_extent, work); |
| 3187 | btrfs_finish_ordered_io(ordered_extent); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3188 | } |
| 3189 | |
Qu Wenruo | 38a39ac7 | 2021-04-08 20:32:27 +0800 | [diff] [blame] | 3190 | void btrfs_writepage_endio_finish_ordered(struct btrfs_inode *inode, |
| 3191 | struct page *page, u64 start, |
David Sterba | 25c1252 | 2021-07-26 14:15:08 +0200 | [diff] [blame] | 3192 | u64 end, bool uptodate) |
Chris Mason | 211f90e | 2008-07-18 11:56:15 -0400 | [diff] [blame] | 3193 | { |
Qu Wenruo | 38a39ac7 | 2021-04-08 20:32:27 +0800 | [diff] [blame] | 3194 | trace_btrfs_writepage_end_io_hook(inode, start, end, uptodate); |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 3195 | |
Qu Wenruo | e65f152 | 2021-04-01 15:15:06 +0800 | [diff] [blame] | 3196 | btrfs_mark_ordered_io_finished(inode, page, start, end + 1 - start, |
| 3197 | finish_ordered_fn, uptodate); |
Chris Mason | 211f90e | 2008-07-18 11:56:15 -0400 | [diff] [blame] | 3198 | } |
| 3199 | |
Qu Wenruo | 265d4ac | 2020-10-21 14:24:54 +0800 | [diff] [blame] | 3200 | /* |
| 3201 | * check_data_csum - verify checksum of one sector of uncompressed data |
Qu Wenruo | 7ffd27e | 2020-12-02 14:47:58 +0800 | [diff] [blame] | 3202 | * @inode: inode |
Qu Wenruo | 265d4ac | 2020-10-21 14:24:54 +0800 | [diff] [blame] | 3203 | * @io_bio: btrfs_io_bio which contains the csum |
Qu Wenruo | 7ffd27e | 2020-12-02 14:47:58 +0800 | [diff] [blame] | 3204 | * @bio_offset: offset to the beginning of the bio (in bytes) |
Qu Wenruo | 265d4ac | 2020-10-21 14:24:54 +0800 | [diff] [blame] | 3205 | * @page: page where is the data to be verified |
| 3206 | * @pgoff: offset inside the page |
Omar Sandoval | c1d6abd | 2021-03-16 12:43:00 -0700 | [diff] [blame] | 3207 | * @start: logical offset in the file |
Qu Wenruo | 265d4ac | 2020-10-21 14:24:54 +0800 | [diff] [blame] | 3208 | * |
| 3209 | * The length of such check is always one sector size. |
| 3210 | */ |
Omar Sandoval | 47df776 | 2020-04-16 14:46:17 -0700 | [diff] [blame] | 3211 | static int check_data_csum(struct inode *inode, struct btrfs_io_bio *io_bio, |
Omar Sandoval | c1d6abd | 2021-03-16 12:43:00 -0700 | [diff] [blame] | 3212 | u32 bio_offset, struct page *page, u32 pgoff, |
| 3213 | u64 start) |
Miao Xie | dc380ae | 2014-09-12 18:43:55 +0800 | [diff] [blame] | 3214 | { |
Johannes Thumshirn | d517857 | 2019-06-03 16:58:57 +0200 | [diff] [blame] | 3215 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
| 3216 | SHASH_DESC_ON_STACK(shash, fs_info->csum_shash); |
Miao Xie | dc380ae | 2014-09-12 18:43:55 +0800 | [diff] [blame] | 3217 | char *kaddr; |
Qu Wenruo | 265d4ac | 2020-10-21 14:24:54 +0800 | [diff] [blame] | 3218 | u32 len = fs_info->sectorsize; |
David Sterba | 223486c | 2020-07-02 11:27:30 +0200 | [diff] [blame] | 3219 | const u32 csum_size = fs_info->csum_size; |
Qu Wenruo | 7ffd27e | 2020-12-02 14:47:58 +0800 | [diff] [blame] | 3220 | unsigned int offset_sectors; |
Johannes Thumshirn | d517857 | 2019-06-03 16:58:57 +0200 | [diff] [blame] | 3221 | u8 *csum_expected; |
| 3222 | u8 csum[BTRFS_CSUM_SIZE]; |
Miao Xie | dc380ae | 2014-09-12 18:43:55 +0800 | [diff] [blame] | 3223 | |
Qu Wenruo | 265d4ac | 2020-10-21 14:24:54 +0800 | [diff] [blame] | 3224 | ASSERT(pgoff + len <= PAGE_SIZE); |
| 3225 | |
Qu Wenruo | 7ffd27e | 2020-12-02 14:47:58 +0800 | [diff] [blame] | 3226 | offset_sectors = bio_offset >> fs_info->sectorsize_bits; |
| 3227 | csum_expected = ((u8 *)io_bio->csum) + offset_sectors * csum_size; |
Miao Xie | dc380ae | 2014-09-12 18:43:55 +0800 | [diff] [blame] | 3228 | |
| 3229 | kaddr = kmap_atomic(page); |
Johannes Thumshirn | d517857 | 2019-06-03 16:58:57 +0200 | [diff] [blame] | 3230 | shash->tfm = fs_info->csum_shash; |
| 3231 | |
Eric Biggers | fd08001 | 2020-04-30 23:51:59 -0700 | [diff] [blame] | 3232 | crypto_shash_digest(shash, kaddr + pgoff, len, csum); |
Johannes Thumshirn | d517857 | 2019-06-03 16:58:57 +0200 | [diff] [blame] | 3233 | |
| 3234 | if (memcmp(csum, csum_expected, csum_size)) |
Miao Xie | dc380ae | 2014-09-12 18:43:55 +0800 | [diff] [blame] | 3235 | goto zeroit; |
| 3236 | |
| 3237 | kunmap_atomic(kaddr); |
| 3238 | return 0; |
| 3239 | zeroit: |
Omar Sandoval | c1d6abd | 2021-03-16 12:43:00 -0700 | [diff] [blame] | 3240 | btrfs_print_data_csum_error(BTRFS_I(inode), start, csum, csum_expected, |
| 3241 | io_bio->mirror_num); |
Nikolay Borisov | 814723e | 2020-07-02 15:23:32 +0300 | [diff] [blame] | 3242 | if (io_bio->device) |
| 3243 | btrfs_dev_stat_inc_and_print(io_bio->device, |
| 3244 | BTRFS_DEV_STAT_CORRUPTION_ERRS); |
Miao Xie | dc380ae | 2014-09-12 18:43:55 +0800 | [diff] [blame] | 3245 | memset(kaddr + pgoff, 1, len); |
| 3246 | flush_dcache_page(page); |
| 3247 | kunmap_atomic(kaddr); |
Miao Xie | dc380ae | 2014-09-12 18:43:55 +0800 | [diff] [blame] | 3248 | return -EIO; |
| 3249 | } |
| 3250 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3251 | /* |
Qu Wenruo | 7ffd27e | 2020-12-02 14:47:58 +0800 | [diff] [blame] | 3252 | * When reads are done, we need to check csums to verify the data is correct. |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 3253 | * if there's a match, we allow the bio to finish. If not, the code in |
| 3254 | * extent_io.c will try to find good copies for us. |
Qu Wenruo | 7ffd27e | 2020-12-02 14:47:58 +0800 | [diff] [blame] | 3255 | * |
| 3256 | * @bio_offset: offset to the beginning of the bio (in bytes) |
| 3257 | * @start: file offset of the range start |
| 3258 | * @end: file offset of the range end (inclusive) |
Qu Wenruo | 08508fe | 2021-05-03 10:08:54 +0800 | [diff] [blame] | 3259 | * |
| 3260 | * Return a bitmap where bit set means a csum mismatch, and bit not set means |
| 3261 | * csum match. |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3262 | */ |
Qu Wenruo | 08508fe | 2021-05-03 10:08:54 +0800 | [diff] [blame] | 3263 | unsigned int btrfs_verify_data_csum(struct btrfs_io_bio *io_bio, u32 bio_offset, |
| 3264 | struct page *page, u64 start, u64 end) |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 3265 | { |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 3266 | struct inode *inode = page->mapping->host; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3267 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | ff79f81 | 2007-10-15 16:22:25 -0400 | [diff] [blame] | 3268 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Qu Wenruo | f44cf41 | 2020-12-02 14:47:59 +0800 | [diff] [blame] | 3269 | const u32 sectorsize = root->fs_info->sectorsize; |
| 3270 | u32 pg_off; |
Qu Wenruo | 08508fe | 2021-05-03 10:08:54 +0800 | [diff] [blame] | 3271 | unsigned int result = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3272 | |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 3273 | if (PageChecked(page)) { |
| 3274 | ClearPageChecked(page); |
Miao Xie | dc380ae | 2014-09-12 18:43:55 +0800 | [diff] [blame] | 3275 | return 0; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 3276 | } |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 3277 | |
Qu Wenruo | 3670e64 | 2021-07-26 14:34:51 +0800 | [diff] [blame] | 3278 | /* |
| 3279 | * For subpage case, above PageChecked is not safe as it's not subpage |
| 3280 | * compatible. |
| 3281 | * But for now only cow fixup and compressed read utilize PageChecked |
| 3282 | * flag, while in this context we can easily use io_bio->csum to |
| 3283 | * determine if we really need to do csum verification. |
| 3284 | * |
| 3285 | * So for now, just exit if io_bio->csum is NULL, as it means it's |
| 3286 | * compressed read, and its compressed data csum has already been |
| 3287 | * verified. |
| 3288 | */ |
| 3289 | if (io_bio->csum == NULL) |
| 3290 | return 0; |
| 3291 | |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 3292 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM) |
Miao Xie | dc380ae | 2014-09-12 18:43:55 +0800 | [diff] [blame] | 3293 | return 0; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 3294 | |
Josef Bacik | 42437a6 | 2020-10-16 11:29:18 -0400 | [diff] [blame] | 3295 | if (!root->fs_info->csum_root) |
| 3296 | return 0; |
| 3297 | |
Qu Wenruo | f44cf41 | 2020-12-02 14:47:59 +0800 | [diff] [blame] | 3298 | ASSERT(page_offset(page) <= start && |
| 3299 | end <= page_offset(page) + PAGE_SIZE - 1); |
| 3300 | for (pg_off = offset_in_page(start); |
| 3301 | pg_off < offset_in_page(end); |
| 3302 | pg_off += sectorsize, bio_offset += sectorsize) { |
Qu Wenruo | e3c6232 | 2021-07-26 14:35:04 +0800 | [diff] [blame] | 3303 | u64 file_offset = pg_off + page_offset(page); |
Qu Wenruo | f44cf41 | 2020-12-02 14:47:59 +0800 | [diff] [blame] | 3304 | int ret; |
| 3305 | |
Qu Wenruo | e3c6232 | 2021-07-26 14:35:04 +0800 | [diff] [blame] | 3306 | if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID && |
| 3307 | test_range_bit(io_tree, file_offset, |
| 3308 | file_offset + sectorsize - 1, |
| 3309 | EXTENT_NODATASUM, 1, NULL)) { |
| 3310 | /* Skip the range without csum for data reloc inode */ |
| 3311 | clear_extent_bits(io_tree, file_offset, |
| 3312 | file_offset + sectorsize - 1, |
| 3313 | EXTENT_NODATASUM); |
| 3314 | continue; |
| 3315 | } |
Omar Sandoval | c1d6abd | 2021-03-16 12:43:00 -0700 | [diff] [blame] | 3316 | ret = check_data_csum(inode, io_bio, bio_offset, page, pg_off, |
| 3317 | page_offset(page) + pg_off); |
Qu Wenruo | 08508fe | 2021-05-03 10:08:54 +0800 | [diff] [blame] | 3318 | if (ret < 0) { |
| 3319 | const int nr_bit = (pg_off - offset_in_page(start)) >> |
| 3320 | root->fs_info->sectorsize_bits; |
| 3321 | |
| 3322 | result |= (1U << nr_bit); |
| 3323 | } |
Qu Wenruo | f44cf41 | 2020-12-02 14:47:59 +0800 | [diff] [blame] | 3324 | } |
Qu Wenruo | 08508fe | 2021-05-03 10:08:54 +0800 | [diff] [blame] | 3325 | return result; |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 3326 | } |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3327 | |
Nikolay Borisov | c1c3fac | 2018-01-16 09:31:58 +0200 | [diff] [blame] | 3328 | /* |
| 3329 | * btrfs_add_delayed_iput - perform a delayed iput on @inode |
| 3330 | * |
| 3331 | * @inode: The inode we want to perform iput on |
| 3332 | * |
| 3333 | * This function uses the generic vfs_inode::i_count to track whether we should |
| 3334 | * just decrement it (in case it's > 1) or if this is the last iput then link |
| 3335 | * the inode to the delayed iput machinery. Delayed iputs are processed at |
| 3336 | * transaction commit time/superblock commit/cleaner kthread. |
| 3337 | */ |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 3338 | void btrfs_add_delayed_iput(struct inode *inode) |
| 3339 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3340 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
David Sterba | 8089fe6 | 2015-11-19 14:15:51 +0100 | [diff] [blame] | 3341 | struct btrfs_inode *binode = BTRFS_I(inode); |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 3342 | |
| 3343 | if (atomic_add_unless(&inode->i_count, -1, 1)) |
| 3344 | return; |
| 3345 | |
Josef Bacik | 034f784 | 2018-12-03 11:06:52 -0500 | [diff] [blame] | 3346 | atomic_inc(&fs_info->nr_delayed_iputs); |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 3347 | spin_lock(&fs_info->delayed_iput_lock); |
Nikolay Borisov | c1c3fac | 2018-01-16 09:31:58 +0200 | [diff] [blame] | 3348 | ASSERT(list_empty(&binode->delayed_iput)); |
| 3349 | list_add_tail(&binode->delayed_iput, &fs_info->delayed_iputs); |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 3350 | spin_unlock(&fs_info->delayed_iput_lock); |
Josef Bacik | fd340d0 | 2019-01-11 10:21:02 -0500 | [diff] [blame] | 3351 | if (!test_bit(BTRFS_FS_CLEANER_RUNNING, &fs_info->flags)) |
| 3352 | wake_up_process(fs_info->cleaner_kthread); |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 3353 | } |
| 3354 | |
Josef Bacik | 63611e7 | 2019-06-18 10:59:18 -0400 | [diff] [blame] | 3355 | static void run_delayed_iput_locked(struct btrfs_fs_info *fs_info, |
| 3356 | struct btrfs_inode *inode) |
| 3357 | { |
| 3358 | list_del_init(&inode->delayed_iput); |
| 3359 | spin_unlock(&fs_info->delayed_iput_lock); |
| 3360 | iput(&inode->vfs_inode); |
| 3361 | if (atomic_dec_and_test(&fs_info->nr_delayed_iputs)) |
| 3362 | wake_up(&fs_info->delayed_iputs_wait); |
| 3363 | spin_lock(&fs_info->delayed_iput_lock); |
| 3364 | } |
| 3365 | |
| 3366 | static void btrfs_run_delayed_iput(struct btrfs_fs_info *fs_info, |
| 3367 | struct btrfs_inode *inode) |
| 3368 | { |
| 3369 | if (!list_empty(&inode->delayed_iput)) { |
| 3370 | spin_lock(&fs_info->delayed_iput_lock); |
| 3371 | if (!list_empty(&inode->delayed_iput)) |
| 3372 | run_delayed_iput_locked(fs_info, inode); |
| 3373 | spin_unlock(&fs_info->delayed_iput_lock); |
| 3374 | } |
| 3375 | } |
| 3376 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3377 | void btrfs_run_delayed_iputs(struct btrfs_fs_info *fs_info) |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 3378 | { |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 3379 | |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 3380 | spin_lock(&fs_info->delayed_iput_lock); |
David Sterba | 8089fe6 | 2015-11-19 14:15:51 +0100 | [diff] [blame] | 3381 | while (!list_empty(&fs_info->delayed_iputs)) { |
| 3382 | struct btrfs_inode *inode; |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 3383 | |
David Sterba | 8089fe6 | 2015-11-19 14:15:51 +0100 | [diff] [blame] | 3384 | inode = list_first_entry(&fs_info->delayed_iputs, |
| 3385 | struct btrfs_inode, delayed_iput); |
Josef Bacik | 63611e7 | 2019-06-18 10:59:18 -0400 | [diff] [blame] | 3386 | run_delayed_iput_locked(fs_info, inode); |
Josef Bacik | 71795ee | 2021-04-29 10:51:34 -0400 | [diff] [blame] | 3387 | cond_resched_lock(&fs_info->delayed_iput_lock); |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 3388 | } |
David Sterba | 8089fe6 | 2015-11-19 14:15:51 +0100 | [diff] [blame] | 3389 | spin_unlock(&fs_info->delayed_iput_lock); |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 3390 | } |
| 3391 | |
Josef Bacik | 034f784 | 2018-12-03 11:06:52 -0500 | [diff] [blame] | 3392 | /** |
Nikolay Borisov | 2639631 | 2021-01-22 11:57:59 +0200 | [diff] [blame] | 3393 | * Wait for flushing all delayed iputs |
| 3394 | * |
| 3395 | * @fs_info: the filesystem |
Josef Bacik | 034f784 | 2018-12-03 11:06:52 -0500 | [diff] [blame] | 3396 | * |
| 3397 | * This will wait on any delayed iputs that are currently running with KILLABLE |
| 3398 | * set. Once they are all done running we will return, unless we are killed in |
| 3399 | * which case we return EINTR. This helps in user operations like fallocate etc |
| 3400 | * that might get blocked on the iputs. |
Nikolay Borisov | 2639631 | 2021-01-22 11:57:59 +0200 | [diff] [blame] | 3401 | * |
| 3402 | * Return EINTR if we were killed, 0 if nothing's pending |
Josef Bacik | 034f784 | 2018-12-03 11:06:52 -0500 | [diff] [blame] | 3403 | */ |
| 3404 | int btrfs_wait_on_delayed_iputs(struct btrfs_fs_info *fs_info) |
| 3405 | { |
| 3406 | int ret = wait_event_killable(fs_info->delayed_iputs_wait, |
| 3407 | atomic_read(&fs_info->nr_delayed_iputs) == 0); |
| 3408 | if (ret) |
| 3409 | return -EINTR; |
| 3410 | return 0; |
| 3411 | } |
| 3412 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3413 | /* |
Omar Sandoval | f7e9e8f | 2018-05-11 13:13:32 -0700 | [diff] [blame] | 3414 | * This creates an orphan entry for the given inode in case something goes wrong |
| 3415 | * in the middle of an unlink. |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3416 | */ |
Nikolay Borisov | 73f2e54 | 2017-02-20 13:50:59 +0200 | [diff] [blame] | 3417 | int btrfs_orphan_add(struct btrfs_trans_handle *trans, |
Omar Sandoval | 2791906 | 2018-05-11 13:13:37 -0700 | [diff] [blame] | 3418 | struct btrfs_inode *inode) |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3419 | { |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3420 | int ret; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3421 | |
Omar Sandoval | 2791906 | 2018-05-11 13:13:37 -0700 | [diff] [blame] | 3422 | ret = btrfs_insert_orphan_item(trans, inode->root, btrfs_ino(inode)); |
| 3423 | if (ret && ret != -EEXIST) { |
| 3424 | btrfs_abort_transaction(trans, ret); |
| 3425 | return ret; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3426 | } |
| 3427 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3428 | return 0; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3429 | } |
| 3430 | |
| 3431 | /* |
Omar Sandoval | f7e9e8f | 2018-05-11 13:13:32 -0700 | [diff] [blame] | 3432 | * We have done the delete so we can go ahead and remove the orphan item for |
| 3433 | * this particular inode. |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3434 | */ |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 3435 | static int btrfs_orphan_del(struct btrfs_trans_handle *trans, |
Nikolay Borisov | 3d6ae7b | 2017-02-20 13:50:58 +0200 | [diff] [blame] | 3436 | struct btrfs_inode *inode) |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3437 | { |
Omar Sandoval | 2791906 | 2018-05-11 13:13:37 -0700 | [diff] [blame] | 3438 | return btrfs_del_orphan_item(trans, inode->root, btrfs_ino(inode)); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3439 | } |
| 3440 | |
| 3441 | /* |
| 3442 | * this cleans up any orphans that may be left on the list from the last use |
| 3443 | * of this root. |
| 3444 | */ |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3445 | int btrfs_orphan_cleanup(struct btrfs_root *root) |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3446 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3447 | struct btrfs_fs_info *fs_info = root->fs_info; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3448 | struct btrfs_path *path; |
| 3449 | struct extent_buffer *leaf; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3450 | struct btrfs_key key, found_key; |
| 3451 | struct btrfs_trans_handle *trans; |
| 3452 | struct inode *inode; |
Josef Bacik | 8f6d7f4 | 2011-09-26 15:55:20 -0400 | [diff] [blame] | 3453 | u64 last_objectid = 0; |
Omar Sandoval | f7e9e8f | 2018-05-11 13:13:32 -0700 | [diff] [blame] | 3454 | int ret = 0, nr_unlink = 0; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3455 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3456 | if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED)) |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3457 | return 0; |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 3458 | |
| 3459 | path = btrfs_alloc_path(); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3460 | if (!path) { |
| 3461 | ret = -ENOMEM; |
| 3462 | goto out; |
| 3463 | } |
David Sterba | e4058b5 | 2015-11-27 16:31:35 +0100 | [diff] [blame] | 3464 | path->reada = READA_BACK; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3465 | |
| 3466 | key.objectid = BTRFS_ORPHAN_OBJECTID; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 3467 | key.type = BTRFS_ORPHAN_ITEM_KEY; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3468 | key.offset = (u64)-1; |
| 3469 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3470 | while (1) { |
| 3471 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3472 | if (ret < 0) |
| 3473 | goto out; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3474 | |
| 3475 | /* |
| 3476 | * if ret == 0 means we found what we were searching for, which |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 3477 | * is weird, but possible, so only screw with path if we didn't |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3478 | * find the key and see if we have stuff that matches |
| 3479 | */ |
| 3480 | if (ret > 0) { |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3481 | ret = 0; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3482 | if (path->slots[0] == 0) |
| 3483 | break; |
| 3484 | path->slots[0]--; |
| 3485 | } |
| 3486 | |
| 3487 | /* pull out the item */ |
| 3488 | leaf = path->nodes[0]; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3489 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 3490 | |
| 3491 | /* make sure the item matches what we want */ |
| 3492 | if (found_key.objectid != BTRFS_ORPHAN_OBJECTID) |
| 3493 | break; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 3494 | if (found_key.type != BTRFS_ORPHAN_ITEM_KEY) |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3495 | break; |
| 3496 | |
| 3497 | /* release the path since we're done with it */ |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3498 | btrfs_release_path(path); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3499 | |
| 3500 | /* |
| 3501 | * this is where we are basically btrfs_lookup, without the |
| 3502 | * crossing root thing. we store the inode number in the |
| 3503 | * offset of the orphan item. |
| 3504 | */ |
Josef Bacik | 8f6d7f4 | 2011-09-26 15:55:20 -0400 | [diff] [blame] | 3505 | |
| 3506 | if (found_key.offset == last_objectid) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3507 | btrfs_err(fs_info, |
| 3508 | "Error removing orphan entry, stopping orphan cleanup"); |
Josef Bacik | 8f6d7f4 | 2011-09-26 15:55:20 -0400 | [diff] [blame] | 3509 | ret = -EINVAL; |
| 3510 | goto out; |
| 3511 | } |
| 3512 | |
| 3513 | last_objectid = found_key.offset; |
| 3514 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3515 | found_key.objectid = found_key.offset; |
| 3516 | found_key.type = BTRFS_INODE_ITEM_KEY; |
| 3517 | found_key.offset = 0; |
David Sterba | 0202e83 | 2020-05-15 19:35:59 +0200 | [diff] [blame] | 3518 | inode = btrfs_iget(fs_info->sb, last_objectid, root); |
Rusty Russell | 8c6ffba | 2013-07-15 11:20:32 +0930 | [diff] [blame] | 3519 | ret = PTR_ERR_OR_ZERO(inode); |
Filipe Manana | 6771089 | 2016-06-06 11:51:25 +0100 | [diff] [blame] | 3520 | if (ret && ret != -ENOENT) |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3521 | goto out; |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 3522 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3523 | if (ret == -ENOENT && root == fs_info->tree_root) { |
Arne Jansen | f8e9e0b | 2011-12-14 20:12:02 -0500 | [diff] [blame] | 3524 | struct btrfs_root *dead_root; |
Arne Jansen | f8e9e0b | 2011-12-14 20:12:02 -0500 | [diff] [blame] | 3525 | int is_dead_root = 0; |
| 3526 | |
| 3527 | /* |
Filipe Manana | 0c0218e | 2021-03-16 16:54:13 +0000 | [diff] [blame] | 3528 | * This is an orphan in the tree root. Currently these |
Arne Jansen | f8e9e0b | 2011-12-14 20:12:02 -0500 | [diff] [blame] | 3529 | * could come from 2 sources: |
Filipe Manana | 0c0218e | 2021-03-16 16:54:13 +0000 | [diff] [blame] | 3530 | * a) a root (snapshot/subvolume) deletion in progress |
Arne Jansen | f8e9e0b | 2011-12-14 20:12:02 -0500 | [diff] [blame] | 3531 | * b) a free space cache inode |
Filipe Manana | 0c0218e | 2021-03-16 16:54:13 +0000 | [diff] [blame] | 3532 | * We need to distinguish those two, as the orphan item |
| 3533 | * for a root must not get deleted before the deletion |
| 3534 | * of the snapshot/subvolume's tree completes. |
| 3535 | * |
| 3536 | * btrfs_find_orphan_roots() ran before us, which has |
| 3537 | * found all deleted roots and loaded them into |
| 3538 | * fs_info->fs_roots_radix. So here we can find if an |
| 3539 | * orphan item corresponds to a deleted root by looking |
| 3540 | * up the root from that radix tree. |
Arne Jansen | f8e9e0b | 2011-12-14 20:12:02 -0500 | [diff] [blame] | 3541 | */ |
Robbie Ko | a619b3c | 2020-05-07 10:54:40 +0800 | [diff] [blame] | 3542 | |
| 3543 | spin_lock(&fs_info->fs_roots_radix_lock); |
| 3544 | dead_root = radix_tree_lookup(&fs_info->fs_roots_radix, |
| 3545 | (unsigned long)found_key.objectid); |
| 3546 | if (dead_root && btrfs_root_refs(&dead_root->root_item) == 0) |
| 3547 | is_dead_root = 1; |
| 3548 | spin_unlock(&fs_info->fs_roots_radix_lock); |
| 3549 | |
Arne Jansen | f8e9e0b | 2011-12-14 20:12:02 -0500 | [diff] [blame] | 3550 | if (is_dead_root) { |
| 3551 | /* prevent this orphan from being found again */ |
| 3552 | key.offset = found_key.objectid - 1; |
| 3553 | continue; |
| 3554 | } |
Omar Sandoval | f7e9e8f | 2018-05-11 13:13:32 -0700 | [diff] [blame] | 3555 | |
Arne Jansen | f8e9e0b | 2011-12-14 20:12:02 -0500 | [diff] [blame] | 3556 | } |
Omar Sandoval | f7e9e8f | 2018-05-11 13:13:32 -0700 | [diff] [blame] | 3557 | |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 3558 | /* |
Omar Sandoval | f7e9e8f | 2018-05-11 13:13:32 -0700 | [diff] [blame] | 3559 | * If we have an inode with links, there are a couple of |
Boris Burkov | 7052425 | 2021-06-30 13:01:50 -0700 | [diff] [blame] | 3560 | * possibilities: |
| 3561 | * |
| 3562 | * 1. We were halfway through creating fsverity metadata for the |
| 3563 | * file. In that case, the orphan item represents incomplete |
| 3564 | * fsverity metadata which must be cleaned up with |
| 3565 | * btrfs_drop_verity_items and deleting the orphan item. |
| 3566 | |
| 3567 | * 2. Old kernels (before v3.12) used to create an |
Omar Sandoval | f7e9e8f | 2018-05-11 13:13:32 -0700 | [diff] [blame] | 3568 | * orphan item for truncate indicating that there were possibly |
| 3569 | * extent items past i_size that needed to be deleted. In v3.12, |
| 3570 | * truncate was changed to update i_size in sync with the extent |
| 3571 | * items, but the (useless) orphan item was still created. Since |
| 3572 | * v4.18, we don't create the orphan item for truncate at all. |
| 3573 | * |
| 3574 | * So, this item could mean that we need to do a truncate, but |
| 3575 | * only if this filesystem was last used on a pre-v3.12 kernel |
| 3576 | * and was not cleanly unmounted. The odds of that are quite |
| 3577 | * slim, and it's a pain to do the truncate now, so just delete |
| 3578 | * the orphan item. |
| 3579 | * |
| 3580 | * It's also possible that this orphan item was supposed to be |
| 3581 | * deleted but wasn't. The inode number may have been reused, |
| 3582 | * but either way, we can delete the orphan item. |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 3583 | */ |
Omar Sandoval | f7e9e8f | 2018-05-11 13:13:32 -0700 | [diff] [blame] | 3584 | if (ret == -ENOENT || inode->i_nlink) { |
Boris Burkov | 7052425 | 2021-06-30 13:01:50 -0700 | [diff] [blame] | 3585 | if (!ret) { |
| 3586 | ret = btrfs_drop_verity_items(BTRFS_I(inode)); |
Omar Sandoval | f7e9e8f | 2018-05-11 13:13:32 -0700 | [diff] [blame] | 3587 | iput(inode); |
Boris Burkov | 7052425 | 2021-06-30 13:01:50 -0700 | [diff] [blame] | 3588 | if (ret) |
| 3589 | goto out; |
| 3590 | } |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 3591 | trans = btrfs_start_transaction(root, 1); |
| 3592 | if (IS_ERR(trans)) { |
| 3593 | ret = PTR_ERR(trans); |
| 3594 | goto out; |
| 3595 | } |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3596 | btrfs_debug(fs_info, "auto deleting %Lu", |
| 3597 | found_key.objectid); |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 3598 | ret = btrfs_del_orphan_item(trans, root, |
| 3599 | found_key.objectid); |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 3600 | btrfs_end_transaction(trans); |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 3601 | if (ret) |
| 3602 | goto out; |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 3603 | continue; |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3604 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3605 | |
Omar Sandoval | f7e9e8f | 2018-05-11 13:13:32 -0700 | [diff] [blame] | 3606 | nr_unlink++; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3607 | |
| 3608 | /* this will do delete_inode and everything for us */ |
| 3609 | iput(inode); |
| 3610 | } |
Miao Xie | 3254c87 | 2011-11-10 20:45:05 -0500 | [diff] [blame] | 3611 | /* release the path since we're done with it */ |
| 3612 | btrfs_release_path(path); |
| 3613 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3614 | root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE; |
| 3615 | |
Omar Sandoval | a575cee | 2018-05-11 13:13:38 -0700 | [diff] [blame] | 3616 | if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) { |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 3617 | trans = btrfs_join_transaction(root); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3618 | if (!IS_ERR(trans)) |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 3619 | btrfs_end_transaction(trans); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3620 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3621 | |
| 3622 | if (nr_unlink) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3623 | btrfs_debug(fs_info, "unlinked %d orphans", nr_unlink); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3624 | |
| 3625 | out: |
| 3626 | if (ret) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3627 | btrfs_err(fs_info, "could not do orphan cleanup %d", ret); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3628 | btrfs_free_path(path); |
| 3629 | return ret; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3630 | } |
| 3631 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3632 | /* |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3633 | * very simple check to peek ahead in the leaf looking for xattrs. If we |
| 3634 | * don't find any xattrs, we know there can't be any acls. |
| 3635 | * |
| 3636 | * slot is the slot the inode is in, objectid is the objectid of the inode |
| 3637 | */ |
| 3638 | static noinline int acls_after_inode_item(struct extent_buffer *leaf, |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 3639 | int slot, u64 objectid, |
| 3640 | int *first_xattr_slot) |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3641 | { |
| 3642 | u32 nritems = btrfs_header_nritems(leaf); |
| 3643 | struct btrfs_key found_key; |
Josef Bacik | f23b5a5 | 2013-06-19 10:16:26 -0400 | [diff] [blame] | 3644 | static u64 xattr_access = 0; |
| 3645 | static u64 xattr_default = 0; |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3646 | int scanned = 0; |
| 3647 | |
Josef Bacik | f23b5a5 | 2013-06-19 10:16:26 -0400 | [diff] [blame] | 3648 | if (!xattr_access) { |
Andreas Gruenbacher | 97d7929 | 2015-12-02 14:44:35 +0100 | [diff] [blame] | 3649 | xattr_access = btrfs_name_hash(XATTR_NAME_POSIX_ACL_ACCESS, |
| 3650 | strlen(XATTR_NAME_POSIX_ACL_ACCESS)); |
| 3651 | xattr_default = btrfs_name_hash(XATTR_NAME_POSIX_ACL_DEFAULT, |
| 3652 | strlen(XATTR_NAME_POSIX_ACL_DEFAULT)); |
Josef Bacik | f23b5a5 | 2013-06-19 10:16:26 -0400 | [diff] [blame] | 3653 | } |
| 3654 | |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3655 | slot++; |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 3656 | *first_xattr_slot = -1; |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3657 | while (slot < nritems) { |
| 3658 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
| 3659 | |
| 3660 | /* we found a different objectid, there must not be acls */ |
| 3661 | if (found_key.objectid != objectid) |
| 3662 | return 0; |
| 3663 | |
| 3664 | /* we found an xattr, assume we've got an acl */ |
Josef Bacik | f23b5a5 | 2013-06-19 10:16:26 -0400 | [diff] [blame] | 3665 | if (found_key.type == BTRFS_XATTR_ITEM_KEY) { |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 3666 | if (*first_xattr_slot == -1) |
| 3667 | *first_xattr_slot = slot; |
Josef Bacik | f23b5a5 | 2013-06-19 10:16:26 -0400 | [diff] [blame] | 3668 | if (found_key.offset == xattr_access || |
| 3669 | found_key.offset == xattr_default) |
| 3670 | return 1; |
| 3671 | } |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3672 | |
| 3673 | /* |
| 3674 | * we found a key greater than an xattr key, there can't |
| 3675 | * be any acls later on |
| 3676 | */ |
| 3677 | if (found_key.type > BTRFS_XATTR_ITEM_KEY) |
| 3678 | return 0; |
| 3679 | |
| 3680 | slot++; |
| 3681 | scanned++; |
| 3682 | |
| 3683 | /* |
| 3684 | * it goes inode, inode backrefs, xattrs, extents, |
| 3685 | * so if there are a ton of hard links to an inode there can |
| 3686 | * be a lot of backrefs. Don't waste time searching too hard, |
| 3687 | * this is just an optimization |
| 3688 | */ |
| 3689 | if (scanned >= 8) |
| 3690 | break; |
| 3691 | } |
| 3692 | /* we hit the end of the leaf before we found an xattr or |
| 3693 | * something larger than an xattr. We have to assume the inode |
| 3694 | * has acls |
| 3695 | */ |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 3696 | if (*first_xattr_slot == -1) |
| 3697 | *first_xattr_slot = slot; |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3698 | return 1; |
| 3699 | } |
| 3700 | |
| 3701 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3702 | * read an inode from the btree into the in-memory inode |
| 3703 | */ |
Filipe Manana | 4222ea7 | 2018-10-24 10:13:03 +0100 | [diff] [blame] | 3704 | static int btrfs_read_locked_inode(struct inode *inode, |
| 3705 | struct btrfs_path *in_path) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3706 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3707 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Filipe Manana | 4222ea7 | 2018-10-24 10:13:03 +0100 | [diff] [blame] | 3708 | struct btrfs_path *path = in_path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3709 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3710 | struct btrfs_inode_item *inode_item; |
| 3711 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 3712 | struct btrfs_key location; |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 3713 | unsigned long ptr; |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3714 | int maybe_acls; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 3715 | u32 rdev; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3716 | int ret; |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 3717 | bool filled = false; |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 3718 | int first_xattr_slot; |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 3719 | |
| 3720 | ret = btrfs_fill_inode(inode, &rdev); |
| 3721 | if (!ret) |
| 3722 | filled = true; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3723 | |
Filipe Manana | 4222ea7 | 2018-10-24 10:13:03 +0100 | [diff] [blame] | 3724 | if (!path) { |
| 3725 | path = btrfs_alloc_path(); |
| 3726 | if (!path) |
| 3727 | return -ENOMEM; |
| 3728 | } |
Mark Fasheh | 1748f84 | 2011-07-12 11:25:31 -0700 | [diff] [blame] | 3729 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3730 | memcpy(&location, &BTRFS_I(inode)->location, sizeof(location)); |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 3731 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3732 | ret = btrfs_lookup_inode(NULL, root, path, &location, 0); |
Filipe Manana | 6771089 | 2016-06-06 11:51:25 +0100 | [diff] [blame] | 3733 | if (ret) { |
Filipe Manana | 4222ea7 | 2018-10-24 10:13:03 +0100 | [diff] [blame] | 3734 | if (path != in_path) |
| 3735 | btrfs_free_path(path); |
Al Viro | f5b3a41 | 2018-07-29 23:04:51 +0100 | [diff] [blame] | 3736 | return ret; |
Filipe Manana | 6771089 | 2016-06-06 11:51:25 +0100 | [diff] [blame] | 3737 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3738 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3739 | leaf = path->nodes[0]; |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 3740 | |
| 3741 | if (filled) |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 3742 | goto cache_index; |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 3743 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3744 | inode_item = btrfs_item_ptr(leaf, path->slots[0], |
| 3745 | struct btrfs_inode_item); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3746 | inode->i_mode = btrfs_inode_mode(leaf, inode_item); |
Miklos Szeredi | bfe8684 | 2011-10-28 14:13:29 +0200 | [diff] [blame] | 3747 | set_nlink(inode, btrfs_inode_nlink(leaf, inode_item)); |
Eric W. Biederman | 2f2f43d | 2012-02-10 11:05:07 -0800 | [diff] [blame] | 3748 | i_uid_write(inode, btrfs_inode_uid(leaf, inode_item)); |
| 3749 | i_gid_write(inode, btrfs_inode_gid(leaf, inode_item)); |
Nikolay Borisov | 6ef06d2 | 2017-02-20 13:50:34 +0200 | [diff] [blame] | 3750 | btrfs_i_size_write(BTRFS_I(inode), btrfs_inode_size(leaf, inode_item)); |
Josef Bacik | 41a2ee7 | 2020-01-17 09:02:21 -0500 | [diff] [blame] | 3751 | btrfs_inode_set_file_extent_range(BTRFS_I(inode), 0, |
| 3752 | round_up(i_size_read(inode), fs_info->sectorsize)); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3753 | |
David Sterba | a937b97 | 2014-12-12 17:39:12 +0100 | [diff] [blame] | 3754 | inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->atime); |
| 3755 | inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->atime); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3756 | |
David Sterba | a937b97 | 2014-12-12 17:39:12 +0100 | [diff] [blame] | 3757 | inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->mtime); |
| 3758 | inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->mtime); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3759 | |
David Sterba | a937b97 | 2014-12-12 17:39:12 +0100 | [diff] [blame] | 3760 | inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->ctime); |
| 3761 | inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->ctime); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3762 | |
chandan r | 9cc97d6 | 2012-07-04 12:48:07 +0530 | [diff] [blame] | 3763 | BTRFS_I(inode)->i_otime.tv_sec = |
| 3764 | btrfs_timespec_sec(leaf, &inode_item->otime); |
| 3765 | BTRFS_I(inode)->i_otime.tv_nsec = |
| 3766 | btrfs_timespec_nsec(leaf, &inode_item->otime); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3767 | |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 3768 | inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item)); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3769 | BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item); |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 3770 | BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item); |
| 3771 | |
Jeff Layton | c7f88c4 | 2017-12-11 06:35:12 -0500 | [diff] [blame] | 3772 | inode_set_iversion_queried(inode, |
| 3773 | btrfs_inode_sequence(leaf, inode_item)); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3774 | inode->i_generation = BTRFS_I(inode)->generation; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 3775 | inode->i_rdev = 0; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3776 | rdev = btrfs_inode_rdev(leaf, inode_item); |
| 3777 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 3778 | BTRFS_I(inode)->index_cnt = (u64)-1; |
Boris Burkov | 77eea05 | 2021-06-30 13:01:48 -0700 | [diff] [blame] | 3779 | btrfs_inode_split_flags(btrfs_inode_flags(leaf, inode_item), |
| 3780 | &BTRFS_I(inode)->flags, &BTRFS_I(inode)->ro_flags); |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 3781 | |
| 3782 | cache_index: |
Yang Dongsheng | 6e17d30 | 2015-04-09 12:08:43 +0800 | [diff] [blame] | 3783 | /* |
| 3784 | * If we were modified in the current generation and evicted from memory |
| 3785 | * and then re-read we need to do a full sync since we don't have any |
| 3786 | * idea about which extents were modified before we were evicted from |
| 3787 | * cache. |
| 3788 | * |
| 3789 | * This is required for both inode re-read from disk and delayed inode |
| 3790 | * in delayed_nodes_tree. |
| 3791 | */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3792 | if (BTRFS_I(inode)->last_trans == fs_info->generation) |
Yang Dongsheng | 6e17d30 | 2015-04-09 12:08:43 +0800 | [diff] [blame] | 3793 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, |
| 3794 | &BTRFS_I(inode)->runtime_flags); |
| 3795 | |
Filipe Manana | bde6c24 | 2015-07-24 00:00:19 +0100 | [diff] [blame] | 3796 | /* |
| 3797 | * We don't persist the id of the transaction where an unlink operation |
| 3798 | * against the inode was last made. So here we assume the inode might |
| 3799 | * have been evicted, and therefore the exact value of last_unlink_trans |
| 3800 | * lost, and set it to last_trans to avoid metadata inconsistencies |
| 3801 | * between the inode and its parent if the inode is fsync'ed and the log |
| 3802 | * replayed. For example, in the scenario: |
| 3803 | * |
| 3804 | * touch mydir/foo |
| 3805 | * ln mydir/foo mydir/bar |
| 3806 | * sync |
| 3807 | * unlink mydir/bar |
| 3808 | * echo 2 > /proc/sys/vm/drop_caches # evicts inode |
| 3809 | * xfs_io -c fsync mydir/foo |
| 3810 | * <power failure> |
| 3811 | * mount fs, triggers fsync log replay |
| 3812 | * |
| 3813 | * We must make sure that when we fsync our inode foo we also log its |
| 3814 | * parent inode, otherwise after log replay the parent still has the |
| 3815 | * dentry with the "bar" name but our inode foo has a link count of 1 |
| 3816 | * and doesn't have an inode ref with the name "bar" anymore. |
| 3817 | * |
| 3818 | * Setting last_unlink_trans to last_trans is a pessimistic approach, |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 3819 | * but it guarantees correctness at the expense of occasional full |
Filipe Manana | bde6c24 | 2015-07-24 00:00:19 +0100 | [diff] [blame] | 3820 | * transaction commits on fsync if our inode is a directory, or if our |
| 3821 | * inode is not a directory, logging its parent unnecessarily. |
| 3822 | */ |
| 3823 | BTRFS_I(inode)->last_unlink_trans = BTRFS_I(inode)->last_trans; |
| 3824 | |
Filipe Manana | 3ebac17 | 2020-07-15 12:30:43 +0100 | [diff] [blame] | 3825 | /* |
| 3826 | * Same logic as for last_unlink_trans. We don't persist the generation |
| 3827 | * of the last transaction where this inode was used for a reflink |
| 3828 | * operation, so after eviction and reloading the inode we must be |
| 3829 | * pessimistic and assume the last transaction that modified the inode. |
| 3830 | */ |
| 3831 | BTRFS_I(inode)->last_reflink_trans = BTRFS_I(inode)->last_trans; |
| 3832 | |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 3833 | path->slots[0]++; |
| 3834 | if (inode->i_nlink != 1 || |
| 3835 | path->slots[0] >= btrfs_header_nritems(leaf)) |
| 3836 | goto cache_acl; |
| 3837 | |
| 3838 | btrfs_item_key_to_cpu(leaf, &location, path->slots[0]); |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 3839 | if (location.objectid != btrfs_ino(BTRFS_I(inode))) |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 3840 | goto cache_acl; |
| 3841 | |
| 3842 | ptr = btrfs_item_ptr_offset(leaf, path->slots[0]); |
| 3843 | if (location.type == BTRFS_INODE_REF_KEY) { |
| 3844 | struct btrfs_inode_ref *ref; |
| 3845 | |
| 3846 | ref = (struct btrfs_inode_ref *)ptr; |
| 3847 | BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref); |
| 3848 | } else if (location.type == BTRFS_INODE_EXTREF_KEY) { |
| 3849 | struct btrfs_inode_extref *extref; |
| 3850 | |
| 3851 | extref = (struct btrfs_inode_extref *)ptr; |
| 3852 | BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf, |
| 3853 | extref); |
| 3854 | } |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 3855 | cache_acl: |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3856 | /* |
| 3857 | * try to precache a NULL acl entry for files that don't have |
| 3858 | * any xattrs or acls |
| 3859 | */ |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3860 | maybe_acls = acls_after_inode_item(leaf, path->slots[0], |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 3861 | btrfs_ino(BTRFS_I(inode)), &first_xattr_slot); |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 3862 | if (first_xattr_slot != -1) { |
| 3863 | path->slots[0] = first_xattr_slot; |
| 3864 | ret = btrfs_load_inode_props(inode, path); |
| 3865 | if (ret) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3866 | btrfs_err(fs_info, |
David Sterba | 351fd35 | 2014-05-15 16:48:20 +0200 | [diff] [blame] | 3867 | "error loading props for ino %llu (root %llu): %d", |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 3868 | btrfs_ino(BTRFS_I(inode)), |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 3869 | root->root_key.objectid, ret); |
| 3870 | } |
Filipe Manana | 4222ea7 | 2018-10-24 10:13:03 +0100 | [diff] [blame] | 3871 | if (path != in_path) |
| 3872 | btrfs_free_path(path); |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 3873 | |
Al Viro | 72c0490 | 2009-06-24 16:58:48 -0400 | [diff] [blame] | 3874 | if (!maybe_acls) |
| 3875 | cache_no_acl(inode); |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3876 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3877 | switch (inode->i_mode & S_IFMT) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3878 | case S_IFREG: |
| 3879 | inode->i_mapping->a_ops = &btrfs_aops; |
| 3880 | inode->i_fop = &btrfs_file_operations; |
| 3881 | inode->i_op = &btrfs_file_inode_operations; |
| 3882 | break; |
| 3883 | case S_IFDIR: |
| 3884 | inode->i_fop = &btrfs_dir_file_operations; |
Omar Sandoval | 67ade05 | 2017-01-25 17:06:38 -0800 | [diff] [blame] | 3885 | inode->i_op = &btrfs_dir_inode_operations; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3886 | break; |
| 3887 | case S_IFLNK: |
| 3888 | inode->i_op = &btrfs_symlink_inode_operations; |
Al Viro | 21fc61c | 2015-11-17 01:07:57 -0500 | [diff] [blame] | 3889 | inode_nohighmem(inode); |
Omar Sandoval | 4779cc0 | 2018-09-24 15:16:55 -0700 | [diff] [blame] | 3890 | inode->i_mapping->a_ops = &btrfs_aops; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3891 | break; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 3892 | default: |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 3893 | inode->i_op = &btrfs_special_inode_operations; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 3894 | init_special_inode(inode, inode->i_mode, rdev); |
| 3895 | break; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3896 | } |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 3897 | |
David Sterba | 7b6a221 | 2018-03-26 18:40:21 +0200 | [diff] [blame] | 3898 | btrfs_sync_inode_flags_to_i_flags(inode); |
Filipe Manana | 6771089 | 2016-06-06 11:51:25 +0100 | [diff] [blame] | 3899 | return 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3900 | } |
| 3901 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3902 | /* |
| 3903 | * given a leaf and an inode, copy the inode fields into the leaf |
| 3904 | */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3905 | static void fill_inode_item(struct btrfs_trans_handle *trans, |
| 3906 | struct extent_buffer *leaf, |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3907 | struct btrfs_inode_item *item, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3908 | struct inode *inode) |
| 3909 | { |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3910 | struct btrfs_map_token token; |
Boris Burkov | 77eea05 | 2021-06-30 13:01:48 -0700 | [diff] [blame] | 3911 | u64 flags; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3912 | |
David Sterba | c82f823 | 2019-08-09 17:48:21 +0200 | [diff] [blame] | 3913 | btrfs_init_map_token(&token, leaf); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3914 | |
David Sterba | cc4c13d | 2020-04-29 02:15:56 +0200 | [diff] [blame] | 3915 | btrfs_set_token_inode_uid(&token, item, i_uid_read(inode)); |
| 3916 | btrfs_set_token_inode_gid(&token, item, i_gid_read(inode)); |
| 3917 | btrfs_set_token_inode_size(&token, item, BTRFS_I(inode)->disk_i_size); |
| 3918 | btrfs_set_token_inode_mode(&token, item, inode->i_mode); |
| 3919 | btrfs_set_token_inode_nlink(&token, item, inode->i_nlink); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3920 | |
David Sterba | cc4c13d | 2020-04-29 02:15:56 +0200 | [diff] [blame] | 3921 | btrfs_set_token_timespec_sec(&token, &item->atime, |
| 3922 | inode->i_atime.tv_sec); |
| 3923 | btrfs_set_token_timespec_nsec(&token, &item->atime, |
| 3924 | inode->i_atime.tv_nsec); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3925 | |
David Sterba | cc4c13d | 2020-04-29 02:15:56 +0200 | [diff] [blame] | 3926 | btrfs_set_token_timespec_sec(&token, &item->mtime, |
| 3927 | inode->i_mtime.tv_sec); |
| 3928 | btrfs_set_token_timespec_nsec(&token, &item->mtime, |
| 3929 | inode->i_mtime.tv_nsec); |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3930 | |
David Sterba | cc4c13d | 2020-04-29 02:15:56 +0200 | [diff] [blame] | 3931 | btrfs_set_token_timespec_sec(&token, &item->ctime, |
| 3932 | inode->i_ctime.tv_sec); |
| 3933 | btrfs_set_token_timespec_nsec(&token, &item->ctime, |
| 3934 | inode->i_ctime.tv_nsec); |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3935 | |
David Sterba | cc4c13d | 2020-04-29 02:15:56 +0200 | [diff] [blame] | 3936 | btrfs_set_token_timespec_sec(&token, &item->otime, |
| 3937 | BTRFS_I(inode)->i_otime.tv_sec); |
| 3938 | btrfs_set_token_timespec_nsec(&token, &item->otime, |
| 3939 | BTRFS_I(inode)->i_otime.tv_nsec); |
chandan r | 9cc97d6 | 2012-07-04 12:48:07 +0530 | [diff] [blame] | 3940 | |
David Sterba | cc4c13d | 2020-04-29 02:15:56 +0200 | [diff] [blame] | 3941 | btrfs_set_token_inode_nbytes(&token, item, inode_get_bytes(inode)); |
| 3942 | btrfs_set_token_inode_generation(&token, item, |
| 3943 | BTRFS_I(inode)->generation); |
| 3944 | btrfs_set_token_inode_sequence(&token, item, inode_peek_iversion(inode)); |
| 3945 | btrfs_set_token_inode_transid(&token, item, trans->transid); |
| 3946 | btrfs_set_token_inode_rdev(&token, item, inode->i_rdev); |
Boris Burkov | 77eea05 | 2021-06-30 13:01:48 -0700 | [diff] [blame] | 3947 | flags = btrfs_inode_combine_flags(BTRFS_I(inode)->flags, |
| 3948 | BTRFS_I(inode)->ro_flags); |
| 3949 | btrfs_set_token_inode_flags(&token, item, flags); |
David Sterba | cc4c13d | 2020-04-29 02:15:56 +0200 | [diff] [blame] | 3950 | btrfs_set_token_inode_block_group(&token, item, 0); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3951 | } |
| 3952 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3953 | /* |
| 3954 | * copy everything in the in-memory inode into the btree. |
| 3955 | */ |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 3956 | static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans, |
Nikolay Borisov | dfeb9e7 | 2020-11-02 16:48:58 +0200 | [diff] [blame] | 3957 | struct btrfs_root *root, |
| 3958 | struct btrfs_inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3959 | { |
| 3960 | struct btrfs_inode_item *inode_item; |
| 3961 | struct btrfs_path *path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3962 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3963 | int ret; |
| 3964 | |
| 3965 | path = btrfs_alloc_path(); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 3966 | if (!path) |
| 3967 | return -ENOMEM; |
| 3968 | |
Nikolay Borisov | dfeb9e7 | 2020-11-02 16:48:58 +0200 | [diff] [blame] | 3969 | ret = btrfs_lookup_inode(trans, root, path, &inode->location, 1); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3970 | if (ret) { |
| 3971 | if (ret > 0) |
| 3972 | ret = -ENOENT; |
| 3973 | goto failed; |
| 3974 | } |
| 3975 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3976 | leaf = path->nodes[0]; |
| 3977 | inode_item = btrfs_item_ptr(leaf, path->slots[0], |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 3978 | struct btrfs_inode_item); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3979 | |
Nikolay Borisov | dfeb9e7 | 2020-11-02 16:48:58 +0200 | [diff] [blame] | 3980 | fill_inode_item(trans, leaf, inode_item, &inode->vfs_inode); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3981 | btrfs_mark_buffer_dirty(leaf); |
Nikolay Borisov | dfeb9e7 | 2020-11-02 16:48:58 +0200 | [diff] [blame] | 3982 | btrfs_set_inode_last_trans(trans, inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3983 | ret = 0; |
| 3984 | failed: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3985 | btrfs_free_path(path); |
| 3986 | return ret; |
| 3987 | } |
| 3988 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3989 | /* |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 3990 | * copy everything in the in-memory inode into the btree. |
| 3991 | */ |
| 3992 | noinline int btrfs_update_inode(struct btrfs_trans_handle *trans, |
Nikolay Borisov | 9a56fcd | 2020-11-02 16:48:59 +0200 | [diff] [blame] | 3993 | struct btrfs_root *root, |
| 3994 | struct btrfs_inode *inode) |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 3995 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3996 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 3997 | int ret; |
| 3998 | |
| 3999 | /* |
| 4000 | * If the inode is a free space inode, we can deadlock during commit |
| 4001 | * if we put it into the delayed code. |
| 4002 | * |
| 4003 | * The data relocation inode should also be directly updated |
| 4004 | * without delay |
| 4005 | */ |
Nikolay Borisov | 9a56fcd | 2020-11-02 16:48:59 +0200 | [diff] [blame] | 4006 | if (!btrfs_is_free_space_inode(inode) |
Josef Bacik | 1d52c78 | 2014-09-18 11:30:44 -0400 | [diff] [blame] | 4007 | && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4008 | && !test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) { |
Alexander Block | 8ea05e3 | 2012-07-25 17:35:53 +0200 | [diff] [blame] | 4009 | btrfs_update_root_times(trans, root); |
| 4010 | |
Nikolay Borisov | 9a56fcd | 2020-11-02 16:48:59 +0200 | [diff] [blame] | 4011 | ret = btrfs_delayed_update_inode(trans, root, inode); |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 4012 | if (!ret) |
Nikolay Borisov | 9a56fcd | 2020-11-02 16:48:59 +0200 | [diff] [blame] | 4013 | btrfs_set_inode_last_trans(trans, inode); |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 4014 | return ret; |
| 4015 | } |
| 4016 | |
Nikolay Borisov | 9a56fcd | 2020-11-02 16:48:59 +0200 | [diff] [blame] | 4017 | return btrfs_update_inode_item(trans, root, inode); |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 4018 | } |
| 4019 | |
Nikolay Borisov | 729f796 | 2020-11-02 16:49:06 +0200 | [diff] [blame] | 4020 | int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans, |
| 4021 | struct btrfs_root *root, struct btrfs_inode *inode) |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 4022 | { |
| 4023 | int ret; |
| 4024 | |
Nikolay Borisov | 729f796 | 2020-11-02 16:49:06 +0200 | [diff] [blame] | 4025 | ret = btrfs_update_inode(trans, root, inode); |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 4026 | if (ret == -ENOSPC) |
Nikolay Borisov | 729f796 | 2020-11-02 16:49:06 +0200 | [diff] [blame] | 4027 | return btrfs_update_inode_item(trans, root, inode); |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 4028 | return ret; |
| 4029 | } |
| 4030 | |
| 4031 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 4032 | * unlink helper that gets used here in inode.c and in the tree logging |
| 4033 | * recovery code. It remove a link in a directory with a given name, and |
| 4034 | * also drops the back refs in the inode to the directory |
| 4035 | */ |
Al Viro | 9298679 | 2011-03-04 17:14:37 +0000 | [diff] [blame] | 4036 | static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans, |
| 4037 | struct btrfs_root *root, |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 4038 | struct btrfs_inode *dir, |
| 4039 | struct btrfs_inode *inode, |
Al Viro | 9298679 | 2011-03-04 17:14:37 +0000 | [diff] [blame] | 4040 | const char *name, int name_len) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4041 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4042 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4043 | struct btrfs_path *path; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4044 | int ret = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4045 | struct btrfs_dir_item *di; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4046 | u64 index; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4047 | u64 ino = btrfs_ino(inode); |
| 4048 | u64 dir_ino = btrfs_ino(dir); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4049 | |
| 4050 | path = btrfs_alloc_path(); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 4051 | if (!path) { |
| 4052 | ret = -ENOMEM; |
Tsutomu Itoh | 554233a | 2011-02-03 03:16:25 +0000 | [diff] [blame] | 4053 | goto out; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 4054 | } |
| 4055 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4056 | di = btrfs_lookup_dir_item(trans, root, path, dir_ino, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4057 | name, name_len, -1); |
Liu Bo | 3cf5068 | 2018-09-12 06:06:26 +0800 | [diff] [blame] | 4058 | if (IS_ERR_OR_NULL(di)) { |
| 4059 | ret = di ? PTR_ERR(di) : -ENOENT; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4060 | goto err; |
| 4061 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4062 | ret = btrfs_delete_one_dir_name(trans, root, path, di); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 4063 | if (ret) |
| 4064 | goto err; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4065 | btrfs_release_path(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4066 | |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 4067 | /* |
| 4068 | * If we don't have dir index, we have to get it by looking up |
| 4069 | * the inode ref, since we get the inode ref, remove it directly, |
| 4070 | * it is unnecessary to do delayed deletion. |
| 4071 | * |
| 4072 | * But if we have dir index, needn't search inode ref to get it. |
| 4073 | * Since the inode ref is close to the inode item, it is better |
| 4074 | * that we delay to delete it, and just do this deletion when |
| 4075 | * we update the inode item. |
| 4076 | */ |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 4077 | if (inode->dir_index) { |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 4078 | ret = btrfs_delayed_delete_inode_ref(inode); |
| 4079 | if (!ret) { |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 4080 | index = inode->dir_index; |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 4081 | goto skip_backref; |
| 4082 | } |
| 4083 | } |
| 4084 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4085 | ret = btrfs_del_inode_ref(trans, root, name, name_len, ino, |
| 4086 | dir_ino, &index); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4087 | if (ret) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4088 | btrfs_info(fs_info, |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 4089 | "failed to delete reference to %.*s, inode %llu parent %llu", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 4090 | name_len, name, ino, dir_ino); |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 4091 | btrfs_abort_transaction(trans, ret); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4092 | goto err; |
| 4093 | } |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 4094 | skip_backref: |
Lu Fengqi | 9add294 | 2018-08-01 11:32:26 +0800 | [diff] [blame] | 4095 | ret = btrfs_delete_delayed_dir_index(trans, dir, index); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4096 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 4097 | btrfs_abort_transaction(trans, ret); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4098 | goto err; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4099 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4100 | |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 4101 | ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len, inode, |
| 4102 | dir_ino); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4103 | if (ret != 0 && ret != -ENOENT) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 4104 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4105 | goto err; |
| 4106 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4107 | |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 4108 | ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len, dir, |
| 4109 | index); |
Chris Mason | 6418c96 | 2010-10-30 07:34:24 -0400 | [diff] [blame] | 4110 | if (ret == -ENOENT) |
| 4111 | ret = 0; |
Zach Brown | d4e3991 | 2013-04-02 21:02:16 +0000 | [diff] [blame] | 4112 | else if (ret) |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 4113 | btrfs_abort_transaction(trans, ret); |
Josef Bacik | 63611e7 | 2019-06-18 10:59:18 -0400 | [diff] [blame] | 4114 | |
| 4115 | /* |
| 4116 | * If we have a pending delayed iput we could end up with the final iput |
| 4117 | * being run in btrfs-cleaner context. If we have enough of these built |
| 4118 | * up we can end up burning a lot of time in btrfs-cleaner without any |
| 4119 | * way to throttle the unlinks. Since we're currently holding a ref on |
| 4120 | * the inode we can run the delayed iput here without any issues as the |
| 4121 | * final iput won't be done until after we drop the ref we're currently |
| 4122 | * holding. |
| 4123 | */ |
| 4124 | btrfs_run_delayed_iput(fs_info, inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4125 | err: |
| 4126 | btrfs_free_path(path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4127 | if (ret) |
| 4128 | goto out; |
| 4129 | |
Nikolay Borisov | 6ef06d2 | 2017-02-20 13:50:34 +0200 | [diff] [blame] | 4130 | btrfs_i_size_write(dir, dir->vfs_inode.i_size - name_len * 2); |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 4131 | inode_inc_iversion(&inode->vfs_inode); |
| 4132 | inode_inc_iversion(&dir->vfs_inode); |
| 4133 | inode->vfs_inode.i_ctime = dir->vfs_inode.i_mtime = |
| 4134 | dir->vfs_inode.i_ctime = current_time(&inode->vfs_inode); |
Nikolay Borisov | 9a56fcd | 2020-11-02 16:48:59 +0200 | [diff] [blame] | 4135 | ret = btrfs_update_inode(trans, root, dir); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4136 | out: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4137 | return ret; |
| 4138 | } |
| 4139 | |
Al Viro | 9298679 | 2011-03-04 17:14:37 +0000 | [diff] [blame] | 4140 | int btrfs_unlink_inode(struct btrfs_trans_handle *trans, |
| 4141 | struct btrfs_root *root, |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 4142 | struct btrfs_inode *dir, struct btrfs_inode *inode, |
Al Viro | 9298679 | 2011-03-04 17:14:37 +0000 | [diff] [blame] | 4143 | const char *name, int name_len) |
| 4144 | { |
| 4145 | int ret; |
| 4146 | ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len); |
| 4147 | if (!ret) { |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 4148 | drop_nlink(&inode->vfs_inode); |
Nikolay Borisov | 9a56fcd | 2020-11-02 16:48:59 +0200 | [diff] [blame] | 4149 | ret = btrfs_update_inode(trans, root, inode); |
Al Viro | 9298679 | 2011-03-04 17:14:37 +0000 | [diff] [blame] | 4150 | } |
| 4151 | return ret; |
| 4152 | } |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4153 | |
| 4154 | /* |
| 4155 | * helper to start transaction for unlink and rmdir. |
| 4156 | * |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 4157 | * unlink and rmdir are special in btrfs, they do not always free space, so |
| 4158 | * if we cannot make our reservations the normal way try and see if there is |
| 4159 | * plenty of slack room in the global reserve to migrate, otherwise we cannot |
| 4160 | * allow the unlink to occur. |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4161 | */ |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 4162 | static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir) |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4163 | { |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4164 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4165 | |
Josef Bacik | e70bea5 | 2011-10-11 14:18:24 -0400 | [diff] [blame] | 4166 | /* |
| 4167 | * 1 for the possible orphan item |
| 4168 | * 1 for the dir item |
| 4169 | * 1 for the dir index |
| 4170 | * 1 for the inode ref |
Josef Bacik | e70bea5 | 2011-10-11 14:18:24 -0400 | [diff] [blame] | 4171 | * 1 for the inode |
| 4172 | */ |
Josef Bacik | 7f9fe61 | 2020-03-13 15:58:05 -0400 | [diff] [blame] | 4173 | return btrfs_start_transaction_fallback_global_rsv(root, 5); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4174 | } |
| 4175 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4176 | static int btrfs_unlink(struct inode *dir, struct dentry *dentry) |
| 4177 | { |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4178 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4179 | struct btrfs_trans_handle *trans; |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 4180 | struct inode *inode = d_inode(dentry); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4181 | int ret; |
| 4182 | |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 4183 | trans = __unlink_start_trans(dir); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4184 | if (IS_ERR(trans)) |
Josef Bacik | 5df6a9f | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4185 | return PTR_ERR(trans); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4186 | |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 4187 | btrfs_record_unlink_dir(trans, BTRFS_I(dir), BTRFS_I(d_inode(dentry)), |
| 4188 | 0); |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 4189 | |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 4190 | ret = btrfs_unlink_inode(trans, root, BTRFS_I(dir), |
| 4191 | BTRFS_I(d_inode(dentry)), dentry->d_name.name, |
| 4192 | dentry->d_name.len); |
Tsutomu Itoh | b532402 | 2011-07-19 07:27:20 +0000 | [diff] [blame] | 4193 | if (ret) |
| 4194 | goto out; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 4195 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4196 | if (inode->i_nlink == 0) { |
Nikolay Borisov | 73f2e54 | 2017-02-20 13:50:59 +0200 | [diff] [blame] | 4197 | ret = btrfs_orphan_add(trans, BTRFS_I(inode)); |
Tsutomu Itoh | b532402 | 2011-07-19 07:27:20 +0000 | [diff] [blame] | 4198 | if (ret) |
| 4199 | goto out; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4200 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 4201 | |
Tsutomu Itoh | b532402 | 2011-07-19 07:27:20 +0000 | [diff] [blame] | 4202 | out: |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 4203 | btrfs_end_transaction(trans); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4204 | btrfs_btree_balance_dirty(root->fs_info); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4205 | return ret; |
| 4206 | } |
| 4207 | |
Misono Tomohiro | f60a236 | 2018-04-18 11:34:52 +0900 | [diff] [blame] | 4208 | static int btrfs_unlink_subvol(struct btrfs_trans_handle *trans, |
Josef Bacik | 045d396 | 2019-12-18 17:20:27 -0500 | [diff] [blame] | 4209 | struct inode *dir, struct dentry *dentry) |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4210 | { |
Lu Fengqi | 401b3b1 | 2018-08-01 11:32:30 +0800 | [diff] [blame] | 4211 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Josef Bacik | 045d396 | 2019-12-18 17:20:27 -0500 | [diff] [blame] | 4212 | struct btrfs_inode *inode = BTRFS_I(d_inode(dentry)); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4213 | struct btrfs_path *path; |
| 4214 | struct extent_buffer *leaf; |
| 4215 | struct btrfs_dir_item *di; |
| 4216 | struct btrfs_key key; |
Josef Bacik | 045d396 | 2019-12-18 17:20:27 -0500 | [diff] [blame] | 4217 | const char *name = dentry->d_name.name; |
| 4218 | int name_len = dentry->d_name.len; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4219 | u64 index; |
| 4220 | int ret; |
Josef Bacik | 045d396 | 2019-12-18 17:20:27 -0500 | [diff] [blame] | 4221 | u64 objectid; |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 4222 | u64 dir_ino = btrfs_ino(BTRFS_I(dir)); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4223 | |
Josef Bacik | 045d396 | 2019-12-18 17:20:27 -0500 | [diff] [blame] | 4224 | if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID) { |
| 4225 | objectid = inode->root->root_key.objectid; |
| 4226 | } else if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) { |
| 4227 | objectid = inode->location.objectid; |
| 4228 | } else { |
| 4229 | WARN_ON(1); |
| 4230 | return -EINVAL; |
| 4231 | } |
| 4232 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4233 | path = btrfs_alloc_path(); |
| 4234 | if (!path) |
| 4235 | return -ENOMEM; |
| 4236 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4237 | di = btrfs_lookup_dir_item(trans, root, path, dir_ino, |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4238 | name, name_len, -1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4239 | if (IS_ERR_OR_NULL(di)) { |
Liu Bo | 3cf5068 | 2018-09-12 06:06:26 +0800 | [diff] [blame] | 4240 | ret = di ? PTR_ERR(di) : -ENOENT; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4241 | goto out; |
| 4242 | } |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4243 | |
| 4244 | leaf = path->nodes[0]; |
| 4245 | btrfs_dir_item_key_to_cpu(leaf, di, &key); |
| 4246 | WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid); |
| 4247 | ret = btrfs_delete_one_dir_name(trans, root, path, di); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4248 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 4249 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4250 | goto out; |
| 4251 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4252 | btrfs_release_path(path); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4253 | |
Josef Bacik | d49d328 | 2019-12-18 17:20:28 -0500 | [diff] [blame] | 4254 | /* |
| 4255 | * This is a placeholder inode for a subvolume we didn't have a |
| 4256 | * reference to at the time of the snapshot creation. In the meantime |
| 4257 | * we could have renamed the real subvol link into our snapshot, so |
David Sterba | 1a9fd41 | 2021-05-21 17:42:23 +0200 | [diff] [blame] | 4258 | * depending on btrfs_del_root_ref to return -ENOENT here is incorrect. |
Josef Bacik | d49d328 | 2019-12-18 17:20:28 -0500 | [diff] [blame] | 4259 | * Instead simply lookup the dir_index_item for this entry so we can |
| 4260 | * remove it. Otherwise we know we have a ref to the root and we can |
| 4261 | * call btrfs_del_root_ref, and it _shouldn't_ fail. |
| 4262 | */ |
| 4263 | if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) { |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4264 | di = btrfs_search_dir_index_item(root, path, dir_ino, |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4265 | name, name_len); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4266 | if (IS_ERR_OR_NULL(di)) { |
| 4267 | if (!di) |
| 4268 | ret = -ENOENT; |
| 4269 | else |
| 4270 | ret = PTR_ERR(di); |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 4271 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4272 | goto out; |
| 4273 | } |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4274 | |
| 4275 | leaf = path->nodes[0]; |
| 4276 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4277 | index = key.offset; |
Josef Bacik | d49d328 | 2019-12-18 17:20:28 -0500 | [diff] [blame] | 4278 | btrfs_release_path(path); |
| 4279 | } else { |
| 4280 | ret = btrfs_del_root_ref(trans, objectid, |
| 4281 | root->root_key.objectid, dir_ino, |
| 4282 | &index, name, name_len); |
| 4283 | if (ret) { |
| 4284 | btrfs_abort_transaction(trans, ret); |
| 4285 | goto out; |
| 4286 | } |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4287 | } |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4288 | |
Lu Fengqi | 9add294 | 2018-08-01 11:32:26 +0800 | [diff] [blame] | 4289 | ret = btrfs_delete_delayed_dir_index(trans, BTRFS_I(dir), index); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4290 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 4291 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4292 | goto out; |
| 4293 | } |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 4294 | |
Nikolay Borisov | 6ef06d2 | 2017-02-20 13:50:34 +0200 | [diff] [blame] | 4295 | btrfs_i_size_write(BTRFS_I(dir), dir->i_size - name_len * 2); |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 4296 | inode_inc_iversion(dir); |
Deepa Dinamani | c2050a4 | 2016-09-14 07:48:06 -0700 | [diff] [blame] | 4297 | dir->i_mtime = dir->i_ctime = current_time(dir); |
Nikolay Borisov | 729f796 | 2020-11-02 16:49:06 +0200 | [diff] [blame] | 4298 | ret = btrfs_update_inode_fallback(trans, root, BTRFS_I(dir)); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4299 | if (ret) |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 4300 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4301 | out: |
Josef Bacik | 71d7aed | 2011-06-14 14:24:32 -0400 | [diff] [blame] | 4302 | btrfs_free_path(path); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4303 | return ret; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4304 | } |
| 4305 | |
Misono Tomohiro | ec42f16 | 2018-04-18 11:34:13 +0900 | [diff] [blame] | 4306 | /* |
| 4307 | * Helper to check if the subvolume references other subvolumes or if it's |
| 4308 | * default. |
| 4309 | */ |
Misono Tomohiro | f60a236 | 2018-04-18 11:34:52 +0900 | [diff] [blame] | 4310 | static noinline int may_destroy_subvol(struct btrfs_root *root) |
Misono Tomohiro | ec42f16 | 2018-04-18 11:34:13 +0900 | [diff] [blame] | 4311 | { |
| 4312 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 4313 | struct btrfs_path *path; |
| 4314 | struct btrfs_dir_item *di; |
| 4315 | struct btrfs_key key; |
| 4316 | u64 dir_id; |
| 4317 | int ret; |
| 4318 | |
| 4319 | path = btrfs_alloc_path(); |
| 4320 | if (!path) |
| 4321 | return -ENOMEM; |
| 4322 | |
| 4323 | /* Make sure this root isn't set as the default subvol */ |
| 4324 | dir_id = btrfs_super_root_dir(fs_info->super_copy); |
| 4325 | di = btrfs_lookup_dir_item(NULL, fs_info->tree_root, path, |
| 4326 | dir_id, "default", 7, 0); |
| 4327 | if (di && !IS_ERR(di)) { |
| 4328 | btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key); |
| 4329 | if (key.objectid == root->root_key.objectid) { |
| 4330 | ret = -EPERM; |
| 4331 | btrfs_err(fs_info, |
| 4332 | "deleting default subvolume %llu is not allowed", |
| 4333 | key.objectid); |
| 4334 | goto out; |
| 4335 | } |
| 4336 | btrfs_release_path(path); |
| 4337 | } |
| 4338 | |
| 4339 | key.objectid = root->root_key.objectid; |
| 4340 | key.type = BTRFS_ROOT_REF_KEY; |
| 4341 | key.offset = (u64)-1; |
| 4342 | |
| 4343 | ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0); |
| 4344 | if (ret < 0) |
| 4345 | goto out; |
| 4346 | BUG_ON(ret == 0); |
| 4347 | |
| 4348 | ret = 0; |
| 4349 | if (path->slots[0] > 0) { |
| 4350 | path->slots[0]--; |
| 4351 | btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]); |
| 4352 | if (key.objectid == root->root_key.objectid && |
| 4353 | key.type == BTRFS_ROOT_REF_KEY) |
| 4354 | ret = -ENOTEMPTY; |
| 4355 | } |
| 4356 | out: |
| 4357 | btrfs_free_path(path); |
| 4358 | return ret; |
| 4359 | } |
| 4360 | |
Nikolay Borisov | 20a6800 | 2018-04-27 14:36:24 +0300 | [diff] [blame] | 4361 | /* Delete all dentries for inodes belonging to the root */ |
| 4362 | static void btrfs_prune_dentries(struct btrfs_root *root) |
| 4363 | { |
| 4364 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 4365 | struct rb_node *node; |
| 4366 | struct rb_node *prev; |
| 4367 | struct btrfs_inode *entry; |
| 4368 | struct inode *inode; |
| 4369 | u64 objectid = 0; |
| 4370 | |
| 4371 | if (!test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) |
| 4372 | WARN_ON(btrfs_root_refs(&root->root_item) != 0); |
| 4373 | |
| 4374 | spin_lock(&root->inode_lock); |
| 4375 | again: |
| 4376 | node = root->inode_tree.rb_node; |
| 4377 | prev = NULL; |
| 4378 | while (node) { |
| 4379 | prev = node; |
| 4380 | entry = rb_entry(node, struct btrfs_inode, rb_node); |
| 4381 | |
David Sterba | 3750851 | 2018-06-29 10:56:40 +0200 | [diff] [blame] | 4382 | if (objectid < btrfs_ino(entry)) |
Nikolay Borisov | 20a6800 | 2018-04-27 14:36:24 +0300 | [diff] [blame] | 4383 | node = node->rb_left; |
David Sterba | 3750851 | 2018-06-29 10:56:40 +0200 | [diff] [blame] | 4384 | else if (objectid > btrfs_ino(entry)) |
Nikolay Borisov | 20a6800 | 2018-04-27 14:36:24 +0300 | [diff] [blame] | 4385 | node = node->rb_right; |
| 4386 | else |
| 4387 | break; |
| 4388 | } |
| 4389 | if (!node) { |
| 4390 | while (prev) { |
| 4391 | entry = rb_entry(prev, struct btrfs_inode, rb_node); |
David Sterba | 3750851 | 2018-06-29 10:56:40 +0200 | [diff] [blame] | 4392 | if (objectid <= btrfs_ino(entry)) { |
Nikolay Borisov | 20a6800 | 2018-04-27 14:36:24 +0300 | [diff] [blame] | 4393 | node = prev; |
| 4394 | break; |
| 4395 | } |
| 4396 | prev = rb_next(prev); |
| 4397 | } |
| 4398 | } |
| 4399 | while (node) { |
| 4400 | entry = rb_entry(node, struct btrfs_inode, rb_node); |
David Sterba | 3750851 | 2018-06-29 10:56:40 +0200 | [diff] [blame] | 4401 | objectid = btrfs_ino(entry) + 1; |
Nikolay Borisov | 20a6800 | 2018-04-27 14:36:24 +0300 | [diff] [blame] | 4402 | inode = igrab(&entry->vfs_inode); |
| 4403 | if (inode) { |
| 4404 | spin_unlock(&root->inode_lock); |
| 4405 | if (atomic_read(&inode->i_count) > 1) |
| 4406 | d_prune_aliases(inode); |
| 4407 | /* |
| 4408 | * btrfs_drop_inode will have it removed from the inode |
| 4409 | * cache when its usage count hits zero. |
| 4410 | */ |
| 4411 | iput(inode); |
| 4412 | cond_resched(); |
| 4413 | spin_lock(&root->inode_lock); |
| 4414 | goto again; |
| 4415 | } |
| 4416 | |
| 4417 | if (cond_resched_lock(&root->inode_lock)) |
| 4418 | goto again; |
| 4419 | |
| 4420 | node = rb_next(node); |
| 4421 | } |
| 4422 | spin_unlock(&root->inode_lock); |
| 4423 | } |
| 4424 | |
Misono Tomohiro | f60a236 | 2018-04-18 11:34:52 +0900 | [diff] [blame] | 4425 | int btrfs_delete_subvolume(struct inode *dir, struct dentry *dentry) |
| 4426 | { |
| 4427 | struct btrfs_fs_info *fs_info = btrfs_sb(dentry->d_sb); |
| 4428 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 4429 | struct inode *inode = d_inode(dentry); |
| 4430 | struct btrfs_root *dest = BTRFS_I(inode)->root; |
| 4431 | struct btrfs_trans_handle *trans; |
| 4432 | struct btrfs_block_rsv block_rsv; |
| 4433 | u64 root_flags; |
Misono Tomohiro | f60a236 | 2018-04-18 11:34:52 +0900 | [diff] [blame] | 4434 | int ret; |
Misono Tomohiro | f60a236 | 2018-04-18 11:34:52 +0900 | [diff] [blame] | 4435 | |
| 4436 | /* |
| 4437 | * Don't allow to delete a subvolume with send in progress. This is |
| 4438 | * inside the inode lock so the error handling that has to drop the bit |
| 4439 | * again is not run concurrently. |
| 4440 | */ |
| 4441 | spin_lock(&dest->root_item_lock); |
Lu Fengqi | a7176f7 | 2018-08-04 21:10:53 +0800 | [diff] [blame] | 4442 | if (dest->send_in_progress) { |
Misono Tomohiro | f60a236 | 2018-04-18 11:34:52 +0900 | [diff] [blame] | 4443 | spin_unlock(&dest->root_item_lock); |
| 4444 | btrfs_warn(fs_info, |
| 4445 | "attempt to delete subvolume %llu during send", |
| 4446 | dest->root_key.objectid); |
| 4447 | return -EPERM; |
| 4448 | } |
Lu Fengqi | a7176f7 | 2018-08-04 21:10:53 +0800 | [diff] [blame] | 4449 | root_flags = btrfs_root_flags(&dest->root_item); |
| 4450 | btrfs_set_root_flags(&dest->root_item, |
| 4451 | root_flags | BTRFS_ROOT_SUBVOL_DEAD); |
| 4452 | spin_unlock(&dest->root_item_lock); |
Misono Tomohiro | f60a236 | 2018-04-18 11:34:52 +0900 | [diff] [blame] | 4453 | |
| 4454 | down_write(&fs_info->subvol_sem); |
| 4455 | |
Nikolay Borisov | ee0d904 | 2020-11-24 17:49:30 +0200 | [diff] [blame] | 4456 | ret = may_destroy_subvol(dest); |
| 4457 | if (ret) |
Misono Tomohiro | f60a236 | 2018-04-18 11:34:52 +0900 | [diff] [blame] | 4458 | goto out_up_write; |
| 4459 | |
| 4460 | btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP); |
| 4461 | /* |
| 4462 | * One for dir inode, |
| 4463 | * two for dir entries, |
| 4464 | * two for root ref/backref. |
| 4465 | */ |
Nikolay Borisov | ee0d904 | 2020-11-24 17:49:30 +0200 | [diff] [blame] | 4466 | ret = btrfs_subvolume_reserve_metadata(root, &block_rsv, 5, true); |
| 4467 | if (ret) |
Misono Tomohiro | f60a236 | 2018-04-18 11:34:52 +0900 | [diff] [blame] | 4468 | goto out_up_write; |
| 4469 | |
| 4470 | trans = btrfs_start_transaction(root, 0); |
| 4471 | if (IS_ERR(trans)) { |
Nikolay Borisov | ee0d904 | 2020-11-24 17:49:30 +0200 | [diff] [blame] | 4472 | ret = PTR_ERR(trans); |
Misono Tomohiro | f60a236 | 2018-04-18 11:34:52 +0900 | [diff] [blame] | 4473 | goto out_release; |
| 4474 | } |
| 4475 | trans->block_rsv = &block_rsv; |
| 4476 | trans->bytes_reserved = block_rsv.size; |
| 4477 | |
| 4478 | btrfs_record_snapshot_destroy(trans, BTRFS_I(dir)); |
| 4479 | |
Josef Bacik | 045d396 | 2019-12-18 17:20:27 -0500 | [diff] [blame] | 4480 | ret = btrfs_unlink_subvol(trans, dir, dentry); |
Misono Tomohiro | f60a236 | 2018-04-18 11:34:52 +0900 | [diff] [blame] | 4481 | if (ret) { |
Misono Tomohiro | f60a236 | 2018-04-18 11:34:52 +0900 | [diff] [blame] | 4482 | btrfs_abort_transaction(trans, ret); |
| 4483 | goto out_end_trans; |
| 4484 | } |
| 4485 | |
Josef Bacik | 2731f51 | 2021-03-12 15:25:04 -0500 | [diff] [blame] | 4486 | ret = btrfs_record_root_in_trans(trans, dest); |
| 4487 | if (ret) { |
| 4488 | btrfs_abort_transaction(trans, ret); |
| 4489 | goto out_end_trans; |
| 4490 | } |
Misono Tomohiro | f60a236 | 2018-04-18 11:34:52 +0900 | [diff] [blame] | 4491 | |
| 4492 | memset(&dest->root_item.drop_progress, 0, |
| 4493 | sizeof(dest->root_item.drop_progress)); |
David Sterba | c842268 | 2020-09-15 21:44:52 +0200 | [diff] [blame] | 4494 | btrfs_set_root_drop_level(&dest->root_item, 0); |
Misono Tomohiro | f60a236 | 2018-04-18 11:34:52 +0900 | [diff] [blame] | 4495 | btrfs_set_root_refs(&dest->root_item, 0); |
| 4496 | |
| 4497 | if (!test_and_set_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &dest->state)) { |
| 4498 | ret = btrfs_insert_orphan_item(trans, |
| 4499 | fs_info->tree_root, |
| 4500 | dest->root_key.objectid); |
| 4501 | if (ret) { |
| 4502 | btrfs_abort_transaction(trans, ret); |
Misono Tomohiro | f60a236 | 2018-04-18 11:34:52 +0900 | [diff] [blame] | 4503 | goto out_end_trans; |
| 4504 | } |
| 4505 | } |
| 4506 | |
Lu Fengqi | d195779 | 2018-05-29 15:01:54 +0800 | [diff] [blame] | 4507 | ret = btrfs_uuid_tree_remove(trans, dest->root_item.uuid, |
Misono Tomohiro | f60a236 | 2018-04-18 11:34:52 +0900 | [diff] [blame] | 4508 | BTRFS_UUID_KEY_SUBVOL, |
| 4509 | dest->root_key.objectid); |
| 4510 | if (ret && ret != -ENOENT) { |
| 4511 | btrfs_abort_transaction(trans, ret); |
Misono Tomohiro | f60a236 | 2018-04-18 11:34:52 +0900 | [diff] [blame] | 4512 | goto out_end_trans; |
| 4513 | } |
| 4514 | if (!btrfs_is_empty_uuid(dest->root_item.received_uuid)) { |
Lu Fengqi | d195779 | 2018-05-29 15:01:54 +0800 | [diff] [blame] | 4515 | ret = btrfs_uuid_tree_remove(trans, |
Misono Tomohiro | f60a236 | 2018-04-18 11:34:52 +0900 | [diff] [blame] | 4516 | dest->root_item.received_uuid, |
| 4517 | BTRFS_UUID_KEY_RECEIVED_SUBVOL, |
| 4518 | dest->root_key.objectid); |
| 4519 | if (ret && ret != -ENOENT) { |
| 4520 | btrfs_abort_transaction(trans, ret); |
Misono Tomohiro | f60a236 | 2018-04-18 11:34:52 +0900 | [diff] [blame] | 4521 | goto out_end_trans; |
| 4522 | } |
| 4523 | } |
| 4524 | |
Qu Wenruo | 082b6c9 | 2020-06-16 10:17:37 +0800 | [diff] [blame] | 4525 | free_anon_bdev(dest->anon_dev); |
| 4526 | dest->anon_dev = 0; |
Misono Tomohiro | f60a236 | 2018-04-18 11:34:52 +0900 | [diff] [blame] | 4527 | out_end_trans: |
| 4528 | trans->block_rsv = NULL; |
| 4529 | trans->bytes_reserved = 0; |
| 4530 | ret = btrfs_end_transaction(trans); |
Misono Tomohiro | f60a236 | 2018-04-18 11:34:52 +0900 | [diff] [blame] | 4531 | inode->i_flags |= S_DEAD; |
| 4532 | out_release: |
Qu Wenruo | e85fde5 | 2020-07-24 14:46:10 +0800 | [diff] [blame] | 4533 | btrfs_subvolume_release_metadata(root, &block_rsv); |
Misono Tomohiro | f60a236 | 2018-04-18 11:34:52 +0900 | [diff] [blame] | 4534 | out_up_write: |
| 4535 | up_write(&fs_info->subvol_sem); |
Nikolay Borisov | ee0d904 | 2020-11-24 17:49:30 +0200 | [diff] [blame] | 4536 | if (ret) { |
Misono Tomohiro | f60a236 | 2018-04-18 11:34:52 +0900 | [diff] [blame] | 4537 | spin_lock(&dest->root_item_lock); |
| 4538 | root_flags = btrfs_root_flags(&dest->root_item); |
| 4539 | btrfs_set_root_flags(&dest->root_item, |
| 4540 | root_flags & ~BTRFS_ROOT_SUBVOL_DEAD); |
| 4541 | spin_unlock(&dest->root_item_lock); |
| 4542 | } else { |
| 4543 | d_invalidate(dentry); |
Nikolay Borisov | 20a6800 | 2018-04-27 14:36:24 +0300 | [diff] [blame] | 4544 | btrfs_prune_dentries(dest); |
Misono Tomohiro | f60a236 | 2018-04-18 11:34:52 +0900 | [diff] [blame] | 4545 | ASSERT(dest->send_in_progress == 0); |
Misono Tomohiro | f60a236 | 2018-04-18 11:34:52 +0900 | [diff] [blame] | 4546 | } |
| 4547 | |
Nikolay Borisov | ee0d904 | 2020-11-24 17:49:30 +0200 | [diff] [blame] | 4548 | return ret; |
Misono Tomohiro | f60a236 | 2018-04-18 11:34:52 +0900 | [diff] [blame] | 4549 | } |
| 4550 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4551 | static int btrfs_rmdir(struct inode *dir, struct dentry *dentry) |
| 4552 | { |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 4553 | struct inode *inode = d_inode(dentry); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 4554 | int err = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4555 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4556 | struct btrfs_trans_handle *trans; |
Filipe Manana | 44f714d | 2016-06-06 16:11:13 +0100 | [diff] [blame] | 4557 | u64 last_unlink_trans; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4558 | |
David Sterba | b3ae244 | 2012-09-13 16:04:34 -0600 | [diff] [blame] | 4559 | if (inode->i_size > BTRFS_EMPTY_DIR_SIZE) |
Yan | 134d451 | 2007-10-25 15:49:25 -0400 | [diff] [blame] | 4560 | return -ENOTEMPTY; |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 4561 | if (btrfs_ino(BTRFS_I(inode)) == BTRFS_FIRST_FREE_OBJECTID) |
Misono Tomohiro | a79a464 | 2018-04-18 11:35:31 +0900 | [diff] [blame] | 4562 | return btrfs_delete_subvolume(dir, dentry); |
Yan | 134d451 | 2007-10-25 15:49:25 -0400 | [diff] [blame] | 4563 | |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 4564 | trans = __unlink_start_trans(dir); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4565 | if (IS_ERR(trans)) |
Josef Bacik | 5df6a9f | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4566 | return PTR_ERR(trans); |
Josef Bacik | 5df6a9f | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4567 | |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 4568 | if (unlikely(btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) { |
Josef Bacik | 045d396 | 2019-12-18 17:20:27 -0500 | [diff] [blame] | 4569 | err = btrfs_unlink_subvol(trans, dir, dentry); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4570 | goto out; |
| 4571 | } |
| 4572 | |
Nikolay Borisov | 73f2e54 | 2017-02-20 13:50:59 +0200 | [diff] [blame] | 4573 | err = btrfs_orphan_add(trans, BTRFS_I(inode)); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 4574 | if (err) |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4575 | goto out; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 4576 | |
Filipe Manana | 44f714d | 2016-06-06 16:11:13 +0100 | [diff] [blame] | 4577 | last_unlink_trans = BTRFS_I(inode)->last_unlink_trans; |
| 4578 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4579 | /* now the directory is empty */ |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 4580 | err = btrfs_unlink_inode(trans, root, BTRFS_I(dir), |
| 4581 | BTRFS_I(d_inode(dentry)), dentry->d_name.name, |
| 4582 | dentry->d_name.len); |
Filipe Manana | 44f714d | 2016-06-06 16:11:13 +0100 | [diff] [blame] | 4583 | if (!err) { |
Nikolay Borisov | 6ef06d2 | 2017-02-20 13:50:34 +0200 | [diff] [blame] | 4584 | btrfs_i_size_write(BTRFS_I(inode), 0); |
Filipe Manana | 44f714d | 2016-06-06 16:11:13 +0100 | [diff] [blame] | 4585 | /* |
| 4586 | * Propagate the last_unlink_trans value of the deleted dir to |
| 4587 | * its parent directory. This is to prevent an unrecoverable |
| 4588 | * log tree in the case we do something like this: |
| 4589 | * 1) create dir foo |
| 4590 | * 2) create snapshot under dir foo |
| 4591 | * 3) delete the snapshot |
| 4592 | * 4) rmdir foo |
| 4593 | * 5) mkdir foo |
| 4594 | * 6) fsync foo or some file inside foo |
| 4595 | */ |
| 4596 | if (last_unlink_trans >= trans->transid) |
| 4597 | BTRFS_I(dir)->last_unlink_trans = last_unlink_trans; |
| 4598 | } |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4599 | out: |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 4600 | btrfs_end_transaction(trans); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4601 | btrfs_btree_balance_dirty(root->fs_info); |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 4602 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4603 | return err; |
| 4604 | } |
| 4605 | |
Josef Bacik | ddfae63 | 2017-10-19 14:16:02 -0400 | [diff] [blame] | 4606 | /* |
| 4607 | * Return this if we need to call truncate_block for the last bit of the |
| 4608 | * truncate. |
| 4609 | */ |
| 4610 | #define NEED_TRUNCATE_BLOCK 1 |
Filipe Manana | 0305cd5 | 2015-10-16 12:34:25 +0100 | [diff] [blame] | 4611 | |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 4612 | /* |
Filipe Manana | 4f7e673 | 2021-05-24 11:35:54 +0100 | [diff] [blame] | 4613 | * Remove inode items from a given root. |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4614 | * |
Filipe Manana | 4f7e673 | 2021-05-24 11:35:54 +0100 | [diff] [blame] | 4615 | * @trans: A transaction handle. |
| 4616 | * @root: The root from which to remove items. |
| 4617 | * @inode: The inode whose items we want to remove. |
| 4618 | * @new_size: The new i_size for the inode. This is only applicable when |
| 4619 | * @min_type is BTRFS_EXTENT_DATA_KEY, must be 0 otherwise. |
| 4620 | * @min_type: The minimum key type to remove. All keys with a type |
| 4621 | * greater than this value are removed and all keys with |
| 4622 | * this type are removed only if their offset is >= @new_size. |
Filipe Manana | 0d7d316 | 2021-05-24 11:35:55 +0100 | [diff] [blame] | 4623 | * @extents_found: Output parameter that will contain the number of file |
| 4624 | * extent items that were removed or adjusted to the new |
| 4625 | * inode i_size. The caller is responsible for initializing |
| 4626 | * the counter. Also, it can be NULL if the caller does not |
| 4627 | * need this counter. |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 4628 | * |
Filipe Manana | 4f7e673 | 2021-05-24 11:35:54 +0100 | [diff] [blame] | 4629 | * Remove all keys associated with the inode from the given root that have a key |
| 4630 | * with a type greater than or equals to @min_type. When @min_type has a value of |
| 4631 | * BTRFS_EXTENT_DATA_KEY, only remove file extent items that have an offset value |
| 4632 | * greater than or equals to @new_size. If a file extent item that starts before |
| 4633 | * @new_size and ends after it is found, its length is adjusted. |
| 4634 | * |
| 4635 | * Returns: 0 on success, < 0 on error and NEED_TRUNCATE_BLOCK when @min_type is |
| 4636 | * BTRFS_EXTENT_DATA_KEY and the caller must truncate the last block. |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4637 | */ |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4638 | int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans, |
| 4639 | struct btrfs_root *root, |
Nikolay Borisov | 5074339 | 2020-11-02 16:48:55 +0200 | [diff] [blame] | 4640 | struct btrfs_inode *inode, |
Filipe Manana | 0d7d316 | 2021-05-24 11:35:55 +0100 | [diff] [blame] | 4641 | u64 new_size, u32 min_type, |
| 4642 | u64 *extents_found) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4643 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4644 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4645 | struct btrfs_path *path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4646 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4647 | struct btrfs_file_extent_item *fi; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4648 | struct btrfs_key key; |
| 4649 | struct btrfs_key found_key; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4650 | u64 extent_start = 0; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4651 | u64 extent_num_bytes = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4652 | u64 extent_offset = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4653 | u64 item_end = 0; |
Filipe Manana | c1aa457 | 2015-06-20 18:20:09 +0100 | [diff] [blame] | 4654 | u64 last_size = new_size; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4655 | u32 found_type = (u8)-1; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4656 | int found_extent; |
| 4657 | int del_item; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4658 | int pending_del_nr = 0; |
| 4659 | int pending_del_slot = 0; |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4660 | int extent_type = -1; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4661 | int ret; |
Nikolay Borisov | 5074339 | 2020-11-02 16:48:55 +0200 | [diff] [blame] | 4662 | u64 ino = btrfs_ino(inode); |
Chris Mason | 28ed134 | 2014-12-17 09:41:04 -0800 | [diff] [blame] | 4663 | u64 bytes_deleted = 0; |
Thomas Meyer | 897ca81 | 2017-10-07 16:02:21 +0200 | [diff] [blame] | 4664 | bool be_nice = false; |
| 4665 | bool should_throttle = false; |
Filipe Manana | 28553fa | 2020-02-07 12:23:09 +0000 | [diff] [blame] | 4666 | const u64 lock_start = ALIGN_DOWN(new_size, fs_info->sectorsize); |
| 4667 | struct extent_state *cached_state = NULL; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4668 | |
| 4669 | BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4670 | |
Chris Mason | 28ed134 | 2014-12-17 09:41:04 -0800 | [diff] [blame] | 4671 | /* |
Qu Wenruo | 92a7cc4 | 2020-05-15 14:01:40 +0800 | [diff] [blame] | 4672 | * For non-free space inodes and non-shareable roots, we want to back |
| 4673 | * off from time to time. This means all inodes in subvolume roots, |
| 4674 | * reloc roots, and data reloc roots. |
Chris Mason | 28ed134 | 2014-12-17 09:41:04 -0800 | [diff] [blame] | 4675 | */ |
Nikolay Borisov | 5074339 | 2020-11-02 16:48:55 +0200 | [diff] [blame] | 4676 | if (!btrfs_is_free_space_inode(inode) && |
Qu Wenruo | 92a7cc4 | 2020-05-15 14:01:40 +0800 | [diff] [blame] | 4677 | test_bit(BTRFS_ROOT_SHAREABLE, &root->state)) |
Thomas Meyer | 897ca81 | 2017-10-07 16:02:21 +0200 | [diff] [blame] | 4678 | be_nice = true; |
Chris Mason | 28ed134 | 2014-12-17 09:41:04 -0800 | [diff] [blame] | 4679 | |
Mark Fasheh | 0eb0e19 | 2011-07-12 16:44:10 -0700 | [diff] [blame] | 4680 | path = btrfs_alloc_path(); |
| 4681 | if (!path) |
| 4682 | return -ENOMEM; |
David Sterba | e4058b5 | 2015-11-27 16:31:35 +0100 | [diff] [blame] | 4683 | path->reada = READA_BACK; |
Mark Fasheh | 0eb0e19 | 2011-07-12 16:44:10 -0700 | [diff] [blame] | 4684 | |
Qu Wenruo | 82028e0a | 2020-05-15 14:01:41 +0800 | [diff] [blame] | 4685 | if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) { |
Nikolay Borisov | 5074339 | 2020-11-02 16:48:55 +0200 | [diff] [blame] | 4686 | lock_extent_bits(&inode->io_tree, lock_start, (u64)-1, |
Filipe Manana | a5ae50d | 2020-02-20 13:29:49 +0000 | [diff] [blame] | 4687 | &cached_state); |
Filipe Manana | 28553fa | 2020-02-07 12:23:09 +0000 | [diff] [blame] | 4688 | |
Qu Wenruo | 82028e0a | 2020-05-15 14:01:41 +0800 | [diff] [blame] | 4689 | /* |
| 4690 | * We want to drop from the next block forward in case this |
| 4691 | * new size is not block aligned since we will be keeping the |
| 4692 | * last block of the extent just the way it is. |
| 4693 | */ |
Nikolay Borisov | 5074339 | 2020-11-02 16:48:55 +0200 | [diff] [blame] | 4694 | btrfs_drop_extent_cache(inode, ALIGN(new_size, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4695 | fs_info->sectorsize), |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 4696 | (u64)-1, 0); |
Qu Wenruo | 82028e0a | 2020-05-15 14:01:41 +0800 | [diff] [blame] | 4697 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4698 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 4699 | /* |
| 4700 | * This function is also used to drop the items in the log tree before |
| 4701 | * we relog the inode, so if root != BTRFS_I(inode)->root, it means |
Andrea Gelmini | 52042d8 | 2018-11-28 12:05:13 +0100 | [diff] [blame] | 4702 | * it is used to drop the logged items. So we shouldn't kill the delayed |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 4703 | * items. |
| 4704 | */ |
Nikolay Borisov | 5074339 | 2020-11-02 16:48:55 +0200 | [diff] [blame] | 4705 | if (min_type == 0 && root == inode->root) |
| 4706 | btrfs_kill_delayed_inode_items(inode); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 4707 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4708 | key.objectid = ino; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4709 | key.offset = (u64)-1; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4710 | key.type = (u8)-1; |
| 4711 | |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4712 | search_again: |
Chris Mason | 28ed134 | 2014-12-17 09:41:04 -0800 | [diff] [blame] | 4713 | /* |
| 4714 | * with a 16K leaf size and 128MB extents, you can actually queue |
| 4715 | * up a huge file in a single leaf. Most of the time that |
| 4716 | * bytes_deleted is > 0, it will be huge by the time we get here |
| 4717 | */ |
Omar Sandoval | fd86a3a | 2018-05-11 13:13:30 -0700 | [diff] [blame] | 4718 | if (be_nice && bytes_deleted > SZ_32M && |
| 4719 | btrfs_should_end_transaction(trans)) { |
| 4720 | ret = -EAGAIN; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4721 | goto out; |
| 4722 | } |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4723 | |
Omar Sandoval | fd86a3a | 2018-05-11 13:13:30 -0700 | [diff] [blame] | 4724 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 4725 | if (ret < 0) |
| 4726 | goto out; |
| 4727 | |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4728 | if (ret > 0) { |
Omar Sandoval | fd86a3a | 2018-05-11 13:13:30 -0700 | [diff] [blame] | 4729 | ret = 0; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4730 | /* there are no items in the tree for us to truncate, we're |
| 4731 | * done |
| 4732 | */ |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4733 | if (path->slots[0] == 0) |
| 4734 | goto out; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4735 | path->slots[0]--; |
| 4736 | } |
| 4737 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4738 | while (1) { |
Josef Bacik | 9ddc959 | 2020-01-17 09:02:22 -0500 | [diff] [blame] | 4739 | u64 clear_start = 0, clear_len = 0; |
| 4740 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4741 | fi = NULL; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4742 | leaf = path->nodes[0]; |
| 4743 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 4744 | found_type = found_key.type; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4745 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4746 | if (found_key.objectid != ino) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4747 | break; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4748 | |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4749 | if (found_type < min_type) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4750 | break; |
| 4751 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4752 | item_end = found_key.offset; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4753 | if (found_type == BTRFS_EXTENT_DATA_KEY) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4754 | fi = btrfs_item_ptr(leaf, path->slots[0], |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4755 | struct btrfs_file_extent_item); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4756 | extent_type = btrfs_file_extent_type(leaf, fi); |
| 4757 | if (extent_type != BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4758 | item_end += |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4759 | btrfs_file_extent_num_bytes(leaf, fi); |
Liu Bo | 09ed2f1 | 2017-03-10 11:09:48 -0800 | [diff] [blame] | 4760 | |
| 4761 | trace_btrfs_truncate_show_fi_regular( |
Nikolay Borisov | 5074339 | 2020-11-02 16:48:55 +0200 | [diff] [blame] | 4762 | inode, leaf, fi, found_key.offset); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4763 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
Qu Wenruo | e41ca58 | 2018-06-06 15:41:49 +0800 | [diff] [blame] | 4764 | item_end += btrfs_file_extent_ram_bytes(leaf, |
| 4765 | fi); |
Liu Bo | 09ed2f1 | 2017-03-10 11:09:48 -0800 | [diff] [blame] | 4766 | |
| 4767 | trace_btrfs_truncate_show_fi_inline( |
Nikolay Borisov | 5074339 | 2020-11-02 16:48:55 +0200 | [diff] [blame] | 4768 | inode, leaf, fi, path->slots[0], |
Liu Bo | 09ed2f1 | 2017-03-10 11:09:48 -0800 | [diff] [blame] | 4769 | found_key.offset); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4770 | } |
Yan | 008630c | 2007-11-07 13:31:09 -0500 | [diff] [blame] | 4771 | item_end--; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4772 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4773 | if (found_type > min_type) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4774 | del_item = 1; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4775 | } else { |
Filipe Manana | 76b42ab | 2017-02-14 16:56:01 +0000 | [diff] [blame] | 4776 | if (item_end < new_size) |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4777 | break; |
| 4778 | if (found_key.offset >= new_size) |
| 4779 | del_item = 1; |
| 4780 | else |
| 4781 | del_item = 0; |
| 4782 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4783 | found_extent = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4784 | /* FIXME, shrink the extent if the ref count is only 1 */ |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4785 | if (found_type != BTRFS_EXTENT_DATA_KEY) |
| 4786 | goto delete; |
| 4787 | |
Filipe Manana | 0d7d316 | 2021-05-24 11:35:55 +0100 | [diff] [blame] | 4788 | if (extents_found != NULL) |
| 4789 | (*extents_found)++; |
| 4790 | |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4791 | if (extent_type != BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4792 | u64 num_dec; |
Josef Bacik | 9ddc959 | 2020-01-17 09:02:22 -0500 | [diff] [blame] | 4793 | |
| 4794 | clear_start = found_key.offset; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4795 | extent_start = btrfs_file_extent_disk_bytenr(leaf, fi); |
Miao Xie | f70a9a6b | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 4796 | if (!del_item) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4797 | u64 orig_num_bytes = |
| 4798 | btrfs_file_extent_num_bytes(leaf, fi); |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 4799 | extent_num_bytes = ALIGN(new_size - |
| 4800 | found_key.offset, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4801 | fs_info->sectorsize); |
Josef Bacik | 9ddc959 | 2020-01-17 09:02:22 -0500 | [diff] [blame] | 4802 | clear_start = ALIGN(new_size, fs_info->sectorsize); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4803 | btrfs_set_file_extent_num_bytes(leaf, fi, |
| 4804 | extent_num_bytes); |
| 4805 | num_dec = (orig_num_bytes - |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 4806 | extent_num_bytes); |
Qu Wenruo | 92a7cc4 | 2020-05-15 14:01:40 +0800 | [diff] [blame] | 4807 | if (test_bit(BTRFS_ROOT_SHAREABLE, |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 4808 | &root->state) && |
| 4809 | extent_start != 0) |
Nikolay Borisov | 5074339 | 2020-11-02 16:48:55 +0200 | [diff] [blame] | 4810 | inode_sub_bytes(&inode->vfs_inode, |
| 4811 | num_dec); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4812 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4813 | } else { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4814 | extent_num_bytes = |
| 4815 | btrfs_file_extent_disk_num_bytes(leaf, |
| 4816 | fi); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4817 | extent_offset = found_key.offset - |
| 4818 | btrfs_file_extent_offset(leaf, fi); |
| 4819 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4820 | /* FIXME blocksize != 4096 */ |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 4821 | num_dec = btrfs_file_extent_num_bytes(leaf, fi); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4822 | if (extent_start != 0) { |
| 4823 | found_extent = 1; |
Qu Wenruo | 92a7cc4 | 2020-05-15 14:01:40 +0800 | [diff] [blame] | 4824 | if (test_bit(BTRFS_ROOT_SHAREABLE, |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 4825 | &root->state)) |
Nikolay Borisov | 5074339 | 2020-11-02 16:48:55 +0200 | [diff] [blame] | 4826 | inode_sub_bytes(&inode->vfs_inode, |
| 4827 | num_dec); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4828 | } |
| 4829 | } |
Josef Bacik | 9ddc959 | 2020-01-17 09:02:22 -0500 | [diff] [blame] | 4830 | clear_len = num_dec; |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 4831 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4832 | /* |
| 4833 | * we can't truncate inline items that have had |
| 4834 | * special encodings |
| 4835 | */ |
| 4836 | if (!del_item && |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4837 | btrfs_file_extent_encryption(leaf, fi) == 0 && |
Josef Bacik | ddfae63 | 2017-10-19 14:16:02 -0400 | [diff] [blame] | 4838 | btrfs_file_extent_other_encoding(leaf, fi) == 0 && |
| 4839 | btrfs_file_extent_compression(leaf, fi) == 0) { |
| 4840 | u32 size = (u32)(new_size - found_key.offset); |
Chris Mason | 514ac8a | 2014-01-03 21:07:00 -0800 | [diff] [blame] | 4841 | |
Josef Bacik | ddfae63 | 2017-10-19 14:16:02 -0400 | [diff] [blame] | 4842 | btrfs_set_file_extent_ram_bytes(leaf, fi, size); |
| 4843 | size = btrfs_file_extent_calc_inline_size(size); |
David Sterba | 78ac4f9 | 2019-03-20 14:49:12 +0100 | [diff] [blame] | 4844 | btrfs_truncate_item(path, size, 1); |
Josef Bacik | ddfae63 | 2017-10-19 14:16:02 -0400 | [diff] [blame] | 4845 | } else if (!del_item) { |
Chris Mason | 514ac8a | 2014-01-03 21:07:00 -0800 | [diff] [blame] | 4846 | /* |
Josef Bacik | ddfae63 | 2017-10-19 14:16:02 -0400 | [diff] [blame] | 4847 | * We have to bail so the last_size is set to |
| 4848 | * just before this extent. |
Chris Mason | 514ac8a | 2014-01-03 21:07:00 -0800 | [diff] [blame] | 4849 | */ |
Omar Sandoval | fd86a3a | 2018-05-11 13:13:30 -0700 | [diff] [blame] | 4850 | ret = NEED_TRUNCATE_BLOCK; |
Josef Bacik | ddfae63 | 2017-10-19 14:16:02 -0400 | [diff] [blame] | 4851 | break; |
Josef Bacik | 9ddc959 | 2020-01-17 09:02:22 -0500 | [diff] [blame] | 4852 | } else { |
| 4853 | /* |
| 4854 | * Inline extents are special, we just treat |
| 4855 | * them as a full sector worth in the file |
| 4856 | * extent tree just for simplicity sake. |
| 4857 | */ |
| 4858 | clear_len = fs_info->sectorsize; |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 4859 | } |
Josef Bacik | ddfae63 | 2017-10-19 14:16:02 -0400 | [diff] [blame] | 4860 | |
Qu Wenruo | 92a7cc4 | 2020-05-15 14:01:40 +0800 | [diff] [blame] | 4861 | if (test_bit(BTRFS_ROOT_SHAREABLE, &root->state)) |
Nikolay Borisov | 5074339 | 2020-11-02 16:48:55 +0200 | [diff] [blame] | 4862 | inode_sub_bytes(&inode->vfs_inode, |
| 4863 | item_end + 1 - new_size); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4864 | } |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4865 | delete: |
Josef Bacik | 9ddc959 | 2020-01-17 09:02:22 -0500 | [diff] [blame] | 4866 | /* |
| 4867 | * We use btrfs_truncate_inode_items() to clean up log trees for |
| 4868 | * multiple fsyncs, and in this case we don't want to clear the |
| 4869 | * file extent range because it's just the log. |
| 4870 | */ |
Nikolay Borisov | 5074339 | 2020-11-02 16:48:55 +0200 | [diff] [blame] | 4871 | if (root == inode->root) { |
| 4872 | ret = btrfs_inode_clear_file_extent_range(inode, |
Josef Bacik | 9ddc959 | 2020-01-17 09:02:22 -0500 | [diff] [blame] | 4873 | clear_start, clear_len); |
| 4874 | if (ret) { |
| 4875 | btrfs_abort_transaction(trans, ret); |
| 4876 | break; |
| 4877 | } |
| 4878 | } |
| 4879 | |
Josef Bacik | ddfae63 | 2017-10-19 14:16:02 -0400 | [diff] [blame] | 4880 | if (del_item) |
| 4881 | last_size = found_key.offset; |
| 4882 | else |
| 4883 | last_size = new_size; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4884 | if (del_item) { |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4885 | if (!pending_del_nr) { |
| 4886 | /* no pending yet, add ourselves */ |
| 4887 | pending_del_slot = path->slots[0]; |
| 4888 | pending_del_nr = 1; |
| 4889 | } else if (pending_del_nr && |
| 4890 | path->slots[0] + 1 == pending_del_slot) { |
| 4891 | /* hop on the pending chunk */ |
| 4892 | pending_del_nr++; |
| 4893 | pending_del_slot = path->slots[0]; |
| 4894 | } else { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4895 | BUG(); |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4896 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4897 | } else { |
| 4898 | break; |
| 4899 | } |
Thomas Meyer | 897ca81 | 2017-10-07 16:02:21 +0200 | [diff] [blame] | 4900 | should_throttle = false; |
Chris Mason | 28f75a0 | 2015-02-04 06:59:29 -0800 | [diff] [blame] | 4901 | |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 4902 | if (found_extent && |
Qu Wenruo | 82028e0a | 2020-05-15 14:01:41 +0800 | [diff] [blame] | 4903 | root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) { |
Qu Wenruo | ffd4bb2 | 2019-04-04 14:45:36 +0800 | [diff] [blame] | 4904 | struct btrfs_ref ref = { 0 }; |
| 4905 | |
Chris Mason | 28ed134 | 2014-12-17 09:41:04 -0800 | [diff] [blame] | 4906 | bytes_deleted += extent_num_bytes; |
Qu Wenruo | ffd4bb2 | 2019-04-04 14:45:36 +0800 | [diff] [blame] | 4907 | |
| 4908 | btrfs_init_generic_ref(&ref, BTRFS_DROP_DELAYED_REF, |
| 4909 | extent_start, extent_num_bytes, 0); |
| 4910 | ref.real_root = root->root_key.objectid; |
| 4911 | btrfs_init_data_ref(&ref, btrfs_header_owner(leaf), |
| 4912 | ino, extent_offset); |
| 4913 | ret = btrfs_free_extent(trans, &ref); |
Omar Sandoval | 0552210 | 2018-05-11 13:13:31 -0700 | [diff] [blame] | 4914 | if (ret) { |
| 4915 | btrfs_abort_transaction(trans, ret); |
| 4916 | break; |
| 4917 | } |
Chris Mason | 28f75a0 | 2015-02-04 06:59:29 -0800 | [diff] [blame] | 4918 | if (be_nice) { |
Lu Fengqi | 7c86162 | 2018-10-11 13:40:36 +0800 | [diff] [blame] | 4919 | if (btrfs_should_throttle_delayed_refs(trans)) |
Thomas Meyer | 897ca81 | 2017-10-07 16:02:21 +0200 | [diff] [blame] | 4920 | should_throttle = true; |
Chris Mason | 28f75a0 | 2015-02-04 06:59:29 -0800 | [diff] [blame] | 4921 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4922 | } |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4923 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4924 | if (found_type == BTRFS_INODE_ITEM_KEY) |
| 4925 | break; |
| 4926 | |
| 4927 | if (path->slots[0] == 0 || |
Josef Bacik | 1262133 | 2015-02-03 07:50:16 -0800 | [diff] [blame] | 4928 | path->slots[0] != pending_del_slot || |
Josef Bacik | 28bad21 | 2018-12-03 10:20:38 -0500 | [diff] [blame] | 4929 | should_throttle) { |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4930 | if (pending_del_nr) { |
| 4931 | ret = btrfs_del_items(trans, root, path, |
| 4932 | pending_del_slot, |
| 4933 | pending_del_nr); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4934 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 4935 | btrfs_abort_transaction(trans, ret); |
Omar Sandoval | fd86a3a | 2018-05-11 13:13:30 -0700 | [diff] [blame] | 4936 | break; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4937 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4938 | pending_del_nr = 0; |
| 4939 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4940 | btrfs_release_path(path); |
Josef Bacik | 28bad21 | 2018-12-03 10:20:38 -0500 | [diff] [blame] | 4941 | |
Chris Mason | 28f75a0 | 2015-02-04 06:59:29 -0800 | [diff] [blame] | 4942 | /* |
Josef Bacik | 28bad21 | 2018-12-03 10:20:38 -0500 | [diff] [blame] | 4943 | * We can generate a lot of delayed refs, so we need to |
| 4944 | * throttle every once and a while and make sure we're |
| 4945 | * adding enough space to keep up with the work we are |
| 4946 | * generating. Since we hold a transaction here we |
| 4947 | * can't flush, and we don't want to FLUSH_LIMIT because |
| 4948 | * we could have generated too many delayed refs to |
| 4949 | * actually allocate, so just bail if we're short and |
| 4950 | * let the normal reservation dance happen higher up. |
Chris Mason | 28f75a0 | 2015-02-04 06:59:29 -0800 | [diff] [blame] | 4951 | */ |
Josef Bacik | 28bad21 | 2018-12-03 10:20:38 -0500 | [diff] [blame] | 4952 | if (should_throttle) { |
| 4953 | ret = btrfs_delayed_refs_rsv_refill(fs_info, |
| 4954 | BTRFS_RESERVE_NO_FLUSH); |
| 4955 | if (ret) { |
| 4956 | ret = -EAGAIN; |
| 4957 | break; |
| 4958 | } |
Chris Mason | 28f75a0 | 2015-02-04 06:59:29 -0800 | [diff] [blame] | 4959 | } |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4960 | goto search_again; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4961 | } else { |
| 4962 | path->slots[0]--; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4963 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4964 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4965 | out: |
Omar Sandoval | fd86a3a | 2018-05-11 13:13:30 -0700 | [diff] [blame] | 4966 | if (ret >= 0 && pending_del_nr) { |
| 4967 | int err; |
| 4968 | |
| 4969 | err = btrfs_del_items(trans, root, path, pending_del_slot, |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4970 | pending_del_nr); |
Omar Sandoval | fd86a3a | 2018-05-11 13:13:30 -0700 | [diff] [blame] | 4971 | if (err) { |
| 4972 | btrfs_abort_transaction(trans, err); |
| 4973 | ret = err; |
| 4974 | } |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4975 | } |
Filipe Manana | 76b42ab | 2017-02-14 16:56:01 +0000 | [diff] [blame] | 4976 | if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) { |
| 4977 | ASSERT(last_size >= new_size); |
Omar Sandoval | fd86a3a | 2018-05-11 13:13:30 -0700 | [diff] [blame] | 4978 | if (!ret && last_size > new_size) |
Filipe Manana | 76b42ab | 2017-02-14 16:56:01 +0000 | [diff] [blame] | 4979 | last_size = new_size; |
Nikolay Borisov | 5074339 | 2020-11-02 16:48:55 +0200 | [diff] [blame] | 4980 | btrfs_inode_safe_disk_i_size_write(inode, last_size); |
| 4981 | unlock_extent_cached(&inode->io_tree, lock_start, (u64)-1, |
| 4982 | &cached_state); |
Filipe Manana | 76b42ab | 2017-02-14 16:56:01 +0000 | [diff] [blame] | 4983 | } |
Chris Mason | 28ed134 | 2014-12-17 09:41:04 -0800 | [diff] [blame] | 4984 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4985 | btrfs_free_path(path); |
Omar Sandoval | fd86a3a | 2018-05-11 13:13:30 -0700 | [diff] [blame] | 4986 | return ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4987 | } |
| 4988 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4989 | /* |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4990 | * btrfs_truncate_block - read, zero a chunk and write a block |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 4991 | * @inode - inode that we're zeroing |
| 4992 | * @from - the offset to start zeroing |
| 4993 | * @len - the length to zero, 0 to zero the entire range respective to the |
| 4994 | * offset |
| 4995 | * @front - zero up to the offset instead of from the offset on |
| 4996 | * |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4997 | * This will find the block for the "from" offset and cow the block and zero the |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 4998 | * part we want to zero. This is used with truncate and hole punching. |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4999 | */ |
Nikolay Borisov | 217f42e | 2020-11-02 16:49:03 +0200 | [diff] [blame] | 5000 | int btrfs_truncate_block(struct btrfs_inode *inode, loff_t from, loff_t len, |
| 5001 | int front) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5002 | { |
Nikolay Borisov | 217f42e | 2020-11-02 16:49:03 +0200 | [diff] [blame] | 5003 | struct btrfs_fs_info *fs_info = inode->root->fs_info; |
| 5004 | struct address_space *mapping = inode->vfs_inode.i_mapping; |
| 5005 | struct extent_io_tree *io_tree = &inode->io_tree; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5006 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 5007 | struct extent_state *cached_state = NULL; |
Qu Wenruo | 364ecf3 | 2017-02-27 15:10:38 +0800 | [diff] [blame] | 5008 | struct extent_changeset *data_reserved = NULL; |
Qu Wenruo | 6d4572a | 2020-06-24 07:23:50 +0800 | [diff] [blame] | 5009 | bool only_release_metadata = false; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5010 | u32 blocksize = fs_info->sectorsize; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5011 | pgoff_t index = from >> PAGE_SHIFT; |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 5012 | unsigned offset = from & (blocksize - 1); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5013 | struct page *page; |
Josef Bacik | 3b16a4e | 2011-09-21 15:05:58 -0400 | [diff] [blame] | 5014 | gfp_t mask = btrfs_alloc_write_mask(mapping); |
Qu Wenruo | 6d4572a | 2020-06-24 07:23:50 +0800 | [diff] [blame] | 5015 | size_t write_bytes = blocksize; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5016 | int ret = 0; |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 5017 | u64 block_start; |
| 5018 | u64 block_end; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5019 | |
Nikolay Borisov | b03ebd9 | 2018-01-18 14:47:06 +0200 | [diff] [blame] | 5020 | if (IS_ALIGNED(offset, blocksize) && |
| 5021 | (!len || IS_ALIGNED(len, blocksize))) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5022 | goto out; |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 5023 | |
Josef Bacik | 8b62f87 | 2017-10-19 14:15:55 -0400 | [diff] [blame] | 5024 | block_start = round_down(from, blocksize); |
| 5025 | block_end = block_start + blocksize - 1; |
| 5026 | |
Nikolay Borisov | 217f42e | 2020-11-02 16:49:03 +0200 | [diff] [blame] | 5027 | ret = btrfs_check_data_free_space(inode, &data_reserved, block_start, |
| 5028 | blocksize); |
Qu Wenruo | 6d4572a | 2020-06-24 07:23:50 +0800 | [diff] [blame] | 5029 | if (ret < 0) { |
Nikolay Borisov | 217f42e | 2020-11-02 16:49:03 +0200 | [diff] [blame] | 5030 | if (btrfs_check_nocow_lock(inode, block_start, &write_bytes) > 0) { |
Qu Wenruo | 6d4572a | 2020-06-24 07:23:50 +0800 | [diff] [blame] | 5031 | /* For nocow case, no need to reserve data space */ |
| 5032 | only_release_metadata = true; |
| 5033 | } else { |
| 5034 | goto out; |
| 5035 | } |
| 5036 | } |
Nikolay Borisov | 217f42e | 2020-11-02 16:49:03 +0200 | [diff] [blame] | 5037 | ret = btrfs_delalloc_reserve_metadata(inode, blocksize); |
Qu Wenruo | 6d4572a | 2020-06-24 07:23:50 +0800 | [diff] [blame] | 5038 | if (ret < 0) { |
| 5039 | if (!only_release_metadata) |
Nikolay Borisov | 217f42e | 2020-11-02 16:49:03 +0200 | [diff] [blame] | 5040 | btrfs_free_reserved_data_space(inode, data_reserved, |
| 5041 | block_start, blocksize); |
Qu Wenruo | 6d4572a | 2020-06-24 07:23:50 +0800 | [diff] [blame] | 5042 | goto out; |
| 5043 | } |
Chris Mason | 211c17f | 2008-05-15 09:13:45 -0400 | [diff] [blame] | 5044 | again: |
Josef Bacik | 3b16a4e | 2011-09-21 15:05:58 -0400 | [diff] [blame] | 5045 | page = find_or_create_page(mapping, index, mask); |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 5046 | if (!page) { |
Nikolay Borisov | 217f42e | 2020-11-02 16:49:03 +0200 | [diff] [blame] | 5047 | btrfs_delalloc_release_space(inode, data_reserved, block_start, |
| 5048 | blocksize, true); |
| 5049 | btrfs_delalloc_release_extents(inode, blocksize); |
Miao Xie | ac6a2b3 | 2012-12-05 10:56:13 +0000 | [diff] [blame] | 5050 | ret = -ENOMEM; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5051 | goto out; |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 5052 | } |
Qu Wenruo | 32443de | 2021-01-26 16:34:00 +0800 | [diff] [blame] | 5053 | ret = set_page_extent_mapped(page); |
| 5054 | if (ret < 0) |
| 5055 | goto out_unlock; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5056 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5057 | if (!PageUptodate(page)) { |
| 5058 | ret = btrfs_readpage(NULL, page); |
| 5059 | lock_page(page); |
Chris Mason | 211c17f | 2008-05-15 09:13:45 -0400 | [diff] [blame] | 5060 | if (page->mapping != mapping) { |
| 5061 | unlock_page(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5062 | put_page(page); |
Chris Mason | 211c17f | 2008-05-15 09:13:45 -0400 | [diff] [blame] | 5063 | goto again; |
| 5064 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5065 | if (!PageUptodate(page)) { |
| 5066 | ret = -EIO; |
Chris Mason | 8964222 | 2008-07-24 09:41:53 -0400 | [diff] [blame] | 5067 | goto out_unlock; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5068 | } |
| 5069 | } |
Chris Mason | 211c17f | 2008-05-15 09:13:45 -0400 | [diff] [blame] | 5070 | wait_on_page_writeback(page); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5071 | |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 5072 | lock_extent_bits(io_tree, block_start, block_end, &cached_state); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5073 | |
Nikolay Borisov | 217f42e | 2020-11-02 16:49:03 +0200 | [diff] [blame] | 5074 | ordered = btrfs_lookup_ordered_extent(inode, block_start); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5075 | if (ordered) { |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 5076 | unlock_extent_cached(io_tree, block_start, block_end, |
David Sterba | e43bbe5 | 2017-12-12 21:43:52 +0100 | [diff] [blame] | 5077 | &cached_state); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5078 | unlock_page(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5079 | put_page(page); |
Nikolay Borisov | c0a4360 | 2020-09-18 12:15:53 +0300 | [diff] [blame] | 5080 | btrfs_start_ordered_extent(ordered, 1); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5081 | btrfs_put_ordered_extent(ordered); |
| 5082 | goto again; |
| 5083 | } |
| 5084 | |
Nikolay Borisov | 217f42e | 2020-11-02 16:49:03 +0200 | [diff] [blame] | 5085 | clear_extent_bit(&inode->io_tree, block_start, block_end, |
Omar Sandoval | e182163 | 2019-08-15 14:04:04 -0700 | [diff] [blame] | 5086 | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, |
| 5087 | 0, 0, &cached_state); |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 5088 | |
Nikolay Borisov | 217f42e | 2020-11-02 16:49:03 +0200 | [diff] [blame] | 5089 | ret = btrfs_set_extent_delalloc(inode, block_start, block_end, 0, |
Nikolay Borisov | 330a582 | 2019-07-17 16:18:17 +0300 | [diff] [blame] | 5090 | &cached_state); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 5091 | if (ret) { |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 5092 | unlock_extent_cached(io_tree, block_start, block_end, |
David Sterba | e43bbe5 | 2017-12-12 21:43:52 +0100 | [diff] [blame] | 5093 | &cached_state); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 5094 | goto out_unlock; |
| 5095 | } |
| 5096 | |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 5097 | if (offset != blocksize) { |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 5098 | if (!len) |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 5099 | len = blocksize - offset; |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 5100 | if (front) |
Ira Weiny | d048b9c | 2021-05-04 18:40:07 -0700 | [diff] [blame] | 5101 | memzero_page(page, (block_start - page_offset(page)), |
| 5102 | offset); |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 5103 | else |
Ira Weiny | d048b9c | 2021-05-04 18:40:07 -0700 | [diff] [blame] | 5104 | memzero_page(page, (block_start - page_offset(page)) + offset, |
| 5105 | len); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5106 | flush_dcache_page(page); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5107 | } |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 5108 | ClearPageChecked(page); |
Qu Wenruo | 6c9ac8b | 2021-05-31 16:50:51 +0800 | [diff] [blame] | 5109 | btrfs_page_set_dirty(fs_info, page, block_start, block_end + 1 - block_start); |
David Sterba | e43bbe5 | 2017-12-12 21:43:52 +0100 | [diff] [blame] | 5110 | unlock_extent_cached(io_tree, block_start, block_end, &cached_state); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5111 | |
Qu Wenruo | 6d4572a | 2020-06-24 07:23:50 +0800 | [diff] [blame] | 5112 | if (only_release_metadata) |
Nikolay Borisov | 217f42e | 2020-11-02 16:49:03 +0200 | [diff] [blame] | 5113 | set_extent_bit(&inode->io_tree, block_start, block_end, |
Nikolay Borisov | 1cab5e7 | 2020-11-05 11:08:00 +0200 | [diff] [blame] | 5114 | EXTENT_NORESERVE, 0, NULL, NULL, GFP_NOFS, NULL); |
Qu Wenruo | 6d4572a | 2020-06-24 07:23:50 +0800 | [diff] [blame] | 5115 | |
Chris Mason | 8964222 | 2008-07-24 09:41:53 -0400 | [diff] [blame] | 5116 | out_unlock: |
Qu Wenruo | 6d4572a | 2020-06-24 07:23:50 +0800 | [diff] [blame] | 5117 | if (ret) { |
| 5118 | if (only_release_metadata) |
Nikolay Borisov | 217f42e | 2020-11-02 16:49:03 +0200 | [diff] [blame] | 5119 | btrfs_delalloc_release_metadata(inode, blocksize, true); |
Qu Wenruo | 6d4572a | 2020-06-24 07:23:50 +0800 | [diff] [blame] | 5120 | else |
Nikolay Borisov | 217f42e | 2020-11-02 16:49:03 +0200 | [diff] [blame] | 5121 | btrfs_delalloc_release_space(inode, data_reserved, |
Qu Wenruo | 6d4572a | 2020-06-24 07:23:50 +0800 | [diff] [blame] | 5122 | block_start, blocksize, true); |
| 5123 | } |
Nikolay Borisov | 217f42e | 2020-11-02 16:49:03 +0200 | [diff] [blame] | 5124 | btrfs_delalloc_release_extents(inode, blocksize); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5125 | unlock_page(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5126 | put_page(page); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5127 | out: |
Qu Wenruo | 6d4572a | 2020-06-24 07:23:50 +0800 | [diff] [blame] | 5128 | if (only_release_metadata) |
Nikolay Borisov | 217f42e | 2020-11-02 16:49:03 +0200 | [diff] [blame] | 5129 | btrfs_check_nocow_unlock(inode); |
Qu Wenruo | 364ecf3 | 2017-02-27 15:10:38 +0800 | [diff] [blame] | 5130 | extent_changeset_free(data_reserved); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5131 | return ret; |
| 5132 | } |
| 5133 | |
Nikolay Borisov | a4ba6cc | 2020-11-02 16:49:00 +0200 | [diff] [blame] | 5134 | static int maybe_insert_hole(struct btrfs_root *root, struct btrfs_inode *inode, |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 5135 | u64 offset, u64 len) |
| 5136 | { |
Nikolay Borisov | a4ba6cc | 2020-11-02 16:49:00 +0200 | [diff] [blame] | 5137 | struct btrfs_fs_info *fs_info = root->fs_info; |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 5138 | struct btrfs_trans_handle *trans; |
Filipe Manana | 5893dfb | 2020-11-04 11:07:32 +0000 | [diff] [blame] | 5139 | struct btrfs_drop_extents_args drop_args = { 0 }; |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 5140 | int ret; |
| 5141 | |
| 5142 | /* |
Filipe Manana | cceaa89 | 2021-07-20 16:03:40 +0100 | [diff] [blame] | 5143 | * If NO_HOLES is enabled, we don't need to do anything. |
| 5144 | * Later, up in the call chain, either btrfs_set_inode_last_sub_trans() |
| 5145 | * or btrfs_update_inode() will be called, which guarantee that the next |
| 5146 | * fsync will know this inode was changed and needs to be logged. |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 5147 | */ |
Filipe Manana | cceaa89 | 2021-07-20 16:03:40 +0100 | [diff] [blame] | 5148 | if (btrfs_fs_incompat(fs_info, NO_HOLES)) |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 5149 | return 0; |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 5150 | |
| 5151 | /* |
| 5152 | * 1 - for the one we're dropping |
| 5153 | * 1 - for the one we're adding |
| 5154 | * 1 - for updating the inode. |
| 5155 | */ |
| 5156 | trans = btrfs_start_transaction(root, 3); |
| 5157 | if (IS_ERR(trans)) |
| 5158 | return PTR_ERR(trans); |
| 5159 | |
Filipe Manana | 5893dfb | 2020-11-04 11:07:32 +0000 | [diff] [blame] | 5160 | drop_args.start = offset; |
| 5161 | drop_args.end = offset + len; |
| 5162 | drop_args.drop_cache = true; |
| 5163 | |
Nikolay Borisov | a4ba6cc | 2020-11-02 16:49:00 +0200 | [diff] [blame] | 5164 | ret = btrfs_drop_extents(trans, root, inode, &drop_args); |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 5165 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 5166 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 5167 | btrfs_end_transaction(trans); |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 5168 | return ret; |
| 5169 | } |
| 5170 | |
Nikolay Borisov | a4ba6cc | 2020-11-02 16:49:00 +0200 | [diff] [blame] | 5171 | ret = btrfs_insert_file_extent(trans, root, btrfs_ino(inode), |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 5172 | offset, 0, 0, len, 0, len, 0, 0, 0); |
Filipe Manana | 2766ff6 | 2020-11-04 11:07:34 +0000 | [diff] [blame] | 5173 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 5174 | btrfs_abort_transaction(trans, ret); |
Filipe Manana | 2766ff6 | 2020-11-04 11:07:34 +0000 | [diff] [blame] | 5175 | } else { |
Nikolay Borisov | a4ba6cc | 2020-11-02 16:49:00 +0200 | [diff] [blame] | 5176 | btrfs_update_inode_bytes(inode, 0, drop_args.bytes_found); |
| 5177 | btrfs_update_inode(trans, root, inode); |
Filipe Manana | 2766ff6 | 2020-11-04 11:07:34 +0000 | [diff] [blame] | 5178 | } |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 5179 | btrfs_end_transaction(trans); |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 5180 | return ret; |
| 5181 | } |
| 5182 | |
Josef Bacik | 695a0d0 | 2011-03-04 15:46:53 -0500 | [diff] [blame] | 5183 | /* |
| 5184 | * This function puts in dummy file extents for the area we're creating a hole |
| 5185 | * for. So if we are truncating this file to a larger size we need to insert |
| 5186 | * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for |
| 5187 | * the range between oldsize and size |
| 5188 | */ |
Nikolay Borisov | b06359a | 2020-11-02 16:49:04 +0200 | [diff] [blame] | 5189 | int btrfs_cont_expand(struct btrfs_inode *inode, loff_t oldsize, loff_t size) |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 5190 | { |
Nikolay Borisov | b06359a | 2020-11-02 16:49:04 +0200 | [diff] [blame] | 5191 | struct btrfs_root *root = inode->root; |
| 5192 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 5193 | struct extent_io_tree *io_tree = &inode->io_tree; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 5194 | struct extent_map *em = NULL; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 5195 | struct extent_state *cached_state = NULL; |
Nikolay Borisov | b06359a | 2020-11-02 16:49:04 +0200 | [diff] [blame] | 5196 | struct extent_map_tree *em_tree = &inode->extent_tree; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5197 | u64 hole_start = ALIGN(oldsize, fs_info->sectorsize); |
| 5198 | u64 block_end = ALIGN(size, fs_info->sectorsize); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 5199 | u64 last_byte; |
| 5200 | u64 cur_offset; |
| 5201 | u64 hole_size; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 5202 | int err = 0; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 5203 | |
Josef Bacik | a71754f | 2013-06-17 17:14:39 -0400 | [diff] [blame] | 5204 | /* |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 5205 | * If our size started in the middle of a block we need to zero out the |
| 5206 | * rest of the block before we expand the i_size, otherwise we could |
Josef Bacik | a71754f | 2013-06-17 17:14:39 -0400 | [diff] [blame] | 5207 | * expose stale data. |
| 5208 | */ |
Nikolay Borisov | b06359a | 2020-11-02 16:49:04 +0200 | [diff] [blame] | 5209 | err = btrfs_truncate_block(inode, oldsize, 0, 0); |
Josef Bacik | a71754f | 2013-06-17 17:14:39 -0400 | [diff] [blame] | 5210 | if (err) |
| 5211 | return err; |
| 5212 | |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 5213 | if (size <= hole_start) |
| 5214 | return 0; |
| 5215 | |
Nikolay Borisov | b06359a | 2020-11-02 16:49:04 +0200 | [diff] [blame] | 5216 | btrfs_lock_and_flush_ordered_range(inode, hole_start, block_end - 1, |
| 5217 | &cached_state); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 5218 | cur_offset = hole_start; |
| 5219 | while (1) { |
Nikolay Borisov | b06359a | 2020-11-02 16:49:04 +0200 | [diff] [blame] | 5220 | em = btrfs_get_extent(inode, NULL, 0, cur_offset, |
Omar Sandoval | 39b07b5 | 2019-12-02 17:34:23 -0800 | [diff] [blame] | 5221 | block_end - cur_offset); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5222 | if (IS_ERR(em)) { |
| 5223 | err = PTR_ERR(em); |
Zach Brown | f276795 | 2013-01-08 19:37:58 +0000 | [diff] [blame] | 5224 | em = NULL; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5225 | break; |
| 5226 | } |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 5227 | last_byte = min(extent_map_end(em), block_end); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5228 | last_byte = ALIGN(last_byte, fs_info->sectorsize); |
Josef Bacik | 9ddc959 | 2020-01-17 09:02:22 -0500 | [diff] [blame] | 5229 | hole_size = last_byte - cur_offset; |
| 5230 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5231 | if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) { |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 5232 | struct extent_map *hole_em; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5233 | |
Nikolay Borisov | b06359a | 2020-11-02 16:49:04 +0200 | [diff] [blame] | 5234 | err = maybe_insert_hole(root, inode, cur_offset, |
| 5235 | hole_size); |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 5236 | if (err) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 5237 | break; |
Josef Bacik | 9ddc959 | 2020-01-17 09:02:22 -0500 | [diff] [blame] | 5238 | |
Nikolay Borisov | b06359a | 2020-11-02 16:49:04 +0200 | [diff] [blame] | 5239 | err = btrfs_inode_set_file_extent_range(inode, |
Josef Bacik | 9ddc959 | 2020-01-17 09:02:22 -0500 | [diff] [blame] | 5240 | cur_offset, hole_size); |
| 5241 | if (err) |
| 5242 | break; |
| 5243 | |
Nikolay Borisov | b06359a | 2020-11-02 16:49:04 +0200 | [diff] [blame] | 5244 | btrfs_drop_extent_cache(inode, cur_offset, |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 5245 | cur_offset + hole_size - 1, 0); |
| 5246 | hole_em = alloc_extent_map(); |
| 5247 | if (!hole_em) { |
| 5248 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, |
Nikolay Borisov | b06359a | 2020-11-02 16:49:04 +0200 | [diff] [blame] | 5249 | &inode->runtime_flags); |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 5250 | goto next; |
| 5251 | } |
| 5252 | hole_em->start = cur_offset; |
| 5253 | hole_em->len = hole_size; |
| 5254 | hole_em->orig_start = cur_offset; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5255 | |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 5256 | hole_em->block_start = EXTENT_MAP_HOLE; |
| 5257 | hole_em->block_len = 0; |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 5258 | hole_em->orig_block_len = 0; |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 5259 | hole_em->ram_bytes = hole_size; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 5260 | hole_em->compress_type = BTRFS_COMPRESS_NONE; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5261 | hole_em->generation = fs_info->generation; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 5262 | |
| 5263 | while (1) { |
| 5264 | write_lock(&em_tree->lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 5265 | err = add_extent_mapping(em_tree, hole_em, 1); |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 5266 | write_unlock(&em_tree->lock); |
| 5267 | if (err != -EEXIST) |
| 5268 | break; |
Nikolay Borisov | b06359a | 2020-11-02 16:49:04 +0200 | [diff] [blame] | 5269 | btrfs_drop_extent_cache(inode, cur_offset, |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 5270 | cur_offset + |
| 5271 | hole_size - 1, 0); |
| 5272 | } |
| 5273 | free_extent_map(hole_em); |
Josef Bacik | 9ddc959 | 2020-01-17 09:02:22 -0500 | [diff] [blame] | 5274 | } else { |
Nikolay Borisov | b06359a | 2020-11-02 16:49:04 +0200 | [diff] [blame] | 5275 | err = btrfs_inode_set_file_extent_range(inode, |
Josef Bacik | 9ddc959 | 2020-01-17 09:02:22 -0500 | [diff] [blame] | 5276 | cur_offset, hole_size); |
| 5277 | if (err) |
| 5278 | break; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 5279 | } |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 5280 | next: |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 5281 | free_extent_map(em); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 5282 | em = NULL; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 5283 | cur_offset = last_byte; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5284 | if (cur_offset >= block_end) |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 5285 | break; |
| 5286 | } |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 5287 | free_extent_map(em); |
David Sterba | e43bbe5 | 2017-12-12 21:43:52 +0100 | [diff] [blame] | 5288 | unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 5289 | return err; |
| 5290 | } |
| 5291 | |
Eric Sandeen | 3972f26 | 2013-01-12 02:57:22 +0000 | [diff] [blame] | 5292 | static int btrfs_setsize(struct inode *inode, struct iattr *attr) |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5293 | { |
Miao Xie | f4a2f4c | 2011-12-14 20:12:01 -0500 | [diff] [blame] | 5294 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5295 | struct btrfs_trans_handle *trans; |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 5296 | loff_t oldsize = i_size_read(inode); |
Eric Sandeen | 3972f26 | 2013-01-12 02:57:22 +0000 | [diff] [blame] | 5297 | loff_t newsize = attr->ia_size; |
| 5298 | int mask = attr->ia_valid; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5299 | int ret; |
| 5300 | |
Eric Sandeen | 3972f26 | 2013-01-12 02:57:22 +0000 | [diff] [blame] | 5301 | /* |
| 5302 | * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a |
| 5303 | * special case where we need to update the times despite not having |
| 5304 | * these flags set. For all other operations the VFS set these flags |
| 5305 | * explicitly if it wants a timestamp update. |
| 5306 | */ |
Christoph Hellwig | dff6efc | 2013-11-19 07:17:07 -0800 | [diff] [blame] | 5307 | if (newsize != oldsize) { |
| 5308 | inode_inc_iversion(inode); |
| 5309 | if (!(mask & (ATTR_CTIME | ATTR_MTIME))) |
| 5310 | inode->i_ctime = inode->i_mtime = |
Deepa Dinamani | c2050a4 | 2016-09-14 07:48:06 -0700 | [diff] [blame] | 5311 | current_time(inode); |
Christoph Hellwig | dff6efc | 2013-11-19 07:17:07 -0800 | [diff] [blame] | 5312 | } |
Eric Sandeen | 3972f26 | 2013-01-12 02:57:22 +0000 | [diff] [blame] | 5313 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 5314 | if (newsize > oldsize) { |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 5315 | /* |
David Sterba | ea14b57f | 2017-06-22 02:19:11 +0200 | [diff] [blame] | 5316 | * Don't do an expanding truncate while snapshotting is ongoing. |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 5317 | * This is to ensure the snapshot captures a fully consistent |
| 5318 | * state of this file - if the snapshot captures this expanding |
| 5319 | * truncation, it must capture all writes that happened before |
| 5320 | * this truncation. |
| 5321 | */ |
Nikolay Borisov | dcc3eb9 | 2020-01-30 14:59:45 +0200 | [diff] [blame] | 5322 | btrfs_drew_write_lock(&root->snapshot_lock); |
Nikolay Borisov | b06359a | 2020-11-02 16:49:04 +0200 | [diff] [blame] | 5323 | ret = btrfs_cont_expand(BTRFS_I(inode), oldsize, newsize); |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 5324 | if (ret) { |
Nikolay Borisov | dcc3eb9 | 2020-01-30 14:59:45 +0200 | [diff] [blame] | 5325 | btrfs_drew_write_unlock(&root->snapshot_lock); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5326 | return ret; |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 5327 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5328 | |
Miao Xie | f4a2f4c | 2011-12-14 20:12:01 -0500 | [diff] [blame] | 5329 | trans = btrfs_start_transaction(root, 1); |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 5330 | if (IS_ERR(trans)) { |
Nikolay Borisov | dcc3eb9 | 2020-01-30 14:59:45 +0200 | [diff] [blame] | 5331 | btrfs_drew_write_unlock(&root->snapshot_lock); |
Miao Xie | f4a2f4c | 2011-12-14 20:12:01 -0500 | [diff] [blame] | 5332 | return PTR_ERR(trans); |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 5333 | } |
Miao Xie | f4a2f4c | 2011-12-14 20:12:01 -0500 | [diff] [blame] | 5334 | |
| 5335 | i_size_write(inode, newsize); |
Nikolay Borisov | 76aea53 | 2020-11-02 16:48:53 +0200 | [diff] [blame] | 5336 | btrfs_inode_safe_disk_i_size_write(BTRFS_I(inode), 0); |
Chandan Rajendra | 27772b6 | 2016-01-21 15:56:03 +0530 | [diff] [blame] | 5337 | pagecache_isize_extended(inode, oldsize, newsize); |
Nikolay Borisov | 9a56fcd | 2020-11-02 16:48:59 +0200 | [diff] [blame] | 5338 | ret = btrfs_update_inode(trans, root, BTRFS_I(inode)); |
Nikolay Borisov | dcc3eb9 | 2020-01-30 14:59:45 +0200 | [diff] [blame] | 5339 | btrfs_drew_write_unlock(&root->snapshot_lock); |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 5340 | btrfs_end_transaction(trans); |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 5341 | } else { |
Naohiro Aota | 24c0a72 | 2021-02-04 19:22:09 +0900 | [diff] [blame] | 5342 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
| 5343 | |
| 5344 | if (btrfs_is_zoned(fs_info)) { |
| 5345 | ret = btrfs_wait_ordered_range(inode, |
| 5346 | ALIGN(newsize, fs_info->sectorsize), |
| 5347 | (u64)-1); |
| 5348 | if (ret) |
| 5349 | return ret; |
| 5350 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5351 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 5352 | /* |
| 5353 | * We're truncating a file that used to have good data down to |
Nikolay Borisov | 1fd4033 | 2020-10-01 09:40:39 +0300 | [diff] [blame] | 5354 | * zero. Make sure any new writes to the file get on disk |
| 5355 | * on close. |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 5356 | */ |
| 5357 | if (newsize == 0) |
Nikolay Borisov | 1fd4033 | 2020-10-01 09:40:39 +0300 | [diff] [blame] | 5358 | set_bit(BTRFS_INODE_FLUSH_ON_CLOSE, |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 5359 | &BTRFS_I(inode)->runtime_flags); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5360 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 5361 | truncate_setsize(inode, newsize); |
Miao Xie | 2e60a51 | 2013-02-08 07:01:08 +0000 | [diff] [blame] | 5362 | |
Miao Xie | 2e60a51 | 2013-02-08 07:01:08 +0000 | [diff] [blame] | 5363 | inode_dio_wait(inode); |
Miao Xie | 2e60a51 | 2013-02-08 07:01:08 +0000 | [diff] [blame] | 5364 | |
Filipe Manana | 213e8c5 | 2018-02-06 20:40:31 +0000 | [diff] [blame] | 5365 | ret = btrfs_truncate(inode, newsize == oldsize); |
Josef Bacik | 7f4f6e0 | 2013-08-29 16:43:28 -0400 | [diff] [blame] | 5366 | if (ret && inode->i_nlink) { |
| 5367 | int err; |
| 5368 | |
| 5369 | /* |
Omar Sandoval | f7e9e8f | 2018-05-11 13:13:32 -0700 | [diff] [blame] | 5370 | * Truncate failed, so fix up the in-memory size. We |
| 5371 | * adjusted disk_i_size down as we removed extents, so |
| 5372 | * wait for disk_i_size to be stable and then update the |
| 5373 | * in-memory size to match. |
Josef Bacik | 7f4f6e0 | 2013-08-29 16:43:28 -0400 | [diff] [blame] | 5374 | */ |
Omar Sandoval | f7e9e8f | 2018-05-11 13:13:32 -0700 | [diff] [blame] | 5375 | err = btrfs_wait_ordered_range(inode, 0, (u64)-1); |
Josef Bacik | 7f4f6e0 | 2013-08-29 16:43:28 -0400 | [diff] [blame] | 5376 | if (err) |
Omar Sandoval | f7e9e8f | 2018-05-11 13:13:32 -0700 | [diff] [blame] | 5377 | return err; |
| 5378 | i_size_write(inode, BTRFS_I(inode)->disk_i_size); |
Josef Bacik | 7f4f6e0 | 2013-08-29 16:43:28 -0400 | [diff] [blame] | 5379 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5380 | } |
| 5381 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 5382 | return ret; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5383 | } |
| 5384 | |
Christian Brauner | 549c729 | 2021-01-21 14:19:43 +0100 | [diff] [blame] | 5385 | static int btrfs_setattr(struct user_namespace *mnt_userns, struct dentry *dentry, |
| 5386 | struct iattr *attr) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5387 | { |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 5388 | struct inode *inode = d_inode(dentry); |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 5389 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5390 | int err; |
| 5391 | |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 5392 | if (btrfs_root_readonly(root)) |
| 5393 | return -EROFS; |
| 5394 | |
Christian Brauner | d4d0946 | 2021-07-27 12:48:49 +0200 | [diff] [blame^] | 5395 | err = setattr_prepare(mnt_userns, dentry, attr); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5396 | if (err) |
| 5397 | return err; |
| 5398 | |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 5399 | if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) { |
Eric Sandeen | 3972f26 | 2013-01-12 02:57:22 +0000 | [diff] [blame] | 5400 | err = btrfs_setsize(inode, attr); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5401 | if (err) |
| 5402 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5403 | } |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 5404 | |
Christoph Hellwig | 1025774 | 2010-06-04 11:30:02 +0200 | [diff] [blame] | 5405 | if (attr->ia_valid) { |
Christian Brauner | d4d0946 | 2021-07-27 12:48:49 +0200 | [diff] [blame^] | 5406 | setattr_copy(mnt_userns, inode, attr); |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 5407 | inode_inc_iversion(inode); |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 5408 | err = btrfs_dirty_inode(inode); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 5409 | |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 5410 | if (!err && attr->ia_valid & ATTR_MODE) |
Christian Brauner | d4d0946 | 2021-07-27 12:48:49 +0200 | [diff] [blame^] | 5411 | err = posix_acl_chmod(mnt_userns, inode, inode->i_mode); |
Christoph Hellwig | 1025774 | 2010-06-04 11:30:02 +0200 | [diff] [blame] | 5412 | } |
| 5413 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5414 | return err; |
| 5415 | } |
Chris Mason | 61295eb | 2008-01-14 16:24:38 -0500 | [diff] [blame] | 5416 | |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5417 | /* |
| 5418 | * While truncating the inode pages during eviction, we get the VFS calling |
| 5419 | * btrfs_invalidatepage() against each page of the inode. This is slow because |
| 5420 | * the calls to btrfs_invalidatepage() result in a huge amount of calls to |
| 5421 | * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting |
| 5422 | * extent_state structures over and over, wasting lots of time. |
| 5423 | * |
| 5424 | * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all |
| 5425 | * those expensive operations on a per page basis and do only the ordered io |
| 5426 | * finishing, while we release here the extent_map and extent_state structures, |
| 5427 | * without the excessive merging and splitting. |
| 5428 | */ |
| 5429 | static void evict_inode_truncate_pages(struct inode *inode) |
| 5430 | { |
| 5431 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
| 5432 | struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree; |
| 5433 | struct rb_node *node; |
| 5434 | |
| 5435 | ASSERT(inode->i_state & I_FREEING); |
Johannes Weiner | 91b0abe | 2014-04-03 14:47:49 -0700 | [diff] [blame] | 5436 | truncate_inode_pages_final(&inode->i_data); |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5437 | |
| 5438 | write_lock(&map_tree->lock); |
Liu Bo | 07e1ce0 | 2018-08-23 03:51:52 +0800 | [diff] [blame] | 5439 | while (!RB_EMPTY_ROOT(&map_tree->map.rb_root)) { |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5440 | struct extent_map *em; |
| 5441 | |
Liu Bo | 07e1ce0 | 2018-08-23 03:51:52 +0800 | [diff] [blame] | 5442 | node = rb_first_cached(&map_tree->map); |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5443 | em = rb_entry(node, struct extent_map, rb_node); |
Wang Shilong | 180589e | 2013-12-14 15:27:31 +0800 | [diff] [blame] | 5444 | clear_bit(EXTENT_FLAG_PINNED, &em->flags); |
| 5445 | clear_bit(EXTENT_FLAG_LOGGING, &em->flags); |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5446 | remove_extent_mapping(map_tree, em); |
| 5447 | free_extent_map(em); |
Filipe Manana | 7064dd5 | 2014-08-08 02:47:05 +0100 | [diff] [blame] | 5448 | if (need_resched()) { |
| 5449 | write_unlock(&map_tree->lock); |
| 5450 | cond_resched(); |
| 5451 | write_lock(&map_tree->lock); |
| 5452 | } |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5453 | } |
| 5454 | write_unlock(&map_tree->lock); |
| 5455 | |
Filipe Manana | 6ca0709 | 2015-05-26 00:55:42 +0100 | [diff] [blame] | 5456 | /* |
| 5457 | * Keep looping until we have no more ranges in the io tree. |
Matthew Wilcox (Oracle) | ba206a0 | 2020-06-01 21:47:05 -0700 | [diff] [blame] | 5458 | * We can have ongoing bios started by readahead that have |
| 5459 | * their endio callback (extent_io.c:end_bio_extent_readpage) |
Filipe Manana | 9c6429d | 2015-06-10 12:55:41 +0100 | [diff] [blame] | 5460 | * still in progress (unlocked the pages in the bio but did not yet |
| 5461 | * unlocked the ranges in the io tree). Therefore this means some |
Filipe Manana | 6ca0709 | 2015-05-26 00:55:42 +0100 | [diff] [blame] | 5462 | * ranges can still be locked and eviction started because before |
| 5463 | * submitting those bios, which are executed by a separate task (work |
| 5464 | * queue kthread), inode references (inode->i_count) were not taken |
| 5465 | * (which would be dropped in the end io callback of each bio). |
| 5466 | * Therefore here we effectively end up waiting for those bios and |
| 5467 | * anyone else holding locked ranges without having bumped the inode's |
| 5468 | * reference count - if we don't do it, when they access the inode's |
| 5469 | * io_tree to unlock a range it may be too late, leading to an |
| 5470 | * use-after-free issue. |
| 5471 | */ |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5472 | spin_lock(&io_tree->lock); |
| 5473 | while (!RB_EMPTY_ROOT(&io_tree->state)) { |
| 5474 | struct extent_state *state; |
| 5475 | struct extent_state *cached_state = NULL; |
Filipe Manana | 6ca0709 | 2015-05-26 00:55:42 +0100 | [diff] [blame] | 5476 | u64 start; |
| 5477 | u64 end; |
Filipe Manana | 421f092 | 2018-10-12 13:02:48 +0100 | [diff] [blame] | 5478 | unsigned state_flags; |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5479 | |
| 5480 | node = rb_first(&io_tree->state); |
| 5481 | state = rb_entry(node, struct extent_state, rb_node); |
Filipe Manana | 6ca0709 | 2015-05-26 00:55:42 +0100 | [diff] [blame] | 5482 | start = state->start; |
| 5483 | end = state->end; |
Filipe Manana | 421f092 | 2018-10-12 13:02:48 +0100 | [diff] [blame] | 5484 | state_flags = state->state; |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5485 | spin_unlock(&io_tree->lock); |
| 5486 | |
David Sterba | ff13db4 | 2015-12-03 14:30:40 +0100 | [diff] [blame] | 5487 | lock_extent_bits(io_tree, start, end, &cached_state); |
Qu Wenruo | b9d0b38 | 2015-09-29 10:35:16 +0800 | [diff] [blame] | 5488 | |
| 5489 | /* |
| 5490 | * If still has DELALLOC flag, the extent didn't reach disk, |
| 5491 | * and its reserved space won't be freed by delayed_ref. |
| 5492 | * So we need to free its reserved space here. |
| 5493 | * (Refer to comment in btrfs_invalidatepage, case 2) |
| 5494 | * |
| 5495 | * Note, end is the bytenr of last byte, so we need + 1 here. |
| 5496 | */ |
Filipe Manana | 421f092 | 2018-10-12 13:02:48 +0100 | [diff] [blame] | 5497 | if (state_flags & EXTENT_DELALLOC) |
Nikolay Borisov | 8b8a979 | 2020-06-03 08:55:11 +0300 | [diff] [blame] | 5498 | btrfs_qgroup_free_data(BTRFS_I(inode), NULL, start, |
| 5499 | end - start + 1); |
Qu Wenruo | b9d0b38 | 2015-09-29 10:35:16 +0800 | [diff] [blame] | 5500 | |
Filipe Manana | 6ca0709 | 2015-05-26 00:55:42 +0100 | [diff] [blame] | 5501 | clear_extent_bit(io_tree, start, end, |
Omar Sandoval | e182163 | 2019-08-15 14:04:04 -0700 | [diff] [blame] | 5502 | EXTENT_LOCKED | EXTENT_DELALLOC | |
| 5503 | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1, |
| 5504 | &cached_state); |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5505 | |
Filipe Manana | 7064dd5 | 2014-08-08 02:47:05 +0100 | [diff] [blame] | 5506 | cond_resched(); |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5507 | spin_lock(&io_tree->lock); |
| 5508 | } |
| 5509 | spin_unlock(&io_tree->lock); |
| 5510 | } |
| 5511 | |
Omar Sandoval | 4b9d7b5 | 2018-05-11 13:13:36 -0700 | [diff] [blame] | 5512 | static struct btrfs_trans_handle *evict_refill_and_join(struct btrfs_root *root, |
Josef Bacik | ad80cf5 | 2018-09-28 07:18:19 -0400 | [diff] [blame] | 5513 | struct btrfs_block_rsv *rsv) |
Omar Sandoval | 4b9d7b5 | 2018-05-11 13:13:36 -0700 | [diff] [blame] | 5514 | { |
| 5515 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 5516 | struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv; |
Josef Bacik | d3984c9 | 2019-08-01 18:19:37 -0400 | [diff] [blame] | 5517 | struct btrfs_trans_handle *trans; |
Josef Bacik | 2bd36e7 | 2019-08-22 15:14:33 -0400 | [diff] [blame] | 5518 | u64 delayed_refs_extra = btrfs_calc_insert_metadata_size(fs_info, 1); |
Josef Bacik | d3984c9 | 2019-08-01 18:19:37 -0400 | [diff] [blame] | 5519 | int ret; |
Omar Sandoval | 4b9d7b5 | 2018-05-11 13:13:36 -0700 | [diff] [blame] | 5520 | |
Josef Bacik | d3984c9 | 2019-08-01 18:19:37 -0400 | [diff] [blame] | 5521 | /* |
| 5522 | * Eviction should be taking place at some place safe because of our |
| 5523 | * delayed iputs. However the normal flushing code will run delayed |
| 5524 | * iputs, so we cannot use FLUSH_ALL otherwise we'll deadlock. |
| 5525 | * |
| 5526 | * We reserve the delayed_refs_extra here again because we can't use |
| 5527 | * btrfs_start_transaction(root, 0) for the same deadlocky reason as |
| 5528 | * above. We reserve our extra bit here because we generate a ton of |
| 5529 | * delayed refs activity by truncating. |
| 5530 | * |
| 5531 | * If we cannot make our reservation we'll attempt to steal from the |
| 5532 | * global reserve, because we really want to be able to free up space. |
| 5533 | */ |
| 5534 | ret = btrfs_block_rsv_refill(root, rsv, rsv->size + delayed_refs_extra, |
| 5535 | BTRFS_RESERVE_FLUSH_EVICT); |
| 5536 | if (ret) { |
Omar Sandoval | 4b9d7b5 | 2018-05-11 13:13:36 -0700 | [diff] [blame] | 5537 | /* |
| 5538 | * Try to steal from the global reserve if there is space for |
| 5539 | * it. |
| 5540 | */ |
Josef Bacik | d3984c9 | 2019-08-01 18:19:37 -0400 | [diff] [blame] | 5541 | if (btrfs_check_space_for_delayed_refs(fs_info) || |
| 5542 | btrfs_block_rsv_migrate(global_rsv, rsv, rsv->size, 0)) { |
| 5543 | btrfs_warn(fs_info, |
| 5544 | "could not allocate space for delete; will truncate on mount"); |
| 5545 | return ERR_PTR(-ENOSPC); |
| 5546 | } |
| 5547 | delayed_refs_extra = 0; |
Omar Sandoval | 4b9d7b5 | 2018-05-11 13:13:36 -0700 | [diff] [blame] | 5548 | } |
Josef Bacik | d3984c9 | 2019-08-01 18:19:37 -0400 | [diff] [blame] | 5549 | |
| 5550 | trans = btrfs_join_transaction(root); |
| 5551 | if (IS_ERR(trans)) |
| 5552 | return trans; |
| 5553 | |
| 5554 | if (delayed_refs_extra) { |
| 5555 | trans->block_rsv = &fs_info->trans_block_rsv; |
| 5556 | trans->bytes_reserved = delayed_refs_extra; |
| 5557 | btrfs_block_rsv_migrate(rsv, trans->block_rsv, |
| 5558 | delayed_refs_extra, 1); |
| 5559 | } |
| 5560 | return trans; |
Omar Sandoval | 4b9d7b5 | 2018-05-11 13:13:36 -0700 | [diff] [blame] | 5561 | } |
| 5562 | |
Al Viro | bd55597 | 2010-06-07 11:35:40 -0400 | [diff] [blame] | 5563 | void btrfs_evict_inode(struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5564 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5565 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5566 | struct btrfs_trans_handle *trans; |
| 5567 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Omar Sandoval | 4b9d7b5 | 2018-05-11 13:13:36 -0700 | [diff] [blame] | 5568 | struct btrfs_block_rsv *rsv; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5569 | int ret; |
| 5570 | |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 5571 | trace_btrfs_inode_evict(inode); |
| 5572 | |
Nikolay Borisov | 3d48d98 | 2016-06-29 09:46:41 +0300 | [diff] [blame] | 5573 | if (!root) { |
Boris Burkov | 1460540 | 2021-06-30 13:01:49 -0700 | [diff] [blame] | 5574 | fsverity_cleanup_inode(inode); |
Liu Bo | e8f1bc1 | 2018-01-25 11:02:53 -0700 | [diff] [blame] | 5575 | clear_inode(inode); |
Nikolay Borisov | 3d48d98 | 2016-06-29 09:46:41 +0300 | [diff] [blame] | 5576 | return; |
| 5577 | } |
| 5578 | |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5579 | evict_inode_truncate_pages(inode); |
| 5580 | |
Stefan Behrens | 69e9c6c | 2013-09-05 16:58:43 +0200 | [diff] [blame] | 5581 | if (inode->i_nlink && |
| 5582 | ((btrfs_root_refs(&root->root_item) != 0 && |
| 5583 | root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) || |
Nikolay Borisov | 70ddc55 | 2017-02-20 13:50:35 +0200 | [diff] [blame] | 5584 | btrfs_is_free_space_inode(BTRFS_I(inode)))) |
Al Viro | bd55597 | 2010-06-07 11:35:40 -0400 | [diff] [blame] | 5585 | goto no_delete; |
| 5586 | |
Omar Sandoval | 2791906 | 2018-05-11 13:13:37 -0700 | [diff] [blame] | 5587 | if (is_bad_inode(inode)) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5588 | goto no_delete; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5589 | |
Nikolay Borisov | 7ab7956 | 2017-02-20 13:50:57 +0200 | [diff] [blame] | 5590 | btrfs_free_io_failure_record(BTRFS_I(inode), 0, (u64)-1); |
Miao Xie | f612496 | 2014-09-12 18:44:04 +0800 | [diff] [blame] | 5591 | |
Omar Sandoval | 7b40b69 | 2018-05-11 13:13:33 -0700 | [diff] [blame] | 5592 | if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 5593 | goto no_delete; |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 5594 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5595 | if (inode->i_nlink > 0) { |
Stefan Behrens | 69e9c6c | 2013-09-05 16:58:43 +0200 | [diff] [blame] | 5596 | BUG_ON(btrfs_root_refs(&root->root_item) != 0 && |
| 5597 | root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5598 | goto no_delete; |
| 5599 | } |
| 5600 | |
Nikolay Borisov | aa79021 | 2017-01-10 20:35:40 +0200 | [diff] [blame] | 5601 | ret = btrfs_commit_inode_delayed_inode(BTRFS_I(inode)); |
Omar Sandoval | 2791906 | 2018-05-11 13:13:37 -0700 | [diff] [blame] | 5602 | if (ret) |
Miao Xie | 0e8c36a | 2012-12-19 06:59:51 +0000 | [diff] [blame] | 5603 | goto no_delete; |
Miao Xie | 0e8c36a | 2012-12-19 06:59:51 +0000 | [diff] [blame] | 5604 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5605 | rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP); |
Omar Sandoval | 2791906 | 2018-05-11 13:13:37 -0700 | [diff] [blame] | 5606 | if (!rsv) |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 5607 | goto no_delete; |
Josef Bacik | 2bd36e7 | 2019-08-22 15:14:33 -0400 | [diff] [blame] | 5608 | rsv->size = btrfs_calc_metadata_size(fs_info, 1); |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 5609 | rsv->failfast = 1; |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 5610 | |
Nikolay Borisov | 6ef06d2 | 2017-02-20 13:50:34 +0200 | [diff] [blame] | 5611 | btrfs_i_size_write(BTRFS_I(inode), 0); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5612 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5613 | while (1) { |
Josef Bacik | ad80cf5 | 2018-09-28 07:18:19 -0400 | [diff] [blame] | 5614 | trans = evict_refill_and_join(root, rsv); |
Omar Sandoval | 2791906 | 2018-05-11 13:13:37 -0700 | [diff] [blame] | 5615 | if (IS_ERR(trans)) |
| 5616 | goto free_rsv; |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 5617 | |
| 5618 | trans->block_rsv = rsv; |
| 5619 | |
Nikolay Borisov | 5074339 | 2020-11-02 16:48:55 +0200 | [diff] [blame] | 5620 | ret = btrfs_truncate_inode_items(trans, root, BTRFS_I(inode), |
Filipe Manana | 0d7d316 | 2021-05-24 11:35:55 +0100 | [diff] [blame] | 5621 | 0, 0, NULL); |
Omar Sandoval | 2791906 | 2018-05-11 13:13:37 -0700 | [diff] [blame] | 5622 | trans->block_rsv = &fs_info->trans_block_rsv; |
| 5623 | btrfs_end_transaction(trans); |
| 5624 | btrfs_btree_balance_dirty(fs_info); |
| 5625 | if (ret && ret != -ENOSPC && ret != -EAGAIN) |
| 5626 | goto free_rsv; |
| 5627 | else if (!ret) |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5628 | break; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 5629 | } |
| 5630 | |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 5631 | /* |
Omar Sandoval | 2791906 | 2018-05-11 13:13:37 -0700 | [diff] [blame] | 5632 | * Errors here aren't a big deal, it just means we leave orphan items in |
| 5633 | * the tree. They will be cleaned up on the next mount. If the inode |
| 5634 | * number gets reused, cleanup deletes the orphan item without doing |
| 5635 | * anything, and unlink reuses the existing orphan item. |
| 5636 | * |
| 5637 | * If it turns out that we are dropping too many of these, we might want |
| 5638 | * to add a mechanism for retrying these after a commit. |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 5639 | */ |
Josef Bacik | ad80cf5 | 2018-09-28 07:18:19 -0400 | [diff] [blame] | 5640 | trans = evict_refill_and_join(root, rsv); |
Omar Sandoval | 2791906 | 2018-05-11 13:13:37 -0700 | [diff] [blame] | 5641 | if (!IS_ERR(trans)) { |
| 5642 | trans->block_rsv = rsv; |
| 5643 | btrfs_orphan_del(trans, BTRFS_I(inode)); |
| 5644 | trans->block_rsv = &fs_info->trans_block_rsv; |
| 5645 | btrfs_end_transaction(trans); |
| 5646 | } |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 5647 | |
Omar Sandoval | 2791906 | 2018-05-11 13:13:37 -0700 | [diff] [blame] | 5648 | free_rsv: |
| 5649 | btrfs_free_block_rsv(fs_info, rsv); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5650 | no_delete: |
Omar Sandoval | 2791906 | 2018-05-11 13:13:37 -0700 | [diff] [blame] | 5651 | /* |
| 5652 | * If we didn't successfully delete, the orphan item will still be in |
| 5653 | * the tree and we'll retry on the next mount. Again, we might also want |
| 5654 | * to retry these periodically in the future. |
| 5655 | */ |
Nikolay Borisov | f48d1cf | 2017-01-10 20:35:39 +0200 | [diff] [blame] | 5656 | btrfs_remove_delayed_node(BTRFS_I(inode)); |
Boris Burkov | 1460540 | 2021-06-30 13:01:49 -0700 | [diff] [blame] | 5657 | fsverity_cleanup_inode(inode); |
Jan Kara | dbd5768 | 2012-05-03 14:48:02 +0200 | [diff] [blame] | 5658 | clear_inode(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5659 | } |
| 5660 | |
| 5661 | /* |
Qu Wenruo | 6bf9e4b | 2019-03-13 13:55:11 +0800 | [diff] [blame] | 5662 | * Return the key found in the dir entry in the location pointer, fill @type |
| 5663 | * with BTRFS_FT_*, and return 0. |
| 5664 | * |
Su Yue | 005d671 | 2018-03-05 17:13:37 +0800 | [diff] [blame] | 5665 | * If no dir entries were found, returns -ENOENT. |
| 5666 | * If found a corrupted location in dir entry, returns -EUCLEAN. |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5667 | */ |
| 5668 | static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry, |
Qu Wenruo | 6bf9e4b | 2019-03-13 13:55:11 +0800 | [diff] [blame] | 5669 | struct btrfs_key *location, u8 *type) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5670 | { |
| 5671 | const char *name = dentry->d_name.name; |
| 5672 | int namelen = dentry->d_name.len; |
| 5673 | struct btrfs_dir_item *di; |
| 5674 | struct btrfs_path *path; |
| 5675 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Yan | 0d9f7f3 | 2007-10-25 15:48:28 -0400 | [diff] [blame] | 5676 | int ret = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5677 | |
| 5678 | path = btrfs_alloc_path(); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 5679 | if (!path) |
| 5680 | return -ENOMEM; |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 5681 | |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 5682 | di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(BTRFS_I(dir)), |
| 5683 | name, namelen, 0); |
Liu Bo | 3cf5068 | 2018-09-12 06:06:26 +0800 | [diff] [blame] | 5684 | if (IS_ERR_OR_NULL(di)) { |
| 5685 | ret = di ? PTR_ERR(di) : -ENOENT; |
Su Yue | 005d671 | 2018-03-05 17:13:37 +0800 | [diff] [blame] | 5686 | goto out; |
| 5687 | } |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5688 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5689 | btrfs_dir_item_key_to_cpu(path->nodes[0], di, location); |
Liu Bo | 56a0e70 | 2017-10-30 11:14:38 -0600 | [diff] [blame] | 5690 | if (location->type != BTRFS_INODE_ITEM_KEY && |
| 5691 | location->type != BTRFS_ROOT_ITEM_KEY) { |
Su Yue | 005d671 | 2018-03-05 17:13:37 +0800 | [diff] [blame] | 5692 | ret = -EUCLEAN; |
Liu Bo | 56a0e70 | 2017-10-30 11:14:38 -0600 | [diff] [blame] | 5693 | btrfs_warn(root->fs_info, |
| 5694 | "%s gets something invalid in DIR_ITEM (name %s, directory ino %llu, location(%llu %u %llu))", |
| 5695 | __func__, name, btrfs_ino(BTRFS_I(dir)), |
| 5696 | location->objectid, location->type, location->offset); |
Liu Bo | 56a0e70 | 2017-10-30 11:14:38 -0600 | [diff] [blame] | 5697 | } |
Qu Wenruo | 6bf9e4b | 2019-03-13 13:55:11 +0800 | [diff] [blame] | 5698 | if (!ret) |
| 5699 | *type = btrfs_dir_type(path->nodes[0], di); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5700 | out: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5701 | btrfs_free_path(path); |
| 5702 | return ret; |
| 5703 | } |
| 5704 | |
| 5705 | /* |
| 5706 | * when we hit a tree root in a directory, the btrfs part of the inode |
| 5707 | * needs to be changed to reflect the root directory of the tree root. This |
| 5708 | * is kind of like crossing a mount point. |
| 5709 | */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5710 | static int fixup_tree_root_location(struct btrfs_fs_info *fs_info, |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5711 | struct inode *dir, |
| 5712 | struct dentry *dentry, |
| 5713 | struct btrfs_key *location, |
| 5714 | struct btrfs_root **sub_root) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5715 | { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5716 | struct btrfs_path *path; |
| 5717 | struct btrfs_root *new_root; |
| 5718 | struct btrfs_root_ref *ref; |
| 5719 | struct extent_buffer *leaf; |
David Sterba | 1d4c08e | 2015-01-02 19:36:14 +0100 | [diff] [blame] | 5720 | struct btrfs_key key; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5721 | int ret; |
| 5722 | int err = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5723 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5724 | path = btrfs_alloc_path(); |
| 5725 | if (!path) { |
| 5726 | err = -ENOMEM; |
| 5727 | goto out; |
| 5728 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5729 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5730 | err = -ENOENT; |
David Sterba | 1d4c08e | 2015-01-02 19:36:14 +0100 | [diff] [blame] | 5731 | key.objectid = BTRFS_I(dir)->root->root_key.objectid; |
| 5732 | key.type = BTRFS_ROOT_REF_KEY; |
| 5733 | key.offset = location->objectid; |
| 5734 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5735 | ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5736 | if (ret) { |
| 5737 | if (ret < 0) |
| 5738 | err = ret; |
| 5739 | goto out; |
| 5740 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5741 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5742 | leaf = path->nodes[0]; |
| 5743 | ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref); |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 5744 | if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(BTRFS_I(dir)) || |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5745 | btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len) |
| 5746 | goto out; |
| 5747 | |
| 5748 | ret = memcmp_extent_buffer(leaf, dentry->d_name.name, |
| 5749 | (unsigned long)(ref + 1), |
| 5750 | dentry->d_name.len); |
| 5751 | if (ret) |
| 5752 | goto out; |
| 5753 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 5754 | btrfs_release_path(path); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5755 | |
David Sterba | 56e9357 | 2020-05-15 19:35:55 +0200 | [diff] [blame] | 5756 | new_root = btrfs_get_fs_root(fs_info, location->objectid, true); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5757 | if (IS_ERR(new_root)) { |
| 5758 | err = PTR_ERR(new_root); |
| 5759 | goto out; |
| 5760 | } |
| 5761 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5762 | *sub_root = new_root; |
| 5763 | location->objectid = btrfs_root_dirid(&new_root->root_item); |
| 5764 | location->type = BTRFS_INODE_ITEM_KEY; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5765 | location->offset = 0; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5766 | err = 0; |
| 5767 | out: |
| 5768 | btrfs_free_path(path); |
| 5769 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5770 | } |
| 5771 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5772 | static void inode_tree_add(struct inode *inode) |
| 5773 | { |
| 5774 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5775 | struct btrfs_inode *entry; |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 5776 | struct rb_node **p; |
| 5777 | struct rb_node *parent; |
Filipe David Borba Manana | cef2193 | 2013-09-02 12:19:13 +0100 | [diff] [blame] | 5778 | struct rb_node *new = &BTRFS_I(inode)->rb_node; |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 5779 | u64 ino = btrfs_ino(BTRFS_I(inode)); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5780 | |
Al Viro | 1d3382cb | 2010-10-23 15:19:20 -0400 | [diff] [blame] | 5781 | if (inode_unhashed(inode)) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5782 | return; |
Miao Xie | e1409ce | 2013-05-15 07:48:16 +0000 | [diff] [blame] | 5783 | parent = NULL; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5784 | spin_lock(&root->inode_lock); |
Miao Xie | e1409ce | 2013-05-15 07:48:16 +0000 | [diff] [blame] | 5785 | p = &root->inode_tree.rb_node; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5786 | while (*p) { |
| 5787 | parent = *p; |
| 5788 | entry = rb_entry(parent, struct btrfs_inode, rb_node); |
| 5789 | |
David Sterba | 3750851 | 2018-06-29 10:56:40 +0200 | [diff] [blame] | 5790 | if (ino < btrfs_ino(entry)) |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 5791 | p = &parent->rb_left; |
David Sterba | 3750851 | 2018-06-29 10:56:40 +0200 | [diff] [blame] | 5792 | else if (ino > btrfs_ino(entry)) |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 5793 | p = &parent->rb_right; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5794 | else { |
| 5795 | WARN_ON(!(entry->vfs_inode.i_state & |
Al Viro | a4ffdde | 2010-06-02 17:38:30 -0400 | [diff] [blame] | 5796 | (I_WILL_FREE | I_FREEING))); |
Filipe David Borba Manana | cef2193 | 2013-09-02 12:19:13 +0100 | [diff] [blame] | 5797 | rb_replace_node(parent, new, &root->inode_tree); |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 5798 | RB_CLEAR_NODE(parent); |
| 5799 | spin_unlock(&root->inode_lock); |
Filipe David Borba Manana | cef2193 | 2013-09-02 12:19:13 +0100 | [diff] [blame] | 5800 | return; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5801 | } |
| 5802 | } |
Filipe David Borba Manana | cef2193 | 2013-09-02 12:19:13 +0100 | [diff] [blame] | 5803 | rb_link_node(new, parent, p); |
| 5804 | rb_insert_color(new, &root->inode_tree); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5805 | spin_unlock(&root->inode_lock); |
| 5806 | } |
| 5807 | |
Nikolay Borisov | b79b724 | 2020-08-31 14:42:38 +0300 | [diff] [blame] | 5808 | static void inode_tree_del(struct btrfs_inode *inode) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5809 | { |
Nikolay Borisov | b79b724 | 2020-08-31 14:42:38 +0300 | [diff] [blame] | 5810 | struct btrfs_root *root = inode->root; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5811 | int empty = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5812 | |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 5813 | spin_lock(&root->inode_lock); |
Nikolay Borisov | b79b724 | 2020-08-31 14:42:38 +0300 | [diff] [blame] | 5814 | if (!RB_EMPTY_NODE(&inode->rb_node)) { |
| 5815 | rb_erase(&inode->rb_node, &root->inode_tree); |
| 5816 | RB_CLEAR_NODE(&inode->rb_node); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5817 | empty = RB_EMPTY_ROOT(&root->inode_tree); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5818 | } |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 5819 | spin_unlock(&root->inode_lock); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5820 | |
Stefan Behrens | 69e9c6c | 2013-09-05 16:58:43 +0200 | [diff] [blame] | 5821 | if (empty && btrfs_root_refs(&root->root_item) == 0) { |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5822 | spin_lock(&root->inode_lock); |
| 5823 | empty = RB_EMPTY_ROOT(&root->inode_tree); |
| 5824 | spin_unlock(&root->inode_lock); |
| 5825 | if (empty) |
| 5826 | btrfs_add_dead_root(root); |
| 5827 | } |
| 5828 | } |
| 5829 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5830 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5831 | static int btrfs_init_locked_inode(struct inode *inode, void *p) |
| 5832 | { |
| 5833 | struct btrfs_iget_args *args = p; |
David Sterba | 0202e83 | 2020-05-15 19:35:59 +0200 | [diff] [blame] | 5834 | |
| 5835 | inode->i_ino = args->ino; |
| 5836 | BTRFS_I(inode)->location.objectid = args->ino; |
| 5837 | BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY; |
| 5838 | BTRFS_I(inode)->location.offset = 0; |
Josef Bacik | 5c8fd99 | 2020-02-14 16:11:43 -0500 | [diff] [blame] | 5839 | BTRFS_I(inode)->root = btrfs_grab_root(args->root); |
| 5840 | BUG_ON(args->root && !BTRFS_I(inode)->root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5841 | return 0; |
| 5842 | } |
| 5843 | |
| 5844 | static int btrfs_find_actor(struct inode *inode, void *opaque) |
| 5845 | { |
| 5846 | struct btrfs_iget_args *args = opaque; |
David Sterba | 0202e83 | 2020-05-15 19:35:59 +0200 | [diff] [blame] | 5847 | |
| 5848 | return args->ino == BTRFS_I(inode)->location.objectid && |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5849 | args->root == BTRFS_I(inode)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5850 | } |
| 5851 | |
David Sterba | 0202e83 | 2020-05-15 19:35:59 +0200 | [diff] [blame] | 5852 | static struct inode *btrfs_iget_locked(struct super_block *s, u64 ino, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5853 | struct btrfs_root *root) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5854 | { |
| 5855 | struct inode *inode; |
| 5856 | struct btrfs_iget_args args; |
David Sterba | 0202e83 | 2020-05-15 19:35:59 +0200 | [diff] [blame] | 5857 | unsigned long hashval = btrfs_inode_hash(ino, root); |
Filipe David Borba Manana | 778ba82 | 2013-10-06 22:22:33 +0100 | [diff] [blame] | 5858 | |
David Sterba | 0202e83 | 2020-05-15 19:35:59 +0200 | [diff] [blame] | 5859 | args.ino = ino; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5860 | args.root = root; |
| 5861 | |
Filipe David Borba Manana | 778ba82 | 2013-10-06 22:22:33 +0100 | [diff] [blame] | 5862 | inode = iget5_locked(s, hashval, btrfs_find_actor, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5863 | btrfs_init_locked_inode, |
| 5864 | (void *)&args); |
| 5865 | return inode; |
| 5866 | } |
| 5867 | |
David Sterba | 4c66e0d | 2019-10-03 19:09:35 +0200 | [diff] [blame] | 5868 | /* |
David Sterba | 0202e83 | 2020-05-15 19:35:59 +0200 | [diff] [blame] | 5869 | * Get an inode object given its inode number and corresponding root. |
David Sterba | 4c66e0d | 2019-10-03 19:09:35 +0200 | [diff] [blame] | 5870 | * Path can be preallocated to prevent recursing back to iget through |
| 5871 | * allocator. NULL is also valid but may require an additional allocation |
| 5872 | * later. |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 5873 | */ |
David Sterba | 0202e83 | 2020-05-15 19:35:59 +0200 | [diff] [blame] | 5874 | struct inode *btrfs_iget_path(struct super_block *s, u64 ino, |
David Sterba | 4c66e0d | 2019-10-03 19:09:35 +0200 | [diff] [blame] | 5875 | struct btrfs_root *root, struct btrfs_path *path) |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 5876 | { |
| 5877 | struct inode *inode; |
| 5878 | |
David Sterba | 0202e83 | 2020-05-15 19:35:59 +0200 | [diff] [blame] | 5879 | inode = btrfs_iget_locked(s, ino, root); |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 5880 | if (!inode) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5881 | return ERR_PTR(-ENOMEM); |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 5882 | |
| 5883 | if (inode->i_state & I_NEW) { |
Filipe Manana | 6771089 | 2016-06-06 11:51:25 +0100 | [diff] [blame] | 5884 | int ret; |
| 5885 | |
Filipe Manana | 4222ea7 | 2018-10-24 10:13:03 +0100 | [diff] [blame] | 5886 | ret = btrfs_read_locked_inode(inode, path); |
Al Viro | 9bc2cef | 2018-07-29 23:04:50 +0100 | [diff] [blame] | 5887 | if (!ret) { |
Mark Fasheh | 1748f84 | 2011-07-12 11:25:31 -0700 | [diff] [blame] | 5888 | inode_tree_add(inode); |
| 5889 | unlock_new_inode(inode); |
Mark Fasheh | 1748f84 | 2011-07-12 11:25:31 -0700 | [diff] [blame] | 5890 | } else { |
Al Viro | f5b3a41 | 2018-07-29 23:04:51 +0100 | [diff] [blame] | 5891 | iget_failed(inode); |
| 5892 | /* |
| 5893 | * ret > 0 can come from btrfs_search_slot called by |
| 5894 | * btrfs_read_locked_inode, this means the inode item |
| 5895 | * was not found. |
| 5896 | */ |
| 5897 | if (ret > 0) |
| 5898 | ret = -ENOENT; |
| 5899 | inode = ERR_PTR(ret); |
Mark Fasheh | 1748f84 | 2011-07-12 11:25:31 -0700 | [diff] [blame] | 5900 | } |
| 5901 | } |
| 5902 | |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 5903 | return inode; |
| 5904 | } |
| 5905 | |
David Sterba | 0202e83 | 2020-05-15 19:35:59 +0200 | [diff] [blame] | 5906 | struct inode *btrfs_iget(struct super_block *s, u64 ino, struct btrfs_root *root) |
Filipe Manana | 4222ea7 | 2018-10-24 10:13:03 +0100 | [diff] [blame] | 5907 | { |
David Sterba | 0202e83 | 2020-05-15 19:35:59 +0200 | [diff] [blame] | 5908 | return btrfs_iget_path(s, ino, root, NULL); |
Filipe Manana | 4222ea7 | 2018-10-24 10:13:03 +0100 | [diff] [blame] | 5909 | } |
| 5910 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5911 | static struct inode *new_simple_dir(struct super_block *s, |
| 5912 | struct btrfs_key *key, |
| 5913 | struct btrfs_root *root) |
| 5914 | { |
| 5915 | struct inode *inode = new_inode(s); |
| 5916 | |
| 5917 | if (!inode) |
| 5918 | return ERR_PTR(-ENOMEM); |
| 5919 | |
Josef Bacik | 5c8fd99 | 2020-02-14 16:11:43 -0500 | [diff] [blame] | 5920 | BTRFS_I(inode)->root = btrfs_grab_root(root); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5921 | memcpy(&BTRFS_I(inode)->location, key, sizeof(*key)); |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 5922 | set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5923 | |
| 5924 | inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID; |
Omar Sandoval | 6bb6b51 | 2019-12-05 10:36:04 -0800 | [diff] [blame] | 5925 | /* |
| 5926 | * We only need lookup, the rest is read-only and there's no inode |
| 5927 | * associated with the dentry |
| 5928 | */ |
| 5929 | inode->i_op = &simple_dir_inode_operations; |
Omar Sandoval | 1fdf419 | 2017-01-25 17:06:39 -0800 | [diff] [blame] | 5930 | inode->i_opflags &= ~IOP_XATTR; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5931 | inode->i_fop = &simple_dir_operations; |
| 5932 | inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO; |
Deepa Dinamani | c2050a4 | 2016-09-14 07:48:06 -0700 | [diff] [blame] | 5933 | inode->i_mtime = current_time(inode); |
chandan r | 9cc97d6 | 2012-07-04 12:48:07 +0530 | [diff] [blame] | 5934 | inode->i_atime = inode->i_mtime; |
| 5935 | inode->i_ctime = inode->i_mtime; |
Arnd Bergmann | d3c6be6f | 2018-06-21 18:04:06 +0200 | [diff] [blame] | 5936 | BTRFS_I(inode)->i_otime = inode->i_mtime; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5937 | |
| 5938 | return inode; |
| 5939 | } |
| 5940 | |
Qu Wenruo | 6bf9e4b | 2019-03-13 13:55:11 +0800 | [diff] [blame] | 5941 | static inline u8 btrfs_inode_type(struct inode *inode) |
| 5942 | { |
| 5943 | /* |
| 5944 | * Compile-time asserts that generic FT_* types still match |
| 5945 | * BTRFS_FT_* types |
| 5946 | */ |
| 5947 | BUILD_BUG_ON(BTRFS_FT_UNKNOWN != FT_UNKNOWN); |
| 5948 | BUILD_BUG_ON(BTRFS_FT_REG_FILE != FT_REG_FILE); |
| 5949 | BUILD_BUG_ON(BTRFS_FT_DIR != FT_DIR); |
| 5950 | BUILD_BUG_ON(BTRFS_FT_CHRDEV != FT_CHRDEV); |
| 5951 | BUILD_BUG_ON(BTRFS_FT_BLKDEV != FT_BLKDEV); |
| 5952 | BUILD_BUG_ON(BTRFS_FT_FIFO != FT_FIFO); |
| 5953 | BUILD_BUG_ON(BTRFS_FT_SOCK != FT_SOCK); |
| 5954 | BUILD_BUG_ON(BTRFS_FT_SYMLINK != FT_SYMLINK); |
| 5955 | |
| 5956 | return fs_umode_to_ftype(inode->i_mode); |
| 5957 | } |
| 5958 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5959 | struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5960 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5961 | struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5962 | struct inode *inode; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5963 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5964 | struct btrfs_root *sub_root = root; |
| 5965 | struct btrfs_key location; |
Qu Wenruo | 6bf9e4b | 2019-03-13 13:55:11 +0800 | [diff] [blame] | 5966 | u8 di_type = 0; |
Josef Bacik | b4aff1f | 2011-06-28 16:18:59 -0400 | [diff] [blame] | 5967 | int ret = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5968 | |
| 5969 | if (dentry->d_name.len > BTRFS_NAME_LEN) |
| 5970 | return ERR_PTR(-ENAMETOOLONG); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5971 | |
Qu Wenruo | 6bf9e4b | 2019-03-13 13:55:11 +0800 | [diff] [blame] | 5972 | ret = btrfs_inode_by_name(dir, dentry, &location, &di_type); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5973 | if (ret < 0) |
| 5974 | return ERR_PTR(ret); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5975 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5976 | if (location.type == BTRFS_INODE_ITEM_KEY) { |
David Sterba | 0202e83 | 2020-05-15 19:35:59 +0200 | [diff] [blame] | 5977 | inode = btrfs_iget(dir->i_sb, location.objectid, root); |
Qu Wenruo | 6bf9e4b | 2019-03-13 13:55:11 +0800 | [diff] [blame] | 5978 | if (IS_ERR(inode)) |
| 5979 | return inode; |
| 5980 | |
| 5981 | /* Do extra check against inode mode with di_type */ |
| 5982 | if (btrfs_inode_type(inode) != di_type) { |
| 5983 | btrfs_crit(fs_info, |
| 5984 | "inode mode mismatch with dir: inode mode=0%o btrfs type=%u dir type=%u", |
| 5985 | inode->i_mode, btrfs_inode_type(inode), |
| 5986 | di_type); |
| 5987 | iput(inode); |
| 5988 | return ERR_PTR(-EUCLEAN); |
| 5989 | } |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5990 | return inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5991 | } |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5992 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5993 | ret = fixup_tree_root_location(fs_info, dir, dentry, |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5994 | &location, &sub_root); |
| 5995 | if (ret < 0) { |
| 5996 | if (ret != -ENOENT) |
| 5997 | inode = ERR_PTR(ret); |
| 5998 | else |
| 5999 | inode = new_simple_dir(dir->i_sb, &location, sub_root); |
| 6000 | } else { |
David Sterba | 0202e83 | 2020-05-15 19:35:59 +0200 | [diff] [blame] | 6001 | inode = btrfs_iget(dir->i_sb, location.objectid, sub_root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6002 | } |
Josef Bacik | 8727002 | 2020-01-24 09:32:31 -0500 | [diff] [blame] | 6003 | if (root != sub_root) |
Josef Bacik | 0024652 | 2020-01-24 09:33:01 -0500 | [diff] [blame] | 6004 | btrfs_put_root(sub_root); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 6005 | |
Julia Lawall | 34d19ba | 2011-01-24 19:55:19 +0000 | [diff] [blame] | 6006 | if (!IS_ERR(inode) && root != sub_root) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6007 | down_read(&fs_info->cleanup_work_sem); |
David Howells | bc98a42 | 2017-07-17 08:45:34 +0100 | [diff] [blame] | 6008 | if (!sb_rdonly(inode->i_sb)) |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 6009 | ret = btrfs_orphan_cleanup(sub_root); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6010 | up_read(&fs_info->cleanup_work_sem); |
Josef Bacik | 01cd336 | 2013-06-03 21:39:49 -0400 | [diff] [blame] | 6011 | if (ret) { |
| 6012 | iput(inode); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 6013 | inode = ERR_PTR(ret); |
Josef Bacik | 01cd336 | 2013-06-03 21:39:49 -0400 | [diff] [blame] | 6014 | } |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 6015 | } |
| 6016 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 6017 | return inode; |
| 6018 | } |
| 6019 | |
Nick Piggin | fe15ce4 | 2011-01-07 17:49:23 +1100 | [diff] [blame] | 6020 | static int btrfs_dentry_delete(const struct dentry *dentry) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 6021 | { |
| 6022 | struct btrfs_root *root; |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 6023 | struct inode *inode = d_inode(dentry); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 6024 | |
Li Zefan | 848cce0 | 2012-02-21 17:04:28 +0800 | [diff] [blame] | 6025 | if (!inode && !IS_ROOT(dentry)) |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 6026 | inode = d_inode(dentry->d_parent); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 6027 | |
Li Zefan | 848cce0 | 2012-02-21 17:04:28 +0800 | [diff] [blame] | 6028 | if (inode) { |
| 6029 | root = BTRFS_I(inode)->root; |
Yan, Zheng | efefb14 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 6030 | if (btrfs_root_refs(&root->root_item) == 0) |
| 6031 | return 1; |
Li Zefan | 848cce0 | 2012-02-21 17:04:28 +0800 | [diff] [blame] | 6032 | |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 6033 | if (btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) |
Li Zefan | 848cce0 | 2012-02-21 17:04:28 +0800 | [diff] [blame] | 6034 | return 1; |
Yan, Zheng | efefb14 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 6035 | } |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 6036 | return 0; |
| 6037 | } |
| 6038 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 6039 | static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry, |
Al Viro | 00cd8dd | 2012-06-10 17:13:09 -0400 | [diff] [blame] | 6040 | unsigned int flags) |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 6041 | { |
Al Viro | 3837d20 | 2018-10-10 16:38:27 -0400 | [diff] [blame] | 6042 | struct inode *inode = btrfs_lookup_dentry(dir, dentry); |
Josef Bacik | a66e7cc | 2011-09-18 10:34:03 -0400 | [diff] [blame] | 6043 | |
Al Viro | 3837d20 | 2018-10-10 16:38:27 -0400 | [diff] [blame] | 6044 | if (inode == ERR_PTR(-ENOENT)) |
| 6045 | inode = NULL; |
Al Viro | 41d28bc | 2014-10-12 22:24:21 -0400 | [diff] [blame] | 6046 | return d_splice_alias(inode, dentry); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6047 | } |
| 6048 | |
Josef Bacik | 23b5ec7 | 2017-07-24 15:14:25 -0400 | [diff] [blame] | 6049 | /* |
| 6050 | * All this infrastructure exists because dir_emit can fault, and we are holding |
| 6051 | * the tree lock when doing readdir. For now just allocate a buffer and copy |
| 6052 | * our information into that, and then dir_emit from the buffer. This is |
| 6053 | * similar to what NFS does, only we don't keep the buffer around in pagecache |
| 6054 | * because I'm afraid I'll mess that up. Long term we need to make filldir do |
| 6055 | * copy_to_user_inatomic so we don't have to worry about page faulting under the |
| 6056 | * tree lock. |
| 6057 | */ |
| 6058 | static int btrfs_opendir(struct inode *inode, struct file *file) |
| 6059 | { |
| 6060 | struct btrfs_file_private *private; |
| 6061 | |
| 6062 | private = kzalloc(sizeof(struct btrfs_file_private), GFP_KERNEL); |
| 6063 | if (!private) |
| 6064 | return -ENOMEM; |
| 6065 | private->filldir_buf = kzalloc(PAGE_SIZE, GFP_KERNEL); |
| 6066 | if (!private->filldir_buf) { |
| 6067 | kfree(private); |
| 6068 | return -ENOMEM; |
| 6069 | } |
| 6070 | file->private_data = private; |
| 6071 | return 0; |
| 6072 | } |
| 6073 | |
| 6074 | struct dir_entry { |
| 6075 | u64 ino; |
| 6076 | u64 offset; |
| 6077 | unsigned type; |
| 6078 | int name_len; |
| 6079 | }; |
| 6080 | |
| 6081 | static int btrfs_filldir(void *addr, int entries, struct dir_context *ctx) |
| 6082 | { |
| 6083 | while (entries--) { |
| 6084 | struct dir_entry *entry = addr; |
| 6085 | char *name = (char *)(entry + 1); |
| 6086 | |
David Sterba | 92d3217 | 2018-04-16 21:10:14 +0200 | [diff] [blame] | 6087 | ctx->pos = get_unaligned(&entry->offset); |
| 6088 | if (!dir_emit(ctx, name, get_unaligned(&entry->name_len), |
| 6089 | get_unaligned(&entry->ino), |
| 6090 | get_unaligned(&entry->type))) |
Josef Bacik | 23b5ec7 | 2017-07-24 15:14:25 -0400 | [diff] [blame] | 6091 | return 1; |
David Sterba | 92d3217 | 2018-04-16 21:10:14 +0200 | [diff] [blame] | 6092 | addr += sizeof(struct dir_entry) + |
| 6093 | get_unaligned(&entry->name_len); |
Josef Bacik | 23b5ec7 | 2017-07-24 15:14:25 -0400 | [diff] [blame] | 6094 | ctx->pos++; |
| 6095 | } |
| 6096 | return 0; |
| 6097 | } |
| 6098 | |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 6099 | static int btrfs_real_readdir(struct file *file, struct dir_context *ctx) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6100 | { |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 6101 | struct inode *inode = file_inode(file); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6102 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Josef Bacik | 23b5ec7 | 2017-07-24 15:14:25 -0400 | [diff] [blame] | 6103 | struct btrfs_file_private *private = file->private_data; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6104 | struct btrfs_dir_item *di; |
| 6105 | struct btrfs_key key; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6106 | struct btrfs_key found_key; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6107 | struct btrfs_path *path; |
Josef Bacik | 23b5ec7 | 2017-07-24 15:14:25 -0400 | [diff] [blame] | 6108 | void *addr; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 6109 | struct list_head ins_list; |
| 6110 | struct list_head del_list; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6111 | int ret; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6112 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6113 | int slot; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6114 | char *name_ptr; |
| 6115 | int name_len; |
Josef Bacik | 23b5ec7 | 2017-07-24 15:14:25 -0400 | [diff] [blame] | 6116 | int entries = 0; |
| 6117 | int total_len = 0; |
Omar Sandoval | 02dbfc9 | 2016-05-20 13:50:33 -0700 | [diff] [blame] | 6118 | bool put = false; |
Jeff Mahoney | c2951f3 | 2016-11-21 15:59:04 +0100 | [diff] [blame] | 6119 | struct btrfs_key location; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6120 | |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 6121 | if (!dir_emit_dots(file, ctx)) |
| 6122 | return 0; |
| 6123 | |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 6124 | path = btrfs_alloc_path(); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 6125 | if (!path) |
| 6126 | return -ENOMEM; |
Chris Mason | ff5714c | 2011-05-28 07:00:39 -0400 | [diff] [blame] | 6127 | |
Josef Bacik | 23b5ec7 | 2017-07-24 15:14:25 -0400 | [diff] [blame] | 6128 | addr = private->filldir_buf; |
David Sterba | e4058b5 | 2015-11-27 16:31:35 +0100 | [diff] [blame] | 6129 | path->reada = READA_FORWARD; |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 6130 | |
Jeff Mahoney | c2951f3 | 2016-11-21 15:59:04 +0100 | [diff] [blame] | 6131 | INIT_LIST_HEAD(&ins_list); |
| 6132 | INIT_LIST_HEAD(&del_list); |
| 6133 | put = btrfs_readdir_get_delayed_items(inode, &ins_list, &del_list); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 6134 | |
Josef Bacik | 23b5ec7 | 2017-07-24 15:14:25 -0400 | [diff] [blame] | 6135 | again: |
Jeff Mahoney | c2951f3 | 2016-11-21 15:59:04 +0100 | [diff] [blame] | 6136 | key.type = BTRFS_DIR_INDEX_KEY; |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 6137 | key.offset = ctx->pos; |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 6138 | key.objectid = btrfs_ino(BTRFS_I(inode)); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6139 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6140 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 6141 | if (ret < 0) |
| 6142 | goto err; |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 6143 | |
| 6144 | while (1) { |
Josef Bacik | 23b5ec7 | 2017-07-24 15:14:25 -0400 | [diff] [blame] | 6145 | struct dir_entry *entry; |
| 6146 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6147 | leaf = path->nodes[0]; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6148 | slot = path->slots[0]; |
Li Zefan | b9e03af | 2011-03-23 10:43:58 +0800 | [diff] [blame] | 6149 | if (slot >= btrfs_header_nritems(leaf)) { |
| 6150 | ret = btrfs_next_leaf(root, path); |
| 6151 | if (ret < 0) |
| 6152 | goto err; |
| 6153 | else if (ret > 0) |
| 6154 | break; |
| 6155 | continue; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6156 | } |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 6157 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6158 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
| 6159 | |
| 6160 | if (found_key.objectid != key.objectid) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6161 | break; |
Jeff Mahoney | c2951f3 | 2016-11-21 15:59:04 +0100 | [diff] [blame] | 6162 | if (found_key.type != BTRFS_DIR_INDEX_KEY) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6163 | break; |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 6164 | if (found_key.offset < ctx->pos) |
Li Zefan | b9e03af | 2011-03-23 10:43:58 +0800 | [diff] [blame] | 6165 | goto next; |
Jeff Mahoney | c2951f3 | 2016-11-21 15:59:04 +0100 | [diff] [blame] | 6166 | if (btrfs_should_delete_dir_index(&del_list, found_key.offset)) |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 6167 | goto next; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6168 | di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item); |
Jeff Mahoney | c2951f3 | 2016-11-21 15:59:04 +0100 | [diff] [blame] | 6169 | name_len = btrfs_dir_name_len(leaf, di); |
Josef Bacik | 23b5ec7 | 2017-07-24 15:14:25 -0400 | [diff] [blame] | 6170 | if ((total_len + sizeof(struct dir_entry) + name_len) >= |
| 6171 | PAGE_SIZE) { |
| 6172 | btrfs_release_path(path); |
| 6173 | ret = btrfs_filldir(private->filldir_buf, entries, ctx); |
| 6174 | if (ret) |
| 6175 | goto nopos; |
| 6176 | addr = private->filldir_buf; |
| 6177 | entries = 0; |
| 6178 | total_len = 0; |
| 6179 | goto again; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6180 | } |
Josef Bacik | 23b5ec7 | 2017-07-24 15:14:25 -0400 | [diff] [blame] | 6181 | |
| 6182 | entry = addr; |
David Sterba | 92d3217 | 2018-04-16 21:10:14 +0200 | [diff] [blame] | 6183 | put_unaligned(name_len, &entry->name_len); |
Josef Bacik | 23b5ec7 | 2017-07-24 15:14:25 -0400 | [diff] [blame] | 6184 | name_ptr = (char *)(entry + 1); |
Jeff Mahoney | c2951f3 | 2016-11-21 15:59:04 +0100 | [diff] [blame] | 6185 | read_extent_buffer(leaf, name_ptr, (unsigned long)(di + 1), |
| 6186 | name_len); |
Phillip Potter | 7d157c3 | 2019-03-26 21:39:34 +0000 | [diff] [blame] | 6187 | put_unaligned(fs_ftype_to_dtype(btrfs_dir_type(leaf, di)), |
David Sterba | 92d3217 | 2018-04-16 21:10:14 +0200 | [diff] [blame] | 6188 | &entry->type); |
Jeff Mahoney | c2951f3 | 2016-11-21 15:59:04 +0100 | [diff] [blame] | 6189 | btrfs_dir_item_key_to_cpu(leaf, di, &location); |
David Sterba | 92d3217 | 2018-04-16 21:10:14 +0200 | [diff] [blame] | 6190 | put_unaligned(location.objectid, &entry->ino); |
| 6191 | put_unaligned(found_key.offset, &entry->offset); |
Josef Bacik | 23b5ec7 | 2017-07-24 15:14:25 -0400 | [diff] [blame] | 6192 | entries++; |
| 6193 | addr += sizeof(struct dir_entry) + name_len; |
| 6194 | total_len += sizeof(struct dir_entry) + name_len; |
Li Zefan | b9e03af | 2011-03-23 10:43:58 +0800 | [diff] [blame] | 6195 | next: |
| 6196 | path->slots[0]++; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6197 | } |
Josef Bacik | 23b5ec7 | 2017-07-24 15:14:25 -0400 | [diff] [blame] | 6198 | btrfs_release_path(path); |
| 6199 | |
| 6200 | ret = btrfs_filldir(private->filldir_buf, entries, ctx); |
| 6201 | if (ret) |
| 6202 | goto nopos; |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 6203 | |
Jeff Mahoney | d2fbb2b | 2016-11-05 13:26:35 -0400 | [diff] [blame] | 6204 | ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list); |
Jeff Mahoney | c2951f3 | 2016-11-21 15:59:04 +0100 | [diff] [blame] | 6205 | if (ret) |
David Sterba | bc4ef75 | 2015-11-13 13:44:28 +0100 | [diff] [blame] | 6206 | goto nopos; |
| 6207 | |
Zach Brown | db62efb | 2013-07-11 16:19:42 -0700 | [diff] [blame] | 6208 | /* |
| 6209 | * Stop new entries from being returned after we return the last |
| 6210 | * entry. |
| 6211 | * |
| 6212 | * New directory entries are assigned a strictly increasing |
| 6213 | * offset. This means that new entries created during readdir |
| 6214 | * are *guaranteed* to be seen in the future by that readdir. |
| 6215 | * This has broken buggy programs which operate on names as |
| 6216 | * they're returned by readdir. Until we re-use freed offsets |
| 6217 | * we have this hack to stop new entries from being returned |
| 6218 | * under the assumption that they'll never reach this huge |
| 6219 | * offset. |
| 6220 | * |
| 6221 | * This is being careful not to overflow 32bit loff_t unless the |
| 6222 | * last entry requires it because doing so has broken 32bit apps |
| 6223 | * in the past. |
| 6224 | */ |
Jeff Mahoney | c2951f3 | 2016-11-21 15:59:04 +0100 | [diff] [blame] | 6225 | if (ctx->pos >= INT_MAX) |
| 6226 | ctx->pos = LLONG_MAX; |
| 6227 | else |
| 6228 | ctx->pos = INT_MAX; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6229 | nopos: |
| 6230 | ret = 0; |
| 6231 | err: |
Omar Sandoval | 02dbfc9 | 2016-05-20 13:50:33 -0700 | [diff] [blame] | 6232 | if (put) |
| 6233 | btrfs_readdir_put_delayed_items(inode, &ins_list, &del_list); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6234 | btrfs_free_path(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6235 | return ret; |
| 6236 | } |
| 6237 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6238 | /* |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 6239 | * This is somewhat expensive, updating the tree every time the |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6240 | * inode changes. But, it is most likely to find the inode in cache. |
| 6241 | * FIXME, needs more benchmarking...there are no reasons other than performance |
| 6242 | * to keep or drop this code. |
| 6243 | */ |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 6244 | static int btrfs_dirty_inode(struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6245 | { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6246 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6247 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 6248 | struct btrfs_trans_handle *trans; |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 6249 | int ret; |
| 6250 | |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 6251 | if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags)) |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 6252 | return 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6253 | |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 6254 | trans = btrfs_join_transaction(root); |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 6255 | if (IS_ERR(trans)) |
| 6256 | return PTR_ERR(trans); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 6257 | |
Nikolay Borisov | 9a56fcd | 2020-11-02 16:48:59 +0200 | [diff] [blame] | 6258 | ret = btrfs_update_inode(trans, root, BTRFS_I(inode)); |
Nikolay Borisov | 4d14c5c | 2021-02-22 18:40:44 +0200 | [diff] [blame] | 6259 | if (ret && (ret == -ENOSPC || ret == -EDQUOT)) { |
Chris Mason | 94b6044 | 2010-05-26 11:02:00 -0400 | [diff] [blame] | 6260 | /* whoops, lets try again with the full transaction */ |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 6261 | btrfs_end_transaction(trans); |
Chris Mason | 94b6044 | 2010-05-26 11:02:00 -0400 | [diff] [blame] | 6262 | trans = btrfs_start_transaction(root, 1); |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 6263 | if (IS_ERR(trans)) |
| 6264 | return PTR_ERR(trans); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 6265 | |
Nikolay Borisov | 9a56fcd | 2020-11-02 16:48:59 +0200 | [diff] [blame] | 6266 | ret = btrfs_update_inode(trans, root, BTRFS_I(inode)); |
Chris Mason | 94b6044 | 2010-05-26 11:02:00 -0400 | [diff] [blame] | 6267 | } |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 6268 | btrfs_end_transaction(trans); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 6269 | if (BTRFS_I(inode)->delayed_node) |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6270 | btrfs_balance_delayed_items(fs_info); |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 6271 | |
| 6272 | return ret; |
| 6273 | } |
| 6274 | |
| 6275 | /* |
| 6276 | * This is a copy of file_update_time. We need this so we can return error on |
| 6277 | * ENOSPC for updating the inode in the case of file write and mmap writes. |
| 6278 | */ |
Deepa Dinamani | 95582b0 | 2018-05-08 19:36:02 -0700 | [diff] [blame] | 6279 | static int btrfs_update_time(struct inode *inode, struct timespec64 *now, |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 6280 | int flags) |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 6281 | { |
Alexander Block | 2bc556528 | 2012-06-15 09:49:33 +0200 | [diff] [blame] | 6282 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Jeff Layton | 3a8c723 | 2017-12-11 06:35:24 -0500 | [diff] [blame] | 6283 | bool dirty = flags & ~S_VERSION; |
Alexander Block | 2bc556528 | 2012-06-15 09:49:33 +0200 | [diff] [blame] | 6284 | |
| 6285 | if (btrfs_root_readonly(root)) |
| 6286 | return -EROFS; |
| 6287 | |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 6288 | if (flags & S_VERSION) |
Jeff Layton | 3a8c723 | 2017-12-11 06:35:24 -0500 | [diff] [blame] | 6289 | dirty |= inode_maybe_inc_iversion(inode, dirty); |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 6290 | if (flags & S_CTIME) |
| 6291 | inode->i_ctime = *now; |
| 6292 | if (flags & S_MTIME) |
| 6293 | inode->i_mtime = *now; |
| 6294 | if (flags & S_ATIME) |
| 6295 | inode->i_atime = *now; |
Jeff Layton | 3a8c723 | 2017-12-11 06:35:24 -0500 | [diff] [blame] | 6296 | return dirty ? btrfs_dirty_inode(inode) : 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6297 | } |
| 6298 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 6299 | /* |
| 6300 | * find the highest existing sequence number in a directory |
| 6301 | * and then set the in-memory index_cnt variable to reflect |
| 6302 | * free sequence numbers |
| 6303 | */ |
Nikolay Borisov | 4c570655 | 2017-02-20 13:50:32 +0200 | [diff] [blame] | 6304 | static int btrfs_set_inode_index_count(struct btrfs_inode *inode) |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6305 | { |
Nikolay Borisov | 4c570655 | 2017-02-20 13:50:32 +0200 | [diff] [blame] | 6306 | struct btrfs_root *root = inode->root; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6307 | struct btrfs_key key, found_key; |
| 6308 | struct btrfs_path *path; |
| 6309 | struct extent_buffer *leaf; |
| 6310 | int ret; |
| 6311 | |
Nikolay Borisov | 4c570655 | 2017-02-20 13:50:32 +0200 | [diff] [blame] | 6312 | key.objectid = btrfs_ino(inode); |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 6313 | key.type = BTRFS_DIR_INDEX_KEY; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6314 | key.offset = (u64)-1; |
| 6315 | |
| 6316 | path = btrfs_alloc_path(); |
| 6317 | if (!path) |
| 6318 | return -ENOMEM; |
| 6319 | |
| 6320 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 6321 | if (ret < 0) |
| 6322 | goto out; |
| 6323 | /* FIXME: we should be able to handle this */ |
| 6324 | if (ret == 0) |
| 6325 | goto out; |
| 6326 | ret = 0; |
| 6327 | |
| 6328 | /* |
| 6329 | * MAGIC NUMBER EXPLANATION: |
| 6330 | * since we search a directory based on f_pos we have to start at 2 |
| 6331 | * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody |
| 6332 | * else has to start at 2 |
| 6333 | */ |
| 6334 | if (path->slots[0] == 0) { |
Nikolay Borisov | 4c570655 | 2017-02-20 13:50:32 +0200 | [diff] [blame] | 6335 | inode->index_cnt = 2; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6336 | goto out; |
| 6337 | } |
| 6338 | |
| 6339 | path->slots[0]--; |
| 6340 | |
| 6341 | leaf = path->nodes[0]; |
| 6342 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 6343 | |
Nikolay Borisov | 4c570655 | 2017-02-20 13:50:32 +0200 | [diff] [blame] | 6344 | if (found_key.objectid != btrfs_ino(inode) || |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 6345 | found_key.type != BTRFS_DIR_INDEX_KEY) { |
Nikolay Borisov | 4c570655 | 2017-02-20 13:50:32 +0200 | [diff] [blame] | 6346 | inode->index_cnt = 2; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6347 | goto out; |
| 6348 | } |
| 6349 | |
Nikolay Borisov | 4c570655 | 2017-02-20 13:50:32 +0200 | [diff] [blame] | 6350 | inode->index_cnt = found_key.offset + 1; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6351 | out: |
| 6352 | btrfs_free_path(path); |
| 6353 | return ret; |
| 6354 | } |
| 6355 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 6356 | /* |
| 6357 | * helper to find a free sequence number in a given directory. This current |
| 6358 | * code is very simple, later versions will do smarter things in the btree |
| 6359 | */ |
Nikolay Borisov | 877574e | 2017-02-20 13:50:33 +0200 | [diff] [blame] | 6360 | int btrfs_set_inode_index(struct btrfs_inode *dir, u64 *index) |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6361 | { |
| 6362 | int ret = 0; |
| 6363 | |
Nikolay Borisov | 877574e | 2017-02-20 13:50:33 +0200 | [diff] [blame] | 6364 | if (dir->index_cnt == (u64)-1) { |
| 6365 | ret = btrfs_inode_delayed_dir_index_count(dir); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 6366 | if (ret) { |
| 6367 | ret = btrfs_set_inode_index_count(dir); |
| 6368 | if (ret) |
| 6369 | return ret; |
| 6370 | } |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6371 | } |
| 6372 | |
Nikolay Borisov | 877574e | 2017-02-20 13:50:33 +0200 | [diff] [blame] | 6373 | *index = dir->index_cnt; |
| 6374 | dir->index_cnt++; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6375 | |
| 6376 | return ret; |
| 6377 | } |
| 6378 | |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6379 | static int btrfs_insert_inode_locked(struct inode *inode) |
| 6380 | { |
| 6381 | struct btrfs_iget_args args; |
David Sterba | 0202e83 | 2020-05-15 19:35:59 +0200 | [diff] [blame] | 6382 | |
| 6383 | args.ino = BTRFS_I(inode)->location.objectid; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6384 | args.root = BTRFS_I(inode)->root; |
| 6385 | |
| 6386 | return insert_inode_locked4(inode, |
| 6387 | btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root), |
| 6388 | btrfs_find_actor, &args); |
| 6389 | } |
| 6390 | |
Anand Jain | 19aee8d | 2017-07-18 17:37:05 +0800 | [diff] [blame] | 6391 | /* |
| 6392 | * Inherit flags from the parent inode. |
| 6393 | * |
| 6394 | * Currently only the compression flags and the cow flags are inherited. |
| 6395 | */ |
| 6396 | static void btrfs_inherit_iflags(struct inode *inode, struct inode *dir) |
| 6397 | { |
| 6398 | unsigned int flags; |
| 6399 | |
| 6400 | if (!dir) |
| 6401 | return; |
| 6402 | |
| 6403 | flags = BTRFS_I(dir)->flags; |
| 6404 | |
| 6405 | if (flags & BTRFS_INODE_NOCOMPRESS) { |
| 6406 | BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS; |
| 6407 | BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS; |
| 6408 | } else if (flags & BTRFS_INODE_COMPRESS) { |
| 6409 | BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS; |
| 6410 | BTRFS_I(inode)->flags |= BTRFS_INODE_COMPRESS; |
| 6411 | } |
| 6412 | |
| 6413 | if (flags & BTRFS_INODE_NODATACOW) { |
| 6414 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW; |
| 6415 | if (S_ISREG(inode->i_mode)) |
| 6416 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM; |
| 6417 | } |
| 6418 | |
David Sterba | 7b6a221 | 2018-03-26 18:40:21 +0200 | [diff] [blame] | 6419 | btrfs_sync_inode_flags_to_i_flags(inode); |
Anand Jain | 19aee8d | 2017-07-18 17:37:05 +0800 | [diff] [blame] | 6420 | } |
| 6421 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6422 | static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans, |
| 6423 | struct btrfs_root *root, |
Christian Brauner | b3b6f5b | 2021-07-27 12:48:41 +0200 | [diff] [blame] | 6424 | struct user_namespace *mnt_userns, |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6425 | struct inode *dir, |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 6426 | const char *name, int name_len, |
Al Viro | 175a4eb | 2011-07-26 03:30:54 -0400 | [diff] [blame] | 6427 | u64 ref_objectid, u64 objectid, |
| 6428 | umode_t mode, u64 *index) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6429 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6430 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6431 | struct inode *inode; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6432 | struct btrfs_inode_item *inode_item; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6433 | struct btrfs_key *location; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6434 | struct btrfs_path *path; |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 6435 | struct btrfs_inode_ref *ref; |
| 6436 | struct btrfs_key key[2]; |
| 6437 | u32 sizes[2]; |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 6438 | int nitems = name ? 2 : 1; |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 6439 | unsigned long ptr; |
Josef Bacik | 11a19a9 | 2019-09-09 10:12:04 -0400 | [diff] [blame] | 6440 | unsigned int nofs_flag; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6441 | int ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6442 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6443 | path = btrfs_alloc_path(); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 6444 | if (!path) |
| 6445 | return ERR_PTR(-ENOMEM); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6446 | |
Josef Bacik | 11a19a9 | 2019-09-09 10:12:04 -0400 | [diff] [blame] | 6447 | nofs_flag = memalloc_nofs_save(); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6448 | inode = new_inode(fs_info->sb); |
Josef Bacik | 11a19a9 | 2019-09-09 10:12:04 -0400 | [diff] [blame] | 6449 | memalloc_nofs_restore(nofs_flag); |
Yoshinori Sano | 8fb2764 | 2011-04-09 02:30:07 +0000 | [diff] [blame] | 6450 | if (!inode) { |
| 6451 | btrfs_free_path(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6452 | return ERR_PTR(-ENOMEM); |
Yoshinori Sano | 8fb2764 | 2011-04-09 02:30:07 +0000 | [diff] [blame] | 6453 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6454 | |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 6455 | /* |
Filipe Manana | 5762b5c | 2014-08-01 00:10:32 +0100 | [diff] [blame] | 6456 | * O_TMPFILE, set link count to 0, so that after this point, |
| 6457 | * we fill in an inode item with the correct link count. |
| 6458 | */ |
| 6459 | if (!name) |
| 6460 | set_nlink(inode, 0); |
| 6461 | |
| 6462 | /* |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 6463 | * we have to initialize this early, so we can reclaim the inode |
| 6464 | * number if we fail afterwards in this function. |
| 6465 | */ |
| 6466 | inode->i_ino = objectid; |
| 6467 | |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 6468 | if (dir && name) { |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 6469 | trace_btrfs_inode_request(dir); |
| 6470 | |
Nikolay Borisov | 877574e | 2017-02-20 13:50:33 +0200 | [diff] [blame] | 6471 | ret = btrfs_set_inode_index(BTRFS_I(dir), index); |
Shen Feng | 0977143 | 2009-04-02 16:46:06 -0400 | [diff] [blame] | 6472 | if (ret) { |
Yoshinori Sano | 8fb2764 | 2011-04-09 02:30:07 +0000 | [diff] [blame] | 6473 | btrfs_free_path(path); |
Shen Feng | 0977143 | 2009-04-02 16:46:06 -0400 | [diff] [blame] | 6474 | iput(inode); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6475 | return ERR_PTR(ret); |
Shen Feng | 0977143 | 2009-04-02 16:46:06 -0400 | [diff] [blame] | 6476 | } |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 6477 | } else if (dir) { |
| 6478 | *index = 0; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6479 | } |
| 6480 | /* |
| 6481 | * index_cnt is ignored for everything but a dir, |
Su Yue | df6703e | 2018-01-12 11:08:02 +0800 | [diff] [blame] | 6482 | * btrfs_set_inode_index_count has an explanation for the magic |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6483 | * number |
| 6484 | */ |
| 6485 | BTRFS_I(inode)->index_cnt = 2; |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 6486 | BTRFS_I(inode)->dir_index = *index; |
Josef Bacik | 5c8fd99 | 2020-02-14 16:11:43 -0500 | [diff] [blame] | 6487 | BTRFS_I(inode)->root = btrfs_grab_root(root); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 6488 | BTRFS_I(inode)->generation = trans->transid; |
Josef Bacik | 7619585 | 2010-11-19 02:18:02 +0000 | [diff] [blame] | 6489 | inode->i_generation = BTRFS_I(inode)->generation; |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6490 | |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 6491 | /* |
| 6492 | * We could have gotten an inode number from somebody who was fsynced |
| 6493 | * and then removed in this same transaction, so let's just set full |
| 6494 | * sync since it will be a full sync anyway and this will blow away the |
| 6495 | * old info in the log. |
| 6496 | */ |
| 6497 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags); |
| 6498 | |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 6499 | key[0].objectid = objectid; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 6500 | key[0].type = BTRFS_INODE_ITEM_KEY; |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 6501 | key[0].offset = 0; |
| 6502 | |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 6503 | sizes[0] = sizeof(struct btrfs_inode_item); |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 6504 | |
| 6505 | if (name) { |
| 6506 | /* |
| 6507 | * Start new inodes with an inode_ref. This is slightly more |
| 6508 | * efficient for small numbers of hard links since they will |
| 6509 | * be packed into one item. Extended refs will kick in if we |
| 6510 | * add more hard links than can fit in the ref item. |
| 6511 | */ |
| 6512 | key[1].objectid = objectid; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 6513 | key[1].type = BTRFS_INODE_REF_KEY; |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 6514 | key[1].offset = ref_objectid; |
| 6515 | |
| 6516 | sizes[1] = name_len + sizeof(*ref); |
| 6517 | } |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 6518 | |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6519 | location = &BTRFS_I(inode)->location; |
| 6520 | location->objectid = objectid; |
| 6521 | location->offset = 0; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 6522 | location->type = BTRFS_INODE_ITEM_KEY; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6523 | |
| 6524 | ret = btrfs_insert_inode_locked(inode); |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6525 | if (ret < 0) { |
| 6526 | iput(inode); |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6527 | goto fail; |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6528 | } |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6529 | |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 6530 | ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems); |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 6531 | if (ret != 0) |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6532 | goto fail_unlock; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6533 | |
Christian Brauner | b3b6f5b | 2021-07-27 12:48:41 +0200 | [diff] [blame] | 6534 | inode_init_owner(mnt_userns, inode, dir, mode); |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 6535 | inode_set_bytes(inode, 0); |
chandan r | 9cc97d6 | 2012-07-04 12:48:07 +0530 | [diff] [blame] | 6536 | |
Deepa Dinamani | c2050a4 | 2016-09-14 07:48:06 -0700 | [diff] [blame] | 6537 | inode->i_mtime = current_time(inode); |
chandan r | 9cc97d6 | 2012-07-04 12:48:07 +0530 | [diff] [blame] | 6538 | inode->i_atime = inode->i_mtime; |
| 6539 | inode->i_ctime = inode->i_mtime; |
Arnd Bergmann | d3c6be6f | 2018-06-21 18:04:06 +0200 | [diff] [blame] | 6540 | BTRFS_I(inode)->i_otime = inode->i_mtime; |
chandan r | 9cc97d6 | 2012-07-04 12:48:07 +0530 | [diff] [blame] | 6541 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6542 | inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 6543 | struct btrfs_inode_item); |
David Sterba | b159fa2 | 2016-11-08 18:09:03 +0100 | [diff] [blame] | 6544 | memzero_extent_buffer(path->nodes[0], (unsigned long)inode_item, |
Li Zefan | 293f7e0 | 2012-07-10 00:58:58 -0600 | [diff] [blame] | 6545 | sizeof(*inode_item)); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 6546 | fill_inode_item(trans, path->nodes[0], inode_item, inode); |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 6547 | |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 6548 | if (name) { |
| 6549 | ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1, |
| 6550 | struct btrfs_inode_ref); |
| 6551 | btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len); |
| 6552 | btrfs_set_inode_ref_index(path->nodes[0], ref, *index); |
| 6553 | ptr = (unsigned long)(ref + 1); |
| 6554 | write_extent_buffer(path->nodes[0], name, ptr, name_len); |
| 6555 | } |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 6556 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6557 | btrfs_mark_buffer_dirty(path->nodes[0]); |
| 6558 | btrfs_free_path(path); |
| 6559 | |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 6560 | btrfs_inherit_iflags(inode, dir); |
| 6561 | |
Al Viro | 569254b | 2011-07-24 17:08:40 -0400 | [diff] [blame] | 6562 | if (S_ISREG(mode)) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6563 | if (btrfs_test_opt(fs_info, NODATASUM)) |
Chris Mason | 9427216 | 2009-07-02 12:26:06 -0400 | [diff] [blame] | 6564 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6565 | if (btrfs_test_opt(fs_info, NODATACOW)) |
Josef Bacik | f2bdf9a | 2013-02-21 15:28:28 -0500 | [diff] [blame] | 6566 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW | |
| 6567 | BTRFS_INODE_NODATASUM; |
Chris Mason | 9427216 | 2009-07-02 12:26:06 -0400 | [diff] [blame] | 6568 | } |
| 6569 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6570 | inode_tree_add(inode); |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 6571 | |
| 6572 | trace_btrfs_inode_new(inode); |
Nikolay Borisov | d909441 | 2020-06-05 10:41:13 +0300 | [diff] [blame] | 6573 | btrfs_set_inode_last_trans(trans, BTRFS_I(inode)); |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 6574 | |
Alexander Block | 8ea05e3 | 2012-07-25 17:35:53 +0200 | [diff] [blame] | 6575 | btrfs_update_root_times(trans, root); |
| 6576 | |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 6577 | ret = btrfs_inode_inherit_props(trans, inode, dir); |
| 6578 | if (ret) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6579 | btrfs_err(fs_info, |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 6580 | "error inheriting props for ino %llu (root %llu): %d", |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 6581 | btrfs_ino(BTRFS_I(inode)), root->root_key.objectid, ret); |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 6582 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6583 | return inode; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6584 | |
| 6585 | fail_unlock: |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6586 | discard_new_inode(inode); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6587 | fail: |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 6588 | if (dir && name) |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6589 | BTRFS_I(dir)->index_cnt--; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6590 | btrfs_free_path(path); |
| 6591 | return ERR_PTR(ret); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6592 | } |
| 6593 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 6594 | /* |
| 6595 | * utility function to add 'inode' into 'parent_inode' with |
| 6596 | * a give name and a given sequence number. |
| 6597 | * if 'add_backref' is true, also insert a backref from the |
| 6598 | * inode to the parent directory. |
| 6599 | */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 6600 | int btrfs_add_link(struct btrfs_trans_handle *trans, |
Nikolay Borisov | db0a669 | 2017-02-20 13:51:08 +0200 | [diff] [blame] | 6601 | struct btrfs_inode *parent_inode, struct btrfs_inode *inode, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 6602 | const char *name, int name_len, int add_backref, u64 index) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6603 | { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6604 | int ret = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6605 | struct btrfs_key key; |
Nikolay Borisov | db0a669 | 2017-02-20 13:51:08 +0200 | [diff] [blame] | 6606 | struct btrfs_root *root = parent_inode->root; |
| 6607 | u64 ino = btrfs_ino(inode); |
| 6608 | u64 parent_ino = btrfs_ino(parent_inode); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6609 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6610 | if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) { |
Nikolay Borisov | db0a669 | 2017-02-20 13:51:08 +0200 | [diff] [blame] | 6611 | memcpy(&key, &inode->root->root_key, sizeof(key)); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6612 | } else { |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6613 | key.objectid = ino; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 6614 | key.type = BTRFS_INODE_ITEM_KEY; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6615 | key.offset = 0; |
| 6616 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6617 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6618 | if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) { |
Lu Fengqi | 6025c19 | 2018-08-01 11:32:29 +0800 | [diff] [blame] | 6619 | ret = btrfs_add_root_ref(trans, key.objectid, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6620 | root->root_key.objectid, parent_ino, |
| 6621 | index, name, name_len); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6622 | } else if (add_backref) { |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6623 | ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino, |
| 6624 | parent_ino, index); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6625 | } |
| 6626 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6627 | /* Nothing to clean up yet */ |
| 6628 | if (ret) |
| 6629 | return ret; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6630 | |
Lu Fengqi | 684572d | 2018-08-04 21:10:57 +0800 | [diff] [blame] | 6631 | ret = btrfs_insert_dir_item(trans, name, name_len, parent_inode, &key, |
Nikolay Borisov | db0a669 | 2017-02-20 13:51:08 +0200 | [diff] [blame] | 6632 | btrfs_inode_type(&inode->vfs_inode), index); |
Chris Mason | 9c52057 | 2012-12-17 14:26:57 -0500 | [diff] [blame] | 6633 | if (ret == -EEXIST || ret == -EOVERFLOW) |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6634 | goto fail_dir_item; |
| 6635 | else if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 6636 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6637 | return ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6638 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6639 | |
Nikolay Borisov | db0a669 | 2017-02-20 13:51:08 +0200 | [diff] [blame] | 6640 | btrfs_i_size_write(parent_inode, parent_inode->vfs_inode.i_size + |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6641 | name_len * 2); |
Nikolay Borisov | db0a669 | 2017-02-20 13:51:08 +0200 | [diff] [blame] | 6642 | inode_inc_iversion(&parent_inode->vfs_inode); |
Filipe Manana | 5338e43 | 2019-05-15 16:02:47 +0100 | [diff] [blame] | 6643 | /* |
| 6644 | * If we are replaying a log tree, we do not want to update the mtime |
| 6645 | * and ctime of the parent directory with the current time, since the |
| 6646 | * log replay procedure is responsible for setting them to their correct |
| 6647 | * values (the ones it had when the fsync was done). |
| 6648 | */ |
| 6649 | if (!test_bit(BTRFS_FS_LOG_RECOVERING, &root->fs_info->flags)) { |
| 6650 | struct timespec64 now = current_time(&parent_inode->vfs_inode); |
| 6651 | |
| 6652 | parent_inode->vfs_inode.i_mtime = now; |
| 6653 | parent_inode->vfs_inode.i_ctime = now; |
| 6654 | } |
Nikolay Borisov | 9a56fcd | 2020-11-02 16:48:59 +0200 | [diff] [blame] | 6655 | ret = btrfs_update_inode(trans, root, parent_inode); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6656 | if (ret) |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 6657 | btrfs_abort_transaction(trans, ret); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6658 | return ret; |
Chris Mason | fe66a05 | 2012-02-20 08:40:56 -0500 | [diff] [blame] | 6659 | |
| 6660 | fail_dir_item: |
| 6661 | if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) { |
| 6662 | u64 local_index; |
| 6663 | int err; |
Lu Fengqi | 3ee1c55 | 2018-08-01 11:32:28 +0800 | [diff] [blame] | 6664 | err = btrfs_del_root_ref(trans, key.objectid, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6665 | root->root_key.objectid, parent_ino, |
| 6666 | &local_index, name, name_len); |
Johannes Thumshirn | 1690dd41 | 2018-12-12 15:14:17 +0100 | [diff] [blame] | 6667 | if (err) |
| 6668 | btrfs_abort_transaction(trans, err); |
Chris Mason | fe66a05 | 2012-02-20 08:40:56 -0500 | [diff] [blame] | 6669 | } else if (add_backref) { |
| 6670 | u64 local_index; |
| 6671 | int err; |
| 6672 | |
| 6673 | err = btrfs_del_inode_ref(trans, root, name, name_len, |
| 6674 | ino, parent_ino, &local_index); |
Johannes Thumshirn | 1690dd41 | 2018-12-12 15:14:17 +0100 | [diff] [blame] | 6675 | if (err) |
| 6676 | btrfs_abort_transaction(trans, err); |
Chris Mason | fe66a05 | 2012-02-20 08:40:56 -0500 | [diff] [blame] | 6677 | } |
Johannes Thumshirn | 1690dd41 | 2018-12-12 15:14:17 +0100 | [diff] [blame] | 6678 | |
| 6679 | /* Return the original error code */ |
Chris Mason | fe66a05 | 2012-02-20 08:40:56 -0500 | [diff] [blame] | 6680 | return ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6681 | } |
| 6682 | |
| 6683 | static int btrfs_add_nondir(struct btrfs_trans_handle *trans, |
Nikolay Borisov | cef415a | 2017-02-20 13:51:09 +0200 | [diff] [blame] | 6684 | struct btrfs_inode *dir, struct dentry *dentry, |
| 6685 | struct btrfs_inode *inode, int backref, u64 index) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6686 | { |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 6687 | int err = btrfs_add_link(trans, dir, inode, |
| 6688 | dentry->d_name.name, dentry->d_name.len, |
| 6689 | backref, index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6690 | if (err > 0) |
| 6691 | err = -EEXIST; |
| 6692 | return err; |
| 6693 | } |
| 6694 | |
Christian Brauner | 549c729 | 2021-01-21 14:19:43 +0100 | [diff] [blame] | 6695 | static int btrfs_mknod(struct user_namespace *mnt_userns, struct inode *dir, |
| 6696 | struct dentry *dentry, umode_t mode, dev_t rdev) |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6697 | { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6698 | struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6699 | struct btrfs_trans_handle *trans; |
| 6700 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 6701 | struct inode *inode = NULL; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6702 | int err; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6703 | u64 objectid; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 6704 | u64 index = 0; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6705 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 6706 | /* |
| 6707 | * 2 for inode item and ref |
| 6708 | * 2 for dir items |
| 6709 | * 1 for xattr if selinux is on |
| 6710 | */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6711 | trans = btrfs_start_transaction(root, 5); |
| 6712 | if (IS_ERR(trans)) |
| 6713 | return PTR_ERR(trans); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 6714 | |
Nikolay Borisov | 543068a | 2020-12-07 17:32:33 +0200 | [diff] [blame] | 6715 | err = btrfs_get_free_objectid(root, &objectid); |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 6716 | if (err) |
| 6717 | goto out_unlock; |
| 6718 | |
Christian Brauner | 7210527 | 2021-07-27 12:48:44 +0200 | [diff] [blame] | 6719 | inode = btrfs_new_inode(trans, root, mnt_userns, dir, |
Christian Brauner | b3b6f5b | 2021-07-27 12:48:41 +0200 | [diff] [blame] | 6720 | dentry->d_name.name, dentry->d_name.len, |
| 6721 | btrfs_ino(BTRFS_I(dir)), objectid, mode, &index); |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 6722 | if (IS_ERR(inode)) { |
| 6723 | err = PTR_ERR(inode); |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6724 | inode = NULL; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6725 | goto out_unlock; |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 6726 | } |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6727 | |
Casey Schaufler | ad19db7 | 2011-12-15 10:09:07 -0500 | [diff] [blame] | 6728 | /* |
| 6729 | * If the active LSM wants to access the inode during |
| 6730 | * d_instantiate it needs these. Smack checks to see |
| 6731 | * if the filesystem supports xattrs by looking at the |
| 6732 | * ops vector. |
| 6733 | */ |
Casey Schaufler | ad19db7 | 2011-12-15 10:09:07 -0500 | [diff] [blame] | 6734 | inode->i_op = &btrfs_special_inode_operations; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6735 | init_special_inode(inode, inode->i_mode, rdev); |
| 6736 | |
| 6737 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6738 | if (err) |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6739 | goto out_unlock; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6740 | |
Nikolay Borisov | cef415a | 2017-02-20 13:51:09 +0200 | [diff] [blame] | 6741 | err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode), |
| 6742 | 0, index); |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6743 | if (err) |
| 6744 | goto out_unlock; |
| 6745 | |
Nikolay Borisov | 9a56fcd | 2020-11-02 16:48:59 +0200 | [diff] [blame] | 6746 | btrfs_update_inode(trans, root, BTRFS_I(inode)); |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6747 | d_instantiate_new(dentry, inode); |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6748 | |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6749 | out_unlock: |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 6750 | btrfs_end_transaction(trans); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6751 | btrfs_btree_balance_dirty(fs_info); |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6752 | if (err && inode) { |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6753 | inode_dec_link_count(inode); |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6754 | discard_new_inode(inode); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6755 | } |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6756 | return err; |
| 6757 | } |
| 6758 | |
Christian Brauner | 549c729 | 2021-01-21 14:19:43 +0100 | [diff] [blame] | 6759 | static int btrfs_create(struct user_namespace *mnt_userns, struct inode *dir, |
| 6760 | struct dentry *dentry, umode_t mode, bool excl) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6761 | { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6762 | struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6763 | struct btrfs_trans_handle *trans; |
| 6764 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 6765 | struct inode *inode = NULL; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6766 | int err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6767 | u64 objectid; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 6768 | u64 index = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6769 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 6770 | /* |
| 6771 | * 2 for inode item and ref |
| 6772 | * 2 for dir items |
| 6773 | * 1 for xattr if selinux is on |
| 6774 | */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6775 | trans = btrfs_start_transaction(root, 5); |
| 6776 | if (IS_ERR(trans)) |
| 6777 | return PTR_ERR(trans); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 6778 | |
Nikolay Borisov | 543068a | 2020-12-07 17:32:33 +0200 | [diff] [blame] | 6779 | err = btrfs_get_free_objectid(root, &objectid); |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 6780 | if (err) |
| 6781 | goto out_unlock; |
| 6782 | |
Christian Brauner | e93ca49 | 2021-07-27 12:48:45 +0200 | [diff] [blame] | 6783 | inode = btrfs_new_inode(trans, root, mnt_userns, dir, |
Christian Brauner | b3b6f5b | 2021-07-27 12:48:41 +0200 | [diff] [blame] | 6784 | dentry->d_name.name, dentry->d_name.len, |
| 6785 | btrfs_ino(BTRFS_I(dir)), objectid, mode, &index); |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 6786 | if (IS_ERR(inode)) { |
| 6787 | err = PTR_ERR(inode); |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6788 | inode = NULL; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6789 | goto out_unlock; |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 6790 | } |
Casey Schaufler | ad19db7 | 2011-12-15 10:09:07 -0500 | [diff] [blame] | 6791 | /* |
| 6792 | * If the active LSM wants to access the inode during |
| 6793 | * d_instantiate it needs these. Smack checks to see |
| 6794 | * if the filesystem supports xattrs by looking at the |
| 6795 | * ops vector. |
| 6796 | */ |
| 6797 | inode->i_fop = &btrfs_file_operations; |
| 6798 | inode->i_op = &btrfs_file_inode_operations; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6799 | inode->i_mapping->a_ops = &btrfs_aops; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6800 | |
| 6801 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); |
| 6802 | if (err) |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6803 | goto out_unlock; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6804 | |
Nikolay Borisov | 9a56fcd | 2020-11-02 16:48:59 +0200 | [diff] [blame] | 6805 | err = btrfs_update_inode(trans, root, BTRFS_I(inode)); |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6806 | if (err) |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6807 | goto out_unlock; |
Casey Schaufler | ad19db7 | 2011-12-15 10:09:07 -0500 | [diff] [blame] | 6808 | |
Nikolay Borisov | cef415a | 2017-02-20 13:51:09 +0200 | [diff] [blame] | 6809 | err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode), |
| 6810 | 0, index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6811 | if (err) |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6812 | goto out_unlock; |
Filipe Brandenburger | 43baa57 | 2012-11-30 03:40:09 +0000 | [diff] [blame] | 6813 | |
Al Viro | 1e2e547 | 2018-05-04 08:23:01 -0400 | [diff] [blame] | 6814 | d_instantiate_new(dentry, inode); |
Filipe Brandenburger | 43baa57 | 2012-11-30 03:40:09 +0000 | [diff] [blame] | 6815 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6816 | out_unlock: |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 6817 | btrfs_end_transaction(trans); |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6818 | if (err && inode) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6819 | inode_dec_link_count(inode); |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6820 | discard_new_inode(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6821 | } |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6822 | btrfs_btree_balance_dirty(fs_info); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6823 | return err; |
| 6824 | } |
| 6825 | |
| 6826 | static int btrfs_link(struct dentry *old_dentry, struct inode *dir, |
| 6827 | struct dentry *dentry) |
| 6828 | { |
Filipe Manana | 271dba45 | 2016-01-05 16:24:05 +0000 | [diff] [blame] | 6829 | struct btrfs_trans_handle *trans = NULL; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6830 | struct btrfs_root *root = BTRFS_I(dir)->root; |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 6831 | struct inode *inode = d_inode(old_dentry); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6832 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 6833 | u64 index; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6834 | int err; |
| 6835 | int drop_inode = 0; |
| 6836 | |
TARUISI Hiroaki | 4a8be42 | 2009-11-12 07:14:26 +0000 | [diff] [blame] | 6837 | /* do not allow sys_link's with other subvols of the same device */ |
Misono Tomohiro | 4fd786e | 2018-08-06 14:25:24 +0900 | [diff] [blame] | 6838 | if (root->root_key.objectid != BTRFS_I(inode)->root->root_key.objectid) |
Mark Fasheh | 3ab3564 | 2011-03-22 17:20:26 +0000 | [diff] [blame] | 6839 | return -EXDEV; |
TARUISI Hiroaki | 4a8be42 | 2009-11-12 07:14:26 +0000 | [diff] [blame] | 6840 | |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 6841 | if (inode->i_nlink >= BTRFS_LINK_MAX) |
Al Viro | c055e99 | 2011-03-04 17:15:18 +0000 | [diff] [blame] | 6842 | return -EMLINK; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 6843 | |
Nikolay Borisov | 877574e | 2017-02-20 13:50:33 +0200 | [diff] [blame] | 6844 | err = btrfs_set_inode_index(BTRFS_I(dir), &index); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6845 | if (err) |
| 6846 | goto fail; |
| 6847 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6848 | /* |
Miao Xie | 7e6b646 | 2011-02-18 09:21:17 +0000 | [diff] [blame] | 6849 | * 2 items for inode and inode ref |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6850 | * 2 items for dir items |
Miao Xie | 7e6b646 | 2011-02-18 09:21:17 +0000 | [diff] [blame] | 6851 | * 1 item for parent inode |
Omar Sandoval | 399b0bb | 2018-05-11 13:13:40 -0700 | [diff] [blame] | 6852 | * 1 item for orphan item deletion if O_TMPFILE |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6853 | */ |
Omar Sandoval | 399b0bb | 2018-05-11 13:13:40 -0700 | [diff] [blame] | 6854 | trans = btrfs_start_transaction(root, inode->i_nlink ? 5 : 6); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6855 | if (IS_ERR(trans)) { |
| 6856 | err = PTR_ERR(trans); |
Filipe Manana | 271dba45 | 2016-01-05 16:24:05 +0000 | [diff] [blame] | 6857 | trans = NULL; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6858 | goto fail; |
| 6859 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6860 | |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 6861 | /* There are several dir indexes for this inode, clear the cache. */ |
| 6862 | BTRFS_I(inode)->dir_index = 0ULL; |
Zach Brown | 8b558c5 | 2013-10-16 12:10:34 -0700 | [diff] [blame] | 6863 | inc_nlink(inode); |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 6864 | inode_inc_iversion(inode); |
Deepa Dinamani | c2050a4 | 2016-09-14 07:48:06 -0700 | [diff] [blame] | 6865 | inode->i_ctime = current_time(inode); |
Al Viro | 7de9c6ee | 2010-10-23 11:11:40 -0400 | [diff] [blame] | 6866 | ihold(inode); |
Josef Bacik | e997615 | 2012-10-11 15:53:56 -0400 | [diff] [blame] | 6867 | set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6868 | |
Nikolay Borisov | cef415a | 2017-02-20 13:51:09 +0200 | [diff] [blame] | 6869 | err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode), |
| 6870 | 1, index); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6871 | |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 6872 | if (err) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6873 | drop_inode = 1; |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 6874 | } else { |
Al Viro | 10d9f30 | 2011-07-16 23:09:10 -0400 | [diff] [blame] | 6875 | struct dentry *parent = dentry->d_parent; |
Filipe Manana | d4682ba | 2018-06-11 19:24:28 +0100 | [diff] [blame] | 6876 | |
Nikolay Borisov | 9a56fcd | 2020-11-02 16:48:59 +0200 | [diff] [blame] | 6877 | err = btrfs_update_inode(trans, root, BTRFS_I(inode)); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6878 | if (err) |
| 6879 | goto fail; |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 6880 | if (inode->i_nlink == 1) { |
| 6881 | /* |
| 6882 | * If new hard link count is 1, it's a file created |
| 6883 | * with open(2) O_TMPFILE flag. |
| 6884 | */ |
Nikolay Borisov | 3d6ae7b | 2017-02-20 13:50:58 +0200 | [diff] [blame] | 6885 | err = btrfs_orphan_del(trans, BTRFS_I(inode)); |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 6886 | if (err) |
| 6887 | goto fail; |
| 6888 | } |
Al Viro | 08c422c | 2011-12-23 07:58:13 -0500 | [diff] [blame] | 6889 | d_instantiate(dentry, inode); |
Filipe Manana | 75b463d | 2020-08-11 12:43:48 +0100 | [diff] [blame] | 6890 | btrfs_log_new_name(trans, BTRFS_I(inode), NULL, parent); |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 6891 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6892 | |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 6893 | fail: |
Filipe Manana | 271dba45 | 2016-01-05 16:24:05 +0000 | [diff] [blame] | 6894 | if (trans) |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 6895 | btrfs_end_transaction(trans); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6896 | if (drop_inode) { |
| 6897 | inode_dec_link_count(inode); |
| 6898 | iput(inode); |
| 6899 | } |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6900 | btrfs_btree_balance_dirty(fs_info); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6901 | return err; |
| 6902 | } |
| 6903 | |
Christian Brauner | 549c729 | 2021-01-21 14:19:43 +0100 | [diff] [blame] | 6904 | static int btrfs_mkdir(struct user_namespace *mnt_userns, struct inode *dir, |
| 6905 | struct dentry *dentry, umode_t mode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6906 | { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6907 | struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); |
Chris Mason | b9d8666 | 2008-05-02 16:13:49 -0400 | [diff] [blame] | 6908 | struct inode *inode = NULL; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6909 | struct btrfs_trans_handle *trans; |
| 6910 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 6911 | int err = 0; |
Chris Mason | b9d8666 | 2008-05-02 16:13:49 -0400 | [diff] [blame] | 6912 | u64 objectid = 0; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 6913 | u64 index = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6914 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 6915 | /* |
| 6916 | * 2 items for inode and ref |
| 6917 | * 2 items for dir items |
| 6918 | * 1 for xattr if selinux is on |
| 6919 | */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6920 | trans = btrfs_start_transaction(root, 5); |
| 6921 | if (IS_ERR(trans)) |
| 6922 | return PTR_ERR(trans); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6923 | |
Nikolay Borisov | 543068a | 2020-12-07 17:32:33 +0200 | [diff] [blame] | 6924 | err = btrfs_get_free_objectid(root, &objectid); |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 6925 | if (err) |
| 6926 | goto out_fail; |
| 6927 | |
Christian Brauner | b0b3e44 | 2021-07-27 12:48:46 +0200 | [diff] [blame] | 6928 | inode = btrfs_new_inode(trans, root, mnt_userns, dir, |
Christian Brauner | b3b6f5b | 2021-07-27 12:48:41 +0200 | [diff] [blame] | 6929 | dentry->d_name.name, dentry->d_name.len, |
| 6930 | btrfs_ino(BTRFS_I(dir)), objectid, |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 6931 | S_IFDIR | mode, &index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6932 | if (IS_ERR(inode)) { |
| 6933 | err = PTR_ERR(inode); |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6934 | inode = NULL; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6935 | goto out_fail; |
| 6936 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6937 | |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6938 | /* these must be set before we unlock the inode */ |
| 6939 | inode->i_op = &btrfs_dir_inode_operations; |
| 6940 | inode->i_fop = &btrfs_dir_file_operations; |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 6941 | |
Eric Paris | 2a7dba3 | 2011-02-01 11:05:39 -0500 | [diff] [blame] | 6942 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 6943 | if (err) |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6944 | goto out_fail; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6945 | |
Nikolay Borisov | 6ef06d2 | 2017-02-20 13:50:34 +0200 | [diff] [blame] | 6946 | btrfs_i_size_write(BTRFS_I(inode), 0); |
Nikolay Borisov | 9a56fcd | 2020-11-02 16:48:59 +0200 | [diff] [blame] | 6947 | err = btrfs_update_inode(trans, root, BTRFS_I(inode)); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6948 | if (err) |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6949 | goto out_fail; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6950 | |
Nikolay Borisov | db0a669 | 2017-02-20 13:51:08 +0200 | [diff] [blame] | 6951 | err = btrfs_add_link(trans, BTRFS_I(dir), BTRFS_I(inode), |
| 6952 | dentry->d_name.name, |
| 6953 | dentry->d_name.len, 0, index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6954 | if (err) |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6955 | goto out_fail; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6956 | |
Al Viro | 1e2e547 | 2018-05-04 08:23:01 -0400 | [diff] [blame] | 6957 | d_instantiate_new(dentry, inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6958 | |
| 6959 | out_fail: |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 6960 | btrfs_end_transaction(trans); |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6961 | if (err && inode) { |
Wang Shilong | c7cfb8a | 2014-12-24 14:45:30 +0800 | [diff] [blame] | 6962 | inode_dec_link_count(inode); |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6963 | discard_new_inode(inode); |
Wang Shilong | c7cfb8a | 2014-12-24 14:45:30 +0800 | [diff] [blame] | 6964 | } |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6965 | btrfs_btree_balance_dirty(fs_info); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6966 | return err; |
| 6967 | } |
| 6968 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6969 | static noinline int uncompress_inline(struct btrfs_path *path, |
Byongho Lee | e40da0e | 2015-05-19 23:46:45 +0900 | [diff] [blame] | 6970 | struct page *page, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6971 | size_t pg_offset, u64 extent_offset, |
| 6972 | struct btrfs_file_extent_item *item) |
| 6973 | { |
| 6974 | int ret; |
| 6975 | struct extent_buffer *leaf = path->nodes[0]; |
| 6976 | char *tmp; |
| 6977 | size_t max_size; |
| 6978 | unsigned long inline_size; |
| 6979 | unsigned long ptr; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 6980 | int compress_type; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6981 | |
| 6982 | WARN_ON(pg_offset != 0); |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 6983 | compress_type = btrfs_file_extent_compression(leaf, item); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6984 | max_size = btrfs_file_extent_ram_bytes(leaf, item); |
| 6985 | inline_size = btrfs_file_extent_inline_item_len(leaf, |
Ross Kirk | dd3cc16 | 2013-09-16 15:58:09 +0100 | [diff] [blame] | 6986 | btrfs_item_nr(path->slots[0])); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6987 | tmp = kmalloc(inline_size, GFP_NOFS); |
Tsutomu Itoh | 8d41371 | 2011-04-25 19:43:52 -0400 | [diff] [blame] | 6988 | if (!tmp) |
| 6989 | return -ENOMEM; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6990 | ptr = btrfs_file_extent_inline_start(item); |
| 6991 | |
| 6992 | read_extent_buffer(leaf, tmp, ptr, inline_size); |
| 6993 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 6994 | max_size = min_t(unsigned long, PAGE_SIZE, max_size); |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 6995 | ret = btrfs_decompress(compress_type, tmp, page, |
| 6996 | extent_offset, inline_size, max_size); |
Zygo Blaxell | e1699d2 | 2017-03-10 16:45:44 -0500 | [diff] [blame] | 6997 | |
| 6998 | /* |
| 6999 | * decompression code contains a memset to fill in any space between the end |
| 7000 | * of the uncompressed data and the end of max_size in case the decompressed |
| 7001 | * data ends up shorter than ram_bytes. That doesn't cover the hole between |
| 7002 | * the end of an inline extent and the beginning of the next block, so we |
| 7003 | * cover that region here. |
| 7004 | */ |
| 7005 | |
Ira Weiny | d048b9c | 2021-05-04 18:40:07 -0700 | [diff] [blame] | 7006 | if (max_size + pg_offset < PAGE_SIZE) |
| 7007 | memzero_page(page, pg_offset + max_size, |
| 7008 | PAGE_SIZE - max_size - pg_offset); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 7009 | kfree(tmp); |
Zach Brown | 166ae5a | 2014-05-09 17:15:10 -0400 | [diff] [blame] | 7010 | return ret; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 7011 | } |
| 7012 | |
Omar Sandoval | 39b07b5 | 2019-12-02 17:34:23 -0800 | [diff] [blame] | 7013 | /** |
| 7014 | * btrfs_get_extent - Lookup the first extent overlapping a range in a file. |
| 7015 | * @inode: file to search in |
| 7016 | * @page: page to read extent data into if the extent is inline |
| 7017 | * @pg_offset: offset into @page to copy to |
| 7018 | * @start: file offset |
| 7019 | * @len: length of range starting at @start |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 7020 | * |
Omar Sandoval | 39b07b5 | 2019-12-02 17:34:23 -0800 | [diff] [blame] | 7021 | * This returns the first &struct extent_map which overlaps with the given |
| 7022 | * range, reading it from the B-tree and caching it if necessary. Note that |
| 7023 | * there may be more extents which overlap the given range after the returned |
| 7024 | * extent_map. |
| 7025 | * |
| 7026 | * If @page is not NULL and the extent is inline, this also reads the extent |
| 7027 | * data directly into the page and marks the extent up to date in the io_tree. |
| 7028 | * |
| 7029 | * Return: ERR_PTR on error, non-NULL extent_map on success. |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 7030 | */ |
Nikolay Borisov | fc4f21b1 | 2017-02-20 13:51:06 +0200 | [diff] [blame] | 7031 | struct extent_map *btrfs_get_extent(struct btrfs_inode *inode, |
Omar Sandoval | 39b07b5 | 2019-12-02 17:34:23 -0800 | [diff] [blame] | 7032 | struct page *page, size_t pg_offset, |
| 7033 | u64 start, u64 len) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7034 | { |
David Sterba | 3ffbd68 | 2018-06-29 10:56:42 +0200 | [diff] [blame] | 7035 | struct btrfs_fs_info *fs_info = inode->root->fs_info; |
Nikolay Borisov | 1028d1c | 2020-08-03 12:58:46 +0300 | [diff] [blame] | 7036 | int ret = 0; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7037 | u64 extent_start = 0; |
| 7038 | u64 extent_end = 0; |
Nikolay Borisov | fc4f21b1 | 2017-02-20 13:51:06 +0200 | [diff] [blame] | 7039 | u64 objectid = btrfs_ino(inode); |
Linus Torvalds | 7e74e23 | 2019-05-01 12:19:20 -0700 | [diff] [blame] | 7040 | int extent_type = -1; |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 7041 | struct btrfs_path *path = NULL; |
Nikolay Borisov | fc4f21b1 | 2017-02-20 13:51:06 +0200 | [diff] [blame] | 7042 | struct btrfs_root *root = inode->root; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7043 | struct btrfs_file_extent_item *item; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 7044 | struct extent_buffer *leaf; |
| 7045 | struct btrfs_key found_key; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7046 | struct extent_map *em = NULL; |
Nikolay Borisov | fc4f21b1 | 2017-02-20 13:51:06 +0200 | [diff] [blame] | 7047 | struct extent_map_tree *em_tree = &inode->extent_tree; |
| 7048 | struct extent_io_tree *io_tree = &inode->io_tree; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7049 | |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 7050 | read_lock(&em_tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 7051 | em = lookup_extent_mapping(em_tree, start, len); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 7052 | read_unlock(&em_tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 7053 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7054 | if (em) { |
Chris Mason | e1c4b74 | 2008-04-22 13:26:46 -0400 | [diff] [blame] | 7055 | if (em->start > start || em->start + em->len <= start) |
| 7056 | free_extent_map(em); |
| 7057 | else if (em->block_start == EXTENT_MAP_INLINE && page) |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 7058 | free_extent_map(em); |
| 7059 | else |
| 7060 | goto out; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7061 | } |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 7062 | em = alloc_extent_map(); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7063 | if (!em) { |
Nikolay Borisov | 1028d1c | 2020-08-03 12:58:46 +0300 | [diff] [blame] | 7064 | ret = -ENOMEM; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 7065 | goto out; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7066 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 7067 | em->start = EXTENT_MAP_HOLE; |
Chris Mason | 445a694 | 2008-11-10 11:53:33 -0500 | [diff] [blame] | 7068 | em->orig_start = EXTENT_MAP_HOLE; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 7069 | em->len = (u64)-1; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 7070 | em->block_len = (u64)-1; |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 7071 | |
Liu Bo | bee6ec8 | 2018-08-17 05:05:28 +0800 | [diff] [blame] | 7072 | path = btrfs_alloc_path(); |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 7073 | if (!path) { |
Nikolay Borisov | 1028d1c | 2020-08-03 12:58:46 +0300 | [diff] [blame] | 7074 | ret = -ENOMEM; |
Liu Bo | bee6ec8 | 2018-08-17 05:05:28 +0800 | [diff] [blame] | 7075 | goto out; |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 7076 | } |
| 7077 | |
Liu Bo | bee6ec8 | 2018-08-17 05:05:28 +0800 | [diff] [blame] | 7078 | /* Chances are we'll be called again, so go ahead and do readahead */ |
| 7079 | path->reada = READA_FORWARD; |
Josef Bacik | 4d7240f | 2020-10-23 09:58:09 -0400 | [diff] [blame] | 7080 | |
| 7081 | /* |
| 7082 | * The same explanation in load_free_space_cache applies here as well, |
| 7083 | * we only read when we're loading the free space cache, and at that |
| 7084 | * point the commit_root has everything we need. |
| 7085 | */ |
| 7086 | if (btrfs_is_free_space_inode(inode)) { |
| 7087 | path->search_commit_root = 1; |
| 7088 | path->skip_locking = 1; |
| 7089 | } |
Josef Bacik | 5189941 | 2020-08-20 11:46:01 -0400 | [diff] [blame] | 7090 | |
Nikolay Borisov | 5c9a702 | 2017-12-01 11:19:40 +0200 | [diff] [blame] | 7091 | ret = btrfs_lookup_file_extent(NULL, root, path, objectid, start, 0); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7092 | if (ret < 0) { |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7093 | goto out; |
Nikolay Borisov | b8eeab7 | 2018-12-17 11:49:00 +0200 | [diff] [blame] | 7094 | } else if (ret > 0) { |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7095 | if (path->slots[0] == 0) |
| 7096 | goto not_found; |
| 7097 | path->slots[0]--; |
Nikolay Borisov | 1028d1c | 2020-08-03 12:58:46 +0300 | [diff] [blame] | 7098 | ret = 0; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7099 | } |
| 7100 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 7101 | leaf = path->nodes[0]; |
| 7102 | item = btrfs_item_ptr(leaf, path->slots[0], |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7103 | struct btrfs_file_extent_item); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 7104 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 7105 | if (found_key.objectid != objectid || |
Nikolay Borisov | 694c12e | 2018-12-17 10:35:59 +0200 | [diff] [blame] | 7106 | found_key.type != BTRFS_EXTENT_DATA_KEY) { |
Josef Bacik | 25a5034 | 2013-10-14 12:08:38 -0400 | [diff] [blame] | 7107 | /* |
| 7108 | * If we backup past the first extent we want to move forward |
| 7109 | * and see if there is an extent in front of us, otherwise we'll |
| 7110 | * say there is a hole for our whole search range which can |
| 7111 | * cause problems. |
| 7112 | */ |
| 7113 | extent_end = start; |
| 7114 | goto next; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7115 | } |
| 7116 | |
Nikolay Borisov | 694c12e | 2018-12-17 10:35:59 +0200 | [diff] [blame] | 7117 | extent_type = btrfs_file_extent_type(leaf, item); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 7118 | extent_start = found_key.offset; |
Filipe Manana | a5eeb3d | 2020-03-09 12:41:06 +0000 | [diff] [blame] | 7119 | extent_end = btrfs_file_extent_end(path); |
Nikolay Borisov | 694c12e | 2018-12-17 10:35:59 +0200 | [diff] [blame] | 7120 | if (extent_type == BTRFS_FILE_EXTENT_REG || |
| 7121 | extent_type == BTRFS_FILE_EXTENT_PREALLOC) { |
Qu Wenruo | 6bf9e4b | 2019-03-13 13:55:11 +0800 | [diff] [blame] | 7122 | /* Only regular file could have regular/prealloc extent */ |
| 7123 | if (!S_ISREG(inode->vfs_inode.i_mode)) { |
Nikolay Borisov | 1028d1c | 2020-08-03 12:58:46 +0300 | [diff] [blame] | 7124 | ret = -EUCLEAN; |
Qu Wenruo | 6bf9e4b | 2019-03-13 13:55:11 +0800 | [diff] [blame] | 7125 | btrfs_crit(fs_info, |
| 7126 | "regular/prealloc extent found for non-regular inode %llu", |
| 7127 | btrfs_ino(inode)); |
| 7128 | goto out; |
| 7129 | } |
Liu Bo | 09ed2f1 | 2017-03-10 11:09:48 -0800 | [diff] [blame] | 7130 | trace_btrfs_get_extent_show_fi_regular(inode, leaf, item, |
| 7131 | extent_start); |
Nikolay Borisov | 694c12e | 2018-12-17 10:35:59 +0200 | [diff] [blame] | 7132 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
Liu Bo | 09ed2f1 | 2017-03-10 11:09:48 -0800 | [diff] [blame] | 7133 | trace_btrfs_get_extent_show_fi_inline(inode, leaf, item, |
| 7134 | path->slots[0], |
| 7135 | extent_start); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 7136 | } |
Josef Bacik | 25a5034 | 2013-10-14 12:08:38 -0400 | [diff] [blame] | 7137 | next: |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 7138 | if (start >= extent_end) { |
| 7139 | path->slots[0]++; |
| 7140 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { |
| 7141 | ret = btrfs_next_leaf(root, path); |
Nikolay Borisov | 1028d1c | 2020-08-03 12:58:46 +0300 | [diff] [blame] | 7142 | if (ret < 0) |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 7143 | goto out; |
Nikolay Borisov | 1028d1c | 2020-08-03 12:58:46 +0300 | [diff] [blame] | 7144 | else if (ret > 0) |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 7145 | goto not_found; |
Nikolay Borisov | 1028d1c | 2020-08-03 12:58:46 +0300 | [diff] [blame] | 7146 | |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 7147 | leaf = path->nodes[0]; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7148 | } |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 7149 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 7150 | if (found_key.objectid != objectid || |
| 7151 | found_key.type != BTRFS_EXTENT_DATA_KEY) |
| 7152 | goto not_found; |
| 7153 | if (start + len <= found_key.offset) |
| 7154 | goto not_found; |
Wang Shilong | e2eca69 | 2014-07-17 11:44:14 +0800 | [diff] [blame] | 7155 | if (start > found_key.offset) |
| 7156 | goto next; |
Nikolay Borisov | 02a033d | 2018-12-17 10:36:02 +0200 | [diff] [blame] | 7157 | |
| 7158 | /* New extent overlaps with existing one */ |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 7159 | em->start = start; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 7160 | em->orig_start = start; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 7161 | em->len = found_key.offset - start; |
Nikolay Borisov | 02a033d | 2018-12-17 10:36:02 +0200 | [diff] [blame] | 7162 | em->block_start = EXTENT_MAP_HOLE; |
| 7163 | goto insert; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 7164 | } |
| 7165 | |
Omar Sandoval | 39b07b5 | 2019-12-02 17:34:23 -0800 | [diff] [blame] | 7166 | btrfs_extent_item_to_extent_map(inode, path, item, !page, em); |
Filipe Manana | 7ffbb59 | 2014-06-09 03:48:05 +0100 | [diff] [blame] | 7167 | |
Nikolay Borisov | 694c12e | 2018-12-17 10:35:59 +0200 | [diff] [blame] | 7168 | if (extent_type == BTRFS_FILE_EXTENT_REG || |
| 7169 | extent_type == BTRFS_FILE_EXTENT_PREALLOC) { |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7170 | goto insert; |
Nikolay Borisov | 694c12e | 2018-12-17 10:35:59 +0200 | [diff] [blame] | 7171 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 7172 | unsigned long ptr; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7173 | char *map; |
Chris Mason | 3326d1b | 2007-10-15 16:18:25 -0400 | [diff] [blame] | 7174 | size_t size; |
| 7175 | size_t extent_offset; |
| 7176 | size_t copy_size; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7177 | |
Omar Sandoval | 39b07b5 | 2019-12-02 17:34:23 -0800 | [diff] [blame] | 7178 | if (!page) |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 7179 | goto out; |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 7180 | |
Qu Wenruo | e41ca58 | 2018-06-06 15:41:49 +0800 | [diff] [blame] | 7181 | size = btrfs_file_extent_ram_bytes(leaf, item); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 7182 | extent_offset = page_offset(page) + pg_offset - extent_start; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 7183 | copy_size = min_t(u64, PAGE_SIZE - pg_offset, |
| 7184 | size - extent_offset); |
Chris Mason | 3326d1b | 2007-10-15 16:18:25 -0400 | [diff] [blame] | 7185 | em->start = extent_start + extent_offset; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7186 | em->len = ALIGN(copy_size, fs_info->sectorsize); |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 7187 | em->orig_block_len = em->len; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 7188 | em->orig_start = em->start; |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 7189 | ptr = btrfs_file_extent_inline_start(item) + extent_offset; |
Liu Bo | e49aabd | 2018-08-25 13:47:09 +0800 | [diff] [blame] | 7190 | |
Edmund Nadolski | bf46f52 | 2017-11-20 13:24:49 -0700 | [diff] [blame] | 7191 | if (!PageUptodate(page)) { |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 7192 | if (btrfs_file_extent_compression(leaf, item) != |
| 7193 | BTRFS_COMPRESS_NONE) { |
Byongho Lee | e40da0e | 2015-05-19 23:46:45 +0900 | [diff] [blame] | 7194 | ret = uncompress_inline(path, page, pg_offset, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 7195 | extent_offset, item); |
Nikolay Borisov | 1028d1c | 2020-08-03 12:58:46 +0300 | [diff] [blame] | 7196 | if (ret) |
Zach Brown | 166ae5a | 2014-05-09 17:15:10 -0400 | [diff] [blame] | 7197 | goto out; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 7198 | } else { |
Ira Weiny | 58c1a35 | 2021-02-16 18:48:23 -0800 | [diff] [blame] | 7199 | map = kmap_local_page(page); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 7200 | read_extent_buffer(leaf, map + pg_offset, ptr, |
| 7201 | copy_size); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 7202 | if (pg_offset + copy_size < PAGE_SIZE) { |
Chris Mason | 93c82d5 | 2009-09-11 12:36:29 -0400 | [diff] [blame] | 7203 | memset(map + pg_offset + copy_size, 0, |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 7204 | PAGE_SIZE - pg_offset - |
Chris Mason | 93c82d5 | 2009-09-11 12:36:29 -0400 | [diff] [blame] | 7205 | copy_size); |
| 7206 | } |
Ira Weiny | 58c1a35 | 2021-02-16 18:48:23 -0800 | [diff] [blame] | 7207 | kunmap_local(map); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 7208 | } |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 7209 | flush_dcache_page(page); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7210 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 7211 | set_extent_uptodate(io_tree, em->start, |
Arne Jansen | 507903b | 2011-04-06 10:02:20 +0000 | [diff] [blame] | 7212 | extent_map_end(em) - 1, NULL, GFP_NOFS); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7213 | goto insert; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7214 | } |
| 7215 | not_found: |
| 7216 | em->start = start; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 7217 | em->orig_start = start; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 7218 | em->len = len; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 7219 | em->block_start = EXTENT_MAP_HOLE; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7220 | insert: |
Nikolay Borisov | 1028d1c | 2020-08-03 12:58:46 +0300 | [diff] [blame] | 7221 | ret = 0; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 7222 | btrfs_release_path(path); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 7223 | if (em->start > start || extent_map_end(em) <= start) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7224 | btrfs_err(fs_info, |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 7225 | "bad extent! em: [%llu %llu] passed [%llu %llu]", |
| 7226 | em->start, em->len, start, len); |
Nikolay Borisov | 1028d1c | 2020-08-03 12:58:46 +0300 | [diff] [blame] | 7227 | ret = -EIO; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7228 | goto out; |
| 7229 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 7230 | |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 7231 | write_lock(&em_tree->lock); |
Nikolay Borisov | 1028d1c | 2020-08-03 12:58:46 +0300 | [diff] [blame] | 7232 | ret = btrfs_add_extent_mapping(fs_info, em_tree, &em, start, len); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 7233 | write_unlock(&em_tree->lock); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7234 | out: |
Liu Bo | c641428 | 2018-08-23 07:36:17 +0800 | [diff] [blame] | 7235 | btrfs_free_path(path); |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 7236 | |
Nikolay Borisov | fc4f21b1 | 2017-02-20 13:51:06 +0200 | [diff] [blame] | 7237 | trace_btrfs_get_extent(root, inode, em); |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 7238 | |
Nikolay Borisov | 1028d1c | 2020-08-03 12:58:46 +0300 | [diff] [blame] | 7239 | if (ret) { |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7240 | free_extent_map(em); |
Nikolay Borisov | 1028d1c | 2020-08-03 12:58:46 +0300 | [diff] [blame] | 7241 | return ERR_PTR(ret); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7242 | } |
| 7243 | return em; |
| 7244 | } |
| 7245 | |
Nikolay Borisov | fc4f21b1 | 2017-02-20 13:51:06 +0200 | [diff] [blame] | 7246 | struct extent_map *btrfs_get_extent_fiemap(struct btrfs_inode *inode, |
Nikolay Borisov | 4ab47a8 | 2018-12-12 09:42:32 +0200 | [diff] [blame] | 7247 | u64 start, u64 len) |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 7248 | { |
| 7249 | struct extent_map *em; |
| 7250 | struct extent_map *hole_em = NULL; |
Nikolay Borisov | f3714ef | 2018-12-12 09:42:33 +0200 | [diff] [blame] | 7251 | u64 delalloc_start = start; |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 7252 | u64 end; |
Nikolay Borisov | f3714ef | 2018-12-12 09:42:33 +0200 | [diff] [blame] | 7253 | u64 delalloc_len; |
| 7254 | u64 delalloc_end; |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 7255 | int err = 0; |
| 7256 | |
Omar Sandoval | 39b07b5 | 2019-12-02 17:34:23 -0800 | [diff] [blame] | 7257 | em = btrfs_get_extent(inode, NULL, 0, start, len); |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 7258 | if (IS_ERR(em)) |
| 7259 | return em; |
Dan Carpenter | 9986277 | 2017-04-11 11:57:15 +0300 | [diff] [blame] | 7260 | /* |
| 7261 | * If our em maps to: |
| 7262 | * - a hole or |
| 7263 | * - a pre-alloc extent, |
| 7264 | * there might actually be delalloc bytes behind it. |
| 7265 | */ |
| 7266 | if (em->block_start != EXTENT_MAP_HOLE && |
| 7267 | !test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) |
| 7268 | return em; |
| 7269 | else |
| 7270 | hole_em = em; |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 7271 | |
| 7272 | /* check to see if we've wrapped (len == -1 or similar) */ |
| 7273 | end = start + len; |
| 7274 | if (end < start) |
| 7275 | end = (u64)-1; |
| 7276 | else |
| 7277 | end -= 1; |
| 7278 | |
| 7279 | em = NULL; |
| 7280 | |
| 7281 | /* ok, we didn't find anything, lets look for delalloc */ |
Nikolay Borisov | f3714ef | 2018-12-12 09:42:33 +0200 | [diff] [blame] | 7282 | delalloc_len = count_range_bits(&inode->io_tree, &delalloc_start, |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 7283 | end, len, EXTENT_DELALLOC, 1); |
Nikolay Borisov | f3714ef | 2018-12-12 09:42:33 +0200 | [diff] [blame] | 7284 | delalloc_end = delalloc_start + delalloc_len; |
| 7285 | if (delalloc_end < delalloc_start) |
| 7286 | delalloc_end = (u64)-1; |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 7287 | |
| 7288 | /* |
Nikolay Borisov | f3714ef | 2018-12-12 09:42:33 +0200 | [diff] [blame] | 7289 | * We didn't find anything useful, return the original results from |
| 7290 | * get_extent() |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 7291 | */ |
Nikolay Borisov | f3714ef | 2018-12-12 09:42:33 +0200 | [diff] [blame] | 7292 | if (delalloc_start > end || delalloc_end <= start) { |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 7293 | em = hole_em; |
| 7294 | hole_em = NULL; |
| 7295 | goto out; |
| 7296 | } |
| 7297 | |
Nikolay Borisov | f3714ef | 2018-12-12 09:42:33 +0200 | [diff] [blame] | 7298 | /* |
| 7299 | * Adjust the delalloc_start to make sure it doesn't go backwards from |
| 7300 | * the start they passed in |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 7301 | */ |
Nikolay Borisov | f3714ef | 2018-12-12 09:42:33 +0200 | [diff] [blame] | 7302 | delalloc_start = max(start, delalloc_start); |
| 7303 | delalloc_len = delalloc_end - delalloc_start; |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 7304 | |
Nikolay Borisov | f3714ef | 2018-12-12 09:42:33 +0200 | [diff] [blame] | 7305 | if (delalloc_len > 0) { |
| 7306 | u64 hole_start; |
Nikolay Borisov | 02950af | 2018-12-12 09:42:34 +0200 | [diff] [blame] | 7307 | u64 hole_len; |
Nikolay Borisov | f3714ef | 2018-12-12 09:42:33 +0200 | [diff] [blame] | 7308 | const u64 hole_end = extent_map_end(hole_em); |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 7309 | |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 7310 | em = alloc_extent_map(); |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 7311 | if (!em) { |
| 7312 | err = -ENOMEM; |
| 7313 | goto out; |
| 7314 | } |
Nikolay Borisov | f3714ef | 2018-12-12 09:42:33 +0200 | [diff] [blame] | 7315 | |
| 7316 | ASSERT(hole_em); |
| 7317 | /* |
| 7318 | * When btrfs_get_extent can't find anything it returns one |
| 7319 | * huge hole |
| 7320 | * |
| 7321 | * Make sure what it found really fits our range, and adjust to |
| 7322 | * make sure it is based on the start from the caller |
| 7323 | */ |
| 7324 | if (hole_end <= start || hole_em->start > end) { |
| 7325 | free_extent_map(hole_em); |
| 7326 | hole_em = NULL; |
| 7327 | } else { |
| 7328 | hole_start = max(hole_em->start, start); |
| 7329 | hole_len = hole_end - hole_start; |
| 7330 | } |
| 7331 | |
| 7332 | if (hole_em && delalloc_start > hole_start) { |
| 7333 | /* |
| 7334 | * Our hole starts before our delalloc, so we have to |
| 7335 | * return just the parts of the hole that go until the |
| 7336 | * delalloc starts |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 7337 | */ |
Nikolay Borisov | f3714ef | 2018-12-12 09:42:33 +0200 | [diff] [blame] | 7338 | em->len = min(hole_len, delalloc_start - hole_start); |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 7339 | em->start = hole_start; |
| 7340 | em->orig_start = hole_start; |
| 7341 | /* |
Nikolay Borisov | f3714ef | 2018-12-12 09:42:33 +0200 | [diff] [blame] | 7342 | * Don't adjust block start at all, it is fixed at |
| 7343 | * EXTENT_MAP_HOLE |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 7344 | */ |
| 7345 | em->block_start = hole_em->block_start; |
| 7346 | em->block_len = hole_len; |
Liu Bo | f9e4fb5 | 2013-01-07 10:10:12 +0000 | [diff] [blame] | 7347 | if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags)) |
| 7348 | set_bit(EXTENT_FLAG_PREALLOC, &em->flags); |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 7349 | } else { |
Nikolay Borisov | f3714ef | 2018-12-12 09:42:33 +0200 | [diff] [blame] | 7350 | /* |
| 7351 | * Hole is out of passed range or it starts after |
| 7352 | * delalloc range |
| 7353 | */ |
| 7354 | em->start = delalloc_start; |
| 7355 | em->len = delalloc_len; |
| 7356 | em->orig_start = delalloc_start; |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 7357 | em->block_start = EXTENT_MAP_DELALLOC; |
Nikolay Borisov | f3714ef | 2018-12-12 09:42:33 +0200 | [diff] [blame] | 7358 | em->block_len = delalloc_len; |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 7359 | } |
Nikolay Borisov | bf8d32b | 2017-12-01 11:19:43 +0200 | [diff] [blame] | 7360 | } else { |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 7361 | return hole_em; |
| 7362 | } |
| 7363 | out: |
| 7364 | |
| 7365 | free_extent_map(hole_em); |
| 7366 | if (err) { |
| 7367 | free_extent_map(em); |
| 7368 | return ERR_PTR(err); |
| 7369 | } |
| 7370 | return em; |
| 7371 | } |
| 7372 | |
Nikolay Borisov | 64f5418 | 2020-06-03 08:55:31 +0300 | [diff] [blame] | 7373 | static struct extent_map *btrfs_create_dio_extent(struct btrfs_inode *inode, |
Filipe Manana | 5f9a8a5 | 2016-05-12 13:53:36 +0100 | [diff] [blame] | 7374 | const u64 start, |
| 7375 | const u64 len, |
| 7376 | const u64 orig_start, |
| 7377 | const u64 block_start, |
| 7378 | const u64 block_len, |
| 7379 | const u64 orig_block_len, |
| 7380 | const u64 ram_bytes, |
| 7381 | const int type) |
| 7382 | { |
| 7383 | struct extent_map *em = NULL; |
| 7384 | int ret; |
| 7385 | |
Filipe Manana | 5f9a8a5 | 2016-05-12 13:53:36 +0100 | [diff] [blame] | 7386 | if (type != BTRFS_ORDERED_NOCOW) { |
Nikolay Borisov | 64f5418 | 2020-06-03 08:55:31 +0300 | [diff] [blame] | 7387 | em = create_io_em(inode, start, len, orig_start, block_start, |
| 7388 | block_len, orig_block_len, ram_bytes, |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 7389 | BTRFS_COMPRESS_NONE, /* compress_type */ |
| 7390 | type); |
Filipe Manana | 5f9a8a5 | 2016-05-12 13:53:36 +0100 | [diff] [blame] | 7391 | if (IS_ERR(em)) |
| 7392 | goto out; |
| 7393 | } |
Nikolay Borisov | 64f5418 | 2020-06-03 08:55:31 +0300 | [diff] [blame] | 7394 | ret = btrfs_add_ordered_extent_dio(inode, start, block_start, len, |
| 7395 | block_len, type); |
Filipe Manana | 5f9a8a5 | 2016-05-12 13:53:36 +0100 | [diff] [blame] | 7396 | if (ret) { |
| 7397 | if (em) { |
| 7398 | free_extent_map(em); |
Nikolay Borisov | 64f5418 | 2020-06-03 08:55:31 +0300 | [diff] [blame] | 7399 | btrfs_drop_extent_cache(inode, start, start + len - 1, 0); |
Filipe Manana | 5f9a8a5 | 2016-05-12 13:53:36 +0100 | [diff] [blame] | 7400 | } |
| 7401 | em = ERR_PTR(ret); |
| 7402 | } |
| 7403 | out: |
Filipe Manana | 5f9a8a5 | 2016-05-12 13:53:36 +0100 | [diff] [blame] | 7404 | |
| 7405 | return em; |
| 7406 | } |
| 7407 | |
Nikolay Borisov | 9fc6f91 | 2020-06-03 08:55:32 +0300 | [diff] [blame] | 7408 | static struct extent_map *btrfs_new_extent_direct(struct btrfs_inode *inode, |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7409 | u64 start, u64 len) |
| 7410 | { |
Nikolay Borisov | 9fc6f91 | 2020-06-03 08:55:32 +0300 | [diff] [blame] | 7411 | struct btrfs_root *root = inode->root; |
| 7412 | struct btrfs_fs_info *fs_info = root->fs_info; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 7413 | struct extent_map *em; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7414 | struct btrfs_key ins; |
| 7415 | u64 alloc_hint; |
| 7416 | int ret; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7417 | |
Nikolay Borisov | 9fc6f91 | 2020-06-03 08:55:32 +0300 | [diff] [blame] | 7418 | alloc_hint = get_extent_allocation_hint(inode, start, len); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7419 | ret = btrfs_reserve_extent(root, len, len, fs_info->sectorsize, |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 7420 | 0, alloc_hint, &ins, 1, 1); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 7421 | if (ret) |
| 7422 | return ERR_PTR(ret); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7423 | |
Nikolay Borisov | 9fc6f91 | 2020-06-03 08:55:32 +0300 | [diff] [blame] | 7424 | em = btrfs_create_dio_extent(inode, start, ins.offset, start, |
Filipe Manana | 5f9a8a5 | 2016-05-12 13:53:36 +0100 | [diff] [blame] | 7425 | ins.objectid, ins.offset, ins.offset, |
Liu Bo | 6288d6e | 2017-02-21 12:12:58 -0800 | [diff] [blame] | 7426 | ins.offset, BTRFS_ORDERED_REGULAR); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7427 | btrfs_dec_block_group_reservations(fs_info, ins.objectid); |
Filipe Manana | 5f9a8a5 | 2016-05-12 13:53:36 +0100 | [diff] [blame] | 7428 | if (IS_ERR(em)) |
Nikolay Borisov | 9fc6f91 | 2020-06-03 08:55:32 +0300 | [diff] [blame] | 7429 | btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, |
| 7430 | 1); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 7431 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7432 | return em; |
| 7433 | } |
| 7434 | |
Anand Jain | f463963 | 2021-02-10 21:25:16 -0800 | [diff] [blame] | 7435 | static bool btrfs_extent_readonly(struct btrfs_fs_info *fs_info, u64 bytenr) |
Anand Jain | 05947ae | 2021-02-10 21:25:15 -0800 | [diff] [blame] | 7436 | { |
| 7437 | struct btrfs_block_group *block_group; |
Anand Jain | f463963 | 2021-02-10 21:25:16 -0800 | [diff] [blame] | 7438 | bool readonly = false; |
Anand Jain | 05947ae | 2021-02-10 21:25:15 -0800 | [diff] [blame] | 7439 | |
| 7440 | block_group = btrfs_lookup_block_group(fs_info, bytenr); |
| 7441 | if (!block_group || block_group->ro) |
Anand Jain | f463963 | 2021-02-10 21:25:16 -0800 | [diff] [blame] | 7442 | readonly = true; |
Anand Jain | 05947ae | 2021-02-10 21:25:15 -0800 | [diff] [blame] | 7443 | if (block_group) |
| 7444 | btrfs_put_block_group(block_group); |
| 7445 | return readonly; |
| 7446 | } |
| 7447 | |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7448 | /* |
Qu Wenruo | e4ecaf9 | 2020-06-24 07:23:51 +0800 | [diff] [blame] | 7449 | * Check if we can do nocow write into the range [@offset, @offset + @len) |
| 7450 | * |
| 7451 | * @offset: File offset |
| 7452 | * @len: The length to write, will be updated to the nocow writeable |
| 7453 | * range |
| 7454 | * @orig_start: (optional) Return the original file offset of the file extent |
| 7455 | * @orig_len: (optional) Return the original on-disk length of the file extent |
| 7456 | * @ram_bytes: (optional) Return the ram_bytes of the file extent |
Boris Burkov | a84d5d4 | 2020-08-18 11:00:05 -0700 | [diff] [blame] | 7457 | * @strict: if true, omit optimizations that might force us into unnecessary |
| 7458 | * cow. e.g., don't trust generation number. |
Qu Wenruo | e4ecaf9 | 2020-06-24 07:23:51 +0800 | [diff] [blame] | 7459 | * |
Qu Wenruo | e4ecaf9 | 2020-06-24 07:23:51 +0800 | [diff] [blame] | 7460 | * Return: |
| 7461 | * >0 and update @len if we can do nocow write |
| 7462 | * 0 if we can't do nocow write |
| 7463 | * <0 if error happened |
| 7464 | * |
| 7465 | * NOTE: This only checks the file extents, caller is responsible to wait for |
| 7466 | * any ordered extents. |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7467 | */ |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 7468 | noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len, |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 7469 | u64 *orig_start, u64 *orig_block_len, |
Boris Burkov | a84d5d4 | 2020-08-18 11:00:05 -0700 | [diff] [blame] | 7470 | u64 *ram_bytes, bool strict) |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7471 | { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 7472 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7473 | struct btrfs_path *path; |
| 7474 | int ret; |
| 7475 | struct extent_buffer *leaf; |
| 7476 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Miao Xie | 7b2b708 | 2014-02-27 13:58:05 +0800 | [diff] [blame] | 7477 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7478 | struct btrfs_file_extent_item *fi; |
| 7479 | struct btrfs_key key; |
| 7480 | u64 disk_bytenr; |
| 7481 | u64 backref_offset; |
| 7482 | u64 extent_end; |
| 7483 | u64 num_bytes; |
| 7484 | int slot; |
| 7485 | int found_type; |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 7486 | bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW); |
Miao Xie | e77751a | 2013-12-27 21:11:50 +0800 | [diff] [blame] | 7487 | |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7488 | path = btrfs_alloc_path(); |
| 7489 | if (!path) |
| 7490 | return -ENOMEM; |
| 7491 | |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 7492 | ret = btrfs_lookup_file_extent(NULL, root, path, |
| 7493 | btrfs_ino(BTRFS_I(inode)), offset, 0); |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7494 | if (ret < 0) |
| 7495 | goto out; |
| 7496 | |
| 7497 | slot = path->slots[0]; |
| 7498 | if (ret == 1) { |
| 7499 | if (slot == 0) { |
| 7500 | /* can't find the item, must cow */ |
| 7501 | ret = 0; |
| 7502 | goto out; |
| 7503 | } |
| 7504 | slot--; |
| 7505 | } |
| 7506 | ret = 0; |
| 7507 | leaf = path->nodes[0]; |
| 7508 | btrfs_item_key_to_cpu(leaf, &key, slot); |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 7509 | if (key.objectid != btrfs_ino(BTRFS_I(inode)) || |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7510 | key.type != BTRFS_EXTENT_DATA_KEY) { |
| 7511 | /* not our file or wrong item type, must cow */ |
| 7512 | goto out; |
| 7513 | } |
| 7514 | |
| 7515 | if (key.offset > offset) { |
| 7516 | /* Wrong offset, must cow */ |
| 7517 | goto out; |
| 7518 | } |
| 7519 | |
| 7520 | fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item); |
| 7521 | found_type = btrfs_file_extent_type(leaf, fi); |
| 7522 | if (found_type != BTRFS_FILE_EXTENT_REG && |
| 7523 | found_type != BTRFS_FILE_EXTENT_PREALLOC) { |
| 7524 | /* not a regular extent, must cow */ |
| 7525 | goto out; |
| 7526 | } |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 7527 | |
| 7528 | if (!nocow && found_type == BTRFS_FILE_EXTENT_REG) |
| 7529 | goto out; |
| 7530 | |
Miao Xie | e77751a | 2013-12-27 21:11:50 +0800 | [diff] [blame] | 7531 | extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi); |
| 7532 | if (extent_end <= offset) |
| 7533 | goto out; |
| 7534 | |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7535 | disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 7536 | if (disk_bytenr == 0) |
| 7537 | goto out; |
| 7538 | |
| 7539 | if (btrfs_file_extent_compression(leaf, fi) || |
| 7540 | btrfs_file_extent_encryption(leaf, fi) || |
| 7541 | btrfs_file_extent_other_encoding(leaf, fi)) |
| 7542 | goto out; |
| 7543 | |
Ethan Lien | 78d4295 | 2018-05-17 14:58:29 +0800 | [diff] [blame] | 7544 | /* |
| 7545 | * Do the same check as in btrfs_cross_ref_exist but without the |
| 7546 | * unnecessary search. |
| 7547 | */ |
Boris Burkov | a84d5d4 | 2020-08-18 11:00:05 -0700 | [diff] [blame] | 7548 | if (!strict && |
| 7549 | (btrfs_file_extent_generation(leaf, fi) <= |
| 7550 | btrfs_root_last_snapshot(&root->root_item))) |
Ethan Lien | 78d4295 | 2018-05-17 14:58:29 +0800 | [diff] [blame] | 7551 | goto out; |
| 7552 | |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7553 | backref_offset = btrfs_file_extent_offset(leaf, fi); |
| 7554 | |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 7555 | if (orig_start) { |
| 7556 | *orig_start = key.offset - backref_offset; |
| 7557 | *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi); |
| 7558 | *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi); |
| 7559 | } |
Josef Bacik | eb384b5 | 2013-04-24 16:32:55 -0400 | [diff] [blame] | 7560 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 7561 | if (btrfs_extent_readonly(fs_info, disk_bytenr)) |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7562 | goto out; |
Miao Xie | 7b2b708 | 2014-02-27 13:58:05 +0800 | [diff] [blame] | 7563 | |
| 7564 | num_bytes = min(offset + *len, extent_end) - offset; |
| 7565 | if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) { |
| 7566 | u64 range_end; |
| 7567 | |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 7568 | range_end = round_up(offset + num_bytes, |
| 7569 | root->fs_info->sectorsize) - 1; |
Miao Xie | 7b2b708 | 2014-02-27 13:58:05 +0800 | [diff] [blame] | 7570 | ret = test_range_bit(io_tree, offset, range_end, |
| 7571 | EXTENT_DELALLOC, 0, NULL); |
| 7572 | if (ret) { |
| 7573 | ret = -EAGAIN; |
| 7574 | goto out; |
| 7575 | } |
| 7576 | } |
| 7577 | |
Josef Bacik | 1bda19e | 2013-10-18 12:10:36 -0400 | [diff] [blame] | 7578 | btrfs_release_path(path); |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7579 | |
| 7580 | /* |
| 7581 | * look for other files referencing this extent, if we |
| 7582 | * find any we must cow |
| 7583 | */ |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 7584 | |
Liu Bo | e4c3b2d | 2017-01-30 12:25:28 -0800 | [diff] [blame] | 7585 | ret = btrfs_cross_ref_exist(root, btrfs_ino(BTRFS_I(inode)), |
Boris Burkov | a84d5d4 | 2020-08-18 11:00:05 -0700 | [diff] [blame] | 7586 | key.offset - backref_offset, disk_bytenr, |
| 7587 | strict); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 7588 | if (ret) { |
| 7589 | ret = 0; |
| 7590 | goto out; |
| 7591 | } |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7592 | |
| 7593 | /* |
| 7594 | * adjust disk_bytenr and num_bytes to cover just the bytes |
| 7595 | * in this extent we are about to write. If there |
| 7596 | * are any csums in that range we have to cow in order |
| 7597 | * to keep the csums correct |
| 7598 | */ |
| 7599 | disk_bytenr += backref_offset; |
| 7600 | disk_bytenr += offset - key.offset; |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 7601 | if (csum_exist_in_range(fs_info, disk_bytenr, num_bytes)) |
| 7602 | goto out; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7603 | /* |
| 7604 | * all of the above have passed, it is safe to overwrite this extent |
| 7605 | * without cow |
| 7606 | */ |
Josef Bacik | eb384b5 | 2013-04-24 16:32:55 -0400 | [diff] [blame] | 7607 | *len = num_bytes; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7608 | ret = 1; |
| 7609 | out: |
| 7610 | btrfs_free_path(path); |
| 7611 | return ret; |
| 7612 | } |
| 7613 | |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7614 | static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend, |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 7615 | struct extent_state **cached_state, bool writing) |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7616 | { |
| 7617 | struct btrfs_ordered_extent *ordered; |
| 7618 | int ret = 0; |
| 7619 | |
| 7620 | while (1) { |
| 7621 | lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend, |
David Sterba | ff13db4 | 2015-12-03 14:30:40 +0100 | [diff] [blame] | 7622 | cached_state); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7623 | /* |
| 7624 | * We're concerned with the entire range that we're going to be |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 7625 | * doing DIO to, so we need to make sure there's no ordered |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7626 | * extents in this range. |
| 7627 | */ |
Nikolay Borisov | a776c6f | 2017-02-20 13:50:49 +0200 | [diff] [blame] | 7628 | ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), lockstart, |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7629 | lockend - lockstart + 1); |
| 7630 | |
| 7631 | /* |
| 7632 | * We need to make sure there are no buffered pages in this |
| 7633 | * range either, we could have raced between the invalidate in |
| 7634 | * generic_file_direct_write and locking the extent. The |
| 7635 | * invalidate needs to happen so that reads after a write do not |
| 7636 | * get stale data. |
| 7637 | */ |
Alex Gartrell | fc4adbff | 2014-05-20 13:07:56 -0700 | [diff] [blame] | 7638 | if (!ordered && |
David Sterba | 051c98e | 2018-03-07 15:33:22 +0100 | [diff] [blame] | 7639 | (!writing || !filemap_range_has_page(inode->i_mapping, |
| 7640 | lockstart, lockend))) |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7641 | break; |
| 7642 | |
| 7643 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend, |
David Sterba | e43bbe5 | 2017-12-12 21:43:52 +0100 | [diff] [blame] | 7644 | cached_state); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7645 | |
| 7646 | if (ordered) { |
Filipe Manana | ade7702 | 2016-02-18 14:28:55 +0000 | [diff] [blame] | 7647 | /* |
| 7648 | * If we are doing a DIO read and the ordered extent we |
| 7649 | * found is for a buffered write, we can not wait for it |
| 7650 | * to complete and retry, because if we do so we can |
| 7651 | * deadlock with concurrent buffered writes on page |
| 7652 | * locks. This happens only if our DIO read covers more |
| 7653 | * than one extent map, if at this point has already |
| 7654 | * created an ordered extent for a previous extent map |
| 7655 | * and locked its range in the inode's io tree, and a |
| 7656 | * concurrent write against that previous extent map's |
| 7657 | * range and this range started (we unlock the ranges |
| 7658 | * in the io tree only when the bios complete and |
| 7659 | * buffered writes always lock pages before attempting |
| 7660 | * to lock range in the io tree). |
| 7661 | */ |
| 7662 | if (writing || |
| 7663 | test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags)) |
Nikolay Borisov | c0a4360 | 2020-09-18 12:15:53 +0300 | [diff] [blame] | 7664 | btrfs_start_ordered_extent(ordered, 1); |
Filipe Manana | ade7702 | 2016-02-18 14:28:55 +0000 | [diff] [blame] | 7665 | else |
| 7666 | ret = -ENOTBLK; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7667 | btrfs_put_ordered_extent(ordered); |
| 7668 | } else { |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7669 | /* |
Filipe Manana | b850ae1 | 2015-12-08 16:23:16 +0000 | [diff] [blame] | 7670 | * We could trigger writeback for this range (and wait |
| 7671 | * for it to complete) and then invalidate the pages for |
| 7672 | * this range (through invalidate_inode_pages2_range()), |
| 7673 | * but that can lead us to a deadlock with a concurrent |
Matthew Wilcox (Oracle) | ba206a0 | 2020-06-01 21:47:05 -0700 | [diff] [blame] | 7674 | * call to readahead (a buffered read or a defrag call |
Filipe Manana | b850ae1 | 2015-12-08 16:23:16 +0000 | [diff] [blame] | 7675 | * triggered a readahead) on a page lock due to an |
| 7676 | * ordered dio extent we created before but did not have |
| 7677 | * yet a corresponding bio submitted (whence it can not |
Matthew Wilcox (Oracle) | ba206a0 | 2020-06-01 21:47:05 -0700 | [diff] [blame] | 7678 | * complete), which makes readahead wait for that |
Filipe Manana | b850ae1 | 2015-12-08 16:23:16 +0000 | [diff] [blame] | 7679 | * ordered extent to complete while holding a lock on |
| 7680 | * that page. |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7681 | */ |
Filipe Manana | b850ae1 | 2015-12-08 16:23:16 +0000 | [diff] [blame] | 7682 | ret = -ENOTBLK; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7683 | } |
| 7684 | |
Filipe Manana | ade7702 | 2016-02-18 14:28:55 +0000 | [diff] [blame] | 7685 | if (ret) |
| 7686 | break; |
| 7687 | |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7688 | cond_resched(); |
| 7689 | } |
| 7690 | |
| 7691 | return ret; |
| 7692 | } |
| 7693 | |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 7694 | /* The callers of this must take lock_extent() */ |
Nikolay Borisov | 4b67c11 | 2020-06-03 08:55:05 +0300 | [diff] [blame] | 7695 | static struct extent_map *create_io_em(struct btrfs_inode *inode, u64 start, |
| 7696 | u64 len, u64 orig_start, u64 block_start, |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 7697 | u64 block_len, u64 orig_block_len, |
| 7698 | u64 ram_bytes, int compress_type, |
| 7699 | int type) |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7700 | { |
| 7701 | struct extent_map_tree *em_tree; |
| 7702 | struct extent_map *em; |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7703 | int ret; |
| 7704 | |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 7705 | ASSERT(type == BTRFS_ORDERED_PREALLOC || |
| 7706 | type == BTRFS_ORDERED_COMPRESSED || |
| 7707 | type == BTRFS_ORDERED_NOCOW || |
Liu Bo | 1af4a0a | 2017-02-13 15:35:09 -0800 | [diff] [blame] | 7708 | type == BTRFS_ORDERED_REGULAR); |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 7709 | |
Nikolay Borisov | 4b67c11 | 2020-06-03 08:55:05 +0300 | [diff] [blame] | 7710 | em_tree = &inode->extent_tree; |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7711 | em = alloc_extent_map(); |
| 7712 | if (!em) |
| 7713 | return ERR_PTR(-ENOMEM); |
| 7714 | |
| 7715 | em->start = start; |
| 7716 | em->orig_start = orig_start; |
| 7717 | em->len = len; |
| 7718 | em->block_len = block_len; |
| 7719 | em->block_start = block_start; |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 7720 | em->orig_block_len = orig_block_len; |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 7721 | em->ram_bytes = ram_bytes; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 7722 | em->generation = -1; |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7723 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
Liu Bo | 1af4a0a | 2017-02-13 15:35:09 -0800 | [diff] [blame] | 7724 | if (type == BTRFS_ORDERED_PREALLOC) { |
Josef Bacik | b11e234 | 2012-12-03 10:58:15 -0500 | [diff] [blame] | 7725 | set_bit(EXTENT_FLAG_FILLING, &em->flags); |
Liu Bo | 1af4a0a | 2017-02-13 15:35:09 -0800 | [diff] [blame] | 7726 | } else if (type == BTRFS_ORDERED_COMPRESSED) { |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 7727 | set_bit(EXTENT_FLAG_COMPRESSED, &em->flags); |
| 7728 | em->compress_type = compress_type; |
| 7729 | } |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7730 | |
| 7731 | do { |
Nikolay Borisov | 4b67c11 | 2020-06-03 08:55:05 +0300 | [diff] [blame] | 7732 | btrfs_drop_extent_cache(inode, em->start, |
| 7733 | em->start + em->len - 1, 0); |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7734 | write_lock(&em_tree->lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 7735 | ret = add_extent_mapping(em_tree, em, 1); |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7736 | write_unlock(&em_tree->lock); |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 7737 | /* |
| 7738 | * The caller has taken lock_extent(), who could race with us |
| 7739 | * to add em? |
| 7740 | */ |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7741 | } while (ret == -EEXIST); |
| 7742 | |
| 7743 | if (ret) { |
| 7744 | free_extent_map(em); |
| 7745 | return ERR_PTR(ret); |
| 7746 | } |
| 7747 | |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 7748 | /* em got 2 refs now, callers needs to do free_extent_map once. */ |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7749 | return em; |
| 7750 | } |
| 7751 | |
Nikolay Borisov | 1c8d017 | 2018-05-02 15:19:32 +0300 | [diff] [blame] | 7752 | |
Nikolay Borisov | c5794e5 | 2018-05-02 15:19:33 +0300 | [diff] [blame] | 7753 | static int btrfs_get_blocks_direct_write(struct extent_map **map, |
Nikolay Borisov | c5794e5 | 2018-05-02 15:19:33 +0300 | [diff] [blame] | 7754 | struct inode *inode, |
| 7755 | struct btrfs_dio_data *dio_data, |
| 7756 | u64 start, u64 len) |
| 7757 | { |
| 7758 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
| 7759 | struct extent_map *em = *map; |
| 7760 | int ret = 0; |
| 7761 | |
| 7762 | /* |
| 7763 | * We don't allocate a new extent in the following cases |
| 7764 | * |
| 7765 | * 1) The inode is marked as NODATACOW. In this case we'll just use the |
| 7766 | * existing extent. |
| 7767 | * 2) The extent is marked as PREALLOC. We're good to go here and can |
| 7768 | * just use the extent. |
| 7769 | * |
| 7770 | */ |
| 7771 | if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) || |
| 7772 | ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) && |
| 7773 | em->block_start != EXTENT_MAP_HOLE)) { |
| 7774 | int type; |
| 7775 | u64 block_start, orig_start, orig_block_len, ram_bytes; |
| 7776 | |
| 7777 | if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) |
| 7778 | type = BTRFS_ORDERED_PREALLOC; |
| 7779 | else |
| 7780 | type = BTRFS_ORDERED_NOCOW; |
| 7781 | len = min(len, em->len - (start - em->start)); |
| 7782 | block_start = em->block_start + (start - em->start); |
| 7783 | |
| 7784 | if (can_nocow_extent(inode, start, &len, &orig_start, |
Boris Burkov | a84d5d4 | 2020-08-18 11:00:05 -0700 | [diff] [blame] | 7785 | &orig_block_len, &ram_bytes, false) == 1 && |
Nikolay Borisov | c5794e5 | 2018-05-02 15:19:33 +0300 | [diff] [blame] | 7786 | btrfs_inc_nocow_writers(fs_info, block_start)) { |
| 7787 | struct extent_map *em2; |
| 7788 | |
Nikolay Borisov | 64f5418 | 2020-06-03 08:55:31 +0300 | [diff] [blame] | 7789 | em2 = btrfs_create_dio_extent(BTRFS_I(inode), start, len, |
Nikolay Borisov | c5794e5 | 2018-05-02 15:19:33 +0300 | [diff] [blame] | 7790 | orig_start, block_start, |
| 7791 | len, orig_block_len, |
| 7792 | ram_bytes, type); |
| 7793 | btrfs_dec_nocow_writers(fs_info, block_start); |
| 7794 | if (type == BTRFS_ORDERED_PREALLOC) { |
| 7795 | free_extent_map(em); |
| 7796 | *map = em = em2; |
| 7797 | } |
| 7798 | |
| 7799 | if (em2 && IS_ERR(em2)) { |
| 7800 | ret = PTR_ERR(em2); |
| 7801 | goto out; |
| 7802 | } |
| 7803 | /* |
| 7804 | * For inode marked NODATACOW or extent marked PREALLOC, |
| 7805 | * use the existing or preallocated extent, so does not |
| 7806 | * need to adjust btrfs_space_info's bytes_may_use. |
| 7807 | */ |
Nikolay Borisov | 9db5d51 | 2020-06-03 08:55:38 +0300 | [diff] [blame] | 7808 | btrfs_free_reserved_data_space_noquota(fs_info, len); |
Nikolay Borisov | c5794e5 | 2018-05-02 15:19:33 +0300 | [diff] [blame] | 7809 | goto skip_cow; |
| 7810 | } |
| 7811 | } |
| 7812 | |
| 7813 | /* this will cow the extent */ |
Nikolay Borisov | c5794e5 | 2018-05-02 15:19:33 +0300 | [diff] [blame] | 7814 | free_extent_map(em); |
Nikolay Borisov | 9fc6f91 | 2020-06-03 08:55:32 +0300 | [diff] [blame] | 7815 | *map = em = btrfs_new_extent_direct(BTRFS_I(inode), start, len); |
Nikolay Borisov | c5794e5 | 2018-05-02 15:19:33 +0300 | [diff] [blame] | 7816 | if (IS_ERR(em)) { |
| 7817 | ret = PTR_ERR(em); |
| 7818 | goto out; |
| 7819 | } |
| 7820 | |
| 7821 | len = min(len, em->len - (start - em->start)); |
| 7822 | |
| 7823 | skip_cow: |
Nikolay Borisov | c5794e5 | 2018-05-02 15:19:33 +0300 | [diff] [blame] | 7824 | /* |
| 7825 | * Need to update the i_size under the extent lock so buffered |
| 7826 | * readers will get the updated i_size when we unlock. |
| 7827 | */ |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 7828 | if (start + len > i_size_read(inode)) |
Nikolay Borisov | c5794e5 | 2018-05-02 15:19:33 +0300 | [diff] [blame] | 7829 | i_size_write(inode, start + len); |
| 7830 | |
Nikolay Borisov | c5794e5 | 2018-05-02 15:19:33 +0300 | [diff] [blame] | 7831 | dio_data->reserve -= len; |
Nikolay Borisov | c5794e5 | 2018-05-02 15:19:33 +0300 | [diff] [blame] | 7832 | out: |
| 7833 | return ret; |
| 7834 | } |
| 7835 | |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 7836 | static int btrfs_dio_iomap_begin(struct inode *inode, loff_t start, |
| 7837 | loff_t length, unsigned int flags, struct iomap *iomap, |
| 7838 | struct iomap *srcmap) |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7839 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7840 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7841 | struct extent_map *em; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7842 | struct extent_state *cached_state = NULL; |
chandan | 50745b0 | 2015-08-28 21:10:13 +0530 | [diff] [blame] | 7843 | struct btrfs_dio_data *dio_data = NULL; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7844 | u64 lockstart, lockend; |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 7845 | const bool write = !!(flags & IOMAP_WRITE); |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 7846 | int ret = 0; |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 7847 | u64 len = length; |
| 7848 | bool unlock_extents = false; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7849 | |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 7850 | if (!write) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7851 | len = min_t(u64, len, fs_info->sectorsize); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7852 | |
Josef Bacik | c329861 | 2012-08-03 16:49:19 -0400 | [diff] [blame] | 7853 | lockstart = start; |
| 7854 | lockend = start + len - 1; |
| 7855 | |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 7856 | /* |
| 7857 | * The generic stuff only does filemap_write_and_wait_range, which |
| 7858 | * isn't enough if we've written compressed pages to this area, so we |
| 7859 | * need to flush the dirty pages again to make absolutely sure that any |
| 7860 | * outstanding dirty pages are on disk. |
| 7861 | */ |
| 7862 | if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, |
| 7863 | &BTRFS_I(inode)->runtime_flags)) { |
| 7864 | ret = filemap_fdatawrite_range(inode->i_mapping, start, |
| 7865 | start + length - 1); |
| 7866 | if (ret) |
| 7867 | return ret; |
Josef Bacik | e1cbbfa | 2015-03-17 10:52:28 -0400 | [diff] [blame] | 7868 | } |
| 7869 | |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 7870 | dio_data = kzalloc(sizeof(*dio_data), GFP_NOFS); |
| 7871 | if (!dio_data) |
| 7872 | return -ENOMEM; |
| 7873 | |
| 7874 | dio_data->length = length; |
| 7875 | if (write) { |
| 7876 | dio_data->reserve = round_up(length, fs_info->sectorsize); |
| 7877 | ret = btrfs_delalloc_reserve_space(BTRFS_I(inode), |
| 7878 | &dio_data->data_reserved, |
| 7879 | start, dio_data->reserve); |
| 7880 | if (ret) { |
| 7881 | extent_changeset_free(dio_data->data_reserved); |
| 7882 | kfree(dio_data); |
| 7883 | return ret; |
| 7884 | } |
| 7885 | } |
| 7886 | iomap->private = dio_data; |
| 7887 | |
| 7888 | |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7889 | /* |
| 7890 | * If this errors out it's because we couldn't invalidate pagecache for |
| 7891 | * this range and we need to fallback to buffered. |
| 7892 | */ |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 7893 | if (lock_extent_direct(inode, lockstart, lockend, &cached_state, write)) { |
Filipe Manana | 9c9464c | 2015-11-04 09:52:04 +0000 | [diff] [blame] | 7894 | ret = -ENOTBLK; |
| 7895 | goto err; |
| 7896 | } |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7897 | |
Omar Sandoval | 39b07b5 | 2019-12-02 17:34:23 -0800 | [diff] [blame] | 7898 | em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7899 | if (IS_ERR(em)) { |
| 7900 | ret = PTR_ERR(em); |
| 7901 | goto unlock_err; |
| 7902 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7903 | |
| 7904 | /* |
| 7905 | * Ok for INLINE and COMPRESSED extents we need to fallback on buffered |
| 7906 | * io. INLINE is special, and we could probably kludge it in here, but |
| 7907 | * it's still buffered so for safety lets just fall back to the generic |
| 7908 | * buffered path. |
| 7909 | * |
| 7910 | * For COMPRESSED we _have_ to read the entire extent in so we can |
| 7911 | * decompress it, so there will be buffering required no matter what we |
| 7912 | * do, so go ahead and fallback to buffered. |
| 7913 | * |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 7914 | * We return -ENOTBLK because that's what makes DIO go ahead and go back |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7915 | * to buffered IO. Don't blame me, this is the price we pay for using |
| 7916 | * the generic code. |
| 7917 | */ |
| 7918 | if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) || |
| 7919 | em->block_start == EXTENT_MAP_INLINE) { |
| 7920 | free_extent_map(em); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7921 | ret = -ENOTBLK; |
| 7922 | goto unlock_err; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7923 | } |
| 7924 | |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 7925 | len = min(len, em->len - (start - em->start)); |
| 7926 | if (write) { |
| 7927 | ret = btrfs_get_blocks_direct_write(&em, inode, dio_data, |
| 7928 | start, len); |
Nikolay Borisov | c5794e5 | 2018-05-02 15:19:33 +0300 | [diff] [blame] | 7929 | if (ret < 0) |
| 7930 | goto unlock_err; |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 7931 | unlock_extents = true; |
| 7932 | /* Recalc len in case the new em is smaller than requested */ |
| 7933 | len = min(len, em->len - (start - em->start)); |
Nikolay Borisov | c5794e5 | 2018-05-02 15:19:33 +0300 | [diff] [blame] | 7934 | } else { |
Nikolay Borisov | 1c8d017 | 2018-05-02 15:19:32 +0300 | [diff] [blame] | 7935 | /* |
| 7936 | * We need to unlock only the end area that we aren't using. |
| 7937 | * The rest is going to be unlocked by the endio routine. |
| 7938 | */ |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 7939 | lockstart = start + len; |
| 7940 | if (lockstart < lockend) |
| 7941 | unlock_extents = true; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7942 | } |
| 7943 | |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 7944 | if (unlock_extents) |
| 7945 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, |
| 7946 | lockstart, lockend, &cached_state); |
| 7947 | else |
| 7948 | free_extent_state(cached_state); |
| 7949 | |
| 7950 | /* |
| 7951 | * Translate extent map information to iomap. |
| 7952 | * We trim the extents (and move the addr) even though iomap code does |
| 7953 | * that, since we have locked only the parts we are performing I/O in. |
| 7954 | */ |
| 7955 | if ((em->block_start == EXTENT_MAP_HOLE) || |
| 7956 | (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) && !write)) { |
| 7957 | iomap->addr = IOMAP_NULL_ADDR; |
| 7958 | iomap->type = IOMAP_HOLE; |
| 7959 | } else { |
| 7960 | iomap->addr = em->block_start + (start - em->start); |
| 7961 | iomap->type = IOMAP_MAPPED; |
| 7962 | } |
| 7963 | iomap->offset = start; |
| 7964 | iomap->bdev = fs_info->fs_devices->latest_bdev; |
| 7965 | iomap->length = len; |
| 7966 | |
Johannes Thumshirn | e380adf | 2021-05-19 00:40:27 +0900 | [diff] [blame] | 7967 | if (write && btrfs_use_zone_append(BTRFS_I(inode), em->block_start)) |
Naohiro Aota | 544d24f | 2021-02-04 19:22:06 +0900 | [diff] [blame] | 7968 | iomap->flags |= IOMAP_F_ZONE_APPEND; |
| 7969 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7970 | free_extent_map(em); |
| 7971 | |
| 7972 | return 0; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7973 | |
| 7974 | unlock_err: |
Omar Sandoval | e182163 | 2019-08-15 14:04:04 -0700 | [diff] [blame] | 7975 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend, |
| 7976 | &cached_state); |
Filipe Manana | 9c9464c | 2015-11-04 09:52:04 +0000 | [diff] [blame] | 7977 | err: |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 7978 | if (dio_data) { |
| 7979 | btrfs_delalloc_release_space(BTRFS_I(inode), |
| 7980 | dio_data->data_reserved, start, |
| 7981 | dio_data->reserve, true); |
| 7982 | btrfs_delalloc_release_extents(BTRFS_I(inode), dio_data->reserve); |
| 7983 | extent_changeset_free(dio_data->data_reserved); |
| 7984 | kfree(dio_data); |
| 7985 | } |
| 7986 | return ret; |
| 7987 | } |
| 7988 | |
| 7989 | static int btrfs_dio_iomap_end(struct inode *inode, loff_t pos, loff_t length, |
| 7990 | ssize_t written, unsigned int flags, struct iomap *iomap) |
| 7991 | { |
| 7992 | int ret = 0; |
| 7993 | struct btrfs_dio_data *dio_data = iomap->private; |
| 7994 | size_t submitted = dio_data->submitted; |
| 7995 | const bool write = !!(flags & IOMAP_WRITE); |
| 7996 | |
| 7997 | if (!write && (iomap->type == IOMAP_HOLE)) { |
| 7998 | /* If reading from a hole, unlock and return */ |
| 7999 | unlock_extent(&BTRFS_I(inode)->io_tree, pos, pos + length - 1); |
| 8000 | goto out; |
| 8001 | } |
| 8002 | |
| 8003 | if (submitted < length) { |
| 8004 | pos += submitted; |
| 8005 | length -= submitted; |
| 8006 | if (write) |
| 8007 | __endio_write_update_ordered(BTRFS_I(inode), pos, |
| 8008 | length, false); |
| 8009 | else |
| 8010 | unlock_extent(&BTRFS_I(inode)->io_tree, pos, |
| 8011 | pos + length - 1); |
| 8012 | ret = -ENOTBLK; |
| 8013 | } |
| 8014 | |
| 8015 | if (write) { |
| 8016 | if (dio_data->reserve) |
| 8017 | btrfs_delalloc_release_space(BTRFS_I(inode), |
| 8018 | dio_data->data_reserved, pos, |
| 8019 | dio_data->reserve, true); |
| 8020 | btrfs_delalloc_release_extents(BTRFS_I(inode), dio_data->length); |
| 8021 | extent_changeset_free(dio_data->data_reserved); |
| 8022 | } |
| 8023 | out: |
| 8024 | kfree(dio_data); |
| 8025 | iomap->private = NULL; |
| 8026 | |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 8027 | return ret; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8028 | } |
| 8029 | |
Omar Sandoval | 769b4f2 | 2020-04-16 14:46:22 -0700 | [diff] [blame] | 8030 | static void btrfs_dio_private_put(struct btrfs_dio_private *dip) |
| 8031 | { |
| 8032 | /* |
| 8033 | * This implies a barrier so that stores to dio_bio->bi_status before |
| 8034 | * this and loads of dio_bio->bi_status after this are fully ordered. |
| 8035 | */ |
| 8036 | if (!refcount_dec_and_test(&dip->refs)) |
| 8037 | return; |
| 8038 | |
Naohiro Aota | cfe9444 | 2021-02-04 19:21:59 +0900 | [diff] [blame] | 8039 | if (btrfs_op(dip->dio_bio) == BTRFS_MAP_WRITE) { |
Nikolay Borisov | b672b5c | 2020-06-03 08:55:24 +0300 | [diff] [blame] | 8040 | __endio_write_update_ordered(BTRFS_I(dip->inode), |
| 8041 | dip->logical_offset, |
Omar Sandoval | 769b4f2 | 2020-04-16 14:46:22 -0700 | [diff] [blame] | 8042 | dip->bytes, |
| 8043 | !dip->dio_bio->bi_status); |
| 8044 | } else { |
| 8045 | unlock_extent(&BTRFS_I(dip->inode)->io_tree, |
| 8046 | dip->logical_offset, |
| 8047 | dip->logical_offset + dip->bytes - 1); |
| 8048 | } |
| 8049 | |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 8050 | bio_endio(dip->dio_bio); |
Omar Sandoval | 769b4f2 | 2020-04-16 14:46:22 -0700 | [diff] [blame] | 8051 | kfree(dip); |
| 8052 | } |
| 8053 | |
Omar Sandoval | 77d5d68 | 2020-04-16 14:46:25 -0700 | [diff] [blame] | 8054 | static blk_status_t submit_dio_repair_bio(struct inode *inode, struct bio *bio, |
| 8055 | int mirror_num, |
| 8056 | unsigned long bio_flags) |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8057 | { |
Omar Sandoval | 77d5d68 | 2020-04-16 14:46:25 -0700 | [diff] [blame] | 8058 | struct btrfs_dio_private *dip = bio->bi_private; |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 8059 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Omar Sandoval | 58efbc9 | 2017-08-22 23:45:59 -0700 | [diff] [blame] | 8060 | blk_status_t ret; |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8061 | |
Mike Christie | 37226b2 | 2016-06-05 14:31:52 -0500 | [diff] [blame] | 8062 | BUG_ON(bio_op(bio) == REQ_OP_WRITE); |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8063 | |
Omar Sandoval | 5c047a6 | 2020-04-16 14:46:24 -0700 | [diff] [blame] | 8064 | ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DATA); |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8065 | if (ret) |
Nikolay Borisov | ea057f6 | 2017-12-13 10:25:38 +0200 | [diff] [blame] | 8066 | return ret; |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8067 | |
Omar Sandoval | fd9d667 | 2020-04-16 14:46:23 -0700 | [diff] [blame] | 8068 | refcount_inc(&dip->refs); |
Chris Mason | 08635ba | 2019-07-10 12:28:14 -0700 | [diff] [blame] | 8069 | ret = btrfs_map_bio(fs_info, bio, mirror_num); |
Omar Sandoval | 77d5d68 | 2020-04-16 14:46:25 -0700 | [diff] [blame] | 8070 | if (ret) |
Omar Sandoval | fd9d667 | 2020-04-16 14:46:23 -0700 | [diff] [blame] | 8071 | refcount_dec(&dip->refs); |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8072 | return ret; |
| 8073 | } |
| 8074 | |
Omar Sandoval | 769b4f2 | 2020-04-16 14:46:22 -0700 | [diff] [blame] | 8075 | static blk_status_t btrfs_check_read_dio_bio(struct inode *inode, |
| 8076 | struct btrfs_io_bio *io_bio, |
Omar Sandoval | fd9d667 | 2020-04-16 14:46:23 -0700 | [diff] [blame] | 8077 | const bool uptodate) |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8078 | { |
Omar Sandoval | fd9d667 | 2020-04-16 14:46:23 -0700 | [diff] [blame] | 8079 | struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info; |
| 8080 | const u32 sectorsize = fs_info->sectorsize; |
Josef Bacik | 7870d08 | 2017-05-05 11:57:15 -0400 | [diff] [blame] | 8081 | struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree; |
Omar Sandoval | fd9d667 | 2020-04-16 14:46:23 -0700 | [diff] [blame] | 8082 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
| 8083 | const bool csum = !(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM); |
Liu Bo | 17347ce | 2017-05-15 15:33:27 -0700 | [diff] [blame] | 8084 | struct bio_vec bvec; |
| 8085 | struct bvec_iter iter; |
Omar Sandoval | fd9d667 | 2020-04-16 14:46:23 -0700 | [diff] [blame] | 8086 | u64 start = io_bio->logical; |
Qu Wenruo | 7ffd27e | 2020-12-02 14:47:58 +0800 | [diff] [blame] | 8087 | u32 bio_offset = 0; |
Omar Sandoval | 58efbc9 | 2017-08-22 23:45:59 -0700 | [diff] [blame] | 8088 | blk_status_t err = BLK_STS_OK; |
Miao Xie | dc380ae | 2014-09-12 18:43:55 +0800 | [diff] [blame] | 8089 | |
Omar Sandoval | fd9d667 | 2020-04-16 14:46:23 -0700 | [diff] [blame] | 8090 | __bio_for_each_segment(bvec, &io_bio->bio, iter, io_bio->iter) { |
| 8091 | unsigned int i, nr_sectors, pgoff; |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 8092 | |
Liu Bo | 17347ce | 2017-05-15 15:33:27 -0700 | [diff] [blame] | 8093 | nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec.bv_len); |
| 8094 | pgoff = bvec.bv_offset; |
Omar Sandoval | fd9d667 | 2020-04-16 14:46:23 -0700 | [diff] [blame] | 8095 | for (i = 0; i < nr_sectors; i++) { |
Liu Bo | 97bf5a5 | 2017-04-07 13:11:10 -0700 | [diff] [blame] | 8096 | ASSERT(pgoff < PAGE_SIZE); |
Omar Sandoval | fd9d667 | 2020-04-16 14:46:23 -0700 | [diff] [blame] | 8097 | if (uptodate && |
Qu Wenruo | 7ffd27e | 2020-12-02 14:47:58 +0800 | [diff] [blame] | 8098 | (!csum || !check_data_csum(inode, io_bio, |
Omar Sandoval | c1d6abd | 2021-03-16 12:43:00 -0700 | [diff] [blame] | 8099 | bio_offset, bvec.bv_page, |
| 8100 | pgoff, start))) { |
Omar Sandoval | fd9d667 | 2020-04-16 14:46:23 -0700 | [diff] [blame] | 8101 | clean_io_failure(fs_info, failure_tree, io_tree, |
| 8102 | start, bvec.bv_page, |
| 8103 | btrfs_ino(BTRFS_I(inode)), |
| 8104 | pgoff); |
| 8105 | } else { |
Qu Wenruo | 150e4b0 | 2021-05-03 10:08:55 +0800 | [diff] [blame] | 8106 | int ret; |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8107 | |
Qu Wenruo | 7ffd27e | 2020-12-02 14:47:58 +0800 | [diff] [blame] | 8108 | ASSERT((start - io_bio->logical) < UINT_MAX); |
Qu Wenruo | 150e4b0 | 2021-05-03 10:08:55 +0800 | [diff] [blame] | 8109 | ret = btrfs_repair_one_sector(inode, |
| 8110 | &io_bio->bio, |
| 8111 | start - io_bio->logical, |
| 8112 | bvec.bv_page, pgoff, |
| 8113 | start, io_bio->mirror_num, |
| 8114 | submit_dio_repair_bio); |
| 8115 | if (ret) |
| 8116 | err = errno_to_blk_status(ret); |
Omar Sandoval | fd9d667 | 2020-04-16 14:46:23 -0700 | [diff] [blame] | 8117 | } |
| 8118 | start += sectorsize; |
Qu Wenruo | 7ffd27e | 2020-12-02 14:47:58 +0800 | [diff] [blame] | 8119 | ASSERT(bio_offset + sectorsize > bio_offset); |
| 8120 | bio_offset += sectorsize; |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 8121 | pgoff += sectorsize; |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 8122 | } |
Kent Overstreet | 2c30c71 | 2013-11-07 12:20:26 -0800 | [diff] [blame] | 8123 | } |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 8124 | return err; |
| 8125 | } |
| 8126 | |
Nikolay Borisov | b672b5c | 2020-06-03 08:55:24 +0300 | [diff] [blame] | 8127 | static void __endio_write_update_ordered(struct btrfs_inode *inode, |
Qu Wenruo | 52427260 | 2017-03-08 10:25:52 +0800 | [diff] [blame] | 8128 | const u64 offset, const u64 bytes, |
| 8129 | const bool uptodate) |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8130 | { |
Qu Wenruo | e65f152 | 2021-04-01 15:15:06 +0800 | [diff] [blame] | 8131 | btrfs_mark_ordered_io_finished(inode, NULL, offset, bytes, |
| 8132 | finish_ordered_fn, uptodate); |
Filipe Manana | 1454377 | 2015-11-24 16:23:54 +0000 | [diff] [blame] | 8133 | } |
| 8134 | |
Qu Wenruo | 8896a08 | 2020-10-21 14:24:53 +0800 | [diff] [blame] | 8135 | static blk_status_t btrfs_submit_bio_start_direct_io(struct inode *inode, |
Qu Wenruo | 1941b64 | 2020-12-02 14:47:57 +0800 | [diff] [blame] | 8136 | struct bio *bio, |
| 8137 | u64 dio_file_offset) |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 8138 | { |
Qu Wenruo | 1941b64 | 2020-12-02 14:47:57 +0800 | [diff] [blame] | 8139 | return btrfs_csum_one_bio(BTRFS_I(inode), bio, dio_file_offset, 1); |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 8140 | } |
| 8141 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 8142 | static void btrfs_end_dio_bio(struct bio *bio) |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8143 | { |
| 8144 | struct btrfs_dio_private *dip = bio->bi_private; |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 8145 | blk_status_t err = bio->bi_status; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8146 | |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8147 | if (err) |
| 8148 | btrfs_warn(BTRFS_I(dip->inode)->root->fs_info, |
Mike Christie | 6296b96 | 2016-06-05 14:32:21 -0500 | [diff] [blame] | 8149 | "direct IO failed ino %llu rw %d,%u sector %#Lx len %u err no %d", |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 8150 | btrfs_ino(BTRFS_I(dip->inode)), bio_op(bio), |
David Sterba | 1201b58 | 2020-11-26 15:41:27 +0100 | [diff] [blame] | 8151 | bio->bi_opf, bio->bi_iter.bi_sector, |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8152 | bio->bi_iter.bi_size, err); |
| 8153 | |
Omar Sandoval | 769b4f2 | 2020-04-16 14:46:22 -0700 | [diff] [blame] | 8154 | if (bio_op(bio) == REQ_OP_READ) { |
| 8155 | err = btrfs_check_read_dio_bio(dip->inode, btrfs_io_bio(bio), |
Omar Sandoval | fd9d667 | 2020-04-16 14:46:23 -0700 | [diff] [blame] | 8156 | !err); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8157 | } |
| 8158 | |
Omar Sandoval | 769b4f2 | 2020-04-16 14:46:22 -0700 | [diff] [blame] | 8159 | if (err) |
| 8160 | dip->dio_bio->bi_status = err; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8161 | |
Naohiro Aota | 544d24f | 2021-02-04 19:22:06 +0900 | [diff] [blame] | 8162 | btrfs_record_physical_zoned(dip->inode, dip->logical_offset, bio); |
| 8163 | |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8164 | bio_put(bio); |
Omar Sandoval | 769b4f2 | 2020-04-16 14:46:22 -0700 | [diff] [blame] | 8165 | btrfs_dio_private_put(dip); |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 8166 | } |
| 8167 | |
David Sterba | d0ee393 | 2018-03-08 14:35:48 +0100 | [diff] [blame] | 8168 | static inline blk_status_t btrfs_submit_dio_bio(struct bio *bio, |
| 8169 | struct inode *inode, u64 file_offset, int async_submit) |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8170 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8171 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 8172 | struct btrfs_dio_private *dip = bio->bi_private; |
Naohiro Aota | cfe9444 | 2021-02-04 19:21:59 +0900 | [diff] [blame] | 8173 | bool write = btrfs_op(bio) == BTRFS_MAP_WRITE; |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 8174 | blk_status_t ret; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8175 | |
Liu Bo | 4c274bc | 2017-11-01 17:19:27 -0600 | [diff] [blame] | 8176 | /* Check btrfs_submit_bio_hook() for rules about async submit. */ |
Josef Bacik | b812ce2 | 2012-11-16 13:56:32 -0500 | [diff] [blame] | 8177 | if (async_submit) |
| 8178 | async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers); |
| 8179 | |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 8180 | if (!write) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8181 | ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DATA); |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 8182 | if (ret) |
| 8183 | goto err; |
| 8184 | } |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8185 | |
Nikolay Borisov | e6961ca | 2017-08-03 15:44:58 +0300 | [diff] [blame] | 8186 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM) |
Josef Bacik | 1ae3993 | 2011-04-06 14:41:34 -0400 | [diff] [blame] | 8187 | goto map; |
| 8188 | |
| 8189 | if (write && async_submit) { |
Qu Wenruo | 1941b64 | 2020-12-02 14:47:57 +0800 | [diff] [blame] | 8190 | ret = btrfs_wq_submit_bio(inode, bio, 0, 0, file_offset, |
David Sterba | e288c08 | 2018-07-18 17:36:24 +0200 | [diff] [blame] | 8191 | btrfs_submit_bio_start_direct_io); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8192 | goto err; |
Josef Bacik | 1ae3993 | 2011-04-06 14:41:34 -0400 | [diff] [blame] | 8193 | } else if (write) { |
| 8194 | /* |
| 8195 | * If we aren't doing async submit, calculate the csum of the |
| 8196 | * bio now. |
| 8197 | */ |
Nikolay Borisov | bd242a0 | 2020-06-03 08:55:07 +0300 | [diff] [blame] | 8198 | ret = btrfs_csum_one_bio(BTRFS_I(inode), bio, file_offset, 1); |
Josef Bacik | 1ae3993 | 2011-04-06 14:41:34 -0400 | [diff] [blame] | 8199 | if (ret) |
| 8200 | goto err; |
Miao Xie | 23ea8e5 | 2014-09-12 18:43:54 +0800 | [diff] [blame] | 8201 | } else { |
Omar Sandoval | 8587957 | 2020-04-16 14:46:21 -0700 | [diff] [blame] | 8202 | u64 csum_offset; |
| 8203 | |
| 8204 | csum_offset = file_offset - dip->logical_offset; |
David Sterba | 265fdfa | 2020-07-01 21:19:09 +0200 | [diff] [blame] | 8205 | csum_offset >>= fs_info->sectorsize_bits; |
David Sterba | 55fc29b | 2020-06-30 02:01:31 +0200 | [diff] [blame] | 8206 | csum_offset *= fs_info->csum_size; |
Omar Sandoval | 8587957 | 2020-04-16 14:46:21 -0700 | [diff] [blame] | 8207 | btrfs_io_bio(bio)->csum = dip->csums + csum_offset; |
Tsutomu Itoh | c2db107 | 2011-03-01 06:48:31 +0000 | [diff] [blame] | 8208 | } |
Josef Bacik | 1ae3993 | 2011-04-06 14:41:34 -0400 | [diff] [blame] | 8209 | map: |
Chris Mason | 08635ba | 2019-07-10 12:28:14 -0700 | [diff] [blame] | 8210 | ret = btrfs_map_bio(fs_info, bio, 0); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8211 | err: |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8212 | return ret; |
| 8213 | } |
| 8214 | |
Omar Sandoval | c36cac2 | 2020-04-16 14:46:13 -0700 | [diff] [blame] | 8215 | /* |
| 8216 | * If this succeeds, the btrfs_dio_private is responsible for cleaning up locked |
| 8217 | * or ordered extents whether or not we submit any bios. |
| 8218 | */ |
| 8219 | static struct btrfs_dio_private *btrfs_create_dio_private(struct bio *dio_bio, |
| 8220 | struct inode *inode, |
| 8221 | loff_t file_offset) |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8222 | { |
Naohiro Aota | cfe9444 | 2021-02-04 19:21:59 +0900 | [diff] [blame] | 8223 | const bool write = (btrfs_op(dio_bio) == BTRFS_MAP_WRITE); |
Omar Sandoval | 8587957 | 2020-04-16 14:46:21 -0700 | [diff] [blame] | 8224 | const bool csum = !(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM); |
| 8225 | size_t dip_size; |
Omar Sandoval | c36cac2 | 2020-04-16 14:46:13 -0700 | [diff] [blame] | 8226 | struct btrfs_dio_private *dip; |
Omar Sandoval | c36cac2 | 2020-04-16 14:46:13 -0700 | [diff] [blame] | 8227 | |
Omar Sandoval | 8587957 | 2020-04-16 14:46:21 -0700 | [diff] [blame] | 8228 | dip_size = sizeof(*dip); |
| 8229 | if (!write && csum) { |
| 8230 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Omar Sandoval | 8587957 | 2020-04-16 14:46:21 -0700 | [diff] [blame] | 8231 | size_t nblocks; |
| 8232 | |
David Sterba | 265fdfa | 2020-07-01 21:19:09 +0200 | [diff] [blame] | 8233 | nblocks = dio_bio->bi_iter.bi_size >> fs_info->sectorsize_bits; |
David Sterba | 223486c | 2020-07-02 11:27:30 +0200 | [diff] [blame] | 8234 | dip_size += fs_info->csum_size * nblocks; |
Omar Sandoval | 8587957 | 2020-04-16 14:46:21 -0700 | [diff] [blame] | 8235 | } |
| 8236 | |
| 8237 | dip = kzalloc(dip_size, GFP_NOFS); |
Omar Sandoval | c36cac2 | 2020-04-16 14:46:13 -0700 | [diff] [blame] | 8238 | if (!dip) |
| 8239 | return NULL; |
| 8240 | |
Omar Sandoval | c36cac2 | 2020-04-16 14:46:13 -0700 | [diff] [blame] | 8241 | dip->inode = inode; |
| 8242 | dip->logical_offset = file_offset; |
| 8243 | dip->bytes = dio_bio->bi_iter.bi_size; |
David Sterba | 1201b58 | 2020-11-26 15:41:27 +0100 | [diff] [blame] | 8244 | dip->disk_bytenr = dio_bio->bi_iter.bi_sector << 9; |
Omar Sandoval | c36cac2 | 2020-04-16 14:46:13 -0700 | [diff] [blame] | 8245 | dip->dio_bio = dio_bio; |
Omar Sandoval | e3b318d | 2020-04-16 14:46:20 -0700 | [diff] [blame] | 8246 | refcount_set(&dip->refs, 1); |
Omar Sandoval | c36cac2 | 2020-04-16 14:46:13 -0700 | [diff] [blame] | 8247 | return dip; |
| 8248 | } |
| 8249 | |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 8250 | static blk_qc_t btrfs_submit_direct(struct inode *inode, struct iomap *iomap, |
| 8251 | struct bio *dio_bio, loff_t file_offset) |
Omar Sandoval | c36cac2 | 2020-04-16 14:46:13 -0700 | [diff] [blame] | 8252 | { |
Naohiro Aota | cfe9444 | 2021-02-04 19:21:59 +0900 | [diff] [blame] | 8253 | const bool write = (btrfs_op(dio_bio) == BTRFS_MAP_WRITE); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8254 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Omar Sandoval | 769b4f2 | 2020-04-16 14:46:22 -0700 | [diff] [blame] | 8255 | const bool raid56 = (btrfs_data_alloc_profile(fs_info) & |
| 8256 | BTRFS_BLOCK_GROUP_RAID56_MASK); |
Omar Sandoval | c36cac2 | 2020-04-16 14:46:13 -0700 | [diff] [blame] | 8257 | struct btrfs_dio_private *dip; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8258 | struct bio *bio; |
Omar Sandoval | c36cac2 | 2020-04-16 14:46:13 -0700 | [diff] [blame] | 8259 | u64 start_sector; |
Josef Bacik | 1ae3993 | 2011-04-06 14:41:34 -0400 | [diff] [blame] | 8260 | int async_submit = 0; |
Liu Bo | 725130b | 2017-05-16 09:51:39 -0700 | [diff] [blame] | 8261 | u64 submit_len; |
Naohiro Aota | 42b5d73 | 2021-07-21 21:43:34 +0900 | [diff] [blame] | 8262 | u64 clone_offset = 0; |
| 8263 | u64 clone_len; |
Michal Rostecki | 4203431 | 2021-01-27 14:57:27 +0100 | [diff] [blame] | 8264 | u64 logical; |
Chandan Rajendra | 5f4dc8f | 2016-01-21 15:56:00 +0530 | [diff] [blame] | 8265 | int ret; |
Omar Sandoval | 58efbc9 | 2017-08-22 23:45:59 -0700 | [diff] [blame] | 8266 | blk_status_t status; |
Nikolay Borisov | 89b798a | 2019-06-03 12:05:05 +0300 | [diff] [blame] | 8267 | struct btrfs_io_geometry geom; |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 8268 | struct btrfs_dio_data *dio_data = iomap->private; |
Michal Rostecki | 4203431 | 2021-01-27 14:57:27 +0100 | [diff] [blame] | 8269 | struct extent_map *em = NULL; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8270 | |
Omar Sandoval | c36cac2 | 2020-04-16 14:46:13 -0700 | [diff] [blame] | 8271 | dip = btrfs_create_dio_private(dio_bio, inode, file_offset); |
| 8272 | if (!dip) { |
| 8273 | if (!write) { |
| 8274 | unlock_extent(&BTRFS_I(inode)->io_tree, file_offset, |
| 8275 | file_offset + dio_bio->bi_iter.bi_size - 1); |
| 8276 | } |
| 8277 | dio_bio->bi_status = BLK_STS_RESOURCE; |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 8278 | bio_endio(dio_bio); |
| 8279 | return BLK_QC_T_NONE; |
Josef Bacik | 02f57c7 | 2011-04-06 14:25:44 -0400 | [diff] [blame] | 8280 | } |
| 8281 | |
Josef Bacik | 334c16d | 2020-10-16 11:29:14 -0400 | [diff] [blame] | 8282 | if (!write) { |
Omar Sandoval | 8587957 | 2020-04-16 14:46:21 -0700 | [diff] [blame] | 8283 | /* |
| 8284 | * Load the csums up front to reduce csum tree searches and |
| 8285 | * contention when submitting bios. |
Josef Bacik | 334c16d | 2020-10-16 11:29:14 -0400 | [diff] [blame] | 8286 | * |
| 8287 | * If we have csums disabled this will do nothing. |
Omar Sandoval | 8587957 | 2020-04-16 14:46:21 -0700 | [diff] [blame] | 8288 | */ |
Qu Wenruo | 6275193 | 2020-12-02 14:48:06 +0800 | [diff] [blame] | 8289 | status = btrfs_lookup_bio_sums(inode, dio_bio, dip->csums); |
Omar Sandoval | 8587957 | 2020-04-16 14:46:21 -0700 | [diff] [blame] | 8290 | if (status != BLK_STS_OK) |
| 8291 | goto out_err; |
| 8292 | } |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 8293 | |
Omar Sandoval | 769b4f2 | 2020-04-16 14:46:22 -0700 | [diff] [blame] | 8294 | start_sector = dio_bio->bi_iter.bi_sector; |
| 8295 | submit_len = dio_bio->bi_iter.bi_size; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8296 | |
Arnd Bergmann | 3c91ee6 | 2017-05-18 15:33:29 +0200 | [diff] [blame] | 8297 | do { |
Michal Rostecki | 4203431 | 2021-01-27 14:57:27 +0100 | [diff] [blame] | 8298 | logical = start_sector << 9; |
| 8299 | em = btrfs_get_chunk_map(fs_info, logical, submit_len); |
| 8300 | if (IS_ERR(em)) { |
| 8301 | status = errno_to_blk_status(PTR_ERR(em)); |
| 8302 | em = NULL; |
| 8303 | goto out_err_em; |
| 8304 | } |
| 8305 | ret = btrfs_get_io_geometry(fs_info, em, btrfs_op(dio_bio), |
Qu Wenruo | 43c0d1a | 2021-04-13 17:58:48 +0800 | [diff] [blame] | 8306 | logical, &geom); |
Omar Sandoval | 769b4f2 | 2020-04-16 14:46:22 -0700 | [diff] [blame] | 8307 | if (ret) { |
| 8308 | status = errno_to_blk_status(ret); |
Michal Rostecki | 4203431 | 2021-01-27 14:57:27 +0100 | [diff] [blame] | 8309 | goto out_err_em; |
Omar Sandoval | 769b4f2 | 2020-04-16 14:46:22 -0700 | [diff] [blame] | 8310 | } |
Omar Sandoval | 769b4f2 | 2020-04-16 14:46:22 -0700 | [diff] [blame] | 8311 | |
Naohiro Aota | 42b5d73 | 2021-07-21 21:43:34 +0900 | [diff] [blame] | 8312 | clone_len = min(submit_len, geom.len); |
| 8313 | ASSERT(clone_len <= UINT_MAX); |
Josef Bacik | 02f57c7 | 2011-04-06 14:25:44 -0400 | [diff] [blame] | 8314 | |
Liu Bo | 725130b | 2017-05-16 09:51:39 -0700 | [diff] [blame] | 8315 | /* |
| 8316 | * This will never fail as it's passing GPF_NOFS and |
| 8317 | * the allocation is backed by btrfs_bioset. |
| 8318 | */ |
Omar Sandoval | 769b4f2 | 2020-04-16 14:46:22 -0700 | [diff] [blame] | 8319 | bio = btrfs_bio_clone_partial(dio_bio, clone_offset, clone_len); |
Liu Bo | 725130b | 2017-05-16 09:51:39 -0700 | [diff] [blame] | 8320 | bio->bi_private = dip; |
| 8321 | bio->bi_end_io = btrfs_end_dio_bio; |
| 8322 | btrfs_io_bio(bio)->logical = file_offset; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8323 | |
Naohiro Aota | 544d24f | 2021-02-04 19:22:06 +0900 | [diff] [blame] | 8324 | if (bio_op(bio) == REQ_OP_ZONE_APPEND) { |
| 8325 | status = extract_ordered_extent(BTRFS_I(inode), bio, |
| 8326 | file_offset); |
| 8327 | if (status) { |
| 8328 | bio_put(bio); |
| 8329 | goto out_err; |
| 8330 | } |
| 8331 | } |
| 8332 | |
Liu Bo | 725130b | 2017-05-16 09:51:39 -0700 | [diff] [blame] | 8333 | ASSERT(submit_len >= clone_len); |
| 8334 | submit_len -= clone_len; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8335 | |
Liu Bo | 725130b | 2017-05-16 09:51:39 -0700 | [diff] [blame] | 8336 | /* |
| 8337 | * Increase the count before we submit the bio so we know |
| 8338 | * the end IO handler won't happen before we increase the |
| 8339 | * count. Otherwise, the dip might get freed before we're |
| 8340 | * done setting it up. |
Omar Sandoval | 769b4f2 | 2020-04-16 14:46:22 -0700 | [diff] [blame] | 8341 | * |
| 8342 | * We transfer the initial reference to the last bio, so we |
| 8343 | * don't need to increment the reference count for the last one. |
Liu Bo | 725130b | 2017-05-16 09:51:39 -0700 | [diff] [blame] | 8344 | */ |
Omar Sandoval | 769b4f2 | 2020-04-16 14:46:22 -0700 | [diff] [blame] | 8345 | if (submit_len > 0) { |
| 8346 | refcount_inc(&dip->refs); |
| 8347 | /* |
| 8348 | * If we are submitting more than one bio, submit them |
| 8349 | * all asynchronously. The exception is RAID 5 or 6, as |
| 8350 | * asynchronous checksums make it difficult to collect |
| 8351 | * full stripe writes. |
| 8352 | */ |
| 8353 | if (!raid56) |
| 8354 | async_submit = 1; |
| 8355 | } |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8356 | |
David Sterba | d0ee393 | 2018-03-08 14:35:48 +0100 | [diff] [blame] | 8357 | status = btrfs_submit_dio_bio(bio, inode, file_offset, |
Omar Sandoval | 58efbc9 | 2017-08-22 23:45:59 -0700 | [diff] [blame] | 8358 | async_submit); |
| 8359 | if (status) { |
Liu Bo | 725130b | 2017-05-16 09:51:39 -0700 | [diff] [blame] | 8360 | bio_put(bio); |
Omar Sandoval | 769b4f2 | 2020-04-16 14:46:22 -0700 | [diff] [blame] | 8361 | if (submit_len > 0) |
| 8362 | refcount_dec(&dip->refs); |
Michal Rostecki | 4203431 | 2021-01-27 14:57:27 +0100 | [diff] [blame] | 8363 | goto out_err_em; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8364 | } |
Liu Bo | 725130b | 2017-05-16 09:51:39 -0700 | [diff] [blame] | 8365 | |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 8366 | dio_data->submitted += clone_len; |
Liu Bo | 725130b | 2017-05-16 09:51:39 -0700 | [diff] [blame] | 8367 | clone_offset += clone_len; |
| 8368 | start_sector += clone_len >> 9; |
| 8369 | file_offset += clone_len; |
Michal Rostecki | 4203431 | 2021-01-27 14:57:27 +0100 | [diff] [blame] | 8370 | |
| 8371 | free_extent_map(em); |
Arnd Bergmann | 3c91ee6 | 2017-05-18 15:33:29 +0200 | [diff] [blame] | 8372 | } while (submit_len > 0); |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 8373 | return BLK_QC_T_NONE; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8374 | |
Michal Rostecki | 4203431 | 2021-01-27 14:57:27 +0100 | [diff] [blame] | 8375 | out_err_em: |
| 8376 | free_extent_map(em); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8377 | out_err: |
Omar Sandoval | 769b4f2 | 2020-04-16 14:46:22 -0700 | [diff] [blame] | 8378 | dip->dio_bio->bi_status = status; |
| 8379 | btrfs_dio_private_put(dip); |
Michal Rostecki | 4203431 | 2021-01-27 14:57:27 +0100 | [diff] [blame] | 8380 | |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 8381 | return BLK_QC_T_NONE; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8382 | } |
| 8383 | |
Goldwyn Rodrigues | 4e4cabe | 2020-09-24 11:39:12 -0500 | [diff] [blame] | 8384 | const struct iomap_ops btrfs_dio_iomap_ops = { |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 8385 | .iomap_begin = btrfs_dio_iomap_begin, |
| 8386 | .iomap_end = btrfs_dio_iomap_end, |
| 8387 | }; |
| 8388 | |
Goldwyn Rodrigues | 4e4cabe | 2020-09-24 11:39:12 -0500 | [diff] [blame] | 8389 | const struct iomap_dio_ops btrfs_dio_ops = { |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 8390 | .submit_io = btrfs_submit_direct, |
| 8391 | }; |
| 8392 | |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 8393 | static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, |
David Sterba | bab16e2 | 2020-06-23 20:56:12 +0200 | [diff] [blame] | 8394 | u64 start, u64 len) |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 8395 | { |
Tsutomu Itoh | 05dadc0 | 2012-11-29 05:08:26 +0000 | [diff] [blame] | 8396 | int ret; |
| 8397 | |
Christoph Hellwig | 45dd052 | 2020-05-23 09:30:14 +0200 | [diff] [blame] | 8398 | ret = fiemap_prep(inode, fieinfo, start, &len, 0); |
Tsutomu Itoh | 05dadc0 | 2012-11-29 05:08:26 +0000 | [diff] [blame] | 8399 | if (ret) |
| 8400 | return ret; |
| 8401 | |
Nikolay Borisov | facee0a | 2020-08-31 14:42:49 +0300 | [diff] [blame] | 8402 | return extent_fiemap(BTRFS_I(inode), fieinfo, start, len); |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 8403 | } |
| 8404 | |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8405 | int btrfs_readpage(struct file *file, struct page *page) |
| 8406 | { |
Nikolay Borisov | 0f20881 | 2020-09-14 14:39:16 +0300 | [diff] [blame] | 8407 | struct btrfs_inode *inode = BTRFS_I(page->mapping->host); |
| 8408 | u64 start = page_offset(page); |
| 8409 | u64 end = start + PAGE_SIZE - 1; |
Qu Wenruo | 390ed29 | 2021-04-14 16:42:15 +0800 | [diff] [blame] | 8410 | struct btrfs_bio_ctrl bio_ctrl = { 0 }; |
Nikolay Borisov | c1be9c1 | 2020-09-14 12:37:08 +0300 | [diff] [blame] | 8411 | int ret; |
| 8412 | |
Nikolay Borisov | 0f20881 | 2020-09-14 14:39:16 +0300 | [diff] [blame] | 8413 | btrfs_lock_and_flush_ordered_range(inode, start, end, NULL); |
| 8414 | |
Qu Wenruo | 390ed29 | 2021-04-14 16:42:15 +0800 | [diff] [blame] | 8415 | ret = btrfs_do_readpage(page, NULL, &bio_ctrl, 0, NULL); |
| 8416 | if (bio_ctrl.bio) |
| 8417 | ret = submit_one_bio(bio_ctrl.bio, 0, bio_ctrl.bio_flags); |
Nikolay Borisov | c1be9c1 | 2020-09-14 12:37:08 +0300 | [diff] [blame] | 8418 | return ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8419 | } |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 8420 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8421 | static int btrfs_writepage(struct page *page, struct writeback_control *wbc) |
| 8422 | { |
Josef Bacik | be7bd73 | 2015-10-22 15:05:09 -0400 | [diff] [blame] | 8423 | struct inode *inode = page->mapping->host; |
| 8424 | int ret; |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8425 | |
| 8426 | if (current->flags & PF_MEMALLOC) { |
| 8427 | redirty_page_for_writepage(wbc, page); |
| 8428 | unlock_page(page); |
| 8429 | return 0; |
| 8430 | } |
Josef Bacik | be7bd73 | 2015-10-22 15:05:09 -0400 | [diff] [blame] | 8431 | |
| 8432 | /* |
| 8433 | * If we are under memory pressure we will call this directly from the |
| 8434 | * VM, we need to make sure we have the inode referenced for the ordered |
| 8435 | * extent. If not just return like we didn't do anything. |
| 8436 | */ |
| 8437 | if (!igrab(inode)) { |
| 8438 | redirty_page_for_writepage(wbc, page); |
| 8439 | return AOP_WRITEPAGE_ACTIVATE; |
| 8440 | } |
Nikolay Borisov | 0a9b0e5 | 2017-12-08 15:55:59 +0200 | [diff] [blame] | 8441 | ret = extent_write_full_page(page, wbc); |
Josef Bacik | be7bd73 | 2015-10-22 15:05:09 -0400 | [diff] [blame] | 8442 | btrfs_add_delayed_iput(inode); |
| 8443 | return ret; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8444 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8445 | |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 8446 | static int btrfs_writepages(struct address_space *mapping, |
| 8447 | struct writeback_control *wbc) |
Chris Mason | b293f02e | 2007-11-01 19:45:34 -0400 | [diff] [blame] | 8448 | { |
Nikolay Borisov | 8ae225a | 2018-04-19 10:46:38 +0300 | [diff] [blame] | 8449 | return extent_writepages(mapping, wbc); |
Chris Mason | b293f02e | 2007-11-01 19:45:34 -0400 | [diff] [blame] | 8450 | } |
| 8451 | |
Matthew Wilcox (Oracle) | ba206a0 | 2020-06-01 21:47:05 -0700 | [diff] [blame] | 8452 | static void btrfs_readahead(struct readahead_control *rac) |
Chris Mason | 3ab2fb5 | 2007-11-08 10:59:22 -0500 | [diff] [blame] | 8453 | { |
Matthew Wilcox (Oracle) | ba206a0 | 2020-06-01 21:47:05 -0700 | [diff] [blame] | 8454 | extent_readahead(rac); |
Chris Mason | 3ab2fb5 | 2007-11-08 10:59:22 -0500 | [diff] [blame] | 8455 | } |
Nikolay Borisov | 2a3ff0a | 2018-04-19 10:46:36 +0300 | [diff] [blame] | 8456 | |
Qu Wenruo | 7c11d0a | 2021-07-26 14:35:03 +0800 | [diff] [blame] | 8457 | /* |
| 8458 | * For releasepage() and invalidatepage() we have a race window where |
| 8459 | * end_page_writeback() is called but the subpage spinlock is not yet released. |
| 8460 | * If we continue to release/invalidate the page, we could cause use-after-free |
| 8461 | * for subpage spinlock. So this function is to spin and wait for subpage |
| 8462 | * spinlock. |
| 8463 | */ |
| 8464 | static void wait_subpage_spinlock(struct page *page) |
| 8465 | { |
| 8466 | struct btrfs_fs_info *fs_info = btrfs_sb(page->mapping->host->i_sb); |
| 8467 | struct btrfs_subpage *subpage; |
| 8468 | |
| 8469 | if (fs_info->sectorsize == PAGE_SIZE) |
| 8470 | return; |
| 8471 | |
| 8472 | ASSERT(PagePrivate(page) && page->private); |
| 8473 | subpage = (struct btrfs_subpage *)page->private; |
| 8474 | |
| 8475 | /* |
| 8476 | * This may look insane as we just acquire the spinlock and release it, |
| 8477 | * without doing anything. But we just want to make sure no one is |
| 8478 | * still holding the subpage spinlock. |
| 8479 | * And since the page is not dirty nor writeback, and we have page |
| 8480 | * locked, the only possible way to hold a spinlock is from the endio |
| 8481 | * function to clear page writeback. |
| 8482 | * |
| 8483 | * Here we just acquire the spinlock so that all existing callers |
| 8484 | * should exit and we're safe to release/invalidate the page. |
| 8485 | */ |
| 8486 | spin_lock_irq(&subpage->lock); |
| 8487 | spin_unlock_irq(&subpage->lock); |
| 8488 | } |
| 8489 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8490 | static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8491 | { |
Nikolay Borisov | 477a30b | 2018-04-19 10:46:34 +0300 | [diff] [blame] | 8492 | int ret = try_release_extent_mapping(page, gfp_flags); |
Qu Wenruo | 7c11d0a | 2021-07-26 14:35:03 +0800 | [diff] [blame] | 8493 | |
| 8494 | if (ret == 1) { |
| 8495 | wait_subpage_spinlock(page); |
Qu Wenruo | 32443de | 2021-01-26 16:34:00 +0800 | [diff] [blame] | 8496 | clear_page_extent_mapped(page); |
Qu Wenruo | 7c11d0a | 2021-07-26 14:35:03 +0800 | [diff] [blame] | 8497 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8498 | return ret; |
| 8499 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8500 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8501 | static int btrfs_releasepage(struct page *page, gfp_t gfp_flags) |
| 8502 | { |
Chris Mason | 98509cf | 2008-09-11 15:51:43 -0400 | [diff] [blame] | 8503 | if (PageWriteback(page) || PageDirty(page)) |
| 8504 | return 0; |
Michal Hocko | 3ba7ab2 | 2017-01-09 15:39:02 +0100 | [diff] [blame] | 8505 | return __btrfs_releasepage(page, gfp_flags); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8506 | } |
| 8507 | |
Roman Gushchin | f8e6608 | 2020-03-04 16:57:35 -0800 | [diff] [blame] | 8508 | #ifdef CONFIG_MIGRATION |
| 8509 | static int btrfs_migratepage(struct address_space *mapping, |
| 8510 | struct page *newpage, struct page *page, |
| 8511 | enum migrate_mode mode) |
| 8512 | { |
| 8513 | int ret; |
| 8514 | |
| 8515 | ret = migrate_page_move_mapping(mapping, newpage, page, 0); |
| 8516 | if (ret != MIGRATEPAGE_SUCCESS) |
| 8517 | return ret; |
| 8518 | |
Guoqing Jiang | d1b89bc | 2020-06-01 21:47:45 -0700 | [diff] [blame] | 8519 | if (page_has_private(page)) |
| 8520 | attach_page_private(newpage, detach_page_private(page)); |
Roman Gushchin | f8e6608 | 2020-03-04 16:57:35 -0800 | [diff] [blame] | 8521 | |
Qu Wenruo | f57ad93 | 2021-04-07 19:22:13 +0800 | [diff] [blame] | 8522 | if (PageOrdered(page)) { |
| 8523 | ClearPageOrdered(page); |
| 8524 | SetPageOrdered(newpage); |
Roman Gushchin | f8e6608 | 2020-03-04 16:57:35 -0800 | [diff] [blame] | 8525 | } |
| 8526 | |
| 8527 | if (mode != MIGRATE_SYNC_NO_COPY) |
| 8528 | migrate_page_copy(newpage, page); |
| 8529 | else |
| 8530 | migrate_page_states(newpage, page); |
| 8531 | return MIGRATEPAGE_SUCCESS; |
| 8532 | } |
| 8533 | #endif |
| 8534 | |
Lukas Czerner | d47992f | 2013-05-21 23:17:23 -0400 | [diff] [blame] | 8535 | static void btrfs_invalidatepage(struct page *page, unsigned int offset, |
| 8536 | unsigned int length) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8537 | { |
Nikolay Borisov | 53ac7ea | 2020-08-31 14:42:43 +0300 | [diff] [blame] | 8538 | struct btrfs_inode *inode = BTRFS_I(page->mapping->host); |
Qu Wenruo | b945a46 | 2021-05-31 16:50:46 +0800 | [diff] [blame] | 8539 | struct btrfs_fs_info *fs_info = inode->root->fs_info; |
Nikolay Borisov | 53ac7ea | 2020-08-31 14:42:43 +0300 | [diff] [blame] | 8540 | struct extent_io_tree *tree = &inode->io_tree; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 8541 | struct extent_state *cached_state = NULL; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8542 | u64 page_start = page_offset(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 8543 | u64 page_end = page_start + PAGE_SIZE - 1; |
Qu Wenruo | 3b83584 | 2021-04-06 19:54:53 +0800 | [diff] [blame] | 8544 | u64 cur; |
Nikolay Borisov | 53ac7ea | 2020-08-31 14:42:43 +0300 | [diff] [blame] | 8545 | int inode_evicting = inode->vfs_inode.i_state & I_FREEING; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8546 | |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 8547 | /* |
Qu Wenruo | 266a258 | 2021-04-06 08:27:18 +0800 | [diff] [blame] | 8548 | * We have page locked so no new ordered extent can be created on this |
| 8549 | * page, nor bio can be submitted for this page. |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 8550 | * |
Qu Wenruo | 266a258 | 2021-04-06 08:27:18 +0800 | [diff] [blame] | 8551 | * But already submitted bio can still be finished on this page. |
Qu Wenruo | f57ad93 | 2021-04-07 19:22:13 +0800 | [diff] [blame] | 8552 | * Furthermore, endio function won't skip page which has Ordered |
| 8553 | * (Private2) already cleared, so it's possible for endio and |
| 8554 | * invalidatepage to do the same ordered extent accounting twice |
| 8555 | * on one page. |
Qu Wenruo | 266a258 | 2021-04-06 08:27:18 +0800 | [diff] [blame] | 8556 | * |
| 8557 | * So here we wait for any submitted bios to finish, so that we won't |
| 8558 | * do double ordered extent accounting on the same page. |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 8559 | */ |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8560 | wait_on_page_writeback(page); |
Qu Wenruo | 7c11d0a | 2021-07-26 14:35:03 +0800 | [diff] [blame] | 8561 | wait_subpage_spinlock(page); |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 8562 | |
Qu Wenruo | bcd7745 | 2021-05-31 16:50:55 +0800 | [diff] [blame] | 8563 | /* |
| 8564 | * For subpage case, we have call sites like |
| 8565 | * btrfs_punch_hole_lock_range() which passes range not aligned to |
| 8566 | * sectorsize. |
| 8567 | * If the range doesn't cover the full page, we don't need to and |
| 8568 | * shouldn't clear page extent mapped, as page->private can still |
| 8569 | * record subpage dirty bits for other part of the range. |
| 8570 | * |
| 8571 | * For cases that can invalidate the full even the range doesn't |
| 8572 | * cover the full page, like invalidating the last page, we're |
| 8573 | * still safe to wait for ordered extent to finish. |
| 8574 | */ |
| 8575 | if (!(offset == 0 && length == PAGE_SIZE)) { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8576 | btrfs_releasepage(page, GFP_NOFS); |
| 8577 | return; |
| 8578 | } |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 8579 | |
| 8580 | if (!inode_evicting) |
David Sterba | ff13db4 | 2015-12-03 14:30:40 +0100 | [diff] [blame] | 8581 | lock_extent_bits(tree, page_start, page_end, &cached_state); |
Qu Wenruo | 951c80f | 2021-01-27 14:38:48 +0800 | [diff] [blame] | 8582 | |
Qu Wenruo | 3b83584 | 2021-04-06 19:54:53 +0800 | [diff] [blame] | 8583 | cur = page_start; |
| 8584 | while (cur < page_end) { |
| 8585 | struct btrfs_ordered_extent *ordered; |
| 8586 | bool delete_states; |
| 8587 | u64 range_end; |
Qu Wenruo | b945a46 | 2021-05-31 16:50:46 +0800 | [diff] [blame] | 8588 | u32 range_len; |
Qu Wenruo | 3b83584 | 2021-04-06 19:54:53 +0800 | [diff] [blame] | 8589 | |
| 8590 | ordered = btrfs_lookup_first_ordered_range(inode, cur, |
| 8591 | page_end + 1 - cur); |
| 8592 | if (!ordered) { |
| 8593 | range_end = page_end; |
| 8594 | /* |
| 8595 | * No ordered extent covering this range, we are safe |
| 8596 | * to delete all extent states in the range. |
| 8597 | */ |
| 8598 | delete_states = true; |
| 8599 | goto next; |
| 8600 | } |
| 8601 | if (ordered->file_offset > cur) { |
| 8602 | /* |
| 8603 | * There is a range between [cur, oe->file_offset) not |
| 8604 | * covered by any ordered extent. |
| 8605 | * We are safe to delete all extent states, and handle |
| 8606 | * the ordered extent in the next iteration. |
| 8607 | */ |
| 8608 | range_end = ordered->file_offset - 1; |
| 8609 | delete_states = true; |
| 8610 | goto next; |
| 8611 | } |
| 8612 | |
| 8613 | range_end = min(ordered->file_offset + ordered->num_bytes - 1, |
| 8614 | page_end); |
Qu Wenruo | b945a46 | 2021-05-31 16:50:46 +0800 | [diff] [blame] | 8615 | ASSERT(range_end + 1 - cur < U32_MAX); |
| 8616 | range_len = range_end + 1 - cur; |
| 8617 | if (!btrfs_page_test_ordered(fs_info, page, cur, range_len)) { |
Qu Wenruo | 3b83584 | 2021-04-06 19:54:53 +0800 | [diff] [blame] | 8618 | /* |
Qu Wenruo | f57ad93 | 2021-04-07 19:22:13 +0800 | [diff] [blame] | 8619 | * If Ordered (Private2) is cleared, it means endio has |
| 8620 | * already been executed for the range. |
Qu Wenruo | 3b83584 | 2021-04-06 19:54:53 +0800 | [diff] [blame] | 8621 | * We can't delete the extent states as |
| 8622 | * btrfs_finish_ordered_io() may still use some of them. |
| 8623 | */ |
| 8624 | delete_states = false; |
| 8625 | goto next; |
| 8626 | } |
Qu Wenruo | b945a46 | 2021-05-31 16:50:46 +0800 | [diff] [blame] | 8627 | btrfs_page_clear_ordered(fs_info, page, cur, range_len); |
Qu Wenruo | 3b83584 | 2021-04-06 19:54:53 +0800 | [diff] [blame] | 8628 | |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 8629 | /* |
Filipe Manana | 2766ff6 | 2020-11-04 11:07:34 +0000 | [diff] [blame] | 8630 | * IO on this page will never be started, so we need to account |
| 8631 | * for any ordered extents now. Don't clear EXTENT_DELALLOC_NEW |
| 8632 | * here, must leave that up for the ordered extent completion. |
Qu Wenruo | 3b83584 | 2021-04-06 19:54:53 +0800 | [diff] [blame] | 8633 | * |
| 8634 | * This will also unlock the range for incoming |
| 8635 | * btrfs_finish_ordered_io(). |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 8636 | */ |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 8637 | if (!inode_evicting) |
Qu Wenruo | 3b83584 | 2021-04-06 19:54:53 +0800 | [diff] [blame] | 8638 | clear_extent_bit(tree, cur, range_end, |
Filipe Manana | 2766ff6 | 2020-11-04 11:07:34 +0000 | [diff] [blame] | 8639 | EXTENT_DELALLOC | |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 8640 | EXTENT_LOCKED | EXTENT_DO_ACCOUNTING | |
David Sterba | ae0f162 | 2017-10-31 16:37:52 +0100 | [diff] [blame] | 8641 | EXTENT_DEFRAG, 1, 0, &cached_state); |
Qu Wenruo | 3b83584 | 2021-04-06 19:54:53 +0800 | [diff] [blame] | 8642 | |
| 8643 | spin_lock_irq(&inode->ordered_tree.lock); |
| 8644 | set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags); |
| 8645 | ordered->truncated_len = min(ordered->truncated_len, |
| 8646 | cur - ordered->file_offset); |
| 8647 | spin_unlock_irq(&inode->ordered_tree.lock); |
| 8648 | |
| 8649 | if (btrfs_dec_test_ordered_pending(inode, &ordered, |
David Sterba | f41b6ba | 2021-07-26 14:15:10 +0200 | [diff] [blame] | 8650 | cur, range_end + 1 - cur)) { |
Qu Wenruo | 3b83584 | 2021-04-06 19:54:53 +0800 | [diff] [blame] | 8651 | btrfs_finish_ordered_io(ordered); |
| 8652 | /* |
| 8653 | * The ordered extent has finished, now we're again |
| 8654 | * safe to delete all extent states of the range. |
| 8655 | */ |
| 8656 | delete_states = true; |
| 8657 | } else { |
| 8658 | /* |
| 8659 | * btrfs_finish_ordered_io() will get executed by endio |
| 8660 | * of other pages, thus we can't delete extent states |
| 8661 | * anymore |
| 8662 | */ |
| 8663 | delete_states = false; |
| 8664 | } |
| 8665 | next: |
| 8666 | if (ordered) |
| 8667 | btrfs_put_ordered_extent(ordered); |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 8668 | /* |
Qu Wenruo | 3b83584 | 2021-04-06 19:54:53 +0800 | [diff] [blame] | 8669 | * Qgroup reserved space handler |
| 8670 | * Sector(s) here will be either: |
Qu Wenruo | 266a258 | 2021-04-06 08:27:18 +0800 | [diff] [blame] | 8671 | * |
Qu Wenruo | 3b83584 | 2021-04-06 19:54:53 +0800 | [diff] [blame] | 8672 | * 1) Already written to disk or bio already finished |
| 8673 | * Then its QGROUP_RESERVED bit in io_tree is already cleared. |
| 8674 | * Qgroup will be handled by its qgroup_record then. |
| 8675 | * btrfs_qgroup_free_data() call will do nothing here. |
| 8676 | * |
| 8677 | * 2) Not written to disk yet |
| 8678 | * Then btrfs_qgroup_free_data() call will clear the |
| 8679 | * QGROUP_RESERVED bit of its io_tree, and free the qgroup |
| 8680 | * reserved data space. |
| 8681 | * Since the IO will never happen for this page. |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 8682 | */ |
Qu Wenruo | 3b83584 | 2021-04-06 19:54:53 +0800 | [diff] [blame] | 8683 | btrfs_qgroup_free_data(inode, NULL, cur, range_end + 1 - cur); |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 8684 | if (!inode_evicting) { |
Qu Wenruo | 3b83584 | 2021-04-06 19:54:53 +0800 | [diff] [blame] | 8685 | clear_extent_bit(tree, cur, range_end, EXTENT_LOCKED | |
Filipe Manana | 2766ff6 | 2020-11-04 11:07:34 +0000 | [diff] [blame] | 8686 | EXTENT_DELALLOC | EXTENT_UPTODATE | |
| 8687 | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, |
Qu Wenruo | 3b83584 | 2021-04-06 19:54:53 +0800 | [diff] [blame] | 8688 | delete_states, &cached_state); |
| 8689 | } |
| 8690 | cur = range_end + 1; |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 8691 | } |
Qu Wenruo | 3b83584 | 2021-04-06 19:54:53 +0800 | [diff] [blame] | 8692 | /* |
| 8693 | * We have iterated through all ordered extents of the page, the page |
Qu Wenruo | f57ad93 | 2021-04-07 19:22:13 +0800 | [diff] [blame] | 8694 | * should not have Ordered (Private2) anymore, or the above iteration |
| 8695 | * did something wrong. |
Qu Wenruo | 3b83584 | 2021-04-06 19:54:53 +0800 | [diff] [blame] | 8696 | */ |
Qu Wenruo | f57ad93 | 2021-04-07 19:22:13 +0800 | [diff] [blame] | 8697 | ASSERT(!PageOrdered(page)); |
Qu Wenruo | 3b83584 | 2021-04-06 19:54:53 +0800 | [diff] [blame] | 8698 | if (!inode_evicting) |
| 8699 | __btrfs_releasepage(page, GFP_NOFS); |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 8700 | ClearPageChecked(page); |
Qu Wenruo | 32443de | 2021-01-26 16:34:00 +0800 | [diff] [blame] | 8701 | clear_page_extent_mapped(page); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8702 | } |
| 8703 | |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8704 | /* |
| 8705 | * btrfs_page_mkwrite() is not allowed to change the file size as it gets |
| 8706 | * called from a page fault handler when a page is first dirtied. Hence we must |
| 8707 | * be careful to check for EOF conditions here. We set the page up correctly |
| 8708 | * for a written page which means we get ENOSPC checking when writing into |
| 8709 | * holes and correct delalloc and unwritten extent mapping on filesystems that |
| 8710 | * support these features. |
| 8711 | * |
| 8712 | * We are not allowed to take the i_mutex here so we have to play games to |
| 8713 | * protect against truncate races as the page could now be beyond EOF. Because |
Omar Sandoval | d1342aa | 2018-05-11 13:13:29 -0700 | [diff] [blame] | 8714 | * truncate_setsize() writes the inode size before removing pages, once we have |
| 8715 | * the page lock we can determine safely if the page is beyond EOF. If it is not |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8716 | * beyond EOF, then the page is guaranteed safe against truncation until we |
| 8717 | * unlock the page. |
| 8718 | */ |
Souptick Joarder | a528a24 | 2018-06-06 19:54:44 +0530 | [diff] [blame] | 8719 | vm_fault_t btrfs_page_mkwrite(struct vm_fault *vmf) |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8720 | { |
Nick Piggin | c2ec175 | 2009-03-31 15:23:21 -0700 | [diff] [blame] | 8721 | struct page *page = vmf->page; |
Dave Jiang | 11bac80 | 2017-02-24 14:56:41 -0800 | [diff] [blame] | 8722 | struct inode *inode = file_inode(vmf->vma->vm_file); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8723 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8724 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
| 8725 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 8726 | struct extent_state *cached_state = NULL; |
Qu Wenruo | 364ecf3 | 2017-02-27 15:10:38 +0800 | [diff] [blame] | 8727 | struct extent_changeset *data_reserved = NULL; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8728 | unsigned long zero_start; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8729 | loff_t size; |
Souptick Joarder | a528a24 | 2018-06-06 19:54:44 +0530 | [diff] [blame] | 8730 | vm_fault_t ret; |
| 8731 | int ret2; |
Chris Mason | 9998eb7 | 2012-01-25 13:47:40 -0500 | [diff] [blame] | 8732 | int reserved = 0; |
Chandan Rajendra | d0b7da88 | 2016-01-21 15:55:57 +0530 | [diff] [blame] | 8733 | u64 reserved_space; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8734 | u64 page_start; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8735 | u64 page_end; |
Chandan Rajendra | d0b7da88 | 2016-01-21 15:55:57 +0530 | [diff] [blame] | 8736 | u64 end; |
| 8737 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 8738 | reserved_space = PAGE_SIZE; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8739 | |
Jan Kara | b2b5ef5 | 2012-06-12 16:20:45 +0200 | [diff] [blame] | 8740 | sb_start_pagefault(inode->i_sb); |
Qu Wenruo | df48063 | 2015-09-08 17:25:54 +0800 | [diff] [blame] | 8741 | page_start = page_offset(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 8742 | page_end = page_start + PAGE_SIZE - 1; |
Chandan Rajendra | d0b7da88 | 2016-01-21 15:55:57 +0530 | [diff] [blame] | 8743 | end = page_end; |
Qu Wenruo | df48063 | 2015-09-08 17:25:54 +0800 | [diff] [blame] | 8744 | |
Chandan Rajendra | d0b7da88 | 2016-01-21 15:55:57 +0530 | [diff] [blame] | 8745 | /* |
| 8746 | * Reserving delalloc space after obtaining the page lock can lead to |
| 8747 | * deadlock. For example, if a dirty page is locked by this function |
| 8748 | * and the call to btrfs_delalloc_reserve_space() ends up triggering |
| 8749 | * dirty page write out, then the btrfs_writepage() function could |
| 8750 | * end up waiting indefinitely to get a lock on the page currently |
| 8751 | * being processed by btrfs_page_mkwrite() function. |
| 8752 | */ |
Nikolay Borisov | e5b7231e | 2020-06-03 08:55:42 +0300 | [diff] [blame] | 8753 | ret2 = btrfs_delalloc_reserve_space(BTRFS_I(inode), &data_reserved, |
| 8754 | page_start, reserved_space); |
Souptick Joarder | a528a24 | 2018-06-06 19:54:44 +0530 | [diff] [blame] | 8755 | if (!ret2) { |
| 8756 | ret2 = file_update_time(vmf->vma->vm_file); |
Chris Mason | 9998eb7 | 2012-01-25 13:47:40 -0500 | [diff] [blame] | 8757 | reserved = 1; |
| 8758 | } |
Souptick Joarder | a528a24 | 2018-06-06 19:54:44 +0530 | [diff] [blame] | 8759 | if (ret2) { |
| 8760 | ret = vmf_error(ret2); |
Chris Mason | 9998eb7 | 2012-01-25 13:47:40 -0500 | [diff] [blame] | 8761 | if (reserved) |
| 8762 | goto out; |
| 8763 | goto out_noreserve; |
Nick Piggin | 56a76f8 | 2009-03-31 15:23:23 -0700 | [diff] [blame] | 8764 | } |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 8765 | |
Nick Piggin | 56a76f8 | 2009-03-31 15:23:23 -0700 | [diff] [blame] | 8766 | ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */ |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8767 | again: |
Josef Bacik | 8318ba7 | 2021-02-10 17:14:33 -0500 | [diff] [blame] | 8768 | down_read(&BTRFS_I(inode)->i_mmap_lock); |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8769 | lock_page(page); |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8770 | size = i_size_read(inode); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8771 | |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8772 | if ((page->mapping != inode->i_mapping) || |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8773 | (page_start >= size)) { |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8774 | /* page got truncated out from underneath us */ |
| 8775 | goto out_unlock; |
| 8776 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8777 | wait_on_page_writeback(page); |
| 8778 | |
David Sterba | ff13db4 | 2015-12-03 14:30:40 +0100 | [diff] [blame] | 8779 | lock_extent_bits(io_tree, page_start, page_end, &cached_state); |
Qu Wenruo | 32443de | 2021-01-26 16:34:00 +0800 | [diff] [blame] | 8780 | ret2 = set_page_extent_mapped(page); |
| 8781 | if (ret2 < 0) { |
| 8782 | ret = vmf_error(ret2); |
| 8783 | unlock_extent_cached(io_tree, page_start, page_end, &cached_state); |
| 8784 | goto out_unlock; |
| 8785 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8786 | |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 8787 | /* |
| 8788 | * we can't set the delalloc bits if there are pending ordered |
| 8789 | * extents. Drop our locks and wait for them to finish |
| 8790 | */ |
Nikolay Borisov | a776c6f | 2017-02-20 13:50:49 +0200 | [diff] [blame] | 8791 | ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start, |
| 8792 | PAGE_SIZE); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8793 | if (ordered) { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 8794 | unlock_extent_cached(io_tree, page_start, page_end, |
David Sterba | e43bbe5 | 2017-12-12 21:43:52 +0100 | [diff] [blame] | 8795 | &cached_state); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8796 | unlock_page(page); |
Josef Bacik | 8318ba7 | 2021-02-10 17:14:33 -0500 | [diff] [blame] | 8797 | up_read(&BTRFS_I(inode)->i_mmap_lock); |
Nikolay Borisov | c0a4360 | 2020-09-18 12:15:53 +0300 | [diff] [blame] | 8798 | btrfs_start_ordered_extent(ordered, 1); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8799 | btrfs_put_ordered_extent(ordered); |
| 8800 | goto again; |
| 8801 | } |
| 8802 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 8803 | if (page->index == ((size - 1) >> PAGE_SHIFT)) { |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 8804 | reserved_space = round_up(size - page_start, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8805 | fs_info->sectorsize); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 8806 | if (reserved_space < PAGE_SIZE) { |
Chandan Rajendra | d0b7da88 | 2016-01-21 15:55:57 +0530 | [diff] [blame] | 8807 | end = page_start + reserved_space - 1; |
Nikolay Borisov | 86d5292 | 2020-06-03 08:55:40 +0300 | [diff] [blame] | 8808 | btrfs_delalloc_release_space(BTRFS_I(inode), |
| 8809 | data_reserved, page_start, |
| 8810 | PAGE_SIZE - reserved_space, true); |
Chandan Rajendra | d0b7da88 | 2016-01-21 15:55:57 +0530 | [diff] [blame] | 8811 | } |
| 8812 | } |
| 8813 | |
Josef Bacik | fbf1908 | 2009-10-01 17:10:23 -0400 | [diff] [blame] | 8814 | /* |
Liu Bo | 5416034 | 2016-12-13 12:15:19 -0800 | [diff] [blame] | 8815 | * page_mkwrite gets called when the page is firstly dirtied after it's |
| 8816 | * faulted in, but write(2) could also dirty a page and set delalloc |
| 8817 | * bits, thus in this case for space account reason, we still need to |
| 8818 | * clear any delalloc bits within this page range since we have to |
| 8819 | * reserve data&meta space before lock_page() (see above comments). |
Josef Bacik | fbf1908 | 2009-10-01 17:10:23 -0400 | [diff] [blame] | 8820 | */ |
Chandan Rajendra | d0b7da88 | 2016-01-21 15:55:57 +0530 | [diff] [blame] | 8821 | clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, end, |
Omar Sandoval | e182163 | 2019-08-15 14:04:04 -0700 | [diff] [blame] | 8822 | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING | |
| 8823 | EXTENT_DEFRAG, 0, 0, &cached_state); |
Josef Bacik | fbf1908 | 2009-10-01 17:10:23 -0400 | [diff] [blame] | 8824 | |
Nikolay Borisov | c2566f2 | 2020-06-03 08:55:35 +0300 | [diff] [blame] | 8825 | ret2 = btrfs_set_extent_delalloc(BTRFS_I(inode), page_start, end, 0, |
Nikolay Borisov | 330a582 | 2019-07-17 16:18:17 +0300 | [diff] [blame] | 8826 | &cached_state); |
Souptick Joarder | a528a24 | 2018-06-06 19:54:44 +0530 | [diff] [blame] | 8827 | if (ret2) { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 8828 | unlock_extent_cached(io_tree, page_start, page_end, |
David Sterba | e43bbe5 | 2017-12-12 21:43:52 +0100 | [diff] [blame] | 8829 | &cached_state); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 8830 | ret = VM_FAULT_SIGBUS; |
| 8831 | goto out_unlock; |
| 8832 | } |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8833 | |
| 8834 | /* page is wholly or partially inside EOF */ |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 8835 | if (page_start + PAGE_SIZE > size) |
Johannes Thumshirn | 7073017 | 2018-12-05 15:23:03 +0100 | [diff] [blame] | 8836 | zero_start = offset_in_page(size); |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8837 | else |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 8838 | zero_start = PAGE_SIZE; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8839 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 8840 | if (zero_start != PAGE_SIZE) { |
Ira Weiny | d048b9c | 2021-05-04 18:40:07 -0700 | [diff] [blame] | 8841 | memzero_page(page, zero_start, PAGE_SIZE - zero_start); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8842 | flush_dcache_page(page); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8843 | } |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 8844 | ClearPageChecked(page); |
Qu Wenruo | 2d8ec40 | 2021-05-31 16:50:52 +0800 | [diff] [blame] | 8845 | btrfs_page_set_dirty(fs_info, page, page_start, end + 1 - page_start); |
| 8846 | btrfs_page_set_uptodate(fs_info, page, page_start, end + 1 - page_start); |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 8847 | |
Filipe Manana | bc0939f | 2021-02-23 12:08:48 +0000 | [diff] [blame] | 8848 | btrfs_set_inode_last_sub_trans(BTRFS_I(inode)); |
Chris Mason | 257c62e | 2009-10-13 13:21:08 -0400 | [diff] [blame] | 8849 | |
David Sterba | e43bbe5 | 2017-12-12 21:43:52 +0100 | [diff] [blame] | 8850 | unlock_extent_cached(io_tree, page_start, page_end, &cached_state); |
Josef Bacik | 8318ba7 | 2021-02-10 17:14:33 -0500 | [diff] [blame] | 8851 | up_read(&BTRFS_I(inode)->i_mmap_lock); |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8852 | |
Yunfeng Ye | 76de60e | 2019-12-03 16:59:25 +0800 | [diff] [blame] | 8853 | btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE); |
| 8854 | sb_end_pagefault(inode->i_sb); |
| 8855 | extent_changeset_free(data_reserved); |
| 8856 | return VM_FAULT_LOCKED; |
Chris Mason | 717beb9 | 2018-06-25 10:03:41 -0700 | [diff] [blame] | 8857 | |
| 8858 | out_unlock: |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8859 | unlock_page(page); |
Josef Bacik | 8318ba7 | 2021-02-10 17:14:33 -0500 | [diff] [blame] | 8860 | up_read(&BTRFS_I(inode)->i_mmap_lock); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 8861 | out: |
Qu Wenruo | 8702ba9 | 2019-10-14 14:34:51 +0800 | [diff] [blame] | 8862 | btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE); |
Nikolay Borisov | 86d5292 | 2020-06-03 08:55:40 +0300 | [diff] [blame] | 8863 | btrfs_delalloc_release_space(BTRFS_I(inode), data_reserved, page_start, |
Qu Wenruo | 43b1859 | 2017-12-12 15:34:32 +0800 | [diff] [blame] | 8864 | reserved_space, (ret != 0)); |
Chris Mason | 9998eb7 | 2012-01-25 13:47:40 -0500 | [diff] [blame] | 8865 | out_noreserve: |
Jan Kara | b2b5ef5 | 2012-06-12 16:20:45 +0200 | [diff] [blame] | 8866 | sb_end_pagefault(inode->i_sb); |
Qu Wenruo | 364ecf3 | 2017-02-27 15:10:38 +0800 | [diff] [blame] | 8867 | extent_changeset_free(data_reserved); |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8868 | return ret; |
| 8869 | } |
| 8870 | |
Filipe Manana | 213e8c5 | 2018-02-06 20:40:31 +0000 | [diff] [blame] | 8871 | static int btrfs_truncate(struct inode *inode, bool skip_writeback) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8872 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8873 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8874 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 8875 | struct btrfs_block_rsv *rsv; |
Omar Sandoval | ad7e1a7 | 2018-05-22 09:59:50 -0700 | [diff] [blame] | 8876 | int ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8877 | struct btrfs_trans_handle *trans; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8878 | u64 mask = fs_info->sectorsize - 1; |
Josef Bacik | 2bd36e7 | 2019-08-22 15:14:33 -0400 | [diff] [blame] | 8879 | u64 min_size = btrfs_calc_metadata_size(fs_info, 1); |
Filipe Manana | 0d7d316 | 2021-05-24 11:35:55 +0100 | [diff] [blame] | 8880 | u64 extents_found = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8881 | |
Filipe Manana | 213e8c5 | 2018-02-06 20:40:31 +0000 | [diff] [blame] | 8882 | if (!skip_writeback) { |
| 8883 | ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask), |
| 8884 | (u64)-1); |
| 8885 | if (ret) |
| 8886 | return ret; |
| 8887 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8888 | |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 8889 | /* |
Omar Sandoval | f7e9e8f | 2018-05-11 13:13:32 -0700 | [diff] [blame] | 8890 | * Yes ladies and gentlemen, this is indeed ugly. We have a couple of |
| 8891 | * things going on here: |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 8892 | * |
Omar Sandoval | f7e9e8f | 2018-05-11 13:13:32 -0700 | [diff] [blame] | 8893 | * 1) We need to reserve space to update our inode. |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 8894 | * |
Omar Sandoval | f7e9e8f | 2018-05-11 13:13:32 -0700 | [diff] [blame] | 8895 | * 2) We need to have something to cache all the space that is going to |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 8896 | * be free'd up by the truncate operation, but also have some slack |
| 8897 | * space reserved in case it uses space during the truncate (thank you |
| 8898 | * very much snapshotting). |
| 8899 | * |
Omar Sandoval | f7e9e8f | 2018-05-11 13:13:32 -0700 | [diff] [blame] | 8900 | * And we need these to be separate. The fact is we can use a lot of |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 8901 | * space doing the truncate, and we have no earthly idea how much space |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 8902 | * we will use, so we need the truncate reservation to be separate so it |
Omar Sandoval | f7e9e8f | 2018-05-11 13:13:32 -0700 | [diff] [blame] | 8903 | * doesn't end up using space reserved for updating the inode. We also |
| 8904 | * need to be able to stop the transaction and start a new one, which |
| 8905 | * means we need to be able to update the inode several times, and we |
| 8906 | * have no idea of knowing how many times that will be, so we can't just |
| 8907 | * reserve 1 item for the entirety of the operation, so that has to be |
| 8908 | * done separately as well. |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 8909 | * |
| 8910 | * So that leaves us with |
| 8911 | * |
Omar Sandoval | f7e9e8f | 2018-05-11 13:13:32 -0700 | [diff] [blame] | 8912 | * 1) rsv - for the truncate reservation, which we will steal from the |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 8913 | * transaction reservation. |
Omar Sandoval | f7e9e8f | 2018-05-11 13:13:32 -0700 | [diff] [blame] | 8914 | * 2) fs_info->trans_block_rsv - this will have 1 items worth left for |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 8915 | * updating the inode. |
| 8916 | */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 8917 | rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP); |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 8918 | if (!rsv) |
| 8919 | return -ENOMEM; |
Josef Bacik | 4a33854 | 2011-08-29 11:01:31 -0400 | [diff] [blame] | 8920 | rsv->size = min_size; |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 8921 | rsv->failfast = 1; |
Josef Bacik | f0cd846 | 2011-03-04 14:37:08 -0500 | [diff] [blame] | 8922 | |
Josef Bacik | 907cbce | 2011-08-08 13:46:15 -0400 | [diff] [blame] | 8923 | /* |
Josef Bacik | 0712718 | 2011-08-19 10:29:59 -0400 | [diff] [blame] | 8924 | * 1 for the truncate slack space |
Josef Bacik | 907cbce | 2011-08-08 13:46:15 -0400 | [diff] [blame] | 8925 | * 1 for updating the inode. |
| 8926 | */ |
Josef Bacik | f3fe820 | 2013-01-07 17:03:21 -0500 | [diff] [blame] | 8927 | trans = btrfs_start_transaction(root, 2); |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 8928 | if (IS_ERR(trans)) { |
Omar Sandoval | ad7e1a7 | 2018-05-22 09:59:50 -0700 | [diff] [blame] | 8929 | ret = PTR_ERR(trans); |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 8930 | goto out; |
| 8931 | } |
Josef Bacik | f0cd846 | 2011-03-04 14:37:08 -0500 | [diff] [blame] | 8932 | |
Josef Bacik | 907cbce | 2011-08-08 13:46:15 -0400 | [diff] [blame] | 8933 | /* Migrate the slack space for the truncate to our reserve */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8934 | ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, rsv, |
Lu Fengqi | 3a58417 | 2018-08-04 21:10:55 +0800 | [diff] [blame] | 8935 | min_size, false); |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 8936 | BUG_ON(ret); |
Josef Bacik | f0cd846 | 2011-03-04 14:37:08 -0500 | [diff] [blame] | 8937 | |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 8938 | trans->block_rsv = rsv; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 8939 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 8940 | while (1) { |
Nikolay Borisov | 5074339 | 2020-11-02 16:48:55 +0200 | [diff] [blame] | 8941 | ret = btrfs_truncate_inode_items(trans, root, BTRFS_I(inode), |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 8942 | inode->i_size, |
Filipe Manana | 0d7d316 | 2021-05-24 11:35:55 +0100 | [diff] [blame] | 8943 | BTRFS_EXTENT_DATA_KEY, |
| 8944 | &extents_found); |
Josef Bacik | ddfae63 | 2017-10-19 14:16:02 -0400 | [diff] [blame] | 8945 | trans->block_rsv = &fs_info->trans_block_rsv; |
Omar Sandoval | ad7e1a7 | 2018-05-22 09:59:50 -0700 | [diff] [blame] | 8946 | if (ret != -ENOSPC && ret != -EAGAIN) |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 8947 | break; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8948 | |
Nikolay Borisov | 9a56fcd | 2020-11-02 16:48:59 +0200 | [diff] [blame] | 8949 | ret = btrfs_update_inode(trans, root, BTRFS_I(inode)); |
Omar Sandoval | ad7e1a7 | 2018-05-22 09:59:50 -0700 | [diff] [blame] | 8950 | if (ret) |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 8951 | break; |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 8952 | |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 8953 | btrfs_end_transaction(trans); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 8954 | btrfs_btree_balance_dirty(fs_info); |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 8955 | |
| 8956 | trans = btrfs_start_transaction(root, 2); |
| 8957 | if (IS_ERR(trans)) { |
Omar Sandoval | ad7e1a7 | 2018-05-22 09:59:50 -0700 | [diff] [blame] | 8958 | ret = PTR_ERR(trans); |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 8959 | trans = NULL; |
| 8960 | break; |
| 8961 | } |
| 8962 | |
Nikolay Borisov | 63f018b | 2020-03-10 10:59:31 +0200 | [diff] [blame] | 8963 | btrfs_block_rsv_release(fs_info, rsv, -1, NULL); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8964 | ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, |
Lu Fengqi | 3a58417 | 2018-08-04 21:10:55 +0800 | [diff] [blame] | 8965 | rsv, min_size, false); |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 8966 | BUG_ON(ret); /* shouldn't happen */ |
| 8967 | trans->block_rsv = rsv; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 8968 | } |
| 8969 | |
Josef Bacik | ddfae63 | 2017-10-19 14:16:02 -0400 | [diff] [blame] | 8970 | /* |
| 8971 | * We can't call btrfs_truncate_block inside a trans handle as we could |
| 8972 | * deadlock with freeze, if we got NEED_TRUNCATE_BLOCK then we know |
| 8973 | * we've truncated everything except the last little bit, and can do |
| 8974 | * btrfs_truncate_block and then update the disk_i_size. |
| 8975 | */ |
| 8976 | if (ret == NEED_TRUNCATE_BLOCK) { |
| 8977 | btrfs_end_transaction(trans); |
| 8978 | btrfs_btree_balance_dirty(fs_info); |
| 8979 | |
Nikolay Borisov | 217f42e | 2020-11-02 16:49:03 +0200 | [diff] [blame] | 8980 | ret = btrfs_truncate_block(BTRFS_I(inode), inode->i_size, 0, 0); |
Josef Bacik | ddfae63 | 2017-10-19 14:16:02 -0400 | [diff] [blame] | 8981 | if (ret) |
| 8982 | goto out; |
| 8983 | trans = btrfs_start_transaction(root, 1); |
| 8984 | if (IS_ERR(trans)) { |
| 8985 | ret = PTR_ERR(trans); |
| 8986 | goto out; |
| 8987 | } |
Nikolay Borisov | 76aea53 | 2020-11-02 16:48:53 +0200 | [diff] [blame] | 8988 | btrfs_inode_safe_disk_i_size_write(BTRFS_I(inode), 0); |
Josef Bacik | ddfae63 | 2017-10-19 14:16:02 -0400 | [diff] [blame] | 8989 | } |
| 8990 | |
Chris Mason | 917c16b | 2011-11-08 14:49:59 -0500 | [diff] [blame] | 8991 | if (trans) { |
Omar Sandoval | ad7e1a7 | 2018-05-22 09:59:50 -0700 | [diff] [blame] | 8992 | int ret2; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 8993 | |
Omar Sandoval | ad7e1a7 | 2018-05-22 09:59:50 -0700 | [diff] [blame] | 8994 | trans->block_rsv = &fs_info->trans_block_rsv; |
Nikolay Borisov | 9a56fcd | 2020-11-02 16:48:59 +0200 | [diff] [blame] | 8995 | ret2 = btrfs_update_inode(trans, root, BTRFS_I(inode)); |
Omar Sandoval | ad7e1a7 | 2018-05-22 09:59:50 -0700 | [diff] [blame] | 8996 | if (ret2 && !ret) |
| 8997 | ret = ret2; |
| 8998 | |
| 8999 | ret2 = btrfs_end_transaction(trans); |
| 9000 | if (ret2 && !ret) |
| 9001 | ret = ret2; |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 9002 | btrfs_btree_balance_dirty(fs_info); |
Chris Mason | 917c16b | 2011-11-08 14:49:59 -0500 | [diff] [blame] | 9003 | } |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 9004 | out: |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 9005 | btrfs_free_block_rsv(fs_info, rsv); |
Filipe Manana | 0d7d316 | 2021-05-24 11:35:55 +0100 | [diff] [blame] | 9006 | /* |
| 9007 | * So if we truncate and then write and fsync we normally would just |
| 9008 | * write the extents that changed, which is a problem if we need to |
| 9009 | * first truncate that entire inode. So set this flag so we write out |
| 9010 | * all of the extents in the inode to the sync log so we're completely |
| 9011 | * safe. |
| 9012 | * |
| 9013 | * If no extents were dropped or trimmed we don't need to force the next |
| 9014 | * fsync to truncate all the inode's items from the log and re-log them |
| 9015 | * all. This means the truncate operation did not change the file size, |
| 9016 | * or changed it to a smaller size but there was only an implicit hole |
| 9017 | * between the old i_size and the new i_size, and there were no prealloc |
| 9018 | * extents beyond i_size to drop. |
| 9019 | */ |
| 9020 | if (extents_found > 0) |
| 9021 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags); |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 9022 | |
Omar Sandoval | ad7e1a7 | 2018-05-22 09:59:50 -0700 | [diff] [blame] | 9023 | return ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9024 | } |
| 9025 | |
Sven Wegener | 3b96362 | 2008-06-09 21:57:42 -0400 | [diff] [blame] | 9026 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 9027 | * create a new subvolume directory/inode (helper for the ioctl). |
| 9028 | */ |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 9029 | int btrfs_create_subvol_root(struct btrfs_trans_handle *trans, |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 9030 | struct btrfs_root *new_root, |
Nikolay Borisov | 6994802 | 2020-12-07 17:32:37 +0200 | [diff] [blame] | 9031 | struct btrfs_root *parent_root) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9032 | { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9033 | struct inode *inode; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 9034 | int err; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 9035 | u64 index = 0; |
Nikolay Borisov | 2312510 | 2020-12-07 17:32:36 +0200 | [diff] [blame] | 9036 | u64 ino; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9037 | |
Nikolay Borisov | 2312510 | 2020-12-07 17:32:36 +0200 | [diff] [blame] | 9038 | err = btrfs_get_free_objectid(new_root, &ino); |
| 9039 | if (err < 0) |
| 9040 | return err; |
| 9041 | |
Christian Brauner | b3b6f5b | 2021-07-27 12:48:41 +0200 | [diff] [blame] | 9042 | inode = btrfs_new_inode(trans, new_root, &init_user_ns, NULL, "..", 2, |
| 9043 | ino, ino, |
Florian Albrechtskirchinger | 12fc9d0 | 2012-02-10 22:15:54 +0100 | [diff] [blame] | 9044 | S_IFDIR | (~current_umask() & S_IRWXUGO), |
| 9045 | &index); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 9046 | if (IS_ERR(inode)) |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 9047 | return PTR_ERR(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9048 | inode->i_op = &btrfs_dir_inode_operations; |
| 9049 | inode->i_fop = &btrfs_dir_file_operations; |
| 9050 | |
Miklos Szeredi | bfe8684 | 2011-10-28 14:13:29 +0200 | [diff] [blame] | 9051 | set_nlink(inode, 1); |
Nikolay Borisov | 6ef06d2 | 2017-02-20 13:50:34 +0200 | [diff] [blame] | 9052 | btrfs_i_size_write(BTRFS_I(inode), 0); |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 9053 | unlock_new_inode(inode); |
Sven Wegener | 3b96362 | 2008-06-09 21:57:42 -0400 | [diff] [blame] | 9054 | |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 9055 | err = btrfs_subvol_inherit_props(trans, new_root, parent_root); |
| 9056 | if (err) |
| 9057 | btrfs_err(new_root->fs_info, |
David Sterba | 351fd35 | 2014-05-15 16:48:20 +0200 | [diff] [blame] | 9058 | "error inheriting subvolume %llu properties: %d", |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 9059 | new_root->root_key.objectid, err); |
| 9060 | |
Nikolay Borisov | 9a56fcd | 2020-11-02 16:48:59 +0200 | [diff] [blame] | 9061 | err = btrfs_update_inode(trans, new_root, BTRFS_I(inode)); |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 9062 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 9063 | iput(inode); |
Mark Fasheh | ce59897 | 2011-07-26 11:32:23 -0700 | [diff] [blame] | 9064 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9065 | } |
| 9066 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9067 | struct inode *btrfs_alloc_inode(struct super_block *sb) |
| 9068 | { |
Josef Bacik | 69fe2d7 | 2017-10-19 14:15:57 -0400 | [diff] [blame] | 9069 | struct btrfs_fs_info *fs_info = btrfs_sb(sb); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9070 | struct btrfs_inode *ei; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9071 | struct inode *inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9072 | |
David Sterba | 712e36c | 2017-10-31 17:08:27 +0100 | [diff] [blame] | 9073 | ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_KERNEL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9074 | if (!ei) |
| 9075 | return NULL; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9076 | |
| 9077 | ei->root = NULL; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9078 | ei->generation = 0; |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 9079 | ei->last_trans = 0; |
Chris Mason | 257c62e | 2009-10-13 13:21:08 -0400 | [diff] [blame] | 9080 | ei->last_sub_trans = 0; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 9081 | ei->logged_trans = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9082 | ei->delalloc_bytes = 0; |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 9083 | ei->new_delalloc_bytes = 0; |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 9084 | ei->defrag_bytes = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9085 | ei->disk_i_size = 0; |
| 9086 | ei->flags = 0; |
Boris Burkov | 77eea05 | 2021-06-30 13:01:48 -0700 | [diff] [blame] | 9087 | ei->ro_flags = 0; |
Josef Bacik | 7709cde | 2011-08-04 10:25:02 -0400 | [diff] [blame] | 9088 | ei->csum_bytes = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9089 | ei->index_cnt = (u64)-1; |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 9090 | ei->dir_index = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9091 | ei->last_unlink_trans = 0; |
Filipe Manana | 3ebac17 | 2020-07-15 12:30:43 +0100 | [diff] [blame] | 9092 | ei->last_reflink_trans = 0; |
Liu Bo | 46d8bc3 | 2012-08-29 01:07:55 -0600 | [diff] [blame] | 9093 | ei->last_log_commit = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9094 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 9095 | spin_lock_init(&ei->lock); |
| 9096 | ei->outstanding_extents = 0; |
Josef Bacik | 69fe2d7 | 2017-10-19 14:15:57 -0400 | [diff] [blame] | 9097 | if (sb->s_magic != BTRFS_TEST_MAGIC) |
| 9098 | btrfs_init_metadata_block_rsv(fs_info, &ei->block_rsv, |
| 9099 | BTRFS_BLOCK_RSV_DELALLOC); |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 9100 | ei->runtime_flags = 0; |
David Sterba | b52aa8c | 2017-07-17 19:17:20 +0200 | [diff] [blame] | 9101 | ei->prop_compress = BTRFS_COMPRESS_NONE; |
David Sterba | eec63c6 | 2017-07-17 19:41:31 +0200 | [diff] [blame] | 9102 | ei->defrag_compress = BTRFS_COMPRESS_NONE; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9103 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 9104 | ei->delayed_node = NULL; |
| 9105 | |
chandan r | 9cc97d6 | 2012-07-04 12:48:07 +0530 | [diff] [blame] | 9106 | ei->i_otime.tv_sec = 0; |
| 9107 | ei->i_otime.tv_nsec = 0; |
| 9108 | |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9109 | inode = &ei->vfs_inode; |
David Sterba | a8067e0 | 2011-04-21 00:34:43 +0200 | [diff] [blame] | 9110 | extent_map_tree_init(&ei->extent_tree); |
Qu Wenruo | 43eb5f2 | 2019-03-01 10:47:59 +0800 | [diff] [blame] | 9111 | extent_io_tree_init(fs_info, &ei->io_tree, IO_TREE_INODE_IO, inode); |
| 9112 | extent_io_tree_init(fs_info, &ei->io_failure_tree, |
| 9113 | IO_TREE_INODE_IO_FAILURE, inode); |
Josef Bacik | 41a2ee7 | 2020-01-17 09:02:21 -0500 | [diff] [blame] | 9114 | extent_io_tree_init(fs_info, &ei->file_extent_tree, |
| 9115 | IO_TREE_INODE_FILE_EXTENT, inode); |
David Sterba | 7b43973 | 2019-03-11 15:58:30 +0100 | [diff] [blame] | 9116 | ei->io_tree.track_uptodate = true; |
| 9117 | ei->io_failure_tree.track_uptodate = true; |
Josef Bacik | b812ce2 | 2012-11-16 13:56:32 -0500 | [diff] [blame] | 9118 | atomic_set(&ei->sync_writers, 0); |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9119 | mutex_init(&ei->log_mutex); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 9120 | btrfs_ordered_inode_tree_init(&ei->ordered_tree); |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9121 | INIT_LIST_HEAD(&ei->delalloc_inodes); |
David Sterba | 8089fe6 | 2015-11-19 14:15:51 +0100 | [diff] [blame] | 9122 | INIT_LIST_HEAD(&ei->delayed_iput); |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9123 | RB_CLEAR_NODE(&ei->rb_node); |
Josef Bacik | 8318ba7 | 2021-02-10 17:14:33 -0500 | [diff] [blame] | 9124 | init_rwsem(&ei->i_mmap_lock); |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9125 | |
| 9126 | return inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9127 | } |
| 9128 | |
Josef Bacik | aaedb55 | 2013-10-11 14:44:09 -0400 | [diff] [blame] | 9129 | #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS |
| 9130 | void btrfs_test_destroy_inode(struct inode *inode) |
| 9131 | { |
Nikolay Borisov | dcdbc05 | 2017-02-20 13:50:45 +0200 | [diff] [blame] | 9132 | btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0); |
Josef Bacik | aaedb55 | 2013-10-11 14:44:09 -0400 | [diff] [blame] | 9133 | kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode)); |
| 9134 | } |
| 9135 | #endif |
| 9136 | |
Al Viro | 26602ca | 2019-04-10 15:14:41 -0400 | [diff] [blame] | 9137 | void btrfs_free_inode(struct inode *inode) |
Nick Piggin | fa0d7e3d | 2011-01-07 17:49:49 +1100 | [diff] [blame] | 9138 | { |
Nick Piggin | fa0d7e3d | 2011-01-07 17:49:49 +1100 | [diff] [blame] | 9139 | kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode)); |
| 9140 | } |
| 9141 | |
Nikolay Borisov | 633cc81 | 2020-09-18 12:15:49 +0300 | [diff] [blame] | 9142 | void btrfs_destroy_inode(struct inode *vfs_inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9143 | { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 9144 | struct btrfs_ordered_extent *ordered; |
Nikolay Borisov | 633cc81 | 2020-09-18 12:15:49 +0300 | [diff] [blame] | 9145 | struct btrfs_inode *inode = BTRFS_I(vfs_inode); |
| 9146 | struct btrfs_root *root = inode->root; |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 9147 | |
Nikolay Borisov | 633cc81 | 2020-09-18 12:15:49 +0300 | [diff] [blame] | 9148 | WARN_ON(!hlist_empty(&vfs_inode->i_dentry)); |
| 9149 | WARN_ON(vfs_inode->i_data.nrpages); |
| 9150 | WARN_ON(inode->block_rsv.reserved); |
| 9151 | WARN_ON(inode->block_rsv.size); |
| 9152 | WARN_ON(inode->outstanding_extents); |
| 9153 | WARN_ON(inode->delalloc_bytes); |
| 9154 | WARN_ON(inode->new_delalloc_bytes); |
| 9155 | WARN_ON(inode->csum_bytes); |
| 9156 | WARN_ON(inode->defrag_bytes); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9157 | |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 9158 | /* |
Josef Bacik | a6dbd42 | 2009-11-11 15:53:34 -0500 | [diff] [blame] | 9159 | * This can happen where we create an inode, but somebody else also |
| 9160 | * created the same inode and we need to destroy the one we already |
| 9161 | * created. |
| 9162 | */ |
| 9163 | if (!root) |
Al Viro | 26602ca | 2019-04-10 15:14:41 -0400 | [diff] [blame] | 9164 | return; |
Josef Bacik | a6dbd42 | 2009-11-11 15:53:34 -0500 | [diff] [blame] | 9165 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 9166 | while (1) { |
Nikolay Borisov | 633cc81 | 2020-09-18 12:15:49 +0300 | [diff] [blame] | 9167 | ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 9168 | if (!ordered) |
| 9169 | break; |
| 9170 | else { |
Nikolay Borisov | 633cc81 | 2020-09-18 12:15:49 +0300 | [diff] [blame] | 9171 | btrfs_err(root->fs_info, |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 9172 | "found ordered extent %llu %llu on inode cleanup", |
Omar Sandoval | bffe633 | 2019-12-02 17:34:19 -0800 | [diff] [blame] | 9173 | ordered->file_offset, ordered->num_bytes); |
Nikolay Borisov | 71fe0a5 | 2020-09-18 12:15:50 +0300 | [diff] [blame] | 9174 | btrfs_remove_ordered_extent(inode, ordered); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 9175 | btrfs_put_ordered_extent(ordered); |
| 9176 | btrfs_put_ordered_extent(ordered); |
| 9177 | } |
| 9178 | } |
Nikolay Borisov | 633cc81 | 2020-09-18 12:15:49 +0300 | [diff] [blame] | 9179 | btrfs_qgroup_check_reserved_leak(inode); |
| 9180 | inode_tree_del(inode); |
| 9181 | btrfs_drop_extent_cache(inode, 0, (u64)-1, 0); |
| 9182 | btrfs_inode_clear_file_extent_range(inode, 0, (u64)-1); |
| 9183 | btrfs_put_root(inode->root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9184 | } |
| 9185 | |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 9186 | int btrfs_drop_inode(struct inode *inode) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 9187 | { |
| 9188 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 9189 | |
Naohiro Aota | 6379ef9 | 2013-06-06 09:56:34 +0000 | [diff] [blame] | 9190 | if (root == NULL) |
| 9191 | return 1; |
| 9192 | |
Liu Bo | fa6ac87 | 2013-02-20 14:10:23 +0000 | [diff] [blame] | 9193 | /* the snap/subvol tree is on deleting */ |
Stefan Behrens | 69e9c6c | 2013-09-05 16:58:43 +0200 | [diff] [blame] | 9194 | if (btrfs_root_refs(&root->root_item) == 0) |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 9195 | return 1; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 9196 | else |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 9197 | return generic_drop_inode(inode); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 9198 | } |
| 9199 | |
Sven Wegener | 0ee0fda | 2008-07-30 16:54:26 -0400 | [diff] [blame] | 9200 | static void init_once(void *foo) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9201 | { |
| 9202 | struct btrfs_inode *ei = (struct btrfs_inode *) foo; |
| 9203 | |
| 9204 | inode_init_once(&ei->vfs_inode); |
| 9205 | } |
| 9206 | |
David Sterba | e67c718 | 2018-02-19 17:24:18 +0100 | [diff] [blame] | 9207 | void __cold btrfs_destroy_cachep(void) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9208 | { |
Kirill A. Shutemov | 8c0a853 | 2012-09-26 11:33:07 +1000 | [diff] [blame] | 9209 | /* |
| 9210 | * Make sure all delayed rcu free inodes are flushed before we |
| 9211 | * destroy cache. |
| 9212 | */ |
| 9213 | rcu_barrier(); |
Kinglong Mee | 5598e90 | 2016-01-29 21:36:35 +0800 | [diff] [blame] | 9214 | kmem_cache_destroy(btrfs_inode_cachep); |
| 9215 | kmem_cache_destroy(btrfs_trans_handle_cachep); |
Kinglong Mee | 5598e90 | 2016-01-29 21:36:35 +0800 | [diff] [blame] | 9216 | kmem_cache_destroy(btrfs_path_cachep); |
| 9217 | kmem_cache_destroy(btrfs_free_space_cachep); |
Christophe Leroy | 3acd485 | 2019-08-21 15:05:55 +0000 | [diff] [blame] | 9218 | kmem_cache_destroy(btrfs_free_space_bitmap_cachep); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9219 | } |
| 9220 | |
Liu Bo | f5c29bd | 2017-11-02 17:21:50 -0600 | [diff] [blame] | 9221 | int __init btrfs_init_cachep(void) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9222 | { |
David Sterba | 837e197 | 2012-09-07 03:00:48 -0600 | [diff] [blame] | 9223 | btrfs_inode_cachep = kmem_cache_create("btrfs_inode", |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 9224 | sizeof(struct btrfs_inode), 0, |
Vladimir Davydov | 5d09705 | 2016-01-14 15:18:21 -0800 | [diff] [blame] | 9225 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT, |
| 9226 | init_once); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9227 | if (!btrfs_inode_cachep) |
| 9228 | goto fail; |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 9229 | |
David Sterba | 837e197 | 2012-09-07 03:00:48 -0600 | [diff] [blame] | 9230 | btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle", |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 9231 | sizeof(struct btrfs_trans_handle), 0, |
Nikolay Borisov | fba4b69 | 2016-06-23 21:17:08 +0300 | [diff] [blame] | 9232 | SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9233 | if (!btrfs_trans_handle_cachep) |
| 9234 | goto fail; |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 9235 | |
David Sterba | 837e197 | 2012-09-07 03:00:48 -0600 | [diff] [blame] | 9236 | btrfs_path_cachep = kmem_cache_create("btrfs_path", |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 9237 | sizeof(struct btrfs_path), 0, |
Nikolay Borisov | fba4b69 | 2016-06-23 21:17:08 +0300 | [diff] [blame] | 9238 | SLAB_MEM_SPREAD, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9239 | if (!btrfs_path_cachep) |
| 9240 | goto fail; |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 9241 | |
David Sterba | 837e197 | 2012-09-07 03:00:48 -0600 | [diff] [blame] | 9242 | btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space", |
Josef Bacik | dc89e98 | 2011-01-28 17:05:48 -0500 | [diff] [blame] | 9243 | sizeof(struct btrfs_free_space), 0, |
Nikolay Borisov | fba4b69 | 2016-06-23 21:17:08 +0300 | [diff] [blame] | 9244 | SLAB_MEM_SPREAD, NULL); |
Josef Bacik | dc89e98 | 2011-01-28 17:05:48 -0500 | [diff] [blame] | 9245 | if (!btrfs_free_space_cachep) |
| 9246 | goto fail; |
| 9247 | |
Christophe Leroy | 3acd485 | 2019-08-21 15:05:55 +0000 | [diff] [blame] | 9248 | btrfs_free_space_bitmap_cachep = kmem_cache_create("btrfs_free_space_bitmap", |
| 9249 | PAGE_SIZE, PAGE_SIZE, |
David Sterba | 34e4999 | 2021-03-15 15:18:24 +0100 | [diff] [blame] | 9250 | SLAB_MEM_SPREAD, NULL); |
Christophe Leroy | 3acd485 | 2019-08-21 15:05:55 +0000 | [diff] [blame] | 9251 | if (!btrfs_free_space_bitmap_cachep) |
| 9252 | goto fail; |
| 9253 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9254 | return 0; |
| 9255 | fail: |
| 9256 | btrfs_destroy_cachep(); |
| 9257 | return -ENOMEM; |
| 9258 | } |
| 9259 | |
Christian Brauner | 549c729 | 2021-01-21 14:19:43 +0100 | [diff] [blame] | 9260 | static int btrfs_getattr(struct user_namespace *mnt_userns, |
| 9261 | const struct path *path, struct kstat *stat, |
David Howells | a528d35 | 2017-01-31 16:46:22 +0000 | [diff] [blame] | 9262 | u32 request_mask, unsigned int flags) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9263 | { |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 9264 | u64 delalloc_bytes; |
Filipe Manana | 2766ff6 | 2020-11-04 11:07:34 +0000 | [diff] [blame] | 9265 | u64 inode_bytes; |
David Howells | a528d35 | 2017-01-31 16:46:22 +0000 | [diff] [blame] | 9266 | struct inode *inode = d_inode(path->dentry); |
David Sterba | fadc0d8 | 2011-11-20 07:33:38 -0500 | [diff] [blame] | 9267 | u32 blocksize = inode->i_sb->s_blocksize; |
Yonghong Song | 04a87e3 | 2017-05-12 15:07:43 -0700 | [diff] [blame] | 9268 | u32 bi_flags = BTRFS_I(inode)->flags; |
Boris Burkov | 1460540 | 2021-06-30 13:01:49 -0700 | [diff] [blame] | 9269 | u32 bi_ro_flags = BTRFS_I(inode)->ro_flags; |
Yonghong Song | 04a87e3 | 2017-05-12 15:07:43 -0700 | [diff] [blame] | 9270 | |
| 9271 | stat->result_mask |= STATX_BTIME; |
| 9272 | stat->btime.tv_sec = BTRFS_I(inode)->i_otime.tv_sec; |
| 9273 | stat->btime.tv_nsec = BTRFS_I(inode)->i_otime.tv_nsec; |
| 9274 | if (bi_flags & BTRFS_INODE_APPEND) |
| 9275 | stat->attributes |= STATX_ATTR_APPEND; |
| 9276 | if (bi_flags & BTRFS_INODE_COMPRESS) |
| 9277 | stat->attributes |= STATX_ATTR_COMPRESSED; |
| 9278 | if (bi_flags & BTRFS_INODE_IMMUTABLE) |
| 9279 | stat->attributes |= STATX_ATTR_IMMUTABLE; |
| 9280 | if (bi_flags & BTRFS_INODE_NODUMP) |
| 9281 | stat->attributes |= STATX_ATTR_NODUMP; |
Boris Burkov | 1460540 | 2021-06-30 13:01:49 -0700 | [diff] [blame] | 9282 | if (bi_ro_flags & BTRFS_INODE_RO_VERITY) |
| 9283 | stat->attributes |= STATX_ATTR_VERITY; |
Yonghong Song | 04a87e3 | 2017-05-12 15:07:43 -0700 | [diff] [blame] | 9284 | |
| 9285 | stat->attributes_mask |= (STATX_ATTR_APPEND | |
| 9286 | STATX_ATTR_COMPRESSED | |
| 9287 | STATX_ATTR_IMMUTABLE | |
| 9288 | STATX_ATTR_NODUMP); |
David Sterba | fadc0d8 | 2011-11-20 07:33:38 -0500 | [diff] [blame] | 9289 | |
Christian Brauner | c020d2e | 2021-07-27 12:48:43 +0200 | [diff] [blame] | 9290 | generic_fillattr(mnt_userns, inode, stat); |
Al Viro | 0ee5dc6 | 2011-07-07 15:44:25 -0400 | [diff] [blame] | 9291 | stat->dev = BTRFS_I(inode)->root->anon_dev; |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 9292 | |
| 9293 | spin_lock(&BTRFS_I(inode)->lock); |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 9294 | delalloc_bytes = BTRFS_I(inode)->new_delalloc_bytes; |
Filipe Manana | 2766ff6 | 2020-11-04 11:07:34 +0000 | [diff] [blame] | 9295 | inode_bytes = inode_get_bytes(inode); |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 9296 | spin_unlock(&BTRFS_I(inode)->lock); |
Filipe Manana | 2766ff6 | 2020-11-04 11:07:34 +0000 | [diff] [blame] | 9297 | stat->blocks = (ALIGN(inode_bytes, blocksize) + |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 9298 | ALIGN(delalloc_bytes, blocksize)) >> 9; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9299 | return 0; |
| 9300 | } |
| 9301 | |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9302 | static int btrfs_rename_exchange(struct inode *old_dir, |
| 9303 | struct dentry *old_dentry, |
| 9304 | struct inode *new_dir, |
| 9305 | struct dentry *new_dentry) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9306 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9307 | struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9308 | struct btrfs_trans_handle *trans; |
| 9309 | struct btrfs_root *root = BTRFS_I(old_dir)->root; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9310 | struct btrfs_root *dest = BTRFS_I(new_dir)->root; |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9311 | struct inode *new_inode = new_dentry->d_inode; |
| 9312 | struct inode *old_inode = old_dentry->d_inode; |
Deepa Dinamani | 95582b0 | 2018-05-08 19:36:02 -0700 | [diff] [blame] | 9313 | struct timespec64 ctime = current_time(old_inode); |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 9314 | u64 old_ino = btrfs_ino(BTRFS_I(old_inode)); |
| 9315 | u64 new_ino = btrfs_ino(BTRFS_I(new_inode)); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9316 | u64 old_idx = 0; |
| 9317 | u64 new_idx = 0; |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9318 | int ret; |
Filipe Manana | 75b463d | 2020-08-11 12:43:48 +0100 | [diff] [blame] | 9319 | int ret2; |
Filipe Manana | 86e8aa0 | 2016-05-05 02:02:27 +0100 | [diff] [blame] | 9320 | bool root_log_pinned = false; |
| 9321 | bool dest_log_pinned = false; |
Josef Bacik | dc09ef3 | 2021-05-19 14:04:21 -0400 | [diff] [blame] | 9322 | bool need_abort = false; |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9323 | |
NeilBrown | 3f79f6f | 2021-08-06 14:26:24 +1000 | [diff] [blame] | 9324 | /* |
| 9325 | * For non-subvolumes allow exchange only within one subvolume, in the |
| 9326 | * same inode namespace. Two subvolumes (represented as directory) can |
| 9327 | * be exchanged as they're a logical link and have a fixed inode number. |
| 9328 | */ |
| 9329 | if (root != dest && |
| 9330 | (old_ino != BTRFS_FIRST_FREE_OBJECTID || |
| 9331 | new_ino != BTRFS_FIRST_FREE_OBJECTID)) |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9332 | return -EXDEV; |
| 9333 | |
| 9334 | /* close the race window with snapshot create/destroy ioctl */ |
Josef Bacik | 943eb3b | 2019-11-19 13:59:20 -0500 | [diff] [blame] | 9335 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID || |
| 9336 | new_ino == BTRFS_FIRST_FREE_OBJECTID) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9337 | down_read(&fs_info->subvol_sem); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9338 | |
| 9339 | /* |
| 9340 | * We want to reserve the absolute worst case amount of items. So if |
| 9341 | * both inodes are subvols and we need to unlink them then that would |
| 9342 | * require 4 item modifications, but if they are both normal inodes it |
| 9343 | * would require 5 item modifications, so we'll assume their normal |
| 9344 | * inodes. So 5 * 2 is 10, plus 2 for the new links, so 12 total items |
| 9345 | * should cover the worst case number of items we'll modify. |
| 9346 | */ |
| 9347 | trans = btrfs_start_transaction(root, 12); |
| 9348 | if (IS_ERR(trans)) { |
| 9349 | ret = PTR_ERR(trans); |
| 9350 | goto out_notrans; |
| 9351 | } |
| 9352 | |
Josef Bacik | 00aa8e8 | 2021-03-12 15:25:02 -0500 | [diff] [blame] | 9353 | if (dest != root) { |
| 9354 | ret = btrfs_record_root_in_trans(trans, dest); |
| 9355 | if (ret) |
| 9356 | goto out_fail; |
| 9357 | } |
Josef Bacik | 3e17409 | 2019-11-15 15:43:06 -0500 | [diff] [blame] | 9358 | |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9359 | /* |
| 9360 | * We need to find a free sequence number both in the source and |
| 9361 | * in the destination directory for the exchange. |
| 9362 | */ |
Nikolay Borisov | 877574e | 2017-02-20 13:50:33 +0200 | [diff] [blame] | 9363 | ret = btrfs_set_inode_index(BTRFS_I(new_dir), &old_idx); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9364 | if (ret) |
| 9365 | goto out_fail; |
Nikolay Borisov | 877574e | 2017-02-20 13:50:33 +0200 | [diff] [blame] | 9366 | ret = btrfs_set_inode_index(BTRFS_I(old_dir), &new_idx); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9367 | if (ret) |
| 9368 | goto out_fail; |
| 9369 | |
| 9370 | BTRFS_I(old_inode)->dir_index = 0ULL; |
| 9371 | BTRFS_I(new_inode)->dir_index = 0ULL; |
| 9372 | |
| 9373 | /* Reference for the source. */ |
| 9374 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) { |
| 9375 | /* force full log commit if subvolume involved. */ |
David Sterba | 9078776 | 2019-03-20 13:28:05 +0100 | [diff] [blame] | 9376 | btrfs_set_log_full_commit(trans); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9377 | } else { |
| 9378 | ret = btrfs_insert_inode_ref(trans, dest, |
| 9379 | new_dentry->d_name.name, |
| 9380 | new_dentry->d_name.len, |
| 9381 | old_ino, |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 9382 | btrfs_ino(BTRFS_I(new_dir)), |
| 9383 | old_idx); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9384 | if (ret) |
| 9385 | goto out_fail; |
Josef Bacik | dc09ef3 | 2021-05-19 14:04:21 -0400 | [diff] [blame] | 9386 | need_abort = true; |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9387 | } |
| 9388 | |
| 9389 | /* And now for the dest. */ |
| 9390 | if (new_ino == BTRFS_FIRST_FREE_OBJECTID) { |
| 9391 | /* force full log commit if subvolume involved. */ |
David Sterba | 9078776 | 2019-03-20 13:28:05 +0100 | [diff] [blame] | 9392 | btrfs_set_log_full_commit(trans); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9393 | } else { |
| 9394 | ret = btrfs_insert_inode_ref(trans, root, |
| 9395 | old_dentry->d_name.name, |
| 9396 | old_dentry->d_name.len, |
| 9397 | new_ino, |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 9398 | btrfs_ino(BTRFS_I(old_dir)), |
| 9399 | new_idx); |
Josef Bacik | dc09ef3 | 2021-05-19 14:04:21 -0400 | [diff] [blame] | 9400 | if (ret) { |
| 9401 | if (need_abort) |
| 9402 | btrfs_abort_transaction(trans, ret); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9403 | goto out_fail; |
Josef Bacik | dc09ef3 | 2021-05-19 14:04:21 -0400 | [diff] [blame] | 9404 | } |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9405 | } |
| 9406 | |
| 9407 | /* Update inode version and ctime/mtime. */ |
| 9408 | inode_inc_iversion(old_dir); |
| 9409 | inode_inc_iversion(new_dir); |
| 9410 | inode_inc_iversion(old_inode); |
| 9411 | inode_inc_iversion(new_inode); |
| 9412 | old_dir->i_ctime = old_dir->i_mtime = ctime; |
| 9413 | new_dir->i_ctime = new_dir->i_mtime = ctime; |
| 9414 | old_inode->i_ctime = ctime; |
| 9415 | new_inode->i_ctime = ctime; |
| 9416 | |
| 9417 | if (old_dentry->d_parent != new_dentry->d_parent) { |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 9418 | btrfs_record_unlink_dir(trans, BTRFS_I(old_dir), |
| 9419 | BTRFS_I(old_inode), 1); |
| 9420 | btrfs_record_unlink_dir(trans, BTRFS_I(new_dir), |
| 9421 | BTRFS_I(new_inode), 1); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9422 | } |
| 9423 | |
Filipe Manana | bd54f38 | 2021-07-27 11:24:45 +0100 | [diff] [blame] | 9424 | /* |
| 9425 | * Now pin the logs of the roots. We do it to ensure that no other task |
| 9426 | * can sync the logs while we are in progress with the rename, because |
| 9427 | * that could result in an inconsistency in case any of the inodes that |
| 9428 | * are part of this rename operation were logged before. |
| 9429 | * |
| 9430 | * We pin the logs even if at this precise moment none of the inodes was |
| 9431 | * logged before. This is because right after we checked for that, some |
| 9432 | * other task fsyncing some other inode not involved with this rename |
| 9433 | * operation could log that one of our inodes exists. |
| 9434 | * |
| 9435 | * We don't need to pin the logs before the above calls to |
| 9436 | * btrfs_insert_inode_ref(), since those don't ever need to change a log. |
| 9437 | */ |
| 9438 | if (old_ino != BTRFS_FIRST_FREE_OBJECTID) { |
| 9439 | btrfs_pin_log_trans(root); |
| 9440 | root_log_pinned = true; |
| 9441 | } |
| 9442 | if (new_ino != BTRFS_FIRST_FREE_OBJECTID) { |
| 9443 | btrfs_pin_log_trans(dest); |
| 9444 | dest_log_pinned = true; |
| 9445 | } |
| 9446 | |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9447 | /* src is a subvolume */ |
| 9448 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) { |
Josef Bacik | 045d396 | 2019-12-18 17:20:27 -0500 | [diff] [blame] | 9449 | ret = btrfs_unlink_subvol(trans, old_dir, old_dentry); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9450 | } else { /* src is an inode */ |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 9451 | ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir), |
| 9452 | BTRFS_I(old_dentry->d_inode), |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9453 | old_dentry->d_name.name, |
| 9454 | old_dentry->d_name.len); |
| 9455 | if (!ret) |
Nikolay Borisov | 9a56fcd | 2020-11-02 16:48:59 +0200 | [diff] [blame] | 9456 | ret = btrfs_update_inode(trans, root, BTRFS_I(old_inode)); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9457 | } |
| 9458 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 9459 | btrfs_abort_transaction(trans, ret); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9460 | goto out_fail; |
| 9461 | } |
| 9462 | |
| 9463 | /* dest is a subvolume */ |
| 9464 | if (new_ino == BTRFS_FIRST_FREE_OBJECTID) { |
Josef Bacik | 045d396 | 2019-12-18 17:20:27 -0500 | [diff] [blame] | 9465 | ret = btrfs_unlink_subvol(trans, new_dir, new_dentry); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9466 | } else { /* dest is an inode */ |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 9467 | ret = __btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir), |
| 9468 | BTRFS_I(new_dentry->d_inode), |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9469 | new_dentry->d_name.name, |
| 9470 | new_dentry->d_name.len); |
| 9471 | if (!ret) |
Nikolay Borisov | 9a56fcd | 2020-11-02 16:48:59 +0200 | [diff] [blame] | 9472 | ret = btrfs_update_inode(trans, dest, BTRFS_I(new_inode)); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9473 | } |
| 9474 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 9475 | btrfs_abort_transaction(trans, ret); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9476 | goto out_fail; |
| 9477 | } |
| 9478 | |
Nikolay Borisov | db0a669 | 2017-02-20 13:51:08 +0200 | [diff] [blame] | 9479 | ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode), |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9480 | new_dentry->d_name.name, |
| 9481 | new_dentry->d_name.len, 0, old_idx); |
| 9482 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 9483 | btrfs_abort_transaction(trans, ret); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9484 | goto out_fail; |
| 9485 | } |
| 9486 | |
Nikolay Borisov | db0a669 | 2017-02-20 13:51:08 +0200 | [diff] [blame] | 9487 | ret = btrfs_add_link(trans, BTRFS_I(old_dir), BTRFS_I(new_inode), |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9488 | old_dentry->d_name.name, |
| 9489 | old_dentry->d_name.len, 0, new_idx); |
| 9490 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 9491 | btrfs_abort_transaction(trans, ret); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9492 | goto out_fail; |
| 9493 | } |
| 9494 | |
| 9495 | if (old_inode->i_nlink == 1) |
| 9496 | BTRFS_I(old_inode)->dir_index = old_idx; |
| 9497 | if (new_inode->i_nlink == 1) |
| 9498 | BTRFS_I(new_inode)->dir_index = new_idx; |
| 9499 | |
Filipe Manana | 86e8aa0 | 2016-05-05 02:02:27 +0100 | [diff] [blame] | 9500 | if (root_log_pinned) { |
Filipe Manana | 75b463d | 2020-08-11 12:43:48 +0100 | [diff] [blame] | 9501 | btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir), |
| 9502 | new_dentry->d_parent); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9503 | btrfs_end_log_trans(root); |
Filipe Manana | 86e8aa0 | 2016-05-05 02:02:27 +0100 | [diff] [blame] | 9504 | root_log_pinned = false; |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9505 | } |
Filipe Manana | 86e8aa0 | 2016-05-05 02:02:27 +0100 | [diff] [blame] | 9506 | if (dest_log_pinned) { |
Filipe Manana | 75b463d | 2020-08-11 12:43:48 +0100 | [diff] [blame] | 9507 | btrfs_log_new_name(trans, BTRFS_I(new_inode), BTRFS_I(new_dir), |
| 9508 | old_dentry->d_parent); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9509 | btrfs_end_log_trans(dest); |
Filipe Manana | 86e8aa0 | 2016-05-05 02:02:27 +0100 | [diff] [blame] | 9510 | dest_log_pinned = false; |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9511 | } |
| 9512 | out_fail: |
Filipe Manana | 86e8aa0 | 2016-05-05 02:02:27 +0100 | [diff] [blame] | 9513 | /* |
| 9514 | * If we have pinned a log and an error happened, we unpin tasks |
| 9515 | * trying to sync the log and force them to fallback to a transaction |
| 9516 | * commit if the log currently contains any of the inodes involved in |
| 9517 | * this rename operation (to ensure we do not persist a log with an |
| 9518 | * inconsistent state for any of these inodes or leading to any |
| 9519 | * inconsistencies when replayed). If the transaction was aborted, the |
| 9520 | * abortion reason is propagated to userspace when attempting to commit |
| 9521 | * the transaction. If the log does not contain any of these inodes, we |
| 9522 | * allow the tasks to sync it. |
| 9523 | */ |
| 9524 | if (ret && (root_log_pinned || dest_log_pinned)) { |
Nikolay Borisov | 0f8939b | 2017-01-18 00:31:30 +0200 | [diff] [blame] | 9525 | if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) || |
| 9526 | btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) || |
| 9527 | btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) || |
Filipe Manana | 1c167b8 | 2021-07-29 15:28:34 +0100 | [diff] [blame] | 9528 | btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)) |
David Sterba | 9078776 | 2019-03-20 13:28:05 +0100 | [diff] [blame] | 9529 | btrfs_set_log_full_commit(trans); |
Filipe Manana | 86e8aa0 | 2016-05-05 02:02:27 +0100 | [diff] [blame] | 9530 | |
| 9531 | if (root_log_pinned) { |
| 9532 | btrfs_end_log_trans(root); |
| 9533 | root_log_pinned = false; |
| 9534 | } |
| 9535 | if (dest_log_pinned) { |
| 9536 | btrfs_end_log_trans(dest); |
| 9537 | dest_log_pinned = false; |
| 9538 | } |
| 9539 | } |
Filipe Manana | 75b463d | 2020-08-11 12:43:48 +0100 | [diff] [blame] | 9540 | ret2 = btrfs_end_transaction(trans); |
| 9541 | ret = ret ? ret : ret2; |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9542 | out_notrans: |
Josef Bacik | 943eb3b | 2019-11-19 13:59:20 -0500 | [diff] [blame] | 9543 | if (new_ino == BTRFS_FIRST_FREE_OBJECTID || |
| 9544 | old_ino == BTRFS_FIRST_FREE_OBJECTID) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9545 | up_read(&fs_info->subvol_sem); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9546 | |
| 9547 | return ret; |
| 9548 | } |
| 9549 | |
| 9550 | static int btrfs_whiteout_for_rename(struct btrfs_trans_handle *trans, |
| 9551 | struct btrfs_root *root, |
Christian Brauner | ca07274 | 2021-07-27 12:48:42 +0200 | [diff] [blame] | 9552 | struct user_namespace *mnt_userns, |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9553 | struct inode *dir, |
| 9554 | struct dentry *dentry) |
| 9555 | { |
| 9556 | int ret; |
| 9557 | struct inode *inode; |
| 9558 | u64 objectid; |
| 9559 | u64 index; |
| 9560 | |
Nikolay Borisov | 543068a | 2020-12-07 17:32:33 +0200 | [diff] [blame] | 9561 | ret = btrfs_get_free_objectid(root, &objectid); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9562 | if (ret) |
| 9563 | return ret; |
| 9564 | |
Christian Brauner | ca07274 | 2021-07-27 12:48:42 +0200 | [diff] [blame] | 9565 | inode = btrfs_new_inode(trans, root, mnt_userns, dir, |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9566 | dentry->d_name.name, |
| 9567 | dentry->d_name.len, |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 9568 | btrfs_ino(BTRFS_I(dir)), |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9569 | objectid, |
| 9570 | S_IFCHR | WHITEOUT_MODE, |
| 9571 | &index); |
| 9572 | |
| 9573 | if (IS_ERR(inode)) { |
| 9574 | ret = PTR_ERR(inode); |
| 9575 | return ret; |
| 9576 | } |
| 9577 | |
| 9578 | inode->i_op = &btrfs_special_inode_operations; |
| 9579 | init_special_inode(inode, inode->i_mode, |
| 9580 | WHITEOUT_DEV); |
| 9581 | |
| 9582 | ret = btrfs_init_inode_security(trans, inode, dir, |
| 9583 | &dentry->d_name); |
| 9584 | if (ret) |
Filipe Manana | c990161 | 2016-05-05 01:41:57 +0100 | [diff] [blame] | 9585 | goto out; |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9586 | |
Nikolay Borisov | cef415a | 2017-02-20 13:51:09 +0200 | [diff] [blame] | 9587 | ret = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, |
| 9588 | BTRFS_I(inode), 0, index); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9589 | if (ret) |
Filipe Manana | c990161 | 2016-05-05 01:41:57 +0100 | [diff] [blame] | 9590 | goto out; |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9591 | |
Nikolay Borisov | 9a56fcd | 2020-11-02 16:48:59 +0200 | [diff] [blame] | 9592 | ret = btrfs_update_inode(trans, root, BTRFS_I(inode)); |
Filipe Manana | c990161 | 2016-05-05 01:41:57 +0100 | [diff] [blame] | 9593 | out: |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9594 | unlock_new_inode(inode); |
Filipe Manana | c990161 | 2016-05-05 01:41:57 +0100 | [diff] [blame] | 9595 | if (ret) |
| 9596 | inode_dec_link_count(inode); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9597 | iput(inode); |
| 9598 | |
Filipe Manana | c990161 | 2016-05-05 01:41:57 +0100 | [diff] [blame] | 9599 | return ret; |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9600 | } |
| 9601 | |
Christian Brauner | ca07274 | 2021-07-27 12:48:42 +0200 | [diff] [blame] | 9602 | static int btrfs_rename(struct user_namespace *mnt_userns, |
| 9603 | struct inode *old_dir, struct dentry *old_dentry, |
| 9604 | struct inode *new_dir, struct dentry *new_dentry, |
| 9605 | unsigned int flags) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9606 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9607 | struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9608 | struct btrfs_trans_handle *trans; |
Filipe Manana | 5062af3 | 2016-05-05 10:26:26 +0100 | [diff] [blame] | 9609 | unsigned int trans_num_items; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9610 | struct btrfs_root *root = BTRFS_I(old_dir)->root; |
| 9611 | struct btrfs_root *dest = BTRFS_I(new_dir)->root; |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 9612 | struct inode *new_inode = d_inode(new_dentry); |
| 9613 | struct inode *old_inode = d_inode(old_dentry); |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 9614 | u64 index = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9615 | int ret; |
Filipe Manana | 75b463d | 2020-08-11 12:43:48 +0100 | [diff] [blame] | 9616 | int ret2; |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 9617 | u64 old_ino = btrfs_ino(BTRFS_I(old_inode)); |
Filipe Manana | 3dc9e8f7 | 2016-04-29 11:34:22 +0100 | [diff] [blame] | 9618 | bool log_pinned = false; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9619 | |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 9620 | if (btrfs_ino(BTRFS_I(new_dir)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) |
Yan, Zheng | f679a84 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 9621 | return -EPERM; |
| 9622 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9623 | /* we only allow rename subvolume link between subvolumes */ |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 9624 | if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest) |
Chris Mason | 3394e16 | 2008-11-17 20:42:26 -0500 | [diff] [blame] | 9625 | return -EXDEV; |
| 9626 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 9627 | if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID || |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 9628 | (new_inode && btrfs_ino(BTRFS_I(new_inode)) == BTRFS_FIRST_FREE_OBJECTID)) |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9629 | return -ENOTEMPTY; |
| 9630 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9631 | if (S_ISDIR(old_inode->i_mode) && new_inode && |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9632 | new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9633 | return -ENOTEMPTY; |
Chris Mason | 9c52057 | 2012-12-17 14:26:57 -0500 | [diff] [blame] | 9634 | |
| 9635 | |
| 9636 | /* check for collisions, even if the name isn't there */ |
Josef Bacik | 4871c15 | 2013-10-09 12:24:04 -0400 | [diff] [blame] | 9637 | ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino, |
Chris Mason | 9c52057 | 2012-12-17 14:26:57 -0500 | [diff] [blame] | 9638 | new_dentry->d_name.name, |
| 9639 | new_dentry->d_name.len); |
| 9640 | |
| 9641 | if (ret) { |
| 9642 | if (ret == -EEXIST) { |
| 9643 | /* we shouldn't get |
| 9644 | * eexist without a new_inode */ |
Dulshani Gunawardhana | fae7f21 | 2013-10-31 10:30:08 +0530 | [diff] [blame] | 9645 | if (WARN_ON(!new_inode)) { |
Chris Mason | 9c52057 | 2012-12-17 14:26:57 -0500 | [diff] [blame] | 9646 | return ret; |
| 9647 | } |
| 9648 | } else { |
| 9649 | /* maybe -EOVERFLOW */ |
| 9650 | return ret; |
| 9651 | } |
| 9652 | } |
| 9653 | ret = 0; |
| 9654 | |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 9655 | /* |
Chris Mason | 8d875f9 | 2014-08-12 10:47:42 -0700 | [diff] [blame] | 9656 | * we're using rename to replace one file with another. Start IO on it |
| 9657 | * now so we don't add too much work to the end of the transaction |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 9658 | */ |
Chris Mason | 8d875f9 | 2014-08-12 10:47:42 -0700 | [diff] [blame] | 9659 | if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size) |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 9660 | filemap_flush(old_inode->i_mapping); |
| 9661 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 9662 | /* close the racy window with snapshot create/destroy ioctl */ |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 9663 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9664 | down_read(&fs_info->subvol_sem); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 9665 | /* |
| 9666 | * We want to reserve the absolute worst case amount of items. So if |
| 9667 | * both inodes are subvols and we need to unlink them then that would |
| 9668 | * require 4 item modifications, but if they are both normal inodes it |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9669 | * would require 5 item modifications, so we'll assume they are normal |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 9670 | * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items |
| 9671 | * should cover the worst case number of items we'll modify. |
Filipe Manana | 5062af3 | 2016-05-05 10:26:26 +0100 | [diff] [blame] | 9672 | * If our rename has the whiteout flag, we need more 5 units for the |
| 9673 | * new inode (1 inode item, 1 inode ref, 2 dir items and 1 xattr item |
| 9674 | * when selinux is enabled). |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 9675 | */ |
Filipe Manana | 5062af3 | 2016-05-05 10:26:26 +0100 | [diff] [blame] | 9676 | trans_num_items = 11; |
| 9677 | if (flags & RENAME_WHITEOUT) |
| 9678 | trans_num_items += 5; |
| 9679 | trans = btrfs_start_transaction(root, trans_num_items); |
Johann Lombardi | b44c59a | 2011-03-31 13:23:47 +0000 | [diff] [blame] | 9680 | if (IS_ERR(trans)) { |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9681 | ret = PTR_ERR(trans); |
| 9682 | goto out_notrans; |
| 9683 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 9684 | |
Josef Bacik | b0fec6f | 2021-03-12 15:25:03 -0500 | [diff] [blame] | 9685 | if (dest != root) { |
| 9686 | ret = btrfs_record_root_in_trans(trans, dest); |
| 9687 | if (ret) |
| 9688 | goto out_fail; |
| 9689 | } |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9690 | |
Nikolay Borisov | 877574e | 2017-02-20 13:50:33 +0200 | [diff] [blame] | 9691 | ret = btrfs_set_inode_index(BTRFS_I(new_dir), &index); |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 9692 | if (ret) |
| 9693 | goto out_fail; |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 9694 | |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 9695 | BTRFS_I(old_inode)->dir_index = 0ULL; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 9696 | if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9697 | /* force full log commit if subvolume involved. */ |
David Sterba | 9078776 | 2019-03-20 13:28:05 +0100 | [diff] [blame] | 9698 | btrfs_set_log_full_commit(trans); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9699 | } else { |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 9700 | ret = btrfs_insert_inode_ref(trans, dest, |
| 9701 | new_dentry->d_name.name, |
| 9702 | new_dentry->d_name.len, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 9703 | old_ino, |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 9704 | btrfs_ino(BTRFS_I(new_dir)), index); |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 9705 | if (ret) |
| 9706 | goto out_fail; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9707 | } |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 9708 | |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 9709 | inode_inc_iversion(old_dir); |
| 9710 | inode_inc_iversion(new_dir); |
| 9711 | inode_inc_iversion(old_inode); |
Deepa Dinamani | 04b285f | 2016-02-06 23:57:21 -0800 | [diff] [blame] | 9712 | old_dir->i_ctime = old_dir->i_mtime = |
| 9713 | new_dir->i_ctime = new_dir->i_mtime = |
Deepa Dinamani | c2050a4 | 2016-09-14 07:48:06 -0700 | [diff] [blame] | 9714 | old_inode->i_ctime = current_time(old_dir); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 9715 | |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 9716 | if (old_dentry->d_parent != new_dentry->d_parent) |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 9717 | btrfs_record_unlink_dir(trans, BTRFS_I(old_dir), |
| 9718 | BTRFS_I(old_inode), 1); |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 9719 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 9720 | if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) { |
Josef Bacik | 045d396 | 2019-12-18 17:20:27 -0500 | [diff] [blame] | 9721 | ret = btrfs_unlink_subvol(trans, old_dir, old_dentry); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9722 | } else { |
Filipe Manana | bd54f38 | 2021-07-27 11:24:45 +0100 | [diff] [blame] | 9723 | /* |
| 9724 | * Now pin the log. We do it to ensure that no other task can |
| 9725 | * sync the log while we are in progress with the rename, as |
| 9726 | * that could result in an inconsistency in case any of the |
| 9727 | * inodes that are part of this rename operation were logged |
| 9728 | * before. |
| 9729 | * |
| 9730 | * We pin the log even if at this precise moment none of the |
| 9731 | * inodes was logged before. This is because right after we |
| 9732 | * checked for that, some other task fsyncing some other inode |
| 9733 | * not involved with this rename operation could log that one of |
| 9734 | * our inodes exists. |
| 9735 | * |
| 9736 | * We don't need to pin the logs before the above call to |
| 9737 | * btrfs_insert_inode_ref(), since that does not need to change |
| 9738 | * a log. |
| 9739 | */ |
| 9740 | btrfs_pin_log_trans(root); |
| 9741 | log_pinned = true; |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 9742 | ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir), |
| 9743 | BTRFS_I(d_inode(old_dentry)), |
Al Viro | 9298679 | 2011-03-04 17:14:37 +0000 | [diff] [blame] | 9744 | old_dentry->d_name.name, |
| 9745 | old_dentry->d_name.len); |
| 9746 | if (!ret) |
Nikolay Borisov | 9a56fcd | 2020-11-02 16:48:59 +0200 | [diff] [blame] | 9747 | ret = btrfs_update_inode(trans, root, BTRFS_I(old_inode)); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9748 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 9749 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 9750 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 9751 | goto out_fail; |
| 9752 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9753 | |
| 9754 | if (new_inode) { |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 9755 | inode_inc_iversion(new_inode); |
Deepa Dinamani | c2050a4 | 2016-09-14 07:48:06 -0700 | [diff] [blame] | 9756 | new_inode->i_ctime = current_time(new_inode); |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 9757 | if (unlikely(btrfs_ino(BTRFS_I(new_inode)) == |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9758 | BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) { |
Josef Bacik | 045d396 | 2019-12-18 17:20:27 -0500 | [diff] [blame] | 9759 | ret = btrfs_unlink_subvol(trans, new_dir, new_dentry); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9760 | BUG_ON(new_inode->i_nlink == 0); |
| 9761 | } else { |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 9762 | ret = btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir), |
| 9763 | BTRFS_I(d_inode(new_dentry)), |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9764 | new_dentry->d_name.name, |
| 9765 | new_dentry->d_name.len); |
| 9766 | } |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 9767 | if (!ret && new_inode->i_nlink == 0) |
Nikolay Borisov | 73f2e54 | 2017-02-20 13:50:59 +0200 | [diff] [blame] | 9768 | ret = btrfs_orphan_add(trans, |
| 9769 | BTRFS_I(d_inode(new_dentry))); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 9770 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 9771 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 9772 | goto out_fail; |
| 9773 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9774 | } |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 9775 | |
Nikolay Borisov | db0a669 | 2017-02-20 13:51:08 +0200 | [diff] [blame] | 9776 | ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode), |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9777 | new_dentry->d_name.name, |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 9778 | new_dentry->d_name.len, 0, index); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 9779 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 9780 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 9781 | goto out_fail; |
| 9782 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9783 | |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 9784 | if (old_inode->i_nlink == 1) |
| 9785 | BTRFS_I(old_inode)->dir_index = index; |
| 9786 | |
Filipe Manana | 3dc9e8f7 | 2016-04-29 11:34:22 +0100 | [diff] [blame] | 9787 | if (log_pinned) { |
Filipe Manana | 75b463d | 2020-08-11 12:43:48 +0100 | [diff] [blame] | 9788 | btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir), |
| 9789 | new_dentry->d_parent); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9790 | btrfs_end_log_trans(root); |
Filipe Manana | 3dc9e8f7 | 2016-04-29 11:34:22 +0100 | [diff] [blame] | 9791 | log_pinned = false; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9792 | } |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9793 | |
| 9794 | if (flags & RENAME_WHITEOUT) { |
Christian Brauner | ca07274 | 2021-07-27 12:48:42 +0200 | [diff] [blame] | 9795 | ret = btrfs_whiteout_for_rename(trans, root, mnt_userns, |
| 9796 | old_dir, old_dentry); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9797 | |
| 9798 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 9799 | btrfs_abort_transaction(trans, ret); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9800 | goto out_fail; |
| 9801 | } |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 9802 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9803 | out_fail: |
Filipe Manana | 3dc9e8f7 | 2016-04-29 11:34:22 +0100 | [diff] [blame] | 9804 | /* |
| 9805 | * If we have pinned the log and an error happened, we unpin tasks |
| 9806 | * trying to sync the log and force them to fallback to a transaction |
| 9807 | * commit if the log currently contains any of the inodes involved in |
| 9808 | * this rename operation (to ensure we do not persist a log with an |
| 9809 | * inconsistent state for any of these inodes or leading to any |
| 9810 | * inconsistencies when replayed). If the transaction was aborted, the |
| 9811 | * abortion reason is propagated to userspace when attempting to commit |
| 9812 | * the transaction. If the log does not contain any of these inodes, we |
| 9813 | * allow the tasks to sync it. |
| 9814 | */ |
| 9815 | if (ret && log_pinned) { |
Nikolay Borisov | 0f8939b | 2017-01-18 00:31:30 +0200 | [diff] [blame] | 9816 | if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) || |
| 9817 | btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) || |
| 9818 | btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) || |
Filipe Manana | 3dc9e8f7 | 2016-04-29 11:34:22 +0100 | [diff] [blame] | 9819 | (new_inode && |
Nikolay Borisov | 0f8939b | 2017-01-18 00:31:30 +0200 | [diff] [blame] | 9820 | btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation))) |
David Sterba | 9078776 | 2019-03-20 13:28:05 +0100 | [diff] [blame] | 9821 | btrfs_set_log_full_commit(trans); |
Filipe Manana | 3dc9e8f7 | 2016-04-29 11:34:22 +0100 | [diff] [blame] | 9822 | |
| 9823 | btrfs_end_log_trans(root); |
| 9824 | log_pinned = false; |
| 9825 | } |
Filipe Manana | 75b463d | 2020-08-11 12:43:48 +0100 | [diff] [blame] | 9826 | ret2 = btrfs_end_transaction(trans); |
| 9827 | ret = ret ? ret : ret2; |
Johann Lombardi | b44c59a | 2011-03-31 13:23:47 +0000 | [diff] [blame] | 9828 | out_notrans: |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 9829 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9830 | up_read(&fs_info->subvol_sem); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 9831 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9832 | return ret; |
| 9833 | } |
| 9834 | |
Christian Brauner | 549c729 | 2021-01-21 14:19:43 +0100 | [diff] [blame] | 9835 | static int btrfs_rename2(struct user_namespace *mnt_userns, struct inode *old_dir, |
| 9836 | struct dentry *old_dentry, struct inode *new_dir, |
| 9837 | struct dentry *new_dentry, unsigned int flags) |
Miklos Szeredi | 80ace85 | 2014-07-23 15:15:32 +0200 | [diff] [blame] | 9838 | { |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9839 | if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT)) |
Miklos Szeredi | 80ace85 | 2014-07-23 15:15:32 +0200 | [diff] [blame] | 9840 | return -EINVAL; |
| 9841 | |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9842 | if (flags & RENAME_EXCHANGE) |
| 9843 | return btrfs_rename_exchange(old_dir, old_dentry, new_dir, |
| 9844 | new_dentry); |
| 9845 | |
Christian Brauner | ca07274 | 2021-07-27 12:48:42 +0200 | [diff] [blame] | 9846 | return btrfs_rename(mnt_userns, old_dir, old_dentry, new_dir, |
| 9847 | new_dentry, flags); |
Miklos Szeredi | 80ace85 | 2014-07-23 15:15:32 +0200 | [diff] [blame] | 9848 | } |
| 9849 | |
Nikolay Borisov | 3a2f8c0 | 2018-04-24 17:23:59 +0300 | [diff] [blame] | 9850 | struct btrfs_delalloc_work { |
| 9851 | struct inode *inode; |
| 9852 | struct completion completion; |
| 9853 | struct list_head list; |
| 9854 | struct btrfs_work work; |
| 9855 | }; |
| 9856 | |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 9857 | static void btrfs_run_delalloc_work(struct btrfs_work *work) |
| 9858 | { |
| 9859 | struct btrfs_delalloc_work *delalloc_work; |
Josef Bacik | 9f23e28 | 2013-10-28 15:03:41 -0400 | [diff] [blame] | 9860 | struct inode *inode; |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 9861 | |
| 9862 | delalloc_work = container_of(work, struct btrfs_delalloc_work, |
| 9863 | work); |
Josef Bacik | 9f23e28 | 2013-10-28 15:03:41 -0400 | [diff] [blame] | 9864 | inode = delalloc_work->inode; |
David Sterba | 3042460 | 2015-11-27 19:27:11 +0100 | [diff] [blame] | 9865 | filemap_flush(inode->i_mapping); |
| 9866 | if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, |
| 9867 | &BTRFS_I(inode)->runtime_flags)) |
Josef Bacik | 9f23e28 | 2013-10-28 15:03:41 -0400 | [diff] [blame] | 9868 | filemap_flush(inode->i_mapping); |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 9869 | |
Nikolay Borisov | 076da91 | 2018-04-23 10:54:16 +0300 | [diff] [blame] | 9870 | iput(inode); |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 9871 | complete(&delalloc_work->completion); |
| 9872 | } |
| 9873 | |
Nikolay Borisov | 3a2f8c0 | 2018-04-24 17:23:59 +0300 | [diff] [blame] | 9874 | static struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode) |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 9875 | { |
| 9876 | struct btrfs_delalloc_work *work; |
| 9877 | |
David Sterba | 100d570 | 2015-12-08 14:39:32 +0100 | [diff] [blame] | 9878 | work = kmalloc(sizeof(*work), GFP_NOFS); |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 9879 | if (!work) |
| 9880 | return NULL; |
| 9881 | |
| 9882 | init_completion(&work->completion); |
| 9883 | INIT_LIST_HEAD(&work->list); |
| 9884 | work->inode = inode; |
Omar Sandoval | a0cac0e | 2019-09-16 11:30:57 -0700 | [diff] [blame] | 9885 | btrfs_init_work(&work->work, btrfs_run_delalloc_work, NULL, NULL); |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 9886 | |
| 9887 | return work; |
| 9888 | } |
| 9889 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 9890 | /* |
| 9891 | * some fairly slow code that needs optimization. This walks the list |
| 9892 | * of all the inodes with pending delalloc and forces them to disk. |
| 9893 | */ |
Josef Bacik | e076ab2a | 2021-01-07 17:08:30 -0500 | [diff] [blame] | 9894 | static int start_delalloc_inodes(struct btrfs_root *root, |
| 9895 | struct writeback_control *wbc, bool snapshot, |
Filipe Manana | 3d45f22 | 2020-12-02 11:55:58 +0000 | [diff] [blame] | 9896 | bool in_reclaim_context) |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 9897 | { |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 9898 | struct btrfs_inode *binode; |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 9899 | struct inode *inode; |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 9900 | struct btrfs_delalloc_work *work, *next; |
| 9901 | struct list_head works; |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 9902 | struct list_head splice; |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 9903 | int ret = 0; |
Josef Bacik | e076ab2a | 2021-01-07 17:08:30 -0500 | [diff] [blame] | 9904 | bool full_flush = wbc->nr_to_write == LONG_MAX; |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 9905 | |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 9906 | INIT_LIST_HEAD(&works); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 9907 | INIT_LIST_HEAD(&splice); |
Miao Xie | 63607cc | 2013-01-22 10:50:35 +0000 | [diff] [blame] | 9908 | |
Miao Xie | 573bfb7 | 2014-03-06 13:55:03 +0800 | [diff] [blame] | 9909 | mutex_lock(&root->delalloc_mutex); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9910 | spin_lock(&root->delalloc_lock); |
| 9911 | list_splice_init(&root->delalloc_inodes, &splice); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 9912 | while (!list_empty(&splice)) { |
| 9913 | binode = list_entry(splice.next, struct btrfs_inode, |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 9914 | delalloc_inodes); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 9915 | |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9916 | list_move_tail(&binode->delalloc_inodes, |
| 9917 | &root->delalloc_inodes); |
Filipe Manana | 3d45f22 | 2020-12-02 11:55:58 +0000 | [diff] [blame] | 9918 | |
| 9919 | if (in_reclaim_context && |
| 9920 | test_bit(BTRFS_INODE_NO_DELALLOC_FLUSH, &binode->runtime_flags)) |
| 9921 | continue; |
| 9922 | |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 9923 | inode = igrab(&binode->vfs_inode); |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 9924 | if (!inode) { |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9925 | cond_resched_lock(&root->delalloc_lock); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 9926 | continue; |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 9927 | } |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9928 | spin_unlock(&root->delalloc_lock); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 9929 | |
Ethan Lien | 3cd24c6 | 2018-11-01 14:49:03 +0800 | [diff] [blame] | 9930 | if (snapshot) |
| 9931 | set_bit(BTRFS_INODE_SNAPSHOT_FLUSH, |
| 9932 | &binode->runtime_flags); |
Josef Bacik | e076ab2a | 2021-01-07 17:08:30 -0500 | [diff] [blame] | 9933 | if (full_flush) { |
| 9934 | work = btrfs_alloc_delalloc_work(inode); |
| 9935 | if (!work) { |
| 9936 | iput(inode); |
| 9937 | ret = -ENOMEM; |
| 9938 | goto out; |
| 9939 | } |
| 9940 | list_add_tail(&work->list, &works); |
| 9941 | btrfs_queue_work(root->fs_info->flush_workers, |
| 9942 | &work->work); |
| 9943 | } else { |
Josef Bacik | b377630 | 2021-07-14 14:47:23 -0400 | [diff] [blame] | 9944 | ret = filemap_fdatawrite_wbc(inode->i_mapping, wbc); |
Josef Bacik | e076ab2a | 2021-01-07 17:08:30 -0500 | [diff] [blame] | 9945 | btrfs_add_delayed_iput(inode); |
| 9946 | if (ret || wbc->nr_to_write <= 0) |
Josef Bacik | b491213 | 2020-07-21 10:22:12 -0400 | [diff] [blame] | 9947 | goto out; |
| 9948 | } |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 9949 | cond_resched(); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9950 | spin_lock(&root->delalloc_lock); |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 9951 | } |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9952 | spin_unlock(&root->delalloc_lock); |
Chris Mason | 8c8bee1 | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 9953 | |
Wang Shilong | a1ecaab | 2014-04-02 19:53:32 +0800 | [diff] [blame] | 9954 | out: |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 9955 | list_for_each_entry_safe(work, next, &works, list) { |
| 9956 | list_del_init(&work->list); |
Nikolay Borisov | 40012f9 | 2018-04-19 10:46:39 +0300 | [diff] [blame] | 9957 | wait_for_completion(&work->completion); |
| 9958 | kfree(work); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 9959 | } |
| 9960 | |
Nikolay Borisov | 81f1d39 | 2018-04-19 10:46:37 +0300 | [diff] [blame] | 9961 | if (!list_empty(&splice)) { |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9962 | spin_lock(&root->delalloc_lock); |
| 9963 | list_splice_tail(&splice, &root->delalloc_inodes); |
| 9964 | spin_unlock(&root->delalloc_lock); |
| 9965 | } |
Miao Xie | 573bfb7 | 2014-03-06 13:55:03 +0800 | [diff] [blame] | 9966 | mutex_unlock(&root->delalloc_mutex); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9967 | return ret; |
| 9968 | } |
| 9969 | |
Filipe Manana | f9baa50 | 2021-04-22 12:08:05 +0100 | [diff] [blame] | 9970 | int btrfs_start_delalloc_snapshot(struct btrfs_root *root, bool in_reclaim_context) |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9971 | { |
Josef Bacik | e076ab2a | 2021-01-07 17:08:30 -0500 | [diff] [blame] | 9972 | struct writeback_control wbc = { |
| 9973 | .nr_to_write = LONG_MAX, |
| 9974 | .sync_mode = WB_SYNC_NONE, |
| 9975 | .range_start = 0, |
| 9976 | .range_end = LLONG_MAX, |
| 9977 | }; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9978 | struct btrfs_fs_info *fs_info = root->fs_info; |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9979 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9980 | if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9981 | return -EROFS; |
| 9982 | |
Filipe Manana | f9baa50 | 2021-04-22 12:08:05 +0100 | [diff] [blame] | 9983 | return start_delalloc_inodes(root, &wbc, true, in_reclaim_context); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9984 | } |
| 9985 | |
Nikolay Borisov | 9db4dc2 | 2021-01-11 12:58:11 +0200 | [diff] [blame] | 9986 | int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, long nr, |
Filipe Manana | 3d45f22 | 2020-12-02 11:55:58 +0000 | [diff] [blame] | 9987 | bool in_reclaim_context) |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9988 | { |
Josef Bacik | e076ab2a | 2021-01-07 17:08:30 -0500 | [diff] [blame] | 9989 | struct writeback_control wbc = { |
Nikolay Borisov | 9db4dc2 | 2021-01-11 12:58:11 +0200 | [diff] [blame] | 9990 | .nr_to_write = nr, |
Josef Bacik | e076ab2a | 2021-01-07 17:08:30 -0500 | [diff] [blame] | 9991 | .sync_mode = WB_SYNC_NONE, |
| 9992 | .range_start = 0, |
| 9993 | .range_end = LLONG_MAX, |
| 9994 | }; |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9995 | struct btrfs_root *root; |
| 9996 | struct list_head splice; |
| 9997 | int ret; |
| 9998 | |
Wang Shilong | 2c21b4d | 2014-01-14 19:42:20 +0800 | [diff] [blame] | 9999 | if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 10000 | return -EROFS; |
| 10001 | |
| 10002 | INIT_LIST_HEAD(&splice); |
| 10003 | |
Miao Xie | 573bfb7 | 2014-03-06 13:55:03 +0800 | [diff] [blame] | 10004 | mutex_lock(&fs_info->delalloc_root_mutex); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 10005 | spin_lock(&fs_info->delalloc_root_lock); |
| 10006 | list_splice_init(&fs_info->delalloc_roots, &splice); |
Nikolay Borisov | d7830b7 | 2021-01-11 12:58:12 +0200 | [diff] [blame] | 10007 | while (!list_empty(&splice)) { |
Josef Bacik | e076ab2a | 2021-01-07 17:08:30 -0500 | [diff] [blame] | 10008 | /* |
| 10009 | * Reset nr_to_write here so we know that we're doing a full |
| 10010 | * flush. |
| 10011 | */ |
Nikolay Borisov | 9db4dc2 | 2021-01-11 12:58:11 +0200 | [diff] [blame] | 10012 | if (nr == LONG_MAX) |
Josef Bacik | e076ab2a | 2021-01-07 17:08:30 -0500 | [diff] [blame] | 10013 | wbc.nr_to_write = LONG_MAX; |
| 10014 | |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 10015 | root = list_first_entry(&splice, struct btrfs_root, |
| 10016 | delalloc_root); |
Josef Bacik | 0024652 | 2020-01-24 09:33:01 -0500 | [diff] [blame] | 10017 | root = btrfs_grab_root(root); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 10018 | BUG_ON(!root); |
| 10019 | list_move_tail(&root->delalloc_root, |
| 10020 | &fs_info->delalloc_roots); |
| 10021 | spin_unlock(&fs_info->delalloc_root_lock); |
| 10022 | |
Josef Bacik | e076ab2a | 2021-01-07 17:08:30 -0500 | [diff] [blame] | 10023 | ret = start_delalloc_inodes(root, &wbc, false, in_reclaim_context); |
Josef Bacik | 0024652 | 2020-01-24 09:33:01 -0500 | [diff] [blame] | 10024 | btrfs_put_root(root); |
Josef Bacik | e076ab2a | 2021-01-07 17:08:30 -0500 | [diff] [blame] | 10025 | if (ret < 0 || wbc.nr_to_write <= 0) |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 10026 | goto out; |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 10027 | spin_lock(&fs_info->delalloc_root_lock); |
| 10028 | } |
| 10029 | spin_unlock(&fs_info->delalloc_root_lock); |
| 10030 | |
Miao Xie | 6c255e6 | 2014-03-06 13:55:01 +0800 | [diff] [blame] | 10031 | ret = 0; |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 10032 | out: |
Nikolay Borisov | 81f1d39 | 2018-04-19 10:46:37 +0300 | [diff] [blame] | 10033 | if (!list_empty(&splice)) { |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 10034 | spin_lock(&fs_info->delalloc_root_lock); |
| 10035 | list_splice_tail(&splice, &fs_info->delalloc_roots); |
| 10036 | spin_unlock(&fs_info->delalloc_root_lock); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 10037 | } |
Miao Xie | 573bfb7 | 2014-03-06 13:55:03 +0800 | [diff] [blame] | 10038 | mutex_unlock(&fs_info->delalloc_root_mutex); |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 10039 | return ret; |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 10040 | } |
| 10041 | |
Christian Brauner | 549c729 | 2021-01-21 14:19:43 +0100 | [diff] [blame] | 10042 | static int btrfs_symlink(struct user_namespace *mnt_userns, struct inode *dir, |
| 10043 | struct dentry *dentry, const char *symname) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10044 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 10045 | struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10046 | struct btrfs_trans_handle *trans; |
| 10047 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 10048 | struct btrfs_path *path; |
| 10049 | struct btrfs_key key; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 10050 | struct inode *inode = NULL; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10051 | int err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10052 | u64 objectid; |
Dulshani Gunawardhana | 6787125 | 2013-10-31 10:33:04 +0530 | [diff] [blame] | 10053 | u64 index = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10054 | int name_len; |
| 10055 | int datasize; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 10056 | unsigned long ptr; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10057 | struct btrfs_file_extent_item *ei; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 10058 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10059 | |
Filipe David Borba Manana | f06becc | 2013-09-16 09:53:28 +0100 | [diff] [blame] | 10060 | name_len = strlen(symname); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 10061 | if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info)) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10062 | return -ENAMETOOLONG; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 10063 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 10064 | /* |
| 10065 | * 2 items for inode item and ref |
| 10066 | * 2 items for dir items |
Filipe Manana | 9269d12 | 2015-12-31 18:16:29 +0000 | [diff] [blame] | 10067 | * 1 item for updating parent inode item |
| 10068 | * 1 item for the inline extent item |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 10069 | * 1 item for xattr if selinux is on |
| 10070 | */ |
Filipe Manana | 9269d12 | 2015-12-31 18:16:29 +0000 | [diff] [blame] | 10071 | trans = btrfs_start_transaction(root, 7); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 10072 | if (IS_ERR(trans)) |
| 10073 | return PTR_ERR(trans); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 10074 | |
Nikolay Borisov | 543068a | 2020-12-07 17:32:33 +0200 | [diff] [blame] | 10075 | err = btrfs_get_free_objectid(root, &objectid); |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 10076 | if (err) |
| 10077 | goto out_unlock; |
| 10078 | |
Christian Brauner | 5a05210 | 2021-07-27 12:48:47 +0200 | [diff] [blame] | 10079 | inode = btrfs_new_inode(trans, root, mnt_userns, dir, |
Christian Brauner | b3b6f5b | 2021-07-27 12:48:41 +0200 | [diff] [blame] | 10080 | dentry->d_name.name, dentry->d_name.len, |
| 10081 | btrfs_ino(BTRFS_I(dir)), objectid, |
| 10082 | S_IFLNK | S_IRWXUGO, &index); |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 10083 | if (IS_ERR(inode)) { |
| 10084 | err = PTR_ERR(inode); |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 10085 | inode = NULL; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10086 | goto out_unlock; |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 10087 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10088 | |
Casey Schaufler | ad19db7 | 2011-12-15 10:09:07 -0500 | [diff] [blame] | 10089 | /* |
| 10090 | * If the active LSM wants to access the inode during |
| 10091 | * d_instantiate it needs these. Smack checks to see |
| 10092 | * if the filesystem supports xattrs by looking at the |
| 10093 | * ops vector. |
| 10094 | */ |
| 10095 | inode->i_fop = &btrfs_file_operations; |
| 10096 | inode->i_op = &btrfs_file_inode_operations; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 10097 | inode->i_mapping->a_ops = &btrfs_aops; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 10098 | |
| 10099 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); |
| 10100 | if (err) |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 10101 | goto out_unlock; |
Casey Schaufler | ad19db7 | 2011-12-15 10:09:07 -0500 | [diff] [blame] | 10102 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10103 | path = btrfs_alloc_path(); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 10104 | if (!path) { |
| 10105 | err = -ENOMEM; |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 10106 | goto out_unlock; |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 10107 | } |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 10108 | key.objectid = btrfs_ino(BTRFS_I(inode)); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10109 | key.offset = 0; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 10110 | key.type = BTRFS_EXTENT_DATA_KEY; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10111 | datasize = btrfs_file_extent_calc_inline_size(name_len); |
| 10112 | err = btrfs_insert_empty_item(trans, root, path, &key, |
| 10113 | datasize); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 10114 | if (err) { |
Julia Lawall | b083916 | 2011-05-14 07:10:51 +0000 | [diff] [blame] | 10115 | btrfs_free_path(path); |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 10116 | goto out_unlock; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 10117 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 10118 | leaf = path->nodes[0]; |
| 10119 | ei = btrfs_item_ptr(leaf, path->slots[0], |
| 10120 | struct btrfs_file_extent_item); |
| 10121 | btrfs_set_file_extent_generation(leaf, ei, trans->transid); |
| 10122 | btrfs_set_file_extent_type(leaf, ei, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10123 | BTRFS_FILE_EXTENT_INLINE); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 10124 | btrfs_set_file_extent_encryption(leaf, ei, 0); |
| 10125 | btrfs_set_file_extent_compression(leaf, ei, 0); |
| 10126 | btrfs_set_file_extent_other_encoding(leaf, ei, 0); |
| 10127 | btrfs_set_file_extent_ram_bytes(leaf, ei, name_len); |
| 10128 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10129 | ptr = btrfs_file_extent_inline_start(ei); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 10130 | write_extent_buffer(leaf, symname, ptr, name_len); |
| 10131 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10132 | btrfs_free_path(path); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 10133 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10134 | inode->i_op = &btrfs_symlink_inode_operations; |
Al Viro | 21fc61c | 2015-11-17 01:07:57 -0500 | [diff] [blame] | 10135 | inode_nohighmem(inode); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 10136 | inode_set_bytes(inode, name_len); |
Nikolay Borisov | 6ef06d2 | 2017-02-20 13:50:34 +0200 | [diff] [blame] | 10137 | btrfs_i_size_write(BTRFS_I(inode), name_len); |
Nikolay Borisov | 9a56fcd | 2020-11-02 16:48:59 +0200 | [diff] [blame] | 10138 | err = btrfs_update_inode(trans, root, BTRFS_I(inode)); |
Filipe Manana | d50866d | 2015-12-31 18:08:24 +0000 | [diff] [blame] | 10139 | /* |
| 10140 | * Last step, add directory indexes for our symlink inode. This is the |
| 10141 | * last step to avoid extra cleanup of these indexes if an error happens |
| 10142 | * elsewhere above. |
| 10143 | */ |
| 10144 | if (!err) |
Nikolay Borisov | cef415a | 2017-02-20 13:51:09 +0200 | [diff] [blame] | 10145 | err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, |
| 10146 | BTRFS_I(inode), 0, index); |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 10147 | if (err) |
| 10148 | goto out_unlock; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 10149 | |
Al Viro | 1e2e547 | 2018-05-04 08:23:01 -0400 | [diff] [blame] | 10150 | d_instantiate_new(dentry, inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10151 | |
| 10152 | out_unlock: |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 10153 | btrfs_end_transaction(trans); |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 10154 | if (err && inode) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10155 | inode_dec_link_count(inode); |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 10156 | discard_new_inode(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10157 | } |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 10158 | btrfs_btree_balance_dirty(fs_info); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10159 | return err; |
| 10160 | } |
Chris Mason | 1643298 | 2008-04-10 10:23:21 -0400 | [diff] [blame] | 10161 | |
Filipe Manana | 8fccebf | 2020-09-08 11:27:20 +0100 | [diff] [blame] | 10162 | static struct btrfs_trans_handle *insert_prealloc_file_extent( |
| 10163 | struct btrfs_trans_handle *trans_in, |
Nikolay Borisov | 90dffd0 | 2020-11-02 16:48:54 +0200 | [diff] [blame] | 10164 | struct btrfs_inode *inode, |
| 10165 | struct btrfs_key *ins, |
Qu Wenruo | 203f44c5 | 2020-06-10 09:04:40 +0800 | [diff] [blame] | 10166 | u64 file_offset) |
| 10167 | { |
| 10168 | struct btrfs_file_extent_item stack_fi; |
Filipe Manana | bf38564 | 2020-09-08 11:27:22 +0100 | [diff] [blame] | 10169 | struct btrfs_replace_extent_info extent_info; |
Filipe Manana | 8fccebf | 2020-09-08 11:27:20 +0100 | [diff] [blame] | 10170 | struct btrfs_trans_handle *trans = trans_in; |
| 10171 | struct btrfs_path *path; |
Qu Wenruo | 203f44c5 | 2020-06-10 09:04:40 +0800 | [diff] [blame] | 10172 | u64 start = ins->objectid; |
| 10173 | u64 len = ins->offset; |
Qu Wenruo | fbf48bb | 2021-03-03 18:41:51 +0800 | [diff] [blame] | 10174 | int qgroup_released; |
Qu Wenruo | 9729f10 | 2020-06-10 09:04:41 +0800 | [diff] [blame] | 10175 | int ret; |
Qu Wenruo | 203f44c5 | 2020-06-10 09:04:40 +0800 | [diff] [blame] | 10176 | |
| 10177 | memset(&stack_fi, 0, sizeof(stack_fi)); |
| 10178 | |
| 10179 | btrfs_set_stack_file_extent_type(&stack_fi, BTRFS_FILE_EXTENT_PREALLOC); |
| 10180 | btrfs_set_stack_file_extent_disk_bytenr(&stack_fi, start); |
| 10181 | btrfs_set_stack_file_extent_disk_num_bytes(&stack_fi, len); |
| 10182 | btrfs_set_stack_file_extent_num_bytes(&stack_fi, len); |
| 10183 | btrfs_set_stack_file_extent_ram_bytes(&stack_fi, len); |
| 10184 | btrfs_set_stack_file_extent_compression(&stack_fi, BTRFS_COMPRESS_NONE); |
| 10185 | /* Encryption and other encoding is reserved and all 0 */ |
| 10186 | |
Qu Wenruo | fbf48bb | 2021-03-03 18:41:51 +0800 | [diff] [blame] | 10187 | qgroup_released = btrfs_qgroup_release_data(inode, file_offset, len); |
| 10188 | if (qgroup_released < 0) |
| 10189 | return ERR_PTR(qgroup_released); |
Filipe Manana | 8fccebf | 2020-09-08 11:27:20 +0100 | [diff] [blame] | 10190 | |
| 10191 | if (trans) { |
Nikolay Borisov | 90dffd0 | 2020-11-02 16:48:54 +0200 | [diff] [blame] | 10192 | ret = insert_reserved_file_extent(trans, inode, |
Filipe Manana | 2766ff6 | 2020-11-04 11:07:34 +0000 | [diff] [blame] | 10193 | file_offset, &stack_fi, |
Qu Wenruo | fbf48bb | 2021-03-03 18:41:51 +0800 | [diff] [blame] | 10194 | true, qgroup_released); |
Filipe Manana | 8fccebf | 2020-09-08 11:27:20 +0100 | [diff] [blame] | 10195 | if (ret) |
Qu Wenruo | a3ee79b | 2021-03-03 18:41:52 +0800 | [diff] [blame] | 10196 | goto free_qgroup; |
Filipe Manana | 8fccebf | 2020-09-08 11:27:20 +0100 | [diff] [blame] | 10197 | return trans; |
| 10198 | } |
| 10199 | |
| 10200 | extent_info.disk_offset = start; |
| 10201 | extent_info.disk_len = len; |
| 10202 | extent_info.data_offset = 0; |
| 10203 | extent_info.data_len = len; |
| 10204 | extent_info.file_offset = file_offset; |
| 10205 | extent_info.extent_buf = (char *)&stack_fi; |
Filipe Manana | 8fccebf | 2020-09-08 11:27:20 +0100 | [diff] [blame] | 10206 | extent_info.is_new_extent = true; |
Qu Wenruo | fbf48bb | 2021-03-03 18:41:51 +0800 | [diff] [blame] | 10207 | extent_info.qgroup_reserved = qgroup_released; |
Filipe Manana | 8fccebf | 2020-09-08 11:27:20 +0100 | [diff] [blame] | 10208 | extent_info.insertions = 0; |
| 10209 | |
| 10210 | path = btrfs_alloc_path(); |
Qu Wenruo | a3ee79b | 2021-03-03 18:41:52 +0800 | [diff] [blame] | 10211 | if (!path) { |
| 10212 | ret = -ENOMEM; |
| 10213 | goto free_qgroup; |
| 10214 | } |
Filipe Manana | 8fccebf | 2020-09-08 11:27:20 +0100 | [diff] [blame] | 10215 | |
Nikolay Borisov | bfc7847 | 2021-02-17 15:12:47 +0200 | [diff] [blame] | 10216 | ret = btrfs_replace_file_extents(inode, path, file_offset, |
Filipe Manana | 8fccebf | 2020-09-08 11:27:20 +0100 | [diff] [blame] | 10217 | file_offset + len - 1, &extent_info, |
| 10218 | &trans); |
| 10219 | btrfs_free_path(path); |
| 10220 | if (ret) |
Qu Wenruo | a3ee79b | 2021-03-03 18:41:52 +0800 | [diff] [blame] | 10221 | goto free_qgroup; |
Filipe Manana | 8fccebf | 2020-09-08 11:27:20 +0100 | [diff] [blame] | 10222 | return trans; |
Qu Wenruo | a3ee79b | 2021-03-03 18:41:52 +0800 | [diff] [blame] | 10223 | |
| 10224 | free_qgroup: |
| 10225 | /* |
| 10226 | * We have released qgroup data range at the beginning of the function, |
| 10227 | * and normally qgroup_released bytes will be freed when committing |
| 10228 | * transaction. |
| 10229 | * But if we error out early, we have to free what we have released |
| 10230 | * or we leak qgroup data reservation. |
| 10231 | */ |
| 10232 | btrfs_qgroup_free_refroot(inode->root->fs_info, |
| 10233 | inode->root->root_key.objectid, qgroup_released, |
| 10234 | BTRFS_QGROUP_RSV_DATA); |
| 10235 | return ERR_PTR(ret); |
Qu Wenruo | 203f44c5 | 2020-06-10 09:04:40 +0800 | [diff] [blame] | 10236 | } |
Filipe Manana | 8fccebf | 2020-09-08 11:27:20 +0100 | [diff] [blame] | 10237 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 10238 | static int __btrfs_prealloc_file_range(struct inode *inode, int mode, |
| 10239 | u64 start, u64 num_bytes, u64 min_size, |
| 10240 | loff_t actual_len, u64 *alloc_hint, |
| 10241 | struct btrfs_trans_handle *trans) |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 10242 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 10243 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 10244 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
| 10245 | struct extent_map *em; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 10246 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 10247 | struct btrfs_key ins; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 10248 | u64 cur_offset = start; |
Josef Bacik | b778cf9 | 2020-02-13 10:47:31 -0500 | [diff] [blame] | 10249 | u64 clear_offset = start; |
Josef Bacik | 55a61d1 | 2010-11-22 18:50:32 +0000 | [diff] [blame] | 10250 | u64 i_size; |
Chris Mason | 154ea28 | 2013-03-05 11:11:26 -0500 | [diff] [blame] | 10251 | u64 cur_bytes; |
Josef Bacik | 0b670dc | 2015-09-23 17:11:16 -0400 | [diff] [blame] | 10252 | u64 last_alloc = (u64)-1; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 10253 | int ret = 0; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 10254 | bool own_trans = true; |
Wang Xiaoguang | 1851309 | 2016-07-25 15:51:40 +0800 | [diff] [blame] | 10255 | u64 end = start + num_bytes - 1; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 10256 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 10257 | if (trans) |
| 10258 | own_trans = false; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 10259 | while (num_bytes > 0) { |
Byongho Lee | ee22184 | 2015-12-15 01:42:10 +0900 | [diff] [blame] | 10260 | cur_bytes = min_t(u64, num_bytes, SZ_256M); |
Chris Mason | 154ea28 | 2013-03-05 11:11:26 -0500 | [diff] [blame] | 10261 | cur_bytes = max(cur_bytes, min_size); |
Josef Bacik | 0b670dc | 2015-09-23 17:11:16 -0400 | [diff] [blame] | 10262 | /* |
| 10263 | * If we are severely fragmented we could end up with really |
| 10264 | * small allocations, so if the allocator is returning small |
| 10265 | * chunks lets make its job easier by only searching for those |
| 10266 | * sized chunks. |
| 10267 | */ |
| 10268 | cur_bytes = min(cur_bytes, last_alloc); |
Wang Xiaoguang | 1851309 | 2016-07-25 15:51:40 +0800 | [diff] [blame] | 10269 | ret = btrfs_reserve_extent(root, cur_bytes, cur_bytes, |
| 10270 | min_size, 0, *alloc_hint, &ins, 1, 0); |
Filipe Manana | 8fccebf | 2020-09-08 11:27:20 +0100 | [diff] [blame] | 10271 | if (ret) |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 10272 | break; |
Josef Bacik | b778cf9 | 2020-02-13 10:47:31 -0500 | [diff] [blame] | 10273 | |
| 10274 | /* |
| 10275 | * We've reserved this space, and thus converted it from |
| 10276 | * ->bytes_may_use to ->bytes_reserved. Any error that happens |
| 10277 | * from here on out we will only need to clear our reservation |
| 10278 | * for the remaining unreserved area, so advance our |
| 10279 | * clear_offset by our extent size. |
| 10280 | */ |
| 10281 | clear_offset += ins.offset; |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 10282 | |
Josef Bacik | 0b670dc | 2015-09-23 17:11:16 -0400 | [diff] [blame] | 10283 | last_alloc = ins.offset; |
Nikolay Borisov | 90dffd0 | 2020-11-02 16:48:54 +0200 | [diff] [blame] | 10284 | trans = insert_prealloc_file_extent(trans, BTRFS_I(inode), |
| 10285 | &ins, cur_offset); |
Filipe Manana | 1afc708 | 2020-10-14 10:10:36 +0100 | [diff] [blame] | 10286 | /* |
| 10287 | * Now that we inserted the prealloc extent we can finally |
| 10288 | * decrement the number of reservations in the block group. |
| 10289 | * If we did it before, we could race with relocation and have |
| 10290 | * relocation miss the reserved extent, making it fail later. |
| 10291 | */ |
| 10292 | btrfs_dec_block_group_reservations(fs_info, ins.objectid); |
Filipe Manana | 8fccebf | 2020-09-08 11:27:20 +0100 | [diff] [blame] | 10293 | if (IS_ERR(trans)) { |
| 10294 | ret = PTR_ERR(trans); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 10295 | btrfs_free_reserved_extent(fs_info, ins.objectid, |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 10296 | ins.offset, 0); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 10297 | break; |
| 10298 | } |
Dongsheng Yang | 3119321 | 2014-12-12 16:44:35 +0800 | [diff] [blame] | 10299 | |
Nikolay Borisov | dcdbc05 | 2017-02-20 13:50:45 +0200 | [diff] [blame] | 10300 | btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset, |
Chris Mason | a1ed835 | 2009-09-11 12:27:37 -0400 | [diff] [blame] | 10301 | cur_offset + ins.offset -1, 0); |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 10302 | |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 10303 | em = alloc_extent_map(); |
| 10304 | if (!em) { |
| 10305 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, |
| 10306 | &BTRFS_I(inode)->runtime_flags); |
| 10307 | goto next; |
| 10308 | } |
| 10309 | |
| 10310 | em->start = cur_offset; |
| 10311 | em->orig_start = cur_offset; |
| 10312 | em->len = ins.offset; |
| 10313 | em->block_start = ins.objectid; |
| 10314 | em->block_len = ins.offset; |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 10315 | em->orig_block_len = ins.offset; |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 10316 | em->ram_bytes = ins.offset; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 10317 | set_bit(EXTENT_FLAG_PREALLOC, &em->flags); |
| 10318 | em->generation = trans->transid; |
| 10319 | |
| 10320 | while (1) { |
| 10321 | write_lock(&em_tree->lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 10322 | ret = add_extent_mapping(em_tree, em, 1); |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 10323 | write_unlock(&em_tree->lock); |
| 10324 | if (ret != -EEXIST) |
| 10325 | break; |
Nikolay Borisov | dcdbc05 | 2017-02-20 13:50:45 +0200 | [diff] [blame] | 10326 | btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset, |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 10327 | cur_offset + ins.offset - 1, |
| 10328 | 0); |
| 10329 | } |
| 10330 | free_extent_map(em); |
| 10331 | next: |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 10332 | num_bytes -= ins.offset; |
| 10333 | cur_offset += ins.offset; |
Yan, Zheng | efa5646 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 10334 | *alloc_hint = ins.objectid + ins.offset; |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 10335 | |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 10336 | inode_inc_iversion(inode); |
Deepa Dinamani | c2050a4 | 2016-09-14 07:48:06 -0700 | [diff] [blame] | 10337 | inode->i_ctime = current_time(inode); |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 10338 | BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 10339 | if (!(mode & FALLOC_FL_KEEP_SIZE) && |
Yan, Zheng | efa5646 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 10340 | (actual_len > inode->i_size) && |
| 10341 | (cur_offset > inode->i_size)) { |
Aneesh Kumar K.V | d1ea6a6 | 2010-01-20 07:28:54 +0000 | [diff] [blame] | 10342 | if (cur_offset > actual_len) |
Josef Bacik | 55a61d1 | 2010-11-22 18:50:32 +0000 | [diff] [blame] | 10343 | i_size = actual_len; |
Aneesh Kumar K.V | d1ea6a6 | 2010-01-20 07:28:54 +0000 | [diff] [blame] | 10344 | else |
Josef Bacik | 55a61d1 | 2010-11-22 18:50:32 +0000 | [diff] [blame] | 10345 | i_size = cur_offset; |
| 10346 | i_size_write(inode, i_size); |
Nikolay Borisov | 76aea53 | 2020-11-02 16:48:53 +0200 | [diff] [blame] | 10347 | btrfs_inode_safe_disk_i_size_write(BTRFS_I(inode), 0); |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 10348 | } |
| 10349 | |
Nikolay Borisov | 9a56fcd | 2020-11-02 16:48:59 +0200 | [diff] [blame] | 10350 | ret = btrfs_update_inode(trans, root, BTRFS_I(inode)); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 10351 | |
| 10352 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 10353 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 10354 | if (own_trans) |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 10355 | btrfs_end_transaction(trans); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 10356 | break; |
| 10357 | } |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 10358 | |
Filipe Manana | 8fccebf | 2020-09-08 11:27:20 +0100 | [diff] [blame] | 10359 | if (own_trans) { |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 10360 | btrfs_end_transaction(trans); |
Filipe Manana | 8fccebf | 2020-09-08 11:27:20 +0100 | [diff] [blame] | 10361 | trans = NULL; |
| 10362 | } |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 10363 | } |
Josef Bacik | b778cf9 | 2020-02-13 10:47:31 -0500 | [diff] [blame] | 10364 | if (clear_offset < end) |
Nikolay Borisov | 25ce28c | 2020-06-03 08:55:39 +0300 | [diff] [blame] | 10365 | btrfs_free_reserved_data_space(BTRFS_I(inode), NULL, clear_offset, |
Josef Bacik | b778cf9 | 2020-02-13 10:47:31 -0500 | [diff] [blame] | 10366 | end - clear_offset + 1); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 10367 | return ret; |
| 10368 | } |
| 10369 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 10370 | int btrfs_prealloc_file_range(struct inode *inode, int mode, |
| 10371 | u64 start, u64 num_bytes, u64 min_size, |
| 10372 | loff_t actual_len, u64 *alloc_hint) |
| 10373 | { |
| 10374 | return __btrfs_prealloc_file_range(inode, mode, start, num_bytes, |
| 10375 | min_size, actual_len, alloc_hint, |
| 10376 | NULL); |
| 10377 | } |
| 10378 | |
| 10379 | int btrfs_prealloc_file_range_trans(struct inode *inode, |
| 10380 | struct btrfs_trans_handle *trans, int mode, |
| 10381 | u64 start, u64 num_bytes, u64 min_size, |
| 10382 | loff_t actual_len, u64 *alloc_hint) |
| 10383 | { |
| 10384 | return __btrfs_prealloc_file_range(inode, mode, start, num_bytes, |
| 10385 | min_size, actual_len, alloc_hint, trans); |
| 10386 | } |
| 10387 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 10388 | static int btrfs_set_page_dirty(struct page *page) |
| 10389 | { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 10390 | return __set_page_dirty_nobuffers(page); |
| 10391 | } |
| 10392 | |
Christian Brauner | 549c729 | 2021-01-21 14:19:43 +0100 | [diff] [blame] | 10393 | static int btrfs_permission(struct user_namespace *mnt_userns, |
| 10394 | struct inode *inode, int mask) |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 10395 | { |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 10396 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Jeff Mahoney | cb6db4e | 2011-08-15 17:27:21 +0000 | [diff] [blame] | 10397 | umode_t mode = inode->i_mode; |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 10398 | |
Jeff Mahoney | cb6db4e | 2011-08-15 17:27:21 +0000 | [diff] [blame] | 10399 | if (mask & MAY_WRITE && |
| 10400 | (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) { |
| 10401 | if (btrfs_root_readonly(root)) |
| 10402 | return -EROFS; |
| 10403 | if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY) |
| 10404 | return -EACCES; |
| 10405 | } |
Christian Brauner | 47291ba | 2021-01-21 14:19:24 +0100 | [diff] [blame] | 10406 | return generic_permission(&init_user_ns, inode, mask); |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 10407 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10408 | |
Christian Brauner | 549c729 | 2021-01-21 14:19:43 +0100 | [diff] [blame] | 10409 | static int btrfs_tmpfile(struct user_namespace *mnt_userns, struct inode *dir, |
| 10410 | struct dentry *dentry, umode_t mode) |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 10411 | { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 10412 | struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 10413 | struct btrfs_trans_handle *trans; |
| 10414 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 10415 | struct inode *inode = NULL; |
| 10416 | u64 objectid; |
| 10417 | u64 index; |
| 10418 | int ret = 0; |
| 10419 | |
| 10420 | /* |
| 10421 | * 5 units required for adding orphan entry |
| 10422 | */ |
| 10423 | trans = btrfs_start_transaction(root, 5); |
| 10424 | if (IS_ERR(trans)) |
| 10425 | return PTR_ERR(trans); |
| 10426 | |
Nikolay Borisov | 543068a | 2020-12-07 17:32:33 +0200 | [diff] [blame] | 10427 | ret = btrfs_get_free_objectid(root, &objectid); |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 10428 | if (ret) |
| 10429 | goto out; |
| 10430 | |
Christian Brauner | 98b6ab5 | 2021-07-27 12:48:48 +0200 | [diff] [blame] | 10431 | inode = btrfs_new_inode(trans, root, mnt_userns, dir, NULL, 0, |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 10432 | btrfs_ino(BTRFS_I(dir)), objectid, mode, &index); |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 10433 | if (IS_ERR(inode)) { |
| 10434 | ret = PTR_ERR(inode); |
| 10435 | inode = NULL; |
| 10436 | goto out; |
| 10437 | } |
| 10438 | |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 10439 | inode->i_fop = &btrfs_file_operations; |
| 10440 | inode->i_op = &btrfs_file_inode_operations; |
| 10441 | |
| 10442 | inode->i_mapping->a_ops = &btrfs_aops; |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 10443 | |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 10444 | ret = btrfs_init_inode_security(trans, inode, dir, NULL); |
| 10445 | if (ret) |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 10446 | goto out; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 10447 | |
Nikolay Borisov | 9a56fcd | 2020-11-02 16:48:59 +0200 | [diff] [blame] | 10448 | ret = btrfs_update_inode(trans, root, BTRFS_I(inode)); |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 10449 | if (ret) |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 10450 | goto out; |
Nikolay Borisov | 73f2e54 | 2017-02-20 13:50:59 +0200 | [diff] [blame] | 10451 | ret = btrfs_orphan_add(trans, BTRFS_I(inode)); |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 10452 | if (ret) |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 10453 | goto out; |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 10454 | |
Filipe Manana | 5762b5c | 2014-08-01 00:10:32 +0100 | [diff] [blame] | 10455 | /* |
| 10456 | * We set number of links to 0 in btrfs_new_inode(), and here we set |
| 10457 | * it to 1 because d_tmpfile() will issue a warning if the count is 0, |
| 10458 | * through: |
| 10459 | * |
| 10460 | * d_tmpfile() -> inode_dec_link_count() -> drop_nlink() |
| 10461 | */ |
| 10462 | set_nlink(inode, 1); |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 10463 | d_tmpfile(dentry, inode); |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 10464 | unlock_new_inode(inode); |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 10465 | mark_inode_dirty(inode); |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 10466 | out: |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 10467 | btrfs_end_transaction(trans); |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 10468 | if (ret && inode) |
| 10469 | discard_new_inode(inode); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 10470 | btrfs_btree_balance_dirty(fs_info); |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 10471 | return ret; |
| 10472 | } |
| 10473 | |
Qu Wenruo | d2a9106 | 2021-05-31 16:50:49 +0800 | [diff] [blame] | 10474 | void btrfs_set_range_writeback(struct btrfs_inode *inode, u64 start, u64 end) |
Josef Bacik | c6100a4 | 2017-05-05 11:57:13 -0400 | [diff] [blame] | 10475 | { |
Qu Wenruo | d2a9106 | 2021-05-31 16:50:49 +0800 | [diff] [blame] | 10476 | struct btrfs_fs_info *fs_info = inode->root->fs_info; |
Josef Bacik | c6100a4 | 2017-05-05 11:57:13 -0400 | [diff] [blame] | 10477 | unsigned long index = start >> PAGE_SHIFT; |
| 10478 | unsigned long end_index = end >> PAGE_SHIFT; |
| 10479 | struct page *page; |
Qu Wenruo | d2a9106 | 2021-05-31 16:50:49 +0800 | [diff] [blame] | 10480 | u32 len; |
Josef Bacik | c6100a4 | 2017-05-05 11:57:13 -0400 | [diff] [blame] | 10481 | |
Qu Wenruo | d2a9106 | 2021-05-31 16:50:49 +0800 | [diff] [blame] | 10482 | ASSERT(end + 1 - start <= U32_MAX); |
| 10483 | len = end + 1 - start; |
Josef Bacik | c6100a4 | 2017-05-05 11:57:13 -0400 | [diff] [blame] | 10484 | while (index <= end_index) { |
Qu Wenruo | d2a9106 | 2021-05-31 16:50:49 +0800 | [diff] [blame] | 10485 | page = find_get_page(inode->vfs_inode.i_mapping, index); |
Josef Bacik | c6100a4 | 2017-05-05 11:57:13 -0400 | [diff] [blame] | 10486 | ASSERT(page); /* Pages should be in the extent_io_tree */ |
Qu Wenruo | d2a9106 | 2021-05-31 16:50:49 +0800 | [diff] [blame] | 10487 | |
| 10488 | btrfs_page_set_writeback(fs_info, page, start, len); |
Josef Bacik | c6100a4 | 2017-05-05 11:57:13 -0400 | [diff] [blame] | 10489 | put_page(page); |
| 10490 | index++; |
| 10491 | } |
| 10492 | } |
| 10493 | |
Omar Sandoval | ed46ff3 | 2016-11-03 10:28:14 -0700 | [diff] [blame] | 10494 | #ifdef CONFIG_SWAP |
| 10495 | /* |
| 10496 | * Add an entry indicating a block group or device which is pinned by a |
| 10497 | * swapfile. Returns 0 on success, 1 if there is already an entry for it, or a |
| 10498 | * negative errno on failure. |
| 10499 | */ |
| 10500 | static int btrfs_add_swapfile_pin(struct inode *inode, void *ptr, |
| 10501 | bool is_block_group) |
| 10502 | { |
| 10503 | struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info; |
| 10504 | struct btrfs_swapfile_pin *sp, *entry; |
| 10505 | struct rb_node **p; |
| 10506 | struct rb_node *parent = NULL; |
| 10507 | |
| 10508 | sp = kmalloc(sizeof(*sp), GFP_NOFS); |
| 10509 | if (!sp) |
| 10510 | return -ENOMEM; |
| 10511 | sp->ptr = ptr; |
| 10512 | sp->inode = inode; |
| 10513 | sp->is_block_group = is_block_group; |
Filipe Manana | 195a49e | 2021-02-05 12:55:37 +0000 | [diff] [blame] | 10514 | sp->bg_extent_count = 1; |
Omar Sandoval | ed46ff3 | 2016-11-03 10:28:14 -0700 | [diff] [blame] | 10515 | |
| 10516 | spin_lock(&fs_info->swapfile_pins_lock); |
| 10517 | p = &fs_info->swapfile_pins.rb_node; |
| 10518 | while (*p) { |
| 10519 | parent = *p; |
| 10520 | entry = rb_entry(parent, struct btrfs_swapfile_pin, node); |
| 10521 | if (sp->ptr < entry->ptr || |
| 10522 | (sp->ptr == entry->ptr && sp->inode < entry->inode)) { |
| 10523 | p = &(*p)->rb_left; |
| 10524 | } else if (sp->ptr > entry->ptr || |
| 10525 | (sp->ptr == entry->ptr && sp->inode > entry->inode)) { |
| 10526 | p = &(*p)->rb_right; |
| 10527 | } else { |
Filipe Manana | 195a49e | 2021-02-05 12:55:37 +0000 | [diff] [blame] | 10528 | if (is_block_group) |
| 10529 | entry->bg_extent_count++; |
Omar Sandoval | ed46ff3 | 2016-11-03 10:28:14 -0700 | [diff] [blame] | 10530 | spin_unlock(&fs_info->swapfile_pins_lock); |
| 10531 | kfree(sp); |
| 10532 | return 1; |
| 10533 | } |
| 10534 | } |
| 10535 | rb_link_node(&sp->node, parent, p); |
| 10536 | rb_insert_color(&sp->node, &fs_info->swapfile_pins); |
| 10537 | spin_unlock(&fs_info->swapfile_pins_lock); |
| 10538 | return 0; |
| 10539 | } |
| 10540 | |
| 10541 | /* Free all of the entries pinned by this swapfile. */ |
| 10542 | static void btrfs_free_swapfile_pins(struct inode *inode) |
| 10543 | { |
| 10544 | struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info; |
| 10545 | struct btrfs_swapfile_pin *sp; |
| 10546 | struct rb_node *node, *next; |
| 10547 | |
| 10548 | spin_lock(&fs_info->swapfile_pins_lock); |
| 10549 | node = rb_first(&fs_info->swapfile_pins); |
| 10550 | while (node) { |
| 10551 | next = rb_next(node); |
| 10552 | sp = rb_entry(node, struct btrfs_swapfile_pin, node); |
| 10553 | if (sp->inode == inode) { |
| 10554 | rb_erase(&sp->node, &fs_info->swapfile_pins); |
Filipe Manana | 195a49e | 2021-02-05 12:55:37 +0000 | [diff] [blame] | 10555 | if (sp->is_block_group) { |
| 10556 | btrfs_dec_block_group_swap_extents(sp->ptr, |
| 10557 | sp->bg_extent_count); |
Omar Sandoval | ed46ff3 | 2016-11-03 10:28:14 -0700 | [diff] [blame] | 10558 | btrfs_put_block_group(sp->ptr); |
Filipe Manana | 195a49e | 2021-02-05 12:55:37 +0000 | [diff] [blame] | 10559 | } |
Omar Sandoval | ed46ff3 | 2016-11-03 10:28:14 -0700 | [diff] [blame] | 10560 | kfree(sp); |
| 10561 | } |
| 10562 | node = next; |
| 10563 | } |
| 10564 | spin_unlock(&fs_info->swapfile_pins_lock); |
| 10565 | } |
| 10566 | |
| 10567 | struct btrfs_swap_info { |
| 10568 | u64 start; |
| 10569 | u64 block_start; |
| 10570 | u64 block_len; |
| 10571 | u64 lowest_ppage; |
| 10572 | u64 highest_ppage; |
| 10573 | unsigned long nr_pages; |
| 10574 | int nr_extents; |
| 10575 | }; |
| 10576 | |
| 10577 | static int btrfs_add_swap_extent(struct swap_info_struct *sis, |
| 10578 | struct btrfs_swap_info *bsi) |
| 10579 | { |
| 10580 | unsigned long nr_pages; |
| 10581 | u64 first_ppage, first_ppage_reported, next_ppage; |
| 10582 | int ret; |
| 10583 | |
| 10584 | first_ppage = ALIGN(bsi->block_start, PAGE_SIZE) >> PAGE_SHIFT; |
| 10585 | next_ppage = ALIGN_DOWN(bsi->block_start + bsi->block_len, |
| 10586 | PAGE_SIZE) >> PAGE_SHIFT; |
| 10587 | |
| 10588 | if (first_ppage >= next_ppage) |
| 10589 | return 0; |
| 10590 | nr_pages = next_ppage - first_ppage; |
| 10591 | |
| 10592 | first_ppage_reported = first_ppage; |
| 10593 | if (bsi->start == 0) |
| 10594 | first_ppage_reported++; |
| 10595 | if (bsi->lowest_ppage > first_ppage_reported) |
| 10596 | bsi->lowest_ppage = first_ppage_reported; |
| 10597 | if (bsi->highest_ppage < (next_ppage - 1)) |
| 10598 | bsi->highest_ppage = next_ppage - 1; |
| 10599 | |
| 10600 | ret = add_swap_extent(sis, bsi->nr_pages, nr_pages, first_ppage); |
| 10601 | if (ret < 0) |
| 10602 | return ret; |
| 10603 | bsi->nr_extents += ret; |
| 10604 | bsi->nr_pages += nr_pages; |
| 10605 | return 0; |
| 10606 | } |
| 10607 | |
| 10608 | static void btrfs_swap_deactivate(struct file *file) |
| 10609 | { |
| 10610 | struct inode *inode = file_inode(file); |
| 10611 | |
| 10612 | btrfs_free_swapfile_pins(inode); |
| 10613 | atomic_dec(&BTRFS_I(inode)->root->nr_swapfiles); |
| 10614 | } |
| 10615 | |
| 10616 | static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file, |
| 10617 | sector_t *span) |
| 10618 | { |
| 10619 | struct inode *inode = file_inode(file); |
Filipe Manana | dd0734f | 2021-02-05 12:55:38 +0000 | [diff] [blame] | 10620 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 10621 | struct btrfs_fs_info *fs_info = root->fs_info; |
Omar Sandoval | ed46ff3 | 2016-11-03 10:28:14 -0700 | [diff] [blame] | 10622 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
| 10623 | struct extent_state *cached_state = NULL; |
| 10624 | struct extent_map *em = NULL; |
| 10625 | struct btrfs_device *device = NULL; |
| 10626 | struct btrfs_swap_info bsi = { |
| 10627 | .lowest_ppage = (sector_t)-1ULL, |
| 10628 | }; |
| 10629 | int ret = 0; |
| 10630 | u64 isize; |
| 10631 | u64 start; |
| 10632 | |
| 10633 | /* |
| 10634 | * If the swap file was just created, make sure delalloc is done. If the |
| 10635 | * file changes again after this, the user is doing something stupid and |
| 10636 | * we don't really care. |
| 10637 | */ |
| 10638 | ret = btrfs_wait_ordered_range(inode, 0, (u64)-1); |
| 10639 | if (ret) |
| 10640 | return ret; |
| 10641 | |
| 10642 | /* |
| 10643 | * The inode is locked, so these flags won't change after we check them. |
| 10644 | */ |
| 10645 | if (BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS) { |
| 10646 | btrfs_warn(fs_info, "swapfile must not be compressed"); |
| 10647 | return -EINVAL; |
| 10648 | } |
| 10649 | if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW)) { |
| 10650 | btrfs_warn(fs_info, "swapfile must not be copy-on-write"); |
| 10651 | return -EINVAL; |
| 10652 | } |
| 10653 | if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) { |
| 10654 | btrfs_warn(fs_info, "swapfile must not be checksummed"); |
| 10655 | return -EINVAL; |
| 10656 | } |
| 10657 | |
| 10658 | /* |
| 10659 | * Balance or device remove/replace/resize can move stuff around from |
Goldwyn Rodrigues | c3e1f96 | 2020-08-25 10:02:32 -0500 | [diff] [blame] | 10660 | * under us. The exclop protection makes sure they aren't running/won't |
| 10661 | * run concurrently while we are mapping the swap extents, and |
| 10662 | * fs_info->swapfile_pins prevents them from running while the swap |
| 10663 | * file is active and moving the extents. Note that this also prevents |
| 10664 | * a concurrent device add which isn't actually necessary, but it's not |
Omar Sandoval | ed46ff3 | 2016-11-03 10:28:14 -0700 | [diff] [blame] | 10665 | * really worth the trouble to allow it. |
| 10666 | */ |
Goldwyn Rodrigues | c3e1f96 | 2020-08-25 10:02:32 -0500 | [diff] [blame] | 10667 | if (!btrfs_exclop_start(fs_info, BTRFS_EXCLOP_SWAP_ACTIVATE)) { |
Omar Sandoval | ed46ff3 | 2016-11-03 10:28:14 -0700 | [diff] [blame] | 10668 | btrfs_warn(fs_info, |
| 10669 | "cannot activate swapfile while exclusive operation is running"); |
| 10670 | return -EBUSY; |
| 10671 | } |
Filipe Manana | dd0734f | 2021-02-05 12:55:38 +0000 | [diff] [blame] | 10672 | |
| 10673 | /* |
| 10674 | * Prevent snapshot creation while we are activating the swap file. |
| 10675 | * We do not want to race with snapshot creation. If snapshot creation |
| 10676 | * already started before we bumped nr_swapfiles from 0 to 1 and |
| 10677 | * completes before the first write into the swap file after it is |
| 10678 | * activated, than that write would fallback to COW. |
| 10679 | */ |
| 10680 | if (!btrfs_drew_try_write_lock(&root->snapshot_lock)) { |
| 10681 | btrfs_exclop_finish(fs_info); |
| 10682 | btrfs_warn(fs_info, |
| 10683 | "cannot activate swapfile because snapshot creation is in progress"); |
| 10684 | return -EINVAL; |
| 10685 | } |
Omar Sandoval | ed46ff3 | 2016-11-03 10:28:14 -0700 | [diff] [blame] | 10686 | /* |
| 10687 | * Snapshots can create extents which require COW even if NODATACOW is |
| 10688 | * set. We use this counter to prevent snapshots. We must increment it |
| 10689 | * before walking the extents because we don't want a concurrent |
| 10690 | * snapshot to run after we've already checked the extents. |
| 10691 | */ |
Filipe Manana | dd0734f | 2021-02-05 12:55:38 +0000 | [diff] [blame] | 10692 | atomic_inc(&root->nr_swapfiles); |
Omar Sandoval | ed46ff3 | 2016-11-03 10:28:14 -0700 | [diff] [blame] | 10693 | |
| 10694 | isize = ALIGN_DOWN(inode->i_size, fs_info->sectorsize); |
| 10695 | |
| 10696 | lock_extent_bits(io_tree, 0, isize - 1, &cached_state); |
| 10697 | start = 0; |
| 10698 | while (start < isize) { |
| 10699 | u64 logical_block_start, physical_block_start; |
David Sterba | 32da5386 | 2019-10-29 19:20:18 +0100 | [diff] [blame] | 10700 | struct btrfs_block_group *bg; |
Omar Sandoval | ed46ff3 | 2016-11-03 10:28:14 -0700 | [diff] [blame] | 10701 | u64 len = isize - start; |
| 10702 | |
Omar Sandoval | 39b07b5 | 2019-12-02 17:34:23 -0800 | [diff] [blame] | 10703 | em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len); |
Omar Sandoval | ed46ff3 | 2016-11-03 10:28:14 -0700 | [diff] [blame] | 10704 | if (IS_ERR(em)) { |
| 10705 | ret = PTR_ERR(em); |
| 10706 | goto out; |
| 10707 | } |
| 10708 | |
| 10709 | if (em->block_start == EXTENT_MAP_HOLE) { |
| 10710 | btrfs_warn(fs_info, "swapfile must not have holes"); |
| 10711 | ret = -EINVAL; |
| 10712 | goto out; |
| 10713 | } |
| 10714 | if (em->block_start == EXTENT_MAP_INLINE) { |
| 10715 | /* |
| 10716 | * It's unlikely we'll ever actually find ourselves |
| 10717 | * here, as a file small enough to fit inline won't be |
| 10718 | * big enough to store more than the swap header, but in |
| 10719 | * case something changes in the future, let's catch it |
| 10720 | * here rather than later. |
| 10721 | */ |
| 10722 | btrfs_warn(fs_info, "swapfile must not be inline"); |
| 10723 | ret = -EINVAL; |
| 10724 | goto out; |
| 10725 | } |
| 10726 | if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) { |
| 10727 | btrfs_warn(fs_info, "swapfile must not be compressed"); |
| 10728 | ret = -EINVAL; |
| 10729 | goto out; |
| 10730 | } |
| 10731 | |
| 10732 | logical_block_start = em->block_start + (start - em->start); |
| 10733 | len = min(len, em->len - (start - em->start)); |
| 10734 | free_extent_map(em); |
| 10735 | em = NULL; |
| 10736 | |
Boris Burkov | a84d5d4 | 2020-08-18 11:00:05 -0700 | [diff] [blame] | 10737 | ret = can_nocow_extent(inode, start, &len, NULL, NULL, NULL, true); |
Omar Sandoval | ed46ff3 | 2016-11-03 10:28:14 -0700 | [diff] [blame] | 10738 | if (ret < 0) { |
| 10739 | goto out; |
| 10740 | } else if (ret) { |
| 10741 | ret = 0; |
| 10742 | } else { |
| 10743 | btrfs_warn(fs_info, |
| 10744 | "swapfile must not be copy-on-write"); |
| 10745 | ret = -EINVAL; |
| 10746 | goto out; |
| 10747 | } |
| 10748 | |
| 10749 | em = btrfs_get_chunk_map(fs_info, logical_block_start, len); |
| 10750 | if (IS_ERR(em)) { |
| 10751 | ret = PTR_ERR(em); |
| 10752 | goto out; |
| 10753 | } |
| 10754 | |
| 10755 | if (em->map_lookup->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) { |
| 10756 | btrfs_warn(fs_info, |
| 10757 | "swapfile must have single data profile"); |
| 10758 | ret = -EINVAL; |
| 10759 | goto out; |
| 10760 | } |
| 10761 | |
| 10762 | if (device == NULL) { |
| 10763 | device = em->map_lookup->stripes[0].dev; |
| 10764 | ret = btrfs_add_swapfile_pin(inode, device, false); |
| 10765 | if (ret == 1) |
| 10766 | ret = 0; |
| 10767 | else if (ret) |
| 10768 | goto out; |
| 10769 | } else if (device != em->map_lookup->stripes[0].dev) { |
| 10770 | btrfs_warn(fs_info, "swapfile must be on one device"); |
| 10771 | ret = -EINVAL; |
| 10772 | goto out; |
| 10773 | } |
| 10774 | |
| 10775 | physical_block_start = (em->map_lookup->stripes[0].physical + |
| 10776 | (logical_block_start - em->start)); |
| 10777 | len = min(len, em->len - (logical_block_start - em->start)); |
| 10778 | free_extent_map(em); |
| 10779 | em = NULL; |
| 10780 | |
| 10781 | bg = btrfs_lookup_block_group(fs_info, logical_block_start); |
| 10782 | if (!bg) { |
| 10783 | btrfs_warn(fs_info, |
| 10784 | "could not find block group containing swapfile"); |
| 10785 | ret = -EINVAL; |
| 10786 | goto out; |
| 10787 | } |
| 10788 | |
Filipe Manana | 195a49e | 2021-02-05 12:55:37 +0000 | [diff] [blame] | 10789 | if (!btrfs_inc_block_group_swap_extents(bg)) { |
| 10790 | btrfs_warn(fs_info, |
| 10791 | "block group for swapfile at %llu is read-only%s", |
| 10792 | bg->start, |
| 10793 | atomic_read(&fs_info->scrubs_running) ? |
| 10794 | " (scrub running)" : ""); |
| 10795 | btrfs_put_block_group(bg); |
| 10796 | ret = -EINVAL; |
| 10797 | goto out; |
| 10798 | } |
| 10799 | |
Omar Sandoval | ed46ff3 | 2016-11-03 10:28:14 -0700 | [diff] [blame] | 10800 | ret = btrfs_add_swapfile_pin(inode, bg, true); |
| 10801 | if (ret) { |
| 10802 | btrfs_put_block_group(bg); |
| 10803 | if (ret == 1) |
| 10804 | ret = 0; |
| 10805 | else |
| 10806 | goto out; |
| 10807 | } |
| 10808 | |
| 10809 | if (bsi.block_len && |
| 10810 | bsi.block_start + bsi.block_len == physical_block_start) { |
| 10811 | bsi.block_len += len; |
| 10812 | } else { |
| 10813 | if (bsi.block_len) { |
| 10814 | ret = btrfs_add_swap_extent(sis, &bsi); |
| 10815 | if (ret) |
| 10816 | goto out; |
| 10817 | } |
| 10818 | bsi.start = start; |
| 10819 | bsi.block_start = physical_block_start; |
| 10820 | bsi.block_len = len; |
| 10821 | } |
| 10822 | |
| 10823 | start += len; |
| 10824 | } |
| 10825 | |
| 10826 | if (bsi.block_len) |
| 10827 | ret = btrfs_add_swap_extent(sis, &bsi); |
| 10828 | |
| 10829 | out: |
| 10830 | if (!IS_ERR_OR_NULL(em)) |
| 10831 | free_extent_map(em); |
| 10832 | |
| 10833 | unlock_extent_cached(io_tree, 0, isize - 1, &cached_state); |
| 10834 | |
| 10835 | if (ret) |
| 10836 | btrfs_swap_deactivate(file); |
| 10837 | |
Filipe Manana | dd0734f | 2021-02-05 12:55:38 +0000 | [diff] [blame] | 10838 | btrfs_drew_write_unlock(&root->snapshot_lock); |
| 10839 | |
Goldwyn Rodrigues | c3e1f96 | 2020-08-25 10:02:32 -0500 | [diff] [blame] | 10840 | btrfs_exclop_finish(fs_info); |
Omar Sandoval | ed46ff3 | 2016-11-03 10:28:14 -0700 | [diff] [blame] | 10841 | |
| 10842 | if (ret) |
| 10843 | return ret; |
| 10844 | |
| 10845 | if (device) |
| 10846 | sis->bdev = device->bdev; |
| 10847 | *span = bsi.highest_ppage - bsi.lowest_ppage + 1; |
| 10848 | sis->max = bsi.nr_pages; |
| 10849 | sis->pages = bsi.nr_pages - 1; |
| 10850 | sis->highest_bit = bsi.nr_pages - 1; |
| 10851 | return bsi.nr_extents; |
| 10852 | } |
| 10853 | #else |
| 10854 | static void btrfs_swap_deactivate(struct file *file) |
| 10855 | { |
| 10856 | } |
| 10857 | |
| 10858 | static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file, |
| 10859 | sector_t *span) |
| 10860 | { |
| 10861 | return -EOPNOTSUPP; |
| 10862 | } |
| 10863 | #endif |
| 10864 | |
Filipe Manana | 2766ff6 | 2020-11-04 11:07:34 +0000 | [diff] [blame] | 10865 | /* |
| 10866 | * Update the number of bytes used in the VFS' inode. When we replace extents in |
| 10867 | * a range (clone, dedupe, fallocate's zero range), we must update the number of |
| 10868 | * bytes used by the inode in an atomic manner, so that concurrent stat(2) calls |
| 10869 | * always get a correct value. |
| 10870 | */ |
| 10871 | void btrfs_update_inode_bytes(struct btrfs_inode *inode, |
| 10872 | const u64 add_bytes, |
| 10873 | const u64 del_bytes) |
| 10874 | { |
| 10875 | if (add_bytes == del_bytes) |
| 10876 | return; |
| 10877 | |
| 10878 | spin_lock(&inode->lock); |
| 10879 | if (del_bytes > 0) |
| 10880 | inode_sub_bytes(&inode->vfs_inode, del_bytes); |
| 10881 | if (add_bytes > 0) |
| 10882 | inode_add_bytes(&inode->vfs_inode, add_bytes); |
| 10883 | spin_unlock(&inode->lock); |
| 10884 | } |
| 10885 | |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 10886 | static const struct inode_operations btrfs_dir_inode_operations = { |
Chris Mason | 3394e16 | 2008-11-17 20:42:26 -0500 | [diff] [blame] | 10887 | .getattr = btrfs_getattr, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10888 | .lookup = btrfs_lookup, |
| 10889 | .create = btrfs_create, |
| 10890 | .unlink = btrfs_unlink, |
| 10891 | .link = btrfs_link, |
| 10892 | .mkdir = btrfs_mkdir, |
| 10893 | .rmdir = btrfs_rmdir, |
Miklos Szeredi | 2773bf0 | 2016-09-27 11:03:58 +0200 | [diff] [blame] | 10894 | .rename = btrfs_rename2, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10895 | .symlink = btrfs_symlink, |
| 10896 | .setattr = btrfs_setattr, |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 10897 | .mknod = btrfs_mknod, |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 10898 | .listxattr = btrfs_listxattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 10899 | .permission = btrfs_permission, |
Christoph Hellwig | 4e34e71 | 2011-07-23 17:37:31 +0200 | [diff] [blame] | 10900 | .get_acl = btrfs_get_acl, |
Christoph Hellwig | 996a710 | 2013-12-20 05:16:43 -0800 | [diff] [blame] | 10901 | .set_acl = btrfs_set_acl, |
Guangyu Sun | 93fd63c | 2013-09-16 10:42:03 -0700 | [diff] [blame] | 10902 | .update_time = btrfs_update_time, |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 10903 | .tmpfile = btrfs_tmpfile, |
Miklos Szeredi | 97fc297 | 2021-04-07 14:36:43 +0200 | [diff] [blame] | 10904 | .fileattr_get = btrfs_fileattr_get, |
| 10905 | .fileattr_set = btrfs_fileattr_set, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10906 | }; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 10907 | |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 10908 | static const struct file_operations btrfs_dir_file_operations = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10909 | .llseek = generic_file_llseek, |
| 10910 | .read = generic_read_dir, |
Omar Sandoval | 02dbfc9 | 2016-05-20 13:50:33 -0700 | [diff] [blame] | 10911 | .iterate_shared = btrfs_real_readdir, |
Josef Bacik | 23b5ec7 | 2017-07-24 15:14:25 -0400 | [diff] [blame] | 10912 | .open = btrfs_opendir, |
Christoph Hellwig | 34287aa | 2007-09-14 10:22:47 -0400 | [diff] [blame] | 10913 | .unlocked_ioctl = btrfs_ioctl, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10914 | #ifdef CONFIG_COMPAT |
Luke Dashjr | 4c63c24 | 2015-10-29 08:22:21 +0000 | [diff] [blame] | 10915 | .compat_ioctl = btrfs_compat_ioctl, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10916 | #endif |
Sage Weil | 6bf13c0 | 2008-06-10 10:07:39 -0400 | [diff] [blame] | 10917 | .release = btrfs_release_file, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 10918 | .fsync = btrfs_sync_file, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10919 | }; |
| 10920 | |
Chris Mason | 3505439 | 2009-01-21 13:11:13 -0500 | [diff] [blame] | 10921 | /* |
| 10922 | * btrfs doesn't support the bmap operation because swapfiles |
| 10923 | * use bmap to make a mapping of extents in the file. They assume |
| 10924 | * these extents won't change over the life of the file and they |
| 10925 | * use the bmap result to do IO directly to the drive. |
| 10926 | * |
| 10927 | * the btrfs bmap call would return logical addresses that aren't |
| 10928 | * suitable for IO and they also will change frequently as COW |
| 10929 | * operations happen. So, swapfile + btrfs == corruption. |
| 10930 | * |
| 10931 | * For now we're avoiding this by dropping bmap. |
| 10932 | */ |
Alexey Dobriyan | 7f09410 | 2009-09-21 17:01:10 -0700 | [diff] [blame] | 10933 | static const struct address_space_operations btrfs_aops = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10934 | .readpage = btrfs_readpage, |
| 10935 | .writepage = btrfs_writepage, |
Chris Mason | b293f02e | 2007-11-01 19:45:34 -0400 | [diff] [blame] | 10936 | .writepages = btrfs_writepages, |
Matthew Wilcox (Oracle) | ba206a0 | 2020-06-01 21:47:05 -0700 | [diff] [blame] | 10937 | .readahead = btrfs_readahead, |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 10938 | .direct_IO = noop_direct_IO, |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 10939 | .invalidatepage = btrfs_invalidatepage, |
| 10940 | .releasepage = btrfs_releasepage, |
Roman Gushchin | f8e6608 | 2020-03-04 16:57:35 -0800 | [diff] [blame] | 10941 | #ifdef CONFIG_MIGRATION |
| 10942 | .migratepage = btrfs_migratepage, |
| 10943 | #endif |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 10944 | .set_page_dirty = btrfs_set_page_dirty, |
Andi Kleen | 465fdd9 | 2009-09-16 11:50:18 +0200 | [diff] [blame] | 10945 | .error_remove_page = generic_error_remove_page, |
Omar Sandoval | ed46ff3 | 2016-11-03 10:28:14 -0700 | [diff] [blame] | 10946 | .swap_activate = btrfs_swap_activate, |
| 10947 | .swap_deactivate = btrfs_swap_deactivate, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10948 | }; |
| 10949 | |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 10950 | static const struct inode_operations btrfs_file_inode_operations = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10951 | .getattr = btrfs_getattr, |
| 10952 | .setattr = btrfs_setattr, |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 10953 | .listxattr = btrfs_listxattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 10954 | .permission = btrfs_permission, |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 10955 | .fiemap = btrfs_fiemap, |
Christoph Hellwig | 4e34e71 | 2011-07-23 17:37:31 +0200 | [diff] [blame] | 10956 | .get_acl = btrfs_get_acl, |
Christoph Hellwig | 996a710 | 2013-12-20 05:16:43 -0800 | [diff] [blame] | 10957 | .set_acl = btrfs_set_acl, |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 10958 | .update_time = btrfs_update_time, |
Miklos Szeredi | 97fc297 | 2021-04-07 14:36:43 +0200 | [diff] [blame] | 10959 | .fileattr_get = btrfs_fileattr_get, |
| 10960 | .fileattr_set = btrfs_fileattr_set, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10961 | }; |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 10962 | static const struct inode_operations btrfs_special_inode_operations = { |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 10963 | .getattr = btrfs_getattr, |
| 10964 | .setattr = btrfs_setattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 10965 | .permission = btrfs_permission, |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 10966 | .listxattr = btrfs_listxattr, |
Christoph Hellwig | 4e34e71 | 2011-07-23 17:37:31 +0200 | [diff] [blame] | 10967 | .get_acl = btrfs_get_acl, |
Christoph Hellwig | 996a710 | 2013-12-20 05:16:43 -0800 | [diff] [blame] | 10968 | .set_acl = btrfs_set_acl, |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 10969 | .update_time = btrfs_update_time, |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 10970 | }; |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 10971 | static const struct inode_operations btrfs_symlink_inode_operations = { |
Al Viro | 6b25539 | 2015-11-17 10:20:54 -0500 | [diff] [blame] | 10972 | .get_link = page_get_link, |
Li Zefan | f209561 | 2010-11-19 02:05:24 +0000 | [diff] [blame] | 10973 | .getattr = btrfs_getattr, |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 10974 | .setattr = btrfs_setattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 10975 | .permission = btrfs_permission, |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 10976 | .listxattr = btrfs_listxattr, |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 10977 | .update_time = btrfs_update_time, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10978 | }; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 10979 | |
Alexey Dobriyan | 82d339d | 2009-10-09 09:54:36 -0400 | [diff] [blame] | 10980 | const struct dentry_operations btrfs_dentry_operations = { |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 10981 | .d_delete = btrfs_dentry_delete, |
| 10982 | }; |