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" |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 54 | |
| 55 | struct btrfs_iget_args { |
David Sterba | 0202e83 | 2020-05-15 19:35:59 +0200 | [diff] [blame] | 56 | u64 ino; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 57 | struct btrfs_root *root; |
| 58 | }; |
| 59 | |
Filipe Manana | f28a492 | 2015-12-08 19:23:20 +0000 | [diff] [blame] | 60 | struct btrfs_dio_data { |
Filipe Manana | f28a492 | 2015-12-08 19:23:20 +0000 | [diff] [blame] | 61 | u64 reserve; |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 62 | loff_t length; |
| 63 | ssize_t submitted; |
| 64 | struct extent_changeset *data_reserved; |
Filipe Manana | f28a492 | 2015-12-08 19:23:20 +0000 | [diff] [blame] | 65 | }; |
| 66 | |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 67 | static const struct inode_operations btrfs_dir_inode_operations; |
| 68 | static const struct inode_operations btrfs_symlink_inode_operations; |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 69 | static const struct inode_operations btrfs_special_inode_operations; |
| 70 | static const struct inode_operations btrfs_file_inode_operations; |
Alexey Dobriyan | 7f09410 | 2009-09-21 17:01:10 -0700 | [diff] [blame] | 71 | static const struct address_space_operations btrfs_aops; |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 72 | static const struct file_operations btrfs_dir_file_operations; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 73 | |
| 74 | static struct kmem_cache *btrfs_inode_cachep; |
| 75 | struct kmem_cache *btrfs_trans_handle_cachep; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 76 | struct kmem_cache *btrfs_path_cachep; |
Josef Bacik | dc89e98 | 2011-01-28 17:05:48 -0500 | [diff] [blame] | 77 | struct kmem_cache *btrfs_free_space_cachep; |
Christophe Leroy | 3acd485 | 2019-08-21 15:05:55 +0000 | [diff] [blame] | 78 | struct kmem_cache *btrfs_free_space_bitmap_cachep; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 79 | |
Eric Sandeen | 3972f26 | 2013-01-12 02:57:22 +0000 | [diff] [blame] | 80 | static int btrfs_setsize(struct inode *inode, struct iattr *attr); |
Filipe Manana | 213e8c5 | 2018-02-06 20:40:31 +0000 | [diff] [blame] | 81 | static int btrfs_truncate(struct inode *inode, bool skip_writeback); |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 82 | static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent); |
Nikolay Borisov | 6e26c44 | 2020-06-03 08:55:14 +0300 | [diff] [blame] | 83 | static noinline int cow_file_range(struct btrfs_inode *inode, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 84 | struct page *locked_page, |
Nikolay Borisov | 74e9194 | 2019-07-17 16:18:16 +0300 | [diff] [blame] | 85 | u64 start, u64 end, int *page_started, |
Nikolay Borisov | 330a582 | 2019-07-17 16:18:17 +0300 | [diff] [blame] | 86 | unsigned long *nr_written, int unlock); |
Nikolay Borisov | 4b67c11 | 2020-06-03 08:55:05 +0300 | [diff] [blame] | 87 | static struct extent_map *create_io_em(struct btrfs_inode *inode, u64 start, |
| 88 | u64 len, u64 orig_start, u64 block_start, |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 89 | u64 block_len, u64 orig_block_len, |
| 90 | u64 ram_bytes, int compress_type, |
| 91 | int type); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 92 | |
Nikolay Borisov | b672b5c | 2020-06-03 08:55:24 +0300 | [diff] [blame] | 93 | static void __endio_write_update_ordered(struct btrfs_inode *inode, |
Qu Wenruo | 52427260 | 2017-03-08 10:25:52 +0800 | [diff] [blame] | 94 | const u64 offset, const u64 bytes, |
| 95 | const bool uptodate); |
| 96 | |
| 97 | /* |
Goldwyn Rodrigues | a14b78a | 2020-09-24 11:39:16 -0500 | [diff] [blame] | 98 | * btrfs_inode_lock - lock inode i_rwsem based on arguments passed |
| 99 | * |
| 100 | * ilock_flags can have the following bit set: |
| 101 | * |
| 102 | * BTRFS_ILOCK_SHARED - acquire a shared lock on the inode |
| 103 | * BTRFS_ILOCK_TRY - try to acquire the lock, if fails on first attempt |
| 104 | * return -EAGAIN |
| 105 | */ |
| 106 | int btrfs_inode_lock(struct inode *inode, unsigned int ilock_flags) |
| 107 | { |
| 108 | if (ilock_flags & BTRFS_ILOCK_SHARED) { |
| 109 | if (ilock_flags & BTRFS_ILOCK_TRY) { |
| 110 | if (!inode_trylock_shared(inode)) |
| 111 | return -EAGAIN; |
| 112 | else |
| 113 | return 0; |
| 114 | } |
| 115 | inode_lock_shared(inode); |
| 116 | } else { |
| 117 | if (ilock_flags & BTRFS_ILOCK_TRY) { |
| 118 | if (!inode_trylock(inode)) |
| 119 | return -EAGAIN; |
| 120 | else |
| 121 | return 0; |
| 122 | } |
| 123 | inode_lock(inode); |
| 124 | } |
| 125 | return 0; |
| 126 | } |
| 127 | |
| 128 | /* |
| 129 | * btrfs_inode_unlock - unock inode i_rwsem |
| 130 | * |
| 131 | * ilock_flags should contain the same bits set as passed to btrfs_inode_lock() |
| 132 | * to decide whether the lock acquired is shared or exclusive. |
| 133 | */ |
| 134 | void btrfs_inode_unlock(struct inode *inode, unsigned int ilock_flags) |
| 135 | { |
| 136 | if (ilock_flags & BTRFS_ILOCK_SHARED) |
| 137 | inode_unlock_shared(inode); |
| 138 | else |
| 139 | inode_unlock(inode); |
| 140 | } |
| 141 | |
| 142 | /* |
Qu Wenruo | 52427260 | 2017-03-08 10:25:52 +0800 | [diff] [blame] | 143 | * Cleanup all submitted ordered extents in specified range to handle errors |
Andrea Gelmini | 52042d8 | 2018-11-28 12:05:13 +0100 | [diff] [blame] | 144 | * from the btrfs_run_delalloc_range() callback. |
Qu Wenruo | 52427260 | 2017-03-08 10:25:52 +0800 | [diff] [blame] | 145 | * |
| 146 | * NOTE: caller must ensure that when an error happens, it can not call |
| 147 | * extent_clear_unlock_delalloc() to clear both the bits EXTENT_DO_ACCOUNTING |
| 148 | * and EXTENT_DELALLOC simultaneously, because that causes the reserved metadata |
| 149 | * 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] | 150 | * extent (btrfs_finish_ordered_io()). |
Qu Wenruo | 52427260 | 2017-03-08 10:25:52 +0800 | [diff] [blame] | 151 | */ |
Nikolay Borisov | 64e1db5 | 2020-06-03 08:55:25 +0300 | [diff] [blame] | 152 | static inline void btrfs_cleanup_ordered_extents(struct btrfs_inode *inode, |
Nikolay Borisov | d1051d6 | 2018-11-21 17:10:52 +0200 | [diff] [blame] | 153 | struct page *locked_page, |
| 154 | u64 offset, u64 bytes) |
Qu Wenruo | 52427260 | 2017-03-08 10:25:52 +0800 | [diff] [blame] | 155 | { |
Naohiro Aota | 63d71450c | 2017-09-01 17:58:47 +0900 | [diff] [blame] | 156 | unsigned long index = offset >> PAGE_SHIFT; |
| 157 | unsigned long end_index = (offset + bytes - 1) >> PAGE_SHIFT; |
Nikolay Borisov | d1051d6 | 2018-11-21 17:10:52 +0200 | [diff] [blame] | 158 | u64 page_start = page_offset(locked_page); |
| 159 | u64 page_end = page_start + PAGE_SIZE - 1; |
| 160 | |
Naohiro Aota | 63d71450c | 2017-09-01 17:58:47 +0900 | [diff] [blame] | 161 | struct page *page; |
| 162 | |
| 163 | while (index <= end_index) { |
Nikolay Borisov | 64e1db5 | 2020-06-03 08:55:25 +0300 | [diff] [blame] | 164 | page = find_get_page(inode->vfs_inode.i_mapping, index); |
Naohiro Aota | 63d71450c | 2017-09-01 17:58:47 +0900 | [diff] [blame] | 165 | index++; |
| 166 | if (!page) |
| 167 | continue; |
| 168 | ClearPagePrivate2(page); |
| 169 | put_page(page); |
| 170 | } |
Nikolay Borisov | d1051d6 | 2018-11-21 17:10:52 +0200 | [diff] [blame] | 171 | |
| 172 | /* |
| 173 | * In case this page belongs to the delalloc range being instantiated |
| 174 | * then skip it, since the first page of a range is going to be |
| 175 | * properly cleaned up by the caller of run_delalloc_range |
| 176 | */ |
| 177 | if (page_start >= offset && page_end <= (offset + bytes - 1)) { |
| 178 | offset += PAGE_SIZE; |
| 179 | bytes -= PAGE_SIZE; |
| 180 | } |
| 181 | |
Nikolay Borisov | 64e1db5 | 2020-06-03 08:55:25 +0300 | [diff] [blame] | 182 | return __endio_write_update_ordered(inode, offset, bytes, false); |
Qu Wenruo | 52427260 | 2017-03-08 10:25:52 +0800 | [diff] [blame] | 183 | } |
| 184 | |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 185 | static int btrfs_dirty_inode(struct inode *inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 186 | |
Yan, Zheng | f34f57a | 2009-11-12 09:35:27 +0000 | [diff] [blame] | 187 | static int btrfs_init_inode_security(struct btrfs_trans_handle *trans, |
Eric Paris | 2a7dba3 | 2011-02-01 11:05:39 -0500 | [diff] [blame] | 188 | struct inode *inode, struct inode *dir, |
| 189 | const struct qstr *qstr) |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 190 | { |
| 191 | int err; |
| 192 | |
Yan, Zheng | f34f57a | 2009-11-12 09:35:27 +0000 | [diff] [blame] | 193 | err = btrfs_init_acl(trans, inode, dir); |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 194 | if (!err) |
Eric Paris | 2a7dba3 | 2011-02-01 11:05:39 -0500 | [diff] [blame] | 195 | err = btrfs_xattr_security_init(trans, inode, dir, qstr); |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 196 | return err; |
| 197 | } |
| 198 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 199 | /* |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 200 | * this does all the hard work for inserting an inline extent into |
| 201 | * the btree. The caller should have done a btrfs_drop_extents so that |
| 202 | * no overlapping inline items exist in the btree |
| 203 | */ |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 204 | static int insert_inline_extent(struct btrfs_trans_handle *trans, |
Filipe Manana | 5893dfb | 2020-11-04 11:07:32 +0000 | [diff] [blame] | 205 | struct btrfs_path *path, bool extent_inserted, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 206 | struct btrfs_root *root, struct inode *inode, |
| 207 | u64 start, size_t size, size_t compressed_size, |
Li Zefan | fe3f566 | 2011-03-28 08:30:38 +0000 | [diff] [blame] | 208 | int compress_type, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 209 | struct page **compressed_pages) |
| 210 | { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 211 | struct extent_buffer *leaf; |
| 212 | struct page *page = NULL; |
| 213 | char *kaddr; |
| 214 | unsigned long ptr; |
| 215 | struct btrfs_file_extent_item *ei; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 216 | int ret; |
| 217 | size_t cur_size = size; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 218 | unsigned long offset; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 219 | |
Jia-Ju Bai | 982f1f5d | 2019-07-27 16:51:13 +0800 | [diff] [blame] | 220 | ASSERT((compressed_size > 0 && compressed_pages) || |
| 221 | (compressed_size == 0 && !compressed_pages)); |
| 222 | |
Li Zefan | fe3f566 | 2011-03-28 08:30:38 +0000 | [diff] [blame] | 223 | if (compressed_size && compressed_pages) |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 224 | cur_size = compressed_size; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 225 | |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 226 | if (!extent_inserted) { |
| 227 | struct btrfs_key key; |
| 228 | size_t datasize; |
| 229 | |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 230 | key.objectid = btrfs_ino(BTRFS_I(inode)); |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 231 | key.offset = start; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 232 | key.type = BTRFS_EXTENT_DATA_KEY; |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 233 | |
| 234 | datasize = btrfs_file_extent_calc_inline_size(cur_size); |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 235 | ret = btrfs_insert_empty_item(trans, root, path, &key, |
| 236 | datasize); |
David Sterba | 79b4f4c | 2017-06-15 19:09:51 +0200 | [diff] [blame] | 237 | if (ret) |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 238 | goto fail; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 239 | } |
| 240 | leaf = path->nodes[0]; |
| 241 | ei = btrfs_item_ptr(leaf, path->slots[0], |
| 242 | struct btrfs_file_extent_item); |
| 243 | btrfs_set_file_extent_generation(leaf, ei, trans->transid); |
| 244 | btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE); |
| 245 | btrfs_set_file_extent_encryption(leaf, ei, 0); |
| 246 | btrfs_set_file_extent_other_encoding(leaf, ei, 0); |
| 247 | btrfs_set_file_extent_ram_bytes(leaf, ei, size); |
| 248 | ptr = btrfs_file_extent_inline_start(ei); |
| 249 | |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 250 | if (compress_type != BTRFS_COMPRESS_NONE) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 251 | struct page *cpage; |
| 252 | int i = 0; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 253 | while (compressed_size > 0) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 254 | cpage = compressed_pages[i]; |
Chris Mason | 5b050f0 | 2008-11-11 09:34:41 -0500 | [diff] [blame] | 255 | cur_size = min_t(unsigned long, compressed_size, |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 256 | PAGE_SIZE); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 257 | |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 258 | kaddr = kmap_atomic(cpage); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 259 | write_extent_buffer(leaf, kaddr, ptr, cur_size); |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 260 | kunmap_atomic(kaddr); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 261 | |
| 262 | i++; |
| 263 | ptr += cur_size; |
| 264 | compressed_size -= cur_size; |
| 265 | } |
| 266 | btrfs_set_file_extent_compression(leaf, ei, |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 267 | compress_type); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 268 | } else { |
| 269 | page = find_get_page(inode->i_mapping, |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 270 | start >> PAGE_SHIFT); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 271 | btrfs_set_file_extent_compression(leaf, ei, 0); |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 272 | kaddr = kmap_atomic(page); |
Johannes Thumshirn | 7073017 | 2018-12-05 15:23:03 +0100 | [diff] [blame] | 273 | offset = offset_in_page(start); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 274 | write_extent_buffer(leaf, kaddr + offset, ptr, size); |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 275 | kunmap_atomic(kaddr); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 276 | put_page(page); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 277 | } |
| 278 | btrfs_mark_buffer_dirty(leaf); |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 279 | btrfs_release_path(path); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 280 | |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 281 | /* |
Josef Bacik | 9ddc959 | 2020-01-17 09:02:22 -0500 | [diff] [blame] | 282 | * We align size to sectorsize for inline extents just for simplicity |
| 283 | * sake. |
| 284 | */ |
| 285 | size = ALIGN(size, root->fs_info->sectorsize); |
| 286 | ret = btrfs_inode_set_file_extent_range(BTRFS_I(inode), start, size); |
| 287 | if (ret) |
| 288 | goto fail; |
| 289 | |
| 290 | /* |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 291 | * we're an inline extent, so nobody can |
| 292 | * extend the file past i_size without locking |
| 293 | * a page we already have locked. |
| 294 | * |
| 295 | * We must do any isize and inode updates |
| 296 | * before we unlock the pages. Otherwise we |
| 297 | * could end up racing with unlink. |
| 298 | */ |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 299 | BTRFS_I(inode)->disk_i_size = inode->i_size; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 300 | fail: |
David Sterba | 79b4f4c | 2017-06-15 19:09:51 +0200 | [diff] [blame] | 301 | return ret; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | |
| 305 | /* |
| 306 | * conditionally insert an inline extent into the file. This |
| 307 | * does the checks required to make sure the data is small enough |
| 308 | * to fit as an inline extent. |
| 309 | */ |
Nikolay Borisov | a034940 | 2020-06-03 08:55:12 +0300 | [diff] [blame] | 310 | 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] | 311 | u64 end, size_t compressed_size, |
| 312 | int compress_type, |
| 313 | struct page **compressed_pages) |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 314 | { |
Filipe Manana | 5893dfb | 2020-11-04 11:07:32 +0000 | [diff] [blame] | 315 | struct btrfs_drop_extents_args drop_args = { 0 }; |
Nikolay Borisov | a034940 | 2020-06-03 08:55:12 +0300 | [diff] [blame] | 316 | struct btrfs_root *root = inode->root; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 317 | struct btrfs_fs_info *fs_info = root->fs_info; |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 318 | struct btrfs_trans_handle *trans; |
Nikolay Borisov | a034940 | 2020-06-03 08:55:12 +0300 | [diff] [blame] | 319 | u64 isize = i_size_read(&inode->vfs_inode); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 320 | u64 actual_end = min(end + 1, isize); |
| 321 | u64 inline_len = actual_end - start; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 322 | u64 aligned_end = ALIGN(end, fs_info->sectorsize); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 323 | u64 data_len = inline_len; |
| 324 | int ret; |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 325 | struct btrfs_path *path; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 326 | |
| 327 | if (compressed_size) |
| 328 | data_len = compressed_size; |
| 329 | |
| 330 | if (start > 0 || |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 331 | actual_end > fs_info->sectorsize || |
| 332 | data_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info) || |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 333 | (!compressed_size && |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 334 | (actual_end & (fs_info->sectorsize - 1)) == 0) || |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 335 | end + 1 < isize || |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 336 | data_len > fs_info->max_inline) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 337 | return 1; |
| 338 | } |
| 339 | |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 340 | path = btrfs_alloc_path(); |
| 341 | if (!path) |
| 342 | return -ENOMEM; |
| 343 | |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 344 | trans = btrfs_join_transaction(root); |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 345 | if (IS_ERR(trans)) { |
| 346 | btrfs_free_path(path); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 347 | return PTR_ERR(trans); |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 348 | } |
Nikolay Borisov | a034940 | 2020-06-03 08:55:12 +0300 | [diff] [blame] | 349 | trans->block_rsv = &inode->block_rsv; |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 350 | |
Filipe Manana | 5893dfb | 2020-11-04 11:07:32 +0000 | [diff] [blame] | 351 | drop_args.path = path; |
| 352 | drop_args.start = start; |
| 353 | drop_args.end = aligned_end; |
| 354 | drop_args.drop_cache = true; |
| 355 | drop_args.replace_extent = true; |
| 356 | |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 357 | if (compressed_size && compressed_pages) |
Filipe Manana | 5893dfb | 2020-11-04 11:07:32 +0000 | [diff] [blame] | 358 | 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] | 359 | compressed_size); |
| 360 | else |
Filipe Manana | 5893dfb | 2020-11-04 11:07:32 +0000 | [diff] [blame] | 361 | 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] | 362 | inline_len); |
| 363 | |
Filipe Manana | 5893dfb | 2020-11-04 11:07:32 +0000 | [diff] [blame] | 364 | ret = btrfs_drop_extents(trans, root, inode, &drop_args); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 365 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 366 | btrfs_abort_transaction(trans, ret); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 367 | goto out; |
| 368 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 369 | |
| 370 | if (isize > actual_end) |
| 371 | inline_len = min_t(u64, isize, actual_end); |
Filipe Manana | 5893dfb | 2020-11-04 11:07:32 +0000 | [diff] [blame] | 372 | ret = insert_inline_extent(trans, path, drop_args.extent_inserted, |
Nikolay Borisov | a034940 | 2020-06-03 08:55:12 +0300 | [diff] [blame] | 373 | root, &inode->vfs_inode, start, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 374 | inline_len, compressed_size, |
Li Zefan | fe3f566 | 2011-03-28 08:30:38 +0000 | [diff] [blame] | 375 | compress_type, compressed_pages); |
Josef Bacik | 2adcac1 | 2012-05-23 16:10:14 -0400 | [diff] [blame] | 376 | if (ret && ret != -ENOSPC) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 377 | btrfs_abort_transaction(trans, ret); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 378 | goto out; |
Josef Bacik | 2adcac1 | 2012-05-23 16:10:14 -0400 | [diff] [blame] | 379 | } else if (ret == -ENOSPC) { |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 380 | ret = 1; |
| 381 | goto out; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 382 | } |
Josef Bacik | 2adcac1 | 2012-05-23 16:10:14 -0400 | [diff] [blame] | 383 | |
Filipe Manana | 2766ff6 | 2020-11-04 11:07:34 +0000 | [diff] [blame] | 384 | btrfs_update_inode_bytes(inode, inline_len, drop_args.bytes_found); |
Nikolay Borisov | 9a56fcd | 2020-11-02 16:48:59 +0200 | [diff] [blame] | 385 | ret = btrfs_update_inode(trans, root, inode); |
Filipe Manana | 2766ff6 | 2020-11-04 11:07:34 +0000 | [diff] [blame] | 386 | if (ret && ret != -ENOSPC) { |
| 387 | btrfs_abort_transaction(trans, ret); |
| 388 | goto out; |
| 389 | } else if (ret == -ENOSPC) { |
| 390 | ret = 1; |
| 391 | goto out; |
| 392 | } |
| 393 | |
Nikolay Borisov | a034940 | 2020-06-03 08:55:12 +0300 | [diff] [blame] | 394 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &inode->runtime_flags); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 395 | out: |
Qu Wenruo | 94ed938 | 2015-09-08 17:25:56 +0800 | [diff] [blame] | 396 | /* |
| 397 | * Don't forget to free the reserved space, as for inlined extent |
| 398 | * it won't count as data extent, free them directly here. |
| 399 | * And at reserve time, it's always aligned to page size, so |
| 400 | * just free one page here. |
| 401 | */ |
Nikolay Borisov | a034940 | 2020-06-03 08:55:12 +0300 | [diff] [blame] | 402 | btrfs_qgroup_free_data(inode, NULL, 0, PAGE_SIZE); |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 403 | btrfs_free_path(path); |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 404 | btrfs_end_transaction(trans); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 405 | return ret; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 406 | } |
| 407 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 408 | struct async_extent { |
| 409 | u64 start; |
| 410 | u64 ram_size; |
| 411 | u64 compressed_size; |
| 412 | struct page **pages; |
| 413 | unsigned long nr_pages; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 414 | int compress_type; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 415 | struct list_head list; |
| 416 | }; |
| 417 | |
Nikolay Borisov | 97db120 | 2019-03-12 17:20:24 +0200 | [diff] [blame] | 418 | struct async_chunk { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 419 | struct inode *inode; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 420 | struct page *locked_page; |
| 421 | u64 start; |
| 422 | u64 end; |
Liu Bo | f82b735 | 2017-10-23 23:18:16 -0600 | [diff] [blame] | 423 | unsigned int write_flags; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 424 | struct list_head extents; |
Chris Mason | ec39f76 | 2019-07-10 12:28:17 -0700 | [diff] [blame] | 425 | struct cgroup_subsys_state *blkcg_css; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 426 | struct btrfs_work work; |
Nikolay Borisov | 97db120 | 2019-03-12 17:20:24 +0200 | [diff] [blame] | 427 | atomic_t *pending; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 428 | }; |
| 429 | |
Nikolay Borisov | 97db120 | 2019-03-12 17:20:24 +0200 | [diff] [blame] | 430 | struct async_cow { |
| 431 | /* Number of chunks in flight; must be first in the structure */ |
| 432 | atomic_t num_chunks; |
| 433 | struct async_chunk chunks[]; |
| 434 | }; |
| 435 | |
| 436 | static noinline int add_async_extent(struct async_chunk *cow, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 437 | u64 start, u64 ram_size, |
| 438 | u64 compressed_size, |
| 439 | struct page **pages, |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 440 | unsigned long nr_pages, |
| 441 | int compress_type) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 442 | { |
| 443 | struct async_extent *async_extent; |
| 444 | |
| 445 | async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 446 | BUG_ON(!async_extent); /* -ENOMEM */ |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 447 | async_extent->start = start; |
| 448 | async_extent->ram_size = ram_size; |
| 449 | async_extent->compressed_size = compressed_size; |
| 450 | async_extent->pages = pages; |
| 451 | async_extent->nr_pages = nr_pages; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 452 | async_extent->compress_type = compress_type; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 453 | list_add_tail(&async_extent->list, &cow->extents); |
| 454 | return 0; |
| 455 | } |
| 456 | |
Qu Wenruo | 42c16da | 2019-07-01 05:12:46 +0000 | [diff] [blame] | 457 | /* |
| 458 | * Check if the inode has flags compatible with compression |
| 459 | */ |
Nikolay Borisov | 99c88dc | 2020-06-03 08:55:26 +0300 | [diff] [blame] | 460 | static inline bool inode_can_compress(struct btrfs_inode *inode) |
Qu Wenruo | 42c16da | 2019-07-01 05:12:46 +0000 | [diff] [blame] | 461 | { |
Nikolay Borisov | 99c88dc | 2020-06-03 08:55:26 +0300 | [diff] [blame] | 462 | if (inode->flags & BTRFS_INODE_NODATACOW || |
| 463 | inode->flags & BTRFS_INODE_NODATASUM) |
Qu Wenruo | 42c16da | 2019-07-01 05:12:46 +0000 | [diff] [blame] | 464 | return false; |
| 465 | return true; |
| 466 | } |
| 467 | |
| 468 | /* |
| 469 | * Check if the inode needs to be submitted to compression, based on mount |
| 470 | * options, defragmentation, properties or heuristics. |
| 471 | */ |
Nikolay Borisov | 808a129 | 2020-06-03 08:55:27 +0300 | [diff] [blame] | 472 | static inline int inode_need_compress(struct btrfs_inode *inode, u64 start, |
| 473 | u64 end) |
Wang Shilong | f79707b | 2014-07-17 11:44:09 +0800 | [diff] [blame] | 474 | { |
Nikolay Borisov | 808a129 | 2020-06-03 08:55:27 +0300 | [diff] [blame] | 475 | struct btrfs_fs_info *fs_info = inode->root->fs_info; |
Wang Shilong | f79707b | 2014-07-17 11:44:09 +0800 | [diff] [blame] | 476 | |
Nikolay Borisov | 808a129 | 2020-06-03 08:55:27 +0300 | [diff] [blame] | 477 | if (!inode_can_compress(inode)) { |
Qu Wenruo | 42c16da | 2019-07-01 05:12:46 +0000 | [diff] [blame] | 478 | WARN(IS_ENABLED(CONFIG_BTRFS_DEBUG), |
| 479 | KERN_ERR "BTRFS: unexpected compression for ino %llu\n", |
Nikolay Borisov | 808a129 | 2020-06-03 08:55:27 +0300 | [diff] [blame] | 480 | btrfs_ino(inode)); |
Qu Wenruo | 42c16da | 2019-07-01 05:12:46 +0000 | [diff] [blame] | 481 | return 0; |
| 482 | } |
Wang Shilong | f79707b | 2014-07-17 11:44:09 +0800 | [diff] [blame] | 483 | /* force compress */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 484 | if (btrfs_test_opt(fs_info, FORCE_COMPRESS)) |
Wang Shilong | f79707b | 2014-07-17 11:44:09 +0800 | [diff] [blame] | 485 | return 1; |
David Sterba | eec63c6 | 2017-07-17 19:41:31 +0200 | [diff] [blame] | 486 | /* defrag ioctl */ |
Nikolay Borisov | 808a129 | 2020-06-03 08:55:27 +0300 | [diff] [blame] | 487 | if (inode->defrag_compress) |
David Sterba | eec63c6 | 2017-07-17 19:41:31 +0200 | [diff] [blame] | 488 | return 1; |
Wang Shilong | f79707b | 2014-07-17 11:44:09 +0800 | [diff] [blame] | 489 | /* bad compression ratios */ |
Nikolay Borisov | 808a129 | 2020-06-03 08:55:27 +0300 | [diff] [blame] | 490 | if (inode->flags & BTRFS_INODE_NOCOMPRESS) |
Wang Shilong | f79707b | 2014-07-17 11:44:09 +0800 | [diff] [blame] | 491 | return 0; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 492 | if (btrfs_test_opt(fs_info, COMPRESS) || |
Nikolay Borisov | 808a129 | 2020-06-03 08:55:27 +0300 | [diff] [blame] | 493 | inode->flags & BTRFS_INODE_COMPRESS || |
| 494 | inode->prop_compress) |
| 495 | return btrfs_compress_heuristic(&inode->vfs_inode, start, end); |
Wang Shilong | f79707b | 2014-07-17 11:44:09 +0800 | [diff] [blame] | 496 | return 0; |
| 497 | } |
| 498 | |
Nikolay Borisov | 6158e1c | 2017-02-20 13:50:43 +0200 | [diff] [blame] | 499 | static inline void inode_should_defrag(struct btrfs_inode *inode, |
Anand Jain | 26d30f8 | 2016-12-19 19:09:06 +0800 | [diff] [blame] | 500 | u64 start, u64 end, u64 num_bytes, u64 small_write) |
| 501 | { |
| 502 | /* If this is a small write inside eof, kick off a defrag */ |
| 503 | if (num_bytes < small_write && |
Nikolay Borisov | 6158e1c | 2017-02-20 13:50:43 +0200 | [diff] [blame] | 504 | (start > 0 || end + 1 < inode->disk_i_size)) |
Anand Jain | 26d30f8 | 2016-12-19 19:09:06 +0800 | [diff] [blame] | 505 | btrfs_add_inode_defrag(NULL, inode); |
| 506 | } |
| 507 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 508 | /* |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 509 | * we create compressed extents in two phases. The first |
| 510 | * phase compresses a range of pages that have already been |
| 511 | * locked (both pages and state bits are locked). |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 512 | * |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 513 | * This is done inside an ordered work queue, and the compression |
| 514 | * is spread across many cpus. The actual IO submission is step |
| 515 | * two, and the ordered work queue takes care of making sure that |
| 516 | * happens in the same order things were put onto the queue by |
| 517 | * writepages and friends. |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 518 | * |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 519 | * If this code finds it can't get good compression, it puts an |
| 520 | * entry onto the work queue to write the uncompressed bytes. This |
| 521 | * makes sure that both compressed inodes and uncompressed inodes |
Artem Bityutskiy | b257031 | 2012-07-25 18:12:06 +0300 | [diff] [blame] | 522 | * are written in the same order that the flusher thread sent them |
| 523 | * down. |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 524 | */ |
Nikolay Borisov | ac3e993 | 2019-07-17 14:41:44 +0300 | [diff] [blame] | 525 | static noinline int compress_file_range(struct async_chunk *async_chunk) |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 526 | { |
Nikolay Borisov | 1368c6d | 2019-03-12 17:20:27 +0200 | [diff] [blame] | 527 | struct inode *inode = async_chunk->inode; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 528 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 529 | u64 blocksize = fs_info->sectorsize; |
Nikolay Borisov | 1368c6d | 2019-03-12 17:20:27 +0200 | [diff] [blame] | 530 | u64 start = async_chunk->start; |
| 531 | u64 end = async_chunk->end; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 532 | u64 actual_end; |
Josef Bacik | d98da49 | 2019-10-11 09:03:54 -0400 | [diff] [blame] | 533 | u64 i_size; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 534 | int ret = 0; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 535 | struct page **pages = NULL; |
| 536 | unsigned long nr_pages; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 537 | unsigned long total_compressed = 0; |
| 538 | unsigned long total_in = 0; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 539 | int i; |
| 540 | int will_compress; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 541 | int compress_type = fs_info->compress_type; |
Nikolay Borisov | ac3e993 | 2019-07-17 14:41:44 +0300 | [diff] [blame] | 542 | int compressed_extents = 0; |
Chris Mason | 4adaa61 | 2013-03-26 13:07:00 -0400 | [diff] [blame] | 543 | int redirty = 0; |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 544 | |
Nikolay Borisov | 6158e1c | 2017-02-20 13:50:43 +0200 | [diff] [blame] | 545 | inode_should_defrag(BTRFS_I(inode), start, end, end - start + 1, |
| 546 | SZ_16K); |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 547 | |
Josef Bacik | d98da49 | 2019-10-11 09:03:54 -0400 | [diff] [blame] | 548 | /* |
| 549 | * We need to save i_size before now because it could change in between |
| 550 | * us evaluating the size and assigning it. This is because we lock and |
| 551 | * unlock the page in truncate and fallocate, and then modify the i_size |
| 552 | * later on. |
| 553 | * |
| 554 | * The barriers are to emulate READ_ONCE, remove that once i_size_read |
| 555 | * does that for us. |
| 556 | */ |
| 557 | barrier(); |
| 558 | i_size = i_size_read(inode); |
| 559 | barrier(); |
| 560 | actual_end = min_t(u64, i_size, end + 1); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 561 | again: |
| 562 | will_compress = 0; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 563 | nr_pages = (end >> PAGE_SHIFT) - (start >> PAGE_SHIFT) + 1; |
David Sterba | 069eac7 | 2017-02-14 19:36:54 +0100 | [diff] [blame] | 564 | BUILD_BUG_ON((BTRFS_MAX_COMPRESSED % PAGE_SIZE) != 0); |
| 565 | nr_pages = min_t(unsigned long, nr_pages, |
| 566 | BTRFS_MAX_COMPRESSED / PAGE_SIZE); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 567 | |
Chris Mason | f03d9301f | 2009-02-04 09:31:06 -0500 | [diff] [blame] | 568 | /* |
| 569 | * we don't want to send crud past the end of i_size through |
| 570 | * compression, that's just a waste of CPU time. So, if the |
| 571 | * end of the file is before the start of our current |
| 572 | * requested range of bytes, we bail out to the uncompressed |
| 573 | * cleanup code that can deal with all of this. |
| 574 | * |
| 575 | * It isn't really the fastest way to fix things, but this is a |
| 576 | * very uncommon corner. |
| 577 | */ |
| 578 | if (actual_end <= start) |
| 579 | goto cleanup_and_bail_uncompressed; |
| 580 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 581 | total_compressed = actual_end - start; |
| 582 | |
Shilong Wang | 4bcbb33 | 2014-10-07 18:44:35 -0400 | [diff] [blame] | 583 | /* |
| 584 | * skip compression for a small file range(<=blocksize) that |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 585 | * 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] | 586 | */ |
| 587 | if (total_compressed <= blocksize && |
| 588 | (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size)) |
| 589 | goto cleanup_and_bail_uncompressed; |
| 590 | |
David Sterba | 069eac7 | 2017-02-14 19:36:54 +0100 | [diff] [blame] | 591 | total_compressed = min_t(unsigned long, total_compressed, |
| 592 | BTRFS_MAX_UNCOMPRESSED); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 593 | total_in = 0; |
| 594 | ret = 0; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 595 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 596 | /* |
| 597 | * we do compression for mount -o compress and when the |
| 598 | * inode has not been flagged as nocompress. This flag can |
| 599 | * change at any time if we discover bad compression ratios. |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 600 | */ |
Nikolay Borisov | 808a129 | 2020-06-03 08:55:27 +0300 | [diff] [blame] | 601 | if (inode_need_compress(BTRFS_I(inode), start, end)) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 602 | WARN_ON(pages); |
David Sterba | 31e818f | 2015-02-20 18:00:26 +0100 | [diff] [blame] | 603 | pages = kcalloc(nr_pages, sizeof(struct page *), GFP_NOFS); |
Li Zefan | 560f7d7 | 2011-09-08 10:22:01 +0800 | [diff] [blame] | 604 | if (!pages) { |
| 605 | /* just bail out to the uncompressed code */ |
Filipe Manana | 3527a01 | 2018-10-13 00:37:25 +0100 | [diff] [blame] | 606 | nr_pages = 0; |
Li Zefan | 560f7d7 | 2011-09-08 10:22:01 +0800 | [diff] [blame] | 607 | goto cont; |
| 608 | } |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 609 | |
David Sterba | eec63c6 | 2017-07-17 19:41:31 +0200 | [diff] [blame] | 610 | if (BTRFS_I(inode)->defrag_compress) |
| 611 | compress_type = BTRFS_I(inode)->defrag_compress; |
| 612 | else if (BTRFS_I(inode)->prop_compress) |
David Sterba | b52aa8c | 2017-07-17 19:17:20 +0200 | [diff] [blame] | 613 | compress_type = BTRFS_I(inode)->prop_compress; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 614 | |
Chris Mason | 4adaa61 | 2013-03-26 13:07:00 -0400 | [diff] [blame] | 615 | /* |
| 616 | * we need to call clear_page_dirty_for_io on each |
| 617 | * page in the range. Otherwise applications with the file |
| 618 | * mmap'd can wander in and change the page contents while |
| 619 | * we are compressing them. |
| 620 | * |
| 621 | * If the compression fails for any reason, we set the pages |
| 622 | * dirty again later on. |
Timofey Titovets | e9679de | 2017-10-24 01:29:48 +0300 | [diff] [blame] | 623 | * |
| 624 | * Note that the remaining part is redirtied, the start pointer |
| 625 | * has moved, the end is the original one. |
Chris Mason | 4adaa61 | 2013-03-26 13:07:00 -0400 | [diff] [blame] | 626 | */ |
Timofey Titovets | e9679de | 2017-10-24 01:29:48 +0300 | [diff] [blame] | 627 | if (!redirty) { |
| 628 | extent_range_clear_dirty_for_io(inode, start, end); |
| 629 | redirty = 1; |
| 630 | } |
David Sterba | f51d2b5 | 2017-09-15 17:36:57 +0200 | [diff] [blame] | 631 | |
| 632 | /* Compression level is applied here and only here */ |
| 633 | ret = btrfs_compress_pages( |
| 634 | compress_type | (fs_info->compress_level << 4), |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 635 | inode->i_mapping, start, |
David Sterba | 38c3146 | 2017-02-14 19:04:07 +0100 | [diff] [blame] | 636 | pages, |
David Sterba | 4d3a800 | 2017-02-14 19:04:07 +0100 | [diff] [blame] | 637 | &nr_pages, |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 638 | &total_in, |
David Sterba | e5d7490 | 2017-02-14 19:45:05 +0100 | [diff] [blame] | 639 | &total_compressed); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 640 | |
| 641 | if (!ret) { |
Johannes Thumshirn | 7073017 | 2018-12-05 15:23:03 +0100 | [diff] [blame] | 642 | unsigned long offset = offset_in_page(total_compressed); |
David Sterba | 4d3a800 | 2017-02-14 19:04:07 +0100 | [diff] [blame] | 643 | struct page *page = pages[nr_pages - 1]; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 644 | char *kaddr; |
| 645 | |
| 646 | /* zero the tail end of the last page, we might be |
| 647 | * sending it down to disk |
| 648 | */ |
| 649 | if (offset) { |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 650 | kaddr = kmap_atomic(page); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 651 | memset(kaddr + offset, 0, |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 652 | PAGE_SIZE - offset); |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 653 | kunmap_atomic(kaddr); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 654 | } |
| 655 | will_compress = 1; |
| 656 | } |
| 657 | } |
Li Zefan | 560f7d7 | 2011-09-08 10:22:01 +0800 | [diff] [blame] | 658 | cont: |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 659 | if (start == 0) { |
| 660 | /* lets try to make an inline extent */ |
Timofey Titovets | 6018ba0 | 2017-09-15 01:57:26 +0300 | [diff] [blame] | 661 | if (ret || total_in < actual_end) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 662 | /* we didn't compress the entire range, try |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 663 | * to make an uncompressed inline extent. |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 664 | */ |
Nikolay Borisov | a034940 | 2020-06-03 08:55:12 +0300 | [diff] [blame] | 665 | ret = cow_file_range_inline(BTRFS_I(inode), start, end, |
| 666 | 0, BTRFS_COMPRESS_NONE, |
| 667 | NULL); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 668 | } else { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 669 | /* try making a compressed inline extent */ |
Nikolay Borisov | a034940 | 2020-06-03 08:55:12 +0300 | [diff] [blame] | 670 | ret = cow_file_range_inline(BTRFS_I(inode), start, end, |
Li Zefan | fe3f566 | 2011-03-28 08:30:38 +0000 | [diff] [blame] | 671 | total_compressed, |
| 672 | compress_type, pages); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 673 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 674 | if (ret <= 0) { |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 675 | unsigned long clear_flags = EXTENT_DELALLOC | |
Josef Bacik | 8b62f87 | 2017-10-19 14:15:55 -0400 | [diff] [blame] | 676 | EXTENT_DELALLOC_NEW | EXTENT_DEFRAG | |
| 677 | EXTENT_DO_ACCOUNTING; |
Filipe Manana | e6eb431 | 2014-10-10 10:45:12 +0100 | [diff] [blame] | 678 | unsigned long page_error_op; |
| 679 | |
Filipe Manana | e6eb431 | 2014-10-10 10:45:12 +0100 | [diff] [blame] | 680 | page_error_op = ret < 0 ? PAGE_SET_ERROR : 0; |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 681 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 682 | /* |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 683 | * inline extent creation worked or returned error, |
| 684 | * we don't need to create any more async work items. |
| 685 | * Unlock and free up our temp pages. |
Josef Bacik | 8b62f87 | 2017-10-19 14:15:55 -0400 | [diff] [blame] | 686 | * |
| 687 | * We use DO_ACCOUNTING here because we need the |
| 688 | * delalloc_release_metadata to be done _after_ we drop |
| 689 | * our outstanding extent for clearing delalloc for this |
| 690 | * range. |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 691 | */ |
Nikolay Borisov | ad7ff17 | 2020-06-03 08:55:06 +0300 | [diff] [blame] | 692 | extent_clear_unlock_delalloc(BTRFS_I(inode), start, end, |
| 693 | NULL, |
Nikolay Borisov | 74e9194 | 2019-07-17 16:18:16 +0300 | [diff] [blame] | 694 | clear_flags, |
Qu Wenruo | ba8b04c | 2016-07-19 16:50:36 +0800 | [diff] [blame] | 695 | PAGE_UNLOCK | |
Qu Wenruo | 6869b0a | 2021-01-26 16:33:45 +0800 | [diff] [blame] | 696 | PAGE_START_WRITEBACK | |
Filipe Manana | e6eb431 | 2014-10-10 10:45:12 +0100 | [diff] [blame] | 697 | page_error_op | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 698 | PAGE_END_WRITEBACK); |
Nikolay Borisov | cecc8d9 | 2019-07-17 14:41:45 +0300 | [diff] [blame] | 699 | |
Qu Wenruo | 1e6e238 | 2020-07-28 16:39:26 +0800 | [diff] [blame] | 700 | /* |
| 701 | * Ensure we only free the compressed pages if we have |
| 702 | * them allocated, as we can still reach here with |
| 703 | * inode_need_compress() == false. |
| 704 | */ |
| 705 | if (pages) { |
| 706 | for (i = 0; i < nr_pages; i++) { |
| 707 | WARN_ON(pages[i]->mapping); |
| 708 | put_page(pages[i]); |
| 709 | } |
| 710 | kfree(pages); |
Nikolay Borisov | cecc8d9 | 2019-07-17 14:41:45 +0300 | [diff] [blame] | 711 | } |
Nikolay Borisov | cecc8d9 | 2019-07-17 14:41:45 +0300 | [diff] [blame] | 712 | return 0; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 713 | } |
| 714 | } |
| 715 | |
| 716 | if (will_compress) { |
| 717 | /* |
| 718 | * we aren't doing an inline extent round the compressed size |
| 719 | * up to a block size boundary so the allocator does sane |
| 720 | * things |
| 721 | */ |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 722 | total_compressed = ALIGN(total_compressed, blocksize); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 723 | |
| 724 | /* |
| 725 | * one last check to make sure the compression is really a |
Timofey Titovets | 170607e | 2017-06-06 14:41:15 +0300 | [diff] [blame] | 726 | * win, compare the page count read with the blocks on disk, |
| 727 | * compression must free at least one sector size |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 728 | */ |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 729 | total_in = ALIGN(total_in, PAGE_SIZE); |
Timofey Titovets | 170607e | 2017-06-06 14:41:15 +0300 | [diff] [blame] | 730 | if (total_compressed + blocksize <= total_in) { |
Nikolay Borisov | ac3e993 | 2019-07-17 14:41:44 +0300 | [diff] [blame] | 731 | compressed_extents++; |
Ashish Samant | c8bb0c8 | 2016-03-25 19:01:33 -0700 | [diff] [blame] | 732 | |
| 733 | /* |
| 734 | * The async work queues will take care of doing actual |
| 735 | * allocation on disk for these compressed pages, and |
| 736 | * will submit them to the elevator. |
| 737 | */ |
Nikolay Borisov | b532627 | 2019-03-12 17:20:25 +0200 | [diff] [blame] | 738 | add_async_extent(async_chunk, start, total_in, |
David Sterba | 4d3a800 | 2017-02-14 19:04:07 +0100 | [diff] [blame] | 739 | total_compressed, pages, nr_pages, |
Ashish Samant | c8bb0c8 | 2016-03-25 19:01:33 -0700 | [diff] [blame] | 740 | compress_type); |
| 741 | |
Timofey Titovets | 1170862 | 2017-10-03 18:06:01 +0300 | [diff] [blame] | 742 | if (start + total_in < end) { |
| 743 | start += total_in; |
Ashish Samant | c8bb0c8 | 2016-03-25 19:01:33 -0700 | [diff] [blame] | 744 | pages = NULL; |
| 745 | cond_resched(); |
| 746 | goto again; |
| 747 | } |
Nikolay Borisov | ac3e993 | 2019-07-17 14:41:44 +0300 | [diff] [blame] | 748 | return compressed_extents; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 749 | } |
| 750 | } |
Ashish Samant | c8bb0c8 | 2016-03-25 19:01:33 -0700 | [diff] [blame] | 751 | if (pages) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 752 | /* |
| 753 | * the compression code ran but failed to make things smaller, |
| 754 | * free any pages it allocated and our page pointer array |
| 755 | */ |
David Sterba | 4d3a800 | 2017-02-14 19:04:07 +0100 | [diff] [blame] | 756 | for (i = 0; i < nr_pages; i++) { |
Chris Mason | 70b99e6 | 2008-10-31 12:46:39 -0400 | [diff] [blame] | 757 | WARN_ON(pages[i]->mapping); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 758 | put_page(pages[i]); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 759 | } |
| 760 | kfree(pages); |
| 761 | pages = NULL; |
| 762 | total_compressed = 0; |
David Sterba | 4d3a800 | 2017-02-14 19:04:07 +0100 | [diff] [blame] | 763 | nr_pages = 0; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 764 | |
| 765 | /* flag the file so we don't compress in the future */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 766 | if (!btrfs_test_opt(fs_info, FORCE_COMPRESS) && |
David Sterba | b52aa8c | 2017-07-17 19:17:20 +0200 | [diff] [blame] | 767 | !(BTRFS_I(inode)->prop_compress)) { |
Chris Mason | a555f81 | 2010-01-28 16:18:15 -0500 | [diff] [blame] | 768 | BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS; |
Chris Mason | 1e701a3 | 2010-03-11 09:42:04 -0500 | [diff] [blame] | 769 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 770 | } |
Chris Mason | f03d9301f | 2009-02-04 09:31:06 -0500 | [diff] [blame] | 771 | cleanup_and_bail_uncompressed: |
Ashish Samant | c8bb0c8 | 2016-03-25 19:01:33 -0700 | [diff] [blame] | 772 | /* |
| 773 | * No compression, but we still need to write the pages in the file |
| 774 | * we've been given so far. redirty the locked page if it corresponds |
| 775 | * to our extent and set things up for the async work queue to run |
| 776 | * cow_file_range to do the normal delalloc dance. |
| 777 | */ |
Chris Mason | 1d53c9e | 2019-07-10 12:28:16 -0700 | [diff] [blame] | 778 | if (async_chunk->locked_page && |
| 779 | (page_offset(async_chunk->locked_page) >= start && |
| 780 | page_offset(async_chunk->locked_page)) <= end) { |
Nikolay Borisov | 1368c6d | 2019-03-12 17:20:27 +0200 | [diff] [blame] | 781 | __set_page_dirty_nobuffers(async_chunk->locked_page); |
Ashish Samant | c8bb0c8 | 2016-03-25 19:01:33 -0700 | [diff] [blame] | 782 | /* unlocked later on in the async handlers */ |
Chris Mason | 1d53c9e | 2019-07-10 12:28:16 -0700 | [diff] [blame] | 783 | } |
Ashish Samant | c8bb0c8 | 2016-03-25 19:01:33 -0700 | [diff] [blame] | 784 | |
| 785 | if (redirty) |
| 786 | extent_range_redirty_for_io(inode, start, end); |
Nikolay Borisov | b532627 | 2019-03-12 17:20:25 +0200 | [diff] [blame] | 787 | add_async_extent(async_chunk, start, end - start + 1, 0, NULL, 0, |
Ashish Samant | c8bb0c8 | 2016-03-25 19:01:33 -0700 | [diff] [blame] | 788 | BTRFS_COMPRESS_NONE); |
Nikolay Borisov | ac3e993 | 2019-07-17 14:41:44 +0300 | [diff] [blame] | 789 | compressed_extents++; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 790 | |
Nikolay Borisov | ac3e993 | 2019-07-17 14:41:44 +0300 | [diff] [blame] | 791 | return compressed_extents; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 792 | } |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 793 | |
Filipe Manana | 40ae837 | 2014-10-06 22:14:24 +0100 | [diff] [blame] | 794 | static void free_async_extent_pages(struct async_extent *async_extent) |
| 795 | { |
| 796 | int i; |
| 797 | |
| 798 | if (!async_extent->pages) |
| 799 | return; |
| 800 | |
| 801 | for (i = 0; i < async_extent->nr_pages; i++) { |
| 802 | WARN_ON(async_extent->pages[i]->mapping); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 803 | put_page(async_extent->pages[i]); |
Filipe Manana | 40ae837 | 2014-10-06 22:14:24 +0100 | [diff] [blame] | 804 | } |
| 805 | kfree(async_extent->pages); |
| 806 | async_extent->nr_pages = 0; |
| 807 | async_extent->pages = NULL; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 808 | } |
| 809 | |
| 810 | /* |
| 811 | * phase two of compressed writeback. This is the ordered portion |
| 812 | * of the code, which only gets called in the order the work was |
| 813 | * queued. We walk all the async extents created by compress_file_range |
| 814 | * and send them down to the disk. |
| 815 | */ |
Nikolay Borisov | b532627 | 2019-03-12 17:20:25 +0200 | [diff] [blame] | 816 | static noinline void submit_compressed_extents(struct async_chunk *async_chunk) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 817 | { |
Nikolay Borisov | a0ff10d | 2020-06-03 08:55:17 +0300 | [diff] [blame] | 818 | struct btrfs_inode *inode = BTRFS_I(async_chunk->inode); |
| 819 | struct btrfs_fs_info *fs_info = inode->root->fs_info; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 820 | struct async_extent *async_extent; |
| 821 | u64 alloc_hint = 0; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 822 | struct btrfs_key ins; |
| 823 | struct extent_map *em; |
Nikolay Borisov | a0ff10d | 2020-06-03 08:55:17 +0300 | [diff] [blame] | 824 | struct btrfs_root *root = inode->root; |
| 825 | struct extent_io_tree *io_tree = &inode->io_tree; |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 826 | int ret = 0; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 827 | |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 828 | again: |
Nikolay Borisov | b532627 | 2019-03-12 17:20:25 +0200 | [diff] [blame] | 829 | while (!list_empty(&async_chunk->extents)) { |
| 830 | async_extent = list_entry(async_chunk->extents.next, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 831 | struct async_extent, list); |
| 832 | list_del(&async_extent->list); |
| 833 | |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 834 | retry: |
Nikolay Borisov | 7447555 | 2019-03-12 17:20:30 +0200 | [diff] [blame] | 835 | lock_extent(io_tree, async_extent->start, |
| 836 | async_extent->start + async_extent->ram_size - 1); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 837 | /* did the compression code fall back to uncompressed IO? */ |
| 838 | if (!async_extent->pages) { |
| 839 | int page_started = 0; |
| 840 | unsigned long nr_written = 0; |
| 841 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 842 | /* allocate blocks */ |
Nikolay Borisov | a0ff10d | 2020-06-03 08:55:17 +0300 | [diff] [blame] | 843 | ret = cow_file_range(inode, async_chunk->locked_page, |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 844 | async_extent->start, |
| 845 | async_extent->start + |
| 846 | async_extent->ram_size - 1, |
Nikolay Borisov | 330a582 | 2019-07-17 16:18:17 +0300 | [diff] [blame] | 847 | &page_started, &nr_written, 0); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 848 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 849 | /* JDM XXX */ |
| 850 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 851 | /* |
| 852 | * if page_started, cow_file_range inserted an |
| 853 | * inline extent and took care of all the unlocking |
| 854 | * and IO for us. Otherwise, we need to submit |
| 855 | * all those pages down to the drive. |
| 856 | */ |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 857 | if (!page_started && !ret) |
Nikolay Borisov | a0ff10d | 2020-06-03 08:55:17 +0300 | [diff] [blame] | 858 | extent_write_locked_range(&inode->vfs_inode, |
Nikolay Borisov | 5e3ee23 | 2017-12-08 15:55:58 +0200 | [diff] [blame] | 859 | async_extent->start, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 860 | async_extent->start + |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 861 | async_extent->ram_size - 1, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 862 | WB_SYNC_ALL); |
Chris Mason | 1d53c9e | 2019-07-10 12:28:16 -0700 | [diff] [blame] | 863 | else if (ret && async_chunk->locked_page) |
Nikolay Borisov | b532627 | 2019-03-12 17:20:25 +0200 | [diff] [blame] | 864 | unlock_page(async_chunk->locked_page); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 865 | kfree(async_extent); |
| 866 | cond_resched(); |
| 867 | continue; |
| 868 | } |
| 869 | |
Wang Xiaoguang | 1851309 | 2016-07-25 15:51:40 +0800 | [diff] [blame] | 870 | ret = btrfs_reserve_extent(root, async_extent->ram_size, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 871 | async_extent->compressed_size, |
| 872 | async_extent->compressed_size, |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 873 | 0, alloc_hint, &ins, 1, 1); |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 874 | if (ret) { |
Filipe Manana | 40ae837 | 2014-10-06 22:14:24 +0100 | [diff] [blame] | 875 | free_async_extent_pages(async_extent); |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 876 | |
Josef Bacik | fdf8e2e | 2013-06-14 16:58:23 -0400 | [diff] [blame] | 877 | if (ret == -ENOSPC) { |
| 878 | unlock_extent(io_tree, async_extent->start, |
| 879 | async_extent->start + |
| 880 | async_extent->ram_size - 1); |
Liu Bo | ce62003 | 2014-07-24 22:48:05 +0800 | [diff] [blame] | 881 | |
| 882 | /* |
| 883 | * we need to redirty the pages if we decide to |
| 884 | * fallback to uncompressed IO, otherwise we |
| 885 | * will not submit these pages down to lower |
| 886 | * layers. |
| 887 | */ |
Nikolay Borisov | a0ff10d | 2020-06-03 08:55:17 +0300 | [diff] [blame] | 888 | extent_range_redirty_for_io(&inode->vfs_inode, |
Liu Bo | ce62003 | 2014-07-24 22:48:05 +0800 | [diff] [blame] | 889 | async_extent->start, |
| 890 | async_extent->start + |
| 891 | async_extent->ram_size - 1); |
| 892 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 893 | goto retry; |
Josef Bacik | fdf8e2e | 2013-06-14 16:58:23 -0400 | [diff] [blame] | 894 | } |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 895 | goto out_free; |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 896 | } |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 897 | /* |
| 898 | * here we're doing allocation and writeback of the |
| 899 | * compressed pages |
| 900 | */ |
Nikolay Borisov | a0ff10d | 2020-06-03 08:55:17 +0300 | [diff] [blame] | 901 | em = create_io_em(inode, async_extent->start, |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 902 | async_extent->ram_size, /* len */ |
| 903 | async_extent->start, /* orig_start */ |
| 904 | ins.objectid, /* block_start */ |
| 905 | ins.offset, /* block_len */ |
| 906 | ins.offset, /* orig_block_len */ |
| 907 | async_extent->ram_size, /* ram_bytes */ |
| 908 | async_extent->compress_type, |
| 909 | BTRFS_ORDERED_COMPRESSED); |
| 910 | if (IS_ERR(em)) |
| 911 | /* ret value is not necessary due to void function */ |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 912 | goto out_free_reserve; |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 913 | free_extent_map(em); |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 914 | |
Nikolay Borisov | a0ff10d | 2020-06-03 08:55:17 +0300 | [diff] [blame] | 915 | ret = btrfs_add_ordered_extent_compress(inode, |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 916 | async_extent->start, |
| 917 | ins.objectid, |
| 918 | async_extent->ram_size, |
| 919 | ins.offset, |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 920 | async_extent->compress_type); |
Filipe Manana | d9f8596 | 2014-08-25 10:43:00 +0100 | [diff] [blame] | 921 | if (ret) { |
Nikolay Borisov | a0ff10d | 2020-06-03 08:55:17 +0300 | [diff] [blame] | 922 | btrfs_drop_extent_cache(inode, async_extent->start, |
Filipe Manana | d9f8596 | 2014-08-25 10:43:00 +0100 | [diff] [blame] | 923 | async_extent->start + |
| 924 | async_extent->ram_size - 1, 0); |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 925 | goto out_free_reserve; |
Filipe Manana | d9f8596 | 2014-08-25 10:43:00 +0100 | [diff] [blame] | 926 | } |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 927 | btrfs_dec_block_group_reservations(fs_info, ins.objectid); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 928 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 929 | /* |
| 930 | * clear dirty, set writeback and unlock the pages. |
| 931 | */ |
Nikolay Borisov | a0ff10d | 2020-06-03 08:55:17 +0300 | [diff] [blame] | 932 | extent_clear_unlock_delalloc(inode, async_extent->start, |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 933 | async_extent->start + |
| 934 | async_extent->ram_size - 1, |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 935 | NULL, EXTENT_LOCKED | EXTENT_DELALLOC, |
Qu Wenruo | 6869b0a | 2021-01-26 16:33:45 +0800 | [diff] [blame] | 936 | PAGE_UNLOCK | PAGE_START_WRITEBACK); |
Nikolay Borisov | a0ff10d | 2020-06-03 08:55:17 +0300 | [diff] [blame] | 937 | if (btrfs_submit_compressed_write(inode, async_extent->start, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 938 | async_extent->ram_size, |
| 939 | ins.objectid, |
| 940 | ins.offset, async_extent->pages, |
Liu Bo | f82b735 | 2017-10-23 23:18:16 -0600 | [diff] [blame] | 941 | async_extent->nr_pages, |
Chris Mason | ec39f76 | 2019-07-10 12:28:17 -0700 | [diff] [blame] | 942 | async_chunk->write_flags, |
| 943 | async_chunk->blkcg_css)) { |
Filipe Manana | fce2a4e | 2014-10-06 22:14:23 +0100 | [diff] [blame] | 944 | struct page *p = async_extent->pages[0]; |
| 945 | const u64 start = async_extent->start; |
| 946 | const u64 end = start + async_extent->ram_size - 1; |
| 947 | |
Nikolay Borisov | a0ff10d | 2020-06-03 08:55:17 +0300 | [diff] [blame] | 948 | p->mapping = inode->vfs_inode.i_mapping; |
Nikolay Borisov | c629732 | 2018-11-08 10:18:08 +0200 | [diff] [blame] | 949 | btrfs_writepage_endio_finish_ordered(p, start, end, 0); |
Nikolay Borisov | 7087a9d | 2018-11-01 14:09:48 +0200 | [diff] [blame] | 950 | |
Filipe Manana | fce2a4e | 2014-10-06 22:14:23 +0100 | [diff] [blame] | 951 | p->mapping = NULL; |
Nikolay Borisov | a0ff10d | 2020-06-03 08:55:17 +0300 | [diff] [blame] | 952 | extent_clear_unlock_delalloc(inode, start, end, NULL, 0, |
Filipe Manana | fce2a4e | 2014-10-06 22:14:23 +0100 | [diff] [blame] | 953 | PAGE_END_WRITEBACK | |
| 954 | PAGE_SET_ERROR); |
Filipe Manana | 40ae837 | 2014-10-06 22:14:24 +0100 | [diff] [blame] | 955 | free_async_extent_pages(async_extent); |
Filipe Manana | fce2a4e | 2014-10-06 22:14:23 +0100 | [diff] [blame] | 956 | } |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 957 | alloc_hint = ins.objectid + ins.offset; |
| 958 | kfree(async_extent); |
| 959 | cond_resched(); |
| 960 | } |
Filipe Manana | dec8f17 | 2014-10-06 22:14:26 +0100 | [diff] [blame] | 961 | return; |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 962 | out_free_reserve: |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 963 | btrfs_dec_block_group_reservations(fs_info, ins.objectid); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 964 | btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 965 | out_free: |
Nikolay Borisov | a0ff10d | 2020-06-03 08:55:17 +0300 | [diff] [blame] | 966 | extent_clear_unlock_delalloc(inode, async_extent->start, |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 967 | async_extent->start + |
| 968 | async_extent->ram_size - 1, |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 969 | NULL, EXTENT_LOCKED | EXTENT_DELALLOC | |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 970 | EXTENT_DELALLOC_NEW | |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 971 | EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING, |
Qu Wenruo | 6869b0a | 2021-01-26 16:33:45 +0800 | [diff] [blame] | 972 | PAGE_UNLOCK | PAGE_START_WRITEBACK | |
| 973 | PAGE_END_WRITEBACK | PAGE_SET_ERROR); |
Filipe Manana | 40ae837 | 2014-10-06 22:14:24 +0100 | [diff] [blame] | 974 | free_async_extent_pages(async_extent); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 975 | kfree(async_extent); |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 976 | goto again; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 977 | } |
| 978 | |
Nikolay Borisov | 43c6984 | 2020-06-03 08:55:02 +0300 | [diff] [blame] | 979 | static u64 get_extent_allocation_hint(struct btrfs_inode *inode, u64 start, |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 980 | u64 num_bytes) |
| 981 | { |
Nikolay Borisov | 43c6984 | 2020-06-03 08:55:02 +0300 | [diff] [blame] | 982 | struct extent_map_tree *em_tree = &inode->extent_tree; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 983 | struct extent_map *em; |
| 984 | u64 alloc_hint = 0; |
| 985 | |
| 986 | read_lock(&em_tree->lock); |
| 987 | em = search_extent_mapping(em_tree, start, num_bytes); |
| 988 | if (em) { |
| 989 | /* |
| 990 | * if block start isn't an actual block number then find the |
| 991 | * first block in this inode and use that as a hint. If that |
| 992 | * block is also bogus then just don't worry about it. |
| 993 | */ |
| 994 | if (em->block_start >= EXTENT_MAP_LAST_BYTE) { |
| 995 | free_extent_map(em); |
| 996 | em = search_extent_mapping(em_tree, 0, 0); |
| 997 | if (em && em->block_start < EXTENT_MAP_LAST_BYTE) |
| 998 | alloc_hint = em->block_start; |
| 999 | if (em) |
| 1000 | free_extent_map(em); |
| 1001 | } else { |
| 1002 | alloc_hint = em->block_start; |
| 1003 | free_extent_map(em); |
| 1004 | } |
| 1005 | } |
| 1006 | read_unlock(&em_tree->lock); |
| 1007 | |
| 1008 | return alloc_hint; |
| 1009 | } |
| 1010 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1011 | /* |
| 1012 | * when extent_io.c finds a delayed allocation range in the file, |
| 1013 | * the call backs end up in this code. The basic idea is to |
| 1014 | * allocate extents on disk for the range, and create ordered data structs |
| 1015 | * in ram to track those extents. |
| 1016 | * |
| 1017 | * locked_page is the page that writepage had locked already. We use |
| 1018 | * it to make sure we don't do extra locks or unlocks. |
| 1019 | * |
| 1020 | * *page_started is set to one if we unlock locked_page and do everything |
| 1021 | * required to start IO on it. It may be clean and already done with |
| 1022 | * IO when we return. |
| 1023 | */ |
Nikolay Borisov | 6e26c44 | 2020-06-03 08:55:14 +0300 | [diff] [blame] | 1024 | static noinline int cow_file_range(struct btrfs_inode *inode, |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 1025 | struct page *locked_page, |
Nikolay Borisov | 74e9194 | 2019-07-17 16:18:16 +0300 | [diff] [blame] | 1026 | u64 start, u64 end, int *page_started, |
Nikolay Borisov | 330a582 | 2019-07-17 16:18:17 +0300 | [diff] [blame] | 1027 | unsigned long *nr_written, int unlock) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1028 | { |
Nikolay Borisov | 6e26c44 | 2020-06-03 08:55:14 +0300 | [diff] [blame] | 1029 | struct btrfs_root *root = inode->root; |
| 1030 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1031 | u64 alloc_hint = 0; |
| 1032 | u64 num_bytes; |
| 1033 | unsigned long ram_size; |
Filipe Manana | a315e68 | 2017-03-06 23:04:20 +0000 | [diff] [blame] | 1034 | u64 cur_alloc_size = 0; |
Filipe Manana | 432cd2a | 2020-06-08 13:32:55 +0100 | [diff] [blame] | 1035 | u64 min_alloc_size; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1036 | u64 blocksize = fs_info->sectorsize; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1037 | struct btrfs_key ins; |
| 1038 | struct extent_map *em; |
Filipe Manana | a315e68 | 2017-03-06 23:04:20 +0000 | [diff] [blame] | 1039 | unsigned clear_bits; |
| 1040 | unsigned long page_ops; |
| 1041 | bool extent_reserved = false; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1042 | int ret = 0; |
| 1043 | |
Nikolay Borisov | 6e26c44 | 2020-06-03 08:55:14 +0300 | [diff] [blame] | 1044 | if (btrfs_is_free_space_inode(inode)) { |
Josef Bacik | 02ecd2c | 2013-10-25 16:19:08 -0400 | [diff] [blame] | 1045 | WARN_ON_ONCE(1); |
Josef Bacik | 29bce2f | 2014-02-07 12:21:23 -0500 | [diff] [blame] | 1046 | ret = -EINVAL; |
| 1047 | goto out_unlock; |
Josef Bacik | 02ecd2c | 2013-10-25 16:19:08 -0400 | [diff] [blame] | 1048 | } |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1049 | |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 1050 | num_bytes = ALIGN(end - start + 1, blocksize); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1051 | num_bytes = max(blocksize, num_bytes); |
Anand Jain | 566b176 | 2018-02-15 18:07:59 +0800 | [diff] [blame] | 1052 | ASSERT(num_bytes <= btrfs_super_total_bytes(fs_info->super_copy)); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1053 | |
Nikolay Borisov | 6e26c44 | 2020-06-03 08:55:14 +0300 | [diff] [blame] | 1054 | inode_should_defrag(inode, start, end, num_bytes, SZ_64K); |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1055 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1056 | if (start == 0) { |
| 1057 | /* lets try to make an inline extent */ |
Nikolay Borisov | 6e26c44 | 2020-06-03 08:55:14 +0300 | [diff] [blame] | 1058 | ret = cow_file_range_inline(inode, start, end, 0, |
Nikolay Borisov | d02c0e2 | 2018-03-02 09:43:15 +0200 | [diff] [blame] | 1059 | BTRFS_COMPRESS_NONE, NULL); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1060 | if (ret == 0) { |
Josef Bacik | 8b62f87 | 2017-10-19 14:15:55 -0400 | [diff] [blame] | 1061 | /* |
| 1062 | * We use DO_ACCOUNTING here because we need the |
| 1063 | * delalloc_release_metadata to be run _after_ we drop |
| 1064 | * our outstanding extent for clearing delalloc for this |
| 1065 | * range. |
| 1066 | */ |
Nikolay Borisov | 6e26c44 | 2020-06-03 08:55:14 +0300 | [diff] [blame] | 1067 | extent_clear_unlock_delalloc(inode, start, end, NULL, |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1068 | EXTENT_LOCKED | EXTENT_DELALLOC | |
Josef Bacik | 8b62f87 | 2017-10-19 14:15:55 -0400 | [diff] [blame] | 1069 | EXTENT_DELALLOC_NEW | EXTENT_DEFRAG | |
| 1070 | EXTENT_DO_ACCOUNTING, PAGE_UNLOCK | |
Qu Wenruo | 6869b0a | 2021-01-26 16:33:45 +0800 | [diff] [blame] | 1071 | PAGE_START_WRITEBACK | PAGE_END_WRITEBACK); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1072 | *nr_written = *nr_written + |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1073 | (end - start + PAGE_SIZE) / PAGE_SIZE; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1074 | *page_started = 1; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1075 | goto out; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1076 | } else if (ret < 0) { |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1077 | goto out_unlock; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1078 | } |
| 1079 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1080 | |
Nikolay Borisov | 6e26c44 | 2020-06-03 08:55:14 +0300 | [diff] [blame] | 1081 | alloc_hint = get_extent_allocation_hint(inode, start, num_bytes); |
| 1082 | btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 1083 | |
Filipe Manana | 432cd2a | 2020-06-08 13:32:55 +0100 | [diff] [blame] | 1084 | /* |
| 1085 | * Relocation relies on the relocated extents to have exactly the same |
| 1086 | * size as the original extents. Normally writeback for relocation data |
| 1087 | * extents follows a NOCOW path because relocation preallocates the |
| 1088 | * extents. However, due to an operation such as scrub turning a block |
| 1089 | * group to RO mode, it may fallback to COW mode, so we must make sure |
| 1090 | * an extent allocated during COW has exactly the requested size and can |
| 1091 | * not be split into smaller extents, otherwise relocation breaks and |
| 1092 | * fails during the stage where it updates the bytenr of file extent |
| 1093 | * items. |
| 1094 | */ |
| 1095 | if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID) |
| 1096 | min_alloc_size = num_bytes; |
| 1097 | else |
| 1098 | min_alloc_size = fs_info->sectorsize; |
| 1099 | |
Anand Jain | 3752d22 | 2018-02-15 12:29:38 +0800 | [diff] [blame] | 1100 | while (num_bytes > 0) { |
| 1101 | cur_alloc_size = num_bytes; |
Wang Xiaoguang | 1851309 | 2016-07-25 15:51:40 +0800 | [diff] [blame] | 1102 | ret = btrfs_reserve_extent(root, cur_alloc_size, cur_alloc_size, |
Filipe Manana | 432cd2a | 2020-06-08 13:32:55 +0100 | [diff] [blame] | 1103 | min_alloc_size, 0, alloc_hint, |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 1104 | &ins, 1, 1); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 1105 | if (ret < 0) |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1106 | goto out_unlock; |
Filipe Manana | a315e68 | 2017-03-06 23:04:20 +0000 | [diff] [blame] | 1107 | cur_alloc_size = ins.offset; |
| 1108 | extent_reserved = true; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1109 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1110 | ram_size = ins.offset; |
Nikolay Borisov | 6e26c44 | 2020-06-03 08:55:14 +0300 | [diff] [blame] | 1111 | em = create_io_em(inode, start, ins.offset, /* len */ |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 1112 | start, /* orig_start */ |
| 1113 | ins.objectid, /* block_start */ |
| 1114 | ins.offset, /* block_len */ |
| 1115 | ins.offset, /* orig_block_len */ |
| 1116 | ram_size, /* ram_bytes */ |
| 1117 | BTRFS_COMPRESS_NONE, /* compress_type */ |
Liu Bo | 1af4a0a | 2017-02-13 15:35:09 -0800 | [diff] [blame] | 1118 | BTRFS_ORDERED_REGULAR /* type */); |
Su Yue | 090a127a | 2018-05-30 16:48:56 +0800 | [diff] [blame] | 1119 | if (IS_ERR(em)) { |
| 1120 | ret = PTR_ERR(em); |
Liu Bo | ace68ba | 2013-04-22 10:53:47 +0000 | [diff] [blame] | 1121 | goto out_reserve; |
Su Yue | 090a127a | 2018-05-30 16:48:56 +0800 | [diff] [blame] | 1122 | } |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 1123 | free_extent_map(em); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1124 | |
Nikolay Borisov | 6e26c44 | 2020-06-03 08:55:14 +0300 | [diff] [blame] | 1125 | ret = btrfs_add_ordered_extent(inode, start, ins.objectid, |
Qu Wenruo | 3c198fe | 2021-01-21 14:13:54 +0800 | [diff] [blame] | 1126 | ram_size, cur_alloc_size, |
| 1127 | BTRFS_ORDERED_REGULAR); |
Liu Bo | ace68ba | 2013-04-22 10:53:47 +0000 | [diff] [blame] | 1128 | if (ret) |
Filipe Manana | d9f8596 | 2014-08-25 10:43:00 +0100 | [diff] [blame] | 1129 | goto out_drop_extent_cache; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1130 | |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1131 | if (root->root_key.objectid == |
| 1132 | BTRFS_DATA_RELOC_TREE_OBJECTID) { |
Nikolay Borisov | 6e26c44 | 2020-06-03 08:55:14 +0300 | [diff] [blame] | 1133 | ret = btrfs_reloc_clone_csums(inode, start, |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1134 | cur_alloc_size); |
Qu Wenruo | 4dbd80f | 2017-03-08 10:25:51 +0800 | [diff] [blame] | 1135 | /* |
| 1136 | * Only drop cache here, and process as normal. |
| 1137 | * |
| 1138 | * We must not allow extent_clear_unlock_delalloc() |
| 1139 | * at out_unlock label to free meta of this ordered |
| 1140 | * extent, as its meta should be freed by |
| 1141 | * btrfs_finish_ordered_io(). |
| 1142 | * |
| 1143 | * So we must continue until @start is increased to |
| 1144 | * skip current ordered extent. |
| 1145 | */ |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 1146 | if (ret) |
Nikolay Borisov | 6e26c44 | 2020-06-03 08:55:14 +0300 | [diff] [blame] | 1147 | btrfs_drop_extent_cache(inode, start, |
Qu Wenruo | 4dbd80f | 2017-03-08 10:25:51 +0800 | [diff] [blame] | 1148 | start + ram_size - 1, 0); |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1149 | } |
| 1150 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1151 | btrfs_dec_block_group_reservations(fs_info, ins.objectid); |
Filipe Manana | 9cfa3e3 | 2016-04-26 15:39:32 +0100 | [diff] [blame] | 1152 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1153 | /* we're not doing compressed IO, don't unlock the first |
| 1154 | * page (which the caller expects to stay locked), don't |
| 1155 | * clear any dirty bits and don't set any writeback bits |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 1156 | * |
| 1157 | * Do set the Private2 bit so we know this page was properly |
| 1158 | * setup for writepage |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1159 | */ |
Filipe Manana | a315e68 | 2017-03-06 23:04:20 +0000 | [diff] [blame] | 1160 | page_ops = unlock ? PAGE_UNLOCK : 0; |
| 1161 | page_ops |= PAGE_SET_PRIVATE2; |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 1162 | |
Nikolay Borisov | 6e26c44 | 2020-06-03 08:55:14 +0300 | [diff] [blame] | 1163 | extent_clear_unlock_delalloc(inode, start, start + ram_size - 1, |
Nikolay Borisov | 74e9194 | 2019-07-17 16:18:16 +0300 | [diff] [blame] | 1164 | locked_page, |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1165 | EXTENT_LOCKED | EXTENT_DELALLOC, |
Filipe Manana | a315e68 | 2017-03-06 23:04:20 +0000 | [diff] [blame] | 1166 | page_ops); |
Anand Jain | 3752d22 | 2018-02-15 12:29:38 +0800 | [diff] [blame] | 1167 | if (num_bytes < cur_alloc_size) |
| 1168 | num_bytes = 0; |
Qu Wenruo | 4dbd80f | 2017-03-08 10:25:51 +0800 | [diff] [blame] | 1169 | else |
Anand Jain | 3752d22 | 2018-02-15 12:29:38 +0800 | [diff] [blame] | 1170 | num_bytes -= cur_alloc_size; |
Chris Mason | c59f895 | 2007-12-17 20:14:04 -0500 | [diff] [blame] | 1171 | alloc_hint = ins.objectid + ins.offset; |
| 1172 | start += cur_alloc_size; |
Filipe Manana | a315e68 | 2017-03-06 23:04:20 +0000 | [diff] [blame] | 1173 | extent_reserved = false; |
Qu Wenruo | 4dbd80f | 2017-03-08 10:25:51 +0800 | [diff] [blame] | 1174 | |
| 1175 | /* |
| 1176 | * btrfs_reloc_clone_csums() error, since start is increased |
| 1177 | * extent_clear_unlock_delalloc() at out_unlock label won't |
| 1178 | * free metadata of current ordered extent, we're OK to exit. |
| 1179 | */ |
| 1180 | if (ret) |
| 1181 | goto out_unlock; |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 1182 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1183 | out: |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1184 | return ret; |
Miao Xie | b7d5b0a | 2012-11-01 07:32:18 +0000 | [diff] [blame] | 1185 | |
Filipe Manana | d9f8596 | 2014-08-25 10:43:00 +0100 | [diff] [blame] | 1186 | out_drop_extent_cache: |
Nikolay Borisov | 6e26c44 | 2020-06-03 08:55:14 +0300 | [diff] [blame] | 1187 | btrfs_drop_extent_cache(inode, start, start + ram_size - 1, 0); |
Liu Bo | ace68ba | 2013-04-22 10:53:47 +0000 | [diff] [blame] | 1188 | out_reserve: |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1189 | btrfs_dec_block_group_reservations(fs_info, ins.objectid); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 1190 | btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1191 | out_unlock: |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 1192 | clear_bits = EXTENT_LOCKED | EXTENT_DELALLOC | EXTENT_DELALLOC_NEW | |
| 1193 | EXTENT_DEFRAG | EXTENT_CLEAR_META_RESV; |
Qu Wenruo | 6869b0a | 2021-01-26 16:33:45 +0800 | [diff] [blame] | 1194 | page_ops = PAGE_UNLOCK | PAGE_START_WRITEBACK | PAGE_END_WRITEBACK; |
Filipe Manana | a315e68 | 2017-03-06 23:04:20 +0000 | [diff] [blame] | 1195 | /* |
| 1196 | * If we reserved an extent for our delalloc range (or a subrange) and |
| 1197 | * failed to create the respective ordered extent, then it means that |
| 1198 | * when we reserved the extent we decremented the extent's size from |
| 1199 | * the data space_info's bytes_may_use counter and incremented the |
| 1200 | * space_info's bytes_reserved counter by the same amount. We must make |
| 1201 | * sure extent_clear_unlock_delalloc() does not try to decrement again |
| 1202 | * the data space_info's bytes_may_use counter, therefore we do not pass |
| 1203 | * it the flag EXTENT_CLEAR_DATA_RESV. |
| 1204 | */ |
| 1205 | if (extent_reserved) { |
Nikolay Borisov | 6e26c44 | 2020-06-03 08:55:14 +0300 | [diff] [blame] | 1206 | extent_clear_unlock_delalloc(inode, start, |
Filipe Manana | e2c8e92 | 2020-05-27 11:15:53 +0100 | [diff] [blame] | 1207 | start + cur_alloc_size - 1, |
Filipe Manana | a315e68 | 2017-03-06 23:04:20 +0000 | [diff] [blame] | 1208 | locked_page, |
| 1209 | clear_bits, |
| 1210 | page_ops); |
| 1211 | start += cur_alloc_size; |
| 1212 | if (start >= end) |
| 1213 | goto out; |
| 1214 | } |
Nikolay Borisov | 6e26c44 | 2020-06-03 08:55:14 +0300 | [diff] [blame] | 1215 | extent_clear_unlock_delalloc(inode, start, end, locked_page, |
Filipe Manana | a315e68 | 2017-03-06 23:04:20 +0000 | [diff] [blame] | 1216 | clear_bits | EXTENT_CLEAR_DATA_RESV, |
| 1217 | page_ops); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1218 | goto out; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1219 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1220 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1221 | /* |
| 1222 | * work queue call back to started compression on a file and pages |
| 1223 | */ |
| 1224 | static noinline void async_cow_start(struct btrfs_work *work) |
| 1225 | { |
Nikolay Borisov | b532627 | 2019-03-12 17:20:25 +0200 | [diff] [blame] | 1226 | struct async_chunk *async_chunk; |
Nikolay Borisov | ac3e993 | 2019-07-17 14:41:44 +0300 | [diff] [blame] | 1227 | int compressed_extents; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1228 | |
Nikolay Borisov | b532627 | 2019-03-12 17:20:25 +0200 | [diff] [blame] | 1229 | async_chunk = container_of(work, struct async_chunk, work); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1230 | |
Nikolay Borisov | ac3e993 | 2019-07-17 14:41:44 +0300 | [diff] [blame] | 1231 | compressed_extents = compress_file_range(async_chunk); |
| 1232 | if (compressed_extents == 0) { |
Nikolay Borisov | b532627 | 2019-03-12 17:20:25 +0200 | [diff] [blame] | 1233 | btrfs_add_delayed_iput(async_chunk->inode); |
| 1234 | async_chunk->inode = NULL; |
Josef Bacik | 8180ef8 | 2012-06-08 15:16:12 -0400 | [diff] [blame] | 1235 | } |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1236 | } |
| 1237 | |
| 1238 | /* |
| 1239 | * work queue call back to submit previously compressed pages |
| 1240 | */ |
| 1241 | static noinline void async_cow_submit(struct btrfs_work *work) |
| 1242 | { |
Nikolay Borisov | c5a68ae | 2019-03-12 17:20:26 +0200 | [diff] [blame] | 1243 | struct async_chunk *async_chunk = container_of(work, struct async_chunk, |
| 1244 | work); |
| 1245 | struct btrfs_fs_info *fs_info = btrfs_work_owner(work); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1246 | unsigned long nr_pages; |
| 1247 | |
Nikolay Borisov | b532627 | 2019-03-12 17:20:25 +0200 | [diff] [blame] | 1248 | nr_pages = (async_chunk->end - async_chunk->start + PAGE_SIZE) >> |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1249 | PAGE_SHIFT; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1250 | |
David Sterba | 093258e | 2018-02-26 16:15:17 +0100 | [diff] [blame] | 1251 | /* atomic_sub_return implies a barrier */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1252 | if (atomic_sub_return(nr_pages, &fs_info->async_delalloc_pages) < |
David Sterba | 093258e | 2018-02-26 16:15:17 +0100 | [diff] [blame] | 1253 | 5 * SZ_1M) |
| 1254 | cond_wake_up_nomb(&fs_info->async_submit_wait); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1255 | |
Nikolay Borisov | 4546d17 | 2019-01-03 10:50:03 +0200 | [diff] [blame] | 1256 | /* |
Nikolay Borisov | b532627 | 2019-03-12 17:20:25 +0200 | [diff] [blame] | 1257 | * ->inode could be NULL if async_chunk_start has failed to compress, |
Nikolay Borisov | 4546d17 | 2019-01-03 10:50:03 +0200 | [diff] [blame] | 1258 | * in which case we don't have anything to submit, yet we need to |
| 1259 | * always adjust ->async_delalloc_pages as its paired with the init |
| 1260 | * happening in cow_file_range_async |
| 1261 | */ |
Nikolay Borisov | b532627 | 2019-03-12 17:20:25 +0200 | [diff] [blame] | 1262 | if (async_chunk->inode) |
| 1263 | submit_compressed_extents(async_chunk); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1264 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1265 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1266 | static noinline void async_cow_free(struct btrfs_work *work) |
| 1267 | { |
Nikolay Borisov | b532627 | 2019-03-12 17:20:25 +0200 | [diff] [blame] | 1268 | struct async_chunk *async_chunk; |
Nikolay Borisov | 97db120 | 2019-03-12 17:20:24 +0200 | [diff] [blame] | 1269 | |
Nikolay Borisov | b532627 | 2019-03-12 17:20:25 +0200 | [diff] [blame] | 1270 | async_chunk = container_of(work, struct async_chunk, work); |
| 1271 | if (async_chunk->inode) |
| 1272 | btrfs_add_delayed_iput(async_chunk->inode); |
Chris Mason | ec39f76 | 2019-07-10 12:28:17 -0700 | [diff] [blame] | 1273 | if (async_chunk->blkcg_css) |
| 1274 | css_put(async_chunk->blkcg_css); |
Nikolay Borisov | 97db120 | 2019-03-12 17:20:24 +0200 | [diff] [blame] | 1275 | /* |
| 1276 | * 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] | 1277 | * async_chunk's, freeing it ensures the whole array has been freed. |
Nikolay Borisov | 97db120 | 2019-03-12 17:20:24 +0200 | [diff] [blame] | 1278 | */ |
Nikolay Borisov | b532627 | 2019-03-12 17:20:25 +0200 | [diff] [blame] | 1279 | if (atomic_dec_and_test(async_chunk->pending)) |
Nikolay Borisov | b1c16ac | 2019-04-01 11:29:57 +0300 | [diff] [blame] | 1280 | kvfree(async_chunk->pending); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1281 | } |
| 1282 | |
Nikolay Borisov | 751b643 | 2020-06-03 08:55:22 +0300 | [diff] [blame] | 1283 | static int cow_file_range_async(struct btrfs_inode *inode, |
Chris Mason | ec39f76 | 2019-07-10 12:28:17 -0700 | [diff] [blame] | 1284 | struct writeback_control *wbc, |
| 1285 | struct page *locked_page, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1286 | u64 start, u64 end, int *page_started, |
David Sterba | fac07d2 | 2019-10-29 18:28:57 +0100 | [diff] [blame] | 1287 | unsigned long *nr_written) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1288 | { |
Nikolay Borisov | 751b643 | 2020-06-03 08:55:22 +0300 | [diff] [blame] | 1289 | struct btrfs_fs_info *fs_info = inode->root->fs_info; |
Chris Mason | ec39f76 | 2019-07-10 12:28:17 -0700 | [diff] [blame] | 1290 | struct cgroup_subsys_state *blkcg_css = wbc_blkcg_css(wbc); |
Nikolay Borisov | 97db120 | 2019-03-12 17:20:24 +0200 | [diff] [blame] | 1291 | struct async_cow *ctx; |
| 1292 | struct async_chunk *async_chunk; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1293 | unsigned long nr_pages; |
| 1294 | u64 cur_end; |
Nikolay Borisov | 97db120 | 2019-03-12 17:20:24 +0200 | [diff] [blame] | 1295 | u64 num_chunks = DIV_ROUND_UP(end - start, SZ_512K); |
| 1296 | int i; |
| 1297 | bool should_compress; |
Nikolay Borisov | b1c16ac | 2019-04-01 11:29:57 +0300 | [diff] [blame] | 1298 | unsigned nofs_flag; |
David Sterba | fac07d2 | 2019-10-29 18:28:57 +0100 | [diff] [blame] | 1299 | const unsigned int write_flags = wbc_to_write_flags(wbc); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1300 | |
Nikolay Borisov | 751b643 | 2020-06-03 08:55:22 +0300 | [diff] [blame] | 1301 | unlock_extent(&inode->io_tree, start, end); |
Nikolay Borisov | 97db120 | 2019-03-12 17:20:24 +0200 | [diff] [blame] | 1302 | |
Nikolay Borisov | 751b643 | 2020-06-03 08:55:22 +0300 | [diff] [blame] | 1303 | if (inode->flags & BTRFS_INODE_NOCOMPRESS && |
Nikolay Borisov | 97db120 | 2019-03-12 17:20:24 +0200 | [diff] [blame] | 1304 | !btrfs_test_opt(fs_info, FORCE_COMPRESS)) { |
| 1305 | num_chunks = 1; |
| 1306 | should_compress = false; |
| 1307 | } else { |
| 1308 | should_compress = true; |
| 1309 | } |
| 1310 | |
Nikolay Borisov | b1c16ac | 2019-04-01 11:29:57 +0300 | [diff] [blame] | 1311 | nofs_flag = memalloc_nofs_save(); |
| 1312 | ctx = kvmalloc(struct_size(ctx, chunks, num_chunks), GFP_KERNEL); |
| 1313 | memalloc_nofs_restore(nofs_flag); |
| 1314 | |
Nikolay Borisov | 97db120 | 2019-03-12 17:20:24 +0200 | [diff] [blame] | 1315 | if (!ctx) { |
| 1316 | unsigned clear_bits = EXTENT_LOCKED | EXTENT_DELALLOC | |
| 1317 | EXTENT_DELALLOC_NEW | EXTENT_DEFRAG | |
| 1318 | EXTENT_DO_ACCOUNTING; |
Qu Wenruo | 6869b0a | 2021-01-26 16:33:45 +0800 | [diff] [blame] | 1319 | unsigned long page_ops = PAGE_UNLOCK | PAGE_START_WRITEBACK | |
| 1320 | PAGE_END_WRITEBACK | PAGE_SET_ERROR; |
Nikolay Borisov | 97db120 | 2019-03-12 17:20:24 +0200 | [diff] [blame] | 1321 | |
Nikolay Borisov | 751b643 | 2020-06-03 08:55:22 +0300 | [diff] [blame] | 1322 | extent_clear_unlock_delalloc(inode, start, end, locked_page, |
| 1323 | clear_bits, page_ops); |
Nikolay Borisov | 97db120 | 2019-03-12 17:20:24 +0200 | [diff] [blame] | 1324 | return -ENOMEM; |
| 1325 | } |
| 1326 | |
| 1327 | async_chunk = ctx->chunks; |
| 1328 | atomic_set(&ctx->num_chunks, num_chunks); |
| 1329 | |
| 1330 | for (i = 0; i < num_chunks; i++) { |
| 1331 | if (should_compress) |
| 1332 | cur_end = min(end, start + SZ_512K - 1); |
| 1333 | else |
| 1334 | cur_end = end; |
| 1335 | |
Nikolay Borisov | bd4691a | 2019-01-03 10:50:01 +0200 | [diff] [blame] | 1336 | /* |
| 1337 | * igrab is called higher up in the call chain, take only the |
| 1338 | * lightweight reference for the callback lifetime |
| 1339 | */ |
Nikolay Borisov | 751b643 | 2020-06-03 08:55:22 +0300 | [diff] [blame] | 1340 | ihold(&inode->vfs_inode); |
Nikolay Borisov | 97db120 | 2019-03-12 17:20:24 +0200 | [diff] [blame] | 1341 | async_chunk[i].pending = &ctx->num_chunks; |
Nikolay Borisov | 751b643 | 2020-06-03 08:55:22 +0300 | [diff] [blame] | 1342 | async_chunk[i].inode = &inode->vfs_inode; |
Nikolay Borisov | 97db120 | 2019-03-12 17:20:24 +0200 | [diff] [blame] | 1343 | async_chunk[i].start = start; |
| 1344 | async_chunk[i].end = cur_end; |
Nikolay Borisov | 97db120 | 2019-03-12 17:20:24 +0200 | [diff] [blame] | 1345 | async_chunk[i].write_flags = write_flags; |
| 1346 | INIT_LIST_HEAD(&async_chunk[i].extents); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1347 | |
Chris Mason | 1d53c9e | 2019-07-10 12:28:16 -0700 | [diff] [blame] | 1348 | /* |
| 1349 | * The locked_page comes all the way from writepage and its |
| 1350 | * the original page we were actually given. As we spread |
| 1351 | * this large delalloc region across multiple async_chunk |
| 1352 | * structs, only the first struct needs a pointer to locked_page |
| 1353 | * |
| 1354 | * This way we don't need racey decisions about who is supposed |
| 1355 | * to unlock it. |
| 1356 | */ |
| 1357 | if (locked_page) { |
Chris Mason | ec39f76 | 2019-07-10 12:28:17 -0700 | [diff] [blame] | 1358 | /* |
| 1359 | * Depending on the compressibility, the pages might or |
| 1360 | * might not go through async. We want all of them to |
| 1361 | * be accounted against wbc once. Let's do it here |
| 1362 | * before the paths diverge. wbc accounting is used |
| 1363 | * only for foreign writeback detection and doesn't |
| 1364 | * need full accuracy. Just account the whole thing |
| 1365 | * against the first page. |
| 1366 | */ |
| 1367 | wbc_account_cgroup_owner(wbc, locked_page, |
| 1368 | cur_end - start); |
Chris Mason | 1d53c9e | 2019-07-10 12:28:16 -0700 | [diff] [blame] | 1369 | async_chunk[i].locked_page = locked_page; |
| 1370 | locked_page = NULL; |
| 1371 | } else { |
| 1372 | async_chunk[i].locked_page = NULL; |
| 1373 | } |
| 1374 | |
Chris Mason | ec39f76 | 2019-07-10 12:28:17 -0700 | [diff] [blame] | 1375 | if (blkcg_css != blkcg_root_css) { |
| 1376 | css_get(blkcg_css); |
| 1377 | async_chunk[i].blkcg_css = blkcg_css; |
| 1378 | } else { |
| 1379 | async_chunk[i].blkcg_css = NULL; |
| 1380 | } |
| 1381 | |
Omar Sandoval | a0cac0e | 2019-09-16 11:30:57 -0700 | [diff] [blame] | 1382 | btrfs_init_work(&async_chunk[i].work, async_cow_start, |
| 1383 | async_cow_submit, async_cow_free); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1384 | |
Nikolay Borisov | 97db120 | 2019-03-12 17:20:24 +0200 | [diff] [blame] | 1385 | nr_pages = DIV_ROUND_UP(cur_end - start, PAGE_SIZE); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1386 | atomic_add(nr_pages, &fs_info->async_delalloc_pages); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1387 | |
Nikolay Borisov | 97db120 | 2019-03-12 17:20:24 +0200 | [diff] [blame] | 1388 | btrfs_queue_work(fs_info->delalloc_workers, &async_chunk[i].work); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1389 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1390 | *nr_written += nr_pages; |
| 1391 | start = cur_end + 1; |
| 1392 | } |
| 1393 | *page_started = 1; |
| 1394 | return 0; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1395 | } |
| 1396 | |
Naohiro Aota | 42c0110 | 2021-02-04 19:22:07 +0900 | [diff] [blame] | 1397 | static noinline int run_delalloc_zoned(struct btrfs_inode *inode, |
| 1398 | struct page *locked_page, u64 start, |
| 1399 | u64 end, int *page_started, |
| 1400 | unsigned long *nr_written) |
| 1401 | { |
| 1402 | int ret; |
| 1403 | |
| 1404 | ret = cow_file_range(inode, locked_page, start, end, page_started, |
| 1405 | nr_written, 0); |
| 1406 | if (ret) |
| 1407 | return ret; |
| 1408 | |
| 1409 | if (*page_started) |
| 1410 | return 0; |
| 1411 | |
| 1412 | __set_page_dirty_nobuffers(locked_page); |
| 1413 | account_page_redirty(locked_page); |
| 1414 | extent_write_locked_range(&inode->vfs_inode, start, end, WB_SYNC_ALL); |
| 1415 | *page_started = 1; |
| 1416 | |
| 1417 | return 0; |
| 1418 | } |
| 1419 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 1420 | 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] | 1421 | u64 bytenr, u64 num_bytes) |
| 1422 | { |
| 1423 | int ret; |
| 1424 | struct btrfs_ordered_sum *sums; |
| 1425 | LIST_HEAD(list); |
| 1426 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1427 | ret = btrfs_lookup_csums_range(fs_info->csum_root, bytenr, |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 1428 | bytenr + num_bytes - 1, &list, 0); |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1429 | if (ret == 0 && list_empty(&list)) |
| 1430 | return 0; |
| 1431 | |
| 1432 | while (!list_empty(&list)) { |
| 1433 | sums = list_entry(list.next, struct btrfs_ordered_sum, list); |
| 1434 | list_del(&sums->list); |
| 1435 | kfree(sums); |
| 1436 | } |
Liu Bo | 5811375 | 2018-01-31 17:09:13 -0700 | [diff] [blame] | 1437 | if (ret < 0) |
| 1438 | return ret; |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1439 | return 1; |
| 1440 | } |
| 1441 | |
Nikolay Borisov | 8ba96f3 | 2020-06-03 08:55:20 +0300 | [diff] [blame] | 1442 | 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] | 1443 | const u64 start, const u64 end, |
| 1444 | int *page_started, unsigned long *nr_written) |
| 1445 | { |
Nikolay Borisov | 8ba96f3 | 2020-06-03 08:55:20 +0300 | [diff] [blame] | 1446 | const bool is_space_ino = btrfs_is_free_space_inode(inode); |
| 1447 | const bool is_reloc_ino = (inode->root->root_key.objectid == |
Filipe Manana | 6bd335b | 2020-06-08 13:33:05 +0100 | [diff] [blame] | 1448 | BTRFS_DATA_RELOC_TREE_OBJECTID); |
Filipe Manana | 2166e5e | 2020-05-27 11:16:19 +0100 | [diff] [blame] | 1449 | const u64 range_bytes = end + 1 - start; |
Nikolay Borisov | 8ba96f3 | 2020-06-03 08:55:20 +0300 | [diff] [blame] | 1450 | struct extent_io_tree *io_tree = &inode->io_tree; |
Filipe Manana | 467dc47 | 2020-05-27 11:16:07 +0100 | [diff] [blame] | 1451 | u64 range_start = start; |
| 1452 | u64 count; |
| 1453 | |
| 1454 | /* |
| 1455 | * If EXTENT_NORESERVE is set it means that when the buffered write was |
| 1456 | * made we had not enough available data space and therefore we did not |
| 1457 | * reserve data space for it, since we though we could do NOCOW for the |
| 1458 | * respective file range (either there is prealloc extent or the inode |
| 1459 | * has the NOCOW bit set). |
| 1460 | * |
| 1461 | * However when we need to fallback to COW mode (because for example the |
| 1462 | * block group for the corresponding extent was turned to RO mode by a |
| 1463 | * scrub or relocation) we need to do the following: |
| 1464 | * |
| 1465 | * 1) We increment the bytes_may_use counter of the data space info. |
| 1466 | * If COW succeeds, it allocates a new data extent and after doing |
| 1467 | * that it decrements the space info's bytes_may_use counter and |
| 1468 | * increments its bytes_reserved counter by the same amount (we do |
| 1469 | * this at btrfs_add_reserved_bytes()). So we need to increment the |
| 1470 | * bytes_may_use counter to compensate (when space is reserved at |
| 1471 | * buffered write time, the bytes_may_use counter is incremented); |
| 1472 | * |
| 1473 | * 2) We clear the EXTENT_NORESERVE bit from the range. We do this so |
| 1474 | * that if the COW path fails for any reason, it decrements (through |
| 1475 | * extent_clear_unlock_delalloc()) the bytes_may_use counter of the |
| 1476 | * data space info, which we incremented in the step above. |
Filipe Manana | 2166e5e | 2020-05-27 11:16:19 +0100 | [diff] [blame] | 1477 | * |
| 1478 | * 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] | 1479 | * space cache inode or an inode of the data relocation tree, we must |
| 1480 | * also increment bytes_may_use of the data space_info for the same |
| 1481 | * reason. Space caches and relocated data extents always get a prealloc |
Filipe Manana | 2166e5e | 2020-05-27 11:16:19 +0100 | [diff] [blame] | 1482 | * extent for them, however scrub or balance may have set the block |
Filipe Manana | 6bd335b | 2020-06-08 13:33:05 +0100 | [diff] [blame] | 1483 | * group that contains that extent to RO mode and therefore force COW |
| 1484 | * when starting writeback. |
Filipe Manana | 467dc47 | 2020-05-27 11:16:07 +0100 | [diff] [blame] | 1485 | */ |
Filipe Manana | 2166e5e | 2020-05-27 11:16:19 +0100 | [diff] [blame] | 1486 | count = count_range_bits(io_tree, &range_start, end, range_bytes, |
Filipe Manana | 467dc47 | 2020-05-27 11:16:07 +0100 | [diff] [blame] | 1487 | EXTENT_NORESERVE, 0); |
Filipe Manana | 6bd335b | 2020-06-08 13:33:05 +0100 | [diff] [blame] | 1488 | if (count > 0 || is_space_ino || is_reloc_ino) { |
| 1489 | u64 bytes = count; |
Nikolay Borisov | 8ba96f3 | 2020-06-03 08:55:20 +0300 | [diff] [blame] | 1490 | struct btrfs_fs_info *fs_info = inode->root->fs_info; |
Filipe Manana | 467dc47 | 2020-05-27 11:16:07 +0100 | [diff] [blame] | 1491 | struct btrfs_space_info *sinfo = fs_info->data_sinfo; |
| 1492 | |
Filipe Manana | 6bd335b | 2020-06-08 13:33:05 +0100 | [diff] [blame] | 1493 | if (is_space_ino || is_reloc_ino) |
| 1494 | bytes = range_bytes; |
| 1495 | |
Filipe Manana | 467dc47 | 2020-05-27 11:16:07 +0100 | [diff] [blame] | 1496 | spin_lock(&sinfo->lock); |
Filipe Manana | 2166e5e | 2020-05-27 11:16:19 +0100 | [diff] [blame] | 1497 | btrfs_space_info_update_bytes_may_use(fs_info, sinfo, bytes); |
Filipe Manana | 467dc47 | 2020-05-27 11:16:07 +0100 | [diff] [blame] | 1498 | spin_unlock(&sinfo->lock); |
| 1499 | |
Filipe Manana | 2166e5e | 2020-05-27 11:16:19 +0100 | [diff] [blame] | 1500 | if (count > 0) |
| 1501 | clear_extent_bit(io_tree, start, end, EXTENT_NORESERVE, |
| 1502 | 0, 0, NULL); |
Filipe Manana | 467dc47 | 2020-05-27 11:16:07 +0100 | [diff] [blame] | 1503 | } |
| 1504 | |
Nikolay Borisov | 8ba96f3 | 2020-06-03 08:55:20 +0300 | [diff] [blame] | 1505 | return cow_file_range(inode, locked_page, start, end, page_started, |
| 1506 | nr_written, 1); |
Filipe Manana | 467dc47 | 2020-05-27 11:16:07 +0100 | [diff] [blame] | 1507 | } |
| 1508 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1509 | /* |
| 1510 | * when nowcow writeback call back. This checks for snapshots or COW copies |
| 1511 | * of the extents that exist in the file, and COWs the file as required. |
| 1512 | * |
| 1513 | * If no cow copies or snapshots exist, we write directly to the existing |
| 1514 | * blocks on disk |
| 1515 | */ |
Nikolay Borisov | 968322c | 2020-06-03 08:55:21 +0300 | [diff] [blame] | 1516 | static noinline int run_delalloc_nocow(struct btrfs_inode *inode, |
Chris Mason | 7f366cf | 2009-03-12 20:12:45 -0400 | [diff] [blame] | 1517 | struct page *locked_page, |
Nikolay Borisov | 3e02484 | 2019-08-21 10:42:03 +0300 | [diff] [blame] | 1518 | const u64 start, const u64 end, |
| 1519 | int *page_started, int force, |
| 1520 | unsigned long *nr_written) |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1521 | { |
Nikolay Borisov | 968322c | 2020-06-03 08:55:21 +0300 | [diff] [blame] | 1522 | struct btrfs_fs_info *fs_info = inode->root->fs_info; |
| 1523 | struct btrfs_root *root = inode->root; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1524 | struct btrfs_path *path; |
Nikolay Borisov | 3e02484 | 2019-08-21 10:42:03 +0300 | [diff] [blame] | 1525 | u64 cow_start = (u64)-1; |
| 1526 | u64 cur_offset = start; |
Robbie Ko | 8ecebf4d | 2018-08-06 10:30:30 +0800 | [diff] [blame] | 1527 | int ret; |
Nikolay Borisov | 3e02484 | 2019-08-21 10:42:03 +0300 | [diff] [blame] | 1528 | bool check_prev = true; |
Nikolay Borisov | 968322c | 2020-06-03 08:55:21 +0300 | [diff] [blame] | 1529 | const bool freespace_inode = btrfs_is_free_space_inode(inode); |
| 1530 | u64 ino = btrfs_ino(inode); |
Nikolay Borisov | 762bf09 | 2019-08-22 17:24:20 +0300 | [diff] [blame] | 1531 | bool nocow = false; |
| 1532 | u64 disk_bytenr = 0; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1533 | |
| 1534 | path = btrfs_alloc_path(); |
Josef Bacik | 17ca04a | 2012-05-31 15:58:55 -0400 | [diff] [blame] | 1535 | if (!path) { |
Nikolay Borisov | 968322c | 2020-06-03 08:55:21 +0300 | [diff] [blame] | 1536 | extent_clear_unlock_delalloc(inode, start, end, locked_page, |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1537 | EXTENT_LOCKED | EXTENT_DELALLOC | |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 1538 | EXTENT_DO_ACCOUNTING | |
| 1539 | EXTENT_DEFRAG, PAGE_UNLOCK | |
Qu Wenruo | 6869b0a | 2021-01-26 16:33:45 +0800 | [diff] [blame] | 1540 | PAGE_START_WRITEBACK | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1541 | PAGE_END_WRITEBACK); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 1542 | return -ENOMEM; |
Josef Bacik | 17ca04a | 2012-05-31 15:58:55 -0400 | [diff] [blame] | 1543 | } |
Li Zefan | 82d5902 | 2011-04-20 10:33:24 +0800 | [diff] [blame] | 1544 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1545 | while (1) { |
Nikolay Borisov | 3e02484 | 2019-08-21 10:42:03 +0300 | [diff] [blame] | 1546 | struct btrfs_key found_key; |
| 1547 | struct btrfs_file_extent_item *fi; |
| 1548 | struct extent_buffer *leaf; |
| 1549 | u64 extent_end; |
| 1550 | u64 extent_offset; |
Nikolay Borisov | 3e02484 | 2019-08-21 10:42:03 +0300 | [diff] [blame] | 1551 | u64 num_bytes = 0; |
| 1552 | u64 disk_num_bytes; |
Nikolay Borisov | 3e02484 | 2019-08-21 10:42:03 +0300 | [diff] [blame] | 1553 | u64 ram_bytes; |
| 1554 | int extent_type; |
Nikolay Borisov | 762bf09 | 2019-08-22 17:24:20 +0300 | [diff] [blame] | 1555 | |
| 1556 | nocow = false; |
Nikolay Borisov | 3e02484 | 2019-08-21 10:42:03 +0300 | [diff] [blame] | 1557 | |
Liu Bo | e4c3b2d | 2017-01-30 12:25:28 -0800 | [diff] [blame] | 1558 | ret = btrfs_lookup_file_extent(NULL, root, path, ino, |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1559 | cur_offset, 0); |
Josef Bacik | d788a34 | 2013-10-25 16:55:08 -0400 | [diff] [blame] | 1560 | if (ret < 0) |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1561 | goto error; |
Nikolay Borisov | a6bd9cd | 2019-08-21 10:42:57 +0300 | [diff] [blame] | 1562 | |
| 1563 | /* |
| 1564 | * If there is no extent for our range when doing the initial |
| 1565 | * search, then go back to the previous slot as it will be the |
| 1566 | * one containing the search offset |
| 1567 | */ |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1568 | if (ret > 0 && path->slots[0] > 0 && check_prev) { |
| 1569 | leaf = path->nodes[0]; |
| 1570 | btrfs_item_key_to_cpu(leaf, &found_key, |
| 1571 | path->slots[0] - 1); |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 1572 | if (found_key.objectid == ino && |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1573 | found_key.type == BTRFS_EXTENT_DATA_KEY) |
| 1574 | path->slots[0]--; |
| 1575 | } |
Nikolay Borisov | 3e02484 | 2019-08-21 10:42:03 +0300 | [diff] [blame] | 1576 | check_prev = false; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1577 | next_slot: |
Nikolay Borisov | a6bd9cd | 2019-08-21 10:42:57 +0300 | [diff] [blame] | 1578 | /* Go to next leaf if we have exhausted the current one */ |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1579 | leaf = path->nodes[0]; |
| 1580 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { |
| 1581 | ret = btrfs_next_leaf(root, path); |
Liu Bo | e891669 | 2018-01-25 11:02:50 -0700 | [diff] [blame] | 1582 | if (ret < 0) { |
| 1583 | if (cow_start != (u64)-1) |
| 1584 | cur_offset = cow_start; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1585 | goto error; |
Liu Bo | e891669 | 2018-01-25 11:02:50 -0700 | [diff] [blame] | 1586 | } |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1587 | if (ret > 0) |
| 1588 | break; |
| 1589 | leaf = path->nodes[0]; |
| 1590 | } |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1591 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1592 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1593 | |
Nikolay Borisov | a6bd9cd | 2019-08-21 10:42:57 +0300 | [diff] [blame] | 1594 | /* Didn't find anything for our INO */ |
Filipe Manana | 1d512cb | 2015-11-09 00:33:58 +0000 | [diff] [blame] | 1595 | if (found_key.objectid > ino) |
| 1596 | break; |
Nikolay Borisov | a6bd9cd | 2019-08-21 10:42:57 +0300 | [diff] [blame] | 1597 | /* |
| 1598 | * Keep searching until we find an EXTENT_ITEM or there are no |
| 1599 | * more extents for this inode |
| 1600 | */ |
Filipe Manana | 1d512cb | 2015-11-09 00:33:58 +0000 | [diff] [blame] | 1601 | if (WARN_ON_ONCE(found_key.objectid < ino) || |
| 1602 | found_key.type < BTRFS_EXTENT_DATA_KEY) { |
| 1603 | path->slots[0]++; |
| 1604 | goto next_slot; |
| 1605 | } |
Nikolay Borisov | a6bd9cd | 2019-08-21 10:42:57 +0300 | [diff] [blame] | 1606 | |
| 1607 | /* Found key is not EXTENT_DATA_KEY or starts after req range */ |
Filipe Manana | 1d512cb | 2015-11-09 00:33:58 +0000 | [diff] [blame] | 1608 | if (found_key.type > BTRFS_EXTENT_DATA_KEY || |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1609 | found_key.offset > end) |
| 1610 | break; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1611 | |
Nikolay Borisov | a6bd9cd | 2019-08-21 10:42:57 +0300 | [diff] [blame] | 1612 | /* |
| 1613 | * If the found extent starts after requested offset, then |
| 1614 | * adjust extent_end to be right before this extent begins |
| 1615 | */ |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1616 | if (found_key.offset > cur_offset) { |
| 1617 | extent_end = found_key.offset; |
Chris Mason | e9061e2 | 2009-10-09 09:57:45 -0400 | [diff] [blame] | 1618 | extent_type = 0; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1619 | goto out_check; |
| 1620 | } |
Chris Mason | c31f883 | 2008-01-08 15:46:31 -0500 | [diff] [blame] | 1621 | |
Nikolay Borisov | a6bd9cd | 2019-08-21 10:42:57 +0300 | [diff] [blame] | 1622 | /* |
| 1623 | * Found extent which begins before our range and potentially |
| 1624 | * intersect it |
| 1625 | */ |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1626 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 1627 | struct btrfs_file_extent_item); |
| 1628 | extent_type = btrfs_file_extent_type(leaf, fi); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1629 | |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 1630 | ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1631 | if (extent_type == BTRFS_FILE_EXTENT_REG || |
| 1632 | extent_type == BTRFS_FILE_EXTENT_PREALLOC) { |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1633 | disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1634 | extent_offset = btrfs_file_extent_offset(leaf, fi); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1635 | extent_end = found_key.offset + |
| 1636 | btrfs_file_extent_num_bytes(leaf, fi); |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 1637 | disk_num_bytes = |
| 1638 | btrfs_file_extent_disk_num_bytes(leaf, fi); |
Nikolay Borisov | a6bd9cd | 2019-08-21 10:42:57 +0300 | [diff] [blame] | 1639 | /* |
Filipe Manana | de7999a | 2019-12-11 09:01:40 +0000 | [diff] [blame] | 1640 | * If the extent we got ends before our current offset, |
| 1641 | * skip to the next extent. |
Nikolay Borisov | a6bd9cd | 2019-08-21 10:42:57 +0300 | [diff] [blame] | 1642 | */ |
Filipe Manana | de7999a | 2019-12-11 09:01:40 +0000 | [diff] [blame] | 1643 | if (extent_end <= cur_offset) { |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1644 | path->slots[0]++; |
| 1645 | goto next_slot; |
| 1646 | } |
Nikolay Borisov | a6bd9cd | 2019-08-21 10:42:57 +0300 | [diff] [blame] | 1647 | /* Skip holes */ |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1648 | if (disk_bytenr == 0) |
| 1649 | goto out_check; |
Nikolay Borisov | a6bd9cd | 2019-08-21 10:42:57 +0300 | [diff] [blame] | 1650 | /* Skip compressed/encrypted/encoded extents */ |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1651 | if (btrfs_file_extent_compression(leaf, fi) || |
| 1652 | btrfs_file_extent_encryption(leaf, fi) || |
| 1653 | btrfs_file_extent_other_encoding(leaf, fi)) |
| 1654 | goto out_check; |
Ethan Lien | 78d4295 | 2018-05-17 14:58:29 +0800 | [diff] [blame] | 1655 | /* |
Nikolay Borisov | a6bd9cd | 2019-08-21 10:42:57 +0300 | [diff] [blame] | 1656 | * If extent is created before the last volume's snapshot |
| 1657 | * this implies the extent is shared, hence we can't do |
| 1658 | * nocow. This is the same check as in |
| 1659 | * btrfs_cross_ref_exist but without calling |
| 1660 | * btrfs_search_slot. |
Ethan Lien | 78d4295 | 2018-05-17 14:58:29 +0800 | [diff] [blame] | 1661 | */ |
Nikolay Borisov | 3e02484 | 2019-08-21 10:42:03 +0300 | [diff] [blame] | 1662 | if (!freespace_inode && |
Lu Fengqi | 27a7ff5 | 2018-11-29 17:31:32 +0800 | [diff] [blame] | 1663 | btrfs_file_extent_generation(leaf, fi) <= |
Ethan Lien | 78d4295 | 2018-05-17 14:58:29 +0800 | [diff] [blame] | 1664 | btrfs_root_last_snapshot(&root->root_item)) |
| 1665 | goto out_check; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1666 | if (extent_type == BTRFS_FILE_EXTENT_REG && !force) |
| 1667 | goto out_check; |
Filipe Manana | c65ca98 | 2020-11-18 11:00:17 +0000 | [diff] [blame] | 1668 | |
| 1669 | /* |
| 1670 | * The following checks can be expensive, as they need to |
| 1671 | * take other locks and do btree or rbtree searches, so |
| 1672 | * release the path to avoid blocking other tasks for too |
| 1673 | * long. |
| 1674 | */ |
| 1675 | btrfs_release_path(path); |
| 1676 | |
Liu Bo | 5811375 | 2018-01-31 17:09:13 -0700 | [diff] [blame] | 1677 | ret = btrfs_cross_ref_exist(root, ino, |
| 1678 | found_key.offset - |
Boris Burkov | a84d5d4 | 2020-08-18 11:00:05 -0700 | [diff] [blame] | 1679 | extent_offset, disk_bytenr, false); |
Liu Bo | 5811375 | 2018-01-31 17:09:13 -0700 | [diff] [blame] | 1680 | if (ret) { |
| 1681 | /* |
| 1682 | * ret could be -EIO if the above fails to read |
| 1683 | * metadata. |
| 1684 | */ |
| 1685 | if (ret < 0) { |
| 1686 | if (cow_start != (u64)-1) |
| 1687 | cur_offset = cow_start; |
| 1688 | goto error; |
| 1689 | } |
| 1690 | |
Nikolay Borisov | 3e02484 | 2019-08-21 10:42:03 +0300 | [diff] [blame] | 1691 | WARN_ON_ONCE(freespace_inode); |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1692 | goto out_check; |
Liu Bo | 5811375 | 2018-01-31 17:09:13 -0700 | [diff] [blame] | 1693 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1694 | disk_bytenr += extent_offset; |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1695 | disk_bytenr += cur_offset - found_key.offset; |
| 1696 | num_bytes = min(end + 1, extent_end) - cur_offset; |
| 1697 | /* |
Nikolay Borisov | a6bd9cd | 2019-08-21 10:42:57 +0300 | [diff] [blame] | 1698 | * If there are pending snapshots for this root, we |
| 1699 | * fall into common COW way |
Wang Shilong | e9894fd | 2014-03-27 11:12:25 +0800 | [diff] [blame] | 1700 | */ |
Nikolay Borisov | 3e02484 | 2019-08-21 10:42:03 +0300 | [diff] [blame] | 1701 | if (!freespace_inode && atomic_read(&root->snapshot_force_cow)) |
Robbie Ko | 8ecebf4d | 2018-08-06 10:30:30 +0800 | [diff] [blame] | 1702 | goto out_check; |
Wang Shilong | e9894fd | 2014-03-27 11:12:25 +0800 | [diff] [blame] | 1703 | /* |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1704 | * force cow if csum exists in the range. |
| 1705 | * this ensure that csum for a given extent are |
| 1706 | * either valid or do not exist. |
| 1707 | */ |
Liu Bo | 5811375 | 2018-01-31 17:09:13 -0700 | [diff] [blame] | 1708 | ret = csum_exist_in_range(fs_info, disk_bytenr, |
| 1709 | num_bytes); |
| 1710 | if (ret) { |
Liu Bo | 5811375 | 2018-01-31 17:09:13 -0700 | [diff] [blame] | 1711 | /* |
| 1712 | * ret could be -EIO if the above fails to read |
| 1713 | * metadata. |
| 1714 | */ |
| 1715 | if (ret < 0) { |
| 1716 | if (cow_start != (u64)-1) |
| 1717 | cur_offset = cow_start; |
| 1718 | goto error; |
| 1719 | } |
Nikolay Borisov | 3e02484 | 2019-08-21 10:42:03 +0300 | [diff] [blame] | 1720 | WARN_ON_ONCE(freespace_inode); |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1721 | goto out_check; |
Robbie Ko | 91e1f56 | 2016-10-07 10:01:29 +0800 | [diff] [blame] | 1722 | } |
Filipe Manana | 2090303 | 2021-02-05 12:55:36 +0000 | [diff] [blame] | 1723 | /* If the extent's block group is RO, we must COW */ |
Robbie Ko | 8ecebf4d | 2018-08-06 10:30:30 +0800 | [diff] [blame] | 1724 | if (!btrfs_inc_nocow_writers(fs_info, disk_bytenr)) |
Filipe Manana | f78c436 | 2016-05-09 13:15:41 +0100 | [diff] [blame] | 1725 | goto out_check; |
Nikolay Borisov | 3e02484 | 2019-08-21 10:42:03 +0300 | [diff] [blame] | 1726 | nocow = true; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1727 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
Nikolay Borisov | e8e2100 | 2019-08-22 17:25:23 +0300 | [diff] [blame] | 1728 | extent_end = found_key.offset + ram_bytes; |
| 1729 | extent_end = ALIGN(extent_end, fs_info->sectorsize); |
Nikolay Borisov | 922f051 | 2019-08-05 17:47:06 +0300 | [diff] [blame] | 1730 | /* Skip extents outside of our requested range */ |
| 1731 | if (extent_end <= start) { |
| 1732 | path->slots[0]++; |
| 1733 | goto next_slot; |
| 1734 | } |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1735 | } else { |
Nikolay Borisov | e8e2100 | 2019-08-22 17:25:23 +0300 | [diff] [blame] | 1736 | /* If this triggers then we have a memory corruption */ |
Arnd Bergmann | 290342f | 2019-03-25 14:02:25 +0100 | [diff] [blame] | 1737 | BUG(); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1738 | } |
| 1739 | out_check: |
Nikolay Borisov | a6bd9cd | 2019-08-21 10:42:57 +0300 | [diff] [blame] | 1740 | /* |
| 1741 | * If nocow is false then record the beginning of the range |
| 1742 | * that needs to be COWed |
| 1743 | */ |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1744 | if (!nocow) { |
| 1745 | if (cow_start == (u64)-1) |
| 1746 | cow_start = cur_offset; |
| 1747 | cur_offset = extent_end; |
| 1748 | if (cur_offset > end) |
| 1749 | break; |
Filipe Manana | c65ca98 | 2020-11-18 11:00:17 +0000 | [diff] [blame] | 1750 | if (!path->nodes[0]) |
| 1751 | continue; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1752 | path->slots[0]++; |
| 1753 | goto next_slot; |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1754 | } |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1755 | |
Nikolay Borisov | a6bd9cd | 2019-08-21 10:42:57 +0300 | [diff] [blame] | 1756 | /* |
| 1757 | * COW range from cow_start to found_key.offset - 1. As the key |
| 1758 | * will contain the beginning of the first extent that can be |
| 1759 | * NOCOW, following one which needs to be COW'ed |
| 1760 | */ |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1761 | if (cow_start != (u64)-1) { |
Nikolay Borisov | 968322c | 2020-06-03 08:55:21 +0300 | [diff] [blame] | 1762 | ret = fallback_to_cow(inode, locked_page, |
Nikolay Borisov | 8ba96f3 | 2020-06-03 08:55:20 +0300 | [diff] [blame] | 1763 | cow_start, found_key.offset - 1, |
Filipe Manana | 467dc47 | 2020-05-27 11:16:07 +0100 | [diff] [blame] | 1764 | page_started, nr_written); |
Josef Bacik | 230ed39 | 2020-07-06 09:14:12 -0400 | [diff] [blame] | 1765 | if (ret) |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1766 | goto error; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1767 | cow_start = (u64)-1; |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1768 | } |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1769 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1770 | if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) { |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 1771 | u64 orig_start = found_key.offset - extent_offset; |
Nikolay Borisov | 3e02484 | 2019-08-21 10:42:03 +0300 | [diff] [blame] | 1772 | struct extent_map *em; |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 1773 | |
Nikolay Borisov | 968322c | 2020-06-03 08:55:21 +0300 | [diff] [blame] | 1774 | em = create_io_em(inode, cur_offset, num_bytes, |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 1775 | orig_start, |
| 1776 | disk_bytenr, /* block_start */ |
| 1777 | num_bytes, /* block_len */ |
| 1778 | disk_num_bytes, /* orig_block_len */ |
| 1779 | ram_bytes, BTRFS_COMPRESS_NONE, |
| 1780 | BTRFS_ORDERED_PREALLOC); |
| 1781 | if (IS_ERR(em)) { |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 1782 | ret = PTR_ERR(em); |
| 1783 | goto error; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1784 | } |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 1785 | free_extent_map(em); |
Nikolay Borisov | 968322c | 2020-06-03 08:55:21 +0300 | [diff] [blame] | 1786 | ret = btrfs_add_ordered_extent(inode, cur_offset, |
Nikolay Borisov | bb55f62 | 2019-08-05 17:47:05 +0300 | [diff] [blame] | 1787 | disk_bytenr, num_bytes, |
| 1788 | num_bytes, |
| 1789 | BTRFS_ORDERED_PREALLOC); |
Nikolay Borisov | 762bf09 | 2019-08-22 17:24:20 +0300 | [diff] [blame] | 1790 | if (ret) { |
Nikolay Borisov | 968322c | 2020-06-03 08:55:21 +0300 | [diff] [blame] | 1791 | btrfs_drop_extent_cache(inode, cur_offset, |
Nikolay Borisov | 762bf09 | 2019-08-22 17:24:20 +0300 | [diff] [blame] | 1792 | cur_offset + num_bytes - 1, |
| 1793 | 0); |
| 1794 | goto error; |
| 1795 | } |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1796 | } else { |
Nikolay Borisov | 968322c | 2020-06-03 08:55:21 +0300 | [diff] [blame] | 1797 | ret = btrfs_add_ordered_extent(inode, cur_offset, |
Nikolay Borisov | bb55f62 | 2019-08-05 17:47:05 +0300 | [diff] [blame] | 1798 | disk_bytenr, num_bytes, |
| 1799 | num_bytes, |
| 1800 | BTRFS_ORDERED_NOCOW); |
Nikolay Borisov | 762bf09 | 2019-08-22 17:24:20 +0300 | [diff] [blame] | 1801 | if (ret) |
| 1802 | goto error; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1803 | } |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1804 | |
Filipe Manana | f78c436 | 2016-05-09 13:15:41 +0100 | [diff] [blame] | 1805 | if (nocow) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1806 | btrfs_dec_nocow_writers(fs_info, disk_bytenr); |
Nikolay Borisov | 762bf09 | 2019-08-22 17:24:20 +0300 | [diff] [blame] | 1807 | nocow = false; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1808 | |
Yan, Zheng | efa5646 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 1809 | if (root->root_key.objectid == |
Qu Wenruo | 4dbd80f | 2017-03-08 10:25:51 +0800 | [diff] [blame] | 1810 | BTRFS_DATA_RELOC_TREE_OBJECTID) |
| 1811 | /* |
| 1812 | * Error handled later, as we must prevent |
| 1813 | * extent_clear_unlock_delalloc() in error handler |
| 1814 | * from freeing metadata of created ordered extent. |
| 1815 | */ |
Nikolay Borisov | 968322c | 2020-06-03 08:55:21 +0300 | [diff] [blame] | 1816 | ret = btrfs_reloc_clone_csums(inode, cur_offset, |
Yan, Zheng | efa5646 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 1817 | num_bytes); |
Yan, Zheng | efa5646 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 1818 | |
Nikolay Borisov | 968322c | 2020-06-03 08:55:21 +0300 | [diff] [blame] | 1819 | extent_clear_unlock_delalloc(inode, cur_offset, |
Nikolay Borisov | 74e9194 | 2019-07-17 16:18:16 +0300 | [diff] [blame] | 1820 | cur_offset + num_bytes - 1, |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1821 | locked_page, EXTENT_LOCKED | |
Wang Xiaoguang | 1851309 | 2016-07-25 15:51:40 +0800 | [diff] [blame] | 1822 | EXTENT_DELALLOC | |
| 1823 | EXTENT_CLEAR_DATA_RESV, |
| 1824 | PAGE_UNLOCK | PAGE_SET_PRIVATE2); |
| 1825 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1826 | cur_offset = extent_end; |
Qu Wenruo | 4dbd80f | 2017-03-08 10:25:51 +0800 | [diff] [blame] | 1827 | |
| 1828 | /* |
| 1829 | * btrfs_reloc_clone_csums() error, now we're OK to call error |
| 1830 | * handler, as metadata for created ordered extent will only |
| 1831 | * be freed by btrfs_finish_ordered_io(). |
| 1832 | */ |
| 1833 | if (ret) |
| 1834 | goto error; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1835 | if (cur_offset > end) |
| 1836 | break; |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1837 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1838 | btrfs_release_path(path); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1839 | |
Robbie Ko | 506481b | 2018-10-30 18:04:04 +0800 | [diff] [blame] | 1840 | if (cur_offset <= end && cow_start == (u64)-1) |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1841 | cow_start = cur_offset; |
Josef Bacik | 17ca04a | 2012-05-31 15:58:55 -0400 | [diff] [blame] | 1842 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1843 | if (cow_start != (u64)-1) { |
Robbie Ko | 506481b | 2018-10-30 18:04:04 +0800 | [diff] [blame] | 1844 | cur_offset = end; |
Nikolay Borisov | 968322c | 2020-06-03 08:55:21 +0300 | [diff] [blame] | 1845 | ret = fallback_to_cow(inode, locked_page, cow_start, end, |
| 1846 | page_started, nr_written); |
Josef Bacik | d788a34 | 2013-10-25 16:55:08 -0400 | [diff] [blame] | 1847 | if (ret) |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1848 | goto error; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1849 | } |
| 1850 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1851 | error: |
Nikolay Borisov | 762bf09 | 2019-08-22 17:24:20 +0300 | [diff] [blame] | 1852 | if (nocow) |
| 1853 | btrfs_dec_nocow_writers(fs_info, disk_bytenr); |
| 1854 | |
Josef Bacik | 17ca04a | 2012-05-31 15:58:55 -0400 | [diff] [blame] | 1855 | if (ret && cur_offset < end) |
Nikolay Borisov | 968322c | 2020-06-03 08:55:21 +0300 | [diff] [blame] | 1856 | extent_clear_unlock_delalloc(inode, cur_offset, end, |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1857 | locked_page, EXTENT_LOCKED | |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 1858 | EXTENT_DELALLOC | EXTENT_DEFRAG | |
| 1859 | EXTENT_DO_ACCOUNTING, PAGE_UNLOCK | |
Qu Wenruo | 6869b0a | 2021-01-26 16:33:45 +0800 | [diff] [blame] | 1860 | PAGE_START_WRITEBACK | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1861 | PAGE_END_WRITEBACK); |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1862 | btrfs_free_path(path); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1863 | return ret; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1864 | } |
| 1865 | |
Nikolay Borisov | 0c49422 | 2020-06-03 08:55:28 +0300 | [diff] [blame] | 1866 | static inline int need_force_cow(struct btrfs_inode *inode, u64 start, u64 end) |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 1867 | { |
| 1868 | |
Nikolay Borisov | 0c49422 | 2020-06-03 08:55:28 +0300 | [diff] [blame] | 1869 | if (!(inode->flags & BTRFS_INODE_NODATACOW) && |
| 1870 | !(inode->flags & BTRFS_INODE_PREALLOC)) |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 1871 | return 0; |
| 1872 | |
| 1873 | /* |
| 1874 | * @defrag_bytes is a hint value, no spinlock held here, |
| 1875 | * if is not zero, it means the file is defragging. |
| 1876 | * Force cow if given extent needs to be defragged. |
| 1877 | */ |
Nikolay Borisov | 0c49422 | 2020-06-03 08:55:28 +0300 | [diff] [blame] | 1878 | if (inode->defrag_bytes && |
| 1879 | test_range_bit(&inode->io_tree, start, end, EXTENT_DEFRAG, 0, NULL)) |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 1880 | return 1; |
| 1881 | |
| 1882 | return 0; |
| 1883 | } |
| 1884 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1885 | /* |
Nikolay Borisov | 5eaad97 | 2018-11-01 14:09:46 +0200 | [diff] [blame] | 1886 | * Function to process delayed allocation (create CoW) for ranges which are |
| 1887 | * being touched for the first time. |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1888 | */ |
Nikolay Borisov | 98456b9 | 2020-06-03 08:55:29 +0300 | [diff] [blame] | 1889 | 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] | 1890 | u64 start, u64 end, int *page_started, unsigned long *nr_written, |
| 1891 | struct writeback_control *wbc) |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1892 | { |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1893 | int ret; |
Nikolay Borisov | 98456b9 | 2020-06-03 08:55:29 +0300 | [diff] [blame] | 1894 | int force_cow = need_force_cow(inode, start, end); |
Naohiro Aota | 42c0110 | 2021-02-04 19:22:07 +0900 | [diff] [blame] | 1895 | const bool zoned = btrfs_is_zoned(inode->root->fs_info); |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1896 | |
Nikolay Borisov | 98456b9 | 2020-06-03 08:55:29 +0300 | [diff] [blame] | 1897 | if (inode->flags & BTRFS_INODE_NODATACOW && !force_cow) { |
Naohiro Aota | 42c0110 | 2021-02-04 19:22:07 +0900 | [diff] [blame] | 1898 | ASSERT(!zoned); |
Nikolay Borisov | 98456b9 | 2020-06-03 08:55:29 +0300 | [diff] [blame] | 1899 | ret = run_delalloc_nocow(inode, locked_page, start, end, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1900 | page_started, 1, nr_written); |
Nikolay Borisov | 98456b9 | 2020-06-03 08:55:29 +0300 | [diff] [blame] | 1901 | } else if (inode->flags & BTRFS_INODE_PREALLOC && !force_cow) { |
Naohiro Aota | 42c0110 | 2021-02-04 19:22:07 +0900 | [diff] [blame] | 1902 | ASSERT(!zoned); |
Nikolay Borisov | 98456b9 | 2020-06-03 08:55:29 +0300 | [diff] [blame] | 1903 | ret = run_delalloc_nocow(inode, locked_page, start, end, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1904 | page_started, 0, nr_written); |
Nikolay Borisov | 98456b9 | 2020-06-03 08:55:29 +0300 | [diff] [blame] | 1905 | } else if (!inode_can_compress(inode) || |
| 1906 | !inode_need_compress(inode, start, end)) { |
Naohiro Aota | 42c0110 | 2021-02-04 19:22:07 +0900 | [diff] [blame] | 1907 | if (zoned) |
| 1908 | ret = run_delalloc_zoned(inode, locked_page, start, end, |
| 1909 | page_started, nr_written); |
| 1910 | else |
| 1911 | ret = cow_file_range(inode, locked_page, start, end, |
| 1912 | page_started, nr_written, 1); |
Josef Bacik | 7ddf5a4 | 2012-06-08 15:26:47 -0400 | [diff] [blame] | 1913 | } else { |
Nikolay Borisov | 98456b9 | 2020-06-03 08:55:29 +0300 | [diff] [blame] | 1914 | set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, &inode->runtime_flags); |
| 1915 | ret = cow_file_range_async(inode, wbc, locked_page, start, end, |
David Sterba | fac07d2 | 2019-10-29 18:28:57 +0100 | [diff] [blame] | 1916 | page_started, nr_written); |
Josef Bacik | 7ddf5a4 | 2012-06-08 15:26:47 -0400 | [diff] [blame] | 1917 | } |
Qu Wenruo | 52427260 | 2017-03-08 10:25:52 +0800 | [diff] [blame] | 1918 | if (ret) |
Nikolay Borisov | 98456b9 | 2020-06-03 08:55:29 +0300 | [diff] [blame] | 1919 | btrfs_cleanup_ordered_extents(inode, locked_page, start, |
Nikolay Borisov | d1051d6 | 2018-11-21 17:10:52 +0200 | [diff] [blame] | 1920 | end - start + 1); |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 1921 | return ret; |
| 1922 | } |
| 1923 | |
Nikolay Borisov | abbb55f | 2018-11-01 14:09:53 +0200 | [diff] [blame] | 1924 | void btrfs_split_delalloc_extent(struct inode *inode, |
| 1925 | struct extent_state *orig, u64 split) |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1926 | { |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1927 | u64 size; |
| 1928 | |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1929 | /* not delalloc, ignore it */ |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1930 | if (!(orig->state & EXTENT_DELALLOC)) |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 1931 | return; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1932 | |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1933 | size = orig->end - orig->start + 1; |
| 1934 | if (size > BTRFS_MAX_EXTENT_SIZE) { |
David Sterba | 823bb20 | 2017-01-04 11:09:51 +0100 | [diff] [blame] | 1935 | u32 num_extents; |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1936 | u64 new_size; |
| 1937 | |
| 1938 | /* |
Nikolay Borisov | 5c84819 | 2018-11-01 14:09:52 +0200 | [diff] [blame] | 1939 | * See the explanation in btrfs_merge_delalloc_extent, the same |
Josef Bacik | ba11721 | 2015-03-13 15:01:24 -0400 | [diff] [blame] | 1940 | * applies here, just in reverse. |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1941 | */ |
| 1942 | new_size = orig->end - split + 1; |
David Sterba | 823bb20 | 2017-01-04 11:09:51 +0100 | [diff] [blame] | 1943 | num_extents = count_max_extents(new_size); |
Josef Bacik | ba11721 | 2015-03-13 15:01:24 -0400 | [diff] [blame] | 1944 | new_size = split - orig->start; |
David Sterba | 823bb20 | 2017-01-04 11:09:51 +0100 | [diff] [blame] | 1945 | num_extents += count_max_extents(new_size); |
| 1946 | if (count_max_extents(size) >= num_extents) |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1947 | return; |
| 1948 | } |
| 1949 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1950 | spin_lock(&BTRFS_I(inode)->lock); |
Josef Bacik | 8b62f87 | 2017-10-19 14:15:55 -0400 | [diff] [blame] | 1951 | btrfs_mod_outstanding_extents(BTRFS_I(inode), 1); |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1952 | spin_unlock(&BTRFS_I(inode)->lock); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1953 | } |
| 1954 | |
| 1955 | /* |
Nikolay Borisov | 5c84819 | 2018-11-01 14:09:52 +0200 | [diff] [blame] | 1956 | * Handle merged delayed allocation extents so we can keep track of new extents |
| 1957 | * that are just merged onto old extents, such as when we are doing sequential |
| 1958 | * 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] | 1959 | */ |
Nikolay Borisov | 5c84819 | 2018-11-01 14:09:52 +0200 | [diff] [blame] | 1960 | void btrfs_merge_delalloc_extent(struct inode *inode, struct extent_state *new, |
| 1961 | struct extent_state *other) |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1962 | { |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1963 | u64 new_size, old_size; |
David Sterba | 823bb20 | 2017-01-04 11:09:51 +0100 | [diff] [blame] | 1964 | u32 num_extents; |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1965 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1966 | /* not delalloc, ignore it */ |
| 1967 | if (!(other->state & EXTENT_DELALLOC)) |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 1968 | return; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1969 | |
Josef Bacik | 8461a3d | 2015-03-13 15:12:08 -0400 | [diff] [blame] | 1970 | if (new->start > other->start) |
| 1971 | new_size = new->end - other->start + 1; |
| 1972 | else |
| 1973 | new_size = other->end - new->start + 1; |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1974 | |
| 1975 | /* we're not bigger than the max, unreserve the space and go */ |
| 1976 | if (new_size <= BTRFS_MAX_EXTENT_SIZE) { |
| 1977 | spin_lock(&BTRFS_I(inode)->lock); |
Josef Bacik | 8b62f87 | 2017-10-19 14:15:55 -0400 | [diff] [blame] | 1978 | btrfs_mod_outstanding_extents(BTRFS_I(inode), -1); |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1979 | spin_unlock(&BTRFS_I(inode)->lock); |
| 1980 | return; |
| 1981 | } |
| 1982 | |
| 1983 | /* |
Josef Bacik | ba11721 | 2015-03-13 15:01:24 -0400 | [diff] [blame] | 1984 | * We have to add up either side to figure out how many extents were |
| 1985 | * accounted for before we merged into one big extent. If the number of |
| 1986 | * extents we accounted for is <= the amount we need for the new range |
| 1987 | * then we can return, otherwise drop. Think of it like this |
| 1988 | * |
| 1989 | * [ 4k][MAX_SIZE] |
| 1990 | * |
| 1991 | * So we've grown the extent by a MAX_SIZE extent, this would mean we |
| 1992 | * need 2 outstanding extents, on one side we have 1 and the other side |
| 1993 | * we have 1 so they are == and we can return. But in this case |
| 1994 | * |
| 1995 | * [MAX_SIZE+4k][MAX_SIZE+4k] |
| 1996 | * |
| 1997 | * Each range on their own accounts for 2 extents, but merged together |
| 1998 | * they are only 3 extents worth of accounting, so we need to drop in |
| 1999 | * this case. |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 2000 | */ |
Josef Bacik | ba11721 | 2015-03-13 15:01:24 -0400 | [diff] [blame] | 2001 | old_size = other->end - other->start + 1; |
David Sterba | 823bb20 | 2017-01-04 11:09:51 +0100 | [diff] [blame] | 2002 | num_extents = count_max_extents(old_size); |
Josef Bacik | ba11721 | 2015-03-13 15:01:24 -0400 | [diff] [blame] | 2003 | old_size = new->end - new->start + 1; |
David Sterba | 823bb20 | 2017-01-04 11:09:51 +0100 | [diff] [blame] | 2004 | num_extents += count_max_extents(old_size); |
| 2005 | if (count_max_extents(new_size) >= num_extents) |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 2006 | return; |
| 2007 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 2008 | spin_lock(&BTRFS_I(inode)->lock); |
Josef Bacik | 8b62f87 | 2017-10-19 14:15:55 -0400 | [diff] [blame] | 2009 | btrfs_mod_outstanding_extents(BTRFS_I(inode), -1); |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 2010 | spin_unlock(&BTRFS_I(inode)->lock); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 2011 | } |
| 2012 | |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 2013 | static void btrfs_add_delalloc_inodes(struct btrfs_root *root, |
| 2014 | struct inode *inode) |
| 2015 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2016 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
| 2017 | |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 2018 | spin_lock(&root->delalloc_lock); |
| 2019 | if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) { |
| 2020 | list_add_tail(&BTRFS_I(inode)->delalloc_inodes, |
| 2021 | &root->delalloc_inodes); |
| 2022 | set_bit(BTRFS_INODE_IN_DELALLOC_LIST, |
| 2023 | &BTRFS_I(inode)->runtime_flags); |
| 2024 | root->nr_delalloc_inodes++; |
| 2025 | if (root->nr_delalloc_inodes == 1) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2026 | spin_lock(&fs_info->delalloc_root_lock); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 2027 | BUG_ON(!list_empty(&root->delalloc_root)); |
| 2028 | list_add_tail(&root->delalloc_root, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2029 | &fs_info->delalloc_roots); |
| 2030 | spin_unlock(&fs_info->delalloc_root_lock); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 2031 | } |
| 2032 | } |
| 2033 | spin_unlock(&root->delalloc_lock); |
| 2034 | } |
| 2035 | |
Nikolay Borisov | 2b87733 | 2018-04-27 12:21:51 +0300 | [diff] [blame] | 2036 | |
| 2037 | void __btrfs_del_delalloc_inode(struct btrfs_root *root, |
| 2038 | struct btrfs_inode *inode) |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 2039 | { |
David Sterba | 3ffbd68 | 2018-06-29 10:56:42 +0200 | [diff] [blame] | 2040 | struct btrfs_fs_info *fs_info = root->fs_info; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2041 | |
Nikolay Borisov | 9e3e97f | 2017-02-20 13:51:07 +0200 | [diff] [blame] | 2042 | if (!list_empty(&inode->delalloc_inodes)) { |
| 2043 | list_del_init(&inode->delalloc_inodes); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 2044 | clear_bit(BTRFS_INODE_IN_DELALLOC_LIST, |
Nikolay Borisov | 9e3e97f | 2017-02-20 13:51:07 +0200 | [diff] [blame] | 2045 | &inode->runtime_flags); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 2046 | root->nr_delalloc_inodes--; |
| 2047 | if (!root->nr_delalloc_inodes) { |
Nikolay Borisov | 7c8a0d3 | 2018-04-27 12:21:52 +0300 | [diff] [blame] | 2048 | ASSERT(list_empty(&root->delalloc_inodes)); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2049 | spin_lock(&fs_info->delalloc_root_lock); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 2050 | BUG_ON(list_empty(&root->delalloc_root)); |
| 2051 | list_del_init(&root->delalloc_root); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2052 | spin_unlock(&fs_info->delalloc_root_lock); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 2053 | } |
| 2054 | } |
Nikolay Borisov | 2b87733 | 2018-04-27 12:21:51 +0300 | [diff] [blame] | 2055 | } |
| 2056 | |
| 2057 | static void btrfs_del_delalloc_inode(struct btrfs_root *root, |
| 2058 | struct btrfs_inode *inode) |
| 2059 | { |
| 2060 | spin_lock(&root->delalloc_lock); |
| 2061 | __btrfs_del_delalloc_inode(root, inode); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 2062 | spin_unlock(&root->delalloc_lock); |
| 2063 | } |
| 2064 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2065 | /* |
Nikolay Borisov | e06a1fc | 2018-11-01 14:09:50 +0200 | [diff] [blame] | 2066 | * Properly track delayed allocation bytes in the inode and to maintain the |
| 2067 | * list of inodes that have pending delalloc work to be done. |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2068 | */ |
Nikolay Borisov | e06a1fc | 2018-11-01 14:09:50 +0200 | [diff] [blame] | 2069 | void btrfs_set_delalloc_extent(struct inode *inode, struct extent_state *state, |
| 2070 | unsigned *bits) |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 2071 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2072 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
| 2073 | |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 2074 | if ((*bits & EXTENT_DEFRAG) && !(*bits & EXTENT_DELALLOC)) |
| 2075 | WARN_ON(1); |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 2076 | /* |
| 2077 | * set_bit and clear bit hooks normally require _irqsave/restore |
Sergei Trofimovich | 27160b6 | 2011-05-20 20:20:32 +0000 | [diff] [blame] | 2078 | * but in this case, we are only testing for the DELALLOC |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 2079 | * bit, which is only set or cleared with irqs on |
| 2080 | */ |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 2081 | if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) { |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 2082 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 2083 | u64 len = state->end + 1 - state->start; |
Josef Bacik | 8b62f87 | 2017-10-19 14:15:55 -0400 | [diff] [blame] | 2084 | u32 num_extents = count_max_extents(len); |
Nikolay Borisov | 70ddc55 | 2017-02-20 13:50:35 +0200 | [diff] [blame] | 2085 | bool do_list = !btrfs_is_free_space_inode(BTRFS_I(inode)); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 2086 | |
Josef Bacik | 8b62f87 | 2017-10-19 14:15:55 -0400 | [diff] [blame] | 2087 | spin_lock(&BTRFS_I(inode)->lock); |
| 2088 | btrfs_mod_outstanding_extents(BTRFS_I(inode), num_extents); |
| 2089 | spin_unlock(&BTRFS_I(inode)->lock); |
Josef Bacik | 287a0ab | 2010-03-19 18:07:23 +0000 | [diff] [blame] | 2090 | |
Josef Bacik | 6a3891c | 2015-03-16 17:38:52 -0400 | [diff] [blame] | 2091 | /* For sanity tests */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2092 | if (btrfs_is_testing(fs_info)) |
Josef Bacik | 6a3891c | 2015-03-16 17:38:52 -0400 | [diff] [blame] | 2093 | return; |
| 2094 | |
Nikolay Borisov | 104b4e5 | 2017-06-20 21:01:20 +0300 | [diff] [blame] | 2095 | percpu_counter_add_batch(&fs_info->delalloc_bytes, len, |
| 2096 | fs_info->delalloc_batch); |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 2097 | spin_lock(&BTRFS_I(inode)->lock); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 2098 | BTRFS_I(inode)->delalloc_bytes += len; |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 2099 | if (*bits & EXTENT_DEFRAG) |
| 2100 | BTRFS_I(inode)->defrag_bytes += len; |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 2101 | if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST, |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 2102 | &BTRFS_I(inode)->runtime_flags)) |
| 2103 | btrfs_add_delalloc_inodes(root, inode); |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 2104 | spin_unlock(&BTRFS_I(inode)->lock); |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 2105 | } |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 2106 | |
| 2107 | if (!(state->state & EXTENT_DELALLOC_NEW) && |
| 2108 | (*bits & EXTENT_DELALLOC_NEW)) { |
| 2109 | spin_lock(&BTRFS_I(inode)->lock); |
| 2110 | BTRFS_I(inode)->new_delalloc_bytes += state->end + 1 - |
| 2111 | state->start; |
| 2112 | spin_unlock(&BTRFS_I(inode)->lock); |
| 2113 | } |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 2114 | } |
| 2115 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2116 | /* |
Nikolay Borisov | a36bb5f | 2018-11-01 14:09:51 +0200 | [diff] [blame] | 2117 | * Once a range is no longer delalloc this function ensures that proper |
| 2118 | * accounting happens. |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2119 | */ |
Nikolay Borisov | a36bb5f | 2018-11-01 14:09:51 +0200 | [diff] [blame] | 2120 | void btrfs_clear_delalloc_extent(struct inode *vfs_inode, |
| 2121 | struct extent_state *state, unsigned *bits) |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 2122 | { |
Nikolay Borisov | a36bb5f | 2018-11-01 14:09:51 +0200 | [diff] [blame] | 2123 | struct btrfs_inode *inode = BTRFS_I(vfs_inode); |
| 2124 | struct btrfs_fs_info *fs_info = btrfs_sb(vfs_inode->i_sb); |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 2125 | u64 len = state->end + 1 - state->start; |
David Sterba | 823bb20 | 2017-01-04 11:09:51 +0100 | [diff] [blame] | 2126 | u32 num_extents = count_max_extents(len); |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 2127 | |
Filipe Manana | 4a4b964 | 2017-07-27 19:52:55 +0100 | [diff] [blame] | 2128 | if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG)) { |
| 2129 | spin_lock(&inode->lock); |
Nikolay Borisov | 6fc0ef6 | 2017-02-20 13:51:03 +0200 | [diff] [blame] | 2130 | inode->defrag_bytes -= len; |
Filipe Manana | 4a4b964 | 2017-07-27 19:52:55 +0100 | [diff] [blame] | 2131 | spin_unlock(&inode->lock); |
| 2132 | } |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 2133 | |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 2134 | /* |
| 2135 | * set_bit and clear bit hooks normally require _irqsave/restore |
Sergei Trofimovich | 27160b6 | 2011-05-20 20:20:32 +0000 | [diff] [blame] | 2136 | * but in this case, we are only testing for the DELALLOC |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 2137 | * bit, which is only set or cleared with irqs on |
| 2138 | */ |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 2139 | if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) { |
Nikolay Borisov | 6fc0ef6 | 2017-02-20 13:51:03 +0200 | [diff] [blame] | 2140 | struct btrfs_root *root = inode->root; |
Liu Bo | 83eea1f | 2012-07-10 05:28:39 -0600 | [diff] [blame] | 2141 | bool do_list = !btrfs_is_free_space_inode(inode); |
Chris Mason | bcbfce8 | 2008-04-22 13:26:47 -0400 | [diff] [blame] | 2142 | |
Josef Bacik | 8b62f87 | 2017-10-19 14:15:55 -0400 | [diff] [blame] | 2143 | spin_lock(&inode->lock); |
| 2144 | btrfs_mod_outstanding_extents(inode, -num_extents); |
| 2145 | spin_unlock(&inode->lock); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 2146 | |
Josef Bacik | b6d08f0 | 2013-09-27 14:57:43 -0400 | [diff] [blame] | 2147 | /* |
| 2148 | * We don't reserve metadata space for space cache inodes so we |
Andrea Gelmini | 52042d8 | 2018-11-28 12:05:13 +0100 | [diff] [blame] | 2149 | * don't need to call delalloc_release_metadata if there is an |
Josef Bacik | b6d08f0 | 2013-09-27 14:57:43 -0400 | [diff] [blame] | 2150 | * error. |
| 2151 | */ |
Filipe Manana | a315e68 | 2017-03-06 23:04:20 +0000 | [diff] [blame] | 2152 | if (*bits & EXTENT_CLEAR_META_RESV && |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2153 | root != fs_info->tree_root) |
Qu Wenruo | 43b1859 | 2017-12-12 15:34:32 +0800 | [diff] [blame] | 2154 | btrfs_delalloc_release_metadata(inode, len, false); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 2155 | |
Josef Bacik | 6a3891c | 2015-03-16 17:38:52 -0400 | [diff] [blame] | 2156 | /* For sanity tests. */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2157 | if (btrfs_is_testing(fs_info)) |
Josef Bacik | 6a3891c | 2015-03-16 17:38:52 -0400 | [diff] [blame] | 2158 | return; |
| 2159 | |
Filipe Manana | a315e68 | 2017-03-06 23:04:20 +0000 | [diff] [blame] | 2160 | if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID && |
| 2161 | do_list && !(state->state & EXTENT_NORESERVE) && |
| 2162 | (*bits & EXTENT_CLEAR_DATA_RESV)) |
Nikolay Borisov | 9db5d51 | 2020-06-03 08:55:38 +0300 | [diff] [blame] | 2163 | btrfs_free_reserved_data_space_noquota(fs_info, len); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 2164 | |
Nikolay Borisov | 104b4e5 | 2017-06-20 21:01:20 +0300 | [diff] [blame] | 2165 | percpu_counter_add_batch(&fs_info->delalloc_bytes, -len, |
| 2166 | fs_info->delalloc_batch); |
Nikolay Borisov | 6fc0ef6 | 2017-02-20 13:51:03 +0200 | [diff] [blame] | 2167 | spin_lock(&inode->lock); |
| 2168 | inode->delalloc_bytes -= len; |
| 2169 | if (do_list && inode->delalloc_bytes == 0 && |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 2170 | test_bit(BTRFS_INODE_IN_DELALLOC_LIST, |
Nikolay Borisov | 9e3e97f | 2017-02-20 13:51:07 +0200 | [diff] [blame] | 2171 | &inode->runtime_flags)) |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 2172 | btrfs_del_delalloc_inode(root, inode); |
Nikolay Borisov | 6fc0ef6 | 2017-02-20 13:51:03 +0200 | [diff] [blame] | 2173 | spin_unlock(&inode->lock); |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 2174 | } |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 2175 | |
| 2176 | if ((state->state & EXTENT_DELALLOC_NEW) && |
| 2177 | (*bits & EXTENT_DELALLOC_NEW)) { |
| 2178 | spin_lock(&inode->lock); |
| 2179 | ASSERT(inode->new_delalloc_bytes >= len); |
| 2180 | inode->new_delalloc_bytes -= len; |
Filipe Manana | 2766ff6 | 2020-11-04 11:07:34 +0000 | [diff] [blame] | 2181 | if (*bits & EXTENT_ADD_INODE_BYTES) |
| 2182 | inode_add_bytes(&inode->vfs_inode, len); |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 2183 | spin_unlock(&inode->lock); |
| 2184 | } |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 2185 | } |
| 2186 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2187 | /* |
Nikolay Borisov | da12fe5 | 2018-11-27 20:57:58 +0200 | [diff] [blame] | 2188 | * btrfs_bio_fits_in_stripe - Checks whether the size of the given bio will fit |
| 2189 | * in a chunk's stripe. This function ensures that bios do not span a |
| 2190 | * stripe/chunk |
Liu Bo | 6f034ec | 2016-06-22 18:31:49 -0700 | [diff] [blame] | 2191 | * |
Nikolay Borisov | da12fe5 | 2018-11-27 20:57:58 +0200 | [diff] [blame] | 2192 | * @page - The page we are about to add to the bio |
| 2193 | * @size - size we want to add to the bio |
| 2194 | * @bio - bio we want to ensure is smaller than a stripe |
| 2195 | * @bio_flags - flags of the bio |
| 2196 | * |
| 2197 | * return 1 if page cannot be added to the bio |
| 2198 | * return 0 if page can be added to the bio |
Liu Bo | 6f034ec | 2016-06-22 18:31:49 -0700 | [diff] [blame] | 2199 | * return error otherwise |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2200 | */ |
Nikolay Borisov | da12fe5 | 2018-11-27 20:57:58 +0200 | [diff] [blame] | 2201 | int btrfs_bio_fits_in_stripe(struct page *page, size_t size, struct bio *bio, |
| 2202 | unsigned long bio_flags) |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 2203 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2204 | struct inode *inode = page->mapping->host; |
| 2205 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
David Sterba | 1201b58 | 2020-11-26 15:41:27 +0100 | [diff] [blame] | 2206 | u64 logical = bio->bi_iter.bi_sector << 9; |
Michal Rostecki | 4203431 | 2021-01-27 14:57:27 +0100 | [diff] [blame] | 2207 | struct extent_map *em; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 2208 | u64 length = 0; |
| 2209 | u64 map_length; |
Michal Rostecki | 4203431 | 2021-01-27 14:57:27 +0100 | [diff] [blame] | 2210 | int ret = 0; |
Nikolay Borisov | 89b798a | 2019-06-03 12:05:05 +0300 | [diff] [blame] | 2211 | struct btrfs_io_geometry geom; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 2212 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 2213 | if (bio_flags & EXTENT_BIO_COMPRESSED) |
| 2214 | return 0; |
| 2215 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 2216 | length = bio->bi_iter.bi_size; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 2217 | map_length = length; |
Michal Rostecki | 4203431 | 2021-01-27 14:57:27 +0100 | [diff] [blame] | 2218 | em = btrfs_get_chunk_map(fs_info, logical, map_length); |
| 2219 | if (IS_ERR(em)) |
| 2220 | return PTR_ERR(em); |
| 2221 | ret = btrfs_get_io_geometry(fs_info, em, btrfs_op(bio), logical, |
| 2222 | map_length, &geom); |
Liu Bo | 6f034ec | 2016-06-22 18:31:49 -0700 | [diff] [blame] | 2223 | if (ret < 0) |
Michal Rostecki | 4203431 | 2021-01-27 14:57:27 +0100 | [diff] [blame] | 2224 | goto out; |
Nikolay Borisov | 89b798a | 2019-06-03 12:05:05 +0300 | [diff] [blame] | 2225 | |
| 2226 | if (geom.len < length + size) |
Michal Rostecki | 4203431 | 2021-01-27 14:57:27 +0100 | [diff] [blame] | 2227 | ret = 1; |
| 2228 | out: |
| 2229 | free_extent_map(em); |
| 2230 | return ret; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 2231 | } |
| 2232 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2233 | /* |
| 2234 | * in order to insert checksums into the metadata in large chunks, |
| 2235 | * we wait until bio submission time. All the pages in the bio are |
| 2236 | * checksummed and sums are attached onto the ordered extent record. |
| 2237 | * |
| 2238 | * At IO completion time the cums attached on the ordered extent record |
| 2239 | * are inserted into the btree |
| 2240 | */ |
Qu Wenruo | 8896a08 | 2020-10-21 14:24:53 +0800 | [diff] [blame] | 2241 | 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] | 2242 | u64 dio_file_offset) |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 2243 | { |
Johannes Thumshirn | c965d64 | 2020-07-28 20:25:41 +0900 | [diff] [blame] | 2244 | return btrfs_csum_one_bio(BTRFS_I(inode), bio, 0, 0); |
Chris Mason | 4a69a41 | 2008-11-06 22:03:00 -0500 | [diff] [blame] | 2245 | } |
Chris Mason | e015640 | 2008-04-16 11:15:20 -0400 | [diff] [blame] | 2246 | |
Johannes Thumshirn | cacb2ce | 2021-02-04 19:22:01 +0900 | [diff] [blame] | 2247 | bool btrfs_bio_fits_in_ordered_extent(struct page *page, struct bio *bio, |
| 2248 | unsigned int size) |
| 2249 | { |
| 2250 | struct btrfs_inode *inode = BTRFS_I(page->mapping->host); |
| 2251 | struct btrfs_fs_info *fs_info = inode->root->fs_info; |
| 2252 | struct btrfs_ordered_extent *ordered; |
| 2253 | u64 len = bio->bi_iter.bi_size + size; |
| 2254 | bool ret = true; |
| 2255 | |
| 2256 | ASSERT(btrfs_is_zoned(fs_info)); |
| 2257 | ASSERT(fs_info->max_zone_append_size > 0); |
| 2258 | ASSERT(bio_op(bio) == REQ_OP_ZONE_APPEND); |
| 2259 | |
| 2260 | /* Ordered extent not yet created, so we're good */ |
| 2261 | ordered = btrfs_lookup_ordered_extent(inode, page_offset(page)); |
| 2262 | if (!ordered) |
| 2263 | return ret; |
| 2264 | |
| 2265 | if ((bio->bi_iter.bi_sector << SECTOR_SHIFT) + len > |
| 2266 | ordered->disk_bytenr + ordered->disk_num_bytes) |
| 2267 | ret = false; |
| 2268 | |
| 2269 | btrfs_put_ordered_extent(ordered); |
| 2270 | |
| 2271 | return ret; |
| 2272 | } |
| 2273 | |
Naohiro Aota | d22002f | 2021-02-04 19:22:00 +0900 | [diff] [blame] | 2274 | static blk_status_t extract_ordered_extent(struct btrfs_inode *inode, |
| 2275 | struct bio *bio, loff_t file_offset) |
| 2276 | { |
| 2277 | struct btrfs_ordered_extent *ordered; |
| 2278 | struct extent_map *em = NULL, *em_new = NULL; |
| 2279 | struct extent_map_tree *em_tree = &inode->extent_tree; |
| 2280 | u64 start = (u64)bio->bi_iter.bi_sector << SECTOR_SHIFT; |
| 2281 | u64 len = bio->bi_iter.bi_size; |
| 2282 | u64 end = start + len; |
| 2283 | u64 ordered_end; |
| 2284 | u64 pre, post; |
| 2285 | int ret = 0; |
| 2286 | |
| 2287 | ordered = btrfs_lookup_ordered_extent(inode, file_offset); |
| 2288 | if (WARN_ON_ONCE(!ordered)) |
| 2289 | return BLK_STS_IOERR; |
| 2290 | |
| 2291 | /* No need to split */ |
| 2292 | if (ordered->disk_num_bytes == len) |
| 2293 | goto out; |
| 2294 | |
| 2295 | /* We cannot split once end_bio'd ordered extent */ |
| 2296 | if (WARN_ON_ONCE(ordered->bytes_left != ordered->disk_num_bytes)) { |
| 2297 | ret = -EINVAL; |
| 2298 | goto out; |
| 2299 | } |
| 2300 | |
| 2301 | /* We cannot split a compressed ordered extent */ |
| 2302 | if (WARN_ON_ONCE(ordered->disk_num_bytes != ordered->num_bytes)) { |
| 2303 | ret = -EINVAL; |
| 2304 | goto out; |
| 2305 | } |
| 2306 | |
| 2307 | ordered_end = ordered->disk_bytenr + ordered->disk_num_bytes; |
| 2308 | /* bio must be in one ordered extent */ |
| 2309 | if (WARN_ON_ONCE(start < ordered->disk_bytenr || end > ordered_end)) { |
| 2310 | ret = -EINVAL; |
| 2311 | goto out; |
| 2312 | } |
| 2313 | |
| 2314 | /* Checksum list should be empty */ |
| 2315 | if (WARN_ON_ONCE(!list_empty(&ordered->list))) { |
| 2316 | ret = -EINVAL; |
| 2317 | goto out; |
| 2318 | } |
| 2319 | |
| 2320 | pre = start - ordered->disk_bytenr; |
| 2321 | post = ordered_end - end; |
| 2322 | |
| 2323 | ret = btrfs_split_ordered_extent(ordered, pre, post); |
| 2324 | if (ret) |
| 2325 | goto out; |
| 2326 | |
| 2327 | read_lock(&em_tree->lock); |
| 2328 | em = lookup_extent_mapping(em_tree, ordered->file_offset, len); |
| 2329 | if (!em) { |
| 2330 | read_unlock(&em_tree->lock); |
| 2331 | ret = -EIO; |
| 2332 | goto out; |
| 2333 | } |
| 2334 | read_unlock(&em_tree->lock); |
| 2335 | |
| 2336 | ASSERT(!test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)); |
| 2337 | /* |
| 2338 | * We cannot reuse em_new here but have to create a new one, as |
| 2339 | * unpin_extent_cache() expects the start of the extent map to be the |
| 2340 | * logical offset of the file, which does not hold true anymore after |
| 2341 | * splitting. |
| 2342 | */ |
| 2343 | em_new = create_io_em(inode, em->start + pre, len, |
| 2344 | em->start + pre, em->block_start + pre, len, |
| 2345 | len, len, BTRFS_COMPRESS_NONE, |
| 2346 | BTRFS_ORDERED_REGULAR); |
| 2347 | if (IS_ERR(em_new)) { |
| 2348 | ret = PTR_ERR(em_new); |
| 2349 | goto out; |
| 2350 | } |
| 2351 | free_extent_map(em_new); |
| 2352 | |
| 2353 | out: |
| 2354 | free_extent_map(em); |
| 2355 | btrfs_put_ordered_extent(ordered); |
| 2356 | |
| 2357 | return errno_to_blk_status(ret); |
| 2358 | } |
| 2359 | |
Chris Mason | 4a69a41 | 2008-11-06 22:03:00 -0500 | [diff] [blame] | 2360 | /* |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 2361 | * 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] | 2362 | * on write, or reading the csums from the tree before a read. |
| 2363 | * |
| 2364 | * Rules about async/sync submit, |
| 2365 | * a) read: sync submit |
| 2366 | * |
| 2367 | * b) write without checksum: sync submit |
| 2368 | * |
| 2369 | * c) write with checksum: |
| 2370 | * c-1) if bio is issued by fsync: sync submit |
| 2371 | * (sync_writers != 0) |
| 2372 | * |
| 2373 | * c-2) if root is reloc root: sync submit |
| 2374 | * (only in case of buffered IO) |
| 2375 | * |
| 2376 | * c-3) otherwise: async submit |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2377 | */ |
Nikolay Borisov | 908930f | 2020-09-18 16:34:37 +0300 | [diff] [blame] | 2378 | blk_status_t btrfs_submit_data_bio(struct inode *inode, struct bio *bio, |
| 2379 | int mirror_num, unsigned long bio_flags) |
Nikolay Borisov | 50489a5 | 2019-04-10 19:46:04 +0300 | [diff] [blame] | 2380 | |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 2381 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2382 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 2383 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chandan Rajendra | 0d51e28 | 2015-07-27 15:26:43 +0530 | [diff] [blame] | 2384 | enum btrfs_wq_endio_type metadata = BTRFS_WQ_ENDIO_DATA; |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 2385 | blk_status_t ret = 0; |
Chris Mason | 19b9bdb | 2008-10-30 14:23:13 -0400 | [diff] [blame] | 2386 | int skip_sum; |
Josef Bacik | b812ce2 | 2012-11-16 13:56:32 -0500 | [diff] [blame] | 2387 | int async = !atomic_read(&BTRFS_I(inode)->sync_writers); |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 2388 | |
Josef Bacik | 42437a6 | 2020-10-16 11:29:18 -0400 | [diff] [blame] | 2389 | skip_sum = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM) || |
| 2390 | !fs_info->csum_root; |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 2391 | |
Nikolay Borisov | 70ddc55 | 2017-02-20 13:50:35 +0200 | [diff] [blame] | 2392 | if (btrfs_is_free_space_inode(BTRFS_I(inode))) |
Chandan Rajendra | 0d51e28 | 2015-07-27 15:26:43 +0530 | [diff] [blame] | 2393 | metadata = BTRFS_WQ_ENDIO_FREE_SPACE; |
Jeff Mahoney | 0417341 | 2011-10-03 23:23:12 -0400 | [diff] [blame] | 2394 | |
Naohiro Aota | d22002f | 2021-02-04 19:22:00 +0900 | [diff] [blame] | 2395 | if (bio_op(bio) == REQ_OP_ZONE_APPEND) { |
| 2396 | struct page *page = bio_first_bvec_all(bio)->bv_page; |
| 2397 | loff_t file_offset = page_offset(page); |
| 2398 | |
| 2399 | ret = extract_ordered_extent(BTRFS_I(inode), bio, file_offset); |
| 2400 | if (ret) |
| 2401 | goto out; |
| 2402 | } |
| 2403 | |
Naohiro Aota | cfe9444 | 2021-02-04 19:21:59 +0900 | [diff] [blame] | 2404 | if (btrfs_op(bio) != BTRFS_MAP_WRITE) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2405 | ret = btrfs_bio_wq_end_io(fs_info, bio, metadata); |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2406 | if (ret) |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 2407 | goto out; |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2408 | |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 2409 | if (bio_flags & EXTENT_BIO_COMPRESSED) { |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 2410 | ret = btrfs_submit_compressed_read(inode, bio, |
| 2411 | mirror_num, |
| 2412 | bio_flags); |
| 2413 | goto out; |
Josef Bacik | 334c16d | 2020-10-16 11:29:14 -0400 | [diff] [blame] | 2414 | } else { |
| 2415 | /* |
| 2416 | * Lookup bio sums does extra checks around whether we |
| 2417 | * need to csum or not, which is why we ignore skip_sum |
| 2418 | * here. |
| 2419 | */ |
Qu Wenruo | 6275193 | 2020-12-02 14:48:06 +0800 | [diff] [blame] | 2420 | ret = btrfs_lookup_bio_sums(inode, bio, NULL); |
Tsutomu Itoh | c2db107 | 2011-03-01 06:48:31 +0000 | [diff] [blame] | 2421 | if (ret) |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 2422 | goto out; |
Tsutomu Itoh | c2db107 | 2011-03-01 06:48:31 +0000 | [diff] [blame] | 2423 | } |
Chris Mason | 4d1b5fb | 2008-08-20 09:44:52 -0400 | [diff] [blame] | 2424 | goto mapit; |
Josef Bacik | b812ce2 | 2012-11-16 13:56:32 -0500 | [diff] [blame] | 2425 | } else if (async && !skip_sum) { |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 2426 | /* csum items have already been cloned */ |
| 2427 | if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID) |
| 2428 | goto mapit; |
Chris Mason | 19b9bdb | 2008-10-30 14:23:13 -0400 | [diff] [blame] | 2429 | /* we're doing a write, do the async checksumming */ |
Qu Wenruo | 8896a08 | 2020-10-21 14:24:53 +0800 | [diff] [blame] | 2430 | ret = btrfs_wq_submit_bio(inode, bio, mirror_num, bio_flags, |
| 2431 | 0, btrfs_submit_bio_start); |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 2432 | goto out; |
Josef Bacik | b812ce2 | 2012-11-16 13:56:32 -0500 | [diff] [blame] | 2433 | } else if (!skip_sum) { |
Nikolay Borisov | bd242a0 | 2020-06-03 08:55:07 +0300 | [diff] [blame] | 2434 | ret = btrfs_csum_one_bio(BTRFS_I(inode), bio, 0, 0); |
Josef Bacik | b812ce2 | 2012-11-16 13:56:32 -0500 | [diff] [blame] | 2435 | if (ret) |
| 2436 | goto out; |
Chris Mason | 19b9bdb | 2008-10-30 14:23:13 -0400 | [diff] [blame] | 2437 | } |
| 2438 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2439 | mapit: |
Chris Mason | 08635ba | 2019-07-10 12:28:14 -0700 | [diff] [blame] | 2440 | ret = btrfs_map_bio(fs_info, bio, mirror_num); |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 2441 | |
| 2442 | out: |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 2443 | if (ret) { |
| 2444 | bio->bi_status = ret; |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 2445 | bio_endio(bio); |
| 2446 | } |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 2447 | return ret; |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 2448 | } |
Chris Mason | 6885f30 | 2008-02-20 16:11:05 -0500 | [diff] [blame] | 2449 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2450 | /* |
| 2451 | * given a list of ordered sums record them in the inode. This happens |
| 2452 | * at IO completion time based on sums calculated at bio submission time. |
| 2453 | */ |
Nikolay Borisov | 510f85e | 2020-09-18 12:15:52 +0300 | [diff] [blame] | 2454 | static int add_pending_csums(struct btrfs_trans_handle *trans, |
| 2455 | struct list_head *list) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2456 | { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2457 | struct btrfs_ordered_sum *sum; |
Nikolay Borisov | ac01f26 | 2018-01-08 10:59:43 +0200 | [diff] [blame] | 2458 | int ret; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2459 | |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 2460 | list_for_each_entry(sum, list, list) { |
David Sterba | 7c2871a | 2017-11-08 01:07:43 +0100 | [diff] [blame] | 2461 | trans->adding_csums = true; |
Nikolay Borisov | 510f85e | 2020-09-18 12:15:52 +0300 | [diff] [blame] | 2462 | ret = btrfs_csum_file_blocks(trans, trans->fs_info->csum_root, sum); |
David Sterba | 7c2871a | 2017-11-08 01:07:43 +0100 | [diff] [blame] | 2463 | trans->adding_csums = false; |
Nikolay Borisov | ac01f26 | 2018-01-08 10:59:43 +0200 | [diff] [blame] | 2464 | if (ret) |
| 2465 | return ret; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2466 | } |
| 2467 | return 0; |
| 2468 | } |
| 2469 | |
Filipe Manana | c334730 | 2020-11-04 11:07:31 +0000 | [diff] [blame] | 2470 | static int btrfs_find_new_delalloc_bytes(struct btrfs_inode *inode, |
| 2471 | const u64 start, |
| 2472 | const u64 len, |
| 2473 | struct extent_state **cached_state) |
| 2474 | { |
| 2475 | u64 search_start = start; |
| 2476 | const u64 end = start + len - 1; |
| 2477 | |
| 2478 | while (search_start < end) { |
| 2479 | const u64 search_len = end - search_start + 1; |
| 2480 | struct extent_map *em; |
| 2481 | u64 em_len; |
| 2482 | int ret = 0; |
| 2483 | |
| 2484 | em = btrfs_get_extent(inode, NULL, 0, search_start, search_len); |
| 2485 | if (IS_ERR(em)) |
| 2486 | return PTR_ERR(em); |
| 2487 | |
| 2488 | if (em->block_start != EXTENT_MAP_HOLE) |
| 2489 | goto next; |
| 2490 | |
| 2491 | em_len = em->len; |
| 2492 | if (em->start < search_start) |
| 2493 | em_len -= search_start - em->start; |
| 2494 | if (em_len > search_len) |
| 2495 | em_len = search_len; |
| 2496 | |
| 2497 | ret = set_extent_bit(&inode->io_tree, search_start, |
| 2498 | search_start + em_len - 1, |
Nikolay Borisov | 1cab5e7 | 2020-11-05 11:08:00 +0200 | [diff] [blame] | 2499 | EXTENT_DELALLOC_NEW, 0, NULL, cached_state, |
| 2500 | GFP_NOFS, NULL); |
Filipe Manana | c334730 | 2020-11-04 11:07:31 +0000 | [diff] [blame] | 2501 | next: |
| 2502 | search_start = extent_map_end(em); |
| 2503 | free_extent_map(em); |
| 2504 | if (ret) |
| 2505 | return ret; |
| 2506 | } |
| 2507 | return 0; |
| 2508 | } |
| 2509 | |
Nikolay Borisov | c2566f2 | 2020-06-03 08:55:35 +0300 | [diff] [blame] | 2510 | 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] | 2511 | unsigned int extra_bits, |
Nikolay Borisov | 330a582 | 2019-07-17 16:18:17 +0300 | [diff] [blame] | 2512 | struct extent_state **cached_state) |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 2513 | { |
Johannes Thumshirn | fdb1e12 | 2018-12-05 15:23:04 +0100 | [diff] [blame] | 2514 | WARN_ON(PAGE_ALIGNED(end)); |
Filipe Manana | c334730 | 2020-11-04 11:07:31 +0000 | [diff] [blame] | 2515 | |
| 2516 | if (start >= i_size_read(&inode->vfs_inode) && |
| 2517 | !(inode->flags & BTRFS_INODE_PREALLOC)) { |
| 2518 | /* |
| 2519 | * There can't be any extents following eof in this case so just |
| 2520 | * set the delalloc new bit for the range directly. |
| 2521 | */ |
| 2522 | extra_bits |= EXTENT_DELALLOC_NEW; |
| 2523 | } else { |
| 2524 | int ret; |
| 2525 | |
| 2526 | ret = btrfs_find_new_delalloc_bytes(inode, start, |
| 2527 | end + 1 - start, |
| 2528 | cached_state); |
| 2529 | if (ret) |
| 2530 | return ret; |
| 2531 | } |
| 2532 | |
Nikolay Borisov | c2566f2 | 2020-06-03 08:55:35 +0300 | [diff] [blame] | 2533 | return set_extent_delalloc(&inode->io_tree, start, end, extra_bits, |
| 2534 | cached_state); |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 2535 | } |
| 2536 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2537 | /* see btrfs_writepage_start_hook for details on why this is required */ |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2538 | struct btrfs_writepage_fixup { |
| 2539 | struct page *page; |
Josef Bacik | f4b1363 | 2020-01-21 14:34:52 -0500 | [diff] [blame] | 2540 | struct inode *inode; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2541 | struct btrfs_work work; |
| 2542 | }; |
| 2543 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 2544 | static void btrfs_writepage_fixup_worker(struct btrfs_work *work) |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2545 | { |
| 2546 | struct btrfs_writepage_fixup *fixup; |
| 2547 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 2548 | struct extent_state *cached_state = NULL; |
Qu Wenruo | 364ecf3 | 2017-02-27 15:10:38 +0800 | [diff] [blame] | 2549 | struct extent_changeset *data_reserved = NULL; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2550 | struct page *page; |
Nikolay Borisov | 65d87f7 | 2020-06-05 10:51:51 +0300 | [diff] [blame] | 2551 | struct btrfs_inode *inode; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2552 | u64 page_start; |
| 2553 | u64 page_end; |
Chris Mason | 25f3c50 | 2020-01-21 11:51:42 -0500 | [diff] [blame] | 2554 | int ret = 0; |
Josef Bacik | f4b1363 | 2020-01-21 14:34:52 -0500 | [diff] [blame] | 2555 | bool free_delalloc_space = true; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2556 | |
| 2557 | fixup = container_of(work, struct btrfs_writepage_fixup, work); |
| 2558 | page = fixup->page; |
Nikolay Borisov | 65d87f7 | 2020-06-05 10:51:51 +0300 | [diff] [blame] | 2559 | inode = BTRFS_I(fixup->inode); |
Josef Bacik | f4b1363 | 2020-01-21 14:34:52 -0500 | [diff] [blame] | 2560 | page_start = page_offset(page); |
| 2561 | page_end = page_offset(page) + PAGE_SIZE - 1; |
| 2562 | |
| 2563 | /* |
| 2564 | * This is similar to page_mkwrite, we need to reserve the space before |
| 2565 | * we take the page lock. |
| 2566 | */ |
Nikolay Borisov | 65d87f7 | 2020-06-05 10:51:51 +0300 | [diff] [blame] | 2567 | ret = btrfs_delalloc_reserve_space(inode, &data_reserved, page_start, |
| 2568 | PAGE_SIZE); |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 2569 | again: |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2570 | lock_page(page); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2571 | |
Chris Mason | 25f3c50 | 2020-01-21 11:51:42 -0500 | [diff] [blame] | 2572 | /* |
| 2573 | * Before we queued this fixup, we took a reference on the page. |
| 2574 | * page->mapping may go NULL, but it shouldn't be moved to a different |
| 2575 | * address space. |
| 2576 | */ |
Josef Bacik | f4b1363 | 2020-01-21 14:34:52 -0500 | [diff] [blame] | 2577 | if (!page->mapping || !PageDirty(page) || !PageChecked(page)) { |
| 2578 | /* |
| 2579 | * Unfortunately this is a little tricky, either |
| 2580 | * |
| 2581 | * 1) We got here and our page had already been dealt with and |
| 2582 | * we reserved our space, thus ret == 0, so we need to just |
| 2583 | * drop our space reservation and bail. This can happen the |
| 2584 | * first time we come into the fixup worker, or could happen |
| 2585 | * while waiting for the ordered extent. |
| 2586 | * 2) Our page was already dealt with, but we happened to get an |
| 2587 | * ENOSPC above from the btrfs_delalloc_reserve_space. In |
| 2588 | * this case we obviously don't have anything to release, but |
| 2589 | * because the page was already dealt with we don't want to |
| 2590 | * mark the page with an error, so make sure we're resetting |
| 2591 | * ret to 0. This is why we have this check _before_ the ret |
| 2592 | * check, because we do not want to have a surprise ENOSPC |
| 2593 | * when the page was already properly dealt with. |
| 2594 | */ |
| 2595 | if (!ret) { |
Nikolay Borisov | 65d87f7 | 2020-06-05 10:51:51 +0300 | [diff] [blame] | 2596 | btrfs_delalloc_release_extents(inode, PAGE_SIZE); |
| 2597 | btrfs_delalloc_release_space(inode, data_reserved, |
Josef Bacik | f4b1363 | 2020-01-21 14:34:52 -0500 | [diff] [blame] | 2598 | page_start, PAGE_SIZE, |
| 2599 | true); |
| 2600 | } |
| 2601 | ret = 0; |
Chris Mason | 25f3c50 | 2020-01-21 11:51:42 -0500 | [diff] [blame] | 2602 | goto out_page; |
Josef Bacik | f4b1363 | 2020-01-21 14:34:52 -0500 | [diff] [blame] | 2603 | } |
Chris Mason | 25f3c50 | 2020-01-21 11:51:42 -0500 | [diff] [blame] | 2604 | |
| 2605 | /* |
Josef Bacik | f4b1363 | 2020-01-21 14:34:52 -0500 | [diff] [blame] | 2606 | * We can't mess with the page state unless it is locked, so now that |
| 2607 | * it is locked bail if we failed to make our space reservation. |
Chris Mason | 25f3c50 | 2020-01-21 11:51:42 -0500 | [diff] [blame] | 2608 | */ |
Josef Bacik | f4b1363 | 2020-01-21 14:34:52 -0500 | [diff] [blame] | 2609 | if (ret) |
| 2610 | goto out_page; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2611 | |
Nikolay Borisov | 65d87f7 | 2020-06-05 10:51:51 +0300 | [diff] [blame] | 2612 | lock_extent_bits(&inode->io_tree, page_start, page_end, &cached_state); |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 2613 | |
| 2614 | /* already ordered? We're done */ |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 2615 | if (PagePrivate2(page)) |
Josef Bacik | f4b1363 | 2020-01-21 14:34:52 -0500 | [diff] [blame] | 2616 | goto out_reserved; |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 2617 | |
Nikolay Borisov | 65d87f7 | 2020-06-05 10:51:51 +0300 | [diff] [blame] | 2618 | ordered = btrfs_lookup_ordered_range(inode, page_start, PAGE_SIZE); |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 2619 | if (ordered) { |
Nikolay Borisov | 65d87f7 | 2020-06-05 10:51:51 +0300 | [diff] [blame] | 2620 | unlock_extent_cached(&inode->io_tree, page_start, page_end, |
| 2621 | &cached_state); |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 2622 | unlock_page(page); |
Nikolay Borisov | c0a4360 | 2020-09-18 12:15:53 +0300 | [diff] [blame] | 2623 | btrfs_start_ordered_extent(ordered, 1); |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 2624 | btrfs_put_ordered_extent(ordered); |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 2625 | goto again; |
| 2626 | } |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2627 | |
Nikolay Borisov | 65d87f7 | 2020-06-05 10:51:51 +0300 | [diff] [blame] | 2628 | ret = btrfs_set_extent_delalloc(inode, page_start, page_end, 0, |
Nikolay Borisov | 330a582 | 2019-07-17 16:18:17 +0300 | [diff] [blame] | 2629 | &cached_state); |
Chris Mason | 25f3c50 | 2020-01-21 11:51:42 -0500 | [diff] [blame] | 2630 | if (ret) |
Filipe Manana | 5368700 | 2019-10-09 17:43:59 +0100 | [diff] [blame] | 2631 | goto out_reserved; |
Nikolay Borisov | f3038ee | 2017-12-05 09:29:19 +0200 | [diff] [blame] | 2632 | |
Chris Mason | 25f3c50 | 2020-01-21 11:51:42 -0500 | [diff] [blame] | 2633 | /* |
| 2634 | * Everything went as planned, we're now the owner of a dirty page with |
| 2635 | * delayed allocation bits set and space reserved for our COW |
| 2636 | * destination. |
| 2637 | * |
| 2638 | * The page was dirty when we started, nothing should have cleaned it. |
| 2639 | */ |
| 2640 | BUG_ON(!PageDirty(page)); |
Josef Bacik | f4b1363 | 2020-01-21 14:34:52 -0500 | [diff] [blame] | 2641 | free_delalloc_space = false; |
Filipe Manana | 5368700 | 2019-10-09 17:43:59 +0100 | [diff] [blame] | 2642 | out_reserved: |
Nikolay Borisov | 65d87f7 | 2020-06-05 10:51:51 +0300 | [diff] [blame] | 2643 | btrfs_delalloc_release_extents(inode, PAGE_SIZE); |
Josef Bacik | f4b1363 | 2020-01-21 14:34:52 -0500 | [diff] [blame] | 2644 | if (free_delalloc_space) |
Nikolay Borisov | 65d87f7 | 2020-06-05 10:51:51 +0300 | [diff] [blame] | 2645 | btrfs_delalloc_release_space(inode, data_reserved, page_start, |
| 2646 | PAGE_SIZE, true); |
| 2647 | unlock_extent_cached(&inode->io_tree, page_start, page_end, |
David Sterba | e43bbe5 | 2017-12-12 21:43:52 +0100 | [diff] [blame] | 2648 | &cached_state); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2649 | out_page: |
Chris Mason | 25f3c50 | 2020-01-21 11:51:42 -0500 | [diff] [blame] | 2650 | if (ret) { |
| 2651 | /* |
| 2652 | * We hit ENOSPC or other errors. Update the mapping and page |
| 2653 | * to reflect the errors and clean the page. |
| 2654 | */ |
| 2655 | mapping_set_error(page->mapping, ret); |
| 2656 | end_extent_writepage(page, ret, page_start, page_end); |
| 2657 | clear_page_dirty_for_io(page); |
| 2658 | SetPageError(page); |
| 2659 | } |
| 2660 | ClearPageChecked(page); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2661 | unlock_page(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2662 | put_page(page); |
Miao Xie | b897abe | 2011-01-26 16:19:22 +0800 | [diff] [blame] | 2663 | kfree(fixup); |
Qu Wenruo | 364ecf3 | 2017-02-27 15:10:38 +0800 | [diff] [blame] | 2664 | extent_changeset_free(data_reserved); |
Josef Bacik | f4b1363 | 2020-01-21 14:34:52 -0500 | [diff] [blame] | 2665 | /* |
| 2666 | * As a precaution, do a delayed iput in case it would be the last iput |
| 2667 | * that could need flushing space. Recursing back to fixup worker would |
| 2668 | * deadlock. |
| 2669 | */ |
Nikolay Borisov | 65d87f7 | 2020-06-05 10:51:51 +0300 | [diff] [blame] | 2670 | btrfs_add_delayed_iput(&inode->vfs_inode); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2671 | } |
| 2672 | |
| 2673 | /* |
| 2674 | * There are a few paths in the higher layers of the kernel that directly |
| 2675 | * set the page dirty bit without asking the filesystem if it is a |
| 2676 | * good idea. This causes problems because we want to make sure COW |
| 2677 | * properly happens and the data=ordered rules are followed. |
| 2678 | * |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2679 | * 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] | 2680 | * hasn't been properly setup for IO. We kick off an async process |
| 2681 | * to fix it up. The async helper will wait for ordered extents, set |
| 2682 | * the delalloc bit and make it safe to write the page. |
| 2683 | */ |
Nikolay Borisov | d75855b | 2018-11-01 14:09:47 +0200 | [diff] [blame] | 2684 | int btrfs_writepage_cow_fixup(struct page *page, u64 start, u64 end) |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2685 | { |
| 2686 | struct inode *inode = page->mapping->host; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2687 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2688 | struct btrfs_writepage_fixup *fixup; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2689 | |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 2690 | /* this page is properly in the ordered list */ |
| 2691 | if (TestClearPagePrivate2(page)) |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2692 | return 0; |
| 2693 | |
Chris Mason | 25f3c50 | 2020-01-21 11:51:42 -0500 | [diff] [blame] | 2694 | /* |
| 2695 | * PageChecked is set below when we create a fixup worker for this page, |
| 2696 | * don't try to create another one if we're already PageChecked() |
| 2697 | * |
| 2698 | * The extent_io writepage code will redirty the page if we send back |
| 2699 | * EAGAIN. |
| 2700 | */ |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2701 | if (PageChecked(page)) |
| 2702 | return -EAGAIN; |
| 2703 | |
| 2704 | fixup = kzalloc(sizeof(*fixup), GFP_NOFS); |
| 2705 | if (!fixup) |
| 2706 | return -EAGAIN; |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 2707 | |
Josef Bacik | f4b1363 | 2020-01-21 14:34:52 -0500 | [diff] [blame] | 2708 | /* |
| 2709 | * We are already holding a reference to this inode from |
| 2710 | * write_cache_pages. We need to hold it because the space reservation |
| 2711 | * takes place outside of the page lock, and we can't trust |
| 2712 | * page->mapping outside of the page lock. |
| 2713 | */ |
| 2714 | ihold(inode); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2715 | SetPageChecked(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2716 | get_page(page); |
Omar Sandoval | a0cac0e | 2019-09-16 11:30:57 -0700 | [diff] [blame] | 2717 | btrfs_init_work(&fixup->work, btrfs_writepage_fixup_worker, NULL, NULL); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2718 | fixup->page = page; |
Josef Bacik | f4b1363 | 2020-01-21 14:34:52 -0500 | [diff] [blame] | 2719 | fixup->inode = inode; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2720 | btrfs_queue_work(fs_info->fixup_workers, &fixup->work); |
Chris Mason | 25f3c50 | 2020-01-21 11:51:42 -0500 | [diff] [blame] | 2721 | |
| 2722 | return -EAGAIN; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2723 | } |
| 2724 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2725 | static int insert_reserved_file_extent(struct btrfs_trans_handle *trans, |
Nikolay Borisov | c553f94 | 2020-06-03 08:55:19 +0300 | [diff] [blame] | 2726 | struct btrfs_inode *inode, u64 file_pos, |
Qu Wenruo | 9729f10 | 2020-06-10 09:04:41 +0800 | [diff] [blame] | 2727 | struct btrfs_file_extent_item *stack_fi, |
Filipe Manana | 2766ff6 | 2020-11-04 11:07:34 +0000 | [diff] [blame] | 2728 | const bool update_inode_bytes, |
Qu Wenruo | 9729f10 | 2020-06-10 09:04:41 +0800 | [diff] [blame] | 2729 | u64 qgroup_reserved) |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2730 | { |
Nikolay Borisov | c553f94 | 2020-06-03 08:55:19 +0300 | [diff] [blame] | 2731 | struct btrfs_root *root = inode->root; |
Filipe Manana | 2766ff6 | 2020-11-04 11:07:34 +0000 | [diff] [blame] | 2732 | const u64 sectorsize = root->fs_info->sectorsize; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2733 | struct btrfs_path *path; |
| 2734 | struct extent_buffer *leaf; |
| 2735 | struct btrfs_key ins; |
Qu Wenruo | 203f44c5 | 2020-06-10 09:04:40 +0800 | [diff] [blame] | 2736 | u64 disk_num_bytes = btrfs_stack_file_extent_disk_num_bytes(stack_fi); |
| 2737 | u64 disk_bytenr = btrfs_stack_file_extent_disk_bytenr(stack_fi); |
| 2738 | u64 num_bytes = btrfs_stack_file_extent_num_bytes(stack_fi); |
| 2739 | u64 ram_bytes = btrfs_stack_file_extent_ram_bytes(stack_fi); |
Filipe Manana | 5893dfb | 2020-11-04 11:07:32 +0000 | [diff] [blame] | 2740 | struct btrfs_drop_extents_args drop_args = { 0 }; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2741 | int ret; |
| 2742 | |
| 2743 | path = btrfs_alloc_path(); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 2744 | if (!path) |
| 2745 | return -ENOMEM; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2746 | |
Chris Mason | a1ed835 | 2009-09-11 12:27:37 -0400 | [diff] [blame] | 2747 | /* |
| 2748 | * we may be replacing one extent in the tree with another. |
| 2749 | * The new extent is pinned in the extent map, and we don't want |
| 2750 | * to drop it from the cache until it is completely in the btree. |
| 2751 | * |
| 2752 | * So, tell btrfs_drop_extents to leave this extent in the cache. |
| 2753 | * the caller is expected to unpin it and allow it to be merged |
| 2754 | * with the others. |
| 2755 | */ |
Filipe Manana | 5893dfb | 2020-11-04 11:07:32 +0000 | [diff] [blame] | 2756 | drop_args.path = path; |
| 2757 | drop_args.start = file_pos; |
| 2758 | drop_args.end = file_pos + num_bytes; |
| 2759 | drop_args.replace_extent = true; |
| 2760 | drop_args.extent_item_size = sizeof(*stack_fi); |
| 2761 | ret = btrfs_drop_extents(trans, root, inode, &drop_args); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2762 | if (ret) |
| 2763 | goto out; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2764 | |
Filipe Manana | 5893dfb | 2020-11-04 11:07:32 +0000 | [diff] [blame] | 2765 | if (!drop_args.extent_inserted) { |
Nikolay Borisov | c553f94 | 2020-06-03 08:55:19 +0300 | [diff] [blame] | 2766 | ins.objectid = btrfs_ino(inode); |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 2767 | ins.offset = file_pos; |
| 2768 | ins.type = BTRFS_EXTENT_DATA_KEY; |
| 2769 | |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 2770 | ret = btrfs_insert_empty_item(trans, root, path, &ins, |
Qu Wenruo | 203f44c5 | 2020-06-10 09:04:40 +0800 | [diff] [blame] | 2771 | sizeof(*stack_fi)); |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 2772 | if (ret) |
| 2773 | goto out; |
| 2774 | } |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2775 | leaf = path->nodes[0]; |
Qu Wenruo | 203f44c5 | 2020-06-10 09:04:40 +0800 | [diff] [blame] | 2776 | btrfs_set_stack_file_extent_generation(stack_fi, trans->transid); |
| 2777 | write_extent_buffer(leaf, stack_fi, |
| 2778 | btrfs_item_ptr_offset(leaf, path->slots[0]), |
| 2779 | sizeof(struct btrfs_file_extent_item)); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 2780 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2781 | btrfs_mark_buffer_dirty(leaf); |
Josef Bacik | ce19533 | 2012-09-25 15:26:16 -0400 | [diff] [blame] | 2782 | btrfs_release_path(path); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2783 | |
Filipe Manana | 2766ff6 | 2020-11-04 11:07:34 +0000 | [diff] [blame] | 2784 | /* |
| 2785 | * If we dropped an inline extent here, we know the range where it is |
| 2786 | * was not marked with the EXTENT_DELALLOC_NEW bit, so we update the |
| 2787 | * number of bytes only for that range contaning the inline extent. |
| 2788 | * The remaining of the range will be processed when clearning the |
| 2789 | * EXTENT_DELALLOC_BIT bit through the ordered extent completion. |
| 2790 | */ |
| 2791 | if (file_pos == 0 && !IS_ALIGNED(drop_args.bytes_found, sectorsize)) { |
| 2792 | u64 inline_size = round_down(drop_args.bytes_found, sectorsize); |
| 2793 | |
| 2794 | inline_size = drop_args.bytes_found - inline_size; |
| 2795 | btrfs_update_inode_bytes(inode, sectorsize, inline_size); |
| 2796 | drop_args.bytes_found -= inline_size; |
| 2797 | num_bytes -= sectorsize; |
| 2798 | } |
| 2799 | |
| 2800 | if (update_inode_bytes) |
| 2801 | btrfs_update_inode_bytes(inode, num_bytes, drop_args.bytes_found); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2802 | |
| 2803 | ins.objectid = disk_bytenr; |
| 2804 | ins.offset = disk_num_bytes; |
| 2805 | ins.type = BTRFS_EXTENT_ITEM_KEY; |
Qu Wenruo | a12b877 | 2017-02-27 15:10:37 +0800 | [diff] [blame] | 2806 | |
Nikolay Borisov | c553f94 | 2020-06-03 08:55:19 +0300 | [diff] [blame] | 2807 | ret = btrfs_inode_set_file_extent_range(inode, file_pos, ram_bytes); |
Josef Bacik | 9ddc959 | 2020-01-17 09:02:22 -0500 | [diff] [blame] | 2808 | if (ret) |
| 2809 | goto out; |
| 2810 | |
Nikolay Borisov | c553f94 | 2020-06-03 08:55:19 +0300 | [diff] [blame] | 2811 | ret = btrfs_alloc_reserved_file_extent(trans, root, btrfs_ino(inode), |
Qu Wenruo | 9729f10 | 2020-06-10 09:04:41 +0800 | [diff] [blame] | 2812 | file_pos, qgroup_reserved, &ins); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2813 | out: |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2814 | btrfs_free_path(path); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 2815 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2816 | return ret; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2817 | } |
| 2818 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2819 | static void btrfs_release_delalloc_bytes(struct btrfs_fs_info *fs_info, |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 2820 | u64 start, u64 len) |
| 2821 | { |
David Sterba | 32da5386 | 2019-10-29 19:20:18 +0100 | [diff] [blame] | 2822 | struct btrfs_block_group *cache; |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 2823 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2824 | cache = btrfs_lookup_block_group(fs_info, start); |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 2825 | ASSERT(cache); |
| 2826 | |
| 2827 | spin_lock(&cache->lock); |
| 2828 | cache->delalloc_bytes -= len; |
| 2829 | spin_unlock(&cache->lock); |
| 2830 | |
| 2831 | btrfs_put_block_group(cache); |
| 2832 | } |
| 2833 | |
Qu Wenruo | 203f44c5 | 2020-06-10 09:04:40 +0800 | [diff] [blame] | 2834 | static int insert_ordered_extent_file_extent(struct btrfs_trans_handle *trans, |
Qu Wenruo | 203f44c5 | 2020-06-10 09:04:40 +0800 | [diff] [blame] | 2835 | struct btrfs_ordered_extent *oe) |
| 2836 | { |
| 2837 | struct btrfs_file_extent_item stack_fi; |
| 2838 | u64 logical_len; |
Filipe Manana | 2766ff6 | 2020-11-04 11:07:34 +0000 | [diff] [blame] | 2839 | bool update_inode_bytes; |
Qu Wenruo | 203f44c5 | 2020-06-10 09:04:40 +0800 | [diff] [blame] | 2840 | |
| 2841 | memset(&stack_fi, 0, sizeof(stack_fi)); |
| 2842 | btrfs_set_stack_file_extent_type(&stack_fi, BTRFS_FILE_EXTENT_REG); |
| 2843 | btrfs_set_stack_file_extent_disk_bytenr(&stack_fi, oe->disk_bytenr); |
| 2844 | btrfs_set_stack_file_extent_disk_num_bytes(&stack_fi, |
| 2845 | oe->disk_num_bytes); |
| 2846 | if (test_bit(BTRFS_ORDERED_TRUNCATED, &oe->flags)) |
| 2847 | logical_len = oe->truncated_len; |
| 2848 | else |
| 2849 | logical_len = oe->num_bytes; |
| 2850 | btrfs_set_stack_file_extent_num_bytes(&stack_fi, logical_len); |
| 2851 | btrfs_set_stack_file_extent_ram_bytes(&stack_fi, logical_len); |
| 2852 | btrfs_set_stack_file_extent_compression(&stack_fi, oe->compress_type); |
| 2853 | /* Encryption and other encoding is reserved and all 0 */ |
| 2854 | |
Filipe Manana | 2766ff6 | 2020-11-04 11:07:34 +0000 | [diff] [blame] | 2855 | /* |
| 2856 | * For delalloc, when completing an ordered extent we update the inode's |
| 2857 | * bytes when clearing the range in the inode's io tree, so pass false |
| 2858 | * as the argument 'update_inode_bytes' to insert_reserved_file_extent(), |
| 2859 | * except if the ordered extent was truncated. |
| 2860 | */ |
| 2861 | update_inode_bytes = test_bit(BTRFS_ORDERED_DIRECT, &oe->flags) || |
| 2862 | test_bit(BTRFS_ORDERED_TRUNCATED, &oe->flags); |
| 2863 | |
Nikolay Borisov | 3c38c87 | 2020-09-18 12:15:51 +0300 | [diff] [blame] | 2864 | return insert_reserved_file_extent(trans, BTRFS_I(oe->inode), |
| 2865 | oe->file_offset, &stack_fi, |
Filipe Manana | 2766ff6 | 2020-11-04 11:07:34 +0000 | [diff] [blame] | 2866 | update_inode_bytes, oe->qgroup_rsv); |
Qu Wenruo | 203f44c5 | 2020-06-10 09:04:40 +0800 | [diff] [blame] | 2867 | } |
| 2868 | |
| 2869 | /* |
| 2870 | * As ordered data IO finishes, this gets called so we can finish |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2871 | * an ordered extent if the range of bytes in the file it covers are |
| 2872 | * fully written. |
| 2873 | */ |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2874 | static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2875 | { |
Nikolay Borisov | 72e7e6e | 2020-11-02 16:48:56 +0200 | [diff] [blame] | 2876 | struct btrfs_inode *inode = BTRFS_I(ordered_extent->inode); |
| 2877 | struct btrfs_root *root = inode->root; |
| 2878 | struct btrfs_fs_info *fs_info = root->fs_info; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 2879 | struct btrfs_trans_handle *trans = NULL; |
Nikolay Borisov | 72e7e6e | 2020-11-02 16:48:56 +0200 | [diff] [blame] | 2880 | struct extent_io_tree *io_tree = &inode->io_tree; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 2881 | struct extent_state *cached_state = NULL; |
Omar Sandoval | bffe633 | 2019-12-02 17:34:19 -0800 | [diff] [blame] | 2882 | u64 start, end; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 2883 | int compress_type = 0; |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 2884 | int ret = 0; |
Omar Sandoval | bffe633 | 2019-12-02 17:34:19 -0800 | [diff] [blame] | 2885 | u64 logical_len = ordered_extent->num_bytes; |
Nikolay Borisov | 8d51012 | 2019-10-08 20:43:06 +0300 | [diff] [blame] | 2886 | bool freespace_inode; |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 2887 | bool truncated = false; |
Josef Bacik | 49940bd | 2018-10-11 15:54:21 -0400 | [diff] [blame] | 2888 | bool clear_reserved_extent = true; |
Filipe Manana | 2766ff6 | 2020-11-04 11:07:34 +0000 | [diff] [blame] | 2889 | unsigned int clear_bits = EXTENT_DEFRAG; |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 2890 | |
Omar Sandoval | bffe633 | 2019-12-02 17:34:19 -0800 | [diff] [blame] | 2891 | start = ordered_extent->file_offset; |
| 2892 | end = start + ordered_extent->num_bytes - 1; |
| 2893 | |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 2894 | if (!test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) && |
| 2895 | !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags) && |
| 2896 | !test_bit(BTRFS_ORDERED_DIRECT, &ordered_extent->flags)) |
Filipe Manana | 2766ff6 | 2020-11-04 11:07:34 +0000 | [diff] [blame] | 2897 | clear_bits |= EXTENT_DELALLOC_NEW; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2898 | |
Nikolay Borisov | 72e7e6e | 2020-11-02 16:48:56 +0200 | [diff] [blame] | 2899 | freespace_inode = btrfs_is_free_space_inode(inode); |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 2900 | |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2901 | if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) { |
| 2902 | ret = -EIO; |
| 2903 | goto out; |
| 2904 | } |
| 2905 | |
Naohiro Aota | d8e3fb1 | 2021-02-04 19:22:05 +0900 | [diff] [blame] | 2906 | if (ordered_extent->disk) |
| 2907 | btrfs_rewrite_logical_zoned(ordered_extent); |
| 2908 | |
Nikolay Borisov | 72e7e6e | 2020-11-02 16:48:56 +0200 | [diff] [blame] | 2909 | btrfs_free_io_failure_record(inode, start, end); |
Miao Xie | f612496 | 2014-09-12 18:44:04 +0800 | [diff] [blame] | 2910 | |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 2911 | if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) { |
| 2912 | truncated = true; |
| 2913 | logical_len = ordered_extent->truncated_len; |
| 2914 | /* Truncated the entire extent, don't bother adding */ |
| 2915 | if (!logical_len) |
| 2916 | goto out; |
| 2917 | } |
| 2918 | |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 2919 | if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) { |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2920 | BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */ |
Qu Wenruo | 94ed938 | 2015-09-08 17:25:56 +0800 | [diff] [blame] | 2921 | |
Nikolay Borisov | 72e7e6e | 2020-11-02 16:48:56 +0200 | [diff] [blame] | 2922 | btrfs_inode_safe_disk_i_size_write(inode, 0); |
Nikolay Borisov | 8d51012 | 2019-10-08 20:43:06 +0300 | [diff] [blame] | 2923 | if (freespace_inode) |
| 2924 | trans = btrfs_join_transaction_spacecache(root); |
Josef Bacik | 6c760c0 | 2012-11-09 10:53:21 -0500 | [diff] [blame] | 2925 | else |
| 2926 | trans = btrfs_join_transaction(root); |
| 2927 | if (IS_ERR(trans)) { |
| 2928 | ret = PTR_ERR(trans); |
| 2929 | trans = NULL; |
| 2930 | goto out; |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 2931 | } |
Nikolay Borisov | 72e7e6e | 2020-11-02 16:48:56 +0200 | [diff] [blame] | 2932 | trans->block_rsv = &inode->block_rsv; |
Nikolay Borisov | 729f796 | 2020-11-02 16:49:06 +0200 | [diff] [blame] | 2933 | ret = btrfs_update_inode_fallback(trans, root, inode); |
Josef Bacik | 6c760c0 | 2012-11-09 10:53:21 -0500 | [diff] [blame] | 2934 | if (ret) /* -ENOMEM or corruption */ |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 2935 | btrfs_abort_transaction(trans, ret); |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 2936 | goto out; |
| 2937 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2938 | |
Filipe Manana | 2766ff6 | 2020-11-04 11:07:34 +0000 | [diff] [blame] | 2939 | clear_bits |= EXTENT_LOCKED; |
Omar Sandoval | bffe633 | 2019-12-02 17:34:19 -0800 | [diff] [blame] | 2940 | lock_extent_bits(io_tree, start, end, &cached_state); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2941 | |
Nikolay Borisov | 8d51012 | 2019-10-08 20:43:06 +0300 | [diff] [blame] | 2942 | if (freespace_inode) |
| 2943 | trans = btrfs_join_transaction_spacecache(root); |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 2944 | else |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 2945 | trans = btrfs_join_transaction(root); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2946 | if (IS_ERR(trans)) { |
| 2947 | ret = PTR_ERR(trans); |
| 2948 | trans = NULL; |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 2949 | goto out; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2950 | } |
Chris Mason | a79b7d4 | 2014-05-22 16:18:52 -0700 | [diff] [blame] | 2951 | |
Nikolay Borisov | 72e7e6e | 2020-11-02 16:48:56 +0200 | [diff] [blame] | 2952 | trans->block_rsv = &inode->block_rsv; |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 2953 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2954 | if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags)) |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 2955 | compress_type = ordered_extent->compress_type; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2956 | if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) { |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 2957 | BUG_ON(compress_type); |
Nikolay Borisov | 72e7e6e | 2020-11-02 16:48:56 +0200 | [diff] [blame] | 2958 | ret = btrfs_mark_extent_written(trans, inode, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2959 | ordered_extent->file_offset, |
| 2960 | ordered_extent->file_offset + |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 2961 | logical_len); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2962 | } else { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2963 | BUG_ON(root == fs_info->tree_root); |
Nikolay Borisov | 3c38c87 | 2020-09-18 12:15:51 +0300 | [diff] [blame] | 2964 | ret = insert_ordered_extent_file_extent(trans, ordered_extent); |
Josef Bacik | 49940bd | 2018-10-11 15:54:21 -0400 | [diff] [blame] | 2965 | if (!ret) { |
| 2966 | clear_reserved_extent = false; |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2967 | btrfs_release_delalloc_bytes(fs_info, |
Omar Sandoval | bffe633 | 2019-12-02 17:34:19 -0800 | [diff] [blame] | 2968 | ordered_extent->disk_bytenr, |
| 2969 | ordered_extent->disk_num_bytes); |
Josef Bacik | 49940bd | 2018-10-11 15:54:21 -0400 | [diff] [blame] | 2970 | } |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2971 | } |
Nikolay Borisov | 72e7e6e | 2020-11-02 16:48:56 +0200 | [diff] [blame] | 2972 | unpin_extent_cache(&inode->extent_tree, ordered_extent->file_offset, |
Omar Sandoval | bffe633 | 2019-12-02 17:34:19 -0800 | [diff] [blame] | 2973 | ordered_extent->num_bytes, trans->transid); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2974 | if (ret < 0) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 2975 | btrfs_abort_transaction(trans, ret); |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 2976 | goto out; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2977 | } |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 2978 | |
Nikolay Borisov | 510f85e | 2020-09-18 12:15:52 +0300 | [diff] [blame] | 2979 | ret = add_pending_csums(trans, &ordered_extent->list); |
Nikolay Borisov | ac01f26 | 2018-01-08 10:59:43 +0200 | [diff] [blame] | 2980 | if (ret) { |
| 2981 | btrfs_abort_transaction(trans, ret); |
| 2982 | goto out; |
| 2983 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2984 | |
Filipe Manana | 2766ff6 | 2020-11-04 11:07:34 +0000 | [diff] [blame] | 2985 | /* |
| 2986 | * If this is a new delalloc range, clear its new delalloc flag to |
| 2987 | * update the inode's number of bytes. This needs to be done first |
| 2988 | * before updating the inode item. |
| 2989 | */ |
| 2990 | if ((clear_bits & EXTENT_DELALLOC_NEW) && |
| 2991 | !test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) |
Nikolay Borisov | 72e7e6e | 2020-11-02 16:48:56 +0200 | [diff] [blame] | 2992 | clear_extent_bit(&inode->io_tree, start, end, |
Filipe Manana | 2766ff6 | 2020-11-04 11:07:34 +0000 | [diff] [blame] | 2993 | EXTENT_DELALLOC_NEW | EXTENT_ADD_INODE_BYTES, |
| 2994 | 0, 0, &cached_state); |
| 2995 | |
Nikolay Borisov | 72e7e6e | 2020-11-02 16:48:56 +0200 | [diff] [blame] | 2996 | btrfs_inode_safe_disk_i_size_write(inode, 0); |
Nikolay Borisov | 729f796 | 2020-11-02 16:49:06 +0200 | [diff] [blame] | 2997 | ret = btrfs_update_inode_fallback(trans, root, inode); |
Josef Bacik | 6c760c0 | 2012-11-09 10:53:21 -0500 | [diff] [blame] | 2998 | if (ret) { /* -ENOMEM or corruption */ |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 2999 | btrfs_abort_transaction(trans, ret); |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 3000 | goto out; |
Josef Bacik | 1ef30be | 2011-04-05 19:25:36 -0400 | [diff] [blame] | 3001 | } |
| 3002 | ret = 0; |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 3003 | out: |
Nikolay Borisov | 72e7e6e | 2020-11-02 16:48:56 +0200 | [diff] [blame] | 3004 | clear_extent_bit(&inode->io_tree, start, end, clear_bits, |
Omar Sandoval | bffe633 | 2019-12-02 17:34:19 -0800 | [diff] [blame] | 3005 | (clear_bits & EXTENT_LOCKED) ? 1 : 0, 0, |
Omar Sandoval | 313facc | 2019-12-02 17:34:18 -0800 | [diff] [blame] | 3006 | &cached_state); |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 3007 | |
Miao Xie | a698d075 | 2012-09-20 01:51:59 -0600 | [diff] [blame] | 3008 | if (trans) |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 3009 | btrfs_end_transaction(trans); |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 3010 | |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 3011 | if (ret || truncated) { |
Omar Sandoval | bffe633 | 2019-12-02 17:34:19 -0800 | [diff] [blame] | 3012 | u64 unwritten_start = start; |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 3013 | |
| 3014 | if (truncated) |
Omar Sandoval | bffe633 | 2019-12-02 17:34:19 -0800 | [diff] [blame] | 3015 | unwritten_start += logical_len; |
| 3016 | clear_extent_uptodate(io_tree, unwritten_start, end, NULL); |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 3017 | |
| 3018 | /* 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] | 3019 | btrfs_drop_extent_cache(inode, unwritten_start, end, 0); |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 3020 | |
Josef Bacik | 0bec9ef | 2013-01-31 14:58:00 -0500 | [diff] [blame] | 3021 | /* |
| 3022 | * If the ordered extent had an IOERR or something else went |
| 3023 | * wrong we need to return the space for this ordered extent |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 3024 | * back to the allocator. We only free the extent in the |
| 3025 | * truncated case if we didn't write out the extent at all. |
Josef Bacik | 49940bd | 2018-10-11 15:54:21 -0400 | [diff] [blame] | 3026 | * |
| 3027 | * If we made it past insert_reserved_file_extent before we |
| 3028 | * errored out then we don't need to do this as the accounting |
| 3029 | * has already been done. |
Josef Bacik | 0bec9ef | 2013-01-31 14:58:00 -0500 | [diff] [blame] | 3030 | */ |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 3031 | if ((ret || !logical_len) && |
Josef Bacik | 49940bd | 2018-10-11 15:54:21 -0400 | [diff] [blame] | 3032 | clear_reserved_extent && |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 3033 | !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) && |
Nikolay Borisov | 4eaaec2 | 2019-11-21 14:03:29 +0200 | [diff] [blame] | 3034 | !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) { |
| 3035 | /* |
| 3036 | * Discard the range before returning it back to the |
| 3037 | * free space pool |
| 3038 | */ |
Dennis Zhou | 46b27f5 | 2019-12-13 16:22:11 -0800 | [diff] [blame] | 3039 | if (ret && btrfs_test_opt(fs_info, DISCARD_SYNC)) |
Nikolay Borisov | 4eaaec2 | 2019-11-21 14:03:29 +0200 | [diff] [blame] | 3040 | btrfs_discard_extent(fs_info, |
Omar Sandoval | bffe633 | 2019-12-02 17:34:19 -0800 | [diff] [blame] | 3041 | ordered_extent->disk_bytenr, |
| 3042 | ordered_extent->disk_num_bytes, |
| 3043 | NULL); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3044 | btrfs_free_reserved_extent(fs_info, |
Omar Sandoval | bffe633 | 2019-12-02 17:34:19 -0800 | [diff] [blame] | 3045 | ordered_extent->disk_bytenr, |
| 3046 | ordered_extent->disk_num_bytes, 1); |
Nikolay Borisov | 4eaaec2 | 2019-11-21 14:03:29 +0200 | [diff] [blame] | 3047 | } |
Josef Bacik | 0bec9ef | 2013-01-31 14:58:00 -0500 | [diff] [blame] | 3048 | } |
| 3049 | |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 3050 | /* |
Liu Bo | 8bad3c0 | 2012-06-18 12:14:23 +0800 | [diff] [blame] | 3051 | * This needs to be done to make sure anybody waiting knows we are done |
| 3052 | * updating everything for this ordered extent. |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 3053 | */ |
Nikolay Borisov | 72e7e6e | 2020-11-02 16:48:56 +0200 | [diff] [blame] | 3054 | btrfs_remove_ordered_extent(inode, ordered_extent); |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 3055 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3056 | /* once for us */ |
| 3057 | btrfs_put_ordered_extent(ordered_extent); |
| 3058 | /* once for the tree */ |
| 3059 | btrfs_put_ordered_extent(ordered_extent); |
| 3060 | |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 3061 | return ret; |
| 3062 | } |
| 3063 | |
| 3064 | static void finish_ordered_fn(struct btrfs_work *work) |
| 3065 | { |
| 3066 | struct btrfs_ordered_extent *ordered_extent; |
| 3067 | ordered_extent = container_of(work, struct btrfs_ordered_extent, work); |
| 3068 | btrfs_finish_ordered_io(ordered_extent); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3069 | } |
| 3070 | |
Nikolay Borisov | c629732 | 2018-11-08 10:18:08 +0200 | [diff] [blame] | 3071 | void btrfs_writepage_endio_finish_ordered(struct page *page, u64 start, |
| 3072 | u64 end, int uptodate) |
Chris Mason | 211f90e | 2008-07-18 11:56:15 -0400 | [diff] [blame] | 3073 | { |
Nikolay Borisov | 3347c48 | 2020-08-31 14:42:44 +0300 | [diff] [blame] | 3074 | struct btrfs_inode *inode = BTRFS_I(page->mapping->host); |
| 3075 | struct btrfs_fs_info *fs_info = inode->root->fs_info; |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 3076 | struct btrfs_ordered_extent *ordered_extent = NULL; |
Liu Bo | 9e0af23 | 2014-08-15 23:36:53 +0800 | [diff] [blame] | 3077 | struct btrfs_workqueue *wq; |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 3078 | |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 3079 | trace_btrfs_writepage_end_io_hook(page, start, end, uptodate); |
| 3080 | |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 3081 | ClearPagePrivate2(page); |
Nikolay Borisov | 3347c48 | 2020-08-31 14:42:44 +0300 | [diff] [blame] | 3082 | if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start, |
| 3083 | end - start + 1, uptodate)) |
David Sterba | c3988d6 | 2017-02-17 15:18:32 +0100 | [diff] [blame] | 3084 | return; |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 3085 | |
Nikolay Borisov | 3347c48 | 2020-08-31 14:42:44 +0300 | [diff] [blame] | 3086 | if (btrfs_is_free_space_inode(inode)) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3087 | wq = fs_info->endio_freespace_worker; |
Omar Sandoval | a0cac0e | 2019-09-16 11:30:57 -0700 | [diff] [blame] | 3088 | else |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3089 | wq = fs_info->endio_write_workers; |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 3090 | |
Omar Sandoval | a0cac0e | 2019-09-16 11:30:57 -0700 | [diff] [blame] | 3091 | btrfs_init_work(&ordered_extent->work, finish_ordered_fn, NULL, NULL); |
Liu Bo | 9e0af23 | 2014-08-15 23:36:53 +0800 | [diff] [blame] | 3092 | btrfs_queue_work(wq, &ordered_extent->work); |
Chris Mason | 211f90e | 2008-07-18 11:56:15 -0400 | [diff] [blame] | 3093 | } |
| 3094 | |
Qu Wenruo | 265d4ac | 2020-10-21 14:24:54 +0800 | [diff] [blame] | 3095 | /* |
| 3096 | * check_data_csum - verify checksum of one sector of uncompressed data |
Qu Wenruo | 7ffd27e | 2020-12-02 14:47:58 +0800 | [diff] [blame] | 3097 | * @inode: inode |
Qu Wenruo | 265d4ac | 2020-10-21 14:24:54 +0800 | [diff] [blame] | 3098 | * @io_bio: btrfs_io_bio which contains the csum |
Qu Wenruo | 7ffd27e | 2020-12-02 14:47:58 +0800 | [diff] [blame] | 3099 | * @bio_offset: offset to the beginning of the bio (in bytes) |
Qu Wenruo | 265d4ac | 2020-10-21 14:24:54 +0800 | [diff] [blame] | 3100 | * @page: page where is the data to be verified |
| 3101 | * @pgoff: offset inside the page |
Omar Sandoval | c1d6abd | 2021-03-16 12:43:00 -0700 | [diff] [blame] | 3102 | * @start: logical offset in the file |
Qu Wenruo | 265d4ac | 2020-10-21 14:24:54 +0800 | [diff] [blame] | 3103 | * |
| 3104 | * The length of such check is always one sector size. |
| 3105 | */ |
Omar Sandoval | 47df776 | 2020-04-16 14:46:17 -0700 | [diff] [blame] | 3106 | 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] | 3107 | u32 bio_offset, struct page *page, u32 pgoff, |
| 3108 | u64 start) |
Miao Xie | dc380ae | 2014-09-12 18:43:55 +0800 | [diff] [blame] | 3109 | { |
Johannes Thumshirn | d517857 | 2019-06-03 16:58:57 +0200 | [diff] [blame] | 3110 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
| 3111 | SHASH_DESC_ON_STACK(shash, fs_info->csum_shash); |
Miao Xie | dc380ae | 2014-09-12 18:43:55 +0800 | [diff] [blame] | 3112 | char *kaddr; |
Qu Wenruo | 265d4ac | 2020-10-21 14:24:54 +0800 | [diff] [blame] | 3113 | u32 len = fs_info->sectorsize; |
David Sterba | 223486c | 2020-07-02 11:27:30 +0200 | [diff] [blame] | 3114 | const u32 csum_size = fs_info->csum_size; |
Qu Wenruo | 7ffd27e | 2020-12-02 14:47:58 +0800 | [diff] [blame] | 3115 | unsigned int offset_sectors; |
Johannes Thumshirn | d517857 | 2019-06-03 16:58:57 +0200 | [diff] [blame] | 3116 | u8 *csum_expected; |
| 3117 | u8 csum[BTRFS_CSUM_SIZE]; |
Miao Xie | dc380ae | 2014-09-12 18:43:55 +0800 | [diff] [blame] | 3118 | |
Qu Wenruo | 265d4ac | 2020-10-21 14:24:54 +0800 | [diff] [blame] | 3119 | ASSERT(pgoff + len <= PAGE_SIZE); |
| 3120 | |
Qu Wenruo | 7ffd27e | 2020-12-02 14:47:58 +0800 | [diff] [blame] | 3121 | offset_sectors = bio_offset >> fs_info->sectorsize_bits; |
| 3122 | csum_expected = ((u8 *)io_bio->csum) + offset_sectors * csum_size; |
Miao Xie | dc380ae | 2014-09-12 18:43:55 +0800 | [diff] [blame] | 3123 | |
| 3124 | kaddr = kmap_atomic(page); |
Johannes Thumshirn | d517857 | 2019-06-03 16:58:57 +0200 | [diff] [blame] | 3125 | shash->tfm = fs_info->csum_shash; |
| 3126 | |
Eric Biggers | fd08001 | 2020-04-30 23:51:59 -0700 | [diff] [blame] | 3127 | crypto_shash_digest(shash, kaddr + pgoff, len, csum); |
Johannes Thumshirn | d517857 | 2019-06-03 16:58:57 +0200 | [diff] [blame] | 3128 | |
| 3129 | if (memcmp(csum, csum_expected, csum_size)) |
Miao Xie | dc380ae | 2014-09-12 18:43:55 +0800 | [diff] [blame] | 3130 | goto zeroit; |
| 3131 | |
| 3132 | kunmap_atomic(kaddr); |
| 3133 | return 0; |
| 3134 | zeroit: |
Omar Sandoval | c1d6abd | 2021-03-16 12:43:00 -0700 | [diff] [blame] | 3135 | btrfs_print_data_csum_error(BTRFS_I(inode), start, csum, csum_expected, |
| 3136 | io_bio->mirror_num); |
Nikolay Borisov | 814723e | 2020-07-02 15:23:32 +0300 | [diff] [blame] | 3137 | if (io_bio->device) |
| 3138 | btrfs_dev_stat_inc_and_print(io_bio->device, |
| 3139 | BTRFS_DEV_STAT_CORRUPTION_ERRS); |
Miao Xie | dc380ae | 2014-09-12 18:43:55 +0800 | [diff] [blame] | 3140 | memset(kaddr + pgoff, 1, len); |
| 3141 | flush_dcache_page(page); |
| 3142 | kunmap_atomic(kaddr); |
Miao Xie | dc380ae | 2014-09-12 18:43:55 +0800 | [diff] [blame] | 3143 | return -EIO; |
| 3144 | } |
| 3145 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3146 | /* |
Qu Wenruo | 7ffd27e | 2020-12-02 14:47:58 +0800 | [diff] [blame] | 3147 | * 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] | 3148 | * if there's a match, we allow the bio to finish. If not, the code in |
| 3149 | * extent_io.c will try to find good copies for us. |
Qu Wenruo | 7ffd27e | 2020-12-02 14:47:58 +0800 | [diff] [blame] | 3150 | * |
| 3151 | * @bio_offset: offset to the beginning of the bio (in bytes) |
| 3152 | * @start: file offset of the range start |
| 3153 | * @end: file offset of the range end (inclusive) |
| 3154 | * @mirror: mirror number |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3155 | */ |
Qu Wenruo | 7ffd27e | 2020-12-02 14:47:58 +0800 | [diff] [blame] | 3156 | int btrfs_verify_data_csum(struct btrfs_io_bio *io_bio, u32 bio_offset, |
Nikolay Borisov | 9a446d6 | 2020-09-18 16:34:33 +0300 | [diff] [blame] | 3157 | struct page *page, u64 start, u64 end, int mirror) |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 3158 | { |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 3159 | struct inode *inode = page->mapping->host; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3160 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | ff79f81 | 2007-10-15 16:22:25 -0400 | [diff] [blame] | 3161 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Qu Wenruo | f44cf41 | 2020-12-02 14:47:59 +0800 | [diff] [blame] | 3162 | const u32 sectorsize = root->fs_info->sectorsize; |
| 3163 | u32 pg_off; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3164 | |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 3165 | if (PageChecked(page)) { |
| 3166 | ClearPageChecked(page); |
Miao Xie | dc380ae | 2014-09-12 18:43:55 +0800 | [diff] [blame] | 3167 | return 0; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 3168 | } |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 3169 | |
| 3170 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM) |
Miao Xie | dc380ae | 2014-09-12 18:43:55 +0800 | [diff] [blame] | 3171 | return 0; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 3172 | |
Josef Bacik | 42437a6 | 2020-10-16 11:29:18 -0400 | [diff] [blame] | 3173 | if (!root->fs_info->csum_root) |
| 3174 | return 0; |
| 3175 | |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 3176 | if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID && |
Chris Mason | 9655d29 | 2009-09-02 15:22:30 -0400 | [diff] [blame] | 3177 | test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) { |
David Sterba | 9116621 | 2016-04-26 23:54:39 +0200 | [diff] [blame] | 3178 | clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM); |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 3179 | return 0; |
| 3180 | } |
| 3181 | |
Qu Wenruo | f44cf41 | 2020-12-02 14:47:59 +0800 | [diff] [blame] | 3182 | ASSERT(page_offset(page) <= start && |
| 3183 | end <= page_offset(page) + PAGE_SIZE - 1); |
| 3184 | for (pg_off = offset_in_page(start); |
| 3185 | pg_off < offset_in_page(end); |
| 3186 | pg_off += sectorsize, bio_offset += sectorsize) { |
| 3187 | int ret; |
| 3188 | |
Omar Sandoval | c1d6abd | 2021-03-16 12:43:00 -0700 | [diff] [blame] | 3189 | ret = check_data_csum(inode, io_bio, bio_offset, page, pg_off, |
| 3190 | page_offset(page) + pg_off); |
Qu Wenruo | f44cf41 | 2020-12-02 14:47:59 +0800 | [diff] [blame] | 3191 | if (ret < 0) |
| 3192 | return -EIO; |
| 3193 | } |
| 3194 | return 0; |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 3195 | } |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3196 | |
Nikolay Borisov | c1c3fac | 2018-01-16 09:31:58 +0200 | [diff] [blame] | 3197 | /* |
| 3198 | * btrfs_add_delayed_iput - perform a delayed iput on @inode |
| 3199 | * |
| 3200 | * @inode: The inode we want to perform iput on |
| 3201 | * |
| 3202 | * This function uses the generic vfs_inode::i_count to track whether we should |
| 3203 | * just decrement it (in case it's > 1) or if this is the last iput then link |
| 3204 | * the inode to the delayed iput machinery. Delayed iputs are processed at |
| 3205 | * transaction commit time/superblock commit/cleaner kthread. |
| 3206 | */ |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 3207 | void btrfs_add_delayed_iput(struct inode *inode) |
| 3208 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3209 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
David Sterba | 8089fe6 | 2015-11-19 14:15:51 +0100 | [diff] [blame] | 3210 | struct btrfs_inode *binode = BTRFS_I(inode); |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 3211 | |
| 3212 | if (atomic_add_unless(&inode->i_count, -1, 1)) |
| 3213 | return; |
| 3214 | |
Josef Bacik | 034f784 | 2018-12-03 11:06:52 -0500 | [diff] [blame] | 3215 | atomic_inc(&fs_info->nr_delayed_iputs); |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 3216 | spin_lock(&fs_info->delayed_iput_lock); |
Nikolay Borisov | c1c3fac | 2018-01-16 09:31:58 +0200 | [diff] [blame] | 3217 | ASSERT(list_empty(&binode->delayed_iput)); |
| 3218 | list_add_tail(&binode->delayed_iput, &fs_info->delayed_iputs); |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 3219 | spin_unlock(&fs_info->delayed_iput_lock); |
Josef Bacik | fd340d0 | 2019-01-11 10:21:02 -0500 | [diff] [blame] | 3220 | if (!test_bit(BTRFS_FS_CLEANER_RUNNING, &fs_info->flags)) |
| 3221 | wake_up_process(fs_info->cleaner_kthread); |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 3222 | } |
| 3223 | |
Josef Bacik | 63611e7 | 2019-06-18 10:59:18 -0400 | [diff] [blame] | 3224 | static void run_delayed_iput_locked(struct btrfs_fs_info *fs_info, |
| 3225 | struct btrfs_inode *inode) |
| 3226 | { |
| 3227 | list_del_init(&inode->delayed_iput); |
| 3228 | spin_unlock(&fs_info->delayed_iput_lock); |
| 3229 | iput(&inode->vfs_inode); |
| 3230 | if (atomic_dec_and_test(&fs_info->nr_delayed_iputs)) |
| 3231 | wake_up(&fs_info->delayed_iputs_wait); |
| 3232 | spin_lock(&fs_info->delayed_iput_lock); |
| 3233 | } |
| 3234 | |
| 3235 | static void btrfs_run_delayed_iput(struct btrfs_fs_info *fs_info, |
| 3236 | struct btrfs_inode *inode) |
| 3237 | { |
| 3238 | if (!list_empty(&inode->delayed_iput)) { |
| 3239 | spin_lock(&fs_info->delayed_iput_lock); |
| 3240 | if (!list_empty(&inode->delayed_iput)) |
| 3241 | run_delayed_iput_locked(fs_info, inode); |
| 3242 | spin_unlock(&fs_info->delayed_iput_lock); |
| 3243 | } |
| 3244 | } |
| 3245 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3246 | void btrfs_run_delayed_iputs(struct btrfs_fs_info *fs_info) |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 3247 | { |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 3248 | |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 3249 | spin_lock(&fs_info->delayed_iput_lock); |
David Sterba | 8089fe6 | 2015-11-19 14:15:51 +0100 | [diff] [blame] | 3250 | while (!list_empty(&fs_info->delayed_iputs)) { |
| 3251 | struct btrfs_inode *inode; |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 3252 | |
David Sterba | 8089fe6 | 2015-11-19 14:15:51 +0100 | [diff] [blame] | 3253 | inode = list_first_entry(&fs_info->delayed_iputs, |
| 3254 | struct btrfs_inode, delayed_iput); |
Josef Bacik | 63611e7 | 2019-06-18 10:59:18 -0400 | [diff] [blame] | 3255 | run_delayed_iput_locked(fs_info, inode); |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 3256 | } |
David Sterba | 8089fe6 | 2015-11-19 14:15:51 +0100 | [diff] [blame] | 3257 | spin_unlock(&fs_info->delayed_iput_lock); |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 3258 | } |
| 3259 | |
Josef Bacik | 034f784 | 2018-12-03 11:06:52 -0500 | [diff] [blame] | 3260 | /** |
Nikolay Borisov | 2639631 | 2021-01-22 11:57:59 +0200 | [diff] [blame] | 3261 | * Wait for flushing all delayed iputs |
| 3262 | * |
| 3263 | * @fs_info: the filesystem |
Josef Bacik | 034f784 | 2018-12-03 11:06:52 -0500 | [diff] [blame] | 3264 | * |
| 3265 | * This will wait on any delayed iputs that are currently running with KILLABLE |
| 3266 | * set. Once they are all done running we will return, unless we are killed in |
| 3267 | * which case we return EINTR. This helps in user operations like fallocate etc |
| 3268 | * that might get blocked on the iputs. |
Nikolay Borisov | 2639631 | 2021-01-22 11:57:59 +0200 | [diff] [blame] | 3269 | * |
| 3270 | * Return EINTR if we were killed, 0 if nothing's pending |
Josef Bacik | 034f784 | 2018-12-03 11:06:52 -0500 | [diff] [blame] | 3271 | */ |
| 3272 | int btrfs_wait_on_delayed_iputs(struct btrfs_fs_info *fs_info) |
| 3273 | { |
| 3274 | int ret = wait_event_killable(fs_info->delayed_iputs_wait, |
| 3275 | atomic_read(&fs_info->nr_delayed_iputs) == 0); |
| 3276 | if (ret) |
| 3277 | return -EINTR; |
| 3278 | return 0; |
| 3279 | } |
| 3280 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3281 | /* |
Omar Sandoval | f7e9e8f | 2018-05-11 13:13:32 -0700 | [diff] [blame] | 3282 | * This creates an orphan entry for the given inode in case something goes wrong |
| 3283 | * in the middle of an unlink. |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3284 | */ |
Nikolay Borisov | 73f2e54 | 2017-02-20 13:50:59 +0200 | [diff] [blame] | 3285 | int btrfs_orphan_add(struct btrfs_trans_handle *trans, |
Omar Sandoval | 2791906 | 2018-05-11 13:13:37 -0700 | [diff] [blame] | 3286 | struct btrfs_inode *inode) |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3287 | { |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3288 | int ret; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3289 | |
Omar Sandoval | 2791906 | 2018-05-11 13:13:37 -0700 | [diff] [blame] | 3290 | ret = btrfs_insert_orphan_item(trans, inode->root, btrfs_ino(inode)); |
| 3291 | if (ret && ret != -EEXIST) { |
| 3292 | btrfs_abort_transaction(trans, ret); |
| 3293 | return ret; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3294 | } |
| 3295 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3296 | return 0; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3297 | } |
| 3298 | |
| 3299 | /* |
Omar Sandoval | f7e9e8f | 2018-05-11 13:13:32 -0700 | [diff] [blame] | 3300 | * We have done the delete so we can go ahead and remove the orphan item for |
| 3301 | * this particular inode. |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3302 | */ |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 3303 | static int btrfs_orphan_del(struct btrfs_trans_handle *trans, |
Nikolay Borisov | 3d6ae7b | 2017-02-20 13:50:58 +0200 | [diff] [blame] | 3304 | struct btrfs_inode *inode) |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3305 | { |
Omar Sandoval | 2791906 | 2018-05-11 13:13:37 -0700 | [diff] [blame] | 3306 | return btrfs_del_orphan_item(trans, inode->root, btrfs_ino(inode)); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3307 | } |
| 3308 | |
| 3309 | /* |
| 3310 | * this cleans up any orphans that may be left on the list from the last use |
| 3311 | * of this root. |
| 3312 | */ |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3313 | int btrfs_orphan_cleanup(struct btrfs_root *root) |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3314 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3315 | struct btrfs_fs_info *fs_info = root->fs_info; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3316 | struct btrfs_path *path; |
| 3317 | struct extent_buffer *leaf; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3318 | struct btrfs_key key, found_key; |
| 3319 | struct btrfs_trans_handle *trans; |
| 3320 | struct inode *inode; |
Josef Bacik | 8f6d7f4 | 2011-09-26 15:55:20 -0400 | [diff] [blame] | 3321 | u64 last_objectid = 0; |
Omar Sandoval | f7e9e8f | 2018-05-11 13:13:32 -0700 | [diff] [blame] | 3322 | int ret = 0, nr_unlink = 0; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3323 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3324 | if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED)) |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3325 | return 0; |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 3326 | |
| 3327 | path = btrfs_alloc_path(); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3328 | if (!path) { |
| 3329 | ret = -ENOMEM; |
| 3330 | goto out; |
| 3331 | } |
David Sterba | e4058b5 | 2015-11-27 16:31:35 +0100 | [diff] [blame] | 3332 | path->reada = READA_BACK; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3333 | |
| 3334 | key.objectid = BTRFS_ORPHAN_OBJECTID; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 3335 | key.type = BTRFS_ORPHAN_ITEM_KEY; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3336 | key.offset = (u64)-1; |
| 3337 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3338 | while (1) { |
| 3339 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3340 | if (ret < 0) |
| 3341 | goto out; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3342 | |
| 3343 | /* |
| 3344 | * 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] | 3345 | * 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] | 3346 | * find the key and see if we have stuff that matches |
| 3347 | */ |
| 3348 | if (ret > 0) { |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3349 | ret = 0; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3350 | if (path->slots[0] == 0) |
| 3351 | break; |
| 3352 | path->slots[0]--; |
| 3353 | } |
| 3354 | |
| 3355 | /* pull out the item */ |
| 3356 | leaf = path->nodes[0]; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3357 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 3358 | |
| 3359 | /* make sure the item matches what we want */ |
| 3360 | if (found_key.objectid != BTRFS_ORPHAN_OBJECTID) |
| 3361 | break; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 3362 | if (found_key.type != BTRFS_ORPHAN_ITEM_KEY) |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3363 | break; |
| 3364 | |
| 3365 | /* release the path since we're done with it */ |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3366 | btrfs_release_path(path); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3367 | |
| 3368 | /* |
| 3369 | * this is where we are basically btrfs_lookup, without the |
| 3370 | * crossing root thing. we store the inode number in the |
| 3371 | * offset of the orphan item. |
| 3372 | */ |
Josef Bacik | 8f6d7f4 | 2011-09-26 15:55:20 -0400 | [diff] [blame] | 3373 | |
| 3374 | if (found_key.offset == last_objectid) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3375 | btrfs_err(fs_info, |
| 3376 | "Error removing orphan entry, stopping orphan cleanup"); |
Josef Bacik | 8f6d7f4 | 2011-09-26 15:55:20 -0400 | [diff] [blame] | 3377 | ret = -EINVAL; |
| 3378 | goto out; |
| 3379 | } |
| 3380 | |
| 3381 | last_objectid = found_key.offset; |
| 3382 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3383 | found_key.objectid = found_key.offset; |
| 3384 | found_key.type = BTRFS_INODE_ITEM_KEY; |
| 3385 | found_key.offset = 0; |
David Sterba | 0202e83 | 2020-05-15 19:35:59 +0200 | [diff] [blame] | 3386 | inode = btrfs_iget(fs_info->sb, last_objectid, root); |
Rusty Russell | 8c6ffba | 2013-07-15 11:20:32 +0930 | [diff] [blame] | 3387 | ret = PTR_ERR_OR_ZERO(inode); |
Filipe Manana | 6771089 | 2016-06-06 11:51:25 +0100 | [diff] [blame] | 3388 | if (ret && ret != -ENOENT) |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3389 | goto out; |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 3390 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3391 | if (ret == -ENOENT && root == fs_info->tree_root) { |
Arne Jansen | f8e9e0b | 2011-12-14 20:12:02 -0500 | [diff] [blame] | 3392 | struct btrfs_root *dead_root; |
Arne Jansen | f8e9e0b | 2011-12-14 20:12:02 -0500 | [diff] [blame] | 3393 | int is_dead_root = 0; |
| 3394 | |
| 3395 | /* |
| 3396 | * this is an orphan in the tree root. Currently these |
| 3397 | * could come from 2 sources: |
| 3398 | * a) a snapshot deletion in progress |
| 3399 | * b) a free space cache inode |
| 3400 | * We need to distinguish those two, as the snapshot |
| 3401 | * orphan must not get deleted. |
| 3402 | * find_dead_roots already ran before us, so if this |
| 3403 | * is a snapshot deletion, we should find the root |
Robbie Ko | a619b3c | 2020-05-07 10:54:40 +0800 | [diff] [blame] | 3404 | * in the fs_roots radix tree. |
Arne Jansen | f8e9e0b | 2011-12-14 20:12:02 -0500 | [diff] [blame] | 3405 | */ |
Robbie Ko | a619b3c | 2020-05-07 10:54:40 +0800 | [diff] [blame] | 3406 | |
| 3407 | spin_lock(&fs_info->fs_roots_radix_lock); |
| 3408 | dead_root = radix_tree_lookup(&fs_info->fs_roots_radix, |
| 3409 | (unsigned long)found_key.objectid); |
| 3410 | if (dead_root && btrfs_root_refs(&dead_root->root_item) == 0) |
| 3411 | is_dead_root = 1; |
| 3412 | spin_unlock(&fs_info->fs_roots_radix_lock); |
| 3413 | |
Arne Jansen | f8e9e0b | 2011-12-14 20:12:02 -0500 | [diff] [blame] | 3414 | if (is_dead_root) { |
| 3415 | /* prevent this orphan from being found again */ |
| 3416 | key.offset = found_key.objectid - 1; |
| 3417 | continue; |
| 3418 | } |
Omar Sandoval | f7e9e8f | 2018-05-11 13:13:32 -0700 | [diff] [blame] | 3419 | |
Arne Jansen | f8e9e0b | 2011-12-14 20:12:02 -0500 | [diff] [blame] | 3420 | } |
Omar Sandoval | f7e9e8f | 2018-05-11 13:13:32 -0700 | [diff] [blame] | 3421 | |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 3422 | /* |
Omar Sandoval | f7e9e8f | 2018-05-11 13:13:32 -0700 | [diff] [blame] | 3423 | * If we have an inode with links, there are a couple of |
| 3424 | * possibilities. Old kernels (before v3.12) used to create an |
| 3425 | * orphan item for truncate indicating that there were possibly |
| 3426 | * extent items past i_size that needed to be deleted. In v3.12, |
| 3427 | * truncate was changed to update i_size in sync with the extent |
| 3428 | * items, but the (useless) orphan item was still created. Since |
| 3429 | * v4.18, we don't create the orphan item for truncate at all. |
| 3430 | * |
| 3431 | * So, this item could mean that we need to do a truncate, but |
| 3432 | * only if this filesystem was last used on a pre-v3.12 kernel |
| 3433 | * and was not cleanly unmounted. The odds of that are quite |
| 3434 | * slim, and it's a pain to do the truncate now, so just delete |
| 3435 | * the orphan item. |
| 3436 | * |
| 3437 | * It's also possible that this orphan item was supposed to be |
| 3438 | * deleted but wasn't. The inode number may have been reused, |
| 3439 | * but either way, we can delete the orphan item. |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 3440 | */ |
Omar Sandoval | f7e9e8f | 2018-05-11 13:13:32 -0700 | [diff] [blame] | 3441 | if (ret == -ENOENT || inode->i_nlink) { |
| 3442 | if (!ret) |
| 3443 | iput(inode); |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 3444 | trans = btrfs_start_transaction(root, 1); |
| 3445 | if (IS_ERR(trans)) { |
| 3446 | ret = PTR_ERR(trans); |
| 3447 | goto out; |
| 3448 | } |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3449 | btrfs_debug(fs_info, "auto deleting %Lu", |
| 3450 | found_key.objectid); |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 3451 | ret = btrfs_del_orphan_item(trans, root, |
| 3452 | found_key.objectid); |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 3453 | btrfs_end_transaction(trans); |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 3454 | if (ret) |
| 3455 | goto out; |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 3456 | continue; |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3457 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3458 | |
Omar Sandoval | f7e9e8f | 2018-05-11 13:13:32 -0700 | [diff] [blame] | 3459 | nr_unlink++; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3460 | |
| 3461 | /* this will do delete_inode and everything for us */ |
| 3462 | iput(inode); |
| 3463 | } |
Miao Xie | 3254c87 | 2011-11-10 20:45:05 -0500 | [diff] [blame] | 3464 | /* release the path since we're done with it */ |
| 3465 | btrfs_release_path(path); |
| 3466 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3467 | root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE; |
| 3468 | |
Omar Sandoval | a575cee | 2018-05-11 13:13:38 -0700 | [diff] [blame] | 3469 | if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) { |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 3470 | trans = btrfs_join_transaction(root); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3471 | if (!IS_ERR(trans)) |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 3472 | btrfs_end_transaction(trans); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3473 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3474 | |
| 3475 | if (nr_unlink) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3476 | btrfs_debug(fs_info, "unlinked %d orphans", nr_unlink); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3477 | |
| 3478 | out: |
| 3479 | if (ret) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3480 | btrfs_err(fs_info, "could not do orphan cleanup %d", ret); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3481 | btrfs_free_path(path); |
| 3482 | return ret; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3483 | } |
| 3484 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3485 | /* |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3486 | * very simple check to peek ahead in the leaf looking for xattrs. If we |
| 3487 | * don't find any xattrs, we know there can't be any acls. |
| 3488 | * |
| 3489 | * slot is the slot the inode is in, objectid is the objectid of the inode |
| 3490 | */ |
| 3491 | 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] | 3492 | int slot, u64 objectid, |
| 3493 | int *first_xattr_slot) |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3494 | { |
| 3495 | u32 nritems = btrfs_header_nritems(leaf); |
| 3496 | struct btrfs_key found_key; |
Josef Bacik | f23b5a5 | 2013-06-19 10:16:26 -0400 | [diff] [blame] | 3497 | static u64 xattr_access = 0; |
| 3498 | static u64 xattr_default = 0; |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3499 | int scanned = 0; |
| 3500 | |
Josef Bacik | f23b5a5 | 2013-06-19 10:16:26 -0400 | [diff] [blame] | 3501 | if (!xattr_access) { |
Andreas Gruenbacher | 97d7929 | 2015-12-02 14:44:35 +0100 | [diff] [blame] | 3502 | xattr_access = btrfs_name_hash(XATTR_NAME_POSIX_ACL_ACCESS, |
| 3503 | strlen(XATTR_NAME_POSIX_ACL_ACCESS)); |
| 3504 | xattr_default = btrfs_name_hash(XATTR_NAME_POSIX_ACL_DEFAULT, |
| 3505 | strlen(XATTR_NAME_POSIX_ACL_DEFAULT)); |
Josef Bacik | f23b5a5 | 2013-06-19 10:16:26 -0400 | [diff] [blame] | 3506 | } |
| 3507 | |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3508 | slot++; |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 3509 | *first_xattr_slot = -1; |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3510 | while (slot < nritems) { |
| 3511 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
| 3512 | |
| 3513 | /* we found a different objectid, there must not be acls */ |
| 3514 | if (found_key.objectid != objectid) |
| 3515 | return 0; |
| 3516 | |
| 3517 | /* we found an xattr, assume we've got an acl */ |
Josef Bacik | f23b5a5 | 2013-06-19 10:16:26 -0400 | [diff] [blame] | 3518 | if (found_key.type == BTRFS_XATTR_ITEM_KEY) { |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 3519 | if (*first_xattr_slot == -1) |
| 3520 | *first_xattr_slot = slot; |
Josef Bacik | f23b5a5 | 2013-06-19 10:16:26 -0400 | [diff] [blame] | 3521 | if (found_key.offset == xattr_access || |
| 3522 | found_key.offset == xattr_default) |
| 3523 | return 1; |
| 3524 | } |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3525 | |
| 3526 | /* |
| 3527 | * we found a key greater than an xattr key, there can't |
| 3528 | * be any acls later on |
| 3529 | */ |
| 3530 | if (found_key.type > BTRFS_XATTR_ITEM_KEY) |
| 3531 | return 0; |
| 3532 | |
| 3533 | slot++; |
| 3534 | scanned++; |
| 3535 | |
| 3536 | /* |
| 3537 | * it goes inode, inode backrefs, xattrs, extents, |
| 3538 | * so if there are a ton of hard links to an inode there can |
| 3539 | * be a lot of backrefs. Don't waste time searching too hard, |
| 3540 | * this is just an optimization |
| 3541 | */ |
| 3542 | if (scanned >= 8) |
| 3543 | break; |
| 3544 | } |
| 3545 | /* we hit the end of the leaf before we found an xattr or |
| 3546 | * something larger than an xattr. We have to assume the inode |
| 3547 | * has acls |
| 3548 | */ |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 3549 | if (*first_xattr_slot == -1) |
| 3550 | *first_xattr_slot = slot; |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3551 | return 1; |
| 3552 | } |
| 3553 | |
| 3554 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3555 | * read an inode from the btree into the in-memory inode |
| 3556 | */ |
Filipe Manana | 4222ea7 | 2018-10-24 10:13:03 +0100 | [diff] [blame] | 3557 | static int btrfs_read_locked_inode(struct inode *inode, |
| 3558 | struct btrfs_path *in_path) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3559 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3560 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Filipe Manana | 4222ea7 | 2018-10-24 10:13:03 +0100 | [diff] [blame] | 3561 | struct btrfs_path *path = in_path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3562 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3563 | struct btrfs_inode_item *inode_item; |
| 3564 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 3565 | struct btrfs_key location; |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 3566 | unsigned long ptr; |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3567 | int maybe_acls; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 3568 | u32 rdev; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3569 | int ret; |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 3570 | bool filled = false; |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 3571 | int first_xattr_slot; |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 3572 | |
| 3573 | ret = btrfs_fill_inode(inode, &rdev); |
| 3574 | if (!ret) |
| 3575 | filled = true; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3576 | |
Filipe Manana | 4222ea7 | 2018-10-24 10:13:03 +0100 | [diff] [blame] | 3577 | if (!path) { |
| 3578 | path = btrfs_alloc_path(); |
| 3579 | if (!path) |
| 3580 | return -ENOMEM; |
| 3581 | } |
Mark Fasheh | 1748f84 | 2011-07-12 11:25:31 -0700 | [diff] [blame] | 3582 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3583 | memcpy(&location, &BTRFS_I(inode)->location, sizeof(location)); |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 3584 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3585 | ret = btrfs_lookup_inode(NULL, root, path, &location, 0); |
Filipe Manana | 6771089 | 2016-06-06 11:51:25 +0100 | [diff] [blame] | 3586 | if (ret) { |
Filipe Manana | 4222ea7 | 2018-10-24 10:13:03 +0100 | [diff] [blame] | 3587 | if (path != in_path) |
| 3588 | btrfs_free_path(path); |
Al Viro | f5b3a41 | 2018-07-29 23:04:51 +0100 | [diff] [blame] | 3589 | return ret; |
Filipe Manana | 6771089 | 2016-06-06 11:51:25 +0100 | [diff] [blame] | 3590 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3591 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3592 | leaf = path->nodes[0]; |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 3593 | |
| 3594 | if (filled) |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 3595 | goto cache_index; |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 3596 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3597 | inode_item = btrfs_item_ptr(leaf, path->slots[0], |
| 3598 | struct btrfs_inode_item); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3599 | inode->i_mode = btrfs_inode_mode(leaf, inode_item); |
Miklos Szeredi | bfe8684 | 2011-10-28 14:13:29 +0200 | [diff] [blame] | 3600 | set_nlink(inode, btrfs_inode_nlink(leaf, inode_item)); |
Eric W. Biederman | 2f2f43d | 2012-02-10 11:05:07 -0800 | [diff] [blame] | 3601 | i_uid_write(inode, btrfs_inode_uid(leaf, inode_item)); |
| 3602 | i_gid_write(inode, btrfs_inode_gid(leaf, inode_item)); |
Nikolay Borisov | 6ef06d2 | 2017-02-20 13:50:34 +0200 | [diff] [blame] | 3603 | 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] | 3604 | btrfs_inode_set_file_extent_range(BTRFS_I(inode), 0, |
| 3605 | round_up(i_size_read(inode), fs_info->sectorsize)); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3606 | |
David Sterba | a937b97 | 2014-12-12 17:39:12 +0100 | [diff] [blame] | 3607 | inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->atime); |
| 3608 | inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->atime); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3609 | |
David Sterba | a937b97 | 2014-12-12 17:39:12 +0100 | [diff] [blame] | 3610 | inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->mtime); |
| 3611 | inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->mtime); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3612 | |
David Sterba | a937b97 | 2014-12-12 17:39:12 +0100 | [diff] [blame] | 3613 | inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->ctime); |
| 3614 | inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->ctime); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3615 | |
chandan r | 9cc97d6 | 2012-07-04 12:48:07 +0530 | [diff] [blame] | 3616 | BTRFS_I(inode)->i_otime.tv_sec = |
| 3617 | btrfs_timespec_sec(leaf, &inode_item->otime); |
| 3618 | BTRFS_I(inode)->i_otime.tv_nsec = |
| 3619 | btrfs_timespec_nsec(leaf, &inode_item->otime); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3620 | |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 3621 | inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item)); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3622 | BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item); |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 3623 | BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item); |
| 3624 | |
Jeff Layton | c7f88c4 | 2017-12-11 06:35:12 -0500 | [diff] [blame] | 3625 | inode_set_iversion_queried(inode, |
| 3626 | btrfs_inode_sequence(leaf, inode_item)); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3627 | inode->i_generation = BTRFS_I(inode)->generation; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 3628 | inode->i_rdev = 0; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3629 | rdev = btrfs_inode_rdev(leaf, inode_item); |
| 3630 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 3631 | BTRFS_I(inode)->index_cnt = (u64)-1; |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 3632 | BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item); |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 3633 | |
| 3634 | cache_index: |
Yang Dongsheng | 6e17d30 | 2015-04-09 12:08:43 +0800 | [diff] [blame] | 3635 | /* |
| 3636 | * If we were modified in the current generation and evicted from memory |
| 3637 | * and then re-read we need to do a full sync since we don't have any |
| 3638 | * idea about which extents were modified before we were evicted from |
| 3639 | * cache. |
| 3640 | * |
| 3641 | * This is required for both inode re-read from disk and delayed inode |
| 3642 | * in delayed_nodes_tree. |
| 3643 | */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3644 | if (BTRFS_I(inode)->last_trans == fs_info->generation) |
Yang Dongsheng | 6e17d30 | 2015-04-09 12:08:43 +0800 | [diff] [blame] | 3645 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, |
| 3646 | &BTRFS_I(inode)->runtime_flags); |
| 3647 | |
Filipe Manana | bde6c24 | 2015-07-24 00:00:19 +0100 | [diff] [blame] | 3648 | /* |
| 3649 | * We don't persist the id of the transaction where an unlink operation |
| 3650 | * against the inode was last made. So here we assume the inode might |
| 3651 | * have been evicted, and therefore the exact value of last_unlink_trans |
| 3652 | * lost, and set it to last_trans to avoid metadata inconsistencies |
| 3653 | * between the inode and its parent if the inode is fsync'ed and the log |
| 3654 | * replayed. For example, in the scenario: |
| 3655 | * |
| 3656 | * touch mydir/foo |
| 3657 | * ln mydir/foo mydir/bar |
| 3658 | * sync |
| 3659 | * unlink mydir/bar |
| 3660 | * echo 2 > /proc/sys/vm/drop_caches # evicts inode |
| 3661 | * xfs_io -c fsync mydir/foo |
| 3662 | * <power failure> |
| 3663 | * mount fs, triggers fsync log replay |
| 3664 | * |
| 3665 | * We must make sure that when we fsync our inode foo we also log its |
| 3666 | * parent inode, otherwise after log replay the parent still has the |
| 3667 | * dentry with the "bar" name but our inode foo has a link count of 1 |
| 3668 | * and doesn't have an inode ref with the name "bar" anymore. |
| 3669 | * |
| 3670 | * Setting last_unlink_trans to last_trans is a pessimistic approach, |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 3671 | * but it guarantees correctness at the expense of occasional full |
Filipe Manana | bde6c24 | 2015-07-24 00:00:19 +0100 | [diff] [blame] | 3672 | * transaction commits on fsync if our inode is a directory, or if our |
| 3673 | * inode is not a directory, logging its parent unnecessarily. |
| 3674 | */ |
| 3675 | BTRFS_I(inode)->last_unlink_trans = BTRFS_I(inode)->last_trans; |
| 3676 | |
Filipe Manana | 3ebac17 | 2020-07-15 12:30:43 +0100 | [diff] [blame] | 3677 | /* |
| 3678 | * Same logic as for last_unlink_trans. We don't persist the generation |
| 3679 | * of the last transaction where this inode was used for a reflink |
| 3680 | * operation, so after eviction and reloading the inode we must be |
| 3681 | * pessimistic and assume the last transaction that modified the inode. |
| 3682 | */ |
| 3683 | BTRFS_I(inode)->last_reflink_trans = BTRFS_I(inode)->last_trans; |
| 3684 | |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 3685 | path->slots[0]++; |
| 3686 | if (inode->i_nlink != 1 || |
| 3687 | path->slots[0] >= btrfs_header_nritems(leaf)) |
| 3688 | goto cache_acl; |
| 3689 | |
| 3690 | btrfs_item_key_to_cpu(leaf, &location, path->slots[0]); |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 3691 | if (location.objectid != btrfs_ino(BTRFS_I(inode))) |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 3692 | goto cache_acl; |
| 3693 | |
| 3694 | ptr = btrfs_item_ptr_offset(leaf, path->slots[0]); |
| 3695 | if (location.type == BTRFS_INODE_REF_KEY) { |
| 3696 | struct btrfs_inode_ref *ref; |
| 3697 | |
| 3698 | ref = (struct btrfs_inode_ref *)ptr; |
| 3699 | BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref); |
| 3700 | } else if (location.type == BTRFS_INODE_EXTREF_KEY) { |
| 3701 | struct btrfs_inode_extref *extref; |
| 3702 | |
| 3703 | extref = (struct btrfs_inode_extref *)ptr; |
| 3704 | BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf, |
| 3705 | extref); |
| 3706 | } |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 3707 | cache_acl: |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3708 | /* |
| 3709 | * try to precache a NULL acl entry for files that don't have |
| 3710 | * any xattrs or acls |
| 3711 | */ |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3712 | maybe_acls = acls_after_inode_item(leaf, path->slots[0], |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 3713 | btrfs_ino(BTRFS_I(inode)), &first_xattr_slot); |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 3714 | if (first_xattr_slot != -1) { |
| 3715 | path->slots[0] = first_xattr_slot; |
| 3716 | ret = btrfs_load_inode_props(inode, path); |
| 3717 | if (ret) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3718 | btrfs_err(fs_info, |
David Sterba | 351fd35 | 2014-05-15 16:48:20 +0200 | [diff] [blame] | 3719 | "error loading props for ino %llu (root %llu): %d", |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 3720 | btrfs_ino(BTRFS_I(inode)), |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 3721 | root->root_key.objectid, ret); |
| 3722 | } |
Filipe Manana | 4222ea7 | 2018-10-24 10:13:03 +0100 | [diff] [blame] | 3723 | if (path != in_path) |
| 3724 | btrfs_free_path(path); |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 3725 | |
Al Viro | 72c0490 | 2009-06-24 16:58:48 -0400 | [diff] [blame] | 3726 | if (!maybe_acls) |
| 3727 | cache_no_acl(inode); |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3728 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3729 | switch (inode->i_mode & S_IFMT) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3730 | case S_IFREG: |
| 3731 | inode->i_mapping->a_ops = &btrfs_aops; |
| 3732 | inode->i_fop = &btrfs_file_operations; |
| 3733 | inode->i_op = &btrfs_file_inode_operations; |
| 3734 | break; |
| 3735 | case S_IFDIR: |
| 3736 | inode->i_fop = &btrfs_dir_file_operations; |
Omar Sandoval | 67ade05 | 2017-01-25 17:06:38 -0800 | [diff] [blame] | 3737 | inode->i_op = &btrfs_dir_inode_operations; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3738 | break; |
| 3739 | case S_IFLNK: |
| 3740 | inode->i_op = &btrfs_symlink_inode_operations; |
Al Viro | 21fc61c | 2015-11-17 01:07:57 -0500 | [diff] [blame] | 3741 | inode_nohighmem(inode); |
Omar Sandoval | 4779cc0 | 2018-09-24 15:16:55 -0700 | [diff] [blame] | 3742 | inode->i_mapping->a_ops = &btrfs_aops; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3743 | break; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 3744 | default: |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 3745 | inode->i_op = &btrfs_special_inode_operations; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 3746 | init_special_inode(inode, inode->i_mode, rdev); |
| 3747 | break; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3748 | } |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 3749 | |
David Sterba | 7b6a221 | 2018-03-26 18:40:21 +0200 | [diff] [blame] | 3750 | btrfs_sync_inode_flags_to_i_flags(inode); |
Filipe Manana | 6771089 | 2016-06-06 11:51:25 +0100 | [diff] [blame] | 3751 | return 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3752 | } |
| 3753 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3754 | /* |
| 3755 | * given a leaf and an inode, copy the inode fields into the leaf |
| 3756 | */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3757 | static void fill_inode_item(struct btrfs_trans_handle *trans, |
| 3758 | struct extent_buffer *leaf, |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3759 | struct btrfs_inode_item *item, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3760 | struct inode *inode) |
| 3761 | { |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3762 | struct btrfs_map_token token; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3763 | |
David Sterba | c82f823 | 2019-08-09 17:48:21 +0200 | [diff] [blame] | 3764 | btrfs_init_map_token(&token, leaf); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3765 | |
David Sterba | cc4c13d | 2020-04-29 02:15:56 +0200 | [diff] [blame] | 3766 | btrfs_set_token_inode_uid(&token, item, i_uid_read(inode)); |
| 3767 | btrfs_set_token_inode_gid(&token, item, i_gid_read(inode)); |
| 3768 | btrfs_set_token_inode_size(&token, item, BTRFS_I(inode)->disk_i_size); |
| 3769 | btrfs_set_token_inode_mode(&token, item, inode->i_mode); |
| 3770 | btrfs_set_token_inode_nlink(&token, item, inode->i_nlink); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3771 | |
David Sterba | cc4c13d | 2020-04-29 02:15:56 +0200 | [diff] [blame] | 3772 | btrfs_set_token_timespec_sec(&token, &item->atime, |
| 3773 | inode->i_atime.tv_sec); |
| 3774 | btrfs_set_token_timespec_nsec(&token, &item->atime, |
| 3775 | inode->i_atime.tv_nsec); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3776 | |
David Sterba | cc4c13d | 2020-04-29 02:15:56 +0200 | [diff] [blame] | 3777 | btrfs_set_token_timespec_sec(&token, &item->mtime, |
| 3778 | inode->i_mtime.tv_sec); |
| 3779 | btrfs_set_token_timespec_nsec(&token, &item->mtime, |
| 3780 | inode->i_mtime.tv_nsec); |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3781 | |
David Sterba | cc4c13d | 2020-04-29 02:15:56 +0200 | [diff] [blame] | 3782 | btrfs_set_token_timespec_sec(&token, &item->ctime, |
| 3783 | inode->i_ctime.tv_sec); |
| 3784 | btrfs_set_token_timespec_nsec(&token, &item->ctime, |
| 3785 | inode->i_ctime.tv_nsec); |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3786 | |
David Sterba | cc4c13d | 2020-04-29 02:15:56 +0200 | [diff] [blame] | 3787 | btrfs_set_token_timespec_sec(&token, &item->otime, |
| 3788 | BTRFS_I(inode)->i_otime.tv_sec); |
| 3789 | btrfs_set_token_timespec_nsec(&token, &item->otime, |
| 3790 | BTRFS_I(inode)->i_otime.tv_nsec); |
chandan r | 9cc97d6 | 2012-07-04 12:48:07 +0530 | [diff] [blame] | 3791 | |
David Sterba | cc4c13d | 2020-04-29 02:15:56 +0200 | [diff] [blame] | 3792 | btrfs_set_token_inode_nbytes(&token, item, inode_get_bytes(inode)); |
| 3793 | btrfs_set_token_inode_generation(&token, item, |
| 3794 | BTRFS_I(inode)->generation); |
| 3795 | btrfs_set_token_inode_sequence(&token, item, inode_peek_iversion(inode)); |
| 3796 | btrfs_set_token_inode_transid(&token, item, trans->transid); |
| 3797 | btrfs_set_token_inode_rdev(&token, item, inode->i_rdev); |
| 3798 | btrfs_set_token_inode_flags(&token, item, BTRFS_I(inode)->flags); |
| 3799 | btrfs_set_token_inode_block_group(&token, item, 0); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3800 | } |
| 3801 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3802 | /* |
| 3803 | * copy everything in the in-memory inode into the btree. |
| 3804 | */ |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 3805 | static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans, |
Nikolay Borisov | dfeb9e7 | 2020-11-02 16:48:58 +0200 | [diff] [blame] | 3806 | struct btrfs_root *root, |
| 3807 | struct btrfs_inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3808 | { |
| 3809 | struct btrfs_inode_item *inode_item; |
| 3810 | struct btrfs_path *path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3811 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3812 | int ret; |
| 3813 | |
| 3814 | path = btrfs_alloc_path(); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 3815 | if (!path) |
| 3816 | return -ENOMEM; |
| 3817 | |
Nikolay Borisov | dfeb9e7 | 2020-11-02 16:48:58 +0200 | [diff] [blame] | 3818 | ret = btrfs_lookup_inode(trans, root, path, &inode->location, 1); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3819 | if (ret) { |
| 3820 | if (ret > 0) |
| 3821 | ret = -ENOENT; |
| 3822 | goto failed; |
| 3823 | } |
| 3824 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3825 | leaf = path->nodes[0]; |
| 3826 | inode_item = btrfs_item_ptr(leaf, path->slots[0], |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 3827 | struct btrfs_inode_item); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3828 | |
Nikolay Borisov | dfeb9e7 | 2020-11-02 16:48:58 +0200 | [diff] [blame] | 3829 | fill_inode_item(trans, leaf, inode_item, &inode->vfs_inode); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3830 | btrfs_mark_buffer_dirty(leaf); |
Nikolay Borisov | dfeb9e7 | 2020-11-02 16:48:58 +0200 | [diff] [blame] | 3831 | btrfs_set_inode_last_trans(trans, inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3832 | ret = 0; |
| 3833 | failed: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3834 | btrfs_free_path(path); |
| 3835 | return ret; |
| 3836 | } |
| 3837 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3838 | /* |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 3839 | * copy everything in the in-memory inode into the btree. |
| 3840 | */ |
| 3841 | noinline int btrfs_update_inode(struct btrfs_trans_handle *trans, |
Nikolay Borisov | 9a56fcd | 2020-11-02 16:48:59 +0200 | [diff] [blame] | 3842 | struct btrfs_root *root, |
| 3843 | struct btrfs_inode *inode) |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 3844 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3845 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 3846 | int ret; |
| 3847 | |
| 3848 | /* |
| 3849 | * If the inode is a free space inode, we can deadlock during commit |
| 3850 | * if we put it into the delayed code. |
| 3851 | * |
| 3852 | * The data relocation inode should also be directly updated |
| 3853 | * without delay |
| 3854 | */ |
Nikolay Borisov | 9a56fcd | 2020-11-02 16:48:59 +0200 | [diff] [blame] | 3855 | if (!btrfs_is_free_space_inode(inode) |
Josef Bacik | 1d52c78 | 2014-09-18 11:30:44 -0400 | [diff] [blame] | 3856 | && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3857 | && !test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) { |
Alexander Block | 8ea05e3 | 2012-07-25 17:35:53 +0200 | [diff] [blame] | 3858 | btrfs_update_root_times(trans, root); |
| 3859 | |
Nikolay Borisov | 9a56fcd | 2020-11-02 16:48:59 +0200 | [diff] [blame] | 3860 | ret = btrfs_delayed_update_inode(trans, root, inode); |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 3861 | if (!ret) |
Nikolay Borisov | 9a56fcd | 2020-11-02 16:48:59 +0200 | [diff] [blame] | 3862 | btrfs_set_inode_last_trans(trans, inode); |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 3863 | return ret; |
| 3864 | } |
| 3865 | |
Nikolay Borisov | 9a56fcd | 2020-11-02 16:48:59 +0200 | [diff] [blame] | 3866 | return btrfs_update_inode_item(trans, root, inode); |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 3867 | } |
| 3868 | |
Nikolay Borisov | 729f796 | 2020-11-02 16:49:06 +0200 | [diff] [blame] | 3869 | int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans, |
| 3870 | struct btrfs_root *root, struct btrfs_inode *inode) |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 3871 | { |
| 3872 | int ret; |
| 3873 | |
Nikolay Borisov | 729f796 | 2020-11-02 16:49:06 +0200 | [diff] [blame] | 3874 | ret = btrfs_update_inode(trans, root, inode); |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 3875 | if (ret == -ENOSPC) |
Nikolay Borisov | 729f796 | 2020-11-02 16:49:06 +0200 | [diff] [blame] | 3876 | return btrfs_update_inode_item(trans, root, inode); |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 3877 | return ret; |
| 3878 | } |
| 3879 | |
| 3880 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3881 | * unlink helper that gets used here in inode.c and in the tree logging |
| 3882 | * recovery code. It remove a link in a directory with a given name, and |
| 3883 | * also drops the back refs in the inode to the directory |
| 3884 | */ |
Al Viro | 9298679 | 2011-03-04 17:14:37 +0000 | [diff] [blame] | 3885 | static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans, |
| 3886 | struct btrfs_root *root, |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 3887 | struct btrfs_inode *dir, |
| 3888 | struct btrfs_inode *inode, |
Al Viro | 9298679 | 2011-03-04 17:14:37 +0000 | [diff] [blame] | 3889 | const char *name, int name_len) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3890 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3891 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3892 | struct btrfs_path *path; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3893 | int ret = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3894 | struct btrfs_dir_item *di; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 3895 | u64 index; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3896 | u64 ino = btrfs_ino(inode); |
| 3897 | u64 dir_ino = btrfs_ino(dir); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3898 | |
| 3899 | path = btrfs_alloc_path(); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3900 | if (!path) { |
| 3901 | ret = -ENOMEM; |
Tsutomu Itoh | 554233a | 2011-02-03 03:16:25 +0000 | [diff] [blame] | 3902 | goto out; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3903 | } |
| 3904 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3905 | di = btrfs_lookup_dir_item(trans, root, path, dir_ino, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3906 | name, name_len, -1); |
Liu Bo | 3cf5068 | 2018-09-12 06:06:26 +0800 | [diff] [blame] | 3907 | if (IS_ERR_OR_NULL(di)) { |
| 3908 | ret = di ? PTR_ERR(di) : -ENOENT; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3909 | goto err; |
| 3910 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3911 | ret = btrfs_delete_one_dir_name(trans, root, path, di); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3912 | if (ret) |
| 3913 | goto err; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3914 | btrfs_release_path(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3915 | |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 3916 | /* |
| 3917 | * If we don't have dir index, we have to get it by looking up |
| 3918 | * the inode ref, since we get the inode ref, remove it directly, |
| 3919 | * it is unnecessary to do delayed deletion. |
| 3920 | * |
| 3921 | * But if we have dir index, needn't search inode ref to get it. |
| 3922 | * Since the inode ref is close to the inode item, it is better |
| 3923 | * that we delay to delete it, and just do this deletion when |
| 3924 | * we update the inode item. |
| 3925 | */ |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 3926 | if (inode->dir_index) { |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 3927 | ret = btrfs_delayed_delete_inode_ref(inode); |
| 3928 | if (!ret) { |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 3929 | index = inode->dir_index; |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 3930 | goto skip_backref; |
| 3931 | } |
| 3932 | } |
| 3933 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3934 | ret = btrfs_del_inode_ref(trans, root, name, name_len, ino, |
| 3935 | dir_ino, &index); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 3936 | if (ret) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3937 | btrfs_info(fs_info, |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 3938 | "failed to delete reference to %.*s, inode %llu parent %llu", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 3939 | name_len, name, ino, dir_ino); |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3940 | btrfs_abort_transaction(trans, ret); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 3941 | goto err; |
| 3942 | } |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 3943 | skip_backref: |
Lu Fengqi | 9add294 | 2018-08-01 11:32:26 +0800 | [diff] [blame] | 3944 | ret = btrfs_delete_delayed_dir_index(trans, dir, index); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3945 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3946 | btrfs_abort_transaction(trans, ret); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3947 | goto err; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3948 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3949 | |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 3950 | ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len, inode, |
| 3951 | dir_ino); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3952 | if (ret != 0 && ret != -ENOENT) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3953 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3954 | goto err; |
| 3955 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3956 | |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 3957 | ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len, dir, |
| 3958 | index); |
Chris Mason | 6418c96 | 2010-10-30 07:34:24 -0400 | [diff] [blame] | 3959 | if (ret == -ENOENT) |
| 3960 | ret = 0; |
Zach Brown | d4e3991 | 2013-04-02 21:02:16 +0000 | [diff] [blame] | 3961 | else if (ret) |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3962 | btrfs_abort_transaction(trans, ret); |
Josef Bacik | 63611e7 | 2019-06-18 10:59:18 -0400 | [diff] [blame] | 3963 | |
| 3964 | /* |
| 3965 | * If we have a pending delayed iput we could end up with the final iput |
| 3966 | * being run in btrfs-cleaner context. If we have enough of these built |
| 3967 | * up we can end up burning a lot of time in btrfs-cleaner without any |
| 3968 | * way to throttle the unlinks. Since we're currently holding a ref on |
| 3969 | * the inode we can run the delayed iput here without any issues as the |
| 3970 | * final iput won't be done until after we drop the ref we're currently |
| 3971 | * holding. |
| 3972 | */ |
| 3973 | btrfs_run_delayed_iput(fs_info, inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3974 | err: |
| 3975 | btrfs_free_path(path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3976 | if (ret) |
| 3977 | goto out; |
| 3978 | |
Nikolay Borisov | 6ef06d2 | 2017-02-20 13:50:34 +0200 | [diff] [blame] | 3979 | 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] | 3980 | inode_inc_iversion(&inode->vfs_inode); |
| 3981 | inode_inc_iversion(&dir->vfs_inode); |
| 3982 | inode->vfs_inode.i_ctime = dir->vfs_inode.i_mtime = |
| 3983 | dir->vfs_inode.i_ctime = current_time(&inode->vfs_inode); |
Nikolay Borisov | 9a56fcd | 2020-11-02 16:48:59 +0200 | [diff] [blame] | 3984 | ret = btrfs_update_inode(trans, root, dir); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3985 | out: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3986 | return ret; |
| 3987 | } |
| 3988 | |
Al Viro | 9298679 | 2011-03-04 17:14:37 +0000 | [diff] [blame] | 3989 | int btrfs_unlink_inode(struct btrfs_trans_handle *trans, |
| 3990 | struct btrfs_root *root, |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 3991 | struct btrfs_inode *dir, struct btrfs_inode *inode, |
Al Viro | 9298679 | 2011-03-04 17:14:37 +0000 | [diff] [blame] | 3992 | const char *name, int name_len) |
| 3993 | { |
| 3994 | int ret; |
| 3995 | ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len); |
| 3996 | if (!ret) { |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 3997 | drop_nlink(&inode->vfs_inode); |
Nikolay Borisov | 9a56fcd | 2020-11-02 16:48:59 +0200 | [diff] [blame] | 3998 | ret = btrfs_update_inode(trans, root, inode); |
Al Viro | 9298679 | 2011-03-04 17:14:37 +0000 | [diff] [blame] | 3999 | } |
| 4000 | return ret; |
| 4001 | } |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4002 | |
| 4003 | /* |
| 4004 | * helper to start transaction for unlink and rmdir. |
| 4005 | * |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 4006 | * unlink and rmdir are special in btrfs, they do not always free space, so |
| 4007 | * if we cannot make our reservations the normal way try and see if there is |
| 4008 | * plenty of slack room in the global reserve to migrate, otherwise we cannot |
| 4009 | * allow the unlink to occur. |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4010 | */ |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 4011 | static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir) |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4012 | { |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4013 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4014 | |
Josef Bacik | e70bea5 | 2011-10-11 14:18:24 -0400 | [diff] [blame] | 4015 | /* |
| 4016 | * 1 for the possible orphan item |
| 4017 | * 1 for the dir item |
| 4018 | * 1 for the dir index |
| 4019 | * 1 for the inode ref |
Josef Bacik | e70bea5 | 2011-10-11 14:18:24 -0400 | [diff] [blame] | 4020 | * 1 for the inode |
| 4021 | */ |
Josef Bacik | 7f9fe61 | 2020-03-13 15:58:05 -0400 | [diff] [blame] | 4022 | return btrfs_start_transaction_fallback_global_rsv(root, 5); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4023 | } |
| 4024 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4025 | static int btrfs_unlink(struct inode *dir, struct dentry *dentry) |
| 4026 | { |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4027 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4028 | struct btrfs_trans_handle *trans; |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 4029 | struct inode *inode = d_inode(dentry); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4030 | int ret; |
| 4031 | |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 4032 | trans = __unlink_start_trans(dir); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4033 | if (IS_ERR(trans)) |
Josef Bacik | 5df6a9f | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4034 | return PTR_ERR(trans); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4035 | |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 4036 | btrfs_record_unlink_dir(trans, BTRFS_I(dir), BTRFS_I(d_inode(dentry)), |
| 4037 | 0); |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 4038 | |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 4039 | ret = btrfs_unlink_inode(trans, root, BTRFS_I(dir), |
| 4040 | BTRFS_I(d_inode(dentry)), dentry->d_name.name, |
| 4041 | dentry->d_name.len); |
Tsutomu Itoh | b532402 | 2011-07-19 07:27:20 +0000 | [diff] [blame] | 4042 | if (ret) |
| 4043 | goto out; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 4044 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4045 | if (inode->i_nlink == 0) { |
Nikolay Borisov | 73f2e54 | 2017-02-20 13:50:59 +0200 | [diff] [blame] | 4046 | ret = btrfs_orphan_add(trans, BTRFS_I(inode)); |
Tsutomu Itoh | b532402 | 2011-07-19 07:27:20 +0000 | [diff] [blame] | 4047 | if (ret) |
| 4048 | goto out; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4049 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 4050 | |
Tsutomu Itoh | b532402 | 2011-07-19 07:27:20 +0000 | [diff] [blame] | 4051 | out: |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 4052 | btrfs_end_transaction(trans); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4053 | btrfs_btree_balance_dirty(root->fs_info); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4054 | return ret; |
| 4055 | } |
| 4056 | |
Misono Tomohiro | f60a236 | 2018-04-18 11:34:52 +0900 | [diff] [blame] | 4057 | static int btrfs_unlink_subvol(struct btrfs_trans_handle *trans, |
Josef Bacik | 045d396 | 2019-12-18 17:20:27 -0500 | [diff] [blame] | 4058 | struct inode *dir, struct dentry *dentry) |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4059 | { |
Lu Fengqi | 401b3b1 | 2018-08-01 11:32:30 +0800 | [diff] [blame] | 4060 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Josef Bacik | 045d396 | 2019-12-18 17:20:27 -0500 | [diff] [blame] | 4061 | struct btrfs_inode *inode = BTRFS_I(d_inode(dentry)); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4062 | struct btrfs_path *path; |
| 4063 | struct extent_buffer *leaf; |
| 4064 | struct btrfs_dir_item *di; |
| 4065 | struct btrfs_key key; |
Josef Bacik | 045d396 | 2019-12-18 17:20:27 -0500 | [diff] [blame] | 4066 | const char *name = dentry->d_name.name; |
| 4067 | int name_len = dentry->d_name.len; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4068 | u64 index; |
| 4069 | int ret; |
Josef Bacik | 045d396 | 2019-12-18 17:20:27 -0500 | [diff] [blame] | 4070 | u64 objectid; |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 4071 | u64 dir_ino = btrfs_ino(BTRFS_I(dir)); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4072 | |
Josef Bacik | 045d396 | 2019-12-18 17:20:27 -0500 | [diff] [blame] | 4073 | if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID) { |
| 4074 | objectid = inode->root->root_key.objectid; |
| 4075 | } else if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) { |
| 4076 | objectid = inode->location.objectid; |
| 4077 | } else { |
| 4078 | WARN_ON(1); |
| 4079 | return -EINVAL; |
| 4080 | } |
| 4081 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4082 | path = btrfs_alloc_path(); |
| 4083 | if (!path) |
| 4084 | return -ENOMEM; |
| 4085 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4086 | di = btrfs_lookup_dir_item(trans, root, path, dir_ino, |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4087 | name, name_len, -1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4088 | if (IS_ERR_OR_NULL(di)) { |
Liu Bo | 3cf5068 | 2018-09-12 06:06:26 +0800 | [diff] [blame] | 4089 | ret = di ? PTR_ERR(di) : -ENOENT; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4090 | goto out; |
| 4091 | } |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4092 | |
| 4093 | leaf = path->nodes[0]; |
| 4094 | btrfs_dir_item_key_to_cpu(leaf, di, &key); |
| 4095 | WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid); |
| 4096 | ret = btrfs_delete_one_dir_name(trans, root, path, di); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4097 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 4098 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4099 | goto out; |
| 4100 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4101 | btrfs_release_path(path); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4102 | |
Josef Bacik | d49d328 | 2019-12-18 17:20:28 -0500 | [diff] [blame] | 4103 | /* |
| 4104 | * This is a placeholder inode for a subvolume we didn't have a |
| 4105 | * reference to at the time of the snapshot creation. In the meantime |
| 4106 | * we could have renamed the real subvol link into our snapshot, so |
| 4107 | * depending on btrfs_del_root_ref to return -ENOENT here is incorret. |
| 4108 | * Instead simply lookup the dir_index_item for this entry so we can |
| 4109 | * remove it. Otherwise we know we have a ref to the root and we can |
| 4110 | * call btrfs_del_root_ref, and it _shouldn't_ fail. |
| 4111 | */ |
| 4112 | if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) { |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4113 | di = btrfs_search_dir_index_item(root, path, dir_ino, |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4114 | name, name_len); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4115 | if (IS_ERR_OR_NULL(di)) { |
| 4116 | if (!di) |
| 4117 | ret = -ENOENT; |
| 4118 | else |
| 4119 | ret = PTR_ERR(di); |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 4120 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4121 | goto out; |
| 4122 | } |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4123 | |
| 4124 | leaf = path->nodes[0]; |
| 4125 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4126 | index = key.offset; |
Josef Bacik | d49d328 | 2019-12-18 17:20:28 -0500 | [diff] [blame] | 4127 | btrfs_release_path(path); |
| 4128 | } else { |
| 4129 | ret = btrfs_del_root_ref(trans, objectid, |
| 4130 | root->root_key.objectid, dir_ino, |
| 4131 | &index, name, name_len); |
| 4132 | if (ret) { |
| 4133 | btrfs_abort_transaction(trans, ret); |
| 4134 | goto out; |
| 4135 | } |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4136 | } |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4137 | |
Lu Fengqi | 9add294 | 2018-08-01 11:32:26 +0800 | [diff] [blame] | 4138 | ret = btrfs_delete_delayed_dir_index(trans, BTRFS_I(dir), index); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4139 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 4140 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4141 | goto out; |
| 4142 | } |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 4143 | |
Nikolay Borisov | 6ef06d2 | 2017-02-20 13:50:34 +0200 | [diff] [blame] | 4144 | 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] | 4145 | inode_inc_iversion(dir); |
Deepa Dinamani | c2050a4 | 2016-09-14 07:48:06 -0700 | [diff] [blame] | 4146 | dir->i_mtime = dir->i_ctime = current_time(dir); |
Nikolay Borisov | 729f796 | 2020-11-02 16:49:06 +0200 | [diff] [blame] | 4147 | ret = btrfs_update_inode_fallback(trans, root, BTRFS_I(dir)); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4148 | if (ret) |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 4149 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4150 | out: |
Josef Bacik | 71d7aed | 2011-06-14 14:24:32 -0400 | [diff] [blame] | 4151 | btrfs_free_path(path); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4152 | return ret; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4153 | } |
| 4154 | |
Misono Tomohiro | ec42f16 | 2018-04-18 11:34:13 +0900 | [diff] [blame] | 4155 | /* |
| 4156 | * Helper to check if the subvolume references other subvolumes or if it's |
| 4157 | * default. |
| 4158 | */ |
Misono Tomohiro | f60a236 | 2018-04-18 11:34:52 +0900 | [diff] [blame] | 4159 | static noinline int may_destroy_subvol(struct btrfs_root *root) |
Misono Tomohiro | ec42f16 | 2018-04-18 11:34:13 +0900 | [diff] [blame] | 4160 | { |
| 4161 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 4162 | struct btrfs_path *path; |
| 4163 | struct btrfs_dir_item *di; |
| 4164 | struct btrfs_key key; |
| 4165 | u64 dir_id; |
| 4166 | int ret; |
| 4167 | |
| 4168 | path = btrfs_alloc_path(); |
| 4169 | if (!path) |
| 4170 | return -ENOMEM; |
| 4171 | |
| 4172 | /* Make sure this root isn't set as the default subvol */ |
| 4173 | dir_id = btrfs_super_root_dir(fs_info->super_copy); |
| 4174 | di = btrfs_lookup_dir_item(NULL, fs_info->tree_root, path, |
| 4175 | dir_id, "default", 7, 0); |
| 4176 | if (di && !IS_ERR(di)) { |
| 4177 | btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key); |
| 4178 | if (key.objectid == root->root_key.objectid) { |
| 4179 | ret = -EPERM; |
| 4180 | btrfs_err(fs_info, |
| 4181 | "deleting default subvolume %llu is not allowed", |
| 4182 | key.objectid); |
| 4183 | goto out; |
| 4184 | } |
| 4185 | btrfs_release_path(path); |
| 4186 | } |
| 4187 | |
| 4188 | key.objectid = root->root_key.objectid; |
| 4189 | key.type = BTRFS_ROOT_REF_KEY; |
| 4190 | key.offset = (u64)-1; |
| 4191 | |
| 4192 | ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0); |
| 4193 | if (ret < 0) |
| 4194 | goto out; |
| 4195 | BUG_ON(ret == 0); |
| 4196 | |
| 4197 | ret = 0; |
| 4198 | if (path->slots[0] > 0) { |
| 4199 | path->slots[0]--; |
| 4200 | btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]); |
| 4201 | if (key.objectid == root->root_key.objectid && |
| 4202 | key.type == BTRFS_ROOT_REF_KEY) |
| 4203 | ret = -ENOTEMPTY; |
| 4204 | } |
| 4205 | out: |
| 4206 | btrfs_free_path(path); |
| 4207 | return ret; |
| 4208 | } |
| 4209 | |
Nikolay Borisov | 20a6800 | 2018-04-27 14:36:24 +0300 | [diff] [blame] | 4210 | /* Delete all dentries for inodes belonging to the root */ |
| 4211 | static void btrfs_prune_dentries(struct btrfs_root *root) |
| 4212 | { |
| 4213 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 4214 | struct rb_node *node; |
| 4215 | struct rb_node *prev; |
| 4216 | struct btrfs_inode *entry; |
| 4217 | struct inode *inode; |
| 4218 | u64 objectid = 0; |
| 4219 | |
| 4220 | if (!test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) |
| 4221 | WARN_ON(btrfs_root_refs(&root->root_item) != 0); |
| 4222 | |
| 4223 | spin_lock(&root->inode_lock); |
| 4224 | again: |
| 4225 | node = root->inode_tree.rb_node; |
| 4226 | prev = NULL; |
| 4227 | while (node) { |
| 4228 | prev = node; |
| 4229 | entry = rb_entry(node, struct btrfs_inode, rb_node); |
| 4230 | |
David Sterba | 3750851 | 2018-06-29 10:56:40 +0200 | [diff] [blame] | 4231 | if (objectid < btrfs_ino(entry)) |
Nikolay Borisov | 20a6800 | 2018-04-27 14:36:24 +0300 | [diff] [blame] | 4232 | node = node->rb_left; |
David Sterba | 3750851 | 2018-06-29 10:56:40 +0200 | [diff] [blame] | 4233 | else if (objectid > btrfs_ino(entry)) |
Nikolay Borisov | 20a6800 | 2018-04-27 14:36:24 +0300 | [diff] [blame] | 4234 | node = node->rb_right; |
| 4235 | else |
| 4236 | break; |
| 4237 | } |
| 4238 | if (!node) { |
| 4239 | while (prev) { |
| 4240 | entry = rb_entry(prev, struct btrfs_inode, rb_node); |
David Sterba | 3750851 | 2018-06-29 10:56:40 +0200 | [diff] [blame] | 4241 | if (objectid <= btrfs_ino(entry)) { |
Nikolay Borisov | 20a6800 | 2018-04-27 14:36:24 +0300 | [diff] [blame] | 4242 | node = prev; |
| 4243 | break; |
| 4244 | } |
| 4245 | prev = rb_next(prev); |
| 4246 | } |
| 4247 | } |
| 4248 | while (node) { |
| 4249 | entry = rb_entry(node, struct btrfs_inode, rb_node); |
David Sterba | 3750851 | 2018-06-29 10:56:40 +0200 | [diff] [blame] | 4250 | objectid = btrfs_ino(entry) + 1; |
Nikolay Borisov | 20a6800 | 2018-04-27 14:36:24 +0300 | [diff] [blame] | 4251 | inode = igrab(&entry->vfs_inode); |
| 4252 | if (inode) { |
| 4253 | spin_unlock(&root->inode_lock); |
| 4254 | if (atomic_read(&inode->i_count) > 1) |
| 4255 | d_prune_aliases(inode); |
| 4256 | /* |
| 4257 | * btrfs_drop_inode will have it removed from the inode |
| 4258 | * cache when its usage count hits zero. |
| 4259 | */ |
| 4260 | iput(inode); |
| 4261 | cond_resched(); |
| 4262 | spin_lock(&root->inode_lock); |
| 4263 | goto again; |
| 4264 | } |
| 4265 | |
| 4266 | if (cond_resched_lock(&root->inode_lock)) |
| 4267 | goto again; |
| 4268 | |
| 4269 | node = rb_next(node); |
| 4270 | } |
| 4271 | spin_unlock(&root->inode_lock); |
| 4272 | } |
| 4273 | |
Misono Tomohiro | f60a236 | 2018-04-18 11:34:52 +0900 | [diff] [blame] | 4274 | int btrfs_delete_subvolume(struct inode *dir, struct dentry *dentry) |
| 4275 | { |
| 4276 | struct btrfs_fs_info *fs_info = btrfs_sb(dentry->d_sb); |
| 4277 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 4278 | struct inode *inode = d_inode(dentry); |
| 4279 | struct btrfs_root *dest = BTRFS_I(inode)->root; |
| 4280 | struct btrfs_trans_handle *trans; |
| 4281 | struct btrfs_block_rsv block_rsv; |
| 4282 | u64 root_flags; |
Misono Tomohiro | f60a236 | 2018-04-18 11:34:52 +0900 | [diff] [blame] | 4283 | int ret; |
Misono Tomohiro | f60a236 | 2018-04-18 11:34:52 +0900 | [diff] [blame] | 4284 | |
| 4285 | /* |
| 4286 | * Don't allow to delete a subvolume with send in progress. This is |
| 4287 | * inside the inode lock so the error handling that has to drop the bit |
| 4288 | * again is not run concurrently. |
| 4289 | */ |
| 4290 | spin_lock(&dest->root_item_lock); |
Lu Fengqi | a7176f7 | 2018-08-04 21:10:53 +0800 | [diff] [blame] | 4291 | if (dest->send_in_progress) { |
Misono Tomohiro | f60a236 | 2018-04-18 11:34:52 +0900 | [diff] [blame] | 4292 | spin_unlock(&dest->root_item_lock); |
| 4293 | btrfs_warn(fs_info, |
| 4294 | "attempt to delete subvolume %llu during send", |
| 4295 | dest->root_key.objectid); |
| 4296 | return -EPERM; |
| 4297 | } |
Lu Fengqi | a7176f7 | 2018-08-04 21:10:53 +0800 | [diff] [blame] | 4298 | root_flags = btrfs_root_flags(&dest->root_item); |
| 4299 | btrfs_set_root_flags(&dest->root_item, |
| 4300 | root_flags | BTRFS_ROOT_SUBVOL_DEAD); |
| 4301 | spin_unlock(&dest->root_item_lock); |
Misono Tomohiro | f60a236 | 2018-04-18 11:34:52 +0900 | [diff] [blame] | 4302 | |
| 4303 | down_write(&fs_info->subvol_sem); |
| 4304 | |
Nikolay Borisov | ee0d904 | 2020-11-24 17:49:30 +0200 | [diff] [blame] | 4305 | ret = may_destroy_subvol(dest); |
| 4306 | if (ret) |
Misono Tomohiro | f60a236 | 2018-04-18 11:34:52 +0900 | [diff] [blame] | 4307 | goto out_up_write; |
| 4308 | |
| 4309 | btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP); |
| 4310 | /* |
| 4311 | * One for dir inode, |
| 4312 | * two for dir entries, |
| 4313 | * two for root ref/backref. |
| 4314 | */ |
Nikolay Borisov | ee0d904 | 2020-11-24 17:49:30 +0200 | [diff] [blame] | 4315 | ret = btrfs_subvolume_reserve_metadata(root, &block_rsv, 5, true); |
| 4316 | if (ret) |
Misono Tomohiro | f60a236 | 2018-04-18 11:34:52 +0900 | [diff] [blame] | 4317 | goto out_up_write; |
| 4318 | |
| 4319 | trans = btrfs_start_transaction(root, 0); |
| 4320 | if (IS_ERR(trans)) { |
Nikolay Borisov | ee0d904 | 2020-11-24 17:49:30 +0200 | [diff] [blame] | 4321 | ret = PTR_ERR(trans); |
Misono Tomohiro | f60a236 | 2018-04-18 11:34:52 +0900 | [diff] [blame] | 4322 | goto out_release; |
| 4323 | } |
| 4324 | trans->block_rsv = &block_rsv; |
| 4325 | trans->bytes_reserved = block_rsv.size; |
| 4326 | |
| 4327 | btrfs_record_snapshot_destroy(trans, BTRFS_I(dir)); |
| 4328 | |
Josef Bacik | 045d396 | 2019-12-18 17:20:27 -0500 | [diff] [blame] | 4329 | ret = btrfs_unlink_subvol(trans, dir, dentry); |
Misono Tomohiro | f60a236 | 2018-04-18 11:34:52 +0900 | [diff] [blame] | 4330 | if (ret) { |
Misono Tomohiro | f60a236 | 2018-04-18 11:34:52 +0900 | [diff] [blame] | 4331 | btrfs_abort_transaction(trans, ret); |
| 4332 | goto out_end_trans; |
| 4333 | } |
| 4334 | |
| 4335 | btrfs_record_root_in_trans(trans, dest); |
| 4336 | |
| 4337 | memset(&dest->root_item.drop_progress, 0, |
| 4338 | sizeof(dest->root_item.drop_progress)); |
David Sterba | c842268 | 2020-09-15 21:44:52 +0200 | [diff] [blame] | 4339 | btrfs_set_root_drop_level(&dest->root_item, 0); |
Misono Tomohiro | f60a236 | 2018-04-18 11:34:52 +0900 | [diff] [blame] | 4340 | btrfs_set_root_refs(&dest->root_item, 0); |
| 4341 | |
| 4342 | if (!test_and_set_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &dest->state)) { |
| 4343 | ret = btrfs_insert_orphan_item(trans, |
| 4344 | fs_info->tree_root, |
| 4345 | dest->root_key.objectid); |
| 4346 | if (ret) { |
| 4347 | btrfs_abort_transaction(trans, ret); |
Misono Tomohiro | f60a236 | 2018-04-18 11:34:52 +0900 | [diff] [blame] | 4348 | goto out_end_trans; |
| 4349 | } |
| 4350 | } |
| 4351 | |
Lu Fengqi | d195779 | 2018-05-29 15:01:54 +0800 | [diff] [blame] | 4352 | ret = btrfs_uuid_tree_remove(trans, dest->root_item.uuid, |
Misono Tomohiro | f60a236 | 2018-04-18 11:34:52 +0900 | [diff] [blame] | 4353 | BTRFS_UUID_KEY_SUBVOL, |
| 4354 | dest->root_key.objectid); |
| 4355 | if (ret && ret != -ENOENT) { |
| 4356 | btrfs_abort_transaction(trans, ret); |
Misono Tomohiro | f60a236 | 2018-04-18 11:34:52 +0900 | [diff] [blame] | 4357 | goto out_end_trans; |
| 4358 | } |
| 4359 | if (!btrfs_is_empty_uuid(dest->root_item.received_uuid)) { |
Lu Fengqi | d195779 | 2018-05-29 15:01:54 +0800 | [diff] [blame] | 4360 | ret = btrfs_uuid_tree_remove(trans, |
Misono Tomohiro | f60a236 | 2018-04-18 11:34:52 +0900 | [diff] [blame] | 4361 | dest->root_item.received_uuid, |
| 4362 | BTRFS_UUID_KEY_RECEIVED_SUBVOL, |
| 4363 | dest->root_key.objectid); |
| 4364 | if (ret && ret != -ENOENT) { |
| 4365 | btrfs_abort_transaction(trans, ret); |
Misono Tomohiro | f60a236 | 2018-04-18 11:34:52 +0900 | [diff] [blame] | 4366 | goto out_end_trans; |
| 4367 | } |
| 4368 | } |
| 4369 | |
Qu Wenruo | 082b6c9 | 2020-06-16 10:17:37 +0800 | [diff] [blame] | 4370 | free_anon_bdev(dest->anon_dev); |
| 4371 | dest->anon_dev = 0; |
Misono Tomohiro | f60a236 | 2018-04-18 11:34:52 +0900 | [diff] [blame] | 4372 | out_end_trans: |
| 4373 | trans->block_rsv = NULL; |
| 4374 | trans->bytes_reserved = 0; |
| 4375 | ret = btrfs_end_transaction(trans); |
Misono Tomohiro | f60a236 | 2018-04-18 11:34:52 +0900 | [diff] [blame] | 4376 | inode->i_flags |= S_DEAD; |
| 4377 | out_release: |
Qu Wenruo | e85fde5 | 2020-07-24 14:46:10 +0800 | [diff] [blame] | 4378 | btrfs_subvolume_release_metadata(root, &block_rsv); |
Misono Tomohiro | f60a236 | 2018-04-18 11:34:52 +0900 | [diff] [blame] | 4379 | out_up_write: |
| 4380 | up_write(&fs_info->subvol_sem); |
Nikolay Borisov | ee0d904 | 2020-11-24 17:49:30 +0200 | [diff] [blame] | 4381 | if (ret) { |
Misono Tomohiro | f60a236 | 2018-04-18 11:34:52 +0900 | [diff] [blame] | 4382 | spin_lock(&dest->root_item_lock); |
| 4383 | root_flags = btrfs_root_flags(&dest->root_item); |
| 4384 | btrfs_set_root_flags(&dest->root_item, |
| 4385 | root_flags & ~BTRFS_ROOT_SUBVOL_DEAD); |
| 4386 | spin_unlock(&dest->root_item_lock); |
| 4387 | } else { |
| 4388 | d_invalidate(dentry); |
Nikolay Borisov | 20a6800 | 2018-04-27 14:36:24 +0300 | [diff] [blame] | 4389 | btrfs_prune_dentries(dest); |
Misono Tomohiro | f60a236 | 2018-04-18 11:34:52 +0900 | [diff] [blame] | 4390 | ASSERT(dest->send_in_progress == 0); |
Misono Tomohiro | f60a236 | 2018-04-18 11:34:52 +0900 | [diff] [blame] | 4391 | } |
| 4392 | |
Nikolay Borisov | ee0d904 | 2020-11-24 17:49:30 +0200 | [diff] [blame] | 4393 | return ret; |
Misono Tomohiro | f60a236 | 2018-04-18 11:34:52 +0900 | [diff] [blame] | 4394 | } |
| 4395 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4396 | static int btrfs_rmdir(struct inode *dir, struct dentry *dentry) |
| 4397 | { |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 4398 | struct inode *inode = d_inode(dentry); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 4399 | int err = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4400 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4401 | struct btrfs_trans_handle *trans; |
Filipe Manana | 44f714d | 2016-06-06 16:11:13 +0100 | [diff] [blame] | 4402 | u64 last_unlink_trans; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4403 | |
David Sterba | b3ae244 | 2012-09-13 16:04:34 -0600 | [diff] [blame] | 4404 | if (inode->i_size > BTRFS_EMPTY_DIR_SIZE) |
Yan | 134d451 | 2007-10-25 15:49:25 -0400 | [diff] [blame] | 4405 | return -ENOTEMPTY; |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 4406 | if (btrfs_ino(BTRFS_I(inode)) == BTRFS_FIRST_FREE_OBJECTID) |
Misono Tomohiro | a79a464 | 2018-04-18 11:35:31 +0900 | [diff] [blame] | 4407 | return btrfs_delete_subvolume(dir, dentry); |
Yan | 134d451 | 2007-10-25 15:49:25 -0400 | [diff] [blame] | 4408 | |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 4409 | trans = __unlink_start_trans(dir); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4410 | if (IS_ERR(trans)) |
Josef Bacik | 5df6a9f | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4411 | return PTR_ERR(trans); |
Josef Bacik | 5df6a9f | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4412 | |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 4413 | if (unlikely(btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) { |
Josef Bacik | 045d396 | 2019-12-18 17:20:27 -0500 | [diff] [blame] | 4414 | err = btrfs_unlink_subvol(trans, dir, dentry); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4415 | goto out; |
| 4416 | } |
| 4417 | |
Nikolay Borisov | 73f2e54 | 2017-02-20 13:50:59 +0200 | [diff] [blame] | 4418 | err = btrfs_orphan_add(trans, BTRFS_I(inode)); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 4419 | if (err) |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4420 | goto out; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 4421 | |
Filipe Manana | 44f714d | 2016-06-06 16:11:13 +0100 | [diff] [blame] | 4422 | last_unlink_trans = BTRFS_I(inode)->last_unlink_trans; |
| 4423 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4424 | /* now the directory is empty */ |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 4425 | err = btrfs_unlink_inode(trans, root, BTRFS_I(dir), |
| 4426 | BTRFS_I(d_inode(dentry)), dentry->d_name.name, |
| 4427 | dentry->d_name.len); |
Filipe Manana | 44f714d | 2016-06-06 16:11:13 +0100 | [diff] [blame] | 4428 | if (!err) { |
Nikolay Borisov | 6ef06d2 | 2017-02-20 13:50:34 +0200 | [diff] [blame] | 4429 | btrfs_i_size_write(BTRFS_I(inode), 0); |
Filipe Manana | 44f714d | 2016-06-06 16:11:13 +0100 | [diff] [blame] | 4430 | /* |
| 4431 | * Propagate the last_unlink_trans value of the deleted dir to |
| 4432 | * its parent directory. This is to prevent an unrecoverable |
| 4433 | * log tree in the case we do something like this: |
| 4434 | * 1) create dir foo |
| 4435 | * 2) create snapshot under dir foo |
| 4436 | * 3) delete the snapshot |
| 4437 | * 4) rmdir foo |
| 4438 | * 5) mkdir foo |
| 4439 | * 6) fsync foo or some file inside foo |
| 4440 | */ |
| 4441 | if (last_unlink_trans >= trans->transid) |
| 4442 | BTRFS_I(dir)->last_unlink_trans = last_unlink_trans; |
| 4443 | } |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4444 | out: |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 4445 | btrfs_end_transaction(trans); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4446 | btrfs_btree_balance_dirty(root->fs_info); |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 4447 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4448 | return err; |
| 4449 | } |
| 4450 | |
Josef Bacik | ddfae63 | 2017-10-19 14:16:02 -0400 | [diff] [blame] | 4451 | /* |
| 4452 | * Return this if we need to call truncate_block for the last bit of the |
| 4453 | * truncate. |
| 4454 | */ |
| 4455 | #define NEED_TRUNCATE_BLOCK 1 |
Filipe Manana | 0305cd5 | 2015-10-16 12:34:25 +0100 | [diff] [blame] | 4456 | |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 4457 | /* |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4458 | * this can truncate away extent items, csum items and directory items. |
| 4459 | * It starts at a high offset and removes keys until it can't find |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 4460 | * any higher than new_size |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4461 | * |
| 4462 | * csum items that cross the new i_size are truncated to the new size |
| 4463 | * as well. |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 4464 | * |
| 4465 | * min_type is the minimum key type to truncate down to. If set to 0, this |
| 4466 | * will kill all the items on this inode, including the INODE_ITEM_KEY. |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4467 | */ |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4468 | int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans, |
| 4469 | struct btrfs_root *root, |
Nikolay Borisov | 5074339 | 2020-11-02 16:48:55 +0200 | [diff] [blame] | 4470 | struct btrfs_inode *inode, |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4471 | u64 new_size, u32 min_type) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4472 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4473 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4474 | struct btrfs_path *path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4475 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4476 | struct btrfs_file_extent_item *fi; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4477 | struct btrfs_key key; |
| 4478 | struct btrfs_key found_key; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4479 | u64 extent_start = 0; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4480 | u64 extent_num_bytes = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4481 | u64 extent_offset = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4482 | u64 item_end = 0; |
Filipe Manana | c1aa457 | 2015-06-20 18:20:09 +0100 | [diff] [blame] | 4483 | u64 last_size = new_size; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4484 | u32 found_type = (u8)-1; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4485 | int found_extent; |
| 4486 | int del_item; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4487 | int pending_del_nr = 0; |
| 4488 | int pending_del_slot = 0; |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4489 | int extent_type = -1; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4490 | int ret; |
Nikolay Borisov | 5074339 | 2020-11-02 16:48:55 +0200 | [diff] [blame] | 4491 | u64 ino = btrfs_ino(inode); |
Chris Mason | 28ed134 | 2014-12-17 09:41:04 -0800 | [diff] [blame] | 4492 | u64 bytes_deleted = 0; |
Thomas Meyer | 897ca81 | 2017-10-07 16:02:21 +0200 | [diff] [blame] | 4493 | bool be_nice = false; |
| 4494 | bool should_throttle = false; |
Filipe Manana | 28553fa | 2020-02-07 12:23:09 +0000 | [diff] [blame] | 4495 | const u64 lock_start = ALIGN_DOWN(new_size, fs_info->sectorsize); |
| 4496 | struct extent_state *cached_state = NULL; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4497 | |
| 4498 | BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4499 | |
Chris Mason | 28ed134 | 2014-12-17 09:41:04 -0800 | [diff] [blame] | 4500 | /* |
Qu Wenruo | 92a7cc4 | 2020-05-15 14:01:40 +0800 | [diff] [blame] | 4501 | * For non-free space inodes and non-shareable roots, we want to back |
| 4502 | * off from time to time. This means all inodes in subvolume roots, |
| 4503 | * reloc roots, and data reloc roots. |
Chris Mason | 28ed134 | 2014-12-17 09:41:04 -0800 | [diff] [blame] | 4504 | */ |
Nikolay Borisov | 5074339 | 2020-11-02 16:48:55 +0200 | [diff] [blame] | 4505 | if (!btrfs_is_free_space_inode(inode) && |
Qu Wenruo | 92a7cc4 | 2020-05-15 14:01:40 +0800 | [diff] [blame] | 4506 | test_bit(BTRFS_ROOT_SHAREABLE, &root->state)) |
Thomas Meyer | 897ca81 | 2017-10-07 16:02:21 +0200 | [diff] [blame] | 4507 | be_nice = true; |
Chris Mason | 28ed134 | 2014-12-17 09:41:04 -0800 | [diff] [blame] | 4508 | |
Mark Fasheh | 0eb0e19 | 2011-07-12 16:44:10 -0700 | [diff] [blame] | 4509 | path = btrfs_alloc_path(); |
| 4510 | if (!path) |
| 4511 | return -ENOMEM; |
David Sterba | e4058b5 | 2015-11-27 16:31:35 +0100 | [diff] [blame] | 4512 | path->reada = READA_BACK; |
Mark Fasheh | 0eb0e19 | 2011-07-12 16:44:10 -0700 | [diff] [blame] | 4513 | |
Qu Wenruo | 82028e0a | 2020-05-15 14:01:41 +0800 | [diff] [blame] | 4514 | if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) { |
Nikolay Borisov | 5074339 | 2020-11-02 16:48:55 +0200 | [diff] [blame] | 4515 | lock_extent_bits(&inode->io_tree, lock_start, (u64)-1, |
Filipe Manana | a5ae50d | 2020-02-20 13:29:49 +0000 | [diff] [blame] | 4516 | &cached_state); |
Filipe Manana | 28553fa | 2020-02-07 12:23:09 +0000 | [diff] [blame] | 4517 | |
Qu Wenruo | 82028e0a | 2020-05-15 14:01:41 +0800 | [diff] [blame] | 4518 | /* |
| 4519 | * We want to drop from the next block forward in case this |
| 4520 | * new size is not block aligned since we will be keeping the |
| 4521 | * last block of the extent just the way it is. |
| 4522 | */ |
Nikolay Borisov | 5074339 | 2020-11-02 16:48:55 +0200 | [diff] [blame] | 4523 | btrfs_drop_extent_cache(inode, ALIGN(new_size, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4524 | fs_info->sectorsize), |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 4525 | (u64)-1, 0); |
Qu Wenruo | 82028e0a | 2020-05-15 14:01:41 +0800 | [diff] [blame] | 4526 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4527 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 4528 | /* |
| 4529 | * This function is also used to drop the items in the log tree before |
| 4530 | * 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] | 4531 | * 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] | 4532 | * items. |
| 4533 | */ |
Nikolay Borisov | 5074339 | 2020-11-02 16:48:55 +0200 | [diff] [blame] | 4534 | if (min_type == 0 && root == inode->root) |
| 4535 | btrfs_kill_delayed_inode_items(inode); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 4536 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4537 | key.objectid = ino; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4538 | key.offset = (u64)-1; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4539 | key.type = (u8)-1; |
| 4540 | |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4541 | search_again: |
Chris Mason | 28ed134 | 2014-12-17 09:41:04 -0800 | [diff] [blame] | 4542 | /* |
| 4543 | * with a 16K leaf size and 128MB extents, you can actually queue |
| 4544 | * up a huge file in a single leaf. Most of the time that |
| 4545 | * bytes_deleted is > 0, it will be huge by the time we get here |
| 4546 | */ |
Omar Sandoval | fd86a3a | 2018-05-11 13:13:30 -0700 | [diff] [blame] | 4547 | if (be_nice && bytes_deleted > SZ_32M && |
| 4548 | btrfs_should_end_transaction(trans)) { |
| 4549 | ret = -EAGAIN; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4550 | goto out; |
| 4551 | } |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4552 | |
Omar Sandoval | fd86a3a | 2018-05-11 13:13:30 -0700 | [diff] [blame] | 4553 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 4554 | if (ret < 0) |
| 4555 | goto out; |
| 4556 | |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4557 | if (ret > 0) { |
Omar Sandoval | fd86a3a | 2018-05-11 13:13:30 -0700 | [diff] [blame] | 4558 | ret = 0; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4559 | /* there are no items in the tree for us to truncate, we're |
| 4560 | * done |
| 4561 | */ |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4562 | if (path->slots[0] == 0) |
| 4563 | goto out; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4564 | path->slots[0]--; |
| 4565 | } |
| 4566 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4567 | while (1) { |
Josef Bacik | 9ddc959 | 2020-01-17 09:02:22 -0500 | [diff] [blame] | 4568 | u64 clear_start = 0, clear_len = 0; |
| 4569 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4570 | fi = NULL; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4571 | leaf = path->nodes[0]; |
| 4572 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 4573 | found_type = found_key.type; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4574 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4575 | if (found_key.objectid != ino) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4576 | break; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4577 | |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4578 | if (found_type < min_type) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4579 | break; |
| 4580 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4581 | item_end = found_key.offset; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4582 | if (found_type == BTRFS_EXTENT_DATA_KEY) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4583 | fi = btrfs_item_ptr(leaf, path->slots[0], |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4584 | struct btrfs_file_extent_item); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4585 | extent_type = btrfs_file_extent_type(leaf, fi); |
| 4586 | if (extent_type != BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4587 | item_end += |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4588 | btrfs_file_extent_num_bytes(leaf, fi); |
Liu Bo | 09ed2f1 | 2017-03-10 11:09:48 -0800 | [diff] [blame] | 4589 | |
| 4590 | trace_btrfs_truncate_show_fi_regular( |
Nikolay Borisov | 5074339 | 2020-11-02 16:48:55 +0200 | [diff] [blame] | 4591 | inode, leaf, fi, found_key.offset); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4592 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
Qu Wenruo | e41ca58 | 2018-06-06 15:41:49 +0800 | [diff] [blame] | 4593 | item_end += btrfs_file_extent_ram_bytes(leaf, |
| 4594 | fi); |
Liu Bo | 09ed2f1 | 2017-03-10 11:09:48 -0800 | [diff] [blame] | 4595 | |
| 4596 | trace_btrfs_truncate_show_fi_inline( |
Nikolay Borisov | 5074339 | 2020-11-02 16:48:55 +0200 | [diff] [blame] | 4597 | inode, leaf, fi, path->slots[0], |
Liu Bo | 09ed2f1 | 2017-03-10 11:09:48 -0800 | [diff] [blame] | 4598 | found_key.offset); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4599 | } |
Yan | 008630c | 2007-11-07 13:31:09 -0500 | [diff] [blame] | 4600 | item_end--; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4601 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4602 | if (found_type > min_type) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4603 | del_item = 1; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4604 | } else { |
Filipe Manana | 76b42ab | 2017-02-14 16:56:01 +0000 | [diff] [blame] | 4605 | if (item_end < new_size) |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4606 | break; |
| 4607 | if (found_key.offset >= new_size) |
| 4608 | del_item = 1; |
| 4609 | else |
| 4610 | del_item = 0; |
| 4611 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4612 | found_extent = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4613 | /* FIXME, shrink the extent if the ref count is only 1 */ |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4614 | if (found_type != BTRFS_EXTENT_DATA_KEY) |
| 4615 | goto delete; |
| 4616 | |
| 4617 | if (extent_type != BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4618 | u64 num_dec; |
Josef Bacik | 9ddc959 | 2020-01-17 09:02:22 -0500 | [diff] [blame] | 4619 | |
| 4620 | clear_start = found_key.offset; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4621 | extent_start = btrfs_file_extent_disk_bytenr(leaf, fi); |
Miao Xie | f70a9a6b | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 4622 | if (!del_item) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4623 | u64 orig_num_bytes = |
| 4624 | btrfs_file_extent_num_bytes(leaf, fi); |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 4625 | extent_num_bytes = ALIGN(new_size - |
| 4626 | found_key.offset, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4627 | fs_info->sectorsize); |
Josef Bacik | 9ddc959 | 2020-01-17 09:02:22 -0500 | [diff] [blame] | 4628 | clear_start = ALIGN(new_size, fs_info->sectorsize); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4629 | btrfs_set_file_extent_num_bytes(leaf, fi, |
| 4630 | extent_num_bytes); |
| 4631 | num_dec = (orig_num_bytes - |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 4632 | extent_num_bytes); |
Qu Wenruo | 92a7cc4 | 2020-05-15 14:01:40 +0800 | [diff] [blame] | 4633 | if (test_bit(BTRFS_ROOT_SHAREABLE, |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 4634 | &root->state) && |
| 4635 | extent_start != 0) |
Nikolay Borisov | 5074339 | 2020-11-02 16:48:55 +0200 | [diff] [blame] | 4636 | inode_sub_bytes(&inode->vfs_inode, |
| 4637 | num_dec); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4638 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4639 | } else { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4640 | extent_num_bytes = |
| 4641 | btrfs_file_extent_disk_num_bytes(leaf, |
| 4642 | fi); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4643 | extent_offset = found_key.offset - |
| 4644 | btrfs_file_extent_offset(leaf, fi); |
| 4645 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4646 | /* FIXME blocksize != 4096 */ |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 4647 | num_dec = btrfs_file_extent_num_bytes(leaf, fi); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4648 | if (extent_start != 0) { |
| 4649 | found_extent = 1; |
Qu Wenruo | 92a7cc4 | 2020-05-15 14:01:40 +0800 | [diff] [blame] | 4650 | if (test_bit(BTRFS_ROOT_SHAREABLE, |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 4651 | &root->state)) |
Nikolay Borisov | 5074339 | 2020-11-02 16:48:55 +0200 | [diff] [blame] | 4652 | inode_sub_bytes(&inode->vfs_inode, |
| 4653 | num_dec); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4654 | } |
| 4655 | } |
Josef Bacik | 9ddc959 | 2020-01-17 09:02:22 -0500 | [diff] [blame] | 4656 | clear_len = num_dec; |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 4657 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4658 | /* |
| 4659 | * we can't truncate inline items that have had |
| 4660 | * special encodings |
| 4661 | */ |
| 4662 | if (!del_item && |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4663 | btrfs_file_extent_encryption(leaf, fi) == 0 && |
Josef Bacik | ddfae63 | 2017-10-19 14:16:02 -0400 | [diff] [blame] | 4664 | btrfs_file_extent_other_encoding(leaf, fi) == 0 && |
| 4665 | btrfs_file_extent_compression(leaf, fi) == 0) { |
| 4666 | u32 size = (u32)(new_size - found_key.offset); |
Chris Mason | 514ac8a | 2014-01-03 21:07:00 -0800 | [diff] [blame] | 4667 | |
Josef Bacik | ddfae63 | 2017-10-19 14:16:02 -0400 | [diff] [blame] | 4668 | btrfs_set_file_extent_ram_bytes(leaf, fi, size); |
| 4669 | size = btrfs_file_extent_calc_inline_size(size); |
David Sterba | 78ac4f9 | 2019-03-20 14:49:12 +0100 | [diff] [blame] | 4670 | btrfs_truncate_item(path, size, 1); |
Josef Bacik | ddfae63 | 2017-10-19 14:16:02 -0400 | [diff] [blame] | 4671 | } else if (!del_item) { |
Chris Mason | 514ac8a | 2014-01-03 21:07:00 -0800 | [diff] [blame] | 4672 | /* |
Josef Bacik | ddfae63 | 2017-10-19 14:16:02 -0400 | [diff] [blame] | 4673 | * We have to bail so the last_size is set to |
| 4674 | * just before this extent. |
Chris Mason | 514ac8a | 2014-01-03 21:07:00 -0800 | [diff] [blame] | 4675 | */ |
Omar Sandoval | fd86a3a | 2018-05-11 13:13:30 -0700 | [diff] [blame] | 4676 | ret = NEED_TRUNCATE_BLOCK; |
Josef Bacik | ddfae63 | 2017-10-19 14:16:02 -0400 | [diff] [blame] | 4677 | break; |
Josef Bacik | 9ddc959 | 2020-01-17 09:02:22 -0500 | [diff] [blame] | 4678 | } else { |
| 4679 | /* |
| 4680 | * Inline extents are special, we just treat |
| 4681 | * them as a full sector worth in the file |
| 4682 | * extent tree just for simplicity sake. |
| 4683 | */ |
| 4684 | clear_len = fs_info->sectorsize; |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 4685 | } |
Josef Bacik | ddfae63 | 2017-10-19 14:16:02 -0400 | [diff] [blame] | 4686 | |
Qu Wenruo | 92a7cc4 | 2020-05-15 14:01:40 +0800 | [diff] [blame] | 4687 | if (test_bit(BTRFS_ROOT_SHAREABLE, &root->state)) |
Nikolay Borisov | 5074339 | 2020-11-02 16:48:55 +0200 | [diff] [blame] | 4688 | inode_sub_bytes(&inode->vfs_inode, |
| 4689 | item_end + 1 - new_size); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4690 | } |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4691 | delete: |
Josef Bacik | 9ddc959 | 2020-01-17 09:02:22 -0500 | [diff] [blame] | 4692 | /* |
| 4693 | * We use btrfs_truncate_inode_items() to clean up log trees for |
| 4694 | * multiple fsyncs, and in this case we don't want to clear the |
| 4695 | * file extent range because it's just the log. |
| 4696 | */ |
Nikolay Borisov | 5074339 | 2020-11-02 16:48:55 +0200 | [diff] [blame] | 4697 | if (root == inode->root) { |
| 4698 | ret = btrfs_inode_clear_file_extent_range(inode, |
Josef Bacik | 9ddc959 | 2020-01-17 09:02:22 -0500 | [diff] [blame] | 4699 | clear_start, clear_len); |
| 4700 | if (ret) { |
| 4701 | btrfs_abort_transaction(trans, ret); |
| 4702 | break; |
| 4703 | } |
| 4704 | } |
| 4705 | |
Josef Bacik | ddfae63 | 2017-10-19 14:16:02 -0400 | [diff] [blame] | 4706 | if (del_item) |
| 4707 | last_size = found_key.offset; |
| 4708 | else |
| 4709 | last_size = new_size; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4710 | if (del_item) { |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4711 | if (!pending_del_nr) { |
| 4712 | /* no pending yet, add ourselves */ |
| 4713 | pending_del_slot = path->slots[0]; |
| 4714 | pending_del_nr = 1; |
| 4715 | } else if (pending_del_nr && |
| 4716 | path->slots[0] + 1 == pending_del_slot) { |
| 4717 | /* hop on the pending chunk */ |
| 4718 | pending_del_nr++; |
| 4719 | pending_del_slot = path->slots[0]; |
| 4720 | } else { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4721 | BUG(); |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4722 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4723 | } else { |
| 4724 | break; |
| 4725 | } |
Thomas Meyer | 897ca81 | 2017-10-07 16:02:21 +0200 | [diff] [blame] | 4726 | should_throttle = false; |
Chris Mason | 28f75a0 | 2015-02-04 06:59:29 -0800 | [diff] [blame] | 4727 | |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 4728 | if (found_extent && |
Qu Wenruo | 82028e0a | 2020-05-15 14:01:41 +0800 | [diff] [blame] | 4729 | root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) { |
Qu Wenruo | ffd4bb2 | 2019-04-04 14:45:36 +0800 | [diff] [blame] | 4730 | struct btrfs_ref ref = { 0 }; |
| 4731 | |
Chris Mason | 28ed134 | 2014-12-17 09:41:04 -0800 | [diff] [blame] | 4732 | bytes_deleted += extent_num_bytes; |
Qu Wenruo | ffd4bb2 | 2019-04-04 14:45:36 +0800 | [diff] [blame] | 4733 | |
| 4734 | btrfs_init_generic_ref(&ref, BTRFS_DROP_DELAYED_REF, |
| 4735 | extent_start, extent_num_bytes, 0); |
| 4736 | ref.real_root = root->root_key.objectid; |
| 4737 | btrfs_init_data_ref(&ref, btrfs_header_owner(leaf), |
| 4738 | ino, extent_offset); |
| 4739 | ret = btrfs_free_extent(trans, &ref); |
Omar Sandoval | 0552210 | 2018-05-11 13:13:31 -0700 | [diff] [blame] | 4740 | if (ret) { |
| 4741 | btrfs_abort_transaction(trans, ret); |
| 4742 | break; |
| 4743 | } |
Chris Mason | 28f75a0 | 2015-02-04 06:59:29 -0800 | [diff] [blame] | 4744 | if (be_nice) { |
Lu Fengqi | 7c86162 | 2018-10-11 13:40:36 +0800 | [diff] [blame] | 4745 | if (btrfs_should_throttle_delayed_refs(trans)) |
Thomas Meyer | 897ca81 | 2017-10-07 16:02:21 +0200 | [diff] [blame] | 4746 | should_throttle = true; |
Chris Mason | 28f75a0 | 2015-02-04 06:59:29 -0800 | [diff] [blame] | 4747 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4748 | } |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4749 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4750 | if (found_type == BTRFS_INODE_ITEM_KEY) |
| 4751 | break; |
| 4752 | |
| 4753 | if (path->slots[0] == 0 || |
Josef Bacik | 1262133 | 2015-02-03 07:50:16 -0800 | [diff] [blame] | 4754 | path->slots[0] != pending_del_slot || |
Josef Bacik | 28bad21 | 2018-12-03 10:20:38 -0500 | [diff] [blame] | 4755 | should_throttle) { |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4756 | if (pending_del_nr) { |
| 4757 | ret = btrfs_del_items(trans, root, path, |
| 4758 | pending_del_slot, |
| 4759 | pending_del_nr); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4760 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 4761 | btrfs_abort_transaction(trans, ret); |
Omar Sandoval | fd86a3a | 2018-05-11 13:13:30 -0700 | [diff] [blame] | 4762 | break; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4763 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4764 | pending_del_nr = 0; |
| 4765 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4766 | btrfs_release_path(path); |
Josef Bacik | 28bad21 | 2018-12-03 10:20:38 -0500 | [diff] [blame] | 4767 | |
Chris Mason | 28f75a0 | 2015-02-04 06:59:29 -0800 | [diff] [blame] | 4768 | /* |
Josef Bacik | 28bad21 | 2018-12-03 10:20:38 -0500 | [diff] [blame] | 4769 | * We can generate a lot of delayed refs, so we need to |
| 4770 | * throttle every once and a while and make sure we're |
| 4771 | * adding enough space to keep up with the work we are |
| 4772 | * generating. Since we hold a transaction here we |
| 4773 | * can't flush, and we don't want to FLUSH_LIMIT because |
| 4774 | * we could have generated too many delayed refs to |
| 4775 | * actually allocate, so just bail if we're short and |
| 4776 | * let the normal reservation dance happen higher up. |
Chris Mason | 28f75a0 | 2015-02-04 06:59:29 -0800 | [diff] [blame] | 4777 | */ |
Josef Bacik | 28bad21 | 2018-12-03 10:20:38 -0500 | [diff] [blame] | 4778 | if (should_throttle) { |
| 4779 | ret = btrfs_delayed_refs_rsv_refill(fs_info, |
| 4780 | BTRFS_RESERVE_NO_FLUSH); |
| 4781 | if (ret) { |
| 4782 | ret = -EAGAIN; |
| 4783 | break; |
| 4784 | } |
Chris Mason | 28f75a0 | 2015-02-04 06:59:29 -0800 | [diff] [blame] | 4785 | } |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4786 | goto search_again; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4787 | } else { |
| 4788 | path->slots[0]--; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4789 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4790 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4791 | out: |
Omar Sandoval | fd86a3a | 2018-05-11 13:13:30 -0700 | [diff] [blame] | 4792 | if (ret >= 0 && pending_del_nr) { |
| 4793 | int err; |
| 4794 | |
| 4795 | err = btrfs_del_items(trans, root, path, pending_del_slot, |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4796 | pending_del_nr); |
Omar Sandoval | fd86a3a | 2018-05-11 13:13:30 -0700 | [diff] [blame] | 4797 | if (err) { |
| 4798 | btrfs_abort_transaction(trans, err); |
| 4799 | ret = err; |
| 4800 | } |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4801 | } |
Filipe Manana | 76b42ab | 2017-02-14 16:56:01 +0000 | [diff] [blame] | 4802 | if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) { |
| 4803 | ASSERT(last_size >= new_size); |
Omar Sandoval | fd86a3a | 2018-05-11 13:13:30 -0700 | [diff] [blame] | 4804 | if (!ret && last_size > new_size) |
Filipe Manana | 76b42ab | 2017-02-14 16:56:01 +0000 | [diff] [blame] | 4805 | last_size = new_size; |
Nikolay Borisov | 5074339 | 2020-11-02 16:48:55 +0200 | [diff] [blame] | 4806 | btrfs_inode_safe_disk_i_size_write(inode, last_size); |
| 4807 | unlock_extent_cached(&inode->io_tree, lock_start, (u64)-1, |
| 4808 | &cached_state); |
Filipe Manana | 76b42ab | 2017-02-14 16:56:01 +0000 | [diff] [blame] | 4809 | } |
Chris Mason | 28ed134 | 2014-12-17 09:41:04 -0800 | [diff] [blame] | 4810 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4811 | btrfs_free_path(path); |
Omar Sandoval | fd86a3a | 2018-05-11 13:13:30 -0700 | [diff] [blame] | 4812 | return ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4813 | } |
| 4814 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4815 | /* |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4816 | * btrfs_truncate_block - read, zero a chunk and write a block |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 4817 | * @inode - inode that we're zeroing |
| 4818 | * @from - the offset to start zeroing |
| 4819 | * @len - the length to zero, 0 to zero the entire range respective to the |
| 4820 | * offset |
| 4821 | * @front - zero up to the offset instead of from the offset on |
| 4822 | * |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4823 | * 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] | 4824 | * 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] | 4825 | */ |
Nikolay Borisov | 217f42e | 2020-11-02 16:49:03 +0200 | [diff] [blame] | 4826 | int btrfs_truncate_block(struct btrfs_inode *inode, loff_t from, loff_t len, |
| 4827 | int front) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4828 | { |
Nikolay Borisov | 217f42e | 2020-11-02 16:49:03 +0200 | [diff] [blame] | 4829 | struct btrfs_fs_info *fs_info = inode->root->fs_info; |
| 4830 | struct address_space *mapping = inode->vfs_inode.i_mapping; |
| 4831 | struct extent_io_tree *io_tree = &inode->io_tree; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4832 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4833 | struct extent_state *cached_state = NULL; |
Qu Wenruo | 364ecf3 | 2017-02-27 15:10:38 +0800 | [diff] [blame] | 4834 | struct extent_changeset *data_reserved = NULL; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4835 | char *kaddr; |
Qu Wenruo | 6d4572a | 2020-06-24 07:23:50 +0800 | [diff] [blame] | 4836 | bool only_release_metadata = false; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4837 | u32 blocksize = fs_info->sectorsize; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 4838 | pgoff_t index = from >> PAGE_SHIFT; |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4839 | unsigned offset = from & (blocksize - 1); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4840 | struct page *page; |
Josef Bacik | 3b16a4e | 2011-09-21 15:05:58 -0400 | [diff] [blame] | 4841 | gfp_t mask = btrfs_alloc_write_mask(mapping); |
Qu Wenruo | 6d4572a | 2020-06-24 07:23:50 +0800 | [diff] [blame] | 4842 | size_t write_bytes = blocksize; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4843 | int ret = 0; |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4844 | u64 block_start; |
| 4845 | u64 block_end; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4846 | |
Nikolay Borisov | b03ebd9 | 2018-01-18 14:47:06 +0200 | [diff] [blame] | 4847 | if (IS_ALIGNED(offset, blocksize) && |
| 4848 | (!len || IS_ALIGNED(len, blocksize))) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4849 | goto out; |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4850 | |
Josef Bacik | 8b62f87 | 2017-10-19 14:15:55 -0400 | [diff] [blame] | 4851 | block_start = round_down(from, blocksize); |
| 4852 | block_end = block_start + blocksize - 1; |
| 4853 | |
Nikolay Borisov | 217f42e | 2020-11-02 16:49:03 +0200 | [diff] [blame] | 4854 | ret = btrfs_check_data_free_space(inode, &data_reserved, block_start, |
| 4855 | blocksize); |
Qu Wenruo | 6d4572a | 2020-06-24 07:23:50 +0800 | [diff] [blame] | 4856 | if (ret < 0) { |
Nikolay Borisov | 217f42e | 2020-11-02 16:49:03 +0200 | [diff] [blame] | 4857 | if (btrfs_check_nocow_lock(inode, block_start, &write_bytes) > 0) { |
Qu Wenruo | 6d4572a | 2020-06-24 07:23:50 +0800 | [diff] [blame] | 4858 | /* For nocow case, no need to reserve data space */ |
| 4859 | only_release_metadata = true; |
| 4860 | } else { |
| 4861 | goto out; |
| 4862 | } |
| 4863 | } |
Nikolay Borisov | 217f42e | 2020-11-02 16:49:03 +0200 | [diff] [blame] | 4864 | ret = btrfs_delalloc_reserve_metadata(inode, blocksize); |
Qu Wenruo | 6d4572a | 2020-06-24 07:23:50 +0800 | [diff] [blame] | 4865 | if (ret < 0) { |
| 4866 | if (!only_release_metadata) |
Nikolay Borisov | 217f42e | 2020-11-02 16:49:03 +0200 | [diff] [blame] | 4867 | btrfs_free_reserved_data_space(inode, data_reserved, |
| 4868 | block_start, blocksize); |
Qu Wenruo | 6d4572a | 2020-06-24 07:23:50 +0800 | [diff] [blame] | 4869 | goto out; |
| 4870 | } |
Chris Mason | 211c17f | 2008-05-15 09:13:45 -0400 | [diff] [blame] | 4871 | again: |
Josef Bacik | 3b16a4e | 2011-09-21 15:05:58 -0400 | [diff] [blame] | 4872 | page = find_or_create_page(mapping, index, mask); |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 4873 | if (!page) { |
Nikolay Borisov | 217f42e | 2020-11-02 16:49:03 +0200 | [diff] [blame] | 4874 | btrfs_delalloc_release_space(inode, data_reserved, block_start, |
| 4875 | blocksize, true); |
| 4876 | btrfs_delalloc_release_extents(inode, blocksize); |
Miao Xie | ac6a2b3 | 2012-12-05 10:56:13 +0000 | [diff] [blame] | 4877 | ret = -ENOMEM; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4878 | goto out; |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 4879 | } |
Qu Wenruo | 32443de | 2021-01-26 16:34:00 +0800 | [diff] [blame] | 4880 | ret = set_page_extent_mapped(page); |
| 4881 | if (ret < 0) |
| 4882 | goto out_unlock; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4883 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4884 | if (!PageUptodate(page)) { |
| 4885 | ret = btrfs_readpage(NULL, page); |
| 4886 | lock_page(page); |
Chris Mason | 211c17f | 2008-05-15 09:13:45 -0400 | [diff] [blame] | 4887 | if (page->mapping != mapping) { |
| 4888 | unlock_page(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 4889 | put_page(page); |
Chris Mason | 211c17f | 2008-05-15 09:13:45 -0400 | [diff] [blame] | 4890 | goto again; |
| 4891 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4892 | if (!PageUptodate(page)) { |
| 4893 | ret = -EIO; |
Chris Mason | 8964222 | 2008-07-24 09:41:53 -0400 | [diff] [blame] | 4894 | goto out_unlock; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4895 | } |
| 4896 | } |
Chris Mason | 211c17f | 2008-05-15 09:13:45 -0400 | [diff] [blame] | 4897 | wait_on_page_writeback(page); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4898 | |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4899 | lock_extent_bits(io_tree, block_start, block_end, &cached_state); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4900 | |
Nikolay Borisov | 217f42e | 2020-11-02 16:49:03 +0200 | [diff] [blame] | 4901 | ordered = btrfs_lookup_ordered_extent(inode, block_start); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4902 | if (ordered) { |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4903 | unlock_extent_cached(io_tree, block_start, block_end, |
David Sterba | e43bbe5 | 2017-12-12 21:43:52 +0100 | [diff] [blame] | 4904 | &cached_state); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4905 | unlock_page(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 4906 | put_page(page); |
Nikolay Borisov | c0a4360 | 2020-09-18 12:15:53 +0300 | [diff] [blame] | 4907 | btrfs_start_ordered_extent(ordered, 1); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4908 | btrfs_put_ordered_extent(ordered); |
| 4909 | goto again; |
| 4910 | } |
| 4911 | |
Nikolay Borisov | 217f42e | 2020-11-02 16:49:03 +0200 | [diff] [blame] | 4912 | clear_extent_bit(&inode->io_tree, block_start, block_end, |
Omar Sandoval | e182163 | 2019-08-15 14:04:04 -0700 | [diff] [blame] | 4913 | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, |
| 4914 | 0, 0, &cached_state); |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 4915 | |
Nikolay Borisov | 217f42e | 2020-11-02 16:49:03 +0200 | [diff] [blame] | 4916 | ret = btrfs_set_extent_delalloc(inode, block_start, block_end, 0, |
Nikolay Borisov | 330a582 | 2019-07-17 16:18:17 +0300 | [diff] [blame] | 4917 | &cached_state); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4918 | if (ret) { |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4919 | unlock_extent_cached(io_tree, block_start, block_end, |
David Sterba | e43bbe5 | 2017-12-12 21:43:52 +0100 | [diff] [blame] | 4920 | &cached_state); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4921 | goto out_unlock; |
| 4922 | } |
| 4923 | |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4924 | if (offset != blocksize) { |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 4925 | if (!len) |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4926 | len = blocksize - offset; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4927 | kaddr = kmap(page); |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 4928 | if (front) |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4929 | memset(kaddr + (block_start - page_offset(page)), |
| 4930 | 0, offset); |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 4931 | else |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4932 | memset(kaddr + (block_start - page_offset(page)) + offset, |
| 4933 | 0, len); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4934 | flush_dcache_page(page); |
| 4935 | kunmap(page); |
| 4936 | } |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 4937 | ClearPageChecked(page); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4938 | set_page_dirty(page); |
David Sterba | e43bbe5 | 2017-12-12 21:43:52 +0100 | [diff] [blame] | 4939 | unlock_extent_cached(io_tree, block_start, block_end, &cached_state); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4940 | |
Qu Wenruo | 6d4572a | 2020-06-24 07:23:50 +0800 | [diff] [blame] | 4941 | if (only_release_metadata) |
Nikolay Borisov | 217f42e | 2020-11-02 16:49:03 +0200 | [diff] [blame] | 4942 | set_extent_bit(&inode->io_tree, block_start, block_end, |
Nikolay Borisov | 1cab5e7 | 2020-11-05 11:08:00 +0200 | [diff] [blame] | 4943 | EXTENT_NORESERVE, 0, NULL, NULL, GFP_NOFS, NULL); |
Qu Wenruo | 6d4572a | 2020-06-24 07:23:50 +0800 | [diff] [blame] | 4944 | |
Chris Mason | 8964222 | 2008-07-24 09:41:53 -0400 | [diff] [blame] | 4945 | out_unlock: |
Qu Wenruo | 6d4572a | 2020-06-24 07:23:50 +0800 | [diff] [blame] | 4946 | if (ret) { |
| 4947 | if (only_release_metadata) |
Nikolay Borisov | 217f42e | 2020-11-02 16:49:03 +0200 | [diff] [blame] | 4948 | btrfs_delalloc_release_metadata(inode, blocksize, true); |
Qu Wenruo | 6d4572a | 2020-06-24 07:23:50 +0800 | [diff] [blame] | 4949 | else |
Nikolay Borisov | 217f42e | 2020-11-02 16:49:03 +0200 | [diff] [blame] | 4950 | btrfs_delalloc_release_space(inode, data_reserved, |
Qu Wenruo | 6d4572a | 2020-06-24 07:23:50 +0800 | [diff] [blame] | 4951 | block_start, blocksize, true); |
| 4952 | } |
Nikolay Borisov | 217f42e | 2020-11-02 16:49:03 +0200 | [diff] [blame] | 4953 | btrfs_delalloc_release_extents(inode, blocksize); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4954 | unlock_page(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 4955 | put_page(page); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4956 | out: |
Qu Wenruo | 6d4572a | 2020-06-24 07:23:50 +0800 | [diff] [blame] | 4957 | if (only_release_metadata) |
Nikolay Borisov | 217f42e | 2020-11-02 16:49:03 +0200 | [diff] [blame] | 4958 | btrfs_check_nocow_unlock(inode); |
Qu Wenruo | 364ecf3 | 2017-02-27 15:10:38 +0800 | [diff] [blame] | 4959 | extent_changeset_free(data_reserved); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4960 | return ret; |
| 4961 | } |
| 4962 | |
Nikolay Borisov | a4ba6cc | 2020-11-02 16:49:00 +0200 | [diff] [blame] | 4963 | 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] | 4964 | u64 offset, u64 len) |
| 4965 | { |
Nikolay Borisov | a4ba6cc | 2020-11-02 16:49:00 +0200 | [diff] [blame] | 4966 | struct btrfs_fs_info *fs_info = root->fs_info; |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4967 | struct btrfs_trans_handle *trans; |
Filipe Manana | 5893dfb | 2020-11-04 11:07:32 +0000 | [diff] [blame] | 4968 | struct btrfs_drop_extents_args drop_args = { 0 }; |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4969 | int ret; |
| 4970 | |
| 4971 | /* |
| 4972 | * Still need to make sure the inode looks like it's been updated so |
| 4973 | * that any holes get logged if we fsync. |
| 4974 | */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4975 | if (btrfs_fs_incompat(fs_info, NO_HOLES)) { |
Nikolay Borisov | a4ba6cc | 2020-11-02 16:49:00 +0200 | [diff] [blame] | 4976 | inode->last_trans = fs_info->generation; |
| 4977 | inode->last_sub_trans = root->log_transid; |
| 4978 | inode->last_log_commit = root->last_log_commit; |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4979 | return 0; |
| 4980 | } |
| 4981 | |
| 4982 | /* |
| 4983 | * 1 - for the one we're dropping |
| 4984 | * 1 - for the one we're adding |
| 4985 | * 1 - for updating the inode. |
| 4986 | */ |
| 4987 | trans = btrfs_start_transaction(root, 3); |
| 4988 | if (IS_ERR(trans)) |
| 4989 | return PTR_ERR(trans); |
| 4990 | |
Filipe Manana | 5893dfb | 2020-11-04 11:07:32 +0000 | [diff] [blame] | 4991 | drop_args.start = offset; |
| 4992 | drop_args.end = offset + len; |
| 4993 | drop_args.drop_cache = true; |
| 4994 | |
Nikolay Borisov | a4ba6cc | 2020-11-02 16:49:00 +0200 | [diff] [blame] | 4995 | ret = btrfs_drop_extents(trans, root, inode, &drop_args); |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4996 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 4997 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 4998 | btrfs_end_transaction(trans); |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4999 | return ret; |
| 5000 | } |
| 5001 | |
Nikolay Borisov | a4ba6cc | 2020-11-02 16:49:00 +0200 | [diff] [blame] | 5002 | ret = btrfs_insert_file_extent(trans, root, btrfs_ino(inode), |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 5003 | offset, 0, 0, len, 0, len, 0, 0, 0); |
Filipe Manana | 2766ff6 | 2020-11-04 11:07:34 +0000 | [diff] [blame] | 5004 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 5005 | btrfs_abort_transaction(trans, ret); |
Filipe Manana | 2766ff6 | 2020-11-04 11:07:34 +0000 | [diff] [blame] | 5006 | } else { |
Nikolay Borisov | a4ba6cc | 2020-11-02 16:49:00 +0200 | [diff] [blame] | 5007 | btrfs_update_inode_bytes(inode, 0, drop_args.bytes_found); |
| 5008 | btrfs_update_inode(trans, root, inode); |
Filipe Manana | 2766ff6 | 2020-11-04 11:07:34 +0000 | [diff] [blame] | 5009 | } |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 5010 | btrfs_end_transaction(trans); |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 5011 | return ret; |
| 5012 | } |
| 5013 | |
Josef Bacik | 695a0d0 | 2011-03-04 15:46:53 -0500 | [diff] [blame] | 5014 | /* |
| 5015 | * This function puts in dummy file extents for the area we're creating a hole |
| 5016 | * for. So if we are truncating this file to a larger size we need to insert |
| 5017 | * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for |
| 5018 | * the range between oldsize and size |
| 5019 | */ |
Nikolay Borisov | b06359a | 2020-11-02 16:49:04 +0200 | [diff] [blame] | 5020 | 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] | 5021 | { |
Nikolay Borisov | b06359a | 2020-11-02 16:49:04 +0200 | [diff] [blame] | 5022 | struct btrfs_root *root = inode->root; |
| 5023 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 5024 | struct extent_io_tree *io_tree = &inode->io_tree; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 5025 | struct extent_map *em = NULL; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 5026 | struct extent_state *cached_state = NULL; |
Nikolay Borisov | b06359a | 2020-11-02 16:49:04 +0200 | [diff] [blame] | 5027 | struct extent_map_tree *em_tree = &inode->extent_tree; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5028 | u64 hole_start = ALIGN(oldsize, fs_info->sectorsize); |
| 5029 | u64 block_end = ALIGN(size, fs_info->sectorsize); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 5030 | u64 last_byte; |
| 5031 | u64 cur_offset; |
| 5032 | u64 hole_size; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 5033 | int err = 0; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 5034 | |
Josef Bacik | a71754f | 2013-06-17 17:14:39 -0400 | [diff] [blame] | 5035 | /* |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 5036 | * If our size started in the middle of a block we need to zero out the |
| 5037 | * 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] | 5038 | * expose stale data. |
| 5039 | */ |
Nikolay Borisov | b06359a | 2020-11-02 16:49:04 +0200 | [diff] [blame] | 5040 | err = btrfs_truncate_block(inode, oldsize, 0, 0); |
Josef Bacik | a71754f | 2013-06-17 17:14:39 -0400 | [diff] [blame] | 5041 | if (err) |
| 5042 | return err; |
| 5043 | |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 5044 | if (size <= hole_start) |
| 5045 | return 0; |
| 5046 | |
Nikolay Borisov | b06359a | 2020-11-02 16:49:04 +0200 | [diff] [blame] | 5047 | btrfs_lock_and_flush_ordered_range(inode, hole_start, block_end - 1, |
| 5048 | &cached_state); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 5049 | cur_offset = hole_start; |
| 5050 | while (1) { |
Nikolay Borisov | b06359a | 2020-11-02 16:49:04 +0200 | [diff] [blame] | 5051 | em = btrfs_get_extent(inode, NULL, 0, cur_offset, |
Omar Sandoval | 39b07b5 | 2019-12-02 17:34:23 -0800 | [diff] [blame] | 5052 | block_end - cur_offset); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5053 | if (IS_ERR(em)) { |
| 5054 | err = PTR_ERR(em); |
Zach Brown | f276795 | 2013-01-08 19:37:58 +0000 | [diff] [blame] | 5055 | em = NULL; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5056 | break; |
| 5057 | } |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 5058 | last_byte = min(extent_map_end(em), block_end); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5059 | last_byte = ALIGN(last_byte, fs_info->sectorsize); |
Josef Bacik | 9ddc959 | 2020-01-17 09:02:22 -0500 | [diff] [blame] | 5060 | hole_size = last_byte - cur_offset; |
| 5061 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5062 | if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) { |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 5063 | struct extent_map *hole_em; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5064 | |
Nikolay Borisov | b06359a | 2020-11-02 16:49:04 +0200 | [diff] [blame] | 5065 | err = maybe_insert_hole(root, inode, cur_offset, |
| 5066 | hole_size); |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 5067 | if (err) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 5068 | break; |
Josef Bacik | 9ddc959 | 2020-01-17 09:02:22 -0500 | [diff] [blame] | 5069 | |
Nikolay Borisov | b06359a | 2020-11-02 16:49:04 +0200 | [diff] [blame] | 5070 | err = btrfs_inode_set_file_extent_range(inode, |
Josef Bacik | 9ddc959 | 2020-01-17 09:02:22 -0500 | [diff] [blame] | 5071 | cur_offset, hole_size); |
| 5072 | if (err) |
| 5073 | break; |
| 5074 | |
Nikolay Borisov | b06359a | 2020-11-02 16:49:04 +0200 | [diff] [blame] | 5075 | btrfs_drop_extent_cache(inode, cur_offset, |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 5076 | cur_offset + hole_size - 1, 0); |
| 5077 | hole_em = alloc_extent_map(); |
| 5078 | if (!hole_em) { |
| 5079 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, |
Nikolay Borisov | b06359a | 2020-11-02 16:49:04 +0200 | [diff] [blame] | 5080 | &inode->runtime_flags); |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 5081 | goto next; |
| 5082 | } |
| 5083 | hole_em->start = cur_offset; |
| 5084 | hole_em->len = hole_size; |
| 5085 | hole_em->orig_start = cur_offset; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5086 | |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 5087 | hole_em->block_start = EXTENT_MAP_HOLE; |
| 5088 | hole_em->block_len = 0; |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 5089 | hole_em->orig_block_len = 0; |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 5090 | hole_em->ram_bytes = hole_size; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 5091 | hole_em->compress_type = BTRFS_COMPRESS_NONE; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5092 | hole_em->generation = fs_info->generation; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 5093 | |
| 5094 | while (1) { |
| 5095 | write_lock(&em_tree->lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 5096 | err = add_extent_mapping(em_tree, hole_em, 1); |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 5097 | write_unlock(&em_tree->lock); |
| 5098 | if (err != -EEXIST) |
| 5099 | break; |
Nikolay Borisov | b06359a | 2020-11-02 16:49:04 +0200 | [diff] [blame] | 5100 | btrfs_drop_extent_cache(inode, cur_offset, |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 5101 | cur_offset + |
| 5102 | hole_size - 1, 0); |
| 5103 | } |
| 5104 | free_extent_map(hole_em); |
Josef Bacik | 9ddc959 | 2020-01-17 09:02:22 -0500 | [diff] [blame] | 5105 | } else { |
Nikolay Borisov | b06359a | 2020-11-02 16:49:04 +0200 | [diff] [blame] | 5106 | err = btrfs_inode_set_file_extent_range(inode, |
Josef Bacik | 9ddc959 | 2020-01-17 09:02:22 -0500 | [diff] [blame] | 5107 | cur_offset, hole_size); |
| 5108 | if (err) |
| 5109 | break; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 5110 | } |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 5111 | next: |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 5112 | free_extent_map(em); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 5113 | em = NULL; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 5114 | cur_offset = last_byte; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5115 | if (cur_offset >= block_end) |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 5116 | break; |
| 5117 | } |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 5118 | free_extent_map(em); |
David Sterba | e43bbe5 | 2017-12-12 21:43:52 +0100 | [diff] [blame] | 5119 | unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 5120 | return err; |
| 5121 | } |
| 5122 | |
Eric Sandeen | 3972f26 | 2013-01-12 02:57:22 +0000 | [diff] [blame] | 5123 | static int btrfs_setsize(struct inode *inode, struct iattr *attr) |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5124 | { |
Miao Xie | f4a2f4c | 2011-12-14 20:12:01 -0500 | [diff] [blame] | 5125 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5126 | struct btrfs_trans_handle *trans; |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 5127 | loff_t oldsize = i_size_read(inode); |
Eric Sandeen | 3972f26 | 2013-01-12 02:57:22 +0000 | [diff] [blame] | 5128 | loff_t newsize = attr->ia_size; |
| 5129 | int mask = attr->ia_valid; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5130 | int ret; |
| 5131 | |
Eric Sandeen | 3972f26 | 2013-01-12 02:57:22 +0000 | [diff] [blame] | 5132 | /* |
| 5133 | * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a |
| 5134 | * special case where we need to update the times despite not having |
| 5135 | * these flags set. For all other operations the VFS set these flags |
| 5136 | * explicitly if it wants a timestamp update. |
| 5137 | */ |
Christoph Hellwig | dff6efc | 2013-11-19 07:17:07 -0800 | [diff] [blame] | 5138 | if (newsize != oldsize) { |
| 5139 | inode_inc_iversion(inode); |
| 5140 | if (!(mask & (ATTR_CTIME | ATTR_MTIME))) |
| 5141 | inode->i_ctime = inode->i_mtime = |
Deepa Dinamani | c2050a4 | 2016-09-14 07:48:06 -0700 | [diff] [blame] | 5142 | current_time(inode); |
Christoph Hellwig | dff6efc | 2013-11-19 07:17:07 -0800 | [diff] [blame] | 5143 | } |
Eric Sandeen | 3972f26 | 2013-01-12 02:57:22 +0000 | [diff] [blame] | 5144 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 5145 | if (newsize > oldsize) { |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 5146 | /* |
David Sterba | ea14b57f | 2017-06-22 02:19:11 +0200 | [diff] [blame] | 5147 | * Don't do an expanding truncate while snapshotting is ongoing. |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 5148 | * This is to ensure the snapshot captures a fully consistent |
| 5149 | * state of this file - if the snapshot captures this expanding |
| 5150 | * truncation, it must capture all writes that happened before |
| 5151 | * this truncation. |
| 5152 | */ |
Nikolay Borisov | dcc3eb9 | 2020-01-30 14:59:45 +0200 | [diff] [blame] | 5153 | btrfs_drew_write_lock(&root->snapshot_lock); |
Nikolay Borisov | b06359a | 2020-11-02 16:49:04 +0200 | [diff] [blame] | 5154 | ret = btrfs_cont_expand(BTRFS_I(inode), oldsize, newsize); |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 5155 | if (ret) { |
Nikolay Borisov | dcc3eb9 | 2020-01-30 14:59:45 +0200 | [diff] [blame] | 5156 | btrfs_drew_write_unlock(&root->snapshot_lock); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5157 | return ret; |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 5158 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5159 | |
Miao Xie | f4a2f4c | 2011-12-14 20:12:01 -0500 | [diff] [blame] | 5160 | trans = btrfs_start_transaction(root, 1); |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 5161 | if (IS_ERR(trans)) { |
Nikolay Borisov | dcc3eb9 | 2020-01-30 14:59:45 +0200 | [diff] [blame] | 5162 | btrfs_drew_write_unlock(&root->snapshot_lock); |
Miao Xie | f4a2f4c | 2011-12-14 20:12:01 -0500 | [diff] [blame] | 5163 | return PTR_ERR(trans); |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 5164 | } |
Miao Xie | f4a2f4c | 2011-12-14 20:12:01 -0500 | [diff] [blame] | 5165 | |
| 5166 | i_size_write(inode, newsize); |
Nikolay Borisov | 76aea53 | 2020-11-02 16:48:53 +0200 | [diff] [blame] | 5167 | btrfs_inode_safe_disk_i_size_write(BTRFS_I(inode), 0); |
Chandan Rajendra | 27772b6 | 2016-01-21 15:56:03 +0530 | [diff] [blame] | 5168 | pagecache_isize_extended(inode, oldsize, newsize); |
Nikolay Borisov | 9a56fcd | 2020-11-02 16:48:59 +0200 | [diff] [blame] | 5169 | ret = btrfs_update_inode(trans, root, BTRFS_I(inode)); |
Nikolay Borisov | dcc3eb9 | 2020-01-30 14:59:45 +0200 | [diff] [blame] | 5170 | btrfs_drew_write_unlock(&root->snapshot_lock); |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 5171 | btrfs_end_transaction(trans); |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 5172 | } else { |
Naohiro Aota | 24c0a72 | 2021-02-04 19:22:09 +0900 | [diff] [blame] | 5173 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
| 5174 | |
| 5175 | if (btrfs_is_zoned(fs_info)) { |
| 5176 | ret = btrfs_wait_ordered_range(inode, |
| 5177 | ALIGN(newsize, fs_info->sectorsize), |
| 5178 | (u64)-1); |
| 5179 | if (ret) |
| 5180 | return ret; |
| 5181 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5182 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 5183 | /* |
| 5184 | * 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] | 5185 | * zero. Make sure any new writes to the file get on disk |
| 5186 | * on close. |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 5187 | */ |
| 5188 | if (newsize == 0) |
Nikolay Borisov | 1fd4033 | 2020-10-01 09:40:39 +0300 | [diff] [blame] | 5189 | set_bit(BTRFS_INODE_FLUSH_ON_CLOSE, |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 5190 | &BTRFS_I(inode)->runtime_flags); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5191 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 5192 | truncate_setsize(inode, newsize); |
Miao Xie | 2e60a51 | 2013-02-08 07:01:08 +0000 | [diff] [blame] | 5193 | |
Miao Xie | 2e60a51 | 2013-02-08 07:01:08 +0000 | [diff] [blame] | 5194 | inode_dio_wait(inode); |
Miao Xie | 2e60a51 | 2013-02-08 07:01:08 +0000 | [diff] [blame] | 5195 | |
Filipe Manana | 213e8c5 | 2018-02-06 20:40:31 +0000 | [diff] [blame] | 5196 | ret = btrfs_truncate(inode, newsize == oldsize); |
Josef Bacik | 7f4f6e0 | 2013-08-29 16:43:28 -0400 | [diff] [blame] | 5197 | if (ret && inode->i_nlink) { |
| 5198 | int err; |
| 5199 | |
| 5200 | /* |
Omar Sandoval | f7e9e8f | 2018-05-11 13:13:32 -0700 | [diff] [blame] | 5201 | * Truncate failed, so fix up the in-memory size. We |
| 5202 | * adjusted disk_i_size down as we removed extents, so |
| 5203 | * wait for disk_i_size to be stable and then update the |
| 5204 | * in-memory size to match. |
Josef Bacik | 7f4f6e0 | 2013-08-29 16:43:28 -0400 | [diff] [blame] | 5205 | */ |
Omar Sandoval | f7e9e8f | 2018-05-11 13:13:32 -0700 | [diff] [blame] | 5206 | err = btrfs_wait_ordered_range(inode, 0, (u64)-1); |
Josef Bacik | 7f4f6e0 | 2013-08-29 16:43:28 -0400 | [diff] [blame] | 5207 | if (err) |
Omar Sandoval | f7e9e8f | 2018-05-11 13:13:32 -0700 | [diff] [blame] | 5208 | return err; |
| 5209 | i_size_write(inode, BTRFS_I(inode)->disk_i_size); |
Josef Bacik | 7f4f6e0 | 2013-08-29 16:43:28 -0400 | [diff] [blame] | 5210 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5211 | } |
| 5212 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 5213 | return ret; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5214 | } |
| 5215 | |
Christian Brauner | 549c729 | 2021-01-21 14:19:43 +0100 | [diff] [blame] | 5216 | static int btrfs_setattr(struct user_namespace *mnt_userns, struct dentry *dentry, |
| 5217 | struct iattr *attr) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5218 | { |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 5219 | struct inode *inode = d_inode(dentry); |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 5220 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5221 | int err; |
| 5222 | |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 5223 | if (btrfs_root_readonly(root)) |
| 5224 | return -EROFS; |
| 5225 | |
Christian Brauner | 2f221d6 | 2021-01-21 14:19:26 +0100 | [diff] [blame] | 5226 | err = setattr_prepare(&init_user_ns, dentry, attr); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5227 | if (err) |
| 5228 | return err; |
| 5229 | |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 5230 | if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) { |
Eric Sandeen | 3972f26 | 2013-01-12 02:57:22 +0000 | [diff] [blame] | 5231 | err = btrfs_setsize(inode, attr); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5232 | if (err) |
| 5233 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5234 | } |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 5235 | |
Christoph Hellwig | 1025774 | 2010-06-04 11:30:02 +0200 | [diff] [blame] | 5236 | if (attr->ia_valid) { |
Christian Brauner | 2f221d6 | 2021-01-21 14:19:26 +0100 | [diff] [blame] | 5237 | setattr_copy(&init_user_ns, inode, attr); |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 5238 | inode_inc_iversion(inode); |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 5239 | err = btrfs_dirty_inode(inode); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 5240 | |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 5241 | if (!err && attr->ia_valid & ATTR_MODE) |
Christian Brauner | e65ce2a | 2021-01-21 14:19:27 +0100 | [diff] [blame] | 5242 | err = posix_acl_chmod(&init_user_ns, inode, |
| 5243 | inode->i_mode); |
Christoph Hellwig | 1025774 | 2010-06-04 11:30:02 +0200 | [diff] [blame] | 5244 | } |
| 5245 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5246 | return err; |
| 5247 | } |
Chris Mason | 61295eb | 2008-01-14 16:24:38 -0500 | [diff] [blame] | 5248 | |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5249 | /* |
| 5250 | * While truncating the inode pages during eviction, we get the VFS calling |
| 5251 | * btrfs_invalidatepage() against each page of the inode. This is slow because |
| 5252 | * the calls to btrfs_invalidatepage() result in a huge amount of calls to |
| 5253 | * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting |
| 5254 | * extent_state structures over and over, wasting lots of time. |
| 5255 | * |
| 5256 | * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all |
| 5257 | * those expensive operations on a per page basis and do only the ordered io |
| 5258 | * finishing, while we release here the extent_map and extent_state structures, |
| 5259 | * without the excessive merging and splitting. |
| 5260 | */ |
| 5261 | static void evict_inode_truncate_pages(struct inode *inode) |
| 5262 | { |
| 5263 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
| 5264 | struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree; |
| 5265 | struct rb_node *node; |
| 5266 | |
| 5267 | ASSERT(inode->i_state & I_FREEING); |
Johannes Weiner | 91b0abe | 2014-04-03 14:47:49 -0700 | [diff] [blame] | 5268 | truncate_inode_pages_final(&inode->i_data); |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5269 | |
| 5270 | write_lock(&map_tree->lock); |
Liu Bo | 07e1ce0 | 2018-08-23 03:51:52 +0800 | [diff] [blame] | 5271 | while (!RB_EMPTY_ROOT(&map_tree->map.rb_root)) { |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5272 | struct extent_map *em; |
| 5273 | |
Liu Bo | 07e1ce0 | 2018-08-23 03:51:52 +0800 | [diff] [blame] | 5274 | node = rb_first_cached(&map_tree->map); |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5275 | em = rb_entry(node, struct extent_map, rb_node); |
Wang Shilong | 180589e | 2013-12-14 15:27:31 +0800 | [diff] [blame] | 5276 | clear_bit(EXTENT_FLAG_PINNED, &em->flags); |
| 5277 | clear_bit(EXTENT_FLAG_LOGGING, &em->flags); |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5278 | remove_extent_mapping(map_tree, em); |
| 5279 | free_extent_map(em); |
Filipe Manana | 7064dd5 | 2014-08-08 02:47:05 +0100 | [diff] [blame] | 5280 | if (need_resched()) { |
| 5281 | write_unlock(&map_tree->lock); |
| 5282 | cond_resched(); |
| 5283 | write_lock(&map_tree->lock); |
| 5284 | } |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5285 | } |
| 5286 | write_unlock(&map_tree->lock); |
| 5287 | |
Filipe Manana | 6ca0709 | 2015-05-26 00:55:42 +0100 | [diff] [blame] | 5288 | /* |
| 5289 | * 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] | 5290 | * We can have ongoing bios started by readahead that have |
| 5291 | * their endio callback (extent_io.c:end_bio_extent_readpage) |
Filipe Manana | 9c6429d | 2015-06-10 12:55:41 +0100 | [diff] [blame] | 5292 | * still in progress (unlocked the pages in the bio but did not yet |
| 5293 | * unlocked the ranges in the io tree). Therefore this means some |
Filipe Manana | 6ca0709 | 2015-05-26 00:55:42 +0100 | [diff] [blame] | 5294 | * ranges can still be locked and eviction started because before |
| 5295 | * submitting those bios, which are executed by a separate task (work |
| 5296 | * queue kthread), inode references (inode->i_count) were not taken |
| 5297 | * (which would be dropped in the end io callback of each bio). |
| 5298 | * Therefore here we effectively end up waiting for those bios and |
| 5299 | * anyone else holding locked ranges without having bumped the inode's |
| 5300 | * reference count - if we don't do it, when they access the inode's |
| 5301 | * io_tree to unlock a range it may be too late, leading to an |
| 5302 | * use-after-free issue. |
| 5303 | */ |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5304 | spin_lock(&io_tree->lock); |
| 5305 | while (!RB_EMPTY_ROOT(&io_tree->state)) { |
| 5306 | struct extent_state *state; |
| 5307 | struct extent_state *cached_state = NULL; |
Filipe Manana | 6ca0709 | 2015-05-26 00:55:42 +0100 | [diff] [blame] | 5308 | u64 start; |
| 5309 | u64 end; |
Filipe Manana | 421f092 | 2018-10-12 13:02:48 +0100 | [diff] [blame] | 5310 | unsigned state_flags; |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5311 | |
| 5312 | node = rb_first(&io_tree->state); |
| 5313 | state = rb_entry(node, struct extent_state, rb_node); |
Filipe Manana | 6ca0709 | 2015-05-26 00:55:42 +0100 | [diff] [blame] | 5314 | start = state->start; |
| 5315 | end = state->end; |
Filipe Manana | 421f092 | 2018-10-12 13:02:48 +0100 | [diff] [blame] | 5316 | state_flags = state->state; |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5317 | spin_unlock(&io_tree->lock); |
| 5318 | |
David Sterba | ff13db4 | 2015-12-03 14:30:40 +0100 | [diff] [blame] | 5319 | lock_extent_bits(io_tree, start, end, &cached_state); |
Qu Wenruo | b9d0b38 | 2015-09-29 10:35:16 +0800 | [diff] [blame] | 5320 | |
| 5321 | /* |
| 5322 | * If still has DELALLOC flag, the extent didn't reach disk, |
| 5323 | * and its reserved space won't be freed by delayed_ref. |
| 5324 | * So we need to free its reserved space here. |
| 5325 | * (Refer to comment in btrfs_invalidatepage, case 2) |
| 5326 | * |
| 5327 | * Note, end is the bytenr of last byte, so we need + 1 here. |
| 5328 | */ |
Filipe Manana | 421f092 | 2018-10-12 13:02:48 +0100 | [diff] [blame] | 5329 | if (state_flags & EXTENT_DELALLOC) |
Nikolay Borisov | 8b8a979 | 2020-06-03 08:55:11 +0300 | [diff] [blame] | 5330 | btrfs_qgroup_free_data(BTRFS_I(inode), NULL, start, |
| 5331 | end - start + 1); |
Qu Wenruo | b9d0b38 | 2015-09-29 10:35:16 +0800 | [diff] [blame] | 5332 | |
Filipe Manana | 6ca0709 | 2015-05-26 00:55:42 +0100 | [diff] [blame] | 5333 | clear_extent_bit(io_tree, start, end, |
Omar Sandoval | e182163 | 2019-08-15 14:04:04 -0700 | [diff] [blame] | 5334 | EXTENT_LOCKED | EXTENT_DELALLOC | |
| 5335 | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1, |
| 5336 | &cached_state); |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5337 | |
Filipe Manana | 7064dd5 | 2014-08-08 02:47:05 +0100 | [diff] [blame] | 5338 | cond_resched(); |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5339 | spin_lock(&io_tree->lock); |
| 5340 | } |
| 5341 | spin_unlock(&io_tree->lock); |
| 5342 | } |
| 5343 | |
Omar Sandoval | 4b9d7b5 | 2018-05-11 13:13:36 -0700 | [diff] [blame] | 5344 | 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] | 5345 | struct btrfs_block_rsv *rsv) |
Omar Sandoval | 4b9d7b5 | 2018-05-11 13:13:36 -0700 | [diff] [blame] | 5346 | { |
| 5347 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 5348 | struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv; |
Josef Bacik | d3984c9 | 2019-08-01 18:19:37 -0400 | [diff] [blame] | 5349 | struct btrfs_trans_handle *trans; |
Josef Bacik | 2bd36e7 | 2019-08-22 15:14:33 -0400 | [diff] [blame] | 5350 | u64 delayed_refs_extra = btrfs_calc_insert_metadata_size(fs_info, 1); |
Josef Bacik | d3984c9 | 2019-08-01 18:19:37 -0400 | [diff] [blame] | 5351 | int ret; |
Omar Sandoval | 4b9d7b5 | 2018-05-11 13:13:36 -0700 | [diff] [blame] | 5352 | |
Josef Bacik | d3984c9 | 2019-08-01 18:19:37 -0400 | [diff] [blame] | 5353 | /* |
| 5354 | * Eviction should be taking place at some place safe because of our |
| 5355 | * delayed iputs. However the normal flushing code will run delayed |
| 5356 | * iputs, so we cannot use FLUSH_ALL otherwise we'll deadlock. |
| 5357 | * |
| 5358 | * We reserve the delayed_refs_extra here again because we can't use |
| 5359 | * btrfs_start_transaction(root, 0) for the same deadlocky reason as |
| 5360 | * above. We reserve our extra bit here because we generate a ton of |
| 5361 | * delayed refs activity by truncating. |
| 5362 | * |
| 5363 | * If we cannot make our reservation we'll attempt to steal from the |
| 5364 | * global reserve, because we really want to be able to free up space. |
| 5365 | */ |
| 5366 | ret = btrfs_block_rsv_refill(root, rsv, rsv->size + delayed_refs_extra, |
| 5367 | BTRFS_RESERVE_FLUSH_EVICT); |
| 5368 | if (ret) { |
Omar Sandoval | 4b9d7b5 | 2018-05-11 13:13:36 -0700 | [diff] [blame] | 5369 | /* |
| 5370 | * Try to steal from the global reserve if there is space for |
| 5371 | * it. |
| 5372 | */ |
Josef Bacik | d3984c9 | 2019-08-01 18:19:37 -0400 | [diff] [blame] | 5373 | if (btrfs_check_space_for_delayed_refs(fs_info) || |
| 5374 | btrfs_block_rsv_migrate(global_rsv, rsv, rsv->size, 0)) { |
| 5375 | btrfs_warn(fs_info, |
| 5376 | "could not allocate space for delete; will truncate on mount"); |
| 5377 | return ERR_PTR(-ENOSPC); |
| 5378 | } |
| 5379 | delayed_refs_extra = 0; |
Omar Sandoval | 4b9d7b5 | 2018-05-11 13:13:36 -0700 | [diff] [blame] | 5380 | } |
Josef Bacik | d3984c9 | 2019-08-01 18:19:37 -0400 | [diff] [blame] | 5381 | |
| 5382 | trans = btrfs_join_transaction(root); |
| 5383 | if (IS_ERR(trans)) |
| 5384 | return trans; |
| 5385 | |
| 5386 | if (delayed_refs_extra) { |
| 5387 | trans->block_rsv = &fs_info->trans_block_rsv; |
| 5388 | trans->bytes_reserved = delayed_refs_extra; |
| 5389 | btrfs_block_rsv_migrate(rsv, trans->block_rsv, |
| 5390 | delayed_refs_extra, 1); |
| 5391 | } |
| 5392 | return trans; |
Omar Sandoval | 4b9d7b5 | 2018-05-11 13:13:36 -0700 | [diff] [blame] | 5393 | } |
| 5394 | |
Al Viro | bd55597 | 2010-06-07 11:35:40 -0400 | [diff] [blame] | 5395 | void btrfs_evict_inode(struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5396 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5397 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5398 | struct btrfs_trans_handle *trans; |
| 5399 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Omar Sandoval | 4b9d7b5 | 2018-05-11 13:13:36 -0700 | [diff] [blame] | 5400 | struct btrfs_block_rsv *rsv; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5401 | int ret; |
| 5402 | |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 5403 | trace_btrfs_inode_evict(inode); |
| 5404 | |
Nikolay Borisov | 3d48d98 | 2016-06-29 09:46:41 +0300 | [diff] [blame] | 5405 | if (!root) { |
Liu Bo | e8f1bc1 | 2018-01-25 11:02:53 -0700 | [diff] [blame] | 5406 | clear_inode(inode); |
Nikolay Borisov | 3d48d98 | 2016-06-29 09:46:41 +0300 | [diff] [blame] | 5407 | return; |
| 5408 | } |
| 5409 | |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5410 | evict_inode_truncate_pages(inode); |
| 5411 | |
Stefan Behrens | 69e9c6c | 2013-09-05 16:58:43 +0200 | [diff] [blame] | 5412 | if (inode->i_nlink && |
| 5413 | ((btrfs_root_refs(&root->root_item) != 0 && |
| 5414 | root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) || |
Nikolay Borisov | 70ddc55 | 2017-02-20 13:50:35 +0200 | [diff] [blame] | 5415 | btrfs_is_free_space_inode(BTRFS_I(inode)))) |
Al Viro | bd55597 | 2010-06-07 11:35:40 -0400 | [diff] [blame] | 5416 | goto no_delete; |
| 5417 | |
Omar Sandoval | 2791906 | 2018-05-11 13:13:37 -0700 | [diff] [blame] | 5418 | if (is_bad_inode(inode)) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5419 | goto no_delete; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5420 | |
Nikolay Borisov | 7ab7956 | 2017-02-20 13:50:57 +0200 | [diff] [blame] | 5421 | btrfs_free_io_failure_record(BTRFS_I(inode), 0, (u64)-1); |
Miao Xie | f612496 | 2014-09-12 18:44:04 +0800 | [diff] [blame] | 5422 | |
Omar Sandoval | 7b40b69 | 2018-05-11 13:13:33 -0700 | [diff] [blame] | 5423 | if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 5424 | goto no_delete; |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 5425 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5426 | if (inode->i_nlink > 0) { |
Stefan Behrens | 69e9c6c | 2013-09-05 16:58:43 +0200 | [diff] [blame] | 5427 | BUG_ON(btrfs_root_refs(&root->root_item) != 0 && |
| 5428 | root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5429 | goto no_delete; |
| 5430 | } |
| 5431 | |
Nikolay Borisov | aa79021 | 2017-01-10 20:35:40 +0200 | [diff] [blame] | 5432 | ret = btrfs_commit_inode_delayed_inode(BTRFS_I(inode)); |
Omar Sandoval | 2791906 | 2018-05-11 13:13:37 -0700 | [diff] [blame] | 5433 | if (ret) |
Miao Xie | 0e8c36a | 2012-12-19 06:59:51 +0000 | [diff] [blame] | 5434 | goto no_delete; |
Miao Xie | 0e8c36a | 2012-12-19 06:59:51 +0000 | [diff] [blame] | 5435 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5436 | rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP); |
Omar Sandoval | 2791906 | 2018-05-11 13:13:37 -0700 | [diff] [blame] | 5437 | if (!rsv) |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 5438 | goto no_delete; |
Josef Bacik | 2bd36e7 | 2019-08-22 15:14:33 -0400 | [diff] [blame] | 5439 | rsv->size = btrfs_calc_metadata_size(fs_info, 1); |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 5440 | rsv->failfast = 1; |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 5441 | |
Nikolay Borisov | 6ef06d2 | 2017-02-20 13:50:34 +0200 | [diff] [blame] | 5442 | btrfs_i_size_write(BTRFS_I(inode), 0); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5443 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5444 | while (1) { |
Josef Bacik | ad80cf5 | 2018-09-28 07:18:19 -0400 | [diff] [blame] | 5445 | trans = evict_refill_and_join(root, rsv); |
Omar Sandoval | 2791906 | 2018-05-11 13:13:37 -0700 | [diff] [blame] | 5446 | if (IS_ERR(trans)) |
| 5447 | goto free_rsv; |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 5448 | |
| 5449 | trans->block_rsv = rsv; |
| 5450 | |
Nikolay Borisov | 5074339 | 2020-11-02 16:48:55 +0200 | [diff] [blame] | 5451 | ret = btrfs_truncate_inode_items(trans, root, BTRFS_I(inode), |
| 5452 | 0, 0); |
Omar Sandoval | 2791906 | 2018-05-11 13:13:37 -0700 | [diff] [blame] | 5453 | trans->block_rsv = &fs_info->trans_block_rsv; |
| 5454 | btrfs_end_transaction(trans); |
| 5455 | btrfs_btree_balance_dirty(fs_info); |
| 5456 | if (ret && ret != -ENOSPC && ret != -EAGAIN) |
| 5457 | goto free_rsv; |
| 5458 | else if (!ret) |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5459 | break; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 5460 | } |
| 5461 | |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 5462 | /* |
Omar Sandoval | 2791906 | 2018-05-11 13:13:37 -0700 | [diff] [blame] | 5463 | * Errors here aren't a big deal, it just means we leave orphan items in |
| 5464 | * the tree. They will be cleaned up on the next mount. If the inode |
| 5465 | * number gets reused, cleanup deletes the orphan item without doing |
| 5466 | * anything, and unlink reuses the existing orphan item. |
| 5467 | * |
| 5468 | * If it turns out that we are dropping too many of these, we might want |
| 5469 | * to add a mechanism for retrying these after a commit. |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 5470 | */ |
Josef Bacik | ad80cf5 | 2018-09-28 07:18:19 -0400 | [diff] [blame] | 5471 | trans = evict_refill_and_join(root, rsv); |
Omar Sandoval | 2791906 | 2018-05-11 13:13:37 -0700 | [diff] [blame] | 5472 | if (!IS_ERR(trans)) { |
| 5473 | trans->block_rsv = rsv; |
| 5474 | btrfs_orphan_del(trans, BTRFS_I(inode)); |
| 5475 | trans->block_rsv = &fs_info->trans_block_rsv; |
| 5476 | btrfs_end_transaction(trans); |
| 5477 | } |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 5478 | |
Omar Sandoval | 2791906 | 2018-05-11 13:13:37 -0700 | [diff] [blame] | 5479 | free_rsv: |
| 5480 | btrfs_free_block_rsv(fs_info, rsv); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5481 | no_delete: |
Omar Sandoval | 2791906 | 2018-05-11 13:13:37 -0700 | [diff] [blame] | 5482 | /* |
| 5483 | * If we didn't successfully delete, the orphan item will still be in |
| 5484 | * the tree and we'll retry on the next mount. Again, we might also want |
| 5485 | * to retry these periodically in the future. |
| 5486 | */ |
Nikolay Borisov | f48d1cf | 2017-01-10 20:35:39 +0200 | [diff] [blame] | 5487 | btrfs_remove_delayed_node(BTRFS_I(inode)); |
Jan Kara | dbd5768 | 2012-05-03 14:48:02 +0200 | [diff] [blame] | 5488 | clear_inode(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5489 | } |
| 5490 | |
| 5491 | /* |
Qu Wenruo | 6bf9e4b | 2019-03-13 13:55:11 +0800 | [diff] [blame] | 5492 | * Return the key found in the dir entry in the location pointer, fill @type |
| 5493 | * with BTRFS_FT_*, and return 0. |
| 5494 | * |
Su Yue | 005d671 | 2018-03-05 17:13:37 +0800 | [diff] [blame] | 5495 | * If no dir entries were found, returns -ENOENT. |
| 5496 | * If found a corrupted location in dir entry, returns -EUCLEAN. |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5497 | */ |
| 5498 | static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry, |
Qu Wenruo | 6bf9e4b | 2019-03-13 13:55:11 +0800 | [diff] [blame] | 5499 | struct btrfs_key *location, u8 *type) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5500 | { |
| 5501 | const char *name = dentry->d_name.name; |
| 5502 | int namelen = dentry->d_name.len; |
| 5503 | struct btrfs_dir_item *di; |
| 5504 | struct btrfs_path *path; |
| 5505 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Yan | 0d9f7f3 | 2007-10-25 15:48:28 -0400 | [diff] [blame] | 5506 | int ret = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5507 | |
| 5508 | path = btrfs_alloc_path(); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 5509 | if (!path) |
| 5510 | return -ENOMEM; |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 5511 | |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 5512 | di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(BTRFS_I(dir)), |
| 5513 | name, namelen, 0); |
Liu Bo | 3cf5068 | 2018-09-12 06:06:26 +0800 | [diff] [blame] | 5514 | if (IS_ERR_OR_NULL(di)) { |
| 5515 | ret = di ? PTR_ERR(di) : -ENOENT; |
Su Yue | 005d671 | 2018-03-05 17:13:37 +0800 | [diff] [blame] | 5516 | goto out; |
| 5517 | } |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5518 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5519 | btrfs_dir_item_key_to_cpu(path->nodes[0], di, location); |
Liu Bo | 56a0e70 | 2017-10-30 11:14:38 -0600 | [diff] [blame] | 5520 | if (location->type != BTRFS_INODE_ITEM_KEY && |
| 5521 | location->type != BTRFS_ROOT_ITEM_KEY) { |
Su Yue | 005d671 | 2018-03-05 17:13:37 +0800 | [diff] [blame] | 5522 | ret = -EUCLEAN; |
Liu Bo | 56a0e70 | 2017-10-30 11:14:38 -0600 | [diff] [blame] | 5523 | btrfs_warn(root->fs_info, |
| 5524 | "%s gets something invalid in DIR_ITEM (name %s, directory ino %llu, location(%llu %u %llu))", |
| 5525 | __func__, name, btrfs_ino(BTRFS_I(dir)), |
| 5526 | location->objectid, location->type, location->offset); |
Liu Bo | 56a0e70 | 2017-10-30 11:14:38 -0600 | [diff] [blame] | 5527 | } |
Qu Wenruo | 6bf9e4b | 2019-03-13 13:55:11 +0800 | [diff] [blame] | 5528 | if (!ret) |
| 5529 | *type = btrfs_dir_type(path->nodes[0], di); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5530 | out: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5531 | btrfs_free_path(path); |
| 5532 | return ret; |
| 5533 | } |
| 5534 | |
| 5535 | /* |
| 5536 | * when we hit a tree root in a directory, the btrfs part of the inode |
| 5537 | * needs to be changed to reflect the root directory of the tree root. This |
| 5538 | * is kind of like crossing a mount point. |
| 5539 | */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5540 | static int fixup_tree_root_location(struct btrfs_fs_info *fs_info, |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5541 | struct inode *dir, |
| 5542 | struct dentry *dentry, |
| 5543 | struct btrfs_key *location, |
| 5544 | struct btrfs_root **sub_root) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5545 | { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5546 | struct btrfs_path *path; |
| 5547 | struct btrfs_root *new_root; |
| 5548 | struct btrfs_root_ref *ref; |
| 5549 | struct extent_buffer *leaf; |
David Sterba | 1d4c08e | 2015-01-02 19:36:14 +0100 | [diff] [blame] | 5550 | struct btrfs_key key; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5551 | int ret; |
| 5552 | int err = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5553 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5554 | path = btrfs_alloc_path(); |
| 5555 | if (!path) { |
| 5556 | err = -ENOMEM; |
| 5557 | goto out; |
| 5558 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5559 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5560 | err = -ENOENT; |
David Sterba | 1d4c08e | 2015-01-02 19:36:14 +0100 | [diff] [blame] | 5561 | key.objectid = BTRFS_I(dir)->root->root_key.objectid; |
| 5562 | key.type = BTRFS_ROOT_REF_KEY; |
| 5563 | key.offset = location->objectid; |
| 5564 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5565 | 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] | 5566 | if (ret) { |
| 5567 | if (ret < 0) |
| 5568 | err = ret; |
| 5569 | goto out; |
| 5570 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5571 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5572 | leaf = path->nodes[0]; |
| 5573 | ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref); |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 5574 | if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(BTRFS_I(dir)) || |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5575 | btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len) |
| 5576 | goto out; |
| 5577 | |
| 5578 | ret = memcmp_extent_buffer(leaf, dentry->d_name.name, |
| 5579 | (unsigned long)(ref + 1), |
| 5580 | dentry->d_name.len); |
| 5581 | if (ret) |
| 5582 | goto out; |
| 5583 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 5584 | btrfs_release_path(path); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5585 | |
David Sterba | 56e9357 | 2020-05-15 19:35:55 +0200 | [diff] [blame] | 5586 | new_root = btrfs_get_fs_root(fs_info, location->objectid, true); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5587 | if (IS_ERR(new_root)) { |
| 5588 | err = PTR_ERR(new_root); |
| 5589 | goto out; |
| 5590 | } |
| 5591 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5592 | *sub_root = new_root; |
| 5593 | location->objectid = btrfs_root_dirid(&new_root->root_item); |
| 5594 | location->type = BTRFS_INODE_ITEM_KEY; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5595 | location->offset = 0; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5596 | err = 0; |
| 5597 | out: |
| 5598 | btrfs_free_path(path); |
| 5599 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5600 | } |
| 5601 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5602 | static void inode_tree_add(struct inode *inode) |
| 5603 | { |
| 5604 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5605 | struct btrfs_inode *entry; |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 5606 | struct rb_node **p; |
| 5607 | struct rb_node *parent; |
Filipe David Borba Manana | cef2193 | 2013-09-02 12:19:13 +0100 | [diff] [blame] | 5608 | struct rb_node *new = &BTRFS_I(inode)->rb_node; |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 5609 | u64 ino = btrfs_ino(BTRFS_I(inode)); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5610 | |
Al Viro | 1d3382cb | 2010-10-23 15:19:20 -0400 | [diff] [blame] | 5611 | if (inode_unhashed(inode)) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5612 | return; |
Miao Xie | e1409ce | 2013-05-15 07:48:16 +0000 | [diff] [blame] | 5613 | parent = NULL; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5614 | spin_lock(&root->inode_lock); |
Miao Xie | e1409ce | 2013-05-15 07:48:16 +0000 | [diff] [blame] | 5615 | p = &root->inode_tree.rb_node; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5616 | while (*p) { |
| 5617 | parent = *p; |
| 5618 | entry = rb_entry(parent, struct btrfs_inode, rb_node); |
| 5619 | |
David Sterba | 3750851 | 2018-06-29 10:56:40 +0200 | [diff] [blame] | 5620 | if (ino < btrfs_ino(entry)) |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 5621 | p = &parent->rb_left; |
David Sterba | 3750851 | 2018-06-29 10:56:40 +0200 | [diff] [blame] | 5622 | else if (ino > btrfs_ino(entry)) |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 5623 | p = &parent->rb_right; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5624 | else { |
| 5625 | WARN_ON(!(entry->vfs_inode.i_state & |
Al Viro | a4ffdde | 2010-06-02 17:38:30 -0400 | [diff] [blame] | 5626 | (I_WILL_FREE | I_FREEING))); |
Filipe David Borba Manana | cef2193 | 2013-09-02 12:19:13 +0100 | [diff] [blame] | 5627 | rb_replace_node(parent, new, &root->inode_tree); |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 5628 | RB_CLEAR_NODE(parent); |
| 5629 | spin_unlock(&root->inode_lock); |
Filipe David Borba Manana | cef2193 | 2013-09-02 12:19:13 +0100 | [diff] [blame] | 5630 | return; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5631 | } |
| 5632 | } |
Filipe David Borba Manana | cef2193 | 2013-09-02 12:19:13 +0100 | [diff] [blame] | 5633 | rb_link_node(new, parent, p); |
| 5634 | rb_insert_color(new, &root->inode_tree); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5635 | spin_unlock(&root->inode_lock); |
| 5636 | } |
| 5637 | |
Nikolay Borisov | b79b724 | 2020-08-31 14:42:38 +0300 | [diff] [blame] | 5638 | static void inode_tree_del(struct btrfs_inode *inode) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5639 | { |
Nikolay Borisov | b79b724 | 2020-08-31 14:42:38 +0300 | [diff] [blame] | 5640 | struct btrfs_root *root = inode->root; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5641 | int empty = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5642 | |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 5643 | spin_lock(&root->inode_lock); |
Nikolay Borisov | b79b724 | 2020-08-31 14:42:38 +0300 | [diff] [blame] | 5644 | if (!RB_EMPTY_NODE(&inode->rb_node)) { |
| 5645 | rb_erase(&inode->rb_node, &root->inode_tree); |
| 5646 | RB_CLEAR_NODE(&inode->rb_node); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5647 | empty = RB_EMPTY_ROOT(&root->inode_tree); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5648 | } |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 5649 | spin_unlock(&root->inode_lock); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5650 | |
Stefan Behrens | 69e9c6c | 2013-09-05 16:58:43 +0200 | [diff] [blame] | 5651 | if (empty && btrfs_root_refs(&root->root_item) == 0) { |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5652 | spin_lock(&root->inode_lock); |
| 5653 | empty = RB_EMPTY_ROOT(&root->inode_tree); |
| 5654 | spin_unlock(&root->inode_lock); |
| 5655 | if (empty) |
| 5656 | btrfs_add_dead_root(root); |
| 5657 | } |
| 5658 | } |
| 5659 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5660 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5661 | static int btrfs_init_locked_inode(struct inode *inode, void *p) |
| 5662 | { |
| 5663 | struct btrfs_iget_args *args = p; |
David Sterba | 0202e83 | 2020-05-15 19:35:59 +0200 | [diff] [blame] | 5664 | |
| 5665 | inode->i_ino = args->ino; |
| 5666 | BTRFS_I(inode)->location.objectid = args->ino; |
| 5667 | BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY; |
| 5668 | BTRFS_I(inode)->location.offset = 0; |
Josef Bacik | 5c8fd99 | 2020-02-14 16:11:43 -0500 | [diff] [blame] | 5669 | BTRFS_I(inode)->root = btrfs_grab_root(args->root); |
| 5670 | BUG_ON(args->root && !BTRFS_I(inode)->root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5671 | return 0; |
| 5672 | } |
| 5673 | |
| 5674 | static int btrfs_find_actor(struct inode *inode, void *opaque) |
| 5675 | { |
| 5676 | struct btrfs_iget_args *args = opaque; |
David Sterba | 0202e83 | 2020-05-15 19:35:59 +0200 | [diff] [blame] | 5677 | |
| 5678 | return args->ino == BTRFS_I(inode)->location.objectid && |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5679 | args->root == BTRFS_I(inode)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5680 | } |
| 5681 | |
David Sterba | 0202e83 | 2020-05-15 19:35:59 +0200 | [diff] [blame] | 5682 | static struct inode *btrfs_iget_locked(struct super_block *s, u64 ino, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5683 | struct btrfs_root *root) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5684 | { |
| 5685 | struct inode *inode; |
| 5686 | struct btrfs_iget_args args; |
David Sterba | 0202e83 | 2020-05-15 19:35:59 +0200 | [diff] [blame] | 5687 | unsigned long hashval = btrfs_inode_hash(ino, root); |
Filipe David Borba Manana | 778ba82 | 2013-10-06 22:22:33 +0100 | [diff] [blame] | 5688 | |
David Sterba | 0202e83 | 2020-05-15 19:35:59 +0200 | [diff] [blame] | 5689 | args.ino = ino; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5690 | args.root = root; |
| 5691 | |
Filipe David Borba Manana | 778ba82 | 2013-10-06 22:22:33 +0100 | [diff] [blame] | 5692 | inode = iget5_locked(s, hashval, btrfs_find_actor, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5693 | btrfs_init_locked_inode, |
| 5694 | (void *)&args); |
| 5695 | return inode; |
| 5696 | } |
| 5697 | |
David Sterba | 4c66e0d | 2019-10-03 19:09:35 +0200 | [diff] [blame] | 5698 | /* |
David Sterba | 0202e83 | 2020-05-15 19:35:59 +0200 | [diff] [blame] | 5699 | * Get an inode object given its inode number and corresponding root. |
David Sterba | 4c66e0d | 2019-10-03 19:09:35 +0200 | [diff] [blame] | 5700 | * Path can be preallocated to prevent recursing back to iget through |
| 5701 | * allocator. NULL is also valid but may require an additional allocation |
| 5702 | * later. |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 5703 | */ |
David Sterba | 0202e83 | 2020-05-15 19:35:59 +0200 | [diff] [blame] | 5704 | struct inode *btrfs_iget_path(struct super_block *s, u64 ino, |
David Sterba | 4c66e0d | 2019-10-03 19:09:35 +0200 | [diff] [blame] | 5705 | struct btrfs_root *root, struct btrfs_path *path) |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 5706 | { |
| 5707 | struct inode *inode; |
| 5708 | |
David Sterba | 0202e83 | 2020-05-15 19:35:59 +0200 | [diff] [blame] | 5709 | inode = btrfs_iget_locked(s, ino, root); |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 5710 | if (!inode) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5711 | return ERR_PTR(-ENOMEM); |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 5712 | |
| 5713 | if (inode->i_state & I_NEW) { |
Filipe Manana | 6771089 | 2016-06-06 11:51:25 +0100 | [diff] [blame] | 5714 | int ret; |
| 5715 | |
Filipe Manana | 4222ea7 | 2018-10-24 10:13:03 +0100 | [diff] [blame] | 5716 | ret = btrfs_read_locked_inode(inode, path); |
Al Viro | 9bc2cef | 2018-07-29 23:04:50 +0100 | [diff] [blame] | 5717 | if (!ret) { |
Mark Fasheh | 1748f84 | 2011-07-12 11:25:31 -0700 | [diff] [blame] | 5718 | inode_tree_add(inode); |
| 5719 | unlock_new_inode(inode); |
Mark Fasheh | 1748f84 | 2011-07-12 11:25:31 -0700 | [diff] [blame] | 5720 | } else { |
Al Viro | f5b3a41 | 2018-07-29 23:04:51 +0100 | [diff] [blame] | 5721 | iget_failed(inode); |
| 5722 | /* |
| 5723 | * ret > 0 can come from btrfs_search_slot called by |
| 5724 | * btrfs_read_locked_inode, this means the inode item |
| 5725 | * was not found. |
| 5726 | */ |
| 5727 | if (ret > 0) |
| 5728 | ret = -ENOENT; |
| 5729 | inode = ERR_PTR(ret); |
Mark Fasheh | 1748f84 | 2011-07-12 11:25:31 -0700 | [diff] [blame] | 5730 | } |
| 5731 | } |
| 5732 | |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 5733 | return inode; |
| 5734 | } |
| 5735 | |
David Sterba | 0202e83 | 2020-05-15 19:35:59 +0200 | [diff] [blame] | 5736 | 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] | 5737 | { |
David Sterba | 0202e83 | 2020-05-15 19:35:59 +0200 | [diff] [blame] | 5738 | return btrfs_iget_path(s, ino, root, NULL); |
Filipe Manana | 4222ea7 | 2018-10-24 10:13:03 +0100 | [diff] [blame] | 5739 | } |
| 5740 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5741 | static struct inode *new_simple_dir(struct super_block *s, |
| 5742 | struct btrfs_key *key, |
| 5743 | struct btrfs_root *root) |
| 5744 | { |
| 5745 | struct inode *inode = new_inode(s); |
| 5746 | |
| 5747 | if (!inode) |
| 5748 | return ERR_PTR(-ENOMEM); |
| 5749 | |
Josef Bacik | 5c8fd99 | 2020-02-14 16:11:43 -0500 | [diff] [blame] | 5750 | BTRFS_I(inode)->root = btrfs_grab_root(root); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5751 | memcpy(&BTRFS_I(inode)->location, key, sizeof(*key)); |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 5752 | set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5753 | |
| 5754 | inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID; |
Omar Sandoval | 6bb6b51 | 2019-12-05 10:36:04 -0800 | [diff] [blame] | 5755 | /* |
| 5756 | * We only need lookup, the rest is read-only and there's no inode |
| 5757 | * associated with the dentry |
| 5758 | */ |
| 5759 | inode->i_op = &simple_dir_inode_operations; |
Omar Sandoval | 1fdf419 | 2017-01-25 17:06:39 -0800 | [diff] [blame] | 5760 | inode->i_opflags &= ~IOP_XATTR; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5761 | inode->i_fop = &simple_dir_operations; |
| 5762 | inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO; |
Deepa Dinamani | c2050a4 | 2016-09-14 07:48:06 -0700 | [diff] [blame] | 5763 | inode->i_mtime = current_time(inode); |
chandan r | 9cc97d6 | 2012-07-04 12:48:07 +0530 | [diff] [blame] | 5764 | inode->i_atime = inode->i_mtime; |
| 5765 | inode->i_ctime = inode->i_mtime; |
Arnd Bergmann | d3c6be6f | 2018-06-21 18:04:06 +0200 | [diff] [blame] | 5766 | BTRFS_I(inode)->i_otime = inode->i_mtime; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5767 | |
| 5768 | return inode; |
| 5769 | } |
| 5770 | |
Qu Wenruo | 6bf9e4b | 2019-03-13 13:55:11 +0800 | [diff] [blame] | 5771 | static inline u8 btrfs_inode_type(struct inode *inode) |
| 5772 | { |
| 5773 | /* |
| 5774 | * Compile-time asserts that generic FT_* types still match |
| 5775 | * BTRFS_FT_* types |
| 5776 | */ |
| 5777 | BUILD_BUG_ON(BTRFS_FT_UNKNOWN != FT_UNKNOWN); |
| 5778 | BUILD_BUG_ON(BTRFS_FT_REG_FILE != FT_REG_FILE); |
| 5779 | BUILD_BUG_ON(BTRFS_FT_DIR != FT_DIR); |
| 5780 | BUILD_BUG_ON(BTRFS_FT_CHRDEV != FT_CHRDEV); |
| 5781 | BUILD_BUG_ON(BTRFS_FT_BLKDEV != FT_BLKDEV); |
| 5782 | BUILD_BUG_ON(BTRFS_FT_FIFO != FT_FIFO); |
| 5783 | BUILD_BUG_ON(BTRFS_FT_SOCK != FT_SOCK); |
| 5784 | BUILD_BUG_ON(BTRFS_FT_SYMLINK != FT_SYMLINK); |
| 5785 | |
| 5786 | return fs_umode_to_ftype(inode->i_mode); |
| 5787 | } |
| 5788 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5789 | struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5790 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5791 | struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5792 | struct inode *inode; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5793 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5794 | struct btrfs_root *sub_root = root; |
| 5795 | struct btrfs_key location; |
Qu Wenruo | 6bf9e4b | 2019-03-13 13:55:11 +0800 | [diff] [blame] | 5796 | u8 di_type = 0; |
Josef Bacik | b4aff1f | 2011-06-28 16:18:59 -0400 | [diff] [blame] | 5797 | int ret = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5798 | |
| 5799 | if (dentry->d_name.len > BTRFS_NAME_LEN) |
| 5800 | return ERR_PTR(-ENAMETOOLONG); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5801 | |
Qu Wenruo | 6bf9e4b | 2019-03-13 13:55:11 +0800 | [diff] [blame] | 5802 | ret = btrfs_inode_by_name(dir, dentry, &location, &di_type); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5803 | if (ret < 0) |
| 5804 | return ERR_PTR(ret); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5805 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5806 | if (location.type == BTRFS_INODE_ITEM_KEY) { |
David Sterba | 0202e83 | 2020-05-15 19:35:59 +0200 | [diff] [blame] | 5807 | inode = btrfs_iget(dir->i_sb, location.objectid, root); |
Qu Wenruo | 6bf9e4b | 2019-03-13 13:55:11 +0800 | [diff] [blame] | 5808 | if (IS_ERR(inode)) |
| 5809 | return inode; |
| 5810 | |
| 5811 | /* Do extra check against inode mode with di_type */ |
| 5812 | if (btrfs_inode_type(inode) != di_type) { |
| 5813 | btrfs_crit(fs_info, |
| 5814 | "inode mode mismatch with dir: inode mode=0%o btrfs type=%u dir type=%u", |
| 5815 | inode->i_mode, btrfs_inode_type(inode), |
| 5816 | di_type); |
| 5817 | iput(inode); |
| 5818 | return ERR_PTR(-EUCLEAN); |
| 5819 | } |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5820 | return inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5821 | } |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5822 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5823 | ret = fixup_tree_root_location(fs_info, dir, dentry, |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5824 | &location, &sub_root); |
| 5825 | if (ret < 0) { |
| 5826 | if (ret != -ENOENT) |
| 5827 | inode = ERR_PTR(ret); |
| 5828 | else |
| 5829 | inode = new_simple_dir(dir->i_sb, &location, sub_root); |
| 5830 | } else { |
David Sterba | 0202e83 | 2020-05-15 19:35:59 +0200 | [diff] [blame] | 5831 | inode = btrfs_iget(dir->i_sb, location.objectid, sub_root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5832 | } |
Josef Bacik | 8727002 | 2020-01-24 09:32:31 -0500 | [diff] [blame] | 5833 | if (root != sub_root) |
Josef Bacik | 0024652 | 2020-01-24 09:33:01 -0500 | [diff] [blame] | 5834 | btrfs_put_root(sub_root); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5835 | |
Julia Lawall | 34d19ba | 2011-01-24 19:55:19 +0000 | [diff] [blame] | 5836 | if (!IS_ERR(inode) && root != sub_root) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5837 | down_read(&fs_info->cleanup_work_sem); |
David Howells | bc98a42 | 2017-07-17 08:45:34 +0100 | [diff] [blame] | 5838 | if (!sb_rdonly(inode->i_sb)) |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 5839 | ret = btrfs_orphan_cleanup(sub_root); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5840 | up_read(&fs_info->cleanup_work_sem); |
Josef Bacik | 01cd336 | 2013-06-03 21:39:49 -0400 | [diff] [blame] | 5841 | if (ret) { |
| 5842 | iput(inode); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 5843 | inode = ERR_PTR(ret); |
Josef Bacik | 01cd336 | 2013-06-03 21:39:49 -0400 | [diff] [blame] | 5844 | } |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 5845 | } |
| 5846 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5847 | return inode; |
| 5848 | } |
| 5849 | |
Nick Piggin | fe15ce4 | 2011-01-07 17:49:23 +1100 | [diff] [blame] | 5850 | static int btrfs_dentry_delete(const struct dentry *dentry) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5851 | { |
| 5852 | struct btrfs_root *root; |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 5853 | struct inode *inode = d_inode(dentry); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5854 | |
Li Zefan | 848cce0 | 2012-02-21 17:04:28 +0800 | [diff] [blame] | 5855 | if (!inode && !IS_ROOT(dentry)) |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 5856 | inode = d_inode(dentry->d_parent); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5857 | |
Li Zefan | 848cce0 | 2012-02-21 17:04:28 +0800 | [diff] [blame] | 5858 | if (inode) { |
| 5859 | root = BTRFS_I(inode)->root; |
Yan, Zheng | efefb14 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5860 | if (btrfs_root_refs(&root->root_item) == 0) |
| 5861 | return 1; |
Li Zefan | 848cce0 | 2012-02-21 17:04:28 +0800 | [diff] [blame] | 5862 | |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 5863 | if (btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) |
Li Zefan | 848cce0 | 2012-02-21 17:04:28 +0800 | [diff] [blame] | 5864 | return 1; |
Yan, Zheng | efefb14 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5865 | } |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5866 | return 0; |
| 5867 | } |
| 5868 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5869 | static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry, |
Al Viro | 00cd8dd | 2012-06-10 17:13:09 -0400 | [diff] [blame] | 5870 | unsigned int flags) |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5871 | { |
Al Viro | 3837d20 | 2018-10-10 16:38:27 -0400 | [diff] [blame] | 5872 | struct inode *inode = btrfs_lookup_dentry(dir, dentry); |
Josef Bacik | a66e7cc | 2011-09-18 10:34:03 -0400 | [diff] [blame] | 5873 | |
Al Viro | 3837d20 | 2018-10-10 16:38:27 -0400 | [diff] [blame] | 5874 | if (inode == ERR_PTR(-ENOENT)) |
| 5875 | inode = NULL; |
Al Viro | 41d28bc | 2014-10-12 22:24:21 -0400 | [diff] [blame] | 5876 | return d_splice_alias(inode, dentry); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5877 | } |
| 5878 | |
Josef Bacik | 23b5ec7 | 2017-07-24 15:14:25 -0400 | [diff] [blame] | 5879 | /* |
| 5880 | * All this infrastructure exists because dir_emit can fault, and we are holding |
| 5881 | * the tree lock when doing readdir. For now just allocate a buffer and copy |
| 5882 | * our information into that, and then dir_emit from the buffer. This is |
| 5883 | * similar to what NFS does, only we don't keep the buffer around in pagecache |
| 5884 | * because I'm afraid I'll mess that up. Long term we need to make filldir do |
| 5885 | * copy_to_user_inatomic so we don't have to worry about page faulting under the |
| 5886 | * tree lock. |
| 5887 | */ |
| 5888 | static int btrfs_opendir(struct inode *inode, struct file *file) |
| 5889 | { |
| 5890 | struct btrfs_file_private *private; |
| 5891 | |
| 5892 | private = kzalloc(sizeof(struct btrfs_file_private), GFP_KERNEL); |
| 5893 | if (!private) |
| 5894 | return -ENOMEM; |
| 5895 | private->filldir_buf = kzalloc(PAGE_SIZE, GFP_KERNEL); |
| 5896 | if (!private->filldir_buf) { |
| 5897 | kfree(private); |
| 5898 | return -ENOMEM; |
| 5899 | } |
| 5900 | file->private_data = private; |
| 5901 | return 0; |
| 5902 | } |
| 5903 | |
| 5904 | struct dir_entry { |
| 5905 | u64 ino; |
| 5906 | u64 offset; |
| 5907 | unsigned type; |
| 5908 | int name_len; |
| 5909 | }; |
| 5910 | |
| 5911 | static int btrfs_filldir(void *addr, int entries, struct dir_context *ctx) |
| 5912 | { |
| 5913 | while (entries--) { |
| 5914 | struct dir_entry *entry = addr; |
| 5915 | char *name = (char *)(entry + 1); |
| 5916 | |
David Sterba | 92d3217 | 2018-04-16 21:10:14 +0200 | [diff] [blame] | 5917 | ctx->pos = get_unaligned(&entry->offset); |
| 5918 | if (!dir_emit(ctx, name, get_unaligned(&entry->name_len), |
| 5919 | get_unaligned(&entry->ino), |
| 5920 | get_unaligned(&entry->type))) |
Josef Bacik | 23b5ec7 | 2017-07-24 15:14:25 -0400 | [diff] [blame] | 5921 | return 1; |
David Sterba | 92d3217 | 2018-04-16 21:10:14 +0200 | [diff] [blame] | 5922 | addr += sizeof(struct dir_entry) + |
| 5923 | get_unaligned(&entry->name_len); |
Josef Bacik | 23b5ec7 | 2017-07-24 15:14:25 -0400 | [diff] [blame] | 5924 | ctx->pos++; |
| 5925 | } |
| 5926 | return 0; |
| 5927 | } |
| 5928 | |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5929 | static int btrfs_real_readdir(struct file *file, struct dir_context *ctx) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5930 | { |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5931 | struct inode *inode = file_inode(file); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5932 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Josef Bacik | 23b5ec7 | 2017-07-24 15:14:25 -0400 | [diff] [blame] | 5933 | struct btrfs_file_private *private = file->private_data; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5934 | struct btrfs_dir_item *di; |
| 5935 | struct btrfs_key key; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5936 | struct btrfs_key found_key; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5937 | struct btrfs_path *path; |
Josef Bacik | 23b5ec7 | 2017-07-24 15:14:25 -0400 | [diff] [blame] | 5938 | void *addr; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5939 | struct list_head ins_list; |
| 5940 | struct list_head del_list; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5941 | int ret; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5942 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5943 | int slot; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5944 | char *name_ptr; |
| 5945 | int name_len; |
Josef Bacik | 23b5ec7 | 2017-07-24 15:14:25 -0400 | [diff] [blame] | 5946 | int entries = 0; |
| 5947 | int total_len = 0; |
Omar Sandoval | 02dbfc9 | 2016-05-20 13:50:33 -0700 | [diff] [blame] | 5948 | bool put = false; |
Jeff Mahoney | c2951f3 | 2016-11-21 15:59:04 +0100 | [diff] [blame] | 5949 | struct btrfs_key location; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5950 | |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5951 | if (!dir_emit_dots(file, ctx)) |
| 5952 | return 0; |
| 5953 | |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5954 | path = btrfs_alloc_path(); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5955 | if (!path) |
| 5956 | return -ENOMEM; |
Chris Mason | ff5714c | 2011-05-28 07:00:39 -0400 | [diff] [blame] | 5957 | |
Josef Bacik | 23b5ec7 | 2017-07-24 15:14:25 -0400 | [diff] [blame] | 5958 | addr = private->filldir_buf; |
David Sterba | e4058b5 | 2015-11-27 16:31:35 +0100 | [diff] [blame] | 5959 | path->reada = READA_FORWARD; |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5960 | |
Jeff Mahoney | c2951f3 | 2016-11-21 15:59:04 +0100 | [diff] [blame] | 5961 | INIT_LIST_HEAD(&ins_list); |
| 5962 | INIT_LIST_HEAD(&del_list); |
| 5963 | put = btrfs_readdir_get_delayed_items(inode, &ins_list, &del_list); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5964 | |
Josef Bacik | 23b5ec7 | 2017-07-24 15:14:25 -0400 | [diff] [blame] | 5965 | again: |
Jeff Mahoney | c2951f3 | 2016-11-21 15:59:04 +0100 | [diff] [blame] | 5966 | key.type = BTRFS_DIR_INDEX_KEY; |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5967 | key.offset = ctx->pos; |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 5968 | key.objectid = btrfs_ino(BTRFS_I(inode)); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5969 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5970 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 5971 | if (ret < 0) |
| 5972 | goto err; |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5973 | |
| 5974 | while (1) { |
Josef Bacik | 23b5ec7 | 2017-07-24 15:14:25 -0400 | [diff] [blame] | 5975 | struct dir_entry *entry; |
| 5976 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5977 | leaf = path->nodes[0]; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5978 | slot = path->slots[0]; |
Li Zefan | b9e03af | 2011-03-23 10:43:58 +0800 | [diff] [blame] | 5979 | if (slot >= btrfs_header_nritems(leaf)) { |
| 5980 | ret = btrfs_next_leaf(root, path); |
| 5981 | if (ret < 0) |
| 5982 | goto err; |
| 5983 | else if (ret > 0) |
| 5984 | break; |
| 5985 | continue; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5986 | } |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5987 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5988 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
| 5989 | |
| 5990 | if (found_key.objectid != key.objectid) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5991 | break; |
Jeff Mahoney | c2951f3 | 2016-11-21 15:59:04 +0100 | [diff] [blame] | 5992 | if (found_key.type != BTRFS_DIR_INDEX_KEY) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5993 | break; |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5994 | if (found_key.offset < ctx->pos) |
Li Zefan | b9e03af | 2011-03-23 10:43:58 +0800 | [diff] [blame] | 5995 | goto next; |
Jeff Mahoney | c2951f3 | 2016-11-21 15:59:04 +0100 | [diff] [blame] | 5996 | if (btrfs_should_delete_dir_index(&del_list, found_key.offset)) |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5997 | goto next; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5998 | di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item); |
Jeff Mahoney | c2951f3 | 2016-11-21 15:59:04 +0100 | [diff] [blame] | 5999 | name_len = btrfs_dir_name_len(leaf, di); |
Josef Bacik | 23b5ec7 | 2017-07-24 15:14:25 -0400 | [diff] [blame] | 6000 | if ((total_len + sizeof(struct dir_entry) + name_len) >= |
| 6001 | PAGE_SIZE) { |
| 6002 | btrfs_release_path(path); |
| 6003 | ret = btrfs_filldir(private->filldir_buf, entries, ctx); |
| 6004 | if (ret) |
| 6005 | goto nopos; |
| 6006 | addr = private->filldir_buf; |
| 6007 | entries = 0; |
| 6008 | total_len = 0; |
| 6009 | goto again; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6010 | } |
Josef Bacik | 23b5ec7 | 2017-07-24 15:14:25 -0400 | [diff] [blame] | 6011 | |
| 6012 | entry = addr; |
David Sterba | 92d3217 | 2018-04-16 21:10:14 +0200 | [diff] [blame] | 6013 | put_unaligned(name_len, &entry->name_len); |
Josef Bacik | 23b5ec7 | 2017-07-24 15:14:25 -0400 | [diff] [blame] | 6014 | name_ptr = (char *)(entry + 1); |
Jeff Mahoney | c2951f3 | 2016-11-21 15:59:04 +0100 | [diff] [blame] | 6015 | read_extent_buffer(leaf, name_ptr, (unsigned long)(di + 1), |
| 6016 | name_len); |
Phillip Potter | 7d157c3 | 2019-03-26 21:39:34 +0000 | [diff] [blame] | 6017 | put_unaligned(fs_ftype_to_dtype(btrfs_dir_type(leaf, di)), |
David Sterba | 92d3217 | 2018-04-16 21:10:14 +0200 | [diff] [blame] | 6018 | &entry->type); |
Jeff Mahoney | c2951f3 | 2016-11-21 15:59:04 +0100 | [diff] [blame] | 6019 | btrfs_dir_item_key_to_cpu(leaf, di, &location); |
David Sterba | 92d3217 | 2018-04-16 21:10:14 +0200 | [diff] [blame] | 6020 | put_unaligned(location.objectid, &entry->ino); |
| 6021 | put_unaligned(found_key.offset, &entry->offset); |
Josef Bacik | 23b5ec7 | 2017-07-24 15:14:25 -0400 | [diff] [blame] | 6022 | entries++; |
| 6023 | addr += sizeof(struct dir_entry) + name_len; |
| 6024 | total_len += sizeof(struct dir_entry) + name_len; |
Li Zefan | b9e03af | 2011-03-23 10:43:58 +0800 | [diff] [blame] | 6025 | next: |
| 6026 | path->slots[0]++; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6027 | } |
Josef Bacik | 23b5ec7 | 2017-07-24 15:14:25 -0400 | [diff] [blame] | 6028 | btrfs_release_path(path); |
| 6029 | |
| 6030 | ret = btrfs_filldir(private->filldir_buf, entries, ctx); |
| 6031 | if (ret) |
| 6032 | goto nopos; |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 6033 | |
Jeff Mahoney | d2fbb2b | 2016-11-05 13:26:35 -0400 | [diff] [blame] | 6034 | ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list); |
Jeff Mahoney | c2951f3 | 2016-11-21 15:59:04 +0100 | [diff] [blame] | 6035 | if (ret) |
David Sterba | bc4ef75 | 2015-11-13 13:44:28 +0100 | [diff] [blame] | 6036 | goto nopos; |
| 6037 | |
Zach Brown | db62efb | 2013-07-11 16:19:42 -0700 | [diff] [blame] | 6038 | /* |
| 6039 | * Stop new entries from being returned after we return the last |
| 6040 | * entry. |
| 6041 | * |
| 6042 | * New directory entries are assigned a strictly increasing |
| 6043 | * offset. This means that new entries created during readdir |
| 6044 | * are *guaranteed* to be seen in the future by that readdir. |
| 6045 | * This has broken buggy programs which operate on names as |
| 6046 | * they're returned by readdir. Until we re-use freed offsets |
| 6047 | * we have this hack to stop new entries from being returned |
| 6048 | * under the assumption that they'll never reach this huge |
| 6049 | * offset. |
| 6050 | * |
| 6051 | * This is being careful not to overflow 32bit loff_t unless the |
| 6052 | * last entry requires it because doing so has broken 32bit apps |
| 6053 | * in the past. |
| 6054 | */ |
Jeff Mahoney | c2951f3 | 2016-11-21 15:59:04 +0100 | [diff] [blame] | 6055 | if (ctx->pos >= INT_MAX) |
| 6056 | ctx->pos = LLONG_MAX; |
| 6057 | else |
| 6058 | ctx->pos = INT_MAX; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6059 | nopos: |
| 6060 | ret = 0; |
| 6061 | err: |
Omar Sandoval | 02dbfc9 | 2016-05-20 13:50:33 -0700 | [diff] [blame] | 6062 | if (put) |
| 6063 | btrfs_readdir_put_delayed_items(inode, &ins_list, &del_list); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6064 | btrfs_free_path(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6065 | return ret; |
| 6066 | } |
| 6067 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6068 | /* |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 6069 | * This is somewhat expensive, updating the tree every time the |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6070 | * inode changes. But, it is most likely to find the inode in cache. |
| 6071 | * FIXME, needs more benchmarking...there are no reasons other than performance |
| 6072 | * to keep or drop this code. |
| 6073 | */ |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 6074 | static int btrfs_dirty_inode(struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6075 | { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6076 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6077 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 6078 | struct btrfs_trans_handle *trans; |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 6079 | int ret; |
| 6080 | |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 6081 | if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags)) |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 6082 | return 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6083 | |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 6084 | trans = btrfs_join_transaction(root); |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 6085 | if (IS_ERR(trans)) |
| 6086 | return PTR_ERR(trans); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 6087 | |
Nikolay Borisov | 9a56fcd | 2020-11-02 16:48:59 +0200 | [diff] [blame] | 6088 | ret = btrfs_update_inode(trans, root, BTRFS_I(inode)); |
Nikolay Borisov | 4d14c5c | 2021-02-22 18:40:44 +0200 | [diff] [blame] | 6089 | if (ret && (ret == -ENOSPC || ret == -EDQUOT)) { |
Chris Mason | 94b6044 | 2010-05-26 11:02:00 -0400 | [diff] [blame] | 6090 | /* whoops, lets try again with the full transaction */ |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 6091 | btrfs_end_transaction(trans); |
Chris Mason | 94b6044 | 2010-05-26 11:02:00 -0400 | [diff] [blame] | 6092 | trans = btrfs_start_transaction(root, 1); |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 6093 | if (IS_ERR(trans)) |
| 6094 | return PTR_ERR(trans); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 6095 | |
Nikolay Borisov | 9a56fcd | 2020-11-02 16:48:59 +0200 | [diff] [blame] | 6096 | ret = btrfs_update_inode(trans, root, BTRFS_I(inode)); |
Chris Mason | 94b6044 | 2010-05-26 11:02:00 -0400 | [diff] [blame] | 6097 | } |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 6098 | btrfs_end_transaction(trans); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 6099 | if (BTRFS_I(inode)->delayed_node) |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6100 | btrfs_balance_delayed_items(fs_info); |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 6101 | |
| 6102 | return ret; |
| 6103 | } |
| 6104 | |
| 6105 | /* |
| 6106 | * This is a copy of file_update_time. We need this so we can return error on |
| 6107 | * ENOSPC for updating the inode in the case of file write and mmap writes. |
| 6108 | */ |
Deepa Dinamani | 95582b0 | 2018-05-08 19:36:02 -0700 | [diff] [blame] | 6109 | static int btrfs_update_time(struct inode *inode, struct timespec64 *now, |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 6110 | int flags) |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 6111 | { |
Alexander Block | 2bc556528 | 2012-06-15 09:49:33 +0200 | [diff] [blame] | 6112 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Jeff Layton | 3a8c723 | 2017-12-11 06:35:24 -0500 | [diff] [blame] | 6113 | bool dirty = flags & ~S_VERSION; |
Alexander Block | 2bc556528 | 2012-06-15 09:49:33 +0200 | [diff] [blame] | 6114 | |
| 6115 | if (btrfs_root_readonly(root)) |
| 6116 | return -EROFS; |
| 6117 | |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 6118 | if (flags & S_VERSION) |
Jeff Layton | 3a8c723 | 2017-12-11 06:35:24 -0500 | [diff] [blame] | 6119 | dirty |= inode_maybe_inc_iversion(inode, dirty); |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 6120 | if (flags & S_CTIME) |
| 6121 | inode->i_ctime = *now; |
| 6122 | if (flags & S_MTIME) |
| 6123 | inode->i_mtime = *now; |
| 6124 | if (flags & S_ATIME) |
| 6125 | inode->i_atime = *now; |
Jeff Layton | 3a8c723 | 2017-12-11 06:35:24 -0500 | [diff] [blame] | 6126 | return dirty ? btrfs_dirty_inode(inode) : 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6127 | } |
| 6128 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 6129 | /* |
| 6130 | * find the highest existing sequence number in a directory |
| 6131 | * and then set the in-memory index_cnt variable to reflect |
| 6132 | * free sequence numbers |
| 6133 | */ |
Nikolay Borisov | 4c570655 | 2017-02-20 13:50:32 +0200 | [diff] [blame] | 6134 | static int btrfs_set_inode_index_count(struct btrfs_inode *inode) |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6135 | { |
Nikolay Borisov | 4c570655 | 2017-02-20 13:50:32 +0200 | [diff] [blame] | 6136 | struct btrfs_root *root = inode->root; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6137 | struct btrfs_key key, found_key; |
| 6138 | struct btrfs_path *path; |
| 6139 | struct extent_buffer *leaf; |
| 6140 | int ret; |
| 6141 | |
Nikolay Borisov | 4c570655 | 2017-02-20 13:50:32 +0200 | [diff] [blame] | 6142 | key.objectid = btrfs_ino(inode); |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 6143 | key.type = BTRFS_DIR_INDEX_KEY; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6144 | key.offset = (u64)-1; |
| 6145 | |
| 6146 | path = btrfs_alloc_path(); |
| 6147 | if (!path) |
| 6148 | return -ENOMEM; |
| 6149 | |
| 6150 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 6151 | if (ret < 0) |
| 6152 | goto out; |
| 6153 | /* FIXME: we should be able to handle this */ |
| 6154 | if (ret == 0) |
| 6155 | goto out; |
| 6156 | ret = 0; |
| 6157 | |
| 6158 | /* |
| 6159 | * MAGIC NUMBER EXPLANATION: |
| 6160 | * since we search a directory based on f_pos we have to start at 2 |
| 6161 | * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody |
| 6162 | * else has to start at 2 |
| 6163 | */ |
| 6164 | if (path->slots[0] == 0) { |
Nikolay Borisov | 4c570655 | 2017-02-20 13:50:32 +0200 | [diff] [blame] | 6165 | inode->index_cnt = 2; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6166 | goto out; |
| 6167 | } |
| 6168 | |
| 6169 | path->slots[0]--; |
| 6170 | |
| 6171 | leaf = path->nodes[0]; |
| 6172 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 6173 | |
Nikolay Borisov | 4c570655 | 2017-02-20 13:50:32 +0200 | [diff] [blame] | 6174 | if (found_key.objectid != btrfs_ino(inode) || |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 6175 | found_key.type != BTRFS_DIR_INDEX_KEY) { |
Nikolay Borisov | 4c570655 | 2017-02-20 13:50:32 +0200 | [diff] [blame] | 6176 | inode->index_cnt = 2; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6177 | goto out; |
| 6178 | } |
| 6179 | |
Nikolay Borisov | 4c570655 | 2017-02-20 13:50:32 +0200 | [diff] [blame] | 6180 | inode->index_cnt = found_key.offset + 1; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6181 | out: |
| 6182 | btrfs_free_path(path); |
| 6183 | return ret; |
| 6184 | } |
| 6185 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 6186 | /* |
| 6187 | * helper to find a free sequence number in a given directory. This current |
| 6188 | * code is very simple, later versions will do smarter things in the btree |
| 6189 | */ |
Nikolay Borisov | 877574e | 2017-02-20 13:50:33 +0200 | [diff] [blame] | 6190 | int btrfs_set_inode_index(struct btrfs_inode *dir, u64 *index) |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6191 | { |
| 6192 | int ret = 0; |
| 6193 | |
Nikolay Borisov | 877574e | 2017-02-20 13:50:33 +0200 | [diff] [blame] | 6194 | if (dir->index_cnt == (u64)-1) { |
| 6195 | ret = btrfs_inode_delayed_dir_index_count(dir); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 6196 | if (ret) { |
| 6197 | ret = btrfs_set_inode_index_count(dir); |
| 6198 | if (ret) |
| 6199 | return ret; |
| 6200 | } |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6201 | } |
| 6202 | |
Nikolay Borisov | 877574e | 2017-02-20 13:50:33 +0200 | [diff] [blame] | 6203 | *index = dir->index_cnt; |
| 6204 | dir->index_cnt++; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6205 | |
| 6206 | return ret; |
| 6207 | } |
| 6208 | |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6209 | static int btrfs_insert_inode_locked(struct inode *inode) |
| 6210 | { |
| 6211 | struct btrfs_iget_args args; |
David Sterba | 0202e83 | 2020-05-15 19:35:59 +0200 | [diff] [blame] | 6212 | |
| 6213 | args.ino = BTRFS_I(inode)->location.objectid; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6214 | args.root = BTRFS_I(inode)->root; |
| 6215 | |
| 6216 | return insert_inode_locked4(inode, |
| 6217 | btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root), |
| 6218 | btrfs_find_actor, &args); |
| 6219 | } |
| 6220 | |
Anand Jain | 19aee8d | 2017-07-18 17:37:05 +0800 | [diff] [blame] | 6221 | /* |
| 6222 | * Inherit flags from the parent inode. |
| 6223 | * |
| 6224 | * Currently only the compression flags and the cow flags are inherited. |
| 6225 | */ |
| 6226 | static void btrfs_inherit_iflags(struct inode *inode, struct inode *dir) |
| 6227 | { |
| 6228 | unsigned int flags; |
| 6229 | |
| 6230 | if (!dir) |
| 6231 | return; |
| 6232 | |
| 6233 | flags = BTRFS_I(dir)->flags; |
| 6234 | |
| 6235 | if (flags & BTRFS_INODE_NOCOMPRESS) { |
| 6236 | BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS; |
| 6237 | BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS; |
| 6238 | } else if (flags & BTRFS_INODE_COMPRESS) { |
| 6239 | BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS; |
| 6240 | BTRFS_I(inode)->flags |= BTRFS_INODE_COMPRESS; |
| 6241 | } |
| 6242 | |
| 6243 | if (flags & BTRFS_INODE_NODATACOW) { |
| 6244 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW; |
| 6245 | if (S_ISREG(inode->i_mode)) |
| 6246 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM; |
| 6247 | } |
| 6248 | |
David Sterba | 7b6a221 | 2018-03-26 18:40:21 +0200 | [diff] [blame] | 6249 | btrfs_sync_inode_flags_to_i_flags(inode); |
Anand Jain | 19aee8d | 2017-07-18 17:37:05 +0800 | [diff] [blame] | 6250 | } |
| 6251 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6252 | static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans, |
| 6253 | struct btrfs_root *root, |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6254 | struct inode *dir, |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 6255 | const char *name, int name_len, |
Al Viro | 175a4eb | 2011-07-26 03:30:54 -0400 | [diff] [blame] | 6256 | u64 ref_objectid, u64 objectid, |
| 6257 | umode_t mode, u64 *index) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6258 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6259 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6260 | struct inode *inode; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6261 | struct btrfs_inode_item *inode_item; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6262 | struct btrfs_key *location; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6263 | struct btrfs_path *path; |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 6264 | struct btrfs_inode_ref *ref; |
| 6265 | struct btrfs_key key[2]; |
| 6266 | u32 sizes[2]; |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 6267 | int nitems = name ? 2 : 1; |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 6268 | unsigned long ptr; |
Josef Bacik | 11a19a9 | 2019-09-09 10:12:04 -0400 | [diff] [blame] | 6269 | unsigned int nofs_flag; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6270 | int ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6271 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6272 | path = btrfs_alloc_path(); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 6273 | if (!path) |
| 6274 | return ERR_PTR(-ENOMEM); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6275 | |
Josef Bacik | 11a19a9 | 2019-09-09 10:12:04 -0400 | [diff] [blame] | 6276 | nofs_flag = memalloc_nofs_save(); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6277 | inode = new_inode(fs_info->sb); |
Josef Bacik | 11a19a9 | 2019-09-09 10:12:04 -0400 | [diff] [blame] | 6278 | memalloc_nofs_restore(nofs_flag); |
Yoshinori Sano | 8fb2764 | 2011-04-09 02:30:07 +0000 | [diff] [blame] | 6279 | if (!inode) { |
| 6280 | btrfs_free_path(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6281 | return ERR_PTR(-ENOMEM); |
Yoshinori Sano | 8fb2764 | 2011-04-09 02:30:07 +0000 | [diff] [blame] | 6282 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6283 | |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 6284 | /* |
Filipe Manana | 5762b5c | 2014-08-01 00:10:32 +0100 | [diff] [blame] | 6285 | * O_TMPFILE, set link count to 0, so that after this point, |
| 6286 | * we fill in an inode item with the correct link count. |
| 6287 | */ |
| 6288 | if (!name) |
| 6289 | set_nlink(inode, 0); |
| 6290 | |
| 6291 | /* |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 6292 | * we have to initialize this early, so we can reclaim the inode |
| 6293 | * number if we fail afterwards in this function. |
| 6294 | */ |
| 6295 | inode->i_ino = objectid; |
| 6296 | |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 6297 | if (dir && name) { |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 6298 | trace_btrfs_inode_request(dir); |
| 6299 | |
Nikolay Borisov | 877574e | 2017-02-20 13:50:33 +0200 | [diff] [blame] | 6300 | ret = btrfs_set_inode_index(BTRFS_I(dir), index); |
Shen Feng | 0977143 | 2009-04-02 16:46:06 -0400 | [diff] [blame] | 6301 | if (ret) { |
Yoshinori Sano | 8fb2764 | 2011-04-09 02:30:07 +0000 | [diff] [blame] | 6302 | btrfs_free_path(path); |
Shen Feng | 0977143 | 2009-04-02 16:46:06 -0400 | [diff] [blame] | 6303 | iput(inode); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6304 | return ERR_PTR(ret); |
Shen Feng | 0977143 | 2009-04-02 16:46:06 -0400 | [diff] [blame] | 6305 | } |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 6306 | } else if (dir) { |
| 6307 | *index = 0; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6308 | } |
| 6309 | /* |
| 6310 | * index_cnt is ignored for everything but a dir, |
Su Yue | df6703e | 2018-01-12 11:08:02 +0800 | [diff] [blame] | 6311 | * btrfs_set_inode_index_count has an explanation for the magic |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6312 | * number |
| 6313 | */ |
| 6314 | BTRFS_I(inode)->index_cnt = 2; |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 6315 | BTRFS_I(inode)->dir_index = *index; |
Josef Bacik | 5c8fd99 | 2020-02-14 16:11:43 -0500 | [diff] [blame] | 6316 | BTRFS_I(inode)->root = btrfs_grab_root(root); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 6317 | BTRFS_I(inode)->generation = trans->transid; |
Josef Bacik | 7619585 | 2010-11-19 02:18:02 +0000 | [diff] [blame] | 6318 | inode->i_generation = BTRFS_I(inode)->generation; |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6319 | |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 6320 | /* |
| 6321 | * We could have gotten an inode number from somebody who was fsynced |
| 6322 | * and then removed in this same transaction, so let's just set full |
| 6323 | * sync since it will be a full sync anyway and this will blow away the |
| 6324 | * old info in the log. |
| 6325 | */ |
| 6326 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags); |
| 6327 | |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 6328 | key[0].objectid = objectid; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 6329 | key[0].type = BTRFS_INODE_ITEM_KEY; |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 6330 | key[0].offset = 0; |
| 6331 | |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 6332 | sizes[0] = sizeof(struct btrfs_inode_item); |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 6333 | |
| 6334 | if (name) { |
| 6335 | /* |
| 6336 | * Start new inodes with an inode_ref. This is slightly more |
| 6337 | * efficient for small numbers of hard links since they will |
| 6338 | * be packed into one item. Extended refs will kick in if we |
| 6339 | * add more hard links than can fit in the ref item. |
| 6340 | */ |
| 6341 | key[1].objectid = objectid; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 6342 | key[1].type = BTRFS_INODE_REF_KEY; |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 6343 | key[1].offset = ref_objectid; |
| 6344 | |
| 6345 | sizes[1] = name_len + sizeof(*ref); |
| 6346 | } |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 6347 | |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6348 | location = &BTRFS_I(inode)->location; |
| 6349 | location->objectid = objectid; |
| 6350 | location->offset = 0; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 6351 | location->type = BTRFS_INODE_ITEM_KEY; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6352 | |
| 6353 | ret = btrfs_insert_inode_locked(inode); |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6354 | if (ret < 0) { |
| 6355 | iput(inode); |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6356 | goto fail; |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6357 | } |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6358 | |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 6359 | ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems); |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 6360 | if (ret != 0) |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6361 | goto fail_unlock; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6362 | |
Christian Brauner | 21cb47b | 2021-01-21 14:19:25 +0100 | [diff] [blame] | 6363 | inode_init_owner(&init_user_ns, inode, dir, mode); |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 6364 | inode_set_bytes(inode, 0); |
chandan r | 9cc97d6 | 2012-07-04 12:48:07 +0530 | [diff] [blame] | 6365 | |
Deepa Dinamani | c2050a4 | 2016-09-14 07:48:06 -0700 | [diff] [blame] | 6366 | inode->i_mtime = current_time(inode); |
chandan r | 9cc97d6 | 2012-07-04 12:48:07 +0530 | [diff] [blame] | 6367 | inode->i_atime = inode->i_mtime; |
| 6368 | inode->i_ctime = inode->i_mtime; |
Arnd Bergmann | d3c6be6f | 2018-06-21 18:04:06 +0200 | [diff] [blame] | 6369 | BTRFS_I(inode)->i_otime = inode->i_mtime; |
chandan r | 9cc97d6 | 2012-07-04 12:48:07 +0530 | [diff] [blame] | 6370 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6371 | inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 6372 | struct btrfs_inode_item); |
David Sterba | b159fa2 | 2016-11-08 18:09:03 +0100 | [diff] [blame] | 6373 | memzero_extent_buffer(path->nodes[0], (unsigned long)inode_item, |
Li Zefan | 293f7e0 | 2012-07-10 00:58:58 -0600 | [diff] [blame] | 6374 | sizeof(*inode_item)); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 6375 | fill_inode_item(trans, path->nodes[0], inode_item, inode); |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 6376 | |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 6377 | if (name) { |
| 6378 | ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1, |
| 6379 | struct btrfs_inode_ref); |
| 6380 | btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len); |
| 6381 | btrfs_set_inode_ref_index(path->nodes[0], ref, *index); |
| 6382 | ptr = (unsigned long)(ref + 1); |
| 6383 | write_extent_buffer(path->nodes[0], name, ptr, name_len); |
| 6384 | } |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 6385 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6386 | btrfs_mark_buffer_dirty(path->nodes[0]); |
| 6387 | btrfs_free_path(path); |
| 6388 | |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 6389 | btrfs_inherit_iflags(inode, dir); |
| 6390 | |
Al Viro | 569254b | 2011-07-24 17:08:40 -0400 | [diff] [blame] | 6391 | if (S_ISREG(mode)) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6392 | if (btrfs_test_opt(fs_info, NODATASUM)) |
Chris Mason | 9427216 | 2009-07-02 12:26:06 -0400 | [diff] [blame] | 6393 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6394 | if (btrfs_test_opt(fs_info, NODATACOW)) |
Josef Bacik | f2bdf9a | 2013-02-21 15:28:28 -0500 | [diff] [blame] | 6395 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW | |
| 6396 | BTRFS_INODE_NODATASUM; |
Chris Mason | 9427216 | 2009-07-02 12:26:06 -0400 | [diff] [blame] | 6397 | } |
| 6398 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6399 | inode_tree_add(inode); |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 6400 | |
| 6401 | trace_btrfs_inode_new(inode); |
Nikolay Borisov | d909441 | 2020-06-05 10:41:13 +0300 | [diff] [blame] | 6402 | btrfs_set_inode_last_trans(trans, BTRFS_I(inode)); |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 6403 | |
Alexander Block | 8ea05e3 | 2012-07-25 17:35:53 +0200 | [diff] [blame] | 6404 | btrfs_update_root_times(trans, root); |
| 6405 | |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 6406 | ret = btrfs_inode_inherit_props(trans, inode, dir); |
| 6407 | if (ret) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6408 | btrfs_err(fs_info, |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 6409 | "error inheriting props for ino %llu (root %llu): %d", |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 6410 | btrfs_ino(BTRFS_I(inode)), root->root_key.objectid, ret); |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 6411 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6412 | return inode; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6413 | |
| 6414 | fail_unlock: |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6415 | discard_new_inode(inode); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6416 | fail: |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 6417 | if (dir && name) |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6418 | BTRFS_I(dir)->index_cnt--; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6419 | btrfs_free_path(path); |
| 6420 | return ERR_PTR(ret); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6421 | } |
| 6422 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 6423 | /* |
| 6424 | * utility function to add 'inode' into 'parent_inode' with |
| 6425 | * a give name and a given sequence number. |
| 6426 | * if 'add_backref' is true, also insert a backref from the |
| 6427 | * inode to the parent directory. |
| 6428 | */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 6429 | int btrfs_add_link(struct btrfs_trans_handle *trans, |
Nikolay Borisov | db0a669 | 2017-02-20 13:51:08 +0200 | [diff] [blame] | 6430 | struct btrfs_inode *parent_inode, struct btrfs_inode *inode, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 6431 | const char *name, int name_len, int add_backref, u64 index) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6432 | { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6433 | int ret = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6434 | struct btrfs_key key; |
Nikolay Borisov | db0a669 | 2017-02-20 13:51:08 +0200 | [diff] [blame] | 6435 | struct btrfs_root *root = parent_inode->root; |
| 6436 | u64 ino = btrfs_ino(inode); |
| 6437 | u64 parent_ino = btrfs_ino(parent_inode); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6438 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6439 | if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) { |
Nikolay Borisov | db0a669 | 2017-02-20 13:51:08 +0200 | [diff] [blame] | 6440 | memcpy(&key, &inode->root->root_key, sizeof(key)); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6441 | } else { |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6442 | key.objectid = ino; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 6443 | key.type = BTRFS_INODE_ITEM_KEY; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6444 | key.offset = 0; |
| 6445 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6446 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6447 | if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) { |
Lu Fengqi | 6025c19 | 2018-08-01 11:32:29 +0800 | [diff] [blame] | 6448 | ret = btrfs_add_root_ref(trans, key.objectid, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6449 | root->root_key.objectid, parent_ino, |
| 6450 | index, name, name_len); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6451 | } else if (add_backref) { |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6452 | ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino, |
| 6453 | parent_ino, index); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6454 | } |
| 6455 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6456 | /* Nothing to clean up yet */ |
| 6457 | if (ret) |
| 6458 | return ret; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6459 | |
Lu Fengqi | 684572d | 2018-08-04 21:10:57 +0800 | [diff] [blame] | 6460 | ret = btrfs_insert_dir_item(trans, name, name_len, parent_inode, &key, |
Nikolay Borisov | db0a669 | 2017-02-20 13:51:08 +0200 | [diff] [blame] | 6461 | btrfs_inode_type(&inode->vfs_inode), index); |
Chris Mason | 9c52057 | 2012-12-17 14:26:57 -0500 | [diff] [blame] | 6462 | if (ret == -EEXIST || ret == -EOVERFLOW) |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6463 | goto fail_dir_item; |
| 6464 | else if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 6465 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6466 | return ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6467 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6468 | |
Nikolay Borisov | db0a669 | 2017-02-20 13:51:08 +0200 | [diff] [blame] | 6469 | btrfs_i_size_write(parent_inode, parent_inode->vfs_inode.i_size + |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6470 | name_len * 2); |
Nikolay Borisov | db0a669 | 2017-02-20 13:51:08 +0200 | [diff] [blame] | 6471 | inode_inc_iversion(&parent_inode->vfs_inode); |
Filipe Manana | 5338e43 | 2019-05-15 16:02:47 +0100 | [diff] [blame] | 6472 | /* |
| 6473 | * If we are replaying a log tree, we do not want to update the mtime |
| 6474 | * and ctime of the parent directory with the current time, since the |
| 6475 | * log replay procedure is responsible for setting them to their correct |
| 6476 | * values (the ones it had when the fsync was done). |
| 6477 | */ |
| 6478 | if (!test_bit(BTRFS_FS_LOG_RECOVERING, &root->fs_info->flags)) { |
| 6479 | struct timespec64 now = current_time(&parent_inode->vfs_inode); |
| 6480 | |
| 6481 | parent_inode->vfs_inode.i_mtime = now; |
| 6482 | parent_inode->vfs_inode.i_ctime = now; |
| 6483 | } |
Nikolay Borisov | 9a56fcd | 2020-11-02 16:48:59 +0200 | [diff] [blame] | 6484 | ret = btrfs_update_inode(trans, root, parent_inode); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6485 | if (ret) |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 6486 | btrfs_abort_transaction(trans, ret); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6487 | return ret; |
Chris Mason | fe66a05 | 2012-02-20 08:40:56 -0500 | [diff] [blame] | 6488 | |
| 6489 | fail_dir_item: |
| 6490 | if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) { |
| 6491 | u64 local_index; |
| 6492 | int err; |
Lu Fengqi | 3ee1c55 | 2018-08-01 11:32:28 +0800 | [diff] [blame] | 6493 | err = btrfs_del_root_ref(trans, key.objectid, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6494 | root->root_key.objectid, parent_ino, |
| 6495 | &local_index, name, name_len); |
Johannes Thumshirn | 1690dd41 | 2018-12-12 15:14:17 +0100 | [diff] [blame] | 6496 | if (err) |
| 6497 | btrfs_abort_transaction(trans, err); |
Chris Mason | fe66a05 | 2012-02-20 08:40:56 -0500 | [diff] [blame] | 6498 | } else if (add_backref) { |
| 6499 | u64 local_index; |
| 6500 | int err; |
| 6501 | |
| 6502 | err = btrfs_del_inode_ref(trans, root, name, name_len, |
| 6503 | ino, parent_ino, &local_index); |
Johannes Thumshirn | 1690dd41 | 2018-12-12 15:14:17 +0100 | [diff] [blame] | 6504 | if (err) |
| 6505 | btrfs_abort_transaction(trans, err); |
Chris Mason | fe66a05 | 2012-02-20 08:40:56 -0500 | [diff] [blame] | 6506 | } |
Johannes Thumshirn | 1690dd41 | 2018-12-12 15:14:17 +0100 | [diff] [blame] | 6507 | |
| 6508 | /* Return the original error code */ |
Chris Mason | fe66a05 | 2012-02-20 08:40:56 -0500 | [diff] [blame] | 6509 | return ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6510 | } |
| 6511 | |
| 6512 | static int btrfs_add_nondir(struct btrfs_trans_handle *trans, |
Nikolay Borisov | cef415a | 2017-02-20 13:51:09 +0200 | [diff] [blame] | 6513 | struct btrfs_inode *dir, struct dentry *dentry, |
| 6514 | struct btrfs_inode *inode, int backref, u64 index) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6515 | { |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 6516 | int err = btrfs_add_link(trans, dir, inode, |
| 6517 | dentry->d_name.name, dentry->d_name.len, |
| 6518 | backref, index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6519 | if (err > 0) |
| 6520 | err = -EEXIST; |
| 6521 | return err; |
| 6522 | } |
| 6523 | |
Christian Brauner | 549c729 | 2021-01-21 14:19:43 +0100 | [diff] [blame] | 6524 | static int btrfs_mknod(struct user_namespace *mnt_userns, struct inode *dir, |
| 6525 | struct dentry *dentry, umode_t mode, dev_t rdev) |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6526 | { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6527 | struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6528 | struct btrfs_trans_handle *trans; |
| 6529 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 6530 | struct inode *inode = NULL; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6531 | int err; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6532 | u64 objectid; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 6533 | u64 index = 0; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6534 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 6535 | /* |
| 6536 | * 2 for inode item and ref |
| 6537 | * 2 for dir items |
| 6538 | * 1 for xattr if selinux is on |
| 6539 | */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6540 | trans = btrfs_start_transaction(root, 5); |
| 6541 | if (IS_ERR(trans)) |
| 6542 | return PTR_ERR(trans); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 6543 | |
Nikolay Borisov | 543068a | 2020-12-07 17:32:33 +0200 | [diff] [blame] | 6544 | err = btrfs_get_free_objectid(root, &objectid); |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 6545 | if (err) |
| 6546 | goto out_unlock; |
| 6547 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6548 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 6549 | dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid, |
| 6550 | mode, &index); |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 6551 | if (IS_ERR(inode)) { |
| 6552 | err = PTR_ERR(inode); |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6553 | inode = NULL; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6554 | goto out_unlock; |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 6555 | } |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6556 | |
Casey Schaufler | ad19db7 | 2011-12-15 10:09:07 -0500 | [diff] [blame] | 6557 | /* |
| 6558 | * If the active LSM wants to access the inode during |
| 6559 | * d_instantiate it needs these. Smack checks to see |
| 6560 | * if the filesystem supports xattrs by looking at the |
| 6561 | * ops vector. |
| 6562 | */ |
Casey Schaufler | ad19db7 | 2011-12-15 10:09:07 -0500 | [diff] [blame] | 6563 | inode->i_op = &btrfs_special_inode_operations; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6564 | init_special_inode(inode, inode->i_mode, rdev); |
| 6565 | |
| 6566 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6567 | if (err) |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6568 | goto out_unlock; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6569 | |
Nikolay Borisov | cef415a | 2017-02-20 13:51:09 +0200 | [diff] [blame] | 6570 | err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode), |
| 6571 | 0, index); |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6572 | if (err) |
| 6573 | goto out_unlock; |
| 6574 | |
Nikolay Borisov | 9a56fcd | 2020-11-02 16:48:59 +0200 | [diff] [blame] | 6575 | btrfs_update_inode(trans, root, BTRFS_I(inode)); |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6576 | d_instantiate_new(dentry, inode); |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6577 | |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6578 | out_unlock: |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 6579 | btrfs_end_transaction(trans); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6580 | btrfs_btree_balance_dirty(fs_info); |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6581 | if (err && inode) { |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6582 | inode_dec_link_count(inode); |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6583 | discard_new_inode(inode); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6584 | } |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6585 | return err; |
| 6586 | } |
| 6587 | |
Christian Brauner | 549c729 | 2021-01-21 14:19:43 +0100 | [diff] [blame] | 6588 | static int btrfs_create(struct user_namespace *mnt_userns, struct inode *dir, |
| 6589 | struct dentry *dentry, umode_t mode, bool excl) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6590 | { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6591 | struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6592 | struct btrfs_trans_handle *trans; |
| 6593 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 6594 | struct inode *inode = NULL; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6595 | int err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6596 | u64 objectid; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 6597 | u64 index = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6598 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 6599 | /* |
| 6600 | * 2 for inode item and ref |
| 6601 | * 2 for dir items |
| 6602 | * 1 for xattr if selinux is on |
| 6603 | */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6604 | trans = btrfs_start_transaction(root, 5); |
| 6605 | if (IS_ERR(trans)) |
| 6606 | return PTR_ERR(trans); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 6607 | |
Nikolay Borisov | 543068a | 2020-12-07 17:32:33 +0200 | [diff] [blame] | 6608 | err = btrfs_get_free_objectid(root, &objectid); |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 6609 | if (err) |
| 6610 | goto out_unlock; |
| 6611 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6612 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 6613 | dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid, |
| 6614 | mode, &index); |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 6615 | if (IS_ERR(inode)) { |
| 6616 | err = PTR_ERR(inode); |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6617 | inode = NULL; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6618 | goto out_unlock; |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 6619 | } |
Casey Schaufler | ad19db7 | 2011-12-15 10:09:07 -0500 | [diff] [blame] | 6620 | /* |
| 6621 | * If the active LSM wants to access the inode during |
| 6622 | * d_instantiate it needs these. Smack checks to see |
| 6623 | * if the filesystem supports xattrs by looking at the |
| 6624 | * ops vector. |
| 6625 | */ |
| 6626 | inode->i_fop = &btrfs_file_operations; |
| 6627 | inode->i_op = &btrfs_file_inode_operations; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6628 | inode->i_mapping->a_ops = &btrfs_aops; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6629 | |
| 6630 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); |
| 6631 | if (err) |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6632 | goto out_unlock; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6633 | |
Nikolay Borisov | 9a56fcd | 2020-11-02 16:48:59 +0200 | [diff] [blame] | 6634 | err = btrfs_update_inode(trans, root, BTRFS_I(inode)); |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6635 | if (err) |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6636 | goto out_unlock; |
Casey Schaufler | ad19db7 | 2011-12-15 10:09:07 -0500 | [diff] [blame] | 6637 | |
Nikolay Borisov | cef415a | 2017-02-20 13:51:09 +0200 | [diff] [blame] | 6638 | err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode), |
| 6639 | 0, index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6640 | if (err) |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6641 | goto out_unlock; |
Filipe Brandenburger | 43baa57 | 2012-11-30 03:40:09 +0000 | [diff] [blame] | 6642 | |
Al Viro | 1e2e547 | 2018-05-04 08:23:01 -0400 | [diff] [blame] | 6643 | d_instantiate_new(dentry, inode); |
Filipe Brandenburger | 43baa57 | 2012-11-30 03:40:09 +0000 | [diff] [blame] | 6644 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6645 | out_unlock: |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 6646 | btrfs_end_transaction(trans); |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6647 | if (err && inode) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6648 | inode_dec_link_count(inode); |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6649 | discard_new_inode(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6650 | } |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6651 | btrfs_btree_balance_dirty(fs_info); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6652 | return err; |
| 6653 | } |
| 6654 | |
| 6655 | static int btrfs_link(struct dentry *old_dentry, struct inode *dir, |
| 6656 | struct dentry *dentry) |
| 6657 | { |
Filipe Manana | 271dba45 | 2016-01-05 16:24:05 +0000 | [diff] [blame] | 6658 | struct btrfs_trans_handle *trans = NULL; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6659 | struct btrfs_root *root = BTRFS_I(dir)->root; |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 6660 | struct inode *inode = d_inode(old_dentry); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6661 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 6662 | u64 index; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6663 | int err; |
| 6664 | int drop_inode = 0; |
| 6665 | |
TARUISI Hiroaki | 4a8be42 | 2009-11-12 07:14:26 +0000 | [diff] [blame] | 6666 | /* 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] | 6667 | if (root->root_key.objectid != BTRFS_I(inode)->root->root_key.objectid) |
Mark Fasheh | 3ab3564 | 2011-03-22 17:20:26 +0000 | [diff] [blame] | 6668 | return -EXDEV; |
TARUISI Hiroaki | 4a8be42 | 2009-11-12 07:14:26 +0000 | [diff] [blame] | 6669 | |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 6670 | if (inode->i_nlink >= BTRFS_LINK_MAX) |
Al Viro | c055e99 | 2011-03-04 17:15:18 +0000 | [diff] [blame] | 6671 | return -EMLINK; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 6672 | |
Nikolay Borisov | 877574e | 2017-02-20 13:50:33 +0200 | [diff] [blame] | 6673 | err = btrfs_set_inode_index(BTRFS_I(dir), &index); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6674 | if (err) |
| 6675 | goto fail; |
| 6676 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6677 | /* |
Miao Xie | 7e6b646 | 2011-02-18 09:21:17 +0000 | [diff] [blame] | 6678 | * 2 items for inode and inode ref |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6679 | * 2 items for dir items |
Miao Xie | 7e6b646 | 2011-02-18 09:21:17 +0000 | [diff] [blame] | 6680 | * 1 item for parent inode |
Omar Sandoval | 399b0bb | 2018-05-11 13:13:40 -0700 | [diff] [blame] | 6681 | * 1 item for orphan item deletion if O_TMPFILE |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6682 | */ |
Omar Sandoval | 399b0bb | 2018-05-11 13:13:40 -0700 | [diff] [blame] | 6683 | trans = btrfs_start_transaction(root, inode->i_nlink ? 5 : 6); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6684 | if (IS_ERR(trans)) { |
| 6685 | err = PTR_ERR(trans); |
Filipe Manana | 271dba45 | 2016-01-05 16:24:05 +0000 | [diff] [blame] | 6686 | trans = NULL; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6687 | goto fail; |
| 6688 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6689 | |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 6690 | /* There are several dir indexes for this inode, clear the cache. */ |
| 6691 | BTRFS_I(inode)->dir_index = 0ULL; |
Zach Brown | 8b558c5 | 2013-10-16 12:10:34 -0700 | [diff] [blame] | 6692 | inc_nlink(inode); |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 6693 | inode_inc_iversion(inode); |
Deepa Dinamani | c2050a4 | 2016-09-14 07:48:06 -0700 | [diff] [blame] | 6694 | inode->i_ctime = current_time(inode); |
Al Viro | 7de9c6ee | 2010-10-23 11:11:40 -0400 | [diff] [blame] | 6695 | ihold(inode); |
Josef Bacik | e997615 | 2012-10-11 15:53:56 -0400 | [diff] [blame] | 6696 | set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6697 | |
Nikolay Borisov | cef415a | 2017-02-20 13:51:09 +0200 | [diff] [blame] | 6698 | err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode), |
| 6699 | 1, index); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6700 | |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 6701 | if (err) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6702 | drop_inode = 1; |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 6703 | } else { |
Al Viro | 10d9f30 | 2011-07-16 23:09:10 -0400 | [diff] [blame] | 6704 | struct dentry *parent = dentry->d_parent; |
Filipe Manana | d4682ba | 2018-06-11 19:24:28 +0100 | [diff] [blame] | 6705 | |
Nikolay Borisov | 9a56fcd | 2020-11-02 16:48:59 +0200 | [diff] [blame] | 6706 | err = btrfs_update_inode(trans, root, BTRFS_I(inode)); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6707 | if (err) |
| 6708 | goto fail; |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 6709 | if (inode->i_nlink == 1) { |
| 6710 | /* |
| 6711 | * If new hard link count is 1, it's a file created |
| 6712 | * with open(2) O_TMPFILE flag. |
| 6713 | */ |
Nikolay Borisov | 3d6ae7b | 2017-02-20 13:50:58 +0200 | [diff] [blame] | 6714 | err = btrfs_orphan_del(trans, BTRFS_I(inode)); |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 6715 | if (err) |
| 6716 | goto fail; |
| 6717 | } |
Al Viro | 08c422c | 2011-12-23 07:58:13 -0500 | [diff] [blame] | 6718 | d_instantiate(dentry, inode); |
Filipe Manana | 75b463d | 2020-08-11 12:43:48 +0100 | [diff] [blame] | 6719 | btrfs_log_new_name(trans, BTRFS_I(inode), NULL, parent); |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 6720 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6721 | |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 6722 | fail: |
Filipe Manana | 271dba45 | 2016-01-05 16:24:05 +0000 | [diff] [blame] | 6723 | if (trans) |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 6724 | btrfs_end_transaction(trans); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6725 | if (drop_inode) { |
| 6726 | inode_dec_link_count(inode); |
| 6727 | iput(inode); |
| 6728 | } |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6729 | btrfs_btree_balance_dirty(fs_info); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6730 | return err; |
| 6731 | } |
| 6732 | |
Christian Brauner | 549c729 | 2021-01-21 14:19:43 +0100 | [diff] [blame] | 6733 | static int btrfs_mkdir(struct user_namespace *mnt_userns, struct inode *dir, |
| 6734 | struct dentry *dentry, umode_t mode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6735 | { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6736 | struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); |
Chris Mason | b9d8666 | 2008-05-02 16:13:49 -0400 | [diff] [blame] | 6737 | struct inode *inode = NULL; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6738 | struct btrfs_trans_handle *trans; |
| 6739 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 6740 | int err = 0; |
Chris Mason | b9d8666 | 2008-05-02 16:13:49 -0400 | [diff] [blame] | 6741 | u64 objectid = 0; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 6742 | u64 index = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6743 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 6744 | /* |
| 6745 | * 2 items for inode and ref |
| 6746 | * 2 items for dir items |
| 6747 | * 1 for xattr if selinux is on |
| 6748 | */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6749 | trans = btrfs_start_transaction(root, 5); |
| 6750 | if (IS_ERR(trans)) |
| 6751 | return PTR_ERR(trans); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6752 | |
Nikolay Borisov | 543068a | 2020-12-07 17:32:33 +0200 | [diff] [blame] | 6753 | err = btrfs_get_free_objectid(root, &objectid); |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 6754 | if (err) |
| 6755 | goto out_fail; |
| 6756 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6757 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 6758 | dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid, |
| 6759 | S_IFDIR | mode, &index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6760 | if (IS_ERR(inode)) { |
| 6761 | err = PTR_ERR(inode); |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6762 | inode = NULL; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6763 | goto out_fail; |
| 6764 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6765 | |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6766 | /* these must be set before we unlock the inode */ |
| 6767 | inode->i_op = &btrfs_dir_inode_operations; |
| 6768 | inode->i_fop = &btrfs_dir_file_operations; |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 6769 | |
Eric Paris | 2a7dba3 | 2011-02-01 11:05:39 -0500 | [diff] [blame] | 6770 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 6771 | if (err) |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6772 | goto out_fail; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6773 | |
Nikolay Borisov | 6ef06d2 | 2017-02-20 13:50:34 +0200 | [diff] [blame] | 6774 | btrfs_i_size_write(BTRFS_I(inode), 0); |
Nikolay Borisov | 9a56fcd | 2020-11-02 16:48:59 +0200 | [diff] [blame] | 6775 | err = btrfs_update_inode(trans, root, BTRFS_I(inode)); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6776 | if (err) |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6777 | goto out_fail; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6778 | |
Nikolay Borisov | db0a669 | 2017-02-20 13:51:08 +0200 | [diff] [blame] | 6779 | err = btrfs_add_link(trans, BTRFS_I(dir), BTRFS_I(inode), |
| 6780 | dentry->d_name.name, |
| 6781 | dentry->d_name.len, 0, index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6782 | if (err) |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6783 | goto out_fail; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6784 | |
Al Viro | 1e2e547 | 2018-05-04 08:23:01 -0400 | [diff] [blame] | 6785 | d_instantiate_new(dentry, inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6786 | |
| 6787 | out_fail: |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 6788 | btrfs_end_transaction(trans); |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6789 | if (err && inode) { |
Wang Shilong | c7cfb8a | 2014-12-24 14:45:30 +0800 | [diff] [blame] | 6790 | inode_dec_link_count(inode); |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6791 | discard_new_inode(inode); |
Wang Shilong | c7cfb8a | 2014-12-24 14:45:30 +0800 | [diff] [blame] | 6792 | } |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6793 | btrfs_btree_balance_dirty(fs_info); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6794 | return err; |
| 6795 | } |
| 6796 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6797 | static noinline int uncompress_inline(struct btrfs_path *path, |
Byongho Lee | e40da0e | 2015-05-19 23:46:45 +0900 | [diff] [blame] | 6798 | struct page *page, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6799 | size_t pg_offset, u64 extent_offset, |
| 6800 | struct btrfs_file_extent_item *item) |
| 6801 | { |
| 6802 | int ret; |
| 6803 | struct extent_buffer *leaf = path->nodes[0]; |
| 6804 | char *tmp; |
| 6805 | size_t max_size; |
| 6806 | unsigned long inline_size; |
| 6807 | unsigned long ptr; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 6808 | int compress_type; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6809 | |
| 6810 | WARN_ON(pg_offset != 0); |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 6811 | compress_type = btrfs_file_extent_compression(leaf, item); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6812 | max_size = btrfs_file_extent_ram_bytes(leaf, item); |
| 6813 | inline_size = btrfs_file_extent_inline_item_len(leaf, |
Ross Kirk | dd3cc16 | 2013-09-16 15:58:09 +0100 | [diff] [blame] | 6814 | btrfs_item_nr(path->slots[0])); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6815 | tmp = kmalloc(inline_size, GFP_NOFS); |
Tsutomu Itoh | 8d41371 | 2011-04-25 19:43:52 -0400 | [diff] [blame] | 6816 | if (!tmp) |
| 6817 | return -ENOMEM; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6818 | ptr = btrfs_file_extent_inline_start(item); |
| 6819 | |
| 6820 | read_extent_buffer(leaf, tmp, ptr, inline_size); |
| 6821 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 6822 | max_size = min_t(unsigned long, PAGE_SIZE, max_size); |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 6823 | ret = btrfs_decompress(compress_type, tmp, page, |
| 6824 | extent_offset, inline_size, max_size); |
Zygo Blaxell | e1699d2 | 2017-03-10 16:45:44 -0500 | [diff] [blame] | 6825 | |
| 6826 | /* |
| 6827 | * decompression code contains a memset to fill in any space between the end |
| 6828 | * of the uncompressed data and the end of max_size in case the decompressed |
| 6829 | * data ends up shorter than ram_bytes. That doesn't cover the hole between |
| 6830 | * the end of an inline extent and the beginning of the next block, so we |
| 6831 | * cover that region here. |
| 6832 | */ |
| 6833 | |
| 6834 | if (max_size + pg_offset < PAGE_SIZE) { |
| 6835 | char *map = kmap(page); |
| 6836 | memset(map + pg_offset + max_size, 0, PAGE_SIZE - max_size - pg_offset); |
| 6837 | kunmap(page); |
| 6838 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6839 | kfree(tmp); |
Zach Brown | 166ae5a | 2014-05-09 17:15:10 -0400 | [diff] [blame] | 6840 | return ret; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6841 | } |
| 6842 | |
Omar Sandoval | 39b07b5 | 2019-12-02 17:34:23 -0800 | [diff] [blame] | 6843 | /** |
| 6844 | * btrfs_get_extent - Lookup the first extent overlapping a range in a file. |
| 6845 | * @inode: file to search in |
| 6846 | * @page: page to read extent data into if the extent is inline |
| 6847 | * @pg_offset: offset into @page to copy to |
| 6848 | * @start: file offset |
| 6849 | * @len: length of range starting at @start |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 6850 | * |
Omar Sandoval | 39b07b5 | 2019-12-02 17:34:23 -0800 | [diff] [blame] | 6851 | * This returns the first &struct extent_map which overlaps with the given |
| 6852 | * range, reading it from the B-tree and caching it if necessary. Note that |
| 6853 | * there may be more extents which overlap the given range after the returned |
| 6854 | * extent_map. |
| 6855 | * |
| 6856 | * If @page is not NULL and the extent is inline, this also reads the extent |
| 6857 | * data directly into the page and marks the extent up to date in the io_tree. |
| 6858 | * |
| 6859 | * Return: ERR_PTR on error, non-NULL extent_map on success. |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 6860 | */ |
Nikolay Borisov | fc4f21b1 | 2017-02-20 13:51:06 +0200 | [diff] [blame] | 6861 | struct extent_map *btrfs_get_extent(struct btrfs_inode *inode, |
Omar Sandoval | 39b07b5 | 2019-12-02 17:34:23 -0800 | [diff] [blame] | 6862 | struct page *page, size_t pg_offset, |
| 6863 | u64 start, u64 len) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6864 | { |
David Sterba | 3ffbd68 | 2018-06-29 10:56:42 +0200 | [diff] [blame] | 6865 | struct btrfs_fs_info *fs_info = inode->root->fs_info; |
Nikolay Borisov | 1028d1c | 2020-08-03 12:58:46 +0300 | [diff] [blame] | 6866 | int ret = 0; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6867 | u64 extent_start = 0; |
| 6868 | u64 extent_end = 0; |
Nikolay Borisov | fc4f21b1 | 2017-02-20 13:51:06 +0200 | [diff] [blame] | 6869 | u64 objectid = btrfs_ino(inode); |
Linus Torvalds | 7e74e23 | 2019-05-01 12:19:20 -0700 | [diff] [blame] | 6870 | int extent_type = -1; |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 6871 | struct btrfs_path *path = NULL; |
Nikolay Borisov | fc4f21b1 | 2017-02-20 13:51:06 +0200 | [diff] [blame] | 6872 | struct btrfs_root *root = inode->root; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6873 | struct btrfs_file_extent_item *item; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6874 | struct extent_buffer *leaf; |
| 6875 | struct btrfs_key found_key; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6876 | struct extent_map *em = NULL; |
Nikolay Borisov | fc4f21b1 | 2017-02-20 13:51:06 +0200 | [diff] [blame] | 6877 | struct extent_map_tree *em_tree = &inode->extent_tree; |
| 6878 | struct extent_io_tree *io_tree = &inode->io_tree; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6879 | |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 6880 | read_lock(&em_tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6881 | em = lookup_extent_mapping(em_tree, start, len); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 6882 | read_unlock(&em_tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6883 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6884 | if (em) { |
Chris Mason | e1c4b74 | 2008-04-22 13:26:46 -0400 | [diff] [blame] | 6885 | if (em->start > start || em->start + em->len <= start) |
| 6886 | free_extent_map(em); |
| 6887 | else if (em->block_start == EXTENT_MAP_INLINE && page) |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 6888 | free_extent_map(em); |
| 6889 | else |
| 6890 | goto out; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6891 | } |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 6892 | em = alloc_extent_map(); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6893 | if (!em) { |
Nikolay Borisov | 1028d1c | 2020-08-03 12:58:46 +0300 | [diff] [blame] | 6894 | ret = -ENOMEM; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6895 | goto out; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6896 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6897 | em->start = EXTENT_MAP_HOLE; |
Chris Mason | 445a694 | 2008-11-10 11:53:33 -0500 | [diff] [blame] | 6898 | em->orig_start = EXTENT_MAP_HOLE; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6899 | em->len = (u64)-1; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6900 | em->block_len = (u64)-1; |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 6901 | |
Liu Bo | bee6ec8 | 2018-08-17 05:05:28 +0800 | [diff] [blame] | 6902 | path = btrfs_alloc_path(); |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 6903 | if (!path) { |
Nikolay Borisov | 1028d1c | 2020-08-03 12:58:46 +0300 | [diff] [blame] | 6904 | ret = -ENOMEM; |
Liu Bo | bee6ec8 | 2018-08-17 05:05:28 +0800 | [diff] [blame] | 6905 | goto out; |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 6906 | } |
| 6907 | |
Liu Bo | bee6ec8 | 2018-08-17 05:05:28 +0800 | [diff] [blame] | 6908 | /* Chances are we'll be called again, so go ahead and do readahead */ |
| 6909 | path->reada = READA_FORWARD; |
Josef Bacik | 4d7240f | 2020-10-23 09:58:09 -0400 | [diff] [blame] | 6910 | |
| 6911 | /* |
| 6912 | * The same explanation in load_free_space_cache applies here as well, |
| 6913 | * we only read when we're loading the free space cache, and at that |
| 6914 | * point the commit_root has everything we need. |
| 6915 | */ |
| 6916 | if (btrfs_is_free_space_inode(inode)) { |
| 6917 | path->search_commit_root = 1; |
| 6918 | path->skip_locking = 1; |
| 6919 | } |
Josef Bacik | 5189941 | 2020-08-20 11:46:01 -0400 | [diff] [blame] | 6920 | |
Nikolay Borisov | 5c9a702 | 2017-12-01 11:19:40 +0200 | [diff] [blame] | 6921 | ret = btrfs_lookup_file_extent(NULL, root, path, objectid, start, 0); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6922 | if (ret < 0) { |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6923 | goto out; |
Nikolay Borisov | b8eeab7 | 2018-12-17 11:49:00 +0200 | [diff] [blame] | 6924 | } else if (ret > 0) { |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6925 | if (path->slots[0] == 0) |
| 6926 | goto not_found; |
| 6927 | path->slots[0]--; |
Nikolay Borisov | 1028d1c | 2020-08-03 12:58:46 +0300 | [diff] [blame] | 6928 | ret = 0; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6929 | } |
| 6930 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6931 | leaf = path->nodes[0]; |
| 6932 | item = btrfs_item_ptr(leaf, path->slots[0], |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6933 | struct btrfs_file_extent_item); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6934 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6935 | if (found_key.objectid != objectid || |
Nikolay Borisov | 694c12e | 2018-12-17 10:35:59 +0200 | [diff] [blame] | 6936 | found_key.type != BTRFS_EXTENT_DATA_KEY) { |
Josef Bacik | 25a5034 | 2013-10-14 12:08:38 -0400 | [diff] [blame] | 6937 | /* |
| 6938 | * If we backup past the first extent we want to move forward |
| 6939 | * and see if there is an extent in front of us, otherwise we'll |
| 6940 | * say there is a hole for our whole search range which can |
| 6941 | * cause problems. |
| 6942 | */ |
| 6943 | extent_end = start; |
| 6944 | goto next; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6945 | } |
| 6946 | |
Nikolay Borisov | 694c12e | 2018-12-17 10:35:59 +0200 | [diff] [blame] | 6947 | extent_type = btrfs_file_extent_type(leaf, item); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6948 | extent_start = found_key.offset; |
Filipe Manana | a5eeb3d | 2020-03-09 12:41:06 +0000 | [diff] [blame] | 6949 | extent_end = btrfs_file_extent_end(path); |
Nikolay Borisov | 694c12e | 2018-12-17 10:35:59 +0200 | [diff] [blame] | 6950 | if (extent_type == BTRFS_FILE_EXTENT_REG || |
| 6951 | extent_type == BTRFS_FILE_EXTENT_PREALLOC) { |
Qu Wenruo | 6bf9e4b | 2019-03-13 13:55:11 +0800 | [diff] [blame] | 6952 | /* Only regular file could have regular/prealloc extent */ |
| 6953 | if (!S_ISREG(inode->vfs_inode.i_mode)) { |
Nikolay Borisov | 1028d1c | 2020-08-03 12:58:46 +0300 | [diff] [blame] | 6954 | ret = -EUCLEAN; |
Qu Wenruo | 6bf9e4b | 2019-03-13 13:55:11 +0800 | [diff] [blame] | 6955 | btrfs_crit(fs_info, |
| 6956 | "regular/prealloc extent found for non-regular inode %llu", |
| 6957 | btrfs_ino(inode)); |
| 6958 | goto out; |
| 6959 | } |
Liu Bo | 09ed2f1 | 2017-03-10 11:09:48 -0800 | [diff] [blame] | 6960 | trace_btrfs_get_extent_show_fi_regular(inode, leaf, item, |
| 6961 | extent_start); |
Nikolay Borisov | 694c12e | 2018-12-17 10:35:59 +0200 | [diff] [blame] | 6962 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
Liu Bo | 09ed2f1 | 2017-03-10 11:09:48 -0800 | [diff] [blame] | 6963 | trace_btrfs_get_extent_show_fi_inline(inode, leaf, item, |
| 6964 | path->slots[0], |
| 6965 | extent_start); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6966 | } |
Josef Bacik | 25a5034 | 2013-10-14 12:08:38 -0400 | [diff] [blame] | 6967 | next: |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6968 | if (start >= extent_end) { |
| 6969 | path->slots[0]++; |
| 6970 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { |
| 6971 | ret = btrfs_next_leaf(root, path); |
Nikolay Borisov | 1028d1c | 2020-08-03 12:58:46 +0300 | [diff] [blame] | 6972 | if (ret < 0) |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6973 | goto out; |
Nikolay Borisov | 1028d1c | 2020-08-03 12:58:46 +0300 | [diff] [blame] | 6974 | else if (ret > 0) |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6975 | goto not_found; |
Nikolay Borisov | 1028d1c | 2020-08-03 12:58:46 +0300 | [diff] [blame] | 6976 | |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6977 | leaf = path->nodes[0]; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6978 | } |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6979 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 6980 | if (found_key.objectid != objectid || |
| 6981 | found_key.type != BTRFS_EXTENT_DATA_KEY) |
| 6982 | goto not_found; |
| 6983 | if (start + len <= found_key.offset) |
| 6984 | goto not_found; |
Wang Shilong | e2eca69 | 2014-07-17 11:44:14 +0800 | [diff] [blame] | 6985 | if (start > found_key.offset) |
| 6986 | goto next; |
Nikolay Borisov | 02a033d | 2018-12-17 10:36:02 +0200 | [diff] [blame] | 6987 | |
| 6988 | /* New extent overlaps with existing one */ |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6989 | em->start = start; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 6990 | em->orig_start = start; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6991 | em->len = found_key.offset - start; |
Nikolay Borisov | 02a033d | 2018-12-17 10:36:02 +0200 | [diff] [blame] | 6992 | em->block_start = EXTENT_MAP_HOLE; |
| 6993 | goto insert; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6994 | } |
| 6995 | |
Omar Sandoval | 39b07b5 | 2019-12-02 17:34:23 -0800 | [diff] [blame] | 6996 | btrfs_extent_item_to_extent_map(inode, path, item, !page, em); |
Filipe Manana | 7ffbb59 | 2014-06-09 03:48:05 +0100 | [diff] [blame] | 6997 | |
Nikolay Borisov | 694c12e | 2018-12-17 10:35:59 +0200 | [diff] [blame] | 6998 | if (extent_type == BTRFS_FILE_EXTENT_REG || |
| 6999 | extent_type == BTRFS_FILE_EXTENT_PREALLOC) { |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7000 | goto insert; |
Nikolay Borisov | 694c12e | 2018-12-17 10:35:59 +0200 | [diff] [blame] | 7001 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 7002 | unsigned long ptr; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7003 | char *map; |
Chris Mason | 3326d1b | 2007-10-15 16:18:25 -0400 | [diff] [blame] | 7004 | size_t size; |
| 7005 | size_t extent_offset; |
| 7006 | size_t copy_size; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7007 | |
Omar Sandoval | 39b07b5 | 2019-12-02 17:34:23 -0800 | [diff] [blame] | 7008 | if (!page) |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 7009 | goto out; |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 7010 | |
Qu Wenruo | e41ca58 | 2018-06-06 15:41:49 +0800 | [diff] [blame] | 7011 | size = btrfs_file_extent_ram_bytes(leaf, item); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 7012 | extent_offset = page_offset(page) + pg_offset - extent_start; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 7013 | copy_size = min_t(u64, PAGE_SIZE - pg_offset, |
| 7014 | size - extent_offset); |
Chris Mason | 3326d1b | 2007-10-15 16:18:25 -0400 | [diff] [blame] | 7015 | em->start = extent_start + extent_offset; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7016 | em->len = ALIGN(copy_size, fs_info->sectorsize); |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 7017 | em->orig_block_len = em->len; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 7018 | em->orig_start = em->start; |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 7019 | ptr = btrfs_file_extent_inline_start(item) + extent_offset; |
Liu Bo | e49aabd | 2018-08-25 13:47:09 +0800 | [diff] [blame] | 7020 | |
Edmund Nadolski | bf46f52 | 2017-11-20 13:24:49 -0700 | [diff] [blame] | 7021 | if (!PageUptodate(page)) { |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 7022 | if (btrfs_file_extent_compression(leaf, item) != |
| 7023 | BTRFS_COMPRESS_NONE) { |
Byongho Lee | e40da0e | 2015-05-19 23:46:45 +0900 | [diff] [blame] | 7024 | ret = uncompress_inline(path, page, pg_offset, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 7025 | extent_offset, item); |
Nikolay Borisov | 1028d1c | 2020-08-03 12:58:46 +0300 | [diff] [blame] | 7026 | if (ret) |
Zach Brown | 166ae5a | 2014-05-09 17:15:10 -0400 | [diff] [blame] | 7027 | goto out; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 7028 | } else { |
| 7029 | map = kmap(page); |
| 7030 | read_extent_buffer(leaf, map + pg_offset, ptr, |
| 7031 | copy_size); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 7032 | if (pg_offset + copy_size < PAGE_SIZE) { |
Chris Mason | 93c82d5 | 2009-09-11 12:36:29 -0400 | [diff] [blame] | 7033 | memset(map + pg_offset + copy_size, 0, |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 7034 | PAGE_SIZE - pg_offset - |
Chris Mason | 93c82d5 | 2009-09-11 12:36:29 -0400 | [diff] [blame] | 7035 | copy_size); |
| 7036 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 7037 | kunmap(page); |
| 7038 | } |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 7039 | flush_dcache_page(page); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7040 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 7041 | set_extent_uptodate(io_tree, em->start, |
Arne Jansen | 507903b | 2011-04-06 10:02:20 +0000 | [diff] [blame] | 7042 | extent_map_end(em) - 1, NULL, GFP_NOFS); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7043 | goto insert; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7044 | } |
| 7045 | not_found: |
| 7046 | em->start = start; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 7047 | em->orig_start = start; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 7048 | em->len = len; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 7049 | em->block_start = EXTENT_MAP_HOLE; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7050 | insert: |
Nikolay Borisov | 1028d1c | 2020-08-03 12:58:46 +0300 | [diff] [blame] | 7051 | ret = 0; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 7052 | btrfs_release_path(path); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 7053 | if (em->start > start || extent_map_end(em) <= start) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7054 | btrfs_err(fs_info, |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 7055 | "bad extent! em: [%llu %llu] passed [%llu %llu]", |
| 7056 | em->start, em->len, start, len); |
Nikolay Borisov | 1028d1c | 2020-08-03 12:58:46 +0300 | [diff] [blame] | 7057 | ret = -EIO; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7058 | goto out; |
| 7059 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 7060 | |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 7061 | write_lock(&em_tree->lock); |
Nikolay Borisov | 1028d1c | 2020-08-03 12:58:46 +0300 | [diff] [blame] | 7062 | ret = btrfs_add_extent_mapping(fs_info, em_tree, &em, start, len); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 7063 | write_unlock(&em_tree->lock); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7064 | out: |
Liu Bo | c641428 | 2018-08-23 07:36:17 +0800 | [diff] [blame] | 7065 | btrfs_free_path(path); |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 7066 | |
Nikolay Borisov | fc4f21b1 | 2017-02-20 13:51:06 +0200 | [diff] [blame] | 7067 | trace_btrfs_get_extent(root, inode, em); |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 7068 | |
Nikolay Borisov | 1028d1c | 2020-08-03 12:58:46 +0300 | [diff] [blame] | 7069 | if (ret) { |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7070 | free_extent_map(em); |
Nikolay Borisov | 1028d1c | 2020-08-03 12:58:46 +0300 | [diff] [blame] | 7071 | return ERR_PTR(ret); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7072 | } |
| 7073 | return em; |
| 7074 | } |
| 7075 | |
Nikolay Borisov | fc4f21b1 | 2017-02-20 13:51:06 +0200 | [diff] [blame] | 7076 | struct extent_map *btrfs_get_extent_fiemap(struct btrfs_inode *inode, |
Nikolay Borisov | 4ab47a8 | 2018-12-12 09:42:32 +0200 | [diff] [blame] | 7077 | u64 start, u64 len) |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 7078 | { |
| 7079 | struct extent_map *em; |
| 7080 | struct extent_map *hole_em = NULL; |
Nikolay Borisov | f3714ef | 2018-12-12 09:42:33 +0200 | [diff] [blame] | 7081 | u64 delalloc_start = start; |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 7082 | u64 end; |
Nikolay Borisov | f3714ef | 2018-12-12 09:42:33 +0200 | [diff] [blame] | 7083 | u64 delalloc_len; |
| 7084 | u64 delalloc_end; |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 7085 | int err = 0; |
| 7086 | |
Omar Sandoval | 39b07b5 | 2019-12-02 17:34:23 -0800 | [diff] [blame] | 7087 | em = btrfs_get_extent(inode, NULL, 0, start, len); |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 7088 | if (IS_ERR(em)) |
| 7089 | return em; |
Dan Carpenter | 9986277 | 2017-04-11 11:57:15 +0300 | [diff] [blame] | 7090 | /* |
| 7091 | * If our em maps to: |
| 7092 | * - a hole or |
| 7093 | * - a pre-alloc extent, |
| 7094 | * there might actually be delalloc bytes behind it. |
| 7095 | */ |
| 7096 | if (em->block_start != EXTENT_MAP_HOLE && |
| 7097 | !test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) |
| 7098 | return em; |
| 7099 | else |
| 7100 | hole_em = em; |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 7101 | |
| 7102 | /* check to see if we've wrapped (len == -1 or similar) */ |
| 7103 | end = start + len; |
| 7104 | if (end < start) |
| 7105 | end = (u64)-1; |
| 7106 | else |
| 7107 | end -= 1; |
| 7108 | |
| 7109 | em = NULL; |
| 7110 | |
| 7111 | /* ok, we didn't find anything, lets look for delalloc */ |
Nikolay Borisov | f3714ef | 2018-12-12 09:42:33 +0200 | [diff] [blame] | 7112 | delalloc_len = count_range_bits(&inode->io_tree, &delalloc_start, |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 7113 | end, len, EXTENT_DELALLOC, 1); |
Nikolay Borisov | f3714ef | 2018-12-12 09:42:33 +0200 | [diff] [blame] | 7114 | delalloc_end = delalloc_start + delalloc_len; |
| 7115 | if (delalloc_end < delalloc_start) |
| 7116 | delalloc_end = (u64)-1; |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 7117 | |
| 7118 | /* |
Nikolay Borisov | f3714ef | 2018-12-12 09:42:33 +0200 | [diff] [blame] | 7119 | * We didn't find anything useful, return the original results from |
| 7120 | * get_extent() |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 7121 | */ |
Nikolay Borisov | f3714ef | 2018-12-12 09:42:33 +0200 | [diff] [blame] | 7122 | if (delalloc_start > end || delalloc_end <= start) { |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 7123 | em = hole_em; |
| 7124 | hole_em = NULL; |
| 7125 | goto out; |
| 7126 | } |
| 7127 | |
Nikolay Borisov | f3714ef | 2018-12-12 09:42:33 +0200 | [diff] [blame] | 7128 | /* |
| 7129 | * Adjust the delalloc_start to make sure it doesn't go backwards from |
| 7130 | * the start they passed in |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 7131 | */ |
Nikolay Borisov | f3714ef | 2018-12-12 09:42:33 +0200 | [diff] [blame] | 7132 | delalloc_start = max(start, delalloc_start); |
| 7133 | delalloc_len = delalloc_end - delalloc_start; |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 7134 | |
Nikolay Borisov | f3714ef | 2018-12-12 09:42:33 +0200 | [diff] [blame] | 7135 | if (delalloc_len > 0) { |
| 7136 | u64 hole_start; |
Nikolay Borisov | 02950af | 2018-12-12 09:42:34 +0200 | [diff] [blame] | 7137 | u64 hole_len; |
Nikolay Borisov | f3714ef | 2018-12-12 09:42:33 +0200 | [diff] [blame] | 7138 | const u64 hole_end = extent_map_end(hole_em); |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 7139 | |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 7140 | em = alloc_extent_map(); |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 7141 | if (!em) { |
| 7142 | err = -ENOMEM; |
| 7143 | goto out; |
| 7144 | } |
Nikolay Borisov | f3714ef | 2018-12-12 09:42:33 +0200 | [diff] [blame] | 7145 | |
| 7146 | ASSERT(hole_em); |
| 7147 | /* |
| 7148 | * When btrfs_get_extent can't find anything it returns one |
| 7149 | * huge hole |
| 7150 | * |
| 7151 | * Make sure what it found really fits our range, and adjust to |
| 7152 | * make sure it is based on the start from the caller |
| 7153 | */ |
| 7154 | if (hole_end <= start || hole_em->start > end) { |
| 7155 | free_extent_map(hole_em); |
| 7156 | hole_em = NULL; |
| 7157 | } else { |
| 7158 | hole_start = max(hole_em->start, start); |
| 7159 | hole_len = hole_end - hole_start; |
| 7160 | } |
| 7161 | |
| 7162 | if (hole_em && delalloc_start > hole_start) { |
| 7163 | /* |
| 7164 | * Our hole starts before our delalloc, so we have to |
| 7165 | * return just the parts of the hole that go until the |
| 7166 | * delalloc starts |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 7167 | */ |
Nikolay Borisov | f3714ef | 2018-12-12 09:42:33 +0200 | [diff] [blame] | 7168 | em->len = min(hole_len, delalloc_start - hole_start); |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 7169 | em->start = hole_start; |
| 7170 | em->orig_start = hole_start; |
| 7171 | /* |
Nikolay Borisov | f3714ef | 2018-12-12 09:42:33 +0200 | [diff] [blame] | 7172 | * Don't adjust block start at all, it is fixed at |
| 7173 | * EXTENT_MAP_HOLE |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 7174 | */ |
| 7175 | em->block_start = hole_em->block_start; |
| 7176 | em->block_len = hole_len; |
Liu Bo | f9e4fb5 | 2013-01-07 10:10:12 +0000 | [diff] [blame] | 7177 | if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags)) |
| 7178 | set_bit(EXTENT_FLAG_PREALLOC, &em->flags); |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 7179 | } else { |
Nikolay Borisov | f3714ef | 2018-12-12 09:42:33 +0200 | [diff] [blame] | 7180 | /* |
| 7181 | * Hole is out of passed range or it starts after |
| 7182 | * delalloc range |
| 7183 | */ |
| 7184 | em->start = delalloc_start; |
| 7185 | em->len = delalloc_len; |
| 7186 | em->orig_start = delalloc_start; |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 7187 | em->block_start = EXTENT_MAP_DELALLOC; |
Nikolay Borisov | f3714ef | 2018-12-12 09:42:33 +0200 | [diff] [blame] | 7188 | em->block_len = delalloc_len; |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 7189 | } |
Nikolay Borisov | bf8d32b | 2017-12-01 11:19:43 +0200 | [diff] [blame] | 7190 | } else { |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 7191 | return hole_em; |
| 7192 | } |
| 7193 | out: |
| 7194 | |
| 7195 | free_extent_map(hole_em); |
| 7196 | if (err) { |
| 7197 | free_extent_map(em); |
| 7198 | return ERR_PTR(err); |
| 7199 | } |
| 7200 | return em; |
| 7201 | } |
| 7202 | |
Nikolay Borisov | 64f5418 | 2020-06-03 08:55:31 +0300 | [diff] [blame] | 7203 | static struct extent_map *btrfs_create_dio_extent(struct btrfs_inode *inode, |
Filipe Manana | 5f9a8a5 | 2016-05-12 13:53:36 +0100 | [diff] [blame] | 7204 | const u64 start, |
| 7205 | const u64 len, |
| 7206 | const u64 orig_start, |
| 7207 | const u64 block_start, |
| 7208 | const u64 block_len, |
| 7209 | const u64 orig_block_len, |
| 7210 | const u64 ram_bytes, |
| 7211 | const int type) |
| 7212 | { |
| 7213 | struct extent_map *em = NULL; |
| 7214 | int ret; |
| 7215 | |
Filipe Manana | 5f9a8a5 | 2016-05-12 13:53:36 +0100 | [diff] [blame] | 7216 | if (type != BTRFS_ORDERED_NOCOW) { |
Nikolay Borisov | 64f5418 | 2020-06-03 08:55:31 +0300 | [diff] [blame] | 7217 | em = create_io_em(inode, start, len, orig_start, block_start, |
| 7218 | block_len, orig_block_len, ram_bytes, |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 7219 | BTRFS_COMPRESS_NONE, /* compress_type */ |
| 7220 | type); |
Filipe Manana | 5f9a8a5 | 2016-05-12 13:53:36 +0100 | [diff] [blame] | 7221 | if (IS_ERR(em)) |
| 7222 | goto out; |
| 7223 | } |
Nikolay Borisov | 64f5418 | 2020-06-03 08:55:31 +0300 | [diff] [blame] | 7224 | ret = btrfs_add_ordered_extent_dio(inode, start, block_start, len, |
| 7225 | block_len, type); |
Filipe Manana | 5f9a8a5 | 2016-05-12 13:53:36 +0100 | [diff] [blame] | 7226 | if (ret) { |
| 7227 | if (em) { |
| 7228 | free_extent_map(em); |
Nikolay Borisov | 64f5418 | 2020-06-03 08:55:31 +0300 | [diff] [blame] | 7229 | btrfs_drop_extent_cache(inode, start, start + len - 1, 0); |
Filipe Manana | 5f9a8a5 | 2016-05-12 13:53:36 +0100 | [diff] [blame] | 7230 | } |
| 7231 | em = ERR_PTR(ret); |
| 7232 | } |
| 7233 | out: |
Filipe Manana | 5f9a8a5 | 2016-05-12 13:53:36 +0100 | [diff] [blame] | 7234 | |
| 7235 | return em; |
| 7236 | } |
| 7237 | |
Nikolay Borisov | 9fc6f91 | 2020-06-03 08:55:32 +0300 | [diff] [blame] | 7238 | static struct extent_map *btrfs_new_extent_direct(struct btrfs_inode *inode, |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7239 | u64 start, u64 len) |
| 7240 | { |
Nikolay Borisov | 9fc6f91 | 2020-06-03 08:55:32 +0300 | [diff] [blame] | 7241 | struct btrfs_root *root = inode->root; |
| 7242 | struct btrfs_fs_info *fs_info = root->fs_info; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 7243 | struct extent_map *em; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7244 | struct btrfs_key ins; |
| 7245 | u64 alloc_hint; |
| 7246 | int ret; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7247 | |
Nikolay Borisov | 9fc6f91 | 2020-06-03 08:55:32 +0300 | [diff] [blame] | 7248 | alloc_hint = get_extent_allocation_hint(inode, start, len); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7249 | ret = btrfs_reserve_extent(root, len, len, fs_info->sectorsize, |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 7250 | 0, alloc_hint, &ins, 1, 1); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 7251 | if (ret) |
| 7252 | return ERR_PTR(ret); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7253 | |
Nikolay Borisov | 9fc6f91 | 2020-06-03 08:55:32 +0300 | [diff] [blame] | 7254 | em = btrfs_create_dio_extent(inode, start, ins.offset, start, |
Filipe Manana | 5f9a8a5 | 2016-05-12 13:53:36 +0100 | [diff] [blame] | 7255 | ins.objectid, ins.offset, ins.offset, |
Liu Bo | 6288d6e | 2017-02-21 12:12:58 -0800 | [diff] [blame] | 7256 | ins.offset, BTRFS_ORDERED_REGULAR); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7257 | btrfs_dec_block_group_reservations(fs_info, ins.objectid); |
Filipe Manana | 5f9a8a5 | 2016-05-12 13:53:36 +0100 | [diff] [blame] | 7258 | if (IS_ERR(em)) |
Nikolay Borisov | 9fc6f91 | 2020-06-03 08:55:32 +0300 | [diff] [blame] | 7259 | btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, |
| 7260 | 1); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 7261 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7262 | return em; |
| 7263 | } |
| 7264 | |
Anand Jain | f463963 | 2021-02-10 21:25:16 -0800 | [diff] [blame^] | 7265 | 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] | 7266 | { |
| 7267 | struct btrfs_block_group *block_group; |
Anand Jain | f463963 | 2021-02-10 21:25:16 -0800 | [diff] [blame^] | 7268 | bool readonly = false; |
Anand Jain | 05947ae | 2021-02-10 21:25:15 -0800 | [diff] [blame] | 7269 | |
| 7270 | block_group = btrfs_lookup_block_group(fs_info, bytenr); |
| 7271 | if (!block_group || block_group->ro) |
Anand Jain | f463963 | 2021-02-10 21:25:16 -0800 | [diff] [blame^] | 7272 | readonly = true; |
Anand Jain | 05947ae | 2021-02-10 21:25:15 -0800 | [diff] [blame] | 7273 | if (block_group) |
| 7274 | btrfs_put_block_group(block_group); |
| 7275 | return readonly; |
| 7276 | } |
| 7277 | |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7278 | /* |
Qu Wenruo | e4ecaf9 | 2020-06-24 07:23:51 +0800 | [diff] [blame] | 7279 | * Check if we can do nocow write into the range [@offset, @offset + @len) |
| 7280 | * |
| 7281 | * @offset: File offset |
| 7282 | * @len: The length to write, will be updated to the nocow writeable |
| 7283 | * range |
| 7284 | * @orig_start: (optional) Return the original file offset of the file extent |
| 7285 | * @orig_len: (optional) Return the original on-disk length of the file extent |
| 7286 | * @ram_bytes: (optional) Return the ram_bytes of the file extent |
Boris Burkov | a84d5d4 | 2020-08-18 11:00:05 -0700 | [diff] [blame] | 7287 | * @strict: if true, omit optimizations that might force us into unnecessary |
| 7288 | * cow. e.g., don't trust generation number. |
Qu Wenruo | e4ecaf9 | 2020-06-24 07:23:51 +0800 | [diff] [blame] | 7289 | * |
Qu Wenruo | e4ecaf9 | 2020-06-24 07:23:51 +0800 | [diff] [blame] | 7290 | * Return: |
| 7291 | * >0 and update @len if we can do nocow write |
| 7292 | * 0 if we can't do nocow write |
| 7293 | * <0 if error happened |
| 7294 | * |
| 7295 | * NOTE: This only checks the file extents, caller is responsible to wait for |
| 7296 | * any ordered extents. |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7297 | */ |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 7298 | noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len, |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 7299 | u64 *orig_start, u64 *orig_block_len, |
Boris Burkov | a84d5d4 | 2020-08-18 11:00:05 -0700 | [diff] [blame] | 7300 | u64 *ram_bytes, bool strict) |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7301 | { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 7302 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7303 | struct btrfs_path *path; |
| 7304 | int ret; |
| 7305 | struct extent_buffer *leaf; |
| 7306 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Miao Xie | 7b2b708 | 2014-02-27 13:58:05 +0800 | [diff] [blame] | 7307 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7308 | struct btrfs_file_extent_item *fi; |
| 7309 | struct btrfs_key key; |
| 7310 | u64 disk_bytenr; |
| 7311 | u64 backref_offset; |
| 7312 | u64 extent_end; |
| 7313 | u64 num_bytes; |
| 7314 | int slot; |
| 7315 | int found_type; |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 7316 | bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW); |
Miao Xie | e77751a | 2013-12-27 21:11:50 +0800 | [diff] [blame] | 7317 | |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7318 | path = btrfs_alloc_path(); |
| 7319 | if (!path) |
| 7320 | return -ENOMEM; |
| 7321 | |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 7322 | ret = btrfs_lookup_file_extent(NULL, root, path, |
| 7323 | btrfs_ino(BTRFS_I(inode)), offset, 0); |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7324 | if (ret < 0) |
| 7325 | goto out; |
| 7326 | |
| 7327 | slot = path->slots[0]; |
| 7328 | if (ret == 1) { |
| 7329 | if (slot == 0) { |
| 7330 | /* can't find the item, must cow */ |
| 7331 | ret = 0; |
| 7332 | goto out; |
| 7333 | } |
| 7334 | slot--; |
| 7335 | } |
| 7336 | ret = 0; |
| 7337 | leaf = path->nodes[0]; |
| 7338 | btrfs_item_key_to_cpu(leaf, &key, slot); |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 7339 | if (key.objectid != btrfs_ino(BTRFS_I(inode)) || |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7340 | key.type != BTRFS_EXTENT_DATA_KEY) { |
| 7341 | /* not our file or wrong item type, must cow */ |
| 7342 | goto out; |
| 7343 | } |
| 7344 | |
| 7345 | if (key.offset > offset) { |
| 7346 | /* Wrong offset, must cow */ |
| 7347 | goto out; |
| 7348 | } |
| 7349 | |
| 7350 | fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item); |
| 7351 | found_type = btrfs_file_extent_type(leaf, fi); |
| 7352 | if (found_type != BTRFS_FILE_EXTENT_REG && |
| 7353 | found_type != BTRFS_FILE_EXTENT_PREALLOC) { |
| 7354 | /* not a regular extent, must cow */ |
| 7355 | goto out; |
| 7356 | } |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 7357 | |
| 7358 | if (!nocow && found_type == BTRFS_FILE_EXTENT_REG) |
| 7359 | goto out; |
| 7360 | |
Miao Xie | e77751a | 2013-12-27 21:11:50 +0800 | [diff] [blame] | 7361 | extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi); |
| 7362 | if (extent_end <= offset) |
| 7363 | goto out; |
| 7364 | |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7365 | disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 7366 | if (disk_bytenr == 0) |
| 7367 | goto out; |
| 7368 | |
| 7369 | if (btrfs_file_extent_compression(leaf, fi) || |
| 7370 | btrfs_file_extent_encryption(leaf, fi) || |
| 7371 | btrfs_file_extent_other_encoding(leaf, fi)) |
| 7372 | goto out; |
| 7373 | |
Ethan Lien | 78d4295 | 2018-05-17 14:58:29 +0800 | [diff] [blame] | 7374 | /* |
| 7375 | * Do the same check as in btrfs_cross_ref_exist but without the |
| 7376 | * unnecessary search. |
| 7377 | */ |
Boris Burkov | a84d5d4 | 2020-08-18 11:00:05 -0700 | [diff] [blame] | 7378 | if (!strict && |
| 7379 | (btrfs_file_extent_generation(leaf, fi) <= |
| 7380 | btrfs_root_last_snapshot(&root->root_item))) |
Ethan Lien | 78d4295 | 2018-05-17 14:58:29 +0800 | [diff] [blame] | 7381 | goto out; |
| 7382 | |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7383 | backref_offset = btrfs_file_extent_offset(leaf, fi); |
| 7384 | |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 7385 | if (orig_start) { |
| 7386 | *orig_start = key.offset - backref_offset; |
| 7387 | *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi); |
| 7388 | *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi); |
| 7389 | } |
Josef Bacik | eb384b5 | 2013-04-24 16:32:55 -0400 | [diff] [blame] | 7390 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 7391 | if (btrfs_extent_readonly(fs_info, disk_bytenr)) |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7392 | goto out; |
Miao Xie | 7b2b708 | 2014-02-27 13:58:05 +0800 | [diff] [blame] | 7393 | |
| 7394 | num_bytes = min(offset + *len, extent_end) - offset; |
| 7395 | if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) { |
| 7396 | u64 range_end; |
| 7397 | |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 7398 | range_end = round_up(offset + num_bytes, |
| 7399 | root->fs_info->sectorsize) - 1; |
Miao Xie | 7b2b708 | 2014-02-27 13:58:05 +0800 | [diff] [blame] | 7400 | ret = test_range_bit(io_tree, offset, range_end, |
| 7401 | EXTENT_DELALLOC, 0, NULL); |
| 7402 | if (ret) { |
| 7403 | ret = -EAGAIN; |
| 7404 | goto out; |
| 7405 | } |
| 7406 | } |
| 7407 | |
Josef Bacik | 1bda19e | 2013-10-18 12:10:36 -0400 | [diff] [blame] | 7408 | btrfs_release_path(path); |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7409 | |
| 7410 | /* |
| 7411 | * look for other files referencing this extent, if we |
| 7412 | * find any we must cow |
| 7413 | */ |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 7414 | |
Liu Bo | e4c3b2d | 2017-01-30 12:25:28 -0800 | [diff] [blame] | 7415 | ret = btrfs_cross_ref_exist(root, btrfs_ino(BTRFS_I(inode)), |
Boris Burkov | a84d5d4 | 2020-08-18 11:00:05 -0700 | [diff] [blame] | 7416 | key.offset - backref_offset, disk_bytenr, |
| 7417 | strict); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 7418 | if (ret) { |
| 7419 | ret = 0; |
| 7420 | goto out; |
| 7421 | } |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7422 | |
| 7423 | /* |
| 7424 | * adjust disk_bytenr and num_bytes to cover just the bytes |
| 7425 | * in this extent we are about to write. If there |
| 7426 | * are any csums in that range we have to cow in order |
| 7427 | * to keep the csums correct |
| 7428 | */ |
| 7429 | disk_bytenr += backref_offset; |
| 7430 | disk_bytenr += offset - key.offset; |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 7431 | if (csum_exist_in_range(fs_info, disk_bytenr, num_bytes)) |
| 7432 | goto out; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7433 | /* |
| 7434 | * all of the above have passed, it is safe to overwrite this extent |
| 7435 | * without cow |
| 7436 | */ |
Josef Bacik | eb384b5 | 2013-04-24 16:32:55 -0400 | [diff] [blame] | 7437 | *len = num_bytes; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7438 | ret = 1; |
| 7439 | out: |
| 7440 | btrfs_free_path(path); |
| 7441 | return ret; |
| 7442 | } |
| 7443 | |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7444 | static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend, |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 7445 | struct extent_state **cached_state, bool writing) |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7446 | { |
| 7447 | struct btrfs_ordered_extent *ordered; |
| 7448 | int ret = 0; |
| 7449 | |
| 7450 | while (1) { |
| 7451 | lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend, |
David Sterba | ff13db4 | 2015-12-03 14:30:40 +0100 | [diff] [blame] | 7452 | cached_state); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7453 | /* |
| 7454 | * 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] | 7455 | * 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] | 7456 | * extents in this range. |
| 7457 | */ |
Nikolay Borisov | a776c6f | 2017-02-20 13:50:49 +0200 | [diff] [blame] | 7458 | ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), lockstart, |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7459 | lockend - lockstart + 1); |
| 7460 | |
| 7461 | /* |
| 7462 | * We need to make sure there are no buffered pages in this |
| 7463 | * range either, we could have raced between the invalidate in |
| 7464 | * generic_file_direct_write and locking the extent. The |
| 7465 | * invalidate needs to happen so that reads after a write do not |
| 7466 | * get stale data. |
| 7467 | */ |
Alex Gartrell | fc4adbff | 2014-05-20 13:07:56 -0700 | [diff] [blame] | 7468 | if (!ordered && |
David Sterba | 051c98e | 2018-03-07 15:33:22 +0100 | [diff] [blame] | 7469 | (!writing || !filemap_range_has_page(inode->i_mapping, |
| 7470 | lockstart, lockend))) |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7471 | break; |
| 7472 | |
| 7473 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend, |
David Sterba | e43bbe5 | 2017-12-12 21:43:52 +0100 | [diff] [blame] | 7474 | cached_state); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7475 | |
| 7476 | if (ordered) { |
Filipe Manana | ade7702 | 2016-02-18 14:28:55 +0000 | [diff] [blame] | 7477 | /* |
| 7478 | * If we are doing a DIO read and the ordered extent we |
| 7479 | * found is for a buffered write, we can not wait for it |
| 7480 | * to complete and retry, because if we do so we can |
| 7481 | * deadlock with concurrent buffered writes on page |
| 7482 | * locks. This happens only if our DIO read covers more |
| 7483 | * than one extent map, if at this point has already |
| 7484 | * created an ordered extent for a previous extent map |
| 7485 | * and locked its range in the inode's io tree, and a |
| 7486 | * concurrent write against that previous extent map's |
| 7487 | * range and this range started (we unlock the ranges |
| 7488 | * in the io tree only when the bios complete and |
| 7489 | * buffered writes always lock pages before attempting |
| 7490 | * to lock range in the io tree). |
| 7491 | */ |
| 7492 | if (writing || |
| 7493 | test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags)) |
Nikolay Borisov | c0a4360 | 2020-09-18 12:15:53 +0300 | [diff] [blame] | 7494 | btrfs_start_ordered_extent(ordered, 1); |
Filipe Manana | ade7702 | 2016-02-18 14:28:55 +0000 | [diff] [blame] | 7495 | else |
| 7496 | ret = -ENOTBLK; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7497 | btrfs_put_ordered_extent(ordered); |
| 7498 | } else { |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7499 | /* |
Filipe Manana | b850ae1 | 2015-12-08 16:23:16 +0000 | [diff] [blame] | 7500 | * We could trigger writeback for this range (and wait |
| 7501 | * for it to complete) and then invalidate the pages for |
| 7502 | * this range (through invalidate_inode_pages2_range()), |
| 7503 | * but that can lead us to a deadlock with a concurrent |
Matthew Wilcox (Oracle) | ba206a0 | 2020-06-01 21:47:05 -0700 | [diff] [blame] | 7504 | * call to readahead (a buffered read or a defrag call |
Filipe Manana | b850ae1 | 2015-12-08 16:23:16 +0000 | [diff] [blame] | 7505 | * triggered a readahead) on a page lock due to an |
| 7506 | * ordered dio extent we created before but did not have |
| 7507 | * yet a corresponding bio submitted (whence it can not |
Matthew Wilcox (Oracle) | ba206a0 | 2020-06-01 21:47:05 -0700 | [diff] [blame] | 7508 | * complete), which makes readahead wait for that |
Filipe Manana | b850ae1 | 2015-12-08 16:23:16 +0000 | [diff] [blame] | 7509 | * ordered extent to complete while holding a lock on |
| 7510 | * that page. |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7511 | */ |
Filipe Manana | b850ae1 | 2015-12-08 16:23:16 +0000 | [diff] [blame] | 7512 | ret = -ENOTBLK; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7513 | } |
| 7514 | |
Filipe Manana | ade7702 | 2016-02-18 14:28:55 +0000 | [diff] [blame] | 7515 | if (ret) |
| 7516 | break; |
| 7517 | |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7518 | cond_resched(); |
| 7519 | } |
| 7520 | |
| 7521 | return ret; |
| 7522 | } |
| 7523 | |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 7524 | /* The callers of this must take lock_extent() */ |
Nikolay Borisov | 4b67c11 | 2020-06-03 08:55:05 +0300 | [diff] [blame] | 7525 | static struct extent_map *create_io_em(struct btrfs_inode *inode, u64 start, |
| 7526 | u64 len, u64 orig_start, u64 block_start, |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 7527 | u64 block_len, u64 orig_block_len, |
| 7528 | u64 ram_bytes, int compress_type, |
| 7529 | int type) |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7530 | { |
| 7531 | struct extent_map_tree *em_tree; |
| 7532 | struct extent_map *em; |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7533 | int ret; |
| 7534 | |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 7535 | ASSERT(type == BTRFS_ORDERED_PREALLOC || |
| 7536 | type == BTRFS_ORDERED_COMPRESSED || |
| 7537 | type == BTRFS_ORDERED_NOCOW || |
Liu Bo | 1af4a0a | 2017-02-13 15:35:09 -0800 | [diff] [blame] | 7538 | type == BTRFS_ORDERED_REGULAR); |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 7539 | |
Nikolay Borisov | 4b67c11 | 2020-06-03 08:55:05 +0300 | [diff] [blame] | 7540 | em_tree = &inode->extent_tree; |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7541 | em = alloc_extent_map(); |
| 7542 | if (!em) |
| 7543 | return ERR_PTR(-ENOMEM); |
| 7544 | |
| 7545 | em->start = start; |
| 7546 | em->orig_start = orig_start; |
| 7547 | em->len = len; |
| 7548 | em->block_len = block_len; |
| 7549 | em->block_start = block_start; |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 7550 | em->orig_block_len = orig_block_len; |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 7551 | em->ram_bytes = ram_bytes; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 7552 | em->generation = -1; |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7553 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
Liu Bo | 1af4a0a | 2017-02-13 15:35:09 -0800 | [diff] [blame] | 7554 | if (type == BTRFS_ORDERED_PREALLOC) { |
Josef Bacik | b11e234 | 2012-12-03 10:58:15 -0500 | [diff] [blame] | 7555 | set_bit(EXTENT_FLAG_FILLING, &em->flags); |
Liu Bo | 1af4a0a | 2017-02-13 15:35:09 -0800 | [diff] [blame] | 7556 | } else if (type == BTRFS_ORDERED_COMPRESSED) { |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 7557 | set_bit(EXTENT_FLAG_COMPRESSED, &em->flags); |
| 7558 | em->compress_type = compress_type; |
| 7559 | } |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7560 | |
| 7561 | do { |
Nikolay Borisov | 4b67c11 | 2020-06-03 08:55:05 +0300 | [diff] [blame] | 7562 | btrfs_drop_extent_cache(inode, em->start, |
| 7563 | em->start + em->len - 1, 0); |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7564 | write_lock(&em_tree->lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 7565 | ret = add_extent_mapping(em_tree, em, 1); |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7566 | write_unlock(&em_tree->lock); |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 7567 | /* |
| 7568 | * The caller has taken lock_extent(), who could race with us |
| 7569 | * to add em? |
| 7570 | */ |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7571 | } while (ret == -EEXIST); |
| 7572 | |
| 7573 | if (ret) { |
| 7574 | free_extent_map(em); |
| 7575 | return ERR_PTR(ret); |
| 7576 | } |
| 7577 | |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 7578 | /* 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] | 7579 | return em; |
| 7580 | } |
| 7581 | |
Nikolay Borisov | 1c8d017 | 2018-05-02 15:19:32 +0300 | [diff] [blame] | 7582 | |
Nikolay Borisov | c5794e5 | 2018-05-02 15:19:33 +0300 | [diff] [blame] | 7583 | static int btrfs_get_blocks_direct_write(struct extent_map **map, |
Nikolay Borisov | c5794e5 | 2018-05-02 15:19:33 +0300 | [diff] [blame] | 7584 | struct inode *inode, |
| 7585 | struct btrfs_dio_data *dio_data, |
| 7586 | u64 start, u64 len) |
| 7587 | { |
| 7588 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
| 7589 | struct extent_map *em = *map; |
| 7590 | int ret = 0; |
| 7591 | |
| 7592 | /* |
| 7593 | * We don't allocate a new extent in the following cases |
| 7594 | * |
| 7595 | * 1) The inode is marked as NODATACOW. In this case we'll just use the |
| 7596 | * existing extent. |
| 7597 | * 2) The extent is marked as PREALLOC. We're good to go here and can |
| 7598 | * just use the extent. |
| 7599 | * |
| 7600 | */ |
| 7601 | if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) || |
| 7602 | ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) && |
| 7603 | em->block_start != EXTENT_MAP_HOLE)) { |
| 7604 | int type; |
| 7605 | u64 block_start, orig_start, orig_block_len, ram_bytes; |
| 7606 | |
| 7607 | if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) |
| 7608 | type = BTRFS_ORDERED_PREALLOC; |
| 7609 | else |
| 7610 | type = BTRFS_ORDERED_NOCOW; |
| 7611 | len = min(len, em->len - (start - em->start)); |
| 7612 | block_start = em->block_start + (start - em->start); |
| 7613 | |
| 7614 | if (can_nocow_extent(inode, start, &len, &orig_start, |
Boris Burkov | a84d5d4 | 2020-08-18 11:00:05 -0700 | [diff] [blame] | 7615 | &orig_block_len, &ram_bytes, false) == 1 && |
Nikolay Borisov | c5794e5 | 2018-05-02 15:19:33 +0300 | [diff] [blame] | 7616 | btrfs_inc_nocow_writers(fs_info, block_start)) { |
| 7617 | struct extent_map *em2; |
| 7618 | |
Nikolay Borisov | 64f5418 | 2020-06-03 08:55:31 +0300 | [diff] [blame] | 7619 | em2 = btrfs_create_dio_extent(BTRFS_I(inode), start, len, |
Nikolay Borisov | c5794e5 | 2018-05-02 15:19:33 +0300 | [diff] [blame] | 7620 | orig_start, block_start, |
| 7621 | len, orig_block_len, |
| 7622 | ram_bytes, type); |
| 7623 | btrfs_dec_nocow_writers(fs_info, block_start); |
| 7624 | if (type == BTRFS_ORDERED_PREALLOC) { |
| 7625 | free_extent_map(em); |
| 7626 | *map = em = em2; |
| 7627 | } |
| 7628 | |
| 7629 | if (em2 && IS_ERR(em2)) { |
| 7630 | ret = PTR_ERR(em2); |
| 7631 | goto out; |
| 7632 | } |
| 7633 | /* |
| 7634 | * For inode marked NODATACOW or extent marked PREALLOC, |
| 7635 | * use the existing or preallocated extent, so does not |
| 7636 | * need to adjust btrfs_space_info's bytes_may_use. |
| 7637 | */ |
Nikolay Borisov | 9db5d51 | 2020-06-03 08:55:38 +0300 | [diff] [blame] | 7638 | btrfs_free_reserved_data_space_noquota(fs_info, len); |
Nikolay Borisov | c5794e5 | 2018-05-02 15:19:33 +0300 | [diff] [blame] | 7639 | goto skip_cow; |
| 7640 | } |
| 7641 | } |
| 7642 | |
| 7643 | /* this will cow the extent */ |
Nikolay Borisov | c5794e5 | 2018-05-02 15:19:33 +0300 | [diff] [blame] | 7644 | free_extent_map(em); |
Nikolay Borisov | 9fc6f91 | 2020-06-03 08:55:32 +0300 | [diff] [blame] | 7645 | *map = em = btrfs_new_extent_direct(BTRFS_I(inode), start, len); |
Nikolay Borisov | c5794e5 | 2018-05-02 15:19:33 +0300 | [diff] [blame] | 7646 | if (IS_ERR(em)) { |
| 7647 | ret = PTR_ERR(em); |
| 7648 | goto out; |
| 7649 | } |
| 7650 | |
| 7651 | len = min(len, em->len - (start - em->start)); |
| 7652 | |
| 7653 | skip_cow: |
Nikolay Borisov | c5794e5 | 2018-05-02 15:19:33 +0300 | [diff] [blame] | 7654 | /* |
| 7655 | * Need to update the i_size under the extent lock so buffered |
| 7656 | * readers will get the updated i_size when we unlock. |
| 7657 | */ |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 7658 | if (start + len > i_size_read(inode)) |
Nikolay Borisov | c5794e5 | 2018-05-02 15:19:33 +0300 | [diff] [blame] | 7659 | i_size_write(inode, start + len); |
| 7660 | |
Nikolay Borisov | c5794e5 | 2018-05-02 15:19:33 +0300 | [diff] [blame] | 7661 | dio_data->reserve -= len; |
Nikolay Borisov | c5794e5 | 2018-05-02 15:19:33 +0300 | [diff] [blame] | 7662 | out: |
| 7663 | return ret; |
| 7664 | } |
| 7665 | |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 7666 | static int btrfs_dio_iomap_begin(struct inode *inode, loff_t start, |
| 7667 | loff_t length, unsigned int flags, struct iomap *iomap, |
| 7668 | struct iomap *srcmap) |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7669 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7670 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7671 | struct extent_map *em; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7672 | struct extent_state *cached_state = NULL; |
chandan | 50745b0 | 2015-08-28 21:10:13 +0530 | [diff] [blame] | 7673 | struct btrfs_dio_data *dio_data = NULL; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7674 | u64 lockstart, lockend; |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 7675 | const bool write = !!(flags & IOMAP_WRITE); |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 7676 | int ret = 0; |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 7677 | u64 len = length; |
| 7678 | bool unlock_extents = false; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7679 | |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 7680 | if (!write) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7681 | len = min_t(u64, len, fs_info->sectorsize); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7682 | |
Josef Bacik | c329861 | 2012-08-03 16:49:19 -0400 | [diff] [blame] | 7683 | lockstart = start; |
| 7684 | lockend = start + len - 1; |
| 7685 | |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 7686 | /* |
| 7687 | * The generic stuff only does filemap_write_and_wait_range, which |
| 7688 | * isn't enough if we've written compressed pages to this area, so we |
| 7689 | * need to flush the dirty pages again to make absolutely sure that any |
| 7690 | * outstanding dirty pages are on disk. |
| 7691 | */ |
| 7692 | if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, |
| 7693 | &BTRFS_I(inode)->runtime_flags)) { |
| 7694 | ret = filemap_fdatawrite_range(inode->i_mapping, start, |
| 7695 | start + length - 1); |
| 7696 | if (ret) |
| 7697 | return ret; |
Josef Bacik | e1cbbfa | 2015-03-17 10:52:28 -0400 | [diff] [blame] | 7698 | } |
| 7699 | |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 7700 | dio_data = kzalloc(sizeof(*dio_data), GFP_NOFS); |
| 7701 | if (!dio_data) |
| 7702 | return -ENOMEM; |
| 7703 | |
| 7704 | dio_data->length = length; |
| 7705 | if (write) { |
| 7706 | dio_data->reserve = round_up(length, fs_info->sectorsize); |
| 7707 | ret = btrfs_delalloc_reserve_space(BTRFS_I(inode), |
| 7708 | &dio_data->data_reserved, |
| 7709 | start, dio_data->reserve); |
| 7710 | if (ret) { |
| 7711 | extent_changeset_free(dio_data->data_reserved); |
| 7712 | kfree(dio_data); |
| 7713 | return ret; |
| 7714 | } |
| 7715 | } |
| 7716 | iomap->private = dio_data; |
| 7717 | |
| 7718 | |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7719 | /* |
| 7720 | * If this errors out it's because we couldn't invalidate pagecache for |
| 7721 | * this range and we need to fallback to buffered. |
| 7722 | */ |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 7723 | if (lock_extent_direct(inode, lockstart, lockend, &cached_state, write)) { |
Filipe Manana | 9c9464c | 2015-11-04 09:52:04 +0000 | [diff] [blame] | 7724 | ret = -ENOTBLK; |
| 7725 | goto err; |
| 7726 | } |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7727 | |
Omar Sandoval | 39b07b5 | 2019-12-02 17:34:23 -0800 | [diff] [blame] | 7728 | em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7729 | if (IS_ERR(em)) { |
| 7730 | ret = PTR_ERR(em); |
| 7731 | goto unlock_err; |
| 7732 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7733 | |
| 7734 | /* |
| 7735 | * Ok for INLINE and COMPRESSED extents we need to fallback on buffered |
| 7736 | * io. INLINE is special, and we could probably kludge it in here, but |
| 7737 | * it's still buffered so for safety lets just fall back to the generic |
| 7738 | * buffered path. |
| 7739 | * |
| 7740 | * For COMPRESSED we _have_ to read the entire extent in so we can |
| 7741 | * decompress it, so there will be buffering required no matter what we |
| 7742 | * do, so go ahead and fallback to buffered. |
| 7743 | * |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 7744 | * 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] | 7745 | * to buffered IO. Don't blame me, this is the price we pay for using |
| 7746 | * the generic code. |
| 7747 | */ |
| 7748 | if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) || |
| 7749 | em->block_start == EXTENT_MAP_INLINE) { |
| 7750 | free_extent_map(em); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7751 | ret = -ENOTBLK; |
| 7752 | goto unlock_err; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7753 | } |
| 7754 | |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 7755 | len = min(len, em->len - (start - em->start)); |
| 7756 | if (write) { |
| 7757 | ret = btrfs_get_blocks_direct_write(&em, inode, dio_data, |
| 7758 | start, len); |
Nikolay Borisov | c5794e5 | 2018-05-02 15:19:33 +0300 | [diff] [blame] | 7759 | if (ret < 0) |
| 7760 | goto unlock_err; |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 7761 | unlock_extents = true; |
| 7762 | /* Recalc len in case the new em is smaller than requested */ |
| 7763 | len = min(len, em->len - (start - em->start)); |
Nikolay Borisov | c5794e5 | 2018-05-02 15:19:33 +0300 | [diff] [blame] | 7764 | } else { |
Nikolay Borisov | 1c8d017 | 2018-05-02 15:19:32 +0300 | [diff] [blame] | 7765 | /* |
| 7766 | * We need to unlock only the end area that we aren't using. |
| 7767 | * The rest is going to be unlocked by the endio routine. |
| 7768 | */ |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 7769 | lockstart = start + len; |
| 7770 | if (lockstart < lockend) |
| 7771 | unlock_extents = true; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7772 | } |
| 7773 | |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 7774 | if (unlock_extents) |
| 7775 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, |
| 7776 | lockstart, lockend, &cached_state); |
| 7777 | else |
| 7778 | free_extent_state(cached_state); |
| 7779 | |
| 7780 | /* |
| 7781 | * Translate extent map information to iomap. |
| 7782 | * We trim the extents (and move the addr) even though iomap code does |
| 7783 | * that, since we have locked only the parts we are performing I/O in. |
| 7784 | */ |
| 7785 | if ((em->block_start == EXTENT_MAP_HOLE) || |
| 7786 | (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) && !write)) { |
| 7787 | iomap->addr = IOMAP_NULL_ADDR; |
| 7788 | iomap->type = IOMAP_HOLE; |
| 7789 | } else { |
| 7790 | iomap->addr = em->block_start + (start - em->start); |
| 7791 | iomap->type = IOMAP_MAPPED; |
| 7792 | } |
| 7793 | iomap->offset = start; |
| 7794 | iomap->bdev = fs_info->fs_devices->latest_bdev; |
| 7795 | iomap->length = len; |
| 7796 | |
Naohiro Aota | 544d24f | 2021-02-04 19:22:06 +0900 | [diff] [blame] | 7797 | if (write && btrfs_use_zone_append(BTRFS_I(inode), em)) |
| 7798 | iomap->flags |= IOMAP_F_ZONE_APPEND; |
| 7799 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7800 | free_extent_map(em); |
| 7801 | |
| 7802 | return 0; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7803 | |
| 7804 | unlock_err: |
Omar Sandoval | e182163 | 2019-08-15 14:04:04 -0700 | [diff] [blame] | 7805 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend, |
| 7806 | &cached_state); |
Filipe Manana | 9c9464c | 2015-11-04 09:52:04 +0000 | [diff] [blame] | 7807 | err: |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 7808 | if (dio_data) { |
| 7809 | btrfs_delalloc_release_space(BTRFS_I(inode), |
| 7810 | dio_data->data_reserved, start, |
| 7811 | dio_data->reserve, true); |
| 7812 | btrfs_delalloc_release_extents(BTRFS_I(inode), dio_data->reserve); |
| 7813 | extent_changeset_free(dio_data->data_reserved); |
| 7814 | kfree(dio_data); |
| 7815 | } |
| 7816 | return ret; |
| 7817 | } |
| 7818 | |
| 7819 | static int btrfs_dio_iomap_end(struct inode *inode, loff_t pos, loff_t length, |
| 7820 | ssize_t written, unsigned int flags, struct iomap *iomap) |
| 7821 | { |
| 7822 | int ret = 0; |
| 7823 | struct btrfs_dio_data *dio_data = iomap->private; |
| 7824 | size_t submitted = dio_data->submitted; |
| 7825 | const bool write = !!(flags & IOMAP_WRITE); |
| 7826 | |
| 7827 | if (!write && (iomap->type == IOMAP_HOLE)) { |
| 7828 | /* If reading from a hole, unlock and return */ |
| 7829 | unlock_extent(&BTRFS_I(inode)->io_tree, pos, pos + length - 1); |
| 7830 | goto out; |
| 7831 | } |
| 7832 | |
| 7833 | if (submitted < length) { |
| 7834 | pos += submitted; |
| 7835 | length -= submitted; |
| 7836 | if (write) |
| 7837 | __endio_write_update_ordered(BTRFS_I(inode), pos, |
| 7838 | length, false); |
| 7839 | else |
| 7840 | unlock_extent(&BTRFS_I(inode)->io_tree, pos, |
| 7841 | pos + length - 1); |
| 7842 | ret = -ENOTBLK; |
| 7843 | } |
| 7844 | |
| 7845 | if (write) { |
| 7846 | if (dio_data->reserve) |
| 7847 | btrfs_delalloc_release_space(BTRFS_I(inode), |
| 7848 | dio_data->data_reserved, pos, |
| 7849 | dio_data->reserve, true); |
| 7850 | btrfs_delalloc_release_extents(BTRFS_I(inode), dio_data->length); |
| 7851 | extent_changeset_free(dio_data->data_reserved); |
| 7852 | } |
| 7853 | out: |
| 7854 | kfree(dio_data); |
| 7855 | iomap->private = NULL; |
| 7856 | |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7857 | return ret; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7858 | } |
| 7859 | |
Omar Sandoval | 769b4f2 | 2020-04-16 14:46:22 -0700 | [diff] [blame] | 7860 | static void btrfs_dio_private_put(struct btrfs_dio_private *dip) |
| 7861 | { |
| 7862 | /* |
| 7863 | * This implies a barrier so that stores to dio_bio->bi_status before |
| 7864 | * this and loads of dio_bio->bi_status after this are fully ordered. |
| 7865 | */ |
| 7866 | if (!refcount_dec_and_test(&dip->refs)) |
| 7867 | return; |
| 7868 | |
Naohiro Aota | cfe9444 | 2021-02-04 19:21:59 +0900 | [diff] [blame] | 7869 | if (btrfs_op(dip->dio_bio) == BTRFS_MAP_WRITE) { |
Nikolay Borisov | b672b5c | 2020-06-03 08:55:24 +0300 | [diff] [blame] | 7870 | __endio_write_update_ordered(BTRFS_I(dip->inode), |
| 7871 | dip->logical_offset, |
Omar Sandoval | 769b4f2 | 2020-04-16 14:46:22 -0700 | [diff] [blame] | 7872 | dip->bytes, |
| 7873 | !dip->dio_bio->bi_status); |
| 7874 | } else { |
| 7875 | unlock_extent(&BTRFS_I(dip->inode)->io_tree, |
| 7876 | dip->logical_offset, |
| 7877 | dip->logical_offset + dip->bytes - 1); |
| 7878 | } |
| 7879 | |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 7880 | bio_endio(dip->dio_bio); |
Omar Sandoval | 769b4f2 | 2020-04-16 14:46:22 -0700 | [diff] [blame] | 7881 | kfree(dip); |
| 7882 | } |
| 7883 | |
Omar Sandoval | 77d5d68 | 2020-04-16 14:46:25 -0700 | [diff] [blame] | 7884 | static blk_status_t submit_dio_repair_bio(struct inode *inode, struct bio *bio, |
| 7885 | int mirror_num, |
| 7886 | unsigned long bio_flags) |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7887 | { |
Omar Sandoval | 77d5d68 | 2020-04-16 14:46:25 -0700 | [diff] [blame] | 7888 | struct btrfs_dio_private *dip = bio->bi_private; |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 7889 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Omar Sandoval | 58efbc9 | 2017-08-22 23:45:59 -0700 | [diff] [blame] | 7890 | blk_status_t ret; |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7891 | |
Mike Christie | 37226b2 | 2016-06-05 14:31:52 -0500 | [diff] [blame] | 7892 | BUG_ON(bio_op(bio) == REQ_OP_WRITE); |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7893 | |
Omar Sandoval | 5c047a6 | 2020-04-16 14:46:24 -0700 | [diff] [blame] | 7894 | 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] | 7895 | if (ret) |
Nikolay Borisov | ea057f6 | 2017-12-13 10:25:38 +0200 | [diff] [blame] | 7896 | return ret; |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7897 | |
Omar Sandoval | fd9d667 | 2020-04-16 14:46:23 -0700 | [diff] [blame] | 7898 | refcount_inc(&dip->refs); |
Chris Mason | 08635ba | 2019-07-10 12:28:14 -0700 | [diff] [blame] | 7899 | ret = btrfs_map_bio(fs_info, bio, mirror_num); |
Omar Sandoval | 77d5d68 | 2020-04-16 14:46:25 -0700 | [diff] [blame] | 7900 | if (ret) |
Omar Sandoval | fd9d667 | 2020-04-16 14:46:23 -0700 | [diff] [blame] | 7901 | refcount_dec(&dip->refs); |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7902 | return ret; |
| 7903 | } |
| 7904 | |
Omar Sandoval | 769b4f2 | 2020-04-16 14:46:22 -0700 | [diff] [blame] | 7905 | static blk_status_t btrfs_check_read_dio_bio(struct inode *inode, |
| 7906 | struct btrfs_io_bio *io_bio, |
Omar Sandoval | fd9d667 | 2020-04-16 14:46:23 -0700 | [diff] [blame] | 7907 | const bool uptodate) |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7908 | { |
Omar Sandoval | fd9d667 | 2020-04-16 14:46:23 -0700 | [diff] [blame] | 7909 | struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info; |
| 7910 | const u32 sectorsize = fs_info->sectorsize; |
Josef Bacik | 7870d08 | 2017-05-05 11:57:15 -0400 | [diff] [blame] | 7911 | struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree; |
Omar Sandoval | fd9d667 | 2020-04-16 14:46:23 -0700 | [diff] [blame] | 7912 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
| 7913 | const bool csum = !(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM); |
Liu Bo | 17347ce | 2017-05-15 15:33:27 -0700 | [diff] [blame] | 7914 | struct bio_vec bvec; |
| 7915 | struct bvec_iter iter; |
Omar Sandoval | fd9d667 | 2020-04-16 14:46:23 -0700 | [diff] [blame] | 7916 | u64 start = io_bio->logical; |
Qu Wenruo | 7ffd27e | 2020-12-02 14:47:58 +0800 | [diff] [blame] | 7917 | u32 bio_offset = 0; |
Omar Sandoval | 58efbc9 | 2017-08-22 23:45:59 -0700 | [diff] [blame] | 7918 | blk_status_t err = BLK_STS_OK; |
Miao Xie | dc380ae | 2014-09-12 18:43:55 +0800 | [diff] [blame] | 7919 | |
Omar Sandoval | fd9d667 | 2020-04-16 14:46:23 -0700 | [diff] [blame] | 7920 | __bio_for_each_segment(bvec, &io_bio->bio, iter, io_bio->iter) { |
| 7921 | unsigned int i, nr_sectors, pgoff; |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 7922 | |
Liu Bo | 17347ce | 2017-05-15 15:33:27 -0700 | [diff] [blame] | 7923 | nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec.bv_len); |
| 7924 | pgoff = bvec.bv_offset; |
Omar Sandoval | fd9d667 | 2020-04-16 14:46:23 -0700 | [diff] [blame] | 7925 | for (i = 0; i < nr_sectors; i++) { |
Liu Bo | 97bf5a5 | 2017-04-07 13:11:10 -0700 | [diff] [blame] | 7926 | ASSERT(pgoff < PAGE_SIZE); |
Omar Sandoval | fd9d667 | 2020-04-16 14:46:23 -0700 | [diff] [blame] | 7927 | if (uptodate && |
Qu Wenruo | 7ffd27e | 2020-12-02 14:47:58 +0800 | [diff] [blame] | 7928 | (!csum || !check_data_csum(inode, io_bio, |
Omar Sandoval | c1d6abd | 2021-03-16 12:43:00 -0700 | [diff] [blame] | 7929 | bio_offset, bvec.bv_page, |
| 7930 | pgoff, start))) { |
Omar Sandoval | fd9d667 | 2020-04-16 14:46:23 -0700 | [diff] [blame] | 7931 | clean_io_failure(fs_info, failure_tree, io_tree, |
| 7932 | start, bvec.bv_page, |
| 7933 | btrfs_ino(BTRFS_I(inode)), |
| 7934 | pgoff); |
| 7935 | } else { |
| 7936 | blk_status_t status; |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7937 | |
Qu Wenruo | 7ffd27e | 2020-12-02 14:47:58 +0800 | [diff] [blame] | 7938 | ASSERT((start - io_bio->logical) < UINT_MAX); |
Omar Sandoval | 77d5d68 | 2020-04-16 14:46:25 -0700 | [diff] [blame] | 7939 | status = btrfs_submit_read_repair(inode, |
| 7940 | &io_bio->bio, |
| 7941 | start - io_bio->logical, |
Omar Sandoval | fd9d667 | 2020-04-16 14:46:23 -0700 | [diff] [blame] | 7942 | bvec.bv_page, pgoff, |
| 7943 | start, |
| 7944 | start + sectorsize - 1, |
Omar Sandoval | 77d5d68 | 2020-04-16 14:46:25 -0700 | [diff] [blame] | 7945 | io_bio->mirror_num, |
| 7946 | submit_dio_repair_bio); |
Omar Sandoval | fd9d667 | 2020-04-16 14:46:23 -0700 | [diff] [blame] | 7947 | if (status) |
| 7948 | err = status; |
| 7949 | } |
| 7950 | start += sectorsize; |
Qu Wenruo | 7ffd27e | 2020-12-02 14:47:58 +0800 | [diff] [blame] | 7951 | ASSERT(bio_offset + sectorsize > bio_offset); |
| 7952 | bio_offset += sectorsize; |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 7953 | pgoff += sectorsize; |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 7954 | } |
Kent Overstreet | 2c30c71 | 2013-11-07 12:20:26 -0800 | [diff] [blame] | 7955 | } |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 7956 | return err; |
| 7957 | } |
| 7958 | |
Nikolay Borisov | b672b5c | 2020-06-03 08:55:24 +0300 | [diff] [blame] | 7959 | static void __endio_write_update_ordered(struct btrfs_inode *inode, |
Qu Wenruo | 52427260 | 2017-03-08 10:25:52 +0800 | [diff] [blame] | 7960 | const u64 offset, const u64 bytes, |
| 7961 | const bool uptodate) |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7962 | { |
Nikolay Borisov | b672b5c | 2020-06-03 08:55:24 +0300 | [diff] [blame] | 7963 | struct btrfs_fs_info *fs_info = inode->root->fs_info; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7964 | struct btrfs_ordered_extent *ordered = NULL; |
Qu Wenruo | 52427260 | 2017-03-08 10:25:52 +0800 | [diff] [blame] | 7965 | struct btrfs_workqueue *wq; |
Filipe Manana | 1454377 | 2015-11-24 16:23:54 +0000 | [diff] [blame] | 7966 | u64 ordered_offset = offset; |
| 7967 | u64 ordered_bytes = bytes; |
Naohiro Aota | 67c003f | 2017-09-01 17:59:07 +0900 | [diff] [blame] | 7968 | u64 last_offset; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7969 | |
Nikolay Borisov | b672b5c | 2020-06-03 08:55:24 +0300 | [diff] [blame] | 7970 | if (btrfs_is_free_space_inode(inode)) |
Qu Wenruo | 52427260 | 2017-03-08 10:25:52 +0800 | [diff] [blame] | 7971 | wq = fs_info->endio_freespace_worker; |
Omar Sandoval | a0cac0e | 2019-09-16 11:30:57 -0700 | [diff] [blame] | 7972 | else |
Qu Wenruo | 52427260 | 2017-03-08 10:25:52 +0800 | [diff] [blame] | 7973 | wq = fs_info->endio_write_workers; |
Qu Wenruo | 52427260 | 2017-03-08 10:25:52 +0800 | [diff] [blame] | 7974 | |
Nikolay Borisov | b25f0d0 | 2018-04-11 11:21:17 +0300 | [diff] [blame] | 7975 | while (ordered_offset < offset + bytes) { |
| 7976 | last_offset = ordered_offset; |
Nikolay Borisov | b672b5c | 2020-06-03 08:55:24 +0300 | [diff] [blame] | 7977 | if (btrfs_dec_test_first_ordered_pending(inode, &ordered, |
Nikolay Borisov | 7095821 | 2020-06-03 08:55:23 +0300 | [diff] [blame] | 7978 | &ordered_offset, |
| 7979 | ordered_bytes, |
| 7980 | uptodate)) { |
Omar Sandoval | a0cac0e | 2019-09-16 11:30:57 -0700 | [diff] [blame] | 7981 | btrfs_init_work(&ordered->work, finish_ordered_fn, NULL, |
| 7982 | NULL); |
Nikolay Borisov | b25f0d0 | 2018-04-11 11:21:17 +0300 | [diff] [blame] | 7983 | btrfs_queue_work(wq, &ordered->work); |
| 7984 | } |
Qu Wenruo | 58f74b2 | 2020-12-22 13:59:24 +0800 | [diff] [blame] | 7985 | |
| 7986 | /* No ordered extent found in the range, exit */ |
Nikolay Borisov | b25f0d0 | 2018-04-11 11:21:17 +0300 | [diff] [blame] | 7987 | if (ordered_offset == last_offset) |
| 7988 | return; |
| 7989 | /* |
| 7990 | * Our bio might span multiple ordered extents. In this case |
Andrea Gelmini | 52042d8 | 2018-11-28 12:05:13 +0100 | [diff] [blame] | 7991 | * we keep going until we have accounted the whole dio. |
Nikolay Borisov | b25f0d0 | 2018-04-11 11:21:17 +0300 | [diff] [blame] | 7992 | */ |
| 7993 | if (ordered_offset < offset + bytes) { |
| 7994 | ordered_bytes = offset + bytes - ordered_offset; |
| 7995 | ordered = NULL; |
| 7996 | } |
Chris Mason | 163cf09 | 2010-11-28 19:56:33 -0500 | [diff] [blame] | 7997 | } |
Filipe Manana | 1454377 | 2015-11-24 16:23:54 +0000 | [diff] [blame] | 7998 | } |
| 7999 | |
Qu Wenruo | 8896a08 | 2020-10-21 14:24:53 +0800 | [diff] [blame] | 8000 | 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] | 8001 | struct bio *bio, |
| 8002 | u64 dio_file_offset) |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 8003 | { |
Qu Wenruo | 1941b64 | 2020-12-02 14:47:57 +0800 | [diff] [blame] | 8004 | 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] | 8005 | } |
| 8006 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 8007 | static void btrfs_end_dio_bio(struct bio *bio) |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8008 | { |
| 8009 | struct btrfs_dio_private *dip = bio->bi_private; |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 8010 | blk_status_t err = bio->bi_status; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8011 | |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8012 | if (err) |
| 8013 | btrfs_warn(BTRFS_I(dip->inode)->root->fs_info, |
Mike Christie | 6296b96 | 2016-06-05 14:32:21 -0500 | [diff] [blame] | 8014 | "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] | 8015 | btrfs_ino(BTRFS_I(dip->inode)), bio_op(bio), |
David Sterba | 1201b58 | 2020-11-26 15:41:27 +0100 | [diff] [blame] | 8016 | bio->bi_opf, bio->bi_iter.bi_sector, |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8017 | bio->bi_iter.bi_size, err); |
| 8018 | |
Omar Sandoval | 769b4f2 | 2020-04-16 14:46:22 -0700 | [diff] [blame] | 8019 | if (bio_op(bio) == REQ_OP_READ) { |
| 8020 | err = btrfs_check_read_dio_bio(dip->inode, btrfs_io_bio(bio), |
Omar Sandoval | fd9d667 | 2020-04-16 14:46:23 -0700 | [diff] [blame] | 8021 | !err); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8022 | } |
| 8023 | |
Omar Sandoval | 769b4f2 | 2020-04-16 14:46:22 -0700 | [diff] [blame] | 8024 | if (err) |
| 8025 | dip->dio_bio->bi_status = err; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8026 | |
Naohiro Aota | 544d24f | 2021-02-04 19:22:06 +0900 | [diff] [blame] | 8027 | btrfs_record_physical_zoned(dip->inode, dip->logical_offset, bio); |
| 8028 | |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8029 | bio_put(bio); |
Omar Sandoval | 769b4f2 | 2020-04-16 14:46:22 -0700 | [diff] [blame] | 8030 | btrfs_dio_private_put(dip); |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 8031 | } |
| 8032 | |
David Sterba | d0ee393 | 2018-03-08 14:35:48 +0100 | [diff] [blame] | 8033 | static inline blk_status_t btrfs_submit_dio_bio(struct bio *bio, |
| 8034 | struct inode *inode, u64 file_offset, int async_submit) |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8035 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8036 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 8037 | struct btrfs_dio_private *dip = bio->bi_private; |
Naohiro Aota | cfe9444 | 2021-02-04 19:21:59 +0900 | [diff] [blame] | 8038 | bool write = btrfs_op(bio) == BTRFS_MAP_WRITE; |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 8039 | blk_status_t ret; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8040 | |
Liu Bo | 4c274bc | 2017-11-01 17:19:27 -0600 | [diff] [blame] | 8041 | /* Check btrfs_submit_bio_hook() for rules about async submit. */ |
Josef Bacik | b812ce2 | 2012-11-16 13:56:32 -0500 | [diff] [blame] | 8042 | if (async_submit) |
| 8043 | async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers); |
| 8044 | |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 8045 | if (!write) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8046 | 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] | 8047 | if (ret) |
| 8048 | goto err; |
| 8049 | } |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8050 | |
Nikolay Borisov | e6961ca | 2017-08-03 15:44:58 +0300 | [diff] [blame] | 8051 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM) |
Josef Bacik | 1ae3993 | 2011-04-06 14:41:34 -0400 | [diff] [blame] | 8052 | goto map; |
| 8053 | |
| 8054 | if (write && async_submit) { |
Qu Wenruo | 1941b64 | 2020-12-02 14:47:57 +0800 | [diff] [blame] | 8055 | ret = btrfs_wq_submit_bio(inode, bio, 0, 0, file_offset, |
David Sterba | e288c08 | 2018-07-18 17:36:24 +0200 | [diff] [blame] | 8056 | btrfs_submit_bio_start_direct_io); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8057 | goto err; |
Josef Bacik | 1ae3993 | 2011-04-06 14:41:34 -0400 | [diff] [blame] | 8058 | } else if (write) { |
| 8059 | /* |
| 8060 | * If we aren't doing async submit, calculate the csum of the |
| 8061 | * bio now. |
| 8062 | */ |
Nikolay Borisov | bd242a0 | 2020-06-03 08:55:07 +0300 | [diff] [blame] | 8063 | ret = btrfs_csum_one_bio(BTRFS_I(inode), bio, file_offset, 1); |
Josef Bacik | 1ae3993 | 2011-04-06 14:41:34 -0400 | [diff] [blame] | 8064 | if (ret) |
| 8065 | goto err; |
Miao Xie | 23ea8e5 | 2014-09-12 18:43:54 +0800 | [diff] [blame] | 8066 | } else { |
Omar Sandoval | 8587957 | 2020-04-16 14:46:21 -0700 | [diff] [blame] | 8067 | u64 csum_offset; |
| 8068 | |
| 8069 | csum_offset = file_offset - dip->logical_offset; |
David Sterba | 265fdfa | 2020-07-01 21:19:09 +0200 | [diff] [blame] | 8070 | csum_offset >>= fs_info->sectorsize_bits; |
David Sterba | 55fc29b | 2020-06-30 02:01:31 +0200 | [diff] [blame] | 8071 | csum_offset *= fs_info->csum_size; |
Omar Sandoval | 8587957 | 2020-04-16 14:46:21 -0700 | [diff] [blame] | 8072 | btrfs_io_bio(bio)->csum = dip->csums + csum_offset; |
Tsutomu Itoh | c2db107 | 2011-03-01 06:48:31 +0000 | [diff] [blame] | 8073 | } |
Josef Bacik | 1ae3993 | 2011-04-06 14:41:34 -0400 | [diff] [blame] | 8074 | map: |
Chris Mason | 08635ba | 2019-07-10 12:28:14 -0700 | [diff] [blame] | 8075 | ret = btrfs_map_bio(fs_info, bio, 0); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8076 | err: |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8077 | return ret; |
| 8078 | } |
| 8079 | |
Omar Sandoval | c36cac2 | 2020-04-16 14:46:13 -0700 | [diff] [blame] | 8080 | /* |
| 8081 | * If this succeeds, the btrfs_dio_private is responsible for cleaning up locked |
| 8082 | * or ordered extents whether or not we submit any bios. |
| 8083 | */ |
| 8084 | static struct btrfs_dio_private *btrfs_create_dio_private(struct bio *dio_bio, |
| 8085 | struct inode *inode, |
| 8086 | loff_t file_offset) |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8087 | { |
Naohiro Aota | cfe9444 | 2021-02-04 19:21:59 +0900 | [diff] [blame] | 8088 | const bool write = (btrfs_op(dio_bio) == BTRFS_MAP_WRITE); |
Omar Sandoval | 8587957 | 2020-04-16 14:46:21 -0700 | [diff] [blame] | 8089 | const bool csum = !(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM); |
| 8090 | size_t dip_size; |
Omar Sandoval | c36cac2 | 2020-04-16 14:46:13 -0700 | [diff] [blame] | 8091 | struct btrfs_dio_private *dip; |
Omar Sandoval | c36cac2 | 2020-04-16 14:46:13 -0700 | [diff] [blame] | 8092 | |
Omar Sandoval | 8587957 | 2020-04-16 14:46:21 -0700 | [diff] [blame] | 8093 | dip_size = sizeof(*dip); |
| 8094 | if (!write && csum) { |
| 8095 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Omar Sandoval | 8587957 | 2020-04-16 14:46:21 -0700 | [diff] [blame] | 8096 | size_t nblocks; |
| 8097 | |
David Sterba | 265fdfa | 2020-07-01 21:19:09 +0200 | [diff] [blame] | 8098 | nblocks = dio_bio->bi_iter.bi_size >> fs_info->sectorsize_bits; |
David Sterba | 223486c | 2020-07-02 11:27:30 +0200 | [diff] [blame] | 8099 | dip_size += fs_info->csum_size * nblocks; |
Omar Sandoval | 8587957 | 2020-04-16 14:46:21 -0700 | [diff] [blame] | 8100 | } |
| 8101 | |
| 8102 | dip = kzalloc(dip_size, GFP_NOFS); |
Omar Sandoval | c36cac2 | 2020-04-16 14:46:13 -0700 | [diff] [blame] | 8103 | if (!dip) |
| 8104 | return NULL; |
| 8105 | |
Omar Sandoval | c36cac2 | 2020-04-16 14:46:13 -0700 | [diff] [blame] | 8106 | dip->inode = inode; |
| 8107 | dip->logical_offset = file_offset; |
| 8108 | dip->bytes = dio_bio->bi_iter.bi_size; |
David Sterba | 1201b58 | 2020-11-26 15:41:27 +0100 | [diff] [blame] | 8109 | dip->disk_bytenr = dio_bio->bi_iter.bi_sector << 9; |
Omar Sandoval | c36cac2 | 2020-04-16 14:46:13 -0700 | [diff] [blame] | 8110 | dip->dio_bio = dio_bio; |
Omar Sandoval | e3b318d | 2020-04-16 14:46:20 -0700 | [diff] [blame] | 8111 | refcount_set(&dip->refs, 1); |
Omar Sandoval | c36cac2 | 2020-04-16 14:46:13 -0700 | [diff] [blame] | 8112 | return dip; |
| 8113 | } |
| 8114 | |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 8115 | static blk_qc_t btrfs_submit_direct(struct inode *inode, struct iomap *iomap, |
| 8116 | struct bio *dio_bio, loff_t file_offset) |
Omar Sandoval | c36cac2 | 2020-04-16 14:46:13 -0700 | [diff] [blame] | 8117 | { |
Naohiro Aota | cfe9444 | 2021-02-04 19:21:59 +0900 | [diff] [blame] | 8118 | const bool write = (btrfs_op(dio_bio) == BTRFS_MAP_WRITE); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8119 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Omar Sandoval | 769b4f2 | 2020-04-16 14:46:22 -0700 | [diff] [blame] | 8120 | const bool raid56 = (btrfs_data_alloc_profile(fs_info) & |
| 8121 | BTRFS_BLOCK_GROUP_RAID56_MASK); |
Omar Sandoval | c36cac2 | 2020-04-16 14:46:13 -0700 | [diff] [blame] | 8122 | struct btrfs_dio_private *dip; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8123 | struct bio *bio; |
Omar Sandoval | c36cac2 | 2020-04-16 14:46:13 -0700 | [diff] [blame] | 8124 | u64 start_sector; |
Josef Bacik | 1ae3993 | 2011-04-06 14:41:34 -0400 | [diff] [blame] | 8125 | int async_submit = 0; |
Liu Bo | 725130b | 2017-05-16 09:51:39 -0700 | [diff] [blame] | 8126 | u64 submit_len; |
| 8127 | int clone_offset = 0; |
| 8128 | int clone_len; |
Michal Rostecki | 4203431 | 2021-01-27 14:57:27 +0100 | [diff] [blame] | 8129 | u64 logical; |
Chandan Rajendra | 5f4dc8f | 2016-01-21 15:56:00 +0530 | [diff] [blame] | 8130 | int ret; |
Omar Sandoval | 58efbc9 | 2017-08-22 23:45:59 -0700 | [diff] [blame] | 8131 | blk_status_t status; |
Nikolay Borisov | 89b798a | 2019-06-03 12:05:05 +0300 | [diff] [blame] | 8132 | struct btrfs_io_geometry geom; |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 8133 | struct btrfs_dio_data *dio_data = iomap->private; |
Michal Rostecki | 4203431 | 2021-01-27 14:57:27 +0100 | [diff] [blame] | 8134 | struct extent_map *em = NULL; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8135 | |
Omar Sandoval | c36cac2 | 2020-04-16 14:46:13 -0700 | [diff] [blame] | 8136 | dip = btrfs_create_dio_private(dio_bio, inode, file_offset); |
| 8137 | if (!dip) { |
| 8138 | if (!write) { |
| 8139 | unlock_extent(&BTRFS_I(inode)->io_tree, file_offset, |
| 8140 | file_offset + dio_bio->bi_iter.bi_size - 1); |
| 8141 | } |
| 8142 | dio_bio->bi_status = BLK_STS_RESOURCE; |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 8143 | bio_endio(dio_bio); |
| 8144 | return BLK_QC_T_NONE; |
Josef Bacik | 02f57c7 | 2011-04-06 14:25:44 -0400 | [diff] [blame] | 8145 | } |
| 8146 | |
Josef Bacik | 334c16d | 2020-10-16 11:29:14 -0400 | [diff] [blame] | 8147 | if (!write) { |
Omar Sandoval | 8587957 | 2020-04-16 14:46:21 -0700 | [diff] [blame] | 8148 | /* |
| 8149 | * Load the csums up front to reduce csum tree searches and |
| 8150 | * contention when submitting bios. |
Josef Bacik | 334c16d | 2020-10-16 11:29:14 -0400 | [diff] [blame] | 8151 | * |
| 8152 | * If we have csums disabled this will do nothing. |
Omar Sandoval | 8587957 | 2020-04-16 14:46:21 -0700 | [diff] [blame] | 8153 | */ |
Qu Wenruo | 6275193 | 2020-12-02 14:48:06 +0800 | [diff] [blame] | 8154 | status = btrfs_lookup_bio_sums(inode, dio_bio, dip->csums); |
Omar Sandoval | 8587957 | 2020-04-16 14:46:21 -0700 | [diff] [blame] | 8155 | if (status != BLK_STS_OK) |
| 8156 | goto out_err; |
| 8157 | } |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 8158 | |
Omar Sandoval | 769b4f2 | 2020-04-16 14:46:22 -0700 | [diff] [blame] | 8159 | start_sector = dio_bio->bi_iter.bi_sector; |
| 8160 | submit_len = dio_bio->bi_iter.bi_size; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8161 | |
Arnd Bergmann | 3c91ee6 | 2017-05-18 15:33:29 +0200 | [diff] [blame] | 8162 | do { |
Michal Rostecki | 4203431 | 2021-01-27 14:57:27 +0100 | [diff] [blame] | 8163 | logical = start_sector << 9; |
| 8164 | em = btrfs_get_chunk_map(fs_info, logical, submit_len); |
| 8165 | if (IS_ERR(em)) { |
| 8166 | status = errno_to_blk_status(PTR_ERR(em)); |
| 8167 | em = NULL; |
| 8168 | goto out_err_em; |
| 8169 | } |
| 8170 | ret = btrfs_get_io_geometry(fs_info, em, btrfs_op(dio_bio), |
| 8171 | logical, submit_len, &geom); |
Omar Sandoval | 769b4f2 | 2020-04-16 14:46:22 -0700 | [diff] [blame] | 8172 | if (ret) { |
| 8173 | status = errno_to_blk_status(ret); |
Michal Rostecki | 4203431 | 2021-01-27 14:57:27 +0100 | [diff] [blame] | 8174 | goto out_err_em; |
Omar Sandoval | 769b4f2 | 2020-04-16 14:46:22 -0700 | [diff] [blame] | 8175 | } |
| 8176 | ASSERT(geom.len <= INT_MAX); |
| 8177 | |
Nikolay Borisov | 89b798a | 2019-06-03 12:05:05 +0300 | [diff] [blame] | 8178 | clone_len = min_t(int, submit_len, geom.len); |
Josef Bacik | 02f57c7 | 2011-04-06 14:25:44 -0400 | [diff] [blame] | 8179 | |
Liu Bo | 725130b | 2017-05-16 09:51:39 -0700 | [diff] [blame] | 8180 | /* |
| 8181 | * This will never fail as it's passing GPF_NOFS and |
| 8182 | * the allocation is backed by btrfs_bioset. |
| 8183 | */ |
Omar Sandoval | 769b4f2 | 2020-04-16 14:46:22 -0700 | [diff] [blame] | 8184 | bio = btrfs_bio_clone_partial(dio_bio, clone_offset, clone_len); |
Liu Bo | 725130b | 2017-05-16 09:51:39 -0700 | [diff] [blame] | 8185 | bio->bi_private = dip; |
| 8186 | bio->bi_end_io = btrfs_end_dio_bio; |
| 8187 | btrfs_io_bio(bio)->logical = file_offset; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8188 | |
Naohiro Aota | 544d24f | 2021-02-04 19:22:06 +0900 | [diff] [blame] | 8189 | if (bio_op(bio) == REQ_OP_ZONE_APPEND) { |
| 8190 | status = extract_ordered_extent(BTRFS_I(inode), bio, |
| 8191 | file_offset); |
| 8192 | if (status) { |
| 8193 | bio_put(bio); |
| 8194 | goto out_err; |
| 8195 | } |
| 8196 | } |
| 8197 | |
Liu Bo | 725130b | 2017-05-16 09:51:39 -0700 | [diff] [blame] | 8198 | ASSERT(submit_len >= clone_len); |
| 8199 | submit_len -= clone_len; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8200 | |
Liu Bo | 725130b | 2017-05-16 09:51:39 -0700 | [diff] [blame] | 8201 | /* |
| 8202 | * Increase the count before we submit the bio so we know |
| 8203 | * the end IO handler won't happen before we increase the |
| 8204 | * count. Otherwise, the dip might get freed before we're |
| 8205 | * done setting it up. |
Omar Sandoval | 769b4f2 | 2020-04-16 14:46:22 -0700 | [diff] [blame] | 8206 | * |
| 8207 | * We transfer the initial reference to the last bio, so we |
| 8208 | * don't need to increment the reference count for the last one. |
Liu Bo | 725130b | 2017-05-16 09:51:39 -0700 | [diff] [blame] | 8209 | */ |
Omar Sandoval | 769b4f2 | 2020-04-16 14:46:22 -0700 | [diff] [blame] | 8210 | if (submit_len > 0) { |
| 8211 | refcount_inc(&dip->refs); |
| 8212 | /* |
| 8213 | * If we are submitting more than one bio, submit them |
| 8214 | * all asynchronously. The exception is RAID 5 or 6, as |
| 8215 | * asynchronous checksums make it difficult to collect |
| 8216 | * full stripe writes. |
| 8217 | */ |
| 8218 | if (!raid56) |
| 8219 | async_submit = 1; |
| 8220 | } |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8221 | |
David Sterba | d0ee393 | 2018-03-08 14:35:48 +0100 | [diff] [blame] | 8222 | status = btrfs_submit_dio_bio(bio, inode, file_offset, |
Omar Sandoval | 58efbc9 | 2017-08-22 23:45:59 -0700 | [diff] [blame] | 8223 | async_submit); |
| 8224 | if (status) { |
Liu Bo | 725130b | 2017-05-16 09:51:39 -0700 | [diff] [blame] | 8225 | bio_put(bio); |
Omar Sandoval | 769b4f2 | 2020-04-16 14:46:22 -0700 | [diff] [blame] | 8226 | if (submit_len > 0) |
| 8227 | refcount_dec(&dip->refs); |
Michal Rostecki | 4203431 | 2021-01-27 14:57:27 +0100 | [diff] [blame] | 8228 | goto out_err_em; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8229 | } |
Liu Bo | 725130b | 2017-05-16 09:51:39 -0700 | [diff] [blame] | 8230 | |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 8231 | dio_data->submitted += clone_len; |
Liu Bo | 725130b | 2017-05-16 09:51:39 -0700 | [diff] [blame] | 8232 | clone_offset += clone_len; |
| 8233 | start_sector += clone_len >> 9; |
| 8234 | file_offset += clone_len; |
Michal Rostecki | 4203431 | 2021-01-27 14:57:27 +0100 | [diff] [blame] | 8235 | |
| 8236 | free_extent_map(em); |
Arnd Bergmann | 3c91ee6 | 2017-05-18 15:33:29 +0200 | [diff] [blame] | 8237 | } while (submit_len > 0); |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 8238 | return BLK_QC_T_NONE; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8239 | |
Michal Rostecki | 4203431 | 2021-01-27 14:57:27 +0100 | [diff] [blame] | 8240 | out_err_em: |
| 8241 | free_extent_map(em); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8242 | out_err: |
Omar Sandoval | 769b4f2 | 2020-04-16 14:46:22 -0700 | [diff] [blame] | 8243 | dip->dio_bio->bi_status = status; |
| 8244 | btrfs_dio_private_put(dip); |
Michal Rostecki | 4203431 | 2021-01-27 14:57:27 +0100 | [diff] [blame] | 8245 | |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 8246 | return BLK_QC_T_NONE; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8247 | } |
| 8248 | |
Goldwyn Rodrigues | 4e4cabe | 2020-09-24 11:39:12 -0500 | [diff] [blame] | 8249 | const struct iomap_ops btrfs_dio_iomap_ops = { |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 8250 | .iomap_begin = btrfs_dio_iomap_begin, |
| 8251 | .iomap_end = btrfs_dio_iomap_end, |
| 8252 | }; |
| 8253 | |
Goldwyn Rodrigues | 4e4cabe | 2020-09-24 11:39:12 -0500 | [diff] [blame] | 8254 | const struct iomap_dio_ops btrfs_dio_ops = { |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 8255 | .submit_io = btrfs_submit_direct, |
| 8256 | }; |
| 8257 | |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 8258 | static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, |
David Sterba | bab16e2 | 2020-06-23 20:56:12 +0200 | [diff] [blame] | 8259 | u64 start, u64 len) |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 8260 | { |
Tsutomu Itoh | 05dadc0 | 2012-11-29 05:08:26 +0000 | [diff] [blame] | 8261 | int ret; |
| 8262 | |
Christoph Hellwig | 45dd052 | 2020-05-23 09:30:14 +0200 | [diff] [blame] | 8263 | ret = fiemap_prep(inode, fieinfo, start, &len, 0); |
Tsutomu Itoh | 05dadc0 | 2012-11-29 05:08:26 +0000 | [diff] [blame] | 8264 | if (ret) |
| 8265 | return ret; |
| 8266 | |
Nikolay Borisov | facee0a | 2020-08-31 14:42:49 +0300 | [diff] [blame] | 8267 | return extent_fiemap(BTRFS_I(inode), fieinfo, start, len); |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 8268 | } |
| 8269 | |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8270 | int btrfs_readpage(struct file *file, struct page *page) |
| 8271 | { |
Nikolay Borisov | 0f20881 | 2020-09-14 14:39:16 +0300 | [diff] [blame] | 8272 | struct btrfs_inode *inode = BTRFS_I(page->mapping->host); |
| 8273 | u64 start = page_offset(page); |
| 8274 | u64 end = start + PAGE_SIZE - 1; |
Nikolay Borisov | c1be9c1 | 2020-09-14 12:37:08 +0300 | [diff] [blame] | 8275 | unsigned long bio_flags = 0; |
Nikolay Borisov | 0f20881 | 2020-09-14 14:39:16 +0300 | [diff] [blame] | 8276 | struct bio *bio = NULL; |
Nikolay Borisov | c1be9c1 | 2020-09-14 12:37:08 +0300 | [diff] [blame] | 8277 | int ret; |
| 8278 | |
Nikolay Borisov | 0f20881 | 2020-09-14 14:39:16 +0300 | [diff] [blame] | 8279 | btrfs_lock_and_flush_ordered_range(inode, start, end, NULL); |
| 8280 | |
| 8281 | ret = btrfs_do_readpage(page, NULL, &bio, &bio_flags, 0, NULL); |
Nikolay Borisov | c1be9c1 | 2020-09-14 12:37:08 +0300 | [diff] [blame] | 8282 | if (bio) |
| 8283 | ret = submit_one_bio(bio, 0, bio_flags); |
| 8284 | return ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8285 | } |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 8286 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8287 | static int btrfs_writepage(struct page *page, struct writeback_control *wbc) |
| 8288 | { |
Josef Bacik | be7bd73 | 2015-10-22 15:05:09 -0400 | [diff] [blame] | 8289 | struct inode *inode = page->mapping->host; |
| 8290 | int ret; |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8291 | |
| 8292 | if (current->flags & PF_MEMALLOC) { |
| 8293 | redirty_page_for_writepage(wbc, page); |
| 8294 | unlock_page(page); |
| 8295 | return 0; |
| 8296 | } |
Josef Bacik | be7bd73 | 2015-10-22 15:05:09 -0400 | [diff] [blame] | 8297 | |
| 8298 | /* |
| 8299 | * If we are under memory pressure we will call this directly from the |
| 8300 | * VM, we need to make sure we have the inode referenced for the ordered |
| 8301 | * extent. If not just return like we didn't do anything. |
| 8302 | */ |
| 8303 | if (!igrab(inode)) { |
| 8304 | redirty_page_for_writepage(wbc, page); |
| 8305 | return AOP_WRITEPAGE_ACTIVATE; |
| 8306 | } |
Nikolay Borisov | 0a9b0e5 | 2017-12-08 15:55:59 +0200 | [diff] [blame] | 8307 | ret = extent_write_full_page(page, wbc); |
Josef Bacik | be7bd73 | 2015-10-22 15:05:09 -0400 | [diff] [blame] | 8308 | btrfs_add_delayed_iput(inode); |
| 8309 | return ret; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8310 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8311 | |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 8312 | static int btrfs_writepages(struct address_space *mapping, |
| 8313 | struct writeback_control *wbc) |
Chris Mason | b293f02e | 2007-11-01 19:45:34 -0400 | [diff] [blame] | 8314 | { |
Nikolay Borisov | 8ae225a | 2018-04-19 10:46:38 +0300 | [diff] [blame] | 8315 | return extent_writepages(mapping, wbc); |
Chris Mason | b293f02e | 2007-11-01 19:45:34 -0400 | [diff] [blame] | 8316 | } |
| 8317 | |
Matthew Wilcox (Oracle) | ba206a0 | 2020-06-01 21:47:05 -0700 | [diff] [blame] | 8318 | static void btrfs_readahead(struct readahead_control *rac) |
Chris Mason | 3ab2fb5 | 2007-11-08 10:59:22 -0500 | [diff] [blame] | 8319 | { |
Matthew Wilcox (Oracle) | ba206a0 | 2020-06-01 21:47:05 -0700 | [diff] [blame] | 8320 | extent_readahead(rac); |
Chris Mason | 3ab2fb5 | 2007-11-08 10:59:22 -0500 | [diff] [blame] | 8321 | } |
Nikolay Borisov | 2a3ff0a | 2018-04-19 10:46:36 +0300 | [diff] [blame] | 8322 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8323 | static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8324 | { |
Nikolay Borisov | 477a30b | 2018-04-19 10:46:34 +0300 | [diff] [blame] | 8325 | int ret = try_release_extent_mapping(page, gfp_flags); |
Guoqing Jiang | d1b89bc | 2020-06-01 21:47:45 -0700 | [diff] [blame] | 8326 | if (ret == 1) |
Qu Wenruo | 32443de | 2021-01-26 16:34:00 +0800 | [diff] [blame] | 8327 | clear_page_extent_mapped(page); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8328 | return ret; |
| 8329 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8330 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8331 | static int btrfs_releasepage(struct page *page, gfp_t gfp_flags) |
| 8332 | { |
Chris Mason | 98509cf | 2008-09-11 15:51:43 -0400 | [diff] [blame] | 8333 | if (PageWriteback(page) || PageDirty(page)) |
| 8334 | return 0; |
Michal Hocko | 3ba7ab2 | 2017-01-09 15:39:02 +0100 | [diff] [blame] | 8335 | return __btrfs_releasepage(page, gfp_flags); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8336 | } |
| 8337 | |
Roman Gushchin | f8e6608 | 2020-03-04 16:57:35 -0800 | [diff] [blame] | 8338 | #ifdef CONFIG_MIGRATION |
| 8339 | static int btrfs_migratepage(struct address_space *mapping, |
| 8340 | struct page *newpage, struct page *page, |
| 8341 | enum migrate_mode mode) |
| 8342 | { |
| 8343 | int ret; |
| 8344 | |
| 8345 | ret = migrate_page_move_mapping(mapping, newpage, page, 0); |
| 8346 | if (ret != MIGRATEPAGE_SUCCESS) |
| 8347 | return ret; |
| 8348 | |
Guoqing Jiang | d1b89bc | 2020-06-01 21:47:45 -0700 | [diff] [blame] | 8349 | if (page_has_private(page)) |
| 8350 | attach_page_private(newpage, detach_page_private(page)); |
Roman Gushchin | f8e6608 | 2020-03-04 16:57:35 -0800 | [diff] [blame] | 8351 | |
| 8352 | if (PagePrivate2(page)) { |
| 8353 | ClearPagePrivate2(page); |
| 8354 | SetPagePrivate2(newpage); |
| 8355 | } |
| 8356 | |
| 8357 | if (mode != MIGRATE_SYNC_NO_COPY) |
| 8358 | migrate_page_copy(newpage, page); |
| 8359 | else |
| 8360 | migrate_page_states(newpage, page); |
| 8361 | return MIGRATEPAGE_SUCCESS; |
| 8362 | } |
| 8363 | #endif |
| 8364 | |
Lukas Czerner | d47992f | 2013-05-21 23:17:23 -0400 | [diff] [blame] | 8365 | static void btrfs_invalidatepage(struct page *page, unsigned int offset, |
| 8366 | unsigned int length) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8367 | { |
Nikolay Borisov | 53ac7ea | 2020-08-31 14:42:43 +0300 | [diff] [blame] | 8368 | struct btrfs_inode *inode = BTRFS_I(page->mapping->host); |
| 8369 | struct extent_io_tree *tree = &inode->io_tree; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8370 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 8371 | struct extent_state *cached_state = NULL; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8372 | u64 page_start = page_offset(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 8373 | u64 page_end = page_start + PAGE_SIZE - 1; |
Chandan Rajendra | dbfdb6d | 2016-01-21 15:55:58 +0530 | [diff] [blame] | 8374 | u64 start; |
| 8375 | u64 end; |
Nikolay Borisov | 53ac7ea | 2020-08-31 14:42:43 +0300 | [diff] [blame] | 8376 | int inode_evicting = inode->vfs_inode.i_state & I_FREEING; |
Filipe Manana | 2766ff6 | 2020-11-04 11:07:34 +0000 | [diff] [blame] | 8377 | bool found_ordered = false; |
| 8378 | bool completed_ordered = false; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8379 | |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 8380 | /* |
| 8381 | * we have the page locked, so new writeback can't start, |
| 8382 | * and the dirty bit won't be cleared while we are here. |
| 8383 | * |
| 8384 | * Wait for IO on this page so that we can safely clear |
| 8385 | * the PagePrivate2 bit and do ordered accounting |
| 8386 | */ |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8387 | wait_on_page_writeback(page); |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 8388 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8389 | if (offset) { |
| 8390 | btrfs_releasepage(page, GFP_NOFS); |
| 8391 | return; |
| 8392 | } |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 8393 | |
| 8394 | if (!inode_evicting) |
David Sterba | ff13db4 | 2015-12-03 14:30:40 +0100 | [diff] [blame] | 8395 | lock_extent_bits(tree, page_start, page_end, &cached_state); |
Qu Wenruo | 951c80f | 2021-01-27 14:38:48 +0800 | [diff] [blame] | 8396 | |
Chandan Rajendra | dbfdb6d | 2016-01-21 15:55:58 +0530 | [diff] [blame] | 8397 | start = page_start; |
Qu Wenruo | 951c80f | 2021-01-27 14:38:48 +0800 | [diff] [blame] | 8398 | again: |
Nikolay Borisov | 53ac7ea | 2020-08-31 14:42:43 +0300 | [diff] [blame] | 8399 | ordered = btrfs_lookup_ordered_range(inode, start, page_end - start + 1); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8400 | if (ordered) { |
Filipe Manana | 2766ff6 | 2020-11-04 11:07:34 +0000 | [diff] [blame] | 8401 | found_ordered = true; |
Omar Sandoval | bffe633 | 2019-12-02 17:34:19 -0800 | [diff] [blame] | 8402 | end = min(page_end, |
| 8403 | ordered->file_offset + ordered->num_bytes - 1); |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 8404 | /* |
Filipe Manana | 2766ff6 | 2020-11-04 11:07:34 +0000 | [diff] [blame] | 8405 | * IO on this page will never be started, so we need to account |
| 8406 | * for any ordered extents now. Don't clear EXTENT_DELALLOC_NEW |
| 8407 | * here, must leave that up for the ordered extent completion. |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 8408 | */ |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 8409 | if (!inode_evicting) |
Chandan Rajendra | dbfdb6d | 2016-01-21 15:55:58 +0530 | [diff] [blame] | 8410 | clear_extent_bit(tree, start, end, |
Filipe Manana | 2766ff6 | 2020-11-04 11:07:34 +0000 | [diff] [blame] | 8411 | EXTENT_DELALLOC | |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 8412 | EXTENT_LOCKED | EXTENT_DO_ACCOUNTING | |
David Sterba | ae0f162 | 2017-10-31 16:37:52 +0100 | [diff] [blame] | 8413 | EXTENT_DEFRAG, 1, 0, &cached_state); |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 8414 | /* |
| 8415 | * whoever cleared the private bit is responsible |
| 8416 | * for the finish_ordered_io |
| 8417 | */ |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 8418 | if (TestClearPagePrivate2(page)) { |
| 8419 | struct btrfs_ordered_inode_tree *tree; |
| 8420 | u64 new_len; |
| 8421 | |
Nikolay Borisov | 53ac7ea | 2020-08-31 14:42:43 +0300 | [diff] [blame] | 8422 | tree = &inode->ordered_tree; |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 8423 | |
| 8424 | spin_lock_irq(&tree->lock); |
| 8425 | set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags); |
Chandan Rajendra | dbfdb6d | 2016-01-21 15:55:58 +0530 | [diff] [blame] | 8426 | new_len = start - ordered->file_offset; |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 8427 | if (new_len < ordered->truncated_len) |
| 8428 | ordered->truncated_len = new_len; |
| 8429 | spin_unlock_irq(&tree->lock); |
| 8430 | |
Nikolay Borisov | 53ac7ea | 2020-08-31 14:42:43 +0300 | [diff] [blame] | 8431 | if (btrfs_dec_test_ordered_pending(inode, &ordered, |
| 8432 | start, |
Filipe Manana | 2766ff6 | 2020-11-04 11:07:34 +0000 | [diff] [blame] | 8433 | end - start + 1, 1)) { |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 8434 | btrfs_finish_ordered_io(ordered); |
Filipe Manana | 2766ff6 | 2020-11-04 11:07:34 +0000 | [diff] [blame] | 8435 | completed_ordered = true; |
| 8436 | } |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 8437 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8438 | btrfs_put_ordered_extent(ordered); |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 8439 | if (!inode_evicting) { |
| 8440 | cached_state = NULL; |
Chandan Rajendra | dbfdb6d | 2016-01-21 15:55:58 +0530 | [diff] [blame] | 8441 | lock_extent_bits(tree, start, end, |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 8442 | &cached_state); |
| 8443 | } |
Chandan Rajendra | dbfdb6d | 2016-01-21 15:55:58 +0530 | [diff] [blame] | 8444 | |
| 8445 | start = end + 1; |
| 8446 | if (start < page_end) |
| 8447 | goto again; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8448 | } |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 8449 | |
Qu Wenruo | b9d0b38 | 2015-09-29 10:35:16 +0800 | [diff] [blame] | 8450 | /* |
| 8451 | * Qgroup reserved space handler |
| 8452 | * Page here will be either |
Qu Wenruo | fa91e4a | 2020-07-17 15:12:05 +0800 | [diff] [blame] | 8453 | * 1) Already written to disk or ordered extent already submitted |
| 8454 | * Then its QGROUP_RESERVED bit in io_tree is already cleaned. |
| 8455 | * Qgroup will be handled by its qgroup_record then. |
| 8456 | * btrfs_qgroup_free_data() call will do nothing here. |
| 8457 | * |
| 8458 | * 2) Not written to disk yet |
| 8459 | * Then btrfs_qgroup_free_data() call will clear the QGROUP_RESERVED |
| 8460 | * bit of its io_tree, and free the qgroup reserved data space. |
| 8461 | * Since the IO will never happen for this page. |
Qu Wenruo | b9d0b38 | 2015-09-29 10:35:16 +0800 | [diff] [blame] | 8462 | */ |
Nikolay Borisov | 53ac7ea | 2020-08-31 14:42:43 +0300 | [diff] [blame] | 8463 | btrfs_qgroup_free_data(inode, NULL, page_start, PAGE_SIZE); |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 8464 | if (!inode_evicting) { |
Filipe Manana | 2766ff6 | 2020-11-04 11:07:34 +0000 | [diff] [blame] | 8465 | bool delete = true; |
| 8466 | |
| 8467 | /* |
| 8468 | * If there's an ordered extent for this range and we have not |
| 8469 | * finished it ourselves, we must leave EXTENT_DELALLOC_NEW set |
| 8470 | * in the range for the ordered extent completion. We must also |
| 8471 | * not delete the range, otherwise we would lose that bit (and |
| 8472 | * any other bits set in the range). Make sure EXTENT_UPTODATE |
| 8473 | * is cleared if we don't delete, otherwise it can lead to |
| 8474 | * corruptions if the i_size is extented later. |
| 8475 | */ |
| 8476 | if (found_ordered && !completed_ordered) |
| 8477 | delete = false; |
Omar Sandoval | e182163 | 2019-08-15 14:04:04 -0700 | [diff] [blame] | 8478 | clear_extent_bit(tree, page_start, page_end, EXTENT_LOCKED | |
Filipe Manana | 2766ff6 | 2020-11-04 11:07:34 +0000 | [diff] [blame] | 8479 | EXTENT_DELALLOC | EXTENT_UPTODATE | |
| 8480 | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, |
| 8481 | delete, &cached_state); |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 8482 | |
| 8483 | __btrfs_releasepage(page, GFP_NOFS); |
| 8484 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8485 | |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 8486 | ClearPageChecked(page); |
Qu Wenruo | 32443de | 2021-01-26 16:34:00 +0800 | [diff] [blame] | 8487 | clear_page_extent_mapped(page); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8488 | } |
| 8489 | |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8490 | /* |
| 8491 | * btrfs_page_mkwrite() is not allowed to change the file size as it gets |
| 8492 | * called from a page fault handler when a page is first dirtied. Hence we must |
| 8493 | * be careful to check for EOF conditions here. We set the page up correctly |
| 8494 | * for a written page which means we get ENOSPC checking when writing into |
| 8495 | * holes and correct delalloc and unwritten extent mapping on filesystems that |
| 8496 | * support these features. |
| 8497 | * |
| 8498 | * We are not allowed to take the i_mutex here so we have to play games to |
| 8499 | * 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] | 8500 | * truncate_setsize() writes the inode size before removing pages, once we have |
| 8501 | * 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] | 8502 | * beyond EOF, then the page is guaranteed safe against truncation until we |
| 8503 | * unlock the page. |
| 8504 | */ |
Souptick Joarder | a528a24 | 2018-06-06 19:54:44 +0530 | [diff] [blame] | 8505 | vm_fault_t btrfs_page_mkwrite(struct vm_fault *vmf) |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8506 | { |
Nick Piggin | c2ec175 | 2009-03-31 15:23:21 -0700 | [diff] [blame] | 8507 | struct page *page = vmf->page; |
Dave Jiang | 11bac80 | 2017-02-24 14:56:41 -0800 | [diff] [blame] | 8508 | struct inode *inode = file_inode(vmf->vma->vm_file); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8509 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8510 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
| 8511 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 8512 | struct extent_state *cached_state = NULL; |
Qu Wenruo | 364ecf3 | 2017-02-27 15:10:38 +0800 | [diff] [blame] | 8513 | struct extent_changeset *data_reserved = NULL; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8514 | char *kaddr; |
| 8515 | unsigned long zero_start; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8516 | loff_t size; |
Souptick Joarder | a528a24 | 2018-06-06 19:54:44 +0530 | [diff] [blame] | 8517 | vm_fault_t ret; |
| 8518 | int ret2; |
Chris Mason | 9998eb7 | 2012-01-25 13:47:40 -0500 | [diff] [blame] | 8519 | int reserved = 0; |
Chandan Rajendra | d0b7da88 | 2016-01-21 15:55:57 +0530 | [diff] [blame] | 8520 | u64 reserved_space; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8521 | u64 page_start; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8522 | u64 page_end; |
Chandan Rajendra | d0b7da88 | 2016-01-21 15:55:57 +0530 | [diff] [blame] | 8523 | u64 end; |
| 8524 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 8525 | reserved_space = PAGE_SIZE; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8526 | |
Jan Kara | b2b5ef5 | 2012-06-12 16:20:45 +0200 | [diff] [blame] | 8527 | sb_start_pagefault(inode->i_sb); |
Qu Wenruo | df48063 | 2015-09-08 17:25:54 +0800 | [diff] [blame] | 8528 | page_start = page_offset(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 8529 | page_end = page_start + PAGE_SIZE - 1; |
Chandan Rajendra | d0b7da88 | 2016-01-21 15:55:57 +0530 | [diff] [blame] | 8530 | end = page_end; |
Qu Wenruo | df48063 | 2015-09-08 17:25:54 +0800 | [diff] [blame] | 8531 | |
Chandan Rajendra | d0b7da88 | 2016-01-21 15:55:57 +0530 | [diff] [blame] | 8532 | /* |
| 8533 | * Reserving delalloc space after obtaining the page lock can lead to |
| 8534 | * deadlock. For example, if a dirty page is locked by this function |
| 8535 | * and the call to btrfs_delalloc_reserve_space() ends up triggering |
| 8536 | * dirty page write out, then the btrfs_writepage() function could |
| 8537 | * end up waiting indefinitely to get a lock on the page currently |
| 8538 | * being processed by btrfs_page_mkwrite() function. |
| 8539 | */ |
Nikolay Borisov | e5b7231e | 2020-06-03 08:55:42 +0300 | [diff] [blame] | 8540 | ret2 = btrfs_delalloc_reserve_space(BTRFS_I(inode), &data_reserved, |
| 8541 | page_start, reserved_space); |
Souptick Joarder | a528a24 | 2018-06-06 19:54:44 +0530 | [diff] [blame] | 8542 | if (!ret2) { |
| 8543 | ret2 = file_update_time(vmf->vma->vm_file); |
Chris Mason | 9998eb7 | 2012-01-25 13:47:40 -0500 | [diff] [blame] | 8544 | reserved = 1; |
| 8545 | } |
Souptick Joarder | a528a24 | 2018-06-06 19:54:44 +0530 | [diff] [blame] | 8546 | if (ret2) { |
| 8547 | ret = vmf_error(ret2); |
Chris Mason | 9998eb7 | 2012-01-25 13:47:40 -0500 | [diff] [blame] | 8548 | if (reserved) |
| 8549 | goto out; |
| 8550 | goto out_noreserve; |
Nick Piggin | 56a76f8 | 2009-03-31 15:23:23 -0700 | [diff] [blame] | 8551 | } |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 8552 | |
Nick Piggin | 56a76f8 | 2009-03-31 15:23:23 -0700 | [diff] [blame] | 8553 | ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */ |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8554 | again: |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8555 | lock_page(page); |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8556 | size = i_size_read(inode); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8557 | |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8558 | if ((page->mapping != inode->i_mapping) || |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8559 | (page_start >= size)) { |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8560 | /* page got truncated out from underneath us */ |
| 8561 | goto out_unlock; |
| 8562 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8563 | wait_on_page_writeback(page); |
| 8564 | |
David Sterba | ff13db4 | 2015-12-03 14:30:40 +0100 | [diff] [blame] | 8565 | lock_extent_bits(io_tree, page_start, page_end, &cached_state); |
Qu Wenruo | 32443de | 2021-01-26 16:34:00 +0800 | [diff] [blame] | 8566 | ret2 = set_page_extent_mapped(page); |
| 8567 | if (ret2 < 0) { |
| 8568 | ret = vmf_error(ret2); |
| 8569 | unlock_extent_cached(io_tree, page_start, page_end, &cached_state); |
| 8570 | goto out_unlock; |
| 8571 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8572 | |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 8573 | /* |
| 8574 | * we can't set the delalloc bits if there are pending ordered |
| 8575 | * extents. Drop our locks and wait for them to finish |
| 8576 | */ |
Nikolay Borisov | a776c6f | 2017-02-20 13:50:49 +0200 | [diff] [blame] | 8577 | ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start, |
| 8578 | PAGE_SIZE); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8579 | if (ordered) { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 8580 | unlock_extent_cached(io_tree, page_start, page_end, |
David Sterba | e43bbe5 | 2017-12-12 21:43:52 +0100 | [diff] [blame] | 8581 | &cached_state); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8582 | unlock_page(page); |
Nikolay Borisov | c0a4360 | 2020-09-18 12:15:53 +0300 | [diff] [blame] | 8583 | btrfs_start_ordered_extent(ordered, 1); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8584 | btrfs_put_ordered_extent(ordered); |
| 8585 | goto again; |
| 8586 | } |
| 8587 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 8588 | if (page->index == ((size - 1) >> PAGE_SHIFT)) { |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 8589 | reserved_space = round_up(size - page_start, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8590 | fs_info->sectorsize); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 8591 | if (reserved_space < PAGE_SIZE) { |
Chandan Rajendra | d0b7da88 | 2016-01-21 15:55:57 +0530 | [diff] [blame] | 8592 | end = page_start + reserved_space - 1; |
Nikolay Borisov | 86d5292 | 2020-06-03 08:55:40 +0300 | [diff] [blame] | 8593 | btrfs_delalloc_release_space(BTRFS_I(inode), |
| 8594 | data_reserved, page_start, |
| 8595 | PAGE_SIZE - reserved_space, true); |
Chandan Rajendra | d0b7da88 | 2016-01-21 15:55:57 +0530 | [diff] [blame] | 8596 | } |
| 8597 | } |
| 8598 | |
Josef Bacik | fbf1908 | 2009-10-01 17:10:23 -0400 | [diff] [blame] | 8599 | /* |
Liu Bo | 5416034 | 2016-12-13 12:15:19 -0800 | [diff] [blame] | 8600 | * page_mkwrite gets called when the page is firstly dirtied after it's |
| 8601 | * faulted in, but write(2) could also dirty a page and set delalloc |
| 8602 | * bits, thus in this case for space account reason, we still need to |
| 8603 | * clear any delalloc bits within this page range since we have to |
| 8604 | * reserve data&meta space before lock_page() (see above comments). |
Josef Bacik | fbf1908 | 2009-10-01 17:10:23 -0400 | [diff] [blame] | 8605 | */ |
Chandan Rajendra | d0b7da88 | 2016-01-21 15:55:57 +0530 | [diff] [blame] | 8606 | clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, end, |
Omar Sandoval | e182163 | 2019-08-15 14:04:04 -0700 | [diff] [blame] | 8607 | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING | |
| 8608 | EXTENT_DEFRAG, 0, 0, &cached_state); |
Josef Bacik | fbf1908 | 2009-10-01 17:10:23 -0400 | [diff] [blame] | 8609 | |
Nikolay Borisov | c2566f2 | 2020-06-03 08:55:35 +0300 | [diff] [blame] | 8610 | ret2 = btrfs_set_extent_delalloc(BTRFS_I(inode), page_start, end, 0, |
Nikolay Borisov | 330a582 | 2019-07-17 16:18:17 +0300 | [diff] [blame] | 8611 | &cached_state); |
Souptick Joarder | a528a24 | 2018-06-06 19:54:44 +0530 | [diff] [blame] | 8612 | if (ret2) { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 8613 | unlock_extent_cached(io_tree, page_start, page_end, |
David Sterba | e43bbe5 | 2017-12-12 21:43:52 +0100 | [diff] [blame] | 8614 | &cached_state); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 8615 | ret = VM_FAULT_SIGBUS; |
| 8616 | goto out_unlock; |
| 8617 | } |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8618 | |
| 8619 | /* page is wholly or partially inside EOF */ |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 8620 | if (page_start + PAGE_SIZE > size) |
Johannes Thumshirn | 7073017 | 2018-12-05 15:23:03 +0100 | [diff] [blame] | 8621 | zero_start = offset_in_page(size); |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8622 | else |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 8623 | zero_start = PAGE_SIZE; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8624 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 8625 | if (zero_start != PAGE_SIZE) { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8626 | kaddr = kmap(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 8627 | memset(kaddr + zero_start, 0, PAGE_SIZE - zero_start); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8628 | flush_dcache_page(page); |
| 8629 | kunmap(page); |
| 8630 | } |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 8631 | ClearPageChecked(page); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8632 | set_page_dirty(page); |
Chris Mason | 50a9b21 | 2009-09-11 12:33:12 -0400 | [diff] [blame] | 8633 | SetPageUptodate(page); |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 8634 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8635 | BTRFS_I(inode)->last_trans = fs_info->generation; |
Chris Mason | 257c62e | 2009-10-13 13:21:08 -0400 | [diff] [blame] | 8636 | BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid; |
Liu Bo | 46d8bc3 | 2012-08-29 01:07:55 -0600 | [diff] [blame] | 8637 | BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit; |
Chris Mason | 257c62e | 2009-10-13 13:21:08 -0400 | [diff] [blame] | 8638 | |
David Sterba | e43bbe5 | 2017-12-12 21:43:52 +0100 | [diff] [blame] | 8639 | unlock_extent_cached(io_tree, page_start, page_end, &cached_state); |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8640 | |
Yunfeng Ye | 76de60e | 2019-12-03 16:59:25 +0800 | [diff] [blame] | 8641 | btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE); |
| 8642 | sb_end_pagefault(inode->i_sb); |
| 8643 | extent_changeset_free(data_reserved); |
| 8644 | return VM_FAULT_LOCKED; |
Chris Mason | 717beb9 | 2018-06-25 10:03:41 -0700 | [diff] [blame] | 8645 | |
| 8646 | out_unlock: |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8647 | unlock_page(page); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 8648 | out: |
Qu Wenruo | 8702ba9 | 2019-10-14 14:34:51 +0800 | [diff] [blame] | 8649 | btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE); |
Nikolay Borisov | 86d5292 | 2020-06-03 08:55:40 +0300 | [diff] [blame] | 8650 | btrfs_delalloc_release_space(BTRFS_I(inode), data_reserved, page_start, |
Qu Wenruo | 43b1859 | 2017-12-12 15:34:32 +0800 | [diff] [blame] | 8651 | reserved_space, (ret != 0)); |
Chris Mason | 9998eb7 | 2012-01-25 13:47:40 -0500 | [diff] [blame] | 8652 | out_noreserve: |
Jan Kara | b2b5ef5 | 2012-06-12 16:20:45 +0200 | [diff] [blame] | 8653 | sb_end_pagefault(inode->i_sb); |
Qu Wenruo | 364ecf3 | 2017-02-27 15:10:38 +0800 | [diff] [blame] | 8654 | extent_changeset_free(data_reserved); |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8655 | return ret; |
| 8656 | } |
| 8657 | |
Filipe Manana | 213e8c5 | 2018-02-06 20:40:31 +0000 | [diff] [blame] | 8658 | static int btrfs_truncate(struct inode *inode, bool skip_writeback) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8659 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8660 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8661 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 8662 | struct btrfs_block_rsv *rsv; |
Omar Sandoval | ad7e1a7 | 2018-05-22 09:59:50 -0700 | [diff] [blame] | 8663 | int ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8664 | struct btrfs_trans_handle *trans; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8665 | u64 mask = fs_info->sectorsize - 1; |
Josef Bacik | 2bd36e7 | 2019-08-22 15:14:33 -0400 | [diff] [blame] | 8666 | u64 min_size = btrfs_calc_metadata_size(fs_info, 1); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8667 | |
Filipe Manana | 213e8c5 | 2018-02-06 20:40:31 +0000 | [diff] [blame] | 8668 | if (!skip_writeback) { |
| 8669 | ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask), |
| 8670 | (u64)-1); |
| 8671 | if (ret) |
| 8672 | return ret; |
| 8673 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8674 | |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 8675 | /* |
Omar Sandoval | f7e9e8f | 2018-05-11 13:13:32 -0700 | [diff] [blame] | 8676 | * Yes ladies and gentlemen, this is indeed ugly. We have a couple of |
| 8677 | * things going on here: |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 8678 | * |
Omar Sandoval | f7e9e8f | 2018-05-11 13:13:32 -0700 | [diff] [blame] | 8679 | * 1) We need to reserve space to update our inode. |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 8680 | * |
Omar Sandoval | f7e9e8f | 2018-05-11 13:13:32 -0700 | [diff] [blame] | 8681 | * 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] | 8682 | * be free'd up by the truncate operation, but also have some slack |
| 8683 | * space reserved in case it uses space during the truncate (thank you |
| 8684 | * very much snapshotting). |
| 8685 | * |
Omar Sandoval | f7e9e8f | 2018-05-11 13:13:32 -0700 | [diff] [blame] | 8686 | * 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] | 8687 | * 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] | 8688 | * 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] | 8689 | * doesn't end up using space reserved for updating the inode. We also |
| 8690 | * need to be able to stop the transaction and start a new one, which |
| 8691 | * means we need to be able to update the inode several times, and we |
| 8692 | * have no idea of knowing how many times that will be, so we can't just |
| 8693 | * reserve 1 item for the entirety of the operation, so that has to be |
| 8694 | * done separately as well. |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 8695 | * |
| 8696 | * So that leaves us with |
| 8697 | * |
Omar Sandoval | f7e9e8f | 2018-05-11 13:13:32 -0700 | [diff] [blame] | 8698 | * 1) rsv - for the truncate reservation, which we will steal from the |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 8699 | * transaction reservation. |
Omar Sandoval | f7e9e8f | 2018-05-11 13:13:32 -0700 | [diff] [blame] | 8700 | * 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] | 8701 | * updating the inode. |
| 8702 | */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 8703 | rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP); |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 8704 | if (!rsv) |
| 8705 | return -ENOMEM; |
Josef Bacik | 4a33854 | 2011-08-29 11:01:31 -0400 | [diff] [blame] | 8706 | rsv->size = min_size; |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 8707 | rsv->failfast = 1; |
Josef Bacik | f0cd846 | 2011-03-04 14:37:08 -0500 | [diff] [blame] | 8708 | |
Josef Bacik | 907cbce | 2011-08-08 13:46:15 -0400 | [diff] [blame] | 8709 | /* |
Josef Bacik | 0712718 | 2011-08-19 10:29:59 -0400 | [diff] [blame] | 8710 | * 1 for the truncate slack space |
Josef Bacik | 907cbce | 2011-08-08 13:46:15 -0400 | [diff] [blame] | 8711 | * 1 for updating the inode. |
| 8712 | */ |
Josef Bacik | f3fe820 | 2013-01-07 17:03:21 -0500 | [diff] [blame] | 8713 | trans = btrfs_start_transaction(root, 2); |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 8714 | if (IS_ERR(trans)) { |
Omar Sandoval | ad7e1a7 | 2018-05-22 09:59:50 -0700 | [diff] [blame] | 8715 | ret = PTR_ERR(trans); |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 8716 | goto out; |
| 8717 | } |
Josef Bacik | f0cd846 | 2011-03-04 14:37:08 -0500 | [diff] [blame] | 8718 | |
Josef Bacik | 907cbce | 2011-08-08 13:46:15 -0400 | [diff] [blame] | 8719 | /* Migrate the slack space for the truncate to our reserve */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8720 | ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, rsv, |
Lu Fengqi | 3a58417 | 2018-08-04 21:10:55 +0800 | [diff] [blame] | 8721 | min_size, false); |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 8722 | BUG_ON(ret); |
Josef Bacik | f0cd846 | 2011-03-04 14:37:08 -0500 | [diff] [blame] | 8723 | |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 8724 | /* |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 8725 | * So if we truncate and then write and fsync we normally would just |
| 8726 | * write the extents that changed, which is a problem if we need to |
| 8727 | * first truncate that entire inode. So set this flag so we write out |
| 8728 | * all of the extents in the inode to the sync log so we're completely |
| 8729 | * safe. |
| 8730 | */ |
| 8731 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags); |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 8732 | trans->block_rsv = rsv; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 8733 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 8734 | while (1) { |
Nikolay Borisov | 5074339 | 2020-11-02 16:48:55 +0200 | [diff] [blame] | 8735 | ret = btrfs_truncate_inode_items(trans, root, BTRFS_I(inode), |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 8736 | inode->i_size, |
| 8737 | BTRFS_EXTENT_DATA_KEY); |
Josef Bacik | ddfae63 | 2017-10-19 14:16:02 -0400 | [diff] [blame] | 8738 | trans->block_rsv = &fs_info->trans_block_rsv; |
Omar Sandoval | ad7e1a7 | 2018-05-22 09:59:50 -0700 | [diff] [blame] | 8739 | if (ret != -ENOSPC && ret != -EAGAIN) |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 8740 | break; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8741 | |
Nikolay Borisov | 9a56fcd | 2020-11-02 16:48:59 +0200 | [diff] [blame] | 8742 | ret = btrfs_update_inode(trans, root, BTRFS_I(inode)); |
Omar Sandoval | ad7e1a7 | 2018-05-22 09:59:50 -0700 | [diff] [blame] | 8743 | if (ret) |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 8744 | break; |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 8745 | |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 8746 | btrfs_end_transaction(trans); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 8747 | btrfs_btree_balance_dirty(fs_info); |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 8748 | |
| 8749 | trans = btrfs_start_transaction(root, 2); |
| 8750 | if (IS_ERR(trans)) { |
Omar Sandoval | ad7e1a7 | 2018-05-22 09:59:50 -0700 | [diff] [blame] | 8751 | ret = PTR_ERR(trans); |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 8752 | trans = NULL; |
| 8753 | break; |
| 8754 | } |
| 8755 | |
Nikolay Borisov | 63f018b | 2020-03-10 10:59:31 +0200 | [diff] [blame] | 8756 | btrfs_block_rsv_release(fs_info, rsv, -1, NULL); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8757 | ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, |
Lu Fengqi | 3a58417 | 2018-08-04 21:10:55 +0800 | [diff] [blame] | 8758 | rsv, min_size, false); |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 8759 | BUG_ON(ret); /* shouldn't happen */ |
| 8760 | trans->block_rsv = rsv; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 8761 | } |
| 8762 | |
Josef Bacik | ddfae63 | 2017-10-19 14:16:02 -0400 | [diff] [blame] | 8763 | /* |
| 8764 | * We can't call btrfs_truncate_block inside a trans handle as we could |
| 8765 | * deadlock with freeze, if we got NEED_TRUNCATE_BLOCK then we know |
| 8766 | * we've truncated everything except the last little bit, and can do |
| 8767 | * btrfs_truncate_block and then update the disk_i_size. |
| 8768 | */ |
| 8769 | if (ret == NEED_TRUNCATE_BLOCK) { |
| 8770 | btrfs_end_transaction(trans); |
| 8771 | btrfs_btree_balance_dirty(fs_info); |
| 8772 | |
Nikolay Borisov | 217f42e | 2020-11-02 16:49:03 +0200 | [diff] [blame] | 8773 | ret = btrfs_truncate_block(BTRFS_I(inode), inode->i_size, 0, 0); |
Josef Bacik | ddfae63 | 2017-10-19 14:16:02 -0400 | [diff] [blame] | 8774 | if (ret) |
| 8775 | goto out; |
| 8776 | trans = btrfs_start_transaction(root, 1); |
| 8777 | if (IS_ERR(trans)) { |
| 8778 | ret = PTR_ERR(trans); |
| 8779 | goto out; |
| 8780 | } |
Nikolay Borisov | 76aea53 | 2020-11-02 16:48:53 +0200 | [diff] [blame] | 8781 | btrfs_inode_safe_disk_i_size_write(BTRFS_I(inode), 0); |
Josef Bacik | ddfae63 | 2017-10-19 14:16:02 -0400 | [diff] [blame] | 8782 | } |
| 8783 | |
Chris Mason | 917c16b | 2011-11-08 14:49:59 -0500 | [diff] [blame] | 8784 | if (trans) { |
Omar Sandoval | ad7e1a7 | 2018-05-22 09:59:50 -0700 | [diff] [blame] | 8785 | int ret2; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 8786 | |
Omar Sandoval | ad7e1a7 | 2018-05-22 09:59:50 -0700 | [diff] [blame] | 8787 | trans->block_rsv = &fs_info->trans_block_rsv; |
Nikolay Borisov | 9a56fcd | 2020-11-02 16:48:59 +0200 | [diff] [blame] | 8788 | ret2 = btrfs_update_inode(trans, root, BTRFS_I(inode)); |
Omar Sandoval | ad7e1a7 | 2018-05-22 09:59:50 -0700 | [diff] [blame] | 8789 | if (ret2 && !ret) |
| 8790 | ret = ret2; |
| 8791 | |
| 8792 | ret2 = btrfs_end_transaction(trans); |
| 8793 | if (ret2 && !ret) |
| 8794 | ret = ret2; |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 8795 | btrfs_btree_balance_dirty(fs_info); |
Chris Mason | 917c16b | 2011-11-08 14:49:59 -0500 | [diff] [blame] | 8796 | } |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 8797 | out: |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 8798 | btrfs_free_block_rsv(fs_info, rsv); |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 8799 | |
Omar Sandoval | ad7e1a7 | 2018-05-22 09:59:50 -0700 | [diff] [blame] | 8800 | return ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8801 | } |
| 8802 | |
Sven Wegener | 3b96362 | 2008-06-09 21:57:42 -0400 | [diff] [blame] | 8803 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 8804 | * create a new subvolume directory/inode (helper for the ioctl). |
| 8805 | */ |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 8806 | int btrfs_create_subvol_root(struct btrfs_trans_handle *trans, |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 8807 | struct btrfs_root *new_root, |
Nikolay Borisov | 6994802 | 2020-12-07 17:32:37 +0200 | [diff] [blame] | 8808 | struct btrfs_root *parent_root) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8809 | { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8810 | struct inode *inode; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 8811 | int err; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 8812 | u64 index = 0; |
Nikolay Borisov | 2312510 | 2020-12-07 17:32:36 +0200 | [diff] [blame] | 8813 | u64 ino; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8814 | |
Nikolay Borisov | 2312510 | 2020-12-07 17:32:36 +0200 | [diff] [blame] | 8815 | err = btrfs_get_free_objectid(new_root, &ino); |
| 8816 | if (err < 0) |
| 8817 | return err; |
| 8818 | |
| 8819 | inode = btrfs_new_inode(trans, new_root, NULL, "..", 2, ino, ino, |
Florian Albrechtskirchinger | 12fc9d0 | 2012-02-10 22:15:54 +0100 | [diff] [blame] | 8820 | S_IFDIR | (~current_umask() & S_IRWXUGO), |
| 8821 | &index); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 8822 | if (IS_ERR(inode)) |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 8823 | return PTR_ERR(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8824 | inode->i_op = &btrfs_dir_inode_operations; |
| 8825 | inode->i_fop = &btrfs_dir_file_operations; |
| 8826 | |
Miklos Szeredi | bfe8684 | 2011-10-28 14:13:29 +0200 | [diff] [blame] | 8827 | set_nlink(inode, 1); |
Nikolay Borisov | 6ef06d2 | 2017-02-20 13:50:34 +0200 | [diff] [blame] | 8828 | btrfs_i_size_write(BTRFS_I(inode), 0); |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 8829 | unlock_new_inode(inode); |
Sven Wegener | 3b96362 | 2008-06-09 21:57:42 -0400 | [diff] [blame] | 8830 | |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 8831 | err = btrfs_subvol_inherit_props(trans, new_root, parent_root); |
| 8832 | if (err) |
| 8833 | btrfs_err(new_root->fs_info, |
David Sterba | 351fd35 | 2014-05-15 16:48:20 +0200 | [diff] [blame] | 8834 | "error inheriting subvolume %llu properties: %d", |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 8835 | new_root->root_key.objectid, err); |
| 8836 | |
Nikolay Borisov | 9a56fcd | 2020-11-02 16:48:59 +0200 | [diff] [blame] | 8837 | err = btrfs_update_inode(trans, new_root, BTRFS_I(inode)); |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 8838 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 8839 | iput(inode); |
Mark Fasheh | ce59897 | 2011-07-26 11:32:23 -0700 | [diff] [blame] | 8840 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8841 | } |
| 8842 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8843 | struct inode *btrfs_alloc_inode(struct super_block *sb) |
| 8844 | { |
Josef Bacik | 69fe2d7 | 2017-10-19 14:15:57 -0400 | [diff] [blame] | 8845 | struct btrfs_fs_info *fs_info = btrfs_sb(sb); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8846 | struct btrfs_inode *ei; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8847 | struct inode *inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8848 | |
David Sterba | 712e36c | 2017-10-31 17:08:27 +0100 | [diff] [blame] | 8849 | ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_KERNEL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8850 | if (!ei) |
| 8851 | return NULL; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8852 | |
| 8853 | ei->root = NULL; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8854 | ei->generation = 0; |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 8855 | ei->last_trans = 0; |
Chris Mason | 257c62e | 2009-10-13 13:21:08 -0400 | [diff] [blame] | 8856 | ei->last_sub_trans = 0; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 8857 | ei->logged_trans = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8858 | ei->delalloc_bytes = 0; |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 8859 | ei->new_delalloc_bytes = 0; |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 8860 | ei->defrag_bytes = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8861 | ei->disk_i_size = 0; |
| 8862 | ei->flags = 0; |
Josef Bacik | 7709cde | 2011-08-04 10:25:02 -0400 | [diff] [blame] | 8863 | ei->csum_bytes = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8864 | ei->index_cnt = (u64)-1; |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 8865 | ei->dir_index = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8866 | ei->last_unlink_trans = 0; |
Filipe Manana | 3ebac17 | 2020-07-15 12:30:43 +0100 | [diff] [blame] | 8867 | ei->last_reflink_trans = 0; |
Liu Bo | 46d8bc3 | 2012-08-29 01:07:55 -0600 | [diff] [blame] | 8868 | ei->last_log_commit = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8869 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 8870 | spin_lock_init(&ei->lock); |
| 8871 | ei->outstanding_extents = 0; |
Josef Bacik | 69fe2d7 | 2017-10-19 14:15:57 -0400 | [diff] [blame] | 8872 | if (sb->s_magic != BTRFS_TEST_MAGIC) |
| 8873 | btrfs_init_metadata_block_rsv(fs_info, &ei->block_rsv, |
| 8874 | BTRFS_BLOCK_RSV_DELALLOC); |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 8875 | ei->runtime_flags = 0; |
David Sterba | b52aa8c | 2017-07-17 19:17:20 +0200 | [diff] [blame] | 8876 | ei->prop_compress = BTRFS_COMPRESS_NONE; |
David Sterba | eec63c6 | 2017-07-17 19:41:31 +0200 | [diff] [blame] | 8877 | ei->defrag_compress = BTRFS_COMPRESS_NONE; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8878 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 8879 | ei->delayed_node = NULL; |
| 8880 | |
chandan r | 9cc97d6 | 2012-07-04 12:48:07 +0530 | [diff] [blame] | 8881 | ei->i_otime.tv_sec = 0; |
| 8882 | ei->i_otime.tv_nsec = 0; |
| 8883 | |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8884 | inode = &ei->vfs_inode; |
David Sterba | a8067e0 | 2011-04-21 00:34:43 +0200 | [diff] [blame] | 8885 | extent_map_tree_init(&ei->extent_tree); |
Qu Wenruo | 43eb5f2 | 2019-03-01 10:47:59 +0800 | [diff] [blame] | 8886 | extent_io_tree_init(fs_info, &ei->io_tree, IO_TREE_INODE_IO, inode); |
| 8887 | extent_io_tree_init(fs_info, &ei->io_failure_tree, |
| 8888 | IO_TREE_INODE_IO_FAILURE, inode); |
Josef Bacik | 41a2ee7 | 2020-01-17 09:02:21 -0500 | [diff] [blame] | 8889 | extent_io_tree_init(fs_info, &ei->file_extent_tree, |
| 8890 | IO_TREE_INODE_FILE_EXTENT, inode); |
David Sterba | 7b43973 | 2019-03-11 15:58:30 +0100 | [diff] [blame] | 8891 | ei->io_tree.track_uptodate = true; |
| 8892 | ei->io_failure_tree.track_uptodate = true; |
Josef Bacik | b812ce2 | 2012-11-16 13:56:32 -0500 | [diff] [blame] | 8893 | atomic_set(&ei->sync_writers, 0); |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8894 | mutex_init(&ei->log_mutex); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8895 | btrfs_ordered_inode_tree_init(&ei->ordered_tree); |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8896 | INIT_LIST_HEAD(&ei->delalloc_inodes); |
David Sterba | 8089fe6 | 2015-11-19 14:15:51 +0100 | [diff] [blame] | 8897 | INIT_LIST_HEAD(&ei->delayed_iput); |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8898 | RB_CLEAR_NODE(&ei->rb_node); |
| 8899 | |
| 8900 | return inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8901 | } |
| 8902 | |
Josef Bacik | aaedb55 | 2013-10-11 14:44:09 -0400 | [diff] [blame] | 8903 | #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS |
| 8904 | void btrfs_test_destroy_inode(struct inode *inode) |
| 8905 | { |
Nikolay Borisov | dcdbc05 | 2017-02-20 13:50:45 +0200 | [diff] [blame] | 8906 | btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0); |
Josef Bacik | aaedb55 | 2013-10-11 14:44:09 -0400 | [diff] [blame] | 8907 | kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode)); |
| 8908 | } |
| 8909 | #endif |
| 8910 | |
Al Viro | 26602ca | 2019-04-10 15:14:41 -0400 | [diff] [blame] | 8911 | void btrfs_free_inode(struct inode *inode) |
Nick Piggin | fa0d7e3d | 2011-01-07 17:49:49 +1100 | [diff] [blame] | 8912 | { |
Nick Piggin | fa0d7e3d | 2011-01-07 17:49:49 +1100 | [diff] [blame] | 8913 | kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode)); |
| 8914 | } |
| 8915 | |
Nikolay Borisov | 633cc81 | 2020-09-18 12:15:49 +0300 | [diff] [blame] | 8916 | void btrfs_destroy_inode(struct inode *vfs_inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8917 | { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8918 | struct btrfs_ordered_extent *ordered; |
Nikolay Borisov | 633cc81 | 2020-09-18 12:15:49 +0300 | [diff] [blame] | 8919 | struct btrfs_inode *inode = BTRFS_I(vfs_inode); |
| 8920 | struct btrfs_root *root = inode->root; |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 8921 | |
Nikolay Borisov | 633cc81 | 2020-09-18 12:15:49 +0300 | [diff] [blame] | 8922 | WARN_ON(!hlist_empty(&vfs_inode->i_dentry)); |
| 8923 | WARN_ON(vfs_inode->i_data.nrpages); |
| 8924 | WARN_ON(inode->block_rsv.reserved); |
| 8925 | WARN_ON(inode->block_rsv.size); |
| 8926 | WARN_ON(inode->outstanding_extents); |
| 8927 | WARN_ON(inode->delalloc_bytes); |
| 8928 | WARN_ON(inode->new_delalloc_bytes); |
| 8929 | WARN_ON(inode->csum_bytes); |
| 8930 | WARN_ON(inode->defrag_bytes); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8931 | |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 8932 | /* |
Josef Bacik | a6dbd42 | 2009-11-11 15:53:34 -0500 | [diff] [blame] | 8933 | * This can happen where we create an inode, but somebody else also |
| 8934 | * created the same inode and we need to destroy the one we already |
| 8935 | * created. |
| 8936 | */ |
| 8937 | if (!root) |
Al Viro | 26602ca | 2019-04-10 15:14:41 -0400 | [diff] [blame] | 8938 | return; |
Josef Bacik | a6dbd42 | 2009-11-11 15:53:34 -0500 | [diff] [blame] | 8939 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 8940 | while (1) { |
Nikolay Borisov | 633cc81 | 2020-09-18 12:15:49 +0300 | [diff] [blame] | 8941 | ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8942 | if (!ordered) |
| 8943 | break; |
| 8944 | else { |
Nikolay Borisov | 633cc81 | 2020-09-18 12:15:49 +0300 | [diff] [blame] | 8945 | btrfs_err(root->fs_info, |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 8946 | "found ordered extent %llu %llu on inode cleanup", |
Omar Sandoval | bffe633 | 2019-12-02 17:34:19 -0800 | [diff] [blame] | 8947 | ordered->file_offset, ordered->num_bytes); |
Nikolay Borisov | 71fe0a5 | 2020-09-18 12:15:50 +0300 | [diff] [blame] | 8948 | btrfs_remove_ordered_extent(inode, ordered); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8949 | btrfs_put_ordered_extent(ordered); |
| 8950 | btrfs_put_ordered_extent(ordered); |
| 8951 | } |
| 8952 | } |
Nikolay Borisov | 633cc81 | 2020-09-18 12:15:49 +0300 | [diff] [blame] | 8953 | btrfs_qgroup_check_reserved_leak(inode); |
| 8954 | inode_tree_del(inode); |
| 8955 | btrfs_drop_extent_cache(inode, 0, (u64)-1, 0); |
| 8956 | btrfs_inode_clear_file_extent_range(inode, 0, (u64)-1); |
| 8957 | btrfs_put_root(inode->root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8958 | } |
| 8959 | |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 8960 | int btrfs_drop_inode(struct inode *inode) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 8961 | { |
| 8962 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 8963 | |
Naohiro Aota | 6379ef9 | 2013-06-06 09:56:34 +0000 | [diff] [blame] | 8964 | if (root == NULL) |
| 8965 | return 1; |
| 8966 | |
Liu Bo | fa6ac87 | 2013-02-20 14:10:23 +0000 | [diff] [blame] | 8967 | /* the snap/subvol tree is on deleting */ |
Stefan Behrens | 69e9c6c | 2013-09-05 16:58:43 +0200 | [diff] [blame] | 8968 | if (btrfs_root_refs(&root->root_item) == 0) |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 8969 | return 1; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 8970 | else |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 8971 | return generic_drop_inode(inode); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 8972 | } |
| 8973 | |
Sven Wegener | 0ee0fda | 2008-07-30 16:54:26 -0400 | [diff] [blame] | 8974 | static void init_once(void *foo) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8975 | { |
| 8976 | struct btrfs_inode *ei = (struct btrfs_inode *) foo; |
| 8977 | |
| 8978 | inode_init_once(&ei->vfs_inode); |
| 8979 | } |
| 8980 | |
David Sterba | e67c718 | 2018-02-19 17:24:18 +0100 | [diff] [blame] | 8981 | void __cold btrfs_destroy_cachep(void) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8982 | { |
Kirill A. Shutemov | 8c0a853 | 2012-09-26 11:33:07 +1000 | [diff] [blame] | 8983 | /* |
| 8984 | * Make sure all delayed rcu free inodes are flushed before we |
| 8985 | * destroy cache. |
| 8986 | */ |
| 8987 | rcu_barrier(); |
Kinglong Mee | 5598e90 | 2016-01-29 21:36:35 +0800 | [diff] [blame] | 8988 | kmem_cache_destroy(btrfs_inode_cachep); |
| 8989 | kmem_cache_destroy(btrfs_trans_handle_cachep); |
Kinglong Mee | 5598e90 | 2016-01-29 21:36:35 +0800 | [diff] [blame] | 8990 | kmem_cache_destroy(btrfs_path_cachep); |
| 8991 | kmem_cache_destroy(btrfs_free_space_cachep); |
Christophe Leroy | 3acd485 | 2019-08-21 15:05:55 +0000 | [diff] [blame] | 8992 | kmem_cache_destroy(btrfs_free_space_bitmap_cachep); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8993 | } |
| 8994 | |
Liu Bo | f5c29bd | 2017-11-02 17:21:50 -0600 | [diff] [blame] | 8995 | int __init btrfs_init_cachep(void) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8996 | { |
David Sterba | 837e197 | 2012-09-07 03:00:48 -0600 | [diff] [blame] | 8997 | btrfs_inode_cachep = kmem_cache_create("btrfs_inode", |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 8998 | sizeof(struct btrfs_inode), 0, |
Vladimir Davydov | 5d09705 | 2016-01-14 15:18:21 -0800 | [diff] [blame] | 8999 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT, |
| 9000 | init_once); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9001 | if (!btrfs_inode_cachep) |
| 9002 | goto fail; |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 9003 | |
David Sterba | 837e197 | 2012-09-07 03:00:48 -0600 | [diff] [blame] | 9004 | btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle", |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 9005 | sizeof(struct btrfs_trans_handle), 0, |
Nikolay Borisov | fba4b69 | 2016-06-23 21:17:08 +0300 | [diff] [blame] | 9006 | SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9007 | if (!btrfs_trans_handle_cachep) |
| 9008 | goto fail; |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 9009 | |
David Sterba | 837e197 | 2012-09-07 03:00:48 -0600 | [diff] [blame] | 9010 | btrfs_path_cachep = kmem_cache_create("btrfs_path", |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 9011 | sizeof(struct btrfs_path), 0, |
Nikolay Borisov | fba4b69 | 2016-06-23 21:17:08 +0300 | [diff] [blame] | 9012 | SLAB_MEM_SPREAD, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9013 | if (!btrfs_path_cachep) |
| 9014 | goto fail; |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 9015 | |
David Sterba | 837e197 | 2012-09-07 03:00:48 -0600 | [diff] [blame] | 9016 | btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space", |
Josef Bacik | dc89e98 | 2011-01-28 17:05:48 -0500 | [diff] [blame] | 9017 | sizeof(struct btrfs_free_space), 0, |
Nikolay Borisov | fba4b69 | 2016-06-23 21:17:08 +0300 | [diff] [blame] | 9018 | SLAB_MEM_SPREAD, NULL); |
Josef Bacik | dc89e98 | 2011-01-28 17:05:48 -0500 | [diff] [blame] | 9019 | if (!btrfs_free_space_cachep) |
| 9020 | goto fail; |
| 9021 | |
Christophe Leroy | 3acd485 | 2019-08-21 15:05:55 +0000 | [diff] [blame] | 9022 | btrfs_free_space_bitmap_cachep = kmem_cache_create("btrfs_free_space_bitmap", |
| 9023 | PAGE_SIZE, PAGE_SIZE, |
David Sterba | 34e4999 | 2021-03-15 15:18:24 +0100 | [diff] [blame] | 9024 | SLAB_MEM_SPREAD, NULL); |
Christophe Leroy | 3acd485 | 2019-08-21 15:05:55 +0000 | [diff] [blame] | 9025 | if (!btrfs_free_space_bitmap_cachep) |
| 9026 | goto fail; |
| 9027 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9028 | return 0; |
| 9029 | fail: |
| 9030 | btrfs_destroy_cachep(); |
| 9031 | return -ENOMEM; |
| 9032 | } |
| 9033 | |
Christian Brauner | 549c729 | 2021-01-21 14:19:43 +0100 | [diff] [blame] | 9034 | static int btrfs_getattr(struct user_namespace *mnt_userns, |
| 9035 | const struct path *path, struct kstat *stat, |
David Howells | a528d35 | 2017-01-31 16:46:22 +0000 | [diff] [blame] | 9036 | u32 request_mask, unsigned int flags) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9037 | { |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 9038 | u64 delalloc_bytes; |
Filipe Manana | 2766ff6 | 2020-11-04 11:07:34 +0000 | [diff] [blame] | 9039 | u64 inode_bytes; |
David Howells | a528d35 | 2017-01-31 16:46:22 +0000 | [diff] [blame] | 9040 | struct inode *inode = d_inode(path->dentry); |
David Sterba | fadc0d8 | 2011-11-20 07:33:38 -0500 | [diff] [blame] | 9041 | u32 blocksize = inode->i_sb->s_blocksize; |
Yonghong Song | 04a87e3 | 2017-05-12 15:07:43 -0700 | [diff] [blame] | 9042 | u32 bi_flags = BTRFS_I(inode)->flags; |
| 9043 | |
| 9044 | stat->result_mask |= STATX_BTIME; |
| 9045 | stat->btime.tv_sec = BTRFS_I(inode)->i_otime.tv_sec; |
| 9046 | stat->btime.tv_nsec = BTRFS_I(inode)->i_otime.tv_nsec; |
| 9047 | if (bi_flags & BTRFS_INODE_APPEND) |
| 9048 | stat->attributes |= STATX_ATTR_APPEND; |
| 9049 | if (bi_flags & BTRFS_INODE_COMPRESS) |
| 9050 | stat->attributes |= STATX_ATTR_COMPRESSED; |
| 9051 | if (bi_flags & BTRFS_INODE_IMMUTABLE) |
| 9052 | stat->attributes |= STATX_ATTR_IMMUTABLE; |
| 9053 | if (bi_flags & BTRFS_INODE_NODUMP) |
| 9054 | stat->attributes |= STATX_ATTR_NODUMP; |
| 9055 | |
| 9056 | stat->attributes_mask |= (STATX_ATTR_APPEND | |
| 9057 | STATX_ATTR_COMPRESSED | |
| 9058 | STATX_ATTR_IMMUTABLE | |
| 9059 | STATX_ATTR_NODUMP); |
David Sterba | fadc0d8 | 2011-11-20 07:33:38 -0500 | [diff] [blame] | 9060 | |
Christian Brauner | 0d56a45 | 2021-01-21 14:19:30 +0100 | [diff] [blame] | 9061 | generic_fillattr(&init_user_ns, inode, stat); |
Al Viro | 0ee5dc6 | 2011-07-07 15:44:25 -0400 | [diff] [blame] | 9062 | stat->dev = BTRFS_I(inode)->root->anon_dev; |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 9063 | |
| 9064 | spin_lock(&BTRFS_I(inode)->lock); |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 9065 | delalloc_bytes = BTRFS_I(inode)->new_delalloc_bytes; |
Filipe Manana | 2766ff6 | 2020-11-04 11:07:34 +0000 | [diff] [blame] | 9066 | inode_bytes = inode_get_bytes(inode); |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 9067 | spin_unlock(&BTRFS_I(inode)->lock); |
Filipe Manana | 2766ff6 | 2020-11-04 11:07:34 +0000 | [diff] [blame] | 9068 | stat->blocks = (ALIGN(inode_bytes, blocksize) + |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 9069 | ALIGN(delalloc_bytes, blocksize)) >> 9; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9070 | return 0; |
| 9071 | } |
| 9072 | |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9073 | static int btrfs_rename_exchange(struct inode *old_dir, |
| 9074 | struct dentry *old_dentry, |
| 9075 | struct inode *new_dir, |
| 9076 | struct dentry *new_dentry) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9077 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9078 | struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9079 | struct btrfs_trans_handle *trans; |
| 9080 | struct btrfs_root *root = BTRFS_I(old_dir)->root; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9081 | struct btrfs_root *dest = BTRFS_I(new_dir)->root; |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9082 | struct inode *new_inode = new_dentry->d_inode; |
| 9083 | struct inode *old_inode = old_dentry->d_inode; |
Deepa Dinamani | 95582b0 | 2018-05-08 19:36:02 -0700 | [diff] [blame] | 9084 | struct timespec64 ctime = current_time(old_inode); |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 9085 | u64 old_ino = btrfs_ino(BTRFS_I(old_inode)); |
| 9086 | u64 new_ino = btrfs_ino(BTRFS_I(new_inode)); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9087 | u64 old_idx = 0; |
| 9088 | u64 new_idx = 0; |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9089 | int ret; |
Filipe Manana | 75b463d | 2020-08-11 12:43:48 +0100 | [diff] [blame] | 9090 | int ret2; |
Filipe Manana | 86e8aa0 | 2016-05-05 02:02:27 +0100 | [diff] [blame] | 9091 | bool root_log_pinned = false; |
| 9092 | bool dest_log_pinned = false; |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9093 | |
| 9094 | /* we only allow rename subvolume link between subvolumes */ |
| 9095 | if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest) |
| 9096 | return -EXDEV; |
| 9097 | |
| 9098 | /* close the race window with snapshot create/destroy ioctl */ |
Josef Bacik | 943eb3b | 2019-11-19 13:59:20 -0500 | [diff] [blame] | 9099 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID || |
| 9100 | new_ino == BTRFS_FIRST_FREE_OBJECTID) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9101 | down_read(&fs_info->subvol_sem); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9102 | |
| 9103 | /* |
| 9104 | * We want to reserve the absolute worst case amount of items. So if |
| 9105 | * both inodes are subvols and we need to unlink them then that would |
| 9106 | * require 4 item modifications, but if they are both normal inodes it |
| 9107 | * would require 5 item modifications, so we'll assume their normal |
| 9108 | * inodes. So 5 * 2 is 10, plus 2 for the new links, so 12 total items |
| 9109 | * should cover the worst case number of items we'll modify. |
| 9110 | */ |
| 9111 | trans = btrfs_start_transaction(root, 12); |
| 9112 | if (IS_ERR(trans)) { |
| 9113 | ret = PTR_ERR(trans); |
| 9114 | goto out_notrans; |
| 9115 | } |
| 9116 | |
Josef Bacik | 3e17409 | 2019-11-15 15:43:06 -0500 | [diff] [blame] | 9117 | if (dest != root) |
| 9118 | btrfs_record_root_in_trans(trans, dest); |
| 9119 | |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9120 | /* |
| 9121 | * We need to find a free sequence number both in the source and |
| 9122 | * in the destination directory for the exchange. |
| 9123 | */ |
Nikolay Borisov | 877574e | 2017-02-20 13:50:33 +0200 | [diff] [blame] | 9124 | ret = btrfs_set_inode_index(BTRFS_I(new_dir), &old_idx); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9125 | if (ret) |
| 9126 | goto out_fail; |
Nikolay Borisov | 877574e | 2017-02-20 13:50:33 +0200 | [diff] [blame] | 9127 | ret = btrfs_set_inode_index(BTRFS_I(old_dir), &new_idx); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9128 | if (ret) |
| 9129 | goto out_fail; |
| 9130 | |
| 9131 | BTRFS_I(old_inode)->dir_index = 0ULL; |
| 9132 | BTRFS_I(new_inode)->dir_index = 0ULL; |
| 9133 | |
| 9134 | /* Reference for the source. */ |
| 9135 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) { |
| 9136 | /* force full log commit if subvolume involved. */ |
David Sterba | 9078776 | 2019-03-20 13:28:05 +0100 | [diff] [blame] | 9137 | btrfs_set_log_full_commit(trans); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9138 | } else { |
Filipe Manana | 376e5a5 | 2016-05-05 02:08:56 +0100 | [diff] [blame] | 9139 | btrfs_pin_log_trans(root); |
| 9140 | root_log_pinned = true; |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9141 | ret = btrfs_insert_inode_ref(trans, dest, |
| 9142 | new_dentry->d_name.name, |
| 9143 | new_dentry->d_name.len, |
| 9144 | old_ino, |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 9145 | btrfs_ino(BTRFS_I(new_dir)), |
| 9146 | old_idx); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9147 | if (ret) |
| 9148 | goto out_fail; |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9149 | } |
| 9150 | |
| 9151 | /* And now for the dest. */ |
| 9152 | if (new_ino == BTRFS_FIRST_FREE_OBJECTID) { |
| 9153 | /* force full log commit if subvolume involved. */ |
David Sterba | 9078776 | 2019-03-20 13:28:05 +0100 | [diff] [blame] | 9154 | btrfs_set_log_full_commit(trans); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9155 | } else { |
Filipe Manana | 376e5a5 | 2016-05-05 02:08:56 +0100 | [diff] [blame] | 9156 | btrfs_pin_log_trans(dest); |
| 9157 | dest_log_pinned = true; |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9158 | ret = btrfs_insert_inode_ref(trans, root, |
| 9159 | old_dentry->d_name.name, |
| 9160 | old_dentry->d_name.len, |
| 9161 | new_ino, |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 9162 | btrfs_ino(BTRFS_I(old_dir)), |
| 9163 | new_idx); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9164 | if (ret) |
| 9165 | goto out_fail; |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9166 | } |
| 9167 | |
| 9168 | /* Update inode version and ctime/mtime. */ |
| 9169 | inode_inc_iversion(old_dir); |
| 9170 | inode_inc_iversion(new_dir); |
| 9171 | inode_inc_iversion(old_inode); |
| 9172 | inode_inc_iversion(new_inode); |
| 9173 | old_dir->i_ctime = old_dir->i_mtime = ctime; |
| 9174 | new_dir->i_ctime = new_dir->i_mtime = ctime; |
| 9175 | old_inode->i_ctime = ctime; |
| 9176 | new_inode->i_ctime = ctime; |
| 9177 | |
| 9178 | if (old_dentry->d_parent != new_dentry->d_parent) { |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 9179 | btrfs_record_unlink_dir(trans, BTRFS_I(old_dir), |
| 9180 | BTRFS_I(old_inode), 1); |
| 9181 | btrfs_record_unlink_dir(trans, BTRFS_I(new_dir), |
| 9182 | BTRFS_I(new_inode), 1); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9183 | } |
| 9184 | |
| 9185 | /* src is a subvolume */ |
| 9186 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) { |
Josef Bacik | 045d396 | 2019-12-18 17:20:27 -0500 | [diff] [blame] | 9187 | ret = btrfs_unlink_subvol(trans, old_dir, old_dentry); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9188 | } else { /* src is an inode */ |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 9189 | ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir), |
| 9190 | BTRFS_I(old_dentry->d_inode), |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9191 | old_dentry->d_name.name, |
| 9192 | old_dentry->d_name.len); |
| 9193 | if (!ret) |
Nikolay Borisov | 9a56fcd | 2020-11-02 16:48:59 +0200 | [diff] [blame] | 9194 | ret = btrfs_update_inode(trans, root, BTRFS_I(old_inode)); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9195 | } |
| 9196 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 9197 | btrfs_abort_transaction(trans, ret); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9198 | goto out_fail; |
| 9199 | } |
| 9200 | |
| 9201 | /* dest is a subvolume */ |
| 9202 | if (new_ino == BTRFS_FIRST_FREE_OBJECTID) { |
Josef Bacik | 045d396 | 2019-12-18 17:20:27 -0500 | [diff] [blame] | 9203 | ret = btrfs_unlink_subvol(trans, new_dir, new_dentry); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9204 | } else { /* dest is an inode */ |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 9205 | ret = __btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir), |
| 9206 | BTRFS_I(new_dentry->d_inode), |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9207 | new_dentry->d_name.name, |
| 9208 | new_dentry->d_name.len); |
| 9209 | if (!ret) |
Nikolay Borisov | 9a56fcd | 2020-11-02 16:48:59 +0200 | [diff] [blame] | 9210 | ret = btrfs_update_inode(trans, dest, BTRFS_I(new_inode)); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9211 | } |
| 9212 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 9213 | btrfs_abort_transaction(trans, ret); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9214 | goto out_fail; |
| 9215 | } |
| 9216 | |
Nikolay Borisov | db0a669 | 2017-02-20 13:51:08 +0200 | [diff] [blame] | 9217 | 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] | 9218 | new_dentry->d_name.name, |
| 9219 | new_dentry->d_name.len, 0, old_idx); |
| 9220 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 9221 | btrfs_abort_transaction(trans, ret); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9222 | goto out_fail; |
| 9223 | } |
| 9224 | |
Nikolay Borisov | db0a669 | 2017-02-20 13:51:08 +0200 | [diff] [blame] | 9225 | 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] | 9226 | old_dentry->d_name.name, |
| 9227 | old_dentry->d_name.len, 0, new_idx); |
| 9228 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 9229 | btrfs_abort_transaction(trans, ret); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9230 | goto out_fail; |
| 9231 | } |
| 9232 | |
| 9233 | if (old_inode->i_nlink == 1) |
| 9234 | BTRFS_I(old_inode)->dir_index = old_idx; |
| 9235 | if (new_inode->i_nlink == 1) |
| 9236 | BTRFS_I(new_inode)->dir_index = new_idx; |
| 9237 | |
Filipe Manana | 86e8aa0 | 2016-05-05 02:02:27 +0100 | [diff] [blame] | 9238 | if (root_log_pinned) { |
Filipe Manana | 75b463d | 2020-08-11 12:43:48 +0100 | [diff] [blame] | 9239 | btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir), |
| 9240 | new_dentry->d_parent); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9241 | btrfs_end_log_trans(root); |
Filipe Manana | 86e8aa0 | 2016-05-05 02:02:27 +0100 | [diff] [blame] | 9242 | root_log_pinned = false; |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9243 | } |
Filipe Manana | 86e8aa0 | 2016-05-05 02:02:27 +0100 | [diff] [blame] | 9244 | if (dest_log_pinned) { |
Filipe Manana | 75b463d | 2020-08-11 12:43:48 +0100 | [diff] [blame] | 9245 | btrfs_log_new_name(trans, BTRFS_I(new_inode), BTRFS_I(new_dir), |
| 9246 | old_dentry->d_parent); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9247 | btrfs_end_log_trans(dest); |
Filipe Manana | 86e8aa0 | 2016-05-05 02:02:27 +0100 | [diff] [blame] | 9248 | dest_log_pinned = false; |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9249 | } |
| 9250 | out_fail: |
Filipe Manana | 86e8aa0 | 2016-05-05 02:02:27 +0100 | [diff] [blame] | 9251 | /* |
| 9252 | * If we have pinned a log and an error happened, we unpin tasks |
| 9253 | * trying to sync the log and force them to fallback to a transaction |
| 9254 | * commit if the log currently contains any of the inodes involved in |
| 9255 | * this rename operation (to ensure we do not persist a log with an |
| 9256 | * inconsistent state for any of these inodes or leading to any |
| 9257 | * inconsistencies when replayed). If the transaction was aborted, the |
| 9258 | * abortion reason is propagated to userspace when attempting to commit |
| 9259 | * the transaction. If the log does not contain any of these inodes, we |
| 9260 | * allow the tasks to sync it. |
| 9261 | */ |
| 9262 | if (ret && (root_log_pinned || dest_log_pinned)) { |
Nikolay Borisov | 0f8939b | 2017-01-18 00:31:30 +0200 | [diff] [blame] | 9263 | if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) || |
| 9264 | btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) || |
| 9265 | btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) || |
Filipe Manana | 86e8aa0 | 2016-05-05 02:02:27 +0100 | [diff] [blame] | 9266 | (new_inode && |
Nikolay Borisov | 0f8939b | 2017-01-18 00:31:30 +0200 | [diff] [blame] | 9267 | btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation))) |
David Sterba | 9078776 | 2019-03-20 13:28:05 +0100 | [diff] [blame] | 9268 | btrfs_set_log_full_commit(trans); |
Filipe Manana | 86e8aa0 | 2016-05-05 02:02:27 +0100 | [diff] [blame] | 9269 | |
| 9270 | if (root_log_pinned) { |
| 9271 | btrfs_end_log_trans(root); |
| 9272 | root_log_pinned = false; |
| 9273 | } |
| 9274 | if (dest_log_pinned) { |
| 9275 | btrfs_end_log_trans(dest); |
| 9276 | dest_log_pinned = false; |
| 9277 | } |
| 9278 | } |
Filipe Manana | 75b463d | 2020-08-11 12:43:48 +0100 | [diff] [blame] | 9279 | ret2 = btrfs_end_transaction(trans); |
| 9280 | ret = ret ? ret : ret2; |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9281 | out_notrans: |
Josef Bacik | 943eb3b | 2019-11-19 13:59:20 -0500 | [diff] [blame] | 9282 | if (new_ino == BTRFS_FIRST_FREE_OBJECTID || |
| 9283 | old_ino == BTRFS_FIRST_FREE_OBJECTID) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9284 | up_read(&fs_info->subvol_sem); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9285 | |
| 9286 | return ret; |
| 9287 | } |
| 9288 | |
| 9289 | static int btrfs_whiteout_for_rename(struct btrfs_trans_handle *trans, |
| 9290 | struct btrfs_root *root, |
| 9291 | struct inode *dir, |
| 9292 | struct dentry *dentry) |
| 9293 | { |
| 9294 | int ret; |
| 9295 | struct inode *inode; |
| 9296 | u64 objectid; |
| 9297 | u64 index; |
| 9298 | |
Nikolay Borisov | 543068a | 2020-12-07 17:32:33 +0200 | [diff] [blame] | 9299 | ret = btrfs_get_free_objectid(root, &objectid); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9300 | if (ret) |
| 9301 | return ret; |
| 9302 | |
| 9303 | inode = btrfs_new_inode(trans, root, dir, |
| 9304 | dentry->d_name.name, |
| 9305 | dentry->d_name.len, |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 9306 | btrfs_ino(BTRFS_I(dir)), |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9307 | objectid, |
| 9308 | S_IFCHR | WHITEOUT_MODE, |
| 9309 | &index); |
| 9310 | |
| 9311 | if (IS_ERR(inode)) { |
| 9312 | ret = PTR_ERR(inode); |
| 9313 | return ret; |
| 9314 | } |
| 9315 | |
| 9316 | inode->i_op = &btrfs_special_inode_operations; |
| 9317 | init_special_inode(inode, inode->i_mode, |
| 9318 | WHITEOUT_DEV); |
| 9319 | |
| 9320 | ret = btrfs_init_inode_security(trans, inode, dir, |
| 9321 | &dentry->d_name); |
| 9322 | if (ret) |
Filipe Manana | c990161 | 2016-05-05 01:41:57 +0100 | [diff] [blame] | 9323 | goto out; |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9324 | |
Nikolay Borisov | cef415a | 2017-02-20 13:51:09 +0200 | [diff] [blame] | 9325 | ret = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, |
| 9326 | BTRFS_I(inode), 0, index); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9327 | if (ret) |
Filipe Manana | c990161 | 2016-05-05 01:41:57 +0100 | [diff] [blame] | 9328 | goto out; |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9329 | |
Nikolay Borisov | 9a56fcd | 2020-11-02 16:48:59 +0200 | [diff] [blame] | 9330 | ret = btrfs_update_inode(trans, root, BTRFS_I(inode)); |
Filipe Manana | c990161 | 2016-05-05 01:41:57 +0100 | [diff] [blame] | 9331 | out: |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9332 | unlock_new_inode(inode); |
Filipe Manana | c990161 | 2016-05-05 01:41:57 +0100 | [diff] [blame] | 9333 | if (ret) |
| 9334 | inode_dec_link_count(inode); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9335 | iput(inode); |
| 9336 | |
Filipe Manana | c990161 | 2016-05-05 01:41:57 +0100 | [diff] [blame] | 9337 | return ret; |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9338 | } |
| 9339 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9340 | static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry, |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9341 | struct inode *new_dir, struct dentry *new_dentry, |
| 9342 | unsigned int flags) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9343 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9344 | struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9345 | struct btrfs_trans_handle *trans; |
Filipe Manana | 5062af3 | 2016-05-05 10:26:26 +0100 | [diff] [blame] | 9346 | unsigned int trans_num_items; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9347 | struct btrfs_root *root = BTRFS_I(old_dir)->root; |
| 9348 | struct btrfs_root *dest = BTRFS_I(new_dir)->root; |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 9349 | struct inode *new_inode = d_inode(new_dentry); |
| 9350 | struct inode *old_inode = d_inode(old_dentry); |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 9351 | u64 index = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9352 | int ret; |
Filipe Manana | 75b463d | 2020-08-11 12:43:48 +0100 | [diff] [blame] | 9353 | int ret2; |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 9354 | u64 old_ino = btrfs_ino(BTRFS_I(old_inode)); |
Filipe Manana | 3dc9e8f7 | 2016-04-29 11:34:22 +0100 | [diff] [blame] | 9355 | bool log_pinned = false; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9356 | |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 9357 | if (btrfs_ino(BTRFS_I(new_dir)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) |
Yan, Zheng | f679a84 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 9358 | return -EPERM; |
| 9359 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9360 | /* we only allow rename subvolume link between subvolumes */ |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 9361 | if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest) |
Chris Mason | 3394e16 | 2008-11-17 20:42:26 -0500 | [diff] [blame] | 9362 | return -EXDEV; |
| 9363 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 9364 | if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID || |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 9365 | (new_inode && btrfs_ino(BTRFS_I(new_inode)) == BTRFS_FIRST_FREE_OBJECTID)) |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9366 | return -ENOTEMPTY; |
| 9367 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9368 | if (S_ISDIR(old_inode->i_mode) && new_inode && |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9369 | new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9370 | return -ENOTEMPTY; |
Chris Mason | 9c52057 | 2012-12-17 14:26:57 -0500 | [diff] [blame] | 9371 | |
| 9372 | |
| 9373 | /* check for collisions, even if the name isn't there */ |
Josef Bacik | 4871c15 | 2013-10-09 12:24:04 -0400 | [diff] [blame] | 9374 | ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino, |
Chris Mason | 9c52057 | 2012-12-17 14:26:57 -0500 | [diff] [blame] | 9375 | new_dentry->d_name.name, |
| 9376 | new_dentry->d_name.len); |
| 9377 | |
| 9378 | if (ret) { |
| 9379 | if (ret == -EEXIST) { |
| 9380 | /* we shouldn't get |
| 9381 | * eexist without a new_inode */ |
Dulshani Gunawardhana | fae7f21 | 2013-10-31 10:30:08 +0530 | [diff] [blame] | 9382 | if (WARN_ON(!new_inode)) { |
Chris Mason | 9c52057 | 2012-12-17 14:26:57 -0500 | [diff] [blame] | 9383 | return ret; |
| 9384 | } |
| 9385 | } else { |
| 9386 | /* maybe -EOVERFLOW */ |
| 9387 | return ret; |
| 9388 | } |
| 9389 | } |
| 9390 | ret = 0; |
| 9391 | |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 9392 | /* |
Chris Mason | 8d875f9 | 2014-08-12 10:47:42 -0700 | [diff] [blame] | 9393 | * we're using rename to replace one file with another. Start IO on it |
| 9394 | * 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] | 9395 | */ |
Chris Mason | 8d875f9 | 2014-08-12 10:47:42 -0700 | [diff] [blame] | 9396 | 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] | 9397 | filemap_flush(old_inode->i_mapping); |
| 9398 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 9399 | /* close the racy window with snapshot create/destroy ioctl */ |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 9400 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9401 | down_read(&fs_info->subvol_sem); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 9402 | /* |
| 9403 | * We want to reserve the absolute worst case amount of items. So if |
| 9404 | * both inodes are subvols and we need to unlink them then that would |
| 9405 | * require 4 item modifications, but if they are both normal inodes it |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9406 | * would require 5 item modifications, so we'll assume they are normal |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 9407 | * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items |
| 9408 | * should cover the worst case number of items we'll modify. |
Filipe Manana | 5062af3 | 2016-05-05 10:26:26 +0100 | [diff] [blame] | 9409 | * If our rename has the whiteout flag, we need more 5 units for the |
| 9410 | * new inode (1 inode item, 1 inode ref, 2 dir items and 1 xattr item |
| 9411 | * when selinux is enabled). |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 9412 | */ |
Filipe Manana | 5062af3 | 2016-05-05 10:26:26 +0100 | [diff] [blame] | 9413 | trans_num_items = 11; |
| 9414 | if (flags & RENAME_WHITEOUT) |
| 9415 | trans_num_items += 5; |
| 9416 | trans = btrfs_start_transaction(root, trans_num_items); |
Johann Lombardi | b44c59a | 2011-03-31 13:23:47 +0000 | [diff] [blame] | 9417 | if (IS_ERR(trans)) { |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9418 | ret = PTR_ERR(trans); |
| 9419 | goto out_notrans; |
| 9420 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 9421 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9422 | if (dest != root) |
| 9423 | btrfs_record_root_in_trans(trans, dest); |
| 9424 | |
Nikolay Borisov | 877574e | 2017-02-20 13:50:33 +0200 | [diff] [blame] | 9425 | ret = btrfs_set_inode_index(BTRFS_I(new_dir), &index); |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 9426 | if (ret) |
| 9427 | goto out_fail; |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 9428 | |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 9429 | BTRFS_I(old_inode)->dir_index = 0ULL; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 9430 | if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9431 | /* force full log commit if subvolume involved. */ |
David Sterba | 9078776 | 2019-03-20 13:28:05 +0100 | [diff] [blame] | 9432 | btrfs_set_log_full_commit(trans); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9433 | } else { |
Filipe Manana | c4aba95 | 2016-04-29 13:14:42 +0100 | [diff] [blame] | 9434 | btrfs_pin_log_trans(root); |
| 9435 | log_pinned = true; |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 9436 | ret = btrfs_insert_inode_ref(trans, dest, |
| 9437 | new_dentry->d_name.name, |
| 9438 | new_dentry->d_name.len, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 9439 | old_ino, |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 9440 | btrfs_ino(BTRFS_I(new_dir)), index); |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 9441 | if (ret) |
| 9442 | goto out_fail; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9443 | } |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 9444 | |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 9445 | inode_inc_iversion(old_dir); |
| 9446 | inode_inc_iversion(new_dir); |
| 9447 | inode_inc_iversion(old_inode); |
Deepa Dinamani | 04b285f | 2016-02-06 23:57:21 -0800 | [diff] [blame] | 9448 | old_dir->i_ctime = old_dir->i_mtime = |
| 9449 | new_dir->i_ctime = new_dir->i_mtime = |
Deepa Dinamani | c2050a4 | 2016-09-14 07:48:06 -0700 | [diff] [blame] | 9450 | old_inode->i_ctime = current_time(old_dir); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 9451 | |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 9452 | if (old_dentry->d_parent != new_dentry->d_parent) |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 9453 | btrfs_record_unlink_dir(trans, BTRFS_I(old_dir), |
| 9454 | BTRFS_I(old_inode), 1); |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 9455 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 9456 | if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) { |
Josef Bacik | 045d396 | 2019-12-18 17:20:27 -0500 | [diff] [blame] | 9457 | ret = btrfs_unlink_subvol(trans, old_dir, old_dentry); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9458 | } else { |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 9459 | ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir), |
| 9460 | BTRFS_I(d_inode(old_dentry)), |
Al Viro | 9298679 | 2011-03-04 17:14:37 +0000 | [diff] [blame] | 9461 | old_dentry->d_name.name, |
| 9462 | old_dentry->d_name.len); |
| 9463 | if (!ret) |
Nikolay Borisov | 9a56fcd | 2020-11-02 16:48:59 +0200 | [diff] [blame] | 9464 | ret = btrfs_update_inode(trans, root, BTRFS_I(old_inode)); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9465 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 9466 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 9467 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 9468 | goto out_fail; |
| 9469 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9470 | |
| 9471 | if (new_inode) { |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 9472 | inode_inc_iversion(new_inode); |
Deepa Dinamani | c2050a4 | 2016-09-14 07:48:06 -0700 | [diff] [blame] | 9473 | new_inode->i_ctime = current_time(new_inode); |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 9474 | if (unlikely(btrfs_ino(BTRFS_I(new_inode)) == |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9475 | BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) { |
Josef Bacik | 045d396 | 2019-12-18 17:20:27 -0500 | [diff] [blame] | 9476 | ret = btrfs_unlink_subvol(trans, new_dir, new_dentry); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9477 | BUG_ON(new_inode->i_nlink == 0); |
| 9478 | } else { |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 9479 | ret = btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir), |
| 9480 | BTRFS_I(d_inode(new_dentry)), |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9481 | new_dentry->d_name.name, |
| 9482 | new_dentry->d_name.len); |
| 9483 | } |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 9484 | if (!ret && new_inode->i_nlink == 0) |
Nikolay Borisov | 73f2e54 | 2017-02-20 13:50:59 +0200 | [diff] [blame] | 9485 | ret = btrfs_orphan_add(trans, |
| 9486 | BTRFS_I(d_inode(new_dentry))); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 9487 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 9488 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 9489 | goto out_fail; |
| 9490 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9491 | } |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 9492 | |
Nikolay Borisov | db0a669 | 2017-02-20 13:51:08 +0200 | [diff] [blame] | 9493 | 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] | 9494 | new_dentry->d_name.name, |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 9495 | new_dentry->d_name.len, 0, index); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 9496 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 9497 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 9498 | goto out_fail; |
| 9499 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9500 | |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 9501 | if (old_inode->i_nlink == 1) |
| 9502 | BTRFS_I(old_inode)->dir_index = index; |
| 9503 | |
Filipe Manana | 3dc9e8f7 | 2016-04-29 11:34:22 +0100 | [diff] [blame] | 9504 | if (log_pinned) { |
Filipe Manana | 75b463d | 2020-08-11 12:43:48 +0100 | [diff] [blame] | 9505 | btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir), |
| 9506 | new_dentry->d_parent); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9507 | btrfs_end_log_trans(root); |
Filipe Manana | 3dc9e8f7 | 2016-04-29 11:34:22 +0100 | [diff] [blame] | 9508 | log_pinned = false; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9509 | } |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9510 | |
| 9511 | if (flags & RENAME_WHITEOUT) { |
| 9512 | ret = btrfs_whiteout_for_rename(trans, root, old_dir, |
| 9513 | old_dentry); |
| 9514 | |
| 9515 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 9516 | btrfs_abort_transaction(trans, ret); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9517 | goto out_fail; |
| 9518 | } |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 9519 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9520 | out_fail: |
Filipe Manana | 3dc9e8f7 | 2016-04-29 11:34:22 +0100 | [diff] [blame] | 9521 | /* |
| 9522 | * If we have pinned the log and an error happened, we unpin tasks |
| 9523 | * trying to sync the log and force them to fallback to a transaction |
| 9524 | * commit if the log currently contains any of the inodes involved in |
| 9525 | * this rename operation (to ensure we do not persist a log with an |
| 9526 | * inconsistent state for any of these inodes or leading to any |
| 9527 | * inconsistencies when replayed). If the transaction was aborted, the |
| 9528 | * abortion reason is propagated to userspace when attempting to commit |
| 9529 | * the transaction. If the log does not contain any of these inodes, we |
| 9530 | * allow the tasks to sync it. |
| 9531 | */ |
| 9532 | if (ret && log_pinned) { |
Nikolay Borisov | 0f8939b | 2017-01-18 00:31:30 +0200 | [diff] [blame] | 9533 | if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) || |
| 9534 | btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) || |
| 9535 | btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) || |
Filipe Manana | 3dc9e8f7 | 2016-04-29 11:34:22 +0100 | [diff] [blame] | 9536 | (new_inode && |
Nikolay Borisov | 0f8939b | 2017-01-18 00:31:30 +0200 | [diff] [blame] | 9537 | btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation))) |
David Sterba | 9078776 | 2019-03-20 13:28:05 +0100 | [diff] [blame] | 9538 | btrfs_set_log_full_commit(trans); |
Filipe Manana | 3dc9e8f7 | 2016-04-29 11:34:22 +0100 | [diff] [blame] | 9539 | |
| 9540 | btrfs_end_log_trans(root); |
| 9541 | log_pinned = false; |
| 9542 | } |
Filipe Manana | 75b463d | 2020-08-11 12:43:48 +0100 | [diff] [blame] | 9543 | ret2 = btrfs_end_transaction(trans); |
| 9544 | ret = ret ? ret : ret2; |
Johann Lombardi | b44c59a | 2011-03-31 13:23:47 +0000 | [diff] [blame] | 9545 | out_notrans: |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 9546 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9547 | up_read(&fs_info->subvol_sem); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 9548 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9549 | return ret; |
| 9550 | } |
| 9551 | |
Christian Brauner | 549c729 | 2021-01-21 14:19:43 +0100 | [diff] [blame] | 9552 | static int btrfs_rename2(struct user_namespace *mnt_userns, struct inode *old_dir, |
| 9553 | struct dentry *old_dentry, struct inode *new_dir, |
| 9554 | struct dentry *new_dentry, unsigned int flags) |
Miklos Szeredi | 80ace85 | 2014-07-23 15:15:32 +0200 | [diff] [blame] | 9555 | { |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9556 | if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT)) |
Miklos Szeredi | 80ace85 | 2014-07-23 15:15:32 +0200 | [diff] [blame] | 9557 | return -EINVAL; |
| 9558 | |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9559 | if (flags & RENAME_EXCHANGE) |
| 9560 | return btrfs_rename_exchange(old_dir, old_dentry, new_dir, |
| 9561 | new_dentry); |
| 9562 | |
| 9563 | return btrfs_rename(old_dir, old_dentry, new_dir, new_dentry, flags); |
Miklos Szeredi | 80ace85 | 2014-07-23 15:15:32 +0200 | [diff] [blame] | 9564 | } |
| 9565 | |
Nikolay Borisov | 3a2f8c0 | 2018-04-24 17:23:59 +0300 | [diff] [blame] | 9566 | struct btrfs_delalloc_work { |
| 9567 | struct inode *inode; |
| 9568 | struct completion completion; |
| 9569 | struct list_head list; |
| 9570 | struct btrfs_work work; |
| 9571 | }; |
| 9572 | |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 9573 | static void btrfs_run_delalloc_work(struct btrfs_work *work) |
| 9574 | { |
| 9575 | struct btrfs_delalloc_work *delalloc_work; |
Josef Bacik | 9f23e28 | 2013-10-28 15:03:41 -0400 | [diff] [blame] | 9576 | struct inode *inode; |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 9577 | |
| 9578 | delalloc_work = container_of(work, struct btrfs_delalloc_work, |
| 9579 | work); |
Josef Bacik | 9f23e28 | 2013-10-28 15:03:41 -0400 | [diff] [blame] | 9580 | inode = delalloc_work->inode; |
David Sterba | 3042460 | 2015-11-27 19:27:11 +0100 | [diff] [blame] | 9581 | filemap_flush(inode->i_mapping); |
| 9582 | if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, |
| 9583 | &BTRFS_I(inode)->runtime_flags)) |
Josef Bacik | 9f23e28 | 2013-10-28 15:03:41 -0400 | [diff] [blame] | 9584 | filemap_flush(inode->i_mapping); |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 9585 | |
Nikolay Borisov | 076da91 | 2018-04-23 10:54:16 +0300 | [diff] [blame] | 9586 | iput(inode); |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 9587 | complete(&delalloc_work->completion); |
| 9588 | } |
| 9589 | |
Nikolay Borisov | 3a2f8c0 | 2018-04-24 17:23:59 +0300 | [diff] [blame] | 9590 | static struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode) |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 9591 | { |
| 9592 | struct btrfs_delalloc_work *work; |
| 9593 | |
David Sterba | 100d570 | 2015-12-08 14:39:32 +0100 | [diff] [blame] | 9594 | work = kmalloc(sizeof(*work), GFP_NOFS); |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 9595 | if (!work) |
| 9596 | return NULL; |
| 9597 | |
| 9598 | init_completion(&work->completion); |
| 9599 | INIT_LIST_HEAD(&work->list); |
| 9600 | work->inode = inode; |
Omar Sandoval | a0cac0e | 2019-09-16 11:30:57 -0700 | [diff] [blame] | 9601 | btrfs_init_work(&work->work, btrfs_run_delalloc_work, NULL, NULL); |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 9602 | |
| 9603 | return work; |
| 9604 | } |
| 9605 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 9606 | /* |
| 9607 | * some fairly slow code that needs optimization. This walks the list |
| 9608 | * of all the inodes with pending delalloc and forces them to disk. |
| 9609 | */ |
Josef Bacik | e076ab2a | 2021-01-07 17:08:30 -0500 | [diff] [blame] | 9610 | static int start_delalloc_inodes(struct btrfs_root *root, |
| 9611 | struct writeback_control *wbc, bool snapshot, |
Filipe Manana | 3d45f22 | 2020-12-02 11:55:58 +0000 | [diff] [blame] | 9612 | bool in_reclaim_context) |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 9613 | { |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 9614 | struct btrfs_inode *binode; |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 9615 | struct inode *inode; |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 9616 | struct btrfs_delalloc_work *work, *next; |
| 9617 | struct list_head works; |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 9618 | struct list_head splice; |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 9619 | int ret = 0; |
Josef Bacik | e076ab2a | 2021-01-07 17:08:30 -0500 | [diff] [blame] | 9620 | bool full_flush = wbc->nr_to_write == LONG_MAX; |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 9621 | |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 9622 | INIT_LIST_HEAD(&works); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 9623 | INIT_LIST_HEAD(&splice); |
Miao Xie | 63607cc | 2013-01-22 10:50:35 +0000 | [diff] [blame] | 9624 | |
Miao Xie | 573bfb7 | 2014-03-06 13:55:03 +0800 | [diff] [blame] | 9625 | mutex_lock(&root->delalloc_mutex); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9626 | spin_lock(&root->delalloc_lock); |
| 9627 | list_splice_init(&root->delalloc_inodes, &splice); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 9628 | while (!list_empty(&splice)) { |
| 9629 | binode = list_entry(splice.next, struct btrfs_inode, |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 9630 | delalloc_inodes); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 9631 | |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9632 | list_move_tail(&binode->delalloc_inodes, |
| 9633 | &root->delalloc_inodes); |
Filipe Manana | 3d45f22 | 2020-12-02 11:55:58 +0000 | [diff] [blame] | 9634 | |
| 9635 | if (in_reclaim_context && |
| 9636 | test_bit(BTRFS_INODE_NO_DELALLOC_FLUSH, &binode->runtime_flags)) |
| 9637 | continue; |
| 9638 | |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 9639 | inode = igrab(&binode->vfs_inode); |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 9640 | if (!inode) { |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9641 | cond_resched_lock(&root->delalloc_lock); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 9642 | continue; |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 9643 | } |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9644 | spin_unlock(&root->delalloc_lock); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 9645 | |
Ethan Lien | 3cd24c6 | 2018-11-01 14:49:03 +0800 | [diff] [blame] | 9646 | if (snapshot) |
| 9647 | set_bit(BTRFS_INODE_SNAPSHOT_FLUSH, |
| 9648 | &binode->runtime_flags); |
Josef Bacik | e076ab2a | 2021-01-07 17:08:30 -0500 | [diff] [blame] | 9649 | if (full_flush) { |
| 9650 | work = btrfs_alloc_delalloc_work(inode); |
| 9651 | if (!work) { |
| 9652 | iput(inode); |
| 9653 | ret = -ENOMEM; |
| 9654 | goto out; |
| 9655 | } |
| 9656 | list_add_tail(&work->list, &works); |
| 9657 | btrfs_queue_work(root->fs_info->flush_workers, |
| 9658 | &work->work); |
| 9659 | } else { |
| 9660 | ret = sync_inode(inode, wbc); |
| 9661 | if (!ret && |
| 9662 | test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, |
| 9663 | &BTRFS_I(inode)->runtime_flags)) |
| 9664 | ret = sync_inode(inode, wbc); |
| 9665 | btrfs_add_delayed_iput(inode); |
| 9666 | if (ret || wbc->nr_to_write <= 0) |
Josef Bacik | b491213 | 2020-07-21 10:22:12 -0400 | [diff] [blame] | 9667 | goto out; |
| 9668 | } |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 9669 | cond_resched(); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9670 | spin_lock(&root->delalloc_lock); |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 9671 | } |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9672 | spin_unlock(&root->delalloc_lock); |
Chris Mason | 8c8bee1 | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 9673 | |
Wang Shilong | a1ecaab | 2014-04-02 19:53:32 +0800 | [diff] [blame] | 9674 | out: |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 9675 | list_for_each_entry_safe(work, next, &works, list) { |
| 9676 | list_del_init(&work->list); |
Nikolay Borisov | 40012f9 | 2018-04-19 10:46:39 +0300 | [diff] [blame] | 9677 | wait_for_completion(&work->completion); |
| 9678 | kfree(work); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 9679 | } |
| 9680 | |
Nikolay Borisov | 81f1d39 | 2018-04-19 10:46:37 +0300 | [diff] [blame] | 9681 | if (!list_empty(&splice)) { |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9682 | spin_lock(&root->delalloc_lock); |
| 9683 | list_splice_tail(&splice, &root->delalloc_inodes); |
| 9684 | spin_unlock(&root->delalloc_lock); |
| 9685 | } |
Miao Xie | 573bfb7 | 2014-03-06 13:55:03 +0800 | [diff] [blame] | 9686 | mutex_unlock(&root->delalloc_mutex); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9687 | return ret; |
| 9688 | } |
| 9689 | |
Ethan Lien | 3cd24c6 | 2018-11-01 14:49:03 +0800 | [diff] [blame] | 9690 | int btrfs_start_delalloc_snapshot(struct btrfs_root *root) |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9691 | { |
Josef Bacik | e076ab2a | 2021-01-07 17:08:30 -0500 | [diff] [blame] | 9692 | struct writeback_control wbc = { |
| 9693 | .nr_to_write = LONG_MAX, |
| 9694 | .sync_mode = WB_SYNC_NONE, |
| 9695 | .range_start = 0, |
| 9696 | .range_end = LLONG_MAX, |
| 9697 | }; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9698 | struct btrfs_fs_info *fs_info = root->fs_info; |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9699 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9700 | if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9701 | return -EROFS; |
| 9702 | |
Josef Bacik | e076ab2a | 2021-01-07 17:08:30 -0500 | [diff] [blame] | 9703 | return start_delalloc_inodes(root, &wbc, true, false); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9704 | } |
| 9705 | |
Nikolay Borisov | 9db4dc2 | 2021-01-11 12:58:11 +0200 | [diff] [blame] | 9706 | 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] | 9707 | bool in_reclaim_context) |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9708 | { |
Josef Bacik | e076ab2a | 2021-01-07 17:08:30 -0500 | [diff] [blame] | 9709 | struct writeback_control wbc = { |
Nikolay Borisov | 9db4dc2 | 2021-01-11 12:58:11 +0200 | [diff] [blame] | 9710 | .nr_to_write = nr, |
Josef Bacik | e076ab2a | 2021-01-07 17:08:30 -0500 | [diff] [blame] | 9711 | .sync_mode = WB_SYNC_NONE, |
| 9712 | .range_start = 0, |
| 9713 | .range_end = LLONG_MAX, |
| 9714 | }; |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9715 | struct btrfs_root *root; |
| 9716 | struct list_head splice; |
| 9717 | int ret; |
| 9718 | |
Wang Shilong | 2c21b4d | 2014-01-14 19:42:20 +0800 | [diff] [blame] | 9719 | if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9720 | return -EROFS; |
| 9721 | |
| 9722 | INIT_LIST_HEAD(&splice); |
| 9723 | |
Miao Xie | 573bfb7 | 2014-03-06 13:55:03 +0800 | [diff] [blame] | 9724 | mutex_lock(&fs_info->delalloc_root_mutex); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9725 | spin_lock(&fs_info->delalloc_root_lock); |
| 9726 | list_splice_init(&fs_info->delalloc_roots, &splice); |
Nikolay Borisov | d7830b7 | 2021-01-11 12:58:12 +0200 | [diff] [blame] | 9727 | while (!list_empty(&splice)) { |
Josef Bacik | e076ab2a | 2021-01-07 17:08:30 -0500 | [diff] [blame] | 9728 | /* |
| 9729 | * Reset nr_to_write here so we know that we're doing a full |
| 9730 | * flush. |
| 9731 | */ |
Nikolay Borisov | 9db4dc2 | 2021-01-11 12:58:11 +0200 | [diff] [blame] | 9732 | if (nr == LONG_MAX) |
Josef Bacik | e076ab2a | 2021-01-07 17:08:30 -0500 | [diff] [blame] | 9733 | wbc.nr_to_write = LONG_MAX; |
| 9734 | |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9735 | root = list_first_entry(&splice, struct btrfs_root, |
| 9736 | delalloc_root); |
Josef Bacik | 0024652 | 2020-01-24 09:33:01 -0500 | [diff] [blame] | 9737 | root = btrfs_grab_root(root); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9738 | BUG_ON(!root); |
| 9739 | list_move_tail(&root->delalloc_root, |
| 9740 | &fs_info->delalloc_roots); |
| 9741 | spin_unlock(&fs_info->delalloc_root_lock); |
| 9742 | |
Josef Bacik | e076ab2a | 2021-01-07 17:08:30 -0500 | [diff] [blame] | 9743 | ret = start_delalloc_inodes(root, &wbc, false, in_reclaim_context); |
Josef Bacik | 0024652 | 2020-01-24 09:33:01 -0500 | [diff] [blame] | 9744 | btrfs_put_root(root); |
Josef Bacik | e076ab2a | 2021-01-07 17:08:30 -0500 | [diff] [blame] | 9745 | if (ret < 0 || wbc.nr_to_write <= 0) |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9746 | goto out; |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9747 | spin_lock(&fs_info->delalloc_root_lock); |
| 9748 | } |
| 9749 | spin_unlock(&fs_info->delalloc_root_lock); |
| 9750 | |
Miao Xie | 6c255e6 | 2014-03-06 13:55:01 +0800 | [diff] [blame] | 9751 | ret = 0; |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 9752 | out: |
Nikolay Borisov | 81f1d39 | 2018-04-19 10:46:37 +0300 | [diff] [blame] | 9753 | if (!list_empty(&splice)) { |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9754 | spin_lock(&fs_info->delalloc_root_lock); |
| 9755 | list_splice_tail(&splice, &fs_info->delalloc_roots); |
| 9756 | spin_unlock(&fs_info->delalloc_root_lock); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 9757 | } |
Miao Xie | 573bfb7 | 2014-03-06 13:55:03 +0800 | [diff] [blame] | 9758 | mutex_unlock(&fs_info->delalloc_root_mutex); |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 9759 | return ret; |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 9760 | } |
| 9761 | |
Christian Brauner | 549c729 | 2021-01-21 14:19:43 +0100 | [diff] [blame] | 9762 | static int btrfs_symlink(struct user_namespace *mnt_userns, struct inode *dir, |
| 9763 | struct dentry *dentry, const char *symname) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9764 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9765 | struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9766 | struct btrfs_trans_handle *trans; |
| 9767 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 9768 | struct btrfs_path *path; |
| 9769 | struct btrfs_key key; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 9770 | struct inode *inode = NULL; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9771 | int err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9772 | u64 objectid; |
Dulshani Gunawardhana | 6787125 | 2013-10-31 10:33:04 +0530 | [diff] [blame] | 9773 | u64 index = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9774 | int name_len; |
| 9775 | int datasize; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 9776 | unsigned long ptr; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9777 | struct btrfs_file_extent_item *ei; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 9778 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9779 | |
Filipe David Borba Manana | f06becc | 2013-09-16 09:53:28 +0100 | [diff] [blame] | 9780 | name_len = strlen(symname); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9781 | if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info)) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9782 | return -ENAMETOOLONG; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 9783 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 9784 | /* |
| 9785 | * 2 items for inode item and ref |
| 9786 | * 2 items for dir items |
Filipe Manana | 9269d12 | 2015-12-31 18:16:29 +0000 | [diff] [blame] | 9787 | * 1 item for updating parent inode item |
| 9788 | * 1 item for the inline extent item |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 9789 | * 1 item for xattr if selinux is on |
| 9790 | */ |
Filipe Manana | 9269d12 | 2015-12-31 18:16:29 +0000 | [diff] [blame] | 9791 | trans = btrfs_start_transaction(root, 7); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 9792 | if (IS_ERR(trans)) |
| 9793 | return PTR_ERR(trans); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 9794 | |
Nikolay Borisov | 543068a | 2020-12-07 17:32:33 +0200 | [diff] [blame] | 9795 | err = btrfs_get_free_objectid(root, &objectid); |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 9796 | if (err) |
| 9797 | goto out_unlock; |
| 9798 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 9799 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 9800 | dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), |
| 9801 | objectid, S_IFLNK|S_IRWXUGO, &index); |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 9802 | if (IS_ERR(inode)) { |
| 9803 | err = PTR_ERR(inode); |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 9804 | inode = NULL; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9805 | goto out_unlock; |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 9806 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9807 | |
Casey Schaufler | ad19db7 | 2011-12-15 10:09:07 -0500 | [diff] [blame] | 9808 | /* |
| 9809 | * If the active LSM wants to access the inode during |
| 9810 | * d_instantiate it needs these. Smack checks to see |
| 9811 | * if the filesystem supports xattrs by looking at the |
| 9812 | * ops vector. |
| 9813 | */ |
| 9814 | inode->i_fop = &btrfs_file_operations; |
| 9815 | inode->i_op = &btrfs_file_inode_operations; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 9816 | inode->i_mapping->a_ops = &btrfs_aops; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 9817 | |
| 9818 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); |
| 9819 | if (err) |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 9820 | goto out_unlock; |
Casey Schaufler | ad19db7 | 2011-12-15 10:09:07 -0500 | [diff] [blame] | 9821 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9822 | path = btrfs_alloc_path(); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 9823 | if (!path) { |
| 9824 | err = -ENOMEM; |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 9825 | goto out_unlock; |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 9826 | } |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 9827 | key.objectid = btrfs_ino(BTRFS_I(inode)); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9828 | key.offset = 0; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 9829 | key.type = BTRFS_EXTENT_DATA_KEY; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9830 | datasize = btrfs_file_extent_calc_inline_size(name_len); |
| 9831 | err = btrfs_insert_empty_item(trans, root, path, &key, |
| 9832 | datasize); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 9833 | if (err) { |
Julia Lawall | b083916 | 2011-05-14 07:10:51 +0000 | [diff] [blame] | 9834 | btrfs_free_path(path); |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 9835 | goto out_unlock; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 9836 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 9837 | leaf = path->nodes[0]; |
| 9838 | ei = btrfs_item_ptr(leaf, path->slots[0], |
| 9839 | struct btrfs_file_extent_item); |
| 9840 | btrfs_set_file_extent_generation(leaf, ei, trans->transid); |
| 9841 | btrfs_set_file_extent_type(leaf, ei, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9842 | BTRFS_FILE_EXTENT_INLINE); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 9843 | btrfs_set_file_extent_encryption(leaf, ei, 0); |
| 9844 | btrfs_set_file_extent_compression(leaf, ei, 0); |
| 9845 | btrfs_set_file_extent_other_encoding(leaf, ei, 0); |
| 9846 | btrfs_set_file_extent_ram_bytes(leaf, ei, name_len); |
| 9847 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9848 | ptr = btrfs_file_extent_inline_start(ei); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 9849 | write_extent_buffer(leaf, symname, ptr, name_len); |
| 9850 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9851 | btrfs_free_path(path); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 9852 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9853 | inode->i_op = &btrfs_symlink_inode_operations; |
Al Viro | 21fc61c | 2015-11-17 01:07:57 -0500 | [diff] [blame] | 9854 | inode_nohighmem(inode); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 9855 | inode_set_bytes(inode, name_len); |
Nikolay Borisov | 6ef06d2 | 2017-02-20 13:50:34 +0200 | [diff] [blame] | 9856 | btrfs_i_size_write(BTRFS_I(inode), name_len); |
Nikolay Borisov | 9a56fcd | 2020-11-02 16:48:59 +0200 | [diff] [blame] | 9857 | err = btrfs_update_inode(trans, root, BTRFS_I(inode)); |
Filipe Manana | d50866d | 2015-12-31 18:08:24 +0000 | [diff] [blame] | 9858 | /* |
| 9859 | * Last step, add directory indexes for our symlink inode. This is the |
| 9860 | * last step to avoid extra cleanup of these indexes if an error happens |
| 9861 | * elsewhere above. |
| 9862 | */ |
| 9863 | if (!err) |
Nikolay Borisov | cef415a | 2017-02-20 13:51:09 +0200 | [diff] [blame] | 9864 | err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, |
| 9865 | BTRFS_I(inode), 0, index); |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 9866 | if (err) |
| 9867 | goto out_unlock; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 9868 | |
Al Viro | 1e2e547 | 2018-05-04 08:23:01 -0400 | [diff] [blame] | 9869 | d_instantiate_new(dentry, inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9870 | |
| 9871 | out_unlock: |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 9872 | btrfs_end_transaction(trans); |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 9873 | if (err && inode) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9874 | inode_dec_link_count(inode); |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 9875 | discard_new_inode(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9876 | } |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 9877 | btrfs_btree_balance_dirty(fs_info); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9878 | return err; |
| 9879 | } |
Chris Mason | 1643298 | 2008-04-10 10:23:21 -0400 | [diff] [blame] | 9880 | |
Filipe Manana | 8fccebf | 2020-09-08 11:27:20 +0100 | [diff] [blame] | 9881 | static struct btrfs_trans_handle *insert_prealloc_file_extent( |
| 9882 | struct btrfs_trans_handle *trans_in, |
Nikolay Borisov | 90dffd0 | 2020-11-02 16:48:54 +0200 | [diff] [blame] | 9883 | struct btrfs_inode *inode, |
| 9884 | struct btrfs_key *ins, |
Qu Wenruo | 203f44c5 | 2020-06-10 09:04:40 +0800 | [diff] [blame] | 9885 | u64 file_offset) |
| 9886 | { |
| 9887 | struct btrfs_file_extent_item stack_fi; |
Filipe Manana | bf38564 | 2020-09-08 11:27:22 +0100 | [diff] [blame] | 9888 | struct btrfs_replace_extent_info extent_info; |
Filipe Manana | 8fccebf | 2020-09-08 11:27:20 +0100 | [diff] [blame] | 9889 | struct btrfs_trans_handle *trans = trans_in; |
| 9890 | struct btrfs_path *path; |
Qu Wenruo | 203f44c5 | 2020-06-10 09:04:40 +0800 | [diff] [blame] | 9891 | u64 start = ins->objectid; |
| 9892 | u64 len = ins->offset; |
Qu Wenruo | fbf48bb | 2021-03-03 18:41:51 +0800 | [diff] [blame] | 9893 | int qgroup_released; |
Qu Wenruo | 9729f10 | 2020-06-10 09:04:41 +0800 | [diff] [blame] | 9894 | int ret; |
Qu Wenruo | 203f44c5 | 2020-06-10 09:04:40 +0800 | [diff] [blame] | 9895 | |
| 9896 | memset(&stack_fi, 0, sizeof(stack_fi)); |
| 9897 | |
| 9898 | btrfs_set_stack_file_extent_type(&stack_fi, BTRFS_FILE_EXTENT_PREALLOC); |
| 9899 | btrfs_set_stack_file_extent_disk_bytenr(&stack_fi, start); |
| 9900 | btrfs_set_stack_file_extent_disk_num_bytes(&stack_fi, len); |
| 9901 | btrfs_set_stack_file_extent_num_bytes(&stack_fi, len); |
| 9902 | btrfs_set_stack_file_extent_ram_bytes(&stack_fi, len); |
| 9903 | btrfs_set_stack_file_extent_compression(&stack_fi, BTRFS_COMPRESS_NONE); |
| 9904 | /* Encryption and other encoding is reserved and all 0 */ |
| 9905 | |
Qu Wenruo | fbf48bb | 2021-03-03 18:41:51 +0800 | [diff] [blame] | 9906 | qgroup_released = btrfs_qgroup_release_data(inode, file_offset, len); |
| 9907 | if (qgroup_released < 0) |
| 9908 | return ERR_PTR(qgroup_released); |
Filipe Manana | 8fccebf | 2020-09-08 11:27:20 +0100 | [diff] [blame] | 9909 | |
| 9910 | if (trans) { |
Nikolay Borisov | 90dffd0 | 2020-11-02 16:48:54 +0200 | [diff] [blame] | 9911 | ret = insert_reserved_file_extent(trans, inode, |
Filipe Manana | 2766ff6 | 2020-11-04 11:07:34 +0000 | [diff] [blame] | 9912 | file_offset, &stack_fi, |
Qu Wenruo | fbf48bb | 2021-03-03 18:41:51 +0800 | [diff] [blame] | 9913 | true, qgroup_released); |
Filipe Manana | 8fccebf | 2020-09-08 11:27:20 +0100 | [diff] [blame] | 9914 | if (ret) |
Qu Wenruo | a3ee79b | 2021-03-03 18:41:52 +0800 | [diff] [blame] | 9915 | goto free_qgroup; |
Filipe Manana | 8fccebf | 2020-09-08 11:27:20 +0100 | [diff] [blame] | 9916 | return trans; |
| 9917 | } |
| 9918 | |
| 9919 | extent_info.disk_offset = start; |
| 9920 | extent_info.disk_len = len; |
| 9921 | extent_info.data_offset = 0; |
| 9922 | extent_info.data_len = len; |
| 9923 | extent_info.file_offset = file_offset; |
| 9924 | extent_info.extent_buf = (char *)&stack_fi; |
Filipe Manana | 8fccebf | 2020-09-08 11:27:20 +0100 | [diff] [blame] | 9925 | extent_info.is_new_extent = true; |
Qu Wenruo | fbf48bb | 2021-03-03 18:41:51 +0800 | [diff] [blame] | 9926 | extent_info.qgroup_reserved = qgroup_released; |
Filipe Manana | 8fccebf | 2020-09-08 11:27:20 +0100 | [diff] [blame] | 9927 | extent_info.insertions = 0; |
| 9928 | |
| 9929 | path = btrfs_alloc_path(); |
Qu Wenruo | a3ee79b | 2021-03-03 18:41:52 +0800 | [diff] [blame] | 9930 | if (!path) { |
| 9931 | ret = -ENOMEM; |
| 9932 | goto free_qgroup; |
| 9933 | } |
Filipe Manana | 8fccebf | 2020-09-08 11:27:20 +0100 | [diff] [blame] | 9934 | |
Nikolay Borisov | bfc7847 | 2021-02-17 15:12:47 +0200 | [diff] [blame] | 9935 | ret = btrfs_replace_file_extents(inode, path, file_offset, |
Filipe Manana | 8fccebf | 2020-09-08 11:27:20 +0100 | [diff] [blame] | 9936 | file_offset + len - 1, &extent_info, |
| 9937 | &trans); |
| 9938 | btrfs_free_path(path); |
| 9939 | if (ret) |
Qu Wenruo | a3ee79b | 2021-03-03 18:41:52 +0800 | [diff] [blame] | 9940 | goto free_qgroup; |
Filipe Manana | 8fccebf | 2020-09-08 11:27:20 +0100 | [diff] [blame] | 9941 | return trans; |
Qu Wenruo | a3ee79b | 2021-03-03 18:41:52 +0800 | [diff] [blame] | 9942 | |
| 9943 | free_qgroup: |
| 9944 | /* |
| 9945 | * We have released qgroup data range at the beginning of the function, |
| 9946 | * and normally qgroup_released bytes will be freed when committing |
| 9947 | * transaction. |
| 9948 | * But if we error out early, we have to free what we have released |
| 9949 | * or we leak qgroup data reservation. |
| 9950 | */ |
| 9951 | btrfs_qgroup_free_refroot(inode->root->fs_info, |
| 9952 | inode->root->root_key.objectid, qgroup_released, |
| 9953 | BTRFS_QGROUP_RSV_DATA); |
| 9954 | return ERR_PTR(ret); |
Qu Wenruo | 203f44c5 | 2020-06-10 09:04:40 +0800 | [diff] [blame] | 9955 | } |
Filipe Manana | 8fccebf | 2020-09-08 11:27:20 +0100 | [diff] [blame] | 9956 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 9957 | static int __btrfs_prealloc_file_range(struct inode *inode, int mode, |
| 9958 | u64 start, u64 num_bytes, u64 min_size, |
| 9959 | loff_t actual_len, u64 *alloc_hint, |
| 9960 | struct btrfs_trans_handle *trans) |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 9961 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9962 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 9963 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
| 9964 | struct extent_map *em; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 9965 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 9966 | struct btrfs_key ins; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 9967 | u64 cur_offset = start; |
Josef Bacik | b778cf9 | 2020-02-13 10:47:31 -0500 | [diff] [blame] | 9968 | u64 clear_offset = start; |
Josef Bacik | 55a61d1 | 2010-11-22 18:50:32 +0000 | [diff] [blame] | 9969 | u64 i_size; |
Chris Mason | 154ea28 | 2013-03-05 11:11:26 -0500 | [diff] [blame] | 9970 | u64 cur_bytes; |
Josef Bacik | 0b670dc | 2015-09-23 17:11:16 -0400 | [diff] [blame] | 9971 | u64 last_alloc = (u64)-1; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 9972 | int ret = 0; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 9973 | bool own_trans = true; |
Wang Xiaoguang | 1851309 | 2016-07-25 15:51:40 +0800 | [diff] [blame] | 9974 | u64 end = start + num_bytes - 1; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 9975 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 9976 | if (trans) |
| 9977 | own_trans = false; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 9978 | while (num_bytes > 0) { |
Byongho Lee | ee22184 | 2015-12-15 01:42:10 +0900 | [diff] [blame] | 9979 | cur_bytes = min_t(u64, num_bytes, SZ_256M); |
Chris Mason | 154ea28 | 2013-03-05 11:11:26 -0500 | [diff] [blame] | 9980 | cur_bytes = max(cur_bytes, min_size); |
Josef Bacik | 0b670dc | 2015-09-23 17:11:16 -0400 | [diff] [blame] | 9981 | /* |
| 9982 | * If we are severely fragmented we could end up with really |
| 9983 | * small allocations, so if the allocator is returning small |
| 9984 | * chunks lets make its job easier by only searching for those |
| 9985 | * sized chunks. |
| 9986 | */ |
| 9987 | cur_bytes = min(cur_bytes, last_alloc); |
Wang Xiaoguang | 1851309 | 2016-07-25 15:51:40 +0800 | [diff] [blame] | 9988 | ret = btrfs_reserve_extent(root, cur_bytes, cur_bytes, |
| 9989 | min_size, 0, *alloc_hint, &ins, 1, 0); |
Filipe Manana | 8fccebf | 2020-09-08 11:27:20 +0100 | [diff] [blame] | 9990 | if (ret) |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 9991 | break; |
Josef Bacik | b778cf9 | 2020-02-13 10:47:31 -0500 | [diff] [blame] | 9992 | |
| 9993 | /* |
| 9994 | * We've reserved this space, and thus converted it from |
| 9995 | * ->bytes_may_use to ->bytes_reserved. Any error that happens |
| 9996 | * from here on out we will only need to clear our reservation |
| 9997 | * for the remaining unreserved area, so advance our |
| 9998 | * clear_offset by our extent size. |
| 9999 | */ |
| 10000 | clear_offset += ins.offset; |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 10001 | |
Josef Bacik | 0b670dc | 2015-09-23 17:11:16 -0400 | [diff] [blame] | 10002 | last_alloc = ins.offset; |
Nikolay Borisov | 90dffd0 | 2020-11-02 16:48:54 +0200 | [diff] [blame] | 10003 | trans = insert_prealloc_file_extent(trans, BTRFS_I(inode), |
| 10004 | &ins, cur_offset); |
Filipe Manana | 1afc708 | 2020-10-14 10:10:36 +0100 | [diff] [blame] | 10005 | /* |
| 10006 | * Now that we inserted the prealloc extent we can finally |
| 10007 | * decrement the number of reservations in the block group. |
| 10008 | * If we did it before, we could race with relocation and have |
| 10009 | * relocation miss the reserved extent, making it fail later. |
| 10010 | */ |
| 10011 | btrfs_dec_block_group_reservations(fs_info, ins.objectid); |
Filipe Manana | 8fccebf | 2020-09-08 11:27:20 +0100 | [diff] [blame] | 10012 | if (IS_ERR(trans)) { |
| 10013 | ret = PTR_ERR(trans); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 10014 | btrfs_free_reserved_extent(fs_info, ins.objectid, |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 10015 | ins.offset, 0); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 10016 | break; |
| 10017 | } |
Dongsheng Yang | 3119321 | 2014-12-12 16:44:35 +0800 | [diff] [blame] | 10018 | |
Nikolay Borisov | dcdbc05 | 2017-02-20 13:50:45 +0200 | [diff] [blame] | 10019 | btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset, |
Chris Mason | a1ed835 | 2009-09-11 12:27:37 -0400 | [diff] [blame] | 10020 | cur_offset + ins.offset -1, 0); |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 10021 | |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 10022 | em = alloc_extent_map(); |
| 10023 | if (!em) { |
| 10024 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, |
| 10025 | &BTRFS_I(inode)->runtime_flags); |
| 10026 | goto next; |
| 10027 | } |
| 10028 | |
| 10029 | em->start = cur_offset; |
| 10030 | em->orig_start = cur_offset; |
| 10031 | em->len = ins.offset; |
| 10032 | em->block_start = ins.objectid; |
| 10033 | em->block_len = ins.offset; |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 10034 | em->orig_block_len = ins.offset; |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 10035 | em->ram_bytes = ins.offset; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 10036 | set_bit(EXTENT_FLAG_PREALLOC, &em->flags); |
| 10037 | em->generation = trans->transid; |
| 10038 | |
| 10039 | while (1) { |
| 10040 | write_lock(&em_tree->lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 10041 | ret = add_extent_mapping(em_tree, em, 1); |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 10042 | write_unlock(&em_tree->lock); |
| 10043 | if (ret != -EEXIST) |
| 10044 | break; |
Nikolay Borisov | dcdbc05 | 2017-02-20 13:50:45 +0200 | [diff] [blame] | 10045 | btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset, |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 10046 | cur_offset + ins.offset - 1, |
| 10047 | 0); |
| 10048 | } |
| 10049 | free_extent_map(em); |
| 10050 | next: |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 10051 | num_bytes -= ins.offset; |
| 10052 | cur_offset += ins.offset; |
Yan, Zheng | efa5646 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 10053 | *alloc_hint = ins.objectid + ins.offset; |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 10054 | |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 10055 | inode_inc_iversion(inode); |
Deepa Dinamani | c2050a4 | 2016-09-14 07:48:06 -0700 | [diff] [blame] | 10056 | inode->i_ctime = current_time(inode); |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 10057 | BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 10058 | if (!(mode & FALLOC_FL_KEEP_SIZE) && |
Yan, Zheng | efa5646 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 10059 | (actual_len > inode->i_size) && |
| 10060 | (cur_offset > inode->i_size)) { |
Aneesh Kumar K.V | d1ea6a6 | 2010-01-20 07:28:54 +0000 | [diff] [blame] | 10061 | if (cur_offset > actual_len) |
Josef Bacik | 55a61d1 | 2010-11-22 18:50:32 +0000 | [diff] [blame] | 10062 | i_size = actual_len; |
Aneesh Kumar K.V | d1ea6a6 | 2010-01-20 07:28:54 +0000 | [diff] [blame] | 10063 | else |
Josef Bacik | 55a61d1 | 2010-11-22 18:50:32 +0000 | [diff] [blame] | 10064 | i_size = cur_offset; |
| 10065 | i_size_write(inode, i_size); |
Nikolay Borisov | 76aea53 | 2020-11-02 16:48:53 +0200 | [diff] [blame] | 10066 | btrfs_inode_safe_disk_i_size_write(BTRFS_I(inode), 0); |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 10067 | } |
| 10068 | |
Nikolay Borisov | 9a56fcd | 2020-11-02 16:48:59 +0200 | [diff] [blame] | 10069 | ret = btrfs_update_inode(trans, root, BTRFS_I(inode)); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 10070 | |
| 10071 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 10072 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 10073 | if (own_trans) |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 10074 | btrfs_end_transaction(trans); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 10075 | break; |
| 10076 | } |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 10077 | |
Filipe Manana | 8fccebf | 2020-09-08 11:27:20 +0100 | [diff] [blame] | 10078 | if (own_trans) { |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 10079 | btrfs_end_transaction(trans); |
Filipe Manana | 8fccebf | 2020-09-08 11:27:20 +0100 | [diff] [blame] | 10080 | trans = NULL; |
| 10081 | } |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 10082 | } |
Josef Bacik | b778cf9 | 2020-02-13 10:47:31 -0500 | [diff] [blame] | 10083 | if (clear_offset < end) |
Nikolay Borisov | 25ce28c | 2020-06-03 08:55:39 +0300 | [diff] [blame] | 10084 | btrfs_free_reserved_data_space(BTRFS_I(inode), NULL, clear_offset, |
Josef Bacik | b778cf9 | 2020-02-13 10:47:31 -0500 | [diff] [blame] | 10085 | end - clear_offset + 1); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 10086 | return ret; |
| 10087 | } |
| 10088 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 10089 | int btrfs_prealloc_file_range(struct inode *inode, int mode, |
| 10090 | u64 start, u64 num_bytes, u64 min_size, |
| 10091 | loff_t actual_len, u64 *alloc_hint) |
| 10092 | { |
| 10093 | return __btrfs_prealloc_file_range(inode, mode, start, num_bytes, |
| 10094 | min_size, actual_len, alloc_hint, |
| 10095 | NULL); |
| 10096 | } |
| 10097 | |
| 10098 | int btrfs_prealloc_file_range_trans(struct inode *inode, |
| 10099 | struct btrfs_trans_handle *trans, int mode, |
| 10100 | u64 start, u64 num_bytes, u64 min_size, |
| 10101 | loff_t actual_len, u64 *alloc_hint) |
| 10102 | { |
| 10103 | return __btrfs_prealloc_file_range(inode, mode, start, num_bytes, |
| 10104 | min_size, actual_len, alloc_hint, trans); |
| 10105 | } |
| 10106 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 10107 | static int btrfs_set_page_dirty(struct page *page) |
| 10108 | { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 10109 | return __set_page_dirty_nobuffers(page); |
| 10110 | } |
| 10111 | |
Christian Brauner | 549c729 | 2021-01-21 14:19:43 +0100 | [diff] [blame] | 10112 | static int btrfs_permission(struct user_namespace *mnt_userns, |
| 10113 | struct inode *inode, int mask) |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 10114 | { |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 10115 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Jeff Mahoney | cb6db4e | 2011-08-15 17:27:21 +0000 | [diff] [blame] | 10116 | umode_t mode = inode->i_mode; |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 10117 | |
Jeff Mahoney | cb6db4e | 2011-08-15 17:27:21 +0000 | [diff] [blame] | 10118 | if (mask & MAY_WRITE && |
| 10119 | (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) { |
| 10120 | if (btrfs_root_readonly(root)) |
| 10121 | return -EROFS; |
| 10122 | if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY) |
| 10123 | return -EACCES; |
| 10124 | } |
Christian Brauner | 47291ba | 2021-01-21 14:19:24 +0100 | [diff] [blame] | 10125 | return generic_permission(&init_user_ns, inode, mask); |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 10126 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10127 | |
Christian Brauner | 549c729 | 2021-01-21 14:19:43 +0100 | [diff] [blame] | 10128 | static int btrfs_tmpfile(struct user_namespace *mnt_userns, struct inode *dir, |
| 10129 | struct dentry *dentry, umode_t mode) |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 10130 | { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 10131 | struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 10132 | struct btrfs_trans_handle *trans; |
| 10133 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 10134 | struct inode *inode = NULL; |
| 10135 | u64 objectid; |
| 10136 | u64 index; |
| 10137 | int ret = 0; |
| 10138 | |
| 10139 | /* |
| 10140 | * 5 units required for adding orphan entry |
| 10141 | */ |
| 10142 | trans = btrfs_start_transaction(root, 5); |
| 10143 | if (IS_ERR(trans)) |
| 10144 | return PTR_ERR(trans); |
| 10145 | |
Nikolay Borisov | 543068a | 2020-12-07 17:32:33 +0200 | [diff] [blame] | 10146 | ret = btrfs_get_free_objectid(root, &objectid); |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 10147 | if (ret) |
| 10148 | goto out; |
| 10149 | |
| 10150 | inode = btrfs_new_inode(trans, root, dir, NULL, 0, |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 10151 | btrfs_ino(BTRFS_I(dir)), objectid, mode, &index); |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 10152 | if (IS_ERR(inode)) { |
| 10153 | ret = PTR_ERR(inode); |
| 10154 | inode = NULL; |
| 10155 | goto out; |
| 10156 | } |
| 10157 | |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 10158 | inode->i_fop = &btrfs_file_operations; |
| 10159 | inode->i_op = &btrfs_file_inode_operations; |
| 10160 | |
| 10161 | inode->i_mapping->a_ops = &btrfs_aops; |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 10162 | |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 10163 | ret = btrfs_init_inode_security(trans, inode, dir, NULL); |
| 10164 | if (ret) |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 10165 | goto out; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 10166 | |
Nikolay Borisov | 9a56fcd | 2020-11-02 16:48:59 +0200 | [diff] [blame] | 10167 | ret = btrfs_update_inode(trans, root, BTRFS_I(inode)); |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 10168 | if (ret) |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 10169 | goto out; |
Nikolay Borisov | 73f2e54 | 2017-02-20 13:50:59 +0200 | [diff] [blame] | 10170 | ret = btrfs_orphan_add(trans, BTRFS_I(inode)); |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 10171 | if (ret) |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 10172 | goto out; |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 10173 | |
Filipe Manana | 5762b5c | 2014-08-01 00:10:32 +0100 | [diff] [blame] | 10174 | /* |
| 10175 | * We set number of links to 0 in btrfs_new_inode(), and here we set |
| 10176 | * it to 1 because d_tmpfile() will issue a warning if the count is 0, |
| 10177 | * through: |
| 10178 | * |
| 10179 | * d_tmpfile() -> inode_dec_link_count() -> drop_nlink() |
| 10180 | */ |
| 10181 | set_nlink(inode, 1); |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 10182 | d_tmpfile(dentry, inode); |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 10183 | unlock_new_inode(inode); |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 10184 | mark_inode_dirty(inode); |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 10185 | out: |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 10186 | btrfs_end_transaction(trans); |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 10187 | if (ret && inode) |
| 10188 | discard_new_inode(inode); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 10189 | btrfs_btree_balance_dirty(fs_info); |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 10190 | return ret; |
| 10191 | } |
| 10192 | |
David Sterba | 5cdc84b | 2018-07-18 20:32:52 +0200 | [diff] [blame] | 10193 | void btrfs_set_range_writeback(struct extent_io_tree *tree, u64 start, u64 end) |
Josef Bacik | c6100a4 | 2017-05-05 11:57:13 -0400 | [diff] [blame] | 10194 | { |
David Sterba | 5cdc84b | 2018-07-18 20:32:52 +0200 | [diff] [blame] | 10195 | struct inode *inode = tree->private_data; |
Josef Bacik | c6100a4 | 2017-05-05 11:57:13 -0400 | [diff] [blame] | 10196 | unsigned long index = start >> PAGE_SHIFT; |
| 10197 | unsigned long end_index = end >> PAGE_SHIFT; |
| 10198 | struct page *page; |
| 10199 | |
| 10200 | while (index <= end_index) { |
| 10201 | page = find_get_page(inode->i_mapping, index); |
| 10202 | ASSERT(page); /* Pages should be in the extent_io_tree */ |
| 10203 | set_page_writeback(page); |
| 10204 | put_page(page); |
| 10205 | index++; |
| 10206 | } |
| 10207 | } |
| 10208 | |
Omar Sandoval | ed46ff3 | 2016-11-03 10:28:14 -0700 | [diff] [blame] | 10209 | #ifdef CONFIG_SWAP |
| 10210 | /* |
| 10211 | * Add an entry indicating a block group or device which is pinned by a |
| 10212 | * swapfile. Returns 0 on success, 1 if there is already an entry for it, or a |
| 10213 | * negative errno on failure. |
| 10214 | */ |
| 10215 | static int btrfs_add_swapfile_pin(struct inode *inode, void *ptr, |
| 10216 | bool is_block_group) |
| 10217 | { |
| 10218 | struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info; |
| 10219 | struct btrfs_swapfile_pin *sp, *entry; |
| 10220 | struct rb_node **p; |
| 10221 | struct rb_node *parent = NULL; |
| 10222 | |
| 10223 | sp = kmalloc(sizeof(*sp), GFP_NOFS); |
| 10224 | if (!sp) |
| 10225 | return -ENOMEM; |
| 10226 | sp->ptr = ptr; |
| 10227 | sp->inode = inode; |
| 10228 | sp->is_block_group = is_block_group; |
Filipe Manana | 195a49e | 2021-02-05 12:55:37 +0000 | [diff] [blame] | 10229 | sp->bg_extent_count = 1; |
Omar Sandoval | ed46ff3 | 2016-11-03 10:28:14 -0700 | [diff] [blame] | 10230 | |
| 10231 | spin_lock(&fs_info->swapfile_pins_lock); |
| 10232 | p = &fs_info->swapfile_pins.rb_node; |
| 10233 | while (*p) { |
| 10234 | parent = *p; |
| 10235 | entry = rb_entry(parent, struct btrfs_swapfile_pin, node); |
| 10236 | if (sp->ptr < entry->ptr || |
| 10237 | (sp->ptr == entry->ptr && sp->inode < entry->inode)) { |
| 10238 | p = &(*p)->rb_left; |
| 10239 | } else if (sp->ptr > entry->ptr || |
| 10240 | (sp->ptr == entry->ptr && sp->inode > entry->inode)) { |
| 10241 | p = &(*p)->rb_right; |
| 10242 | } else { |
Filipe Manana | 195a49e | 2021-02-05 12:55:37 +0000 | [diff] [blame] | 10243 | if (is_block_group) |
| 10244 | entry->bg_extent_count++; |
Omar Sandoval | ed46ff3 | 2016-11-03 10:28:14 -0700 | [diff] [blame] | 10245 | spin_unlock(&fs_info->swapfile_pins_lock); |
| 10246 | kfree(sp); |
| 10247 | return 1; |
| 10248 | } |
| 10249 | } |
| 10250 | rb_link_node(&sp->node, parent, p); |
| 10251 | rb_insert_color(&sp->node, &fs_info->swapfile_pins); |
| 10252 | spin_unlock(&fs_info->swapfile_pins_lock); |
| 10253 | return 0; |
| 10254 | } |
| 10255 | |
| 10256 | /* Free all of the entries pinned by this swapfile. */ |
| 10257 | static void btrfs_free_swapfile_pins(struct inode *inode) |
| 10258 | { |
| 10259 | struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info; |
| 10260 | struct btrfs_swapfile_pin *sp; |
| 10261 | struct rb_node *node, *next; |
| 10262 | |
| 10263 | spin_lock(&fs_info->swapfile_pins_lock); |
| 10264 | node = rb_first(&fs_info->swapfile_pins); |
| 10265 | while (node) { |
| 10266 | next = rb_next(node); |
| 10267 | sp = rb_entry(node, struct btrfs_swapfile_pin, node); |
| 10268 | if (sp->inode == inode) { |
| 10269 | rb_erase(&sp->node, &fs_info->swapfile_pins); |
Filipe Manana | 195a49e | 2021-02-05 12:55:37 +0000 | [diff] [blame] | 10270 | if (sp->is_block_group) { |
| 10271 | btrfs_dec_block_group_swap_extents(sp->ptr, |
| 10272 | sp->bg_extent_count); |
Omar Sandoval | ed46ff3 | 2016-11-03 10:28:14 -0700 | [diff] [blame] | 10273 | btrfs_put_block_group(sp->ptr); |
Filipe Manana | 195a49e | 2021-02-05 12:55:37 +0000 | [diff] [blame] | 10274 | } |
Omar Sandoval | ed46ff3 | 2016-11-03 10:28:14 -0700 | [diff] [blame] | 10275 | kfree(sp); |
| 10276 | } |
| 10277 | node = next; |
| 10278 | } |
| 10279 | spin_unlock(&fs_info->swapfile_pins_lock); |
| 10280 | } |
| 10281 | |
| 10282 | struct btrfs_swap_info { |
| 10283 | u64 start; |
| 10284 | u64 block_start; |
| 10285 | u64 block_len; |
| 10286 | u64 lowest_ppage; |
| 10287 | u64 highest_ppage; |
| 10288 | unsigned long nr_pages; |
| 10289 | int nr_extents; |
| 10290 | }; |
| 10291 | |
| 10292 | static int btrfs_add_swap_extent(struct swap_info_struct *sis, |
| 10293 | struct btrfs_swap_info *bsi) |
| 10294 | { |
| 10295 | unsigned long nr_pages; |
| 10296 | u64 first_ppage, first_ppage_reported, next_ppage; |
| 10297 | int ret; |
| 10298 | |
| 10299 | first_ppage = ALIGN(bsi->block_start, PAGE_SIZE) >> PAGE_SHIFT; |
| 10300 | next_ppage = ALIGN_DOWN(bsi->block_start + bsi->block_len, |
| 10301 | PAGE_SIZE) >> PAGE_SHIFT; |
| 10302 | |
| 10303 | if (first_ppage >= next_ppage) |
| 10304 | return 0; |
| 10305 | nr_pages = next_ppage - first_ppage; |
| 10306 | |
| 10307 | first_ppage_reported = first_ppage; |
| 10308 | if (bsi->start == 0) |
| 10309 | first_ppage_reported++; |
| 10310 | if (bsi->lowest_ppage > first_ppage_reported) |
| 10311 | bsi->lowest_ppage = first_ppage_reported; |
| 10312 | if (bsi->highest_ppage < (next_ppage - 1)) |
| 10313 | bsi->highest_ppage = next_ppage - 1; |
| 10314 | |
| 10315 | ret = add_swap_extent(sis, bsi->nr_pages, nr_pages, first_ppage); |
| 10316 | if (ret < 0) |
| 10317 | return ret; |
| 10318 | bsi->nr_extents += ret; |
| 10319 | bsi->nr_pages += nr_pages; |
| 10320 | return 0; |
| 10321 | } |
| 10322 | |
| 10323 | static void btrfs_swap_deactivate(struct file *file) |
| 10324 | { |
| 10325 | struct inode *inode = file_inode(file); |
| 10326 | |
| 10327 | btrfs_free_swapfile_pins(inode); |
| 10328 | atomic_dec(&BTRFS_I(inode)->root->nr_swapfiles); |
| 10329 | } |
| 10330 | |
| 10331 | static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file, |
| 10332 | sector_t *span) |
| 10333 | { |
| 10334 | struct inode *inode = file_inode(file); |
Filipe Manana | dd0734f | 2021-02-05 12:55:38 +0000 | [diff] [blame] | 10335 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 10336 | struct btrfs_fs_info *fs_info = root->fs_info; |
Omar Sandoval | ed46ff3 | 2016-11-03 10:28:14 -0700 | [diff] [blame] | 10337 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
| 10338 | struct extent_state *cached_state = NULL; |
| 10339 | struct extent_map *em = NULL; |
| 10340 | struct btrfs_device *device = NULL; |
| 10341 | struct btrfs_swap_info bsi = { |
| 10342 | .lowest_ppage = (sector_t)-1ULL, |
| 10343 | }; |
| 10344 | int ret = 0; |
| 10345 | u64 isize; |
| 10346 | u64 start; |
| 10347 | |
| 10348 | /* |
| 10349 | * If the swap file was just created, make sure delalloc is done. If the |
| 10350 | * file changes again after this, the user is doing something stupid and |
| 10351 | * we don't really care. |
| 10352 | */ |
| 10353 | ret = btrfs_wait_ordered_range(inode, 0, (u64)-1); |
| 10354 | if (ret) |
| 10355 | return ret; |
| 10356 | |
| 10357 | /* |
| 10358 | * The inode is locked, so these flags won't change after we check them. |
| 10359 | */ |
| 10360 | if (BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS) { |
| 10361 | btrfs_warn(fs_info, "swapfile must not be compressed"); |
| 10362 | return -EINVAL; |
| 10363 | } |
| 10364 | if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW)) { |
| 10365 | btrfs_warn(fs_info, "swapfile must not be copy-on-write"); |
| 10366 | return -EINVAL; |
| 10367 | } |
| 10368 | if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) { |
| 10369 | btrfs_warn(fs_info, "swapfile must not be checksummed"); |
| 10370 | return -EINVAL; |
| 10371 | } |
| 10372 | |
| 10373 | /* |
| 10374 | * Balance or device remove/replace/resize can move stuff around from |
Goldwyn Rodrigues | c3e1f96 | 2020-08-25 10:02:32 -0500 | [diff] [blame] | 10375 | * under us. The exclop protection makes sure they aren't running/won't |
| 10376 | * run concurrently while we are mapping the swap extents, and |
| 10377 | * fs_info->swapfile_pins prevents them from running while the swap |
| 10378 | * file is active and moving the extents. Note that this also prevents |
| 10379 | * 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] | 10380 | * really worth the trouble to allow it. |
| 10381 | */ |
Goldwyn Rodrigues | c3e1f96 | 2020-08-25 10:02:32 -0500 | [diff] [blame] | 10382 | if (!btrfs_exclop_start(fs_info, BTRFS_EXCLOP_SWAP_ACTIVATE)) { |
Omar Sandoval | ed46ff3 | 2016-11-03 10:28:14 -0700 | [diff] [blame] | 10383 | btrfs_warn(fs_info, |
| 10384 | "cannot activate swapfile while exclusive operation is running"); |
| 10385 | return -EBUSY; |
| 10386 | } |
Filipe Manana | dd0734f | 2021-02-05 12:55:38 +0000 | [diff] [blame] | 10387 | |
| 10388 | /* |
| 10389 | * Prevent snapshot creation while we are activating the swap file. |
| 10390 | * We do not want to race with snapshot creation. If snapshot creation |
| 10391 | * already started before we bumped nr_swapfiles from 0 to 1 and |
| 10392 | * completes before the first write into the swap file after it is |
| 10393 | * activated, than that write would fallback to COW. |
| 10394 | */ |
| 10395 | if (!btrfs_drew_try_write_lock(&root->snapshot_lock)) { |
| 10396 | btrfs_exclop_finish(fs_info); |
| 10397 | btrfs_warn(fs_info, |
| 10398 | "cannot activate swapfile because snapshot creation is in progress"); |
| 10399 | return -EINVAL; |
| 10400 | } |
Omar Sandoval | ed46ff3 | 2016-11-03 10:28:14 -0700 | [diff] [blame] | 10401 | /* |
| 10402 | * Snapshots can create extents which require COW even if NODATACOW is |
| 10403 | * set. We use this counter to prevent snapshots. We must increment it |
| 10404 | * before walking the extents because we don't want a concurrent |
| 10405 | * snapshot to run after we've already checked the extents. |
| 10406 | */ |
Filipe Manana | dd0734f | 2021-02-05 12:55:38 +0000 | [diff] [blame] | 10407 | atomic_inc(&root->nr_swapfiles); |
Omar Sandoval | ed46ff3 | 2016-11-03 10:28:14 -0700 | [diff] [blame] | 10408 | |
| 10409 | isize = ALIGN_DOWN(inode->i_size, fs_info->sectorsize); |
| 10410 | |
| 10411 | lock_extent_bits(io_tree, 0, isize - 1, &cached_state); |
| 10412 | start = 0; |
| 10413 | while (start < isize) { |
| 10414 | u64 logical_block_start, physical_block_start; |
David Sterba | 32da5386 | 2019-10-29 19:20:18 +0100 | [diff] [blame] | 10415 | struct btrfs_block_group *bg; |
Omar Sandoval | ed46ff3 | 2016-11-03 10:28:14 -0700 | [diff] [blame] | 10416 | u64 len = isize - start; |
| 10417 | |
Omar Sandoval | 39b07b5 | 2019-12-02 17:34:23 -0800 | [diff] [blame] | 10418 | em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len); |
Omar Sandoval | ed46ff3 | 2016-11-03 10:28:14 -0700 | [diff] [blame] | 10419 | if (IS_ERR(em)) { |
| 10420 | ret = PTR_ERR(em); |
| 10421 | goto out; |
| 10422 | } |
| 10423 | |
| 10424 | if (em->block_start == EXTENT_MAP_HOLE) { |
| 10425 | btrfs_warn(fs_info, "swapfile must not have holes"); |
| 10426 | ret = -EINVAL; |
| 10427 | goto out; |
| 10428 | } |
| 10429 | if (em->block_start == EXTENT_MAP_INLINE) { |
| 10430 | /* |
| 10431 | * It's unlikely we'll ever actually find ourselves |
| 10432 | * here, as a file small enough to fit inline won't be |
| 10433 | * big enough to store more than the swap header, but in |
| 10434 | * case something changes in the future, let's catch it |
| 10435 | * here rather than later. |
| 10436 | */ |
| 10437 | btrfs_warn(fs_info, "swapfile must not be inline"); |
| 10438 | ret = -EINVAL; |
| 10439 | goto out; |
| 10440 | } |
| 10441 | if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) { |
| 10442 | btrfs_warn(fs_info, "swapfile must not be compressed"); |
| 10443 | ret = -EINVAL; |
| 10444 | goto out; |
| 10445 | } |
| 10446 | |
| 10447 | logical_block_start = em->block_start + (start - em->start); |
| 10448 | len = min(len, em->len - (start - em->start)); |
| 10449 | free_extent_map(em); |
| 10450 | em = NULL; |
| 10451 | |
Boris Burkov | a84d5d4 | 2020-08-18 11:00:05 -0700 | [diff] [blame] | 10452 | ret = can_nocow_extent(inode, start, &len, NULL, NULL, NULL, true); |
Omar Sandoval | ed46ff3 | 2016-11-03 10:28:14 -0700 | [diff] [blame] | 10453 | if (ret < 0) { |
| 10454 | goto out; |
| 10455 | } else if (ret) { |
| 10456 | ret = 0; |
| 10457 | } else { |
| 10458 | btrfs_warn(fs_info, |
| 10459 | "swapfile must not be copy-on-write"); |
| 10460 | ret = -EINVAL; |
| 10461 | goto out; |
| 10462 | } |
| 10463 | |
| 10464 | em = btrfs_get_chunk_map(fs_info, logical_block_start, len); |
| 10465 | if (IS_ERR(em)) { |
| 10466 | ret = PTR_ERR(em); |
| 10467 | goto out; |
| 10468 | } |
| 10469 | |
| 10470 | if (em->map_lookup->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) { |
| 10471 | btrfs_warn(fs_info, |
| 10472 | "swapfile must have single data profile"); |
| 10473 | ret = -EINVAL; |
| 10474 | goto out; |
| 10475 | } |
| 10476 | |
| 10477 | if (device == NULL) { |
| 10478 | device = em->map_lookup->stripes[0].dev; |
| 10479 | ret = btrfs_add_swapfile_pin(inode, device, false); |
| 10480 | if (ret == 1) |
| 10481 | ret = 0; |
| 10482 | else if (ret) |
| 10483 | goto out; |
| 10484 | } else if (device != em->map_lookup->stripes[0].dev) { |
| 10485 | btrfs_warn(fs_info, "swapfile must be on one device"); |
| 10486 | ret = -EINVAL; |
| 10487 | goto out; |
| 10488 | } |
| 10489 | |
| 10490 | physical_block_start = (em->map_lookup->stripes[0].physical + |
| 10491 | (logical_block_start - em->start)); |
| 10492 | len = min(len, em->len - (logical_block_start - em->start)); |
| 10493 | free_extent_map(em); |
| 10494 | em = NULL; |
| 10495 | |
| 10496 | bg = btrfs_lookup_block_group(fs_info, logical_block_start); |
| 10497 | if (!bg) { |
| 10498 | btrfs_warn(fs_info, |
| 10499 | "could not find block group containing swapfile"); |
| 10500 | ret = -EINVAL; |
| 10501 | goto out; |
| 10502 | } |
| 10503 | |
Filipe Manana | 195a49e | 2021-02-05 12:55:37 +0000 | [diff] [blame] | 10504 | if (!btrfs_inc_block_group_swap_extents(bg)) { |
| 10505 | btrfs_warn(fs_info, |
| 10506 | "block group for swapfile at %llu is read-only%s", |
| 10507 | bg->start, |
| 10508 | atomic_read(&fs_info->scrubs_running) ? |
| 10509 | " (scrub running)" : ""); |
| 10510 | btrfs_put_block_group(bg); |
| 10511 | ret = -EINVAL; |
| 10512 | goto out; |
| 10513 | } |
| 10514 | |
Omar Sandoval | ed46ff3 | 2016-11-03 10:28:14 -0700 | [diff] [blame] | 10515 | ret = btrfs_add_swapfile_pin(inode, bg, true); |
| 10516 | if (ret) { |
| 10517 | btrfs_put_block_group(bg); |
| 10518 | if (ret == 1) |
| 10519 | ret = 0; |
| 10520 | else |
| 10521 | goto out; |
| 10522 | } |
| 10523 | |
| 10524 | if (bsi.block_len && |
| 10525 | bsi.block_start + bsi.block_len == physical_block_start) { |
| 10526 | bsi.block_len += len; |
| 10527 | } else { |
| 10528 | if (bsi.block_len) { |
| 10529 | ret = btrfs_add_swap_extent(sis, &bsi); |
| 10530 | if (ret) |
| 10531 | goto out; |
| 10532 | } |
| 10533 | bsi.start = start; |
| 10534 | bsi.block_start = physical_block_start; |
| 10535 | bsi.block_len = len; |
| 10536 | } |
| 10537 | |
| 10538 | start += len; |
| 10539 | } |
| 10540 | |
| 10541 | if (bsi.block_len) |
| 10542 | ret = btrfs_add_swap_extent(sis, &bsi); |
| 10543 | |
| 10544 | out: |
| 10545 | if (!IS_ERR_OR_NULL(em)) |
| 10546 | free_extent_map(em); |
| 10547 | |
| 10548 | unlock_extent_cached(io_tree, 0, isize - 1, &cached_state); |
| 10549 | |
| 10550 | if (ret) |
| 10551 | btrfs_swap_deactivate(file); |
| 10552 | |
Filipe Manana | dd0734f | 2021-02-05 12:55:38 +0000 | [diff] [blame] | 10553 | btrfs_drew_write_unlock(&root->snapshot_lock); |
| 10554 | |
Goldwyn Rodrigues | c3e1f96 | 2020-08-25 10:02:32 -0500 | [diff] [blame] | 10555 | btrfs_exclop_finish(fs_info); |
Omar Sandoval | ed46ff3 | 2016-11-03 10:28:14 -0700 | [diff] [blame] | 10556 | |
| 10557 | if (ret) |
| 10558 | return ret; |
| 10559 | |
| 10560 | if (device) |
| 10561 | sis->bdev = device->bdev; |
| 10562 | *span = bsi.highest_ppage - bsi.lowest_ppage + 1; |
| 10563 | sis->max = bsi.nr_pages; |
| 10564 | sis->pages = bsi.nr_pages - 1; |
| 10565 | sis->highest_bit = bsi.nr_pages - 1; |
| 10566 | return bsi.nr_extents; |
| 10567 | } |
| 10568 | #else |
| 10569 | static void btrfs_swap_deactivate(struct file *file) |
| 10570 | { |
| 10571 | } |
| 10572 | |
| 10573 | static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file, |
| 10574 | sector_t *span) |
| 10575 | { |
| 10576 | return -EOPNOTSUPP; |
| 10577 | } |
| 10578 | #endif |
| 10579 | |
Filipe Manana | 2766ff6 | 2020-11-04 11:07:34 +0000 | [diff] [blame] | 10580 | /* |
| 10581 | * Update the number of bytes used in the VFS' inode. When we replace extents in |
| 10582 | * a range (clone, dedupe, fallocate's zero range), we must update the number of |
| 10583 | * bytes used by the inode in an atomic manner, so that concurrent stat(2) calls |
| 10584 | * always get a correct value. |
| 10585 | */ |
| 10586 | void btrfs_update_inode_bytes(struct btrfs_inode *inode, |
| 10587 | const u64 add_bytes, |
| 10588 | const u64 del_bytes) |
| 10589 | { |
| 10590 | if (add_bytes == del_bytes) |
| 10591 | return; |
| 10592 | |
| 10593 | spin_lock(&inode->lock); |
| 10594 | if (del_bytes > 0) |
| 10595 | inode_sub_bytes(&inode->vfs_inode, del_bytes); |
| 10596 | if (add_bytes > 0) |
| 10597 | inode_add_bytes(&inode->vfs_inode, add_bytes); |
| 10598 | spin_unlock(&inode->lock); |
| 10599 | } |
| 10600 | |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 10601 | static const struct inode_operations btrfs_dir_inode_operations = { |
Chris Mason | 3394e16 | 2008-11-17 20:42:26 -0500 | [diff] [blame] | 10602 | .getattr = btrfs_getattr, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10603 | .lookup = btrfs_lookup, |
| 10604 | .create = btrfs_create, |
| 10605 | .unlink = btrfs_unlink, |
| 10606 | .link = btrfs_link, |
| 10607 | .mkdir = btrfs_mkdir, |
| 10608 | .rmdir = btrfs_rmdir, |
Miklos Szeredi | 2773bf0 | 2016-09-27 11:03:58 +0200 | [diff] [blame] | 10609 | .rename = btrfs_rename2, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10610 | .symlink = btrfs_symlink, |
| 10611 | .setattr = btrfs_setattr, |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 10612 | .mknod = btrfs_mknod, |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 10613 | .listxattr = btrfs_listxattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 10614 | .permission = btrfs_permission, |
Christoph Hellwig | 4e34e71 | 2011-07-23 17:37:31 +0200 | [diff] [blame] | 10615 | .get_acl = btrfs_get_acl, |
Christoph Hellwig | 996a710 | 2013-12-20 05:16:43 -0800 | [diff] [blame] | 10616 | .set_acl = btrfs_set_acl, |
Guangyu Sun | 93fd63c | 2013-09-16 10:42:03 -0700 | [diff] [blame] | 10617 | .update_time = btrfs_update_time, |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 10618 | .tmpfile = btrfs_tmpfile, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10619 | }; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 10620 | |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 10621 | static const struct file_operations btrfs_dir_file_operations = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10622 | .llseek = generic_file_llseek, |
| 10623 | .read = generic_read_dir, |
Omar Sandoval | 02dbfc9 | 2016-05-20 13:50:33 -0700 | [diff] [blame] | 10624 | .iterate_shared = btrfs_real_readdir, |
Josef Bacik | 23b5ec7 | 2017-07-24 15:14:25 -0400 | [diff] [blame] | 10625 | .open = btrfs_opendir, |
Christoph Hellwig | 34287aa | 2007-09-14 10:22:47 -0400 | [diff] [blame] | 10626 | .unlocked_ioctl = btrfs_ioctl, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10627 | #ifdef CONFIG_COMPAT |
Luke Dashjr | 4c63c24 | 2015-10-29 08:22:21 +0000 | [diff] [blame] | 10628 | .compat_ioctl = btrfs_compat_ioctl, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10629 | #endif |
Sage Weil | 6bf13c0 | 2008-06-10 10:07:39 -0400 | [diff] [blame] | 10630 | .release = btrfs_release_file, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 10631 | .fsync = btrfs_sync_file, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10632 | }; |
| 10633 | |
Chris Mason | 3505439 | 2009-01-21 13:11:13 -0500 | [diff] [blame] | 10634 | /* |
| 10635 | * btrfs doesn't support the bmap operation because swapfiles |
| 10636 | * use bmap to make a mapping of extents in the file. They assume |
| 10637 | * these extents won't change over the life of the file and they |
| 10638 | * use the bmap result to do IO directly to the drive. |
| 10639 | * |
| 10640 | * the btrfs bmap call would return logical addresses that aren't |
| 10641 | * suitable for IO and they also will change frequently as COW |
| 10642 | * operations happen. So, swapfile + btrfs == corruption. |
| 10643 | * |
| 10644 | * For now we're avoiding this by dropping bmap. |
| 10645 | */ |
Alexey Dobriyan | 7f09410 | 2009-09-21 17:01:10 -0700 | [diff] [blame] | 10646 | static const struct address_space_operations btrfs_aops = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10647 | .readpage = btrfs_readpage, |
| 10648 | .writepage = btrfs_writepage, |
Chris Mason | b293f02e | 2007-11-01 19:45:34 -0400 | [diff] [blame] | 10649 | .writepages = btrfs_writepages, |
Matthew Wilcox (Oracle) | ba206a0 | 2020-06-01 21:47:05 -0700 | [diff] [blame] | 10650 | .readahead = btrfs_readahead, |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 10651 | .direct_IO = noop_direct_IO, |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 10652 | .invalidatepage = btrfs_invalidatepage, |
| 10653 | .releasepage = btrfs_releasepage, |
Roman Gushchin | f8e6608 | 2020-03-04 16:57:35 -0800 | [diff] [blame] | 10654 | #ifdef CONFIG_MIGRATION |
| 10655 | .migratepage = btrfs_migratepage, |
| 10656 | #endif |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 10657 | .set_page_dirty = btrfs_set_page_dirty, |
Andi Kleen | 465fdd9 | 2009-09-16 11:50:18 +0200 | [diff] [blame] | 10658 | .error_remove_page = generic_error_remove_page, |
Omar Sandoval | ed46ff3 | 2016-11-03 10:28:14 -0700 | [diff] [blame] | 10659 | .swap_activate = btrfs_swap_activate, |
| 10660 | .swap_deactivate = btrfs_swap_deactivate, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10661 | }; |
| 10662 | |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 10663 | static const struct inode_operations btrfs_file_inode_operations = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10664 | .getattr = btrfs_getattr, |
| 10665 | .setattr = btrfs_setattr, |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 10666 | .listxattr = btrfs_listxattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 10667 | .permission = btrfs_permission, |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 10668 | .fiemap = btrfs_fiemap, |
Christoph Hellwig | 4e34e71 | 2011-07-23 17:37:31 +0200 | [diff] [blame] | 10669 | .get_acl = btrfs_get_acl, |
Christoph Hellwig | 996a710 | 2013-12-20 05:16:43 -0800 | [diff] [blame] | 10670 | .set_acl = btrfs_set_acl, |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 10671 | .update_time = btrfs_update_time, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10672 | }; |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 10673 | static const struct inode_operations btrfs_special_inode_operations = { |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 10674 | .getattr = btrfs_getattr, |
| 10675 | .setattr = btrfs_setattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 10676 | .permission = btrfs_permission, |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 10677 | .listxattr = btrfs_listxattr, |
Christoph Hellwig | 4e34e71 | 2011-07-23 17:37:31 +0200 | [diff] [blame] | 10678 | .get_acl = btrfs_get_acl, |
Christoph Hellwig | 996a710 | 2013-12-20 05:16:43 -0800 | [diff] [blame] | 10679 | .set_acl = btrfs_set_acl, |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 10680 | .update_time = btrfs_update_time, |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 10681 | }; |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 10682 | static const struct inode_operations btrfs_symlink_inode_operations = { |
Al Viro | 6b25539 | 2015-11-17 10:20:54 -0500 | [diff] [blame] | 10683 | .get_link = page_get_link, |
Li Zefan | f209561 | 2010-11-19 02:05:24 +0000 | [diff] [blame] | 10684 | .getattr = btrfs_getattr, |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 10685 | .setattr = btrfs_setattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 10686 | .permission = btrfs_permission, |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 10687 | .listxattr = btrfs_listxattr, |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 10688 | .update_time = btrfs_update_time, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10689 | }; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 10690 | |
Alexey Dobriyan | 82d339d | 2009-10-09 09:54:36 -0400 | [diff] [blame] | 10691 | const struct dentry_operations btrfs_dentry_operations = { |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 10692 | .d_delete = btrfs_dentry_delete, |
| 10693 | }; |