David Sterba | c1d7c51 | 2018-04-03 19:23:33 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Chris Mason | 6cbd557 | 2007-06-12 09:07:21 -0400 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2007 Oracle. All rights reserved. |
Chris Mason | 6cbd557 | 2007-06-12 09:07:21 -0400 | [diff] [blame] | 4 | */ |
| 5 | |
Herbert Xu | 7999096 | 2020-06-12 16:57:37 +1000 | [diff] [blame] | 6 | #include <crypto/hash.h> |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 7 | #include <linux/kernel.h> |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 8 | #include <linux/bio.h> |
Sage Weil | f2eb0a2 | 2008-05-02 14:43:14 -0400 | [diff] [blame] | 9 | #include <linux/file.h> |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10 | #include <linux/fs.h> |
| 11 | #include <linux/pagemap.h> |
| 12 | #include <linux/highmem.h> |
| 13 | #include <linux/time.h> |
| 14 | #include <linux/init.h> |
| 15 | #include <linux/string.h> |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 16 | #include <linux/backing-dev.h> |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 17 | #include <linux/writeback.h> |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 18 | #include <linux/compat.h> |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 19 | #include <linux/xattr.h> |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 20 | #include <linux/posix_acl.h> |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 21 | #include <linux/falloc.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 22 | #include <linux/slab.h> |
David Sterba | 7a36dde | 2011-05-06 15:33:15 +0200 | [diff] [blame] | 23 | #include <linux/ratelimit.h> |
Filipe Brandenburger | 55e301f | 2013-01-29 06:04:50 +0000 | [diff] [blame] | 24 | #include <linux/btrfs.h> |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 25 | #include <linux/blkdev.h> |
Josef Bacik | f23b5a5 | 2013-06-19 10:16:26 -0400 | [diff] [blame] | 26 | #include <linux/posix_acl_xattr.h> |
Christoph Hellwig | e2e40f2 | 2015-02-22 08:58:50 -0800 | [diff] [blame] | 27 | #include <linux/uio.h> |
Josef Bacik | 69fe2d7 | 2017-10-19 14:15:57 -0400 | [diff] [blame] | 28 | #include <linux/magic.h> |
Jeff Layton | ae5e165 | 2018-01-29 06:41:30 -0500 | [diff] [blame] | 29 | #include <linux/iversion.h> |
Omar Sandoval | ed46ff3 | 2016-11-03 10:28:14 -0700 | [diff] [blame] | 30 | #include <linux/swap.h> |
Roman Gushchin | f8e6608 | 2020-03-04 16:57:35 -0800 | [diff] [blame] | 31 | #include <linux/migrate.h> |
Nikolay Borisov | b1c16ac | 2019-04-01 11:29:57 +0300 | [diff] [blame] | 32 | #include <linux/sched/mm.h> |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 33 | #include <linux/iomap.h> |
David Sterba | 92d3217 | 2018-04-16 21:10:14 +0200 | [diff] [blame] | 34 | #include <asm/unaligned.h> |
David Sterba | 602cbe9 | 2019-08-21 18:48:25 +0200 | [diff] [blame] | 35 | #include "misc.h" |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 36 | #include "ctree.h" |
| 37 | #include "disk-io.h" |
| 38 | #include "transaction.h" |
| 39 | #include "btrfs_inode.h" |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 40 | #include "print-tree.h" |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 41 | #include "ordered-data.h" |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 42 | #include "xattr.h" |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 43 | #include "tree-log.h" |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 44 | #include "volumes.h" |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 45 | #include "compression.h" |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 46 | #include "locking.h" |
Josef Bacik | dc89e98 | 2011-01-28 17:05:48 -0500 | [diff] [blame] | 47 | #include "free-space-cache.h" |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 48 | #include "props.h" |
Dongsheng Yang | 3119321 | 2014-12-12 16:44:35 +0800 | [diff] [blame] | 49 | #include "qgroup.h" |
Josef Bacik | 8673634 | 2019-06-19 15:12:00 -0400 | [diff] [blame] | 50 | #include "delalloc-space.h" |
Josef Bacik | aac0023 | 2019-06-20 15:37:44 -0400 | [diff] [blame] | 51 | #include "block-group.h" |
Filipe Manana | 467dc47 | 2020-05-27 11:16:07 +0100 | [diff] [blame] | 52 | #include "space-info.h" |
Naohiro Aota | d8e3fb1 | 2021-02-04 19:22:05 +0900 | [diff] [blame] | 53 | #include "zoned.h" |
Qu Wenruo | b945a46 | 2021-05-31 16:50:46 +0800 | [diff] [blame] | 54 | #include "subpage.h" |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 55 | |
| 56 | struct btrfs_iget_args { |
David Sterba | 0202e83 | 2020-05-15 19:35:59 +0200 | [diff] [blame] | 57 | u64 ino; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 58 | struct btrfs_root *root; |
| 59 | }; |
| 60 | |
Filipe Manana | f28a492 | 2015-12-08 19:23:20 +0000 | [diff] [blame] | 61 | struct btrfs_dio_data { |
Filipe Manana | f28a492 | 2015-12-08 19:23:20 +0000 | [diff] [blame] | 62 | u64 reserve; |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 63 | loff_t length; |
| 64 | ssize_t submitted; |
| 65 | struct extent_changeset *data_reserved; |
Filipe Manana | f28a492 | 2015-12-08 19:23:20 +0000 | [diff] [blame] | 66 | }; |
| 67 | |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 68 | static const struct inode_operations btrfs_dir_inode_operations; |
| 69 | static const struct inode_operations btrfs_symlink_inode_operations; |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 70 | static const struct inode_operations btrfs_special_inode_operations; |
| 71 | static const struct inode_operations btrfs_file_inode_operations; |
Alexey Dobriyan | 7f09410 | 2009-09-21 17:01:10 -0700 | [diff] [blame] | 72 | static const struct address_space_operations btrfs_aops; |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 73 | static const struct file_operations btrfs_dir_file_operations; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 74 | |
| 75 | static struct kmem_cache *btrfs_inode_cachep; |
| 76 | struct kmem_cache *btrfs_trans_handle_cachep; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 77 | struct kmem_cache *btrfs_path_cachep; |
Josef Bacik | dc89e98 | 2011-01-28 17:05:48 -0500 | [diff] [blame] | 78 | struct kmem_cache *btrfs_free_space_cachep; |
Christophe Leroy | 3acd485 | 2019-08-21 15:05:55 +0000 | [diff] [blame] | 79 | struct kmem_cache *btrfs_free_space_bitmap_cachep; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 80 | |
Eric Sandeen | 3972f26 | 2013-01-12 02:57:22 +0000 | [diff] [blame] | 81 | static int btrfs_setsize(struct inode *inode, struct iattr *attr); |
Filipe Manana | 213e8c5 | 2018-02-06 20:40:31 +0000 | [diff] [blame] | 82 | static int btrfs_truncate(struct inode *inode, bool skip_writeback); |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 83 | static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent); |
Nikolay Borisov | 6e26c44 | 2020-06-03 08:55:14 +0300 | [diff] [blame] | 84 | static noinline int cow_file_range(struct btrfs_inode *inode, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 85 | struct page *locked_page, |
Nikolay Borisov | 74e9194 | 2019-07-17 16:18:16 +0300 | [diff] [blame] | 86 | u64 start, u64 end, int *page_started, |
Nikolay Borisov | 330a582 | 2019-07-17 16:18:17 +0300 | [diff] [blame] | 87 | unsigned long *nr_written, int unlock); |
Nikolay Borisov | 4b67c11 | 2020-06-03 08:55:05 +0300 | [diff] [blame] | 88 | static struct extent_map *create_io_em(struct btrfs_inode *inode, u64 start, |
| 89 | u64 len, u64 orig_start, u64 block_start, |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 90 | u64 block_len, u64 orig_block_len, |
| 91 | u64 ram_bytes, int compress_type, |
| 92 | int type); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 93 | |
Nikolay Borisov | b672b5c | 2020-06-03 08:55:24 +0300 | [diff] [blame] | 94 | static void __endio_write_update_ordered(struct btrfs_inode *inode, |
Qu Wenruo | 52427260 | 2017-03-08 10:25:52 +0800 | [diff] [blame] | 95 | const u64 offset, const u64 bytes, |
| 96 | const bool uptodate); |
| 97 | |
| 98 | /* |
Goldwyn Rodrigues | a14b78a | 2020-09-24 11:39:16 -0500 | [diff] [blame] | 99 | * btrfs_inode_lock - lock inode i_rwsem based on arguments passed |
| 100 | * |
| 101 | * ilock_flags can have the following bit set: |
| 102 | * |
| 103 | * BTRFS_ILOCK_SHARED - acquire a shared lock on the inode |
| 104 | * BTRFS_ILOCK_TRY - try to acquire the lock, if fails on first attempt |
| 105 | * return -EAGAIN |
Josef Bacik | 8318ba7 | 2021-02-10 17:14:33 -0500 | [diff] [blame] | 106 | * BTRFS_ILOCK_MMAP - acquire a write lock on the i_mmap_lock |
Goldwyn Rodrigues | a14b78a | 2020-09-24 11:39:16 -0500 | [diff] [blame] | 107 | */ |
| 108 | int btrfs_inode_lock(struct inode *inode, unsigned int ilock_flags) |
| 109 | { |
| 110 | if (ilock_flags & BTRFS_ILOCK_SHARED) { |
| 111 | if (ilock_flags & BTRFS_ILOCK_TRY) { |
| 112 | if (!inode_trylock_shared(inode)) |
| 113 | return -EAGAIN; |
| 114 | else |
| 115 | return 0; |
| 116 | } |
| 117 | inode_lock_shared(inode); |
| 118 | } else { |
| 119 | if (ilock_flags & BTRFS_ILOCK_TRY) { |
| 120 | if (!inode_trylock(inode)) |
| 121 | return -EAGAIN; |
| 122 | else |
| 123 | return 0; |
| 124 | } |
| 125 | inode_lock(inode); |
| 126 | } |
Josef Bacik | 8318ba7 | 2021-02-10 17:14:33 -0500 | [diff] [blame] | 127 | if (ilock_flags & BTRFS_ILOCK_MMAP) |
| 128 | down_write(&BTRFS_I(inode)->i_mmap_lock); |
Goldwyn Rodrigues | a14b78a | 2020-09-24 11:39:16 -0500 | [diff] [blame] | 129 | return 0; |
| 130 | } |
| 131 | |
| 132 | /* |
| 133 | * btrfs_inode_unlock - unock inode i_rwsem |
| 134 | * |
| 135 | * ilock_flags should contain the same bits set as passed to btrfs_inode_lock() |
| 136 | * to decide whether the lock acquired is shared or exclusive. |
| 137 | */ |
| 138 | void btrfs_inode_unlock(struct inode *inode, unsigned int ilock_flags) |
| 139 | { |
Josef Bacik | 8318ba7 | 2021-02-10 17:14:33 -0500 | [diff] [blame] | 140 | if (ilock_flags & BTRFS_ILOCK_MMAP) |
| 141 | up_write(&BTRFS_I(inode)->i_mmap_lock); |
Goldwyn Rodrigues | a14b78a | 2020-09-24 11:39:16 -0500 | [diff] [blame] | 142 | if (ilock_flags & BTRFS_ILOCK_SHARED) |
| 143 | inode_unlock_shared(inode); |
| 144 | else |
| 145 | inode_unlock(inode); |
| 146 | } |
| 147 | |
| 148 | /* |
Qu Wenruo | 52427260 | 2017-03-08 10:25:52 +0800 | [diff] [blame] | 149 | * Cleanup all submitted ordered extents in specified range to handle errors |
Andrea Gelmini | 52042d8 | 2018-11-28 12:05:13 +0100 | [diff] [blame] | 150 | * from the btrfs_run_delalloc_range() callback. |
Qu Wenruo | 52427260 | 2017-03-08 10:25:52 +0800 | [diff] [blame] | 151 | * |
| 152 | * NOTE: caller must ensure that when an error happens, it can not call |
| 153 | * extent_clear_unlock_delalloc() to clear both the bits EXTENT_DO_ACCOUNTING |
| 154 | * and EXTENT_DELALLOC simultaneously, because that causes the reserved metadata |
| 155 | * 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] | 156 | * extent (btrfs_finish_ordered_io()). |
Qu Wenruo | 52427260 | 2017-03-08 10:25:52 +0800 | [diff] [blame] | 157 | */ |
Nikolay Borisov | 64e1db5 | 2020-06-03 08:55:25 +0300 | [diff] [blame] | 158 | static inline void btrfs_cleanup_ordered_extents(struct btrfs_inode *inode, |
Nikolay Borisov | d1051d6 | 2018-11-21 17:10:52 +0200 | [diff] [blame] | 159 | struct page *locked_page, |
| 160 | u64 offset, u64 bytes) |
Qu Wenruo | 52427260 | 2017-03-08 10:25:52 +0800 | [diff] [blame] | 161 | { |
Naohiro Aota | 63d71450c | 2017-09-01 17:58:47 +0900 | [diff] [blame] | 162 | unsigned long index = offset >> PAGE_SHIFT; |
| 163 | unsigned long end_index = (offset + bytes - 1) >> PAGE_SHIFT; |
Nikolay Borisov | d1051d6 | 2018-11-21 17:10:52 +0200 | [diff] [blame] | 164 | u64 page_start = page_offset(locked_page); |
| 165 | u64 page_end = page_start + PAGE_SIZE - 1; |
| 166 | |
Naohiro Aota | 63d71450c | 2017-09-01 17:58:47 +0900 | [diff] [blame] | 167 | struct page *page; |
| 168 | |
| 169 | while (index <= end_index) { |
Qu Wenruo | 968f256 | 2021-05-18 15:09:41 +0800 | [diff] [blame] | 170 | /* |
| 171 | * For locked page, we will call end_extent_writepage() on it |
| 172 | * in run_delalloc_range() for the error handling. That |
| 173 | * end_extent_writepage() function will call |
| 174 | * btrfs_mark_ordered_io_finished() to clear page Ordered and |
| 175 | * run the ordered extent accounting. |
| 176 | * |
| 177 | * Here we can't just clear the Ordered bit, or |
| 178 | * btrfs_mark_ordered_io_finished() would skip the accounting |
| 179 | * for the page range, and the ordered extent will never finish. |
| 180 | */ |
| 181 | if (index == (page_offset(locked_page) >> PAGE_SHIFT)) { |
| 182 | index++; |
| 183 | continue; |
| 184 | } |
Nikolay Borisov | 64e1db5 | 2020-06-03 08:55:25 +0300 | [diff] [blame] | 185 | page = find_get_page(inode->vfs_inode.i_mapping, index); |
Naohiro Aota | 63d71450c | 2017-09-01 17:58:47 +0900 | [diff] [blame] | 186 | index++; |
| 187 | if (!page) |
| 188 | continue; |
Qu Wenruo | 968f256 | 2021-05-18 15:09:41 +0800 | [diff] [blame] | 189 | |
| 190 | /* |
| 191 | * Here we just clear all Ordered bits for every page in the |
| 192 | * range, then __endio_write_update_ordered() will handle |
| 193 | * the ordered extent accounting for the range. |
| 194 | */ |
Qu Wenruo | b945a46 | 2021-05-31 16:50:46 +0800 | [diff] [blame] | 195 | btrfs_page_clamp_clear_ordered(inode->root->fs_info, page, |
| 196 | offset, bytes); |
Naohiro Aota | 63d71450c | 2017-09-01 17:58:47 +0900 | [diff] [blame] | 197 | put_page(page); |
| 198 | } |
Nikolay Borisov | d1051d6 | 2018-11-21 17:10:52 +0200 | [diff] [blame] | 199 | |
Qu Wenruo | b945a46 | 2021-05-31 16:50:46 +0800 | [diff] [blame] | 200 | /* The locked page covers the full range, nothing needs to be done */ |
| 201 | if (bytes + offset <= page_offset(locked_page) + PAGE_SIZE) |
| 202 | return; |
Nikolay Borisov | d1051d6 | 2018-11-21 17:10:52 +0200 | [diff] [blame] | 203 | /* |
| 204 | * In case this page belongs to the delalloc range being instantiated |
| 205 | * then skip it, since the first page of a range is going to be |
| 206 | * properly cleaned up by the caller of run_delalloc_range |
| 207 | */ |
| 208 | if (page_start >= offset && page_end <= (offset + bytes - 1)) { |
Qu Wenruo | b945a46 | 2021-05-31 16:50:46 +0800 | [diff] [blame] | 209 | bytes = offset + bytes - page_offset(locked_page) - PAGE_SIZE; |
| 210 | offset = page_offset(locked_page) + PAGE_SIZE; |
Nikolay Borisov | d1051d6 | 2018-11-21 17:10:52 +0200 | [diff] [blame] | 211 | } |
| 212 | |
Nikolay Borisov | 64e1db5 | 2020-06-03 08:55:25 +0300 | [diff] [blame] | 213 | return __endio_write_update_ordered(inode, offset, bytes, false); |
Qu Wenruo | 52427260 | 2017-03-08 10:25:52 +0800 | [diff] [blame] | 214 | } |
| 215 | |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 216 | static int btrfs_dirty_inode(struct inode *inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 217 | |
Yan, Zheng | f34f57a | 2009-11-12 09:35:27 +0000 | [diff] [blame] | 218 | static int btrfs_init_inode_security(struct btrfs_trans_handle *trans, |
Eric Paris | 2a7dba3 | 2011-02-01 11:05:39 -0500 | [diff] [blame] | 219 | struct inode *inode, struct inode *dir, |
| 220 | const struct qstr *qstr) |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 221 | { |
| 222 | int err; |
| 223 | |
Yan, Zheng | f34f57a | 2009-11-12 09:35:27 +0000 | [diff] [blame] | 224 | err = btrfs_init_acl(trans, inode, dir); |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 225 | if (!err) |
Eric Paris | 2a7dba3 | 2011-02-01 11:05:39 -0500 | [diff] [blame] | 226 | err = btrfs_xattr_security_init(trans, inode, dir, qstr); |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 227 | return err; |
| 228 | } |
| 229 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 230 | /* |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 231 | * this does all the hard work for inserting an inline extent into |
| 232 | * the btree. The caller should have done a btrfs_drop_extents so that |
| 233 | * no overlapping inline items exist in the btree |
| 234 | */ |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 235 | static int insert_inline_extent(struct btrfs_trans_handle *trans, |
Filipe Manana | 5893dfb | 2020-11-04 11:07:32 +0000 | [diff] [blame] | 236 | struct btrfs_path *path, bool extent_inserted, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 237 | struct btrfs_root *root, struct inode *inode, |
| 238 | u64 start, size_t size, size_t compressed_size, |
Li Zefan | fe3f566 | 2011-03-28 08:30:38 +0000 | [diff] [blame] | 239 | int compress_type, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 240 | struct page **compressed_pages) |
| 241 | { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 242 | struct extent_buffer *leaf; |
| 243 | struct page *page = NULL; |
| 244 | char *kaddr; |
| 245 | unsigned long ptr; |
| 246 | struct btrfs_file_extent_item *ei; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 247 | int ret; |
| 248 | size_t cur_size = size; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 249 | unsigned long offset; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 250 | |
Jia-Ju Bai | 982f1f5d | 2019-07-27 16:51:13 +0800 | [diff] [blame] | 251 | ASSERT((compressed_size > 0 && compressed_pages) || |
| 252 | (compressed_size == 0 && !compressed_pages)); |
| 253 | |
Li Zefan | fe3f566 | 2011-03-28 08:30:38 +0000 | [diff] [blame] | 254 | if (compressed_size && compressed_pages) |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 255 | cur_size = compressed_size; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 256 | |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 257 | if (!extent_inserted) { |
| 258 | struct btrfs_key key; |
| 259 | size_t datasize; |
| 260 | |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 261 | key.objectid = btrfs_ino(BTRFS_I(inode)); |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 262 | key.offset = start; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 263 | key.type = BTRFS_EXTENT_DATA_KEY; |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 264 | |
| 265 | datasize = btrfs_file_extent_calc_inline_size(cur_size); |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 266 | ret = btrfs_insert_empty_item(trans, root, path, &key, |
| 267 | datasize); |
David Sterba | 79b4f4c | 2017-06-15 19:09:51 +0200 | [diff] [blame] | 268 | if (ret) |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 269 | goto fail; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 270 | } |
| 271 | leaf = path->nodes[0]; |
| 272 | ei = btrfs_item_ptr(leaf, path->slots[0], |
| 273 | struct btrfs_file_extent_item); |
| 274 | btrfs_set_file_extent_generation(leaf, ei, trans->transid); |
| 275 | btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE); |
| 276 | btrfs_set_file_extent_encryption(leaf, ei, 0); |
| 277 | btrfs_set_file_extent_other_encoding(leaf, ei, 0); |
| 278 | btrfs_set_file_extent_ram_bytes(leaf, ei, size); |
| 279 | ptr = btrfs_file_extent_inline_start(ei); |
| 280 | |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 281 | if (compress_type != BTRFS_COMPRESS_NONE) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 282 | struct page *cpage; |
| 283 | int i = 0; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 284 | while (compressed_size > 0) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 285 | cpage = compressed_pages[i]; |
Chris Mason | 5b050f0 | 2008-11-11 09:34:41 -0500 | [diff] [blame] | 286 | cur_size = min_t(unsigned long, compressed_size, |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 287 | PAGE_SIZE); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 288 | |
David Sterba | 4c2bf27 | 2021-06-15 17:15:38 +0200 | [diff] [blame] | 289 | kaddr = page_address(cpage); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 290 | write_extent_buffer(leaf, kaddr, ptr, cur_size); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 291 | |
| 292 | i++; |
| 293 | ptr += cur_size; |
| 294 | compressed_size -= cur_size; |
| 295 | } |
| 296 | btrfs_set_file_extent_compression(leaf, ei, |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 297 | compress_type); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 298 | } else { |
| 299 | page = find_get_page(inode->i_mapping, |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 300 | start >> PAGE_SHIFT); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 301 | btrfs_set_file_extent_compression(leaf, ei, 0); |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 302 | kaddr = kmap_atomic(page); |
Johannes Thumshirn | 7073017 | 2018-12-05 15:23:03 +0100 | [diff] [blame] | 303 | offset = offset_in_page(start); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 304 | write_extent_buffer(leaf, kaddr + offset, ptr, size); |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 305 | kunmap_atomic(kaddr); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 306 | put_page(page); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 307 | } |
| 308 | btrfs_mark_buffer_dirty(leaf); |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 309 | btrfs_release_path(path); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 310 | |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 311 | /* |
Josef Bacik | 9ddc959 | 2020-01-17 09:02:22 -0500 | [diff] [blame] | 312 | * We align size to sectorsize for inline extents just for simplicity |
| 313 | * sake. |
| 314 | */ |
| 315 | size = ALIGN(size, root->fs_info->sectorsize); |
| 316 | ret = btrfs_inode_set_file_extent_range(BTRFS_I(inode), start, size); |
| 317 | if (ret) |
| 318 | goto fail; |
| 319 | |
| 320 | /* |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 321 | * we're an inline extent, so nobody can |
| 322 | * extend the file past i_size without locking |
| 323 | * a page we already have locked. |
| 324 | * |
| 325 | * We must do any isize and inode updates |
| 326 | * before we unlock the pages. Otherwise we |
| 327 | * could end up racing with unlink. |
| 328 | */ |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 329 | BTRFS_I(inode)->disk_i_size = inode->i_size; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 330 | fail: |
David Sterba | 79b4f4c | 2017-06-15 19:09:51 +0200 | [diff] [blame] | 331 | return ret; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 332 | } |
| 333 | |
| 334 | |
| 335 | /* |
| 336 | * conditionally insert an inline extent into the file. This |
| 337 | * does the checks required to make sure the data is small enough |
| 338 | * to fit as an inline extent. |
| 339 | */ |
Nikolay Borisov | a034940 | 2020-06-03 08:55:12 +0300 | [diff] [blame] | 340 | 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] | 341 | u64 end, size_t compressed_size, |
| 342 | int compress_type, |
| 343 | struct page **compressed_pages) |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 344 | { |
Filipe Manana | 5893dfb | 2020-11-04 11:07:32 +0000 | [diff] [blame] | 345 | struct btrfs_drop_extents_args drop_args = { 0 }; |
Nikolay Borisov | a034940 | 2020-06-03 08:55:12 +0300 | [diff] [blame] | 346 | struct btrfs_root *root = inode->root; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 347 | struct btrfs_fs_info *fs_info = root->fs_info; |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 348 | struct btrfs_trans_handle *trans; |
Nikolay Borisov | a034940 | 2020-06-03 08:55:12 +0300 | [diff] [blame] | 349 | u64 isize = i_size_read(&inode->vfs_inode); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 350 | u64 actual_end = min(end + 1, isize); |
| 351 | u64 inline_len = actual_end - start; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 352 | u64 aligned_end = ALIGN(end, fs_info->sectorsize); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 353 | u64 data_len = inline_len; |
| 354 | int ret; |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 355 | struct btrfs_path *path; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 356 | |
| 357 | if (compressed_size) |
| 358 | data_len = compressed_size; |
| 359 | |
| 360 | if (start > 0 || |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 361 | actual_end > fs_info->sectorsize || |
| 362 | data_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info) || |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 363 | (!compressed_size && |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 364 | (actual_end & (fs_info->sectorsize - 1)) == 0) || |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 365 | end + 1 < isize || |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 366 | data_len > fs_info->max_inline) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 367 | return 1; |
| 368 | } |
| 369 | |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 370 | path = btrfs_alloc_path(); |
| 371 | if (!path) |
| 372 | return -ENOMEM; |
| 373 | |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 374 | trans = btrfs_join_transaction(root); |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 375 | if (IS_ERR(trans)) { |
| 376 | btrfs_free_path(path); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 377 | return PTR_ERR(trans); |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 378 | } |
Nikolay Borisov | a034940 | 2020-06-03 08:55:12 +0300 | [diff] [blame] | 379 | trans->block_rsv = &inode->block_rsv; |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 380 | |
Filipe Manana | 5893dfb | 2020-11-04 11:07:32 +0000 | [diff] [blame] | 381 | drop_args.path = path; |
| 382 | drop_args.start = start; |
| 383 | drop_args.end = aligned_end; |
| 384 | drop_args.drop_cache = true; |
| 385 | drop_args.replace_extent = true; |
| 386 | |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 387 | if (compressed_size && compressed_pages) |
Filipe Manana | 5893dfb | 2020-11-04 11:07:32 +0000 | [diff] [blame] | 388 | 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] | 389 | compressed_size); |
| 390 | else |
Filipe Manana | 5893dfb | 2020-11-04 11:07:32 +0000 | [diff] [blame] | 391 | 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] | 392 | inline_len); |
| 393 | |
Filipe Manana | 5893dfb | 2020-11-04 11:07:32 +0000 | [diff] [blame] | 394 | ret = btrfs_drop_extents(trans, root, inode, &drop_args); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 395 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 396 | btrfs_abort_transaction(trans, ret); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 397 | goto out; |
| 398 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 399 | |
| 400 | if (isize > actual_end) |
| 401 | inline_len = min_t(u64, isize, actual_end); |
Filipe Manana | 5893dfb | 2020-11-04 11:07:32 +0000 | [diff] [blame] | 402 | ret = insert_inline_extent(trans, path, drop_args.extent_inserted, |
Nikolay Borisov | a034940 | 2020-06-03 08:55:12 +0300 | [diff] [blame] | 403 | root, &inode->vfs_inode, start, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 404 | inline_len, compressed_size, |
Li Zefan | fe3f566 | 2011-03-28 08:30:38 +0000 | [diff] [blame] | 405 | compress_type, compressed_pages); |
Josef Bacik | 2adcac1 | 2012-05-23 16:10:14 -0400 | [diff] [blame] | 406 | if (ret && ret != -ENOSPC) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 407 | btrfs_abort_transaction(trans, ret); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 408 | goto out; |
Josef Bacik | 2adcac1 | 2012-05-23 16:10:14 -0400 | [diff] [blame] | 409 | } else if (ret == -ENOSPC) { |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 410 | ret = 1; |
| 411 | goto out; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 412 | } |
Josef Bacik | 2adcac1 | 2012-05-23 16:10:14 -0400 | [diff] [blame] | 413 | |
Filipe Manana | 2766ff6 | 2020-11-04 11:07:34 +0000 | [diff] [blame] | 414 | btrfs_update_inode_bytes(inode, inline_len, drop_args.bytes_found); |
Nikolay Borisov | 9a56fcd | 2020-11-02 16:48:59 +0200 | [diff] [blame] | 415 | ret = btrfs_update_inode(trans, root, inode); |
Filipe Manana | 2766ff6 | 2020-11-04 11:07:34 +0000 | [diff] [blame] | 416 | if (ret && ret != -ENOSPC) { |
| 417 | btrfs_abort_transaction(trans, ret); |
| 418 | goto out; |
| 419 | } else if (ret == -ENOSPC) { |
| 420 | ret = 1; |
| 421 | goto out; |
| 422 | } |
| 423 | |
Nikolay Borisov | a034940 | 2020-06-03 08:55:12 +0300 | [diff] [blame] | 424 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &inode->runtime_flags); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 425 | out: |
Qu Wenruo | 94ed938 | 2015-09-08 17:25:56 +0800 | [diff] [blame] | 426 | /* |
| 427 | * Don't forget to free the reserved space, as for inlined extent |
| 428 | * it won't count as data extent, free them directly here. |
| 429 | * And at reserve time, it's always aligned to page size, so |
| 430 | * just free one page here. |
| 431 | */ |
Nikolay Borisov | a034940 | 2020-06-03 08:55:12 +0300 | [diff] [blame] | 432 | btrfs_qgroup_free_data(inode, NULL, 0, PAGE_SIZE); |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 433 | btrfs_free_path(path); |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 434 | btrfs_end_transaction(trans); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 435 | return ret; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 436 | } |
| 437 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 438 | struct async_extent { |
| 439 | u64 start; |
| 440 | u64 ram_size; |
| 441 | u64 compressed_size; |
| 442 | struct page **pages; |
| 443 | unsigned long nr_pages; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 444 | int compress_type; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 445 | struct list_head list; |
| 446 | }; |
| 447 | |
Nikolay Borisov | 97db120 | 2019-03-12 17:20:24 +0200 | [diff] [blame] | 448 | struct async_chunk { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 449 | struct inode *inode; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 450 | struct page *locked_page; |
| 451 | u64 start; |
| 452 | u64 end; |
Liu Bo | f82b735 | 2017-10-23 23:18:16 -0600 | [diff] [blame] | 453 | unsigned int write_flags; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 454 | struct list_head extents; |
Chris Mason | ec39f76 | 2019-07-10 12:28:17 -0700 | [diff] [blame] | 455 | struct cgroup_subsys_state *blkcg_css; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 456 | struct btrfs_work work; |
Nikolay Borisov | 97db120 | 2019-03-12 17:20:24 +0200 | [diff] [blame] | 457 | atomic_t *pending; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 458 | }; |
| 459 | |
Nikolay Borisov | 97db120 | 2019-03-12 17:20:24 +0200 | [diff] [blame] | 460 | struct async_cow { |
| 461 | /* Number of chunks in flight; must be first in the structure */ |
| 462 | atomic_t num_chunks; |
| 463 | struct async_chunk chunks[]; |
| 464 | }; |
| 465 | |
| 466 | static noinline int add_async_extent(struct async_chunk *cow, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 467 | u64 start, u64 ram_size, |
| 468 | u64 compressed_size, |
| 469 | struct page **pages, |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 470 | unsigned long nr_pages, |
| 471 | int compress_type) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 472 | { |
| 473 | struct async_extent *async_extent; |
| 474 | |
| 475 | async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 476 | BUG_ON(!async_extent); /* -ENOMEM */ |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 477 | async_extent->start = start; |
| 478 | async_extent->ram_size = ram_size; |
| 479 | async_extent->compressed_size = compressed_size; |
| 480 | async_extent->pages = pages; |
| 481 | async_extent->nr_pages = nr_pages; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 482 | async_extent->compress_type = compress_type; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 483 | list_add_tail(&async_extent->list, &cow->extents); |
| 484 | return 0; |
| 485 | } |
| 486 | |
Qu Wenruo | 42c16da | 2019-07-01 05:12:46 +0000 | [diff] [blame] | 487 | /* |
| 488 | * Check if the inode has flags compatible with compression |
| 489 | */ |
Nikolay Borisov | 99c88dc | 2020-06-03 08:55:26 +0300 | [diff] [blame] | 490 | static inline bool inode_can_compress(struct btrfs_inode *inode) |
Qu Wenruo | 42c16da | 2019-07-01 05:12:46 +0000 | [diff] [blame] | 491 | { |
Qu Wenruo | 95ea048 | 2021-07-26 14:35:06 +0800 | [diff] [blame] | 492 | /* Subpage doesn't support compression yet */ |
| 493 | if (inode->root->fs_info->sectorsize < PAGE_SIZE) |
| 494 | return false; |
Nikolay Borisov | 99c88dc | 2020-06-03 08:55:26 +0300 | [diff] [blame] | 495 | if (inode->flags & BTRFS_INODE_NODATACOW || |
| 496 | inode->flags & BTRFS_INODE_NODATASUM) |
Qu Wenruo | 42c16da | 2019-07-01 05:12:46 +0000 | [diff] [blame] | 497 | return false; |
| 498 | return true; |
| 499 | } |
| 500 | |
| 501 | /* |
| 502 | * Check if the inode needs to be submitted to compression, based on mount |
| 503 | * options, defragmentation, properties or heuristics. |
| 504 | */ |
Nikolay Borisov | 808a129 | 2020-06-03 08:55:27 +0300 | [diff] [blame] | 505 | static inline int inode_need_compress(struct btrfs_inode *inode, u64 start, |
| 506 | u64 end) |
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 | struct btrfs_fs_info *fs_info = inode->root->fs_info; |
Wang Shilong | f79707b | 2014-07-17 11:44:09 +0800 | [diff] [blame] | 509 | |
Nikolay Borisov | 808a129 | 2020-06-03 08:55:27 +0300 | [diff] [blame] | 510 | if (!inode_can_compress(inode)) { |
Qu Wenruo | 42c16da | 2019-07-01 05:12:46 +0000 | [diff] [blame] | 511 | WARN(IS_ENABLED(CONFIG_BTRFS_DEBUG), |
| 512 | KERN_ERR "BTRFS: unexpected compression for ino %llu\n", |
Nikolay Borisov | 808a129 | 2020-06-03 08:55:27 +0300 | [diff] [blame] | 513 | btrfs_ino(inode)); |
Qu Wenruo | 42c16da | 2019-07-01 05:12:46 +0000 | [diff] [blame] | 514 | return 0; |
| 515 | } |
Wang Shilong | f79707b | 2014-07-17 11:44:09 +0800 | [diff] [blame] | 516 | /* force compress */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 517 | if (btrfs_test_opt(fs_info, FORCE_COMPRESS)) |
Wang Shilong | f79707b | 2014-07-17 11:44:09 +0800 | [diff] [blame] | 518 | return 1; |
David Sterba | eec63c6 | 2017-07-17 19:41:31 +0200 | [diff] [blame] | 519 | /* defrag ioctl */ |
Nikolay Borisov | 808a129 | 2020-06-03 08:55:27 +0300 | [diff] [blame] | 520 | if (inode->defrag_compress) |
David Sterba | eec63c6 | 2017-07-17 19:41:31 +0200 | [diff] [blame] | 521 | return 1; |
Wang Shilong | f79707b | 2014-07-17 11:44:09 +0800 | [diff] [blame] | 522 | /* bad compression ratios */ |
Nikolay Borisov | 808a129 | 2020-06-03 08:55:27 +0300 | [diff] [blame] | 523 | if (inode->flags & BTRFS_INODE_NOCOMPRESS) |
Wang Shilong | f79707b | 2014-07-17 11:44:09 +0800 | [diff] [blame] | 524 | return 0; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 525 | if (btrfs_test_opt(fs_info, COMPRESS) || |
Nikolay Borisov | 808a129 | 2020-06-03 08:55:27 +0300 | [diff] [blame] | 526 | inode->flags & BTRFS_INODE_COMPRESS || |
| 527 | inode->prop_compress) |
| 528 | return btrfs_compress_heuristic(&inode->vfs_inode, start, end); |
Wang Shilong | f79707b | 2014-07-17 11:44:09 +0800 | [diff] [blame] | 529 | return 0; |
| 530 | } |
| 531 | |
Nikolay Borisov | 6158e1c | 2017-02-20 13:50:43 +0200 | [diff] [blame] | 532 | static inline void inode_should_defrag(struct btrfs_inode *inode, |
Anand Jain | 26d30f8 | 2016-12-19 19:09:06 +0800 | [diff] [blame] | 533 | u64 start, u64 end, u64 num_bytes, u64 small_write) |
| 534 | { |
| 535 | /* If this is a small write inside eof, kick off a defrag */ |
| 536 | if (num_bytes < small_write && |
Nikolay Borisov | 6158e1c | 2017-02-20 13:50:43 +0200 | [diff] [blame] | 537 | (start > 0 || end + 1 < inode->disk_i_size)) |
Anand Jain | 26d30f8 | 2016-12-19 19:09:06 +0800 | [diff] [blame] | 538 | btrfs_add_inode_defrag(NULL, inode); |
| 539 | } |
| 540 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 541 | /* |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 542 | * we create compressed extents in two phases. The first |
| 543 | * phase compresses a range of pages that have already been |
| 544 | * locked (both pages and state bits are locked). |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 545 | * |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 546 | * This is done inside an ordered work queue, and the compression |
| 547 | * is spread across many cpus. The actual IO submission is step |
| 548 | * two, and the ordered work queue takes care of making sure that |
| 549 | * happens in the same order things were put onto the queue by |
| 550 | * writepages and friends. |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 551 | * |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 552 | * If this code finds it can't get good compression, it puts an |
| 553 | * entry onto the work queue to write the uncompressed bytes. This |
| 554 | * makes sure that both compressed inodes and uncompressed inodes |
Artem Bityutskiy | b257031 | 2012-07-25 18:12:06 +0300 | [diff] [blame] | 555 | * are written in the same order that the flusher thread sent them |
| 556 | * down. |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 557 | */ |
Nikolay Borisov | ac3e993 | 2019-07-17 14:41:44 +0300 | [diff] [blame] | 558 | static noinline int compress_file_range(struct async_chunk *async_chunk) |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 559 | { |
Nikolay Borisov | 1368c6d | 2019-03-12 17:20:27 +0200 | [diff] [blame] | 560 | struct inode *inode = async_chunk->inode; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 561 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 562 | u64 blocksize = fs_info->sectorsize; |
Nikolay Borisov | 1368c6d | 2019-03-12 17:20:27 +0200 | [diff] [blame] | 563 | u64 start = async_chunk->start; |
| 564 | u64 end = async_chunk->end; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 565 | u64 actual_end; |
Josef Bacik | d98da49 | 2019-10-11 09:03:54 -0400 | [diff] [blame] | 566 | u64 i_size; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 567 | int ret = 0; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 568 | struct page **pages = NULL; |
| 569 | unsigned long nr_pages; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 570 | unsigned long total_compressed = 0; |
| 571 | unsigned long total_in = 0; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 572 | int i; |
| 573 | int will_compress; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 574 | int compress_type = fs_info->compress_type; |
Nikolay Borisov | ac3e993 | 2019-07-17 14:41:44 +0300 | [diff] [blame] | 575 | int compressed_extents = 0; |
Chris Mason | 4adaa61 | 2013-03-26 13:07:00 -0400 | [diff] [blame] | 576 | int redirty = 0; |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 577 | |
Nikolay Borisov | 6158e1c | 2017-02-20 13:50:43 +0200 | [diff] [blame] | 578 | inode_should_defrag(BTRFS_I(inode), start, end, end - start + 1, |
| 579 | SZ_16K); |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 580 | |
Josef Bacik | d98da49 | 2019-10-11 09:03:54 -0400 | [diff] [blame] | 581 | /* |
| 582 | * We need to save i_size before now because it could change in between |
| 583 | * us evaluating the size and assigning it. This is because we lock and |
| 584 | * unlock the page in truncate and fallocate, and then modify the i_size |
| 585 | * later on. |
| 586 | * |
| 587 | * The barriers are to emulate READ_ONCE, remove that once i_size_read |
| 588 | * does that for us. |
| 589 | */ |
| 590 | barrier(); |
| 591 | i_size = i_size_read(inode); |
| 592 | barrier(); |
| 593 | actual_end = min_t(u64, i_size, end + 1); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 594 | again: |
| 595 | will_compress = 0; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 596 | nr_pages = (end >> PAGE_SHIFT) - (start >> PAGE_SHIFT) + 1; |
David Sterba | 069eac7 | 2017-02-14 19:36:54 +0100 | [diff] [blame] | 597 | BUILD_BUG_ON((BTRFS_MAX_COMPRESSED % PAGE_SIZE) != 0); |
| 598 | nr_pages = min_t(unsigned long, nr_pages, |
| 599 | BTRFS_MAX_COMPRESSED / PAGE_SIZE); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 600 | |
Chris Mason | f03d9301f | 2009-02-04 09:31:06 -0500 | [diff] [blame] | 601 | /* |
| 602 | * we don't want to send crud past the end of i_size through |
| 603 | * compression, that's just a waste of CPU time. So, if the |
| 604 | * end of the file is before the start of our current |
| 605 | * requested range of bytes, we bail out to the uncompressed |
| 606 | * cleanup code that can deal with all of this. |
| 607 | * |
| 608 | * It isn't really the fastest way to fix things, but this is a |
| 609 | * very uncommon corner. |
| 610 | */ |
| 611 | if (actual_end <= start) |
| 612 | goto cleanup_and_bail_uncompressed; |
| 613 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 614 | total_compressed = actual_end - start; |
| 615 | |
Shilong Wang | 4bcbb33 | 2014-10-07 18:44:35 -0400 | [diff] [blame] | 616 | /* |
| 617 | * skip compression for a small file range(<=blocksize) that |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 618 | * 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] | 619 | */ |
| 620 | if (total_compressed <= blocksize && |
| 621 | (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size)) |
| 622 | goto cleanup_and_bail_uncompressed; |
| 623 | |
David Sterba | 069eac7 | 2017-02-14 19:36:54 +0100 | [diff] [blame] | 624 | total_compressed = min_t(unsigned long, total_compressed, |
| 625 | BTRFS_MAX_UNCOMPRESSED); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 626 | total_in = 0; |
| 627 | ret = 0; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 628 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 629 | /* |
| 630 | * we do compression for mount -o compress and when the |
| 631 | * inode has not been flagged as nocompress. This flag can |
| 632 | * change at any time if we discover bad compression ratios. |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 633 | */ |
David Sterba | f216562 | 2021-06-14 12:45:18 +0200 | [diff] [blame] | 634 | if (nr_pages > 1 && inode_need_compress(BTRFS_I(inode), start, end)) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 635 | WARN_ON(pages); |
David Sterba | 31e818f | 2015-02-20 18:00:26 +0100 | [diff] [blame] | 636 | pages = kcalloc(nr_pages, sizeof(struct page *), GFP_NOFS); |
Li Zefan | 560f7d7 | 2011-09-08 10:22:01 +0800 | [diff] [blame] | 637 | if (!pages) { |
| 638 | /* just bail out to the uncompressed code */ |
Filipe Manana | 3527a01 | 2018-10-13 00:37:25 +0100 | [diff] [blame] | 639 | nr_pages = 0; |
Li Zefan | 560f7d7 | 2011-09-08 10:22:01 +0800 | [diff] [blame] | 640 | goto cont; |
| 641 | } |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 642 | |
David Sterba | eec63c6 | 2017-07-17 19:41:31 +0200 | [diff] [blame] | 643 | if (BTRFS_I(inode)->defrag_compress) |
| 644 | compress_type = BTRFS_I(inode)->defrag_compress; |
| 645 | else if (BTRFS_I(inode)->prop_compress) |
David Sterba | b52aa8c | 2017-07-17 19:17:20 +0200 | [diff] [blame] | 646 | compress_type = BTRFS_I(inode)->prop_compress; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 647 | |
Chris Mason | 4adaa61 | 2013-03-26 13:07:00 -0400 | [diff] [blame] | 648 | /* |
| 649 | * we need to call clear_page_dirty_for_io on each |
| 650 | * page in the range. Otherwise applications with the file |
| 651 | * mmap'd can wander in and change the page contents while |
| 652 | * we are compressing them. |
| 653 | * |
| 654 | * If the compression fails for any reason, we set the pages |
| 655 | * dirty again later on. |
Timofey Titovets | e9679de | 2017-10-24 01:29:48 +0300 | [diff] [blame] | 656 | * |
| 657 | * Note that the remaining part is redirtied, the start pointer |
| 658 | * has moved, the end is the original one. |
Chris Mason | 4adaa61 | 2013-03-26 13:07:00 -0400 | [diff] [blame] | 659 | */ |
Timofey Titovets | e9679de | 2017-10-24 01:29:48 +0300 | [diff] [blame] | 660 | if (!redirty) { |
| 661 | extent_range_clear_dirty_for_io(inode, start, end); |
| 662 | redirty = 1; |
| 663 | } |
David Sterba | f51d2b5 | 2017-09-15 17:36:57 +0200 | [diff] [blame] | 664 | |
| 665 | /* Compression level is applied here and only here */ |
| 666 | ret = btrfs_compress_pages( |
| 667 | compress_type | (fs_info->compress_level << 4), |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 668 | inode->i_mapping, start, |
David Sterba | 38c3146 | 2017-02-14 19:04:07 +0100 | [diff] [blame] | 669 | pages, |
David Sterba | 4d3a800 | 2017-02-14 19:04:07 +0100 | [diff] [blame] | 670 | &nr_pages, |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 671 | &total_in, |
David Sterba | e5d7490 | 2017-02-14 19:45:05 +0100 | [diff] [blame] | 672 | &total_compressed); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 673 | |
| 674 | if (!ret) { |
Johannes Thumshirn | 7073017 | 2018-12-05 15:23:03 +0100 | [diff] [blame] | 675 | unsigned long offset = offset_in_page(total_compressed); |
David Sterba | 4d3a800 | 2017-02-14 19:04:07 +0100 | [diff] [blame] | 676 | struct page *page = pages[nr_pages - 1]; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 677 | |
| 678 | /* zero the tail end of the last page, we might be |
| 679 | * sending it down to disk |
| 680 | */ |
Ira Weiny | d048b9c | 2021-05-04 18:40:07 -0700 | [diff] [blame] | 681 | if (offset) |
| 682 | memzero_page(page, offset, PAGE_SIZE - offset); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 683 | will_compress = 1; |
| 684 | } |
| 685 | } |
Li Zefan | 560f7d7 | 2011-09-08 10:22:01 +0800 | [diff] [blame] | 686 | cont: |
Qu Wenruo | 7367253 | 2021-07-26 14:34:59 +0800 | [diff] [blame] | 687 | /* |
| 688 | * Check cow_file_range() for why we don't even try to create inline |
| 689 | * extent for subpage case. |
| 690 | */ |
| 691 | if (start == 0 && fs_info->sectorsize == PAGE_SIZE) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 692 | /* lets try to make an inline extent */ |
Timofey Titovets | 6018ba0 | 2017-09-15 01:57:26 +0300 | [diff] [blame] | 693 | if (ret || total_in < actual_end) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 694 | /* we didn't compress the entire range, try |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 695 | * to make an uncompressed inline extent. |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 696 | */ |
Nikolay Borisov | a034940 | 2020-06-03 08:55:12 +0300 | [diff] [blame] | 697 | ret = cow_file_range_inline(BTRFS_I(inode), start, end, |
| 698 | 0, BTRFS_COMPRESS_NONE, |
| 699 | NULL); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 700 | } else { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 701 | /* try making a compressed inline extent */ |
Nikolay Borisov | a034940 | 2020-06-03 08:55:12 +0300 | [diff] [blame] | 702 | ret = cow_file_range_inline(BTRFS_I(inode), start, end, |
Li Zefan | fe3f566 | 2011-03-28 08:30:38 +0000 | [diff] [blame] | 703 | total_compressed, |
| 704 | compress_type, pages); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 705 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 706 | if (ret <= 0) { |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 707 | unsigned long clear_flags = EXTENT_DELALLOC | |
Josef Bacik | 8b62f87 | 2017-10-19 14:15:55 -0400 | [diff] [blame] | 708 | EXTENT_DELALLOC_NEW | EXTENT_DEFRAG | |
| 709 | EXTENT_DO_ACCOUNTING; |
Filipe Manana | e6eb431 | 2014-10-10 10:45:12 +0100 | [diff] [blame] | 710 | unsigned long page_error_op; |
| 711 | |
Filipe Manana | e6eb431 | 2014-10-10 10:45:12 +0100 | [diff] [blame] | 712 | page_error_op = ret < 0 ? PAGE_SET_ERROR : 0; |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 713 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 714 | /* |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 715 | * inline extent creation worked or returned error, |
| 716 | * we don't need to create any more async work items. |
| 717 | * Unlock and free up our temp pages. |
Josef Bacik | 8b62f87 | 2017-10-19 14:15:55 -0400 | [diff] [blame] | 718 | * |
| 719 | * We use DO_ACCOUNTING here because we need the |
| 720 | * delalloc_release_metadata to be done _after_ we drop |
| 721 | * our outstanding extent for clearing delalloc for this |
| 722 | * range. |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 723 | */ |
Nikolay Borisov | ad7ff17 | 2020-06-03 08:55:06 +0300 | [diff] [blame] | 724 | extent_clear_unlock_delalloc(BTRFS_I(inode), start, end, |
| 725 | NULL, |
Nikolay Borisov | 74e9194 | 2019-07-17 16:18:16 +0300 | [diff] [blame] | 726 | clear_flags, |
Qu Wenruo | ba8b04c | 2016-07-19 16:50:36 +0800 | [diff] [blame] | 727 | PAGE_UNLOCK | |
Qu Wenruo | 6869b0a | 2021-01-26 16:33:45 +0800 | [diff] [blame] | 728 | PAGE_START_WRITEBACK | |
Filipe Manana | e6eb431 | 2014-10-10 10:45:12 +0100 | [diff] [blame] | 729 | page_error_op | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 730 | PAGE_END_WRITEBACK); |
Nikolay Borisov | cecc8d9 | 2019-07-17 14:41:45 +0300 | [diff] [blame] | 731 | |
Qu Wenruo | 1e6e238 | 2020-07-28 16:39:26 +0800 | [diff] [blame] | 732 | /* |
| 733 | * Ensure we only free the compressed pages if we have |
| 734 | * them allocated, as we can still reach here with |
| 735 | * inode_need_compress() == false. |
| 736 | */ |
| 737 | if (pages) { |
| 738 | for (i = 0; i < nr_pages; i++) { |
| 739 | WARN_ON(pages[i]->mapping); |
| 740 | put_page(pages[i]); |
| 741 | } |
| 742 | kfree(pages); |
Nikolay Borisov | cecc8d9 | 2019-07-17 14:41:45 +0300 | [diff] [blame] | 743 | } |
Nikolay Borisov | cecc8d9 | 2019-07-17 14:41:45 +0300 | [diff] [blame] | 744 | return 0; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 745 | } |
| 746 | } |
| 747 | |
| 748 | if (will_compress) { |
| 749 | /* |
| 750 | * we aren't doing an inline extent round the compressed size |
| 751 | * up to a block size boundary so the allocator does sane |
| 752 | * things |
| 753 | */ |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 754 | total_compressed = ALIGN(total_compressed, blocksize); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 755 | |
| 756 | /* |
| 757 | * one last check to make sure the compression is really a |
Timofey Titovets | 170607e | 2017-06-06 14:41:15 +0300 | [diff] [blame] | 758 | * win, compare the page count read with the blocks on disk, |
| 759 | * compression must free at least one sector size |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 760 | */ |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 761 | total_in = ALIGN(total_in, PAGE_SIZE); |
Timofey Titovets | 170607e | 2017-06-06 14:41:15 +0300 | [diff] [blame] | 762 | if (total_compressed + blocksize <= total_in) { |
Nikolay Borisov | ac3e993 | 2019-07-17 14:41:44 +0300 | [diff] [blame] | 763 | compressed_extents++; |
Ashish Samant | c8bb0c8 | 2016-03-25 19:01:33 -0700 | [diff] [blame] | 764 | |
| 765 | /* |
| 766 | * The async work queues will take care of doing actual |
| 767 | * allocation on disk for these compressed pages, and |
| 768 | * will submit them to the elevator. |
| 769 | */ |
Nikolay Borisov | b532627 | 2019-03-12 17:20:25 +0200 | [diff] [blame] | 770 | add_async_extent(async_chunk, start, total_in, |
David Sterba | 4d3a800 | 2017-02-14 19:04:07 +0100 | [diff] [blame] | 771 | total_compressed, pages, nr_pages, |
Ashish Samant | c8bb0c8 | 2016-03-25 19:01:33 -0700 | [diff] [blame] | 772 | compress_type); |
| 773 | |
Timofey Titovets | 1170862 | 2017-10-03 18:06:01 +0300 | [diff] [blame] | 774 | if (start + total_in < end) { |
| 775 | start += total_in; |
Ashish Samant | c8bb0c8 | 2016-03-25 19:01:33 -0700 | [diff] [blame] | 776 | pages = NULL; |
| 777 | cond_resched(); |
| 778 | goto again; |
| 779 | } |
Nikolay Borisov | ac3e993 | 2019-07-17 14:41:44 +0300 | [diff] [blame] | 780 | return compressed_extents; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 781 | } |
| 782 | } |
Ashish Samant | c8bb0c8 | 2016-03-25 19:01:33 -0700 | [diff] [blame] | 783 | if (pages) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 784 | /* |
| 785 | * the compression code ran but failed to make things smaller, |
| 786 | * free any pages it allocated and our page pointer array |
| 787 | */ |
David Sterba | 4d3a800 | 2017-02-14 19:04:07 +0100 | [diff] [blame] | 788 | for (i = 0; i < nr_pages; i++) { |
Chris Mason | 70b99e6 | 2008-10-31 12:46:39 -0400 | [diff] [blame] | 789 | WARN_ON(pages[i]->mapping); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 790 | put_page(pages[i]); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 791 | } |
| 792 | kfree(pages); |
| 793 | pages = NULL; |
| 794 | total_compressed = 0; |
David Sterba | 4d3a800 | 2017-02-14 19:04:07 +0100 | [diff] [blame] | 795 | nr_pages = 0; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 796 | |
| 797 | /* flag the file so we don't compress in the future */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 798 | if (!btrfs_test_opt(fs_info, FORCE_COMPRESS) && |
David Sterba | b52aa8c | 2017-07-17 19:17:20 +0200 | [diff] [blame] | 799 | !(BTRFS_I(inode)->prop_compress)) { |
Chris Mason | a555f81 | 2010-01-28 16:18:15 -0500 | [diff] [blame] | 800 | BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS; |
Chris Mason | 1e701a3 | 2010-03-11 09:42:04 -0500 | [diff] [blame] | 801 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 802 | } |
Chris Mason | f03d9301f | 2009-02-04 09:31:06 -0500 | [diff] [blame] | 803 | cleanup_and_bail_uncompressed: |
Ashish Samant | c8bb0c8 | 2016-03-25 19:01:33 -0700 | [diff] [blame] | 804 | /* |
| 805 | * No compression, but we still need to write the pages in the file |
| 806 | * we've been given so far. redirty the locked page if it corresponds |
| 807 | * to our extent and set things up for the async work queue to run |
| 808 | * cow_file_range to do the normal delalloc dance. |
| 809 | */ |
Chris Mason | 1d53c9e | 2019-07-10 12:28:16 -0700 | [diff] [blame] | 810 | if (async_chunk->locked_page && |
| 811 | (page_offset(async_chunk->locked_page) >= start && |
| 812 | page_offset(async_chunk->locked_page)) <= end) { |
Nikolay Borisov | 1368c6d | 2019-03-12 17:20:27 +0200 | [diff] [blame] | 813 | __set_page_dirty_nobuffers(async_chunk->locked_page); |
Ashish Samant | c8bb0c8 | 2016-03-25 19:01:33 -0700 | [diff] [blame] | 814 | /* unlocked later on in the async handlers */ |
Chris Mason | 1d53c9e | 2019-07-10 12:28:16 -0700 | [diff] [blame] | 815 | } |
Ashish Samant | c8bb0c8 | 2016-03-25 19:01:33 -0700 | [diff] [blame] | 816 | |
| 817 | if (redirty) |
| 818 | extent_range_redirty_for_io(inode, start, end); |
Nikolay Borisov | b532627 | 2019-03-12 17:20:25 +0200 | [diff] [blame] | 819 | add_async_extent(async_chunk, start, end - start + 1, 0, NULL, 0, |
Ashish Samant | c8bb0c8 | 2016-03-25 19:01:33 -0700 | [diff] [blame] | 820 | BTRFS_COMPRESS_NONE); |
Nikolay Borisov | ac3e993 | 2019-07-17 14:41:44 +0300 | [diff] [blame] | 821 | compressed_extents++; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 822 | |
Nikolay Borisov | ac3e993 | 2019-07-17 14:41:44 +0300 | [diff] [blame] | 823 | return compressed_extents; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 824 | } |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 825 | |
Filipe Manana | 40ae837 | 2014-10-06 22:14:24 +0100 | [diff] [blame] | 826 | static void free_async_extent_pages(struct async_extent *async_extent) |
| 827 | { |
| 828 | int i; |
| 829 | |
| 830 | if (!async_extent->pages) |
| 831 | return; |
| 832 | |
| 833 | for (i = 0; i < async_extent->nr_pages; i++) { |
| 834 | WARN_ON(async_extent->pages[i]->mapping); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 835 | put_page(async_extent->pages[i]); |
Filipe Manana | 40ae837 | 2014-10-06 22:14:24 +0100 | [diff] [blame] | 836 | } |
| 837 | kfree(async_extent->pages); |
| 838 | async_extent->nr_pages = 0; |
| 839 | async_extent->pages = NULL; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 840 | } |
| 841 | |
| 842 | /* |
| 843 | * phase two of compressed writeback. This is the ordered portion |
| 844 | * of the code, which only gets called in the order the work was |
| 845 | * queued. We walk all the async extents created by compress_file_range |
| 846 | * and send them down to the disk. |
| 847 | */ |
Nikolay Borisov | b532627 | 2019-03-12 17:20:25 +0200 | [diff] [blame] | 848 | static noinline void submit_compressed_extents(struct async_chunk *async_chunk) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 849 | { |
Nikolay Borisov | a0ff10d | 2020-06-03 08:55:17 +0300 | [diff] [blame] | 850 | struct btrfs_inode *inode = BTRFS_I(async_chunk->inode); |
| 851 | struct btrfs_fs_info *fs_info = inode->root->fs_info; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 852 | struct async_extent *async_extent; |
| 853 | u64 alloc_hint = 0; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 854 | struct btrfs_key ins; |
| 855 | struct extent_map *em; |
Nikolay Borisov | a0ff10d | 2020-06-03 08:55:17 +0300 | [diff] [blame] | 856 | struct btrfs_root *root = inode->root; |
| 857 | struct extent_io_tree *io_tree = &inode->io_tree; |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 858 | int ret = 0; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 859 | |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 860 | again: |
Nikolay Borisov | b532627 | 2019-03-12 17:20:25 +0200 | [diff] [blame] | 861 | while (!list_empty(&async_chunk->extents)) { |
| 862 | async_extent = list_entry(async_chunk->extents.next, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 863 | struct async_extent, list); |
| 864 | list_del(&async_extent->list); |
| 865 | |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 866 | retry: |
Nikolay Borisov | 7447555 | 2019-03-12 17:20:30 +0200 | [diff] [blame] | 867 | lock_extent(io_tree, async_extent->start, |
| 868 | async_extent->start + async_extent->ram_size - 1); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 869 | /* did the compression code fall back to uncompressed IO? */ |
| 870 | if (!async_extent->pages) { |
| 871 | int page_started = 0; |
| 872 | unsigned long nr_written = 0; |
| 873 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 874 | /* allocate blocks */ |
Nikolay Borisov | a0ff10d | 2020-06-03 08:55:17 +0300 | [diff] [blame] | 875 | ret = cow_file_range(inode, async_chunk->locked_page, |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 876 | async_extent->start, |
| 877 | async_extent->start + |
| 878 | async_extent->ram_size - 1, |
Nikolay Borisov | 330a582 | 2019-07-17 16:18:17 +0300 | [diff] [blame] | 879 | &page_started, &nr_written, 0); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 880 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 881 | /* JDM XXX */ |
| 882 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 883 | /* |
| 884 | * if page_started, cow_file_range inserted an |
| 885 | * inline extent and took care of all the unlocking |
| 886 | * and IO for us. Otherwise, we need to submit |
| 887 | * all those pages down to the drive. |
| 888 | */ |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 889 | if (!page_started && !ret) |
Nikolay Borisov | a0ff10d | 2020-06-03 08:55:17 +0300 | [diff] [blame] | 890 | extent_write_locked_range(&inode->vfs_inode, |
Nikolay Borisov | 5e3ee23 | 2017-12-08 15:55:58 +0200 | [diff] [blame] | 891 | async_extent->start, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 892 | async_extent->start + |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 893 | async_extent->ram_size - 1, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 894 | WB_SYNC_ALL); |
Chris Mason | 1d53c9e | 2019-07-10 12:28:16 -0700 | [diff] [blame] | 895 | else if (ret && async_chunk->locked_page) |
Nikolay Borisov | b532627 | 2019-03-12 17:20:25 +0200 | [diff] [blame] | 896 | unlock_page(async_chunk->locked_page); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 897 | kfree(async_extent); |
| 898 | cond_resched(); |
| 899 | continue; |
| 900 | } |
| 901 | |
Wang Xiaoguang | 1851309 | 2016-07-25 15:51:40 +0800 | [diff] [blame] | 902 | ret = btrfs_reserve_extent(root, async_extent->ram_size, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 903 | async_extent->compressed_size, |
| 904 | async_extent->compressed_size, |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 905 | 0, alloc_hint, &ins, 1, 1); |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 906 | if (ret) { |
Filipe Manana | 40ae837 | 2014-10-06 22:14:24 +0100 | [diff] [blame] | 907 | free_async_extent_pages(async_extent); |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 908 | |
Josef Bacik | fdf8e2e | 2013-06-14 16:58:23 -0400 | [diff] [blame] | 909 | if (ret == -ENOSPC) { |
| 910 | unlock_extent(io_tree, async_extent->start, |
| 911 | async_extent->start + |
| 912 | async_extent->ram_size - 1); |
Liu Bo | ce62003 | 2014-07-24 22:48:05 +0800 | [diff] [blame] | 913 | |
| 914 | /* |
| 915 | * we need to redirty the pages if we decide to |
| 916 | * fallback to uncompressed IO, otherwise we |
| 917 | * will not submit these pages down to lower |
| 918 | * layers. |
| 919 | */ |
Nikolay Borisov | a0ff10d | 2020-06-03 08:55:17 +0300 | [diff] [blame] | 920 | extent_range_redirty_for_io(&inode->vfs_inode, |
Liu Bo | ce62003 | 2014-07-24 22:48:05 +0800 | [diff] [blame] | 921 | async_extent->start, |
| 922 | async_extent->start + |
| 923 | async_extent->ram_size - 1); |
| 924 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 925 | goto retry; |
Josef Bacik | fdf8e2e | 2013-06-14 16:58:23 -0400 | [diff] [blame] | 926 | } |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 927 | goto out_free; |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 928 | } |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 929 | /* |
| 930 | * here we're doing allocation and writeback of the |
| 931 | * compressed pages |
| 932 | */ |
Nikolay Borisov | a0ff10d | 2020-06-03 08:55:17 +0300 | [diff] [blame] | 933 | em = create_io_em(inode, async_extent->start, |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 934 | async_extent->ram_size, /* len */ |
| 935 | async_extent->start, /* orig_start */ |
| 936 | ins.objectid, /* block_start */ |
| 937 | ins.offset, /* block_len */ |
| 938 | ins.offset, /* orig_block_len */ |
| 939 | async_extent->ram_size, /* ram_bytes */ |
| 940 | async_extent->compress_type, |
| 941 | BTRFS_ORDERED_COMPRESSED); |
| 942 | if (IS_ERR(em)) |
| 943 | /* ret value is not necessary due to void function */ |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 944 | goto out_free_reserve; |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 945 | free_extent_map(em); |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 946 | |
Nikolay Borisov | a0ff10d | 2020-06-03 08:55:17 +0300 | [diff] [blame] | 947 | ret = btrfs_add_ordered_extent_compress(inode, |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 948 | async_extent->start, |
| 949 | ins.objectid, |
| 950 | async_extent->ram_size, |
| 951 | ins.offset, |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 952 | async_extent->compress_type); |
Filipe Manana | d9f8596 | 2014-08-25 10:43:00 +0100 | [diff] [blame] | 953 | if (ret) { |
Nikolay Borisov | a0ff10d | 2020-06-03 08:55:17 +0300 | [diff] [blame] | 954 | btrfs_drop_extent_cache(inode, async_extent->start, |
Filipe Manana | d9f8596 | 2014-08-25 10:43:00 +0100 | [diff] [blame] | 955 | async_extent->start + |
| 956 | async_extent->ram_size - 1, 0); |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 957 | goto out_free_reserve; |
Filipe Manana | d9f8596 | 2014-08-25 10:43:00 +0100 | [diff] [blame] | 958 | } |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 959 | btrfs_dec_block_group_reservations(fs_info, ins.objectid); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 960 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 961 | /* |
| 962 | * clear dirty, set writeback and unlock the pages. |
| 963 | */ |
Nikolay Borisov | a0ff10d | 2020-06-03 08:55:17 +0300 | [diff] [blame] | 964 | extent_clear_unlock_delalloc(inode, async_extent->start, |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 965 | async_extent->start + |
| 966 | async_extent->ram_size - 1, |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 967 | NULL, EXTENT_LOCKED | EXTENT_DELALLOC, |
Qu Wenruo | 6869b0a | 2021-01-26 16:33:45 +0800 | [diff] [blame] | 968 | PAGE_UNLOCK | PAGE_START_WRITEBACK); |
Nikolay Borisov | a0ff10d | 2020-06-03 08:55:17 +0300 | [diff] [blame] | 969 | if (btrfs_submit_compressed_write(inode, async_extent->start, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 970 | async_extent->ram_size, |
| 971 | ins.objectid, |
| 972 | ins.offset, async_extent->pages, |
Liu Bo | f82b735 | 2017-10-23 23:18:16 -0600 | [diff] [blame] | 973 | async_extent->nr_pages, |
Chris Mason | ec39f76 | 2019-07-10 12:28:17 -0700 | [diff] [blame] | 974 | async_chunk->write_flags, |
| 975 | async_chunk->blkcg_css)) { |
Filipe Manana | fce2a4e | 2014-10-06 22:14:23 +0100 | [diff] [blame] | 976 | struct page *p = async_extent->pages[0]; |
| 977 | const u64 start = async_extent->start; |
| 978 | const u64 end = start + async_extent->ram_size - 1; |
| 979 | |
Nikolay Borisov | a0ff10d | 2020-06-03 08:55:17 +0300 | [diff] [blame] | 980 | p->mapping = inode->vfs_inode.i_mapping; |
Qu Wenruo | 38a39ac7 | 2021-04-08 20:32:27 +0800 | [diff] [blame] | 981 | btrfs_writepage_endio_finish_ordered(inode, p, start, |
David Sterba | 25c1252 | 2021-07-26 14:15:08 +0200 | [diff] [blame] | 982 | end, false); |
Nikolay Borisov | 7087a9d | 2018-11-01 14:09:48 +0200 | [diff] [blame] | 983 | |
Filipe Manana | fce2a4e | 2014-10-06 22:14:23 +0100 | [diff] [blame] | 984 | p->mapping = NULL; |
Nikolay Borisov | a0ff10d | 2020-06-03 08:55:17 +0300 | [diff] [blame] | 985 | extent_clear_unlock_delalloc(inode, start, end, NULL, 0, |
Filipe Manana | fce2a4e | 2014-10-06 22:14:23 +0100 | [diff] [blame] | 986 | PAGE_END_WRITEBACK | |
| 987 | PAGE_SET_ERROR); |
Filipe Manana | 40ae837 | 2014-10-06 22:14:24 +0100 | [diff] [blame] | 988 | free_async_extent_pages(async_extent); |
Filipe Manana | fce2a4e | 2014-10-06 22:14:23 +0100 | [diff] [blame] | 989 | } |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 990 | alloc_hint = ins.objectid + ins.offset; |
| 991 | kfree(async_extent); |
| 992 | cond_resched(); |
| 993 | } |
Filipe Manana | dec8f17 | 2014-10-06 22:14:26 +0100 | [diff] [blame] | 994 | return; |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 995 | out_free_reserve: |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 996 | btrfs_dec_block_group_reservations(fs_info, ins.objectid); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 997 | btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 998 | out_free: |
Nikolay Borisov | a0ff10d | 2020-06-03 08:55:17 +0300 | [diff] [blame] | 999 | extent_clear_unlock_delalloc(inode, async_extent->start, |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 1000 | async_extent->start + |
| 1001 | async_extent->ram_size - 1, |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1002 | NULL, EXTENT_LOCKED | EXTENT_DELALLOC | |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 1003 | EXTENT_DELALLOC_NEW | |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 1004 | EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING, |
Qu Wenruo | 6869b0a | 2021-01-26 16:33:45 +0800 | [diff] [blame] | 1005 | PAGE_UNLOCK | PAGE_START_WRITEBACK | |
| 1006 | PAGE_END_WRITEBACK | PAGE_SET_ERROR); |
Filipe Manana | 40ae837 | 2014-10-06 22:14:24 +0100 | [diff] [blame] | 1007 | free_async_extent_pages(async_extent); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1008 | kfree(async_extent); |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 1009 | goto again; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1010 | } |
| 1011 | |
Nikolay Borisov | 43c6984 | 2020-06-03 08:55:02 +0300 | [diff] [blame] | 1012 | static u64 get_extent_allocation_hint(struct btrfs_inode *inode, u64 start, |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 1013 | u64 num_bytes) |
| 1014 | { |
Nikolay Borisov | 43c6984 | 2020-06-03 08:55:02 +0300 | [diff] [blame] | 1015 | struct extent_map_tree *em_tree = &inode->extent_tree; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 1016 | struct extent_map *em; |
| 1017 | u64 alloc_hint = 0; |
| 1018 | |
| 1019 | read_lock(&em_tree->lock); |
| 1020 | em = search_extent_mapping(em_tree, start, num_bytes); |
| 1021 | if (em) { |
| 1022 | /* |
| 1023 | * if block start isn't an actual block number then find the |
| 1024 | * first block in this inode and use that as a hint. If that |
| 1025 | * block is also bogus then just don't worry about it. |
| 1026 | */ |
| 1027 | if (em->block_start >= EXTENT_MAP_LAST_BYTE) { |
| 1028 | free_extent_map(em); |
| 1029 | em = search_extent_mapping(em_tree, 0, 0); |
| 1030 | if (em && em->block_start < EXTENT_MAP_LAST_BYTE) |
| 1031 | alloc_hint = em->block_start; |
| 1032 | if (em) |
| 1033 | free_extent_map(em); |
| 1034 | } else { |
| 1035 | alloc_hint = em->block_start; |
| 1036 | free_extent_map(em); |
| 1037 | } |
| 1038 | } |
| 1039 | read_unlock(&em_tree->lock); |
| 1040 | |
| 1041 | return alloc_hint; |
| 1042 | } |
| 1043 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1044 | /* |
| 1045 | * when extent_io.c finds a delayed allocation range in the file, |
| 1046 | * the call backs end up in this code. The basic idea is to |
| 1047 | * allocate extents on disk for the range, and create ordered data structs |
| 1048 | * in ram to track those extents. |
| 1049 | * |
| 1050 | * locked_page is the page that writepage had locked already. We use |
| 1051 | * it to make sure we don't do extra locks or unlocks. |
| 1052 | * |
| 1053 | * *page_started is set to one if we unlock locked_page and do everything |
| 1054 | * required to start IO on it. It may be clean and already done with |
| 1055 | * IO when we return. |
| 1056 | */ |
Nikolay Borisov | 6e26c44 | 2020-06-03 08:55:14 +0300 | [diff] [blame] | 1057 | static noinline int cow_file_range(struct btrfs_inode *inode, |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 1058 | struct page *locked_page, |
Nikolay Borisov | 74e9194 | 2019-07-17 16:18:16 +0300 | [diff] [blame] | 1059 | u64 start, u64 end, int *page_started, |
Nikolay Borisov | 330a582 | 2019-07-17 16:18:17 +0300 | [diff] [blame] | 1060 | unsigned long *nr_written, int unlock) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1061 | { |
Nikolay Borisov | 6e26c44 | 2020-06-03 08:55:14 +0300 | [diff] [blame] | 1062 | struct btrfs_root *root = inode->root; |
| 1063 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1064 | u64 alloc_hint = 0; |
| 1065 | u64 num_bytes; |
| 1066 | unsigned long ram_size; |
Filipe Manana | a315e68 | 2017-03-06 23:04:20 +0000 | [diff] [blame] | 1067 | u64 cur_alloc_size = 0; |
Filipe Manana | 432cd2a | 2020-06-08 13:32:55 +0100 | [diff] [blame] | 1068 | u64 min_alloc_size; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1069 | u64 blocksize = fs_info->sectorsize; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1070 | struct btrfs_key ins; |
| 1071 | struct extent_map *em; |
Filipe Manana | a315e68 | 2017-03-06 23:04:20 +0000 | [diff] [blame] | 1072 | unsigned clear_bits; |
| 1073 | unsigned long page_ops; |
| 1074 | bool extent_reserved = false; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1075 | int ret = 0; |
| 1076 | |
Nikolay Borisov | 6e26c44 | 2020-06-03 08:55:14 +0300 | [diff] [blame] | 1077 | if (btrfs_is_free_space_inode(inode)) { |
Josef Bacik | 02ecd2c | 2013-10-25 16:19:08 -0400 | [diff] [blame] | 1078 | WARN_ON_ONCE(1); |
Josef Bacik | 29bce2f | 2014-02-07 12:21:23 -0500 | [diff] [blame] | 1079 | ret = -EINVAL; |
| 1080 | goto out_unlock; |
Josef Bacik | 02ecd2c | 2013-10-25 16:19:08 -0400 | [diff] [blame] | 1081 | } |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1082 | |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 1083 | num_bytes = ALIGN(end - start + 1, blocksize); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1084 | num_bytes = max(blocksize, num_bytes); |
Anand Jain | 566b176 | 2018-02-15 18:07:59 +0800 | [diff] [blame] | 1085 | ASSERT(num_bytes <= btrfs_super_total_bytes(fs_info->super_copy)); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1086 | |
Nikolay Borisov | 6e26c44 | 2020-06-03 08:55:14 +0300 | [diff] [blame] | 1087 | inode_should_defrag(inode, start, end, num_bytes, SZ_64K); |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1088 | |
Qu Wenruo | 7367253 | 2021-07-26 14:34:59 +0800 | [diff] [blame] | 1089 | /* |
| 1090 | * Due to the page size limit, for subpage we can only trigger the |
| 1091 | * writeback for the dirty sectors of page, that means data writeback |
| 1092 | * is doing more writeback than what we want. |
| 1093 | * |
| 1094 | * This is especially unexpected for some call sites like fallocate, |
| 1095 | * where we only increase i_size after everything is done. |
| 1096 | * This means we can trigger inline extent even if we didn't want to. |
| 1097 | * So here we skip inline extent creation completely. |
| 1098 | */ |
| 1099 | if (start == 0 && fs_info->sectorsize == PAGE_SIZE) { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1100 | /* lets try to make an inline extent */ |
Nikolay Borisov | 6e26c44 | 2020-06-03 08:55:14 +0300 | [diff] [blame] | 1101 | ret = cow_file_range_inline(inode, start, end, 0, |
Nikolay Borisov | d02c0e2 | 2018-03-02 09:43:15 +0200 | [diff] [blame] | 1102 | BTRFS_COMPRESS_NONE, NULL); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1103 | if (ret == 0) { |
Josef Bacik | 8b62f87 | 2017-10-19 14:15:55 -0400 | [diff] [blame] | 1104 | /* |
| 1105 | * We use DO_ACCOUNTING here because we need the |
| 1106 | * delalloc_release_metadata to be run _after_ we drop |
| 1107 | * our outstanding extent for clearing delalloc for this |
| 1108 | * range. |
| 1109 | */ |
Qu Wenruo | 4750af3 | 2021-05-31 16:50:48 +0800 | [diff] [blame] | 1110 | extent_clear_unlock_delalloc(inode, start, end, |
| 1111 | locked_page, |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1112 | EXTENT_LOCKED | EXTENT_DELALLOC | |
Josef Bacik | 8b62f87 | 2017-10-19 14:15:55 -0400 | [diff] [blame] | 1113 | EXTENT_DELALLOC_NEW | EXTENT_DEFRAG | |
| 1114 | EXTENT_DO_ACCOUNTING, PAGE_UNLOCK | |
Qu Wenruo | 6869b0a | 2021-01-26 16:33:45 +0800 | [diff] [blame] | 1115 | PAGE_START_WRITEBACK | PAGE_END_WRITEBACK); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1116 | *nr_written = *nr_written + |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1117 | (end - start + PAGE_SIZE) / PAGE_SIZE; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1118 | *page_started = 1; |
Qu Wenruo | 4750af3 | 2021-05-31 16:50:48 +0800 | [diff] [blame] | 1119 | /* |
| 1120 | * locked_page is locked by the caller of |
| 1121 | * writepage_delalloc(), not locked by |
| 1122 | * __process_pages_contig(). |
| 1123 | * |
| 1124 | * We can't let __process_pages_contig() to unlock it, |
| 1125 | * as it doesn't have any subpage::writers recorded. |
| 1126 | * |
| 1127 | * Here we manually unlock the page, since the caller |
| 1128 | * can't use page_started to determine if it's an |
| 1129 | * inline extent or a compressed extent. |
| 1130 | */ |
| 1131 | unlock_page(locked_page); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1132 | goto out; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1133 | } else if (ret < 0) { |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1134 | goto out_unlock; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1135 | } |
| 1136 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1137 | |
Nikolay Borisov | 6e26c44 | 2020-06-03 08:55:14 +0300 | [diff] [blame] | 1138 | alloc_hint = get_extent_allocation_hint(inode, start, num_bytes); |
| 1139 | btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 1140 | |
Filipe Manana | 432cd2a | 2020-06-08 13:32:55 +0100 | [diff] [blame] | 1141 | /* |
| 1142 | * Relocation relies on the relocated extents to have exactly the same |
| 1143 | * size as the original extents. Normally writeback for relocation data |
| 1144 | * extents follows a NOCOW path because relocation preallocates the |
| 1145 | * extents. However, due to an operation such as scrub turning a block |
| 1146 | * group to RO mode, it may fallback to COW mode, so we must make sure |
| 1147 | * an extent allocated during COW has exactly the requested size and can |
| 1148 | * not be split into smaller extents, otherwise relocation breaks and |
| 1149 | * fails during the stage where it updates the bytenr of file extent |
| 1150 | * items. |
| 1151 | */ |
| 1152 | if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID) |
| 1153 | min_alloc_size = num_bytes; |
| 1154 | else |
| 1155 | min_alloc_size = fs_info->sectorsize; |
| 1156 | |
Anand Jain | 3752d22 | 2018-02-15 12:29:38 +0800 | [diff] [blame] | 1157 | while (num_bytes > 0) { |
| 1158 | cur_alloc_size = num_bytes; |
Wang Xiaoguang | 1851309 | 2016-07-25 15:51:40 +0800 | [diff] [blame] | 1159 | ret = btrfs_reserve_extent(root, cur_alloc_size, cur_alloc_size, |
Filipe Manana | 432cd2a | 2020-06-08 13:32:55 +0100 | [diff] [blame] | 1160 | min_alloc_size, 0, alloc_hint, |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 1161 | &ins, 1, 1); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 1162 | if (ret < 0) |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1163 | goto out_unlock; |
Filipe Manana | a315e68 | 2017-03-06 23:04:20 +0000 | [diff] [blame] | 1164 | cur_alloc_size = ins.offset; |
| 1165 | extent_reserved = true; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1166 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1167 | ram_size = ins.offset; |
Nikolay Borisov | 6e26c44 | 2020-06-03 08:55:14 +0300 | [diff] [blame] | 1168 | em = create_io_em(inode, start, ins.offset, /* len */ |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 1169 | start, /* orig_start */ |
| 1170 | ins.objectid, /* block_start */ |
| 1171 | ins.offset, /* block_len */ |
| 1172 | ins.offset, /* orig_block_len */ |
| 1173 | ram_size, /* ram_bytes */ |
| 1174 | BTRFS_COMPRESS_NONE, /* compress_type */ |
Liu Bo | 1af4a0a | 2017-02-13 15:35:09 -0800 | [diff] [blame] | 1175 | BTRFS_ORDERED_REGULAR /* type */); |
Su Yue | 090a127a | 2018-05-30 16:48:56 +0800 | [diff] [blame] | 1176 | if (IS_ERR(em)) { |
| 1177 | ret = PTR_ERR(em); |
Liu Bo | ace68ba | 2013-04-22 10:53:47 +0000 | [diff] [blame] | 1178 | goto out_reserve; |
Su Yue | 090a127a | 2018-05-30 16:48:56 +0800 | [diff] [blame] | 1179 | } |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 1180 | free_extent_map(em); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1181 | |
Nikolay Borisov | 6e26c44 | 2020-06-03 08:55:14 +0300 | [diff] [blame] | 1182 | ret = btrfs_add_ordered_extent(inode, start, ins.objectid, |
Qu Wenruo | 3c198fe | 2021-01-21 14:13:54 +0800 | [diff] [blame] | 1183 | ram_size, cur_alloc_size, |
| 1184 | BTRFS_ORDERED_REGULAR); |
Liu Bo | ace68ba | 2013-04-22 10:53:47 +0000 | [diff] [blame] | 1185 | if (ret) |
Filipe Manana | d9f8596 | 2014-08-25 10:43:00 +0100 | [diff] [blame] | 1186 | goto out_drop_extent_cache; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1187 | |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1188 | if (root->root_key.objectid == |
| 1189 | BTRFS_DATA_RELOC_TREE_OBJECTID) { |
Nikolay Borisov | 6e26c44 | 2020-06-03 08:55:14 +0300 | [diff] [blame] | 1190 | ret = btrfs_reloc_clone_csums(inode, start, |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1191 | cur_alloc_size); |
Qu Wenruo | 4dbd80f | 2017-03-08 10:25:51 +0800 | [diff] [blame] | 1192 | /* |
| 1193 | * Only drop cache here, and process as normal. |
| 1194 | * |
| 1195 | * We must not allow extent_clear_unlock_delalloc() |
| 1196 | * at out_unlock label to free meta of this ordered |
| 1197 | * extent, as its meta should be freed by |
| 1198 | * btrfs_finish_ordered_io(). |
| 1199 | * |
| 1200 | * So we must continue until @start is increased to |
| 1201 | * skip current ordered extent. |
| 1202 | */ |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 1203 | if (ret) |
Nikolay Borisov | 6e26c44 | 2020-06-03 08:55:14 +0300 | [diff] [blame] | 1204 | btrfs_drop_extent_cache(inode, start, |
Qu Wenruo | 4dbd80f | 2017-03-08 10:25:51 +0800 | [diff] [blame] | 1205 | start + ram_size - 1, 0); |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1206 | } |
| 1207 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1208 | btrfs_dec_block_group_reservations(fs_info, ins.objectid); |
Filipe Manana | 9cfa3e3 | 2016-04-26 15:39:32 +0100 | [diff] [blame] | 1209 | |
Qu Wenruo | f57ad93 | 2021-04-07 19:22:13 +0800 | [diff] [blame] | 1210 | /* |
| 1211 | * We're not doing compressed IO, don't unlock the first page |
| 1212 | * (which the caller expects to stay locked), don't clear any |
| 1213 | * dirty bits and don't set any writeback bits |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 1214 | * |
Qu Wenruo | f57ad93 | 2021-04-07 19:22:13 +0800 | [diff] [blame] | 1215 | * Do set the Ordered (Private2) bit so we know this page was |
| 1216 | * properly setup for writepage. |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1217 | */ |
Filipe Manana | a315e68 | 2017-03-06 23:04:20 +0000 | [diff] [blame] | 1218 | page_ops = unlock ? PAGE_UNLOCK : 0; |
Qu Wenruo | f57ad93 | 2021-04-07 19:22:13 +0800 | [diff] [blame] | 1219 | page_ops |= PAGE_SET_ORDERED; |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 1220 | |
Nikolay Borisov | 6e26c44 | 2020-06-03 08:55:14 +0300 | [diff] [blame] | 1221 | extent_clear_unlock_delalloc(inode, start, start + ram_size - 1, |
Nikolay Borisov | 74e9194 | 2019-07-17 16:18:16 +0300 | [diff] [blame] | 1222 | locked_page, |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1223 | EXTENT_LOCKED | EXTENT_DELALLOC, |
Filipe Manana | a315e68 | 2017-03-06 23:04:20 +0000 | [diff] [blame] | 1224 | page_ops); |
Anand Jain | 3752d22 | 2018-02-15 12:29:38 +0800 | [diff] [blame] | 1225 | if (num_bytes < cur_alloc_size) |
| 1226 | num_bytes = 0; |
Qu Wenruo | 4dbd80f | 2017-03-08 10:25:51 +0800 | [diff] [blame] | 1227 | else |
Anand Jain | 3752d22 | 2018-02-15 12:29:38 +0800 | [diff] [blame] | 1228 | num_bytes -= cur_alloc_size; |
Chris Mason | c59f895 | 2007-12-17 20:14:04 -0500 | [diff] [blame] | 1229 | alloc_hint = ins.objectid + ins.offset; |
| 1230 | start += cur_alloc_size; |
Filipe Manana | a315e68 | 2017-03-06 23:04:20 +0000 | [diff] [blame] | 1231 | extent_reserved = false; |
Qu Wenruo | 4dbd80f | 2017-03-08 10:25:51 +0800 | [diff] [blame] | 1232 | |
| 1233 | /* |
| 1234 | * btrfs_reloc_clone_csums() error, since start is increased |
| 1235 | * extent_clear_unlock_delalloc() at out_unlock label won't |
| 1236 | * free metadata of current ordered extent, we're OK to exit. |
| 1237 | */ |
| 1238 | if (ret) |
| 1239 | goto out_unlock; |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 1240 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1241 | out: |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1242 | return ret; |
Miao Xie | b7d5b0a | 2012-11-01 07:32:18 +0000 | [diff] [blame] | 1243 | |
Filipe Manana | d9f8596 | 2014-08-25 10:43:00 +0100 | [diff] [blame] | 1244 | out_drop_extent_cache: |
Nikolay Borisov | 6e26c44 | 2020-06-03 08:55:14 +0300 | [diff] [blame] | 1245 | btrfs_drop_extent_cache(inode, start, start + ram_size - 1, 0); |
Liu Bo | ace68ba | 2013-04-22 10:53:47 +0000 | [diff] [blame] | 1246 | out_reserve: |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1247 | btrfs_dec_block_group_reservations(fs_info, ins.objectid); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 1248 | btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1249 | out_unlock: |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 1250 | clear_bits = EXTENT_LOCKED | EXTENT_DELALLOC | EXTENT_DELALLOC_NEW | |
| 1251 | EXTENT_DEFRAG | EXTENT_CLEAR_META_RESV; |
Qu Wenruo | 6869b0a | 2021-01-26 16:33:45 +0800 | [diff] [blame] | 1252 | page_ops = PAGE_UNLOCK | PAGE_START_WRITEBACK | PAGE_END_WRITEBACK; |
Filipe Manana | a315e68 | 2017-03-06 23:04:20 +0000 | [diff] [blame] | 1253 | /* |
| 1254 | * If we reserved an extent for our delalloc range (or a subrange) and |
| 1255 | * failed to create the respective ordered extent, then it means that |
| 1256 | * when we reserved the extent we decremented the extent's size from |
| 1257 | * the data space_info's bytes_may_use counter and incremented the |
| 1258 | * space_info's bytes_reserved counter by the same amount. We must make |
| 1259 | * sure extent_clear_unlock_delalloc() does not try to decrement again |
| 1260 | * the data space_info's bytes_may_use counter, therefore we do not pass |
| 1261 | * it the flag EXTENT_CLEAR_DATA_RESV. |
| 1262 | */ |
| 1263 | if (extent_reserved) { |
Nikolay Borisov | 6e26c44 | 2020-06-03 08:55:14 +0300 | [diff] [blame] | 1264 | extent_clear_unlock_delalloc(inode, start, |
Filipe Manana | e2c8e92 | 2020-05-27 11:15:53 +0100 | [diff] [blame] | 1265 | start + cur_alloc_size - 1, |
Filipe Manana | a315e68 | 2017-03-06 23:04:20 +0000 | [diff] [blame] | 1266 | locked_page, |
| 1267 | clear_bits, |
| 1268 | page_ops); |
| 1269 | start += cur_alloc_size; |
| 1270 | if (start >= end) |
| 1271 | goto out; |
| 1272 | } |
Nikolay Borisov | 6e26c44 | 2020-06-03 08:55:14 +0300 | [diff] [blame] | 1273 | extent_clear_unlock_delalloc(inode, start, end, locked_page, |
Filipe Manana | a315e68 | 2017-03-06 23:04:20 +0000 | [diff] [blame] | 1274 | clear_bits | EXTENT_CLEAR_DATA_RESV, |
| 1275 | page_ops); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1276 | goto out; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1277 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1278 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1279 | /* |
| 1280 | * work queue call back to started compression on a file and pages |
| 1281 | */ |
| 1282 | static noinline void async_cow_start(struct btrfs_work *work) |
| 1283 | { |
Nikolay Borisov | b532627 | 2019-03-12 17:20:25 +0200 | [diff] [blame] | 1284 | struct async_chunk *async_chunk; |
Nikolay Borisov | ac3e993 | 2019-07-17 14:41:44 +0300 | [diff] [blame] | 1285 | int compressed_extents; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1286 | |
Nikolay Borisov | b532627 | 2019-03-12 17:20:25 +0200 | [diff] [blame] | 1287 | async_chunk = container_of(work, struct async_chunk, work); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1288 | |
Nikolay Borisov | ac3e993 | 2019-07-17 14:41:44 +0300 | [diff] [blame] | 1289 | compressed_extents = compress_file_range(async_chunk); |
| 1290 | if (compressed_extents == 0) { |
Nikolay Borisov | b532627 | 2019-03-12 17:20:25 +0200 | [diff] [blame] | 1291 | btrfs_add_delayed_iput(async_chunk->inode); |
| 1292 | async_chunk->inode = NULL; |
Josef Bacik | 8180ef8 | 2012-06-08 15:16:12 -0400 | [diff] [blame] | 1293 | } |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1294 | } |
| 1295 | |
| 1296 | /* |
| 1297 | * work queue call back to submit previously compressed pages |
| 1298 | */ |
| 1299 | static noinline void async_cow_submit(struct btrfs_work *work) |
| 1300 | { |
Nikolay Borisov | c5a68ae | 2019-03-12 17:20:26 +0200 | [diff] [blame] | 1301 | struct async_chunk *async_chunk = container_of(work, struct async_chunk, |
| 1302 | work); |
| 1303 | struct btrfs_fs_info *fs_info = btrfs_work_owner(work); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1304 | unsigned long nr_pages; |
| 1305 | |
Nikolay Borisov | b532627 | 2019-03-12 17:20:25 +0200 | [diff] [blame] | 1306 | nr_pages = (async_chunk->end - async_chunk->start + PAGE_SIZE) >> |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1307 | PAGE_SHIFT; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1308 | |
Nikolay Borisov | 4546d17 | 2019-01-03 10:50:03 +0200 | [diff] [blame] | 1309 | /* |
Nikolay Borisov | b532627 | 2019-03-12 17:20:25 +0200 | [diff] [blame] | 1310 | * ->inode could be NULL if async_chunk_start has failed to compress, |
Nikolay Borisov | 4546d17 | 2019-01-03 10:50:03 +0200 | [diff] [blame] | 1311 | * in which case we don't have anything to submit, yet we need to |
| 1312 | * always adjust ->async_delalloc_pages as its paired with the init |
| 1313 | * happening in cow_file_range_async |
| 1314 | */ |
Nikolay Borisov | b532627 | 2019-03-12 17:20:25 +0200 | [diff] [blame] | 1315 | if (async_chunk->inode) |
| 1316 | submit_compressed_extents(async_chunk); |
Josef Bacik | ac98141 | 2021-07-14 14:47:17 -0400 | [diff] [blame] | 1317 | |
| 1318 | /* atomic_sub_return implies a barrier */ |
| 1319 | if (atomic_sub_return(nr_pages, &fs_info->async_delalloc_pages) < |
| 1320 | 5 * SZ_1M) |
| 1321 | cond_wake_up_nomb(&fs_info->async_submit_wait); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1322 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1323 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1324 | static noinline void async_cow_free(struct btrfs_work *work) |
| 1325 | { |
Nikolay Borisov | b532627 | 2019-03-12 17:20:25 +0200 | [diff] [blame] | 1326 | struct async_chunk *async_chunk; |
Nikolay Borisov | 97db120 | 2019-03-12 17:20:24 +0200 | [diff] [blame] | 1327 | |
Nikolay Borisov | b532627 | 2019-03-12 17:20:25 +0200 | [diff] [blame] | 1328 | async_chunk = container_of(work, struct async_chunk, work); |
| 1329 | if (async_chunk->inode) |
| 1330 | btrfs_add_delayed_iput(async_chunk->inode); |
Chris Mason | ec39f76 | 2019-07-10 12:28:17 -0700 | [diff] [blame] | 1331 | if (async_chunk->blkcg_css) |
| 1332 | css_put(async_chunk->blkcg_css); |
Nikolay Borisov | 97db120 | 2019-03-12 17:20:24 +0200 | [diff] [blame] | 1333 | /* |
| 1334 | * Since the pointer to 'pending' is at the beginning of the array of |
Nikolay Borisov | b532627 | 2019-03-12 17:20:25 +0200 | [diff] [blame] | 1335 | * async_chunk's, freeing it ensures the whole array has been freed. |
Nikolay Borisov | 97db120 | 2019-03-12 17:20:24 +0200 | [diff] [blame] | 1336 | */ |
Nikolay Borisov | b532627 | 2019-03-12 17:20:25 +0200 | [diff] [blame] | 1337 | if (atomic_dec_and_test(async_chunk->pending)) |
Nikolay Borisov | b1c16ac | 2019-04-01 11:29:57 +0300 | [diff] [blame] | 1338 | kvfree(async_chunk->pending); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1339 | } |
| 1340 | |
Nikolay Borisov | 751b643 | 2020-06-03 08:55:22 +0300 | [diff] [blame] | 1341 | static int cow_file_range_async(struct btrfs_inode *inode, |
Chris Mason | ec39f76 | 2019-07-10 12:28:17 -0700 | [diff] [blame] | 1342 | struct writeback_control *wbc, |
| 1343 | struct page *locked_page, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1344 | u64 start, u64 end, int *page_started, |
David Sterba | fac07d2 | 2019-10-29 18:28:57 +0100 | [diff] [blame] | 1345 | unsigned long *nr_written) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1346 | { |
Nikolay Borisov | 751b643 | 2020-06-03 08:55:22 +0300 | [diff] [blame] | 1347 | struct btrfs_fs_info *fs_info = inode->root->fs_info; |
Chris Mason | ec39f76 | 2019-07-10 12:28:17 -0700 | [diff] [blame] | 1348 | struct cgroup_subsys_state *blkcg_css = wbc_blkcg_css(wbc); |
Nikolay Borisov | 97db120 | 2019-03-12 17:20:24 +0200 | [diff] [blame] | 1349 | struct async_cow *ctx; |
| 1350 | struct async_chunk *async_chunk; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1351 | unsigned long nr_pages; |
| 1352 | u64 cur_end; |
Nikolay Borisov | 97db120 | 2019-03-12 17:20:24 +0200 | [diff] [blame] | 1353 | u64 num_chunks = DIV_ROUND_UP(end - start, SZ_512K); |
| 1354 | int i; |
| 1355 | bool should_compress; |
Nikolay Borisov | b1c16ac | 2019-04-01 11:29:57 +0300 | [diff] [blame] | 1356 | unsigned nofs_flag; |
David Sterba | fac07d2 | 2019-10-29 18:28:57 +0100 | [diff] [blame] | 1357 | const unsigned int write_flags = wbc_to_write_flags(wbc); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1358 | |
Nikolay Borisov | 751b643 | 2020-06-03 08:55:22 +0300 | [diff] [blame] | 1359 | unlock_extent(&inode->io_tree, start, end); |
Nikolay Borisov | 97db120 | 2019-03-12 17:20:24 +0200 | [diff] [blame] | 1360 | |
Nikolay Borisov | 751b643 | 2020-06-03 08:55:22 +0300 | [diff] [blame] | 1361 | if (inode->flags & BTRFS_INODE_NOCOMPRESS && |
Nikolay Borisov | 97db120 | 2019-03-12 17:20:24 +0200 | [diff] [blame] | 1362 | !btrfs_test_opt(fs_info, FORCE_COMPRESS)) { |
| 1363 | num_chunks = 1; |
| 1364 | should_compress = false; |
| 1365 | } else { |
| 1366 | should_compress = true; |
| 1367 | } |
| 1368 | |
Nikolay Borisov | b1c16ac | 2019-04-01 11:29:57 +0300 | [diff] [blame] | 1369 | nofs_flag = memalloc_nofs_save(); |
| 1370 | ctx = kvmalloc(struct_size(ctx, chunks, num_chunks), GFP_KERNEL); |
| 1371 | memalloc_nofs_restore(nofs_flag); |
| 1372 | |
Nikolay Borisov | 97db120 | 2019-03-12 17:20:24 +0200 | [diff] [blame] | 1373 | if (!ctx) { |
| 1374 | unsigned clear_bits = EXTENT_LOCKED | EXTENT_DELALLOC | |
| 1375 | EXTENT_DELALLOC_NEW | EXTENT_DEFRAG | |
| 1376 | EXTENT_DO_ACCOUNTING; |
Qu Wenruo | 6869b0a | 2021-01-26 16:33:45 +0800 | [diff] [blame] | 1377 | unsigned long page_ops = PAGE_UNLOCK | PAGE_START_WRITEBACK | |
| 1378 | PAGE_END_WRITEBACK | PAGE_SET_ERROR; |
Nikolay Borisov | 97db120 | 2019-03-12 17:20:24 +0200 | [diff] [blame] | 1379 | |
Nikolay Borisov | 751b643 | 2020-06-03 08:55:22 +0300 | [diff] [blame] | 1380 | extent_clear_unlock_delalloc(inode, start, end, locked_page, |
| 1381 | clear_bits, page_ops); |
Nikolay Borisov | 97db120 | 2019-03-12 17:20:24 +0200 | [diff] [blame] | 1382 | return -ENOMEM; |
| 1383 | } |
| 1384 | |
| 1385 | async_chunk = ctx->chunks; |
| 1386 | atomic_set(&ctx->num_chunks, num_chunks); |
| 1387 | |
| 1388 | for (i = 0; i < num_chunks; i++) { |
| 1389 | if (should_compress) |
| 1390 | cur_end = min(end, start + SZ_512K - 1); |
| 1391 | else |
| 1392 | cur_end = end; |
| 1393 | |
Nikolay Borisov | bd4691a | 2019-01-03 10:50:01 +0200 | [diff] [blame] | 1394 | /* |
| 1395 | * igrab is called higher up in the call chain, take only the |
| 1396 | * lightweight reference for the callback lifetime |
| 1397 | */ |
Nikolay Borisov | 751b643 | 2020-06-03 08:55:22 +0300 | [diff] [blame] | 1398 | ihold(&inode->vfs_inode); |
Nikolay Borisov | 97db120 | 2019-03-12 17:20:24 +0200 | [diff] [blame] | 1399 | async_chunk[i].pending = &ctx->num_chunks; |
Nikolay Borisov | 751b643 | 2020-06-03 08:55:22 +0300 | [diff] [blame] | 1400 | async_chunk[i].inode = &inode->vfs_inode; |
Nikolay Borisov | 97db120 | 2019-03-12 17:20:24 +0200 | [diff] [blame] | 1401 | async_chunk[i].start = start; |
| 1402 | async_chunk[i].end = cur_end; |
Nikolay Borisov | 97db120 | 2019-03-12 17:20:24 +0200 | [diff] [blame] | 1403 | async_chunk[i].write_flags = write_flags; |
| 1404 | INIT_LIST_HEAD(&async_chunk[i].extents); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1405 | |
Chris Mason | 1d53c9e | 2019-07-10 12:28:16 -0700 | [diff] [blame] | 1406 | /* |
| 1407 | * The locked_page comes all the way from writepage and its |
| 1408 | * the original page we were actually given. As we spread |
| 1409 | * this large delalloc region across multiple async_chunk |
| 1410 | * structs, only the first struct needs a pointer to locked_page |
| 1411 | * |
| 1412 | * This way we don't need racey decisions about who is supposed |
| 1413 | * to unlock it. |
| 1414 | */ |
| 1415 | if (locked_page) { |
Chris Mason | ec39f76 | 2019-07-10 12:28:17 -0700 | [diff] [blame] | 1416 | /* |
| 1417 | * Depending on the compressibility, the pages might or |
| 1418 | * might not go through async. We want all of them to |
| 1419 | * be accounted against wbc once. Let's do it here |
| 1420 | * before the paths diverge. wbc accounting is used |
| 1421 | * only for foreign writeback detection and doesn't |
| 1422 | * need full accuracy. Just account the whole thing |
| 1423 | * against the first page. |
| 1424 | */ |
| 1425 | wbc_account_cgroup_owner(wbc, locked_page, |
| 1426 | cur_end - start); |
Chris Mason | 1d53c9e | 2019-07-10 12:28:16 -0700 | [diff] [blame] | 1427 | async_chunk[i].locked_page = locked_page; |
| 1428 | locked_page = NULL; |
| 1429 | } else { |
| 1430 | async_chunk[i].locked_page = NULL; |
| 1431 | } |
| 1432 | |
Chris Mason | ec39f76 | 2019-07-10 12:28:17 -0700 | [diff] [blame] | 1433 | if (blkcg_css != blkcg_root_css) { |
| 1434 | css_get(blkcg_css); |
| 1435 | async_chunk[i].blkcg_css = blkcg_css; |
| 1436 | } else { |
| 1437 | async_chunk[i].blkcg_css = NULL; |
| 1438 | } |
| 1439 | |
Omar Sandoval | a0cac0e | 2019-09-16 11:30:57 -0700 | [diff] [blame] | 1440 | btrfs_init_work(&async_chunk[i].work, async_cow_start, |
| 1441 | async_cow_submit, async_cow_free); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1442 | |
Nikolay Borisov | 97db120 | 2019-03-12 17:20:24 +0200 | [diff] [blame] | 1443 | nr_pages = DIV_ROUND_UP(cur_end - start, PAGE_SIZE); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1444 | atomic_add(nr_pages, &fs_info->async_delalloc_pages); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1445 | |
Nikolay Borisov | 97db120 | 2019-03-12 17:20:24 +0200 | [diff] [blame] | 1446 | btrfs_queue_work(fs_info->delalloc_workers, &async_chunk[i].work); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1447 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1448 | *nr_written += nr_pages; |
| 1449 | start = cur_end + 1; |
| 1450 | } |
| 1451 | *page_started = 1; |
| 1452 | return 0; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1453 | } |
| 1454 | |
Naohiro Aota | 42c0110 | 2021-02-04 19:22:07 +0900 | [diff] [blame] | 1455 | static noinline int run_delalloc_zoned(struct btrfs_inode *inode, |
| 1456 | struct page *locked_page, u64 start, |
| 1457 | u64 end, int *page_started, |
| 1458 | unsigned long *nr_written) |
| 1459 | { |
| 1460 | int ret; |
| 1461 | |
| 1462 | ret = cow_file_range(inode, locked_page, start, end, page_started, |
| 1463 | nr_written, 0); |
| 1464 | if (ret) |
| 1465 | return ret; |
| 1466 | |
| 1467 | if (*page_started) |
| 1468 | return 0; |
| 1469 | |
| 1470 | __set_page_dirty_nobuffers(locked_page); |
| 1471 | account_page_redirty(locked_page); |
| 1472 | extent_write_locked_range(&inode->vfs_inode, start, end, WB_SYNC_ALL); |
| 1473 | *page_started = 1; |
| 1474 | |
| 1475 | return 0; |
| 1476 | } |
| 1477 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 1478 | 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] | 1479 | u64 bytenr, u64 num_bytes) |
| 1480 | { |
| 1481 | int ret; |
| 1482 | struct btrfs_ordered_sum *sums; |
| 1483 | LIST_HEAD(list); |
| 1484 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1485 | ret = btrfs_lookup_csums_range(fs_info->csum_root, bytenr, |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 1486 | bytenr + num_bytes - 1, &list, 0); |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1487 | if (ret == 0 && list_empty(&list)) |
| 1488 | return 0; |
| 1489 | |
| 1490 | while (!list_empty(&list)) { |
| 1491 | sums = list_entry(list.next, struct btrfs_ordered_sum, list); |
| 1492 | list_del(&sums->list); |
| 1493 | kfree(sums); |
| 1494 | } |
Liu Bo | 5811375 | 2018-01-31 17:09:13 -0700 | [diff] [blame] | 1495 | if (ret < 0) |
| 1496 | return ret; |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1497 | return 1; |
| 1498 | } |
| 1499 | |
Nikolay Borisov | 8ba96f3 | 2020-06-03 08:55:20 +0300 | [diff] [blame] | 1500 | 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] | 1501 | const u64 start, const u64 end, |
| 1502 | int *page_started, unsigned long *nr_written) |
| 1503 | { |
Nikolay Borisov | 8ba96f3 | 2020-06-03 08:55:20 +0300 | [diff] [blame] | 1504 | const bool is_space_ino = btrfs_is_free_space_inode(inode); |
| 1505 | const bool is_reloc_ino = (inode->root->root_key.objectid == |
Filipe Manana | 6bd335b | 2020-06-08 13:33:05 +0100 | [diff] [blame] | 1506 | BTRFS_DATA_RELOC_TREE_OBJECTID); |
Filipe Manana | 2166e5e | 2020-05-27 11:16:19 +0100 | [diff] [blame] | 1507 | const u64 range_bytes = end + 1 - start; |
Nikolay Borisov | 8ba96f3 | 2020-06-03 08:55:20 +0300 | [diff] [blame] | 1508 | struct extent_io_tree *io_tree = &inode->io_tree; |
Filipe Manana | 467dc47 | 2020-05-27 11:16:07 +0100 | [diff] [blame] | 1509 | u64 range_start = start; |
| 1510 | u64 count; |
| 1511 | |
| 1512 | /* |
| 1513 | * If EXTENT_NORESERVE is set it means that when the buffered write was |
| 1514 | * made we had not enough available data space and therefore we did not |
| 1515 | * reserve data space for it, since we though we could do NOCOW for the |
| 1516 | * respective file range (either there is prealloc extent or the inode |
| 1517 | * has the NOCOW bit set). |
| 1518 | * |
| 1519 | * However when we need to fallback to COW mode (because for example the |
| 1520 | * block group for the corresponding extent was turned to RO mode by a |
| 1521 | * scrub or relocation) we need to do the following: |
| 1522 | * |
| 1523 | * 1) We increment the bytes_may_use counter of the data space info. |
| 1524 | * If COW succeeds, it allocates a new data extent and after doing |
| 1525 | * that it decrements the space info's bytes_may_use counter and |
| 1526 | * increments its bytes_reserved counter by the same amount (we do |
| 1527 | * this at btrfs_add_reserved_bytes()). So we need to increment the |
| 1528 | * bytes_may_use counter to compensate (when space is reserved at |
| 1529 | * buffered write time, the bytes_may_use counter is incremented); |
| 1530 | * |
| 1531 | * 2) We clear the EXTENT_NORESERVE bit from the range. We do this so |
| 1532 | * that if the COW path fails for any reason, it decrements (through |
| 1533 | * extent_clear_unlock_delalloc()) the bytes_may_use counter of the |
| 1534 | * data space info, which we incremented in the step above. |
Filipe Manana | 2166e5e | 2020-05-27 11:16:19 +0100 | [diff] [blame] | 1535 | * |
| 1536 | * 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] | 1537 | * space cache inode or an inode of the data relocation tree, we must |
| 1538 | * also increment bytes_may_use of the data space_info for the same |
| 1539 | * reason. Space caches and relocated data extents always get a prealloc |
Filipe Manana | 2166e5e | 2020-05-27 11:16:19 +0100 | [diff] [blame] | 1540 | * extent for them, however scrub or balance may have set the block |
Filipe Manana | 6bd335b | 2020-06-08 13:33:05 +0100 | [diff] [blame] | 1541 | * group that contains that extent to RO mode and therefore force COW |
| 1542 | * when starting writeback. |
Filipe Manana | 467dc47 | 2020-05-27 11:16:07 +0100 | [diff] [blame] | 1543 | */ |
Filipe Manana | 2166e5e | 2020-05-27 11:16:19 +0100 | [diff] [blame] | 1544 | count = count_range_bits(io_tree, &range_start, end, range_bytes, |
Filipe Manana | 467dc47 | 2020-05-27 11:16:07 +0100 | [diff] [blame] | 1545 | EXTENT_NORESERVE, 0); |
Filipe Manana | 6bd335b | 2020-06-08 13:33:05 +0100 | [diff] [blame] | 1546 | if (count > 0 || is_space_ino || is_reloc_ino) { |
| 1547 | u64 bytes = count; |
Nikolay Borisov | 8ba96f3 | 2020-06-03 08:55:20 +0300 | [diff] [blame] | 1548 | struct btrfs_fs_info *fs_info = inode->root->fs_info; |
Filipe Manana | 467dc47 | 2020-05-27 11:16:07 +0100 | [diff] [blame] | 1549 | struct btrfs_space_info *sinfo = fs_info->data_sinfo; |
| 1550 | |
Filipe Manana | 6bd335b | 2020-06-08 13:33:05 +0100 | [diff] [blame] | 1551 | if (is_space_ino || is_reloc_ino) |
| 1552 | bytes = range_bytes; |
| 1553 | |
Filipe Manana | 467dc47 | 2020-05-27 11:16:07 +0100 | [diff] [blame] | 1554 | spin_lock(&sinfo->lock); |
Filipe Manana | 2166e5e | 2020-05-27 11:16:19 +0100 | [diff] [blame] | 1555 | btrfs_space_info_update_bytes_may_use(fs_info, sinfo, bytes); |
Filipe Manana | 467dc47 | 2020-05-27 11:16:07 +0100 | [diff] [blame] | 1556 | spin_unlock(&sinfo->lock); |
| 1557 | |
Filipe Manana | 2166e5e | 2020-05-27 11:16:19 +0100 | [diff] [blame] | 1558 | if (count > 0) |
| 1559 | clear_extent_bit(io_tree, start, end, EXTENT_NORESERVE, |
| 1560 | 0, 0, NULL); |
Filipe Manana | 467dc47 | 2020-05-27 11:16:07 +0100 | [diff] [blame] | 1561 | } |
| 1562 | |
Nikolay Borisov | 8ba96f3 | 2020-06-03 08:55:20 +0300 | [diff] [blame] | 1563 | return cow_file_range(inode, locked_page, start, end, page_started, |
| 1564 | nr_written, 1); |
Filipe Manana | 467dc47 | 2020-05-27 11:16:07 +0100 | [diff] [blame] | 1565 | } |
| 1566 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1567 | /* |
| 1568 | * when nowcow writeback call back. This checks for snapshots or COW copies |
| 1569 | * of the extents that exist in the file, and COWs the file as required. |
| 1570 | * |
| 1571 | * If no cow copies or snapshots exist, we write directly to the existing |
| 1572 | * blocks on disk |
| 1573 | */ |
Nikolay Borisov | 968322c | 2020-06-03 08:55:21 +0300 | [diff] [blame] | 1574 | static noinline int run_delalloc_nocow(struct btrfs_inode *inode, |
Chris Mason | 7f366cf | 2009-03-12 20:12:45 -0400 | [diff] [blame] | 1575 | struct page *locked_page, |
Nikolay Borisov | 3e02484 | 2019-08-21 10:42:03 +0300 | [diff] [blame] | 1576 | const u64 start, const u64 end, |
Goldwyn Rodrigues | 6e65ae7 | 2021-03-04 09:06:25 -0600 | [diff] [blame] | 1577 | int *page_started, |
Nikolay Borisov | 3e02484 | 2019-08-21 10:42:03 +0300 | [diff] [blame] | 1578 | unsigned long *nr_written) |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1579 | { |
Nikolay Borisov | 968322c | 2020-06-03 08:55:21 +0300 | [diff] [blame] | 1580 | struct btrfs_fs_info *fs_info = inode->root->fs_info; |
| 1581 | struct btrfs_root *root = inode->root; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1582 | struct btrfs_path *path; |
Nikolay Borisov | 3e02484 | 2019-08-21 10:42:03 +0300 | [diff] [blame] | 1583 | u64 cow_start = (u64)-1; |
| 1584 | u64 cur_offset = start; |
Robbie Ko | 8ecebf4d | 2018-08-06 10:30:30 +0800 | [diff] [blame] | 1585 | int ret; |
Nikolay Borisov | 3e02484 | 2019-08-21 10:42:03 +0300 | [diff] [blame] | 1586 | bool check_prev = true; |
Nikolay Borisov | 968322c | 2020-06-03 08:55:21 +0300 | [diff] [blame] | 1587 | const bool freespace_inode = btrfs_is_free_space_inode(inode); |
| 1588 | u64 ino = btrfs_ino(inode); |
Nikolay Borisov | 762bf09 | 2019-08-22 17:24:20 +0300 | [diff] [blame] | 1589 | bool nocow = false; |
| 1590 | u64 disk_bytenr = 0; |
Goldwyn Rodrigues | 6e65ae7 | 2021-03-04 09:06:25 -0600 | [diff] [blame] | 1591 | const bool force = inode->flags & BTRFS_INODE_NODATACOW; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1592 | |
| 1593 | path = btrfs_alloc_path(); |
Josef Bacik | 17ca04a | 2012-05-31 15:58:55 -0400 | [diff] [blame] | 1594 | if (!path) { |
Nikolay Borisov | 968322c | 2020-06-03 08:55:21 +0300 | [diff] [blame] | 1595 | extent_clear_unlock_delalloc(inode, start, end, locked_page, |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1596 | EXTENT_LOCKED | EXTENT_DELALLOC | |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 1597 | EXTENT_DO_ACCOUNTING | |
| 1598 | EXTENT_DEFRAG, PAGE_UNLOCK | |
Qu Wenruo | 6869b0a | 2021-01-26 16:33:45 +0800 | [diff] [blame] | 1599 | PAGE_START_WRITEBACK | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1600 | PAGE_END_WRITEBACK); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 1601 | return -ENOMEM; |
Josef Bacik | 17ca04a | 2012-05-31 15:58:55 -0400 | [diff] [blame] | 1602 | } |
Li Zefan | 82d5902 | 2011-04-20 10:33:24 +0800 | [diff] [blame] | 1603 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1604 | while (1) { |
Nikolay Borisov | 3e02484 | 2019-08-21 10:42:03 +0300 | [diff] [blame] | 1605 | struct btrfs_key found_key; |
| 1606 | struct btrfs_file_extent_item *fi; |
| 1607 | struct extent_buffer *leaf; |
| 1608 | u64 extent_end; |
| 1609 | u64 extent_offset; |
Nikolay Borisov | 3e02484 | 2019-08-21 10:42:03 +0300 | [diff] [blame] | 1610 | u64 num_bytes = 0; |
| 1611 | u64 disk_num_bytes; |
Nikolay Borisov | 3e02484 | 2019-08-21 10:42:03 +0300 | [diff] [blame] | 1612 | u64 ram_bytes; |
| 1613 | int extent_type; |
Nikolay Borisov | 762bf09 | 2019-08-22 17:24:20 +0300 | [diff] [blame] | 1614 | |
| 1615 | nocow = false; |
Nikolay Borisov | 3e02484 | 2019-08-21 10:42:03 +0300 | [diff] [blame] | 1616 | |
Liu Bo | e4c3b2d | 2017-01-30 12:25:28 -0800 | [diff] [blame] | 1617 | ret = btrfs_lookup_file_extent(NULL, root, path, ino, |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1618 | cur_offset, 0); |
Josef Bacik | d788a34 | 2013-10-25 16:55:08 -0400 | [diff] [blame] | 1619 | if (ret < 0) |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1620 | goto error; |
Nikolay Borisov | a6bd9cd | 2019-08-21 10:42:57 +0300 | [diff] [blame] | 1621 | |
| 1622 | /* |
| 1623 | * If there is no extent for our range when doing the initial |
| 1624 | * search, then go back to the previous slot as it will be the |
| 1625 | * one containing the search offset |
| 1626 | */ |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1627 | if (ret > 0 && path->slots[0] > 0 && check_prev) { |
| 1628 | leaf = path->nodes[0]; |
| 1629 | btrfs_item_key_to_cpu(leaf, &found_key, |
| 1630 | path->slots[0] - 1); |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 1631 | if (found_key.objectid == ino && |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1632 | found_key.type == BTRFS_EXTENT_DATA_KEY) |
| 1633 | path->slots[0]--; |
| 1634 | } |
Nikolay Borisov | 3e02484 | 2019-08-21 10:42:03 +0300 | [diff] [blame] | 1635 | check_prev = false; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1636 | next_slot: |
Nikolay Borisov | a6bd9cd | 2019-08-21 10:42:57 +0300 | [diff] [blame] | 1637 | /* Go to next leaf if we have exhausted the current one */ |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1638 | leaf = path->nodes[0]; |
| 1639 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { |
| 1640 | ret = btrfs_next_leaf(root, path); |
Liu Bo | e891669 | 2018-01-25 11:02:50 -0700 | [diff] [blame] | 1641 | if (ret < 0) { |
| 1642 | if (cow_start != (u64)-1) |
| 1643 | cur_offset = cow_start; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1644 | goto error; |
Liu Bo | e891669 | 2018-01-25 11:02:50 -0700 | [diff] [blame] | 1645 | } |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1646 | if (ret > 0) |
| 1647 | break; |
| 1648 | leaf = path->nodes[0]; |
| 1649 | } |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1650 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1651 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1652 | |
Nikolay Borisov | a6bd9cd | 2019-08-21 10:42:57 +0300 | [diff] [blame] | 1653 | /* Didn't find anything for our INO */ |
Filipe Manana | 1d512cb | 2015-11-09 00:33:58 +0000 | [diff] [blame] | 1654 | if (found_key.objectid > ino) |
| 1655 | break; |
Nikolay Borisov | a6bd9cd | 2019-08-21 10:42:57 +0300 | [diff] [blame] | 1656 | /* |
| 1657 | * Keep searching until we find an EXTENT_ITEM or there are no |
| 1658 | * more extents for this inode |
| 1659 | */ |
Filipe Manana | 1d512cb | 2015-11-09 00:33:58 +0000 | [diff] [blame] | 1660 | if (WARN_ON_ONCE(found_key.objectid < ino) || |
| 1661 | found_key.type < BTRFS_EXTENT_DATA_KEY) { |
| 1662 | path->slots[0]++; |
| 1663 | goto next_slot; |
| 1664 | } |
Nikolay Borisov | a6bd9cd | 2019-08-21 10:42:57 +0300 | [diff] [blame] | 1665 | |
| 1666 | /* Found key is not EXTENT_DATA_KEY or starts after req range */ |
Filipe Manana | 1d512cb | 2015-11-09 00:33:58 +0000 | [diff] [blame] | 1667 | if (found_key.type > BTRFS_EXTENT_DATA_KEY || |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1668 | found_key.offset > end) |
| 1669 | break; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1670 | |
Nikolay Borisov | a6bd9cd | 2019-08-21 10:42:57 +0300 | [diff] [blame] | 1671 | /* |
| 1672 | * If the found extent starts after requested offset, then |
| 1673 | * adjust extent_end to be right before this extent begins |
| 1674 | */ |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1675 | if (found_key.offset > cur_offset) { |
| 1676 | extent_end = found_key.offset; |
Chris Mason | e9061e2 | 2009-10-09 09:57:45 -0400 | [diff] [blame] | 1677 | extent_type = 0; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1678 | goto out_check; |
| 1679 | } |
Chris Mason | c31f883 | 2008-01-08 15:46:31 -0500 | [diff] [blame] | 1680 | |
Nikolay Borisov | a6bd9cd | 2019-08-21 10:42:57 +0300 | [diff] [blame] | 1681 | /* |
| 1682 | * Found extent which begins before our range and potentially |
| 1683 | * intersect it |
| 1684 | */ |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1685 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 1686 | struct btrfs_file_extent_item); |
| 1687 | extent_type = btrfs_file_extent_type(leaf, fi); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1688 | |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 1689 | ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1690 | if (extent_type == BTRFS_FILE_EXTENT_REG || |
| 1691 | extent_type == BTRFS_FILE_EXTENT_PREALLOC) { |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1692 | disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1693 | extent_offset = btrfs_file_extent_offset(leaf, fi); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1694 | extent_end = found_key.offset + |
| 1695 | btrfs_file_extent_num_bytes(leaf, fi); |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 1696 | disk_num_bytes = |
| 1697 | btrfs_file_extent_disk_num_bytes(leaf, fi); |
Nikolay Borisov | a6bd9cd | 2019-08-21 10:42:57 +0300 | [diff] [blame] | 1698 | /* |
Filipe Manana | de7999a | 2019-12-11 09:01:40 +0000 | [diff] [blame] | 1699 | * If the extent we got ends before our current offset, |
| 1700 | * skip to the next extent. |
Nikolay Borisov | a6bd9cd | 2019-08-21 10:42:57 +0300 | [diff] [blame] | 1701 | */ |
Filipe Manana | de7999a | 2019-12-11 09:01:40 +0000 | [diff] [blame] | 1702 | if (extent_end <= cur_offset) { |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1703 | path->slots[0]++; |
| 1704 | goto next_slot; |
| 1705 | } |
Nikolay Borisov | a6bd9cd | 2019-08-21 10:42:57 +0300 | [diff] [blame] | 1706 | /* Skip holes */ |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1707 | if (disk_bytenr == 0) |
| 1708 | goto out_check; |
Nikolay Borisov | a6bd9cd | 2019-08-21 10:42:57 +0300 | [diff] [blame] | 1709 | /* Skip compressed/encrypted/encoded extents */ |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1710 | if (btrfs_file_extent_compression(leaf, fi) || |
| 1711 | btrfs_file_extent_encryption(leaf, fi) || |
| 1712 | btrfs_file_extent_other_encoding(leaf, fi)) |
| 1713 | goto out_check; |
Ethan Lien | 78d4295 | 2018-05-17 14:58:29 +0800 | [diff] [blame] | 1714 | /* |
Nikolay Borisov | a6bd9cd | 2019-08-21 10:42:57 +0300 | [diff] [blame] | 1715 | * If extent is created before the last volume's snapshot |
| 1716 | * this implies the extent is shared, hence we can't do |
| 1717 | * nocow. This is the same check as in |
| 1718 | * btrfs_cross_ref_exist but without calling |
| 1719 | * btrfs_search_slot. |
Ethan Lien | 78d4295 | 2018-05-17 14:58:29 +0800 | [diff] [blame] | 1720 | */ |
Nikolay Borisov | 3e02484 | 2019-08-21 10:42:03 +0300 | [diff] [blame] | 1721 | if (!freespace_inode && |
Lu Fengqi | 27a7ff5 | 2018-11-29 17:31:32 +0800 | [diff] [blame] | 1722 | btrfs_file_extent_generation(leaf, fi) <= |
Ethan Lien | 78d4295 | 2018-05-17 14:58:29 +0800 | [diff] [blame] | 1723 | btrfs_root_last_snapshot(&root->root_item)) |
| 1724 | goto out_check; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1725 | if (extent_type == BTRFS_FILE_EXTENT_REG && !force) |
| 1726 | goto out_check; |
Filipe Manana | c65ca98 | 2020-11-18 11:00:17 +0000 | [diff] [blame] | 1727 | |
| 1728 | /* |
| 1729 | * The following checks can be expensive, as they need to |
| 1730 | * take other locks and do btree or rbtree searches, so |
| 1731 | * release the path to avoid blocking other tasks for too |
| 1732 | * long. |
| 1733 | */ |
| 1734 | btrfs_release_path(path); |
| 1735 | |
Liu Bo | 5811375 | 2018-01-31 17:09:13 -0700 | [diff] [blame] | 1736 | ret = btrfs_cross_ref_exist(root, ino, |
| 1737 | found_key.offset - |
Boris Burkov | a84d5d4 | 2020-08-18 11:00:05 -0700 | [diff] [blame] | 1738 | extent_offset, disk_bytenr, false); |
Liu Bo | 5811375 | 2018-01-31 17:09:13 -0700 | [diff] [blame] | 1739 | if (ret) { |
| 1740 | /* |
| 1741 | * ret could be -EIO if the above fails to read |
| 1742 | * metadata. |
| 1743 | */ |
| 1744 | if (ret < 0) { |
| 1745 | if (cow_start != (u64)-1) |
| 1746 | cur_offset = cow_start; |
| 1747 | goto error; |
| 1748 | } |
| 1749 | |
Nikolay Borisov | 3e02484 | 2019-08-21 10:42:03 +0300 | [diff] [blame] | 1750 | WARN_ON_ONCE(freespace_inode); |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1751 | goto out_check; |
Liu Bo | 5811375 | 2018-01-31 17:09:13 -0700 | [diff] [blame] | 1752 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1753 | disk_bytenr += extent_offset; |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1754 | disk_bytenr += cur_offset - found_key.offset; |
| 1755 | num_bytes = min(end + 1, extent_end) - cur_offset; |
| 1756 | /* |
Nikolay Borisov | a6bd9cd | 2019-08-21 10:42:57 +0300 | [diff] [blame] | 1757 | * If there are pending snapshots for this root, we |
| 1758 | * fall into common COW way |
Wang Shilong | e9894fd | 2014-03-27 11:12:25 +0800 | [diff] [blame] | 1759 | */ |
Nikolay Borisov | 3e02484 | 2019-08-21 10:42:03 +0300 | [diff] [blame] | 1760 | if (!freespace_inode && atomic_read(&root->snapshot_force_cow)) |
Robbie Ko | 8ecebf4d | 2018-08-06 10:30:30 +0800 | [diff] [blame] | 1761 | goto out_check; |
Wang Shilong | e9894fd | 2014-03-27 11:12:25 +0800 | [diff] [blame] | 1762 | /* |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1763 | * force cow if csum exists in the range. |
| 1764 | * this ensure that csum for a given extent are |
| 1765 | * either valid or do not exist. |
| 1766 | */ |
Liu Bo | 5811375 | 2018-01-31 17:09:13 -0700 | [diff] [blame] | 1767 | ret = csum_exist_in_range(fs_info, disk_bytenr, |
| 1768 | num_bytes); |
| 1769 | if (ret) { |
Liu Bo | 5811375 | 2018-01-31 17:09:13 -0700 | [diff] [blame] | 1770 | /* |
| 1771 | * ret could be -EIO if the above fails to read |
| 1772 | * metadata. |
| 1773 | */ |
| 1774 | if (ret < 0) { |
| 1775 | if (cow_start != (u64)-1) |
| 1776 | cur_offset = cow_start; |
| 1777 | goto error; |
| 1778 | } |
Nikolay Borisov | 3e02484 | 2019-08-21 10:42:03 +0300 | [diff] [blame] | 1779 | WARN_ON_ONCE(freespace_inode); |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1780 | goto out_check; |
Robbie Ko | 91e1f56 | 2016-10-07 10:01:29 +0800 | [diff] [blame] | 1781 | } |
Filipe Manana | 2090303 | 2021-02-05 12:55:36 +0000 | [diff] [blame] | 1782 | /* If the extent's block group is RO, we must COW */ |
Robbie Ko | 8ecebf4d | 2018-08-06 10:30:30 +0800 | [diff] [blame] | 1783 | if (!btrfs_inc_nocow_writers(fs_info, disk_bytenr)) |
Filipe Manana | f78c436 | 2016-05-09 13:15:41 +0100 | [diff] [blame] | 1784 | goto out_check; |
Nikolay Borisov | 3e02484 | 2019-08-21 10:42:03 +0300 | [diff] [blame] | 1785 | nocow = true; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1786 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
Nikolay Borisov | e8e2100 | 2019-08-22 17:25:23 +0300 | [diff] [blame] | 1787 | extent_end = found_key.offset + ram_bytes; |
| 1788 | extent_end = ALIGN(extent_end, fs_info->sectorsize); |
Nikolay Borisov | 922f051 | 2019-08-05 17:47:06 +0300 | [diff] [blame] | 1789 | /* Skip extents outside of our requested range */ |
| 1790 | if (extent_end <= start) { |
| 1791 | path->slots[0]++; |
| 1792 | goto next_slot; |
| 1793 | } |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1794 | } else { |
Nikolay Borisov | e8e2100 | 2019-08-22 17:25:23 +0300 | [diff] [blame] | 1795 | /* If this triggers then we have a memory corruption */ |
Arnd Bergmann | 290342f | 2019-03-25 14:02:25 +0100 | [diff] [blame] | 1796 | BUG(); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1797 | } |
| 1798 | out_check: |
Nikolay Borisov | a6bd9cd | 2019-08-21 10:42:57 +0300 | [diff] [blame] | 1799 | /* |
| 1800 | * If nocow is false then record the beginning of the range |
| 1801 | * that needs to be COWed |
| 1802 | */ |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1803 | if (!nocow) { |
| 1804 | if (cow_start == (u64)-1) |
| 1805 | cow_start = cur_offset; |
| 1806 | cur_offset = extent_end; |
| 1807 | if (cur_offset > end) |
| 1808 | break; |
Filipe Manana | c65ca98 | 2020-11-18 11:00:17 +0000 | [diff] [blame] | 1809 | if (!path->nodes[0]) |
| 1810 | continue; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1811 | path->slots[0]++; |
| 1812 | goto next_slot; |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1813 | } |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1814 | |
Nikolay Borisov | a6bd9cd | 2019-08-21 10:42:57 +0300 | [diff] [blame] | 1815 | /* |
| 1816 | * COW range from cow_start to found_key.offset - 1. As the key |
| 1817 | * will contain the beginning of the first extent that can be |
| 1818 | * NOCOW, following one which needs to be COW'ed |
| 1819 | */ |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1820 | if (cow_start != (u64)-1) { |
Nikolay Borisov | 968322c | 2020-06-03 08:55:21 +0300 | [diff] [blame] | 1821 | ret = fallback_to_cow(inode, locked_page, |
Nikolay Borisov | 8ba96f3 | 2020-06-03 08:55:20 +0300 | [diff] [blame] | 1822 | cow_start, found_key.offset - 1, |
Filipe Manana | 467dc47 | 2020-05-27 11:16:07 +0100 | [diff] [blame] | 1823 | page_started, nr_written); |
Josef Bacik | 230ed39 | 2020-07-06 09:14:12 -0400 | [diff] [blame] | 1824 | if (ret) |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1825 | goto error; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1826 | cow_start = (u64)-1; |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1827 | } |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1828 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1829 | if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) { |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 1830 | u64 orig_start = found_key.offset - extent_offset; |
Nikolay Borisov | 3e02484 | 2019-08-21 10:42:03 +0300 | [diff] [blame] | 1831 | struct extent_map *em; |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 1832 | |
Nikolay Borisov | 968322c | 2020-06-03 08:55:21 +0300 | [diff] [blame] | 1833 | em = create_io_em(inode, cur_offset, num_bytes, |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 1834 | orig_start, |
| 1835 | disk_bytenr, /* block_start */ |
| 1836 | num_bytes, /* block_len */ |
| 1837 | disk_num_bytes, /* orig_block_len */ |
| 1838 | ram_bytes, BTRFS_COMPRESS_NONE, |
| 1839 | BTRFS_ORDERED_PREALLOC); |
| 1840 | if (IS_ERR(em)) { |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 1841 | ret = PTR_ERR(em); |
| 1842 | goto error; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1843 | } |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 1844 | free_extent_map(em); |
Nikolay Borisov | 968322c | 2020-06-03 08:55:21 +0300 | [diff] [blame] | 1845 | ret = btrfs_add_ordered_extent(inode, cur_offset, |
Nikolay Borisov | bb55f62 | 2019-08-05 17:47:05 +0300 | [diff] [blame] | 1846 | disk_bytenr, num_bytes, |
| 1847 | num_bytes, |
| 1848 | BTRFS_ORDERED_PREALLOC); |
Nikolay Borisov | 762bf09 | 2019-08-22 17:24:20 +0300 | [diff] [blame] | 1849 | if (ret) { |
Nikolay Borisov | 968322c | 2020-06-03 08:55:21 +0300 | [diff] [blame] | 1850 | btrfs_drop_extent_cache(inode, cur_offset, |
Nikolay Borisov | 762bf09 | 2019-08-22 17:24:20 +0300 | [diff] [blame] | 1851 | cur_offset + num_bytes - 1, |
| 1852 | 0); |
| 1853 | goto error; |
| 1854 | } |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1855 | } else { |
Nikolay Borisov | 968322c | 2020-06-03 08:55:21 +0300 | [diff] [blame] | 1856 | ret = btrfs_add_ordered_extent(inode, cur_offset, |
Nikolay Borisov | bb55f62 | 2019-08-05 17:47:05 +0300 | [diff] [blame] | 1857 | disk_bytenr, num_bytes, |
| 1858 | num_bytes, |
| 1859 | BTRFS_ORDERED_NOCOW); |
Nikolay Borisov | 762bf09 | 2019-08-22 17:24:20 +0300 | [diff] [blame] | 1860 | if (ret) |
| 1861 | goto error; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1862 | } |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1863 | |
Filipe Manana | f78c436 | 2016-05-09 13:15:41 +0100 | [diff] [blame] | 1864 | if (nocow) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1865 | btrfs_dec_nocow_writers(fs_info, disk_bytenr); |
Nikolay Borisov | 762bf09 | 2019-08-22 17:24:20 +0300 | [diff] [blame] | 1866 | nocow = false; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1867 | |
Yan, Zheng | efa5646 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 1868 | if (root->root_key.objectid == |
Qu Wenruo | 4dbd80f | 2017-03-08 10:25:51 +0800 | [diff] [blame] | 1869 | BTRFS_DATA_RELOC_TREE_OBJECTID) |
| 1870 | /* |
| 1871 | * Error handled later, as we must prevent |
| 1872 | * extent_clear_unlock_delalloc() in error handler |
| 1873 | * from freeing metadata of created ordered extent. |
| 1874 | */ |
Nikolay Borisov | 968322c | 2020-06-03 08:55:21 +0300 | [diff] [blame] | 1875 | ret = btrfs_reloc_clone_csums(inode, cur_offset, |
Yan, Zheng | efa5646 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 1876 | num_bytes); |
Yan, Zheng | efa5646 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 1877 | |
Nikolay Borisov | 968322c | 2020-06-03 08:55:21 +0300 | [diff] [blame] | 1878 | extent_clear_unlock_delalloc(inode, cur_offset, |
Nikolay Borisov | 74e9194 | 2019-07-17 16:18:16 +0300 | [diff] [blame] | 1879 | cur_offset + num_bytes - 1, |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1880 | locked_page, EXTENT_LOCKED | |
Wang Xiaoguang | 1851309 | 2016-07-25 15:51:40 +0800 | [diff] [blame] | 1881 | EXTENT_DELALLOC | |
| 1882 | EXTENT_CLEAR_DATA_RESV, |
Qu Wenruo | f57ad93 | 2021-04-07 19:22:13 +0800 | [diff] [blame] | 1883 | PAGE_UNLOCK | PAGE_SET_ORDERED); |
Wang Xiaoguang | 1851309 | 2016-07-25 15:51:40 +0800 | [diff] [blame] | 1884 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1885 | cur_offset = extent_end; |
Qu Wenruo | 4dbd80f | 2017-03-08 10:25:51 +0800 | [diff] [blame] | 1886 | |
| 1887 | /* |
| 1888 | * btrfs_reloc_clone_csums() error, now we're OK to call error |
| 1889 | * handler, as metadata for created ordered extent will only |
| 1890 | * be freed by btrfs_finish_ordered_io(). |
| 1891 | */ |
| 1892 | if (ret) |
| 1893 | goto error; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1894 | if (cur_offset > end) |
| 1895 | break; |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1896 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1897 | btrfs_release_path(path); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1898 | |
Robbie Ko | 506481b | 2018-10-30 18:04:04 +0800 | [diff] [blame] | 1899 | if (cur_offset <= end && cow_start == (u64)-1) |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1900 | cow_start = cur_offset; |
Josef Bacik | 17ca04a | 2012-05-31 15:58:55 -0400 | [diff] [blame] | 1901 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1902 | if (cow_start != (u64)-1) { |
Robbie Ko | 506481b | 2018-10-30 18:04:04 +0800 | [diff] [blame] | 1903 | cur_offset = end; |
Nikolay Borisov | 968322c | 2020-06-03 08:55:21 +0300 | [diff] [blame] | 1904 | ret = fallback_to_cow(inode, locked_page, cow_start, end, |
| 1905 | page_started, nr_written); |
Josef Bacik | d788a34 | 2013-10-25 16:55:08 -0400 | [diff] [blame] | 1906 | if (ret) |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1907 | goto error; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1908 | } |
| 1909 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1910 | error: |
Nikolay Borisov | 762bf09 | 2019-08-22 17:24:20 +0300 | [diff] [blame] | 1911 | if (nocow) |
| 1912 | btrfs_dec_nocow_writers(fs_info, disk_bytenr); |
| 1913 | |
Josef Bacik | 17ca04a | 2012-05-31 15:58:55 -0400 | [diff] [blame] | 1914 | if (ret && cur_offset < end) |
Nikolay Borisov | 968322c | 2020-06-03 08:55:21 +0300 | [diff] [blame] | 1915 | extent_clear_unlock_delalloc(inode, cur_offset, end, |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1916 | locked_page, EXTENT_LOCKED | |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 1917 | EXTENT_DELALLOC | EXTENT_DEFRAG | |
| 1918 | EXTENT_DO_ACCOUNTING, PAGE_UNLOCK | |
Qu Wenruo | 6869b0a | 2021-01-26 16:33:45 +0800 | [diff] [blame] | 1919 | PAGE_START_WRITEBACK | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1920 | PAGE_END_WRITEBACK); |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1921 | btrfs_free_path(path); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1922 | return ret; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1923 | } |
| 1924 | |
Goldwyn Rodrigues | 6e65ae7 | 2021-03-04 09:06:25 -0600 | [diff] [blame] | 1925 | static bool should_nocow(struct btrfs_inode *inode, u64 start, u64 end) |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 1926 | { |
Goldwyn Rodrigues | 6e65ae7 | 2021-03-04 09:06:25 -0600 | [diff] [blame] | 1927 | if (inode->flags & (BTRFS_INODE_NODATACOW | BTRFS_INODE_PREALLOC)) { |
| 1928 | if (inode->defrag_bytes && |
| 1929 | test_range_bit(&inode->io_tree, start, end, EXTENT_DEFRAG, |
| 1930 | 0, NULL)) |
| 1931 | return false; |
| 1932 | return true; |
| 1933 | } |
| 1934 | return false; |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 1935 | } |
| 1936 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1937 | /* |
Nikolay Borisov | 5eaad97 | 2018-11-01 14:09:46 +0200 | [diff] [blame] | 1938 | * Function to process delayed allocation (create CoW) for ranges which are |
| 1939 | * being touched for the first time. |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1940 | */ |
Nikolay Borisov | 98456b9 | 2020-06-03 08:55:29 +0300 | [diff] [blame] | 1941 | 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] | 1942 | u64 start, u64 end, int *page_started, unsigned long *nr_written, |
| 1943 | struct writeback_control *wbc) |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1944 | { |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1945 | int ret; |
Naohiro Aota | 42c0110 | 2021-02-04 19:22:07 +0900 | [diff] [blame] | 1946 | const bool zoned = btrfs_is_zoned(inode->root->fs_info); |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1947 | |
Goldwyn Rodrigues | 6e65ae7 | 2021-03-04 09:06:25 -0600 | [diff] [blame] | 1948 | if (should_nocow(inode, start, end)) { |
Naohiro Aota | 42c0110 | 2021-02-04 19:22:07 +0900 | [diff] [blame] | 1949 | ASSERT(!zoned); |
Nikolay Borisov | 98456b9 | 2020-06-03 08:55:29 +0300 | [diff] [blame] | 1950 | ret = run_delalloc_nocow(inode, locked_page, start, end, |
Goldwyn Rodrigues | 6e65ae7 | 2021-03-04 09:06:25 -0600 | [diff] [blame] | 1951 | page_started, nr_written); |
Nikolay Borisov | 98456b9 | 2020-06-03 08:55:29 +0300 | [diff] [blame] | 1952 | } else if (!inode_can_compress(inode) || |
| 1953 | !inode_need_compress(inode, start, end)) { |
Naohiro Aota | 42c0110 | 2021-02-04 19:22:07 +0900 | [diff] [blame] | 1954 | if (zoned) |
| 1955 | ret = run_delalloc_zoned(inode, locked_page, start, end, |
| 1956 | page_started, nr_written); |
| 1957 | else |
| 1958 | ret = cow_file_range(inode, locked_page, start, end, |
| 1959 | page_started, nr_written, 1); |
Josef Bacik | 7ddf5a4 | 2012-06-08 15:26:47 -0400 | [diff] [blame] | 1960 | } else { |
Nikolay Borisov | 98456b9 | 2020-06-03 08:55:29 +0300 | [diff] [blame] | 1961 | set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, &inode->runtime_flags); |
| 1962 | ret = cow_file_range_async(inode, wbc, locked_page, start, end, |
David Sterba | fac07d2 | 2019-10-29 18:28:57 +0100 | [diff] [blame] | 1963 | page_started, nr_written); |
Josef Bacik | 7ddf5a4 | 2012-06-08 15:26:47 -0400 | [diff] [blame] | 1964 | } |
Qu Wenruo | 52427260 | 2017-03-08 10:25:52 +0800 | [diff] [blame] | 1965 | if (ret) |
Nikolay Borisov | 98456b9 | 2020-06-03 08:55:29 +0300 | [diff] [blame] | 1966 | btrfs_cleanup_ordered_extents(inode, locked_page, start, |
Nikolay Borisov | d1051d6 | 2018-11-21 17:10:52 +0200 | [diff] [blame] | 1967 | end - start + 1); |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 1968 | return ret; |
| 1969 | } |
| 1970 | |
Nikolay Borisov | abbb55f | 2018-11-01 14:09:53 +0200 | [diff] [blame] | 1971 | void btrfs_split_delalloc_extent(struct inode *inode, |
| 1972 | struct extent_state *orig, u64 split) |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1973 | { |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1974 | u64 size; |
| 1975 | |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1976 | /* not delalloc, ignore it */ |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1977 | if (!(orig->state & EXTENT_DELALLOC)) |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 1978 | return; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1979 | |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1980 | size = orig->end - orig->start + 1; |
| 1981 | if (size > BTRFS_MAX_EXTENT_SIZE) { |
David Sterba | 823bb20 | 2017-01-04 11:09:51 +0100 | [diff] [blame] | 1982 | u32 num_extents; |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1983 | u64 new_size; |
| 1984 | |
| 1985 | /* |
Nikolay Borisov | 5c84819 | 2018-11-01 14:09:52 +0200 | [diff] [blame] | 1986 | * See the explanation in btrfs_merge_delalloc_extent, the same |
Josef Bacik | ba11721 | 2015-03-13 15:01:24 -0400 | [diff] [blame] | 1987 | * applies here, just in reverse. |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1988 | */ |
| 1989 | new_size = orig->end - split + 1; |
David Sterba | 823bb20 | 2017-01-04 11:09:51 +0100 | [diff] [blame] | 1990 | num_extents = count_max_extents(new_size); |
Josef Bacik | ba11721 | 2015-03-13 15:01:24 -0400 | [diff] [blame] | 1991 | new_size = split - orig->start; |
David Sterba | 823bb20 | 2017-01-04 11:09:51 +0100 | [diff] [blame] | 1992 | num_extents += count_max_extents(new_size); |
| 1993 | if (count_max_extents(size) >= num_extents) |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1994 | return; |
| 1995 | } |
| 1996 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1997 | spin_lock(&BTRFS_I(inode)->lock); |
Josef Bacik | 8b62f87 | 2017-10-19 14:15:55 -0400 | [diff] [blame] | 1998 | btrfs_mod_outstanding_extents(BTRFS_I(inode), 1); |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1999 | spin_unlock(&BTRFS_I(inode)->lock); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 2000 | } |
| 2001 | |
| 2002 | /* |
Nikolay Borisov | 5c84819 | 2018-11-01 14:09:52 +0200 | [diff] [blame] | 2003 | * Handle merged delayed allocation extents so we can keep track of new extents |
| 2004 | * that are just merged onto old extents, such as when we are doing sequential |
| 2005 | * 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] | 2006 | */ |
Nikolay Borisov | 5c84819 | 2018-11-01 14:09:52 +0200 | [diff] [blame] | 2007 | void btrfs_merge_delalloc_extent(struct inode *inode, struct extent_state *new, |
| 2008 | struct extent_state *other) |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 2009 | { |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 2010 | u64 new_size, old_size; |
David Sterba | 823bb20 | 2017-01-04 11:09:51 +0100 | [diff] [blame] | 2011 | u32 num_extents; |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 2012 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 2013 | /* not delalloc, ignore it */ |
| 2014 | if (!(other->state & EXTENT_DELALLOC)) |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 2015 | return; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 2016 | |
Josef Bacik | 8461a3d | 2015-03-13 15:12:08 -0400 | [diff] [blame] | 2017 | if (new->start > other->start) |
| 2018 | new_size = new->end - other->start + 1; |
| 2019 | else |
| 2020 | new_size = other->end - new->start + 1; |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 2021 | |
| 2022 | /* we're not bigger than the max, unreserve the space and go */ |
| 2023 | if (new_size <= BTRFS_MAX_EXTENT_SIZE) { |
| 2024 | spin_lock(&BTRFS_I(inode)->lock); |
Josef Bacik | 8b62f87 | 2017-10-19 14:15:55 -0400 | [diff] [blame] | 2025 | btrfs_mod_outstanding_extents(BTRFS_I(inode), -1); |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 2026 | spin_unlock(&BTRFS_I(inode)->lock); |
| 2027 | return; |
| 2028 | } |
| 2029 | |
| 2030 | /* |
Josef Bacik | ba11721 | 2015-03-13 15:01:24 -0400 | [diff] [blame] | 2031 | * We have to add up either side to figure out how many extents were |
| 2032 | * accounted for before we merged into one big extent. If the number of |
| 2033 | * extents we accounted for is <= the amount we need for the new range |
| 2034 | * then we can return, otherwise drop. Think of it like this |
| 2035 | * |
| 2036 | * [ 4k][MAX_SIZE] |
| 2037 | * |
| 2038 | * So we've grown the extent by a MAX_SIZE extent, this would mean we |
| 2039 | * need 2 outstanding extents, on one side we have 1 and the other side |
| 2040 | * we have 1 so they are == and we can return. But in this case |
| 2041 | * |
| 2042 | * [MAX_SIZE+4k][MAX_SIZE+4k] |
| 2043 | * |
| 2044 | * Each range on their own accounts for 2 extents, but merged together |
| 2045 | * they are only 3 extents worth of accounting, so we need to drop in |
| 2046 | * this case. |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 2047 | */ |
Josef Bacik | ba11721 | 2015-03-13 15:01:24 -0400 | [diff] [blame] | 2048 | old_size = other->end - other->start + 1; |
David Sterba | 823bb20 | 2017-01-04 11:09:51 +0100 | [diff] [blame] | 2049 | num_extents = count_max_extents(old_size); |
Josef Bacik | ba11721 | 2015-03-13 15:01:24 -0400 | [diff] [blame] | 2050 | old_size = new->end - new->start + 1; |
David Sterba | 823bb20 | 2017-01-04 11:09:51 +0100 | [diff] [blame] | 2051 | num_extents += count_max_extents(old_size); |
| 2052 | if (count_max_extents(new_size) >= num_extents) |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 2053 | return; |
| 2054 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 2055 | spin_lock(&BTRFS_I(inode)->lock); |
Josef Bacik | 8b62f87 | 2017-10-19 14:15:55 -0400 | [diff] [blame] | 2056 | btrfs_mod_outstanding_extents(BTRFS_I(inode), -1); |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 2057 | spin_unlock(&BTRFS_I(inode)->lock); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 2058 | } |
| 2059 | |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 2060 | static void btrfs_add_delalloc_inodes(struct btrfs_root *root, |
| 2061 | struct inode *inode) |
| 2062 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2063 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
| 2064 | |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 2065 | spin_lock(&root->delalloc_lock); |
| 2066 | if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) { |
| 2067 | list_add_tail(&BTRFS_I(inode)->delalloc_inodes, |
| 2068 | &root->delalloc_inodes); |
| 2069 | set_bit(BTRFS_INODE_IN_DELALLOC_LIST, |
| 2070 | &BTRFS_I(inode)->runtime_flags); |
| 2071 | root->nr_delalloc_inodes++; |
| 2072 | if (root->nr_delalloc_inodes == 1) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2073 | spin_lock(&fs_info->delalloc_root_lock); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 2074 | BUG_ON(!list_empty(&root->delalloc_root)); |
| 2075 | list_add_tail(&root->delalloc_root, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2076 | &fs_info->delalloc_roots); |
| 2077 | spin_unlock(&fs_info->delalloc_root_lock); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 2078 | } |
| 2079 | } |
| 2080 | spin_unlock(&root->delalloc_lock); |
| 2081 | } |
| 2082 | |
Nikolay Borisov | 2b87733 | 2018-04-27 12:21:51 +0300 | [diff] [blame] | 2083 | |
| 2084 | void __btrfs_del_delalloc_inode(struct btrfs_root *root, |
| 2085 | struct btrfs_inode *inode) |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 2086 | { |
David Sterba | 3ffbd68 | 2018-06-29 10:56:42 +0200 | [diff] [blame] | 2087 | struct btrfs_fs_info *fs_info = root->fs_info; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2088 | |
Nikolay Borisov | 9e3e97f | 2017-02-20 13:51:07 +0200 | [diff] [blame] | 2089 | if (!list_empty(&inode->delalloc_inodes)) { |
| 2090 | list_del_init(&inode->delalloc_inodes); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 2091 | clear_bit(BTRFS_INODE_IN_DELALLOC_LIST, |
Nikolay Borisov | 9e3e97f | 2017-02-20 13:51:07 +0200 | [diff] [blame] | 2092 | &inode->runtime_flags); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 2093 | root->nr_delalloc_inodes--; |
| 2094 | if (!root->nr_delalloc_inodes) { |
Nikolay Borisov | 7c8a0d3 | 2018-04-27 12:21:52 +0300 | [diff] [blame] | 2095 | ASSERT(list_empty(&root->delalloc_inodes)); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2096 | spin_lock(&fs_info->delalloc_root_lock); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 2097 | BUG_ON(list_empty(&root->delalloc_root)); |
| 2098 | list_del_init(&root->delalloc_root); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2099 | spin_unlock(&fs_info->delalloc_root_lock); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 2100 | } |
| 2101 | } |
Nikolay Borisov | 2b87733 | 2018-04-27 12:21:51 +0300 | [diff] [blame] | 2102 | } |
| 2103 | |
| 2104 | static void btrfs_del_delalloc_inode(struct btrfs_root *root, |
| 2105 | struct btrfs_inode *inode) |
| 2106 | { |
| 2107 | spin_lock(&root->delalloc_lock); |
| 2108 | __btrfs_del_delalloc_inode(root, inode); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 2109 | spin_unlock(&root->delalloc_lock); |
| 2110 | } |
| 2111 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2112 | /* |
Nikolay Borisov | e06a1fc | 2018-11-01 14:09:50 +0200 | [diff] [blame] | 2113 | * Properly track delayed allocation bytes in the inode and to maintain the |
| 2114 | * list of inodes that have pending delalloc work to be done. |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2115 | */ |
Nikolay Borisov | e06a1fc | 2018-11-01 14:09:50 +0200 | [diff] [blame] | 2116 | void btrfs_set_delalloc_extent(struct inode *inode, struct extent_state *state, |
| 2117 | unsigned *bits) |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 2118 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2119 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
| 2120 | |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 2121 | if ((*bits & EXTENT_DEFRAG) && !(*bits & EXTENT_DELALLOC)) |
| 2122 | WARN_ON(1); |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 2123 | /* |
| 2124 | * set_bit and clear bit hooks normally require _irqsave/restore |
Sergei Trofimovich | 27160b6 | 2011-05-20 20:20:32 +0000 | [diff] [blame] | 2125 | * but in this case, we are only testing for the DELALLOC |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 2126 | * bit, which is only set or cleared with irqs on |
| 2127 | */ |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 2128 | if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) { |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 2129 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 2130 | u64 len = state->end + 1 - state->start; |
Josef Bacik | 8b62f87 | 2017-10-19 14:15:55 -0400 | [diff] [blame] | 2131 | u32 num_extents = count_max_extents(len); |
Nikolay Borisov | 70ddc55 | 2017-02-20 13:50:35 +0200 | [diff] [blame] | 2132 | bool do_list = !btrfs_is_free_space_inode(BTRFS_I(inode)); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 2133 | |
Josef Bacik | 8b62f87 | 2017-10-19 14:15:55 -0400 | [diff] [blame] | 2134 | spin_lock(&BTRFS_I(inode)->lock); |
| 2135 | btrfs_mod_outstanding_extents(BTRFS_I(inode), num_extents); |
| 2136 | spin_unlock(&BTRFS_I(inode)->lock); |
Josef Bacik | 287a0ab | 2010-03-19 18:07:23 +0000 | [diff] [blame] | 2137 | |
Josef Bacik | 6a3891c | 2015-03-16 17:38:52 -0400 | [diff] [blame] | 2138 | /* For sanity tests */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2139 | if (btrfs_is_testing(fs_info)) |
Josef Bacik | 6a3891c | 2015-03-16 17:38:52 -0400 | [diff] [blame] | 2140 | return; |
| 2141 | |
Nikolay Borisov | 104b4e5 | 2017-06-20 21:01:20 +0300 | [diff] [blame] | 2142 | percpu_counter_add_batch(&fs_info->delalloc_bytes, len, |
| 2143 | fs_info->delalloc_batch); |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 2144 | spin_lock(&BTRFS_I(inode)->lock); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 2145 | BTRFS_I(inode)->delalloc_bytes += len; |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 2146 | if (*bits & EXTENT_DEFRAG) |
| 2147 | BTRFS_I(inode)->defrag_bytes += len; |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 2148 | if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST, |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 2149 | &BTRFS_I(inode)->runtime_flags)) |
| 2150 | btrfs_add_delalloc_inodes(root, inode); |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 2151 | spin_unlock(&BTRFS_I(inode)->lock); |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 2152 | } |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 2153 | |
| 2154 | if (!(state->state & EXTENT_DELALLOC_NEW) && |
| 2155 | (*bits & EXTENT_DELALLOC_NEW)) { |
| 2156 | spin_lock(&BTRFS_I(inode)->lock); |
| 2157 | BTRFS_I(inode)->new_delalloc_bytes += state->end + 1 - |
| 2158 | state->start; |
| 2159 | spin_unlock(&BTRFS_I(inode)->lock); |
| 2160 | } |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 2161 | } |
| 2162 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2163 | /* |
Nikolay Borisov | a36bb5f | 2018-11-01 14:09:51 +0200 | [diff] [blame] | 2164 | * Once a range is no longer delalloc this function ensures that proper |
| 2165 | * accounting happens. |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2166 | */ |
Nikolay Borisov | a36bb5f | 2018-11-01 14:09:51 +0200 | [diff] [blame] | 2167 | void btrfs_clear_delalloc_extent(struct inode *vfs_inode, |
| 2168 | struct extent_state *state, unsigned *bits) |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 2169 | { |
Nikolay Borisov | a36bb5f | 2018-11-01 14:09:51 +0200 | [diff] [blame] | 2170 | struct btrfs_inode *inode = BTRFS_I(vfs_inode); |
| 2171 | struct btrfs_fs_info *fs_info = btrfs_sb(vfs_inode->i_sb); |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 2172 | u64 len = state->end + 1 - state->start; |
David Sterba | 823bb20 | 2017-01-04 11:09:51 +0100 | [diff] [blame] | 2173 | u32 num_extents = count_max_extents(len); |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 2174 | |
Filipe Manana | 4a4b964 | 2017-07-27 19:52:55 +0100 | [diff] [blame] | 2175 | if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG)) { |
| 2176 | spin_lock(&inode->lock); |
Nikolay Borisov | 6fc0ef6 | 2017-02-20 13:51:03 +0200 | [diff] [blame] | 2177 | inode->defrag_bytes -= len; |
Filipe Manana | 4a4b964 | 2017-07-27 19:52:55 +0100 | [diff] [blame] | 2178 | spin_unlock(&inode->lock); |
| 2179 | } |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 2180 | |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 2181 | /* |
| 2182 | * set_bit and clear bit hooks normally require _irqsave/restore |
Sergei Trofimovich | 27160b6 | 2011-05-20 20:20:32 +0000 | [diff] [blame] | 2183 | * but in this case, we are only testing for the DELALLOC |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 2184 | * bit, which is only set or cleared with irqs on |
| 2185 | */ |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 2186 | if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) { |
Nikolay Borisov | 6fc0ef6 | 2017-02-20 13:51:03 +0200 | [diff] [blame] | 2187 | struct btrfs_root *root = inode->root; |
Liu Bo | 83eea1f | 2012-07-10 05:28:39 -0600 | [diff] [blame] | 2188 | bool do_list = !btrfs_is_free_space_inode(inode); |
Chris Mason | bcbfce8 | 2008-04-22 13:26:47 -0400 | [diff] [blame] | 2189 | |
Josef Bacik | 8b62f87 | 2017-10-19 14:15:55 -0400 | [diff] [blame] | 2190 | spin_lock(&inode->lock); |
| 2191 | btrfs_mod_outstanding_extents(inode, -num_extents); |
| 2192 | spin_unlock(&inode->lock); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 2193 | |
Josef Bacik | b6d08f0 | 2013-09-27 14:57:43 -0400 | [diff] [blame] | 2194 | /* |
| 2195 | * We don't reserve metadata space for space cache inodes so we |
Andrea Gelmini | 52042d8 | 2018-11-28 12:05:13 +0100 | [diff] [blame] | 2196 | * don't need to call delalloc_release_metadata if there is an |
Josef Bacik | b6d08f0 | 2013-09-27 14:57:43 -0400 | [diff] [blame] | 2197 | * error. |
| 2198 | */ |
Filipe Manana | a315e68 | 2017-03-06 23:04:20 +0000 | [diff] [blame] | 2199 | if (*bits & EXTENT_CLEAR_META_RESV && |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2200 | root != fs_info->tree_root) |
Qu Wenruo | 43b1859 | 2017-12-12 15:34:32 +0800 | [diff] [blame] | 2201 | btrfs_delalloc_release_metadata(inode, len, false); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 2202 | |
Josef Bacik | 6a3891c | 2015-03-16 17:38:52 -0400 | [diff] [blame] | 2203 | /* For sanity tests. */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2204 | if (btrfs_is_testing(fs_info)) |
Josef Bacik | 6a3891c | 2015-03-16 17:38:52 -0400 | [diff] [blame] | 2205 | return; |
| 2206 | |
Filipe Manana | a315e68 | 2017-03-06 23:04:20 +0000 | [diff] [blame] | 2207 | if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID && |
| 2208 | do_list && !(state->state & EXTENT_NORESERVE) && |
| 2209 | (*bits & EXTENT_CLEAR_DATA_RESV)) |
Nikolay Borisov | 9db5d51 | 2020-06-03 08:55:38 +0300 | [diff] [blame] | 2210 | btrfs_free_reserved_data_space_noquota(fs_info, len); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 2211 | |
Nikolay Borisov | 104b4e5 | 2017-06-20 21:01:20 +0300 | [diff] [blame] | 2212 | percpu_counter_add_batch(&fs_info->delalloc_bytes, -len, |
| 2213 | fs_info->delalloc_batch); |
Nikolay Borisov | 6fc0ef6 | 2017-02-20 13:51:03 +0200 | [diff] [blame] | 2214 | spin_lock(&inode->lock); |
| 2215 | inode->delalloc_bytes -= len; |
| 2216 | if (do_list && inode->delalloc_bytes == 0 && |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 2217 | test_bit(BTRFS_INODE_IN_DELALLOC_LIST, |
Nikolay Borisov | 9e3e97f | 2017-02-20 13:51:07 +0200 | [diff] [blame] | 2218 | &inode->runtime_flags)) |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 2219 | btrfs_del_delalloc_inode(root, inode); |
Nikolay Borisov | 6fc0ef6 | 2017-02-20 13:51:03 +0200 | [diff] [blame] | 2220 | spin_unlock(&inode->lock); |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 2221 | } |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 2222 | |
| 2223 | if ((state->state & EXTENT_DELALLOC_NEW) && |
| 2224 | (*bits & EXTENT_DELALLOC_NEW)) { |
| 2225 | spin_lock(&inode->lock); |
| 2226 | ASSERT(inode->new_delalloc_bytes >= len); |
| 2227 | inode->new_delalloc_bytes -= len; |
Filipe Manana | 2766ff6 | 2020-11-04 11:07:34 +0000 | [diff] [blame] | 2228 | if (*bits & EXTENT_ADD_INODE_BYTES) |
| 2229 | inode_add_bytes(&inode->vfs_inode, len); |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 2230 | spin_unlock(&inode->lock); |
| 2231 | } |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 2232 | } |
| 2233 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2234 | /* |
Nikolay Borisov | da12fe5 | 2018-11-27 20:57:58 +0200 | [diff] [blame] | 2235 | * btrfs_bio_fits_in_stripe - Checks whether the size of the given bio will fit |
| 2236 | * in a chunk's stripe. This function ensures that bios do not span a |
| 2237 | * stripe/chunk |
Liu Bo | 6f034ec | 2016-06-22 18:31:49 -0700 | [diff] [blame] | 2238 | * |
Nikolay Borisov | da12fe5 | 2018-11-27 20:57:58 +0200 | [diff] [blame] | 2239 | * @page - The page we are about to add to the bio |
| 2240 | * @size - size we want to add to the bio |
| 2241 | * @bio - bio we want to ensure is smaller than a stripe |
| 2242 | * @bio_flags - flags of the bio |
| 2243 | * |
| 2244 | * return 1 if page cannot be added to the bio |
| 2245 | * return 0 if page can be added to the bio |
Liu Bo | 6f034ec | 2016-06-22 18:31:49 -0700 | [diff] [blame] | 2246 | * return error otherwise |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2247 | */ |
Nikolay Borisov | da12fe5 | 2018-11-27 20:57:58 +0200 | [diff] [blame] | 2248 | int btrfs_bio_fits_in_stripe(struct page *page, size_t size, struct bio *bio, |
| 2249 | unsigned long bio_flags) |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 2250 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2251 | struct inode *inode = page->mapping->host; |
| 2252 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
David Sterba | 1201b58 | 2020-11-26 15:41:27 +0100 | [diff] [blame] | 2253 | u64 logical = bio->bi_iter.bi_sector << 9; |
Qu Wenruo | 1a0b5c4 | 2021-04-13 18:00:47 +0800 | [diff] [blame] | 2254 | u32 bio_len = bio->bi_iter.bi_size; |
Michal Rostecki | 4203431 | 2021-01-27 14:57:27 +0100 | [diff] [blame] | 2255 | struct extent_map *em; |
Michal Rostecki | 4203431 | 2021-01-27 14:57:27 +0100 | [diff] [blame] | 2256 | int ret = 0; |
Nikolay Borisov | 89b798a | 2019-06-03 12:05:05 +0300 | [diff] [blame] | 2257 | struct btrfs_io_geometry geom; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 2258 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 2259 | if (bio_flags & EXTENT_BIO_COMPRESSED) |
| 2260 | return 0; |
| 2261 | |
Qu Wenruo | 1a0b5c4 | 2021-04-13 18:00:47 +0800 | [diff] [blame] | 2262 | em = btrfs_get_chunk_map(fs_info, logical, fs_info->sectorsize); |
Michal Rostecki | 4203431 | 2021-01-27 14:57:27 +0100 | [diff] [blame] | 2263 | if (IS_ERR(em)) |
| 2264 | return PTR_ERR(em); |
Qu Wenruo | 43c0d1a | 2021-04-13 17:58:48 +0800 | [diff] [blame] | 2265 | ret = btrfs_get_io_geometry(fs_info, em, btrfs_op(bio), logical, &geom); |
Liu Bo | 6f034ec | 2016-06-22 18:31:49 -0700 | [diff] [blame] | 2266 | if (ret < 0) |
Michal Rostecki | 4203431 | 2021-01-27 14:57:27 +0100 | [diff] [blame] | 2267 | goto out; |
Nikolay Borisov | 89b798a | 2019-06-03 12:05:05 +0300 | [diff] [blame] | 2268 | |
Qu Wenruo | 1a0b5c4 | 2021-04-13 18:00:47 +0800 | [diff] [blame] | 2269 | if (geom.len < bio_len + size) |
Michal Rostecki | 4203431 | 2021-01-27 14:57:27 +0100 | [diff] [blame] | 2270 | ret = 1; |
| 2271 | out: |
| 2272 | free_extent_map(em); |
| 2273 | return ret; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 2274 | } |
| 2275 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2276 | /* |
| 2277 | * in order to insert checksums into the metadata in large chunks, |
| 2278 | * we wait until bio submission time. All the pages in the bio are |
| 2279 | * checksummed and sums are attached onto the ordered extent record. |
| 2280 | * |
| 2281 | * At IO completion time the cums attached on the ordered extent record |
| 2282 | * are inserted into the btree |
| 2283 | */ |
Qu Wenruo | 8896a08 | 2020-10-21 14:24:53 +0800 | [diff] [blame] | 2284 | 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] | 2285 | u64 dio_file_offset) |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 2286 | { |
Johannes Thumshirn | c965d64 | 2020-07-28 20:25:41 +0900 | [diff] [blame] | 2287 | return btrfs_csum_one_bio(BTRFS_I(inode), bio, 0, 0); |
Chris Mason | 4a69a41 | 2008-11-06 22:03:00 -0500 | [diff] [blame] | 2288 | } |
Chris Mason | e015640 | 2008-04-16 11:15:20 -0400 | [diff] [blame] | 2289 | |
Naohiro Aota | abb99cf | 2021-06-28 17:57:28 +0900 | [diff] [blame] | 2290 | /* |
| 2291 | * Split an extent_map at [start, start + len] |
| 2292 | * |
| 2293 | * This function is intended to be used only for extract_ordered_extent(). |
| 2294 | */ |
| 2295 | static int split_zoned_em(struct btrfs_inode *inode, u64 start, u64 len, |
| 2296 | u64 pre, u64 post) |
| 2297 | { |
| 2298 | struct extent_map_tree *em_tree = &inode->extent_tree; |
| 2299 | struct extent_map *em; |
| 2300 | struct extent_map *split_pre = NULL; |
| 2301 | struct extent_map *split_mid = NULL; |
| 2302 | struct extent_map *split_post = NULL; |
| 2303 | int ret = 0; |
| 2304 | int modified; |
| 2305 | unsigned long flags; |
| 2306 | |
| 2307 | /* Sanity check */ |
| 2308 | if (pre == 0 && post == 0) |
| 2309 | return 0; |
| 2310 | |
| 2311 | split_pre = alloc_extent_map(); |
| 2312 | if (pre) |
| 2313 | split_mid = alloc_extent_map(); |
| 2314 | if (post) |
| 2315 | split_post = alloc_extent_map(); |
| 2316 | if (!split_pre || (pre && !split_mid) || (post && !split_post)) { |
| 2317 | ret = -ENOMEM; |
| 2318 | goto out; |
| 2319 | } |
| 2320 | |
| 2321 | ASSERT(pre + post < len); |
| 2322 | |
| 2323 | lock_extent(&inode->io_tree, start, start + len - 1); |
| 2324 | write_lock(&em_tree->lock); |
| 2325 | em = lookup_extent_mapping(em_tree, start, len); |
| 2326 | if (!em) { |
| 2327 | ret = -EIO; |
| 2328 | goto out_unlock; |
| 2329 | } |
| 2330 | |
| 2331 | ASSERT(em->len == len); |
| 2332 | ASSERT(!test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)); |
| 2333 | ASSERT(em->block_start < EXTENT_MAP_LAST_BYTE); |
| 2334 | |
| 2335 | flags = em->flags; |
| 2336 | clear_bit(EXTENT_FLAG_PINNED, &em->flags); |
| 2337 | clear_bit(EXTENT_FLAG_LOGGING, &flags); |
| 2338 | modified = !list_empty(&em->list); |
| 2339 | |
| 2340 | /* First, replace the em with a new extent_map starting from * em->start */ |
| 2341 | split_pre->start = em->start; |
| 2342 | split_pre->len = (pre ? pre : em->len - post); |
| 2343 | split_pre->orig_start = split_pre->start; |
| 2344 | split_pre->block_start = em->block_start; |
| 2345 | split_pre->block_len = split_pre->len; |
| 2346 | split_pre->orig_block_len = split_pre->block_len; |
| 2347 | split_pre->ram_bytes = split_pre->len; |
| 2348 | split_pre->flags = flags; |
| 2349 | split_pre->compress_type = em->compress_type; |
| 2350 | split_pre->generation = em->generation; |
| 2351 | |
| 2352 | replace_extent_mapping(em_tree, em, split_pre, modified); |
| 2353 | |
| 2354 | /* |
| 2355 | * Now we only have an extent_map at: |
| 2356 | * [em->start, em->start + pre] if pre != 0 |
| 2357 | * [em->start, em->start + em->len - post] if pre == 0 |
| 2358 | */ |
| 2359 | |
| 2360 | if (pre) { |
| 2361 | /* Insert the middle extent_map */ |
| 2362 | split_mid->start = em->start + pre; |
| 2363 | split_mid->len = em->len - pre - post; |
| 2364 | split_mid->orig_start = split_mid->start; |
| 2365 | split_mid->block_start = em->block_start + pre; |
| 2366 | split_mid->block_len = split_mid->len; |
| 2367 | split_mid->orig_block_len = split_mid->block_len; |
| 2368 | split_mid->ram_bytes = split_mid->len; |
| 2369 | split_mid->flags = flags; |
| 2370 | split_mid->compress_type = em->compress_type; |
| 2371 | split_mid->generation = em->generation; |
| 2372 | add_extent_mapping(em_tree, split_mid, modified); |
| 2373 | } |
| 2374 | |
| 2375 | if (post) { |
| 2376 | split_post->start = em->start + em->len - post; |
| 2377 | split_post->len = post; |
| 2378 | split_post->orig_start = split_post->start; |
| 2379 | split_post->block_start = em->block_start + em->len - post; |
| 2380 | split_post->block_len = split_post->len; |
| 2381 | split_post->orig_block_len = split_post->block_len; |
| 2382 | split_post->ram_bytes = split_post->len; |
| 2383 | split_post->flags = flags; |
| 2384 | split_post->compress_type = em->compress_type; |
| 2385 | split_post->generation = em->generation; |
| 2386 | add_extent_mapping(em_tree, split_post, modified); |
| 2387 | } |
| 2388 | |
| 2389 | /* Once for us */ |
| 2390 | free_extent_map(em); |
| 2391 | /* Once for the tree */ |
| 2392 | free_extent_map(em); |
| 2393 | |
| 2394 | out_unlock: |
| 2395 | write_unlock(&em_tree->lock); |
| 2396 | unlock_extent(&inode->io_tree, start, start + len - 1); |
| 2397 | out: |
| 2398 | free_extent_map(split_pre); |
| 2399 | free_extent_map(split_mid); |
| 2400 | free_extent_map(split_post); |
| 2401 | |
| 2402 | return ret; |
| 2403 | } |
| 2404 | |
Naohiro Aota | d22002f | 2021-02-04 19:22:00 +0900 | [diff] [blame] | 2405 | static blk_status_t extract_ordered_extent(struct btrfs_inode *inode, |
| 2406 | struct bio *bio, loff_t file_offset) |
| 2407 | { |
| 2408 | struct btrfs_ordered_extent *ordered; |
Naohiro Aota | d22002f | 2021-02-04 19:22:00 +0900 | [diff] [blame] | 2409 | u64 start = (u64)bio->bi_iter.bi_sector << SECTOR_SHIFT; |
Naohiro Aota | abb99cf | 2021-06-28 17:57:28 +0900 | [diff] [blame] | 2410 | u64 file_len; |
Naohiro Aota | d22002f | 2021-02-04 19:22:00 +0900 | [diff] [blame] | 2411 | u64 len = bio->bi_iter.bi_size; |
| 2412 | u64 end = start + len; |
| 2413 | u64 ordered_end; |
| 2414 | u64 pre, post; |
| 2415 | int ret = 0; |
| 2416 | |
| 2417 | ordered = btrfs_lookup_ordered_extent(inode, file_offset); |
| 2418 | if (WARN_ON_ONCE(!ordered)) |
| 2419 | return BLK_STS_IOERR; |
| 2420 | |
| 2421 | /* No need to split */ |
| 2422 | if (ordered->disk_num_bytes == len) |
| 2423 | goto out; |
| 2424 | |
| 2425 | /* We cannot split once end_bio'd ordered extent */ |
| 2426 | if (WARN_ON_ONCE(ordered->bytes_left != ordered->disk_num_bytes)) { |
| 2427 | ret = -EINVAL; |
| 2428 | goto out; |
| 2429 | } |
| 2430 | |
| 2431 | /* We cannot split a compressed ordered extent */ |
| 2432 | if (WARN_ON_ONCE(ordered->disk_num_bytes != ordered->num_bytes)) { |
| 2433 | ret = -EINVAL; |
| 2434 | goto out; |
| 2435 | } |
| 2436 | |
| 2437 | ordered_end = ordered->disk_bytenr + ordered->disk_num_bytes; |
| 2438 | /* bio must be in one ordered extent */ |
| 2439 | if (WARN_ON_ONCE(start < ordered->disk_bytenr || end > ordered_end)) { |
| 2440 | ret = -EINVAL; |
| 2441 | goto out; |
| 2442 | } |
| 2443 | |
| 2444 | /* Checksum list should be empty */ |
| 2445 | if (WARN_ON_ONCE(!list_empty(&ordered->list))) { |
| 2446 | ret = -EINVAL; |
| 2447 | goto out; |
| 2448 | } |
| 2449 | |
Naohiro Aota | abb99cf | 2021-06-28 17:57:28 +0900 | [diff] [blame] | 2450 | file_len = ordered->num_bytes; |
Naohiro Aota | d22002f | 2021-02-04 19:22:00 +0900 | [diff] [blame] | 2451 | pre = start - ordered->disk_bytenr; |
| 2452 | post = ordered_end - end; |
| 2453 | |
| 2454 | ret = btrfs_split_ordered_extent(ordered, pre, post); |
| 2455 | if (ret) |
| 2456 | goto out; |
Naohiro Aota | abb99cf | 2021-06-28 17:57:28 +0900 | [diff] [blame] | 2457 | ret = split_zoned_em(inode, file_offset, file_len, pre, post); |
Naohiro Aota | d22002f | 2021-02-04 19:22:00 +0900 | [diff] [blame] | 2458 | |
| 2459 | out: |
Naohiro Aota | d22002f | 2021-02-04 19:22:00 +0900 | [diff] [blame] | 2460 | btrfs_put_ordered_extent(ordered); |
| 2461 | |
| 2462 | return errno_to_blk_status(ret); |
| 2463 | } |
| 2464 | |
Chris Mason | 4a69a41 | 2008-11-06 22:03:00 -0500 | [diff] [blame] | 2465 | /* |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 2466 | * 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] | 2467 | * on write, or reading the csums from the tree before a read. |
| 2468 | * |
| 2469 | * Rules about async/sync submit, |
| 2470 | * a) read: sync submit |
| 2471 | * |
| 2472 | * b) write without checksum: sync submit |
| 2473 | * |
| 2474 | * c) write with checksum: |
| 2475 | * c-1) if bio is issued by fsync: sync submit |
| 2476 | * (sync_writers != 0) |
| 2477 | * |
| 2478 | * c-2) if root is reloc root: sync submit |
| 2479 | * (only in case of buffered IO) |
| 2480 | * |
| 2481 | * c-3) otherwise: async submit |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2482 | */ |
Nikolay Borisov | 908930f | 2020-09-18 16:34:37 +0300 | [diff] [blame] | 2483 | blk_status_t btrfs_submit_data_bio(struct inode *inode, struct bio *bio, |
| 2484 | int mirror_num, unsigned long bio_flags) |
Nikolay Borisov | 50489a5 | 2019-04-10 19:46:04 +0300 | [diff] [blame] | 2485 | |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 2486 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2487 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 2488 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chandan Rajendra | 0d51e28 | 2015-07-27 15:26:43 +0530 | [diff] [blame] | 2489 | enum btrfs_wq_endio_type metadata = BTRFS_WQ_ENDIO_DATA; |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 2490 | blk_status_t ret = 0; |
Chris Mason | 19b9bdb | 2008-10-30 14:23:13 -0400 | [diff] [blame] | 2491 | int skip_sum; |
Josef Bacik | b812ce2 | 2012-11-16 13:56:32 -0500 | [diff] [blame] | 2492 | int async = !atomic_read(&BTRFS_I(inode)->sync_writers); |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 2493 | |
Josef Bacik | 42437a6 | 2020-10-16 11:29:18 -0400 | [diff] [blame] | 2494 | skip_sum = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM) || |
| 2495 | !fs_info->csum_root; |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 2496 | |
Nikolay Borisov | 70ddc55 | 2017-02-20 13:50:35 +0200 | [diff] [blame] | 2497 | if (btrfs_is_free_space_inode(BTRFS_I(inode))) |
Chandan Rajendra | 0d51e28 | 2015-07-27 15:26:43 +0530 | [diff] [blame] | 2498 | metadata = BTRFS_WQ_ENDIO_FREE_SPACE; |
Jeff Mahoney | 0417341 | 2011-10-03 23:23:12 -0400 | [diff] [blame] | 2499 | |
Naohiro Aota | d22002f | 2021-02-04 19:22:00 +0900 | [diff] [blame] | 2500 | if (bio_op(bio) == REQ_OP_ZONE_APPEND) { |
| 2501 | struct page *page = bio_first_bvec_all(bio)->bv_page; |
| 2502 | loff_t file_offset = page_offset(page); |
| 2503 | |
| 2504 | ret = extract_ordered_extent(BTRFS_I(inode), bio, file_offset); |
| 2505 | if (ret) |
| 2506 | goto out; |
| 2507 | } |
| 2508 | |
Naohiro Aota | cfe9444 | 2021-02-04 19:21:59 +0900 | [diff] [blame] | 2509 | if (btrfs_op(bio) != BTRFS_MAP_WRITE) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2510 | ret = btrfs_bio_wq_end_io(fs_info, bio, metadata); |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2511 | if (ret) |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 2512 | goto out; |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2513 | |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 2514 | if (bio_flags & EXTENT_BIO_COMPRESSED) { |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 2515 | ret = btrfs_submit_compressed_read(inode, bio, |
| 2516 | mirror_num, |
| 2517 | bio_flags); |
| 2518 | goto out; |
Josef Bacik | 334c16d | 2020-10-16 11:29:14 -0400 | [diff] [blame] | 2519 | } else { |
| 2520 | /* |
| 2521 | * Lookup bio sums does extra checks around whether we |
| 2522 | * need to csum or not, which is why we ignore skip_sum |
| 2523 | * here. |
| 2524 | */ |
Qu Wenruo | 6275193 | 2020-12-02 14:48:06 +0800 | [diff] [blame] | 2525 | ret = btrfs_lookup_bio_sums(inode, bio, NULL); |
Tsutomu Itoh | c2db107 | 2011-03-01 06:48:31 +0000 | [diff] [blame] | 2526 | if (ret) |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 2527 | goto out; |
Tsutomu Itoh | c2db107 | 2011-03-01 06:48:31 +0000 | [diff] [blame] | 2528 | } |
Chris Mason | 4d1b5fb | 2008-08-20 09:44:52 -0400 | [diff] [blame] | 2529 | goto mapit; |
Josef Bacik | b812ce2 | 2012-11-16 13:56:32 -0500 | [diff] [blame] | 2530 | } else if (async && !skip_sum) { |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 2531 | /* csum items have already been cloned */ |
| 2532 | if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID) |
| 2533 | goto mapit; |
Chris Mason | 19b9bdb | 2008-10-30 14:23:13 -0400 | [diff] [blame] | 2534 | /* we're doing a write, do the async checksumming */ |
Qu Wenruo | 8896a08 | 2020-10-21 14:24:53 +0800 | [diff] [blame] | 2535 | ret = btrfs_wq_submit_bio(inode, bio, mirror_num, bio_flags, |
| 2536 | 0, btrfs_submit_bio_start); |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 2537 | goto out; |
Josef Bacik | b812ce2 | 2012-11-16 13:56:32 -0500 | [diff] [blame] | 2538 | } else if (!skip_sum) { |
Nikolay Borisov | bd242a0 | 2020-06-03 08:55:07 +0300 | [diff] [blame] | 2539 | ret = btrfs_csum_one_bio(BTRFS_I(inode), bio, 0, 0); |
Josef Bacik | b812ce2 | 2012-11-16 13:56:32 -0500 | [diff] [blame] | 2540 | if (ret) |
| 2541 | goto out; |
Chris Mason | 19b9bdb | 2008-10-30 14:23:13 -0400 | [diff] [blame] | 2542 | } |
| 2543 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2544 | mapit: |
Chris Mason | 08635ba | 2019-07-10 12:28:14 -0700 | [diff] [blame] | 2545 | ret = btrfs_map_bio(fs_info, bio, mirror_num); |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 2546 | |
| 2547 | out: |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 2548 | if (ret) { |
| 2549 | bio->bi_status = ret; |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 2550 | bio_endio(bio); |
| 2551 | } |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 2552 | return ret; |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 2553 | } |
Chris Mason | 6885f30 | 2008-02-20 16:11:05 -0500 | [diff] [blame] | 2554 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2555 | /* |
| 2556 | * given a list of ordered sums record them in the inode. This happens |
| 2557 | * at IO completion time based on sums calculated at bio submission time. |
| 2558 | */ |
Nikolay Borisov | 510f85e | 2020-09-18 12:15:52 +0300 | [diff] [blame] | 2559 | static int add_pending_csums(struct btrfs_trans_handle *trans, |
| 2560 | struct list_head *list) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2561 | { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2562 | struct btrfs_ordered_sum *sum; |
Nikolay Borisov | ac01f26 | 2018-01-08 10:59:43 +0200 | [diff] [blame] | 2563 | int ret; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2564 | |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 2565 | list_for_each_entry(sum, list, list) { |
David Sterba | 7c2871a | 2017-11-08 01:07:43 +0100 | [diff] [blame] | 2566 | trans->adding_csums = true; |
Nikolay Borisov | 510f85e | 2020-09-18 12:15:52 +0300 | [diff] [blame] | 2567 | ret = btrfs_csum_file_blocks(trans, trans->fs_info->csum_root, sum); |
David Sterba | 7c2871a | 2017-11-08 01:07:43 +0100 | [diff] [blame] | 2568 | trans->adding_csums = false; |
Nikolay Borisov | ac01f26 | 2018-01-08 10:59:43 +0200 | [diff] [blame] | 2569 | if (ret) |
| 2570 | return ret; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2571 | } |
| 2572 | return 0; |
| 2573 | } |
| 2574 | |
Filipe Manana | c334730 | 2020-11-04 11:07:31 +0000 | [diff] [blame] | 2575 | static int btrfs_find_new_delalloc_bytes(struct btrfs_inode *inode, |
| 2576 | const u64 start, |
| 2577 | const u64 len, |
| 2578 | struct extent_state **cached_state) |
| 2579 | { |
| 2580 | u64 search_start = start; |
| 2581 | const u64 end = start + len - 1; |
| 2582 | |
| 2583 | while (search_start < end) { |
| 2584 | const u64 search_len = end - search_start + 1; |
| 2585 | struct extent_map *em; |
| 2586 | u64 em_len; |
| 2587 | int ret = 0; |
| 2588 | |
| 2589 | em = btrfs_get_extent(inode, NULL, 0, search_start, search_len); |
| 2590 | if (IS_ERR(em)) |
| 2591 | return PTR_ERR(em); |
| 2592 | |
| 2593 | if (em->block_start != EXTENT_MAP_HOLE) |
| 2594 | goto next; |
| 2595 | |
| 2596 | em_len = em->len; |
| 2597 | if (em->start < search_start) |
| 2598 | em_len -= search_start - em->start; |
| 2599 | if (em_len > search_len) |
| 2600 | em_len = search_len; |
| 2601 | |
| 2602 | ret = set_extent_bit(&inode->io_tree, search_start, |
| 2603 | search_start + em_len - 1, |
Nikolay Borisov | 1cab5e7 | 2020-11-05 11:08:00 +0200 | [diff] [blame] | 2604 | EXTENT_DELALLOC_NEW, 0, NULL, cached_state, |
| 2605 | GFP_NOFS, NULL); |
Filipe Manana | c334730 | 2020-11-04 11:07:31 +0000 | [diff] [blame] | 2606 | next: |
| 2607 | search_start = extent_map_end(em); |
| 2608 | free_extent_map(em); |
| 2609 | if (ret) |
| 2610 | return ret; |
| 2611 | } |
| 2612 | return 0; |
| 2613 | } |
| 2614 | |
Nikolay Borisov | c2566f2 | 2020-06-03 08:55:35 +0300 | [diff] [blame] | 2615 | 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] | 2616 | unsigned int extra_bits, |
Nikolay Borisov | 330a582 | 2019-07-17 16:18:17 +0300 | [diff] [blame] | 2617 | struct extent_state **cached_state) |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 2618 | { |
Johannes Thumshirn | fdb1e12 | 2018-12-05 15:23:04 +0100 | [diff] [blame] | 2619 | WARN_ON(PAGE_ALIGNED(end)); |
Filipe Manana | c334730 | 2020-11-04 11:07:31 +0000 | [diff] [blame] | 2620 | |
| 2621 | if (start >= i_size_read(&inode->vfs_inode) && |
| 2622 | !(inode->flags & BTRFS_INODE_PREALLOC)) { |
| 2623 | /* |
| 2624 | * There can't be any extents following eof in this case so just |
| 2625 | * set the delalloc new bit for the range directly. |
| 2626 | */ |
| 2627 | extra_bits |= EXTENT_DELALLOC_NEW; |
| 2628 | } else { |
| 2629 | int ret; |
| 2630 | |
| 2631 | ret = btrfs_find_new_delalloc_bytes(inode, start, |
| 2632 | end + 1 - start, |
| 2633 | cached_state); |
| 2634 | if (ret) |
| 2635 | return ret; |
| 2636 | } |
| 2637 | |
Nikolay Borisov | c2566f2 | 2020-06-03 08:55:35 +0300 | [diff] [blame] | 2638 | return set_extent_delalloc(&inode->io_tree, start, end, extra_bits, |
| 2639 | cached_state); |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 2640 | } |
| 2641 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2642 | /* see btrfs_writepage_start_hook for details on why this is required */ |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2643 | struct btrfs_writepage_fixup { |
| 2644 | struct page *page; |
Josef Bacik | f4b1363 | 2020-01-21 14:34:52 -0500 | [diff] [blame] | 2645 | struct inode *inode; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2646 | struct btrfs_work work; |
| 2647 | }; |
| 2648 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 2649 | static void btrfs_writepage_fixup_worker(struct btrfs_work *work) |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2650 | { |
| 2651 | struct btrfs_writepage_fixup *fixup; |
| 2652 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 2653 | struct extent_state *cached_state = NULL; |
Qu Wenruo | 364ecf3 | 2017-02-27 15:10:38 +0800 | [diff] [blame] | 2654 | struct extent_changeset *data_reserved = NULL; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2655 | struct page *page; |
Nikolay Borisov | 65d87f7 | 2020-06-05 10:51:51 +0300 | [diff] [blame] | 2656 | struct btrfs_inode *inode; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2657 | u64 page_start; |
| 2658 | u64 page_end; |
Chris Mason | 25f3c50 | 2020-01-21 11:51:42 -0500 | [diff] [blame] | 2659 | int ret = 0; |
Josef Bacik | f4b1363 | 2020-01-21 14:34:52 -0500 | [diff] [blame] | 2660 | bool free_delalloc_space = true; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2661 | |
| 2662 | fixup = container_of(work, struct btrfs_writepage_fixup, work); |
| 2663 | page = fixup->page; |
Nikolay Borisov | 65d87f7 | 2020-06-05 10:51:51 +0300 | [diff] [blame] | 2664 | inode = BTRFS_I(fixup->inode); |
Josef Bacik | f4b1363 | 2020-01-21 14:34:52 -0500 | [diff] [blame] | 2665 | page_start = page_offset(page); |
| 2666 | page_end = page_offset(page) + PAGE_SIZE - 1; |
| 2667 | |
| 2668 | /* |
| 2669 | * This is similar to page_mkwrite, we need to reserve the space before |
| 2670 | * we take the page lock. |
| 2671 | */ |
Nikolay Borisov | 65d87f7 | 2020-06-05 10:51:51 +0300 | [diff] [blame] | 2672 | ret = btrfs_delalloc_reserve_space(inode, &data_reserved, page_start, |
| 2673 | PAGE_SIZE); |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 2674 | again: |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2675 | lock_page(page); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2676 | |
Chris Mason | 25f3c50 | 2020-01-21 11:51:42 -0500 | [diff] [blame] | 2677 | /* |
| 2678 | * Before we queued this fixup, we took a reference on the page. |
| 2679 | * page->mapping may go NULL, but it shouldn't be moved to a different |
| 2680 | * address space. |
| 2681 | */ |
Josef Bacik | f4b1363 | 2020-01-21 14:34:52 -0500 | [diff] [blame] | 2682 | if (!page->mapping || !PageDirty(page) || !PageChecked(page)) { |
| 2683 | /* |
| 2684 | * Unfortunately this is a little tricky, either |
| 2685 | * |
| 2686 | * 1) We got here and our page had already been dealt with and |
| 2687 | * we reserved our space, thus ret == 0, so we need to just |
| 2688 | * drop our space reservation and bail. This can happen the |
| 2689 | * first time we come into the fixup worker, or could happen |
| 2690 | * while waiting for the ordered extent. |
| 2691 | * 2) Our page was already dealt with, but we happened to get an |
| 2692 | * ENOSPC above from the btrfs_delalloc_reserve_space. In |
| 2693 | * this case we obviously don't have anything to release, but |
| 2694 | * because the page was already dealt with we don't want to |
| 2695 | * mark the page with an error, so make sure we're resetting |
| 2696 | * ret to 0. This is why we have this check _before_ the ret |
| 2697 | * check, because we do not want to have a surprise ENOSPC |
| 2698 | * when the page was already properly dealt with. |
| 2699 | */ |
| 2700 | if (!ret) { |
Nikolay Borisov | 65d87f7 | 2020-06-05 10:51:51 +0300 | [diff] [blame] | 2701 | btrfs_delalloc_release_extents(inode, PAGE_SIZE); |
| 2702 | btrfs_delalloc_release_space(inode, data_reserved, |
Josef Bacik | f4b1363 | 2020-01-21 14:34:52 -0500 | [diff] [blame] | 2703 | page_start, PAGE_SIZE, |
| 2704 | true); |
| 2705 | } |
| 2706 | ret = 0; |
Chris Mason | 25f3c50 | 2020-01-21 11:51:42 -0500 | [diff] [blame] | 2707 | goto out_page; |
Josef Bacik | f4b1363 | 2020-01-21 14:34:52 -0500 | [diff] [blame] | 2708 | } |
Chris Mason | 25f3c50 | 2020-01-21 11:51:42 -0500 | [diff] [blame] | 2709 | |
| 2710 | /* |
Josef Bacik | f4b1363 | 2020-01-21 14:34:52 -0500 | [diff] [blame] | 2711 | * We can't mess with the page state unless it is locked, so now that |
| 2712 | * it is locked bail if we failed to make our space reservation. |
Chris Mason | 25f3c50 | 2020-01-21 11:51:42 -0500 | [diff] [blame] | 2713 | */ |
Josef Bacik | f4b1363 | 2020-01-21 14:34:52 -0500 | [diff] [blame] | 2714 | if (ret) |
| 2715 | goto out_page; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2716 | |
Nikolay Borisov | 65d87f7 | 2020-06-05 10:51:51 +0300 | [diff] [blame] | 2717 | lock_extent_bits(&inode->io_tree, page_start, page_end, &cached_state); |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 2718 | |
| 2719 | /* already ordered? We're done */ |
Qu Wenruo | f57ad93 | 2021-04-07 19:22:13 +0800 | [diff] [blame] | 2720 | if (PageOrdered(page)) |
Josef Bacik | f4b1363 | 2020-01-21 14:34:52 -0500 | [diff] [blame] | 2721 | goto out_reserved; |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 2722 | |
Nikolay Borisov | 65d87f7 | 2020-06-05 10:51:51 +0300 | [diff] [blame] | 2723 | ordered = btrfs_lookup_ordered_range(inode, page_start, PAGE_SIZE); |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 2724 | if (ordered) { |
Nikolay Borisov | 65d87f7 | 2020-06-05 10:51:51 +0300 | [diff] [blame] | 2725 | unlock_extent_cached(&inode->io_tree, page_start, page_end, |
| 2726 | &cached_state); |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 2727 | unlock_page(page); |
Nikolay Borisov | c0a4360 | 2020-09-18 12:15:53 +0300 | [diff] [blame] | 2728 | btrfs_start_ordered_extent(ordered, 1); |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 2729 | btrfs_put_ordered_extent(ordered); |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 2730 | goto again; |
| 2731 | } |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2732 | |
Nikolay Borisov | 65d87f7 | 2020-06-05 10:51:51 +0300 | [diff] [blame] | 2733 | ret = btrfs_set_extent_delalloc(inode, page_start, page_end, 0, |
Nikolay Borisov | 330a582 | 2019-07-17 16:18:17 +0300 | [diff] [blame] | 2734 | &cached_state); |
Chris Mason | 25f3c50 | 2020-01-21 11:51:42 -0500 | [diff] [blame] | 2735 | if (ret) |
Filipe Manana | 5368700 | 2019-10-09 17:43:59 +0100 | [diff] [blame] | 2736 | goto out_reserved; |
Nikolay Borisov | f3038ee | 2017-12-05 09:29:19 +0200 | [diff] [blame] | 2737 | |
Chris Mason | 25f3c50 | 2020-01-21 11:51:42 -0500 | [diff] [blame] | 2738 | /* |
| 2739 | * Everything went as planned, we're now the owner of a dirty page with |
| 2740 | * delayed allocation bits set and space reserved for our COW |
| 2741 | * destination. |
| 2742 | * |
| 2743 | * The page was dirty when we started, nothing should have cleaned it. |
| 2744 | */ |
| 2745 | BUG_ON(!PageDirty(page)); |
Josef Bacik | f4b1363 | 2020-01-21 14:34:52 -0500 | [diff] [blame] | 2746 | free_delalloc_space = false; |
Filipe Manana | 5368700 | 2019-10-09 17:43:59 +0100 | [diff] [blame] | 2747 | out_reserved: |
Nikolay Borisov | 65d87f7 | 2020-06-05 10:51:51 +0300 | [diff] [blame] | 2748 | btrfs_delalloc_release_extents(inode, PAGE_SIZE); |
Josef Bacik | f4b1363 | 2020-01-21 14:34:52 -0500 | [diff] [blame] | 2749 | if (free_delalloc_space) |
Nikolay Borisov | 65d87f7 | 2020-06-05 10:51:51 +0300 | [diff] [blame] | 2750 | btrfs_delalloc_release_space(inode, data_reserved, page_start, |
| 2751 | PAGE_SIZE, true); |
| 2752 | unlock_extent_cached(&inode->io_tree, page_start, page_end, |
David Sterba | e43bbe5 | 2017-12-12 21:43:52 +0100 | [diff] [blame] | 2753 | &cached_state); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2754 | out_page: |
Chris Mason | 25f3c50 | 2020-01-21 11:51:42 -0500 | [diff] [blame] | 2755 | if (ret) { |
| 2756 | /* |
| 2757 | * We hit ENOSPC or other errors. Update the mapping and page |
| 2758 | * to reflect the errors and clean the page. |
| 2759 | */ |
| 2760 | mapping_set_error(page->mapping, ret); |
| 2761 | end_extent_writepage(page, ret, page_start, page_end); |
| 2762 | clear_page_dirty_for_io(page); |
| 2763 | SetPageError(page); |
| 2764 | } |
| 2765 | ClearPageChecked(page); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2766 | unlock_page(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2767 | put_page(page); |
Miao Xie | b897abe | 2011-01-26 16:19:22 +0800 | [diff] [blame] | 2768 | kfree(fixup); |
Qu Wenruo | 364ecf3 | 2017-02-27 15:10:38 +0800 | [diff] [blame] | 2769 | extent_changeset_free(data_reserved); |
Josef Bacik | f4b1363 | 2020-01-21 14:34:52 -0500 | [diff] [blame] | 2770 | /* |
| 2771 | * As a precaution, do a delayed iput in case it would be the last iput |
| 2772 | * that could need flushing space. Recursing back to fixup worker would |
| 2773 | * deadlock. |
| 2774 | */ |
Nikolay Borisov | 65d87f7 | 2020-06-05 10:51:51 +0300 | [diff] [blame] | 2775 | btrfs_add_delayed_iput(&inode->vfs_inode); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2776 | } |
| 2777 | |
| 2778 | /* |
| 2779 | * There are a few paths in the higher layers of the kernel that directly |
| 2780 | * set the page dirty bit without asking the filesystem if it is a |
| 2781 | * good idea. This causes problems because we want to make sure COW |
| 2782 | * properly happens and the data=ordered rules are followed. |
| 2783 | * |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2784 | * 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] | 2785 | * hasn't been properly setup for IO. We kick off an async process |
| 2786 | * to fix it up. The async helper will wait for ordered extents, set |
| 2787 | * the delalloc bit and make it safe to write the page. |
| 2788 | */ |
Qu Wenruo | a129ffb | 2021-07-27 13:41:32 +0800 | [diff] [blame] | 2789 | int btrfs_writepage_cow_fixup(struct page *page) |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2790 | { |
| 2791 | struct inode *inode = page->mapping->host; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2792 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2793 | struct btrfs_writepage_fixup *fixup; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2794 | |
Qu Wenruo | f57ad93 | 2021-04-07 19:22:13 +0800 | [diff] [blame] | 2795 | /* This page has ordered extent covering it already */ |
| 2796 | if (PageOrdered(page)) |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2797 | return 0; |
| 2798 | |
Chris Mason | 25f3c50 | 2020-01-21 11:51:42 -0500 | [diff] [blame] | 2799 | /* |
| 2800 | * PageChecked is set below when we create a fixup worker for this page, |
| 2801 | * don't try to create another one if we're already PageChecked() |
| 2802 | * |
| 2803 | * The extent_io writepage code will redirty the page if we send back |
| 2804 | * EAGAIN. |
| 2805 | */ |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2806 | if (PageChecked(page)) |
| 2807 | return -EAGAIN; |
| 2808 | |
| 2809 | fixup = kzalloc(sizeof(*fixup), GFP_NOFS); |
| 2810 | if (!fixup) |
| 2811 | return -EAGAIN; |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 2812 | |
Josef Bacik | f4b1363 | 2020-01-21 14:34:52 -0500 | [diff] [blame] | 2813 | /* |
| 2814 | * We are already holding a reference to this inode from |
| 2815 | * write_cache_pages. We need to hold it because the space reservation |
| 2816 | * takes place outside of the page lock, and we can't trust |
| 2817 | * page->mapping outside of the page lock. |
| 2818 | */ |
| 2819 | ihold(inode); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2820 | SetPageChecked(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2821 | get_page(page); |
Omar Sandoval | a0cac0e | 2019-09-16 11:30:57 -0700 | [diff] [blame] | 2822 | btrfs_init_work(&fixup->work, btrfs_writepage_fixup_worker, NULL, NULL); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2823 | fixup->page = page; |
Josef Bacik | f4b1363 | 2020-01-21 14:34:52 -0500 | [diff] [blame] | 2824 | fixup->inode = inode; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2825 | btrfs_queue_work(fs_info->fixup_workers, &fixup->work); |
Chris Mason | 25f3c50 | 2020-01-21 11:51:42 -0500 | [diff] [blame] | 2826 | |
| 2827 | return -EAGAIN; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2828 | } |
| 2829 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2830 | static int insert_reserved_file_extent(struct btrfs_trans_handle *trans, |
Nikolay Borisov | c553f94 | 2020-06-03 08:55:19 +0300 | [diff] [blame] | 2831 | struct btrfs_inode *inode, u64 file_pos, |
Qu Wenruo | 9729f10 | 2020-06-10 09:04:41 +0800 | [diff] [blame] | 2832 | struct btrfs_file_extent_item *stack_fi, |
Filipe Manana | 2766ff6 | 2020-11-04 11:07:34 +0000 | [diff] [blame] | 2833 | const bool update_inode_bytes, |
Qu Wenruo | 9729f10 | 2020-06-10 09:04:41 +0800 | [diff] [blame] | 2834 | u64 qgroup_reserved) |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2835 | { |
Nikolay Borisov | c553f94 | 2020-06-03 08:55:19 +0300 | [diff] [blame] | 2836 | struct btrfs_root *root = inode->root; |
Filipe Manana | 2766ff6 | 2020-11-04 11:07:34 +0000 | [diff] [blame] | 2837 | const u64 sectorsize = root->fs_info->sectorsize; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2838 | struct btrfs_path *path; |
| 2839 | struct extent_buffer *leaf; |
| 2840 | struct btrfs_key ins; |
Qu Wenruo | 203f44c5 | 2020-06-10 09:04:40 +0800 | [diff] [blame] | 2841 | u64 disk_num_bytes = btrfs_stack_file_extent_disk_num_bytes(stack_fi); |
| 2842 | u64 disk_bytenr = btrfs_stack_file_extent_disk_bytenr(stack_fi); |
| 2843 | u64 num_bytes = btrfs_stack_file_extent_num_bytes(stack_fi); |
| 2844 | u64 ram_bytes = btrfs_stack_file_extent_ram_bytes(stack_fi); |
Filipe Manana | 5893dfb | 2020-11-04 11:07:32 +0000 | [diff] [blame] | 2845 | struct btrfs_drop_extents_args drop_args = { 0 }; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2846 | int ret; |
| 2847 | |
| 2848 | path = btrfs_alloc_path(); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 2849 | if (!path) |
| 2850 | return -ENOMEM; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2851 | |
Chris Mason | a1ed835 | 2009-09-11 12:27:37 -0400 | [diff] [blame] | 2852 | /* |
| 2853 | * we may be replacing one extent in the tree with another. |
| 2854 | * The new extent is pinned in the extent map, and we don't want |
| 2855 | * to drop it from the cache until it is completely in the btree. |
| 2856 | * |
| 2857 | * So, tell btrfs_drop_extents to leave this extent in the cache. |
| 2858 | * the caller is expected to unpin it and allow it to be merged |
| 2859 | * with the others. |
| 2860 | */ |
Filipe Manana | 5893dfb | 2020-11-04 11:07:32 +0000 | [diff] [blame] | 2861 | drop_args.path = path; |
| 2862 | drop_args.start = file_pos; |
| 2863 | drop_args.end = file_pos + num_bytes; |
| 2864 | drop_args.replace_extent = true; |
| 2865 | drop_args.extent_item_size = sizeof(*stack_fi); |
| 2866 | ret = btrfs_drop_extents(trans, root, inode, &drop_args); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2867 | if (ret) |
| 2868 | goto out; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2869 | |
Filipe Manana | 5893dfb | 2020-11-04 11:07:32 +0000 | [diff] [blame] | 2870 | if (!drop_args.extent_inserted) { |
Nikolay Borisov | c553f94 | 2020-06-03 08:55:19 +0300 | [diff] [blame] | 2871 | ins.objectid = btrfs_ino(inode); |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 2872 | ins.offset = file_pos; |
| 2873 | ins.type = BTRFS_EXTENT_DATA_KEY; |
| 2874 | |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 2875 | ret = btrfs_insert_empty_item(trans, root, path, &ins, |
Qu Wenruo | 203f44c5 | 2020-06-10 09:04:40 +0800 | [diff] [blame] | 2876 | sizeof(*stack_fi)); |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 2877 | if (ret) |
| 2878 | goto out; |
| 2879 | } |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2880 | leaf = path->nodes[0]; |
Qu Wenruo | 203f44c5 | 2020-06-10 09:04:40 +0800 | [diff] [blame] | 2881 | btrfs_set_stack_file_extent_generation(stack_fi, trans->transid); |
| 2882 | write_extent_buffer(leaf, stack_fi, |
| 2883 | btrfs_item_ptr_offset(leaf, path->slots[0]), |
| 2884 | sizeof(struct btrfs_file_extent_item)); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 2885 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2886 | btrfs_mark_buffer_dirty(leaf); |
Josef Bacik | ce19533 | 2012-09-25 15:26:16 -0400 | [diff] [blame] | 2887 | btrfs_release_path(path); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2888 | |
Filipe Manana | 2766ff6 | 2020-11-04 11:07:34 +0000 | [diff] [blame] | 2889 | /* |
| 2890 | * If we dropped an inline extent here, we know the range where it is |
| 2891 | * 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] | 2892 | * number of bytes only for that range containing the inline extent. |
Filipe Manana | 2766ff6 | 2020-11-04 11:07:34 +0000 | [diff] [blame] | 2893 | * The remaining of the range will be processed when clearning the |
| 2894 | * EXTENT_DELALLOC_BIT bit through the ordered extent completion. |
| 2895 | */ |
| 2896 | if (file_pos == 0 && !IS_ALIGNED(drop_args.bytes_found, sectorsize)) { |
| 2897 | u64 inline_size = round_down(drop_args.bytes_found, sectorsize); |
| 2898 | |
| 2899 | inline_size = drop_args.bytes_found - inline_size; |
| 2900 | btrfs_update_inode_bytes(inode, sectorsize, inline_size); |
| 2901 | drop_args.bytes_found -= inline_size; |
| 2902 | num_bytes -= sectorsize; |
| 2903 | } |
| 2904 | |
| 2905 | if (update_inode_bytes) |
| 2906 | btrfs_update_inode_bytes(inode, num_bytes, drop_args.bytes_found); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2907 | |
| 2908 | ins.objectid = disk_bytenr; |
| 2909 | ins.offset = disk_num_bytes; |
| 2910 | ins.type = BTRFS_EXTENT_ITEM_KEY; |
Qu Wenruo | a12b877 | 2017-02-27 15:10:37 +0800 | [diff] [blame] | 2911 | |
Nikolay Borisov | c553f94 | 2020-06-03 08:55:19 +0300 | [diff] [blame] | 2912 | ret = btrfs_inode_set_file_extent_range(inode, file_pos, ram_bytes); |
Josef Bacik | 9ddc959 | 2020-01-17 09:02:22 -0500 | [diff] [blame] | 2913 | if (ret) |
| 2914 | goto out; |
| 2915 | |
Nikolay Borisov | c553f94 | 2020-06-03 08:55:19 +0300 | [diff] [blame] | 2916 | ret = btrfs_alloc_reserved_file_extent(trans, root, btrfs_ino(inode), |
Qu Wenruo | 9729f10 | 2020-06-10 09:04:41 +0800 | [diff] [blame] | 2917 | file_pos, qgroup_reserved, &ins); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2918 | out: |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2919 | btrfs_free_path(path); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 2920 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2921 | return ret; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2922 | } |
| 2923 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2924 | static void btrfs_release_delalloc_bytes(struct btrfs_fs_info *fs_info, |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 2925 | u64 start, u64 len) |
| 2926 | { |
David Sterba | 32da5386 | 2019-10-29 19:20:18 +0100 | [diff] [blame] | 2927 | struct btrfs_block_group *cache; |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 2928 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2929 | cache = btrfs_lookup_block_group(fs_info, start); |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 2930 | ASSERT(cache); |
| 2931 | |
| 2932 | spin_lock(&cache->lock); |
| 2933 | cache->delalloc_bytes -= len; |
| 2934 | spin_unlock(&cache->lock); |
| 2935 | |
| 2936 | btrfs_put_block_group(cache); |
| 2937 | } |
| 2938 | |
Qu Wenruo | 203f44c5 | 2020-06-10 09:04:40 +0800 | [diff] [blame] | 2939 | static int insert_ordered_extent_file_extent(struct btrfs_trans_handle *trans, |
Qu Wenruo | 203f44c5 | 2020-06-10 09:04:40 +0800 | [diff] [blame] | 2940 | struct btrfs_ordered_extent *oe) |
| 2941 | { |
| 2942 | struct btrfs_file_extent_item stack_fi; |
| 2943 | u64 logical_len; |
Filipe Manana | 2766ff6 | 2020-11-04 11:07:34 +0000 | [diff] [blame] | 2944 | bool update_inode_bytes; |
Qu Wenruo | 203f44c5 | 2020-06-10 09:04:40 +0800 | [diff] [blame] | 2945 | |
| 2946 | memset(&stack_fi, 0, sizeof(stack_fi)); |
| 2947 | btrfs_set_stack_file_extent_type(&stack_fi, BTRFS_FILE_EXTENT_REG); |
| 2948 | btrfs_set_stack_file_extent_disk_bytenr(&stack_fi, oe->disk_bytenr); |
| 2949 | btrfs_set_stack_file_extent_disk_num_bytes(&stack_fi, |
| 2950 | oe->disk_num_bytes); |
| 2951 | if (test_bit(BTRFS_ORDERED_TRUNCATED, &oe->flags)) |
| 2952 | logical_len = oe->truncated_len; |
| 2953 | else |
| 2954 | logical_len = oe->num_bytes; |
| 2955 | btrfs_set_stack_file_extent_num_bytes(&stack_fi, logical_len); |
| 2956 | btrfs_set_stack_file_extent_ram_bytes(&stack_fi, logical_len); |
| 2957 | btrfs_set_stack_file_extent_compression(&stack_fi, oe->compress_type); |
| 2958 | /* Encryption and other encoding is reserved and all 0 */ |
| 2959 | |
Filipe Manana | 2766ff6 | 2020-11-04 11:07:34 +0000 | [diff] [blame] | 2960 | /* |
| 2961 | * For delalloc, when completing an ordered extent we update the inode's |
| 2962 | * bytes when clearing the range in the inode's io tree, so pass false |
| 2963 | * as the argument 'update_inode_bytes' to insert_reserved_file_extent(), |
| 2964 | * except if the ordered extent was truncated. |
| 2965 | */ |
| 2966 | update_inode_bytes = test_bit(BTRFS_ORDERED_DIRECT, &oe->flags) || |
| 2967 | test_bit(BTRFS_ORDERED_TRUNCATED, &oe->flags); |
| 2968 | |
Nikolay Borisov | 3c38c87 | 2020-09-18 12:15:51 +0300 | [diff] [blame] | 2969 | return insert_reserved_file_extent(trans, BTRFS_I(oe->inode), |
| 2970 | oe->file_offset, &stack_fi, |
Filipe Manana | 2766ff6 | 2020-11-04 11:07:34 +0000 | [diff] [blame] | 2971 | update_inode_bytes, oe->qgroup_rsv); |
Qu Wenruo | 203f44c5 | 2020-06-10 09:04:40 +0800 | [diff] [blame] | 2972 | } |
| 2973 | |
| 2974 | /* |
| 2975 | * As ordered data IO finishes, this gets called so we can finish |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2976 | * an ordered extent if the range of bytes in the file it covers are |
| 2977 | * fully written. |
| 2978 | */ |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2979 | static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2980 | { |
Nikolay Borisov | 72e7e6e | 2020-11-02 16:48:56 +0200 | [diff] [blame] | 2981 | struct btrfs_inode *inode = BTRFS_I(ordered_extent->inode); |
| 2982 | struct btrfs_root *root = inode->root; |
| 2983 | struct btrfs_fs_info *fs_info = root->fs_info; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 2984 | struct btrfs_trans_handle *trans = NULL; |
Nikolay Borisov | 72e7e6e | 2020-11-02 16:48:56 +0200 | [diff] [blame] | 2985 | struct extent_io_tree *io_tree = &inode->io_tree; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 2986 | struct extent_state *cached_state = NULL; |
Omar Sandoval | bffe633 | 2019-12-02 17:34:19 -0800 | [diff] [blame] | 2987 | u64 start, end; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 2988 | int compress_type = 0; |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 2989 | int ret = 0; |
Omar Sandoval | bffe633 | 2019-12-02 17:34:19 -0800 | [diff] [blame] | 2990 | u64 logical_len = ordered_extent->num_bytes; |
Nikolay Borisov | 8d51012 | 2019-10-08 20:43:06 +0300 | [diff] [blame] | 2991 | bool freespace_inode; |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 2992 | bool truncated = false; |
Josef Bacik | 49940bd | 2018-10-11 15:54:21 -0400 | [diff] [blame] | 2993 | bool clear_reserved_extent = true; |
Filipe Manana | 2766ff6 | 2020-11-04 11:07:34 +0000 | [diff] [blame] | 2994 | unsigned int clear_bits = EXTENT_DEFRAG; |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 2995 | |
Omar Sandoval | bffe633 | 2019-12-02 17:34:19 -0800 | [diff] [blame] | 2996 | start = ordered_extent->file_offset; |
| 2997 | end = start + ordered_extent->num_bytes - 1; |
| 2998 | |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 2999 | if (!test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) && |
| 3000 | !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags) && |
| 3001 | !test_bit(BTRFS_ORDERED_DIRECT, &ordered_extent->flags)) |
Filipe Manana | 2766ff6 | 2020-11-04 11:07:34 +0000 | [diff] [blame] | 3002 | clear_bits |= EXTENT_DELALLOC_NEW; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3003 | |
Nikolay Borisov | 72e7e6e | 2020-11-02 16:48:56 +0200 | [diff] [blame] | 3004 | freespace_inode = btrfs_is_free_space_inode(inode); |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 3005 | |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 3006 | if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) { |
| 3007 | ret = -EIO; |
| 3008 | goto out; |
| 3009 | } |
| 3010 | |
Christoph Hellwig | c7c3a6d | 2021-07-22 09:53:59 +0200 | [diff] [blame] | 3011 | if (ordered_extent->bdev) |
Naohiro Aota | d8e3fb1 | 2021-02-04 19:22:05 +0900 | [diff] [blame] | 3012 | btrfs_rewrite_logical_zoned(ordered_extent); |
| 3013 | |
Nikolay Borisov | 72e7e6e | 2020-11-02 16:48:56 +0200 | [diff] [blame] | 3014 | btrfs_free_io_failure_record(inode, start, end); |
Miao Xie | f612496 | 2014-09-12 18:44:04 +0800 | [diff] [blame] | 3015 | |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 3016 | if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) { |
| 3017 | truncated = true; |
| 3018 | logical_len = ordered_extent->truncated_len; |
| 3019 | /* Truncated the entire extent, don't bother adding */ |
| 3020 | if (!logical_len) |
| 3021 | goto out; |
| 3022 | } |
| 3023 | |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 3024 | if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) { |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3025 | BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */ |
Qu Wenruo | 94ed938 | 2015-09-08 17:25:56 +0800 | [diff] [blame] | 3026 | |
Nikolay Borisov | 72e7e6e | 2020-11-02 16:48:56 +0200 | [diff] [blame] | 3027 | btrfs_inode_safe_disk_i_size_write(inode, 0); |
Nikolay Borisov | 8d51012 | 2019-10-08 20:43:06 +0300 | [diff] [blame] | 3028 | if (freespace_inode) |
| 3029 | trans = btrfs_join_transaction_spacecache(root); |
Josef Bacik | 6c760c0 | 2012-11-09 10:53:21 -0500 | [diff] [blame] | 3030 | else |
| 3031 | trans = btrfs_join_transaction(root); |
| 3032 | if (IS_ERR(trans)) { |
| 3033 | ret = PTR_ERR(trans); |
| 3034 | trans = NULL; |
| 3035 | goto out; |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 3036 | } |
Nikolay Borisov | 72e7e6e | 2020-11-02 16:48:56 +0200 | [diff] [blame] | 3037 | trans->block_rsv = &inode->block_rsv; |
Nikolay Borisov | 729f796 | 2020-11-02 16:49:06 +0200 | [diff] [blame] | 3038 | ret = btrfs_update_inode_fallback(trans, root, inode); |
Josef Bacik | 6c760c0 | 2012-11-09 10:53:21 -0500 | [diff] [blame] | 3039 | if (ret) /* -ENOMEM or corruption */ |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3040 | btrfs_abort_transaction(trans, ret); |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 3041 | goto out; |
| 3042 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3043 | |
Filipe Manana | 2766ff6 | 2020-11-04 11:07:34 +0000 | [diff] [blame] | 3044 | clear_bits |= EXTENT_LOCKED; |
Omar Sandoval | bffe633 | 2019-12-02 17:34:19 -0800 | [diff] [blame] | 3045 | lock_extent_bits(io_tree, start, end, &cached_state); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3046 | |
Nikolay Borisov | 8d51012 | 2019-10-08 20:43:06 +0300 | [diff] [blame] | 3047 | if (freespace_inode) |
| 3048 | trans = btrfs_join_transaction_spacecache(root); |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 3049 | else |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 3050 | trans = btrfs_join_transaction(root); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3051 | if (IS_ERR(trans)) { |
| 3052 | ret = PTR_ERR(trans); |
| 3053 | trans = NULL; |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 3054 | goto out; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3055 | } |
Chris Mason | a79b7d4 | 2014-05-22 16:18:52 -0700 | [diff] [blame] | 3056 | |
Nikolay Borisov | 72e7e6e | 2020-11-02 16:48:56 +0200 | [diff] [blame] | 3057 | trans->block_rsv = &inode->block_rsv; |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 3058 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 3059 | if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags)) |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 3060 | compress_type = ordered_extent->compress_type; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 3061 | if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) { |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 3062 | BUG_ON(compress_type); |
Nikolay Borisov | 72e7e6e | 2020-11-02 16:48:56 +0200 | [diff] [blame] | 3063 | ret = btrfs_mark_extent_written(trans, inode, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 3064 | ordered_extent->file_offset, |
| 3065 | ordered_extent->file_offset + |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 3066 | logical_len); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 3067 | } else { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3068 | BUG_ON(root == fs_info->tree_root); |
Nikolay Borisov | 3c38c87 | 2020-09-18 12:15:51 +0300 | [diff] [blame] | 3069 | ret = insert_ordered_extent_file_extent(trans, ordered_extent); |
Josef Bacik | 49940bd | 2018-10-11 15:54:21 -0400 | [diff] [blame] | 3070 | if (!ret) { |
| 3071 | clear_reserved_extent = false; |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3072 | btrfs_release_delalloc_bytes(fs_info, |
Omar Sandoval | bffe633 | 2019-12-02 17:34:19 -0800 | [diff] [blame] | 3073 | ordered_extent->disk_bytenr, |
| 3074 | ordered_extent->disk_num_bytes); |
Josef Bacik | 49940bd | 2018-10-11 15:54:21 -0400 | [diff] [blame] | 3075 | } |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 3076 | } |
Nikolay Borisov | 72e7e6e | 2020-11-02 16:48:56 +0200 | [diff] [blame] | 3077 | unpin_extent_cache(&inode->extent_tree, ordered_extent->file_offset, |
Omar Sandoval | bffe633 | 2019-12-02 17:34:19 -0800 | [diff] [blame] | 3078 | ordered_extent->num_bytes, trans->transid); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3079 | if (ret < 0) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3080 | btrfs_abort_transaction(trans, ret); |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 3081 | goto out; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3082 | } |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 3083 | |
Nikolay Borisov | 510f85e | 2020-09-18 12:15:52 +0300 | [diff] [blame] | 3084 | ret = add_pending_csums(trans, &ordered_extent->list); |
Nikolay Borisov | ac01f26 | 2018-01-08 10:59:43 +0200 | [diff] [blame] | 3085 | if (ret) { |
| 3086 | btrfs_abort_transaction(trans, ret); |
| 3087 | goto out; |
| 3088 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3089 | |
Filipe Manana | 2766ff6 | 2020-11-04 11:07:34 +0000 | [diff] [blame] | 3090 | /* |
| 3091 | * If this is a new delalloc range, clear its new delalloc flag to |
| 3092 | * update the inode's number of bytes. This needs to be done first |
| 3093 | * before updating the inode item. |
| 3094 | */ |
| 3095 | if ((clear_bits & EXTENT_DELALLOC_NEW) && |
| 3096 | !test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) |
Nikolay Borisov | 72e7e6e | 2020-11-02 16:48:56 +0200 | [diff] [blame] | 3097 | clear_extent_bit(&inode->io_tree, start, end, |
Filipe Manana | 2766ff6 | 2020-11-04 11:07:34 +0000 | [diff] [blame] | 3098 | EXTENT_DELALLOC_NEW | EXTENT_ADD_INODE_BYTES, |
| 3099 | 0, 0, &cached_state); |
| 3100 | |
Nikolay Borisov | 72e7e6e | 2020-11-02 16:48:56 +0200 | [diff] [blame] | 3101 | btrfs_inode_safe_disk_i_size_write(inode, 0); |
Nikolay Borisov | 729f796 | 2020-11-02 16:49:06 +0200 | [diff] [blame] | 3102 | ret = btrfs_update_inode_fallback(trans, root, inode); |
Josef Bacik | 6c760c0 | 2012-11-09 10:53:21 -0500 | [diff] [blame] | 3103 | if (ret) { /* -ENOMEM or corruption */ |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3104 | btrfs_abort_transaction(trans, ret); |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 3105 | goto out; |
Josef Bacik | 1ef30be | 2011-04-05 19:25:36 -0400 | [diff] [blame] | 3106 | } |
| 3107 | ret = 0; |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 3108 | out: |
Nikolay Borisov | 72e7e6e | 2020-11-02 16:48:56 +0200 | [diff] [blame] | 3109 | clear_extent_bit(&inode->io_tree, start, end, clear_bits, |
Omar Sandoval | bffe633 | 2019-12-02 17:34:19 -0800 | [diff] [blame] | 3110 | (clear_bits & EXTENT_LOCKED) ? 1 : 0, 0, |
Omar Sandoval | 313facc | 2019-12-02 17:34:18 -0800 | [diff] [blame] | 3111 | &cached_state); |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 3112 | |
Miao Xie | a698d075 | 2012-09-20 01:51:59 -0600 | [diff] [blame] | 3113 | if (trans) |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 3114 | btrfs_end_transaction(trans); |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 3115 | |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 3116 | if (ret || truncated) { |
Omar Sandoval | bffe633 | 2019-12-02 17:34:19 -0800 | [diff] [blame] | 3117 | u64 unwritten_start = start; |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 3118 | |
Josef Bacik | d61bec0 | 2021-05-19 09:38:27 -0400 | [diff] [blame] | 3119 | /* |
| 3120 | * If we failed to finish this ordered extent for any reason we |
| 3121 | * need to make sure BTRFS_ORDERED_IOERR is set on the ordered |
| 3122 | * extent, and mark the inode with the error if it wasn't |
| 3123 | * already set. Any error during writeback would have already |
| 3124 | * set the mapping error, so we need to set it if we're the ones |
| 3125 | * marking this ordered extent as failed. |
| 3126 | */ |
| 3127 | if (ret && !test_and_set_bit(BTRFS_ORDERED_IOERR, |
| 3128 | &ordered_extent->flags)) |
| 3129 | mapping_set_error(ordered_extent->inode->i_mapping, -EIO); |
| 3130 | |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 3131 | if (truncated) |
Omar Sandoval | bffe633 | 2019-12-02 17:34:19 -0800 | [diff] [blame] | 3132 | unwritten_start += logical_len; |
| 3133 | clear_extent_uptodate(io_tree, unwritten_start, end, NULL); |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 3134 | |
| 3135 | /* 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] | 3136 | btrfs_drop_extent_cache(inode, unwritten_start, end, 0); |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 3137 | |
Josef Bacik | 0bec9ef | 2013-01-31 14:58:00 -0500 | [diff] [blame] | 3138 | /* |
| 3139 | * If the ordered extent had an IOERR or something else went |
| 3140 | * wrong we need to return the space for this ordered extent |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 3141 | * back to the allocator. We only free the extent in the |
| 3142 | * truncated case if we didn't write out the extent at all. |
Josef Bacik | 49940bd | 2018-10-11 15:54:21 -0400 | [diff] [blame] | 3143 | * |
| 3144 | * If we made it past insert_reserved_file_extent before we |
| 3145 | * errored out then we don't need to do this as the accounting |
| 3146 | * has already been done. |
Josef Bacik | 0bec9ef | 2013-01-31 14:58:00 -0500 | [diff] [blame] | 3147 | */ |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 3148 | if ((ret || !logical_len) && |
Josef Bacik | 49940bd | 2018-10-11 15:54:21 -0400 | [diff] [blame] | 3149 | clear_reserved_extent && |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 3150 | !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) && |
Nikolay Borisov | 4eaaec2 | 2019-11-21 14:03:29 +0200 | [diff] [blame] | 3151 | !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) { |
| 3152 | /* |
| 3153 | * Discard the range before returning it back to the |
| 3154 | * free space pool |
| 3155 | */ |
Dennis Zhou | 46b27f5 | 2019-12-13 16:22:11 -0800 | [diff] [blame] | 3156 | if (ret && btrfs_test_opt(fs_info, DISCARD_SYNC)) |
Nikolay Borisov | 4eaaec2 | 2019-11-21 14:03:29 +0200 | [diff] [blame] | 3157 | btrfs_discard_extent(fs_info, |
Omar Sandoval | bffe633 | 2019-12-02 17:34:19 -0800 | [diff] [blame] | 3158 | ordered_extent->disk_bytenr, |
| 3159 | ordered_extent->disk_num_bytes, |
| 3160 | NULL); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3161 | btrfs_free_reserved_extent(fs_info, |
Omar Sandoval | bffe633 | 2019-12-02 17:34:19 -0800 | [diff] [blame] | 3162 | ordered_extent->disk_bytenr, |
| 3163 | ordered_extent->disk_num_bytes, 1); |
Nikolay Borisov | 4eaaec2 | 2019-11-21 14:03:29 +0200 | [diff] [blame] | 3164 | } |
Josef Bacik | 0bec9ef | 2013-01-31 14:58:00 -0500 | [diff] [blame] | 3165 | } |
| 3166 | |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 3167 | /* |
Liu Bo | 8bad3c0 | 2012-06-18 12:14:23 +0800 | [diff] [blame] | 3168 | * This needs to be done to make sure anybody waiting knows we are done |
| 3169 | * updating everything for this ordered extent. |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 3170 | */ |
Nikolay Borisov | 72e7e6e | 2020-11-02 16:48:56 +0200 | [diff] [blame] | 3171 | btrfs_remove_ordered_extent(inode, ordered_extent); |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 3172 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3173 | /* once for us */ |
| 3174 | btrfs_put_ordered_extent(ordered_extent); |
| 3175 | /* once for the tree */ |
| 3176 | btrfs_put_ordered_extent(ordered_extent); |
| 3177 | |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 3178 | return ret; |
| 3179 | } |
| 3180 | |
| 3181 | static void finish_ordered_fn(struct btrfs_work *work) |
| 3182 | { |
| 3183 | struct btrfs_ordered_extent *ordered_extent; |
| 3184 | ordered_extent = container_of(work, struct btrfs_ordered_extent, work); |
| 3185 | btrfs_finish_ordered_io(ordered_extent); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3186 | } |
| 3187 | |
Qu Wenruo | 38a39ac7 | 2021-04-08 20:32:27 +0800 | [diff] [blame] | 3188 | void btrfs_writepage_endio_finish_ordered(struct btrfs_inode *inode, |
| 3189 | struct page *page, u64 start, |
David Sterba | 25c1252 | 2021-07-26 14:15:08 +0200 | [diff] [blame] | 3190 | u64 end, bool uptodate) |
Chris Mason | 211f90e | 2008-07-18 11:56:15 -0400 | [diff] [blame] | 3191 | { |
Qu Wenruo | 38a39ac7 | 2021-04-08 20:32:27 +0800 | [diff] [blame] | 3192 | trace_btrfs_writepage_end_io_hook(inode, start, end, uptodate); |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 3193 | |
Qu Wenruo | e65f152 | 2021-04-01 15:15:06 +0800 | [diff] [blame] | 3194 | btrfs_mark_ordered_io_finished(inode, page, start, end + 1 - start, |
| 3195 | finish_ordered_fn, uptodate); |
Chris Mason | 211f90e | 2008-07-18 11:56:15 -0400 | [diff] [blame] | 3196 | } |
| 3197 | |
Qu Wenruo | 265d4ac | 2020-10-21 14:24:54 +0800 | [diff] [blame] | 3198 | /* |
| 3199 | * check_data_csum - verify checksum of one sector of uncompressed data |
Qu Wenruo | 7ffd27e | 2020-12-02 14:47:58 +0800 | [diff] [blame] | 3200 | * @inode: inode |
Qu Wenruo | 265d4ac | 2020-10-21 14:24:54 +0800 | [diff] [blame] | 3201 | * @io_bio: btrfs_io_bio which contains the csum |
Qu Wenruo | 7ffd27e | 2020-12-02 14:47:58 +0800 | [diff] [blame] | 3202 | * @bio_offset: offset to the beginning of the bio (in bytes) |
Qu Wenruo | 265d4ac | 2020-10-21 14:24:54 +0800 | [diff] [blame] | 3203 | * @page: page where is the data to be verified |
| 3204 | * @pgoff: offset inside the page |
Omar Sandoval | c1d6abd | 2021-03-16 12:43:00 -0700 | [diff] [blame] | 3205 | * @start: logical offset in the file |
Qu Wenruo | 265d4ac | 2020-10-21 14:24:54 +0800 | [diff] [blame] | 3206 | * |
| 3207 | * The length of such check is always one sector size. |
| 3208 | */ |
Omar Sandoval | 47df776 | 2020-04-16 14:46:17 -0700 | [diff] [blame] | 3209 | static int check_data_csum(struct inode *inode, struct btrfs_io_bio *io_bio, |
Omar Sandoval | c1d6abd | 2021-03-16 12:43:00 -0700 | [diff] [blame] | 3210 | u32 bio_offset, struct page *page, u32 pgoff, |
| 3211 | u64 start) |
Miao Xie | dc380ae | 2014-09-12 18:43:55 +0800 | [diff] [blame] | 3212 | { |
Johannes Thumshirn | d517857 | 2019-06-03 16:58:57 +0200 | [diff] [blame] | 3213 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
| 3214 | SHASH_DESC_ON_STACK(shash, fs_info->csum_shash); |
Miao Xie | dc380ae | 2014-09-12 18:43:55 +0800 | [diff] [blame] | 3215 | char *kaddr; |
Qu Wenruo | 265d4ac | 2020-10-21 14:24:54 +0800 | [diff] [blame] | 3216 | u32 len = fs_info->sectorsize; |
David Sterba | 223486c | 2020-07-02 11:27:30 +0200 | [diff] [blame] | 3217 | const u32 csum_size = fs_info->csum_size; |
Qu Wenruo | 7ffd27e | 2020-12-02 14:47:58 +0800 | [diff] [blame] | 3218 | unsigned int offset_sectors; |
Johannes Thumshirn | d517857 | 2019-06-03 16:58:57 +0200 | [diff] [blame] | 3219 | u8 *csum_expected; |
| 3220 | u8 csum[BTRFS_CSUM_SIZE]; |
Miao Xie | dc380ae | 2014-09-12 18:43:55 +0800 | [diff] [blame] | 3221 | |
Qu Wenruo | 265d4ac | 2020-10-21 14:24:54 +0800 | [diff] [blame] | 3222 | ASSERT(pgoff + len <= PAGE_SIZE); |
| 3223 | |
Qu Wenruo | 7ffd27e | 2020-12-02 14:47:58 +0800 | [diff] [blame] | 3224 | offset_sectors = bio_offset >> fs_info->sectorsize_bits; |
| 3225 | csum_expected = ((u8 *)io_bio->csum) + offset_sectors * csum_size; |
Miao Xie | dc380ae | 2014-09-12 18:43:55 +0800 | [diff] [blame] | 3226 | |
| 3227 | kaddr = kmap_atomic(page); |
Johannes Thumshirn | d517857 | 2019-06-03 16:58:57 +0200 | [diff] [blame] | 3228 | shash->tfm = fs_info->csum_shash; |
| 3229 | |
Eric Biggers | fd08001 | 2020-04-30 23:51:59 -0700 | [diff] [blame] | 3230 | crypto_shash_digest(shash, kaddr + pgoff, len, csum); |
Johannes Thumshirn | d517857 | 2019-06-03 16:58:57 +0200 | [diff] [blame] | 3231 | |
| 3232 | if (memcmp(csum, csum_expected, csum_size)) |
Miao Xie | dc380ae | 2014-09-12 18:43:55 +0800 | [diff] [blame] | 3233 | goto zeroit; |
| 3234 | |
| 3235 | kunmap_atomic(kaddr); |
| 3236 | return 0; |
| 3237 | zeroit: |
Omar Sandoval | c1d6abd | 2021-03-16 12:43:00 -0700 | [diff] [blame] | 3238 | btrfs_print_data_csum_error(BTRFS_I(inode), start, csum, csum_expected, |
| 3239 | io_bio->mirror_num); |
Nikolay Borisov | 814723e | 2020-07-02 15:23:32 +0300 | [diff] [blame] | 3240 | if (io_bio->device) |
| 3241 | btrfs_dev_stat_inc_and_print(io_bio->device, |
| 3242 | BTRFS_DEV_STAT_CORRUPTION_ERRS); |
Miao Xie | dc380ae | 2014-09-12 18:43:55 +0800 | [diff] [blame] | 3243 | memset(kaddr + pgoff, 1, len); |
| 3244 | flush_dcache_page(page); |
| 3245 | kunmap_atomic(kaddr); |
Miao Xie | dc380ae | 2014-09-12 18:43:55 +0800 | [diff] [blame] | 3246 | return -EIO; |
| 3247 | } |
| 3248 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3249 | /* |
Qu Wenruo | 7ffd27e | 2020-12-02 14:47:58 +0800 | [diff] [blame] | 3250 | * 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] | 3251 | * if there's a match, we allow the bio to finish. If not, the code in |
| 3252 | * extent_io.c will try to find good copies for us. |
Qu Wenruo | 7ffd27e | 2020-12-02 14:47:58 +0800 | [diff] [blame] | 3253 | * |
| 3254 | * @bio_offset: offset to the beginning of the bio (in bytes) |
| 3255 | * @start: file offset of the range start |
| 3256 | * @end: file offset of the range end (inclusive) |
Qu Wenruo | 08508fe | 2021-05-03 10:08:54 +0800 | [diff] [blame] | 3257 | * |
| 3258 | * Return a bitmap where bit set means a csum mismatch, and bit not set means |
| 3259 | * csum match. |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3260 | */ |
Qu Wenruo | 08508fe | 2021-05-03 10:08:54 +0800 | [diff] [blame] | 3261 | unsigned int btrfs_verify_data_csum(struct btrfs_io_bio *io_bio, u32 bio_offset, |
| 3262 | struct page *page, u64 start, u64 end) |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 3263 | { |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 3264 | struct inode *inode = page->mapping->host; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3265 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | ff79f81 | 2007-10-15 16:22:25 -0400 | [diff] [blame] | 3266 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Qu Wenruo | f44cf41 | 2020-12-02 14:47:59 +0800 | [diff] [blame] | 3267 | const u32 sectorsize = root->fs_info->sectorsize; |
| 3268 | u32 pg_off; |
Qu Wenruo | 08508fe | 2021-05-03 10:08:54 +0800 | [diff] [blame] | 3269 | unsigned int result = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3270 | |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 3271 | if (PageChecked(page)) { |
| 3272 | ClearPageChecked(page); |
Miao Xie | dc380ae | 2014-09-12 18:43:55 +0800 | [diff] [blame] | 3273 | return 0; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 3274 | } |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 3275 | |
Qu Wenruo | 3670e64 | 2021-07-26 14:34:51 +0800 | [diff] [blame] | 3276 | /* |
| 3277 | * For subpage case, above PageChecked is not safe as it's not subpage |
| 3278 | * compatible. |
| 3279 | * But for now only cow fixup and compressed read utilize PageChecked |
| 3280 | * flag, while in this context we can easily use io_bio->csum to |
| 3281 | * determine if we really need to do csum verification. |
| 3282 | * |
| 3283 | * So for now, just exit if io_bio->csum is NULL, as it means it's |
| 3284 | * compressed read, and its compressed data csum has already been |
| 3285 | * verified. |
| 3286 | */ |
| 3287 | if (io_bio->csum == NULL) |
| 3288 | return 0; |
| 3289 | |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 3290 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM) |
Miao Xie | dc380ae | 2014-09-12 18:43:55 +0800 | [diff] [blame] | 3291 | return 0; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 3292 | |
Josef Bacik | 42437a6 | 2020-10-16 11:29:18 -0400 | [diff] [blame] | 3293 | if (!root->fs_info->csum_root) |
| 3294 | return 0; |
| 3295 | |
Qu Wenruo | f44cf41 | 2020-12-02 14:47:59 +0800 | [diff] [blame] | 3296 | ASSERT(page_offset(page) <= start && |
| 3297 | end <= page_offset(page) + PAGE_SIZE - 1); |
| 3298 | for (pg_off = offset_in_page(start); |
| 3299 | pg_off < offset_in_page(end); |
| 3300 | pg_off += sectorsize, bio_offset += sectorsize) { |
Qu Wenruo | e3c6232 | 2021-07-26 14:35:04 +0800 | [diff] [blame] | 3301 | u64 file_offset = pg_off + page_offset(page); |
Qu Wenruo | f44cf41 | 2020-12-02 14:47:59 +0800 | [diff] [blame] | 3302 | int ret; |
| 3303 | |
Qu Wenruo | e3c6232 | 2021-07-26 14:35:04 +0800 | [diff] [blame] | 3304 | if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID && |
| 3305 | test_range_bit(io_tree, file_offset, |
| 3306 | file_offset + sectorsize - 1, |
| 3307 | EXTENT_NODATASUM, 1, NULL)) { |
| 3308 | /* Skip the range without csum for data reloc inode */ |
| 3309 | clear_extent_bits(io_tree, file_offset, |
| 3310 | file_offset + sectorsize - 1, |
| 3311 | EXTENT_NODATASUM); |
| 3312 | continue; |
| 3313 | } |
Omar Sandoval | c1d6abd | 2021-03-16 12:43:00 -0700 | [diff] [blame] | 3314 | ret = check_data_csum(inode, io_bio, bio_offset, page, pg_off, |
| 3315 | page_offset(page) + pg_off); |
Qu Wenruo | 08508fe | 2021-05-03 10:08:54 +0800 | [diff] [blame] | 3316 | if (ret < 0) { |
| 3317 | const int nr_bit = (pg_off - offset_in_page(start)) >> |
| 3318 | root->fs_info->sectorsize_bits; |
| 3319 | |
| 3320 | result |= (1U << nr_bit); |
| 3321 | } |
Qu Wenruo | f44cf41 | 2020-12-02 14:47:59 +0800 | [diff] [blame] | 3322 | } |
Qu Wenruo | 08508fe | 2021-05-03 10:08:54 +0800 | [diff] [blame] | 3323 | return result; |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 3324 | } |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3325 | |
Nikolay Borisov | c1c3fac | 2018-01-16 09:31:58 +0200 | [diff] [blame] | 3326 | /* |
| 3327 | * btrfs_add_delayed_iput - perform a delayed iput on @inode |
| 3328 | * |
| 3329 | * @inode: The inode we want to perform iput on |
| 3330 | * |
| 3331 | * This function uses the generic vfs_inode::i_count to track whether we should |
| 3332 | * just decrement it (in case it's > 1) or if this is the last iput then link |
| 3333 | * the inode to the delayed iput machinery. Delayed iputs are processed at |
| 3334 | * transaction commit time/superblock commit/cleaner kthread. |
| 3335 | */ |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 3336 | void btrfs_add_delayed_iput(struct inode *inode) |
| 3337 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3338 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
David Sterba | 8089fe6 | 2015-11-19 14:15:51 +0100 | [diff] [blame] | 3339 | struct btrfs_inode *binode = BTRFS_I(inode); |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 3340 | |
| 3341 | if (atomic_add_unless(&inode->i_count, -1, 1)) |
| 3342 | return; |
| 3343 | |
Josef Bacik | 034f784 | 2018-12-03 11:06:52 -0500 | [diff] [blame] | 3344 | atomic_inc(&fs_info->nr_delayed_iputs); |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 3345 | spin_lock(&fs_info->delayed_iput_lock); |
Nikolay Borisov | c1c3fac | 2018-01-16 09:31:58 +0200 | [diff] [blame] | 3346 | ASSERT(list_empty(&binode->delayed_iput)); |
| 3347 | list_add_tail(&binode->delayed_iput, &fs_info->delayed_iputs); |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 3348 | spin_unlock(&fs_info->delayed_iput_lock); |
Josef Bacik | fd340d0 | 2019-01-11 10:21:02 -0500 | [diff] [blame] | 3349 | if (!test_bit(BTRFS_FS_CLEANER_RUNNING, &fs_info->flags)) |
| 3350 | wake_up_process(fs_info->cleaner_kthread); |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 3351 | } |
| 3352 | |
Josef Bacik | 63611e7 | 2019-06-18 10:59:18 -0400 | [diff] [blame] | 3353 | static void run_delayed_iput_locked(struct btrfs_fs_info *fs_info, |
| 3354 | struct btrfs_inode *inode) |
| 3355 | { |
| 3356 | list_del_init(&inode->delayed_iput); |
| 3357 | spin_unlock(&fs_info->delayed_iput_lock); |
| 3358 | iput(&inode->vfs_inode); |
| 3359 | if (atomic_dec_and_test(&fs_info->nr_delayed_iputs)) |
| 3360 | wake_up(&fs_info->delayed_iputs_wait); |
| 3361 | spin_lock(&fs_info->delayed_iput_lock); |
| 3362 | } |
| 3363 | |
| 3364 | static void btrfs_run_delayed_iput(struct btrfs_fs_info *fs_info, |
| 3365 | struct btrfs_inode *inode) |
| 3366 | { |
| 3367 | if (!list_empty(&inode->delayed_iput)) { |
| 3368 | spin_lock(&fs_info->delayed_iput_lock); |
| 3369 | if (!list_empty(&inode->delayed_iput)) |
| 3370 | run_delayed_iput_locked(fs_info, inode); |
| 3371 | spin_unlock(&fs_info->delayed_iput_lock); |
| 3372 | } |
| 3373 | } |
| 3374 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3375 | void btrfs_run_delayed_iputs(struct btrfs_fs_info *fs_info) |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 3376 | { |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 3377 | |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 3378 | spin_lock(&fs_info->delayed_iput_lock); |
David Sterba | 8089fe6 | 2015-11-19 14:15:51 +0100 | [diff] [blame] | 3379 | while (!list_empty(&fs_info->delayed_iputs)) { |
| 3380 | struct btrfs_inode *inode; |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 3381 | |
David Sterba | 8089fe6 | 2015-11-19 14:15:51 +0100 | [diff] [blame] | 3382 | inode = list_first_entry(&fs_info->delayed_iputs, |
| 3383 | struct btrfs_inode, delayed_iput); |
Josef Bacik | 63611e7 | 2019-06-18 10:59:18 -0400 | [diff] [blame] | 3384 | run_delayed_iput_locked(fs_info, inode); |
Josef Bacik | 71795ee | 2021-04-29 10:51:34 -0400 | [diff] [blame] | 3385 | cond_resched_lock(&fs_info->delayed_iput_lock); |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 3386 | } |
David Sterba | 8089fe6 | 2015-11-19 14:15:51 +0100 | [diff] [blame] | 3387 | spin_unlock(&fs_info->delayed_iput_lock); |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 3388 | } |
| 3389 | |
Josef Bacik | 034f784 | 2018-12-03 11:06:52 -0500 | [diff] [blame] | 3390 | /** |
Nikolay Borisov | 2639631 | 2021-01-22 11:57:59 +0200 | [diff] [blame] | 3391 | * Wait for flushing all delayed iputs |
| 3392 | * |
| 3393 | * @fs_info: the filesystem |
Josef Bacik | 034f784 | 2018-12-03 11:06:52 -0500 | [diff] [blame] | 3394 | * |
| 3395 | * This will wait on any delayed iputs that are currently running with KILLABLE |
| 3396 | * set. Once they are all done running we will return, unless we are killed in |
| 3397 | * which case we return EINTR. This helps in user operations like fallocate etc |
| 3398 | * that might get blocked on the iputs. |
Nikolay Borisov | 2639631 | 2021-01-22 11:57:59 +0200 | [diff] [blame] | 3399 | * |
| 3400 | * Return EINTR if we were killed, 0 if nothing's pending |
Josef Bacik | 034f784 | 2018-12-03 11:06:52 -0500 | [diff] [blame] | 3401 | */ |
| 3402 | int btrfs_wait_on_delayed_iputs(struct btrfs_fs_info *fs_info) |
| 3403 | { |
| 3404 | int ret = wait_event_killable(fs_info->delayed_iputs_wait, |
| 3405 | atomic_read(&fs_info->nr_delayed_iputs) == 0); |
| 3406 | if (ret) |
| 3407 | return -EINTR; |
| 3408 | return 0; |
| 3409 | } |
| 3410 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3411 | /* |
Omar Sandoval | f7e9e8f | 2018-05-11 13:13:32 -0700 | [diff] [blame] | 3412 | * This creates an orphan entry for the given inode in case something goes wrong |
| 3413 | * in the middle of an unlink. |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3414 | */ |
Nikolay Borisov | 73f2e54 | 2017-02-20 13:50:59 +0200 | [diff] [blame] | 3415 | int btrfs_orphan_add(struct btrfs_trans_handle *trans, |
Omar Sandoval | 2791906 | 2018-05-11 13:13:37 -0700 | [diff] [blame] | 3416 | struct btrfs_inode *inode) |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3417 | { |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3418 | int ret; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3419 | |
Omar Sandoval | 2791906 | 2018-05-11 13:13:37 -0700 | [diff] [blame] | 3420 | ret = btrfs_insert_orphan_item(trans, inode->root, btrfs_ino(inode)); |
| 3421 | if (ret && ret != -EEXIST) { |
| 3422 | btrfs_abort_transaction(trans, ret); |
| 3423 | return ret; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3424 | } |
| 3425 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3426 | return 0; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3427 | } |
| 3428 | |
| 3429 | /* |
Omar Sandoval | f7e9e8f | 2018-05-11 13:13:32 -0700 | [diff] [blame] | 3430 | * We have done the delete so we can go ahead and remove the orphan item for |
| 3431 | * this particular inode. |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3432 | */ |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 3433 | static int btrfs_orphan_del(struct btrfs_trans_handle *trans, |
Nikolay Borisov | 3d6ae7b | 2017-02-20 13:50:58 +0200 | [diff] [blame] | 3434 | struct btrfs_inode *inode) |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3435 | { |
Omar Sandoval | 2791906 | 2018-05-11 13:13:37 -0700 | [diff] [blame] | 3436 | return btrfs_del_orphan_item(trans, inode->root, btrfs_ino(inode)); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3437 | } |
| 3438 | |
| 3439 | /* |
| 3440 | * this cleans up any orphans that may be left on the list from the last use |
| 3441 | * of this root. |
| 3442 | */ |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3443 | int btrfs_orphan_cleanup(struct btrfs_root *root) |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3444 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3445 | struct btrfs_fs_info *fs_info = root->fs_info; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3446 | struct btrfs_path *path; |
| 3447 | struct extent_buffer *leaf; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3448 | struct btrfs_key key, found_key; |
| 3449 | struct btrfs_trans_handle *trans; |
| 3450 | struct inode *inode; |
Josef Bacik | 8f6d7f4 | 2011-09-26 15:55:20 -0400 | [diff] [blame] | 3451 | u64 last_objectid = 0; |
Omar Sandoval | f7e9e8f | 2018-05-11 13:13:32 -0700 | [diff] [blame] | 3452 | int ret = 0, nr_unlink = 0; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3453 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3454 | if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED)) |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3455 | return 0; |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 3456 | |
| 3457 | path = btrfs_alloc_path(); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3458 | if (!path) { |
| 3459 | ret = -ENOMEM; |
| 3460 | goto out; |
| 3461 | } |
David Sterba | e4058b5 | 2015-11-27 16:31:35 +0100 | [diff] [blame] | 3462 | path->reada = READA_BACK; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3463 | |
| 3464 | key.objectid = BTRFS_ORPHAN_OBJECTID; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 3465 | key.type = BTRFS_ORPHAN_ITEM_KEY; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3466 | key.offset = (u64)-1; |
| 3467 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3468 | while (1) { |
| 3469 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3470 | if (ret < 0) |
| 3471 | goto out; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3472 | |
| 3473 | /* |
| 3474 | * 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] | 3475 | * 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] | 3476 | * find the key and see if we have stuff that matches |
| 3477 | */ |
| 3478 | if (ret > 0) { |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3479 | ret = 0; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3480 | if (path->slots[0] == 0) |
| 3481 | break; |
| 3482 | path->slots[0]--; |
| 3483 | } |
| 3484 | |
| 3485 | /* pull out the item */ |
| 3486 | leaf = path->nodes[0]; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3487 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 3488 | |
| 3489 | /* make sure the item matches what we want */ |
| 3490 | if (found_key.objectid != BTRFS_ORPHAN_OBJECTID) |
| 3491 | break; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 3492 | if (found_key.type != BTRFS_ORPHAN_ITEM_KEY) |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3493 | break; |
| 3494 | |
| 3495 | /* release the path since we're done with it */ |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3496 | btrfs_release_path(path); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3497 | |
| 3498 | /* |
| 3499 | * this is where we are basically btrfs_lookup, without the |
| 3500 | * crossing root thing. we store the inode number in the |
| 3501 | * offset of the orphan item. |
| 3502 | */ |
Josef Bacik | 8f6d7f4 | 2011-09-26 15:55:20 -0400 | [diff] [blame] | 3503 | |
| 3504 | if (found_key.offset == last_objectid) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3505 | btrfs_err(fs_info, |
| 3506 | "Error removing orphan entry, stopping orphan cleanup"); |
Josef Bacik | 8f6d7f4 | 2011-09-26 15:55:20 -0400 | [diff] [blame] | 3507 | ret = -EINVAL; |
| 3508 | goto out; |
| 3509 | } |
| 3510 | |
| 3511 | last_objectid = found_key.offset; |
| 3512 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3513 | found_key.objectid = found_key.offset; |
| 3514 | found_key.type = BTRFS_INODE_ITEM_KEY; |
| 3515 | found_key.offset = 0; |
David Sterba | 0202e83 | 2020-05-15 19:35:59 +0200 | [diff] [blame] | 3516 | inode = btrfs_iget(fs_info->sb, last_objectid, root); |
Rusty Russell | 8c6ffba | 2013-07-15 11:20:32 +0930 | [diff] [blame] | 3517 | ret = PTR_ERR_OR_ZERO(inode); |
Filipe Manana | 6771089 | 2016-06-06 11:51:25 +0100 | [diff] [blame] | 3518 | if (ret && ret != -ENOENT) |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3519 | goto out; |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 3520 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3521 | if (ret == -ENOENT && root == fs_info->tree_root) { |
Arne Jansen | f8e9e0b | 2011-12-14 20:12:02 -0500 | [diff] [blame] | 3522 | struct btrfs_root *dead_root; |
Arne Jansen | f8e9e0b | 2011-12-14 20:12:02 -0500 | [diff] [blame] | 3523 | int is_dead_root = 0; |
| 3524 | |
| 3525 | /* |
Filipe Manana | 0c0218e | 2021-03-16 16:54:13 +0000 | [diff] [blame] | 3526 | * This is an orphan in the tree root. Currently these |
Arne Jansen | f8e9e0b | 2011-12-14 20:12:02 -0500 | [diff] [blame] | 3527 | * could come from 2 sources: |
Filipe Manana | 0c0218e | 2021-03-16 16:54:13 +0000 | [diff] [blame] | 3528 | * a) a root (snapshot/subvolume) deletion in progress |
Arne Jansen | f8e9e0b | 2011-12-14 20:12:02 -0500 | [diff] [blame] | 3529 | * b) a free space cache inode |
Filipe Manana | 0c0218e | 2021-03-16 16:54:13 +0000 | [diff] [blame] | 3530 | * We need to distinguish those two, as the orphan item |
| 3531 | * for a root must not get deleted before the deletion |
| 3532 | * of the snapshot/subvolume's tree completes. |
| 3533 | * |
| 3534 | * btrfs_find_orphan_roots() ran before us, which has |
| 3535 | * found all deleted roots and loaded them into |
| 3536 | * fs_info->fs_roots_radix. So here we can find if an |
| 3537 | * orphan item corresponds to a deleted root by looking |
| 3538 | * up the root from that radix tree. |
Arne Jansen | f8e9e0b | 2011-12-14 20:12:02 -0500 | [diff] [blame] | 3539 | */ |
Robbie Ko | a619b3c | 2020-05-07 10:54:40 +0800 | [diff] [blame] | 3540 | |
| 3541 | spin_lock(&fs_info->fs_roots_radix_lock); |
| 3542 | dead_root = radix_tree_lookup(&fs_info->fs_roots_radix, |
| 3543 | (unsigned long)found_key.objectid); |
| 3544 | if (dead_root && btrfs_root_refs(&dead_root->root_item) == 0) |
| 3545 | is_dead_root = 1; |
| 3546 | spin_unlock(&fs_info->fs_roots_radix_lock); |
| 3547 | |
Arne Jansen | f8e9e0b | 2011-12-14 20:12:02 -0500 | [diff] [blame] | 3548 | if (is_dead_root) { |
| 3549 | /* prevent this orphan from being found again */ |
| 3550 | key.offset = found_key.objectid - 1; |
| 3551 | continue; |
| 3552 | } |
Omar Sandoval | f7e9e8f | 2018-05-11 13:13:32 -0700 | [diff] [blame] | 3553 | |
Arne Jansen | f8e9e0b | 2011-12-14 20:12:02 -0500 | [diff] [blame] | 3554 | } |
Omar Sandoval | f7e9e8f | 2018-05-11 13:13:32 -0700 | [diff] [blame] | 3555 | |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 3556 | /* |
Omar Sandoval | f7e9e8f | 2018-05-11 13:13:32 -0700 | [diff] [blame] | 3557 | * If we have an inode with links, there are a couple of |
| 3558 | * possibilities. Old kernels (before v3.12) used to create an |
| 3559 | * orphan item for truncate indicating that there were possibly |
| 3560 | * extent items past i_size that needed to be deleted. In v3.12, |
| 3561 | * truncate was changed to update i_size in sync with the extent |
| 3562 | * items, but the (useless) orphan item was still created. Since |
| 3563 | * v4.18, we don't create the orphan item for truncate at all. |
| 3564 | * |
| 3565 | * So, this item could mean that we need to do a truncate, but |
| 3566 | * only if this filesystem was last used on a pre-v3.12 kernel |
| 3567 | * and was not cleanly unmounted. The odds of that are quite |
| 3568 | * slim, and it's a pain to do the truncate now, so just delete |
| 3569 | * the orphan item. |
| 3570 | * |
| 3571 | * It's also possible that this orphan item was supposed to be |
| 3572 | * deleted but wasn't. The inode number may have been reused, |
| 3573 | * but either way, we can delete the orphan item. |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 3574 | */ |
Omar Sandoval | f7e9e8f | 2018-05-11 13:13:32 -0700 | [diff] [blame] | 3575 | if (ret == -ENOENT || inode->i_nlink) { |
| 3576 | if (!ret) |
| 3577 | iput(inode); |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 3578 | trans = btrfs_start_transaction(root, 1); |
| 3579 | if (IS_ERR(trans)) { |
| 3580 | ret = PTR_ERR(trans); |
| 3581 | goto out; |
| 3582 | } |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3583 | btrfs_debug(fs_info, "auto deleting %Lu", |
| 3584 | found_key.objectid); |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 3585 | ret = btrfs_del_orphan_item(trans, root, |
| 3586 | found_key.objectid); |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 3587 | btrfs_end_transaction(trans); |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 3588 | if (ret) |
| 3589 | goto out; |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 3590 | continue; |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3591 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3592 | |
Omar Sandoval | f7e9e8f | 2018-05-11 13:13:32 -0700 | [diff] [blame] | 3593 | nr_unlink++; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3594 | |
| 3595 | /* this will do delete_inode and everything for us */ |
| 3596 | iput(inode); |
| 3597 | } |
Miao Xie | 3254c87 | 2011-11-10 20:45:05 -0500 | [diff] [blame] | 3598 | /* release the path since we're done with it */ |
| 3599 | btrfs_release_path(path); |
| 3600 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3601 | root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE; |
| 3602 | |
Omar Sandoval | a575cee | 2018-05-11 13:13:38 -0700 | [diff] [blame] | 3603 | if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) { |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 3604 | trans = btrfs_join_transaction(root); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3605 | if (!IS_ERR(trans)) |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 3606 | btrfs_end_transaction(trans); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3607 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3608 | |
| 3609 | if (nr_unlink) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3610 | btrfs_debug(fs_info, "unlinked %d orphans", nr_unlink); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3611 | |
| 3612 | out: |
| 3613 | if (ret) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3614 | btrfs_err(fs_info, "could not do orphan cleanup %d", ret); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3615 | btrfs_free_path(path); |
| 3616 | return ret; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3617 | } |
| 3618 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3619 | /* |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3620 | * very simple check to peek ahead in the leaf looking for xattrs. If we |
| 3621 | * don't find any xattrs, we know there can't be any acls. |
| 3622 | * |
| 3623 | * slot is the slot the inode is in, objectid is the objectid of the inode |
| 3624 | */ |
| 3625 | 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] | 3626 | int slot, u64 objectid, |
| 3627 | int *first_xattr_slot) |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3628 | { |
| 3629 | u32 nritems = btrfs_header_nritems(leaf); |
| 3630 | struct btrfs_key found_key; |
Josef Bacik | f23b5a5 | 2013-06-19 10:16:26 -0400 | [diff] [blame] | 3631 | static u64 xattr_access = 0; |
| 3632 | static u64 xattr_default = 0; |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3633 | int scanned = 0; |
| 3634 | |
Josef Bacik | f23b5a5 | 2013-06-19 10:16:26 -0400 | [diff] [blame] | 3635 | if (!xattr_access) { |
Andreas Gruenbacher | 97d7929 | 2015-12-02 14:44:35 +0100 | [diff] [blame] | 3636 | xattr_access = btrfs_name_hash(XATTR_NAME_POSIX_ACL_ACCESS, |
| 3637 | strlen(XATTR_NAME_POSIX_ACL_ACCESS)); |
| 3638 | xattr_default = btrfs_name_hash(XATTR_NAME_POSIX_ACL_DEFAULT, |
| 3639 | strlen(XATTR_NAME_POSIX_ACL_DEFAULT)); |
Josef Bacik | f23b5a5 | 2013-06-19 10:16:26 -0400 | [diff] [blame] | 3640 | } |
| 3641 | |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3642 | slot++; |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 3643 | *first_xattr_slot = -1; |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3644 | while (slot < nritems) { |
| 3645 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
| 3646 | |
| 3647 | /* we found a different objectid, there must not be acls */ |
| 3648 | if (found_key.objectid != objectid) |
| 3649 | return 0; |
| 3650 | |
| 3651 | /* we found an xattr, assume we've got an acl */ |
Josef Bacik | f23b5a5 | 2013-06-19 10:16:26 -0400 | [diff] [blame] | 3652 | if (found_key.type == BTRFS_XATTR_ITEM_KEY) { |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 3653 | if (*first_xattr_slot == -1) |
| 3654 | *first_xattr_slot = slot; |
Josef Bacik | f23b5a5 | 2013-06-19 10:16:26 -0400 | [diff] [blame] | 3655 | if (found_key.offset == xattr_access || |
| 3656 | found_key.offset == xattr_default) |
| 3657 | return 1; |
| 3658 | } |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3659 | |
| 3660 | /* |
| 3661 | * we found a key greater than an xattr key, there can't |
| 3662 | * be any acls later on |
| 3663 | */ |
| 3664 | if (found_key.type > BTRFS_XATTR_ITEM_KEY) |
| 3665 | return 0; |
| 3666 | |
| 3667 | slot++; |
| 3668 | scanned++; |
| 3669 | |
| 3670 | /* |
| 3671 | * it goes inode, inode backrefs, xattrs, extents, |
| 3672 | * so if there are a ton of hard links to an inode there can |
| 3673 | * be a lot of backrefs. Don't waste time searching too hard, |
| 3674 | * this is just an optimization |
| 3675 | */ |
| 3676 | if (scanned >= 8) |
| 3677 | break; |
| 3678 | } |
| 3679 | /* we hit the end of the leaf before we found an xattr or |
| 3680 | * something larger than an xattr. We have to assume the inode |
| 3681 | * has acls |
| 3682 | */ |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 3683 | if (*first_xattr_slot == -1) |
| 3684 | *first_xattr_slot = slot; |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3685 | return 1; |
| 3686 | } |
| 3687 | |
| 3688 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3689 | * read an inode from the btree into the in-memory inode |
| 3690 | */ |
Filipe Manana | 4222ea7 | 2018-10-24 10:13:03 +0100 | [diff] [blame] | 3691 | static int btrfs_read_locked_inode(struct inode *inode, |
| 3692 | struct btrfs_path *in_path) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3693 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3694 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Filipe Manana | 4222ea7 | 2018-10-24 10:13:03 +0100 | [diff] [blame] | 3695 | struct btrfs_path *path = in_path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3696 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3697 | struct btrfs_inode_item *inode_item; |
| 3698 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 3699 | struct btrfs_key location; |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 3700 | unsigned long ptr; |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3701 | int maybe_acls; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 3702 | u32 rdev; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3703 | int ret; |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 3704 | bool filled = false; |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 3705 | int first_xattr_slot; |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 3706 | |
| 3707 | ret = btrfs_fill_inode(inode, &rdev); |
| 3708 | if (!ret) |
| 3709 | filled = true; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3710 | |
Filipe Manana | 4222ea7 | 2018-10-24 10:13:03 +0100 | [diff] [blame] | 3711 | if (!path) { |
| 3712 | path = btrfs_alloc_path(); |
| 3713 | if (!path) |
| 3714 | return -ENOMEM; |
| 3715 | } |
Mark Fasheh | 1748f84 | 2011-07-12 11:25:31 -0700 | [diff] [blame] | 3716 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3717 | memcpy(&location, &BTRFS_I(inode)->location, sizeof(location)); |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 3718 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3719 | ret = btrfs_lookup_inode(NULL, root, path, &location, 0); |
Filipe Manana | 6771089 | 2016-06-06 11:51:25 +0100 | [diff] [blame] | 3720 | if (ret) { |
Filipe Manana | 4222ea7 | 2018-10-24 10:13:03 +0100 | [diff] [blame] | 3721 | if (path != in_path) |
| 3722 | btrfs_free_path(path); |
Al Viro | f5b3a41 | 2018-07-29 23:04:51 +0100 | [diff] [blame] | 3723 | return ret; |
Filipe Manana | 6771089 | 2016-06-06 11:51:25 +0100 | [diff] [blame] | 3724 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3725 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3726 | leaf = path->nodes[0]; |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 3727 | |
| 3728 | if (filled) |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 3729 | goto cache_index; |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 3730 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3731 | inode_item = btrfs_item_ptr(leaf, path->slots[0], |
| 3732 | struct btrfs_inode_item); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3733 | inode->i_mode = btrfs_inode_mode(leaf, inode_item); |
Miklos Szeredi | bfe8684 | 2011-10-28 14:13:29 +0200 | [diff] [blame] | 3734 | set_nlink(inode, btrfs_inode_nlink(leaf, inode_item)); |
Eric W. Biederman | 2f2f43d | 2012-02-10 11:05:07 -0800 | [diff] [blame] | 3735 | i_uid_write(inode, btrfs_inode_uid(leaf, inode_item)); |
| 3736 | i_gid_write(inode, btrfs_inode_gid(leaf, inode_item)); |
Nikolay Borisov | 6ef06d2 | 2017-02-20 13:50:34 +0200 | [diff] [blame] | 3737 | 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] | 3738 | btrfs_inode_set_file_extent_range(BTRFS_I(inode), 0, |
| 3739 | round_up(i_size_read(inode), fs_info->sectorsize)); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3740 | |
David Sterba | a937b97 | 2014-12-12 17:39:12 +0100 | [diff] [blame] | 3741 | inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->atime); |
| 3742 | inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->atime); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3743 | |
David Sterba | a937b97 | 2014-12-12 17:39:12 +0100 | [diff] [blame] | 3744 | inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->mtime); |
| 3745 | inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->mtime); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3746 | |
David Sterba | a937b97 | 2014-12-12 17:39:12 +0100 | [diff] [blame] | 3747 | inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->ctime); |
| 3748 | inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->ctime); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3749 | |
chandan r | 9cc97d6 | 2012-07-04 12:48:07 +0530 | [diff] [blame] | 3750 | BTRFS_I(inode)->i_otime.tv_sec = |
| 3751 | btrfs_timespec_sec(leaf, &inode_item->otime); |
| 3752 | BTRFS_I(inode)->i_otime.tv_nsec = |
| 3753 | btrfs_timespec_nsec(leaf, &inode_item->otime); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3754 | |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 3755 | inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item)); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3756 | BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item); |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 3757 | BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item); |
| 3758 | |
Jeff Layton | c7f88c4 | 2017-12-11 06:35:12 -0500 | [diff] [blame] | 3759 | inode_set_iversion_queried(inode, |
| 3760 | btrfs_inode_sequence(leaf, inode_item)); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3761 | inode->i_generation = BTRFS_I(inode)->generation; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 3762 | inode->i_rdev = 0; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3763 | rdev = btrfs_inode_rdev(leaf, inode_item); |
| 3764 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 3765 | BTRFS_I(inode)->index_cnt = (u64)-1; |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 3766 | BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item); |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 3767 | |
| 3768 | cache_index: |
Yang Dongsheng | 6e17d30 | 2015-04-09 12:08:43 +0800 | [diff] [blame] | 3769 | /* |
| 3770 | * If we were modified in the current generation and evicted from memory |
| 3771 | * and then re-read we need to do a full sync since we don't have any |
| 3772 | * idea about which extents were modified before we were evicted from |
| 3773 | * cache. |
| 3774 | * |
| 3775 | * This is required for both inode re-read from disk and delayed inode |
| 3776 | * in delayed_nodes_tree. |
| 3777 | */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3778 | if (BTRFS_I(inode)->last_trans == fs_info->generation) |
Yang Dongsheng | 6e17d30 | 2015-04-09 12:08:43 +0800 | [diff] [blame] | 3779 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, |
| 3780 | &BTRFS_I(inode)->runtime_flags); |
| 3781 | |
Filipe Manana | bde6c24 | 2015-07-24 00:00:19 +0100 | [diff] [blame] | 3782 | /* |
| 3783 | * We don't persist the id of the transaction where an unlink operation |
| 3784 | * against the inode was last made. So here we assume the inode might |
| 3785 | * have been evicted, and therefore the exact value of last_unlink_trans |
| 3786 | * lost, and set it to last_trans to avoid metadata inconsistencies |
| 3787 | * between the inode and its parent if the inode is fsync'ed and the log |
| 3788 | * replayed. For example, in the scenario: |
| 3789 | * |
| 3790 | * touch mydir/foo |
| 3791 | * ln mydir/foo mydir/bar |
| 3792 | * sync |
| 3793 | * unlink mydir/bar |
| 3794 | * echo 2 > /proc/sys/vm/drop_caches # evicts inode |
| 3795 | * xfs_io -c fsync mydir/foo |
| 3796 | * <power failure> |
| 3797 | * mount fs, triggers fsync log replay |
| 3798 | * |
| 3799 | * We must make sure that when we fsync our inode foo we also log its |
| 3800 | * parent inode, otherwise after log replay the parent still has the |
| 3801 | * dentry with the "bar" name but our inode foo has a link count of 1 |
| 3802 | * and doesn't have an inode ref with the name "bar" anymore. |
| 3803 | * |
| 3804 | * Setting last_unlink_trans to last_trans is a pessimistic approach, |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 3805 | * but it guarantees correctness at the expense of occasional full |
Filipe Manana | bde6c24 | 2015-07-24 00:00:19 +0100 | [diff] [blame] | 3806 | * transaction commits on fsync if our inode is a directory, or if our |
| 3807 | * inode is not a directory, logging its parent unnecessarily. |
| 3808 | */ |
| 3809 | BTRFS_I(inode)->last_unlink_trans = BTRFS_I(inode)->last_trans; |
| 3810 | |
Filipe Manana | 3ebac17 | 2020-07-15 12:30:43 +0100 | [diff] [blame] | 3811 | /* |
| 3812 | * Same logic as for last_unlink_trans. We don't persist the generation |
| 3813 | * of the last transaction where this inode was used for a reflink |
| 3814 | * operation, so after eviction and reloading the inode we must be |
| 3815 | * pessimistic and assume the last transaction that modified the inode. |
| 3816 | */ |
| 3817 | BTRFS_I(inode)->last_reflink_trans = BTRFS_I(inode)->last_trans; |
| 3818 | |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 3819 | path->slots[0]++; |
| 3820 | if (inode->i_nlink != 1 || |
| 3821 | path->slots[0] >= btrfs_header_nritems(leaf)) |
| 3822 | goto cache_acl; |
| 3823 | |
| 3824 | btrfs_item_key_to_cpu(leaf, &location, path->slots[0]); |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 3825 | if (location.objectid != btrfs_ino(BTRFS_I(inode))) |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 3826 | goto cache_acl; |
| 3827 | |
| 3828 | ptr = btrfs_item_ptr_offset(leaf, path->slots[0]); |
| 3829 | if (location.type == BTRFS_INODE_REF_KEY) { |
| 3830 | struct btrfs_inode_ref *ref; |
| 3831 | |
| 3832 | ref = (struct btrfs_inode_ref *)ptr; |
| 3833 | BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref); |
| 3834 | } else if (location.type == BTRFS_INODE_EXTREF_KEY) { |
| 3835 | struct btrfs_inode_extref *extref; |
| 3836 | |
| 3837 | extref = (struct btrfs_inode_extref *)ptr; |
| 3838 | BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf, |
| 3839 | extref); |
| 3840 | } |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 3841 | cache_acl: |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3842 | /* |
| 3843 | * try to precache a NULL acl entry for files that don't have |
| 3844 | * any xattrs or acls |
| 3845 | */ |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3846 | maybe_acls = acls_after_inode_item(leaf, path->slots[0], |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 3847 | btrfs_ino(BTRFS_I(inode)), &first_xattr_slot); |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 3848 | if (first_xattr_slot != -1) { |
| 3849 | path->slots[0] = first_xattr_slot; |
| 3850 | ret = btrfs_load_inode_props(inode, path); |
| 3851 | if (ret) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3852 | btrfs_err(fs_info, |
David Sterba | 351fd35 | 2014-05-15 16:48:20 +0200 | [diff] [blame] | 3853 | "error loading props for ino %llu (root %llu): %d", |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 3854 | btrfs_ino(BTRFS_I(inode)), |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 3855 | root->root_key.objectid, ret); |
| 3856 | } |
Filipe Manana | 4222ea7 | 2018-10-24 10:13:03 +0100 | [diff] [blame] | 3857 | if (path != in_path) |
| 3858 | btrfs_free_path(path); |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 3859 | |
Al Viro | 72c0490 | 2009-06-24 16:58:48 -0400 | [diff] [blame] | 3860 | if (!maybe_acls) |
| 3861 | cache_no_acl(inode); |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3862 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3863 | switch (inode->i_mode & S_IFMT) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3864 | case S_IFREG: |
| 3865 | inode->i_mapping->a_ops = &btrfs_aops; |
| 3866 | inode->i_fop = &btrfs_file_operations; |
| 3867 | inode->i_op = &btrfs_file_inode_operations; |
| 3868 | break; |
| 3869 | case S_IFDIR: |
| 3870 | inode->i_fop = &btrfs_dir_file_operations; |
Omar Sandoval | 67ade05 | 2017-01-25 17:06:38 -0800 | [diff] [blame] | 3871 | inode->i_op = &btrfs_dir_inode_operations; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3872 | break; |
| 3873 | case S_IFLNK: |
| 3874 | inode->i_op = &btrfs_symlink_inode_operations; |
Al Viro | 21fc61c | 2015-11-17 01:07:57 -0500 | [diff] [blame] | 3875 | inode_nohighmem(inode); |
Omar Sandoval | 4779cc0 | 2018-09-24 15:16:55 -0700 | [diff] [blame] | 3876 | inode->i_mapping->a_ops = &btrfs_aops; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3877 | break; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 3878 | default: |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 3879 | inode->i_op = &btrfs_special_inode_operations; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 3880 | init_special_inode(inode, inode->i_mode, rdev); |
| 3881 | break; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3882 | } |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 3883 | |
David Sterba | 7b6a221 | 2018-03-26 18:40:21 +0200 | [diff] [blame] | 3884 | btrfs_sync_inode_flags_to_i_flags(inode); |
Filipe Manana | 6771089 | 2016-06-06 11:51:25 +0100 | [diff] [blame] | 3885 | return 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3886 | } |
| 3887 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3888 | /* |
| 3889 | * given a leaf and an inode, copy the inode fields into the leaf |
| 3890 | */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3891 | static void fill_inode_item(struct btrfs_trans_handle *trans, |
| 3892 | struct extent_buffer *leaf, |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3893 | struct btrfs_inode_item *item, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3894 | struct inode *inode) |
| 3895 | { |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3896 | struct btrfs_map_token token; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3897 | |
David Sterba | c82f823 | 2019-08-09 17:48:21 +0200 | [diff] [blame] | 3898 | btrfs_init_map_token(&token, leaf); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3899 | |
David Sterba | cc4c13d | 2020-04-29 02:15:56 +0200 | [diff] [blame] | 3900 | btrfs_set_token_inode_uid(&token, item, i_uid_read(inode)); |
| 3901 | btrfs_set_token_inode_gid(&token, item, i_gid_read(inode)); |
| 3902 | btrfs_set_token_inode_size(&token, item, BTRFS_I(inode)->disk_i_size); |
| 3903 | btrfs_set_token_inode_mode(&token, item, inode->i_mode); |
| 3904 | btrfs_set_token_inode_nlink(&token, item, inode->i_nlink); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3905 | |
David Sterba | cc4c13d | 2020-04-29 02:15:56 +0200 | [diff] [blame] | 3906 | btrfs_set_token_timespec_sec(&token, &item->atime, |
| 3907 | inode->i_atime.tv_sec); |
| 3908 | btrfs_set_token_timespec_nsec(&token, &item->atime, |
| 3909 | inode->i_atime.tv_nsec); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3910 | |
David Sterba | cc4c13d | 2020-04-29 02:15:56 +0200 | [diff] [blame] | 3911 | btrfs_set_token_timespec_sec(&token, &item->mtime, |
| 3912 | inode->i_mtime.tv_sec); |
| 3913 | btrfs_set_token_timespec_nsec(&token, &item->mtime, |
| 3914 | inode->i_mtime.tv_nsec); |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3915 | |
David Sterba | cc4c13d | 2020-04-29 02:15:56 +0200 | [diff] [blame] | 3916 | btrfs_set_token_timespec_sec(&token, &item->ctime, |
| 3917 | inode->i_ctime.tv_sec); |
| 3918 | btrfs_set_token_timespec_nsec(&token, &item->ctime, |
| 3919 | inode->i_ctime.tv_nsec); |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3920 | |
David Sterba | cc4c13d | 2020-04-29 02:15:56 +0200 | [diff] [blame] | 3921 | btrfs_set_token_timespec_sec(&token, &item->otime, |
| 3922 | BTRFS_I(inode)->i_otime.tv_sec); |
| 3923 | btrfs_set_token_timespec_nsec(&token, &item->otime, |
| 3924 | BTRFS_I(inode)->i_otime.tv_nsec); |
chandan r | 9cc97d6 | 2012-07-04 12:48:07 +0530 | [diff] [blame] | 3925 | |
David Sterba | cc4c13d | 2020-04-29 02:15:56 +0200 | [diff] [blame] | 3926 | btrfs_set_token_inode_nbytes(&token, item, inode_get_bytes(inode)); |
| 3927 | btrfs_set_token_inode_generation(&token, item, |
| 3928 | BTRFS_I(inode)->generation); |
| 3929 | btrfs_set_token_inode_sequence(&token, item, inode_peek_iversion(inode)); |
| 3930 | btrfs_set_token_inode_transid(&token, item, trans->transid); |
| 3931 | btrfs_set_token_inode_rdev(&token, item, inode->i_rdev); |
| 3932 | btrfs_set_token_inode_flags(&token, item, BTRFS_I(inode)->flags); |
| 3933 | btrfs_set_token_inode_block_group(&token, item, 0); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3934 | } |
| 3935 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3936 | /* |
| 3937 | * copy everything in the in-memory inode into the btree. |
| 3938 | */ |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 3939 | static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans, |
Nikolay Borisov | dfeb9e7 | 2020-11-02 16:48:58 +0200 | [diff] [blame] | 3940 | struct btrfs_root *root, |
| 3941 | struct btrfs_inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3942 | { |
| 3943 | struct btrfs_inode_item *inode_item; |
| 3944 | struct btrfs_path *path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3945 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3946 | int ret; |
| 3947 | |
| 3948 | path = btrfs_alloc_path(); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 3949 | if (!path) |
| 3950 | return -ENOMEM; |
| 3951 | |
Nikolay Borisov | dfeb9e7 | 2020-11-02 16:48:58 +0200 | [diff] [blame] | 3952 | ret = btrfs_lookup_inode(trans, root, path, &inode->location, 1); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3953 | if (ret) { |
| 3954 | if (ret > 0) |
| 3955 | ret = -ENOENT; |
| 3956 | goto failed; |
| 3957 | } |
| 3958 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3959 | leaf = path->nodes[0]; |
| 3960 | inode_item = btrfs_item_ptr(leaf, path->slots[0], |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 3961 | struct btrfs_inode_item); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3962 | |
Nikolay Borisov | dfeb9e7 | 2020-11-02 16:48:58 +0200 | [diff] [blame] | 3963 | fill_inode_item(trans, leaf, inode_item, &inode->vfs_inode); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3964 | btrfs_mark_buffer_dirty(leaf); |
Nikolay Borisov | dfeb9e7 | 2020-11-02 16:48:58 +0200 | [diff] [blame] | 3965 | btrfs_set_inode_last_trans(trans, inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3966 | ret = 0; |
| 3967 | failed: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3968 | btrfs_free_path(path); |
| 3969 | return ret; |
| 3970 | } |
| 3971 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3972 | /* |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 3973 | * copy everything in the in-memory inode into the btree. |
| 3974 | */ |
| 3975 | noinline int btrfs_update_inode(struct btrfs_trans_handle *trans, |
Nikolay Borisov | 9a56fcd | 2020-11-02 16:48:59 +0200 | [diff] [blame] | 3976 | struct btrfs_root *root, |
| 3977 | struct btrfs_inode *inode) |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 3978 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3979 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 3980 | int ret; |
| 3981 | |
| 3982 | /* |
| 3983 | * If the inode is a free space inode, we can deadlock during commit |
| 3984 | * if we put it into the delayed code. |
| 3985 | * |
| 3986 | * The data relocation inode should also be directly updated |
| 3987 | * without delay |
| 3988 | */ |
Nikolay Borisov | 9a56fcd | 2020-11-02 16:48:59 +0200 | [diff] [blame] | 3989 | if (!btrfs_is_free_space_inode(inode) |
Josef Bacik | 1d52c78 | 2014-09-18 11:30:44 -0400 | [diff] [blame] | 3990 | && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3991 | && !test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) { |
Alexander Block | 8ea05e3 | 2012-07-25 17:35:53 +0200 | [diff] [blame] | 3992 | btrfs_update_root_times(trans, root); |
| 3993 | |
Nikolay Borisov | 9a56fcd | 2020-11-02 16:48:59 +0200 | [diff] [blame] | 3994 | ret = btrfs_delayed_update_inode(trans, root, inode); |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 3995 | if (!ret) |
Nikolay Borisov | 9a56fcd | 2020-11-02 16:48:59 +0200 | [diff] [blame] | 3996 | btrfs_set_inode_last_trans(trans, inode); |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 3997 | return ret; |
| 3998 | } |
| 3999 | |
Nikolay Borisov | 9a56fcd | 2020-11-02 16:48:59 +0200 | [diff] [blame] | 4000 | return btrfs_update_inode_item(trans, root, inode); |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 4001 | } |
| 4002 | |
Nikolay Borisov | 729f796 | 2020-11-02 16:49:06 +0200 | [diff] [blame] | 4003 | int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans, |
| 4004 | struct btrfs_root *root, struct btrfs_inode *inode) |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 4005 | { |
| 4006 | int ret; |
| 4007 | |
Nikolay Borisov | 729f796 | 2020-11-02 16:49:06 +0200 | [diff] [blame] | 4008 | ret = btrfs_update_inode(trans, root, inode); |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 4009 | if (ret == -ENOSPC) |
Nikolay Borisov | 729f796 | 2020-11-02 16:49:06 +0200 | [diff] [blame] | 4010 | return btrfs_update_inode_item(trans, root, inode); |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 4011 | return ret; |
| 4012 | } |
| 4013 | |
| 4014 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 4015 | * unlink helper that gets used here in inode.c and in the tree logging |
| 4016 | * recovery code. It remove a link in a directory with a given name, and |
| 4017 | * also drops the back refs in the inode to the directory |
| 4018 | */ |
Al Viro | 9298679 | 2011-03-04 17:14:37 +0000 | [diff] [blame] | 4019 | static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans, |
| 4020 | struct btrfs_root *root, |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 4021 | struct btrfs_inode *dir, |
| 4022 | struct btrfs_inode *inode, |
Al Viro | 9298679 | 2011-03-04 17:14:37 +0000 | [diff] [blame] | 4023 | const char *name, int name_len) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4024 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4025 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4026 | struct btrfs_path *path; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4027 | int ret = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4028 | struct btrfs_dir_item *di; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4029 | u64 index; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4030 | u64 ino = btrfs_ino(inode); |
| 4031 | u64 dir_ino = btrfs_ino(dir); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4032 | |
| 4033 | path = btrfs_alloc_path(); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 4034 | if (!path) { |
| 4035 | ret = -ENOMEM; |
Tsutomu Itoh | 554233a | 2011-02-03 03:16:25 +0000 | [diff] [blame] | 4036 | goto out; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 4037 | } |
| 4038 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4039 | di = btrfs_lookup_dir_item(trans, root, path, dir_ino, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4040 | name, name_len, -1); |
Liu Bo | 3cf5068 | 2018-09-12 06:06:26 +0800 | [diff] [blame] | 4041 | if (IS_ERR_OR_NULL(di)) { |
| 4042 | ret = di ? PTR_ERR(di) : -ENOENT; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4043 | goto err; |
| 4044 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4045 | ret = btrfs_delete_one_dir_name(trans, root, path, di); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 4046 | if (ret) |
| 4047 | goto err; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4048 | btrfs_release_path(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4049 | |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 4050 | /* |
| 4051 | * If we don't have dir index, we have to get it by looking up |
| 4052 | * the inode ref, since we get the inode ref, remove it directly, |
| 4053 | * it is unnecessary to do delayed deletion. |
| 4054 | * |
| 4055 | * But if we have dir index, needn't search inode ref to get it. |
| 4056 | * Since the inode ref is close to the inode item, it is better |
| 4057 | * that we delay to delete it, and just do this deletion when |
| 4058 | * we update the inode item. |
| 4059 | */ |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 4060 | if (inode->dir_index) { |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 4061 | ret = btrfs_delayed_delete_inode_ref(inode); |
| 4062 | if (!ret) { |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 4063 | index = inode->dir_index; |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 4064 | goto skip_backref; |
| 4065 | } |
| 4066 | } |
| 4067 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4068 | ret = btrfs_del_inode_ref(trans, root, name, name_len, ino, |
| 4069 | dir_ino, &index); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4070 | if (ret) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4071 | btrfs_info(fs_info, |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 4072 | "failed to delete reference to %.*s, inode %llu parent %llu", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 4073 | name_len, name, ino, dir_ino); |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 4074 | btrfs_abort_transaction(trans, ret); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4075 | goto err; |
| 4076 | } |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 4077 | skip_backref: |
Lu Fengqi | 9add294 | 2018-08-01 11:32:26 +0800 | [diff] [blame] | 4078 | ret = btrfs_delete_delayed_dir_index(trans, dir, index); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4079 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 4080 | btrfs_abort_transaction(trans, ret); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4081 | goto err; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4082 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4083 | |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 4084 | ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len, inode, |
| 4085 | dir_ino); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4086 | if (ret != 0 && ret != -ENOENT) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 4087 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4088 | goto err; |
| 4089 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4090 | |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 4091 | ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len, dir, |
| 4092 | index); |
Chris Mason | 6418c96 | 2010-10-30 07:34:24 -0400 | [diff] [blame] | 4093 | if (ret == -ENOENT) |
| 4094 | ret = 0; |
Zach Brown | d4e3991 | 2013-04-02 21:02:16 +0000 | [diff] [blame] | 4095 | else if (ret) |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 4096 | btrfs_abort_transaction(trans, ret); |
Josef Bacik | 63611e7 | 2019-06-18 10:59:18 -0400 | [diff] [blame] | 4097 | |
| 4098 | /* |
| 4099 | * If we have a pending delayed iput we could end up with the final iput |
| 4100 | * being run in btrfs-cleaner context. If we have enough of these built |
| 4101 | * up we can end up burning a lot of time in btrfs-cleaner without any |
| 4102 | * way to throttle the unlinks. Since we're currently holding a ref on |
| 4103 | * the inode we can run the delayed iput here without any issues as the |
| 4104 | * final iput won't be done until after we drop the ref we're currently |
| 4105 | * holding. |
| 4106 | */ |
| 4107 | btrfs_run_delayed_iput(fs_info, inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4108 | err: |
| 4109 | btrfs_free_path(path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4110 | if (ret) |
| 4111 | goto out; |
| 4112 | |
Nikolay Borisov | 6ef06d2 | 2017-02-20 13:50:34 +0200 | [diff] [blame] | 4113 | 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] | 4114 | inode_inc_iversion(&inode->vfs_inode); |
| 4115 | inode_inc_iversion(&dir->vfs_inode); |
| 4116 | inode->vfs_inode.i_ctime = dir->vfs_inode.i_mtime = |
| 4117 | dir->vfs_inode.i_ctime = current_time(&inode->vfs_inode); |
Nikolay Borisov | 9a56fcd | 2020-11-02 16:48:59 +0200 | [diff] [blame] | 4118 | ret = btrfs_update_inode(trans, root, dir); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4119 | out: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4120 | return ret; |
| 4121 | } |
| 4122 | |
Al Viro | 9298679 | 2011-03-04 17:14:37 +0000 | [diff] [blame] | 4123 | int btrfs_unlink_inode(struct btrfs_trans_handle *trans, |
| 4124 | struct btrfs_root *root, |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 4125 | struct btrfs_inode *dir, struct btrfs_inode *inode, |
Al Viro | 9298679 | 2011-03-04 17:14:37 +0000 | [diff] [blame] | 4126 | const char *name, int name_len) |
| 4127 | { |
| 4128 | int ret; |
| 4129 | ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len); |
| 4130 | if (!ret) { |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 4131 | drop_nlink(&inode->vfs_inode); |
Nikolay Borisov | 9a56fcd | 2020-11-02 16:48:59 +0200 | [diff] [blame] | 4132 | ret = btrfs_update_inode(trans, root, inode); |
Al Viro | 9298679 | 2011-03-04 17:14:37 +0000 | [diff] [blame] | 4133 | } |
| 4134 | return ret; |
| 4135 | } |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4136 | |
| 4137 | /* |
| 4138 | * helper to start transaction for unlink and rmdir. |
| 4139 | * |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 4140 | * unlink and rmdir are special in btrfs, they do not always free space, so |
| 4141 | * if we cannot make our reservations the normal way try and see if there is |
| 4142 | * plenty of slack room in the global reserve to migrate, otherwise we cannot |
| 4143 | * allow the unlink to occur. |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4144 | */ |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 4145 | static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir) |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4146 | { |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4147 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4148 | |
Josef Bacik | e70bea5 | 2011-10-11 14:18:24 -0400 | [diff] [blame] | 4149 | /* |
| 4150 | * 1 for the possible orphan item |
| 4151 | * 1 for the dir item |
| 4152 | * 1 for the dir index |
| 4153 | * 1 for the inode ref |
Josef Bacik | e70bea5 | 2011-10-11 14:18:24 -0400 | [diff] [blame] | 4154 | * 1 for the inode |
| 4155 | */ |
Josef Bacik | 7f9fe61 | 2020-03-13 15:58:05 -0400 | [diff] [blame] | 4156 | return btrfs_start_transaction_fallback_global_rsv(root, 5); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4157 | } |
| 4158 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4159 | static int btrfs_unlink(struct inode *dir, struct dentry *dentry) |
| 4160 | { |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4161 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4162 | struct btrfs_trans_handle *trans; |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 4163 | struct inode *inode = d_inode(dentry); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4164 | int ret; |
| 4165 | |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 4166 | trans = __unlink_start_trans(dir); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4167 | if (IS_ERR(trans)) |
Josef Bacik | 5df6a9f | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4168 | return PTR_ERR(trans); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4169 | |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 4170 | btrfs_record_unlink_dir(trans, BTRFS_I(dir), BTRFS_I(d_inode(dentry)), |
| 4171 | 0); |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 4172 | |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 4173 | ret = btrfs_unlink_inode(trans, root, BTRFS_I(dir), |
| 4174 | BTRFS_I(d_inode(dentry)), dentry->d_name.name, |
| 4175 | dentry->d_name.len); |
Tsutomu Itoh | b532402 | 2011-07-19 07:27:20 +0000 | [diff] [blame] | 4176 | if (ret) |
| 4177 | goto out; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 4178 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4179 | if (inode->i_nlink == 0) { |
Nikolay Borisov | 73f2e54 | 2017-02-20 13:50:59 +0200 | [diff] [blame] | 4180 | ret = btrfs_orphan_add(trans, BTRFS_I(inode)); |
Tsutomu Itoh | b532402 | 2011-07-19 07:27:20 +0000 | [diff] [blame] | 4181 | if (ret) |
| 4182 | goto out; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4183 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 4184 | |
Tsutomu Itoh | b532402 | 2011-07-19 07:27:20 +0000 | [diff] [blame] | 4185 | out: |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 4186 | btrfs_end_transaction(trans); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4187 | btrfs_btree_balance_dirty(root->fs_info); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4188 | return ret; |
| 4189 | } |
| 4190 | |
Misono Tomohiro | f60a236 | 2018-04-18 11:34:52 +0900 | [diff] [blame] | 4191 | static int btrfs_unlink_subvol(struct btrfs_trans_handle *trans, |
Josef Bacik | 045d396 | 2019-12-18 17:20:27 -0500 | [diff] [blame] | 4192 | struct inode *dir, struct dentry *dentry) |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4193 | { |
Lu Fengqi | 401b3b1 | 2018-08-01 11:32:30 +0800 | [diff] [blame] | 4194 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Josef Bacik | 045d396 | 2019-12-18 17:20:27 -0500 | [diff] [blame] | 4195 | struct btrfs_inode *inode = BTRFS_I(d_inode(dentry)); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4196 | struct btrfs_path *path; |
| 4197 | struct extent_buffer *leaf; |
| 4198 | struct btrfs_dir_item *di; |
| 4199 | struct btrfs_key key; |
Josef Bacik | 045d396 | 2019-12-18 17:20:27 -0500 | [diff] [blame] | 4200 | const char *name = dentry->d_name.name; |
| 4201 | int name_len = dentry->d_name.len; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4202 | u64 index; |
| 4203 | int ret; |
Josef Bacik | 045d396 | 2019-12-18 17:20:27 -0500 | [diff] [blame] | 4204 | u64 objectid; |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 4205 | u64 dir_ino = btrfs_ino(BTRFS_I(dir)); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4206 | |
Josef Bacik | 045d396 | 2019-12-18 17:20:27 -0500 | [diff] [blame] | 4207 | if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID) { |
| 4208 | objectid = inode->root->root_key.objectid; |
| 4209 | } else if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) { |
| 4210 | objectid = inode->location.objectid; |
| 4211 | } else { |
| 4212 | WARN_ON(1); |
| 4213 | return -EINVAL; |
| 4214 | } |
| 4215 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4216 | path = btrfs_alloc_path(); |
| 4217 | if (!path) |
| 4218 | return -ENOMEM; |
| 4219 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4220 | di = btrfs_lookup_dir_item(trans, root, path, dir_ino, |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4221 | name, name_len, -1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4222 | if (IS_ERR_OR_NULL(di)) { |
Liu Bo | 3cf5068 | 2018-09-12 06:06:26 +0800 | [diff] [blame] | 4223 | ret = di ? PTR_ERR(di) : -ENOENT; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4224 | goto out; |
| 4225 | } |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4226 | |
| 4227 | leaf = path->nodes[0]; |
| 4228 | btrfs_dir_item_key_to_cpu(leaf, di, &key); |
| 4229 | WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid); |
| 4230 | ret = btrfs_delete_one_dir_name(trans, root, path, di); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4231 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 4232 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4233 | goto out; |
| 4234 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4235 | btrfs_release_path(path); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4236 | |
Josef Bacik | d49d328 | 2019-12-18 17:20:28 -0500 | [diff] [blame] | 4237 | /* |
| 4238 | * This is a placeholder inode for a subvolume we didn't have a |
| 4239 | * reference to at the time of the snapshot creation. In the meantime |
| 4240 | * we could have renamed the real subvol link into our snapshot, so |
David Sterba | 1a9fd41 | 2021-05-21 17:42:23 +0200 | [diff] [blame] | 4241 | * depending on btrfs_del_root_ref to return -ENOENT here is incorrect. |
Josef Bacik | d49d328 | 2019-12-18 17:20:28 -0500 | [diff] [blame] | 4242 | * Instead simply lookup the dir_index_item for this entry so we can |
| 4243 | * remove it. Otherwise we know we have a ref to the root and we can |
| 4244 | * call btrfs_del_root_ref, and it _shouldn't_ fail. |
| 4245 | */ |
| 4246 | if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) { |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4247 | di = btrfs_search_dir_index_item(root, path, dir_ino, |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4248 | name, name_len); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4249 | if (IS_ERR_OR_NULL(di)) { |
| 4250 | if (!di) |
| 4251 | ret = -ENOENT; |
| 4252 | else |
| 4253 | ret = PTR_ERR(di); |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 4254 | btrfs_abort_transaction(trans, ret); |
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_item_key_to_cpu(leaf, &key, path->slots[0]); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4260 | index = key.offset; |
Josef Bacik | d49d328 | 2019-12-18 17:20:28 -0500 | [diff] [blame] | 4261 | btrfs_release_path(path); |
| 4262 | } else { |
| 4263 | ret = btrfs_del_root_ref(trans, objectid, |
| 4264 | root->root_key.objectid, dir_ino, |
| 4265 | &index, name, name_len); |
| 4266 | if (ret) { |
| 4267 | btrfs_abort_transaction(trans, ret); |
| 4268 | goto out; |
| 4269 | } |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4270 | } |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4271 | |
Lu Fengqi | 9add294 | 2018-08-01 11:32:26 +0800 | [diff] [blame] | 4272 | ret = btrfs_delete_delayed_dir_index(trans, BTRFS_I(dir), index); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4273 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 4274 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4275 | goto out; |
| 4276 | } |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 4277 | |
Nikolay Borisov | 6ef06d2 | 2017-02-20 13:50:34 +0200 | [diff] [blame] | 4278 | 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] | 4279 | inode_inc_iversion(dir); |
Deepa Dinamani | c2050a4 | 2016-09-14 07:48:06 -0700 | [diff] [blame] | 4280 | dir->i_mtime = dir->i_ctime = current_time(dir); |
Nikolay Borisov | 729f796 | 2020-11-02 16:49:06 +0200 | [diff] [blame] | 4281 | ret = btrfs_update_inode_fallback(trans, root, BTRFS_I(dir)); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4282 | if (ret) |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 4283 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4284 | out: |
Josef Bacik | 71d7aed | 2011-06-14 14:24:32 -0400 | [diff] [blame] | 4285 | btrfs_free_path(path); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4286 | return ret; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4287 | } |
| 4288 | |
Misono Tomohiro | ec42f16 | 2018-04-18 11:34:13 +0900 | [diff] [blame] | 4289 | /* |
| 4290 | * Helper to check if the subvolume references other subvolumes or if it's |
| 4291 | * default. |
| 4292 | */ |
Misono Tomohiro | f60a236 | 2018-04-18 11:34:52 +0900 | [diff] [blame] | 4293 | static noinline int may_destroy_subvol(struct btrfs_root *root) |
Misono Tomohiro | ec42f16 | 2018-04-18 11:34:13 +0900 | [diff] [blame] | 4294 | { |
| 4295 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 4296 | struct btrfs_path *path; |
| 4297 | struct btrfs_dir_item *di; |
| 4298 | struct btrfs_key key; |
| 4299 | u64 dir_id; |
| 4300 | int ret; |
| 4301 | |
| 4302 | path = btrfs_alloc_path(); |
| 4303 | if (!path) |
| 4304 | return -ENOMEM; |
| 4305 | |
| 4306 | /* Make sure this root isn't set as the default subvol */ |
| 4307 | dir_id = btrfs_super_root_dir(fs_info->super_copy); |
| 4308 | di = btrfs_lookup_dir_item(NULL, fs_info->tree_root, path, |
| 4309 | dir_id, "default", 7, 0); |
| 4310 | if (di && !IS_ERR(di)) { |
| 4311 | btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key); |
| 4312 | if (key.objectid == root->root_key.objectid) { |
| 4313 | ret = -EPERM; |
| 4314 | btrfs_err(fs_info, |
| 4315 | "deleting default subvolume %llu is not allowed", |
| 4316 | key.objectid); |
| 4317 | goto out; |
| 4318 | } |
| 4319 | btrfs_release_path(path); |
| 4320 | } |
| 4321 | |
| 4322 | key.objectid = root->root_key.objectid; |
| 4323 | key.type = BTRFS_ROOT_REF_KEY; |
| 4324 | key.offset = (u64)-1; |
| 4325 | |
| 4326 | ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0); |
| 4327 | if (ret < 0) |
| 4328 | goto out; |
| 4329 | BUG_ON(ret == 0); |
| 4330 | |
| 4331 | ret = 0; |
| 4332 | if (path->slots[0] > 0) { |
| 4333 | path->slots[0]--; |
| 4334 | btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]); |
| 4335 | if (key.objectid == root->root_key.objectid && |
| 4336 | key.type == BTRFS_ROOT_REF_KEY) |
| 4337 | ret = -ENOTEMPTY; |
| 4338 | } |
| 4339 | out: |
| 4340 | btrfs_free_path(path); |
| 4341 | return ret; |
| 4342 | } |
| 4343 | |
Nikolay Borisov | 20a6800 | 2018-04-27 14:36:24 +0300 | [diff] [blame] | 4344 | /* Delete all dentries for inodes belonging to the root */ |
| 4345 | static void btrfs_prune_dentries(struct btrfs_root *root) |
| 4346 | { |
| 4347 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 4348 | struct rb_node *node; |
| 4349 | struct rb_node *prev; |
| 4350 | struct btrfs_inode *entry; |
| 4351 | struct inode *inode; |
| 4352 | u64 objectid = 0; |
| 4353 | |
| 4354 | if (!test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) |
| 4355 | WARN_ON(btrfs_root_refs(&root->root_item) != 0); |
| 4356 | |
| 4357 | spin_lock(&root->inode_lock); |
| 4358 | again: |
| 4359 | node = root->inode_tree.rb_node; |
| 4360 | prev = NULL; |
| 4361 | while (node) { |
| 4362 | prev = node; |
| 4363 | entry = rb_entry(node, struct btrfs_inode, rb_node); |
| 4364 | |
David Sterba | 3750851 | 2018-06-29 10:56:40 +0200 | [diff] [blame] | 4365 | if (objectid < btrfs_ino(entry)) |
Nikolay Borisov | 20a6800 | 2018-04-27 14:36:24 +0300 | [diff] [blame] | 4366 | node = node->rb_left; |
David Sterba | 3750851 | 2018-06-29 10:56:40 +0200 | [diff] [blame] | 4367 | else if (objectid > btrfs_ino(entry)) |
Nikolay Borisov | 20a6800 | 2018-04-27 14:36:24 +0300 | [diff] [blame] | 4368 | node = node->rb_right; |
| 4369 | else |
| 4370 | break; |
| 4371 | } |
| 4372 | if (!node) { |
| 4373 | while (prev) { |
| 4374 | entry = rb_entry(prev, struct btrfs_inode, rb_node); |
David Sterba | 3750851 | 2018-06-29 10:56:40 +0200 | [diff] [blame] | 4375 | if (objectid <= btrfs_ino(entry)) { |
Nikolay Borisov | 20a6800 | 2018-04-27 14:36:24 +0300 | [diff] [blame] | 4376 | node = prev; |
| 4377 | break; |
| 4378 | } |
| 4379 | prev = rb_next(prev); |
| 4380 | } |
| 4381 | } |
| 4382 | while (node) { |
| 4383 | entry = rb_entry(node, struct btrfs_inode, rb_node); |
David Sterba | 3750851 | 2018-06-29 10:56:40 +0200 | [diff] [blame] | 4384 | objectid = btrfs_ino(entry) + 1; |
Nikolay Borisov | 20a6800 | 2018-04-27 14:36:24 +0300 | [diff] [blame] | 4385 | inode = igrab(&entry->vfs_inode); |
| 4386 | if (inode) { |
| 4387 | spin_unlock(&root->inode_lock); |
| 4388 | if (atomic_read(&inode->i_count) > 1) |
| 4389 | d_prune_aliases(inode); |
| 4390 | /* |
| 4391 | * btrfs_drop_inode will have it removed from the inode |
| 4392 | * cache when its usage count hits zero. |
| 4393 | */ |
| 4394 | iput(inode); |
| 4395 | cond_resched(); |
| 4396 | spin_lock(&root->inode_lock); |
| 4397 | goto again; |
| 4398 | } |
| 4399 | |
| 4400 | if (cond_resched_lock(&root->inode_lock)) |
| 4401 | goto again; |
| 4402 | |
| 4403 | node = rb_next(node); |
| 4404 | } |
| 4405 | spin_unlock(&root->inode_lock); |
| 4406 | } |
| 4407 | |
Misono Tomohiro | f60a236 | 2018-04-18 11:34:52 +0900 | [diff] [blame] | 4408 | int btrfs_delete_subvolume(struct inode *dir, struct dentry *dentry) |
| 4409 | { |
| 4410 | struct btrfs_fs_info *fs_info = btrfs_sb(dentry->d_sb); |
| 4411 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 4412 | struct inode *inode = d_inode(dentry); |
| 4413 | struct btrfs_root *dest = BTRFS_I(inode)->root; |
| 4414 | struct btrfs_trans_handle *trans; |
| 4415 | struct btrfs_block_rsv block_rsv; |
| 4416 | u64 root_flags; |
Misono Tomohiro | f60a236 | 2018-04-18 11:34:52 +0900 | [diff] [blame] | 4417 | int ret; |
Misono Tomohiro | f60a236 | 2018-04-18 11:34:52 +0900 | [diff] [blame] | 4418 | |
| 4419 | /* |
| 4420 | * Don't allow to delete a subvolume with send in progress. This is |
| 4421 | * inside the inode lock so the error handling that has to drop the bit |
| 4422 | * again is not run concurrently. |
| 4423 | */ |
| 4424 | spin_lock(&dest->root_item_lock); |
Lu Fengqi | a7176f7 | 2018-08-04 21:10:53 +0800 | [diff] [blame] | 4425 | if (dest->send_in_progress) { |
Misono Tomohiro | f60a236 | 2018-04-18 11:34:52 +0900 | [diff] [blame] | 4426 | spin_unlock(&dest->root_item_lock); |
| 4427 | btrfs_warn(fs_info, |
| 4428 | "attempt to delete subvolume %llu during send", |
| 4429 | dest->root_key.objectid); |
| 4430 | return -EPERM; |
| 4431 | } |
Lu Fengqi | a7176f7 | 2018-08-04 21:10:53 +0800 | [diff] [blame] | 4432 | root_flags = btrfs_root_flags(&dest->root_item); |
| 4433 | btrfs_set_root_flags(&dest->root_item, |
| 4434 | root_flags | BTRFS_ROOT_SUBVOL_DEAD); |
| 4435 | spin_unlock(&dest->root_item_lock); |
Misono Tomohiro | f60a236 | 2018-04-18 11:34:52 +0900 | [diff] [blame] | 4436 | |
| 4437 | down_write(&fs_info->subvol_sem); |
| 4438 | |
Nikolay Borisov | ee0d904 | 2020-11-24 17:49:30 +0200 | [diff] [blame] | 4439 | ret = may_destroy_subvol(dest); |
| 4440 | if (ret) |
Misono Tomohiro | f60a236 | 2018-04-18 11:34:52 +0900 | [diff] [blame] | 4441 | goto out_up_write; |
| 4442 | |
| 4443 | btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP); |
| 4444 | /* |
| 4445 | * One for dir inode, |
| 4446 | * two for dir entries, |
| 4447 | * two for root ref/backref. |
| 4448 | */ |
Nikolay Borisov | ee0d904 | 2020-11-24 17:49:30 +0200 | [diff] [blame] | 4449 | ret = btrfs_subvolume_reserve_metadata(root, &block_rsv, 5, true); |
| 4450 | if (ret) |
Misono Tomohiro | f60a236 | 2018-04-18 11:34:52 +0900 | [diff] [blame] | 4451 | goto out_up_write; |
| 4452 | |
| 4453 | trans = btrfs_start_transaction(root, 0); |
| 4454 | if (IS_ERR(trans)) { |
Nikolay Borisov | ee0d904 | 2020-11-24 17:49:30 +0200 | [diff] [blame] | 4455 | ret = PTR_ERR(trans); |
Misono Tomohiro | f60a236 | 2018-04-18 11:34:52 +0900 | [diff] [blame] | 4456 | goto out_release; |
| 4457 | } |
| 4458 | trans->block_rsv = &block_rsv; |
| 4459 | trans->bytes_reserved = block_rsv.size; |
| 4460 | |
| 4461 | btrfs_record_snapshot_destroy(trans, BTRFS_I(dir)); |
| 4462 | |
Josef Bacik | 045d396 | 2019-12-18 17:20:27 -0500 | [diff] [blame] | 4463 | ret = btrfs_unlink_subvol(trans, dir, dentry); |
Misono Tomohiro | f60a236 | 2018-04-18 11:34:52 +0900 | [diff] [blame] | 4464 | if (ret) { |
Misono Tomohiro | f60a236 | 2018-04-18 11:34:52 +0900 | [diff] [blame] | 4465 | btrfs_abort_transaction(trans, ret); |
| 4466 | goto out_end_trans; |
| 4467 | } |
| 4468 | |
Josef Bacik | 2731f51 | 2021-03-12 15:25:04 -0500 | [diff] [blame] | 4469 | ret = btrfs_record_root_in_trans(trans, dest); |
| 4470 | if (ret) { |
| 4471 | btrfs_abort_transaction(trans, ret); |
| 4472 | goto out_end_trans; |
| 4473 | } |
Misono Tomohiro | f60a236 | 2018-04-18 11:34:52 +0900 | [diff] [blame] | 4474 | |
| 4475 | memset(&dest->root_item.drop_progress, 0, |
| 4476 | sizeof(dest->root_item.drop_progress)); |
David Sterba | c842268 | 2020-09-15 21:44:52 +0200 | [diff] [blame] | 4477 | btrfs_set_root_drop_level(&dest->root_item, 0); |
Misono Tomohiro | f60a236 | 2018-04-18 11:34:52 +0900 | [diff] [blame] | 4478 | btrfs_set_root_refs(&dest->root_item, 0); |
| 4479 | |
| 4480 | if (!test_and_set_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &dest->state)) { |
| 4481 | ret = btrfs_insert_orphan_item(trans, |
| 4482 | fs_info->tree_root, |
| 4483 | dest->root_key.objectid); |
| 4484 | if (ret) { |
| 4485 | btrfs_abort_transaction(trans, ret); |
Misono Tomohiro | f60a236 | 2018-04-18 11:34:52 +0900 | [diff] [blame] | 4486 | goto out_end_trans; |
| 4487 | } |
| 4488 | } |
| 4489 | |
Lu Fengqi | d195779 | 2018-05-29 15:01:54 +0800 | [diff] [blame] | 4490 | ret = btrfs_uuid_tree_remove(trans, dest->root_item.uuid, |
Misono Tomohiro | f60a236 | 2018-04-18 11:34:52 +0900 | [diff] [blame] | 4491 | BTRFS_UUID_KEY_SUBVOL, |
| 4492 | dest->root_key.objectid); |
| 4493 | if (ret && ret != -ENOENT) { |
| 4494 | btrfs_abort_transaction(trans, ret); |
Misono Tomohiro | f60a236 | 2018-04-18 11:34:52 +0900 | [diff] [blame] | 4495 | goto out_end_trans; |
| 4496 | } |
| 4497 | if (!btrfs_is_empty_uuid(dest->root_item.received_uuid)) { |
Lu Fengqi | d195779 | 2018-05-29 15:01:54 +0800 | [diff] [blame] | 4498 | ret = btrfs_uuid_tree_remove(trans, |
Misono Tomohiro | f60a236 | 2018-04-18 11:34:52 +0900 | [diff] [blame] | 4499 | dest->root_item.received_uuid, |
| 4500 | BTRFS_UUID_KEY_RECEIVED_SUBVOL, |
| 4501 | dest->root_key.objectid); |
| 4502 | if (ret && ret != -ENOENT) { |
| 4503 | btrfs_abort_transaction(trans, ret); |
Misono Tomohiro | f60a236 | 2018-04-18 11:34:52 +0900 | [diff] [blame] | 4504 | goto out_end_trans; |
| 4505 | } |
| 4506 | } |
| 4507 | |
Qu Wenruo | 082b6c9 | 2020-06-16 10:17:37 +0800 | [diff] [blame] | 4508 | free_anon_bdev(dest->anon_dev); |
| 4509 | dest->anon_dev = 0; |
Misono Tomohiro | f60a236 | 2018-04-18 11:34:52 +0900 | [diff] [blame] | 4510 | out_end_trans: |
| 4511 | trans->block_rsv = NULL; |
| 4512 | trans->bytes_reserved = 0; |
| 4513 | ret = btrfs_end_transaction(trans); |
Misono Tomohiro | f60a236 | 2018-04-18 11:34:52 +0900 | [diff] [blame] | 4514 | inode->i_flags |= S_DEAD; |
| 4515 | out_release: |
Qu Wenruo | e85fde5 | 2020-07-24 14:46:10 +0800 | [diff] [blame] | 4516 | btrfs_subvolume_release_metadata(root, &block_rsv); |
Misono Tomohiro | f60a236 | 2018-04-18 11:34:52 +0900 | [diff] [blame] | 4517 | out_up_write: |
| 4518 | up_write(&fs_info->subvol_sem); |
Nikolay Borisov | ee0d904 | 2020-11-24 17:49:30 +0200 | [diff] [blame] | 4519 | if (ret) { |
Misono Tomohiro | f60a236 | 2018-04-18 11:34:52 +0900 | [diff] [blame] | 4520 | spin_lock(&dest->root_item_lock); |
| 4521 | root_flags = btrfs_root_flags(&dest->root_item); |
| 4522 | btrfs_set_root_flags(&dest->root_item, |
| 4523 | root_flags & ~BTRFS_ROOT_SUBVOL_DEAD); |
| 4524 | spin_unlock(&dest->root_item_lock); |
| 4525 | } else { |
| 4526 | d_invalidate(dentry); |
Nikolay Borisov | 20a6800 | 2018-04-27 14:36:24 +0300 | [diff] [blame] | 4527 | btrfs_prune_dentries(dest); |
Misono Tomohiro | f60a236 | 2018-04-18 11:34:52 +0900 | [diff] [blame] | 4528 | ASSERT(dest->send_in_progress == 0); |
Misono Tomohiro | f60a236 | 2018-04-18 11:34:52 +0900 | [diff] [blame] | 4529 | } |
| 4530 | |
Nikolay Borisov | ee0d904 | 2020-11-24 17:49:30 +0200 | [diff] [blame] | 4531 | return ret; |
Misono Tomohiro | f60a236 | 2018-04-18 11:34:52 +0900 | [diff] [blame] | 4532 | } |
| 4533 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4534 | static int btrfs_rmdir(struct inode *dir, struct dentry *dentry) |
| 4535 | { |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 4536 | struct inode *inode = d_inode(dentry); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 4537 | int err = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4538 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4539 | struct btrfs_trans_handle *trans; |
Filipe Manana | 44f714d | 2016-06-06 16:11:13 +0100 | [diff] [blame] | 4540 | u64 last_unlink_trans; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4541 | |
David Sterba | b3ae244 | 2012-09-13 16:04:34 -0600 | [diff] [blame] | 4542 | if (inode->i_size > BTRFS_EMPTY_DIR_SIZE) |
Yan | 134d451 | 2007-10-25 15:49:25 -0400 | [diff] [blame] | 4543 | return -ENOTEMPTY; |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 4544 | if (btrfs_ino(BTRFS_I(inode)) == BTRFS_FIRST_FREE_OBJECTID) |
Misono Tomohiro | a79a464 | 2018-04-18 11:35:31 +0900 | [diff] [blame] | 4545 | return btrfs_delete_subvolume(dir, dentry); |
Yan | 134d451 | 2007-10-25 15:49:25 -0400 | [diff] [blame] | 4546 | |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 4547 | trans = __unlink_start_trans(dir); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4548 | if (IS_ERR(trans)) |
Josef Bacik | 5df6a9f | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4549 | return PTR_ERR(trans); |
Josef Bacik | 5df6a9f | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4550 | |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 4551 | if (unlikely(btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) { |
Josef Bacik | 045d396 | 2019-12-18 17:20:27 -0500 | [diff] [blame] | 4552 | err = btrfs_unlink_subvol(trans, dir, dentry); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4553 | goto out; |
| 4554 | } |
| 4555 | |
Nikolay Borisov | 73f2e54 | 2017-02-20 13:50:59 +0200 | [diff] [blame] | 4556 | err = btrfs_orphan_add(trans, BTRFS_I(inode)); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 4557 | if (err) |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4558 | goto out; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 4559 | |
Filipe Manana | 44f714d | 2016-06-06 16:11:13 +0100 | [diff] [blame] | 4560 | last_unlink_trans = BTRFS_I(inode)->last_unlink_trans; |
| 4561 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4562 | /* now the directory is empty */ |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 4563 | err = btrfs_unlink_inode(trans, root, BTRFS_I(dir), |
| 4564 | BTRFS_I(d_inode(dentry)), dentry->d_name.name, |
| 4565 | dentry->d_name.len); |
Filipe Manana | 44f714d | 2016-06-06 16:11:13 +0100 | [diff] [blame] | 4566 | if (!err) { |
Nikolay Borisov | 6ef06d2 | 2017-02-20 13:50:34 +0200 | [diff] [blame] | 4567 | btrfs_i_size_write(BTRFS_I(inode), 0); |
Filipe Manana | 44f714d | 2016-06-06 16:11:13 +0100 | [diff] [blame] | 4568 | /* |
| 4569 | * Propagate the last_unlink_trans value of the deleted dir to |
| 4570 | * its parent directory. This is to prevent an unrecoverable |
| 4571 | * log tree in the case we do something like this: |
| 4572 | * 1) create dir foo |
| 4573 | * 2) create snapshot under dir foo |
| 4574 | * 3) delete the snapshot |
| 4575 | * 4) rmdir foo |
| 4576 | * 5) mkdir foo |
| 4577 | * 6) fsync foo or some file inside foo |
| 4578 | */ |
| 4579 | if (last_unlink_trans >= trans->transid) |
| 4580 | BTRFS_I(dir)->last_unlink_trans = last_unlink_trans; |
| 4581 | } |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4582 | out: |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 4583 | btrfs_end_transaction(trans); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4584 | btrfs_btree_balance_dirty(root->fs_info); |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 4585 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4586 | return err; |
| 4587 | } |
| 4588 | |
Josef Bacik | ddfae63 | 2017-10-19 14:16:02 -0400 | [diff] [blame] | 4589 | /* |
| 4590 | * Return this if we need to call truncate_block for the last bit of the |
| 4591 | * truncate. |
| 4592 | */ |
| 4593 | #define NEED_TRUNCATE_BLOCK 1 |
Filipe Manana | 0305cd5 | 2015-10-16 12:34:25 +0100 | [diff] [blame] | 4594 | |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 4595 | /* |
Filipe Manana | 4f7e673 | 2021-05-24 11:35:54 +0100 | [diff] [blame] | 4596 | * Remove inode items from a given root. |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4597 | * |
Filipe Manana | 4f7e673 | 2021-05-24 11:35:54 +0100 | [diff] [blame] | 4598 | * @trans: A transaction handle. |
| 4599 | * @root: The root from which to remove items. |
| 4600 | * @inode: The inode whose items we want to remove. |
| 4601 | * @new_size: The new i_size for the inode. This is only applicable when |
| 4602 | * @min_type is BTRFS_EXTENT_DATA_KEY, must be 0 otherwise. |
| 4603 | * @min_type: The minimum key type to remove. All keys with a type |
| 4604 | * greater than this value are removed and all keys with |
| 4605 | * this type are removed only if their offset is >= @new_size. |
Filipe Manana | 0d7d316 | 2021-05-24 11:35:55 +0100 | [diff] [blame] | 4606 | * @extents_found: Output parameter that will contain the number of file |
| 4607 | * extent items that were removed or adjusted to the new |
| 4608 | * inode i_size. The caller is responsible for initializing |
| 4609 | * the counter. Also, it can be NULL if the caller does not |
| 4610 | * need this counter. |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 4611 | * |
Filipe Manana | 4f7e673 | 2021-05-24 11:35:54 +0100 | [diff] [blame] | 4612 | * Remove all keys associated with the inode from the given root that have a key |
| 4613 | * with a type greater than or equals to @min_type. When @min_type has a value of |
| 4614 | * BTRFS_EXTENT_DATA_KEY, only remove file extent items that have an offset value |
| 4615 | * greater than or equals to @new_size. If a file extent item that starts before |
| 4616 | * @new_size and ends after it is found, its length is adjusted. |
| 4617 | * |
| 4618 | * Returns: 0 on success, < 0 on error and NEED_TRUNCATE_BLOCK when @min_type is |
| 4619 | * BTRFS_EXTENT_DATA_KEY and the caller must truncate the last block. |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4620 | */ |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4621 | int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans, |
| 4622 | struct btrfs_root *root, |
Nikolay Borisov | 5074339 | 2020-11-02 16:48:55 +0200 | [diff] [blame] | 4623 | struct btrfs_inode *inode, |
Filipe Manana | 0d7d316 | 2021-05-24 11:35:55 +0100 | [diff] [blame] | 4624 | u64 new_size, u32 min_type, |
| 4625 | u64 *extents_found) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4626 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4627 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4628 | struct btrfs_path *path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4629 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4630 | struct btrfs_file_extent_item *fi; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4631 | struct btrfs_key key; |
| 4632 | struct btrfs_key found_key; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4633 | u64 extent_start = 0; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4634 | u64 extent_num_bytes = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4635 | u64 extent_offset = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4636 | u64 item_end = 0; |
Filipe Manana | c1aa457 | 2015-06-20 18:20:09 +0100 | [diff] [blame] | 4637 | u64 last_size = new_size; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4638 | u32 found_type = (u8)-1; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4639 | int found_extent; |
| 4640 | int del_item; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4641 | int pending_del_nr = 0; |
| 4642 | int pending_del_slot = 0; |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4643 | int extent_type = -1; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4644 | int ret; |
Nikolay Borisov | 5074339 | 2020-11-02 16:48:55 +0200 | [diff] [blame] | 4645 | u64 ino = btrfs_ino(inode); |
Chris Mason | 28ed134 | 2014-12-17 09:41:04 -0800 | [diff] [blame] | 4646 | u64 bytes_deleted = 0; |
Thomas Meyer | 897ca81 | 2017-10-07 16:02:21 +0200 | [diff] [blame] | 4647 | bool be_nice = false; |
| 4648 | bool should_throttle = false; |
Filipe Manana | 28553fa | 2020-02-07 12:23:09 +0000 | [diff] [blame] | 4649 | const u64 lock_start = ALIGN_DOWN(new_size, fs_info->sectorsize); |
| 4650 | struct extent_state *cached_state = NULL; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4651 | |
| 4652 | BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4653 | |
Chris Mason | 28ed134 | 2014-12-17 09:41:04 -0800 | [diff] [blame] | 4654 | /* |
Qu Wenruo | 92a7cc4 | 2020-05-15 14:01:40 +0800 | [diff] [blame] | 4655 | * For non-free space inodes and non-shareable roots, we want to back |
| 4656 | * off from time to time. This means all inodes in subvolume roots, |
| 4657 | * reloc roots, and data reloc roots. |
Chris Mason | 28ed134 | 2014-12-17 09:41:04 -0800 | [diff] [blame] | 4658 | */ |
Nikolay Borisov | 5074339 | 2020-11-02 16:48:55 +0200 | [diff] [blame] | 4659 | if (!btrfs_is_free_space_inode(inode) && |
Qu Wenruo | 92a7cc4 | 2020-05-15 14:01:40 +0800 | [diff] [blame] | 4660 | test_bit(BTRFS_ROOT_SHAREABLE, &root->state)) |
Thomas Meyer | 897ca81 | 2017-10-07 16:02:21 +0200 | [diff] [blame] | 4661 | be_nice = true; |
Chris Mason | 28ed134 | 2014-12-17 09:41:04 -0800 | [diff] [blame] | 4662 | |
Mark Fasheh | 0eb0e19 | 2011-07-12 16:44:10 -0700 | [diff] [blame] | 4663 | path = btrfs_alloc_path(); |
| 4664 | if (!path) |
| 4665 | return -ENOMEM; |
David Sterba | e4058b5 | 2015-11-27 16:31:35 +0100 | [diff] [blame] | 4666 | path->reada = READA_BACK; |
Mark Fasheh | 0eb0e19 | 2011-07-12 16:44:10 -0700 | [diff] [blame] | 4667 | |
Qu Wenruo | 82028e0a | 2020-05-15 14:01:41 +0800 | [diff] [blame] | 4668 | if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) { |
Nikolay Borisov | 5074339 | 2020-11-02 16:48:55 +0200 | [diff] [blame] | 4669 | lock_extent_bits(&inode->io_tree, lock_start, (u64)-1, |
Filipe Manana | a5ae50d | 2020-02-20 13:29:49 +0000 | [diff] [blame] | 4670 | &cached_state); |
Filipe Manana | 28553fa | 2020-02-07 12:23:09 +0000 | [diff] [blame] | 4671 | |
Qu Wenruo | 82028e0a | 2020-05-15 14:01:41 +0800 | [diff] [blame] | 4672 | /* |
| 4673 | * We want to drop from the next block forward in case this |
| 4674 | * new size is not block aligned since we will be keeping the |
| 4675 | * last block of the extent just the way it is. |
| 4676 | */ |
Nikolay Borisov | 5074339 | 2020-11-02 16:48:55 +0200 | [diff] [blame] | 4677 | btrfs_drop_extent_cache(inode, ALIGN(new_size, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4678 | fs_info->sectorsize), |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 4679 | (u64)-1, 0); |
Qu Wenruo | 82028e0a | 2020-05-15 14:01:41 +0800 | [diff] [blame] | 4680 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4681 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 4682 | /* |
| 4683 | * This function is also used to drop the items in the log tree before |
| 4684 | * we relog the inode, so if root != BTRFS_I(inode)->root, it means |
Andrea Gelmini | 52042d8 | 2018-11-28 12:05:13 +0100 | [diff] [blame] | 4685 | * it is used to drop the logged items. So we shouldn't kill the delayed |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 4686 | * items. |
| 4687 | */ |
Nikolay Borisov | 5074339 | 2020-11-02 16:48:55 +0200 | [diff] [blame] | 4688 | if (min_type == 0 && root == inode->root) |
| 4689 | btrfs_kill_delayed_inode_items(inode); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 4690 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4691 | key.objectid = ino; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4692 | key.offset = (u64)-1; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4693 | key.type = (u8)-1; |
| 4694 | |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4695 | search_again: |
Chris Mason | 28ed134 | 2014-12-17 09:41:04 -0800 | [diff] [blame] | 4696 | /* |
| 4697 | * with a 16K leaf size and 128MB extents, you can actually queue |
| 4698 | * up a huge file in a single leaf. Most of the time that |
| 4699 | * bytes_deleted is > 0, it will be huge by the time we get here |
| 4700 | */ |
Omar Sandoval | fd86a3a | 2018-05-11 13:13:30 -0700 | [diff] [blame] | 4701 | if (be_nice && bytes_deleted > SZ_32M && |
| 4702 | btrfs_should_end_transaction(trans)) { |
| 4703 | ret = -EAGAIN; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4704 | goto out; |
| 4705 | } |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4706 | |
Omar Sandoval | fd86a3a | 2018-05-11 13:13:30 -0700 | [diff] [blame] | 4707 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 4708 | if (ret < 0) |
| 4709 | goto out; |
| 4710 | |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4711 | if (ret > 0) { |
Omar Sandoval | fd86a3a | 2018-05-11 13:13:30 -0700 | [diff] [blame] | 4712 | ret = 0; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4713 | /* there are no items in the tree for us to truncate, we're |
| 4714 | * done |
| 4715 | */ |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4716 | if (path->slots[0] == 0) |
| 4717 | goto out; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4718 | path->slots[0]--; |
| 4719 | } |
| 4720 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4721 | while (1) { |
Josef Bacik | 9ddc959 | 2020-01-17 09:02:22 -0500 | [diff] [blame] | 4722 | u64 clear_start = 0, clear_len = 0; |
| 4723 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4724 | fi = NULL; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4725 | leaf = path->nodes[0]; |
| 4726 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 4727 | found_type = found_key.type; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4728 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4729 | if (found_key.objectid != ino) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4730 | break; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4731 | |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4732 | if (found_type < min_type) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4733 | break; |
| 4734 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4735 | item_end = found_key.offset; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4736 | if (found_type == BTRFS_EXTENT_DATA_KEY) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4737 | fi = btrfs_item_ptr(leaf, path->slots[0], |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4738 | struct btrfs_file_extent_item); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4739 | extent_type = btrfs_file_extent_type(leaf, fi); |
| 4740 | if (extent_type != BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4741 | item_end += |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4742 | btrfs_file_extent_num_bytes(leaf, fi); |
Liu Bo | 09ed2f1 | 2017-03-10 11:09:48 -0800 | [diff] [blame] | 4743 | |
| 4744 | trace_btrfs_truncate_show_fi_regular( |
Nikolay Borisov | 5074339 | 2020-11-02 16:48:55 +0200 | [diff] [blame] | 4745 | inode, leaf, fi, found_key.offset); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4746 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
Qu Wenruo | e41ca58 | 2018-06-06 15:41:49 +0800 | [diff] [blame] | 4747 | item_end += btrfs_file_extent_ram_bytes(leaf, |
| 4748 | fi); |
Liu Bo | 09ed2f1 | 2017-03-10 11:09:48 -0800 | [diff] [blame] | 4749 | |
| 4750 | trace_btrfs_truncate_show_fi_inline( |
Nikolay Borisov | 5074339 | 2020-11-02 16:48:55 +0200 | [diff] [blame] | 4751 | inode, leaf, fi, path->slots[0], |
Liu Bo | 09ed2f1 | 2017-03-10 11:09:48 -0800 | [diff] [blame] | 4752 | found_key.offset); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4753 | } |
Yan | 008630c | 2007-11-07 13:31:09 -0500 | [diff] [blame] | 4754 | item_end--; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4755 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4756 | if (found_type > min_type) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4757 | del_item = 1; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4758 | } else { |
Filipe Manana | 76b42ab | 2017-02-14 16:56:01 +0000 | [diff] [blame] | 4759 | if (item_end < new_size) |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4760 | break; |
| 4761 | if (found_key.offset >= new_size) |
| 4762 | del_item = 1; |
| 4763 | else |
| 4764 | del_item = 0; |
| 4765 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4766 | found_extent = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4767 | /* FIXME, shrink the extent if the ref count is only 1 */ |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4768 | if (found_type != BTRFS_EXTENT_DATA_KEY) |
| 4769 | goto delete; |
| 4770 | |
Filipe Manana | 0d7d316 | 2021-05-24 11:35:55 +0100 | [diff] [blame] | 4771 | if (extents_found != NULL) |
| 4772 | (*extents_found)++; |
| 4773 | |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4774 | if (extent_type != BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4775 | u64 num_dec; |
Josef Bacik | 9ddc959 | 2020-01-17 09:02:22 -0500 | [diff] [blame] | 4776 | |
| 4777 | clear_start = found_key.offset; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4778 | extent_start = btrfs_file_extent_disk_bytenr(leaf, fi); |
Miao Xie | f70a9a6b | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 4779 | if (!del_item) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4780 | u64 orig_num_bytes = |
| 4781 | btrfs_file_extent_num_bytes(leaf, fi); |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 4782 | extent_num_bytes = ALIGN(new_size - |
| 4783 | found_key.offset, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4784 | fs_info->sectorsize); |
Josef Bacik | 9ddc959 | 2020-01-17 09:02:22 -0500 | [diff] [blame] | 4785 | clear_start = ALIGN(new_size, fs_info->sectorsize); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4786 | btrfs_set_file_extent_num_bytes(leaf, fi, |
| 4787 | extent_num_bytes); |
| 4788 | num_dec = (orig_num_bytes - |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 4789 | extent_num_bytes); |
Qu Wenruo | 92a7cc4 | 2020-05-15 14:01:40 +0800 | [diff] [blame] | 4790 | if (test_bit(BTRFS_ROOT_SHAREABLE, |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 4791 | &root->state) && |
| 4792 | extent_start != 0) |
Nikolay Borisov | 5074339 | 2020-11-02 16:48:55 +0200 | [diff] [blame] | 4793 | inode_sub_bytes(&inode->vfs_inode, |
| 4794 | num_dec); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4795 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4796 | } else { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4797 | extent_num_bytes = |
| 4798 | btrfs_file_extent_disk_num_bytes(leaf, |
| 4799 | fi); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4800 | extent_offset = found_key.offset - |
| 4801 | btrfs_file_extent_offset(leaf, fi); |
| 4802 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4803 | /* FIXME blocksize != 4096 */ |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 4804 | num_dec = btrfs_file_extent_num_bytes(leaf, fi); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4805 | if (extent_start != 0) { |
| 4806 | found_extent = 1; |
Qu Wenruo | 92a7cc4 | 2020-05-15 14:01:40 +0800 | [diff] [blame] | 4807 | if (test_bit(BTRFS_ROOT_SHAREABLE, |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 4808 | &root->state)) |
Nikolay Borisov | 5074339 | 2020-11-02 16:48:55 +0200 | [diff] [blame] | 4809 | inode_sub_bytes(&inode->vfs_inode, |
| 4810 | num_dec); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4811 | } |
| 4812 | } |
Josef Bacik | 9ddc959 | 2020-01-17 09:02:22 -0500 | [diff] [blame] | 4813 | clear_len = num_dec; |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 4814 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4815 | /* |
| 4816 | * we can't truncate inline items that have had |
| 4817 | * special encodings |
| 4818 | */ |
| 4819 | if (!del_item && |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4820 | btrfs_file_extent_encryption(leaf, fi) == 0 && |
Josef Bacik | ddfae63 | 2017-10-19 14:16:02 -0400 | [diff] [blame] | 4821 | btrfs_file_extent_other_encoding(leaf, fi) == 0 && |
| 4822 | btrfs_file_extent_compression(leaf, fi) == 0) { |
| 4823 | u32 size = (u32)(new_size - found_key.offset); |
Chris Mason | 514ac8a | 2014-01-03 21:07:00 -0800 | [diff] [blame] | 4824 | |
Josef Bacik | ddfae63 | 2017-10-19 14:16:02 -0400 | [diff] [blame] | 4825 | btrfs_set_file_extent_ram_bytes(leaf, fi, size); |
| 4826 | size = btrfs_file_extent_calc_inline_size(size); |
David Sterba | 78ac4f9 | 2019-03-20 14:49:12 +0100 | [diff] [blame] | 4827 | btrfs_truncate_item(path, size, 1); |
Josef Bacik | ddfae63 | 2017-10-19 14:16:02 -0400 | [diff] [blame] | 4828 | } else if (!del_item) { |
Chris Mason | 514ac8a | 2014-01-03 21:07:00 -0800 | [diff] [blame] | 4829 | /* |
Josef Bacik | ddfae63 | 2017-10-19 14:16:02 -0400 | [diff] [blame] | 4830 | * We have to bail so the last_size is set to |
| 4831 | * just before this extent. |
Chris Mason | 514ac8a | 2014-01-03 21:07:00 -0800 | [diff] [blame] | 4832 | */ |
Omar Sandoval | fd86a3a | 2018-05-11 13:13:30 -0700 | [diff] [blame] | 4833 | ret = NEED_TRUNCATE_BLOCK; |
Josef Bacik | ddfae63 | 2017-10-19 14:16:02 -0400 | [diff] [blame] | 4834 | break; |
Josef Bacik | 9ddc959 | 2020-01-17 09:02:22 -0500 | [diff] [blame] | 4835 | } else { |
| 4836 | /* |
| 4837 | * Inline extents are special, we just treat |
| 4838 | * them as a full sector worth in the file |
| 4839 | * extent tree just for simplicity sake. |
| 4840 | */ |
| 4841 | clear_len = fs_info->sectorsize; |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 4842 | } |
Josef Bacik | ddfae63 | 2017-10-19 14:16:02 -0400 | [diff] [blame] | 4843 | |
Qu Wenruo | 92a7cc4 | 2020-05-15 14:01:40 +0800 | [diff] [blame] | 4844 | if (test_bit(BTRFS_ROOT_SHAREABLE, &root->state)) |
Nikolay Borisov | 5074339 | 2020-11-02 16:48:55 +0200 | [diff] [blame] | 4845 | inode_sub_bytes(&inode->vfs_inode, |
| 4846 | item_end + 1 - new_size); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4847 | } |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4848 | delete: |
Josef Bacik | 9ddc959 | 2020-01-17 09:02:22 -0500 | [diff] [blame] | 4849 | /* |
| 4850 | * We use btrfs_truncate_inode_items() to clean up log trees for |
| 4851 | * multiple fsyncs, and in this case we don't want to clear the |
| 4852 | * file extent range because it's just the log. |
| 4853 | */ |
Nikolay Borisov | 5074339 | 2020-11-02 16:48:55 +0200 | [diff] [blame] | 4854 | if (root == inode->root) { |
| 4855 | ret = btrfs_inode_clear_file_extent_range(inode, |
Josef Bacik | 9ddc959 | 2020-01-17 09:02:22 -0500 | [diff] [blame] | 4856 | clear_start, clear_len); |
| 4857 | if (ret) { |
| 4858 | btrfs_abort_transaction(trans, ret); |
| 4859 | break; |
| 4860 | } |
| 4861 | } |
| 4862 | |
Josef Bacik | ddfae63 | 2017-10-19 14:16:02 -0400 | [diff] [blame] | 4863 | if (del_item) |
| 4864 | last_size = found_key.offset; |
| 4865 | else |
| 4866 | last_size = new_size; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4867 | if (del_item) { |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4868 | if (!pending_del_nr) { |
| 4869 | /* no pending yet, add ourselves */ |
| 4870 | pending_del_slot = path->slots[0]; |
| 4871 | pending_del_nr = 1; |
| 4872 | } else if (pending_del_nr && |
| 4873 | path->slots[0] + 1 == pending_del_slot) { |
| 4874 | /* hop on the pending chunk */ |
| 4875 | pending_del_nr++; |
| 4876 | pending_del_slot = path->slots[0]; |
| 4877 | } else { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4878 | BUG(); |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4879 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4880 | } else { |
| 4881 | break; |
| 4882 | } |
Thomas Meyer | 897ca81 | 2017-10-07 16:02:21 +0200 | [diff] [blame] | 4883 | should_throttle = false; |
Chris Mason | 28f75a0 | 2015-02-04 06:59:29 -0800 | [diff] [blame] | 4884 | |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 4885 | if (found_extent && |
Qu Wenruo | 82028e0a | 2020-05-15 14:01:41 +0800 | [diff] [blame] | 4886 | root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) { |
Qu Wenruo | ffd4bb2 | 2019-04-04 14:45:36 +0800 | [diff] [blame] | 4887 | struct btrfs_ref ref = { 0 }; |
| 4888 | |
Chris Mason | 28ed134 | 2014-12-17 09:41:04 -0800 | [diff] [blame] | 4889 | bytes_deleted += extent_num_bytes; |
Qu Wenruo | ffd4bb2 | 2019-04-04 14:45:36 +0800 | [diff] [blame] | 4890 | |
| 4891 | btrfs_init_generic_ref(&ref, BTRFS_DROP_DELAYED_REF, |
| 4892 | extent_start, extent_num_bytes, 0); |
| 4893 | ref.real_root = root->root_key.objectid; |
| 4894 | btrfs_init_data_ref(&ref, btrfs_header_owner(leaf), |
| 4895 | ino, extent_offset); |
| 4896 | ret = btrfs_free_extent(trans, &ref); |
Omar Sandoval | 0552210 | 2018-05-11 13:13:31 -0700 | [diff] [blame] | 4897 | if (ret) { |
| 4898 | btrfs_abort_transaction(trans, ret); |
| 4899 | break; |
| 4900 | } |
Chris Mason | 28f75a0 | 2015-02-04 06:59:29 -0800 | [diff] [blame] | 4901 | if (be_nice) { |
Lu Fengqi | 7c86162 | 2018-10-11 13:40:36 +0800 | [diff] [blame] | 4902 | if (btrfs_should_throttle_delayed_refs(trans)) |
Thomas Meyer | 897ca81 | 2017-10-07 16:02:21 +0200 | [diff] [blame] | 4903 | should_throttle = true; |
Chris Mason | 28f75a0 | 2015-02-04 06:59:29 -0800 | [diff] [blame] | 4904 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4905 | } |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4906 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4907 | if (found_type == BTRFS_INODE_ITEM_KEY) |
| 4908 | break; |
| 4909 | |
| 4910 | if (path->slots[0] == 0 || |
Josef Bacik | 1262133 | 2015-02-03 07:50:16 -0800 | [diff] [blame] | 4911 | path->slots[0] != pending_del_slot || |
Josef Bacik | 28bad21 | 2018-12-03 10:20:38 -0500 | [diff] [blame] | 4912 | should_throttle) { |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4913 | if (pending_del_nr) { |
| 4914 | ret = btrfs_del_items(trans, root, path, |
| 4915 | pending_del_slot, |
| 4916 | pending_del_nr); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4917 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 4918 | btrfs_abort_transaction(trans, ret); |
Omar Sandoval | fd86a3a | 2018-05-11 13:13:30 -0700 | [diff] [blame] | 4919 | break; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4920 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4921 | pending_del_nr = 0; |
| 4922 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4923 | btrfs_release_path(path); |
Josef Bacik | 28bad21 | 2018-12-03 10:20:38 -0500 | [diff] [blame] | 4924 | |
Chris Mason | 28f75a0 | 2015-02-04 06:59:29 -0800 | [diff] [blame] | 4925 | /* |
Josef Bacik | 28bad21 | 2018-12-03 10:20:38 -0500 | [diff] [blame] | 4926 | * We can generate a lot of delayed refs, so we need to |
| 4927 | * throttle every once and a while and make sure we're |
| 4928 | * adding enough space to keep up with the work we are |
| 4929 | * generating. Since we hold a transaction here we |
| 4930 | * can't flush, and we don't want to FLUSH_LIMIT because |
| 4931 | * we could have generated too many delayed refs to |
| 4932 | * actually allocate, so just bail if we're short and |
| 4933 | * let the normal reservation dance happen higher up. |
Chris Mason | 28f75a0 | 2015-02-04 06:59:29 -0800 | [diff] [blame] | 4934 | */ |
Josef Bacik | 28bad21 | 2018-12-03 10:20:38 -0500 | [diff] [blame] | 4935 | if (should_throttle) { |
| 4936 | ret = btrfs_delayed_refs_rsv_refill(fs_info, |
| 4937 | BTRFS_RESERVE_NO_FLUSH); |
| 4938 | if (ret) { |
| 4939 | ret = -EAGAIN; |
| 4940 | break; |
| 4941 | } |
Chris Mason | 28f75a0 | 2015-02-04 06:59:29 -0800 | [diff] [blame] | 4942 | } |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4943 | goto search_again; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4944 | } else { |
| 4945 | path->slots[0]--; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4946 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4947 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4948 | out: |
Omar Sandoval | fd86a3a | 2018-05-11 13:13:30 -0700 | [diff] [blame] | 4949 | if (ret >= 0 && pending_del_nr) { |
| 4950 | int err; |
| 4951 | |
| 4952 | err = btrfs_del_items(trans, root, path, pending_del_slot, |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4953 | pending_del_nr); |
Omar Sandoval | fd86a3a | 2018-05-11 13:13:30 -0700 | [diff] [blame] | 4954 | if (err) { |
| 4955 | btrfs_abort_transaction(trans, err); |
| 4956 | ret = err; |
| 4957 | } |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4958 | } |
Filipe Manana | 76b42ab | 2017-02-14 16:56:01 +0000 | [diff] [blame] | 4959 | if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) { |
| 4960 | ASSERT(last_size >= new_size); |
Omar Sandoval | fd86a3a | 2018-05-11 13:13:30 -0700 | [diff] [blame] | 4961 | if (!ret && last_size > new_size) |
Filipe Manana | 76b42ab | 2017-02-14 16:56:01 +0000 | [diff] [blame] | 4962 | last_size = new_size; |
Nikolay Borisov | 5074339 | 2020-11-02 16:48:55 +0200 | [diff] [blame] | 4963 | btrfs_inode_safe_disk_i_size_write(inode, last_size); |
| 4964 | unlock_extent_cached(&inode->io_tree, lock_start, (u64)-1, |
| 4965 | &cached_state); |
Filipe Manana | 76b42ab | 2017-02-14 16:56:01 +0000 | [diff] [blame] | 4966 | } |
Chris Mason | 28ed134 | 2014-12-17 09:41:04 -0800 | [diff] [blame] | 4967 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4968 | btrfs_free_path(path); |
Omar Sandoval | fd86a3a | 2018-05-11 13:13:30 -0700 | [diff] [blame] | 4969 | return ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4970 | } |
| 4971 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4972 | /* |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4973 | * btrfs_truncate_block - read, zero a chunk and write a block |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 4974 | * @inode - inode that we're zeroing |
| 4975 | * @from - the offset to start zeroing |
| 4976 | * @len - the length to zero, 0 to zero the entire range respective to the |
| 4977 | * offset |
| 4978 | * @front - zero up to the offset instead of from the offset on |
| 4979 | * |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4980 | * 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] | 4981 | * 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] | 4982 | */ |
Nikolay Borisov | 217f42e | 2020-11-02 16:49:03 +0200 | [diff] [blame] | 4983 | int btrfs_truncate_block(struct btrfs_inode *inode, loff_t from, loff_t len, |
| 4984 | int front) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4985 | { |
Nikolay Borisov | 217f42e | 2020-11-02 16:49:03 +0200 | [diff] [blame] | 4986 | struct btrfs_fs_info *fs_info = inode->root->fs_info; |
| 4987 | struct address_space *mapping = inode->vfs_inode.i_mapping; |
| 4988 | struct extent_io_tree *io_tree = &inode->io_tree; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4989 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4990 | struct extent_state *cached_state = NULL; |
Qu Wenruo | 364ecf3 | 2017-02-27 15:10:38 +0800 | [diff] [blame] | 4991 | struct extent_changeset *data_reserved = NULL; |
Qu Wenruo | 6d4572a | 2020-06-24 07:23:50 +0800 | [diff] [blame] | 4992 | bool only_release_metadata = false; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4993 | u32 blocksize = fs_info->sectorsize; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 4994 | pgoff_t index = from >> PAGE_SHIFT; |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4995 | unsigned offset = from & (blocksize - 1); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4996 | struct page *page; |
Josef Bacik | 3b16a4e | 2011-09-21 15:05:58 -0400 | [diff] [blame] | 4997 | gfp_t mask = btrfs_alloc_write_mask(mapping); |
Qu Wenruo | 6d4572a | 2020-06-24 07:23:50 +0800 | [diff] [blame] | 4998 | size_t write_bytes = blocksize; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4999 | int ret = 0; |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 5000 | u64 block_start; |
| 5001 | u64 block_end; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5002 | |
Nikolay Borisov | b03ebd9 | 2018-01-18 14:47:06 +0200 | [diff] [blame] | 5003 | if (IS_ALIGNED(offset, blocksize) && |
| 5004 | (!len || IS_ALIGNED(len, blocksize))) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5005 | goto out; |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 5006 | |
Josef Bacik | 8b62f87 | 2017-10-19 14:15:55 -0400 | [diff] [blame] | 5007 | block_start = round_down(from, blocksize); |
| 5008 | block_end = block_start + blocksize - 1; |
| 5009 | |
Nikolay Borisov | 217f42e | 2020-11-02 16:49:03 +0200 | [diff] [blame] | 5010 | ret = btrfs_check_data_free_space(inode, &data_reserved, block_start, |
| 5011 | blocksize); |
Qu Wenruo | 6d4572a | 2020-06-24 07:23:50 +0800 | [diff] [blame] | 5012 | if (ret < 0) { |
Nikolay Borisov | 217f42e | 2020-11-02 16:49:03 +0200 | [diff] [blame] | 5013 | if (btrfs_check_nocow_lock(inode, block_start, &write_bytes) > 0) { |
Qu Wenruo | 6d4572a | 2020-06-24 07:23:50 +0800 | [diff] [blame] | 5014 | /* For nocow case, no need to reserve data space */ |
| 5015 | only_release_metadata = true; |
| 5016 | } else { |
| 5017 | goto out; |
| 5018 | } |
| 5019 | } |
Nikolay Borisov | 217f42e | 2020-11-02 16:49:03 +0200 | [diff] [blame] | 5020 | ret = btrfs_delalloc_reserve_metadata(inode, blocksize); |
Qu Wenruo | 6d4572a | 2020-06-24 07:23:50 +0800 | [diff] [blame] | 5021 | if (ret < 0) { |
| 5022 | if (!only_release_metadata) |
Nikolay Borisov | 217f42e | 2020-11-02 16:49:03 +0200 | [diff] [blame] | 5023 | btrfs_free_reserved_data_space(inode, data_reserved, |
| 5024 | block_start, blocksize); |
Qu Wenruo | 6d4572a | 2020-06-24 07:23:50 +0800 | [diff] [blame] | 5025 | goto out; |
| 5026 | } |
Chris Mason | 211c17f | 2008-05-15 09:13:45 -0400 | [diff] [blame] | 5027 | again: |
Josef Bacik | 3b16a4e | 2011-09-21 15:05:58 -0400 | [diff] [blame] | 5028 | page = find_or_create_page(mapping, index, mask); |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 5029 | if (!page) { |
Nikolay Borisov | 217f42e | 2020-11-02 16:49:03 +0200 | [diff] [blame] | 5030 | btrfs_delalloc_release_space(inode, data_reserved, block_start, |
| 5031 | blocksize, true); |
| 5032 | btrfs_delalloc_release_extents(inode, blocksize); |
Miao Xie | ac6a2b3 | 2012-12-05 10:56:13 +0000 | [diff] [blame] | 5033 | ret = -ENOMEM; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5034 | goto out; |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 5035 | } |
Qu Wenruo | 32443de | 2021-01-26 16:34:00 +0800 | [diff] [blame] | 5036 | ret = set_page_extent_mapped(page); |
| 5037 | if (ret < 0) |
| 5038 | goto out_unlock; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5039 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5040 | if (!PageUptodate(page)) { |
| 5041 | ret = btrfs_readpage(NULL, page); |
| 5042 | lock_page(page); |
Chris Mason | 211c17f | 2008-05-15 09:13:45 -0400 | [diff] [blame] | 5043 | if (page->mapping != mapping) { |
| 5044 | unlock_page(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5045 | put_page(page); |
Chris Mason | 211c17f | 2008-05-15 09:13:45 -0400 | [diff] [blame] | 5046 | goto again; |
| 5047 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5048 | if (!PageUptodate(page)) { |
| 5049 | ret = -EIO; |
Chris Mason | 8964222 | 2008-07-24 09:41:53 -0400 | [diff] [blame] | 5050 | goto out_unlock; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5051 | } |
| 5052 | } |
Chris Mason | 211c17f | 2008-05-15 09:13:45 -0400 | [diff] [blame] | 5053 | wait_on_page_writeback(page); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5054 | |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 5055 | lock_extent_bits(io_tree, block_start, block_end, &cached_state); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5056 | |
Nikolay Borisov | 217f42e | 2020-11-02 16:49:03 +0200 | [diff] [blame] | 5057 | ordered = btrfs_lookup_ordered_extent(inode, block_start); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5058 | if (ordered) { |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 5059 | unlock_extent_cached(io_tree, block_start, block_end, |
David Sterba | e43bbe5 | 2017-12-12 21:43:52 +0100 | [diff] [blame] | 5060 | &cached_state); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5061 | unlock_page(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5062 | put_page(page); |
Nikolay Borisov | c0a4360 | 2020-09-18 12:15:53 +0300 | [diff] [blame] | 5063 | btrfs_start_ordered_extent(ordered, 1); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5064 | btrfs_put_ordered_extent(ordered); |
| 5065 | goto again; |
| 5066 | } |
| 5067 | |
Nikolay Borisov | 217f42e | 2020-11-02 16:49:03 +0200 | [diff] [blame] | 5068 | clear_extent_bit(&inode->io_tree, block_start, block_end, |
Omar Sandoval | e182163 | 2019-08-15 14:04:04 -0700 | [diff] [blame] | 5069 | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, |
| 5070 | 0, 0, &cached_state); |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 5071 | |
Nikolay Borisov | 217f42e | 2020-11-02 16:49:03 +0200 | [diff] [blame] | 5072 | ret = btrfs_set_extent_delalloc(inode, block_start, block_end, 0, |
Nikolay Borisov | 330a582 | 2019-07-17 16:18:17 +0300 | [diff] [blame] | 5073 | &cached_state); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 5074 | if (ret) { |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 5075 | unlock_extent_cached(io_tree, block_start, block_end, |
David Sterba | e43bbe5 | 2017-12-12 21:43:52 +0100 | [diff] [blame] | 5076 | &cached_state); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 5077 | goto out_unlock; |
| 5078 | } |
| 5079 | |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 5080 | if (offset != blocksize) { |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 5081 | if (!len) |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 5082 | len = blocksize - offset; |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 5083 | if (front) |
Ira Weiny | d048b9c | 2021-05-04 18:40:07 -0700 | [diff] [blame] | 5084 | memzero_page(page, (block_start - page_offset(page)), |
| 5085 | offset); |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 5086 | else |
Ira Weiny | d048b9c | 2021-05-04 18:40:07 -0700 | [diff] [blame] | 5087 | memzero_page(page, (block_start - page_offset(page)) + offset, |
| 5088 | len); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5089 | flush_dcache_page(page); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5090 | } |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 5091 | ClearPageChecked(page); |
Qu Wenruo | 6c9ac8b | 2021-05-31 16:50:51 +0800 | [diff] [blame] | 5092 | 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] | 5093 | unlock_extent_cached(io_tree, block_start, block_end, &cached_state); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5094 | |
Qu Wenruo | 6d4572a | 2020-06-24 07:23:50 +0800 | [diff] [blame] | 5095 | if (only_release_metadata) |
Nikolay Borisov | 217f42e | 2020-11-02 16:49:03 +0200 | [diff] [blame] | 5096 | set_extent_bit(&inode->io_tree, block_start, block_end, |
Nikolay Borisov | 1cab5e7 | 2020-11-05 11:08:00 +0200 | [diff] [blame] | 5097 | EXTENT_NORESERVE, 0, NULL, NULL, GFP_NOFS, NULL); |
Qu Wenruo | 6d4572a | 2020-06-24 07:23:50 +0800 | [diff] [blame] | 5098 | |
Chris Mason | 8964222 | 2008-07-24 09:41:53 -0400 | [diff] [blame] | 5099 | out_unlock: |
Qu Wenruo | 6d4572a | 2020-06-24 07:23:50 +0800 | [diff] [blame] | 5100 | if (ret) { |
| 5101 | if (only_release_metadata) |
Nikolay Borisov | 217f42e | 2020-11-02 16:49:03 +0200 | [diff] [blame] | 5102 | btrfs_delalloc_release_metadata(inode, blocksize, true); |
Qu Wenruo | 6d4572a | 2020-06-24 07:23:50 +0800 | [diff] [blame] | 5103 | else |
Nikolay Borisov | 217f42e | 2020-11-02 16:49:03 +0200 | [diff] [blame] | 5104 | btrfs_delalloc_release_space(inode, data_reserved, |
Qu Wenruo | 6d4572a | 2020-06-24 07:23:50 +0800 | [diff] [blame] | 5105 | block_start, blocksize, true); |
| 5106 | } |
Nikolay Borisov | 217f42e | 2020-11-02 16:49:03 +0200 | [diff] [blame] | 5107 | btrfs_delalloc_release_extents(inode, blocksize); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5108 | unlock_page(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5109 | put_page(page); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5110 | out: |
Qu Wenruo | 6d4572a | 2020-06-24 07:23:50 +0800 | [diff] [blame] | 5111 | if (only_release_metadata) |
Nikolay Borisov | 217f42e | 2020-11-02 16:49:03 +0200 | [diff] [blame] | 5112 | btrfs_check_nocow_unlock(inode); |
Qu Wenruo | 364ecf3 | 2017-02-27 15:10:38 +0800 | [diff] [blame] | 5113 | extent_changeset_free(data_reserved); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5114 | return ret; |
| 5115 | } |
| 5116 | |
Nikolay Borisov | a4ba6cc | 2020-11-02 16:49:00 +0200 | [diff] [blame] | 5117 | 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] | 5118 | u64 offset, u64 len) |
| 5119 | { |
Nikolay Borisov | a4ba6cc | 2020-11-02 16:49:00 +0200 | [diff] [blame] | 5120 | struct btrfs_fs_info *fs_info = root->fs_info; |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 5121 | struct btrfs_trans_handle *trans; |
Filipe Manana | 5893dfb | 2020-11-04 11:07:32 +0000 | [diff] [blame] | 5122 | struct btrfs_drop_extents_args drop_args = { 0 }; |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 5123 | int ret; |
| 5124 | |
| 5125 | /* |
Filipe Manana | cceaa89 | 2021-07-20 16:03:40 +0100 | [diff] [blame] | 5126 | * If NO_HOLES is enabled, we don't need to do anything. |
| 5127 | * Later, up in the call chain, either btrfs_set_inode_last_sub_trans() |
| 5128 | * or btrfs_update_inode() will be called, which guarantee that the next |
| 5129 | * fsync will know this inode was changed and needs to be logged. |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 5130 | */ |
Filipe Manana | cceaa89 | 2021-07-20 16:03:40 +0100 | [diff] [blame] | 5131 | if (btrfs_fs_incompat(fs_info, NO_HOLES)) |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 5132 | return 0; |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 5133 | |
| 5134 | /* |
| 5135 | * 1 - for the one we're dropping |
| 5136 | * 1 - for the one we're adding |
| 5137 | * 1 - for updating the inode. |
| 5138 | */ |
| 5139 | trans = btrfs_start_transaction(root, 3); |
| 5140 | if (IS_ERR(trans)) |
| 5141 | return PTR_ERR(trans); |
| 5142 | |
Filipe Manana | 5893dfb | 2020-11-04 11:07:32 +0000 | [diff] [blame] | 5143 | drop_args.start = offset; |
| 5144 | drop_args.end = offset + len; |
| 5145 | drop_args.drop_cache = true; |
| 5146 | |
Nikolay Borisov | a4ba6cc | 2020-11-02 16:49:00 +0200 | [diff] [blame] | 5147 | ret = btrfs_drop_extents(trans, root, inode, &drop_args); |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 5148 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 5149 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 5150 | btrfs_end_transaction(trans); |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 5151 | return ret; |
| 5152 | } |
| 5153 | |
Nikolay Borisov | a4ba6cc | 2020-11-02 16:49:00 +0200 | [diff] [blame] | 5154 | ret = btrfs_insert_file_extent(trans, root, btrfs_ino(inode), |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 5155 | offset, 0, 0, len, 0, len, 0, 0, 0); |
Filipe Manana | 2766ff6 | 2020-11-04 11:07:34 +0000 | [diff] [blame] | 5156 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 5157 | btrfs_abort_transaction(trans, ret); |
Filipe Manana | 2766ff6 | 2020-11-04 11:07:34 +0000 | [diff] [blame] | 5158 | } else { |
Nikolay Borisov | a4ba6cc | 2020-11-02 16:49:00 +0200 | [diff] [blame] | 5159 | btrfs_update_inode_bytes(inode, 0, drop_args.bytes_found); |
| 5160 | btrfs_update_inode(trans, root, inode); |
Filipe Manana | 2766ff6 | 2020-11-04 11:07:34 +0000 | [diff] [blame] | 5161 | } |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 5162 | btrfs_end_transaction(trans); |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 5163 | return ret; |
| 5164 | } |
| 5165 | |
Josef Bacik | 695a0d0 | 2011-03-04 15:46:53 -0500 | [diff] [blame] | 5166 | /* |
| 5167 | * This function puts in dummy file extents for the area we're creating a hole |
| 5168 | * for. So if we are truncating this file to a larger size we need to insert |
| 5169 | * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for |
| 5170 | * the range between oldsize and size |
| 5171 | */ |
Nikolay Borisov | b06359a | 2020-11-02 16:49:04 +0200 | [diff] [blame] | 5172 | 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] | 5173 | { |
Nikolay Borisov | b06359a | 2020-11-02 16:49:04 +0200 | [diff] [blame] | 5174 | struct btrfs_root *root = inode->root; |
| 5175 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 5176 | struct extent_io_tree *io_tree = &inode->io_tree; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 5177 | struct extent_map *em = NULL; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 5178 | struct extent_state *cached_state = NULL; |
Nikolay Borisov | b06359a | 2020-11-02 16:49:04 +0200 | [diff] [blame] | 5179 | struct extent_map_tree *em_tree = &inode->extent_tree; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5180 | u64 hole_start = ALIGN(oldsize, fs_info->sectorsize); |
| 5181 | u64 block_end = ALIGN(size, fs_info->sectorsize); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 5182 | u64 last_byte; |
| 5183 | u64 cur_offset; |
| 5184 | u64 hole_size; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 5185 | int err = 0; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 5186 | |
Josef Bacik | a71754f | 2013-06-17 17:14:39 -0400 | [diff] [blame] | 5187 | /* |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 5188 | * If our size started in the middle of a block we need to zero out the |
| 5189 | * 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] | 5190 | * expose stale data. |
| 5191 | */ |
Nikolay Borisov | b06359a | 2020-11-02 16:49:04 +0200 | [diff] [blame] | 5192 | err = btrfs_truncate_block(inode, oldsize, 0, 0); |
Josef Bacik | a71754f | 2013-06-17 17:14:39 -0400 | [diff] [blame] | 5193 | if (err) |
| 5194 | return err; |
| 5195 | |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 5196 | if (size <= hole_start) |
| 5197 | return 0; |
| 5198 | |
Nikolay Borisov | b06359a | 2020-11-02 16:49:04 +0200 | [diff] [blame] | 5199 | btrfs_lock_and_flush_ordered_range(inode, hole_start, block_end - 1, |
| 5200 | &cached_state); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 5201 | cur_offset = hole_start; |
| 5202 | while (1) { |
Nikolay Borisov | b06359a | 2020-11-02 16:49:04 +0200 | [diff] [blame] | 5203 | em = btrfs_get_extent(inode, NULL, 0, cur_offset, |
Omar Sandoval | 39b07b5 | 2019-12-02 17:34:23 -0800 | [diff] [blame] | 5204 | block_end - cur_offset); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5205 | if (IS_ERR(em)) { |
| 5206 | err = PTR_ERR(em); |
Zach Brown | f276795 | 2013-01-08 19:37:58 +0000 | [diff] [blame] | 5207 | em = NULL; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5208 | break; |
| 5209 | } |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 5210 | last_byte = min(extent_map_end(em), block_end); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5211 | last_byte = ALIGN(last_byte, fs_info->sectorsize); |
Josef Bacik | 9ddc959 | 2020-01-17 09:02:22 -0500 | [diff] [blame] | 5212 | hole_size = last_byte - cur_offset; |
| 5213 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5214 | if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) { |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 5215 | struct extent_map *hole_em; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5216 | |
Nikolay Borisov | b06359a | 2020-11-02 16:49:04 +0200 | [diff] [blame] | 5217 | err = maybe_insert_hole(root, inode, cur_offset, |
| 5218 | hole_size); |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 5219 | if (err) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 5220 | break; |
Josef Bacik | 9ddc959 | 2020-01-17 09:02:22 -0500 | [diff] [blame] | 5221 | |
Nikolay Borisov | b06359a | 2020-11-02 16:49:04 +0200 | [diff] [blame] | 5222 | err = btrfs_inode_set_file_extent_range(inode, |
Josef Bacik | 9ddc959 | 2020-01-17 09:02:22 -0500 | [diff] [blame] | 5223 | cur_offset, hole_size); |
| 5224 | if (err) |
| 5225 | break; |
| 5226 | |
Nikolay Borisov | b06359a | 2020-11-02 16:49:04 +0200 | [diff] [blame] | 5227 | btrfs_drop_extent_cache(inode, cur_offset, |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 5228 | cur_offset + hole_size - 1, 0); |
| 5229 | hole_em = alloc_extent_map(); |
| 5230 | if (!hole_em) { |
| 5231 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, |
Nikolay Borisov | b06359a | 2020-11-02 16:49:04 +0200 | [diff] [blame] | 5232 | &inode->runtime_flags); |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 5233 | goto next; |
| 5234 | } |
| 5235 | hole_em->start = cur_offset; |
| 5236 | hole_em->len = hole_size; |
| 5237 | hole_em->orig_start = cur_offset; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5238 | |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 5239 | hole_em->block_start = EXTENT_MAP_HOLE; |
| 5240 | hole_em->block_len = 0; |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 5241 | hole_em->orig_block_len = 0; |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 5242 | hole_em->ram_bytes = hole_size; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 5243 | hole_em->compress_type = BTRFS_COMPRESS_NONE; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5244 | hole_em->generation = fs_info->generation; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 5245 | |
| 5246 | while (1) { |
| 5247 | write_lock(&em_tree->lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 5248 | err = add_extent_mapping(em_tree, hole_em, 1); |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 5249 | write_unlock(&em_tree->lock); |
| 5250 | if (err != -EEXIST) |
| 5251 | break; |
Nikolay Borisov | b06359a | 2020-11-02 16:49:04 +0200 | [diff] [blame] | 5252 | btrfs_drop_extent_cache(inode, cur_offset, |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 5253 | cur_offset + |
| 5254 | hole_size - 1, 0); |
| 5255 | } |
| 5256 | free_extent_map(hole_em); |
Josef Bacik | 9ddc959 | 2020-01-17 09:02:22 -0500 | [diff] [blame] | 5257 | } else { |
Nikolay Borisov | b06359a | 2020-11-02 16:49:04 +0200 | [diff] [blame] | 5258 | err = btrfs_inode_set_file_extent_range(inode, |
Josef Bacik | 9ddc959 | 2020-01-17 09:02:22 -0500 | [diff] [blame] | 5259 | cur_offset, hole_size); |
| 5260 | if (err) |
| 5261 | break; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 5262 | } |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 5263 | next: |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 5264 | free_extent_map(em); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 5265 | em = NULL; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 5266 | cur_offset = last_byte; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5267 | if (cur_offset >= block_end) |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 5268 | break; |
| 5269 | } |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 5270 | free_extent_map(em); |
David Sterba | e43bbe5 | 2017-12-12 21:43:52 +0100 | [diff] [blame] | 5271 | unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 5272 | return err; |
| 5273 | } |
| 5274 | |
Eric Sandeen | 3972f26 | 2013-01-12 02:57:22 +0000 | [diff] [blame] | 5275 | static int btrfs_setsize(struct inode *inode, struct iattr *attr) |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5276 | { |
Miao Xie | f4a2f4c | 2011-12-14 20:12:01 -0500 | [diff] [blame] | 5277 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5278 | struct btrfs_trans_handle *trans; |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 5279 | loff_t oldsize = i_size_read(inode); |
Eric Sandeen | 3972f26 | 2013-01-12 02:57:22 +0000 | [diff] [blame] | 5280 | loff_t newsize = attr->ia_size; |
| 5281 | int mask = attr->ia_valid; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5282 | int ret; |
| 5283 | |
Eric Sandeen | 3972f26 | 2013-01-12 02:57:22 +0000 | [diff] [blame] | 5284 | /* |
| 5285 | * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a |
| 5286 | * special case where we need to update the times despite not having |
| 5287 | * these flags set. For all other operations the VFS set these flags |
| 5288 | * explicitly if it wants a timestamp update. |
| 5289 | */ |
Christoph Hellwig | dff6efc | 2013-11-19 07:17:07 -0800 | [diff] [blame] | 5290 | if (newsize != oldsize) { |
| 5291 | inode_inc_iversion(inode); |
| 5292 | if (!(mask & (ATTR_CTIME | ATTR_MTIME))) |
| 5293 | inode->i_ctime = inode->i_mtime = |
Deepa Dinamani | c2050a4 | 2016-09-14 07:48:06 -0700 | [diff] [blame] | 5294 | current_time(inode); |
Christoph Hellwig | dff6efc | 2013-11-19 07:17:07 -0800 | [diff] [blame] | 5295 | } |
Eric Sandeen | 3972f26 | 2013-01-12 02:57:22 +0000 | [diff] [blame] | 5296 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 5297 | if (newsize > oldsize) { |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 5298 | /* |
David Sterba | ea14b57f | 2017-06-22 02:19:11 +0200 | [diff] [blame] | 5299 | * Don't do an expanding truncate while snapshotting is ongoing. |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 5300 | * This is to ensure the snapshot captures a fully consistent |
| 5301 | * state of this file - if the snapshot captures this expanding |
| 5302 | * truncation, it must capture all writes that happened before |
| 5303 | * this truncation. |
| 5304 | */ |
Nikolay Borisov | dcc3eb9 | 2020-01-30 14:59:45 +0200 | [diff] [blame] | 5305 | btrfs_drew_write_lock(&root->snapshot_lock); |
Nikolay Borisov | b06359a | 2020-11-02 16:49:04 +0200 | [diff] [blame] | 5306 | ret = btrfs_cont_expand(BTRFS_I(inode), oldsize, newsize); |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 5307 | if (ret) { |
Nikolay Borisov | dcc3eb9 | 2020-01-30 14:59:45 +0200 | [diff] [blame] | 5308 | btrfs_drew_write_unlock(&root->snapshot_lock); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5309 | return ret; |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 5310 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5311 | |
Miao Xie | f4a2f4c | 2011-12-14 20:12:01 -0500 | [diff] [blame] | 5312 | trans = btrfs_start_transaction(root, 1); |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 5313 | if (IS_ERR(trans)) { |
Nikolay Borisov | dcc3eb9 | 2020-01-30 14:59:45 +0200 | [diff] [blame] | 5314 | btrfs_drew_write_unlock(&root->snapshot_lock); |
Miao Xie | f4a2f4c | 2011-12-14 20:12:01 -0500 | [diff] [blame] | 5315 | return PTR_ERR(trans); |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 5316 | } |
Miao Xie | f4a2f4c | 2011-12-14 20:12:01 -0500 | [diff] [blame] | 5317 | |
| 5318 | i_size_write(inode, newsize); |
Nikolay Borisov | 76aea53 | 2020-11-02 16:48:53 +0200 | [diff] [blame] | 5319 | btrfs_inode_safe_disk_i_size_write(BTRFS_I(inode), 0); |
Chandan Rajendra | 27772b6 | 2016-01-21 15:56:03 +0530 | [diff] [blame] | 5320 | pagecache_isize_extended(inode, oldsize, newsize); |
Nikolay Borisov | 9a56fcd | 2020-11-02 16:48:59 +0200 | [diff] [blame] | 5321 | ret = btrfs_update_inode(trans, root, BTRFS_I(inode)); |
Nikolay Borisov | dcc3eb9 | 2020-01-30 14:59:45 +0200 | [diff] [blame] | 5322 | btrfs_drew_write_unlock(&root->snapshot_lock); |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 5323 | btrfs_end_transaction(trans); |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 5324 | } else { |
Naohiro Aota | 24c0a72 | 2021-02-04 19:22:09 +0900 | [diff] [blame] | 5325 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
| 5326 | |
| 5327 | if (btrfs_is_zoned(fs_info)) { |
| 5328 | ret = btrfs_wait_ordered_range(inode, |
| 5329 | ALIGN(newsize, fs_info->sectorsize), |
| 5330 | (u64)-1); |
| 5331 | if (ret) |
| 5332 | return ret; |
| 5333 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5334 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 5335 | /* |
| 5336 | * 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] | 5337 | * zero. Make sure any new writes to the file get on disk |
| 5338 | * on close. |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 5339 | */ |
| 5340 | if (newsize == 0) |
Nikolay Borisov | 1fd4033 | 2020-10-01 09:40:39 +0300 | [diff] [blame] | 5341 | set_bit(BTRFS_INODE_FLUSH_ON_CLOSE, |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 5342 | &BTRFS_I(inode)->runtime_flags); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5343 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 5344 | truncate_setsize(inode, newsize); |
Miao Xie | 2e60a51 | 2013-02-08 07:01:08 +0000 | [diff] [blame] | 5345 | |
Miao Xie | 2e60a51 | 2013-02-08 07:01:08 +0000 | [diff] [blame] | 5346 | inode_dio_wait(inode); |
Miao Xie | 2e60a51 | 2013-02-08 07:01:08 +0000 | [diff] [blame] | 5347 | |
Filipe Manana | 213e8c5 | 2018-02-06 20:40:31 +0000 | [diff] [blame] | 5348 | ret = btrfs_truncate(inode, newsize == oldsize); |
Josef Bacik | 7f4f6e0 | 2013-08-29 16:43:28 -0400 | [diff] [blame] | 5349 | if (ret && inode->i_nlink) { |
| 5350 | int err; |
| 5351 | |
| 5352 | /* |
Omar Sandoval | f7e9e8f | 2018-05-11 13:13:32 -0700 | [diff] [blame] | 5353 | * Truncate failed, so fix up the in-memory size. We |
| 5354 | * adjusted disk_i_size down as we removed extents, so |
| 5355 | * wait for disk_i_size to be stable and then update the |
| 5356 | * in-memory size to match. |
Josef Bacik | 7f4f6e0 | 2013-08-29 16:43:28 -0400 | [diff] [blame] | 5357 | */ |
Omar Sandoval | f7e9e8f | 2018-05-11 13:13:32 -0700 | [diff] [blame] | 5358 | err = btrfs_wait_ordered_range(inode, 0, (u64)-1); |
Josef Bacik | 7f4f6e0 | 2013-08-29 16:43:28 -0400 | [diff] [blame] | 5359 | if (err) |
Omar Sandoval | f7e9e8f | 2018-05-11 13:13:32 -0700 | [diff] [blame] | 5360 | return err; |
| 5361 | i_size_write(inode, BTRFS_I(inode)->disk_i_size); |
Josef Bacik | 7f4f6e0 | 2013-08-29 16:43:28 -0400 | [diff] [blame] | 5362 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5363 | } |
| 5364 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 5365 | return ret; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5366 | } |
| 5367 | |
Christian Brauner | 549c729 | 2021-01-21 14:19:43 +0100 | [diff] [blame] | 5368 | static int btrfs_setattr(struct user_namespace *mnt_userns, struct dentry *dentry, |
| 5369 | struct iattr *attr) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5370 | { |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 5371 | struct inode *inode = d_inode(dentry); |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 5372 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5373 | int err; |
| 5374 | |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 5375 | if (btrfs_root_readonly(root)) |
| 5376 | return -EROFS; |
| 5377 | |
Christian Brauner | 2f221d6 | 2021-01-21 14:19:26 +0100 | [diff] [blame] | 5378 | err = setattr_prepare(&init_user_ns, dentry, attr); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5379 | if (err) |
| 5380 | return err; |
| 5381 | |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 5382 | if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) { |
Eric Sandeen | 3972f26 | 2013-01-12 02:57:22 +0000 | [diff] [blame] | 5383 | err = btrfs_setsize(inode, attr); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5384 | if (err) |
| 5385 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5386 | } |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 5387 | |
Christoph Hellwig | 1025774 | 2010-06-04 11:30:02 +0200 | [diff] [blame] | 5388 | if (attr->ia_valid) { |
Christian Brauner | 2f221d6 | 2021-01-21 14:19:26 +0100 | [diff] [blame] | 5389 | setattr_copy(&init_user_ns, inode, attr); |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 5390 | inode_inc_iversion(inode); |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 5391 | err = btrfs_dirty_inode(inode); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 5392 | |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 5393 | if (!err && attr->ia_valid & ATTR_MODE) |
Christian Brauner | e65ce2a | 2021-01-21 14:19:27 +0100 | [diff] [blame] | 5394 | err = posix_acl_chmod(&init_user_ns, inode, |
| 5395 | inode->i_mode); |
Christoph Hellwig | 1025774 | 2010-06-04 11:30:02 +0200 | [diff] [blame] | 5396 | } |
| 5397 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5398 | return err; |
| 5399 | } |
Chris Mason | 61295eb | 2008-01-14 16:24:38 -0500 | [diff] [blame] | 5400 | |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5401 | /* |
| 5402 | * While truncating the inode pages during eviction, we get the VFS calling |
| 5403 | * btrfs_invalidatepage() against each page of the inode. This is slow because |
| 5404 | * the calls to btrfs_invalidatepage() result in a huge amount of calls to |
| 5405 | * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting |
| 5406 | * extent_state structures over and over, wasting lots of time. |
| 5407 | * |
| 5408 | * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all |
| 5409 | * those expensive operations on a per page basis and do only the ordered io |
| 5410 | * finishing, while we release here the extent_map and extent_state structures, |
| 5411 | * without the excessive merging and splitting. |
| 5412 | */ |
| 5413 | static void evict_inode_truncate_pages(struct inode *inode) |
| 5414 | { |
| 5415 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
| 5416 | struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree; |
| 5417 | struct rb_node *node; |
| 5418 | |
| 5419 | ASSERT(inode->i_state & I_FREEING); |
Johannes Weiner | 91b0abe | 2014-04-03 14:47:49 -0700 | [diff] [blame] | 5420 | truncate_inode_pages_final(&inode->i_data); |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5421 | |
| 5422 | write_lock(&map_tree->lock); |
Liu Bo | 07e1ce0 | 2018-08-23 03:51:52 +0800 | [diff] [blame] | 5423 | while (!RB_EMPTY_ROOT(&map_tree->map.rb_root)) { |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5424 | struct extent_map *em; |
| 5425 | |
Liu Bo | 07e1ce0 | 2018-08-23 03:51:52 +0800 | [diff] [blame] | 5426 | node = rb_first_cached(&map_tree->map); |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5427 | em = rb_entry(node, struct extent_map, rb_node); |
Wang Shilong | 180589e | 2013-12-14 15:27:31 +0800 | [diff] [blame] | 5428 | clear_bit(EXTENT_FLAG_PINNED, &em->flags); |
| 5429 | clear_bit(EXTENT_FLAG_LOGGING, &em->flags); |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5430 | remove_extent_mapping(map_tree, em); |
| 5431 | free_extent_map(em); |
Filipe Manana | 7064dd5 | 2014-08-08 02:47:05 +0100 | [diff] [blame] | 5432 | if (need_resched()) { |
| 5433 | write_unlock(&map_tree->lock); |
| 5434 | cond_resched(); |
| 5435 | write_lock(&map_tree->lock); |
| 5436 | } |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5437 | } |
| 5438 | write_unlock(&map_tree->lock); |
| 5439 | |
Filipe Manana | 6ca0709 | 2015-05-26 00:55:42 +0100 | [diff] [blame] | 5440 | /* |
| 5441 | * 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] | 5442 | * We can have ongoing bios started by readahead that have |
| 5443 | * their endio callback (extent_io.c:end_bio_extent_readpage) |
Filipe Manana | 9c6429d | 2015-06-10 12:55:41 +0100 | [diff] [blame] | 5444 | * still in progress (unlocked the pages in the bio but did not yet |
| 5445 | * unlocked the ranges in the io tree). Therefore this means some |
Filipe Manana | 6ca0709 | 2015-05-26 00:55:42 +0100 | [diff] [blame] | 5446 | * ranges can still be locked and eviction started because before |
| 5447 | * submitting those bios, which are executed by a separate task (work |
| 5448 | * queue kthread), inode references (inode->i_count) were not taken |
| 5449 | * (which would be dropped in the end io callback of each bio). |
| 5450 | * Therefore here we effectively end up waiting for those bios and |
| 5451 | * anyone else holding locked ranges without having bumped the inode's |
| 5452 | * reference count - if we don't do it, when they access the inode's |
| 5453 | * io_tree to unlock a range it may be too late, leading to an |
| 5454 | * use-after-free issue. |
| 5455 | */ |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5456 | spin_lock(&io_tree->lock); |
| 5457 | while (!RB_EMPTY_ROOT(&io_tree->state)) { |
| 5458 | struct extent_state *state; |
| 5459 | struct extent_state *cached_state = NULL; |
Filipe Manana | 6ca0709 | 2015-05-26 00:55:42 +0100 | [diff] [blame] | 5460 | u64 start; |
| 5461 | u64 end; |
Filipe Manana | 421f092 | 2018-10-12 13:02:48 +0100 | [diff] [blame] | 5462 | unsigned state_flags; |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5463 | |
| 5464 | node = rb_first(&io_tree->state); |
| 5465 | state = rb_entry(node, struct extent_state, rb_node); |
Filipe Manana | 6ca0709 | 2015-05-26 00:55:42 +0100 | [diff] [blame] | 5466 | start = state->start; |
| 5467 | end = state->end; |
Filipe Manana | 421f092 | 2018-10-12 13:02:48 +0100 | [diff] [blame] | 5468 | state_flags = state->state; |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5469 | spin_unlock(&io_tree->lock); |
| 5470 | |
David Sterba | ff13db4 | 2015-12-03 14:30:40 +0100 | [diff] [blame] | 5471 | lock_extent_bits(io_tree, start, end, &cached_state); |
Qu Wenruo | b9d0b38 | 2015-09-29 10:35:16 +0800 | [diff] [blame] | 5472 | |
| 5473 | /* |
| 5474 | * If still has DELALLOC flag, the extent didn't reach disk, |
| 5475 | * and its reserved space won't be freed by delayed_ref. |
| 5476 | * So we need to free its reserved space here. |
| 5477 | * (Refer to comment in btrfs_invalidatepage, case 2) |
| 5478 | * |
| 5479 | * Note, end is the bytenr of last byte, so we need + 1 here. |
| 5480 | */ |
Filipe Manana | 421f092 | 2018-10-12 13:02:48 +0100 | [diff] [blame] | 5481 | if (state_flags & EXTENT_DELALLOC) |
Nikolay Borisov | 8b8a979 | 2020-06-03 08:55:11 +0300 | [diff] [blame] | 5482 | btrfs_qgroup_free_data(BTRFS_I(inode), NULL, start, |
| 5483 | end - start + 1); |
Qu Wenruo | b9d0b38 | 2015-09-29 10:35:16 +0800 | [diff] [blame] | 5484 | |
Filipe Manana | 6ca0709 | 2015-05-26 00:55:42 +0100 | [diff] [blame] | 5485 | clear_extent_bit(io_tree, start, end, |
Omar Sandoval | e182163 | 2019-08-15 14:04:04 -0700 | [diff] [blame] | 5486 | EXTENT_LOCKED | EXTENT_DELALLOC | |
| 5487 | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1, |
| 5488 | &cached_state); |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5489 | |
Filipe Manana | 7064dd5 | 2014-08-08 02:47:05 +0100 | [diff] [blame] | 5490 | cond_resched(); |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5491 | spin_lock(&io_tree->lock); |
| 5492 | } |
| 5493 | spin_unlock(&io_tree->lock); |
| 5494 | } |
| 5495 | |
Omar Sandoval | 4b9d7b5 | 2018-05-11 13:13:36 -0700 | [diff] [blame] | 5496 | 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] | 5497 | struct btrfs_block_rsv *rsv) |
Omar Sandoval | 4b9d7b5 | 2018-05-11 13:13:36 -0700 | [diff] [blame] | 5498 | { |
| 5499 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 5500 | struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv; |
Josef Bacik | d3984c9 | 2019-08-01 18:19:37 -0400 | [diff] [blame] | 5501 | struct btrfs_trans_handle *trans; |
Josef Bacik | 2bd36e7 | 2019-08-22 15:14:33 -0400 | [diff] [blame] | 5502 | u64 delayed_refs_extra = btrfs_calc_insert_metadata_size(fs_info, 1); |
Josef Bacik | d3984c9 | 2019-08-01 18:19:37 -0400 | [diff] [blame] | 5503 | int ret; |
Omar Sandoval | 4b9d7b5 | 2018-05-11 13:13:36 -0700 | [diff] [blame] | 5504 | |
Josef Bacik | d3984c9 | 2019-08-01 18:19:37 -0400 | [diff] [blame] | 5505 | /* |
| 5506 | * Eviction should be taking place at some place safe because of our |
| 5507 | * delayed iputs. However the normal flushing code will run delayed |
| 5508 | * iputs, so we cannot use FLUSH_ALL otherwise we'll deadlock. |
| 5509 | * |
| 5510 | * We reserve the delayed_refs_extra here again because we can't use |
| 5511 | * btrfs_start_transaction(root, 0) for the same deadlocky reason as |
| 5512 | * above. We reserve our extra bit here because we generate a ton of |
| 5513 | * delayed refs activity by truncating. |
| 5514 | * |
| 5515 | * If we cannot make our reservation we'll attempt to steal from the |
| 5516 | * global reserve, because we really want to be able to free up space. |
| 5517 | */ |
| 5518 | ret = btrfs_block_rsv_refill(root, rsv, rsv->size + delayed_refs_extra, |
| 5519 | BTRFS_RESERVE_FLUSH_EVICT); |
| 5520 | if (ret) { |
Omar Sandoval | 4b9d7b5 | 2018-05-11 13:13:36 -0700 | [diff] [blame] | 5521 | /* |
| 5522 | * Try to steal from the global reserve if there is space for |
| 5523 | * it. |
| 5524 | */ |
Josef Bacik | d3984c9 | 2019-08-01 18:19:37 -0400 | [diff] [blame] | 5525 | if (btrfs_check_space_for_delayed_refs(fs_info) || |
| 5526 | btrfs_block_rsv_migrate(global_rsv, rsv, rsv->size, 0)) { |
| 5527 | btrfs_warn(fs_info, |
| 5528 | "could not allocate space for delete; will truncate on mount"); |
| 5529 | return ERR_PTR(-ENOSPC); |
| 5530 | } |
| 5531 | delayed_refs_extra = 0; |
Omar Sandoval | 4b9d7b5 | 2018-05-11 13:13:36 -0700 | [diff] [blame] | 5532 | } |
Josef Bacik | d3984c9 | 2019-08-01 18:19:37 -0400 | [diff] [blame] | 5533 | |
| 5534 | trans = btrfs_join_transaction(root); |
| 5535 | if (IS_ERR(trans)) |
| 5536 | return trans; |
| 5537 | |
| 5538 | if (delayed_refs_extra) { |
| 5539 | trans->block_rsv = &fs_info->trans_block_rsv; |
| 5540 | trans->bytes_reserved = delayed_refs_extra; |
| 5541 | btrfs_block_rsv_migrate(rsv, trans->block_rsv, |
| 5542 | delayed_refs_extra, 1); |
| 5543 | } |
| 5544 | return trans; |
Omar Sandoval | 4b9d7b5 | 2018-05-11 13:13:36 -0700 | [diff] [blame] | 5545 | } |
| 5546 | |
Al Viro | bd55597 | 2010-06-07 11:35:40 -0400 | [diff] [blame] | 5547 | void btrfs_evict_inode(struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5548 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5549 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5550 | struct btrfs_trans_handle *trans; |
| 5551 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Omar Sandoval | 4b9d7b5 | 2018-05-11 13:13:36 -0700 | [diff] [blame] | 5552 | struct btrfs_block_rsv *rsv; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5553 | int ret; |
| 5554 | |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 5555 | trace_btrfs_inode_evict(inode); |
| 5556 | |
Nikolay Borisov | 3d48d98 | 2016-06-29 09:46:41 +0300 | [diff] [blame] | 5557 | if (!root) { |
Liu Bo | e8f1bc1 | 2018-01-25 11:02:53 -0700 | [diff] [blame] | 5558 | clear_inode(inode); |
Nikolay Borisov | 3d48d98 | 2016-06-29 09:46:41 +0300 | [diff] [blame] | 5559 | return; |
| 5560 | } |
| 5561 | |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5562 | evict_inode_truncate_pages(inode); |
| 5563 | |
Stefan Behrens | 69e9c6c | 2013-09-05 16:58:43 +0200 | [diff] [blame] | 5564 | if (inode->i_nlink && |
| 5565 | ((btrfs_root_refs(&root->root_item) != 0 && |
| 5566 | root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) || |
Nikolay Borisov | 70ddc55 | 2017-02-20 13:50:35 +0200 | [diff] [blame] | 5567 | btrfs_is_free_space_inode(BTRFS_I(inode)))) |
Al Viro | bd55597 | 2010-06-07 11:35:40 -0400 | [diff] [blame] | 5568 | goto no_delete; |
| 5569 | |
Omar Sandoval | 2791906 | 2018-05-11 13:13:37 -0700 | [diff] [blame] | 5570 | if (is_bad_inode(inode)) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5571 | goto no_delete; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5572 | |
Nikolay Borisov | 7ab7956 | 2017-02-20 13:50:57 +0200 | [diff] [blame] | 5573 | btrfs_free_io_failure_record(BTRFS_I(inode), 0, (u64)-1); |
Miao Xie | f612496 | 2014-09-12 18:44:04 +0800 | [diff] [blame] | 5574 | |
Omar Sandoval | 7b40b69 | 2018-05-11 13:13:33 -0700 | [diff] [blame] | 5575 | if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 5576 | goto no_delete; |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 5577 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5578 | if (inode->i_nlink > 0) { |
Stefan Behrens | 69e9c6c | 2013-09-05 16:58:43 +0200 | [diff] [blame] | 5579 | BUG_ON(btrfs_root_refs(&root->root_item) != 0 && |
| 5580 | root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5581 | goto no_delete; |
| 5582 | } |
| 5583 | |
Nikolay Borisov | aa79021 | 2017-01-10 20:35:40 +0200 | [diff] [blame] | 5584 | ret = btrfs_commit_inode_delayed_inode(BTRFS_I(inode)); |
Omar Sandoval | 2791906 | 2018-05-11 13:13:37 -0700 | [diff] [blame] | 5585 | if (ret) |
Miao Xie | 0e8c36a | 2012-12-19 06:59:51 +0000 | [diff] [blame] | 5586 | goto no_delete; |
Miao Xie | 0e8c36a | 2012-12-19 06:59:51 +0000 | [diff] [blame] | 5587 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5588 | rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP); |
Omar Sandoval | 2791906 | 2018-05-11 13:13:37 -0700 | [diff] [blame] | 5589 | if (!rsv) |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 5590 | goto no_delete; |
Josef Bacik | 2bd36e7 | 2019-08-22 15:14:33 -0400 | [diff] [blame] | 5591 | rsv->size = btrfs_calc_metadata_size(fs_info, 1); |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 5592 | rsv->failfast = 1; |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 5593 | |
Nikolay Borisov | 6ef06d2 | 2017-02-20 13:50:34 +0200 | [diff] [blame] | 5594 | btrfs_i_size_write(BTRFS_I(inode), 0); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5595 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5596 | while (1) { |
Josef Bacik | ad80cf5 | 2018-09-28 07:18:19 -0400 | [diff] [blame] | 5597 | trans = evict_refill_and_join(root, rsv); |
Omar Sandoval | 2791906 | 2018-05-11 13:13:37 -0700 | [diff] [blame] | 5598 | if (IS_ERR(trans)) |
| 5599 | goto free_rsv; |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 5600 | |
| 5601 | trans->block_rsv = rsv; |
| 5602 | |
Nikolay Borisov | 5074339 | 2020-11-02 16:48:55 +0200 | [diff] [blame] | 5603 | ret = btrfs_truncate_inode_items(trans, root, BTRFS_I(inode), |
Filipe Manana | 0d7d316 | 2021-05-24 11:35:55 +0100 | [diff] [blame] | 5604 | 0, 0, NULL); |
Omar Sandoval | 2791906 | 2018-05-11 13:13:37 -0700 | [diff] [blame] | 5605 | trans->block_rsv = &fs_info->trans_block_rsv; |
| 5606 | btrfs_end_transaction(trans); |
| 5607 | btrfs_btree_balance_dirty(fs_info); |
| 5608 | if (ret && ret != -ENOSPC && ret != -EAGAIN) |
| 5609 | goto free_rsv; |
| 5610 | else if (!ret) |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5611 | break; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 5612 | } |
| 5613 | |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 5614 | /* |
Omar Sandoval | 2791906 | 2018-05-11 13:13:37 -0700 | [diff] [blame] | 5615 | * Errors here aren't a big deal, it just means we leave orphan items in |
| 5616 | * the tree. They will be cleaned up on the next mount. If the inode |
| 5617 | * number gets reused, cleanup deletes the orphan item without doing |
| 5618 | * anything, and unlink reuses the existing orphan item. |
| 5619 | * |
| 5620 | * If it turns out that we are dropping too many of these, we might want |
| 5621 | * to add a mechanism for retrying these after a commit. |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 5622 | */ |
Josef Bacik | ad80cf5 | 2018-09-28 07:18:19 -0400 | [diff] [blame] | 5623 | trans = evict_refill_and_join(root, rsv); |
Omar Sandoval | 2791906 | 2018-05-11 13:13:37 -0700 | [diff] [blame] | 5624 | if (!IS_ERR(trans)) { |
| 5625 | trans->block_rsv = rsv; |
| 5626 | btrfs_orphan_del(trans, BTRFS_I(inode)); |
| 5627 | trans->block_rsv = &fs_info->trans_block_rsv; |
| 5628 | btrfs_end_transaction(trans); |
| 5629 | } |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 5630 | |
Omar Sandoval | 2791906 | 2018-05-11 13:13:37 -0700 | [diff] [blame] | 5631 | free_rsv: |
| 5632 | btrfs_free_block_rsv(fs_info, rsv); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5633 | no_delete: |
Omar Sandoval | 2791906 | 2018-05-11 13:13:37 -0700 | [diff] [blame] | 5634 | /* |
| 5635 | * If we didn't successfully delete, the orphan item will still be in |
| 5636 | * the tree and we'll retry on the next mount. Again, we might also want |
| 5637 | * to retry these periodically in the future. |
| 5638 | */ |
Nikolay Borisov | f48d1cf | 2017-01-10 20:35:39 +0200 | [diff] [blame] | 5639 | btrfs_remove_delayed_node(BTRFS_I(inode)); |
Jan Kara | dbd5768 | 2012-05-03 14:48:02 +0200 | [diff] [blame] | 5640 | clear_inode(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5641 | } |
| 5642 | |
| 5643 | /* |
Qu Wenruo | 6bf9e4b | 2019-03-13 13:55:11 +0800 | [diff] [blame] | 5644 | * Return the key found in the dir entry in the location pointer, fill @type |
| 5645 | * with BTRFS_FT_*, and return 0. |
| 5646 | * |
Su Yue | 005d671 | 2018-03-05 17:13:37 +0800 | [diff] [blame] | 5647 | * If no dir entries were found, returns -ENOENT. |
| 5648 | * If found a corrupted location in dir entry, returns -EUCLEAN. |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5649 | */ |
| 5650 | static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry, |
Qu Wenruo | 6bf9e4b | 2019-03-13 13:55:11 +0800 | [diff] [blame] | 5651 | struct btrfs_key *location, u8 *type) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5652 | { |
| 5653 | const char *name = dentry->d_name.name; |
| 5654 | int namelen = dentry->d_name.len; |
| 5655 | struct btrfs_dir_item *di; |
| 5656 | struct btrfs_path *path; |
| 5657 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Yan | 0d9f7f3 | 2007-10-25 15:48:28 -0400 | [diff] [blame] | 5658 | int ret = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5659 | |
| 5660 | path = btrfs_alloc_path(); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 5661 | if (!path) |
| 5662 | return -ENOMEM; |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 5663 | |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 5664 | di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(BTRFS_I(dir)), |
| 5665 | name, namelen, 0); |
Liu Bo | 3cf5068 | 2018-09-12 06:06:26 +0800 | [diff] [blame] | 5666 | if (IS_ERR_OR_NULL(di)) { |
| 5667 | ret = di ? PTR_ERR(di) : -ENOENT; |
Su Yue | 005d671 | 2018-03-05 17:13:37 +0800 | [diff] [blame] | 5668 | goto out; |
| 5669 | } |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5670 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5671 | btrfs_dir_item_key_to_cpu(path->nodes[0], di, location); |
Liu Bo | 56a0e70 | 2017-10-30 11:14:38 -0600 | [diff] [blame] | 5672 | if (location->type != BTRFS_INODE_ITEM_KEY && |
| 5673 | location->type != BTRFS_ROOT_ITEM_KEY) { |
Su Yue | 005d671 | 2018-03-05 17:13:37 +0800 | [diff] [blame] | 5674 | ret = -EUCLEAN; |
Liu Bo | 56a0e70 | 2017-10-30 11:14:38 -0600 | [diff] [blame] | 5675 | btrfs_warn(root->fs_info, |
| 5676 | "%s gets something invalid in DIR_ITEM (name %s, directory ino %llu, location(%llu %u %llu))", |
| 5677 | __func__, name, btrfs_ino(BTRFS_I(dir)), |
| 5678 | location->objectid, location->type, location->offset); |
Liu Bo | 56a0e70 | 2017-10-30 11:14:38 -0600 | [diff] [blame] | 5679 | } |
Qu Wenruo | 6bf9e4b | 2019-03-13 13:55:11 +0800 | [diff] [blame] | 5680 | if (!ret) |
| 5681 | *type = btrfs_dir_type(path->nodes[0], di); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5682 | out: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5683 | btrfs_free_path(path); |
| 5684 | return ret; |
| 5685 | } |
| 5686 | |
| 5687 | /* |
| 5688 | * when we hit a tree root in a directory, the btrfs part of the inode |
| 5689 | * needs to be changed to reflect the root directory of the tree root. This |
| 5690 | * is kind of like crossing a mount point. |
| 5691 | */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5692 | static int fixup_tree_root_location(struct btrfs_fs_info *fs_info, |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5693 | struct inode *dir, |
| 5694 | struct dentry *dentry, |
| 5695 | struct btrfs_key *location, |
| 5696 | struct btrfs_root **sub_root) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5697 | { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5698 | struct btrfs_path *path; |
| 5699 | struct btrfs_root *new_root; |
| 5700 | struct btrfs_root_ref *ref; |
| 5701 | struct extent_buffer *leaf; |
David Sterba | 1d4c08e | 2015-01-02 19:36:14 +0100 | [diff] [blame] | 5702 | struct btrfs_key key; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5703 | int ret; |
| 5704 | int err = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5705 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5706 | path = btrfs_alloc_path(); |
| 5707 | if (!path) { |
| 5708 | err = -ENOMEM; |
| 5709 | goto out; |
| 5710 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5711 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5712 | err = -ENOENT; |
David Sterba | 1d4c08e | 2015-01-02 19:36:14 +0100 | [diff] [blame] | 5713 | key.objectid = BTRFS_I(dir)->root->root_key.objectid; |
| 5714 | key.type = BTRFS_ROOT_REF_KEY; |
| 5715 | key.offset = location->objectid; |
| 5716 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5717 | 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] | 5718 | if (ret) { |
| 5719 | if (ret < 0) |
| 5720 | err = ret; |
| 5721 | goto out; |
| 5722 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5723 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5724 | leaf = path->nodes[0]; |
| 5725 | ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref); |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 5726 | if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(BTRFS_I(dir)) || |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5727 | btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len) |
| 5728 | goto out; |
| 5729 | |
| 5730 | ret = memcmp_extent_buffer(leaf, dentry->d_name.name, |
| 5731 | (unsigned long)(ref + 1), |
| 5732 | dentry->d_name.len); |
| 5733 | if (ret) |
| 5734 | goto out; |
| 5735 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 5736 | btrfs_release_path(path); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5737 | |
David Sterba | 56e9357 | 2020-05-15 19:35:55 +0200 | [diff] [blame] | 5738 | new_root = btrfs_get_fs_root(fs_info, location->objectid, true); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5739 | if (IS_ERR(new_root)) { |
| 5740 | err = PTR_ERR(new_root); |
| 5741 | goto out; |
| 5742 | } |
| 5743 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5744 | *sub_root = new_root; |
| 5745 | location->objectid = btrfs_root_dirid(&new_root->root_item); |
| 5746 | location->type = BTRFS_INODE_ITEM_KEY; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5747 | location->offset = 0; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5748 | err = 0; |
| 5749 | out: |
| 5750 | btrfs_free_path(path); |
| 5751 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5752 | } |
| 5753 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5754 | static void inode_tree_add(struct inode *inode) |
| 5755 | { |
| 5756 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5757 | struct btrfs_inode *entry; |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 5758 | struct rb_node **p; |
| 5759 | struct rb_node *parent; |
Filipe David Borba Manana | cef2193 | 2013-09-02 12:19:13 +0100 | [diff] [blame] | 5760 | struct rb_node *new = &BTRFS_I(inode)->rb_node; |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 5761 | u64 ino = btrfs_ino(BTRFS_I(inode)); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5762 | |
Al Viro | 1d3382cb | 2010-10-23 15:19:20 -0400 | [diff] [blame] | 5763 | if (inode_unhashed(inode)) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5764 | return; |
Miao Xie | e1409ce | 2013-05-15 07:48:16 +0000 | [diff] [blame] | 5765 | parent = NULL; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5766 | spin_lock(&root->inode_lock); |
Miao Xie | e1409ce | 2013-05-15 07:48:16 +0000 | [diff] [blame] | 5767 | p = &root->inode_tree.rb_node; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5768 | while (*p) { |
| 5769 | parent = *p; |
| 5770 | entry = rb_entry(parent, struct btrfs_inode, rb_node); |
| 5771 | |
David Sterba | 3750851 | 2018-06-29 10:56:40 +0200 | [diff] [blame] | 5772 | if (ino < btrfs_ino(entry)) |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 5773 | p = &parent->rb_left; |
David Sterba | 3750851 | 2018-06-29 10:56:40 +0200 | [diff] [blame] | 5774 | else if (ino > btrfs_ino(entry)) |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 5775 | p = &parent->rb_right; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5776 | else { |
| 5777 | WARN_ON(!(entry->vfs_inode.i_state & |
Al Viro | a4ffdde | 2010-06-02 17:38:30 -0400 | [diff] [blame] | 5778 | (I_WILL_FREE | I_FREEING))); |
Filipe David Borba Manana | cef2193 | 2013-09-02 12:19:13 +0100 | [diff] [blame] | 5779 | rb_replace_node(parent, new, &root->inode_tree); |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 5780 | RB_CLEAR_NODE(parent); |
| 5781 | spin_unlock(&root->inode_lock); |
Filipe David Borba Manana | cef2193 | 2013-09-02 12:19:13 +0100 | [diff] [blame] | 5782 | return; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5783 | } |
| 5784 | } |
Filipe David Borba Manana | cef2193 | 2013-09-02 12:19:13 +0100 | [diff] [blame] | 5785 | rb_link_node(new, parent, p); |
| 5786 | rb_insert_color(new, &root->inode_tree); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5787 | spin_unlock(&root->inode_lock); |
| 5788 | } |
| 5789 | |
Nikolay Borisov | b79b724 | 2020-08-31 14:42:38 +0300 | [diff] [blame] | 5790 | static void inode_tree_del(struct btrfs_inode *inode) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5791 | { |
Nikolay Borisov | b79b724 | 2020-08-31 14:42:38 +0300 | [diff] [blame] | 5792 | struct btrfs_root *root = inode->root; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5793 | int empty = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5794 | |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 5795 | spin_lock(&root->inode_lock); |
Nikolay Borisov | b79b724 | 2020-08-31 14:42:38 +0300 | [diff] [blame] | 5796 | if (!RB_EMPTY_NODE(&inode->rb_node)) { |
| 5797 | rb_erase(&inode->rb_node, &root->inode_tree); |
| 5798 | RB_CLEAR_NODE(&inode->rb_node); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5799 | empty = RB_EMPTY_ROOT(&root->inode_tree); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5800 | } |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 5801 | spin_unlock(&root->inode_lock); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5802 | |
Stefan Behrens | 69e9c6c | 2013-09-05 16:58:43 +0200 | [diff] [blame] | 5803 | if (empty && btrfs_root_refs(&root->root_item) == 0) { |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5804 | spin_lock(&root->inode_lock); |
| 5805 | empty = RB_EMPTY_ROOT(&root->inode_tree); |
| 5806 | spin_unlock(&root->inode_lock); |
| 5807 | if (empty) |
| 5808 | btrfs_add_dead_root(root); |
| 5809 | } |
| 5810 | } |
| 5811 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5812 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5813 | static int btrfs_init_locked_inode(struct inode *inode, void *p) |
| 5814 | { |
| 5815 | struct btrfs_iget_args *args = p; |
David Sterba | 0202e83 | 2020-05-15 19:35:59 +0200 | [diff] [blame] | 5816 | |
| 5817 | inode->i_ino = args->ino; |
| 5818 | BTRFS_I(inode)->location.objectid = args->ino; |
| 5819 | BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY; |
| 5820 | BTRFS_I(inode)->location.offset = 0; |
Josef Bacik | 5c8fd99 | 2020-02-14 16:11:43 -0500 | [diff] [blame] | 5821 | BTRFS_I(inode)->root = btrfs_grab_root(args->root); |
| 5822 | BUG_ON(args->root && !BTRFS_I(inode)->root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5823 | return 0; |
| 5824 | } |
| 5825 | |
| 5826 | static int btrfs_find_actor(struct inode *inode, void *opaque) |
| 5827 | { |
| 5828 | struct btrfs_iget_args *args = opaque; |
David Sterba | 0202e83 | 2020-05-15 19:35:59 +0200 | [diff] [blame] | 5829 | |
| 5830 | return args->ino == BTRFS_I(inode)->location.objectid && |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5831 | args->root == BTRFS_I(inode)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5832 | } |
| 5833 | |
David Sterba | 0202e83 | 2020-05-15 19:35:59 +0200 | [diff] [blame] | 5834 | static struct inode *btrfs_iget_locked(struct super_block *s, u64 ino, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5835 | struct btrfs_root *root) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5836 | { |
| 5837 | struct inode *inode; |
| 5838 | struct btrfs_iget_args args; |
David Sterba | 0202e83 | 2020-05-15 19:35:59 +0200 | [diff] [blame] | 5839 | unsigned long hashval = btrfs_inode_hash(ino, root); |
Filipe David Borba Manana | 778ba82 | 2013-10-06 22:22:33 +0100 | [diff] [blame] | 5840 | |
David Sterba | 0202e83 | 2020-05-15 19:35:59 +0200 | [diff] [blame] | 5841 | args.ino = ino; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5842 | args.root = root; |
| 5843 | |
Filipe David Borba Manana | 778ba82 | 2013-10-06 22:22:33 +0100 | [diff] [blame] | 5844 | inode = iget5_locked(s, hashval, btrfs_find_actor, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5845 | btrfs_init_locked_inode, |
| 5846 | (void *)&args); |
| 5847 | return inode; |
| 5848 | } |
| 5849 | |
David Sterba | 4c66e0d | 2019-10-03 19:09:35 +0200 | [diff] [blame] | 5850 | /* |
David Sterba | 0202e83 | 2020-05-15 19:35:59 +0200 | [diff] [blame] | 5851 | * Get an inode object given its inode number and corresponding root. |
David Sterba | 4c66e0d | 2019-10-03 19:09:35 +0200 | [diff] [blame] | 5852 | * Path can be preallocated to prevent recursing back to iget through |
| 5853 | * allocator. NULL is also valid but may require an additional allocation |
| 5854 | * later. |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 5855 | */ |
David Sterba | 0202e83 | 2020-05-15 19:35:59 +0200 | [diff] [blame] | 5856 | struct inode *btrfs_iget_path(struct super_block *s, u64 ino, |
David Sterba | 4c66e0d | 2019-10-03 19:09:35 +0200 | [diff] [blame] | 5857 | struct btrfs_root *root, struct btrfs_path *path) |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 5858 | { |
| 5859 | struct inode *inode; |
| 5860 | |
David Sterba | 0202e83 | 2020-05-15 19:35:59 +0200 | [diff] [blame] | 5861 | inode = btrfs_iget_locked(s, ino, root); |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 5862 | if (!inode) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5863 | return ERR_PTR(-ENOMEM); |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 5864 | |
| 5865 | if (inode->i_state & I_NEW) { |
Filipe Manana | 6771089 | 2016-06-06 11:51:25 +0100 | [diff] [blame] | 5866 | int ret; |
| 5867 | |
Filipe Manana | 4222ea7 | 2018-10-24 10:13:03 +0100 | [diff] [blame] | 5868 | ret = btrfs_read_locked_inode(inode, path); |
Al Viro | 9bc2cef | 2018-07-29 23:04:50 +0100 | [diff] [blame] | 5869 | if (!ret) { |
Mark Fasheh | 1748f84 | 2011-07-12 11:25:31 -0700 | [diff] [blame] | 5870 | inode_tree_add(inode); |
| 5871 | unlock_new_inode(inode); |
Mark Fasheh | 1748f84 | 2011-07-12 11:25:31 -0700 | [diff] [blame] | 5872 | } else { |
Al Viro | f5b3a41 | 2018-07-29 23:04:51 +0100 | [diff] [blame] | 5873 | iget_failed(inode); |
| 5874 | /* |
| 5875 | * ret > 0 can come from btrfs_search_slot called by |
| 5876 | * btrfs_read_locked_inode, this means the inode item |
| 5877 | * was not found. |
| 5878 | */ |
| 5879 | if (ret > 0) |
| 5880 | ret = -ENOENT; |
| 5881 | inode = ERR_PTR(ret); |
Mark Fasheh | 1748f84 | 2011-07-12 11:25:31 -0700 | [diff] [blame] | 5882 | } |
| 5883 | } |
| 5884 | |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 5885 | return inode; |
| 5886 | } |
| 5887 | |
David Sterba | 0202e83 | 2020-05-15 19:35:59 +0200 | [diff] [blame] | 5888 | 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] | 5889 | { |
David Sterba | 0202e83 | 2020-05-15 19:35:59 +0200 | [diff] [blame] | 5890 | return btrfs_iget_path(s, ino, root, NULL); |
Filipe Manana | 4222ea7 | 2018-10-24 10:13:03 +0100 | [diff] [blame] | 5891 | } |
| 5892 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5893 | static struct inode *new_simple_dir(struct super_block *s, |
| 5894 | struct btrfs_key *key, |
| 5895 | struct btrfs_root *root) |
| 5896 | { |
| 5897 | struct inode *inode = new_inode(s); |
| 5898 | |
| 5899 | if (!inode) |
| 5900 | return ERR_PTR(-ENOMEM); |
| 5901 | |
Josef Bacik | 5c8fd99 | 2020-02-14 16:11:43 -0500 | [diff] [blame] | 5902 | BTRFS_I(inode)->root = btrfs_grab_root(root); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5903 | memcpy(&BTRFS_I(inode)->location, key, sizeof(*key)); |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 5904 | set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5905 | |
| 5906 | inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID; |
Omar Sandoval | 6bb6b51 | 2019-12-05 10:36:04 -0800 | [diff] [blame] | 5907 | /* |
| 5908 | * We only need lookup, the rest is read-only and there's no inode |
| 5909 | * associated with the dentry |
| 5910 | */ |
| 5911 | inode->i_op = &simple_dir_inode_operations; |
Omar Sandoval | 1fdf419 | 2017-01-25 17:06:39 -0800 | [diff] [blame] | 5912 | inode->i_opflags &= ~IOP_XATTR; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5913 | inode->i_fop = &simple_dir_operations; |
| 5914 | inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO; |
Deepa Dinamani | c2050a4 | 2016-09-14 07:48:06 -0700 | [diff] [blame] | 5915 | inode->i_mtime = current_time(inode); |
chandan r | 9cc97d6 | 2012-07-04 12:48:07 +0530 | [diff] [blame] | 5916 | inode->i_atime = inode->i_mtime; |
| 5917 | inode->i_ctime = inode->i_mtime; |
Arnd Bergmann | d3c6be6f | 2018-06-21 18:04:06 +0200 | [diff] [blame] | 5918 | BTRFS_I(inode)->i_otime = inode->i_mtime; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5919 | |
| 5920 | return inode; |
| 5921 | } |
| 5922 | |
Qu Wenruo | 6bf9e4b | 2019-03-13 13:55:11 +0800 | [diff] [blame] | 5923 | static inline u8 btrfs_inode_type(struct inode *inode) |
| 5924 | { |
| 5925 | /* |
| 5926 | * Compile-time asserts that generic FT_* types still match |
| 5927 | * BTRFS_FT_* types |
| 5928 | */ |
| 5929 | BUILD_BUG_ON(BTRFS_FT_UNKNOWN != FT_UNKNOWN); |
| 5930 | BUILD_BUG_ON(BTRFS_FT_REG_FILE != FT_REG_FILE); |
| 5931 | BUILD_BUG_ON(BTRFS_FT_DIR != FT_DIR); |
| 5932 | BUILD_BUG_ON(BTRFS_FT_CHRDEV != FT_CHRDEV); |
| 5933 | BUILD_BUG_ON(BTRFS_FT_BLKDEV != FT_BLKDEV); |
| 5934 | BUILD_BUG_ON(BTRFS_FT_FIFO != FT_FIFO); |
| 5935 | BUILD_BUG_ON(BTRFS_FT_SOCK != FT_SOCK); |
| 5936 | BUILD_BUG_ON(BTRFS_FT_SYMLINK != FT_SYMLINK); |
| 5937 | |
| 5938 | return fs_umode_to_ftype(inode->i_mode); |
| 5939 | } |
| 5940 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5941 | struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5942 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5943 | struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5944 | struct inode *inode; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5945 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5946 | struct btrfs_root *sub_root = root; |
| 5947 | struct btrfs_key location; |
Qu Wenruo | 6bf9e4b | 2019-03-13 13:55:11 +0800 | [diff] [blame] | 5948 | u8 di_type = 0; |
Josef Bacik | b4aff1f | 2011-06-28 16:18:59 -0400 | [diff] [blame] | 5949 | int ret = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5950 | |
| 5951 | if (dentry->d_name.len > BTRFS_NAME_LEN) |
| 5952 | return ERR_PTR(-ENAMETOOLONG); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5953 | |
Qu Wenruo | 6bf9e4b | 2019-03-13 13:55:11 +0800 | [diff] [blame] | 5954 | ret = btrfs_inode_by_name(dir, dentry, &location, &di_type); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5955 | if (ret < 0) |
| 5956 | return ERR_PTR(ret); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5957 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5958 | if (location.type == BTRFS_INODE_ITEM_KEY) { |
David Sterba | 0202e83 | 2020-05-15 19:35:59 +0200 | [diff] [blame] | 5959 | inode = btrfs_iget(dir->i_sb, location.objectid, root); |
Qu Wenruo | 6bf9e4b | 2019-03-13 13:55:11 +0800 | [diff] [blame] | 5960 | if (IS_ERR(inode)) |
| 5961 | return inode; |
| 5962 | |
| 5963 | /* Do extra check against inode mode with di_type */ |
| 5964 | if (btrfs_inode_type(inode) != di_type) { |
| 5965 | btrfs_crit(fs_info, |
| 5966 | "inode mode mismatch with dir: inode mode=0%o btrfs type=%u dir type=%u", |
| 5967 | inode->i_mode, btrfs_inode_type(inode), |
| 5968 | di_type); |
| 5969 | iput(inode); |
| 5970 | return ERR_PTR(-EUCLEAN); |
| 5971 | } |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5972 | return inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5973 | } |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5974 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5975 | ret = fixup_tree_root_location(fs_info, dir, dentry, |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5976 | &location, &sub_root); |
| 5977 | if (ret < 0) { |
| 5978 | if (ret != -ENOENT) |
| 5979 | inode = ERR_PTR(ret); |
| 5980 | else |
| 5981 | inode = new_simple_dir(dir->i_sb, &location, sub_root); |
| 5982 | } else { |
David Sterba | 0202e83 | 2020-05-15 19:35:59 +0200 | [diff] [blame] | 5983 | inode = btrfs_iget(dir->i_sb, location.objectid, sub_root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5984 | } |
Josef Bacik | 8727002 | 2020-01-24 09:32:31 -0500 | [diff] [blame] | 5985 | if (root != sub_root) |
Josef Bacik | 0024652 | 2020-01-24 09:33:01 -0500 | [diff] [blame] | 5986 | btrfs_put_root(sub_root); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5987 | |
Julia Lawall | 34d19ba | 2011-01-24 19:55:19 +0000 | [diff] [blame] | 5988 | if (!IS_ERR(inode) && root != sub_root) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5989 | down_read(&fs_info->cleanup_work_sem); |
David Howells | bc98a42 | 2017-07-17 08:45:34 +0100 | [diff] [blame] | 5990 | if (!sb_rdonly(inode->i_sb)) |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 5991 | ret = btrfs_orphan_cleanup(sub_root); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5992 | up_read(&fs_info->cleanup_work_sem); |
Josef Bacik | 01cd336 | 2013-06-03 21:39:49 -0400 | [diff] [blame] | 5993 | if (ret) { |
| 5994 | iput(inode); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 5995 | inode = ERR_PTR(ret); |
Josef Bacik | 01cd336 | 2013-06-03 21:39:49 -0400 | [diff] [blame] | 5996 | } |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 5997 | } |
| 5998 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5999 | return inode; |
| 6000 | } |
| 6001 | |
Nick Piggin | fe15ce4 | 2011-01-07 17:49:23 +1100 | [diff] [blame] | 6002 | static int btrfs_dentry_delete(const struct dentry *dentry) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 6003 | { |
| 6004 | struct btrfs_root *root; |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 6005 | struct inode *inode = d_inode(dentry); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 6006 | |
Li Zefan | 848cce0 | 2012-02-21 17:04:28 +0800 | [diff] [blame] | 6007 | if (!inode && !IS_ROOT(dentry)) |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 6008 | inode = d_inode(dentry->d_parent); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 6009 | |
Li Zefan | 848cce0 | 2012-02-21 17:04:28 +0800 | [diff] [blame] | 6010 | if (inode) { |
| 6011 | root = BTRFS_I(inode)->root; |
Yan, Zheng | efefb14 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 6012 | if (btrfs_root_refs(&root->root_item) == 0) |
| 6013 | return 1; |
Li Zefan | 848cce0 | 2012-02-21 17:04:28 +0800 | [diff] [blame] | 6014 | |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 6015 | if (btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) |
Li Zefan | 848cce0 | 2012-02-21 17:04:28 +0800 | [diff] [blame] | 6016 | return 1; |
Yan, Zheng | efefb14 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 6017 | } |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 6018 | return 0; |
| 6019 | } |
| 6020 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 6021 | static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry, |
Al Viro | 00cd8dd | 2012-06-10 17:13:09 -0400 | [diff] [blame] | 6022 | unsigned int flags) |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 6023 | { |
Al Viro | 3837d20 | 2018-10-10 16:38:27 -0400 | [diff] [blame] | 6024 | struct inode *inode = btrfs_lookup_dentry(dir, dentry); |
Josef Bacik | a66e7cc | 2011-09-18 10:34:03 -0400 | [diff] [blame] | 6025 | |
Al Viro | 3837d20 | 2018-10-10 16:38:27 -0400 | [diff] [blame] | 6026 | if (inode == ERR_PTR(-ENOENT)) |
| 6027 | inode = NULL; |
Al Viro | 41d28bc | 2014-10-12 22:24:21 -0400 | [diff] [blame] | 6028 | return d_splice_alias(inode, dentry); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6029 | } |
| 6030 | |
Josef Bacik | 23b5ec7 | 2017-07-24 15:14:25 -0400 | [diff] [blame] | 6031 | /* |
| 6032 | * All this infrastructure exists because dir_emit can fault, and we are holding |
| 6033 | * the tree lock when doing readdir. For now just allocate a buffer and copy |
| 6034 | * our information into that, and then dir_emit from the buffer. This is |
| 6035 | * similar to what NFS does, only we don't keep the buffer around in pagecache |
| 6036 | * because I'm afraid I'll mess that up. Long term we need to make filldir do |
| 6037 | * copy_to_user_inatomic so we don't have to worry about page faulting under the |
| 6038 | * tree lock. |
| 6039 | */ |
| 6040 | static int btrfs_opendir(struct inode *inode, struct file *file) |
| 6041 | { |
| 6042 | struct btrfs_file_private *private; |
| 6043 | |
| 6044 | private = kzalloc(sizeof(struct btrfs_file_private), GFP_KERNEL); |
| 6045 | if (!private) |
| 6046 | return -ENOMEM; |
| 6047 | private->filldir_buf = kzalloc(PAGE_SIZE, GFP_KERNEL); |
| 6048 | if (!private->filldir_buf) { |
| 6049 | kfree(private); |
| 6050 | return -ENOMEM; |
| 6051 | } |
| 6052 | file->private_data = private; |
| 6053 | return 0; |
| 6054 | } |
| 6055 | |
| 6056 | struct dir_entry { |
| 6057 | u64 ino; |
| 6058 | u64 offset; |
| 6059 | unsigned type; |
| 6060 | int name_len; |
| 6061 | }; |
| 6062 | |
| 6063 | static int btrfs_filldir(void *addr, int entries, struct dir_context *ctx) |
| 6064 | { |
| 6065 | while (entries--) { |
| 6066 | struct dir_entry *entry = addr; |
| 6067 | char *name = (char *)(entry + 1); |
| 6068 | |
David Sterba | 92d3217 | 2018-04-16 21:10:14 +0200 | [diff] [blame] | 6069 | ctx->pos = get_unaligned(&entry->offset); |
| 6070 | if (!dir_emit(ctx, name, get_unaligned(&entry->name_len), |
| 6071 | get_unaligned(&entry->ino), |
| 6072 | get_unaligned(&entry->type))) |
Josef Bacik | 23b5ec7 | 2017-07-24 15:14:25 -0400 | [diff] [blame] | 6073 | return 1; |
David Sterba | 92d3217 | 2018-04-16 21:10:14 +0200 | [diff] [blame] | 6074 | addr += sizeof(struct dir_entry) + |
| 6075 | get_unaligned(&entry->name_len); |
Josef Bacik | 23b5ec7 | 2017-07-24 15:14:25 -0400 | [diff] [blame] | 6076 | ctx->pos++; |
| 6077 | } |
| 6078 | return 0; |
| 6079 | } |
| 6080 | |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 6081 | static int btrfs_real_readdir(struct file *file, struct dir_context *ctx) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6082 | { |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 6083 | struct inode *inode = file_inode(file); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6084 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Josef Bacik | 23b5ec7 | 2017-07-24 15:14:25 -0400 | [diff] [blame] | 6085 | struct btrfs_file_private *private = file->private_data; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6086 | struct btrfs_dir_item *di; |
| 6087 | struct btrfs_key key; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6088 | struct btrfs_key found_key; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6089 | struct btrfs_path *path; |
Josef Bacik | 23b5ec7 | 2017-07-24 15:14:25 -0400 | [diff] [blame] | 6090 | void *addr; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 6091 | struct list_head ins_list; |
| 6092 | struct list_head del_list; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6093 | int ret; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6094 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6095 | int slot; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6096 | char *name_ptr; |
| 6097 | int name_len; |
Josef Bacik | 23b5ec7 | 2017-07-24 15:14:25 -0400 | [diff] [blame] | 6098 | int entries = 0; |
| 6099 | int total_len = 0; |
Omar Sandoval | 02dbfc9 | 2016-05-20 13:50:33 -0700 | [diff] [blame] | 6100 | bool put = false; |
Jeff Mahoney | c2951f3 | 2016-11-21 15:59:04 +0100 | [diff] [blame] | 6101 | struct btrfs_key location; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6102 | |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 6103 | if (!dir_emit_dots(file, ctx)) |
| 6104 | return 0; |
| 6105 | |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 6106 | path = btrfs_alloc_path(); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 6107 | if (!path) |
| 6108 | return -ENOMEM; |
Chris Mason | ff5714c | 2011-05-28 07:00:39 -0400 | [diff] [blame] | 6109 | |
Josef Bacik | 23b5ec7 | 2017-07-24 15:14:25 -0400 | [diff] [blame] | 6110 | addr = private->filldir_buf; |
David Sterba | e4058b5 | 2015-11-27 16:31:35 +0100 | [diff] [blame] | 6111 | path->reada = READA_FORWARD; |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 6112 | |
Jeff Mahoney | c2951f3 | 2016-11-21 15:59:04 +0100 | [diff] [blame] | 6113 | INIT_LIST_HEAD(&ins_list); |
| 6114 | INIT_LIST_HEAD(&del_list); |
| 6115 | put = btrfs_readdir_get_delayed_items(inode, &ins_list, &del_list); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 6116 | |
Josef Bacik | 23b5ec7 | 2017-07-24 15:14:25 -0400 | [diff] [blame] | 6117 | again: |
Jeff Mahoney | c2951f3 | 2016-11-21 15:59:04 +0100 | [diff] [blame] | 6118 | key.type = BTRFS_DIR_INDEX_KEY; |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 6119 | key.offset = ctx->pos; |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 6120 | key.objectid = btrfs_ino(BTRFS_I(inode)); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6121 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6122 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 6123 | if (ret < 0) |
| 6124 | goto err; |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 6125 | |
| 6126 | while (1) { |
Josef Bacik | 23b5ec7 | 2017-07-24 15:14:25 -0400 | [diff] [blame] | 6127 | struct dir_entry *entry; |
| 6128 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6129 | leaf = path->nodes[0]; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6130 | slot = path->slots[0]; |
Li Zefan | b9e03af | 2011-03-23 10:43:58 +0800 | [diff] [blame] | 6131 | if (slot >= btrfs_header_nritems(leaf)) { |
| 6132 | ret = btrfs_next_leaf(root, path); |
| 6133 | if (ret < 0) |
| 6134 | goto err; |
| 6135 | else if (ret > 0) |
| 6136 | break; |
| 6137 | continue; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6138 | } |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 6139 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6140 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
| 6141 | |
| 6142 | if (found_key.objectid != key.objectid) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6143 | break; |
Jeff Mahoney | c2951f3 | 2016-11-21 15:59:04 +0100 | [diff] [blame] | 6144 | if (found_key.type != BTRFS_DIR_INDEX_KEY) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6145 | break; |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 6146 | if (found_key.offset < ctx->pos) |
Li Zefan | b9e03af | 2011-03-23 10:43:58 +0800 | [diff] [blame] | 6147 | goto next; |
Jeff Mahoney | c2951f3 | 2016-11-21 15:59:04 +0100 | [diff] [blame] | 6148 | if (btrfs_should_delete_dir_index(&del_list, found_key.offset)) |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 6149 | goto next; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6150 | di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item); |
Jeff Mahoney | c2951f3 | 2016-11-21 15:59:04 +0100 | [diff] [blame] | 6151 | name_len = btrfs_dir_name_len(leaf, di); |
Josef Bacik | 23b5ec7 | 2017-07-24 15:14:25 -0400 | [diff] [blame] | 6152 | if ((total_len + sizeof(struct dir_entry) + name_len) >= |
| 6153 | PAGE_SIZE) { |
| 6154 | btrfs_release_path(path); |
| 6155 | ret = btrfs_filldir(private->filldir_buf, entries, ctx); |
| 6156 | if (ret) |
| 6157 | goto nopos; |
| 6158 | addr = private->filldir_buf; |
| 6159 | entries = 0; |
| 6160 | total_len = 0; |
| 6161 | goto again; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6162 | } |
Josef Bacik | 23b5ec7 | 2017-07-24 15:14:25 -0400 | [diff] [blame] | 6163 | |
| 6164 | entry = addr; |
David Sterba | 92d3217 | 2018-04-16 21:10:14 +0200 | [diff] [blame] | 6165 | put_unaligned(name_len, &entry->name_len); |
Josef Bacik | 23b5ec7 | 2017-07-24 15:14:25 -0400 | [diff] [blame] | 6166 | name_ptr = (char *)(entry + 1); |
Jeff Mahoney | c2951f3 | 2016-11-21 15:59:04 +0100 | [diff] [blame] | 6167 | read_extent_buffer(leaf, name_ptr, (unsigned long)(di + 1), |
| 6168 | name_len); |
Phillip Potter | 7d157c3 | 2019-03-26 21:39:34 +0000 | [diff] [blame] | 6169 | put_unaligned(fs_ftype_to_dtype(btrfs_dir_type(leaf, di)), |
David Sterba | 92d3217 | 2018-04-16 21:10:14 +0200 | [diff] [blame] | 6170 | &entry->type); |
Jeff Mahoney | c2951f3 | 2016-11-21 15:59:04 +0100 | [diff] [blame] | 6171 | btrfs_dir_item_key_to_cpu(leaf, di, &location); |
David Sterba | 92d3217 | 2018-04-16 21:10:14 +0200 | [diff] [blame] | 6172 | put_unaligned(location.objectid, &entry->ino); |
| 6173 | put_unaligned(found_key.offset, &entry->offset); |
Josef Bacik | 23b5ec7 | 2017-07-24 15:14:25 -0400 | [diff] [blame] | 6174 | entries++; |
| 6175 | addr += sizeof(struct dir_entry) + name_len; |
| 6176 | total_len += sizeof(struct dir_entry) + name_len; |
Li Zefan | b9e03af | 2011-03-23 10:43:58 +0800 | [diff] [blame] | 6177 | next: |
| 6178 | path->slots[0]++; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6179 | } |
Josef Bacik | 23b5ec7 | 2017-07-24 15:14:25 -0400 | [diff] [blame] | 6180 | btrfs_release_path(path); |
| 6181 | |
| 6182 | ret = btrfs_filldir(private->filldir_buf, entries, ctx); |
| 6183 | if (ret) |
| 6184 | goto nopos; |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 6185 | |
Jeff Mahoney | d2fbb2b | 2016-11-05 13:26:35 -0400 | [diff] [blame] | 6186 | ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list); |
Jeff Mahoney | c2951f3 | 2016-11-21 15:59:04 +0100 | [diff] [blame] | 6187 | if (ret) |
David Sterba | bc4ef75 | 2015-11-13 13:44:28 +0100 | [diff] [blame] | 6188 | goto nopos; |
| 6189 | |
Zach Brown | db62efb | 2013-07-11 16:19:42 -0700 | [diff] [blame] | 6190 | /* |
| 6191 | * Stop new entries from being returned after we return the last |
| 6192 | * entry. |
| 6193 | * |
| 6194 | * New directory entries are assigned a strictly increasing |
| 6195 | * offset. This means that new entries created during readdir |
| 6196 | * are *guaranteed* to be seen in the future by that readdir. |
| 6197 | * This has broken buggy programs which operate on names as |
| 6198 | * they're returned by readdir. Until we re-use freed offsets |
| 6199 | * we have this hack to stop new entries from being returned |
| 6200 | * under the assumption that they'll never reach this huge |
| 6201 | * offset. |
| 6202 | * |
| 6203 | * This is being careful not to overflow 32bit loff_t unless the |
| 6204 | * last entry requires it because doing so has broken 32bit apps |
| 6205 | * in the past. |
| 6206 | */ |
Jeff Mahoney | c2951f3 | 2016-11-21 15:59:04 +0100 | [diff] [blame] | 6207 | if (ctx->pos >= INT_MAX) |
| 6208 | ctx->pos = LLONG_MAX; |
| 6209 | else |
| 6210 | ctx->pos = INT_MAX; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6211 | nopos: |
| 6212 | ret = 0; |
| 6213 | err: |
Omar Sandoval | 02dbfc9 | 2016-05-20 13:50:33 -0700 | [diff] [blame] | 6214 | if (put) |
| 6215 | btrfs_readdir_put_delayed_items(inode, &ins_list, &del_list); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6216 | btrfs_free_path(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6217 | return ret; |
| 6218 | } |
| 6219 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6220 | /* |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 6221 | * This is somewhat expensive, updating the tree every time the |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6222 | * inode changes. But, it is most likely to find the inode in cache. |
| 6223 | * FIXME, needs more benchmarking...there are no reasons other than performance |
| 6224 | * to keep or drop this code. |
| 6225 | */ |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 6226 | static int btrfs_dirty_inode(struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6227 | { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6228 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6229 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 6230 | struct btrfs_trans_handle *trans; |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 6231 | int ret; |
| 6232 | |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 6233 | if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags)) |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 6234 | return 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6235 | |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 6236 | trans = btrfs_join_transaction(root); |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 6237 | if (IS_ERR(trans)) |
| 6238 | return PTR_ERR(trans); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 6239 | |
Nikolay Borisov | 9a56fcd | 2020-11-02 16:48:59 +0200 | [diff] [blame] | 6240 | ret = btrfs_update_inode(trans, root, BTRFS_I(inode)); |
Nikolay Borisov | 4d14c5c | 2021-02-22 18:40:44 +0200 | [diff] [blame] | 6241 | if (ret && (ret == -ENOSPC || ret == -EDQUOT)) { |
Chris Mason | 94b6044 | 2010-05-26 11:02:00 -0400 | [diff] [blame] | 6242 | /* whoops, lets try again with the full transaction */ |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 6243 | btrfs_end_transaction(trans); |
Chris Mason | 94b6044 | 2010-05-26 11:02:00 -0400 | [diff] [blame] | 6244 | trans = btrfs_start_transaction(root, 1); |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 6245 | if (IS_ERR(trans)) |
| 6246 | return PTR_ERR(trans); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 6247 | |
Nikolay Borisov | 9a56fcd | 2020-11-02 16:48:59 +0200 | [diff] [blame] | 6248 | ret = btrfs_update_inode(trans, root, BTRFS_I(inode)); |
Chris Mason | 94b6044 | 2010-05-26 11:02:00 -0400 | [diff] [blame] | 6249 | } |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 6250 | btrfs_end_transaction(trans); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 6251 | if (BTRFS_I(inode)->delayed_node) |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6252 | btrfs_balance_delayed_items(fs_info); |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 6253 | |
| 6254 | return ret; |
| 6255 | } |
| 6256 | |
| 6257 | /* |
| 6258 | * This is a copy of file_update_time. We need this so we can return error on |
| 6259 | * ENOSPC for updating the inode in the case of file write and mmap writes. |
| 6260 | */ |
Deepa Dinamani | 95582b0 | 2018-05-08 19:36:02 -0700 | [diff] [blame] | 6261 | static int btrfs_update_time(struct inode *inode, struct timespec64 *now, |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 6262 | int flags) |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 6263 | { |
Alexander Block | 2bc556528 | 2012-06-15 09:49:33 +0200 | [diff] [blame] | 6264 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Jeff Layton | 3a8c723 | 2017-12-11 06:35:24 -0500 | [diff] [blame] | 6265 | bool dirty = flags & ~S_VERSION; |
Alexander Block | 2bc556528 | 2012-06-15 09:49:33 +0200 | [diff] [blame] | 6266 | |
| 6267 | if (btrfs_root_readonly(root)) |
| 6268 | return -EROFS; |
| 6269 | |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 6270 | if (flags & S_VERSION) |
Jeff Layton | 3a8c723 | 2017-12-11 06:35:24 -0500 | [diff] [blame] | 6271 | dirty |= inode_maybe_inc_iversion(inode, dirty); |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 6272 | if (flags & S_CTIME) |
| 6273 | inode->i_ctime = *now; |
| 6274 | if (flags & S_MTIME) |
| 6275 | inode->i_mtime = *now; |
| 6276 | if (flags & S_ATIME) |
| 6277 | inode->i_atime = *now; |
Jeff Layton | 3a8c723 | 2017-12-11 06:35:24 -0500 | [diff] [blame] | 6278 | return dirty ? btrfs_dirty_inode(inode) : 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6279 | } |
| 6280 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 6281 | /* |
| 6282 | * find the highest existing sequence number in a directory |
| 6283 | * and then set the in-memory index_cnt variable to reflect |
| 6284 | * free sequence numbers |
| 6285 | */ |
Nikolay Borisov | 4c570655 | 2017-02-20 13:50:32 +0200 | [diff] [blame] | 6286 | static int btrfs_set_inode_index_count(struct btrfs_inode *inode) |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6287 | { |
Nikolay Borisov | 4c570655 | 2017-02-20 13:50:32 +0200 | [diff] [blame] | 6288 | struct btrfs_root *root = inode->root; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6289 | struct btrfs_key key, found_key; |
| 6290 | struct btrfs_path *path; |
| 6291 | struct extent_buffer *leaf; |
| 6292 | int ret; |
| 6293 | |
Nikolay Borisov | 4c570655 | 2017-02-20 13:50:32 +0200 | [diff] [blame] | 6294 | key.objectid = btrfs_ino(inode); |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 6295 | key.type = BTRFS_DIR_INDEX_KEY; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6296 | key.offset = (u64)-1; |
| 6297 | |
| 6298 | path = btrfs_alloc_path(); |
| 6299 | if (!path) |
| 6300 | return -ENOMEM; |
| 6301 | |
| 6302 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 6303 | if (ret < 0) |
| 6304 | goto out; |
| 6305 | /* FIXME: we should be able to handle this */ |
| 6306 | if (ret == 0) |
| 6307 | goto out; |
| 6308 | ret = 0; |
| 6309 | |
| 6310 | /* |
| 6311 | * MAGIC NUMBER EXPLANATION: |
| 6312 | * since we search a directory based on f_pos we have to start at 2 |
| 6313 | * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody |
| 6314 | * else has to start at 2 |
| 6315 | */ |
| 6316 | if (path->slots[0] == 0) { |
Nikolay Borisov | 4c570655 | 2017-02-20 13:50:32 +0200 | [diff] [blame] | 6317 | inode->index_cnt = 2; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6318 | goto out; |
| 6319 | } |
| 6320 | |
| 6321 | path->slots[0]--; |
| 6322 | |
| 6323 | leaf = path->nodes[0]; |
| 6324 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 6325 | |
Nikolay Borisov | 4c570655 | 2017-02-20 13:50:32 +0200 | [diff] [blame] | 6326 | if (found_key.objectid != btrfs_ino(inode) || |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 6327 | found_key.type != BTRFS_DIR_INDEX_KEY) { |
Nikolay Borisov | 4c570655 | 2017-02-20 13:50:32 +0200 | [diff] [blame] | 6328 | inode->index_cnt = 2; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6329 | goto out; |
| 6330 | } |
| 6331 | |
Nikolay Borisov | 4c570655 | 2017-02-20 13:50:32 +0200 | [diff] [blame] | 6332 | inode->index_cnt = found_key.offset + 1; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6333 | out: |
| 6334 | btrfs_free_path(path); |
| 6335 | return ret; |
| 6336 | } |
| 6337 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 6338 | /* |
| 6339 | * helper to find a free sequence number in a given directory. This current |
| 6340 | * code is very simple, later versions will do smarter things in the btree |
| 6341 | */ |
Nikolay Borisov | 877574e | 2017-02-20 13:50:33 +0200 | [diff] [blame] | 6342 | int btrfs_set_inode_index(struct btrfs_inode *dir, u64 *index) |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6343 | { |
| 6344 | int ret = 0; |
| 6345 | |
Nikolay Borisov | 877574e | 2017-02-20 13:50:33 +0200 | [diff] [blame] | 6346 | if (dir->index_cnt == (u64)-1) { |
| 6347 | ret = btrfs_inode_delayed_dir_index_count(dir); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 6348 | if (ret) { |
| 6349 | ret = btrfs_set_inode_index_count(dir); |
| 6350 | if (ret) |
| 6351 | return ret; |
| 6352 | } |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6353 | } |
| 6354 | |
Nikolay Borisov | 877574e | 2017-02-20 13:50:33 +0200 | [diff] [blame] | 6355 | *index = dir->index_cnt; |
| 6356 | dir->index_cnt++; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6357 | |
| 6358 | return ret; |
| 6359 | } |
| 6360 | |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6361 | static int btrfs_insert_inode_locked(struct inode *inode) |
| 6362 | { |
| 6363 | struct btrfs_iget_args args; |
David Sterba | 0202e83 | 2020-05-15 19:35:59 +0200 | [diff] [blame] | 6364 | |
| 6365 | args.ino = BTRFS_I(inode)->location.objectid; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6366 | args.root = BTRFS_I(inode)->root; |
| 6367 | |
| 6368 | return insert_inode_locked4(inode, |
| 6369 | btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root), |
| 6370 | btrfs_find_actor, &args); |
| 6371 | } |
| 6372 | |
Anand Jain | 19aee8d | 2017-07-18 17:37:05 +0800 | [diff] [blame] | 6373 | /* |
| 6374 | * Inherit flags from the parent inode. |
| 6375 | * |
| 6376 | * Currently only the compression flags and the cow flags are inherited. |
| 6377 | */ |
| 6378 | static void btrfs_inherit_iflags(struct inode *inode, struct inode *dir) |
| 6379 | { |
| 6380 | unsigned int flags; |
| 6381 | |
| 6382 | if (!dir) |
| 6383 | return; |
| 6384 | |
| 6385 | flags = BTRFS_I(dir)->flags; |
| 6386 | |
| 6387 | if (flags & BTRFS_INODE_NOCOMPRESS) { |
| 6388 | BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS; |
| 6389 | BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS; |
| 6390 | } else if (flags & BTRFS_INODE_COMPRESS) { |
| 6391 | BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS; |
| 6392 | BTRFS_I(inode)->flags |= BTRFS_INODE_COMPRESS; |
| 6393 | } |
| 6394 | |
| 6395 | if (flags & BTRFS_INODE_NODATACOW) { |
| 6396 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW; |
| 6397 | if (S_ISREG(inode->i_mode)) |
| 6398 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM; |
| 6399 | } |
| 6400 | |
David Sterba | 7b6a221 | 2018-03-26 18:40:21 +0200 | [diff] [blame] | 6401 | btrfs_sync_inode_flags_to_i_flags(inode); |
Anand Jain | 19aee8d | 2017-07-18 17:37:05 +0800 | [diff] [blame] | 6402 | } |
| 6403 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6404 | static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans, |
| 6405 | struct btrfs_root *root, |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6406 | struct inode *dir, |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 6407 | const char *name, int name_len, |
Al Viro | 175a4eb | 2011-07-26 03:30:54 -0400 | [diff] [blame] | 6408 | u64 ref_objectid, u64 objectid, |
| 6409 | umode_t mode, u64 *index) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6410 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6411 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6412 | struct inode *inode; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6413 | struct btrfs_inode_item *inode_item; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6414 | struct btrfs_key *location; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6415 | struct btrfs_path *path; |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 6416 | struct btrfs_inode_ref *ref; |
| 6417 | struct btrfs_key key[2]; |
| 6418 | u32 sizes[2]; |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 6419 | int nitems = name ? 2 : 1; |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 6420 | unsigned long ptr; |
Josef Bacik | 11a19a9 | 2019-09-09 10:12:04 -0400 | [diff] [blame] | 6421 | unsigned int nofs_flag; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6422 | int ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6423 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6424 | path = btrfs_alloc_path(); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 6425 | if (!path) |
| 6426 | return ERR_PTR(-ENOMEM); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6427 | |
Josef Bacik | 11a19a9 | 2019-09-09 10:12:04 -0400 | [diff] [blame] | 6428 | nofs_flag = memalloc_nofs_save(); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6429 | inode = new_inode(fs_info->sb); |
Josef Bacik | 11a19a9 | 2019-09-09 10:12:04 -0400 | [diff] [blame] | 6430 | memalloc_nofs_restore(nofs_flag); |
Yoshinori Sano | 8fb2764 | 2011-04-09 02:30:07 +0000 | [diff] [blame] | 6431 | if (!inode) { |
| 6432 | btrfs_free_path(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6433 | return ERR_PTR(-ENOMEM); |
Yoshinori Sano | 8fb2764 | 2011-04-09 02:30:07 +0000 | [diff] [blame] | 6434 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6435 | |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 6436 | /* |
Filipe Manana | 5762b5c | 2014-08-01 00:10:32 +0100 | [diff] [blame] | 6437 | * O_TMPFILE, set link count to 0, so that after this point, |
| 6438 | * we fill in an inode item with the correct link count. |
| 6439 | */ |
| 6440 | if (!name) |
| 6441 | set_nlink(inode, 0); |
| 6442 | |
| 6443 | /* |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 6444 | * we have to initialize this early, so we can reclaim the inode |
| 6445 | * number if we fail afterwards in this function. |
| 6446 | */ |
| 6447 | inode->i_ino = objectid; |
| 6448 | |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 6449 | if (dir && name) { |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 6450 | trace_btrfs_inode_request(dir); |
| 6451 | |
Nikolay Borisov | 877574e | 2017-02-20 13:50:33 +0200 | [diff] [blame] | 6452 | ret = btrfs_set_inode_index(BTRFS_I(dir), index); |
Shen Feng | 0977143 | 2009-04-02 16:46:06 -0400 | [diff] [blame] | 6453 | if (ret) { |
Yoshinori Sano | 8fb2764 | 2011-04-09 02:30:07 +0000 | [diff] [blame] | 6454 | btrfs_free_path(path); |
Shen Feng | 0977143 | 2009-04-02 16:46:06 -0400 | [diff] [blame] | 6455 | iput(inode); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6456 | return ERR_PTR(ret); |
Shen Feng | 0977143 | 2009-04-02 16:46:06 -0400 | [diff] [blame] | 6457 | } |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 6458 | } else if (dir) { |
| 6459 | *index = 0; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6460 | } |
| 6461 | /* |
| 6462 | * index_cnt is ignored for everything but a dir, |
Su Yue | df6703e | 2018-01-12 11:08:02 +0800 | [diff] [blame] | 6463 | * btrfs_set_inode_index_count has an explanation for the magic |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6464 | * number |
| 6465 | */ |
| 6466 | BTRFS_I(inode)->index_cnt = 2; |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 6467 | BTRFS_I(inode)->dir_index = *index; |
Josef Bacik | 5c8fd99 | 2020-02-14 16:11:43 -0500 | [diff] [blame] | 6468 | BTRFS_I(inode)->root = btrfs_grab_root(root); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 6469 | BTRFS_I(inode)->generation = trans->transid; |
Josef Bacik | 7619585 | 2010-11-19 02:18:02 +0000 | [diff] [blame] | 6470 | inode->i_generation = BTRFS_I(inode)->generation; |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6471 | |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 6472 | /* |
| 6473 | * We could have gotten an inode number from somebody who was fsynced |
| 6474 | * and then removed in this same transaction, so let's just set full |
| 6475 | * sync since it will be a full sync anyway and this will blow away the |
| 6476 | * old info in the log. |
| 6477 | */ |
| 6478 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags); |
| 6479 | |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 6480 | key[0].objectid = objectid; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 6481 | key[0].type = BTRFS_INODE_ITEM_KEY; |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 6482 | key[0].offset = 0; |
| 6483 | |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 6484 | sizes[0] = sizeof(struct btrfs_inode_item); |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 6485 | |
| 6486 | if (name) { |
| 6487 | /* |
| 6488 | * Start new inodes with an inode_ref. This is slightly more |
| 6489 | * efficient for small numbers of hard links since they will |
| 6490 | * be packed into one item. Extended refs will kick in if we |
| 6491 | * add more hard links than can fit in the ref item. |
| 6492 | */ |
| 6493 | key[1].objectid = objectid; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 6494 | key[1].type = BTRFS_INODE_REF_KEY; |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 6495 | key[1].offset = ref_objectid; |
| 6496 | |
| 6497 | sizes[1] = name_len + sizeof(*ref); |
| 6498 | } |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 6499 | |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6500 | location = &BTRFS_I(inode)->location; |
| 6501 | location->objectid = objectid; |
| 6502 | location->offset = 0; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 6503 | location->type = BTRFS_INODE_ITEM_KEY; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6504 | |
| 6505 | ret = btrfs_insert_inode_locked(inode); |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6506 | if (ret < 0) { |
| 6507 | iput(inode); |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6508 | goto fail; |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6509 | } |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6510 | |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 6511 | ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems); |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 6512 | if (ret != 0) |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6513 | goto fail_unlock; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6514 | |
Christian Brauner | 21cb47b | 2021-01-21 14:19:25 +0100 | [diff] [blame] | 6515 | inode_init_owner(&init_user_ns, inode, dir, mode); |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 6516 | inode_set_bytes(inode, 0); |
chandan r | 9cc97d6 | 2012-07-04 12:48:07 +0530 | [diff] [blame] | 6517 | |
Deepa Dinamani | c2050a4 | 2016-09-14 07:48:06 -0700 | [diff] [blame] | 6518 | inode->i_mtime = current_time(inode); |
chandan r | 9cc97d6 | 2012-07-04 12:48:07 +0530 | [diff] [blame] | 6519 | inode->i_atime = inode->i_mtime; |
| 6520 | inode->i_ctime = inode->i_mtime; |
Arnd Bergmann | d3c6be6f | 2018-06-21 18:04:06 +0200 | [diff] [blame] | 6521 | BTRFS_I(inode)->i_otime = inode->i_mtime; |
chandan r | 9cc97d6 | 2012-07-04 12:48:07 +0530 | [diff] [blame] | 6522 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6523 | inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 6524 | struct btrfs_inode_item); |
David Sterba | b159fa2 | 2016-11-08 18:09:03 +0100 | [diff] [blame] | 6525 | memzero_extent_buffer(path->nodes[0], (unsigned long)inode_item, |
Li Zefan | 293f7e0 | 2012-07-10 00:58:58 -0600 | [diff] [blame] | 6526 | sizeof(*inode_item)); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 6527 | fill_inode_item(trans, path->nodes[0], inode_item, inode); |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 6528 | |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 6529 | if (name) { |
| 6530 | ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1, |
| 6531 | struct btrfs_inode_ref); |
| 6532 | btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len); |
| 6533 | btrfs_set_inode_ref_index(path->nodes[0], ref, *index); |
| 6534 | ptr = (unsigned long)(ref + 1); |
| 6535 | write_extent_buffer(path->nodes[0], name, ptr, name_len); |
| 6536 | } |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 6537 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6538 | btrfs_mark_buffer_dirty(path->nodes[0]); |
| 6539 | btrfs_free_path(path); |
| 6540 | |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 6541 | btrfs_inherit_iflags(inode, dir); |
| 6542 | |
Al Viro | 569254b | 2011-07-24 17:08:40 -0400 | [diff] [blame] | 6543 | if (S_ISREG(mode)) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6544 | if (btrfs_test_opt(fs_info, NODATASUM)) |
Chris Mason | 9427216 | 2009-07-02 12:26:06 -0400 | [diff] [blame] | 6545 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6546 | if (btrfs_test_opt(fs_info, NODATACOW)) |
Josef Bacik | f2bdf9a | 2013-02-21 15:28:28 -0500 | [diff] [blame] | 6547 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW | |
| 6548 | BTRFS_INODE_NODATASUM; |
Chris Mason | 9427216 | 2009-07-02 12:26:06 -0400 | [diff] [blame] | 6549 | } |
| 6550 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6551 | inode_tree_add(inode); |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 6552 | |
| 6553 | trace_btrfs_inode_new(inode); |
Nikolay Borisov | d909441 | 2020-06-05 10:41:13 +0300 | [diff] [blame] | 6554 | btrfs_set_inode_last_trans(trans, BTRFS_I(inode)); |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 6555 | |
Alexander Block | 8ea05e3 | 2012-07-25 17:35:53 +0200 | [diff] [blame] | 6556 | btrfs_update_root_times(trans, root); |
| 6557 | |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 6558 | ret = btrfs_inode_inherit_props(trans, inode, dir); |
| 6559 | if (ret) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6560 | btrfs_err(fs_info, |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 6561 | "error inheriting props for ino %llu (root %llu): %d", |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 6562 | btrfs_ino(BTRFS_I(inode)), root->root_key.objectid, ret); |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 6563 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6564 | return inode; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6565 | |
| 6566 | fail_unlock: |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6567 | discard_new_inode(inode); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6568 | fail: |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 6569 | if (dir && name) |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6570 | BTRFS_I(dir)->index_cnt--; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6571 | btrfs_free_path(path); |
| 6572 | return ERR_PTR(ret); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6573 | } |
| 6574 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 6575 | /* |
| 6576 | * utility function to add 'inode' into 'parent_inode' with |
| 6577 | * a give name and a given sequence number. |
| 6578 | * if 'add_backref' is true, also insert a backref from the |
| 6579 | * inode to the parent directory. |
| 6580 | */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 6581 | int btrfs_add_link(struct btrfs_trans_handle *trans, |
Nikolay Borisov | db0a669 | 2017-02-20 13:51:08 +0200 | [diff] [blame] | 6582 | struct btrfs_inode *parent_inode, struct btrfs_inode *inode, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 6583 | const char *name, int name_len, int add_backref, u64 index) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6584 | { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6585 | int ret = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6586 | struct btrfs_key key; |
Nikolay Borisov | db0a669 | 2017-02-20 13:51:08 +0200 | [diff] [blame] | 6587 | struct btrfs_root *root = parent_inode->root; |
| 6588 | u64 ino = btrfs_ino(inode); |
| 6589 | u64 parent_ino = btrfs_ino(parent_inode); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6590 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6591 | if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) { |
Nikolay Borisov | db0a669 | 2017-02-20 13:51:08 +0200 | [diff] [blame] | 6592 | memcpy(&key, &inode->root->root_key, sizeof(key)); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6593 | } else { |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6594 | key.objectid = ino; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 6595 | key.type = BTRFS_INODE_ITEM_KEY; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6596 | key.offset = 0; |
| 6597 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6598 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6599 | if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) { |
Lu Fengqi | 6025c19 | 2018-08-01 11:32:29 +0800 | [diff] [blame] | 6600 | ret = btrfs_add_root_ref(trans, key.objectid, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6601 | root->root_key.objectid, parent_ino, |
| 6602 | index, name, name_len); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6603 | } else if (add_backref) { |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6604 | ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino, |
| 6605 | parent_ino, index); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6606 | } |
| 6607 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6608 | /* Nothing to clean up yet */ |
| 6609 | if (ret) |
| 6610 | return ret; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6611 | |
Lu Fengqi | 684572d | 2018-08-04 21:10:57 +0800 | [diff] [blame] | 6612 | ret = btrfs_insert_dir_item(trans, name, name_len, parent_inode, &key, |
Nikolay Borisov | db0a669 | 2017-02-20 13:51:08 +0200 | [diff] [blame] | 6613 | btrfs_inode_type(&inode->vfs_inode), index); |
Chris Mason | 9c52057 | 2012-12-17 14:26:57 -0500 | [diff] [blame] | 6614 | if (ret == -EEXIST || ret == -EOVERFLOW) |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6615 | goto fail_dir_item; |
| 6616 | else if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 6617 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6618 | return ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6619 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6620 | |
Nikolay Borisov | db0a669 | 2017-02-20 13:51:08 +0200 | [diff] [blame] | 6621 | btrfs_i_size_write(parent_inode, parent_inode->vfs_inode.i_size + |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6622 | name_len * 2); |
Nikolay Borisov | db0a669 | 2017-02-20 13:51:08 +0200 | [diff] [blame] | 6623 | inode_inc_iversion(&parent_inode->vfs_inode); |
Filipe Manana | 5338e43 | 2019-05-15 16:02:47 +0100 | [diff] [blame] | 6624 | /* |
| 6625 | * If we are replaying a log tree, we do not want to update the mtime |
| 6626 | * and ctime of the parent directory with the current time, since the |
| 6627 | * log replay procedure is responsible for setting them to their correct |
| 6628 | * values (the ones it had when the fsync was done). |
| 6629 | */ |
| 6630 | if (!test_bit(BTRFS_FS_LOG_RECOVERING, &root->fs_info->flags)) { |
| 6631 | struct timespec64 now = current_time(&parent_inode->vfs_inode); |
| 6632 | |
| 6633 | parent_inode->vfs_inode.i_mtime = now; |
| 6634 | parent_inode->vfs_inode.i_ctime = now; |
| 6635 | } |
Nikolay Borisov | 9a56fcd | 2020-11-02 16:48:59 +0200 | [diff] [blame] | 6636 | ret = btrfs_update_inode(trans, root, parent_inode); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6637 | if (ret) |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 6638 | btrfs_abort_transaction(trans, ret); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6639 | return ret; |
Chris Mason | fe66a05 | 2012-02-20 08:40:56 -0500 | [diff] [blame] | 6640 | |
| 6641 | fail_dir_item: |
| 6642 | if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) { |
| 6643 | u64 local_index; |
| 6644 | int err; |
Lu Fengqi | 3ee1c55 | 2018-08-01 11:32:28 +0800 | [diff] [blame] | 6645 | err = btrfs_del_root_ref(trans, key.objectid, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6646 | root->root_key.objectid, parent_ino, |
| 6647 | &local_index, name, name_len); |
Johannes Thumshirn | 1690dd41 | 2018-12-12 15:14:17 +0100 | [diff] [blame] | 6648 | if (err) |
| 6649 | btrfs_abort_transaction(trans, err); |
Chris Mason | fe66a05 | 2012-02-20 08:40:56 -0500 | [diff] [blame] | 6650 | } else if (add_backref) { |
| 6651 | u64 local_index; |
| 6652 | int err; |
| 6653 | |
| 6654 | err = btrfs_del_inode_ref(trans, root, name, name_len, |
| 6655 | ino, parent_ino, &local_index); |
Johannes Thumshirn | 1690dd41 | 2018-12-12 15:14:17 +0100 | [diff] [blame] | 6656 | if (err) |
| 6657 | btrfs_abort_transaction(trans, err); |
Chris Mason | fe66a05 | 2012-02-20 08:40:56 -0500 | [diff] [blame] | 6658 | } |
Johannes Thumshirn | 1690dd41 | 2018-12-12 15:14:17 +0100 | [diff] [blame] | 6659 | |
| 6660 | /* Return the original error code */ |
Chris Mason | fe66a05 | 2012-02-20 08:40:56 -0500 | [diff] [blame] | 6661 | return ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6662 | } |
| 6663 | |
| 6664 | static int btrfs_add_nondir(struct btrfs_trans_handle *trans, |
Nikolay Borisov | cef415a | 2017-02-20 13:51:09 +0200 | [diff] [blame] | 6665 | struct btrfs_inode *dir, struct dentry *dentry, |
| 6666 | struct btrfs_inode *inode, int backref, u64 index) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6667 | { |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 6668 | int err = btrfs_add_link(trans, dir, inode, |
| 6669 | dentry->d_name.name, dentry->d_name.len, |
| 6670 | backref, index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6671 | if (err > 0) |
| 6672 | err = -EEXIST; |
| 6673 | return err; |
| 6674 | } |
| 6675 | |
Christian Brauner | 549c729 | 2021-01-21 14:19:43 +0100 | [diff] [blame] | 6676 | static int btrfs_mknod(struct user_namespace *mnt_userns, struct inode *dir, |
| 6677 | struct dentry *dentry, umode_t mode, dev_t rdev) |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6678 | { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6679 | struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6680 | struct btrfs_trans_handle *trans; |
| 6681 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 6682 | struct inode *inode = NULL; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6683 | int err; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6684 | u64 objectid; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 6685 | u64 index = 0; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6686 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 6687 | /* |
| 6688 | * 2 for inode item and ref |
| 6689 | * 2 for dir items |
| 6690 | * 1 for xattr if selinux is on |
| 6691 | */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6692 | trans = btrfs_start_transaction(root, 5); |
| 6693 | if (IS_ERR(trans)) |
| 6694 | return PTR_ERR(trans); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 6695 | |
Nikolay Borisov | 543068a | 2020-12-07 17:32:33 +0200 | [diff] [blame] | 6696 | err = btrfs_get_free_objectid(root, &objectid); |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 6697 | if (err) |
| 6698 | goto out_unlock; |
| 6699 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6700 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 6701 | dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid, |
| 6702 | mode, &index); |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 6703 | if (IS_ERR(inode)) { |
| 6704 | err = PTR_ERR(inode); |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6705 | inode = NULL; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6706 | goto out_unlock; |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 6707 | } |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6708 | |
Casey Schaufler | ad19db7 | 2011-12-15 10:09:07 -0500 | [diff] [blame] | 6709 | /* |
| 6710 | * If the active LSM wants to access the inode during |
| 6711 | * d_instantiate it needs these. Smack checks to see |
| 6712 | * if the filesystem supports xattrs by looking at the |
| 6713 | * ops vector. |
| 6714 | */ |
Casey Schaufler | ad19db7 | 2011-12-15 10:09:07 -0500 | [diff] [blame] | 6715 | inode->i_op = &btrfs_special_inode_operations; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6716 | init_special_inode(inode, inode->i_mode, rdev); |
| 6717 | |
| 6718 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6719 | if (err) |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6720 | goto out_unlock; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6721 | |
Nikolay Borisov | cef415a | 2017-02-20 13:51:09 +0200 | [diff] [blame] | 6722 | err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode), |
| 6723 | 0, index); |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6724 | if (err) |
| 6725 | goto out_unlock; |
| 6726 | |
Nikolay Borisov | 9a56fcd | 2020-11-02 16:48:59 +0200 | [diff] [blame] | 6727 | btrfs_update_inode(trans, root, BTRFS_I(inode)); |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6728 | d_instantiate_new(dentry, inode); |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6729 | |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6730 | out_unlock: |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 6731 | btrfs_end_transaction(trans); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6732 | btrfs_btree_balance_dirty(fs_info); |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6733 | if (err && inode) { |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6734 | inode_dec_link_count(inode); |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6735 | discard_new_inode(inode); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6736 | } |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6737 | return err; |
| 6738 | } |
| 6739 | |
Christian Brauner | 549c729 | 2021-01-21 14:19:43 +0100 | [diff] [blame] | 6740 | static int btrfs_create(struct user_namespace *mnt_userns, struct inode *dir, |
| 6741 | struct dentry *dentry, umode_t mode, bool excl) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6742 | { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6743 | struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6744 | struct btrfs_trans_handle *trans; |
| 6745 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 6746 | struct inode *inode = NULL; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6747 | int err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6748 | u64 objectid; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 6749 | u64 index = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6750 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 6751 | /* |
| 6752 | * 2 for inode item and ref |
| 6753 | * 2 for dir items |
| 6754 | * 1 for xattr if selinux is on |
| 6755 | */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6756 | trans = btrfs_start_transaction(root, 5); |
| 6757 | if (IS_ERR(trans)) |
| 6758 | return PTR_ERR(trans); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 6759 | |
Nikolay Borisov | 543068a | 2020-12-07 17:32:33 +0200 | [diff] [blame] | 6760 | err = btrfs_get_free_objectid(root, &objectid); |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 6761 | if (err) |
| 6762 | goto out_unlock; |
| 6763 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6764 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 6765 | dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid, |
| 6766 | mode, &index); |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 6767 | if (IS_ERR(inode)) { |
| 6768 | err = PTR_ERR(inode); |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6769 | inode = NULL; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6770 | goto out_unlock; |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 6771 | } |
Casey Schaufler | ad19db7 | 2011-12-15 10:09:07 -0500 | [diff] [blame] | 6772 | /* |
| 6773 | * If the active LSM wants to access the inode during |
| 6774 | * d_instantiate it needs these. Smack checks to see |
| 6775 | * if the filesystem supports xattrs by looking at the |
| 6776 | * ops vector. |
| 6777 | */ |
| 6778 | inode->i_fop = &btrfs_file_operations; |
| 6779 | inode->i_op = &btrfs_file_inode_operations; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6780 | inode->i_mapping->a_ops = &btrfs_aops; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6781 | |
| 6782 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); |
| 6783 | if (err) |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6784 | goto out_unlock; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6785 | |
Nikolay Borisov | 9a56fcd | 2020-11-02 16:48:59 +0200 | [diff] [blame] | 6786 | err = btrfs_update_inode(trans, root, BTRFS_I(inode)); |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6787 | if (err) |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6788 | goto out_unlock; |
Casey Schaufler | ad19db7 | 2011-12-15 10:09:07 -0500 | [diff] [blame] | 6789 | |
Nikolay Borisov | cef415a | 2017-02-20 13:51:09 +0200 | [diff] [blame] | 6790 | err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode), |
| 6791 | 0, index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6792 | if (err) |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6793 | goto out_unlock; |
Filipe Brandenburger | 43baa57 | 2012-11-30 03:40:09 +0000 | [diff] [blame] | 6794 | |
Al Viro | 1e2e547 | 2018-05-04 08:23:01 -0400 | [diff] [blame] | 6795 | d_instantiate_new(dentry, inode); |
Filipe Brandenburger | 43baa57 | 2012-11-30 03:40:09 +0000 | [diff] [blame] | 6796 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6797 | out_unlock: |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 6798 | btrfs_end_transaction(trans); |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6799 | if (err && inode) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6800 | inode_dec_link_count(inode); |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6801 | discard_new_inode(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6802 | } |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6803 | btrfs_btree_balance_dirty(fs_info); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6804 | return err; |
| 6805 | } |
| 6806 | |
| 6807 | static int btrfs_link(struct dentry *old_dentry, struct inode *dir, |
| 6808 | struct dentry *dentry) |
| 6809 | { |
Filipe Manana | 271dba45 | 2016-01-05 16:24:05 +0000 | [diff] [blame] | 6810 | struct btrfs_trans_handle *trans = NULL; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6811 | struct btrfs_root *root = BTRFS_I(dir)->root; |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 6812 | struct inode *inode = d_inode(old_dentry); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6813 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 6814 | u64 index; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6815 | int err; |
| 6816 | int drop_inode = 0; |
| 6817 | |
TARUISI Hiroaki | 4a8be42 | 2009-11-12 07:14:26 +0000 | [diff] [blame] | 6818 | /* 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] | 6819 | if (root->root_key.objectid != BTRFS_I(inode)->root->root_key.objectid) |
Mark Fasheh | 3ab3564 | 2011-03-22 17:20:26 +0000 | [diff] [blame] | 6820 | return -EXDEV; |
TARUISI Hiroaki | 4a8be42 | 2009-11-12 07:14:26 +0000 | [diff] [blame] | 6821 | |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 6822 | if (inode->i_nlink >= BTRFS_LINK_MAX) |
Al Viro | c055e99 | 2011-03-04 17:15:18 +0000 | [diff] [blame] | 6823 | return -EMLINK; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 6824 | |
Nikolay Borisov | 877574e | 2017-02-20 13:50:33 +0200 | [diff] [blame] | 6825 | err = btrfs_set_inode_index(BTRFS_I(dir), &index); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6826 | if (err) |
| 6827 | goto fail; |
| 6828 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6829 | /* |
Miao Xie | 7e6b646 | 2011-02-18 09:21:17 +0000 | [diff] [blame] | 6830 | * 2 items for inode and inode ref |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6831 | * 2 items for dir items |
Miao Xie | 7e6b646 | 2011-02-18 09:21:17 +0000 | [diff] [blame] | 6832 | * 1 item for parent inode |
Omar Sandoval | 399b0bb | 2018-05-11 13:13:40 -0700 | [diff] [blame] | 6833 | * 1 item for orphan item deletion if O_TMPFILE |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6834 | */ |
Omar Sandoval | 399b0bb | 2018-05-11 13:13:40 -0700 | [diff] [blame] | 6835 | trans = btrfs_start_transaction(root, inode->i_nlink ? 5 : 6); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6836 | if (IS_ERR(trans)) { |
| 6837 | err = PTR_ERR(trans); |
Filipe Manana | 271dba45 | 2016-01-05 16:24:05 +0000 | [diff] [blame] | 6838 | trans = NULL; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6839 | goto fail; |
| 6840 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6841 | |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 6842 | /* There are several dir indexes for this inode, clear the cache. */ |
| 6843 | BTRFS_I(inode)->dir_index = 0ULL; |
Zach Brown | 8b558c5 | 2013-10-16 12:10:34 -0700 | [diff] [blame] | 6844 | inc_nlink(inode); |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 6845 | inode_inc_iversion(inode); |
Deepa Dinamani | c2050a4 | 2016-09-14 07:48:06 -0700 | [diff] [blame] | 6846 | inode->i_ctime = current_time(inode); |
Al Viro | 7de9c6ee | 2010-10-23 11:11:40 -0400 | [diff] [blame] | 6847 | ihold(inode); |
Josef Bacik | e997615 | 2012-10-11 15:53:56 -0400 | [diff] [blame] | 6848 | set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6849 | |
Nikolay Borisov | cef415a | 2017-02-20 13:51:09 +0200 | [diff] [blame] | 6850 | err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode), |
| 6851 | 1, index); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6852 | |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 6853 | if (err) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6854 | drop_inode = 1; |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 6855 | } else { |
Al Viro | 10d9f30 | 2011-07-16 23:09:10 -0400 | [diff] [blame] | 6856 | struct dentry *parent = dentry->d_parent; |
Filipe Manana | d4682ba | 2018-06-11 19:24:28 +0100 | [diff] [blame] | 6857 | |
Nikolay Borisov | 9a56fcd | 2020-11-02 16:48:59 +0200 | [diff] [blame] | 6858 | err = btrfs_update_inode(trans, root, BTRFS_I(inode)); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6859 | if (err) |
| 6860 | goto fail; |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 6861 | if (inode->i_nlink == 1) { |
| 6862 | /* |
| 6863 | * If new hard link count is 1, it's a file created |
| 6864 | * with open(2) O_TMPFILE flag. |
| 6865 | */ |
Nikolay Borisov | 3d6ae7b | 2017-02-20 13:50:58 +0200 | [diff] [blame] | 6866 | err = btrfs_orphan_del(trans, BTRFS_I(inode)); |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 6867 | if (err) |
| 6868 | goto fail; |
| 6869 | } |
Al Viro | 08c422c | 2011-12-23 07:58:13 -0500 | [diff] [blame] | 6870 | d_instantiate(dentry, inode); |
Filipe Manana | 75b463d | 2020-08-11 12:43:48 +0100 | [diff] [blame] | 6871 | btrfs_log_new_name(trans, BTRFS_I(inode), NULL, parent); |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 6872 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6873 | |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 6874 | fail: |
Filipe Manana | 271dba45 | 2016-01-05 16:24:05 +0000 | [diff] [blame] | 6875 | if (trans) |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 6876 | btrfs_end_transaction(trans); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6877 | if (drop_inode) { |
| 6878 | inode_dec_link_count(inode); |
| 6879 | iput(inode); |
| 6880 | } |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6881 | btrfs_btree_balance_dirty(fs_info); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6882 | return err; |
| 6883 | } |
| 6884 | |
Christian Brauner | 549c729 | 2021-01-21 14:19:43 +0100 | [diff] [blame] | 6885 | static int btrfs_mkdir(struct user_namespace *mnt_userns, struct inode *dir, |
| 6886 | struct dentry *dentry, umode_t mode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6887 | { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6888 | struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); |
Chris Mason | b9d8666 | 2008-05-02 16:13:49 -0400 | [diff] [blame] | 6889 | struct inode *inode = NULL; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6890 | struct btrfs_trans_handle *trans; |
| 6891 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 6892 | int err = 0; |
Chris Mason | b9d8666 | 2008-05-02 16:13:49 -0400 | [diff] [blame] | 6893 | u64 objectid = 0; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 6894 | u64 index = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6895 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 6896 | /* |
| 6897 | * 2 items for inode and ref |
| 6898 | * 2 items for dir items |
| 6899 | * 1 for xattr if selinux is on |
| 6900 | */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6901 | trans = btrfs_start_transaction(root, 5); |
| 6902 | if (IS_ERR(trans)) |
| 6903 | return PTR_ERR(trans); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6904 | |
Nikolay Borisov | 543068a | 2020-12-07 17:32:33 +0200 | [diff] [blame] | 6905 | err = btrfs_get_free_objectid(root, &objectid); |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 6906 | if (err) |
| 6907 | goto out_fail; |
| 6908 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6909 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 6910 | dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid, |
| 6911 | S_IFDIR | mode, &index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6912 | if (IS_ERR(inode)) { |
| 6913 | err = PTR_ERR(inode); |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6914 | inode = NULL; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6915 | goto out_fail; |
| 6916 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6917 | |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6918 | /* these must be set before we unlock the inode */ |
| 6919 | inode->i_op = &btrfs_dir_inode_operations; |
| 6920 | inode->i_fop = &btrfs_dir_file_operations; |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 6921 | |
Eric Paris | 2a7dba3 | 2011-02-01 11:05:39 -0500 | [diff] [blame] | 6922 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 6923 | if (err) |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6924 | goto out_fail; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6925 | |
Nikolay Borisov | 6ef06d2 | 2017-02-20 13:50:34 +0200 | [diff] [blame] | 6926 | btrfs_i_size_write(BTRFS_I(inode), 0); |
Nikolay Borisov | 9a56fcd | 2020-11-02 16:48:59 +0200 | [diff] [blame] | 6927 | err = btrfs_update_inode(trans, root, BTRFS_I(inode)); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6928 | if (err) |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6929 | goto out_fail; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6930 | |
Nikolay Borisov | db0a669 | 2017-02-20 13:51:08 +0200 | [diff] [blame] | 6931 | err = btrfs_add_link(trans, BTRFS_I(dir), BTRFS_I(inode), |
| 6932 | dentry->d_name.name, |
| 6933 | dentry->d_name.len, 0, index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6934 | if (err) |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6935 | goto out_fail; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6936 | |
Al Viro | 1e2e547 | 2018-05-04 08:23:01 -0400 | [diff] [blame] | 6937 | d_instantiate_new(dentry, inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6938 | |
| 6939 | out_fail: |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 6940 | btrfs_end_transaction(trans); |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6941 | if (err && inode) { |
Wang Shilong | c7cfb8a | 2014-12-24 14:45:30 +0800 | [diff] [blame] | 6942 | inode_dec_link_count(inode); |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6943 | discard_new_inode(inode); |
Wang Shilong | c7cfb8a | 2014-12-24 14:45:30 +0800 | [diff] [blame] | 6944 | } |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6945 | btrfs_btree_balance_dirty(fs_info); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6946 | return err; |
| 6947 | } |
| 6948 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6949 | static noinline int uncompress_inline(struct btrfs_path *path, |
Byongho Lee | e40da0e | 2015-05-19 23:46:45 +0900 | [diff] [blame] | 6950 | struct page *page, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6951 | size_t pg_offset, u64 extent_offset, |
| 6952 | struct btrfs_file_extent_item *item) |
| 6953 | { |
| 6954 | int ret; |
| 6955 | struct extent_buffer *leaf = path->nodes[0]; |
| 6956 | char *tmp; |
| 6957 | size_t max_size; |
| 6958 | unsigned long inline_size; |
| 6959 | unsigned long ptr; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 6960 | int compress_type; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6961 | |
| 6962 | WARN_ON(pg_offset != 0); |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 6963 | compress_type = btrfs_file_extent_compression(leaf, item); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6964 | max_size = btrfs_file_extent_ram_bytes(leaf, item); |
| 6965 | inline_size = btrfs_file_extent_inline_item_len(leaf, |
Ross Kirk | dd3cc16 | 2013-09-16 15:58:09 +0100 | [diff] [blame] | 6966 | btrfs_item_nr(path->slots[0])); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6967 | tmp = kmalloc(inline_size, GFP_NOFS); |
Tsutomu Itoh | 8d41371 | 2011-04-25 19:43:52 -0400 | [diff] [blame] | 6968 | if (!tmp) |
| 6969 | return -ENOMEM; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6970 | ptr = btrfs_file_extent_inline_start(item); |
| 6971 | |
| 6972 | read_extent_buffer(leaf, tmp, ptr, inline_size); |
| 6973 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 6974 | max_size = min_t(unsigned long, PAGE_SIZE, max_size); |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 6975 | ret = btrfs_decompress(compress_type, tmp, page, |
| 6976 | extent_offset, inline_size, max_size); |
Zygo Blaxell | e1699d2 | 2017-03-10 16:45:44 -0500 | [diff] [blame] | 6977 | |
| 6978 | /* |
| 6979 | * decompression code contains a memset to fill in any space between the end |
| 6980 | * of the uncompressed data and the end of max_size in case the decompressed |
| 6981 | * data ends up shorter than ram_bytes. That doesn't cover the hole between |
| 6982 | * the end of an inline extent and the beginning of the next block, so we |
| 6983 | * cover that region here. |
| 6984 | */ |
| 6985 | |
Ira Weiny | d048b9c | 2021-05-04 18:40:07 -0700 | [diff] [blame] | 6986 | if (max_size + pg_offset < PAGE_SIZE) |
| 6987 | memzero_page(page, pg_offset + max_size, |
| 6988 | PAGE_SIZE - max_size - pg_offset); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6989 | kfree(tmp); |
Zach Brown | 166ae5a | 2014-05-09 17:15:10 -0400 | [diff] [blame] | 6990 | return ret; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6991 | } |
| 6992 | |
Omar Sandoval | 39b07b5 | 2019-12-02 17:34:23 -0800 | [diff] [blame] | 6993 | /** |
| 6994 | * btrfs_get_extent - Lookup the first extent overlapping a range in a file. |
| 6995 | * @inode: file to search in |
| 6996 | * @page: page to read extent data into if the extent is inline |
| 6997 | * @pg_offset: offset into @page to copy to |
| 6998 | * @start: file offset |
| 6999 | * @len: length of range starting at @start |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 7000 | * |
Omar Sandoval | 39b07b5 | 2019-12-02 17:34:23 -0800 | [diff] [blame] | 7001 | * This returns the first &struct extent_map which overlaps with the given |
| 7002 | * range, reading it from the B-tree and caching it if necessary. Note that |
| 7003 | * there may be more extents which overlap the given range after the returned |
| 7004 | * extent_map. |
| 7005 | * |
| 7006 | * If @page is not NULL and the extent is inline, this also reads the extent |
| 7007 | * data directly into the page and marks the extent up to date in the io_tree. |
| 7008 | * |
| 7009 | * Return: ERR_PTR on error, non-NULL extent_map on success. |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 7010 | */ |
Nikolay Borisov | fc4f21b1 | 2017-02-20 13:51:06 +0200 | [diff] [blame] | 7011 | struct extent_map *btrfs_get_extent(struct btrfs_inode *inode, |
Omar Sandoval | 39b07b5 | 2019-12-02 17:34:23 -0800 | [diff] [blame] | 7012 | struct page *page, size_t pg_offset, |
| 7013 | u64 start, u64 len) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7014 | { |
David Sterba | 3ffbd68 | 2018-06-29 10:56:42 +0200 | [diff] [blame] | 7015 | struct btrfs_fs_info *fs_info = inode->root->fs_info; |
Nikolay Borisov | 1028d1c | 2020-08-03 12:58:46 +0300 | [diff] [blame] | 7016 | int ret = 0; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7017 | u64 extent_start = 0; |
| 7018 | u64 extent_end = 0; |
Nikolay Borisov | fc4f21b1 | 2017-02-20 13:51:06 +0200 | [diff] [blame] | 7019 | u64 objectid = btrfs_ino(inode); |
Linus Torvalds | 7e74e23 | 2019-05-01 12:19:20 -0700 | [diff] [blame] | 7020 | int extent_type = -1; |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 7021 | struct btrfs_path *path = NULL; |
Nikolay Borisov | fc4f21b1 | 2017-02-20 13:51:06 +0200 | [diff] [blame] | 7022 | struct btrfs_root *root = inode->root; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7023 | struct btrfs_file_extent_item *item; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 7024 | struct extent_buffer *leaf; |
| 7025 | struct btrfs_key found_key; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7026 | struct extent_map *em = NULL; |
Nikolay Borisov | fc4f21b1 | 2017-02-20 13:51:06 +0200 | [diff] [blame] | 7027 | struct extent_map_tree *em_tree = &inode->extent_tree; |
| 7028 | struct extent_io_tree *io_tree = &inode->io_tree; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7029 | |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 7030 | read_lock(&em_tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 7031 | em = lookup_extent_mapping(em_tree, start, len); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 7032 | read_unlock(&em_tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 7033 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7034 | if (em) { |
Chris Mason | e1c4b74 | 2008-04-22 13:26:46 -0400 | [diff] [blame] | 7035 | if (em->start > start || em->start + em->len <= start) |
| 7036 | free_extent_map(em); |
| 7037 | else if (em->block_start == EXTENT_MAP_INLINE && page) |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 7038 | free_extent_map(em); |
| 7039 | else |
| 7040 | goto out; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7041 | } |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 7042 | em = alloc_extent_map(); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7043 | if (!em) { |
Nikolay Borisov | 1028d1c | 2020-08-03 12:58:46 +0300 | [diff] [blame] | 7044 | ret = -ENOMEM; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 7045 | goto out; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7046 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 7047 | em->start = EXTENT_MAP_HOLE; |
Chris Mason | 445a694 | 2008-11-10 11:53:33 -0500 | [diff] [blame] | 7048 | em->orig_start = EXTENT_MAP_HOLE; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 7049 | em->len = (u64)-1; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 7050 | em->block_len = (u64)-1; |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 7051 | |
Liu Bo | bee6ec8 | 2018-08-17 05:05:28 +0800 | [diff] [blame] | 7052 | path = btrfs_alloc_path(); |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 7053 | if (!path) { |
Nikolay Borisov | 1028d1c | 2020-08-03 12:58:46 +0300 | [diff] [blame] | 7054 | ret = -ENOMEM; |
Liu Bo | bee6ec8 | 2018-08-17 05:05:28 +0800 | [diff] [blame] | 7055 | goto out; |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 7056 | } |
| 7057 | |
Liu Bo | bee6ec8 | 2018-08-17 05:05:28 +0800 | [diff] [blame] | 7058 | /* Chances are we'll be called again, so go ahead and do readahead */ |
| 7059 | path->reada = READA_FORWARD; |
Josef Bacik | 4d7240f | 2020-10-23 09:58:09 -0400 | [diff] [blame] | 7060 | |
| 7061 | /* |
| 7062 | * The same explanation in load_free_space_cache applies here as well, |
| 7063 | * we only read when we're loading the free space cache, and at that |
| 7064 | * point the commit_root has everything we need. |
| 7065 | */ |
| 7066 | if (btrfs_is_free_space_inode(inode)) { |
| 7067 | path->search_commit_root = 1; |
| 7068 | path->skip_locking = 1; |
| 7069 | } |
Josef Bacik | 5189941 | 2020-08-20 11:46:01 -0400 | [diff] [blame] | 7070 | |
Nikolay Borisov | 5c9a702 | 2017-12-01 11:19:40 +0200 | [diff] [blame] | 7071 | ret = btrfs_lookup_file_extent(NULL, root, path, objectid, start, 0); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7072 | if (ret < 0) { |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7073 | goto out; |
Nikolay Borisov | b8eeab7 | 2018-12-17 11:49:00 +0200 | [diff] [blame] | 7074 | } else if (ret > 0) { |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7075 | if (path->slots[0] == 0) |
| 7076 | goto not_found; |
| 7077 | path->slots[0]--; |
Nikolay Borisov | 1028d1c | 2020-08-03 12:58:46 +0300 | [diff] [blame] | 7078 | ret = 0; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7079 | } |
| 7080 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 7081 | leaf = path->nodes[0]; |
| 7082 | item = btrfs_item_ptr(leaf, path->slots[0], |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7083 | struct btrfs_file_extent_item); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 7084 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 7085 | if (found_key.objectid != objectid || |
Nikolay Borisov | 694c12e | 2018-12-17 10:35:59 +0200 | [diff] [blame] | 7086 | found_key.type != BTRFS_EXTENT_DATA_KEY) { |
Josef Bacik | 25a5034 | 2013-10-14 12:08:38 -0400 | [diff] [blame] | 7087 | /* |
| 7088 | * If we backup past the first extent we want to move forward |
| 7089 | * and see if there is an extent in front of us, otherwise we'll |
| 7090 | * say there is a hole for our whole search range which can |
| 7091 | * cause problems. |
| 7092 | */ |
| 7093 | extent_end = start; |
| 7094 | goto next; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7095 | } |
| 7096 | |
Nikolay Borisov | 694c12e | 2018-12-17 10:35:59 +0200 | [diff] [blame] | 7097 | extent_type = btrfs_file_extent_type(leaf, item); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 7098 | extent_start = found_key.offset; |
Filipe Manana | a5eeb3d | 2020-03-09 12:41:06 +0000 | [diff] [blame] | 7099 | extent_end = btrfs_file_extent_end(path); |
Nikolay Borisov | 694c12e | 2018-12-17 10:35:59 +0200 | [diff] [blame] | 7100 | if (extent_type == BTRFS_FILE_EXTENT_REG || |
| 7101 | extent_type == BTRFS_FILE_EXTENT_PREALLOC) { |
Qu Wenruo | 6bf9e4b | 2019-03-13 13:55:11 +0800 | [diff] [blame] | 7102 | /* Only regular file could have regular/prealloc extent */ |
| 7103 | if (!S_ISREG(inode->vfs_inode.i_mode)) { |
Nikolay Borisov | 1028d1c | 2020-08-03 12:58:46 +0300 | [diff] [blame] | 7104 | ret = -EUCLEAN; |
Qu Wenruo | 6bf9e4b | 2019-03-13 13:55:11 +0800 | [diff] [blame] | 7105 | btrfs_crit(fs_info, |
| 7106 | "regular/prealloc extent found for non-regular inode %llu", |
| 7107 | btrfs_ino(inode)); |
| 7108 | goto out; |
| 7109 | } |
Liu Bo | 09ed2f1 | 2017-03-10 11:09:48 -0800 | [diff] [blame] | 7110 | trace_btrfs_get_extent_show_fi_regular(inode, leaf, item, |
| 7111 | extent_start); |
Nikolay Borisov | 694c12e | 2018-12-17 10:35:59 +0200 | [diff] [blame] | 7112 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
Liu Bo | 09ed2f1 | 2017-03-10 11:09:48 -0800 | [diff] [blame] | 7113 | trace_btrfs_get_extent_show_fi_inline(inode, leaf, item, |
| 7114 | path->slots[0], |
| 7115 | extent_start); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 7116 | } |
Josef Bacik | 25a5034 | 2013-10-14 12:08:38 -0400 | [diff] [blame] | 7117 | next: |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 7118 | if (start >= extent_end) { |
| 7119 | path->slots[0]++; |
| 7120 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { |
| 7121 | ret = btrfs_next_leaf(root, path); |
Nikolay Borisov | 1028d1c | 2020-08-03 12:58:46 +0300 | [diff] [blame] | 7122 | if (ret < 0) |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 7123 | goto out; |
Nikolay Borisov | 1028d1c | 2020-08-03 12:58:46 +0300 | [diff] [blame] | 7124 | else if (ret > 0) |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 7125 | goto not_found; |
Nikolay Borisov | 1028d1c | 2020-08-03 12:58:46 +0300 | [diff] [blame] | 7126 | |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 7127 | leaf = path->nodes[0]; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7128 | } |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 7129 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 7130 | if (found_key.objectid != objectid || |
| 7131 | found_key.type != BTRFS_EXTENT_DATA_KEY) |
| 7132 | goto not_found; |
| 7133 | if (start + len <= found_key.offset) |
| 7134 | goto not_found; |
Wang Shilong | e2eca69 | 2014-07-17 11:44:14 +0800 | [diff] [blame] | 7135 | if (start > found_key.offset) |
| 7136 | goto next; |
Nikolay Borisov | 02a033d | 2018-12-17 10:36:02 +0200 | [diff] [blame] | 7137 | |
| 7138 | /* New extent overlaps with existing one */ |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 7139 | em->start = start; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 7140 | em->orig_start = start; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 7141 | em->len = found_key.offset - start; |
Nikolay Borisov | 02a033d | 2018-12-17 10:36:02 +0200 | [diff] [blame] | 7142 | em->block_start = EXTENT_MAP_HOLE; |
| 7143 | goto insert; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 7144 | } |
| 7145 | |
Omar Sandoval | 39b07b5 | 2019-12-02 17:34:23 -0800 | [diff] [blame] | 7146 | btrfs_extent_item_to_extent_map(inode, path, item, !page, em); |
Filipe Manana | 7ffbb59 | 2014-06-09 03:48:05 +0100 | [diff] [blame] | 7147 | |
Nikolay Borisov | 694c12e | 2018-12-17 10:35:59 +0200 | [diff] [blame] | 7148 | if (extent_type == BTRFS_FILE_EXTENT_REG || |
| 7149 | extent_type == BTRFS_FILE_EXTENT_PREALLOC) { |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7150 | goto insert; |
Nikolay Borisov | 694c12e | 2018-12-17 10:35:59 +0200 | [diff] [blame] | 7151 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 7152 | unsigned long ptr; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7153 | char *map; |
Chris Mason | 3326d1b | 2007-10-15 16:18:25 -0400 | [diff] [blame] | 7154 | size_t size; |
| 7155 | size_t extent_offset; |
| 7156 | size_t copy_size; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7157 | |
Omar Sandoval | 39b07b5 | 2019-12-02 17:34:23 -0800 | [diff] [blame] | 7158 | if (!page) |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 7159 | goto out; |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 7160 | |
Qu Wenruo | e41ca58 | 2018-06-06 15:41:49 +0800 | [diff] [blame] | 7161 | size = btrfs_file_extent_ram_bytes(leaf, item); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 7162 | extent_offset = page_offset(page) + pg_offset - extent_start; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 7163 | copy_size = min_t(u64, PAGE_SIZE - pg_offset, |
| 7164 | size - extent_offset); |
Chris Mason | 3326d1b | 2007-10-15 16:18:25 -0400 | [diff] [blame] | 7165 | em->start = extent_start + extent_offset; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7166 | em->len = ALIGN(copy_size, fs_info->sectorsize); |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 7167 | em->orig_block_len = em->len; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 7168 | em->orig_start = em->start; |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 7169 | ptr = btrfs_file_extent_inline_start(item) + extent_offset; |
Liu Bo | e49aabd | 2018-08-25 13:47:09 +0800 | [diff] [blame] | 7170 | |
Edmund Nadolski | bf46f52 | 2017-11-20 13:24:49 -0700 | [diff] [blame] | 7171 | if (!PageUptodate(page)) { |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 7172 | if (btrfs_file_extent_compression(leaf, item) != |
| 7173 | BTRFS_COMPRESS_NONE) { |
Byongho Lee | e40da0e | 2015-05-19 23:46:45 +0900 | [diff] [blame] | 7174 | ret = uncompress_inline(path, page, pg_offset, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 7175 | extent_offset, item); |
Nikolay Borisov | 1028d1c | 2020-08-03 12:58:46 +0300 | [diff] [blame] | 7176 | if (ret) |
Zach Brown | 166ae5a | 2014-05-09 17:15:10 -0400 | [diff] [blame] | 7177 | goto out; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 7178 | } else { |
Ira Weiny | 58c1a35 | 2021-02-16 18:48:23 -0800 | [diff] [blame] | 7179 | map = kmap_local_page(page); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 7180 | read_extent_buffer(leaf, map + pg_offset, ptr, |
| 7181 | copy_size); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 7182 | if (pg_offset + copy_size < PAGE_SIZE) { |
Chris Mason | 93c82d5 | 2009-09-11 12:36:29 -0400 | [diff] [blame] | 7183 | memset(map + pg_offset + copy_size, 0, |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 7184 | PAGE_SIZE - pg_offset - |
Chris Mason | 93c82d5 | 2009-09-11 12:36:29 -0400 | [diff] [blame] | 7185 | copy_size); |
| 7186 | } |
Ira Weiny | 58c1a35 | 2021-02-16 18:48:23 -0800 | [diff] [blame] | 7187 | kunmap_local(map); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 7188 | } |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 7189 | flush_dcache_page(page); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7190 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 7191 | set_extent_uptodate(io_tree, em->start, |
Arne Jansen | 507903b | 2011-04-06 10:02:20 +0000 | [diff] [blame] | 7192 | extent_map_end(em) - 1, NULL, GFP_NOFS); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7193 | goto insert; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7194 | } |
| 7195 | not_found: |
| 7196 | em->start = start; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 7197 | em->orig_start = start; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 7198 | em->len = len; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 7199 | em->block_start = EXTENT_MAP_HOLE; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7200 | insert: |
Nikolay Borisov | 1028d1c | 2020-08-03 12:58:46 +0300 | [diff] [blame] | 7201 | ret = 0; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 7202 | btrfs_release_path(path); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 7203 | if (em->start > start || extent_map_end(em) <= start) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7204 | btrfs_err(fs_info, |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 7205 | "bad extent! em: [%llu %llu] passed [%llu %llu]", |
| 7206 | em->start, em->len, start, len); |
Nikolay Borisov | 1028d1c | 2020-08-03 12:58:46 +0300 | [diff] [blame] | 7207 | ret = -EIO; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7208 | goto out; |
| 7209 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 7210 | |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 7211 | write_lock(&em_tree->lock); |
Nikolay Borisov | 1028d1c | 2020-08-03 12:58:46 +0300 | [diff] [blame] | 7212 | ret = btrfs_add_extent_mapping(fs_info, em_tree, &em, start, len); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 7213 | write_unlock(&em_tree->lock); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7214 | out: |
Liu Bo | c641428 | 2018-08-23 07:36:17 +0800 | [diff] [blame] | 7215 | btrfs_free_path(path); |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 7216 | |
Nikolay Borisov | fc4f21b1 | 2017-02-20 13:51:06 +0200 | [diff] [blame] | 7217 | trace_btrfs_get_extent(root, inode, em); |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 7218 | |
Nikolay Borisov | 1028d1c | 2020-08-03 12:58:46 +0300 | [diff] [blame] | 7219 | if (ret) { |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7220 | free_extent_map(em); |
Nikolay Borisov | 1028d1c | 2020-08-03 12:58:46 +0300 | [diff] [blame] | 7221 | return ERR_PTR(ret); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7222 | } |
| 7223 | return em; |
| 7224 | } |
| 7225 | |
Nikolay Borisov | fc4f21b1 | 2017-02-20 13:51:06 +0200 | [diff] [blame] | 7226 | struct extent_map *btrfs_get_extent_fiemap(struct btrfs_inode *inode, |
Nikolay Borisov | 4ab47a8 | 2018-12-12 09:42:32 +0200 | [diff] [blame] | 7227 | u64 start, u64 len) |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 7228 | { |
| 7229 | struct extent_map *em; |
| 7230 | struct extent_map *hole_em = NULL; |
Nikolay Borisov | f3714ef | 2018-12-12 09:42:33 +0200 | [diff] [blame] | 7231 | u64 delalloc_start = start; |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 7232 | u64 end; |
Nikolay Borisov | f3714ef | 2018-12-12 09:42:33 +0200 | [diff] [blame] | 7233 | u64 delalloc_len; |
| 7234 | u64 delalloc_end; |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 7235 | int err = 0; |
| 7236 | |
Omar Sandoval | 39b07b5 | 2019-12-02 17:34:23 -0800 | [diff] [blame] | 7237 | em = btrfs_get_extent(inode, NULL, 0, start, len); |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 7238 | if (IS_ERR(em)) |
| 7239 | return em; |
Dan Carpenter | 9986277 | 2017-04-11 11:57:15 +0300 | [diff] [blame] | 7240 | /* |
| 7241 | * If our em maps to: |
| 7242 | * - a hole or |
| 7243 | * - a pre-alloc extent, |
| 7244 | * there might actually be delalloc bytes behind it. |
| 7245 | */ |
| 7246 | if (em->block_start != EXTENT_MAP_HOLE && |
| 7247 | !test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) |
| 7248 | return em; |
| 7249 | else |
| 7250 | hole_em = em; |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 7251 | |
| 7252 | /* check to see if we've wrapped (len == -1 or similar) */ |
| 7253 | end = start + len; |
| 7254 | if (end < start) |
| 7255 | end = (u64)-1; |
| 7256 | else |
| 7257 | end -= 1; |
| 7258 | |
| 7259 | em = NULL; |
| 7260 | |
| 7261 | /* ok, we didn't find anything, lets look for delalloc */ |
Nikolay Borisov | f3714ef | 2018-12-12 09:42:33 +0200 | [diff] [blame] | 7262 | delalloc_len = count_range_bits(&inode->io_tree, &delalloc_start, |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 7263 | end, len, EXTENT_DELALLOC, 1); |
Nikolay Borisov | f3714ef | 2018-12-12 09:42:33 +0200 | [diff] [blame] | 7264 | delalloc_end = delalloc_start + delalloc_len; |
| 7265 | if (delalloc_end < delalloc_start) |
| 7266 | delalloc_end = (u64)-1; |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 7267 | |
| 7268 | /* |
Nikolay Borisov | f3714ef | 2018-12-12 09:42:33 +0200 | [diff] [blame] | 7269 | * We didn't find anything useful, return the original results from |
| 7270 | * get_extent() |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 7271 | */ |
Nikolay Borisov | f3714ef | 2018-12-12 09:42:33 +0200 | [diff] [blame] | 7272 | if (delalloc_start > end || delalloc_end <= start) { |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 7273 | em = hole_em; |
| 7274 | hole_em = NULL; |
| 7275 | goto out; |
| 7276 | } |
| 7277 | |
Nikolay Borisov | f3714ef | 2018-12-12 09:42:33 +0200 | [diff] [blame] | 7278 | /* |
| 7279 | * Adjust the delalloc_start to make sure it doesn't go backwards from |
| 7280 | * the start they passed in |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 7281 | */ |
Nikolay Borisov | f3714ef | 2018-12-12 09:42:33 +0200 | [diff] [blame] | 7282 | delalloc_start = max(start, delalloc_start); |
| 7283 | delalloc_len = delalloc_end - delalloc_start; |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 7284 | |
Nikolay Borisov | f3714ef | 2018-12-12 09:42:33 +0200 | [diff] [blame] | 7285 | if (delalloc_len > 0) { |
| 7286 | u64 hole_start; |
Nikolay Borisov | 02950af | 2018-12-12 09:42:34 +0200 | [diff] [blame] | 7287 | u64 hole_len; |
Nikolay Borisov | f3714ef | 2018-12-12 09:42:33 +0200 | [diff] [blame] | 7288 | const u64 hole_end = extent_map_end(hole_em); |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 7289 | |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 7290 | em = alloc_extent_map(); |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 7291 | if (!em) { |
| 7292 | err = -ENOMEM; |
| 7293 | goto out; |
| 7294 | } |
Nikolay Borisov | f3714ef | 2018-12-12 09:42:33 +0200 | [diff] [blame] | 7295 | |
| 7296 | ASSERT(hole_em); |
| 7297 | /* |
| 7298 | * When btrfs_get_extent can't find anything it returns one |
| 7299 | * huge hole |
| 7300 | * |
| 7301 | * Make sure what it found really fits our range, and adjust to |
| 7302 | * make sure it is based on the start from the caller |
| 7303 | */ |
| 7304 | if (hole_end <= start || hole_em->start > end) { |
| 7305 | free_extent_map(hole_em); |
| 7306 | hole_em = NULL; |
| 7307 | } else { |
| 7308 | hole_start = max(hole_em->start, start); |
| 7309 | hole_len = hole_end - hole_start; |
| 7310 | } |
| 7311 | |
| 7312 | if (hole_em && delalloc_start > hole_start) { |
| 7313 | /* |
| 7314 | * Our hole starts before our delalloc, so we have to |
| 7315 | * return just the parts of the hole that go until the |
| 7316 | * delalloc starts |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 7317 | */ |
Nikolay Borisov | f3714ef | 2018-12-12 09:42:33 +0200 | [diff] [blame] | 7318 | em->len = min(hole_len, delalloc_start - hole_start); |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 7319 | em->start = hole_start; |
| 7320 | em->orig_start = hole_start; |
| 7321 | /* |
Nikolay Borisov | f3714ef | 2018-12-12 09:42:33 +0200 | [diff] [blame] | 7322 | * Don't adjust block start at all, it is fixed at |
| 7323 | * EXTENT_MAP_HOLE |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 7324 | */ |
| 7325 | em->block_start = hole_em->block_start; |
| 7326 | em->block_len = hole_len; |
Liu Bo | f9e4fb5 | 2013-01-07 10:10:12 +0000 | [diff] [blame] | 7327 | if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags)) |
| 7328 | set_bit(EXTENT_FLAG_PREALLOC, &em->flags); |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 7329 | } else { |
Nikolay Borisov | f3714ef | 2018-12-12 09:42:33 +0200 | [diff] [blame] | 7330 | /* |
| 7331 | * Hole is out of passed range or it starts after |
| 7332 | * delalloc range |
| 7333 | */ |
| 7334 | em->start = delalloc_start; |
| 7335 | em->len = delalloc_len; |
| 7336 | em->orig_start = delalloc_start; |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 7337 | em->block_start = EXTENT_MAP_DELALLOC; |
Nikolay Borisov | f3714ef | 2018-12-12 09:42:33 +0200 | [diff] [blame] | 7338 | em->block_len = delalloc_len; |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 7339 | } |
Nikolay Borisov | bf8d32b | 2017-12-01 11:19:43 +0200 | [diff] [blame] | 7340 | } else { |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 7341 | return hole_em; |
| 7342 | } |
| 7343 | out: |
| 7344 | |
| 7345 | free_extent_map(hole_em); |
| 7346 | if (err) { |
| 7347 | free_extent_map(em); |
| 7348 | return ERR_PTR(err); |
| 7349 | } |
| 7350 | return em; |
| 7351 | } |
| 7352 | |
Nikolay Borisov | 64f5418 | 2020-06-03 08:55:31 +0300 | [diff] [blame] | 7353 | static struct extent_map *btrfs_create_dio_extent(struct btrfs_inode *inode, |
Filipe Manana | 5f9a8a5 | 2016-05-12 13:53:36 +0100 | [diff] [blame] | 7354 | const u64 start, |
| 7355 | const u64 len, |
| 7356 | const u64 orig_start, |
| 7357 | const u64 block_start, |
| 7358 | const u64 block_len, |
| 7359 | const u64 orig_block_len, |
| 7360 | const u64 ram_bytes, |
| 7361 | const int type) |
| 7362 | { |
| 7363 | struct extent_map *em = NULL; |
| 7364 | int ret; |
| 7365 | |
Filipe Manana | 5f9a8a5 | 2016-05-12 13:53:36 +0100 | [diff] [blame] | 7366 | if (type != BTRFS_ORDERED_NOCOW) { |
Nikolay Borisov | 64f5418 | 2020-06-03 08:55:31 +0300 | [diff] [blame] | 7367 | em = create_io_em(inode, start, len, orig_start, block_start, |
| 7368 | block_len, orig_block_len, ram_bytes, |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 7369 | BTRFS_COMPRESS_NONE, /* compress_type */ |
| 7370 | type); |
Filipe Manana | 5f9a8a5 | 2016-05-12 13:53:36 +0100 | [diff] [blame] | 7371 | if (IS_ERR(em)) |
| 7372 | goto out; |
| 7373 | } |
Nikolay Borisov | 64f5418 | 2020-06-03 08:55:31 +0300 | [diff] [blame] | 7374 | ret = btrfs_add_ordered_extent_dio(inode, start, block_start, len, |
| 7375 | block_len, type); |
Filipe Manana | 5f9a8a5 | 2016-05-12 13:53:36 +0100 | [diff] [blame] | 7376 | if (ret) { |
| 7377 | if (em) { |
| 7378 | free_extent_map(em); |
Nikolay Borisov | 64f5418 | 2020-06-03 08:55:31 +0300 | [diff] [blame] | 7379 | btrfs_drop_extent_cache(inode, start, start + len - 1, 0); |
Filipe Manana | 5f9a8a5 | 2016-05-12 13:53:36 +0100 | [diff] [blame] | 7380 | } |
| 7381 | em = ERR_PTR(ret); |
| 7382 | } |
| 7383 | out: |
Filipe Manana | 5f9a8a5 | 2016-05-12 13:53:36 +0100 | [diff] [blame] | 7384 | |
| 7385 | return em; |
| 7386 | } |
| 7387 | |
Nikolay Borisov | 9fc6f91 | 2020-06-03 08:55:32 +0300 | [diff] [blame] | 7388 | static struct extent_map *btrfs_new_extent_direct(struct btrfs_inode *inode, |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7389 | u64 start, u64 len) |
| 7390 | { |
Nikolay Borisov | 9fc6f91 | 2020-06-03 08:55:32 +0300 | [diff] [blame] | 7391 | struct btrfs_root *root = inode->root; |
| 7392 | struct btrfs_fs_info *fs_info = root->fs_info; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 7393 | struct extent_map *em; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7394 | struct btrfs_key ins; |
| 7395 | u64 alloc_hint; |
| 7396 | int ret; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7397 | |
Nikolay Borisov | 9fc6f91 | 2020-06-03 08:55:32 +0300 | [diff] [blame] | 7398 | alloc_hint = get_extent_allocation_hint(inode, start, len); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7399 | ret = btrfs_reserve_extent(root, len, len, fs_info->sectorsize, |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 7400 | 0, alloc_hint, &ins, 1, 1); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 7401 | if (ret) |
| 7402 | return ERR_PTR(ret); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7403 | |
Nikolay Borisov | 9fc6f91 | 2020-06-03 08:55:32 +0300 | [diff] [blame] | 7404 | em = btrfs_create_dio_extent(inode, start, ins.offset, start, |
Filipe Manana | 5f9a8a5 | 2016-05-12 13:53:36 +0100 | [diff] [blame] | 7405 | ins.objectid, ins.offset, ins.offset, |
Liu Bo | 6288d6e | 2017-02-21 12:12:58 -0800 | [diff] [blame] | 7406 | ins.offset, BTRFS_ORDERED_REGULAR); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7407 | btrfs_dec_block_group_reservations(fs_info, ins.objectid); |
Filipe Manana | 5f9a8a5 | 2016-05-12 13:53:36 +0100 | [diff] [blame] | 7408 | if (IS_ERR(em)) |
Nikolay Borisov | 9fc6f91 | 2020-06-03 08:55:32 +0300 | [diff] [blame] | 7409 | btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, |
| 7410 | 1); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 7411 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7412 | return em; |
| 7413 | } |
| 7414 | |
Anand Jain | f463963 | 2021-02-10 21:25:16 -0800 | [diff] [blame] | 7415 | 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] | 7416 | { |
| 7417 | struct btrfs_block_group *block_group; |
Anand Jain | f463963 | 2021-02-10 21:25:16 -0800 | [diff] [blame] | 7418 | bool readonly = false; |
Anand Jain | 05947ae | 2021-02-10 21:25:15 -0800 | [diff] [blame] | 7419 | |
| 7420 | block_group = btrfs_lookup_block_group(fs_info, bytenr); |
| 7421 | if (!block_group || block_group->ro) |
Anand Jain | f463963 | 2021-02-10 21:25:16 -0800 | [diff] [blame] | 7422 | readonly = true; |
Anand Jain | 05947ae | 2021-02-10 21:25:15 -0800 | [diff] [blame] | 7423 | if (block_group) |
| 7424 | btrfs_put_block_group(block_group); |
| 7425 | return readonly; |
| 7426 | } |
| 7427 | |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7428 | /* |
Qu Wenruo | e4ecaf9 | 2020-06-24 07:23:51 +0800 | [diff] [blame] | 7429 | * Check if we can do nocow write into the range [@offset, @offset + @len) |
| 7430 | * |
| 7431 | * @offset: File offset |
| 7432 | * @len: The length to write, will be updated to the nocow writeable |
| 7433 | * range |
| 7434 | * @orig_start: (optional) Return the original file offset of the file extent |
| 7435 | * @orig_len: (optional) Return the original on-disk length of the file extent |
| 7436 | * @ram_bytes: (optional) Return the ram_bytes of the file extent |
Boris Burkov | a84d5d4 | 2020-08-18 11:00:05 -0700 | [diff] [blame] | 7437 | * @strict: if true, omit optimizations that might force us into unnecessary |
| 7438 | * cow. e.g., don't trust generation number. |
Qu Wenruo | e4ecaf9 | 2020-06-24 07:23:51 +0800 | [diff] [blame] | 7439 | * |
Qu Wenruo | e4ecaf9 | 2020-06-24 07:23:51 +0800 | [diff] [blame] | 7440 | * Return: |
| 7441 | * >0 and update @len if we can do nocow write |
| 7442 | * 0 if we can't do nocow write |
| 7443 | * <0 if error happened |
| 7444 | * |
| 7445 | * NOTE: This only checks the file extents, caller is responsible to wait for |
| 7446 | * any ordered extents. |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7447 | */ |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 7448 | noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len, |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 7449 | u64 *orig_start, u64 *orig_block_len, |
Boris Burkov | a84d5d4 | 2020-08-18 11:00:05 -0700 | [diff] [blame] | 7450 | u64 *ram_bytes, bool strict) |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7451 | { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 7452 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7453 | struct btrfs_path *path; |
| 7454 | int ret; |
| 7455 | struct extent_buffer *leaf; |
| 7456 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Miao Xie | 7b2b708 | 2014-02-27 13:58:05 +0800 | [diff] [blame] | 7457 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7458 | struct btrfs_file_extent_item *fi; |
| 7459 | struct btrfs_key key; |
| 7460 | u64 disk_bytenr; |
| 7461 | u64 backref_offset; |
| 7462 | u64 extent_end; |
| 7463 | u64 num_bytes; |
| 7464 | int slot; |
| 7465 | int found_type; |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 7466 | bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW); |
Miao Xie | e77751a | 2013-12-27 21:11:50 +0800 | [diff] [blame] | 7467 | |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7468 | path = btrfs_alloc_path(); |
| 7469 | if (!path) |
| 7470 | return -ENOMEM; |
| 7471 | |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 7472 | ret = btrfs_lookup_file_extent(NULL, root, path, |
| 7473 | btrfs_ino(BTRFS_I(inode)), offset, 0); |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7474 | if (ret < 0) |
| 7475 | goto out; |
| 7476 | |
| 7477 | slot = path->slots[0]; |
| 7478 | if (ret == 1) { |
| 7479 | if (slot == 0) { |
| 7480 | /* can't find the item, must cow */ |
| 7481 | ret = 0; |
| 7482 | goto out; |
| 7483 | } |
| 7484 | slot--; |
| 7485 | } |
| 7486 | ret = 0; |
| 7487 | leaf = path->nodes[0]; |
| 7488 | btrfs_item_key_to_cpu(leaf, &key, slot); |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 7489 | if (key.objectid != btrfs_ino(BTRFS_I(inode)) || |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7490 | key.type != BTRFS_EXTENT_DATA_KEY) { |
| 7491 | /* not our file or wrong item type, must cow */ |
| 7492 | goto out; |
| 7493 | } |
| 7494 | |
| 7495 | if (key.offset > offset) { |
| 7496 | /* Wrong offset, must cow */ |
| 7497 | goto out; |
| 7498 | } |
| 7499 | |
| 7500 | fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item); |
| 7501 | found_type = btrfs_file_extent_type(leaf, fi); |
| 7502 | if (found_type != BTRFS_FILE_EXTENT_REG && |
| 7503 | found_type != BTRFS_FILE_EXTENT_PREALLOC) { |
| 7504 | /* not a regular extent, must cow */ |
| 7505 | goto out; |
| 7506 | } |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 7507 | |
| 7508 | if (!nocow && found_type == BTRFS_FILE_EXTENT_REG) |
| 7509 | goto out; |
| 7510 | |
Miao Xie | e77751a | 2013-12-27 21:11:50 +0800 | [diff] [blame] | 7511 | extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi); |
| 7512 | if (extent_end <= offset) |
| 7513 | goto out; |
| 7514 | |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7515 | disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 7516 | if (disk_bytenr == 0) |
| 7517 | goto out; |
| 7518 | |
| 7519 | if (btrfs_file_extent_compression(leaf, fi) || |
| 7520 | btrfs_file_extent_encryption(leaf, fi) || |
| 7521 | btrfs_file_extent_other_encoding(leaf, fi)) |
| 7522 | goto out; |
| 7523 | |
Ethan Lien | 78d4295 | 2018-05-17 14:58:29 +0800 | [diff] [blame] | 7524 | /* |
| 7525 | * Do the same check as in btrfs_cross_ref_exist but without the |
| 7526 | * unnecessary search. |
| 7527 | */ |
Boris Burkov | a84d5d4 | 2020-08-18 11:00:05 -0700 | [diff] [blame] | 7528 | if (!strict && |
| 7529 | (btrfs_file_extent_generation(leaf, fi) <= |
| 7530 | btrfs_root_last_snapshot(&root->root_item))) |
Ethan Lien | 78d4295 | 2018-05-17 14:58:29 +0800 | [diff] [blame] | 7531 | goto out; |
| 7532 | |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7533 | backref_offset = btrfs_file_extent_offset(leaf, fi); |
| 7534 | |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 7535 | if (orig_start) { |
| 7536 | *orig_start = key.offset - backref_offset; |
| 7537 | *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi); |
| 7538 | *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi); |
| 7539 | } |
Josef Bacik | eb384b5 | 2013-04-24 16:32:55 -0400 | [diff] [blame] | 7540 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 7541 | if (btrfs_extent_readonly(fs_info, disk_bytenr)) |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7542 | goto out; |
Miao Xie | 7b2b708 | 2014-02-27 13:58:05 +0800 | [diff] [blame] | 7543 | |
| 7544 | num_bytes = min(offset + *len, extent_end) - offset; |
| 7545 | if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) { |
| 7546 | u64 range_end; |
| 7547 | |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 7548 | range_end = round_up(offset + num_bytes, |
| 7549 | root->fs_info->sectorsize) - 1; |
Miao Xie | 7b2b708 | 2014-02-27 13:58:05 +0800 | [diff] [blame] | 7550 | ret = test_range_bit(io_tree, offset, range_end, |
| 7551 | EXTENT_DELALLOC, 0, NULL); |
| 7552 | if (ret) { |
| 7553 | ret = -EAGAIN; |
| 7554 | goto out; |
| 7555 | } |
| 7556 | } |
| 7557 | |
Josef Bacik | 1bda19e | 2013-10-18 12:10:36 -0400 | [diff] [blame] | 7558 | btrfs_release_path(path); |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7559 | |
| 7560 | /* |
| 7561 | * look for other files referencing this extent, if we |
| 7562 | * find any we must cow |
| 7563 | */ |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 7564 | |
Liu Bo | e4c3b2d | 2017-01-30 12:25:28 -0800 | [diff] [blame] | 7565 | ret = btrfs_cross_ref_exist(root, btrfs_ino(BTRFS_I(inode)), |
Boris Burkov | a84d5d4 | 2020-08-18 11:00:05 -0700 | [diff] [blame] | 7566 | key.offset - backref_offset, disk_bytenr, |
| 7567 | strict); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 7568 | if (ret) { |
| 7569 | ret = 0; |
| 7570 | goto out; |
| 7571 | } |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7572 | |
| 7573 | /* |
| 7574 | * adjust disk_bytenr and num_bytes to cover just the bytes |
| 7575 | * in this extent we are about to write. If there |
| 7576 | * are any csums in that range we have to cow in order |
| 7577 | * to keep the csums correct |
| 7578 | */ |
| 7579 | disk_bytenr += backref_offset; |
| 7580 | disk_bytenr += offset - key.offset; |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 7581 | if (csum_exist_in_range(fs_info, disk_bytenr, num_bytes)) |
| 7582 | goto out; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7583 | /* |
| 7584 | * all of the above have passed, it is safe to overwrite this extent |
| 7585 | * without cow |
| 7586 | */ |
Josef Bacik | eb384b5 | 2013-04-24 16:32:55 -0400 | [diff] [blame] | 7587 | *len = num_bytes; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7588 | ret = 1; |
| 7589 | out: |
| 7590 | btrfs_free_path(path); |
| 7591 | return ret; |
| 7592 | } |
| 7593 | |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7594 | static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend, |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 7595 | struct extent_state **cached_state, bool writing) |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7596 | { |
| 7597 | struct btrfs_ordered_extent *ordered; |
| 7598 | int ret = 0; |
| 7599 | |
| 7600 | while (1) { |
| 7601 | lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend, |
David Sterba | ff13db4 | 2015-12-03 14:30:40 +0100 | [diff] [blame] | 7602 | cached_state); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7603 | /* |
| 7604 | * 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] | 7605 | * 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] | 7606 | * extents in this range. |
| 7607 | */ |
Nikolay Borisov | a776c6f | 2017-02-20 13:50:49 +0200 | [diff] [blame] | 7608 | ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), lockstart, |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7609 | lockend - lockstart + 1); |
| 7610 | |
| 7611 | /* |
| 7612 | * We need to make sure there are no buffered pages in this |
| 7613 | * range either, we could have raced between the invalidate in |
| 7614 | * generic_file_direct_write and locking the extent. The |
| 7615 | * invalidate needs to happen so that reads after a write do not |
| 7616 | * get stale data. |
| 7617 | */ |
Alex Gartrell | fc4adbff | 2014-05-20 13:07:56 -0700 | [diff] [blame] | 7618 | if (!ordered && |
David Sterba | 051c98e | 2018-03-07 15:33:22 +0100 | [diff] [blame] | 7619 | (!writing || !filemap_range_has_page(inode->i_mapping, |
| 7620 | lockstart, lockend))) |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7621 | break; |
| 7622 | |
| 7623 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend, |
David Sterba | e43bbe5 | 2017-12-12 21:43:52 +0100 | [diff] [blame] | 7624 | cached_state); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7625 | |
| 7626 | if (ordered) { |
Filipe Manana | ade7702 | 2016-02-18 14:28:55 +0000 | [diff] [blame] | 7627 | /* |
| 7628 | * If we are doing a DIO read and the ordered extent we |
| 7629 | * found is for a buffered write, we can not wait for it |
| 7630 | * to complete and retry, because if we do so we can |
| 7631 | * deadlock with concurrent buffered writes on page |
| 7632 | * locks. This happens only if our DIO read covers more |
| 7633 | * than one extent map, if at this point has already |
| 7634 | * created an ordered extent for a previous extent map |
| 7635 | * and locked its range in the inode's io tree, and a |
| 7636 | * concurrent write against that previous extent map's |
| 7637 | * range and this range started (we unlock the ranges |
| 7638 | * in the io tree only when the bios complete and |
| 7639 | * buffered writes always lock pages before attempting |
| 7640 | * to lock range in the io tree). |
| 7641 | */ |
| 7642 | if (writing || |
| 7643 | test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags)) |
Nikolay Borisov | c0a4360 | 2020-09-18 12:15:53 +0300 | [diff] [blame] | 7644 | btrfs_start_ordered_extent(ordered, 1); |
Filipe Manana | ade7702 | 2016-02-18 14:28:55 +0000 | [diff] [blame] | 7645 | else |
| 7646 | ret = -ENOTBLK; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7647 | btrfs_put_ordered_extent(ordered); |
| 7648 | } else { |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7649 | /* |
Filipe Manana | b850ae1 | 2015-12-08 16:23:16 +0000 | [diff] [blame] | 7650 | * We could trigger writeback for this range (and wait |
| 7651 | * for it to complete) and then invalidate the pages for |
| 7652 | * this range (through invalidate_inode_pages2_range()), |
| 7653 | * but that can lead us to a deadlock with a concurrent |
Matthew Wilcox (Oracle) | ba206a0 | 2020-06-01 21:47:05 -0700 | [diff] [blame] | 7654 | * call to readahead (a buffered read or a defrag call |
Filipe Manana | b850ae1 | 2015-12-08 16:23:16 +0000 | [diff] [blame] | 7655 | * triggered a readahead) on a page lock due to an |
| 7656 | * ordered dio extent we created before but did not have |
| 7657 | * yet a corresponding bio submitted (whence it can not |
Matthew Wilcox (Oracle) | ba206a0 | 2020-06-01 21:47:05 -0700 | [diff] [blame] | 7658 | * complete), which makes readahead wait for that |
Filipe Manana | b850ae1 | 2015-12-08 16:23:16 +0000 | [diff] [blame] | 7659 | * ordered extent to complete while holding a lock on |
| 7660 | * that page. |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7661 | */ |
Filipe Manana | b850ae1 | 2015-12-08 16:23:16 +0000 | [diff] [blame] | 7662 | ret = -ENOTBLK; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7663 | } |
| 7664 | |
Filipe Manana | ade7702 | 2016-02-18 14:28:55 +0000 | [diff] [blame] | 7665 | if (ret) |
| 7666 | break; |
| 7667 | |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7668 | cond_resched(); |
| 7669 | } |
| 7670 | |
| 7671 | return ret; |
| 7672 | } |
| 7673 | |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 7674 | /* The callers of this must take lock_extent() */ |
Nikolay Borisov | 4b67c11 | 2020-06-03 08:55:05 +0300 | [diff] [blame] | 7675 | static struct extent_map *create_io_em(struct btrfs_inode *inode, u64 start, |
| 7676 | u64 len, u64 orig_start, u64 block_start, |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 7677 | u64 block_len, u64 orig_block_len, |
| 7678 | u64 ram_bytes, int compress_type, |
| 7679 | int type) |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7680 | { |
| 7681 | struct extent_map_tree *em_tree; |
| 7682 | struct extent_map *em; |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7683 | int ret; |
| 7684 | |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 7685 | ASSERT(type == BTRFS_ORDERED_PREALLOC || |
| 7686 | type == BTRFS_ORDERED_COMPRESSED || |
| 7687 | type == BTRFS_ORDERED_NOCOW || |
Liu Bo | 1af4a0a | 2017-02-13 15:35:09 -0800 | [diff] [blame] | 7688 | type == BTRFS_ORDERED_REGULAR); |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 7689 | |
Nikolay Borisov | 4b67c11 | 2020-06-03 08:55:05 +0300 | [diff] [blame] | 7690 | em_tree = &inode->extent_tree; |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7691 | em = alloc_extent_map(); |
| 7692 | if (!em) |
| 7693 | return ERR_PTR(-ENOMEM); |
| 7694 | |
| 7695 | em->start = start; |
| 7696 | em->orig_start = orig_start; |
| 7697 | em->len = len; |
| 7698 | em->block_len = block_len; |
| 7699 | em->block_start = block_start; |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 7700 | em->orig_block_len = orig_block_len; |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 7701 | em->ram_bytes = ram_bytes; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 7702 | em->generation = -1; |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7703 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
Liu Bo | 1af4a0a | 2017-02-13 15:35:09 -0800 | [diff] [blame] | 7704 | if (type == BTRFS_ORDERED_PREALLOC) { |
Josef Bacik | b11e234 | 2012-12-03 10:58:15 -0500 | [diff] [blame] | 7705 | set_bit(EXTENT_FLAG_FILLING, &em->flags); |
Liu Bo | 1af4a0a | 2017-02-13 15:35:09 -0800 | [diff] [blame] | 7706 | } else if (type == BTRFS_ORDERED_COMPRESSED) { |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 7707 | set_bit(EXTENT_FLAG_COMPRESSED, &em->flags); |
| 7708 | em->compress_type = compress_type; |
| 7709 | } |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7710 | |
| 7711 | do { |
Nikolay Borisov | 4b67c11 | 2020-06-03 08:55:05 +0300 | [diff] [blame] | 7712 | btrfs_drop_extent_cache(inode, em->start, |
| 7713 | em->start + em->len - 1, 0); |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7714 | write_lock(&em_tree->lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 7715 | ret = add_extent_mapping(em_tree, em, 1); |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7716 | write_unlock(&em_tree->lock); |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 7717 | /* |
| 7718 | * The caller has taken lock_extent(), who could race with us |
| 7719 | * to add em? |
| 7720 | */ |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7721 | } while (ret == -EEXIST); |
| 7722 | |
| 7723 | if (ret) { |
| 7724 | free_extent_map(em); |
| 7725 | return ERR_PTR(ret); |
| 7726 | } |
| 7727 | |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 7728 | /* 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] | 7729 | return em; |
| 7730 | } |
| 7731 | |
Nikolay Borisov | 1c8d017 | 2018-05-02 15:19:32 +0300 | [diff] [blame] | 7732 | |
Nikolay Borisov | c5794e5 | 2018-05-02 15:19:33 +0300 | [diff] [blame] | 7733 | static int btrfs_get_blocks_direct_write(struct extent_map **map, |
Nikolay Borisov | c5794e5 | 2018-05-02 15:19:33 +0300 | [diff] [blame] | 7734 | struct inode *inode, |
| 7735 | struct btrfs_dio_data *dio_data, |
| 7736 | u64 start, u64 len) |
| 7737 | { |
| 7738 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
| 7739 | struct extent_map *em = *map; |
| 7740 | int ret = 0; |
| 7741 | |
| 7742 | /* |
| 7743 | * We don't allocate a new extent in the following cases |
| 7744 | * |
| 7745 | * 1) The inode is marked as NODATACOW. In this case we'll just use the |
| 7746 | * existing extent. |
| 7747 | * 2) The extent is marked as PREALLOC. We're good to go here and can |
| 7748 | * just use the extent. |
| 7749 | * |
| 7750 | */ |
| 7751 | if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) || |
| 7752 | ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) && |
| 7753 | em->block_start != EXTENT_MAP_HOLE)) { |
| 7754 | int type; |
| 7755 | u64 block_start, orig_start, orig_block_len, ram_bytes; |
| 7756 | |
| 7757 | if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) |
| 7758 | type = BTRFS_ORDERED_PREALLOC; |
| 7759 | else |
| 7760 | type = BTRFS_ORDERED_NOCOW; |
| 7761 | len = min(len, em->len - (start - em->start)); |
| 7762 | block_start = em->block_start + (start - em->start); |
| 7763 | |
| 7764 | if (can_nocow_extent(inode, start, &len, &orig_start, |
Boris Burkov | a84d5d4 | 2020-08-18 11:00:05 -0700 | [diff] [blame] | 7765 | &orig_block_len, &ram_bytes, false) == 1 && |
Nikolay Borisov | c5794e5 | 2018-05-02 15:19:33 +0300 | [diff] [blame] | 7766 | btrfs_inc_nocow_writers(fs_info, block_start)) { |
| 7767 | struct extent_map *em2; |
| 7768 | |
Nikolay Borisov | 64f5418 | 2020-06-03 08:55:31 +0300 | [diff] [blame] | 7769 | em2 = btrfs_create_dio_extent(BTRFS_I(inode), start, len, |
Nikolay Borisov | c5794e5 | 2018-05-02 15:19:33 +0300 | [diff] [blame] | 7770 | orig_start, block_start, |
| 7771 | len, orig_block_len, |
| 7772 | ram_bytes, type); |
| 7773 | btrfs_dec_nocow_writers(fs_info, block_start); |
| 7774 | if (type == BTRFS_ORDERED_PREALLOC) { |
| 7775 | free_extent_map(em); |
| 7776 | *map = em = em2; |
| 7777 | } |
| 7778 | |
| 7779 | if (em2 && IS_ERR(em2)) { |
| 7780 | ret = PTR_ERR(em2); |
| 7781 | goto out; |
| 7782 | } |
| 7783 | /* |
| 7784 | * For inode marked NODATACOW or extent marked PREALLOC, |
| 7785 | * use the existing or preallocated extent, so does not |
| 7786 | * need to adjust btrfs_space_info's bytes_may_use. |
| 7787 | */ |
Nikolay Borisov | 9db5d51 | 2020-06-03 08:55:38 +0300 | [diff] [blame] | 7788 | btrfs_free_reserved_data_space_noquota(fs_info, len); |
Nikolay Borisov | c5794e5 | 2018-05-02 15:19:33 +0300 | [diff] [blame] | 7789 | goto skip_cow; |
| 7790 | } |
| 7791 | } |
| 7792 | |
| 7793 | /* this will cow the extent */ |
Nikolay Borisov | c5794e5 | 2018-05-02 15:19:33 +0300 | [diff] [blame] | 7794 | free_extent_map(em); |
Nikolay Borisov | 9fc6f91 | 2020-06-03 08:55:32 +0300 | [diff] [blame] | 7795 | *map = em = btrfs_new_extent_direct(BTRFS_I(inode), start, len); |
Nikolay Borisov | c5794e5 | 2018-05-02 15:19:33 +0300 | [diff] [blame] | 7796 | if (IS_ERR(em)) { |
| 7797 | ret = PTR_ERR(em); |
| 7798 | goto out; |
| 7799 | } |
| 7800 | |
| 7801 | len = min(len, em->len - (start - em->start)); |
| 7802 | |
| 7803 | skip_cow: |
Nikolay Borisov | c5794e5 | 2018-05-02 15:19:33 +0300 | [diff] [blame] | 7804 | /* |
| 7805 | * Need to update the i_size under the extent lock so buffered |
| 7806 | * readers will get the updated i_size when we unlock. |
| 7807 | */ |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 7808 | if (start + len > i_size_read(inode)) |
Nikolay Borisov | c5794e5 | 2018-05-02 15:19:33 +0300 | [diff] [blame] | 7809 | i_size_write(inode, start + len); |
| 7810 | |
Nikolay Borisov | c5794e5 | 2018-05-02 15:19:33 +0300 | [diff] [blame] | 7811 | dio_data->reserve -= len; |
Nikolay Borisov | c5794e5 | 2018-05-02 15:19:33 +0300 | [diff] [blame] | 7812 | out: |
| 7813 | return ret; |
| 7814 | } |
| 7815 | |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 7816 | static int btrfs_dio_iomap_begin(struct inode *inode, loff_t start, |
| 7817 | loff_t length, unsigned int flags, struct iomap *iomap, |
| 7818 | struct iomap *srcmap) |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7819 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7820 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7821 | struct extent_map *em; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7822 | struct extent_state *cached_state = NULL; |
chandan | 50745b0 | 2015-08-28 21:10:13 +0530 | [diff] [blame] | 7823 | struct btrfs_dio_data *dio_data = NULL; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7824 | u64 lockstart, lockend; |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 7825 | const bool write = !!(flags & IOMAP_WRITE); |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 7826 | int ret = 0; |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 7827 | u64 len = length; |
| 7828 | bool unlock_extents = false; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7829 | |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 7830 | if (!write) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7831 | len = min_t(u64, len, fs_info->sectorsize); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7832 | |
Josef Bacik | c329861 | 2012-08-03 16:49:19 -0400 | [diff] [blame] | 7833 | lockstart = start; |
| 7834 | lockend = start + len - 1; |
| 7835 | |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 7836 | /* |
| 7837 | * The generic stuff only does filemap_write_and_wait_range, which |
| 7838 | * isn't enough if we've written compressed pages to this area, so we |
| 7839 | * need to flush the dirty pages again to make absolutely sure that any |
| 7840 | * outstanding dirty pages are on disk. |
| 7841 | */ |
| 7842 | if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, |
| 7843 | &BTRFS_I(inode)->runtime_flags)) { |
| 7844 | ret = filemap_fdatawrite_range(inode->i_mapping, start, |
| 7845 | start + length - 1); |
| 7846 | if (ret) |
| 7847 | return ret; |
Josef Bacik | e1cbbfa | 2015-03-17 10:52:28 -0400 | [diff] [blame] | 7848 | } |
| 7849 | |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 7850 | dio_data = kzalloc(sizeof(*dio_data), GFP_NOFS); |
| 7851 | if (!dio_data) |
| 7852 | return -ENOMEM; |
| 7853 | |
| 7854 | dio_data->length = length; |
| 7855 | if (write) { |
| 7856 | dio_data->reserve = round_up(length, fs_info->sectorsize); |
| 7857 | ret = btrfs_delalloc_reserve_space(BTRFS_I(inode), |
| 7858 | &dio_data->data_reserved, |
| 7859 | start, dio_data->reserve); |
| 7860 | if (ret) { |
| 7861 | extent_changeset_free(dio_data->data_reserved); |
| 7862 | kfree(dio_data); |
| 7863 | return ret; |
| 7864 | } |
| 7865 | } |
| 7866 | iomap->private = dio_data; |
| 7867 | |
| 7868 | |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7869 | /* |
| 7870 | * If this errors out it's because we couldn't invalidate pagecache for |
| 7871 | * this range and we need to fallback to buffered. |
| 7872 | */ |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 7873 | if (lock_extent_direct(inode, lockstart, lockend, &cached_state, write)) { |
Filipe Manana | 9c9464c | 2015-11-04 09:52:04 +0000 | [diff] [blame] | 7874 | ret = -ENOTBLK; |
| 7875 | goto err; |
| 7876 | } |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7877 | |
Omar Sandoval | 39b07b5 | 2019-12-02 17:34:23 -0800 | [diff] [blame] | 7878 | em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7879 | if (IS_ERR(em)) { |
| 7880 | ret = PTR_ERR(em); |
| 7881 | goto unlock_err; |
| 7882 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7883 | |
| 7884 | /* |
| 7885 | * Ok for INLINE and COMPRESSED extents we need to fallback on buffered |
| 7886 | * io. INLINE is special, and we could probably kludge it in here, but |
| 7887 | * it's still buffered so for safety lets just fall back to the generic |
| 7888 | * buffered path. |
| 7889 | * |
| 7890 | * For COMPRESSED we _have_ to read the entire extent in so we can |
| 7891 | * decompress it, so there will be buffering required no matter what we |
| 7892 | * do, so go ahead and fallback to buffered. |
| 7893 | * |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 7894 | * 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] | 7895 | * to buffered IO. Don't blame me, this is the price we pay for using |
| 7896 | * the generic code. |
| 7897 | */ |
| 7898 | if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) || |
| 7899 | em->block_start == EXTENT_MAP_INLINE) { |
| 7900 | free_extent_map(em); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7901 | ret = -ENOTBLK; |
| 7902 | goto unlock_err; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7903 | } |
| 7904 | |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 7905 | len = min(len, em->len - (start - em->start)); |
| 7906 | if (write) { |
| 7907 | ret = btrfs_get_blocks_direct_write(&em, inode, dio_data, |
| 7908 | start, len); |
Nikolay Borisov | c5794e5 | 2018-05-02 15:19:33 +0300 | [diff] [blame] | 7909 | if (ret < 0) |
| 7910 | goto unlock_err; |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 7911 | unlock_extents = true; |
| 7912 | /* Recalc len in case the new em is smaller than requested */ |
| 7913 | len = min(len, em->len - (start - em->start)); |
Nikolay Borisov | c5794e5 | 2018-05-02 15:19:33 +0300 | [diff] [blame] | 7914 | } else { |
Nikolay Borisov | 1c8d017 | 2018-05-02 15:19:32 +0300 | [diff] [blame] | 7915 | /* |
| 7916 | * We need to unlock only the end area that we aren't using. |
| 7917 | * The rest is going to be unlocked by the endio routine. |
| 7918 | */ |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 7919 | lockstart = start + len; |
| 7920 | if (lockstart < lockend) |
| 7921 | unlock_extents = true; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7922 | } |
| 7923 | |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 7924 | if (unlock_extents) |
| 7925 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, |
| 7926 | lockstart, lockend, &cached_state); |
| 7927 | else |
| 7928 | free_extent_state(cached_state); |
| 7929 | |
| 7930 | /* |
| 7931 | * Translate extent map information to iomap. |
| 7932 | * We trim the extents (and move the addr) even though iomap code does |
| 7933 | * that, since we have locked only the parts we are performing I/O in. |
| 7934 | */ |
| 7935 | if ((em->block_start == EXTENT_MAP_HOLE) || |
| 7936 | (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) && !write)) { |
| 7937 | iomap->addr = IOMAP_NULL_ADDR; |
| 7938 | iomap->type = IOMAP_HOLE; |
| 7939 | } else { |
| 7940 | iomap->addr = em->block_start + (start - em->start); |
| 7941 | iomap->type = IOMAP_MAPPED; |
| 7942 | } |
| 7943 | iomap->offset = start; |
| 7944 | iomap->bdev = fs_info->fs_devices->latest_bdev; |
| 7945 | iomap->length = len; |
| 7946 | |
Johannes Thumshirn | e380adf | 2021-05-19 00:40:27 +0900 | [diff] [blame] | 7947 | if (write && btrfs_use_zone_append(BTRFS_I(inode), em->block_start)) |
Naohiro Aota | 544d24f | 2021-02-04 19:22:06 +0900 | [diff] [blame] | 7948 | iomap->flags |= IOMAP_F_ZONE_APPEND; |
| 7949 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7950 | free_extent_map(em); |
| 7951 | |
| 7952 | return 0; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7953 | |
| 7954 | unlock_err: |
Omar Sandoval | e182163 | 2019-08-15 14:04:04 -0700 | [diff] [blame] | 7955 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend, |
| 7956 | &cached_state); |
Filipe Manana | 9c9464c | 2015-11-04 09:52:04 +0000 | [diff] [blame] | 7957 | err: |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 7958 | if (dio_data) { |
| 7959 | btrfs_delalloc_release_space(BTRFS_I(inode), |
| 7960 | dio_data->data_reserved, start, |
| 7961 | dio_data->reserve, true); |
| 7962 | btrfs_delalloc_release_extents(BTRFS_I(inode), dio_data->reserve); |
| 7963 | extent_changeset_free(dio_data->data_reserved); |
| 7964 | kfree(dio_data); |
| 7965 | } |
| 7966 | return ret; |
| 7967 | } |
| 7968 | |
| 7969 | static int btrfs_dio_iomap_end(struct inode *inode, loff_t pos, loff_t length, |
| 7970 | ssize_t written, unsigned int flags, struct iomap *iomap) |
| 7971 | { |
| 7972 | int ret = 0; |
| 7973 | struct btrfs_dio_data *dio_data = iomap->private; |
| 7974 | size_t submitted = dio_data->submitted; |
| 7975 | const bool write = !!(flags & IOMAP_WRITE); |
| 7976 | |
| 7977 | if (!write && (iomap->type == IOMAP_HOLE)) { |
| 7978 | /* If reading from a hole, unlock and return */ |
| 7979 | unlock_extent(&BTRFS_I(inode)->io_tree, pos, pos + length - 1); |
| 7980 | goto out; |
| 7981 | } |
| 7982 | |
| 7983 | if (submitted < length) { |
| 7984 | pos += submitted; |
| 7985 | length -= submitted; |
| 7986 | if (write) |
| 7987 | __endio_write_update_ordered(BTRFS_I(inode), pos, |
| 7988 | length, false); |
| 7989 | else |
| 7990 | unlock_extent(&BTRFS_I(inode)->io_tree, pos, |
| 7991 | pos + length - 1); |
| 7992 | ret = -ENOTBLK; |
| 7993 | } |
| 7994 | |
| 7995 | if (write) { |
| 7996 | if (dio_data->reserve) |
| 7997 | btrfs_delalloc_release_space(BTRFS_I(inode), |
| 7998 | dio_data->data_reserved, pos, |
| 7999 | dio_data->reserve, true); |
| 8000 | btrfs_delalloc_release_extents(BTRFS_I(inode), dio_data->length); |
| 8001 | extent_changeset_free(dio_data->data_reserved); |
| 8002 | } |
| 8003 | out: |
| 8004 | kfree(dio_data); |
| 8005 | iomap->private = NULL; |
| 8006 | |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 8007 | return ret; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8008 | } |
| 8009 | |
Omar Sandoval | 769b4f2 | 2020-04-16 14:46:22 -0700 | [diff] [blame] | 8010 | static void btrfs_dio_private_put(struct btrfs_dio_private *dip) |
| 8011 | { |
| 8012 | /* |
| 8013 | * This implies a barrier so that stores to dio_bio->bi_status before |
| 8014 | * this and loads of dio_bio->bi_status after this are fully ordered. |
| 8015 | */ |
| 8016 | if (!refcount_dec_and_test(&dip->refs)) |
| 8017 | return; |
| 8018 | |
Naohiro Aota | cfe9444 | 2021-02-04 19:21:59 +0900 | [diff] [blame] | 8019 | if (btrfs_op(dip->dio_bio) == BTRFS_MAP_WRITE) { |
Nikolay Borisov | b672b5c | 2020-06-03 08:55:24 +0300 | [diff] [blame] | 8020 | __endio_write_update_ordered(BTRFS_I(dip->inode), |
| 8021 | dip->logical_offset, |
Omar Sandoval | 769b4f2 | 2020-04-16 14:46:22 -0700 | [diff] [blame] | 8022 | dip->bytes, |
| 8023 | !dip->dio_bio->bi_status); |
| 8024 | } else { |
| 8025 | unlock_extent(&BTRFS_I(dip->inode)->io_tree, |
| 8026 | dip->logical_offset, |
| 8027 | dip->logical_offset + dip->bytes - 1); |
| 8028 | } |
| 8029 | |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 8030 | bio_endio(dip->dio_bio); |
Omar Sandoval | 769b4f2 | 2020-04-16 14:46:22 -0700 | [diff] [blame] | 8031 | kfree(dip); |
| 8032 | } |
| 8033 | |
Omar Sandoval | 77d5d68 | 2020-04-16 14:46:25 -0700 | [diff] [blame] | 8034 | static blk_status_t submit_dio_repair_bio(struct inode *inode, struct bio *bio, |
| 8035 | int mirror_num, |
| 8036 | unsigned long bio_flags) |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8037 | { |
Omar Sandoval | 77d5d68 | 2020-04-16 14:46:25 -0700 | [diff] [blame] | 8038 | struct btrfs_dio_private *dip = bio->bi_private; |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 8039 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Omar Sandoval | 58efbc9 | 2017-08-22 23:45:59 -0700 | [diff] [blame] | 8040 | blk_status_t ret; |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8041 | |
Mike Christie | 37226b2 | 2016-06-05 14:31:52 -0500 | [diff] [blame] | 8042 | BUG_ON(bio_op(bio) == REQ_OP_WRITE); |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8043 | |
Omar Sandoval | 5c047a6 | 2020-04-16 14:46:24 -0700 | [diff] [blame] | 8044 | 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] | 8045 | if (ret) |
Nikolay Borisov | ea057f6 | 2017-12-13 10:25:38 +0200 | [diff] [blame] | 8046 | return ret; |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8047 | |
Omar Sandoval | fd9d667 | 2020-04-16 14:46:23 -0700 | [diff] [blame] | 8048 | refcount_inc(&dip->refs); |
Chris Mason | 08635ba | 2019-07-10 12:28:14 -0700 | [diff] [blame] | 8049 | ret = btrfs_map_bio(fs_info, bio, mirror_num); |
Omar Sandoval | 77d5d68 | 2020-04-16 14:46:25 -0700 | [diff] [blame] | 8050 | if (ret) |
Omar Sandoval | fd9d667 | 2020-04-16 14:46:23 -0700 | [diff] [blame] | 8051 | refcount_dec(&dip->refs); |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8052 | return ret; |
| 8053 | } |
| 8054 | |
Omar Sandoval | 769b4f2 | 2020-04-16 14:46:22 -0700 | [diff] [blame] | 8055 | static blk_status_t btrfs_check_read_dio_bio(struct inode *inode, |
| 8056 | struct btrfs_io_bio *io_bio, |
Omar Sandoval | fd9d667 | 2020-04-16 14:46:23 -0700 | [diff] [blame] | 8057 | const bool uptodate) |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8058 | { |
Omar Sandoval | fd9d667 | 2020-04-16 14:46:23 -0700 | [diff] [blame] | 8059 | struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info; |
| 8060 | const u32 sectorsize = fs_info->sectorsize; |
Josef Bacik | 7870d08 | 2017-05-05 11:57:15 -0400 | [diff] [blame] | 8061 | struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree; |
Omar Sandoval | fd9d667 | 2020-04-16 14:46:23 -0700 | [diff] [blame] | 8062 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
| 8063 | const bool csum = !(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM); |
Liu Bo | 17347ce | 2017-05-15 15:33:27 -0700 | [diff] [blame] | 8064 | struct bio_vec bvec; |
| 8065 | struct bvec_iter iter; |
Omar Sandoval | fd9d667 | 2020-04-16 14:46:23 -0700 | [diff] [blame] | 8066 | u64 start = io_bio->logical; |
Qu Wenruo | 7ffd27e | 2020-12-02 14:47:58 +0800 | [diff] [blame] | 8067 | u32 bio_offset = 0; |
Omar Sandoval | 58efbc9 | 2017-08-22 23:45:59 -0700 | [diff] [blame] | 8068 | blk_status_t err = BLK_STS_OK; |
Miao Xie | dc380ae | 2014-09-12 18:43:55 +0800 | [diff] [blame] | 8069 | |
Omar Sandoval | fd9d667 | 2020-04-16 14:46:23 -0700 | [diff] [blame] | 8070 | __bio_for_each_segment(bvec, &io_bio->bio, iter, io_bio->iter) { |
| 8071 | unsigned int i, nr_sectors, pgoff; |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 8072 | |
Liu Bo | 17347ce | 2017-05-15 15:33:27 -0700 | [diff] [blame] | 8073 | nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec.bv_len); |
| 8074 | pgoff = bvec.bv_offset; |
Omar Sandoval | fd9d667 | 2020-04-16 14:46:23 -0700 | [diff] [blame] | 8075 | for (i = 0; i < nr_sectors; i++) { |
Liu Bo | 97bf5a5 | 2017-04-07 13:11:10 -0700 | [diff] [blame] | 8076 | ASSERT(pgoff < PAGE_SIZE); |
Omar Sandoval | fd9d667 | 2020-04-16 14:46:23 -0700 | [diff] [blame] | 8077 | if (uptodate && |
Qu Wenruo | 7ffd27e | 2020-12-02 14:47:58 +0800 | [diff] [blame] | 8078 | (!csum || !check_data_csum(inode, io_bio, |
Omar Sandoval | c1d6abd | 2021-03-16 12:43:00 -0700 | [diff] [blame] | 8079 | bio_offset, bvec.bv_page, |
| 8080 | pgoff, start))) { |
Omar Sandoval | fd9d667 | 2020-04-16 14:46:23 -0700 | [diff] [blame] | 8081 | clean_io_failure(fs_info, failure_tree, io_tree, |
| 8082 | start, bvec.bv_page, |
| 8083 | btrfs_ino(BTRFS_I(inode)), |
| 8084 | pgoff); |
| 8085 | } else { |
Qu Wenruo | 150e4b0 | 2021-05-03 10:08:55 +0800 | [diff] [blame] | 8086 | int ret; |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8087 | |
Qu Wenruo | 7ffd27e | 2020-12-02 14:47:58 +0800 | [diff] [blame] | 8088 | ASSERT((start - io_bio->logical) < UINT_MAX); |
Qu Wenruo | 150e4b0 | 2021-05-03 10:08:55 +0800 | [diff] [blame] | 8089 | ret = btrfs_repair_one_sector(inode, |
| 8090 | &io_bio->bio, |
| 8091 | start - io_bio->logical, |
| 8092 | bvec.bv_page, pgoff, |
| 8093 | start, io_bio->mirror_num, |
| 8094 | submit_dio_repair_bio); |
| 8095 | if (ret) |
| 8096 | err = errno_to_blk_status(ret); |
Omar Sandoval | fd9d667 | 2020-04-16 14:46:23 -0700 | [diff] [blame] | 8097 | } |
| 8098 | start += sectorsize; |
Qu Wenruo | 7ffd27e | 2020-12-02 14:47:58 +0800 | [diff] [blame] | 8099 | ASSERT(bio_offset + sectorsize > bio_offset); |
| 8100 | bio_offset += sectorsize; |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 8101 | pgoff += sectorsize; |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 8102 | } |
Kent Overstreet | 2c30c71 | 2013-11-07 12:20:26 -0800 | [diff] [blame] | 8103 | } |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 8104 | return err; |
| 8105 | } |
| 8106 | |
Nikolay Borisov | b672b5c | 2020-06-03 08:55:24 +0300 | [diff] [blame] | 8107 | static void __endio_write_update_ordered(struct btrfs_inode *inode, |
Qu Wenruo | 52427260 | 2017-03-08 10:25:52 +0800 | [diff] [blame] | 8108 | const u64 offset, const u64 bytes, |
| 8109 | const bool uptodate) |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8110 | { |
Qu Wenruo | e65f152 | 2021-04-01 15:15:06 +0800 | [diff] [blame] | 8111 | btrfs_mark_ordered_io_finished(inode, NULL, offset, bytes, |
| 8112 | finish_ordered_fn, uptodate); |
Filipe Manana | 1454377 | 2015-11-24 16:23:54 +0000 | [diff] [blame] | 8113 | } |
| 8114 | |
Qu Wenruo | 8896a08 | 2020-10-21 14:24:53 +0800 | [diff] [blame] | 8115 | 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] | 8116 | struct bio *bio, |
| 8117 | u64 dio_file_offset) |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 8118 | { |
Qu Wenruo | 1941b64 | 2020-12-02 14:47:57 +0800 | [diff] [blame] | 8119 | 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] | 8120 | } |
| 8121 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 8122 | static void btrfs_end_dio_bio(struct bio *bio) |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8123 | { |
| 8124 | struct btrfs_dio_private *dip = bio->bi_private; |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 8125 | blk_status_t err = bio->bi_status; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8126 | |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8127 | if (err) |
| 8128 | btrfs_warn(BTRFS_I(dip->inode)->root->fs_info, |
Mike Christie | 6296b96 | 2016-06-05 14:32:21 -0500 | [diff] [blame] | 8129 | "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] | 8130 | btrfs_ino(BTRFS_I(dip->inode)), bio_op(bio), |
David Sterba | 1201b58 | 2020-11-26 15:41:27 +0100 | [diff] [blame] | 8131 | bio->bi_opf, bio->bi_iter.bi_sector, |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8132 | bio->bi_iter.bi_size, err); |
| 8133 | |
Omar Sandoval | 769b4f2 | 2020-04-16 14:46:22 -0700 | [diff] [blame] | 8134 | if (bio_op(bio) == REQ_OP_READ) { |
| 8135 | err = btrfs_check_read_dio_bio(dip->inode, btrfs_io_bio(bio), |
Omar Sandoval | fd9d667 | 2020-04-16 14:46:23 -0700 | [diff] [blame] | 8136 | !err); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8137 | } |
| 8138 | |
Omar Sandoval | 769b4f2 | 2020-04-16 14:46:22 -0700 | [diff] [blame] | 8139 | if (err) |
| 8140 | dip->dio_bio->bi_status = err; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8141 | |
Naohiro Aota | 544d24f | 2021-02-04 19:22:06 +0900 | [diff] [blame] | 8142 | btrfs_record_physical_zoned(dip->inode, dip->logical_offset, bio); |
| 8143 | |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8144 | bio_put(bio); |
Omar Sandoval | 769b4f2 | 2020-04-16 14:46:22 -0700 | [diff] [blame] | 8145 | btrfs_dio_private_put(dip); |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 8146 | } |
| 8147 | |
David Sterba | d0ee393 | 2018-03-08 14:35:48 +0100 | [diff] [blame] | 8148 | static inline blk_status_t btrfs_submit_dio_bio(struct bio *bio, |
| 8149 | struct inode *inode, u64 file_offset, int async_submit) |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8150 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8151 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 8152 | struct btrfs_dio_private *dip = bio->bi_private; |
Naohiro Aota | cfe9444 | 2021-02-04 19:21:59 +0900 | [diff] [blame] | 8153 | bool write = btrfs_op(bio) == BTRFS_MAP_WRITE; |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 8154 | blk_status_t ret; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8155 | |
Liu Bo | 4c274bc | 2017-11-01 17:19:27 -0600 | [diff] [blame] | 8156 | /* Check btrfs_submit_bio_hook() for rules about async submit. */ |
Josef Bacik | b812ce2 | 2012-11-16 13:56:32 -0500 | [diff] [blame] | 8157 | if (async_submit) |
| 8158 | async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers); |
| 8159 | |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 8160 | if (!write) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8161 | 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] | 8162 | if (ret) |
| 8163 | goto err; |
| 8164 | } |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8165 | |
Nikolay Borisov | e6961ca | 2017-08-03 15:44:58 +0300 | [diff] [blame] | 8166 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM) |
Josef Bacik | 1ae3993 | 2011-04-06 14:41:34 -0400 | [diff] [blame] | 8167 | goto map; |
| 8168 | |
| 8169 | if (write && async_submit) { |
Qu Wenruo | 1941b64 | 2020-12-02 14:47:57 +0800 | [diff] [blame] | 8170 | ret = btrfs_wq_submit_bio(inode, bio, 0, 0, file_offset, |
David Sterba | e288c08 | 2018-07-18 17:36:24 +0200 | [diff] [blame] | 8171 | btrfs_submit_bio_start_direct_io); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8172 | goto err; |
Josef Bacik | 1ae3993 | 2011-04-06 14:41:34 -0400 | [diff] [blame] | 8173 | } else if (write) { |
| 8174 | /* |
| 8175 | * If we aren't doing async submit, calculate the csum of the |
| 8176 | * bio now. |
| 8177 | */ |
Nikolay Borisov | bd242a0 | 2020-06-03 08:55:07 +0300 | [diff] [blame] | 8178 | ret = btrfs_csum_one_bio(BTRFS_I(inode), bio, file_offset, 1); |
Josef Bacik | 1ae3993 | 2011-04-06 14:41:34 -0400 | [diff] [blame] | 8179 | if (ret) |
| 8180 | goto err; |
Miao Xie | 23ea8e5 | 2014-09-12 18:43:54 +0800 | [diff] [blame] | 8181 | } else { |
Omar Sandoval | 8587957 | 2020-04-16 14:46:21 -0700 | [diff] [blame] | 8182 | u64 csum_offset; |
| 8183 | |
| 8184 | csum_offset = file_offset - dip->logical_offset; |
David Sterba | 265fdfa | 2020-07-01 21:19:09 +0200 | [diff] [blame] | 8185 | csum_offset >>= fs_info->sectorsize_bits; |
David Sterba | 55fc29b | 2020-06-30 02:01:31 +0200 | [diff] [blame] | 8186 | csum_offset *= fs_info->csum_size; |
Omar Sandoval | 8587957 | 2020-04-16 14:46:21 -0700 | [diff] [blame] | 8187 | btrfs_io_bio(bio)->csum = dip->csums + csum_offset; |
Tsutomu Itoh | c2db107 | 2011-03-01 06:48:31 +0000 | [diff] [blame] | 8188 | } |
Josef Bacik | 1ae3993 | 2011-04-06 14:41:34 -0400 | [diff] [blame] | 8189 | map: |
Chris Mason | 08635ba | 2019-07-10 12:28:14 -0700 | [diff] [blame] | 8190 | ret = btrfs_map_bio(fs_info, bio, 0); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8191 | err: |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8192 | return ret; |
| 8193 | } |
| 8194 | |
Omar Sandoval | c36cac2 | 2020-04-16 14:46:13 -0700 | [diff] [blame] | 8195 | /* |
| 8196 | * If this succeeds, the btrfs_dio_private is responsible for cleaning up locked |
| 8197 | * or ordered extents whether or not we submit any bios. |
| 8198 | */ |
| 8199 | static struct btrfs_dio_private *btrfs_create_dio_private(struct bio *dio_bio, |
| 8200 | struct inode *inode, |
| 8201 | loff_t file_offset) |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8202 | { |
Naohiro Aota | cfe9444 | 2021-02-04 19:21:59 +0900 | [diff] [blame] | 8203 | const bool write = (btrfs_op(dio_bio) == BTRFS_MAP_WRITE); |
Omar Sandoval | 8587957 | 2020-04-16 14:46:21 -0700 | [diff] [blame] | 8204 | const bool csum = !(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM); |
| 8205 | size_t dip_size; |
Omar Sandoval | c36cac2 | 2020-04-16 14:46:13 -0700 | [diff] [blame] | 8206 | struct btrfs_dio_private *dip; |
Omar Sandoval | c36cac2 | 2020-04-16 14:46:13 -0700 | [diff] [blame] | 8207 | |
Omar Sandoval | 8587957 | 2020-04-16 14:46:21 -0700 | [diff] [blame] | 8208 | dip_size = sizeof(*dip); |
| 8209 | if (!write && csum) { |
| 8210 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Omar Sandoval | 8587957 | 2020-04-16 14:46:21 -0700 | [diff] [blame] | 8211 | size_t nblocks; |
| 8212 | |
David Sterba | 265fdfa | 2020-07-01 21:19:09 +0200 | [diff] [blame] | 8213 | nblocks = dio_bio->bi_iter.bi_size >> fs_info->sectorsize_bits; |
David Sterba | 223486c | 2020-07-02 11:27:30 +0200 | [diff] [blame] | 8214 | dip_size += fs_info->csum_size * nblocks; |
Omar Sandoval | 8587957 | 2020-04-16 14:46:21 -0700 | [diff] [blame] | 8215 | } |
| 8216 | |
| 8217 | dip = kzalloc(dip_size, GFP_NOFS); |
Omar Sandoval | c36cac2 | 2020-04-16 14:46:13 -0700 | [diff] [blame] | 8218 | if (!dip) |
| 8219 | return NULL; |
| 8220 | |
Omar Sandoval | c36cac2 | 2020-04-16 14:46:13 -0700 | [diff] [blame] | 8221 | dip->inode = inode; |
| 8222 | dip->logical_offset = file_offset; |
| 8223 | dip->bytes = dio_bio->bi_iter.bi_size; |
David Sterba | 1201b58 | 2020-11-26 15:41:27 +0100 | [diff] [blame] | 8224 | dip->disk_bytenr = dio_bio->bi_iter.bi_sector << 9; |
Omar Sandoval | c36cac2 | 2020-04-16 14:46:13 -0700 | [diff] [blame] | 8225 | dip->dio_bio = dio_bio; |
Omar Sandoval | e3b318d | 2020-04-16 14:46:20 -0700 | [diff] [blame] | 8226 | refcount_set(&dip->refs, 1); |
Omar Sandoval | c36cac2 | 2020-04-16 14:46:13 -0700 | [diff] [blame] | 8227 | return dip; |
| 8228 | } |
| 8229 | |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 8230 | static blk_qc_t btrfs_submit_direct(struct inode *inode, struct iomap *iomap, |
| 8231 | struct bio *dio_bio, loff_t file_offset) |
Omar Sandoval | c36cac2 | 2020-04-16 14:46:13 -0700 | [diff] [blame] | 8232 | { |
Naohiro Aota | cfe9444 | 2021-02-04 19:21:59 +0900 | [diff] [blame] | 8233 | const bool write = (btrfs_op(dio_bio) == BTRFS_MAP_WRITE); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8234 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Omar Sandoval | 769b4f2 | 2020-04-16 14:46:22 -0700 | [diff] [blame] | 8235 | const bool raid56 = (btrfs_data_alloc_profile(fs_info) & |
| 8236 | BTRFS_BLOCK_GROUP_RAID56_MASK); |
Omar Sandoval | c36cac2 | 2020-04-16 14:46:13 -0700 | [diff] [blame] | 8237 | struct btrfs_dio_private *dip; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8238 | struct bio *bio; |
Omar Sandoval | c36cac2 | 2020-04-16 14:46:13 -0700 | [diff] [blame] | 8239 | u64 start_sector; |
Josef Bacik | 1ae3993 | 2011-04-06 14:41:34 -0400 | [diff] [blame] | 8240 | int async_submit = 0; |
Liu Bo | 725130b | 2017-05-16 09:51:39 -0700 | [diff] [blame] | 8241 | u64 submit_len; |
| 8242 | int clone_offset = 0; |
| 8243 | int clone_len; |
Michal Rostecki | 4203431 | 2021-01-27 14:57:27 +0100 | [diff] [blame] | 8244 | u64 logical; |
Chandan Rajendra | 5f4dc8f | 2016-01-21 15:56:00 +0530 | [diff] [blame] | 8245 | int ret; |
Omar Sandoval | 58efbc9 | 2017-08-22 23:45:59 -0700 | [diff] [blame] | 8246 | blk_status_t status; |
Nikolay Borisov | 89b798a | 2019-06-03 12:05:05 +0300 | [diff] [blame] | 8247 | struct btrfs_io_geometry geom; |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 8248 | struct btrfs_dio_data *dio_data = iomap->private; |
Michal Rostecki | 4203431 | 2021-01-27 14:57:27 +0100 | [diff] [blame] | 8249 | struct extent_map *em = NULL; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8250 | |
Omar Sandoval | c36cac2 | 2020-04-16 14:46:13 -0700 | [diff] [blame] | 8251 | dip = btrfs_create_dio_private(dio_bio, inode, file_offset); |
| 8252 | if (!dip) { |
| 8253 | if (!write) { |
| 8254 | unlock_extent(&BTRFS_I(inode)->io_tree, file_offset, |
| 8255 | file_offset + dio_bio->bi_iter.bi_size - 1); |
| 8256 | } |
| 8257 | dio_bio->bi_status = BLK_STS_RESOURCE; |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 8258 | bio_endio(dio_bio); |
| 8259 | return BLK_QC_T_NONE; |
Josef Bacik | 02f57c7 | 2011-04-06 14:25:44 -0400 | [diff] [blame] | 8260 | } |
| 8261 | |
Josef Bacik | 334c16d | 2020-10-16 11:29:14 -0400 | [diff] [blame] | 8262 | if (!write) { |
Omar Sandoval | 8587957 | 2020-04-16 14:46:21 -0700 | [diff] [blame] | 8263 | /* |
| 8264 | * Load the csums up front to reduce csum tree searches and |
| 8265 | * contention when submitting bios. |
Josef Bacik | 334c16d | 2020-10-16 11:29:14 -0400 | [diff] [blame] | 8266 | * |
| 8267 | * If we have csums disabled this will do nothing. |
Omar Sandoval | 8587957 | 2020-04-16 14:46:21 -0700 | [diff] [blame] | 8268 | */ |
Qu Wenruo | 6275193 | 2020-12-02 14:48:06 +0800 | [diff] [blame] | 8269 | status = btrfs_lookup_bio_sums(inode, dio_bio, dip->csums); |
Omar Sandoval | 8587957 | 2020-04-16 14:46:21 -0700 | [diff] [blame] | 8270 | if (status != BLK_STS_OK) |
| 8271 | goto out_err; |
| 8272 | } |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 8273 | |
Omar Sandoval | 769b4f2 | 2020-04-16 14:46:22 -0700 | [diff] [blame] | 8274 | start_sector = dio_bio->bi_iter.bi_sector; |
| 8275 | submit_len = dio_bio->bi_iter.bi_size; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8276 | |
Arnd Bergmann | 3c91ee6 | 2017-05-18 15:33:29 +0200 | [diff] [blame] | 8277 | do { |
Michal Rostecki | 4203431 | 2021-01-27 14:57:27 +0100 | [diff] [blame] | 8278 | logical = start_sector << 9; |
| 8279 | em = btrfs_get_chunk_map(fs_info, logical, submit_len); |
| 8280 | if (IS_ERR(em)) { |
| 8281 | status = errno_to_blk_status(PTR_ERR(em)); |
| 8282 | em = NULL; |
| 8283 | goto out_err_em; |
| 8284 | } |
| 8285 | ret = btrfs_get_io_geometry(fs_info, em, btrfs_op(dio_bio), |
Qu Wenruo | 43c0d1a | 2021-04-13 17:58:48 +0800 | [diff] [blame] | 8286 | logical, &geom); |
Omar Sandoval | 769b4f2 | 2020-04-16 14:46:22 -0700 | [diff] [blame] | 8287 | if (ret) { |
| 8288 | status = errno_to_blk_status(ret); |
Michal Rostecki | 4203431 | 2021-01-27 14:57:27 +0100 | [diff] [blame] | 8289 | goto out_err_em; |
Omar Sandoval | 769b4f2 | 2020-04-16 14:46:22 -0700 | [diff] [blame] | 8290 | } |
| 8291 | ASSERT(geom.len <= INT_MAX); |
| 8292 | |
Nikolay Borisov | 89b798a | 2019-06-03 12:05:05 +0300 | [diff] [blame] | 8293 | clone_len = min_t(int, submit_len, geom.len); |
Josef Bacik | 02f57c7 | 2011-04-06 14:25:44 -0400 | [diff] [blame] | 8294 | |
Liu Bo | 725130b | 2017-05-16 09:51:39 -0700 | [diff] [blame] | 8295 | /* |
| 8296 | * This will never fail as it's passing GPF_NOFS and |
| 8297 | * the allocation is backed by btrfs_bioset. |
| 8298 | */ |
Omar Sandoval | 769b4f2 | 2020-04-16 14:46:22 -0700 | [diff] [blame] | 8299 | bio = btrfs_bio_clone_partial(dio_bio, clone_offset, clone_len); |
Liu Bo | 725130b | 2017-05-16 09:51:39 -0700 | [diff] [blame] | 8300 | bio->bi_private = dip; |
| 8301 | bio->bi_end_io = btrfs_end_dio_bio; |
| 8302 | btrfs_io_bio(bio)->logical = file_offset; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8303 | |
Naohiro Aota | 544d24f | 2021-02-04 19:22:06 +0900 | [diff] [blame] | 8304 | if (bio_op(bio) == REQ_OP_ZONE_APPEND) { |
| 8305 | status = extract_ordered_extent(BTRFS_I(inode), bio, |
| 8306 | file_offset); |
| 8307 | if (status) { |
| 8308 | bio_put(bio); |
| 8309 | goto out_err; |
| 8310 | } |
| 8311 | } |
| 8312 | |
Liu Bo | 725130b | 2017-05-16 09:51:39 -0700 | [diff] [blame] | 8313 | ASSERT(submit_len >= clone_len); |
| 8314 | submit_len -= clone_len; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8315 | |
Liu Bo | 725130b | 2017-05-16 09:51:39 -0700 | [diff] [blame] | 8316 | /* |
| 8317 | * Increase the count before we submit the bio so we know |
| 8318 | * the end IO handler won't happen before we increase the |
| 8319 | * count. Otherwise, the dip might get freed before we're |
| 8320 | * done setting it up. |
Omar Sandoval | 769b4f2 | 2020-04-16 14:46:22 -0700 | [diff] [blame] | 8321 | * |
| 8322 | * We transfer the initial reference to the last bio, so we |
| 8323 | * don't need to increment the reference count for the last one. |
Liu Bo | 725130b | 2017-05-16 09:51:39 -0700 | [diff] [blame] | 8324 | */ |
Omar Sandoval | 769b4f2 | 2020-04-16 14:46:22 -0700 | [diff] [blame] | 8325 | if (submit_len > 0) { |
| 8326 | refcount_inc(&dip->refs); |
| 8327 | /* |
| 8328 | * If we are submitting more than one bio, submit them |
| 8329 | * all asynchronously. The exception is RAID 5 or 6, as |
| 8330 | * asynchronous checksums make it difficult to collect |
| 8331 | * full stripe writes. |
| 8332 | */ |
| 8333 | if (!raid56) |
| 8334 | async_submit = 1; |
| 8335 | } |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8336 | |
David Sterba | d0ee393 | 2018-03-08 14:35:48 +0100 | [diff] [blame] | 8337 | status = btrfs_submit_dio_bio(bio, inode, file_offset, |
Omar Sandoval | 58efbc9 | 2017-08-22 23:45:59 -0700 | [diff] [blame] | 8338 | async_submit); |
| 8339 | if (status) { |
Liu Bo | 725130b | 2017-05-16 09:51:39 -0700 | [diff] [blame] | 8340 | bio_put(bio); |
Omar Sandoval | 769b4f2 | 2020-04-16 14:46:22 -0700 | [diff] [blame] | 8341 | if (submit_len > 0) |
| 8342 | refcount_dec(&dip->refs); |
Michal Rostecki | 4203431 | 2021-01-27 14:57:27 +0100 | [diff] [blame] | 8343 | goto out_err_em; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8344 | } |
Liu Bo | 725130b | 2017-05-16 09:51:39 -0700 | [diff] [blame] | 8345 | |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 8346 | dio_data->submitted += clone_len; |
Liu Bo | 725130b | 2017-05-16 09:51:39 -0700 | [diff] [blame] | 8347 | clone_offset += clone_len; |
| 8348 | start_sector += clone_len >> 9; |
| 8349 | file_offset += clone_len; |
Michal Rostecki | 4203431 | 2021-01-27 14:57:27 +0100 | [diff] [blame] | 8350 | |
| 8351 | free_extent_map(em); |
Arnd Bergmann | 3c91ee6 | 2017-05-18 15:33:29 +0200 | [diff] [blame] | 8352 | } while (submit_len > 0); |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 8353 | return BLK_QC_T_NONE; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8354 | |
Michal Rostecki | 4203431 | 2021-01-27 14:57:27 +0100 | [diff] [blame] | 8355 | out_err_em: |
| 8356 | free_extent_map(em); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8357 | out_err: |
Omar Sandoval | 769b4f2 | 2020-04-16 14:46:22 -0700 | [diff] [blame] | 8358 | dip->dio_bio->bi_status = status; |
| 8359 | btrfs_dio_private_put(dip); |
Michal Rostecki | 4203431 | 2021-01-27 14:57:27 +0100 | [diff] [blame] | 8360 | |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 8361 | return BLK_QC_T_NONE; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8362 | } |
| 8363 | |
Goldwyn Rodrigues | 4e4cabe | 2020-09-24 11:39:12 -0500 | [diff] [blame] | 8364 | const struct iomap_ops btrfs_dio_iomap_ops = { |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 8365 | .iomap_begin = btrfs_dio_iomap_begin, |
| 8366 | .iomap_end = btrfs_dio_iomap_end, |
| 8367 | }; |
| 8368 | |
Goldwyn Rodrigues | 4e4cabe | 2020-09-24 11:39:12 -0500 | [diff] [blame] | 8369 | const struct iomap_dio_ops btrfs_dio_ops = { |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 8370 | .submit_io = btrfs_submit_direct, |
| 8371 | }; |
| 8372 | |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 8373 | static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, |
David Sterba | bab16e2 | 2020-06-23 20:56:12 +0200 | [diff] [blame] | 8374 | u64 start, u64 len) |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 8375 | { |
Tsutomu Itoh | 05dadc0 | 2012-11-29 05:08:26 +0000 | [diff] [blame] | 8376 | int ret; |
| 8377 | |
Christoph Hellwig | 45dd052 | 2020-05-23 09:30:14 +0200 | [diff] [blame] | 8378 | ret = fiemap_prep(inode, fieinfo, start, &len, 0); |
Tsutomu Itoh | 05dadc0 | 2012-11-29 05:08:26 +0000 | [diff] [blame] | 8379 | if (ret) |
| 8380 | return ret; |
| 8381 | |
Nikolay Borisov | facee0a | 2020-08-31 14:42:49 +0300 | [diff] [blame] | 8382 | return extent_fiemap(BTRFS_I(inode), fieinfo, start, len); |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 8383 | } |
| 8384 | |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8385 | int btrfs_readpage(struct file *file, struct page *page) |
| 8386 | { |
Nikolay Borisov | 0f20881 | 2020-09-14 14:39:16 +0300 | [diff] [blame] | 8387 | struct btrfs_inode *inode = BTRFS_I(page->mapping->host); |
| 8388 | u64 start = page_offset(page); |
| 8389 | u64 end = start + PAGE_SIZE - 1; |
Qu Wenruo | 390ed29 | 2021-04-14 16:42:15 +0800 | [diff] [blame] | 8390 | struct btrfs_bio_ctrl bio_ctrl = { 0 }; |
Nikolay Borisov | c1be9c1 | 2020-09-14 12:37:08 +0300 | [diff] [blame] | 8391 | int ret; |
| 8392 | |
Nikolay Borisov | 0f20881 | 2020-09-14 14:39:16 +0300 | [diff] [blame] | 8393 | btrfs_lock_and_flush_ordered_range(inode, start, end, NULL); |
| 8394 | |
Qu Wenruo | 390ed29 | 2021-04-14 16:42:15 +0800 | [diff] [blame] | 8395 | ret = btrfs_do_readpage(page, NULL, &bio_ctrl, 0, NULL); |
| 8396 | if (bio_ctrl.bio) |
| 8397 | ret = submit_one_bio(bio_ctrl.bio, 0, bio_ctrl.bio_flags); |
Nikolay Borisov | c1be9c1 | 2020-09-14 12:37:08 +0300 | [diff] [blame] | 8398 | return ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8399 | } |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 8400 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8401 | static int btrfs_writepage(struct page *page, struct writeback_control *wbc) |
| 8402 | { |
Josef Bacik | be7bd73 | 2015-10-22 15:05:09 -0400 | [diff] [blame] | 8403 | struct inode *inode = page->mapping->host; |
| 8404 | int ret; |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8405 | |
| 8406 | if (current->flags & PF_MEMALLOC) { |
| 8407 | redirty_page_for_writepage(wbc, page); |
| 8408 | unlock_page(page); |
| 8409 | return 0; |
| 8410 | } |
Josef Bacik | be7bd73 | 2015-10-22 15:05:09 -0400 | [diff] [blame] | 8411 | |
| 8412 | /* |
| 8413 | * If we are under memory pressure we will call this directly from the |
| 8414 | * VM, we need to make sure we have the inode referenced for the ordered |
| 8415 | * extent. If not just return like we didn't do anything. |
| 8416 | */ |
| 8417 | if (!igrab(inode)) { |
| 8418 | redirty_page_for_writepage(wbc, page); |
| 8419 | return AOP_WRITEPAGE_ACTIVATE; |
| 8420 | } |
Nikolay Borisov | 0a9b0e5 | 2017-12-08 15:55:59 +0200 | [diff] [blame] | 8421 | ret = extent_write_full_page(page, wbc); |
Josef Bacik | be7bd73 | 2015-10-22 15:05:09 -0400 | [diff] [blame] | 8422 | btrfs_add_delayed_iput(inode); |
| 8423 | return ret; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8424 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8425 | |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 8426 | static int btrfs_writepages(struct address_space *mapping, |
| 8427 | struct writeback_control *wbc) |
Chris Mason | b293f02e | 2007-11-01 19:45:34 -0400 | [diff] [blame] | 8428 | { |
Nikolay Borisov | 8ae225a | 2018-04-19 10:46:38 +0300 | [diff] [blame] | 8429 | return extent_writepages(mapping, wbc); |
Chris Mason | b293f02e | 2007-11-01 19:45:34 -0400 | [diff] [blame] | 8430 | } |
| 8431 | |
Matthew Wilcox (Oracle) | ba206a0 | 2020-06-01 21:47:05 -0700 | [diff] [blame] | 8432 | static void btrfs_readahead(struct readahead_control *rac) |
Chris Mason | 3ab2fb5 | 2007-11-08 10:59:22 -0500 | [diff] [blame] | 8433 | { |
Matthew Wilcox (Oracle) | ba206a0 | 2020-06-01 21:47:05 -0700 | [diff] [blame] | 8434 | extent_readahead(rac); |
Chris Mason | 3ab2fb5 | 2007-11-08 10:59:22 -0500 | [diff] [blame] | 8435 | } |
Nikolay Borisov | 2a3ff0a | 2018-04-19 10:46:36 +0300 | [diff] [blame] | 8436 | |
Qu Wenruo | 7c11d0a | 2021-07-26 14:35:03 +0800 | [diff] [blame] | 8437 | /* |
| 8438 | * For releasepage() and invalidatepage() we have a race window where |
| 8439 | * end_page_writeback() is called but the subpage spinlock is not yet released. |
| 8440 | * If we continue to release/invalidate the page, we could cause use-after-free |
| 8441 | * for subpage spinlock. So this function is to spin and wait for subpage |
| 8442 | * spinlock. |
| 8443 | */ |
| 8444 | static void wait_subpage_spinlock(struct page *page) |
| 8445 | { |
| 8446 | struct btrfs_fs_info *fs_info = btrfs_sb(page->mapping->host->i_sb); |
| 8447 | struct btrfs_subpage *subpage; |
| 8448 | |
| 8449 | if (fs_info->sectorsize == PAGE_SIZE) |
| 8450 | return; |
| 8451 | |
| 8452 | ASSERT(PagePrivate(page) && page->private); |
| 8453 | subpage = (struct btrfs_subpage *)page->private; |
| 8454 | |
| 8455 | /* |
| 8456 | * This may look insane as we just acquire the spinlock and release it, |
| 8457 | * without doing anything. But we just want to make sure no one is |
| 8458 | * still holding the subpage spinlock. |
| 8459 | * And since the page is not dirty nor writeback, and we have page |
| 8460 | * locked, the only possible way to hold a spinlock is from the endio |
| 8461 | * function to clear page writeback. |
| 8462 | * |
| 8463 | * Here we just acquire the spinlock so that all existing callers |
| 8464 | * should exit and we're safe to release/invalidate the page. |
| 8465 | */ |
| 8466 | spin_lock_irq(&subpage->lock); |
| 8467 | spin_unlock_irq(&subpage->lock); |
| 8468 | } |
| 8469 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8470 | static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8471 | { |
Nikolay Borisov | 477a30b | 2018-04-19 10:46:34 +0300 | [diff] [blame] | 8472 | int ret = try_release_extent_mapping(page, gfp_flags); |
Qu Wenruo | 7c11d0a | 2021-07-26 14:35:03 +0800 | [diff] [blame] | 8473 | |
| 8474 | if (ret == 1) { |
| 8475 | wait_subpage_spinlock(page); |
Qu Wenruo | 32443de | 2021-01-26 16:34:00 +0800 | [diff] [blame] | 8476 | clear_page_extent_mapped(page); |
Qu Wenruo | 7c11d0a | 2021-07-26 14:35:03 +0800 | [diff] [blame] | 8477 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8478 | return ret; |
| 8479 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8480 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8481 | static int btrfs_releasepage(struct page *page, gfp_t gfp_flags) |
| 8482 | { |
Chris Mason | 98509cf | 2008-09-11 15:51:43 -0400 | [diff] [blame] | 8483 | if (PageWriteback(page) || PageDirty(page)) |
| 8484 | return 0; |
Michal Hocko | 3ba7ab2 | 2017-01-09 15:39:02 +0100 | [diff] [blame] | 8485 | return __btrfs_releasepage(page, gfp_flags); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8486 | } |
| 8487 | |
Roman Gushchin | f8e6608 | 2020-03-04 16:57:35 -0800 | [diff] [blame] | 8488 | #ifdef CONFIG_MIGRATION |
| 8489 | static int btrfs_migratepage(struct address_space *mapping, |
| 8490 | struct page *newpage, struct page *page, |
| 8491 | enum migrate_mode mode) |
| 8492 | { |
| 8493 | int ret; |
| 8494 | |
| 8495 | ret = migrate_page_move_mapping(mapping, newpage, page, 0); |
| 8496 | if (ret != MIGRATEPAGE_SUCCESS) |
| 8497 | return ret; |
| 8498 | |
Guoqing Jiang | d1b89bc | 2020-06-01 21:47:45 -0700 | [diff] [blame] | 8499 | if (page_has_private(page)) |
| 8500 | attach_page_private(newpage, detach_page_private(page)); |
Roman Gushchin | f8e6608 | 2020-03-04 16:57:35 -0800 | [diff] [blame] | 8501 | |
Qu Wenruo | f57ad93 | 2021-04-07 19:22:13 +0800 | [diff] [blame] | 8502 | if (PageOrdered(page)) { |
| 8503 | ClearPageOrdered(page); |
| 8504 | SetPageOrdered(newpage); |
Roman Gushchin | f8e6608 | 2020-03-04 16:57:35 -0800 | [diff] [blame] | 8505 | } |
| 8506 | |
| 8507 | if (mode != MIGRATE_SYNC_NO_COPY) |
| 8508 | migrate_page_copy(newpage, page); |
| 8509 | else |
| 8510 | migrate_page_states(newpage, page); |
| 8511 | return MIGRATEPAGE_SUCCESS; |
| 8512 | } |
| 8513 | #endif |
| 8514 | |
Lukas Czerner | d47992f | 2013-05-21 23:17:23 -0400 | [diff] [blame] | 8515 | static void btrfs_invalidatepage(struct page *page, unsigned int offset, |
| 8516 | unsigned int length) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8517 | { |
Nikolay Borisov | 53ac7ea | 2020-08-31 14:42:43 +0300 | [diff] [blame] | 8518 | struct btrfs_inode *inode = BTRFS_I(page->mapping->host); |
Qu Wenruo | b945a46 | 2021-05-31 16:50:46 +0800 | [diff] [blame] | 8519 | struct btrfs_fs_info *fs_info = inode->root->fs_info; |
Nikolay Borisov | 53ac7ea | 2020-08-31 14:42:43 +0300 | [diff] [blame] | 8520 | struct extent_io_tree *tree = &inode->io_tree; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 8521 | struct extent_state *cached_state = NULL; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8522 | u64 page_start = page_offset(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 8523 | u64 page_end = page_start + PAGE_SIZE - 1; |
Qu Wenruo | 3b83584 | 2021-04-06 19:54:53 +0800 | [diff] [blame] | 8524 | u64 cur; |
Nikolay Borisov | 53ac7ea | 2020-08-31 14:42:43 +0300 | [diff] [blame] | 8525 | int inode_evicting = inode->vfs_inode.i_state & I_FREEING; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8526 | |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 8527 | /* |
Qu Wenruo | 266a258 | 2021-04-06 08:27:18 +0800 | [diff] [blame] | 8528 | * We have page locked so no new ordered extent can be created on this |
| 8529 | * page, nor bio can be submitted for this page. |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 8530 | * |
Qu Wenruo | 266a258 | 2021-04-06 08:27:18 +0800 | [diff] [blame] | 8531 | * But already submitted bio can still be finished on this page. |
Qu Wenruo | f57ad93 | 2021-04-07 19:22:13 +0800 | [diff] [blame] | 8532 | * Furthermore, endio function won't skip page which has Ordered |
| 8533 | * (Private2) already cleared, so it's possible for endio and |
| 8534 | * invalidatepage to do the same ordered extent accounting twice |
| 8535 | * on one page. |
Qu Wenruo | 266a258 | 2021-04-06 08:27:18 +0800 | [diff] [blame] | 8536 | * |
| 8537 | * So here we wait for any submitted bios to finish, so that we won't |
| 8538 | * do double ordered extent accounting on the same page. |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 8539 | */ |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8540 | wait_on_page_writeback(page); |
Qu Wenruo | 7c11d0a | 2021-07-26 14:35:03 +0800 | [diff] [blame] | 8541 | wait_subpage_spinlock(page); |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 8542 | |
Qu Wenruo | bcd7745 | 2021-05-31 16:50:55 +0800 | [diff] [blame] | 8543 | /* |
| 8544 | * For subpage case, we have call sites like |
| 8545 | * btrfs_punch_hole_lock_range() which passes range not aligned to |
| 8546 | * sectorsize. |
| 8547 | * If the range doesn't cover the full page, we don't need to and |
| 8548 | * shouldn't clear page extent mapped, as page->private can still |
| 8549 | * record subpage dirty bits for other part of the range. |
| 8550 | * |
| 8551 | * For cases that can invalidate the full even the range doesn't |
| 8552 | * cover the full page, like invalidating the last page, we're |
| 8553 | * still safe to wait for ordered extent to finish. |
| 8554 | */ |
| 8555 | if (!(offset == 0 && length == PAGE_SIZE)) { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8556 | btrfs_releasepage(page, GFP_NOFS); |
| 8557 | return; |
| 8558 | } |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 8559 | |
| 8560 | if (!inode_evicting) |
David Sterba | ff13db4 | 2015-12-03 14:30:40 +0100 | [diff] [blame] | 8561 | lock_extent_bits(tree, page_start, page_end, &cached_state); |
Qu Wenruo | 951c80f | 2021-01-27 14:38:48 +0800 | [diff] [blame] | 8562 | |
Qu Wenruo | 3b83584 | 2021-04-06 19:54:53 +0800 | [diff] [blame] | 8563 | cur = page_start; |
| 8564 | while (cur < page_end) { |
| 8565 | struct btrfs_ordered_extent *ordered; |
| 8566 | bool delete_states; |
| 8567 | u64 range_end; |
Qu Wenruo | b945a46 | 2021-05-31 16:50:46 +0800 | [diff] [blame] | 8568 | u32 range_len; |
Qu Wenruo | 3b83584 | 2021-04-06 19:54:53 +0800 | [diff] [blame] | 8569 | |
| 8570 | ordered = btrfs_lookup_first_ordered_range(inode, cur, |
| 8571 | page_end + 1 - cur); |
| 8572 | if (!ordered) { |
| 8573 | range_end = page_end; |
| 8574 | /* |
| 8575 | * No ordered extent covering this range, we are safe |
| 8576 | * to delete all extent states in the range. |
| 8577 | */ |
| 8578 | delete_states = true; |
| 8579 | goto next; |
| 8580 | } |
| 8581 | if (ordered->file_offset > cur) { |
| 8582 | /* |
| 8583 | * There is a range between [cur, oe->file_offset) not |
| 8584 | * covered by any ordered extent. |
| 8585 | * We are safe to delete all extent states, and handle |
| 8586 | * the ordered extent in the next iteration. |
| 8587 | */ |
| 8588 | range_end = ordered->file_offset - 1; |
| 8589 | delete_states = true; |
| 8590 | goto next; |
| 8591 | } |
| 8592 | |
| 8593 | range_end = min(ordered->file_offset + ordered->num_bytes - 1, |
| 8594 | page_end); |
Qu Wenruo | b945a46 | 2021-05-31 16:50:46 +0800 | [diff] [blame] | 8595 | ASSERT(range_end + 1 - cur < U32_MAX); |
| 8596 | range_len = range_end + 1 - cur; |
| 8597 | if (!btrfs_page_test_ordered(fs_info, page, cur, range_len)) { |
Qu Wenruo | 3b83584 | 2021-04-06 19:54:53 +0800 | [diff] [blame] | 8598 | /* |
Qu Wenruo | f57ad93 | 2021-04-07 19:22:13 +0800 | [diff] [blame] | 8599 | * If Ordered (Private2) is cleared, it means endio has |
| 8600 | * already been executed for the range. |
Qu Wenruo | 3b83584 | 2021-04-06 19:54:53 +0800 | [diff] [blame] | 8601 | * We can't delete the extent states as |
| 8602 | * btrfs_finish_ordered_io() may still use some of them. |
| 8603 | */ |
| 8604 | delete_states = false; |
| 8605 | goto next; |
| 8606 | } |
Qu Wenruo | b945a46 | 2021-05-31 16:50:46 +0800 | [diff] [blame] | 8607 | btrfs_page_clear_ordered(fs_info, page, cur, range_len); |
Qu Wenruo | 3b83584 | 2021-04-06 19:54:53 +0800 | [diff] [blame] | 8608 | |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 8609 | /* |
Filipe Manana | 2766ff6 | 2020-11-04 11:07:34 +0000 | [diff] [blame] | 8610 | * IO on this page will never be started, so we need to account |
| 8611 | * for any ordered extents now. Don't clear EXTENT_DELALLOC_NEW |
| 8612 | * here, must leave that up for the ordered extent completion. |
Qu Wenruo | 3b83584 | 2021-04-06 19:54:53 +0800 | [diff] [blame] | 8613 | * |
| 8614 | * This will also unlock the range for incoming |
| 8615 | * btrfs_finish_ordered_io(). |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 8616 | */ |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 8617 | if (!inode_evicting) |
Qu Wenruo | 3b83584 | 2021-04-06 19:54:53 +0800 | [diff] [blame] | 8618 | clear_extent_bit(tree, cur, range_end, |
Filipe Manana | 2766ff6 | 2020-11-04 11:07:34 +0000 | [diff] [blame] | 8619 | EXTENT_DELALLOC | |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 8620 | EXTENT_LOCKED | EXTENT_DO_ACCOUNTING | |
David Sterba | ae0f162 | 2017-10-31 16:37:52 +0100 | [diff] [blame] | 8621 | EXTENT_DEFRAG, 1, 0, &cached_state); |
Qu Wenruo | 3b83584 | 2021-04-06 19:54:53 +0800 | [diff] [blame] | 8622 | |
| 8623 | spin_lock_irq(&inode->ordered_tree.lock); |
| 8624 | set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags); |
| 8625 | ordered->truncated_len = min(ordered->truncated_len, |
| 8626 | cur - ordered->file_offset); |
| 8627 | spin_unlock_irq(&inode->ordered_tree.lock); |
| 8628 | |
| 8629 | if (btrfs_dec_test_ordered_pending(inode, &ordered, |
David Sterba | f41b6ba | 2021-07-26 14:15:10 +0200 | [diff] [blame] | 8630 | cur, range_end + 1 - cur)) { |
Qu Wenruo | 3b83584 | 2021-04-06 19:54:53 +0800 | [diff] [blame] | 8631 | btrfs_finish_ordered_io(ordered); |
| 8632 | /* |
| 8633 | * The ordered extent has finished, now we're again |
| 8634 | * safe to delete all extent states of the range. |
| 8635 | */ |
| 8636 | delete_states = true; |
| 8637 | } else { |
| 8638 | /* |
| 8639 | * btrfs_finish_ordered_io() will get executed by endio |
| 8640 | * of other pages, thus we can't delete extent states |
| 8641 | * anymore |
| 8642 | */ |
| 8643 | delete_states = false; |
| 8644 | } |
| 8645 | next: |
| 8646 | if (ordered) |
| 8647 | btrfs_put_ordered_extent(ordered); |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 8648 | /* |
Qu Wenruo | 3b83584 | 2021-04-06 19:54:53 +0800 | [diff] [blame] | 8649 | * Qgroup reserved space handler |
| 8650 | * Sector(s) here will be either: |
Qu Wenruo | 266a258 | 2021-04-06 08:27:18 +0800 | [diff] [blame] | 8651 | * |
Qu Wenruo | 3b83584 | 2021-04-06 19:54:53 +0800 | [diff] [blame] | 8652 | * 1) Already written to disk or bio already finished |
| 8653 | * Then its QGROUP_RESERVED bit in io_tree is already cleared. |
| 8654 | * Qgroup will be handled by its qgroup_record then. |
| 8655 | * btrfs_qgroup_free_data() call will do nothing here. |
| 8656 | * |
| 8657 | * 2) Not written to disk yet |
| 8658 | * Then btrfs_qgroup_free_data() call will clear the |
| 8659 | * QGROUP_RESERVED bit of its io_tree, and free the qgroup |
| 8660 | * reserved data space. |
| 8661 | * Since the IO will never happen for this page. |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 8662 | */ |
Qu Wenruo | 3b83584 | 2021-04-06 19:54:53 +0800 | [diff] [blame] | 8663 | 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] | 8664 | if (!inode_evicting) { |
Qu Wenruo | 3b83584 | 2021-04-06 19:54:53 +0800 | [diff] [blame] | 8665 | clear_extent_bit(tree, cur, range_end, EXTENT_LOCKED | |
Filipe Manana | 2766ff6 | 2020-11-04 11:07:34 +0000 | [diff] [blame] | 8666 | EXTENT_DELALLOC | EXTENT_UPTODATE | |
| 8667 | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, |
Qu Wenruo | 3b83584 | 2021-04-06 19:54:53 +0800 | [diff] [blame] | 8668 | delete_states, &cached_state); |
| 8669 | } |
| 8670 | cur = range_end + 1; |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 8671 | } |
Qu Wenruo | 3b83584 | 2021-04-06 19:54:53 +0800 | [diff] [blame] | 8672 | /* |
| 8673 | * We have iterated through all ordered extents of the page, the page |
Qu Wenruo | f57ad93 | 2021-04-07 19:22:13 +0800 | [diff] [blame] | 8674 | * should not have Ordered (Private2) anymore, or the above iteration |
| 8675 | * did something wrong. |
Qu Wenruo | 3b83584 | 2021-04-06 19:54:53 +0800 | [diff] [blame] | 8676 | */ |
Qu Wenruo | f57ad93 | 2021-04-07 19:22:13 +0800 | [diff] [blame] | 8677 | ASSERT(!PageOrdered(page)); |
Qu Wenruo | 3b83584 | 2021-04-06 19:54:53 +0800 | [diff] [blame] | 8678 | if (!inode_evicting) |
| 8679 | __btrfs_releasepage(page, GFP_NOFS); |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 8680 | ClearPageChecked(page); |
Qu Wenruo | 32443de | 2021-01-26 16:34:00 +0800 | [diff] [blame] | 8681 | clear_page_extent_mapped(page); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8682 | } |
| 8683 | |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8684 | /* |
| 8685 | * btrfs_page_mkwrite() is not allowed to change the file size as it gets |
| 8686 | * called from a page fault handler when a page is first dirtied. Hence we must |
| 8687 | * be careful to check for EOF conditions here. We set the page up correctly |
| 8688 | * for a written page which means we get ENOSPC checking when writing into |
| 8689 | * holes and correct delalloc and unwritten extent mapping on filesystems that |
| 8690 | * support these features. |
| 8691 | * |
| 8692 | * We are not allowed to take the i_mutex here so we have to play games to |
| 8693 | * 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] | 8694 | * truncate_setsize() writes the inode size before removing pages, once we have |
| 8695 | * 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] | 8696 | * beyond EOF, then the page is guaranteed safe against truncation until we |
| 8697 | * unlock the page. |
| 8698 | */ |
Souptick Joarder | a528a24 | 2018-06-06 19:54:44 +0530 | [diff] [blame] | 8699 | vm_fault_t btrfs_page_mkwrite(struct vm_fault *vmf) |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8700 | { |
Nick Piggin | c2ec175 | 2009-03-31 15:23:21 -0700 | [diff] [blame] | 8701 | struct page *page = vmf->page; |
Dave Jiang | 11bac80 | 2017-02-24 14:56:41 -0800 | [diff] [blame] | 8702 | struct inode *inode = file_inode(vmf->vma->vm_file); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8703 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8704 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
| 8705 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 8706 | struct extent_state *cached_state = NULL; |
Qu Wenruo | 364ecf3 | 2017-02-27 15:10:38 +0800 | [diff] [blame] | 8707 | struct extent_changeset *data_reserved = NULL; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8708 | unsigned long zero_start; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8709 | loff_t size; |
Souptick Joarder | a528a24 | 2018-06-06 19:54:44 +0530 | [diff] [blame] | 8710 | vm_fault_t ret; |
| 8711 | int ret2; |
Chris Mason | 9998eb7 | 2012-01-25 13:47:40 -0500 | [diff] [blame] | 8712 | int reserved = 0; |
Chandan Rajendra | d0b7da88 | 2016-01-21 15:55:57 +0530 | [diff] [blame] | 8713 | u64 reserved_space; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8714 | u64 page_start; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8715 | u64 page_end; |
Chandan Rajendra | d0b7da88 | 2016-01-21 15:55:57 +0530 | [diff] [blame] | 8716 | u64 end; |
| 8717 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 8718 | reserved_space = PAGE_SIZE; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8719 | |
Jan Kara | b2b5ef5 | 2012-06-12 16:20:45 +0200 | [diff] [blame] | 8720 | sb_start_pagefault(inode->i_sb); |
Qu Wenruo | df48063 | 2015-09-08 17:25:54 +0800 | [diff] [blame] | 8721 | page_start = page_offset(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 8722 | page_end = page_start + PAGE_SIZE - 1; |
Chandan Rajendra | d0b7da88 | 2016-01-21 15:55:57 +0530 | [diff] [blame] | 8723 | end = page_end; |
Qu Wenruo | df48063 | 2015-09-08 17:25:54 +0800 | [diff] [blame] | 8724 | |
Chandan Rajendra | d0b7da88 | 2016-01-21 15:55:57 +0530 | [diff] [blame] | 8725 | /* |
| 8726 | * Reserving delalloc space after obtaining the page lock can lead to |
| 8727 | * deadlock. For example, if a dirty page is locked by this function |
| 8728 | * and the call to btrfs_delalloc_reserve_space() ends up triggering |
| 8729 | * dirty page write out, then the btrfs_writepage() function could |
| 8730 | * end up waiting indefinitely to get a lock on the page currently |
| 8731 | * being processed by btrfs_page_mkwrite() function. |
| 8732 | */ |
Nikolay Borisov | e5b7231e | 2020-06-03 08:55:42 +0300 | [diff] [blame] | 8733 | ret2 = btrfs_delalloc_reserve_space(BTRFS_I(inode), &data_reserved, |
| 8734 | page_start, reserved_space); |
Souptick Joarder | a528a24 | 2018-06-06 19:54:44 +0530 | [diff] [blame] | 8735 | if (!ret2) { |
| 8736 | ret2 = file_update_time(vmf->vma->vm_file); |
Chris Mason | 9998eb7 | 2012-01-25 13:47:40 -0500 | [diff] [blame] | 8737 | reserved = 1; |
| 8738 | } |
Souptick Joarder | a528a24 | 2018-06-06 19:54:44 +0530 | [diff] [blame] | 8739 | if (ret2) { |
| 8740 | ret = vmf_error(ret2); |
Chris Mason | 9998eb7 | 2012-01-25 13:47:40 -0500 | [diff] [blame] | 8741 | if (reserved) |
| 8742 | goto out; |
| 8743 | goto out_noreserve; |
Nick Piggin | 56a76f8 | 2009-03-31 15:23:23 -0700 | [diff] [blame] | 8744 | } |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 8745 | |
Nick Piggin | 56a76f8 | 2009-03-31 15:23:23 -0700 | [diff] [blame] | 8746 | ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */ |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8747 | again: |
Josef Bacik | 8318ba7 | 2021-02-10 17:14:33 -0500 | [diff] [blame] | 8748 | down_read(&BTRFS_I(inode)->i_mmap_lock); |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8749 | lock_page(page); |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8750 | size = i_size_read(inode); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8751 | |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8752 | if ((page->mapping != inode->i_mapping) || |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8753 | (page_start >= size)) { |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8754 | /* page got truncated out from underneath us */ |
| 8755 | goto out_unlock; |
| 8756 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8757 | wait_on_page_writeback(page); |
| 8758 | |
David Sterba | ff13db4 | 2015-12-03 14:30:40 +0100 | [diff] [blame] | 8759 | lock_extent_bits(io_tree, page_start, page_end, &cached_state); |
Qu Wenruo | 32443de | 2021-01-26 16:34:00 +0800 | [diff] [blame] | 8760 | ret2 = set_page_extent_mapped(page); |
| 8761 | if (ret2 < 0) { |
| 8762 | ret = vmf_error(ret2); |
| 8763 | unlock_extent_cached(io_tree, page_start, page_end, &cached_state); |
| 8764 | goto out_unlock; |
| 8765 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8766 | |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 8767 | /* |
| 8768 | * we can't set the delalloc bits if there are pending ordered |
| 8769 | * extents. Drop our locks and wait for them to finish |
| 8770 | */ |
Nikolay Borisov | a776c6f | 2017-02-20 13:50:49 +0200 | [diff] [blame] | 8771 | ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start, |
| 8772 | PAGE_SIZE); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8773 | if (ordered) { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 8774 | unlock_extent_cached(io_tree, page_start, page_end, |
David Sterba | e43bbe5 | 2017-12-12 21:43:52 +0100 | [diff] [blame] | 8775 | &cached_state); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8776 | unlock_page(page); |
Josef Bacik | 8318ba7 | 2021-02-10 17:14:33 -0500 | [diff] [blame] | 8777 | up_read(&BTRFS_I(inode)->i_mmap_lock); |
Nikolay Borisov | c0a4360 | 2020-09-18 12:15:53 +0300 | [diff] [blame] | 8778 | btrfs_start_ordered_extent(ordered, 1); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8779 | btrfs_put_ordered_extent(ordered); |
| 8780 | goto again; |
| 8781 | } |
| 8782 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 8783 | if (page->index == ((size - 1) >> PAGE_SHIFT)) { |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 8784 | reserved_space = round_up(size - page_start, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8785 | fs_info->sectorsize); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 8786 | if (reserved_space < PAGE_SIZE) { |
Chandan Rajendra | d0b7da88 | 2016-01-21 15:55:57 +0530 | [diff] [blame] | 8787 | end = page_start + reserved_space - 1; |
Nikolay Borisov | 86d5292 | 2020-06-03 08:55:40 +0300 | [diff] [blame] | 8788 | btrfs_delalloc_release_space(BTRFS_I(inode), |
| 8789 | data_reserved, page_start, |
| 8790 | PAGE_SIZE - reserved_space, true); |
Chandan Rajendra | d0b7da88 | 2016-01-21 15:55:57 +0530 | [diff] [blame] | 8791 | } |
| 8792 | } |
| 8793 | |
Josef Bacik | fbf1908 | 2009-10-01 17:10:23 -0400 | [diff] [blame] | 8794 | /* |
Liu Bo | 5416034 | 2016-12-13 12:15:19 -0800 | [diff] [blame] | 8795 | * page_mkwrite gets called when the page is firstly dirtied after it's |
| 8796 | * faulted in, but write(2) could also dirty a page and set delalloc |
| 8797 | * bits, thus in this case for space account reason, we still need to |
| 8798 | * clear any delalloc bits within this page range since we have to |
| 8799 | * reserve data&meta space before lock_page() (see above comments). |
Josef Bacik | fbf1908 | 2009-10-01 17:10:23 -0400 | [diff] [blame] | 8800 | */ |
Chandan Rajendra | d0b7da88 | 2016-01-21 15:55:57 +0530 | [diff] [blame] | 8801 | clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, end, |
Omar Sandoval | e182163 | 2019-08-15 14:04:04 -0700 | [diff] [blame] | 8802 | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING | |
| 8803 | EXTENT_DEFRAG, 0, 0, &cached_state); |
Josef Bacik | fbf1908 | 2009-10-01 17:10:23 -0400 | [diff] [blame] | 8804 | |
Nikolay Borisov | c2566f2 | 2020-06-03 08:55:35 +0300 | [diff] [blame] | 8805 | ret2 = btrfs_set_extent_delalloc(BTRFS_I(inode), page_start, end, 0, |
Nikolay Borisov | 330a582 | 2019-07-17 16:18:17 +0300 | [diff] [blame] | 8806 | &cached_state); |
Souptick Joarder | a528a24 | 2018-06-06 19:54:44 +0530 | [diff] [blame] | 8807 | if (ret2) { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 8808 | unlock_extent_cached(io_tree, page_start, page_end, |
David Sterba | e43bbe5 | 2017-12-12 21:43:52 +0100 | [diff] [blame] | 8809 | &cached_state); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 8810 | ret = VM_FAULT_SIGBUS; |
| 8811 | goto out_unlock; |
| 8812 | } |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8813 | |
| 8814 | /* page is wholly or partially inside EOF */ |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 8815 | if (page_start + PAGE_SIZE > size) |
Johannes Thumshirn | 7073017 | 2018-12-05 15:23:03 +0100 | [diff] [blame] | 8816 | zero_start = offset_in_page(size); |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8817 | else |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 8818 | zero_start = PAGE_SIZE; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8819 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 8820 | if (zero_start != PAGE_SIZE) { |
Ira Weiny | d048b9c | 2021-05-04 18:40:07 -0700 | [diff] [blame] | 8821 | memzero_page(page, zero_start, PAGE_SIZE - zero_start); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8822 | flush_dcache_page(page); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8823 | } |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 8824 | ClearPageChecked(page); |
Qu Wenruo | 2d8ec40 | 2021-05-31 16:50:52 +0800 | [diff] [blame] | 8825 | btrfs_page_set_dirty(fs_info, page, page_start, end + 1 - page_start); |
| 8826 | 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] | 8827 | |
Filipe Manana | bc0939f | 2021-02-23 12:08:48 +0000 | [diff] [blame] | 8828 | btrfs_set_inode_last_sub_trans(BTRFS_I(inode)); |
Chris Mason | 257c62e | 2009-10-13 13:21:08 -0400 | [diff] [blame] | 8829 | |
David Sterba | e43bbe5 | 2017-12-12 21:43:52 +0100 | [diff] [blame] | 8830 | unlock_extent_cached(io_tree, page_start, page_end, &cached_state); |
Josef Bacik | 8318ba7 | 2021-02-10 17:14:33 -0500 | [diff] [blame] | 8831 | up_read(&BTRFS_I(inode)->i_mmap_lock); |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8832 | |
Yunfeng Ye | 76de60e | 2019-12-03 16:59:25 +0800 | [diff] [blame] | 8833 | btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE); |
| 8834 | sb_end_pagefault(inode->i_sb); |
| 8835 | extent_changeset_free(data_reserved); |
| 8836 | return VM_FAULT_LOCKED; |
Chris Mason | 717beb9 | 2018-06-25 10:03:41 -0700 | [diff] [blame] | 8837 | |
| 8838 | out_unlock: |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8839 | unlock_page(page); |
Josef Bacik | 8318ba7 | 2021-02-10 17:14:33 -0500 | [diff] [blame] | 8840 | up_read(&BTRFS_I(inode)->i_mmap_lock); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 8841 | out: |
Qu Wenruo | 8702ba9 | 2019-10-14 14:34:51 +0800 | [diff] [blame] | 8842 | btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE); |
Nikolay Borisov | 86d5292 | 2020-06-03 08:55:40 +0300 | [diff] [blame] | 8843 | btrfs_delalloc_release_space(BTRFS_I(inode), data_reserved, page_start, |
Qu Wenruo | 43b1859 | 2017-12-12 15:34:32 +0800 | [diff] [blame] | 8844 | reserved_space, (ret != 0)); |
Chris Mason | 9998eb7 | 2012-01-25 13:47:40 -0500 | [diff] [blame] | 8845 | out_noreserve: |
Jan Kara | b2b5ef5 | 2012-06-12 16:20:45 +0200 | [diff] [blame] | 8846 | sb_end_pagefault(inode->i_sb); |
Qu Wenruo | 364ecf3 | 2017-02-27 15:10:38 +0800 | [diff] [blame] | 8847 | extent_changeset_free(data_reserved); |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8848 | return ret; |
| 8849 | } |
| 8850 | |
Filipe Manana | 213e8c5 | 2018-02-06 20:40:31 +0000 | [diff] [blame] | 8851 | static int btrfs_truncate(struct inode *inode, bool skip_writeback) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8852 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8853 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8854 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 8855 | struct btrfs_block_rsv *rsv; |
Omar Sandoval | ad7e1a7 | 2018-05-22 09:59:50 -0700 | [diff] [blame] | 8856 | int ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8857 | struct btrfs_trans_handle *trans; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8858 | u64 mask = fs_info->sectorsize - 1; |
Josef Bacik | 2bd36e7 | 2019-08-22 15:14:33 -0400 | [diff] [blame] | 8859 | u64 min_size = btrfs_calc_metadata_size(fs_info, 1); |
Filipe Manana | 0d7d316 | 2021-05-24 11:35:55 +0100 | [diff] [blame] | 8860 | u64 extents_found = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8861 | |
Filipe Manana | 213e8c5 | 2018-02-06 20:40:31 +0000 | [diff] [blame] | 8862 | if (!skip_writeback) { |
| 8863 | ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask), |
| 8864 | (u64)-1); |
| 8865 | if (ret) |
| 8866 | return ret; |
| 8867 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8868 | |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 8869 | /* |
Omar Sandoval | f7e9e8f | 2018-05-11 13:13:32 -0700 | [diff] [blame] | 8870 | * Yes ladies and gentlemen, this is indeed ugly. We have a couple of |
| 8871 | * things going on here: |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 8872 | * |
Omar Sandoval | f7e9e8f | 2018-05-11 13:13:32 -0700 | [diff] [blame] | 8873 | * 1) We need to reserve space to update our inode. |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 8874 | * |
Omar Sandoval | f7e9e8f | 2018-05-11 13:13:32 -0700 | [diff] [blame] | 8875 | * 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] | 8876 | * be free'd up by the truncate operation, but also have some slack |
| 8877 | * space reserved in case it uses space during the truncate (thank you |
| 8878 | * very much snapshotting). |
| 8879 | * |
Omar Sandoval | f7e9e8f | 2018-05-11 13:13:32 -0700 | [diff] [blame] | 8880 | * 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] | 8881 | * 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] | 8882 | * 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] | 8883 | * doesn't end up using space reserved for updating the inode. We also |
| 8884 | * need to be able to stop the transaction and start a new one, which |
| 8885 | * means we need to be able to update the inode several times, and we |
| 8886 | * have no idea of knowing how many times that will be, so we can't just |
| 8887 | * reserve 1 item for the entirety of the operation, so that has to be |
| 8888 | * done separately as well. |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 8889 | * |
| 8890 | * So that leaves us with |
| 8891 | * |
Omar Sandoval | f7e9e8f | 2018-05-11 13:13:32 -0700 | [diff] [blame] | 8892 | * 1) rsv - for the truncate reservation, which we will steal from the |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 8893 | * transaction reservation. |
Omar Sandoval | f7e9e8f | 2018-05-11 13:13:32 -0700 | [diff] [blame] | 8894 | * 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] | 8895 | * updating the inode. |
| 8896 | */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 8897 | rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP); |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 8898 | if (!rsv) |
| 8899 | return -ENOMEM; |
Josef Bacik | 4a33854 | 2011-08-29 11:01:31 -0400 | [diff] [blame] | 8900 | rsv->size = min_size; |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 8901 | rsv->failfast = 1; |
Josef Bacik | f0cd846 | 2011-03-04 14:37:08 -0500 | [diff] [blame] | 8902 | |
Josef Bacik | 907cbce | 2011-08-08 13:46:15 -0400 | [diff] [blame] | 8903 | /* |
Josef Bacik | 0712718 | 2011-08-19 10:29:59 -0400 | [diff] [blame] | 8904 | * 1 for the truncate slack space |
Josef Bacik | 907cbce | 2011-08-08 13:46:15 -0400 | [diff] [blame] | 8905 | * 1 for updating the inode. |
| 8906 | */ |
Josef Bacik | f3fe820 | 2013-01-07 17:03:21 -0500 | [diff] [blame] | 8907 | trans = btrfs_start_transaction(root, 2); |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 8908 | if (IS_ERR(trans)) { |
Omar Sandoval | ad7e1a7 | 2018-05-22 09:59:50 -0700 | [diff] [blame] | 8909 | ret = PTR_ERR(trans); |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 8910 | goto out; |
| 8911 | } |
Josef Bacik | f0cd846 | 2011-03-04 14:37:08 -0500 | [diff] [blame] | 8912 | |
Josef Bacik | 907cbce | 2011-08-08 13:46:15 -0400 | [diff] [blame] | 8913 | /* Migrate the slack space for the truncate to our reserve */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8914 | ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, rsv, |
Lu Fengqi | 3a58417 | 2018-08-04 21:10:55 +0800 | [diff] [blame] | 8915 | min_size, false); |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 8916 | BUG_ON(ret); |
Josef Bacik | f0cd846 | 2011-03-04 14:37:08 -0500 | [diff] [blame] | 8917 | |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 8918 | trans->block_rsv = rsv; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 8919 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 8920 | while (1) { |
Nikolay Borisov | 5074339 | 2020-11-02 16:48:55 +0200 | [diff] [blame] | 8921 | ret = btrfs_truncate_inode_items(trans, root, BTRFS_I(inode), |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 8922 | inode->i_size, |
Filipe Manana | 0d7d316 | 2021-05-24 11:35:55 +0100 | [diff] [blame] | 8923 | BTRFS_EXTENT_DATA_KEY, |
| 8924 | &extents_found); |
Josef Bacik | ddfae63 | 2017-10-19 14:16:02 -0400 | [diff] [blame] | 8925 | trans->block_rsv = &fs_info->trans_block_rsv; |
Omar Sandoval | ad7e1a7 | 2018-05-22 09:59:50 -0700 | [diff] [blame] | 8926 | if (ret != -ENOSPC && ret != -EAGAIN) |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 8927 | break; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8928 | |
Nikolay Borisov | 9a56fcd | 2020-11-02 16:48:59 +0200 | [diff] [blame] | 8929 | ret = btrfs_update_inode(trans, root, BTRFS_I(inode)); |
Omar Sandoval | ad7e1a7 | 2018-05-22 09:59:50 -0700 | [diff] [blame] | 8930 | if (ret) |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 8931 | break; |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 8932 | |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 8933 | btrfs_end_transaction(trans); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 8934 | btrfs_btree_balance_dirty(fs_info); |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 8935 | |
| 8936 | trans = btrfs_start_transaction(root, 2); |
| 8937 | if (IS_ERR(trans)) { |
Omar Sandoval | ad7e1a7 | 2018-05-22 09:59:50 -0700 | [diff] [blame] | 8938 | ret = PTR_ERR(trans); |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 8939 | trans = NULL; |
| 8940 | break; |
| 8941 | } |
| 8942 | |
Nikolay Borisov | 63f018b | 2020-03-10 10:59:31 +0200 | [diff] [blame] | 8943 | btrfs_block_rsv_release(fs_info, rsv, -1, NULL); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8944 | ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, |
Lu Fengqi | 3a58417 | 2018-08-04 21:10:55 +0800 | [diff] [blame] | 8945 | rsv, min_size, false); |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 8946 | BUG_ON(ret); /* shouldn't happen */ |
| 8947 | trans->block_rsv = rsv; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 8948 | } |
| 8949 | |
Josef Bacik | ddfae63 | 2017-10-19 14:16:02 -0400 | [diff] [blame] | 8950 | /* |
| 8951 | * We can't call btrfs_truncate_block inside a trans handle as we could |
| 8952 | * deadlock with freeze, if we got NEED_TRUNCATE_BLOCK then we know |
| 8953 | * we've truncated everything except the last little bit, and can do |
| 8954 | * btrfs_truncate_block and then update the disk_i_size. |
| 8955 | */ |
| 8956 | if (ret == NEED_TRUNCATE_BLOCK) { |
| 8957 | btrfs_end_transaction(trans); |
| 8958 | btrfs_btree_balance_dirty(fs_info); |
| 8959 | |
Nikolay Borisov | 217f42e | 2020-11-02 16:49:03 +0200 | [diff] [blame] | 8960 | ret = btrfs_truncate_block(BTRFS_I(inode), inode->i_size, 0, 0); |
Josef Bacik | ddfae63 | 2017-10-19 14:16:02 -0400 | [diff] [blame] | 8961 | if (ret) |
| 8962 | goto out; |
| 8963 | trans = btrfs_start_transaction(root, 1); |
| 8964 | if (IS_ERR(trans)) { |
| 8965 | ret = PTR_ERR(trans); |
| 8966 | goto out; |
| 8967 | } |
Nikolay Borisov | 76aea53 | 2020-11-02 16:48:53 +0200 | [diff] [blame] | 8968 | btrfs_inode_safe_disk_i_size_write(BTRFS_I(inode), 0); |
Josef Bacik | ddfae63 | 2017-10-19 14:16:02 -0400 | [diff] [blame] | 8969 | } |
| 8970 | |
Chris Mason | 917c16b | 2011-11-08 14:49:59 -0500 | [diff] [blame] | 8971 | if (trans) { |
Omar Sandoval | ad7e1a7 | 2018-05-22 09:59:50 -0700 | [diff] [blame] | 8972 | int ret2; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 8973 | |
Omar Sandoval | ad7e1a7 | 2018-05-22 09:59:50 -0700 | [diff] [blame] | 8974 | trans->block_rsv = &fs_info->trans_block_rsv; |
Nikolay Borisov | 9a56fcd | 2020-11-02 16:48:59 +0200 | [diff] [blame] | 8975 | ret2 = btrfs_update_inode(trans, root, BTRFS_I(inode)); |
Omar Sandoval | ad7e1a7 | 2018-05-22 09:59:50 -0700 | [diff] [blame] | 8976 | if (ret2 && !ret) |
| 8977 | ret = ret2; |
| 8978 | |
| 8979 | ret2 = btrfs_end_transaction(trans); |
| 8980 | if (ret2 && !ret) |
| 8981 | ret = ret2; |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 8982 | btrfs_btree_balance_dirty(fs_info); |
Chris Mason | 917c16b | 2011-11-08 14:49:59 -0500 | [diff] [blame] | 8983 | } |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 8984 | out: |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 8985 | btrfs_free_block_rsv(fs_info, rsv); |
Filipe Manana | 0d7d316 | 2021-05-24 11:35:55 +0100 | [diff] [blame] | 8986 | /* |
| 8987 | * So if we truncate and then write and fsync we normally would just |
| 8988 | * write the extents that changed, which is a problem if we need to |
| 8989 | * first truncate that entire inode. So set this flag so we write out |
| 8990 | * all of the extents in the inode to the sync log so we're completely |
| 8991 | * safe. |
| 8992 | * |
| 8993 | * If no extents were dropped or trimmed we don't need to force the next |
| 8994 | * fsync to truncate all the inode's items from the log and re-log them |
| 8995 | * all. This means the truncate operation did not change the file size, |
| 8996 | * or changed it to a smaller size but there was only an implicit hole |
| 8997 | * between the old i_size and the new i_size, and there were no prealloc |
| 8998 | * extents beyond i_size to drop. |
| 8999 | */ |
| 9000 | if (extents_found > 0) |
| 9001 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags); |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 9002 | |
Omar Sandoval | ad7e1a7 | 2018-05-22 09:59:50 -0700 | [diff] [blame] | 9003 | return ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9004 | } |
| 9005 | |
Sven Wegener | 3b96362 | 2008-06-09 21:57:42 -0400 | [diff] [blame] | 9006 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 9007 | * create a new subvolume directory/inode (helper for the ioctl). |
| 9008 | */ |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 9009 | int btrfs_create_subvol_root(struct btrfs_trans_handle *trans, |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 9010 | struct btrfs_root *new_root, |
Nikolay Borisov | 6994802 | 2020-12-07 17:32:37 +0200 | [diff] [blame] | 9011 | struct btrfs_root *parent_root) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9012 | { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9013 | struct inode *inode; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 9014 | int err; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 9015 | u64 index = 0; |
Nikolay Borisov | 2312510 | 2020-12-07 17:32:36 +0200 | [diff] [blame] | 9016 | u64 ino; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9017 | |
Nikolay Borisov | 2312510 | 2020-12-07 17:32:36 +0200 | [diff] [blame] | 9018 | err = btrfs_get_free_objectid(new_root, &ino); |
| 9019 | if (err < 0) |
| 9020 | return err; |
| 9021 | |
| 9022 | inode = btrfs_new_inode(trans, new_root, NULL, "..", 2, ino, ino, |
Florian Albrechtskirchinger | 12fc9d0 | 2012-02-10 22:15:54 +0100 | [diff] [blame] | 9023 | S_IFDIR | (~current_umask() & S_IRWXUGO), |
| 9024 | &index); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 9025 | if (IS_ERR(inode)) |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 9026 | return PTR_ERR(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9027 | inode->i_op = &btrfs_dir_inode_operations; |
| 9028 | inode->i_fop = &btrfs_dir_file_operations; |
| 9029 | |
Miklos Szeredi | bfe8684 | 2011-10-28 14:13:29 +0200 | [diff] [blame] | 9030 | set_nlink(inode, 1); |
Nikolay Borisov | 6ef06d2 | 2017-02-20 13:50:34 +0200 | [diff] [blame] | 9031 | btrfs_i_size_write(BTRFS_I(inode), 0); |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 9032 | unlock_new_inode(inode); |
Sven Wegener | 3b96362 | 2008-06-09 21:57:42 -0400 | [diff] [blame] | 9033 | |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 9034 | err = btrfs_subvol_inherit_props(trans, new_root, parent_root); |
| 9035 | if (err) |
| 9036 | btrfs_err(new_root->fs_info, |
David Sterba | 351fd35 | 2014-05-15 16:48:20 +0200 | [diff] [blame] | 9037 | "error inheriting subvolume %llu properties: %d", |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 9038 | new_root->root_key.objectid, err); |
| 9039 | |
Nikolay Borisov | 9a56fcd | 2020-11-02 16:48:59 +0200 | [diff] [blame] | 9040 | err = btrfs_update_inode(trans, new_root, BTRFS_I(inode)); |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 9041 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 9042 | iput(inode); |
Mark Fasheh | ce59897 | 2011-07-26 11:32:23 -0700 | [diff] [blame] | 9043 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9044 | } |
| 9045 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9046 | struct inode *btrfs_alloc_inode(struct super_block *sb) |
| 9047 | { |
Josef Bacik | 69fe2d7 | 2017-10-19 14:15:57 -0400 | [diff] [blame] | 9048 | struct btrfs_fs_info *fs_info = btrfs_sb(sb); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9049 | struct btrfs_inode *ei; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9050 | struct inode *inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9051 | |
David Sterba | 712e36c | 2017-10-31 17:08:27 +0100 | [diff] [blame] | 9052 | ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_KERNEL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9053 | if (!ei) |
| 9054 | return NULL; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9055 | |
| 9056 | ei->root = NULL; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9057 | ei->generation = 0; |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 9058 | ei->last_trans = 0; |
Chris Mason | 257c62e | 2009-10-13 13:21:08 -0400 | [diff] [blame] | 9059 | ei->last_sub_trans = 0; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 9060 | ei->logged_trans = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9061 | ei->delalloc_bytes = 0; |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 9062 | ei->new_delalloc_bytes = 0; |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 9063 | ei->defrag_bytes = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9064 | ei->disk_i_size = 0; |
| 9065 | ei->flags = 0; |
Josef Bacik | 7709cde | 2011-08-04 10:25:02 -0400 | [diff] [blame] | 9066 | ei->csum_bytes = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9067 | ei->index_cnt = (u64)-1; |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 9068 | ei->dir_index = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9069 | ei->last_unlink_trans = 0; |
Filipe Manana | 3ebac17 | 2020-07-15 12:30:43 +0100 | [diff] [blame] | 9070 | ei->last_reflink_trans = 0; |
Liu Bo | 46d8bc3 | 2012-08-29 01:07:55 -0600 | [diff] [blame] | 9071 | ei->last_log_commit = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9072 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 9073 | spin_lock_init(&ei->lock); |
| 9074 | ei->outstanding_extents = 0; |
Josef Bacik | 69fe2d7 | 2017-10-19 14:15:57 -0400 | [diff] [blame] | 9075 | if (sb->s_magic != BTRFS_TEST_MAGIC) |
| 9076 | btrfs_init_metadata_block_rsv(fs_info, &ei->block_rsv, |
| 9077 | BTRFS_BLOCK_RSV_DELALLOC); |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 9078 | ei->runtime_flags = 0; |
David Sterba | b52aa8c | 2017-07-17 19:17:20 +0200 | [diff] [blame] | 9079 | ei->prop_compress = BTRFS_COMPRESS_NONE; |
David Sterba | eec63c6 | 2017-07-17 19:41:31 +0200 | [diff] [blame] | 9080 | ei->defrag_compress = BTRFS_COMPRESS_NONE; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9081 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 9082 | ei->delayed_node = NULL; |
| 9083 | |
chandan r | 9cc97d6 | 2012-07-04 12:48:07 +0530 | [diff] [blame] | 9084 | ei->i_otime.tv_sec = 0; |
| 9085 | ei->i_otime.tv_nsec = 0; |
| 9086 | |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9087 | inode = &ei->vfs_inode; |
David Sterba | a8067e0 | 2011-04-21 00:34:43 +0200 | [diff] [blame] | 9088 | extent_map_tree_init(&ei->extent_tree); |
Qu Wenruo | 43eb5f2 | 2019-03-01 10:47:59 +0800 | [diff] [blame] | 9089 | extent_io_tree_init(fs_info, &ei->io_tree, IO_TREE_INODE_IO, inode); |
| 9090 | extent_io_tree_init(fs_info, &ei->io_failure_tree, |
| 9091 | IO_TREE_INODE_IO_FAILURE, inode); |
Josef Bacik | 41a2ee7 | 2020-01-17 09:02:21 -0500 | [diff] [blame] | 9092 | extent_io_tree_init(fs_info, &ei->file_extent_tree, |
| 9093 | IO_TREE_INODE_FILE_EXTENT, inode); |
David Sterba | 7b43973 | 2019-03-11 15:58:30 +0100 | [diff] [blame] | 9094 | ei->io_tree.track_uptodate = true; |
| 9095 | ei->io_failure_tree.track_uptodate = true; |
Josef Bacik | b812ce2 | 2012-11-16 13:56:32 -0500 | [diff] [blame] | 9096 | atomic_set(&ei->sync_writers, 0); |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9097 | mutex_init(&ei->log_mutex); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 9098 | btrfs_ordered_inode_tree_init(&ei->ordered_tree); |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9099 | INIT_LIST_HEAD(&ei->delalloc_inodes); |
David Sterba | 8089fe6 | 2015-11-19 14:15:51 +0100 | [diff] [blame] | 9100 | INIT_LIST_HEAD(&ei->delayed_iput); |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9101 | RB_CLEAR_NODE(&ei->rb_node); |
Josef Bacik | 8318ba7 | 2021-02-10 17:14:33 -0500 | [diff] [blame] | 9102 | init_rwsem(&ei->i_mmap_lock); |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9103 | |
| 9104 | return inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9105 | } |
| 9106 | |
Josef Bacik | aaedb55 | 2013-10-11 14:44:09 -0400 | [diff] [blame] | 9107 | #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS |
| 9108 | void btrfs_test_destroy_inode(struct inode *inode) |
| 9109 | { |
Nikolay Borisov | dcdbc05 | 2017-02-20 13:50:45 +0200 | [diff] [blame] | 9110 | btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0); |
Josef Bacik | aaedb55 | 2013-10-11 14:44:09 -0400 | [diff] [blame] | 9111 | kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode)); |
| 9112 | } |
| 9113 | #endif |
| 9114 | |
Al Viro | 26602ca | 2019-04-10 15:14:41 -0400 | [diff] [blame] | 9115 | void btrfs_free_inode(struct inode *inode) |
Nick Piggin | fa0d7e3d | 2011-01-07 17:49:49 +1100 | [diff] [blame] | 9116 | { |
Nick Piggin | fa0d7e3d | 2011-01-07 17:49:49 +1100 | [diff] [blame] | 9117 | kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode)); |
| 9118 | } |
| 9119 | |
Nikolay Borisov | 633cc81 | 2020-09-18 12:15:49 +0300 | [diff] [blame] | 9120 | void btrfs_destroy_inode(struct inode *vfs_inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9121 | { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 9122 | struct btrfs_ordered_extent *ordered; |
Nikolay Borisov | 633cc81 | 2020-09-18 12:15:49 +0300 | [diff] [blame] | 9123 | struct btrfs_inode *inode = BTRFS_I(vfs_inode); |
| 9124 | struct btrfs_root *root = inode->root; |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 9125 | |
Nikolay Borisov | 633cc81 | 2020-09-18 12:15:49 +0300 | [diff] [blame] | 9126 | WARN_ON(!hlist_empty(&vfs_inode->i_dentry)); |
| 9127 | WARN_ON(vfs_inode->i_data.nrpages); |
| 9128 | WARN_ON(inode->block_rsv.reserved); |
| 9129 | WARN_ON(inode->block_rsv.size); |
| 9130 | WARN_ON(inode->outstanding_extents); |
| 9131 | WARN_ON(inode->delalloc_bytes); |
| 9132 | WARN_ON(inode->new_delalloc_bytes); |
| 9133 | WARN_ON(inode->csum_bytes); |
| 9134 | WARN_ON(inode->defrag_bytes); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9135 | |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 9136 | /* |
Josef Bacik | a6dbd42 | 2009-11-11 15:53:34 -0500 | [diff] [blame] | 9137 | * This can happen where we create an inode, but somebody else also |
| 9138 | * created the same inode and we need to destroy the one we already |
| 9139 | * created. |
| 9140 | */ |
| 9141 | if (!root) |
Al Viro | 26602ca | 2019-04-10 15:14:41 -0400 | [diff] [blame] | 9142 | return; |
Josef Bacik | a6dbd42 | 2009-11-11 15:53:34 -0500 | [diff] [blame] | 9143 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 9144 | while (1) { |
Nikolay Borisov | 633cc81 | 2020-09-18 12:15:49 +0300 | [diff] [blame] | 9145 | ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 9146 | if (!ordered) |
| 9147 | break; |
| 9148 | else { |
Nikolay Borisov | 633cc81 | 2020-09-18 12:15:49 +0300 | [diff] [blame] | 9149 | btrfs_err(root->fs_info, |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 9150 | "found ordered extent %llu %llu on inode cleanup", |
Omar Sandoval | bffe633 | 2019-12-02 17:34:19 -0800 | [diff] [blame] | 9151 | ordered->file_offset, ordered->num_bytes); |
Nikolay Borisov | 71fe0a5 | 2020-09-18 12:15:50 +0300 | [diff] [blame] | 9152 | btrfs_remove_ordered_extent(inode, ordered); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 9153 | btrfs_put_ordered_extent(ordered); |
| 9154 | btrfs_put_ordered_extent(ordered); |
| 9155 | } |
| 9156 | } |
Nikolay Borisov | 633cc81 | 2020-09-18 12:15:49 +0300 | [diff] [blame] | 9157 | btrfs_qgroup_check_reserved_leak(inode); |
| 9158 | inode_tree_del(inode); |
| 9159 | btrfs_drop_extent_cache(inode, 0, (u64)-1, 0); |
| 9160 | btrfs_inode_clear_file_extent_range(inode, 0, (u64)-1); |
| 9161 | btrfs_put_root(inode->root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9162 | } |
| 9163 | |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 9164 | int btrfs_drop_inode(struct inode *inode) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 9165 | { |
| 9166 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 9167 | |
Naohiro Aota | 6379ef9 | 2013-06-06 09:56:34 +0000 | [diff] [blame] | 9168 | if (root == NULL) |
| 9169 | return 1; |
| 9170 | |
Liu Bo | fa6ac87 | 2013-02-20 14:10:23 +0000 | [diff] [blame] | 9171 | /* the snap/subvol tree is on deleting */ |
Stefan Behrens | 69e9c6c | 2013-09-05 16:58:43 +0200 | [diff] [blame] | 9172 | if (btrfs_root_refs(&root->root_item) == 0) |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 9173 | return 1; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 9174 | else |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 9175 | return generic_drop_inode(inode); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 9176 | } |
| 9177 | |
Sven Wegener | 0ee0fda | 2008-07-30 16:54:26 -0400 | [diff] [blame] | 9178 | static void init_once(void *foo) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9179 | { |
| 9180 | struct btrfs_inode *ei = (struct btrfs_inode *) foo; |
| 9181 | |
| 9182 | inode_init_once(&ei->vfs_inode); |
| 9183 | } |
| 9184 | |
David Sterba | e67c718 | 2018-02-19 17:24:18 +0100 | [diff] [blame] | 9185 | void __cold btrfs_destroy_cachep(void) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9186 | { |
Kirill A. Shutemov | 8c0a853 | 2012-09-26 11:33:07 +1000 | [diff] [blame] | 9187 | /* |
| 9188 | * Make sure all delayed rcu free inodes are flushed before we |
| 9189 | * destroy cache. |
| 9190 | */ |
| 9191 | rcu_barrier(); |
Kinglong Mee | 5598e90 | 2016-01-29 21:36:35 +0800 | [diff] [blame] | 9192 | kmem_cache_destroy(btrfs_inode_cachep); |
| 9193 | kmem_cache_destroy(btrfs_trans_handle_cachep); |
Kinglong Mee | 5598e90 | 2016-01-29 21:36:35 +0800 | [diff] [blame] | 9194 | kmem_cache_destroy(btrfs_path_cachep); |
| 9195 | kmem_cache_destroy(btrfs_free_space_cachep); |
Christophe Leroy | 3acd485 | 2019-08-21 15:05:55 +0000 | [diff] [blame] | 9196 | kmem_cache_destroy(btrfs_free_space_bitmap_cachep); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9197 | } |
| 9198 | |
Liu Bo | f5c29bd | 2017-11-02 17:21:50 -0600 | [diff] [blame] | 9199 | int __init btrfs_init_cachep(void) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9200 | { |
David Sterba | 837e197 | 2012-09-07 03:00:48 -0600 | [diff] [blame] | 9201 | btrfs_inode_cachep = kmem_cache_create("btrfs_inode", |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 9202 | sizeof(struct btrfs_inode), 0, |
Vladimir Davydov | 5d09705 | 2016-01-14 15:18:21 -0800 | [diff] [blame] | 9203 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT, |
| 9204 | init_once); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9205 | if (!btrfs_inode_cachep) |
| 9206 | goto fail; |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 9207 | |
David Sterba | 837e197 | 2012-09-07 03:00:48 -0600 | [diff] [blame] | 9208 | btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle", |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 9209 | sizeof(struct btrfs_trans_handle), 0, |
Nikolay Borisov | fba4b69 | 2016-06-23 21:17:08 +0300 | [diff] [blame] | 9210 | SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9211 | if (!btrfs_trans_handle_cachep) |
| 9212 | goto fail; |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 9213 | |
David Sterba | 837e197 | 2012-09-07 03:00:48 -0600 | [diff] [blame] | 9214 | btrfs_path_cachep = kmem_cache_create("btrfs_path", |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 9215 | sizeof(struct btrfs_path), 0, |
Nikolay Borisov | fba4b69 | 2016-06-23 21:17:08 +0300 | [diff] [blame] | 9216 | SLAB_MEM_SPREAD, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9217 | if (!btrfs_path_cachep) |
| 9218 | goto fail; |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 9219 | |
David Sterba | 837e197 | 2012-09-07 03:00:48 -0600 | [diff] [blame] | 9220 | btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space", |
Josef Bacik | dc89e98 | 2011-01-28 17:05:48 -0500 | [diff] [blame] | 9221 | sizeof(struct btrfs_free_space), 0, |
Nikolay Borisov | fba4b69 | 2016-06-23 21:17:08 +0300 | [diff] [blame] | 9222 | SLAB_MEM_SPREAD, NULL); |
Josef Bacik | dc89e98 | 2011-01-28 17:05:48 -0500 | [diff] [blame] | 9223 | if (!btrfs_free_space_cachep) |
| 9224 | goto fail; |
| 9225 | |
Christophe Leroy | 3acd485 | 2019-08-21 15:05:55 +0000 | [diff] [blame] | 9226 | btrfs_free_space_bitmap_cachep = kmem_cache_create("btrfs_free_space_bitmap", |
| 9227 | PAGE_SIZE, PAGE_SIZE, |
David Sterba | 34e4999 | 2021-03-15 15:18:24 +0100 | [diff] [blame] | 9228 | SLAB_MEM_SPREAD, NULL); |
Christophe Leroy | 3acd485 | 2019-08-21 15:05:55 +0000 | [diff] [blame] | 9229 | if (!btrfs_free_space_bitmap_cachep) |
| 9230 | goto fail; |
| 9231 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9232 | return 0; |
| 9233 | fail: |
| 9234 | btrfs_destroy_cachep(); |
| 9235 | return -ENOMEM; |
| 9236 | } |
| 9237 | |
Christian Brauner | 549c729 | 2021-01-21 14:19:43 +0100 | [diff] [blame] | 9238 | static int btrfs_getattr(struct user_namespace *mnt_userns, |
| 9239 | const struct path *path, struct kstat *stat, |
David Howells | a528d35 | 2017-01-31 16:46:22 +0000 | [diff] [blame] | 9240 | u32 request_mask, unsigned int flags) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9241 | { |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 9242 | u64 delalloc_bytes; |
Filipe Manana | 2766ff6 | 2020-11-04 11:07:34 +0000 | [diff] [blame] | 9243 | u64 inode_bytes; |
David Howells | a528d35 | 2017-01-31 16:46:22 +0000 | [diff] [blame] | 9244 | struct inode *inode = d_inode(path->dentry); |
David Sterba | fadc0d8 | 2011-11-20 07:33:38 -0500 | [diff] [blame] | 9245 | u32 blocksize = inode->i_sb->s_blocksize; |
Yonghong Song | 04a87e3 | 2017-05-12 15:07:43 -0700 | [diff] [blame] | 9246 | u32 bi_flags = BTRFS_I(inode)->flags; |
| 9247 | |
| 9248 | stat->result_mask |= STATX_BTIME; |
| 9249 | stat->btime.tv_sec = BTRFS_I(inode)->i_otime.tv_sec; |
| 9250 | stat->btime.tv_nsec = BTRFS_I(inode)->i_otime.tv_nsec; |
| 9251 | if (bi_flags & BTRFS_INODE_APPEND) |
| 9252 | stat->attributes |= STATX_ATTR_APPEND; |
| 9253 | if (bi_flags & BTRFS_INODE_COMPRESS) |
| 9254 | stat->attributes |= STATX_ATTR_COMPRESSED; |
| 9255 | if (bi_flags & BTRFS_INODE_IMMUTABLE) |
| 9256 | stat->attributes |= STATX_ATTR_IMMUTABLE; |
| 9257 | if (bi_flags & BTRFS_INODE_NODUMP) |
| 9258 | stat->attributes |= STATX_ATTR_NODUMP; |
| 9259 | |
| 9260 | stat->attributes_mask |= (STATX_ATTR_APPEND | |
| 9261 | STATX_ATTR_COMPRESSED | |
| 9262 | STATX_ATTR_IMMUTABLE | |
| 9263 | STATX_ATTR_NODUMP); |
David Sterba | fadc0d8 | 2011-11-20 07:33:38 -0500 | [diff] [blame] | 9264 | |
Christian Brauner | 0d56a45 | 2021-01-21 14:19:30 +0100 | [diff] [blame] | 9265 | generic_fillattr(&init_user_ns, inode, stat); |
Al Viro | 0ee5dc6 | 2011-07-07 15:44:25 -0400 | [diff] [blame] | 9266 | stat->dev = BTRFS_I(inode)->root->anon_dev; |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 9267 | |
| 9268 | spin_lock(&BTRFS_I(inode)->lock); |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 9269 | delalloc_bytes = BTRFS_I(inode)->new_delalloc_bytes; |
Filipe Manana | 2766ff6 | 2020-11-04 11:07:34 +0000 | [diff] [blame] | 9270 | inode_bytes = inode_get_bytes(inode); |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 9271 | spin_unlock(&BTRFS_I(inode)->lock); |
Filipe Manana | 2766ff6 | 2020-11-04 11:07:34 +0000 | [diff] [blame] | 9272 | stat->blocks = (ALIGN(inode_bytes, blocksize) + |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 9273 | ALIGN(delalloc_bytes, blocksize)) >> 9; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9274 | return 0; |
| 9275 | } |
| 9276 | |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9277 | static int btrfs_rename_exchange(struct inode *old_dir, |
| 9278 | struct dentry *old_dentry, |
| 9279 | struct inode *new_dir, |
| 9280 | struct dentry *new_dentry) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9281 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9282 | struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9283 | struct btrfs_trans_handle *trans; |
| 9284 | struct btrfs_root *root = BTRFS_I(old_dir)->root; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9285 | struct btrfs_root *dest = BTRFS_I(new_dir)->root; |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9286 | struct inode *new_inode = new_dentry->d_inode; |
| 9287 | struct inode *old_inode = old_dentry->d_inode; |
Deepa Dinamani | 95582b0 | 2018-05-08 19:36:02 -0700 | [diff] [blame] | 9288 | struct timespec64 ctime = current_time(old_inode); |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 9289 | u64 old_ino = btrfs_ino(BTRFS_I(old_inode)); |
| 9290 | u64 new_ino = btrfs_ino(BTRFS_I(new_inode)); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9291 | u64 old_idx = 0; |
| 9292 | u64 new_idx = 0; |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9293 | int ret; |
Filipe Manana | 75b463d | 2020-08-11 12:43:48 +0100 | [diff] [blame] | 9294 | int ret2; |
Filipe Manana | 86e8aa0 | 2016-05-05 02:02:27 +0100 | [diff] [blame] | 9295 | bool root_log_pinned = false; |
| 9296 | bool dest_log_pinned = false; |
Josef Bacik | dc09ef3 | 2021-05-19 14:04:21 -0400 | [diff] [blame] | 9297 | bool need_abort = false; |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9298 | |
NeilBrown | 3f79f6f | 2021-08-06 14:26:24 +1000 | [diff] [blame] | 9299 | /* |
| 9300 | * For non-subvolumes allow exchange only within one subvolume, in the |
| 9301 | * same inode namespace. Two subvolumes (represented as directory) can |
| 9302 | * be exchanged as they're a logical link and have a fixed inode number. |
| 9303 | */ |
| 9304 | if (root != dest && |
| 9305 | (old_ino != BTRFS_FIRST_FREE_OBJECTID || |
| 9306 | new_ino != BTRFS_FIRST_FREE_OBJECTID)) |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9307 | return -EXDEV; |
| 9308 | |
| 9309 | /* close the race window with snapshot create/destroy ioctl */ |
Josef Bacik | 943eb3b | 2019-11-19 13:59:20 -0500 | [diff] [blame] | 9310 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID || |
| 9311 | new_ino == BTRFS_FIRST_FREE_OBJECTID) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9312 | down_read(&fs_info->subvol_sem); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9313 | |
| 9314 | /* |
| 9315 | * We want to reserve the absolute worst case amount of items. So if |
| 9316 | * both inodes are subvols and we need to unlink them then that would |
| 9317 | * require 4 item modifications, but if they are both normal inodes it |
| 9318 | * would require 5 item modifications, so we'll assume their normal |
| 9319 | * inodes. So 5 * 2 is 10, plus 2 for the new links, so 12 total items |
| 9320 | * should cover the worst case number of items we'll modify. |
| 9321 | */ |
| 9322 | trans = btrfs_start_transaction(root, 12); |
| 9323 | if (IS_ERR(trans)) { |
| 9324 | ret = PTR_ERR(trans); |
| 9325 | goto out_notrans; |
| 9326 | } |
| 9327 | |
Josef Bacik | 00aa8e8 | 2021-03-12 15:25:02 -0500 | [diff] [blame] | 9328 | if (dest != root) { |
| 9329 | ret = btrfs_record_root_in_trans(trans, dest); |
| 9330 | if (ret) |
| 9331 | goto out_fail; |
| 9332 | } |
Josef Bacik | 3e17409 | 2019-11-15 15:43:06 -0500 | [diff] [blame] | 9333 | |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9334 | /* |
| 9335 | * We need to find a free sequence number both in the source and |
| 9336 | * in the destination directory for the exchange. |
| 9337 | */ |
Nikolay Borisov | 877574e | 2017-02-20 13:50:33 +0200 | [diff] [blame] | 9338 | ret = btrfs_set_inode_index(BTRFS_I(new_dir), &old_idx); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9339 | if (ret) |
| 9340 | goto out_fail; |
Nikolay Borisov | 877574e | 2017-02-20 13:50:33 +0200 | [diff] [blame] | 9341 | ret = btrfs_set_inode_index(BTRFS_I(old_dir), &new_idx); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9342 | if (ret) |
| 9343 | goto out_fail; |
| 9344 | |
| 9345 | BTRFS_I(old_inode)->dir_index = 0ULL; |
| 9346 | BTRFS_I(new_inode)->dir_index = 0ULL; |
| 9347 | |
| 9348 | /* Reference for the source. */ |
| 9349 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) { |
| 9350 | /* force full log commit if subvolume involved. */ |
David Sterba | 9078776 | 2019-03-20 13:28:05 +0100 | [diff] [blame] | 9351 | btrfs_set_log_full_commit(trans); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9352 | } else { |
Filipe Manana | 376e5a5 | 2016-05-05 02:08:56 +0100 | [diff] [blame] | 9353 | btrfs_pin_log_trans(root); |
| 9354 | root_log_pinned = true; |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9355 | ret = btrfs_insert_inode_ref(trans, dest, |
| 9356 | new_dentry->d_name.name, |
| 9357 | new_dentry->d_name.len, |
| 9358 | old_ino, |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 9359 | btrfs_ino(BTRFS_I(new_dir)), |
| 9360 | old_idx); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9361 | if (ret) |
| 9362 | goto out_fail; |
Josef Bacik | dc09ef3 | 2021-05-19 14:04:21 -0400 | [diff] [blame] | 9363 | need_abort = true; |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9364 | } |
| 9365 | |
| 9366 | /* And now for the dest. */ |
| 9367 | if (new_ino == BTRFS_FIRST_FREE_OBJECTID) { |
| 9368 | /* force full log commit if subvolume involved. */ |
David Sterba | 9078776 | 2019-03-20 13:28:05 +0100 | [diff] [blame] | 9369 | btrfs_set_log_full_commit(trans); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9370 | } else { |
Filipe Manana | 376e5a5 | 2016-05-05 02:08:56 +0100 | [diff] [blame] | 9371 | btrfs_pin_log_trans(dest); |
| 9372 | dest_log_pinned = true; |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9373 | ret = btrfs_insert_inode_ref(trans, root, |
| 9374 | old_dentry->d_name.name, |
| 9375 | old_dentry->d_name.len, |
| 9376 | new_ino, |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 9377 | btrfs_ino(BTRFS_I(old_dir)), |
| 9378 | new_idx); |
Josef Bacik | dc09ef3 | 2021-05-19 14:04:21 -0400 | [diff] [blame] | 9379 | if (ret) { |
| 9380 | if (need_abort) |
| 9381 | btrfs_abort_transaction(trans, ret); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9382 | goto out_fail; |
Josef Bacik | dc09ef3 | 2021-05-19 14:04:21 -0400 | [diff] [blame] | 9383 | } |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9384 | } |
| 9385 | |
| 9386 | /* Update inode version and ctime/mtime. */ |
| 9387 | inode_inc_iversion(old_dir); |
| 9388 | inode_inc_iversion(new_dir); |
| 9389 | inode_inc_iversion(old_inode); |
| 9390 | inode_inc_iversion(new_inode); |
| 9391 | old_dir->i_ctime = old_dir->i_mtime = ctime; |
| 9392 | new_dir->i_ctime = new_dir->i_mtime = ctime; |
| 9393 | old_inode->i_ctime = ctime; |
| 9394 | new_inode->i_ctime = ctime; |
| 9395 | |
| 9396 | if (old_dentry->d_parent != new_dentry->d_parent) { |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 9397 | btrfs_record_unlink_dir(trans, BTRFS_I(old_dir), |
| 9398 | BTRFS_I(old_inode), 1); |
| 9399 | btrfs_record_unlink_dir(trans, BTRFS_I(new_dir), |
| 9400 | BTRFS_I(new_inode), 1); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9401 | } |
| 9402 | |
| 9403 | /* src is a subvolume */ |
| 9404 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) { |
Josef Bacik | 045d396 | 2019-12-18 17:20:27 -0500 | [diff] [blame] | 9405 | ret = btrfs_unlink_subvol(trans, old_dir, old_dentry); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9406 | } else { /* src is an inode */ |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 9407 | ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir), |
| 9408 | BTRFS_I(old_dentry->d_inode), |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9409 | old_dentry->d_name.name, |
| 9410 | old_dentry->d_name.len); |
| 9411 | if (!ret) |
Nikolay Borisov | 9a56fcd | 2020-11-02 16:48:59 +0200 | [diff] [blame] | 9412 | ret = btrfs_update_inode(trans, root, BTRFS_I(old_inode)); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9413 | } |
| 9414 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 9415 | btrfs_abort_transaction(trans, ret); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9416 | goto out_fail; |
| 9417 | } |
| 9418 | |
| 9419 | /* dest is a subvolume */ |
| 9420 | if (new_ino == BTRFS_FIRST_FREE_OBJECTID) { |
Josef Bacik | 045d396 | 2019-12-18 17:20:27 -0500 | [diff] [blame] | 9421 | ret = btrfs_unlink_subvol(trans, new_dir, new_dentry); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9422 | } else { /* dest is an inode */ |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 9423 | ret = __btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir), |
| 9424 | BTRFS_I(new_dentry->d_inode), |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9425 | new_dentry->d_name.name, |
| 9426 | new_dentry->d_name.len); |
| 9427 | if (!ret) |
Nikolay Borisov | 9a56fcd | 2020-11-02 16:48:59 +0200 | [diff] [blame] | 9428 | ret = btrfs_update_inode(trans, dest, BTRFS_I(new_inode)); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9429 | } |
| 9430 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 9431 | btrfs_abort_transaction(trans, ret); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9432 | goto out_fail; |
| 9433 | } |
| 9434 | |
Nikolay Borisov | db0a669 | 2017-02-20 13:51:08 +0200 | [diff] [blame] | 9435 | 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] | 9436 | new_dentry->d_name.name, |
| 9437 | new_dentry->d_name.len, 0, old_idx); |
| 9438 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 9439 | btrfs_abort_transaction(trans, ret); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9440 | goto out_fail; |
| 9441 | } |
| 9442 | |
Nikolay Borisov | db0a669 | 2017-02-20 13:51:08 +0200 | [diff] [blame] | 9443 | 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] | 9444 | old_dentry->d_name.name, |
| 9445 | old_dentry->d_name.len, 0, new_idx); |
| 9446 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 9447 | btrfs_abort_transaction(trans, ret); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9448 | goto out_fail; |
| 9449 | } |
| 9450 | |
| 9451 | if (old_inode->i_nlink == 1) |
| 9452 | BTRFS_I(old_inode)->dir_index = old_idx; |
| 9453 | if (new_inode->i_nlink == 1) |
| 9454 | BTRFS_I(new_inode)->dir_index = new_idx; |
| 9455 | |
Filipe Manana | 86e8aa0 | 2016-05-05 02:02:27 +0100 | [diff] [blame] | 9456 | if (root_log_pinned) { |
Filipe Manana | 75b463d | 2020-08-11 12:43:48 +0100 | [diff] [blame] | 9457 | btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir), |
| 9458 | new_dentry->d_parent); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9459 | btrfs_end_log_trans(root); |
Filipe Manana | 86e8aa0 | 2016-05-05 02:02:27 +0100 | [diff] [blame] | 9460 | root_log_pinned = false; |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9461 | } |
Filipe Manana | 86e8aa0 | 2016-05-05 02:02:27 +0100 | [diff] [blame] | 9462 | if (dest_log_pinned) { |
Filipe Manana | 75b463d | 2020-08-11 12:43:48 +0100 | [diff] [blame] | 9463 | btrfs_log_new_name(trans, BTRFS_I(new_inode), BTRFS_I(new_dir), |
| 9464 | old_dentry->d_parent); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9465 | btrfs_end_log_trans(dest); |
Filipe Manana | 86e8aa0 | 2016-05-05 02:02:27 +0100 | [diff] [blame] | 9466 | dest_log_pinned = false; |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9467 | } |
| 9468 | out_fail: |
Filipe Manana | 86e8aa0 | 2016-05-05 02:02:27 +0100 | [diff] [blame] | 9469 | /* |
| 9470 | * If we have pinned a log and an error happened, we unpin tasks |
| 9471 | * trying to sync the log and force them to fallback to a transaction |
| 9472 | * commit if the log currently contains any of the inodes involved in |
| 9473 | * this rename operation (to ensure we do not persist a log with an |
| 9474 | * inconsistent state for any of these inodes or leading to any |
| 9475 | * inconsistencies when replayed). If the transaction was aborted, the |
| 9476 | * abortion reason is propagated to userspace when attempting to commit |
| 9477 | * the transaction. If the log does not contain any of these inodes, we |
| 9478 | * allow the tasks to sync it. |
| 9479 | */ |
| 9480 | if (ret && (root_log_pinned || dest_log_pinned)) { |
Nikolay Borisov | 0f8939b | 2017-01-18 00:31:30 +0200 | [diff] [blame] | 9481 | if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) || |
| 9482 | btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) || |
| 9483 | btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) || |
Filipe Manana | 86e8aa0 | 2016-05-05 02:02:27 +0100 | [diff] [blame] | 9484 | (new_inode && |
Nikolay Borisov | 0f8939b | 2017-01-18 00:31:30 +0200 | [diff] [blame] | 9485 | btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation))) |
David Sterba | 9078776 | 2019-03-20 13:28:05 +0100 | [diff] [blame] | 9486 | btrfs_set_log_full_commit(trans); |
Filipe Manana | 86e8aa0 | 2016-05-05 02:02:27 +0100 | [diff] [blame] | 9487 | |
| 9488 | if (root_log_pinned) { |
| 9489 | btrfs_end_log_trans(root); |
| 9490 | root_log_pinned = false; |
| 9491 | } |
| 9492 | if (dest_log_pinned) { |
| 9493 | btrfs_end_log_trans(dest); |
| 9494 | dest_log_pinned = false; |
| 9495 | } |
| 9496 | } |
Filipe Manana | 75b463d | 2020-08-11 12:43:48 +0100 | [diff] [blame] | 9497 | ret2 = btrfs_end_transaction(trans); |
| 9498 | ret = ret ? ret : ret2; |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9499 | out_notrans: |
Josef Bacik | 943eb3b | 2019-11-19 13:59:20 -0500 | [diff] [blame] | 9500 | if (new_ino == BTRFS_FIRST_FREE_OBJECTID || |
| 9501 | old_ino == BTRFS_FIRST_FREE_OBJECTID) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9502 | up_read(&fs_info->subvol_sem); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9503 | |
| 9504 | return ret; |
| 9505 | } |
| 9506 | |
| 9507 | static int btrfs_whiteout_for_rename(struct btrfs_trans_handle *trans, |
| 9508 | struct btrfs_root *root, |
| 9509 | struct inode *dir, |
| 9510 | struct dentry *dentry) |
| 9511 | { |
| 9512 | int ret; |
| 9513 | struct inode *inode; |
| 9514 | u64 objectid; |
| 9515 | u64 index; |
| 9516 | |
Nikolay Borisov | 543068a | 2020-12-07 17:32:33 +0200 | [diff] [blame] | 9517 | ret = btrfs_get_free_objectid(root, &objectid); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9518 | if (ret) |
| 9519 | return ret; |
| 9520 | |
| 9521 | inode = btrfs_new_inode(trans, root, dir, |
| 9522 | dentry->d_name.name, |
| 9523 | dentry->d_name.len, |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 9524 | btrfs_ino(BTRFS_I(dir)), |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9525 | objectid, |
| 9526 | S_IFCHR | WHITEOUT_MODE, |
| 9527 | &index); |
| 9528 | |
| 9529 | if (IS_ERR(inode)) { |
| 9530 | ret = PTR_ERR(inode); |
| 9531 | return ret; |
| 9532 | } |
| 9533 | |
| 9534 | inode->i_op = &btrfs_special_inode_operations; |
| 9535 | init_special_inode(inode, inode->i_mode, |
| 9536 | WHITEOUT_DEV); |
| 9537 | |
| 9538 | ret = btrfs_init_inode_security(trans, inode, dir, |
| 9539 | &dentry->d_name); |
| 9540 | if (ret) |
Filipe Manana | c990161 | 2016-05-05 01:41:57 +0100 | [diff] [blame] | 9541 | goto out; |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9542 | |
Nikolay Borisov | cef415a | 2017-02-20 13:51:09 +0200 | [diff] [blame] | 9543 | ret = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, |
| 9544 | BTRFS_I(inode), 0, index); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9545 | if (ret) |
Filipe Manana | c990161 | 2016-05-05 01:41:57 +0100 | [diff] [blame] | 9546 | goto out; |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9547 | |
Nikolay Borisov | 9a56fcd | 2020-11-02 16:48:59 +0200 | [diff] [blame] | 9548 | ret = btrfs_update_inode(trans, root, BTRFS_I(inode)); |
Filipe Manana | c990161 | 2016-05-05 01:41:57 +0100 | [diff] [blame] | 9549 | out: |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9550 | unlock_new_inode(inode); |
Filipe Manana | c990161 | 2016-05-05 01:41:57 +0100 | [diff] [blame] | 9551 | if (ret) |
| 9552 | inode_dec_link_count(inode); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9553 | iput(inode); |
| 9554 | |
Filipe Manana | c990161 | 2016-05-05 01:41:57 +0100 | [diff] [blame] | 9555 | return ret; |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9556 | } |
| 9557 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9558 | static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry, |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9559 | struct inode *new_dir, struct dentry *new_dentry, |
| 9560 | unsigned int flags) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9561 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9562 | struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9563 | struct btrfs_trans_handle *trans; |
Filipe Manana | 5062af3 | 2016-05-05 10:26:26 +0100 | [diff] [blame] | 9564 | unsigned int trans_num_items; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9565 | struct btrfs_root *root = BTRFS_I(old_dir)->root; |
| 9566 | struct btrfs_root *dest = BTRFS_I(new_dir)->root; |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 9567 | struct inode *new_inode = d_inode(new_dentry); |
| 9568 | struct inode *old_inode = d_inode(old_dentry); |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 9569 | u64 index = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9570 | int ret; |
Filipe Manana | 75b463d | 2020-08-11 12:43:48 +0100 | [diff] [blame] | 9571 | int ret2; |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 9572 | u64 old_ino = btrfs_ino(BTRFS_I(old_inode)); |
Filipe Manana | 3dc9e8f7 | 2016-04-29 11:34:22 +0100 | [diff] [blame] | 9573 | bool log_pinned = false; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9574 | |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 9575 | if (btrfs_ino(BTRFS_I(new_dir)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) |
Yan, Zheng | f679a84 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 9576 | return -EPERM; |
| 9577 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9578 | /* we only allow rename subvolume link between subvolumes */ |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 9579 | if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest) |
Chris Mason | 3394e16 | 2008-11-17 20:42:26 -0500 | [diff] [blame] | 9580 | return -EXDEV; |
| 9581 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 9582 | if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID || |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 9583 | (new_inode && btrfs_ino(BTRFS_I(new_inode)) == BTRFS_FIRST_FREE_OBJECTID)) |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9584 | return -ENOTEMPTY; |
| 9585 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9586 | if (S_ISDIR(old_inode->i_mode) && new_inode && |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9587 | new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9588 | return -ENOTEMPTY; |
Chris Mason | 9c52057 | 2012-12-17 14:26:57 -0500 | [diff] [blame] | 9589 | |
| 9590 | |
| 9591 | /* check for collisions, even if the name isn't there */ |
Josef Bacik | 4871c15 | 2013-10-09 12:24:04 -0400 | [diff] [blame] | 9592 | ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino, |
Chris Mason | 9c52057 | 2012-12-17 14:26:57 -0500 | [diff] [blame] | 9593 | new_dentry->d_name.name, |
| 9594 | new_dentry->d_name.len); |
| 9595 | |
| 9596 | if (ret) { |
| 9597 | if (ret == -EEXIST) { |
| 9598 | /* we shouldn't get |
| 9599 | * eexist without a new_inode */ |
Dulshani Gunawardhana | fae7f21 | 2013-10-31 10:30:08 +0530 | [diff] [blame] | 9600 | if (WARN_ON(!new_inode)) { |
Chris Mason | 9c52057 | 2012-12-17 14:26:57 -0500 | [diff] [blame] | 9601 | return ret; |
| 9602 | } |
| 9603 | } else { |
| 9604 | /* maybe -EOVERFLOW */ |
| 9605 | return ret; |
| 9606 | } |
| 9607 | } |
| 9608 | ret = 0; |
| 9609 | |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 9610 | /* |
Chris Mason | 8d875f9 | 2014-08-12 10:47:42 -0700 | [diff] [blame] | 9611 | * we're using rename to replace one file with another. Start IO on it |
| 9612 | * 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] | 9613 | */ |
Chris Mason | 8d875f9 | 2014-08-12 10:47:42 -0700 | [diff] [blame] | 9614 | 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] | 9615 | filemap_flush(old_inode->i_mapping); |
| 9616 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 9617 | /* close the racy window with snapshot create/destroy ioctl */ |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 9618 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9619 | down_read(&fs_info->subvol_sem); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 9620 | /* |
| 9621 | * We want to reserve the absolute worst case amount of items. So if |
| 9622 | * both inodes are subvols and we need to unlink them then that would |
| 9623 | * require 4 item modifications, but if they are both normal inodes it |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9624 | * would require 5 item modifications, so we'll assume they are normal |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 9625 | * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items |
| 9626 | * should cover the worst case number of items we'll modify. |
Filipe Manana | 5062af3 | 2016-05-05 10:26:26 +0100 | [diff] [blame] | 9627 | * If our rename has the whiteout flag, we need more 5 units for the |
| 9628 | * new inode (1 inode item, 1 inode ref, 2 dir items and 1 xattr item |
| 9629 | * when selinux is enabled). |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 9630 | */ |
Filipe Manana | 5062af3 | 2016-05-05 10:26:26 +0100 | [diff] [blame] | 9631 | trans_num_items = 11; |
| 9632 | if (flags & RENAME_WHITEOUT) |
| 9633 | trans_num_items += 5; |
| 9634 | trans = btrfs_start_transaction(root, trans_num_items); |
Johann Lombardi | b44c59a | 2011-03-31 13:23:47 +0000 | [diff] [blame] | 9635 | if (IS_ERR(trans)) { |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9636 | ret = PTR_ERR(trans); |
| 9637 | goto out_notrans; |
| 9638 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 9639 | |
Josef Bacik | b0fec6f | 2021-03-12 15:25:03 -0500 | [diff] [blame] | 9640 | if (dest != root) { |
| 9641 | ret = btrfs_record_root_in_trans(trans, dest); |
| 9642 | if (ret) |
| 9643 | goto out_fail; |
| 9644 | } |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9645 | |
Nikolay Borisov | 877574e | 2017-02-20 13:50:33 +0200 | [diff] [blame] | 9646 | ret = btrfs_set_inode_index(BTRFS_I(new_dir), &index); |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 9647 | if (ret) |
| 9648 | goto out_fail; |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 9649 | |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 9650 | BTRFS_I(old_inode)->dir_index = 0ULL; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 9651 | if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9652 | /* force full log commit if subvolume involved. */ |
David Sterba | 9078776 | 2019-03-20 13:28:05 +0100 | [diff] [blame] | 9653 | btrfs_set_log_full_commit(trans); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9654 | } else { |
Filipe Manana | c4aba95 | 2016-04-29 13:14:42 +0100 | [diff] [blame] | 9655 | btrfs_pin_log_trans(root); |
| 9656 | log_pinned = true; |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 9657 | ret = btrfs_insert_inode_ref(trans, dest, |
| 9658 | new_dentry->d_name.name, |
| 9659 | new_dentry->d_name.len, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 9660 | old_ino, |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 9661 | btrfs_ino(BTRFS_I(new_dir)), index); |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 9662 | if (ret) |
| 9663 | goto out_fail; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9664 | } |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 9665 | |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 9666 | inode_inc_iversion(old_dir); |
| 9667 | inode_inc_iversion(new_dir); |
| 9668 | inode_inc_iversion(old_inode); |
Deepa Dinamani | 04b285f | 2016-02-06 23:57:21 -0800 | [diff] [blame] | 9669 | old_dir->i_ctime = old_dir->i_mtime = |
| 9670 | new_dir->i_ctime = new_dir->i_mtime = |
Deepa Dinamani | c2050a4 | 2016-09-14 07:48:06 -0700 | [diff] [blame] | 9671 | old_inode->i_ctime = current_time(old_dir); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 9672 | |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 9673 | if (old_dentry->d_parent != new_dentry->d_parent) |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 9674 | btrfs_record_unlink_dir(trans, BTRFS_I(old_dir), |
| 9675 | BTRFS_I(old_inode), 1); |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 9676 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 9677 | if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) { |
Josef Bacik | 045d396 | 2019-12-18 17:20:27 -0500 | [diff] [blame] | 9678 | ret = btrfs_unlink_subvol(trans, old_dir, old_dentry); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9679 | } else { |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 9680 | ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir), |
| 9681 | BTRFS_I(d_inode(old_dentry)), |
Al Viro | 9298679 | 2011-03-04 17:14:37 +0000 | [diff] [blame] | 9682 | old_dentry->d_name.name, |
| 9683 | old_dentry->d_name.len); |
| 9684 | if (!ret) |
Nikolay Borisov | 9a56fcd | 2020-11-02 16:48:59 +0200 | [diff] [blame] | 9685 | ret = btrfs_update_inode(trans, root, BTRFS_I(old_inode)); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9686 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 9687 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 9688 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 9689 | goto out_fail; |
| 9690 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9691 | |
| 9692 | if (new_inode) { |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 9693 | inode_inc_iversion(new_inode); |
Deepa Dinamani | c2050a4 | 2016-09-14 07:48:06 -0700 | [diff] [blame] | 9694 | new_inode->i_ctime = current_time(new_inode); |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 9695 | if (unlikely(btrfs_ino(BTRFS_I(new_inode)) == |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9696 | BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) { |
Josef Bacik | 045d396 | 2019-12-18 17:20:27 -0500 | [diff] [blame] | 9697 | ret = btrfs_unlink_subvol(trans, new_dir, new_dentry); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9698 | BUG_ON(new_inode->i_nlink == 0); |
| 9699 | } else { |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 9700 | ret = btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir), |
| 9701 | BTRFS_I(d_inode(new_dentry)), |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9702 | new_dentry->d_name.name, |
| 9703 | new_dentry->d_name.len); |
| 9704 | } |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 9705 | if (!ret && new_inode->i_nlink == 0) |
Nikolay Borisov | 73f2e54 | 2017-02-20 13:50:59 +0200 | [diff] [blame] | 9706 | ret = btrfs_orphan_add(trans, |
| 9707 | BTRFS_I(d_inode(new_dentry))); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 9708 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 9709 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 9710 | goto out_fail; |
| 9711 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9712 | } |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 9713 | |
Nikolay Borisov | db0a669 | 2017-02-20 13:51:08 +0200 | [diff] [blame] | 9714 | 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] | 9715 | new_dentry->d_name.name, |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 9716 | new_dentry->d_name.len, 0, index); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 9717 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 9718 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 9719 | goto out_fail; |
| 9720 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9721 | |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 9722 | if (old_inode->i_nlink == 1) |
| 9723 | BTRFS_I(old_inode)->dir_index = index; |
| 9724 | |
Filipe Manana | 3dc9e8f7 | 2016-04-29 11:34:22 +0100 | [diff] [blame] | 9725 | if (log_pinned) { |
Filipe Manana | 75b463d | 2020-08-11 12:43:48 +0100 | [diff] [blame] | 9726 | btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir), |
| 9727 | new_dentry->d_parent); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9728 | btrfs_end_log_trans(root); |
Filipe Manana | 3dc9e8f7 | 2016-04-29 11:34:22 +0100 | [diff] [blame] | 9729 | log_pinned = false; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9730 | } |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9731 | |
| 9732 | if (flags & RENAME_WHITEOUT) { |
| 9733 | ret = btrfs_whiteout_for_rename(trans, root, old_dir, |
| 9734 | old_dentry); |
| 9735 | |
| 9736 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 9737 | btrfs_abort_transaction(trans, ret); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9738 | goto out_fail; |
| 9739 | } |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 9740 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9741 | out_fail: |
Filipe Manana | 3dc9e8f7 | 2016-04-29 11:34:22 +0100 | [diff] [blame] | 9742 | /* |
| 9743 | * If we have pinned the log and an error happened, we unpin tasks |
| 9744 | * trying to sync the log and force them to fallback to a transaction |
| 9745 | * commit if the log currently contains any of the inodes involved in |
| 9746 | * this rename operation (to ensure we do not persist a log with an |
| 9747 | * inconsistent state for any of these inodes or leading to any |
| 9748 | * inconsistencies when replayed). If the transaction was aborted, the |
| 9749 | * abortion reason is propagated to userspace when attempting to commit |
| 9750 | * the transaction. If the log does not contain any of these inodes, we |
| 9751 | * allow the tasks to sync it. |
| 9752 | */ |
| 9753 | if (ret && log_pinned) { |
Nikolay Borisov | 0f8939b | 2017-01-18 00:31:30 +0200 | [diff] [blame] | 9754 | if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) || |
| 9755 | btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) || |
| 9756 | btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) || |
Filipe Manana | 3dc9e8f7 | 2016-04-29 11:34:22 +0100 | [diff] [blame] | 9757 | (new_inode && |
Nikolay Borisov | 0f8939b | 2017-01-18 00:31:30 +0200 | [diff] [blame] | 9758 | btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation))) |
David Sterba | 9078776 | 2019-03-20 13:28:05 +0100 | [diff] [blame] | 9759 | btrfs_set_log_full_commit(trans); |
Filipe Manana | 3dc9e8f7 | 2016-04-29 11:34:22 +0100 | [diff] [blame] | 9760 | |
| 9761 | btrfs_end_log_trans(root); |
| 9762 | log_pinned = false; |
| 9763 | } |
Filipe Manana | 75b463d | 2020-08-11 12:43:48 +0100 | [diff] [blame] | 9764 | ret2 = btrfs_end_transaction(trans); |
| 9765 | ret = ret ? ret : ret2; |
Johann Lombardi | b44c59a | 2011-03-31 13:23:47 +0000 | [diff] [blame] | 9766 | out_notrans: |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 9767 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9768 | up_read(&fs_info->subvol_sem); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 9769 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9770 | return ret; |
| 9771 | } |
| 9772 | |
Christian Brauner | 549c729 | 2021-01-21 14:19:43 +0100 | [diff] [blame] | 9773 | static int btrfs_rename2(struct user_namespace *mnt_userns, struct inode *old_dir, |
| 9774 | struct dentry *old_dentry, struct inode *new_dir, |
| 9775 | struct dentry *new_dentry, unsigned int flags) |
Miklos Szeredi | 80ace85 | 2014-07-23 15:15:32 +0200 | [diff] [blame] | 9776 | { |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9777 | if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT)) |
Miklos Szeredi | 80ace85 | 2014-07-23 15:15:32 +0200 | [diff] [blame] | 9778 | return -EINVAL; |
| 9779 | |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9780 | if (flags & RENAME_EXCHANGE) |
| 9781 | return btrfs_rename_exchange(old_dir, old_dentry, new_dir, |
| 9782 | new_dentry); |
| 9783 | |
| 9784 | return btrfs_rename(old_dir, old_dentry, new_dir, new_dentry, flags); |
Miklos Szeredi | 80ace85 | 2014-07-23 15:15:32 +0200 | [diff] [blame] | 9785 | } |
| 9786 | |
Nikolay Borisov | 3a2f8c0 | 2018-04-24 17:23:59 +0300 | [diff] [blame] | 9787 | struct btrfs_delalloc_work { |
| 9788 | struct inode *inode; |
| 9789 | struct completion completion; |
| 9790 | struct list_head list; |
| 9791 | struct btrfs_work work; |
| 9792 | }; |
| 9793 | |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 9794 | static void btrfs_run_delalloc_work(struct btrfs_work *work) |
| 9795 | { |
| 9796 | struct btrfs_delalloc_work *delalloc_work; |
Josef Bacik | 9f23e28 | 2013-10-28 15:03:41 -0400 | [diff] [blame] | 9797 | struct inode *inode; |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 9798 | |
| 9799 | delalloc_work = container_of(work, struct btrfs_delalloc_work, |
| 9800 | work); |
Josef Bacik | 9f23e28 | 2013-10-28 15:03:41 -0400 | [diff] [blame] | 9801 | inode = delalloc_work->inode; |
David Sterba | 3042460 | 2015-11-27 19:27:11 +0100 | [diff] [blame] | 9802 | filemap_flush(inode->i_mapping); |
| 9803 | if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, |
| 9804 | &BTRFS_I(inode)->runtime_flags)) |
Josef Bacik | 9f23e28 | 2013-10-28 15:03:41 -0400 | [diff] [blame] | 9805 | filemap_flush(inode->i_mapping); |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 9806 | |
Nikolay Borisov | 076da91 | 2018-04-23 10:54:16 +0300 | [diff] [blame] | 9807 | iput(inode); |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 9808 | complete(&delalloc_work->completion); |
| 9809 | } |
| 9810 | |
Nikolay Borisov | 3a2f8c0 | 2018-04-24 17:23:59 +0300 | [diff] [blame] | 9811 | static struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode) |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 9812 | { |
| 9813 | struct btrfs_delalloc_work *work; |
| 9814 | |
David Sterba | 100d570 | 2015-12-08 14:39:32 +0100 | [diff] [blame] | 9815 | work = kmalloc(sizeof(*work), GFP_NOFS); |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 9816 | if (!work) |
| 9817 | return NULL; |
| 9818 | |
| 9819 | init_completion(&work->completion); |
| 9820 | INIT_LIST_HEAD(&work->list); |
| 9821 | work->inode = inode; |
Omar Sandoval | a0cac0e | 2019-09-16 11:30:57 -0700 | [diff] [blame] | 9822 | btrfs_init_work(&work->work, btrfs_run_delalloc_work, NULL, NULL); |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 9823 | |
| 9824 | return work; |
| 9825 | } |
| 9826 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 9827 | /* |
| 9828 | * some fairly slow code that needs optimization. This walks the list |
| 9829 | * of all the inodes with pending delalloc and forces them to disk. |
| 9830 | */ |
Josef Bacik | e076ab2a | 2021-01-07 17:08:30 -0500 | [diff] [blame] | 9831 | static int start_delalloc_inodes(struct btrfs_root *root, |
| 9832 | struct writeback_control *wbc, bool snapshot, |
Filipe Manana | 3d45f22 | 2020-12-02 11:55:58 +0000 | [diff] [blame] | 9833 | bool in_reclaim_context) |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 9834 | { |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 9835 | struct btrfs_inode *binode; |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 9836 | struct inode *inode; |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 9837 | struct btrfs_delalloc_work *work, *next; |
| 9838 | struct list_head works; |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 9839 | struct list_head splice; |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 9840 | int ret = 0; |
Josef Bacik | e076ab2a | 2021-01-07 17:08:30 -0500 | [diff] [blame] | 9841 | bool full_flush = wbc->nr_to_write == LONG_MAX; |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 9842 | |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 9843 | INIT_LIST_HEAD(&works); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 9844 | INIT_LIST_HEAD(&splice); |
Miao Xie | 63607cc | 2013-01-22 10:50:35 +0000 | [diff] [blame] | 9845 | |
Miao Xie | 573bfb7 | 2014-03-06 13:55:03 +0800 | [diff] [blame] | 9846 | mutex_lock(&root->delalloc_mutex); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9847 | spin_lock(&root->delalloc_lock); |
| 9848 | list_splice_init(&root->delalloc_inodes, &splice); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 9849 | while (!list_empty(&splice)) { |
| 9850 | binode = list_entry(splice.next, struct btrfs_inode, |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 9851 | delalloc_inodes); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 9852 | |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9853 | list_move_tail(&binode->delalloc_inodes, |
| 9854 | &root->delalloc_inodes); |
Filipe Manana | 3d45f22 | 2020-12-02 11:55:58 +0000 | [diff] [blame] | 9855 | |
| 9856 | if (in_reclaim_context && |
| 9857 | test_bit(BTRFS_INODE_NO_DELALLOC_FLUSH, &binode->runtime_flags)) |
| 9858 | continue; |
| 9859 | |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 9860 | inode = igrab(&binode->vfs_inode); |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 9861 | if (!inode) { |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9862 | cond_resched_lock(&root->delalloc_lock); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 9863 | continue; |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 9864 | } |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9865 | spin_unlock(&root->delalloc_lock); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 9866 | |
Ethan Lien | 3cd24c6 | 2018-11-01 14:49:03 +0800 | [diff] [blame] | 9867 | if (snapshot) |
| 9868 | set_bit(BTRFS_INODE_SNAPSHOT_FLUSH, |
| 9869 | &binode->runtime_flags); |
Josef Bacik | e076ab2a | 2021-01-07 17:08:30 -0500 | [diff] [blame] | 9870 | if (full_flush) { |
| 9871 | work = btrfs_alloc_delalloc_work(inode); |
| 9872 | if (!work) { |
| 9873 | iput(inode); |
| 9874 | ret = -ENOMEM; |
| 9875 | goto out; |
| 9876 | } |
| 9877 | list_add_tail(&work->list, &works); |
| 9878 | btrfs_queue_work(root->fs_info->flush_workers, |
| 9879 | &work->work); |
| 9880 | } else { |
Josef Bacik | b377630 | 2021-07-14 14:47:23 -0400 | [diff] [blame] | 9881 | ret = filemap_fdatawrite_wbc(inode->i_mapping, wbc); |
Josef Bacik | e076ab2a | 2021-01-07 17:08:30 -0500 | [diff] [blame] | 9882 | btrfs_add_delayed_iput(inode); |
| 9883 | if (ret || wbc->nr_to_write <= 0) |
Josef Bacik | b491213 | 2020-07-21 10:22:12 -0400 | [diff] [blame] | 9884 | goto out; |
| 9885 | } |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 9886 | cond_resched(); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9887 | spin_lock(&root->delalloc_lock); |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 9888 | } |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9889 | spin_unlock(&root->delalloc_lock); |
Chris Mason | 8c8bee1 | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 9890 | |
Wang Shilong | a1ecaab | 2014-04-02 19:53:32 +0800 | [diff] [blame] | 9891 | out: |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 9892 | list_for_each_entry_safe(work, next, &works, list) { |
| 9893 | list_del_init(&work->list); |
Nikolay Borisov | 40012f9 | 2018-04-19 10:46:39 +0300 | [diff] [blame] | 9894 | wait_for_completion(&work->completion); |
| 9895 | kfree(work); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 9896 | } |
| 9897 | |
Nikolay Borisov | 81f1d39 | 2018-04-19 10:46:37 +0300 | [diff] [blame] | 9898 | if (!list_empty(&splice)) { |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9899 | spin_lock(&root->delalloc_lock); |
| 9900 | list_splice_tail(&splice, &root->delalloc_inodes); |
| 9901 | spin_unlock(&root->delalloc_lock); |
| 9902 | } |
Miao Xie | 573bfb7 | 2014-03-06 13:55:03 +0800 | [diff] [blame] | 9903 | mutex_unlock(&root->delalloc_mutex); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9904 | return ret; |
| 9905 | } |
| 9906 | |
Filipe Manana | f9baa50 | 2021-04-22 12:08:05 +0100 | [diff] [blame] | 9907 | 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] | 9908 | { |
Josef Bacik | e076ab2a | 2021-01-07 17:08:30 -0500 | [diff] [blame] | 9909 | struct writeback_control wbc = { |
| 9910 | .nr_to_write = LONG_MAX, |
| 9911 | .sync_mode = WB_SYNC_NONE, |
| 9912 | .range_start = 0, |
| 9913 | .range_end = LLONG_MAX, |
| 9914 | }; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9915 | struct btrfs_fs_info *fs_info = root->fs_info; |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9916 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9917 | if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9918 | return -EROFS; |
| 9919 | |
Filipe Manana | f9baa50 | 2021-04-22 12:08:05 +0100 | [diff] [blame] | 9920 | return start_delalloc_inodes(root, &wbc, true, in_reclaim_context); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9921 | } |
| 9922 | |
Nikolay Borisov | 9db4dc2 | 2021-01-11 12:58:11 +0200 | [diff] [blame] | 9923 | 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] | 9924 | bool in_reclaim_context) |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9925 | { |
Josef Bacik | e076ab2a | 2021-01-07 17:08:30 -0500 | [diff] [blame] | 9926 | struct writeback_control wbc = { |
Nikolay Borisov | 9db4dc2 | 2021-01-11 12:58:11 +0200 | [diff] [blame] | 9927 | .nr_to_write = nr, |
Josef Bacik | e076ab2a | 2021-01-07 17:08:30 -0500 | [diff] [blame] | 9928 | .sync_mode = WB_SYNC_NONE, |
| 9929 | .range_start = 0, |
| 9930 | .range_end = LLONG_MAX, |
| 9931 | }; |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9932 | struct btrfs_root *root; |
| 9933 | struct list_head splice; |
| 9934 | int ret; |
| 9935 | |
Wang Shilong | 2c21b4d | 2014-01-14 19:42:20 +0800 | [diff] [blame] | 9936 | if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9937 | return -EROFS; |
| 9938 | |
| 9939 | INIT_LIST_HEAD(&splice); |
| 9940 | |
Miao Xie | 573bfb7 | 2014-03-06 13:55:03 +0800 | [diff] [blame] | 9941 | mutex_lock(&fs_info->delalloc_root_mutex); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9942 | spin_lock(&fs_info->delalloc_root_lock); |
| 9943 | list_splice_init(&fs_info->delalloc_roots, &splice); |
Nikolay Borisov | d7830b7 | 2021-01-11 12:58:12 +0200 | [diff] [blame] | 9944 | while (!list_empty(&splice)) { |
Josef Bacik | e076ab2a | 2021-01-07 17:08:30 -0500 | [diff] [blame] | 9945 | /* |
| 9946 | * Reset nr_to_write here so we know that we're doing a full |
| 9947 | * flush. |
| 9948 | */ |
Nikolay Borisov | 9db4dc2 | 2021-01-11 12:58:11 +0200 | [diff] [blame] | 9949 | if (nr == LONG_MAX) |
Josef Bacik | e076ab2a | 2021-01-07 17:08:30 -0500 | [diff] [blame] | 9950 | wbc.nr_to_write = LONG_MAX; |
| 9951 | |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9952 | root = list_first_entry(&splice, struct btrfs_root, |
| 9953 | delalloc_root); |
Josef Bacik | 0024652 | 2020-01-24 09:33:01 -0500 | [diff] [blame] | 9954 | root = btrfs_grab_root(root); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9955 | BUG_ON(!root); |
| 9956 | list_move_tail(&root->delalloc_root, |
| 9957 | &fs_info->delalloc_roots); |
| 9958 | spin_unlock(&fs_info->delalloc_root_lock); |
| 9959 | |
Josef Bacik | e076ab2a | 2021-01-07 17:08:30 -0500 | [diff] [blame] | 9960 | ret = start_delalloc_inodes(root, &wbc, false, in_reclaim_context); |
Josef Bacik | 0024652 | 2020-01-24 09:33:01 -0500 | [diff] [blame] | 9961 | btrfs_put_root(root); |
Josef Bacik | e076ab2a | 2021-01-07 17:08:30 -0500 | [diff] [blame] | 9962 | if (ret < 0 || wbc.nr_to_write <= 0) |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9963 | goto out; |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9964 | spin_lock(&fs_info->delalloc_root_lock); |
| 9965 | } |
| 9966 | spin_unlock(&fs_info->delalloc_root_lock); |
| 9967 | |
Miao Xie | 6c255e6 | 2014-03-06 13:55:01 +0800 | [diff] [blame] | 9968 | ret = 0; |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 9969 | out: |
Nikolay Borisov | 81f1d39 | 2018-04-19 10:46:37 +0300 | [diff] [blame] | 9970 | if (!list_empty(&splice)) { |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9971 | spin_lock(&fs_info->delalloc_root_lock); |
| 9972 | list_splice_tail(&splice, &fs_info->delalloc_roots); |
| 9973 | spin_unlock(&fs_info->delalloc_root_lock); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 9974 | } |
Miao Xie | 573bfb7 | 2014-03-06 13:55:03 +0800 | [diff] [blame] | 9975 | mutex_unlock(&fs_info->delalloc_root_mutex); |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 9976 | return ret; |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 9977 | } |
| 9978 | |
Christian Brauner | 549c729 | 2021-01-21 14:19:43 +0100 | [diff] [blame] | 9979 | static int btrfs_symlink(struct user_namespace *mnt_userns, struct inode *dir, |
| 9980 | struct dentry *dentry, const char *symname) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9981 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9982 | struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9983 | struct btrfs_trans_handle *trans; |
| 9984 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 9985 | struct btrfs_path *path; |
| 9986 | struct btrfs_key key; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 9987 | struct inode *inode = NULL; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9988 | int err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9989 | u64 objectid; |
Dulshani Gunawardhana | 6787125 | 2013-10-31 10:33:04 +0530 | [diff] [blame] | 9990 | u64 index = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9991 | int name_len; |
| 9992 | int datasize; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 9993 | unsigned long ptr; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9994 | struct btrfs_file_extent_item *ei; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 9995 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9996 | |
Filipe David Borba Manana | f06becc | 2013-09-16 09:53:28 +0100 | [diff] [blame] | 9997 | name_len = strlen(symname); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9998 | if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info)) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9999 | return -ENAMETOOLONG; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 10000 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 10001 | /* |
| 10002 | * 2 items for inode item and ref |
| 10003 | * 2 items for dir items |
Filipe Manana | 9269d12 | 2015-12-31 18:16:29 +0000 | [diff] [blame] | 10004 | * 1 item for updating parent inode item |
| 10005 | * 1 item for the inline extent item |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 10006 | * 1 item for xattr if selinux is on |
| 10007 | */ |
Filipe Manana | 9269d12 | 2015-12-31 18:16:29 +0000 | [diff] [blame] | 10008 | trans = btrfs_start_transaction(root, 7); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 10009 | if (IS_ERR(trans)) |
| 10010 | return PTR_ERR(trans); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 10011 | |
Nikolay Borisov | 543068a | 2020-12-07 17:32:33 +0200 | [diff] [blame] | 10012 | err = btrfs_get_free_objectid(root, &objectid); |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 10013 | if (err) |
| 10014 | goto out_unlock; |
| 10015 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 10016 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 10017 | dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), |
| 10018 | objectid, S_IFLNK|S_IRWXUGO, &index); |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 10019 | if (IS_ERR(inode)) { |
| 10020 | err = PTR_ERR(inode); |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 10021 | inode = NULL; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10022 | goto out_unlock; |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 10023 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10024 | |
Casey Schaufler | ad19db7 | 2011-12-15 10:09:07 -0500 | [diff] [blame] | 10025 | /* |
| 10026 | * If the active LSM wants to access the inode during |
| 10027 | * d_instantiate it needs these. Smack checks to see |
| 10028 | * if the filesystem supports xattrs by looking at the |
| 10029 | * ops vector. |
| 10030 | */ |
| 10031 | inode->i_fop = &btrfs_file_operations; |
| 10032 | inode->i_op = &btrfs_file_inode_operations; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 10033 | inode->i_mapping->a_ops = &btrfs_aops; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 10034 | |
| 10035 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); |
| 10036 | if (err) |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 10037 | goto out_unlock; |
Casey Schaufler | ad19db7 | 2011-12-15 10:09:07 -0500 | [diff] [blame] | 10038 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10039 | path = btrfs_alloc_path(); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 10040 | if (!path) { |
| 10041 | err = -ENOMEM; |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 10042 | goto out_unlock; |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 10043 | } |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 10044 | key.objectid = btrfs_ino(BTRFS_I(inode)); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10045 | key.offset = 0; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 10046 | key.type = BTRFS_EXTENT_DATA_KEY; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10047 | datasize = btrfs_file_extent_calc_inline_size(name_len); |
| 10048 | err = btrfs_insert_empty_item(trans, root, path, &key, |
| 10049 | datasize); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 10050 | if (err) { |
Julia Lawall | b083916 | 2011-05-14 07:10:51 +0000 | [diff] [blame] | 10051 | btrfs_free_path(path); |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 10052 | goto out_unlock; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 10053 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 10054 | leaf = path->nodes[0]; |
| 10055 | ei = btrfs_item_ptr(leaf, path->slots[0], |
| 10056 | struct btrfs_file_extent_item); |
| 10057 | btrfs_set_file_extent_generation(leaf, ei, trans->transid); |
| 10058 | btrfs_set_file_extent_type(leaf, ei, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10059 | BTRFS_FILE_EXTENT_INLINE); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 10060 | btrfs_set_file_extent_encryption(leaf, ei, 0); |
| 10061 | btrfs_set_file_extent_compression(leaf, ei, 0); |
| 10062 | btrfs_set_file_extent_other_encoding(leaf, ei, 0); |
| 10063 | btrfs_set_file_extent_ram_bytes(leaf, ei, name_len); |
| 10064 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10065 | ptr = btrfs_file_extent_inline_start(ei); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 10066 | write_extent_buffer(leaf, symname, ptr, name_len); |
| 10067 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10068 | btrfs_free_path(path); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 10069 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10070 | inode->i_op = &btrfs_symlink_inode_operations; |
Al Viro | 21fc61c | 2015-11-17 01:07:57 -0500 | [diff] [blame] | 10071 | inode_nohighmem(inode); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 10072 | inode_set_bytes(inode, name_len); |
Nikolay Borisov | 6ef06d2 | 2017-02-20 13:50:34 +0200 | [diff] [blame] | 10073 | btrfs_i_size_write(BTRFS_I(inode), name_len); |
Nikolay Borisov | 9a56fcd | 2020-11-02 16:48:59 +0200 | [diff] [blame] | 10074 | err = btrfs_update_inode(trans, root, BTRFS_I(inode)); |
Filipe Manana | d50866d | 2015-12-31 18:08:24 +0000 | [diff] [blame] | 10075 | /* |
| 10076 | * Last step, add directory indexes for our symlink inode. This is the |
| 10077 | * last step to avoid extra cleanup of these indexes if an error happens |
| 10078 | * elsewhere above. |
| 10079 | */ |
| 10080 | if (!err) |
Nikolay Borisov | cef415a | 2017-02-20 13:51:09 +0200 | [diff] [blame] | 10081 | err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, |
| 10082 | BTRFS_I(inode), 0, index); |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 10083 | if (err) |
| 10084 | goto out_unlock; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 10085 | |
Al Viro | 1e2e547 | 2018-05-04 08:23:01 -0400 | [diff] [blame] | 10086 | d_instantiate_new(dentry, inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10087 | |
| 10088 | out_unlock: |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 10089 | btrfs_end_transaction(trans); |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 10090 | if (err && inode) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10091 | inode_dec_link_count(inode); |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 10092 | discard_new_inode(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10093 | } |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 10094 | btrfs_btree_balance_dirty(fs_info); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10095 | return err; |
| 10096 | } |
Chris Mason | 1643298 | 2008-04-10 10:23:21 -0400 | [diff] [blame] | 10097 | |
Filipe Manana | 8fccebf | 2020-09-08 11:27:20 +0100 | [diff] [blame] | 10098 | static struct btrfs_trans_handle *insert_prealloc_file_extent( |
| 10099 | struct btrfs_trans_handle *trans_in, |
Nikolay Borisov | 90dffd0 | 2020-11-02 16:48:54 +0200 | [diff] [blame] | 10100 | struct btrfs_inode *inode, |
| 10101 | struct btrfs_key *ins, |
Qu Wenruo | 203f44c5 | 2020-06-10 09:04:40 +0800 | [diff] [blame] | 10102 | u64 file_offset) |
| 10103 | { |
| 10104 | struct btrfs_file_extent_item stack_fi; |
Filipe Manana | bf38564 | 2020-09-08 11:27:22 +0100 | [diff] [blame] | 10105 | struct btrfs_replace_extent_info extent_info; |
Filipe Manana | 8fccebf | 2020-09-08 11:27:20 +0100 | [diff] [blame] | 10106 | struct btrfs_trans_handle *trans = trans_in; |
| 10107 | struct btrfs_path *path; |
Qu Wenruo | 203f44c5 | 2020-06-10 09:04:40 +0800 | [diff] [blame] | 10108 | u64 start = ins->objectid; |
| 10109 | u64 len = ins->offset; |
Qu Wenruo | fbf48bb | 2021-03-03 18:41:51 +0800 | [diff] [blame] | 10110 | int qgroup_released; |
Qu Wenruo | 9729f10 | 2020-06-10 09:04:41 +0800 | [diff] [blame] | 10111 | int ret; |
Qu Wenruo | 203f44c5 | 2020-06-10 09:04:40 +0800 | [diff] [blame] | 10112 | |
| 10113 | memset(&stack_fi, 0, sizeof(stack_fi)); |
| 10114 | |
| 10115 | btrfs_set_stack_file_extent_type(&stack_fi, BTRFS_FILE_EXTENT_PREALLOC); |
| 10116 | btrfs_set_stack_file_extent_disk_bytenr(&stack_fi, start); |
| 10117 | btrfs_set_stack_file_extent_disk_num_bytes(&stack_fi, len); |
| 10118 | btrfs_set_stack_file_extent_num_bytes(&stack_fi, len); |
| 10119 | btrfs_set_stack_file_extent_ram_bytes(&stack_fi, len); |
| 10120 | btrfs_set_stack_file_extent_compression(&stack_fi, BTRFS_COMPRESS_NONE); |
| 10121 | /* Encryption and other encoding is reserved and all 0 */ |
| 10122 | |
Qu Wenruo | fbf48bb | 2021-03-03 18:41:51 +0800 | [diff] [blame] | 10123 | qgroup_released = btrfs_qgroup_release_data(inode, file_offset, len); |
| 10124 | if (qgroup_released < 0) |
| 10125 | return ERR_PTR(qgroup_released); |
Filipe Manana | 8fccebf | 2020-09-08 11:27:20 +0100 | [diff] [blame] | 10126 | |
| 10127 | if (trans) { |
Nikolay Borisov | 90dffd0 | 2020-11-02 16:48:54 +0200 | [diff] [blame] | 10128 | ret = insert_reserved_file_extent(trans, inode, |
Filipe Manana | 2766ff6 | 2020-11-04 11:07:34 +0000 | [diff] [blame] | 10129 | file_offset, &stack_fi, |
Qu Wenruo | fbf48bb | 2021-03-03 18:41:51 +0800 | [diff] [blame] | 10130 | true, qgroup_released); |
Filipe Manana | 8fccebf | 2020-09-08 11:27:20 +0100 | [diff] [blame] | 10131 | if (ret) |
Qu Wenruo | a3ee79b | 2021-03-03 18:41:52 +0800 | [diff] [blame] | 10132 | goto free_qgroup; |
Filipe Manana | 8fccebf | 2020-09-08 11:27:20 +0100 | [diff] [blame] | 10133 | return trans; |
| 10134 | } |
| 10135 | |
| 10136 | extent_info.disk_offset = start; |
| 10137 | extent_info.disk_len = len; |
| 10138 | extent_info.data_offset = 0; |
| 10139 | extent_info.data_len = len; |
| 10140 | extent_info.file_offset = file_offset; |
| 10141 | extent_info.extent_buf = (char *)&stack_fi; |
Filipe Manana | 8fccebf | 2020-09-08 11:27:20 +0100 | [diff] [blame] | 10142 | extent_info.is_new_extent = true; |
Qu Wenruo | fbf48bb | 2021-03-03 18:41:51 +0800 | [diff] [blame] | 10143 | extent_info.qgroup_reserved = qgroup_released; |
Filipe Manana | 8fccebf | 2020-09-08 11:27:20 +0100 | [diff] [blame] | 10144 | extent_info.insertions = 0; |
| 10145 | |
| 10146 | path = btrfs_alloc_path(); |
Qu Wenruo | a3ee79b | 2021-03-03 18:41:52 +0800 | [diff] [blame] | 10147 | if (!path) { |
| 10148 | ret = -ENOMEM; |
| 10149 | goto free_qgroup; |
| 10150 | } |
Filipe Manana | 8fccebf | 2020-09-08 11:27:20 +0100 | [diff] [blame] | 10151 | |
Nikolay Borisov | bfc7847 | 2021-02-17 15:12:47 +0200 | [diff] [blame] | 10152 | ret = btrfs_replace_file_extents(inode, path, file_offset, |
Filipe Manana | 8fccebf | 2020-09-08 11:27:20 +0100 | [diff] [blame] | 10153 | file_offset + len - 1, &extent_info, |
| 10154 | &trans); |
| 10155 | btrfs_free_path(path); |
| 10156 | if (ret) |
Qu Wenruo | a3ee79b | 2021-03-03 18:41:52 +0800 | [diff] [blame] | 10157 | goto free_qgroup; |
Filipe Manana | 8fccebf | 2020-09-08 11:27:20 +0100 | [diff] [blame] | 10158 | return trans; |
Qu Wenruo | a3ee79b | 2021-03-03 18:41:52 +0800 | [diff] [blame] | 10159 | |
| 10160 | free_qgroup: |
| 10161 | /* |
| 10162 | * We have released qgroup data range at the beginning of the function, |
| 10163 | * and normally qgroup_released bytes will be freed when committing |
| 10164 | * transaction. |
| 10165 | * But if we error out early, we have to free what we have released |
| 10166 | * or we leak qgroup data reservation. |
| 10167 | */ |
| 10168 | btrfs_qgroup_free_refroot(inode->root->fs_info, |
| 10169 | inode->root->root_key.objectid, qgroup_released, |
| 10170 | BTRFS_QGROUP_RSV_DATA); |
| 10171 | return ERR_PTR(ret); |
Qu Wenruo | 203f44c5 | 2020-06-10 09:04:40 +0800 | [diff] [blame] | 10172 | } |
Filipe Manana | 8fccebf | 2020-09-08 11:27:20 +0100 | [diff] [blame] | 10173 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 10174 | static int __btrfs_prealloc_file_range(struct inode *inode, int mode, |
| 10175 | u64 start, u64 num_bytes, u64 min_size, |
| 10176 | loff_t actual_len, u64 *alloc_hint, |
| 10177 | struct btrfs_trans_handle *trans) |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 10178 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 10179 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 10180 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
| 10181 | struct extent_map *em; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 10182 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 10183 | struct btrfs_key ins; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 10184 | u64 cur_offset = start; |
Josef Bacik | b778cf9 | 2020-02-13 10:47:31 -0500 | [diff] [blame] | 10185 | u64 clear_offset = start; |
Josef Bacik | 55a61d1 | 2010-11-22 18:50:32 +0000 | [diff] [blame] | 10186 | u64 i_size; |
Chris Mason | 154ea28 | 2013-03-05 11:11:26 -0500 | [diff] [blame] | 10187 | u64 cur_bytes; |
Josef Bacik | 0b670dc | 2015-09-23 17:11:16 -0400 | [diff] [blame] | 10188 | u64 last_alloc = (u64)-1; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 10189 | int ret = 0; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 10190 | bool own_trans = true; |
Wang Xiaoguang | 1851309 | 2016-07-25 15:51:40 +0800 | [diff] [blame] | 10191 | u64 end = start + num_bytes - 1; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 10192 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 10193 | if (trans) |
| 10194 | own_trans = false; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 10195 | while (num_bytes > 0) { |
Byongho Lee | ee22184 | 2015-12-15 01:42:10 +0900 | [diff] [blame] | 10196 | cur_bytes = min_t(u64, num_bytes, SZ_256M); |
Chris Mason | 154ea28 | 2013-03-05 11:11:26 -0500 | [diff] [blame] | 10197 | cur_bytes = max(cur_bytes, min_size); |
Josef Bacik | 0b670dc | 2015-09-23 17:11:16 -0400 | [diff] [blame] | 10198 | /* |
| 10199 | * If we are severely fragmented we could end up with really |
| 10200 | * small allocations, so if the allocator is returning small |
| 10201 | * chunks lets make its job easier by only searching for those |
| 10202 | * sized chunks. |
| 10203 | */ |
| 10204 | cur_bytes = min(cur_bytes, last_alloc); |
Wang Xiaoguang | 1851309 | 2016-07-25 15:51:40 +0800 | [diff] [blame] | 10205 | ret = btrfs_reserve_extent(root, cur_bytes, cur_bytes, |
| 10206 | min_size, 0, *alloc_hint, &ins, 1, 0); |
Filipe Manana | 8fccebf | 2020-09-08 11:27:20 +0100 | [diff] [blame] | 10207 | if (ret) |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 10208 | break; |
Josef Bacik | b778cf9 | 2020-02-13 10:47:31 -0500 | [diff] [blame] | 10209 | |
| 10210 | /* |
| 10211 | * We've reserved this space, and thus converted it from |
| 10212 | * ->bytes_may_use to ->bytes_reserved. Any error that happens |
| 10213 | * from here on out we will only need to clear our reservation |
| 10214 | * for the remaining unreserved area, so advance our |
| 10215 | * clear_offset by our extent size. |
| 10216 | */ |
| 10217 | clear_offset += ins.offset; |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 10218 | |
Josef Bacik | 0b670dc | 2015-09-23 17:11:16 -0400 | [diff] [blame] | 10219 | last_alloc = ins.offset; |
Nikolay Borisov | 90dffd0 | 2020-11-02 16:48:54 +0200 | [diff] [blame] | 10220 | trans = insert_prealloc_file_extent(trans, BTRFS_I(inode), |
| 10221 | &ins, cur_offset); |
Filipe Manana | 1afc708 | 2020-10-14 10:10:36 +0100 | [diff] [blame] | 10222 | /* |
| 10223 | * Now that we inserted the prealloc extent we can finally |
| 10224 | * decrement the number of reservations in the block group. |
| 10225 | * If we did it before, we could race with relocation and have |
| 10226 | * relocation miss the reserved extent, making it fail later. |
| 10227 | */ |
| 10228 | btrfs_dec_block_group_reservations(fs_info, ins.objectid); |
Filipe Manana | 8fccebf | 2020-09-08 11:27:20 +0100 | [diff] [blame] | 10229 | if (IS_ERR(trans)) { |
| 10230 | ret = PTR_ERR(trans); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 10231 | btrfs_free_reserved_extent(fs_info, ins.objectid, |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 10232 | ins.offset, 0); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 10233 | break; |
| 10234 | } |
Dongsheng Yang | 3119321 | 2014-12-12 16:44:35 +0800 | [diff] [blame] | 10235 | |
Nikolay Borisov | dcdbc05 | 2017-02-20 13:50:45 +0200 | [diff] [blame] | 10236 | btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset, |
Chris Mason | a1ed835 | 2009-09-11 12:27:37 -0400 | [diff] [blame] | 10237 | cur_offset + ins.offset -1, 0); |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 10238 | |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 10239 | em = alloc_extent_map(); |
| 10240 | if (!em) { |
| 10241 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, |
| 10242 | &BTRFS_I(inode)->runtime_flags); |
| 10243 | goto next; |
| 10244 | } |
| 10245 | |
| 10246 | em->start = cur_offset; |
| 10247 | em->orig_start = cur_offset; |
| 10248 | em->len = ins.offset; |
| 10249 | em->block_start = ins.objectid; |
| 10250 | em->block_len = ins.offset; |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 10251 | em->orig_block_len = ins.offset; |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 10252 | em->ram_bytes = ins.offset; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 10253 | set_bit(EXTENT_FLAG_PREALLOC, &em->flags); |
| 10254 | em->generation = trans->transid; |
| 10255 | |
| 10256 | while (1) { |
| 10257 | write_lock(&em_tree->lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 10258 | ret = add_extent_mapping(em_tree, em, 1); |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 10259 | write_unlock(&em_tree->lock); |
| 10260 | if (ret != -EEXIST) |
| 10261 | break; |
Nikolay Borisov | dcdbc05 | 2017-02-20 13:50:45 +0200 | [diff] [blame] | 10262 | btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset, |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 10263 | cur_offset + ins.offset - 1, |
| 10264 | 0); |
| 10265 | } |
| 10266 | free_extent_map(em); |
| 10267 | next: |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 10268 | num_bytes -= ins.offset; |
| 10269 | cur_offset += ins.offset; |
Yan, Zheng | efa5646 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 10270 | *alloc_hint = ins.objectid + ins.offset; |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 10271 | |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 10272 | inode_inc_iversion(inode); |
Deepa Dinamani | c2050a4 | 2016-09-14 07:48:06 -0700 | [diff] [blame] | 10273 | inode->i_ctime = current_time(inode); |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 10274 | BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 10275 | if (!(mode & FALLOC_FL_KEEP_SIZE) && |
Yan, Zheng | efa5646 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 10276 | (actual_len > inode->i_size) && |
| 10277 | (cur_offset > inode->i_size)) { |
Aneesh Kumar K.V | d1ea6a6 | 2010-01-20 07:28:54 +0000 | [diff] [blame] | 10278 | if (cur_offset > actual_len) |
Josef Bacik | 55a61d1 | 2010-11-22 18:50:32 +0000 | [diff] [blame] | 10279 | i_size = actual_len; |
Aneesh Kumar K.V | d1ea6a6 | 2010-01-20 07:28:54 +0000 | [diff] [blame] | 10280 | else |
Josef Bacik | 55a61d1 | 2010-11-22 18:50:32 +0000 | [diff] [blame] | 10281 | i_size = cur_offset; |
| 10282 | i_size_write(inode, i_size); |
Nikolay Borisov | 76aea53 | 2020-11-02 16:48:53 +0200 | [diff] [blame] | 10283 | btrfs_inode_safe_disk_i_size_write(BTRFS_I(inode), 0); |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 10284 | } |
| 10285 | |
Nikolay Borisov | 9a56fcd | 2020-11-02 16:48:59 +0200 | [diff] [blame] | 10286 | ret = btrfs_update_inode(trans, root, BTRFS_I(inode)); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 10287 | |
| 10288 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 10289 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 10290 | if (own_trans) |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 10291 | btrfs_end_transaction(trans); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 10292 | break; |
| 10293 | } |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 10294 | |
Filipe Manana | 8fccebf | 2020-09-08 11:27:20 +0100 | [diff] [blame] | 10295 | if (own_trans) { |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 10296 | btrfs_end_transaction(trans); |
Filipe Manana | 8fccebf | 2020-09-08 11:27:20 +0100 | [diff] [blame] | 10297 | trans = NULL; |
| 10298 | } |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 10299 | } |
Josef Bacik | b778cf9 | 2020-02-13 10:47:31 -0500 | [diff] [blame] | 10300 | if (clear_offset < end) |
Nikolay Borisov | 25ce28c | 2020-06-03 08:55:39 +0300 | [diff] [blame] | 10301 | btrfs_free_reserved_data_space(BTRFS_I(inode), NULL, clear_offset, |
Josef Bacik | b778cf9 | 2020-02-13 10:47:31 -0500 | [diff] [blame] | 10302 | end - clear_offset + 1); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 10303 | return ret; |
| 10304 | } |
| 10305 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 10306 | int btrfs_prealloc_file_range(struct inode *inode, int mode, |
| 10307 | u64 start, u64 num_bytes, u64 min_size, |
| 10308 | loff_t actual_len, u64 *alloc_hint) |
| 10309 | { |
| 10310 | return __btrfs_prealloc_file_range(inode, mode, start, num_bytes, |
| 10311 | min_size, actual_len, alloc_hint, |
| 10312 | NULL); |
| 10313 | } |
| 10314 | |
| 10315 | int btrfs_prealloc_file_range_trans(struct inode *inode, |
| 10316 | struct btrfs_trans_handle *trans, int mode, |
| 10317 | u64 start, u64 num_bytes, u64 min_size, |
| 10318 | loff_t actual_len, u64 *alloc_hint) |
| 10319 | { |
| 10320 | return __btrfs_prealloc_file_range(inode, mode, start, num_bytes, |
| 10321 | min_size, actual_len, alloc_hint, trans); |
| 10322 | } |
| 10323 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 10324 | static int btrfs_set_page_dirty(struct page *page) |
| 10325 | { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 10326 | return __set_page_dirty_nobuffers(page); |
| 10327 | } |
| 10328 | |
Christian Brauner | 549c729 | 2021-01-21 14:19:43 +0100 | [diff] [blame] | 10329 | static int btrfs_permission(struct user_namespace *mnt_userns, |
| 10330 | struct inode *inode, int mask) |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 10331 | { |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 10332 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Jeff Mahoney | cb6db4e | 2011-08-15 17:27:21 +0000 | [diff] [blame] | 10333 | umode_t mode = inode->i_mode; |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 10334 | |
Jeff Mahoney | cb6db4e | 2011-08-15 17:27:21 +0000 | [diff] [blame] | 10335 | if (mask & MAY_WRITE && |
| 10336 | (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) { |
| 10337 | if (btrfs_root_readonly(root)) |
| 10338 | return -EROFS; |
| 10339 | if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY) |
| 10340 | return -EACCES; |
| 10341 | } |
Christian Brauner | 47291ba | 2021-01-21 14:19:24 +0100 | [diff] [blame] | 10342 | return generic_permission(&init_user_ns, inode, mask); |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 10343 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10344 | |
Christian Brauner | 549c729 | 2021-01-21 14:19:43 +0100 | [diff] [blame] | 10345 | static int btrfs_tmpfile(struct user_namespace *mnt_userns, struct inode *dir, |
| 10346 | struct dentry *dentry, umode_t mode) |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 10347 | { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 10348 | struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 10349 | struct btrfs_trans_handle *trans; |
| 10350 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 10351 | struct inode *inode = NULL; |
| 10352 | u64 objectid; |
| 10353 | u64 index; |
| 10354 | int ret = 0; |
| 10355 | |
| 10356 | /* |
| 10357 | * 5 units required for adding orphan entry |
| 10358 | */ |
| 10359 | trans = btrfs_start_transaction(root, 5); |
| 10360 | if (IS_ERR(trans)) |
| 10361 | return PTR_ERR(trans); |
| 10362 | |
Nikolay Borisov | 543068a | 2020-12-07 17:32:33 +0200 | [diff] [blame] | 10363 | ret = btrfs_get_free_objectid(root, &objectid); |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 10364 | if (ret) |
| 10365 | goto out; |
| 10366 | |
| 10367 | inode = btrfs_new_inode(trans, root, dir, NULL, 0, |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 10368 | btrfs_ino(BTRFS_I(dir)), objectid, mode, &index); |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 10369 | if (IS_ERR(inode)) { |
| 10370 | ret = PTR_ERR(inode); |
| 10371 | inode = NULL; |
| 10372 | goto out; |
| 10373 | } |
| 10374 | |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 10375 | inode->i_fop = &btrfs_file_operations; |
| 10376 | inode->i_op = &btrfs_file_inode_operations; |
| 10377 | |
| 10378 | inode->i_mapping->a_ops = &btrfs_aops; |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 10379 | |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 10380 | ret = btrfs_init_inode_security(trans, inode, dir, NULL); |
| 10381 | if (ret) |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 10382 | goto out; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 10383 | |
Nikolay Borisov | 9a56fcd | 2020-11-02 16:48:59 +0200 | [diff] [blame] | 10384 | ret = btrfs_update_inode(trans, root, BTRFS_I(inode)); |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 10385 | if (ret) |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 10386 | goto out; |
Nikolay Borisov | 73f2e54 | 2017-02-20 13:50:59 +0200 | [diff] [blame] | 10387 | ret = btrfs_orphan_add(trans, BTRFS_I(inode)); |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 10388 | if (ret) |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 10389 | goto out; |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 10390 | |
Filipe Manana | 5762b5c | 2014-08-01 00:10:32 +0100 | [diff] [blame] | 10391 | /* |
| 10392 | * We set number of links to 0 in btrfs_new_inode(), and here we set |
| 10393 | * it to 1 because d_tmpfile() will issue a warning if the count is 0, |
| 10394 | * through: |
| 10395 | * |
| 10396 | * d_tmpfile() -> inode_dec_link_count() -> drop_nlink() |
| 10397 | */ |
| 10398 | set_nlink(inode, 1); |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 10399 | d_tmpfile(dentry, inode); |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 10400 | unlock_new_inode(inode); |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 10401 | mark_inode_dirty(inode); |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 10402 | out: |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 10403 | btrfs_end_transaction(trans); |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 10404 | if (ret && inode) |
| 10405 | discard_new_inode(inode); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 10406 | btrfs_btree_balance_dirty(fs_info); |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 10407 | return ret; |
| 10408 | } |
| 10409 | |
Qu Wenruo | d2a9106 | 2021-05-31 16:50:49 +0800 | [diff] [blame] | 10410 | 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] | 10411 | { |
Qu Wenruo | d2a9106 | 2021-05-31 16:50:49 +0800 | [diff] [blame] | 10412 | struct btrfs_fs_info *fs_info = inode->root->fs_info; |
Josef Bacik | c6100a4 | 2017-05-05 11:57:13 -0400 | [diff] [blame] | 10413 | unsigned long index = start >> PAGE_SHIFT; |
| 10414 | unsigned long end_index = end >> PAGE_SHIFT; |
| 10415 | struct page *page; |
Qu Wenruo | d2a9106 | 2021-05-31 16:50:49 +0800 | [diff] [blame] | 10416 | u32 len; |
Josef Bacik | c6100a4 | 2017-05-05 11:57:13 -0400 | [diff] [blame] | 10417 | |
Qu Wenruo | d2a9106 | 2021-05-31 16:50:49 +0800 | [diff] [blame] | 10418 | ASSERT(end + 1 - start <= U32_MAX); |
| 10419 | len = end + 1 - start; |
Josef Bacik | c6100a4 | 2017-05-05 11:57:13 -0400 | [diff] [blame] | 10420 | while (index <= end_index) { |
Qu Wenruo | d2a9106 | 2021-05-31 16:50:49 +0800 | [diff] [blame] | 10421 | page = find_get_page(inode->vfs_inode.i_mapping, index); |
Josef Bacik | c6100a4 | 2017-05-05 11:57:13 -0400 | [diff] [blame] | 10422 | ASSERT(page); /* Pages should be in the extent_io_tree */ |
Qu Wenruo | d2a9106 | 2021-05-31 16:50:49 +0800 | [diff] [blame] | 10423 | |
| 10424 | btrfs_page_set_writeback(fs_info, page, start, len); |
Josef Bacik | c6100a4 | 2017-05-05 11:57:13 -0400 | [diff] [blame] | 10425 | put_page(page); |
| 10426 | index++; |
| 10427 | } |
| 10428 | } |
| 10429 | |
Omar Sandoval | ed46ff3 | 2016-11-03 10:28:14 -0700 | [diff] [blame] | 10430 | #ifdef CONFIG_SWAP |
| 10431 | /* |
| 10432 | * Add an entry indicating a block group or device which is pinned by a |
| 10433 | * swapfile. Returns 0 on success, 1 if there is already an entry for it, or a |
| 10434 | * negative errno on failure. |
| 10435 | */ |
| 10436 | static int btrfs_add_swapfile_pin(struct inode *inode, void *ptr, |
| 10437 | bool is_block_group) |
| 10438 | { |
| 10439 | struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info; |
| 10440 | struct btrfs_swapfile_pin *sp, *entry; |
| 10441 | struct rb_node **p; |
| 10442 | struct rb_node *parent = NULL; |
| 10443 | |
| 10444 | sp = kmalloc(sizeof(*sp), GFP_NOFS); |
| 10445 | if (!sp) |
| 10446 | return -ENOMEM; |
| 10447 | sp->ptr = ptr; |
| 10448 | sp->inode = inode; |
| 10449 | sp->is_block_group = is_block_group; |
Filipe Manana | 195a49e | 2021-02-05 12:55:37 +0000 | [diff] [blame] | 10450 | sp->bg_extent_count = 1; |
Omar Sandoval | ed46ff3 | 2016-11-03 10:28:14 -0700 | [diff] [blame] | 10451 | |
| 10452 | spin_lock(&fs_info->swapfile_pins_lock); |
| 10453 | p = &fs_info->swapfile_pins.rb_node; |
| 10454 | while (*p) { |
| 10455 | parent = *p; |
| 10456 | entry = rb_entry(parent, struct btrfs_swapfile_pin, node); |
| 10457 | if (sp->ptr < entry->ptr || |
| 10458 | (sp->ptr == entry->ptr && sp->inode < entry->inode)) { |
| 10459 | p = &(*p)->rb_left; |
| 10460 | } else if (sp->ptr > entry->ptr || |
| 10461 | (sp->ptr == entry->ptr && sp->inode > entry->inode)) { |
| 10462 | p = &(*p)->rb_right; |
| 10463 | } else { |
Filipe Manana | 195a49e | 2021-02-05 12:55:37 +0000 | [diff] [blame] | 10464 | if (is_block_group) |
| 10465 | entry->bg_extent_count++; |
Omar Sandoval | ed46ff3 | 2016-11-03 10:28:14 -0700 | [diff] [blame] | 10466 | spin_unlock(&fs_info->swapfile_pins_lock); |
| 10467 | kfree(sp); |
| 10468 | return 1; |
| 10469 | } |
| 10470 | } |
| 10471 | rb_link_node(&sp->node, parent, p); |
| 10472 | rb_insert_color(&sp->node, &fs_info->swapfile_pins); |
| 10473 | spin_unlock(&fs_info->swapfile_pins_lock); |
| 10474 | return 0; |
| 10475 | } |
| 10476 | |
| 10477 | /* Free all of the entries pinned by this swapfile. */ |
| 10478 | static void btrfs_free_swapfile_pins(struct inode *inode) |
| 10479 | { |
| 10480 | struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info; |
| 10481 | struct btrfs_swapfile_pin *sp; |
| 10482 | struct rb_node *node, *next; |
| 10483 | |
| 10484 | spin_lock(&fs_info->swapfile_pins_lock); |
| 10485 | node = rb_first(&fs_info->swapfile_pins); |
| 10486 | while (node) { |
| 10487 | next = rb_next(node); |
| 10488 | sp = rb_entry(node, struct btrfs_swapfile_pin, node); |
| 10489 | if (sp->inode == inode) { |
| 10490 | rb_erase(&sp->node, &fs_info->swapfile_pins); |
Filipe Manana | 195a49e | 2021-02-05 12:55:37 +0000 | [diff] [blame] | 10491 | if (sp->is_block_group) { |
| 10492 | btrfs_dec_block_group_swap_extents(sp->ptr, |
| 10493 | sp->bg_extent_count); |
Omar Sandoval | ed46ff3 | 2016-11-03 10:28:14 -0700 | [diff] [blame] | 10494 | btrfs_put_block_group(sp->ptr); |
Filipe Manana | 195a49e | 2021-02-05 12:55:37 +0000 | [diff] [blame] | 10495 | } |
Omar Sandoval | ed46ff3 | 2016-11-03 10:28:14 -0700 | [diff] [blame] | 10496 | kfree(sp); |
| 10497 | } |
| 10498 | node = next; |
| 10499 | } |
| 10500 | spin_unlock(&fs_info->swapfile_pins_lock); |
| 10501 | } |
| 10502 | |
| 10503 | struct btrfs_swap_info { |
| 10504 | u64 start; |
| 10505 | u64 block_start; |
| 10506 | u64 block_len; |
| 10507 | u64 lowest_ppage; |
| 10508 | u64 highest_ppage; |
| 10509 | unsigned long nr_pages; |
| 10510 | int nr_extents; |
| 10511 | }; |
| 10512 | |
| 10513 | static int btrfs_add_swap_extent(struct swap_info_struct *sis, |
| 10514 | struct btrfs_swap_info *bsi) |
| 10515 | { |
| 10516 | unsigned long nr_pages; |
| 10517 | u64 first_ppage, first_ppage_reported, next_ppage; |
| 10518 | int ret; |
| 10519 | |
| 10520 | first_ppage = ALIGN(bsi->block_start, PAGE_SIZE) >> PAGE_SHIFT; |
| 10521 | next_ppage = ALIGN_DOWN(bsi->block_start + bsi->block_len, |
| 10522 | PAGE_SIZE) >> PAGE_SHIFT; |
| 10523 | |
| 10524 | if (first_ppage >= next_ppage) |
| 10525 | return 0; |
| 10526 | nr_pages = next_ppage - first_ppage; |
| 10527 | |
| 10528 | first_ppage_reported = first_ppage; |
| 10529 | if (bsi->start == 0) |
| 10530 | first_ppage_reported++; |
| 10531 | if (bsi->lowest_ppage > first_ppage_reported) |
| 10532 | bsi->lowest_ppage = first_ppage_reported; |
| 10533 | if (bsi->highest_ppage < (next_ppage - 1)) |
| 10534 | bsi->highest_ppage = next_ppage - 1; |
| 10535 | |
| 10536 | ret = add_swap_extent(sis, bsi->nr_pages, nr_pages, first_ppage); |
| 10537 | if (ret < 0) |
| 10538 | return ret; |
| 10539 | bsi->nr_extents += ret; |
| 10540 | bsi->nr_pages += nr_pages; |
| 10541 | return 0; |
| 10542 | } |
| 10543 | |
| 10544 | static void btrfs_swap_deactivate(struct file *file) |
| 10545 | { |
| 10546 | struct inode *inode = file_inode(file); |
| 10547 | |
| 10548 | btrfs_free_swapfile_pins(inode); |
| 10549 | atomic_dec(&BTRFS_I(inode)->root->nr_swapfiles); |
| 10550 | } |
| 10551 | |
| 10552 | static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file, |
| 10553 | sector_t *span) |
| 10554 | { |
| 10555 | struct inode *inode = file_inode(file); |
Filipe Manana | dd0734f | 2021-02-05 12:55:38 +0000 | [diff] [blame] | 10556 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 10557 | struct btrfs_fs_info *fs_info = root->fs_info; |
Omar Sandoval | ed46ff3 | 2016-11-03 10:28:14 -0700 | [diff] [blame] | 10558 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
| 10559 | struct extent_state *cached_state = NULL; |
| 10560 | struct extent_map *em = NULL; |
| 10561 | struct btrfs_device *device = NULL; |
| 10562 | struct btrfs_swap_info bsi = { |
| 10563 | .lowest_ppage = (sector_t)-1ULL, |
| 10564 | }; |
| 10565 | int ret = 0; |
| 10566 | u64 isize; |
| 10567 | u64 start; |
| 10568 | |
| 10569 | /* |
| 10570 | * If the swap file was just created, make sure delalloc is done. If the |
| 10571 | * file changes again after this, the user is doing something stupid and |
| 10572 | * we don't really care. |
| 10573 | */ |
| 10574 | ret = btrfs_wait_ordered_range(inode, 0, (u64)-1); |
| 10575 | if (ret) |
| 10576 | return ret; |
| 10577 | |
| 10578 | /* |
| 10579 | * The inode is locked, so these flags won't change after we check them. |
| 10580 | */ |
| 10581 | if (BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS) { |
| 10582 | btrfs_warn(fs_info, "swapfile must not be compressed"); |
| 10583 | return -EINVAL; |
| 10584 | } |
| 10585 | if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW)) { |
| 10586 | btrfs_warn(fs_info, "swapfile must not be copy-on-write"); |
| 10587 | return -EINVAL; |
| 10588 | } |
| 10589 | if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) { |
| 10590 | btrfs_warn(fs_info, "swapfile must not be checksummed"); |
| 10591 | return -EINVAL; |
| 10592 | } |
| 10593 | |
| 10594 | /* |
| 10595 | * Balance or device remove/replace/resize can move stuff around from |
Goldwyn Rodrigues | c3e1f96 | 2020-08-25 10:02:32 -0500 | [diff] [blame] | 10596 | * under us. The exclop protection makes sure they aren't running/won't |
| 10597 | * run concurrently while we are mapping the swap extents, and |
| 10598 | * fs_info->swapfile_pins prevents them from running while the swap |
| 10599 | * file is active and moving the extents. Note that this also prevents |
| 10600 | * 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] | 10601 | * really worth the trouble to allow it. |
| 10602 | */ |
Goldwyn Rodrigues | c3e1f96 | 2020-08-25 10:02:32 -0500 | [diff] [blame] | 10603 | if (!btrfs_exclop_start(fs_info, BTRFS_EXCLOP_SWAP_ACTIVATE)) { |
Omar Sandoval | ed46ff3 | 2016-11-03 10:28:14 -0700 | [diff] [blame] | 10604 | btrfs_warn(fs_info, |
| 10605 | "cannot activate swapfile while exclusive operation is running"); |
| 10606 | return -EBUSY; |
| 10607 | } |
Filipe Manana | dd0734f | 2021-02-05 12:55:38 +0000 | [diff] [blame] | 10608 | |
| 10609 | /* |
| 10610 | * Prevent snapshot creation while we are activating the swap file. |
| 10611 | * We do not want to race with snapshot creation. If snapshot creation |
| 10612 | * already started before we bumped nr_swapfiles from 0 to 1 and |
| 10613 | * completes before the first write into the swap file after it is |
| 10614 | * activated, than that write would fallback to COW. |
| 10615 | */ |
| 10616 | if (!btrfs_drew_try_write_lock(&root->snapshot_lock)) { |
| 10617 | btrfs_exclop_finish(fs_info); |
| 10618 | btrfs_warn(fs_info, |
| 10619 | "cannot activate swapfile because snapshot creation is in progress"); |
| 10620 | return -EINVAL; |
| 10621 | } |
Omar Sandoval | ed46ff3 | 2016-11-03 10:28:14 -0700 | [diff] [blame] | 10622 | /* |
| 10623 | * Snapshots can create extents which require COW even if NODATACOW is |
| 10624 | * set. We use this counter to prevent snapshots. We must increment it |
| 10625 | * before walking the extents because we don't want a concurrent |
| 10626 | * snapshot to run after we've already checked the extents. |
| 10627 | */ |
Filipe Manana | dd0734f | 2021-02-05 12:55:38 +0000 | [diff] [blame] | 10628 | atomic_inc(&root->nr_swapfiles); |
Omar Sandoval | ed46ff3 | 2016-11-03 10:28:14 -0700 | [diff] [blame] | 10629 | |
| 10630 | isize = ALIGN_DOWN(inode->i_size, fs_info->sectorsize); |
| 10631 | |
| 10632 | lock_extent_bits(io_tree, 0, isize - 1, &cached_state); |
| 10633 | start = 0; |
| 10634 | while (start < isize) { |
| 10635 | u64 logical_block_start, physical_block_start; |
David Sterba | 32da5386 | 2019-10-29 19:20:18 +0100 | [diff] [blame] | 10636 | struct btrfs_block_group *bg; |
Omar Sandoval | ed46ff3 | 2016-11-03 10:28:14 -0700 | [diff] [blame] | 10637 | u64 len = isize - start; |
| 10638 | |
Omar Sandoval | 39b07b5 | 2019-12-02 17:34:23 -0800 | [diff] [blame] | 10639 | em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len); |
Omar Sandoval | ed46ff3 | 2016-11-03 10:28:14 -0700 | [diff] [blame] | 10640 | if (IS_ERR(em)) { |
| 10641 | ret = PTR_ERR(em); |
| 10642 | goto out; |
| 10643 | } |
| 10644 | |
| 10645 | if (em->block_start == EXTENT_MAP_HOLE) { |
| 10646 | btrfs_warn(fs_info, "swapfile must not have holes"); |
| 10647 | ret = -EINVAL; |
| 10648 | goto out; |
| 10649 | } |
| 10650 | if (em->block_start == EXTENT_MAP_INLINE) { |
| 10651 | /* |
| 10652 | * It's unlikely we'll ever actually find ourselves |
| 10653 | * here, as a file small enough to fit inline won't be |
| 10654 | * big enough to store more than the swap header, but in |
| 10655 | * case something changes in the future, let's catch it |
| 10656 | * here rather than later. |
| 10657 | */ |
| 10658 | btrfs_warn(fs_info, "swapfile must not be inline"); |
| 10659 | ret = -EINVAL; |
| 10660 | goto out; |
| 10661 | } |
| 10662 | if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) { |
| 10663 | btrfs_warn(fs_info, "swapfile must not be compressed"); |
| 10664 | ret = -EINVAL; |
| 10665 | goto out; |
| 10666 | } |
| 10667 | |
| 10668 | logical_block_start = em->block_start + (start - em->start); |
| 10669 | len = min(len, em->len - (start - em->start)); |
| 10670 | free_extent_map(em); |
| 10671 | em = NULL; |
| 10672 | |
Boris Burkov | a84d5d4 | 2020-08-18 11:00:05 -0700 | [diff] [blame] | 10673 | ret = can_nocow_extent(inode, start, &len, NULL, NULL, NULL, true); |
Omar Sandoval | ed46ff3 | 2016-11-03 10:28:14 -0700 | [diff] [blame] | 10674 | if (ret < 0) { |
| 10675 | goto out; |
| 10676 | } else if (ret) { |
| 10677 | ret = 0; |
| 10678 | } else { |
| 10679 | btrfs_warn(fs_info, |
| 10680 | "swapfile must not be copy-on-write"); |
| 10681 | ret = -EINVAL; |
| 10682 | goto out; |
| 10683 | } |
| 10684 | |
| 10685 | em = btrfs_get_chunk_map(fs_info, logical_block_start, len); |
| 10686 | if (IS_ERR(em)) { |
| 10687 | ret = PTR_ERR(em); |
| 10688 | goto out; |
| 10689 | } |
| 10690 | |
| 10691 | if (em->map_lookup->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) { |
| 10692 | btrfs_warn(fs_info, |
| 10693 | "swapfile must have single data profile"); |
| 10694 | ret = -EINVAL; |
| 10695 | goto out; |
| 10696 | } |
| 10697 | |
| 10698 | if (device == NULL) { |
| 10699 | device = em->map_lookup->stripes[0].dev; |
| 10700 | ret = btrfs_add_swapfile_pin(inode, device, false); |
| 10701 | if (ret == 1) |
| 10702 | ret = 0; |
| 10703 | else if (ret) |
| 10704 | goto out; |
| 10705 | } else if (device != em->map_lookup->stripes[0].dev) { |
| 10706 | btrfs_warn(fs_info, "swapfile must be on one device"); |
| 10707 | ret = -EINVAL; |
| 10708 | goto out; |
| 10709 | } |
| 10710 | |
| 10711 | physical_block_start = (em->map_lookup->stripes[0].physical + |
| 10712 | (logical_block_start - em->start)); |
| 10713 | len = min(len, em->len - (logical_block_start - em->start)); |
| 10714 | free_extent_map(em); |
| 10715 | em = NULL; |
| 10716 | |
| 10717 | bg = btrfs_lookup_block_group(fs_info, logical_block_start); |
| 10718 | if (!bg) { |
| 10719 | btrfs_warn(fs_info, |
| 10720 | "could not find block group containing swapfile"); |
| 10721 | ret = -EINVAL; |
| 10722 | goto out; |
| 10723 | } |
| 10724 | |
Filipe Manana | 195a49e | 2021-02-05 12:55:37 +0000 | [diff] [blame] | 10725 | if (!btrfs_inc_block_group_swap_extents(bg)) { |
| 10726 | btrfs_warn(fs_info, |
| 10727 | "block group for swapfile at %llu is read-only%s", |
| 10728 | bg->start, |
| 10729 | atomic_read(&fs_info->scrubs_running) ? |
| 10730 | " (scrub running)" : ""); |
| 10731 | btrfs_put_block_group(bg); |
| 10732 | ret = -EINVAL; |
| 10733 | goto out; |
| 10734 | } |
| 10735 | |
Omar Sandoval | ed46ff3 | 2016-11-03 10:28:14 -0700 | [diff] [blame] | 10736 | ret = btrfs_add_swapfile_pin(inode, bg, true); |
| 10737 | if (ret) { |
| 10738 | btrfs_put_block_group(bg); |
| 10739 | if (ret == 1) |
| 10740 | ret = 0; |
| 10741 | else |
| 10742 | goto out; |
| 10743 | } |
| 10744 | |
| 10745 | if (bsi.block_len && |
| 10746 | bsi.block_start + bsi.block_len == physical_block_start) { |
| 10747 | bsi.block_len += len; |
| 10748 | } else { |
| 10749 | if (bsi.block_len) { |
| 10750 | ret = btrfs_add_swap_extent(sis, &bsi); |
| 10751 | if (ret) |
| 10752 | goto out; |
| 10753 | } |
| 10754 | bsi.start = start; |
| 10755 | bsi.block_start = physical_block_start; |
| 10756 | bsi.block_len = len; |
| 10757 | } |
| 10758 | |
| 10759 | start += len; |
| 10760 | } |
| 10761 | |
| 10762 | if (bsi.block_len) |
| 10763 | ret = btrfs_add_swap_extent(sis, &bsi); |
| 10764 | |
| 10765 | out: |
| 10766 | if (!IS_ERR_OR_NULL(em)) |
| 10767 | free_extent_map(em); |
| 10768 | |
| 10769 | unlock_extent_cached(io_tree, 0, isize - 1, &cached_state); |
| 10770 | |
| 10771 | if (ret) |
| 10772 | btrfs_swap_deactivate(file); |
| 10773 | |
Filipe Manana | dd0734f | 2021-02-05 12:55:38 +0000 | [diff] [blame] | 10774 | btrfs_drew_write_unlock(&root->snapshot_lock); |
| 10775 | |
Goldwyn Rodrigues | c3e1f96 | 2020-08-25 10:02:32 -0500 | [diff] [blame] | 10776 | btrfs_exclop_finish(fs_info); |
Omar Sandoval | ed46ff3 | 2016-11-03 10:28:14 -0700 | [diff] [blame] | 10777 | |
| 10778 | if (ret) |
| 10779 | return ret; |
| 10780 | |
| 10781 | if (device) |
| 10782 | sis->bdev = device->bdev; |
| 10783 | *span = bsi.highest_ppage - bsi.lowest_ppage + 1; |
| 10784 | sis->max = bsi.nr_pages; |
| 10785 | sis->pages = bsi.nr_pages - 1; |
| 10786 | sis->highest_bit = bsi.nr_pages - 1; |
| 10787 | return bsi.nr_extents; |
| 10788 | } |
| 10789 | #else |
| 10790 | static void btrfs_swap_deactivate(struct file *file) |
| 10791 | { |
| 10792 | } |
| 10793 | |
| 10794 | static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file, |
| 10795 | sector_t *span) |
| 10796 | { |
| 10797 | return -EOPNOTSUPP; |
| 10798 | } |
| 10799 | #endif |
| 10800 | |
Filipe Manana | 2766ff6 | 2020-11-04 11:07:34 +0000 | [diff] [blame] | 10801 | /* |
| 10802 | * Update the number of bytes used in the VFS' inode. When we replace extents in |
| 10803 | * a range (clone, dedupe, fallocate's zero range), we must update the number of |
| 10804 | * bytes used by the inode in an atomic manner, so that concurrent stat(2) calls |
| 10805 | * always get a correct value. |
| 10806 | */ |
| 10807 | void btrfs_update_inode_bytes(struct btrfs_inode *inode, |
| 10808 | const u64 add_bytes, |
| 10809 | const u64 del_bytes) |
| 10810 | { |
| 10811 | if (add_bytes == del_bytes) |
| 10812 | return; |
| 10813 | |
| 10814 | spin_lock(&inode->lock); |
| 10815 | if (del_bytes > 0) |
| 10816 | inode_sub_bytes(&inode->vfs_inode, del_bytes); |
| 10817 | if (add_bytes > 0) |
| 10818 | inode_add_bytes(&inode->vfs_inode, add_bytes); |
| 10819 | spin_unlock(&inode->lock); |
| 10820 | } |
| 10821 | |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 10822 | static const struct inode_operations btrfs_dir_inode_operations = { |
Chris Mason | 3394e16 | 2008-11-17 20:42:26 -0500 | [diff] [blame] | 10823 | .getattr = btrfs_getattr, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10824 | .lookup = btrfs_lookup, |
| 10825 | .create = btrfs_create, |
| 10826 | .unlink = btrfs_unlink, |
| 10827 | .link = btrfs_link, |
| 10828 | .mkdir = btrfs_mkdir, |
| 10829 | .rmdir = btrfs_rmdir, |
Miklos Szeredi | 2773bf0 | 2016-09-27 11:03:58 +0200 | [diff] [blame] | 10830 | .rename = btrfs_rename2, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10831 | .symlink = btrfs_symlink, |
| 10832 | .setattr = btrfs_setattr, |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 10833 | .mknod = btrfs_mknod, |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 10834 | .listxattr = btrfs_listxattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 10835 | .permission = btrfs_permission, |
Christoph Hellwig | 4e34e71 | 2011-07-23 17:37:31 +0200 | [diff] [blame] | 10836 | .get_acl = btrfs_get_acl, |
Christoph Hellwig | 996a710 | 2013-12-20 05:16:43 -0800 | [diff] [blame] | 10837 | .set_acl = btrfs_set_acl, |
Guangyu Sun | 93fd63c | 2013-09-16 10:42:03 -0700 | [diff] [blame] | 10838 | .update_time = btrfs_update_time, |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 10839 | .tmpfile = btrfs_tmpfile, |
Miklos Szeredi | 97fc297 | 2021-04-07 14:36:43 +0200 | [diff] [blame] | 10840 | .fileattr_get = btrfs_fileattr_get, |
| 10841 | .fileattr_set = btrfs_fileattr_set, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10842 | }; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 10843 | |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 10844 | static const struct file_operations btrfs_dir_file_operations = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10845 | .llseek = generic_file_llseek, |
| 10846 | .read = generic_read_dir, |
Omar Sandoval | 02dbfc9 | 2016-05-20 13:50:33 -0700 | [diff] [blame] | 10847 | .iterate_shared = btrfs_real_readdir, |
Josef Bacik | 23b5ec7 | 2017-07-24 15:14:25 -0400 | [diff] [blame] | 10848 | .open = btrfs_opendir, |
Christoph Hellwig | 34287aa | 2007-09-14 10:22:47 -0400 | [diff] [blame] | 10849 | .unlocked_ioctl = btrfs_ioctl, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10850 | #ifdef CONFIG_COMPAT |
Luke Dashjr | 4c63c24 | 2015-10-29 08:22:21 +0000 | [diff] [blame] | 10851 | .compat_ioctl = btrfs_compat_ioctl, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10852 | #endif |
Sage Weil | 6bf13c0 | 2008-06-10 10:07:39 -0400 | [diff] [blame] | 10853 | .release = btrfs_release_file, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 10854 | .fsync = btrfs_sync_file, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10855 | }; |
| 10856 | |
Chris Mason | 3505439 | 2009-01-21 13:11:13 -0500 | [diff] [blame] | 10857 | /* |
| 10858 | * btrfs doesn't support the bmap operation because swapfiles |
| 10859 | * use bmap to make a mapping of extents in the file. They assume |
| 10860 | * these extents won't change over the life of the file and they |
| 10861 | * use the bmap result to do IO directly to the drive. |
| 10862 | * |
| 10863 | * the btrfs bmap call would return logical addresses that aren't |
| 10864 | * suitable for IO and they also will change frequently as COW |
| 10865 | * operations happen. So, swapfile + btrfs == corruption. |
| 10866 | * |
| 10867 | * For now we're avoiding this by dropping bmap. |
| 10868 | */ |
Alexey Dobriyan | 7f09410 | 2009-09-21 17:01:10 -0700 | [diff] [blame] | 10869 | static const struct address_space_operations btrfs_aops = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10870 | .readpage = btrfs_readpage, |
| 10871 | .writepage = btrfs_writepage, |
Chris Mason | b293f02e | 2007-11-01 19:45:34 -0400 | [diff] [blame] | 10872 | .writepages = btrfs_writepages, |
Matthew Wilcox (Oracle) | ba206a0 | 2020-06-01 21:47:05 -0700 | [diff] [blame] | 10873 | .readahead = btrfs_readahead, |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 10874 | .direct_IO = noop_direct_IO, |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 10875 | .invalidatepage = btrfs_invalidatepage, |
| 10876 | .releasepage = btrfs_releasepage, |
Roman Gushchin | f8e6608 | 2020-03-04 16:57:35 -0800 | [diff] [blame] | 10877 | #ifdef CONFIG_MIGRATION |
| 10878 | .migratepage = btrfs_migratepage, |
| 10879 | #endif |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 10880 | .set_page_dirty = btrfs_set_page_dirty, |
Andi Kleen | 465fdd9 | 2009-09-16 11:50:18 +0200 | [diff] [blame] | 10881 | .error_remove_page = generic_error_remove_page, |
Omar Sandoval | ed46ff3 | 2016-11-03 10:28:14 -0700 | [diff] [blame] | 10882 | .swap_activate = btrfs_swap_activate, |
| 10883 | .swap_deactivate = btrfs_swap_deactivate, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10884 | }; |
| 10885 | |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 10886 | static const struct inode_operations btrfs_file_inode_operations = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10887 | .getattr = btrfs_getattr, |
| 10888 | .setattr = btrfs_setattr, |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 10889 | .listxattr = btrfs_listxattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 10890 | .permission = btrfs_permission, |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 10891 | .fiemap = btrfs_fiemap, |
Christoph Hellwig | 4e34e71 | 2011-07-23 17:37:31 +0200 | [diff] [blame] | 10892 | .get_acl = btrfs_get_acl, |
Christoph Hellwig | 996a710 | 2013-12-20 05:16:43 -0800 | [diff] [blame] | 10893 | .set_acl = btrfs_set_acl, |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 10894 | .update_time = btrfs_update_time, |
Miklos Szeredi | 97fc297 | 2021-04-07 14:36:43 +0200 | [diff] [blame] | 10895 | .fileattr_get = btrfs_fileattr_get, |
| 10896 | .fileattr_set = btrfs_fileattr_set, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10897 | }; |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 10898 | static const struct inode_operations btrfs_special_inode_operations = { |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 10899 | .getattr = btrfs_getattr, |
| 10900 | .setattr = btrfs_setattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 10901 | .permission = btrfs_permission, |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 10902 | .listxattr = btrfs_listxattr, |
Christoph Hellwig | 4e34e71 | 2011-07-23 17:37:31 +0200 | [diff] [blame] | 10903 | .get_acl = btrfs_get_acl, |
Christoph Hellwig | 996a710 | 2013-12-20 05:16:43 -0800 | [diff] [blame] | 10904 | .set_acl = btrfs_set_acl, |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 10905 | .update_time = btrfs_update_time, |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 10906 | }; |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 10907 | static const struct inode_operations btrfs_symlink_inode_operations = { |
Al Viro | 6b25539 | 2015-11-17 10:20:54 -0500 | [diff] [blame] | 10908 | .get_link = page_get_link, |
Li Zefan | f209561 | 2010-11-19 02:05:24 +0000 | [diff] [blame] | 10909 | .getattr = btrfs_getattr, |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 10910 | .setattr = btrfs_setattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 10911 | .permission = btrfs_permission, |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 10912 | .listxattr = btrfs_listxattr, |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 10913 | .update_time = btrfs_update_time, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10914 | }; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 10915 | |
Alexey Dobriyan | 82d339d | 2009-10-09 09:54:36 -0400 | [diff] [blame] | 10916 | const struct dentry_operations btrfs_dentry_operations = { |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 10917 | .d_delete = btrfs_dentry_delete, |
| 10918 | }; |