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" |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 48 | #include "inode-map.h" |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 49 | #include "props.h" |
Dongsheng Yang | 3119321 | 2014-12-12 16:44:35 +0800 | [diff] [blame] | 50 | #include "qgroup.h" |
Josef Bacik | 8673634 | 2019-06-19 15:12:00 -0400 | [diff] [blame] | 51 | #include "delalloc-space.h" |
Josef Bacik | aac0023 | 2019-06-20 15:37:44 -0400 | [diff] [blame] | 52 | #include "block-group.h" |
Filipe Manana | 467dc47 | 2020-05-27 11:16:07 +0100 | [diff] [blame] | 53 | #include "space-info.h" |
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 | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 696 | PAGE_CLEAR_DIRTY | |
| 697 | PAGE_SET_WRITEBACK | |
Filipe Manana | e6eb431 | 2014-10-10 10:45:12 +0100 | [diff] [blame] | 698 | page_error_op | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 699 | PAGE_END_WRITEBACK); |
Nikolay Borisov | cecc8d9 | 2019-07-17 14:41:45 +0300 | [diff] [blame] | 700 | |
Qu Wenruo | 1e6e238 | 2020-07-28 16:39:26 +0800 | [diff] [blame] | 701 | /* |
| 702 | * Ensure we only free the compressed pages if we have |
| 703 | * them allocated, as we can still reach here with |
| 704 | * inode_need_compress() == false. |
| 705 | */ |
| 706 | if (pages) { |
| 707 | for (i = 0; i < nr_pages; i++) { |
| 708 | WARN_ON(pages[i]->mapping); |
| 709 | put_page(pages[i]); |
| 710 | } |
| 711 | kfree(pages); |
Nikolay Borisov | cecc8d9 | 2019-07-17 14:41:45 +0300 | [diff] [blame] | 712 | } |
Nikolay Borisov | cecc8d9 | 2019-07-17 14:41:45 +0300 | [diff] [blame] | 713 | return 0; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 714 | } |
| 715 | } |
| 716 | |
| 717 | if (will_compress) { |
| 718 | /* |
| 719 | * we aren't doing an inline extent round the compressed size |
| 720 | * up to a block size boundary so the allocator does sane |
| 721 | * things |
| 722 | */ |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 723 | total_compressed = ALIGN(total_compressed, blocksize); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 724 | |
| 725 | /* |
| 726 | * one last check to make sure the compression is really a |
Timofey Titovets | 170607e | 2017-06-06 14:41:15 +0300 | [diff] [blame] | 727 | * win, compare the page count read with the blocks on disk, |
| 728 | * compression must free at least one sector size |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 729 | */ |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 730 | total_in = ALIGN(total_in, PAGE_SIZE); |
Timofey Titovets | 170607e | 2017-06-06 14:41:15 +0300 | [diff] [blame] | 731 | if (total_compressed + blocksize <= total_in) { |
Nikolay Borisov | ac3e993 | 2019-07-17 14:41:44 +0300 | [diff] [blame] | 732 | compressed_extents++; |
Ashish Samant | c8bb0c8 | 2016-03-25 19:01:33 -0700 | [diff] [blame] | 733 | |
| 734 | /* |
| 735 | * The async work queues will take care of doing actual |
| 736 | * allocation on disk for these compressed pages, and |
| 737 | * will submit them to the elevator. |
| 738 | */ |
Nikolay Borisov | b532627 | 2019-03-12 17:20:25 +0200 | [diff] [blame] | 739 | add_async_extent(async_chunk, start, total_in, |
David Sterba | 4d3a800 | 2017-02-14 19:04:07 +0100 | [diff] [blame] | 740 | total_compressed, pages, nr_pages, |
Ashish Samant | c8bb0c8 | 2016-03-25 19:01:33 -0700 | [diff] [blame] | 741 | compress_type); |
| 742 | |
Timofey Titovets | 1170862 | 2017-10-03 18:06:01 +0300 | [diff] [blame] | 743 | if (start + total_in < end) { |
| 744 | start += total_in; |
Ashish Samant | c8bb0c8 | 2016-03-25 19:01:33 -0700 | [diff] [blame] | 745 | pages = NULL; |
| 746 | cond_resched(); |
| 747 | goto again; |
| 748 | } |
Nikolay Borisov | ac3e993 | 2019-07-17 14:41:44 +0300 | [diff] [blame] | 749 | return compressed_extents; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 750 | } |
| 751 | } |
Ashish Samant | c8bb0c8 | 2016-03-25 19:01:33 -0700 | [diff] [blame] | 752 | if (pages) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 753 | /* |
| 754 | * the compression code ran but failed to make things smaller, |
| 755 | * free any pages it allocated and our page pointer array |
| 756 | */ |
David Sterba | 4d3a800 | 2017-02-14 19:04:07 +0100 | [diff] [blame] | 757 | for (i = 0; i < nr_pages; i++) { |
Chris Mason | 70b99e6 | 2008-10-31 12:46:39 -0400 | [diff] [blame] | 758 | WARN_ON(pages[i]->mapping); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 759 | put_page(pages[i]); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 760 | } |
| 761 | kfree(pages); |
| 762 | pages = NULL; |
| 763 | total_compressed = 0; |
David Sterba | 4d3a800 | 2017-02-14 19:04:07 +0100 | [diff] [blame] | 764 | nr_pages = 0; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 765 | |
| 766 | /* flag the file so we don't compress in the future */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 767 | if (!btrfs_test_opt(fs_info, FORCE_COMPRESS) && |
David Sterba | b52aa8c | 2017-07-17 19:17:20 +0200 | [diff] [blame] | 768 | !(BTRFS_I(inode)->prop_compress)) { |
Chris Mason | a555f81 | 2010-01-28 16:18:15 -0500 | [diff] [blame] | 769 | BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS; |
Chris Mason | 1e701a3 | 2010-03-11 09:42:04 -0500 | [diff] [blame] | 770 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 771 | } |
Chris Mason | f03d9301f | 2009-02-04 09:31:06 -0500 | [diff] [blame] | 772 | cleanup_and_bail_uncompressed: |
Ashish Samant | c8bb0c8 | 2016-03-25 19:01:33 -0700 | [diff] [blame] | 773 | /* |
| 774 | * No compression, but we still need to write the pages in the file |
| 775 | * we've been given so far. redirty the locked page if it corresponds |
| 776 | * to our extent and set things up for the async work queue to run |
| 777 | * cow_file_range to do the normal delalloc dance. |
| 778 | */ |
Chris Mason | 1d53c9e | 2019-07-10 12:28:16 -0700 | [diff] [blame] | 779 | if (async_chunk->locked_page && |
| 780 | (page_offset(async_chunk->locked_page) >= start && |
| 781 | page_offset(async_chunk->locked_page)) <= end) { |
Nikolay Borisov | 1368c6d | 2019-03-12 17:20:27 +0200 | [diff] [blame] | 782 | __set_page_dirty_nobuffers(async_chunk->locked_page); |
Ashish Samant | c8bb0c8 | 2016-03-25 19:01:33 -0700 | [diff] [blame] | 783 | /* unlocked later on in the async handlers */ |
Chris Mason | 1d53c9e | 2019-07-10 12:28:16 -0700 | [diff] [blame] | 784 | } |
Ashish Samant | c8bb0c8 | 2016-03-25 19:01:33 -0700 | [diff] [blame] | 785 | |
| 786 | if (redirty) |
| 787 | extent_range_redirty_for_io(inode, start, end); |
Nikolay Borisov | b532627 | 2019-03-12 17:20:25 +0200 | [diff] [blame] | 788 | add_async_extent(async_chunk, start, end - start + 1, 0, NULL, 0, |
Ashish Samant | c8bb0c8 | 2016-03-25 19:01:33 -0700 | [diff] [blame] | 789 | BTRFS_COMPRESS_NONE); |
Nikolay Borisov | ac3e993 | 2019-07-17 14:41:44 +0300 | [diff] [blame] | 790 | compressed_extents++; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 791 | |
Nikolay Borisov | ac3e993 | 2019-07-17 14:41:44 +0300 | [diff] [blame] | 792 | return compressed_extents; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 793 | } |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 794 | |
Filipe Manana | 40ae837 | 2014-10-06 22:14:24 +0100 | [diff] [blame] | 795 | static void free_async_extent_pages(struct async_extent *async_extent) |
| 796 | { |
| 797 | int i; |
| 798 | |
| 799 | if (!async_extent->pages) |
| 800 | return; |
| 801 | |
| 802 | for (i = 0; i < async_extent->nr_pages; i++) { |
| 803 | WARN_ON(async_extent->pages[i]->mapping); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 804 | put_page(async_extent->pages[i]); |
Filipe Manana | 40ae837 | 2014-10-06 22:14:24 +0100 | [diff] [blame] | 805 | } |
| 806 | kfree(async_extent->pages); |
| 807 | async_extent->nr_pages = 0; |
| 808 | async_extent->pages = NULL; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 809 | } |
| 810 | |
| 811 | /* |
| 812 | * phase two of compressed writeback. This is the ordered portion |
| 813 | * of the code, which only gets called in the order the work was |
| 814 | * queued. We walk all the async extents created by compress_file_range |
| 815 | * and send them down to the disk. |
| 816 | */ |
Nikolay Borisov | b532627 | 2019-03-12 17:20:25 +0200 | [diff] [blame] | 817 | static noinline void submit_compressed_extents(struct async_chunk *async_chunk) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 818 | { |
Nikolay Borisov | a0ff10d | 2020-06-03 08:55:17 +0300 | [diff] [blame] | 819 | struct btrfs_inode *inode = BTRFS_I(async_chunk->inode); |
| 820 | struct btrfs_fs_info *fs_info = inode->root->fs_info; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 821 | struct async_extent *async_extent; |
| 822 | u64 alloc_hint = 0; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 823 | struct btrfs_key ins; |
| 824 | struct extent_map *em; |
Nikolay Borisov | a0ff10d | 2020-06-03 08:55:17 +0300 | [diff] [blame] | 825 | struct btrfs_root *root = inode->root; |
| 826 | struct extent_io_tree *io_tree = &inode->io_tree; |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 827 | int ret = 0; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 828 | |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 829 | again: |
Nikolay Borisov | b532627 | 2019-03-12 17:20:25 +0200 | [diff] [blame] | 830 | while (!list_empty(&async_chunk->extents)) { |
| 831 | async_extent = list_entry(async_chunk->extents.next, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 832 | struct async_extent, list); |
| 833 | list_del(&async_extent->list); |
| 834 | |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 835 | retry: |
Nikolay Borisov | 7447555 | 2019-03-12 17:20:30 +0200 | [diff] [blame] | 836 | lock_extent(io_tree, async_extent->start, |
| 837 | async_extent->start + async_extent->ram_size - 1); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 838 | /* did the compression code fall back to uncompressed IO? */ |
| 839 | if (!async_extent->pages) { |
| 840 | int page_started = 0; |
| 841 | unsigned long nr_written = 0; |
| 842 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 843 | /* allocate blocks */ |
Nikolay Borisov | a0ff10d | 2020-06-03 08:55:17 +0300 | [diff] [blame] | 844 | ret = cow_file_range(inode, async_chunk->locked_page, |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 845 | async_extent->start, |
| 846 | async_extent->start + |
| 847 | async_extent->ram_size - 1, |
Nikolay Borisov | 330a582 | 2019-07-17 16:18:17 +0300 | [diff] [blame] | 848 | &page_started, &nr_written, 0); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 849 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 850 | /* JDM XXX */ |
| 851 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 852 | /* |
| 853 | * if page_started, cow_file_range inserted an |
| 854 | * inline extent and took care of all the unlocking |
| 855 | * and IO for us. Otherwise, we need to submit |
| 856 | * all those pages down to the drive. |
| 857 | */ |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 858 | if (!page_started && !ret) |
Nikolay Borisov | a0ff10d | 2020-06-03 08:55:17 +0300 | [diff] [blame] | 859 | extent_write_locked_range(&inode->vfs_inode, |
Nikolay Borisov | 5e3ee23 | 2017-12-08 15:55:58 +0200 | [diff] [blame] | 860 | async_extent->start, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 861 | async_extent->start + |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 862 | async_extent->ram_size - 1, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 863 | WB_SYNC_ALL); |
Chris Mason | 1d53c9e | 2019-07-10 12:28:16 -0700 | [diff] [blame] | 864 | else if (ret && async_chunk->locked_page) |
Nikolay Borisov | b532627 | 2019-03-12 17:20:25 +0200 | [diff] [blame] | 865 | unlock_page(async_chunk->locked_page); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 866 | kfree(async_extent); |
| 867 | cond_resched(); |
| 868 | continue; |
| 869 | } |
| 870 | |
Wang Xiaoguang | 1851309 | 2016-07-25 15:51:40 +0800 | [diff] [blame] | 871 | ret = btrfs_reserve_extent(root, async_extent->ram_size, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 872 | async_extent->compressed_size, |
| 873 | async_extent->compressed_size, |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 874 | 0, alloc_hint, &ins, 1, 1); |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 875 | if (ret) { |
Filipe Manana | 40ae837 | 2014-10-06 22:14:24 +0100 | [diff] [blame] | 876 | free_async_extent_pages(async_extent); |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 877 | |
Josef Bacik | fdf8e2e | 2013-06-14 16:58:23 -0400 | [diff] [blame] | 878 | if (ret == -ENOSPC) { |
| 879 | unlock_extent(io_tree, async_extent->start, |
| 880 | async_extent->start + |
| 881 | async_extent->ram_size - 1); |
Liu Bo | ce62003 | 2014-07-24 22:48:05 +0800 | [diff] [blame] | 882 | |
| 883 | /* |
| 884 | * we need to redirty the pages if we decide to |
| 885 | * fallback to uncompressed IO, otherwise we |
| 886 | * will not submit these pages down to lower |
| 887 | * layers. |
| 888 | */ |
Nikolay Borisov | a0ff10d | 2020-06-03 08:55:17 +0300 | [diff] [blame] | 889 | extent_range_redirty_for_io(&inode->vfs_inode, |
Liu Bo | ce62003 | 2014-07-24 22:48:05 +0800 | [diff] [blame] | 890 | async_extent->start, |
| 891 | async_extent->start + |
| 892 | async_extent->ram_size - 1); |
| 893 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 894 | goto retry; |
Josef Bacik | fdf8e2e | 2013-06-14 16:58:23 -0400 | [diff] [blame] | 895 | } |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 896 | goto out_free; |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 897 | } |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 898 | /* |
| 899 | * here we're doing allocation and writeback of the |
| 900 | * compressed pages |
| 901 | */ |
Nikolay Borisov | a0ff10d | 2020-06-03 08:55:17 +0300 | [diff] [blame] | 902 | em = create_io_em(inode, async_extent->start, |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 903 | async_extent->ram_size, /* len */ |
| 904 | async_extent->start, /* orig_start */ |
| 905 | ins.objectid, /* block_start */ |
| 906 | ins.offset, /* block_len */ |
| 907 | ins.offset, /* orig_block_len */ |
| 908 | async_extent->ram_size, /* ram_bytes */ |
| 909 | async_extent->compress_type, |
| 910 | BTRFS_ORDERED_COMPRESSED); |
| 911 | if (IS_ERR(em)) |
| 912 | /* ret value is not necessary due to void function */ |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 913 | goto out_free_reserve; |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 914 | free_extent_map(em); |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 915 | |
Nikolay Borisov | a0ff10d | 2020-06-03 08:55:17 +0300 | [diff] [blame] | 916 | ret = btrfs_add_ordered_extent_compress(inode, |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 917 | async_extent->start, |
| 918 | ins.objectid, |
| 919 | async_extent->ram_size, |
| 920 | ins.offset, |
| 921 | BTRFS_ORDERED_COMPRESSED, |
| 922 | async_extent->compress_type); |
Filipe Manana | d9f8596 | 2014-08-25 10:43:00 +0100 | [diff] [blame] | 923 | if (ret) { |
Nikolay Borisov | a0ff10d | 2020-06-03 08:55:17 +0300 | [diff] [blame] | 924 | btrfs_drop_extent_cache(inode, async_extent->start, |
Filipe Manana | d9f8596 | 2014-08-25 10:43:00 +0100 | [diff] [blame] | 925 | async_extent->start + |
| 926 | async_extent->ram_size - 1, 0); |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 927 | goto out_free_reserve; |
Filipe Manana | d9f8596 | 2014-08-25 10:43:00 +0100 | [diff] [blame] | 928 | } |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 929 | btrfs_dec_block_group_reservations(fs_info, ins.objectid); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 930 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 931 | /* |
| 932 | * clear dirty, set writeback and unlock the pages. |
| 933 | */ |
Nikolay Borisov | a0ff10d | 2020-06-03 08:55:17 +0300 | [diff] [blame] | 934 | extent_clear_unlock_delalloc(inode, async_extent->start, |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 935 | async_extent->start + |
| 936 | async_extent->ram_size - 1, |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 937 | NULL, EXTENT_LOCKED | EXTENT_DELALLOC, |
| 938 | PAGE_UNLOCK | PAGE_CLEAR_DIRTY | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 939 | PAGE_SET_WRITEBACK); |
Nikolay Borisov | a0ff10d | 2020-06-03 08:55:17 +0300 | [diff] [blame] | 940 | if (btrfs_submit_compressed_write(inode, async_extent->start, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 941 | async_extent->ram_size, |
| 942 | ins.objectid, |
| 943 | ins.offset, async_extent->pages, |
Liu Bo | f82b735 | 2017-10-23 23:18:16 -0600 | [diff] [blame] | 944 | async_extent->nr_pages, |
Chris Mason | ec39f76 | 2019-07-10 12:28:17 -0700 | [diff] [blame] | 945 | async_chunk->write_flags, |
| 946 | async_chunk->blkcg_css)) { |
Filipe Manana | fce2a4e | 2014-10-06 22:14:23 +0100 | [diff] [blame] | 947 | struct page *p = async_extent->pages[0]; |
| 948 | const u64 start = async_extent->start; |
| 949 | const u64 end = start + async_extent->ram_size - 1; |
| 950 | |
Nikolay Borisov | a0ff10d | 2020-06-03 08:55:17 +0300 | [diff] [blame] | 951 | p->mapping = inode->vfs_inode.i_mapping; |
Nikolay Borisov | c629732 | 2018-11-08 10:18:08 +0200 | [diff] [blame] | 952 | btrfs_writepage_endio_finish_ordered(p, start, end, 0); |
Nikolay Borisov | 7087a9d | 2018-11-01 14:09:48 +0200 | [diff] [blame] | 953 | |
Filipe Manana | fce2a4e | 2014-10-06 22:14:23 +0100 | [diff] [blame] | 954 | p->mapping = NULL; |
Nikolay Borisov | a0ff10d | 2020-06-03 08:55:17 +0300 | [diff] [blame] | 955 | extent_clear_unlock_delalloc(inode, start, end, NULL, 0, |
Filipe Manana | fce2a4e | 2014-10-06 22:14:23 +0100 | [diff] [blame] | 956 | PAGE_END_WRITEBACK | |
| 957 | PAGE_SET_ERROR); |
Filipe Manana | 40ae837 | 2014-10-06 22:14:24 +0100 | [diff] [blame] | 958 | free_async_extent_pages(async_extent); |
Filipe Manana | fce2a4e | 2014-10-06 22:14:23 +0100 | [diff] [blame] | 959 | } |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 960 | alloc_hint = ins.objectid + ins.offset; |
| 961 | kfree(async_extent); |
| 962 | cond_resched(); |
| 963 | } |
Filipe Manana | dec8f17 | 2014-10-06 22:14:26 +0100 | [diff] [blame] | 964 | return; |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 965 | out_free_reserve: |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 966 | btrfs_dec_block_group_reservations(fs_info, ins.objectid); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 967 | btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 968 | out_free: |
Nikolay Borisov | a0ff10d | 2020-06-03 08:55:17 +0300 | [diff] [blame] | 969 | extent_clear_unlock_delalloc(inode, async_extent->start, |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 970 | async_extent->start + |
| 971 | async_extent->ram_size - 1, |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 972 | NULL, EXTENT_LOCKED | EXTENT_DELALLOC | |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 973 | EXTENT_DELALLOC_NEW | |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 974 | EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING, |
| 975 | PAGE_UNLOCK | PAGE_CLEAR_DIRTY | |
Filipe Manana | 704de49 | 2014-10-06 22:14:22 +0100 | [diff] [blame] | 976 | PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK | |
| 977 | PAGE_SET_ERROR); |
Filipe Manana | 40ae837 | 2014-10-06 22:14:24 +0100 | [diff] [blame] | 978 | free_async_extent_pages(async_extent); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 979 | kfree(async_extent); |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 980 | goto again; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 981 | } |
| 982 | |
Nikolay Borisov | 43c6984 | 2020-06-03 08:55:02 +0300 | [diff] [blame] | 983 | static u64 get_extent_allocation_hint(struct btrfs_inode *inode, u64 start, |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 984 | u64 num_bytes) |
| 985 | { |
Nikolay Borisov | 43c6984 | 2020-06-03 08:55:02 +0300 | [diff] [blame] | 986 | struct extent_map_tree *em_tree = &inode->extent_tree; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 987 | struct extent_map *em; |
| 988 | u64 alloc_hint = 0; |
| 989 | |
| 990 | read_lock(&em_tree->lock); |
| 991 | em = search_extent_mapping(em_tree, start, num_bytes); |
| 992 | if (em) { |
| 993 | /* |
| 994 | * if block start isn't an actual block number then find the |
| 995 | * first block in this inode and use that as a hint. If that |
| 996 | * block is also bogus then just don't worry about it. |
| 997 | */ |
| 998 | if (em->block_start >= EXTENT_MAP_LAST_BYTE) { |
| 999 | free_extent_map(em); |
| 1000 | em = search_extent_mapping(em_tree, 0, 0); |
| 1001 | if (em && em->block_start < EXTENT_MAP_LAST_BYTE) |
| 1002 | alloc_hint = em->block_start; |
| 1003 | if (em) |
| 1004 | free_extent_map(em); |
| 1005 | } else { |
| 1006 | alloc_hint = em->block_start; |
| 1007 | free_extent_map(em); |
| 1008 | } |
| 1009 | } |
| 1010 | read_unlock(&em_tree->lock); |
| 1011 | |
| 1012 | return alloc_hint; |
| 1013 | } |
| 1014 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1015 | /* |
| 1016 | * when extent_io.c finds a delayed allocation range in the file, |
| 1017 | * the call backs end up in this code. The basic idea is to |
| 1018 | * allocate extents on disk for the range, and create ordered data structs |
| 1019 | * in ram to track those extents. |
| 1020 | * |
| 1021 | * locked_page is the page that writepage had locked already. We use |
| 1022 | * it to make sure we don't do extra locks or unlocks. |
| 1023 | * |
| 1024 | * *page_started is set to one if we unlock locked_page and do everything |
| 1025 | * required to start IO on it. It may be clean and already done with |
| 1026 | * IO when we return. |
| 1027 | */ |
Nikolay Borisov | 6e26c44 | 2020-06-03 08:55:14 +0300 | [diff] [blame] | 1028 | static noinline int cow_file_range(struct btrfs_inode *inode, |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 1029 | struct page *locked_page, |
Nikolay Borisov | 74e9194 | 2019-07-17 16:18:16 +0300 | [diff] [blame] | 1030 | u64 start, u64 end, int *page_started, |
Nikolay Borisov | 330a582 | 2019-07-17 16:18:17 +0300 | [diff] [blame] | 1031 | unsigned long *nr_written, int unlock) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1032 | { |
Nikolay Borisov | 6e26c44 | 2020-06-03 08:55:14 +0300 | [diff] [blame] | 1033 | struct btrfs_root *root = inode->root; |
| 1034 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1035 | u64 alloc_hint = 0; |
| 1036 | u64 num_bytes; |
| 1037 | unsigned long ram_size; |
Filipe Manana | a315e68 | 2017-03-06 23:04:20 +0000 | [diff] [blame] | 1038 | u64 cur_alloc_size = 0; |
Filipe Manana | 432cd2a | 2020-06-08 13:32:55 +0100 | [diff] [blame] | 1039 | u64 min_alloc_size; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1040 | u64 blocksize = fs_info->sectorsize; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1041 | struct btrfs_key ins; |
| 1042 | struct extent_map *em; |
Filipe Manana | a315e68 | 2017-03-06 23:04:20 +0000 | [diff] [blame] | 1043 | unsigned clear_bits; |
| 1044 | unsigned long page_ops; |
| 1045 | bool extent_reserved = false; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1046 | int ret = 0; |
| 1047 | |
Nikolay Borisov | 6e26c44 | 2020-06-03 08:55:14 +0300 | [diff] [blame] | 1048 | if (btrfs_is_free_space_inode(inode)) { |
Josef Bacik | 02ecd2c | 2013-10-25 16:19:08 -0400 | [diff] [blame] | 1049 | WARN_ON_ONCE(1); |
Josef Bacik | 29bce2f | 2014-02-07 12:21:23 -0500 | [diff] [blame] | 1050 | ret = -EINVAL; |
| 1051 | goto out_unlock; |
Josef Bacik | 02ecd2c | 2013-10-25 16:19:08 -0400 | [diff] [blame] | 1052 | } |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1053 | |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 1054 | num_bytes = ALIGN(end - start + 1, blocksize); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1055 | num_bytes = max(blocksize, num_bytes); |
Anand Jain | 566b176 | 2018-02-15 18:07:59 +0800 | [diff] [blame] | 1056 | ASSERT(num_bytes <= btrfs_super_total_bytes(fs_info->super_copy)); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1057 | |
Nikolay Borisov | 6e26c44 | 2020-06-03 08:55:14 +0300 | [diff] [blame] | 1058 | inode_should_defrag(inode, start, end, num_bytes, SZ_64K); |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1059 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1060 | if (start == 0) { |
| 1061 | /* lets try to make an inline extent */ |
Nikolay Borisov | 6e26c44 | 2020-06-03 08:55:14 +0300 | [diff] [blame] | 1062 | ret = cow_file_range_inline(inode, start, end, 0, |
Nikolay Borisov | d02c0e2 | 2018-03-02 09:43:15 +0200 | [diff] [blame] | 1063 | BTRFS_COMPRESS_NONE, NULL); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1064 | if (ret == 0) { |
Josef Bacik | 8b62f87 | 2017-10-19 14:15:55 -0400 | [diff] [blame] | 1065 | /* |
| 1066 | * We use DO_ACCOUNTING here because we need the |
| 1067 | * delalloc_release_metadata to be run _after_ we drop |
| 1068 | * our outstanding extent for clearing delalloc for this |
| 1069 | * range. |
| 1070 | */ |
Nikolay Borisov | 6e26c44 | 2020-06-03 08:55:14 +0300 | [diff] [blame] | 1071 | extent_clear_unlock_delalloc(inode, start, end, NULL, |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1072 | EXTENT_LOCKED | EXTENT_DELALLOC | |
Josef Bacik | 8b62f87 | 2017-10-19 14:15:55 -0400 | [diff] [blame] | 1073 | EXTENT_DELALLOC_NEW | EXTENT_DEFRAG | |
| 1074 | EXTENT_DO_ACCOUNTING, PAGE_UNLOCK | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1075 | PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK | |
| 1076 | PAGE_END_WRITEBACK); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1077 | *nr_written = *nr_written + |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1078 | (end - start + PAGE_SIZE) / PAGE_SIZE; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1079 | *page_started = 1; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1080 | goto out; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1081 | } else if (ret < 0) { |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1082 | goto out_unlock; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1083 | } |
| 1084 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1085 | |
Nikolay Borisov | 6e26c44 | 2020-06-03 08:55:14 +0300 | [diff] [blame] | 1086 | alloc_hint = get_extent_allocation_hint(inode, start, num_bytes); |
| 1087 | btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 1088 | |
Filipe Manana | 432cd2a | 2020-06-08 13:32:55 +0100 | [diff] [blame] | 1089 | /* |
| 1090 | * Relocation relies on the relocated extents to have exactly the same |
| 1091 | * size as the original extents. Normally writeback for relocation data |
| 1092 | * extents follows a NOCOW path because relocation preallocates the |
| 1093 | * extents. However, due to an operation such as scrub turning a block |
| 1094 | * group to RO mode, it may fallback to COW mode, so we must make sure |
| 1095 | * an extent allocated during COW has exactly the requested size and can |
| 1096 | * not be split into smaller extents, otherwise relocation breaks and |
| 1097 | * fails during the stage where it updates the bytenr of file extent |
| 1098 | * items. |
| 1099 | */ |
| 1100 | if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID) |
| 1101 | min_alloc_size = num_bytes; |
| 1102 | else |
| 1103 | min_alloc_size = fs_info->sectorsize; |
| 1104 | |
Anand Jain | 3752d22 | 2018-02-15 12:29:38 +0800 | [diff] [blame] | 1105 | while (num_bytes > 0) { |
| 1106 | cur_alloc_size = num_bytes; |
Wang Xiaoguang | 1851309 | 2016-07-25 15:51:40 +0800 | [diff] [blame] | 1107 | ret = btrfs_reserve_extent(root, cur_alloc_size, cur_alloc_size, |
Filipe Manana | 432cd2a | 2020-06-08 13:32:55 +0100 | [diff] [blame] | 1108 | min_alloc_size, 0, alloc_hint, |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 1109 | &ins, 1, 1); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 1110 | if (ret < 0) |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1111 | goto out_unlock; |
Filipe Manana | a315e68 | 2017-03-06 23:04:20 +0000 | [diff] [blame] | 1112 | cur_alloc_size = ins.offset; |
| 1113 | extent_reserved = true; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1114 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1115 | ram_size = ins.offset; |
Nikolay Borisov | 6e26c44 | 2020-06-03 08:55:14 +0300 | [diff] [blame] | 1116 | em = create_io_em(inode, start, ins.offset, /* len */ |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 1117 | start, /* orig_start */ |
| 1118 | ins.objectid, /* block_start */ |
| 1119 | ins.offset, /* block_len */ |
| 1120 | ins.offset, /* orig_block_len */ |
| 1121 | ram_size, /* ram_bytes */ |
| 1122 | BTRFS_COMPRESS_NONE, /* compress_type */ |
Liu Bo | 1af4a0a | 2017-02-13 15:35:09 -0800 | [diff] [blame] | 1123 | BTRFS_ORDERED_REGULAR /* type */); |
Su Yue | 090a127a | 2018-05-30 16:48:56 +0800 | [diff] [blame] | 1124 | if (IS_ERR(em)) { |
| 1125 | ret = PTR_ERR(em); |
Liu Bo | ace68ba | 2013-04-22 10:53:47 +0000 | [diff] [blame] | 1126 | goto out_reserve; |
Su Yue | 090a127a | 2018-05-30 16:48:56 +0800 | [diff] [blame] | 1127 | } |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 1128 | free_extent_map(em); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1129 | |
Nikolay Borisov | 6e26c44 | 2020-06-03 08:55:14 +0300 | [diff] [blame] | 1130 | ret = btrfs_add_ordered_extent(inode, start, ins.objectid, |
| 1131 | ram_size, cur_alloc_size, 0); |
Liu Bo | ace68ba | 2013-04-22 10:53:47 +0000 | [diff] [blame] | 1132 | if (ret) |
Filipe Manana | d9f8596 | 2014-08-25 10:43:00 +0100 | [diff] [blame] | 1133 | goto out_drop_extent_cache; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1134 | |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1135 | if (root->root_key.objectid == |
| 1136 | BTRFS_DATA_RELOC_TREE_OBJECTID) { |
Nikolay Borisov | 6e26c44 | 2020-06-03 08:55:14 +0300 | [diff] [blame] | 1137 | ret = btrfs_reloc_clone_csums(inode, start, |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1138 | cur_alloc_size); |
Qu Wenruo | 4dbd80f | 2017-03-08 10:25:51 +0800 | [diff] [blame] | 1139 | /* |
| 1140 | * Only drop cache here, and process as normal. |
| 1141 | * |
| 1142 | * We must not allow extent_clear_unlock_delalloc() |
| 1143 | * at out_unlock label to free meta of this ordered |
| 1144 | * extent, as its meta should be freed by |
| 1145 | * btrfs_finish_ordered_io(). |
| 1146 | * |
| 1147 | * So we must continue until @start is increased to |
| 1148 | * skip current ordered extent. |
| 1149 | */ |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 1150 | if (ret) |
Nikolay Borisov | 6e26c44 | 2020-06-03 08:55:14 +0300 | [diff] [blame] | 1151 | btrfs_drop_extent_cache(inode, start, |
Qu Wenruo | 4dbd80f | 2017-03-08 10:25:51 +0800 | [diff] [blame] | 1152 | start + ram_size - 1, 0); |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1153 | } |
| 1154 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1155 | btrfs_dec_block_group_reservations(fs_info, ins.objectid); |
Filipe Manana | 9cfa3e3 | 2016-04-26 15:39:32 +0100 | [diff] [blame] | 1156 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1157 | /* we're not doing compressed IO, don't unlock the first |
| 1158 | * page (which the caller expects to stay locked), don't |
| 1159 | * clear any dirty bits and don't set any writeback bits |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 1160 | * |
| 1161 | * Do set the Private2 bit so we know this page was properly |
| 1162 | * setup for writepage |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1163 | */ |
Filipe Manana | a315e68 | 2017-03-06 23:04:20 +0000 | [diff] [blame] | 1164 | page_ops = unlock ? PAGE_UNLOCK : 0; |
| 1165 | page_ops |= PAGE_SET_PRIVATE2; |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 1166 | |
Nikolay Borisov | 6e26c44 | 2020-06-03 08:55:14 +0300 | [diff] [blame] | 1167 | extent_clear_unlock_delalloc(inode, start, start + ram_size - 1, |
Nikolay Borisov | 74e9194 | 2019-07-17 16:18:16 +0300 | [diff] [blame] | 1168 | locked_page, |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1169 | EXTENT_LOCKED | EXTENT_DELALLOC, |
Filipe Manana | a315e68 | 2017-03-06 23:04:20 +0000 | [diff] [blame] | 1170 | page_ops); |
Anand Jain | 3752d22 | 2018-02-15 12:29:38 +0800 | [diff] [blame] | 1171 | if (num_bytes < cur_alloc_size) |
| 1172 | num_bytes = 0; |
Qu Wenruo | 4dbd80f | 2017-03-08 10:25:51 +0800 | [diff] [blame] | 1173 | else |
Anand Jain | 3752d22 | 2018-02-15 12:29:38 +0800 | [diff] [blame] | 1174 | num_bytes -= cur_alloc_size; |
Chris Mason | c59f895 | 2007-12-17 20:14:04 -0500 | [diff] [blame] | 1175 | alloc_hint = ins.objectid + ins.offset; |
| 1176 | start += cur_alloc_size; |
Filipe Manana | a315e68 | 2017-03-06 23:04:20 +0000 | [diff] [blame] | 1177 | extent_reserved = false; |
Qu Wenruo | 4dbd80f | 2017-03-08 10:25:51 +0800 | [diff] [blame] | 1178 | |
| 1179 | /* |
| 1180 | * btrfs_reloc_clone_csums() error, since start is increased |
| 1181 | * extent_clear_unlock_delalloc() at out_unlock label won't |
| 1182 | * free metadata of current ordered extent, we're OK to exit. |
| 1183 | */ |
| 1184 | if (ret) |
| 1185 | goto out_unlock; |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 1186 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1187 | out: |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1188 | return ret; |
Miao Xie | b7d5b0a | 2012-11-01 07:32:18 +0000 | [diff] [blame] | 1189 | |
Filipe Manana | d9f8596 | 2014-08-25 10:43:00 +0100 | [diff] [blame] | 1190 | out_drop_extent_cache: |
Nikolay Borisov | 6e26c44 | 2020-06-03 08:55:14 +0300 | [diff] [blame] | 1191 | btrfs_drop_extent_cache(inode, start, start + ram_size - 1, 0); |
Liu Bo | ace68ba | 2013-04-22 10:53:47 +0000 | [diff] [blame] | 1192 | out_reserve: |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1193 | btrfs_dec_block_group_reservations(fs_info, ins.objectid); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 1194 | btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1195 | out_unlock: |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 1196 | clear_bits = EXTENT_LOCKED | EXTENT_DELALLOC | EXTENT_DELALLOC_NEW | |
| 1197 | EXTENT_DEFRAG | EXTENT_CLEAR_META_RESV; |
Filipe Manana | a315e68 | 2017-03-06 23:04:20 +0000 | [diff] [blame] | 1198 | page_ops = PAGE_UNLOCK | PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK | |
| 1199 | PAGE_END_WRITEBACK; |
| 1200 | /* |
| 1201 | * If we reserved an extent for our delalloc range (or a subrange) and |
| 1202 | * failed to create the respective ordered extent, then it means that |
| 1203 | * when we reserved the extent we decremented the extent's size from |
| 1204 | * the data space_info's bytes_may_use counter and incremented the |
| 1205 | * space_info's bytes_reserved counter by the same amount. We must make |
| 1206 | * sure extent_clear_unlock_delalloc() does not try to decrement again |
| 1207 | * the data space_info's bytes_may_use counter, therefore we do not pass |
| 1208 | * it the flag EXTENT_CLEAR_DATA_RESV. |
| 1209 | */ |
| 1210 | if (extent_reserved) { |
Nikolay Borisov | 6e26c44 | 2020-06-03 08:55:14 +0300 | [diff] [blame] | 1211 | extent_clear_unlock_delalloc(inode, start, |
Filipe Manana | e2c8e92 | 2020-05-27 11:15:53 +0100 | [diff] [blame] | 1212 | start + cur_alloc_size - 1, |
Filipe Manana | a315e68 | 2017-03-06 23:04:20 +0000 | [diff] [blame] | 1213 | locked_page, |
| 1214 | clear_bits, |
| 1215 | page_ops); |
| 1216 | start += cur_alloc_size; |
| 1217 | if (start >= end) |
| 1218 | goto out; |
| 1219 | } |
Nikolay Borisov | 6e26c44 | 2020-06-03 08:55:14 +0300 | [diff] [blame] | 1220 | extent_clear_unlock_delalloc(inode, start, end, locked_page, |
Filipe Manana | a315e68 | 2017-03-06 23:04:20 +0000 | [diff] [blame] | 1221 | clear_bits | EXTENT_CLEAR_DATA_RESV, |
| 1222 | page_ops); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1223 | goto out; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1224 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1225 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1226 | /* |
| 1227 | * work queue call back to started compression on a file and pages |
| 1228 | */ |
| 1229 | static noinline void async_cow_start(struct btrfs_work *work) |
| 1230 | { |
Nikolay Borisov | b532627 | 2019-03-12 17:20:25 +0200 | [diff] [blame] | 1231 | struct async_chunk *async_chunk; |
Nikolay Borisov | ac3e993 | 2019-07-17 14:41:44 +0300 | [diff] [blame] | 1232 | int compressed_extents; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1233 | |
Nikolay Borisov | b532627 | 2019-03-12 17:20:25 +0200 | [diff] [blame] | 1234 | async_chunk = container_of(work, struct async_chunk, work); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1235 | |
Nikolay Borisov | ac3e993 | 2019-07-17 14:41:44 +0300 | [diff] [blame] | 1236 | compressed_extents = compress_file_range(async_chunk); |
| 1237 | if (compressed_extents == 0) { |
Nikolay Borisov | b532627 | 2019-03-12 17:20:25 +0200 | [diff] [blame] | 1238 | btrfs_add_delayed_iput(async_chunk->inode); |
| 1239 | async_chunk->inode = NULL; |
Josef Bacik | 8180ef8 | 2012-06-08 15:16:12 -0400 | [diff] [blame] | 1240 | } |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1241 | } |
| 1242 | |
| 1243 | /* |
| 1244 | * work queue call back to submit previously compressed pages |
| 1245 | */ |
| 1246 | static noinline void async_cow_submit(struct btrfs_work *work) |
| 1247 | { |
Nikolay Borisov | c5a68ae | 2019-03-12 17:20:26 +0200 | [diff] [blame] | 1248 | struct async_chunk *async_chunk = container_of(work, struct async_chunk, |
| 1249 | work); |
| 1250 | struct btrfs_fs_info *fs_info = btrfs_work_owner(work); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1251 | unsigned long nr_pages; |
| 1252 | |
Nikolay Borisov | b532627 | 2019-03-12 17:20:25 +0200 | [diff] [blame] | 1253 | nr_pages = (async_chunk->end - async_chunk->start + PAGE_SIZE) >> |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1254 | PAGE_SHIFT; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1255 | |
David Sterba | 093258e | 2018-02-26 16:15:17 +0100 | [diff] [blame] | 1256 | /* atomic_sub_return implies a barrier */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1257 | if (atomic_sub_return(nr_pages, &fs_info->async_delalloc_pages) < |
David Sterba | 093258e | 2018-02-26 16:15:17 +0100 | [diff] [blame] | 1258 | 5 * SZ_1M) |
| 1259 | cond_wake_up_nomb(&fs_info->async_submit_wait); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1260 | |
Nikolay Borisov | 4546d17 | 2019-01-03 10:50:03 +0200 | [diff] [blame] | 1261 | /* |
Nikolay Borisov | b532627 | 2019-03-12 17:20:25 +0200 | [diff] [blame] | 1262 | * ->inode could be NULL if async_chunk_start has failed to compress, |
Nikolay Borisov | 4546d17 | 2019-01-03 10:50:03 +0200 | [diff] [blame] | 1263 | * in which case we don't have anything to submit, yet we need to |
| 1264 | * always adjust ->async_delalloc_pages as its paired with the init |
| 1265 | * happening in cow_file_range_async |
| 1266 | */ |
Nikolay Borisov | b532627 | 2019-03-12 17:20:25 +0200 | [diff] [blame] | 1267 | if (async_chunk->inode) |
| 1268 | submit_compressed_extents(async_chunk); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1269 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1270 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1271 | static noinline void async_cow_free(struct btrfs_work *work) |
| 1272 | { |
Nikolay Borisov | b532627 | 2019-03-12 17:20:25 +0200 | [diff] [blame] | 1273 | struct async_chunk *async_chunk; |
Nikolay Borisov | 97db120 | 2019-03-12 17:20:24 +0200 | [diff] [blame] | 1274 | |
Nikolay Borisov | b532627 | 2019-03-12 17:20:25 +0200 | [diff] [blame] | 1275 | async_chunk = container_of(work, struct async_chunk, work); |
| 1276 | if (async_chunk->inode) |
| 1277 | btrfs_add_delayed_iput(async_chunk->inode); |
Chris Mason | ec39f76 | 2019-07-10 12:28:17 -0700 | [diff] [blame] | 1278 | if (async_chunk->blkcg_css) |
| 1279 | css_put(async_chunk->blkcg_css); |
Nikolay Borisov | 97db120 | 2019-03-12 17:20:24 +0200 | [diff] [blame] | 1280 | /* |
| 1281 | * 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] | 1282 | * async_chunk's, freeing it ensures the whole array has been freed. |
Nikolay Borisov | 97db120 | 2019-03-12 17:20:24 +0200 | [diff] [blame] | 1283 | */ |
Nikolay Borisov | b532627 | 2019-03-12 17:20:25 +0200 | [diff] [blame] | 1284 | if (atomic_dec_and_test(async_chunk->pending)) |
Nikolay Borisov | b1c16ac | 2019-04-01 11:29:57 +0300 | [diff] [blame] | 1285 | kvfree(async_chunk->pending); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1286 | } |
| 1287 | |
Nikolay Borisov | 751b643 | 2020-06-03 08:55:22 +0300 | [diff] [blame] | 1288 | static int cow_file_range_async(struct btrfs_inode *inode, |
Chris Mason | ec39f76 | 2019-07-10 12:28:17 -0700 | [diff] [blame] | 1289 | struct writeback_control *wbc, |
| 1290 | struct page *locked_page, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1291 | u64 start, u64 end, int *page_started, |
David Sterba | fac07d2 | 2019-10-29 18:28:57 +0100 | [diff] [blame] | 1292 | unsigned long *nr_written) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1293 | { |
Nikolay Borisov | 751b643 | 2020-06-03 08:55:22 +0300 | [diff] [blame] | 1294 | struct btrfs_fs_info *fs_info = inode->root->fs_info; |
Chris Mason | ec39f76 | 2019-07-10 12:28:17 -0700 | [diff] [blame] | 1295 | struct cgroup_subsys_state *blkcg_css = wbc_blkcg_css(wbc); |
Nikolay Borisov | 97db120 | 2019-03-12 17:20:24 +0200 | [diff] [blame] | 1296 | struct async_cow *ctx; |
| 1297 | struct async_chunk *async_chunk; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1298 | unsigned long nr_pages; |
| 1299 | u64 cur_end; |
Nikolay Borisov | 97db120 | 2019-03-12 17:20:24 +0200 | [diff] [blame] | 1300 | u64 num_chunks = DIV_ROUND_UP(end - start, SZ_512K); |
| 1301 | int i; |
| 1302 | bool should_compress; |
Nikolay Borisov | b1c16ac | 2019-04-01 11:29:57 +0300 | [diff] [blame] | 1303 | unsigned nofs_flag; |
David Sterba | fac07d2 | 2019-10-29 18:28:57 +0100 | [diff] [blame] | 1304 | const unsigned int write_flags = wbc_to_write_flags(wbc); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1305 | |
Nikolay Borisov | 751b643 | 2020-06-03 08:55:22 +0300 | [diff] [blame] | 1306 | unlock_extent(&inode->io_tree, start, end); |
Nikolay Borisov | 97db120 | 2019-03-12 17:20:24 +0200 | [diff] [blame] | 1307 | |
Nikolay Borisov | 751b643 | 2020-06-03 08:55:22 +0300 | [diff] [blame] | 1308 | if (inode->flags & BTRFS_INODE_NOCOMPRESS && |
Nikolay Borisov | 97db120 | 2019-03-12 17:20:24 +0200 | [diff] [blame] | 1309 | !btrfs_test_opt(fs_info, FORCE_COMPRESS)) { |
| 1310 | num_chunks = 1; |
| 1311 | should_compress = false; |
| 1312 | } else { |
| 1313 | should_compress = true; |
| 1314 | } |
| 1315 | |
Nikolay Borisov | b1c16ac | 2019-04-01 11:29:57 +0300 | [diff] [blame] | 1316 | nofs_flag = memalloc_nofs_save(); |
| 1317 | ctx = kvmalloc(struct_size(ctx, chunks, num_chunks), GFP_KERNEL); |
| 1318 | memalloc_nofs_restore(nofs_flag); |
| 1319 | |
Nikolay Borisov | 97db120 | 2019-03-12 17:20:24 +0200 | [diff] [blame] | 1320 | if (!ctx) { |
| 1321 | unsigned clear_bits = EXTENT_LOCKED | EXTENT_DELALLOC | |
| 1322 | EXTENT_DELALLOC_NEW | EXTENT_DEFRAG | |
| 1323 | EXTENT_DO_ACCOUNTING; |
| 1324 | unsigned long page_ops = PAGE_UNLOCK | PAGE_CLEAR_DIRTY | |
| 1325 | PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK | |
| 1326 | PAGE_SET_ERROR; |
| 1327 | |
Nikolay Borisov | 751b643 | 2020-06-03 08:55:22 +0300 | [diff] [blame] | 1328 | extent_clear_unlock_delalloc(inode, start, end, locked_page, |
| 1329 | clear_bits, page_ops); |
Nikolay Borisov | 97db120 | 2019-03-12 17:20:24 +0200 | [diff] [blame] | 1330 | return -ENOMEM; |
| 1331 | } |
| 1332 | |
| 1333 | async_chunk = ctx->chunks; |
| 1334 | atomic_set(&ctx->num_chunks, num_chunks); |
| 1335 | |
| 1336 | for (i = 0; i < num_chunks; i++) { |
| 1337 | if (should_compress) |
| 1338 | cur_end = min(end, start + SZ_512K - 1); |
| 1339 | else |
| 1340 | cur_end = end; |
| 1341 | |
Nikolay Borisov | bd4691a | 2019-01-03 10:50:01 +0200 | [diff] [blame] | 1342 | /* |
| 1343 | * igrab is called higher up in the call chain, take only the |
| 1344 | * lightweight reference for the callback lifetime |
| 1345 | */ |
Nikolay Borisov | 751b643 | 2020-06-03 08:55:22 +0300 | [diff] [blame] | 1346 | ihold(&inode->vfs_inode); |
Nikolay Borisov | 97db120 | 2019-03-12 17:20:24 +0200 | [diff] [blame] | 1347 | async_chunk[i].pending = &ctx->num_chunks; |
Nikolay Borisov | 751b643 | 2020-06-03 08:55:22 +0300 | [diff] [blame] | 1348 | async_chunk[i].inode = &inode->vfs_inode; |
Nikolay Borisov | 97db120 | 2019-03-12 17:20:24 +0200 | [diff] [blame] | 1349 | async_chunk[i].start = start; |
| 1350 | async_chunk[i].end = cur_end; |
Nikolay Borisov | 97db120 | 2019-03-12 17:20:24 +0200 | [diff] [blame] | 1351 | async_chunk[i].write_flags = write_flags; |
| 1352 | INIT_LIST_HEAD(&async_chunk[i].extents); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1353 | |
Chris Mason | 1d53c9e | 2019-07-10 12:28:16 -0700 | [diff] [blame] | 1354 | /* |
| 1355 | * The locked_page comes all the way from writepage and its |
| 1356 | * the original page we were actually given. As we spread |
| 1357 | * this large delalloc region across multiple async_chunk |
| 1358 | * structs, only the first struct needs a pointer to locked_page |
| 1359 | * |
| 1360 | * This way we don't need racey decisions about who is supposed |
| 1361 | * to unlock it. |
| 1362 | */ |
| 1363 | if (locked_page) { |
Chris Mason | ec39f76 | 2019-07-10 12:28:17 -0700 | [diff] [blame] | 1364 | /* |
| 1365 | * Depending on the compressibility, the pages might or |
| 1366 | * might not go through async. We want all of them to |
| 1367 | * be accounted against wbc once. Let's do it here |
| 1368 | * before the paths diverge. wbc accounting is used |
| 1369 | * only for foreign writeback detection and doesn't |
| 1370 | * need full accuracy. Just account the whole thing |
| 1371 | * against the first page. |
| 1372 | */ |
| 1373 | wbc_account_cgroup_owner(wbc, locked_page, |
| 1374 | cur_end - start); |
Chris Mason | 1d53c9e | 2019-07-10 12:28:16 -0700 | [diff] [blame] | 1375 | async_chunk[i].locked_page = locked_page; |
| 1376 | locked_page = NULL; |
| 1377 | } else { |
| 1378 | async_chunk[i].locked_page = NULL; |
| 1379 | } |
| 1380 | |
Chris Mason | ec39f76 | 2019-07-10 12:28:17 -0700 | [diff] [blame] | 1381 | if (blkcg_css != blkcg_root_css) { |
| 1382 | css_get(blkcg_css); |
| 1383 | async_chunk[i].blkcg_css = blkcg_css; |
| 1384 | } else { |
| 1385 | async_chunk[i].blkcg_css = NULL; |
| 1386 | } |
| 1387 | |
Omar Sandoval | a0cac0e | 2019-09-16 11:30:57 -0700 | [diff] [blame] | 1388 | btrfs_init_work(&async_chunk[i].work, async_cow_start, |
| 1389 | async_cow_submit, async_cow_free); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1390 | |
Nikolay Borisov | 97db120 | 2019-03-12 17:20:24 +0200 | [diff] [blame] | 1391 | nr_pages = DIV_ROUND_UP(cur_end - start, PAGE_SIZE); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1392 | atomic_add(nr_pages, &fs_info->async_delalloc_pages); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1393 | |
Nikolay Borisov | 97db120 | 2019-03-12 17:20:24 +0200 | [diff] [blame] | 1394 | btrfs_queue_work(fs_info->delalloc_workers, &async_chunk[i].work); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1395 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1396 | *nr_written += nr_pages; |
| 1397 | start = cur_end + 1; |
| 1398 | } |
| 1399 | *page_started = 1; |
| 1400 | return 0; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1401 | } |
| 1402 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 1403 | 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] | 1404 | u64 bytenr, u64 num_bytes) |
| 1405 | { |
| 1406 | int ret; |
| 1407 | struct btrfs_ordered_sum *sums; |
| 1408 | LIST_HEAD(list); |
| 1409 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1410 | ret = btrfs_lookup_csums_range(fs_info->csum_root, bytenr, |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 1411 | bytenr + num_bytes - 1, &list, 0); |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1412 | if (ret == 0 && list_empty(&list)) |
| 1413 | return 0; |
| 1414 | |
| 1415 | while (!list_empty(&list)) { |
| 1416 | sums = list_entry(list.next, struct btrfs_ordered_sum, list); |
| 1417 | list_del(&sums->list); |
| 1418 | kfree(sums); |
| 1419 | } |
Liu Bo | 5811375 | 2018-01-31 17:09:13 -0700 | [diff] [blame] | 1420 | if (ret < 0) |
| 1421 | return ret; |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1422 | return 1; |
| 1423 | } |
| 1424 | |
Nikolay Borisov | 8ba96f3 | 2020-06-03 08:55:20 +0300 | [diff] [blame] | 1425 | 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] | 1426 | const u64 start, const u64 end, |
| 1427 | int *page_started, unsigned long *nr_written) |
| 1428 | { |
Nikolay Borisov | 8ba96f3 | 2020-06-03 08:55:20 +0300 | [diff] [blame] | 1429 | const bool is_space_ino = btrfs_is_free_space_inode(inode); |
| 1430 | const bool is_reloc_ino = (inode->root->root_key.objectid == |
Filipe Manana | 6bd335b | 2020-06-08 13:33:05 +0100 | [diff] [blame] | 1431 | BTRFS_DATA_RELOC_TREE_OBJECTID); |
Filipe Manana | 2166e5e | 2020-05-27 11:16:19 +0100 | [diff] [blame] | 1432 | const u64 range_bytes = end + 1 - start; |
Nikolay Borisov | 8ba96f3 | 2020-06-03 08:55:20 +0300 | [diff] [blame] | 1433 | struct extent_io_tree *io_tree = &inode->io_tree; |
Filipe Manana | 467dc47 | 2020-05-27 11:16:07 +0100 | [diff] [blame] | 1434 | u64 range_start = start; |
| 1435 | u64 count; |
| 1436 | |
| 1437 | /* |
| 1438 | * If EXTENT_NORESERVE is set it means that when the buffered write was |
| 1439 | * made we had not enough available data space and therefore we did not |
| 1440 | * reserve data space for it, since we though we could do NOCOW for the |
| 1441 | * respective file range (either there is prealloc extent or the inode |
| 1442 | * has the NOCOW bit set). |
| 1443 | * |
| 1444 | * However when we need to fallback to COW mode (because for example the |
| 1445 | * block group for the corresponding extent was turned to RO mode by a |
| 1446 | * scrub or relocation) we need to do the following: |
| 1447 | * |
| 1448 | * 1) We increment the bytes_may_use counter of the data space info. |
| 1449 | * If COW succeeds, it allocates a new data extent and after doing |
| 1450 | * that it decrements the space info's bytes_may_use counter and |
| 1451 | * increments its bytes_reserved counter by the same amount (we do |
| 1452 | * this at btrfs_add_reserved_bytes()). So we need to increment the |
| 1453 | * bytes_may_use counter to compensate (when space is reserved at |
| 1454 | * buffered write time, the bytes_may_use counter is incremented); |
| 1455 | * |
| 1456 | * 2) We clear the EXTENT_NORESERVE bit from the range. We do this so |
| 1457 | * that if the COW path fails for any reason, it decrements (through |
| 1458 | * extent_clear_unlock_delalloc()) the bytes_may_use counter of the |
| 1459 | * data space info, which we incremented in the step above. |
Filipe Manana | 2166e5e | 2020-05-27 11:16:19 +0100 | [diff] [blame] | 1460 | * |
| 1461 | * 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] | 1462 | * space cache inode or an inode of the data relocation tree, we must |
| 1463 | * also increment bytes_may_use of the data space_info for the same |
| 1464 | * reason. Space caches and relocated data extents always get a prealloc |
Filipe Manana | 2166e5e | 2020-05-27 11:16:19 +0100 | [diff] [blame] | 1465 | * extent for them, however scrub or balance may have set the block |
Filipe Manana | 6bd335b | 2020-06-08 13:33:05 +0100 | [diff] [blame] | 1466 | * group that contains that extent to RO mode and therefore force COW |
| 1467 | * when starting writeback. |
Filipe Manana | 467dc47 | 2020-05-27 11:16:07 +0100 | [diff] [blame] | 1468 | */ |
Filipe Manana | 2166e5e | 2020-05-27 11:16:19 +0100 | [diff] [blame] | 1469 | count = count_range_bits(io_tree, &range_start, end, range_bytes, |
Filipe Manana | 467dc47 | 2020-05-27 11:16:07 +0100 | [diff] [blame] | 1470 | EXTENT_NORESERVE, 0); |
Filipe Manana | 6bd335b | 2020-06-08 13:33:05 +0100 | [diff] [blame] | 1471 | if (count > 0 || is_space_ino || is_reloc_ino) { |
| 1472 | u64 bytes = count; |
Nikolay Borisov | 8ba96f3 | 2020-06-03 08:55:20 +0300 | [diff] [blame] | 1473 | struct btrfs_fs_info *fs_info = inode->root->fs_info; |
Filipe Manana | 467dc47 | 2020-05-27 11:16:07 +0100 | [diff] [blame] | 1474 | struct btrfs_space_info *sinfo = fs_info->data_sinfo; |
| 1475 | |
Filipe Manana | 6bd335b | 2020-06-08 13:33:05 +0100 | [diff] [blame] | 1476 | if (is_space_ino || is_reloc_ino) |
| 1477 | bytes = range_bytes; |
| 1478 | |
Filipe Manana | 467dc47 | 2020-05-27 11:16:07 +0100 | [diff] [blame] | 1479 | spin_lock(&sinfo->lock); |
Filipe Manana | 2166e5e | 2020-05-27 11:16:19 +0100 | [diff] [blame] | 1480 | btrfs_space_info_update_bytes_may_use(fs_info, sinfo, bytes); |
Filipe Manana | 467dc47 | 2020-05-27 11:16:07 +0100 | [diff] [blame] | 1481 | spin_unlock(&sinfo->lock); |
| 1482 | |
Filipe Manana | 2166e5e | 2020-05-27 11:16:19 +0100 | [diff] [blame] | 1483 | if (count > 0) |
| 1484 | clear_extent_bit(io_tree, start, end, EXTENT_NORESERVE, |
| 1485 | 0, 0, NULL); |
Filipe Manana | 467dc47 | 2020-05-27 11:16:07 +0100 | [diff] [blame] | 1486 | } |
| 1487 | |
Nikolay Borisov | 8ba96f3 | 2020-06-03 08:55:20 +0300 | [diff] [blame] | 1488 | return cow_file_range(inode, locked_page, start, end, page_started, |
| 1489 | nr_written, 1); |
Filipe Manana | 467dc47 | 2020-05-27 11:16:07 +0100 | [diff] [blame] | 1490 | } |
| 1491 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1492 | /* |
| 1493 | * when nowcow writeback call back. This checks for snapshots or COW copies |
| 1494 | * of the extents that exist in the file, and COWs the file as required. |
| 1495 | * |
| 1496 | * If no cow copies or snapshots exist, we write directly to the existing |
| 1497 | * blocks on disk |
| 1498 | */ |
Nikolay Borisov | 968322c | 2020-06-03 08:55:21 +0300 | [diff] [blame] | 1499 | static noinline int run_delalloc_nocow(struct btrfs_inode *inode, |
Chris Mason | 7f366cf | 2009-03-12 20:12:45 -0400 | [diff] [blame] | 1500 | struct page *locked_page, |
Nikolay Borisov | 3e02484 | 2019-08-21 10:42:03 +0300 | [diff] [blame] | 1501 | const u64 start, const u64 end, |
| 1502 | int *page_started, int force, |
| 1503 | unsigned long *nr_written) |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1504 | { |
Nikolay Borisov | 968322c | 2020-06-03 08:55:21 +0300 | [diff] [blame] | 1505 | struct btrfs_fs_info *fs_info = inode->root->fs_info; |
| 1506 | struct btrfs_root *root = inode->root; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1507 | struct btrfs_path *path; |
Nikolay Borisov | 3e02484 | 2019-08-21 10:42:03 +0300 | [diff] [blame] | 1508 | u64 cow_start = (u64)-1; |
| 1509 | u64 cur_offset = start; |
Robbie Ko | 8ecebf4d | 2018-08-06 10:30:30 +0800 | [diff] [blame] | 1510 | int ret; |
Nikolay Borisov | 3e02484 | 2019-08-21 10:42:03 +0300 | [diff] [blame] | 1511 | bool check_prev = true; |
Nikolay Borisov | 968322c | 2020-06-03 08:55:21 +0300 | [diff] [blame] | 1512 | const bool freespace_inode = btrfs_is_free_space_inode(inode); |
| 1513 | u64 ino = btrfs_ino(inode); |
Nikolay Borisov | 762bf09 | 2019-08-22 17:24:20 +0300 | [diff] [blame] | 1514 | bool nocow = false; |
| 1515 | u64 disk_bytenr = 0; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1516 | |
| 1517 | path = btrfs_alloc_path(); |
Josef Bacik | 17ca04a | 2012-05-31 15:58:55 -0400 | [diff] [blame] | 1518 | if (!path) { |
Nikolay Borisov | 968322c | 2020-06-03 08:55:21 +0300 | [diff] [blame] | 1519 | extent_clear_unlock_delalloc(inode, start, end, locked_page, |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1520 | EXTENT_LOCKED | EXTENT_DELALLOC | |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 1521 | EXTENT_DO_ACCOUNTING | |
| 1522 | EXTENT_DEFRAG, PAGE_UNLOCK | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1523 | PAGE_CLEAR_DIRTY | |
| 1524 | PAGE_SET_WRITEBACK | |
| 1525 | PAGE_END_WRITEBACK); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 1526 | return -ENOMEM; |
Josef Bacik | 17ca04a | 2012-05-31 15:58:55 -0400 | [diff] [blame] | 1527 | } |
Li Zefan | 82d5902 | 2011-04-20 10:33:24 +0800 | [diff] [blame] | 1528 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1529 | while (1) { |
Nikolay Borisov | 3e02484 | 2019-08-21 10:42:03 +0300 | [diff] [blame] | 1530 | struct btrfs_key found_key; |
| 1531 | struct btrfs_file_extent_item *fi; |
| 1532 | struct extent_buffer *leaf; |
| 1533 | u64 extent_end; |
| 1534 | u64 extent_offset; |
Nikolay Borisov | 3e02484 | 2019-08-21 10:42:03 +0300 | [diff] [blame] | 1535 | u64 num_bytes = 0; |
| 1536 | u64 disk_num_bytes; |
Nikolay Borisov | 3e02484 | 2019-08-21 10:42:03 +0300 | [diff] [blame] | 1537 | u64 ram_bytes; |
| 1538 | int extent_type; |
Nikolay Borisov | 762bf09 | 2019-08-22 17:24:20 +0300 | [diff] [blame] | 1539 | |
| 1540 | nocow = false; |
Nikolay Borisov | 3e02484 | 2019-08-21 10:42:03 +0300 | [diff] [blame] | 1541 | |
Liu Bo | e4c3b2d | 2017-01-30 12:25:28 -0800 | [diff] [blame] | 1542 | ret = btrfs_lookup_file_extent(NULL, root, path, ino, |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1543 | cur_offset, 0); |
Josef Bacik | d788a34 | 2013-10-25 16:55:08 -0400 | [diff] [blame] | 1544 | if (ret < 0) |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1545 | goto error; |
Nikolay Borisov | a6bd9cd | 2019-08-21 10:42:57 +0300 | [diff] [blame] | 1546 | |
| 1547 | /* |
| 1548 | * If there is no extent for our range when doing the initial |
| 1549 | * search, then go back to the previous slot as it will be the |
| 1550 | * one containing the search offset |
| 1551 | */ |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1552 | if (ret > 0 && path->slots[0] > 0 && check_prev) { |
| 1553 | leaf = path->nodes[0]; |
| 1554 | btrfs_item_key_to_cpu(leaf, &found_key, |
| 1555 | path->slots[0] - 1); |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 1556 | if (found_key.objectid == ino && |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1557 | found_key.type == BTRFS_EXTENT_DATA_KEY) |
| 1558 | path->slots[0]--; |
| 1559 | } |
Nikolay Borisov | 3e02484 | 2019-08-21 10:42:03 +0300 | [diff] [blame] | 1560 | check_prev = false; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1561 | next_slot: |
Nikolay Borisov | a6bd9cd | 2019-08-21 10:42:57 +0300 | [diff] [blame] | 1562 | /* Go to next leaf if we have exhausted the current one */ |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1563 | leaf = path->nodes[0]; |
| 1564 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { |
| 1565 | ret = btrfs_next_leaf(root, path); |
Liu Bo | e891669 | 2018-01-25 11:02:50 -0700 | [diff] [blame] | 1566 | if (ret < 0) { |
| 1567 | if (cow_start != (u64)-1) |
| 1568 | cur_offset = cow_start; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1569 | goto error; |
Liu Bo | e891669 | 2018-01-25 11:02:50 -0700 | [diff] [blame] | 1570 | } |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1571 | if (ret > 0) |
| 1572 | break; |
| 1573 | leaf = path->nodes[0]; |
| 1574 | } |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1575 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1576 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1577 | |
Nikolay Borisov | a6bd9cd | 2019-08-21 10:42:57 +0300 | [diff] [blame] | 1578 | /* Didn't find anything for our INO */ |
Filipe Manana | 1d512cb | 2015-11-09 00:33:58 +0000 | [diff] [blame] | 1579 | if (found_key.objectid > ino) |
| 1580 | break; |
Nikolay Borisov | a6bd9cd | 2019-08-21 10:42:57 +0300 | [diff] [blame] | 1581 | /* |
| 1582 | * Keep searching until we find an EXTENT_ITEM or there are no |
| 1583 | * more extents for this inode |
| 1584 | */ |
Filipe Manana | 1d512cb | 2015-11-09 00:33:58 +0000 | [diff] [blame] | 1585 | if (WARN_ON_ONCE(found_key.objectid < ino) || |
| 1586 | found_key.type < BTRFS_EXTENT_DATA_KEY) { |
| 1587 | path->slots[0]++; |
| 1588 | goto next_slot; |
| 1589 | } |
Nikolay Borisov | a6bd9cd | 2019-08-21 10:42:57 +0300 | [diff] [blame] | 1590 | |
| 1591 | /* Found key is not EXTENT_DATA_KEY or starts after req range */ |
Filipe Manana | 1d512cb | 2015-11-09 00:33:58 +0000 | [diff] [blame] | 1592 | if (found_key.type > BTRFS_EXTENT_DATA_KEY || |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1593 | found_key.offset > end) |
| 1594 | break; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1595 | |
Nikolay Borisov | a6bd9cd | 2019-08-21 10:42:57 +0300 | [diff] [blame] | 1596 | /* |
| 1597 | * If the found extent starts after requested offset, then |
| 1598 | * adjust extent_end to be right before this extent begins |
| 1599 | */ |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1600 | if (found_key.offset > cur_offset) { |
| 1601 | extent_end = found_key.offset; |
Chris Mason | e9061e2 | 2009-10-09 09:57:45 -0400 | [diff] [blame] | 1602 | extent_type = 0; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1603 | goto out_check; |
| 1604 | } |
Chris Mason | c31f883 | 2008-01-08 15:46:31 -0500 | [diff] [blame] | 1605 | |
Nikolay Borisov | a6bd9cd | 2019-08-21 10:42:57 +0300 | [diff] [blame] | 1606 | /* |
| 1607 | * Found extent which begins before our range and potentially |
| 1608 | * intersect it |
| 1609 | */ |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1610 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 1611 | struct btrfs_file_extent_item); |
| 1612 | extent_type = btrfs_file_extent_type(leaf, fi); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1613 | |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 1614 | ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1615 | if (extent_type == BTRFS_FILE_EXTENT_REG || |
| 1616 | extent_type == BTRFS_FILE_EXTENT_PREALLOC) { |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1617 | disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1618 | extent_offset = btrfs_file_extent_offset(leaf, fi); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1619 | extent_end = found_key.offset + |
| 1620 | btrfs_file_extent_num_bytes(leaf, fi); |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 1621 | disk_num_bytes = |
| 1622 | btrfs_file_extent_disk_num_bytes(leaf, fi); |
Nikolay Borisov | a6bd9cd | 2019-08-21 10:42:57 +0300 | [diff] [blame] | 1623 | /* |
Filipe Manana | de7999a | 2019-12-11 09:01:40 +0000 | [diff] [blame] | 1624 | * If the extent we got ends before our current offset, |
| 1625 | * skip to the next extent. |
Nikolay Borisov | a6bd9cd | 2019-08-21 10:42:57 +0300 | [diff] [blame] | 1626 | */ |
Filipe Manana | de7999a | 2019-12-11 09:01:40 +0000 | [diff] [blame] | 1627 | if (extent_end <= cur_offset) { |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1628 | path->slots[0]++; |
| 1629 | goto next_slot; |
| 1630 | } |
Nikolay Borisov | a6bd9cd | 2019-08-21 10:42:57 +0300 | [diff] [blame] | 1631 | /* Skip holes */ |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1632 | if (disk_bytenr == 0) |
| 1633 | goto out_check; |
Nikolay Borisov | a6bd9cd | 2019-08-21 10:42:57 +0300 | [diff] [blame] | 1634 | /* Skip compressed/encrypted/encoded extents */ |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1635 | if (btrfs_file_extent_compression(leaf, fi) || |
| 1636 | btrfs_file_extent_encryption(leaf, fi) || |
| 1637 | btrfs_file_extent_other_encoding(leaf, fi)) |
| 1638 | goto out_check; |
Ethan Lien | 78d4295 | 2018-05-17 14:58:29 +0800 | [diff] [blame] | 1639 | /* |
Nikolay Borisov | a6bd9cd | 2019-08-21 10:42:57 +0300 | [diff] [blame] | 1640 | * If extent is created before the last volume's snapshot |
| 1641 | * this implies the extent is shared, hence we can't do |
| 1642 | * nocow. This is the same check as in |
| 1643 | * btrfs_cross_ref_exist but without calling |
| 1644 | * btrfs_search_slot. |
Ethan Lien | 78d4295 | 2018-05-17 14:58:29 +0800 | [diff] [blame] | 1645 | */ |
Nikolay Borisov | 3e02484 | 2019-08-21 10:42:03 +0300 | [diff] [blame] | 1646 | if (!freespace_inode && |
Lu Fengqi | 27a7ff5 | 2018-11-29 17:31:32 +0800 | [diff] [blame] | 1647 | btrfs_file_extent_generation(leaf, fi) <= |
Ethan Lien | 78d4295 | 2018-05-17 14:58:29 +0800 | [diff] [blame] | 1648 | btrfs_root_last_snapshot(&root->root_item)) |
| 1649 | goto out_check; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1650 | if (extent_type == BTRFS_FILE_EXTENT_REG && !force) |
| 1651 | goto out_check; |
Filipe Manana | c65ca98 | 2020-11-18 11:00:17 +0000 | [diff] [blame^] | 1652 | |
| 1653 | /* |
| 1654 | * The following checks can be expensive, as they need to |
| 1655 | * take other locks and do btree or rbtree searches, so |
| 1656 | * release the path to avoid blocking other tasks for too |
| 1657 | * long. |
| 1658 | */ |
| 1659 | btrfs_release_path(path); |
| 1660 | |
Nikolay Borisov | a6bd9cd | 2019-08-21 10:42:57 +0300 | [diff] [blame] | 1661 | /* If extent is RO, we must COW it */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 1662 | if (btrfs_extent_readonly(fs_info, disk_bytenr)) |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1663 | goto out_check; |
Liu Bo | 5811375 | 2018-01-31 17:09:13 -0700 | [diff] [blame] | 1664 | ret = btrfs_cross_ref_exist(root, ino, |
| 1665 | found_key.offset - |
Boris Burkov | a84d5d4 | 2020-08-18 11:00:05 -0700 | [diff] [blame] | 1666 | extent_offset, disk_bytenr, false); |
Liu Bo | 5811375 | 2018-01-31 17:09:13 -0700 | [diff] [blame] | 1667 | if (ret) { |
| 1668 | /* |
| 1669 | * ret could be -EIO if the above fails to read |
| 1670 | * metadata. |
| 1671 | */ |
| 1672 | if (ret < 0) { |
| 1673 | if (cow_start != (u64)-1) |
| 1674 | cur_offset = cow_start; |
| 1675 | goto error; |
| 1676 | } |
| 1677 | |
Nikolay Borisov | 3e02484 | 2019-08-21 10:42:03 +0300 | [diff] [blame] | 1678 | WARN_ON_ONCE(freespace_inode); |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1679 | goto out_check; |
Liu Bo | 5811375 | 2018-01-31 17:09:13 -0700 | [diff] [blame] | 1680 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1681 | disk_bytenr += extent_offset; |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1682 | disk_bytenr += cur_offset - found_key.offset; |
| 1683 | num_bytes = min(end + 1, extent_end) - cur_offset; |
| 1684 | /* |
Nikolay Borisov | a6bd9cd | 2019-08-21 10:42:57 +0300 | [diff] [blame] | 1685 | * If there are pending snapshots for this root, we |
| 1686 | * fall into common COW way |
Wang Shilong | e9894fd | 2014-03-27 11:12:25 +0800 | [diff] [blame] | 1687 | */ |
Nikolay Borisov | 3e02484 | 2019-08-21 10:42:03 +0300 | [diff] [blame] | 1688 | if (!freespace_inode && atomic_read(&root->snapshot_force_cow)) |
Robbie Ko | 8ecebf4d | 2018-08-06 10:30:30 +0800 | [diff] [blame] | 1689 | goto out_check; |
Wang Shilong | e9894fd | 2014-03-27 11:12:25 +0800 | [diff] [blame] | 1690 | /* |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1691 | * force cow if csum exists in the range. |
| 1692 | * this ensure that csum for a given extent are |
| 1693 | * either valid or do not exist. |
| 1694 | */ |
Liu Bo | 5811375 | 2018-01-31 17:09:13 -0700 | [diff] [blame] | 1695 | ret = csum_exist_in_range(fs_info, disk_bytenr, |
| 1696 | num_bytes); |
| 1697 | if (ret) { |
Liu Bo | 5811375 | 2018-01-31 17:09:13 -0700 | [diff] [blame] | 1698 | /* |
| 1699 | * ret could be -EIO if the above fails to read |
| 1700 | * metadata. |
| 1701 | */ |
| 1702 | if (ret < 0) { |
| 1703 | if (cow_start != (u64)-1) |
| 1704 | cur_offset = cow_start; |
| 1705 | goto error; |
| 1706 | } |
Nikolay Borisov | 3e02484 | 2019-08-21 10:42:03 +0300 | [diff] [blame] | 1707 | WARN_ON_ONCE(freespace_inode); |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1708 | goto out_check; |
Robbie Ko | 91e1f56 | 2016-10-07 10:01:29 +0800 | [diff] [blame] | 1709 | } |
Robbie Ko | 8ecebf4d | 2018-08-06 10:30:30 +0800 | [diff] [blame] | 1710 | if (!btrfs_inc_nocow_writers(fs_info, disk_bytenr)) |
Filipe Manana | f78c436 | 2016-05-09 13:15:41 +0100 | [diff] [blame] | 1711 | goto out_check; |
Nikolay Borisov | 3e02484 | 2019-08-21 10:42:03 +0300 | [diff] [blame] | 1712 | nocow = true; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1713 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
Nikolay Borisov | e8e2100 | 2019-08-22 17:25:23 +0300 | [diff] [blame] | 1714 | extent_end = found_key.offset + ram_bytes; |
| 1715 | extent_end = ALIGN(extent_end, fs_info->sectorsize); |
Nikolay Borisov | 922f051 | 2019-08-05 17:47:06 +0300 | [diff] [blame] | 1716 | /* Skip extents outside of our requested range */ |
| 1717 | if (extent_end <= start) { |
| 1718 | path->slots[0]++; |
| 1719 | goto next_slot; |
| 1720 | } |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1721 | } else { |
Nikolay Borisov | e8e2100 | 2019-08-22 17:25:23 +0300 | [diff] [blame] | 1722 | /* If this triggers then we have a memory corruption */ |
Arnd Bergmann | 290342f | 2019-03-25 14:02:25 +0100 | [diff] [blame] | 1723 | BUG(); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1724 | } |
| 1725 | out_check: |
Nikolay Borisov | a6bd9cd | 2019-08-21 10:42:57 +0300 | [diff] [blame] | 1726 | /* |
| 1727 | * If nocow is false then record the beginning of the range |
| 1728 | * that needs to be COWed |
| 1729 | */ |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1730 | if (!nocow) { |
| 1731 | if (cow_start == (u64)-1) |
| 1732 | cow_start = cur_offset; |
| 1733 | cur_offset = extent_end; |
| 1734 | if (cur_offset > end) |
| 1735 | break; |
Filipe Manana | c65ca98 | 2020-11-18 11:00:17 +0000 | [diff] [blame^] | 1736 | if (!path->nodes[0]) |
| 1737 | continue; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1738 | path->slots[0]++; |
| 1739 | goto next_slot; |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1740 | } |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1741 | |
Nikolay Borisov | a6bd9cd | 2019-08-21 10:42:57 +0300 | [diff] [blame] | 1742 | /* |
| 1743 | * COW range from cow_start to found_key.offset - 1. As the key |
| 1744 | * will contain the beginning of the first extent that can be |
| 1745 | * NOCOW, following one which needs to be COW'ed |
| 1746 | */ |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1747 | if (cow_start != (u64)-1) { |
Nikolay Borisov | 968322c | 2020-06-03 08:55:21 +0300 | [diff] [blame] | 1748 | ret = fallback_to_cow(inode, locked_page, |
Nikolay Borisov | 8ba96f3 | 2020-06-03 08:55:20 +0300 | [diff] [blame] | 1749 | cow_start, found_key.offset - 1, |
Filipe Manana | 467dc47 | 2020-05-27 11:16:07 +0100 | [diff] [blame] | 1750 | page_started, nr_written); |
Josef Bacik | 230ed39 | 2020-07-06 09:14:12 -0400 | [diff] [blame] | 1751 | if (ret) |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1752 | goto error; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1753 | cow_start = (u64)-1; |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1754 | } |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1755 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1756 | if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) { |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 1757 | u64 orig_start = found_key.offset - extent_offset; |
Nikolay Borisov | 3e02484 | 2019-08-21 10:42:03 +0300 | [diff] [blame] | 1758 | struct extent_map *em; |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 1759 | |
Nikolay Borisov | 968322c | 2020-06-03 08:55:21 +0300 | [diff] [blame] | 1760 | em = create_io_em(inode, cur_offset, num_bytes, |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 1761 | orig_start, |
| 1762 | disk_bytenr, /* block_start */ |
| 1763 | num_bytes, /* block_len */ |
| 1764 | disk_num_bytes, /* orig_block_len */ |
| 1765 | ram_bytes, BTRFS_COMPRESS_NONE, |
| 1766 | BTRFS_ORDERED_PREALLOC); |
| 1767 | if (IS_ERR(em)) { |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 1768 | ret = PTR_ERR(em); |
| 1769 | goto error; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1770 | } |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 1771 | free_extent_map(em); |
Nikolay Borisov | 968322c | 2020-06-03 08:55:21 +0300 | [diff] [blame] | 1772 | ret = btrfs_add_ordered_extent(inode, cur_offset, |
Nikolay Borisov | bb55f62 | 2019-08-05 17:47:05 +0300 | [diff] [blame] | 1773 | disk_bytenr, num_bytes, |
| 1774 | num_bytes, |
| 1775 | BTRFS_ORDERED_PREALLOC); |
Nikolay Borisov | 762bf09 | 2019-08-22 17:24:20 +0300 | [diff] [blame] | 1776 | if (ret) { |
Nikolay Borisov | 968322c | 2020-06-03 08:55:21 +0300 | [diff] [blame] | 1777 | btrfs_drop_extent_cache(inode, cur_offset, |
Nikolay Borisov | 762bf09 | 2019-08-22 17:24:20 +0300 | [diff] [blame] | 1778 | cur_offset + num_bytes - 1, |
| 1779 | 0); |
| 1780 | goto error; |
| 1781 | } |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1782 | } else { |
Nikolay Borisov | 968322c | 2020-06-03 08:55:21 +0300 | [diff] [blame] | 1783 | ret = btrfs_add_ordered_extent(inode, cur_offset, |
Nikolay Borisov | bb55f62 | 2019-08-05 17:47:05 +0300 | [diff] [blame] | 1784 | disk_bytenr, num_bytes, |
| 1785 | num_bytes, |
| 1786 | BTRFS_ORDERED_NOCOW); |
Nikolay Borisov | 762bf09 | 2019-08-22 17:24:20 +0300 | [diff] [blame] | 1787 | if (ret) |
| 1788 | goto error; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1789 | } |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1790 | |
Filipe Manana | f78c436 | 2016-05-09 13:15:41 +0100 | [diff] [blame] | 1791 | if (nocow) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1792 | btrfs_dec_nocow_writers(fs_info, disk_bytenr); |
Nikolay Borisov | 762bf09 | 2019-08-22 17:24:20 +0300 | [diff] [blame] | 1793 | nocow = false; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1794 | |
Yan, Zheng | efa5646 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 1795 | if (root->root_key.objectid == |
Qu Wenruo | 4dbd80f | 2017-03-08 10:25:51 +0800 | [diff] [blame] | 1796 | BTRFS_DATA_RELOC_TREE_OBJECTID) |
| 1797 | /* |
| 1798 | * Error handled later, as we must prevent |
| 1799 | * extent_clear_unlock_delalloc() in error handler |
| 1800 | * from freeing metadata of created ordered extent. |
| 1801 | */ |
Nikolay Borisov | 968322c | 2020-06-03 08:55:21 +0300 | [diff] [blame] | 1802 | ret = btrfs_reloc_clone_csums(inode, cur_offset, |
Yan, Zheng | efa5646 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 1803 | num_bytes); |
Yan, Zheng | efa5646 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 1804 | |
Nikolay Borisov | 968322c | 2020-06-03 08:55:21 +0300 | [diff] [blame] | 1805 | extent_clear_unlock_delalloc(inode, cur_offset, |
Nikolay Borisov | 74e9194 | 2019-07-17 16:18:16 +0300 | [diff] [blame] | 1806 | cur_offset + num_bytes - 1, |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1807 | locked_page, EXTENT_LOCKED | |
Wang Xiaoguang | 1851309 | 2016-07-25 15:51:40 +0800 | [diff] [blame] | 1808 | EXTENT_DELALLOC | |
| 1809 | EXTENT_CLEAR_DATA_RESV, |
| 1810 | PAGE_UNLOCK | PAGE_SET_PRIVATE2); |
| 1811 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1812 | cur_offset = extent_end; |
Qu Wenruo | 4dbd80f | 2017-03-08 10:25:51 +0800 | [diff] [blame] | 1813 | |
| 1814 | /* |
| 1815 | * btrfs_reloc_clone_csums() error, now we're OK to call error |
| 1816 | * handler, as metadata for created ordered extent will only |
| 1817 | * be freed by btrfs_finish_ordered_io(). |
| 1818 | */ |
| 1819 | if (ret) |
| 1820 | goto error; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1821 | if (cur_offset > end) |
| 1822 | break; |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1823 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1824 | btrfs_release_path(path); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1825 | |
Robbie Ko | 506481b | 2018-10-30 18:04:04 +0800 | [diff] [blame] | 1826 | if (cur_offset <= end && cow_start == (u64)-1) |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1827 | cow_start = cur_offset; |
Josef Bacik | 17ca04a | 2012-05-31 15:58:55 -0400 | [diff] [blame] | 1828 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1829 | if (cow_start != (u64)-1) { |
Robbie Ko | 506481b | 2018-10-30 18:04:04 +0800 | [diff] [blame] | 1830 | cur_offset = end; |
Nikolay Borisov | 968322c | 2020-06-03 08:55:21 +0300 | [diff] [blame] | 1831 | ret = fallback_to_cow(inode, locked_page, cow_start, end, |
| 1832 | page_started, nr_written); |
Josef Bacik | d788a34 | 2013-10-25 16:55:08 -0400 | [diff] [blame] | 1833 | if (ret) |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1834 | goto error; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1835 | } |
| 1836 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1837 | error: |
Nikolay Borisov | 762bf09 | 2019-08-22 17:24:20 +0300 | [diff] [blame] | 1838 | if (nocow) |
| 1839 | btrfs_dec_nocow_writers(fs_info, disk_bytenr); |
| 1840 | |
Josef Bacik | 17ca04a | 2012-05-31 15:58:55 -0400 | [diff] [blame] | 1841 | if (ret && cur_offset < end) |
Nikolay Borisov | 968322c | 2020-06-03 08:55:21 +0300 | [diff] [blame] | 1842 | extent_clear_unlock_delalloc(inode, cur_offset, end, |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1843 | locked_page, EXTENT_LOCKED | |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 1844 | EXTENT_DELALLOC | EXTENT_DEFRAG | |
| 1845 | EXTENT_DO_ACCOUNTING, PAGE_UNLOCK | |
| 1846 | PAGE_CLEAR_DIRTY | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1847 | PAGE_SET_WRITEBACK | |
| 1848 | PAGE_END_WRITEBACK); |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1849 | btrfs_free_path(path); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1850 | return ret; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1851 | } |
| 1852 | |
Nikolay Borisov | 0c49422 | 2020-06-03 08:55:28 +0300 | [diff] [blame] | 1853 | 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] | 1854 | { |
| 1855 | |
Nikolay Borisov | 0c49422 | 2020-06-03 08:55:28 +0300 | [diff] [blame] | 1856 | if (!(inode->flags & BTRFS_INODE_NODATACOW) && |
| 1857 | !(inode->flags & BTRFS_INODE_PREALLOC)) |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 1858 | return 0; |
| 1859 | |
| 1860 | /* |
| 1861 | * @defrag_bytes is a hint value, no spinlock held here, |
| 1862 | * if is not zero, it means the file is defragging. |
| 1863 | * Force cow if given extent needs to be defragged. |
| 1864 | */ |
Nikolay Borisov | 0c49422 | 2020-06-03 08:55:28 +0300 | [diff] [blame] | 1865 | if (inode->defrag_bytes && |
| 1866 | test_range_bit(&inode->io_tree, start, end, EXTENT_DEFRAG, 0, NULL)) |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 1867 | return 1; |
| 1868 | |
| 1869 | return 0; |
| 1870 | } |
| 1871 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1872 | /* |
Nikolay Borisov | 5eaad97 | 2018-11-01 14:09:46 +0200 | [diff] [blame] | 1873 | * Function to process delayed allocation (create CoW) for ranges which are |
| 1874 | * being touched for the first time. |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1875 | */ |
Nikolay Borisov | 98456b9 | 2020-06-03 08:55:29 +0300 | [diff] [blame] | 1876 | 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] | 1877 | u64 start, u64 end, int *page_started, unsigned long *nr_written, |
| 1878 | struct writeback_control *wbc) |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1879 | { |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1880 | int ret; |
Nikolay Borisov | 98456b9 | 2020-06-03 08:55:29 +0300 | [diff] [blame] | 1881 | int force_cow = need_force_cow(inode, start, end); |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1882 | |
Nikolay Borisov | 98456b9 | 2020-06-03 08:55:29 +0300 | [diff] [blame] | 1883 | if (inode->flags & BTRFS_INODE_NODATACOW && !force_cow) { |
| 1884 | ret = run_delalloc_nocow(inode, locked_page, start, end, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1885 | page_started, 1, nr_written); |
Nikolay Borisov | 98456b9 | 2020-06-03 08:55:29 +0300 | [diff] [blame] | 1886 | } else if (inode->flags & BTRFS_INODE_PREALLOC && !force_cow) { |
| 1887 | ret = run_delalloc_nocow(inode, locked_page, start, end, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1888 | page_started, 0, nr_written); |
Nikolay Borisov | 98456b9 | 2020-06-03 08:55:29 +0300 | [diff] [blame] | 1889 | } else if (!inode_can_compress(inode) || |
| 1890 | !inode_need_compress(inode, start, end)) { |
| 1891 | ret = cow_file_range(inode, locked_page, start, end, |
| 1892 | page_started, nr_written, 1); |
Josef Bacik | 7ddf5a4 | 2012-06-08 15:26:47 -0400 | [diff] [blame] | 1893 | } else { |
Nikolay Borisov | 98456b9 | 2020-06-03 08:55:29 +0300 | [diff] [blame] | 1894 | set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, &inode->runtime_flags); |
| 1895 | ret = cow_file_range_async(inode, wbc, locked_page, start, end, |
David Sterba | fac07d2 | 2019-10-29 18:28:57 +0100 | [diff] [blame] | 1896 | page_started, nr_written); |
Josef Bacik | 7ddf5a4 | 2012-06-08 15:26:47 -0400 | [diff] [blame] | 1897 | } |
Qu Wenruo | 52427260 | 2017-03-08 10:25:52 +0800 | [diff] [blame] | 1898 | if (ret) |
Nikolay Borisov | 98456b9 | 2020-06-03 08:55:29 +0300 | [diff] [blame] | 1899 | btrfs_cleanup_ordered_extents(inode, locked_page, start, |
Nikolay Borisov | d1051d6 | 2018-11-21 17:10:52 +0200 | [diff] [blame] | 1900 | end - start + 1); |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 1901 | return ret; |
| 1902 | } |
| 1903 | |
Nikolay Borisov | abbb55f | 2018-11-01 14:09:53 +0200 | [diff] [blame] | 1904 | void btrfs_split_delalloc_extent(struct inode *inode, |
| 1905 | struct extent_state *orig, u64 split) |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1906 | { |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1907 | u64 size; |
| 1908 | |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1909 | /* not delalloc, ignore it */ |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1910 | if (!(orig->state & EXTENT_DELALLOC)) |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 1911 | return; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1912 | |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1913 | size = orig->end - orig->start + 1; |
| 1914 | if (size > BTRFS_MAX_EXTENT_SIZE) { |
David Sterba | 823bb20 | 2017-01-04 11:09:51 +0100 | [diff] [blame] | 1915 | u32 num_extents; |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1916 | u64 new_size; |
| 1917 | |
| 1918 | /* |
Nikolay Borisov | 5c84819 | 2018-11-01 14:09:52 +0200 | [diff] [blame] | 1919 | * See the explanation in btrfs_merge_delalloc_extent, the same |
Josef Bacik | ba11721 | 2015-03-13 15:01:24 -0400 | [diff] [blame] | 1920 | * applies here, just in reverse. |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1921 | */ |
| 1922 | new_size = orig->end - split + 1; |
David Sterba | 823bb20 | 2017-01-04 11:09:51 +0100 | [diff] [blame] | 1923 | num_extents = count_max_extents(new_size); |
Josef Bacik | ba11721 | 2015-03-13 15:01:24 -0400 | [diff] [blame] | 1924 | new_size = split - orig->start; |
David Sterba | 823bb20 | 2017-01-04 11:09:51 +0100 | [diff] [blame] | 1925 | num_extents += count_max_extents(new_size); |
| 1926 | if (count_max_extents(size) >= num_extents) |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1927 | return; |
| 1928 | } |
| 1929 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1930 | spin_lock(&BTRFS_I(inode)->lock); |
Josef Bacik | 8b62f87 | 2017-10-19 14:15:55 -0400 | [diff] [blame] | 1931 | btrfs_mod_outstanding_extents(BTRFS_I(inode), 1); |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1932 | spin_unlock(&BTRFS_I(inode)->lock); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1933 | } |
| 1934 | |
| 1935 | /* |
Nikolay Borisov | 5c84819 | 2018-11-01 14:09:52 +0200 | [diff] [blame] | 1936 | * Handle merged delayed allocation extents so we can keep track of new extents |
| 1937 | * that are just merged onto old extents, such as when we are doing sequential |
| 1938 | * 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] | 1939 | */ |
Nikolay Borisov | 5c84819 | 2018-11-01 14:09:52 +0200 | [diff] [blame] | 1940 | void btrfs_merge_delalloc_extent(struct inode *inode, struct extent_state *new, |
| 1941 | struct extent_state *other) |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1942 | { |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1943 | u64 new_size, old_size; |
David Sterba | 823bb20 | 2017-01-04 11:09:51 +0100 | [diff] [blame] | 1944 | u32 num_extents; |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1945 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1946 | /* not delalloc, ignore it */ |
| 1947 | if (!(other->state & EXTENT_DELALLOC)) |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 1948 | return; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1949 | |
Josef Bacik | 8461a3d | 2015-03-13 15:12:08 -0400 | [diff] [blame] | 1950 | if (new->start > other->start) |
| 1951 | new_size = new->end - other->start + 1; |
| 1952 | else |
| 1953 | new_size = other->end - new->start + 1; |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1954 | |
| 1955 | /* we're not bigger than the max, unreserve the space and go */ |
| 1956 | if (new_size <= BTRFS_MAX_EXTENT_SIZE) { |
| 1957 | spin_lock(&BTRFS_I(inode)->lock); |
Josef Bacik | 8b62f87 | 2017-10-19 14:15:55 -0400 | [diff] [blame] | 1958 | btrfs_mod_outstanding_extents(BTRFS_I(inode), -1); |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1959 | spin_unlock(&BTRFS_I(inode)->lock); |
| 1960 | return; |
| 1961 | } |
| 1962 | |
| 1963 | /* |
Josef Bacik | ba11721 | 2015-03-13 15:01:24 -0400 | [diff] [blame] | 1964 | * We have to add up either side to figure out how many extents were |
| 1965 | * accounted for before we merged into one big extent. If the number of |
| 1966 | * extents we accounted for is <= the amount we need for the new range |
| 1967 | * then we can return, otherwise drop. Think of it like this |
| 1968 | * |
| 1969 | * [ 4k][MAX_SIZE] |
| 1970 | * |
| 1971 | * So we've grown the extent by a MAX_SIZE extent, this would mean we |
| 1972 | * need 2 outstanding extents, on one side we have 1 and the other side |
| 1973 | * we have 1 so they are == and we can return. But in this case |
| 1974 | * |
| 1975 | * [MAX_SIZE+4k][MAX_SIZE+4k] |
| 1976 | * |
| 1977 | * Each range on their own accounts for 2 extents, but merged together |
| 1978 | * they are only 3 extents worth of accounting, so we need to drop in |
| 1979 | * this case. |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1980 | */ |
Josef Bacik | ba11721 | 2015-03-13 15:01:24 -0400 | [diff] [blame] | 1981 | old_size = other->end - other->start + 1; |
David Sterba | 823bb20 | 2017-01-04 11:09:51 +0100 | [diff] [blame] | 1982 | num_extents = count_max_extents(old_size); |
Josef Bacik | ba11721 | 2015-03-13 15:01:24 -0400 | [diff] [blame] | 1983 | old_size = new->end - new->start + 1; |
David Sterba | 823bb20 | 2017-01-04 11:09:51 +0100 | [diff] [blame] | 1984 | num_extents += count_max_extents(old_size); |
| 1985 | if (count_max_extents(new_size) >= num_extents) |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1986 | return; |
| 1987 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1988 | spin_lock(&BTRFS_I(inode)->lock); |
Josef Bacik | 8b62f87 | 2017-10-19 14:15:55 -0400 | [diff] [blame] | 1989 | btrfs_mod_outstanding_extents(BTRFS_I(inode), -1); |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1990 | spin_unlock(&BTRFS_I(inode)->lock); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1991 | } |
| 1992 | |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 1993 | static void btrfs_add_delalloc_inodes(struct btrfs_root *root, |
| 1994 | struct inode *inode) |
| 1995 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1996 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
| 1997 | |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 1998 | spin_lock(&root->delalloc_lock); |
| 1999 | if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) { |
| 2000 | list_add_tail(&BTRFS_I(inode)->delalloc_inodes, |
| 2001 | &root->delalloc_inodes); |
| 2002 | set_bit(BTRFS_INODE_IN_DELALLOC_LIST, |
| 2003 | &BTRFS_I(inode)->runtime_flags); |
| 2004 | root->nr_delalloc_inodes++; |
| 2005 | if (root->nr_delalloc_inodes == 1) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2006 | spin_lock(&fs_info->delalloc_root_lock); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 2007 | BUG_ON(!list_empty(&root->delalloc_root)); |
| 2008 | list_add_tail(&root->delalloc_root, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2009 | &fs_info->delalloc_roots); |
| 2010 | spin_unlock(&fs_info->delalloc_root_lock); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 2011 | } |
| 2012 | } |
| 2013 | spin_unlock(&root->delalloc_lock); |
| 2014 | } |
| 2015 | |
Nikolay Borisov | 2b87733 | 2018-04-27 12:21:51 +0300 | [diff] [blame] | 2016 | |
| 2017 | void __btrfs_del_delalloc_inode(struct btrfs_root *root, |
| 2018 | struct btrfs_inode *inode) |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 2019 | { |
David Sterba | 3ffbd68 | 2018-06-29 10:56:42 +0200 | [diff] [blame] | 2020 | struct btrfs_fs_info *fs_info = root->fs_info; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2021 | |
Nikolay Borisov | 9e3e97f | 2017-02-20 13:51:07 +0200 | [diff] [blame] | 2022 | if (!list_empty(&inode->delalloc_inodes)) { |
| 2023 | list_del_init(&inode->delalloc_inodes); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 2024 | clear_bit(BTRFS_INODE_IN_DELALLOC_LIST, |
Nikolay Borisov | 9e3e97f | 2017-02-20 13:51:07 +0200 | [diff] [blame] | 2025 | &inode->runtime_flags); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 2026 | root->nr_delalloc_inodes--; |
| 2027 | if (!root->nr_delalloc_inodes) { |
Nikolay Borisov | 7c8a0d3 | 2018-04-27 12:21:52 +0300 | [diff] [blame] | 2028 | ASSERT(list_empty(&root->delalloc_inodes)); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2029 | spin_lock(&fs_info->delalloc_root_lock); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 2030 | BUG_ON(list_empty(&root->delalloc_root)); |
| 2031 | list_del_init(&root->delalloc_root); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2032 | spin_unlock(&fs_info->delalloc_root_lock); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 2033 | } |
| 2034 | } |
Nikolay Borisov | 2b87733 | 2018-04-27 12:21:51 +0300 | [diff] [blame] | 2035 | } |
| 2036 | |
| 2037 | static void btrfs_del_delalloc_inode(struct btrfs_root *root, |
| 2038 | struct btrfs_inode *inode) |
| 2039 | { |
| 2040 | spin_lock(&root->delalloc_lock); |
| 2041 | __btrfs_del_delalloc_inode(root, inode); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 2042 | spin_unlock(&root->delalloc_lock); |
| 2043 | } |
| 2044 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2045 | /* |
Nikolay Borisov | e06a1fc | 2018-11-01 14:09:50 +0200 | [diff] [blame] | 2046 | * Properly track delayed allocation bytes in the inode and to maintain the |
| 2047 | * list of inodes that have pending delalloc work to be done. |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2048 | */ |
Nikolay Borisov | e06a1fc | 2018-11-01 14:09:50 +0200 | [diff] [blame] | 2049 | void btrfs_set_delalloc_extent(struct inode *inode, struct extent_state *state, |
| 2050 | unsigned *bits) |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 2051 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2052 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
| 2053 | |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 2054 | if ((*bits & EXTENT_DEFRAG) && !(*bits & EXTENT_DELALLOC)) |
| 2055 | WARN_ON(1); |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 2056 | /* |
| 2057 | * set_bit and clear bit hooks normally require _irqsave/restore |
Sergei Trofimovich | 27160b6 | 2011-05-20 20:20:32 +0000 | [diff] [blame] | 2058 | * but in this case, we are only testing for the DELALLOC |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 2059 | * bit, which is only set or cleared with irqs on |
| 2060 | */ |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 2061 | if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) { |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 2062 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 2063 | u64 len = state->end + 1 - state->start; |
Josef Bacik | 8b62f87 | 2017-10-19 14:15:55 -0400 | [diff] [blame] | 2064 | u32 num_extents = count_max_extents(len); |
Nikolay Borisov | 70ddc55 | 2017-02-20 13:50:35 +0200 | [diff] [blame] | 2065 | bool do_list = !btrfs_is_free_space_inode(BTRFS_I(inode)); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 2066 | |
Josef Bacik | 8b62f87 | 2017-10-19 14:15:55 -0400 | [diff] [blame] | 2067 | spin_lock(&BTRFS_I(inode)->lock); |
| 2068 | btrfs_mod_outstanding_extents(BTRFS_I(inode), num_extents); |
| 2069 | spin_unlock(&BTRFS_I(inode)->lock); |
Josef Bacik | 287a0ab | 2010-03-19 18:07:23 +0000 | [diff] [blame] | 2070 | |
Josef Bacik | 6a3891c | 2015-03-16 17:38:52 -0400 | [diff] [blame] | 2071 | /* For sanity tests */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2072 | if (btrfs_is_testing(fs_info)) |
Josef Bacik | 6a3891c | 2015-03-16 17:38:52 -0400 | [diff] [blame] | 2073 | return; |
| 2074 | |
Nikolay Borisov | 104b4e5 | 2017-06-20 21:01:20 +0300 | [diff] [blame] | 2075 | percpu_counter_add_batch(&fs_info->delalloc_bytes, len, |
| 2076 | fs_info->delalloc_batch); |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 2077 | spin_lock(&BTRFS_I(inode)->lock); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 2078 | BTRFS_I(inode)->delalloc_bytes += len; |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 2079 | if (*bits & EXTENT_DEFRAG) |
| 2080 | BTRFS_I(inode)->defrag_bytes += len; |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 2081 | if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST, |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 2082 | &BTRFS_I(inode)->runtime_flags)) |
| 2083 | btrfs_add_delalloc_inodes(root, inode); |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 2084 | spin_unlock(&BTRFS_I(inode)->lock); |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 2085 | } |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 2086 | |
| 2087 | if (!(state->state & EXTENT_DELALLOC_NEW) && |
| 2088 | (*bits & EXTENT_DELALLOC_NEW)) { |
| 2089 | spin_lock(&BTRFS_I(inode)->lock); |
| 2090 | BTRFS_I(inode)->new_delalloc_bytes += state->end + 1 - |
| 2091 | state->start; |
| 2092 | spin_unlock(&BTRFS_I(inode)->lock); |
| 2093 | } |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 2094 | } |
| 2095 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2096 | /* |
Nikolay Borisov | a36bb5f | 2018-11-01 14:09:51 +0200 | [diff] [blame] | 2097 | * Once a range is no longer delalloc this function ensures that proper |
| 2098 | * accounting happens. |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2099 | */ |
Nikolay Borisov | a36bb5f | 2018-11-01 14:09:51 +0200 | [diff] [blame] | 2100 | void btrfs_clear_delalloc_extent(struct inode *vfs_inode, |
| 2101 | struct extent_state *state, unsigned *bits) |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 2102 | { |
Nikolay Borisov | a36bb5f | 2018-11-01 14:09:51 +0200 | [diff] [blame] | 2103 | struct btrfs_inode *inode = BTRFS_I(vfs_inode); |
| 2104 | struct btrfs_fs_info *fs_info = btrfs_sb(vfs_inode->i_sb); |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 2105 | u64 len = state->end + 1 - state->start; |
David Sterba | 823bb20 | 2017-01-04 11:09:51 +0100 | [diff] [blame] | 2106 | u32 num_extents = count_max_extents(len); |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 2107 | |
Filipe Manana | 4a4b964 | 2017-07-27 19:52:55 +0100 | [diff] [blame] | 2108 | if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG)) { |
| 2109 | spin_lock(&inode->lock); |
Nikolay Borisov | 6fc0ef6 | 2017-02-20 13:51:03 +0200 | [diff] [blame] | 2110 | inode->defrag_bytes -= len; |
Filipe Manana | 4a4b964 | 2017-07-27 19:52:55 +0100 | [diff] [blame] | 2111 | spin_unlock(&inode->lock); |
| 2112 | } |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 2113 | |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 2114 | /* |
| 2115 | * set_bit and clear bit hooks normally require _irqsave/restore |
Sergei Trofimovich | 27160b6 | 2011-05-20 20:20:32 +0000 | [diff] [blame] | 2116 | * but in this case, we are only testing for the DELALLOC |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 2117 | * bit, which is only set or cleared with irqs on |
| 2118 | */ |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 2119 | if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) { |
Nikolay Borisov | 6fc0ef6 | 2017-02-20 13:51:03 +0200 | [diff] [blame] | 2120 | struct btrfs_root *root = inode->root; |
Liu Bo | 83eea1f | 2012-07-10 05:28:39 -0600 | [diff] [blame] | 2121 | bool do_list = !btrfs_is_free_space_inode(inode); |
Chris Mason | bcbfce8 | 2008-04-22 13:26:47 -0400 | [diff] [blame] | 2122 | |
Josef Bacik | 8b62f87 | 2017-10-19 14:15:55 -0400 | [diff] [blame] | 2123 | spin_lock(&inode->lock); |
| 2124 | btrfs_mod_outstanding_extents(inode, -num_extents); |
| 2125 | spin_unlock(&inode->lock); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 2126 | |
Josef Bacik | b6d08f0 | 2013-09-27 14:57:43 -0400 | [diff] [blame] | 2127 | /* |
| 2128 | * We don't reserve metadata space for space cache inodes so we |
Andrea Gelmini | 52042d8 | 2018-11-28 12:05:13 +0100 | [diff] [blame] | 2129 | * don't need to call delalloc_release_metadata if there is an |
Josef Bacik | b6d08f0 | 2013-09-27 14:57:43 -0400 | [diff] [blame] | 2130 | * error. |
| 2131 | */ |
Filipe Manana | a315e68 | 2017-03-06 23:04:20 +0000 | [diff] [blame] | 2132 | if (*bits & EXTENT_CLEAR_META_RESV && |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2133 | root != fs_info->tree_root) |
Qu Wenruo | 43b1859 | 2017-12-12 15:34:32 +0800 | [diff] [blame] | 2134 | btrfs_delalloc_release_metadata(inode, len, false); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 2135 | |
Josef Bacik | 6a3891c | 2015-03-16 17:38:52 -0400 | [diff] [blame] | 2136 | /* For sanity tests. */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2137 | if (btrfs_is_testing(fs_info)) |
Josef Bacik | 6a3891c | 2015-03-16 17:38:52 -0400 | [diff] [blame] | 2138 | return; |
| 2139 | |
Filipe Manana | a315e68 | 2017-03-06 23:04:20 +0000 | [diff] [blame] | 2140 | if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID && |
| 2141 | do_list && !(state->state & EXTENT_NORESERVE) && |
| 2142 | (*bits & EXTENT_CLEAR_DATA_RESV)) |
Nikolay Borisov | 9db5d51 | 2020-06-03 08:55:38 +0300 | [diff] [blame] | 2143 | btrfs_free_reserved_data_space_noquota(fs_info, len); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 2144 | |
Nikolay Borisov | 104b4e5 | 2017-06-20 21:01:20 +0300 | [diff] [blame] | 2145 | percpu_counter_add_batch(&fs_info->delalloc_bytes, -len, |
| 2146 | fs_info->delalloc_batch); |
Nikolay Borisov | 6fc0ef6 | 2017-02-20 13:51:03 +0200 | [diff] [blame] | 2147 | spin_lock(&inode->lock); |
| 2148 | inode->delalloc_bytes -= len; |
| 2149 | if (do_list && inode->delalloc_bytes == 0 && |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 2150 | test_bit(BTRFS_INODE_IN_DELALLOC_LIST, |
Nikolay Borisov | 9e3e97f | 2017-02-20 13:51:07 +0200 | [diff] [blame] | 2151 | &inode->runtime_flags)) |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 2152 | btrfs_del_delalloc_inode(root, inode); |
Nikolay Borisov | 6fc0ef6 | 2017-02-20 13:51:03 +0200 | [diff] [blame] | 2153 | spin_unlock(&inode->lock); |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 2154 | } |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 2155 | |
| 2156 | if ((state->state & EXTENT_DELALLOC_NEW) && |
| 2157 | (*bits & EXTENT_DELALLOC_NEW)) { |
| 2158 | spin_lock(&inode->lock); |
| 2159 | ASSERT(inode->new_delalloc_bytes >= len); |
| 2160 | inode->new_delalloc_bytes -= len; |
Filipe Manana | 2766ff6 | 2020-11-04 11:07:34 +0000 | [diff] [blame] | 2161 | if (*bits & EXTENT_ADD_INODE_BYTES) |
| 2162 | inode_add_bytes(&inode->vfs_inode, len); |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 2163 | spin_unlock(&inode->lock); |
| 2164 | } |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 2165 | } |
| 2166 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2167 | /* |
Nikolay Borisov | da12fe5 | 2018-11-27 20:57:58 +0200 | [diff] [blame] | 2168 | * btrfs_bio_fits_in_stripe - Checks whether the size of the given bio will fit |
| 2169 | * in a chunk's stripe. This function ensures that bios do not span a |
| 2170 | * stripe/chunk |
Liu Bo | 6f034ec | 2016-06-22 18:31:49 -0700 | [diff] [blame] | 2171 | * |
Nikolay Borisov | da12fe5 | 2018-11-27 20:57:58 +0200 | [diff] [blame] | 2172 | * @page - The page we are about to add to the bio |
| 2173 | * @size - size we want to add to the bio |
| 2174 | * @bio - bio we want to ensure is smaller than a stripe |
| 2175 | * @bio_flags - flags of the bio |
| 2176 | * |
| 2177 | * return 1 if page cannot be added to the bio |
| 2178 | * return 0 if page can be added to the bio |
Liu Bo | 6f034ec | 2016-06-22 18:31:49 -0700 | [diff] [blame] | 2179 | * return error otherwise |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2180 | */ |
Nikolay Borisov | da12fe5 | 2018-11-27 20:57:58 +0200 | [diff] [blame] | 2181 | int btrfs_bio_fits_in_stripe(struct page *page, size_t size, struct bio *bio, |
| 2182 | unsigned long bio_flags) |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 2183 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2184 | struct inode *inode = page->mapping->host; |
| 2185 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 2186 | u64 logical = (u64)bio->bi_iter.bi_sector << 9; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 2187 | u64 length = 0; |
| 2188 | u64 map_length; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 2189 | int ret; |
Nikolay Borisov | 89b798a | 2019-06-03 12:05:05 +0300 | [diff] [blame] | 2190 | struct btrfs_io_geometry geom; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 2191 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 2192 | if (bio_flags & EXTENT_BIO_COMPRESSED) |
| 2193 | return 0; |
| 2194 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 2195 | length = bio->bi_iter.bi_size; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 2196 | map_length = length; |
Nikolay Borisov | 89b798a | 2019-06-03 12:05:05 +0300 | [diff] [blame] | 2197 | ret = btrfs_get_io_geometry(fs_info, btrfs_op(bio), logical, map_length, |
| 2198 | &geom); |
Liu Bo | 6f034ec | 2016-06-22 18:31:49 -0700 | [diff] [blame] | 2199 | if (ret < 0) |
| 2200 | return ret; |
Nikolay Borisov | 89b798a | 2019-06-03 12:05:05 +0300 | [diff] [blame] | 2201 | |
| 2202 | if (geom.len < length + size) |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 2203 | return 1; |
Jeff Mahoney | 3444a97 | 2011-10-03 23:23:13 -0400 | [diff] [blame] | 2204 | return 0; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 2205 | } |
| 2206 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2207 | /* |
| 2208 | * in order to insert checksums into the metadata in large chunks, |
| 2209 | * we wait until bio submission time. All the pages in the bio are |
| 2210 | * checksummed and sums are attached onto the ordered extent record. |
| 2211 | * |
| 2212 | * At IO completion time the cums attached on the ordered extent record |
| 2213 | * are inserted into the btree |
| 2214 | */ |
Qu Wenruo | 8896a08 | 2020-10-21 14:24:53 +0800 | [diff] [blame] | 2215 | static blk_status_t btrfs_submit_bio_start(struct inode *inode, struct bio *bio, |
| 2216 | u64 bio_offset) |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 2217 | { |
Johannes Thumshirn | c965d64 | 2020-07-28 20:25:41 +0900 | [diff] [blame] | 2218 | return btrfs_csum_one_bio(BTRFS_I(inode), bio, 0, 0); |
Chris Mason | 4a69a41 | 2008-11-06 22:03:00 -0500 | [diff] [blame] | 2219 | } |
Chris Mason | e015640 | 2008-04-16 11:15:20 -0400 | [diff] [blame] | 2220 | |
Chris Mason | 4a69a41 | 2008-11-06 22:03:00 -0500 | [diff] [blame] | 2221 | /* |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 2222 | * 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] | 2223 | * on write, or reading the csums from the tree before a read. |
| 2224 | * |
| 2225 | * Rules about async/sync submit, |
| 2226 | * a) read: sync submit |
| 2227 | * |
| 2228 | * b) write without checksum: sync submit |
| 2229 | * |
| 2230 | * c) write with checksum: |
| 2231 | * c-1) if bio is issued by fsync: sync submit |
| 2232 | * (sync_writers != 0) |
| 2233 | * |
| 2234 | * c-2) if root is reloc root: sync submit |
| 2235 | * (only in case of buffered IO) |
| 2236 | * |
| 2237 | * c-3) otherwise: async submit |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2238 | */ |
Nikolay Borisov | 908930f | 2020-09-18 16:34:37 +0300 | [diff] [blame] | 2239 | blk_status_t btrfs_submit_data_bio(struct inode *inode, struct bio *bio, |
| 2240 | int mirror_num, unsigned long bio_flags) |
Nikolay Borisov | 50489a5 | 2019-04-10 19:46:04 +0300 | [diff] [blame] | 2241 | |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 2242 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2243 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 2244 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chandan Rajendra | 0d51e28 | 2015-07-27 15:26:43 +0530 | [diff] [blame] | 2245 | enum btrfs_wq_endio_type metadata = BTRFS_WQ_ENDIO_DATA; |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 2246 | blk_status_t ret = 0; |
Chris Mason | 19b9bdb | 2008-10-30 14:23:13 -0400 | [diff] [blame] | 2247 | int skip_sum; |
Josef Bacik | b812ce2 | 2012-11-16 13:56:32 -0500 | [diff] [blame] | 2248 | int async = !atomic_read(&BTRFS_I(inode)->sync_writers); |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 2249 | |
Josef Bacik | 42437a6 | 2020-10-16 11:29:18 -0400 | [diff] [blame] | 2250 | skip_sum = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM) || |
| 2251 | !fs_info->csum_root; |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 2252 | |
Nikolay Borisov | 70ddc55 | 2017-02-20 13:50:35 +0200 | [diff] [blame] | 2253 | if (btrfs_is_free_space_inode(BTRFS_I(inode))) |
Chandan Rajendra | 0d51e28 | 2015-07-27 15:26:43 +0530 | [diff] [blame] | 2254 | metadata = BTRFS_WQ_ENDIO_FREE_SPACE; |
Jeff Mahoney | 0417341 | 2011-10-03 23:23:12 -0400 | [diff] [blame] | 2255 | |
Mike Christie | 37226b2 | 2016-06-05 14:31:52 -0500 | [diff] [blame] | 2256 | if (bio_op(bio) != REQ_OP_WRITE) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2257 | ret = btrfs_bio_wq_end_io(fs_info, bio, metadata); |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2258 | if (ret) |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 2259 | goto out; |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2260 | |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 2261 | if (bio_flags & EXTENT_BIO_COMPRESSED) { |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 2262 | ret = btrfs_submit_compressed_read(inode, bio, |
| 2263 | mirror_num, |
| 2264 | bio_flags); |
| 2265 | goto out; |
Josef Bacik | 334c16d | 2020-10-16 11:29:14 -0400 | [diff] [blame] | 2266 | } else { |
| 2267 | /* |
| 2268 | * Lookup bio sums does extra checks around whether we |
| 2269 | * need to csum or not, which is why we ignore skip_sum |
| 2270 | * here. |
| 2271 | */ |
Omar Sandoval | db72e47 | 2019-12-10 10:37:35 -0800 | [diff] [blame] | 2272 | ret = btrfs_lookup_bio_sums(inode, bio, (u64)-1, NULL); |
Tsutomu Itoh | c2db107 | 2011-03-01 06:48:31 +0000 | [diff] [blame] | 2273 | if (ret) |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 2274 | goto out; |
Tsutomu Itoh | c2db107 | 2011-03-01 06:48:31 +0000 | [diff] [blame] | 2275 | } |
Chris Mason | 4d1b5fb | 2008-08-20 09:44:52 -0400 | [diff] [blame] | 2276 | goto mapit; |
Josef Bacik | b812ce2 | 2012-11-16 13:56:32 -0500 | [diff] [blame] | 2277 | } else if (async && !skip_sum) { |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 2278 | /* csum items have already been cloned */ |
| 2279 | if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID) |
| 2280 | goto mapit; |
Chris Mason | 19b9bdb | 2008-10-30 14:23:13 -0400 | [diff] [blame] | 2281 | /* we're doing a write, do the async checksumming */ |
Qu Wenruo | 8896a08 | 2020-10-21 14:24:53 +0800 | [diff] [blame] | 2282 | ret = btrfs_wq_submit_bio(inode, bio, mirror_num, bio_flags, |
| 2283 | 0, btrfs_submit_bio_start); |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 2284 | goto out; |
Josef Bacik | b812ce2 | 2012-11-16 13:56:32 -0500 | [diff] [blame] | 2285 | } else if (!skip_sum) { |
Nikolay Borisov | bd242a0 | 2020-06-03 08:55:07 +0300 | [diff] [blame] | 2286 | ret = btrfs_csum_one_bio(BTRFS_I(inode), bio, 0, 0); |
Josef Bacik | b812ce2 | 2012-11-16 13:56:32 -0500 | [diff] [blame] | 2287 | if (ret) |
| 2288 | goto out; |
Chris Mason | 19b9bdb | 2008-10-30 14:23:13 -0400 | [diff] [blame] | 2289 | } |
| 2290 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2291 | mapit: |
Chris Mason | 08635ba | 2019-07-10 12:28:14 -0700 | [diff] [blame] | 2292 | ret = btrfs_map_bio(fs_info, bio, mirror_num); |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 2293 | |
| 2294 | out: |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 2295 | if (ret) { |
| 2296 | bio->bi_status = ret; |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 2297 | bio_endio(bio); |
| 2298 | } |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 2299 | return ret; |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 2300 | } |
Chris Mason | 6885f30 | 2008-02-20 16:11:05 -0500 | [diff] [blame] | 2301 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2302 | /* |
| 2303 | * given a list of ordered sums record them in the inode. This happens |
| 2304 | * at IO completion time based on sums calculated at bio submission time. |
| 2305 | */ |
Nikolay Borisov | 510f85e | 2020-09-18 12:15:52 +0300 | [diff] [blame] | 2306 | static int add_pending_csums(struct btrfs_trans_handle *trans, |
| 2307 | struct list_head *list) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2308 | { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2309 | struct btrfs_ordered_sum *sum; |
Nikolay Borisov | ac01f26 | 2018-01-08 10:59:43 +0200 | [diff] [blame] | 2310 | int ret; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2311 | |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 2312 | list_for_each_entry(sum, list, list) { |
David Sterba | 7c2871a | 2017-11-08 01:07:43 +0100 | [diff] [blame] | 2313 | trans->adding_csums = true; |
Nikolay Borisov | 510f85e | 2020-09-18 12:15:52 +0300 | [diff] [blame] | 2314 | ret = btrfs_csum_file_blocks(trans, trans->fs_info->csum_root, sum); |
David Sterba | 7c2871a | 2017-11-08 01:07:43 +0100 | [diff] [blame] | 2315 | trans->adding_csums = false; |
Nikolay Borisov | ac01f26 | 2018-01-08 10:59:43 +0200 | [diff] [blame] | 2316 | if (ret) |
| 2317 | return ret; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2318 | } |
| 2319 | return 0; |
| 2320 | } |
| 2321 | |
Filipe Manana | c334730 | 2020-11-04 11:07:31 +0000 | [diff] [blame] | 2322 | static int btrfs_find_new_delalloc_bytes(struct btrfs_inode *inode, |
| 2323 | const u64 start, |
| 2324 | const u64 len, |
| 2325 | struct extent_state **cached_state) |
| 2326 | { |
| 2327 | u64 search_start = start; |
| 2328 | const u64 end = start + len - 1; |
| 2329 | |
| 2330 | while (search_start < end) { |
| 2331 | const u64 search_len = end - search_start + 1; |
| 2332 | struct extent_map *em; |
| 2333 | u64 em_len; |
| 2334 | int ret = 0; |
| 2335 | |
| 2336 | em = btrfs_get_extent(inode, NULL, 0, search_start, search_len); |
| 2337 | if (IS_ERR(em)) |
| 2338 | return PTR_ERR(em); |
| 2339 | |
| 2340 | if (em->block_start != EXTENT_MAP_HOLE) |
| 2341 | goto next; |
| 2342 | |
| 2343 | em_len = em->len; |
| 2344 | if (em->start < search_start) |
| 2345 | em_len -= search_start - em->start; |
| 2346 | if (em_len > search_len) |
| 2347 | em_len = search_len; |
| 2348 | |
| 2349 | ret = set_extent_bit(&inode->io_tree, search_start, |
| 2350 | search_start + em_len - 1, |
Nikolay Borisov | 1cab5e7 | 2020-11-05 11:08:00 +0200 | [diff] [blame] | 2351 | EXTENT_DELALLOC_NEW, 0, NULL, cached_state, |
| 2352 | GFP_NOFS, NULL); |
Filipe Manana | c334730 | 2020-11-04 11:07:31 +0000 | [diff] [blame] | 2353 | next: |
| 2354 | search_start = extent_map_end(em); |
| 2355 | free_extent_map(em); |
| 2356 | if (ret) |
| 2357 | return ret; |
| 2358 | } |
| 2359 | return 0; |
| 2360 | } |
| 2361 | |
Nikolay Borisov | c2566f2 | 2020-06-03 08:55:35 +0300 | [diff] [blame] | 2362 | 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] | 2363 | unsigned int extra_bits, |
Nikolay Borisov | 330a582 | 2019-07-17 16:18:17 +0300 | [diff] [blame] | 2364 | struct extent_state **cached_state) |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 2365 | { |
Johannes Thumshirn | fdb1e12 | 2018-12-05 15:23:04 +0100 | [diff] [blame] | 2366 | WARN_ON(PAGE_ALIGNED(end)); |
Filipe Manana | c334730 | 2020-11-04 11:07:31 +0000 | [diff] [blame] | 2367 | |
| 2368 | if (start >= i_size_read(&inode->vfs_inode) && |
| 2369 | !(inode->flags & BTRFS_INODE_PREALLOC)) { |
| 2370 | /* |
| 2371 | * There can't be any extents following eof in this case so just |
| 2372 | * set the delalloc new bit for the range directly. |
| 2373 | */ |
| 2374 | extra_bits |= EXTENT_DELALLOC_NEW; |
| 2375 | } else { |
| 2376 | int ret; |
| 2377 | |
| 2378 | ret = btrfs_find_new_delalloc_bytes(inode, start, |
| 2379 | end + 1 - start, |
| 2380 | cached_state); |
| 2381 | if (ret) |
| 2382 | return ret; |
| 2383 | } |
| 2384 | |
Nikolay Borisov | c2566f2 | 2020-06-03 08:55:35 +0300 | [diff] [blame] | 2385 | return set_extent_delalloc(&inode->io_tree, start, end, extra_bits, |
| 2386 | cached_state); |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 2387 | } |
| 2388 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2389 | /* see btrfs_writepage_start_hook for details on why this is required */ |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2390 | struct btrfs_writepage_fixup { |
| 2391 | struct page *page; |
Josef Bacik | f4b1363 | 2020-01-21 14:34:52 -0500 | [diff] [blame] | 2392 | struct inode *inode; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2393 | struct btrfs_work work; |
| 2394 | }; |
| 2395 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 2396 | static void btrfs_writepage_fixup_worker(struct btrfs_work *work) |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2397 | { |
| 2398 | struct btrfs_writepage_fixup *fixup; |
| 2399 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 2400 | struct extent_state *cached_state = NULL; |
Qu Wenruo | 364ecf3 | 2017-02-27 15:10:38 +0800 | [diff] [blame] | 2401 | struct extent_changeset *data_reserved = NULL; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2402 | struct page *page; |
Nikolay Borisov | 65d87f7 | 2020-06-05 10:51:51 +0300 | [diff] [blame] | 2403 | struct btrfs_inode *inode; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2404 | u64 page_start; |
| 2405 | u64 page_end; |
Chris Mason | 25f3c50 | 2020-01-21 11:51:42 -0500 | [diff] [blame] | 2406 | int ret = 0; |
Josef Bacik | f4b1363 | 2020-01-21 14:34:52 -0500 | [diff] [blame] | 2407 | bool free_delalloc_space = true; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2408 | |
| 2409 | fixup = container_of(work, struct btrfs_writepage_fixup, work); |
| 2410 | page = fixup->page; |
Nikolay Borisov | 65d87f7 | 2020-06-05 10:51:51 +0300 | [diff] [blame] | 2411 | inode = BTRFS_I(fixup->inode); |
Josef Bacik | f4b1363 | 2020-01-21 14:34:52 -0500 | [diff] [blame] | 2412 | page_start = page_offset(page); |
| 2413 | page_end = page_offset(page) + PAGE_SIZE - 1; |
| 2414 | |
| 2415 | /* |
| 2416 | * This is similar to page_mkwrite, we need to reserve the space before |
| 2417 | * we take the page lock. |
| 2418 | */ |
Nikolay Borisov | 65d87f7 | 2020-06-05 10:51:51 +0300 | [diff] [blame] | 2419 | ret = btrfs_delalloc_reserve_space(inode, &data_reserved, page_start, |
| 2420 | PAGE_SIZE); |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 2421 | again: |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2422 | lock_page(page); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2423 | |
Chris Mason | 25f3c50 | 2020-01-21 11:51:42 -0500 | [diff] [blame] | 2424 | /* |
| 2425 | * Before we queued this fixup, we took a reference on the page. |
| 2426 | * page->mapping may go NULL, but it shouldn't be moved to a different |
| 2427 | * address space. |
| 2428 | */ |
Josef Bacik | f4b1363 | 2020-01-21 14:34:52 -0500 | [diff] [blame] | 2429 | if (!page->mapping || !PageDirty(page) || !PageChecked(page)) { |
| 2430 | /* |
| 2431 | * Unfortunately this is a little tricky, either |
| 2432 | * |
| 2433 | * 1) We got here and our page had already been dealt with and |
| 2434 | * we reserved our space, thus ret == 0, so we need to just |
| 2435 | * drop our space reservation and bail. This can happen the |
| 2436 | * first time we come into the fixup worker, or could happen |
| 2437 | * while waiting for the ordered extent. |
| 2438 | * 2) Our page was already dealt with, but we happened to get an |
| 2439 | * ENOSPC above from the btrfs_delalloc_reserve_space. In |
| 2440 | * this case we obviously don't have anything to release, but |
| 2441 | * because the page was already dealt with we don't want to |
| 2442 | * mark the page with an error, so make sure we're resetting |
| 2443 | * ret to 0. This is why we have this check _before_ the ret |
| 2444 | * check, because we do not want to have a surprise ENOSPC |
| 2445 | * when the page was already properly dealt with. |
| 2446 | */ |
| 2447 | if (!ret) { |
Nikolay Borisov | 65d87f7 | 2020-06-05 10:51:51 +0300 | [diff] [blame] | 2448 | btrfs_delalloc_release_extents(inode, PAGE_SIZE); |
| 2449 | btrfs_delalloc_release_space(inode, data_reserved, |
Josef Bacik | f4b1363 | 2020-01-21 14:34:52 -0500 | [diff] [blame] | 2450 | page_start, PAGE_SIZE, |
| 2451 | true); |
| 2452 | } |
| 2453 | ret = 0; |
Chris Mason | 25f3c50 | 2020-01-21 11:51:42 -0500 | [diff] [blame] | 2454 | goto out_page; |
Josef Bacik | f4b1363 | 2020-01-21 14:34:52 -0500 | [diff] [blame] | 2455 | } |
Chris Mason | 25f3c50 | 2020-01-21 11:51:42 -0500 | [diff] [blame] | 2456 | |
| 2457 | /* |
Josef Bacik | f4b1363 | 2020-01-21 14:34:52 -0500 | [diff] [blame] | 2458 | * We can't mess with the page state unless it is locked, so now that |
| 2459 | * it is locked bail if we failed to make our space reservation. |
Chris Mason | 25f3c50 | 2020-01-21 11:51:42 -0500 | [diff] [blame] | 2460 | */ |
Josef Bacik | f4b1363 | 2020-01-21 14:34:52 -0500 | [diff] [blame] | 2461 | if (ret) |
| 2462 | goto out_page; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2463 | |
Nikolay Borisov | 65d87f7 | 2020-06-05 10:51:51 +0300 | [diff] [blame] | 2464 | lock_extent_bits(&inode->io_tree, page_start, page_end, &cached_state); |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 2465 | |
| 2466 | /* already ordered? We're done */ |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 2467 | if (PagePrivate2(page)) |
Josef Bacik | f4b1363 | 2020-01-21 14:34:52 -0500 | [diff] [blame] | 2468 | goto out_reserved; |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 2469 | |
Nikolay Borisov | 65d87f7 | 2020-06-05 10:51:51 +0300 | [diff] [blame] | 2470 | ordered = btrfs_lookup_ordered_range(inode, page_start, PAGE_SIZE); |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 2471 | if (ordered) { |
Nikolay Borisov | 65d87f7 | 2020-06-05 10:51:51 +0300 | [diff] [blame] | 2472 | unlock_extent_cached(&inode->io_tree, page_start, page_end, |
| 2473 | &cached_state); |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 2474 | unlock_page(page); |
Nikolay Borisov | c0a4360 | 2020-09-18 12:15:53 +0300 | [diff] [blame] | 2475 | btrfs_start_ordered_extent(ordered, 1); |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 2476 | btrfs_put_ordered_extent(ordered); |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 2477 | goto again; |
| 2478 | } |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2479 | |
Nikolay Borisov | 65d87f7 | 2020-06-05 10:51:51 +0300 | [diff] [blame] | 2480 | ret = btrfs_set_extent_delalloc(inode, page_start, page_end, 0, |
Nikolay Borisov | 330a582 | 2019-07-17 16:18:17 +0300 | [diff] [blame] | 2481 | &cached_state); |
Chris Mason | 25f3c50 | 2020-01-21 11:51:42 -0500 | [diff] [blame] | 2482 | if (ret) |
Filipe Manana | 5368700 | 2019-10-09 17:43:59 +0100 | [diff] [blame] | 2483 | goto out_reserved; |
Nikolay Borisov | f3038ee | 2017-12-05 09:29:19 +0200 | [diff] [blame] | 2484 | |
Chris Mason | 25f3c50 | 2020-01-21 11:51:42 -0500 | [diff] [blame] | 2485 | /* |
| 2486 | * Everything went as planned, we're now the owner of a dirty page with |
| 2487 | * delayed allocation bits set and space reserved for our COW |
| 2488 | * destination. |
| 2489 | * |
| 2490 | * The page was dirty when we started, nothing should have cleaned it. |
| 2491 | */ |
| 2492 | BUG_ON(!PageDirty(page)); |
Josef Bacik | f4b1363 | 2020-01-21 14:34:52 -0500 | [diff] [blame] | 2493 | free_delalloc_space = false; |
Filipe Manana | 5368700 | 2019-10-09 17:43:59 +0100 | [diff] [blame] | 2494 | out_reserved: |
Nikolay Borisov | 65d87f7 | 2020-06-05 10:51:51 +0300 | [diff] [blame] | 2495 | btrfs_delalloc_release_extents(inode, PAGE_SIZE); |
Josef Bacik | f4b1363 | 2020-01-21 14:34:52 -0500 | [diff] [blame] | 2496 | if (free_delalloc_space) |
Nikolay Borisov | 65d87f7 | 2020-06-05 10:51:51 +0300 | [diff] [blame] | 2497 | btrfs_delalloc_release_space(inode, data_reserved, page_start, |
| 2498 | PAGE_SIZE, true); |
| 2499 | unlock_extent_cached(&inode->io_tree, page_start, page_end, |
David Sterba | e43bbe5 | 2017-12-12 21:43:52 +0100 | [diff] [blame] | 2500 | &cached_state); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2501 | out_page: |
Chris Mason | 25f3c50 | 2020-01-21 11:51:42 -0500 | [diff] [blame] | 2502 | if (ret) { |
| 2503 | /* |
| 2504 | * We hit ENOSPC or other errors. Update the mapping and page |
| 2505 | * to reflect the errors and clean the page. |
| 2506 | */ |
| 2507 | mapping_set_error(page->mapping, ret); |
| 2508 | end_extent_writepage(page, ret, page_start, page_end); |
| 2509 | clear_page_dirty_for_io(page); |
| 2510 | SetPageError(page); |
| 2511 | } |
| 2512 | ClearPageChecked(page); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2513 | unlock_page(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2514 | put_page(page); |
Miao Xie | b897abe | 2011-01-26 16:19:22 +0800 | [diff] [blame] | 2515 | kfree(fixup); |
Qu Wenruo | 364ecf3 | 2017-02-27 15:10:38 +0800 | [diff] [blame] | 2516 | extent_changeset_free(data_reserved); |
Josef Bacik | f4b1363 | 2020-01-21 14:34:52 -0500 | [diff] [blame] | 2517 | /* |
| 2518 | * As a precaution, do a delayed iput in case it would be the last iput |
| 2519 | * that could need flushing space. Recursing back to fixup worker would |
| 2520 | * deadlock. |
| 2521 | */ |
Nikolay Borisov | 65d87f7 | 2020-06-05 10:51:51 +0300 | [diff] [blame] | 2522 | btrfs_add_delayed_iput(&inode->vfs_inode); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2523 | } |
| 2524 | |
| 2525 | /* |
| 2526 | * There are a few paths in the higher layers of the kernel that directly |
| 2527 | * set the page dirty bit without asking the filesystem if it is a |
| 2528 | * good idea. This causes problems because we want to make sure COW |
| 2529 | * properly happens and the data=ordered rules are followed. |
| 2530 | * |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2531 | * 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] | 2532 | * hasn't been properly setup for IO. We kick off an async process |
| 2533 | * to fix it up. The async helper will wait for ordered extents, set |
| 2534 | * the delalloc bit and make it safe to write the page. |
| 2535 | */ |
Nikolay Borisov | d75855b | 2018-11-01 14:09:47 +0200 | [diff] [blame] | 2536 | int btrfs_writepage_cow_fixup(struct page *page, u64 start, u64 end) |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2537 | { |
| 2538 | struct inode *inode = page->mapping->host; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2539 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2540 | struct btrfs_writepage_fixup *fixup; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2541 | |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 2542 | /* this page is properly in the ordered list */ |
| 2543 | if (TestClearPagePrivate2(page)) |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2544 | return 0; |
| 2545 | |
Chris Mason | 25f3c50 | 2020-01-21 11:51:42 -0500 | [diff] [blame] | 2546 | /* |
| 2547 | * PageChecked is set below when we create a fixup worker for this page, |
| 2548 | * don't try to create another one if we're already PageChecked() |
| 2549 | * |
| 2550 | * The extent_io writepage code will redirty the page if we send back |
| 2551 | * EAGAIN. |
| 2552 | */ |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2553 | if (PageChecked(page)) |
| 2554 | return -EAGAIN; |
| 2555 | |
| 2556 | fixup = kzalloc(sizeof(*fixup), GFP_NOFS); |
| 2557 | if (!fixup) |
| 2558 | return -EAGAIN; |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 2559 | |
Josef Bacik | f4b1363 | 2020-01-21 14:34:52 -0500 | [diff] [blame] | 2560 | /* |
| 2561 | * We are already holding a reference to this inode from |
| 2562 | * write_cache_pages. We need to hold it because the space reservation |
| 2563 | * takes place outside of the page lock, and we can't trust |
| 2564 | * page->mapping outside of the page lock. |
| 2565 | */ |
| 2566 | ihold(inode); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2567 | SetPageChecked(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2568 | get_page(page); |
Omar Sandoval | a0cac0e | 2019-09-16 11:30:57 -0700 | [diff] [blame] | 2569 | btrfs_init_work(&fixup->work, btrfs_writepage_fixup_worker, NULL, NULL); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2570 | fixup->page = page; |
Josef Bacik | f4b1363 | 2020-01-21 14:34:52 -0500 | [diff] [blame] | 2571 | fixup->inode = inode; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2572 | btrfs_queue_work(fs_info->fixup_workers, &fixup->work); |
Chris Mason | 25f3c50 | 2020-01-21 11:51:42 -0500 | [diff] [blame] | 2573 | |
| 2574 | return -EAGAIN; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2575 | } |
| 2576 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2577 | static int insert_reserved_file_extent(struct btrfs_trans_handle *trans, |
Nikolay Borisov | c553f94 | 2020-06-03 08:55:19 +0300 | [diff] [blame] | 2578 | struct btrfs_inode *inode, u64 file_pos, |
Qu Wenruo | 9729f10 | 2020-06-10 09:04:41 +0800 | [diff] [blame] | 2579 | struct btrfs_file_extent_item *stack_fi, |
Filipe Manana | 2766ff6 | 2020-11-04 11:07:34 +0000 | [diff] [blame] | 2580 | const bool update_inode_bytes, |
Qu Wenruo | 9729f10 | 2020-06-10 09:04:41 +0800 | [diff] [blame] | 2581 | u64 qgroup_reserved) |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2582 | { |
Nikolay Borisov | c553f94 | 2020-06-03 08:55:19 +0300 | [diff] [blame] | 2583 | struct btrfs_root *root = inode->root; |
Filipe Manana | 2766ff6 | 2020-11-04 11:07:34 +0000 | [diff] [blame] | 2584 | const u64 sectorsize = root->fs_info->sectorsize; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2585 | struct btrfs_path *path; |
| 2586 | struct extent_buffer *leaf; |
| 2587 | struct btrfs_key ins; |
Qu Wenruo | 203f44c5 | 2020-06-10 09:04:40 +0800 | [diff] [blame] | 2588 | u64 disk_num_bytes = btrfs_stack_file_extent_disk_num_bytes(stack_fi); |
| 2589 | u64 disk_bytenr = btrfs_stack_file_extent_disk_bytenr(stack_fi); |
| 2590 | u64 num_bytes = btrfs_stack_file_extent_num_bytes(stack_fi); |
| 2591 | u64 ram_bytes = btrfs_stack_file_extent_ram_bytes(stack_fi); |
Filipe Manana | 5893dfb | 2020-11-04 11:07:32 +0000 | [diff] [blame] | 2592 | struct btrfs_drop_extents_args drop_args = { 0 }; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2593 | int ret; |
| 2594 | |
| 2595 | path = btrfs_alloc_path(); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 2596 | if (!path) |
| 2597 | return -ENOMEM; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2598 | |
Chris Mason | a1ed835 | 2009-09-11 12:27:37 -0400 | [diff] [blame] | 2599 | /* |
| 2600 | * we may be replacing one extent in the tree with another. |
| 2601 | * The new extent is pinned in the extent map, and we don't want |
| 2602 | * to drop it from the cache until it is completely in the btree. |
| 2603 | * |
| 2604 | * So, tell btrfs_drop_extents to leave this extent in the cache. |
| 2605 | * the caller is expected to unpin it and allow it to be merged |
| 2606 | * with the others. |
| 2607 | */ |
Filipe Manana | 5893dfb | 2020-11-04 11:07:32 +0000 | [diff] [blame] | 2608 | drop_args.path = path; |
| 2609 | drop_args.start = file_pos; |
| 2610 | drop_args.end = file_pos + num_bytes; |
| 2611 | drop_args.replace_extent = true; |
| 2612 | drop_args.extent_item_size = sizeof(*stack_fi); |
| 2613 | ret = btrfs_drop_extents(trans, root, inode, &drop_args); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2614 | if (ret) |
| 2615 | goto out; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2616 | |
Filipe Manana | 5893dfb | 2020-11-04 11:07:32 +0000 | [diff] [blame] | 2617 | if (!drop_args.extent_inserted) { |
Nikolay Borisov | c553f94 | 2020-06-03 08:55:19 +0300 | [diff] [blame] | 2618 | ins.objectid = btrfs_ino(inode); |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 2619 | ins.offset = file_pos; |
| 2620 | ins.type = BTRFS_EXTENT_DATA_KEY; |
| 2621 | |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 2622 | ret = btrfs_insert_empty_item(trans, root, path, &ins, |
Qu Wenruo | 203f44c5 | 2020-06-10 09:04:40 +0800 | [diff] [blame] | 2623 | sizeof(*stack_fi)); |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 2624 | if (ret) |
| 2625 | goto out; |
| 2626 | } |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2627 | leaf = path->nodes[0]; |
Qu Wenruo | 203f44c5 | 2020-06-10 09:04:40 +0800 | [diff] [blame] | 2628 | btrfs_set_stack_file_extent_generation(stack_fi, trans->transid); |
| 2629 | write_extent_buffer(leaf, stack_fi, |
| 2630 | btrfs_item_ptr_offset(leaf, path->slots[0]), |
| 2631 | sizeof(struct btrfs_file_extent_item)); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 2632 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2633 | btrfs_mark_buffer_dirty(leaf); |
Josef Bacik | ce19533 | 2012-09-25 15:26:16 -0400 | [diff] [blame] | 2634 | btrfs_release_path(path); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2635 | |
Filipe Manana | 2766ff6 | 2020-11-04 11:07:34 +0000 | [diff] [blame] | 2636 | /* |
| 2637 | * If we dropped an inline extent here, we know the range where it is |
| 2638 | * was not marked with the EXTENT_DELALLOC_NEW bit, so we update the |
| 2639 | * number of bytes only for that range contaning the inline extent. |
| 2640 | * The remaining of the range will be processed when clearning the |
| 2641 | * EXTENT_DELALLOC_BIT bit through the ordered extent completion. |
| 2642 | */ |
| 2643 | if (file_pos == 0 && !IS_ALIGNED(drop_args.bytes_found, sectorsize)) { |
| 2644 | u64 inline_size = round_down(drop_args.bytes_found, sectorsize); |
| 2645 | |
| 2646 | inline_size = drop_args.bytes_found - inline_size; |
| 2647 | btrfs_update_inode_bytes(inode, sectorsize, inline_size); |
| 2648 | drop_args.bytes_found -= inline_size; |
| 2649 | num_bytes -= sectorsize; |
| 2650 | } |
| 2651 | |
| 2652 | if (update_inode_bytes) |
| 2653 | btrfs_update_inode_bytes(inode, num_bytes, drop_args.bytes_found); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2654 | |
| 2655 | ins.objectid = disk_bytenr; |
| 2656 | ins.offset = disk_num_bytes; |
| 2657 | ins.type = BTRFS_EXTENT_ITEM_KEY; |
Qu Wenruo | a12b877 | 2017-02-27 15:10:37 +0800 | [diff] [blame] | 2658 | |
Nikolay Borisov | c553f94 | 2020-06-03 08:55:19 +0300 | [diff] [blame] | 2659 | ret = btrfs_inode_set_file_extent_range(inode, file_pos, ram_bytes); |
Josef Bacik | 9ddc959 | 2020-01-17 09:02:22 -0500 | [diff] [blame] | 2660 | if (ret) |
| 2661 | goto out; |
| 2662 | |
Nikolay Borisov | c553f94 | 2020-06-03 08:55:19 +0300 | [diff] [blame] | 2663 | ret = btrfs_alloc_reserved_file_extent(trans, root, btrfs_ino(inode), |
Qu Wenruo | 9729f10 | 2020-06-10 09:04:41 +0800 | [diff] [blame] | 2664 | file_pos, qgroup_reserved, &ins); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2665 | out: |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2666 | btrfs_free_path(path); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 2667 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2668 | return ret; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2669 | } |
| 2670 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2671 | static void btrfs_release_delalloc_bytes(struct btrfs_fs_info *fs_info, |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 2672 | u64 start, u64 len) |
| 2673 | { |
David Sterba | 32da5386 | 2019-10-29 19:20:18 +0100 | [diff] [blame] | 2674 | struct btrfs_block_group *cache; |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 2675 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2676 | cache = btrfs_lookup_block_group(fs_info, start); |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 2677 | ASSERT(cache); |
| 2678 | |
| 2679 | spin_lock(&cache->lock); |
| 2680 | cache->delalloc_bytes -= len; |
| 2681 | spin_unlock(&cache->lock); |
| 2682 | |
| 2683 | btrfs_put_block_group(cache); |
| 2684 | } |
| 2685 | |
Qu Wenruo | 203f44c5 | 2020-06-10 09:04:40 +0800 | [diff] [blame] | 2686 | static int insert_ordered_extent_file_extent(struct btrfs_trans_handle *trans, |
Qu Wenruo | 203f44c5 | 2020-06-10 09:04:40 +0800 | [diff] [blame] | 2687 | struct btrfs_ordered_extent *oe) |
| 2688 | { |
| 2689 | struct btrfs_file_extent_item stack_fi; |
| 2690 | u64 logical_len; |
Filipe Manana | 2766ff6 | 2020-11-04 11:07:34 +0000 | [diff] [blame] | 2691 | bool update_inode_bytes; |
Qu Wenruo | 203f44c5 | 2020-06-10 09:04:40 +0800 | [diff] [blame] | 2692 | |
| 2693 | memset(&stack_fi, 0, sizeof(stack_fi)); |
| 2694 | btrfs_set_stack_file_extent_type(&stack_fi, BTRFS_FILE_EXTENT_REG); |
| 2695 | btrfs_set_stack_file_extent_disk_bytenr(&stack_fi, oe->disk_bytenr); |
| 2696 | btrfs_set_stack_file_extent_disk_num_bytes(&stack_fi, |
| 2697 | oe->disk_num_bytes); |
| 2698 | if (test_bit(BTRFS_ORDERED_TRUNCATED, &oe->flags)) |
| 2699 | logical_len = oe->truncated_len; |
| 2700 | else |
| 2701 | logical_len = oe->num_bytes; |
| 2702 | btrfs_set_stack_file_extent_num_bytes(&stack_fi, logical_len); |
| 2703 | btrfs_set_stack_file_extent_ram_bytes(&stack_fi, logical_len); |
| 2704 | btrfs_set_stack_file_extent_compression(&stack_fi, oe->compress_type); |
| 2705 | /* Encryption and other encoding is reserved and all 0 */ |
| 2706 | |
Filipe Manana | 2766ff6 | 2020-11-04 11:07:34 +0000 | [diff] [blame] | 2707 | /* |
| 2708 | * For delalloc, when completing an ordered extent we update the inode's |
| 2709 | * bytes when clearing the range in the inode's io tree, so pass false |
| 2710 | * as the argument 'update_inode_bytes' to insert_reserved_file_extent(), |
| 2711 | * except if the ordered extent was truncated. |
| 2712 | */ |
| 2713 | update_inode_bytes = test_bit(BTRFS_ORDERED_DIRECT, &oe->flags) || |
| 2714 | test_bit(BTRFS_ORDERED_TRUNCATED, &oe->flags); |
| 2715 | |
Nikolay Borisov | 3c38c87 | 2020-09-18 12:15:51 +0300 | [diff] [blame] | 2716 | return insert_reserved_file_extent(trans, BTRFS_I(oe->inode), |
| 2717 | oe->file_offset, &stack_fi, |
Filipe Manana | 2766ff6 | 2020-11-04 11:07:34 +0000 | [diff] [blame] | 2718 | update_inode_bytes, oe->qgroup_rsv); |
Qu Wenruo | 203f44c5 | 2020-06-10 09:04:40 +0800 | [diff] [blame] | 2719 | } |
| 2720 | |
| 2721 | /* |
| 2722 | * As ordered data IO finishes, this gets called so we can finish |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2723 | * an ordered extent if the range of bytes in the file it covers are |
| 2724 | * fully written. |
| 2725 | */ |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2726 | static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2727 | { |
Nikolay Borisov | 72e7e6e | 2020-11-02 16:48:56 +0200 | [diff] [blame] | 2728 | struct btrfs_inode *inode = BTRFS_I(ordered_extent->inode); |
| 2729 | struct btrfs_root *root = inode->root; |
| 2730 | struct btrfs_fs_info *fs_info = root->fs_info; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 2731 | struct btrfs_trans_handle *trans = NULL; |
Nikolay Borisov | 72e7e6e | 2020-11-02 16:48:56 +0200 | [diff] [blame] | 2732 | struct extent_io_tree *io_tree = &inode->io_tree; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 2733 | struct extent_state *cached_state = NULL; |
Omar Sandoval | bffe633 | 2019-12-02 17:34:19 -0800 | [diff] [blame] | 2734 | u64 start, end; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 2735 | int compress_type = 0; |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 2736 | int ret = 0; |
Omar Sandoval | bffe633 | 2019-12-02 17:34:19 -0800 | [diff] [blame] | 2737 | u64 logical_len = ordered_extent->num_bytes; |
Nikolay Borisov | 8d51012 | 2019-10-08 20:43:06 +0300 | [diff] [blame] | 2738 | bool freespace_inode; |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 2739 | bool truncated = false; |
Josef Bacik | 49940bd | 2018-10-11 15:54:21 -0400 | [diff] [blame] | 2740 | bool clear_reserved_extent = true; |
Filipe Manana | 2766ff6 | 2020-11-04 11:07:34 +0000 | [diff] [blame] | 2741 | unsigned int clear_bits = EXTENT_DEFRAG; |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 2742 | |
Omar Sandoval | bffe633 | 2019-12-02 17:34:19 -0800 | [diff] [blame] | 2743 | start = ordered_extent->file_offset; |
| 2744 | end = start + ordered_extent->num_bytes - 1; |
| 2745 | |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 2746 | if (!test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) && |
| 2747 | !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags) && |
| 2748 | !test_bit(BTRFS_ORDERED_DIRECT, &ordered_extent->flags)) |
Filipe Manana | 2766ff6 | 2020-11-04 11:07:34 +0000 | [diff] [blame] | 2749 | clear_bits |= EXTENT_DELALLOC_NEW; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2750 | |
Nikolay Borisov | 72e7e6e | 2020-11-02 16:48:56 +0200 | [diff] [blame] | 2751 | freespace_inode = btrfs_is_free_space_inode(inode); |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 2752 | |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2753 | if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) { |
| 2754 | ret = -EIO; |
| 2755 | goto out; |
| 2756 | } |
| 2757 | |
Nikolay Borisov | 72e7e6e | 2020-11-02 16:48:56 +0200 | [diff] [blame] | 2758 | btrfs_free_io_failure_record(inode, start, end); |
Miao Xie | f612496 | 2014-09-12 18:44:04 +0800 | [diff] [blame] | 2759 | |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 2760 | if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) { |
| 2761 | truncated = true; |
| 2762 | logical_len = ordered_extent->truncated_len; |
| 2763 | /* Truncated the entire extent, don't bother adding */ |
| 2764 | if (!logical_len) |
| 2765 | goto out; |
| 2766 | } |
| 2767 | |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 2768 | if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) { |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2769 | BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */ |
Qu Wenruo | 94ed938 | 2015-09-08 17:25:56 +0800 | [diff] [blame] | 2770 | |
Nikolay Borisov | 72e7e6e | 2020-11-02 16:48:56 +0200 | [diff] [blame] | 2771 | btrfs_inode_safe_disk_i_size_write(inode, 0); |
Nikolay Borisov | 8d51012 | 2019-10-08 20:43:06 +0300 | [diff] [blame] | 2772 | if (freespace_inode) |
| 2773 | trans = btrfs_join_transaction_spacecache(root); |
Josef Bacik | 6c760c0 | 2012-11-09 10:53:21 -0500 | [diff] [blame] | 2774 | else |
| 2775 | trans = btrfs_join_transaction(root); |
| 2776 | if (IS_ERR(trans)) { |
| 2777 | ret = PTR_ERR(trans); |
| 2778 | trans = NULL; |
| 2779 | goto out; |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 2780 | } |
Nikolay Borisov | 72e7e6e | 2020-11-02 16:48:56 +0200 | [diff] [blame] | 2781 | trans->block_rsv = &inode->block_rsv; |
Nikolay Borisov | 729f796 | 2020-11-02 16:49:06 +0200 | [diff] [blame] | 2782 | ret = btrfs_update_inode_fallback(trans, root, inode); |
Josef Bacik | 6c760c0 | 2012-11-09 10:53:21 -0500 | [diff] [blame] | 2783 | if (ret) /* -ENOMEM or corruption */ |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 2784 | btrfs_abort_transaction(trans, ret); |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 2785 | goto out; |
| 2786 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2787 | |
Filipe Manana | 2766ff6 | 2020-11-04 11:07:34 +0000 | [diff] [blame] | 2788 | clear_bits |= EXTENT_LOCKED; |
Omar Sandoval | bffe633 | 2019-12-02 17:34:19 -0800 | [diff] [blame] | 2789 | lock_extent_bits(io_tree, start, end, &cached_state); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2790 | |
Nikolay Borisov | 8d51012 | 2019-10-08 20:43:06 +0300 | [diff] [blame] | 2791 | if (freespace_inode) |
| 2792 | trans = btrfs_join_transaction_spacecache(root); |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 2793 | else |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 2794 | trans = btrfs_join_transaction(root); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2795 | if (IS_ERR(trans)) { |
| 2796 | ret = PTR_ERR(trans); |
| 2797 | trans = NULL; |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 2798 | goto out; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2799 | } |
Chris Mason | a79b7d4 | 2014-05-22 16:18:52 -0700 | [diff] [blame] | 2800 | |
Nikolay Borisov | 72e7e6e | 2020-11-02 16:48:56 +0200 | [diff] [blame] | 2801 | trans->block_rsv = &inode->block_rsv; |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 2802 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2803 | if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags)) |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 2804 | compress_type = ordered_extent->compress_type; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2805 | if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) { |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 2806 | BUG_ON(compress_type); |
Nikolay Borisov | 72e7e6e | 2020-11-02 16:48:56 +0200 | [diff] [blame] | 2807 | ret = btrfs_mark_extent_written(trans, inode, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2808 | ordered_extent->file_offset, |
| 2809 | ordered_extent->file_offset + |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 2810 | logical_len); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2811 | } else { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2812 | BUG_ON(root == fs_info->tree_root); |
Nikolay Borisov | 3c38c87 | 2020-09-18 12:15:51 +0300 | [diff] [blame] | 2813 | ret = insert_ordered_extent_file_extent(trans, ordered_extent); |
Josef Bacik | 49940bd | 2018-10-11 15:54:21 -0400 | [diff] [blame] | 2814 | if (!ret) { |
| 2815 | clear_reserved_extent = false; |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2816 | btrfs_release_delalloc_bytes(fs_info, |
Omar Sandoval | bffe633 | 2019-12-02 17:34:19 -0800 | [diff] [blame] | 2817 | ordered_extent->disk_bytenr, |
| 2818 | ordered_extent->disk_num_bytes); |
Josef Bacik | 49940bd | 2018-10-11 15:54:21 -0400 | [diff] [blame] | 2819 | } |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2820 | } |
Nikolay Borisov | 72e7e6e | 2020-11-02 16:48:56 +0200 | [diff] [blame] | 2821 | unpin_extent_cache(&inode->extent_tree, ordered_extent->file_offset, |
Omar Sandoval | bffe633 | 2019-12-02 17:34:19 -0800 | [diff] [blame] | 2822 | ordered_extent->num_bytes, trans->transid); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2823 | if (ret < 0) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 2824 | btrfs_abort_transaction(trans, ret); |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 2825 | goto out; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2826 | } |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 2827 | |
Nikolay Borisov | 510f85e | 2020-09-18 12:15:52 +0300 | [diff] [blame] | 2828 | ret = add_pending_csums(trans, &ordered_extent->list); |
Nikolay Borisov | ac01f26 | 2018-01-08 10:59:43 +0200 | [diff] [blame] | 2829 | if (ret) { |
| 2830 | btrfs_abort_transaction(trans, ret); |
| 2831 | goto out; |
| 2832 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2833 | |
Filipe Manana | 2766ff6 | 2020-11-04 11:07:34 +0000 | [diff] [blame] | 2834 | /* |
| 2835 | * If this is a new delalloc range, clear its new delalloc flag to |
| 2836 | * update the inode's number of bytes. This needs to be done first |
| 2837 | * before updating the inode item. |
| 2838 | */ |
| 2839 | if ((clear_bits & EXTENT_DELALLOC_NEW) && |
| 2840 | !test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) |
Nikolay Borisov | 72e7e6e | 2020-11-02 16:48:56 +0200 | [diff] [blame] | 2841 | clear_extent_bit(&inode->io_tree, start, end, |
Filipe Manana | 2766ff6 | 2020-11-04 11:07:34 +0000 | [diff] [blame] | 2842 | EXTENT_DELALLOC_NEW | EXTENT_ADD_INODE_BYTES, |
| 2843 | 0, 0, &cached_state); |
| 2844 | |
Nikolay Borisov | 72e7e6e | 2020-11-02 16:48:56 +0200 | [diff] [blame] | 2845 | btrfs_inode_safe_disk_i_size_write(inode, 0); |
Nikolay Borisov | 729f796 | 2020-11-02 16:49:06 +0200 | [diff] [blame] | 2846 | ret = btrfs_update_inode_fallback(trans, root, inode); |
Josef Bacik | 6c760c0 | 2012-11-09 10:53:21 -0500 | [diff] [blame] | 2847 | if (ret) { /* -ENOMEM or corruption */ |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 2848 | btrfs_abort_transaction(trans, ret); |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 2849 | goto out; |
Josef Bacik | 1ef30be | 2011-04-05 19:25:36 -0400 | [diff] [blame] | 2850 | } |
| 2851 | ret = 0; |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 2852 | out: |
Nikolay Borisov | 72e7e6e | 2020-11-02 16:48:56 +0200 | [diff] [blame] | 2853 | clear_extent_bit(&inode->io_tree, start, end, clear_bits, |
Omar Sandoval | bffe633 | 2019-12-02 17:34:19 -0800 | [diff] [blame] | 2854 | (clear_bits & EXTENT_LOCKED) ? 1 : 0, 0, |
Omar Sandoval | 313facc | 2019-12-02 17:34:18 -0800 | [diff] [blame] | 2855 | &cached_state); |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 2856 | |
Miao Xie | a698d075 | 2012-09-20 01:51:59 -0600 | [diff] [blame] | 2857 | if (trans) |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 2858 | btrfs_end_transaction(trans); |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 2859 | |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 2860 | if (ret || truncated) { |
Omar Sandoval | bffe633 | 2019-12-02 17:34:19 -0800 | [diff] [blame] | 2861 | u64 unwritten_start = start; |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 2862 | |
| 2863 | if (truncated) |
Omar Sandoval | bffe633 | 2019-12-02 17:34:19 -0800 | [diff] [blame] | 2864 | unwritten_start += logical_len; |
| 2865 | clear_extent_uptodate(io_tree, unwritten_start, end, NULL); |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 2866 | |
| 2867 | /* 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] | 2868 | btrfs_drop_extent_cache(inode, unwritten_start, end, 0); |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2869 | |
Josef Bacik | 0bec9ef | 2013-01-31 14:58:00 -0500 | [diff] [blame] | 2870 | /* |
| 2871 | * If the ordered extent had an IOERR or something else went |
| 2872 | * wrong we need to return the space for this ordered extent |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 2873 | * back to the allocator. We only free the extent in the |
| 2874 | * truncated case if we didn't write out the extent at all. |
Josef Bacik | 49940bd | 2018-10-11 15:54:21 -0400 | [diff] [blame] | 2875 | * |
| 2876 | * If we made it past insert_reserved_file_extent before we |
| 2877 | * errored out then we don't need to do this as the accounting |
| 2878 | * has already been done. |
Josef Bacik | 0bec9ef | 2013-01-31 14:58:00 -0500 | [diff] [blame] | 2879 | */ |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 2880 | if ((ret || !logical_len) && |
Josef Bacik | 49940bd | 2018-10-11 15:54:21 -0400 | [diff] [blame] | 2881 | clear_reserved_extent && |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 2882 | !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) && |
Nikolay Borisov | 4eaaec2 | 2019-11-21 14:03:29 +0200 | [diff] [blame] | 2883 | !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) { |
| 2884 | /* |
| 2885 | * Discard the range before returning it back to the |
| 2886 | * free space pool |
| 2887 | */ |
Dennis Zhou | 46b27f5 | 2019-12-13 16:22:11 -0800 | [diff] [blame] | 2888 | if (ret && btrfs_test_opt(fs_info, DISCARD_SYNC)) |
Nikolay Borisov | 4eaaec2 | 2019-11-21 14:03:29 +0200 | [diff] [blame] | 2889 | btrfs_discard_extent(fs_info, |
Omar Sandoval | bffe633 | 2019-12-02 17:34:19 -0800 | [diff] [blame] | 2890 | ordered_extent->disk_bytenr, |
| 2891 | ordered_extent->disk_num_bytes, |
| 2892 | NULL); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2893 | btrfs_free_reserved_extent(fs_info, |
Omar Sandoval | bffe633 | 2019-12-02 17:34:19 -0800 | [diff] [blame] | 2894 | ordered_extent->disk_bytenr, |
| 2895 | ordered_extent->disk_num_bytes, 1); |
Nikolay Borisov | 4eaaec2 | 2019-11-21 14:03:29 +0200 | [diff] [blame] | 2896 | } |
Josef Bacik | 0bec9ef | 2013-01-31 14:58:00 -0500 | [diff] [blame] | 2897 | } |
| 2898 | |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2899 | /* |
Liu Bo | 8bad3c0 | 2012-06-18 12:14:23 +0800 | [diff] [blame] | 2900 | * This needs to be done to make sure anybody waiting knows we are done |
| 2901 | * updating everything for this ordered extent. |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2902 | */ |
Nikolay Borisov | 72e7e6e | 2020-11-02 16:48:56 +0200 | [diff] [blame] | 2903 | btrfs_remove_ordered_extent(inode, ordered_extent); |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2904 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2905 | /* once for us */ |
| 2906 | btrfs_put_ordered_extent(ordered_extent); |
| 2907 | /* once for the tree */ |
| 2908 | btrfs_put_ordered_extent(ordered_extent); |
| 2909 | |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2910 | return ret; |
| 2911 | } |
| 2912 | |
| 2913 | static void finish_ordered_fn(struct btrfs_work *work) |
| 2914 | { |
| 2915 | struct btrfs_ordered_extent *ordered_extent; |
| 2916 | ordered_extent = container_of(work, struct btrfs_ordered_extent, work); |
| 2917 | btrfs_finish_ordered_io(ordered_extent); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2918 | } |
| 2919 | |
Nikolay Borisov | c629732 | 2018-11-08 10:18:08 +0200 | [diff] [blame] | 2920 | void btrfs_writepage_endio_finish_ordered(struct page *page, u64 start, |
| 2921 | u64 end, int uptodate) |
Chris Mason | 211f90e | 2008-07-18 11:56:15 -0400 | [diff] [blame] | 2922 | { |
Nikolay Borisov | 3347c48 | 2020-08-31 14:42:44 +0300 | [diff] [blame] | 2923 | struct btrfs_inode *inode = BTRFS_I(page->mapping->host); |
| 2924 | struct btrfs_fs_info *fs_info = inode->root->fs_info; |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2925 | struct btrfs_ordered_extent *ordered_extent = NULL; |
Liu Bo | 9e0af23 | 2014-08-15 23:36:53 +0800 | [diff] [blame] | 2926 | struct btrfs_workqueue *wq; |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2927 | |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 2928 | trace_btrfs_writepage_end_io_hook(page, start, end, uptodate); |
| 2929 | |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 2930 | ClearPagePrivate2(page); |
Nikolay Borisov | 3347c48 | 2020-08-31 14:42:44 +0300 | [diff] [blame] | 2931 | if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start, |
| 2932 | end - start + 1, uptodate)) |
David Sterba | c3988d6 | 2017-02-17 15:18:32 +0100 | [diff] [blame] | 2933 | return; |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2934 | |
Nikolay Borisov | 3347c48 | 2020-08-31 14:42:44 +0300 | [diff] [blame] | 2935 | if (btrfs_is_free_space_inode(inode)) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2936 | wq = fs_info->endio_freespace_worker; |
Omar Sandoval | a0cac0e | 2019-09-16 11:30:57 -0700 | [diff] [blame] | 2937 | else |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2938 | wq = fs_info->endio_write_workers; |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2939 | |
Omar Sandoval | a0cac0e | 2019-09-16 11:30:57 -0700 | [diff] [blame] | 2940 | btrfs_init_work(&ordered_extent->work, finish_ordered_fn, NULL, NULL); |
Liu Bo | 9e0af23 | 2014-08-15 23:36:53 +0800 | [diff] [blame] | 2941 | btrfs_queue_work(wq, &ordered_extent->work); |
Chris Mason | 211f90e | 2008-07-18 11:56:15 -0400 | [diff] [blame] | 2942 | } |
| 2943 | |
Qu Wenruo | 265d4ac | 2020-10-21 14:24:54 +0800 | [diff] [blame] | 2944 | /* |
| 2945 | * check_data_csum - verify checksum of one sector of uncompressed data |
| 2946 | * @inode: the inode |
| 2947 | * @io_bio: btrfs_io_bio which contains the csum |
| 2948 | * @icsum: checksum index in the io_bio->csum array, size of csum_size |
| 2949 | * @page: page where is the data to be verified |
| 2950 | * @pgoff: offset inside the page |
| 2951 | * |
| 2952 | * The length of such check is always one sector size. |
| 2953 | */ |
Omar Sandoval | 47df776 | 2020-04-16 14:46:17 -0700 | [diff] [blame] | 2954 | static int check_data_csum(struct inode *inode, struct btrfs_io_bio *io_bio, |
Qu Wenruo | 265d4ac | 2020-10-21 14:24:54 +0800 | [diff] [blame] | 2955 | int icsum, struct page *page, int pgoff) |
Miao Xie | dc380ae | 2014-09-12 18:43:55 +0800 | [diff] [blame] | 2956 | { |
Johannes Thumshirn | d517857 | 2019-06-03 16:58:57 +0200 | [diff] [blame] | 2957 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
| 2958 | SHASH_DESC_ON_STACK(shash, fs_info->csum_shash); |
Miao Xie | dc380ae | 2014-09-12 18:43:55 +0800 | [diff] [blame] | 2959 | char *kaddr; |
Qu Wenruo | 265d4ac | 2020-10-21 14:24:54 +0800 | [diff] [blame] | 2960 | u32 len = fs_info->sectorsize; |
David Sterba | 223486c | 2020-07-02 11:27:30 +0200 | [diff] [blame] | 2961 | const u32 csum_size = fs_info->csum_size; |
Johannes Thumshirn | d517857 | 2019-06-03 16:58:57 +0200 | [diff] [blame] | 2962 | u8 *csum_expected; |
| 2963 | u8 csum[BTRFS_CSUM_SIZE]; |
Miao Xie | dc380ae | 2014-09-12 18:43:55 +0800 | [diff] [blame] | 2964 | |
Qu Wenruo | 265d4ac | 2020-10-21 14:24:54 +0800 | [diff] [blame] | 2965 | ASSERT(pgoff + len <= PAGE_SIZE); |
| 2966 | |
Johannes Thumshirn | d517857 | 2019-06-03 16:58:57 +0200 | [diff] [blame] | 2967 | csum_expected = ((u8 *)io_bio->csum) + icsum * csum_size; |
Miao Xie | dc380ae | 2014-09-12 18:43:55 +0800 | [diff] [blame] | 2968 | |
| 2969 | kaddr = kmap_atomic(page); |
Johannes Thumshirn | d517857 | 2019-06-03 16:58:57 +0200 | [diff] [blame] | 2970 | shash->tfm = fs_info->csum_shash; |
| 2971 | |
Eric Biggers | fd08001 | 2020-04-30 23:51:59 -0700 | [diff] [blame] | 2972 | crypto_shash_digest(shash, kaddr + pgoff, len, csum); |
Johannes Thumshirn | d517857 | 2019-06-03 16:58:57 +0200 | [diff] [blame] | 2973 | |
| 2974 | if (memcmp(csum, csum_expected, csum_size)) |
Miao Xie | dc380ae | 2014-09-12 18:43:55 +0800 | [diff] [blame] | 2975 | goto zeroit; |
| 2976 | |
| 2977 | kunmap_atomic(kaddr); |
| 2978 | return 0; |
| 2979 | zeroit: |
Qu Wenruo | 265d4ac | 2020-10-21 14:24:54 +0800 | [diff] [blame] | 2980 | btrfs_print_data_csum_error(BTRFS_I(inode), page_offset(page) + pgoff, |
| 2981 | csum, csum_expected, io_bio->mirror_num); |
Nikolay Borisov | 814723e | 2020-07-02 15:23:32 +0300 | [diff] [blame] | 2982 | if (io_bio->device) |
| 2983 | btrfs_dev_stat_inc_and_print(io_bio->device, |
| 2984 | BTRFS_DEV_STAT_CORRUPTION_ERRS); |
Miao Xie | dc380ae | 2014-09-12 18:43:55 +0800 | [diff] [blame] | 2985 | memset(kaddr + pgoff, 1, len); |
| 2986 | flush_dcache_page(page); |
| 2987 | kunmap_atomic(kaddr); |
Miao Xie | dc380ae | 2014-09-12 18:43:55 +0800 | [diff] [blame] | 2988 | return -EIO; |
| 2989 | } |
| 2990 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2991 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2992 | * 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] | 2993 | * if there's a match, we allow the bio to finish. If not, the code in |
| 2994 | * extent_io.c will try to find good copies for us. |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2995 | */ |
Nikolay Borisov | 9a446d6 | 2020-09-18 16:34:33 +0300 | [diff] [blame] | 2996 | int btrfs_verify_data_csum(struct btrfs_io_bio *io_bio, u64 phy_offset, |
| 2997 | struct page *page, u64 start, u64 end, int mirror) |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 2998 | { |
Miao Xie | 4eee4fa | 2012-12-21 09:17:45 +0000 | [diff] [blame] | 2999 | size_t offset = start - page_offset(page); |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 3000 | struct inode *inode = page->mapping->host; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3001 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | ff79f81 | 2007-10-15 16:22:25 -0400 | [diff] [blame] | 3002 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3003 | |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 3004 | if (PageChecked(page)) { |
| 3005 | ClearPageChecked(page); |
Miao Xie | dc380ae | 2014-09-12 18:43:55 +0800 | [diff] [blame] | 3006 | return 0; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 3007 | } |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 3008 | |
| 3009 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM) |
Miao Xie | dc380ae | 2014-09-12 18:43:55 +0800 | [diff] [blame] | 3010 | return 0; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 3011 | |
Josef Bacik | 42437a6 | 2020-10-16 11:29:18 -0400 | [diff] [blame] | 3012 | if (!root->fs_info->csum_root) |
| 3013 | return 0; |
| 3014 | |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 3015 | if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID && |
Chris Mason | 9655d29 | 2009-09-02 15:22:30 -0400 | [diff] [blame] | 3016 | test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) { |
David Sterba | 9116621 | 2016-04-26 23:54:39 +0200 | [diff] [blame] | 3017 | clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM); |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 3018 | return 0; |
| 3019 | } |
| 3020 | |
David Sterba | 265fdfa | 2020-07-01 21:19:09 +0200 | [diff] [blame] | 3021 | phy_offset >>= root->fs_info->sectorsize_bits; |
Qu Wenruo | 265d4ac | 2020-10-21 14:24:54 +0800 | [diff] [blame] | 3022 | return check_data_csum(inode, io_bio, phy_offset, page, offset); |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 3023 | } |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3024 | |
Nikolay Borisov | c1c3fac | 2018-01-16 09:31:58 +0200 | [diff] [blame] | 3025 | /* |
| 3026 | * btrfs_add_delayed_iput - perform a delayed iput on @inode |
| 3027 | * |
| 3028 | * @inode: The inode we want to perform iput on |
| 3029 | * |
| 3030 | * This function uses the generic vfs_inode::i_count to track whether we should |
| 3031 | * just decrement it (in case it's > 1) or if this is the last iput then link |
| 3032 | * the inode to the delayed iput machinery. Delayed iputs are processed at |
| 3033 | * transaction commit time/superblock commit/cleaner kthread. |
| 3034 | */ |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 3035 | void btrfs_add_delayed_iput(struct inode *inode) |
| 3036 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3037 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
David Sterba | 8089fe6 | 2015-11-19 14:15:51 +0100 | [diff] [blame] | 3038 | struct btrfs_inode *binode = BTRFS_I(inode); |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 3039 | |
| 3040 | if (atomic_add_unless(&inode->i_count, -1, 1)) |
| 3041 | return; |
| 3042 | |
Josef Bacik | 034f784 | 2018-12-03 11:06:52 -0500 | [diff] [blame] | 3043 | atomic_inc(&fs_info->nr_delayed_iputs); |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 3044 | spin_lock(&fs_info->delayed_iput_lock); |
Nikolay Borisov | c1c3fac | 2018-01-16 09:31:58 +0200 | [diff] [blame] | 3045 | ASSERT(list_empty(&binode->delayed_iput)); |
| 3046 | list_add_tail(&binode->delayed_iput, &fs_info->delayed_iputs); |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 3047 | spin_unlock(&fs_info->delayed_iput_lock); |
Josef Bacik | fd340d0 | 2019-01-11 10:21:02 -0500 | [diff] [blame] | 3048 | if (!test_bit(BTRFS_FS_CLEANER_RUNNING, &fs_info->flags)) |
| 3049 | wake_up_process(fs_info->cleaner_kthread); |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 3050 | } |
| 3051 | |
Josef Bacik | 63611e7 | 2019-06-18 10:59:18 -0400 | [diff] [blame] | 3052 | static void run_delayed_iput_locked(struct btrfs_fs_info *fs_info, |
| 3053 | struct btrfs_inode *inode) |
| 3054 | { |
| 3055 | list_del_init(&inode->delayed_iput); |
| 3056 | spin_unlock(&fs_info->delayed_iput_lock); |
| 3057 | iput(&inode->vfs_inode); |
| 3058 | if (atomic_dec_and_test(&fs_info->nr_delayed_iputs)) |
| 3059 | wake_up(&fs_info->delayed_iputs_wait); |
| 3060 | spin_lock(&fs_info->delayed_iput_lock); |
| 3061 | } |
| 3062 | |
| 3063 | static void btrfs_run_delayed_iput(struct btrfs_fs_info *fs_info, |
| 3064 | struct btrfs_inode *inode) |
| 3065 | { |
| 3066 | if (!list_empty(&inode->delayed_iput)) { |
| 3067 | spin_lock(&fs_info->delayed_iput_lock); |
| 3068 | if (!list_empty(&inode->delayed_iput)) |
| 3069 | run_delayed_iput_locked(fs_info, inode); |
| 3070 | spin_unlock(&fs_info->delayed_iput_lock); |
| 3071 | } |
| 3072 | } |
| 3073 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3074 | void btrfs_run_delayed_iputs(struct btrfs_fs_info *fs_info) |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 3075 | { |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 3076 | |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 3077 | spin_lock(&fs_info->delayed_iput_lock); |
David Sterba | 8089fe6 | 2015-11-19 14:15:51 +0100 | [diff] [blame] | 3078 | while (!list_empty(&fs_info->delayed_iputs)) { |
| 3079 | struct btrfs_inode *inode; |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 3080 | |
David Sterba | 8089fe6 | 2015-11-19 14:15:51 +0100 | [diff] [blame] | 3081 | inode = list_first_entry(&fs_info->delayed_iputs, |
| 3082 | struct btrfs_inode, delayed_iput); |
Josef Bacik | 63611e7 | 2019-06-18 10:59:18 -0400 | [diff] [blame] | 3083 | run_delayed_iput_locked(fs_info, inode); |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 3084 | } |
David Sterba | 8089fe6 | 2015-11-19 14:15:51 +0100 | [diff] [blame] | 3085 | spin_unlock(&fs_info->delayed_iput_lock); |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 3086 | } |
| 3087 | |
Josef Bacik | 034f784 | 2018-12-03 11:06:52 -0500 | [diff] [blame] | 3088 | /** |
| 3089 | * btrfs_wait_on_delayed_iputs - wait on the delayed iputs to be done running |
| 3090 | * @fs_info - the fs_info for this fs |
| 3091 | * @return - EINTR if we were killed, 0 if nothing's pending |
| 3092 | * |
| 3093 | * This will wait on any delayed iputs that are currently running with KILLABLE |
| 3094 | * set. Once they are all done running we will return, unless we are killed in |
| 3095 | * which case we return EINTR. This helps in user operations like fallocate etc |
| 3096 | * that might get blocked on the iputs. |
| 3097 | */ |
| 3098 | int btrfs_wait_on_delayed_iputs(struct btrfs_fs_info *fs_info) |
| 3099 | { |
| 3100 | int ret = wait_event_killable(fs_info->delayed_iputs_wait, |
| 3101 | atomic_read(&fs_info->nr_delayed_iputs) == 0); |
| 3102 | if (ret) |
| 3103 | return -EINTR; |
| 3104 | return 0; |
| 3105 | } |
| 3106 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3107 | /* |
Omar Sandoval | f7e9e8f | 2018-05-11 13:13:32 -0700 | [diff] [blame] | 3108 | * This creates an orphan entry for the given inode in case something goes wrong |
| 3109 | * in the middle of an unlink. |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3110 | */ |
Nikolay Borisov | 73f2e54 | 2017-02-20 13:50:59 +0200 | [diff] [blame] | 3111 | int btrfs_orphan_add(struct btrfs_trans_handle *trans, |
Omar Sandoval | 2791906 | 2018-05-11 13:13:37 -0700 | [diff] [blame] | 3112 | struct btrfs_inode *inode) |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3113 | { |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3114 | int ret; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3115 | |
Omar Sandoval | 2791906 | 2018-05-11 13:13:37 -0700 | [diff] [blame] | 3116 | ret = btrfs_insert_orphan_item(trans, inode->root, btrfs_ino(inode)); |
| 3117 | if (ret && ret != -EEXIST) { |
| 3118 | btrfs_abort_transaction(trans, ret); |
| 3119 | return ret; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3120 | } |
| 3121 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3122 | return 0; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3123 | } |
| 3124 | |
| 3125 | /* |
Omar Sandoval | f7e9e8f | 2018-05-11 13:13:32 -0700 | [diff] [blame] | 3126 | * We have done the delete so we can go ahead and remove the orphan item for |
| 3127 | * this particular inode. |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3128 | */ |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 3129 | static int btrfs_orphan_del(struct btrfs_trans_handle *trans, |
Nikolay Borisov | 3d6ae7b | 2017-02-20 13:50:58 +0200 | [diff] [blame] | 3130 | struct btrfs_inode *inode) |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3131 | { |
Omar Sandoval | 2791906 | 2018-05-11 13:13:37 -0700 | [diff] [blame] | 3132 | return btrfs_del_orphan_item(trans, inode->root, btrfs_ino(inode)); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3133 | } |
| 3134 | |
| 3135 | /* |
| 3136 | * this cleans up any orphans that may be left on the list from the last use |
| 3137 | * of this root. |
| 3138 | */ |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3139 | int btrfs_orphan_cleanup(struct btrfs_root *root) |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3140 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3141 | struct btrfs_fs_info *fs_info = root->fs_info; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3142 | struct btrfs_path *path; |
| 3143 | struct extent_buffer *leaf; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3144 | struct btrfs_key key, found_key; |
| 3145 | struct btrfs_trans_handle *trans; |
| 3146 | struct inode *inode; |
Josef Bacik | 8f6d7f4 | 2011-09-26 15:55:20 -0400 | [diff] [blame] | 3147 | u64 last_objectid = 0; |
Omar Sandoval | f7e9e8f | 2018-05-11 13:13:32 -0700 | [diff] [blame] | 3148 | int ret = 0, nr_unlink = 0; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3149 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3150 | if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED)) |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3151 | return 0; |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 3152 | |
| 3153 | path = btrfs_alloc_path(); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3154 | if (!path) { |
| 3155 | ret = -ENOMEM; |
| 3156 | goto out; |
| 3157 | } |
David Sterba | e4058b5 | 2015-11-27 16:31:35 +0100 | [diff] [blame] | 3158 | path->reada = READA_BACK; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3159 | |
| 3160 | key.objectid = BTRFS_ORPHAN_OBJECTID; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 3161 | key.type = BTRFS_ORPHAN_ITEM_KEY; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3162 | key.offset = (u64)-1; |
| 3163 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3164 | while (1) { |
| 3165 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3166 | if (ret < 0) |
| 3167 | goto out; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3168 | |
| 3169 | /* |
| 3170 | * 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] | 3171 | * 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] | 3172 | * find the key and see if we have stuff that matches |
| 3173 | */ |
| 3174 | if (ret > 0) { |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3175 | ret = 0; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3176 | if (path->slots[0] == 0) |
| 3177 | break; |
| 3178 | path->slots[0]--; |
| 3179 | } |
| 3180 | |
| 3181 | /* pull out the item */ |
| 3182 | leaf = path->nodes[0]; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3183 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 3184 | |
| 3185 | /* make sure the item matches what we want */ |
| 3186 | if (found_key.objectid != BTRFS_ORPHAN_OBJECTID) |
| 3187 | break; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 3188 | if (found_key.type != BTRFS_ORPHAN_ITEM_KEY) |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3189 | break; |
| 3190 | |
| 3191 | /* release the path since we're done with it */ |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3192 | btrfs_release_path(path); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3193 | |
| 3194 | /* |
| 3195 | * this is where we are basically btrfs_lookup, without the |
| 3196 | * crossing root thing. we store the inode number in the |
| 3197 | * offset of the orphan item. |
| 3198 | */ |
Josef Bacik | 8f6d7f4 | 2011-09-26 15:55:20 -0400 | [diff] [blame] | 3199 | |
| 3200 | if (found_key.offset == last_objectid) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3201 | btrfs_err(fs_info, |
| 3202 | "Error removing orphan entry, stopping orphan cleanup"); |
Josef Bacik | 8f6d7f4 | 2011-09-26 15:55:20 -0400 | [diff] [blame] | 3203 | ret = -EINVAL; |
| 3204 | goto out; |
| 3205 | } |
| 3206 | |
| 3207 | last_objectid = found_key.offset; |
| 3208 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3209 | found_key.objectid = found_key.offset; |
| 3210 | found_key.type = BTRFS_INODE_ITEM_KEY; |
| 3211 | found_key.offset = 0; |
David Sterba | 0202e83 | 2020-05-15 19:35:59 +0200 | [diff] [blame] | 3212 | inode = btrfs_iget(fs_info->sb, last_objectid, root); |
Rusty Russell | 8c6ffba | 2013-07-15 11:20:32 +0930 | [diff] [blame] | 3213 | ret = PTR_ERR_OR_ZERO(inode); |
Filipe Manana | 6771089 | 2016-06-06 11:51:25 +0100 | [diff] [blame] | 3214 | if (ret && ret != -ENOENT) |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3215 | goto out; |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 3216 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3217 | if (ret == -ENOENT && root == fs_info->tree_root) { |
Arne Jansen | f8e9e0b | 2011-12-14 20:12:02 -0500 | [diff] [blame] | 3218 | struct btrfs_root *dead_root; |
Arne Jansen | f8e9e0b | 2011-12-14 20:12:02 -0500 | [diff] [blame] | 3219 | int is_dead_root = 0; |
| 3220 | |
| 3221 | /* |
| 3222 | * this is an orphan in the tree root. Currently these |
| 3223 | * could come from 2 sources: |
| 3224 | * a) a snapshot deletion in progress |
| 3225 | * b) a free space cache inode |
| 3226 | * We need to distinguish those two, as the snapshot |
| 3227 | * orphan must not get deleted. |
| 3228 | * find_dead_roots already ran before us, so if this |
| 3229 | * is a snapshot deletion, we should find the root |
Robbie Ko | a619b3c | 2020-05-07 10:54:40 +0800 | [diff] [blame] | 3230 | * in the fs_roots radix tree. |
Arne Jansen | f8e9e0b | 2011-12-14 20:12:02 -0500 | [diff] [blame] | 3231 | */ |
Robbie Ko | a619b3c | 2020-05-07 10:54:40 +0800 | [diff] [blame] | 3232 | |
| 3233 | spin_lock(&fs_info->fs_roots_radix_lock); |
| 3234 | dead_root = radix_tree_lookup(&fs_info->fs_roots_radix, |
| 3235 | (unsigned long)found_key.objectid); |
| 3236 | if (dead_root && btrfs_root_refs(&dead_root->root_item) == 0) |
| 3237 | is_dead_root = 1; |
| 3238 | spin_unlock(&fs_info->fs_roots_radix_lock); |
| 3239 | |
Arne Jansen | f8e9e0b | 2011-12-14 20:12:02 -0500 | [diff] [blame] | 3240 | if (is_dead_root) { |
| 3241 | /* prevent this orphan from being found again */ |
| 3242 | key.offset = found_key.objectid - 1; |
| 3243 | continue; |
| 3244 | } |
Omar Sandoval | f7e9e8f | 2018-05-11 13:13:32 -0700 | [diff] [blame] | 3245 | |
Arne Jansen | f8e9e0b | 2011-12-14 20:12:02 -0500 | [diff] [blame] | 3246 | } |
Omar Sandoval | f7e9e8f | 2018-05-11 13:13:32 -0700 | [diff] [blame] | 3247 | |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 3248 | /* |
Omar Sandoval | f7e9e8f | 2018-05-11 13:13:32 -0700 | [diff] [blame] | 3249 | * If we have an inode with links, there are a couple of |
| 3250 | * possibilities. Old kernels (before v3.12) used to create an |
| 3251 | * orphan item for truncate indicating that there were possibly |
| 3252 | * extent items past i_size that needed to be deleted. In v3.12, |
| 3253 | * truncate was changed to update i_size in sync with the extent |
| 3254 | * items, but the (useless) orphan item was still created. Since |
| 3255 | * v4.18, we don't create the orphan item for truncate at all. |
| 3256 | * |
| 3257 | * So, this item could mean that we need to do a truncate, but |
| 3258 | * only if this filesystem was last used on a pre-v3.12 kernel |
| 3259 | * and was not cleanly unmounted. The odds of that are quite |
| 3260 | * slim, and it's a pain to do the truncate now, so just delete |
| 3261 | * the orphan item. |
| 3262 | * |
| 3263 | * It's also possible that this orphan item was supposed to be |
| 3264 | * deleted but wasn't. The inode number may have been reused, |
| 3265 | * but either way, we can delete the orphan item. |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 3266 | */ |
Omar Sandoval | f7e9e8f | 2018-05-11 13:13:32 -0700 | [diff] [blame] | 3267 | if (ret == -ENOENT || inode->i_nlink) { |
| 3268 | if (!ret) |
| 3269 | iput(inode); |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 3270 | trans = btrfs_start_transaction(root, 1); |
| 3271 | if (IS_ERR(trans)) { |
| 3272 | ret = PTR_ERR(trans); |
| 3273 | goto out; |
| 3274 | } |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3275 | btrfs_debug(fs_info, "auto deleting %Lu", |
| 3276 | found_key.objectid); |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 3277 | ret = btrfs_del_orphan_item(trans, root, |
| 3278 | found_key.objectid); |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 3279 | btrfs_end_transaction(trans); |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 3280 | if (ret) |
| 3281 | goto out; |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 3282 | continue; |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3283 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3284 | |
Omar Sandoval | f7e9e8f | 2018-05-11 13:13:32 -0700 | [diff] [blame] | 3285 | nr_unlink++; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3286 | |
| 3287 | /* this will do delete_inode and everything for us */ |
| 3288 | iput(inode); |
| 3289 | } |
Miao Xie | 3254c87 | 2011-11-10 20:45:05 -0500 | [diff] [blame] | 3290 | /* release the path since we're done with it */ |
| 3291 | btrfs_release_path(path); |
| 3292 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3293 | root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE; |
| 3294 | |
Omar Sandoval | a575cee | 2018-05-11 13:13:38 -0700 | [diff] [blame] | 3295 | if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) { |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 3296 | trans = btrfs_join_transaction(root); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3297 | if (!IS_ERR(trans)) |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 3298 | btrfs_end_transaction(trans); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3299 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3300 | |
| 3301 | if (nr_unlink) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3302 | btrfs_debug(fs_info, "unlinked %d orphans", nr_unlink); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3303 | |
| 3304 | out: |
| 3305 | if (ret) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3306 | btrfs_err(fs_info, "could not do orphan cleanup %d", ret); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3307 | btrfs_free_path(path); |
| 3308 | return ret; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3309 | } |
| 3310 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3311 | /* |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3312 | * very simple check to peek ahead in the leaf looking for xattrs. If we |
| 3313 | * don't find any xattrs, we know there can't be any acls. |
| 3314 | * |
| 3315 | * slot is the slot the inode is in, objectid is the objectid of the inode |
| 3316 | */ |
| 3317 | 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] | 3318 | int slot, u64 objectid, |
| 3319 | int *first_xattr_slot) |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3320 | { |
| 3321 | u32 nritems = btrfs_header_nritems(leaf); |
| 3322 | struct btrfs_key found_key; |
Josef Bacik | f23b5a5 | 2013-06-19 10:16:26 -0400 | [diff] [blame] | 3323 | static u64 xattr_access = 0; |
| 3324 | static u64 xattr_default = 0; |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3325 | int scanned = 0; |
| 3326 | |
Josef Bacik | f23b5a5 | 2013-06-19 10:16:26 -0400 | [diff] [blame] | 3327 | if (!xattr_access) { |
Andreas Gruenbacher | 97d7929 | 2015-12-02 14:44:35 +0100 | [diff] [blame] | 3328 | xattr_access = btrfs_name_hash(XATTR_NAME_POSIX_ACL_ACCESS, |
| 3329 | strlen(XATTR_NAME_POSIX_ACL_ACCESS)); |
| 3330 | xattr_default = btrfs_name_hash(XATTR_NAME_POSIX_ACL_DEFAULT, |
| 3331 | strlen(XATTR_NAME_POSIX_ACL_DEFAULT)); |
Josef Bacik | f23b5a5 | 2013-06-19 10:16:26 -0400 | [diff] [blame] | 3332 | } |
| 3333 | |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3334 | slot++; |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 3335 | *first_xattr_slot = -1; |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3336 | while (slot < nritems) { |
| 3337 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
| 3338 | |
| 3339 | /* we found a different objectid, there must not be acls */ |
| 3340 | if (found_key.objectid != objectid) |
| 3341 | return 0; |
| 3342 | |
| 3343 | /* we found an xattr, assume we've got an acl */ |
Josef Bacik | f23b5a5 | 2013-06-19 10:16:26 -0400 | [diff] [blame] | 3344 | if (found_key.type == BTRFS_XATTR_ITEM_KEY) { |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 3345 | if (*first_xattr_slot == -1) |
| 3346 | *first_xattr_slot = slot; |
Josef Bacik | f23b5a5 | 2013-06-19 10:16:26 -0400 | [diff] [blame] | 3347 | if (found_key.offset == xattr_access || |
| 3348 | found_key.offset == xattr_default) |
| 3349 | return 1; |
| 3350 | } |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3351 | |
| 3352 | /* |
| 3353 | * we found a key greater than an xattr key, there can't |
| 3354 | * be any acls later on |
| 3355 | */ |
| 3356 | if (found_key.type > BTRFS_XATTR_ITEM_KEY) |
| 3357 | return 0; |
| 3358 | |
| 3359 | slot++; |
| 3360 | scanned++; |
| 3361 | |
| 3362 | /* |
| 3363 | * it goes inode, inode backrefs, xattrs, extents, |
| 3364 | * so if there are a ton of hard links to an inode there can |
| 3365 | * be a lot of backrefs. Don't waste time searching too hard, |
| 3366 | * this is just an optimization |
| 3367 | */ |
| 3368 | if (scanned >= 8) |
| 3369 | break; |
| 3370 | } |
| 3371 | /* we hit the end of the leaf before we found an xattr or |
| 3372 | * something larger than an xattr. We have to assume the inode |
| 3373 | * has acls |
| 3374 | */ |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 3375 | if (*first_xattr_slot == -1) |
| 3376 | *first_xattr_slot = slot; |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3377 | return 1; |
| 3378 | } |
| 3379 | |
| 3380 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3381 | * read an inode from the btree into the in-memory inode |
| 3382 | */ |
Filipe Manana | 4222ea7 | 2018-10-24 10:13:03 +0100 | [diff] [blame] | 3383 | static int btrfs_read_locked_inode(struct inode *inode, |
| 3384 | struct btrfs_path *in_path) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3385 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3386 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Filipe Manana | 4222ea7 | 2018-10-24 10:13:03 +0100 | [diff] [blame] | 3387 | struct btrfs_path *path = in_path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3388 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3389 | struct btrfs_inode_item *inode_item; |
| 3390 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 3391 | struct btrfs_key location; |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 3392 | unsigned long ptr; |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3393 | int maybe_acls; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 3394 | u32 rdev; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3395 | int ret; |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 3396 | bool filled = false; |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 3397 | int first_xattr_slot; |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 3398 | |
| 3399 | ret = btrfs_fill_inode(inode, &rdev); |
| 3400 | if (!ret) |
| 3401 | filled = true; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3402 | |
Filipe Manana | 4222ea7 | 2018-10-24 10:13:03 +0100 | [diff] [blame] | 3403 | if (!path) { |
| 3404 | path = btrfs_alloc_path(); |
| 3405 | if (!path) |
| 3406 | return -ENOMEM; |
| 3407 | } |
Mark Fasheh | 1748f84 | 2011-07-12 11:25:31 -0700 | [diff] [blame] | 3408 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3409 | memcpy(&location, &BTRFS_I(inode)->location, sizeof(location)); |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 3410 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3411 | ret = btrfs_lookup_inode(NULL, root, path, &location, 0); |
Filipe Manana | 6771089 | 2016-06-06 11:51:25 +0100 | [diff] [blame] | 3412 | if (ret) { |
Filipe Manana | 4222ea7 | 2018-10-24 10:13:03 +0100 | [diff] [blame] | 3413 | if (path != in_path) |
| 3414 | btrfs_free_path(path); |
Al Viro | f5b3a41 | 2018-07-29 23:04:51 +0100 | [diff] [blame] | 3415 | return ret; |
Filipe Manana | 6771089 | 2016-06-06 11:51:25 +0100 | [diff] [blame] | 3416 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3417 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3418 | leaf = path->nodes[0]; |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 3419 | |
| 3420 | if (filled) |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 3421 | goto cache_index; |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 3422 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3423 | inode_item = btrfs_item_ptr(leaf, path->slots[0], |
| 3424 | struct btrfs_inode_item); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3425 | inode->i_mode = btrfs_inode_mode(leaf, inode_item); |
Miklos Szeredi | bfe8684 | 2011-10-28 14:13:29 +0200 | [diff] [blame] | 3426 | set_nlink(inode, btrfs_inode_nlink(leaf, inode_item)); |
Eric W. Biederman | 2f2f43d | 2012-02-10 11:05:07 -0800 | [diff] [blame] | 3427 | i_uid_write(inode, btrfs_inode_uid(leaf, inode_item)); |
| 3428 | i_gid_write(inode, btrfs_inode_gid(leaf, inode_item)); |
Nikolay Borisov | 6ef06d2 | 2017-02-20 13:50:34 +0200 | [diff] [blame] | 3429 | 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] | 3430 | btrfs_inode_set_file_extent_range(BTRFS_I(inode), 0, |
| 3431 | round_up(i_size_read(inode), fs_info->sectorsize)); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3432 | |
David Sterba | a937b97 | 2014-12-12 17:39:12 +0100 | [diff] [blame] | 3433 | inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->atime); |
| 3434 | inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->atime); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3435 | |
David Sterba | a937b97 | 2014-12-12 17:39:12 +0100 | [diff] [blame] | 3436 | inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->mtime); |
| 3437 | inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->mtime); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3438 | |
David Sterba | a937b97 | 2014-12-12 17:39:12 +0100 | [diff] [blame] | 3439 | inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->ctime); |
| 3440 | inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->ctime); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3441 | |
chandan r | 9cc97d6 | 2012-07-04 12:48:07 +0530 | [diff] [blame] | 3442 | BTRFS_I(inode)->i_otime.tv_sec = |
| 3443 | btrfs_timespec_sec(leaf, &inode_item->otime); |
| 3444 | BTRFS_I(inode)->i_otime.tv_nsec = |
| 3445 | btrfs_timespec_nsec(leaf, &inode_item->otime); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3446 | |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 3447 | inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item)); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3448 | BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item); |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 3449 | BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item); |
| 3450 | |
Jeff Layton | c7f88c4 | 2017-12-11 06:35:12 -0500 | [diff] [blame] | 3451 | inode_set_iversion_queried(inode, |
| 3452 | btrfs_inode_sequence(leaf, inode_item)); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3453 | inode->i_generation = BTRFS_I(inode)->generation; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 3454 | inode->i_rdev = 0; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3455 | rdev = btrfs_inode_rdev(leaf, inode_item); |
| 3456 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 3457 | BTRFS_I(inode)->index_cnt = (u64)-1; |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 3458 | BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item); |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 3459 | |
| 3460 | cache_index: |
Yang Dongsheng | 6e17d30 | 2015-04-09 12:08:43 +0800 | [diff] [blame] | 3461 | /* |
| 3462 | * If we were modified in the current generation and evicted from memory |
| 3463 | * and then re-read we need to do a full sync since we don't have any |
| 3464 | * idea about which extents were modified before we were evicted from |
| 3465 | * cache. |
| 3466 | * |
| 3467 | * This is required for both inode re-read from disk and delayed inode |
| 3468 | * in delayed_nodes_tree. |
| 3469 | */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3470 | if (BTRFS_I(inode)->last_trans == fs_info->generation) |
Yang Dongsheng | 6e17d30 | 2015-04-09 12:08:43 +0800 | [diff] [blame] | 3471 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, |
| 3472 | &BTRFS_I(inode)->runtime_flags); |
| 3473 | |
Filipe Manana | bde6c24 | 2015-07-24 00:00:19 +0100 | [diff] [blame] | 3474 | /* |
| 3475 | * We don't persist the id of the transaction where an unlink operation |
| 3476 | * against the inode was last made. So here we assume the inode might |
| 3477 | * have been evicted, and therefore the exact value of last_unlink_trans |
| 3478 | * lost, and set it to last_trans to avoid metadata inconsistencies |
| 3479 | * between the inode and its parent if the inode is fsync'ed and the log |
| 3480 | * replayed. For example, in the scenario: |
| 3481 | * |
| 3482 | * touch mydir/foo |
| 3483 | * ln mydir/foo mydir/bar |
| 3484 | * sync |
| 3485 | * unlink mydir/bar |
| 3486 | * echo 2 > /proc/sys/vm/drop_caches # evicts inode |
| 3487 | * xfs_io -c fsync mydir/foo |
| 3488 | * <power failure> |
| 3489 | * mount fs, triggers fsync log replay |
| 3490 | * |
| 3491 | * We must make sure that when we fsync our inode foo we also log its |
| 3492 | * parent inode, otherwise after log replay the parent still has the |
| 3493 | * dentry with the "bar" name but our inode foo has a link count of 1 |
| 3494 | * and doesn't have an inode ref with the name "bar" anymore. |
| 3495 | * |
| 3496 | * Setting last_unlink_trans to last_trans is a pessimistic approach, |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 3497 | * but it guarantees correctness at the expense of occasional full |
Filipe Manana | bde6c24 | 2015-07-24 00:00:19 +0100 | [diff] [blame] | 3498 | * transaction commits on fsync if our inode is a directory, or if our |
| 3499 | * inode is not a directory, logging its parent unnecessarily. |
| 3500 | */ |
| 3501 | BTRFS_I(inode)->last_unlink_trans = BTRFS_I(inode)->last_trans; |
| 3502 | |
Filipe Manana | 3ebac17 | 2020-07-15 12:30:43 +0100 | [diff] [blame] | 3503 | /* |
| 3504 | * Same logic as for last_unlink_trans. We don't persist the generation |
| 3505 | * of the last transaction where this inode was used for a reflink |
| 3506 | * operation, so after eviction and reloading the inode we must be |
| 3507 | * pessimistic and assume the last transaction that modified the inode. |
| 3508 | */ |
| 3509 | BTRFS_I(inode)->last_reflink_trans = BTRFS_I(inode)->last_trans; |
| 3510 | |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 3511 | path->slots[0]++; |
| 3512 | if (inode->i_nlink != 1 || |
| 3513 | path->slots[0] >= btrfs_header_nritems(leaf)) |
| 3514 | goto cache_acl; |
| 3515 | |
| 3516 | btrfs_item_key_to_cpu(leaf, &location, path->slots[0]); |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 3517 | if (location.objectid != btrfs_ino(BTRFS_I(inode))) |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 3518 | goto cache_acl; |
| 3519 | |
| 3520 | ptr = btrfs_item_ptr_offset(leaf, path->slots[0]); |
| 3521 | if (location.type == BTRFS_INODE_REF_KEY) { |
| 3522 | struct btrfs_inode_ref *ref; |
| 3523 | |
| 3524 | ref = (struct btrfs_inode_ref *)ptr; |
| 3525 | BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref); |
| 3526 | } else if (location.type == BTRFS_INODE_EXTREF_KEY) { |
| 3527 | struct btrfs_inode_extref *extref; |
| 3528 | |
| 3529 | extref = (struct btrfs_inode_extref *)ptr; |
| 3530 | BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf, |
| 3531 | extref); |
| 3532 | } |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 3533 | cache_acl: |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3534 | /* |
| 3535 | * try to precache a NULL acl entry for files that don't have |
| 3536 | * any xattrs or acls |
| 3537 | */ |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3538 | maybe_acls = acls_after_inode_item(leaf, path->slots[0], |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 3539 | btrfs_ino(BTRFS_I(inode)), &first_xattr_slot); |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 3540 | if (first_xattr_slot != -1) { |
| 3541 | path->slots[0] = first_xattr_slot; |
| 3542 | ret = btrfs_load_inode_props(inode, path); |
| 3543 | if (ret) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3544 | btrfs_err(fs_info, |
David Sterba | 351fd35 | 2014-05-15 16:48:20 +0200 | [diff] [blame] | 3545 | "error loading props for ino %llu (root %llu): %d", |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 3546 | btrfs_ino(BTRFS_I(inode)), |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 3547 | root->root_key.objectid, ret); |
| 3548 | } |
Filipe Manana | 4222ea7 | 2018-10-24 10:13:03 +0100 | [diff] [blame] | 3549 | if (path != in_path) |
| 3550 | btrfs_free_path(path); |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 3551 | |
Al Viro | 72c0490 | 2009-06-24 16:58:48 -0400 | [diff] [blame] | 3552 | if (!maybe_acls) |
| 3553 | cache_no_acl(inode); |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3554 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3555 | switch (inode->i_mode & S_IFMT) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3556 | case S_IFREG: |
| 3557 | inode->i_mapping->a_ops = &btrfs_aops; |
| 3558 | inode->i_fop = &btrfs_file_operations; |
| 3559 | inode->i_op = &btrfs_file_inode_operations; |
| 3560 | break; |
| 3561 | case S_IFDIR: |
| 3562 | inode->i_fop = &btrfs_dir_file_operations; |
Omar Sandoval | 67ade05 | 2017-01-25 17:06:38 -0800 | [diff] [blame] | 3563 | inode->i_op = &btrfs_dir_inode_operations; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3564 | break; |
| 3565 | case S_IFLNK: |
| 3566 | inode->i_op = &btrfs_symlink_inode_operations; |
Al Viro | 21fc61c | 2015-11-17 01:07:57 -0500 | [diff] [blame] | 3567 | inode_nohighmem(inode); |
Omar Sandoval | 4779cc0 | 2018-09-24 15:16:55 -0700 | [diff] [blame] | 3568 | inode->i_mapping->a_ops = &btrfs_aops; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3569 | break; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 3570 | default: |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 3571 | inode->i_op = &btrfs_special_inode_operations; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 3572 | init_special_inode(inode, inode->i_mode, rdev); |
| 3573 | break; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3574 | } |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 3575 | |
David Sterba | 7b6a221 | 2018-03-26 18:40:21 +0200 | [diff] [blame] | 3576 | btrfs_sync_inode_flags_to_i_flags(inode); |
Filipe Manana | 6771089 | 2016-06-06 11:51:25 +0100 | [diff] [blame] | 3577 | return 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3578 | } |
| 3579 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3580 | /* |
| 3581 | * given a leaf and an inode, copy the inode fields into the leaf |
| 3582 | */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3583 | static void fill_inode_item(struct btrfs_trans_handle *trans, |
| 3584 | struct extent_buffer *leaf, |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3585 | struct btrfs_inode_item *item, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3586 | struct inode *inode) |
| 3587 | { |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3588 | struct btrfs_map_token token; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3589 | |
David Sterba | c82f823 | 2019-08-09 17:48:21 +0200 | [diff] [blame] | 3590 | btrfs_init_map_token(&token, leaf); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3591 | |
David Sterba | cc4c13d | 2020-04-29 02:15:56 +0200 | [diff] [blame] | 3592 | btrfs_set_token_inode_uid(&token, item, i_uid_read(inode)); |
| 3593 | btrfs_set_token_inode_gid(&token, item, i_gid_read(inode)); |
| 3594 | btrfs_set_token_inode_size(&token, item, BTRFS_I(inode)->disk_i_size); |
| 3595 | btrfs_set_token_inode_mode(&token, item, inode->i_mode); |
| 3596 | btrfs_set_token_inode_nlink(&token, item, inode->i_nlink); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3597 | |
David Sterba | cc4c13d | 2020-04-29 02:15:56 +0200 | [diff] [blame] | 3598 | btrfs_set_token_timespec_sec(&token, &item->atime, |
| 3599 | inode->i_atime.tv_sec); |
| 3600 | btrfs_set_token_timespec_nsec(&token, &item->atime, |
| 3601 | inode->i_atime.tv_nsec); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3602 | |
David Sterba | cc4c13d | 2020-04-29 02:15:56 +0200 | [diff] [blame] | 3603 | btrfs_set_token_timespec_sec(&token, &item->mtime, |
| 3604 | inode->i_mtime.tv_sec); |
| 3605 | btrfs_set_token_timespec_nsec(&token, &item->mtime, |
| 3606 | inode->i_mtime.tv_nsec); |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3607 | |
David Sterba | cc4c13d | 2020-04-29 02:15:56 +0200 | [diff] [blame] | 3608 | btrfs_set_token_timespec_sec(&token, &item->ctime, |
| 3609 | inode->i_ctime.tv_sec); |
| 3610 | btrfs_set_token_timespec_nsec(&token, &item->ctime, |
| 3611 | inode->i_ctime.tv_nsec); |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3612 | |
David Sterba | cc4c13d | 2020-04-29 02:15:56 +0200 | [diff] [blame] | 3613 | btrfs_set_token_timespec_sec(&token, &item->otime, |
| 3614 | BTRFS_I(inode)->i_otime.tv_sec); |
| 3615 | btrfs_set_token_timespec_nsec(&token, &item->otime, |
| 3616 | BTRFS_I(inode)->i_otime.tv_nsec); |
chandan r | 9cc97d6 | 2012-07-04 12:48:07 +0530 | [diff] [blame] | 3617 | |
David Sterba | cc4c13d | 2020-04-29 02:15:56 +0200 | [diff] [blame] | 3618 | btrfs_set_token_inode_nbytes(&token, item, inode_get_bytes(inode)); |
| 3619 | btrfs_set_token_inode_generation(&token, item, |
| 3620 | BTRFS_I(inode)->generation); |
| 3621 | btrfs_set_token_inode_sequence(&token, item, inode_peek_iversion(inode)); |
| 3622 | btrfs_set_token_inode_transid(&token, item, trans->transid); |
| 3623 | btrfs_set_token_inode_rdev(&token, item, inode->i_rdev); |
| 3624 | btrfs_set_token_inode_flags(&token, item, BTRFS_I(inode)->flags); |
| 3625 | btrfs_set_token_inode_block_group(&token, item, 0); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3626 | } |
| 3627 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3628 | /* |
| 3629 | * copy everything in the in-memory inode into the btree. |
| 3630 | */ |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 3631 | static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans, |
Nikolay Borisov | dfeb9e7 | 2020-11-02 16:48:58 +0200 | [diff] [blame] | 3632 | struct btrfs_root *root, |
| 3633 | struct btrfs_inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3634 | { |
| 3635 | struct btrfs_inode_item *inode_item; |
| 3636 | struct btrfs_path *path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3637 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3638 | int ret; |
| 3639 | |
| 3640 | path = btrfs_alloc_path(); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 3641 | if (!path) |
| 3642 | return -ENOMEM; |
| 3643 | |
Nikolay Borisov | dfeb9e7 | 2020-11-02 16:48:58 +0200 | [diff] [blame] | 3644 | ret = btrfs_lookup_inode(trans, root, path, &inode->location, 1); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3645 | if (ret) { |
| 3646 | if (ret > 0) |
| 3647 | ret = -ENOENT; |
| 3648 | goto failed; |
| 3649 | } |
| 3650 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3651 | leaf = path->nodes[0]; |
| 3652 | inode_item = btrfs_item_ptr(leaf, path->slots[0], |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 3653 | struct btrfs_inode_item); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3654 | |
Nikolay Borisov | dfeb9e7 | 2020-11-02 16:48:58 +0200 | [diff] [blame] | 3655 | fill_inode_item(trans, leaf, inode_item, &inode->vfs_inode); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3656 | btrfs_mark_buffer_dirty(leaf); |
Nikolay Borisov | dfeb9e7 | 2020-11-02 16:48:58 +0200 | [diff] [blame] | 3657 | btrfs_set_inode_last_trans(trans, inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3658 | ret = 0; |
| 3659 | failed: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3660 | btrfs_free_path(path); |
| 3661 | return ret; |
| 3662 | } |
| 3663 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3664 | /* |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 3665 | * copy everything in the in-memory inode into the btree. |
| 3666 | */ |
| 3667 | noinline int btrfs_update_inode(struct btrfs_trans_handle *trans, |
Nikolay Borisov | 9a56fcd | 2020-11-02 16:48:59 +0200 | [diff] [blame] | 3668 | struct btrfs_root *root, |
| 3669 | struct btrfs_inode *inode) |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 3670 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3671 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 3672 | int ret; |
| 3673 | |
| 3674 | /* |
| 3675 | * If the inode is a free space inode, we can deadlock during commit |
| 3676 | * if we put it into the delayed code. |
| 3677 | * |
| 3678 | * The data relocation inode should also be directly updated |
| 3679 | * without delay |
| 3680 | */ |
Nikolay Borisov | 9a56fcd | 2020-11-02 16:48:59 +0200 | [diff] [blame] | 3681 | if (!btrfs_is_free_space_inode(inode) |
Josef Bacik | 1d52c78 | 2014-09-18 11:30:44 -0400 | [diff] [blame] | 3682 | && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3683 | && !test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) { |
Alexander Block | 8ea05e3 | 2012-07-25 17:35:53 +0200 | [diff] [blame] | 3684 | btrfs_update_root_times(trans, root); |
| 3685 | |
Nikolay Borisov | 9a56fcd | 2020-11-02 16:48:59 +0200 | [diff] [blame] | 3686 | ret = btrfs_delayed_update_inode(trans, root, inode); |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 3687 | if (!ret) |
Nikolay Borisov | 9a56fcd | 2020-11-02 16:48:59 +0200 | [diff] [blame] | 3688 | btrfs_set_inode_last_trans(trans, inode); |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 3689 | return ret; |
| 3690 | } |
| 3691 | |
Nikolay Borisov | 9a56fcd | 2020-11-02 16:48:59 +0200 | [diff] [blame] | 3692 | return btrfs_update_inode_item(trans, root, inode); |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 3693 | } |
| 3694 | |
Nikolay Borisov | 729f796 | 2020-11-02 16:49:06 +0200 | [diff] [blame] | 3695 | int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans, |
| 3696 | struct btrfs_root *root, struct btrfs_inode *inode) |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 3697 | { |
| 3698 | int ret; |
| 3699 | |
Nikolay Borisov | 729f796 | 2020-11-02 16:49:06 +0200 | [diff] [blame] | 3700 | ret = btrfs_update_inode(trans, root, inode); |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 3701 | if (ret == -ENOSPC) |
Nikolay Borisov | 729f796 | 2020-11-02 16:49:06 +0200 | [diff] [blame] | 3702 | return btrfs_update_inode_item(trans, root, inode); |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 3703 | return ret; |
| 3704 | } |
| 3705 | |
| 3706 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3707 | * unlink helper that gets used here in inode.c and in the tree logging |
| 3708 | * recovery code. It remove a link in a directory with a given name, and |
| 3709 | * also drops the back refs in the inode to the directory |
| 3710 | */ |
Al Viro | 9298679 | 2011-03-04 17:14:37 +0000 | [diff] [blame] | 3711 | static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans, |
| 3712 | struct btrfs_root *root, |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 3713 | struct btrfs_inode *dir, |
| 3714 | struct btrfs_inode *inode, |
Al Viro | 9298679 | 2011-03-04 17:14:37 +0000 | [diff] [blame] | 3715 | const char *name, int name_len) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3716 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3717 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3718 | struct btrfs_path *path; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3719 | int ret = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3720 | struct btrfs_dir_item *di; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 3721 | u64 index; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3722 | u64 ino = btrfs_ino(inode); |
| 3723 | u64 dir_ino = btrfs_ino(dir); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3724 | |
| 3725 | path = btrfs_alloc_path(); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3726 | if (!path) { |
| 3727 | ret = -ENOMEM; |
Tsutomu Itoh | 554233a | 2011-02-03 03:16:25 +0000 | [diff] [blame] | 3728 | goto out; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3729 | } |
| 3730 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3731 | di = btrfs_lookup_dir_item(trans, root, path, dir_ino, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3732 | name, name_len, -1); |
Liu Bo | 3cf5068 | 2018-09-12 06:06:26 +0800 | [diff] [blame] | 3733 | if (IS_ERR_OR_NULL(di)) { |
| 3734 | ret = di ? PTR_ERR(di) : -ENOENT; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3735 | goto err; |
| 3736 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3737 | ret = btrfs_delete_one_dir_name(trans, root, path, di); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3738 | if (ret) |
| 3739 | goto err; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3740 | btrfs_release_path(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3741 | |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 3742 | /* |
| 3743 | * If we don't have dir index, we have to get it by looking up |
| 3744 | * the inode ref, since we get the inode ref, remove it directly, |
| 3745 | * it is unnecessary to do delayed deletion. |
| 3746 | * |
| 3747 | * But if we have dir index, needn't search inode ref to get it. |
| 3748 | * Since the inode ref is close to the inode item, it is better |
| 3749 | * that we delay to delete it, and just do this deletion when |
| 3750 | * we update the inode item. |
| 3751 | */ |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 3752 | if (inode->dir_index) { |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 3753 | ret = btrfs_delayed_delete_inode_ref(inode); |
| 3754 | if (!ret) { |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 3755 | index = inode->dir_index; |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 3756 | goto skip_backref; |
| 3757 | } |
| 3758 | } |
| 3759 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3760 | ret = btrfs_del_inode_ref(trans, root, name, name_len, ino, |
| 3761 | dir_ino, &index); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 3762 | if (ret) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3763 | btrfs_info(fs_info, |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 3764 | "failed to delete reference to %.*s, inode %llu parent %llu", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 3765 | name_len, name, ino, dir_ino); |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3766 | btrfs_abort_transaction(trans, ret); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 3767 | goto err; |
| 3768 | } |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 3769 | skip_backref: |
Lu Fengqi | 9add294 | 2018-08-01 11:32:26 +0800 | [diff] [blame] | 3770 | ret = btrfs_delete_delayed_dir_index(trans, dir, index); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3771 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3772 | btrfs_abort_transaction(trans, ret); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3773 | goto err; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3774 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3775 | |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 3776 | ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len, inode, |
| 3777 | dir_ino); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3778 | if (ret != 0 && ret != -ENOENT) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3779 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3780 | goto err; |
| 3781 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3782 | |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 3783 | ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len, dir, |
| 3784 | index); |
Chris Mason | 6418c96 | 2010-10-30 07:34:24 -0400 | [diff] [blame] | 3785 | if (ret == -ENOENT) |
| 3786 | ret = 0; |
Zach Brown | d4e3991 | 2013-04-02 21:02:16 +0000 | [diff] [blame] | 3787 | else if (ret) |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3788 | btrfs_abort_transaction(trans, ret); |
Josef Bacik | 63611e7 | 2019-06-18 10:59:18 -0400 | [diff] [blame] | 3789 | |
| 3790 | /* |
| 3791 | * If we have a pending delayed iput we could end up with the final iput |
| 3792 | * being run in btrfs-cleaner context. If we have enough of these built |
| 3793 | * up we can end up burning a lot of time in btrfs-cleaner without any |
| 3794 | * way to throttle the unlinks. Since we're currently holding a ref on |
| 3795 | * the inode we can run the delayed iput here without any issues as the |
| 3796 | * final iput won't be done until after we drop the ref we're currently |
| 3797 | * holding. |
| 3798 | */ |
| 3799 | btrfs_run_delayed_iput(fs_info, inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3800 | err: |
| 3801 | btrfs_free_path(path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3802 | if (ret) |
| 3803 | goto out; |
| 3804 | |
Nikolay Borisov | 6ef06d2 | 2017-02-20 13:50:34 +0200 | [diff] [blame] | 3805 | 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] | 3806 | inode_inc_iversion(&inode->vfs_inode); |
| 3807 | inode_inc_iversion(&dir->vfs_inode); |
| 3808 | inode->vfs_inode.i_ctime = dir->vfs_inode.i_mtime = |
| 3809 | dir->vfs_inode.i_ctime = current_time(&inode->vfs_inode); |
Nikolay Borisov | 9a56fcd | 2020-11-02 16:48:59 +0200 | [diff] [blame] | 3810 | ret = btrfs_update_inode(trans, root, dir); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3811 | out: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3812 | return ret; |
| 3813 | } |
| 3814 | |
Al Viro | 9298679 | 2011-03-04 17:14:37 +0000 | [diff] [blame] | 3815 | int btrfs_unlink_inode(struct btrfs_trans_handle *trans, |
| 3816 | struct btrfs_root *root, |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 3817 | struct btrfs_inode *dir, struct btrfs_inode *inode, |
Al Viro | 9298679 | 2011-03-04 17:14:37 +0000 | [diff] [blame] | 3818 | const char *name, int name_len) |
| 3819 | { |
| 3820 | int ret; |
| 3821 | ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len); |
| 3822 | if (!ret) { |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 3823 | drop_nlink(&inode->vfs_inode); |
Nikolay Borisov | 9a56fcd | 2020-11-02 16:48:59 +0200 | [diff] [blame] | 3824 | ret = btrfs_update_inode(trans, root, inode); |
Al Viro | 9298679 | 2011-03-04 17:14:37 +0000 | [diff] [blame] | 3825 | } |
| 3826 | return ret; |
| 3827 | } |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3828 | |
| 3829 | /* |
| 3830 | * helper to start transaction for unlink and rmdir. |
| 3831 | * |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 3832 | * unlink and rmdir are special in btrfs, they do not always free space, so |
| 3833 | * if we cannot make our reservations the normal way try and see if there is |
| 3834 | * plenty of slack room in the global reserve to migrate, otherwise we cannot |
| 3835 | * allow the unlink to occur. |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3836 | */ |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 3837 | static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir) |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3838 | { |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3839 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3840 | |
Josef Bacik | e70bea5 | 2011-10-11 14:18:24 -0400 | [diff] [blame] | 3841 | /* |
| 3842 | * 1 for the possible orphan item |
| 3843 | * 1 for the dir item |
| 3844 | * 1 for the dir index |
| 3845 | * 1 for the inode ref |
Josef Bacik | e70bea5 | 2011-10-11 14:18:24 -0400 | [diff] [blame] | 3846 | * 1 for the inode |
| 3847 | */ |
Josef Bacik | 7f9fe61 | 2020-03-13 15:58:05 -0400 | [diff] [blame] | 3848 | return btrfs_start_transaction_fallback_global_rsv(root, 5); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3849 | } |
| 3850 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3851 | static int btrfs_unlink(struct inode *dir, struct dentry *dentry) |
| 3852 | { |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3853 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3854 | struct btrfs_trans_handle *trans; |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 3855 | struct inode *inode = d_inode(dentry); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3856 | int ret; |
| 3857 | |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 3858 | trans = __unlink_start_trans(dir); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3859 | if (IS_ERR(trans)) |
Josef Bacik | 5df6a9f | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 3860 | return PTR_ERR(trans); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3861 | |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 3862 | btrfs_record_unlink_dir(trans, BTRFS_I(dir), BTRFS_I(d_inode(dentry)), |
| 3863 | 0); |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 3864 | |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 3865 | ret = btrfs_unlink_inode(trans, root, BTRFS_I(dir), |
| 3866 | BTRFS_I(d_inode(dentry)), dentry->d_name.name, |
| 3867 | dentry->d_name.len); |
Tsutomu Itoh | b532402 | 2011-07-19 07:27:20 +0000 | [diff] [blame] | 3868 | if (ret) |
| 3869 | goto out; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3870 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3871 | if (inode->i_nlink == 0) { |
Nikolay Borisov | 73f2e54 | 2017-02-20 13:50:59 +0200 | [diff] [blame] | 3872 | ret = btrfs_orphan_add(trans, BTRFS_I(inode)); |
Tsutomu Itoh | b532402 | 2011-07-19 07:27:20 +0000 | [diff] [blame] | 3873 | if (ret) |
| 3874 | goto out; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3875 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3876 | |
Tsutomu Itoh | b532402 | 2011-07-19 07:27:20 +0000 | [diff] [blame] | 3877 | out: |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 3878 | btrfs_end_transaction(trans); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3879 | btrfs_btree_balance_dirty(root->fs_info); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3880 | return ret; |
| 3881 | } |
| 3882 | |
Misono Tomohiro | f60a236 | 2018-04-18 11:34:52 +0900 | [diff] [blame] | 3883 | static int btrfs_unlink_subvol(struct btrfs_trans_handle *trans, |
Josef Bacik | 045d396 | 2019-12-18 17:20:27 -0500 | [diff] [blame] | 3884 | struct inode *dir, struct dentry *dentry) |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3885 | { |
Lu Fengqi | 401b3b1 | 2018-08-01 11:32:30 +0800 | [diff] [blame] | 3886 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Josef Bacik | 045d396 | 2019-12-18 17:20:27 -0500 | [diff] [blame] | 3887 | struct btrfs_inode *inode = BTRFS_I(d_inode(dentry)); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3888 | struct btrfs_path *path; |
| 3889 | struct extent_buffer *leaf; |
| 3890 | struct btrfs_dir_item *di; |
| 3891 | struct btrfs_key key; |
Josef Bacik | 045d396 | 2019-12-18 17:20:27 -0500 | [diff] [blame] | 3892 | const char *name = dentry->d_name.name; |
| 3893 | int name_len = dentry->d_name.len; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3894 | u64 index; |
| 3895 | int ret; |
Josef Bacik | 045d396 | 2019-12-18 17:20:27 -0500 | [diff] [blame] | 3896 | u64 objectid; |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 3897 | u64 dir_ino = btrfs_ino(BTRFS_I(dir)); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3898 | |
Josef Bacik | 045d396 | 2019-12-18 17:20:27 -0500 | [diff] [blame] | 3899 | if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID) { |
| 3900 | objectid = inode->root->root_key.objectid; |
| 3901 | } else if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) { |
| 3902 | objectid = inode->location.objectid; |
| 3903 | } else { |
| 3904 | WARN_ON(1); |
| 3905 | return -EINVAL; |
| 3906 | } |
| 3907 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3908 | path = btrfs_alloc_path(); |
| 3909 | if (!path) |
| 3910 | return -ENOMEM; |
| 3911 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3912 | di = btrfs_lookup_dir_item(trans, root, path, dir_ino, |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3913 | name, name_len, -1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3914 | if (IS_ERR_OR_NULL(di)) { |
Liu Bo | 3cf5068 | 2018-09-12 06:06:26 +0800 | [diff] [blame] | 3915 | ret = di ? PTR_ERR(di) : -ENOENT; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3916 | goto out; |
| 3917 | } |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3918 | |
| 3919 | leaf = path->nodes[0]; |
| 3920 | btrfs_dir_item_key_to_cpu(leaf, di, &key); |
| 3921 | WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid); |
| 3922 | ret = btrfs_delete_one_dir_name(trans, root, path, di); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3923 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3924 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3925 | goto out; |
| 3926 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3927 | btrfs_release_path(path); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3928 | |
Josef Bacik | d49d328 | 2019-12-18 17:20:28 -0500 | [diff] [blame] | 3929 | /* |
| 3930 | * This is a placeholder inode for a subvolume we didn't have a |
| 3931 | * reference to at the time of the snapshot creation. In the meantime |
| 3932 | * we could have renamed the real subvol link into our snapshot, so |
| 3933 | * depending on btrfs_del_root_ref to return -ENOENT here is incorret. |
| 3934 | * Instead simply lookup the dir_index_item for this entry so we can |
| 3935 | * remove it. Otherwise we know we have a ref to the root and we can |
| 3936 | * call btrfs_del_root_ref, and it _shouldn't_ fail. |
| 3937 | */ |
| 3938 | if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) { |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3939 | di = btrfs_search_dir_index_item(root, path, dir_ino, |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3940 | name, name_len); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3941 | if (IS_ERR_OR_NULL(di)) { |
| 3942 | if (!di) |
| 3943 | ret = -ENOENT; |
| 3944 | else |
| 3945 | ret = PTR_ERR(di); |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3946 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3947 | goto out; |
| 3948 | } |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3949 | |
| 3950 | leaf = path->nodes[0]; |
| 3951 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3952 | index = key.offset; |
Josef Bacik | d49d328 | 2019-12-18 17:20:28 -0500 | [diff] [blame] | 3953 | btrfs_release_path(path); |
| 3954 | } else { |
| 3955 | ret = btrfs_del_root_ref(trans, objectid, |
| 3956 | root->root_key.objectid, dir_ino, |
| 3957 | &index, name, name_len); |
| 3958 | if (ret) { |
| 3959 | btrfs_abort_transaction(trans, ret); |
| 3960 | goto out; |
| 3961 | } |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3962 | } |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3963 | |
Lu Fengqi | 9add294 | 2018-08-01 11:32:26 +0800 | [diff] [blame] | 3964 | ret = btrfs_delete_delayed_dir_index(trans, BTRFS_I(dir), index); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3965 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3966 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3967 | goto out; |
| 3968 | } |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 3969 | |
Nikolay Borisov | 6ef06d2 | 2017-02-20 13:50:34 +0200 | [diff] [blame] | 3970 | 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] | 3971 | inode_inc_iversion(dir); |
Deepa Dinamani | c2050a4 | 2016-09-14 07:48:06 -0700 | [diff] [blame] | 3972 | dir->i_mtime = dir->i_ctime = current_time(dir); |
Nikolay Borisov | 729f796 | 2020-11-02 16:49:06 +0200 | [diff] [blame] | 3973 | ret = btrfs_update_inode_fallback(trans, root, BTRFS_I(dir)); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3974 | if (ret) |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3975 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3976 | out: |
Josef Bacik | 71d7aed | 2011-06-14 14:24:32 -0400 | [diff] [blame] | 3977 | btrfs_free_path(path); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3978 | return ret; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3979 | } |
| 3980 | |
Misono Tomohiro | ec42f16 | 2018-04-18 11:34:13 +0900 | [diff] [blame] | 3981 | /* |
| 3982 | * Helper to check if the subvolume references other subvolumes or if it's |
| 3983 | * default. |
| 3984 | */ |
Misono Tomohiro | f60a236 | 2018-04-18 11:34:52 +0900 | [diff] [blame] | 3985 | static noinline int may_destroy_subvol(struct btrfs_root *root) |
Misono Tomohiro | ec42f16 | 2018-04-18 11:34:13 +0900 | [diff] [blame] | 3986 | { |
| 3987 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 3988 | struct btrfs_path *path; |
| 3989 | struct btrfs_dir_item *di; |
| 3990 | struct btrfs_key key; |
| 3991 | u64 dir_id; |
| 3992 | int ret; |
| 3993 | |
| 3994 | path = btrfs_alloc_path(); |
| 3995 | if (!path) |
| 3996 | return -ENOMEM; |
| 3997 | |
| 3998 | /* Make sure this root isn't set as the default subvol */ |
| 3999 | dir_id = btrfs_super_root_dir(fs_info->super_copy); |
| 4000 | di = btrfs_lookup_dir_item(NULL, fs_info->tree_root, path, |
| 4001 | dir_id, "default", 7, 0); |
| 4002 | if (di && !IS_ERR(di)) { |
| 4003 | btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key); |
| 4004 | if (key.objectid == root->root_key.objectid) { |
| 4005 | ret = -EPERM; |
| 4006 | btrfs_err(fs_info, |
| 4007 | "deleting default subvolume %llu is not allowed", |
| 4008 | key.objectid); |
| 4009 | goto out; |
| 4010 | } |
| 4011 | btrfs_release_path(path); |
| 4012 | } |
| 4013 | |
| 4014 | key.objectid = root->root_key.objectid; |
| 4015 | key.type = BTRFS_ROOT_REF_KEY; |
| 4016 | key.offset = (u64)-1; |
| 4017 | |
| 4018 | ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0); |
| 4019 | if (ret < 0) |
| 4020 | goto out; |
| 4021 | BUG_ON(ret == 0); |
| 4022 | |
| 4023 | ret = 0; |
| 4024 | if (path->slots[0] > 0) { |
| 4025 | path->slots[0]--; |
| 4026 | btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]); |
| 4027 | if (key.objectid == root->root_key.objectid && |
| 4028 | key.type == BTRFS_ROOT_REF_KEY) |
| 4029 | ret = -ENOTEMPTY; |
| 4030 | } |
| 4031 | out: |
| 4032 | btrfs_free_path(path); |
| 4033 | return ret; |
| 4034 | } |
| 4035 | |
Nikolay Borisov | 20a6800 | 2018-04-27 14:36:24 +0300 | [diff] [blame] | 4036 | /* Delete all dentries for inodes belonging to the root */ |
| 4037 | static void btrfs_prune_dentries(struct btrfs_root *root) |
| 4038 | { |
| 4039 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 4040 | struct rb_node *node; |
| 4041 | struct rb_node *prev; |
| 4042 | struct btrfs_inode *entry; |
| 4043 | struct inode *inode; |
| 4044 | u64 objectid = 0; |
| 4045 | |
| 4046 | if (!test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) |
| 4047 | WARN_ON(btrfs_root_refs(&root->root_item) != 0); |
| 4048 | |
| 4049 | spin_lock(&root->inode_lock); |
| 4050 | again: |
| 4051 | node = root->inode_tree.rb_node; |
| 4052 | prev = NULL; |
| 4053 | while (node) { |
| 4054 | prev = node; |
| 4055 | entry = rb_entry(node, struct btrfs_inode, rb_node); |
| 4056 | |
David Sterba | 3750851 | 2018-06-29 10:56:40 +0200 | [diff] [blame] | 4057 | if (objectid < btrfs_ino(entry)) |
Nikolay Borisov | 20a6800 | 2018-04-27 14:36:24 +0300 | [diff] [blame] | 4058 | node = node->rb_left; |
David Sterba | 3750851 | 2018-06-29 10:56:40 +0200 | [diff] [blame] | 4059 | else if (objectid > btrfs_ino(entry)) |
Nikolay Borisov | 20a6800 | 2018-04-27 14:36:24 +0300 | [diff] [blame] | 4060 | node = node->rb_right; |
| 4061 | else |
| 4062 | break; |
| 4063 | } |
| 4064 | if (!node) { |
| 4065 | while (prev) { |
| 4066 | entry = rb_entry(prev, struct btrfs_inode, rb_node); |
David Sterba | 3750851 | 2018-06-29 10:56:40 +0200 | [diff] [blame] | 4067 | if (objectid <= btrfs_ino(entry)) { |
Nikolay Borisov | 20a6800 | 2018-04-27 14:36:24 +0300 | [diff] [blame] | 4068 | node = prev; |
| 4069 | break; |
| 4070 | } |
| 4071 | prev = rb_next(prev); |
| 4072 | } |
| 4073 | } |
| 4074 | while (node) { |
| 4075 | entry = rb_entry(node, struct btrfs_inode, rb_node); |
David Sterba | 3750851 | 2018-06-29 10:56:40 +0200 | [diff] [blame] | 4076 | objectid = btrfs_ino(entry) + 1; |
Nikolay Borisov | 20a6800 | 2018-04-27 14:36:24 +0300 | [diff] [blame] | 4077 | inode = igrab(&entry->vfs_inode); |
| 4078 | if (inode) { |
| 4079 | spin_unlock(&root->inode_lock); |
| 4080 | if (atomic_read(&inode->i_count) > 1) |
| 4081 | d_prune_aliases(inode); |
| 4082 | /* |
| 4083 | * btrfs_drop_inode will have it removed from the inode |
| 4084 | * cache when its usage count hits zero. |
| 4085 | */ |
| 4086 | iput(inode); |
| 4087 | cond_resched(); |
| 4088 | spin_lock(&root->inode_lock); |
| 4089 | goto again; |
| 4090 | } |
| 4091 | |
| 4092 | if (cond_resched_lock(&root->inode_lock)) |
| 4093 | goto again; |
| 4094 | |
| 4095 | node = rb_next(node); |
| 4096 | } |
| 4097 | spin_unlock(&root->inode_lock); |
| 4098 | } |
| 4099 | |
Misono Tomohiro | f60a236 | 2018-04-18 11:34:52 +0900 | [diff] [blame] | 4100 | int btrfs_delete_subvolume(struct inode *dir, struct dentry *dentry) |
| 4101 | { |
| 4102 | struct btrfs_fs_info *fs_info = btrfs_sb(dentry->d_sb); |
| 4103 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 4104 | struct inode *inode = d_inode(dentry); |
| 4105 | struct btrfs_root *dest = BTRFS_I(inode)->root; |
| 4106 | struct btrfs_trans_handle *trans; |
| 4107 | struct btrfs_block_rsv block_rsv; |
| 4108 | u64 root_flags; |
Misono Tomohiro | f60a236 | 2018-04-18 11:34:52 +0900 | [diff] [blame] | 4109 | int ret; |
| 4110 | int err; |
| 4111 | |
| 4112 | /* |
| 4113 | * Don't allow to delete a subvolume with send in progress. This is |
| 4114 | * inside the inode lock so the error handling that has to drop the bit |
| 4115 | * again is not run concurrently. |
| 4116 | */ |
| 4117 | spin_lock(&dest->root_item_lock); |
Lu Fengqi | a7176f7 | 2018-08-04 21:10:53 +0800 | [diff] [blame] | 4118 | if (dest->send_in_progress) { |
Misono Tomohiro | f60a236 | 2018-04-18 11:34:52 +0900 | [diff] [blame] | 4119 | spin_unlock(&dest->root_item_lock); |
| 4120 | btrfs_warn(fs_info, |
| 4121 | "attempt to delete subvolume %llu during send", |
| 4122 | dest->root_key.objectid); |
| 4123 | return -EPERM; |
| 4124 | } |
Lu Fengqi | a7176f7 | 2018-08-04 21:10:53 +0800 | [diff] [blame] | 4125 | root_flags = btrfs_root_flags(&dest->root_item); |
| 4126 | btrfs_set_root_flags(&dest->root_item, |
| 4127 | root_flags | BTRFS_ROOT_SUBVOL_DEAD); |
| 4128 | spin_unlock(&dest->root_item_lock); |
Misono Tomohiro | f60a236 | 2018-04-18 11:34:52 +0900 | [diff] [blame] | 4129 | |
| 4130 | down_write(&fs_info->subvol_sem); |
| 4131 | |
| 4132 | err = may_destroy_subvol(dest); |
| 4133 | if (err) |
| 4134 | goto out_up_write; |
| 4135 | |
| 4136 | btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP); |
| 4137 | /* |
| 4138 | * One for dir inode, |
| 4139 | * two for dir entries, |
| 4140 | * two for root ref/backref. |
| 4141 | */ |
Gu JinXiang | c4c129d | 2018-05-30 11:00:38 +0800 | [diff] [blame] | 4142 | err = btrfs_subvolume_reserve_metadata(root, &block_rsv, 5, true); |
Misono Tomohiro | f60a236 | 2018-04-18 11:34:52 +0900 | [diff] [blame] | 4143 | if (err) |
| 4144 | goto out_up_write; |
| 4145 | |
| 4146 | trans = btrfs_start_transaction(root, 0); |
| 4147 | if (IS_ERR(trans)) { |
| 4148 | err = PTR_ERR(trans); |
| 4149 | goto out_release; |
| 4150 | } |
| 4151 | trans->block_rsv = &block_rsv; |
| 4152 | trans->bytes_reserved = block_rsv.size; |
| 4153 | |
| 4154 | btrfs_record_snapshot_destroy(trans, BTRFS_I(dir)); |
| 4155 | |
Josef Bacik | 045d396 | 2019-12-18 17:20:27 -0500 | [diff] [blame] | 4156 | ret = btrfs_unlink_subvol(trans, dir, dentry); |
Misono Tomohiro | f60a236 | 2018-04-18 11:34:52 +0900 | [diff] [blame] | 4157 | if (ret) { |
| 4158 | err = ret; |
| 4159 | btrfs_abort_transaction(trans, ret); |
| 4160 | goto out_end_trans; |
| 4161 | } |
| 4162 | |
| 4163 | btrfs_record_root_in_trans(trans, dest); |
| 4164 | |
| 4165 | memset(&dest->root_item.drop_progress, 0, |
| 4166 | sizeof(dest->root_item.drop_progress)); |
David Sterba | c842268 | 2020-09-15 21:44:52 +0200 | [diff] [blame] | 4167 | btrfs_set_root_drop_level(&dest->root_item, 0); |
Misono Tomohiro | f60a236 | 2018-04-18 11:34:52 +0900 | [diff] [blame] | 4168 | btrfs_set_root_refs(&dest->root_item, 0); |
| 4169 | |
| 4170 | if (!test_and_set_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &dest->state)) { |
| 4171 | ret = btrfs_insert_orphan_item(trans, |
| 4172 | fs_info->tree_root, |
| 4173 | dest->root_key.objectid); |
| 4174 | if (ret) { |
| 4175 | btrfs_abort_transaction(trans, ret); |
| 4176 | err = ret; |
| 4177 | goto out_end_trans; |
| 4178 | } |
| 4179 | } |
| 4180 | |
Lu Fengqi | d195779 | 2018-05-29 15:01:54 +0800 | [diff] [blame] | 4181 | ret = btrfs_uuid_tree_remove(trans, dest->root_item.uuid, |
Misono Tomohiro | f60a236 | 2018-04-18 11:34:52 +0900 | [diff] [blame] | 4182 | BTRFS_UUID_KEY_SUBVOL, |
| 4183 | dest->root_key.objectid); |
| 4184 | if (ret && ret != -ENOENT) { |
| 4185 | btrfs_abort_transaction(trans, ret); |
| 4186 | err = ret; |
| 4187 | goto out_end_trans; |
| 4188 | } |
| 4189 | if (!btrfs_is_empty_uuid(dest->root_item.received_uuid)) { |
Lu Fengqi | d195779 | 2018-05-29 15:01:54 +0800 | [diff] [blame] | 4190 | ret = btrfs_uuid_tree_remove(trans, |
Misono Tomohiro | f60a236 | 2018-04-18 11:34:52 +0900 | [diff] [blame] | 4191 | dest->root_item.received_uuid, |
| 4192 | BTRFS_UUID_KEY_RECEIVED_SUBVOL, |
| 4193 | dest->root_key.objectid); |
| 4194 | if (ret && ret != -ENOENT) { |
| 4195 | btrfs_abort_transaction(trans, ret); |
| 4196 | err = ret; |
| 4197 | goto out_end_trans; |
| 4198 | } |
| 4199 | } |
| 4200 | |
Qu Wenruo | 082b6c9 | 2020-06-16 10:17:37 +0800 | [diff] [blame] | 4201 | free_anon_bdev(dest->anon_dev); |
| 4202 | dest->anon_dev = 0; |
Misono Tomohiro | f60a236 | 2018-04-18 11:34:52 +0900 | [diff] [blame] | 4203 | out_end_trans: |
| 4204 | trans->block_rsv = NULL; |
| 4205 | trans->bytes_reserved = 0; |
| 4206 | ret = btrfs_end_transaction(trans); |
| 4207 | if (ret && !err) |
| 4208 | err = ret; |
| 4209 | inode->i_flags |= S_DEAD; |
| 4210 | out_release: |
Qu Wenruo | e85fde5 | 2020-07-24 14:46:10 +0800 | [diff] [blame] | 4211 | btrfs_subvolume_release_metadata(root, &block_rsv); |
Misono Tomohiro | f60a236 | 2018-04-18 11:34:52 +0900 | [diff] [blame] | 4212 | out_up_write: |
| 4213 | up_write(&fs_info->subvol_sem); |
| 4214 | if (err) { |
| 4215 | spin_lock(&dest->root_item_lock); |
| 4216 | root_flags = btrfs_root_flags(&dest->root_item); |
| 4217 | btrfs_set_root_flags(&dest->root_item, |
| 4218 | root_flags & ~BTRFS_ROOT_SUBVOL_DEAD); |
| 4219 | spin_unlock(&dest->root_item_lock); |
| 4220 | } else { |
| 4221 | d_invalidate(dentry); |
Nikolay Borisov | 20a6800 | 2018-04-27 14:36:24 +0300 | [diff] [blame] | 4222 | btrfs_prune_dentries(dest); |
Misono Tomohiro | f60a236 | 2018-04-18 11:34:52 +0900 | [diff] [blame] | 4223 | ASSERT(dest->send_in_progress == 0); |
| 4224 | |
| 4225 | /* the last ref */ |
| 4226 | if (dest->ino_cache_inode) { |
| 4227 | iput(dest->ino_cache_inode); |
| 4228 | dest->ino_cache_inode = NULL; |
| 4229 | } |
| 4230 | } |
| 4231 | |
| 4232 | return err; |
| 4233 | } |
| 4234 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4235 | static int btrfs_rmdir(struct inode *dir, struct dentry *dentry) |
| 4236 | { |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 4237 | struct inode *inode = d_inode(dentry); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 4238 | int err = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4239 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4240 | struct btrfs_trans_handle *trans; |
Filipe Manana | 44f714d | 2016-06-06 16:11:13 +0100 | [diff] [blame] | 4241 | u64 last_unlink_trans; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4242 | |
David Sterba | b3ae244 | 2012-09-13 16:04:34 -0600 | [diff] [blame] | 4243 | if (inode->i_size > BTRFS_EMPTY_DIR_SIZE) |
Yan | 134d451 | 2007-10-25 15:49:25 -0400 | [diff] [blame] | 4244 | return -ENOTEMPTY; |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 4245 | if (btrfs_ino(BTRFS_I(inode)) == BTRFS_FIRST_FREE_OBJECTID) |
Misono Tomohiro | a79a464 | 2018-04-18 11:35:31 +0900 | [diff] [blame] | 4246 | return btrfs_delete_subvolume(dir, dentry); |
Yan | 134d451 | 2007-10-25 15:49:25 -0400 | [diff] [blame] | 4247 | |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 4248 | trans = __unlink_start_trans(dir); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4249 | if (IS_ERR(trans)) |
Josef Bacik | 5df6a9f | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4250 | return PTR_ERR(trans); |
Josef Bacik | 5df6a9f | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4251 | |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 4252 | if (unlikely(btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) { |
Josef Bacik | 045d396 | 2019-12-18 17:20:27 -0500 | [diff] [blame] | 4253 | err = btrfs_unlink_subvol(trans, dir, dentry); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4254 | goto out; |
| 4255 | } |
| 4256 | |
Nikolay Borisov | 73f2e54 | 2017-02-20 13:50:59 +0200 | [diff] [blame] | 4257 | err = btrfs_orphan_add(trans, BTRFS_I(inode)); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 4258 | if (err) |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4259 | goto out; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 4260 | |
Filipe Manana | 44f714d | 2016-06-06 16:11:13 +0100 | [diff] [blame] | 4261 | last_unlink_trans = BTRFS_I(inode)->last_unlink_trans; |
| 4262 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4263 | /* now the directory is empty */ |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 4264 | err = btrfs_unlink_inode(trans, root, BTRFS_I(dir), |
| 4265 | BTRFS_I(d_inode(dentry)), dentry->d_name.name, |
| 4266 | dentry->d_name.len); |
Filipe Manana | 44f714d | 2016-06-06 16:11:13 +0100 | [diff] [blame] | 4267 | if (!err) { |
Nikolay Borisov | 6ef06d2 | 2017-02-20 13:50:34 +0200 | [diff] [blame] | 4268 | btrfs_i_size_write(BTRFS_I(inode), 0); |
Filipe Manana | 44f714d | 2016-06-06 16:11:13 +0100 | [diff] [blame] | 4269 | /* |
| 4270 | * Propagate the last_unlink_trans value of the deleted dir to |
| 4271 | * its parent directory. This is to prevent an unrecoverable |
| 4272 | * log tree in the case we do something like this: |
| 4273 | * 1) create dir foo |
| 4274 | * 2) create snapshot under dir foo |
| 4275 | * 3) delete the snapshot |
| 4276 | * 4) rmdir foo |
| 4277 | * 5) mkdir foo |
| 4278 | * 6) fsync foo or some file inside foo |
| 4279 | */ |
| 4280 | if (last_unlink_trans >= trans->transid) |
| 4281 | BTRFS_I(dir)->last_unlink_trans = last_unlink_trans; |
| 4282 | } |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4283 | out: |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 4284 | btrfs_end_transaction(trans); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4285 | btrfs_btree_balance_dirty(root->fs_info); |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 4286 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4287 | return err; |
| 4288 | } |
| 4289 | |
Josef Bacik | ddfae63 | 2017-10-19 14:16:02 -0400 | [diff] [blame] | 4290 | /* |
| 4291 | * Return this if we need to call truncate_block for the last bit of the |
| 4292 | * truncate. |
| 4293 | */ |
| 4294 | #define NEED_TRUNCATE_BLOCK 1 |
Filipe Manana | 0305cd5 | 2015-10-16 12:34:25 +0100 | [diff] [blame] | 4295 | |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 4296 | /* |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4297 | * this can truncate away extent items, csum items and directory items. |
| 4298 | * 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] | 4299 | * any higher than new_size |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4300 | * |
| 4301 | * csum items that cross the new i_size are truncated to the new size |
| 4302 | * as well. |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 4303 | * |
| 4304 | * min_type is the minimum key type to truncate down to. If set to 0, this |
| 4305 | * 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] | 4306 | */ |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4307 | int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans, |
| 4308 | struct btrfs_root *root, |
Nikolay Borisov | 5074339 | 2020-11-02 16:48:55 +0200 | [diff] [blame] | 4309 | struct btrfs_inode *inode, |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4310 | u64 new_size, u32 min_type) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4311 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4312 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4313 | struct btrfs_path *path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4314 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4315 | struct btrfs_file_extent_item *fi; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4316 | struct btrfs_key key; |
| 4317 | struct btrfs_key found_key; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4318 | u64 extent_start = 0; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4319 | u64 extent_num_bytes = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4320 | u64 extent_offset = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4321 | u64 item_end = 0; |
Filipe Manana | c1aa457 | 2015-06-20 18:20:09 +0100 | [diff] [blame] | 4322 | u64 last_size = new_size; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4323 | u32 found_type = (u8)-1; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4324 | int found_extent; |
| 4325 | int del_item; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4326 | int pending_del_nr = 0; |
| 4327 | int pending_del_slot = 0; |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4328 | int extent_type = -1; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4329 | int ret; |
Nikolay Borisov | 5074339 | 2020-11-02 16:48:55 +0200 | [diff] [blame] | 4330 | u64 ino = btrfs_ino(inode); |
Chris Mason | 28ed134 | 2014-12-17 09:41:04 -0800 | [diff] [blame] | 4331 | u64 bytes_deleted = 0; |
Thomas Meyer | 897ca81 | 2017-10-07 16:02:21 +0200 | [diff] [blame] | 4332 | bool be_nice = false; |
| 4333 | bool should_throttle = false; |
Filipe Manana | 28553fa | 2020-02-07 12:23:09 +0000 | [diff] [blame] | 4334 | const u64 lock_start = ALIGN_DOWN(new_size, fs_info->sectorsize); |
| 4335 | struct extent_state *cached_state = NULL; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4336 | |
| 4337 | BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4338 | |
Chris Mason | 28ed134 | 2014-12-17 09:41:04 -0800 | [diff] [blame] | 4339 | /* |
Qu Wenruo | 92a7cc4 | 2020-05-15 14:01:40 +0800 | [diff] [blame] | 4340 | * For non-free space inodes and non-shareable roots, we want to back |
| 4341 | * off from time to time. This means all inodes in subvolume roots, |
| 4342 | * reloc roots, and data reloc roots. |
Chris Mason | 28ed134 | 2014-12-17 09:41:04 -0800 | [diff] [blame] | 4343 | */ |
Nikolay Borisov | 5074339 | 2020-11-02 16:48:55 +0200 | [diff] [blame] | 4344 | if (!btrfs_is_free_space_inode(inode) && |
Qu Wenruo | 92a7cc4 | 2020-05-15 14:01:40 +0800 | [diff] [blame] | 4345 | test_bit(BTRFS_ROOT_SHAREABLE, &root->state)) |
Thomas Meyer | 897ca81 | 2017-10-07 16:02:21 +0200 | [diff] [blame] | 4346 | be_nice = true; |
Chris Mason | 28ed134 | 2014-12-17 09:41:04 -0800 | [diff] [blame] | 4347 | |
Mark Fasheh | 0eb0e19 | 2011-07-12 16:44:10 -0700 | [diff] [blame] | 4348 | path = btrfs_alloc_path(); |
| 4349 | if (!path) |
| 4350 | return -ENOMEM; |
David Sterba | e4058b5 | 2015-11-27 16:31:35 +0100 | [diff] [blame] | 4351 | path->reada = READA_BACK; |
Mark Fasheh | 0eb0e19 | 2011-07-12 16:44:10 -0700 | [diff] [blame] | 4352 | |
Qu Wenruo | 82028e0a | 2020-05-15 14:01:41 +0800 | [diff] [blame] | 4353 | if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) { |
Nikolay Borisov | 5074339 | 2020-11-02 16:48:55 +0200 | [diff] [blame] | 4354 | lock_extent_bits(&inode->io_tree, lock_start, (u64)-1, |
Filipe Manana | a5ae50d | 2020-02-20 13:29:49 +0000 | [diff] [blame] | 4355 | &cached_state); |
Filipe Manana | 28553fa | 2020-02-07 12:23:09 +0000 | [diff] [blame] | 4356 | |
Qu Wenruo | 82028e0a | 2020-05-15 14:01:41 +0800 | [diff] [blame] | 4357 | /* |
| 4358 | * We want to drop from the next block forward in case this |
| 4359 | * new size is not block aligned since we will be keeping the |
| 4360 | * last block of the extent just the way it is. |
| 4361 | */ |
Nikolay Borisov | 5074339 | 2020-11-02 16:48:55 +0200 | [diff] [blame] | 4362 | btrfs_drop_extent_cache(inode, ALIGN(new_size, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4363 | fs_info->sectorsize), |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 4364 | (u64)-1, 0); |
Qu Wenruo | 82028e0a | 2020-05-15 14:01:41 +0800 | [diff] [blame] | 4365 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4366 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 4367 | /* |
| 4368 | * This function is also used to drop the items in the log tree before |
| 4369 | * 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] | 4370 | * 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] | 4371 | * items. |
| 4372 | */ |
Nikolay Borisov | 5074339 | 2020-11-02 16:48:55 +0200 | [diff] [blame] | 4373 | if (min_type == 0 && root == inode->root) |
| 4374 | btrfs_kill_delayed_inode_items(inode); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 4375 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4376 | key.objectid = ino; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4377 | key.offset = (u64)-1; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4378 | key.type = (u8)-1; |
| 4379 | |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4380 | search_again: |
Chris Mason | 28ed134 | 2014-12-17 09:41:04 -0800 | [diff] [blame] | 4381 | /* |
| 4382 | * with a 16K leaf size and 128MB extents, you can actually queue |
| 4383 | * up a huge file in a single leaf. Most of the time that |
| 4384 | * bytes_deleted is > 0, it will be huge by the time we get here |
| 4385 | */ |
Omar Sandoval | fd86a3a | 2018-05-11 13:13:30 -0700 | [diff] [blame] | 4386 | if (be_nice && bytes_deleted > SZ_32M && |
| 4387 | btrfs_should_end_transaction(trans)) { |
| 4388 | ret = -EAGAIN; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4389 | goto out; |
| 4390 | } |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4391 | |
Omar Sandoval | fd86a3a | 2018-05-11 13:13:30 -0700 | [diff] [blame] | 4392 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 4393 | if (ret < 0) |
| 4394 | goto out; |
| 4395 | |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4396 | if (ret > 0) { |
Omar Sandoval | fd86a3a | 2018-05-11 13:13:30 -0700 | [diff] [blame] | 4397 | ret = 0; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4398 | /* there are no items in the tree for us to truncate, we're |
| 4399 | * done |
| 4400 | */ |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4401 | if (path->slots[0] == 0) |
| 4402 | goto out; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4403 | path->slots[0]--; |
| 4404 | } |
| 4405 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4406 | while (1) { |
Josef Bacik | 9ddc959 | 2020-01-17 09:02:22 -0500 | [diff] [blame] | 4407 | u64 clear_start = 0, clear_len = 0; |
| 4408 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4409 | fi = NULL; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4410 | leaf = path->nodes[0]; |
| 4411 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 4412 | found_type = found_key.type; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4413 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4414 | if (found_key.objectid != ino) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4415 | break; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4416 | |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4417 | if (found_type < min_type) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4418 | break; |
| 4419 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4420 | item_end = found_key.offset; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4421 | if (found_type == BTRFS_EXTENT_DATA_KEY) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4422 | fi = btrfs_item_ptr(leaf, path->slots[0], |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4423 | struct btrfs_file_extent_item); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4424 | extent_type = btrfs_file_extent_type(leaf, fi); |
| 4425 | if (extent_type != BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4426 | item_end += |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4427 | btrfs_file_extent_num_bytes(leaf, fi); |
Liu Bo | 09ed2f1 | 2017-03-10 11:09:48 -0800 | [diff] [blame] | 4428 | |
| 4429 | trace_btrfs_truncate_show_fi_regular( |
Nikolay Borisov | 5074339 | 2020-11-02 16:48:55 +0200 | [diff] [blame] | 4430 | inode, leaf, fi, found_key.offset); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4431 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
Qu Wenruo | e41ca58 | 2018-06-06 15:41:49 +0800 | [diff] [blame] | 4432 | item_end += btrfs_file_extent_ram_bytes(leaf, |
| 4433 | fi); |
Liu Bo | 09ed2f1 | 2017-03-10 11:09:48 -0800 | [diff] [blame] | 4434 | |
| 4435 | trace_btrfs_truncate_show_fi_inline( |
Nikolay Borisov | 5074339 | 2020-11-02 16:48:55 +0200 | [diff] [blame] | 4436 | inode, leaf, fi, path->slots[0], |
Liu Bo | 09ed2f1 | 2017-03-10 11:09:48 -0800 | [diff] [blame] | 4437 | found_key.offset); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4438 | } |
Yan | 008630c | 2007-11-07 13:31:09 -0500 | [diff] [blame] | 4439 | item_end--; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4440 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4441 | if (found_type > min_type) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4442 | del_item = 1; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4443 | } else { |
Filipe Manana | 76b42ab | 2017-02-14 16:56:01 +0000 | [diff] [blame] | 4444 | if (item_end < new_size) |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4445 | break; |
| 4446 | if (found_key.offset >= new_size) |
| 4447 | del_item = 1; |
| 4448 | else |
| 4449 | del_item = 0; |
| 4450 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4451 | found_extent = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4452 | /* FIXME, shrink the extent if the ref count is only 1 */ |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4453 | if (found_type != BTRFS_EXTENT_DATA_KEY) |
| 4454 | goto delete; |
| 4455 | |
| 4456 | if (extent_type != BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4457 | u64 num_dec; |
Josef Bacik | 9ddc959 | 2020-01-17 09:02:22 -0500 | [diff] [blame] | 4458 | |
| 4459 | clear_start = found_key.offset; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4460 | extent_start = btrfs_file_extent_disk_bytenr(leaf, fi); |
Miao Xie | f70a9a6b | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 4461 | if (!del_item) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4462 | u64 orig_num_bytes = |
| 4463 | btrfs_file_extent_num_bytes(leaf, fi); |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 4464 | extent_num_bytes = ALIGN(new_size - |
| 4465 | found_key.offset, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4466 | fs_info->sectorsize); |
Josef Bacik | 9ddc959 | 2020-01-17 09:02:22 -0500 | [diff] [blame] | 4467 | clear_start = ALIGN(new_size, fs_info->sectorsize); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4468 | btrfs_set_file_extent_num_bytes(leaf, fi, |
| 4469 | extent_num_bytes); |
| 4470 | num_dec = (orig_num_bytes - |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 4471 | extent_num_bytes); |
Qu Wenruo | 92a7cc4 | 2020-05-15 14:01:40 +0800 | [diff] [blame] | 4472 | if (test_bit(BTRFS_ROOT_SHAREABLE, |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 4473 | &root->state) && |
| 4474 | extent_start != 0) |
Nikolay Borisov | 5074339 | 2020-11-02 16:48:55 +0200 | [diff] [blame] | 4475 | inode_sub_bytes(&inode->vfs_inode, |
| 4476 | num_dec); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4477 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4478 | } else { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4479 | extent_num_bytes = |
| 4480 | btrfs_file_extent_disk_num_bytes(leaf, |
| 4481 | fi); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4482 | extent_offset = found_key.offset - |
| 4483 | btrfs_file_extent_offset(leaf, fi); |
| 4484 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4485 | /* FIXME blocksize != 4096 */ |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 4486 | num_dec = btrfs_file_extent_num_bytes(leaf, fi); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4487 | if (extent_start != 0) { |
| 4488 | found_extent = 1; |
Qu Wenruo | 92a7cc4 | 2020-05-15 14:01:40 +0800 | [diff] [blame] | 4489 | if (test_bit(BTRFS_ROOT_SHAREABLE, |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 4490 | &root->state)) |
Nikolay Borisov | 5074339 | 2020-11-02 16:48:55 +0200 | [diff] [blame] | 4491 | inode_sub_bytes(&inode->vfs_inode, |
| 4492 | num_dec); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4493 | } |
| 4494 | } |
Josef Bacik | 9ddc959 | 2020-01-17 09:02:22 -0500 | [diff] [blame] | 4495 | clear_len = num_dec; |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 4496 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4497 | /* |
| 4498 | * we can't truncate inline items that have had |
| 4499 | * special encodings |
| 4500 | */ |
| 4501 | if (!del_item && |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4502 | btrfs_file_extent_encryption(leaf, fi) == 0 && |
Josef Bacik | ddfae63 | 2017-10-19 14:16:02 -0400 | [diff] [blame] | 4503 | btrfs_file_extent_other_encoding(leaf, fi) == 0 && |
| 4504 | btrfs_file_extent_compression(leaf, fi) == 0) { |
| 4505 | u32 size = (u32)(new_size - found_key.offset); |
Chris Mason | 514ac8a | 2014-01-03 21:07:00 -0800 | [diff] [blame] | 4506 | |
Josef Bacik | ddfae63 | 2017-10-19 14:16:02 -0400 | [diff] [blame] | 4507 | btrfs_set_file_extent_ram_bytes(leaf, fi, size); |
| 4508 | size = btrfs_file_extent_calc_inline_size(size); |
David Sterba | 78ac4f9 | 2019-03-20 14:49:12 +0100 | [diff] [blame] | 4509 | btrfs_truncate_item(path, size, 1); |
Josef Bacik | ddfae63 | 2017-10-19 14:16:02 -0400 | [diff] [blame] | 4510 | } else if (!del_item) { |
Chris Mason | 514ac8a | 2014-01-03 21:07:00 -0800 | [diff] [blame] | 4511 | /* |
Josef Bacik | ddfae63 | 2017-10-19 14:16:02 -0400 | [diff] [blame] | 4512 | * We have to bail so the last_size is set to |
| 4513 | * just before this extent. |
Chris Mason | 514ac8a | 2014-01-03 21:07:00 -0800 | [diff] [blame] | 4514 | */ |
Omar Sandoval | fd86a3a | 2018-05-11 13:13:30 -0700 | [diff] [blame] | 4515 | ret = NEED_TRUNCATE_BLOCK; |
Josef Bacik | ddfae63 | 2017-10-19 14:16:02 -0400 | [diff] [blame] | 4516 | break; |
Josef Bacik | 9ddc959 | 2020-01-17 09:02:22 -0500 | [diff] [blame] | 4517 | } else { |
| 4518 | /* |
| 4519 | * Inline extents are special, we just treat |
| 4520 | * them as a full sector worth in the file |
| 4521 | * extent tree just for simplicity sake. |
| 4522 | */ |
| 4523 | clear_len = fs_info->sectorsize; |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 4524 | } |
Josef Bacik | ddfae63 | 2017-10-19 14:16:02 -0400 | [diff] [blame] | 4525 | |
Qu Wenruo | 92a7cc4 | 2020-05-15 14:01:40 +0800 | [diff] [blame] | 4526 | if (test_bit(BTRFS_ROOT_SHAREABLE, &root->state)) |
Nikolay Borisov | 5074339 | 2020-11-02 16:48:55 +0200 | [diff] [blame] | 4527 | inode_sub_bytes(&inode->vfs_inode, |
| 4528 | item_end + 1 - new_size); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4529 | } |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4530 | delete: |
Josef Bacik | 9ddc959 | 2020-01-17 09:02:22 -0500 | [diff] [blame] | 4531 | /* |
| 4532 | * We use btrfs_truncate_inode_items() to clean up log trees for |
| 4533 | * multiple fsyncs, and in this case we don't want to clear the |
| 4534 | * file extent range because it's just the log. |
| 4535 | */ |
Nikolay Borisov | 5074339 | 2020-11-02 16:48:55 +0200 | [diff] [blame] | 4536 | if (root == inode->root) { |
| 4537 | ret = btrfs_inode_clear_file_extent_range(inode, |
Josef Bacik | 9ddc959 | 2020-01-17 09:02:22 -0500 | [diff] [blame] | 4538 | clear_start, clear_len); |
| 4539 | if (ret) { |
| 4540 | btrfs_abort_transaction(trans, ret); |
| 4541 | break; |
| 4542 | } |
| 4543 | } |
| 4544 | |
Josef Bacik | ddfae63 | 2017-10-19 14:16:02 -0400 | [diff] [blame] | 4545 | if (del_item) |
| 4546 | last_size = found_key.offset; |
| 4547 | else |
| 4548 | last_size = new_size; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4549 | if (del_item) { |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4550 | if (!pending_del_nr) { |
| 4551 | /* no pending yet, add ourselves */ |
| 4552 | pending_del_slot = path->slots[0]; |
| 4553 | pending_del_nr = 1; |
| 4554 | } else if (pending_del_nr && |
| 4555 | path->slots[0] + 1 == pending_del_slot) { |
| 4556 | /* hop on the pending chunk */ |
| 4557 | pending_del_nr++; |
| 4558 | pending_del_slot = path->slots[0]; |
| 4559 | } else { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4560 | BUG(); |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4561 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4562 | } else { |
| 4563 | break; |
| 4564 | } |
Thomas Meyer | 897ca81 | 2017-10-07 16:02:21 +0200 | [diff] [blame] | 4565 | should_throttle = false; |
Chris Mason | 28f75a0 | 2015-02-04 06:59:29 -0800 | [diff] [blame] | 4566 | |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 4567 | if (found_extent && |
Qu Wenruo | 82028e0a | 2020-05-15 14:01:41 +0800 | [diff] [blame] | 4568 | root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) { |
Qu Wenruo | ffd4bb2 | 2019-04-04 14:45:36 +0800 | [diff] [blame] | 4569 | struct btrfs_ref ref = { 0 }; |
| 4570 | |
Chris Mason | 28ed134 | 2014-12-17 09:41:04 -0800 | [diff] [blame] | 4571 | bytes_deleted += extent_num_bytes; |
Qu Wenruo | ffd4bb2 | 2019-04-04 14:45:36 +0800 | [diff] [blame] | 4572 | |
| 4573 | btrfs_init_generic_ref(&ref, BTRFS_DROP_DELAYED_REF, |
| 4574 | extent_start, extent_num_bytes, 0); |
| 4575 | ref.real_root = root->root_key.objectid; |
| 4576 | btrfs_init_data_ref(&ref, btrfs_header_owner(leaf), |
| 4577 | ino, extent_offset); |
| 4578 | ret = btrfs_free_extent(trans, &ref); |
Omar Sandoval | 0552210 | 2018-05-11 13:13:31 -0700 | [diff] [blame] | 4579 | if (ret) { |
| 4580 | btrfs_abort_transaction(trans, ret); |
| 4581 | break; |
| 4582 | } |
Chris Mason | 28f75a0 | 2015-02-04 06:59:29 -0800 | [diff] [blame] | 4583 | if (be_nice) { |
Lu Fengqi | 7c86162 | 2018-10-11 13:40:36 +0800 | [diff] [blame] | 4584 | if (btrfs_should_throttle_delayed_refs(trans)) |
Thomas Meyer | 897ca81 | 2017-10-07 16:02:21 +0200 | [diff] [blame] | 4585 | should_throttle = true; |
Chris Mason | 28f75a0 | 2015-02-04 06:59:29 -0800 | [diff] [blame] | 4586 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4587 | } |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4588 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4589 | if (found_type == BTRFS_INODE_ITEM_KEY) |
| 4590 | break; |
| 4591 | |
| 4592 | if (path->slots[0] == 0 || |
Josef Bacik | 1262133 | 2015-02-03 07:50:16 -0800 | [diff] [blame] | 4593 | path->slots[0] != pending_del_slot || |
Josef Bacik | 28bad21 | 2018-12-03 10:20:38 -0500 | [diff] [blame] | 4594 | should_throttle) { |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4595 | if (pending_del_nr) { |
| 4596 | ret = btrfs_del_items(trans, root, path, |
| 4597 | pending_del_slot, |
| 4598 | pending_del_nr); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4599 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 4600 | btrfs_abort_transaction(trans, ret); |
Omar Sandoval | fd86a3a | 2018-05-11 13:13:30 -0700 | [diff] [blame] | 4601 | break; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4602 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4603 | pending_del_nr = 0; |
| 4604 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4605 | btrfs_release_path(path); |
Josef Bacik | 28bad21 | 2018-12-03 10:20:38 -0500 | [diff] [blame] | 4606 | |
Chris Mason | 28f75a0 | 2015-02-04 06:59:29 -0800 | [diff] [blame] | 4607 | /* |
Josef Bacik | 28bad21 | 2018-12-03 10:20:38 -0500 | [diff] [blame] | 4608 | * We can generate a lot of delayed refs, so we need to |
| 4609 | * throttle every once and a while and make sure we're |
| 4610 | * adding enough space to keep up with the work we are |
| 4611 | * generating. Since we hold a transaction here we |
| 4612 | * can't flush, and we don't want to FLUSH_LIMIT because |
| 4613 | * we could have generated too many delayed refs to |
| 4614 | * actually allocate, so just bail if we're short and |
| 4615 | * let the normal reservation dance happen higher up. |
Chris Mason | 28f75a0 | 2015-02-04 06:59:29 -0800 | [diff] [blame] | 4616 | */ |
Josef Bacik | 28bad21 | 2018-12-03 10:20:38 -0500 | [diff] [blame] | 4617 | if (should_throttle) { |
| 4618 | ret = btrfs_delayed_refs_rsv_refill(fs_info, |
| 4619 | BTRFS_RESERVE_NO_FLUSH); |
| 4620 | if (ret) { |
| 4621 | ret = -EAGAIN; |
| 4622 | break; |
| 4623 | } |
Chris Mason | 28f75a0 | 2015-02-04 06:59:29 -0800 | [diff] [blame] | 4624 | } |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4625 | goto search_again; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4626 | } else { |
| 4627 | path->slots[0]--; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4628 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4629 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4630 | out: |
Omar Sandoval | fd86a3a | 2018-05-11 13:13:30 -0700 | [diff] [blame] | 4631 | if (ret >= 0 && pending_del_nr) { |
| 4632 | int err; |
| 4633 | |
| 4634 | err = btrfs_del_items(trans, root, path, pending_del_slot, |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4635 | pending_del_nr); |
Omar Sandoval | fd86a3a | 2018-05-11 13:13:30 -0700 | [diff] [blame] | 4636 | if (err) { |
| 4637 | btrfs_abort_transaction(trans, err); |
| 4638 | ret = err; |
| 4639 | } |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4640 | } |
Filipe Manana | 76b42ab | 2017-02-14 16:56:01 +0000 | [diff] [blame] | 4641 | if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) { |
| 4642 | ASSERT(last_size >= new_size); |
Omar Sandoval | fd86a3a | 2018-05-11 13:13:30 -0700 | [diff] [blame] | 4643 | if (!ret && last_size > new_size) |
Filipe Manana | 76b42ab | 2017-02-14 16:56:01 +0000 | [diff] [blame] | 4644 | last_size = new_size; |
Nikolay Borisov | 5074339 | 2020-11-02 16:48:55 +0200 | [diff] [blame] | 4645 | btrfs_inode_safe_disk_i_size_write(inode, last_size); |
| 4646 | unlock_extent_cached(&inode->io_tree, lock_start, (u64)-1, |
| 4647 | &cached_state); |
Filipe Manana | 76b42ab | 2017-02-14 16:56:01 +0000 | [diff] [blame] | 4648 | } |
Chris Mason | 28ed134 | 2014-12-17 09:41:04 -0800 | [diff] [blame] | 4649 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4650 | btrfs_free_path(path); |
Omar Sandoval | fd86a3a | 2018-05-11 13:13:30 -0700 | [diff] [blame] | 4651 | return ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4652 | } |
| 4653 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4654 | /* |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4655 | * btrfs_truncate_block - read, zero a chunk and write a block |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 4656 | * @inode - inode that we're zeroing |
| 4657 | * @from - the offset to start zeroing |
| 4658 | * @len - the length to zero, 0 to zero the entire range respective to the |
| 4659 | * offset |
| 4660 | * @front - zero up to the offset instead of from the offset on |
| 4661 | * |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4662 | * 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] | 4663 | * 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] | 4664 | */ |
Nikolay Borisov | 217f42e | 2020-11-02 16:49:03 +0200 | [diff] [blame] | 4665 | int btrfs_truncate_block(struct btrfs_inode *inode, loff_t from, loff_t len, |
| 4666 | int front) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4667 | { |
Nikolay Borisov | 217f42e | 2020-11-02 16:49:03 +0200 | [diff] [blame] | 4668 | struct btrfs_fs_info *fs_info = inode->root->fs_info; |
| 4669 | struct address_space *mapping = inode->vfs_inode.i_mapping; |
| 4670 | struct extent_io_tree *io_tree = &inode->io_tree; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4671 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4672 | struct extent_state *cached_state = NULL; |
Qu Wenruo | 364ecf3 | 2017-02-27 15:10:38 +0800 | [diff] [blame] | 4673 | struct extent_changeset *data_reserved = NULL; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4674 | char *kaddr; |
Qu Wenruo | 6d4572a | 2020-06-24 07:23:50 +0800 | [diff] [blame] | 4675 | bool only_release_metadata = false; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4676 | u32 blocksize = fs_info->sectorsize; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 4677 | pgoff_t index = from >> PAGE_SHIFT; |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4678 | unsigned offset = from & (blocksize - 1); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4679 | struct page *page; |
Josef Bacik | 3b16a4e | 2011-09-21 15:05:58 -0400 | [diff] [blame] | 4680 | gfp_t mask = btrfs_alloc_write_mask(mapping); |
Qu Wenruo | 6d4572a | 2020-06-24 07:23:50 +0800 | [diff] [blame] | 4681 | size_t write_bytes = blocksize; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4682 | int ret = 0; |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4683 | u64 block_start; |
| 4684 | u64 block_end; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4685 | |
Nikolay Borisov | b03ebd9 | 2018-01-18 14:47:06 +0200 | [diff] [blame] | 4686 | if (IS_ALIGNED(offset, blocksize) && |
| 4687 | (!len || IS_ALIGNED(len, blocksize))) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4688 | goto out; |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4689 | |
Josef Bacik | 8b62f87 | 2017-10-19 14:15:55 -0400 | [diff] [blame] | 4690 | block_start = round_down(from, blocksize); |
| 4691 | block_end = block_start + blocksize - 1; |
| 4692 | |
Nikolay Borisov | 217f42e | 2020-11-02 16:49:03 +0200 | [diff] [blame] | 4693 | ret = btrfs_check_data_free_space(inode, &data_reserved, block_start, |
| 4694 | blocksize); |
Qu Wenruo | 6d4572a | 2020-06-24 07:23:50 +0800 | [diff] [blame] | 4695 | if (ret < 0) { |
Nikolay Borisov | 217f42e | 2020-11-02 16:49:03 +0200 | [diff] [blame] | 4696 | if (btrfs_check_nocow_lock(inode, block_start, &write_bytes) > 0) { |
Qu Wenruo | 6d4572a | 2020-06-24 07:23:50 +0800 | [diff] [blame] | 4697 | /* For nocow case, no need to reserve data space */ |
| 4698 | only_release_metadata = true; |
| 4699 | } else { |
| 4700 | goto out; |
| 4701 | } |
| 4702 | } |
Nikolay Borisov | 217f42e | 2020-11-02 16:49:03 +0200 | [diff] [blame] | 4703 | ret = btrfs_delalloc_reserve_metadata(inode, blocksize); |
Qu Wenruo | 6d4572a | 2020-06-24 07:23:50 +0800 | [diff] [blame] | 4704 | if (ret < 0) { |
| 4705 | if (!only_release_metadata) |
Nikolay Borisov | 217f42e | 2020-11-02 16:49:03 +0200 | [diff] [blame] | 4706 | btrfs_free_reserved_data_space(inode, data_reserved, |
| 4707 | block_start, blocksize); |
Qu Wenruo | 6d4572a | 2020-06-24 07:23:50 +0800 | [diff] [blame] | 4708 | goto out; |
| 4709 | } |
Chris Mason | 211c17f | 2008-05-15 09:13:45 -0400 | [diff] [blame] | 4710 | again: |
Josef Bacik | 3b16a4e | 2011-09-21 15:05:58 -0400 | [diff] [blame] | 4711 | page = find_or_create_page(mapping, index, mask); |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 4712 | if (!page) { |
Nikolay Borisov | 217f42e | 2020-11-02 16:49:03 +0200 | [diff] [blame] | 4713 | btrfs_delalloc_release_space(inode, data_reserved, block_start, |
| 4714 | blocksize, true); |
| 4715 | btrfs_delalloc_release_extents(inode, blocksize); |
Miao Xie | ac6a2b3 | 2012-12-05 10:56:13 +0000 | [diff] [blame] | 4716 | ret = -ENOMEM; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4717 | goto out; |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 4718 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4719 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4720 | if (!PageUptodate(page)) { |
| 4721 | ret = btrfs_readpage(NULL, page); |
| 4722 | lock_page(page); |
Chris Mason | 211c17f | 2008-05-15 09:13:45 -0400 | [diff] [blame] | 4723 | if (page->mapping != mapping) { |
| 4724 | unlock_page(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 4725 | put_page(page); |
Chris Mason | 211c17f | 2008-05-15 09:13:45 -0400 | [diff] [blame] | 4726 | goto again; |
| 4727 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4728 | if (!PageUptodate(page)) { |
| 4729 | ret = -EIO; |
Chris Mason | 8964222 | 2008-07-24 09:41:53 -0400 | [diff] [blame] | 4730 | goto out_unlock; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4731 | } |
| 4732 | } |
Chris Mason | 211c17f | 2008-05-15 09:13:45 -0400 | [diff] [blame] | 4733 | wait_on_page_writeback(page); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4734 | |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4735 | lock_extent_bits(io_tree, block_start, block_end, &cached_state); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4736 | set_page_extent_mapped(page); |
| 4737 | |
Nikolay Borisov | 217f42e | 2020-11-02 16:49:03 +0200 | [diff] [blame] | 4738 | ordered = btrfs_lookup_ordered_extent(inode, block_start); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4739 | if (ordered) { |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4740 | unlock_extent_cached(io_tree, block_start, block_end, |
David Sterba | e43bbe5 | 2017-12-12 21:43:52 +0100 | [diff] [blame] | 4741 | &cached_state); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4742 | unlock_page(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 4743 | put_page(page); |
Nikolay Borisov | c0a4360 | 2020-09-18 12:15:53 +0300 | [diff] [blame] | 4744 | btrfs_start_ordered_extent(ordered, 1); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4745 | btrfs_put_ordered_extent(ordered); |
| 4746 | goto again; |
| 4747 | } |
| 4748 | |
Nikolay Borisov | 217f42e | 2020-11-02 16:49:03 +0200 | [diff] [blame] | 4749 | clear_extent_bit(&inode->io_tree, block_start, block_end, |
Omar Sandoval | e182163 | 2019-08-15 14:04:04 -0700 | [diff] [blame] | 4750 | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, |
| 4751 | 0, 0, &cached_state); |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 4752 | |
Nikolay Borisov | 217f42e | 2020-11-02 16:49:03 +0200 | [diff] [blame] | 4753 | ret = btrfs_set_extent_delalloc(inode, block_start, block_end, 0, |
Nikolay Borisov | 330a582 | 2019-07-17 16:18:17 +0300 | [diff] [blame] | 4754 | &cached_state); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4755 | if (ret) { |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4756 | unlock_extent_cached(io_tree, block_start, block_end, |
David Sterba | e43bbe5 | 2017-12-12 21:43:52 +0100 | [diff] [blame] | 4757 | &cached_state); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4758 | goto out_unlock; |
| 4759 | } |
| 4760 | |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4761 | if (offset != blocksize) { |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 4762 | if (!len) |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4763 | len = blocksize - offset; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4764 | kaddr = kmap(page); |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 4765 | if (front) |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4766 | memset(kaddr + (block_start - page_offset(page)), |
| 4767 | 0, offset); |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 4768 | else |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4769 | memset(kaddr + (block_start - page_offset(page)) + offset, |
| 4770 | 0, len); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4771 | flush_dcache_page(page); |
| 4772 | kunmap(page); |
| 4773 | } |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 4774 | ClearPageChecked(page); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4775 | set_page_dirty(page); |
David Sterba | e43bbe5 | 2017-12-12 21:43:52 +0100 | [diff] [blame] | 4776 | unlock_extent_cached(io_tree, block_start, block_end, &cached_state); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4777 | |
Qu Wenruo | 6d4572a | 2020-06-24 07:23:50 +0800 | [diff] [blame] | 4778 | if (only_release_metadata) |
Nikolay Borisov | 217f42e | 2020-11-02 16:49:03 +0200 | [diff] [blame] | 4779 | set_extent_bit(&inode->io_tree, block_start, block_end, |
Nikolay Borisov | 1cab5e7 | 2020-11-05 11:08:00 +0200 | [diff] [blame] | 4780 | EXTENT_NORESERVE, 0, NULL, NULL, GFP_NOFS, NULL); |
Qu Wenruo | 6d4572a | 2020-06-24 07:23:50 +0800 | [diff] [blame] | 4781 | |
Chris Mason | 8964222 | 2008-07-24 09:41:53 -0400 | [diff] [blame] | 4782 | out_unlock: |
Qu Wenruo | 6d4572a | 2020-06-24 07:23:50 +0800 | [diff] [blame] | 4783 | if (ret) { |
| 4784 | if (only_release_metadata) |
Nikolay Borisov | 217f42e | 2020-11-02 16:49:03 +0200 | [diff] [blame] | 4785 | btrfs_delalloc_release_metadata(inode, blocksize, true); |
Qu Wenruo | 6d4572a | 2020-06-24 07:23:50 +0800 | [diff] [blame] | 4786 | else |
Nikolay Borisov | 217f42e | 2020-11-02 16:49:03 +0200 | [diff] [blame] | 4787 | btrfs_delalloc_release_space(inode, data_reserved, |
Qu Wenruo | 6d4572a | 2020-06-24 07:23:50 +0800 | [diff] [blame] | 4788 | block_start, blocksize, true); |
| 4789 | } |
Nikolay Borisov | 217f42e | 2020-11-02 16:49:03 +0200 | [diff] [blame] | 4790 | btrfs_delalloc_release_extents(inode, blocksize); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4791 | unlock_page(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 4792 | put_page(page); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4793 | out: |
Qu Wenruo | 6d4572a | 2020-06-24 07:23:50 +0800 | [diff] [blame] | 4794 | if (only_release_metadata) |
Nikolay Borisov | 217f42e | 2020-11-02 16:49:03 +0200 | [diff] [blame] | 4795 | btrfs_check_nocow_unlock(inode); |
Qu Wenruo | 364ecf3 | 2017-02-27 15:10:38 +0800 | [diff] [blame] | 4796 | extent_changeset_free(data_reserved); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4797 | return ret; |
| 4798 | } |
| 4799 | |
Nikolay Borisov | a4ba6cc | 2020-11-02 16:49:00 +0200 | [diff] [blame] | 4800 | 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] | 4801 | u64 offset, u64 len) |
| 4802 | { |
Nikolay Borisov | a4ba6cc | 2020-11-02 16:49:00 +0200 | [diff] [blame] | 4803 | struct btrfs_fs_info *fs_info = root->fs_info; |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4804 | struct btrfs_trans_handle *trans; |
Filipe Manana | 5893dfb | 2020-11-04 11:07:32 +0000 | [diff] [blame] | 4805 | struct btrfs_drop_extents_args drop_args = { 0 }; |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4806 | int ret; |
| 4807 | |
| 4808 | /* |
| 4809 | * Still need to make sure the inode looks like it's been updated so |
| 4810 | * that any holes get logged if we fsync. |
| 4811 | */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4812 | if (btrfs_fs_incompat(fs_info, NO_HOLES)) { |
Nikolay Borisov | a4ba6cc | 2020-11-02 16:49:00 +0200 | [diff] [blame] | 4813 | inode->last_trans = fs_info->generation; |
| 4814 | inode->last_sub_trans = root->log_transid; |
| 4815 | inode->last_log_commit = root->last_log_commit; |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4816 | return 0; |
| 4817 | } |
| 4818 | |
| 4819 | /* |
| 4820 | * 1 - for the one we're dropping |
| 4821 | * 1 - for the one we're adding |
| 4822 | * 1 - for updating the inode. |
| 4823 | */ |
| 4824 | trans = btrfs_start_transaction(root, 3); |
| 4825 | if (IS_ERR(trans)) |
| 4826 | return PTR_ERR(trans); |
| 4827 | |
Filipe Manana | 5893dfb | 2020-11-04 11:07:32 +0000 | [diff] [blame] | 4828 | drop_args.start = offset; |
| 4829 | drop_args.end = offset + len; |
| 4830 | drop_args.drop_cache = true; |
| 4831 | |
Nikolay Borisov | a4ba6cc | 2020-11-02 16:49:00 +0200 | [diff] [blame] | 4832 | ret = btrfs_drop_extents(trans, root, inode, &drop_args); |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4833 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 4834 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 4835 | btrfs_end_transaction(trans); |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4836 | return ret; |
| 4837 | } |
| 4838 | |
Nikolay Borisov | a4ba6cc | 2020-11-02 16:49:00 +0200 | [diff] [blame] | 4839 | ret = btrfs_insert_file_extent(trans, root, btrfs_ino(inode), |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 4840 | offset, 0, 0, len, 0, len, 0, 0, 0); |
Filipe Manana | 2766ff6 | 2020-11-04 11:07:34 +0000 | [diff] [blame] | 4841 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 4842 | btrfs_abort_transaction(trans, ret); |
Filipe Manana | 2766ff6 | 2020-11-04 11:07:34 +0000 | [diff] [blame] | 4843 | } else { |
Nikolay Borisov | a4ba6cc | 2020-11-02 16:49:00 +0200 | [diff] [blame] | 4844 | btrfs_update_inode_bytes(inode, 0, drop_args.bytes_found); |
| 4845 | btrfs_update_inode(trans, root, inode); |
Filipe Manana | 2766ff6 | 2020-11-04 11:07:34 +0000 | [diff] [blame] | 4846 | } |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 4847 | btrfs_end_transaction(trans); |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4848 | return ret; |
| 4849 | } |
| 4850 | |
Josef Bacik | 695a0d0 | 2011-03-04 15:46:53 -0500 | [diff] [blame] | 4851 | /* |
| 4852 | * This function puts in dummy file extents for the area we're creating a hole |
| 4853 | * for. So if we are truncating this file to a larger size we need to insert |
| 4854 | * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for |
| 4855 | * the range between oldsize and size |
| 4856 | */ |
Nikolay Borisov | b06359a | 2020-11-02 16:49:04 +0200 | [diff] [blame] | 4857 | 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] | 4858 | { |
Nikolay Borisov | b06359a | 2020-11-02 16:49:04 +0200 | [diff] [blame] | 4859 | struct btrfs_root *root = inode->root; |
| 4860 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 4861 | struct extent_io_tree *io_tree = &inode->io_tree; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4862 | struct extent_map *em = NULL; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4863 | struct extent_state *cached_state = NULL; |
Nikolay Borisov | b06359a | 2020-11-02 16:49:04 +0200 | [diff] [blame] | 4864 | struct extent_map_tree *em_tree = &inode->extent_tree; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4865 | u64 hole_start = ALIGN(oldsize, fs_info->sectorsize); |
| 4866 | u64 block_end = ALIGN(size, fs_info->sectorsize); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4867 | u64 last_byte; |
| 4868 | u64 cur_offset; |
| 4869 | u64 hole_size; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4870 | int err = 0; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4871 | |
Josef Bacik | a71754f | 2013-06-17 17:14:39 -0400 | [diff] [blame] | 4872 | /* |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4873 | * If our size started in the middle of a block we need to zero out the |
| 4874 | * 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] | 4875 | * expose stale data. |
| 4876 | */ |
Nikolay Borisov | b06359a | 2020-11-02 16:49:04 +0200 | [diff] [blame] | 4877 | err = btrfs_truncate_block(inode, oldsize, 0, 0); |
Josef Bacik | a71754f | 2013-06-17 17:14:39 -0400 | [diff] [blame] | 4878 | if (err) |
| 4879 | return err; |
| 4880 | |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4881 | if (size <= hole_start) |
| 4882 | return 0; |
| 4883 | |
Nikolay Borisov | b06359a | 2020-11-02 16:49:04 +0200 | [diff] [blame] | 4884 | btrfs_lock_and_flush_ordered_range(inode, hole_start, block_end - 1, |
| 4885 | &cached_state); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4886 | cur_offset = hole_start; |
| 4887 | while (1) { |
Nikolay Borisov | b06359a | 2020-11-02 16:49:04 +0200 | [diff] [blame] | 4888 | em = btrfs_get_extent(inode, NULL, 0, cur_offset, |
Omar Sandoval | 39b07b5 | 2019-12-02 17:34:23 -0800 | [diff] [blame] | 4889 | block_end - cur_offset); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4890 | if (IS_ERR(em)) { |
| 4891 | err = PTR_ERR(em); |
Zach Brown | f276795 | 2013-01-08 19:37:58 +0000 | [diff] [blame] | 4892 | em = NULL; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4893 | break; |
| 4894 | } |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4895 | last_byte = min(extent_map_end(em), block_end); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4896 | last_byte = ALIGN(last_byte, fs_info->sectorsize); |
Josef Bacik | 9ddc959 | 2020-01-17 09:02:22 -0500 | [diff] [blame] | 4897 | hole_size = last_byte - cur_offset; |
| 4898 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4899 | if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) { |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4900 | struct extent_map *hole_em; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4901 | |
Nikolay Borisov | b06359a | 2020-11-02 16:49:04 +0200 | [diff] [blame] | 4902 | err = maybe_insert_hole(root, inode, cur_offset, |
| 4903 | hole_size); |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4904 | if (err) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 4905 | break; |
Josef Bacik | 9ddc959 | 2020-01-17 09:02:22 -0500 | [diff] [blame] | 4906 | |
Nikolay Borisov | b06359a | 2020-11-02 16:49:04 +0200 | [diff] [blame] | 4907 | err = btrfs_inode_set_file_extent_range(inode, |
Josef Bacik | 9ddc959 | 2020-01-17 09:02:22 -0500 | [diff] [blame] | 4908 | cur_offset, hole_size); |
| 4909 | if (err) |
| 4910 | break; |
| 4911 | |
Nikolay Borisov | b06359a | 2020-11-02 16:49:04 +0200 | [diff] [blame] | 4912 | btrfs_drop_extent_cache(inode, cur_offset, |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4913 | cur_offset + hole_size - 1, 0); |
| 4914 | hole_em = alloc_extent_map(); |
| 4915 | if (!hole_em) { |
| 4916 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, |
Nikolay Borisov | b06359a | 2020-11-02 16:49:04 +0200 | [diff] [blame] | 4917 | &inode->runtime_flags); |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4918 | goto next; |
| 4919 | } |
| 4920 | hole_em->start = cur_offset; |
| 4921 | hole_em->len = hole_size; |
| 4922 | hole_em->orig_start = cur_offset; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4923 | |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4924 | hole_em->block_start = EXTENT_MAP_HOLE; |
| 4925 | hole_em->block_len = 0; |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 4926 | hole_em->orig_block_len = 0; |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 4927 | hole_em->ram_bytes = hole_size; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4928 | hole_em->compress_type = BTRFS_COMPRESS_NONE; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4929 | hole_em->generation = fs_info->generation; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4930 | |
| 4931 | while (1) { |
| 4932 | write_lock(&em_tree->lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 4933 | err = add_extent_mapping(em_tree, hole_em, 1); |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4934 | write_unlock(&em_tree->lock); |
| 4935 | if (err != -EEXIST) |
| 4936 | break; |
Nikolay Borisov | b06359a | 2020-11-02 16:49:04 +0200 | [diff] [blame] | 4937 | btrfs_drop_extent_cache(inode, cur_offset, |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4938 | cur_offset + |
| 4939 | hole_size - 1, 0); |
| 4940 | } |
| 4941 | free_extent_map(hole_em); |
Josef Bacik | 9ddc959 | 2020-01-17 09:02:22 -0500 | [diff] [blame] | 4942 | } else { |
Nikolay Borisov | b06359a | 2020-11-02 16:49:04 +0200 | [diff] [blame] | 4943 | err = btrfs_inode_set_file_extent_range(inode, |
Josef Bacik | 9ddc959 | 2020-01-17 09:02:22 -0500 | [diff] [blame] | 4944 | cur_offset, hole_size); |
| 4945 | if (err) |
| 4946 | break; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4947 | } |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4948 | next: |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4949 | free_extent_map(em); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4950 | em = NULL; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4951 | cur_offset = last_byte; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4952 | if (cur_offset >= block_end) |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4953 | break; |
| 4954 | } |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4955 | free_extent_map(em); |
David Sterba | e43bbe5 | 2017-12-12 21:43:52 +0100 | [diff] [blame] | 4956 | unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4957 | return err; |
| 4958 | } |
| 4959 | |
Eric Sandeen | 3972f26 | 2013-01-12 02:57:22 +0000 | [diff] [blame] | 4960 | static int btrfs_setsize(struct inode *inode, struct iattr *attr) |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4961 | { |
Miao Xie | f4a2f4c | 2011-12-14 20:12:01 -0500 | [diff] [blame] | 4962 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 4963 | struct btrfs_trans_handle *trans; |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 4964 | loff_t oldsize = i_size_read(inode); |
Eric Sandeen | 3972f26 | 2013-01-12 02:57:22 +0000 | [diff] [blame] | 4965 | loff_t newsize = attr->ia_size; |
| 4966 | int mask = attr->ia_valid; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4967 | int ret; |
| 4968 | |
Eric Sandeen | 3972f26 | 2013-01-12 02:57:22 +0000 | [diff] [blame] | 4969 | /* |
| 4970 | * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a |
| 4971 | * special case where we need to update the times despite not having |
| 4972 | * these flags set. For all other operations the VFS set these flags |
| 4973 | * explicitly if it wants a timestamp update. |
| 4974 | */ |
Christoph Hellwig | dff6efc | 2013-11-19 07:17:07 -0800 | [diff] [blame] | 4975 | if (newsize != oldsize) { |
| 4976 | inode_inc_iversion(inode); |
| 4977 | if (!(mask & (ATTR_CTIME | ATTR_MTIME))) |
| 4978 | inode->i_ctime = inode->i_mtime = |
Deepa Dinamani | c2050a4 | 2016-09-14 07:48:06 -0700 | [diff] [blame] | 4979 | current_time(inode); |
Christoph Hellwig | dff6efc | 2013-11-19 07:17:07 -0800 | [diff] [blame] | 4980 | } |
Eric Sandeen | 3972f26 | 2013-01-12 02:57:22 +0000 | [diff] [blame] | 4981 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 4982 | if (newsize > oldsize) { |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 4983 | /* |
David Sterba | ea14b57f | 2017-06-22 02:19:11 +0200 | [diff] [blame] | 4984 | * Don't do an expanding truncate while snapshotting is ongoing. |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 4985 | * This is to ensure the snapshot captures a fully consistent |
| 4986 | * state of this file - if the snapshot captures this expanding |
| 4987 | * truncation, it must capture all writes that happened before |
| 4988 | * this truncation. |
| 4989 | */ |
Nikolay Borisov | dcc3eb9 | 2020-01-30 14:59:45 +0200 | [diff] [blame] | 4990 | btrfs_drew_write_lock(&root->snapshot_lock); |
Nikolay Borisov | b06359a | 2020-11-02 16:49:04 +0200 | [diff] [blame] | 4991 | ret = btrfs_cont_expand(BTRFS_I(inode), oldsize, newsize); |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 4992 | if (ret) { |
Nikolay Borisov | dcc3eb9 | 2020-01-30 14:59:45 +0200 | [diff] [blame] | 4993 | btrfs_drew_write_unlock(&root->snapshot_lock); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4994 | return ret; |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 4995 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4996 | |
Miao Xie | f4a2f4c | 2011-12-14 20:12:01 -0500 | [diff] [blame] | 4997 | trans = btrfs_start_transaction(root, 1); |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 4998 | if (IS_ERR(trans)) { |
Nikolay Borisov | dcc3eb9 | 2020-01-30 14:59:45 +0200 | [diff] [blame] | 4999 | btrfs_drew_write_unlock(&root->snapshot_lock); |
Miao Xie | f4a2f4c | 2011-12-14 20:12:01 -0500 | [diff] [blame] | 5000 | return PTR_ERR(trans); |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 5001 | } |
Miao Xie | f4a2f4c | 2011-12-14 20:12:01 -0500 | [diff] [blame] | 5002 | |
| 5003 | i_size_write(inode, newsize); |
Nikolay Borisov | 76aea53 | 2020-11-02 16:48:53 +0200 | [diff] [blame] | 5004 | btrfs_inode_safe_disk_i_size_write(BTRFS_I(inode), 0); |
Chandan Rajendra | 27772b6 | 2016-01-21 15:56:03 +0530 | [diff] [blame] | 5005 | pagecache_isize_extended(inode, oldsize, newsize); |
Nikolay Borisov | 9a56fcd | 2020-11-02 16:48:59 +0200 | [diff] [blame] | 5006 | ret = btrfs_update_inode(trans, root, BTRFS_I(inode)); |
Nikolay Borisov | dcc3eb9 | 2020-01-30 14:59:45 +0200 | [diff] [blame] | 5007 | btrfs_drew_write_unlock(&root->snapshot_lock); |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 5008 | btrfs_end_transaction(trans); |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 5009 | } else { |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5010 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 5011 | /* |
| 5012 | * 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] | 5013 | * zero. Make sure any new writes to the file get on disk |
| 5014 | * on close. |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 5015 | */ |
| 5016 | if (newsize == 0) |
Nikolay Borisov | 1fd4033 | 2020-10-01 09:40:39 +0300 | [diff] [blame] | 5017 | set_bit(BTRFS_INODE_FLUSH_ON_CLOSE, |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 5018 | &BTRFS_I(inode)->runtime_flags); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5019 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 5020 | truncate_setsize(inode, newsize); |
Miao Xie | 2e60a51 | 2013-02-08 07:01:08 +0000 | [diff] [blame] | 5021 | |
Miao Xie | 2e60a51 | 2013-02-08 07:01:08 +0000 | [diff] [blame] | 5022 | inode_dio_wait(inode); |
Miao Xie | 2e60a51 | 2013-02-08 07:01:08 +0000 | [diff] [blame] | 5023 | |
Filipe Manana | 213e8c5 | 2018-02-06 20:40:31 +0000 | [diff] [blame] | 5024 | ret = btrfs_truncate(inode, newsize == oldsize); |
Josef Bacik | 7f4f6e0 | 2013-08-29 16:43:28 -0400 | [diff] [blame] | 5025 | if (ret && inode->i_nlink) { |
| 5026 | int err; |
| 5027 | |
| 5028 | /* |
Omar Sandoval | f7e9e8f | 2018-05-11 13:13:32 -0700 | [diff] [blame] | 5029 | * Truncate failed, so fix up the in-memory size. We |
| 5030 | * adjusted disk_i_size down as we removed extents, so |
| 5031 | * wait for disk_i_size to be stable and then update the |
| 5032 | * in-memory size to match. |
Josef Bacik | 7f4f6e0 | 2013-08-29 16:43:28 -0400 | [diff] [blame] | 5033 | */ |
Omar Sandoval | f7e9e8f | 2018-05-11 13:13:32 -0700 | [diff] [blame] | 5034 | err = btrfs_wait_ordered_range(inode, 0, (u64)-1); |
Josef Bacik | 7f4f6e0 | 2013-08-29 16:43:28 -0400 | [diff] [blame] | 5035 | if (err) |
Omar Sandoval | f7e9e8f | 2018-05-11 13:13:32 -0700 | [diff] [blame] | 5036 | return err; |
| 5037 | i_size_write(inode, BTRFS_I(inode)->disk_i_size); |
Josef Bacik | 7f4f6e0 | 2013-08-29 16:43:28 -0400 | [diff] [blame] | 5038 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5039 | } |
| 5040 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 5041 | return ret; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5042 | } |
| 5043 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5044 | static int btrfs_setattr(struct dentry *dentry, struct iattr *attr) |
| 5045 | { |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 5046 | struct inode *inode = d_inode(dentry); |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 5047 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5048 | int err; |
| 5049 | |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 5050 | if (btrfs_root_readonly(root)) |
| 5051 | return -EROFS; |
| 5052 | |
Jan Kara | 31051c8 | 2016-05-26 16:55:18 +0200 | [diff] [blame] | 5053 | err = setattr_prepare(dentry, attr); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5054 | if (err) |
| 5055 | return err; |
| 5056 | |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 5057 | if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) { |
Eric Sandeen | 3972f26 | 2013-01-12 02:57:22 +0000 | [diff] [blame] | 5058 | err = btrfs_setsize(inode, attr); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5059 | if (err) |
| 5060 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5061 | } |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 5062 | |
Christoph Hellwig | 1025774 | 2010-06-04 11:30:02 +0200 | [diff] [blame] | 5063 | if (attr->ia_valid) { |
| 5064 | setattr_copy(inode, attr); |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 5065 | inode_inc_iversion(inode); |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 5066 | err = btrfs_dirty_inode(inode); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 5067 | |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 5068 | if (!err && attr->ia_valid & ATTR_MODE) |
Christoph Hellwig | 996a710 | 2013-12-20 05:16:43 -0800 | [diff] [blame] | 5069 | err = posix_acl_chmod(inode, inode->i_mode); |
Christoph Hellwig | 1025774 | 2010-06-04 11:30:02 +0200 | [diff] [blame] | 5070 | } |
| 5071 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5072 | return err; |
| 5073 | } |
Chris Mason | 61295eb | 2008-01-14 16:24:38 -0500 | [diff] [blame] | 5074 | |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5075 | /* |
| 5076 | * While truncating the inode pages during eviction, we get the VFS calling |
| 5077 | * btrfs_invalidatepage() against each page of the inode. This is slow because |
| 5078 | * the calls to btrfs_invalidatepage() result in a huge amount of calls to |
| 5079 | * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting |
| 5080 | * extent_state structures over and over, wasting lots of time. |
| 5081 | * |
| 5082 | * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all |
| 5083 | * those expensive operations on a per page basis and do only the ordered io |
| 5084 | * finishing, while we release here the extent_map and extent_state structures, |
| 5085 | * without the excessive merging and splitting. |
| 5086 | */ |
| 5087 | static void evict_inode_truncate_pages(struct inode *inode) |
| 5088 | { |
| 5089 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
| 5090 | struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree; |
| 5091 | struct rb_node *node; |
| 5092 | |
| 5093 | ASSERT(inode->i_state & I_FREEING); |
Johannes Weiner | 91b0abe | 2014-04-03 14:47:49 -0700 | [diff] [blame] | 5094 | truncate_inode_pages_final(&inode->i_data); |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5095 | |
| 5096 | write_lock(&map_tree->lock); |
Liu Bo | 07e1ce0 | 2018-08-23 03:51:52 +0800 | [diff] [blame] | 5097 | while (!RB_EMPTY_ROOT(&map_tree->map.rb_root)) { |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5098 | struct extent_map *em; |
| 5099 | |
Liu Bo | 07e1ce0 | 2018-08-23 03:51:52 +0800 | [diff] [blame] | 5100 | node = rb_first_cached(&map_tree->map); |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5101 | em = rb_entry(node, struct extent_map, rb_node); |
Wang Shilong | 180589e | 2013-12-14 15:27:31 +0800 | [diff] [blame] | 5102 | clear_bit(EXTENT_FLAG_PINNED, &em->flags); |
| 5103 | clear_bit(EXTENT_FLAG_LOGGING, &em->flags); |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5104 | remove_extent_mapping(map_tree, em); |
| 5105 | free_extent_map(em); |
Filipe Manana | 7064dd5 | 2014-08-08 02:47:05 +0100 | [diff] [blame] | 5106 | if (need_resched()) { |
| 5107 | write_unlock(&map_tree->lock); |
| 5108 | cond_resched(); |
| 5109 | write_lock(&map_tree->lock); |
| 5110 | } |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5111 | } |
| 5112 | write_unlock(&map_tree->lock); |
| 5113 | |
Filipe Manana | 6ca0709 | 2015-05-26 00:55:42 +0100 | [diff] [blame] | 5114 | /* |
| 5115 | * 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] | 5116 | * We can have ongoing bios started by readahead that have |
| 5117 | * their endio callback (extent_io.c:end_bio_extent_readpage) |
Filipe Manana | 9c6429d | 2015-06-10 12:55:41 +0100 | [diff] [blame] | 5118 | * still in progress (unlocked the pages in the bio but did not yet |
| 5119 | * unlocked the ranges in the io tree). Therefore this means some |
Filipe Manana | 6ca0709 | 2015-05-26 00:55:42 +0100 | [diff] [blame] | 5120 | * ranges can still be locked and eviction started because before |
| 5121 | * submitting those bios, which are executed by a separate task (work |
| 5122 | * queue kthread), inode references (inode->i_count) were not taken |
| 5123 | * (which would be dropped in the end io callback of each bio). |
| 5124 | * Therefore here we effectively end up waiting for those bios and |
| 5125 | * anyone else holding locked ranges without having bumped the inode's |
| 5126 | * reference count - if we don't do it, when they access the inode's |
| 5127 | * io_tree to unlock a range it may be too late, leading to an |
| 5128 | * use-after-free issue. |
| 5129 | */ |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5130 | spin_lock(&io_tree->lock); |
| 5131 | while (!RB_EMPTY_ROOT(&io_tree->state)) { |
| 5132 | struct extent_state *state; |
| 5133 | struct extent_state *cached_state = NULL; |
Filipe Manana | 6ca0709 | 2015-05-26 00:55:42 +0100 | [diff] [blame] | 5134 | u64 start; |
| 5135 | u64 end; |
Filipe Manana | 421f092 | 2018-10-12 13:02:48 +0100 | [diff] [blame] | 5136 | unsigned state_flags; |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5137 | |
| 5138 | node = rb_first(&io_tree->state); |
| 5139 | state = rb_entry(node, struct extent_state, rb_node); |
Filipe Manana | 6ca0709 | 2015-05-26 00:55:42 +0100 | [diff] [blame] | 5140 | start = state->start; |
| 5141 | end = state->end; |
Filipe Manana | 421f092 | 2018-10-12 13:02:48 +0100 | [diff] [blame] | 5142 | state_flags = state->state; |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5143 | spin_unlock(&io_tree->lock); |
| 5144 | |
David Sterba | ff13db4 | 2015-12-03 14:30:40 +0100 | [diff] [blame] | 5145 | lock_extent_bits(io_tree, start, end, &cached_state); |
Qu Wenruo | b9d0b38 | 2015-09-29 10:35:16 +0800 | [diff] [blame] | 5146 | |
| 5147 | /* |
| 5148 | * If still has DELALLOC flag, the extent didn't reach disk, |
| 5149 | * and its reserved space won't be freed by delayed_ref. |
| 5150 | * So we need to free its reserved space here. |
| 5151 | * (Refer to comment in btrfs_invalidatepage, case 2) |
| 5152 | * |
| 5153 | * Note, end is the bytenr of last byte, so we need + 1 here. |
| 5154 | */ |
Filipe Manana | 421f092 | 2018-10-12 13:02:48 +0100 | [diff] [blame] | 5155 | if (state_flags & EXTENT_DELALLOC) |
Nikolay Borisov | 8b8a979 | 2020-06-03 08:55:11 +0300 | [diff] [blame] | 5156 | btrfs_qgroup_free_data(BTRFS_I(inode), NULL, start, |
| 5157 | end - start + 1); |
Qu Wenruo | b9d0b38 | 2015-09-29 10:35:16 +0800 | [diff] [blame] | 5158 | |
Filipe Manana | 6ca0709 | 2015-05-26 00:55:42 +0100 | [diff] [blame] | 5159 | clear_extent_bit(io_tree, start, end, |
Omar Sandoval | e182163 | 2019-08-15 14:04:04 -0700 | [diff] [blame] | 5160 | EXTENT_LOCKED | EXTENT_DELALLOC | |
| 5161 | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1, |
| 5162 | &cached_state); |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5163 | |
Filipe Manana | 7064dd5 | 2014-08-08 02:47:05 +0100 | [diff] [blame] | 5164 | cond_resched(); |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5165 | spin_lock(&io_tree->lock); |
| 5166 | } |
| 5167 | spin_unlock(&io_tree->lock); |
| 5168 | } |
| 5169 | |
Omar Sandoval | 4b9d7b5 | 2018-05-11 13:13:36 -0700 | [diff] [blame] | 5170 | 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] | 5171 | struct btrfs_block_rsv *rsv) |
Omar Sandoval | 4b9d7b5 | 2018-05-11 13:13:36 -0700 | [diff] [blame] | 5172 | { |
| 5173 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 5174 | struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv; |
Josef Bacik | d3984c9 | 2019-08-01 18:19:37 -0400 | [diff] [blame] | 5175 | struct btrfs_trans_handle *trans; |
Josef Bacik | 2bd36e7 | 2019-08-22 15:14:33 -0400 | [diff] [blame] | 5176 | u64 delayed_refs_extra = btrfs_calc_insert_metadata_size(fs_info, 1); |
Josef Bacik | d3984c9 | 2019-08-01 18:19:37 -0400 | [diff] [blame] | 5177 | int ret; |
Omar Sandoval | 4b9d7b5 | 2018-05-11 13:13:36 -0700 | [diff] [blame] | 5178 | |
Josef Bacik | d3984c9 | 2019-08-01 18:19:37 -0400 | [diff] [blame] | 5179 | /* |
| 5180 | * Eviction should be taking place at some place safe because of our |
| 5181 | * delayed iputs. However the normal flushing code will run delayed |
| 5182 | * iputs, so we cannot use FLUSH_ALL otherwise we'll deadlock. |
| 5183 | * |
| 5184 | * We reserve the delayed_refs_extra here again because we can't use |
| 5185 | * btrfs_start_transaction(root, 0) for the same deadlocky reason as |
| 5186 | * above. We reserve our extra bit here because we generate a ton of |
| 5187 | * delayed refs activity by truncating. |
| 5188 | * |
| 5189 | * If we cannot make our reservation we'll attempt to steal from the |
| 5190 | * global reserve, because we really want to be able to free up space. |
| 5191 | */ |
| 5192 | ret = btrfs_block_rsv_refill(root, rsv, rsv->size + delayed_refs_extra, |
| 5193 | BTRFS_RESERVE_FLUSH_EVICT); |
| 5194 | if (ret) { |
Omar Sandoval | 4b9d7b5 | 2018-05-11 13:13:36 -0700 | [diff] [blame] | 5195 | /* |
| 5196 | * Try to steal from the global reserve if there is space for |
| 5197 | * it. |
| 5198 | */ |
Josef Bacik | d3984c9 | 2019-08-01 18:19:37 -0400 | [diff] [blame] | 5199 | if (btrfs_check_space_for_delayed_refs(fs_info) || |
| 5200 | btrfs_block_rsv_migrate(global_rsv, rsv, rsv->size, 0)) { |
| 5201 | btrfs_warn(fs_info, |
| 5202 | "could not allocate space for delete; will truncate on mount"); |
| 5203 | return ERR_PTR(-ENOSPC); |
| 5204 | } |
| 5205 | delayed_refs_extra = 0; |
Omar Sandoval | 4b9d7b5 | 2018-05-11 13:13:36 -0700 | [diff] [blame] | 5206 | } |
Josef Bacik | d3984c9 | 2019-08-01 18:19:37 -0400 | [diff] [blame] | 5207 | |
| 5208 | trans = btrfs_join_transaction(root); |
| 5209 | if (IS_ERR(trans)) |
| 5210 | return trans; |
| 5211 | |
| 5212 | if (delayed_refs_extra) { |
| 5213 | trans->block_rsv = &fs_info->trans_block_rsv; |
| 5214 | trans->bytes_reserved = delayed_refs_extra; |
| 5215 | btrfs_block_rsv_migrate(rsv, trans->block_rsv, |
| 5216 | delayed_refs_extra, 1); |
| 5217 | } |
| 5218 | return trans; |
Omar Sandoval | 4b9d7b5 | 2018-05-11 13:13:36 -0700 | [diff] [blame] | 5219 | } |
| 5220 | |
Al Viro | bd55597 | 2010-06-07 11:35:40 -0400 | [diff] [blame] | 5221 | void btrfs_evict_inode(struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5222 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5223 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5224 | struct btrfs_trans_handle *trans; |
| 5225 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Omar Sandoval | 4b9d7b5 | 2018-05-11 13:13:36 -0700 | [diff] [blame] | 5226 | struct btrfs_block_rsv *rsv; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5227 | int ret; |
| 5228 | |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 5229 | trace_btrfs_inode_evict(inode); |
| 5230 | |
Nikolay Borisov | 3d48d98 | 2016-06-29 09:46:41 +0300 | [diff] [blame] | 5231 | if (!root) { |
Liu Bo | e8f1bc1 | 2018-01-25 11:02:53 -0700 | [diff] [blame] | 5232 | clear_inode(inode); |
Nikolay Borisov | 3d48d98 | 2016-06-29 09:46:41 +0300 | [diff] [blame] | 5233 | return; |
| 5234 | } |
| 5235 | |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5236 | evict_inode_truncate_pages(inode); |
| 5237 | |
Stefan Behrens | 69e9c6c | 2013-09-05 16:58:43 +0200 | [diff] [blame] | 5238 | if (inode->i_nlink && |
| 5239 | ((btrfs_root_refs(&root->root_item) != 0 && |
| 5240 | root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) || |
Nikolay Borisov | 70ddc55 | 2017-02-20 13:50:35 +0200 | [diff] [blame] | 5241 | btrfs_is_free_space_inode(BTRFS_I(inode)))) |
Al Viro | bd55597 | 2010-06-07 11:35:40 -0400 | [diff] [blame] | 5242 | goto no_delete; |
| 5243 | |
Omar Sandoval | 2791906 | 2018-05-11 13:13:37 -0700 | [diff] [blame] | 5244 | if (is_bad_inode(inode)) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5245 | goto no_delete; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5246 | |
Nikolay Borisov | 7ab7956 | 2017-02-20 13:50:57 +0200 | [diff] [blame] | 5247 | btrfs_free_io_failure_record(BTRFS_I(inode), 0, (u64)-1); |
Miao Xie | f612496 | 2014-09-12 18:44:04 +0800 | [diff] [blame] | 5248 | |
Omar Sandoval | 7b40b69 | 2018-05-11 13:13:33 -0700 | [diff] [blame] | 5249 | if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 5250 | goto no_delete; |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 5251 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5252 | if (inode->i_nlink > 0) { |
Stefan Behrens | 69e9c6c | 2013-09-05 16:58:43 +0200 | [diff] [blame] | 5253 | BUG_ON(btrfs_root_refs(&root->root_item) != 0 && |
| 5254 | root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5255 | goto no_delete; |
| 5256 | } |
| 5257 | |
Nikolay Borisov | aa79021 | 2017-01-10 20:35:40 +0200 | [diff] [blame] | 5258 | ret = btrfs_commit_inode_delayed_inode(BTRFS_I(inode)); |
Omar Sandoval | 2791906 | 2018-05-11 13:13:37 -0700 | [diff] [blame] | 5259 | if (ret) |
Miao Xie | 0e8c36a | 2012-12-19 06:59:51 +0000 | [diff] [blame] | 5260 | goto no_delete; |
Miao Xie | 0e8c36a | 2012-12-19 06:59:51 +0000 | [diff] [blame] | 5261 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5262 | rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP); |
Omar Sandoval | 2791906 | 2018-05-11 13:13:37 -0700 | [diff] [blame] | 5263 | if (!rsv) |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 5264 | goto no_delete; |
Josef Bacik | 2bd36e7 | 2019-08-22 15:14:33 -0400 | [diff] [blame] | 5265 | rsv->size = btrfs_calc_metadata_size(fs_info, 1); |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 5266 | rsv->failfast = 1; |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 5267 | |
Nikolay Borisov | 6ef06d2 | 2017-02-20 13:50:34 +0200 | [diff] [blame] | 5268 | btrfs_i_size_write(BTRFS_I(inode), 0); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5269 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5270 | while (1) { |
Josef Bacik | ad80cf5 | 2018-09-28 07:18:19 -0400 | [diff] [blame] | 5271 | trans = evict_refill_and_join(root, rsv); |
Omar Sandoval | 2791906 | 2018-05-11 13:13:37 -0700 | [diff] [blame] | 5272 | if (IS_ERR(trans)) |
| 5273 | goto free_rsv; |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 5274 | |
| 5275 | trans->block_rsv = rsv; |
| 5276 | |
Nikolay Borisov | 5074339 | 2020-11-02 16:48:55 +0200 | [diff] [blame] | 5277 | ret = btrfs_truncate_inode_items(trans, root, BTRFS_I(inode), |
| 5278 | 0, 0); |
Omar Sandoval | 2791906 | 2018-05-11 13:13:37 -0700 | [diff] [blame] | 5279 | trans->block_rsv = &fs_info->trans_block_rsv; |
| 5280 | btrfs_end_transaction(trans); |
| 5281 | btrfs_btree_balance_dirty(fs_info); |
| 5282 | if (ret && ret != -ENOSPC && ret != -EAGAIN) |
| 5283 | goto free_rsv; |
| 5284 | else if (!ret) |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5285 | break; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 5286 | } |
| 5287 | |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 5288 | /* |
Omar Sandoval | 2791906 | 2018-05-11 13:13:37 -0700 | [diff] [blame] | 5289 | * Errors here aren't a big deal, it just means we leave orphan items in |
| 5290 | * the tree. They will be cleaned up on the next mount. If the inode |
| 5291 | * number gets reused, cleanup deletes the orphan item without doing |
| 5292 | * anything, and unlink reuses the existing orphan item. |
| 5293 | * |
| 5294 | * If it turns out that we are dropping too many of these, we might want |
| 5295 | * to add a mechanism for retrying these after a commit. |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 5296 | */ |
Josef Bacik | ad80cf5 | 2018-09-28 07:18:19 -0400 | [diff] [blame] | 5297 | trans = evict_refill_and_join(root, rsv); |
Omar Sandoval | 2791906 | 2018-05-11 13:13:37 -0700 | [diff] [blame] | 5298 | if (!IS_ERR(trans)) { |
| 5299 | trans->block_rsv = rsv; |
| 5300 | btrfs_orphan_del(trans, BTRFS_I(inode)); |
| 5301 | trans->block_rsv = &fs_info->trans_block_rsv; |
| 5302 | btrfs_end_transaction(trans); |
| 5303 | } |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 5304 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5305 | if (!(root == fs_info->tree_root || |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 5306 | root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)) |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 5307 | btrfs_return_ino(root, btrfs_ino(BTRFS_I(inode))); |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 5308 | |
Omar Sandoval | 2791906 | 2018-05-11 13:13:37 -0700 | [diff] [blame] | 5309 | free_rsv: |
| 5310 | btrfs_free_block_rsv(fs_info, rsv); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5311 | no_delete: |
Omar Sandoval | 2791906 | 2018-05-11 13:13:37 -0700 | [diff] [blame] | 5312 | /* |
| 5313 | * If we didn't successfully delete, the orphan item will still be in |
| 5314 | * the tree and we'll retry on the next mount. Again, we might also want |
| 5315 | * to retry these periodically in the future. |
| 5316 | */ |
Nikolay Borisov | f48d1cf | 2017-01-10 20:35:39 +0200 | [diff] [blame] | 5317 | btrfs_remove_delayed_node(BTRFS_I(inode)); |
Jan Kara | dbd5768 | 2012-05-03 14:48:02 +0200 | [diff] [blame] | 5318 | clear_inode(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5319 | } |
| 5320 | |
| 5321 | /* |
Qu Wenruo | 6bf9e4b | 2019-03-13 13:55:11 +0800 | [diff] [blame] | 5322 | * Return the key found in the dir entry in the location pointer, fill @type |
| 5323 | * with BTRFS_FT_*, and return 0. |
| 5324 | * |
Su Yue | 005d671 | 2018-03-05 17:13:37 +0800 | [diff] [blame] | 5325 | * If no dir entries were found, returns -ENOENT. |
| 5326 | * If found a corrupted location in dir entry, returns -EUCLEAN. |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5327 | */ |
| 5328 | static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry, |
Qu Wenruo | 6bf9e4b | 2019-03-13 13:55:11 +0800 | [diff] [blame] | 5329 | struct btrfs_key *location, u8 *type) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5330 | { |
| 5331 | const char *name = dentry->d_name.name; |
| 5332 | int namelen = dentry->d_name.len; |
| 5333 | struct btrfs_dir_item *di; |
| 5334 | struct btrfs_path *path; |
| 5335 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Yan | 0d9f7f3 | 2007-10-25 15:48:28 -0400 | [diff] [blame] | 5336 | int ret = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5337 | |
| 5338 | path = btrfs_alloc_path(); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 5339 | if (!path) |
| 5340 | return -ENOMEM; |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 5341 | |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 5342 | di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(BTRFS_I(dir)), |
| 5343 | name, namelen, 0); |
Liu Bo | 3cf5068 | 2018-09-12 06:06:26 +0800 | [diff] [blame] | 5344 | if (IS_ERR_OR_NULL(di)) { |
| 5345 | ret = di ? PTR_ERR(di) : -ENOENT; |
Su Yue | 005d671 | 2018-03-05 17:13:37 +0800 | [diff] [blame] | 5346 | goto out; |
| 5347 | } |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5348 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5349 | btrfs_dir_item_key_to_cpu(path->nodes[0], di, location); |
Liu Bo | 56a0e70 | 2017-10-30 11:14:38 -0600 | [diff] [blame] | 5350 | if (location->type != BTRFS_INODE_ITEM_KEY && |
| 5351 | location->type != BTRFS_ROOT_ITEM_KEY) { |
Su Yue | 005d671 | 2018-03-05 17:13:37 +0800 | [diff] [blame] | 5352 | ret = -EUCLEAN; |
Liu Bo | 56a0e70 | 2017-10-30 11:14:38 -0600 | [diff] [blame] | 5353 | btrfs_warn(root->fs_info, |
| 5354 | "%s gets something invalid in DIR_ITEM (name %s, directory ino %llu, location(%llu %u %llu))", |
| 5355 | __func__, name, btrfs_ino(BTRFS_I(dir)), |
| 5356 | location->objectid, location->type, location->offset); |
Liu Bo | 56a0e70 | 2017-10-30 11:14:38 -0600 | [diff] [blame] | 5357 | } |
Qu Wenruo | 6bf9e4b | 2019-03-13 13:55:11 +0800 | [diff] [blame] | 5358 | if (!ret) |
| 5359 | *type = btrfs_dir_type(path->nodes[0], di); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5360 | out: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5361 | btrfs_free_path(path); |
| 5362 | return ret; |
| 5363 | } |
| 5364 | |
| 5365 | /* |
| 5366 | * when we hit a tree root in a directory, the btrfs part of the inode |
| 5367 | * needs to be changed to reflect the root directory of the tree root. This |
| 5368 | * is kind of like crossing a mount point. |
| 5369 | */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5370 | static int fixup_tree_root_location(struct btrfs_fs_info *fs_info, |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5371 | struct inode *dir, |
| 5372 | struct dentry *dentry, |
| 5373 | struct btrfs_key *location, |
| 5374 | struct btrfs_root **sub_root) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5375 | { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5376 | struct btrfs_path *path; |
| 5377 | struct btrfs_root *new_root; |
| 5378 | struct btrfs_root_ref *ref; |
| 5379 | struct extent_buffer *leaf; |
David Sterba | 1d4c08e | 2015-01-02 19:36:14 +0100 | [diff] [blame] | 5380 | struct btrfs_key key; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5381 | int ret; |
| 5382 | int err = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5383 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5384 | path = btrfs_alloc_path(); |
| 5385 | if (!path) { |
| 5386 | err = -ENOMEM; |
| 5387 | goto out; |
| 5388 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5389 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5390 | err = -ENOENT; |
David Sterba | 1d4c08e | 2015-01-02 19:36:14 +0100 | [diff] [blame] | 5391 | key.objectid = BTRFS_I(dir)->root->root_key.objectid; |
| 5392 | key.type = BTRFS_ROOT_REF_KEY; |
| 5393 | key.offset = location->objectid; |
| 5394 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5395 | 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] | 5396 | if (ret) { |
| 5397 | if (ret < 0) |
| 5398 | err = ret; |
| 5399 | goto out; |
| 5400 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5401 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5402 | leaf = path->nodes[0]; |
| 5403 | ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref); |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 5404 | if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(BTRFS_I(dir)) || |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5405 | btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len) |
| 5406 | goto out; |
| 5407 | |
| 5408 | ret = memcmp_extent_buffer(leaf, dentry->d_name.name, |
| 5409 | (unsigned long)(ref + 1), |
| 5410 | dentry->d_name.len); |
| 5411 | if (ret) |
| 5412 | goto out; |
| 5413 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 5414 | btrfs_release_path(path); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5415 | |
David Sterba | 56e9357 | 2020-05-15 19:35:55 +0200 | [diff] [blame] | 5416 | new_root = btrfs_get_fs_root(fs_info, location->objectid, true); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5417 | if (IS_ERR(new_root)) { |
| 5418 | err = PTR_ERR(new_root); |
| 5419 | goto out; |
| 5420 | } |
| 5421 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5422 | *sub_root = new_root; |
| 5423 | location->objectid = btrfs_root_dirid(&new_root->root_item); |
| 5424 | location->type = BTRFS_INODE_ITEM_KEY; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5425 | location->offset = 0; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5426 | err = 0; |
| 5427 | out: |
| 5428 | btrfs_free_path(path); |
| 5429 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5430 | } |
| 5431 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5432 | static void inode_tree_add(struct inode *inode) |
| 5433 | { |
| 5434 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5435 | struct btrfs_inode *entry; |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 5436 | struct rb_node **p; |
| 5437 | struct rb_node *parent; |
Filipe David Borba Manana | cef2193 | 2013-09-02 12:19:13 +0100 | [diff] [blame] | 5438 | struct rb_node *new = &BTRFS_I(inode)->rb_node; |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 5439 | u64 ino = btrfs_ino(BTRFS_I(inode)); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5440 | |
Al Viro | 1d3382cb | 2010-10-23 15:19:20 -0400 | [diff] [blame] | 5441 | if (inode_unhashed(inode)) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5442 | return; |
Miao Xie | e1409ce | 2013-05-15 07:48:16 +0000 | [diff] [blame] | 5443 | parent = NULL; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5444 | spin_lock(&root->inode_lock); |
Miao Xie | e1409ce | 2013-05-15 07:48:16 +0000 | [diff] [blame] | 5445 | p = &root->inode_tree.rb_node; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5446 | while (*p) { |
| 5447 | parent = *p; |
| 5448 | entry = rb_entry(parent, struct btrfs_inode, rb_node); |
| 5449 | |
David Sterba | 3750851 | 2018-06-29 10:56:40 +0200 | [diff] [blame] | 5450 | if (ino < btrfs_ino(entry)) |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 5451 | p = &parent->rb_left; |
David Sterba | 3750851 | 2018-06-29 10:56:40 +0200 | [diff] [blame] | 5452 | else if (ino > btrfs_ino(entry)) |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 5453 | p = &parent->rb_right; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5454 | else { |
| 5455 | WARN_ON(!(entry->vfs_inode.i_state & |
Al Viro | a4ffdde | 2010-06-02 17:38:30 -0400 | [diff] [blame] | 5456 | (I_WILL_FREE | I_FREEING))); |
Filipe David Borba Manana | cef2193 | 2013-09-02 12:19:13 +0100 | [diff] [blame] | 5457 | rb_replace_node(parent, new, &root->inode_tree); |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 5458 | RB_CLEAR_NODE(parent); |
| 5459 | spin_unlock(&root->inode_lock); |
Filipe David Borba Manana | cef2193 | 2013-09-02 12:19:13 +0100 | [diff] [blame] | 5460 | return; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5461 | } |
| 5462 | } |
Filipe David Borba Manana | cef2193 | 2013-09-02 12:19:13 +0100 | [diff] [blame] | 5463 | rb_link_node(new, parent, p); |
| 5464 | rb_insert_color(new, &root->inode_tree); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5465 | spin_unlock(&root->inode_lock); |
| 5466 | } |
| 5467 | |
Nikolay Borisov | b79b724 | 2020-08-31 14:42:38 +0300 | [diff] [blame] | 5468 | static void inode_tree_del(struct btrfs_inode *inode) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5469 | { |
Nikolay Borisov | b79b724 | 2020-08-31 14:42:38 +0300 | [diff] [blame] | 5470 | struct btrfs_root *root = inode->root; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5471 | int empty = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5472 | |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 5473 | spin_lock(&root->inode_lock); |
Nikolay Borisov | b79b724 | 2020-08-31 14:42:38 +0300 | [diff] [blame] | 5474 | if (!RB_EMPTY_NODE(&inode->rb_node)) { |
| 5475 | rb_erase(&inode->rb_node, &root->inode_tree); |
| 5476 | RB_CLEAR_NODE(&inode->rb_node); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5477 | empty = RB_EMPTY_ROOT(&root->inode_tree); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5478 | } |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 5479 | spin_unlock(&root->inode_lock); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5480 | |
Stefan Behrens | 69e9c6c | 2013-09-05 16:58:43 +0200 | [diff] [blame] | 5481 | if (empty && btrfs_root_refs(&root->root_item) == 0) { |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5482 | spin_lock(&root->inode_lock); |
| 5483 | empty = RB_EMPTY_ROOT(&root->inode_tree); |
| 5484 | spin_unlock(&root->inode_lock); |
| 5485 | if (empty) |
| 5486 | btrfs_add_dead_root(root); |
| 5487 | } |
| 5488 | } |
| 5489 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5490 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5491 | static int btrfs_init_locked_inode(struct inode *inode, void *p) |
| 5492 | { |
| 5493 | struct btrfs_iget_args *args = p; |
David Sterba | 0202e83 | 2020-05-15 19:35:59 +0200 | [diff] [blame] | 5494 | |
| 5495 | inode->i_ino = args->ino; |
| 5496 | BTRFS_I(inode)->location.objectid = args->ino; |
| 5497 | BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY; |
| 5498 | BTRFS_I(inode)->location.offset = 0; |
Josef Bacik | 5c8fd99 | 2020-02-14 16:11:43 -0500 | [diff] [blame] | 5499 | BTRFS_I(inode)->root = btrfs_grab_root(args->root); |
| 5500 | BUG_ON(args->root && !BTRFS_I(inode)->root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5501 | return 0; |
| 5502 | } |
| 5503 | |
| 5504 | static int btrfs_find_actor(struct inode *inode, void *opaque) |
| 5505 | { |
| 5506 | struct btrfs_iget_args *args = opaque; |
David Sterba | 0202e83 | 2020-05-15 19:35:59 +0200 | [diff] [blame] | 5507 | |
| 5508 | return args->ino == BTRFS_I(inode)->location.objectid && |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5509 | args->root == BTRFS_I(inode)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5510 | } |
| 5511 | |
David Sterba | 0202e83 | 2020-05-15 19:35:59 +0200 | [diff] [blame] | 5512 | static struct inode *btrfs_iget_locked(struct super_block *s, u64 ino, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5513 | struct btrfs_root *root) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5514 | { |
| 5515 | struct inode *inode; |
| 5516 | struct btrfs_iget_args args; |
David Sterba | 0202e83 | 2020-05-15 19:35:59 +0200 | [diff] [blame] | 5517 | unsigned long hashval = btrfs_inode_hash(ino, root); |
Filipe David Borba Manana | 778ba82 | 2013-10-06 22:22:33 +0100 | [diff] [blame] | 5518 | |
David Sterba | 0202e83 | 2020-05-15 19:35:59 +0200 | [diff] [blame] | 5519 | args.ino = ino; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5520 | args.root = root; |
| 5521 | |
Filipe David Borba Manana | 778ba82 | 2013-10-06 22:22:33 +0100 | [diff] [blame] | 5522 | inode = iget5_locked(s, hashval, btrfs_find_actor, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5523 | btrfs_init_locked_inode, |
| 5524 | (void *)&args); |
| 5525 | return inode; |
| 5526 | } |
| 5527 | |
David Sterba | 4c66e0d | 2019-10-03 19:09:35 +0200 | [diff] [blame] | 5528 | /* |
David Sterba | 0202e83 | 2020-05-15 19:35:59 +0200 | [diff] [blame] | 5529 | * Get an inode object given its inode number and corresponding root. |
David Sterba | 4c66e0d | 2019-10-03 19:09:35 +0200 | [diff] [blame] | 5530 | * Path can be preallocated to prevent recursing back to iget through |
| 5531 | * allocator. NULL is also valid but may require an additional allocation |
| 5532 | * later. |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 5533 | */ |
David Sterba | 0202e83 | 2020-05-15 19:35:59 +0200 | [diff] [blame] | 5534 | struct inode *btrfs_iget_path(struct super_block *s, u64 ino, |
David Sterba | 4c66e0d | 2019-10-03 19:09:35 +0200 | [diff] [blame] | 5535 | struct btrfs_root *root, struct btrfs_path *path) |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 5536 | { |
| 5537 | struct inode *inode; |
| 5538 | |
David Sterba | 0202e83 | 2020-05-15 19:35:59 +0200 | [diff] [blame] | 5539 | inode = btrfs_iget_locked(s, ino, root); |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 5540 | if (!inode) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5541 | return ERR_PTR(-ENOMEM); |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 5542 | |
| 5543 | if (inode->i_state & I_NEW) { |
Filipe Manana | 6771089 | 2016-06-06 11:51:25 +0100 | [diff] [blame] | 5544 | int ret; |
| 5545 | |
Filipe Manana | 4222ea7 | 2018-10-24 10:13:03 +0100 | [diff] [blame] | 5546 | ret = btrfs_read_locked_inode(inode, path); |
Al Viro | 9bc2cef | 2018-07-29 23:04:50 +0100 | [diff] [blame] | 5547 | if (!ret) { |
Mark Fasheh | 1748f84 | 2011-07-12 11:25:31 -0700 | [diff] [blame] | 5548 | inode_tree_add(inode); |
| 5549 | unlock_new_inode(inode); |
Mark Fasheh | 1748f84 | 2011-07-12 11:25:31 -0700 | [diff] [blame] | 5550 | } else { |
Al Viro | f5b3a41 | 2018-07-29 23:04:51 +0100 | [diff] [blame] | 5551 | iget_failed(inode); |
| 5552 | /* |
| 5553 | * ret > 0 can come from btrfs_search_slot called by |
| 5554 | * btrfs_read_locked_inode, this means the inode item |
| 5555 | * was not found. |
| 5556 | */ |
| 5557 | if (ret > 0) |
| 5558 | ret = -ENOENT; |
| 5559 | inode = ERR_PTR(ret); |
Mark Fasheh | 1748f84 | 2011-07-12 11:25:31 -0700 | [diff] [blame] | 5560 | } |
| 5561 | } |
| 5562 | |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 5563 | return inode; |
| 5564 | } |
| 5565 | |
David Sterba | 0202e83 | 2020-05-15 19:35:59 +0200 | [diff] [blame] | 5566 | 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] | 5567 | { |
David Sterba | 0202e83 | 2020-05-15 19:35:59 +0200 | [diff] [blame] | 5568 | return btrfs_iget_path(s, ino, root, NULL); |
Filipe Manana | 4222ea7 | 2018-10-24 10:13:03 +0100 | [diff] [blame] | 5569 | } |
| 5570 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5571 | static struct inode *new_simple_dir(struct super_block *s, |
| 5572 | struct btrfs_key *key, |
| 5573 | struct btrfs_root *root) |
| 5574 | { |
| 5575 | struct inode *inode = new_inode(s); |
| 5576 | |
| 5577 | if (!inode) |
| 5578 | return ERR_PTR(-ENOMEM); |
| 5579 | |
Josef Bacik | 5c8fd99 | 2020-02-14 16:11:43 -0500 | [diff] [blame] | 5580 | BTRFS_I(inode)->root = btrfs_grab_root(root); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5581 | memcpy(&BTRFS_I(inode)->location, key, sizeof(*key)); |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 5582 | set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5583 | |
| 5584 | inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID; |
Omar Sandoval | 6bb6b51 | 2019-12-05 10:36:04 -0800 | [diff] [blame] | 5585 | /* |
| 5586 | * We only need lookup, the rest is read-only and there's no inode |
| 5587 | * associated with the dentry |
| 5588 | */ |
| 5589 | inode->i_op = &simple_dir_inode_operations; |
Omar Sandoval | 1fdf419 | 2017-01-25 17:06:39 -0800 | [diff] [blame] | 5590 | inode->i_opflags &= ~IOP_XATTR; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5591 | inode->i_fop = &simple_dir_operations; |
| 5592 | inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO; |
Deepa Dinamani | c2050a4 | 2016-09-14 07:48:06 -0700 | [diff] [blame] | 5593 | inode->i_mtime = current_time(inode); |
chandan r | 9cc97d6 | 2012-07-04 12:48:07 +0530 | [diff] [blame] | 5594 | inode->i_atime = inode->i_mtime; |
| 5595 | inode->i_ctime = inode->i_mtime; |
Arnd Bergmann | d3c6be6f | 2018-06-21 18:04:06 +0200 | [diff] [blame] | 5596 | BTRFS_I(inode)->i_otime = inode->i_mtime; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5597 | |
| 5598 | return inode; |
| 5599 | } |
| 5600 | |
Qu Wenruo | 6bf9e4b | 2019-03-13 13:55:11 +0800 | [diff] [blame] | 5601 | static inline u8 btrfs_inode_type(struct inode *inode) |
| 5602 | { |
| 5603 | /* |
| 5604 | * Compile-time asserts that generic FT_* types still match |
| 5605 | * BTRFS_FT_* types |
| 5606 | */ |
| 5607 | BUILD_BUG_ON(BTRFS_FT_UNKNOWN != FT_UNKNOWN); |
| 5608 | BUILD_BUG_ON(BTRFS_FT_REG_FILE != FT_REG_FILE); |
| 5609 | BUILD_BUG_ON(BTRFS_FT_DIR != FT_DIR); |
| 5610 | BUILD_BUG_ON(BTRFS_FT_CHRDEV != FT_CHRDEV); |
| 5611 | BUILD_BUG_ON(BTRFS_FT_BLKDEV != FT_BLKDEV); |
| 5612 | BUILD_BUG_ON(BTRFS_FT_FIFO != FT_FIFO); |
| 5613 | BUILD_BUG_ON(BTRFS_FT_SOCK != FT_SOCK); |
| 5614 | BUILD_BUG_ON(BTRFS_FT_SYMLINK != FT_SYMLINK); |
| 5615 | |
| 5616 | return fs_umode_to_ftype(inode->i_mode); |
| 5617 | } |
| 5618 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5619 | struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5620 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5621 | struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5622 | struct inode *inode; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5623 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5624 | struct btrfs_root *sub_root = root; |
| 5625 | struct btrfs_key location; |
Qu Wenruo | 6bf9e4b | 2019-03-13 13:55:11 +0800 | [diff] [blame] | 5626 | u8 di_type = 0; |
Josef Bacik | b4aff1f | 2011-06-28 16:18:59 -0400 | [diff] [blame] | 5627 | int ret = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5628 | |
| 5629 | if (dentry->d_name.len > BTRFS_NAME_LEN) |
| 5630 | return ERR_PTR(-ENAMETOOLONG); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5631 | |
Qu Wenruo | 6bf9e4b | 2019-03-13 13:55:11 +0800 | [diff] [blame] | 5632 | ret = btrfs_inode_by_name(dir, dentry, &location, &di_type); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5633 | if (ret < 0) |
| 5634 | return ERR_PTR(ret); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5635 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5636 | if (location.type == BTRFS_INODE_ITEM_KEY) { |
David Sterba | 0202e83 | 2020-05-15 19:35:59 +0200 | [diff] [blame] | 5637 | inode = btrfs_iget(dir->i_sb, location.objectid, root); |
Qu Wenruo | 6bf9e4b | 2019-03-13 13:55:11 +0800 | [diff] [blame] | 5638 | if (IS_ERR(inode)) |
| 5639 | return inode; |
| 5640 | |
| 5641 | /* Do extra check against inode mode with di_type */ |
| 5642 | if (btrfs_inode_type(inode) != di_type) { |
| 5643 | btrfs_crit(fs_info, |
| 5644 | "inode mode mismatch with dir: inode mode=0%o btrfs type=%u dir type=%u", |
| 5645 | inode->i_mode, btrfs_inode_type(inode), |
| 5646 | di_type); |
| 5647 | iput(inode); |
| 5648 | return ERR_PTR(-EUCLEAN); |
| 5649 | } |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5650 | return inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5651 | } |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5652 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5653 | ret = fixup_tree_root_location(fs_info, dir, dentry, |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5654 | &location, &sub_root); |
| 5655 | if (ret < 0) { |
| 5656 | if (ret != -ENOENT) |
| 5657 | inode = ERR_PTR(ret); |
| 5658 | else |
| 5659 | inode = new_simple_dir(dir->i_sb, &location, sub_root); |
| 5660 | } else { |
David Sterba | 0202e83 | 2020-05-15 19:35:59 +0200 | [diff] [blame] | 5661 | inode = btrfs_iget(dir->i_sb, location.objectid, sub_root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5662 | } |
Josef Bacik | 8727002 | 2020-01-24 09:32:31 -0500 | [diff] [blame] | 5663 | if (root != sub_root) |
Josef Bacik | 0024652 | 2020-01-24 09:33:01 -0500 | [diff] [blame] | 5664 | btrfs_put_root(sub_root); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5665 | |
Julia Lawall | 34d19ba | 2011-01-24 19:55:19 +0000 | [diff] [blame] | 5666 | if (!IS_ERR(inode) && root != sub_root) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5667 | down_read(&fs_info->cleanup_work_sem); |
David Howells | bc98a42 | 2017-07-17 08:45:34 +0100 | [diff] [blame] | 5668 | if (!sb_rdonly(inode->i_sb)) |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 5669 | ret = btrfs_orphan_cleanup(sub_root); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5670 | up_read(&fs_info->cleanup_work_sem); |
Josef Bacik | 01cd336 | 2013-06-03 21:39:49 -0400 | [diff] [blame] | 5671 | if (ret) { |
| 5672 | iput(inode); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 5673 | inode = ERR_PTR(ret); |
Josef Bacik | 01cd336 | 2013-06-03 21:39:49 -0400 | [diff] [blame] | 5674 | } |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 5675 | } |
| 5676 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5677 | return inode; |
| 5678 | } |
| 5679 | |
Nick Piggin | fe15ce4 | 2011-01-07 17:49:23 +1100 | [diff] [blame] | 5680 | static int btrfs_dentry_delete(const struct dentry *dentry) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5681 | { |
| 5682 | struct btrfs_root *root; |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 5683 | struct inode *inode = d_inode(dentry); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5684 | |
Li Zefan | 848cce0 | 2012-02-21 17:04:28 +0800 | [diff] [blame] | 5685 | if (!inode && !IS_ROOT(dentry)) |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 5686 | inode = d_inode(dentry->d_parent); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5687 | |
Li Zefan | 848cce0 | 2012-02-21 17:04:28 +0800 | [diff] [blame] | 5688 | if (inode) { |
| 5689 | root = BTRFS_I(inode)->root; |
Yan, Zheng | efefb14 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5690 | if (btrfs_root_refs(&root->root_item) == 0) |
| 5691 | return 1; |
Li Zefan | 848cce0 | 2012-02-21 17:04:28 +0800 | [diff] [blame] | 5692 | |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 5693 | if (btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) |
Li Zefan | 848cce0 | 2012-02-21 17:04:28 +0800 | [diff] [blame] | 5694 | return 1; |
Yan, Zheng | efefb14 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5695 | } |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5696 | return 0; |
| 5697 | } |
| 5698 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5699 | static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry, |
Al Viro | 00cd8dd | 2012-06-10 17:13:09 -0400 | [diff] [blame] | 5700 | unsigned int flags) |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5701 | { |
Al Viro | 3837d20 | 2018-10-10 16:38:27 -0400 | [diff] [blame] | 5702 | struct inode *inode = btrfs_lookup_dentry(dir, dentry); |
Josef Bacik | a66e7cc | 2011-09-18 10:34:03 -0400 | [diff] [blame] | 5703 | |
Al Viro | 3837d20 | 2018-10-10 16:38:27 -0400 | [diff] [blame] | 5704 | if (inode == ERR_PTR(-ENOENT)) |
| 5705 | inode = NULL; |
Al Viro | 41d28bc | 2014-10-12 22:24:21 -0400 | [diff] [blame] | 5706 | return d_splice_alias(inode, dentry); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5707 | } |
| 5708 | |
Josef Bacik | 23b5ec7 | 2017-07-24 15:14:25 -0400 | [diff] [blame] | 5709 | /* |
| 5710 | * All this infrastructure exists because dir_emit can fault, and we are holding |
| 5711 | * the tree lock when doing readdir. For now just allocate a buffer and copy |
| 5712 | * our information into that, and then dir_emit from the buffer. This is |
| 5713 | * similar to what NFS does, only we don't keep the buffer around in pagecache |
| 5714 | * because I'm afraid I'll mess that up. Long term we need to make filldir do |
| 5715 | * copy_to_user_inatomic so we don't have to worry about page faulting under the |
| 5716 | * tree lock. |
| 5717 | */ |
| 5718 | static int btrfs_opendir(struct inode *inode, struct file *file) |
| 5719 | { |
| 5720 | struct btrfs_file_private *private; |
| 5721 | |
| 5722 | private = kzalloc(sizeof(struct btrfs_file_private), GFP_KERNEL); |
| 5723 | if (!private) |
| 5724 | return -ENOMEM; |
| 5725 | private->filldir_buf = kzalloc(PAGE_SIZE, GFP_KERNEL); |
| 5726 | if (!private->filldir_buf) { |
| 5727 | kfree(private); |
| 5728 | return -ENOMEM; |
| 5729 | } |
| 5730 | file->private_data = private; |
| 5731 | return 0; |
| 5732 | } |
| 5733 | |
| 5734 | struct dir_entry { |
| 5735 | u64 ino; |
| 5736 | u64 offset; |
| 5737 | unsigned type; |
| 5738 | int name_len; |
| 5739 | }; |
| 5740 | |
| 5741 | static int btrfs_filldir(void *addr, int entries, struct dir_context *ctx) |
| 5742 | { |
| 5743 | while (entries--) { |
| 5744 | struct dir_entry *entry = addr; |
| 5745 | char *name = (char *)(entry + 1); |
| 5746 | |
David Sterba | 92d3217 | 2018-04-16 21:10:14 +0200 | [diff] [blame] | 5747 | ctx->pos = get_unaligned(&entry->offset); |
| 5748 | if (!dir_emit(ctx, name, get_unaligned(&entry->name_len), |
| 5749 | get_unaligned(&entry->ino), |
| 5750 | get_unaligned(&entry->type))) |
Josef Bacik | 23b5ec7 | 2017-07-24 15:14:25 -0400 | [diff] [blame] | 5751 | return 1; |
David Sterba | 92d3217 | 2018-04-16 21:10:14 +0200 | [diff] [blame] | 5752 | addr += sizeof(struct dir_entry) + |
| 5753 | get_unaligned(&entry->name_len); |
Josef Bacik | 23b5ec7 | 2017-07-24 15:14:25 -0400 | [diff] [blame] | 5754 | ctx->pos++; |
| 5755 | } |
| 5756 | return 0; |
| 5757 | } |
| 5758 | |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5759 | static int btrfs_real_readdir(struct file *file, struct dir_context *ctx) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5760 | { |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5761 | struct inode *inode = file_inode(file); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5762 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Josef Bacik | 23b5ec7 | 2017-07-24 15:14:25 -0400 | [diff] [blame] | 5763 | struct btrfs_file_private *private = file->private_data; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5764 | struct btrfs_dir_item *di; |
| 5765 | struct btrfs_key key; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5766 | struct btrfs_key found_key; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5767 | struct btrfs_path *path; |
Josef Bacik | 23b5ec7 | 2017-07-24 15:14:25 -0400 | [diff] [blame] | 5768 | void *addr; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5769 | struct list_head ins_list; |
| 5770 | struct list_head del_list; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5771 | int ret; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5772 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5773 | int slot; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5774 | char *name_ptr; |
| 5775 | int name_len; |
Josef Bacik | 23b5ec7 | 2017-07-24 15:14:25 -0400 | [diff] [blame] | 5776 | int entries = 0; |
| 5777 | int total_len = 0; |
Omar Sandoval | 02dbfc9 | 2016-05-20 13:50:33 -0700 | [diff] [blame] | 5778 | bool put = false; |
Jeff Mahoney | c2951f3 | 2016-11-21 15:59:04 +0100 | [diff] [blame] | 5779 | struct btrfs_key location; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5780 | |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5781 | if (!dir_emit_dots(file, ctx)) |
| 5782 | return 0; |
| 5783 | |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5784 | path = btrfs_alloc_path(); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5785 | if (!path) |
| 5786 | return -ENOMEM; |
Chris Mason | ff5714c | 2011-05-28 07:00:39 -0400 | [diff] [blame] | 5787 | |
Josef Bacik | 23b5ec7 | 2017-07-24 15:14:25 -0400 | [diff] [blame] | 5788 | addr = private->filldir_buf; |
David Sterba | e4058b5 | 2015-11-27 16:31:35 +0100 | [diff] [blame] | 5789 | path->reada = READA_FORWARD; |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5790 | |
Jeff Mahoney | c2951f3 | 2016-11-21 15:59:04 +0100 | [diff] [blame] | 5791 | INIT_LIST_HEAD(&ins_list); |
| 5792 | INIT_LIST_HEAD(&del_list); |
| 5793 | put = btrfs_readdir_get_delayed_items(inode, &ins_list, &del_list); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5794 | |
Josef Bacik | 23b5ec7 | 2017-07-24 15:14:25 -0400 | [diff] [blame] | 5795 | again: |
Jeff Mahoney | c2951f3 | 2016-11-21 15:59:04 +0100 | [diff] [blame] | 5796 | key.type = BTRFS_DIR_INDEX_KEY; |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5797 | key.offset = ctx->pos; |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 5798 | key.objectid = btrfs_ino(BTRFS_I(inode)); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5799 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5800 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 5801 | if (ret < 0) |
| 5802 | goto err; |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5803 | |
| 5804 | while (1) { |
Josef Bacik | 23b5ec7 | 2017-07-24 15:14:25 -0400 | [diff] [blame] | 5805 | struct dir_entry *entry; |
| 5806 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5807 | leaf = path->nodes[0]; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5808 | slot = path->slots[0]; |
Li Zefan | b9e03af | 2011-03-23 10:43:58 +0800 | [diff] [blame] | 5809 | if (slot >= btrfs_header_nritems(leaf)) { |
| 5810 | ret = btrfs_next_leaf(root, path); |
| 5811 | if (ret < 0) |
| 5812 | goto err; |
| 5813 | else if (ret > 0) |
| 5814 | break; |
| 5815 | continue; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5816 | } |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5817 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5818 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
| 5819 | |
| 5820 | if (found_key.objectid != key.objectid) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5821 | break; |
Jeff Mahoney | c2951f3 | 2016-11-21 15:59:04 +0100 | [diff] [blame] | 5822 | if (found_key.type != BTRFS_DIR_INDEX_KEY) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5823 | break; |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5824 | if (found_key.offset < ctx->pos) |
Li Zefan | b9e03af | 2011-03-23 10:43:58 +0800 | [diff] [blame] | 5825 | goto next; |
Jeff Mahoney | c2951f3 | 2016-11-21 15:59:04 +0100 | [diff] [blame] | 5826 | if (btrfs_should_delete_dir_index(&del_list, found_key.offset)) |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5827 | goto next; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5828 | di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item); |
Jeff Mahoney | c2951f3 | 2016-11-21 15:59:04 +0100 | [diff] [blame] | 5829 | name_len = btrfs_dir_name_len(leaf, di); |
Josef Bacik | 23b5ec7 | 2017-07-24 15:14:25 -0400 | [diff] [blame] | 5830 | if ((total_len + sizeof(struct dir_entry) + name_len) >= |
| 5831 | PAGE_SIZE) { |
| 5832 | btrfs_release_path(path); |
| 5833 | ret = btrfs_filldir(private->filldir_buf, entries, ctx); |
| 5834 | if (ret) |
| 5835 | goto nopos; |
| 5836 | addr = private->filldir_buf; |
| 5837 | entries = 0; |
| 5838 | total_len = 0; |
| 5839 | goto again; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5840 | } |
Josef Bacik | 23b5ec7 | 2017-07-24 15:14:25 -0400 | [diff] [blame] | 5841 | |
| 5842 | entry = addr; |
David Sterba | 92d3217 | 2018-04-16 21:10:14 +0200 | [diff] [blame] | 5843 | put_unaligned(name_len, &entry->name_len); |
Josef Bacik | 23b5ec7 | 2017-07-24 15:14:25 -0400 | [diff] [blame] | 5844 | name_ptr = (char *)(entry + 1); |
Jeff Mahoney | c2951f3 | 2016-11-21 15:59:04 +0100 | [diff] [blame] | 5845 | read_extent_buffer(leaf, name_ptr, (unsigned long)(di + 1), |
| 5846 | name_len); |
Phillip Potter | 7d157c3 | 2019-03-26 21:39:34 +0000 | [diff] [blame] | 5847 | put_unaligned(fs_ftype_to_dtype(btrfs_dir_type(leaf, di)), |
David Sterba | 92d3217 | 2018-04-16 21:10:14 +0200 | [diff] [blame] | 5848 | &entry->type); |
Jeff Mahoney | c2951f3 | 2016-11-21 15:59:04 +0100 | [diff] [blame] | 5849 | btrfs_dir_item_key_to_cpu(leaf, di, &location); |
David Sterba | 92d3217 | 2018-04-16 21:10:14 +0200 | [diff] [blame] | 5850 | put_unaligned(location.objectid, &entry->ino); |
| 5851 | put_unaligned(found_key.offset, &entry->offset); |
Josef Bacik | 23b5ec7 | 2017-07-24 15:14:25 -0400 | [diff] [blame] | 5852 | entries++; |
| 5853 | addr += sizeof(struct dir_entry) + name_len; |
| 5854 | total_len += sizeof(struct dir_entry) + name_len; |
Li Zefan | b9e03af | 2011-03-23 10:43:58 +0800 | [diff] [blame] | 5855 | next: |
| 5856 | path->slots[0]++; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5857 | } |
Josef Bacik | 23b5ec7 | 2017-07-24 15:14:25 -0400 | [diff] [blame] | 5858 | btrfs_release_path(path); |
| 5859 | |
| 5860 | ret = btrfs_filldir(private->filldir_buf, entries, ctx); |
| 5861 | if (ret) |
| 5862 | goto nopos; |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5863 | |
Jeff Mahoney | d2fbb2b | 2016-11-05 13:26:35 -0400 | [diff] [blame] | 5864 | ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list); |
Jeff Mahoney | c2951f3 | 2016-11-21 15:59:04 +0100 | [diff] [blame] | 5865 | if (ret) |
David Sterba | bc4ef75 | 2015-11-13 13:44:28 +0100 | [diff] [blame] | 5866 | goto nopos; |
| 5867 | |
Zach Brown | db62efb | 2013-07-11 16:19:42 -0700 | [diff] [blame] | 5868 | /* |
| 5869 | * Stop new entries from being returned after we return the last |
| 5870 | * entry. |
| 5871 | * |
| 5872 | * New directory entries are assigned a strictly increasing |
| 5873 | * offset. This means that new entries created during readdir |
| 5874 | * are *guaranteed* to be seen in the future by that readdir. |
| 5875 | * This has broken buggy programs which operate on names as |
| 5876 | * they're returned by readdir. Until we re-use freed offsets |
| 5877 | * we have this hack to stop new entries from being returned |
| 5878 | * under the assumption that they'll never reach this huge |
| 5879 | * offset. |
| 5880 | * |
| 5881 | * This is being careful not to overflow 32bit loff_t unless the |
| 5882 | * last entry requires it because doing so has broken 32bit apps |
| 5883 | * in the past. |
| 5884 | */ |
Jeff Mahoney | c2951f3 | 2016-11-21 15:59:04 +0100 | [diff] [blame] | 5885 | if (ctx->pos >= INT_MAX) |
| 5886 | ctx->pos = LLONG_MAX; |
| 5887 | else |
| 5888 | ctx->pos = INT_MAX; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5889 | nopos: |
| 5890 | ret = 0; |
| 5891 | err: |
Omar Sandoval | 02dbfc9 | 2016-05-20 13:50:33 -0700 | [diff] [blame] | 5892 | if (put) |
| 5893 | btrfs_readdir_put_delayed_items(inode, &ins_list, &del_list); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5894 | btrfs_free_path(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5895 | return ret; |
| 5896 | } |
| 5897 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5898 | /* |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 5899 | * This is somewhat expensive, updating the tree every time the |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5900 | * inode changes. But, it is most likely to find the inode in cache. |
| 5901 | * FIXME, needs more benchmarking...there are no reasons other than performance |
| 5902 | * to keep or drop this code. |
| 5903 | */ |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 5904 | static int btrfs_dirty_inode(struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5905 | { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5906 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5907 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5908 | struct btrfs_trans_handle *trans; |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5909 | int ret; |
| 5910 | |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 5911 | if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags)) |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 5912 | return 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5913 | |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 5914 | trans = btrfs_join_transaction(root); |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 5915 | if (IS_ERR(trans)) |
| 5916 | return PTR_ERR(trans); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5917 | |
Nikolay Borisov | 9a56fcd | 2020-11-02 16:48:59 +0200 | [diff] [blame] | 5918 | ret = btrfs_update_inode(trans, root, BTRFS_I(inode)); |
Chris Mason | 94b6044 | 2010-05-26 11:02:00 -0400 | [diff] [blame] | 5919 | if (ret && ret == -ENOSPC) { |
| 5920 | /* whoops, lets try again with the full transaction */ |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 5921 | btrfs_end_transaction(trans); |
Chris Mason | 94b6044 | 2010-05-26 11:02:00 -0400 | [diff] [blame] | 5922 | trans = btrfs_start_transaction(root, 1); |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 5923 | if (IS_ERR(trans)) |
| 5924 | return PTR_ERR(trans); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5925 | |
Nikolay Borisov | 9a56fcd | 2020-11-02 16:48:59 +0200 | [diff] [blame] | 5926 | ret = btrfs_update_inode(trans, root, BTRFS_I(inode)); |
Chris Mason | 94b6044 | 2010-05-26 11:02:00 -0400 | [diff] [blame] | 5927 | } |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 5928 | btrfs_end_transaction(trans); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5929 | if (BTRFS_I(inode)->delayed_node) |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5930 | btrfs_balance_delayed_items(fs_info); |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 5931 | |
| 5932 | return ret; |
| 5933 | } |
| 5934 | |
| 5935 | /* |
| 5936 | * This is a copy of file_update_time. We need this so we can return error on |
| 5937 | * ENOSPC for updating the inode in the case of file write and mmap writes. |
| 5938 | */ |
Deepa Dinamani | 95582b0 | 2018-05-08 19:36:02 -0700 | [diff] [blame] | 5939 | static int btrfs_update_time(struct inode *inode, struct timespec64 *now, |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 5940 | int flags) |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 5941 | { |
Alexander Block | 2bc556528 | 2012-06-15 09:49:33 +0200 | [diff] [blame] | 5942 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Jeff Layton | 3a8c723 | 2017-12-11 06:35:24 -0500 | [diff] [blame] | 5943 | bool dirty = flags & ~S_VERSION; |
Alexander Block | 2bc556528 | 2012-06-15 09:49:33 +0200 | [diff] [blame] | 5944 | |
| 5945 | if (btrfs_root_readonly(root)) |
| 5946 | return -EROFS; |
| 5947 | |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 5948 | if (flags & S_VERSION) |
Jeff Layton | 3a8c723 | 2017-12-11 06:35:24 -0500 | [diff] [blame] | 5949 | dirty |= inode_maybe_inc_iversion(inode, dirty); |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 5950 | if (flags & S_CTIME) |
| 5951 | inode->i_ctime = *now; |
| 5952 | if (flags & S_MTIME) |
| 5953 | inode->i_mtime = *now; |
| 5954 | if (flags & S_ATIME) |
| 5955 | inode->i_atime = *now; |
Jeff Layton | 3a8c723 | 2017-12-11 06:35:24 -0500 | [diff] [blame] | 5956 | return dirty ? btrfs_dirty_inode(inode) : 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5957 | } |
| 5958 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 5959 | /* |
| 5960 | * find the highest existing sequence number in a directory |
| 5961 | * and then set the in-memory index_cnt variable to reflect |
| 5962 | * free sequence numbers |
| 5963 | */ |
Nikolay Borisov | 4c570655 | 2017-02-20 13:50:32 +0200 | [diff] [blame] | 5964 | static int btrfs_set_inode_index_count(struct btrfs_inode *inode) |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5965 | { |
Nikolay Borisov | 4c570655 | 2017-02-20 13:50:32 +0200 | [diff] [blame] | 5966 | struct btrfs_root *root = inode->root; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5967 | struct btrfs_key key, found_key; |
| 5968 | struct btrfs_path *path; |
| 5969 | struct extent_buffer *leaf; |
| 5970 | int ret; |
| 5971 | |
Nikolay Borisov | 4c570655 | 2017-02-20 13:50:32 +0200 | [diff] [blame] | 5972 | key.objectid = btrfs_ino(inode); |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 5973 | key.type = BTRFS_DIR_INDEX_KEY; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5974 | key.offset = (u64)-1; |
| 5975 | |
| 5976 | path = btrfs_alloc_path(); |
| 5977 | if (!path) |
| 5978 | return -ENOMEM; |
| 5979 | |
| 5980 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 5981 | if (ret < 0) |
| 5982 | goto out; |
| 5983 | /* FIXME: we should be able to handle this */ |
| 5984 | if (ret == 0) |
| 5985 | goto out; |
| 5986 | ret = 0; |
| 5987 | |
| 5988 | /* |
| 5989 | * MAGIC NUMBER EXPLANATION: |
| 5990 | * since we search a directory based on f_pos we have to start at 2 |
| 5991 | * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody |
| 5992 | * else has to start at 2 |
| 5993 | */ |
| 5994 | if (path->slots[0] == 0) { |
Nikolay Borisov | 4c570655 | 2017-02-20 13:50:32 +0200 | [diff] [blame] | 5995 | inode->index_cnt = 2; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5996 | goto out; |
| 5997 | } |
| 5998 | |
| 5999 | path->slots[0]--; |
| 6000 | |
| 6001 | leaf = path->nodes[0]; |
| 6002 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 6003 | |
Nikolay Borisov | 4c570655 | 2017-02-20 13:50:32 +0200 | [diff] [blame] | 6004 | if (found_key.objectid != btrfs_ino(inode) || |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 6005 | found_key.type != BTRFS_DIR_INDEX_KEY) { |
Nikolay Borisov | 4c570655 | 2017-02-20 13:50:32 +0200 | [diff] [blame] | 6006 | inode->index_cnt = 2; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6007 | goto out; |
| 6008 | } |
| 6009 | |
Nikolay Borisov | 4c570655 | 2017-02-20 13:50:32 +0200 | [diff] [blame] | 6010 | inode->index_cnt = found_key.offset + 1; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6011 | out: |
| 6012 | btrfs_free_path(path); |
| 6013 | return ret; |
| 6014 | } |
| 6015 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 6016 | /* |
| 6017 | * helper to find a free sequence number in a given directory. This current |
| 6018 | * code is very simple, later versions will do smarter things in the btree |
| 6019 | */ |
Nikolay Borisov | 877574e | 2017-02-20 13:50:33 +0200 | [diff] [blame] | 6020 | int btrfs_set_inode_index(struct btrfs_inode *dir, u64 *index) |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6021 | { |
| 6022 | int ret = 0; |
| 6023 | |
Nikolay Borisov | 877574e | 2017-02-20 13:50:33 +0200 | [diff] [blame] | 6024 | if (dir->index_cnt == (u64)-1) { |
| 6025 | ret = btrfs_inode_delayed_dir_index_count(dir); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 6026 | if (ret) { |
| 6027 | ret = btrfs_set_inode_index_count(dir); |
| 6028 | if (ret) |
| 6029 | return ret; |
| 6030 | } |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6031 | } |
| 6032 | |
Nikolay Borisov | 877574e | 2017-02-20 13:50:33 +0200 | [diff] [blame] | 6033 | *index = dir->index_cnt; |
| 6034 | dir->index_cnt++; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6035 | |
| 6036 | return ret; |
| 6037 | } |
| 6038 | |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6039 | static int btrfs_insert_inode_locked(struct inode *inode) |
| 6040 | { |
| 6041 | struct btrfs_iget_args args; |
David Sterba | 0202e83 | 2020-05-15 19:35:59 +0200 | [diff] [blame] | 6042 | |
| 6043 | args.ino = BTRFS_I(inode)->location.objectid; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6044 | args.root = BTRFS_I(inode)->root; |
| 6045 | |
| 6046 | return insert_inode_locked4(inode, |
| 6047 | btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root), |
| 6048 | btrfs_find_actor, &args); |
| 6049 | } |
| 6050 | |
Anand Jain | 19aee8d | 2017-07-18 17:37:05 +0800 | [diff] [blame] | 6051 | /* |
| 6052 | * Inherit flags from the parent inode. |
| 6053 | * |
| 6054 | * Currently only the compression flags and the cow flags are inherited. |
| 6055 | */ |
| 6056 | static void btrfs_inherit_iflags(struct inode *inode, struct inode *dir) |
| 6057 | { |
| 6058 | unsigned int flags; |
| 6059 | |
| 6060 | if (!dir) |
| 6061 | return; |
| 6062 | |
| 6063 | flags = BTRFS_I(dir)->flags; |
| 6064 | |
| 6065 | if (flags & BTRFS_INODE_NOCOMPRESS) { |
| 6066 | BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS; |
| 6067 | BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS; |
| 6068 | } else if (flags & BTRFS_INODE_COMPRESS) { |
| 6069 | BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS; |
| 6070 | BTRFS_I(inode)->flags |= BTRFS_INODE_COMPRESS; |
| 6071 | } |
| 6072 | |
| 6073 | if (flags & BTRFS_INODE_NODATACOW) { |
| 6074 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW; |
| 6075 | if (S_ISREG(inode->i_mode)) |
| 6076 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM; |
| 6077 | } |
| 6078 | |
David Sterba | 7b6a221 | 2018-03-26 18:40:21 +0200 | [diff] [blame] | 6079 | btrfs_sync_inode_flags_to_i_flags(inode); |
Anand Jain | 19aee8d | 2017-07-18 17:37:05 +0800 | [diff] [blame] | 6080 | } |
| 6081 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6082 | static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans, |
| 6083 | struct btrfs_root *root, |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6084 | struct inode *dir, |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 6085 | const char *name, int name_len, |
Al Viro | 175a4eb | 2011-07-26 03:30:54 -0400 | [diff] [blame] | 6086 | u64 ref_objectid, u64 objectid, |
| 6087 | umode_t mode, u64 *index) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6088 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6089 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6090 | struct inode *inode; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6091 | struct btrfs_inode_item *inode_item; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6092 | struct btrfs_key *location; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6093 | struct btrfs_path *path; |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 6094 | struct btrfs_inode_ref *ref; |
| 6095 | struct btrfs_key key[2]; |
| 6096 | u32 sizes[2]; |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 6097 | int nitems = name ? 2 : 1; |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 6098 | unsigned long ptr; |
Josef Bacik | 11a19a9 | 2019-09-09 10:12:04 -0400 | [diff] [blame] | 6099 | unsigned int nofs_flag; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6100 | int ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6101 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6102 | path = btrfs_alloc_path(); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 6103 | if (!path) |
| 6104 | return ERR_PTR(-ENOMEM); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6105 | |
Josef Bacik | 11a19a9 | 2019-09-09 10:12:04 -0400 | [diff] [blame] | 6106 | nofs_flag = memalloc_nofs_save(); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6107 | inode = new_inode(fs_info->sb); |
Josef Bacik | 11a19a9 | 2019-09-09 10:12:04 -0400 | [diff] [blame] | 6108 | memalloc_nofs_restore(nofs_flag); |
Yoshinori Sano | 8fb2764 | 2011-04-09 02:30:07 +0000 | [diff] [blame] | 6109 | if (!inode) { |
| 6110 | btrfs_free_path(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6111 | return ERR_PTR(-ENOMEM); |
Yoshinori Sano | 8fb2764 | 2011-04-09 02:30:07 +0000 | [diff] [blame] | 6112 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6113 | |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 6114 | /* |
Filipe Manana | 5762b5c | 2014-08-01 00:10:32 +0100 | [diff] [blame] | 6115 | * O_TMPFILE, set link count to 0, so that after this point, |
| 6116 | * we fill in an inode item with the correct link count. |
| 6117 | */ |
| 6118 | if (!name) |
| 6119 | set_nlink(inode, 0); |
| 6120 | |
| 6121 | /* |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 6122 | * we have to initialize this early, so we can reclaim the inode |
| 6123 | * number if we fail afterwards in this function. |
| 6124 | */ |
| 6125 | inode->i_ino = objectid; |
| 6126 | |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 6127 | if (dir && name) { |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 6128 | trace_btrfs_inode_request(dir); |
| 6129 | |
Nikolay Borisov | 877574e | 2017-02-20 13:50:33 +0200 | [diff] [blame] | 6130 | ret = btrfs_set_inode_index(BTRFS_I(dir), index); |
Shen Feng | 0977143 | 2009-04-02 16:46:06 -0400 | [diff] [blame] | 6131 | if (ret) { |
Yoshinori Sano | 8fb2764 | 2011-04-09 02:30:07 +0000 | [diff] [blame] | 6132 | btrfs_free_path(path); |
Shen Feng | 0977143 | 2009-04-02 16:46:06 -0400 | [diff] [blame] | 6133 | iput(inode); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6134 | return ERR_PTR(ret); |
Shen Feng | 0977143 | 2009-04-02 16:46:06 -0400 | [diff] [blame] | 6135 | } |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 6136 | } else if (dir) { |
| 6137 | *index = 0; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6138 | } |
| 6139 | /* |
| 6140 | * index_cnt is ignored for everything but a dir, |
Su Yue | df6703e | 2018-01-12 11:08:02 +0800 | [diff] [blame] | 6141 | * btrfs_set_inode_index_count has an explanation for the magic |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6142 | * number |
| 6143 | */ |
| 6144 | BTRFS_I(inode)->index_cnt = 2; |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 6145 | BTRFS_I(inode)->dir_index = *index; |
Josef Bacik | 5c8fd99 | 2020-02-14 16:11:43 -0500 | [diff] [blame] | 6146 | BTRFS_I(inode)->root = btrfs_grab_root(root); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 6147 | BTRFS_I(inode)->generation = trans->transid; |
Josef Bacik | 7619585 | 2010-11-19 02:18:02 +0000 | [diff] [blame] | 6148 | inode->i_generation = BTRFS_I(inode)->generation; |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6149 | |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 6150 | /* |
| 6151 | * We could have gotten an inode number from somebody who was fsynced |
| 6152 | * and then removed in this same transaction, so let's just set full |
| 6153 | * sync since it will be a full sync anyway and this will blow away the |
| 6154 | * old info in the log. |
| 6155 | */ |
| 6156 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags); |
| 6157 | |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 6158 | key[0].objectid = objectid; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 6159 | key[0].type = BTRFS_INODE_ITEM_KEY; |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 6160 | key[0].offset = 0; |
| 6161 | |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 6162 | sizes[0] = sizeof(struct btrfs_inode_item); |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 6163 | |
| 6164 | if (name) { |
| 6165 | /* |
| 6166 | * Start new inodes with an inode_ref. This is slightly more |
| 6167 | * efficient for small numbers of hard links since they will |
| 6168 | * be packed into one item. Extended refs will kick in if we |
| 6169 | * add more hard links than can fit in the ref item. |
| 6170 | */ |
| 6171 | key[1].objectid = objectid; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 6172 | key[1].type = BTRFS_INODE_REF_KEY; |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 6173 | key[1].offset = ref_objectid; |
| 6174 | |
| 6175 | sizes[1] = name_len + sizeof(*ref); |
| 6176 | } |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 6177 | |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6178 | location = &BTRFS_I(inode)->location; |
| 6179 | location->objectid = objectid; |
| 6180 | location->offset = 0; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 6181 | location->type = BTRFS_INODE_ITEM_KEY; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6182 | |
| 6183 | ret = btrfs_insert_inode_locked(inode); |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6184 | if (ret < 0) { |
| 6185 | iput(inode); |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6186 | goto fail; |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6187 | } |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6188 | |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 6189 | ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems); |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 6190 | if (ret != 0) |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6191 | goto fail_unlock; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6192 | |
Dmitry Monakhov | ecc11fab | 2010-03-04 17:31:47 +0300 | [diff] [blame] | 6193 | inode_init_owner(inode, dir, mode); |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 6194 | inode_set_bytes(inode, 0); |
chandan r | 9cc97d6 | 2012-07-04 12:48:07 +0530 | [diff] [blame] | 6195 | |
Deepa Dinamani | c2050a4 | 2016-09-14 07:48:06 -0700 | [diff] [blame] | 6196 | inode->i_mtime = current_time(inode); |
chandan r | 9cc97d6 | 2012-07-04 12:48:07 +0530 | [diff] [blame] | 6197 | inode->i_atime = inode->i_mtime; |
| 6198 | inode->i_ctime = inode->i_mtime; |
Arnd Bergmann | d3c6be6f | 2018-06-21 18:04:06 +0200 | [diff] [blame] | 6199 | BTRFS_I(inode)->i_otime = inode->i_mtime; |
chandan r | 9cc97d6 | 2012-07-04 12:48:07 +0530 | [diff] [blame] | 6200 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6201 | inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 6202 | struct btrfs_inode_item); |
David Sterba | b159fa2 | 2016-11-08 18:09:03 +0100 | [diff] [blame] | 6203 | memzero_extent_buffer(path->nodes[0], (unsigned long)inode_item, |
Li Zefan | 293f7e0 | 2012-07-10 00:58:58 -0600 | [diff] [blame] | 6204 | sizeof(*inode_item)); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 6205 | fill_inode_item(trans, path->nodes[0], inode_item, inode); |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 6206 | |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 6207 | if (name) { |
| 6208 | ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1, |
| 6209 | struct btrfs_inode_ref); |
| 6210 | btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len); |
| 6211 | btrfs_set_inode_ref_index(path->nodes[0], ref, *index); |
| 6212 | ptr = (unsigned long)(ref + 1); |
| 6213 | write_extent_buffer(path->nodes[0], name, ptr, name_len); |
| 6214 | } |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 6215 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6216 | btrfs_mark_buffer_dirty(path->nodes[0]); |
| 6217 | btrfs_free_path(path); |
| 6218 | |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 6219 | btrfs_inherit_iflags(inode, dir); |
| 6220 | |
Al Viro | 569254b | 2011-07-24 17:08:40 -0400 | [diff] [blame] | 6221 | if (S_ISREG(mode)) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6222 | if (btrfs_test_opt(fs_info, NODATASUM)) |
Chris Mason | 9427216 | 2009-07-02 12:26:06 -0400 | [diff] [blame] | 6223 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6224 | if (btrfs_test_opt(fs_info, NODATACOW)) |
Josef Bacik | f2bdf9a | 2013-02-21 15:28:28 -0500 | [diff] [blame] | 6225 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW | |
| 6226 | BTRFS_INODE_NODATASUM; |
Chris Mason | 9427216 | 2009-07-02 12:26:06 -0400 | [diff] [blame] | 6227 | } |
| 6228 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6229 | inode_tree_add(inode); |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 6230 | |
| 6231 | trace_btrfs_inode_new(inode); |
Nikolay Borisov | d909441 | 2020-06-05 10:41:13 +0300 | [diff] [blame] | 6232 | btrfs_set_inode_last_trans(trans, BTRFS_I(inode)); |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 6233 | |
Alexander Block | 8ea05e3 | 2012-07-25 17:35:53 +0200 | [diff] [blame] | 6234 | btrfs_update_root_times(trans, root); |
| 6235 | |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 6236 | ret = btrfs_inode_inherit_props(trans, inode, dir); |
| 6237 | if (ret) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6238 | btrfs_err(fs_info, |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 6239 | "error inheriting props for ino %llu (root %llu): %d", |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 6240 | btrfs_ino(BTRFS_I(inode)), root->root_key.objectid, ret); |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 6241 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6242 | return inode; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6243 | |
| 6244 | fail_unlock: |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6245 | discard_new_inode(inode); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6246 | fail: |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 6247 | if (dir && name) |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6248 | BTRFS_I(dir)->index_cnt--; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6249 | btrfs_free_path(path); |
| 6250 | return ERR_PTR(ret); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6251 | } |
| 6252 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 6253 | /* |
| 6254 | * utility function to add 'inode' into 'parent_inode' with |
| 6255 | * a give name and a given sequence number. |
| 6256 | * if 'add_backref' is true, also insert a backref from the |
| 6257 | * inode to the parent directory. |
| 6258 | */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 6259 | int btrfs_add_link(struct btrfs_trans_handle *trans, |
Nikolay Borisov | db0a669 | 2017-02-20 13:51:08 +0200 | [diff] [blame] | 6260 | struct btrfs_inode *parent_inode, struct btrfs_inode *inode, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 6261 | const char *name, int name_len, int add_backref, u64 index) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6262 | { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6263 | int ret = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6264 | struct btrfs_key key; |
Nikolay Borisov | db0a669 | 2017-02-20 13:51:08 +0200 | [diff] [blame] | 6265 | struct btrfs_root *root = parent_inode->root; |
| 6266 | u64 ino = btrfs_ino(inode); |
| 6267 | u64 parent_ino = btrfs_ino(parent_inode); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6268 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6269 | if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) { |
Nikolay Borisov | db0a669 | 2017-02-20 13:51:08 +0200 | [diff] [blame] | 6270 | memcpy(&key, &inode->root->root_key, sizeof(key)); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6271 | } else { |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6272 | key.objectid = ino; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 6273 | key.type = BTRFS_INODE_ITEM_KEY; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6274 | key.offset = 0; |
| 6275 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6276 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6277 | if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) { |
Lu Fengqi | 6025c19 | 2018-08-01 11:32:29 +0800 | [diff] [blame] | 6278 | ret = btrfs_add_root_ref(trans, key.objectid, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6279 | root->root_key.objectid, parent_ino, |
| 6280 | index, name, name_len); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6281 | } else if (add_backref) { |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6282 | ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino, |
| 6283 | parent_ino, index); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6284 | } |
| 6285 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6286 | /* Nothing to clean up yet */ |
| 6287 | if (ret) |
| 6288 | return ret; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6289 | |
Lu Fengqi | 684572d | 2018-08-04 21:10:57 +0800 | [diff] [blame] | 6290 | ret = btrfs_insert_dir_item(trans, name, name_len, parent_inode, &key, |
Nikolay Borisov | db0a669 | 2017-02-20 13:51:08 +0200 | [diff] [blame] | 6291 | btrfs_inode_type(&inode->vfs_inode), index); |
Chris Mason | 9c52057 | 2012-12-17 14:26:57 -0500 | [diff] [blame] | 6292 | if (ret == -EEXIST || ret == -EOVERFLOW) |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6293 | goto fail_dir_item; |
| 6294 | else if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 6295 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6296 | return ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6297 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6298 | |
Nikolay Borisov | db0a669 | 2017-02-20 13:51:08 +0200 | [diff] [blame] | 6299 | btrfs_i_size_write(parent_inode, parent_inode->vfs_inode.i_size + |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6300 | name_len * 2); |
Nikolay Borisov | db0a669 | 2017-02-20 13:51:08 +0200 | [diff] [blame] | 6301 | inode_inc_iversion(&parent_inode->vfs_inode); |
Filipe Manana | 5338e43 | 2019-05-15 16:02:47 +0100 | [diff] [blame] | 6302 | /* |
| 6303 | * If we are replaying a log tree, we do not want to update the mtime |
| 6304 | * and ctime of the parent directory with the current time, since the |
| 6305 | * log replay procedure is responsible for setting them to their correct |
| 6306 | * values (the ones it had when the fsync was done). |
| 6307 | */ |
| 6308 | if (!test_bit(BTRFS_FS_LOG_RECOVERING, &root->fs_info->flags)) { |
| 6309 | struct timespec64 now = current_time(&parent_inode->vfs_inode); |
| 6310 | |
| 6311 | parent_inode->vfs_inode.i_mtime = now; |
| 6312 | parent_inode->vfs_inode.i_ctime = now; |
| 6313 | } |
Nikolay Borisov | 9a56fcd | 2020-11-02 16:48:59 +0200 | [diff] [blame] | 6314 | ret = btrfs_update_inode(trans, root, parent_inode); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6315 | if (ret) |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 6316 | btrfs_abort_transaction(trans, ret); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6317 | return ret; |
Chris Mason | fe66a05 | 2012-02-20 08:40:56 -0500 | [diff] [blame] | 6318 | |
| 6319 | fail_dir_item: |
| 6320 | if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) { |
| 6321 | u64 local_index; |
| 6322 | int err; |
Lu Fengqi | 3ee1c55 | 2018-08-01 11:32:28 +0800 | [diff] [blame] | 6323 | err = btrfs_del_root_ref(trans, key.objectid, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6324 | root->root_key.objectid, parent_ino, |
| 6325 | &local_index, name, name_len); |
Johannes Thumshirn | 1690dd41 | 2018-12-12 15:14:17 +0100 | [diff] [blame] | 6326 | if (err) |
| 6327 | btrfs_abort_transaction(trans, err); |
Chris Mason | fe66a05 | 2012-02-20 08:40:56 -0500 | [diff] [blame] | 6328 | } else if (add_backref) { |
| 6329 | u64 local_index; |
| 6330 | int err; |
| 6331 | |
| 6332 | err = btrfs_del_inode_ref(trans, root, name, name_len, |
| 6333 | ino, parent_ino, &local_index); |
Johannes Thumshirn | 1690dd41 | 2018-12-12 15:14:17 +0100 | [diff] [blame] | 6334 | if (err) |
| 6335 | btrfs_abort_transaction(trans, err); |
Chris Mason | fe66a05 | 2012-02-20 08:40:56 -0500 | [diff] [blame] | 6336 | } |
Johannes Thumshirn | 1690dd41 | 2018-12-12 15:14:17 +0100 | [diff] [blame] | 6337 | |
| 6338 | /* Return the original error code */ |
Chris Mason | fe66a05 | 2012-02-20 08:40:56 -0500 | [diff] [blame] | 6339 | return ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6340 | } |
| 6341 | |
| 6342 | static int btrfs_add_nondir(struct btrfs_trans_handle *trans, |
Nikolay Borisov | cef415a | 2017-02-20 13:51:09 +0200 | [diff] [blame] | 6343 | struct btrfs_inode *dir, struct dentry *dentry, |
| 6344 | struct btrfs_inode *inode, int backref, u64 index) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6345 | { |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 6346 | int err = btrfs_add_link(trans, dir, inode, |
| 6347 | dentry->d_name.name, dentry->d_name.len, |
| 6348 | backref, index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6349 | if (err > 0) |
| 6350 | err = -EEXIST; |
| 6351 | return err; |
| 6352 | } |
| 6353 | |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6354 | static int btrfs_mknod(struct inode *dir, struct dentry *dentry, |
Al Viro | 1a67aaf | 2011-07-26 01:52:52 -0400 | [diff] [blame] | 6355 | umode_t mode, dev_t rdev) |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6356 | { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6357 | struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6358 | struct btrfs_trans_handle *trans; |
| 6359 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 6360 | struct inode *inode = NULL; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6361 | int err; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6362 | u64 objectid; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 6363 | u64 index = 0; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6364 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 6365 | /* |
| 6366 | * 2 for inode item and ref |
| 6367 | * 2 for dir items |
| 6368 | * 1 for xattr if selinux is on |
| 6369 | */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6370 | trans = btrfs_start_transaction(root, 5); |
| 6371 | if (IS_ERR(trans)) |
| 6372 | return PTR_ERR(trans); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 6373 | |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 6374 | err = btrfs_find_free_ino(root, &objectid); |
| 6375 | if (err) |
| 6376 | goto out_unlock; |
| 6377 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6378 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 6379 | dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid, |
| 6380 | mode, &index); |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 6381 | if (IS_ERR(inode)) { |
| 6382 | err = PTR_ERR(inode); |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6383 | inode = NULL; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6384 | goto out_unlock; |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 6385 | } |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6386 | |
Casey Schaufler | ad19db7 | 2011-12-15 10:09:07 -0500 | [diff] [blame] | 6387 | /* |
| 6388 | * If the active LSM wants to access the inode during |
| 6389 | * d_instantiate it needs these. Smack checks to see |
| 6390 | * if the filesystem supports xattrs by looking at the |
| 6391 | * ops vector. |
| 6392 | */ |
Casey Schaufler | ad19db7 | 2011-12-15 10:09:07 -0500 | [diff] [blame] | 6393 | inode->i_op = &btrfs_special_inode_operations; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6394 | init_special_inode(inode, inode->i_mode, rdev); |
| 6395 | |
| 6396 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6397 | if (err) |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6398 | goto out_unlock; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6399 | |
Nikolay Borisov | cef415a | 2017-02-20 13:51:09 +0200 | [diff] [blame] | 6400 | err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode), |
| 6401 | 0, index); |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6402 | if (err) |
| 6403 | goto out_unlock; |
| 6404 | |
Nikolay Borisov | 9a56fcd | 2020-11-02 16:48:59 +0200 | [diff] [blame] | 6405 | btrfs_update_inode(trans, root, BTRFS_I(inode)); |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6406 | d_instantiate_new(dentry, inode); |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6407 | |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6408 | out_unlock: |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 6409 | btrfs_end_transaction(trans); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6410 | btrfs_btree_balance_dirty(fs_info); |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6411 | if (err && inode) { |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6412 | inode_dec_link_count(inode); |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6413 | discard_new_inode(inode); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6414 | } |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6415 | return err; |
| 6416 | } |
| 6417 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6418 | static int btrfs_create(struct inode *dir, struct dentry *dentry, |
Al Viro | ebfc3b4 | 2012-06-10 18:05:36 -0400 | [diff] [blame] | 6419 | umode_t mode, bool excl) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6420 | { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6421 | struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6422 | struct btrfs_trans_handle *trans; |
| 6423 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 6424 | struct inode *inode = NULL; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6425 | int err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6426 | u64 objectid; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 6427 | u64 index = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6428 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 6429 | /* |
| 6430 | * 2 for inode item and ref |
| 6431 | * 2 for dir items |
| 6432 | * 1 for xattr if selinux is on |
| 6433 | */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6434 | trans = btrfs_start_transaction(root, 5); |
| 6435 | if (IS_ERR(trans)) |
| 6436 | return PTR_ERR(trans); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 6437 | |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 6438 | err = btrfs_find_free_ino(root, &objectid); |
| 6439 | if (err) |
| 6440 | goto out_unlock; |
| 6441 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6442 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 6443 | dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid, |
| 6444 | mode, &index); |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 6445 | if (IS_ERR(inode)) { |
| 6446 | err = PTR_ERR(inode); |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6447 | inode = NULL; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6448 | goto out_unlock; |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 6449 | } |
Casey Schaufler | ad19db7 | 2011-12-15 10:09:07 -0500 | [diff] [blame] | 6450 | /* |
| 6451 | * If the active LSM wants to access the inode during |
| 6452 | * d_instantiate it needs these. Smack checks to see |
| 6453 | * if the filesystem supports xattrs by looking at the |
| 6454 | * ops vector. |
| 6455 | */ |
| 6456 | inode->i_fop = &btrfs_file_operations; |
| 6457 | inode->i_op = &btrfs_file_inode_operations; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6458 | inode->i_mapping->a_ops = &btrfs_aops; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6459 | |
| 6460 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); |
| 6461 | if (err) |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6462 | goto out_unlock; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6463 | |
Nikolay Borisov | 9a56fcd | 2020-11-02 16:48:59 +0200 | [diff] [blame] | 6464 | err = btrfs_update_inode(trans, root, BTRFS_I(inode)); |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6465 | if (err) |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6466 | goto out_unlock; |
Casey Schaufler | ad19db7 | 2011-12-15 10:09:07 -0500 | [diff] [blame] | 6467 | |
Nikolay Borisov | cef415a | 2017-02-20 13:51:09 +0200 | [diff] [blame] | 6468 | err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode), |
| 6469 | 0, index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6470 | if (err) |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6471 | goto out_unlock; |
Filipe Brandenburger | 43baa57 | 2012-11-30 03:40:09 +0000 | [diff] [blame] | 6472 | |
Al Viro | 1e2e547 | 2018-05-04 08:23:01 -0400 | [diff] [blame] | 6473 | d_instantiate_new(dentry, inode); |
Filipe Brandenburger | 43baa57 | 2012-11-30 03:40:09 +0000 | [diff] [blame] | 6474 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6475 | out_unlock: |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 6476 | btrfs_end_transaction(trans); |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6477 | if (err && inode) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6478 | inode_dec_link_count(inode); |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6479 | discard_new_inode(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6480 | } |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6481 | btrfs_btree_balance_dirty(fs_info); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6482 | return err; |
| 6483 | } |
| 6484 | |
| 6485 | static int btrfs_link(struct dentry *old_dentry, struct inode *dir, |
| 6486 | struct dentry *dentry) |
| 6487 | { |
Filipe Manana | 271dba45 | 2016-01-05 16:24:05 +0000 | [diff] [blame] | 6488 | struct btrfs_trans_handle *trans = NULL; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6489 | struct btrfs_root *root = BTRFS_I(dir)->root; |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 6490 | struct inode *inode = d_inode(old_dentry); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6491 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 6492 | u64 index; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6493 | int err; |
| 6494 | int drop_inode = 0; |
| 6495 | |
TARUISI Hiroaki | 4a8be42 | 2009-11-12 07:14:26 +0000 | [diff] [blame] | 6496 | /* 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] | 6497 | if (root->root_key.objectid != BTRFS_I(inode)->root->root_key.objectid) |
Mark Fasheh | 3ab3564 | 2011-03-22 17:20:26 +0000 | [diff] [blame] | 6498 | return -EXDEV; |
TARUISI Hiroaki | 4a8be42 | 2009-11-12 07:14:26 +0000 | [diff] [blame] | 6499 | |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 6500 | if (inode->i_nlink >= BTRFS_LINK_MAX) |
Al Viro | c055e99 | 2011-03-04 17:15:18 +0000 | [diff] [blame] | 6501 | return -EMLINK; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 6502 | |
Nikolay Borisov | 877574e | 2017-02-20 13:50:33 +0200 | [diff] [blame] | 6503 | err = btrfs_set_inode_index(BTRFS_I(dir), &index); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6504 | if (err) |
| 6505 | goto fail; |
| 6506 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6507 | /* |
Miao Xie | 7e6b646 | 2011-02-18 09:21:17 +0000 | [diff] [blame] | 6508 | * 2 items for inode and inode ref |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6509 | * 2 items for dir items |
Miao Xie | 7e6b646 | 2011-02-18 09:21:17 +0000 | [diff] [blame] | 6510 | * 1 item for parent inode |
Omar Sandoval | 399b0bb | 2018-05-11 13:13:40 -0700 | [diff] [blame] | 6511 | * 1 item for orphan item deletion if O_TMPFILE |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6512 | */ |
Omar Sandoval | 399b0bb | 2018-05-11 13:13:40 -0700 | [diff] [blame] | 6513 | trans = btrfs_start_transaction(root, inode->i_nlink ? 5 : 6); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6514 | if (IS_ERR(trans)) { |
| 6515 | err = PTR_ERR(trans); |
Filipe Manana | 271dba45 | 2016-01-05 16:24:05 +0000 | [diff] [blame] | 6516 | trans = NULL; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6517 | goto fail; |
| 6518 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6519 | |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 6520 | /* There are several dir indexes for this inode, clear the cache. */ |
| 6521 | BTRFS_I(inode)->dir_index = 0ULL; |
Zach Brown | 8b558c5 | 2013-10-16 12:10:34 -0700 | [diff] [blame] | 6522 | inc_nlink(inode); |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 6523 | inode_inc_iversion(inode); |
Deepa Dinamani | c2050a4 | 2016-09-14 07:48:06 -0700 | [diff] [blame] | 6524 | inode->i_ctime = current_time(inode); |
Al Viro | 7de9c6ee | 2010-10-23 11:11:40 -0400 | [diff] [blame] | 6525 | ihold(inode); |
Josef Bacik | e997615 | 2012-10-11 15:53:56 -0400 | [diff] [blame] | 6526 | set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6527 | |
Nikolay Borisov | cef415a | 2017-02-20 13:51:09 +0200 | [diff] [blame] | 6528 | err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode), |
| 6529 | 1, index); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6530 | |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 6531 | if (err) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6532 | drop_inode = 1; |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 6533 | } else { |
Al Viro | 10d9f30 | 2011-07-16 23:09:10 -0400 | [diff] [blame] | 6534 | struct dentry *parent = dentry->d_parent; |
Filipe Manana | d4682ba | 2018-06-11 19:24:28 +0100 | [diff] [blame] | 6535 | |
Nikolay Borisov | 9a56fcd | 2020-11-02 16:48:59 +0200 | [diff] [blame] | 6536 | err = btrfs_update_inode(trans, root, BTRFS_I(inode)); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6537 | if (err) |
| 6538 | goto fail; |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 6539 | if (inode->i_nlink == 1) { |
| 6540 | /* |
| 6541 | * If new hard link count is 1, it's a file created |
| 6542 | * with open(2) O_TMPFILE flag. |
| 6543 | */ |
Nikolay Borisov | 3d6ae7b | 2017-02-20 13:50:58 +0200 | [diff] [blame] | 6544 | err = btrfs_orphan_del(trans, BTRFS_I(inode)); |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 6545 | if (err) |
| 6546 | goto fail; |
| 6547 | } |
Al Viro | 08c422c | 2011-12-23 07:58:13 -0500 | [diff] [blame] | 6548 | d_instantiate(dentry, inode); |
Filipe Manana | 75b463d | 2020-08-11 12:43:48 +0100 | [diff] [blame] | 6549 | btrfs_log_new_name(trans, BTRFS_I(inode), NULL, parent); |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 6550 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6551 | |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 6552 | fail: |
Filipe Manana | 271dba45 | 2016-01-05 16:24:05 +0000 | [diff] [blame] | 6553 | if (trans) |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 6554 | btrfs_end_transaction(trans); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6555 | if (drop_inode) { |
| 6556 | inode_dec_link_count(inode); |
| 6557 | iput(inode); |
| 6558 | } |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6559 | btrfs_btree_balance_dirty(fs_info); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6560 | return err; |
| 6561 | } |
| 6562 | |
Al Viro | 18bb1db | 2011-07-26 01:41:39 -0400 | [diff] [blame] | 6563 | static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6564 | { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6565 | struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); |
Chris Mason | b9d8666 | 2008-05-02 16:13:49 -0400 | [diff] [blame] | 6566 | struct inode *inode = NULL; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6567 | struct btrfs_trans_handle *trans; |
| 6568 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 6569 | int err = 0; |
Chris Mason | b9d8666 | 2008-05-02 16:13:49 -0400 | [diff] [blame] | 6570 | u64 objectid = 0; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 6571 | u64 index = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6572 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 6573 | /* |
| 6574 | * 2 items for inode and ref |
| 6575 | * 2 items for dir items |
| 6576 | * 1 for xattr if selinux is on |
| 6577 | */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6578 | trans = btrfs_start_transaction(root, 5); |
| 6579 | if (IS_ERR(trans)) |
| 6580 | return PTR_ERR(trans); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6581 | |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 6582 | err = btrfs_find_free_ino(root, &objectid); |
| 6583 | if (err) |
| 6584 | goto out_fail; |
| 6585 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6586 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 6587 | dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid, |
| 6588 | S_IFDIR | mode, &index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6589 | if (IS_ERR(inode)) { |
| 6590 | err = PTR_ERR(inode); |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6591 | inode = NULL; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6592 | goto out_fail; |
| 6593 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6594 | |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6595 | /* these must be set before we unlock the inode */ |
| 6596 | inode->i_op = &btrfs_dir_inode_operations; |
| 6597 | inode->i_fop = &btrfs_dir_file_operations; |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 6598 | |
Eric Paris | 2a7dba3 | 2011-02-01 11:05:39 -0500 | [diff] [blame] | 6599 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 6600 | if (err) |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6601 | goto out_fail; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6602 | |
Nikolay Borisov | 6ef06d2 | 2017-02-20 13:50:34 +0200 | [diff] [blame] | 6603 | btrfs_i_size_write(BTRFS_I(inode), 0); |
Nikolay Borisov | 9a56fcd | 2020-11-02 16:48:59 +0200 | [diff] [blame] | 6604 | err = btrfs_update_inode(trans, root, BTRFS_I(inode)); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6605 | if (err) |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6606 | goto out_fail; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6607 | |
Nikolay Borisov | db0a669 | 2017-02-20 13:51:08 +0200 | [diff] [blame] | 6608 | err = btrfs_add_link(trans, BTRFS_I(dir), BTRFS_I(inode), |
| 6609 | dentry->d_name.name, |
| 6610 | dentry->d_name.len, 0, index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6611 | if (err) |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6612 | goto out_fail; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6613 | |
Al Viro | 1e2e547 | 2018-05-04 08:23:01 -0400 | [diff] [blame] | 6614 | d_instantiate_new(dentry, inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6615 | |
| 6616 | out_fail: |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 6617 | btrfs_end_transaction(trans); |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6618 | if (err && inode) { |
Wang Shilong | c7cfb8a | 2014-12-24 14:45:30 +0800 | [diff] [blame] | 6619 | inode_dec_link_count(inode); |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6620 | discard_new_inode(inode); |
Wang Shilong | c7cfb8a | 2014-12-24 14:45:30 +0800 | [diff] [blame] | 6621 | } |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6622 | btrfs_btree_balance_dirty(fs_info); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6623 | return err; |
| 6624 | } |
| 6625 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6626 | static noinline int uncompress_inline(struct btrfs_path *path, |
Byongho Lee | e40da0e | 2015-05-19 23:46:45 +0900 | [diff] [blame] | 6627 | struct page *page, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6628 | size_t pg_offset, u64 extent_offset, |
| 6629 | struct btrfs_file_extent_item *item) |
| 6630 | { |
| 6631 | int ret; |
| 6632 | struct extent_buffer *leaf = path->nodes[0]; |
| 6633 | char *tmp; |
| 6634 | size_t max_size; |
| 6635 | unsigned long inline_size; |
| 6636 | unsigned long ptr; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 6637 | int compress_type; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6638 | |
| 6639 | WARN_ON(pg_offset != 0); |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 6640 | compress_type = btrfs_file_extent_compression(leaf, item); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6641 | max_size = btrfs_file_extent_ram_bytes(leaf, item); |
| 6642 | inline_size = btrfs_file_extent_inline_item_len(leaf, |
Ross Kirk | dd3cc16 | 2013-09-16 15:58:09 +0100 | [diff] [blame] | 6643 | btrfs_item_nr(path->slots[0])); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6644 | tmp = kmalloc(inline_size, GFP_NOFS); |
Tsutomu Itoh | 8d41371 | 2011-04-25 19:43:52 -0400 | [diff] [blame] | 6645 | if (!tmp) |
| 6646 | return -ENOMEM; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6647 | ptr = btrfs_file_extent_inline_start(item); |
| 6648 | |
| 6649 | read_extent_buffer(leaf, tmp, ptr, inline_size); |
| 6650 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 6651 | max_size = min_t(unsigned long, PAGE_SIZE, max_size); |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 6652 | ret = btrfs_decompress(compress_type, tmp, page, |
| 6653 | extent_offset, inline_size, max_size); |
Zygo Blaxell | e1699d2 | 2017-03-10 16:45:44 -0500 | [diff] [blame] | 6654 | |
| 6655 | /* |
| 6656 | * decompression code contains a memset to fill in any space between the end |
| 6657 | * of the uncompressed data and the end of max_size in case the decompressed |
| 6658 | * data ends up shorter than ram_bytes. That doesn't cover the hole between |
| 6659 | * the end of an inline extent and the beginning of the next block, so we |
| 6660 | * cover that region here. |
| 6661 | */ |
| 6662 | |
| 6663 | if (max_size + pg_offset < PAGE_SIZE) { |
| 6664 | char *map = kmap(page); |
| 6665 | memset(map + pg_offset + max_size, 0, PAGE_SIZE - max_size - pg_offset); |
| 6666 | kunmap(page); |
| 6667 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6668 | kfree(tmp); |
Zach Brown | 166ae5a | 2014-05-09 17:15:10 -0400 | [diff] [blame] | 6669 | return ret; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6670 | } |
| 6671 | |
Omar Sandoval | 39b07b5 | 2019-12-02 17:34:23 -0800 | [diff] [blame] | 6672 | /** |
| 6673 | * btrfs_get_extent - Lookup the first extent overlapping a range in a file. |
| 6674 | * @inode: file to search in |
| 6675 | * @page: page to read extent data into if the extent is inline |
| 6676 | * @pg_offset: offset into @page to copy to |
| 6677 | * @start: file offset |
| 6678 | * @len: length of range starting at @start |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 6679 | * |
Omar Sandoval | 39b07b5 | 2019-12-02 17:34:23 -0800 | [diff] [blame] | 6680 | * This returns the first &struct extent_map which overlaps with the given |
| 6681 | * range, reading it from the B-tree and caching it if necessary. Note that |
| 6682 | * there may be more extents which overlap the given range after the returned |
| 6683 | * extent_map. |
| 6684 | * |
| 6685 | * If @page is not NULL and the extent is inline, this also reads the extent |
| 6686 | * data directly into the page and marks the extent up to date in the io_tree. |
| 6687 | * |
| 6688 | * Return: ERR_PTR on error, non-NULL extent_map on success. |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 6689 | */ |
Nikolay Borisov | fc4f21b1 | 2017-02-20 13:51:06 +0200 | [diff] [blame] | 6690 | struct extent_map *btrfs_get_extent(struct btrfs_inode *inode, |
Omar Sandoval | 39b07b5 | 2019-12-02 17:34:23 -0800 | [diff] [blame] | 6691 | struct page *page, size_t pg_offset, |
| 6692 | u64 start, u64 len) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6693 | { |
David Sterba | 3ffbd68 | 2018-06-29 10:56:42 +0200 | [diff] [blame] | 6694 | struct btrfs_fs_info *fs_info = inode->root->fs_info; |
Nikolay Borisov | 1028d1c | 2020-08-03 12:58:46 +0300 | [diff] [blame] | 6695 | int ret = 0; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6696 | u64 extent_start = 0; |
| 6697 | u64 extent_end = 0; |
Nikolay Borisov | fc4f21b1 | 2017-02-20 13:51:06 +0200 | [diff] [blame] | 6698 | u64 objectid = btrfs_ino(inode); |
Linus Torvalds | 7e74e23 | 2019-05-01 12:19:20 -0700 | [diff] [blame] | 6699 | int extent_type = -1; |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 6700 | struct btrfs_path *path = NULL; |
Nikolay Borisov | fc4f21b1 | 2017-02-20 13:51:06 +0200 | [diff] [blame] | 6701 | struct btrfs_root *root = inode->root; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6702 | struct btrfs_file_extent_item *item; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6703 | struct extent_buffer *leaf; |
| 6704 | struct btrfs_key found_key; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6705 | struct extent_map *em = NULL; |
Nikolay Borisov | fc4f21b1 | 2017-02-20 13:51:06 +0200 | [diff] [blame] | 6706 | struct extent_map_tree *em_tree = &inode->extent_tree; |
| 6707 | struct extent_io_tree *io_tree = &inode->io_tree; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6708 | |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 6709 | read_lock(&em_tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6710 | em = lookup_extent_mapping(em_tree, start, len); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 6711 | read_unlock(&em_tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6712 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6713 | if (em) { |
Chris Mason | e1c4b74 | 2008-04-22 13:26:46 -0400 | [diff] [blame] | 6714 | if (em->start > start || em->start + em->len <= start) |
| 6715 | free_extent_map(em); |
| 6716 | else if (em->block_start == EXTENT_MAP_INLINE && page) |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 6717 | free_extent_map(em); |
| 6718 | else |
| 6719 | goto out; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6720 | } |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 6721 | em = alloc_extent_map(); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6722 | if (!em) { |
Nikolay Borisov | 1028d1c | 2020-08-03 12:58:46 +0300 | [diff] [blame] | 6723 | ret = -ENOMEM; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6724 | goto out; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6725 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6726 | em->start = EXTENT_MAP_HOLE; |
Chris Mason | 445a694 | 2008-11-10 11:53:33 -0500 | [diff] [blame] | 6727 | em->orig_start = EXTENT_MAP_HOLE; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6728 | em->len = (u64)-1; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6729 | em->block_len = (u64)-1; |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 6730 | |
Liu Bo | bee6ec8 | 2018-08-17 05:05:28 +0800 | [diff] [blame] | 6731 | path = btrfs_alloc_path(); |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 6732 | if (!path) { |
Nikolay Borisov | 1028d1c | 2020-08-03 12:58:46 +0300 | [diff] [blame] | 6733 | ret = -ENOMEM; |
Liu Bo | bee6ec8 | 2018-08-17 05:05:28 +0800 | [diff] [blame] | 6734 | goto out; |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 6735 | } |
| 6736 | |
Liu Bo | bee6ec8 | 2018-08-17 05:05:28 +0800 | [diff] [blame] | 6737 | /* Chances are we'll be called again, so go ahead and do readahead */ |
| 6738 | path->reada = READA_FORWARD; |
Josef Bacik | 4d7240f | 2020-10-23 09:58:09 -0400 | [diff] [blame] | 6739 | |
| 6740 | /* |
| 6741 | * The same explanation in load_free_space_cache applies here as well, |
| 6742 | * we only read when we're loading the free space cache, and at that |
| 6743 | * point the commit_root has everything we need. |
| 6744 | */ |
| 6745 | if (btrfs_is_free_space_inode(inode)) { |
| 6746 | path->search_commit_root = 1; |
| 6747 | path->skip_locking = 1; |
| 6748 | } |
Josef Bacik | 5189941 | 2020-08-20 11:46:01 -0400 | [diff] [blame] | 6749 | |
Nikolay Borisov | 5c9a702 | 2017-12-01 11:19:40 +0200 | [diff] [blame] | 6750 | ret = btrfs_lookup_file_extent(NULL, root, path, objectid, start, 0); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6751 | if (ret < 0) { |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6752 | goto out; |
Nikolay Borisov | b8eeab7 | 2018-12-17 11:49:00 +0200 | [diff] [blame] | 6753 | } else if (ret > 0) { |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6754 | if (path->slots[0] == 0) |
| 6755 | goto not_found; |
| 6756 | path->slots[0]--; |
Nikolay Borisov | 1028d1c | 2020-08-03 12:58:46 +0300 | [diff] [blame] | 6757 | ret = 0; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6758 | } |
| 6759 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6760 | leaf = path->nodes[0]; |
| 6761 | item = btrfs_item_ptr(leaf, path->slots[0], |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6762 | struct btrfs_file_extent_item); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6763 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6764 | if (found_key.objectid != objectid || |
Nikolay Borisov | 694c12e | 2018-12-17 10:35:59 +0200 | [diff] [blame] | 6765 | found_key.type != BTRFS_EXTENT_DATA_KEY) { |
Josef Bacik | 25a5034 | 2013-10-14 12:08:38 -0400 | [diff] [blame] | 6766 | /* |
| 6767 | * If we backup past the first extent we want to move forward |
| 6768 | * and see if there is an extent in front of us, otherwise we'll |
| 6769 | * say there is a hole for our whole search range which can |
| 6770 | * cause problems. |
| 6771 | */ |
| 6772 | extent_end = start; |
| 6773 | goto next; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6774 | } |
| 6775 | |
Nikolay Borisov | 694c12e | 2018-12-17 10:35:59 +0200 | [diff] [blame] | 6776 | extent_type = btrfs_file_extent_type(leaf, item); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6777 | extent_start = found_key.offset; |
Filipe Manana | a5eeb3d | 2020-03-09 12:41:06 +0000 | [diff] [blame] | 6778 | extent_end = btrfs_file_extent_end(path); |
Nikolay Borisov | 694c12e | 2018-12-17 10:35:59 +0200 | [diff] [blame] | 6779 | if (extent_type == BTRFS_FILE_EXTENT_REG || |
| 6780 | extent_type == BTRFS_FILE_EXTENT_PREALLOC) { |
Qu Wenruo | 6bf9e4b | 2019-03-13 13:55:11 +0800 | [diff] [blame] | 6781 | /* Only regular file could have regular/prealloc extent */ |
| 6782 | if (!S_ISREG(inode->vfs_inode.i_mode)) { |
Nikolay Borisov | 1028d1c | 2020-08-03 12:58:46 +0300 | [diff] [blame] | 6783 | ret = -EUCLEAN; |
Qu Wenruo | 6bf9e4b | 2019-03-13 13:55:11 +0800 | [diff] [blame] | 6784 | btrfs_crit(fs_info, |
| 6785 | "regular/prealloc extent found for non-regular inode %llu", |
| 6786 | btrfs_ino(inode)); |
| 6787 | goto out; |
| 6788 | } |
Liu Bo | 09ed2f1 | 2017-03-10 11:09:48 -0800 | [diff] [blame] | 6789 | trace_btrfs_get_extent_show_fi_regular(inode, leaf, item, |
| 6790 | extent_start); |
Nikolay Borisov | 694c12e | 2018-12-17 10:35:59 +0200 | [diff] [blame] | 6791 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
Liu Bo | 09ed2f1 | 2017-03-10 11:09:48 -0800 | [diff] [blame] | 6792 | trace_btrfs_get_extent_show_fi_inline(inode, leaf, item, |
| 6793 | path->slots[0], |
| 6794 | extent_start); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6795 | } |
Josef Bacik | 25a5034 | 2013-10-14 12:08:38 -0400 | [diff] [blame] | 6796 | next: |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6797 | if (start >= extent_end) { |
| 6798 | path->slots[0]++; |
| 6799 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { |
| 6800 | ret = btrfs_next_leaf(root, path); |
Nikolay Borisov | 1028d1c | 2020-08-03 12:58:46 +0300 | [diff] [blame] | 6801 | if (ret < 0) |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6802 | goto out; |
Nikolay Borisov | 1028d1c | 2020-08-03 12:58:46 +0300 | [diff] [blame] | 6803 | else if (ret > 0) |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6804 | goto not_found; |
Nikolay Borisov | 1028d1c | 2020-08-03 12:58:46 +0300 | [diff] [blame] | 6805 | |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6806 | leaf = path->nodes[0]; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6807 | } |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6808 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 6809 | if (found_key.objectid != objectid || |
| 6810 | found_key.type != BTRFS_EXTENT_DATA_KEY) |
| 6811 | goto not_found; |
| 6812 | if (start + len <= found_key.offset) |
| 6813 | goto not_found; |
Wang Shilong | e2eca69 | 2014-07-17 11:44:14 +0800 | [diff] [blame] | 6814 | if (start > found_key.offset) |
| 6815 | goto next; |
Nikolay Borisov | 02a033d | 2018-12-17 10:36:02 +0200 | [diff] [blame] | 6816 | |
| 6817 | /* New extent overlaps with existing one */ |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6818 | em->start = start; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 6819 | em->orig_start = start; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6820 | em->len = found_key.offset - start; |
Nikolay Borisov | 02a033d | 2018-12-17 10:36:02 +0200 | [diff] [blame] | 6821 | em->block_start = EXTENT_MAP_HOLE; |
| 6822 | goto insert; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6823 | } |
| 6824 | |
Omar Sandoval | 39b07b5 | 2019-12-02 17:34:23 -0800 | [diff] [blame] | 6825 | btrfs_extent_item_to_extent_map(inode, path, item, !page, em); |
Filipe Manana | 7ffbb59 | 2014-06-09 03:48:05 +0100 | [diff] [blame] | 6826 | |
Nikolay Borisov | 694c12e | 2018-12-17 10:35:59 +0200 | [diff] [blame] | 6827 | if (extent_type == BTRFS_FILE_EXTENT_REG || |
| 6828 | extent_type == BTRFS_FILE_EXTENT_PREALLOC) { |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6829 | goto insert; |
Nikolay Borisov | 694c12e | 2018-12-17 10:35:59 +0200 | [diff] [blame] | 6830 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6831 | unsigned long ptr; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6832 | char *map; |
Chris Mason | 3326d1b | 2007-10-15 16:18:25 -0400 | [diff] [blame] | 6833 | size_t size; |
| 6834 | size_t extent_offset; |
| 6835 | size_t copy_size; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6836 | |
Omar Sandoval | 39b07b5 | 2019-12-02 17:34:23 -0800 | [diff] [blame] | 6837 | if (!page) |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 6838 | goto out; |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 6839 | |
Qu Wenruo | e41ca58 | 2018-06-06 15:41:49 +0800 | [diff] [blame] | 6840 | size = btrfs_file_extent_ram_bytes(leaf, item); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6841 | extent_offset = page_offset(page) + pg_offset - extent_start; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 6842 | copy_size = min_t(u64, PAGE_SIZE - pg_offset, |
| 6843 | size - extent_offset); |
Chris Mason | 3326d1b | 2007-10-15 16:18:25 -0400 | [diff] [blame] | 6844 | em->start = extent_start + extent_offset; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6845 | em->len = ALIGN(copy_size, fs_info->sectorsize); |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 6846 | em->orig_block_len = em->len; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 6847 | em->orig_start = em->start; |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 6848 | ptr = btrfs_file_extent_inline_start(item) + extent_offset; |
Liu Bo | e49aabd | 2018-08-25 13:47:09 +0800 | [diff] [blame] | 6849 | |
Edmund Nadolski | bf46f52 | 2017-11-20 13:24:49 -0700 | [diff] [blame] | 6850 | if (!PageUptodate(page)) { |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 6851 | if (btrfs_file_extent_compression(leaf, item) != |
| 6852 | BTRFS_COMPRESS_NONE) { |
Byongho Lee | e40da0e | 2015-05-19 23:46:45 +0900 | [diff] [blame] | 6853 | ret = uncompress_inline(path, page, pg_offset, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6854 | extent_offset, item); |
Nikolay Borisov | 1028d1c | 2020-08-03 12:58:46 +0300 | [diff] [blame] | 6855 | if (ret) |
Zach Brown | 166ae5a | 2014-05-09 17:15:10 -0400 | [diff] [blame] | 6856 | goto out; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6857 | } else { |
| 6858 | map = kmap(page); |
| 6859 | read_extent_buffer(leaf, map + pg_offset, ptr, |
| 6860 | copy_size); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 6861 | if (pg_offset + copy_size < PAGE_SIZE) { |
Chris Mason | 93c82d5 | 2009-09-11 12:36:29 -0400 | [diff] [blame] | 6862 | memset(map + pg_offset + copy_size, 0, |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 6863 | PAGE_SIZE - pg_offset - |
Chris Mason | 93c82d5 | 2009-09-11 12:36:29 -0400 | [diff] [blame] | 6864 | copy_size); |
| 6865 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6866 | kunmap(page); |
| 6867 | } |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 6868 | flush_dcache_page(page); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6869 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6870 | set_extent_uptodate(io_tree, em->start, |
Arne Jansen | 507903b | 2011-04-06 10:02:20 +0000 | [diff] [blame] | 6871 | extent_map_end(em) - 1, NULL, GFP_NOFS); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6872 | goto insert; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6873 | } |
| 6874 | not_found: |
| 6875 | em->start = start; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 6876 | em->orig_start = start; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6877 | em->len = len; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6878 | em->block_start = EXTENT_MAP_HOLE; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6879 | insert: |
Nikolay Borisov | 1028d1c | 2020-08-03 12:58:46 +0300 | [diff] [blame] | 6880 | ret = 0; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 6881 | btrfs_release_path(path); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6882 | if (em->start > start || extent_map_end(em) <= start) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6883 | btrfs_err(fs_info, |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 6884 | "bad extent! em: [%llu %llu] passed [%llu %llu]", |
| 6885 | em->start, em->len, start, len); |
Nikolay Borisov | 1028d1c | 2020-08-03 12:58:46 +0300 | [diff] [blame] | 6886 | ret = -EIO; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6887 | goto out; |
| 6888 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6889 | |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 6890 | write_lock(&em_tree->lock); |
Nikolay Borisov | 1028d1c | 2020-08-03 12:58:46 +0300 | [diff] [blame] | 6891 | ret = btrfs_add_extent_mapping(fs_info, em_tree, &em, start, len); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 6892 | write_unlock(&em_tree->lock); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6893 | out: |
Liu Bo | c641428 | 2018-08-23 07:36:17 +0800 | [diff] [blame] | 6894 | btrfs_free_path(path); |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 6895 | |
Nikolay Borisov | fc4f21b1 | 2017-02-20 13:51:06 +0200 | [diff] [blame] | 6896 | trace_btrfs_get_extent(root, inode, em); |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 6897 | |
Nikolay Borisov | 1028d1c | 2020-08-03 12:58:46 +0300 | [diff] [blame] | 6898 | if (ret) { |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6899 | free_extent_map(em); |
Nikolay Borisov | 1028d1c | 2020-08-03 12:58:46 +0300 | [diff] [blame] | 6900 | return ERR_PTR(ret); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6901 | } |
| 6902 | return em; |
| 6903 | } |
| 6904 | |
Nikolay Borisov | fc4f21b1 | 2017-02-20 13:51:06 +0200 | [diff] [blame] | 6905 | struct extent_map *btrfs_get_extent_fiemap(struct btrfs_inode *inode, |
Nikolay Borisov | 4ab47a8 | 2018-12-12 09:42:32 +0200 | [diff] [blame] | 6906 | u64 start, u64 len) |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6907 | { |
| 6908 | struct extent_map *em; |
| 6909 | struct extent_map *hole_em = NULL; |
Nikolay Borisov | f3714ef | 2018-12-12 09:42:33 +0200 | [diff] [blame] | 6910 | u64 delalloc_start = start; |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6911 | u64 end; |
Nikolay Borisov | f3714ef | 2018-12-12 09:42:33 +0200 | [diff] [blame] | 6912 | u64 delalloc_len; |
| 6913 | u64 delalloc_end; |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6914 | int err = 0; |
| 6915 | |
Omar Sandoval | 39b07b5 | 2019-12-02 17:34:23 -0800 | [diff] [blame] | 6916 | em = btrfs_get_extent(inode, NULL, 0, start, len); |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6917 | if (IS_ERR(em)) |
| 6918 | return em; |
Dan Carpenter | 9986277 | 2017-04-11 11:57:15 +0300 | [diff] [blame] | 6919 | /* |
| 6920 | * If our em maps to: |
| 6921 | * - a hole or |
| 6922 | * - a pre-alloc extent, |
| 6923 | * there might actually be delalloc bytes behind it. |
| 6924 | */ |
| 6925 | if (em->block_start != EXTENT_MAP_HOLE && |
| 6926 | !test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) |
| 6927 | return em; |
| 6928 | else |
| 6929 | hole_em = em; |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6930 | |
| 6931 | /* check to see if we've wrapped (len == -1 or similar) */ |
| 6932 | end = start + len; |
| 6933 | if (end < start) |
| 6934 | end = (u64)-1; |
| 6935 | else |
| 6936 | end -= 1; |
| 6937 | |
| 6938 | em = NULL; |
| 6939 | |
| 6940 | /* ok, we didn't find anything, lets look for delalloc */ |
Nikolay Borisov | f3714ef | 2018-12-12 09:42:33 +0200 | [diff] [blame] | 6941 | delalloc_len = count_range_bits(&inode->io_tree, &delalloc_start, |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6942 | end, len, EXTENT_DELALLOC, 1); |
Nikolay Borisov | f3714ef | 2018-12-12 09:42:33 +0200 | [diff] [blame] | 6943 | delalloc_end = delalloc_start + delalloc_len; |
| 6944 | if (delalloc_end < delalloc_start) |
| 6945 | delalloc_end = (u64)-1; |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6946 | |
| 6947 | /* |
Nikolay Borisov | f3714ef | 2018-12-12 09:42:33 +0200 | [diff] [blame] | 6948 | * We didn't find anything useful, return the original results from |
| 6949 | * get_extent() |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6950 | */ |
Nikolay Borisov | f3714ef | 2018-12-12 09:42:33 +0200 | [diff] [blame] | 6951 | if (delalloc_start > end || delalloc_end <= start) { |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6952 | em = hole_em; |
| 6953 | hole_em = NULL; |
| 6954 | goto out; |
| 6955 | } |
| 6956 | |
Nikolay Borisov | f3714ef | 2018-12-12 09:42:33 +0200 | [diff] [blame] | 6957 | /* |
| 6958 | * Adjust the delalloc_start to make sure it doesn't go backwards from |
| 6959 | * the start they passed in |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6960 | */ |
Nikolay Borisov | f3714ef | 2018-12-12 09:42:33 +0200 | [diff] [blame] | 6961 | delalloc_start = max(start, delalloc_start); |
| 6962 | delalloc_len = delalloc_end - delalloc_start; |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6963 | |
Nikolay Borisov | f3714ef | 2018-12-12 09:42:33 +0200 | [diff] [blame] | 6964 | if (delalloc_len > 0) { |
| 6965 | u64 hole_start; |
Nikolay Borisov | 02950af | 2018-12-12 09:42:34 +0200 | [diff] [blame] | 6966 | u64 hole_len; |
Nikolay Borisov | f3714ef | 2018-12-12 09:42:33 +0200 | [diff] [blame] | 6967 | const u64 hole_end = extent_map_end(hole_em); |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6968 | |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 6969 | em = alloc_extent_map(); |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6970 | if (!em) { |
| 6971 | err = -ENOMEM; |
| 6972 | goto out; |
| 6973 | } |
Nikolay Borisov | f3714ef | 2018-12-12 09:42:33 +0200 | [diff] [blame] | 6974 | |
| 6975 | ASSERT(hole_em); |
| 6976 | /* |
| 6977 | * When btrfs_get_extent can't find anything it returns one |
| 6978 | * huge hole |
| 6979 | * |
| 6980 | * Make sure what it found really fits our range, and adjust to |
| 6981 | * make sure it is based on the start from the caller |
| 6982 | */ |
| 6983 | if (hole_end <= start || hole_em->start > end) { |
| 6984 | free_extent_map(hole_em); |
| 6985 | hole_em = NULL; |
| 6986 | } else { |
| 6987 | hole_start = max(hole_em->start, start); |
| 6988 | hole_len = hole_end - hole_start; |
| 6989 | } |
| 6990 | |
| 6991 | if (hole_em && delalloc_start > hole_start) { |
| 6992 | /* |
| 6993 | * Our hole starts before our delalloc, so we have to |
| 6994 | * return just the parts of the hole that go until the |
| 6995 | * delalloc starts |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6996 | */ |
Nikolay Borisov | f3714ef | 2018-12-12 09:42:33 +0200 | [diff] [blame] | 6997 | em->len = min(hole_len, delalloc_start - hole_start); |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6998 | em->start = hole_start; |
| 6999 | em->orig_start = hole_start; |
| 7000 | /* |
Nikolay Borisov | f3714ef | 2018-12-12 09:42:33 +0200 | [diff] [blame] | 7001 | * Don't adjust block start at all, it is fixed at |
| 7002 | * EXTENT_MAP_HOLE |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 7003 | */ |
| 7004 | em->block_start = hole_em->block_start; |
| 7005 | em->block_len = hole_len; |
Liu Bo | f9e4fb5 | 2013-01-07 10:10:12 +0000 | [diff] [blame] | 7006 | if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags)) |
| 7007 | set_bit(EXTENT_FLAG_PREALLOC, &em->flags); |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 7008 | } else { |
Nikolay Borisov | f3714ef | 2018-12-12 09:42:33 +0200 | [diff] [blame] | 7009 | /* |
| 7010 | * Hole is out of passed range or it starts after |
| 7011 | * delalloc range |
| 7012 | */ |
| 7013 | em->start = delalloc_start; |
| 7014 | em->len = delalloc_len; |
| 7015 | em->orig_start = delalloc_start; |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 7016 | em->block_start = EXTENT_MAP_DELALLOC; |
Nikolay Borisov | f3714ef | 2018-12-12 09:42:33 +0200 | [diff] [blame] | 7017 | em->block_len = delalloc_len; |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 7018 | } |
Nikolay Borisov | bf8d32b | 2017-12-01 11:19:43 +0200 | [diff] [blame] | 7019 | } else { |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 7020 | return hole_em; |
| 7021 | } |
| 7022 | out: |
| 7023 | |
| 7024 | free_extent_map(hole_em); |
| 7025 | if (err) { |
| 7026 | free_extent_map(em); |
| 7027 | return ERR_PTR(err); |
| 7028 | } |
| 7029 | return em; |
| 7030 | } |
| 7031 | |
Nikolay Borisov | 64f5418 | 2020-06-03 08:55:31 +0300 | [diff] [blame] | 7032 | static struct extent_map *btrfs_create_dio_extent(struct btrfs_inode *inode, |
Filipe Manana | 5f9a8a5 | 2016-05-12 13:53:36 +0100 | [diff] [blame] | 7033 | const u64 start, |
| 7034 | const u64 len, |
| 7035 | const u64 orig_start, |
| 7036 | const u64 block_start, |
| 7037 | const u64 block_len, |
| 7038 | const u64 orig_block_len, |
| 7039 | const u64 ram_bytes, |
| 7040 | const int type) |
| 7041 | { |
| 7042 | struct extent_map *em = NULL; |
| 7043 | int ret; |
| 7044 | |
Filipe Manana | 5f9a8a5 | 2016-05-12 13:53:36 +0100 | [diff] [blame] | 7045 | if (type != BTRFS_ORDERED_NOCOW) { |
Nikolay Borisov | 64f5418 | 2020-06-03 08:55:31 +0300 | [diff] [blame] | 7046 | em = create_io_em(inode, start, len, orig_start, block_start, |
| 7047 | block_len, orig_block_len, ram_bytes, |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 7048 | BTRFS_COMPRESS_NONE, /* compress_type */ |
| 7049 | type); |
Filipe Manana | 5f9a8a5 | 2016-05-12 13:53:36 +0100 | [diff] [blame] | 7050 | if (IS_ERR(em)) |
| 7051 | goto out; |
| 7052 | } |
Nikolay Borisov | 64f5418 | 2020-06-03 08:55:31 +0300 | [diff] [blame] | 7053 | ret = btrfs_add_ordered_extent_dio(inode, start, block_start, len, |
| 7054 | block_len, type); |
Filipe Manana | 5f9a8a5 | 2016-05-12 13:53:36 +0100 | [diff] [blame] | 7055 | if (ret) { |
| 7056 | if (em) { |
| 7057 | free_extent_map(em); |
Nikolay Borisov | 64f5418 | 2020-06-03 08:55:31 +0300 | [diff] [blame] | 7058 | btrfs_drop_extent_cache(inode, start, start + len - 1, 0); |
Filipe Manana | 5f9a8a5 | 2016-05-12 13:53:36 +0100 | [diff] [blame] | 7059 | } |
| 7060 | em = ERR_PTR(ret); |
| 7061 | } |
| 7062 | out: |
Filipe Manana | 5f9a8a5 | 2016-05-12 13:53:36 +0100 | [diff] [blame] | 7063 | |
| 7064 | return em; |
| 7065 | } |
| 7066 | |
Nikolay Borisov | 9fc6f91 | 2020-06-03 08:55:32 +0300 | [diff] [blame] | 7067 | static struct extent_map *btrfs_new_extent_direct(struct btrfs_inode *inode, |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7068 | u64 start, u64 len) |
| 7069 | { |
Nikolay Borisov | 9fc6f91 | 2020-06-03 08:55:32 +0300 | [diff] [blame] | 7070 | struct btrfs_root *root = inode->root; |
| 7071 | struct btrfs_fs_info *fs_info = root->fs_info; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 7072 | struct extent_map *em; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7073 | struct btrfs_key ins; |
| 7074 | u64 alloc_hint; |
| 7075 | int ret; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7076 | |
Nikolay Borisov | 9fc6f91 | 2020-06-03 08:55:32 +0300 | [diff] [blame] | 7077 | alloc_hint = get_extent_allocation_hint(inode, start, len); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7078 | ret = btrfs_reserve_extent(root, len, len, fs_info->sectorsize, |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 7079 | 0, alloc_hint, &ins, 1, 1); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 7080 | if (ret) |
| 7081 | return ERR_PTR(ret); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7082 | |
Nikolay Borisov | 9fc6f91 | 2020-06-03 08:55:32 +0300 | [diff] [blame] | 7083 | em = btrfs_create_dio_extent(inode, start, ins.offset, start, |
Filipe Manana | 5f9a8a5 | 2016-05-12 13:53:36 +0100 | [diff] [blame] | 7084 | ins.objectid, ins.offset, ins.offset, |
Liu Bo | 6288d6e | 2017-02-21 12:12:58 -0800 | [diff] [blame] | 7085 | ins.offset, BTRFS_ORDERED_REGULAR); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7086 | btrfs_dec_block_group_reservations(fs_info, ins.objectid); |
Filipe Manana | 5f9a8a5 | 2016-05-12 13:53:36 +0100 | [diff] [blame] | 7087 | if (IS_ERR(em)) |
Nikolay Borisov | 9fc6f91 | 2020-06-03 08:55:32 +0300 | [diff] [blame] | 7088 | btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, |
| 7089 | 1); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 7090 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7091 | return em; |
| 7092 | } |
| 7093 | |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7094 | /* |
Qu Wenruo | e4ecaf9 | 2020-06-24 07:23:51 +0800 | [diff] [blame] | 7095 | * Check if we can do nocow write into the range [@offset, @offset + @len) |
| 7096 | * |
| 7097 | * @offset: File offset |
| 7098 | * @len: The length to write, will be updated to the nocow writeable |
| 7099 | * range |
| 7100 | * @orig_start: (optional) Return the original file offset of the file extent |
| 7101 | * @orig_len: (optional) Return the original on-disk length of the file extent |
| 7102 | * @ram_bytes: (optional) Return the ram_bytes of the file extent |
Boris Burkov | a84d5d4 | 2020-08-18 11:00:05 -0700 | [diff] [blame] | 7103 | * @strict: if true, omit optimizations that might force us into unnecessary |
| 7104 | * cow. e.g., don't trust generation number. |
Qu Wenruo | e4ecaf9 | 2020-06-24 07:23:51 +0800 | [diff] [blame] | 7105 | * |
| 7106 | * This function will flush ordered extents in the range to ensure proper |
| 7107 | * nocow checks for (nowait == false) case. |
| 7108 | * |
| 7109 | * Return: |
| 7110 | * >0 and update @len if we can do nocow write |
| 7111 | * 0 if we can't do nocow write |
| 7112 | * <0 if error happened |
| 7113 | * |
| 7114 | * NOTE: This only checks the file extents, caller is responsible to wait for |
| 7115 | * any ordered extents. |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7116 | */ |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 7117 | noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len, |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 7118 | u64 *orig_start, u64 *orig_block_len, |
Boris Burkov | a84d5d4 | 2020-08-18 11:00:05 -0700 | [diff] [blame] | 7119 | u64 *ram_bytes, bool strict) |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7120 | { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 7121 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7122 | struct btrfs_path *path; |
| 7123 | int ret; |
| 7124 | struct extent_buffer *leaf; |
| 7125 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Miao Xie | 7b2b708 | 2014-02-27 13:58:05 +0800 | [diff] [blame] | 7126 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7127 | struct btrfs_file_extent_item *fi; |
| 7128 | struct btrfs_key key; |
| 7129 | u64 disk_bytenr; |
| 7130 | u64 backref_offset; |
| 7131 | u64 extent_end; |
| 7132 | u64 num_bytes; |
| 7133 | int slot; |
| 7134 | int found_type; |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 7135 | bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW); |
Miao Xie | e77751a | 2013-12-27 21:11:50 +0800 | [diff] [blame] | 7136 | |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7137 | path = btrfs_alloc_path(); |
| 7138 | if (!path) |
| 7139 | return -ENOMEM; |
| 7140 | |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 7141 | ret = btrfs_lookup_file_extent(NULL, root, path, |
| 7142 | btrfs_ino(BTRFS_I(inode)), offset, 0); |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7143 | if (ret < 0) |
| 7144 | goto out; |
| 7145 | |
| 7146 | slot = path->slots[0]; |
| 7147 | if (ret == 1) { |
| 7148 | if (slot == 0) { |
| 7149 | /* can't find the item, must cow */ |
| 7150 | ret = 0; |
| 7151 | goto out; |
| 7152 | } |
| 7153 | slot--; |
| 7154 | } |
| 7155 | ret = 0; |
| 7156 | leaf = path->nodes[0]; |
| 7157 | btrfs_item_key_to_cpu(leaf, &key, slot); |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 7158 | if (key.objectid != btrfs_ino(BTRFS_I(inode)) || |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7159 | key.type != BTRFS_EXTENT_DATA_KEY) { |
| 7160 | /* not our file or wrong item type, must cow */ |
| 7161 | goto out; |
| 7162 | } |
| 7163 | |
| 7164 | if (key.offset > offset) { |
| 7165 | /* Wrong offset, must cow */ |
| 7166 | goto out; |
| 7167 | } |
| 7168 | |
| 7169 | fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item); |
| 7170 | found_type = btrfs_file_extent_type(leaf, fi); |
| 7171 | if (found_type != BTRFS_FILE_EXTENT_REG && |
| 7172 | found_type != BTRFS_FILE_EXTENT_PREALLOC) { |
| 7173 | /* not a regular extent, must cow */ |
| 7174 | goto out; |
| 7175 | } |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 7176 | |
| 7177 | if (!nocow && found_type == BTRFS_FILE_EXTENT_REG) |
| 7178 | goto out; |
| 7179 | |
Miao Xie | e77751a | 2013-12-27 21:11:50 +0800 | [diff] [blame] | 7180 | extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi); |
| 7181 | if (extent_end <= offset) |
| 7182 | goto out; |
| 7183 | |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7184 | disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 7185 | if (disk_bytenr == 0) |
| 7186 | goto out; |
| 7187 | |
| 7188 | if (btrfs_file_extent_compression(leaf, fi) || |
| 7189 | btrfs_file_extent_encryption(leaf, fi) || |
| 7190 | btrfs_file_extent_other_encoding(leaf, fi)) |
| 7191 | goto out; |
| 7192 | |
Ethan Lien | 78d4295 | 2018-05-17 14:58:29 +0800 | [diff] [blame] | 7193 | /* |
| 7194 | * Do the same check as in btrfs_cross_ref_exist but without the |
| 7195 | * unnecessary search. |
| 7196 | */ |
Boris Burkov | a84d5d4 | 2020-08-18 11:00:05 -0700 | [diff] [blame] | 7197 | if (!strict && |
| 7198 | (btrfs_file_extent_generation(leaf, fi) <= |
| 7199 | btrfs_root_last_snapshot(&root->root_item))) |
Ethan Lien | 78d4295 | 2018-05-17 14:58:29 +0800 | [diff] [blame] | 7200 | goto out; |
| 7201 | |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7202 | backref_offset = btrfs_file_extent_offset(leaf, fi); |
| 7203 | |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 7204 | if (orig_start) { |
| 7205 | *orig_start = key.offset - backref_offset; |
| 7206 | *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi); |
| 7207 | *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi); |
| 7208 | } |
Josef Bacik | eb384b5 | 2013-04-24 16:32:55 -0400 | [diff] [blame] | 7209 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 7210 | if (btrfs_extent_readonly(fs_info, disk_bytenr)) |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7211 | goto out; |
Miao Xie | 7b2b708 | 2014-02-27 13:58:05 +0800 | [diff] [blame] | 7212 | |
| 7213 | num_bytes = min(offset + *len, extent_end) - offset; |
| 7214 | if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) { |
| 7215 | u64 range_end; |
| 7216 | |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 7217 | range_end = round_up(offset + num_bytes, |
| 7218 | root->fs_info->sectorsize) - 1; |
Miao Xie | 7b2b708 | 2014-02-27 13:58:05 +0800 | [diff] [blame] | 7219 | ret = test_range_bit(io_tree, offset, range_end, |
| 7220 | EXTENT_DELALLOC, 0, NULL); |
| 7221 | if (ret) { |
| 7222 | ret = -EAGAIN; |
| 7223 | goto out; |
| 7224 | } |
| 7225 | } |
| 7226 | |
Josef Bacik | 1bda19e | 2013-10-18 12:10:36 -0400 | [diff] [blame] | 7227 | btrfs_release_path(path); |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7228 | |
| 7229 | /* |
| 7230 | * look for other files referencing this extent, if we |
| 7231 | * find any we must cow |
| 7232 | */ |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 7233 | |
Liu Bo | e4c3b2d | 2017-01-30 12:25:28 -0800 | [diff] [blame] | 7234 | ret = btrfs_cross_ref_exist(root, btrfs_ino(BTRFS_I(inode)), |
Boris Burkov | a84d5d4 | 2020-08-18 11:00:05 -0700 | [diff] [blame] | 7235 | key.offset - backref_offset, disk_bytenr, |
| 7236 | strict); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 7237 | if (ret) { |
| 7238 | ret = 0; |
| 7239 | goto out; |
| 7240 | } |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7241 | |
| 7242 | /* |
| 7243 | * adjust disk_bytenr and num_bytes to cover just the bytes |
| 7244 | * in this extent we are about to write. If there |
| 7245 | * are any csums in that range we have to cow in order |
| 7246 | * to keep the csums correct |
| 7247 | */ |
| 7248 | disk_bytenr += backref_offset; |
| 7249 | disk_bytenr += offset - key.offset; |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 7250 | if (csum_exist_in_range(fs_info, disk_bytenr, num_bytes)) |
| 7251 | goto out; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7252 | /* |
| 7253 | * all of the above have passed, it is safe to overwrite this extent |
| 7254 | * without cow |
| 7255 | */ |
Josef Bacik | eb384b5 | 2013-04-24 16:32:55 -0400 | [diff] [blame] | 7256 | *len = num_bytes; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7257 | ret = 1; |
| 7258 | out: |
| 7259 | btrfs_free_path(path); |
| 7260 | return ret; |
| 7261 | } |
| 7262 | |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7263 | static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend, |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 7264 | struct extent_state **cached_state, bool writing) |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7265 | { |
| 7266 | struct btrfs_ordered_extent *ordered; |
| 7267 | int ret = 0; |
| 7268 | |
| 7269 | while (1) { |
| 7270 | lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend, |
David Sterba | ff13db4 | 2015-12-03 14:30:40 +0100 | [diff] [blame] | 7271 | cached_state); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7272 | /* |
| 7273 | * 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] | 7274 | * 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] | 7275 | * extents in this range. |
| 7276 | */ |
Nikolay Borisov | a776c6f | 2017-02-20 13:50:49 +0200 | [diff] [blame] | 7277 | ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), lockstart, |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7278 | lockend - lockstart + 1); |
| 7279 | |
| 7280 | /* |
| 7281 | * We need to make sure there are no buffered pages in this |
| 7282 | * range either, we could have raced between the invalidate in |
| 7283 | * generic_file_direct_write and locking the extent. The |
| 7284 | * invalidate needs to happen so that reads after a write do not |
| 7285 | * get stale data. |
| 7286 | */ |
Alex Gartrell | fc4adbff | 2014-05-20 13:07:56 -0700 | [diff] [blame] | 7287 | if (!ordered && |
David Sterba | 051c98e | 2018-03-07 15:33:22 +0100 | [diff] [blame] | 7288 | (!writing || !filemap_range_has_page(inode->i_mapping, |
| 7289 | lockstart, lockend))) |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7290 | break; |
| 7291 | |
| 7292 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend, |
David Sterba | e43bbe5 | 2017-12-12 21:43:52 +0100 | [diff] [blame] | 7293 | cached_state); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7294 | |
| 7295 | if (ordered) { |
Filipe Manana | ade7702 | 2016-02-18 14:28:55 +0000 | [diff] [blame] | 7296 | /* |
| 7297 | * If we are doing a DIO read and the ordered extent we |
| 7298 | * found is for a buffered write, we can not wait for it |
| 7299 | * to complete and retry, because if we do so we can |
| 7300 | * deadlock with concurrent buffered writes on page |
| 7301 | * locks. This happens only if our DIO read covers more |
| 7302 | * than one extent map, if at this point has already |
| 7303 | * created an ordered extent for a previous extent map |
| 7304 | * and locked its range in the inode's io tree, and a |
| 7305 | * concurrent write against that previous extent map's |
| 7306 | * range and this range started (we unlock the ranges |
| 7307 | * in the io tree only when the bios complete and |
| 7308 | * buffered writes always lock pages before attempting |
| 7309 | * to lock range in the io tree). |
| 7310 | */ |
| 7311 | if (writing || |
| 7312 | test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags)) |
Nikolay Borisov | c0a4360 | 2020-09-18 12:15:53 +0300 | [diff] [blame] | 7313 | btrfs_start_ordered_extent(ordered, 1); |
Filipe Manana | ade7702 | 2016-02-18 14:28:55 +0000 | [diff] [blame] | 7314 | else |
| 7315 | ret = -ENOTBLK; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7316 | btrfs_put_ordered_extent(ordered); |
| 7317 | } else { |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7318 | /* |
Filipe Manana | b850ae1 | 2015-12-08 16:23:16 +0000 | [diff] [blame] | 7319 | * We could trigger writeback for this range (and wait |
| 7320 | * for it to complete) and then invalidate the pages for |
| 7321 | * this range (through invalidate_inode_pages2_range()), |
| 7322 | * but that can lead us to a deadlock with a concurrent |
Matthew Wilcox (Oracle) | ba206a0 | 2020-06-01 21:47:05 -0700 | [diff] [blame] | 7323 | * call to readahead (a buffered read or a defrag call |
Filipe Manana | b850ae1 | 2015-12-08 16:23:16 +0000 | [diff] [blame] | 7324 | * triggered a readahead) on a page lock due to an |
| 7325 | * ordered dio extent we created before but did not have |
| 7326 | * yet a corresponding bio submitted (whence it can not |
Matthew Wilcox (Oracle) | ba206a0 | 2020-06-01 21:47:05 -0700 | [diff] [blame] | 7327 | * complete), which makes readahead wait for that |
Filipe Manana | b850ae1 | 2015-12-08 16:23:16 +0000 | [diff] [blame] | 7328 | * ordered extent to complete while holding a lock on |
| 7329 | * that page. |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7330 | */ |
Filipe Manana | b850ae1 | 2015-12-08 16:23:16 +0000 | [diff] [blame] | 7331 | ret = -ENOTBLK; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7332 | } |
| 7333 | |
Filipe Manana | ade7702 | 2016-02-18 14:28:55 +0000 | [diff] [blame] | 7334 | if (ret) |
| 7335 | break; |
| 7336 | |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7337 | cond_resched(); |
| 7338 | } |
| 7339 | |
| 7340 | return ret; |
| 7341 | } |
| 7342 | |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 7343 | /* The callers of this must take lock_extent() */ |
Nikolay Borisov | 4b67c11 | 2020-06-03 08:55:05 +0300 | [diff] [blame] | 7344 | static struct extent_map *create_io_em(struct btrfs_inode *inode, u64 start, |
| 7345 | u64 len, u64 orig_start, u64 block_start, |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 7346 | u64 block_len, u64 orig_block_len, |
| 7347 | u64 ram_bytes, int compress_type, |
| 7348 | int type) |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7349 | { |
| 7350 | struct extent_map_tree *em_tree; |
| 7351 | struct extent_map *em; |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7352 | int ret; |
| 7353 | |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 7354 | ASSERT(type == BTRFS_ORDERED_PREALLOC || |
| 7355 | type == BTRFS_ORDERED_COMPRESSED || |
| 7356 | type == BTRFS_ORDERED_NOCOW || |
Liu Bo | 1af4a0a | 2017-02-13 15:35:09 -0800 | [diff] [blame] | 7357 | type == BTRFS_ORDERED_REGULAR); |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 7358 | |
Nikolay Borisov | 4b67c11 | 2020-06-03 08:55:05 +0300 | [diff] [blame] | 7359 | em_tree = &inode->extent_tree; |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7360 | em = alloc_extent_map(); |
| 7361 | if (!em) |
| 7362 | return ERR_PTR(-ENOMEM); |
| 7363 | |
| 7364 | em->start = start; |
| 7365 | em->orig_start = orig_start; |
| 7366 | em->len = len; |
| 7367 | em->block_len = block_len; |
| 7368 | em->block_start = block_start; |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 7369 | em->orig_block_len = orig_block_len; |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 7370 | em->ram_bytes = ram_bytes; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 7371 | em->generation = -1; |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7372 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
Liu Bo | 1af4a0a | 2017-02-13 15:35:09 -0800 | [diff] [blame] | 7373 | if (type == BTRFS_ORDERED_PREALLOC) { |
Josef Bacik | b11e234 | 2012-12-03 10:58:15 -0500 | [diff] [blame] | 7374 | set_bit(EXTENT_FLAG_FILLING, &em->flags); |
Liu Bo | 1af4a0a | 2017-02-13 15:35:09 -0800 | [diff] [blame] | 7375 | } else if (type == BTRFS_ORDERED_COMPRESSED) { |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 7376 | set_bit(EXTENT_FLAG_COMPRESSED, &em->flags); |
| 7377 | em->compress_type = compress_type; |
| 7378 | } |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7379 | |
| 7380 | do { |
Nikolay Borisov | 4b67c11 | 2020-06-03 08:55:05 +0300 | [diff] [blame] | 7381 | btrfs_drop_extent_cache(inode, em->start, |
| 7382 | em->start + em->len - 1, 0); |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7383 | write_lock(&em_tree->lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 7384 | ret = add_extent_mapping(em_tree, em, 1); |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7385 | write_unlock(&em_tree->lock); |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 7386 | /* |
| 7387 | * The caller has taken lock_extent(), who could race with us |
| 7388 | * to add em? |
| 7389 | */ |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7390 | } while (ret == -EEXIST); |
| 7391 | |
| 7392 | if (ret) { |
| 7393 | free_extent_map(em); |
| 7394 | return ERR_PTR(ret); |
| 7395 | } |
| 7396 | |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 7397 | /* 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] | 7398 | return em; |
| 7399 | } |
| 7400 | |
Nikolay Borisov | 1c8d017 | 2018-05-02 15:19:32 +0300 | [diff] [blame] | 7401 | |
Nikolay Borisov | c5794e5 | 2018-05-02 15:19:33 +0300 | [diff] [blame] | 7402 | static int btrfs_get_blocks_direct_write(struct extent_map **map, |
Nikolay Borisov | c5794e5 | 2018-05-02 15:19:33 +0300 | [diff] [blame] | 7403 | struct inode *inode, |
| 7404 | struct btrfs_dio_data *dio_data, |
| 7405 | u64 start, u64 len) |
| 7406 | { |
| 7407 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
| 7408 | struct extent_map *em = *map; |
| 7409 | int ret = 0; |
| 7410 | |
| 7411 | /* |
| 7412 | * We don't allocate a new extent in the following cases |
| 7413 | * |
| 7414 | * 1) The inode is marked as NODATACOW. In this case we'll just use the |
| 7415 | * existing extent. |
| 7416 | * 2) The extent is marked as PREALLOC. We're good to go here and can |
| 7417 | * just use the extent. |
| 7418 | * |
| 7419 | */ |
| 7420 | if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) || |
| 7421 | ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) && |
| 7422 | em->block_start != EXTENT_MAP_HOLE)) { |
| 7423 | int type; |
| 7424 | u64 block_start, orig_start, orig_block_len, ram_bytes; |
| 7425 | |
| 7426 | if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) |
| 7427 | type = BTRFS_ORDERED_PREALLOC; |
| 7428 | else |
| 7429 | type = BTRFS_ORDERED_NOCOW; |
| 7430 | len = min(len, em->len - (start - em->start)); |
| 7431 | block_start = em->block_start + (start - em->start); |
| 7432 | |
| 7433 | if (can_nocow_extent(inode, start, &len, &orig_start, |
Boris Burkov | a84d5d4 | 2020-08-18 11:00:05 -0700 | [diff] [blame] | 7434 | &orig_block_len, &ram_bytes, false) == 1 && |
Nikolay Borisov | c5794e5 | 2018-05-02 15:19:33 +0300 | [diff] [blame] | 7435 | btrfs_inc_nocow_writers(fs_info, block_start)) { |
| 7436 | struct extent_map *em2; |
| 7437 | |
Nikolay Borisov | 64f5418 | 2020-06-03 08:55:31 +0300 | [diff] [blame] | 7438 | em2 = btrfs_create_dio_extent(BTRFS_I(inode), start, len, |
Nikolay Borisov | c5794e5 | 2018-05-02 15:19:33 +0300 | [diff] [blame] | 7439 | orig_start, block_start, |
| 7440 | len, orig_block_len, |
| 7441 | ram_bytes, type); |
| 7442 | btrfs_dec_nocow_writers(fs_info, block_start); |
| 7443 | if (type == BTRFS_ORDERED_PREALLOC) { |
| 7444 | free_extent_map(em); |
| 7445 | *map = em = em2; |
| 7446 | } |
| 7447 | |
| 7448 | if (em2 && IS_ERR(em2)) { |
| 7449 | ret = PTR_ERR(em2); |
| 7450 | goto out; |
| 7451 | } |
| 7452 | /* |
| 7453 | * For inode marked NODATACOW or extent marked PREALLOC, |
| 7454 | * use the existing or preallocated extent, so does not |
| 7455 | * need to adjust btrfs_space_info's bytes_may_use. |
| 7456 | */ |
Nikolay Borisov | 9db5d51 | 2020-06-03 08:55:38 +0300 | [diff] [blame] | 7457 | btrfs_free_reserved_data_space_noquota(fs_info, len); |
Nikolay Borisov | c5794e5 | 2018-05-02 15:19:33 +0300 | [diff] [blame] | 7458 | goto skip_cow; |
| 7459 | } |
| 7460 | } |
| 7461 | |
| 7462 | /* this will cow the extent */ |
Nikolay Borisov | c5794e5 | 2018-05-02 15:19:33 +0300 | [diff] [blame] | 7463 | free_extent_map(em); |
Nikolay Borisov | 9fc6f91 | 2020-06-03 08:55:32 +0300 | [diff] [blame] | 7464 | *map = em = btrfs_new_extent_direct(BTRFS_I(inode), start, len); |
Nikolay Borisov | c5794e5 | 2018-05-02 15:19:33 +0300 | [diff] [blame] | 7465 | if (IS_ERR(em)) { |
| 7466 | ret = PTR_ERR(em); |
| 7467 | goto out; |
| 7468 | } |
| 7469 | |
| 7470 | len = min(len, em->len - (start - em->start)); |
| 7471 | |
| 7472 | skip_cow: |
Nikolay Borisov | c5794e5 | 2018-05-02 15:19:33 +0300 | [diff] [blame] | 7473 | /* |
| 7474 | * Need to update the i_size under the extent lock so buffered |
| 7475 | * readers will get the updated i_size when we unlock. |
| 7476 | */ |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 7477 | if (start + len > i_size_read(inode)) |
Nikolay Borisov | c5794e5 | 2018-05-02 15:19:33 +0300 | [diff] [blame] | 7478 | i_size_write(inode, start + len); |
| 7479 | |
Nikolay Borisov | c5794e5 | 2018-05-02 15:19:33 +0300 | [diff] [blame] | 7480 | dio_data->reserve -= len; |
Nikolay Borisov | c5794e5 | 2018-05-02 15:19:33 +0300 | [diff] [blame] | 7481 | out: |
| 7482 | return ret; |
| 7483 | } |
| 7484 | |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 7485 | static int btrfs_dio_iomap_begin(struct inode *inode, loff_t start, |
| 7486 | loff_t length, unsigned int flags, struct iomap *iomap, |
| 7487 | struct iomap *srcmap) |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7488 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7489 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7490 | struct extent_map *em; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7491 | struct extent_state *cached_state = NULL; |
chandan | 50745b0 | 2015-08-28 21:10:13 +0530 | [diff] [blame] | 7492 | struct btrfs_dio_data *dio_data = NULL; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7493 | u64 lockstart, lockend; |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 7494 | const bool write = !!(flags & IOMAP_WRITE); |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 7495 | int ret = 0; |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 7496 | u64 len = length; |
| 7497 | bool unlock_extents = false; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7498 | |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 7499 | if (!write) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7500 | len = min_t(u64, len, fs_info->sectorsize); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7501 | |
Josef Bacik | c329861 | 2012-08-03 16:49:19 -0400 | [diff] [blame] | 7502 | lockstart = start; |
| 7503 | lockend = start + len - 1; |
| 7504 | |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 7505 | /* |
| 7506 | * The generic stuff only does filemap_write_and_wait_range, which |
| 7507 | * isn't enough if we've written compressed pages to this area, so we |
| 7508 | * need to flush the dirty pages again to make absolutely sure that any |
| 7509 | * outstanding dirty pages are on disk. |
| 7510 | */ |
| 7511 | if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, |
| 7512 | &BTRFS_I(inode)->runtime_flags)) { |
| 7513 | ret = filemap_fdatawrite_range(inode->i_mapping, start, |
| 7514 | start + length - 1); |
| 7515 | if (ret) |
| 7516 | return ret; |
Josef Bacik | e1cbbfa | 2015-03-17 10:52:28 -0400 | [diff] [blame] | 7517 | } |
| 7518 | |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 7519 | dio_data = kzalloc(sizeof(*dio_data), GFP_NOFS); |
| 7520 | if (!dio_data) |
| 7521 | return -ENOMEM; |
| 7522 | |
| 7523 | dio_data->length = length; |
| 7524 | if (write) { |
| 7525 | dio_data->reserve = round_up(length, fs_info->sectorsize); |
| 7526 | ret = btrfs_delalloc_reserve_space(BTRFS_I(inode), |
| 7527 | &dio_data->data_reserved, |
| 7528 | start, dio_data->reserve); |
| 7529 | if (ret) { |
| 7530 | extent_changeset_free(dio_data->data_reserved); |
| 7531 | kfree(dio_data); |
| 7532 | return ret; |
| 7533 | } |
| 7534 | } |
| 7535 | iomap->private = dio_data; |
| 7536 | |
| 7537 | |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7538 | /* |
| 7539 | * If this errors out it's because we couldn't invalidate pagecache for |
| 7540 | * this range and we need to fallback to buffered. |
| 7541 | */ |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 7542 | if (lock_extent_direct(inode, lockstart, lockend, &cached_state, write)) { |
Filipe Manana | 9c9464c | 2015-11-04 09:52:04 +0000 | [diff] [blame] | 7543 | ret = -ENOTBLK; |
| 7544 | goto err; |
| 7545 | } |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7546 | |
Omar Sandoval | 39b07b5 | 2019-12-02 17:34:23 -0800 | [diff] [blame] | 7547 | em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7548 | if (IS_ERR(em)) { |
| 7549 | ret = PTR_ERR(em); |
| 7550 | goto unlock_err; |
| 7551 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7552 | |
| 7553 | /* |
| 7554 | * Ok for INLINE and COMPRESSED extents we need to fallback on buffered |
| 7555 | * io. INLINE is special, and we could probably kludge it in here, but |
| 7556 | * it's still buffered so for safety lets just fall back to the generic |
| 7557 | * buffered path. |
| 7558 | * |
| 7559 | * For COMPRESSED we _have_ to read the entire extent in so we can |
| 7560 | * decompress it, so there will be buffering required no matter what we |
| 7561 | * do, so go ahead and fallback to buffered. |
| 7562 | * |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 7563 | * 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] | 7564 | * to buffered IO. Don't blame me, this is the price we pay for using |
| 7565 | * the generic code. |
| 7566 | */ |
| 7567 | if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) || |
| 7568 | em->block_start == EXTENT_MAP_INLINE) { |
| 7569 | free_extent_map(em); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7570 | ret = -ENOTBLK; |
| 7571 | goto unlock_err; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7572 | } |
| 7573 | |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 7574 | len = min(len, em->len - (start - em->start)); |
| 7575 | if (write) { |
| 7576 | ret = btrfs_get_blocks_direct_write(&em, inode, dio_data, |
| 7577 | start, len); |
Nikolay Borisov | c5794e5 | 2018-05-02 15:19:33 +0300 | [diff] [blame] | 7578 | if (ret < 0) |
| 7579 | goto unlock_err; |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 7580 | unlock_extents = true; |
| 7581 | /* Recalc len in case the new em is smaller than requested */ |
| 7582 | len = min(len, em->len - (start - em->start)); |
Nikolay Borisov | c5794e5 | 2018-05-02 15:19:33 +0300 | [diff] [blame] | 7583 | } else { |
Nikolay Borisov | 1c8d017 | 2018-05-02 15:19:32 +0300 | [diff] [blame] | 7584 | /* |
| 7585 | * We need to unlock only the end area that we aren't using. |
| 7586 | * The rest is going to be unlocked by the endio routine. |
| 7587 | */ |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 7588 | lockstart = start + len; |
| 7589 | if (lockstart < lockend) |
| 7590 | unlock_extents = true; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7591 | } |
| 7592 | |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 7593 | if (unlock_extents) |
| 7594 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, |
| 7595 | lockstart, lockend, &cached_state); |
| 7596 | else |
| 7597 | free_extent_state(cached_state); |
| 7598 | |
| 7599 | /* |
| 7600 | * Translate extent map information to iomap. |
| 7601 | * We trim the extents (and move the addr) even though iomap code does |
| 7602 | * that, since we have locked only the parts we are performing I/O in. |
| 7603 | */ |
| 7604 | if ((em->block_start == EXTENT_MAP_HOLE) || |
| 7605 | (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) && !write)) { |
| 7606 | iomap->addr = IOMAP_NULL_ADDR; |
| 7607 | iomap->type = IOMAP_HOLE; |
| 7608 | } else { |
| 7609 | iomap->addr = em->block_start + (start - em->start); |
| 7610 | iomap->type = IOMAP_MAPPED; |
| 7611 | } |
| 7612 | iomap->offset = start; |
| 7613 | iomap->bdev = fs_info->fs_devices->latest_bdev; |
| 7614 | iomap->length = len; |
| 7615 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7616 | free_extent_map(em); |
| 7617 | |
| 7618 | return 0; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7619 | |
| 7620 | unlock_err: |
Omar Sandoval | e182163 | 2019-08-15 14:04:04 -0700 | [diff] [blame] | 7621 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend, |
| 7622 | &cached_state); |
Filipe Manana | 9c9464c | 2015-11-04 09:52:04 +0000 | [diff] [blame] | 7623 | err: |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 7624 | if (dio_data) { |
| 7625 | btrfs_delalloc_release_space(BTRFS_I(inode), |
| 7626 | dio_data->data_reserved, start, |
| 7627 | dio_data->reserve, true); |
| 7628 | btrfs_delalloc_release_extents(BTRFS_I(inode), dio_data->reserve); |
| 7629 | extent_changeset_free(dio_data->data_reserved); |
| 7630 | kfree(dio_data); |
| 7631 | } |
| 7632 | return ret; |
| 7633 | } |
| 7634 | |
| 7635 | static int btrfs_dio_iomap_end(struct inode *inode, loff_t pos, loff_t length, |
| 7636 | ssize_t written, unsigned int flags, struct iomap *iomap) |
| 7637 | { |
| 7638 | int ret = 0; |
| 7639 | struct btrfs_dio_data *dio_data = iomap->private; |
| 7640 | size_t submitted = dio_data->submitted; |
| 7641 | const bool write = !!(flags & IOMAP_WRITE); |
| 7642 | |
| 7643 | if (!write && (iomap->type == IOMAP_HOLE)) { |
| 7644 | /* If reading from a hole, unlock and return */ |
| 7645 | unlock_extent(&BTRFS_I(inode)->io_tree, pos, pos + length - 1); |
| 7646 | goto out; |
| 7647 | } |
| 7648 | |
| 7649 | if (submitted < length) { |
| 7650 | pos += submitted; |
| 7651 | length -= submitted; |
| 7652 | if (write) |
| 7653 | __endio_write_update_ordered(BTRFS_I(inode), pos, |
| 7654 | length, false); |
| 7655 | else |
| 7656 | unlock_extent(&BTRFS_I(inode)->io_tree, pos, |
| 7657 | pos + length - 1); |
| 7658 | ret = -ENOTBLK; |
| 7659 | } |
| 7660 | |
| 7661 | if (write) { |
| 7662 | if (dio_data->reserve) |
| 7663 | btrfs_delalloc_release_space(BTRFS_I(inode), |
| 7664 | dio_data->data_reserved, pos, |
| 7665 | dio_data->reserve, true); |
| 7666 | btrfs_delalloc_release_extents(BTRFS_I(inode), dio_data->length); |
| 7667 | extent_changeset_free(dio_data->data_reserved); |
| 7668 | } |
| 7669 | out: |
| 7670 | kfree(dio_data); |
| 7671 | iomap->private = NULL; |
| 7672 | |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7673 | return ret; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7674 | } |
| 7675 | |
Omar Sandoval | 769b4f2 | 2020-04-16 14:46:22 -0700 | [diff] [blame] | 7676 | static void btrfs_dio_private_put(struct btrfs_dio_private *dip) |
| 7677 | { |
| 7678 | /* |
| 7679 | * This implies a barrier so that stores to dio_bio->bi_status before |
| 7680 | * this and loads of dio_bio->bi_status after this are fully ordered. |
| 7681 | */ |
| 7682 | if (!refcount_dec_and_test(&dip->refs)) |
| 7683 | return; |
| 7684 | |
| 7685 | if (bio_op(dip->dio_bio) == REQ_OP_WRITE) { |
Nikolay Borisov | b672b5c | 2020-06-03 08:55:24 +0300 | [diff] [blame] | 7686 | __endio_write_update_ordered(BTRFS_I(dip->inode), |
| 7687 | dip->logical_offset, |
Omar Sandoval | 769b4f2 | 2020-04-16 14:46:22 -0700 | [diff] [blame] | 7688 | dip->bytes, |
| 7689 | !dip->dio_bio->bi_status); |
| 7690 | } else { |
| 7691 | unlock_extent(&BTRFS_I(dip->inode)->io_tree, |
| 7692 | dip->logical_offset, |
| 7693 | dip->logical_offset + dip->bytes - 1); |
| 7694 | } |
| 7695 | |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 7696 | bio_endio(dip->dio_bio); |
Omar Sandoval | 769b4f2 | 2020-04-16 14:46:22 -0700 | [diff] [blame] | 7697 | kfree(dip); |
| 7698 | } |
| 7699 | |
Omar Sandoval | 77d5d68 | 2020-04-16 14:46:25 -0700 | [diff] [blame] | 7700 | static blk_status_t submit_dio_repair_bio(struct inode *inode, struct bio *bio, |
| 7701 | int mirror_num, |
| 7702 | unsigned long bio_flags) |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7703 | { |
Omar Sandoval | 77d5d68 | 2020-04-16 14:46:25 -0700 | [diff] [blame] | 7704 | struct btrfs_dio_private *dip = bio->bi_private; |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 7705 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Omar Sandoval | 58efbc9 | 2017-08-22 23:45:59 -0700 | [diff] [blame] | 7706 | blk_status_t ret; |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7707 | |
Mike Christie | 37226b2 | 2016-06-05 14:31:52 -0500 | [diff] [blame] | 7708 | BUG_ON(bio_op(bio) == REQ_OP_WRITE); |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7709 | |
Omar Sandoval | 5c047a6 | 2020-04-16 14:46:24 -0700 | [diff] [blame] | 7710 | 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] | 7711 | if (ret) |
Nikolay Borisov | ea057f6 | 2017-12-13 10:25:38 +0200 | [diff] [blame] | 7712 | return ret; |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7713 | |
Omar Sandoval | fd9d667 | 2020-04-16 14:46:23 -0700 | [diff] [blame] | 7714 | refcount_inc(&dip->refs); |
Chris Mason | 08635ba | 2019-07-10 12:28:14 -0700 | [diff] [blame] | 7715 | ret = btrfs_map_bio(fs_info, bio, mirror_num); |
Omar Sandoval | 77d5d68 | 2020-04-16 14:46:25 -0700 | [diff] [blame] | 7716 | if (ret) |
Omar Sandoval | fd9d667 | 2020-04-16 14:46:23 -0700 | [diff] [blame] | 7717 | refcount_dec(&dip->refs); |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7718 | return ret; |
| 7719 | } |
| 7720 | |
Omar Sandoval | 769b4f2 | 2020-04-16 14:46:22 -0700 | [diff] [blame] | 7721 | static blk_status_t btrfs_check_read_dio_bio(struct inode *inode, |
| 7722 | struct btrfs_io_bio *io_bio, |
Omar Sandoval | fd9d667 | 2020-04-16 14:46:23 -0700 | [diff] [blame] | 7723 | const bool uptodate) |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7724 | { |
Omar Sandoval | fd9d667 | 2020-04-16 14:46:23 -0700 | [diff] [blame] | 7725 | struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info; |
| 7726 | const u32 sectorsize = fs_info->sectorsize; |
Josef Bacik | 7870d08 | 2017-05-05 11:57:15 -0400 | [diff] [blame] | 7727 | struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree; |
Omar Sandoval | fd9d667 | 2020-04-16 14:46:23 -0700 | [diff] [blame] | 7728 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
| 7729 | const bool csum = !(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM); |
Liu Bo | 17347ce | 2017-05-15 15:33:27 -0700 | [diff] [blame] | 7730 | struct bio_vec bvec; |
| 7731 | struct bvec_iter iter; |
Omar Sandoval | fd9d667 | 2020-04-16 14:46:23 -0700 | [diff] [blame] | 7732 | u64 start = io_bio->logical; |
| 7733 | int icsum = 0; |
Omar Sandoval | 58efbc9 | 2017-08-22 23:45:59 -0700 | [diff] [blame] | 7734 | blk_status_t err = BLK_STS_OK; |
Miao Xie | dc380ae | 2014-09-12 18:43:55 +0800 | [diff] [blame] | 7735 | |
Omar Sandoval | fd9d667 | 2020-04-16 14:46:23 -0700 | [diff] [blame] | 7736 | __bio_for_each_segment(bvec, &io_bio->bio, iter, io_bio->iter) { |
| 7737 | unsigned int i, nr_sectors, pgoff; |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 7738 | |
Liu Bo | 17347ce | 2017-05-15 15:33:27 -0700 | [diff] [blame] | 7739 | nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec.bv_len); |
| 7740 | pgoff = bvec.bv_offset; |
Omar Sandoval | fd9d667 | 2020-04-16 14:46:23 -0700 | [diff] [blame] | 7741 | for (i = 0; i < nr_sectors; i++) { |
Liu Bo | 97bf5a5 | 2017-04-07 13:11:10 -0700 | [diff] [blame] | 7742 | ASSERT(pgoff < PAGE_SIZE); |
Omar Sandoval | fd9d667 | 2020-04-16 14:46:23 -0700 | [diff] [blame] | 7743 | if (uptodate && |
| 7744 | (!csum || !check_data_csum(inode, io_bio, icsum, |
Qu Wenruo | 265d4ac | 2020-10-21 14:24:54 +0800 | [diff] [blame] | 7745 | bvec.bv_page, pgoff))) { |
Omar Sandoval | fd9d667 | 2020-04-16 14:46:23 -0700 | [diff] [blame] | 7746 | clean_io_failure(fs_info, failure_tree, io_tree, |
| 7747 | start, bvec.bv_page, |
| 7748 | btrfs_ino(BTRFS_I(inode)), |
| 7749 | pgoff); |
| 7750 | } else { |
| 7751 | blk_status_t status; |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7752 | |
Omar Sandoval | 77d5d68 | 2020-04-16 14:46:25 -0700 | [diff] [blame] | 7753 | status = btrfs_submit_read_repair(inode, |
| 7754 | &io_bio->bio, |
| 7755 | start - io_bio->logical, |
Omar Sandoval | fd9d667 | 2020-04-16 14:46:23 -0700 | [diff] [blame] | 7756 | bvec.bv_page, pgoff, |
| 7757 | start, |
| 7758 | start + sectorsize - 1, |
Omar Sandoval | 77d5d68 | 2020-04-16 14:46:25 -0700 | [diff] [blame] | 7759 | io_bio->mirror_num, |
| 7760 | submit_dio_repair_bio); |
Omar Sandoval | fd9d667 | 2020-04-16 14:46:23 -0700 | [diff] [blame] | 7761 | if (status) |
| 7762 | err = status; |
| 7763 | } |
| 7764 | start += sectorsize; |
| 7765 | icsum++; |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 7766 | pgoff += sectorsize; |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 7767 | } |
Kent Overstreet | 2c30c71 | 2013-11-07 12:20:26 -0800 | [diff] [blame] | 7768 | } |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 7769 | return err; |
| 7770 | } |
| 7771 | |
Nikolay Borisov | b672b5c | 2020-06-03 08:55:24 +0300 | [diff] [blame] | 7772 | static void __endio_write_update_ordered(struct btrfs_inode *inode, |
Qu Wenruo | 52427260 | 2017-03-08 10:25:52 +0800 | [diff] [blame] | 7773 | const u64 offset, const u64 bytes, |
| 7774 | const bool uptodate) |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7775 | { |
Nikolay Borisov | b672b5c | 2020-06-03 08:55:24 +0300 | [diff] [blame] | 7776 | struct btrfs_fs_info *fs_info = inode->root->fs_info; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7777 | struct btrfs_ordered_extent *ordered = NULL; |
Qu Wenruo | 52427260 | 2017-03-08 10:25:52 +0800 | [diff] [blame] | 7778 | struct btrfs_workqueue *wq; |
Filipe Manana | 1454377 | 2015-11-24 16:23:54 +0000 | [diff] [blame] | 7779 | u64 ordered_offset = offset; |
| 7780 | u64 ordered_bytes = bytes; |
Naohiro Aota | 67c003f | 2017-09-01 17:59:07 +0900 | [diff] [blame] | 7781 | u64 last_offset; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7782 | |
Nikolay Borisov | b672b5c | 2020-06-03 08:55:24 +0300 | [diff] [blame] | 7783 | if (btrfs_is_free_space_inode(inode)) |
Qu Wenruo | 52427260 | 2017-03-08 10:25:52 +0800 | [diff] [blame] | 7784 | wq = fs_info->endio_freespace_worker; |
Omar Sandoval | a0cac0e | 2019-09-16 11:30:57 -0700 | [diff] [blame] | 7785 | else |
Qu Wenruo | 52427260 | 2017-03-08 10:25:52 +0800 | [diff] [blame] | 7786 | wq = fs_info->endio_write_workers; |
Qu Wenruo | 52427260 | 2017-03-08 10:25:52 +0800 | [diff] [blame] | 7787 | |
Nikolay Borisov | b25f0d0 | 2018-04-11 11:21:17 +0300 | [diff] [blame] | 7788 | while (ordered_offset < offset + bytes) { |
| 7789 | last_offset = ordered_offset; |
Nikolay Borisov | b672b5c | 2020-06-03 08:55:24 +0300 | [diff] [blame] | 7790 | if (btrfs_dec_test_first_ordered_pending(inode, &ordered, |
Nikolay Borisov | 7095821 | 2020-06-03 08:55:23 +0300 | [diff] [blame] | 7791 | &ordered_offset, |
| 7792 | ordered_bytes, |
| 7793 | uptodate)) { |
Omar Sandoval | a0cac0e | 2019-09-16 11:30:57 -0700 | [diff] [blame] | 7794 | btrfs_init_work(&ordered->work, finish_ordered_fn, NULL, |
| 7795 | NULL); |
Nikolay Borisov | b25f0d0 | 2018-04-11 11:21:17 +0300 | [diff] [blame] | 7796 | btrfs_queue_work(wq, &ordered->work); |
| 7797 | } |
| 7798 | /* |
| 7799 | * If btrfs_dec_test_ordered_pending does not find any ordered |
| 7800 | * extent in the range, we can exit. |
| 7801 | */ |
| 7802 | if (ordered_offset == last_offset) |
| 7803 | return; |
| 7804 | /* |
| 7805 | * Our bio might span multiple ordered extents. In this case |
Andrea Gelmini | 52042d8 | 2018-11-28 12:05:13 +0100 | [diff] [blame] | 7806 | * we keep going until we have accounted the whole dio. |
Nikolay Borisov | b25f0d0 | 2018-04-11 11:21:17 +0300 | [diff] [blame] | 7807 | */ |
| 7808 | if (ordered_offset < offset + bytes) { |
| 7809 | ordered_bytes = offset + bytes - ordered_offset; |
| 7810 | ordered = NULL; |
| 7811 | } |
Chris Mason | 163cf09 | 2010-11-28 19:56:33 -0500 | [diff] [blame] | 7812 | } |
Filipe Manana | 1454377 | 2015-11-24 16:23:54 +0000 | [diff] [blame] | 7813 | } |
| 7814 | |
Qu Wenruo | 8896a08 | 2020-10-21 14:24:53 +0800 | [diff] [blame] | 7815 | static blk_status_t btrfs_submit_bio_start_direct_io(struct inode *inode, |
| 7816 | struct bio *bio, u64 offset) |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 7817 | { |
Johannes Thumshirn | c965d64 | 2020-07-28 20:25:41 +0900 | [diff] [blame] | 7818 | return btrfs_csum_one_bio(BTRFS_I(inode), bio, offset, 1); |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 7819 | } |
| 7820 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 7821 | static void btrfs_end_dio_bio(struct bio *bio) |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7822 | { |
| 7823 | struct btrfs_dio_private *dip = bio->bi_private; |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 7824 | blk_status_t err = bio->bi_status; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7825 | |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7826 | if (err) |
| 7827 | btrfs_warn(BTRFS_I(dip->inode)->root->fs_info, |
Mike Christie | 6296b96 | 2016-06-05 14:32:21 -0500 | [diff] [blame] | 7828 | "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] | 7829 | btrfs_ino(BTRFS_I(dip->inode)), bio_op(bio), |
| 7830 | bio->bi_opf, |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7831 | (unsigned long long)bio->bi_iter.bi_sector, |
| 7832 | bio->bi_iter.bi_size, err); |
| 7833 | |
Omar Sandoval | 769b4f2 | 2020-04-16 14:46:22 -0700 | [diff] [blame] | 7834 | if (bio_op(bio) == REQ_OP_READ) { |
| 7835 | err = btrfs_check_read_dio_bio(dip->inode, btrfs_io_bio(bio), |
Omar Sandoval | fd9d667 | 2020-04-16 14:46:23 -0700 | [diff] [blame] | 7836 | !err); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7837 | } |
| 7838 | |
Omar Sandoval | 769b4f2 | 2020-04-16 14:46:22 -0700 | [diff] [blame] | 7839 | if (err) |
| 7840 | dip->dio_bio->bi_status = err; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7841 | |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7842 | bio_put(bio); |
Omar Sandoval | 769b4f2 | 2020-04-16 14:46:22 -0700 | [diff] [blame] | 7843 | btrfs_dio_private_put(dip); |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 7844 | } |
| 7845 | |
David Sterba | d0ee393 | 2018-03-08 14:35:48 +0100 | [diff] [blame] | 7846 | static inline blk_status_t btrfs_submit_dio_bio(struct bio *bio, |
| 7847 | struct inode *inode, u64 file_offset, int async_submit) |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7848 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7849 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 7850 | struct btrfs_dio_private *dip = bio->bi_private; |
Mike Christie | 37226b2 | 2016-06-05 14:31:52 -0500 | [diff] [blame] | 7851 | bool write = bio_op(bio) == REQ_OP_WRITE; |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 7852 | blk_status_t ret; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7853 | |
Liu Bo | 4c274bc | 2017-11-01 17:19:27 -0600 | [diff] [blame] | 7854 | /* Check btrfs_submit_bio_hook() for rules about async submit. */ |
Josef Bacik | b812ce2 | 2012-11-16 13:56:32 -0500 | [diff] [blame] | 7855 | if (async_submit) |
| 7856 | async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers); |
| 7857 | |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 7858 | if (!write) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7859 | 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] | 7860 | if (ret) |
| 7861 | goto err; |
| 7862 | } |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7863 | |
Nikolay Borisov | e6961ca | 2017-08-03 15:44:58 +0300 | [diff] [blame] | 7864 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM) |
Josef Bacik | 1ae3993 | 2011-04-06 14:41:34 -0400 | [diff] [blame] | 7865 | goto map; |
| 7866 | |
| 7867 | if (write && async_submit) { |
Qu Wenruo | 8896a08 | 2020-10-21 14:24:53 +0800 | [diff] [blame] | 7868 | ret = btrfs_wq_submit_bio(inode, bio, 0, 0, |
| 7869 | file_offset, |
David Sterba | e288c08 | 2018-07-18 17:36:24 +0200 | [diff] [blame] | 7870 | btrfs_submit_bio_start_direct_io); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7871 | goto err; |
Josef Bacik | 1ae3993 | 2011-04-06 14:41:34 -0400 | [diff] [blame] | 7872 | } else if (write) { |
| 7873 | /* |
| 7874 | * If we aren't doing async submit, calculate the csum of the |
| 7875 | * bio now. |
| 7876 | */ |
Nikolay Borisov | bd242a0 | 2020-06-03 08:55:07 +0300 | [diff] [blame] | 7877 | ret = btrfs_csum_one_bio(BTRFS_I(inode), bio, file_offset, 1); |
Josef Bacik | 1ae3993 | 2011-04-06 14:41:34 -0400 | [diff] [blame] | 7878 | if (ret) |
| 7879 | goto err; |
Miao Xie | 23ea8e5 | 2014-09-12 18:43:54 +0800 | [diff] [blame] | 7880 | } else { |
Omar Sandoval | 8587957 | 2020-04-16 14:46:21 -0700 | [diff] [blame] | 7881 | u64 csum_offset; |
| 7882 | |
| 7883 | csum_offset = file_offset - dip->logical_offset; |
David Sterba | 265fdfa | 2020-07-01 21:19:09 +0200 | [diff] [blame] | 7884 | csum_offset >>= fs_info->sectorsize_bits; |
David Sterba | 55fc29b | 2020-06-30 02:01:31 +0200 | [diff] [blame] | 7885 | csum_offset *= fs_info->csum_size; |
Omar Sandoval | 8587957 | 2020-04-16 14:46:21 -0700 | [diff] [blame] | 7886 | btrfs_io_bio(bio)->csum = dip->csums + csum_offset; |
Tsutomu Itoh | c2db107 | 2011-03-01 06:48:31 +0000 | [diff] [blame] | 7887 | } |
Josef Bacik | 1ae3993 | 2011-04-06 14:41:34 -0400 | [diff] [blame] | 7888 | map: |
Chris Mason | 08635ba | 2019-07-10 12:28:14 -0700 | [diff] [blame] | 7889 | ret = btrfs_map_bio(fs_info, bio, 0); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7890 | err: |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7891 | return ret; |
| 7892 | } |
| 7893 | |
Omar Sandoval | c36cac2 | 2020-04-16 14:46:13 -0700 | [diff] [blame] | 7894 | /* |
| 7895 | * If this succeeds, the btrfs_dio_private is responsible for cleaning up locked |
| 7896 | * or ordered extents whether or not we submit any bios. |
| 7897 | */ |
| 7898 | static struct btrfs_dio_private *btrfs_create_dio_private(struct bio *dio_bio, |
| 7899 | struct inode *inode, |
| 7900 | loff_t file_offset) |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7901 | { |
Omar Sandoval | c36cac2 | 2020-04-16 14:46:13 -0700 | [diff] [blame] | 7902 | const bool write = (bio_op(dio_bio) == REQ_OP_WRITE); |
Omar Sandoval | 8587957 | 2020-04-16 14:46:21 -0700 | [diff] [blame] | 7903 | const bool csum = !(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM); |
| 7904 | size_t dip_size; |
Omar Sandoval | c36cac2 | 2020-04-16 14:46:13 -0700 | [diff] [blame] | 7905 | struct btrfs_dio_private *dip; |
Omar Sandoval | c36cac2 | 2020-04-16 14:46:13 -0700 | [diff] [blame] | 7906 | |
Omar Sandoval | 8587957 | 2020-04-16 14:46:21 -0700 | [diff] [blame] | 7907 | dip_size = sizeof(*dip); |
| 7908 | if (!write && csum) { |
| 7909 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Omar Sandoval | 8587957 | 2020-04-16 14:46:21 -0700 | [diff] [blame] | 7910 | size_t nblocks; |
| 7911 | |
David Sterba | 265fdfa | 2020-07-01 21:19:09 +0200 | [diff] [blame] | 7912 | nblocks = dio_bio->bi_iter.bi_size >> fs_info->sectorsize_bits; |
David Sterba | 223486c | 2020-07-02 11:27:30 +0200 | [diff] [blame] | 7913 | dip_size += fs_info->csum_size * nblocks; |
Omar Sandoval | 8587957 | 2020-04-16 14:46:21 -0700 | [diff] [blame] | 7914 | } |
| 7915 | |
| 7916 | dip = kzalloc(dip_size, GFP_NOFS); |
Omar Sandoval | c36cac2 | 2020-04-16 14:46:13 -0700 | [diff] [blame] | 7917 | if (!dip) |
| 7918 | return NULL; |
| 7919 | |
Omar Sandoval | c36cac2 | 2020-04-16 14:46:13 -0700 | [diff] [blame] | 7920 | dip->inode = inode; |
| 7921 | dip->logical_offset = file_offset; |
| 7922 | dip->bytes = dio_bio->bi_iter.bi_size; |
| 7923 | dip->disk_bytenr = (u64)dio_bio->bi_iter.bi_sector << 9; |
Omar Sandoval | c36cac2 | 2020-04-16 14:46:13 -0700 | [diff] [blame] | 7924 | dip->dio_bio = dio_bio; |
Omar Sandoval | e3b318d | 2020-04-16 14:46:20 -0700 | [diff] [blame] | 7925 | refcount_set(&dip->refs, 1); |
Omar Sandoval | c36cac2 | 2020-04-16 14:46:13 -0700 | [diff] [blame] | 7926 | return dip; |
| 7927 | } |
| 7928 | |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 7929 | static blk_qc_t btrfs_submit_direct(struct inode *inode, struct iomap *iomap, |
| 7930 | struct bio *dio_bio, loff_t file_offset) |
Omar Sandoval | c36cac2 | 2020-04-16 14:46:13 -0700 | [diff] [blame] | 7931 | { |
| 7932 | const bool write = (bio_op(dio_bio) == REQ_OP_WRITE); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7933 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Omar Sandoval | 769b4f2 | 2020-04-16 14:46:22 -0700 | [diff] [blame] | 7934 | const bool raid56 = (btrfs_data_alloc_profile(fs_info) & |
| 7935 | BTRFS_BLOCK_GROUP_RAID56_MASK); |
Omar Sandoval | c36cac2 | 2020-04-16 14:46:13 -0700 | [diff] [blame] | 7936 | struct btrfs_dio_private *dip; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7937 | struct bio *bio; |
Omar Sandoval | c36cac2 | 2020-04-16 14:46:13 -0700 | [diff] [blame] | 7938 | u64 start_sector; |
Josef Bacik | 1ae3993 | 2011-04-06 14:41:34 -0400 | [diff] [blame] | 7939 | int async_submit = 0; |
Liu Bo | 725130b | 2017-05-16 09:51:39 -0700 | [diff] [blame] | 7940 | u64 submit_len; |
| 7941 | int clone_offset = 0; |
| 7942 | int clone_len; |
Chandan Rajendra | 5f4dc8f | 2016-01-21 15:56:00 +0530 | [diff] [blame] | 7943 | int ret; |
Omar Sandoval | 58efbc9 | 2017-08-22 23:45:59 -0700 | [diff] [blame] | 7944 | blk_status_t status; |
Nikolay Borisov | 89b798a | 2019-06-03 12:05:05 +0300 | [diff] [blame] | 7945 | struct btrfs_io_geometry geom; |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 7946 | struct btrfs_dio_data *dio_data = iomap->private; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7947 | |
Omar Sandoval | c36cac2 | 2020-04-16 14:46:13 -0700 | [diff] [blame] | 7948 | dip = btrfs_create_dio_private(dio_bio, inode, file_offset); |
| 7949 | if (!dip) { |
| 7950 | if (!write) { |
| 7951 | unlock_extent(&BTRFS_I(inode)->io_tree, file_offset, |
| 7952 | file_offset + dio_bio->bi_iter.bi_size - 1); |
| 7953 | } |
| 7954 | dio_bio->bi_status = BLK_STS_RESOURCE; |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 7955 | bio_endio(dio_bio); |
| 7956 | return BLK_QC_T_NONE; |
Josef Bacik | 02f57c7 | 2011-04-06 14:25:44 -0400 | [diff] [blame] | 7957 | } |
| 7958 | |
Josef Bacik | 334c16d | 2020-10-16 11:29:14 -0400 | [diff] [blame] | 7959 | if (!write) { |
Omar Sandoval | 8587957 | 2020-04-16 14:46:21 -0700 | [diff] [blame] | 7960 | /* |
| 7961 | * Load the csums up front to reduce csum tree searches and |
| 7962 | * contention when submitting bios. |
Josef Bacik | 334c16d | 2020-10-16 11:29:14 -0400 | [diff] [blame] | 7963 | * |
| 7964 | * If we have csums disabled this will do nothing. |
Omar Sandoval | 8587957 | 2020-04-16 14:46:21 -0700 | [diff] [blame] | 7965 | */ |
| 7966 | status = btrfs_lookup_bio_sums(inode, dio_bio, file_offset, |
| 7967 | dip->csums); |
| 7968 | if (status != BLK_STS_OK) |
| 7969 | goto out_err; |
| 7970 | } |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 7971 | |
Omar Sandoval | 769b4f2 | 2020-04-16 14:46:22 -0700 | [diff] [blame] | 7972 | start_sector = dio_bio->bi_iter.bi_sector; |
| 7973 | submit_len = dio_bio->bi_iter.bi_size; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7974 | |
Arnd Bergmann | 3c91ee6 | 2017-05-18 15:33:29 +0200 | [diff] [blame] | 7975 | do { |
Omar Sandoval | 769b4f2 | 2020-04-16 14:46:22 -0700 | [diff] [blame] | 7976 | ret = btrfs_get_io_geometry(fs_info, btrfs_op(dio_bio), |
| 7977 | start_sector << 9, submit_len, |
| 7978 | &geom); |
| 7979 | if (ret) { |
| 7980 | status = errno_to_blk_status(ret); |
| 7981 | goto out_err; |
| 7982 | } |
| 7983 | ASSERT(geom.len <= INT_MAX); |
| 7984 | |
Nikolay Borisov | 89b798a | 2019-06-03 12:05:05 +0300 | [diff] [blame] | 7985 | clone_len = min_t(int, submit_len, geom.len); |
Josef Bacik | 02f57c7 | 2011-04-06 14:25:44 -0400 | [diff] [blame] | 7986 | |
Liu Bo | 725130b | 2017-05-16 09:51:39 -0700 | [diff] [blame] | 7987 | /* |
| 7988 | * This will never fail as it's passing GPF_NOFS and |
| 7989 | * the allocation is backed by btrfs_bioset. |
| 7990 | */ |
Omar Sandoval | 769b4f2 | 2020-04-16 14:46:22 -0700 | [diff] [blame] | 7991 | bio = btrfs_bio_clone_partial(dio_bio, clone_offset, clone_len); |
Liu Bo | 725130b | 2017-05-16 09:51:39 -0700 | [diff] [blame] | 7992 | bio->bi_private = dip; |
| 7993 | bio->bi_end_io = btrfs_end_dio_bio; |
| 7994 | btrfs_io_bio(bio)->logical = file_offset; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7995 | |
Liu Bo | 725130b | 2017-05-16 09:51:39 -0700 | [diff] [blame] | 7996 | ASSERT(submit_len >= clone_len); |
| 7997 | submit_len -= clone_len; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7998 | |
Liu Bo | 725130b | 2017-05-16 09:51:39 -0700 | [diff] [blame] | 7999 | /* |
| 8000 | * Increase the count before we submit the bio so we know |
| 8001 | * the end IO handler won't happen before we increase the |
| 8002 | * count. Otherwise, the dip might get freed before we're |
| 8003 | * done setting it up. |
Omar Sandoval | 769b4f2 | 2020-04-16 14:46:22 -0700 | [diff] [blame] | 8004 | * |
| 8005 | * We transfer the initial reference to the last bio, so we |
| 8006 | * don't need to increment the reference count for the last one. |
Liu Bo | 725130b | 2017-05-16 09:51:39 -0700 | [diff] [blame] | 8007 | */ |
Omar Sandoval | 769b4f2 | 2020-04-16 14:46:22 -0700 | [diff] [blame] | 8008 | if (submit_len > 0) { |
| 8009 | refcount_inc(&dip->refs); |
| 8010 | /* |
| 8011 | * If we are submitting more than one bio, submit them |
| 8012 | * all asynchronously. The exception is RAID 5 or 6, as |
| 8013 | * asynchronous checksums make it difficult to collect |
| 8014 | * full stripe writes. |
| 8015 | */ |
| 8016 | if (!raid56) |
| 8017 | async_submit = 1; |
| 8018 | } |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8019 | |
David Sterba | d0ee393 | 2018-03-08 14:35:48 +0100 | [diff] [blame] | 8020 | status = btrfs_submit_dio_bio(bio, inode, file_offset, |
Omar Sandoval | 58efbc9 | 2017-08-22 23:45:59 -0700 | [diff] [blame] | 8021 | async_submit); |
| 8022 | if (status) { |
Liu Bo | 725130b | 2017-05-16 09:51:39 -0700 | [diff] [blame] | 8023 | bio_put(bio); |
Omar Sandoval | 769b4f2 | 2020-04-16 14:46:22 -0700 | [diff] [blame] | 8024 | if (submit_len > 0) |
| 8025 | refcount_dec(&dip->refs); |
Liu Bo | 725130b | 2017-05-16 09:51:39 -0700 | [diff] [blame] | 8026 | goto out_err; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8027 | } |
Liu Bo | 725130b | 2017-05-16 09:51:39 -0700 | [diff] [blame] | 8028 | |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 8029 | dio_data->submitted += clone_len; |
Liu Bo | 725130b | 2017-05-16 09:51:39 -0700 | [diff] [blame] | 8030 | clone_offset += clone_len; |
| 8031 | start_sector += clone_len >> 9; |
| 8032 | file_offset += clone_len; |
Arnd Bergmann | 3c91ee6 | 2017-05-18 15:33:29 +0200 | [diff] [blame] | 8033 | } while (submit_len > 0); |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 8034 | return BLK_QC_T_NONE; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8035 | |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8036 | out_err: |
Omar Sandoval | 769b4f2 | 2020-04-16 14:46:22 -0700 | [diff] [blame] | 8037 | dip->dio_bio->bi_status = status; |
| 8038 | btrfs_dio_private_put(dip); |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 8039 | return BLK_QC_T_NONE; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8040 | } |
| 8041 | |
Goldwyn Rodrigues | 4e4cabe | 2020-09-24 11:39:12 -0500 | [diff] [blame] | 8042 | const struct iomap_ops btrfs_dio_iomap_ops = { |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 8043 | .iomap_begin = btrfs_dio_iomap_begin, |
| 8044 | .iomap_end = btrfs_dio_iomap_end, |
| 8045 | }; |
| 8046 | |
Goldwyn Rodrigues | 4e4cabe | 2020-09-24 11:39:12 -0500 | [diff] [blame] | 8047 | const struct iomap_dio_ops btrfs_dio_ops = { |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 8048 | .submit_io = btrfs_submit_direct, |
| 8049 | }; |
| 8050 | |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 8051 | static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, |
David Sterba | bab16e2 | 2020-06-23 20:56:12 +0200 | [diff] [blame] | 8052 | u64 start, u64 len) |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 8053 | { |
Tsutomu Itoh | 05dadc0 | 2012-11-29 05:08:26 +0000 | [diff] [blame] | 8054 | int ret; |
| 8055 | |
Christoph Hellwig | 45dd052 | 2020-05-23 09:30:14 +0200 | [diff] [blame] | 8056 | ret = fiemap_prep(inode, fieinfo, start, &len, 0); |
Tsutomu Itoh | 05dadc0 | 2012-11-29 05:08:26 +0000 | [diff] [blame] | 8057 | if (ret) |
| 8058 | return ret; |
| 8059 | |
Nikolay Borisov | facee0a | 2020-08-31 14:42:49 +0300 | [diff] [blame] | 8060 | return extent_fiemap(BTRFS_I(inode), fieinfo, start, len); |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 8061 | } |
| 8062 | |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8063 | int btrfs_readpage(struct file *file, struct page *page) |
| 8064 | { |
Nikolay Borisov | 0f20881 | 2020-09-14 14:39:16 +0300 | [diff] [blame] | 8065 | struct btrfs_inode *inode = BTRFS_I(page->mapping->host); |
| 8066 | u64 start = page_offset(page); |
| 8067 | u64 end = start + PAGE_SIZE - 1; |
Nikolay Borisov | c1be9c1 | 2020-09-14 12:37:08 +0300 | [diff] [blame] | 8068 | unsigned long bio_flags = 0; |
Nikolay Borisov | 0f20881 | 2020-09-14 14:39:16 +0300 | [diff] [blame] | 8069 | struct bio *bio = NULL; |
Nikolay Borisov | c1be9c1 | 2020-09-14 12:37:08 +0300 | [diff] [blame] | 8070 | int ret; |
| 8071 | |
Nikolay Borisov | 0f20881 | 2020-09-14 14:39:16 +0300 | [diff] [blame] | 8072 | btrfs_lock_and_flush_ordered_range(inode, start, end, NULL); |
| 8073 | |
| 8074 | ret = btrfs_do_readpage(page, NULL, &bio, &bio_flags, 0, NULL); |
Nikolay Borisov | c1be9c1 | 2020-09-14 12:37:08 +0300 | [diff] [blame] | 8075 | if (bio) |
| 8076 | ret = submit_one_bio(bio, 0, bio_flags); |
| 8077 | return ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8078 | } |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 8079 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8080 | static int btrfs_writepage(struct page *page, struct writeback_control *wbc) |
| 8081 | { |
Josef Bacik | be7bd73 | 2015-10-22 15:05:09 -0400 | [diff] [blame] | 8082 | struct inode *inode = page->mapping->host; |
| 8083 | int ret; |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8084 | |
| 8085 | if (current->flags & PF_MEMALLOC) { |
| 8086 | redirty_page_for_writepage(wbc, page); |
| 8087 | unlock_page(page); |
| 8088 | return 0; |
| 8089 | } |
Josef Bacik | be7bd73 | 2015-10-22 15:05:09 -0400 | [diff] [blame] | 8090 | |
| 8091 | /* |
| 8092 | * If we are under memory pressure we will call this directly from the |
| 8093 | * VM, we need to make sure we have the inode referenced for the ordered |
| 8094 | * extent. If not just return like we didn't do anything. |
| 8095 | */ |
| 8096 | if (!igrab(inode)) { |
| 8097 | redirty_page_for_writepage(wbc, page); |
| 8098 | return AOP_WRITEPAGE_ACTIVATE; |
| 8099 | } |
Nikolay Borisov | 0a9b0e5 | 2017-12-08 15:55:59 +0200 | [diff] [blame] | 8100 | ret = extent_write_full_page(page, wbc); |
Josef Bacik | be7bd73 | 2015-10-22 15:05:09 -0400 | [diff] [blame] | 8101 | btrfs_add_delayed_iput(inode); |
| 8102 | return ret; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8103 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8104 | |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 8105 | static int btrfs_writepages(struct address_space *mapping, |
| 8106 | struct writeback_control *wbc) |
Chris Mason | b293f02e | 2007-11-01 19:45:34 -0400 | [diff] [blame] | 8107 | { |
Nikolay Borisov | 8ae225a | 2018-04-19 10:46:38 +0300 | [diff] [blame] | 8108 | return extent_writepages(mapping, wbc); |
Chris Mason | b293f02e | 2007-11-01 19:45:34 -0400 | [diff] [blame] | 8109 | } |
| 8110 | |
Matthew Wilcox (Oracle) | ba206a0 | 2020-06-01 21:47:05 -0700 | [diff] [blame] | 8111 | static void btrfs_readahead(struct readahead_control *rac) |
Chris Mason | 3ab2fb5 | 2007-11-08 10:59:22 -0500 | [diff] [blame] | 8112 | { |
Matthew Wilcox (Oracle) | ba206a0 | 2020-06-01 21:47:05 -0700 | [diff] [blame] | 8113 | extent_readahead(rac); |
Chris Mason | 3ab2fb5 | 2007-11-08 10:59:22 -0500 | [diff] [blame] | 8114 | } |
Nikolay Borisov | 2a3ff0a | 2018-04-19 10:46:36 +0300 | [diff] [blame] | 8115 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8116 | static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8117 | { |
Nikolay Borisov | 477a30b | 2018-04-19 10:46:34 +0300 | [diff] [blame] | 8118 | int ret = try_release_extent_mapping(page, gfp_flags); |
Guoqing Jiang | d1b89bc | 2020-06-01 21:47:45 -0700 | [diff] [blame] | 8119 | if (ret == 1) |
| 8120 | detach_page_private(page); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8121 | return ret; |
| 8122 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8123 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8124 | static int btrfs_releasepage(struct page *page, gfp_t gfp_flags) |
| 8125 | { |
Chris Mason | 98509cf | 2008-09-11 15:51:43 -0400 | [diff] [blame] | 8126 | if (PageWriteback(page) || PageDirty(page)) |
| 8127 | return 0; |
Michal Hocko | 3ba7ab2 | 2017-01-09 15:39:02 +0100 | [diff] [blame] | 8128 | return __btrfs_releasepage(page, gfp_flags); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8129 | } |
| 8130 | |
Roman Gushchin | f8e6608 | 2020-03-04 16:57:35 -0800 | [diff] [blame] | 8131 | #ifdef CONFIG_MIGRATION |
| 8132 | static int btrfs_migratepage(struct address_space *mapping, |
| 8133 | struct page *newpage, struct page *page, |
| 8134 | enum migrate_mode mode) |
| 8135 | { |
| 8136 | int ret; |
| 8137 | |
| 8138 | ret = migrate_page_move_mapping(mapping, newpage, page, 0); |
| 8139 | if (ret != MIGRATEPAGE_SUCCESS) |
| 8140 | return ret; |
| 8141 | |
Guoqing Jiang | d1b89bc | 2020-06-01 21:47:45 -0700 | [diff] [blame] | 8142 | if (page_has_private(page)) |
| 8143 | attach_page_private(newpage, detach_page_private(page)); |
Roman Gushchin | f8e6608 | 2020-03-04 16:57:35 -0800 | [diff] [blame] | 8144 | |
| 8145 | if (PagePrivate2(page)) { |
| 8146 | ClearPagePrivate2(page); |
| 8147 | SetPagePrivate2(newpage); |
| 8148 | } |
| 8149 | |
| 8150 | if (mode != MIGRATE_SYNC_NO_COPY) |
| 8151 | migrate_page_copy(newpage, page); |
| 8152 | else |
| 8153 | migrate_page_states(newpage, page); |
| 8154 | return MIGRATEPAGE_SUCCESS; |
| 8155 | } |
| 8156 | #endif |
| 8157 | |
Lukas Czerner | d47992f | 2013-05-21 23:17:23 -0400 | [diff] [blame] | 8158 | static void btrfs_invalidatepage(struct page *page, unsigned int offset, |
| 8159 | unsigned int length) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8160 | { |
Nikolay Borisov | 53ac7ea | 2020-08-31 14:42:43 +0300 | [diff] [blame] | 8161 | struct btrfs_inode *inode = BTRFS_I(page->mapping->host); |
| 8162 | struct extent_io_tree *tree = &inode->io_tree; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8163 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 8164 | struct extent_state *cached_state = NULL; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8165 | u64 page_start = page_offset(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 8166 | u64 page_end = page_start + PAGE_SIZE - 1; |
Chandan Rajendra | dbfdb6d | 2016-01-21 15:55:58 +0530 | [diff] [blame] | 8167 | u64 start; |
| 8168 | u64 end; |
Nikolay Borisov | 53ac7ea | 2020-08-31 14:42:43 +0300 | [diff] [blame] | 8169 | int inode_evicting = inode->vfs_inode.i_state & I_FREEING; |
Filipe Manana | 2766ff6 | 2020-11-04 11:07:34 +0000 | [diff] [blame] | 8170 | bool found_ordered = false; |
| 8171 | bool completed_ordered = false; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8172 | |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 8173 | /* |
| 8174 | * we have the page locked, so new writeback can't start, |
| 8175 | * and the dirty bit won't be cleared while we are here. |
| 8176 | * |
| 8177 | * Wait for IO on this page so that we can safely clear |
| 8178 | * the PagePrivate2 bit and do ordered accounting |
| 8179 | */ |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8180 | wait_on_page_writeback(page); |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 8181 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8182 | if (offset) { |
| 8183 | btrfs_releasepage(page, GFP_NOFS); |
| 8184 | return; |
| 8185 | } |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 8186 | |
| 8187 | if (!inode_evicting) |
David Sterba | ff13db4 | 2015-12-03 14:30:40 +0100 | [diff] [blame] | 8188 | lock_extent_bits(tree, page_start, page_end, &cached_state); |
Chandan Rajendra | dbfdb6d | 2016-01-21 15:55:58 +0530 | [diff] [blame] | 8189 | again: |
| 8190 | start = page_start; |
Nikolay Borisov | 53ac7ea | 2020-08-31 14:42:43 +0300 | [diff] [blame] | 8191 | ordered = btrfs_lookup_ordered_range(inode, start, page_end - start + 1); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8192 | if (ordered) { |
Filipe Manana | 2766ff6 | 2020-11-04 11:07:34 +0000 | [diff] [blame] | 8193 | found_ordered = true; |
Omar Sandoval | bffe633 | 2019-12-02 17:34:19 -0800 | [diff] [blame] | 8194 | end = min(page_end, |
| 8195 | ordered->file_offset + ordered->num_bytes - 1); |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 8196 | /* |
Filipe Manana | 2766ff6 | 2020-11-04 11:07:34 +0000 | [diff] [blame] | 8197 | * IO on this page will never be started, so we need to account |
| 8198 | * for any ordered extents now. Don't clear EXTENT_DELALLOC_NEW |
| 8199 | * here, must leave that up for the ordered extent completion. |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 8200 | */ |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 8201 | if (!inode_evicting) |
Chandan Rajendra | dbfdb6d | 2016-01-21 15:55:58 +0530 | [diff] [blame] | 8202 | clear_extent_bit(tree, start, end, |
Filipe Manana | 2766ff6 | 2020-11-04 11:07:34 +0000 | [diff] [blame] | 8203 | EXTENT_DELALLOC | |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 8204 | EXTENT_LOCKED | EXTENT_DO_ACCOUNTING | |
David Sterba | ae0f162 | 2017-10-31 16:37:52 +0100 | [diff] [blame] | 8205 | EXTENT_DEFRAG, 1, 0, &cached_state); |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 8206 | /* |
| 8207 | * whoever cleared the private bit is responsible |
| 8208 | * for the finish_ordered_io |
| 8209 | */ |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 8210 | if (TestClearPagePrivate2(page)) { |
| 8211 | struct btrfs_ordered_inode_tree *tree; |
| 8212 | u64 new_len; |
| 8213 | |
Nikolay Borisov | 53ac7ea | 2020-08-31 14:42:43 +0300 | [diff] [blame] | 8214 | tree = &inode->ordered_tree; |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 8215 | |
| 8216 | spin_lock_irq(&tree->lock); |
| 8217 | set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags); |
Chandan Rajendra | dbfdb6d | 2016-01-21 15:55:58 +0530 | [diff] [blame] | 8218 | new_len = start - ordered->file_offset; |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 8219 | if (new_len < ordered->truncated_len) |
| 8220 | ordered->truncated_len = new_len; |
| 8221 | spin_unlock_irq(&tree->lock); |
| 8222 | |
Nikolay Borisov | 53ac7ea | 2020-08-31 14:42:43 +0300 | [diff] [blame] | 8223 | if (btrfs_dec_test_ordered_pending(inode, &ordered, |
| 8224 | start, |
Filipe Manana | 2766ff6 | 2020-11-04 11:07:34 +0000 | [diff] [blame] | 8225 | end - start + 1, 1)) { |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 8226 | btrfs_finish_ordered_io(ordered); |
Filipe Manana | 2766ff6 | 2020-11-04 11:07:34 +0000 | [diff] [blame] | 8227 | completed_ordered = true; |
| 8228 | } |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 8229 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8230 | btrfs_put_ordered_extent(ordered); |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 8231 | if (!inode_evicting) { |
| 8232 | cached_state = NULL; |
Chandan Rajendra | dbfdb6d | 2016-01-21 15:55:58 +0530 | [diff] [blame] | 8233 | lock_extent_bits(tree, start, end, |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 8234 | &cached_state); |
| 8235 | } |
Chandan Rajendra | dbfdb6d | 2016-01-21 15:55:58 +0530 | [diff] [blame] | 8236 | |
| 8237 | start = end + 1; |
| 8238 | if (start < page_end) |
| 8239 | goto again; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8240 | } |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 8241 | |
Qu Wenruo | b9d0b38 | 2015-09-29 10:35:16 +0800 | [diff] [blame] | 8242 | /* |
| 8243 | * Qgroup reserved space handler |
| 8244 | * Page here will be either |
Qu Wenruo | fa91e4a | 2020-07-17 15:12:05 +0800 | [diff] [blame] | 8245 | * 1) Already written to disk or ordered extent already submitted |
| 8246 | * Then its QGROUP_RESERVED bit in io_tree is already cleaned. |
| 8247 | * Qgroup will be handled by its qgroup_record then. |
| 8248 | * btrfs_qgroup_free_data() call will do nothing here. |
| 8249 | * |
| 8250 | * 2) Not written to disk yet |
| 8251 | * Then btrfs_qgroup_free_data() call will clear the QGROUP_RESERVED |
| 8252 | * bit of its io_tree, and free the qgroup reserved data space. |
| 8253 | * Since the IO will never happen for this page. |
Qu Wenruo | b9d0b38 | 2015-09-29 10:35:16 +0800 | [diff] [blame] | 8254 | */ |
Nikolay Borisov | 53ac7ea | 2020-08-31 14:42:43 +0300 | [diff] [blame] | 8255 | btrfs_qgroup_free_data(inode, NULL, page_start, PAGE_SIZE); |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 8256 | if (!inode_evicting) { |
Filipe Manana | 2766ff6 | 2020-11-04 11:07:34 +0000 | [diff] [blame] | 8257 | bool delete = true; |
| 8258 | |
| 8259 | /* |
| 8260 | * If there's an ordered extent for this range and we have not |
| 8261 | * finished it ourselves, we must leave EXTENT_DELALLOC_NEW set |
| 8262 | * in the range for the ordered extent completion. We must also |
| 8263 | * not delete the range, otherwise we would lose that bit (and |
| 8264 | * any other bits set in the range). Make sure EXTENT_UPTODATE |
| 8265 | * is cleared if we don't delete, otherwise it can lead to |
| 8266 | * corruptions if the i_size is extented later. |
| 8267 | */ |
| 8268 | if (found_ordered && !completed_ordered) |
| 8269 | delete = false; |
Omar Sandoval | e182163 | 2019-08-15 14:04:04 -0700 | [diff] [blame] | 8270 | clear_extent_bit(tree, page_start, page_end, EXTENT_LOCKED | |
Filipe Manana | 2766ff6 | 2020-11-04 11:07:34 +0000 | [diff] [blame] | 8271 | EXTENT_DELALLOC | EXTENT_UPTODATE | |
| 8272 | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, |
| 8273 | delete, &cached_state); |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 8274 | |
| 8275 | __btrfs_releasepage(page, GFP_NOFS); |
| 8276 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8277 | |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 8278 | ClearPageChecked(page); |
Guoqing Jiang | d1b89bc | 2020-06-01 21:47:45 -0700 | [diff] [blame] | 8279 | detach_page_private(page); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8280 | } |
| 8281 | |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8282 | /* |
| 8283 | * btrfs_page_mkwrite() is not allowed to change the file size as it gets |
| 8284 | * called from a page fault handler when a page is first dirtied. Hence we must |
| 8285 | * be careful to check for EOF conditions here. We set the page up correctly |
| 8286 | * for a written page which means we get ENOSPC checking when writing into |
| 8287 | * holes and correct delalloc and unwritten extent mapping on filesystems that |
| 8288 | * support these features. |
| 8289 | * |
| 8290 | * We are not allowed to take the i_mutex here so we have to play games to |
| 8291 | * protect against truncate races as the page could now be beyond EOF. Because |
Omar Sandoval | d1342aa | 2018-05-11 13:13:29 -0700 | [diff] [blame] | 8292 | * truncate_setsize() writes the inode size before removing pages, once we have |
| 8293 | * the page lock we can determine safely if the page is beyond EOF. If it is not |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8294 | * beyond EOF, then the page is guaranteed safe against truncation until we |
| 8295 | * unlock the page. |
| 8296 | */ |
Souptick Joarder | a528a24 | 2018-06-06 19:54:44 +0530 | [diff] [blame] | 8297 | vm_fault_t btrfs_page_mkwrite(struct vm_fault *vmf) |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8298 | { |
Nick Piggin | c2ec175 | 2009-03-31 15:23:21 -0700 | [diff] [blame] | 8299 | struct page *page = vmf->page; |
Dave Jiang | 11bac80 | 2017-02-24 14:56:41 -0800 | [diff] [blame] | 8300 | struct inode *inode = file_inode(vmf->vma->vm_file); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8301 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8302 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
| 8303 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 8304 | struct extent_state *cached_state = NULL; |
Qu Wenruo | 364ecf3 | 2017-02-27 15:10:38 +0800 | [diff] [blame] | 8305 | struct extent_changeset *data_reserved = NULL; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8306 | char *kaddr; |
| 8307 | unsigned long zero_start; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8308 | loff_t size; |
Souptick Joarder | a528a24 | 2018-06-06 19:54:44 +0530 | [diff] [blame] | 8309 | vm_fault_t ret; |
| 8310 | int ret2; |
Chris Mason | 9998eb7 | 2012-01-25 13:47:40 -0500 | [diff] [blame] | 8311 | int reserved = 0; |
Chandan Rajendra | d0b7da88 | 2016-01-21 15:55:57 +0530 | [diff] [blame] | 8312 | u64 reserved_space; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8313 | u64 page_start; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8314 | u64 page_end; |
Chandan Rajendra | d0b7da88 | 2016-01-21 15:55:57 +0530 | [diff] [blame] | 8315 | u64 end; |
| 8316 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 8317 | reserved_space = PAGE_SIZE; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8318 | |
Jan Kara | b2b5ef5 | 2012-06-12 16:20:45 +0200 | [diff] [blame] | 8319 | sb_start_pagefault(inode->i_sb); |
Qu Wenruo | df48063 | 2015-09-08 17:25:54 +0800 | [diff] [blame] | 8320 | page_start = page_offset(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 8321 | page_end = page_start + PAGE_SIZE - 1; |
Chandan Rajendra | d0b7da88 | 2016-01-21 15:55:57 +0530 | [diff] [blame] | 8322 | end = page_end; |
Qu Wenruo | df48063 | 2015-09-08 17:25:54 +0800 | [diff] [blame] | 8323 | |
Chandan Rajendra | d0b7da88 | 2016-01-21 15:55:57 +0530 | [diff] [blame] | 8324 | /* |
| 8325 | * Reserving delalloc space after obtaining the page lock can lead to |
| 8326 | * deadlock. For example, if a dirty page is locked by this function |
| 8327 | * and the call to btrfs_delalloc_reserve_space() ends up triggering |
| 8328 | * dirty page write out, then the btrfs_writepage() function could |
| 8329 | * end up waiting indefinitely to get a lock on the page currently |
| 8330 | * being processed by btrfs_page_mkwrite() function. |
| 8331 | */ |
Nikolay Borisov | e5b7231e | 2020-06-03 08:55:42 +0300 | [diff] [blame] | 8332 | ret2 = btrfs_delalloc_reserve_space(BTRFS_I(inode), &data_reserved, |
| 8333 | page_start, reserved_space); |
Souptick Joarder | a528a24 | 2018-06-06 19:54:44 +0530 | [diff] [blame] | 8334 | if (!ret2) { |
| 8335 | ret2 = file_update_time(vmf->vma->vm_file); |
Chris Mason | 9998eb7 | 2012-01-25 13:47:40 -0500 | [diff] [blame] | 8336 | reserved = 1; |
| 8337 | } |
Souptick Joarder | a528a24 | 2018-06-06 19:54:44 +0530 | [diff] [blame] | 8338 | if (ret2) { |
| 8339 | ret = vmf_error(ret2); |
Chris Mason | 9998eb7 | 2012-01-25 13:47:40 -0500 | [diff] [blame] | 8340 | if (reserved) |
| 8341 | goto out; |
| 8342 | goto out_noreserve; |
Nick Piggin | 56a76f8 | 2009-03-31 15:23:23 -0700 | [diff] [blame] | 8343 | } |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 8344 | |
Nick Piggin | 56a76f8 | 2009-03-31 15:23:23 -0700 | [diff] [blame] | 8345 | ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */ |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8346 | again: |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8347 | lock_page(page); |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8348 | size = i_size_read(inode); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8349 | |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8350 | if ((page->mapping != inode->i_mapping) || |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8351 | (page_start >= size)) { |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8352 | /* page got truncated out from underneath us */ |
| 8353 | goto out_unlock; |
| 8354 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8355 | wait_on_page_writeback(page); |
| 8356 | |
David Sterba | ff13db4 | 2015-12-03 14:30:40 +0100 | [diff] [blame] | 8357 | lock_extent_bits(io_tree, page_start, page_end, &cached_state); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8358 | set_page_extent_mapped(page); |
| 8359 | |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 8360 | /* |
| 8361 | * we can't set the delalloc bits if there are pending ordered |
| 8362 | * extents. Drop our locks and wait for them to finish |
| 8363 | */ |
Nikolay Borisov | a776c6f | 2017-02-20 13:50:49 +0200 | [diff] [blame] | 8364 | ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start, |
| 8365 | PAGE_SIZE); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8366 | if (ordered) { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 8367 | unlock_extent_cached(io_tree, page_start, page_end, |
David Sterba | e43bbe5 | 2017-12-12 21:43:52 +0100 | [diff] [blame] | 8368 | &cached_state); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8369 | unlock_page(page); |
Nikolay Borisov | c0a4360 | 2020-09-18 12:15:53 +0300 | [diff] [blame] | 8370 | btrfs_start_ordered_extent(ordered, 1); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8371 | btrfs_put_ordered_extent(ordered); |
| 8372 | goto again; |
| 8373 | } |
| 8374 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 8375 | if (page->index == ((size - 1) >> PAGE_SHIFT)) { |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 8376 | reserved_space = round_up(size - page_start, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8377 | fs_info->sectorsize); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 8378 | if (reserved_space < PAGE_SIZE) { |
Chandan Rajendra | d0b7da88 | 2016-01-21 15:55:57 +0530 | [diff] [blame] | 8379 | end = page_start + reserved_space - 1; |
Nikolay Borisov | 86d5292 | 2020-06-03 08:55:40 +0300 | [diff] [blame] | 8380 | btrfs_delalloc_release_space(BTRFS_I(inode), |
| 8381 | data_reserved, page_start, |
| 8382 | PAGE_SIZE - reserved_space, true); |
Chandan Rajendra | d0b7da88 | 2016-01-21 15:55:57 +0530 | [diff] [blame] | 8383 | } |
| 8384 | } |
| 8385 | |
Josef Bacik | fbf1908 | 2009-10-01 17:10:23 -0400 | [diff] [blame] | 8386 | /* |
Liu Bo | 5416034 | 2016-12-13 12:15:19 -0800 | [diff] [blame] | 8387 | * page_mkwrite gets called when the page is firstly dirtied after it's |
| 8388 | * faulted in, but write(2) could also dirty a page and set delalloc |
| 8389 | * bits, thus in this case for space account reason, we still need to |
| 8390 | * clear any delalloc bits within this page range since we have to |
| 8391 | * reserve data&meta space before lock_page() (see above comments). |
Josef Bacik | fbf1908 | 2009-10-01 17:10:23 -0400 | [diff] [blame] | 8392 | */ |
Chandan Rajendra | d0b7da88 | 2016-01-21 15:55:57 +0530 | [diff] [blame] | 8393 | clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, end, |
Omar Sandoval | e182163 | 2019-08-15 14:04:04 -0700 | [diff] [blame] | 8394 | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING | |
| 8395 | EXTENT_DEFRAG, 0, 0, &cached_state); |
Josef Bacik | fbf1908 | 2009-10-01 17:10:23 -0400 | [diff] [blame] | 8396 | |
Nikolay Borisov | c2566f2 | 2020-06-03 08:55:35 +0300 | [diff] [blame] | 8397 | ret2 = btrfs_set_extent_delalloc(BTRFS_I(inode), page_start, end, 0, |
Nikolay Borisov | 330a582 | 2019-07-17 16:18:17 +0300 | [diff] [blame] | 8398 | &cached_state); |
Souptick Joarder | a528a24 | 2018-06-06 19:54:44 +0530 | [diff] [blame] | 8399 | if (ret2) { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 8400 | unlock_extent_cached(io_tree, page_start, page_end, |
David Sterba | e43bbe5 | 2017-12-12 21:43:52 +0100 | [diff] [blame] | 8401 | &cached_state); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 8402 | ret = VM_FAULT_SIGBUS; |
| 8403 | goto out_unlock; |
| 8404 | } |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8405 | |
| 8406 | /* page is wholly or partially inside EOF */ |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 8407 | if (page_start + PAGE_SIZE > size) |
Johannes Thumshirn | 7073017 | 2018-12-05 15:23:03 +0100 | [diff] [blame] | 8408 | zero_start = offset_in_page(size); |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8409 | else |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 8410 | zero_start = PAGE_SIZE; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8411 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 8412 | if (zero_start != PAGE_SIZE) { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8413 | kaddr = kmap(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 8414 | memset(kaddr + zero_start, 0, PAGE_SIZE - zero_start); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8415 | flush_dcache_page(page); |
| 8416 | kunmap(page); |
| 8417 | } |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 8418 | ClearPageChecked(page); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8419 | set_page_dirty(page); |
Chris Mason | 50a9b21 | 2009-09-11 12:33:12 -0400 | [diff] [blame] | 8420 | SetPageUptodate(page); |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 8421 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8422 | BTRFS_I(inode)->last_trans = fs_info->generation; |
Chris Mason | 257c62e | 2009-10-13 13:21:08 -0400 | [diff] [blame] | 8423 | BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid; |
Liu Bo | 46d8bc3 | 2012-08-29 01:07:55 -0600 | [diff] [blame] | 8424 | 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] | 8425 | |
David Sterba | e43bbe5 | 2017-12-12 21:43:52 +0100 | [diff] [blame] | 8426 | unlock_extent_cached(io_tree, page_start, page_end, &cached_state); |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8427 | |
Yunfeng Ye | 76de60e | 2019-12-03 16:59:25 +0800 | [diff] [blame] | 8428 | btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE); |
| 8429 | sb_end_pagefault(inode->i_sb); |
| 8430 | extent_changeset_free(data_reserved); |
| 8431 | return VM_FAULT_LOCKED; |
Chris Mason | 717beb9 | 2018-06-25 10:03:41 -0700 | [diff] [blame] | 8432 | |
| 8433 | out_unlock: |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8434 | unlock_page(page); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 8435 | out: |
Qu Wenruo | 8702ba9 | 2019-10-14 14:34:51 +0800 | [diff] [blame] | 8436 | btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE); |
Nikolay Borisov | 86d5292 | 2020-06-03 08:55:40 +0300 | [diff] [blame] | 8437 | btrfs_delalloc_release_space(BTRFS_I(inode), data_reserved, page_start, |
Qu Wenruo | 43b1859 | 2017-12-12 15:34:32 +0800 | [diff] [blame] | 8438 | reserved_space, (ret != 0)); |
Chris Mason | 9998eb7 | 2012-01-25 13:47:40 -0500 | [diff] [blame] | 8439 | out_noreserve: |
Jan Kara | b2b5ef5 | 2012-06-12 16:20:45 +0200 | [diff] [blame] | 8440 | sb_end_pagefault(inode->i_sb); |
Qu Wenruo | 364ecf3 | 2017-02-27 15:10:38 +0800 | [diff] [blame] | 8441 | extent_changeset_free(data_reserved); |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8442 | return ret; |
| 8443 | } |
| 8444 | |
Filipe Manana | 213e8c5 | 2018-02-06 20:40:31 +0000 | [diff] [blame] | 8445 | static int btrfs_truncate(struct inode *inode, bool skip_writeback) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8446 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8447 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8448 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 8449 | struct btrfs_block_rsv *rsv; |
Omar Sandoval | ad7e1a7 | 2018-05-22 09:59:50 -0700 | [diff] [blame] | 8450 | int ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8451 | struct btrfs_trans_handle *trans; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8452 | u64 mask = fs_info->sectorsize - 1; |
Josef Bacik | 2bd36e7 | 2019-08-22 15:14:33 -0400 | [diff] [blame] | 8453 | u64 min_size = btrfs_calc_metadata_size(fs_info, 1); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8454 | |
Filipe Manana | 213e8c5 | 2018-02-06 20:40:31 +0000 | [diff] [blame] | 8455 | if (!skip_writeback) { |
| 8456 | ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask), |
| 8457 | (u64)-1); |
| 8458 | if (ret) |
| 8459 | return ret; |
| 8460 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8461 | |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 8462 | /* |
Omar Sandoval | f7e9e8f | 2018-05-11 13:13:32 -0700 | [diff] [blame] | 8463 | * Yes ladies and gentlemen, this is indeed ugly. We have a couple of |
| 8464 | * things going on here: |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 8465 | * |
Omar Sandoval | f7e9e8f | 2018-05-11 13:13:32 -0700 | [diff] [blame] | 8466 | * 1) We need to reserve space to update our inode. |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 8467 | * |
Omar Sandoval | f7e9e8f | 2018-05-11 13:13:32 -0700 | [diff] [blame] | 8468 | * 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] | 8469 | * be free'd up by the truncate operation, but also have some slack |
| 8470 | * space reserved in case it uses space during the truncate (thank you |
| 8471 | * very much snapshotting). |
| 8472 | * |
Omar Sandoval | f7e9e8f | 2018-05-11 13:13:32 -0700 | [diff] [blame] | 8473 | * 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] | 8474 | * 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] | 8475 | * 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] | 8476 | * doesn't end up using space reserved for updating the inode. We also |
| 8477 | * need to be able to stop the transaction and start a new one, which |
| 8478 | * means we need to be able to update the inode several times, and we |
| 8479 | * have no idea of knowing how many times that will be, so we can't just |
| 8480 | * reserve 1 item for the entirety of the operation, so that has to be |
| 8481 | * done separately as well. |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 8482 | * |
| 8483 | * So that leaves us with |
| 8484 | * |
Omar Sandoval | f7e9e8f | 2018-05-11 13:13:32 -0700 | [diff] [blame] | 8485 | * 1) rsv - for the truncate reservation, which we will steal from the |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 8486 | * transaction reservation. |
Omar Sandoval | f7e9e8f | 2018-05-11 13:13:32 -0700 | [diff] [blame] | 8487 | * 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] | 8488 | * updating the inode. |
| 8489 | */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 8490 | rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP); |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 8491 | if (!rsv) |
| 8492 | return -ENOMEM; |
Josef Bacik | 4a33854 | 2011-08-29 11:01:31 -0400 | [diff] [blame] | 8493 | rsv->size = min_size; |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 8494 | rsv->failfast = 1; |
Josef Bacik | f0cd846 | 2011-03-04 14:37:08 -0500 | [diff] [blame] | 8495 | |
Josef Bacik | 907cbce | 2011-08-08 13:46:15 -0400 | [diff] [blame] | 8496 | /* |
Josef Bacik | 0712718 | 2011-08-19 10:29:59 -0400 | [diff] [blame] | 8497 | * 1 for the truncate slack space |
Josef Bacik | 907cbce | 2011-08-08 13:46:15 -0400 | [diff] [blame] | 8498 | * 1 for updating the inode. |
| 8499 | */ |
Josef Bacik | f3fe820 | 2013-01-07 17:03:21 -0500 | [diff] [blame] | 8500 | trans = btrfs_start_transaction(root, 2); |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 8501 | if (IS_ERR(trans)) { |
Omar Sandoval | ad7e1a7 | 2018-05-22 09:59:50 -0700 | [diff] [blame] | 8502 | ret = PTR_ERR(trans); |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 8503 | goto out; |
| 8504 | } |
Josef Bacik | f0cd846 | 2011-03-04 14:37:08 -0500 | [diff] [blame] | 8505 | |
Josef Bacik | 907cbce | 2011-08-08 13:46:15 -0400 | [diff] [blame] | 8506 | /* Migrate the slack space for the truncate to our reserve */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8507 | ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, rsv, |
Lu Fengqi | 3a58417 | 2018-08-04 21:10:55 +0800 | [diff] [blame] | 8508 | min_size, false); |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 8509 | BUG_ON(ret); |
Josef Bacik | f0cd846 | 2011-03-04 14:37:08 -0500 | [diff] [blame] | 8510 | |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 8511 | /* |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 8512 | * So if we truncate and then write and fsync we normally would just |
| 8513 | * write the extents that changed, which is a problem if we need to |
| 8514 | * first truncate that entire inode. So set this flag so we write out |
| 8515 | * all of the extents in the inode to the sync log so we're completely |
| 8516 | * safe. |
| 8517 | */ |
| 8518 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags); |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 8519 | trans->block_rsv = rsv; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 8520 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 8521 | while (1) { |
Nikolay Borisov | 5074339 | 2020-11-02 16:48:55 +0200 | [diff] [blame] | 8522 | ret = btrfs_truncate_inode_items(trans, root, BTRFS_I(inode), |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 8523 | inode->i_size, |
| 8524 | BTRFS_EXTENT_DATA_KEY); |
Josef Bacik | ddfae63 | 2017-10-19 14:16:02 -0400 | [diff] [blame] | 8525 | trans->block_rsv = &fs_info->trans_block_rsv; |
Omar Sandoval | ad7e1a7 | 2018-05-22 09:59:50 -0700 | [diff] [blame] | 8526 | if (ret != -ENOSPC && ret != -EAGAIN) |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 8527 | break; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8528 | |
Nikolay Borisov | 9a56fcd | 2020-11-02 16:48:59 +0200 | [diff] [blame] | 8529 | ret = btrfs_update_inode(trans, root, BTRFS_I(inode)); |
Omar Sandoval | ad7e1a7 | 2018-05-22 09:59:50 -0700 | [diff] [blame] | 8530 | if (ret) |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 8531 | break; |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 8532 | |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 8533 | btrfs_end_transaction(trans); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 8534 | btrfs_btree_balance_dirty(fs_info); |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 8535 | |
| 8536 | trans = btrfs_start_transaction(root, 2); |
| 8537 | if (IS_ERR(trans)) { |
Omar Sandoval | ad7e1a7 | 2018-05-22 09:59:50 -0700 | [diff] [blame] | 8538 | ret = PTR_ERR(trans); |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 8539 | trans = NULL; |
| 8540 | break; |
| 8541 | } |
| 8542 | |
Nikolay Borisov | 63f018b | 2020-03-10 10:59:31 +0200 | [diff] [blame] | 8543 | btrfs_block_rsv_release(fs_info, rsv, -1, NULL); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8544 | ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, |
Lu Fengqi | 3a58417 | 2018-08-04 21:10:55 +0800 | [diff] [blame] | 8545 | rsv, min_size, false); |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 8546 | BUG_ON(ret); /* shouldn't happen */ |
| 8547 | trans->block_rsv = rsv; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 8548 | } |
| 8549 | |
Josef Bacik | ddfae63 | 2017-10-19 14:16:02 -0400 | [diff] [blame] | 8550 | /* |
| 8551 | * We can't call btrfs_truncate_block inside a trans handle as we could |
| 8552 | * deadlock with freeze, if we got NEED_TRUNCATE_BLOCK then we know |
| 8553 | * we've truncated everything except the last little bit, and can do |
| 8554 | * btrfs_truncate_block and then update the disk_i_size. |
| 8555 | */ |
| 8556 | if (ret == NEED_TRUNCATE_BLOCK) { |
| 8557 | btrfs_end_transaction(trans); |
| 8558 | btrfs_btree_balance_dirty(fs_info); |
| 8559 | |
Nikolay Borisov | 217f42e | 2020-11-02 16:49:03 +0200 | [diff] [blame] | 8560 | ret = btrfs_truncate_block(BTRFS_I(inode), inode->i_size, 0, 0); |
Josef Bacik | ddfae63 | 2017-10-19 14:16:02 -0400 | [diff] [blame] | 8561 | if (ret) |
| 8562 | goto out; |
| 8563 | trans = btrfs_start_transaction(root, 1); |
| 8564 | if (IS_ERR(trans)) { |
| 8565 | ret = PTR_ERR(trans); |
| 8566 | goto out; |
| 8567 | } |
Nikolay Borisov | 76aea53 | 2020-11-02 16:48:53 +0200 | [diff] [blame] | 8568 | btrfs_inode_safe_disk_i_size_write(BTRFS_I(inode), 0); |
Josef Bacik | ddfae63 | 2017-10-19 14:16:02 -0400 | [diff] [blame] | 8569 | } |
| 8570 | |
Chris Mason | 917c16b | 2011-11-08 14:49:59 -0500 | [diff] [blame] | 8571 | if (trans) { |
Omar Sandoval | ad7e1a7 | 2018-05-22 09:59:50 -0700 | [diff] [blame] | 8572 | int ret2; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 8573 | |
Omar Sandoval | ad7e1a7 | 2018-05-22 09:59:50 -0700 | [diff] [blame] | 8574 | trans->block_rsv = &fs_info->trans_block_rsv; |
Nikolay Borisov | 9a56fcd | 2020-11-02 16:48:59 +0200 | [diff] [blame] | 8575 | ret2 = btrfs_update_inode(trans, root, BTRFS_I(inode)); |
Omar Sandoval | ad7e1a7 | 2018-05-22 09:59:50 -0700 | [diff] [blame] | 8576 | if (ret2 && !ret) |
| 8577 | ret = ret2; |
| 8578 | |
| 8579 | ret2 = btrfs_end_transaction(trans); |
| 8580 | if (ret2 && !ret) |
| 8581 | ret = ret2; |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 8582 | btrfs_btree_balance_dirty(fs_info); |
Chris Mason | 917c16b | 2011-11-08 14:49:59 -0500 | [diff] [blame] | 8583 | } |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 8584 | out: |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 8585 | btrfs_free_block_rsv(fs_info, rsv); |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 8586 | |
Omar Sandoval | ad7e1a7 | 2018-05-22 09:59:50 -0700 | [diff] [blame] | 8587 | return ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8588 | } |
| 8589 | |
Sven Wegener | 3b96362 | 2008-06-09 21:57:42 -0400 | [diff] [blame] | 8590 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 8591 | * create a new subvolume directory/inode (helper for the ioctl). |
| 8592 | */ |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 8593 | int btrfs_create_subvol_root(struct btrfs_trans_handle *trans, |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 8594 | struct btrfs_root *new_root, |
| 8595 | struct btrfs_root *parent_root, |
| 8596 | u64 new_dirid) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8597 | { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8598 | struct inode *inode; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 8599 | int err; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 8600 | u64 index = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8601 | |
Florian Albrechtskirchinger | 12fc9d0 | 2012-02-10 22:15:54 +0100 | [diff] [blame] | 8602 | inode = btrfs_new_inode(trans, new_root, NULL, "..", 2, |
| 8603 | new_dirid, new_dirid, |
| 8604 | S_IFDIR | (~current_umask() & S_IRWXUGO), |
| 8605 | &index); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 8606 | if (IS_ERR(inode)) |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 8607 | return PTR_ERR(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8608 | inode->i_op = &btrfs_dir_inode_operations; |
| 8609 | inode->i_fop = &btrfs_dir_file_operations; |
| 8610 | |
Miklos Szeredi | bfe8684 | 2011-10-28 14:13:29 +0200 | [diff] [blame] | 8611 | set_nlink(inode, 1); |
Nikolay Borisov | 6ef06d2 | 2017-02-20 13:50:34 +0200 | [diff] [blame] | 8612 | btrfs_i_size_write(BTRFS_I(inode), 0); |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 8613 | unlock_new_inode(inode); |
Sven Wegener | 3b96362 | 2008-06-09 21:57:42 -0400 | [diff] [blame] | 8614 | |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 8615 | err = btrfs_subvol_inherit_props(trans, new_root, parent_root); |
| 8616 | if (err) |
| 8617 | btrfs_err(new_root->fs_info, |
David Sterba | 351fd35 | 2014-05-15 16:48:20 +0200 | [diff] [blame] | 8618 | "error inheriting subvolume %llu properties: %d", |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 8619 | new_root->root_key.objectid, err); |
| 8620 | |
Nikolay Borisov | 9a56fcd | 2020-11-02 16:48:59 +0200 | [diff] [blame] | 8621 | err = btrfs_update_inode(trans, new_root, BTRFS_I(inode)); |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 8622 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 8623 | iput(inode); |
Mark Fasheh | ce59897 | 2011-07-26 11:32:23 -0700 | [diff] [blame] | 8624 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8625 | } |
| 8626 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8627 | struct inode *btrfs_alloc_inode(struct super_block *sb) |
| 8628 | { |
Josef Bacik | 69fe2d7 | 2017-10-19 14:15:57 -0400 | [diff] [blame] | 8629 | struct btrfs_fs_info *fs_info = btrfs_sb(sb); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8630 | struct btrfs_inode *ei; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8631 | struct inode *inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8632 | |
David Sterba | 712e36c | 2017-10-31 17:08:27 +0100 | [diff] [blame] | 8633 | ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_KERNEL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8634 | if (!ei) |
| 8635 | return NULL; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8636 | |
| 8637 | ei->root = NULL; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8638 | ei->generation = 0; |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 8639 | ei->last_trans = 0; |
Chris Mason | 257c62e | 2009-10-13 13:21:08 -0400 | [diff] [blame] | 8640 | ei->last_sub_trans = 0; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 8641 | ei->logged_trans = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8642 | ei->delalloc_bytes = 0; |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 8643 | ei->new_delalloc_bytes = 0; |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 8644 | ei->defrag_bytes = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8645 | ei->disk_i_size = 0; |
| 8646 | ei->flags = 0; |
Josef Bacik | 7709cde | 2011-08-04 10:25:02 -0400 | [diff] [blame] | 8647 | ei->csum_bytes = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8648 | ei->index_cnt = (u64)-1; |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 8649 | ei->dir_index = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8650 | ei->last_unlink_trans = 0; |
Filipe Manana | 3ebac17 | 2020-07-15 12:30:43 +0100 | [diff] [blame] | 8651 | ei->last_reflink_trans = 0; |
Liu Bo | 46d8bc3 | 2012-08-29 01:07:55 -0600 | [diff] [blame] | 8652 | ei->last_log_commit = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8653 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 8654 | spin_lock_init(&ei->lock); |
| 8655 | ei->outstanding_extents = 0; |
Josef Bacik | 69fe2d7 | 2017-10-19 14:15:57 -0400 | [diff] [blame] | 8656 | if (sb->s_magic != BTRFS_TEST_MAGIC) |
| 8657 | btrfs_init_metadata_block_rsv(fs_info, &ei->block_rsv, |
| 8658 | BTRFS_BLOCK_RSV_DELALLOC); |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 8659 | ei->runtime_flags = 0; |
David Sterba | b52aa8c | 2017-07-17 19:17:20 +0200 | [diff] [blame] | 8660 | ei->prop_compress = BTRFS_COMPRESS_NONE; |
David Sterba | eec63c6 | 2017-07-17 19:41:31 +0200 | [diff] [blame] | 8661 | ei->defrag_compress = BTRFS_COMPRESS_NONE; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8662 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 8663 | ei->delayed_node = NULL; |
| 8664 | |
chandan r | 9cc97d6 | 2012-07-04 12:48:07 +0530 | [diff] [blame] | 8665 | ei->i_otime.tv_sec = 0; |
| 8666 | ei->i_otime.tv_nsec = 0; |
| 8667 | |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8668 | inode = &ei->vfs_inode; |
David Sterba | a8067e0 | 2011-04-21 00:34:43 +0200 | [diff] [blame] | 8669 | extent_map_tree_init(&ei->extent_tree); |
Qu Wenruo | 43eb5f2 | 2019-03-01 10:47:59 +0800 | [diff] [blame] | 8670 | extent_io_tree_init(fs_info, &ei->io_tree, IO_TREE_INODE_IO, inode); |
| 8671 | extent_io_tree_init(fs_info, &ei->io_failure_tree, |
| 8672 | IO_TREE_INODE_IO_FAILURE, inode); |
Josef Bacik | 41a2ee7 | 2020-01-17 09:02:21 -0500 | [diff] [blame] | 8673 | extent_io_tree_init(fs_info, &ei->file_extent_tree, |
| 8674 | IO_TREE_INODE_FILE_EXTENT, inode); |
David Sterba | 7b43973 | 2019-03-11 15:58:30 +0100 | [diff] [blame] | 8675 | ei->io_tree.track_uptodate = true; |
| 8676 | ei->io_failure_tree.track_uptodate = true; |
Josef Bacik | b812ce2 | 2012-11-16 13:56:32 -0500 | [diff] [blame] | 8677 | atomic_set(&ei->sync_writers, 0); |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8678 | mutex_init(&ei->log_mutex); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8679 | btrfs_ordered_inode_tree_init(&ei->ordered_tree); |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8680 | INIT_LIST_HEAD(&ei->delalloc_inodes); |
David Sterba | 8089fe6 | 2015-11-19 14:15:51 +0100 | [diff] [blame] | 8681 | INIT_LIST_HEAD(&ei->delayed_iput); |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8682 | RB_CLEAR_NODE(&ei->rb_node); |
| 8683 | |
| 8684 | return inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8685 | } |
| 8686 | |
Josef Bacik | aaedb55 | 2013-10-11 14:44:09 -0400 | [diff] [blame] | 8687 | #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS |
| 8688 | void btrfs_test_destroy_inode(struct inode *inode) |
| 8689 | { |
Nikolay Borisov | dcdbc05 | 2017-02-20 13:50:45 +0200 | [diff] [blame] | 8690 | btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0); |
Josef Bacik | aaedb55 | 2013-10-11 14:44:09 -0400 | [diff] [blame] | 8691 | kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode)); |
| 8692 | } |
| 8693 | #endif |
| 8694 | |
Al Viro | 26602ca | 2019-04-10 15:14:41 -0400 | [diff] [blame] | 8695 | void btrfs_free_inode(struct inode *inode) |
Nick Piggin | fa0d7e3d | 2011-01-07 17:49:49 +1100 | [diff] [blame] | 8696 | { |
Nick Piggin | fa0d7e3d | 2011-01-07 17:49:49 +1100 | [diff] [blame] | 8697 | kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode)); |
| 8698 | } |
| 8699 | |
Nikolay Borisov | 633cc81 | 2020-09-18 12:15:49 +0300 | [diff] [blame] | 8700 | void btrfs_destroy_inode(struct inode *vfs_inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8701 | { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8702 | struct btrfs_ordered_extent *ordered; |
Nikolay Borisov | 633cc81 | 2020-09-18 12:15:49 +0300 | [diff] [blame] | 8703 | struct btrfs_inode *inode = BTRFS_I(vfs_inode); |
| 8704 | struct btrfs_root *root = inode->root; |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 8705 | |
Nikolay Borisov | 633cc81 | 2020-09-18 12:15:49 +0300 | [diff] [blame] | 8706 | WARN_ON(!hlist_empty(&vfs_inode->i_dentry)); |
| 8707 | WARN_ON(vfs_inode->i_data.nrpages); |
| 8708 | WARN_ON(inode->block_rsv.reserved); |
| 8709 | WARN_ON(inode->block_rsv.size); |
| 8710 | WARN_ON(inode->outstanding_extents); |
| 8711 | WARN_ON(inode->delalloc_bytes); |
| 8712 | WARN_ON(inode->new_delalloc_bytes); |
| 8713 | WARN_ON(inode->csum_bytes); |
| 8714 | WARN_ON(inode->defrag_bytes); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8715 | |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 8716 | /* |
Josef Bacik | a6dbd42 | 2009-11-11 15:53:34 -0500 | [diff] [blame] | 8717 | * This can happen where we create an inode, but somebody else also |
| 8718 | * created the same inode and we need to destroy the one we already |
| 8719 | * created. |
| 8720 | */ |
| 8721 | if (!root) |
Al Viro | 26602ca | 2019-04-10 15:14:41 -0400 | [diff] [blame] | 8722 | return; |
Josef Bacik | a6dbd42 | 2009-11-11 15:53:34 -0500 | [diff] [blame] | 8723 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 8724 | while (1) { |
Nikolay Borisov | 633cc81 | 2020-09-18 12:15:49 +0300 | [diff] [blame] | 8725 | ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8726 | if (!ordered) |
| 8727 | break; |
| 8728 | else { |
Nikolay Borisov | 633cc81 | 2020-09-18 12:15:49 +0300 | [diff] [blame] | 8729 | btrfs_err(root->fs_info, |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 8730 | "found ordered extent %llu %llu on inode cleanup", |
Omar Sandoval | bffe633 | 2019-12-02 17:34:19 -0800 | [diff] [blame] | 8731 | ordered->file_offset, ordered->num_bytes); |
Nikolay Borisov | 71fe0a5 | 2020-09-18 12:15:50 +0300 | [diff] [blame] | 8732 | btrfs_remove_ordered_extent(inode, ordered); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8733 | btrfs_put_ordered_extent(ordered); |
| 8734 | btrfs_put_ordered_extent(ordered); |
| 8735 | } |
| 8736 | } |
Nikolay Borisov | 633cc81 | 2020-09-18 12:15:49 +0300 | [diff] [blame] | 8737 | btrfs_qgroup_check_reserved_leak(inode); |
| 8738 | inode_tree_del(inode); |
| 8739 | btrfs_drop_extent_cache(inode, 0, (u64)-1, 0); |
| 8740 | btrfs_inode_clear_file_extent_range(inode, 0, (u64)-1); |
| 8741 | btrfs_put_root(inode->root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8742 | } |
| 8743 | |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 8744 | int btrfs_drop_inode(struct inode *inode) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 8745 | { |
| 8746 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 8747 | |
Naohiro Aota | 6379ef9 | 2013-06-06 09:56:34 +0000 | [diff] [blame] | 8748 | if (root == NULL) |
| 8749 | return 1; |
| 8750 | |
Liu Bo | fa6ac87 | 2013-02-20 14:10:23 +0000 | [diff] [blame] | 8751 | /* the snap/subvol tree is on deleting */ |
Stefan Behrens | 69e9c6c | 2013-09-05 16:58:43 +0200 | [diff] [blame] | 8752 | if (btrfs_root_refs(&root->root_item) == 0) |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 8753 | return 1; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 8754 | else |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 8755 | return generic_drop_inode(inode); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 8756 | } |
| 8757 | |
Sven Wegener | 0ee0fda | 2008-07-30 16:54:26 -0400 | [diff] [blame] | 8758 | static void init_once(void *foo) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8759 | { |
| 8760 | struct btrfs_inode *ei = (struct btrfs_inode *) foo; |
| 8761 | |
| 8762 | inode_init_once(&ei->vfs_inode); |
| 8763 | } |
| 8764 | |
David Sterba | e67c718 | 2018-02-19 17:24:18 +0100 | [diff] [blame] | 8765 | void __cold btrfs_destroy_cachep(void) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8766 | { |
Kirill A. Shutemov | 8c0a853 | 2012-09-26 11:33:07 +1000 | [diff] [blame] | 8767 | /* |
| 8768 | * Make sure all delayed rcu free inodes are flushed before we |
| 8769 | * destroy cache. |
| 8770 | */ |
| 8771 | rcu_barrier(); |
Kinglong Mee | 5598e90 | 2016-01-29 21:36:35 +0800 | [diff] [blame] | 8772 | kmem_cache_destroy(btrfs_inode_cachep); |
| 8773 | kmem_cache_destroy(btrfs_trans_handle_cachep); |
Kinglong Mee | 5598e90 | 2016-01-29 21:36:35 +0800 | [diff] [blame] | 8774 | kmem_cache_destroy(btrfs_path_cachep); |
| 8775 | kmem_cache_destroy(btrfs_free_space_cachep); |
Christophe Leroy | 3acd485 | 2019-08-21 15:05:55 +0000 | [diff] [blame] | 8776 | kmem_cache_destroy(btrfs_free_space_bitmap_cachep); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8777 | } |
| 8778 | |
Liu Bo | f5c29bd | 2017-11-02 17:21:50 -0600 | [diff] [blame] | 8779 | int __init btrfs_init_cachep(void) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8780 | { |
David Sterba | 837e197 | 2012-09-07 03:00:48 -0600 | [diff] [blame] | 8781 | btrfs_inode_cachep = kmem_cache_create("btrfs_inode", |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 8782 | sizeof(struct btrfs_inode), 0, |
Vladimir Davydov | 5d09705 | 2016-01-14 15:18:21 -0800 | [diff] [blame] | 8783 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT, |
| 8784 | init_once); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8785 | if (!btrfs_inode_cachep) |
| 8786 | goto fail; |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 8787 | |
David Sterba | 837e197 | 2012-09-07 03:00:48 -0600 | [diff] [blame] | 8788 | btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle", |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 8789 | sizeof(struct btrfs_trans_handle), 0, |
Nikolay Borisov | fba4b69 | 2016-06-23 21:17:08 +0300 | [diff] [blame] | 8790 | SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8791 | if (!btrfs_trans_handle_cachep) |
| 8792 | goto fail; |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 8793 | |
David Sterba | 837e197 | 2012-09-07 03:00:48 -0600 | [diff] [blame] | 8794 | btrfs_path_cachep = kmem_cache_create("btrfs_path", |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 8795 | sizeof(struct btrfs_path), 0, |
Nikolay Borisov | fba4b69 | 2016-06-23 21:17:08 +0300 | [diff] [blame] | 8796 | SLAB_MEM_SPREAD, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8797 | if (!btrfs_path_cachep) |
| 8798 | goto fail; |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 8799 | |
David Sterba | 837e197 | 2012-09-07 03:00:48 -0600 | [diff] [blame] | 8800 | btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space", |
Josef Bacik | dc89e98 | 2011-01-28 17:05:48 -0500 | [diff] [blame] | 8801 | sizeof(struct btrfs_free_space), 0, |
Nikolay Borisov | fba4b69 | 2016-06-23 21:17:08 +0300 | [diff] [blame] | 8802 | SLAB_MEM_SPREAD, NULL); |
Josef Bacik | dc89e98 | 2011-01-28 17:05:48 -0500 | [diff] [blame] | 8803 | if (!btrfs_free_space_cachep) |
| 8804 | goto fail; |
| 8805 | |
Christophe Leroy | 3acd485 | 2019-08-21 15:05:55 +0000 | [diff] [blame] | 8806 | btrfs_free_space_bitmap_cachep = kmem_cache_create("btrfs_free_space_bitmap", |
| 8807 | PAGE_SIZE, PAGE_SIZE, |
| 8808 | SLAB_RED_ZONE, NULL); |
| 8809 | if (!btrfs_free_space_bitmap_cachep) |
| 8810 | goto fail; |
| 8811 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8812 | return 0; |
| 8813 | fail: |
| 8814 | btrfs_destroy_cachep(); |
| 8815 | return -ENOMEM; |
| 8816 | } |
| 8817 | |
David Howells | a528d35 | 2017-01-31 16:46:22 +0000 | [diff] [blame] | 8818 | static int btrfs_getattr(const struct path *path, struct kstat *stat, |
| 8819 | u32 request_mask, unsigned int flags) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8820 | { |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 8821 | u64 delalloc_bytes; |
Filipe Manana | 2766ff6 | 2020-11-04 11:07:34 +0000 | [diff] [blame] | 8822 | u64 inode_bytes; |
David Howells | a528d35 | 2017-01-31 16:46:22 +0000 | [diff] [blame] | 8823 | struct inode *inode = d_inode(path->dentry); |
David Sterba | fadc0d8 | 2011-11-20 07:33:38 -0500 | [diff] [blame] | 8824 | u32 blocksize = inode->i_sb->s_blocksize; |
Yonghong Song | 04a87e3 | 2017-05-12 15:07:43 -0700 | [diff] [blame] | 8825 | u32 bi_flags = BTRFS_I(inode)->flags; |
| 8826 | |
| 8827 | stat->result_mask |= STATX_BTIME; |
| 8828 | stat->btime.tv_sec = BTRFS_I(inode)->i_otime.tv_sec; |
| 8829 | stat->btime.tv_nsec = BTRFS_I(inode)->i_otime.tv_nsec; |
| 8830 | if (bi_flags & BTRFS_INODE_APPEND) |
| 8831 | stat->attributes |= STATX_ATTR_APPEND; |
| 8832 | if (bi_flags & BTRFS_INODE_COMPRESS) |
| 8833 | stat->attributes |= STATX_ATTR_COMPRESSED; |
| 8834 | if (bi_flags & BTRFS_INODE_IMMUTABLE) |
| 8835 | stat->attributes |= STATX_ATTR_IMMUTABLE; |
| 8836 | if (bi_flags & BTRFS_INODE_NODUMP) |
| 8837 | stat->attributes |= STATX_ATTR_NODUMP; |
| 8838 | |
| 8839 | stat->attributes_mask |= (STATX_ATTR_APPEND | |
| 8840 | STATX_ATTR_COMPRESSED | |
| 8841 | STATX_ATTR_IMMUTABLE | |
| 8842 | STATX_ATTR_NODUMP); |
David Sterba | fadc0d8 | 2011-11-20 07:33:38 -0500 | [diff] [blame] | 8843 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8844 | generic_fillattr(inode, stat); |
Al Viro | 0ee5dc6 | 2011-07-07 15:44:25 -0400 | [diff] [blame] | 8845 | stat->dev = BTRFS_I(inode)->root->anon_dev; |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 8846 | |
| 8847 | spin_lock(&BTRFS_I(inode)->lock); |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 8848 | delalloc_bytes = BTRFS_I(inode)->new_delalloc_bytes; |
Filipe Manana | 2766ff6 | 2020-11-04 11:07:34 +0000 | [diff] [blame] | 8849 | inode_bytes = inode_get_bytes(inode); |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 8850 | spin_unlock(&BTRFS_I(inode)->lock); |
Filipe Manana | 2766ff6 | 2020-11-04 11:07:34 +0000 | [diff] [blame] | 8851 | stat->blocks = (ALIGN(inode_bytes, blocksize) + |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 8852 | ALIGN(delalloc_bytes, blocksize)) >> 9; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8853 | return 0; |
| 8854 | } |
| 8855 | |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 8856 | static int btrfs_rename_exchange(struct inode *old_dir, |
| 8857 | struct dentry *old_dentry, |
| 8858 | struct inode *new_dir, |
| 8859 | struct dentry *new_dentry) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8860 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8861 | struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8862 | struct btrfs_trans_handle *trans; |
| 8863 | struct btrfs_root *root = BTRFS_I(old_dir)->root; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 8864 | struct btrfs_root *dest = BTRFS_I(new_dir)->root; |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 8865 | struct inode *new_inode = new_dentry->d_inode; |
| 8866 | struct inode *old_inode = old_dentry->d_inode; |
Deepa Dinamani | 95582b0 | 2018-05-08 19:36:02 -0700 | [diff] [blame] | 8867 | struct timespec64 ctime = current_time(old_inode); |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 8868 | u64 old_ino = btrfs_ino(BTRFS_I(old_inode)); |
| 8869 | u64 new_ino = btrfs_ino(BTRFS_I(new_inode)); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 8870 | u64 old_idx = 0; |
| 8871 | u64 new_idx = 0; |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 8872 | int ret; |
Filipe Manana | 75b463d | 2020-08-11 12:43:48 +0100 | [diff] [blame] | 8873 | int ret2; |
Filipe Manana | 86e8aa0 | 2016-05-05 02:02:27 +0100 | [diff] [blame] | 8874 | bool root_log_pinned = false; |
| 8875 | bool dest_log_pinned = false; |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 8876 | |
| 8877 | /* we only allow rename subvolume link between subvolumes */ |
| 8878 | if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest) |
| 8879 | return -EXDEV; |
| 8880 | |
| 8881 | /* close the race window with snapshot create/destroy ioctl */ |
Josef Bacik | 943eb3b | 2019-11-19 13:59:20 -0500 | [diff] [blame] | 8882 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID || |
| 8883 | new_ino == BTRFS_FIRST_FREE_OBJECTID) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8884 | down_read(&fs_info->subvol_sem); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 8885 | |
| 8886 | /* |
| 8887 | * We want to reserve the absolute worst case amount of items. So if |
| 8888 | * both inodes are subvols and we need to unlink them then that would |
| 8889 | * require 4 item modifications, but if they are both normal inodes it |
| 8890 | * would require 5 item modifications, so we'll assume their normal |
| 8891 | * inodes. So 5 * 2 is 10, plus 2 for the new links, so 12 total items |
| 8892 | * should cover the worst case number of items we'll modify. |
| 8893 | */ |
| 8894 | trans = btrfs_start_transaction(root, 12); |
| 8895 | if (IS_ERR(trans)) { |
| 8896 | ret = PTR_ERR(trans); |
| 8897 | goto out_notrans; |
| 8898 | } |
| 8899 | |
Josef Bacik | 3e17409 | 2019-11-15 15:43:06 -0500 | [diff] [blame] | 8900 | if (dest != root) |
| 8901 | btrfs_record_root_in_trans(trans, dest); |
| 8902 | |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 8903 | /* |
| 8904 | * We need to find a free sequence number both in the source and |
| 8905 | * in the destination directory for the exchange. |
| 8906 | */ |
Nikolay Borisov | 877574e | 2017-02-20 13:50:33 +0200 | [diff] [blame] | 8907 | ret = btrfs_set_inode_index(BTRFS_I(new_dir), &old_idx); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 8908 | if (ret) |
| 8909 | goto out_fail; |
Nikolay Borisov | 877574e | 2017-02-20 13:50:33 +0200 | [diff] [blame] | 8910 | ret = btrfs_set_inode_index(BTRFS_I(old_dir), &new_idx); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 8911 | if (ret) |
| 8912 | goto out_fail; |
| 8913 | |
| 8914 | BTRFS_I(old_inode)->dir_index = 0ULL; |
| 8915 | BTRFS_I(new_inode)->dir_index = 0ULL; |
| 8916 | |
| 8917 | /* Reference for the source. */ |
| 8918 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) { |
| 8919 | /* force full log commit if subvolume involved. */ |
David Sterba | 9078776 | 2019-03-20 13:28:05 +0100 | [diff] [blame] | 8920 | btrfs_set_log_full_commit(trans); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 8921 | } else { |
Filipe Manana | 376e5a5 | 2016-05-05 02:08:56 +0100 | [diff] [blame] | 8922 | btrfs_pin_log_trans(root); |
| 8923 | root_log_pinned = true; |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 8924 | ret = btrfs_insert_inode_ref(trans, dest, |
| 8925 | new_dentry->d_name.name, |
| 8926 | new_dentry->d_name.len, |
| 8927 | old_ino, |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 8928 | btrfs_ino(BTRFS_I(new_dir)), |
| 8929 | old_idx); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 8930 | if (ret) |
| 8931 | goto out_fail; |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 8932 | } |
| 8933 | |
| 8934 | /* And now for the dest. */ |
| 8935 | if (new_ino == BTRFS_FIRST_FREE_OBJECTID) { |
| 8936 | /* force full log commit if subvolume involved. */ |
David Sterba | 9078776 | 2019-03-20 13:28:05 +0100 | [diff] [blame] | 8937 | btrfs_set_log_full_commit(trans); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 8938 | } else { |
Filipe Manana | 376e5a5 | 2016-05-05 02:08:56 +0100 | [diff] [blame] | 8939 | btrfs_pin_log_trans(dest); |
| 8940 | dest_log_pinned = true; |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 8941 | ret = btrfs_insert_inode_ref(trans, root, |
| 8942 | old_dentry->d_name.name, |
| 8943 | old_dentry->d_name.len, |
| 8944 | new_ino, |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 8945 | btrfs_ino(BTRFS_I(old_dir)), |
| 8946 | new_idx); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 8947 | if (ret) |
| 8948 | goto out_fail; |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 8949 | } |
| 8950 | |
| 8951 | /* Update inode version and ctime/mtime. */ |
| 8952 | inode_inc_iversion(old_dir); |
| 8953 | inode_inc_iversion(new_dir); |
| 8954 | inode_inc_iversion(old_inode); |
| 8955 | inode_inc_iversion(new_inode); |
| 8956 | old_dir->i_ctime = old_dir->i_mtime = ctime; |
| 8957 | new_dir->i_ctime = new_dir->i_mtime = ctime; |
| 8958 | old_inode->i_ctime = ctime; |
| 8959 | new_inode->i_ctime = ctime; |
| 8960 | |
| 8961 | if (old_dentry->d_parent != new_dentry->d_parent) { |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 8962 | btrfs_record_unlink_dir(trans, BTRFS_I(old_dir), |
| 8963 | BTRFS_I(old_inode), 1); |
| 8964 | btrfs_record_unlink_dir(trans, BTRFS_I(new_dir), |
| 8965 | BTRFS_I(new_inode), 1); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 8966 | } |
| 8967 | |
| 8968 | /* src is a subvolume */ |
| 8969 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) { |
Josef Bacik | 045d396 | 2019-12-18 17:20:27 -0500 | [diff] [blame] | 8970 | ret = btrfs_unlink_subvol(trans, old_dir, old_dentry); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 8971 | } else { /* src is an inode */ |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 8972 | ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir), |
| 8973 | BTRFS_I(old_dentry->d_inode), |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 8974 | old_dentry->d_name.name, |
| 8975 | old_dentry->d_name.len); |
| 8976 | if (!ret) |
Nikolay Borisov | 9a56fcd | 2020-11-02 16:48:59 +0200 | [diff] [blame] | 8977 | ret = btrfs_update_inode(trans, root, BTRFS_I(old_inode)); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 8978 | } |
| 8979 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 8980 | btrfs_abort_transaction(trans, ret); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 8981 | goto out_fail; |
| 8982 | } |
| 8983 | |
| 8984 | /* dest is a subvolume */ |
| 8985 | if (new_ino == BTRFS_FIRST_FREE_OBJECTID) { |
Josef Bacik | 045d396 | 2019-12-18 17:20:27 -0500 | [diff] [blame] | 8986 | ret = btrfs_unlink_subvol(trans, new_dir, new_dentry); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 8987 | } else { /* dest is an inode */ |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 8988 | ret = __btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir), |
| 8989 | BTRFS_I(new_dentry->d_inode), |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 8990 | new_dentry->d_name.name, |
| 8991 | new_dentry->d_name.len); |
| 8992 | if (!ret) |
Nikolay Borisov | 9a56fcd | 2020-11-02 16:48:59 +0200 | [diff] [blame] | 8993 | ret = btrfs_update_inode(trans, dest, BTRFS_I(new_inode)); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 8994 | } |
| 8995 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 8996 | btrfs_abort_transaction(trans, ret); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 8997 | goto out_fail; |
| 8998 | } |
| 8999 | |
Nikolay Borisov | db0a669 | 2017-02-20 13:51:08 +0200 | [diff] [blame] | 9000 | 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] | 9001 | new_dentry->d_name.name, |
| 9002 | new_dentry->d_name.len, 0, old_idx); |
| 9003 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 9004 | btrfs_abort_transaction(trans, ret); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9005 | goto out_fail; |
| 9006 | } |
| 9007 | |
Nikolay Borisov | db0a669 | 2017-02-20 13:51:08 +0200 | [diff] [blame] | 9008 | 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] | 9009 | old_dentry->d_name.name, |
| 9010 | old_dentry->d_name.len, 0, new_idx); |
| 9011 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 9012 | btrfs_abort_transaction(trans, ret); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9013 | goto out_fail; |
| 9014 | } |
| 9015 | |
| 9016 | if (old_inode->i_nlink == 1) |
| 9017 | BTRFS_I(old_inode)->dir_index = old_idx; |
| 9018 | if (new_inode->i_nlink == 1) |
| 9019 | BTRFS_I(new_inode)->dir_index = new_idx; |
| 9020 | |
Filipe Manana | 86e8aa0 | 2016-05-05 02:02:27 +0100 | [diff] [blame] | 9021 | if (root_log_pinned) { |
Filipe Manana | 75b463d | 2020-08-11 12:43:48 +0100 | [diff] [blame] | 9022 | btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir), |
| 9023 | new_dentry->d_parent); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9024 | btrfs_end_log_trans(root); |
Filipe Manana | 86e8aa0 | 2016-05-05 02:02:27 +0100 | [diff] [blame] | 9025 | root_log_pinned = false; |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9026 | } |
Filipe Manana | 86e8aa0 | 2016-05-05 02:02:27 +0100 | [diff] [blame] | 9027 | if (dest_log_pinned) { |
Filipe Manana | 75b463d | 2020-08-11 12:43:48 +0100 | [diff] [blame] | 9028 | btrfs_log_new_name(trans, BTRFS_I(new_inode), BTRFS_I(new_dir), |
| 9029 | old_dentry->d_parent); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9030 | btrfs_end_log_trans(dest); |
Filipe Manana | 86e8aa0 | 2016-05-05 02:02:27 +0100 | [diff] [blame] | 9031 | dest_log_pinned = false; |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9032 | } |
| 9033 | out_fail: |
Filipe Manana | 86e8aa0 | 2016-05-05 02:02:27 +0100 | [diff] [blame] | 9034 | /* |
| 9035 | * If we have pinned a log and an error happened, we unpin tasks |
| 9036 | * trying to sync the log and force them to fallback to a transaction |
| 9037 | * commit if the log currently contains any of the inodes involved in |
| 9038 | * this rename operation (to ensure we do not persist a log with an |
| 9039 | * inconsistent state for any of these inodes or leading to any |
| 9040 | * inconsistencies when replayed). If the transaction was aborted, the |
| 9041 | * abortion reason is propagated to userspace when attempting to commit |
| 9042 | * the transaction. If the log does not contain any of these inodes, we |
| 9043 | * allow the tasks to sync it. |
| 9044 | */ |
| 9045 | if (ret && (root_log_pinned || dest_log_pinned)) { |
Nikolay Borisov | 0f8939b | 2017-01-18 00:31:30 +0200 | [diff] [blame] | 9046 | if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) || |
| 9047 | btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) || |
| 9048 | btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) || |
Filipe Manana | 86e8aa0 | 2016-05-05 02:02:27 +0100 | [diff] [blame] | 9049 | (new_inode && |
Nikolay Borisov | 0f8939b | 2017-01-18 00:31:30 +0200 | [diff] [blame] | 9050 | btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation))) |
David Sterba | 9078776 | 2019-03-20 13:28:05 +0100 | [diff] [blame] | 9051 | btrfs_set_log_full_commit(trans); |
Filipe Manana | 86e8aa0 | 2016-05-05 02:02:27 +0100 | [diff] [blame] | 9052 | |
| 9053 | if (root_log_pinned) { |
| 9054 | btrfs_end_log_trans(root); |
| 9055 | root_log_pinned = false; |
| 9056 | } |
| 9057 | if (dest_log_pinned) { |
| 9058 | btrfs_end_log_trans(dest); |
| 9059 | dest_log_pinned = false; |
| 9060 | } |
| 9061 | } |
Filipe Manana | 75b463d | 2020-08-11 12:43:48 +0100 | [diff] [blame] | 9062 | ret2 = btrfs_end_transaction(trans); |
| 9063 | ret = ret ? ret : ret2; |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9064 | out_notrans: |
Josef Bacik | 943eb3b | 2019-11-19 13:59:20 -0500 | [diff] [blame] | 9065 | if (new_ino == BTRFS_FIRST_FREE_OBJECTID || |
| 9066 | old_ino == BTRFS_FIRST_FREE_OBJECTID) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9067 | up_read(&fs_info->subvol_sem); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9068 | |
| 9069 | return ret; |
| 9070 | } |
| 9071 | |
| 9072 | static int btrfs_whiteout_for_rename(struct btrfs_trans_handle *trans, |
| 9073 | struct btrfs_root *root, |
| 9074 | struct inode *dir, |
| 9075 | struct dentry *dentry) |
| 9076 | { |
| 9077 | int ret; |
| 9078 | struct inode *inode; |
| 9079 | u64 objectid; |
| 9080 | u64 index; |
| 9081 | |
| 9082 | ret = btrfs_find_free_ino(root, &objectid); |
| 9083 | if (ret) |
| 9084 | return ret; |
| 9085 | |
| 9086 | inode = btrfs_new_inode(trans, root, dir, |
| 9087 | dentry->d_name.name, |
| 9088 | dentry->d_name.len, |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 9089 | btrfs_ino(BTRFS_I(dir)), |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9090 | objectid, |
| 9091 | S_IFCHR | WHITEOUT_MODE, |
| 9092 | &index); |
| 9093 | |
| 9094 | if (IS_ERR(inode)) { |
| 9095 | ret = PTR_ERR(inode); |
| 9096 | return ret; |
| 9097 | } |
| 9098 | |
| 9099 | inode->i_op = &btrfs_special_inode_operations; |
| 9100 | init_special_inode(inode, inode->i_mode, |
| 9101 | WHITEOUT_DEV); |
| 9102 | |
| 9103 | ret = btrfs_init_inode_security(trans, inode, dir, |
| 9104 | &dentry->d_name); |
| 9105 | if (ret) |
Filipe Manana | c990161 | 2016-05-05 01:41:57 +0100 | [diff] [blame] | 9106 | goto out; |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9107 | |
Nikolay Borisov | cef415a | 2017-02-20 13:51:09 +0200 | [diff] [blame] | 9108 | ret = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, |
| 9109 | BTRFS_I(inode), 0, index); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9110 | if (ret) |
Filipe Manana | c990161 | 2016-05-05 01:41:57 +0100 | [diff] [blame] | 9111 | goto out; |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9112 | |
Nikolay Borisov | 9a56fcd | 2020-11-02 16:48:59 +0200 | [diff] [blame] | 9113 | ret = btrfs_update_inode(trans, root, BTRFS_I(inode)); |
Filipe Manana | c990161 | 2016-05-05 01:41:57 +0100 | [diff] [blame] | 9114 | out: |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9115 | unlock_new_inode(inode); |
Filipe Manana | c990161 | 2016-05-05 01:41:57 +0100 | [diff] [blame] | 9116 | if (ret) |
| 9117 | inode_dec_link_count(inode); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9118 | iput(inode); |
| 9119 | |
Filipe Manana | c990161 | 2016-05-05 01:41:57 +0100 | [diff] [blame] | 9120 | return ret; |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9121 | } |
| 9122 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9123 | static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry, |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9124 | struct inode *new_dir, struct dentry *new_dentry, |
| 9125 | unsigned int flags) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9126 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9127 | struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9128 | struct btrfs_trans_handle *trans; |
Filipe Manana | 5062af3 | 2016-05-05 10:26:26 +0100 | [diff] [blame] | 9129 | unsigned int trans_num_items; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9130 | struct btrfs_root *root = BTRFS_I(old_dir)->root; |
| 9131 | struct btrfs_root *dest = BTRFS_I(new_dir)->root; |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 9132 | struct inode *new_inode = d_inode(new_dentry); |
| 9133 | struct inode *old_inode = d_inode(old_dentry); |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 9134 | u64 index = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9135 | int ret; |
Filipe Manana | 75b463d | 2020-08-11 12:43:48 +0100 | [diff] [blame] | 9136 | int ret2; |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 9137 | u64 old_ino = btrfs_ino(BTRFS_I(old_inode)); |
Filipe Manana | 3dc9e8f7 | 2016-04-29 11:34:22 +0100 | [diff] [blame] | 9138 | bool log_pinned = false; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9139 | |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 9140 | if (btrfs_ino(BTRFS_I(new_dir)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) |
Yan, Zheng | f679a84 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 9141 | return -EPERM; |
| 9142 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9143 | /* we only allow rename subvolume link between subvolumes */ |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 9144 | if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest) |
Chris Mason | 3394e16 | 2008-11-17 20:42:26 -0500 | [diff] [blame] | 9145 | return -EXDEV; |
| 9146 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 9147 | if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID || |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 9148 | (new_inode && btrfs_ino(BTRFS_I(new_inode)) == BTRFS_FIRST_FREE_OBJECTID)) |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9149 | return -ENOTEMPTY; |
| 9150 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9151 | if (S_ISDIR(old_inode->i_mode) && new_inode && |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9152 | new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9153 | return -ENOTEMPTY; |
Chris Mason | 9c52057 | 2012-12-17 14:26:57 -0500 | [diff] [blame] | 9154 | |
| 9155 | |
| 9156 | /* check for collisions, even if the name isn't there */ |
Josef Bacik | 4871c15 | 2013-10-09 12:24:04 -0400 | [diff] [blame] | 9157 | ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino, |
Chris Mason | 9c52057 | 2012-12-17 14:26:57 -0500 | [diff] [blame] | 9158 | new_dentry->d_name.name, |
| 9159 | new_dentry->d_name.len); |
| 9160 | |
| 9161 | if (ret) { |
| 9162 | if (ret == -EEXIST) { |
| 9163 | /* we shouldn't get |
| 9164 | * eexist without a new_inode */ |
Dulshani Gunawardhana | fae7f21 | 2013-10-31 10:30:08 +0530 | [diff] [blame] | 9165 | if (WARN_ON(!new_inode)) { |
Chris Mason | 9c52057 | 2012-12-17 14:26:57 -0500 | [diff] [blame] | 9166 | return ret; |
| 9167 | } |
| 9168 | } else { |
| 9169 | /* maybe -EOVERFLOW */ |
| 9170 | return ret; |
| 9171 | } |
| 9172 | } |
| 9173 | ret = 0; |
| 9174 | |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 9175 | /* |
Chris Mason | 8d875f9 | 2014-08-12 10:47:42 -0700 | [diff] [blame] | 9176 | * we're using rename to replace one file with another. Start IO on it |
| 9177 | * 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] | 9178 | */ |
Chris Mason | 8d875f9 | 2014-08-12 10:47:42 -0700 | [diff] [blame] | 9179 | 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] | 9180 | filemap_flush(old_inode->i_mapping); |
| 9181 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 9182 | /* close the racy window with snapshot create/destroy ioctl */ |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 9183 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9184 | down_read(&fs_info->subvol_sem); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 9185 | /* |
| 9186 | * We want to reserve the absolute worst case amount of items. So if |
| 9187 | * both inodes are subvols and we need to unlink them then that would |
| 9188 | * require 4 item modifications, but if they are both normal inodes it |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9189 | * would require 5 item modifications, so we'll assume they are normal |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 9190 | * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items |
| 9191 | * should cover the worst case number of items we'll modify. |
Filipe Manana | 5062af3 | 2016-05-05 10:26:26 +0100 | [diff] [blame] | 9192 | * If our rename has the whiteout flag, we need more 5 units for the |
| 9193 | * new inode (1 inode item, 1 inode ref, 2 dir items and 1 xattr item |
| 9194 | * when selinux is enabled). |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 9195 | */ |
Filipe Manana | 5062af3 | 2016-05-05 10:26:26 +0100 | [diff] [blame] | 9196 | trans_num_items = 11; |
| 9197 | if (flags & RENAME_WHITEOUT) |
| 9198 | trans_num_items += 5; |
| 9199 | trans = btrfs_start_transaction(root, trans_num_items); |
Johann Lombardi | b44c59a | 2011-03-31 13:23:47 +0000 | [diff] [blame] | 9200 | if (IS_ERR(trans)) { |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9201 | ret = PTR_ERR(trans); |
| 9202 | goto out_notrans; |
| 9203 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 9204 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9205 | if (dest != root) |
| 9206 | btrfs_record_root_in_trans(trans, dest); |
| 9207 | |
Nikolay Borisov | 877574e | 2017-02-20 13:50:33 +0200 | [diff] [blame] | 9208 | ret = btrfs_set_inode_index(BTRFS_I(new_dir), &index); |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 9209 | if (ret) |
| 9210 | goto out_fail; |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 9211 | |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 9212 | BTRFS_I(old_inode)->dir_index = 0ULL; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 9213 | if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9214 | /* force full log commit if subvolume involved. */ |
David Sterba | 9078776 | 2019-03-20 13:28:05 +0100 | [diff] [blame] | 9215 | btrfs_set_log_full_commit(trans); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9216 | } else { |
Filipe Manana | c4aba95 | 2016-04-29 13:14:42 +0100 | [diff] [blame] | 9217 | btrfs_pin_log_trans(root); |
| 9218 | log_pinned = true; |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 9219 | ret = btrfs_insert_inode_ref(trans, dest, |
| 9220 | new_dentry->d_name.name, |
| 9221 | new_dentry->d_name.len, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 9222 | old_ino, |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 9223 | btrfs_ino(BTRFS_I(new_dir)), index); |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 9224 | if (ret) |
| 9225 | goto out_fail; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9226 | } |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 9227 | |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 9228 | inode_inc_iversion(old_dir); |
| 9229 | inode_inc_iversion(new_dir); |
| 9230 | inode_inc_iversion(old_inode); |
Deepa Dinamani | 04b285f | 2016-02-06 23:57:21 -0800 | [diff] [blame] | 9231 | old_dir->i_ctime = old_dir->i_mtime = |
| 9232 | new_dir->i_ctime = new_dir->i_mtime = |
Deepa Dinamani | c2050a4 | 2016-09-14 07:48:06 -0700 | [diff] [blame] | 9233 | old_inode->i_ctime = current_time(old_dir); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 9234 | |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 9235 | if (old_dentry->d_parent != new_dentry->d_parent) |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 9236 | btrfs_record_unlink_dir(trans, BTRFS_I(old_dir), |
| 9237 | BTRFS_I(old_inode), 1); |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 9238 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 9239 | if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) { |
Josef Bacik | 045d396 | 2019-12-18 17:20:27 -0500 | [diff] [blame] | 9240 | ret = btrfs_unlink_subvol(trans, old_dir, old_dentry); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9241 | } else { |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 9242 | ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir), |
| 9243 | BTRFS_I(d_inode(old_dentry)), |
Al Viro | 9298679 | 2011-03-04 17:14:37 +0000 | [diff] [blame] | 9244 | old_dentry->d_name.name, |
| 9245 | old_dentry->d_name.len); |
| 9246 | if (!ret) |
Nikolay Borisov | 9a56fcd | 2020-11-02 16:48:59 +0200 | [diff] [blame] | 9247 | ret = btrfs_update_inode(trans, root, BTRFS_I(old_inode)); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9248 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 9249 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 9250 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 9251 | goto out_fail; |
| 9252 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9253 | |
| 9254 | if (new_inode) { |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 9255 | inode_inc_iversion(new_inode); |
Deepa Dinamani | c2050a4 | 2016-09-14 07:48:06 -0700 | [diff] [blame] | 9256 | new_inode->i_ctime = current_time(new_inode); |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 9257 | if (unlikely(btrfs_ino(BTRFS_I(new_inode)) == |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9258 | BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) { |
Josef Bacik | 045d396 | 2019-12-18 17:20:27 -0500 | [diff] [blame] | 9259 | ret = btrfs_unlink_subvol(trans, new_dir, new_dentry); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9260 | BUG_ON(new_inode->i_nlink == 0); |
| 9261 | } else { |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 9262 | ret = btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir), |
| 9263 | BTRFS_I(d_inode(new_dentry)), |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9264 | new_dentry->d_name.name, |
| 9265 | new_dentry->d_name.len); |
| 9266 | } |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 9267 | if (!ret && new_inode->i_nlink == 0) |
Nikolay Borisov | 73f2e54 | 2017-02-20 13:50:59 +0200 | [diff] [blame] | 9268 | ret = btrfs_orphan_add(trans, |
| 9269 | BTRFS_I(d_inode(new_dentry))); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 9270 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 9271 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 9272 | goto out_fail; |
| 9273 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9274 | } |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 9275 | |
Nikolay Borisov | db0a669 | 2017-02-20 13:51:08 +0200 | [diff] [blame] | 9276 | 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] | 9277 | new_dentry->d_name.name, |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 9278 | new_dentry->d_name.len, 0, index); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 9279 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 9280 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 9281 | goto out_fail; |
| 9282 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9283 | |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 9284 | if (old_inode->i_nlink == 1) |
| 9285 | BTRFS_I(old_inode)->dir_index = index; |
| 9286 | |
Filipe Manana | 3dc9e8f7 | 2016-04-29 11:34:22 +0100 | [diff] [blame] | 9287 | if (log_pinned) { |
Filipe Manana | 75b463d | 2020-08-11 12:43:48 +0100 | [diff] [blame] | 9288 | btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir), |
| 9289 | new_dentry->d_parent); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9290 | btrfs_end_log_trans(root); |
Filipe Manana | 3dc9e8f7 | 2016-04-29 11:34:22 +0100 | [diff] [blame] | 9291 | log_pinned = false; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9292 | } |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9293 | |
| 9294 | if (flags & RENAME_WHITEOUT) { |
| 9295 | ret = btrfs_whiteout_for_rename(trans, root, old_dir, |
| 9296 | old_dentry); |
| 9297 | |
| 9298 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 9299 | btrfs_abort_transaction(trans, ret); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9300 | goto out_fail; |
| 9301 | } |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 9302 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9303 | out_fail: |
Filipe Manana | 3dc9e8f7 | 2016-04-29 11:34:22 +0100 | [diff] [blame] | 9304 | /* |
| 9305 | * If we have pinned the log and an error happened, we unpin tasks |
| 9306 | * trying to sync the log and force them to fallback to a transaction |
| 9307 | * commit if the log currently contains any of the inodes involved in |
| 9308 | * this rename operation (to ensure we do not persist a log with an |
| 9309 | * inconsistent state for any of these inodes or leading to any |
| 9310 | * inconsistencies when replayed). If the transaction was aborted, the |
| 9311 | * abortion reason is propagated to userspace when attempting to commit |
| 9312 | * the transaction. If the log does not contain any of these inodes, we |
| 9313 | * allow the tasks to sync it. |
| 9314 | */ |
| 9315 | if (ret && log_pinned) { |
Nikolay Borisov | 0f8939b | 2017-01-18 00:31:30 +0200 | [diff] [blame] | 9316 | if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) || |
| 9317 | btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) || |
| 9318 | btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) || |
Filipe Manana | 3dc9e8f7 | 2016-04-29 11:34:22 +0100 | [diff] [blame] | 9319 | (new_inode && |
Nikolay Borisov | 0f8939b | 2017-01-18 00:31:30 +0200 | [diff] [blame] | 9320 | btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation))) |
David Sterba | 9078776 | 2019-03-20 13:28:05 +0100 | [diff] [blame] | 9321 | btrfs_set_log_full_commit(trans); |
Filipe Manana | 3dc9e8f7 | 2016-04-29 11:34:22 +0100 | [diff] [blame] | 9322 | |
| 9323 | btrfs_end_log_trans(root); |
| 9324 | log_pinned = false; |
| 9325 | } |
Filipe Manana | 75b463d | 2020-08-11 12:43:48 +0100 | [diff] [blame] | 9326 | ret2 = btrfs_end_transaction(trans); |
| 9327 | ret = ret ? ret : ret2; |
Johann Lombardi | b44c59a | 2011-03-31 13:23:47 +0000 | [diff] [blame] | 9328 | out_notrans: |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 9329 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9330 | up_read(&fs_info->subvol_sem); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 9331 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9332 | return ret; |
| 9333 | } |
| 9334 | |
Miklos Szeredi | 80ace85 | 2014-07-23 15:15:32 +0200 | [diff] [blame] | 9335 | static int btrfs_rename2(struct inode *old_dir, struct dentry *old_dentry, |
| 9336 | struct inode *new_dir, struct dentry *new_dentry, |
| 9337 | unsigned int flags) |
| 9338 | { |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9339 | if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT)) |
Miklos Szeredi | 80ace85 | 2014-07-23 15:15:32 +0200 | [diff] [blame] | 9340 | return -EINVAL; |
| 9341 | |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9342 | if (flags & RENAME_EXCHANGE) |
| 9343 | return btrfs_rename_exchange(old_dir, old_dentry, new_dir, |
| 9344 | new_dentry); |
| 9345 | |
| 9346 | return btrfs_rename(old_dir, old_dentry, new_dir, new_dentry, flags); |
Miklos Szeredi | 80ace85 | 2014-07-23 15:15:32 +0200 | [diff] [blame] | 9347 | } |
| 9348 | |
Nikolay Borisov | 3a2f8c0 | 2018-04-24 17:23:59 +0300 | [diff] [blame] | 9349 | struct btrfs_delalloc_work { |
| 9350 | struct inode *inode; |
| 9351 | struct completion completion; |
| 9352 | struct list_head list; |
| 9353 | struct btrfs_work work; |
| 9354 | }; |
| 9355 | |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 9356 | static void btrfs_run_delalloc_work(struct btrfs_work *work) |
| 9357 | { |
| 9358 | struct btrfs_delalloc_work *delalloc_work; |
Josef Bacik | 9f23e28 | 2013-10-28 15:03:41 -0400 | [diff] [blame] | 9359 | struct inode *inode; |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 9360 | |
| 9361 | delalloc_work = container_of(work, struct btrfs_delalloc_work, |
| 9362 | work); |
Josef Bacik | 9f23e28 | 2013-10-28 15:03:41 -0400 | [diff] [blame] | 9363 | inode = delalloc_work->inode; |
David Sterba | 3042460 | 2015-11-27 19:27:11 +0100 | [diff] [blame] | 9364 | filemap_flush(inode->i_mapping); |
| 9365 | if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, |
| 9366 | &BTRFS_I(inode)->runtime_flags)) |
Josef Bacik | 9f23e28 | 2013-10-28 15:03:41 -0400 | [diff] [blame] | 9367 | filemap_flush(inode->i_mapping); |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 9368 | |
Nikolay Borisov | 076da91 | 2018-04-23 10:54:16 +0300 | [diff] [blame] | 9369 | iput(inode); |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 9370 | complete(&delalloc_work->completion); |
| 9371 | } |
| 9372 | |
Nikolay Borisov | 3a2f8c0 | 2018-04-24 17:23:59 +0300 | [diff] [blame] | 9373 | static struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode) |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 9374 | { |
| 9375 | struct btrfs_delalloc_work *work; |
| 9376 | |
David Sterba | 100d570 | 2015-12-08 14:39:32 +0100 | [diff] [blame] | 9377 | work = kmalloc(sizeof(*work), GFP_NOFS); |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 9378 | if (!work) |
| 9379 | return NULL; |
| 9380 | |
| 9381 | init_completion(&work->completion); |
| 9382 | INIT_LIST_HEAD(&work->list); |
| 9383 | work->inode = inode; |
Omar Sandoval | a0cac0e | 2019-09-16 11:30:57 -0700 | [diff] [blame] | 9384 | btrfs_init_work(&work->work, btrfs_run_delalloc_work, NULL, NULL); |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 9385 | |
| 9386 | return work; |
| 9387 | } |
| 9388 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 9389 | /* |
| 9390 | * some fairly slow code that needs optimization. This walks the list |
| 9391 | * of all the inodes with pending delalloc and forces them to disk. |
| 9392 | */ |
Josef Bacik | b491213 | 2020-07-21 10:22:12 -0400 | [diff] [blame] | 9393 | static int start_delalloc_inodes(struct btrfs_root *root, u64 *nr, bool snapshot) |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 9394 | { |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 9395 | struct btrfs_inode *binode; |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 9396 | struct inode *inode; |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 9397 | struct btrfs_delalloc_work *work, *next; |
| 9398 | struct list_head works; |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 9399 | struct list_head splice; |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 9400 | int ret = 0; |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 9401 | |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 9402 | INIT_LIST_HEAD(&works); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 9403 | INIT_LIST_HEAD(&splice); |
Miao Xie | 63607cc | 2013-01-22 10:50:35 +0000 | [diff] [blame] | 9404 | |
Miao Xie | 573bfb7 | 2014-03-06 13:55:03 +0800 | [diff] [blame] | 9405 | mutex_lock(&root->delalloc_mutex); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9406 | spin_lock(&root->delalloc_lock); |
| 9407 | list_splice_init(&root->delalloc_inodes, &splice); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 9408 | while (!list_empty(&splice)) { |
| 9409 | binode = list_entry(splice.next, struct btrfs_inode, |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 9410 | delalloc_inodes); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 9411 | |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9412 | list_move_tail(&binode->delalloc_inodes, |
| 9413 | &root->delalloc_inodes); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 9414 | inode = igrab(&binode->vfs_inode); |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 9415 | if (!inode) { |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9416 | cond_resched_lock(&root->delalloc_lock); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 9417 | continue; |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 9418 | } |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9419 | spin_unlock(&root->delalloc_lock); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 9420 | |
Ethan Lien | 3cd24c6 | 2018-11-01 14:49:03 +0800 | [diff] [blame] | 9421 | if (snapshot) |
| 9422 | set_bit(BTRFS_INODE_SNAPSHOT_FLUSH, |
| 9423 | &binode->runtime_flags); |
Nikolay Borisov | 076da91 | 2018-04-23 10:54:16 +0300 | [diff] [blame] | 9424 | work = btrfs_alloc_delalloc_work(inode); |
David Sterba | 5d99a998 | 2014-09-29 19:20:37 +0200 | [diff] [blame] | 9425 | if (!work) { |
Nikolay Borisov | 4fbb514 | 2018-04-23 10:54:15 +0300 | [diff] [blame] | 9426 | iput(inode); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 9427 | ret = -ENOMEM; |
Wang Shilong | a1ecaab | 2014-04-02 19:53:32 +0800 | [diff] [blame] | 9428 | goto out; |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 9429 | } |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 9430 | list_add_tail(&work->list, &works); |
Qu Wenruo | a44903a | 2014-02-28 10:46:09 +0800 | [diff] [blame] | 9431 | btrfs_queue_work(root->fs_info->flush_workers, |
| 9432 | &work->work); |
Josef Bacik | b491213 | 2020-07-21 10:22:12 -0400 | [diff] [blame] | 9433 | if (*nr != U64_MAX) { |
| 9434 | (*nr)--; |
| 9435 | if (*nr == 0) |
| 9436 | goto out; |
| 9437 | } |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 9438 | cond_resched(); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9439 | spin_lock(&root->delalloc_lock); |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 9440 | } |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9441 | spin_unlock(&root->delalloc_lock); |
Chris Mason | 8c8bee1 | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 9442 | |
Wang Shilong | a1ecaab | 2014-04-02 19:53:32 +0800 | [diff] [blame] | 9443 | out: |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 9444 | list_for_each_entry_safe(work, next, &works, list) { |
| 9445 | list_del_init(&work->list); |
Nikolay Borisov | 40012f9 | 2018-04-19 10:46:39 +0300 | [diff] [blame] | 9446 | wait_for_completion(&work->completion); |
| 9447 | kfree(work); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 9448 | } |
| 9449 | |
Nikolay Borisov | 81f1d39 | 2018-04-19 10:46:37 +0300 | [diff] [blame] | 9450 | if (!list_empty(&splice)) { |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9451 | spin_lock(&root->delalloc_lock); |
| 9452 | list_splice_tail(&splice, &root->delalloc_inodes); |
| 9453 | spin_unlock(&root->delalloc_lock); |
| 9454 | } |
Miao Xie | 573bfb7 | 2014-03-06 13:55:03 +0800 | [diff] [blame] | 9455 | mutex_unlock(&root->delalloc_mutex); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9456 | return ret; |
| 9457 | } |
| 9458 | |
Ethan Lien | 3cd24c6 | 2018-11-01 14:49:03 +0800 | [diff] [blame] | 9459 | int btrfs_start_delalloc_snapshot(struct btrfs_root *root) |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9460 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9461 | struct btrfs_fs_info *fs_info = root->fs_info; |
Josef Bacik | b491213 | 2020-07-21 10:22:12 -0400 | [diff] [blame] | 9462 | u64 nr = U64_MAX; |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9463 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9464 | if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9465 | return -EROFS; |
| 9466 | |
Josef Bacik | b491213 | 2020-07-21 10:22:12 -0400 | [diff] [blame] | 9467 | return start_delalloc_inodes(root, &nr, true); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9468 | } |
| 9469 | |
Josef Bacik | b491213 | 2020-07-21 10:22:12 -0400 | [diff] [blame] | 9470 | int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, u64 nr) |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9471 | { |
| 9472 | struct btrfs_root *root; |
| 9473 | struct list_head splice; |
| 9474 | int ret; |
| 9475 | |
Wang Shilong | 2c21b4d | 2014-01-14 19:42:20 +0800 | [diff] [blame] | 9476 | if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9477 | return -EROFS; |
| 9478 | |
| 9479 | INIT_LIST_HEAD(&splice); |
| 9480 | |
Miao Xie | 573bfb7 | 2014-03-06 13:55:03 +0800 | [diff] [blame] | 9481 | mutex_lock(&fs_info->delalloc_root_mutex); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9482 | spin_lock(&fs_info->delalloc_root_lock); |
| 9483 | list_splice_init(&fs_info->delalloc_roots, &splice); |
Miao Xie | 6c255e6 | 2014-03-06 13:55:01 +0800 | [diff] [blame] | 9484 | while (!list_empty(&splice) && nr) { |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9485 | root = list_first_entry(&splice, struct btrfs_root, |
| 9486 | delalloc_root); |
Josef Bacik | 0024652 | 2020-01-24 09:33:01 -0500 | [diff] [blame] | 9487 | root = btrfs_grab_root(root); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9488 | BUG_ON(!root); |
| 9489 | list_move_tail(&root->delalloc_root, |
| 9490 | &fs_info->delalloc_roots); |
| 9491 | spin_unlock(&fs_info->delalloc_root_lock); |
| 9492 | |
Josef Bacik | b491213 | 2020-07-21 10:22:12 -0400 | [diff] [blame] | 9493 | ret = start_delalloc_inodes(root, &nr, false); |
Josef Bacik | 0024652 | 2020-01-24 09:33:01 -0500 | [diff] [blame] | 9494 | btrfs_put_root(root); |
Miao Xie | 6c255e6 | 2014-03-06 13:55:01 +0800 | [diff] [blame] | 9495 | if (ret < 0) |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9496 | goto out; |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9497 | spin_lock(&fs_info->delalloc_root_lock); |
| 9498 | } |
| 9499 | spin_unlock(&fs_info->delalloc_root_lock); |
| 9500 | |
Miao Xie | 6c255e6 | 2014-03-06 13:55:01 +0800 | [diff] [blame] | 9501 | ret = 0; |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 9502 | out: |
Nikolay Borisov | 81f1d39 | 2018-04-19 10:46:37 +0300 | [diff] [blame] | 9503 | if (!list_empty(&splice)) { |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9504 | spin_lock(&fs_info->delalloc_root_lock); |
| 9505 | list_splice_tail(&splice, &fs_info->delalloc_roots); |
| 9506 | spin_unlock(&fs_info->delalloc_root_lock); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 9507 | } |
Miao Xie | 573bfb7 | 2014-03-06 13:55:03 +0800 | [diff] [blame] | 9508 | mutex_unlock(&fs_info->delalloc_root_mutex); |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 9509 | return ret; |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 9510 | } |
| 9511 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9512 | static int btrfs_symlink(struct inode *dir, struct dentry *dentry, |
| 9513 | const char *symname) |
| 9514 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9515 | struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9516 | struct btrfs_trans_handle *trans; |
| 9517 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 9518 | struct btrfs_path *path; |
| 9519 | struct btrfs_key key; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 9520 | struct inode *inode = NULL; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9521 | int err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9522 | u64 objectid; |
Dulshani Gunawardhana | 6787125 | 2013-10-31 10:33:04 +0530 | [diff] [blame] | 9523 | u64 index = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9524 | int name_len; |
| 9525 | int datasize; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 9526 | unsigned long ptr; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9527 | struct btrfs_file_extent_item *ei; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 9528 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9529 | |
Filipe David Borba Manana | f06becc | 2013-09-16 09:53:28 +0100 | [diff] [blame] | 9530 | name_len = strlen(symname); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9531 | if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info)) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9532 | return -ENAMETOOLONG; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 9533 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 9534 | /* |
| 9535 | * 2 items for inode item and ref |
| 9536 | * 2 items for dir items |
Filipe Manana | 9269d12 | 2015-12-31 18:16:29 +0000 | [diff] [blame] | 9537 | * 1 item for updating parent inode item |
| 9538 | * 1 item for the inline extent item |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 9539 | * 1 item for xattr if selinux is on |
| 9540 | */ |
Filipe Manana | 9269d12 | 2015-12-31 18:16:29 +0000 | [diff] [blame] | 9541 | trans = btrfs_start_transaction(root, 7); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 9542 | if (IS_ERR(trans)) |
| 9543 | return PTR_ERR(trans); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 9544 | |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 9545 | err = btrfs_find_free_ino(root, &objectid); |
| 9546 | if (err) |
| 9547 | goto out_unlock; |
| 9548 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 9549 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 9550 | dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), |
| 9551 | objectid, S_IFLNK|S_IRWXUGO, &index); |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 9552 | if (IS_ERR(inode)) { |
| 9553 | err = PTR_ERR(inode); |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 9554 | inode = NULL; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9555 | goto out_unlock; |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 9556 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9557 | |
Casey Schaufler | ad19db7 | 2011-12-15 10:09:07 -0500 | [diff] [blame] | 9558 | /* |
| 9559 | * If the active LSM wants to access the inode during |
| 9560 | * d_instantiate it needs these. Smack checks to see |
| 9561 | * if the filesystem supports xattrs by looking at the |
| 9562 | * ops vector. |
| 9563 | */ |
| 9564 | inode->i_fop = &btrfs_file_operations; |
| 9565 | inode->i_op = &btrfs_file_inode_operations; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 9566 | inode->i_mapping->a_ops = &btrfs_aops; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 9567 | |
| 9568 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); |
| 9569 | if (err) |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 9570 | goto out_unlock; |
Casey Schaufler | ad19db7 | 2011-12-15 10:09:07 -0500 | [diff] [blame] | 9571 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9572 | path = btrfs_alloc_path(); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 9573 | if (!path) { |
| 9574 | err = -ENOMEM; |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 9575 | goto out_unlock; |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 9576 | } |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 9577 | key.objectid = btrfs_ino(BTRFS_I(inode)); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9578 | key.offset = 0; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 9579 | key.type = BTRFS_EXTENT_DATA_KEY; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9580 | datasize = btrfs_file_extent_calc_inline_size(name_len); |
| 9581 | err = btrfs_insert_empty_item(trans, root, path, &key, |
| 9582 | datasize); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 9583 | if (err) { |
Julia Lawall | b083916 | 2011-05-14 07:10:51 +0000 | [diff] [blame] | 9584 | btrfs_free_path(path); |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 9585 | goto out_unlock; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 9586 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 9587 | leaf = path->nodes[0]; |
| 9588 | ei = btrfs_item_ptr(leaf, path->slots[0], |
| 9589 | struct btrfs_file_extent_item); |
| 9590 | btrfs_set_file_extent_generation(leaf, ei, trans->transid); |
| 9591 | btrfs_set_file_extent_type(leaf, ei, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9592 | BTRFS_FILE_EXTENT_INLINE); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 9593 | btrfs_set_file_extent_encryption(leaf, ei, 0); |
| 9594 | btrfs_set_file_extent_compression(leaf, ei, 0); |
| 9595 | btrfs_set_file_extent_other_encoding(leaf, ei, 0); |
| 9596 | btrfs_set_file_extent_ram_bytes(leaf, ei, name_len); |
| 9597 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9598 | ptr = btrfs_file_extent_inline_start(ei); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 9599 | write_extent_buffer(leaf, symname, ptr, name_len); |
| 9600 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9601 | btrfs_free_path(path); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 9602 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9603 | inode->i_op = &btrfs_symlink_inode_operations; |
Al Viro | 21fc61c | 2015-11-17 01:07:57 -0500 | [diff] [blame] | 9604 | inode_nohighmem(inode); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 9605 | inode_set_bytes(inode, name_len); |
Nikolay Borisov | 6ef06d2 | 2017-02-20 13:50:34 +0200 | [diff] [blame] | 9606 | btrfs_i_size_write(BTRFS_I(inode), name_len); |
Nikolay Borisov | 9a56fcd | 2020-11-02 16:48:59 +0200 | [diff] [blame] | 9607 | err = btrfs_update_inode(trans, root, BTRFS_I(inode)); |
Filipe Manana | d50866d | 2015-12-31 18:08:24 +0000 | [diff] [blame] | 9608 | /* |
| 9609 | * Last step, add directory indexes for our symlink inode. This is the |
| 9610 | * last step to avoid extra cleanup of these indexes if an error happens |
| 9611 | * elsewhere above. |
| 9612 | */ |
| 9613 | if (!err) |
Nikolay Borisov | cef415a | 2017-02-20 13:51:09 +0200 | [diff] [blame] | 9614 | err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, |
| 9615 | BTRFS_I(inode), 0, index); |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 9616 | if (err) |
| 9617 | goto out_unlock; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 9618 | |
Al Viro | 1e2e547 | 2018-05-04 08:23:01 -0400 | [diff] [blame] | 9619 | d_instantiate_new(dentry, inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9620 | |
| 9621 | out_unlock: |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 9622 | btrfs_end_transaction(trans); |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 9623 | if (err && inode) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9624 | inode_dec_link_count(inode); |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 9625 | discard_new_inode(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9626 | } |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 9627 | btrfs_btree_balance_dirty(fs_info); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9628 | return err; |
| 9629 | } |
Chris Mason | 1643298 | 2008-04-10 10:23:21 -0400 | [diff] [blame] | 9630 | |
Filipe Manana | 8fccebf | 2020-09-08 11:27:20 +0100 | [diff] [blame] | 9631 | static struct btrfs_trans_handle *insert_prealloc_file_extent( |
| 9632 | struct btrfs_trans_handle *trans_in, |
Nikolay Borisov | 90dffd0 | 2020-11-02 16:48:54 +0200 | [diff] [blame] | 9633 | struct btrfs_inode *inode, |
| 9634 | struct btrfs_key *ins, |
Qu Wenruo | 203f44c5 | 2020-06-10 09:04:40 +0800 | [diff] [blame] | 9635 | u64 file_offset) |
| 9636 | { |
| 9637 | struct btrfs_file_extent_item stack_fi; |
Filipe Manana | bf38564 | 2020-09-08 11:27:22 +0100 | [diff] [blame] | 9638 | struct btrfs_replace_extent_info extent_info; |
Filipe Manana | 8fccebf | 2020-09-08 11:27:20 +0100 | [diff] [blame] | 9639 | struct btrfs_trans_handle *trans = trans_in; |
| 9640 | struct btrfs_path *path; |
Qu Wenruo | 203f44c5 | 2020-06-10 09:04:40 +0800 | [diff] [blame] | 9641 | u64 start = ins->objectid; |
| 9642 | u64 len = ins->offset; |
Qu Wenruo | 9729f10 | 2020-06-10 09:04:41 +0800 | [diff] [blame] | 9643 | int ret; |
Qu Wenruo | 203f44c5 | 2020-06-10 09:04:40 +0800 | [diff] [blame] | 9644 | |
| 9645 | memset(&stack_fi, 0, sizeof(stack_fi)); |
| 9646 | |
| 9647 | btrfs_set_stack_file_extent_type(&stack_fi, BTRFS_FILE_EXTENT_PREALLOC); |
| 9648 | btrfs_set_stack_file_extent_disk_bytenr(&stack_fi, start); |
| 9649 | btrfs_set_stack_file_extent_disk_num_bytes(&stack_fi, len); |
| 9650 | btrfs_set_stack_file_extent_num_bytes(&stack_fi, len); |
| 9651 | btrfs_set_stack_file_extent_ram_bytes(&stack_fi, len); |
| 9652 | btrfs_set_stack_file_extent_compression(&stack_fi, BTRFS_COMPRESS_NONE); |
| 9653 | /* Encryption and other encoding is reserved and all 0 */ |
| 9654 | |
Nikolay Borisov | 90dffd0 | 2020-11-02 16:48:54 +0200 | [diff] [blame] | 9655 | ret = btrfs_qgroup_release_data(inode, file_offset, len); |
Qu Wenruo | 9729f10 | 2020-06-10 09:04:41 +0800 | [diff] [blame] | 9656 | if (ret < 0) |
Filipe Manana | 8fccebf | 2020-09-08 11:27:20 +0100 | [diff] [blame] | 9657 | return ERR_PTR(ret); |
| 9658 | |
| 9659 | if (trans) { |
Nikolay Borisov | 90dffd0 | 2020-11-02 16:48:54 +0200 | [diff] [blame] | 9660 | ret = insert_reserved_file_extent(trans, inode, |
Filipe Manana | 2766ff6 | 2020-11-04 11:07:34 +0000 | [diff] [blame] | 9661 | file_offset, &stack_fi, |
| 9662 | true, ret); |
Filipe Manana | 8fccebf | 2020-09-08 11:27:20 +0100 | [diff] [blame] | 9663 | if (ret) |
| 9664 | return ERR_PTR(ret); |
| 9665 | return trans; |
| 9666 | } |
| 9667 | |
| 9668 | extent_info.disk_offset = start; |
| 9669 | extent_info.disk_len = len; |
| 9670 | extent_info.data_offset = 0; |
| 9671 | extent_info.data_len = len; |
| 9672 | extent_info.file_offset = file_offset; |
| 9673 | extent_info.extent_buf = (char *)&stack_fi; |
Filipe Manana | 8fccebf | 2020-09-08 11:27:20 +0100 | [diff] [blame] | 9674 | extent_info.is_new_extent = true; |
| 9675 | extent_info.qgroup_reserved = ret; |
| 9676 | extent_info.insertions = 0; |
| 9677 | |
| 9678 | path = btrfs_alloc_path(); |
| 9679 | if (!path) |
| 9680 | return ERR_PTR(-ENOMEM); |
| 9681 | |
Nikolay Borisov | 90dffd0 | 2020-11-02 16:48:54 +0200 | [diff] [blame] | 9682 | ret = btrfs_replace_file_extents(&inode->vfs_inode, path, file_offset, |
Filipe Manana | 8fccebf | 2020-09-08 11:27:20 +0100 | [diff] [blame] | 9683 | file_offset + len - 1, &extent_info, |
| 9684 | &trans); |
| 9685 | btrfs_free_path(path); |
| 9686 | if (ret) |
| 9687 | return ERR_PTR(ret); |
| 9688 | |
| 9689 | return trans; |
Qu Wenruo | 203f44c5 | 2020-06-10 09:04:40 +0800 | [diff] [blame] | 9690 | } |
Filipe Manana | 8fccebf | 2020-09-08 11:27:20 +0100 | [diff] [blame] | 9691 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 9692 | static int __btrfs_prealloc_file_range(struct inode *inode, int mode, |
| 9693 | u64 start, u64 num_bytes, u64 min_size, |
| 9694 | loff_t actual_len, u64 *alloc_hint, |
| 9695 | struct btrfs_trans_handle *trans) |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 9696 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9697 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 9698 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
| 9699 | struct extent_map *em; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 9700 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 9701 | struct btrfs_key ins; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 9702 | u64 cur_offset = start; |
Josef Bacik | b778cf9 | 2020-02-13 10:47:31 -0500 | [diff] [blame] | 9703 | u64 clear_offset = start; |
Josef Bacik | 55a61d1 | 2010-11-22 18:50:32 +0000 | [diff] [blame] | 9704 | u64 i_size; |
Chris Mason | 154ea28 | 2013-03-05 11:11:26 -0500 | [diff] [blame] | 9705 | u64 cur_bytes; |
Josef Bacik | 0b670dc | 2015-09-23 17:11:16 -0400 | [diff] [blame] | 9706 | u64 last_alloc = (u64)-1; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 9707 | int ret = 0; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 9708 | bool own_trans = true; |
Wang Xiaoguang | 1851309 | 2016-07-25 15:51:40 +0800 | [diff] [blame] | 9709 | u64 end = start + num_bytes - 1; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 9710 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 9711 | if (trans) |
| 9712 | own_trans = false; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 9713 | while (num_bytes > 0) { |
Byongho Lee | ee22184 | 2015-12-15 01:42:10 +0900 | [diff] [blame] | 9714 | cur_bytes = min_t(u64, num_bytes, SZ_256M); |
Chris Mason | 154ea28 | 2013-03-05 11:11:26 -0500 | [diff] [blame] | 9715 | cur_bytes = max(cur_bytes, min_size); |
Josef Bacik | 0b670dc | 2015-09-23 17:11:16 -0400 | [diff] [blame] | 9716 | /* |
| 9717 | * If we are severely fragmented we could end up with really |
| 9718 | * small allocations, so if the allocator is returning small |
| 9719 | * chunks lets make its job easier by only searching for those |
| 9720 | * sized chunks. |
| 9721 | */ |
| 9722 | cur_bytes = min(cur_bytes, last_alloc); |
Wang Xiaoguang | 1851309 | 2016-07-25 15:51:40 +0800 | [diff] [blame] | 9723 | ret = btrfs_reserve_extent(root, cur_bytes, cur_bytes, |
| 9724 | min_size, 0, *alloc_hint, &ins, 1, 0); |
Filipe Manana | 8fccebf | 2020-09-08 11:27:20 +0100 | [diff] [blame] | 9725 | if (ret) |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 9726 | break; |
Josef Bacik | b778cf9 | 2020-02-13 10:47:31 -0500 | [diff] [blame] | 9727 | |
| 9728 | /* |
| 9729 | * We've reserved this space, and thus converted it from |
| 9730 | * ->bytes_may_use to ->bytes_reserved. Any error that happens |
| 9731 | * from here on out we will only need to clear our reservation |
| 9732 | * for the remaining unreserved area, so advance our |
| 9733 | * clear_offset by our extent size. |
| 9734 | */ |
| 9735 | clear_offset += ins.offset; |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 9736 | |
Josef Bacik | 0b670dc | 2015-09-23 17:11:16 -0400 | [diff] [blame] | 9737 | last_alloc = ins.offset; |
Nikolay Borisov | 90dffd0 | 2020-11-02 16:48:54 +0200 | [diff] [blame] | 9738 | trans = insert_prealloc_file_extent(trans, BTRFS_I(inode), |
| 9739 | &ins, cur_offset); |
Filipe Manana | 1afc708 | 2020-10-14 10:10:36 +0100 | [diff] [blame] | 9740 | /* |
| 9741 | * Now that we inserted the prealloc extent we can finally |
| 9742 | * decrement the number of reservations in the block group. |
| 9743 | * If we did it before, we could race with relocation and have |
| 9744 | * relocation miss the reserved extent, making it fail later. |
| 9745 | */ |
| 9746 | btrfs_dec_block_group_reservations(fs_info, ins.objectid); |
Filipe Manana | 8fccebf | 2020-09-08 11:27:20 +0100 | [diff] [blame] | 9747 | if (IS_ERR(trans)) { |
| 9748 | ret = PTR_ERR(trans); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 9749 | btrfs_free_reserved_extent(fs_info, ins.objectid, |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 9750 | ins.offset, 0); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 9751 | break; |
| 9752 | } |
Dongsheng Yang | 3119321 | 2014-12-12 16:44:35 +0800 | [diff] [blame] | 9753 | |
Nikolay Borisov | dcdbc05 | 2017-02-20 13:50:45 +0200 | [diff] [blame] | 9754 | btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset, |
Chris Mason | a1ed835 | 2009-09-11 12:27:37 -0400 | [diff] [blame] | 9755 | cur_offset + ins.offset -1, 0); |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 9756 | |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 9757 | em = alloc_extent_map(); |
| 9758 | if (!em) { |
| 9759 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, |
| 9760 | &BTRFS_I(inode)->runtime_flags); |
| 9761 | goto next; |
| 9762 | } |
| 9763 | |
| 9764 | em->start = cur_offset; |
| 9765 | em->orig_start = cur_offset; |
| 9766 | em->len = ins.offset; |
| 9767 | em->block_start = ins.objectid; |
| 9768 | em->block_len = ins.offset; |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 9769 | em->orig_block_len = ins.offset; |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 9770 | em->ram_bytes = ins.offset; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 9771 | set_bit(EXTENT_FLAG_PREALLOC, &em->flags); |
| 9772 | em->generation = trans->transid; |
| 9773 | |
| 9774 | while (1) { |
| 9775 | write_lock(&em_tree->lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 9776 | ret = add_extent_mapping(em_tree, em, 1); |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 9777 | write_unlock(&em_tree->lock); |
| 9778 | if (ret != -EEXIST) |
| 9779 | break; |
Nikolay Borisov | dcdbc05 | 2017-02-20 13:50:45 +0200 | [diff] [blame] | 9780 | btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset, |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 9781 | cur_offset + ins.offset - 1, |
| 9782 | 0); |
| 9783 | } |
| 9784 | free_extent_map(em); |
| 9785 | next: |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 9786 | num_bytes -= ins.offset; |
| 9787 | cur_offset += ins.offset; |
Yan, Zheng | efa5646 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 9788 | *alloc_hint = ins.objectid + ins.offset; |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 9789 | |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 9790 | inode_inc_iversion(inode); |
Deepa Dinamani | c2050a4 | 2016-09-14 07:48:06 -0700 | [diff] [blame] | 9791 | inode->i_ctime = current_time(inode); |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 9792 | BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 9793 | if (!(mode & FALLOC_FL_KEEP_SIZE) && |
Yan, Zheng | efa5646 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 9794 | (actual_len > inode->i_size) && |
| 9795 | (cur_offset > inode->i_size)) { |
Aneesh Kumar K.V | d1ea6a6 | 2010-01-20 07:28:54 +0000 | [diff] [blame] | 9796 | if (cur_offset > actual_len) |
Josef Bacik | 55a61d1 | 2010-11-22 18:50:32 +0000 | [diff] [blame] | 9797 | i_size = actual_len; |
Aneesh Kumar K.V | d1ea6a6 | 2010-01-20 07:28:54 +0000 | [diff] [blame] | 9798 | else |
Josef Bacik | 55a61d1 | 2010-11-22 18:50:32 +0000 | [diff] [blame] | 9799 | i_size = cur_offset; |
| 9800 | i_size_write(inode, i_size); |
Nikolay Borisov | 76aea53 | 2020-11-02 16:48:53 +0200 | [diff] [blame] | 9801 | btrfs_inode_safe_disk_i_size_write(BTRFS_I(inode), 0); |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 9802 | } |
| 9803 | |
Nikolay Borisov | 9a56fcd | 2020-11-02 16:48:59 +0200 | [diff] [blame] | 9804 | ret = btrfs_update_inode(trans, root, BTRFS_I(inode)); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 9805 | |
| 9806 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 9807 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 9808 | if (own_trans) |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 9809 | btrfs_end_transaction(trans); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 9810 | break; |
| 9811 | } |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 9812 | |
Filipe Manana | 8fccebf | 2020-09-08 11:27:20 +0100 | [diff] [blame] | 9813 | if (own_trans) { |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 9814 | btrfs_end_transaction(trans); |
Filipe Manana | 8fccebf | 2020-09-08 11:27:20 +0100 | [diff] [blame] | 9815 | trans = NULL; |
| 9816 | } |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 9817 | } |
Josef Bacik | b778cf9 | 2020-02-13 10:47:31 -0500 | [diff] [blame] | 9818 | if (clear_offset < end) |
Nikolay Borisov | 25ce28c | 2020-06-03 08:55:39 +0300 | [diff] [blame] | 9819 | btrfs_free_reserved_data_space(BTRFS_I(inode), NULL, clear_offset, |
Josef Bacik | b778cf9 | 2020-02-13 10:47:31 -0500 | [diff] [blame] | 9820 | end - clear_offset + 1); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 9821 | return ret; |
| 9822 | } |
| 9823 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 9824 | int btrfs_prealloc_file_range(struct inode *inode, int mode, |
| 9825 | u64 start, u64 num_bytes, u64 min_size, |
| 9826 | loff_t actual_len, u64 *alloc_hint) |
| 9827 | { |
| 9828 | return __btrfs_prealloc_file_range(inode, mode, start, num_bytes, |
| 9829 | min_size, actual_len, alloc_hint, |
| 9830 | NULL); |
| 9831 | } |
| 9832 | |
| 9833 | int btrfs_prealloc_file_range_trans(struct inode *inode, |
| 9834 | struct btrfs_trans_handle *trans, int mode, |
| 9835 | u64 start, u64 num_bytes, u64 min_size, |
| 9836 | loff_t actual_len, u64 *alloc_hint) |
| 9837 | { |
| 9838 | return __btrfs_prealloc_file_range(inode, mode, start, num_bytes, |
| 9839 | min_size, actual_len, alloc_hint, trans); |
| 9840 | } |
| 9841 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 9842 | static int btrfs_set_page_dirty(struct page *page) |
| 9843 | { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 9844 | return __set_page_dirty_nobuffers(page); |
| 9845 | } |
| 9846 | |
Al Viro | 10556cb2 | 2011-06-20 19:28:19 -0400 | [diff] [blame] | 9847 | static int btrfs_permission(struct inode *inode, int mask) |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 9848 | { |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 9849 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Jeff Mahoney | cb6db4e | 2011-08-15 17:27:21 +0000 | [diff] [blame] | 9850 | umode_t mode = inode->i_mode; |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 9851 | |
Jeff Mahoney | cb6db4e | 2011-08-15 17:27:21 +0000 | [diff] [blame] | 9852 | if (mask & MAY_WRITE && |
| 9853 | (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) { |
| 9854 | if (btrfs_root_readonly(root)) |
| 9855 | return -EROFS; |
| 9856 | if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY) |
| 9857 | return -EACCES; |
| 9858 | } |
Al Viro | 2830ba7 | 2011-06-20 19:16:29 -0400 | [diff] [blame] | 9859 | return generic_permission(inode, mask); |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 9860 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9861 | |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 9862 | static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode) |
| 9863 | { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 9864 | struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 9865 | struct btrfs_trans_handle *trans; |
| 9866 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 9867 | struct inode *inode = NULL; |
| 9868 | u64 objectid; |
| 9869 | u64 index; |
| 9870 | int ret = 0; |
| 9871 | |
| 9872 | /* |
| 9873 | * 5 units required for adding orphan entry |
| 9874 | */ |
| 9875 | trans = btrfs_start_transaction(root, 5); |
| 9876 | if (IS_ERR(trans)) |
| 9877 | return PTR_ERR(trans); |
| 9878 | |
| 9879 | ret = btrfs_find_free_ino(root, &objectid); |
| 9880 | if (ret) |
| 9881 | goto out; |
| 9882 | |
| 9883 | inode = btrfs_new_inode(trans, root, dir, NULL, 0, |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 9884 | btrfs_ino(BTRFS_I(dir)), objectid, mode, &index); |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 9885 | if (IS_ERR(inode)) { |
| 9886 | ret = PTR_ERR(inode); |
| 9887 | inode = NULL; |
| 9888 | goto out; |
| 9889 | } |
| 9890 | |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 9891 | inode->i_fop = &btrfs_file_operations; |
| 9892 | inode->i_op = &btrfs_file_inode_operations; |
| 9893 | |
| 9894 | inode->i_mapping->a_ops = &btrfs_aops; |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 9895 | |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 9896 | ret = btrfs_init_inode_security(trans, inode, dir, NULL); |
| 9897 | if (ret) |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 9898 | goto out; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 9899 | |
Nikolay Borisov | 9a56fcd | 2020-11-02 16:48:59 +0200 | [diff] [blame] | 9900 | ret = btrfs_update_inode(trans, root, BTRFS_I(inode)); |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 9901 | if (ret) |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 9902 | goto out; |
Nikolay Borisov | 73f2e54 | 2017-02-20 13:50:59 +0200 | [diff] [blame] | 9903 | ret = btrfs_orphan_add(trans, BTRFS_I(inode)); |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 9904 | if (ret) |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 9905 | goto out; |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 9906 | |
Filipe Manana | 5762b5c | 2014-08-01 00:10:32 +0100 | [diff] [blame] | 9907 | /* |
| 9908 | * We set number of links to 0 in btrfs_new_inode(), and here we set |
| 9909 | * it to 1 because d_tmpfile() will issue a warning if the count is 0, |
| 9910 | * through: |
| 9911 | * |
| 9912 | * d_tmpfile() -> inode_dec_link_count() -> drop_nlink() |
| 9913 | */ |
| 9914 | set_nlink(inode, 1); |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 9915 | d_tmpfile(dentry, inode); |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 9916 | unlock_new_inode(inode); |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 9917 | mark_inode_dirty(inode); |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 9918 | out: |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 9919 | btrfs_end_transaction(trans); |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 9920 | if (ret && inode) |
| 9921 | discard_new_inode(inode); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 9922 | btrfs_btree_balance_dirty(fs_info); |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 9923 | return ret; |
| 9924 | } |
| 9925 | |
David Sterba | 5cdc84b | 2018-07-18 20:32:52 +0200 | [diff] [blame] | 9926 | 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] | 9927 | { |
David Sterba | 5cdc84b | 2018-07-18 20:32:52 +0200 | [diff] [blame] | 9928 | struct inode *inode = tree->private_data; |
Josef Bacik | c6100a4 | 2017-05-05 11:57:13 -0400 | [diff] [blame] | 9929 | unsigned long index = start >> PAGE_SHIFT; |
| 9930 | unsigned long end_index = end >> PAGE_SHIFT; |
| 9931 | struct page *page; |
| 9932 | |
| 9933 | while (index <= end_index) { |
| 9934 | page = find_get_page(inode->i_mapping, index); |
| 9935 | ASSERT(page); /* Pages should be in the extent_io_tree */ |
| 9936 | set_page_writeback(page); |
| 9937 | put_page(page); |
| 9938 | index++; |
| 9939 | } |
| 9940 | } |
| 9941 | |
Omar Sandoval | ed46ff3 | 2016-11-03 10:28:14 -0700 | [diff] [blame] | 9942 | #ifdef CONFIG_SWAP |
| 9943 | /* |
| 9944 | * Add an entry indicating a block group or device which is pinned by a |
| 9945 | * swapfile. Returns 0 on success, 1 if there is already an entry for it, or a |
| 9946 | * negative errno on failure. |
| 9947 | */ |
| 9948 | static int btrfs_add_swapfile_pin(struct inode *inode, void *ptr, |
| 9949 | bool is_block_group) |
| 9950 | { |
| 9951 | struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info; |
| 9952 | struct btrfs_swapfile_pin *sp, *entry; |
| 9953 | struct rb_node **p; |
| 9954 | struct rb_node *parent = NULL; |
| 9955 | |
| 9956 | sp = kmalloc(sizeof(*sp), GFP_NOFS); |
| 9957 | if (!sp) |
| 9958 | return -ENOMEM; |
| 9959 | sp->ptr = ptr; |
| 9960 | sp->inode = inode; |
| 9961 | sp->is_block_group = is_block_group; |
| 9962 | |
| 9963 | spin_lock(&fs_info->swapfile_pins_lock); |
| 9964 | p = &fs_info->swapfile_pins.rb_node; |
| 9965 | while (*p) { |
| 9966 | parent = *p; |
| 9967 | entry = rb_entry(parent, struct btrfs_swapfile_pin, node); |
| 9968 | if (sp->ptr < entry->ptr || |
| 9969 | (sp->ptr == entry->ptr && sp->inode < entry->inode)) { |
| 9970 | p = &(*p)->rb_left; |
| 9971 | } else if (sp->ptr > entry->ptr || |
| 9972 | (sp->ptr == entry->ptr && sp->inode > entry->inode)) { |
| 9973 | p = &(*p)->rb_right; |
| 9974 | } else { |
| 9975 | spin_unlock(&fs_info->swapfile_pins_lock); |
| 9976 | kfree(sp); |
| 9977 | return 1; |
| 9978 | } |
| 9979 | } |
| 9980 | rb_link_node(&sp->node, parent, p); |
| 9981 | rb_insert_color(&sp->node, &fs_info->swapfile_pins); |
| 9982 | spin_unlock(&fs_info->swapfile_pins_lock); |
| 9983 | return 0; |
| 9984 | } |
| 9985 | |
| 9986 | /* Free all of the entries pinned by this swapfile. */ |
| 9987 | static void btrfs_free_swapfile_pins(struct inode *inode) |
| 9988 | { |
| 9989 | struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info; |
| 9990 | struct btrfs_swapfile_pin *sp; |
| 9991 | struct rb_node *node, *next; |
| 9992 | |
| 9993 | spin_lock(&fs_info->swapfile_pins_lock); |
| 9994 | node = rb_first(&fs_info->swapfile_pins); |
| 9995 | while (node) { |
| 9996 | next = rb_next(node); |
| 9997 | sp = rb_entry(node, struct btrfs_swapfile_pin, node); |
| 9998 | if (sp->inode == inode) { |
| 9999 | rb_erase(&sp->node, &fs_info->swapfile_pins); |
| 10000 | if (sp->is_block_group) |
| 10001 | btrfs_put_block_group(sp->ptr); |
| 10002 | kfree(sp); |
| 10003 | } |
| 10004 | node = next; |
| 10005 | } |
| 10006 | spin_unlock(&fs_info->swapfile_pins_lock); |
| 10007 | } |
| 10008 | |
| 10009 | struct btrfs_swap_info { |
| 10010 | u64 start; |
| 10011 | u64 block_start; |
| 10012 | u64 block_len; |
| 10013 | u64 lowest_ppage; |
| 10014 | u64 highest_ppage; |
| 10015 | unsigned long nr_pages; |
| 10016 | int nr_extents; |
| 10017 | }; |
| 10018 | |
| 10019 | static int btrfs_add_swap_extent(struct swap_info_struct *sis, |
| 10020 | struct btrfs_swap_info *bsi) |
| 10021 | { |
| 10022 | unsigned long nr_pages; |
| 10023 | u64 first_ppage, first_ppage_reported, next_ppage; |
| 10024 | int ret; |
| 10025 | |
| 10026 | first_ppage = ALIGN(bsi->block_start, PAGE_SIZE) >> PAGE_SHIFT; |
| 10027 | next_ppage = ALIGN_DOWN(bsi->block_start + bsi->block_len, |
| 10028 | PAGE_SIZE) >> PAGE_SHIFT; |
| 10029 | |
| 10030 | if (first_ppage >= next_ppage) |
| 10031 | return 0; |
| 10032 | nr_pages = next_ppage - first_ppage; |
| 10033 | |
| 10034 | first_ppage_reported = first_ppage; |
| 10035 | if (bsi->start == 0) |
| 10036 | first_ppage_reported++; |
| 10037 | if (bsi->lowest_ppage > first_ppage_reported) |
| 10038 | bsi->lowest_ppage = first_ppage_reported; |
| 10039 | if (bsi->highest_ppage < (next_ppage - 1)) |
| 10040 | bsi->highest_ppage = next_ppage - 1; |
| 10041 | |
| 10042 | ret = add_swap_extent(sis, bsi->nr_pages, nr_pages, first_ppage); |
| 10043 | if (ret < 0) |
| 10044 | return ret; |
| 10045 | bsi->nr_extents += ret; |
| 10046 | bsi->nr_pages += nr_pages; |
| 10047 | return 0; |
| 10048 | } |
| 10049 | |
| 10050 | static void btrfs_swap_deactivate(struct file *file) |
| 10051 | { |
| 10052 | struct inode *inode = file_inode(file); |
| 10053 | |
| 10054 | btrfs_free_swapfile_pins(inode); |
| 10055 | atomic_dec(&BTRFS_I(inode)->root->nr_swapfiles); |
| 10056 | } |
| 10057 | |
| 10058 | static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file, |
| 10059 | sector_t *span) |
| 10060 | { |
| 10061 | struct inode *inode = file_inode(file); |
| 10062 | struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info; |
| 10063 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
| 10064 | struct extent_state *cached_state = NULL; |
| 10065 | struct extent_map *em = NULL; |
| 10066 | struct btrfs_device *device = NULL; |
| 10067 | struct btrfs_swap_info bsi = { |
| 10068 | .lowest_ppage = (sector_t)-1ULL, |
| 10069 | }; |
| 10070 | int ret = 0; |
| 10071 | u64 isize; |
| 10072 | u64 start; |
| 10073 | |
| 10074 | /* |
| 10075 | * If the swap file was just created, make sure delalloc is done. If the |
| 10076 | * file changes again after this, the user is doing something stupid and |
| 10077 | * we don't really care. |
| 10078 | */ |
| 10079 | ret = btrfs_wait_ordered_range(inode, 0, (u64)-1); |
| 10080 | if (ret) |
| 10081 | return ret; |
| 10082 | |
| 10083 | /* |
| 10084 | * The inode is locked, so these flags won't change after we check them. |
| 10085 | */ |
| 10086 | if (BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS) { |
| 10087 | btrfs_warn(fs_info, "swapfile must not be compressed"); |
| 10088 | return -EINVAL; |
| 10089 | } |
| 10090 | if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW)) { |
| 10091 | btrfs_warn(fs_info, "swapfile must not be copy-on-write"); |
| 10092 | return -EINVAL; |
| 10093 | } |
| 10094 | if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) { |
| 10095 | btrfs_warn(fs_info, "swapfile must not be checksummed"); |
| 10096 | return -EINVAL; |
| 10097 | } |
| 10098 | |
| 10099 | /* |
| 10100 | * Balance or device remove/replace/resize can move stuff around from |
Goldwyn Rodrigues | c3e1f96 | 2020-08-25 10:02:32 -0500 | [diff] [blame] | 10101 | * under us. The exclop protection makes sure they aren't running/won't |
| 10102 | * run concurrently while we are mapping the swap extents, and |
| 10103 | * fs_info->swapfile_pins prevents them from running while the swap |
| 10104 | * file is active and moving the extents. Note that this also prevents |
| 10105 | * 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] | 10106 | * really worth the trouble to allow it. |
| 10107 | */ |
Goldwyn Rodrigues | c3e1f96 | 2020-08-25 10:02:32 -0500 | [diff] [blame] | 10108 | if (!btrfs_exclop_start(fs_info, BTRFS_EXCLOP_SWAP_ACTIVATE)) { |
Omar Sandoval | ed46ff3 | 2016-11-03 10:28:14 -0700 | [diff] [blame] | 10109 | btrfs_warn(fs_info, |
| 10110 | "cannot activate swapfile while exclusive operation is running"); |
| 10111 | return -EBUSY; |
| 10112 | } |
| 10113 | /* |
| 10114 | * Snapshots can create extents which require COW even if NODATACOW is |
| 10115 | * set. We use this counter to prevent snapshots. We must increment it |
| 10116 | * before walking the extents because we don't want a concurrent |
| 10117 | * snapshot to run after we've already checked the extents. |
| 10118 | */ |
| 10119 | atomic_inc(&BTRFS_I(inode)->root->nr_swapfiles); |
| 10120 | |
| 10121 | isize = ALIGN_DOWN(inode->i_size, fs_info->sectorsize); |
| 10122 | |
| 10123 | lock_extent_bits(io_tree, 0, isize - 1, &cached_state); |
| 10124 | start = 0; |
| 10125 | while (start < isize) { |
| 10126 | u64 logical_block_start, physical_block_start; |
David Sterba | 32da5386 | 2019-10-29 19:20:18 +0100 | [diff] [blame] | 10127 | struct btrfs_block_group *bg; |
Omar Sandoval | ed46ff3 | 2016-11-03 10:28:14 -0700 | [diff] [blame] | 10128 | u64 len = isize - start; |
| 10129 | |
Omar Sandoval | 39b07b5 | 2019-12-02 17:34:23 -0800 | [diff] [blame] | 10130 | em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len); |
Omar Sandoval | ed46ff3 | 2016-11-03 10:28:14 -0700 | [diff] [blame] | 10131 | if (IS_ERR(em)) { |
| 10132 | ret = PTR_ERR(em); |
| 10133 | goto out; |
| 10134 | } |
| 10135 | |
| 10136 | if (em->block_start == EXTENT_MAP_HOLE) { |
| 10137 | btrfs_warn(fs_info, "swapfile must not have holes"); |
| 10138 | ret = -EINVAL; |
| 10139 | goto out; |
| 10140 | } |
| 10141 | if (em->block_start == EXTENT_MAP_INLINE) { |
| 10142 | /* |
| 10143 | * It's unlikely we'll ever actually find ourselves |
| 10144 | * here, as a file small enough to fit inline won't be |
| 10145 | * big enough to store more than the swap header, but in |
| 10146 | * case something changes in the future, let's catch it |
| 10147 | * here rather than later. |
| 10148 | */ |
| 10149 | btrfs_warn(fs_info, "swapfile must not be inline"); |
| 10150 | ret = -EINVAL; |
| 10151 | goto out; |
| 10152 | } |
| 10153 | if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) { |
| 10154 | btrfs_warn(fs_info, "swapfile must not be compressed"); |
| 10155 | ret = -EINVAL; |
| 10156 | goto out; |
| 10157 | } |
| 10158 | |
| 10159 | logical_block_start = em->block_start + (start - em->start); |
| 10160 | len = min(len, em->len - (start - em->start)); |
| 10161 | free_extent_map(em); |
| 10162 | em = NULL; |
| 10163 | |
Boris Burkov | a84d5d4 | 2020-08-18 11:00:05 -0700 | [diff] [blame] | 10164 | ret = can_nocow_extent(inode, start, &len, NULL, NULL, NULL, true); |
Omar Sandoval | ed46ff3 | 2016-11-03 10:28:14 -0700 | [diff] [blame] | 10165 | if (ret < 0) { |
| 10166 | goto out; |
| 10167 | } else if (ret) { |
| 10168 | ret = 0; |
| 10169 | } else { |
| 10170 | btrfs_warn(fs_info, |
| 10171 | "swapfile must not be copy-on-write"); |
| 10172 | ret = -EINVAL; |
| 10173 | goto out; |
| 10174 | } |
| 10175 | |
| 10176 | em = btrfs_get_chunk_map(fs_info, logical_block_start, len); |
| 10177 | if (IS_ERR(em)) { |
| 10178 | ret = PTR_ERR(em); |
| 10179 | goto out; |
| 10180 | } |
| 10181 | |
| 10182 | if (em->map_lookup->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) { |
| 10183 | btrfs_warn(fs_info, |
| 10184 | "swapfile must have single data profile"); |
| 10185 | ret = -EINVAL; |
| 10186 | goto out; |
| 10187 | } |
| 10188 | |
| 10189 | if (device == NULL) { |
| 10190 | device = em->map_lookup->stripes[0].dev; |
| 10191 | ret = btrfs_add_swapfile_pin(inode, device, false); |
| 10192 | if (ret == 1) |
| 10193 | ret = 0; |
| 10194 | else if (ret) |
| 10195 | goto out; |
| 10196 | } else if (device != em->map_lookup->stripes[0].dev) { |
| 10197 | btrfs_warn(fs_info, "swapfile must be on one device"); |
| 10198 | ret = -EINVAL; |
| 10199 | goto out; |
| 10200 | } |
| 10201 | |
| 10202 | physical_block_start = (em->map_lookup->stripes[0].physical + |
| 10203 | (logical_block_start - em->start)); |
| 10204 | len = min(len, em->len - (logical_block_start - em->start)); |
| 10205 | free_extent_map(em); |
| 10206 | em = NULL; |
| 10207 | |
| 10208 | bg = btrfs_lookup_block_group(fs_info, logical_block_start); |
| 10209 | if (!bg) { |
| 10210 | btrfs_warn(fs_info, |
| 10211 | "could not find block group containing swapfile"); |
| 10212 | ret = -EINVAL; |
| 10213 | goto out; |
| 10214 | } |
| 10215 | |
| 10216 | ret = btrfs_add_swapfile_pin(inode, bg, true); |
| 10217 | if (ret) { |
| 10218 | btrfs_put_block_group(bg); |
| 10219 | if (ret == 1) |
| 10220 | ret = 0; |
| 10221 | else |
| 10222 | goto out; |
| 10223 | } |
| 10224 | |
| 10225 | if (bsi.block_len && |
| 10226 | bsi.block_start + bsi.block_len == physical_block_start) { |
| 10227 | bsi.block_len += len; |
| 10228 | } else { |
| 10229 | if (bsi.block_len) { |
| 10230 | ret = btrfs_add_swap_extent(sis, &bsi); |
| 10231 | if (ret) |
| 10232 | goto out; |
| 10233 | } |
| 10234 | bsi.start = start; |
| 10235 | bsi.block_start = physical_block_start; |
| 10236 | bsi.block_len = len; |
| 10237 | } |
| 10238 | |
| 10239 | start += len; |
| 10240 | } |
| 10241 | |
| 10242 | if (bsi.block_len) |
| 10243 | ret = btrfs_add_swap_extent(sis, &bsi); |
| 10244 | |
| 10245 | out: |
| 10246 | if (!IS_ERR_OR_NULL(em)) |
| 10247 | free_extent_map(em); |
| 10248 | |
| 10249 | unlock_extent_cached(io_tree, 0, isize - 1, &cached_state); |
| 10250 | |
| 10251 | if (ret) |
| 10252 | btrfs_swap_deactivate(file); |
| 10253 | |
Goldwyn Rodrigues | c3e1f96 | 2020-08-25 10:02:32 -0500 | [diff] [blame] | 10254 | btrfs_exclop_finish(fs_info); |
Omar Sandoval | ed46ff3 | 2016-11-03 10:28:14 -0700 | [diff] [blame] | 10255 | |
| 10256 | if (ret) |
| 10257 | return ret; |
| 10258 | |
| 10259 | if (device) |
| 10260 | sis->bdev = device->bdev; |
| 10261 | *span = bsi.highest_ppage - bsi.lowest_ppage + 1; |
| 10262 | sis->max = bsi.nr_pages; |
| 10263 | sis->pages = bsi.nr_pages - 1; |
| 10264 | sis->highest_bit = bsi.nr_pages - 1; |
| 10265 | return bsi.nr_extents; |
| 10266 | } |
| 10267 | #else |
| 10268 | static void btrfs_swap_deactivate(struct file *file) |
| 10269 | { |
| 10270 | } |
| 10271 | |
| 10272 | static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file, |
| 10273 | sector_t *span) |
| 10274 | { |
| 10275 | return -EOPNOTSUPP; |
| 10276 | } |
| 10277 | #endif |
| 10278 | |
Filipe Manana | 2766ff6 | 2020-11-04 11:07:34 +0000 | [diff] [blame] | 10279 | /* |
| 10280 | * Update the number of bytes used in the VFS' inode. When we replace extents in |
| 10281 | * a range (clone, dedupe, fallocate's zero range), we must update the number of |
| 10282 | * bytes used by the inode in an atomic manner, so that concurrent stat(2) calls |
| 10283 | * always get a correct value. |
| 10284 | */ |
| 10285 | void btrfs_update_inode_bytes(struct btrfs_inode *inode, |
| 10286 | const u64 add_bytes, |
| 10287 | const u64 del_bytes) |
| 10288 | { |
| 10289 | if (add_bytes == del_bytes) |
| 10290 | return; |
| 10291 | |
| 10292 | spin_lock(&inode->lock); |
| 10293 | if (del_bytes > 0) |
| 10294 | inode_sub_bytes(&inode->vfs_inode, del_bytes); |
| 10295 | if (add_bytes > 0) |
| 10296 | inode_add_bytes(&inode->vfs_inode, add_bytes); |
| 10297 | spin_unlock(&inode->lock); |
| 10298 | } |
| 10299 | |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 10300 | static const struct inode_operations btrfs_dir_inode_operations = { |
Chris Mason | 3394e16 | 2008-11-17 20:42:26 -0500 | [diff] [blame] | 10301 | .getattr = btrfs_getattr, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10302 | .lookup = btrfs_lookup, |
| 10303 | .create = btrfs_create, |
| 10304 | .unlink = btrfs_unlink, |
| 10305 | .link = btrfs_link, |
| 10306 | .mkdir = btrfs_mkdir, |
| 10307 | .rmdir = btrfs_rmdir, |
Miklos Szeredi | 2773bf0 | 2016-09-27 11:03:58 +0200 | [diff] [blame] | 10308 | .rename = btrfs_rename2, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10309 | .symlink = btrfs_symlink, |
| 10310 | .setattr = btrfs_setattr, |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 10311 | .mknod = btrfs_mknod, |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 10312 | .listxattr = btrfs_listxattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 10313 | .permission = btrfs_permission, |
Christoph Hellwig | 4e34e71 | 2011-07-23 17:37:31 +0200 | [diff] [blame] | 10314 | .get_acl = btrfs_get_acl, |
Christoph Hellwig | 996a710 | 2013-12-20 05:16:43 -0800 | [diff] [blame] | 10315 | .set_acl = btrfs_set_acl, |
Guangyu Sun | 93fd63c | 2013-09-16 10:42:03 -0700 | [diff] [blame] | 10316 | .update_time = btrfs_update_time, |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 10317 | .tmpfile = btrfs_tmpfile, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10318 | }; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 10319 | |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 10320 | static const struct file_operations btrfs_dir_file_operations = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10321 | .llseek = generic_file_llseek, |
| 10322 | .read = generic_read_dir, |
Omar Sandoval | 02dbfc9 | 2016-05-20 13:50:33 -0700 | [diff] [blame] | 10323 | .iterate_shared = btrfs_real_readdir, |
Josef Bacik | 23b5ec7 | 2017-07-24 15:14:25 -0400 | [diff] [blame] | 10324 | .open = btrfs_opendir, |
Christoph Hellwig | 34287aa | 2007-09-14 10:22:47 -0400 | [diff] [blame] | 10325 | .unlocked_ioctl = btrfs_ioctl, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10326 | #ifdef CONFIG_COMPAT |
Luke Dashjr | 4c63c24 | 2015-10-29 08:22:21 +0000 | [diff] [blame] | 10327 | .compat_ioctl = btrfs_compat_ioctl, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10328 | #endif |
Sage Weil | 6bf13c0 | 2008-06-10 10:07:39 -0400 | [diff] [blame] | 10329 | .release = btrfs_release_file, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 10330 | .fsync = btrfs_sync_file, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10331 | }; |
| 10332 | |
Chris Mason | 3505439 | 2009-01-21 13:11:13 -0500 | [diff] [blame] | 10333 | /* |
| 10334 | * btrfs doesn't support the bmap operation because swapfiles |
| 10335 | * use bmap to make a mapping of extents in the file. They assume |
| 10336 | * these extents won't change over the life of the file and they |
| 10337 | * use the bmap result to do IO directly to the drive. |
| 10338 | * |
| 10339 | * the btrfs bmap call would return logical addresses that aren't |
| 10340 | * suitable for IO and they also will change frequently as COW |
| 10341 | * operations happen. So, swapfile + btrfs == corruption. |
| 10342 | * |
| 10343 | * For now we're avoiding this by dropping bmap. |
| 10344 | */ |
Alexey Dobriyan | 7f09410 | 2009-09-21 17:01:10 -0700 | [diff] [blame] | 10345 | static const struct address_space_operations btrfs_aops = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10346 | .readpage = btrfs_readpage, |
| 10347 | .writepage = btrfs_writepage, |
Chris Mason | b293f02e | 2007-11-01 19:45:34 -0400 | [diff] [blame] | 10348 | .writepages = btrfs_writepages, |
Matthew Wilcox (Oracle) | ba206a0 | 2020-06-01 21:47:05 -0700 | [diff] [blame] | 10349 | .readahead = btrfs_readahead, |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 10350 | .direct_IO = noop_direct_IO, |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 10351 | .invalidatepage = btrfs_invalidatepage, |
| 10352 | .releasepage = btrfs_releasepage, |
Roman Gushchin | f8e6608 | 2020-03-04 16:57:35 -0800 | [diff] [blame] | 10353 | #ifdef CONFIG_MIGRATION |
| 10354 | .migratepage = btrfs_migratepage, |
| 10355 | #endif |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 10356 | .set_page_dirty = btrfs_set_page_dirty, |
Andi Kleen | 465fdd9 | 2009-09-16 11:50:18 +0200 | [diff] [blame] | 10357 | .error_remove_page = generic_error_remove_page, |
Omar Sandoval | ed46ff3 | 2016-11-03 10:28:14 -0700 | [diff] [blame] | 10358 | .swap_activate = btrfs_swap_activate, |
| 10359 | .swap_deactivate = btrfs_swap_deactivate, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10360 | }; |
| 10361 | |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 10362 | static const struct inode_operations btrfs_file_inode_operations = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10363 | .getattr = btrfs_getattr, |
| 10364 | .setattr = btrfs_setattr, |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 10365 | .listxattr = btrfs_listxattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 10366 | .permission = btrfs_permission, |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 10367 | .fiemap = btrfs_fiemap, |
Christoph Hellwig | 4e34e71 | 2011-07-23 17:37:31 +0200 | [diff] [blame] | 10368 | .get_acl = btrfs_get_acl, |
Christoph Hellwig | 996a710 | 2013-12-20 05:16:43 -0800 | [diff] [blame] | 10369 | .set_acl = btrfs_set_acl, |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 10370 | .update_time = btrfs_update_time, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10371 | }; |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 10372 | static const struct inode_operations btrfs_special_inode_operations = { |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 10373 | .getattr = btrfs_getattr, |
| 10374 | .setattr = btrfs_setattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 10375 | .permission = btrfs_permission, |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 10376 | .listxattr = btrfs_listxattr, |
Christoph Hellwig | 4e34e71 | 2011-07-23 17:37:31 +0200 | [diff] [blame] | 10377 | .get_acl = btrfs_get_acl, |
Christoph Hellwig | 996a710 | 2013-12-20 05:16:43 -0800 | [diff] [blame] | 10378 | .set_acl = btrfs_set_acl, |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 10379 | .update_time = btrfs_update_time, |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 10380 | }; |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 10381 | static const struct inode_operations btrfs_symlink_inode_operations = { |
Al Viro | 6b25539 | 2015-11-17 10:20:54 -0500 | [diff] [blame] | 10382 | .get_link = page_get_link, |
Li Zefan | f209561 | 2010-11-19 02:05:24 +0000 | [diff] [blame] | 10383 | .getattr = btrfs_getattr, |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 10384 | .setattr = btrfs_setattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 10385 | .permission = btrfs_permission, |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 10386 | .listxattr = btrfs_listxattr, |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 10387 | .update_time = btrfs_update_time, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10388 | }; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 10389 | |
Alexey Dobriyan | 82d339d | 2009-10-09 09:54:36 -0400 | [diff] [blame] | 10390 | const struct dentry_operations btrfs_dentry_operations = { |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 10391 | .d_delete = btrfs_dentry_delete, |
| 10392 | }; |