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> |
Christoph Hellwig | 348332e | 2021-09-20 14:33:12 +0200 | [diff] [blame] | 9 | #include <linux/blk-cgroup.h> |
Sage Weil | f2eb0a2 | 2008-05-02 14:43:14 -0400 | [diff] [blame] | 10 | #include <linux/file.h> |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 11 | #include <linux/fs.h> |
| 12 | #include <linux/pagemap.h> |
| 13 | #include <linux/highmem.h> |
| 14 | #include <linux/time.h> |
| 15 | #include <linux/init.h> |
| 16 | #include <linux/string.h> |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 17 | #include <linux/backing-dev.h> |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 18 | #include <linux/writeback.h> |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 19 | #include <linux/compat.h> |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 20 | #include <linux/xattr.h> |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 21 | #include <linux/posix_acl.h> |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 22 | #include <linux/falloc.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 23 | #include <linux/slab.h> |
David Sterba | 7a36dde | 2011-05-06 15:33:15 +0200 | [diff] [blame] | 24 | #include <linux/ratelimit.h> |
Filipe Brandenburger | 55e301f | 2013-01-29 06:04:50 +0000 | [diff] [blame] | 25 | #include <linux/btrfs.h> |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 26 | #include <linux/blkdev.h> |
Josef Bacik | f23b5a5 | 2013-06-19 10:16:26 -0400 | [diff] [blame] | 27 | #include <linux/posix_acl_xattr.h> |
Christoph Hellwig | e2e40f2 | 2015-02-22 08:58:50 -0800 | [diff] [blame] | 28 | #include <linux/uio.h> |
Josef Bacik | 69fe2d7 | 2017-10-19 14:15:57 -0400 | [diff] [blame] | 29 | #include <linux/magic.h> |
Jeff Layton | ae5e165 | 2018-01-29 06:41:30 -0500 | [diff] [blame] | 30 | #include <linux/iversion.h> |
Omar Sandoval | ed46ff3 | 2016-11-03 10:28:14 -0700 | [diff] [blame] | 31 | #include <linux/swap.h> |
Roman Gushchin | f8e6608 | 2020-03-04 16:57:35 -0800 | [diff] [blame] | 32 | #include <linux/migrate.h> |
Nikolay Borisov | b1c16ac | 2019-04-01 11:29:57 +0300 | [diff] [blame] | 33 | #include <linux/sched/mm.h> |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 34 | #include <linux/iomap.h> |
David Sterba | 92d3217 | 2018-04-16 21:10:14 +0200 | [diff] [blame] | 35 | #include <asm/unaligned.h> |
Boris Burkov | 1460540 | 2021-06-30 13:01:49 -0700 | [diff] [blame] | 36 | #include <linux/fsverity.h> |
David Sterba | 602cbe9 | 2019-08-21 18:48:25 +0200 | [diff] [blame] | 37 | #include "misc.h" |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 38 | #include "ctree.h" |
| 39 | #include "disk-io.h" |
| 40 | #include "transaction.h" |
| 41 | #include "btrfs_inode.h" |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 42 | #include "print-tree.h" |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 43 | #include "ordered-data.h" |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 44 | #include "xattr.h" |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 45 | #include "tree-log.h" |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 46 | #include "volumes.h" |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 47 | #include "compression.h" |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 48 | #include "locking.h" |
Josef Bacik | dc89e98 | 2011-01-28 17:05:48 -0500 | [diff] [blame] | 49 | #include "free-space-cache.h" |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 50 | #include "props.h" |
Dongsheng Yang | 3119321 | 2014-12-12 16:44:35 +0800 | [diff] [blame] | 51 | #include "qgroup.h" |
Josef Bacik | 8673634 | 2019-06-19 15:12:00 -0400 | [diff] [blame] | 52 | #include "delalloc-space.h" |
Josef Bacik | aac0023 | 2019-06-20 15:37:44 -0400 | [diff] [blame] | 53 | #include "block-group.h" |
Filipe Manana | 467dc47 | 2020-05-27 11:16:07 +0100 | [diff] [blame] | 54 | #include "space-info.h" |
Naohiro Aota | d8e3fb1 | 2021-02-04 19:22:05 +0900 | [diff] [blame] | 55 | #include "zoned.h" |
Qu Wenruo | b945a46 | 2021-05-31 16:50:46 +0800 | [diff] [blame] | 56 | #include "subpage.h" |
Josef Bacik | 26c2c45 | 2021-12-03 17:18:03 -0500 | [diff] [blame] | 57 | #include "inode-item.h" |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 58 | |
| 59 | struct btrfs_iget_args { |
David Sterba | 0202e83 | 2020-05-15 19:35:59 +0200 | [diff] [blame] | 60 | u64 ino; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 61 | struct btrfs_root *root; |
| 62 | }; |
| 63 | |
Filipe Manana | f28a492 | 2015-12-08 19:23:20 +0000 | [diff] [blame] | 64 | struct btrfs_dio_data { |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 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 | 3a60f65 | 2021-10-27 10:39:03 +0200 | [diff] [blame] | 290 | kaddr = kmap_atomic(cpage); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 291 | write_extent_buffer(leaf, kaddr, ptr, cur_size); |
David Sterba | 3a60f65 | 2021-10-27 10:39:03 +0200 | [diff] [blame] | 292 | kunmap_atomic(kaddr); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 293 | |
| 294 | i++; |
| 295 | ptr += cur_size; |
| 296 | compressed_size -= cur_size; |
| 297 | } |
| 298 | btrfs_set_file_extent_compression(leaf, ei, |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 299 | compress_type); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 300 | } else { |
| 301 | page = find_get_page(inode->i_mapping, |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 302 | start >> PAGE_SHIFT); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 303 | btrfs_set_file_extent_compression(leaf, ei, 0); |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 304 | kaddr = kmap_atomic(page); |
Johannes Thumshirn | 7073017 | 2018-12-05 15:23:03 +0100 | [diff] [blame] | 305 | offset = offset_in_page(start); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 306 | write_extent_buffer(leaf, kaddr + offset, ptr, size); |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 307 | kunmap_atomic(kaddr); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 308 | put_page(page); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 309 | } |
| 310 | btrfs_mark_buffer_dirty(leaf); |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 311 | btrfs_release_path(path); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 312 | |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 313 | /* |
Josef Bacik | 9ddc959 | 2020-01-17 09:02:22 -0500 | [diff] [blame] | 314 | * We align size to sectorsize for inline extents just for simplicity |
| 315 | * sake. |
| 316 | */ |
| 317 | size = ALIGN(size, root->fs_info->sectorsize); |
| 318 | ret = btrfs_inode_set_file_extent_range(BTRFS_I(inode), start, size); |
| 319 | if (ret) |
| 320 | goto fail; |
| 321 | |
| 322 | /* |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 323 | * we're an inline extent, so nobody can |
| 324 | * extend the file past i_size without locking |
| 325 | * a page we already have locked. |
| 326 | * |
| 327 | * We must do any isize and inode updates |
| 328 | * before we unlock the pages. Otherwise we |
| 329 | * could end up racing with unlink. |
| 330 | */ |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 331 | BTRFS_I(inode)->disk_i_size = inode->i_size; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 332 | fail: |
David Sterba | 79b4f4c | 2017-06-15 19:09:51 +0200 | [diff] [blame] | 333 | return ret; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | |
| 337 | /* |
| 338 | * conditionally insert an inline extent into the file. This |
| 339 | * does the checks required to make sure the data is small enough |
| 340 | * to fit as an inline extent. |
| 341 | */ |
Nikolay Borisov | a034940 | 2020-06-03 08:55:12 +0300 | [diff] [blame] | 342 | 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] | 343 | u64 end, size_t compressed_size, |
| 344 | int compress_type, |
| 345 | struct page **compressed_pages) |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 346 | { |
Filipe Manana | 5893dfb | 2020-11-04 11:07:32 +0000 | [diff] [blame] | 347 | struct btrfs_drop_extents_args drop_args = { 0 }; |
Nikolay Borisov | a034940 | 2020-06-03 08:55:12 +0300 | [diff] [blame] | 348 | struct btrfs_root *root = inode->root; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 349 | struct btrfs_fs_info *fs_info = root->fs_info; |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 350 | struct btrfs_trans_handle *trans; |
Nikolay Borisov | a034940 | 2020-06-03 08:55:12 +0300 | [diff] [blame] | 351 | u64 isize = i_size_read(&inode->vfs_inode); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 352 | u64 actual_end = min(end + 1, isize); |
| 353 | u64 inline_len = actual_end - start; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 354 | u64 aligned_end = ALIGN(end, fs_info->sectorsize); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 355 | u64 data_len = inline_len; |
| 356 | int ret; |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 357 | struct btrfs_path *path; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 358 | |
| 359 | if (compressed_size) |
| 360 | data_len = compressed_size; |
| 361 | |
| 362 | if (start > 0 || |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 363 | actual_end > fs_info->sectorsize || |
| 364 | data_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info) || |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 365 | (!compressed_size && |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 366 | (actual_end & (fs_info->sectorsize - 1)) == 0) || |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 367 | end + 1 < isize || |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 368 | data_len > fs_info->max_inline) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 369 | return 1; |
| 370 | } |
| 371 | |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 372 | path = btrfs_alloc_path(); |
| 373 | if (!path) |
| 374 | return -ENOMEM; |
| 375 | |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 376 | trans = btrfs_join_transaction(root); |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 377 | if (IS_ERR(trans)) { |
| 378 | btrfs_free_path(path); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 379 | return PTR_ERR(trans); |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 380 | } |
Nikolay Borisov | a034940 | 2020-06-03 08:55:12 +0300 | [diff] [blame] | 381 | trans->block_rsv = &inode->block_rsv; |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 382 | |
Filipe Manana | 5893dfb | 2020-11-04 11:07:32 +0000 | [diff] [blame] | 383 | drop_args.path = path; |
| 384 | drop_args.start = start; |
| 385 | drop_args.end = aligned_end; |
| 386 | drop_args.drop_cache = true; |
| 387 | drop_args.replace_extent = true; |
| 388 | |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 389 | if (compressed_size && compressed_pages) |
Filipe Manana | 5893dfb | 2020-11-04 11:07:32 +0000 | [diff] [blame] | 390 | 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] | 391 | compressed_size); |
| 392 | else |
Filipe Manana | 5893dfb | 2020-11-04 11:07:32 +0000 | [diff] [blame] | 393 | 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] | 394 | inline_len); |
| 395 | |
Filipe Manana | 5893dfb | 2020-11-04 11:07:32 +0000 | [diff] [blame] | 396 | ret = btrfs_drop_extents(trans, root, inode, &drop_args); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 397 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 398 | btrfs_abort_transaction(trans, ret); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 399 | goto out; |
| 400 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 401 | |
| 402 | if (isize > actual_end) |
| 403 | inline_len = min_t(u64, isize, actual_end); |
Filipe Manana | 5893dfb | 2020-11-04 11:07:32 +0000 | [diff] [blame] | 404 | ret = insert_inline_extent(trans, path, drop_args.extent_inserted, |
Nikolay Borisov | a034940 | 2020-06-03 08:55:12 +0300 | [diff] [blame] | 405 | root, &inode->vfs_inode, start, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 406 | inline_len, compressed_size, |
Li Zefan | fe3f566 | 2011-03-28 08:30:38 +0000 | [diff] [blame] | 407 | compress_type, compressed_pages); |
Josef Bacik | 2adcac1 | 2012-05-23 16:10:14 -0400 | [diff] [blame] | 408 | if (ret && ret != -ENOSPC) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 409 | btrfs_abort_transaction(trans, ret); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 410 | goto out; |
Josef Bacik | 2adcac1 | 2012-05-23 16:10:14 -0400 | [diff] [blame] | 411 | } else if (ret == -ENOSPC) { |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 412 | ret = 1; |
| 413 | goto out; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 414 | } |
Josef Bacik | 2adcac1 | 2012-05-23 16:10:14 -0400 | [diff] [blame] | 415 | |
Filipe Manana | 2766ff6 | 2020-11-04 11:07:34 +0000 | [diff] [blame] | 416 | btrfs_update_inode_bytes(inode, inline_len, drop_args.bytes_found); |
Nikolay Borisov | 9a56fcd | 2020-11-02 16:48:59 +0200 | [diff] [blame] | 417 | ret = btrfs_update_inode(trans, root, inode); |
Filipe Manana | 2766ff6 | 2020-11-04 11:07:34 +0000 | [diff] [blame] | 418 | if (ret && ret != -ENOSPC) { |
| 419 | btrfs_abort_transaction(trans, ret); |
| 420 | goto out; |
| 421 | } else if (ret == -ENOSPC) { |
| 422 | ret = 1; |
| 423 | goto out; |
| 424 | } |
| 425 | |
Nikolay Borisov | a034940 | 2020-06-03 08:55:12 +0300 | [diff] [blame] | 426 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &inode->runtime_flags); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 427 | out: |
Qu Wenruo | 94ed938 | 2015-09-08 17:25:56 +0800 | [diff] [blame] | 428 | /* |
| 429 | * Don't forget to free the reserved space, as for inlined extent |
| 430 | * it won't count as data extent, free them directly here. |
| 431 | * And at reserve time, it's always aligned to page size, so |
| 432 | * just free one page here. |
| 433 | */ |
Nikolay Borisov | a034940 | 2020-06-03 08:55:12 +0300 | [diff] [blame] | 434 | btrfs_qgroup_free_data(inode, NULL, 0, PAGE_SIZE); |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 435 | btrfs_free_path(path); |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 436 | btrfs_end_transaction(trans); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 437 | return ret; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 438 | } |
| 439 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 440 | struct async_extent { |
| 441 | u64 start; |
| 442 | u64 ram_size; |
| 443 | u64 compressed_size; |
| 444 | struct page **pages; |
| 445 | unsigned long nr_pages; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 446 | int compress_type; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 447 | struct list_head list; |
| 448 | }; |
| 449 | |
Nikolay Borisov | 97db120 | 2019-03-12 17:20:24 +0200 | [diff] [blame] | 450 | struct async_chunk { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 451 | struct inode *inode; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 452 | struct page *locked_page; |
| 453 | u64 start; |
| 454 | u64 end; |
Liu Bo | f82b735 | 2017-10-23 23:18:16 -0600 | [diff] [blame] | 455 | unsigned int write_flags; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 456 | struct list_head extents; |
Chris Mason | ec39f76 | 2019-07-10 12:28:17 -0700 | [diff] [blame] | 457 | struct cgroup_subsys_state *blkcg_css; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 458 | struct btrfs_work work; |
Qu Wenruo | 9e895a8 | 2021-09-27 15:21:45 +0800 | [diff] [blame] | 459 | struct async_cow *async_cow; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 460 | }; |
| 461 | |
Nikolay Borisov | 97db120 | 2019-03-12 17:20:24 +0200 | [diff] [blame] | 462 | struct async_cow { |
Nikolay Borisov | 97db120 | 2019-03-12 17:20:24 +0200 | [diff] [blame] | 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 | { |
Nikolay Borisov | 99c88dc | 2020-06-03 08:55:26 +0300 | [diff] [blame] | 493 | if (inode->flags & BTRFS_INODE_NODATACOW || |
| 494 | inode->flags & BTRFS_INODE_NODATASUM) |
Qu Wenruo | 42c16da | 2019-07-01 05:12:46 +0000 | [diff] [blame] | 495 | return false; |
| 496 | return true; |
| 497 | } |
| 498 | |
| 499 | /* |
| 500 | * Check if the inode needs to be submitted to compression, based on mount |
| 501 | * options, defragmentation, properties or heuristics. |
| 502 | */ |
Nikolay Borisov | 808a129 | 2020-06-03 08:55:27 +0300 | [diff] [blame] | 503 | static inline int inode_need_compress(struct btrfs_inode *inode, u64 start, |
| 504 | u64 end) |
Wang Shilong | f79707b | 2014-07-17 11:44:09 +0800 | [diff] [blame] | 505 | { |
Nikolay Borisov | 808a129 | 2020-06-03 08:55:27 +0300 | [diff] [blame] | 506 | struct btrfs_fs_info *fs_info = inode->root->fs_info; |
Wang Shilong | f79707b | 2014-07-17 11:44:09 +0800 | [diff] [blame] | 507 | |
Nikolay Borisov | 808a129 | 2020-06-03 08:55:27 +0300 | [diff] [blame] | 508 | if (!inode_can_compress(inode)) { |
Qu Wenruo | 42c16da | 2019-07-01 05:12:46 +0000 | [diff] [blame] | 509 | WARN(IS_ENABLED(CONFIG_BTRFS_DEBUG), |
| 510 | KERN_ERR "BTRFS: unexpected compression for ino %llu\n", |
Nikolay Borisov | 808a129 | 2020-06-03 08:55:27 +0300 | [diff] [blame] | 511 | btrfs_ino(inode)); |
Qu Wenruo | 42c16da | 2019-07-01 05:12:46 +0000 | [diff] [blame] | 512 | return 0; |
| 513 | } |
Qu Wenruo | 0cf9b24 | 2021-09-27 15:22:08 +0800 | [diff] [blame] | 514 | /* |
| 515 | * Special check for subpage. |
| 516 | * |
| 517 | * We lock the full page then run each delalloc range in the page, thus |
| 518 | * for the following case, we will hit some subpage specific corner case: |
| 519 | * |
| 520 | * 0 32K 64K |
| 521 | * | |///////| |///////| |
| 522 | * \- A \- B |
| 523 | * |
| 524 | * In above case, both range A and range B will try to unlock the full |
| 525 | * page [0, 64K), causing the one finished later will have page |
| 526 | * unlocked already, triggering various page lock requirement BUG_ON()s. |
| 527 | * |
| 528 | * So here we add an artificial limit that subpage compression can only |
| 529 | * if the range is fully page aligned. |
| 530 | * |
| 531 | * In theory we only need to ensure the first page is fully covered, but |
| 532 | * the tailing partial page will be locked until the full compression |
| 533 | * finishes, delaying the write of other range. |
| 534 | * |
| 535 | * TODO: Make btrfs_run_delalloc_range() to lock all delalloc range |
| 536 | * first to prevent any submitted async extent to unlock the full page. |
| 537 | * By this, we can ensure for subpage case that only the last async_cow |
| 538 | * will unlock the full page. |
| 539 | */ |
| 540 | if (fs_info->sectorsize < PAGE_SIZE) { |
| 541 | if (!IS_ALIGNED(start, PAGE_SIZE) || |
| 542 | !IS_ALIGNED(end + 1, PAGE_SIZE)) |
| 543 | return 0; |
| 544 | } |
| 545 | |
Wang Shilong | f79707b | 2014-07-17 11:44:09 +0800 | [diff] [blame] | 546 | /* force compress */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 547 | if (btrfs_test_opt(fs_info, FORCE_COMPRESS)) |
Wang Shilong | f79707b | 2014-07-17 11:44:09 +0800 | [diff] [blame] | 548 | return 1; |
David Sterba | eec63c6 | 2017-07-17 19:41:31 +0200 | [diff] [blame] | 549 | /* defrag ioctl */ |
Nikolay Borisov | 808a129 | 2020-06-03 08:55:27 +0300 | [diff] [blame] | 550 | if (inode->defrag_compress) |
David Sterba | eec63c6 | 2017-07-17 19:41:31 +0200 | [diff] [blame] | 551 | return 1; |
Wang Shilong | f79707b | 2014-07-17 11:44:09 +0800 | [diff] [blame] | 552 | /* bad compression ratios */ |
Nikolay Borisov | 808a129 | 2020-06-03 08:55:27 +0300 | [diff] [blame] | 553 | if (inode->flags & BTRFS_INODE_NOCOMPRESS) |
Wang Shilong | f79707b | 2014-07-17 11:44:09 +0800 | [diff] [blame] | 554 | return 0; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 555 | if (btrfs_test_opt(fs_info, COMPRESS) || |
Nikolay Borisov | 808a129 | 2020-06-03 08:55:27 +0300 | [diff] [blame] | 556 | inode->flags & BTRFS_INODE_COMPRESS || |
| 557 | inode->prop_compress) |
| 558 | return btrfs_compress_heuristic(&inode->vfs_inode, start, end); |
Wang Shilong | f79707b | 2014-07-17 11:44:09 +0800 | [diff] [blame] | 559 | return 0; |
| 560 | } |
| 561 | |
Nikolay Borisov | 6158e1c | 2017-02-20 13:50:43 +0200 | [diff] [blame] | 562 | static inline void inode_should_defrag(struct btrfs_inode *inode, |
Anand Jain | 26d30f8 | 2016-12-19 19:09:06 +0800 | [diff] [blame] | 563 | u64 start, u64 end, u64 num_bytes, u64 small_write) |
| 564 | { |
| 565 | /* If this is a small write inside eof, kick off a defrag */ |
| 566 | if (num_bytes < small_write && |
Nikolay Borisov | 6158e1c | 2017-02-20 13:50:43 +0200 | [diff] [blame] | 567 | (start > 0 || end + 1 < inode->disk_i_size)) |
Anand Jain | 26d30f8 | 2016-12-19 19:09:06 +0800 | [diff] [blame] | 568 | btrfs_add_inode_defrag(NULL, inode); |
| 569 | } |
| 570 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 571 | /* |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 572 | * we create compressed extents in two phases. The first |
| 573 | * phase compresses a range of pages that have already been |
| 574 | * locked (both pages and state bits are locked). |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 575 | * |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 576 | * This is done inside an ordered work queue, and the compression |
| 577 | * is spread across many cpus. The actual IO submission is step |
| 578 | * two, and the ordered work queue takes care of making sure that |
| 579 | * happens in the same order things were put onto the queue by |
| 580 | * writepages and friends. |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 581 | * |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 582 | * If this code finds it can't get good compression, it puts an |
| 583 | * entry onto the work queue to write the uncompressed bytes. This |
| 584 | * makes sure that both compressed inodes and uncompressed inodes |
Artem Bityutskiy | b257031 | 2012-07-25 18:12:06 +0300 | [diff] [blame] | 585 | * are written in the same order that the flusher thread sent them |
| 586 | * down. |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 587 | */ |
Nikolay Borisov | ac3e993 | 2019-07-17 14:41:44 +0300 | [diff] [blame] | 588 | static noinline int compress_file_range(struct async_chunk *async_chunk) |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 589 | { |
Nikolay Borisov | 1368c6d | 2019-03-12 17:20:27 +0200 | [diff] [blame] | 590 | struct inode *inode = async_chunk->inode; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 591 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 592 | u64 blocksize = fs_info->sectorsize; |
Nikolay Borisov | 1368c6d | 2019-03-12 17:20:27 +0200 | [diff] [blame] | 593 | u64 start = async_chunk->start; |
| 594 | u64 end = async_chunk->end; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 595 | u64 actual_end; |
Josef Bacik | d98da49 | 2019-10-11 09:03:54 -0400 | [diff] [blame] | 596 | u64 i_size; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 597 | int ret = 0; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 598 | struct page **pages = NULL; |
| 599 | unsigned long nr_pages; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 600 | unsigned long total_compressed = 0; |
| 601 | unsigned long total_in = 0; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 602 | int i; |
| 603 | int will_compress; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 604 | int compress_type = fs_info->compress_type; |
Nikolay Borisov | ac3e993 | 2019-07-17 14:41:44 +0300 | [diff] [blame] | 605 | int compressed_extents = 0; |
Chris Mason | 4adaa61 | 2013-03-26 13:07:00 -0400 | [diff] [blame] | 606 | int redirty = 0; |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 607 | |
Nikolay Borisov | 6158e1c | 2017-02-20 13:50:43 +0200 | [diff] [blame] | 608 | inode_should_defrag(BTRFS_I(inode), start, end, end - start + 1, |
| 609 | SZ_16K); |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 610 | |
Josef Bacik | d98da49 | 2019-10-11 09:03:54 -0400 | [diff] [blame] | 611 | /* |
| 612 | * We need to save i_size before now because it could change in between |
| 613 | * us evaluating the size and assigning it. This is because we lock and |
| 614 | * unlock the page in truncate and fallocate, and then modify the i_size |
| 615 | * later on. |
| 616 | * |
| 617 | * The barriers are to emulate READ_ONCE, remove that once i_size_read |
| 618 | * does that for us. |
| 619 | */ |
| 620 | barrier(); |
| 621 | i_size = i_size_read(inode); |
| 622 | barrier(); |
| 623 | actual_end = min_t(u64, i_size, end + 1); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 624 | again: |
| 625 | will_compress = 0; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 626 | nr_pages = (end >> PAGE_SHIFT) - (start >> PAGE_SHIFT) + 1; |
David Sterba | 069eac7 | 2017-02-14 19:36:54 +0100 | [diff] [blame] | 627 | BUILD_BUG_ON((BTRFS_MAX_COMPRESSED % PAGE_SIZE) != 0); |
| 628 | nr_pages = min_t(unsigned long, nr_pages, |
| 629 | BTRFS_MAX_COMPRESSED / PAGE_SIZE); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 630 | |
Chris Mason | f03d9301f | 2009-02-04 09:31:06 -0500 | [diff] [blame] | 631 | /* |
| 632 | * we don't want to send crud past the end of i_size through |
| 633 | * compression, that's just a waste of CPU time. So, if the |
| 634 | * end of the file is before the start of our current |
| 635 | * requested range of bytes, we bail out to the uncompressed |
| 636 | * cleanup code that can deal with all of this. |
| 637 | * |
| 638 | * It isn't really the fastest way to fix things, but this is a |
| 639 | * very uncommon corner. |
| 640 | */ |
| 641 | if (actual_end <= start) |
| 642 | goto cleanup_and_bail_uncompressed; |
| 643 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 644 | total_compressed = actual_end - start; |
| 645 | |
Shilong Wang | 4bcbb33 | 2014-10-07 18:44:35 -0400 | [diff] [blame] | 646 | /* |
Qu Wenruo | 0cf9b24 | 2021-09-27 15:22:08 +0800 | [diff] [blame] | 647 | * Skip compression for a small file range(<=blocksize) that |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 648 | * 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] | 649 | */ |
| 650 | if (total_compressed <= blocksize && |
| 651 | (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size)) |
| 652 | goto cleanup_and_bail_uncompressed; |
| 653 | |
Qu Wenruo | 0cf9b24 | 2021-09-27 15:22:08 +0800 | [diff] [blame] | 654 | /* |
| 655 | * For subpage case, we require full page alignment for the sector |
| 656 | * aligned range. |
| 657 | * Thus we must also check against @actual_end, not just @end. |
| 658 | */ |
| 659 | if (blocksize < PAGE_SIZE) { |
| 660 | if (!IS_ALIGNED(start, PAGE_SIZE) || |
| 661 | !IS_ALIGNED(round_up(actual_end, blocksize), PAGE_SIZE)) |
| 662 | goto cleanup_and_bail_uncompressed; |
| 663 | } |
| 664 | |
David Sterba | 069eac7 | 2017-02-14 19:36:54 +0100 | [diff] [blame] | 665 | total_compressed = min_t(unsigned long, total_compressed, |
| 666 | BTRFS_MAX_UNCOMPRESSED); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 667 | total_in = 0; |
| 668 | ret = 0; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 669 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 670 | /* |
| 671 | * we do compression for mount -o compress and when the |
| 672 | * inode has not been flagged as nocompress. This flag can |
| 673 | * change at any time if we discover bad compression ratios. |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 674 | */ |
Qu Wenruo | 4e96557 | 2021-08-25 13:41:42 +0800 | [diff] [blame] | 675 | if (inode_need_compress(BTRFS_I(inode), start, end)) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 676 | WARN_ON(pages); |
David Sterba | 31e818f | 2015-02-20 18:00:26 +0100 | [diff] [blame] | 677 | pages = kcalloc(nr_pages, sizeof(struct page *), GFP_NOFS); |
Li Zefan | 560f7d7 | 2011-09-08 10:22:01 +0800 | [diff] [blame] | 678 | if (!pages) { |
| 679 | /* just bail out to the uncompressed code */ |
Filipe Manana | 3527a01 | 2018-10-13 00:37:25 +0100 | [diff] [blame] | 680 | nr_pages = 0; |
Li Zefan | 560f7d7 | 2011-09-08 10:22:01 +0800 | [diff] [blame] | 681 | goto cont; |
| 682 | } |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 683 | |
David Sterba | eec63c6 | 2017-07-17 19:41:31 +0200 | [diff] [blame] | 684 | if (BTRFS_I(inode)->defrag_compress) |
| 685 | compress_type = BTRFS_I(inode)->defrag_compress; |
| 686 | else if (BTRFS_I(inode)->prop_compress) |
David Sterba | b52aa8c | 2017-07-17 19:17:20 +0200 | [diff] [blame] | 687 | compress_type = BTRFS_I(inode)->prop_compress; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 688 | |
Chris Mason | 4adaa61 | 2013-03-26 13:07:00 -0400 | [diff] [blame] | 689 | /* |
| 690 | * we need to call clear_page_dirty_for_io on each |
| 691 | * page in the range. Otherwise applications with the file |
| 692 | * mmap'd can wander in and change the page contents while |
| 693 | * we are compressing them. |
| 694 | * |
| 695 | * If the compression fails for any reason, we set the pages |
| 696 | * dirty again later on. |
Timofey Titovets | e9679de | 2017-10-24 01:29:48 +0300 | [diff] [blame] | 697 | * |
| 698 | * Note that the remaining part is redirtied, the start pointer |
| 699 | * has moved, the end is the original one. |
Chris Mason | 4adaa61 | 2013-03-26 13:07:00 -0400 | [diff] [blame] | 700 | */ |
Timofey Titovets | e9679de | 2017-10-24 01:29:48 +0300 | [diff] [blame] | 701 | if (!redirty) { |
| 702 | extent_range_clear_dirty_for_io(inode, start, end); |
| 703 | redirty = 1; |
| 704 | } |
David Sterba | f51d2b5 | 2017-09-15 17:36:57 +0200 | [diff] [blame] | 705 | |
| 706 | /* Compression level is applied here and only here */ |
| 707 | ret = btrfs_compress_pages( |
| 708 | compress_type | (fs_info->compress_level << 4), |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 709 | inode->i_mapping, start, |
David Sterba | 38c3146 | 2017-02-14 19:04:07 +0100 | [diff] [blame] | 710 | pages, |
David Sterba | 4d3a800 | 2017-02-14 19:04:07 +0100 | [diff] [blame] | 711 | &nr_pages, |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 712 | &total_in, |
David Sterba | e5d7490 | 2017-02-14 19:45:05 +0100 | [diff] [blame] | 713 | &total_compressed); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 714 | |
| 715 | if (!ret) { |
Johannes Thumshirn | 7073017 | 2018-12-05 15:23:03 +0100 | [diff] [blame] | 716 | unsigned long offset = offset_in_page(total_compressed); |
David Sterba | 4d3a800 | 2017-02-14 19:04:07 +0100 | [diff] [blame] | 717 | struct page *page = pages[nr_pages - 1]; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 718 | |
| 719 | /* zero the tail end of the last page, we might be |
| 720 | * sending it down to disk |
| 721 | */ |
Ira Weiny | d048b9c | 2021-05-04 18:40:07 -0700 | [diff] [blame] | 722 | if (offset) |
| 723 | memzero_page(page, offset, PAGE_SIZE - offset); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 724 | will_compress = 1; |
| 725 | } |
| 726 | } |
Li Zefan | 560f7d7 | 2011-09-08 10:22:01 +0800 | [diff] [blame] | 727 | cont: |
Qu Wenruo | 7367253 | 2021-07-26 14:34:59 +0800 | [diff] [blame] | 728 | /* |
| 729 | * Check cow_file_range() for why we don't even try to create inline |
| 730 | * extent for subpage case. |
| 731 | */ |
| 732 | if (start == 0 && fs_info->sectorsize == PAGE_SIZE) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 733 | /* lets try to make an inline extent */ |
Timofey Titovets | 6018ba0 | 2017-09-15 01:57:26 +0300 | [diff] [blame] | 734 | if (ret || total_in < actual_end) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 735 | /* we didn't compress the entire range, try |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 736 | * to make an uncompressed inline extent. |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 737 | */ |
Nikolay Borisov | a034940 | 2020-06-03 08:55:12 +0300 | [diff] [blame] | 738 | ret = cow_file_range_inline(BTRFS_I(inode), start, end, |
| 739 | 0, BTRFS_COMPRESS_NONE, |
| 740 | NULL); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 741 | } else { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 742 | /* try making a compressed inline extent */ |
Nikolay Borisov | a034940 | 2020-06-03 08:55:12 +0300 | [diff] [blame] | 743 | ret = cow_file_range_inline(BTRFS_I(inode), start, end, |
Li Zefan | fe3f566 | 2011-03-28 08:30:38 +0000 | [diff] [blame] | 744 | total_compressed, |
| 745 | compress_type, pages); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 746 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 747 | if (ret <= 0) { |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 748 | unsigned long clear_flags = EXTENT_DELALLOC | |
Josef Bacik | 8b62f87 | 2017-10-19 14:15:55 -0400 | [diff] [blame] | 749 | EXTENT_DELALLOC_NEW | EXTENT_DEFRAG | |
| 750 | EXTENT_DO_ACCOUNTING; |
Filipe Manana | e6eb431 | 2014-10-10 10:45:12 +0100 | [diff] [blame] | 751 | unsigned long page_error_op; |
| 752 | |
Filipe Manana | e6eb431 | 2014-10-10 10:45:12 +0100 | [diff] [blame] | 753 | page_error_op = ret < 0 ? PAGE_SET_ERROR : 0; |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 754 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 755 | /* |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 756 | * inline extent creation worked or returned error, |
| 757 | * we don't need to create any more async work items. |
| 758 | * Unlock and free up our temp pages. |
Josef Bacik | 8b62f87 | 2017-10-19 14:15:55 -0400 | [diff] [blame] | 759 | * |
| 760 | * We use DO_ACCOUNTING here because we need the |
| 761 | * delalloc_release_metadata to be done _after_ we drop |
| 762 | * our outstanding extent for clearing delalloc for this |
| 763 | * range. |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 764 | */ |
Nikolay Borisov | ad7ff17 | 2020-06-03 08:55:06 +0300 | [diff] [blame] | 765 | extent_clear_unlock_delalloc(BTRFS_I(inode), start, end, |
| 766 | NULL, |
Nikolay Borisov | 74e9194 | 2019-07-17 16:18:16 +0300 | [diff] [blame] | 767 | clear_flags, |
Qu Wenruo | ba8b04c | 2016-07-19 16:50:36 +0800 | [diff] [blame] | 768 | PAGE_UNLOCK | |
Qu Wenruo | 6869b0a | 2021-01-26 16:33:45 +0800 | [diff] [blame] | 769 | PAGE_START_WRITEBACK | |
Filipe Manana | e6eb431 | 2014-10-10 10:45:12 +0100 | [diff] [blame] | 770 | page_error_op | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 771 | PAGE_END_WRITEBACK); |
Nikolay Borisov | cecc8d9 | 2019-07-17 14:41:45 +0300 | [diff] [blame] | 772 | |
Qu Wenruo | 1e6e238 | 2020-07-28 16:39:26 +0800 | [diff] [blame] | 773 | /* |
| 774 | * Ensure we only free the compressed pages if we have |
| 775 | * them allocated, as we can still reach here with |
| 776 | * inode_need_compress() == false. |
| 777 | */ |
| 778 | if (pages) { |
| 779 | for (i = 0; i < nr_pages; i++) { |
| 780 | WARN_ON(pages[i]->mapping); |
| 781 | put_page(pages[i]); |
| 782 | } |
| 783 | kfree(pages); |
Nikolay Borisov | cecc8d9 | 2019-07-17 14:41:45 +0300 | [diff] [blame] | 784 | } |
Nikolay Borisov | cecc8d9 | 2019-07-17 14:41:45 +0300 | [diff] [blame] | 785 | return 0; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 786 | } |
| 787 | } |
| 788 | |
| 789 | if (will_compress) { |
| 790 | /* |
| 791 | * we aren't doing an inline extent round the compressed size |
| 792 | * up to a block size boundary so the allocator does sane |
| 793 | * things |
| 794 | */ |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 795 | total_compressed = ALIGN(total_compressed, blocksize); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 796 | |
| 797 | /* |
| 798 | * one last check to make sure the compression is really a |
Timofey Titovets | 170607e | 2017-06-06 14:41:15 +0300 | [diff] [blame] | 799 | * win, compare the page count read with the blocks on disk, |
| 800 | * compression must free at least one sector size |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 801 | */ |
Qu Wenruo | 4c16277 | 2021-09-27 15:21:59 +0800 | [diff] [blame] | 802 | total_in = round_up(total_in, fs_info->sectorsize); |
Timofey Titovets | 170607e | 2017-06-06 14:41:15 +0300 | [diff] [blame] | 803 | if (total_compressed + blocksize <= total_in) { |
Nikolay Borisov | ac3e993 | 2019-07-17 14:41:44 +0300 | [diff] [blame] | 804 | compressed_extents++; |
Ashish Samant | c8bb0c8 | 2016-03-25 19:01:33 -0700 | [diff] [blame] | 805 | |
| 806 | /* |
| 807 | * The async work queues will take care of doing actual |
| 808 | * allocation on disk for these compressed pages, and |
| 809 | * will submit them to the elevator. |
| 810 | */ |
Nikolay Borisov | b532627 | 2019-03-12 17:20:25 +0200 | [diff] [blame] | 811 | add_async_extent(async_chunk, start, total_in, |
David Sterba | 4d3a800 | 2017-02-14 19:04:07 +0100 | [diff] [blame] | 812 | total_compressed, pages, nr_pages, |
Ashish Samant | c8bb0c8 | 2016-03-25 19:01:33 -0700 | [diff] [blame] | 813 | compress_type); |
| 814 | |
Timofey Titovets | 1170862 | 2017-10-03 18:06:01 +0300 | [diff] [blame] | 815 | if (start + total_in < end) { |
| 816 | start += total_in; |
Ashish Samant | c8bb0c8 | 2016-03-25 19:01:33 -0700 | [diff] [blame] | 817 | pages = NULL; |
| 818 | cond_resched(); |
| 819 | goto again; |
| 820 | } |
Nikolay Borisov | ac3e993 | 2019-07-17 14:41:44 +0300 | [diff] [blame] | 821 | return compressed_extents; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 822 | } |
| 823 | } |
Ashish Samant | c8bb0c8 | 2016-03-25 19:01:33 -0700 | [diff] [blame] | 824 | if (pages) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 825 | /* |
| 826 | * the compression code ran but failed to make things smaller, |
| 827 | * free any pages it allocated and our page pointer array |
| 828 | */ |
David Sterba | 4d3a800 | 2017-02-14 19:04:07 +0100 | [diff] [blame] | 829 | for (i = 0; i < nr_pages; i++) { |
Chris Mason | 70b99e6 | 2008-10-31 12:46:39 -0400 | [diff] [blame] | 830 | WARN_ON(pages[i]->mapping); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 831 | put_page(pages[i]); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 832 | } |
| 833 | kfree(pages); |
| 834 | pages = NULL; |
| 835 | total_compressed = 0; |
David Sterba | 4d3a800 | 2017-02-14 19:04:07 +0100 | [diff] [blame] | 836 | nr_pages = 0; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 837 | |
| 838 | /* flag the file so we don't compress in the future */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 839 | if (!btrfs_test_opt(fs_info, FORCE_COMPRESS) && |
David Sterba | b52aa8c | 2017-07-17 19:17:20 +0200 | [diff] [blame] | 840 | !(BTRFS_I(inode)->prop_compress)) { |
Chris Mason | a555f81 | 2010-01-28 16:18:15 -0500 | [diff] [blame] | 841 | BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS; |
Chris Mason | 1e701a3 | 2010-03-11 09:42:04 -0500 | [diff] [blame] | 842 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 843 | } |
Chris Mason | f03d9301f | 2009-02-04 09:31:06 -0500 | [diff] [blame] | 844 | cleanup_and_bail_uncompressed: |
Ashish Samant | c8bb0c8 | 2016-03-25 19:01:33 -0700 | [diff] [blame] | 845 | /* |
| 846 | * No compression, but we still need to write the pages in the file |
| 847 | * we've been given so far. redirty the locked page if it corresponds |
| 848 | * to our extent and set things up for the async work queue to run |
| 849 | * cow_file_range to do the normal delalloc dance. |
| 850 | */ |
Chris Mason | 1d53c9e | 2019-07-10 12:28:16 -0700 | [diff] [blame] | 851 | if (async_chunk->locked_page && |
| 852 | (page_offset(async_chunk->locked_page) >= start && |
| 853 | page_offset(async_chunk->locked_page)) <= end) { |
Nikolay Borisov | 1368c6d | 2019-03-12 17:20:27 +0200 | [diff] [blame] | 854 | __set_page_dirty_nobuffers(async_chunk->locked_page); |
Ashish Samant | c8bb0c8 | 2016-03-25 19:01:33 -0700 | [diff] [blame] | 855 | /* unlocked later on in the async handlers */ |
Chris Mason | 1d53c9e | 2019-07-10 12:28:16 -0700 | [diff] [blame] | 856 | } |
Ashish Samant | c8bb0c8 | 2016-03-25 19:01:33 -0700 | [diff] [blame] | 857 | |
| 858 | if (redirty) |
| 859 | extent_range_redirty_for_io(inode, start, end); |
Nikolay Borisov | b532627 | 2019-03-12 17:20:25 +0200 | [diff] [blame] | 860 | add_async_extent(async_chunk, start, end - start + 1, 0, NULL, 0, |
Ashish Samant | c8bb0c8 | 2016-03-25 19:01:33 -0700 | [diff] [blame] | 861 | BTRFS_COMPRESS_NONE); |
Nikolay Borisov | ac3e993 | 2019-07-17 14:41:44 +0300 | [diff] [blame] | 862 | compressed_extents++; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 863 | |
Nikolay Borisov | ac3e993 | 2019-07-17 14:41:44 +0300 | [diff] [blame] | 864 | return compressed_extents; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 865 | } |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 866 | |
Filipe Manana | 40ae837 | 2014-10-06 22:14:24 +0100 | [diff] [blame] | 867 | static void free_async_extent_pages(struct async_extent *async_extent) |
| 868 | { |
| 869 | int i; |
| 870 | |
| 871 | if (!async_extent->pages) |
| 872 | return; |
| 873 | |
| 874 | for (i = 0; i < async_extent->nr_pages; i++) { |
| 875 | WARN_ON(async_extent->pages[i]->mapping); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 876 | put_page(async_extent->pages[i]); |
Filipe Manana | 40ae837 | 2014-10-06 22:14:24 +0100 | [diff] [blame] | 877 | } |
| 878 | kfree(async_extent->pages); |
| 879 | async_extent->nr_pages = 0; |
| 880 | async_extent->pages = NULL; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 881 | } |
| 882 | |
Qu Wenruo | 2b83a0e | 2021-09-27 15:22:03 +0800 | [diff] [blame] | 883 | static int submit_uncompressed_range(struct btrfs_inode *inode, |
| 884 | struct async_extent *async_extent, |
| 885 | struct page *locked_page) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 886 | { |
Qu Wenruo | 2b83a0e | 2021-09-27 15:22:03 +0800 | [diff] [blame] | 887 | u64 start = async_extent->start; |
| 888 | u64 end = async_extent->start + async_extent->ram_size - 1; |
| 889 | unsigned long nr_written = 0; |
| 890 | int page_started = 0; |
| 891 | int ret; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 892 | |
Qu Wenruo | 2b83a0e | 2021-09-27 15:22:03 +0800 | [diff] [blame] | 893 | /* |
| 894 | * Call cow_file_range() to run the delalloc range directly, since we |
| 895 | * won't go to NOCOW or async path again. |
| 896 | * |
| 897 | * Also we call cow_file_range() with @unlock_page == 0, so that we |
| 898 | * can directly submit them without interruption. |
| 899 | */ |
| 900 | ret = cow_file_range(inode, locked_page, start, end, &page_started, |
| 901 | &nr_written, 0); |
| 902 | /* Inline extent inserted, page gets unlocked and everything is done */ |
| 903 | if (page_started) { |
| 904 | ret = 0; |
| 905 | goto out; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 906 | } |
Qu Wenruo | 2b83a0e | 2021-09-27 15:22:03 +0800 | [diff] [blame] | 907 | if (ret < 0) { |
| 908 | if (locked_page) |
| 909 | unlock_page(locked_page); |
| 910 | goto out; |
| 911 | } |
| 912 | |
| 913 | ret = extent_write_locked_range(&inode->vfs_inode, start, end); |
| 914 | /* All pages will be unlocked, including @locked_page */ |
| 915 | out: |
| 916 | kfree(async_extent); |
| 917 | return ret; |
| 918 | } |
| 919 | |
Qu Wenruo | b4ccace | 2021-09-27 15:21:57 +0800 | [diff] [blame] | 920 | static int submit_one_async_extent(struct btrfs_inode *inode, |
| 921 | struct async_chunk *async_chunk, |
| 922 | struct async_extent *async_extent, |
| 923 | u64 *alloc_hint) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 924 | { |
Qu Wenruo | b4ccace | 2021-09-27 15:21:57 +0800 | [diff] [blame] | 925 | struct extent_io_tree *io_tree = &inode->io_tree; |
| 926 | struct btrfs_root *root = inode->root; |
| 927 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 928 | struct btrfs_key ins; |
Qu Wenruo | 2b83a0e | 2021-09-27 15:22:03 +0800 | [diff] [blame] | 929 | struct page *locked_page = NULL; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 930 | struct extent_map *em; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 931 | int ret = 0; |
Qu Wenruo | b4ccace | 2021-09-27 15:21:57 +0800 | [diff] [blame] | 932 | u64 start = async_extent->start; |
| 933 | u64 end = async_extent->start + async_extent->ram_size - 1; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 934 | |
Qu Wenruo | 2b83a0e | 2021-09-27 15:22:03 +0800 | [diff] [blame] | 935 | /* |
| 936 | * If async_chunk->locked_page is in the async_extent range, we need to |
| 937 | * handle it. |
| 938 | */ |
| 939 | if (async_chunk->locked_page) { |
| 940 | u64 locked_page_start = page_offset(async_chunk->locked_page); |
| 941 | u64 locked_page_end = locked_page_start + PAGE_SIZE - 1; |
| 942 | |
| 943 | if (!(start >= locked_page_end || end <= locked_page_start)) |
| 944 | locked_page = async_chunk->locked_page; |
| 945 | } |
Qu Wenruo | b4ccace | 2021-09-27 15:21:57 +0800 | [diff] [blame] | 946 | lock_extent(io_tree, start, end); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 947 | |
Qu Wenruo | 2b83a0e | 2021-09-27 15:22:03 +0800 | [diff] [blame] | 948 | /* We have fall back to uncompressed write */ |
| 949 | if (!async_extent->pages) |
| 950 | return submit_uncompressed_range(inode, async_extent, locked_page); |
Qu Wenruo | b4ccace | 2021-09-27 15:21:57 +0800 | [diff] [blame] | 951 | |
| 952 | ret = btrfs_reserve_extent(root, async_extent->ram_size, |
| 953 | async_extent->compressed_size, |
| 954 | async_extent->compressed_size, |
| 955 | 0, *alloc_hint, &ins, 1, 1); |
| 956 | if (ret) { |
| 957 | free_async_extent_pages(async_extent); |
| 958 | /* |
| 959 | * Here we used to try again by going back to non-compressed |
| 960 | * path for ENOSPC. But we can't reserve space even for |
| 961 | * compressed size, how could it work for uncompressed size |
| 962 | * which requires larger size? So here we directly go error |
| 963 | * path. |
| 964 | */ |
| 965 | goto out_free; |
| 966 | } |
| 967 | |
| 968 | /* Here we're doing allocation and writeback of the compressed pages */ |
| 969 | em = create_io_em(inode, start, |
| 970 | async_extent->ram_size, /* len */ |
| 971 | start, /* orig_start */ |
| 972 | ins.objectid, /* block_start */ |
| 973 | ins.offset, /* block_len */ |
| 974 | ins.offset, /* orig_block_len */ |
| 975 | async_extent->ram_size, /* ram_bytes */ |
| 976 | async_extent->compress_type, |
| 977 | BTRFS_ORDERED_COMPRESSED); |
| 978 | if (IS_ERR(em)) { |
| 979 | ret = PTR_ERR(em); |
| 980 | goto out_free_reserve; |
| 981 | } |
| 982 | free_extent_map(em); |
| 983 | |
| 984 | ret = btrfs_add_ordered_extent_compress(inode, start, /* file_offset */ |
| 985 | ins.objectid, /* disk_bytenr */ |
| 986 | async_extent->ram_size, /* num_bytes */ |
| 987 | ins.offset, /* disk_num_bytes */ |
| 988 | async_extent->compress_type); |
| 989 | if (ret) { |
| 990 | btrfs_drop_extent_cache(inode, start, end, 0); |
| 991 | goto out_free_reserve; |
| 992 | } |
| 993 | btrfs_dec_block_group_reservations(fs_info, ins.objectid); |
| 994 | |
| 995 | /* Clear dirty, set writeback and unlock the pages. */ |
| 996 | extent_clear_unlock_delalloc(inode, start, end, |
| 997 | NULL, EXTENT_LOCKED | EXTENT_DELALLOC, |
| 998 | PAGE_UNLOCK | PAGE_START_WRITEBACK); |
| 999 | if (btrfs_submit_compressed_write(inode, start, /* file_offset */ |
| 1000 | async_extent->ram_size, /* num_bytes */ |
| 1001 | ins.objectid, /* disk_bytenr */ |
| 1002 | ins.offset, /* compressed_len */ |
| 1003 | async_extent->pages, /* compressed_pages */ |
| 1004 | async_extent->nr_pages, |
| 1005 | async_chunk->write_flags, |
| 1006 | async_chunk->blkcg_css)) { |
| 1007 | const u64 start = async_extent->start; |
| 1008 | const u64 end = start + async_extent->ram_size - 1; |
| 1009 | |
| 1010 | btrfs_writepage_endio_finish_ordered(inode, NULL, start, end, 0); |
| 1011 | |
| 1012 | extent_clear_unlock_delalloc(inode, start, end, NULL, 0, |
| 1013 | PAGE_END_WRITEBACK | PAGE_SET_ERROR); |
| 1014 | free_async_extent_pages(async_extent); |
| 1015 | } |
| 1016 | *alloc_hint = ins.objectid + ins.offset; |
| 1017 | kfree(async_extent); |
| 1018 | return ret; |
| 1019 | |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 1020 | out_free_reserve: |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1021 | btrfs_dec_block_group_reservations(fs_info, ins.objectid); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 1022 | btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1023 | out_free: |
Qu Wenruo | b4ccace | 2021-09-27 15:21:57 +0800 | [diff] [blame] | 1024 | extent_clear_unlock_delalloc(inode, start, end, |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1025 | NULL, EXTENT_LOCKED | EXTENT_DELALLOC | |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 1026 | EXTENT_DELALLOC_NEW | |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 1027 | EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING, |
Qu Wenruo | 6869b0a | 2021-01-26 16:33:45 +0800 | [diff] [blame] | 1028 | PAGE_UNLOCK | PAGE_START_WRITEBACK | |
| 1029 | PAGE_END_WRITEBACK | PAGE_SET_ERROR); |
Filipe Manana | 40ae837 | 2014-10-06 22:14:24 +0100 | [diff] [blame] | 1030 | free_async_extent_pages(async_extent); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1031 | kfree(async_extent); |
Qu Wenruo | b4ccace | 2021-09-27 15:21:57 +0800 | [diff] [blame] | 1032 | return ret; |
| 1033 | } |
| 1034 | |
| 1035 | /* |
| 1036 | * Phase two of compressed writeback. This is the ordered portion of the code, |
| 1037 | * which only gets called in the order the work was queued. We walk all the |
| 1038 | * async extents created by compress_file_range and send them down to the disk. |
| 1039 | */ |
| 1040 | static noinline void submit_compressed_extents(struct async_chunk *async_chunk) |
| 1041 | { |
| 1042 | struct btrfs_inode *inode = BTRFS_I(async_chunk->inode); |
| 1043 | struct btrfs_fs_info *fs_info = inode->root->fs_info; |
| 1044 | struct async_extent *async_extent; |
| 1045 | u64 alloc_hint = 0; |
| 1046 | int ret = 0; |
| 1047 | |
| 1048 | while (!list_empty(&async_chunk->extents)) { |
| 1049 | u64 extent_start; |
| 1050 | u64 ram_size; |
| 1051 | |
| 1052 | async_extent = list_entry(async_chunk->extents.next, |
| 1053 | struct async_extent, list); |
| 1054 | list_del(&async_extent->list); |
| 1055 | extent_start = async_extent->start; |
| 1056 | ram_size = async_extent->ram_size; |
| 1057 | |
| 1058 | ret = submit_one_async_extent(inode, async_chunk, async_extent, |
| 1059 | &alloc_hint); |
| 1060 | btrfs_debug(fs_info, |
| 1061 | "async extent submission failed root=%lld inode=%llu start=%llu len=%llu ret=%d", |
| 1062 | inode->root->root_key.objectid, |
| 1063 | btrfs_ino(inode), extent_start, ram_size, ret); |
| 1064 | } |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1065 | } |
| 1066 | |
Nikolay Borisov | 43c6984 | 2020-06-03 08:55:02 +0300 | [diff] [blame] | 1067 | static u64 get_extent_allocation_hint(struct btrfs_inode *inode, u64 start, |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 1068 | u64 num_bytes) |
| 1069 | { |
Nikolay Borisov | 43c6984 | 2020-06-03 08:55:02 +0300 | [diff] [blame] | 1070 | struct extent_map_tree *em_tree = &inode->extent_tree; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 1071 | struct extent_map *em; |
| 1072 | u64 alloc_hint = 0; |
| 1073 | |
| 1074 | read_lock(&em_tree->lock); |
| 1075 | em = search_extent_mapping(em_tree, start, num_bytes); |
| 1076 | if (em) { |
| 1077 | /* |
| 1078 | * if block start isn't an actual block number then find the |
| 1079 | * first block in this inode and use that as a hint. If that |
| 1080 | * block is also bogus then just don't worry about it. |
| 1081 | */ |
| 1082 | if (em->block_start >= EXTENT_MAP_LAST_BYTE) { |
| 1083 | free_extent_map(em); |
| 1084 | em = search_extent_mapping(em_tree, 0, 0); |
| 1085 | if (em && em->block_start < EXTENT_MAP_LAST_BYTE) |
| 1086 | alloc_hint = em->block_start; |
| 1087 | if (em) |
| 1088 | free_extent_map(em); |
| 1089 | } else { |
| 1090 | alloc_hint = em->block_start; |
| 1091 | free_extent_map(em); |
| 1092 | } |
| 1093 | } |
| 1094 | read_unlock(&em_tree->lock); |
| 1095 | |
| 1096 | return alloc_hint; |
| 1097 | } |
| 1098 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1099 | /* |
| 1100 | * when extent_io.c finds a delayed allocation range in the file, |
| 1101 | * the call backs end up in this code. The basic idea is to |
| 1102 | * allocate extents on disk for the range, and create ordered data structs |
| 1103 | * in ram to track those extents. |
| 1104 | * |
| 1105 | * locked_page is the page that writepage had locked already. We use |
| 1106 | * it to make sure we don't do extra locks or unlocks. |
| 1107 | * |
| 1108 | * *page_started is set to one if we unlock locked_page and do everything |
| 1109 | * required to start IO on it. It may be clean and already done with |
| 1110 | * IO when we return. |
| 1111 | */ |
Nikolay Borisov | 6e26c44 | 2020-06-03 08:55:14 +0300 | [diff] [blame] | 1112 | static noinline int cow_file_range(struct btrfs_inode *inode, |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 1113 | struct page *locked_page, |
Nikolay Borisov | 74e9194 | 2019-07-17 16:18:16 +0300 | [diff] [blame] | 1114 | u64 start, u64 end, int *page_started, |
Nikolay Borisov | 330a582 | 2019-07-17 16:18:17 +0300 | [diff] [blame] | 1115 | unsigned long *nr_written, int unlock) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1116 | { |
Nikolay Borisov | 6e26c44 | 2020-06-03 08:55:14 +0300 | [diff] [blame] | 1117 | struct btrfs_root *root = inode->root; |
| 1118 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1119 | u64 alloc_hint = 0; |
| 1120 | u64 num_bytes; |
| 1121 | unsigned long ram_size; |
Filipe Manana | a315e68 | 2017-03-06 23:04:20 +0000 | [diff] [blame] | 1122 | u64 cur_alloc_size = 0; |
Filipe Manana | 432cd2a | 2020-06-08 13:32:55 +0100 | [diff] [blame] | 1123 | u64 min_alloc_size; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1124 | u64 blocksize = fs_info->sectorsize; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1125 | struct btrfs_key ins; |
| 1126 | struct extent_map *em; |
Filipe Manana | a315e68 | 2017-03-06 23:04:20 +0000 | [diff] [blame] | 1127 | unsigned clear_bits; |
| 1128 | unsigned long page_ops; |
| 1129 | bool extent_reserved = false; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1130 | int ret = 0; |
| 1131 | |
Nikolay Borisov | 6e26c44 | 2020-06-03 08:55:14 +0300 | [diff] [blame] | 1132 | if (btrfs_is_free_space_inode(inode)) { |
Josef Bacik | 02ecd2c | 2013-10-25 16:19:08 -0400 | [diff] [blame] | 1133 | WARN_ON_ONCE(1); |
Josef Bacik | 29bce2f | 2014-02-07 12:21:23 -0500 | [diff] [blame] | 1134 | ret = -EINVAL; |
| 1135 | goto out_unlock; |
Josef Bacik | 02ecd2c | 2013-10-25 16:19:08 -0400 | [diff] [blame] | 1136 | } |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1137 | |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 1138 | num_bytes = ALIGN(end - start + 1, blocksize); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1139 | num_bytes = max(blocksize, num_bytes); |
Anand Jain | 566b176 | 2018-02-15 18:07:59 +0800 | [diff] [blame] | 1140 | ASSERT(num_bytes <= btrfs_super_total_bytes(fs_info->super_copy)); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1141 | |
Nikolay Borisov | 6e26c44 | 2020-06-03 08:55:14 +0300 | [diff] [blame] | 1142 | inode_should_defrag(inode, start, end, num_bytes, SZ_64K); |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1143 | |
Qu Wenruo | 7367253 | 2021-07-26 14:34:59 +0800 | [diff] [blame] | 1144 | /* |
| 1145 | * Due to the page size limit, for subpage we can only trigger the |
| 1146 | * writeback for the dirty sectors of page, that means data writeback |
| 1147 | * is doing more writeback than what we want. |
| 1148 | * |
| 1149 | * This is especially unexpected for some call sites like fallocate, |
| 1150 | * where we only increase i_size after everything is done. |
| 1151 | * This means we can trigger inline extent even if we didn't want to. |
| 1152 | * So here we skip inline extent creation completely. |
| 1153 | */ |
| 1154 | if (start == 0 && fs_info->sectorsize == PAGE_SIZE) { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1155 | /* lets try to make an inline extent */ |
Nikolay Borisov | 6e26c44 | 2020-06-03 08:55:14 +0300 | [diff] [blame] | 1156 | ret = cow_file_range_inline(inode, start, end, 0, |
Nikolay Borisov | d02c0e2 | 2018-03-02 09:43:15 +0200 | [diff] [blame] | 1157 | BTRFS_COMPRESS_NONE, NULL); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1158 | if (ret == 0) { |
Josef Bacik | 8b62f87 | 2017-10-19 14:15:55 -0400 | [diff] [blame] | 1159 | /* |
| 1160 | * We use DO_ACCOUNTING here because we need the |
| 1161 | * delalloc_release_metadata to be run _after_ we drop |
| 1162 | * our outstanding extent for clearing delalloc for this |
| 1163 | * range. |
| 1164 | */ |
Qu Wenruo | 4750af3 | 2021-05-31 16:50:48 +0800 | [diff] [blame] | 1165 | extent_clear_unlock_delalloc(inode, start, end, |
| 1166 | locked_page, |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1167 | EXTENT_LOCKED | EXTENT_DELALLOC | |
Josef Bacik | 8b62f87 | 2017-10-19 14:15:55 -0400 | [diff] [blame] | 1168 | EXTENT_DELALLOC_NEW | EXTENT_DEFRAG | |
| 1169 | EXTENT_DO_ACCOUNTING, PAGE_UNLOCK | |
Qu Wenruo | 6869b0a | 2021-01-26 16:33:45 +0800 | [diff] [blame] | 1170 | PAGE_START_WRITEBACK | PAGE_END_WRITEBACK); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1171 | *nr_written = *nr_written + |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1172 | (end - start + PAGE_SIZE) / PAGE_SIZE; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1173 | *page_started = 1; |
Qu Wenruo | 4750af3 | 2021-05-31 16:50:48 +0800 | [diff] [blame] | 1174 | /* |
| 1175 | * locked_page is locked by the caller of |
| 1176 | * writepage_delalloc(), not locked by |
| 1177 | * __process_pages_contig(). |
| 1178 | * |
| 1179 | * We can't let __process_pages_contig() to unlock it, |
| 1180 | * as it doesn't have any subpage::writers recorded. |
| 1181 | * |
| 1182 | * Here we manually unlock the page, since the caller |
| 1183 | * can't use page_started to determine if it's an |
| 1184 | * inline extent or a compressed extent. |
| 1185 | */ |
| 1186 | unlock_page(locked_page); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1187 | goto out; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1188 | } else if (ret < 0) { |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1189 | goto out_unlock; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1190 | } |
| 1191 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1192 | |
Nikolay Borisov | 6e26c44 | 2020-06-03 08:55:14 +0300 | [diff] [blame] | 1193 | alloc_hint = get_extent_allocation_hint(inode, start, num_bytes); |
| 1194 | btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 1195 | |
Filipe Manana | 432cd2a | 2020-06-08 13:32:55 +0100 | [diff] [blame] | 1196 | /* |
| 1197 | * Relocation relies on the relocated extents to have exactly the same |
| 1198 | * size as the original extents. Normally writeback for relocation data |
| 1199 | * extents follows a NOCOW path because relocation preallocates the |
| 1200 | * extents. However, due to an operation such as scrub turning a block |
| 1201 | * group to RO mode, it may fallback to COW mode, so we must make sure |
| 1202 | * an extent allocated during COW has exactly the requested size and can |
| 1203 | * not be split into smaller extents, otherwise relocation breaks and |
| 1204 | * fails during the stage where it updates the bytenr of file extent |
| 1205 | * items. |
| 1206 | */ |
Johannes Thumshirn | 37f00a6 | 2021-09-09 01:19:25 +0900 | [diff] [blame] | 1207 | if (btrfs_is_data_reloc_root(root)) |
Filipe Manana | 432cd2a | 2020-06-08 13:32:55 +0100 | [diff] [blame] | 1208 | min_alloc_size = num_bytes; |
| 1209 | else |
| 1210 | min_alloc_size = fs_info->sectorsize; |
| 1211 | |
Anand Jain | 3752d22 | 2018-02-15 12:29:38 +0800 | [diff] [blame] | 1212 | while (num_bytes > 0) { |
| 1213 | cur_alloc_size = num_bytes; |
Wang Xiaoguang | 1851309 | 2016-07-25 15:51:40 +0800 | [diff] [blame] | 1214 | ret = btrfs_reserve_extent(root, cur_alloc_size, cur_alloc_size, |
Filipe Manana | 432cd2a | 2020-06-08 13:32:55 +0100 | [diff] [blame] | 1215 | min_alloc_size, 0, alloc_hint, |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 1216 | &ins, 1, 1); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 1217 | if (ret < 0) |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1218 | goto out_unlock; |
Filipe Manana | a315e68 | 2017-03-06 23:04:20 +0000 | [diff] [blame] | 1219 | cur_alloc_size = ins.offset; |
| 1220 | extent_reserved = true; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1221 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1222 | ram_size = ins.offset; |
Nikolay Borisov | 6e26c44 | 2020-06-03 08:55:14 +0300 | [diff] [blame] | 1223 | em = create_io_em(inode, start, ins.offset, /* len */ |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 1224 | start, /* orig_start */ |
| 1225 | ins.objectid, /* block_start */ |
| 1226 | ins.offset, /* block_len */ |
| 1227 | ins.offset, /* orig_block_len */ |
| 1228 | ram_size, /* ram_bytes */ |
| 1229 | BTRFS_COMPRESS_NONE, /* compress_type */ |
Liu Bo | 1af4a0a | 2017-02-13 15:35:09 -0800 | [diff] [blame] | 1230 | BTRFS_ORDERED_REGULAR /* type */); |
Su Yue | 090a127a | 2018-05-30 16:48:56 +0800 | [diff] [blame] | 1231 | if (IS_ERR(em)) { |
| 1232 | ret = PTR_ERR(em); |
Liu Bo | ace68ba | 2013-04-22 10:53:47 +0000 | [diff] [blame] | 1233 | goto out_reserve; |
Su Yue | 090a127a | 2018-05-30 16:48:56 +0800 | [diff] [blame] | 1234 | } |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 1235 | free_extent_map(em); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1236 | |
Nikolay Borisov | 6e26c44 | 2020-06-03 08:55:14 +0300 | [diff] [blame] | 1237 | ret = btrfs_add_ordered_extent(inode, start, ins.objectid, |
Qu Wenruo | 3c198fe | 2021-01-21 14:13:54 +0800 | [diff] [blame] | 1238 | ram_size, cur_alloc_size, |
| 1239 | BTRFS_ORDERED_REGULAR); |
Liu Bo | ace68ba | 2013-04-22 10:53:47 +0000 | [diff] [blame] | 1240 | if (ret) |
Filipe Manana | d9f8596 | 2014-08-25 10:43:00 +0100 | [diff] [blame] | 1241 | goto out_drop_extent_cache; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1242 | |
Johannes Thumshirn | 37f00a6 | 2021-09-09 01:19:25 +0900 | [diff] [blame] | 1243 | if (btrfs_is_data_reloc_root(root)) { |
Nikolay Borisov | 6e26c44 | 2020-06-03 08:55:14 +0300 | [diff] [blame] | 1244 | ret = btrfs_reloc_clone_csums(inode, start, |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1245 | cur_alloc_size); |
Qu Wenruo | 4dbd80f | 2017-03-08 10:25:51 +0800 | [diff] [blame] | 1246 | /* |
| 1247 | * Only drop cache here, and process as normal. |
| 1248 | * |
| 1249 | * We must not allow extent_clear_unlock_delalloc() |
| 1250 | * at out_unlock label to free meta of this ordered |
| 1251 | * extent, as its meta should be freed by |
| 1252 | * btrfs_finish_ordered_io(). |
| 1253 | * |
| 1254 | * So we must continue until @start is increased to |
| 1255 | * skip current ordered extent. |
| 1256 | */ |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 1257 | if (ret) |
Nikolay Borisov | 6e26c44 | 2020-06-03 08:55:14 +0300 | [diff] [blame] | 1258 | btrfs_drop_extent_cache(inode, start, |
Qu Wenruo | 4dbd80f | 2017-03-08 10:25:51 +0800 | [diff] [blame] | 1259 | start + ram_size - 1, 0); |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1260 | } |
| 1261 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1262 | btrfs_dec_block_group_reservations(fs_info, ins.objectid); |
Filipe Manana | 9cfa3e3 | 2016-04-26 15:39:32 +0100 | [diff] [blame] | 1263 | |
Qu Wenruo | f57ad93 | 2021-04-07 19:22:13 +0800 | [diff] [blame] | 1264 | /* |
| 1265 | * We're not doing compressed IO, don't unlock the first page |
| 1266 | * (which the caller expects to stay locked), don't clear any |
| 1267 | * dirty bits and don't set any writeback bits |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 1268 | * |
Qu Wenruo | f57ad93 | 2021-04-07 19:22:13 +0800 | [diff] [blame] | 1269 | * Do set the Ordered (Private2) bit so we know this page was |
| 1270 | * properly setup for writepage. |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1271 | */ |
Filipe Manana | a315e68 | 2017-03-06 23:04:20 +0000 | [diff] [blame] | 1272 | page_ops = unlock ? PAGE_UNLOCK : 0; |
Qu Wenruo | f57ad93 | 2021-04-07 19:22:13 +0800 | [diff] [blame] | 1273 | page_ops |= PAGE_SET_ORDERED; |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 1274 | |
Nikolay Borisov | 6e26c44 | 2020-06-03 08:55:14 +0300 | [diff] [blame] | 1275 | extent_clear_unlock_delalloc(inode, start, start + ram_size - 1, |
Nikolay Borisov | 74e9194 | 2019-07-17 16:18:16 +0300 | [diff] [blame] | 1276 | locked_page, |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1277 | EXTENT_LOCKED | EXTENT_DELALLOC, |
Filipe Manana | a315e68 | 2017-03-06 23:04:20 +0000 | [diff] [blame] | 1278 | page_ops); |
Anand Jain | 3752d22 | 2018-02-15 12:29:38 +0800 | [diff] [blame] | 1279 | if (num_bytes < cur_alloc_size) |
| 1280 | num_bytes = 0; |
Qu Wenruo | 4dbd80f | 2017-03-08 10:25:51 +0800 | [diff] [blame] | 1281 | else |
Anand Jain | 3752d22 | 2018-02-15 12:29:38 +0800 | [diff] [blame] | 1282 | num_bytes -= cur_alloc_size; |
Chris Mason | c59f895 | 2007-12-17 20:14:04 -0500 | [diff] [blame] | 1283 | alloc_hint = ins.objectid + ins.offset; |
| 1284 | start += cur_alloc_size; |
Filipe Manana | a315e68 | 2017-03-06 23:04:20 +0000 | [diff] [blame] | 1285 | extent_reserved = false; |
Qu Wenruo | 4dbd80f | 2017-03-08 10:25:51 +0800 | [diff] [blame] | 1286 | |
| 1287 | /* |
| 1288 | * btrfs_reloc_clone_csums() error, since start is increased |
| 1289 | * extent_clear_unlock_delalloc() at out_unlock label won't |
| 1290 | * free metadata of current ordered extent, we're OK to exit. |
| 1291 | */ |
| 1292 | if (ret) |
| 1293 | goto out_unlock; |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 1294 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1295 | out: |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1296 | return ret; |
Miao Xie | b7d5b0a | 2012-11-01 07:32:18 +0000 | [diff] [blame] | 1297 | |
Filipe Manana | d9f8596 | 2014-08-25 10:43:00 +0100 | [diff] [blame] | 1298 | out_drop_extent_cache: |
Nikolay Borisov | 6e26c44 | 2020-06-03 08:55:14 +0300 | [diff] [blame] | 1299 | btrfs_drop_extent_cache(inode, start, start + ram_size - 1, 0); |
Liu Bo | ace68ba | 2013-04-22 10:53:47 +0000 | [diff] [blame] | 1300 | out_reserve: |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1301 | btrfs_dec_block_group_reservations(fs_info, ins.objectid); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 1302 | btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1303 | out_unlock: |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 1304 | clear_bits = EXTENT_LOCKED | EXTENT_DELALLOC | EXTENT_DELALLOC_NEW | |
| 1305 | EXTENT_DEFRAG | EXTENT_CLEAR_META_RESV; |
Qu Wenruo | 6869b0a | 2021-01-26 16:33:45 +0800 | [diff] [blame] | 1306 | page_ops = PAGE_UNLOCK | PAGE_START_WRITEBACK | PAGE_END_WRITEBACK; |
Filipe Manana | a315e68 | 2017-03-06 23:04:20 +0000 | [diff] [blame] | 1307 | /* |
| 1308 | * If we reserved an extent for our delalloc range (or a subrange) and |
| 1309 | * failed to create the respective ordered extent, then it means that |
| 1310 | * when we reserved the extent we decremented the extent's size from |
| 1311 | * the data space_info's bytes_may_use counter and incremented the |
| 1312 | * space_info's bytes_reserved counter by the same amount. We must make |
| 1313 | * sure extent_clear_unlock_delalloc() does not try to decrement again |
| 1314 | * the data space_info's bytes_may_use counter, therefore we do not pass |
| 1315 | * it the flag EXTENT_CLEAR_DATA_RESV. |
| 1316 | */ |
| 1317 | if (extent_reserved) { |
Nikolay Borisov | 6e26c44 | 2020-06-03 08:55:14 +0300 | [diff] [blame] | 1318 | extent_clear_unlock_delalloc(inode, start, |
Filipe Manana | e2c8e92 | 2020-05-27 11:15:53 +0100 | [diff] [blame] | 1319 | start + cur_alloc_size - 1, |
Filipe Manana | a315e68 | 2017-03-06 23:04:20 +0000 | [diff] [blame] | 1320 | locked_page, |
| 1321 | clear_bits, |
| 1322 | page_ops); |
| 1323 | start += cur_alloc_size; |
| 1324 | if (start >= end) |
| 1325 | goto out; |
| 1326 | } |
Nikolay Borisov | 6e26c44 | 2020-06-03 08:55:14 +0300 | [diff] [blame] | 1327 | extent_clear_unlock_delalloc(inode, start, end, locked_page, |
Filipe Manana | a315e68 | 2017-03-06 23:04:20 +0000 | [diff] [blame] | 1328 | clear_bits | EXTENT_CLEAR_DATA_RESV, |
| 1329 | page_ops); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1330 | goto out; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1331 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1332 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1333 | /* |
| 1334 | * work queue call back to started compression on a file and pages |
| 1335 | */ |
| 1336 | static noinline void async_cow_start(struct btrfs_work *work) |
| 1337 | { |
Nikolay Borisov | b532627 | 2019-03-12 17:20:25 +0200 | [diff] [blame] | 1338 | struct async_chunk *async_chunk; |
Nikolay Borisov | ac3e993 | 2019-07-17 14:41:44 +0300 | [diff] [blame] | 1339 | int compressed_extents; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1340 | |
Nikolay Borisov | b532627 | 2019-03-12 17:20:25 +0200 | [diff] [blame] | 1341 | async_chunk = container_of(work, struct async_chunk, work); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1342 | |
Nikolay Borisov | ac3e993 | 2019-07-17 14:41:44 +0300 | [diff] [blame] | 1343 | compressed_extents = compress_file_range(async_chunk); |
| 1344 | if (compressed_extents == 0) { |
Nikolay Borisov | b532627 | 2019-03-12 17:20:25 +0200 | [diff] [blame] | 1345 | btrfs_add_delayed_iput(async_chunk->inode); |
| 1346 | async_chunk->inode = NULL; |
Josef Bacik | 8180ef8 | 2012-06-08 15:16:12 -0400 | [diff] [blame] | 1347 | } |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1348 | } |
| 1349 | |
| 1350 | /* |
| 1351 | * work queue call back to submit previously compressed pages |
| 1352 | */ |
| 1353 | static noinline void async_cow_submit(struct btrfs_work *work) |
| 1354 | { |
Nikolay Borisov | c5a68ae | 2019-03-12 17:20:26 +0200 | [diff] [blame] | 1355 | struct async_chunk *async_chunk = container_of(work, struct async_chunk, |
| 1356 | work); |
| 1357 | struct btrfs_fs_info *fs_info = btrfs_work_owner(work); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1358 | unsigned long nr_pages; |
| 1359 | |
Nikolay Borisov | b532627 | 2019-03-12 17:20:25 +0200 | [diff] [blame] | 1360 | nr_pages = (async_chunk->end - async_chunk->start + PAGE_SIZE) >> |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1361 | PAGE_SHIFT; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1362 | |
Nikolay Borisov | 4546d17 | 2019-01-03 10:50:03 +0200 | [diff] [blame] | 1363 | /* |
Nikolay Borisov | b532627 | 2019-03-12 17:20:25 +0200 | [diff] [blame] | 1364 | * ->inode could be NULL if async_chunk_start has failed to compress, |
Nikolay Borisov | 4546d17 | 2019-01-03 10:50:03 +0200 | [diff] [blame] | 1365 | * in which case we don't have anything to submit, yet we need to |
| 1366 | * always adjust ->async_delalloc_pages as its paired with the init |
| 1367 | * happening in cow_file_range_async |
| 1368 | */ |
Nikolay Borisov | b532627 | 2019-03-12 17:20:25 +0200 | [diff] [blame] | 1369 | if (async_chunk->inode) |
| 1370 | submit_compressed_extents(async_chunk); |
Josef Bacik | ac98141 | 2021-07-14 14:47:17 -0400 | [diff] [blame] | 1371 | |
| 1372 | /* atomic_sub_return implies a barrier */ |
| 1373 | if (atomic_sub_return(nr_pages, &fs_info->async_delalloc_pages) < |
| 1374 | 5 * SZ_1M) |
| 1375 | cond_wake_up_nomb(&fs_info->async_submit_wait); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1376 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1377 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1378 | static noinline void async_cow_free(struct btrfs_work *work) |
| 1379 | { |
Nikolay Borisov | b532627 | 2019-03-12 17:20:25 +0200 | [diff] [blame] | 1380 | struct async_chunk *async_chunk; |
Qu Wenruo | 9e895a8 | 2021-09-27 15:21:45 +0800 | [diff] [blame] | 1381 | struct async_cow *async_cow; |
Nikolay Borisov | 97db120 | 2019-03-12 17:20:24 +0200 | [diff] [blame] | 1382 | |
Nikolay Borisov | b532627 | 2019-03-12 17:20:25 +0200 | [diff] [blame] | 1383 | async_chunk = container_of(work, struct async_chunk, work); |
| 1384 | if (async_chunk->inode) |
| 1385 | btrfs_add_delayed_iput(async_chunk->inode); |
Chris Mason | ec39f76 | 2019-07-10 12:28:17 -0700 | [diff] [blame] | 1386 | if (async_chunk->blkcg_css) |
| 1387 | css_put(async_chunk->blkcg_css); |
Qu Wenruo | 9e895a8 | 2021-09-27 15:21:45 +0800 | [diff] [blame] | 1388 | |
| 1389 | async_cow = async_chunk->async_cow; |
| 1390 | if (atomic_dec_and_test(&async_cow->num_chunks)) |
| 1391 | kvfree(async_cow); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1392 | } |
| 1393 | |
Nikolay Borisov | 751b643 | 2020-06-03 08:55:22 +0300 | [diff] [blame] | 1394 | static int cow_file_range_async(struct btrfs_inode *inode, |
Chris Mason | ec39f76 | 2019-07-10 12:28:17 -0700 | [diff] [blame] | 1395 | struct writeback_control *wbc, |
| 1396 | struct page *locked_page, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1397 | u64 start, u64 end, int *page_started, |
David Sterba | fac07d2 | 2019-10-29 18:28:57 +0100 | [diff] [blame] | 1398 | unsigned long *nr_written) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1399 | { |
Nikolay Borisov | 751b643 | 2020-06-03 08:55:22 +0300 | [diff] [blame] | 1400 | struct btrfs_fs_info *fs_info = inode->root->fs_info; |
Chris Mason | ec39f76 | 2019-07-10 12:28:17 -0700 | [diff] [blame] | 1401 | struct cgroup_subsys_state *blkcg_css = wbc_blkcg_css(wbc); |
Nikolay Borisov | 97db120 | 2019-03-12 17:20:24 +0200 | [diff] [blame] | 1402 | struct async_cow *ctx; |
| 1403 | struct async_chunk *async_chunk; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1404 | unsigned long nr_pages; |
| 1405 | u64 cur_end; |
Nikolay Borisov | 97db120 | 2019-03-12 17:20:24 +0200 | [diff] [blame] | 1406 | u64 num_chunks = DIV_ROUND_UP(end - start, SZ_512K); |
| 1407 | int i; |
| 1408 | bool should_compress; |
Nikolay Borisov | b1c16ac | 2019-04-01 11:29:57 +0300 | [diff] [blame] | 1409 | unsigned nofs_flag; |
David Sterba | fac07d2 | 2019-10-29 18:28:57 +0100 | [diff] [blame] | 1410 | const unsigned int write_flags = wbc_to_write_flags(wbc); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1411 | |
Nikolay Borisov | 751b643 | 2020-06-03 08:55:22 +0300 | [diff] [blame] | 1412 | unlock_extent(&inode->io_tree, start, end); |
Nikolay Borisov | 97db120 | 2019-03-12 17:20:24 +0200 | [diff] [blame] | 1413 | |
Nikolay Borisov | 751b643 | 2020-06-03 08:55:22 +0300 | [diff] [blame] | 1414 | if (inode->flags & BTRFS_INODE_NOCOMPRESS && |
Nikolay Borisov | 97db120 | 2019-03-12 17:20:24 +0200 | [diff] [blame] | 1415 | !btrfs_test_opt(fs_info, FORCE_COMPRESS)) { |
| 1416 | num_chunks = 1; |
| 1417 | should_compress = false; |
| 1418 | } else { |
| 1419 | should_compress = true; |
| 1420 | } |
| 1421 | |
Nikolay Borisov | b1c16ac | 2019-04-01 11:29:57 +0300 | [diff] [blame] | 1422 | nofs_flag = memalloc_nofs_save(); |
| 1423 | ctx = kvmalloc(struct_size(ctx, chunks, num_chunks), GFP_KERNEL); |
| 1424 | memalloc_nofs_restore(nofs_flag); |
| 1425 | |
Nikolay Borisov | 97db120 | 2019-03-12 17:20:24 +0200 | [diff] [blame] | 1426 | if (!ctx) { |
| 1427 | unsigned clear_bits = EXTENT_LOCKED | EXTENT_DELALLOC | |
| 1428 | EXTENT_DELALLOC_NEW | EXTENT_DEFRAG | |
| 1429 | EXTENT_DO_ACCOUNTING; |
Qu Wenruo | 6869b0a | 2021-01-26 16:33:45 +0800 | [diff] [blame] | 1430 | unsigned long page_ops = PAGE_UNLOCK | PAGE_START_WRITEBACK | |
| 1431 | PAGE_END_WRITEBACK | PAGE_SET_ERROR; |
Nikolay Borisov | 97db120 | 2019-03-12 17:20:24 +0200 | [diff] [blame] | 1432 | |
Nikolay Borisov | 751b643 | 2020-06-03 08:55:22 +0300 | [diff] [blame] | 1433 | extent_clear_unlock_delalloc(inode, start, end, locked_page, |
| 1434 | clear_bits, page_ops); |
Nikolay Borisov | 97db120 | 2019-03-12 17:20:24 +0200 | [diff] [blame] | 1435 | return -ENOMEM; |
| 1436 | } |
| 1437 | |
| 1438 | async_chunk = ctx->chunks; |
| 1439 | atomic_set(&ctx->num_chunks, num_chunks); |
| 1440 | |
| 1441 | for (i = 0; i < num_chunks; i++) { |
| 1442 | if (should_compress) |
| 1443 | cur_end = min(end, start + SZ_512K - 1); |
| 1444 | else |
| 1445 | cur_end = end; |
| 1446 | |
Nikolay Borisov | bd4691a | 2019-01-03 10:50:01 +0200 | [diff] [blame] | 1447 | /* |
| 1448 | * igrab is called higher up in the call chain, take only the |
| 1449 | * lightweight reference for the callback lifetime |
| 1450 | */ |
Nikolay Borisov | 751b643 | 2020-06-03 08:55:22 +0300 | [diff] [blame] | 1451 | ihold(&inode->vfs_inode); |
Qu Wenruo | 9e895a8 | 2021-09-27 15:21:45 +0800 | [diff] [blame] | 1452 | async_chunk[i].async_cow = ctx; |
Nikolay Borisov | 751b643 | 2020-06-03 08:55:22 +0300 | [diff] [blame] | 1453 | async_chunk[i].inode = &inode->vfs_inode; |
Nikolay Borisov | 97db120 | 2019-03-12 17:20:24 +0200 | [diff] [blame] | 1454 | async_chunk[i].start = start; |
| 1455 | async_chunk[i].end = cur_end; |
Nikolay Borisov | 97db120 | 2019-03-12 17:20:24 +0200 | [diff] [blame] | 1456 | async_chunk[i].write_flags = write_flags; |
| 1457 | INIT_LIST_HEAD(&async_chunk[i].extents); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1458 | |
Chris Mason | 1d53c9e | 2019-07-10 12:28:16 -0700 | [diff] [blame] | 1459 | /* |
| 1460 | * The locked_page comes all the way from writepage and its |
| 1461 | * the original page we were actually given. As we spread |
| 1462 | * this large delalloc region across multiple async_chunk |
| 1463 | * structs, only the first struct needs a pointer to locked_page |
| 1464 | * |
| 1465 | * This way we don't need racey decisions about who is supposed |
| 1466 | * to unlock it. |
| 1467 | */ |
| 1468 | if (locked_page) { |
Chris Mason | ec39f76 | 2019-07-10 12:28:17 -0700 | [diff] [blame] | 1469 | /* |
| 1470 | * Depending on the compressibility, the pages might or |
| 1471 | * might not go through async. We want all of them to |
| 1472 | * be accounted against wbc once. Let's do it here |
| 1473 | * before the paths diverge. wbc accounting is used |
| 1474 | * only for foreign writeback detection and doesn't |
| 1475 | * need full accuracy. Just account the whole thing |
| 1476 | * against the first page. |
| 1477 | */ |
| 1478 | wbc_account_cgroup_owner(wbc, locked_page, |
| 1479 | cur_end - start); |
Chris Mason | 1d53c9e | 2019-07-10 12:28:16 -0700 | [diff] [blame] | 1480 | async_chunk[i].locked_page = locked_page; |
| 1481 | locked_page = NULL; |
| 1482 | } else { |
| 1483 | async_chunk[i].locked_page = NULL; |
| 1484 | } |
| 1485 | |
Chris Mason | ec39f76 | 2019-07-10 12:28:17 -0700 | [diff] [blame] | 1486 | if (blkcg_css != blkcg_root_css) { |
| 1487 | css_get(blkcg_css); |
| 1488 | async_chunk[i].blkcg_css = blkcg_css; |
| 1489 | } else { |
| 1490 | async_chunk[i].blkcg_css = NULL; |
| 1491 | } |
| 1492 | |
Omar Sandoval | a0cac0e | 2019-09-16 11:30:57 -0700 | [diff] [blame] | 1493 | btrfs_init_work(&async_chunk[i].work, async_cow_start, |
| 1494 | async_cow_submit, async_cow_free); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1495 | |
Nikolay Borisov | 97db120 | 2019-03-12 17:20:24 +0200 | [diff] [blame] | 1496 | nr_pages = DIV_ROUND_UP(cur_end - start, PAGE_SIZE); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1497 | atomic_add(nr_pages, &fs_info->async_delalloc_pages); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1498 | |
Nikolay Borisov | 97db120 | 2019-03-12 17:20:24 +0200 | [diff] [blame] | 1499 | btrfs_queue_work(fs_info->delalloc_workers, &async_chunk[i].work); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1500 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1501 | *nr_written += nr_pages; |
| 1502 | start = cur_end + 1; |
| 1503 | } |
| 1504 | *page_started = 1; |
| 1505 | return 0; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1506 | } |
| 1507 | |
Naohiro Aota | 42c0110 | 2021-02-04 19:22:07 +0900 | [diff] [blame] | 1508 | static noinline int run_delalloc_zoned(struct btrfs_inode *inode, |
| 1509 | struct page *locked_page, u64 start, |
| 1510 | u64 end, int *page_started, |
| 1511 | unsigned long *nr_written) |
| 1512 | { |
| 1513 | int ret; |
| 1514 | |
| 1515 | ret = cow_file_range(inode, locked_page, start, end, page_started, |
| 1516 | nr_written, 0); |
| 1517 | if (ret) |
| 1518 | return ret; |
| 1519 | |
| 1520 | if (*page_started) |
| 1521 | return 0; |
| 1522 | |
| 1523 | __set_page_dirty_nobuffers(locked_page); |
| 1524 | account_page_redirty(locked_page); |
Qu Wenruo | 2bd0fc9 | 2021-09-27 15:21:58 +0800 | [diff] [blame] | 1525 | extent_write_locked_range(&inode->vfs_inode, start, end); |
Naohiro Aota | 42c0110 | 2021-02-04 19:22:07 +0900 | [diff] [blame] | 1526 | *page_started = 1; |
| 1527 | |
| 1528 | return 0; |
| 1529 | } |
| 1530 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 1531 | 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] | 1532 | u64 bytenr, u64 num_bytes) |
| 1533 | { |
Josef Bacik | fc28b25 | 2021-11-05 16:45:48 -0400 | [diff] [blame] | 1534 | struct btrfs_root *csum_root = btrfs_csum_root(fs_info, bytenr); |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1535 | struct btrfs_ordered_sum *sums; |
Josef Bacik | fc28b25 | 2021-11-05 16:45:48 -0400 | [diff] [blame] | 1536 | int ret; |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1537 | LIST_HEAD(list); |
| 1538 | |
Josef Bacik | fc28b25 | 2021-11-05 16:45:48 -0400 | [diff] [blame] | 1539 | ret = btrfs_lookup_csums_range(csum_root, bytenr, |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 1540 | bytenr + num_bytes - 1, &list, 0); |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1541 | if (ret == 0 && list_empty(&list)) |
| 1542 | return 0; |
| 1543 | |
| 1544 | while (!list_empty(&list)) { |
| 1545 | sums = list_entry(list.next, struct btrfs_ordered_sum, list); |
| 1546 | list_del(&sums->list); |
| 1547 | kfree(sums); |
| 1548 | } |
Liu Bo | 5811375 | 2018-01-31 17:09:13 -0700 | [diff] [blame] | 1549 | if (ret < 0) |
| 1550 | return ret; |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1551 | return 1; |
| 1552 | } |
| 1553 | |
Nikolay Borisov | 8ba96f3 | 2020-06-03 08:55:20 +0300 | [diff] [blame] | 1554 | 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] | 1555 | const u64 start, const u64 end, |
| 1556 | int *page_started, unsigned long *nr_written) |
| 1557 | { |
Nikolay Borisov | 8ba96f3 | 2020-06-03 08:55:20 +0300 | [diff] [blame] | 1558 | const bool is_space_ino = btrfs_is_free_space_inode(inode); |
Johannes Thumshirn | 37f00a6 | 2021-09-09 01:19:25 +0900 | [diff] [blame] | 1559 | const bool is_reloc_ino = btrfs_is_data_reloc_root(inode->root); |
Filipe Manana | 2166e5e | 2020-05-27 11:16:19 +0100 | [diff] [blame] | 1560 | const u64 range_bytes = end + 1 - start; |
Nikolay Borisov | 8ba96f3 | 2020-06-03 08:55:20 +0300 | [diff] [blame] | 1561 | struct extent_io_tree *io_tree = &inode->io_tree; |
Filipe Manana | 467dc47 | 2020-05-27 11:16:07 +0100 | [diff] [blame] | 1562 | u64 range_start = start; |
| 1563 | u64 count; |
| 1564 | |
| 1565 | /* |
| 1566 | * If EXTENT_NORESERVE is set it means that when the buffered write was |
| 1567 | * made we had not enough available data space and therefore we did not |
| 1568 | * reserve data space for it, since we though we could do NOCOW for the |
| 1569 | * respective file range (either there is prealloc extent or the inode |
| 1570 | * has the NOCOW bit set). |
| 1571 | * |
| 1572 | * However when we need to fallback to COW mode (because for example the |
| 1573 | * block group for the corresponding extent was turned to RO mode by a |
| 1574 | * scrub or relocation) we need to do the following: |
| 1575 | * |
| 1576 | * 1) We increment the bytes_may_use counter of the data space info. |
| 1577 | * If COW succeeds, it allocates a new data extent and after doing |
| 1578 | * that it decrements the space info's bytes_may_use counter and |
| 1579 | * increments its bytes_reserved counter by the same amount (we do |
| 1580 | * this at btrfs_add_reserved_bytes()). So we need to increment the |
| 1581 | * bytes_may_use counter to compensate (when space is reserved at |
| 1582 | * buffered write time, the bytes_may_use counter is incremented); |
| 1583 | * |
| 1584 | * 2) We clear the EXTENT_NORESERVE bit from the range. We do this so |
| 1585 | * that if the COW path fails for any reason, it decrements (through |
| 1586 | * extent_clear_unlock_delalloc()) the bytes_may_use counter of the |
| 1587 | * data space info, which we incremented in the step above. |
Filipe Manana | 2166e5e | 2020-05-27 11:16:19 +0100 | [diff] [blame] | 1588 | * |
| 1589 | * 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] | 1590 | * space cache inode or an inode of the data relocation tree, we must |
| 1591 | * also increment bytes_may_use of the data space_info for the same |
| 1592 | * reason. Space caches and relocated data extents always get a prealloc |
Filipe Manana | 2166e5e | 2020-05-27 11:16:19 +0100 | [diff] [blame] | 1593 | * extent for them, however scrub or balance may have set the block |
Filipe Manana | 6bd335b | 2020-06-08 13:33:05 +0100 | [diff] [blame] | 1594 | * group that contains that extent to RO mode and therefore force COW |
| 1595 | * when starting writeback. |
Filipe Manana | 467dc47 | 2020-05-27 11:16:07 +0100 | [diff] [blame] | 1596 | */ |
Filipe Manana | 2166e5e | 2020-05-27 11:16:19 +0100 | [diff] [blame] | 1597 | count = count_range_bits(io_tree, &range_start, end, range_bytes, |
Filipe Manana | 467dc47 | 2020-05-27 11:16:07 +0100 | [diff] [blame] | 1598 | EXTENT_NORESERVE, 0); |
Filipe Manana | 6bd335b | 2020-06-08 13:33:05 +0100 | [diff] [blame] | 1599 | if (count > 0 || is_space_ino || is_reloc_ino) { |
| 1600 | u64 bytes = count; |
Nikolay Borisov | 8ba96f3 | 2020-06-03 08:55:20 +0300 | [diff] [blame] | 1601 | struct btrfs_fs_info *fs_info = inode->root->fs_info; |
Filipe Manana | 467dc47 | 2020-05-27 11:16:07 +0100 | [diff] [blame] | 1602 | struct btrfs_space_info *sinfo = fs_info->data_sinfo; |
| 1603 | |
Filipe Manana | 6bd335b | 2020-06-08 13:33:05 +0100 | [diff] [blame] | 1604 | if (is_space_ino || is_reloc_ino) |
| 1605 | bytes = range_bytes; |
| 1606 | |
Filipe Manana | 467dc47 | 2020-05-27 11:16:07 +0100 | [diff] [blame] | 1607 | spin_lock(&sinfo->lock); |
Filipe Manana | 2166e5e | 2020-05-27 11:16:19 +0100 | [diff] [blame] | 1608 | btrfs_space_info_update_bytes_may_use(fs_info, sinfo, bytes); |
Filipe Manana | 467dc47 | 2020-05-27 11:16:07 +0100 | [diff] [blame] | 1609 | spin_unlock(&sinfo->lock); |
| 1610 | |
Filipe Manana | 2166e5e | 2020-05-27 11:16:19 +0100 | [diff] [blame] | 1611 | if (count > 0) |
| 1612 | clear_extent_bit(io_tree, start, end, EXTENT_NORESERVE, |
| 1613 | 0, 0, NULL); |
Filipe Manana | 467dc47 | 2020-05-27 11:16:07 +0100 | [diff] [blame] | 1614 | } |
| 1615 | |
Nikolay Borisov | 8ba96f3 | 2020-06-03 08:55:20 +0300 | [diff] [blame] | 1616 | return cow_file_range(inode, locked_page, start, end, page_started, |
| 1617 | nr_written, 1); |
Filipe Manana | 467dc47 | 2020-05-27 11:16:07 +0100 | [diff] [blame] | 1618 | } |
| 1619 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1620 | /* |
| 1621 | * when nowcow writeback call back. This checks for snapshots or COW copies |
| 1622 | * of the extents that exist in the file, and COWs the file as required. |
| 1623 | * |
| 1624 | * If no cow copies or snapshots exist, we write directly to the existing |
| 1625 | * blocks on disk |
| 1626 | */ |
Nikolay Borisov | 968322c | 2020-06-03 08:55:21 +0300 | [diff] [blame] | 1627 | static noinline int run_delalloc_nocow(struct btrfs_inode *inode, |
Chris Mason | 7f366cf | 2009-03-12 20:12:45 -0400 | [diff] [blame] | 1628 | struct page *locked_page, |
Nikolay Borisov | 3e02484 | 2019-08-21 10:42:03 +0300 | [diff] [blame] | 1629 | const u64 start, const u64 end, |
Goldwyn Rodrigues | 6e65ae7 | 2021-03-04 09:06:25 -0600 | [diff] [blame] | 1630 | int *page_started, |
Nikolay Borisov | 3e02484 | 2019-08-21 10:42:03 +0300 | [diff] [blame] | 1631 | unsigned long *nr_written) |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1632 | { |
Nikolay Borisov | 968322c | 2020-06-03 08:55:21 +0300 | [diff] [blame] | 1633 | struct btrfs_fs_info *fs_info = inode->root->fs_info; |
| 1634 | struct btrfs_root *root = inode->root; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1635 | struct btrfs_path *path; |
Nikolay Borisov | 3e02484 | 2019-08-21 10:42:03 +0300 | [diff] [blame] | 1636 | u64 cow_start = (u64)-1; |
| 1637 | u64 cur_offset = start; |
Robbie Ko | 8ecebf4d | 2018-08-06 10:30:30 +0800 | [diff] [blame] | 1638 | int ret; |
Nikolay Borisov | 3e02484 | 2019-08-21 10:42:03 +0300 | [diff] [blame] | 1639 | bool check_prev = true; |
Nikolay Borisov | 968322c | 2020-06-03 08:55:21 +0300 | [diff] [blame] | 1640 | const bool freespace_inode = btrfs_is_free_space_inode(inode); |
| 1641 | u64 ino = btrfs_ino(inode); |
Nikolay Borisov | 762bf09 | 2019-08-22 17:24:20 +0300 | [diff] [blame] | 1642 | bool nocow = false; |
| 1643 | u64 disk_bytenr = 0; |
Goldwyn Rodrigues | 6e65ae7 | 2021-03-04 09:06:25 -0600 | [diff] [blame] | 1644 | const bool force = inode->flags & BTRFS_INODE_NODATACOW; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1645 | |
| 1646 | path = btrfs_alloc_path(); |
Josef Bacik | 17ca04a | 2012-05-31 15:58:55 -0400 | [diff] [blame] | 1647 | if (!path) { |
Nikolay Borisov | 968322c | 2020-06-03 08:55:21 +0300 | [diff] [blame] | 1648 | extent_clear_unlock_delalloc(inode, start, end, locked_page, |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1649 | EXTENT_LOCKED | EXTENT_DELALLOC | |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 1650 | EXTENT_DO_ACCOUNTING | |
| 1651 | EXTENT_DEFRAG, PAGE_UNLOCK | |
Qu Wenruo | 6869b0a | 2021-01-26 16:33:45 +0800 | [diff] [blame] | 1652 | PAGE_START_WRITEBACK | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1653 | PAGE_END_WRITEBACK); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 1654 | return -ENOMEM; |
Josef Bacik | 17ca04a | 2012-05-31 15:58:55 -0400 | [diff] [blame] | 1655 | } |
Li Zefan | 82d5902 | 2011-04-20 10:33:24 +0800 | [diff] [blame] | 1656 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1657 | while (1) { |
Nikolay Borisov | 3e02484 | 2019-08-21 10:42:03 +0300 | [diff] [blame] | 1658 | struct btrfs_key found_key; |
| 1659 | struct btrfs_file_extent_item *fi; |
| 1660 | struct extent_buffer *leaf; |
| 1661 | u64 extent_end; |
| 1662 | u64 extent_offset; |
Nikolay Borisov | 3e02484 | 2019-08-21 10:42:03 +0300 | [diff] [blame] | 1663 | u64 num_bytes = 0; |
| 1664 | u64 disk_num_bytes; |
Nikolay Borisov | 3e02484 | 2019-08-21 10:42:03 +0300 | [diff] [blame] | 1665 | u64 ram_bytes; |
| 1666 | int extent_type; |
Nikolay Borisov | 762bf09 | 2019-08-22 17:24:20 +0300 | [diff] [blame] | 1667 | |
| 1668 | nocow = false; |
Nikolay Borisov | 3e02484 | 2019-08-21 10:42:03 +0300 | [diff] [blame] | 1669 | |
Liu Bo | e4c3b2d | 2017-01-30 12:25:28 -0800 | [diff] [blame] | 1670 | ret = btrfs_lookup_file_extent(NULL, root, path, ino, |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1671 | cur_offset, 0); |
Josef Bacik | d788a34 | 2013-10-25 16:55:08 -0400 | [diff] [blame] | 1672 | if (ret < 0) |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1673 | goto error; |
Nikolay Borisov | a6bd9cd | 2019-08-21 10:42:57 +0300 | [diff] [blame] | 1674 | |
| 1675 | /* |
| 1676 | * If there is no extent for our range when doing the initial |
| 1677 | * search, then go back to the previous slot as it will be the |
| 1678 | * one containing the search offset |
| 1679 | */ |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1680 | if (ret > 0 && path->slots[0] > 0 && check_prev) { |
| 1681 | leaf = path->nodes[0]; |
| 1682 | btrfs_item_key_to_cpu(leaf, &found_key, |
| 1683 | path->slots[0] - 1); |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 1684 | if (found_key.objectid == ino && |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1685 | found_key.type == BTRFS_EXTENT_DATA_KEY) |
| 1686 | path->slots[0]--; |
| 1687 | } |
Nikolay Borisov | 3e02484 | 2019-08-21 10:42:03 +0300 | [diff] [blame] | 1688 | check_prev = false; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1689 | next_slot: |
Nikolay Borisov | a6bd9cd | 2019-08-21 10:42:57 +0300 | [diff] [blame] | 1690 | /* Go to next leaf if we have exhausted the current one */ |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1691 | leaf = path->nodes[0]; |
| 1692 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { |
| 1693 | ret = btrfs_next_leaf(root, path); |
Liu Bo | e891669 | 2018-01-25 11:02:50 -0700 | [diff] [blame] | 1694 | if (ret < 0) { |
| 1695 | if (cow_start != (u64)-1) |
| 1696 | cur_offset = cow_start; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1697 | goto error; |
Liu Bo | e891669 | 2018-01-25 11:02:50 -0700 | [diff] [blame] | 1698 | } |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1699 | if (ret > 0) |
| 1700 | break; |
| 1701 | leaf = path->nodes[0]; |
| 1702 | } |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1703 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1704 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1705 | |
Nikolay Borisov | a6bd9cd | 2019-08-21 10:42:57 +0300 | [diff] [blame] | 1706 | /* Didn't find anything for our INO */ |
Filipe Manana | 1d512cb | 2015-11-09 00:33:58 +0000 | [diff] [blame] | 1707 | if (found_key.objectid > ino) |
| 1708 | break; |
Nikolay Borisov | a6bd9cd | 2019-08-21 10:42:57 +0300 | [diff] [blame] | 1709 | /* |
| 1710 | * Keep searching until we find an EXTENT_ITEM or there are no |
| 1711 | * more extents for this inode |
| 1712 | */ |
Filipe Manana | 1d512cb | 2015-11-09 00:33:58 +0000 | [diff] [blame] | 1713 | if (WARN_ON_ONCE(found_key.objectid < ino) || |
| 1714 | found_key.type < BTRFS_EXTENT_DATA_KEY) { |
| 1715 | path->slots[0]++; |
| 1716 | goto next_slot; |
| 1717 | } |
Nikolay Borisov | a6bd9cd | 2019-08-21 10:42:57 +0300 | [diff] [blame] | 1718 | |
| 1719 | /* Found key is not EXTENT_DATA_KEY or starts after req range */ |
Filipe Manana | 1d512cb | 2015-11-09 00:33:58 +0000 | [diff] [blame] | 1720 | if (found_key.type > BTRFS_EXTENT_DATA_KEY || |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1721 | found_key.offset > end) |
| 1722 | break; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1723 | |
Nikolay Borisov | a6bd9cd | 2019-08-21 10:42:57 +0300 | [diff] [blame] | 1724 | /* |
| 1725 | * If the found extent starts after requested offset, then |
| 1726 | * adjust extent_end to be right before this extent begins |
| 1727 | */ |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1728 | if (found_key.offset > cur_offset) { |
| 1729 | extent_end = found_key.offset; |
Chris Mason | e9061e2 | 2009-10-09 09:57:45 -0400 | [diff] [blame] | 1730 | extent_type = 0; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1731 | goto out_check; |
| 1732 | } |
Chris Mason | c31f883 | 2008-01-08 15:46:31 -0500 | [diff] [blame] | 1733 | |
Nikolay Borisov | a6bd9cd | 2019-08-21 10:42:57 +0300 | [diff] [blame] | 1734 | /* |
| 1735 | * Found extent which begins before our range and potentially |
| 1736 | * intersect it |
| 1737 | */ |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1738 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 1739 | struct btrfs_file_extent_item); |
| 1740 | extent_type = btrfs_file_extent_type(leaf, fi); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1741 | |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 1742 | ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1743 | if (extent_type == BTRFS_FILE_EXTENT_REG || |
| 1744 | extent_type == BTRFS_FILE_EXTENT_PREALLOC) { |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1745 | disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1746 | extent_offset = btrfs_file_extent_offset(leaf, fi); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1747 | extent_end = found_key.offset + |
| 1748 | btrfs_file_extent_num_bytes(leaf, fi); |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 1749 | disk_num_bytes = |
| 1750 | btrfs_file_extent_disk_num_bytes(leaf, fi); |
Nikolay Borisov | a6bd9cd | 2019-08-21 10:42:57 +0300 | [diff] [blame] | 1751 | /* |
Filipe Manana | de7999a | 2019-12-11 09:01:40 +0000 | [diff] [blame] | 1752 | * If the extent we got ends before our current offset, |
| 1753 | * skip to the next extent. |
Nikolay Borisov | a6bd9cd | 2019-08-21 10:42:57 +0300 | [diff] [blame] | 1754 | */ |
Filipe Manana | de7999a | 2019-12-11 09:01:40 +0000 | [diff] [blame] | 1755 | if (extent_end <= cur_offset) { |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1756 | path->slots[0]++; |
| 1757 | goto next_slot; |
| 1758 | } |
Nikolay Borisov | a6bd9cd | 2019-08-21 10:42:57 +0300 | [diff] [blame] | 1759 | /* Skip holes */ |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1760 | if (disk_bytenr == 0) |
| 1761 | goto out_check; |
Nikolay Borisov | a6bd9cd | 2019-08-21 10:42:57 +0300 | [diff] [blame] | 1762 | /* Skip compressed/encrypted/encoded extents */ |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1763 | if (btrfs_file_extent_compression(leaf, fi) || |
| 1764 | btrfs_file_extent_encryption(leaf, fi) || |
| 1765 | btrfs_file_extent_other_encoding(leaf, fi)) |
| 1766 | goto out_check; |
Ethan Lien | 78d4295 | 2018-05-17 14:58:29 +0800 | [diff] [blame] | 1767 | /* |
Nikolay Borisov | a6bd9cd | 2019-08-21 10:42:57 +0300 | [diff] [blame] | 1768 | * If extent is created before the last volume's snapshot |
| 1769 | * this implies the extent is shared, hence we can't do |
| 1770 | * nocow. This is the same check as in |
| 1771 | * btrfs_cross_ref_exist but without calling |
| 1772 | * btrfs_search_slot. |
Ethan Lien | 78d4295 | 2018-05-17 14:58:29 +0800 | [diff] [blame] | 1773 | */ |
Nikolay Borisov | 3e02484 | 2019-08-21 10:42:03 +0300 | [diff] [blame] | 1774 | if (!freespace_inode && |
Lu Fengqi | 27a7ff5 | 2018-11-29 17:31:32 +0800 | [diff] [blame] | 1775 | btrfs_file_extent_generation(leaf, fi) <= |
Ethan Lien | 78d4295 | 2018-05-17 14:58:29 +0800 | [diff] [blame] | 1776 | btrfs_root_last_snapshot(&root->root_item)) |
| 1777 | goto out_check; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1778 | if (extent_type == BTRFS_FILE_EXTENT_REG && !force) |
| 1779 | goto out_check; |
Filipe Manana | c65ca98 | 2020-11-18 11:00:17 +0000 | [diff] [blame] | 1780 | |
| 1781 | /* |
| 1782 | * The following checks can be expensive, as they need to |
| 1783 | * take other locks and do btree or rbtree searches, so |
| 1784 | * release the path to avoid blocking other tasks for too |
| 1785 | * long. |
| 1786 | */ |
| 1787 | btrfs_release_path(path); |
| 1788 | |
Liu Bo | 5811375 | 2018-01-31 17:09:13 -0700 | [diff] [blame] | 1789 | ret = btrfs_cross_ref_exist(root, ino, |
| 1790 | found_key.offset - |
Boris Burkov | a84d5d4 | 2020-08-18 11:00:05 -0700 | [diff] [blame] | 1791 | extent_offset, disk_bytenr, false); |
Liu Bo | 5811375 | 2018-01-31 17:09:13 -0700 | [diff] [blame] | 1792 | if (ret) { |
| 1793 | /* |
| 1794 | * ret could be -EIO if the above fails to read |
| 1795 | * metadata. |
| 1796 | */ |
| 1797 | if (ret < 0) { |
| 1798 | if (cow_start != (u64)-1) |
| 1799 | cur_offset = cow_start; |
| 1800 | goto error; |
| 1801 | } |
| 1802 | |
Nikolay Borisov | 3e02484 | 2019-08-21 10:42:03 +0300 | [diff] [blame] | 1803 | WARN_ON_ONCE(freespace_inode); |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1804 | goto out_check; |
Liu Bo | 5811375 | 2018-01-31 17:09:13 -0700 | [diff] [blame] | 1805 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1806 | disk_bytenr += extent_offset; |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1807 | disk_bytenr += cur_offset - found_key.offset; |
| 1808 | num_bytes = min(end + 1, extent_end) - cur_offset; |
| 1809 | /* |
Nikolay Borisov | a6bd9cd | 2019-08-21 10:42:57 +0300 | [diff] [blame] | 1810 | * If there are pending snapshots for this root, we |
| 1811 | * fall into common COW way |
Wang Shilong | e9894fd | 2014-03-27 11:12:25 +0800 | [diff] [blame] | 1812 | */ |
Nikolay Borisov | 3e02484 | 2019-08-21 10:42:03 +0300 | [diff] [blame] | 1813 | if (!freespace_inode && atomic_read(&root->snapshot_force_cow)) |
Robbie Ko | 8ecebf4d | 2018-08-06 10:30:30 +0800 | [diff] [blame] | 1814 | goto out_check; |
Wang Shilong | e9894fd | 2014-03-27 11:12:25 +0800 | [diff] [blame] | 1815 | /* |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1816 | * force cow if csum exists in the range. |
| 1817 | * this ensure that csum for a given extent are |
| 1818 | * either valid or do not exist. |
| 1819 | */ |
Liu Bo | 5811375 | 2018-01-31 17:09:13 -0700 | [diff] [blame] | 1820 | ret = csum_exist_in_range(fs_info, disk_bytenr, |
| 1821 | num_bytes); |
| 1822 | if (ret) { |
Liu Bo | 5811375 | 2018-01-31 17:09:13 -0700 | [diff] [blame] | 1823 | /* |
| 1824 | * ret could be -EIO if the above fails to read |
| 1825 | * metadata. |
| 1826 | */ |
| 1827 | if (ret < 0) { |
| 1828 | if (cow_start != (u64)-1) |
| 1829 | cur_offset = cow_start; |
| 1830 | goto error; |
| 1831 | } |
Nikolay Borisov | 3e02484 | 2019-08-21 10:42:03 +0300 | [diff] [blame] | 1832 | WARN_ON_ONCE(freespace_inode); |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1833 | goto out_check; |
Robbie Ko | 91e1f56 | 2016-10-07 10:01:29 +0800 | [diff] [blame] | 1834 | } |
Filipe Manana | 2090303 | 2021-02-05 12:55:36 +0000 | [diff] [blame] | 1835 | /* If the extent's block group is RO, we must COW */ |
Robbie Ko | 8ecebf4d | 2018-08-06 10:30:30 +0800 | [diff] [blame] | 1836 | if (!btrfs_inc_nocow_writers(fs_info, disk_bytenr)) |
Filipe Manana | f78c436 | 2016-05-09 13:15:41 +0100 | [diff] [blame] | 1837 | goto out_check; |
Nikolay Borisov | 3e02484 | 2019-08-21 10:42:03 +0300 | [diff] [blame] | 1838 | nocow = true; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1839 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
Nikolay Borisov | e8e2100 | 2019-08-22 17:25:23 +0300 | [diff] [blame] | 1840 | extent_end = found_key.offset + ram_bytes; |
| 1841 | extent_end = ALIGN(extent_end, fs_info->sectorsize); |
Nikolay Borisov | 922f051 | 2019-08-05 17:47:06 +0300 | [diff] [blame] | 1842 | /* Skip extents outside of our requested range */ |
| 1843 | if (extent_end <= start) { |
| 1844 | path->slots[0]++; |
| 1845 | goto next_slot; |
| 1846 | } |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1847 | } else { |
Nikolay Borisov | e8e2100 | 2019-08-22 17:25:23 +0300 | [diff] [blame] | 1848 | /* If this triggers then we have a memory corruption */ |
Arnd Bergmann | 290342f | 2019-03-25 14:02:25 +0100 | [diff] [blame] | 1849 | BUG(); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1850 | } |
| 1851 | out_check: |
Nikolay Borisov | a6bd9cd | 2019-08-21 10:42:57 +0300 | [diff] [blame] | 1852 | /* |
| 1853 | * If nocow is false then record the beginning of the range |
| 1854 | * that needs to be COWed |
| 1855 | */ |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1856 | if (!nocow) { |
| 1857 | if (cow_start == (u64)-1) |
| 1858 | cow_start = cur_offset; |
| 1859 | cur_offset = extent_end; |
| 1860 | if (cur_offset > end) |
| 1861 | break; |
Filipe Manana | c65ca98 | 2020-11-18 11:00:17 +0000 | [diff] [blame] | 1862 | if (!path->nodes[0]) |
| 1863 | continue; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1864 | path->slots[0]++; |
| 1865 | goto next_slot; |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1866 | } |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1867 | |
Nikolay Borisov | a6bd9cd | 2019-08-21 10:42:57 +0300 | [diff] [blame] | 1868 | /* |
| 1869 | * COW range from cow_start to found_key.offset - 1. As the key |
| 1870 | * will contain the beginning of the first extent that can be |
| 1871 | * NOCOW, following one which needs to be COW'ed |
| 1872 | */ |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1873 | if (cow_start != (u64)-1) { |
Nikolay Borisov | 968322c | 2020-06-03 08:55:21 +0300 | [diff] [blame] | 1874 | ret = fallback_to_cow(inode, locked_page, |
Nikolay Borisov | 8ba96f3 | 2020-06-03 08:55:20 +0300 | [diff] [blame] | 1875 | cow_start, found_key.offset - 1, |
Filipe Manana | 467dc47 | 2020-05-27 11:16:07 +0100 | [diff] [blame] | 1876 | page_started, nr_written); |
Josef Bacik | 230ed39 | 2020-07-06 09:14:12 -0400 | [diff] [blame] | 1877 | if (ret) |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1878 | goto error; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1879 | cow_start = (u64)-1; |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1880 | } |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1881 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1882 | if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) { |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 1883 | u64 orig_start = found_key.offset - extent_offset; |
Nikolay Borisov | 3e02484 | 2019-08-21 10:42:03 +0300 | [diff] [blame] | 1884 | struct extent_map *em; |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 1885 | |
Nikolay Borisov | 968322c | 2020-06-03 08:55:21 +0300 | [diff] [blame] | 1886 | em = create_io_em(inode, cur_offset, num_bytes, |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 1887 | orig_start, |
| 1888 | disk_bytenr, /* block_start */ |
| 1889 | num_bytes, /* block_len */ |
| 1890 | disk_num_bytes, /* orig_block_len */ |
| 1891 | ram_bytes, BTRFS_COMPRESS_NONE, |
| 1892 | BTRFS_ORDERED_PREALLOC); |
| 1893 | if (IS_ERR(em)) { |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 1894 | ret = PTR_ERR(em); |
| 1895 | goto error; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1896 | } |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 1897 | free_extent_map(em); |
Nikolay Borisov | 968322c | 2020-06-03 08:55:21 +0300 | [diff] [blame] | 1898 | ret = btrfs_add_ordered_extent(inode, cur_offset, |
Nikolay Borisov | bb55f62 | 2019-08-05 17:47:05 +0300 | [diff] [blame] | 1899 | disk_bytenr, num_bytes, |
| 1900 | num_bytes, |
| 1901 | BTRFS_ORDERED_PREALLOC); |
Nikolay Borisov | 762bf09 | 2019-08-22 17:24:20 +0300 | [diff] [blame] | 1902 | if (ret) { |
Nikolay Borisov | 968322c | 2020-06-03 08:55:21 +0300 | [diff] [blame] | 1903 | btrfs_drop_extent_cache(inode, cur_offset, |
Nikolay Borisov | 762bf09 | 2019-08-22 17:24:20 +0300 | [diff] [blame] | 1904 | cur_offset + num_bytes - 1, |
| 1905 | 0); |
| 1906 | goto error; |
| 1907 | } |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1908 | } else { |
Nikolay Borisov | 968322c | 2020-06-03 08:55:21 +0300 | [diff] [blame] | 1909 | ret = btrfs_add_ordered_extent(inode, cur_offset, |
Nikolay Borisov | bb55f62 | 2019-08-05 17:47:05 +0300 | [diff] [blame] | 1910 | disk_bytenr, num_bytes, |
| 1911 | num_bytes, |
| 1912 | BTRFS_ORDERED_NOCOW); |
Nikolay Borisov | 762bf09 | 2019-08-22 17:24:20 +0300 | [diff] [blame] | 1913 | if (ret) |
| 1914 | goto error; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1915 | } |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1916 | |
Filipe Manana | f78c436 | 2016-05-09 13:15:41 +0100 | [diff] [blame] | 1917 | if (nocow) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1918 | btrfs_dec_nocow_writers(fs_info, disk_bytenr); |
Nikolay Borisov | 762bf09 | 2019-08-22 17:24:20 +0300 | [diff] [blame] | 1919 | nocow = false; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1920 | |
Johannes Thumshirn | 37f00a6 | 2021-09-09 01:19:25 +0900 | [diff] [blame] | 1921 | if (btrfs_is_data_reloc_root(root)) |
Qu Wenruo | 4dbd80f | 2017-03-08 10:25:51 +0800 | [diff] [blame] | 1922 | /* |
| 1923 | * Error handled later, as we must prevent |
| 1924 | * extent_clear_unlock_delalloc() in error handler |
| 1925 | * from freeing metadata of created ordered extent. |
| 1926 | */ |
Nikolay Borisov | 968322c | 2020-06-03 08:55:21 +0300 | [diff] [blame] | 1927 | ret = btrfs_reloc_clone_csums(inode, cur_offset, |
Yan, Zheng | efa5646 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 1928 | num_bytes); |
Yan, Zheng | efa5646 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 1929 | |
Nikolay Borisov | 968322c | 2020-06-03 08:55:21 +0300 | [diff] [blame] | 1930 | extent_clear_unlock_delalloc(inode, cur_offset, |
Nikolay Borisov | 74e9194 | 2019-07-17 16:18:16 +0300 | [diff] [blame] | 1931 | cur_offset + num_bytes - 1, |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1932 | locked_page, EXTENT_LOCKED | |
Wang Xiaoguang | 1851309 | 2016-07-25 15:51:40 +0800 | [diff] [blame] | 1933 | EXTENT_DELALLOC | |
| 1934 | EXTENT_CLEAR_DATA_RESV, |
Qu Wenruo | f57ad93 | 2021-04-07 19:22:13 +0800 | [diff] [blame] | 1935 | PAGE_UNLOCK | PAGE_SET_ORDERED); |
Wang Xiaoguang | 1851309 | 2016-07-25 15:51:40 +0800 | [diff] [blame] | 1936 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1937 | cur_offset = extent_end; |
Qu Wenruo | 4dbd80f | 2017-03-08 10:25:51 +0800 | [diff] [blame] | 1938 | |
| 1939 | /* |
| 1940 | * btrfs_reloc_clone_csums() error, now we're OK to call error |
| 1941 | * handler, as metadata for created ordered extent will only |
| 1942 | * be freed by btrfs_finish_ordered_io(). |
| 1943 | */ |
| 1944 | if (ret) |
| 1945 | goto error; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1946 | if (cur_offset > end) |
| 1947 | break; |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1948 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1949 | btrfs_release_path(path); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1950 | |
Robbie Ko | 506481b | 2018-10-30 18:04:04 +0800 | [diff] [blame] | 1951 | if (cur_offset <= end && cow_start == (u64)-1) |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1952 | cow_start = cur_offset; |
Josef Bacik | 17ca04a | 2012-05-31 15:58:55 -0400 | [diff] [blame] | 1953 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1954 | if (cow_start != (u64)-1) { |
Robbie Ko | 506481b | 2018-10-30 18:04:04 +0800 | [diff] [blame] | 1955 | cur_offset = end; |
Nikolay Borisov | 968322c | 2020-06-03 08:55:21 +0300 | [diff] [blame] | 1956 | ret = fallback_to_cow(inode, locked_page, cow_start, end, |
| 1957 | page_started, nr_written); |
Josef Bacik | d788a34 | 2013-10-25 16:55:08 -0400 | [diff] [blame] | 1958 | if (ret) |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1959 | goto error; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1960 | } |
| 1961 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1962 | error: |
Nikolay Borisov | 762bf09 | 2019-08-22 17:24:20 +0300 | [diff] [blame] | 1963 | if (nocow) |
| 1964 | btrfs_dec_nocow_writers(fs_info, disk_bytenr); |
| 1965 | |
Josef Bacik | 17ca04a | 2012-05-31 15:58:55 -0400 | [diff] [blame] | 1966 | if (ret && cur_offset < end) |
Nikolay Borisov | 968322c | 2020-06-03 08:55:21 +0300 | [diff] [blame] | 1967 | extent_clear_unlock_delalloc(inode, cur_offset, end, |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1968 | locked_page, EXTENT_LOCKED | |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 1969 | EXTENT_DELALLOC | EXTENT_DEFRAG | |
| 1970 | EXTENT_DO_ACCOUNTING, PAGE_UNLOCK | |
Qu Wenruo | 6869b0a | 2021-01-26 16:33:45 +0800 | [diff] [blame] | 1971 | PAGE_START_WRITEBACK | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1972 | PAGE_END_WRITEBACK); |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1973 | btrfs_free_path(path); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1974 | return ret; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1975 | } |
| 1976 | |
Goldwyn Rodrigues | 6e65ae7 | 2021-03-04 09:06:25 -0600 | [diff] [blame] | 1977 | static bool should_nocow(struct btrfs_inode *inode, u64 start, u64 end) |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 1978 | { |
Goldwyn Rodrigues | 6e65ae7 | 2021-03-04 09:06:25 -0600 | [diff] [blame] | 1979 | if (inode->flags & (BTRFS_INODE_NODATACOW | BTRFS_INODE_PREALLOC)) { |
| 1980 | if (inode->defrag_bytes && |
| 1981 | test_range_bit(&inode->io_tree, start, end, EXTENT_DEFRAG, |
| 1982 | 0, NULL)) |
| 1983 | return false; |
| 1984 | return true; |
| 1985 | } |
| 1986 | return false; |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 1987 | } |
| 1988 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1989 | /* |
Nikolay Borisov | 5eaad97 | 2018-11-01 14:09:46 +0200 | [diff] [blame] | 1990 | * Function to process delayed allocation (create CoW) for ranges which are |
| 1991 | * being touched for the first time. |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1992 | */ |
Nikolay Borisov | 98456b9 | 2020-06-03 08:55:29 +0300 | [diff] [blame] | 1993 | 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] | 1994 | u64 start, u64 end, int *page_started, unsigned long *nr_written, |
| 1995 | struct writeback_control *wbc) |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1996 | { |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1997 | int ret; |
Naohiro Aota | 42c0110 | 2021-02-04 19:22:07 +0900 | [diff] [blame] | 1998 | const bool zoned = btrfs_is_zoned(inode->root->fs_info); |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1999 | |
Qu Wenruo | 2749f7e | 2021-09-27 15:22:07 +0800 | [diff] [blame] | 2000 | /* |
| 2001 | * The range must cover part of the @locked_page, or the returned |
| 2002 | * @page_started can confuse the caller. |
| 2003 | */ |
| 2004 | ASSERT(!(end <= page_offset(locked_page) || |
| 2005 | start >= page_offset(locked_page) + PAGE_SIZE)); |
| 2006 | |
Goldwyn Rodrigues | 6e65ae7 | 2021-03-04 09:06:25 -0600 | [diff] [blame] | 2007 | if (should_nocow(inode, start, end)) { |
Johannes Thumshirn | 2adada8 | 2021-09-09 01:19:29 +0900 | [diff] [blame] | 2008 | /* |
| 2009 | * Normally on a zoned device we're only doing COW writes, but |
| 2010 | * in case of relocation on a zoned filesystem we have taken |
| 2011 | * precaution, that we're only writing sequentially. It's safe |
| 2012 | * to use run_delalloc_nocow() here, like for regular |
| 2013 | * preallocated inodes. |
| 2014 | */ |
| 2015 | ASSERT(!zoned || |
| 2016 | (zoned && btrfs_is_data_reloc_root(inode->root))); |
Nikolay Borisov | 98456b9 | 2020-06-03 08:55:29 +0300 | [diff] [blame] | 2017 | ret = run_delalloc_nocow(inode, locked_page, start, end, |
Goldwyn Rodrigues | 6e65ae7 | 2021-03-04 09:06:25 -0600 | [diff] [blame] | 2018 | page_started, nr_written); |
Nikolay Borisov | 98456b9 | 2020-06-03 08:55:29 +0300 | [diff] [blame] | 2019 | } else if (!inode_can_compress(inode) || |
| 2020 | !inode_need_compress(inode, start, end)) { |
Naohiro Aota | 42c0110 | 2021-02-04 19:22:07 +0900 | [diff] [blame] | 2021 | if (zoned) |
| 2022 | ret = run_delalloc_zoned(inode, locked_page, start, end, |
| 2023 | page_started, nr_written); |
| 2024 | else |
| 2025 | ret = cow_file_range(inode, locked_page, start, end, |
| 2026 | page_started, nr_written, 1); |
Josef Bacik | 7ddf5a4 | 2012-06-08 15:26:47 -0400 | [diff] [blame] | 2027 | } else { |
Nikolay Borisov | 98456b9 | 2020-06-03 08:55:29 +0300 | [diff] [blame] | 2028 | set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, &inode->runtime_flags); |
| 2029 | ret = cow_file_range_async(inode, wbc, locked_page, start, end, |
David Sterba | fac07d2 | 2019-10-29 18:28:57 +0100 | [diff] [blame] | 2030 | page_started, nr_written); |
Josef Bacik | 7ddf5a4 | 2012-06-08 15:26:47 -0400 | [diff] [blame] | 2031 | } |
Qu Wenruo | 7361b4a | 2021-07-28 14:05:05 +0800 | [diff] [blame] | 2032 | ASSERT(ret <= 0); |
Qu Wenruo | 52427260 | 2017-03-08 10:25:52 +0800 | [diff] [blame] | 2033 | if (ret) |
Nikolay Borisov | 98456b9 | 2020-06-03 08:55:29 +0300 | [diff] [blame] | 2034 | btrfs_cleanup_ordered_extents(inode, locked_page, start, |
Nikolay Borisov | d1051d6 | 2018-11-21 17:10:52 +0200 | [diff] [blame] | 2035 | end - start + 1); |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 2036 | return ret; |
| 2037 | } |
| 2038 | |
Nikolay Borisov | abbb55f | 2018-11-01 14:09:53 +0200 | [diff] [blame] | 2039 | void btrfs_split_delalloc_extent(struct inode *inode, |
| 2040 | struct extent_state *orig, u64 split) |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 2041 | { |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 2042 | u64 size; |
| 2043 | |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 2044 | /* not delalloc, ignore it */ |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 2045 | if (!(orig->state & EXTENT_DELALLOC)) |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 2046 | return; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 2047 | |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 2048 | size = orig->end - orig->start + 1; |
| 2049 | if (size > BTRFS_MAX_EXTENT_SIZE) { |
David Sterba | 823bb20 | 2017-01-04 11:09:51 +0100 | [diff] [blame] | 2050 | u32 num_extents; |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 2051 | u64 new_size; |
| 2052 | |
| 2053 | /* |
Nikolay Borisov | 5c84819 | 2018-11-01 14:09:52 +0200 | [diff] [blame] | 2054 | * See the explanation in btrfs_merge_delalloc_extent, the same |
Josef Bacik | ba11721 | 2015-03-13 15:01:24 -0400 | [diff] [blame] | 2055 | * applies here, just in reverse. |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 2056 | */ |
| 2057 | new_size = orig->end - split + 1; |
David Sterba | 823bb20 | 2017-01-04 11:09:51 +0100 | [diff] [blame] | 2058 | num_extents = count_max_extents(new_size); |
Josef Bacik | ba11721 | 2015-03-13 15:01:24 -0400 | [diff] [blame] | 2059 | new_size = split - orig->start; |
David Sterba | 823bb20 | 2017-01-04 11:09:51 +0100 | [diff] [blame] | 2060 | num_extents += count_max_extents(new_size); |
| 2061 | if (count_max_extents(size) >= num_extents) |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 2062 | return; |
| 2063 | } |
| 2064 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 2065 | spin_lock(&BTRFS_I(inode)->lock); |
Josef Bacik | 8b62f87 | 2017-10-19 14:15:55 -0400 | [diff] [blame] | 2066 | btrfs_mod_outstanding_extents(BTRFS_I(inode), 1); |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 2067 | spin_unlock(&BTRFS_I(inode)->lock); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 2068 | } |
| 2069 | |
| 2070 | /* |
Nikolay Borisov | 5c84819 | 2018-11-01 14:09:52 +0200 | [diff] [blame] | 2071 | * Handle merged delayed allocation extents so we can keep track of new extents |
| 2072 | * that are just merged onto old extents, such as when we are doing sequential |
| 2073 | * 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] | 2074 | */ |
Nikolay Borisov | 5c84819 | 2018-11-01 14:09:52 +0200 | [diff] [blame] | 2075 | void btrfs_merge_delalloc_extent(struct inode *inode, struct extent_state *new, |
| 2076 | struct extent_state *other) |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 2077 | { |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 2078 | u64 new_size, old_size; |
David Sterba | 823bb20 | 2017-01-04 11:09:51 +0100 | [diff] [blame] | 2079 | u32 num_extents; |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 2080 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 2081 | /* not delalloc, ignore it */ |
| 2082 | if (!(other->state & EXTENT_DELALLOC)) |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 2083 | return; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 2084 | |
Josef Bacik | 8461a3d | 2015-03-13 15:12:08 -0400 | [diff] [blame] | 2085 | if (new->start > other->start) |
| 2086 | new_size = new->end - other->start + 1; |
| 2087 | else |
| 2088 | new_size = other->end - new->start + 1; |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 2089 | |
| 2090 | /* we're not bigger than the max, unreserve the space and go */ |
| 2091 | if (new_size <= BTRFS_MAX_EXTENT_SIZE) { |
| 2092 | spin_lock(&BTRFS_I(inode)->lock); |
Josef Bacik | 8b62f87 | 2017-10-19 14:15:55 -0400 | [diff] [blame] | 2093 | btrfs_mod_outstanding_extents(BTRFS_I(inode), -1); |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 2094 | spin_unlock(&BTRFS_I(inode)->lock); |
| 2095 | return; |
| 2096 | } |
| 2097 | |
| 2098 | /* |
Josef Bacik | ba11721 | 2015-03-13 15:01:24 -0400 | [diff] [blame] | 2099 | * We have to add up either side to figure out how many extents were |
| 2100 | * accounted for before we merged into one big extent. If the number of |
| 2101 | * extents we accounted for is <= the amount we need for the new range |
| 2102 | * then we can return, otherwise drop. Think of it like this |
| 2103 | * |
| 2104 | * [ 4k][MAX_SIZE] |
| 2105 | * |
| 2106 | * So we've grown the extent by a MAX_SIZE extent, this would mean we |
| 2107 | * need 2 outstanding extents, on one side we have 1 and the other side |
| 2108 | * we have 1 so they are == and we can return. But in this case |
| 2109 | * |
| 2110 | * [MAX_SIZE+4k][MAX_SIZE+4k] |
| 2111 | * |
| 2112 | * Each range on their own accounts for 2 extents, but merged together |
| 2113 | * they are only 3 extents worth of accounting, so we need to drop in |
| 2114 | * this case. |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 2115 | */ |
Josef Bacik | ba11721 | 2015-03-13 15:01:24 -0400 | [diff] [blame] | 2116 | old_size = other->end - other->start + 1; |
David Sterba | 823bb20 | 2017-01-04 11:09:51 +0100 | [diff] [blame] | 2117 | num_extents = count_max_extents(old_size); |
Josef Bacik | ba11721 | 2015-03-13 15:01:24 -0400 | [diff] [blame] | 2118 | old_size = new->end - new->start + 1; |
David Sterba | 823bb20 | 2017-01-04 11:09:51 +0100 | [diff] [blame] | 2119 | num_extents += count_max_extents(old_size); |
| 2120 | if (count_max_extents(new_size) >= num_extents) |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 2121 | return; |
| 2122 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 2123 | spin_lock(&BTRFS_I(inode)->lock); |
Josef Bacik | 8b62f87 | 2017-10-19 14:15:55 -0400 | [diff] [blame] | 2124 | btrfs_mod_outstanding_extents(BTRFS_I(inode), -1); |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 2125 | spin_unlock(&BTRFS_I(inode)->lock); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 2126 | } |
| 2127 | |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 2128 | static void btrfs_add_delalloc_inodes(struct btrfs_root *root, |
| 2129 | struct inode *inode) |
| 2130 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2131 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
| 2132 | |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 2133 | spin_lock(&root->delalloc_lock); |
| 2134 | if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) { |
| 2135 | list_add_tail(&BTRFS_I(inode)->delalloc_inodes, |
| 2136 | &root->delalloc_inodes); |
| 2137 | set_bit(BTRFS_INODE_IN_DELALLOC_LIST, |
| 2138 | &BTRFS_I(inode)->runtime_flags); |
| 2139 | root->nr_delalloc_inodes++; |
| 2140 | if (root->nr_delalloc_inodes == 1) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2141 | spin_lock(&fs_info->delalloc_root_lock); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 2142 | BUG_ON(!list_empty(&root->delalloc_root)); |
| 2143 | list_add_tail(&root->delalloc_root, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2144 | &fs_info->delalloc_roots); |
| 2145 | spin_unlock(&fs_info->delalloc_root_lock); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 2146 | } |
| 2147 | } |
| 2148 | spin_unlock(&root->delalloc_lock); |
| 2149 | } |
| 2150 | |
Nikolay Borisov | 2b87733 | 2018-04-27 12:21:51 +0300 | [diff] [blame] | 2151 | |
| 2152 | void __btrfs_del_delalloc_inode(struct btrfs_root *root, |
| 2153 | struct btrfs_inode *inode) |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 2154 | { |
David Sterba | 3ffbd68 | 2018-06-29 10:56:42 +0200 | [diff] [blame] | 2155 | struct btrfs_fs_info *fs_info = root->fs_info; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2156 | |
Nikolay Borisov | 9e3e97f | 2017-02-20 13:51:07 +0200 | [diff] [blame] | 2157 | if (!list_empty(&inode->delalloc_inodes)) { |
| 2158 | list_del_init(&inode->delalloc_inodes); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 2159 | clear_bit(BTRFS_INODE_IN_DELALLOC_LIST, |
Nikolay Borisov | 9e3e97f | 2017-02-20 13:51:07 +0200 | [diff] [blame] | 2160 | &inode->runtime_flags); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 2161 | root->nr_delalloc_inodes--; |
| 2162 | if (!root->nr_delalloc_inodes) { |
Nikolay Borisov | 7c8a0d3 | 2018-04-27 12:21:52 +0300 | [diff] [blame] | 2163 | ASSERT(list_empty(&root->delalloc_inodes)); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2164 | spin_lock(&fs_info->delalloc_root_lock); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 2165 | BUG_ON(list_empty(&root->delalloc_root)); |
| 2166 | list_del_init(&root->delalloc_root); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2167 | spin_unlock(&fs_info->delalloc_root_lock); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 2168 | } |
| 2169 | } |
Nikolay Borisov | 2b87733 | 2018-04-27 12:21:51 +0300 | [diff] [blame] | 2170 | } |
| 2171 | |
| 2172 | static void btrfs_del_delalloc_inode(struct btrfs_root *root, |
| 2173 | struct btrfs_inode *inode) |
| 2174 | { |
| 2175 | spin_lock(&root->delalloc_lock); |
| 2176 | __btrfs_del_delalloc_inode(root, inode); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 2177 | spin_unlock(&root->delalloc_lock); |
| 2178 | } |
| 2179 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2180 | /* |
Nikolay Borisov | e06a1fc | 2018-11-01 14:09:50 +0200 | [diff] [blame] | 2181 | * Properly track delayed allocation bytes in the inode and to maintain the |
| 2182 | * list of inodes that have pending delalloc work to be done. |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2183 | */ |
Nikolay Borisov | e06a1fc | 2018-11-01 14:09:50 +0200 | [diff] [blame] | 2184 | void btrfs_set_delalloc_extent(struct inode *inode, struct extent_state *state, |
| 2185 | unsigned *bits) |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 2186 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2187 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
| 2188 | |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 2189 | if ((*bits & EXTENT_DEFRAG) && !(*bits & EXTENT_DELALLOC)) |
| 2190 | WARN_ON(1); |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 2191 | /* |
| 2192 | * set_bit and clear bit hooks normally require _irqsave/restore |
Sergei Trofimovich | 27160b6 | 2011-05-20 20:20:32 +0000 | [diff] [blame] | 2193 | * but in this case, we are only testing for the DELALLOC |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 2194 | * bit, which is only set or cleared with irqs on |
| 2195 | */ |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 2196 | if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) { |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 2197 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 2198 | u64 len = state->end + 1 - state->start; |
Josef Bacik | 8b62f87 | 2017-10-19 14:15:55 -0400 | [diff] [blame] | 2199 | u32 num_extents = count_max_extents(len); |
Nikolay Borisov | 70ddc55 | 2017-02-20 13:50:35 +0200 | [diff] [blame] | 2200 | bool do_list = !btrfs_is_free_space_inode(BTRFS_I(inode)); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 2201 | |
Josef Bacik | 8b62f87 | 2017-10-19 14:15:55 -0400 | [diff] [blame] | 2202 | spin_lock(&BTRFS_I(inode)->lock); |
| 2203 | btrfs_mod_outstanding_extents(BTRFS_I(inode), num_extents); |
| 2204 | spin_unlock(&BTRFS_I(inode)->lock); |
Josef Bacik | 287a0ab | 2010-03-19 18:07:23 +0000 | [diff] [blame] | 2205 | |
Josef Bacik | 6a3891c | 2015-03-16 17:38:52 -0400 | [diff] [blame] | 2206 | /* For sanity tests */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2207 | if (btrfs_is_testing(fs_info)) |
Josef Bacik | 6a3891c | 2015-03-16 17:38:52 -0400 | [diff] [blame] | 2208 | return; |
| 2209 | |
Nikolay Borisov | 104b4e5 | 2017-06-20 21:01:20 +0300 | [diff] [blame] | 2210 | percpu_counter_add_batch(&fs_info->delalloc_bytes, len, |
| 2211 | fs_info->delalloc_batch); |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 2212 | spin_lock(&BTRFS_I(inode)->lock); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 2213 | BTRFS_I(inode)->delalloc_bytes += len; |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 2214 | if (*bits & EXTENT_DEFRAG) |
| 2215 | BTRFS_I(inode)->defrag_bytes += len; |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 2216 | if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST, |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 2217 | &BTRFS_I(inode)->runtime_flags)) |
| 2218 | btrfs_add_delalloc_inodes(root, inode); |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 2219 | spin_unlock(&BTRFS_I(inode)->lock); |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 2220 | } |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 2221 | |
| 2222 | if (!(state->state & EXTENT_DELALLOC_NEW) && |
| 2223 | (*bits & EXTENT_DELALLOC_NEW)) { |
| 2224 | spin_lock(&BTRFS_I(inode)->lock); |
| 2225 | BTRFS_I(inode)->new_delalloc_bytes += state->end + 1 - |
| 2226 | state->start; |
| 2227 | spin_unlock(&BTRFS_I(inode)->lock); |
| 2228 | } |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 2229 | } |
| 2230 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2231 | /* |
Nikolay Borisov | a36bb5f | 2018-11-01 14:09:51 +0200 | [diff] [blame] | 2232 | * Once a range is no longer delalloc this function ensures that proper |
| 2233 | * accounting happens. |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2234 | */ |
Nikolay Borisov | a36bb5f | 2018-11-01 14:09:51 +0200 | [diff] [blame] | 2235 | void btrfs_clear_delalloc_extent(struct inode *vfs_inode, |
| 2236 | struct extent_state *state, unsigned *bits) |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 2237 | { |
Nikolay Borisov | a36bb5f | 2018-11-01 14:09:51 +0200 | [diff] [blame] | 2238 | struct btrfs_inode *inode = BTRFS_I(vfs_inode); |
| 2239 | struct btrfs_fs_info *fs_info = btrfs_sb(vfs_inode->i_sb); |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 2240 | u64 len = state->end + 1 - state->start; |
David Sterba | 823bb20 | 2017-01-04 11:09:51 +0100 | [diff] [blame] | 2241 | u32 num_extents = count_max_extents(len); |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 2242 | |
Filipe Manana | 4a4b964 | 2017-07-27 19:52:55 +0100 | [diff] [blame] | 2243 | if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG)) { |
| 2244 | spin_lock(&inode->lock); |
Nikolay Borisov | 6fc0ef6 | 2017-02-20 13:51:03 +0200 | [diff] [blame] | 2245 | inode->defrag_bytes -= len; |
Filipe Manana | 4a4b964 | 2017-07-27 19:52:55 +0100 | [diff] [blame] | 2246 | spin_unlock(&inode->lock); |
| 2247 | } |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 2248 | |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 2249 | /* |
| 2250 | * set_bit and clear bit hooks normally require _irqsave/restore |
Sergei Trofimovich | 27160b6 | 2011-05-20 20:20:32 +0000 | [diff] [blame] | 2251 | * but in this case, we are only testing for the DELALLOC |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 2252 | * bit, which is only set or cleared with irqs on |
| 2253 | */ |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 2254 | if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) { |
Nikolay Borisov | 6fc0ef6 | 2017-02-20 13:51:03 +0200 | [diff] [blame] | 2255 | struct btrfs_root *root = inode->root; |
Liu Bo | 83eea1f | 2012-07-10 05:28:39 -0600 | [diff] [blame] | 2256 | bool do_list = !btrfs_is_free_space_inode(inode); |
Chris Mason | bcbfce8 | 2008-04-22 13:26:47 -0400 | [diff] [blame] | 2257 | |
Josef Bacik | 8b62f87 | 2017-10-19 14:15:55 -0400 | [diff] [blame] | 2258 | spin_lock(&inode->lock); |
| 2259 | btrfs_mod_outstanding_extents(inode, -num_extents); |
| 2260 | spin_unlock(&inode->lock); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 2261 | |
Josef Bacik | b6d08f0 | 2013-09-27 14:57:43 -0400 | [diff] [blame] | 2262 | /* |
| 2263 | * We don't reserve metadata space for space cache inodes so we |
Andrea Gelmini | 52042d8 | 2018-11-28 12:05:13 +0100 | [diff] [blame] | 2264 | * don't need to call delalloc_release_metadata if there is an |
Josef Bacik | b6d08f0 | 2013-09-27 14:57:43 -0400 | [diff] [blame] | 2265 | * error. |
| 2266 | */ |
Filipe Manana | a315e68 | 2017-03-06 23:04:20 +0000 | [diff] [blame] | 2267 | if (*bits & EXTENT_CLEAR_META_RESV && |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2268 | root != fs_info->tree_root) |
Qu Wenruo | 43b1859 | 2017-12-12 15:34:32 +0800 | [diff] [blame] | 2269 | btrfs_delalloc_release_metadata(inode, len, false); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 2270 | |
Josef Bacik | 6a3891c | 2015-03-16 17:38:52 -0400 | [diff] [blame] | 2271 | /* For sanity tests. */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2272 | if (btrfs_is_testing(fs_info)) |
Josef Bacik | 6a3891c | 2015-03-16 17:38:52 -0400 | [diff] [blame] | 2273 | return; |
| 2274 | |
Johannes Thumshirn | 37f00a6 | 2021-09-09 01:19:25 +0900 | [diff] [blame] | 2275 | if (!btrfs_is_data_reloc_root(root) && |
Filipe Manana | a315e68 | 2017-03-06 23:04:20 +0000 | [diff] [blame] | 2276 | do_list && !(state->state & EXTENT_NORESERVE) && |
| 2277 | (*bits & EXTENT_CLEAR_DATA_RESV)) |
Nikolay Borisov | 9db5d51 | 2020-06-03 08:55:38 +0300 | [diff] [blame] | 2278 | btrfs_free_reserved_data_space_noquota(fs_info, len); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 2279 | |
Nikolay Borisov | 104b4e5 | 2017-06-20 21:01:20 +0300 | [diff] [blame] | 2280 | percpu_counter_add_batch(&fs_info->delalloc_bytes, -len, |
| 2281 | fs_info->delalloc_batch); |
Nikolay Borisov | 6fc0ef6 | 2017-02-20 13:51:03 +0200 | [diff] [blame] | 2282 | spin_lock(&inode->lock); |
| 2283 | inode->delalloc_bytes -= len; |
| 2284 | if (do_list && inode->delalloc_bytes == 0 && |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 2285 | test_bit(BTRFS_INODE_IN_DELALLOC_LIST, |
Nikolay Borisov | 9e3e97f | 2017-02-20 13:51:07 +0200 | [diff] [blame] | 2286 | &inode->runtime_flags)) |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 2287 | btrfs_del_delalloc_inode(root, inode); |
Nikolay Borisov | 6fc0ef6 | 2017-02-20 13:51:03 +0200 | [diff] [blame] | 2288 | spin_unlock(&inode->lock); |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 2289 | } |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 2290 | |
| 2291 | if ((state->state & EXTENT_DELALLOC_NEW) && |
| 2292 | (*bits & EXTENT_DELALLOC_NEW)) { |
| 2293 | spin_lock(&inode->lock); |
| 2294 | ASSERT(inode->new_delalloc_bytes >= len); |
| 2295 | inode->new_delalloc_bytes -= len; |
Filipe Manana | 2766ff6 | 2020-11-04 11:07:34 +0000 | [diff] [blame] | 2296 | if (*bits & EXTENT_ADD_INODE_BYTES) |
| 2297 | inode_add_bytes(&inode->vfs_inode, len); |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 2298 | spin_unlock(&inode->lock); |
| 2299 | } |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 2300 | } |
| 2301 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2302 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2303 | * in order to insert checksums into the metadata in large chunks, |
| 2304 | * we wait until bio submission time. All the pages in the bio are |
| 2305 | * checksummed and sums are attached onto the ordered extent record. |
| 2306 | * |
| 2307 | * At IO completion time the cums attached on the ordered extent record |
| 2308 | * are inserted into the btree |
| 2309 | */ |
Qu Wenruo | 8896a08 | 2020-10-21 14:24:53 +0800 | [diff] [blame] | 2310 | 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] | 2311 | u64 dio_file_offset) |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 2312 | { |
Johannes Thumshirn | c965d64 | 2020-07-28 20:25:41 +0900 | [diff] [blame] | 2313 | return btrfs_csum_one_bio(BTRFS_I(inode), bio, 0, 0); |
Chris Mason | 4a69a41 | 2008-11-06 22:03:00 -0500 | [diff] [blame] | 2314 | } |
Chris Mason | e015640 | 2008-04-16 11:15:20 -0400 | [diff] [blame] | 2315 | |
Naohiro Aota | abb99cf | 2021-06-28 17:57:28 +0900 | [diff] [blame] | 2316 | /* |
| 2317 | * Split an extent_map at [start, start + len] |
| 2318 | * |
| 2319 | * This function is intended to be used only for extract_ordered_extent(). |
| 2320 | */ |
| 2321 | static int split_zoned_em(struct btrfs_inode *inode, u64 start, u64 len, |
| 2322 | u64 pre, u64 post) |
| 2323 | { |
| 2324 | struct extent_map_tree *em_tree = &inode->extent_tree; |
| 2325 | struct extent_map *em; |
| 2326 | struct extent_map *split_pre = NULL; |
| 2327 | struct extent_map *split_mid = NULL; |
| 2328 | struct extent_map *split_post = NULL; |
| 2329 | int ret = 0; |
Naohiro Aota | abb99cf | 2021-06-28 17:57:28 +0900 | [diff] [blame] | 2330 | unsigned long flags; |
| 2331 | |
| 2332 | /* Sanity check */ |
| 2333 | if (pre == 0 && post == 0) |
| 2334 | return 0; |
| 2335 | |
| 2336 | split_pre = alloc_extent_map(); |
| 2337 | if (pre) |
| 2338 | split_mid = alloc_extent_map(); |
| 2339 | if (post) |
| 2340 | split_post = alloc_extent_map(); |
| 2341 | if (!split_pre || (pre && !split_mid) || (post && !split_post)) { |
| 2342 | ret = -ENOMEM; |
| 2343 | goto out; |
| 2344 | } |
| 2345 | |
| 2346 | ASSERT(pre + post < len); |
| 2347 | |
| 2348 | lock_extent(&inode->io_tree, start, start + len - 1); |
| 2349 | write_lock(&em_tree->lock); |
| 2350 | em = lookup_extent_mapping(em_tree, start, len); |
| 2351 | if (!em) { |
| 2352 | ret = -EIO; |
| 2353 | goto out_unlock; |
| 2354 | } |
| 2355 | |
| 2356 | ASSERT(em->len == len); |
| 2357 | ASSERT(!test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)); |
| 2358 | ASSERT(em->block_start < EXTENT_MAP_LAST_BYTE); |
Naohiro Aota | 63fb587 | 2021-08-09 09:29:18 +0900 | [diff] [blame] | 2359 | ASSERT(test_bit(EXTENT_FLAG_PINNED, &em->flags)); |
| 2360 | ASSERT(!test_bit(EXTENT_FLAG_LOGGING, &em->flags)); |
| 2361 | ASSERT(!list_empty(&em->list)); |
Naohiro Aota | abb99cf | 2021-06-28 17:57:28 +0900 | [diff] [blame] | 2362 | |
| 2363 | flags = em->flags; |
| 2364 | clear_bit(EXTENT_FLAG_PINNED, &em->flags); |
Naohiro Aota | abb99cf | 2021-06-28 17:57:28 +0900 | [diff] [blame] | 2365 | |
| 2366 | /* First, replace the em with a new extent_map starting from * em->start */ |
| 2367 | split_pre->start = em->start; |
| 2368 | split_pre->len = (pre ? pre : em->len - post); |
| 2369 | split_pre->orig_start = split_pre->start; |
| 2370 | split_pre->block_start = em->block_start; |
| 2371 | split_pre->block_len = split_pre->len; |
| 2372 | split_pre->orig_block_len = split_pre->block_len; |
| 2373 | split_pre->ram_bytes = split_pre->len; |
| 2374 | split_pre->flags = flags; |
| 2375 | split_pre->compress_type = em->compress_type; |
| 2376 | split_pre->generation = em->generation; |
| 2377 | |
Naohiro Aota | 63fb587 | 2021-08-09 09:29:18 +0900 | [diff] [blame] | 2378 | replace_extent_mapping(em_tree, em, split_pre, 1); |
Naohiro Aota | abb99cf | 2021-06-28 17:57:28 +0900 | [diff] [blame] | 2379 | |
| 2380 | /* |
| 2381 | * Now we only have an extent_map at: |
| 2382 | * [em->start, em->start + pre] if pre != 0 |
| 2383 | * [em->start, em->start + em->len - post] if pre == 0 |
| 2384 | */ |
| 2385 | |
| 2386 | if (pre) { |
| 2387 | /* Insert the middle extent_map */ |
| 2388 | split_mid->start = em->start + pre; |
| 2389 | split_mid->len = em->len - pre - post; |
| 2390 | split_mid->orig_start = split_mid->start; |
| 2391 | split_mid->block_start = em->block_start + pre; |
| 2392 | split_mid->block_len = split_mid->len; |
| 2393 | split_mid->orig_block_len = split_mid->block_len; |
| 2394 | split_mid->ram_bytes = split_mid->len; |
| 2395 | split_mid->flags = flags; |
| 2396 | split_mid->compress_type = em->compress_type; |
| 2397 | split_mid->generation = em->generation; |
Naohiro Aota | 63fb587 | 2021-08-09 09:29:18 +0900 | [diff] [blame] | 2398 | add_extent_mapping(em_tree, split_mid, 1); |
Naohiro Aota | abb99cf | 2021-06-28 17:57:28 +0900 | [diff] [blame] | 2399 | } |
| 2400 | |
| 2401 | if (post) { |
| 2402 | split_post->start = em->start + em->len - post; |
| 2403 | split_post->len = post; |
| 2404 | split_post->orig_start = split_post->start; |
| 2405 | split_post->block_start = em->block_start + em->len - post; |
| 2406 | split_post->block_len = split_post->len; |
| 2407 | split_post->orig_block_len = split_post->block_len; |
| 2408 | split_post->ram_bytes = split_post->len; |
| 2409 | split_post->flags = flags; |
| 2410 | split_post->compress_type = em->compress_type; |
| 2411 | split_post->generation = em->generation; |
Naohiro Aota | 63fb587 | 2021-08-09 09:29:18 +0900 | [diff] [blame] | 2412 | add_extent_mapping(em_tree, split_post, 1); |
Naohiro Aota | abb99cf | 2021-06-28 17:57:28 +0900 | [diff] [blame] | 2413 | } |
| 2414 | |
| 2415 | /* Once for us */ |
| 2416 | free_extent_map(em); |
| 2417 | /* Once for the tree */ |
| 2418 | free_extent_map(em); |
| 2419 | |
| 2420 | out_unlock: |
| 2421 | write_unlock(&em_tree->lock); |
| 2422 | unlock_extent(&inode->io_tree, start, start + len - 1); |
| 2423 | out: |
| 2424 | free_extent_map(split_pre); |
| 2425 | free_extent_map(split_mid); |
| 2426 | free_extent_map(split_post); |
| 2427 | |
| 2428 | return ret; |
| 2429 | } |
| 2430 | |
Naohiro Aota | d22002f | 2021-02-04 19:22:00 +0900 | [diff] [blame] | 2431 | static blk_status_t extract_ordered_extent(struct btrfs_inode *inode, |
| 2432 | struct bio *bio, loff_t file_offset) |
| 2433 | { |
| 2434 | struct btrfs_ordered_extent *ordered; |
Naohiro Aota | d22002f | 2021-02-04 19:22:00 +0900 | [diff] [blame] | 2435 | u64 start = (u64)bio->bi_iter.bi_sector << SECTOR_SHIFT; |
Naohiro Aota | abb99cf | 2021-06-28 17:57:28 +0900 | [diff] [blame] | 2436 | u64 file_len; |
Naohiro Aota | d22002f | 2021-02-04 19:22:00 +0900 | [diff] [blame] | 2437 | u64 len = bio->bi_iter.bi_size; |
| 2438 | u64 end = start + len; |
| 2439 | u64 ordered_end; |
| 2440 | u64 pre, post; |
| 2441 | int ret = 0; |
| 2442 | |
| 2443 | ordered = btrfs_lookup_ordered_extent(inode, file_offset); |
| 2444 | if (WARN_ON_ONCE(!ordered)) |
| 2445 | return BLK_STS_IOERR; |
| 2446 | |
| 2447 | /* No need to split */ |
| 2448 | if (ordered->disk_num_bytes == len) |
| 2449 | goto out; |
| 2450 | |
| 2451 | /* We cannot split once end_bio'd ordered extent */ |
| 2452 | if (WARN_ON_ONCE(ordered->bytes_left != ordered->disk_num_bytes)) { |
| 2453 | ret = -EINVAL; |
| 2454 | goto out; |
| 2455 | } |
| 2456 | |
| 2457 | /* We cannot split a compressed ordered extent */ |
| 2458 | if (WARN_ON_ONCE(ordered->disk_num_bytes != ordered->num_bytes)) { |
| 2459 | ret = -EINVAL; |
| 2460 | goto out; |
| 2461 | } |
| 2462 | |
| 2463 | ordered_end = ordered->disk_bytenr + ordered->disk_num_bytes; |
| 2464 | /* bio must be in one ordered extent */ |
| 2465 | if (WARN_ON_ONCE(start < ordered->disk_bytenr || end > ordered_end)) { |
| 2466 | ret = -EINVAL; |
| 2467 | goto out; |
| 2468 | } |
| 2469 | |
| 2470 | /* Checksum list should be empty */ |
| 2471 | if (WARN_ON_ONCE(!list_empty(&ordered->list))) { |
| 2472 | ret = -EINVAL; |
| 2473 | goto out; |
| 2474 | } |
| 2475 | |
Naohiro Aota | abb99cf | 2021-06-28 17:57:28 +0900 | [diff] [blame] | 2476 | file_len = ordered->num_bytes; |
Naohiro Aota | d22002f | 2021-02-04 19:22:00 +0900 | [diff] [blame] | 2477 | pre = start - ordered->disk_bytenr; |
| 2478 | post = ordered_end - end; |
| 2479 | |
| 2480 | ret = btrfs_split_ordered_extent(ordered, pre, post); |
| 2481 | if (ret) |
| 2482 | goto out; |
Naohiro Aota | abb99cf | 2021-06-28 17:57:28 +0900 | [diff] [blame] | 2483 | ret = split_zoned_em(inode, file_offset, file_len, pre, post); |
Naohiro Aota | d22002f | 2021-02-04 19:22:00 +0900 | [diff] [blame] | 2484 | |
| 2485 | out: |
Naohiro Aota | d22002f | 2021-02-04 19:22:00 +0900 | [diff] [blame] | 2486 | btrfs_put_ordered_extent(ordered); |
| 2487 | |
| 2488 | return errno_to_blk_status(ret); |
| 2489 | } |
| 2490 | |
Chris Mason | 4a69a41 | 2008-11-06 22:03:00 -0500 | [diff] [blame] | 2491 | /* |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 2492 | * 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] | 2493 | * on write, or reading the csums from the tree before a read. |
| 2494 | * |
| 2495 | * Rules about async/sync submit, |
| 2496 | * a) read: sync submit |
| 2497 | * |
| 2498 | * b) write without checksum: sync submit |
| 2499 | * |
| 2500 | * c) write with checksum: |
| 2501 | * c-1) if bio is issued by fsync: sync submit |
| 2502 | * (sync_writers != 0) |
| 2503 | * |
| 2504 | * c-2) if root is reloc root: sync submit |
| 2505 | * (only in case of buffered IO) |
| 2506 | * |
| 2507 | * c-3) otherwise: async submit |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2508 | */ |
Nikolay Borisov | 908930f | 2020-09-18 16:34:37 +0300 | [diff] [blame] | 2509 | blk_status_t btrfs_submit_data_bio(struct inode *inode, struct bio *bio, |
| 2510 | int mirror_num, unsigned long bio_flags) |
Nikolay Borisov | 50489a5 | 2019-04-10 19:46:04 +0300 | [diff] [blame] | 2511 | |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 2512 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2513 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 2514 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chandan Rajendra | 0d51e28 | 2015-07-27 15:26:43 +0530 | [diff] [blame] | 2515 | enum btrfs_wq_endio_type metadata = BTRFS_WQ_ENDIO_DATA; |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 2516 | blk_status_t ret = 0; |
Chris Mason | 19b9bdb | 2008-10-30 14:23:13 -0400 | [diff] [blame] | 2517 | int skip_sum; |
Josef Bacik | b812ce2 | 2012-11-16 13:56:32 -0500 | [diff] [blame] | 2518 | int async = !atomic_read(&BTRFS_I(inode)->sync_writers); |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 2519 | |
Josef Bacik | 42437a6 | 2020-10-16 11:29:18 -0400 | [diff] [blame] | 2520 | skip_sum = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM) || |
Josef Bacik | 056c831 | 2021-11-05 16:45:47 -0400 | [diff] [blame] | 2521 | test_bit(BTRFS_FS_STATE_NO_CSUMS, &fs_info->fs_state); |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 2522 | |
Nikolay Borisov | 70ddc55 | 2017-02-20 13:50:35 +0200 | [diff] [blame] | 2523 | if (btrfs_is_free_space_inode(BTRFS_I(inode))) |
Chandan Rajendra | 0d51e28 | 2015-07-27 15:26:43 +0530 | [diff] [blame] | 2524 | metadata = BTRFS_WQ_ENDIO_FREE_SPACE; |
Jeff Mahoney | 0417341 | 2011-10-03 23:23:12 -0400 | [diff] [blame] | 2525 | |
Naohiro Aota | d22002f | 2021-02-04 19:22:00 +0900 | [diff] [blame] | 2526 | if (bio_op(bio) == REQ_OP_ZONE_APPEND) { |
| 2527 | struct page *page = bio_first_bvec_all(bio)->bv_page; |
| 2528 | loff_t file_offset = page_offset(page); |
| 2529 | |
| 2530 | ret = extract_ordered_extent(BTRFS_I(inode), bio, file_offset); |
| 2531 | if (ret) |
| 2532 | goto out; |
| 2533 | } |
| 2534 | |
Naohiro Aota | cfe9444 | 2021-02-04 19:21:59 +0900 | [diff] [blame] | 2535 | if (btrfs_op(bio) != BTRFS_MAP_WRITE) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2536 | ret = btrfs_bio_wq_end_io(fs_info, bio, metadata); |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2537 | if (ret) |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 2538 | goto out; |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2539 | |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 2540 | if (bio_flags & EXTENT_BIO_COMPRESSED) { |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 2541 | ret = btrfs_submit_compressed_read(inode, bio, |
| 2542 | mirror_num, |
| 2543 | bio_flags); |
| 2544 | goto out; |
Josef Bacik | 334c16d | 2020-10-16 11:29:14 -0400 | [diff] [blame] | 2545 | } else { |
| 2546 | /* |
| 2547 | * Lookup bio sums does extra checks around whether we |
| 2548 | * need to csum or not, which is why we ignore skip_sum |
| 2549 | * here. |
| 2550 | */ |
Qu Wenruo | 6275193 | 2020-12-02 14:48:06 +0800 | [diff] [blame] | 2551 | ret = btrfs_lookup_bio_sums(inode, bio, NULL); |
Tsutomu Itoh | c2db107 | 2011-03-01 06:48:31 +0000 | [diff] [blame] | 2552 | if (ret) |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 2553 | goto out; |
Tsutomu Itoh | c2db107 | 2011-03-01 06:48:31 +0000 | [diff] [blame] | 2554 | } |
Chris Mason | 4d1b5fb | 2008-08-20 09:44:52 -0400 | [diff] [blame] | 2555 | goto mapit; |
Josef Bacik | b812ce2 | 2012-11-16 13:56:32 -0500 | [diff] [blame] | 2556 | } else if (async && !skip_sum) { |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 2557 | /* csum items have already been cloned */ |
Johannes Thumshirn | 37f00a6 | 2021-09-09 01:19:25 +0900 | [diff] [blame] | 2558 | if (btrfs_is_data_reloc_root(root)) |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 2559 | goto mapit; |
Chris Mason | 19b9bdb | 2008-10-30 14:23:13 -0400 | [diff] [blame] | 2560 | /* we're doing a write, do the async checksumming */ |
Qu Wenruo | 8896a08 | 2020-10-21 14:24:53 +0800 | [diff] [blame] | 2561 | ret = btrfs_wq_submit_bio(inode, bio, mirror_num, bio_flags, |
| 2562 | 0, btrfs_submit_bio_start); |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 2563 | goto out; |
Josef Bacik | b812ce2 | 2012-11-16 13:56:32 -0500 | [diff] [blame] | 2564 | } else if (!skip_sum) { |
Nikolay Borisov | bd242a0 | 2020-06-03 08:55:07 +0300 | [diff] [blame] | 2565 | ret = btrfs_csum_one_bio(BTRFS_I(inode), bio, 0, 0); |
Josef Bacik | b812ce2 | 2012-11-16 13:56:32 -0500 | [diff] [blame] | 2566 | if (ret) |
| 2567 | goto out; |
Chris Mason | 19b9bdb | 2008-10-30 14:23:13 -0400 | [diff] [blame] | 2568 | } |
| 2569 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2570 | mapit: |
Chris Mason | 08635ba | 2019-07-10 12:28:14 -0700 | [diff] [blame] | 2571 | ret = btrfs_map_bio(fs_info, bio, mirror_num); |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 2572 | |
| 2573 | out: |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 2574 | if (ret) { |
| 2575 | bio->bi_status = ret; |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 2576 | bio_endio(bio); |
| 2577 | } |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 2578 | return ret; |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 2579 | } |
Chris Mason | 6885f30 | 2008-02-20 16:11:05 -0500 | [diff] [blame] | 2580 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2581 | /* |
| 2582 | * given a list of ordered sums record them in the inode. This happens |
| 2583 | * at IO completion time based on sums calculated at bio submission time. |
| 2584 | */ |
Nikolay Borisov | 510f85e | 2020-09-18 12:15:52 +0300 | [diff] [blame] | 2585 | static int add_pending_csums(struct btrfs_trans_handle *trans, |
| 2586 | struct list_head *list) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2587 | { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2588 | struct btrfs_ordered_sum *sum; |
Josef Bacik | fc28b25 | 2021-11-05 16:45:48 -0400 | [diff] [blame] | 2589 | struct btrfs_root *csum_root = NULL; |
Nikolay Borisov | ac01f26 | 2018-01-08 10:59:43 +0200 | [diff] [blame] | 2590 | int ret; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2591 | |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 2592 | list_for_each_entry(sum, list, list) { |
David Sterba | 7c2871a | 2017-11-08 01:07:43 +0100 | [diff] [blame] | 2593 | trans->adding_csums = true; |
Josef Bacik | fc28b25 | 2021-11-05 16:45:48 -0400 | [diff] [blame] | 2594 | if (!csum_root) |
| 2595 | csum_root = btrfs_csum_root(trans->fs_info, |
| 2596 | sum->bytenr); |
| 2597 | ret = btrfs_csum_file_blocks(trans, csum_root, sum); |
David Sterba | 7c2871a | 2017-11-08 01:07:43 +0100 | [diff] [blame] | 2598 | trans->adding_csums = false; |
Nikolay Borisov | ac01f26 | 2018-01-08 10:59:43 +0200 | [diff] [blame] | 2599 | if (ret) |
| 2600 | return ret; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2601 | } |
| 2602 | return 0; |
| 2603 | } |
| 2604 | |
Filipe Manana | c334730 | 2020-11-04 11:07:31 +0000 | [diff] [blame] | 2605 | static int btrfs_find_new_delalloc_bytes(struct btrfs_inode *inode, |
| 2606 | const u64 start, |
| 2607 | const u64 len, |
| 2608 | struct extent_state **cached_state) |
| 2609 | { |
| 2610 | u64 search_start = start; |
| 2611 | const u64 end = start + len - 1; |
| 2612 | |
| 2613 | while (search_start < end) { |
| 2614 | const u64 search_len = end - search_start + 1; |
| 2615 | struct extent_map *em; |
| 2616 | u64 em_len; |
| 2617 | int ret = 0; |
| 2618 | |
| 2619 | em = btrfs_get_extent(inode, NULL, 0, search_start, search_len); |
| 2620 | if (IS_ERR(em)) |
| 2621 | return PTR_ERR(em); |
| 2622 | |
| 2623 | if (em->block_start != EXTENT_MAP_HOLE) |
| 2624 | goto next; |
| 2625 | |
| 2626 | em_len = em->len; |
| 2627 | if (em->start < search_start) |
| 2628 | em_len -= search_start - em->start; |
| 2629 | if (em_len > search_len) |
| 2630 | em_len = search_len; |
| 2631 | |
| 2632 | ret = set_extent_bit(&inode->io_tree, search_start, |
| 2633 | search_start + em_len - 1, |
Nikolay Borisov | 1cab5e7 | 2020-11-05 11:08:00 +0200 | [diff] [blame] | 2634 | EXTENT_DELALLOC_NEW, 0, NULL, cached_state, |
| 2635 | GFP_NOFS, NULL); |
Filipe Manana | c334730 | 2020-11-04 11:07:31 +0000 | [diff] [blame] | 2636 | next: |
| 2637 | search_start = extent_map_end(em); |
| 2638 | free_extent_map(em); |
| 2639 | if (ret) |
| 2640 | return ret; |
| 2641 | } |
| 2642 | return 0; |
| 2643 | } |
| 2644 | |
Nikolay Borisov | c2566f2 | 2020-06-03 08:55:35 +0300 | [diff] [blame] | 2645 | 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] | 2646 | unsigned int extra_bits, |
Nikolay Borisov | 330a582 | 2019-07-17 16:18:17 +0300 | [diff] [blame] | 2647 | struct extent_state **cached_state) |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 2648 | { |
Johannes Thumshirn | fdb1e12 | 2018-12-05 15:23:04 +0100 | [diff] [blame] | 2649 | WARN_ON(PAGE_ALIGNED(end)); |
Filipe Manana | c334730 | 2020-11-04 11:07:31 +0000 | [diff] [blame] | 2650 | |
| 2651 | if (start >= i_size_read(&inode->vfs_inode) && |
| 2652 | !(inode->flags & BTRFS_INODE_PREALLOC)) { |
| 2653 | /* |
| 2654 | * There can't be any extents following eof in this case so just |
| 2655 | * set the delalloc new bit for the range directly. |
| 2656 | */ |
| 2657 | extra_bits |= EXTENT_DELALLOC_NEW; |
| 2658 | } else { |
| 2659 | int ret; |
| 2660 | |
| 2661 | ret = btrfs_find_new_delalloc_bytes(inode, start, |
| 2662 | end + 1 - start, |
| 2663 | cached_state); |
| 2664 | if (ret) |
| 2665 | return ret; |
| 2666 | } |
| 2667 | |
Nikolay Borisov | c2566f2 | 2020-06-03 08:55:35 +0300 | [diff] [blame] | 2668 | return set_extent_delalloc(&inode->io_tree, start, end, extra_bits, |
| 2669 | cached_state); |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 2670 | } |
| 2671 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2672 | /* see btrfs_writepage_start_hook for details on why this is required */ |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2673 | struct btrfs_writepage_fixup { |
| 2674 | struct page *page; |
Josef Bacik | f4b1363 | 2020-01-21 14:34:52 -0500 | [diff] [blame] | 2675 | struct inode *inode; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2676 | struct btrfs_work work; |
| 2677 | }; |
| 2678 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 2679 | static void btrfs_writepage_fixup_worker(struct btrfs_work *work) |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2680 | { |
| 2681 | struct btrfs_writepage_fixup *fixup; |
| 2682 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 2683 | struct extent_state *cached_state = NULL; |
Qu Wenruo | 364ecf3 | 2017-02-27 15:10:38 +0800 | [diff] [blame] | 2684 | struct extent_changeset *data_reserved = NULL; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2685 | struct page *page; |
Nikolay Borisov | 65d87f7 | 2020-06-05 10:51:51 +0300 | [diff] [blame] | 2686 | struct btrfs_inode *inode; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2687 | u64 page_start; |
| 2688 | u64 page_end; |
Chris Mason | 25f3c50 | 2020-01-21 11:51:42 -0500 | [diff] [blame] | 2689 | int ret = 0; |
Josef Bacik | f4b1363 | 2020-01-21 14:34:52 -0500 | [diff] [blame] | 2690 | bool free_delalloc_space = true; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2691 | |
| 2692 | fixup = container_of(work, struct btrfs_writepage_fixup, work); |
| 2693 | page = fixup->page; |
Nikolay Borisov | 65d87f7 | 2020-06-05 10:51:51 +0300 | [diff] [blame] | 2694 | inode = BTRFS_I(fixup->inode); |
Josef Bacik | f4b1363 | 2020-01-21 14:34:52 -0500 | [diff] [blame] | 2695 | page_start = page_offset(page); |
| 2696 | page_end = page_offset(page) + PAGE_SIZE - 1; |
| 2697 | |
| 2698 | /* |
| 2699 | * This is similar to page_mkwrite, we need to reserve the space before |
| 2700 | * we take the page lock. |
| 2701 | */ |
Nikolay Borisov | 65d87f7 | 2020-06-05 10:51:51 +0300 | [diff] [blame] | 2702 | ret = btrfs_delalloc_reserve_space(inode, &data_reserved, page_start, |
| 2703 | PAGE_SIZE); |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 2704 | again: |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2705 | lock_page(page); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2706 | |
Chris Mason | 25f3c50 | 2020-01-21 11:51:42 -0500 | [diff] [blame] | 2707 | /* |
| 2708 | * Before we queued this fixup, we took a reference on the page. |
| 2709 | * page->mapping may go NULL, but it shouldn't be moved to a different |
| 2710 | * address space. |
| 2711 | */ |
Josef Bacik | f4b1363 | 2020-01-21 14:34:52 -0500 | [diff] [blame] | 2712 | if (!page->mapping || !PageDirty(page) || !PageChecked(page)) { |
| 2713 | /* |
| 2714 | * Unfortunately this is a little tricky, either |
| 2715 | * |
| 2716 | * 1) We got here and our page had already been dealt with and |
| 2717 | * we reserved our space, thus ret == 0, so we need to just |
| 2718 | * drop our space reservation and bail. This can happen the |
| 2719 | * first time we come into the fixup worker, or could happen |
| 2720 | * while waiting for the ordered extent. |
| 2721 | * 2) Our page was already dealt with, but we happened to get an |
| 2722 | * ENOSPC above from the btrfs_delalloc_reserve_space. In |
| 2723 | * this case we obviously don't have anything to release, but |
| 2724 | * because the page was already dealt with we don't want to |
| 2725 | * mark the page with an error, so make sure we're resetting |
| 2726 | * ret to 0. This is why we have this check _before_ the ret |
| 2727 | * check, because we do not want to have a surprise ENOSPC |
| 2728 | * when the page was already properly dealt with. |
| 2729 | */ |
| 2730 | if (!ret) { |
Nikolay Borisov | 65d87f7 | 2020-06-05 10:51:51 +0300 | [diff] [blame] | 2731 | btrfs_delalloc_release_extents(inode, PAGE_SIZE); |
| 2732 | btrfs_delalloc_release_space(inode, data_reserved, |
Josef Bacik | f4b1363 | 2020-01-21 14:34:52 -0500 | [diff] [blame] | 2733 | page_start, PAGE_SIZE, |
| 2734 | true); |
| 2735 | } |
| 2736 | ret = 0; |
Chris Mason | 25f3c50 | 2020-01-21 11:51:42 -0500 | [diff] [blame] | 2737 | goto out_page; |
Josef Bacik | f4b1363 | 2020-01-21 14:34:52 -0500 | [diff] [blame] | 2738 | } |
Chris Mason | 25f3c50 | 2020-01-21 11:51:42 -0500 | [diff] [blame] | 2739 | |
| 2740 | /* |
Josef Bacik | f4b1363 | 2020-01-21 14:34:52 -0500 | [diff] [blame] | 2741 | * We can't mess with the page state unless it is locked, so now that |
| 2742 | * it is locked bail if we failed to make our space reservation. |
Chris Mason | 25f3c50 | 2020-01-21 11:51:42 -0500 | [diff] [blame] | 2743 | */ |
Josef Bacik | f4b1363 | 2020-01-21 14:34:52 -0500 | [diff] [blame] | 2744 | if (ret) |
| 2745 | goto out_page; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2746 | |
Nikolay Borisov | 65d87f7 | 2020-06-05 10:51:51 +0300 | [diff] [blame] | 2747 | lock_extent_bits(&inode->io_tree, page_start, page_end, &cached_state); |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 2748 | |
| 2749 | /* already ordered? We're done */ |
Qu Wenruo | f57ad93 | 2021-04-07 19:22:13 +0800 | [diff] [blame] | 2750 | if (PageOrdered(page)) |
Josef Bacik | f4b1363 | 2020-01-21 14:34:52 -0500 | [diff] [blame] | 2751 | goto out_reserved; |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 2752 | |
Nikolay Borisov | 65d87f7 | 2020-06-05 10:51:51 +0300 | [diff] [blame] | 2753 | ordered = btrfs_lookup_ordered_range(inode, page_start, PAGE_SIZE); |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 2754 | if (ordered) { |
Nikolay Borisov | 65d87f7 | 2020-06-05 10:51:51 +0300 | [diff] [blame] | 2755 | unlock_extent_cached(&inode->io_tree, page_start, page_end, |
| 2756 | &cached_state); |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 2757 | unlock_page(page); |
Nikolay Borisov | c0a4360 | 2020-09-18 12:15:53 +0300 | [diff] [blame] | 2758 | btrfs_start_ordered_extent(ordered, 1); |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 2759 | btrfs_put_ordered_extent(ordered); |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 2760 | goto again; |
| 2761 | } |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2762 | |
Nikolay Borisov | 65d87f7 | 2020-06-05 10:51:51 +0300 | [diff] [blame] | 2763 | ret = btrfs_set_extent_delalloc(inode, page_start, page_end, 0, |
Nikolay Borisov | 330a582 | 2019-07-17 16:18:17 +0300 | [diff] [blame] | 2764 | &cached_state); |
Chris Mason | 25f3c50 | 2020-01-21 11:51:42 -0500 | [diff] [blame] | 2765 | if (ret) |
Filipe Manana | 5368700 | 2019-10-09 17:43:59 +0100 | [diff] [blame] | 2766 | goto out_reserved; |
Nikolay Borisov | f3038ee | 2017-12-05 09:29:19 +0200 | [diff] [blame] | 2767 | |
Chris Mason | 25f3c50 | 2020-01-21 11:51:42 -0500 | [diff] [blame] | 2768 | /* |
| 2769 | * Everything went as planned, we're now the owner of a dirty page with |
| 2770 | * delayed allocation bits set and space reserved for our COW |
| 2771 | * destination. |
| 2772 | * |
| 2773 | * The page was dirty when we started, nothing should have cleaned it. |
| 2774 | */ |
| 2775 | BUG_ON(!PageDirty(page)); |
Josef Bacik | f4b1363 | 2020-01-21 14:34:52 -0500 | [diff] [blame] | 2776 | free_delalloc_space = false; |
Filipe Manana | 5368700 | 2019-10-09 17:43:59 +0100 | [diff] [blame] | 2777 | out_reserved: |
Nikolay Borisov | 65d87f7 | 2020-06-05 10:51:51 +0300 | [diff] [blame] | 2778 | btrfs_delalloc_release_extents(inode, PAGE_SIZE); |
Josef Bacik | f4b1363 | 2020-01-21 14:34:52 -0500 | [diff] [blame] | 2779 | if (free_delalloc_space) |
Nikolay Borisov | 65d87f7 | 2020-06-05 10:51:51 +0300 | [diff] [blame] | 2780 | btrfs_delalloc_release_space(inode, data_reserved, page_start, |
| 2781 | PAGE_SIZE, true); |
| 2782 | unlock_extent_cached(&inode->io_tree, page_start, page_end, |
David Sterba | e43bbe5 | 2017-12-12 21:43:52 +0100 | [diff] [blame] | 2783 | &cached_state); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2784 | out_page: |
Chris Mason | 25f3c50 | 2020-01-21 11:51:42 -0500 | [diff] [blame] | 2785 | if (ret) { |
| 2786 | /* |
| 2787 | * We hit ENOSPC or other errors. Update the mapping and page |
| 2788 | * to reflect the errors and clean the page. |
| 2789 | */ |
| 2790 | mapping_set_error(page->mapping, ret); |
| 2791 | end_extent_writepage(page, ret, page_start, page_end); |
| 2792 | clear_page_dirty_for_io(page); |
| 2793 | SetPageError(page); |
| 2794 | } |
Qu Wenruo | e4f9434 | 2021-09-27 15:21:49 +0800 | [diff] [blame] | 2795 | btrfs_page_clear_checked(inode->root->fs_info, page, page_start, PAGE_SIZE); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2796 | unlock_page(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2797 | put_page(page); |
Miao Xie | b897abe | 2011-01-26 16:19:22 +0800 | [diff] [blame] | 2798 | kfree(fixup); |
Qu Wenruo | 364ecf3 | 2017-02-27 15:10:38 +0800 | [diff] [blame] | 2799 | extent_changeset_free(data_reserved); |
Josef Bacik | f4b1363 | 2020-01-21 14:34:52 -0500 | [diff] [blame] | 2800 | /* |
| 2801 | * As a precaution, do a delayed iput in case it would be the last iput |
| 2802 | * that could need flushing space. Recursing back to fixup worker would |
| 2803 | * deadlock. |
| 2804 | */ |
Nikolay Borisov | 65d87f7 | 2020-06-05 10:51:51 +0300 | [diff] [blame] | 2805 | btrfs_add_delayed_iput(&inode->vfs_inode); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2806 | } |
| 2807 | |
| 2808 | /* |
| 2809 | * There are a few paths in the higher layers of the kernel that directly |
| 2810 | * set the page dirty bit without asking the filesystem if it is a |
| 2811 | * good idea. This causes problems because we want to make sure COW |
| 2812 | * properly happens and the data=ordered rules are followed. |
| 2813 | * |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2814 | * 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] | 2815 | * hasn't been properly setup for IO. We kick off an async process |
| 2816 | * to fix it up. The async helper will wait for ordered extents, set |
| 2817 | * the delalloc bit and make it safe to write the page. |
| 2818 | */ |
Qu Wenruo | a129ffb | 2021-07-27 13:41:32 +0800 | [diff] [blame] | 2819 | int btrfs_writepage_cow_fixup(struct page *page) |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2820 | { |
| 2821 | struct inode *inode = page->mapping->host; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2822 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2823 | struct btrfs_writepage_fixup *fixup; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2824 | |
Qu Wenruo | f57ad93 | 2021-04-07 19:22:13 +0800 | [diff] [blame] | 2825 | /* This page has ordered extent covering it already */ |
| 2826 | if (PageOrdered(page)) |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2827 | return 0; |
| 2828 | |
Chris Mason | 25f3c50 | 2020-01-21 11:51:42 -0500 | [diff] [blame] | 2829 | /* |
| 2830 | * PageChecked is set below when we create a fixup worker for this page, |
| 2831 | * don't try to create another one if we're already PageChecked() |
| 2832 | * |
| 2833 | * The extent_io writepage code will redirty the page if we send back |
| 2834 | * EAGAIN. |
| 2835 | */ |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2836 | if (PageChecked(page)) |
| 2837 | return -EAGAIN; |
| 2838 | |
| 2839 | fixup = kzalloc(sizeof(*fixup), GFP_NOFS); |
| 2840 | if (!fixup) |
| 2841 | return -EAGAIN; |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 2842 | |
Josef Bacik | f4b1363 | 2020-01-21 14:34:52 -0500 | [diff] [blame] | 2843 | /* |
| 2844 | * We are already holding a reference to this inode from |
| 2845 | * write_cache_pages. We need to hold it because the space reservation |
| 2846 | * takes place outside of the page lock, and we can't trust |
| 2847 | * page->mapping outside of the page lock. |
| 2848 | */ |
| 2849 | ihold(inode); |
Qu Wenruo | e4f9434 | 2021-09-27 15:21:49 +0800 | [diff] [blame] | 2850 | btrfs_page_set_checked(fs_info, page, page_offset(page), PAGE_SIZE); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2851 | get_page(page); |
Omar Sandoval | a0cac0e | 2019-09-16 11:30:57 -0700 | [diff] [blame] | 2852 | btrfs_init_work(&fixup->work, btrfs_writepage_fixup_worker, NULL, NULL); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2853 | fixup->page = page; |
Josef Bacik | f4b1363 | 2020-01-21 14:34:52 -0500 | [diff] [blame] | 2854 | fixup->inode = inode; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2855 | btrfs_queue_work(fs_info->fixup_workers, &fixup->work); |
Chris Mason | 25f3c50 | 2020-01-21 11:51:42 -0500 | [diff] [blame] | 2856 | |
| 2857 | return -EAGAIN; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2858 | } |
| 2859 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2860 | static int insert_reserved_file_extent(struct btrfs_trans_handle *trans, |
Nikolay Borisov | c553f94 | 2020-06-03 08:55:19 +0300 | [diff] [blame] | 2861 | struct btrfs_inode *inode, u64 file_pos, |
Qu Wenruo | 9729f10 | 2020-06-10 09:04:41 +0800 | [diff] [blame] | 2862 | struct btrfs_file_extent_item *stack_fi, |
Filipe Manana | 2766ff6 | 2020-11-04 11:07:34 +0000 | [diff] [blame] | 2863 | const bool update_inode_bytes, |
Qu Wenruo | 9729f10 | 2020-06-10 09:04:41 +0800 | [diff] [blame] | 2864 | u64 qgroup_reserved) |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2865 | { |
Nikolay Borisov | c553f94 | 2020-06-03 08:55:19 +0300 | [diff] [blame] | 2866 | struct btrfs_root *root = inode->root; |
Filipe Manana | 2766ff6 | 2020-11-04 11:07:34 +0000 | [diff] [blame] | 2867 | const u64 sectorsize = root->fs_info->sectorsize; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2868 | struct btrfs_path *path; |
| 2869 | struct extent_buffer *leaf; |
| 2870 | struct btrfs_key ins; |
Qu Wenruo | 203f44c5 | 2020-06-10 09:04:40 +0800 | [diff] [blame] | 2871 | u64 disk_num_bytes = btrfs_stack_file_extent_disk_num_bytes(stack_fi); |
| 2872 | u64 disk_bytenr = btrfs_stack_file_extent_disk_bytenr(stack_fi); |
| 2873 | u64 num_bytes = btrfs_stack_file_extent_num_bytes(stack_fi); |
| 2874 | u64 ram_bytes = btrfs_stack_file_extent_ram_bytes(stack_fi); |
Filipe Manana | 5893dfb | 2020-11-04 11:07:32 +0000 | [diff] [blame] | 2875 | struct btrfs_drop_extents_args drop_args = { 0 }; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2876 | int ret; |
| 2877 | |
| 2878 | path = btrfs_alloc_path(); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 2879 | if (!path) |
| 2880 | return -ENOMEM; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2881 | |
Chris Mason | a1ed835 | 2009-09-11 12:27:37 -0400 | [diff] [blame] | 2882 | /* |
| 2883 | * we may be replacing one extent in the tree with another. |
| 2884 | * The new extent is pinned in the extent map, and we don't want |
| 2885 | * to drop it from the cache until it is completely in the btree. |
| 2886 | * |
| 2887 | * So, tell btrfs_drop_extents to leave this extent in the cache. |
| 2888 | * the caller is expected to unpin it and allow it to be merged |
| 2889 | * with the others. |
| 2890 | */ |
Filipe Manana | 5893dfb | 2020-11-04 11:07:32 +0000 | [diff] [blame] | 2891 | drop_args.path = path; |
| 2892 | drop_args.start = file_pos; |
| 2893 | drop_args.end = file_pos + num_bytes; |
| 2894 | drop_args.replace_extent = true; |
| 2895 | drop_args.extent_item_size = sizeof(*stack_fi); |
| 2896 | ret = btrfs_drop_extents(trans, root, inode, &drop_args); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2897 | if (ret) |
| 2898 | goto out; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2899 | |
Filipe Manana | 5893dfb | 2020-11-04 11:07:32 +0000 | [diff] [blame] | 2900 | if (!drop_args.extent_inserted) { |
Nikolay Borisov | c553f94 | 2020-06-03 08:55:19 +0300 | [diff] [blame] | 2901 | ins.objectid = btrfs_ino(inode); |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 2902 | ins.offset = file_pos; |
| 2903 | ins.type = BTRFS_EXTENT_DATA_KEY; |
| 2904 | |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 2905 | ret = btrfs_insert_empty_item(trans, root, path, &ins, |
Qu Wenruo | 203f44c5 | 2020-06-10 09:04:40 +0800 | [diff] [blame] | 2906 | sizeof(*stack_fi)); |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 2907 | if (ret) |
| 2908 | goto out; |
| 2909 | } |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2910 | leaf = path->nodes[0]; |
Qu Wenruo | 203f44c5 | 2020-06-10 09:04:40 +0800 | [diff] [blame] | 2911 | btrfs_set_stack_file_extent_generation(stack_fi, trans->transid); |
| 2912 | write_extent_buffer(leaf, stack_fi, |
| 2913 | btrfs_item_ptr_offset(leaf, path->slots[0]), |
| 2914 | sizeof(struct btrfs_file_extent_item)); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 2915 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2916 | btrfs_mark_buffer_dirty(leaf); |
Josef Bacik | ce19533 | 2012-09-25 15:26:16 -0400 | [diff] [blame] | 2917 | btrfs_release_path(path); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2918 | |
Filipe Manana | 2766ff6 | 2020-11-04 11:07:34 +0000 | [diff] [blame] | 2919 | /* |
| 2920 | * If we dropped an inline extent here, we know the range where it is |
| 2921 | * 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] | 2922 | * number of bytes only for that range containing the inline extent. |
Filipe Manana | 2766ff6 | 2020-11-04 11:07:34 +0000 | [diff] [blame] | 2923 | * The remaining of the range will be processed when clearning the |
| 2924 | * EXTENT_DELALLOC_BIT bit through the ordered extent completion. |
| 2925 | */ |
| 2926 | if (file_pos == 0 && !IS_ALIGNED(drop_args.bytes_found, sectorsize)) { |
| 2927 | u64 inline_size = round_down(drop_args.bytes_found, sectorsize); |
| 2928 | |
| 2929 | inline_size = drop_args.bytes_found - inline_size; |
| 2930 | btrfs_update_inode_bytes(inode, sectorsize, inline_size); |
| 2931 | drop_args.bytes_found -= inline_size; |
| 2932 | num_bytes -= sectorsize; |
| 2933 | } |
| 2934 | |
| 2935 | if (update_inode_bytes) |
| 2936 | btrfs_update_inode_bytes(inode, num_bytes, drop_args.bytes_found); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2937 | |
| 2938 | ins.objectid = disk_bytenr; |
| 2939 | ins.offset = disk_num_bytes; |
| 2940 | ins.type = BTRFS_EXTENT_ITEM_KEY; |
Qu Wenruo | a12b877 | 2017-02-27 15:10:37 +0800 | [diff] [blame] | 2941 | |
Nikolay Borisov | c553f94 | 2020-06-03 08:55:19 +0300 | [diff] [blame] | 2942 | ret = btrfs_inode_set_file_extent_range(inode, file_pos, ram_bytes); |
Josef Bacik | 9ddc959 | 2020-01-17 09:02:22 -0500 | [diff] [blame] | 2943 | if (ret) |
| 2944 | goto out; |
| 2945 | |
Nikolay Borisov | c553f94 | 2020-06-03 08:55:19 +0300 | [diff] [blame] | 2946 | ret = btrfs_alloc_reserved_file_extent(trans, root, btrfs_ino(inode), |
Qu Wenruo | 9729f10 | 2020-06-10 09:04:41 +0800 | [diff] [blame] | 2947 | file_pos, qgroup_reserved, &ins); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2948 | out: |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2949 | btrfs_free_path(path); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 2950 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2951 | return ret; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2952 | } |
| 2953 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2954 | static void btrfs_release_delalloc_bytes(struct btrfs_fs_info *fs_info, |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 2955 | u64 start, u64 len) |
| 2956 | { |
David Sterba | 32da5386 | 2019-10-29 19:20:18 +0100 | [diff] [blame] | 2957 | struct btrfs_block_group *cache; |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 2958 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2959 | cache = btrfs_lookup_block_group(fs_info, start); |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 2960 | ASSERT(cache); |
| 2961 | |
| 2962 | spin_lock(&cache->lock); |
| 2963 | cache->delalloc_bytes -= len; |
| 2964 | spin_unlock(&cache->lock); |
| 2965 | |
| 2966 | btrfs_put_block_group(cache); |
| 2967 | } |
| 2968 | |
Qu Wenruo | 203f44c5 | 2020-06-10 09:04:40 +0800 | [diff] [blame] | 2969 | static int insert_ordered_extent_file_extent(struct btrfs_trans_handle *trans, |
Qu Wenruo | 203f44c5 | 2020-06-10 09:04:40 +0800 | [diff] [blame] | 2970 | struct btrfs_ordered_extent *oe) |
| 2971 | { |
| 2972 | struct btrfs_file_extent_item stack_fi; |
| 2973 | u64 logical_len; |
Filipe Manana | 2766ff6 | 2020-11-04 11:07:34 +0000 | [diff] [blame] | 2974 | bool update_inode_bytes; |
Qu Wenruo | 203f44c5 | 2020-06-10 09:04:40 +0800 | [diff] [blame] | 2975 | |
| 2976 | memset(&stack_fi, 0, sizeof(stack_fi)); |
| 2977 | btrfs_set_stack_file_extent_type(&stack_fi, BTRFS_FILE_EXTENT_REG); |
| 2978 | btrfs_set_stack_file_extent_disk_bytenr(&stack_fi, oe->disk_bytenr); |
| 2979 | btrfs_set_stack_file_extent_disk_num_bytes(&stack_fi, |
| 2980 | oe->disk_num_bytes); |
| 2981 | if (test_bit(BTRFS_ORDERED_TRUNCATED, &oe->flags)) |
| 2982 | logical_len = oe->truncated_len; |
| 2983 | else |
| 2984 | logical_len = oe->num_bytes; |
| 2985 | btrfs_set_stack_file_extent_num_bytes(&stack_fi, logical_len); |
| 2986 | btrfs_set_stack_file_extent_ram_bytes(&stack_fi, logical_len); |
| 2987 | btrfs_set_stack_file_extent_compression(&stack_fi, oe->compress_type); |
| 2988 | /* Encryption and other encoding is reserved and all 0 */ |
| 2989 | |
Filipe Manana | 2766ff6 | 2020-11-04 11:07:34 +0000 | [diff] [blame] | 2990 | /* |
| 2991 | * For delalloc, when completing an ordered extent we update the inode's |
| 2992 | * bytes when clearing the range in the inode's io tree, so pass false |
| 2993 | * as the argument 'update_inode_bytes' to insert_reserved_file_extent(), |
| 2994 | * except if the ordered extent was truncated. |
| 2995 | */ |
| 2996 | update_inode_bytes = test_bit(BTRFS_ORDERED_DIRECT, &oe->flags) || |
| 2997 | test_bit(BTRFS_ORDERED_TRUNCATED, &oe->flags); |
| 2998 | |
Nikolay Borisov | 3c38c87 | 2020-09-18 12:15:51 +0300 | [diff] [blame] | 2999 | return insert_reserved_file_extent(trans, BTRFS_I(oe->inode), |
| 3000 | oe->file_offset, &stack_fi, |
Filipe Manana | 2766ff6 | 2020-11-04 11:07:34 +0000 | [diff] [blame] | 3001 | update_inode_bytes, oe->qgroup_rsv); |
Qu Wenruo | 203f44c5 | 2020-06-10 09:04:40 +0800 | [diff] [blame] | 3002 | } |
| 3003 | |
| 3004 | /* |
| 3005 | * As ordered data IO finishes, this gets called so we can finish |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3006 | * an ordered extent if the range of bytes in the file it covers are |
| 3007 | * fully written. |
| 3008 | */ |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 3009 | static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3010 | { |
Nikolay Borisov | 72e7e6e | 2020-11-02 16:48:56 +0200 | [diff] [blame] | 3011 | struct btrfs_inode *inode = BTRFS_I(ordered_extent->inode); |
| 3012 | struct btrfs_root *root = inode->root; |
| 3013 | struct btrfs_fs_info *fs_info = root->fs_info; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 3014 | struct btrfs_trans_handle *trans = NULL; |
Nikolay Borisov | 72e7e6e | 2020-11-02 16:48:56 +0200 | [diff] [blame] | 3015 | struct extent_io_tree *io_tree = &inode->io_tree; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 3016 | struct extent_state *cached_state = NULL; |
Omar Sandoval | bffe633 | 2019-12-02 17:34:19 -0800 | [diff] [blame] | 3017 | u64 start, end; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 3018 | int compress_type = 0; |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 3019 | int ret = 0; |
Omar Sandoval | bffe633 | 2019-12-02 17:34:19 -0800 | [diff] [blame] | 3020 | u64 logical_len = ordered_extent->num_bytes; |
Nikolay Borisov | 8d51012 | 2019-10-08 20:43:06 +0300 | [diff] [blame] | 3021 | bool freespace_inode; |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 3022 | bool truncated = false; |
Josef Bacik | 49940bd | 2018-10-11 15:54:21 -0400 | [diff] [blame] | 3023 | bool clear_reserved_extent = true; |
Filipe Manana | 2766ff6 | 2020-11-04 11:07:34 +0000 | [diff] [blame] | 3024 | unsigned int clear_bits = EXTENT_DEFRAG; |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 3025 | |
Omar Sandoval | bffe633 | 2019-12-02 17:34:19 -0800 | [diff] [blame] | 3026 | start = ordered_extent->file_offset; |
| 3027 | end = start + ordered_extent->num_bytes - 1; |
| 3028 | |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 3029 | if (!test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) && |
| 3030 | !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags) && |
| 3031 | !test_bit(BTRFS_ORDERED_DIRECT, &ordered_extent->flags)) |
Filipe Manana | 2766ff6 | 2020-11-04 11:07:34 +0000 | [diff] [blame] | 3032 | clear_bits |= EXTENT_DELALLOC_NEW; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3033 | |
Nikolay Borisov | 72e7e6e | 2020-11-02 16:48:56 +0200 | [diff] [blame] | 3034 | freespace_inode = btrfs_is_free_space_inode(inode); |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 3035 | |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 3036 | if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) { |
| 3037 | ret = -EIO; |
| 3038 | goto out; |
| 3039 | } |
| 3040 | |
Naohiro Aota | be1a1d7 | 2021-08-19 21:19:23 +0900 | [diff] [blame] | 3041 | /* A valid bdev implies a write on a sequential zone */ |
| 3042 | if (ordered_extent->bdev) { |
Naohiro Aota | d8e3fb1 | 2021-02-04 19:22:05 +0900 | [diff] [blame] | 3043 | btrfs_rewrite_logical_zoned(ordered_extent); |
Naohiro Aota | be1a1d7 | 2021-08-19 21:19:23 +0900 | [diff] [blame] | 3044 | btrfs_zone_finish_endio(fs_info, ordered_extent->disk_bytenr, |
| 3045 | ordered_extent->disk_num_bytes); |
| 3046 | } |
Naohiro Aota | d8e3fb1 | 2021-02-04 19:22:05 +0900 | [diff] [blame] | 3047 | |
Nikolay Borisov | 72e7e6e | 2020-11-02 16:48:56 +0200 | [diff] [blame] | 3048 | btrfs_free_io_failure_record(inode, start, end); |
Miao Xie | f612496 | 2014-09-12 18:44:04 +0800 | [diff] [blame] | 3049 | |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 3050 | if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) { |
| 3051 | truncated = true; |
| 3052 | logical_len = ordered_extent->truncated_len; |
| 3053 | /* Truncated the entire extent, don't bother adding */ |
| 3054 | if (!logical_len) |
| 3055 | goto out; |
| 3056 | } |
| 3057 | |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 3058 | if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) { |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3059 | BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */ |
Qu Wenruo | 94ed938 | 2015-09-08 17:25:56 +0800 | [diff] [blame] | 3060 | |
Nikolay Borisov | 72e7e6e | 2020-11-02 16:48:56 +0200 | [diff] [blame] | 3061 | btrfs_inode_safe_disk_i_size_write(inode, 0); |
Nikolay Borisov | 8d51012 | 2019-10-08 20:43:06 +0300 | [diff] [blame] | 3062 | if (freespace_inode) |
| 3063 | trans = btrfs_join_transaction_spacecache(root); |
Josef Bacik | 6c760c0 | 2012-11-09 10:53:21 -0500 | [diff] [blame] | 3064 | else |
| 3065 | trans = btrfs_join_transaction(root); |
| 3066 | if (IS_ERR(trans)) { |
| 3067 | ret = PTR_ERR(trans); |
| 3068 | trans = NULL; |
| 3069 | goto out; |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 3070 | } |
Nikolay Borisov | 72e7e6e | 2020-11-02 16:48:56 +0200 | [diff] [blame] | 3071 | trans->block_rsv = &inode->block_rsv; |
Nikolay Borisov | 729f796 | 2020-11-02 16:49:06 +0200 | [diff] [blame] | 3072 | ret = btrfs_update_inode_fallback(trans, root, inode); |
Josef Bacik | 6c760c0 | 2012-11-09 10:53:21 -0500 | [diff] [blame] | 3073 | if (ret) /* -ENOMEM or corruption */ |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3074 | btrfs_abort_transaction(trans, ret); |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 3075 | goto out; |
| 3076 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3077 | |
Filipe Manana | 2766ff6 | 2020-11-04 11:07:34 +0000 | [diff] [blame] | 3078 | clear_bits |= EXTENT_LOCKED; |
Omar Sandoval | bffe633 | 2019-12-02 17:34:19 -0800 | [diff] [blame] | 3079 | lock_extent_bits(io_tree, start, end, &cached_state); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3080 | |
Nikolay Borisov | 8d51012 | 2019-10-08 20:43:06 +0300 | [diff] [blame] | 3081 | if (freespace_inode) |
| 3082 | trans = btrfs_join_transaction_spacecache(root); |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 3083 | else |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 3084 | trans = btrfs_join_transaction(root); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3085 | if (IS_ERR(trans)) { |
| 3086 | ret = PTR_ERR(trans); |
| 3087 | trans = NULL; |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 3088 | goto out; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3089 | } |
Chris Mason | a79b7d4 | 2014-05-22 16:18:52 -0700 | [diff] [blame] | 3090 | |
Nikolay Borisov | 72e7e6e | 2020-11-02 16:48:56 +0200 | [diff] [blame] | 3091 | trans->block_rsv = &inode->block_rsv; |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 3092 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 3093 | if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags)) |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 3094 | compress_type = ordered_extent->compress_type; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 3095 | if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) { |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 3096 | BUG_ON(compress_type); |
Nikolay Borisov | 72e7e6e | 2020-11-02 16:48:56 +0200 | [diff] [blame] | 3097 | ret = btrfs_mark_extent_written(trans, inode, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 3098 | ordered_extent->file_offset, |
| 3099 | ordered_extent->file_offset + |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 3100 | logical_len); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 3101 | } else { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3102 | BUG_ON(root == fs_info->tree_root); |
Nikolay Borisov | 3c38c87 | 2020-09-18 12:15:51 +0300 | [diff] [blame] | 3103 | ret = insert_ordered_extent_file_extent(trans, ordered_extent); |
Josef Bacik | 49940bd | 2018-10-11 15:54:21 -0400 | [diff] [blame] | 3104 | if (!ret) { |
| 3105 | clear_reserved_extent = false; |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3106 | btrfs_release_delalloc_bytes(fs_info, |
Omar Sandoval | bffe633 | 2019-12-02 17:34:19 -0800 | [diff] [blame] | 3107 | ordered_extent->disk_bytenr, |
| 3108 | ordered_extent->disk_num_bytes); |
Josef Bacik | 49940bd | 2018-10-11 15:54:21 -0400 | [diff] [blame] | 3109 | } |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 3110 | } |
Nikolay Borisov | 72e7e6e | 2020-11-02 16:48:56 +0200 | [diff] [blame] | 3111 | unpin_extent_cache(&inode->extent_tree, ordered_extent->file_offset, |
Omar Sandoval | bffe633 | 2019-12-02 17:34:19 -0800 | [diff] [blame] | 3112 | ordered_extent->num_bytes, trans->transid); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3113 | if (ret < 0) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3114 | btrfs_abort_transaction(trans, ret); |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 3115 | goto out; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3116 | } |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 3117 | |
Nikolay Borisov | 510f85e | 2020-09-18 12:15:52 +0300 | [diff] [blame] | 3118 | ret = add_pending_csums(trans, &ordered_extent->list); |
Nikolay Borisov | ac01f26 | 2018-01-08 10:59:43 +0200 | [diff] [blame] | 3119 | if (ret) { |
| 3120 | btrfs_abort_transaction(trans, ret); |
| 3121 | goto out; |
| 3122 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3123 | |
Filipe Manana | 2766ff6 | 2020-11-04 11:07:34 +0000 | [diff] [blame] | 3124 | /* |
| 3125 | * If this is a new delalloc range, clear its new delalloc flag to |
| 3126 | * update the inode's number of bytes. This needs to be done first |
| 3127 | * before updating the inode item. |
| 3128 | */ |
| 3129 | if ((clear_bits & EXTENT_DELALLOC_NEW) && |
| 3130 | !test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) |
Nikolay Borisov | 72e7e6e | 2020-11-02 16:48:56 +0200 | [diff] [blame] | 3131 | clear_extent_bit(&inode->io_tree, start, end, |
Filipe Manana | 2766ff6 | 2020-11-04 11:07:34 +0000 | [diff] [blame] | 3132 | EXTENT_DELALLOC_NEW | EXTENT_ADD_INODE_BYTES, |
| 3133 | 0, 0, &cached_state); |
| 3134 | |
Nikolay Borisov | 72e7e6e | 2020-11-02 16:48:56 +0200 | [diff] [blame] | 3135 | btrfs_inode_safe_disk_i_size_write(inode, 0); |
Nikolay Borisov | 729f796 | 2020-11-02 16:49:06 +0200 | [diff] [blame] | 3136 | ret = btrfs_update_inode_fallback(trans, root, inode); |
Josef Bacik | 6c760c0 | 2012-11-09 10:53:21 -0500 | [diff] [blame] | 3137 | if (ret) { /* -ENOMEM or corruption */ |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3138 | btrfs_abort_transaction(trans, ret); |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 3139 | goto out; |
Josef Bacik | 1ef30be | 2011-04-05 19:25:36 -0400 | [diff] [blame] | 3140 | } |
| 3141 | ret = 0; |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 3142 | out: |
Nikolay Borisov | 72e7e6e | 2020-11-02 16:48:56 +0200 | [diff] [blame] | 3143 | clear_extent_bit(&inode->io_tree, start, end, clear_bits, |
Omar Sandoval | bffe633 | 2019-12-02 17:34:19 -0800 | [diff] [blame] | 3144 | (clear_bits & EXTENT_LOCKED) ? 1 : 0, 0, |
Omar Sandoval | 313facc | 2019-12-02 17:34:18 -0800 | [diff] [blame] | 3145 | &cached_state); |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 3146 | |
Miao Xie | a698d075 | 2012-09-20 01:51:59 -0600 | [diff] [blame] | 3147 | if (trans) |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 3148 | btrfs_end_transaction(trans); |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 3149 | |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 3150 | if (ret || truncated) { |
Omar Sandoval | bffe633 | 2019-12-02 17:34:19 -0800 | [diff] [blame] | 3151 | u64 unwritten_start = start; |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 3152 | |
Josef Bacik | d61bec0 | 2021-05-19 09:38:27 -0400 | [diff] [blame] | 3153 | /* |
| 3154 | * If we failed to finish this ordered extent for any reason we |
| 3155 | * need to make sure BTRFS_ORDERED_IOERR is set on the ordered |
| 3156 | * extent, and mark the inode with the error if it wasn't |
| 3157 | * already set. Any error during writeback would have already |
| 3158 | * set the mapping error, so we need to set it if we're the ones |
| 3159 | * marking this ordered extent as failed. |
| 3160 | */ |
| 3161 | if (ret && !test_and_set_bit(BTRFS_ORDERED_IOERR, |
| 3162 | &ordered_extent->flags)) |
| 3163 | mapping_set_error(ordered_extent->inode->i_mapping, -EIO); |
| 3164 | |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 3165 | if (truncated) |
Omar Sandoval | bffe633 | 2019-12-02 17:34:19 -0800 | [diff] [blame] | 3166 | unwritten_start += logical_len; |
| 3167 | clear_extent_uptodate(io_tree, unwritten_start, end, NULL); |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 3168 | |
| 3169 | /* 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] | 3170 | btrfs_drop_extent_cache(inode, unwritten_start, end, 0); |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 3171 | |
Josef Bacik | 0bec9ef | 2013-01-31 14:58:00 -0500 | [diff] [blame] | 3172 | /* |
| 3173 | * If the ordered extent had an IOERR or something else went |
| 3174 | * wrong we need to return the space for this ordered extent |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 3175 | * back to the allocator. We only free the extent in the |
| 3176 | * truncated case if we didn't write out the extent at all. |
Josef Bacik | 49940bd | 2018-10-11 15:54:21 -0400 | [diff] [blame] | 3177 | * |
| 3178 | * If we made it past insert_reserved_file_extent before we |
| 3179 | * errored out then we don't need to do this as the accounting |
| 3180 | * has already been done. |
Josef Bacik | 0bec9ef | 2013-01-31 14:58:00 -0500 | [diff] [blame] | 3181 | */ |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 3182 | if ((ret || !logical_len) && |
Josef Bacik | 49940bd | 2018-10-11 15:54:21 -0400 | [diff] [blame] | 3183 | clear_reserved_extent && |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 3184 | !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) && |
Nikolay Borisov | 4eaaec2 | 2019-11-21 14:03:29 +0200 | [diff] [blame] | 3185 | !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) { |
| 3186 | /* |
| 3187 | * Discard the range before returning it back to the |
| 3188 | * free space pool |
| 3189 | */ |
Dennis Zhou | 46b27f5 | 2019-12-13 16:22:11 -0800 | [diff] [blame] | 3190 | if (ret && btrfs_test_opt(fs_info, DISCARD_SYNC)) |
Nikolay Borisov | 4eaaec2 | 2019-11-21 14:03:29 +0200 | [diff] [blame] | 3191 | btrfs_discard_extent(fs_info, |
Omar Sandoval | bffe633 | 2019-12-02 17:34:19 -0800 | [diff] [blame] | 3192 | ordered_extent->disk_bytenr, |
| 3193 | ordered_extent->disk_num_bytes, |
| 3194 | NULL); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3195 | btrfs_free_reserved_extent(fs_info, |
Omar Sandoval | bffe633 | 2019-12-02 17:34:19 -0800 | [diff] [blame] | 3196 | ordered_extent->disk_bytenr, |
| 3197 | ordered_extent->disk_num_bytes, 1); |
Nikolay Borisov | 4eaaec2 | 2019-11-21 14:03:29 +0200 | [diff] [blame] | 3198 | } |
Josef Bacik | 0bec9ef | 2013-01-31 14:58:00 -0500 | [diff] [blame] | 3199 | } |
| 3200 | |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 3201 | /* |
Liu Bo | 8bad3c0 | 2012-06-18 12:14:23 +0800 | [diff] [blame] | 3202 | * This needs to be done to make sure anybody waiting knows we are done |
| 3203 | * updating everything for this ordered extent. |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 3204 | */ |
Nikolay Borisov | 72e7e6e | 2020-11-02 16:48:56 +0200 | [diff] [blame] | 3205 | btrfs_remove_ordered_extent(inode, ordered_extent); |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 3206 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3207 | /* once for us */ |
| 3208 | btrfs_put_ordered_extent(ordered_extent); |
| 3209 | /* once for the tree */ |
| 3210 | btrfs_put_ordered_extent(ordered_extent); |
| 3211 | |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 3212 | return ret; |
| 3213 | } |
| 3214 | |
| 3215 | static void finish_ordered_fn(struct btrfs_work *work) |
| 3216 | { |
| 3217 | struct btrfs_ordered_extent *ordered_extent; |
| 3218 | ordered_extent = container_of(work, struct btrfs_ordered_extent, work); |
| 3219 | btrfs_finish_ordered_io(ordered_extent); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3220 | } |
| 3221 | |
Qu Wenruo | 38a39ac7 | 2021-04-08 20:32:27 +0800 | [diff] [blame] | 3222 | void btrfs_writepage_endio_finish_ordered(struct btrfs_inode *inode, |
| 3223 | struct page *page, u64 start, |
David Sterba | 25c1252 | 2021-07-26 14:15:08 +0200 | [diff] [blame] | 3224 | u64 end, bool uptodate) |
Chris Mason | 211f90e | 2008-07-18 11:56:15 -0400 | [diff] [blame] | 3225 | { |
Qu Wenruo | 38a39ac7 | 2021-04-08 20:32:27 +0800 | [diff] [blame] | 3226 | trace_btrfs_writepage_end_io_hook(inode, start, end, uptodate); |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 3227 | |
Qu Wenruo | e65f152 | 2021-04-01 15:15:06 +0800 | [diff] [blame] | 3228 | btrfs_mark_ordered_io_finished(inode, page, start, end + 1 - start, |
| 3229 | finish_ordered_fn, uptodate); |
Chris Mason | 211f90e | 2008-07-18 11:56:15 -0400 | [diff] [blame] | 3230 | } |
| 3231 | |
Qu Wenruo | 265d4ac | 2020-10-21 14:24:54 +0800 | [diff] [blame] | 3232 | /* |
| 3233 | * check_data_csum - verify checksum of one sector of uncompressed data |
Qu Wenruo | 7ffd27e | 2020-12-02 14:47:58 +0800 | [diff] [blame] | 3234 | * @inode: inode |
Qu Wenruo | 265d4ac | 2020-10-21 14:24:54 +0800 | [diff] [blame] | 3235 | * @io_bio: btrfs_io_bio which contains the csum |
Qu Wenruo | 7ffd27e | 2020-12-02 14:47:58 +0800 | [diff] [blame] | 3236 | * @bio_offset: offset to the beginning of the bio (in bytes) |
Qu Wenruo | 265d4ac | 2020-10-21 14:24:54 +0800 | [diff] [blame] | 3237 | * @page: page where is the data to be verified |
| 3238 | * @pgoff: offset inside the page |
Omar Sandoval | c1d6abd | 2021-03-16 12:43:00 -0700 | [diff] [blame] | 3239 | * @start: logical offset in the file |
Qu Wenruo | 265d4ac | 2020-10-21 14:24:54 +0800 | [diff] [blame] | 3240 | * |
| 3241 | * The length of such check is always one sector size. |
| 3242 | */ |
Qu Wenruo | c3a3b19 | 2021-09-15 15:17:18 +0800 | [diff] [blame] | 3243 | static int check_data_csum(struct inode *inode, struct btrfs_bio *bbio, |
Omar Sandoval | c1d6abd | 2021-03-16 12:43:00 -0700 | [diff] [blame] | 3244 | u32 bio_offset, struct page *page, u32 pgoff, |
| 3245 | u64 start) |
Miao Xie | dc380ae | 2014-09-12 18:43:55 +0800 | [diff] [blame] | 3246 | { |
Johannes Thumshirn | d517857 | 2019-06-03 16:58:57 +0200 | [diff] [blame] | 3247 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
| 3248 | SHASH_DESC_ON_STACK(shash, fs_info->csum_shash); |
Miao Xie | dc380ae | 2014-09-12 18:43:55 +0800 | [diff] [blame] | 3249 | char *kaddr; |
Qu Wenruo | 265d4ac | 2020-10-21 14:24:54 +0800 | [diff] [blame] | 3250 | u32 len = fs_info->sectorsize; |
David Sterba | 223486c | 2020-07-02 11:27:30 +0200 | [diff] [blame] | 3251 | const u32 csum_size = fs_info->csum_size; |
Qu Wenruo | 7ffd27e | 2020-12-02 14:47:58 +0800 | [diff] [blame] | 3252 | unsigned int offset_sectors; |
Johannes Thumshirn | d517857 | 2019-06-03 16:58:57 +0200 | [diff] [blame] | 3253 | u8 *csum_expected; |
| 3254 | u8 csum[BTRFS_CSUM_SIZE]; |
Miao Xie | dc380ae | 2014-09-12 18:43:55 +0800 | [diff] [blame] | 3255 | |
Qu Wenruo | 265d4ac | 2020-10-21 14:24:54 +0800 | [diff] [blame] | 3256 | ASSERT(pgoff + len <= PAGE_SIZE); |
| 3257 | |
Qu Wenruo | 7ffd27e | 2020-12-02 14:47:58 +0800 | [diff] [blame] | 3258 | offset_sectors = bio_offset >> fs_info->sectorsize_bits; |
Qu Wenruo | c3a3b19 | 2021-09-15 15:17:18 +0800 | [diff] [blame] | 3259 | csum_expected = ((u8 *)bbio->csum) + offset_sectors * csum_size; |
Miao Xie | dc380ae | 2014-09-12 18:43:55 +0800 | [diff] [blame] | 3260 | |
| 3261 | kaddr = kmap_atomic(page); |
Johannes Thumshirn | d517857 | 2019-06-03 16:58:57 +0200 | [diff] [blame] | 3262 | shash->tfm = fs_info->csum_shash; |
| 3263 | |
Eric Biggers | fd08001 | 2020-04-30 23:51:59 -0700 | [diff] [blame] | 3264 | crypto_shash_digest(shash, kaddr + pgoff, len, csum); |
Johannes Thumshirn | d517857 | 2019-06-03 16:58:57 +0200 | [diff] [blame] | 3265 | |
| 3266 | if (memcmp(csum, csum_expected, csum_size)) |
Miao Xie | dc380ae | 2014-09-12 18:43:55 +0800 | [diff] [blame] | 3267 | goto zeroit; |
| 3268 | |
| 3269 | kunmap_atomic(kaddr); |
| 3270 | return 0; |
| 3271 | zeroit: |
Omar Sandoval | c1d6abd | 2021-03-16 12:43:00 -0700 | [diff] [blame] | 3272 | btrfs_print_data_csum_error(BTRFS_I(inode), start, csum, csum_expected, |
Qu Wenruo | c3a3b19 | 2021-09-15 15:17:18 +0800 | [diff] [blame] | 3273 | bbio->mirror_num); |
| 3274 | if (bbio->device) |
| 3275 | btrfs_dev_stat_inc_and_print(bbio->device, |
Nikolay Borisov | 814723e | 2020-07-02 15:23:32 +0300 | [diff] [blame] | 3276 | BTRFS_DEV_STAT_CORRUPTION_ERRS); |
Miao Xie | dc380ae | 2014-09-12 18:43:55 +0800 | [diff] [blame] | 3277 | memset(kaddr + pgoff, 1, len); |
| 3278 | flush_dcache_page(page); |
| 3279 | kunmap_atomic(kaddr); |
Miao Xie | dc380ae | 2014-09-12 18:43:55 +0800 | [diff] [blame] | 3280 | return -EIO; |
| 3281 | } |
| 3282 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3283 | /* |
Qu Wenruo | 7ffd27e | 2020-12-02 14:47:58 +0800 | [diff] [blame] | 3284 | * 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] | 3285 | * if there's a match, we allow the bio to finish. If not, the code in |
| 3286 | * extent_io.c will try to find good copies for us. |
Qu Wenruo | 7ffd27e | 2020-12-02 14:47:58 +0800 | [diff] [blame] | 3287 | * |
| 3288 | * @bio_offset: offset to the beginning of the bio (in bytes) |
| 3289 | * @start: file offset of the range start |
| 3290 | * @end: file offset of the range end (inclusive) |
Qu Wenruo | 08508fe | 2021-05-03 10:08:54 +0800 | [diff] [blame] | 3291 | * |
| 3292 | * Return a bitmap where bit set means a csum mismatch, and bit not set means |
| 3293 | * csum match. |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3294 | */ |
Qu Wenruo | c3a3b19 | 2021-09-15 15:17:18 +0800 | [diff] [blame] | 3295 | unsigned int btrfs_verify_data_csum(struct btrfs_bio *bbio, |
| 3296 | u32 bio_offset, struct page *page, |
| 3297 | u64 start, u64 end) |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 3298 | { |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 3299 | struct inode *inode = page->mapping->host; |
Qu Wenruo | e4f9434 | 2021-09-27 15:21:49 +0800 | [diff] [blame] | 3300 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3301 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | ff79f81 | 2007-10-15 16:22:25 -0400 | [diff] [blame] | 3302 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Qu Wenruo | f44cf41 | 2020-12-02 14:47:59 +0800 | [diff] [blame] | 3303 | const u32 sectorsize = root->fs_info->sectorsize; |
| 3304 | u32 pg_off; |
Qu Wenruo | 08508fe | 2021-05-03 10:08:54 +0800 | [diff] [blame] | 3305 | unsigned int result = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3306 | |
Qu Wenruo | e4f9434 | 2021-09-27 15:21:49 +0800 | [diff] [blame] | 3307 | if (btrfs_page_test_checked(fs_info, page, start, end + 1 - start)) { |
| 3308 | btrfs_page_clear_checked(fs_info, page, start, end + 1 - start); |
Miao Xie | dc380ae | 2014-09-12 18:43:55 +0800 | [diff] [blame] | 3309 | return 0; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 3310 | } |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 3311 | |
Qu Wenruo | 3670e64 | 2021-07-26 14:34:51 +0800 | [diff] [blame] | 3312 | /* |
Qu Wenruo | e4f9434 | 2021-09-27 15:21:49 +0800 | [diff] [blame] | 3313 | * This only happens for NODATASUM or compressed read. |
| 3314 | * Normally this should be covered by above check for compressed read |
| 3315 | * or the next check for NODATASUM. Just do a quicker exit here. |
Qu Wenruo | 3670e64 | 2021-07-26 14:34:51 +0800 | [diff] [blame] | 3316 | */ |
Qu Wenruo | c3a3b19 | 2021-09-15 15:17:18 +0800 | [diff] [blame] | 3317 | if (bbio->csum == NULL) |
Qu Wenruo | 3670e64 | 2021-07-26 14:34:51 +0800 | [diff] [blame] | 3318 | return 0; |
| 3319 | |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 3320 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM) |
Miao Xie | dc380ae | 2014-09-12 18:43:55 +0800 | [diff] [blame] | 3321 | return 0; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 3322 | |
Josef Bacik | 056c831 | 2021-11-05 16:45:47 -0400 | [diff] [blame] | 3323 | if (unlikely(test_bit(BTRFS_FS_STATE_NO_CSUMS, &fs_info->fs_state))) |
Josef Bacik | 42437a6 | 2020-10-16 11:29:18 -0400 | [diff] [blame] | 3324 | return 0; |
| 3325 | |
Qu Wenruo | f44cf41 | 2020-12-02 14:47:59 +0800 | [diff] [blame] | 3326 | ASSERT(page_offset(page) <= start && |
| 3327 | end <= page_offset(page) + PAGE_SIZE - 1); |
| 3328 | for (pg_off = offset_in_page(start); |
| 3329 | pg_off < offset_in_page(end); |
| 3330 | pg_off += sectorsize, bio_offset += sectorsize) { |
Qu Wenruo | e3c6232 | 2021-07-26 14:35:04 +0800 | [diff] [blame] | 3331 | u64 file_offset = pg_off + page_offset(page); |
Qu Wenruo | f44cf41 | 2020-12-02 14:47:59 +0800 | [diff] [blame] | 3332 | int ret; |
| 3333 | |
Johannes Thumshirn | 37f00a6 | 2021-09-09 01:19:25 +0900 | [diff] [blame] | 3334 | if (btrfs_is_data_reloc_root(root) && |
Qu Wenruo | e3c6232 | 2021-07-26 14:35:04 +0800 | [diff] [blame] | 3335 | test_range_bit(io_tree, file_offset, |
| 3336 | file_offset + sectorsize - 1, |
| 3337 | EXTENT_NODATASUM, 1, NULL)) { |
| 3338 | /* Skip the range without csum for data reloc inode */ |
| 3339 | clear_extent_bits(io_tree, file_offset, |
| 3340 | file_offset + sectorsize - 1, |
| 3341 | EXTENT_NODATASUM); |
| 3342 | continue; |
| 3343 | } |
Qu Wenruo | c3a3b19 | 2021-09-15 15:17:18 +0800 | [diff] [blame] | 3344 | ret = check_data_csum(inode, bbio, bio_offset, page, pg_off, |
Omar Sandoval | c1d6abd | 2021-03-16 12:43:00 -0700 | [diff] [blame] | 3345 | page_offset(page) + pg_off); |
Qu Wenruo | 08508fe | 2021-05-03 10:08:54 +0800 | [diff] [blame] | 3346 | if (ret < 0) { |
| 3347 | const int nr_bit = (pg_off - offset_in_page(start)) >> |
| 3348 | root->fs_info->sectorsize_bits; |
| 3349 | |
| 3350 | result |= (1U << nr_bit); |
| 3351 | } |
Qu Wenruo | f44cf41 | 2020-12-02 14:47:59 +0800 | [diff] [blame] | 3352 | } |
Qu Wenruo | 08508fe | 2021-05-03 10:08:54 +0800 | [diff] [blame] | 3353 | return result; |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 3354 | } |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3355 | |
Nikolay Borisov | c1c3fac | 2018-01-16 09:31:58 +0200 | [diff] [blame] | 3356 | /* |
| 3357 | * btrfs_add_delayed_iput - perform a delayed iput on @inode |
| 3358 | * |
| 3359 | * @inode: The inode we want to perform iput on |
| 3360 | * |
| 3361 | * This function uses the generic vfs_inode::i_count to track whether we should |
| 3362 | * just decrement it (in case it's > 1) or if this is the last iput then link |
| 3363 | * the inode to the delayed iput machinery. Delayed iputs are processed at |
| 3364 | * transaction commit time/superblock commit/cleaner kthread. |
| 3365 | */ |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 3366 | void btrfs_add_delayed_iput(struct inode *inode) |
| 3367 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3368 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
David Sterba | 8089fe6 | 2015-11-19 14:15:51 +0100 | [diff] [blame] | 3369 | struct btrfs_inode *binode = BTRFS_I(inode); |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 3370 | |
| 3371 | if (atomic_add_unless(&inode->i_count, -1, 1)) |
| 3372 | return; |
| 3373 | |
Josef Bacik | 034f784 | 2018-12-03 11:06:52 -0500 | [diff] [blame] | 3374 | atomic_inc(&fs_info->nr_delayed_iputs); |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 3375 | spin_lock(&fs_info->delayed_iput_lock); |
Nikolay Borisov | c1c3fac | 2018-01-16 09:31:58 +0200 | [diff] [blame] | 3376 | ASSERT(list_empty(&binode->delayed_iput)); |
| 3377 | list_add_tail(&binode->delayed_iput, &fs_info->delayed_iputs); |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 3378 | spin_unlock(&fs_info->delayed_iput_lock); |
Josef Bacik | fd340d0 | 2019-01-11 10:21:02 -0500 | [diff] [blame] | 3379 | if (!test_bit(BTRFS_FS_CLEANER_RUNNING, &fs_info->flags)) |
| 3380 | wake_up_process(fs_info->cleaner_kthread); |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 3381 | } |
| 3382 | |
Josef Bacik | 63611e7 | 2019-06-18 10:59:18 -0400 | [diff] [blame] | 3383 | static void run_delayed_iput_locked(struct btrfs_fs_info *fs_info, |
| 3384 | struct btrfs_inode *inode) |
| 3385 | { |
| 3386 | list_del_init(&inode->delayed_iput); |
| 3387 | spin_unlock(&fs_info->delayed_iput_lock); |
| 3388 | iput(&inode->vfs_inode); |
| 3389 | if (atomic_dec_and_test(&fs_info->nr_delayed_iputs)) |
| 3390 | wake_up(&fs_info->delayed_iputs_wait); |
| 3391 | spin_lock(&fs_info->delayed_iput_lock); |
| 3392 | } |
| 3393 | |
| 3394 | static void btrfs_run_delayed_iput(struct btrfs_fs_info *fs_info, |
| 3395 | struct btrfs_inode *inode) |
| 3396 | { |
| 3397 | if (!list_empty(&inode->delayed_iput)) { |
| 3398 | spin_lock(&fs_info->delayed_iput_lock); |
| 3399 | if (!list_empty(&inode->delayed_iput)) |
| 3400 | run_delayed_iput_locked(fs_info, inode); |
| 3401 | spin_unlock(&fs_info->delayed_iput_lock); |
| 3402 | } |
| 3403 | } |
| 3404 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3405 | void btrfs_run_delayed_iputs(struct btrfs_fs_info *fs_info) |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 3406 | { |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 3407 | |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 3408 | spin_lock(&fs_info->delayed_iput_lock); |
David Sterba | 8089fe6 | 2015-11-19 14:15:51 +0100 | [diff] [blame] | 3409 | while (!list_empty(&fs_info->delayed_iputs)) { |
| 3410 | struct btrfs_inode *inode; |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 3411 | |
David Sterba | 8089fe6 | 2015-11-19 14:15:51 +0100 | [diff] [blame] | 3412 | inode = list_first_entry(&fs_info->delayed_iputs, |
| 3413 | struct btrfs_inode, delayed_iput); |
Josef Bacik | 63611e7 | 2019-06-18 10:59:18 -0400 | [diff] [blame] | 3414 | run_delayed_iput_locked(fs_info, inode); |
Josef Bacik | 71795ee | 2021-04-29 10:51:34 -0400 | [diff] [blame] | 3415 | cond_resched_lock(&fs_info->delayed_iput_lock); |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 3416 | } |
David Sterba | 8089fe6 | 2015-11-19 14:15:51 +0100 | [diff] [blame] | 3417 | spin_unlock(&fs_info->delayed_iput_lock); |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 3418 | } |
| 3419 | |
Josef Bacik | 034f784 | 2018-12-03 11:06:52 -0500 | [diff] [blame] | 3420 | /** |
Nikolay Borisov | 2639631 | 2021-01-22 11:57:59 +0200 | [diff] [blame] | 3421 | * Wait for flushing all delayed iputs |
| 3422 | * |
| 3423 | * @fs_info: the filesystem |
Josef Bacik | 034f784 | 2018-12-03 11:06:52 -0500 | [diff] [blame] | 3424 | * |
| 3425 | * This will wait on any delayed iputs that are currently running with KILLABLE |
| 3426 | * set. Once they are all done running we will return, unless we are killed in |
| 3427 | * which case we return EINTR. This helps in user operations like fallocate etc |
| 3428 | * that might get blocked on the iputs. |
Nikolay Borisov | 2639631 | 2021-01-22 11:57:59 +0200 | [diff] [blame] | 3429 | * |
| 3430 | * Return EINTR if we were killed, 0 if nothing's pending |
Josef Bacik | 034f784 | 2018-12-03 11:06:52 -0500 | [diff] [blame] | 3431 | */ |
| 3432 | int btrfs_wait_on_delayed_iputs(struct btrfs_fs_info *fs_info) |
| 3433 | { |
| 3434 | int ret = wait_event_killable(fs_info->delayed_iputs_wait, |
| 3435 | atomic_read(&fs_info->nr_delayed_iputs) == 0); |
| 3436 | if (ret) |
| 3437 | return -EINTR; |
| 3438 | return 0; |
| 3439 | } |
| 3440 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3441 | /* |
Omar Sandoval | f7e9e8f | 2018-05-11 13:13:32 -0700 | [diff] [blame] | 3442 | * This creates an orphan entry for the given inode in case something goes wrong |
| 3443 | * in the middle of an unlink. |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3444 | */ |
Nikolay Borisov | 73f2e54 | 2017-02-20 13:50:59 +0200 | [diff] [blame] | 3445 | int btrfs_orphan_add(struct btrfs_trans_handle *trans, |
Omar Sandoval | 2791906 | 2018-05-11 13:13:37 -0700 | [diff] [blame] | 3446 | struct btrfs_inode *inode) |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3447 | { |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3448 | int ret; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3449 | |
Omar Sandoval | 2791906 | 2018-05-11 13:13:37 -0700 | [diff] [blame] | 3450 | ret = btrfs_insert_orphan_item(trans, inode->root, btrfs_ino(inode)); |
| 3451 | if (ret && ret != -EEXIST) { |
| 3452 | btrfs_abort_transaction(trans, ret); |
| 3453 | return ret; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3454 | } |
| 3455 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3456 | return 0; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3457 | } |
| 3458 | |
| 3459 | /* |
Omar Sandoval | f7e9e8f | 2018-05-11 13:13:32 -0700 | [diff] [blame] | 3460 | * We have done the delete so we can go ahead and remove the orphan item for |
| 3461 | * this particular inode. |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3462 | */ |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 3463 | static int btrfs_orphan_del(struct btrfs_trans_handle *trans, |
Nikolay Borisov | 3d6ae7b | 2017-02-20 13:50:58 +0200 | [diff] [blame] | 3464 | struct btrfs_inode *inode) |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3465 | { |
Omar Sandoval | 2791906 | 2018-05-11 13:13:37 -0700 | [diff] [blame] | 3466 | return btrfs_del_orphan_item(trans, inode->root, btrfs_ino(inode)); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3467 | } |
| 3468 | |
| 3469 | /* |
| 3470 | * this cleans up any orphans that may be left on the list from the last use |
| 3471 | * of this root. |
| 3472 | */ |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3473 | int btrfs_orphan_cleanup(struct btrfs_root *root) |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3474 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3475 | struct btrfs_fs_info *fs_info = root->fs_info; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3476 | struct btrfs_path *path; |
| 3477 | struct extent_buffer *leaf; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3478 | struct btrfs_key key, found_key; |
| 3479 | struct btrfs_trans_handle *trans; |
| 3480 | struct inode *inode; |
Josef Bacik | 8f6d7f4 | 2011-09-26 15:55:20 -0400 | [diff] [blame] | 3481 | u64 last_objectid = 0; |
Omar Sandoval | f7e9e8f | 2018-05-11 13:13:32 -0700 | [diff] [blame] | 3482 | int ret = 0, nr_unlink = 0; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3483 | |
Josef Bacik | 5423001 | 2021-11-09 10:12:06 -0500 | [diff] [blame] | 3484 | if (test_and_set_bit(BTRFS_ROOT_ORPHAN_CLEANUP, &root->state)) |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3485 | return 0; |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 3486 | |
| 3487 | path = btrfs_alloc_path(); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3488 | if (!path) { |
| 3489 | ret = -ENOMEM; |
| 3490 | goto out; |
| 3491 | } |
David Sterba | e4058b5 | 2015-11-27 16:31:35 +0100 | [diff] [blame] | 3492 | path->reada = READA_BACK; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3493 | |
| 3494 | key.objectid = BTRFS_ORPHAN_OBJECTID; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 3495 | key.type = BTRFS_ORPHAN_ITEM_KEY; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3496 | key.offset = (u64)-1; |
| 3497 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3498 | while (1) { |
| 3499 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3500 | if (ret < 0) |
| 3501 | goto out; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3502 | |
| 3503 | /* |
| 3504 | * 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] | 3505 | * 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] | 3506 | * find the key and see if we have stuff that matches |
| 3507 | */ |
| 3508 | if (ret > 0) { |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3509 | ret = 0; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3510 | if (path->slots[0] == 0) |
| 3511 | break; |
| 3512 | path->slots[0]--; |
| 3513 | } |
| 3514 | |
| 3515 | /* pull out the item */ |
| 3516 | leaf = path->nodes[0]; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3517 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 3518 | |
| 3519 | /* make sure the item matches what we want */ |
| 3520 | if (found_key.objectid != BTRFS_ORPHAN_OBJECTID) |
| 3521 | break; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 3522 | if (found_key.type != BTRFS_ORPHAN_ITEM_KEY) |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3523 | break; |
| 3524 | |
| 3525 | /* release the path since we're done with it */ |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3526 | btrfs_release_path(path); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3527 | |
| 3528 | /* |
| 3529 | * this is where we are basically btrfs_lookup, without the |
| 3530 | * crossing root thing. we store the inode number in the |
| 3531 | * offset of the orphan item. |
| 3532 | */ |
Josef Bacik | 8f6d7f4 | 2011-09-26 15:55:20 -0400 | [diff] [blame] | 3533 | |
| 3534 | if (found_key.offset == last_objectid) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3535 | btrfs_err(fs_info, |
| 3536 | "Error removing orphan entry, stopping orphan cleanup"); |
Josef Bacik | 8f6d7f4 | 2011-09-26 15:55:20 -0400 | [diff] [blame] | 3537 | ret = -EINVAL; |
| 3538 | goto out; |
| 3539 | } |
| 3540 | |
| 3541 | last_objectid = found_key.offset; |
| 3542 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3543 | found_key.objectid = found_key.offset; |
| 3544 | found_key.type = BTRFS_INODE_ITEM_KEY; |
| 3545 | found_key.offset = 0; |
David Sterba | 0202e83 | 2020-05-15 19:35:59 +0200 | [diff] [blame] | 3546 | inode = btrfs_iget(fs_info->sb, last_objectid, root); |
Rusty Russell | 8c6ffba | 2013-07-15 11:20:32 +0930 | [diff] [blame] | 3547 | ret = PTR_ERR_OR_ZERO(inode); |
Filipe Manana | 6771089 | 2016-06-06 11:51:25 +0100 | [diff] [blame] | 3548 | if (ret && ret != -ENOENT) |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3549 | goto out; |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 3550 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3551 | if (ret == -ENOENT && root == fs_info->tree_root) { |
Arne Jansen | f8e9e0b | 2011-12-14 20:12:02 -0500 | [diff] [blame] | 3552 | struct btrfs_root *dead_root; |
Arne Jansen | f8e9e0b | 2011-12-14 20:12:02 -0500 | [diff] [blame] | 3553 | int is_dead_root = 0; |
| 3554 | |
| 3555 | /* |
Filipe Manana | 0c0218e | 2021-03-16 16:54:13 +0000 | [diff] [blame] | 3556 | * This is an orphan in the tree root. Currently these |
Arne Jansen | f8e9e0b | 2011-12-14 20:12:02 -0500 | [diff] [blame] | 3557 | * could come from 2 sources: |
Filipe Manana | 0c0218e | 2021-03-16 16:54:13 +0000 | [diff] [blame] | 3558 | * a) a root (snapshot/subvolume) deletion in progress |
Arne Jansen | f8e9e0b | 2011-12-14 20:12:02 -0500 | [diff] [blame] | 3559 | * b) a free space cache inode |
Filipe Manana | 0c0218e | 2021-03-16 16:54:13 +0000 | [diff] [blame] | 3560 | * We need to distinguish those two, as the orphan item |
| 3561 | * for a root must not get deleted before the deletion |
| 3562 | * of the snapshot/subvolume's tree completes. |
| 3563 | * |
| 3564 | * btrfs_find_orphan_roots() ran before us, which has |
| 3565 | * found all deleted roots and loaded them into |
| 3566 | * fs_info->fs_roots_radix. So here we can find if an |
| 3567 | * orphan item corresponds to a deleted root by looking |
| 3568 | * up the root from that radix tree. |
Arne Jansen | f8e9e0b | 2011-12-14 20:12:02 -0500 | [diff] [blame] | 3569 | */ |
Robbie Ko | a619b3c | 2020-05-07 10:54:40 +0800 | [diff] [blame] | 3570 | |
| 3571 | spin_lock(&fs_info->fs_roots_radix_lock); |
| 3572 | dead_root = radix_tree_lookup(&fs_info->fs_roots_radix, |
| 3573 | (unsigned long)found_key.objectid); |
| 3574 | if (dead_root && btrfs_root_refs(&dead_root->root_item) == 0) |
| 3575 | is_dead_root = 1; |
| 3576 | spin_unlock(&fs_info->fs_roots_radix_lock); |
| 3577 | |
Arne Jansen | f8e9e0b | 2011-12-14 20:12:02 -0500 | [diff] [blame] | 3578 | if (is_dead_root) { |
| 3579 | /* prevent this orphan from being found again */ |
| 3580 | key.offset = found_key.objectid - 1; |
| 3581 | continue; |
| 3582 | } |
Omar Sandoval | f7e9e8f | 2018-05-11 13:13:32 -0700 | [diff] [blame] | 3583 | |
Arne Jansen | f8e9e0b | 2011-12-14 20:12:02 -0500 | [diff] [blame] | 3584 | } |
Omar Sandoval | f7e9e8f | 2018-05-11 13:13:32 -0700 | [diff] [blame] | 3585 | |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 3586 | /* |
Omar Sandoval | f7e9e8f | 2018-05-11 13:13:32 -0700 | [diff] [blame] | 3587 | * If we have an inode with links, there are a couple of |
Boris Burkov | 7052425 | 2021-06-30 13:01:50 -0700 | [diff] [blame] | 3588 | * possibilities: |
| 3589 | * |
| 3590 | * 1. We were halfway through creating fsverity metadata for the |
| 3591 | * file. In that case, the orphan item represents incomplete |
| 3592 | * fsverity metadata which must be cleaned up with |
| 3593 | * btrfs_drop_verity_items and deleting the orphan item. |
| 3594 | |
| 3595 | * 2. Old kernels (before v3.12) used to create an |
Omar Sandoval | f7e9e8f | 2018-05-11 13:13:32 -0700 | [diff] [blame] | 3596 | * orphan item for truncate indicating that there were possibly |
| 3597 | * extent items past i_size that needed to be deleted. In v3.12, |
| 3598 | * truncate was changed to update i_size in sync with the extent |
| 3599 | * items, but the (useless) orphan item was still created. Since |
| 3600 | * v4.18, we don't create the orphan item for truncate at all. |
| 3601 | * |
| 3602 | * So, this item could mean that we need to do a truncate, but |
| 3603 | * only if this filesystem was last used on a pre-v3.12 kernel |
| 3604 | * and was not cleanly unmounted. The odds of that are quite |
| 3605 | * slim, and it's a pain to do the truncate now, so just delete |
| 3606 | * the orphan item. |
| 3607 | * |
| 3608 | * It's also possible that this orphan item was supposed to be |
| 3609 | * deleted but wasn't. The inode number may have been reused, |
| 3610 | * but either way, we can delete the orphan item. |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 3611 | */ |
Omar Sandoval | f7e9e8f | 2018-05-11 13:13:32 -0700 | [diff] [blame] | 3612 | if (ret == -ENOENT || inode->i_nlink) { |
Boris Burkov | 7052425 | 2021-06-30 13:01:50 -0700 | [diff] [blame] | 3613 | if (!ret) { |
| 3614 | ret = btrfs_drop_verity_items(BTRFS_I(inode)); |
Omar Sandoval | f7e9e8f | 2018-05-11 13:13:32 -0700 | [diff] [blame] | 3615 | iput(inode); |
Boris Burkov | 7052425 | 2021-06-30 13:01:50 -0700 | [diff] [blame] | 3616 | if (ret) |
| 3617 | goto out; |
| 3618 | } |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 3619 | trans = btrfs_start_transaction(root, 1); |
| 3620 | if (IS_ERR(trans)) { |
| 3621 | ret = PTR_ERR(trans); |
| 3622 | goto out; |
| 3623 | } |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3624 | btrfs_debug(fs_info, "auto deleting %Lu", |
| 3625 | found_key.objectid); |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 3626 | ret = btrfs_del_orphan_item(trans, root, |
| 3627 | found_key.objectid); |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 3628 | btrfs_end_transaction(trans); |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 3629 | if (ret) |
| 3630 | goto out; |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 3631 | continue; |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3632 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3633 | |
Omar Sandoval | f7e9e8f | 2018-05-11 13:13:32 -0700 | [diff] [blame] | 3634 | nr_unlink++; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3635 | |
| 3636 | /* this will do delete_inode and everything for us */ |
| 3637 | iput(inode); |
| 3638 | } |
Miao Xie | 3254c87 | 2011-11-10 20:45:05 -0500 | [diff] [blame] | 3639 | /* release the path since we're done with it */ |
| 3640 | btrfs_release_path(path); |
| 3641 | |
Omar Sandoval | a575cee | 2018-05-11 13:13:38 -0700 | [diff] [blame] | 3642 | if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) { |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 3643 | trans = btrfs_join_transaction(root); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3644 | if (!IS_ERR(trans)) |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 3645 | btrfs_end_transaction(trans); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3646 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3647 | |
| 3648 | if (nr_unlink) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3649 | btrfs_debug(fs_info, "unlinked %d orphans", nr_unlink); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3650 | |
| 3651 | out: |
| 3652 | if (ret) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3653 | btrfs_err(fs_info, "could not do orphan cleanup %d", ret); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3654 | btrfs_free_path(path); |
| 3655 | return ret; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3656 | } |
| 3657 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3658 | /* |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3659 | * very simple check to peek ahead in the leaf looking for xattrs. If we |
| 3660 | * don't find any xattrs, we know there can't be any acls. |
| 3661 | * |
| 3662 | * slot is the slot the inode is in, objectid is the objectid of the inode |
| 3663 | */ |
| 3664 | 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] | 3665 | int slot, u64 objectid, |
| 3666 | int *first_xattr_slot) |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3667 | { |
| 3668 | u32 nritems = btrfs_header_nritems(leaf); |
| 3669 | struct btrfs_key found_key; |
Josef Bacik | f23b5a5 | 2013-06-19 10:16:26 -0400 | [diff] [blame] | 3670 | static u64 xattr_access = 0; |
| 3671 | static u64 xattr_default = 0; |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3672 | int scanned = 0; |
| 3673 | |
Josef Bacik | f23b5a5 | 2013-06-19 10:16:26 -0400 | [diff] [blame] | 3674 | if (!xattr_access) { |
Andreas Gruenbacher | 97d7929 | 2015-12-02 14:44:35 +0100 | [diff] [blame] | 3675 | xattr_access = btrfs_name_hash(XATTR_NAME_POSIX_ACL_ACCESS, |
| 3676 | strlen(XATTR_NAME_POSIX_ACL_ACCESS)); |
| 3677 | xattr_default = btrfs_name_hash(XATTR_NAME_POSIX_ACL_DEFAULT, |
| 3678 | strlen(XATTR_NAME_POSIX_ACL_DEFAULT)); |
Josef Bacik | f23b5a5 | 2013-06-19 10:16:26 -0400 | [diff] [blame] | 3679 | } |
| 3680 | |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3681 | slot++; |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 3682 | *first_xattr_slot = -1; |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3683 | while (slot < nritems) { |
| 3684 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
| 3685 | |
| 3686 | /* we found a different objectid, there must not be acls */ |
| 3687 | if (found_key.objectid != objectid) |
| 3688 | return 0; |
| 3689 | |
| 3690 | /* we found an xattr, assume we've got an acl */ |
Josef Bacik | f23b5a5 | 2013-06-19 10:16:26 -0400 | [diff] [blame] | 3691 | if (found_key.type == BTRFS_XATTR_ITEM_KEY) { |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 3692 | if (*first_xattr_slot == -1) |
| 3693 | *first_xattr_slot = slot; |
Josef Bacik | f23b5a5 | 2013-06-19 10:16:26 -0400 | [diff] [blame] | 3694 | if (found_key.offset == xattr_access || |
| 3695 | found_key.offset == xattr_default) |
| 3696 | return 1; |
| 3697 | } |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3698 | |
| 3699 | /* |
| 3700 | * we found a key greater than an xattr key, there can't |
| 3701 | * be any acls later on |
| 3702 | */ |
| 3703 | if (found_key.type > BTRFS_XATTR_ITEM_KEY) |
| 3704 | return 0; |
| 3705 | |
| 3706 | slot++; |
| 3707 | scanned++; |
| 3708 | |
| 3709 | /* |
| 3710 | * it goes inode, inode backrefs, xattrs, extents, |
| 3711 | * so if there are a ton of hard links to an inode there can |
| 3712 | * be a lot of backrefs. Don't waste time searching too hard, |
| 3713 | * this is just an optimization |
| 3714 | */ |
| 3715 | if (scanned >= 8) |
| 3716 | break; |
| 3717 | } |
| 3718 | /* we hit the end of the leaf before we found an xattr or |
| 3719 | * something larger than an xattr. We have to assume the inode |
| 3720 | * has acls |
| 3721 | */ |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 3722 | if (*first_xattr_slot == -1) |
| 3723 | *first_xattr_slot = slot; |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3724 | return 1; |
| 3725 | } |
| 3726 | |
| 3727 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3728 | * read an inode from the btree into the in-memory inode |
| 3729 | */ |
Filipe Manana | 4222ea7 | 2018-10-24 10:13:03 +0100 | [diff] [blame] | 3730 | static int btrfs_read_locked_inode(struct inode *inode, |
| 3731 | struct btrfs_path *in_path) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3732 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3733 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Filipe Manana | 4222ea7 | 2018-10-24 10:13:03 +0100 | [diff] [blame] | 3734 | struct btrfs_path *path = in_path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3735 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3736 | struct btrfs_inode_item *inode_item; |
| 3737 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 3738 | struct btrfs_key location; |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 3739 | unsigned long ptr; |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3740 | int maybe_acls; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 3741 | u32 rdev; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3742 | int ret; |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 3743 | bool filled = false; |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 3744 | int first_xattr_slot; |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 3745 | |
| 3746 | ret = btrfs_fill_inode(inode, &rdev); |
| 3747 | if (!ret) |
| 3748 | filled = true; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3749 | |
Filipe Manana | 4222ea7 | 2018-10-24 10:13:03 +0100 | [diff] [blame] | 3750 | if (!path) { |
| 3751 | path = btrfs_alloc_path(); |
| 3752 | if (!path) |
| 3753 | return -ENOMEM; |
| 3754 | } |
Mark Fasheh | 1748f84 | 2011-07-12 11:25:31 -0700 | [diff] [blame] | 3755 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3756 | memcpy(&location, &BTRFS_I(inode)->location, sizeof(location)); |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 3757 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3758 | ret = btrfs_lookup_inode(NULL, root, path, &location, 0); |
Filipe Manana | 6771089 | 2016-06-06 11:51:25 +0100 | [diff] [blame] | 3759 | if (ret) { |
Filipe Manana | 4222ea7 | 2018-10-24 10:13:03 +0100 | [diff] [blame] | 3760 | if (path != in_path) |
| 3761 | btrfs_free_path(path); |
Al Viro | f5b3a41 | 2018-07-29 23:04:51 +0100 | [diff] [blame] | 3762 | return ret; |
Filipe Manana | 6771089 | 2016-06-06 11:51:25 +0100 | [diff] [blame] | 3763 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3764 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3765 | leaf = path->nodes[0]; |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 3766 | |
| 3767 | if (filled) |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 3768 | goto cache_index; |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 3769 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3770 | inode_item = btrfs_item_ptr(leaf, path->slots[0], |
| 3771 | struct btrfs_inode_item); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3772 | inode->i_mode = btrfs_inode_mode(leaf, inode_item); |
Miklos Szeredi | bfe8684 | 2011-10-28 14:13:29 +0200 | [diff] [blame] | 3773 | set_nlink(inode, btrfs_inode_nlink(leaf, inode_item)); |
Eric W. Biederman | 2f2f43d | 2012-02-10 11:05:07 -0800 | [diff] [blame] | 3774 | i_uid_write(inode, btrfs_inode_uid(leaf, inode_item)); |
| 3775 | i_gid_write(inode, btrfs_inode_gid(leaf, inode_item)); |
Nikolay Borisov | 6ef06d2 | 2017-02-20 13:50:34 +0200 | [diff] [blame] | 3776 | 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] | 3777 | btrfs_inode_set_file_extent_range(BTRFS_I(inode), 0, |
| 3778 | round_up(i_size_read(inode), fs_info->sectorsize)); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3779 | |
David Sterba | a937b97 | 2014-12-12 17:39:12 +0100 | [diff] [blame] | 3780 | inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->atime); |
| 3781 | inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->atime); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3782 | |
David Sterba | a937b97 | 2014-12-12 17:39:12 +0100 | [diff] [blame] | 3783 | inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->mtime); |
| 3784 | inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->mtime); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3785 | |
David Sterba | a937b97 | 2014-12-12 17:39:12 +0100 | [diff] [blame] | 3786 | inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->ctime); |
| 3787 | inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->ctime); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3788 | |
chandan r | 9cc97d6 | 2012-07-04 12:48:07 +0530 | [diff] [blame] | 3789 | BTRFS_I(inode)->i_otime.tv_sec = |
| 3790 | btrfs_timespec_sec(leaf, &inode_item->otime); |
| 3791 | BTRFS_I(inode)->i_otime.tv_nsec = |
| 3792 | btrfs_timespec_nsec(leaf, &inode_item->otime); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3793 | |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 3794 | inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item)); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3795 | BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item); |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 3796 | BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item); |
| 3797 | |
Jeff Layton | c7f88c4 | 2017-12-11 06:35:12 -0500 | [diff] [blame] | 3798 | inode_set_iversion_queried(inode, |
| 3799 | btrfs_inode_sequence(leaf, inode_item)); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3800 | inode->i_generation = BTRFS_I(inode)->generation; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 3801 | inode->i_rdev = 0; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3802 | rdev = btrfs_inode_rdev(leaf, inode_item); |
| 3803 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 3804 | BTRFS_I(inode)->index_cnt = (u64)-1; |
Boris Burkov | 77eea05 | 2021-06-30 13:01:48 -0700 | [diff] [blame] | 3805 | btrfs_inode_split_flags(btrfs_inode_flags(leaf, inode_item), |
| 3806 | &BTRFS_I(inode)->flags, &BTRFS_I(inode)->ro_flags); |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 3807 | |
| 3808 | cache_index: |
Yang Dongsheng | 6e17d30 | 2015-04-09 12:08:43 +0800 | [diff] [blame] | 3809 | /* |
| 3810 | * If we were modified in the current generation and evicted from memory |
| 3811 | * and then re-read we need to do a full sync since we don't have any |
| 3812 | * idea about which extents were modified before we were evicted from |
| 3813 | * cache. |
| 3814 | * |
| 3815 | * This is required for both inode re-read from disk and delayed inode |
| 3816 | * in delayed_nodes_tree. |
| 3817 | */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3818 | if (BTRFS_I(inode)->last_trans == fs_info->generation) |
Yang Dongsheng | 6e17d30 | 2015-04-09 12:08:43 +0800 | [diff] [blame] | 3819 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, |
| 3820 | &BTRFS_I(inode)->runtime_flags); |
| 3821 | |
Filipe Manana | bde6c24 | 2015-07-24 00:00:19 +0100 | [diff] [blame] | 3822 | /* |
| 3823 | * We don't persist the id of the transaction where an unlink operation |
| 3824 | * against the inode was last made. So here we assume the inode might |
| 3825 | * have been evicted, and therefore the exact value of last_unlink_trans |
| 3826 | * lost, and set it to last_trans to avoid metadata inconsistencies |
| 3827 | * between the inode and its parent if the inode is fsync'ed and the log |
| 3828 | * replayed. For example, in the scenario: |
| 3829 | * |
| 3830 | * touch mydir/foo |
| 3831 | * ln mydir/foo mydir/bar |
| 3832 | * sync |
| 3833 | * unlink mydir/bar |
| 3834 | * echo 2 > /proc/sys/vm/drop_caches # evicts inode |
| 3835 | * xfs_io -c fsync mydir/foo |
| 3836 | * <power failure> |
| 3837 | * mount fs, triggers fsync log replay |
| 3838 | * |
| 3839 | * We must make sure that when we fsync our inode foo we also log its |
| 3840 | * parent inode, otherwise after log replay the parent still has the |
| 3841 | * dentry with the "bar" name but our inode foo has a link count of 1 |
| 3842 | * and doesn't have an inode ref with the name "bar" anymore. |
| 3843 | * |
| 3844 | * Setting last_unlink_trans to last_trans is a pessimistic approach, |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 3845 | * but it guarantees correctness at the expense of occasional full |
Filipe Manana | bde6c24 | 2015-07-24 00:00:19 +0100 | [diff] [blame] | 3846 | * transaction commits on fsync if our inode is a directory, or if our |
| 3847 | * inode is not a directory, logging its parent unnecessarily. |
| 3848 | */ |
| 3849 | BTRFS_I(inode)->last_unlink_trans = BTRFS_I(inode)->last_trans; |
| 3850 | |
Filipe Manana | 3ebac17 | 2020-07-15 12:30:43 +0100 | [diff] [blame] | 3851 | /* |
| 3852 | * Same logic as for last_unlink_trans. We don't persist the generation |
| 3853 | * of the last transaction where this inode was used for a reflink |
| 3854 | * operation, so after eviction and reloading the inode we must be |
| 3855 | * pessimistic and assume the last transaction that modified the inode. |
| 3856 | */ |
| 3857 | BTRFS_I(inode)->last_reflink_trans = BTRFS_I(inode)->last_trans; |
| 3858 | |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 3859 | path->slots[0]++; |
| 3860 | if (inode->i_nlink != 1 || |
| 3861 | path->slots[0] >= btrfs_header_nritems(leaf)) |
| 3862 | goto cache_acl; |
| 3863 | |
| 3864 | btrfs_item_key_to_cpu(leaf, &location, path->slots[0]); |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 3865 | if (location.objectid != btrfs_ino(BTRFS_I(inode))) |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 3866 | goto cache_acl; |
| 3867 | |
| 3868 | ptr = btrfs_item_ptr_offset(leaf, path->slots[0]); |
| 3869 | if (location.type == BTRFS_INODE_REF_KEY) { |
| 3870 | struct btrfs_inode_ref *ref; |
| 3871 | |
| 3872 | ref = (struct btrfs_inode_ref *)ptr; |
| 3873 | BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref); |
| 3874 | } else if (location.type == BTRFS_INODE_EXTREF_KEY) { |
| 3875 | struct btrfs_inode_extref *extref; |
| 3876 | |
| 3877 | extref = (struct btrfs_inode_extref *)ptr; |
| 3878 | BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf, |
| 3879 | extref); |
| 3880 | } |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 3881 | cache_acl: |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3882 | /* |
| 3883 | * try to precache a NULL acl entry for files that don't have |
| 3884 | * any xattrs or acls |
| 3885 | */ |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3886 | maybe_acls = acls_after_inode_item(leaf, path->slots[0], |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 3887 | btrfs_ino(BTRFS_I(inode)), &first_xattr_slot); |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 3888 | if (first_xattr_slot != -1) { |
| 3889 | path->slots[0] = first_xattr_slot; |
| 3890 | ret = btrfs_load_inode_props(inode, path); |
| 3891 | if (ret) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3892 | btrfs_err(fs_info, |
David Sterba | 351fd35 | 2014-05-15 16:48:20 +0200 | [diff] [blame] | 3893 | "error loading props for ino %llu (root %llu): %d", |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 3894 | btrfs_ino(BTRFS_I(inode)), |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 3895 | root->root_key.objectid, ret); |
| 3896 | } |
Filipe Manana | 4222ea7 | 2018-10-24 10:13:03 +0100 | [diff] [blame] | 3897 | if (path != in_path) |
| 3898 | btrfs_free_path(path); |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 3899 | |
Al Viro | 72c0490 | 2009-06-24 16:58:48 -0400 | [diff] [blame] | 3900 | if (!maybe_acls) |
| 3901 | cache_no_acl(inode); |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3902 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3903 | switch (inode->i_mode & S_IFMT) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3904 | case S_IFREG: |
| 3905 | inode->i_mapping->a_ops = &btrfs_aops; |
| 3906 | inode->i_fop = &btrfs_file_operations; |
| 3907 | inode->i_op = &btrfs_file_inode_operations; |
| 3908 | break; |
| 3909 | case S_IFDIR: |
| 3910 | inode->i_fop = &btrfs_dir_file_operations; |
Omar Sandoval | 67ade05 | 2017-01-25 17:06:38 -0800 | [diff] [blame] | 3911 | inode->i_op = &btrfs_dir_inode_operations; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3912 | break; |
| 3913 | case S_IFLNK: |
| 3914 | inode->i_op = &btrfs_symlink_inode_operations; |
Al Viro | 21fc61c | 2015-11-17 01:07:57 -0500 | [diff] [blame] | 3915 | inode_nohighmem(inode); |
Omar Sandoval | 4779cc0 | 2018-09-24 15:16:55 -0700 | [diff] [blame] | 3916 | inode->i_mapping->a_ops = &btrfs_aops; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3917 | break; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 3918 | default: |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 3919 | inode->i_op = &btrfs_special_inode_operations; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 3920 | init_special_inode(inode, inode->i_mode, rdev); |
| 3921 | break; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3922 | } |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 3923 | |
David Sterba | 7b6a221 | 2018-03-26 18:40:21 +0200 | [diff] [blame] | 3924 | btrfs_sync_inode_flags_to_i_flags(inode); |
Filipe Manana | 6771089 | 2016-06-06 11:51:25 +0100 | [diff] [blame] | 3925 | return 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3926 | } |
| 3927 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3928 | /* |
| 3929 | * given a leaf and an inode, copy the inode fields into the leaf |
| 3930 | */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3931 | static void fill_inode_item(struct btrfs_trans_handle *trans, |
| 3932 | struct extent_buffer *leaf, |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3933 | struct btrfs_inode_item *item, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3934 | struct inode *inode) |
| 3935 | { |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3936 | struct btrfs_map_token token; |
Boris Burkov | 77eea05 | 2021-06-30 13:01:48 -0700 | [diff] [blame] | 3937 | u64 flags; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3938 | |
David Sterba | c82f823 | 2019-08-09 17:48:21 +0200 | [diff] [blame] | 3939 | btrfs_init_map_token(&token, leaf); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3940 | |
David Sterba | cc4c13d | 2020-04-29 02:15:56 +0200 | [diff] [blame] | 3941 | btrfs_set_token_inode_uid(&token, item, i_uid_read(inode)); |
| 3942 | btrfs_set_token_inode_gid(&token, item, i_gid_read(inode)); |
| 3943 | btrfs_set_token_inode_size(&token, item, BTRFS_I(inode)->disk_i_size); |
| 3944 | btrfs_set_token_inode_mode(&token, item, inode->i_mode); |
| 3945 | btrfs_set_token_inode_nlink(&token, item, inode->i_nlink); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3946 | |
David Sterba | cc4c13d | 2020-04-29 02:15:56 +0200 | [diff] [blame] | 3947 | btrfs_set_token_timespec_sec(&token, &item->atime, |
| 3948 | inode->i_atime.tv_sec); |
| 3949 | btrfs_set_token_timespec_nsec(&token, &item->atime, |
| 3950 | inode->i_atime.tv_nsec); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3951 | |
David Sterba | cc4c13d | 2020-04-29 02:15:56 +0200 | [diff] [blame] | 3952 | btrfs_set_token_timespec_sec(&token, &item->mtime, |
| 3953 | inode->i_mtime.tv_sec); |
| 3954 | btrfs_set_token_timespec_nsec(&token, &item->mtime, |
| 3955 | inode->i_mtime.tv_nsec); |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3956 | |
David Sterba | cc4c13d | 2020-04-29 02:15:56 +0200 | [diff] [blame] | 3957 | btrfs_set_token_timespec_sec(&token, &item->ctime, |
| 3958 | inode->i_ctime.tv_sec); |
| 3959 | btrfs_set_token_timespec_nsec(&token, &item->ctime, |
| 3960 | inode->i_ctime.tv_nsec); |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3961 | |
David Sterba | cc4c13d | 2020-04-29 02:15:56 +0200 | [diff] [blame] | 3962 | btrfs_set_token_timespec_sec(&token, &item->otime, |
| 3963 | BTRFS_I(inode)->i_otime.tv_sec); |
| 3964 | btrfs_set_token_timespec_nsec(&token, &item->otime, |
| 3965 | BTRFS_I(inode)->i_otime.tv_nsec); |
chandan r | 9cc97d6 | 2012-07-04 12:48:07 +0530 | [diff] [blame] | 3966 | |
David Sterba | cc4c13d | 2020-04-29 02:15:56 +0200 | [diff] [blame] | 3967 | btrfs_set_token_inode_nbytes(&token, item, inode_get_bytes(inode)); |
| 3968 | btrfs_set_token_inode_generation(&token, item, |
| 3969 | BTRFS_I(inode)->generation); |
| 3970 | btrfs_set_token_inode_sequence(&token, item, inode_peek_iversion(inode)); |
| 3971 | btrfs_set_token_inode_transid(&token, item, trans->transid); |
| 3972 | btrfs_set_token_inode_rdev(&token, item, inode->i_rdev); |
Boris Burkov | 77eea05 | 2021-06-30 13:01:48 -0700 | [diff] [blame] | 3973 | flags = btrfs_inode_combine_flags(BTRFS_I(inode)->flags, |
| 3974 | BTRFS_I(inode)->ro_flags); |
| 3975 | btrfs_set_token_inode_flags(&token, item, flags); |
David Sterba | cc4c13d | 2020-04-29 02:15:56 +0200 | [diff] [blame] | 3976 | btrfs_set_token_inode_block_group(&token, item, 0); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3977 | } |
| 3978 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3979 | /* |
| 3980 | * copy everything in the in-memory inode into the btree. |
| 3981 | */ |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 3982 | static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans, |
Nikolay Borisov | dfeb9e7 | 2020-11-02 16:48:58 +0200 | [diff] [blame] | 3983 | struct btrfs_root *root, |
| 3984 | struct btrfs_inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3985 | { |
| 3986 | struct btrfs_inode_item *inode_item; |
| 3987 | struct btrfs_path *path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3988 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3989 | int ret; |
| 3990 | |
| 3991 | path = btrfs_alloc_path(); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 3992 | if (!path) |
| 3993 | return -ENOMEM; |
| 3994 | |
Nikolay Borisov | dfeb9e7 | 2020-11-02 16:48:58 +0200 | [diff] [blame] | 3995 | ret = btrfs_lookup_inode(trans, root, path, &inode->location, 1); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3996 | if (ret) { |
| 3997 | if (ret > 0) |
| 3998 | ret = -ENOENT; |
| 3999 | goto failed; |
| 4000 | } |
| 4001 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4002 | leaf = path->nodes[0]; |
| 4003 | inode_item = btrfs_item_ptr(leaf, path->slots[0], |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 4004 | struct btrfs_inode_item); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4005 | |
Nikolay Borisov | dfeb9e7 | 2020-11-02 16:48:58 +0200 | [diff] [blame] | 4006 | fill_inode_item(trans, leaf, inode_item, &inode->vfs_inode); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4007 | btrfs_mark_buffer_dirty(leaf); |
Nikolay Borisov | dfeb9e7 | 2020-11-02 16:48:58 +0200 | [diff] [blame] | 4008 | btrfs_set_inode_last_trans(trans, inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4009 | ret = 0; |
| 4010 | failed: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4011 | btrfs_free_path(path); |
| 4012 | return ret; |
| 4013 | } |
| 4014 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 4015 | /* |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 4016 | * copy everything in the in-memory inode into the btree. |
| 4017 | */ |
| 4018 | noinline int btrfs_update_inode(struct btrfs_trans_handle *trans, |
Nikolay Borisov | 9a56fcd | 2020-11-02 16:48:59 +0200 | [diff] [blame] | 4019 | struct btrfs_root *root, |
| 4020 | struct btrfs_inode *inode) |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 4021 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4022 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 4023 | int ret; |
| 4024 | |
| 4025 | /* |
| 4026 | * If the inode is a free space inode, we can deadlock during commit |
| 4027 | * if we put it into the delayed code. |
| 4028 | * |
| 4029 | * The data relocation inode should also be directly updated |
| 4030 | * without delay |
| 4031 | */ |
Nikolay Borisov | 9a56fcd | 2020-11-02 16:48:59 +0200 | [diff] [blame] | 4032 | if (!btrfs_is_free_space_inode(inode) |
Johannes Thumshirn | 37f00a6 | 2021-09-09 01:19:25 +0900 | [diff] [blame] | 4033 | && !btrfs_is_data_reloc_root(root) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4034 | && !test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) { |
Alexander Block | 8ea05e3 | 2012-07-25 17:35:53 +0200 | [diff] [blame] | 4035 | btrfs_update_root_times(trans, root); |
| 4036 | |
Nikolay Borisov | 9a56fcd | 2020-11-02 16:48:59 +0200 | [diff] [blame] | 4037 | ret = btrfs_delayed_update_inode(trans, root, inode); |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 4038 | if (!ret) |
Nikolay Borisov | 9a56fcd | 2020-11-02 16:48:59 +0200 | [diff] [blame] | 4039 | btrfs_set_inode_last_trans(trans, inode); |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 4040 | return ret; |
| 4041 | } |
| 4042 | |
Nikolay Borisov | 9a56fcd | 2020-11-02 16:48:59 +0200 | [diff] [blame] | 4043 | return btrfs_update_inode_item(trans, root, inode); |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 4044 | } |
| 4045 | |
Nikolay Borisov | 729f796 | 2020-11-02 16:49:06 +0200 | [diff] [blame] | 4046 | int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans, |
| 4047 | struct btrfs_root *root, struct btrfs_inode *inode) |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 4048 | { |
| 4049 | int ret; |
| 4050 | |
Nikolay Borisov | 729f796 | 2020-11-02 16:49:06 +0200 | [diff] [blame] | 4051 | ret = btrfs_update_inode(trans, root, inode); |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 4052 | if (ret == -ENOSPC) |
Nikolay Borisov | 729f796 | 2020-11-02 16:49:06 +0200 | [diff] [blame] | 4053 | return btrfs_update_inode_item(trans, root, inode); |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 4054 | return ret; |
| 4055 | } |
| 4056 | |
| 4057 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 4058 | * unlink helper that gets used here in inode.c and in the tree logging |
| 4059 | * recovery code. It remove a link in a directory with a given name, and |
| 4060 | * also drops the back refs in the inode to the directory |
| 4061 | */ |
Al Viro | 9298679 | 2011-03-04 17:14:37 +0000 | [diff] [blame] | 4062 | static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans, |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 4063 | struct btrfs_inode *dir, |
| 4064 | struct btrfs_inode *inode, |
Al Viro | 9298679 | 2011-03-04 17:14:37 +0000 | [diff] [blame] | 4065 | const char *name, int name_len) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4066 | { |
Filipe Manana | 4467af8 | 2021-10-25 17:31:50 +0100 | [diff] [blame] | 4067 | struct btrfs_root *root = dir->root; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4068 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4069 | struct btrfs_path *path; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4070 | int ret = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4071 | struct btrfs_dir_item *di; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4072 | u64 index; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4073 | u64 ino = btrfs_ino(inode); |
| 4074 | u64 dir_ino = btrfs_ino(dir); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4075 | |
| 4076 | path = btrfs_alloc_path(); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 4077 | if (!path) { |
| 4078 | ret = -ENOMEM; |
Tsutomu Itoh | 554233a | 2011-02-03 03:16:25 +0000 | [diff] [blame] | 4079 | goto out; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 4080 | } |
| 4081 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4082 | di = btrfs_lookup_dir_item(trans, root, path, dir_ino, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4083 | name, name_len, -1); |
Liu Bo | 3cf5068 | 2018-09-12 06:06:26 +0800 | [diff] [blame] | 4084 | if (IS_ERR_OR_NULL(di)) { |
| 4085 | ret = di ? PTR_ERR(di) : -ENOENT; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4086 | goto err; |
| 4087 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4088 | ret = btrfs_delete_one_dir_name(trans, root, path, di); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 4089 | if (ret) |
| 4090 | goto err; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4091 | btrfs_release_path(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4092 | |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 4093 | /* |
| 4094 | * If we don't have dir index, we have to get it by looking up |
| 4095 | * the inode ref, since we get the inode ref, remove it directly, |
| 4096 | * it is unnecessary to do delayed deletion. |
| 4097 | * |
| 4098 | * But if we have dir index, needn't search inode ref to get it. |
| 4099 | * Since the inode ref is close to the inode item, it is better |
| 4100 | * that we delay to delete it, and just do this deletion when |
| 4101 | * we update the inode item. |
| 4102 | */ |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 4103 | if (inode->dir_index) { |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 4104 | ret = btrfs_delayed_delete_inode_ref(inode); |
| 4105 | if (!ret) { |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 4106 | index = inode->dir_index; |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 4107 | goto skip_backref; |
| 4108 | } |
| 4109 | } |
| 4110 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4111 | ret = btrfs_del_inode_ref(trans, root, name, name_len, ino, |
| 4112 | dir_ino, &index); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4113 | if (ret) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4114 | btrfs_info(fs_info, |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 4115 | "failed to delete reference to %.*s, inode %llu parent %llu", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 4116 | name_len, name, ino, dir_ino); |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 4117 | btrfs_abort_transaction(trans, ret); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4118 | goto err; |
| 4119 | } |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 4120 | skip_backref: |
Lu Fengqi | 9add294 | 2018-08-01 11:32:26 +0800 | [diff] [blame] | 4121 | ret = btrfs_delete_delayed_dir_index(trans, dir, index); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4122 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 4123 | btrfs_abort_transaction(trans, ret); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4124 | goto err; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4125 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4126 | |
Josef Bacik | 9a35fc9 | 2021-10-05 16:35:24 -0400 | [diff] [blame] | 4127 | btrfs_del_inode_ref_in_log(trans, root, name, name_len, inode, |
| 4128 | dir_ino); |
| 4129 | btrfs_del_dir_entries_in_log(trans, root, name, name_len, dir, index); |
Josef Bacik | 63611e7 | 2019-06-18 10:59:18 -0400 | [diff] [blame] | 4130 | |
| 4131 | /* |
| 4132 | * If we have a pending delayed iput we could end up with the final iput |
| 4133 | * being run in btrfs-cleaner context. If we have enough of these built |
| 4134 | * up we can end up burning a lot of time in btrfs-cleaner without any |
| 4135 | * way to throttle the unlinks. Since we're currently holding a ref on |
| 4136 | * the inode we can run the delayed iput here without any issues as the |
| 4137 | * final iput won't be done until after we drop the ref we're currently |
| 4138 | * holding. |
| 4139 | */ |
| 4140 | btrfs_run_delayed_iput(fs_info, inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4141 | err: |
| 4142 | btrfs_free_path(path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4143 | if (ret) |
| 4144 | goto out; |
| 4145 | |
Nikolay Borisov | 6ef06d2 | 2017-02-20 13:50:34 +0200 | [diff] [blame] | 4146 | 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] | 4147 | inode_inc_iversion(&inode->vfs_inode); |
| 4148 | inode_inc_iversion(&dir->vfs_inode); |
| 4149 | inode->vfs_inode.i_ctime = dir->vfs_inode.i_mtime = |
| 4150 | dir->vfs_inode.i_ctime = current_time(&inode->vfs_inode); |
Nikolay Borisov | 9a56fcd | 2020-11-02 16:48:59 +0200 | [diff] [blame] | 4151 | ret = btrfs_update_inode(trans, root, dir); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4152 | out: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4153 | return ret; |
| 4154 | } |
| 4155 | |
Al Viro | 9298679 | 2011-03-04 17:14:37 +0000 | [diff] [blame] | 4156 | int btrfs_unlink_inode(struct btrfs_trans_handle *trans, |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 4157 | struct btrfs_inode *dir, struct btrfs_inode *inode, |
Al Viro | 9298679 | 2011-03-04 17:14:37 +0000 | [diff] [blame] | 4158 | const char *name, int name_len) |
| 4159 | { |
| 4160 | int ret; |
Filipe Manana | 4467af8 | 2021-10-25 17:31:50 +0100 | [diff] [blame] | 4161 | ret = __btrfs_unlink_inode(trans, dir, inode, name, name_len); |
Al Viro | 9298679 | 2011-03-04 17:14:37 +0000 | [diff] [blame] | 4162 | if (!ret) { |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 4163 | drop_nlink(&inode->vfs_inode); |
Filipe Manana | 4467af8 | 2021-10-25 17:31:50 +0100 | [diff] [blame] | 4164 | ret = btrfs_update_inode(trans, inode->root, inode); |
Al Viro | 9298679 | 2011-03-04 17:14:37 +0000 | [diff] [blame] | 4165 | } |
| 4166 | return ret; |
| 4167 | } |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4168 | |
| 4169 | /* |
| 4170 | * helper to start transaction for unlink and rmdir. |
| 4171 | * |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 4172 | * unlink and rmdir are special in btrfs, they do not always free space, so |
| 4173 | * if we cannot make our reservations the normal way try and see if there is |
| 4174 | * plenty of slack room in the global reserve to migrate, otherwise we cannot |
| 4175 | * allow the unlink to occur. |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4176 | */ |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 4177 | static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir) |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4178 | { |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4179 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4180 | |
Josef Bacik | e70bea5 | 2011-10-11 14:18:24 -0400 | [diff] [blame] | 4181 | /* |
| 4182 | * 1 for the possible orphan item |
| 4183 | * 1 for the dir item |
| 4184 | * 1 for the dir index |
| 4185 | * 1 for the inode ref |
Josef Bacik | e70bea5 | 2011-10-11 14:18:24 -0400 | [diff] [blame] | 4186 | * 1 for the inode |
| 4187 | */ |
Josef Bacik | 7f9fe61 | 2020-03-13 15:58:05 -0400 | [diff] [blame] | 4188 | return btrfs_start_transaction_fallback_global_rsv(root, 5); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4189 | } |
| 4190 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4191 | static int btrfs_unlink(struct inode *dir, struct dentry *dentry) |
| 4192 | { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4193 | struct btrfs_trans_handle *trans; |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 4194 | struct inode *inode = d_inode(dentry); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4195 | int ret; |
| 4196 | |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 4197 | trans = __unlink_start_trans(dir); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4198 | if (IS_ERR(trans)) |
Josef Bacik | 5df6a9f | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4199 | return PTR_ERR(trans); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4200 | |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 4201 | btrfs_record_unlink_dir(trans, BTRFS_I(dir), BTRFS_I(d_inode(dentry)), |
| 4202 | 0); |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 4203 | |
Filipe Manana | 4467af8 | 2021-10-25 17:31:50 +0100 | [diff] [blame] | 4204 | ret = btrfs_unlink_inode(trans, BTRFS_I(dir), |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 4205 | BTRFS_I(d_inode(dentry)), dentry->d_name.name, |
| 4206 | dentry->d_name.len); |
Tsutomu Itoh | b532402 | 2011-07-19 07:27:20 +0000 | [diff] [blame] | 4207 | if (ret) |
| 4208 | goto out; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 4209 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4210 | if (inode->i_nlink == 0) { |
Nikolay Borisov | 73f2e54 | 2017-02-20 13:50:59 +0200 | [diff] [blame] | 4211 | ret = btrfs_orphan_add(trans, BTRFS_I(inode)); |
Tsutomu Itoh | b532402 | 2011-07-19 07:27:20 +0000 | [diff] [blame] | 4212 | if (ret) |
| 4213 | goto out; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4214 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 4215 | |
Tsutomu Itoh | b532402 | 2011-07-19 07:27:20 +0000 | [diff] [blame] | 4216 | out: |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 4217 | btrfs_end_transaction(trans); |
Filipe Manana | 4467af8 | 2021-10-25 17:31:50 +0100 | [diff] [blame] | 4218 | btrfs_btree_balance_dirty(BTRFS_I(dir)->root->fs_info); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4219 | return ret; |
| 4220 | } |
| 4221 | |
Misono Tomohiro | f60a236 | 2018-04-18 11:34:52 +0900 | [diff] [blame] | 4222 | static int btrfs_unlink_subvol(struct btrfs_trans_handle *trans, |
Josef Bacik | 045d396 | 2019-12-18 17:20:27 -0500 | [diff] [blame] | 4223 | struct inode *dir, struct dentry *dentry) |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4224 | { |
Lu Fengqi | 401b3b1 | 2018-08-01 11:32:30 +0800 | [diff] [blame] | 4225 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Josef Bacik | 045d396 | 2019-12-18 17:20:27 -0500 | [diff] [blame] | 4226 | struct btrfs_inode *inode = BTRFS_I(d_inode(dentry)); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4227 | struct btrfs_path *path; |
| 4228 | struct extent_buffer *leaf; |
| 4229 | struct btrfs_dir_item *di; |
| 4230 | struct btrfs_key key; |
Josef Bacik | 045d396 | 2019-12-18 17:20:27 -0500 | [diff] [blame] | 4231 | const char *name = dentry->d_name.name; |
| 4232 | int name_len = dentry->d_name.len; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4233 | u64 index; |
| 4234 | int ret; |
Josef Bacik | 045d396 | 2019-12-18 17:20:27 -0500 | [diff] [blame] | 4235 | u64 objectid; |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 4236 | u64 dir_ino = btrfs_ino(BTRFS_I(dir)); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4237 | |
Josef Bacik | 045d396 | 2019-12-18 17:20:27 -0500 | [diff] [blame] | 4238 | if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID) { |
| 4239 | objectid = inode->root->root_key.objectid; |
| 4240 | } else if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) { |
| 4241 | objectid = inode->location.objectid; |
| 4242 | } else { |
| 4243 | WARN_ON(1); |
| 4244 | return -EINVAL; |
| 4245 | } |
| 4246 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4247 | path = btrfs_alloc_path(); |
| 4248 | if (!path) |
| 4249 | return -ENOMEM; |
| 4250 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4251 | di = btrfs_lookup_dir_item(trans, root, path, dir_ino, |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4252 | name, name_len, -1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4253 | if (IS_ERR_OR_NULL(di)) { |
Liu Bo | 3cf5068 | 2018-09-12 06:06:26 +0800 | [diff] [blame] | 4254 | ret = di ? PTR_ERR(di) : -ENOENT; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4255 | goto out; |
| 4256 | } |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4257 | |
| 4258 | leaf = path->nodes[0]; |
| 4259 | btrfs_dir_item_key_to_cpu(leaf, di, &key); |
| 4260 | WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid); |
| 4261 | ret = btrfs_delete_one_dir_name(trans, root, path, di); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4262 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 4263 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4264 | goto out; |
| 4265 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4266 | btrfs_release_path(path); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4267 | |
Josef Bacik | d49d328 | 2019-12-18 17:20:28 -0500 | [diff] [blame] | 4268 | /* |
| 4269 | * This is a placeholder inode for a subvolume we didn't have a |
| 4270 | * reference to at the time of the snapshot creation. In the meantime |
| 4271 | * we could have renamed the real subvol link into our snapshot, so |
David Sterba | 1a9fd41 | 2021-05-21 17:42:23 +0200 | [diff] [blame] | 4272 | * depending on btrfs_del_root_ref to return -ENOENT here is incorrect. |
Josef Bacik | d49d328 | 2019-12-18 17:20:28 -0500 | [diff] [blame] | 4273 | * Instead simply lookup the dir_index_item for this entry so we can |
| 4274 | * remove it. Otherwise we know we have a ref to the root and we can |
| 4275 | * call btrfs_del_root_ref, and it _shouldn't_ fail. |
| 4276 | */ |
| 4277 | if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) { |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4278 | di = btrfs_search_dir_index_item(root, path, dir_ino, |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4279 | name, name_len); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4280 | if (IS_ERR_OR_NULL(di)) { |
| 4281 | if (!di) |
| 4282 | ret = -ENOENT; |
| 4283 | else |
| 4284 | ret = PTR_ERR(di); |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 4285 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4286 | goto out; |
| 4287 | } |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4288 | |
| 4289 | leaf = path->nodes[0]; |
| 4290 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4291 | index = key.offset; |
Josef Bacik | d49d328 | 2019-12-18 17:20:28 -0500 | [diff] [blame] | 4292 | btrfs_release_path(path); |
| 4293 | } else { |
| 4294 | ret = btrfs_del_root_ref(trans, objectid, |
| 4295 | root->root_key.objectid, dir_ino, |
| 4296 | &index, name, name_len); |
| 4297 | if (ret) { |
| 4298 | btrfs_abort_transaction(trans, ret); |
| 4299 | goto out; |
| 4300 | } |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4301 | } |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4302 | |
Lu Fengqi | 9add294 | 2018-08-01 11:32:26 +0800 | [diff] [blame] | 4303 | ret = btrfs_delete_delayed_dir_index(trans, BTRFS_I(dir), index); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4304 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 4305 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4306 | goto out; |
| 4307 | } |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 4308 | |
Nikolay Borisov | 6ef06d2 | 2017-02-20 13:50:34 +0200 | [diff] [blame] | 4309 | 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] | 4310 | inode_inc_iversion(dir); |
Deepa Dinamani | c2050a4 | 2016-09-14 07:48:06 -0700 | [diff] [blame] | 4311 | dir->i_mtime = dir->i_ctime = current_time(dir); |
Nikolay Borisov | 729f796 | 2020-11-02 16:49:06 +0200 | [diff] [blame] | 4312 | ret = btrfs_update_inode_fallback(trans, root, BTRFS_I(dir)); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4313 | if (ret) |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 4314 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4315 | out: |
Josef Bacik | 71d7aed | 2011-06-14 14:24:32 -0400 | [diff] [blame] | 4316 | btrfs_free_path(path); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4317 | return ret; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4318 | } |
| 4319 | |
Misono Tomohiro | ec42f16 | 2018-04-18 11:34:13 +0900 | [diff] [blame] | 4320 | /* |
| 4321 | * Helper to check if the subvolume references other subvolumes or if it's |
| 4322 | * default. |
| 4323 | */ |
Misono Tomohiro | f60a236 | 2018-04-18 11:34:52 +0900 | [diff] [blame] | 4324 | static noinline int may_destroy_subvol(struct btrfs_root *root) |
Misono Tomohiro | ec42f16 | 2018-04-18 11:34:13 +0900 | [diff] [blame] | 4325 | { |
| 4326 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 4327 | struct btrfs_path *path; |
| 4328 | struct btrfs_dir_item *di; |
| 4329 | struct btrfs_key key; |
| 4330 | u64 dir_id; |
| 4331 | int ret; |
| 4332 | |
| 4333 | path = btrfs_alloc_path(); |
| 4334 | if (!path) |
| 4335 | return -ENOMEM; |
| 4336 | |
| 4337 | /* Make sure this root isn't set as the default subvol */ |
| 4338 | dir_id = btrfs_super_root_dir(fs_info->super_copy); |
| 4339 | di = btrfs_lookup_dir_item(NULL, fs_info->tree_root, path, |
| 4340 | dir_id, "default", 7, 0); |
| 4341 | if (di && !IS_ERR(di)) { |
| 4342 | btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key); |
| 4343 | if (key.objectid == root->root_key.objectid) { |
| 4344 | ret = -EPERM; |
| 4345 | btrfs_err(fs_info, |
| 4346 | "deleting default subvolume %llu is not allowed", |
| 4347 | key.objectid); |
| 4348 | goto out; |
| 4349 | } |
| 4350 | btrfs_release_path(path); |
| 4351 | } |
| 4352 | |
| 4353 | key.objectid = root->root_key.objectid; |
| 4354 | key.type = BTRFS_ROOT_REF_KEY; |
| 4355 | key.offset = (u64)-1; |
| 4356 | |
| 4357 | ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0); |
| 4358 | if (ret < 0) |
| 4359 | goto out; |
| 4360 | BUG_ON(ret == 0); |
| 4361 | |
| 4362 | ret = 0; |
| 4363 | if (path->slots[0] > 0) { |
| 4364 | path->slots[0]--; |
| 4365 | btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]); |
| 4366 | if (key.objectid == root->root_key.objectid && |
| 4367 | key.type == BTRFS_ROOT_REF_KEY) |
| 4368 | ret = -ENOTEMPTY; |
| 4369 | } |
| 4370 | out: |
| 4371 | btrfs_free_path(path); |
| 4372 | return ret; |
| 4373 | } |
| 4374 | |
Nikolay Borisov | 20a6800 | 2018-04-27 14:36:24 +0300 | [diff] [blame] | 4375 | /* Delete all dentries for inodes belonging to the root */ |
| 4376 | static void btrfs_prune_dentries(struct btrfs_root *root) |
| 4377 | { |
| 4378 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 4379 | struct rb_node *node; |
| 4380 | struct rb_node *prev; |
| 4381 | struct btrfs_inode *entry; |
| 4382 | struct inode *inode; |
| 4383 | u64 objectid = 0; |
| 4384 | |
Josef Bacik | 8496153 | 2021-10-05 16:35:25 -0400 | [diff] [blame] | 4385 | if (!BTRFS_FS_ERROR(fs_info)) |
Nikolay Borisov | 20a6800 | 2018-04-27 14:36:24 +0300 | [diff] [blame] | 4386 | WARN_ON(btrfs_root_refs(&root->root_item) != 0); |
| 4387 | |
| 4388 | spin_lock(&root->inode_lock); |
| 4389 | again: |
| 4390 | node = root->inode_tree.rb_node; |
| 4391 | prev = NULL; |
| 4392 | while (node) { |
| 4393 | prev = node; |
| 4394 | entry = rb_entry(node, struct btrfs_inode, rb_node); |
| 4395 | |
David Sterba | 3750851 | 2018-06-29 10:56:40 +0200 | [diff] [blame] | 4396 | if (objectid < btrfs_ino(entry)) |
Nikolay Borisov | 20a6800 | 2018-04-27 14:36:24 +0300 | [diff] [blame] | 4397 | node = node->rb_left; |
David Sterba | 3750851 | 2018-06-29 10:56:40 +0200 | [diff] [blame] | 4398 | else if (objectid > btrfs_ino(entry)) |
Nikolay Borisov | 20a6800 | 2018-04-27 14:36:24 +0300 | [diff] [blame] | 4399 | node = node->rb_right; |
| 4400 | else |
| 4401 | break; |
| 4402 | } |
| 4403 | if (!node) { |
| 4404 | while (prev) { |
| 4405 | entry = rb_entry(prev, struct btrfs_inode, rb_node); |
David Sterba | 3750851 | 2018-06-29 10:56:40 +0200 | [diff] [blame] | 4406 | if (objectid <= btrfs_ino(entry)) { |
Nikolay Borisov | 20a6800 | 2018-04-27 14:36:24 +0300 | [diff] [blame] | 4407 | node = prev; |
| 4408 | break; |
| 4409 | } |
| 4410 | prev = rb_next(prev); |
| 4411 | } |
| 4412 | } |
| 4413 | while (node) { |
| 4414 | entry = rb_entry(node, struct btrfs_inode, rb_node); |
David Sterba | 3750851 | 2018-06-29 10:56:40 +0200 | [diff] [blame] | 4415 | objectid = btrfs_ino(entry) + 1; |
Nikolay Borisov | 20a6800 | 2018-04-27 14:36:24 +0300 | [diff] [blame] | 4416 | inode = igrab(&entry->vfs_inode); |
| 4417 | if (inode) { |
| 4418 | spin_unlock(&root->inode_lock); |
| 4419 | if (atomic_read(&inode->i_count) > 1) |
| 4420 | d_prune_aliases(inode); |
| 4421 | /* |
| 4422 | * btrfs_drop_inode will have it removed from the inode |
| 4423 | * cache when its usage count hits zero. |
| 4424 | */ |
| 4425 | iput(inode); |
| 4426 | cond_resched(); |
| 4427 | spin_lock(&root->inode_lock); |
| 4428 | goto again; |
| 4429 | } |
| 4430 | |
| 4431 | if (cond_resched_lock(&root->inode_lock)) |
| 4432 | goto again; |
| 4433 | |
| 4434 | node = rb_next(node); |
| 4435 | } |
| 4436 | spin_unlock(&root->inode_lock); |
| 4437 | } |
| 4438 | |
Misono Tomohiro | f60a236 | 2018-04-18 11:34:52 +0900 | [diff] [blame] | 4439 | int btrfs_delete_subvolume(struct inode *dir, struct dentry *dentry) |
| 4440 | { |
| 4441 | struct btrfs_fs_info *fs_info = btrfs_sb(dentry->d_sb); |
| 4442 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 4443 | struct inode *inode = d_inode(dentry); |
| 4444 | struct btrfs_root *dest = BTRFS_I(inode)->root; |
| 4445 | struct btrfs_trans_handle *trans; |
| 4446 | struct btrfs_block_rsv block_rsv; |
| 4447 | u64 root_flags; |
Misono Tomohiro | f60a236 | 2018-04-18 11:34:52 +0900 | [diff] [blame] | 4448 | int ret; |
Misono Tomohiro | f60a236 | 2018-04-18 11:34:52 +0900 | [diff] [blame] | 4449 | |
| 4450 | /* |
| 4451 | * Don't allow to delete a subvolume with send in progress. This is |
| 4452 | * inside the inode lock so the error handling that has to drop the bit |
| 4453 | * again is not run concurrently. |
| 4454 | */ |
| 4455 | spin_lock(&dest->root_item_lock); |
Lu Fengqi | a7176f7 | 2018-08-04 21:10:53 +0800 | [diff] [blame] | 4456 | if (dest->send_in_progress) { |
Misono Tomohiro | f60a236 | 2018-04-18 11:34:52 +0900 | [diff] [blame] | 4457 | spin_unlock(&dest->root_item_lock); |
| 4458 | btrfs_warn(fs_info, |
| 4459 | "attempt to delete subvolume %llu during send", |
| 4460 | dest->root_key.objectid); |
| 4461 | return -EPERM; |
| 4462 | } |
Lu Fengqi | a7176f7 | 2018-08-04 21:10:53 +0800 | [diff] [blame] | 4463 | root_flags = btrfs_root_flags(&dest->root_item); |
| 4464 | btrfs_set_root_flags(&dest->root_item, |
| 4465 | root_flags | BTRFS_ROOT_SUBVOL_DEAD); |
| 4466 | spin_unlock(&dest->root_item_lock); |
Misono Tomohiro | f60a236 | 2018-04-18 11:34:52 +0900 | [diff] [blame] | 4467 | |
| 4468 | down_write(&fs_info->subvol_sem); |
| 4469 | |
Nikolay Borisov | ee0d904 | 2020-11-24 17:49:30 +0200 | [diff] [blame] | 4470 | ret = may_destroy_subvol(dest); |
| 4471 | if (ret) |
Misono Tomohiro | f60a236 | 2018-04-18 11:34:52 +0900 | [diff] [blame] | 4472 | goto out_up_write; |
| 4473 | |
| 4474 | btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP); |
| 4475 | /* |
| 4476 | * One for dir inode, |
| 4477 | * two for dir entries, |
| 4478 | * two for root ref/backref. |
| 4479 | */ |
Nikolay Borisov | ee0d904 | 2020-11-24 17:49:30 +0200 | [diff] [blame] | 4480 | ret = btrfs_subvolume_reserve_metadata(root, &block_rsv, 5, true); |
| 4481 | if (ret) |
Misono Tomohiro | f60a236 | 2018-04-18 11:34:52 +0900 | [diff] [blame] | 4482 | goto out_up_write; |
| 4483 | |
| 4484 | trans = btrfs_start_transaction(root, 0); |
| 4485 | if (IS_ERR(trans)) { |
Nikolay Borisov | ee0d904 | 2020-11-24 17:49:30 +0200 | [diff] [blame] | 4486 | ret = PTR_ERR(trans); |
Misono Tomohiro | f60a236 | 2018-04-18 11:34:52 +0900 | [diff] [blame] | 4487 | goto out_release; |
| 4488 | } |
| 4489 | trans->block_rsv = &block_rsv; |
| 4490 | trans->bytes_reserved = block_rsv.size; |
| 4491 | |
| 4492 | btrfs_record_snapshot_destroy(trans, BTRFS_I(dir)); |
| 4493 | |
Josef Bacik | 045d396 | 2019-12-18 17:20:27 -0500 | [diff] [blame] | 4494 | ret = btrfs_unlink_subvol(trans, dir, dentry); |
Misono Tomohiro | f60a236 | 2018-04-18 11:34:52 +0900 | [diff] [blame] | 4495 | if (ret) { |
Misono Tomohiro | f60a236 | 2018-04-18 11:34:52 +0900 | [diff] [blame] | 4496 | btrfs_abort_transaction(trans, ret); |
| 4497 | goto out_end_trans; |
| 4498 | } |
| 4499 | |
Josef Bacik | 2731f51 | 2021-03-12 15:25:04 -0500 | [diff] [blame] | 4500 | ret = btrfs_record_root_in_trans(trans, dest); |
| 4501 | if (ret) { |
| 4502 | btrfs_abort_transaction(trans, ret); |
| 4503 | goto out_end_trans; |
| 4504 | } |
Misono Tomohiro | f60a236 | 2018-04-18 11:34:52 +0900 | [diff] [blame] | 4505 | |
| 4506 | memset(&dest->root_item.drop_progress, 0, |
| 4507 | sizeof(dest->root_item.drop_progress)); |
David Sterba | c842268 | 2020-09-15 21:44:52 +0200 | [diff] [blame] | 4508 | btrfs_set_root_drop_level(&dest->root_item, 0); |
Misono Tomohiro | f60a236 | 2018-04-18 11:34:52 +0900 | [diff] [blame] | 4509 | btrfs_set_root_refs(&dest->root_item, 0); |
| 4510 | |
| 4511 | if (!test_and_set_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &dest->state)) { |
| 4512 | ret = btrfs_insert_orphan_item(trans, |
| 4513 | fs_info->tree_root, |
| 4514 | dest->root_key.objectid); |
| 4515 | if (ret) { |
| 4516 | btrfs_abort_transaction(trans, ret); |
Misono Tomohiro | f60a236 | 2018-04-18 11:34:52 +0900 | [diff] [blame] | 4517 | goto out_end_trans; |
| 4518 | } |
| 4519 | } |
| 4520 | |
Lu Fengqi | d195779 | 2018-05-29 15:01:54 +0800 | [diff] [blame] | 4521 | ret = btrfs_uuid_tree_remove(trans, dest->root_item.uuid, |
Misono Tomohiro | f60a236 | 2018-04-18 11:34:52 +0900 | [diff] [blame] | 4522 | BTRFS_UUID_KEY_SUBVOL, |
| 4523 | dest->root_key.objectid); |
| 4524 | if (ret && ret != -ENOENT) { |
| 4525 | btrfs_abort_transaction(trans, ret); |
Misono Tomohiro | f60a236 | 2018-04-18 11:34:52 +0900 | [diff] [blame] | 4526 | goto out_end_trans; |
| 4527 | } |
| 4528 | if (!btrfs_is_empty_uuid(dest->root_item.received_uuid)) { |
Lu Fengqi | d195779 | 2018-05-29 15:01:54 +0800 | [diff] [blame] | 4529 | ret = btrfs_uuid_tree_remove(trans, |
Misono Tomohiro | f60a236 | 2018-04-18 11:34:52 +0900 | [diff] [blame] | 4530 | dest->root_item.received_uuid, |
| 4531 | BTRFS_UUID_KEY_RECEIVED_SUBVOL, |
| 4532 | dest->root_key.objectid); |
| 4533 | if (ret && ret != -ENOENT) { |
| 4534 | btrfs_abort_transaction(trans, ret); |
Misono Tomohiro | f60a236 | 2018-04-18 11:34:52 +0900 | [diff] [blame] | 4535 | goto out_end_trans; |
| 4536 | } |
| 4537 | } |
| 4538 | |
Qu Wenruo | 082b6c9 | 2020-06-16 10:17:37 +0800 | [diff] [blame] | 4539 | free_anon_bdev(dest->anon_dev); |
| 4540 | dest->anon_dev = 0; |
Misono Tomohiro | f60a236 | 2018-04-18 11:34:52 +0900 | [diff] [blame] | 4541 | out_end_trans: |
| 4542 | trans->block_rsv = NULL; |
| 4543 | trans->bytes_reserved = 0; |
| 4544 | ret = btrfs_end_transaction(trans); |
Misono Tomohiro | f60a236 | 2018-04-18 11:34:52 +0900 | [diff] [blame] | 4545 | inode->i_flags |= S_DEAD; |
| 4546 | out_release: |
Qu Wenruo | e85fde5 | 2020-07-24 14:46:10 +0800 | [diff] [blame] | 4547 | btrfs_subvolume_release_metadata(root, &block_rsv); |
Misono Tomohiro | f60a236 | 2018-04-18 11:34:52 +0900 | [diff] [blame] | 4548 | out_up_write: |
| 4549 | up_write(&fs_info->subvol_sem); |
Nikolay Borisov | ee0d904 | 2020-11-24 17:49:30 +0200 | [diff] [blame] | 4550 | if (ret) { |
Misono Tomohiro | f60a236 | 2018-04-18 11:34:52 +0900 | [diff] [blame] | 4551 | spin_lock(&dest->root_item_lock); |
| 4552 | root_flags = btrfs_root_flags(&dest->root_item); |
| 4553 | btrfs_set_root_flags(&dest->root_item, |
| 4554 | root_flags & ~BTRFS_ROOT_SUBVOL_DEAD); |
| 4555 | spin_unlock(&dest->root_item_lock); |
| 4556 | } else { |
| 4557 | d_invalidate(dentry); |
Nikolay Borisov | 20a6800 | 2018-04-27 14:36:24 +0300 | [diff] [blame] | 4558 | btrfs_prune_dentries(dest); |
Misono Tomohiro | f60a236 | 2018-04-18 11:34:52 +0900 | [diff] [blame] | 4559 | ASSERT(dest->send_in_progress == 0); |
Misono Tomohiro | f60a236 | 2018-04-18 11:34:52 +0900 | [diff] [blame] | 4560 | } |
| 4561 | |
Nikolay Borisov | ee0d904 | 2020-11-24 17:49:30 +0200 | [diff] [blame] | 4562 | return ret; |
Misono Tomohiro | f60a236 | 2018-04-18 11:34:52 +0900 | [diff] [blame] | 4563 | } |
| 4564 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4565 | static int btrfs_rmdir(struct inode *dir, struct dentry *dentry) |
| 4566 | { |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 4567 | struct inode *inode = d_inode(dentry); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 4568 | int err = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4569 | struct btrfs_trans_handle *trans; |
Filipe Manana | 44f714d | 2016-06-06 16:11:13 +0100 | [diff] [blame] | 4570 | u64 last_unlink_trans; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4571 | |
David Sterba | b3ae244 | 2012-09-13 16:04:34 -0600 | [diff] [blame] | 4572 | if (inode->i_size > BTRFS_EMPTY_DIR_SIZE) |
Yan | 134d451 | 2007-10-25 15:49:25 -0400 | [diff] [blame] | 4573 | return -ENOTEMPTY; |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 4574 | if (btrfs_ino(BTRFS_I(inode)) == BTRFS_FIRST_FREE_OBJECTID) |
Misono Tomohiro | a79a464 | 2018-04-18 11:35:31 +0900 | [diff] [blame] | 4575 | return btrfs_delete_subvolume(dir, dentry); |
Yan | 134d451 | 2007-10-25 15:49:25 -0400 | [diff] [blame] | 4576 | |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 4577 | trans = __unlink_start_trans(dir); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4578 | if (IS_ERR(trans)) |
Josef Bacik | 5df6a9f | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4579 | return PTR_ERR(trans); |
Josef Bacik | 5df6a9f | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4580 | |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 4581 | if (unlikely(btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) { |
Josef Bacik | 045d396 | 2019-12-18 17:20:27 -0500 | [diff] [blame] | 4582 | err = btrfs_unlink_subvol(trans, dir, dentry); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4583 | goto out; |
| 4584 | } |
| 4585 | |
Nikolay Borisov | 73f2e54 | 2017-02-20 13:50:59 +0200 | [diff] [blame] | 4586 | err = btrfs_orphan_add(trans, BTRFS_I(inode)); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 4587 | if (err) |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4588 | goto out; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 4589 | |
Filipe Manana | 44f714d | 2016-06-06 16:11:13 +0100 | [diff] [blame] | 4590 | last_unlink_trans = BTRFS_I(inode)->last_unlink_trans; |
| 4591 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4592 | /* now the directory is empty */ |
Filipe Manana | 4467af8 | 2021-10-25 17:31:50 +0100 | [diff] [blame] | 4593 | err = btrfs_unlink_inode(trans, BTRFS_I(dir), |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 4594 | BTRFS_I(d_inode(dentry)), dentry->d_name.name, |
| 4595 | dentry->d_name.len); |
Filipe Manana | 44f714d | 2016-06-06 16:11:13 +0100 | [diff] [blame] | 4596 | if (!err) { |
Nikolay Borisov | 6ef06d2 | 2017-02-20 13:50:34 +0200 | [diff] [blame] | 4597 | btrfs_i_size_write(BTRFS_I(inode), 0); |
Filipe Manana | 44f714d | 2016-06-06 16:11:13 +0100 | [diff] [blame] | 4598 | /* |
| 4599 | * Propagate the last_unlink_trans value of the deleted dir to |
| 4600 | * its parent directory. This is to prevent an unrecoverable |
| 4601 | * log tree in the case we do something like this: |
| 4602 | * 1) create dir foo |
| 4603 | * 2) create snapshot under dir foo |
| 4604 | * 3) delete the snapshot |
| 4605 | * 4) rmdir foo |
| 4606 | * 5) mkdir foo |
| 4607 | * 6) fsync foo or some file inside foo |
| 4608 | */ |
| 4609 | if (last_unlink_trans >= trans->transid) |
| 4610 | BTRFS_I(dir)->last_unlink_trans = last_unlink_trans; |
| 4611 | } |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4612 | out: |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 4613 | btrfs_end_transaction(trans); |
Filipe Manana | 4467af8 | 2021-10-25 17:31:50 +0100 | [diff] [blame] | 4614 | btrfs_btree_balance_dirty(BTRFS_I(dir)->root->fs_info); |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 4615 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4616 | return err; |
| 4617 | } |
| 4618 | |
Josef Bacik | ddfae63 | 2017-10-19 14:16:02 -0400 | [diff] [blame] | 4619 | /* |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4620 | * btrfs_truncate_block - read, zero a chunk and write a block |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 4621 | * @inode - inode that we're zeroing |
| 4622 | * @from - the offset to start zeroing |
| 4623 | * @len - the length to zero, 0 to zero the entire range respective to the |
| 4624 | * offset |
| 4625 | * @front - zero up to the offset instead of from the offset on |
| 4626 | * |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4627 | * 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] | 4628 | * 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] | 4629 | */ |
Nikolay Borisov | 217f42e | 2020-11-02 16:49:03 +0200 | [diff] [blame] | 4630 | int btrfs_truncate_block(struct btrfs_inode *inode, loff_t from, loff_t len, |
| 4631 | int front) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4632 | { |
Nikolay Borisov | 217f42e | 2020-11-02 16:49:03 +0200 | [diff] [blame] | 4633 | struct btrfs_fs_info *fs_info = inode->root->fs_info; |
| 4634 | struct address_space *mapping = inode->vfs_inode.i_mapping; |
| 4635 | struct extent_io_tree *io_tree = &inode->io_tree; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4636 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4637 | struct extent_state *cached_state = NULL; |
Qu Wenruo | 364ecf3 | 2017-02-27 15:10:38 +0800 | [diff] [blame] | 4638 | struct extent_changeset *data_reserved = NULL; |
Qu Wenruo | 6d4572a | 2020-06-24 07:23:50 +0800 | [diff] [blame] | 4639 | bool only_release_metadata = false; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4640 | u32 blocksize = fs_info->sectorsize; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 4641 | pgoff_t index = from >> PAGE_SHIFT; |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4642 | unsigned offset = from & (blocksize - 1); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4643 | struct page *page; |
Josef Bacik | 3b16a4e | 2011-09-21 15:05:58 -0400 | [diff] [blame] | 4644 | gfp_t mask = btrfs_alloc_write_mask(mapping); |
Qu Wenruo | 6d4572a | 2020-06-24 07:23:50 +0800 | [diff] [blame] | 4645 | size_t write_bytes = blocksize; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4646 | int ret = 0; |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4647 | u64 block_start; |
| 4648 | u64 block_end; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4649 | |
Nikolay Borisov | b03ebd9 | 2018-01-18 14:47:06 +0200 | [diff] [blame] | 4650 | if (IS_ALIGNED(offset, blocksize) && |
| 4651 | (!len || IS_ALIGNED(len, blocksize))) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4652 | goto out; |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4653 | |
Josef Bacik | 8b62f87 | 2017-10-19 14:15:55 -0400 | [diff] [blame] | 4654 | block_start = round_down(from, blocksize); |
| 4655 | block_end = block_start + blocksize - 1; |
| 4656 | |
Nikolay Borisov | 217f42e | 2020-11-02 16:49:03 +0200 | [diff] [blame] | 4657 | ret = btrfs_check_data_free_space(inode, &data_reserved, block_start, |
| 4658 | blocksize); |
Qu Wenruo | 6d4572a | 2020-06-24 07:23:50 +0800 | [diff] [blame] | 4659 | if (ret < 0) { |
Nikolay Borisov | 217f42e | 2020-11-02 16:49:03 +0200 | [diff] [blame] | 4660 | if (btrfs_check_nocow_lock(inode, block_start, &write_bytes) > 0) { |
Qu Wenruo | 6d4572a | 2020-06-24 07:23:50 +0800 | [diff] [blame] | 4661 | /* For nocow case, no need to reserve data space */ |
| 4662 | only_release_metadata = true; |
| 4663 | } else { |
| 4664 | goto out; |
| 4665 | } |
| 4666 | } |
Nikolay Borisov | 217f42e | 2020-11-02 16:49:03 +0200 | [diff] [blame] | 4667 | ret = btrfs_delalloc_reserve_metadata(inode, blocksize); |
Qu Wenruo | 6d4572a | 2020-06-24 07:23:50 +0800 | [diff] [blame] | 4668 | if (ret < 0) { |
| 4669 | if (!only_release_metadata) |
Nikolay Borisov | 217f42e | 2020-11-02 16:49:03 +0200 | [diff] [blame] | 4670 | btrfs_free_reserved_data_space(inode, data_reserved, |
| 4671 | block_start, blocksize); |
Qu Wenruo | 6d4572a | 2020-06-24 07:23:50 +0800 | [diff] [blame] | 4672 | goto out; |
| 4673 | } |
Chris Mason | 211c17f | 2008-05-15 09:13:45 -0400 | [diff] [blame] | 4674 | again: |
Josef Bacik | 3b16a4e | 2011-09-21 15:05:58 -0400 | [diff] [blame] | 4675 | page = find_or_create_page(mapping, index, mask); |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 4676 | if (!page) { |
Nikolay Borisov | 217f42e | 2020-11-02 16:49:03 +0200 | [diff] [blame] | 4677 | btrfs_delalloc_release_space(inode, data_reserved, block_start, |
| 4678 | blocksize, true); |
| 4679 | btrfs_delalloc_release_extents(inode, blocksize); |
Miao Xie | ac6a2b3 | 2012-12-05 10:56:13 +0000 | [diff] [blame] | 4680 | ret = -ENOMEM; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4681 | goto out; |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 4682 | } |
Qu Wenruo | 32443de | 2021-01-26 16:34:00 +0800 | [diff] [blame] | 4683 | ret = set_page_extent_mapped(page); |
| 4684 | if (ret < 0) |
| 4685 | goto out_unlock; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4686 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4687 | if (!PageUptodate(page)) { |
| 4688 | ret = btrfs_readpage(NULL, page); |
| 4689 | lock_page(page); |
Chris Mason | 211c17f | 2008-05-15 09:13:45 -0400 | [diff] [blame] | 4690 | if (page->mapping != mapping) { |
| 4691 | unlock_page(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 4692 | put_page(page); |
Chris Mason | 211c17f | 2008-05-15 09:13:45 -0400 | [diff] [blame] | 4693 | goto again; |
| 4694 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4695 | if (!PageUptodate(page)) { |
| 4696 | ret = -EIO; |
Chris Mason | 8964222 | 2008-07-24 09:41:53 -0400 | [diff] [blame] | 4697 | goto out_unlock; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4698 | } |
| 4699 | } |
Chris Mason | 211c17f | 2008-05-15 09:13:45 -0400 | [diff] [blame] | 4700 | wait_on_page_writeback(page); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4701 | |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4702 | lock_extent_bits(io_tree, block_start, block_end, &cached_state); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4703 | |
Nikolay Borisov | 217f42e | 2020-11-02 16:49:03 +0200 | [diff] [blame] | 4704 | ordered = btrfs_lookup_ordered_extent(inode, block_start); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4705 | if (ordered) { |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4706 | unlock_extent_cached(io_tree, block_start, block_end, |
David Sterba | e43bbe5 | 2017-12-12 21:43:52 +0100 | [diff] [blame] | 4707 | &cached_state); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4708 | unlock_page(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 4709 | put_page(page); |
Nikolay Borisov | c0a4360 | 2020-09-18 12:15:53 +0300 | [diff] [blame] | 4710 | btrfs_start_ordered_extent(ordered, 1); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4711 | btrfs_put_ordered_extent(ordered); |
| 4712 | goto again; |
| 4713 | } |
| 4714 | |
Nikolay Borisov | 217f42e | 2020-11-02 16:49:03 +0200 | [diff] [blame] | 4715 | clear_extent_bit(&inode->io_tree, block_start, block_end, |
Omar Sandoval | e182163 | 2019-08-15 14:04:04 -0700 | [diff] [blame] | 4716 | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, |
| 4717 | 0, 0, &cached_state); |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 4718 | |
Nikolay Borisov | 217f42e | 2020-11-02 16:49:03 +0200 | [diff] [blame] | 4719 | ret = btrfs_set_extent_delalloc(inode, block_start, block_end, 0, |
Nikolay Borisov | 330a582 | 2019-07-17 16:18:17 +0300 | [diff] [blame] | 4720 | &cached_state); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4721 | if (ret) { |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4722 | unlock_extent_cached(io_tree, block_start, block_end, |
David Sterba | e43bbe5 | 2017-12-12 21:43:52 +0100 | [diff] [blame] | 4723 | &cached_state); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4724 | goto out_unlock; |
| 4725 | } |
| 4726 | |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4727 | if (offset != blocksize) { |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 4728 | if (!len) |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4729 | len = blocksize - offset; |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 4730 | if (front) |
Ira Weiny | d048b9c | 2021-05-04 18:40:07 -0700 | [diff] [blame] | 4731 | memzero_page(page, (block_start - page_offset(page)), |
| 4732 | offset); |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 4733 | else |
Ira Weiny | d048b9c | 2021-05-04 18:40:07 -0700 | [diff] [blame] | 4734 | memzero_page(page, (block_start - page_offset(page)) + offset, |
| 4735 | len); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4736 | flush_dcache_page(page); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4737 | } |
Qu Wenruo | e4f9434 | 2021-09-27 15:21:49 +0800 | [diff] [blame] | 4738 | btrfs_page_clear_checked(fs_info, page, block_start, |
| 4739 | block_end + 1 - block_start); |
Qu Wenruo | 6c9ac8b | 2021-05-31 16:50:51 +0800 | [diff] [blame] | 4740 | 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] | 4741 | unlock_extent_cached(io_tree, block_start, block_end, &cached_state); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4742 | |
Qu Wenruo | 6d4572a | 2020-06-24 07:23:50 +0800 | [diff] [blame] | 4743 | if (only_release_metadata) |
Nikolay Borisov | 217f42e | 2020-11-02 16:49:03 +0200 | [diff] [blame] | 4744 | set_extent_bit(&inode->io_tree, block_start, block_end, |
Nikolay Borisov | 1cab5e7 | 2020-11-05 11:08:00 +0200 | [diff] [blame] | 4745 | EXTENT_NORESERVE, 0, NULL, NULL, GFP_NOFS, NULL); |
Qu Wenruo | 6d4572a | 2020-06-24 07:23:50 +0800 | [diff] [blame] | 4746 | |
Chris Mason | 8964222 | 2008-07-24 09:41:53 -0400 | [diff] [blame] | 4747 | out_unlock: |
Qu Wenruo | 6d4572a | 2020-06-24 07:23:50 +0800 | [diff] [blame] | 4748 | if (ret) { |
| 4749 | if (only_release_metadata) |
Nikolay Borisov | 217f42e | 2020-11-02 16:49:03 +0200 | [diff] [blame] | 4750 | btrfs_delalloc_release_metadata(inode, blocksize, true); |
Qu Wenruo | 6d4572a | 2020-06-24 07:23:50 +0800 | [diff] [blame] | 4751 | else |
Nikolay Borisov | 217f42e | 2020-11-02 16:49:03 +0200 | [diff] [blame] | 4752 | btrfs_delalloc_release_space(inode, data_reserved, |
Qu Wenruo | 6d4572a | 2020-06-24 07:23:50 +0800 | [diff] [blame] | 4753 | block_start, blocksize, true); |
| 4754 | } |
Nikolay Borisov | 217f42e | 2020-11-02 16:49:03 +0200 | [diff] [blame] | 4755 | btrfs_delalloc_release_extents(inode, blocksize); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4756 | unlock_page(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 4757 | put_page(page); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4758 | out: |
Qu Wenruo | 6d4572a | 2020-06-24 07:23:50 +0800 | [diff] [blame] | 4759 | if (only_release_metadata) |
Nikolay Borisov | 217f42e | 2020-11-02 16:49:03 +0200 | [diff] [blame] | 4760 | btrfs_check_nocow_unlock(inode); |
Qu Wenruo | 364ecf3 | 2017-02-27 15:10:38 +0800 | [diff] [blame] | 4761 | extent_changeset_free(data_reserved); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4762 | return ret; |
| 4763 | } |
| 4764 | |
Nikolay Borisov | a4ba6cc | 2020-11-02 16:49:00 +0200 | [diff] [blame] | 4765 | 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] | 4766 | u64 offset, u64 len) |
| 4767 | { |
Nikolay Borisov | a4ba6cc | 2020-11-02 16:49:00 +0200 | [diff] [blame] | 4768 | struct btrfs_fs_info *fs_info = root->fs_info; |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4769 | struct btrfs_trans_handle *trans; |
Filipe Manana | 5893dfb | 2020-11-04 11:07:32 +0000 | [diff] [blame] | 4770 | struct btrfs_drop_extents_args drop_args = { 0 }; |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4771 | int ret; |
| 4772 | |
| 4773 | /* |
Filipe Manana | cceaa89 | 2021-07-20 16:03:40 +0100 | [diff] [blame] | 4774 | * If NO_HOLES is enabled, we don't need to do anything. |
| 4775 | * Later, up in the call chain, either btrfs_set_inode_last_sub_trans() |
| 4776 | * or btrfs_update_inode() will be called, which guarantee that the next |
| 4777 | * fsync will know this inode was changed and needs to be logged. |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4778 | */ |
Filipe Manana | cceaa89 | 2021-07-20 16:03:40 +0100 | [diff] [blame] | 4779 | if (btrfs_fs_incompat(fs_info, NO_HOLES)) |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4780 | return 0; |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4781 | |
| 4782 | /* |
| 4783 | * 1 - for the one we're dropping |
| 4784 | * 1 - for the one we're adding |
| 4785 | * 1 - for updating the inode. |
| 4786 | */ |
| 4787 | trans = btrfs_start_transaction(root, 3); |
| 4788 | if (IS_ERR(trans)) |
| 4789 | return PTR_ERR(trans); |
| 4790 | |
Filipe Manana | 5893dfb | 2020-11-04 11:07:32 +0000 | [diff] [blame] | 4791 | drop_args.start = offset; |
| 4792 | drop_args.end = offset + len; |
| 4793 | drop_args.drop_cache = true; |
| 4794 | |
Nikolay Borisov | a4ba6cc | 2020-11-02 16:49:00 +0200 | [diff] [blame] | 4795 | ret = btrfs_drop_extents(trans, root, inode, &drop_args); |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4796 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 4797 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 4798 | btrfs_end_transaction(trans); |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4799 | return ret; |
| 4800 | } |
| 4801 | |
Nikolay Borisov | a4ba6cc | 2020-11-02 16:49:00 +0200 | [diff] [blame] | 4802 | ret = btrfs_insert_file_extent(trans, root, btrfs_ino(inode), |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 4803 | offset, 0, 0, len, 0, len, 0, 0, 0); |
Filipe Manana | 2766ff6 | 2020-11-04 11:07:34 +0000 | [diff] [blame] | 4804 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 4805 | btrfs_abort_transaction(trans, ret); |
Filipe Manana | 2766ff6 | 2020-11-04 11:07:34 +0000 | [diff] [blame] | 4806 | } else { |
Nikolay Borisov | a4ba6cc | 2020-11-02 16:49:00 +0200 | [diff] [blame] | 4807 | btrfs_update_inode_bytes(inode, 0, drop_args.bytes_found); |
| 4808 | btrfs_update_inode(trans, root, inode); |
Filipe Manana | 2766ff6 | 2020-11-04 11:07:34 +0000 | [diff] [blame] | 4809 | } |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 4810 | btrfs_end_transaction(trans); |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4811 | return ret; |
| 4812 | } |
| 4813 | |
Josef Bacik | 695a0d0 | 2011-03-04 15:46:53 -0500 | [diff] [blame] | 4814 | /* |
| 4815 | * This function puts in dummy file extents for the area we're creating a hole |
| 4816 | * for. So if we are truncating this file to a larger size we need to insert |
| 4817 | * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for |
| 4818 | * the range between oldsize and size |
| 4819 | */ |
Nikolay Borisov | b06359a | 2020-11-02 16:49:04 +0200 | [diff] [blame] | 4820 | 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] | 4821 | { |
Nikolay Borisov | b06359a | 2020-11-02 16:49:04 +0200 | [diff] [blame] | 4822 | struct btrfs_root *root = inode->root; |
| 4823 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 4824 | struct extent_io_tree *io_tree = &inode->io_tree; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4825 | struct extent_map *em = NULL; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4826 | struct extent_state *cached_state = NULL; |
Nikolay Borisov | b06359a | 2020-11-02 16:49:04 +0200 | [diff] [blame] | 4827 | struct extent_map_tree *em_tree = &inode->extent_tree; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4828 | u64 hole_start = ALIGN(oldsize, fs_info->sectorsize); |
| 4829 | u64 block_end = ALIGN(size, fs_info->sectorsize); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4830 | u64 last_byte; |
| 4831 | u64 cur_offset; |
| 4832 | u64 hole_size; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4833 | int err = 0; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4834 | |
Josef Bacik | a71754f | 2013-06-17 17:14:39 -0400 | [diff] [blame] | 4835 | /* |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4836 | * If our size started in the middle of a block we need to zero out the |
| 4837 | * 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] | 4838 | * expose stale data. |
| 4839 | */ |
Nikolay Borisov | b06359a | 2020-11-02 16:49:04 +0200 | [diff] [blame] | 4840 | err = btrfs_truncate_block(inode, oldsize, 0, 0); |
Josef Bacik | a71754f | 2013-06-17 17:14:39 -0400 | [diff] [blame] | 4841 | if (err) |
| 4842 | return err; |
| 4843 | |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4844 | if (size <= hole_start) |
| 4845 | return 0; |
| 4846 | |
Nikolay Borisov | b06359a | 2020-11-02 16:49:04 +0200 | [diff] [blame] | 4847 | btrfs_lock_and_flush_ordered_range(inode, hole_start, block_end - 1, |
| 4848 | &cached_state); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4849 | cur_offset = hole_start; |
| 4850 | while (1) { |
Nikolay Borisov | b06359a | 2020-11-02 16:49:04 +0200 | [diff] [blame] | 4851 | em = btrfs_get_extent(inode, NULL, 0, cur_offset, |
Omar Sandoval | 39b07b5 | 2019-12-02 17:34:23 -0800 | [diff] [blame] | 4852 | block_end - cur_offset); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4853 | if (IS_ERR(em)) { |
| 4854 | err = PTR_ERR(em); |
Zach Brown | f276795 | 2013-01-08 19:37:58 +0000 | [diff] [blame] | 4855 | em = NULL; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4856 | break; |
| 4857 | } |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4858 | last_byte = min(extent_map_end(em), block_end); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4859 | last_byte = ALIGN(last_byte, fs_info->sectorsize); |
Josef Bacik | 9ddc959 | 2020-01-17 09:02:22 -0500 | [diff] [blame] | 4860 | hole_size = last_byte - cur_offset; |
| 4861 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4862 | if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) { |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4863 | struct extent_map *hole_em; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4864 | |
Nikolay Borisov | b06359a | 2020-11-02 16:49:04 +0200 | [diff] [blame] | 4865 | err = maybe_insert_hole(root, inode, cur_offset, |
| 4866 | hole_size); |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4867 | if (err) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 4868 | break; |
Josef Bacik | 9ddc959 | 2020-01-17 09:02:22 -0500 | [diff] [blame] | 4869 | |
Nikolay Borisov | b06359a | 2020-11-02 16:49:04 +0200 | [diff] [blame] | 4870 | err = btrfs_inode_set_file_extent_range(inode, |
Josef Bacik | 9ddc959 | 2020-01-17 09:02:22 -0500 | [diff] [blame] | 4871 | cur_offset, hole_size); |
| 4872 | if (err) |
| 4873 | break; |
| 4874 | |
Nikolay Borisov | b06359a | 2020-11-02 16:49:04 +0200 | [diff] [blame] | 4875 | btrfs_drop_extent_cache(inode, cur_offset, |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4876 | cur_offset + hole_size - 1, 0); |
| 4877 | hole_em = alloc_extent_map(); |
| 4878 | if (!hole_em) { |
| 4879 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, |
Nikolay Borisov | b06359a | 2020-11-02 16:49:04 +0200 | [diff] [blame] | 4880 | &inode->runtime_flags); |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4881 | goto next; |
| 4882 | } |
| 4883 | hole_em->start = cur_offset; |
| 4884 | hole_em->len = hole_size; |
| 4885 | hole_em->orig_start = cur_offset; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4886 | |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4887 | hole_em->block_start = EXTENT_MAP_HOLE; |
| 4888 | hole_em->block_len = 0; |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 4889 | hole_em->orig_block_len = 0; |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 4890 | hole_em->ram_bytes = hole_size; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4891 | hole_em->compress_type = BTRFS_COMPRESS_NONE; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4892 | hole_em->generation = fs_info->generation; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4893 | |
| 4894 | while (1) { |
| 4895 | write_lock(&em_tree->lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 4896 | err = add_extent_mapping(em_tree, hole_em, 1); |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4897 | write_unlock(&em_tree->lock); |
| 4898 | if (err != -EEXIST) |
| 4899 | break; |
Nikolay Borisov | b06359a | 2020-11-02 16:49:04 +0200 | [diff] [blame] | 4900 | btrfs_drop_extent_cache(inode, cur_offset, |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4901 | cur_offset + |
| 4902 | hole_size - 1, 0); |
| 4903 | } |
| 4904 | free_extent_map(hole_em); |
Josef Bacik | 9ddc959 | 2020-01-17 09:02:22 -0500 | [diff] [blame] | 4905 | } else { |
Nikolay Borisov | b06359a | 2020-11-02 16:49:04 +0200 | [diff] [blame] | 4906 | err = btrfs_inode_set_file_extent_range(inode, |
Josef Bacik | 9ddc959 | 2020-01-17 09:02:22 -0500 | [diff] [blame] | 4907 | cur_offset, hole_size); |
| 4908 | if (err) |
| 4909 | break; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4910 | } |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4911 | next: |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4912 | free_extent_map(em); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4913 | em = NULL; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4914 | cur_offset = last_byte; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4915 | if (cur_offset >= block_end) |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4916 | break; |
| 4917 | } |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4918 | free_extent_map(em); |
David Sterba | e43bbe5 | 2017-12-12 21:43:52 +0100 | [diff] [blame] | 4919 | unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4920 | return err; |
| 4921 | } |
| 4922 | |
Eric Sandeen | 3972f26 | 2013-01-12 02:57:22 +0000 | [diff] [blame] | 4923 | static int btrfs_setsize(struct inode *inode, struct iattr *attr) |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4924 | { |
Miao Xie | f4a2f4c | 2011-12-14 20:12:01 -0500 | [diff] [blame] | 4925 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 4926 | struct btrfs_trans_handle *trans; |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 4927 | loff_t oldsize = i_size_read(inode); |
Eric Sandeen | 3972f26 | 2013-01-12 02:57:22 +0000 | [diff] [blame] | 4928 | loff_t newsize = attr->ia_size; |
| 4929 | int mask = attr->ia_valid; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4930 | int ret; |
| 4931 | |
Eric Sandeen | 3972f26 | 2013-01-12 02:57:22 +0000 | [diff] [blame] | 4932 | /* |
| 4933 | * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a |
| 4934 | * special case where we need to update the times despite not having |
| 4935 | * these flags set. For all other operations the VFS set these flags |
| 4936 | * explicitly if it wants a timestamp update. |
| 4937 | */ |
Christoph Hellwig | dff6efc | 2013-11-19 07:17:07 -0800 | [diff] [blame] | 4938 | if (newsize != oldsize) { |
| 4939 | inode_inc_iversion(inode); |
| 4940 | if (!(mask & (ATTR_CTIME | ATTR_MTIME))) |
| 4941 | inode->i_ctime = inode->i_mtime = |
Deepa Dinamani | c2050a4 | 2016-09-14 07:48:06 -0700 | [diff] [blame] | 4942 | current_time(inode); |
Christoph Hellwig | dff6efc | 2013-11-19 07:17:07 -0800 | [diff] [blame] | 4943 | } |
Eric Sandeen | 3972f26 | 2013-01-12 02:57:22 +0000 | [diff] [blame] | 4944 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 4945 | if (newsize > oldsize) { |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 4946 | /* |
David Sterba | ea14b57f | 2017-06-22 02:19:11 +0200 | [diff] [blame] | 4947 | * Don't do an expanding truncate while snapshotting is ongoing. |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 4948 | * This is to ensure the snapshot captures a fully consistent |
| 4949 | * state of this file - if the snapshot captures this expanding |
| 4950 | * truncation, it must capture all writes that happened before |
| 4951 | * this truncation. |
| 4952 | */ |
Nikolay Borisov | dcc3eb9 | 2020-01-30 14:59:45 +0200 | [diff] [blame] | 4953 | btrfs_drew_write_lock(&root->snapshot_lock); |
Nikolay Borisov | b06359a | 2020-11-02 16:49:04 +0200 | [diff] [blame] | 4954 | ret = btrfs_cont_expand(BTRFS_I(inode), oldsize, newsize); |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 4955 | if (ret) { |
Nikolay Borisov | dcc3eb9 | 2020-01-30 14:59:45 +0200 | [diff] [blame] | 4956 | btrfs_drew_write_unlock(&root->snapshot_lock); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4957 | return ret; |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 4958 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4959 | |
Miao Xie | f4a2f4c | 2011-12-14 20:12:01 -0500 | [diff] [blame] | 4960 | trans = btrfs_start_transaction(root, 1); |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 4961 | if (IS_ERR(trans)) { |
Nikolay Borisov | dcc3eb9 | 2020-01-30 14:59:45 +0200 | [diff] [blame] | 4962 | btrfs_drew_write_unlock(&root->snapshot_lock); |
Miao Xie | f4a2f4c | 2011-12-14 20:12:01 -0500 | [diff] [blame] | 4963 | return PTR_ERR(trans); |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 4964 | } |
Miao Xie | f4a2f4c | 2011-12-14 20:12:01 -0500 | [diff] [blame] | 4965 | |
| 4966 | i_size_write(inode, newsize); |
Nikolay Borisov | 76aea53 | 2020-11-02 16:48:53 +0200 | [diff] [blame] | 4967 | btrfs_inode_safe_disk_i_size_write(BTRFS_I(inode), 0); |
Chandan Rajendra | 27772b6 | 2016-01-21 15:56:03 +0530 | [diff] [blame] | 4968 | pagecache_isize_extended(inode, oldsize, newsize); |
Nikolay Borisov | 9a56fcd | 2020-11-02 16:48:59 +0200 | [diff] [blame] | 4969 | ret = btrfs_update_inode(trans, root, BTRFS_I(inode)); |
Nikolay Borisov | dcc3eb9 | 2020-01-30 14:59:45 +0200 | [diff] [blame] | 4970 | btrfs_drew_write_unlock(&root->snapshot_lock); |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 4971 | btrfs_end_transaction(trans); |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 4972 | } else { |
Naohiro Aota | 24c0a72 | 2021-02-04 19:22:09 +0900 | [diff] [blame] | 4973 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
| 4974 | |
| 4975 | if (btrfs_is_zoned(fs_info)) { |
| 4976 | ret = btrfs_wait_ordered_range(inode, |
| 4977 | ALIGN(newsize, fs_info->sectorsize), |
| 4978 | (u64)-1); |
| 4979 | if (ret) |
| 4980 | return ret; |
| 4981 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4982 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 4983 | /* |
| 4984 | * 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] | 4985 | * zero. Make sure any new writes to the file get on disk |
| 4986 | * on close. |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 4987 | */ |
| 4988 | if (newsize == 0) |
Nikolay Borisov | 1fd4033 | 2020-10-01 09:40:39 +0300 | [diff] [blame] | 4989 | set_bit(BTRFS_INODE_FLUSH_ON_CLOSE, |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 4990 | &BTRFS_I(inode)->runtime_flags); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4991 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 4992 | truncate_setsize(inode, newsize); |
Miao Xie | 2e60a51 | 2013-02-08 07:01:08 +0000 | [diff] [blame] | 4993 | |
Miao Xie | 2e60a51 | 2013-02-08 07:01:08 +0000 | [diff] [blame] | 4994 | inode_dio_wait(inode); |
Miao Xie | 2e60a51 | 2013-02-08 07:01:08 +0000 | [diff] [blame] | 4995 | |
Filipe Manana | 213e8c5 | 2018-02-06 20:40:31 +0000 | [diff] [blame] | 4996 | ret = btrfs_truncate(inode, newsize == oldsize); |
Josef Bacik | 7f4f6e0 | 2013-08-29 16:43:28 -0400 | [diff] [blame] | 4997 | if (ret && inode->i_nlink) { |
| 4998 | int err; |
| 4999 | |
| 5000 | /* |
Omar Sandoval | f7e9e8f | 2018-05-11 13:13:32 -0700 | [diff] [blame] | 5001 | * Truncate failed, so fix up the in-memory size. We |
| 5002 | * adjusted disk_i_size down as we removed extents, so |
| 5003 | * wait for disk_i_size to be stable and then update the |
| 5004 | * in-memory size to match. |
Josef Bacik | 7f4f6e0 | 2013-08-29 16:43:28 -0400 | [diff] [blame] | 5005 | */ |
Omar Sandoval | f7e9e8f | 2018-05-11 13:13:32 -0700 | [diff] [blame] | 5006 | err = btrfs_wait_ordered_range(inode, 0, (u64)-1); |
Josef Bacik | 7f4f6e0 | 2013-08-29 16:43:28 -0400 | [diff] [blame] | 5007 | if (err) |
Omar Sandoval | f7e9e8f | 2018-05-11 13:13:32 -0700 | [diff] [blame] | 5008 | return err; |
| 5009 | i_size_write(inode, BTRFS_I(inode)->disk_i_size); |
Josef Bacik | 7f4f6e0 | 2013-08-29 16:43:28 -0400 | [diff] [blame] | 5010 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5011 | } |
| 5012 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 5013 | return ret; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5014 | } |
| 5015 | |
Christian Brauner | 549c729 | 2021-01-21 14:19:43 +0100 | [diff] [blame] | 5016 | static int btrfs_setattr(struct user_namespace *mnt_userns, struct dentry *dentry, |
| 5017 | struct iattr *attr) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5018 | { |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 5019 | struct inode *inode = d_inode(dentry); |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 5020 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5021 | int err; |
| 5022 | |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 5023 | if (btrfs_root_readonly(root)) |
| 5024 | return -EROFS; |
| 5025 | |
Christian Brauner | d4d0946 | 2021-07-27 12:48:49 +0200 | [diff] [blame] | 5026 | err = setattr_prepare(mnt_userns, dentry, attr); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5027 | if (err) |
| 5028 | return err; |
| 5029 | |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 5030 | if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) { |
Eric Sandeen | 3972f26 | 2013-01-12 02:57:22 +0000 | [diff] [blame] | 5031 | err = btrfs_setsize(inode, attr); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5032 | if (err) |
| 5033 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5034 | } |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 5035 | |
Christoph Hellwig | 1025774 | 2010-06-04 11:30:02 +0200 | [diff] [blame] | 5036 | if (attr->ia_valid) { |
Christian Brauner | d4d0946 | 2021-07-27 12:48:49 +0200 | [diff] [blame] | 5037 | setattr_copy(mnt_userns, inode, attr); |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 5038 | inode_inc_iversion(inode); |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 5039 | err = btrfs_dirty_inode(inode); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 5040 | |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 5041 | if (!err && attr->ia_valid & ATTR_MODE) |
Christian Brauner | d4d0946 | 2021-07-27 12:48:49 +0200 | [diff] [blame] | 5042 | err = posix_acl_chmod(mnt_userns, inode, inode->i_mode); |
Christoph Hellwig | 1025774 | 2010-06-04 11:30:02 +0200 | [diff] [blame] | 5043 | } |
| 5044 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5045 | return err; |
| 5046 | } |
Chris Mason | 61295eb | 2008-01-14 16:24:38 -0500 | [diff] [blame] | 5047 | |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5048 | /* |
| 5049 | * While truncating the inode pages during eviction, we get the VFS calling |
| 5050 | * btrfs_invalidatepage() against each page of the inode. This is slow because |
| 5051 | * the calls to btrfs_invalidatepage() result in a huge amount of calls to |
| 5052 | * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting |
| 5053 | * extent_state structures over and over, wasting lots of time. |
| 5054 | * |
| 5055 | * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all |
| 5056 | * those expensive operations on a per page basis and do only the ordered io |
| 5057 | * finishing, while we release here the extent_map and extent_state structures, |
| 5058 | * without the excessive merging and splitting. |
| 5059 | */ |
| 5060 | static void evict_inode_truncate_pages(struct inode *inode) |
| 5061 | { |
| 5062 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
| 5063 | struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree; |
| 5064 | struct rb_node *node; |
| 5065 | |
| 5066 | ASSERT(inode->i_state & I_FREEING); |
Johannes Weiner | 91b0abe | 2014-04-03 14:47:49 -0700 | [diff] [blame] | 5067 | truncate_inode_pages_final(&inode->i_data); |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5068 | |
| 5069 | write_lock(&map_tree->lock); |
Liu Bo | 07e1ce0 | 2018-08-23 03:51:52 +0800 | [diff] [blame] | 5070 | while (!RB_EMPTY_ROOT(&map_tree->map.rb_root)) { |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5071 | struct extent_map *em; |
| 5072 | |
Liu Bo | 07e1ce0 | 2018-08-23 03:51:52 +0800 | [diff] [blame] | 5073 | node = rb_first_cached(&map_tree->map); |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5074 | em = rb_entry(node, struct extent_map, rb_node); |
Wang Shilong | 180589e | 2013-12-14 15:27:31 +0800 | [diff] [blame] | 5075 | clear_bit(EXTENT_FLAG_PINNED, &em->flags); |
| 5076 | clear_bit(EXTENT_FLAG_LOGGING, &em->flags); |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5077 | remove_extent_mapping(map_tree, em); |
| 5078 | free_extent_map(em); |
Filipe Manana | 7064dd5 | 2014-08-08 02:47:05 +0100 | [diff] [blame] | 5079 | if (need_resched()) { |
| 5080 | write_unlock(&map_tree->lock); |
| 5081 | cond_resched(); |
| 5082 | write_lock(&map_tree->lock); |
| 5083 | } |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5084 | } |
| 5085 | write_unlock(&map_tree->lock); |
| 5086 | |
Filipe Manana | 6ca0709 | 2015-05-26 00:55:42 +0100 | [diff] [blame] | 5087 | /* |
| 5088 | * 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] | 5089 | * We can have ongoing bios started by readahead that have |
| 5090 | * their endio callback (extent_io.c:end_bio_extent_readpage) |
Filipe Manana | 9c6429d | 2015-06-10 12:55:41 +0100 | [diff] [blame] | 5091 | * still in progress (unlocked the pages in the bio but did not yet |
| 5092 | * unlocked the ranges in the io tree). Therefore this means some |
Filipe Manana | 6ca0709 | 2015-05-26 00:55:42 +0100 | [diff] [blame] | 5093 | * ranges can still be locked and eviction started because before |
| 5094 | * submitting those bios, which are executed by a separate task (work |
| 5095 | * queue kthread), inode references (inode->i_count) were not taken |
| 5096 | * (which would be dropped in the end io callback of each bio). |
| 5097 | * Therefore here we effectively end up waiting for those bios and |
| 5098 | * anyone else holding locked ranges without having bumped the inode's |
| 5099 | * reference count - if we don't do it, when they access the inode's |
| 5100 | * io_tree to unlock a range it may be too late, leading to an |
| 5101 | * use-after-free issue. |
| 5102 | */ |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5103 | spin_lock(&io_tree->lock); |
| 5104 | while (!RB_EMPTY_ROOT(&io_tree->state)) { |
| 5105 | struct extent_state *state; |
| 5106 | struct extent_state *cached_state = NULL; |
Filipe Manana | 6ca0709 | 2015-05-26 00:55:42 +0100 | [diff] [blame] | 5107 | u64 start; |
| 5108 | u64 end; |
Filipe Manana | 421f092 | 2018-10-12 13:02:48 +0100 | [diff] [blame] | 5109 | unsigned state_flags; |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5110 | |
| 5111 | node = rb_first(&io_tree->state); |
| 5112 | state = rb_entry(node, struct extent_state, rb_node); |
Filipe Manana | 6ca0709 | 2015-05-26 00:55:42 +0100 | [diff] [blame] | 5113 | start = state->start; |
| 5114 | end = state->end; |
Filipe Manana | 421f092 | 2018-10-12 13:02:48 +0100 | [diff] [blame] | 5115 | state_flags = state->state; |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5116 | spin_unlock(&io_tree->lock); |
| 5117 | |
David Sterba | ff13db4 | 2015-12-03 14:30:40 +0100 | [diff] [blame] | 5118 | lock_extent_bits(io_tree, start, end, &cached_state); |
Qu Wenruo | b9d0b38 | 2015-09-29 10:35:16 +0800 | [diff] [blame] | 5119 | |
| 5120 | /* |
| 5121 | * If still has DELALLOC flag, the extent didn't reach disk, |
| 5122 | * and its reserved space won't be freed by delayed_ref. |
| 5123 | * So we need to free its reserved space here. |
| 5124 | * (Refer to comment in btrfs_invalidatepage, case 2) |
| 5125 | * |
| 5126 | * Note, end is the bytenr of last byte, so we need + 1 here. |
| 5127 | */ |
Filipe Manana | 421f092 | 2018-10-12 13:02:48 +0100 | [diff] [blame] | 5128 | if (state_flags & EXTENT_DELALLOC) |
Nikolay Borisov | 8b8a979 | 2020-06-03 08:55:11 +0300 | [diff] [blame] | 5129 | btrfs_qgroup_free_data(BTRFS_I(inode), NULL, start, |
| 5130 | end - start + 1); |
Qu Wenruo | b9d0b38 | 2015-09-29 10:35:16 +0800 | [diff] [blame] | 5131 | |
Filipe Manana | 6ca0709 | 2015-05-26 00:55:42 +0100 | [diff] [blame] | 5132 | clear_extent_bit(io_tree, start, end, |
Omar Sandoval | e182163 | 2019-08-15 14:04:04 -0700 | [diff] [blame] | 5133 | EXTENT_LOCKED | EXTENT_DELALLOC | |
| 5134 | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1, |
| 5135 | &cached_state); |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5136 | |
Filipe Manana | 7064dd5 | 2014-08-08 02:47:05 +0100 | [diff] [blame] | 5137 | cond_resched(); |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5138 | spin_lock(&io_tree->lock); |
| 5139 | } |
| 5140 | spin_unlock(&io_tree->lock); |
| 5141 | } |
| 5142 | |
Omar Sandoval | 4b9d7b5 | 2018-05-11 13:13:36 -0700 | [diff] [blame] | 5143 | 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] | 5144 | struct btrfs_block_rsv *rsv) |
Omar Sandoval | 4b9d7b5 | 2018-05-11 13:13:36 -0700 | [diff] [blame] | 5145 | { |
| 5146 | struct btrfs_fs_info *fs_info = root->fs_info; |
Josef Bacik | d3984c9 | 2019-08-01 18:19:37 -0400 | [diff] [blame] | 5147 | struct btrfs_trans_handle *trans; |
Josef Bacik | 2bd36e7 | 2019-08-22 15:14:33 -0400 | [diff] [blame] | 5148 | u64 delayed_refs_extra = btrfs_calc_insert_metadata_size(fs_info, 1); |
Josef Bacik | d3984c9 | 2019-08-01 18:19:37 -0400 | [diff] [blame] | 5149 | int ret; |
Omar Sandoval | 4b9d7b5 | 2018-05-11 13:13:36 -0700 | [diff] [blame] | 5150 | |
Josef Bacik | d3984c9 | 2019-08-01 18:19:37 -0400 | [diff] [blame] | 5151 | /* |
| 5152 | * Eviction should be taking place at some place safe because of our |
| 5153 | * delayed iputs. However the normal flushing code will run delayed |
| 5154 | * iputs, so we cannot use FLUSH_ALL otherwise we'll deadlock. |
| 5155 | * |
| 5156 | * We reserve the delayed_refs_extra here again because we can't use |
| 5157 | * btrfs_start_transaction(root, 0) for the same deadlocky reason as |
| 5158 | * above. We reserve our extra bit here because we generate a ton of |
| 5159 | * delayed refs activity by truncating. |
| 5160 | * |
Josef Bacik | ee6adbf | 2021-11-09 10:12:04 -0500 | [diff] [blame] | 5161 | * BTRFS_RESERVE_FLUSH_EVICT will steal from the global_rsv if it can, |
| 5162 | * if we fail to make this reservation we can re-try without the |
| 5163 | * delayed_refs_extra so we can make some forward progress. |
Josef Bacik | d3984c9 | 2019-08-01 18:19:37 -0400 | [diff] [blame] | 5164 | */ |
Josef Bacik | 9270501 | 2021-11-09 10:12:07 -0500 | [diff] [blame] | 5165 | ret = btrfs_block_rsv_refill(fs_info, rsv, rsv->size + delayed_refs_extra, |
Josef Bacik | d3984c9 | 2019-08-01 18:19:37 -0400 | [diff] [blame] | 5166 | BTRFS_RESERVE_FLUSH_EVICT); |
| 5167 | if (ret) { |
Josef Bacik | 9270501 | 2021-11-09 10:12:07 -0500 | [diff] [blame] | 5168 | ret = btrfs_block_rsv_refill(fs_info, rsv, rsv->size, |
Josef Bacik | ee6adbf | 2021-11-09 10:12:04 -0500 | [diff] [blame] | 5169 | BTRFS_RESERVE_FLUSH_EVICT); |
| 5170 | if (ret) { |
Josef Bacik | d3984c9 | 2019-08-01 18:19:37 -0400 | [diff] [blame] | 5171 | btrfs_warn(fs_info, |
| 5172 | "could not allocate space for delete; will truncate on mount"); |
| 5173 | return ERR_PTR(-ENOSPC); |
| 5174 | } |
| 5175 | delayed_refs_extra = 0; |
Omar Sandoval | 4b9d7b5 | 2018-05-11 13:13:36 -0700 | [diff] [blame] | 5176 | } |
Josef Bacik | d3984c9 | 2019-08-01 18:19:37 -0400 | [diff] [blame] | 5177 | |
| 5178 | trans = btrfs_join_transaction(root); |
| 5179 | if (IS_ERR(trans)) |
| 5180 | return trans; |
| 5181 | |
| 5182 | if (delayed_refs_extra) { |
| 5183 | trans->block_rsv = &fs_info->trans_block_rsv; |
| 5184 | trans->bytes_reserved = delayed_refs_extra; |
| 5185 | btrfs_block_rsv_migrate(rsv, trans->block_rsv, |
| 5186 | delayed_refs_extra, 1); |
| 5187 | } |
| 5188 | return trans; |
Omar Sandoval | 4b9d7b5 | 2018-05-11 13:13:36 -0700 | [diff] [blame] | 5189 | } |
| 5190 | |
Al Viro | bd55597 | 2010-06-07 11:35:40 -0400 | [diff] [blame] | 5191 | void btrfs_evict_inode(struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5192 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5193 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5194 | struct btrfs_trans_handle *trans; |
| 5195 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Omar Sandoval | 4b9d7b5 | 2018-05-11 13:13:36 -0700 | [diff] [blame] | 5196 | struct btrfs_block_rsv *rsv; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5197 | int ret; |
| 5198 | |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 5199 | trace_btrfs_inode_evict(inode); |
| 5200 | |
Nikolay Borisov | 3d48d98 | 2016-06-29 09:46:41 +0300 | [diff] [blame] | 5201 | if (!root) { |
Boris Burkov | 1460540 | 2021-06-30 13:01:49 -0700 | [diff] [blame] | 5202 | fsverity_cleanup_inode(inode); |
Liu Bo | e8f1bc1 | 2018-01-25 11:02:53 -0700 | [diff] [blame] | 5203 | clear_inode(inode); |
Nikolay Borisov | 3d48d98 | 2016-06-29 09:46:41 +0300 | [diff] [blame] | 5204 | return; |
| 5205 | } |
| 5206 | |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5207 | evict_inode_truncate_pages(inode); |
| 5208 | |
Stefan Behrens | 69e9c6c | 2013-09-05 16:58:43 +0200 | [diff] [blame] | 5209 | if (inode->i_nlink && |
| 5210 | ((btrfs_root_refs(&root->root_item) != 0 && |
| 5211 | root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) || |
Nikolay Borisov | 70ddc55 | 2017-02-20 13:50:35 +0200 | [diff] [blame] | 5212 | btrfs_is_free_space_inode(BTRFS_I(inode)))) |
Al Viro | bd55597 | 2010-06-07 11:35:40 -0400 | [diff] [blame] | 5213 | goto no_delete; |
| 5214 | |
Omar Sandoval | 2791906 | 2018-05-11 13:13:37 -0700 | [diff] [blame] | 5215 | if (is_bad_inode(inode)) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5216 | goto no_delete; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5217 | |
Nikolay Borisov | 7ab7956 | 2017-02-20 13:50:57 +0200 | [diff] [blame] | 5218 | btrfs_free_io_failure_record(BTRFS_I(inode), 0, (u64)-1); |
Miao Xie | f612496 | 2014-09-12 18:44:04 +0800 | [diff] [blame] | 5219 | |
Omar Sandoval | 7b40b69 | 2018-05-11 13:13:33 -0700 | [diff] [blame] | 5220 | if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 5221 | goto no_delete; |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 5222 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5223 | if (inode->i_nlink > 0) { |
Stefan Behrens | 69e9c6c | 2013-09-05 16:58:43 +0200 | [diff] [blame] | 5224 | BUG_ON(btrfs_root_refs(&root->root_item) != 0 && |
| 5225 | root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5226 | goto no_delete; |
| 5227 | } |
| 5228 | |
Nikolay Borisov | aa79021 | 2017-01-10 20:35:40 +0200 | [diff] [blame] | 5229 | ret = btrfs_commit_inode_delayed_inode(BTRFS_I(inode)); |
Omar Sandoval | 2791906 | 2018-05-11 13:13:37 -0700 | [diff] [blame] | 5230 | if (ret) |
Miao Xie | 0e8c36a | 2012-12-19 06:59:51 +0000 | [diff] [blame] | 5231 | goto no_delete; |
Miao Xie | 0e8c36a | 2012-12-19 06:59:51 +0000 | [diff] [blame] | 5232 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5233 | rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP); |
Omar Sandoval | 2791906 | 2018-05-11 13:13:37 -0700 | [diff] [blame] | 5234 | if (!rsv) |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 5235 | goto no_delete; |
Josef Bacik | 2bd36e7 | 2019-08-22 15:14:33 -0400 | [diff] [blame] | 5236 | rsv->size = btrfs_calc_metadata_size(fs_info, 1); |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 5237 | rsv->failfast = 1; |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 5238 | |
Nikolay Borisov | 6ef06d2 | 2017-02-20 13:50:34 +0200 | [diff] [blame] | 5239 | btrfs_i_size_write(BTRFS_I(inode), 0); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5240 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5241 | while (1) { |
Josef Bacik | ad80cf5 | 2018-09-28 07:18:19 -0400 | [diff] [blame] | 5242 | trans = evict_refill_and_join(root, rsv); |
Omar Sandoval | 2791906 | 2018-05-11 13:13:37 -0700 | [diff] [blame] | 5243 | if (IS_ERR(trans)) |
| 5244 | goto free_rsv; |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 5245 | |
| 5246 | trans->block_rsv = rsv; |
| 5247 | |
Nikolay Borisov | 5074339 | 2020-11-02 16:48:55 +0200 | [diff] [blame] | 5248 | ret = btrfs_truncate_inode_items(trans, root, BTRFS_I(inode), |
Filipe Manana | 0d7d316 | 2021-05-24 11:35:55 +0100 | [diff] [blame] | 5249 | 0, 0, NULL); |
Omar Sandoval | 2791906 | 2018-05-11 13:13:37 -0700 | [diff] [blame] | 5250 | trans->block_rsv = &fs_info->trans_block_rsv; |
| 5251 | btrfs_end_transaction(trans); |
| 5252 | btrfs_btree_balance_dirty(fs_info); |
| 5253 | if (ret && ret != -ENOSPC && ret != -EAGAIN) |
| 5254 | goto free_rsv; |
| 5255 | else if (!ret) |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5256 | break; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 5257 | } |
| 5258 | |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 5259 | /* |
Omar Sandoval | 2791906 | 2018-05-11 13:13:37 -0700 | [diff] [blame] | 5260 | * Errors here aren't a big deal, it just means we leave orphan items in |
| 5261 | * the tree. They will be cleaned up on the next mount. If the inode |
| 5262 | * number gets reused, cleanup deletes the orphan item without doing |
| 5263 | * anything, and unlink reuses the existing orphan item. |
| 5264 | * |
| 5265 | * If it turns out that we are dropping too many of these, we might want |
| 5266 | * to add a mechanism for retrying these after a commit. |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 5267 | */ |
Josef Bacik | ad80cf5 | 2018-09-28 07:18:19 -0400 | [diff] [blame] | 5268 | trans = evict_refill_and_join(root, rsv); |
Omar Sandoval | 2791906 | 2018-05-11 13:13:37 -0700 | [diff] [blame] | 5269 | if (!IS_ERR(trans)) { |
| 5270 | trans->block_rsv = rsv; |
| 5271 | btrfs_orphan_del(trans, BTRFS_I(inode)); |
| 5272 | trans->block_rsv = &fs_info->trans_block_rsv; |
| 5273 | btrfs_end_transaction(trans); |
| 5274 | } |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 5275 | |
Omar Sandoval | 2791906 | 2018-05-11 13:13:37 -0700 | [diff] [blame] | 5276 | free_rsv: |
| 5277 | btrfs_free_block_rsv(fs_info, rsv); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5278 | no_delete: |
Omar Sandoval | 2791906 | 2018-05-11 13:13:37 -0700 | [diff] [blame] | 5279 | /* |
| 5280 | * If we didn't successfully delete, the orphan item will still be in |
| 5281 | * the tree and we'll retry on the next mount. Again, we might also want |
| 5282 | * to retry these periodically in the future. |
| 5283 | */ |
Nikolay Borisov | f48d1cf | 2017-01-10 20:35:39 +0200 | [diff] [blame] | 5284 | btrfs_remove_delayed_node(BTRFS_I(inode)); |
Boris Burkov | 1460540 | 2021-06-30 13:01:49 -0700 | [diff] [blame] | 5285 | fsverity_cleanup_inode(inode); |
Jan Kara | dbd5768 | 2012-05-03 14:48:02 +0200 | [diff] [blame] | 5286 | clear_inode(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5287 | } |
| 5288 | |
| 5289 | /* |
Qu Wenruo | 6bf9e4b | 2019-03-13 13:55:11 +0800 | [diff] [blame] | 5290 | * Return the key found in the dir entry in the location pointer, fill @type |
| 5291 | * with BTRFS_FT_*, and return 0. |
| 5292 | * |
Su Yue | 005d671 | 2018-03-05 17:13:37 +0800 | [diff] [blame] | 5293 | * If no dir entries were found, returns -ENOENT. |
| 5294 | * If found a corrupted location in dir entry, returns -EUCLEAN. |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5295 | */ |
| 5296 | static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry, |
Qu Wenruo | 6bf9e4b | 2019-03-13 13:55:11 +0800 | [diff] [blame] | 5297 | struct btrfs_key *location, u8 *type) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5298 | { |
| 5299 | const char *name = dentry->d_name.name; |
| 5300 | int namelen = dentry->d_name.len; |
| 5301 | struct btrfs_dir_item *di; |
| 5302 | struct btrfs_path *path; |
| 5303 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Yan | 0d9f7f3 | 2007-10-25 15:48:28 -0400 | [diff] [blame] | 5304 | int ret = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5305 | |
| 5306 | path = btrfs_alloc_path(); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 5307 | if (!path) |
| 5308 | return -ENOMEM; |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 5309 | |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 5310 | di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(BTRFS_I(dir)), |
| 5311 | name, namelen, 0); |
Liu Bo | 3cf5068 | 2018-09-12 06:06:26 +0800 | [diff] [blame] | 5312 | if (IS_ERR_OR_NULL(di)) { |
| 5313 | ret = di ? PTR_ERR(di) : -ENOENT; |
Su Yue | 005d671 | 2018-03-05 17:13:37 +0800 | [diff] [blame] | 5314 | goto out; |
| 5315 | } |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5316 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5317 | btrfs_dir_item_key_to_cpu(path->nodes[0], di, location); |
Liu Bo | 56a0e70 | 2017-10-30 11:14:38 -0600 | [diff] [blame] | 5318 | if (location->type != BTRFS_INODE_ITEM_KEY && |
| 5319 | location->type != BTRFS_ROOT_ITEM_KEY) { |
Su Yue | 005d671 | 2018-03-05 17:13:37 +0800 | [diff] [blame] | 5320 | ret = -EUCLEAN; |
Liu Bo | 56a0e70 | 2017-10-30 11:14:38 -0600 | [diff] [blame] | 5321 | btrfs_warn(root->fs_info, |
| 5322 | "%s gets something invalid in DIR_ITEM (name %s, directory ino %llu, location(%llu %u %llu))", |
| 5323 | __func__, name, btrfs_ino(BTRFS_I(dir)), |
| 5324 | location->objectid, location->type, location->offset); |
Liu Bo | 56a0e70 | 2017-10-30 11:14:38 -0600 | [diff] [blame] | 5325 | } |
Qu Wenruo | 6bf9e4b | 2019-03-13 13:55:11 +0800 | [diff] [blame] | 5326 | if (!ret) |
| 5327 | *type = btrfs_dir_type(path->nodes[0], di); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5328 | out: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5329 | btrfs_free_path(path); |
| 5330 | return ret; |
| 5331 | } |
| 5332 | |
| 5333 | /* |
| 5334 | * when we hit a tree root in a directory, the btrfs part of the inode |
| 5335 | * needs to be changed to reflect the root directory of the tree root. This |
| 5336 | * is kind of like crossing a mount point. |
| 5337 | */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5338 | static int fixup_tree_root_location(struct btrfs_fs_info *fs_info, |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5339 | struct inode *dir, |
| 5340 | struct dentry *dentry, |
| 5341 | struct btrfs_key *location, |
| 5342 | struct btrfs_root **sub_root) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5343 | { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5344 | struct btrfs_path *path; |
| 5345 | struct btrfs_root *new_root; |
| 5346 | struct btrfs_root_ref *ref; |
| 5347 | struct extent_buffer *leaf; |
David Sterba | 1d4c08e | 2015-01-02 19:36:14 +0100 | [diff] [blame] | 5348 | struct btrfs_key key; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5349 | int ret; |
| 5350 | int err = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5351 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5352 | path = btrfs_alloc_path(); |
| 5353 | if (!path) { |
| 5354 | err = -ENOMEM; |
| 5355 | goto out; |
| 5356 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5357 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5358 | err = -ENOENT; |
David Sterba | 1d4c08e | 2015-01-02 19:36:14 +0100 | [diff] [blame] | 5359 | key.objectid = BTRFS_I(dir)->root->root_key.objectid; |
| 5360 | key.type = BTRFS_ROOT_REF_KEY; |
| 5361 | key.offset = location->objectid; |
| 5362 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5363 | 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] | 5364 | if (ret) { |
| 5365 | if (ret < 0) |
| 5366 | err = ret; |
| 5367 | goto out; |
| 5368 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5369 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5370 | leaf = path->nodes[0]; |
| 5371 | ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref); |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 5372 | if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(BTRFS_I(dir)) || |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5373 | btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len) |
| 5374 | goto out; |
| 5375 | |
| 5376 | ret = memcmp_extent_buffer(leaf, dentry->d_name.name, |
| 5377 | (unsigned long)(ref + 1), |
| 5378 | dentry->d_name.len); |
| 5379 | if (ret) |
| 5380 | goto out; |
| 5381 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 5382 | btrfs_release_path(path); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5383 | |
David Sterba | 56e9357 | 2020-05-15 19:35:55 +0200 | [diff] [blame] | 5384 | new_root = btrfs_get_fs_root(fs_info, location->objectid, true); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5385 | if (IS_ERR(new_root)) { |
| 5386 | err = PTR_ERR(new_root); |
| 5387 | goto out; |
| 5388 | } |
| 5389 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5390 | *sub_root = new_root; |
| 5391 | location->objectid = btrfs_root_dirid(&new_root->root_item); |
| 5392 | location->type = BTRFS_INODE_ITEM_KEY; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5393 | location->offset = 0; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5394 | err = 0; |
| 5395 | out: |
| 5396 | btrfs_free_path(path); |
| 5397 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5398 | } |
| 5399 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5400 | static void inode_tree_add(struct inode *inode) |
| 5401 | { |
| 5402 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5403 | struct btrfs_inode *entry; |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 5404 | struct rb_node **p; |
| 5405 | struct rb_node *parent; |
Filipe David Borba Manana | cef2193 | 2013-09-02 12:19:13 +0100 | [diff] [blame] | 5406 | struct rb_node *new = &BTRFS_I(inode)->rb_node; |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 5407 | u64 ino = btrfs_ino(BTRFS_I(inode)); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5408 | |
Al Viro | 1d3382cb | 2010-10-23 15:19:20 -0400 | [diff] [blame] | 5409 | if (inode_unhashed(inode)) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5410 | return; |
Miao Xie | e1409ce | 2013-05-15 07:48:16 +0000 | [diff] [blame] | 5411 | parent = NULL; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5412 | spin_lock(&root->inode_lock); |
Miao Xie | e1409ce | 2013-05-15 07:48:16 +0000 | [diff] [blame] | 5413 | p = &root->inode_tree.rb_node; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5414 | while (*p) { |
| 5415 | parent = *p; |
| 5416 | entry = rb_entry(parent, struct btrfs_inode, rb_node); |
| 5417 | |
David Sterba | 3750851 | 2018-06-29 10:56:40 +0200 | [diff] [blame] | 5418 | if (ino < btrfs_ino(entry)) |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 5419 | p = &parent->rb_left; |
David Sterba | 3750851 | 2018-06-29 10:56:40 +0200 | [diff] [blame] | 5420 | else if (ino > btrfs_ino(entry)) |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 5421 | p = &parent->rb_right; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5422 | else { |
| 5423 | WARN_ON(!(entry->vfs_inode.i_state & |
Al Viro | a4ffdde | 2010-06-02 17:38:30 -0400 | [diff] [blame] | 5424 | (I_WILL_FREE | I_FREEING))); |
Filipe David Borba Manana | cef2193 | 2013-09-02 12:19:13 +0100 | [diff] [blame] | 5425 | rb_replace_node(parent, new, &root->inode_tree); |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 5426 | RB_CLEAR_NODE(parent); |
| 5427 | spin_unlock(&root->inode_lock); |
Filipe David Borba Manana | cef2193 | 2013-09-02 12:19:13 +0100 | [diff] [blame] | 5428 | return; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5429 | } |
| 5430 | } |
Filipe David Borba Manana | cef2193 | 2013-09-02 12:19:13 +0100 | [diff] [blame] | 5431 | rb_link_node(new, parent, p); |
| 5432 | rb_insert_color(new, &root->inode_tree); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5433 | spin_unlock(&root->inode_lock); |
| 5434 | } |
| 5435 | |
Nikolay Borisov | b79b724 | 2020-08-31 14:42:38 +0300 | [diff] [blame] | 5436 | static void inode_tree_del(struct btrfs_inode *inode) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5437 | { |
Nikolay Borisov | b79b724 | 2020-08-31 14:42:38 +0300 | [diff] [blame] | 5438 | struct btrfs_root *root = inode->root; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5439 | int empty = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5440 | |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 5441 | spin_lock(&root->inode_lock); |
Nikolay Borisov | b79b724 | 2020-08-31 14:42:38 +0300 | [diff] [blame] | 5442 | if (!RB_EMPTY_NODE(&inode->rb_node)) { |
| 5443 | rb_erase(&inode->rb_node, &root->inode_tree); |
| 5444 | RB_CLEAR_NODE(&inode->rb_node); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5445 | empty = RB_EMPTY_ROOT(&root->inode_tree); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5446 | } |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 5447 | spin_unlock(&root->inode_lock); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5448 | |
Stefan Behrens | 69e9c6c | 2013-09-05 16:58:43 +0200 | [diff] [blame] | 5449 | if (empty && btrfs_root_refs(&root->root_item) == 0) { |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5450 | spin_lock(&root->inode_lock); |
| 5451 | empty = RB_EMPTY_ROOT(&root->inode_tree); |
| 5452 | spin_unlock(&root->inode_lock); |
| 5453 | if (empty) |
| 5454 | btrfs_add_dead_root(root); |
| 5455 | } |
| 5456 | } |
| 5457 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5458 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5459 | static int btrfs_init_locked_inode(struct inode *inode, void *p) |
| 5460 | { |
| 5461 | struct btrfs_iget_args *args = p; |
David Sterba | 0202e83 | 2020-05-15 19:35:59 +0200 | [diff] [blame] | 5462 | |
| 5463 | inode->i_ino = args->ino; |
| 5464 | BTRFS_I(inode)->location.objectid = args->ino; |
| 5465 | BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY; |
| 5466 | BTRFS_I(inode)->location.offset = 0; |
Josef Bacik | 5c8fd99 | 2020-02-14 16:11:43 -0500 | [diff] [blame] | 5467 | BTRFS_I(inode)->root = btrfs_grab_root(args->root); |
| 5468 | BUG_ON(args->root && !BTRFS_I(inode)->root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5469 | return 0; |
| 5470 | } |
| 5471 | |
| 5472 | static int btrfs_find_actor(struct inode *inode, void *opaque) |
| 5473 | { |
| 5474 | struct btrfs_iget_args *args = opaque; |
David Sterba | 0202e83 | 2020-05-15 19:35:59 +0200 | [diff] [blame] | 5475 | |
| 5476 | return args->ino == BTRFS_I(inode)->location.objectid && |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5477 | args->root == BTRFS_I(inode)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5478 | } |
| 5479 | |
David Sterba | 0202e83 | 2020-05-15 19:35:59 +0200 | [diff] [blame] | 5480 | static struct inode *btrfs_iget_locked(struct super_block *s, u64 ino, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5481 | struct btrfs_root *root) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5482 | { |
| 5483 | struct inode *inode; |
| 5484 | struct btrfs_iget_args args; |
David Sterba | 0202e83 | 2020-05-15 19:35:59 +0200 | [diff] [blame] | 5485 | unsigned long hashval = btrfs_inode_hash(ino, root); |
Filipe David Borba Manana | 778ba82 | 2013-10-06 22:22:33 +0100 | [diff] [blame] | 5486 | |
David Sterba | 0202e83 | 2020-05-15 19:35:59 +0200 | [diff] [blame] | 5487 | args.ino = ino; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5488 | args.root = root; |
| 5489 | |
Filipe David Borba Manana | 778ba82 | 2013-10-06 22:22:33 +0100 | [diff] [blame] | 5490 | inode = iget5_locked(s, hashval, btrfs_find_actor, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5491 | btrfs_init_locked_inode, |
| 5492 | (void *)&args); |
| 5493 | return inode; |
| 5494 | } |
| 5495 | |
David Sterba | 4c66e0d | 2019-10-03 19:09:35 +0200 | [diff] [blame] | 5496 | /* |
David Sterba | 0202e83 | 2020-05-15 19:35:59 +0200 | [diff] [blame] | 5497 | * Get an inode object given its inode number and corresponding root. |
David Sterba | 4c66e0d | 2019-10-03 19:09:35 +0200 | [diff] [blame] | 5498 | * Path can be preallocated to prevent recursing back to iget through |
| 5499 | * allocator. NULL is also valid but may require an additional allocation |
| 5500 | * later. |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 5501 | */ |
David Sterba | 0202e83 | 2020-05-15 19:35:59 +0200 | [diff] [blame] | 5502 | struct inode *btrfs_iget_path(struct super_block *s, u64 ino, |
David Sterba | 4c66e0d | 2019-10-03 19:09:35 +0200 | [diff] [blame] | 5503 | struct btrfs_root *root, struct btrfs_path *path) |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 5504 | { |
| 5505 | struct inode *inode; |
| 5506 | |
David Sterba | 0202e83 | 2020-05-15 19:35:59 +0200 | [diff] [blame] | 5507 | inode = btrfs_iget_locked(s, ino, root); |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 5508 | if (!inode) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5509 | return ERR_PTR(-ENOMEM); |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 5510 | |
| 5511 | if (inode->i_state & I_NEW) { |
Filipe Manana | 6771089 | 2016-06-06 11:51:25 +0100 | [diff] [blame] | 5512 | int ret; |
| 5513 | |
Filipe Manana | 4222ea7 | 2018-10-24 10:13:03 +0100 | [diff] [blame] | 5514 | ret = btrfs_read_locked_inode(inode, path); |
Al Viro | 9bc2cef | 2018-07-29 23:04:50 +0100 | [diff] [blame] | 5515 | if (!ret) { |
Mark Fasheh | 1748f84 | 2011-07-12 11:25:31 -0700 | [diff] [blame] | 5516 | inode_tree_add(inode); |
| 5517 | unlock_new_inode(inode); |
Mark Fasheh | 1748f84 | 2011-07-12 11:25:31 -0700 | [diff] [blame] | 5518 | } else { |
Al Viro | f5b3a41 | 2018-07-29 23:04:51 +0100 | [diff] [blame] | 5519 | iget_failed(inode); |
| 5520 | /* |
| 5521 | * ret > 0 can come from btrfs_search_slot called by |
| 5522 | * btrfs_read_locked_inode, this means the inode item |
| 5523 | * was not found. |
| 5524 | */ |
| 5525 | if (ret > 0) |
| 5526 | ret = -ENOENT; |
| 5527 | inode = ERR_PTR(ret); |
Mark Fasheh | 1748f84 | 2011-07-12 11:25:31 -0700 | [diff] [blame] | 5528 | } |
| 5529 | } |
| 5530 | |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 5531 | return inode; |
| 5532 | } |
| 5533 | |
David Sterba | 0202e83 | 2020-05-15 19:35:59 +0200 | [diff] [blame] | 5534 | 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] | 5535 | { |
David Sterba | 0202e83 | 2020-05-15 19:35:59 +0200 | [diff] [blame] | 5536 | return btrfs_iget_path(s, ino, root, NULL); |
Filipe Manana | 4222ea7 | 2018-10-24 10:13:03 +0100 | [diff] [blame] | 5537 | } |
| 5538 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5539 | static struct inode *new_simple_dir(struct super_block *s, |
| 5540 | struct btrfs_key *key, |
| 5541 | struct btrfs_root *root) |
| 5542 | { |
| 5543 | struct inode *inode = new_inode(s); |
| 5544 | |
| 5545 | if (!inode) |
| 5546 | return ERR_PTR(-ENOMEM); |
| 5547 | |
Josef Bacik | 5c8fd99 | 2020-02-14 16:11:43 -0500 | [diff] [blame] | 5548 | BTRFS_I(inode)->root = btrfs_grab_root(root); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5549 | memcpy(&BTRFS_I(inode)->location, key, sizeof(*key)); |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 5550 | set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5551 | |
| 5552 | inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID; |
Omar Sandoval | 6bb6b51 | 2019-12-05 10:36:04 -0800 | [diff] [blame] | 5553 | /* |
| 5554 | * We only need lookup, the rest is read-only and there's no inode |
| 5555 | * associated with the dentry |
| 5556 | */ |
| 5557 | inode->i_op = &simple_dir_inode_operations; |
Omar Sandoval | 1fdf419 | 2017-01-25 17:06:39 -0800 | [diff] [blame] | 5558 | inode->i_opflags &= ~IOP_XATTR; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5559 | inode->i_fop = &simple_dir_operations; |
| 5560 | inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO; |
Deepa Dinamani | c2050a4 | 2016-09-14 07:48:06 -0700 | [diff] [blame] | 5561 | inode->i_mtime = current_time(inode); |
chandan r | 9cc97d6 | 2012-07-04 12:48:07 +0530 | [diff] [blame] | 5562 | inode->i_atime = inode->i_mtime; |
| 5563 | inode->i_ctime = inode->i_mtime; |
Arnd Bergmann | d3c6be6f | 2018-06-21 18:04:06 +0200 | [diff] [blame] | 5564 | BTRFS_I(inode)->i_otime = inode->i_mtime; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5565 | |
| 5566 | return inode; |
| 5567 | } |
| 5568 | |
Qu Wenruo | 6bf9e4b | 2019-03-13 13:55:11 +0800 | [diff] [blame] | 5569 | static inline u8 btrfs_inode_type(struct inode *inode) |
| 5570 | { |
| 5571 | /* |
| 5572 | * Compile-time asserts that generic FT_* types still match |
| 5573 | * BTRFS_FT_* types |
| 5574 | */ |
| 5575 | BUILD_BUG_ON(BTRFS_FT_UNKNOWN != FT_UNKNOWN); |
| 5576 | BUILD_BUG_ON(BTRFS_FT_REG_FILE != FT_REG_FILE); |
| 5577 | BUILD_BUG_ON(BTRFS_FT_DIR != FT_DIR); |
| 5578 | BUILD_BUG_ON(BTRFS_FT_CHRDEV != FT_CHRDEV); |
| 5579 | BUILD_BUG_ON(BTRFS_FT_BLKDEV != FT_BLKDEV); |
| 5580 | BUILD_BUG_ON(BTRFS_FT_FIFO != FT_FIFO); |
| 5581 | BUILD_BUG_ON(BTRFS_FT_SOCK != FT_SOCK); |
| 5582 | BUILD_BUG_ON(BTRFS_FT_SYMLINK != FT_SYMLINK); |
| 5583 | |
| 5584 | return fs_umode_to_ftype(inode->i_mode); |
| 5585 | } |
| 5586 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5587 | struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5588 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5589 | struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5590 | struct inode *inode; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5591 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5592 | struct btrfs_root *sub_root = root; |
| 5593 | struct btrfs_key location; |
Qu Wenruo | 6bf9e4b | 2019-03-13 13:55:11 +0800 | [diff] [blame] | 5594 | u8 di_type = 0; |
Josef Bacik | b4aff1f | 2011-06-28 16:18:59 -0400 | [diff] [blame] | 5595 | int ret = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5596 | |
| 5597 | if (dentry->d_name.len > BTRFS_NAME_LEN) |
| 5598 | return ERR_PTR(-ENAMETOOLONG); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5599 | |
Qu Wenruo | 6bf9e4b | 2019-03-13 13:55:11 +0800 | [diff] [blame] | 5600 | ret = btrfs_inode_by_name(dir, dentry, &location, &di_type); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5601 | if (ret < 0) |
| 5602 | return ERR_PTR(ret); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5603 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5604 | if (location.type == BTRFS_INODE_ITEM_KEY) { |
David Sterba | 0202e83 | 2020-05-15 19:35:59 +0200 | [diff] [blame] | 5605 | inode = btrfs_iget(dir->i_sb, location.objectid, root); |
Qu Wenruo | 6bf9e4b | 2019-03-13 13:55:11 +0800 | [diff] [blame] | 5606 | if (IS_ERR(inode)) |
| 5607 | return inode; |
| 5608 | |
| 5609 | /* Do extra check against inode mode with di_type */ |
| 5610 | if (btrfs_inode_type(inode) != di_type) { |
| 5611 | btrfs_crit(fs_info, |
| 5612 | "inode mode mismatch with dir: inode mode=0%o btrfs type=%u dir type=%u", |
| 5613 | inode->i_mode, btrfs_inode_type(inode), |
| 5614 | di_type); |
| 5615 | iput(inode); |
| 5616 | return ERR_PTR(-EUCLEAN); |
| 5617 | } |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5618 | return inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5619 | } |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5620 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5621 | ret = fixup_tree_root_location(fs_info, dir, dentry, |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5622 | &location, &sub_root); |
| 5623 | if (ret < 0) { |
| 5624 | if (ret != -ENOENT) |
| 5625 | inode = ERR_PTR(ret); |
| 5626 | else |
| 5627 | inode = new_simple_dir(dir->i_sb, &location, sub_root); |
| 5628 | } else { |
David Sterba | 0202e83 | 2020-05-15 19:35:59 +0200 | [diff] [blame] | 5629 | inode = btrfs_iget(dir->i_sb, location.objectid, sub_root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5630 | } |
Josef Bacik | 8727002 | 2020-01-24 09:32:31 -0500 | [diff] [blame] | 5631 | if (root != sub_root) |
Josef Bacik | 0024652 | 2020-01-24 09:33:01 -0500 | [diff] [blame] | 5632 | btrfs_put_root(sub_root); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5633 | |
Julia Lawall | 34d19ba | 2011-01-24 19:55:19 +0000 | [diff] [blame] | 5634 | if (!IS_ERR(inode) && root != sub_root) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5635 | down_read(&fs_info->cleanup_work_sem); |
David Howells | bc98a42 | 2017-07-17 08:45:34 +0100 | [diff] [blame] | 5636 | if (!sb_rdonly(inode->i_sb)) |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 5637 | ret = btrfs_orphan_cleanup(sub_root); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5638 | up_read(&fs_info->cleanup_work_sem); |
Josef Bacik | 01cd336 | 2013-06-03 21:39:49 -0400 | [diff] [blame] | 5639 | if (ret) { |
| 5640 | iput(inode); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 5641 | inode = ERR_PTR(ret); |
Josef Bacik | 01cd336 | 2013-06-03 21:39:49 -0400 | [diff] [blame] | 5642 | } |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 5643 | } |
| 5644 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5645 | return inode; |
| 5646 | } |
| 5647 | |
Nick Piggin | fe15ce4 | 2011-01-07 17:49:23 +1100 | [diff] [blame] | 5648 | static int btrfs_dentry_delete(const struct dentry *dentry) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5649 | { |
| 5650 | struct btrfs_root *root; |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 5651 | struct inode *inode = d_inode(dentry); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5652 | |
Li Zefan | 848cce0 | 2012-02-21 17:04:28 +0800 | [diff] [blame] | 5653 | if (!inode && !IS_ROOT(dentry)) |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 5654 | inode = d_inode(dentry->d_parent); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5655 | |
Li Zefan | 848cce0 | 2012-02-21 17:04:28 +0800 | [diff] [blame] | 5656 | if (inode) { |
| 5657 | root = BTRFS_I(inode)->root; |
Yan, Zheng | efefb14 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5658 | if (btrfs_root_refs(&root->root_item) == 0) |
| 5659 | return 1; |
Li Zefan | 848cce0 | 2012-02-21 17:04:28 +0800 | [diff] [blame] | 5660 | |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 5661 | if (btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) |
Li Zefan | 848cce0 | 2012-02-21 17:04:28 +0800 | [diff] [blame] | 5662 | return 1; |
Yan, Zheng | efefb14 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5663 | } |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5664 | return 0; |
| 5665 | } |
| 5666 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5667 | static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry, |
Al Viro | 00cd8dd | 2012-06-10 17:13:09 -0400 | [diff] [blame] | 5668 | unsigned int flags) |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5669 | { |
Al Viro | 3837d20 | 2018-10-10 16:38:27 -0400 | [diff] [blame] | 5670 | struct inode *inode = btrfs_lookup_dentry(dir, dentry); |
Josef Bacik | a66e7cc | 2011-09-18 10:34:03 -0400 | [diff] [blame] | 5671 | |
Al Viro | 3837d20 | 2018-10-10 16:38:27 -0400 | [diff] [blame] | 5672 | if (inode == ERR_PTR(-ENOENT)) |
| 5673 | inode = NULL; |
Al Viro | 41d28bc | 2014-10-12 22:24:21 -0400 | [diff] [blame] | 5674 | return d_splice_alias(inode, dentry); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5675 | } |
| 5676 | |
Josef Bacik | 23b5ec7 | 2017-07-24 15:14:25 -0400 | [diff] [blame] | 5677 | /* |
| 5678 | * All this infrastructure exists because dir_emit can fault, and we are holding |
| 5679 | * the tree lock when doing readdir. For now just allocate a buffer and copy |
| 5680 | * our information into that, and then dir_emit from the buffer. This is |
| 5681 | * similar to what NFS does, only we don't keep the buffer around in pagecache |
| 5682 | * because I'm afraid I'll mess that up. Long term we need to make filldir do |
| 5683 | * copy_to_user_inatomic so we don't have to worry about page faulting under the |
| 5684 | * tree lock. |
| 5685 | */ |
| 5686 | static int btrfs_opendir(struct inode *inode, struct file *file) |
| 5687 | { |
| 5688 | struct btrfs_file_private *private; |
| 5689 | |
| 5690 | private = kzalloc(sizeof(struct btrfs_file_private), GFP_KERNEL); |
| 5691 | if (!private) |
| 5692 | return -ENOMEM; |
| 5693 | private->filldir_buf = kzalloc(PAGE_SIZE, GFP_KERNEL); |
| 5694 | if (!private->filldir_buf) { |
| 5695 | kfree(private); |
| 5696 | return -ENOMEM; |
| 5697 | } |
| 5698 | file->private_data = private; |
| 5699 | return 0; |
| 5700 | } |
| 5701 | |
| 5702 | struct dir_entry { |
| 5703 | u64 ino; |
| 5704 | u64 offset; |
| 5705 | unsigned type; |
| 5706 | int name_len; |
| 5707 | }; |
| 5708 | |
| 5709 | static int btrfs_filldir(void *addr, int entries, struct dir_context *ctx) |
| 5710 | { |
| 5711 | while (entries--) { |
| 5712 | struct dir_entry *entry = addr; |
| 5713 | char *name = (char *)(entry + 1); |
| 5714 | |
David Sterba | 92d3217 | 2018-04-16 21:10:14 +0200 | [diff] [blame] | 5715 | ctx->pos = get_unaligned(&entry->offset); |
| 5716 | if (!dir_emit(ctx, name, get_unaligned(&entry->name_len), |
| 5717 | get_unaligned(&entry->ino), |
| 5718 | get_unaligned(&entry->type))) |
Josef Bacik | 23b5ec7 | 2017-07-24 15:14:25 -0400 | [diff] [blame] | 5719 | return 1; |
David Sterba | 92d3217 | 2018-04-16 21:10:14 +0200 | [diff] [blame] | 5720 | addr += sizeof(struct dir_entry) + |
| 5721 | get_unaligned(&entry->name_len); |
Josef Bacik | 23b5ec7 | 2017-07-24 15:14:25 -0400 | [diff] [blame] | 5722 | ctx->pos++; |
| 5723 | } |
| 5724 | return 0; |
| 5725 | } |
| 5726 | |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5727 | static int btrfs_real_readdir(struct file *file, struct dir_context *ctx) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5728 | { |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5729 | struct inode *inode = file_inode(file); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5730 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Josef Bacik | 23b5ec7 | 2017-07-24 15:14:25 -0400 | [diff] [blame] | 5731 | struct btrfs_file_private *private = file->private_data; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5732 | struct btrfs_dir_item *di; |
| 5733 | struct btrfs_key key; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5734 | struct btrfs_key found_key; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5735 | struct btrfs_path *path; |
Josef Bacik | 23b5ec7 | 2017-07-24 15:14:25 -0400 | [diff] [blame] | 5736 | void *addr; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5737 | struct list_head ins_list; |
| 5738 | struct list_head del_list; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5739 | int ret; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5740 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5741 | int slot; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5742 | char *name_ptr; |
| 5743 | int name_len; |
Josef Bacik | 23b5ec7 | 2017-07-24 15:14:25 -0400 | [diff] [blame] | 5744 | int entries = 0; |
| 5745 | int total_len = 0; |
Omar Sandoval | 02dbfc9 | 2016-05-20 13:50:33 -0700 | [diff] [blame] | 5746 | bool put = false; |
Jeff Mahoney | c2951f3 | 2016-11-21 15:59:04 +0100 | [diff] [blame] | 5747 | struct btrfs_key location; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5748 | |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5749 | if (!dir_emit_dots(file, ctx)) |
| 5750 | return 0; |
| 5751 | |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5752 | path = btrfs_alloc_path(); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5753 | if (!path) |
| 5754 | return -ENOMEM; |
Chris Mason | ff5714c | 2011-05-28 07:00:39 -0400 | [diff] [blame] | 5755 | |
Josef Bacik | 23b5ec7 | 2017-07-24 15:14:25 -0400 | [diff] [blame] | 5756 | addr = private->filldir_buf; |
David Sterba | e4058b5 | 2015-11-27 16:31:35 +0100 | [diff] [blame] | 5757 | path->reada = READA_FORWARD; |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5758 | |
Jeff Mahoney | c2951f3 | 2016-11-21 15:59:04 +0100 | [diff] [blame] | 5759 | INIT_LIST_HEAD(&ins_list); |
| 5760 | INIT_LIST_HEAD(&del_list); |
| 5761 | put = btrfs_readdir_get_delayed_items(inode, &ins_list, &del_list); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5762 | |
Josef Bacik | 23b5ec7 | 2017-07-24 15:14:25 -0400 | [diff] [blame] | 5763 | again: |
Jeff Mahoney | c2951f3 | 2016-11-21 15:59:04 +0100 | [diff] [blame] | 5764 | key.type = BTRFS_DIR_INDEX_KEY; |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5765 | key.offset = ctx->pos; |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 5766 | key.objectid = btrfs_ino(BTRFS_I(inode)); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5767 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5768 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 5769 | if (ret < 0) |
| 5770 | goto err; |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5771 | |
| 5772 | while (1) { |
Josef Bacik | 23b5ec7 | 2017-07-24 15:14:25 -0400 | [diff] [blame] | 5773 | struct dir_entry *entry; |
| 5774 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5775 | leaf = path->nodes[0]; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5776 | slot = path->slots[0]; |
Li Zefan | b9e03af | 2011-03-23 10:43:58 +0800 | [diff] [blame] | 5777 | if (slot >= btrfs_header_nritems(leaf)) { |
| 5778 | ret = btrfs_next_leaf(root, path); |
| 5779 | if (ret < 0) |
| 5780 | goto err; |
| 5781 | else if (ret > 0) |
| 5782 | break; |
| 5783 | continue; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5784 | } |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5785 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5786 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
| 5787 | |
| 5788 | if (found_key.objectid != key.objectid) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5789 | break; |
Jeff Mahoney | c2951f3 | 2016-11-21 15:59:04 +0100 | [diff] [blame] | 5790 | if (found_key.type != BTRFS_DIR_INDEX_KEY) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5791 | break; |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5792 | if (found_key.offset < ctx->pos) |
Li Zefan | b9e03af | 2011-03-23 10:43:58 +0800 | [diff] [blame] | 5793 | goto next; |
Jeff Mahoney | c2951f3 | 2016-11-21 15:59:04 +0100 | [diff] [blame] | 5794 | if (btrfs_should_delete_dir_index(&del_list, found_key.offset)) |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5795 | goto next; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5796 | di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item); |
Jeff Mahoney | c2951f3 | 2016-11-21 15:59:04 +0100 | [diff] [blame] | 5797 | name_len = btrfs_dir_name_len(leaf, di); |
Josef Bacik | 23b5ec7 | 2017-07-24 15:14:25 -0400 | [diff] [blame] | 5798 | if ((total_len + sizeof(struct dir_entry) + name_len) >= |
| 5799 | PAGE_SIZE) { |
| 5800 | btrfs_release_path(path); |
| 5801 | ret = btrfs_filldir(private->filldir_buf, entries, ctx); |
| 5802 | if (ret) |
| 5803 | goto nopos; |
| 5804 | addr = private->filldir_buf; |
| 5805 | entries = 0; |
| 5806 | total_len = 0; |
| 5807 | goto again; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5808 | } |
Josef Bacik | 23b5ec7 | 2017-07-24 15:14:25 -0400 | [diff] [blame] | 5809 | |
| 5810 | entry = addr; |
David Sterba | 92d3217 | 2018-04-16 21:10:14 +0200 | [diff] [blame] | 5811 | put_unaligned(name_len, &entry->name_len); |
Josef Bacik | 23b5ec7 | 2017-07-24 15:14:25 -0400 | [diff] [blame] | 5812 | name_ptr = (char *)(entry + 1); |
Jeff Mahoney | c2951f3 | 2016-11-21 15:59:04 +0100 | [diff] [blame] | 5813 | read_extent_buffer(leaf, name_ptr, (unsigned long)(di + 1), |
| 5814 | name_len); |
Phillip Potter | 7d157c3 | 2019-03-26 21:39:34 +0000 | [diff] [blame] | 5815 | put_unaligned(fs_ftype_to_dtype(btrfs_dir_type(leaf, di)), |
David Sterba | 92d3217 | 2018-04-16 21:10:14 +0200 | [diff] [blame] | 5816 | &entry->type); |
Jeff Mahoney | c2951f3 | 2016-11-21 15:59:04 +0100 | [diff] [blame] | 5817 | btrfs_dir_item_key_to_cpu(leaf, di, &location); |
David Sterba | 92d3217 | 2018-04-16 21:10:14 +0200 | [diff] [blame] | 5818 | put_unaligned(location.objectid, &entry->ino); |
| 5819 | put_unaligned(found_key.offset, &entry->offset); |
Josef Bacik | 23b5ec7 | 2017-07-24 15:14:25 -0400 | [diff] [blame] | 5820 | entries++; |
| 5821 | addr += sizeof(struct dir_entry) + name_len; |
| 5822 | total_len += sizeof(struct dir_entry) + name_len; |
Li Zefan | b9e03af | 2011-03-23 10:43:58 +0800 | [diff] [blame] | 5823 | next: |
| 5824 | path->slots[0]++; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5825 | } |
Josef Bacik | 23b5ec7 | 2017-07-24 15:14:25 -0400 | [diff] [blame] | 5826 | btrfs_release_path(path); |
| 5827 | |
| 5828 | ret = btrfs_filldir(private->filldir_buf, entries, ctx); |
| 5829 | if (ret) |
| 5830 | goto nopos; |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5831 | |
Jeff Mahoney | d2fbb2b | 2016-11-05 13:26:35 -0400 | [diff] [blame] | 5832 | ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list); |
Jeff Mahoney | c2951f3 | 2016-11-21 15:59:04 +0100 | [diff] [blame] | 5833 | if (ret) |
David Sterba | bc4ef75 | 2015-11-13 13:44:28 +0100 | [diff] [blame] | 5834 | goto nopos; |
| 5835 | |
Zach Brown | db62efb | 2013-07-11 16:19:42 -0700 | [diff] [blame] | 5836 | /* |
| 5837 | * Stop new entries from being returned after we return the last |
| 5838 | * entry. |
| 5839 | * |
| 5840 | * New directory entries are assigned a strictly increasing |
| 5841 | * offset. This means that new entries created during readdir |
| 5842 | * are *guaranteed* to be seen in the future by that readdir. |
| 5843 | * This has broken buggy programs which operate on names as |
| 5844 | * they're returned by readdir. Until we re-use freed offsets |
| 5845 | * we have this hack to stop new entries from being returned |
| 5846 | * under the assumption that they'll never reach this huge |
| 5847 | * offset. |
| 5848 | * |
| 5849 | * This is being careful not to overflow 32bit loff_t unless the |
| 5850 | * last entry requires it because doing so has broken 32bit apps |
| 5851 | * in the past. |
| 5852 | */ |
Jeff Mahoney | c2951f3 | 2016-11-21 15:59:04 +0100 | [diff] [blame] | 5853 | if (ctx->pos >= INT_MAX) |
| 5854 | ctx->pos = LLONG_MAX; |
| 5855 | else |
| 5856 | ctx->pos = INT_MAX; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5857 | nopos: |
| 5858 | ret = 0; |
| 5859 | err: |
Omar Sandoval | 02dbfc9 | 2016-05-20 13:50:33 -0700 | [diff] [blame] | 5860 | if (put) |
| 5861 | btrfs_readdir_put_delayed_items(inode, &ins_list, &del_list); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5862 | btrfs_free_path(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5863 | return ret; |
| 5864 | } |
| 5865 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5866 | /* |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 5867 | * This is somewhat expensive, updating the tree every time the |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5868 | * inode changes. But, it is most likely to find the inode in cache. |
| 5869 | * FIXME, needs more benchmarking...there are no reasons other than performance |
| 5870 | * to keep or drop this code. |
| 5871 | */ |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 5872 | static int btrfs_dirty_inode(struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5873 | { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5874 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5875 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5876 | struct btrfs_trans_handle *trans; |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5877 | int ret; |
| 5878 | |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 5879 | if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags)) |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 5880 | return 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5881 | |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 5882 | trans = btrfs_join_transaction(root); |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 5883 | if (IS_ERR(trans)) |
| 5884 | return PTR_ERR(trans); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5885 | |
Nikolay Borisov | 9a56fcd | 2020-11-02 16:48:59 +0200 | [diff] [blame] | 5886 | ret = btrfs_update_inode(trans, root, BTRFS_I(inode)); |
Nikolay Borisov | 4d14c5c | 2021-02-22 18:40:44 +0200 | [diff] [blame] | 5887 | if (ret && (ret == -ENOSPC || ret == -EDQUOT)) { |
Chris Mason | 94b6044 | 2010-05-26 11:02:00 -0400 | [diff] [blame] | 5888 | /* whoops, lets try again with the full transaction */ |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 5889 | btrfs_end_transaction(trans); |
Chris Mason | 94b6044 | 2010-05-26 11:02:00 -0400 | [diff] [blame] | 5890 | trans = btrfs_start_transaction(root, 1); |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 5891 | if (IS_ERR(trans)) |
| 5892 | return PTR_ERR(trans); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5893 | |
Nikolay Borisov | 9a56fcd | 2020-11-02 16:48:59 +0200 | [diff] [blame] | 5894 | ret = btrfs_update_inode(trans, root, BTRFS_I(inode)); |
Chris Mason | 94b6044 | 2010-05-26 11:02:00 -0400 | [diff] [blame] | 5895 | } |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 5896 | btrfs_end_transaction(trans); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5897 | if (BTRFS_I(inode)->delayed_node) |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5898 | btrfs_balance_delayed_items(fs_info); |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 5899 | |
| 5900 | return ret; |
| 5901 | } |
| 5902 | |
| 5903 | /* |
| 5904 | * This is a copy of file_update_time. We need this so we can return error on |
| 5905 | * ENOSPC for updating the inode in the case of file write and mmap writes. |
| 5906 | */ |
Deepa Dinamani | 95582b0 | 2018-05-08 19:36:02 -0700 | [diff] [blame] | 5907 | static int btrfs_update_time(struct inode *inode, struct timespec64 *now, |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 5908 | int flags) |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 5909 | { |
Alexander Block | 2bc556528 | 2012-06-15 09:49:33 +0200 | [diff] [blame] | 5910 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Jeff Layton | 3a8c723 | 2017-12-11 06:35:24 -0500 | [diff] [blame] | 5911 | bool dirty = flags & ~S_VERSION; |
Alexander Block | 2bc556528 | 2012-06-15 09:49:33 +0200 | [diff] [blame] | 5912 | |
| 5913 | if (btrfs_root_readonly(root)) |
| 5914 | return -EROFS; |
| 5915 | |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 5916 | if (flags & S_VERSION) |
Jeff Layton | 3a8c723 | 2017-12-11 06:35:24 -0500 | [diff] [blame] | 5917 | dirty |= inode_maybe_inc_iversion(inode, dirty); |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 5918 | if (flags & S_CTIME) |
| 5919 | inode->i_ctime = *now; |
| 5920 | if (flags & S_MTIME) |
| 5921 | inode->i_mtime = *now; |
| 5922 | if (flags & S_ATIME) |
| 5923 | inode->i_atime = *now; |
Jeff Layton | 3a8c723 | 2017-12-11 06:35:24 -0500 | [diff] [blame] | 5924 | return dirty ? btrfs_dirty_inode(inode) : 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5925 | } |
| 5926 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 5927 | /* |
| 5928 | * find the highest existing sequence number in a directory |
| 5929 | * and then set the in-memory index_cnt variable to reflect |
| 5930 | * free sequence numbers |
| 5931 | */ |
Nikolay Borisov | 4c570655 | 2017-02-20 13:50:32 +0200 | [diff] [blame] | 5932 | static int btrfs_set_inode_index_count(struct btrfs_inode *inode) |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5933 | { |
Nikolay Borisov | 4c570655 | 2017-02-20 13:50:32 +0200 | [diff] [blame] | 5934 | struct btrfs_root *root = inode->root; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5935 | struct btrfs_key key, found_key; |
| 5936 | struct btrfs_path *path; |
| 5937 | struct extent_buffer *leaf; |
| 5938 | int ret; |
| 5939 | |
Nikolay Borisov | 4c570655 | 2017-02-20 13:50:32 +0200 | [diff] [blame] | 5940 | key.objectid = btrfs_ino(inode); |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 5941 | key.type = BTRFS_DIR_INDEX_KEY; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5942 | key.offset = (u64)-1; |
| 5943 | |
| 5944 | path = btrfs_alloc_path(); |
| 5945 | if (!path) |
| 5946 | return -ENOMEM; |
| 5947 | |
| 5948 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 5949 | if (ret < 0) |
| 5950 | goto out; |
| 5951 | /* FIXME: we should be able to handle this */ |
| 5952 | if (ret == 0) |
| 5953 | goto out; |
| 5954 | ret = 0; |
| 5955 | |
| 5956 | /* |
| 5957 | * MAGIC NUMBER EXPLANATION: |
| 5958 | * since we search a directory based on f_pos we have to start at 2 |
| 5959 | * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody |
| 5960 | * else has to start at 2 |
| 5961 | */ |
| 5962 | if (path->slots[0] == 0) { |
Nikolay Borisov | 4c570655 | 2017-02-20 13:50:32 +0200 | [diff] [blame] | 5963 | inode->index_cnt = 2; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5964 | goto out; |
| 5965 | } |
| 5966 | |
| 5967 | path->slots[0]--; |
| 5968 | |
| 5969 | leaf = path->nodes[0]; |
| 5970 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 5971 | |
Nikolay Borisov | 4c570655 | 2017-02-20 13:50:32 +0200 | [diff] [blame] | 5972 | if (found_key.objectid != btrfs_ino(inode) || |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 5973 | found_key.type != BTRFS_DIR_INDEX_KEY) { |
Nikolay Borisov | 4c570655 | 2017-02-20 13:50:32 +0200 | [diff] [blame] | 5974 | inode->index_cnt = 2; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5975 | goto out; |
| 5976 | } |
| 5977 | |
Nikolay Borisov | 4c570655 | 2017-02-20 13:50:32 +0200 | [diff] [blame] | 5978 | inode->index_cnt = found_key.offset + 1; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5979 | out: |
| 5980 | btrfs_free_path(path); |
| 5981 | return ret; |
| 5982 | } |
| 5983 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 5984 | /* |
| 5985 | * helper to find a free sequence number in a given directory. This current |
| 5986 | * code is very simple, later versions will do smarter things in the btree |
| 5987 | */ |
Nikolay Borisov | 877574e | 2017-02-20 13:50:33 +0200 | [diff] [blame] | 5988 | int btrfs_set_inode_index(struct btrfs_inode *dir, u64 *index) |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5989 | { |
| 5990 | int ret = 0; |
| 5991 | |
Nikolay Borisov | 877574e | 2017-02-20 13:50:33 +0200 | [diff] [blame] | 5992 | if (dir->index_cnt == (u64)-1) { |
| 5993 | ret = btrfs_inode_delayed_dir_index_count(dir); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5994 | if (ret) { |
| 5995 | ret = btrfs_set_inode_index_count(dir); |
| 5996 | if (ret) |
| 5997 | return ret; |
| 5998 | } |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5999 | } |
| 6000 | |
Nikolay Borisov | 877574e | 2017-02-20 13:50:33 +0200 | [diff] [blame] | 6001 | *index = dir->index_cnt; |
| 6002 | dir->index_cnt++; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6003 | |
| 6004 | return ret; |
| 6005 | } |
| 6006 | |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6007 | static int btrfs_insert_inode_locked(struct inode *inode) |
| 6008 | { |
| 6009 | struct btrfs_iget_args args; |
David Sterba | 0202e83 | 2020-05-15 19:35:59 +0200 | [diff] [blame] | 6010 | |
| 6011 | args.ino = BTRFS_I(inode)->location.objectid; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6012 | args.root = BTRFS_I(inode)->root; |
| 6013 | |
| 6014 | return insert_inode_locked4(inode, |
| 6015 | btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root), |
| 6016 | btrfs_find_actor, &args); |
| 6017 | } |
| 6018 | |
Anand Jain | 19aee8d | 2017-07-18 17:37:05 +0800 | [diff] [blame] | 6019 | /* |
| 6020 | * Inherit flags from the parent inode. |
| 6021 | * |
| 6022 | * Currently only the compression flags and the cow flags are inherited. |
| 6023 | */ |
| 6024 | static void btrfs_inherit_iflags(struct inode *inode, struct inode *dir) |
| 6025 | { |
| 6026 | unsigned int flags; |
| 6027 | |
| 6028 | if (!dir) |
| 6029 | return; |
| 6030 | |
| 6031 | flags = BTRFS_I(dir)->flags; |
| 6032 | |
| 6033 | if (flags & BTRFS_INODE_NOCOMPRESS) { |
| 6034 | BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS; |
| 6035 | BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS; |
| 6036 | } else if (flags & BTRFS_INODE_COMPRESS) { |
| 6037 | BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS; |
| 6038 | BTRFS_I(inode)->flags |= BTRFS_INODE_COMPRESS; |
| 6039 | } |
| 6040 | |
| 6041 | if (flags & BTRFS_INODE_NODATACOW) { |
| 6042 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW; |
| 6043 | if (S_ISREG(inode->i_mode)) |
| 6044 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM; |
| 6045 | } |
| 6046 | |
David Sterba | 7b6a221 | 2018-03-26 18:40:21 +0200 | [diff] [blame] | 6047 | btrfs_sync_inode_flags_to_i_flags(inode); |
Anand Jain | 19aee8d | 2017-07-18 17:37:05 +0800 | [diff] [blame] | 6048 | } |
| 6049 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6050 | static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans, |
| 6051 | struct btrfs_root *root, |
Christian Brauner | b3b6f5b | 2021-07-27 12:48:41 +0200 | [diff] [blame] | 6052 | struct user_namespace *mnt_userns, |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6053 | struct inode *dir, |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 6054 | const char *name, int name_len, |
Al Viro | 175a4eb | 2011-07-26 03:30:54 -0400 | [diff] [blame] | 6055 | u64 ref_objectid, u64 objectid, |
| 6056 | umode_t mode, u64 *index) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6057 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6058 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6059 | struct inode *inode; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6060 | struct btrfs_inode_item *inode_item; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6061 | struct btrfs_key *location; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6062 | struct btrfs_path *path; |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 6063 | struct btrfs_inode_ref *ref; |
| 6064 | struct btrfs_key key[2]; |
| 6065 | u32 sizes[2]; |
Filipe Manana | b7ef5f3 | 2021-09-24 12:28:13 +0100 | [diff] [blame] | 6066 | struct btrfs_item_batch batch; |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 6067 | unsigned long ptr; |
Josef Bacik | 11a19a9 | 2019-09-09 10:12:04 -0400 | [diff] [blame] | 6068 | unsigned int nofs_flag; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6069 | int ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6070 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6071 | path = btrfs_alloc_path(); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 6072 | if (!path) |
| 6073 | return ERR_PTR(-ENOMEM); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6074 | |
Josef Bacik | 11a19a9 | 2019-09-09 10:12:04 -0400 | [diff] [blame] | 6075 | nofs_flag = memalloc_nofs_save(); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6076 | inode = new_inode(fs_info->sb); |
Josef Bacik | 11a19a9 | 2019-09-09 10:12:04 -0400 | [diff] [blame] | 6077 | memalloc_nofs_restore(nofs_flag); |
Yoshinori Sano | 8fb2764 | 2011-04-09 02:30:07 +0000 | [diff] [blame] | 6078 | if (!inode) { |
| 6079 | btrfs_free_path(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6080 | return ERR_PTR(-ENOMEM); |
Yoshinori Sano | 8fb2764 | 2011-04-09 02:30:07 +0000 | [diff] [blame] | 6081 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6082 | |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 6083 | /* |
Filipe Manana | 5762b5c | 2014-08-01 00:10:32 +0100 | [diff] [blame] | 6084 | * O_TMPFILE, set link count to 0, so that after this point, |
| 6085 | * we fill in an inode item with the correct link count. |
| 6086 | */ |
| 6087 | if (!name) |
| 6088 | set_nlink(inode, 0); |
| 6089 | |
| 6090 | /* |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 6091 | * we have to initialize this early, so we can reclaim the inode |
| 6092 | * number if we fail afterwards in this function. |
| 6093 | */ |
| 6094 | inode->i_ino = objectid; |
| 6095 | |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 6096 | if (dir && name) { |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 6097 | trace_btrfs_inode_request(dir); |
| 6098 | |
Nikolay Borisov | 877574e | 2017-02-20 13:50:33 +0200 | [diff] [blame] | 6099 | ret = btrfs_set_inode_index(BTRFS_I(dir), index); |
Shen Feng | 0977143 | 2009-04-02 16:46:06 -0400 | [diff] [blame] | 6100 | if (ret) { |
Yoshinori Sano | 8fb2764 | 2011-04-09 02:30:07 +0000 | [diff] [blame] | 6101 | btrfs_free_path(path); |
Shen Feng | 0977143 | 2009-04-02 16:46:06 -0400 | [diff] [blame] | 6102 | iput(inode); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6103 | return ERR_PTR(ret); |
Shen Feng | 0977143 | 2009-04-02 16:46:06 -0400 | [diff] [blame] | 6104 | } |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 6105 | } else if (dir) { |
| 6106 | *index = 0; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6107 | } |
| 6108 | /* |
| 6109 | * index_cnt is ignored for everything but a dir, |
Su Yue | df6703e | 2018-01-12 11:08:02 +0800 | [diff] [blame] | 6110 | * btrfs_set_inode_index_count has an explanation for the magic |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6111 | * number |
| 6112 | */ |
| 6113 | BTRFS_I(inode)->index_cnt = 2; |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 6114 | BTRFS_I(inode)->dir_index = *index; |
Josef Bacik | 5c8fd99 | 2020-02-14 16:11:43 -0500 | [diff] [blame] | 6115 | BTRFS_I(inode)->root = btrfs_grab_root(root); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 6116 | BTRFS_I(inode)->generation = trans->transid; |
Josef Bacik | 7619585 | 2010-11-19 02:18:02 +0000 | [diff] [blame] | 6117 | inode->i_generation = BTRFS_I(inode)->generation; |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6118 | |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 6119 | /* |
| 6120 | * We could have gotten an inode number from somebody who was fsynced |
| 6121 | * and then removed in this same transaction, so let's just set full |
| 6122 | * sync since it will be a full sync anyway and this will blow away the |
| 6123 | * old info in the log. |
| 6124 | */ |
| 6125 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags); |
| 6126 | |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 6127 | key[0].objectid = objectid; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 6128 | key[0].type = BTRFS_INODE_ITEM_KEY; |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 6129 | key[0].offset = 0; |
| 6130 | |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 6131 | sizes[0] = sizeof(struct btrfs_inode_item); |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 6132 | |
| 6133 | if (name) { |
| 6134 | /* |
| 6135 | * Start new inodes with an inode_ref. This is slightly more |
| 6136 | * efficient for small numbers of hard links since they will |
| 6137 | * be packed into one item. Extended refs will kick in if we |
| 6138 | * add more hard links than can fit in the ref item. |
| 6139 | */ |
| 6140 | key[1].objectid = objectid; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 6141 | key[1].type = BTRFS_INODE_REF_KEY; |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 6142 | key[1].offset = ref_objectid; |
| 6143 | |
| 6144 | sizes[1] = name_len + sizeof(*ref); |
| 6145 | } |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 6146 | |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6147 | location = &BTRFS_I(inode)->location; |
| 6148 | location->objectid = objectid; |
| 6149 | location->offset = 0; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 6150 | location->type = BTRFS_INODE_ITEM_KEY; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6151 | |
| 6152 | ret = btrfs_insert_inode_locked(inode); |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6153 | if (ret < 0) { |
| 6154 | iput(inode); |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6155 | goto fail; |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6156 | } |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6157 | |
Filipe Manana | b7ef5f3 | 2021-09-24 12:28:13 +0100 | [diff] [blame] | 6158 | batch.keys = &key[0]; |
| 6159 | batch.data_sizes = &sizes[0]; |
| 6160 | batch.total_data_size = sizes[0] + (name ? sizes[1] : 0); |
| 6161 | batch.nr = name ? 2 : 1; |
| 6162 | ret = btrfs_insert_empty_items(trans, root, path, &batch); |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 6163 | if (ret != 0) |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6164 | goto fail_unlock; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6165 | |
Christian Brauner | b3b6f5b | 2021-07-27 12:48:41 +0200 | [diff] [blame] | 6166 | inode_init_owner(mnt_userns, inode, dir, mode); |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 6167 | inode_set_bytes(inode, 0); |
chandan r | 9cc97d6 | 2012-07-04 12:48:07 +0530 | [diff] [blame] | 6168 | |
Deepa Dinamani | c2050a4 | 2016-09-14 07:48:06 -0700 | [diff] [blame] | 6169 | inode->i_mtime = current_time(inode); |
chandan r | 9cc97d6 | 2012-07-04 12:48:07 +0530 | [diff] [blame] | 6170 | inode->i_atime = inode->i_mtime; |
| 6171 | inode->i_ctime = inode->i_mtime; |
Arnd Bergmann | d3c6be6f | 2018-06-21 18:04:06 +0200 | [diff] [blame] | 6172 | BTRFS_I(inode)->i_otime = inode->i_mtime; |
chandan r | 9cc97d6 | 2012-07-04 12:48:07 +0530 | [diff] [blame] | 6173 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6174 | inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 6175 | struct btrfs_inode_item); |
David Sterba | b159fa2 | 2016-11-08 18:09:03 +0100 | [diff] [blame] | 6176 | memzero_extent_buffer(path->nodes[0], (unsigned long)inode_item, |
Li Zefan | 293f7e0 | 2012-07-10 00:58:58 -0600 | [diff] [blame] | 6177 | sizeof(*inode_item)); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 6178 | fill_inode_item(trans, path->nodes[0], inode_item, inode); |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 6179 | |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 6180 | if (name) { |
| 6181 | ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1, |
| 6182 | struct btrfs_inode_ref); |
| 6183 | btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len); |
| 6184 | btrfs_set_inode_ref_index(path->nodes[0], ref, *index); |
| 6185 | ptr = (unsigned long)(ref + 1); |
| 6186 | write_extent_buffer(path->nodes[0], name, ptr, name_len); |
| 6187 | } |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 6188 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6189 | btrfs_mark_buffer_dirty(path->nodes[0]); |
| 6190 | btrfs_free_path(path); |
| 6191 | |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 6192 | btrfs_inherit_iflags(inode, dir); |
| 6193 | |
Al Viro | 569254b | 2011-07-24 17:08:40 -0400 | [diff] [blame] | 6194 | if (S_ISREG(mode)) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6195 | if (btrfs_test_opt(fs_info, NODATASUM)) |
Chris Mason | 9427216 | 2009-07-02 12:26:06 -0400 | [diff] [blame] | 6196 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6197 | if (btrfs_test_opt(fs_info, NODATACOW)) |
Josef Bacik | f2bdf9a | 2013-02-21 15:28:28 -0500 | [diff] [blame] | 6198 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW | |
| 6199 | BTRFS_INODE_NODATASUM; |
Chris Mason | 9427216 | 2009-07-02 12:26:06 -0400 | [diff] [blame] | 6200 | } |
| 6201 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6202 | inode_tree_add(inode); |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 6203 | |
| 6204 | trace_btrfs_inode_new(inode); |
Nikolay Borisov | d909441 | 2020-06-05 10:41:13 +0300 | [diff] [blame] | 6205 | btrfs_set_inode_last_trans(trans, BTRFS_I(inode)); |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 6206 | |
Alexander Block | 8ea05e3 | 2012-07-25 17:35:53 +0200 | [diff] [blame] | 6207 | btrfs_update_root_times(trans, root); |
| 6208 | |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 6209 | ret = btrfs_inode_inherit_props(trans, inode, dir); |
| 6210 | if (ret) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6211 | btrfs_err(fs_info, |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 6212 | "error inheriting props for ino %llu (root %llu): %d", |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 6213 | btrfs_ino(BTRFS_I(inode)), root->root_key.objectid, ret); |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 6214 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6215 | return inode; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6216 | |
| 6217 | fail_unlock: |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6218 | discard_new_inode(inode); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6219 | fail: |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 6220 | if (dir && name) |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6221 | BTRFS_I(dir)->index_cnt--; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6222 | btrfs_free_path(path); |
| 6223 | return ERR_PTR(ret); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6224 | } |
| 6225 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 6226 | /* |
| 6227 | * utility function to add 'inode' into 'parent_inode' with |
| 6228 | * a give name and a given sequence number. |
| 6229 | * if 'add_backref' is true, also insert a backref from the |
| 6230 | * inode to the parent directory. |
| 6231 | */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 6232 | int btrfs_add_link(struct btrfs_trans_handle *trans, |
Nikolay Borisov | db0a669 | 2017-02-20 13:51:08 +0200 | [diff] [blame] | 6233 | struct btrfs_inode *parent_inode, struct btrfs_inode *inode, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 6234 | const char *name, int name_len, int add_backref, u64 index) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6235 | { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6236 | int ret = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6237 | struct btrfs_key key; |
Nikolay Borisov | db0a669 | 2017-02-20 13:51:08 +0200 | [diff] [blame] | 6238 | struct btrfs_root *root = parent_inode->root; |
| 6239 | u64 ino = btrfs_ino(inode); |
| 6240 | u64 parent_ino = btrfs_ino(parent_inode); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6241 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6242 | if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) { |
Nikolay Borisov | db0a669 | 2017-02-20 13:51:08 +0200 | [diff] [blame] | 6243 | memcpy(&key, &inode->root->root_key, sizeof(key)); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6244 | } else { |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6245 | key.objectid = ino; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 6246 | key.type = BTRFS_INODE_ITEM_KEY; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6247 | key.offset = 0; |
| 6248 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6249 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6250 | if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) { |
Lu Fengqi | 6025c19 | 2018-08-01 11:32:29 +0800 | [diff] [blame] | 6251 | ret = btrfs_add_root_ref(trans, key.objectid, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6252 | root->root_key.objectid, parent_ino, |
| 6253 | index, name, name_len); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6254 | } else if (add_backref) { |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6255 | ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino, |
| 6256 | parent_ino, index); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6257 | } |
| 6258 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6259 | /* Nothing to clean up yet */ |
| 6260 | if (ret) |
| 6261 | return ret; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6262 | |
Lu Fengqi | 684572d | 2018-08-04 21:10:57 +0800 | [diff] [blame] | 6263 | ret = btrfs_insert_dir_item(trans, name, name_len, parent_inode, &key, |
Nikolay Borisov | db0a669 | 2017-02-20 13:51:08 +0200 | [diff] [blame] | 6264 | btrfs_inode_type(&inode->vfs_inode), index); |
Chris Mason | 9c52057 | 2012-12-17 14:26:57 -0500 | [diff] [blame] | 6265 | if (ret == -EEXIST || ret == -EOVERFLOW) |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6266 | goto fail_dir_item; |
| 6267 | else if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 6268 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6269 | return ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6270 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6271 | |
Nikolay Borisov | db0a669 | 2017-02-20 13:51:08 +0200 | [diff] [blame] | 6272 | btrfs_i_size_write(parent_inode, parent_inode->vfs_inode.i_size + |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6273 | name_len * 2); |
Nikolay Borisov | db0a669 | 2017-02-20 13:51:08 +0200 | [diff] [blame] | 6274 | inode_inc_iversion(&parent_inode->vfs_inode); |
Filipe Manana | 5338e43 | 2019-05-15 16:02:47 +0100 | [diff] [blame] | 6275 | /* |
| 6276 | * If we are replaying a log tree, we do not want to update the mtime |
| 6277 | * and ctime of the parent directory with the current time, since the |
| 6278 | * log replay procedure is responsible for setting them to their correct |
| 6279 | * values (the ones it had when the fsync was done). |
| 6280 | */ |
| 6281 | if (!test_bit(BTRFS_FS_LOG_RECOVERING, &root->fs_info->flags)) { |
| 6282 | struct timespec64 now = current_time(&parent_inode->vfs_inode); |
| 6283 | |
| 6284 | parent_inode->vfs_inode.i_mtime = now; |
| 6285 | parent_inode->vfs_inode.i_ctime = now; |
| 6286 | } |
Nikolay Borisov | 9a56fcd | 2020-11-02 16:48:59 +0200 | [diff] [blame] | 6287 | ret = btrfs_update_inode(trans, root, parent_inode); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6288 | if (ret) |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 6289 | btrfs_abort_transaction(trans, ret); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6290 | return ret; |
Chris Mason | fe66a05 | 2012-02-20 08:40:56 -0500 | [diff] [blame] | 6291 | |
| 6292 | fail_dir_item: |
| 6293 | if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) { |
| 6294 | u64 local_index; |
| 6295 | int err; |
Lu Fengqi | 3ee1c55 | 2018-08-01 11:32:28 +0800 | [diff] [blame] | 6296 | err = btrfs_del_root_ref(trans, key.objectid, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6297 | root->root_key.objectid, parent_ino, |
| 6298 | &local_index, name, name_len); |
Johannes Thumshirn | 1690dd41 | 2018-12-12 15:14:17 +0100 | [diff] [blame] | 6299 | if (err) |
| 6300 | btrfs_abort_transaction(trans, err); |
Chris Mason | fe66a05 | 2012-02-20 08:40:56 -0500 | [diff] [blame] | 6301 | } else if (add_backref) { |
| 6302 | u64 local_index; |
| 6303 | int err; |
| 6304 | |
| 6305 | err = btrfs_del_inode_ref(trans, root, name, name_len, |
| 6306 | ino, parent_ino, &local_index); |
Johannes Thumshirn | 1690dd41 | 2018-12-12 15:14:17 +0100 | [diff] [blame] | 6307 | if (err) |
| 6308 | btrfs_abort_transaction(trans, err); |
Chris Mason | fe66a05 | 2012-02-20 08:40:56 -0500 | [diff] [blame] | 6309 | } |
Johannes Thumshirn | 1690dd41 | 2018-12-12 15:14:17 +0100 | [diff] [blame] | 6310 | |
| 6311 | /* Return the original error code */ |
Chris Mason | fe66a05 | 2012-02-20 08:40:56 -0500 | [diff] [blame] | 6312 | return ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6313 | } |
| 6314 | |
| 6315 | static int btrfs_add_nondir(struct btrfs_trans_handle *trans, |
Nikolay Borisov | cef415a | 2017-02-20 13:51:09 +0200 | [diff] [blame] | 6316 | struct btrfs_inode *dir, struct dentry *dentry, |
| 6317 | struct btrfs_inode *inode, int backref, u64 index) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6318 | { |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 6319 | int err = btrfs_add_link(trans, dir, inode, |
| 6320 | dentry->d_name.name, dentry->d_name.len, |
| 6321 | backref, index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6322 | if (err > 0) |
| 6323 | err = -EEXIST; |
| 6324 | return err; |
| 6325 | } |
| 6326 | |
Christian Brauner | 549c729 | 2021-01-21 14:19:43 +0100 | [diff] [blame] | 6327 | static int btrfs_mknod(struct user_namespace *mnt_userns, struct inode *dir, |
| 6328 | struct dentry *dentry, umode_t mode, dev_t rdev) |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6329 | { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6330 | struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6331 | struct btrfs_trans_handle *trans; |
| 6332 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 6333 | struct inode *inode = NULL; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6334 | int err; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6335 | u64 objectid; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 6336 | u64 index = 0; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6337 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 6338 | /* |
| 6339 | * 2 for inode item and ref |
| 6340 | * 2 for dir items |
| 6341 | * 1 for xattr if selinux is on |
| 6342 | */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6343 | trans = btrfs_start_transaction(root, 5); |
| 6344 | if (IS_ERR(trans)) |
| 6345 | return PTR_ERR(trans); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 6346 | |
Nikolay Borisov | 543068a | 2020-12-07 17:32:33 +0200 | [diff] [blame] | 6347 | err = btrfs_get_free_objectid(root, &objectid); |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 6348 | if (err) |
| 6349 | goto out_unlock; |
| 6350 | |
Christian Brauner | 7210527 | 2021-07-27 12:48:44 +0200 | [diff] [blame] | 6351 | inode = btrfs_new_inode(trans, root, mnt_userns, dir, |
Christian Brauner | b3b6f5b | 2021-07-27 12:48:41 +0200 | [diff] [blame] | 6352 | dentry->d_name.name, dentry->d_name.len, |
| 6353 | btrfs_ino(BTRFS_I(dir)), objectid, mode, &index); |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 6354 | if (IS_ERR(inode)) { |
| 6355 | err = PTR_ERR(inode); |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6356 | inode = NULL; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6357 | goto out_unlock; |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 6358 | } |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6359 | |
Casey Schaufler | ad19db7 | 2011-12-15 10:09:07 -0500 | [diff] [blame] | 6360 | /* |
| 6361 | * If the active LSM wants to access the inode during |
| 6362 | * d_instantiate it needs these. Smack checks to see |
| 6363 | * if the filesystem supports xattrs by looking at the |
| 6364 | * ops vector. |
| 6365 | */ |
Casey Schaufler | ad19db7 | 2011-12-15 10:09:07 -0500 | [diff] [blame] | 6366 | inode->i_op = &btrfs_special_inode_operations; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6367 | init_special_inode(inode, inode->i_mode, rdev); |
| 6368 | |
| 6369 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6370 | if (err) |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6371 | goto out_unlock; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6372 | |
Nikolay Borisov | cef415a | 2017-02-20 13:51:09 +0200 | [diff] [blame] | 6373 | err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode), |
| 6374 | 0, index); |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6375 | if (err) |
| 6376 | goto out_unlock; |
| 6377 | |
Nikolay Borisov | 9a56fcd | 2020-11-02 16:48:59 +0200 | [diff] [blame] | 6378 | btrfs_update_inode(trans, root, BTRFS_I(inode)); |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6379 | d_instantiate_new(dentry, inode); |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6380 | |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6381 | out_unlock: |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 6382 | btrfs_end_transaction(trans); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6383 | btrfs_btree_balance_dirty(fs_info); |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6384 | if (err && inode) { |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6385 | inode_dec_link_count(inode); |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6386 | discard_new_inode(inode); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6387 | } |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6388 | return err; |
| 6389 | } |
| 6390 | |
Christian Brauner | 549c729 | 2021-01-21 14:19:43 +0100 | [diff] [blame] | 6391 | static int btrfs_create(struct user_namespace *mnt_userns, struct inode *dir, |
| 6392 | struct dentry *dentry, umode_t mode, bool excl) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6393 | { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6394 | struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6395 | struct btrfs_trans_handle *trans; |
| 6396 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 6397 | struct inode *inode = NULL; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6398 | int err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6399 | u64 objectid; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 6400 | u64 index = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6401 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 6402 | /* |
| 6403 | * 2 for inode item and ref |
| 6404 | * 2 for dir items |
| 6405 | * 1 for xattr if selinux is on |
| 6406 | */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6407 | trans = btrfs_start_transaction(root, 5); |
| 6408 | if (IS_ERR(trans)) |
| 6409 | return PTR_ERR(trans); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 6410 | |
Nikolay Borisov | 543068a | 2020-12-07 17:32:33 +0200 | [diff] [blame] | 6411 | err = btrfs_get_free_objectid(root, &objectid); |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 6412 | if (err) |
| 6413 | goto out_unlock; |
| 6414 | |
Christian Brauner | e93ca49 | 2021-07-27 12:48:45 +0200 | [diff] [blame] | 6415 | inode = btrfs_new_inode(trans, root, mnt_userns, dir, |
Christian Brauner | b3b6f5b | 2021-07-27 12:48:41 +0200 | [diff] [blame] | 6416 | dentry->d_name.name, dentry->d_name.len, |
| 6417 | btrfs_ino(BTRFS_I(dir)), objectid, mode, &index); |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 6418 | if (IS_ERR(inode)) { |
| 6419 | err = PTR_ERR(inode); |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6420 | inode = NULL; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6421 | goto out_unlock; |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 6422 | } |
Casey Schaufler | ad19db7 | 2011-12-15 10:09:07 -0500 | [diff] [blame] | 6423 | /* |
| 6424 | * If the active LSM wants to access the inode during |
| 6425 | * d_instantiate it needs these. Smack checks to see |
| 6426 | * if the filesystem supports xattrs by looking at the |
| 6427 | * ops vector. |
| 6428 | */ |
| 6429 | inode->i_fop = &btrfs_file_operations; |
| 6430 | inode->i_op = &btrfs_file_inode_operations; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6431 | inode->i_mapping->a_ops = &btrfs_aops; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6432 | |
| 6433 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); |
| 6434 | if (err) |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6435 | goto out_unlock; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6436 | |
Nikolay Borisov | 9a56fcd | 2020-11-02 16:48:59 +0200 | [diff] [blame] | 6437 | err = btrfs_update_inode(trans, root, BTRFS_I(inode)); |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6438 | if (err) |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6439 | goto out_unlock; |
Casey Schaufler | ad19db7 | 2011-12-15 10:09:07 -0500 | [diff] [blame] | 6440 | |
Nikolay Borisov | cef415a | 2017-02-20 13:51:09 +0200 | [diff] [blame] | 6441 | err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode), |
| 6442 | 0, index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6443 | if (err) |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6444 | goto out_unlock; |
Filipe Brandenburger | 43baa57 | 2012-11-30 03:40:09 +0000 | [diff] [blame] | 6445 | |
Al Viro | 1e2e547 | 2018-05-04 08:23:01 -0400 | [diff] [blame] | 6446 | d_instantiate_new(dentry, inode); |
Filipe Brandenburger | 43baa57 | 2012-11-30 03:40:09 +0000 | [diff] [blame] | 6447 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6448 | out_unlock: |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 6449 | btrfs_end_transaction(trans); |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6450 | if (err && inode) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6451 | inode_dec_link_count(inode); |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6452 | discard_new_inode(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6453 | } |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6454 | btrfs_btree_balance_dirty(fs_info); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6455 | return err; |
| 6456 | } |
| 6457 | |
| 6458 | static int btrfs_link(struct dentry *old_dentry, struct inode *dir, |
| 6459 | struct dentry *dentry) |
| 6460 | { |
Filipe Manana | 271dba45 | 2016-01-05 16:24:05 +0000 | [diff] [blame] | 6461 | struct btrfs_trans_handle *trans = NULL; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6462 | struct btrfs_root *root = BTRFS_I(dir)->root; |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 6463 | struct inode *inode = d_inode(old_dentry); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6464 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 6465 | u64 index; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6466 | int err; |
| 6467 | int drop_inode = 0; |
| 6468 | |
TARUISI Hiroaki | 4a8be42 | 2009-11-12 07:14:26 +0000 | [diff] [blame] | 6469 | /* 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] | 6470 | if (root->root_key.objectid != BTRFS_I(inode)->root->root_key.objectid) |
Mark Fasheh | 3ab3564 | 2011-03-22 17:20:26 +0000 | [diff] [blame] | 6471 | return -EXDEV; |
TARUISI Hiroaki | 4a8be42 | 2009-11-12 07:14:26 +0000 | [diff] [blame] | 6472 | |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 6473 | if (inode->i_nlink >= BTRFS_LINK_MAX) |
Al Viro | c055e99 | 2011-03-04 17:15:18 +0000 | [diff] [blame] | 6474 | return -EMLINK; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 6475 | |
Nikolay Borisov | 877574e | 2017-02-20 13:50:33 +0200 | [diff] [blame] | 6476 | err = btrfs_set_inode_index(BTRFS_I(dir), &index); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6477 | if (err) |
| 6478 | goto fail; |
| 6479 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6480 | /* |
Miao Xie | 7e6b646 | 2011-02-18 09:21:17 +0000 | [diff] [blame] | 6481 | * 2 items for inode and inode ref |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6482 | * 2 items for dir items |
Miao Xie | 7e6b646 | 2011-02-18 09:21:17 +0000 | [diff] [blame] | 6483 | * 1 item for parent inode |
Omar Sandoval | 399b0bb | 2018-05-11 13:13:40 -0700 | [diff] [blame] | 6484 | * 1 item for orphan item deletion if O_TMPFILE |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6485 | */ |
Omar Sandoval | 399b0bb | 2018-05-11 13:13:40 -0700 | [diff] [blame] | 6486 | trans = btrfs_start_transaction(root, inode->i_nlink ? 5 : 6); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6487 | if (IS_ERR(trans)) { |
| 6488 | err = PTR_ERR(trans); |
Filipe Manana | 271dba45 | 2016-01-05 16:24:05 +0000 | [diff] [blame] | 6489 | trans = NULL; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6490 | goto fail; |
| 6491 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6492 | |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 6493 | /* There are several dir indexes for this inode, clear the cache. */ |
| 6494 | BTRFS_I(inode)->dir_index = 0ULL; |
Zach Brown | 8b558c5 | 2013-10-16 12:10:34 -0700 | [diff] [blame] | 6495 | inc_nlink(inode); |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 6496 | inode_inc_iversion(inode); |
Deepa Dinamani | c2050a4 | 2016-09-14 07:48:06 -0700 | [diff] [blame] | 6497 | inode->i_ctime = current_time(inode); |
Al Viro | 7de9c6ee | 2010-10-23 11:11:40 -0400 | [diff] [blame] | 6498 | ihold(inode); |
Josef Bacik | e997615 | 2012-10-11 15:53:56 -0400 | [diff] [blame] | 6499 | set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6500 | |
Nikolay Borisov | cef415a | 2017-02-20 13:51:09 +0200 | [diff] [blame] | 6501 | err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode), |
| 6502 | 1, index); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6503 | |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 6504 | if (err) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6505 | drop_inode = 1; |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 6506 | } else { |
Al Viro | 10d9f30 | 2011-07-16 23:09:10 -0400 | [diff] [blame] | 6507 | struct dentry *parent = dentry->d_parent; |
Filipe Manana | d4682ba | 2018-06-11 19:24:28 +0100 | [diff] [blame] | 6508 | |
Nikolay Borisov | 9a56fcd | 2020-11-02 16:48:59 +0200 | [diff] [blame] | 6509 | err = btrfs_update_inode(trans, root, BTRFS_I(inode)); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6510 | if (err) |
| 6511 | goto fail; |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 6512 | if (inode->i_nlink == 1) { |
| 6513 | /* |
| 6514 | * If new hard link count is 1, it's a file created |
| 6515 | * with open(2) O_TMPFILE flag. |
| 6516 | */ |
Nikolay Borisov | 3d6ae7b | 2017-02-20 13:50:58 +0200 | [diff] [blame] | 6517 | err = btrfs_orphan_del(trans, BTRFS_I(inode)); |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 6518 | if (err) |
| 6519 | goto fail; |
| 6520 | } |
Al Viro | 08c422c | 2011-12-23 07:58:13 -0500 | [diff] [blame] | 6521 | d_instantiate(dentry, inode); |
Filipe Manana | 75b463d | 2020-08-11 12:43:48 +0100 | [diff] [blame] | 6522 | btrfs_log_new_name(trans, BTRFS_I(inode), NULL, parent); |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 6523 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6524 | |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 6525 | fail: |
Filipe Manana | 271dba45 | 2016-01-05 16:24:05 +0000 | [diff] [blame] | 6526 | if (trans) |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 6527 | btrfs_end_transaction(trans); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6528 | if (drop_inode) { |
| 6529 | inode_dec_link_count(inode); |
| 6530 | iput(inode); |
| 6531 | } |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6532 | btrfs_btree_balance_dirty(fs_info); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6533 | return err; |
| 6534 | } |
| 6535 | |
Christian Brauner | 549c729 | 2021-01-21 14:19:43 +0100 | [diff] [blame] | 6536 | static int btrfs_mkdir(struct user_namespace *mnt_userns, struct inode *dir, |
| 6537 | struct dentry *dentry, umode_t mode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6538 | { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6539 | struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); |
Chris Mason | b9d8666 | 2008-05-02 16:13:49 -0400 | [diff] [blame] | 6540 | struct inode *inode = NULL; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6541 | struct btrfs_trans_handle *trans; |
| 6542 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 6543 | int err = 0; |
Chris Mason | b9d8666 | 2008-05-02 16:13:49 -0400 | [diff] [blame] | 6544 | u64 objectid = 0; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 6545 | u64 index = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6546 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 6547 | /* |
| 6548 | * 2 items for inode and ref |
| 6549 | * 2 items for dir items |
| 6550 | * 1 for xattr if selinux is on |
| 6551 | */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6552 | trans = btrfs_start_transaction(root, 5); |
| 6553 | if (IS_ERR(trans)) |
| 6554 | return PTR_ERR(trans); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6555 | |
Nikolay Borisov | 543068a | 2020-12-07 17:32:33 +0200 | [diff] [blame] | 6556 | err = btrfs_get_free_objectid(root, &objectid); |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 6557 | if (err) |
| 6558 | goto out_fail; |
| 6559 | |
Christian Brauner | b0b3e44 | 2021-07-27 12:48:46 +0200 | [diff] [blame] | 6560 | inode = btrfs_new_inode(trans, root, mnt_userns, dir, |
Christian Brauner | b3b6f5b | 2021-07-27 12:48:41 +0200 | [diff] [blame] | 6561 | dentry->d_name.name, dentry->d_name.len, |
| 6562 | btrfs_ino(BTRFS_I(dir)), objectid, |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 6563 | S_IFDIR | mode, &index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6564 | if (IS_ERR(inode)) { |
| 6565 | err = PTR_ERR(inode); |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6566 | inode = NULL; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6567 | goto out_fail; |
| 6568 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6569 | |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6570 | /* these must be set before we unlock the inode */ |
| 6571 | inode->i_op = &btrfs_dir_inode_operations; |
| 6572 | inode->i_fop = &btrfs_dir_file_operations; |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 6573 | |
Eric Paris | 2a7dba3 | 2011-02-01 11:05:39 -0500 | [diff] [blame] | 6574 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 6575 | if (err) |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6576 | goto out_fail; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6577 | |
Nikolay Borisov | 6ef06d2 | 2017-02-20 13:50:34 +0200 | [diff] [blame] | 6578 | btrfs_i_size_write(BTRFS_I(inode), 0); |
Nikolay Borisov | 9a56fcd | 2020-11-02 16:48:59 +0200 | [diff] [blame] | 6579 | err = btrfs_update_inode(trans, root, BTRFS_I(inode)); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6580 | if (err) |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6581 | goto out_fail; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6582 | |
Nikolay Borisov | db0a669 | 2017-02-20 13:51:08 +0200 | [diff] [blame] | 6583 | err = btrfs_add_link(trans, BTRFS_I(dir), BTRFS_I(inode), |
| 6584 | dentry->d_name.name, |
| 6585 | dentry->d_name.len, 0, index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6586 | if (err) |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6587 | goto out_fail; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6588 | |
Al Viro | 1e2e547 | 2018-05-04 08:23:01 -0400 | [diff] [blame] | 6589 | d_instantiate_new(dentry, inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6590 | |
| 6591 | out_fail: |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 6592 | btrfs_end_transaction(trans); |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6593 | if (err && inode) { |
Wang Shilong | c7cfb8a | 2014-12-24 14:45:30 +0800 | [diff] [blame] | 6594 | inode_dec_link_count(inode); |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6595 | discard_new_inode(inode); |
Wang Shilong | c7cfb8a | 2014-12-24 14:45:30 +0800 | [diff] [blame] | 6596 | } |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6597 | btrfs_btree_balance_dirty(fs_info); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6598 | return err; |
| 6599 | } |
| 6600 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6601 | static noinline int uncompress_inline(struct btrfs_path *path, |
Byongho Lee | e40da0e | 2015-05-19 23:46:45 +0900 | [diff] [blame] | 6602 | struct page *page, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6603 | size_t pg_offset, u64 extent_offset, |
| 6604 | struct btrfs_file_extent_item *item) |
| 6605 | { |
| 6606 | int ret; |
| 6607 | struct extent_buffer *leaf = path->nodes[0]; |
| 6608 | char *tmp; |
| 6609 | size_t max_size; |
| 6610 | unsigned long inline_size; |
| 6611 | unsigned long ptr; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 6612 | int compress_type; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6613 | |
| 6614 | WARN_ON(pg_offset != 0); |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 6615 | compress_type = btrfs_file_extent_compression(leaf, item); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6616 | max_size = btrfs_file_extent_ram_bytes(leaf, item); |
Josef Bacik | 437bd07 | 2021-10-21 14:58:33 -0400 | [diff] [blame] | 6617 | inline_size = btrfs_file_extent_inline_item_len(leaf, path->slots[0]); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6618 | tmp = kmalloc(inline_size, GFP_NOFS); |
Tsutomu Itoh | 8d41371 | 2011-04-25 19:43:52 -0400 | [diff] [blame] | 6619 | if (!tmp) |
| 6620 | return -ENOMEM; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6621 | ptr = btrfs_file_extent_inline_start(item); |
| 6622 | |
| 6623 | read_extent_buffer(leaf, tmp, ptr, inline_size); |
| 6624 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 6625 | max_size = min_t(unsigned long, PAGE_SIZE, max_size); |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 6626 | ret = btrfs_decompress(compress_type, tmp, page, |
| 6627 | extent_offset, inline_size, max_size); |
Zygo Blaxell | e1699d2 | 2017-03-10 16:45:44 -0500 | [diff] [blame] | 6628 | |
| 6629 | /* |
| 6630 | * decompression code contains a memset to fill in any space between the end |
| 6631 | * of the uncompressed data and the end of max_size in case the decompressed |
| 6632 | * data ends up shorter than ram_bytes. That doesn't cover the hole between |
| 6633 | * the end of an inline extent and the beginning of the next block, so we |
| 6634 | * cover that region here. |
| 6635 | */ |
| 6636 | |
Ira Weiny | d048b9c | 2021-05-04 18:40:07 -0700 | [diff] [blame] | 6637 | if (max_size + pg_offset < PAGE_SIZE) |
| 6638 | memzero_page(page, pg_offset + max_size, |
| 6639 | PAGE_SIZE - max_size - pg_offset); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6640 | kfree(tmp); |
Zach Brown | 166ae5a | 2014-05-09 17:15:10 -0400 | [diff] [blame] | 6641 | return ret; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6642 | } |
| 6643 | |
Omar Sandoval | 39b07b5 | 2019-12-02 17:34:23 -0800 | [diff] [blame] | 6644 | /** |
| 6645 | * btrfs_get_extent - Lookup the first extent overlapping a range in a file. |
| 6646 | * @inode: file to search in |
| 6647 | * @page: page to read extent data into if the extent is inline |
| 6648 | * @pg_offset: offset into @page to copy to |
| 6649 | * @start: file offset |
| 6650 | * @len: length of range starting at @start |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 6651 | * |
Omar Sandoval | 39b07b5 | 2019-12-02 17:34:23 -0800 | [diff] [blame] | 6652 | * This returns the first &struct extent_map which overlaps with the given |
| 6653 | * range, reading it from the B-tree and caching it if necessary. Note that |
| 6654 | * there may be more extents which overlap the given range after the returned |
| 6655 | * extent_map. |
| 6656 | * |
| 6657 | * If @page is not NULL and the extent is inline, this also reads the extent |
| 6658 | * data directly into the page and marks the extent up to date in the io_tree. |
| 6659 | * |
| 6660 | * Return: ERR_PTR on error, non-NULL extent_map on success. |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 6661 | */ |
Nikolay Borisov | fc4f21b1 | 2017-02-20 13:51:06 +0200 | [diff] [blame] | 6662 | struct extent_map *btrfs_get_extent(struct btrfs_inode *inode, |
Omar Sandoval | 39b07b5 | 2019-12-02 17:34:23 -0800 | [diff] [blame] | 6663 | struct page *page, size_t pg_offset, |
| 6664 | u64 start, u64 len) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6665 | { |
David Sterba | 3ffbd68 | 2018-06-29 10:56:42 +0200 | [diff] [blame] | 6666 | struct btrfs_fs_info *fs_info = inode->root->fs_info; |
Nikolay Borisov | 1028d1c | 2020-08-03 12:58:46 +0300 | [diff] [blame] | 6667 | int ret = 0; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6668 | u64 extent_start = 0; |
| 6669 | u64 extent_end = 0; |
Nikolay Borisov | fc4f21b1 | 2017-02-20 13:51:06 +0200 | [diff] [blame] | 6670 | u64 objectid = btrfs_ino(inode); |
Linus Torvalds | 7e74e23 | 2019-05-01 12:19:20 -0700 | [diff] [blame] | 6671 | int extent_type = -1; |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 6672 | struct btrfs_path *path = NULL; |
Nikolay Borisov | fc4f21b1 | 2017-02-20 13:51:06 +0200 | [diff] [blame] | 6673 | struct btrfs_root *root = inode->root; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6674 | struct btrfs_file_extent_item *item; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6675 | struct extent_buffer *leaf; |
| 6676 | struct btrfs_key found_key; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6677 | struct extent_map *em = NULL; |
Nikolay Borisov | fc4f21b1 | 2017-02-20 13:51:06 +0200 | [diff] [blame] | 6678 | struct extent_map_tree *em_tree = &inode->extent_tree; |
| 6679 | struct extent_io_tree *io_tree = &inode->io_tree; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6680 | |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 6681 | read_lock(&em_tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6682 | em = lookup_extent_mapping(em_tree, start, len); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 6683 | read_unlock(&em_tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6684 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6685 | if (em) { |
Chris Mason | e1c4b74 | 2008-04-22 13:26:46 -0400 | [diff] [blame] | 6686 | if (em->start > start || em->start + em->len <= start) |
| 6687 | free_extent_map(em); |
| 6688 | else if (em->block_start == EXTENT_MAP_INLINE && page) |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 6689 | free_extent_map(em); |
| 6690 | else |
| 6691 | goto out; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6692 | } |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 6693 | em = alloc_extent_map(); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6694 | if (!em) { |
Nikolay Borisov | 1028d1c | 2020-08-03 12:58:46 +0300 | [diff] [blame] | 6695 | ret = -ENOMEM; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6696 | goto out; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6697 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6698 | em->start = EXTENT_MAP_HOLE; |
Chris Mason | 445a694 | 2008-11-10 11:53:33 -0500 | [diff] [blame] | 6699 | em->orig_start = EXTENT_MAP_HOLE; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6700 | em->len = (u64)-1; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6701 | em->block_len = (u64)-1; |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 6702 | |
Liu Bo | bee6ec8 | 2018-08-17 05:05:28 +0800 | [diff] [blame] | 6703 | path = btrfs_alloc_path(); |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 6704 | if (!path) { |
Nikolay Borisov | 1028d1c | 2020-08-03 12:58:46 +0300 | [diff] [blame] | 6705 | ret = -ENOMEM; |
Liu Bo | bee6ec8 | 2018-08-17 05:05:28 +0800 | [diff] [blame] | 6706 | goto out; |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 6707 | } |
| 6708 | |
Liu Bo | bee6ec8 | 2018-08-17 05:05:28 +0800 | [diff] [blame] | 6709 | /* Chances are we'll be called again, so go ahead and do readahead */ |
| 6710 | path->reada = READA_FORWARD; |
Josef Bacik | 4d7240f | 2020-10-23 09:58:09 -0400 | [diff] [blame] | 6711 | |
| 6712 | /* |
| 6713 | * The same explanation in load_free_space_cache applies here as well, |
| 6714 | * we only read when we're loading the free space cache, and at that |
| 6715 | * point the commit_root has everything we need. |
| 6716 | */ |
| 6717 | if (btrfs_is_free_space_inode(inode)) { |
| 6718 | path->search_commit_root = 1; |
| 6719 | path->skip_locking = 1; |
| 6720 | } |
Josef Bacik | 5189941 | 2020-08-20 11:46:01 -0400 | [diff] [blame] | 6721 | |
Nikolay Borisov | 5c9a702 | 2017-12-01 11:19:40 +0200 | [diff] [blame] | 6722 | ret = btrfs_lookup_file_extent(NULL, root, path, objectid, start, 0); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6723 | if (ret < 0) { |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6724 | goto out; |
Nikolay Borisov | b8eeab7 | 2018-12-17 11:49:00 +0200 | [diff] [blame] | 6725 | } else if (ret > 0) { |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6726 | if (path->slots[0] == 0) |
| 6727 | goto not_found; |
| 6728 | path->slots[0]--; |
Nikolay Borisov | 1028d1c | 2020-08-03 12:58:46 +0300 | [diff] [blame] | 6729 | ret = 0; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6730 | } |
| 6731 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6732 | leaf = path->nodes[0]; |
| 6733 | item = btrfs_item_ptr(leaf, path->slots[0], |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6734 | struct btrfs_file_extent_item); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6735 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6736 | if (found_key.objectid != objectid || |
Nikolay Borisov | 694c12e | 2018-12-17 10:35:59 +0200 | [diff] [blame] | 6737 | found_key.type != BTRFS_EXTENT_DATA_KEY) { |
Josef Bacik | 25a5034 | 2013-10-14 12:08:38 -0400 | [diff] [blame] | 6738 | /* |
| 6739 | * If we backup past the first extent we want to move forward |
| 6740 | * and see if there is an extent in front of us, otherwise we'll |
| 6741 | * say there is a hole for our whole search range which can |
| 6742 | * cause problems. |
| 6743 | */ |
| 6744 | extent_end = start; |
| 6745 | goto next; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6746 | } |
| 6747 | |
Nikolay Borisov | 694c12e | 2018-12-17 10:35:59 +0200 | [diff] [blame] | 6748 | extent_type = btrfs_file_extent_type(leaf, item); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6749 | extent_start = found_key.offset; |
Filipe Manana | a5eeb3d | 2020-03-09 12:41:06 +0000 | [diff] [blame] | 6750 | extent_end = btrfs_file_extent_end(path); |
Nikolay Borisov | 694c12e | 2018-12-17 10:35:59 +0200 | [diff] [blame] | 6751 | if (extent_type == BTRFS_FILE_EXTENT_REG || |
| 6752 | extent_type == BTRFS_FILE_EXTENT_PREALLOC) { |
Qu Wenruo | 6bf9e4b | 2019-03-13 13:55:11 +0800 | [diff] [blame] | 6753 | /* Only regular file could have regular/prealloc extent */ |
| 6754 | if (!S_ISREG(inode->vfs_inode.i_mode)) { |
Nikolay Borisov | 1028d1c | 2020-08-03 12:58:46 +0300 | [diff] [blame] | 6755 | ret = -EUCLEAN; |
Qu Wenruo | 6bf9e4b | 2019-03-13 13:55:11 +0800 | [diff] [blame] | 6756 | btrfs_crit(fs_info, |
| 6757 | "regular/prealloc extent found for non-regular inode %llu", |
| 6758 | btrfs_ino(inode)); |
| 6759 | goto out; |
| 6760 | } |
Liu Bo | 09ed2f1 | 2017-03-10 11:09:48 -0800 | [diff] [blame] | 6761 | trace_btrfs_get_extent_show_fi_regular(inode, leaf, item, |
| 6762 | extent_start); |
Nikolay Borisov | 694c12e | 2018-12-17 10:35:59 +0200 | [diff] [blame] | 6763 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
Liu Bo | 09ed2f1 | 2017-03-10 11:09:48 -0800 | [diff] [blame] | 6764 | trace_btrfs_get_extent_show_fi_inline(inode, leaf, item, |
| 6765 | path->slots[0], |
| 6766 | extent_start); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6767 | } |
Josef Bacik | 25a5034 | 2013-10-14 12:08:38 -0400 | [diff] [blame] | 6768 | next: |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6769 | if (start >= extent_end) { |
| 6770 | path->slots[0]++; |
| 6771 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { |
| 6772 | ret = btrfs_next_leaf(root, path); |
Nikolay Borisov | 1028d1c | 2020-08-03 12:58:46 +0300 | [diff] [blame] | 6773 | if (ret < 0) |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6774 | goto out; |
Nikolay Borisov | 1028d1c | 2020-08-03 12:58:46 +0300 | [diff] [blame] | 6775 | else if (ret > 0) |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6776 | goto not_found; |
Nikolay Borisov | 1028d1c | 2020-08-03 12:58:46 +0300 | [diff] [blame] | 6777 | |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6778 | leaf = path->nodes[0]; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6779 | } |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6780 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 6781 | if (found_key.objectid != objectid || |
| 6782 | found_key.type != BTRFS_EXTENT_DATA_KEY) |
| 6783 | goto not_found; |
| 6784 | if (start + len <= found_key.offset) |
| 6785 | goto not_found; |
Wang Shilong | e2eca69 | 2014-07-17 11:44:14 +0800 | [diff] [blame] | 6786 | if (start > found_key.offset) |
| 6787 | goto next; |
Nikolay Borisov | 02a033d | 2018-12-17 10:36:02 +0200 | [diff] [blame] | 6788 | |
| 6789 | /* New extent overlaps with existing one */ |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6790 | em->start = start; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 6791 | em->orig_start = start; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6792 | em->len = found_key.offset - start; |
Nikolay Borisov | 02a033d | 2018-12-17 10:36:02 +0200 | [diff] [blame] | 6793 | em->block_start = EXTENT_MAP_HOLE; |
| 6794 | goto insert; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6795 | } |
| 6796 | |
Omar Sandoval | 39b07b5 | 2019-12-02 17:34:23 -0800 | [diff] [blame] | 6797 | btrfs_extent_item_to_extent_map(inode, path, item, !page, em); |
Filipe Manana | 7ffbb59 | 2014-06-09 03:48:05 +0100 | [diff] [blame] | 6798 | |
Nikolay Borisov | 694c12e | 2018-12-17 10:35:59 +0200 | [diff] [blame] | 6799 | if (extent_type == BTRFS_FILE_EXTENT_REG || |
| 6800 | extent_type == BTRFS_FILE_EXTENT_PREALLOC) { |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6801 | goto insert; |
Nikolay Borisov | 694c12e | 2018-12-17 10:35:59 +0200 | [diff] [blame] | 6802 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6803 | unsigned long ptr; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6804 | char *map; |
Chris Mason | 3326d1b | 2007-10-15 16:18:25 -0400 | [diff] [blame] | 6805 | size_t size; |
| 6806 | size_t extent_offset; |
| 6807 | size_t copy_size; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6808 | |
Omar Sandoval | 39b07b5 | 2019-12-02 17:34:23 -0800 | [diff] [blame] | 6809 | if (!page) |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 6810 | goto out; |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 6811 | |
Qu Wenruo | e41ca58 | 2018-06-06 15:41:49 +0800 | [diff] [blame] | 6812 | size = btrfs_file_extent_ram_bytes(leaf, item); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6813 | extent_offset = page_offset(page) + pg_offset - extent_start; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 6814 | copy_size = min_t(u64, PAGE_SIZE - pg_offset, |
| 6815 | size - extent_offset); |
Chris Mason | 3326d1b | 2007-10-15 16:18:25 -0400 | [diff] [blame] | 6816 | em->start = extent_start + extent_offset; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6817 | em->len = ALIGN(copy_size, fs_info->sectorsize); |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 6818 | em->orig_block_len = em->len; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 6819 | em->orig_start = em->start; |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 6820 | ptr = btrfs_file_extent_inline_start(item) + extent_offset; |
Liu Bo | e49aabd | 2018-08-25 13:47:09 +0800 | [diff] [blame] | 6821 | |
Edmund Nadolski | bf46f52 | 2017-11-20 13:24:49 -0700 | [diff] [blame] | 6822 | if (!PageUptodate(page)) { |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 6823 | if (btrfs_file_extent_compression(leaf, item) != |
| 6824 | BTRFS_COMPRESS_NONE) { |
Byongho Lee | e40da0e | 2015-05-19 23:46:45 +0900 | [diff] [blame] | 6825 | ret = uncompress_inline(path, page, pg_offset, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6826 | extent_offset, item); |
Nikolay Borisov | 1028d1c | 2020-08-03 12:58:46 +0300 | [diff] [blame] | 6827 | if (ret) |
Zach Brown | 166ae5a | 2014-05-09 17:15:10 -0400 | [diff] [blame] | 6828 | goto out; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6829 | } else { |
Ira Weiny | 58c1a35 | 2021-02-16 18:48:23 -0800 | [diff] [blame] | 6830 | map = kmap_local_page(page); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6831 | read_extent_buffer(leaf, map + pg_offset, ptr, |
| 6832 | copy_size); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 6833 | if (pg_offset + copy_size < PAGE_SIZE) { |
Chris Mason | 93c82d5 | 2009-09-11 12:36:29 -0400 | [diff] [blame] | 6834 | memset(map + pg_offset + copy_size, 0, |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 6835 | PAGE_SIZE - pg_offset - |
Chris Mason | 93c82d5 | 2009-09-11 12:36:29 -0400 | [diff] [blame] | 6836 | copy_size); |
| 6837 | } |
Ira Weiny | 58c1a35 | 2021-02-16 18:48:23 -0800 | [diff] [blame] | 6838 | kunmap_local(map); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6839 | } |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 6840 | flush_dcache_page(page); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6841 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6842 | set_extent_uptodate(io_tree, em->start, |
Arne Jansen | 507903b | 2011-04-06 10:02:20 +0000 | [diff] [blame] | 6843 | extent_map_end(em) - 1, NULL, GFP_NOFS); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6844 | goto insert; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6845 | } |
| 6846 | not_found: |
| 6847 | em->start = start; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 6848 | em->orig_start = start; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6849 | em->len = len; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6850 | em->block_start = EXTENT_MAP_HOLE; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6851 | insert: |
Nikolay Borisov | 1028d1c | 2020-08-03 12:58:46 +0300 | [diff] [blame] | 6852 | ret = 0; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 6853 | btrfs_release_path(path); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6854 | if (em->start > start || extent_map_end(em) <= start) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6855 | btrfs_err(fs_info, |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 6856 | "bad extent! em: [%llu %llu] passed [%llu %llu]", |
| 6857 | em->start, em->len, start, len); |
Nikolay Borisov | 1028d1c | 2020-08-03 12:58:46 +0300 | [diff] [blame] | 6858 | ret = -EIO; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6859 | goto out; |
| 6860 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6861 | |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 6862 | write_lock(&em_tree->lock); |
Nikolay Borisov | 1028d1c | 2020-08-03 12:58:46 +0300 | [diff] [blame] | 6863 | ret = btrfs_add_extent_mapping(fs_info, em_tree, &em, start, len); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 6864 | write_unlock(&em_tree->lock); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6865 | out: |
Liu Bo | c641428 | 2018-08-23 07:36:17 +0800 | [diff] [blame] | 6866 | btrfs_free_path(path); |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 6867 | |
Nikolay Borisov | fc4f21b1 | 2017-02-20 13:51:06 +0200 | [diff] [blame] | 6868 | trace_btrfs_get_extent(root, inode, em); |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 6869 | |
Nikolay Borisov | 1028d1c | 2020-08-03 12:58:46 +0300 | [diff] [blame] | 6870 | if (ret) { |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6871 | free_extent_map(em); |
Nikolay Borisov | 1028d1c | 2020-08-03 12:58:46 +0300 | [diff] [blame] | 6872 | return ERR_PTR(ret); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6873 | } |
| 6874 | return em; |
| 6875 | } |
| 6876 | |
Nikolay Borisov | fc4f21b1 | 2017-02-20 13:51:06 +0200 | [diff] [blame] | 6877 | struct extent_map *btrfs_get_extent_fiemap(struct btrfs_inode *inode, |
Nikolay Borisov | 4ab47a8 | 2018-12-12 09:42:32 +0200 | [diff] [blame] | 6878 | u64 start, u64 len) |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6879 | { |
| 6880 | struct extent_map *em; |
| 6881 | struct extent_map *hole_em = NULL; |
Nikolay Borisov | f3714ef | 2018-12-12 09:42:33 +0200 | [diff] [blame] | 6882 | u64 delalloc_start = start; |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6883 | u64 end; |
Nikolay Borisov | f3714ef | 2018-12-12 09:42:33 +0200 | [diff] [blame] | 6884 | u64 delalloc_len; |
| 6885 | u64 delalloc_end; |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6886 | int err = 0; |
| 6887 | |
Omar Sandoval | 39b07b5 | 2019-12-02 17:34:23 -0800 | [diff] [blame] | 6888 | em = btrfs_get_extent(inode, NULL, 0, start, len); |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6889 | if (IS_ERR(em)) |
| 6890 | return em; |
Dan Carpenter | 9986277 | 2017-04-11 11:57:15 +0300 | [diff] [blame] | 6891 | /* |
| 6892 | * If our em maps to: |
| 6893 | * - a hole or |
| 6894 | * - a pre-alloc extent, |
| 6895 | * there might actually be delalloc bytes behind it. |
| 6896 | */ |
| 6897 | if (em->block_start != EXTENT_MAP_HOLE && |
| 6898 | !test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) |
| 6899 | return em; |
| 6900 | else |
| 6901 | hole_em = em; |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6902 | |
| 6903 | /* check to see if we've wrapped (len == -1 or similar) */ |
| 6904 | end = start + len; |
| 6905 | if (end < start) |
| 6906 | end = (u64)-1; |
| 6907 | else |
| 6908 | end -= 1; |
| 6909 | |
| 6910 | em = NULL; |
| 6911 | |
| 6912 | /* ok, we didn't find anything, lets look for delalloc */ |
Nikolay Borisov | f3714ef | 2018-12-12 09:42:33 +0200 | [diff] [blame] | 6913 | delalloc_len = count_range_bits(&inode->io_tree, &delalloc_start, |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6914 | end, len, EXTENT_DELALLOC, 1); |
Nikolay Borisov | f3714ef | 2018-12-12 09:42:33 +0200 | [diff] [blame] | 6915 | delalloc_end = delalloc_start + delalloc_len; |
| 6916 | if (delalloc_end < delalloc_start) |
| 6917 | delalloc_end = (u64)-1; |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6918 | |
| 6919 | /* |
Nikolay Borisov | f3714ef | 2018-12-12 09:42:33 +0200 | [diff] [blame] | 6920 | * We didn't find anything useful, return the original results from |
| 6921 | * get_extent() |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6922 | */ |
Nikolay Borisov | f3714ef | 2018-12-12 09:42:33 +0200 | [diff] [blame] | 6923 | if (delalloc_start > end || delalloc_end <= start) { |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6924 | em = hole_em; |
| 6925 | hole_em = NULL; |
| 6926 | goto out; |
| 6927 | } |
| 6928 | |
Nikolay Borisov | f3714ef | 2018-12-12 09:42:33 +0200 | [diff] [blame] | 6929 | /* |
| 6930 | * Adjust the delalloc_start to make sure it doesn't go backwards from |
| 6931 | * the start they passed in |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6932 | */ |
Nikolay Borisov | f3714ef | 2018-12-12 09:42:33 +0200 | [diff] [blame] | 6933 | delalloc_start = max(start, delalloc_start); |
| 6934 | delalloc_len = delalloc_end - delalloc_start; |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6935 | |
Nikolay Borisov | f3714ef | 2018-12-12 09:42:33 +0200 | [diff] [blame] | 6936 | if (delalloc_len > 0) { |
| 6937 | u64 hole_start; |
Nikolay Borisov | 02950af | 2018-12-12 09:42:34 +0200 | [diff] [blame] | 6938 | u64 hole_len; |
Nikolay Borisov | f3714ef | 2018-12-12 09:42:33 +0200 | [diff] [blame] | 6939 | const u64 hole_end = extent_map_end(hole_em); |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6940 | |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 6941 | em = alloc_extent_map(); |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6942 | if (!em) { |
| 6943 | err = -ENOMEM; |
| 6944 | goto out; |
| 6945 | } |
Nikolay Borisov | f3714ef | 2018-12-12 09:42:33 +0200 | [diff] [blame] | 6946 | |
| 6947 | ASSERT(hole_em); |
| 6948 | /* |
| 6949 | * When btrfs_get_extent can't find anything it returns one |
| 6950 | * huge hole |
| 6951 | * |
| 6952 | * Make sure what it found really fits our range, and adjust to |
| 6953 | * make sure it is based on the start from the caller |
| 6954 | */ |
| 6955 | if (hole_end <= start || hole_em->start > end) { |
| 6956 | free_extent_map(hole_em); |
| 6957 | hole_em = NULL; |
| 6958 | } else { |
| 6959 | hole_start = max(hole_em->start, start); |
| 6960 | hole_len = hole_end - hole_start; |
| 6961 | } |
| 6962 | |
| 6963 | if (hole_em && delalloc_start > hole_start) { |
| 6964 | /* |
| 6965 | * Our hole starts before our delalloc, so we have to |
| 6966 | * return just the parts of the hole that go until the |
| 6967 | * delalloc starts |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6968 | */ |
Nikolay Borisov | f3714ef | 2018-12-12 09:42:33 +0200 | [diff] [blame] | 6969 | em->len = min(hole_len, delalloc_start - hole_start); |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6970 | em->start = hole_start; |
| 6971 | em->orig_start = hole_start; |
| 6972 | /* |
Nikolay Borisov | f3714ef | 2018-12-12 09:42:33 +0200 | [diff] [blame] | 6973 | * Don't adjust block start at all, it is fixed at |
| 6974 | * EXTENT_MAP_HOLE |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6975 | */ |
| 6976 | em->block_start = hole_em->block_start; |
| 6977 | em->block_len = hole_len; |
Liu Bo | f9e4fb5 | 2013-01-07 10:10:12 +0000 | [diff] [blame] | 6978 | if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags)) |
| 6979 | set_bit(EXTENT_FLAG_PREALLOC, &em->flags); |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6980 | } else { |
Nikolay Borisov | f3714ef | 2018-12-12 09:42:33 +0200 | [diff] [blame] | 6981 | /* |
| 6982 | * Hole is out of passed range or it starts after |
| 6983 | * delalloc range |
| 6984 | */ |
| 6985 | em->start = delalloc_start; |
| 6986 | em->len = delalloc_len; |
| 6987 | em->orig_start = delalloc_start; |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6988 | em->block_start = EXTENT_MAP_DELALLOC; |
Nikolay Borisov | f3714ef | 2018-12-12 09:42:33 +0200 | [diff] [blame] | 6989 | em->block_len = delalloc_len; |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6990 | } |
Nikolay Borisov | bf8d32b | 2017-12-01 11:19:43 +0200 | [diff] [blame] | 6991 | } else { |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6992 | return hole_em; |
| 6993 | } |
| 6994 | out: |
| 6995 | |
| 6996 | free_extent_map(hole_em); |
| 6997 | if (err) { |
| 6998 | free_extent_map(em); |
| 6999 | return ERR_PTR(err); |
| 7000 | } |
| 7001 | return em; |
| 7002 | } |
| 7003 | |
Nikolay Borisov | 64f5418 | 2020-06-03 08:55:31 +0300 | [diff] [blame] | 7004 | static struct extent_map *btrfs_create_dio_extent(struct btrfs_inode *inode, |
Filipe Manana | 5f9a8a5 | 2016-05-12 13:53:36 +0100 | [diff] [blame] | 7005 | const u64 start, |
| 7006 | const u64 len, |
| 7007 | const u64 orig_start, |
| 7008 | const u64 block_start, |
| 7009 | const u64 block_len, |
| 7010 | const u64 orig_block_len, |
| 7011 | const u64 ram_bytes, |
| 7012 | const int type) |
| 7013 | { |
| 7014 | struct extent_map *em = NULL; |
| 7015 | int ret; |
| 7016 | |
Filipe Manana | 5f9a8a5 | 2016-05-12 13:53:36 +0100 | [diff] [blame] | 7017 | if (type != BTRFS_ORDERED_NOCOW) { |
Nikolay Borisov | 64f5418 | 2020-06-03 08:55:31 +0300 | [diff] [blame] | 7018 | em = create_io_em(inode, start, len, orig_start, block_start, |
| 7019 | block_len, orig_block_len, ram_bytes, |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 7020 | BTRFS_COMPRESS_NONE, /* compress_type */ |
| 7021 | type); |
Filipe Manana | 5f9a8a5 | 2016-05-12 13:53:36 +0100 | [diff] [blame] | 7022 | if (IS_ERR(em)) |
| 7023 | goto out; |
| 7024 | } |
Nikolay Borisov | 64f5418 | 2020-06-03 08:55:31 +0300 | [diff] [blame] | 7025 | ret = btrfs_add_ordered_extent_dio(inode, start, block_start, len, |
| 7026 | block_len, type); |
Filipe Manana | 5f9a8a5 | 2016-05-12 13:53:36 +0100 | [diff] [blame] | 7027 | if (ret) { |
| 7028 | if (em) { |
| 7029 | free_extent_map(em); |
Nikolay Borisov | 64f5418 | 2020-06-03 08:55:31 +0300 | [diff] [blame] | 7030 | btrfs_drop_extent_cache(inode, start, start + len - 1, 0); |
Filipe Manana | 5f9a8a5 | 2016-05-12 13:53:36 +0100 | [diff] [blame] | 7031 | } |
| 7032 | em = ERR_PTR(ret); |
| 7033 | } |
| 7034 | out: |
Filipe Manana | 5f9a8a5 | 2016-05-12 13:53:36 +0100 | [diff] [blame] | 7035 | |
| 7036 | return em; |
| 7037 | } |
| 7038 | |
Nikolay Borisov | 9fc6f91 | 2020-06-03 08:55:32 +0300 | [diff] [blame] | 7039 | static struct extent_map *btrfs_new_extent_direct(struct btrfs_inode *inode, |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7040 | u64 start, u64 len) |
| 7041 | { |
Nikolay Borisov | 9fc6f91 | 2020-06-03 08:55:32 +0300 | [diff] [blame] | 7042 | struct btrfs_root *root = inode->root; |
| 7043 | struct btrfs_fs_info *fs_info = root->fs_info; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 7044 | struct extent_map *em; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7045 | struct btrfs_key ins; |
| 7046 | u64 alloc_hint; |
| 7047 | int ret; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7048 | |
Nikolay Borisov | 9fc6f91 | 2020-06-03 08:55:32 +0300 | [diff] [blame] | 7049 | alloc_hint = get_extent_allocation_hint(inode, start, len); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7050 | ret = btrfs_reserve_extent(root, len, len, fs_info->sectorsize, |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 7051 | 0, alloc_hint, &ins, 1, 1); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 7052 | if (ret) |
| 7053 | return ERR_PTR(ret); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7054 | |
Nikolay Borisov | 9fc6f91 | 2020-06-03 08:55:32 +0300 | [diff] [blame] | 7055 | em = btrfs_create_dio_extent(inode, start, ins.offset, start, |
Filipe Manana | 5f9a8a5 | 2016-05-12 13:53:36 +0100 | [diff] [blame] | 7056 | ins.objectid, ins.offset, ins.offset, |
Liu Bo | 6288d6e | 2017-02-21 12:12:58 -0800 | [diff] [blame] | 7057 | ins.offset, BTRFS_ORDERED_REGULAR); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7058 | btrfs_dec_block_group_reservations(fs_info, ins.objectid); |
Filipe Manana | 5f9a8a5 | 2016-05-12 13:53:36 +0100 | [diff] [blame] | 7059 | if (IS_ERR(em)) |
Nikolay Borisov | 9fc6f91 | 2020-06-03 08:55:32 +0300 | [diff] [blame] | 7060 | btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, |
| 7061 | 1); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 7062 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7063 | return em; |
| 7064 | } |
| 7065 | |
Anand Jain | f463963 | 2021-02-10 21:25:16 -0800 | [diff] [blame] | 7066 | 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] | 7067 | { |
| 7068 | struct btrfs_block_group *block_group; |
Anand Jain | f463963 | 2021-02-10 21:25:16 -0800 | [diff] [blame] | 7069 | bool readonly = false; |
Anand Jain | 05947ae | 2021-02-10 21:25:15 -0800 | [diff] [blame] | 7070 | |
| 7071 | block_group = btrfs_lookup_block_group(fs_info, bytenr); |
| 7072 | if (!block_group || block_group->ro) |
Anand Jain | f463963 | 2021-02-10 21:25:16 -0800 | [diff] [blame] | 7073 | readonly = true; |
Anand Jain | 05947ae | 2021-02-10 21:25:15 -0800 | [diff] [blame] | 7074 | if (block_group) |
| 7075 | btrfs_put_block_group(block_group); |
| 7076 | return readonly; |
| 7077 | } |
| 7078 | |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7079 | /* |
Qu Wenruo | e4ecaf9 | 2020-06-24 07:23:51 +0800 | [diff] [blame] | 7080 | * Check if we can do nocow write into the range [@offset, @offset + @len) |
| 7081 | * |
| 7082 | * @offset: File offset |
| 7083 | * @len: The length to write, will be updated to the nocow writeable |
| 7084 | * range |
| 7085 | * @orig_start: (optional) Return the original file offset of the file extent |
| 7086 | * @orig_len: (optional) Return the original on-disk length of the file extent |
| 7087 | * @ram_bytes: (optional) Return the ram_bytes of the file extent |
Boris Burkov | a84d5d4 | 2020-08-18 11:00:05 -0700 | [diff] [blame] | 7088 | * @strict: if true, omit optimizations that might force us into unnecessary |
| 7089 | * cow. e.g., don't trust generation number. |
Qu Wenruo | e4ecaf9 | 2020-06-24 07:23:51 +0800 | [diff] [blame] | 7090 | * |
Qu Wenruo | e4ecaf9 | 2020-06-24 07:23:51 +0800 | [diff] [blame] | 7091 | * Return: |
| 7092 | * >0 and update @len if we can do nocow write |
| 7093 | * 0 if we can't do nocow write |
| 7094 | * <0 if error happened |
| 7095 | * |
| 7096 | * NOTE: This only checks the file extents, caller is responsible to wait for |
| 7097 | * any ordered extents. |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7098 | */ |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 7099 | noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len, |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 7100 | u64 *orig_start, u64 *orig_block_len, |
Boris Burkov | a84d5d4 | 2020-08-18 11:00:05 -0700 | [diff] [blame] | 7101 | u64 *ram_bytes, bool strict) |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7102 | { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 7103 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7104 | struct btrfs_path *path; |
| 7105 | int ret; |
| 7106 | struct extent_buffer *leaf; |
| 7107 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Miao Xie | 7b2b708 | 2014-02-27 13:58:05 +0800 | [diff] [blame] | 7108 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7109 | struct btrfs_file_extent_item *fi; |
| 7110 | struct btrfs_key key; |
| 7111 | u64 disk_bytenr; |
| 7112 | u64 backref_offset; |
| 7113 | u64 extent_end; |
| 7114 | u64 num_bytes; |
| 7115 | int slot; |
| 7116 | int found_type; |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 7117 | bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW); |
Miao Xie | e77751a | 2013-12-27 21:11:50 +0800 | [diff] [blame] | 7118 | |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7119 | path = btrfs_alloc_path(); |
| 7120 | if (!path) |
| 7121 | return -ENOMEM; |
| 7122 | |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 7123 | ret = btrfs_lookup_file_extent(NULL, root, path, |
| 7124 | btrfs_ino(BTRFS_I(inode)), offset, 0); |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7125 | if (ret < 0) |
| 7126 | goto out; |
| 7127 | |
| 7128 | slot = path->slots[0]; |
| 7129 | if (ret == 1) { |
| 7130 | if (slot == 0) { |
| 7131 | /* can't find the item, must cow */ |
| 7132 | ret = 0; |
| 7133 | goto out; |
| 7134 | } |
| 7135 | slot--; |
| 7136 | } |
| 7137 | ret = 0; |
| 7138 | leaf = path->nodes[0]; |
| 7139 | btrfs_item_key_to_cpu(leaf, &key, slot); |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 7140 | if (key.objectid != btrfs_ino(BTRFS_I(inode)) || |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7141 | key.type != BTRFS_EXTENT_DATA_KEY) { |
| 7142 | /* not our file or wrong item type, must cow */ |
| 7143 | goto out; |
| 7144 | } |
| 7145 | |
| 7146 | if (key.offset > offset) { |
| 7147 | /* Wrong offset, must cow */ |
| 7148 | goto out; |
| 7149 | } |
| 7150 | |
| 7151 | fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item); |
| 7152 | found_type = btrfs_file_extent_type(leaf, fi); |
| 7153 | if (found_type != BTRFS_FILE_EXTENT_REG && |
| 7154 | found_type != BTRFS_FILE_EXTENT_PREALLOC) { |
| 7155 | /* not a regular extent, must cow */ |
| 7156 | goto out; |
| 7157 | } |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 7158 | |
| 7159 | if (!nocow && found_type == BTRFS_FILE_EXTENT_REG) |
| 7160 | goto out; |
| 7161 | |
Miao Xie | e77751a | 2013-12-27 21:11:50 +0800 | [diff] [blame] | 7162 | extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi); |
| 7163 | if (extent_end <= offset) |
| 7164 | goto out; |
| 7165 | |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7166 | disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 7167 | if (disk_bytenr == 0) |
| 7168 | goto out; |
| 7169 | |
| 7170 | if (btrfs_file_extent_compression(leaf, fi) || |
| 7171 | btrfs_file_extent_encryption(leaf, fi) || |
| 7172 | btrfs_file_extent_other_encoding(leaf, fi)) |
| 7173 | goto out; |
| 7174 | |
Ethan Lien | 78d4295 | 2018-05-17 14:58:29 +0800 | [diff] [blame] | 7175 | /* |
| 7176 | * Do the same check as in btrfs_cross_ref_exist but without the |
| 7177 | * unnecessary search. |
| 7178 | */ |
Boris Burkov | a84d5d4 | 2020-08-18 11:00:05 -0700 | [diff] [blame] | 7179 | if (!strict && |
| 7180 | (btrfs_file_extent_generation(leaf, fi) <= |
| 7181 | btrfs_root_last_snapshot(&root->root_item))) |
Ethan Lien | 78d4295 | 2018-05-17 14:58:29 +0800 | [diff] [blame] | 7182 | goto out; |
| 7183 | |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7184 | backref_offset = btrfs_file_extent_offset(leaf, fi); |
| 7185 | |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 7186 | if (orig_start) { |
| 7187 | *orig_start = key.offset - backref_offset; |
| 7188 | *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi); |
| 7189 | *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi); |
| 7190 | } |
Josef Bacik | eb384b5 | 2013-04-24 16:32:55 -0400 | [diff] [blame] | 7191 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 7192 | if (btrfs_extent_readonly(fs_info, disk_bytenr)) |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7193 | goto out; |
Miao Xie | 7b2b708 | 2014-02-27 13:58:05 +0800 | [diff] [blame] | 7194 | |
| 7195 | num_bytes = min(offset + *len, extent_end) - offset; |
| 7196 | if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) { |
| 7197 | u64 range_end; |
| 7198 | |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 7199 | range_end = round_up(offset + num_bytes, |
| 7200 | root->fs_info->sectorsize) - 1; |
Miao Xie | 7b2b708 | 2014-02-27 13:58:05 +0800 | [diff] [blame] | 7201 | ret = test_range_bit(io_tree, offset, range_end, |
| 7202 | EXTENT_DELALLOC, 0, NULL); |
| 7203 | if (ret) { |
| 7204 | ret = -EAGAIN; |
| 7205 | goto out; |
| 7206 | } |
| 7207 | } |
| 7208 | |
Josef Bacik | 1bda19e | 2013-10-18 12:10:36 -0400 | [diff] [blame] | 7209 | btrfs_release_path(path); |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7210 | |
| 7211 | /* |
| 7212 | * look for other files referencing this extent, if we |
| 7213 | * find any we must cow |
| 7214 | */ |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 7215 | |
Liu Bo | e4c3b2d | 2017-01-30 12:25:28 -0800 | [diff] [blame] | 7216 | ret = btrfs_cross_ref_exist(root, btrfs_ino(BTRFS_I(inode)), |
Boris Burkov | a84d5d4 | 2020-08-18 11:00:05 -0700 | [diff] [blame] | 7217 | key.offset - backref_offset, disk_bytenr, |
| 7218 | strict); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 7219 | if (ret) { |
| 7220 | ret = 0; |
| 7221 | goto out; |
| 7222 | } |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7223 | |
| 7224 | /* |
| 7225 | * adjust disk_bytenr and num_bytes to cover just the bytes |
| 7226 | * in this extent we are about to write. If there |
| 7227 | * are any csums in that range we have to cow in order |
| 7228 | * to keep the csums correct |
| 7229 | */ |
| 7230 | disk_bytenr += backref_offset; |
| 7231 | disk_bytenr += offset - key.offset; |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 7232 | if (csum_exist_in_range(fs_info, disk_bytenr, num_bytes)) |
| 7233 | goto out; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7234 | /* |
| 7235 | * all of the above have passed, it is safe to overwrite this extent |
| 7236 | * without cow |
| 7237 | */ |
Josef Bacik | eb384b5 | 2013-04-24 16:32:55 -0400 | [diff] [blame] | 7238 | *len = num_bytes; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7239 | ret = 1; |
| 7240 | out: |
| 7241 | btrfs_free_path(path); |
| 7242 | return ret; |
| 7243 | } |
| 7244 | |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7245 | static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend, |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 7246 | struct extent_state **cached_state, bool writing) |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7247 | { |
| 7248 | struct btrfs_ordered_extent *ordered; |
| 7249 | int ret = 0; |
| 7250 | |
| 7251 | while (1) { |
| 7252 | lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend, |
David Sterba | ff13db4 | 2015-12-03 14:30:40 +0100 | [diff] [blame] | 7253 | cached_state); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7254 | /* |
| 7255 | * 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] | 7256 | * 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] | 7257 | * extents in this range. |
| 7258 | */ |
Nikolay Borisov | a776c6f | 2017-02-20 13:50:49 +0200 | [diff] [blame] | 7259 | ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), lockstart, |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7260 | lockend - lockstart + 1); |
| 7261 | |
| 7262 | /* |
| 7263 | * We need to make sure there are no buffered pages in this |
| 7264 | * range either, we could have raced between the invalidate in |
| 7265 | * generic_file_direct_write and locking the extent. The |
| 7266 | * invalidate needs to happen so that reads after a write do not |
| 7267 | * get stale data. |
| 7268 | */ |
Alex Gartrell | fc4adbff | 2014-05-20 13:07:56 -0700 | [diff] [blame] | 7269 | if (!ordered && |
David Sterba | 051c98e | 2018-03-07 15:33:22 +0100 | [diff] [blame] | 7270 | (!writing || !filemap_range_has_page(inode->i_mapping, |
| 7271 | lockstart, lockend))) |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7272 | break; |
| 7273 | |
| 7274 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend, |
David Sterba | e43bbe5 | 2017-12-12 21:43:52 +0100 | [diff] [blame] | 7275 | cached_state); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7276 | |
| 7277 | if (ordered) { |
Filipe Manana | ade7702 | 2016-02-18 14:28:55 +0000 | [diff] [blame] | 7278 | /* |
| 7279 | * If we are doing a DIO read and the ordered extent we |
| 7280 | * found is for a buffered write, we can not wait for it |
| 7281 | * to complete and retry, because if we do so we can |
| 7282 | * deadlock with concurrent buffered writes on page |
| 7283 | * locks. This happens only if our DIO read covers more |
| 7284 | * than one extent map, if at this point has already |
| 7285 | * created an ordered extent for a previous extent map |
| 7286 | * and locked its range in the inode's io tree, and a |
| 7287 | * concurrent write against that previous extent map's |
| 7288 | * range and this range started (we unlock the ranges |
| 7289 | * in the io tree only when the bios complete and |
| 7290 | * buffered writes always lock pages before attempting |
| 7291 | * to lock range in the io tree). |
| 7292 | */ |
| 7293 | if (writing || |
| 7294 | test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags)) |
Nikolay Borisov | c0a4360 | 2020-09-18 12:15:53 +0300 | [diff] [blame] | 7295 | btrfs_start_ordered_extent(ordered, 1); |
Filipe Manana | ade7702 | 2016-02-18 14:28:55 +0000 | [diff] [blame] | 7296 | else |
| 7297 | ret = -ENOTBLK; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7298 | btrfs_put_ordered_extent(ordered); |
| 7299 | } else { |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7300 | /* |
Filipe Manana | b850ae1 | 2015-12-08 16:23:16 +0000 | [diff] [blame] | 7301 | * We could trigger writeback for this range (and wait |
| 7302 | * for it to complete) and then invalidate the pages for |
| 7303 | * this range (through invalidate_inode_pages2_range()), |
| 7304 | * but that can lead us to a deadlock with a concurrent |
Matthew Wilcox (Oracle) | ba206a0 | 2020-06-01 21:47:05 -0700 | [diff] [blame] | 7305 | * call to readahead (a buffered read or a defrag call |
Filipe Manana | b850ae1 | 2015-12-08 16:23:16 +0000 | [diff] [blame] | 7306 | * triggered a readahead) on a page lock due to an |
| 7307 | * ordered dio extent we created before but did not have |
| 7308 | * yet a corresponding bio submitted (whence it can not |
Matthew Wilcox (Oracle) | ba206a0 | 2020-06-01 21:47:05 -0700 | [diff] [blame] | 7309 | * complete), which makes readahead wait for that |
Filipe Manana | b850ae1 | 2015-12-08 16:23:16 +0000 | [diff] [blame] | 7310 | * ordered extent to complete while holding a lock on |
| 7311 | * that page. |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7312 | */ |
Filipe Manana | b850ae1 | 2015-12-08 16:23:16 +0000 | [diff] [blame] | 7313 | ret = -ENOTBLK; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7314 | } |
| 7315 | |
Filipe Manana | ade7702 | 2016-02-18 14:28:55 +0000 | [diff] [blame] | 7316 | if (ret) |
| 7317 | break; |
| 7318 | |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7319 | cond_resched(); |
| 7320 | } |
| 7321 | |
| 7322 | return ret; |
| 7323 | } |
| 7324 | |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 7325 | /* The callers of this must take lock_extent() */ |
Nikolay Borisov | 4b67c11 | 2020-06-03 08:55:05 +0300 | [diff] [blame] | 7326 | static struct extent_map *create_io_em(struct btrfs_inode *inode, u64 start, |
| 7327 | u64 len, u64 orig_start, u64 block_start, |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 7328 | u64 block_len, u64 orig_block_len, |
| 7329 | u64 ram_bytes, int compress_type, |
| 7330 | int type) |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7331 | { |
| 7332 | struct extent_map_tree *em_tree; |
| 7333 | struct extent_map *em; |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7334 | int ret; |
| 7335 | |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 7336 | ASSERT(type == BTRFS_ORDERED_PREALLOC || |
| 7337 | type == BTRFS_ORDERED_COMPRESSED || |
| 7338 | type == BTRFS_ORDERED_NOCOW || |
Liu Bo | 1af4a0a | 2017-02-13 15:35:09 -0800 | [diff] [blame] | 7339 | type == BTRFS_ORDERED_REGULAR); |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 7340 | |
Nikolay Borisov | 4b67c11 | 2020-06-03 08:55:05 +0300 | [diff] [blame] | 7341 | em_tree = &inode->extent_tree; |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7342 | em = alloc_extent_map(); |
| 7343 | if (!em) |
| 7344 | return ERR_PTR(-ENOMEM); |
| 7345 | |
| 7346 | em->start = start; |
| 7347 | em->orig_start = orig_start; |
| 7348 | em->len = len; |
| 7349 | em->block_len = block_len; |
| 7350 | em->block_start = block_start; |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 7351 | em->orig_block_len = orig_block_len; |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 7352 | em->ram_bytes = ram_bytes; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 7353 | em->generation = -1; |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7354 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
Liu Bo | 1af4a0a | 2017-02-13 15:35:09 -0800 | [diff] [blame] | 7355 | if (type == BTRFS_ORDERED_PREALLOC) { |
Josef Bacik | b11e234 | 2012-12-03 10:58:15 -0500 | [diff] [blame] | 7356 | set_bit(EXTENT_FLAG_FILLING, &em->flags); |
Liu Bo | 1af4a0a | 2017-02-13 15:35:09 -0800 | [diff] [blame] | 7357 | } else if (type == BTRFS_ORDERED_COMPRESSED) { |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 7358 | set_bit(EXTENT_FLAG_COMPRESSED, &em->flags); |
| 7359 | em->compress_type = compress_type; |
| 7360 | } |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7361 | |
| 7362 | do { |
Nikolay Borisov | 4b67c11 | 2020-06-03 08:55:05 +0300 | [diff] [blame] | 7363 | btrfs_drop_extent_cache(inode, em->start, |
| 7364 | em->start + em->len - 1, 0); |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7365 | write_lock(&em_tree->lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 7366 | ret = add_extent_mapping(em_tree, em, 1); |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7367 | write_unlock(&em_tree->lock); |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 7368 | /* |
| 7369 | * The caller has taken lock_extent(), who could race with us |
| 7370 | * to add em? |
| 7371 | */ |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7372 | } while (ret == -EEXIST); |
| 7373 | |
| 7374 | if (ret) { |
| 7375 | free_extent_map(em); |
| 7376 | return ERR_PTR(ret); |
| 7377 | } |
| 7378 | |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 7379 | /* 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] | 7380 | return em; |
| 7381 | } |
| 7382 | |
Nikolay Borisov | 1c8d017 | 2018-05-02 15:19:32 +0300 | [diff] [blame] | 7383 | |
Nikolay Borisov | c5794e5 | 2018-05-02 15:19:33 +0300 | [diff] [blame] | 7384 | static int btrfs_get_blocks_direct_write(struct extent_map **map, |
Nikolay Borisov | c5794e5 | 2018-05-02 15:19:33 +0300 | [diff] [blame] | 7385 | struct inode *inode, |
| 7386 | struct btrfs_dio_data *dio_data, |
| 7387 | u64 start, u64 len) |
| 7388 | { |
| 7389 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
| 7390 | struct extent_map *em = *map; |
Filipe Manana | f0bfa76 | 2021-10-28 16:03:41 +0100 | [diff] [blame] | 7391 | int type; |
| 7392 | u64 block_start, orig_start, orig_block_len, ram_bytes; |
| 7393 | bool can_nocow = false; |
| 7394 | bool space_reserved = false; |
Nikolay Borisov | c5794e5 | 2018-05-02 15:19:33 +0300 | [diff] [blame] | 7395 | int ret = 0; |
| 7396 | |
| 7397 | /* |
| 7398 | * We don't allocate a new extent in the following cases |
| 7399 | * |
| 7400 | * 1) The inode is marked as NODATACOW. In this case we'll just use the |
| 7401 | * existing extent. |
| 7402 | * 2) The extent is marked as PREALLOC. We're good to go here and can |
| 7403 | * just use the extent. |
| 7404 | * |
| 7405 | */ |
| 7406 | if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) || |
| 7407 | ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) && |
| 7408 | em->block_start != EXTENT_MAP_HOLE)) { |
Nikolay Borisov | c5794e5 | 2018-05-02 15:19:33 +0300 | [diff] [blame] | 7409 | if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) |
| 7410 | type = BTRFS_ORDERED_PREALLOC; |
| 7411 | else |
| 7412 | type = BTRFS_ORDERED_NOCOW; |
| 7413 | len = min(len, em->len - (start - em->start)); |
| 7414 | block_start = em->block_start + (start - em->start); |
| 7415 | |
| 7416 | if (can_nocow_extent(inode, start, &len, &orig_start, |
Boris Burkov | a84d5d4 | 2020-08-18 11:00:05 -0700 | [diff] [blame] | 7417 | &orig_block_len, &ram_bytes, false) == 1 && |
Filipe Manana | f0bfa76 | 2021-10-28 16:03:41 +0100 | [diff] [blame] | 7418 | btrfs_inc_nocow_writers(fs_info, block_start)) |
| 7419 | can_nocow = true; |
| 7420 | } |
Nikolay Borisov | c5794e5 | 2018-05-02 15:19:33 +0300 | [diff] [blame] | 7421 | |
Filipe Manana | f0bfa76 | 2021-10-28 16:03:41 +0100 | [diff] [blame] | 7422 | if (can_nocow) { |
| 7423 | struct extent_map *em2; |
| 7424 | |
| 7425 | /* We can NOCOW, so only need to reserve metadata space. */ |
| 7426 | ret = btrfs_delalloc_reserve_metadata(BTRFS_I(inode), len); |
| 7427 | if (ret < 0) { |
| 7428 | /* Our caller expects us to free the input extent map. */ |
| 7429 | free_extent_map(em); |
| 7430 | *map = NULL; |
Nikolay Borisov | c5794e5 | 2018-05-02 15:19:33 +0300 | [diff] [blame] | 7431 | btrfs_dec_nocow_writers(fs_info, block_start); |
Filipe Manana | f0bfa76 | 2021-10-28 16:03:41 +0100 | [diff] [blame] | 7432 | goto out; |
Nikolay Borisov | c5794e5 | 2018-05-02 15:19:33 +0300 | [diff] [blame] | 7433 | } |
Filipe Manana | f0bfa76 | 2021-10-28 16:03:41 +0100 | [diff] [blame] | 7434 | space_reserved = true; |
| 7435 | |
| 7436 | em2 = btrfs_create_dio_extent(BTRFS_I(inode), start, len, |
| 7437 | orig_start, block_start, |
| 7438 | len, orig_block_len, |
| 7439 | ram_bytes, type); |
| 7440 | btrfs_dec_nocow_writers(fs_info, block_start); |
| 7441 | if (type == BTRFS_ORDERED_PREALLOC) { |
| 7442 | free_extent_map(em); |
| 7443 | *map = em = em2; |
| 7444 | } |
| 7445 | |
| 7446 | if (IS_ERR(em2)) { |
| 7447 | ret = PTR_ERR(em2); |
| 7448 | goto out; |
| 7449 | } |
| 7450 | } else { |
| 7451 | const u64 prev_len = len; |
| 7452 | |
| 7453 | /* Our caller expects us to free the input extent map. */ |
| 7454 | free_extent_map(em); |
| 7455 | *map = NULL; |
| 7456 | |
| 7457 | /* We have to COW, so need to reserve metadata and data space. */ |
| 7458 | ret = btrfs_delalloc_reserve_space(BTRFS_I(inode), |
| 7459 | &dio_data->data_reserved, |
| 7460 | start, len); |
| 7461 | if (ret < 0) |
| 7462 | goto out; |
| 7463 | space_reserved = true; |
| 7464 | |
| 7465 | em = btrfs_new_extent_direct(BTRFS_I(inode), start, len); |
| 7466 | if (IS_ERR(em)) { |
| 7467 | ret = PTR_ERR(em); |
| 7468 | goto out; |
| 7469 | } |
| 7470 | *map = em; |
| 7471 | len = min(len, em->len - (start - em->start)); |
| 7472 | if (len < prev_len) |
| 7473 | btrfs_delalloc_release_space(BTRFS_I(inode), |
| 7474 | dio_data->data_reserved, |
| 7475 | start + len, prev_len - len, |
| 7476 | true); |
Nikolay Borisov | c5794e5 | 2018-05-02 15:19:33 +0300 | [diff] [blame] | 7477 | } |
| 7478 | |
Filipe Manana | f0bfa76 | 2021-10-28 16:03:41 +0100 | [diff] [blame] | 7479 | /* |
| 7480 | * We have created our ordered extent, so we can now release our reservation |
| 7481 | * for an outstanding extent. |
| 7482 | */ |
| 7483 | btrfs_delalloc_release_extents(BTRFS_I(inode), len); |
Nikolay Borisov | c5794e5 | 2018-05-02 15:19:33 +0300 | [diff] [blame] | 7484 | |
Nikolay Borisov | c5794e5 | 2018-05-02 15:19:33 +0300 | [diff] [blame] | 7485 | /* |
| 7486 | * Need to update the i_size under the extent lock so buffered |
| 7487 | * readers will get the updated i_size when we unlock. |
| 7488 | */ |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 7489 | if (start + len > i_size_read(inode)) |
Nikolay Borisov | c5794e5 | 2018-05-02 15:19:33 +0300 | [diff] [blame] | 7490 | i_size_write(inode, start + len); |
Nikolay Borisov | c5794e5 | 2018-05-02 15:19:33 +0300 | [diff] [blame] | 7491 | out: |
Filipe Manana | f0bfa76 | 2021-10-28 16:03:41 +0100 | [diff] [blame] | 7492 | if (ret && space_reserved) { |
| 7493 | btrfs_delalloc_release_extents(BTRFS_I(inode), len); |
| 7494 | if (can_nocow) { |
| 7495 | btrfs_delalloc_release_metadata(BTRFS_I(inode), len, true); |
| 7496 | } else { |
| 7497 | btrfs_delalloc_release_space(BTRFS_I(inode), |
| 7498 | dio_data->data_reserved, |
| 7499 | start, len, true); |
| 7500 | extent_changeset_free(dio_data->data_reserved); |
| 7501 | dio_data->data_reserved = NULL; |
| 7502 | } |
| 7503 | } |
Nikolay Borisov | c5794e5 | 2018-05-02 15:19:33 +0300 | [diff] [blame] | 7504 | return ret; |
| 7505 | } |
| 7506 | |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 7507 | static int btrfs_dio_iomap_begin(struct inode *inode, loff_t start, |
| 7508 | loff_t length, unsigned int flags, struct iomap *iomap, |
| 7509 | struct iomap *srcmap) |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7510 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7511 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7512 | struct extent_map *em; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7513 | struct extent_state *cached_state = NULL; |
chandan | 50745b0 | 2015-08-28 21:10:13 +0530 | [diff] [blame] | 7514 | struct btrfs_dio_data *dio_data = NULL; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7515 | u64 lockstart, lockend; |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 7516 | const bool write = !!(flags & IOMAP_WRITE); |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 7517 | int ret = 0; |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 7518 | u64 len = length; |
| 7519 | bool unlock_extents = false; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7520 | |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 7521 | if (!write) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7522 | len = min_t(u64, len, fs_info->sectorsize); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7523 | |
Josef Bacik | c329861 | 2012-08-03 16:49:19 -0400 | [diff] [blame] | 7524 | lockstart = start; |
| 7525 | lockend = start + len - 1; |
| 7526 | |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 7527 | /* |
| 7528 | * The generic stuff only does filemap_write_and_wait_range, which |
| 7529 | * isn't enough if we've written compressed pages to this area, so we |
| 7530 | * need to flush the dirty pages again to make absolutely sure that any |
| 7531 | * outstanding dirty pages are on disk. |
| 7532 | */ |
| 7533 | if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, |
| 7534 | &BTRFS_I(inode)->runtime_flags)) { |
| 7535 | ret = filemap_fdatawrite_range(inode->i_mapping, start, |
| 7536 | start + length - 1); |
| 7537 | if (ret) |
| 7538 | return ret; |
Josef Bacik | e1cbbfa | 2015-03-17 10:52:28 -0400 | [diff] [blame] | 7539 | } |
| 7540 | |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 7541 | dio_data = kzalloc(sizeof(*dio_data), GFP_NOFS); |
| 7542 | if (!dio_data) |
| 7543 | return -ENOMEM; |
| 7544 | |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 7545 | iomap->private = dio_data; |
| 7546 | |
| 7547 | |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7548 | /* |
| 7549 | * If this errors out it's because we couldn't invalidate pagecache for |
| 7550 | * this range and we need to fallback to buffered. |
| 7551 | */ |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 7552 | if (lock_extent_direct(inode, lockstart, lockend, &cached_state, write)) { |
Filipe Manana | 9c9464c | 2015-11-04 09:52:04 +0000 | [diff] [blame] | 7553 | ret = -ENOTBLK; |
| 7554 | goto err; |
| 7555 | } |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7556 | |
Omar Sandoval | 39b07b5 | 2019-12-02 17:34:23 -0800 | [diff] [blame] | 7557 | em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7558 | if (IS_ERR(em)) { |
| 7559 | ret = PTR_ERR(em); |
| 7560 | goto unlock_err; |
| 7561 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7562 | |
| 7563 | /* |
| 7564 | * Ok for INLINE and COMPRESSED extents we need to fallback on buffered |
| 7565 | * io. INLINE is special, and we could probably kludge it in here, but |
| 7566 | * it's still buffered so for safety lets just fall back to the generic |
| 7567 | * buffered path. |
| 7568 | * |
| 7569 | * For COMPRESSED we _have_ to read the entire extent in so we can |
| 7570 | * decompress it, so there will be buffering required no matter what we |
| 7571 | * do, so go ahead and fallback to buffered. |
| 7572 | * |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 7573 | * 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] | 7574 | * to buffered IO. Don't blame me, this is the price we pay for using |
| 7575 | * the generic code. |
| 7576 | */ |
| 7577 | if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) || |
| 7578 | em->block_start == EXTENT_MAP_INLINE) { |
| 7579 | free_extent_map(em); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7580 | ret = -ENOTBLK; |
| 7581 | goto unlock_err; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7582 | } |
| 7583 | |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 7584 | len = min(len, em->len - (start - em->start)); |
| 7585 | if (write) { |
| 7586 | ret = btrfs_get_blocks_direct_write(&em, inode, dio_data, |
| 7587 | start, len); |
Nikolay Borisov | c5794e5 | 2018-05-02 15:19:33 +0300 | [diff] [blame] | 7588 | if (ret < 0) |
| 7589 | goto unlock_err; |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 7590 | unlock_extents = true; |
| 7591 | /* Recalc len in case the new em is smaller than requested */ |
| 7592 | len = min(len, em->len - (start - em->start)); |
Nikolay Borisov | c5794e5 | 2018-05-02 15:19:33 +0300 | [diff] [blame] | 7593 | } else { |
Nikolay Borisov | 1c8d017 | 2018-05-02 15:19:32 +0300 | [diff] [blame] | 7594 | /* |
| 7595 | * We need to unlock only the end area that we aren't using. |
| 7596 | * The rest is going to be unlocked by the endio routine. |
| 7597 | */ |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 7598 | lockstart = start + len; |
| 7599 | if (lockstart < lockend) |
| 7600 | unlock_extents = true; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7601 | } |
| 7602 | |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 7603 | if (unlock_extents) |
| 7604 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, |
| 7605 | lockstart, lockend, &cached_state); |
| 7606 | else |
| 7607 | free_extent_state(cached_state); |
| 7608 | |
| 7609 | /* |
| 7610 | * Translate extent map information to iomap. |
| 7611 | * We trim the extents (and move the addr) even though iomap code does |
| 7612 | * that, since we have locked only the parts we are performing I/O in. |
| 7613 | */ |
| 7614 | if ((em->block_start == EXTENT_MAP_HOLE) || |
| 7615 | (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) && !write)) { |
| 7616 | iomap->addr = IOMAP_NULL_ADDR; |
| 7617 | iomap->type = IOMAP_HOLE; |
| 7618 | } else { |
| 7619 | iomap->addr = em->block_start + (start - em->start); |
| 7620 | iomap->type = IOMAP_MAPPED; |
| 7621 | } |
| 7622 | iomap->offset = start; |
Anand Jain | d24fa5c | 2021-08-24 13:05:19 +0800 | [diff] [blame] | 7623 | iomap->bdev = fs_info->fs_devices->latest_dev->bdev; |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 7624 | iomap->length = len; |
| 7625 | |
Johannes Thumshirn | e380adf | 2021-05-19 00:40:27 +0900 | [diff] [blame] | 7626 | if (write && btrfs_use_zone_append(BTRFS_I(inode), em->block_start)) |
Naohiro Aota | 544d24f | 2021-02-04 19:22:06 +0900 | [diff] [blame] | 7627 | iomap->flags |= IOMAP_F_ZONE_APPEND; |
| 7628 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7629 | free_extent_map(em); |
| 7630 | |
| 7631 | return 0; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7632 | |
| 7633 | unlock_err: |
Omar Sandoval | e182163 | 2019-08-15 14:04:04 -0700 | [diff] [blame] | 7634 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend, |
| 7635 | &cached_state); |
Filipe Manana | 9c9464c | 2015-11-04 09:52:04 +0000 | [diff] [blame] | 7636 | err: |
Filipe Manana | f0bfa76 | 2021-10-28 16:03:41 +0100 | [diff] [blame] | 7637 | kfree(dio_data); |
| 7638 | |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 7639 | return ret; |
| 7640 | } |
| 7641 | |
| 7642 | static int btrfs_dio_iomap_end(struct inode *inode, loff_t pos, loff_t length, |
| 7643 | ssize_t written, unsigned int flags, struct iomap *iomap) |
| 7644 | { |
| 7645 | int ret = 0; |
| 7646 | struct btrfs_dio_data *dio_data = iomap->private; |
| 7647 | size_t submitted = dio_data->submitted; |
| 7648 | const bool write = !!(flags & IOMAP_WRITE); |
| 7649 | |
| 7650 | if (!write && (iomap->type == IOMAP_HOLE)) { |
| 7651 | /* If reading from a hole, unlock and return */ |
| 7652 | unlock_extent(&BTRFS_I(inode)->io_tree, pos, pos + length - 1); |
| 7653 | goto out; |
| 7654 | } |
| 7655 | |
| 7656 | if (submitted < length) { |
| 7657 | pos += submitted; |
| 7658 | length -= submitted; |
| 7659 | if (write) |
| 7660 | __endio_write_update_ordered(BTRFS_I(inode), pos, |
| 7661 | length, false); |
| 7662 | else |
| 7663 | unlock_extent(&BTRFS_I(inode)->io_tree, pos, |
| 7664 | pos + length - 1); |
| 7665 | ret = -ENOTBLK; |
| 7666 | } |
| 7667 | |
Filipe Manana | f0bfa76 | 2021-10-28 16:03:41 +0100 | [diff] [blame] | 7668 | if (write) |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 7669 | extent_changeset_free(dio_data->data_reserved); |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 7670 | out: |
| 7671 | kfree(dio_data); |
| 7672 | iomap->private = NULL; |
| 7673 | |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7674 | return ret; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7675 | } |
| 7676 | |
Omar Sandoval | 769b4f2 | 2020-04-16 14:46:22 -0700 | [diff] [blame] | 7677 | static void btrfs_dio_private_put(struct btrfs_dio_private *dip) |
| 7678 | { |
| 7679 | /* |
| 7680 | * This implies a barrier so that stores to dio_bio->bi_status before |
| 7681 | * this and loads of dio_bio->bi_status after this are fully ordered. |
| 7682 | */ |
| 7683 | if (!refcount_dec_and_test(&dip->refs)) |
| 7684 | return; |
| 7685 | |
Naohiro Aota | cfe9444 | 2021-02-04 19:21:59 +0900 | [diff] [blame] | 7686 | if (btrfs_op(dip->dio_bio) == BTRFS_MAP_WRITE) { |
Nikolay Borisov | b672b5c | 2020-06-03 08:55:24 +0300 | [diff] [blame] | 7687 | __endio_write_update_ordered(BTRFS_I(dip->inode), |
Qu Wenruo | 47926ab | 2021-10-08 15:29:59 +0800 | [diff] [blame] | 7688 | dip->file_offset, |
Omar Sandoval | 769b4f2 | 2020-04-16 14:46:22 -0700 | [diff] [blame] | 7689 | dip->bytes, |
| 7690 | !dip->dio_bio->bi_status); |
| 7691 | } else { |
| 7692 | unlock_extent(&BTRFS_I(dip->inode)->io_tree, |
Qu Wenruo | 47926ab | 2021-10-08 15:29:59 +0800 | [diff] [blame] | 7693 | dip->file_offset, |
| 7694 | dip->file_offset + dip->bytes - 1); |
Omar Sandoval | 769b4f2 | 2020-04-16 14:46:22 -0700 | [diff] [blame] | 7695 | } |
| 7696 | |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 7697 | bio_endio(dip->dio_bio); |
Omar Sandoval | 769b4f2 | 2020-04-16 14:46:22 -0700 | [diff] [blame] | 7698 | kfree(dip); |
| 7699 | } |
| 7700 | |
Omar Sandoval | 77d5d68 | 2020-04-16 14:46:25 -0700 | [diff] [blame] | 7701 | static blk_status_t submit_dio_repair_bio(struct inode *inode, struct bio *bio, |
| 7702 | int mirror_num, |
| 7703 | unsigned long bio_flags) |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7704 | { |
Omar Sandoval | 77d5d68 | 2020-04-16 14:46:25 -0700 | [diff] [blame] | 7705 | struct btrfs_dio_private *dip = bio->bi_private; |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 7706 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Omar Sandoval | 58efbc9 | 2017-08-22 23:45:59 -0700 | [diff] [blame] | 7707 | blk_status_t ret; |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7708 | |
Mike Christie | 37226b2 | 2016-06-05 14:31:52 -0500 | [diff] [blame] | 7709 | BUG_ON(bio_op(bio) == REQ_OP_WRITE); |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7710 | |
Omar Sandoval | 5c047a6 | 2020-04-16 14:46:24 -0700 | [diff] [blame] | 7711 | 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] | 7712 | if (ret) |
Nikolay Borisov | ea057f6 | 2017-12-13 10:25:38 +0200 | [diff] [blame] | 7713 | return ret; |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7714 | |
Omar Sandoval | fd9d667 | 2020-04-16 14:46:23 -0700 | [diff] [blame] | 7715 | refcount_inc(&dip->refs); |
Chris Mason | 08635ba | 2019-07-10 12:28:14 -0700 | [diff] [blame] | 7716 | ret = btrfs_map_bio(fs_info, bio, mirror_num); |
Omar Sandoval | 77d5d68 | 2020-04-16 14:46:25 -0700 | [diff] [blame] | 7717 | if (ret) |
Omar Sandoval | fd9d667 | 2020-04-16 14:46:23 -0700 | [diff] [blame] | 7718 | refcount_dec(&dip->refs); |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7719 | return ret; |
| 7720 | } |
| 7721 | |
Qu Wenruo | f4f39fc | 2021-10-08 15:30:00 +0800 | [diff] [blame] | 7722 | static blk_status_t btrfs_check_read_dio_bio(struct btrfs_dio_private *dip, |
Qu Wenruo | c3a3b19 | 2021-09-15 15:17:18 +0800 | [diff] [blame] | 7723 | struct btrfs_bio *bbio, |
Omar Sandoval | fd9d667 | 2020-04-16 14:46:23 -0700 | [diff] [blame] | 7724 | const bool uptodate) |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7725 | { |
Qu Wenruo | f4f39fc | 2021-10-08 15:30:00 +0800 | [diff] [blame] | 7726 | struct inode *inode = dip->inode; |
Omar Sandoval | fd9d667 | 2020-04-16 14:46:23 -0700 | [diff] [blame] | 7727 | struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info; |
| 7728 | const u32 sectorsize = fs_info->sectorsize; |
Josef Bacik | 7870d08 | 2017-05-05 11:57:15 -0400 | [diff] [blame] | 7729 | struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree; |
Omar Sandoval | fd9d667 | 2020-04-16 14:46:23 -0700 | [diff] [blame] | 7730 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
| 7731 | const bool csum = !(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM); |
Liu Bo | 17347ce | 2017-05-15 15:33:27 -0700 | [diff] [blame] | 7732 | struct bio_vec bvec; |
| 7733 | struct bvec_iter iter; |
Qu Wenruo | f4f39fc | 2021-10-08 15:30:00 +0800 | [diff] [blame] | 7734 | const u64 orig_file_offset = dip->file_offset; |
| 7735 | u64 start = orig_file_offset; |
Qu Wenruo | 7ffd27e | 2020-12-02 14:47:58 +0800 | [diff] [blame] | 7736 | u32 bio_offset = 0; |
Omar Sandoval | 58efbc9 | 2017-08-22 23:45:59 -0700 | [diff] [blame] | 7737 | blk_status_t err = BLK_STS_OK; |
Miao Xie | dc380ae | 2014-09-12 18:43:55 +0800 | [diff] [blame] | 7738 | |
Qu Wenruo | c3a3b19 | 2021-09-15 15:17:18 +0800 | [diff] [blame] | 7739 | __bio_for_each_segment(bvec, &bbio->bio, iter, bbio->iter) { |
Omar Sandoval | fd9d667 | 2020-04-16 14:46:23 -0700 | [diff] [blame] | 7740 | unsigned int i, nr_sectors, pgoff; |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 7741 | |
Liu Bo | 17347ce | 2017-05-15 15:33:27 -0700 | [diff] [blame] | 7742 | nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec.bv_len); |
| 7743 | pgoff = bvec.bv_offset; |
Omar Sandoval | fd9d667 | 2020-04-16 14:46:23 -0700 | [diff] [blame] | 7744 | for (i = 0; i < nr_sectors; i++) { |
Liu Bo | 97bf5a5 | 2017-04-07 13:11:10 -0700 | [diff] [blame] | 7745 | ASSERT(pgoff < PAGE_SIZE); |
Omar Sandoval | fd9d667 | 2020-04-16 14:46:23 -0700 | [diff] [blame] | 7746 | if (uptodate && |
Qu Wenruo | c3a3b19 | 2021-09-15 15:17:18 +0800 | [diff] [blame] | 7747 | (!csum || !check_data_csum(inode, bbio, |
Omar Sandoval | c1d6abd | 2021-03-16 12:43:00 -0700 | [diff] [blame] | 7748 | bio_offset, bvec.bv_page, |
| 7749 | pgoff, start))) { |
Omar Sandoval | fd9d667 | 2020-04-16 14:46:23 -0700 | [diff] [blame] | 7750 | clean_io_failure(fs_info, failure_tree, io_tree, |
| 7751 | start, bvec.bv_page, |
| 7752 | btrfs_ino(BTRFS_I(inode)), |
| 7753 | pgoff); |
| 7754 | } else { |
Qu Wenruo | 150e4b0 | 2021-05-03 10:08:55 +0800 | [diff] [blame] | 7755 | int ret; |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7756 | |
Qu Wenruo | f4f39fc | 2021-10-08 15:30:00 +0800 | [diff] [blame] | 7757 | ASSERT((start - orig_file_offset) < UINT_MAX); |
Qu Wenruo | 150e4b0 | 2021-05-03 10:08:55 +0800 | [diff] [blame] | 7758 | ret = btrfs_repair_one_sector(inode, |
Qu Wenruo | c3a3b19 | 2021-09-15 15:17:18 +0800 | [diff] [blame] | 7759 | &bbio->bio, |
Qu Wenruo | f4f39fc | 2021-10-08 15:30:00 +0800 | [diff] [blame] | 7760 | start - orig_file_offset, |
Qu Wenruo | 150e4b0 | 2021-05-03 10:08:55 +0800 | [diff] [blame] | 7761 | bvec.bv_page, pgoff, |
Qu Wenruo | c3a3b19 | 2021-09-15 15:17:18 +0800 | [diff] [blame] | 7762 | start, bbio->mirror_num, |
Qu Wenruo | 150e4b0 | 2021-05-03 10:08:55 +0800 | [diff] [blame] | 7763 | submit_dio_repair_bio); |
| 7764 | if (ret) |
| 7765 | err = errno_to_blk_status(ret); |
Omar Sandoval | fd9d667 | 2020-04-16 14:46:23 -0700 | [diff] [blame] | 7766 | } |
| 7767 | start += sectorsize; |
Qu Wenruo | 7ffd27e | 2020-12-02 14:47:58 +0800 | [diff] [blame] | 7768 | ASSERT(bio_offset + sectorsize > bio_offset); |
| 7769 | bio_offset += sectorsize; |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 7770 | pgoff += sectorsize; |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 7771 | } |
Kent Overstreet | 2c30c71 | 2013-11-07 12:20:26 -0800 | [diff] [blame] | 7772 | } |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 7773 | return err; |
| 7774 | } |
| 7775 | |
Nikolay Borisov | b672b5c | 2020-06-03 08:55:24 +0300 | [diff] [blame] | 7776 | static void __endio_write_update_ordered(struct btrfs_inode *inode, |
Qu Wenruo | 52427260 | 2017-03-08 10:25:52 +0800 | [diff] [blame] | 7777 | const u64 offset, const u64 bytes, |
| 7778 | const bool uptodate) |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7779 | { |
Qu Wenruo | e65f152 | 2021-04-01 15:15:06 +0800 | [diff] [blame] | 7780 | btrfs_mark_ordered_io_finished(inode, NULL, offset, bytes, |
| 7781 | finish_ordered_fn, uptodate); |
Filipe Manana | 1454377 | 2015-11-24 16:23:54 +0000 | [diff] [blame] | 7782 | } |
| 7783 | |
Qu Wenruo | 8896a08 | 2020-10-21 14:24:53 +0800 | [diff] [blame] | 7784 | 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] | 7785 | struct bio *bio, |
| 7786 | u64 dio_file_offset) |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 7787 | { |
Qu Wenruo | 1941b64 | 2020-12-02 14:47:57 +0800 | [diff] [blame] | 7788 | 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] | 7789 | } |
| 7790 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 7791 | static void btrfs_end_dio_bio(struct bio *bio) |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7792 | { |
| 7793 | struct btrfs_dio_private *dip = bio->bi_private; |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 7794 | blk_status_t err = bio->bi_status; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7795 | |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7796 | if (err) |
| 7797 | btrfs_warn(BTRFS_I(dip->inode)->root->fs_info, |
Mike Christie | 6296b96 | 2016-06-05 14:32:21 -0500 | [diff] [blame] | 7798 | "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] | 7799 | btrfs_ino(BTRFS_I(dip->inode)), bio_op(bio), |
David Sterba | 1201b58 | 2020-11-26 15:41:27 +0100 | [diff] [blame] | 7800 | bio->bi_opf, bio->bi_iter.bi_sector, |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7801 | bio->bi_iter.bi_size, err); |
| 7802 | |
Qu Wenruo | f4f39fc | 2021-10-08 15:30:00 +0800 | [diff] [blame] | 7803 | if (bio_op(bio) == REQ_OP_READ) |
| 7804 | err = btrfs_check_read_dio_bio(dip, btrfs_bio(bio), !err); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7805 | |
Omar Sandoval | 769b4f2 | 2020-04-16 14:46:22 -0700 | [diff] [blame] | 7806 | if (err) |
| 7807 | dip->dio_bio->bi_status = err; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7808 | |
Qu Wenruo | 47926ab | 2021-10-08 15:29:59 +0800 | [diff] [blame] | 7809 | btrfs_record_physical_zoned(dip->inode, dip->file_offset, bio); |
Naohiro Aota | 544d24f | 2021-02-04 19:22:06 +0900 | [diff] [blame] | 7810 | |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7811 | bio_put(bio); |
Omar Sandoval | 769b4f2 | 2020-04-16 14:46:22 -0700 | [diff] [blame] | 7812 | btrfs_dio_private_put(dip); |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 7813 | } |
| 7814 | |
David Sterba | d0ee393 | 2018-03-08 14:35:48 +0100 | [diff] [blame] | 7815 | static inline blk_status_t btrfs_submit_dio_bio(struct bio *bio, |
| 7816 | struct inode *inode, u64 file_offset, int async_submit) |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7817 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7818 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 7819 | struct btrfs_dio_private *dip = bio->bi_private; |
Naohiro Aota | cfe9444 | 2021-02-04 19:21:59 +0900 | [diff] [blame] | 7820 | bool write = btrfs_op(bio) == BTRFS_MAP_WRITE; |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 7821 | blk_status_t ret; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7822 | |
Liu Bo | 4c274bc | 2017-11-01 17:19:27 -0600 | [diff] [blame] | 7823 | /* Check btrfs_submit_bio_hook() for rules about async submit. */ |
Josef Bacik | b812ce2 | 2012-11-16 13:56:32 -0500 | [diff] [blame] | 7824 | if (async_submit) |
| 7825 | async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers); |
| 7826 | |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 7827 | if (!write) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7828 | 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] | 7829 | if (ret) |
| 7830 | goto err; |
| 7831 | } |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7832 | |
Nikolay Borisov | e6961ca | 2017-08-03 15:44:58 +0300 | [diff] [blame] | 7833 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM) |
Josef Bacik | 1ae3993 | 2011-04-06 14:41:34 -0400 | [diff] [blame] | 7834 | goto map; |
| 7835 | |
| 7836 | if (write && async_submit) { |
Qu Wenruo | 1941b64 | 2020-12-02 14:47:57 +0800 | [diff] [blame] | 7837 | ret = btrfs_wq_submit_bio(inode, bio, 0, 0, file_offset, |
David Sterba | e288c08 | 2018-07-18 17:36:24 +0200 | [diff] [blame] | 7838 | btrfs_submit_bio_start_direct_io); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7839 | goto err; |
Josef Bacik | 1ae3993 | 2011-04-06 14:41:34 -0400 | [diff] [blame] | 7840 | } else if (write) { |
| 7841 | /* |
| 7842 | * If we aren't doing async submit, calculate the csum of the |
| 7843 | * bio now. |
| 7844 | */ |
Nikolay Borisov | bd242a0 | 2020-06-03 08:55:07 +0300 | [diff] [blame] | 7845 | ret = btrfs_csum_one_bio(BTRFS_I(inode), bio, file_offset, 1); |
Josef Bacik | 1ae3993 | 2011-04-06 14:41:34 -0400 | [diff] [blame] | 7846 | if (ret) |
| 7847 | goto err; |
Miao Xie | 23ea8e5 | 2014-09-12 18:43:54 +0800 | [diff] [blame] | 7848 | } else { |
Omar Sandoval | 8587957 | 2020-04-16 14:46:21 -0700 | [diff] [blame] | 7849 | u64 csum_offset; |
| 7850 | |
Qu Wenruo | 47926ab | 2021-10-08 15:29:59 +0800 | [diff] [blame] | 7851 | csum_offset = file_offset - dip->file_offset; |
David Sterba | 265fdfa | 2020-07-01 21:19:09 +0200 | [diff] [blame] | 7852 | csum_offset >>= fs_info->sectorsize_bits; |
David Sterba | 55fc29b | 2020-06-30 02:01:31 +0200 | [diff] [blame] | 7853 | csum_offset *= fs_info->csum_size; |
Qu Wenruo | c3a3b19 | 2021-09-15 15:17:18 +0800 | [diff] [blame] | 7854 | btrfs_bio(bio)->csum = dip->csums + csum_offset; |
Tsutomu Itoh | c2db107 | 2011-03-01 06:48:31 +0000 | [diff] [blame] | 7855 | } |
Josef Bacik | 1ae3993 | 2011-04-06 14:41:34 -0400 | [diff] [blame] | 7856 | map: |
Chris Mason | 08635ba | 2019-07-10 12:28:14 -0700 | [diff] [blame] | 7857 | ret = btrfs_map_bio(fs_info, bio, 0); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7858 | err: |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7859 | return ret; |
| 7860 | } |
| 7861 | |
Omar Sandoval | c36cac2 | 2020-04-16 14:46:13 -0700 | [diff] [blame] | 7862 | /* |
| 7863 | * If this succeeds, the btrfs_dio_private is responsible for cleaning up locked |
| 7864 | * or ordered extents whether or not we submit any bios. |
| 7865 | */ |
| 7866 | static struct btrfs_dio_private *btrfs_create_dio_private(struct bio *dio_bio, |
| 7867 | struct inode *inode, |
| 7868 | loff_t file_offset) |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7869 | { |
Naohiro Aota | cfe9444 | 2021-02-04 19:21:59 +0900 | [diff] [blame] | 7870 | const bool write = (btrfs_op(dio_bio) == BTRFS_MAP_WRITE); |
Omar Sandoval | 8587957 | 2020-04-16 14:46:21 -0700 | [diff] [blame] | 7871 | const bool csum = !(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM); |
| 7872 | size_t dip_size; |
Omar Sandoval | c36cac2 | 2020-04-16 14:46:13 -0700 | [diff] [blame] | 7873 | struct btrfs_dio_private *dip; |
Omar Sandoval | c36cac2 | 2020-04-16 14:46:13 -0700 | [diff] [blame] | 7874 | |
Omar Sandoval | 8587957 | 2020-04-16 14:46:21 -0700 | [diff] [blame] | 7875 | dip_size = sizeof(*dip); |
| 7876 | if (!write && csum) { |
| 7877 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Omar Sandoval | 8587957 | 2020-04-16 14:46:21 -0700 | [diff] [blame] | 7878 | size_t nblocks; |
| 7879 | |
David Sterba | 265fdfa | 2020-07-01 21:19:09 +0200 | [diff] [blame] | 7880 | nblocks = dio_bio->bi_iter.bi_size >> fs_info->sectorsize_bits; |
David Sterba | 223486c | 2020-07-02 11:27:30 +0200 | [diff] [blame] | 7881 | dip_size += fs_info->csum_size * nblocks; |
Omar Sandoval | 8587957 | 2020-04-16 14:46:21 -0700 | [diff] [blame] | 7882 | } |
| 7883 | |
| 7884 | dip = kzalloc(dip_size, GFP_NOFS); |
Omar Sandoval | c36cac2 | 2020-04-16 14:46:13 -0700 | [diff] [blame] | 7885 | if (!dip) |
| 7886 | return NULL; |
| 7887 | |
Omar Sandoval | c36cac2 | 2020-04-16 14:46:13 -0700 | [diff] [blame] | 7888 | dip->inode = inode; |
Qu Wenruo | 47926ab | 2021-10-08 15:29:59 +0800 | [diff] [blame] | 7889 | dip->file_offset = file_offset; |
Omar Sandoval | c36cac2 | 2020-04-16 14:46:13 -0700 | [diff] [blame] | 7890 | dip->bytes = dio_bio->bi_iter.bi_size; |
David Sterba | 1201b58 | 2020-11-26 15:41:27 +0100 | [diff] [blame] | 7891 | dip->disk_bytenr = dio_bio->bi_iter.bi_sector << 9; |
Omar Sandoval | c36cac2 | 2020-04-16 14:46:13 -0700 | [diff] [blame] | 7892 | dip->dio_bio = dio_bio; |
Omar Sandoval | e3b318d | 2020-04-16 14:46:20 -0700 | [diff] [blame] | 7893 | refcount_set(&dip->refs, 1); |
Omar Sandoval | c36cac2 | 2020-04-16 14:46:13 -0700 | [diff] [blame] | 7894 | return dip; |
| 7895 | } |
| 7896 | |
Christoph Hellwig | 3e08773 | 2021-10-12 13:12:24 +0200 | [diff] [blame] | 7897 | static void btrfs_submit_direct(const struct iomap_iter *iter, |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 7898 | struct bio *dio_bio, loff_t file_offset) |
Omar Sandoval | c36cac2 | 2020-04-16 14:46:13 -0700 | [diff] [blame] | 7899 | { |
Christoph Hellwig | a6d3d49 | 2021-08-10 18:33:10 -0700 | [diff] [blame] | 7900 | struct inode *inode = iter->inode; |
Naohiro Aota | cfe9444 | 2021-02-04 19:21:59 +0900 | [diff] [blame] | 7901 | const bool write = (btrfs_op(dio_bio) == BTRFS_MAP_WRITE); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7902 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Omar Sandoval | 769b4f2 | 2020-04-16 14:46:22 -0700 | [diff] [blame] | 7903 | const bool raid56 = (btrfs_data_alloc_profile(fs_info) & |
| 7904 | BTRFS_BLOCK_GROUP_RAID56_MASK); |
Omar Sandoval | c36cac2 | 2020-04-16 14:46:13 -0700 | [diff] [blame] | 7905 | struct btrfs_dio_private *dip; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7906 | struct bio *bio; |
Omar Sandoval | c36cac2 | 2020-04-16 14:46:13 -0700 | [diff] [blame] | 7907 | u64 start_sector; |
Josef Bacik | 1ae3993 | 2011-04-06 14:41:34 -0400 | [diff] [blame] | 7908 | int async_submit = 0; |
Liu Bo | 725130b | 2017-05-16 09:51:39 -0700 | [diff] [blame] | 7909 | u64 submit_len; |
Naohiro Aota | 42b5d73 | 2021-07-21 21:43:34 +0900 | [diff] [blame] | 7910 | u64 clone_offset = 0; |
| 7911 | u64 clone_len; |
Michal Rostecki | 4203431 | 2021-01-27 14:57:27 +0100 | [diff] [blame] | 7912 | u64 logical; |
Chandan Rajendra | 5f4dc8f | 2016-01-21 15:56:00 +0530 | [diff] [blame] | 7913 | int ret; |
Omar Sandoval | 58efbc9 | 2017-08-22 23:45:59 -0700 | [diff] [blame] | 7914 | blk_status_t status; |
Nikolay Borisov | 89b798a | 2019-06-03 12:05:05 +0300 | [diff] [blame] | 7915 | struct btrfs_io_geometry geom; |
Christoph Hellwig | a6d3d49 | 2021-08-10 18:33:10 -0700 | [diff] [blame] | 7916 | struct btrfs_dio_data *dio_data = iter->iomap.private; |
Michal Rostecki | 4203431 | 2021-01-27 14:57:27 +0100 | [diff] [blame] | 7917 | struct extent_map *em = NULL; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7918 | |
Omar Sandoval | c36cac2 | 2020-04-16 14:46:13 -0700 | [diff] [blame] | 7919 | dip = btrfs_create_dio_private(dio_bio, inode, file_offset); |
| 7920 | if (!dip) { |
| 7921 | if (!write) { |
| 7922 | unlock_extent(&BTRFS_I(inode)->io_tree, file_offset, |
| 7923 | file_offset + dio_bio->bi_iter.bi_size - 1); |
| 7924 | } |
| 7925 | dio_bio->bi_status = BLK_STS_RESOURCE; |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 7926 | bio_endio(dio_bio); |
Christoph Hellwig | 3e08773 | 2021-10-12 13:12:24 +0200 | [diff] [blame] | 7927 | return; |
Josef Bacik | 02f57c7 | 2011-04-06 14:25:44 -0400 | [diff] [blame] | 7928 | } |
| 7929 | |
Josef Bacik | 334c16d | 2020-10-16 11:29:14 -0400 | [diff] [blame] | 7930 | if (!write) { |
Omar Sandoval | 8587957 | 2020-04-16 14:46:21 -0700 | [diff] [blame] | 7931 | /* |
| 7932 | * Load the csums up front to reduce csum tree searches and |
| 7933 | * contention when submitting bios. |
Josef Bacik | 334c16d | 2020-10-16 11:29:14 -0400 | [diff] [blame] | 7934 | * |
| 7935 | * If we have csums disabled this will do nothing. |
Omar Sandoval | 8587957 | 2020-04-16 14:46:21 -0700 | [diff] [blame] | 7936 | */ |
Qu Wenruo | 6275193 | 2020-12-02 14:48:06 +0800 | [diff] [blame] | 7937 | status = btrfs_lookup_bio_sums(inode, dio_bio, dip->csums); |
Omar Sandoval | 8587957 | 2020-04-16 14:46:21 -0700 | [diff] [blame] | 7938 | if (status != BLK_STS_OK) |
| 7939 | goto out_err; |
| 7940 | } |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 7941 | |
Omar Sandoval | 769b4f2 | 2020-04-16 14:46:22 -0700 | [diff] [blame] | 7942 | start_sector = dio_bio->bi_iter.bi_sector; |
| 7943 | submit_len = dio_bio->bi_iter.bi_size; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7944 | |
Arnd Bergmann | 3c91ee6 | 2017-05-18 15:33:29 +0200 | [diff] [blame] | 7945 | do { |
Michal Rostecki | 4203431 | 2021-01-27 14:57:27 +0100 | [diff] [blame] | 7946 | logical = start_sector << 9; |
| 7947 | em = btrfs_get_chunk_map(fs_info, logical, submit_len); |
| 7948 | if (IS_ERR(em)) { |
| 7949 | status = errno_to_blk_status(PTR_ERR(em)); |
| 7950 | em = NULL; |
| 7951 | goto out_err_em; |
| 7952 | } |
| 7953 | ret = btrfs_get_io_geometry(fs_info, em, btrfs_op(dio_bio), |
Qu Wenruo | 43c0d1a | 2021-04-13 17:58:48 +0800 | [diff] [blame] | 7954 | logical, &geom); |
Omar Sandoval | 769b4f2 | 2020-04-16 14:46:22 -0700 | [diff] [blame] | 7955 | if (ret) { |
| 7956 | status = errno_to_blk_status(ret); |
Michal Rostecki | 4203431 | 2021-01-27 14:57:27 +0100 | [diff] [blame] | 7957 | goto out_err_em; |
Omar Sandoval | 769b4f2 | 2020-04-16 14:46:22 -0700 | [diff] [blame] | 7958 | } |
Omar Sandoval | 769b4f2 | 2020-04-16 14:46:22 -0700 | [diff] [blame] | 7959 | |
Naohiro Aota | 42b5d73 | 2021-07-21 21:43:34 +0900 | [diff] [blame] | 7960 | clone_len = min(submit_len, geom.len); |
| 7961 | ASSERT(clone_len <= UINT_MAX); |
Josef Bacik | 02f57c7 | 2011-04-06 14:25:44 -0400 | [diff] [blame] | 7962 | |
Liu Bo | 725130b | 2017-05-16 09:51:39 -0700 | [diff] [blame] | 7963 | /* |
| 7964 | * This will never fail as it's passing GPF_NOFS and |
| 7965 | * the allocation is backed by btrfs_bioset. |
| 7966 | */ |
Omar Sandoval | 769b4f2 | 2020-04-16 14:46:22 -0700 | [diff] [blame] | 7967 | bio = btrfs_bio_clone_partial(dio_bio, clone_offset, clone_len); |
Liu Bo | 725130b | 2017-05-16 09:51:39 -0700 | [diff] [blame] | 7968 | bio->bi_private = dip; |
| 7969 | bio->bi_end_io = btrfs_end_dio_bio; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7970 | |
Naohiro Aota | 544d24f | 2021-02-04 19:22:06 +0900 | [diff] [blame] | 7971 | if (bio_op(bio) == REQ_OP_ZONE_APPEND) { |
| 7972 | status = extract_ordered_extent(BTRFS_I(inode), bio, |
| 7973 | file_offset); |
| 7974 | if (status) { |
| 7975 | bio_put(bio); |
| 7976 | goto out_err; |
| 7977 | } |
| 7978 | } |
| 7979 | |
Liu Bo | 725130b | 2017-05-16 09:51:39 -0700 | [diff] [blame] | 7980 | ASSERT(submit_len >= clone_len); |
| 7981 | submit_len -= clone_len; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7982 | |
Liu Bo | 725130b | 2017-05-16 09:51:39 -0700 | [diff] [blame] | 7983 | /* |
| 7984 | * Increase the count before we submit the bio so we know |
| 7985 | * the end IO handler won't happen before we increase the |
| 7986 | * count. Otherwise, the dip might get freed before we're |
| 7987 | * done setting it up. |
Omar Sandoval | 769b4f2 | 2020-04-16 14:46:22 -0700 | [diff] [blame] | 7988 | * |
| 7989 | * We transfer the initial reference to the last bio, so we |
| 7990 | * don't need to increment the reference count for the last one. |
Liu Bo | 725130b | 2017-05-16 09:51:39 -0700 | [diff] [blame] | 7991 | */ |
Omar Sandoval | 769b4f2 | 2020-04-16 14:46:22 -0700 | [diff] [blame] | 7992 | if (submit_len > 0) { |
| 7993 | refcount_inc(&dip->refs); |
| 7994 | /* |
| 7995 | * If we are submitting more than one bio, submit them |
| 7996 | * all asynchronously. The exception is RAID 5 or 6, as |
| 7997 | * asynchronous checksums make it difficult to collect |
| 7998 | * full stripe writes. |
| 7999 | */ |
| 8000 | if (!raid56) |
| 8001 | async_submit = 1; |
| 8002 | } |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8003 | |
David Sterba | d0ee393 | 2018-03-08 14:35:48 +0100 | [diff] [blame] | 8004 | status = btrfs_submit_dio_bio(bio, inode, file_offset, |
Omar Sandoval | 58efbc9 | 2017-08-22 23:45:59 -0700 | [diff] [blame] | 8005 | async_submit); |
| 8006 | if (status) { |
Liu Bo | 725130b | 2017-05-16 09:51:39 -0700 | [diff] [blame] | 8007 | bio_put(bio); |
Omar Sandoval | 769b4f2 | 2020-04-16 14:46:22 -0700 | [diff] [blame] | 8008 | if (submit_len > 0) |
| 8009 | refcount_dec(&dip->refs); |
Michal Rostecki | 4203431 | 2021-01-27 14:57:27 +0100 | [diff] [blame] | 8010 | goto out_err_em; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8011 | } |
Liu Bo | 725130b | 2017-05-16 09:51:39 -0700 | [diff] [blame] | 8012 | |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 8013 | dio_data->submitted += clone_len; |
Liu Bo | 725130b | 2017-05-16 09:51:39 -0700 | [diff] [blame] | 8014 | clone_offset += clone_len; |
| 8015 | start_sector += clone_len >> 9; |
| 8016 | file_offset += clone_len; |
Michal Rostecki | 4203431 | 2021-01-27 14:57:27 +0100 | [diff] [blame] | 8017 | |
| 8018 | free_extent_map(em); |
Arnd Bergmann | 3c91ee6 | 2017-05-18 15:33:29 +0200 | [diff] [blame] | 8019 | } while (submit_len > 0); |
Christoph Hellwig | 3e08773 | 2021-10-12 13:12:24 +0200 | [diff] [blame] | 8020 | return; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8021 | |
Michal Rostecki | 4203431 | 2021-01-27 14:57:27 +0100 | [diff] [blame] | 8022 | out_err_em: |
| 8023 | free_extent_map(em); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8024 | out_err: |
Omar Sandoval | 769b4f2 | 2020-04-16 14:46:22 -0700 | [diff] [blame] | 8025 | dip->dio_bio->bi_status = status; |
| 8026 | btrfs_dio_private_put(dip); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8027 | } |
| 8028 | |
Goldwyn Rodrigues | 4e4cabe | 2020-09-24 11:39:12 -0500 | [diff] [blame] | 8029 | const struct iomap_ops btrfs_dio_iomap_ops = { |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 8030 | .iomap_begin = btrfs_dio_iomap_begin, |
| 8031 | .iomap_end = btrfs_dio_iomap_end, |
| 8032 | }; |
| 8033 | |
Goldwyn Rodrigues | 4e4cabe | 2020-09-24 11:39:12 -0500 | [diff] [blame] | 8034 | const struct iomap_dio_ops btrfs_dio_ops = { |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 8035 | .submit_io = btrfs_submit_direct, |
| 8036 | }; |
| 8037 | |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 8038 | static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, |
David Sterba | bab16e2 | 2020-06-23 20:56:12 +0200 | [diff] [blame] | 8039 | u64 start, u64 len) |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 8040 | { |
Tsutomu Itoh | 05dadc0 | 2012-11-29 05:08:26 +0000 | [diff] [blame] | 8041 | int ret; |
| 8042 | |
Christoph Hellwig | 45dd052 | 2020-05-23 09:30:14 +0200 | [diff] [blame] | 8043 | ret = fiemap_prep(inode, fieinfo, start, &len, 0); |
Tsutomu Itoh | 05dadc0 | 2012-11-29 05:08:26 +0000 | [diff] [blame] | 8044 | if (ret) |
| 8045 | return ret; |
| 8046 | |
Nikolay Borisov | facee0a | 2020-08-31 14:42:49 +0300 | [diff] [blame] | 8047 | return extent_fiemap(BTRFS_I(inode), fieinfo, start, len); |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 8048 | } |
| 8049 | |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8050 | int btrfs_readpage(struct file *file, struct page *page) |
| 8051 | { |
Nikolay Borisov | 0f20881 | 2020-09-14 14:39:16 +0300 | [diff] [blame] | 8052 | struct btrfs_inode *inode = BTRFS_I(page->mapping->host); |
| 8053 | u64 start = page_offset(page); |
| 8054 | u64 end = start + PAGE_SIZE - 1; |
Qu Wenruo | 390ed29 | 2021-04-14 16:42:15 +0800 | [diff] [blame] | 8055 | struct btrfs_bio_ctrl bio_ctrl = { 0 }; |
Nikolay Borisov | c1be9c1 | 2020-09-14 12:37:08 +0300 | [diff] [blame] | 8056 | int ret; |
| 8057 | |
Nikolay Borisov | 0f20881 | 2020-09-14 14:39:16 +0300 | [diff] [blame] | 8058 | btrfs_lock_and_flush_ordered_range(inode, start, end, NULL); |
| 8059 | |
Qu Wenruo | 390ed29 | 2021-04-14 16:42:15 +0800 | [diff] [blame] | 8060 | ret = btrfs_do_readpage(page, NULL, &bio_ctrl, 0, NULL); |
| 8061 | if (bio_ctrl.bio) |
| 8062 | ret = submit_one_bio(bio_ctrl.bio, 0, bio_ctrl.bio_flags); |
Nikolay Borisov | c1be9c1 | 2020-09-14 12:37:08 +0300 | [diff] [blame] | 8063 | return ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8064 | } |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 8065 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8066 | static int btrfs_writepage(struct page *page, struct writeback_control *wbc) |
| 8067 | { |
Josef Bacik | be7bd73 | 2015-10-22 15:05:09 -0400 | [diff] [blame] | 8068 | struct inode *inode = page->mapping->host; |
| 8069 | int ret; |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8070 | |
| 8071 | if (current->flags & PF_MEMALLOC) { |
| 8072 | redirty_page_for_writepage(wbc, page); |
| 8073 | unlock_page(page); |
| 8074 | return 0; |
| 8075 | } |
Josef Bacik | be7bd73 | 2015-10-22 15:05:09 -0400 | [diff] [blame] | 8076 | |
| 8077 | /* |
| 8078 | * If we are under memory pressure we will call this directly from the |
| 8079 | * VM, we need to make sure we have the inode referenced for the ordered |
| 8080 | * extent. If not just return like we didn't do anything. |
| 8081 | */ |
| 8082 | if (!igrab(inode)) { |
| 8083 | redirty_page_for_writepage(wbc, page); |
| 8084 | return AOP_WRITEPAGE_ACTIVATE; |
| 8085 | } |
Nikolay Borisov | 0a9b0e5 | 2017-12-08 15:55:59 +0200 | [diff] [blame] | 8086 | ret = extent_write_full_page(page, wbc); |
Josef Bacik | be7bd73 | 2015-10-22 15:05:09 -0400 | [diff] [blame] | 8087 | btrfs_add_delayed_iput(inode); |
| 8088 | return ret; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8089 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8090 | |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 8091 | static int btrfs_writepages(struct address_space *mapping, |
| 8092 | struct writeback_control *wbc) |
Chris Mason | b293f02e | 2007-11-01 19:45:34 -0400 | [diff] [blame] | 8093 | { |
Nikolay Borisov | 8ae225a | 2018-04-19 10:46:38 +0300 | [diff] [blame] | 8094 | return extent_writepages(mapping, wbc); |
Chris Mason | b293f02e | 2007-11-01 19:45:34 -0400 | [diff] [blame] | 8095 | } |
| 8096 | |
Matthew Wilcox (Oracle) | ba206a0 | 2020-06-01 21:47:05 -0700 | [diff] [blame] | 8097 | static void btrfs_readahead(struct readahead_control *rac) |
Chris Mason | 3ab2fb5 | 2007-11-08 10:59:22 -0500 | [diff] [blame] | 8098 | { |
Matthew Wilcox (Oracle) | ba206a0 | 2020-06-01 21:47:05 -0700 | [diff] [blame] | 8099 | extent_readahead(rac); |
Chris Mason | 3ab2fb5 | 2007-11-08 10:59:22 -0500 | [diff] [blame] | 8100 | } |
Nikolay Borisov | 2a3ff0a | 2018-04-19 10:46:36 +0300 | [diff] [blame] | 8101 | |
Qu Wenruo | 7c11d0a | 2021-07-26 14:35:03 +0800 | [diff] [blame] | 8102 | /* |
| 8103 | * For releasepage() and invalidatepage() we have a race window where |
| 8104 | * end_page_writeback() is called but the subpage spinlock is not yet released. |
| 8105 | * If we continue to release/invalidate the page, we could cause use-after-free |
| 8106 | * for subpage spinlock. So this function is to spin and wait for subpage |
| 8107 | * spinlock. |
| 8108 | */ |
| 8109 | static void wait_subpage_spinlock(struct page *page) |
| 8110 | { |
| 8111 | struct btrfs_fs_info *fs_info = btrfs_sb(page->mapping->host->i_sb); |
| 8112 | struct btrfs_subpage *subpage; |
| 8113 | |
| 8114 | if (fs_info->sectorsize == PAGE_SIZE) |
| 8115 | return; |
| 8116 | |
| 8117 | ASSERT(PagePrivate(page) && page->private); |
| 8118 | subpage = (struct btrfs_subpage *)page->private; |
| 8119 | |
| 8120 | /* |
| 8121 | * This may look insane as we just acquire the spinlock and release it, |
| 8122 | * without doing anything. But we just want to make sure no one is |
| 8123 | * still holding the subpage spinlock. |
| 8124 | * And since the page is not dirty nor writeback, and we have page |
| 8125 | * locked, the only possible way to hold a spinlock is from the endio |
| 8126 | * function to clear page writeback. |
| 8127 | * |
| 8128 | * Here we just acquire the spinlock so that all existing callers |
| 8129 | * should exit and we're safe to release/invalidate the page. |
| 8130 | */ |
| 8131 | spin_lock_irq(&subpage->lock); |
| 8132 | spin_unlock_irq(&subpage->lock); |
| 8133 | } |
| 8134 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8135 | static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8136 | { |
Nikolay Borisov | 477a30b | 2018-04-19 10:46:34 +0300 | [diff] [blame] | 8137 | int ret = try_release_extent_mapping(page, gfp_flags); |
Qu Wenruo | 7c11d0a | 2021-07-26 14:35:03 +0800 | [diff] [blame] | 8138 | |
| 8139 | if (ret == 1) { |
| 8140 | wait_subpage_spinlock(page); |
Qu Wenruo | 32443de | 2021-01-26 16:34:00 +0800 | [diff] [blame] | 8141 | clear_page_extent_mapped(page); |
Qu Wenruo | 7c11d0a | 2021-07-26 14:35:03 +0800 | [diff] [blame] | 8142 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8143 | return ret; |
| 8144 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8145 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8146 | static int btrfs_releasepage(struct page *page, gfp_t gfp_flags) |
| 8147 | { |
Chris Mason | 98509cf | 2008-09-11 15:51:43 -0400 | [diff] [blame] | 8148 | if (PageWriteback(page) || PageDirty(page)) |
| 8149 | return 0; |
Michal Hocko | 3ba7ab2 | 2017-01-09 15:39:02 +0100 | [diff] [blame] | 8150 | return __btrfs_releasepage(page, gfp_flags); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8151 | } |
| 8152 | |
Roman Gushchin | f8e6608 | 2020-03-04 16:57:35 -0800 | [diff] [blame] | 8153 | #ifdef CONFIG_MIGRATION |
| 8154 | static int btrfs_migratepage(struct address_space *mapping, |
| 8155 | struct page *newpage, struct page *page, |
| 8156 | enum migrate_mode mode) |
| 8157 | { |
| 8158 | int ret; |
| 8159 | |
| 8160 | ret = migrate_page_move_mapping(mapping, newpage, page, 0); |
| 8161 | if (ret != MIGRATEPAGE_SUCCESS) |
| 8162 | return ret; |
| 8163 | |
Guoqing Jiang | d1b89bc | 2020-06-01 21:47:45 -0700 | [diff] [blame] | 8164 | if (page_has_private(page)) |
| 8165 | attach_page_private(newpage, detach_page_private(page)); |
Roman Gushchin | f8e6608 | 2020-03-04 16:57:35 -0800 | [diff] [blame] | 8166 | |
Qu Wenruo | f57ad93 | 2021-04-07 19:22:13 +0800 | [diff] [blame] | 8167 | if (PageOrdered(page)) { |
| 8168 | ClearPageOrdered(page); |
| 8169 | SetPageOrdered(newpage); |
Roman Gushchin | f8e6608 | 2020-03-04 16:57:35 -0800 | [diff] [blame] | 8170 | } |
| 8171 | |
| 8172 | if (mode != MIGRATE_SYNC_NO_COPY) |
| 8173 | migrate_page_copy(newpage, page); |
| 8174 | else |
| 8175 | migrate_page_states(newpage, page); |
| 8176 | return MIGRATEPAGE_SUCCESS; |
| 8177 | } |
| 8178 | #endif |
| 8179 | |
Lukas Czerner | d47992f | 2013-05-21 23:17:23 -0400 | [diff] [blame] | 8180 | static void btrfs_invalidatepage(struct page *page, unsigned int offset, |
| 8181 | unsigned int length) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8182 | { |
Nikolay Borisov | 53ac7ea | 2020-08-31 14:42:43 +0300 | [diff] [blame] | 8183 | struct btrfs_inode *inode = BTRFS_I(page->mapping->host); |
Qu Wenruo | b945a46 | 2021-05-31 16:50:46 +0800 | [diff] [blame] | 8184 | struct btrfs_fs_info *fs_info = inode->root->fs_info; |
Nikolay Borisov | 53ac7ea | 2020-08-31 14:42:43 +0300 | [diff] [blame] | 8185 | struct extent_io_tree *tree = &inode->io_tree; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 8186 | struct extent_state *cached_state = NULL; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8187 | u64 page_start = page_offset(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 8188 | u64 page_end = page_start + PAGE_SIZE - 1; |
Qu Wenruo | 3b83584 | 2021-04-06 19:54:53 +0800 | [diff] [blame] | 8189 | u64 cur; |
Nikolay Borisov | 53ac7ea | 2020-08-31 14:42:43 +0300 | [diff] [blame] | 8190 | int inode_evicting = inode->vfs_inode.i_state & I_FREEING; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8191 | |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 8192 | /* |
Qu Wenruo | 266a258 | 2021-04-06 08:27:18 +0800 | [diff] [blame] | 8193 | * We have page locked so no new ordered extent can be created on this |
| 8194 | * page, nor bio can be submitted for this page. |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 8195 | * |
Qu Wenruo | 266a258 | 2021-04-06 08:27:18 +0800 | [diff] [blame] | 8196 | * But already submitted bio can still be finished on this page. |
Qu Wenruo | f57ad93 | 2021-04-07 19:22:13 +0800 | [diff] [blame] | 8197 | * Furthermore, endio function won't skip page which has Ordered |
| 8198 | * (Private2) already cleared, so it's possible for endio and |
| 8199 | * invalidatepage to do the same ordered extent accounting twice |
| 8200 | * on one page. |
Qu Wenruo | 266a258 | 2021-04-06 08:27:18 +0800 | [diff] [blame] | 8201 | * |
| 8202 | * So here we wait for any submitted bios to finish, so that we won't |
| 8203 | * do double ordered extent accounting on the same page. |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 8204 | */ |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8205 | wait_on_page_writeback(page); |
Qu Wenruo | 7c11d0a | 2021-07-26 14:35:03 +0800 | [diff] [blame] | 8206 | wait_subpage_spinlock(page); |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 8207 | |
Qu Wenruo | bcd7745 | 2021-05-31 16:50:55 +0800 | [diff] [blame] | 8208 | /* |
| 8209 | * For subpage case, we have call sites like |
| 8210 | * btrfs_punch_hole_lock_range() which passes range not aligned to |
| 8211 | * sectorsize. |
| 8212 | * If the range doesn't cover the full page, we don't need to and |
| 8213 | * shouldn't clear page extent mapped, as page->private can still |
| 8214 | * record subpage dirty bits for other part of the range. |
| 8215 | * |
| 8216 | * For cases that can invalidate the full even the range doesn't |
| 8217 | * cover the full page, like invalidating the last page, we're |
| 8218 | * still safe to wait for ordered extent to finish. |
| 8219 | */ |
| 8220 | if (!(offset == 0 && length == PAGE_SIZE)) { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8221 | btrfs_releasepage(page, GFP_NOFS); |
| 8222 | return; |
| 8223 | } |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 8224 | |
| 8225 | if (!inode_evicting) |
David Sterba | ff13db4 | 2015-12-03 14:30:40 +0100 | [diff] [blame] | 8226 | lock_extent_bits(tree, page_start, page_end, &cached_state); |
Qu Wenruo | 951c80f | 2021-01-27 14:38:48 +0800 | [diff] [blame] | 8227 | |
Qu Wenruo | 3b83584 | 2021-04-06 19:54:53 +0800 | [diff] [blame] | 8228 | cur = page_start; |
| 8229 | while (cur < page_end) { |
| 8230 | struct btrfs_ordered_extent *ordered; |
| 8231 | bool delete_states; |
| 8232 | u64 range_end; |
Qu Wenruo | b945a46 | 2021-05-31 16:50:46 +0800 | [diff] [blame] | 8233 | u32 range_len; |
Qu Wenruo | 3b83584 | 2021-04-06 19:54:53 +0800 | [diff] [blame] | 8234 | |
| 8235 | ordered = btrfs_lookup_first_ordered_range(inode, cur, |
| 8236 | page_end + 1 - cur); |
| 8237 | if (!ordered) { |
| 8238 | range_end = page_end; |
| 8239 | /* |
| 8240 | * No ordered extent covering this range, we are safe |
| 8241 | * to delete all extent states in the range. |
| 8242 | */ |
| 8243 | delete_states = true; |
| 8244 | goto next; |
| 8245 | } |
| 8246 | if (ordered->file_offset > cur) { |
| 8247 | /* |
| 8248 | * There is a range between [cur, oe->file_offset) not |
| 8249 | * covered by any ordered extent. |
| 8250 | * We are safe to delete all extent states, and handle |
| 8251 | * the ordered extent in the next iteration. |
| 8252 | */ |
| 8253 | range_end = ordered->file_offset - 1; |
| 8254 | delete_states = true; |
| 8255 | goto next; |
| 8256 | } |
| 8257 | |
| 8258 | range_end = min(ordered->file_offset + ordered->num_bytes - 1, |
| 8259 | page_end); |
Qu Wenruo | b945a46 | 2021-05-31 16:50:46 +0800 | [diff] [blame] | 8260 | ASSERT(range_end + 1 - cur < U32_MAX); |
| 8261 | range_len = range_end + 1 - cur; |
| 8262 | if (!btrfs_page_test_ordered(fs_info, page, cur, range_len)) { |
Qu Wenruo | 3b83584 | 2021-04-06 19:54:53 +0800 | [diff] [blame] | 8263 | /* |
Qu Wenruo | f57ad93 | 2021-04-07 19:22:13 +0800 | [diff] [blame] | 8264 | * If Ordered (Private2) is cleared, it means endio has |
| 8265 | * already been executed for the range. |
Qu Wenruo | 3b83584 | 2021-04-06 19:54:53 +0800 | [diff] [blame] | 8266 | * We can't delete the extent states as |
| 8267 | * btrfs_finish_ordered_io() may still use some of them. |
| 8268 | */ |
| 8269 | delete_states = false; |
| 8270 | goto next; |
| 8271 | } |
Qu Wenruo | b945a46 | 2021-05-31 16:50:46 +0800 | [diff] [blame] | 8272 | btrfs_page_clear_ordered(fs_info, page, cur, range_len); |
Qu Wenruo | 3b83584 | 2021-04-06 19:54:53 +0800 | [diff] [blame] | 8273 | |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 8274 | /* |
Filipe Manana | 2766ff6 | 2020-11-04 11:07:34 +0000 | [diff] [blame] | 8275 | * IO on this page will never be started, so we need to account |
| 8276 | * for any ordered extents now. Don't clear EXTENT_DELALLOC_NEW |
| 8277 | * here, must leave that up for the ordered extent completion. |
Qu Wenruo | 3b83584 | 2021-04-06 19:54:53 +0800 | [diff] [blame] | 8278 | * |
| 8279 | * This will also unlock the range for incoming |
| 8280 | * btrfs_finish_ordered_io(). |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 8281 | */ |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 8282 | if (!inode_evicting) |
Qu Wenruo | 3b83584 | 2021-04-06 19:54:53 +0800 | [diff] [blame] | 8283 | clear_extent_bit(tree, cur, range_end, |
Filipe Manana | 2766ff6 | 2020-11-04 11:07:34 +0000 | [diff] [blame] | 8284 | EXTENT_DELALLOC | |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 8285 | EXTENT_LOCKED | EXTENT_DO_ACCOUNTING | |
David Sterba | ae0f162 | 2017-10-31 16:37:52 +0100 | [diff] [blame] | 8286 | EXTENT_DEFRAG, 1, 0, &cached_state); |
Qu Wenruo | 3b83584 | 2021-04-06 19:54:53 +0800 | [diff] [blame] | 8287 | |
| 8288 | spin_lock_irq(&inode->ordered_tree.lock); |
| 8289 | set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags); |
| 8290 | ordered->truncated_len = min(ordered->truncated_len, |
| 8291 | cur - ordered->file_offset); |
| 8292 | spin_unlock_irq(&inode->ordered_tree.lock); |
| 8293 | |
| 8294 | if (btrfs_dec_test_ordered_pending(inode, &ordered, |
David Sterba | f41b6ba | 2021-07-26 14:15:10 +0200 | [diff] [blame] | 8295 | cur, range_end + 1 - cur)) { |
Qu Wenruo | 3b83584 | 2021-04-06 19:54:53 +0800 | [diff] [blame] | 8296 | btrfs_finish_ordered_io(ordered); |
| 8297 | /* |
| 8298 | * The ordered extent has finished, now we're again |
| 8299 | * safe to delete all extent states of the range. |
| 8300 | */ |
| 8301 | delete_states = true; |
| 8302 | } else { |
| 8303 | /* |
| 8304 | * btrfs_finish_ordered_io() will get executed by endio |
| 8305 | * of other pages, thus we can't delete extent states |
| 8306 | * anymore |
| 8307 | */ |
| 8308 | delete_states = false; |
| 8309 | } |
| 8310 | next: |
| 8311 | if (ordered) |
| 8312 | btrfs_put_ordered_extent(ordered); |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 8313 | /* |
Qu Wenruo | 3b83584 | 2021-04-06 19:54:53 +0800 | [diff] [blame] | 8314 | * Qgroup reserved space handler |
| 8315 | * Sector(s) here will be either: |
Qu Wenruo | 266a258 | 2021-04-06 08:27:18 +0800 | [diff] [blame] | 8316 | * |
Qu Wenruo | 3b83584 | 2021-04-06 19:54:53 +0800 | [diff] [blame] | 8317 | * 1) Already written to disk or bio already finished |
| 8318 | * Then its QGROUP_RESERVED bit in io_tree is already cleared. |
| 8319 | * Qgroup will be handled by its qgroup_record then. |
| 8320 | * btrfs_qgroup_free_data() call will do nothing here. |
| 8321 | * |
| 8322 | * 2) Not written to disk yet |
| 8323 | * Then btrfs_qgroup_free_data() call will clear the |
| 8324 | * QGROUP_RESERVED bit of its io_tree, and free the qgroup |
| 8325 | * reserved data space. |
| 8326 | * Since the IO will never happen for this page. |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 8327 | */ |
Qu Wenruo | 3b83584 | 2021-04-06 19:54:53 +0800 | [diff] [blame] | 8328 | 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] | 8329 | if (!inode_evicting) { |
Qu Wenruo | 3b83584 | 2021-04-06 19:54:53 +0800 | [diff] [blame] | 8330 | clear_extent_bit(tree, cur, range_end, EXTENT_LOCKED | |
Filipe Manana | 2766ff6 | 2020-11-04 11:07:34 +0000 | [diff] [blame] | 8331 | EXTENT_DELALLOC | EXTENT_UPTODATE | |
| 8332 | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, |
Qu Wenruo | 3b83584 | 2021-04-06 19:54:53 +0800 | [diff] [blame] | 8333 | delete_states, &cached_state); |
| 8334 | } |
| 8335 | cur = range_end + 1; |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 8336 | } |
Qu Wenruo | 3b83584 | 2021-04-06 19:54:53 +0800 | [diff] [blame] | 8337 | /* |
| 8338 | * We have iterated through all ordered extents of the page, the page |
Qu Wenruo | f57ad93 | 2021-04-07 19:22:13 +0800 | [diff] [blame] | 8339 | * should not have Ordered (Private2) anymore, or the above iteration |
| 8340 | * did something wrong. |
Qu Wenruo | 3b83584 | 2021-04-06 19:54:53 +0800 | [diff] [blame] | 8341 | */ |
Qu Wenruo | f57ad93 | 2021-04-07 19:22:13 +0800 | [diff] [blame] | 8342 | ASSERT(!PageOrdered(page)); |
Qu Wenruo | e4f9434 | 2021-09-27 15:21:49 +0800 | [diff] [blame] | 8343 | btrfs_page_clear_checked(fs_info, page, page_offset(page), PAGE_SIZE); |
Qu Wenruo | 3b83584 | 2021-04-06 19:54:53 +0800 | [diff] [blame] | 8344 | if (!inode_evicting) |
| 8345 | __btrfs_releasepage(page, GFP_NOFS); |
Qu Wenruo | 32443de | 2021-01-26 16:34:00 +0800 | [diff] [blame] | 8346 | clear_page_extent_mapped(page); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8347 | } |
| 8348 | |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8349 | /* |
| 8350 | * btrfs_page_mkwrite() is not allowed to change the file size as it gets |
| 8351 | * called from a page fault handler when a page is first dirtied. Hence we must |
| 8352 | * be careful to check for EOF conditions here. We set the page up correctly |
| 8353 | * for a written page which means we get ENOSPC checking when writing into |
| 8354 | * holes and correct delalloc and unwritten extent mapping on filesystems that |
| 8355 | * support these features. |
| 8356 | * |
| 8357 | * We are not allowed to take the i_mutex here so we have to play games to |
| 8358 | * 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] | 8359 | * truncate_setsize() writes the inode size before removing pages, once we have |
| 8360 | * 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] | 8361 | * beyond EOF, then the page is guaranteed safe against truncation until we |
| 8362 | * unlock the page. |
| 8363 | */ |
Souptick Joarder | a528a24 | 2018-06-06 19:54:44 +0530 | [diff] [blame] | 8364 | vm_fault_t btrfs_page_mkwrite(struct vm_fault *vmf) |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8365 | { |
Nick Piggin | c2ec175 | 2009-03-31 15:23:21 -0700 | [diff] [blame] | 8366 | struct page *page = vmf->page; |
Dave Jiang | 11bac80 | 2017-02-24 14:56:41 -0800 | [diff] [blame] | 8367 | struct inode *inode = file_inode(vmf->vma->vm_file); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8368 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8369 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
| 8370 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 8371 | struct extent_state *cached_state = NULL; |
Qu Wenruo | 364ecf3 | 2017-02-27 15:10:38 +0800 | [diff] [blame] | 8372 | struct extent_changeset *data_reserved = NULL; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8373 | unsigned long zero_start; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8374 | loff_t size; |
Souptick Joarder | a528a24 | 2018-06-06 19:54:44 +0530 | [diff] [blame] | 8375 | vm_fault_t ret; |
| 8376 | int ret2; |
Chris Mason | 9998eb7 | 2012-01-25 13:47:40 -0500 | [diff] [blame] | 8377 | int reserved = 0; |
Chandan Rajendra | d0b7da88 | 2016-01-21 15:55:57 +0530 | [diff] [blame] | 8378 | u64 reserved_space; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8379 | u64 page_start; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8380 | u64 page_end; |
Chandan Rajendra | d0b7da88 | 2016-01-21 15:55:57 +0530 | [diff] [blame] | 8381 | u64 end; |
| 8382 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 8383 | reserved_space = PAGE_SIZE; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8384 | |
Jan Kara | b2b5ef5 | 2012-06-12 16:20:45 +0200 | [diff] [blame] | 8385 | sb_start_pagefault(inode->i_sb); |
Qu Wenruo | df48063 | 2015-09-08 17:25:54 +0800 | [diff] [blame] | 8386 | page_start = page_offset(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 8387 | page_end = page_start + PAGE_SIZE - 1; |
Chandan Rajendra | d0b7da88 | 2016-01-21 15:55:57 +0530 | [diff] [blame] | 8388 | end = page_end; |
Qu Wenruo | df48063 | 2015-09-08 17:25:54 +0800 | [diff] [blame] | 8389 | |
Chandan Rajendra | d0b7da88 | 2016-01-21 15:55:57 +0530 | [diff] [blame] | 8390 | /* |
| 8391 | * Reserving delalloc space after obtaining the page lock can lead to |
| 8392 | * deadlock. For example, if a dirty page is locked by this function |
| 8393 | * and the call to btrfs_delalloc_reserve_space() ends up triggering |
| 8394 | * dirty page write out, then the btrfs_writepage() function could |
| 8395 | * end up waiting indefinitely to get a lock on the page currently |
| 8396 | * being processed by btrfs_page_mkwrite() function. |
| 8397 | */ |
Nikolay Borisov | e5b7231e | 2020-06-03 08:55:42 +0300 | [diff] [blame] | 8398 | ret2 = btrfs_delalloc_reserve_space(BTRFS_I(inode), &data_reserved, |
| 8399 | page_start, reserved_space); |
Souptick Joarder | a528a24 | 2018-06-06 19:54:44 +0530 | [diff] [blame] | 8400 | if (!ret2) { |
| 8401 | ret2 = file_update_time(vmf->vma->vm_file); |
Chris Mason | 9998eb7 | 2012-01-25 13:47:40 -0500 | [diff] [blame] | 8402 | reserved = 1; |
| 8403 | } |
Souptick Joarder | a528a24 | 2018-06-06 19:54:44 +0530 | [diff] [blame] | 8404 | if (ret2) { |
| 8405 | ret = vmf_error(ret2); |
Chris Mason | 9998eb7 | 2012-01-25 13:47:40 -0500 | [diff] [blame] | 8406 | if (reserved) |
| 8407 | goto out; |
| 8408 | goto out_noreserve; |
Nick Piggin | 56a76f8 | 2009-03-31 15:23:23 -0700 | [diff] [blame] | 8409 | } |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 8410 | |
Nick Piggin | 56a76f8 | 2009-03-31 15:23:23 -0700 | [diff] [blame] | 8411 | ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */ |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8412 | again: |
Josef Bacik | 8318ba7 | 2021-02-10 17:14:33 -0500 | [diff] [blame] | 8413 | down_read(&BTRFS_I(inode)->i_mmap_lock); |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8414 | lock_page(page); |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8415 | size = i_size_read(inode); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8416 | |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8417 | if ((page->mapping != inode->i_mapping) || |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8418 | (page_start >= size)) { |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8419 | /* page got truncated out from underneath us */ |
| 8420 | goto out_unlock; |
| 8421 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8422 | wait_on_page_writeback(page); |
| 8423 | |
David Sterba | ff13db4 | 2015-12-03 14:30:40 +0100 | [diff] [blame] | 8424 | lock_extent_bits(io_tree, page_start, page_end, &cached_state); |
Qu Wenruo | 32443de | 2021-01-26 16:34:00 +0800 | [diff] [blame] | 8425 | ret2 = set_page_extent_mapped(page); |
| 8426 | if (ret2 < 0) { |
| 8427 | ret = vmf_error(ret2); |
| 8428 | unlock_extent_cached(io_tree, page_start, page_end, &cached_state); |
| 8429 | goto out_unlock; |
| 8430 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8431 | |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 8432 | /* |
| 8433 | * we can't set the delalloc bits if there are pending ordered |
| 8434 | * extents. Drop our locks and wait for them to finish |
| 8435 | */ |
Nikolay Borisov | a776c6f | 2017-02-20 13:50:49 +0200 | [diff] [blame] | 8436 | ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start, |
| 8437 | PAGE_SIZE); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8438 | if (ordered) { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 8439 | unlock_extent_cached(io_tree, page_start, page_end, |
David Sterba | e43bbe5 | 2017-12-12 21:43:52 +0100 | [diff] [blame] | 8440 | &cached_state); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8441 | unlock_page(page); |
Josef Bacik | 8318ba7 | 2021-02-10 17:14:33 -0500 | [diff] [blame] | 8442 | up_read(&BTRFS_I(inode)->i_mmap_lock); |
Nikolay Borisov | c0a4360 | 2020-09-18 12:15:53 +0300 | [diff] [blame] | 8443 | btrfs_start_ordered_extent(ordered, 1); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8444 | btrfs_put_ordered_extent(ordered); |
| 8445 | goto again; |
| 8446 | } |
| 8447 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 8448 | if (page->index == ((size - 1) >> PAGE_SHIFT)) { |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 8449 | reserved_space = round_up(size - page_start, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8450 | fs_info->sectorsize); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 8451 | if (reserved_space < PAGE_SIZE) { |
Chandan Rajendra | d0b7da88 | 2016-01-21 15:55:57 +0530 | [diff] [blame] | 8452 | end = page_start + reserved_space - 1; |
Nikolay Borisov | 86d5292 | 2020-06-03 08:55:40 +0300 | [diff] [blame] | 8453 | btrfs_delalloc_release_space(BTRFS_I(inode), |
| 8454 | data_reserved, page_start, |
| 8455 | PAGE_SIZE - reserved_space, true); |
Chandan Rajendra | d0b7da88 | 2016-01-21 15:55:57 +0530 | [diff] [blame] | 8456 | } |
| 8457 | } |
| 8458 | |
Josef Bacik | fbf1908 | 2009-10-01 17:10:23 -0400 | [diff] [blame] | 8459 | /* |
Liu Bo | 5416034 | 2016-12-13 12:15:19 -0800 | [diff] [blame] | 8460 | * page_mkwrite gets called when the page is firstly dirtied after it's |
| 8461 | * faulted in, but write(2) could also dirty a page and set delalloc |
| 8462 | * bits, thus in this case for space account reason, we still need to |
| 8463 | * clear any delalloc bits within this page range since we have to |
| 8464 | * reserve data&meta space before lock_page() (see above comments). |
Josef Bacik | fbf1908 | 2009-10-01 17:10:23 -0400 | [diff] [blame] | 8465 | */ |
Chandan Rajendra | d0b7da88 | 2016-01-21 15:55:57 +0530 | [diff] [blame] | 8466 | clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, end, |
Omar Sandoval | e182163 | 2019-08-15 14:04:04 -0700 | [diff] [blame] | 8467 | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING | |
| 8468 | EXTENT_DEFRAG, 0, 0, &cached_state); |
Josef Bacik | fbf1908 | 2009-10-01 17:10:23 -0400 | [diff] [blame] | 8469 | |
Nikolay Borisov | c2566f2 | 2020-06-03 08:55:35 +0300 | [diff] [blame] | 8470 | ret2 = btrfs_set_extent_delalloc(BTRFS_I(inode), page_start, end, 0, |
Nikolay Borisov | 330a582 | 2019-07-17 16:18:17 +0300 | [diff] [blame] | 8471 | &cached_state); |
Souptick Joarder | a528a24 | 2018-06-06 19:54:44 +0530 | [diff] [blame] | 8472 | if (ret2) { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 8473 | unlock_extent_cached(io_tree, page_start, page_end, |
David Sterba | e43bbe5 | 2017-12-12 21:43:52 +0100 | [diff] [blame] | 8474 | &cached_state); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 8475 | ret = VM_FAULT_SIGBUS; |
| 8476 | goto out_unlock; |
| 8477 | } |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8478 | |
| 8479 | /* page is wholly or partially inside EOF */ |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 8480 | if (page_start + PAGE_SIZE > size) |
Johannes Thumshirn | 7073017 | 2018-12-05 15:23:03 +0100 | [diff] [blame] | 8481 | zero_start = offset_in_page(size); |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8482 | else |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 8483 | zero_start = PAGE_SIZE; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8484 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 8485 | if (zero_start != PAGE_SIZE) { |
Ira Weiny | d048b9c | 2021-05-04 18:40:07 -0700 | [diff] [blame] | 8486 | memzero_page(page, zero_start, PAGE_SIZE - zero_start); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8487 | flush_dcache_page(page); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8488 | } |
Qu Wenruo | e4f9434 | 2021-09-27 15:21:49 +0800 | [diff] [blame] | 8489 | btrfs_page_clear_checked(fs_info, page, page_start, PAGE_SIZE); |
Qu Wenruo | 2d8ec40 | 2021-05-31 16:50:52 +0800 | [diff] [blame] | 8490 | btrfs_page_set_dirty(fs_info, page, page_start, end + 1 - page_start); |
| 8491 | 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] | 8492 | |
Filipe Manana | bc0939f | 2021-02-23 12:08:48 +0000 | [diff] [blame] | 8493 | btrfs_set_inode_last_sub_trans(BTRFS_I(inode)); |
Chris Mason | 257c62e | 2009-10-13 13:21:08 -0400 | [diff] [blame] | 8494 | |
David Sterba | e43bbe5 | 2017-12-12 21:43:52 +0100 | [diff] [blame] | 8495 | unlock_extent_cached(io_tree, page_start, page_end, &cached_state); |
Josef Bacik | 8318ba7 | 2021-02-10 17:14:33 -0500 | [diff] [blame] | 8496 | up_read(&BTRFS_I(inode)->i_mmap_lock); |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8497 | |
Yunfeng Ye | 76de60e | 2019-12-03 16:59:25 +0800 | [diff] [blame] | 8498 | btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE); |
| 8499 | sb_end_pagefault(inode->i_sb); |
| 8500 | extent_changeset_free(data_reserved); |
| 8501 | return VM_FAULT_LOCKED; |
Chris Mason | 717beb9 | 2018-06-25 10:03:41 -0700 | [diff] [blame] | 8502 | |
| 8503 | out_unlock: |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8504 | unlock_page(page); |
Josef Bacik | 8318ba7 | 2021-02-10 17:14:33 -0500 | [diff] [blame] | 8505 | up_read(&BTRFS_I(inode)->i_mmap_lock); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 8506 | out: |
Qu Wenruo | 8702ba9 | 2019-10-14 14:34:51 +0800 | [diff] [blame] | 8507 | btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE); |
Nikolay Borisov | 86d5292 | 2020-06-03 08:55:40 +0300 | [diff] [blame] | 8508 | btrfs_delalloc_release_space(BTRFS_I(inode), data_reserved, page_start, |
Qu Wenruo | 43b1859 | 2017-12-12 15:34:32 +0800 | [diff] [blame] | 8509 | reserved_space, (ret != 0)); |
Chris Mason | 9998eb7 | 2012-01-25 13:47:40 -0500 | [diff] [blame] | 8510 | out_noreserve: |
Jan Kara | b2b5ef5 | 2012-06-12 16:20:45 +0200 | [diff] [blame] | 8511 | sb_end_pagefault(inode->i_sb); |
Qu Wenruo | 364ecf3 | 2017-02-27 15:10:38 +0800 | [diff] [blame] | 8512 | extent_changeset_free(data_reserved); |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8513 | return ret; |
| 8514 | } |
| 8515 | |
Filipe Manana | 213e8c5 | 2018-02-06 20:40:31 +0000 | [diff] [blame] | 8516 | static int btrfs_truncate(struct inode *inode, bool skip_writeback) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8517 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8518 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8519 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 8520 | struct btrfs_block_rsv *rsv; |
Omar Sandoval | ad7e1a7 | 2018-05-22 09:59:50 -0700 | [diff] [blame] | 8521 | int ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8522 | struct btrfs_trans_handle *trans; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8523 | u64 mask = fs_info->sectorsize - 1; |
Josef Bacik | 2bd36e7 | 2019-08-22 15:14:33 -0400 | [diff] [blame] | 8524 | u64 min_size = btrfs_calc_metadata_size(fs_info, 1); |
Filipe Manana | 0d7d316 | 2021-05-24 11:35:55 +0100 | [diff] [blame] | 8525 | u64 extents_found = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8526 | |
Filipe Manana | 213e8c5 | 2018-02-06 20:40:31 +0000 | [diff] [blame] | 8527 | if (!skip_writeback) { |
| 8528 | ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask), |
| 8529 | (u64)-1); |
| 8530 | if (ret) |
| 8531 | return ret; |
| 8532 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8533 | |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 8534 | /* |
Omar Sandoval | f7e9e8f | 2018-05-11 13:13:32 -0700 | [diff] [blame] | 8535 | * Yes ladies and gentlemen, this is indeed ugly. We have a couple of |
| 8536 | * things going on here: |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 8537 | * |
Omar Sandoval | f7e9e8f | 2018-05-11 13:13:32 -0700 | [diff] [blame] | 8538 | * 1) We need to reserve space to update our inode. |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 8539 | * |
Omar Sandoval | f7e9e8f | 2018-05-11 13:13:32 -0700 | [diff] [blame] | 8540 | * 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] | 8541 | * be free'd up by the truncate operation, but also have some slack |
| 8542 | * space reserved in case it uses space during the truncate (thank you |
| 8543 | * very much snapshotting). |
| 8544 | * |
Omar Sandoval | f7e9e8f | 2018-05-11 13:13:32 -0700 | [diff] [blame] | 8545 | * 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] | 8546 | * 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] | 8547 | * 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] | 8548 | * doesn't end up using space reserved for updating the inode. We also |
| 8549 | * need to be able to stop the transaction and start a new one, which |
| 8550 | * means we need to be able to update the inode several times, and we |
| 8551 | * have no idea of knowing how many times that will be, so we can't just |
| 8552 | * reserve 1 item for the entirety of the operation, so that has to be |
| 8553 | * done separately as well. |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 8554 | * |
| 8555 | * So that leaves us with |
| 8556 | * |
Omar Sandoval | f7e9e8f | 2018-05-11 13:13:32 -0700 | [diff] [blame] | 8557 | * 1) rsv - for the truncate reservation, which we will steal from the |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 8558 | * transaction reservation. |
Omar Sandoval | f7e9e8f | 2018-05-11 13:13:32 -0700 | [diff] [blame] | 8559 | * 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] | 8560 | * updating the inode. |
| 8561 | */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 8562 | rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP); |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 8563 | if (!rsv) |
| 8564 | return -ENOMEM; |
Josef Bacik | 4a33854 | 2011-08-29 11:01:31 -0400 | [diff] [blame] | 8565 | rsv->size = min_size; |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 8566 | rsv->failfast = 1; |
Josef Bacik | f0cd846 | 2011-03-04 14:37:08 -0500 | [diff] [blame] | 8567 | |
Josef Bacik | 907cbce | 2011-08-08 13:46:15 -0400 | [diff] [blame] | 8568 | /* |
Josef Bacik | 0712718 | 2011-08-19 10:29:59 -0400 | [diff] [blame] | 8569 | * 1 for the truncate slack space |
Josef Bacik | 907cbce | 2011-08-08 13:46:15 -0400 | [diff] [blame] | 8570 | * 1 for updating the inode. |
| 8571 | */ |
Josef Bacik | f3fe820 | 2013-01-07 17:03:21 -0500 | [diff] [blame] | 8572 | trans = btrfs_start_transaction(root, 2); |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 8573 | if (IS_ERR(trans)) { |
Omar Sandoval | ad7e1a7 | 2018-05-22 09:59:50 -0700 | [diff] [blame] | 8574 | ret = PTR_ERR(trans); |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 8575 | goto out; |
| 8576 | } |
Josef Bacik | f0cd846 | 2011-03-04 14:37:08 -0500 | [diff] [blame] | 8577 | |
Josef Bacik | 907cbce | 2011-08-08 13:46:15 -0400 | [diff] [blame] | 8578 | /* Migrate the slack space for the truncate to our reserve */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8579 | ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, rsv, |
Lu Fengqi | 3a58417 | 2018-08-04 21:10:55 +0800 | [diff] [blame] | 8580 | min_size, false); |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 8581 | BUG_ON(ret); |
Josef Bacik | f0cd846 | 2011-03-04 14:37:08 -0500 | [diff] [blame] | 8582 | |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 8583 | trans->block_rsv = rsv; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 8584 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 8585 | while (1) { |
Josef Bacik | 9a4a142 | 2021-12-03 17:18:05 -0500 | [diff] [blame^] | 8586 | struct extent_state *cached_state = NULL; |
| 8587 | const u64 new_size = inode->i_size; |
| 8588 | const u64 lock_start = ALIGN_DOWN(new_size, fs_info->sectorsize); |
| 8589 | |
| 8590 | lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, (u64)-1, |
| 8591 | &cached_state); |
| 8592 | /* |
| 8593 | * We want to drop from the next block forward in case this new |
| 8594 | * size is not block aligned since we will be keeping the last |
| 8595 | * block of the extent just the way it is. |
| 8596 | */ |
| 8597 | btrfs_drop_extent_cache(BTRFS_I(inode), |
| 8598 | ALIGN(new_size, fs_info->sectorsize), |
| 8599 | (u64)-1, 0); |
| 8600 | |
Nikolay Borisov | 5074339 | 2020-11-02 16:48:55 +0200 | [diff] [blame] | 8601 | ret = btrfs_truncate_inode_items(trans, root, BTRFS_I(inode), |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 8602 | inode->i_size, |
Filipe Manana | 0d7d316 | 2021-05-24 11:35:55 +0100 | [diff] [blame] | 8603 | BTRFS_EXTENT_DATA_KEY, |
| 8604 | &extents_found); |
Josef Bacik | 9a4a142 | 2021-12-03 17:18:05 -0500 | [diff] [blame^] | 8605 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, |
| 8606 | (u64)-1, &cached_state); |
| 8607 | |
Josef Bacik | ddfae63 | 2017-10-19 14:16:02 -0400 | [diff] [blame] | 8608 | trans->block_rsv = &fs_info->trans_block_rsv; |
Omar Sandoval | ad7e1a7 | 2018-05-22 09:59:50 -0700 | [diff] [blame] | 8609 | if (ret != -ENOSPC && ret != -EAGAIN) |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 8610 | break; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8611 | |
Nikolay Borisov | 9a56fcd | 2020-11-02 16:48:59 +0200 | [diff] [blame] | 8612 | ret = btrfs_update_inode(trans, root, BTRFS_I(inode)); |
Omar Sandoval | ad7e1a7 | 2018-05-22 09:59:50 -0700 | [diff] [blame] | 8613 | if (ret) |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 8614 | break; |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 8615 | |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 8616 | btrfs_end_transaction(trans); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 8617 | btrfs_btree_balance_dirty(fs_info); |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 8618 | |
| 8619 | trans = btrfs_start_transaction(root, 2); |
| 8620 | if (IS_ERR(trans)) { |
Omar Sandoval | ad7e1a7 | 2018-05-22 09:59:50 -0700 | [diff] [blame] | 8621 | ret = PTR_ERR(trans); |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 8622 | trans = NULL; |
| 8623 | break; |
| 8624 | } |
| 8625 | |
Nikolay Borisov | 63f018b | 2020-03-10 10:59:31 +0200 | [diff] [blame] | 8626 | btrfs_block_rsv_release(fs_info, rsv, -1, NULL); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8627 | ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, |
Lu Fengqi | 3a58417 | 2018-08-04 21:10:55 +0800 | [diff] [blame] | 8628 | rsv, min_size, false); |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 8629 | BUG_ON(ret); /* shouldn't happen */ |
| 8630 | trans->block_rsv = rsv; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 8631 | } |
| 8632 | |
Josef Bacik | ddfae63 | 2017-10-19 14:16:02 -0400 | [diff] [blame] | 8633 | /* |
| 8634 | * We can't call btrfs_truncate_block inside a trans handle as we could |
Josef Bacik | 54f03ab | 2021-12-03 17:18:04 -0500 | [diff] [blame] | 8635 | * deadlock with freeze, if we got BTRFS_NEED_TRUNCATE_BLOCK then we |
| 8636 | * know we've truncated everything except the last little bit, and can |
| 8637 | * do btrfs_truncate_block and then update the disk_i_size. |
Josef Bacik | ddfae63 | 2017-10-19 14:16:02 -0400 | [diff] [blame] | 8638 | */ |
Josef Bacik | 54f03ab | 2021-12-03 17:18:04 -0500 | [diff] [blame] | 8639 | if (ret == BTRFS_NEED_TRUNCATE_BLOCK) { |
Josef Bacik | ddfae63 | 2017-10-19 14:16:02 -0400 | [diff] [blame] | 8640 | btrfs_end_transaction(trans); |
| 8641 | btrfs_btree_balance_dirty(fs_info); |
| 8642 | |
Nikolay Borisov | 217f42e | 2020-11-02 16:49:03 +0200 | [diff] [blame] | 8643 | ret = btrfs_truncate_block(BTRFS_I(inode), inode->i_size, 0, 0); |
Josef Bacik | ddfae63 | 2017-10-19 14:16:02 -0400 | [diff] [blame] | 8644 | if (ret) |
| 8645 | goto out; |
| 8646 | trans = btrfs_start_transaction(root, 1); |
| 8647 | if (IS_ERR(trans)) { |
| 8648 | ret = PTR_ERR(trans); |
| 8649 | goto out; |
| 8650 | } |
Nikolay Borisov | 76aea53 | 2020-11-02 16:48:53 +0200 | [diff] [blame] | 8651 | btrfs_inode_safe_disk_i_size_write(BTRFS_I(inode), 0); |
Josef Bacik | ddfae63 | 2017-10-19 14:16:02 -0400 | [diff] [blame] | 8652 | } |
| 8653 | |
Chris Mason | 917c16b | 2011-11-08 14:49:59 -0500 | [diff] [blame] | 8654 | if (trans) { |
Omar Sandoval | ad7e1a7 | 2018-05-22 09:59:50 -0700 | [diff] [blame] | 8655 | int ret2; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 8656 | |
Omar Sandoval | ad7e1a7 | 2018-05-22 09:59:50 -0700 | [diff] [blame] | 8657 | trans->block_rsv = &fs_info->trans_block_rsv; |
Nikolay Borisov | 9a56fcd | 2020-11-02 16:48:59 +0200 | [diff] [blame] | 8658 | ret2 = btrfs_update_inode(trans, root, BTRFS_I(inode)); |
Omar Sandoval | ad7e1a7 | 2018-05-22 09:59:50 -0700 | [diff] [blame] | 8659 | if (ret2 && !ret) |
| 8660 | ret = ret2; |
| 8661 | |
| 8662 | ret2 = btrfs_end_transaction(trans); |
| 8663 | if (ret2 && !ret) |
| 8664 | ret = ret2; |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 8665 | btrfs_btree_balance_dirty(fs_info); |
Chris Mason | 917c16b | 2011-11-08 14:49:59 -0500 | [diff] [blame] | 8666 | } |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 8667 | out: |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 8668 | btrfs_free_block_rsv(fs_info, rsv); |
Filipe Manana | 0d7d316 | 2021-05-24 11:35:55 +0100 | [diff] [blame] | 8669 | /* |
| 8670 | * So if we truncate and then write and fsync we normally would just |
| 8671 | * write the extents that changed, which is a problem if we need to |
| 8672 | * first truncate that entire inode. So set this flag so we write out |
| 8673 | * all of the extents in the inode to the sync log so we're completely |
| 8674 | * safe. |
| 8675 | * |
| 8676 | * If no extents were dropped or trimmed we don't need to force the next |
| 8677 | * fsync to truncate all the inode's items from the log and re-log them |
| 8678 | * all. This means the truncate operation did not change the file size, |
| 8679 | * or changed it to a smaller size but there was only an implicit hole |
| 8680 | * between the old i_size and the new i_size, and there were no prealloc |
| 8681 | * extents beyond i_size to drop. |
| 8682 | */ |
| 8683 | if (extents_found > 0) |
| 8684 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags); |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 8685 | |
Omar Sandoval | ad7e1a7 | 2018-05-22 09:59:50 -0700 | [diff] [blame] | 8686 | return ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8687 | } |
| 8688 | |
Sven Wegener | 3b96362 | 2008-06-09 21:57:42 -0400 | [diff] [blame] | 8689 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 8690 | * create a new subvolume directory/inode (helper for the ioctl). |
| 8691 | */ |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 8692 | int btrfs_create_subvol_root(struct btrfs_trans_handle *trans, |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 8693 | struct btrfs_root *new_root, |
Christian Brauner | 4d4340c | 2021-07-27 12:48:52 +0200 | [diff] [blame] | 8694 | struct btrfs_root *parent_root, |
| 8695 | struct user_namespace *mnt_userns) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8696 | { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8697 | struct inode *inode; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 8698 | int err; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 8699 | u64 index = 0; |
Nikolay Borisov | 2312510 | 2020-12-07 17:32:36 +0200 | [diff] [blame] | 8700 | u64 ino; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8701 | |
Nikolay Borisov | 2312510 | 2020-12-07 17:32:36 +0200 | [diff] [blame] | 8702 | err = btrfs_get_free_objectid(new_root, &ino); |
| 8703 | if (err < 0) |
| 8704 | return err; |
| 8705 | |
Christian Brauner | 4d4340c | 2021-07-27 12:48:52 +0200 | [diff] [blame] | 8706 | inode = btrfs_new_inode(trans, new_root, mnt_userns, NULL, "..", 2, |
Christian Brauner | b3b6f5b | 2021-07-27 12:48:41 +0200 | [diff] [blame] | 8707 | ino, ino, |
Florian Albrechtskirchinger | 12fc9d0 | 2012-02-10 22:15:54 +0100 | [diff] [blame] | 8708 | S_IFDIR | (~current_umask() & S_IRWXUGO), |
| 8709 | &index); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 8710 | if (IS_ERR(inode)) |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 8711 | return PTR_ERR(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8712 | inode->i_op = &btrfs_dir_inode_operations; |
| 8713 | inode->i_fop = &btrfs_dir_file_operations; |
| 8714 | |
Miklos Szeredi | bfe8684 | 2011-10-28 14:13:29 +0200 | [diff] [blame] | 8715 | set_nlink(inode, 1); |
Nikolay Borisov | 6ef06d2 | 2017-02-20 13:50:34 +0200 | [diff] [blame] | 8716 | btrfs_i_size_write(BTRFS_I(inode), 0); |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 8717 | unlock_new_inode(inode); |
Sven Wegener | 3b96362 | 2008-06-09 21:57:42 -0400 | [diff] [blame] | 8718 | |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 8719 | err = btrfs_subvol_inherit_props(trans, new_root, parent_root); |
| 8720 | if (err) |
| 8721 | btrfs_err(new_root->fs_info, |
David Sterba | 351fd35 | 2014-05-15 16:48:20 +0200 | [diff] [blame] | 8722 | "error inheriting subvolume %llu properties: %d", |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 8723 | new_root->root_key.objectid, err); |
| 8724 | |
Nikolay Borisov | 9a56fcd | 2020-11-02 16:48:59 +0200 | [diff] [blame] | 8725 | err = btrfs_update_inode(trans, new_root, BTRFS_I(inode)); |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 8726 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 8727 | iput(inode); |
Mark Fasheh | ce59897 | 2011-07-26 11:32:23 -0700 | [diff] [blame] | 8728 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8729 | } |
| 8730 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8731 | struct inode *btrfs_alloc_inode(struct super_block *sb) |
| 8732 | { |
Josef Bacik | 69fe2d7 | 2017-10-19 14:15:57 -0400 | [diff] [blame] | 8733 | struct btrfs_fs_info *fs_info = btrfs_sb(sb); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8734 | struct btrfs_inode *ei; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8735 | struct inode *inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8736 | |
David Sterba | 712e36c | 2017-10-31 17:08:27 +0100 | [diff] [blame] | 8737 | ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_KERNEL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8738 | if (!ei) |
| 8739 | return NULL; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8740 | |
| 8741 | ei->root = NULL; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8742 | ei->generation = 0; |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 8743 | ei->last_trans = 0; |
Chris Mason | 257c62e | 2009-10-13 13:21:08 -0400 | [diff] [blame] | 8744 | ei->last_sub_trans = 0; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 8745 | ei->logged_trans = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8746 | ei->delalloc_bytes = 0; |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 8747 | ei->new_delalloc_bytes = 0; |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 8748 | ei->defrag_bytes = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8749 | ei->disk_i_size = 0; |
| 8750 | ei->flags = 0; |
Boris Burkov | 77eea05 | 2021-06-30 13:01:48 -0700 | [diff] [blame] | 8751 | ei->ro_flags = 0; |
Josef Bacik | 7709cde | 2011-08-04 10:25:02 -0400 | [diff] [blame] | 8752 | ei->csum_bytes = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8753 | ei->index_cnt = (u64)-1; |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 8754 | ei->dir_index = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8755 | ei->last_unlink_trans = 0; |
Filipe Manana | 3ebac17 | 2020-07-15 12:30:43 +0100 | [diff] [blame] | 8756 | ei->last_reflink_trans = 0; |
Liu Bo | 46d8bc3 | 2012-08-29 01:07:55 -0600 | [diff] [blame] | 8757 | ei->last_log_commit = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8758 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 8759 | spin_lock_init(&ei->lock); |
| 8760 | ei->outstanding_extents = 0; |
Josef Bacik | 69fe2d7 | 2017-10-19 14:15:57 -0400 | [diff] [blame] | 8761 | if (sb->s_magic != BTRFS_TEST_MAGIC) |
| 8762 | btrfs_init_metadata_block_rsv(fs_info, &ei->block_rsv, |
| 8763 | BTRFS_BLOCK_RSV_DELALLOC); |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 8764 | ei->runtime_flags = 0; |
David Sterba | b52aa8c | 2017-07-17 19:17:20 +0200 | [diff] [blame] | 8765 | ei->prop_compress = BTRFS_COMPRESS_NONE; |
David Sterba | eec63c6 | 2017-07-17 19:41:31 +0200 | [diff] [blame] | 8766 | ei->defrag_compress = BTRFS_COMPRESS_NONE; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8767 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 8768 | ei->delayed_node = NULL; |
| 8769 | |
chandan r | 9cc97d6 | 2012-07-04 12:48:07 +0530 | [diff] [blame] | 8770 | ei->i_otime.tv_sec = 0; |
| 8771 | ei->i_otime.tv_nsec = 0; |
| 8772 | |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8773 | inode = &ei->vfs_inode; |
David Sterba | a8067e0 | 2011-04-21 00:34:43 +0200 | [diff] [blame] | 8774 | extent_map_tree_init(&ei->extent_tree); |
Qu Wenruo | 43eb5f2 | 2019-03-01 10:47:59 +0800 | [diff] [blame] | 8775 | extent_io_tree_init(fs_info, &ei->io_tree, IO_TREE_INODE_IO, inode); |
| 8776 | extent_io_tree_init(fs_info, &ei->io_failure_tree, |
| 8777 | IO_TREE_INODE_IO_FAILURE, inode); |
Josef Bacik | 41a2ee7 | 2020-01-17 09:02:21 -0500 | [diff] [blame] | 8778 | extent_io_tree_init(fs_info, &ei->file_extent_tree, |
| 8779 | IO_TREE_INODE_FILE_EXTENT, inode); |
David Sterba | 7b43973 | 2019-03-11 15:58:30 +0100 | [diff] [blame] | 8780 | ei->io_tree.track_uptodate = true; |
| 8781 | ei->io_failure_tree.track_uptodate = true; |
Josef Bacik | b812ce2 | 2012-11-16 13:56:32 -0500 | [diff] [blame] | 8782 | atomic_set(&ei->sync_writers, 0); |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8783 | mutex_init(&ei->log_mutex); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8784 | btrfs_ordered_inode_tree_init(&ei->ordered_tree); |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8785 | INIT_LIST_HEAD(&ei->delalloc_inodes); |
David Sterba | 8089fe6 | 2015-11-19 14:15:51 +0100 | [diff] [blame] | 8786 | INIT_LIST_HEAD(&ei->delayed_iput); |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8787 | RB_CLEAR_NODE(&ei->rb_node); |
Josef Bacik | 8318ba7 | 2021-02-10 17:14:33 -0500 | [diff] [blame] | 8788 | init_rwsem(&ei->i_mmap_lock); |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8789 | |
| 8790 | return inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8791 | } |
| 8792 | |
Josef Bacik | aaedb55 | 2013-10-11 14:44:09 -0400 | [diff] [blame] | 8793 | #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS |
| 8794 | void btrfs_test_destroy_inode(struct inode *inode) |
| 8795 | { |
Nikolay Borisov | dcdbc05 | 2017-02-20 13:50:45 +0200 | [diff] [blame] | 8796 | btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0); |
Josef Bacik | aaedb55 | 2013-10-11 14:44:09 -0400 | [diff] [blame] | 8797 | kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode)); |
| 8798 | } |
| 8799 | #endif |
| 8800 | |
Al Viro | 26602ca | 2019-04-10 15:14:41 -0400 | [diff] [blame] | 8801 | void btrfs_free_inode(struct inode *inode) |
Nick Piggin | fa0d7e3d | 2011-01-07 17:49:49 +1100 | [diff] [blame] | 8802 | { |
Nick Piggin | fa0d7e3d | 2011-01-07 17:49:49 +1100 | [diff] [blame] | 8803 | kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode)); |
| 8804 | } |
| 8805 | |
Nikolay Borisov | 633cc81 | 2020-09-18 12:15:49 +0300 | [diff] [blame] | 8806 | void btrfs_destroy_inode(struct inode *vfs_inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8807 | { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8808 | struct btrfs_ordered_extent *ordered; |
Nikolay Borisov | 633cc81 | 2020-09-18 12:15:49 +0300 | [diff] [blame] | 8809 | struct btrfs_inode *inode = BTRFS_I(vfs_inode); |
| 8810 | struct btrfs_root *root = inode->root; |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 8811 | |
Nikolay Borisov | 633cc81 | 2020-09-18 12:15:49 +0300 | [diff] [blame] | 8812 | WARN_ON(!hlist_empty(&vfs_inode->i_dentry)); |
| 8813 | WARN_ON(vfs_inode->i_data.nrpages); |
| 8814 | WARN_ON(inode->block_rsv.reserved); |
| 8815 | WARN_ON(inode->block_rsv.size); |
| 8816 | WARN_ON(inode->outstanding_extents); |
Filipe Manana | dc28722 | 2021-09-16 11:32:14 +0100 | [diff] [blame] | 8817 | if (!S_ISDIR(vfs_inode->i_mode)) { |
| 8818 | WARN_ON(inode->delalloc_bytes); |
| 8819 | WARN_ON(inode->new_delalloc_bytes); |
| 8820 | } |
Nikolay Borisov | 633cc81 | 2020-09-18 12:15:49 +0300 | [diff] [blame] | 8821 | WARN_ON(inode->csum_bytes); |
| 8822 | WARN_ON(inode->defrag_bytes); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8823 | |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 8824 | /* |
Josef Bacik | a6dbd42 | 2009-11-11 15:53:34 -0500 | [diff] [blame] | 8825 | * This can happen where we create an inode, but somebody else also |
| 8826 | * created the same inode and we need to destroy the one we already |
| 8827 | * created. |
| 8828 | */ |
| 8829 | if (!root) |
Al Viro | 26602ca | 2019-04-10 15:14:41 -0400 | [diff] [blame] | 8830 | return; |
Josef Bacik | a6dbd42 | 2009-11-11 15:53:34 -0500 | [diff] [blame] | 8831 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 8832 | while (1) { |
Nikolay Borisov | 633cc81 | 2020-09-18 12:15:49 +0300 | [diff] [blame] | 8833 | ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8834 | if (!ordered) |
| 8835 | break; |
| 8836 | else { |
Nikolay Borisov | 633cc81 | 2020-09-18 12:15:49 +0300 | [diff] [blame] | 8837 | btrfs_err(root->fs_info, |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 8838 | "found ordered extent %llu %llu on inode cleanup", |
Omar Sandoval | bffe633 | 2019-12-02 17:34:19 -0800 | [diff] [blame] | 8839 | ordered->file_offset, ordered->num_bytes); |
Nikolay Borisov | 71fe0a5 | 2020-09-18 12:15:50 +0300 | [diff] [blame] | 8840 | btrfs_remove_ordered_extent(inode, ordered); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8841 | btrfs_put_ordered_extent(ordered); |
| 8842 | btrfs_put_ordered_extent(ordered); |
| 8843 | } |
| 8844 | } |
Nikolay Borisov | 633cc81 | 2020-09-18 12:15:49 +0300 | [diff] [blame] | 8845 | btrfs_qgroup_check_reserved_leak(inode); |
| 8846 | inode_tree_del(inode); |
| 8847 | btrfs_drop_extent_cache(inode, 0, (u64)-1, 0); |
| 8848 | btrfs_inode_clear_file_extent_range(inode, 0, (u64)-1); |
| 8849 | btrfs_put_root(inode->root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8850 | } |
| 8851 | |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 8852 | int btrfs_drop_inode(struct inode *inode) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 8853 | { |
| 8854 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 8855 | |
Naohiro Aota | 6379ef9 | 2013-06-06 09:56:34 +0000 | [diff] [blame] | 8856 | if (root == NULL) |
| 8857 | return 1; |
| 8858 | |
Liu Bo | fa6ac87 | 2013-02-20 14:10:23 +0000 | [diff] [blame] | 8859 | /* the snap/subvol tree is on deleting */ |
Stefan Behrens | 69e9c6c | 2013-09-05 16:58:43 +0200 | [diff] [blame] | 8860 | if (btrfs_root_refs(&root->root_item) == 0) |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 8861 | return 1; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 8862 | else |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 8863 | return generic_drop_inode(inode); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 8864 | } |
| 8865 | |
Sven Wegener | 0ee0fda | 2008-07-30 16:54:26 -0400 | [diff] [blame] | 8866 | static void init_once(void *foo) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8867 | { |
| 8868 | struct btrfs_inode *ei = (struct btrfs_inode *) foo; |
| 8869 | |
| 8870 | inode_init_once(&ei->vfs_inode); |
| 8871 | } |
| 8872 | |
David Sterba | e67c718 | 2018-02-19 17:24:18 +0100 | [diff] [blame] | 8873 | void __cold btrfs_destroy_cachep(void) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8874 | { |
Kirill A. Shutemov | 8c0a853 | 2012-09-26 11:33:07 +1000 | [diff] [blame] | 8875 | /* |
| 8876 | * Make sure all delayed rcu free inodes are flushed before we |
| 8877 | * destroy cache. |
| 8878 | */ |
| 8879 | rcu_barrier(); |
Kinglong Mee | 5598e90 | 2016-01-29 21:36:35 +0800 | [diff] [blame] | 8880 | kmem_cache_destroy(btrfs_inode_cachep); |
| 8881 | kmem_cache_destroy(btrfs_trans_handle_cachep); |
Kinglong Mee | 5598e90 | 2016-01-29 21:36:35 +0800 | [diff] [blame] | 8882 | kmem_cache_destroy(btrfs_path_cachep); |
| 8883 | kmem_cache_destroy(btrfs_free_space_cachep); |
Christophe Leroy | 3acd485 | 2019-08-21 15:05:55 +0000 | [diff] [blame] | 8884 | kmem_cache_destroy(btrfs_free_space_bitmap_cachep); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8885 | } |
| 8886 | |
Liu Bo | f5c29bd | 2017-11-02 17:21:50 -0600 | [diff] [blame] | 8887 | int __init btrfs_init_cachep(void) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8888 | { |
David Sterba | 837e197 | 2012-09-07 03:00:48 -0600 | [diff] [blame] | 8889 | btrfs_inode_cachep = kmem_cache_create("btrfs_inode", |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 8890 | sizeof(struct btrfs_inode), 0, |
Vladimir Davydov | 5d09705 | 2016-01-14 15:18:21 -0800 | [diff] [blame] | 8891 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT, |
| 8892 | init_once); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8893 | if (!btrfs_inode_cachep) |
| 8894 | goto fail; |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 8895 | |
David Sterba | 837e197 | 2012-09-07 03:00:48 -0600 | [diff] [blame] | 8896 | btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle", |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 8897 | sizeof(struct btrfs_trans_handle), 0, |
Nikolay Borisov | fba4b69 | 2016-06-23 21:17:08 +0300 | [diff] [blame] | 8898 | SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8899 | if (!btrfs_trans_handle_cachep) |
| 8900 | goto fail; |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 8901 | |
David Sterba | 837e197 | 2012-09-07 03:00:48 -0600 | [diff] [blame] | 8902 | btrfs_path_cachep = kmem_cache_create("btrfs_path", |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 8903 | sizeof(struct btrfs_path), 0, |
Nikolay Borisov | fba4b69 | 2016-06-23 21:17:08 +0300 | [diff] [blame] | 8904 | SLAB_MEM_SPREAD, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8905 | if (!btrfs_path_cachep) |
| 8906 | goto fail; |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 8907 | |
David Sterba | 837e197 | 2012-09-07 03:00:48 -0600 | [diff] [blame] | 8908 | btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space", |
Josef Bacik | dc89e98 | 2011-01-28 17:05:48 -0500 | [diff] [blame] | 8909 | sizeof(struct btrfs_free_space), 0, |
Nikolay Borisov | fba4b69 | 2016-06-23 21:17:08 +0300 | [diff] [blame] | 8910 | SLAB_MEM_SPREAD, NULL); |
Josef Bacik | dc89e98 | 2011-01-28 17:05:48 -0500 | [diff] [blame] | 8911 | if (!btrfs_free_space_cachep) |
| 8912 | goto fail; |
| 8913 | |
Christophe Leroy | 3acd485 | 2019-08-21 15:05:55 +0000 | [diff] [blame] | 8914 | btrfs_free_space_bitmap_cachep = kmem_cache_create("btrfs_free_space_bitmap", |
| 8915 | PAGE_SIZE, PAGE_SIZE, |
David Sterba | 34e4999 | 2021-03-15 15:18:24 +0100 | [diff] [blame] | 8916 | SLAB_MEM_SPREAD, NULL); |
Christophe Leroy | 3acd485 | 2019-08-21 15:05:55 +0000 | [diff] [blame] | 8917 | if (!btrfs_free_space_bitmap_cachep) |
| 8918 | goto fail; |
| 8919 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8920 | return 0; |
| 8921 | fail: |
| 8922 | btrfs_destroy_cachep(); |
| 8923 | return -ENOMEM; |
| 8924 | } |
| 8925 | |
Christian Brauner | 549c729 | 2021-01-21 14:19:43 +0100 | [diff] [blame] | 8926 | static int btrfs_getattr(struct user_namespace *mnt_userns, |
| 8927 | const struct path *path, struct kstat *stat, |
David Howells | a528d35 | 2017-01-31 16:46:22 +0000 | [diff] [blame] | 8928 | u32 request_mask, unsigned int flags) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8929 | { |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 8930 | u64 delalloc_bytes; |
Filipe Manana | 2766ff6 | 2020-11-04 11:07:34 +0000 | [diff] [blame] | 8931 | u64 inode_bytes; |
David Howells | a528d35 | 2017-01-31 16:46:22 +0000 | [diff] [blame] | 8932 | struct inode *inode = d_inode(path->dentry); |
David Sterba | fadc0d8 | 2011-11-20 07:33:38 -0500 | [diff] [blame] | 8933 | u32 blocksize = inode->i_sb->s_blocksize; |
Yonghong Song | 04a87e3 | 2017-05-12 15:07:43 -0700 | [diff] [blame] | 8934 | u32 bi_flags = BTRFS_I(inode)->flags; |
Boris Burkov | 1460540 | 2021-06-30 13:01:49 -0700 | [diff] [blame] | 8935 | u32 bi_ro_flags = BTRFS_I(inode)->ro_flags; |
Yonghong Song | 04a87e3 | 2017-05-12 15:07:43 -0700 | [diff] [blame] | 8936 | |
| 8937 | stat->result_mask |= STATX_BTIME; |
| 8938 | stat->btime.tv_sec = BTRFS_I(inode)->i_otime.tv_sec; |
| 8939 | stat->btime.tv_nsec = BTRFS_I(inode)->i_otime.tv_nsec; |
| 8940 | if (bi_flags & BTRFS_INODE_APPEND) |
| 8941 | stat->attributes |= STATX_ATTR_APPEND; |
| 8942 | if (bi_flags & BTRFS_INODE_COMPRESS) |
| 8943 | stat->attributes |= STATX_ATTR_COMPRESSED; |
| 8944 | if (bi_flags & BTRFS_INODE_IMMUTABLE) |
| 8945 | stat->attributes |= STATX_ATTR_IMMUTABLE; |
| 8946 | if (bi_flags & BTRFS_INODE_NODUMP) |
| 8947 | stat->attributes |= STATX_ATTR_NODUMP; |
Boris Burkov | 1460540 | 2021-06-30 13:01:49 -0700 | [diff] [blame] | 8948 | if (bi_ro_flags & BTRFS_INODE_RO_VERITY) |
| 8949 | stat->attributes |= STATX_ATTR_VERITY; |
Yonghong Song | 04a87e3 | 2017-05-12 15:07:43 -0700 | [diff] [blame] | 8950 | |
| 8951 | stat->attributes_mask |= (STATX_ATTR_APPEND | |
| 8952 | STATX_ATTR_COMPRESSED | |
| 8953 | STATX_ATTR_IMMUTABLE | |
| 8954 | STATX_ATTR_NODUMP); |
David Sterba | fadc0d8 | 2011-11-20 07:33:38 -0500 | [diff] [blame] | 8955 | |
Christian Brauner | c020d2e | 2021-07-27 12:48:43 +0200 | [diff] [blame] | 8956 | generic_fillattr(mnt_userns, inode, stat); |
Al Viro | 0ee5dc6 | 2011-07-07 15:44:25 -0400 | [diff] [blame] | 8957 | stat->dev = BTRFS_I(inode)->root->anon_dev; |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 8958 | |
| 8959 | spin_lock(&BTRFS_I(inode)->lock); |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 8960 | delalloc_bytes = BTRFS_I(inode)->new_delalloc_bytes; |
Filipe Manana | 2766ff6 | 2020-11-04 11:07:34 +0000 | [diff] [blame] | 8961 | inode_bytes = inode_get_bytes(inode); |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 8962 | spin_unlock(&BTRFS_I(inode)->lock); |
Filipe Manana | 2766ff6 | 2020-11-04 11:07:34 +0000 | [diff] [blame] | 8963 | stat->blocks = (ALIGN(inode_bytes, blocksize) + |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 8964 | ALIGN(delalloc_bytes, blocksize)) >> 9; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8965 | return 0; |
| 8966 | } |
| 8967 | |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 8968 | static int btrfs_rename_exchange(struct inode *old_dir, |
| 8969 | struct dentry *old_dentry, |
| 8970 | struct inode *new_dir, |
| 8971 | struct dentry *new_dentry) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8972 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8973 | struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8974 | struct btrfs_trans_handle *trans; |
| 8975 | struct btrfs_root *root = BTRFS_I(old_dir)->root; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 8976 | struct btrfs_root *dest = BTRFS_I(new_dir)->root; |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 8977 | struct inode *new_inode = new_dentry->d_inode; |
| 8978 | struct inode *old_inode = old_dentry->d_inode; |
Deepa Dinamani | 95582b0 | 2018-05-08 19:36:02 -0700 | [diff] [blame] | 8979 | struct timespec64 ctime = current_time(old_inode); |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 8980 | u64 old_ino = btrfs_ino(BTRFS_I(old_inode)); |
| 8981 | u64 new_ino = btrfs_ino(BTRFS_I(new_inode)); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 8982 | u64 old_idx = 0; |
| 8983 | u64 new_idx = 0; |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 8984 | int ret; |
Filipe Manana | 75b463d | 2020-08-11 12:43:48 +0100 | [diff] [blame] | 8985 | int ret2; |
Filipe Manana | 86e8aa0 | 2016-05-05 02:02:27 +0100 | [diff] [blame] | 8986 | bool root_log_pinned = false; |
| 8987 | bool dest_log_pinned = false; |
Josef Bacik | dc09ef3 | 2021-05-19 14:04:21 -0400 | [diff] [blame] | 8988 | bool need_abort = false; |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 8989 | |
NeilBrown | 3f79f6f | 2021-08-06 14:26:24 +1000 | [diff] [blame] | 8990 | /* |
| 8991 | * For non-subvolumes allow exchange only within one subvolume, in the |
| 8992 | * same inode namespace. Two subvolumes (represented as directory) can |
| 8993 | * be exchanged as they're a logical link and have a fixed inode number. |
| 8994 | */ |
| 8995 | if (root != dest && |
| 8996 | (old_ino != BTRFS_FIRST_FREE_OBJECTID || |
| 8997 | new_ino != BTRFS_FIRST_FREE_OBJECTID)) |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 8998 | return -EXDEV; |
| 8999 | |
| 9000 | /* close the race window with snapshot create/destroy ioctl */ |
Josef Bacik | 943eb3b | 2019-11-19 13:59:20 -0500 | [diff] [blame] | 9001 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID || |
| 9002 | new_ino == BTRFS_FIRST_FREE_OBJECTID) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9003 | down_read(&fs_info->subvol_sem); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9004 | |
| 9005 | /* |
| 9006 | * We want to reserve the absolute worst case amount of items. So if |
| 9007 | * both inodes are subvols and we need to unlink them then that would |
| 9008 | * require 4 item modifications, but if they are both normal inodes it |
| 9009 | * would require 5 item modifications, so we'll assume their normal |
| 9010 | * inodes. So 5 * 2 is 10, plus 2 for the new links, so 12 total items |
| 9011 | * should cover the worst case number of items we'll modify. |
| 9012 | */ |
| 9013 | trans = btrfs_start_transaction(root, 12); |
| 9014 | if (IS_ERR(trans)) { |
| 9015 | ret = PTR_ERR(trans); |
| 9016 | goto out_notrans; |
| 9017 | } |
| 9018 | |
Josef Bacik | 00aa8e8 | 2021-03-12 15:25:02 -0500 | [diff] [blame] | 9019 | if (dest != root) { |
| 9020 | ret = btrfs_record_root_in_trans(trans, dest); |
| 9021 | if (ret) |
| 9022 | goto out_fail; |
| 9023 | } |
Josef Bacik | 3e17409 | 2019-11-15 15:43:06 -0500 | [diff] [blame] | 9024 | |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9025 | /* |
| 9026 | * We need to find a free sequence number both in the source and |
| 9027 | * in the destination directory for the exchange. |
| 9028 | */ |
Nikolay Borisov | 877574e | 2017-02-20 13:50:33 +0200 | [diff] [blame] | 9029 | ret = btrfs_set_inode_index(BTRFS_I(new_dir), &old_idx); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9030 | if (ret) |
| 9031 | goto out_fail; |
Nikolay Borisov | 877574e | 2017-02-20 13:50:33 +0200 | [diff] [blame] | 9032 | ret = btrfs_set_inode_index(BTRFS_I(old_dir), &new_idx); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9033 | if (ret) |
| 9034 | goto out_fail; |
| 9035 | |
| 9036 | BTRFS_I(old_inode)->dir_index = 0ULL; |
| 9037 | BTRFS_I(new_inode)->dir_index = 0ULL; |
| 9038 | |
| 9039 | /* Reference for the source. */ |
| 9040 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) { |
| 9041 | /* force full log commit if subvolume involved. */ |
David Sterba | 9078776 | 2019-03-20 13:28:05 +0100 | [diff] [blame] | 9042 | btrfs_set_log_full_commit(trans); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9043 | } else { |
| 9044 | ret = btrfs_insert_inode_ref(trans, dest, |
| 9045 | new_dentry->d_name.name, |
| 9046 | new_dentry->d_name.len, |
| 9047 | old_ino, |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 9048 | btrfs_ino(BTRFS_I(new_dir)), |
| 9049 | old_idx); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9050 | if (ret) |
| 9051 | goto out_fail; |
Josef Bacik | dc09ef3 | 2021-05-19 14:04:21 -0400 | [diff] [blame] | 9052 | need_abort = true; |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9053 | } |
| 9054 | |
| 9055 | /* And now for the dest. */ |
| 9056 | if (new_ino == BTRFS_FIRST_FREE_OBJECTID) { |
| 9057 | /* force full log commit if subvolume involved. */ |
David Sterba | 9078776 | 2019-03-20 13:28:05 +0100 | [diff] [blame] | 9058 | btrfs_set_log_full_commit(trans); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9059 | } else { |
| 9060 | ret = btrfs_insert_inode_ref(trans, root, |
| 9061 | old_dentry->d_name.name, |
| 9062 | old_dentry->d_name.len, |
| 9063 | new_ino, |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 9064 | btrfs_ino(BTRFS_I(old_dir)), |
| 9065 | new_idx); |
Josef Bacik | dc09ef3 | 2021-05-19 14:04:21 -0400 | [diff] [blame] | 9066 | if (ret) { |
| 9067 | if (need_abort) |
| 9068 | btrfs_abort_transaction(trans, ret); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9069 | goto out_fail; |
Josef Bacik | dc09ef3 | 2021-05-19 14:04:21 -0400 | [diff] [blame] | 9070 | } |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9071 | } |
| 9072 | |
| 9073 | /* Update inode version and ctime/mtime. */ |
| 9074 | inode_inc_iversion(old_dir); |
| 9075 | inode_inc_iversion(new_dir); |
| 9076 | inode_inc_iversion(old_inode); |
| 9077 | inode_inc_iversion(new_inode); |
| 9078 | old_dir->i_ctime = old_dir->i_mtime = ctime; |
| 9079 | new_dir->i_ctime = new_dir->i_mtime = ctime; |
| 9080 | old_inode->i_ctime = ctime; |
| 9081 | new_inode->i_ctime = ctime; |
| 9082 | |
| 9083 | if (old_dentry->d_parent != new_dentry->d_parent) { |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 9084 | btrfs_record_unlink_dir(trans, BTRFS_I(old_dir), |
| 9085 | BTRFS_I(old_inode), 1); |
| 9086 | btrfs_record_unlink_dir(trans, BTRFS_I(new_dir), |
| 9087 | BTRFS_I(new_inode), 1); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9088 | } |
| 9089 | |
Filipe Manana | bd54f38 | 2021-07-27 11:24:45 +0100 | [diff] [blame] | 9090 | /* |
| 9091 | * Now pin the logs of the roots. We do it to ensure that no other task |
| 9092 | * can sync the logs while we are in progress with the rename, because |
| 9093 | * that could result in an inconsistency in case any of the inodes that |
| 9094 | * are part of this rename operation were logged before. |
| 9095 | * |
| 9096 | * We pin the logs even if at this precise moment none of the inodes was |
| 9097 | * logged before. This is because right after we checked for that, some |
| 9098 | * other task fsyncing some other inode not involved with this rename |
| 9099 | * operation could log that one of our inodes exists. |
| 9100 | * |
| 9101 | * We don't need to pin the logs before the above calls to |
| 9102 | * btrfs_insert_inode_ref(), since those don't ever need to change a log. |
| 9103 | */ |
| 9104 | if (old_ino != BTRFS_FIRST_FREE_OBJECTID) { |
| 9105 | btrfs_pin_log_trans(root); |
| 9106 | root_log_pinned = true; |
| 9107 | } |
| 9108 | if (new_ino != BTRFS_FIRST_FREE_OBJECTID) { |
| 9109 | btrfs_pin_log_trans(dest); |
| 9110 | dest_log_pinned = true; |
| 9111 | } |
| 9112 | |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9113 | /* src is a subvolume */ |
| 9114 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) { |
Josef Bacik | 045d396 | 2019-12-18 17:20:27 -0500 | [diff] [blame] | 9115 | ret = btrfs_unlink_subvol(trans, old_dir, old_dentry); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9116 | } else { /* src is an inode */ |
Filipe Manana | 4467af8 | 2021-10-25 17:31:50 +0100 | [diff] [blame] | 9117 | ret = __btrfs_unlink_inode(trans, BTRFS_I(old_dir), |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 9118 | BTRFS_I(old_dentry->d_inode), |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9119 | old_dentry->d_name.name, |
| 9120 | old_dentry->d_name.len); |
| 9121 | if (!ret) |
Nikolay Borisov | 9a56fcd | 2020-11-02 16:48:59 +0200 | [diff] [blame] | 9122 | ret = btrfs_update_inode(trans, root, BTRFS_I(old_inode)); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9123 | } |
| 9124 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 9125 | btrfs_abort_transaction(trans, ret); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9126 | goto out_fail; |
| 9127 | } |
| 9128 | |
| 9129 | /* dest is a subvolume */ |
| 9130 | if (new_ino == BTRFS_FIRST_FREE_OBJECTID) { |
Josef Bacik | 045d396 | 2019-12-18 17:20:27 -0500 | [diff] [blame] | 9131 | ret = btrfs_unlink_subvol(trans, new_dir, new_dentry); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9132 | } else { /* dest is an inode */ |
Filipe Manana | 4467af8 | 2021-10-25 17:31:50 +0100 | [diff] [blame] | 9133 | ret = __btrfs_unlink_inode(trans, BTRFS_I(new_dir), |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 9134 | BTRFS_I(new_dentry->d_inode), |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9135 | new_dentry->d_name.name, |
| 9136 | new_dentry->d_name.len); |
| 9137 | if (!ret) |
Nikolay Borisov | 9a56fcd | 2020-11-02 16:48:59 +0200 | [diff] [blame] | 9138 | ret = btrfs_update_inode(trans, dest, BTRFS_I(new_inode)); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9139 | } |
| 9140 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 9141 | btrfs_abort_transaction(trans, ret); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9142 | goto out_fail; |
| 9143 | } |
| 9144 | |
Nikolay Borisov | db0a669 | 2017-02-20 13:51:08 +0200 | [diff] [blame] | 9145 | 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] | 9146 | new_dentry->d_name.name, |
| 9147 | new_dentry->d_name.len, 0, old_idx); |
| 9148 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 9149 | btrfs_abort_transaction(trans, ret); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9150 | goto out_fail; |
| 9151 | } |
| 9152 | |
Nikolay Borisov | db0a669 | 2017-02-20 13:51:08 +0200 | [diff] [blame] | 9153 | 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] | 9154 | old_dentry->d_name.name, |
| 9155 | old_dentry->d_name.len, 0, new_idx); |
| 9156 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 9157 | btrfs_abort_transaction(trans, ret); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9158 | goto out_fail; |
| 9159 | } |
| 9160 | |
| 9161 | if (old_inode->i_nlink == 1) |
| 9162 | BTRFS_I(old_inode)->dir_index = old_idx; |
| 9163 | if (new_inode->i_nlink == 1) |
| 9164 | BTRFS_I(new_inode)->dir_index = new_idx; |
| 9165 | |
Filipe Manana | 86e8aa0 | 2016-05-05 02:02:27 +0100 | [diff] [blame] | 9166 | if (root_log_pinned) { |
Filipe Manana | 75b463d | 2020-08-11 12:43:48 +0100 | [diff] [blame] | 9167 | btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir), |
| 9168 | new_dentry->d_parent); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9169 | btrfs_end_log_trans(root); |
Filipe Manana | 86e8aa0 | 2016-05-05 02:02:27 +0100 | [diff] [blame] | 9170 | root_log_pinned = false; |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9171 | } |
Filipe Manana | 86e8aa0 | 2016-05-05 02:02:27 +0100 | [diff] [blame] | 9172 | if (dest_log_pinned) { |
Filipe Manana | 75b463d | 2020-08-11 12:43:48 +0100 | [diff] [blame] | 9173 | btrfs_log_new_name(trans, BTRFS_I(new_inode), BTRFS_I(new_dir), |
| 9174 | old_dentry->d_parent); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9175 | btrfs_end_log_trans(dest); |
Filipe Manana | 86e8aa0 | 2016-05-05 02:02:27 +0100 | [diff] [blame] | 9176 | dest_log_pinned = false; |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9177 | } |
| 9178 | out_fail: |
Filipe Manana | 86e8aa0 | 2016-05-05 02:02:27 +0100 | [diff] [blame] | 9179 | /* |
| 9180 | * If we have pinned a log and an error happened, we unpin tasks |
| 9181 | * trying to sync the log and force them to fallback to a transaction |
| 9182 | * commit if the log currently contains any of the inodes involved in |
| 9183 | * this rename operation (to ensure we do not persist a log with an |
| 9184 | * inconsistent state for any of these inodes or leading to any |
| 9185 | * inconsistencies when replayed). If the transaction was aborted, the |
| 9186 | * abortion reason is propagated to userspace when attempting to commit |
| 9187 | * the transaction. If the log does not contain any of these inodes, we |
| 9188 | * allow the tasks to sync it. |
| 9189 | */ |
| 9190 | if (ret && (root_log_pinned || dest_log_pinned)) { |
Nikolay Borisov | 0f8939b | 2017-01-18 00:31:30 +0200 | [diff] [blame] | 9191 | if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) || |
| 9192 | btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) || |
| 9193 | btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) || |
Filipe Manana | 1c167b8 | 2021-07-29 15:28:34 +0100 | [diff] [blame] | 9194 | btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)) |
David Sterba | 9078776 | 2019-03-20 13:28:05 +0100 | [diff] [blame] | 9195 | btrfs_set_log_full_commit(trans); |
Filipe Manana | 86e8aa0 | 2016-05-05 02:02:27 +0100 | [diff] [blame] | 9196 | |
| 9197 | if (root_log_pinned) { |
| 9198 | btrfs_end_log_trans(root); |
| 9199 | root_log_pinned = false; |
| 9200 | } |
| 9201 | if (dest_log_pinned) { |
| 9202 | btrfs_end_log_trans(dest); |
| 9203 | dest_log_pinned = false; |
| 9204 | } |
| 9205 | } |
Filipe Manana | 75b463d | 2020-08-11 12:43:48 +0100 | [diff] [blame] | 9206 | ret2 = btrfs_end_transaction(trans); |
| 9207 | ret = ret ? ret : ret2; |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9208 | out_notrans: |
Josef Bacik | 943eb3b | 2019-11-19 13:59:20 -0500 | [diff] [blame] | 9209 | if (new_ino == BTRFS_FIRST_FREE_OBJECTID || |
| 9210 | old_ino == BTRFS_FIRST_FREE_OBJECTID) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9211 | up_read(&fs_info->subvol_sem); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9212 | |
| 9213 | return ret; |
| 9214 | } |
| 9215 | |
| 9216 | static int btrfs_whiteout_for_rename(struct btrfs_trans_handle *trans, |
| 9217 | struct btrfs_root *root, |
Christian Brauner | ca07274 | 2021-07-27 12:48:42 +0200 | [diff] [blame] | 9218 | struct user_namespace *mnt_userns, |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9219 | struct inode *dir, |
| 9220 | struct dentry *dentry) |
| 9221 | { |
| 9222 | int ret; |
| 9223 | struct inode *inode; |
| 9224 | u64 objectid; |
| 9225 | u64 index; |
| 9226 | |
Nikolay Borisov | 543068a | 2020-12-07 17:32:33 +0200 | [diff] [blame] | 9227 | ret = btrfs_get_free_objectid(root, &objectid); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9228 | if (ret) |
| 9229 | return ret; |
| 9230 | |
Christian Brauner | ca07274 | 2021-07-27 12:48:42 +0200 | [diff] [blame] | 9231 | inode = btrfs_new_inode(trans, root, mnt_userns, dir, |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9232 | dentry->d_name.name, |
| 9233 | dentry->d_name.len, |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 9234 | btrfs_ino(BTRFS_I(dir)), |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9235 | objectid, |
| 9236 | S_IFCHR | WHITEOUT_MODE, |
| 9237 | &index); |
| 9238 | |
| 9239 | if (IS_ERR(inode)) { |
| 9240 | ret = PTR_ERR(inode); |
| 9241 | return ret; |
| 9242 | } |
| 9243 | |
| 9244 | inode->i_op = &btrfs_special_inode_operations; |
| 9245 | init_special_inode(inode, inode->i_mode, |
| 9246 | WHITEOUT_DEV); |
| 9247 | |
| 9248 | ret = btrfs_init_inode_security(trans, inode, dir, |
| 9249 | &dentry->d_name); |
| 9250 | if (ret) |
Filipe Manana | c990161 | 2016-05-05 01:41:57 +0100 | [diff] [blame] | 9251 | goto out; |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9252 | |
Nikolay Borisov | cef415a | 2017-02-20 13:51:09 +0200 | [diff] [blame] | 9253 | ret = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, |
| 9254 | BTRFS_I(inode), 0, index); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9255 | if (ret) |
Filipe Manana | c990161 | 2016-05-05 01:41:57 +0100 | [diff] [blame] | 9256 | goto out; |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9257 | |
Nikolay Borisov | 9a56fcd | 2020-11-02 16:48:59 +0200 | [diff] [blame] | 9258 | ret = btrfs_update_inode(trans, root, BTRFS_I(inode)); |
Filipe Manana | c990161 | 2016-05-05 01:41:57 +0100 | [diff] [blame] | 9259 | out: |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9260 | unlock_new_inode(inode); |
Filipe Manana | c990161 | 2016-05-05 01:41:57 +0100 | [diff] [blame] | 9261 | if (ret) |
| 9262 | inode_dec_link_count(inode); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9263 | iput(inode); |
| 9264 | |
Filipe Manana | c990161 | 2016-05-05 01:41:57 +0100 | [diff] [blame] | 9265 | return ret; |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9266 | } |
| 9267 | |
Christian Brauner | ca07274 | 2021-07-27 12:48:42 +0200 | [diff] [blame] | 9268 | static int btrfs_rename(struct user_namespace *mnt_userns, |
| 9269 | struct inode *old_dir, struct dentry *old_dentry, |
| 9270 | struct inode *new_dir, struct dentry *new_dentry, |
| 9271 | unsigned int flags) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9272 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9273 | struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9274 | struct btrfs_trans_handle *trans; |
Filipe Manana | 5062af3 | 2016-05-05 10:26:26 +0100 | [diff] [blame] | 9275 | unsigned int trans_num_items; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9276 | struct btrfs_root *root = BTRFS_I(old_dir)->root; |
| 9277 | struct btrfs_root *dest = BTRFS_I(new_dir)->root; |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 9278 | struct inode *new_inode = d_inode(new_dentry); |
| 9279 | struct inode *old_inode = d_inode(old_dentry); |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 9280 | u64 index = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9281 | int ret; |
Filipe Manana | 75b463d | 2020-08-11 12:43:48 +0100 | [diff] [blame] | 9282 | int ret2; |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 9283 | u64 old_ino = btrfs_ino(BTRFS_I(old_inode)); |
Filipe Manana | 3dc9e8f7 | 2016-04-29 11:34:22 +0100 | [diff] [blame] | 9284 | bool log_pinned = false; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9285 | |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 9286 | if (btrfs_ino(BTRFS_I(new_dir)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) |
Yan, Zheng | f679a84 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 9287 | return -EPERM; |
| 9288 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9289 | /* we only allow rename subvolume link between subvolumes */ |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 9290 | if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest) |
Chris Mason | 3394e16 | 2008-11-17 20:42:26 -0500 | [diff] [blame] | 9291 | return -EXDEV; |
| 9292 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 9293 | if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID || |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 9294 | (new_inode && btrfs_ino(BTRFS_I(new_inode)) == BTRFS_FIRST_FREE_OBJECTID)) |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9295 | return -ENOTEMPTY; |
| 9296 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9297 | if (S_ISDIR(old_inode->i_mode) && new_inode && |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9298 | new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9299 | return -ENOTEMPTY; |
Chris Mason | 9c52057 | 2012-12-17 14:26:57 -0500 | [diff] [blame] | 9300 | |
| 9301 | |
| 9302 | /* check for collisions, even if the name isn't there */ |
Josef Bacik | 4871c15 | 2013-10-09 12:24:04 -0400 | [diff] [blame] | 9303 | ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino, |
Chris Mason | 9c52057 | 2012-12-17 14:26:57 -0500 | [diff] [blame] | 9304 | new_dentry->d_name.name, |
| 9305 | new_dentry->d_name.len); |
| 9306 | |
| 9307 | if (ret) { |
| 9308 | if (ret == -EEXIST) { |
| 9309 | /* we shouldn't get |
| 9310 | * eexist without a new_inode */ |
Dulshani Gunawardhana | fae7f21 | 2013-10-31 10:30:08 +0530 | [diff] [blame] | 9311 | if (WARN_ON(!new_inode)) { |
Chris Mason | 9c52057 | 2012-12-17 14:26:57 -0500 | [diff] [blame] | 9312 | return ret; |
| 9313 | } |
| 9314 | } else { |
| 9315 | /* maybe -EOVERFLOW */ |
| 9316 | return ret; |
| 9317 | } |
| 9318 | } |
| 9319 | ret = 0; |
| 9320 | |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 9321 | /* |
Chris Mason | 8d875f9 | 2014-08-12 10:47:42 -0700 | [diff] [blame] | 9322 | * we're using rename to replace one file with another. Start IO on it |
| 9323 | * 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] | 9324 | */ |
Chris Mason | 8d875f9 | 2014-08-12 10:47:42 -0700 | [diff] [blame] | 9325 | 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] | 9326 | filemap_flush(old_inode->i_mapping); |
| 9327 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 9328 | /* close the racy window with snapshot create/destroy ioctl */ |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 9329 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9330 | down_read(&fs_info->subvol_sem); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 9331 | /* |
| 9332 | * We want to reserve the absolute worst case amount of items. So if |
| 9333 | * both inodes are subvols and we need to unlink them then that would |
| 9334 | * require 4 item modifications, but if they are both normal inodes it |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9335 | * would require 5 item modifications, so we'll assume they are normal |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 9336 | * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items |
| 9337 | * should cover the worst case number of items we'll modify. |
Filipe Manana | 5062af3 | 2016-05-05 10:26:26 +0100 | [diff] [blame] | 9338 | * If our rename has the whiteout flag, we need more 5 units for the |
| 9339 | * new inode (1 inode item, 1 inode ref, 2 dir items and 1 xattr item |
| 9340 | * when selinux is enabled). |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 9341 | */ |
Filipe Manana | 5062af3 | 2016-05-05 10:26:26 +0100 | [diff] [blame] | 9342 | trans_num_items = 11; |
| 9343 | if (flags & RENAME_WHITEOUT) |
| 9344 | trans_num_items += 5; |
| 9345 | trans = btrfs_start_transaction(root, trans_num_items); |
Johann Lombardi | b44c59a | 2011-03-31 13:23:47 +0000 | [diff] [blame] | 9346 | if (IS_ERR(trans)) { |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9347 | ret = PTR_ERR(trans); |
| 9348 | goto out_notrans; |
| 9349 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 9350 | |
Josef Bacik | b0fec6f | 2021-03-12 15:25:03 -0500 | [diff] [blame] | 9351 | if (dest != root) { |
| 9352 | ret = btrfs_record_root_in_trans(trans, dest); |
| 9353 | if (ret) |
| 9354 | goto out_fail; |
| 9355 | } |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9356 | |
Nikolay Borisov | 877574e | 2017-02-20 13:50:33 +0200 | [diff] [blame] | 9357 | ret = btrfs_set_inode_index(BTRFS_I(new_dir), &index); |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 9358 | if (ret) |
| 9359 | goto out_fail; |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 9360 | |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 9361 | BTRFS_I(old_inode)->dir_index = 0ULL; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 9362 | if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9363 | /* force full log commit if subvolume involved. */ |
David Sterba | 9078776 | 2019-03-20 13:28:05 +0100 | [diff] [blame] | 9364 | btrfs_set_log_full_commit(trans); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9365 | } else { |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 9366 | ret = btrfs_insert_inode_ref(trans, dest, |
| 9367 | new_dentry->d_name.name, |
| 9368 | new_dentry->d_name.len, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 9369 | old_ino, |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 9370 | btrfs_ino(BTRFS_I(new_dir)), index); |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 9371 | if (ret) |
| 9372 | goto out_fail; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9373 | } |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 9374 | |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 9375 | inode_inc_iversion(old_dir); |
| 9376 | inode_inc_iversion(new_dir); |
| 9377 | inode_inc_iversion(old_inode); |
Deepa Dinamani | 04b285f | 2016-02-06 23:57:21 -0800 | [diff] [blame] | 9378 | old_dir->i_ctime = old_dir->i_mtime = |
| 9379 | new_dir->i_ctime = new_dir->i_mtime = |
Deepa Dinamani | c2050a4 | 2016-09-14 07:48:06 -0700 | [diff] [blame] | 9380 | old_inode->i_ctime = current_time(old_dir); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 9381 | |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 9382 | if (old_dentry->d_parent != new_dentry->d_parent) |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 9383 | btrfs_record_unlink_dir(trans, BTRFS_I(old_dir), |
| 9384 | BTRFS_I(old_inode), 1); |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 9385 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 9386 | if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) { |
Josef Bacik | 045d396 | 2019-12-18 17:20:27 -0500 | [diff] [blame] | 9387 | ret = btrfs_unlink_subvol(trans, old_dir, old_dentry); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9388 | } else { |
Filipe Manana | bd54f38 | 2021-07-27 11:24:45 +0100 | [diff] [blame] | 9389 | /* |
| 9390 | * Now pin the log. We do it to ensure that no other task can |
| 9391 | * sync the log while we are in progress with the rename, as |
| 9392 | * that could result in an inconsistency in case any of the |
| 9393 | * inodes that are part of this rename operation were logged |
| 9394 | * before. |
| 9395 | * |
| 9396 | * We pin the log even if at this precise moment none of the |
| 9397 | * inodes was logged before. This is because right after we |
| 9398 | * checked for that, some other task fsyncing some other inode |
| 9399 | * not involved with this rename operation could log that one of |
| 9400 | * our inodes exists. |
| 9401 | * |
| 9402 | * We don't need to pin the logs before the above call to |
| 9403 | * btrfs_insert_inode_ref(), since that does not need to change |
| 9404 | * a log. |
| 9405 | */ |
| 9406 | btrfs_pin_log_trans(root); |
| 9407 | log_pinned = true; |
Filipe Manana | 4467af8 | 2021-10-25 17:31:50 +0100 | [diff] [blame] | 9408 | ret = __btrfs_unlink_inode(trans, BTRFS_I(old_dir), |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 9409 | BTRFS_I(d_inode(old_dentry)), |
Al Viro | 9298679 | 2011-03-04 17:14:37 +0000 | [diff] [blame] | 9410 | old_dentry->d_name.name, |
| 9411 | old_dentry->d_name.len); |
| 9412 | if (!ret) |
Nikolay Borisov | 9a56fcd | 2020-11-02 16:48:59 +0200 | [diff] [blame] | 9413 | ret = btrfs_update_inode(trans, root, BTRFS_I(old_inode)); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9414 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 9415 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 9416 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 9417 | goto out_fail; |
| 9418 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9419 | |
| 9420 | if (new_inode) { |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 9421 | inode_inc_iversion(new_inode); |
Deepa Dinamani | c2050a4 | 2016-09-14 07:48:06 -0700 | [diff] [blame] | 9422 | new_inode->i_ctime = current_time(new_inode); |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 9423 | if (unlikely(btrfs_ino(BTRFS_I(new_inode)) == |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9424 | BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) { |
Josef Bacik | 045d396 | 2019-12-18 17:20:27 -0500 | [diff] [blame] | 9425 | ret = btrfs_unlink_subvol(trans, new_dir, new_dentry); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9426 | BUG_ON(new_inode->i_nlink == 0); |
| 9427 | } else { |
Filipe Manana | 4467af8 | 2021-10-25 17:31:50 +0100 | [diff] [blame] | 9428 | ret = btrfs_unlink_inode(trans, BTRFS_I(new_dir), |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 9429 | BTRFS_I(d_inode(new_dentry)), |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9430 | new_dentry->d_name.name, |
| 9431 | new_dentry->d_name.len); |
| 9432 | } |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 9433 | if (!ret && new_inode->i_nlink == 0) |
Nikolay Borisov | 73f2e54 | 2017-02-20 13:50:59 +0200 | [diff] [blame] | 9434 | ret = btrfs_orphan_add(trans, |
| 9435 | BTRFS_I(d_inode(new_dentry))); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 9436 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 9437 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 9438 | goto out_fail; |
| 9439 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9440 | } |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 9441 | |
Nikolay Borisov | db0a669 | 2017-02-20 13:51:08 +0200 | [diff] [blame] | 9442 | 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] | 9443 | new_dentry->d_name.name, |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 9444 | new_dentry->d_name.len, 0, index); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 9445 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 9446 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 9447 | goto out_fail; |
| 9448 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9449 | |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 9450 | if (old_inode->i_nlink == 1) |
| 9451 | BTRFS_I(old_inode)->dir_index = index; |
| 9452 | |
Filipe Manana | 3dc9e8f7 | 2016-04-29 11:34:22 +0100 | [diff] [blame] | 9453 | if (log_pinned) { |
Filipe Manana | 75b463d | 2020-08-11 12:43:48 +0100 | [diff] [blame] | 9454 | btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir), |
| 9455 | new_dentry->d_parent); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9456 | btrfs_end_log_trans(root); |
Filipe Manana | 3dc9e8f7 | 2016-04-29 11:34:22 +0100 | [diff] [blame] | 9457 | log_pinned = false; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9458 | } |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9459 | |
| 9460 | if (flags & RENAME_WHITEOUT) { |
Christian Brauner | ca07274 | 2021-07-27 12:48:42 +0200 | [diff] [blame] | 9461 | ret = btrfs_whiteout_for_rename(trans, root, mnt_userns, |
| 9462 | old_dir, old_dentry); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9463 | |
| 9464 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 9465 | btrfs_abort_transaction(trans, ret); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9466 | goto out_fail; |
| 9467 | } |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 9468 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9469 | out_fail: |
Filipe Manana | 3dc9e8f7 | 2016-04-29 11:34:22 +0100 | [diff] [blame] | 9470 | /* |
| 9471 | * If we have pinned the log and an error happened, we unpin tasks |
| 9472 | * trying to sync the log and force them to fallback to a transaction |
| 9473 | * commit if the log currently contains any of the inodes involved in |
| 9474 | * this rename operation (to ensure we do not persist a log with an |
| 9475 | * inconsistent state for any of these inodes or leading to any |
| 9476 | * inconsistencies when replayed). If the transaction was aborted, the |
| 9477 | * abortion reason is propagated to userspace when attempting to commit |
| 9478 | * the transaction. If the log does not contain any of these inodes, we |
| 9479 | * allow the tasks to sync it. |
| 9480 | */ |
| 9481 | if (ret && log_pinned) { |
Nikolay Borisov | 0f8939b | 2017-01-18 00:31:30 +0200 | [diff] [blame] | 9482 | if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) || |
| 9483 | btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) || |
| 9484 | btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) || |
Filipe Manana | 3dc9e8f7 | 2016-04-29 11:34:22 +0100 | [diff] [blame] | 9485 | (new_inode && |
Nikolay Borisov | 0f8939b | 2017-01-18 00:31:30 +0200 | [diff] [blame] | 9486 | btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation))) |
David Sterba | 9078776 | 2019-03-20 13:28:05 +0100 | [diff] [blame] | 9487 | btrfs_set_log_full_commit(trans); |
Filipe Manana | 3dc9e8f7 | 2016-04-29 11:34:22 +0100 | [diff] [blame] | 9488 | |
| 9489 | btrfs_end_log_trans(root); |
| 9490 | log_pinned = false; |
| 9491 | } |
Filipe Manana | 75b463d | 2020-08-11 12:43:48 +0100 | [diff] [blame] | 9492 | ret2 = btrfs_end_transaction(trans); |
| 9493 | ret = ret ? ret : ret2; |
Johann Lombardi | b44c59a | 2011-03-31 13:23:47 +0000 | [diff] [blame] | 9494 | out_notrans: |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 9495 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9496 | up_read(&fs_info->subvol_sem); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 9497 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9498 | return ret; |
| 9499 | } |
| 9500 | |
Christian Brauner | 549c729 | 2021-01-21 14:19:43 +0100 | [diff] [blame] | 9501 | static int btrfs_rename2(struct user_namespace *mnt_userns, struct inode *old_dir, |
| 9502 | struct dentry *old_dentry, struct inode *new_dir, |
| 9503 | struct dentry *new_dentry, unsigned int flags) |
Miklos Szeredi | 80ace85 | 2014-07-23 15:15:32 +0200 | [diff] [blame] | 9504 | { |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9505 | if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT)) |
Miklos Szeredi | 80ace85 | 2014-07-23 15:15:32 +0200 | [diff] [blame] | 9506 | return -EINVAL; |
| 9507 | |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9508 | if (flags & RENAME_EXCHANGE) |
| 9509 | return btrfs_rename_exchange(old_dir, old_dentry, new_dir, |
| 9510 | new_dentry); |
| 9511 | |
Christian Brauner | ca07274 | 2021-07-27 12:48:42 +0200 | [diff] [blame] | 9512 | return btrfs_rename(mnt_userns, old_dir, old_dentry, new_dir, |
| 9513 | new_dentry, flags); |
Miklos Szeredi | 80ace85 | 2014-07-23 15:15:32 +0200 | [diff] [blame] | 9514 | } |
| 9515 | |
Nikolay Borisov | 3a2f8c0 | 2018-04-24 17:23:59 +0300 | [diff] [blame] | 9516 | struct btrfs_delalloc_work { |
| 9517 | struct inode *inode; |
| 9518 | struct completion completion; |
| 9519 | struct list_head list; |
| 9520 | struct btrfs_work work; |
| 9521 | }; |
| 9522 | |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 9523 | static void btrfs_run_delalloc_work(struct btrfs_work *work) |
| 9524 | { |
| 9525 | struct btrfs_delalloc_work *delalloc_work; |
Josef Bacik | 9f23e28 | 2013-10-28 15:03:41 -0400 | [diff] [blame] | 9526 | struct inode *inode; |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 9527 | |
| 9528 | delalloc_work = container_of(work, struct btrfs_delalloc_work, |
| 9529 | work); |
Josef Bacik | 9f23e28 | 2013-10-28 15:03:41 -0400 | [diff] [blame] | 9530 | inode = delalloc_work->inode; |
David Sterba | 3042460 | 2015-11-27 19:27:11 +0100 | [diff] [blame] | 9531 | filemap_flush(inode->i_mapping); |
| 9532 | if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, |
| 9533 | &BTRFS_I(inode)->runtime_flags)) |
Josef Bacik | 9f23e28 | 2013-10-28 15:03:41 -0400 | [diff] [blame] | 9534 | filemap_flush(inode->i_mapping); |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 9535 | |
Nikolay Borisov | 076da91 | 2018-04-23 10:54:16 +0300 | [diff] [blame] | 9536 | iput(inode); |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 9537 | complete(&delalloc_work->completion); |
| 9538 | } |
| 9539 | |
Nikolay Borisov | 3a2f8c0 | 2018-04-24 17:23:59 +0300 | [diff] [blame] | 9540 | static struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode) |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 9541 | { |
| 9542 | struct btrfs_delalloc_work *work; |
| 9543 | |
David Sterba | 100d570 | 2015-12-08 14:39:32 +0100 | [diff] [blame] | 9544 | work = kmalloc(sizeof(*work), GFP_NOFS); |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 9545 | if (!work) |
| 9546 | return NULL; |
| 9547 | |
| 9548 | init_completion(&work->completion); |
| 9549 | INIT_LIST_HEAD(&work->list); |
| 9550 | work->inode = inode; |
Omar Sandoval | a0cac0e | 2019-09-16 11:30:57 -0700 | [diff] [blame] | 9551 | btrfs_init_work(&work->work, btrfs_run_delalloc_work, NULL, NULL); |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 9552 | |
| 9553 | return work; |
| 9554 | } |
| 9555 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 9556 | /* |
| 9557 | * some fairly slow code that needs optimization. This walks the list |
| 9558 | * of all the inodes with pending delalloc and forces them to disk. |
| 9559 | */ |
Josef Bacik | e076ab2a | 2021-01-07 17:08:30 -0500 | [diff] [blame] | 9560 | static int start_delalloc_inodes(struct btrfs_root *root, |
| 9561 | struct writeback_control *wbc, bool snapshot, |
Filipe Manana | 3d45f22 | 2020-12-02 11:55:58 +0000 | [diff] [blame] | 9562 | bool in_reclaim_context) |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 9563 | { |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 9564 | struct btrfs_inode *binode; |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 9565 | struct inode *inode; |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 9566 | struct btrfs_delalloc_work *work, *next; |
| 9567 | struct list_head works; |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 9568 | struct list_head splice; |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 9569 | int ret = 0; |
Josef Bacik | e076ab2a | 2021-01-07 17:08:30 -0500 | [diff] [blame] | 9570 | bool full_flush = wbc->nr_to_write == LONG_MAX; |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 9571 | |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 9572 | INIT_LIST_HEAD(&works); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 9573 | INIT_LIST_HEAD(&splice); |
Miao Xie | 63607cc | 2013-01-22 10:50:35 +0000 | [diff] [blame] | 9574 | |
Miao Xie | 573bfb7 | 2014-03-06 13:55:03 +0800 | [diff] [blame] | 9575 | mutex_lock(&root->delalloc_mutex); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9576 | spin_lock(&root->delalloc_lock); |
| 9577 | list_splice_init(&root->delalloc_inodes, &splice); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 9578 | while (!list_empty(&splice)) { |
| 9579 | binode = list_entry(splice.next, struct btrfs_inode, |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 9580 | delalloc_inodes); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 9581 | |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9582 | list_move_tail(&binode->delalloc_inodes, |
| 9583 | &root->delalloc_inodes); |
Filipe Manana | 3d45f22 | 2020-12-02 11:55:58 +0000 | [diff] [blame] | 9584 | |
| 9585 | if (in_reclaim_context && |
| 9586 | test_bit(BTRFS_INODE_NO_DELALLOC_FLUSH, &binode->runtime_flags)) |
| 9587 | continue; |
| 9588 | |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 9589 | inode = igrab(&binode->vfs_inode); |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 9590 | if (!inode) { |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9591 | cond_resched_lock(&root->delalloc_lock); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 9592 | continue; |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 9593 | } |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9594 | spin_unlock(&root->delalloc_lock); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 9595 | |
Ethan Lien | 3cd24c6 | 2018-11-01 14:49:03 +0800 | [diff] [blame] | 9596 | if (snapshot) |
| 9597 | set_bit(BTRFS_INODE_SNAPSHOT_FLUSH, |
| 9598 | &binode->runtime_flags); |
Josef Bacik | e076ab2a | 2021-01-07 17:08:30 -0500 | [diff] [blame] | 9599 | if (full_flush) { |
| 9600 | work = btrfs_alloc_delalloc_work(inode); |
| 9601 | if (!work) { |
| 9602 | iput(inode); |
| 9603 | ret = -ENOMEM; |
| 9604 | goto out; |
| 9605 | } |
| 9606 | list_add_tail(&work->list, &works); |
| 9607 | btrfs_queue_work(root->fs_info->flush_workers, |
| 9608 | &work->work); |
| 9609 | } else { |
Josef Bacik | b377630 | 2021-07-14 14:47:23 -0400 | [diff] [blame] | 9610 | ret = filemap_fdatawrite_wbc(inode->i_mapping, wbc); |
Josef Bacik | e076ab2a | 2021-01-07 17:08:30 -0500 | [diff] [blame] | 9611 | btrfs_add_delayed_iput(inode); |
| 9612 | if (ret || wbc->nr_to_write <= 0) |
Josef Bacik | b491213 | 2020-07-21 10:22:12 -0400 | [diff] [blame] | 9613 | goto out; |
| 9614 | } |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 9615 | cond_resched(); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9616 | spin_lock(&root->delalloc_lock); |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 9617 | } |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9618 | spin_unlock(&root->delalloc_lock); |
Chris Mason | 8c8bee1 | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 9619 | |
Wang Shilong | a1ecaab | 2014-04-02 19:53:32 +0800 | [diff] [blame] | 9620 | out: |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 9621 | list_for_each_entry_safe(work, next, &works, list) { |
| 9622 | list_del_init(&work->list); |
Nikolay Borisov | 40012f9 | 2018-04-19 10:46:39 +0300 | [diff] [blame] | 9623 | wait_for_completion(&work->completion); |
| 9624 | kfree(work); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 9625 | } |
| 9626 | |
Nikolay Borisov | 81f1d39 | 2018-04-19 10:46:37 +0300 | [diff] [blame] | 9627 | if (!list_empty(&splice)) { |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9628 | spin_lock(&root->delalloc_lock); |
| 9629 | list_splice_tail(&splice, &root->delalloc_inodes); |
| 9630 | spin_unlock(&root->delalloc_lock); |
| 9631 | } |
Miao Xie | 573bfb7 | 2014-03-06 13:55:03 +0800 | [diff] [blame] | 9632 | mutex_unlock(&root->delalloc_mutex); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9633 | return ret; |
| 9634 | } |
| 9635 | |
Filipe Manana | f9baa50 | 2021-04-22 12:08:05 +0100 | [diff] [blame] | 9636 | 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] | 9637 | { |
Josef Bacik | e076ab2a | 2021-01-07 17:08:30 -0500 | [diff] [blame] | 9638 | struct writeback_control wbc = { |
| 9639 | .nr_to_write = LONG_MAX, |
| 9640 | .sync_mode = WB_SYNC_NONE, |
| 9641 | .range_start = 0, |
| 9642 | .range_end = LLONG_MAX, |
| 9643 | }; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9644 | struct btrfs_fs_info *fs_info = root->fs_info; |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9645 | |
Josef Bacik | 8496153 | 2021-10-05 16:35:25 -0400 | [diff] [blame] | 9646 | if (BTRFS_FS_ERROR(fs_info)) |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9647 | return -EROFS; |
| 9648 | |
Filipe Manana | f9baa50 | 2021-04-22 12:08:05 +0100 | [diff] [blame] | 9649 | return start_delalloc_inodes(root, &wbc, true, in_reclaim_context); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9650 | } |
| 9651 | |
Nikolay Borisov | 9db4dc2 | 2021-01-11 12:58:11 +0200 | [diff] [blame] | 9652 | 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] | 9653 | bool in_reclaim_context) |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9654 | { |
Josef Bacik | e076ab2a | 2021-01-07 17:08:30 -0500 | [diff] [blame] | 9655 | struct writeback_control wbc = { |
Nikolay Borisov | 9db4dc2 | 2021-01-11 12:58:11 +0200 | [diff] [blame] | 9656 | .nr_to_write = nr, |
Josef Bacik | e076ab2a | 2021-01-07 17:08:30 -0500 | [diff] [blame] | 9657 | .sync_mode = WB_SYNC_NONE, |
| 9658 | .range_start = 0, |
| 9659 | .range_end = LLONG_MAX, |
| 9660 | }; |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9661 | struct btrfs_root *root; |
| 9662 | struct list_head splice; |
| 9663 | int ret; |
| 9664 | |
Josef Bacik | 8496153 | 2021-10-05 16:35:25 -0400 | [diff] [blame] | 9665 | if (BTRFS_FS_ERROR(fs_info)) |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9666 | return -EROFS; |
| 9667 | |
| 9668 | INIT_LIST_HEAD(&splice); |
| 9669 | |
Miao Xie | 573bfb7 | 2014-03-06 13:55:03 +0800 | [diff] [blame] | 9670 | mutex_lock(&fs_info->delalloc_root_mutex); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9671 | spin_lock(&fs_info->delalloc_root_lock); |
| 9672 | list_splice_init(&fs_info->delalloc_roots, &splice); |
Nikolay Borisov | d7830b7 | 2021-01-11 12:58:12 +0200 | [diff] [blame] | 9673 | while (!list_empty(&splice)) { |
Josef Bacik | e076ab2a | 2021-01-07 17:08:30 -0500 | [diff] [blame] | 9674 | /* |
| 9675 | * Reset nr_to_write here so we know that we're doing a full |
| 9676 | * flush. |
| 9677 | */ |
Nikolay Borisov | 9db4dc2 | 2021-01-11 12:58:11 +0200 | [diff] [blame] | 9678 | if (nr == LONG_MAX) |
Josef Bacik | e076ab2a | 2021-01-07 17:08:30 -0500 | [diff] [blame] | 9679 | wbc.nr_to_write = LONG_MAX; |
| 9680 | |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9681 | root = list_first_entry(&splice, struct btrfs_root, |
| 9682 | delalloc_root); |
Josef Bacik | 0024652 | 2020-01-24 09:33:01 -0500 | [diff] [blame] | 9683 | root = btrfs_grab_root(root); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9684 | BUG_ON(!root); |
| 9685 | list_move_tail(&root->delalloc_root, |
| 9686 | &fs_info->delalloc_roots); |
| 9687 | spin_unlock(&fs_info->delalloc_root_lock); |
| 9688 | |
Josef Bacik | e076ab2a | 2021-01-07 17:08:30 -0500 | [diff] [blame] | 9689 | ret = start_delalloc_inodes(root, &wbc, false, in_reclaim_context); |
Josef Bacik | 0024652 | 2020-01-24 09:33:01 -0500 | [diff] [blame] | 9690 | btrfs_put_root(root); |
Josef Bacik | e076ab2a | 2021-01-07 17:08:30 -0500 | [diff] [blame] | 9691 | if (ret < 0 || wbc.nr_to_write <= 0) |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9692 | goto out; |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9693 | spin_lock(&fs_info->delalloc_root_lock); |
| 9694 | } |
| 9695 | spin_unlock(&fs_info->delalloc_root_lock); |
| 9696 | |
Miao Xie | 6c255e6 | 2014-03-06 13:55:01 +0800 | [diff] [blame] | 9697 | ret = 0; |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 9698 | out: |
Nikolay Borisov | 81f1d39 | 2018-04-19 10:46:37 +0300 | [diff] [blame] | 9699 | if (!list_empty(&splice)) { |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9700 | spin_lock(&fs_info->delalloc_root_lock); |
| 9701 | list_splice_tail(&splice, &fs_info->delalloc_roots); |
| 9702 | spin_unlock(&fs_info->delalloc_root_lock); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 9703 | } |
Miao Xie | 573bfb7 | 2014-03-06 13:55:03 +0800 | [diff] [blame] | 9704 | mutex_unlock(&fs_info->delalloc_root_mutex); |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 9705 | return ret; |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 9706 | } |
| 9707 | |
Christian Brauner | 549c729 | 2021-01-21 14:19:43 +0100 | [diff] [blame] | 9708 | static int btrfs_symlink(struct user_namespace *mnt_userns, struct inode *dir, |
| 9709 | struct dentry *dentry, const char *symname) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9710 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9711 | struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9712 | struct btrfs_trans_handle *trans; |
| 9713 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 9714 | struct btrfs_path *path; |
| 9715 | struct btrfs_key key; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 9716 | struct inode *inode = NULL; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9717 | int err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9718 | u64 objectid; |
Dulshani Gunawardhana | 6787125 | 2013-10-31 10:33:04 +0530 | [diff] [blame] | 9719 | u64 index = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9720 | int name_len; |
| 9721 | int datasize; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 9722 | unsigned long ptr; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9723 | struct btrfs_file_extent_item *ei; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 9724 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9725 | |
Filipe David Borba Manana | f06becc | 2013-09-16 09:53:28 +0100 | [diff] [blame] | 9726 | name_len = strlen(symname); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9727 | if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info)) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9728 | return -ENAMETOOLONG; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 9729 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 9730 | /* |
| 9731 | * 2 items for inode item and ref |
| 9732 | * 2 items for dir items |
Filipe Manana | 9269d12 | 2015-12-31 18:16:29 +0000 | [diff] [blame] | 9733 | * 1 item for updating parent inode item |
| 9734 | * 1 item for the inline extent item |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 9735 | * 1 item for xattr if selinux is on |
| 9736 | */ |
Filipe Manana | 9269d12 | 2015-12-31 18:16:29 +0000 | [diff] [blame] | 9737 | trans = btrfs_start_transaction(root, 7); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 9738 | if (IS_ERR(trans)) |
| 9739 | return PTR_ERR(trans); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 9740 | |
Nikolay Borisov | 543068a | 2020-12-07 17:32:33 +0200 | [diff] [blame] | 9741 | err = btrfs_get_free_objectid(root, &objectid); |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 9742 | if (err) |
| 9743 | goto out_unlock; |
| 9744 | |
Christian Brauner | 5a05210 | 2021-07-27 12:48:47 +0200 | [diff] [blame] | 9745 | inode = btrfs_new_inode(trans, root, mnt_userns, dir, |
Christian Brauner | b3b6f5b | 2021-07-27 12:48:41 +0200 | [diff] [blame] | 9746 | dentry->d_name.name, dentry->d_name.len, |
| 9747 | btrfs_ino(BTRFS_I(dir)), objectid, |
| 9748 | S_IFLNK | S_IRWXUGO, &index); |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 9749 | if (IS_ERR(inode)) { |
| 9750 | err = PTR_ERR(inode); |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 9751 | inode = NULL; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9752 | goto out_unlock; |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 9753 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9754 | |
Casey Schaufler | ad19db7 | 2011-12-15 10:09:07 -0500 | [diff] [blame] | 9755 | /* |
| 9756 | * If the active LSM wants to access the inode during |
| 9757 | * d_instantiate it needs these. Smack checks to see |
| 9758 | * if the filesystem supports xattrs by looking at the |
| 9759 | * ops vector. |
| 9760 | */ |
| 9761 | inode->i_fop = &btrfs_file_operations; |
| 9762 | inode->i_op = &btrfs_file_inode_operations; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 9763 | inode->i_mapping->a_ops = &btrfs_aops; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 9764 | |
| 9765 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); |
| 9766 | if (err) |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 9767 | goto out_unlock; |
Casey Schaufler | ad19db7 | 2011-12-15 10:09:07 -0500 | [diff] [blame] | 9768 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9769 | path = btrfs_alloc_path(); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 9770 | if (!path) { |
| 9771 | err = -ENOMEM; |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 9772 | goto out_unlock; |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 9773 | } |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 9774 | key.objectid = btrfs_ino(BTRFS_I(inode)); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9775 | key.offset = 0; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 9776 | key.type = BTRFS_EXTENT_DATA_KEY; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9777 | datasize = btrfs_file_extent_calc_inline_size(name_len); |
| 9778 | err = btrfs_insert_empty_item(trans, root, path, &key, |
| 9779 | datasize); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 9780 | if (err) { |
Julia Lawall | b083916 | 2011-05-14 07:10:51 +0000 | [diff] [blame] | 9781 | btrfs_free_path(path); |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 9782 | goto out_unlock; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 9783 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 9784 | leaf = path->nodes[0]; |
| 9785 | ei = btrfs_item_ptr(leaf, path->slots[0], |
| 9786 | struct btrfs_file_extent_item); |
| 9787 | btrfs_set_file_extent_generation(leaf, ei, trans->transid); |
| 9788 | btrfs_set_file_extent_type(leaf, ei, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9789 | BTRFS_FILE_EXTENT_INLINE); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 9790 | btrfs_set_file_extent_encryption(leaf, ei, 0); |
| 9791 | btrfs_set_file_extent_compression(leaf, ei, 0); |
| 9792 | btrfs_set_file_extent_other_encoding(leaf, ei, 0); |
| 9793 | btrfs_set_file_extent_ram_bytes(leaf, ei, name_len); |
| 9794 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9795 | ptr = btrfs_file_extent_inline_start(ei); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 9796 | write_extent_buffer(leaf, symname, ptr, name_len); |
| 9797 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9798 | btrfs_free_path(path); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 9799 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9800 | inode->i_op = &btrfs_symlink_inode_operations; |
Al Viro | 21fc61c | 2015-11-17 01:07:57 -0500 | [diff] [blame] | 9801 | inode_nohighmem(inode); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 9802 | inode_set_bytes(inode, name_len); |
Nikolay Borisov | 6ef06d2 | 2017-02-20 13:50:34 +0200 | [diff] [blame] | 9803 | btrfs_i_size_write(BTRFS_I(inode), name_len); |
Nikolay Borisov | 9a56fcd | 2020-11-02 16:48:59 +0200 | [diff] [blame] | 9804 | err = btrfs_update_inode(trans, root, BTRFS_I(inode)); |
Filipe Manana | d50866d | 2015-12-31 18:08:24 +0000 | [diff] [blame] | 9805 | /* |
| 9806 | * Last step, add directory indexes for our symlink inode. This is the |
| 9807 | * last step to avoid extra cleanup of these indexes if an error happens |
| 9808 | * elsewhere above. |
| 9809 | */ |
| 9810 | if (!err) |
Nikolay Borisov | cef415a | 2017-02-20 13:51:09 +0200 | [diff] [blame] | 9811 | err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, |
| 9812 | BTRFS_I(inode), 0, index); |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 9813 | if (err) |
| 9814 | goto out_unlock; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 9815 | |
Al Viro | 1e2e547 | 2018-05-04 08:23:01 -0400 | [diff] [blame] | 9816 | d_instantiate_new(dentry, inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9817 | |
| 9818 | out_unlock: |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 9819 | btrfs_end_transaction(trans); |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 9820 | if (err && inode) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9821 | inode_dec_link_count(inode); |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 9822 | discard_new_inode(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9823 | } |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 9824 | btrfs_btree_balance_dirty(fs_info); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9825 | return err; |
| 9826 | } |
Chris Mason | 1643298 | 2008-04-10 10:23:21 -0400 | [diff] [blame] | 9827 | |
Filipe Manana | 8fccebf | 2020-09-08 11:27:20 +0100 | [diff] [blame] | 9828 | static struct btrfs_trans_handle *insert_prealloc_file_extent( |
| 9829 | struct btrfs_trans_handle *trans_in, |
Nikolay Borisov | 90dffd0 | 2020-11-02 16:48:54 +0200 | [diff] [blame] | 9830 | struct btrfs_inode *inode, |
| 9831 | struct btrfs_key *ins, |
Qu Wenruo | 203f44c5 | 2020-06-10 09:04:40 +0800 | [diff] [blame] | 9832 | u64 file_offset) |
| 9833 | { |
| 9834 | struct btrfs_file_extent_item stack_fi; |
Filipe Manana | bf38564 | 2020-09-08 11:27:22 +0100 | [diff] [blame] | 9835 | struct btrfs_replace_extent_info extent_info; |
Filipe Manana | 8fccebf | 2020-09-08 11:27:20 +0100 | [diff] [blame] | 9836 | struct btrfs_trans_handle *trans = trans_in; |
| 9837 | struct btrfs_path *path; |
Qu Wenruo | 203f44c5 | 2020-06-10 09:04:40 +0800 | [diff] [blame] | 9838 | u64 start = ins->objectid; |
| 9839 | u64 len = ins->offset; |
Qu Wenruo | fbf48bb | 2021-03-03 18:41:51 +0800 | [diff] [blame] | 9840 | int qgroup_released; |
Qu Wenruo | 9729f10 | 2020-06-10 09:04:41 +0800 | [diff] [blame] | 9841 | int ret; |
Qu Wenruo | 203f44c5 | 2020-06-10 09:04:40 +0800 | [diff] [blame] | 9842 | |
| 9843 | memset(&stack_fi, 0, sizeof(stack_fi)); |
| 9844 | |
| 9845 | btrfs_set_stack_file_extent_type(&stack_fi, BTRFS_FILE_EXTENT_PREALLOC); |
| 9846 | btrfs_set_stack_file_extent_disk_bytenr(&stack_fi, start); |
| 9847 | btrfs_set_stack_file_extent_disk_num_bytes(&stack_fi, len); |
| 9848 | btrfs_set_stack_file_extent_num_bytes(&stack_fi, len); |
| 9849 | btrfs_set_stack_file_extent_ram_bytes(&stack_fi, len); |
| 9850 | btrfs_set_stack_file_extent_compression(&stack_fi, BTRFS_COMPRESS_NONE); |
| 9851 | /* Encryption and other encoding is reserved and all 0 */ |
| 9852 | |
Qu Wenruo | fbf48bb | 2021-03-03 18:41:51 +0800 | [diff] [blame] | 9853 | qgroup_released = btrfs_qgroup_release_data(inode, file_offset, len); |
| 9854 | if (qgroup_released < 0) |
| 9855 | return ERR_PTR(qgroup_released); |
Filipe Manana | 8fccebf | 2020-09-08 11:27:20 +0100 | [diff] [blame] | 9856 | |
| 9857 | if (trans) { |
Nikolay Borisov | 90dffd0 | 2020-11-02 16:48:54 +0200 | [diff] [blame] | 9858 | ret = insert_reserved_file_extent(trans, inode, |
Filipe Manana | 2766ff6 | 2020-11-04 11:07:34 +0000 | [diff] [blame] | 9859 | file_offset, &stack_fi, |
Qu Wenruo | fbf48bb | 2021-03-03 18:41:51 +0800 | [diff] [blame] | 9860 | true, qgroup_released); |
Filipe Manana | 8fccebf | 2020-09-08 11:27:20 +0100 | [diff] [blame] | 9861 | if (ret) |
Qu Wenruo | a3ee79b | 2021-03-03 18:41:52 +0800 | [diff] [blame] | 9862 | goto free_qgroup; |
Filipe Manana | 8fccebf | 2020-09-08 11:27:20 +0100 | [diff] [blame] | 9863 | return trans; |
| 9864 | } |
| 9865 | |
| 9866 | extent_info.disk_offset = start; |
| 9867 | extent_info.disk_len = len; |
| 9868 | extent_info.data_offset = 0; |
| 9869 | extent_info.data_len = len; |
| 9870 | extent_info.file_offset = file_offset; |
| 9871 | extent_info.extent_buf = (char *)&stack_fi; |
Filipe Manana | 8fccebf | 2020-09-08 11:27:20 +0100 | [diff] [blame] | 9872 | extent_info.is_new_extent = true; |
Qu Wenruo | fbf48bb | 2021-03-03 18:41:51 +0800 | [diff] [blame] | 9873 | extent_info.qgroup_reserved = qgroup_released; |
Filipe Manana | 8fccebf | 2020-09-08 11:27:20 +0100 | [diff] [blame] | 9874 | extent_info.insertions = 0; |
| 9875 | |
| 9876 | path = btrfs_alloc_path(); |
Qu Wenruo | a3ee79b | 2021-03-03 18:41:52 +0800 | [diff] [blame] | 9877 | if (!path) { |
| 9878 | ret = -ENOMEM; |
| 9879 | goto free_qgroup; |
| 9880 | } |
Filipe Manana | 8fccebf | 2020-09-08 11:27:20 +0100 | [diff] [blame] | 9881 | |
Nikolay Borisov | bfc7847 | 2021-02-17 15:12:47 +0200 | [diff] [blame] | 9882 | ret = btrfs_replace_file_extents(inode, path, file_offset, |
Filipe Manana | 8fccebf | 2020-09-08 11:27:20 +0100 | [diff] [blame] | 9883 | file_offset + len - 1, &extent_info, |
| 9884 | &trans); |
| 9885 | btrfs_free_path(path); |
| 9886 | if (ret) |
Qu Wenruo | a3ee79b | 2021-03-03 18:41:52 +0800 | [diff] [blame] | 9887 | goto free_qgroup; |
Filipe Manana | 8fccebf | 2020-09-08 11:27:20 +0100 | [diff] [blame] | 9888 | return trans; |
Qu Wenruo | a3ee79b | 2021-03-03 18:41:52 +0800 | [diff] [blame] | 9889 | |
| 9890 | free_qgroup: |
| 9891 | /* |
| 9892 | * We have released qgroup data range at the beginning of the function, |
| 9893 | * and normally qgroup_released bytes will be freed when committing |
| 9894 | * transaction. |
| 9895 | * But if we error out early, we have to free what we have released |
| 9896 | * or we leak qgroup data reservation. |
| 9897 | */ |
| 9898 | btrfs_qgroup_free_refroot(inode->root->fs_info, |
| 9899 | inode->root->root_key.objectid, qgroup_released, |
| 9900 | BTRFS_QGROUP_RSV_DATA); |
| 9901 | return ERR_PTR(ret); |
Qu Wenruo | 203f44c5 | 2020-06-10 09:04:40 +0800 | [diff] [blame] | 9902 | } |
Filipe Manana | 8fccebf | 2020-09-08 11:27:20 +0100 | [diff] [blame] | 9903 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 9904 | static int __btrfs_prealloc_file_range(struct inode *inode, int mode, |
| 9905 | u64 start, u64 num_bytes, u64 min_size, |
| 9906 | loff_t actual_len, u64 *alloc_hint, |
| 9907 | struct btrfs_trans_handle *trans) |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 9908 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9909 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 9910 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
| 9911 | struct extent_map *em; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 9912 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 9913 | struct btrfs_key ins; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 9914 | u64 cur_offset = start; |
Josef Bacik | b778cf9 | 2020-02-13 10:47:31 -0500 | [diff] [blame] | 9915 | u64 clear_offset = start; |
Josef Bacik | 55a61d1 | 2010-11-22 18:50:32 +0000 | [diff] [blame] | 9916 | u64 i_size; |
Chris Mason | 154ea28 | 2013-03-05 11:11:26 -0500 | [diff] [blame] | 9917 | u64 cur_bytes; |
Josef Bacik | 0b670dc | 2015-09-23 17:11:16 -0400 | [diff] [blame] | 9918 | u64 last_alloc = (u64)-1; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 9919 | int ret = 0; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 9920 | bool own_trans = true; |
Wang Xiaoguang | 1851309 | 2016-07-25 15:51:40 +0800 | [diff] [blame] | 9921 | u64 end = start + num_bytes - 1; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 9922 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 9923 | if (trans) |
| 9924 | own_trans = false; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 9925 | while (num_bytes > 0) { |
Byongho Lee | ee22184 | 2015-12-15 01:42:10 +0900 | [diff] [blame] | 9926 | cur_bytes = min_t(u64, num_bytes, SZ_256M); |
Chris Mason | 154ea28 | 2013-03-05 11:11:26 -0500 | [diff] [blame] | 9927 | cur_bytes = max(cur_bytes, min_size); |
Josef Bacik | 0b670dc | 2015-09-23 17:11:16 -0400 | [diff] [blame] | 9928 | /* |
| 9929 | * If we are severely fragmented we could end up with really |
| 9930 | * small allocations, so if the allocator is returning small |
| 9931 | * chunks lets make its job easier by only searching for those |
| 9932 | * sized chunks. |
| 9933 | */ |
| 9934 | cur_bytes = min(cur_bytes, last_alloc); |
Wang Xiaoguang | 1851309 | 2016-07-25 15:51:40 +0800 | [diff] [blame] | 9935 | ret = btrfs_reserve_extent(root, cur_bytes, cur_bytes, |
| 9936 | min_size, 0, *alloc_hint, &ins, 1, 0); |
Filipe Manana | 8fccebf | 2020-09-08 11:27:20 +0100 | [diff] [blame] | 9937 | if (ret) |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 9938 | break; |
Josef Bacik | b778cf9 | 2020-02-13 10:47:31 -0500 | [diff] [blame] | 9939 | |
| 9940 | /* |
| 9941 | * We've reserved this space, and thus converted it from |
| 9942 | * ->bytes_may_use to ->bytes_reserved. Any error that happens |
| 9943 | * from here on out we will only need to clear our reservation |
| 9944 | * for the remaining unreserved area, so advance our |
| 9945 | * clear_offset by our extent size. |
| 9946 | */ |
| 9947 | clear_offset += ins.offset; |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 9948 | |
Josef Bacik | 0b670dc | 2015-09-23 17:11:16 -0400 | [diff] [blame] | 9949 | last_alloc = ins.offset; |
Nikolay Borisov | 90dffd0 | 2020-11-02 16:48:54 +0200 | [diff] [blame] | 9950 | trans = insert_prealloc_file_extent(trans, BTRFS_I(inode), |
| 9951 | &ins, cur_offset); |
Filipe Manana | 1afc708 | 2020-10-14 10:10:36 +0100 | [diff] [blame] | 9952 | /* |
| 9953 | * Now that we inserted the prealloc extent we can finally |
| 9954 | * decrement the number of reservations in the block group. |
| 9955 | * If we did it before, we could race with relocation and have |
| 9956 | * relocation miss the reserved extent, making it fail later. |
| 9957 | */ |
| 9958 | btrfs_dec_block_group_reservations(fs_info, ins.objectid); |
Filipe Manana | 8fccebf | 2020-09-08 11:27:20 +0100 | [diff] [blame] | 9959 | if (IS_ERR(trans)) { |
| 9960 | ret = PTR_ERR(trans); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 9961 | btrfs_free_reserved_extent(fs_info, ins.objectid, |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 9962 | ins.offset, 0); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 9963 | break; |
| 9964 | } |
Dongsheng Yang | 3119321 | 2014-12-12 16:44:35 +0800 | [diff] [blame] | 9965 | |
Nikolay Borisov | dcdbc05 | 2017-02-20 13:50:45 +0200 | [diff] [blame] | 9966 | btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset, |
Chris Mason | a1ed835 | 2009-09-11 12:27:37 -0400 | [diff] [blame] | 9967 | cur_offset + ins.offset -1, 0); |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 9968 | |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 9969 | em = alloc_extent_map(); |
| 9970 | if (!em) { |
| 9971 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, |
| 9972 | &BTRFS_I(inode)->runtime_flags); |
| 9973 | goto next; |
| 9974 | } |
| 9975 | |
| 9976 | em->start = cur_offset; |
| 9977 | em->orig_start = cur_offset; |
| 9978 | em->len = ins.offset; |
| 9979 | em->block_start = ins.objectid; |
| 9980 | em->block_len = ins.offset; |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 9981 | em->orig_block_len = ins.offset; |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 9982 | em->ram_bytes = ins.offset; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 9983 | set_bit(EXTENT_FLAG_PREALLOC, &em->flags); |
| 9984 | em->generation = trans->transid; |
| 9985 | |
| 9986 | while (1) { |
| 9987 | write_lock(&em_tree->lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 9988 | ret = add_extent_mapping(em_tree, em, 1); |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 9989 | write_unlock(&em_tree->lock); |
| 9990 | if (ret != -EEXIST) |
| 9991 | break; |
Nikolay Borisov | dcdbc05 | 2017-02-20 13:50:45 +0200 | [diff] [blame] | 9992 | btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset, |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 9993 | cur_offset + ins.offset - 1, |
| 9994 | 0); |
| 9995 | } |
| 9996 | free_extent_map(em); |
| 9997 | next: |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 9998 | num_bytes -= ins.offset; |
| 9999 | cur_offset += ins.offset; |
Yan, Zheng | efa5646 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 10000 | *alloc_hint = ins.objectid + ins.offset; |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 10001 | |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 10002 | inode_inc_iversion(inode); |
Deepa Dinamani | c2050a4 | 2016-09-14 07:48:06 -0700 | [diff] [blame] | 10003 | inode->i_ctime = current_time(inode); |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 10004 | BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 10005 | if (!(mode & FALLOC_FL_KEEP_SIZE) && |
Yan, Zheng | efa5646 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 10006 | (actual_len > inode->i_size) && |
| 10007 | (cur_offset > inode->i_size)) { |
Aneesh Kumar K.V | d1ea6a6 | 2010-01-20 07:28:54 +0000 | [diff] [blame] | 10008 | if (cur_offset > actual_len) |
Josef Bacik | 55a61d1 | 2010-11-22 18:50:32 +0000 | [diff] [blame] | 10009 | i_size = actual_len; |
Aneesh Kumar K.V | d1ea6a6 | 2010-01-20 07:28:54 +0000 | [diff] [blame] | 10010 | else |
Josef Bacik | 55a61d1 | 2010-11-22 18:50:32 +0000 | [diff] [blame] | 10011 | i_size = cur_offset; |
| 10012 | i_size_write(inode, i_size); |
Nikolay Borisov | 76aea53 | 2020-11-02 16:48:53 +0200 | [diff] [blame] | 10013 | btrfs_inode_safe_disk_i_size_write(BTRFS_I(inode), 0); |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 10014 | } |
| 10015 | |
Nikolay Borisov | 9a56fcd | 2020-11-02 16:48:59 +0200 | [diff] [blame] | 10016 | ret = btrfs_update_inode(trans, root, BTRFS_I(inode)); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 10017 | |
| 10018 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 10019 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 10020 | if (own_trans) |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 10021 | btrfs_end_transaction(trans); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 10022 | break; |
| 10023 | } |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 10024 | |
Filipe Manana | 8fccebf | 2020-09-08 11:27:20 +0100 | [diff] [blame] | 10025 | if (own_trans) { |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 10026 | btrfs_end_transaction(trans); |
Filipe Manana | 8fccebf | 2020-09-08 11:27:20 +0100 | [diff] [blame] | 10027 | trans = NULL; |
| 10028 | } |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 10029 | } |
Josef Bacik | b778cf9 | 2020-02-13 10:47:31 -0500 | [diff] [blame] | 10030 | if (clear_offset < end) |
Nikolay Borisov | 25ce28c | 2020-06-03 08:55:39 +0300 | [diff] [blame] | 10031 | btrfs_free_reserved_data_space(BTRFS_I(inode), NULL, clear_offset, |
Josef Bacik | b778cf9 | 2020-02-13 10:47:31 -0500 | [diff] [blame] | 10032 | end - clear_offset + 1); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 10033 | return ret; |
| 10034 | } |
| 10035 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 10036 | int btrfs_prealloc_file_range(struct inode *inode, int mode, |
| 10037 | u64 start, u64 num_bytes, u64 min_size, |
| 10038 | loff_t actual_len, u64 *alloc_hint) |
| 10039 | { |
| 10040 | return __btrfs_prealloc_file_range(inode, mode, start, num_bytes, |
| 10041 | min_size, actual_len, alloc_hint, |
| 10042 | NULL); |
| 10043 | } |
| 10044 | |
| 10045 | int btrfs_prealloc_file_range_trans(struct inode *inode, |
| 10046 | struct btrfs_trans_handle *trans, int mode, |
| 10047 | u64 start, u64 num_bytes, u64 min_size, |
| 10048 | loff_t actual_len, u64 *alloc_hint) |
| 10049 | { |
| 10050 | return __btrfs_prealloc_file_range(inode, mode, start, num_bytes, |
| 10051 | min_size, actual_len, alloc_hint, trans); |
| 10052 | } |
| 10053 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 10054 | static int btrfs_set_page_dirty(struct page *page) |
| 10055 | { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 10056 | return __set_page_dirty_nobuffers(page); |
| 10057 | } |
| 10058 | |
Christian Brauner | 549c729 | 2021-01-21 14:19:43 +0100 | [diff] [blame] | 10059 | static int btrfs_permission(struct user_namespace *mnt_userns, |
| 10060 | struct inode *inode, int mask) |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 10061 | { |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 10062 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Jeff Mahoney | cb6db4e | 2011-08-15 17:27:21 +0000 | [diff] [blame] | 10063 | umode_t mode = inode->i_mode; |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 10064 | |
Jeff Mahoney | cb6db4e | 2011-08-15 17:27:21 +0000 | [diff] [blame] | 10065 | if (mask & MAY_WRITE && |
| 10066 | (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) { |
| 10067 | if (btrfs_root_readonly(root)) |
| 10068 | return -EROFS; |
| 10069 | if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY) |
| 10070 | return -EACCES; |
| 10071 | } |
Christian Brauner | 3bc71ba | 2021-07-27 12:48:50 +0200 | [diff] [blame] | 10072 | return generic_permission(mnt_userns, inode, mask); |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 10073 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10074 | |
Christian Brauner | 549c729 | 2021-01-21 14:19:43 +0100 | [diff] [blame] | 10075 | static int btrfs_tmpfile(struct user_namespace *mnt_userns, struct inode *dir, |
| 10076 | struct dentry *dentry, umode_t mode) |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 10077 | { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 10078 | struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 10079 | struct btrfs_trans_handle *trans; |
| 10080 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 10081 | struct inode *inode = NULL; |
| 10082 | u64 objectid; |
| 10083 | u64 index; |
| 10084 | int ret = 0; |
| 10085 | |
| 10086 | /* |
| 10087 | * 5 units required for adding orphan entry |
| 10088 | */ |
| 10089 | trans = btrfs_start_transaction(root, 5); |
| 10090 | if (IS_ERR(trans)) |
| 10091 | return PTR_ERR(trans); |
| 10092 | |
Nikolay Borisov | 543068a | 2020-12-07 17:32:33 +0200 | [diff] [blame] | 10093 | ret = btrfs_get_free_objectid(root, &objectid); |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 10094 | if (ret) |
| 10095 | goto out; |
| 10096 | |
Christian Brauner | 98b6ab5 | 2021-07-27 12:48:48 +0200 | [diff] [blame] | 10097 | inode = btrfs_new_inode(trans, root, mnt_userns, dir, NULL, 0, |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 10098 | btrfs_ino(BTRFS_I(dir)), objectid, mode, &index); |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 10099 | if (IS_ERR(inode)) { |
| 10100 | ret = PTR_ERR(inode); |
| 10101 | inode = NULL; |
| 10102 | goto out; |
| 10103 | } |
| 10104 | |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 10105 | inode->i_fop = &btrfs_file_operations; |
| 10106 | inode->i_op = &btrfs_file_inode_operations; |
| 10107 | |
| 10108 | inode->i_mapping->a_ops = &btrfs_aops; |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 10109 | |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 10110 | ret = btrfs_init_inode_security(trans, inode, dir, NULL); |
| 10111 | if (ret) |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 10112 | goto out; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 10113 | |
Nikolay Borisov | 9a56fcd | 2020-11-02 16:48:59 +0200 | [diff] [blame] | 10114 | ret = btrfs_update_inode(trans, root, BTRFS_I(inode)); |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 10115 | if (ret) |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 10116 | goto out; |
Nikolay Borisov | 73f2e54 | 2017-02-20 13:50:59 +0200 | [diff] [blame] | 10117 | ret = btrfs_orphan_add(trans, BTRFS_I(inode)); |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 10118 | if (ret) |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 10119 | goto out; |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 10120 | |
Filipe Manana | 5762b5c | 2014-08-01 00:10:32 +0100 | [diff] [blame] | 10121 | /* |
| 10122 | * We set number of links to 0 in btrfs_new_inode(), and here we set |
| 10123 | * it to 1 because d_tmpfile() will issue a warning if the count is 0, |
| 10124 | * through: |
| 10125 | * |
| 10126 | * d_tmpfile() -> inode_dec_link_count() -> drop_nlink() |
| 10127 | */ |
| 10128 | set_nlink(inode, 1); |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 10129 | d_tmpfile(dentry, inode); |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 10130 | unlock_new_inode(inode); |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 10131 | mark_inode_dirty(inode); |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 10132 | out: |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 10133 | btrfs_end_transaction(trans); |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 10134 | if (ret && inode) |
| 10135 | discard_new_inode(inode); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 10136 | btrfs_btree_balance_dirty(fs_info); |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 10137 | return ret; |
| 10138 | } |
| 10139 | |
Qu Wenruo | d2a9106 | 2021-05-31 16:50:49 +0800 | [diff] [blame] | 10140 | 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] | 10141 | { |
Qu Wenruo | d2a9106 | 2021-05-31 16:50:49 +0800 | [diff] [blame] | 10142 | struct btrfs_fs_info *fs_info = inode->root->fs_info; |
Josef Bacik | c6100a4 | 2017-05-05 11:57:13 -0400 | [diff] [blame] | 10143 | unsigned long index = start >> PAGE_SHIFT; |
| 10144 | unsigned long end_index = end >> PAGE_SHIFT; |
| 10145 | struct page *page; |
Qu Wenruo | d2a9106 | 2021-05-31 16:50:49 +0800 | [diff] [blame] | 10146 | u32 len; |
Josef Bacik | c6100a4 | 2017-05-05 11:57:13 -0400 | [diff] [blame] | 10147 | |
Qu Wenruo | d2a9106 | 2021-05-31 16:50:49 +0800 | [diff] [blame] | 10148 | ASSERT(end + 1 - start <= U32_MAX); |
| 10149 | len = end + 1 - start; |
Josef Bacik | c6100a4 | 2017-05-05 11:57:13 -0400 | [diff] [blame] | 10150 | while (index <= end_index) { |
Qu Wenruo | d2a9106 | 2021-05-31 16:50:49 +0800 | [diff] [blame] | 10151 | page = find_get_page(inode->vfs_inode.i_mapping, index); |
Josef Bacik | c6100a4 | 2017-05-05 11:57:13 -0400 | [diff] [blame] | 10152 | ASSERT(page); /* Pages should be in the extent_io_tree */ |
Qu Wenruo | d2a9106 | 2021-05-31 16:50:49 +0800 | [diff] [blame] | 10153 | |
| 10154 | btrfs_page_set_writeback(fs_info, page, start, len); |
Josef Bacik | c6100a4 | 2017-05-05 11:57:13 -0400 | [diff] [blame] | 10155 | put_page(page); |
| 10156 | index++; |
| 10157 | } |
| 10158 | } |
| 10159 | |
Omar Sandoval | ed46ff3 | 2016-11-03 10:28:14 -0700 | [diff] [blame] | 10160 | #ifdef CONFIG_SWAP |
| 10161 | /* |
| 10162 | * Add an entry indicating a block group or device which is pinned by a |
| 10163 | * swapfile. Returns 0 on success, 1 if there is already an entry for it, or a |
| 10164 | * negative errno on failure. |
| 10165 | */ |
| 10166 | static int btrfs_add_swapfile_pin(struct inode *inode, void *ptr, |
| 10167 | bool is_block_group) |
| 10168 | { |
| 10169 | struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info; |
| 10170 | struct btrfs_swapfile_pin *sp, *entry; |
| 10171 | struct rb_node **p; |
| 10172 | struct rb_node *parent = NULL; |
| 10173 | |
| 10174 | sp = kmalloc(sizeof(*sp), GFP_NOFS); |
| 10175 | if (!sp) |
| 10176 | return -ENOMEM; |
| 10177 | sp->ptr = ptr; |
| 10178 | sp->inode = inode; |
| 10179 | sp->is_block_group = is_block_group; |
Filipe Manana | 195a49e | 2021-02-05 12:55:37 +0000 | [diff] [blame] | 10180 | sp->bg_extent_count = 1; |
Omar Sandoval | ed46ff3 | 2016-11-03 10:28:14 -0700 | [diff] [blame] | 10181 | |
| 10182 | spin_lock(&fs_info->swapfile_pins_lock); |
| 10183 | p = &fs_info->swapfile_pins.rb_node; |
| 10184 | while (*p) { |
| 10185 | parent = *p; |
| 10186 | entry = rb_entry(parent, struct btrfs_swapfile_pin, node); |
| 10187 | if (sp->ptr < entry->ptr || |
| 10188 | (sp->ptr == entry->ptr && sp->inode < entry->inode)) { |
| 10189 | p = &(*p)->rb_left; |
| 10190 | } else if (sp->ptr > entry->ptr || |
| 10191 | (sp->ptr == entry->ptr && sp->inode > entry->inode)) { |
| 10192 | p = &(*p)->rb_right; |
| 10193 | } else { |
Filipe Manana | 195a49e | 2021-02-05 12:55:37 +0000 | [diff] [blame] | 10194 | if (is_block_group) |
| 10195 | entry->bg_extent_count++; |
Omar Sandoval | ed46ff3 | 2016-11-03 10:28:14 -0700 | [diff] [blame] | 10196 | spin_unlock(&fs_info->swapfile_pins_lock); |
| 10197 | kfree(sp); |
| 10198 | return 1; |
| 10199 | } |
| 10200 | } |
| 10201 | rb_link_node(&sp->node, parent, p); |
| 10202 | rb_insert_color(&sp->node, &fs_info->swapfile_pins); |
| 10203 | spin_unlock(&fs_info->swapfile_pins_lock); |
| 10204 | return 0; |
| 10205 | } |
| 10206 | |
| 10207 | /* Free all of the entries pinned by this swapfile. */ |
| 10208 | static void btrfs_free_swapfile_pins(struct inode *inode) |
| 10209 | { |
| 10210 | struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info; |
| 10211 | struct btrfs_swapfile_pin *sp; |
| 10212 | struct rb_node *node, *next; |
| 10213 | |
| 10214 | spin_lock(&fs_info->swapfile_pins_lock); |
| 10215 | node = rb_first(&fs_info->swapfile_pins); |
| 10216 | while (node) { |
| 10217 | next = rb_next(node); |
| 10218 | sp = rb_entry(node, struct btrfs_swapfile_pin, node); |
| 10219 | if (sp->inode == inode) { |
| 10220 | rb_erase(&sp->node, &fs_info->swapfile_pins); |
Filipe Manana | 195a49e | 2021-02-05 12:55:37 +0000 | [diff] [blame] | 10221 | if (sp->is_block_group) { |
| 10222 | btrfs_dec_block_group_swap_extents(sp->ptr, |
| 10223 | sp->bg_extent_count); |
Omar Sandoval | ed46ff3 | 2016-11-03 10:28:14 -0700 | [diff] [blame] | 10224 | btrfs_put_block_group(sp->ptr); |
Filipe Manana | 195a49e | 2021-02-05 12:55:37 +0000 | [diff] [blame] | 10225 | } |
Omar Sandoval | ed46ff3 | 2016-11-03 10:28:14 -0700 | [diff] [blame] | 10226 | kfree(sp); |
| 10227 | } |
| 10228 | node = next; |
| 10229 | } |
| 10230 | spin_unlock(&fs_info->swapfile_pins_lock); |
| 10231 | } |
| 10232 | |
| 10233 | struct btrfs_swap_info { |
| 10234 | u64 start; |
| 10235 | u64 block_start; |
| 10236 | u64 block_len; |
| 10237 | u64 lowest_ppage; |
| 10238 | u64 highest_ppage; |
| 10239 | unsigned long nr_pages; |
| 10240 | int nr_extents; |
| 10241 | }; |
| 10242 | |
| 10243 | static int btrfs_add_swap_extent(struct swap_info_struct *sis, |
| 10244 | struct btrfs_swap_info *bsi) |
| 10245 | { |
| 10246 | unsigned long nr_pages; |
| 10247 | u64 first_ppage, first_ppage_reported, next_ppage; |
| 10248 | int ret; |
| 10249 | |
| 10250 | first_ppage = ALIGN(bsi->block_start, PAGE_SIZE) >> PAGE_SHIFT; |
| 10251 | next_ppage = ALIGN_DOWN(bsi->block_start + bsi->block_len, |
| 10252 | PAGE_SIZE) >> PAGE_SHIFT; |
| 10253 | |
| 10254 | if (first_ppage >= next_ppage) |
| 10255 | return 0; |
| 10256 | nr_pages = next_ppage - first_ppage; |
| 10257 | |
| 10258 | first_ppage_reported = first_ppage; |
| 10259 | if (bsi->start == 0) |
| 10260 | first_ppage_reported++; |
| 10261 | if (bsi->lowest_ppage > first_ppage_reported) |
| 10262 | bsi->lowest_ppage = first_ppage_reported; |
| 10263 | if (bsi->highest_ppage < (next_ppage - 1)) |
| 10264 | bsi->highest_ppage = next_ppage - 1; |
| 10265 | |
| 10266 | ret = add_swap_extent(sis, bsi->nr_pages, nr_pages, first_ppage); |
| 10267 | if (ret < 0) |
| 10268 | return ret; |
| 10269 | bsi->nr_extents += ret; |
| 10270 | bsi->nr_pages += nr_pages; |
| 10271 | return 0; |
| 10272 | } |
| 10273 | |
| 10274 | static void btrfs_swap_deactivate(struct file *file) |
| 10275 | { |
| 10276 | struct inode *inode = file_inode(file); |
| 10277 | |
| 10278 | btrfs_free_swapfile_pins(inode); |
| 10279 | atomic_dec(&BTRFS_I(inode)->root->nr_swapfiles); |
| 10280 | } |
| 10281 | |
| 10282 | static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file, |
| 10283 | sector_t *span) |
| 10284 | { |
| 10285 | struct inode *inode = file_inode(file); |
Filipe Manana | dd0734f | 2021-02-05 12:55:38 +0000 | [diff] [blame] | 10286 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 10287 | struct btrfs_fs_info *fs_info = root->fs_info; |
Omar Sandoval | ed46ff3 | 2016-11-03 10:28:14 -0700 | [diff] [blame] | 10288 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
| 10289 | struct extent_state *cached_state = NULL; |
| 10290 | struct extent_map *em = NULL; |
| 10291 | struct btrfs_device *device = NULL; |
| 10292 | struct btrfs_swap_info bsi = { |
| 10293 | .lowest_ppage = (sector_t)-1ULL, |
| 10294 | }; |
| 10295 | int ret = 0; |
| 10296 | u64 isize; |
| 10297 | u64 start; |
| 10298 | |
| 10299 | /* |
| 10300 | * If the swap file was just created, make sure delalloc is done. If the |
| 10301 | * file changes again after this, the user is doing something stupid and |
| 10302 | * we don't really care. |
| 10303 | */ |
| 10304 | ret = btrfs_wait_ordered_range(inode, 0, (u64)-1); |
| 10305 | if (ret) |
| 10306 | return ret; |
| 10307 | |
| 10308 | /* |
| 10309 | * The inode is locked, so these flags won't change after we check them. |
| 10310 | */ |
| 10311 | if (BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS) { |
| 10312 | btrfs_warn(fs_info, "swapfile must not be compressed"); |
| 10313 | return -EINVAL; |
| 10314 | } |
| 10315 | if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW)) { |
| 10316 | btrfs_warn(fs_info, "swapfile must not be copy-on-write"); |
| 10317 | return -EINVAL; |
| 10318 | } |
| 10319 | if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) { |
| 10320 | btrfs_warn(fs_info, "swapfile must not be checksummed"); |
| 10321 | return -EINVAL; |
| 10322 | } |
| 10323 | |
| 10324 | /* |
| 10325 | * Balance or device remove/replace/resize can move stuff around from |
Goldwyn Rodrigues | c3e1f96 | 2020-08-25 10:02:32 -0500 | [diff] [blame] | 10326 | * under us. The exclop protection makes sure they aren't running/won't |
| 10327 | * run concurrently while we are mapping the swap extents, and |
| 10328 | * fs_info->swapfile_pins prevents them from running while the swap |
| 10329 | * file is active and moving the extents. Note that this also prevents |
| 10330 | * 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] | 10331 | * really worth the trouble to allow it. |
| 10332 | */ |
Goldwyn Rodrigues | c3e1f96 | 2020-08-25 10:02:32 -0500 | [diff] [blame] | 10333 | if (!btrfs_exclop_start(fs_info, BTRFS_EXCLOP_SWAP_ACTIVATE)) { |
Omar Sandoval | ed46ff3 | 2016-11-03 10:28:14 -0700 | [diff] [blame] | 10334 | btrfs_warn(fs_info, |
| 10335 | "cannot activate swapfile while exclusive operation is running"); |
| 10336 | return -EBUSY; |
| 10337 | } |
Filipe Manana | dd0734f | 2021-02-05 12:55:38 +0000 | [diff] [blame] | 10338 | |
| 10339 | /* |
| 10340 | * Prevent snapshot creation while we are activating the swap file. |
| 10341 | * We do not want to race with snapshot creation. If snapshot creation |
| 10342 | * already started before we bumped nr_swapfiles from 0 to 1 and |
| 10343 | * completes before the first write into the swap file after it is |
| 10344 | * activated, than that write would fallback to COW. |
| 10345 | */ |
| 10346 | if (!btrfs_drew_try_write_lock(&root->snapshot_lock)) { |
| 10347 | btrfs_exclop_finish(fs_info); |
| 10348 | btrfs_warn(fs_info, |
| 10349 | "cannot activate swapfile because snapshot creation is in progress"); |
| 10350 | return -EINVAL; |
| 10351 | } |
Omar Sandoval | ed46ff3 | 2016-11-03 10:28:14 -0700 | [diff] [blame] | 10352 | /* |
| 10353 | * Snapshots can create extents which require COW even if NODATACOW is |
| 10354 | * set. We use this counter to prevent snapshots. We must increment it |
| 10355 | * before walking the extents because we don't want a concurrent |
| 10356 | * snapshot to run after we've already checked the extents. |
| 10357 | */ |
Filipe Manana | dd0734f | 2021-02-05 12:55:38 +0000 | [diff] [blame] | 10358 | atomic_inc(&root->nr_swapfiles); |
Omar Sandoval | ed46ff3 | 2016-11-03 10:28:14 -0700 | [diff] [blame] | 10359 | |
| 10360 | isize = ALIGN_DOWN(inode->i_size, fs_info->sectorsize); |
| 10361 | |
| 10362 | lock_extent_bits(io_tree, 0, isize - 1, &cached_state); |
| 10363 | start = 0; |
| 10364 | while (start < isize) { |
| 10365 | u64 logical_block_start, physical_block_start; |
David Sterba | 32da5386 | 2019-10-29 19:20:18 +0100 | [diff] [blame] | 10366 | struct btrfs_block_group *bg; |
Omar Sandoval | ed46ff3 | 2016-11-03 10:28:14 -0700 | [diff] [blame] | 10367 | u64 len = isize - start; |
| 10368 | |
Omar Sandoval | 39b07b5 | 2019-12-02 17:34:23 -0800 | [diff] [blame] | 10369 | em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len); |
Omar Sandoval | ed46ff3 | 2016-11-03 10:28:14 -0700 | [diff] [blame] | 10370 | if (IS_ERR(em)) { |
| 10371 | ret = PTR_ERR(em); |
| 10372 | goto out; |
| 10373 | } |
| 10374 | |
| 10375 | if (em->block_start == EXTENT_MAP_HOLE) { |
| 10376 | btrfs_warn(fs_info, "swapfile must not have holes"); |
| 10377 | ret = -EINVAL; |
| 10378 | goto out; |
| 10379 | } |
| 10380 | if (em->block_start == EXTENT_MAP_INLINE) { |
| 10381 | /* |
| 10382 | * It's unlikely we'll ever actually find ourselves |
| 10383 | * here, as a file small enough to fit inline won't be |
| 10384 | * big enough to store more than the swap header, but in |
| 10385 | * case something changes in the future, let's catch it |
| 10386 | * here rather than later. |
| 10387 | */ |
| 10388 | btrfs_warn(fs_info, "swapfile must not be inline"); |
| 10389 | ret = -EINVAL; |
| 10390 | goto out; |
| 10391 | } |
| 10392 | if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) { |
| 10393 | btrfs_warn(fs_info, "swapfile must not be compressed"); |
| 10394 | ret = -EINVAL; |
| 10395 | goto out; |
| 10396 | } |
| 10397 | |
| 10398 | logical_block_start = em->block_start + (start - em->start); |
| 10399 | len = min(len, em->len - (start - em->start)); |
| 10400 | free_extent_map(em); |
| 10401 | em = NULL; |
| 10402 | |
Boris Burkov | a84d5d4 | 2020-08-18 11:00:05 -0700 | [diff] [blame] | 10403 | ret = can_nocow_extent(inode, start, &len, NULL, NULL, NULL, true); |
Omar Sandoval | ed46ff3 | 2016-11-03 10:28:14 -0700 | [diff] [blame] | 10404 | if (ret < 0) { |
| 10405 | goto out; |
| 10406 | } else if (ret) { |
| 10407 | ret = 0; |
| 10408 | } else { |
| 10409 | btrfs_warn(fs_info, |
| 10410 | "swapfile must not be copy-on-write"); |
| 10411 | ret = -EINVAL; |
| 10412 | goto out; |
| 10413 | } |
| 10414 | |
| 10415 | em = btrfs_get_chunk_map(fs_info, logical_block_start, len); |
| 10416 | if (IS_ERR(em)) { |
| 10417 | ret = PTR_ERR(em); |
| 10418 | goto out; |
| 10419 | } |
| 10420 | |
| 10421 | if (em->map_lookup->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) { |
| 10422 | btrfs_warn(fs_info, |
| 10423 | "swapfile must have single data profile"); |
| 10424 | ret = -EINVAL; |
| 10425 | goto out; |
| 10426 | } |
| 10427 | |
| 10428 | if (device == NULL) { |
| 10429 | device = em->map_lookup->stripes[0].dev; |
| 10430 | ret = btrfs_add_swapfile_pin(inode, device, false); |
| 10431 | if (ret == 1) |
| 10432 | ret = 0; |
| 10433 | else if (ret) |
| 10434 | goto out; |
| 10435 | } else if (device != em->map_lookup->stripes[0].dev) { |
| 10436 | btrfs_warn(fs_info, "swapfile must be on one device"); |
| 10437 | ret = -EINVAL; |
| 10438 | goto out; |
| 10439 | } |
| 10440 | |
| 10441 | physical_block_start = (em->map_lookup->stripes[0].physical + |
| 10442 | (logical_block_start - em->start)); |
| 10443 | len = min(len, em->len - (logical_block_start - em->start)); |
| 10444 | free_extent_map(em); |
| 10445 | em = NULL; |
| 10446 | |
| 10447 | bg = btrfs_lookup_block_group(fs_info, logical_block_start); |
| 10448 | if (!bg) { |
| 10449 | btrfs_warn(fs_info, |
| 10450 | "could not find block group containing swapfile"); |
| 10451 | ret = -EINVAL; |
| 10452 | goto out; |
| 10453 | } |
| 10454 | |
Filipe Manana | 195a49e | 2021-02-05 12:55:37 +0000 | [diff] [blame] | 10455 | if (!btrfs_inc_block_group_swap_extents(bg)) { |
| 10456 | btrfs_warn(fs_info, |
| 10457 | "block group for swapfile at %llu is read-only%s", |
| 10458 | bg->start, |
| 10459 | atomic_read(&fs_info->scrubs_running) ? |
| 10460 | " (scrub running)" : ""); |
| 10461 | btrfs_put_block_group(bg); |
| 10462 | ret = -EINVAL; |
| 10463 | goto out; |
| 10464 | } |
| 10465 | |
Omar Sandoval | ed46ff3 | 2016-11-03 10:28:14 -0700 | [diff] [blame] | 10466 | ret = btrfs_add_swapfile_pin(inode, bg, true); |
| 10467 | if (ret) { |
| 10468 | btrfs_put_block_group(bg); |
| 10469 | if (ret == 1) |
| 10470 | ret = 0; |
| 10471 | else |
| 10472 | goto out; |
| 10473 | } |
| 10474 | |
| 10475 | if (bsi.block_len && |
| 10476 | bsi.block_start + bsi.block_len == physical_block_start) { |
| 10477 | bsi.block_len += len; |
| 10478 | } else { |
| 10479 | if (bsi.block_len) { |
| 10480 | ret = btrfs_add_swap_extent(sis, &bsi); |
| 10481 | if (ret) |
| 10482 | goto out; |
| 10483 | } |
| 10484 | bsi.start = start; |
| 10485 | bsi.block_start = physical_block_start; |
| 10486 | bsi.block_len = len; |
| 10487 | } |
| 10488 | |
| 10489 | start += len; |
| 10490 | } |
| 10491 | |
| 10492 | if (bsi.block_len) |
| 10493 | ret = btrfs_add_swap_extent(sis, &bsi); |
| 10494 | |
| 10495 | out: |
| 10496 | if (!IS_ERR_OR_NULL(em)) |
| 10497 | free_extent_map(em); |
| 10498 | |
| 10499 | unlock_extent_cached(io_tree, 0, isize - 1, &cached_state); |
| 10500 | |
| 10501 | if (ret) |
| 10502 | btrfs_swap_deactivate(file); |
| 10503 | |
Filipe Manana | dd0734f | 2021-02-05 12:55:38 +0000 | [diff] [blame] | 10504 | btrfs_drew_write_unlock(&root->snapshot_lock); |
| 10505 | |
Goldwyn Rodrigues | c3e1f96 | 2020-08-25 10:02:32 -0500 | [diff] [blame] | 10506 | btrfs_exclop_finish(fs_info); |
Omar Sandoval | ed46ff3 | 2016-11-03 10:28:14 -0700 | [diff] [blame] | 10507 | |
| 10508 | if (ret) |
| 10509 | return ret; |
| 10510 | |
| 10511 | if (device) |
| 10512 | sis->bdev = device->bdev; |
| 10513 | *span = bsi.highest_ppage - bsi.lowest_ppage + 1; |
| 10514 | sis->max = bsi.nr_pages; |
| 10515 | sis->pages = bsi.nr_pages - 1; |
| 10516 | sis->highest_bit = bsi.nr_pages - 1; |
| 10517 | return bsi.nr_extents; |
| 10518 | } |
| 10519 | #else |
| 10520 | static void btrfs_swap_deactivate(struct file *file) |
| 10521 | { |
| 10522 | } |
| 10523 | |
| 10524 | static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file, |
| 10525 | sector_t *span) |
| 10526 | { |
| 10527 | return -EOPNOTSUPP; |
| 10528 | } |
| 10529 | #endif |
| 10530 | |
Filipe Manana | 2766ff6 | 2020-11-04 11:07:34 +0000 | [diff] [blame] | 10531 | /* |
| 10532 | * Update the number of bytes used in the VFS' inode. When we replace extents in |
| 10533 | * a range (clone, dedupe, fallocate's zero range), we must update the number of |
| 10534 | * bytes used by the inode in an atomic manner, so that concurrent stat(2) calls |
| 10535 | * always get a correct value. |
| 10536 | */ |
| 10537 | void btrfs_update_inode_bytes(struct btrfs_inode *inode, |
| 10538 | const u64 add_bytes, |
| 10539 | const u64 del_bytes) |
| 10540 | { |
| 10541 | if (add_bytes == del_bytes) |
| 10542 | return; |
| 10543 | |
| 10544 | spin_lock(&inode->lock); |
| 10545 | if (del_bytes > 0) |
| 10546 | inode_sub_bytes(&inode->vfs_inode, del_bytes); |
| 10547 | if (add_bytes > 0) |
| 10548 | inode_add_bytes(&inode->vfs_inode, add_bytes); |
| 10549 | spin_unlock(&inode->lock); |
| 10550 | } |
| 10551 | |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 10552 | static const struct inode_operations btrfs_dir_inode_operations = { |
Chris Mason | 3394e16 | 2008-11-17 20:42:26 -0500 | [diff] [blame] | 10553 | .getattr = btrfs_getattr, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10554 | .lookup = btrfs_lookup, |
| 10555 | .create = btrfs_create, |
| 10556 | .unlink = btrfs_unlink, |
| 10557 | .link = btrfs_link, |
| 10558 | .mkdir = btrfs_mkdir, |
| 10559 | .rmdir = btrfs_rmdir, |
Miklos Szeredi | 2773bf0 | 2016-09-27 11:03:58 +0200 | [diff] [blame] | 10560 | .rename = btrfs_rename2, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10561 | .symlink = btrfs_symlink, |
| 10562 | .setattr = btrfs_setattr, |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 10563 | .mknod = btrfs_mknod, |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 10564 | .listxattr = btrfs_listxattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 10565 | .permission = btrfs_permission, |
Christoph Hellwig | 4e34e71 | 2011-07-23 17:37:31 +0200 | [diff] [blame] | 10566 | .get_acl = btrfs_get_acl, |
Christoph Hellwig | 996a710 | 2013-12-20 05:16:43 -0800 | [diff] [blame] | 10567 | .set_acl = btrfs_set_acl, |
Guangyu Sun | 93fd63c | 2013-09-16 10:42:03 -0700 | [diff] [blame] | 10568 | .update_time = btrfs_update_time, |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 10569 | .tmpfile = btrfs_tmpfile, |
Miklos Szeredi | 97fc297 | 2021-04-07 14:36:43 +0200 | [diff] [blame] | 10570 | .fileattr_get = btrfs_fileattr_get, |
| 10571 | .fileattr_set = btrfs_fileattr_set, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10572 | }; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 10573 | |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 10574 | static const struct file_operations btrfs_dir_file_operations = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10575 | .llseek = generic_file_llseek, |
| 10576 | .read = generic_read_dir, |
Omar Sandoval | 02dbfc9 | 2016-05-20 13:50:33 -0700 | [diff] [blame] | 10577 | .iterate_shared = btrfs_real_readdir, |
Josef Bacik | 23b5ec7 | 2017-07-24 15:14:25 -0400 | [diff] [blame] | 10578 | .open = btrfs_opendir, |
Christoph Hellwig | 34287aa | 2007-09-14 10:22:47 -0400 | [diff] [blame] | 10579 | .unlocked_ioctl = btrfs_ioctl, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10580 | #ifdef CONFIG_COMPAT |
Luke Dashjr | 4c63c24 | 2015-10-29 08:22:21 +0000 | [diff] [blame] | 10581 | .compat_ioctl = btrfs_compat_ioctl, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10582 | #endif |
Sage Weil | 6bf13c0 | 2008-06-10 10:07:39 -0400 | [diff] [blame] | 10583 | .release = btrfs_release_file, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 10584 | .fsync = btrfs_sync_file, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10585 | }; |
| 10586 | |
Chris Mason | 3505439 | 2009-01-21 13:11:13 -0500 | [diff] [blame] | 10587 | /* |
| 10588 | * btrfs doesn't support the bmap operation because swapfiles |
| 10589 | * use bmap to make a mapping of extents in the file. They assume |
| 10590 | * these extents won't change over the life of the file and they |
| 10591 | * use the bmap result to do IO directly to the drive. |
| 10592 | * |
| 10593 | * the btrfs bmap call would return logical addresses that aren't |
| 10594 | * suitable for IO and they also will change frequently as COW |
| 10595 | * operations happen. So, swapfile + btrfs == corruption. |
| 10596 | * |
| 10597 | * For now we're avoiding this by dropping bmap. |
| 10598 | */ |
Alexey Dobriyan | 7f09410 | 2009-09-21 17:01:10 -0700 | [diff] [blame] | 10599 | static const struct address_space_operations btrfs_aops = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10600 | .readpage = btrfs_readpage, |
| 10601 | .writepage = btrfs_writepage, |
Chris Mason | b293f02e | 2007-11-01 19:45:34 -0400 | [diff] [blame] | 10602 | .writepages = btrfs_writepages, |
Matthew Wilcox (Oracle) | ba206a0 | 2020-06-01 21:47:05 -0700 | [diff] [blame] | 10603 | .readahead = btrfs_readahead, |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 10604 | .direct_IO = noop_direct_IO, |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 10605 | .invalidatepage = btrfs_invalidatepage, |
| 10606 | .releasepage = btrfs_releasepage, |
Roman Gushchin | f8e6608 | 2020-03-04 16:57:35 -0800 | [diff] [blame] | 10607 | #ifdef CONFIG_MIGRATION |
| 10608 | .migratepage = btrfs_migratepage, |
| 10609 | #endif |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 10610 | .set_page_dirty = btrfs_set_page_dirty, |
Andi Kleen | 465fdd9 | 2009-09-16 11:50:18 +0200 | [diff] [blame] | 10611 | .error_remove_page = generic_error_remove_page, |
Omar Sandoval | ed46ff3 | 2016-11-03 10:28:14 -0700 | [diff] [blame] | 10612 | .swap_activate = btrfs_swap_activate, |
| 10613 | .swap_deactivate = btrfs_swap_deactivate, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10614 | }; |
| 10615 | |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 10616 | static const struct inode_operations btrfs_file_inode_operations = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10617 | .getattr = btrfs_getattr, |
| 10618 | .setattr = btrfs_setattr, |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 10619 | .listxattr = btrfs_listxattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 10620 | .permission = btrfs_permission, |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 10621 | .fiemap = btrfs_fiemap, |
Christoph Hellwig | 4e34e71 | 2011-07-23 17:37:31 +0200 | [diff] [blame] | 10622 | .get_acl = btrfs_get_acl, |
Christoph Hellwig | 996a710 | 2013-12-20 05:16:43 -0800 | [diff] [blame] | 10623 | .set_acl = btrfs_set_acl, |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 10624 | .update_time = btrfs_update_time, |
Miklos Szeredi | 97fc297 | 2021-04-07 14:36:43 +0200 | [diff] [blame] | 10625 | .fileattr_get = btrfs_fileattr_get, |
| 10626 | .fileattr_set = btrfs_fileattr_set, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10627 | }; |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 10628 | static const struct inode_operations btrfs_special_inode_operations = { |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 10629 | .getattr = btrfs_getattr, |
| 10630 | .setattr = btrfs_setattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 10631 | .permission = btrfs_permission, |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 10632 | .listxattr = btrfs_listxattr, |
Christoph Hellwig | 4e34e71 | 2011-07-23 17:37:31 +0200 | [diff] [blame] | 10633 | .get_acl = btrfs_get_acl, |
Christoph Hellwig | 996a710 | 2013-12-20 05:16:43 -0800 | [diff] [blame] | 10634 | .set_acl = btrfs_set_acl, |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 10635 | .update_time = btrfs_update_time, |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 10636 | }; |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 10637 | static const struct inode_operations btrfs_symlink_inode_operations = { |
Al Viro | 6b25539 | 2015-11-17 10:20:54 -0500 | [diff] [blame] | 10638 | .get_link = page_get_link, |
Li Zefan | f209561 | 2010-11-19 02:05:24 +0000 | [diff] [blame] | 10639 | .getattr = btrfs_getattr, |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 10640 | .setattr = btrfs_setattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 10641 | .permission = btrfs_permission, |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 10642 | .listxattr = btrfs_listxattr, |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 10643 | .update_time = btrfs_update_time, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10644 | }; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 10645 | |
Alexey Dobriyan | 82d339d | 2009-10-09 09:54:36 -0400 | [diff] [blame] | 10646 | const struct dentry_operations btrfs_dentry_operations = { |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 10647 | .d_delete = btrfs_dentry_delete, |
| 10648 | }; |