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 David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 205 | struct btrfs_path *path, int 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 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 226 | inode_add_bytes(inode, size); |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 227 | |
| 228 | if (!extent_inserted) { |
| 229 | struct btrfs_key key; |
| 230 | size_t datasize; |
| 231 | |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 232 | key.objectid = btrfs_ino(BTRFS_I(inode)); |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 233 | key.offset = start; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 234 | key.type = BTRFS_EXTENT_DATA_KEY; |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 235 | |
| 236 | datasize = btrfs_file_extent_calc_inline_size(cur_size); |
| 237 | path->leave_spinning = 1; |
| 238 | ret = btrfs_insert_empty_item(trans, root, path, &key, |
| 239 | datasize); |
David Sterba | 79b4f4c | 2017-06-15 19:09:51 +0200 | [diff] [blame] | 240 | if (ret) |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 241 | goto fail; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 242 | } |
| 243 | leaf = path->nodes[0]; |
| 244 | ei = btrfs_item_ptr(leaf, path->slots[0], |
| 245 | struct btrfs_file_extent_item); |
| 246 | btrfs_set_file_extent_generation(leaf, ei, trans->transid); |
| 247 | btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE); |
| 248 | btrfs_set_file_extent_encryption(leaf, ei, 0); |
| 249 | btrfs_set_file_extent_other_encoding(leaf, ei, 0); |
| 250 | btrfs_set_file_extent_ram_bytes(leaf, ei, size); |
| 251 | ptr = btrfs_file_extent_inline_start(ei); |
| 252 | |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 253 | if (compress_type != BTRFS_COMPRESS_NONE) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 254 | struct page *cpage; |
| 255 | int i = 0; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 256 | while (compressed_size > 0) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 257 | cpage = compressed_pages[i]; |
Chris Mason | 5b050f0 | 2008-11-11 09:34:41 -0500 | [diff] [blame] | 258 | cur_size = min_t(unsigned long, compressed_size, |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 259 | PAGE_SIZE); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 260 | |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 261 | kaddr = kmap_atomic(cpage); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 262 | write_extent_buffer(leaf, kaddr, ptr, cur_size); |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 263 | kunmap_atomic(kaddr); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 264 | |
| 265 | i++; |
| 266 | ptr += cur_size; |
| 267 | compressed_size -= cur_size; |
| 268 | } |
| 269 | btrfs_set_file_extent_compression(leaf, ei, |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 270 | compress_type); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 271 | } else { |
| 272 | page = find_get_page(inode->i_mapping, |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 273 | start >> PAGE_SHIFT); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 274 | btrfs_set_file_extent_compression(leaf, ei, 0); |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 275 | kaddr = kmap_atomic(page); |
Johannes Thumshirn | 7073017 | 2018-12-05 15:23:03 +0100 | [diff] [blame] | 276 | offset = offset_in_page(start); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 277 | write_extent_buffer(leaf, kaddr + offset, ptr, size); |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 278 | kunmap_atomic(kaddr); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 279 | put_page(page); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 280 | } |
| 281 | btrfs_mark_buffer_dirty(leaf); |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 282 | btrfs_release_path(path); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 283 | |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 284 | /* |
Josef Bacik | 9ddc959 | 2020-01-17 09:02:22 -0500 | [diff] [blame] | 285 | * We align size to sectorsize for inline extents just for simplicity |
| 286 | * sake. |
| 287 | */ |
| 288 | size = ALIGN(size, root->fs_info->sectorsize); |
| 289 | ret = btrfs_inode_set_file_extent_range(BTRFS_I(inode), start, size); |
| 290 | if (ret) |
| 291 | goto fail; |
| 292 | |
| 293 | /* |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 294 | * we're an inline extent, so nobody can |
| 295 | * extend the file past i_size without locking |
| 296 | * a page we already have locked. |
| 297 | * |
| 298 | * We must do any isize and inode updates |
| 299 | * before we unlock the pages. Otherwise we |
| 300 | * could end up racing with unlink. |
| 301 | */ |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 302 | BTRFS_I(inode)->disk_i_size = inode->i_size; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 303 | ret = btrfs_update_inode(trans, root, inode); |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 304 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 305 | fail: |
David Sterba | 79b4f4c | 2017-06-15 19:09:51 +0200 | [diff] [blame] | 306 | return ret; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | |
| 310 | /* |
| 311 | * conditionally insert an inline extent into the file. This |
| 312 | * does the checks required to make sure the data is small enough |
| 313 | * to fit as an inline extent. |
| 314 | */ |
Nikolay Borisov | a034940 | 2020-06-03 08:55:12 +0300 | [diff] [blame] | 315 | 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] | 316 | u64 end, size_t compressed_size, |
| 317 | int compress_type, |
| 318 | struct page **compressed_pages) |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 319 | { |
Nikolay Borisov | a034940 | 2020-06-03 08:55:12 +0300 | [diff] [blame] | 320 | struct btrfs_root *root = inode->root; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 321 | struct btrfs_fs_info *fs_info = root->fs_info; |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 322 | struct btrfs_trans_handle *trans; |
Nikolay Borisov | a034940 | 2020-06-03 08:55:12 +0300 | [diff] [blame] | 323 | u64 isize = i_size_read(&inode->vfs_inode); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 324 | u64 actual_end = min(end + 1, isize); |
| 325 | u64 inline_len = actual_end - start; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 326 | u64 aligned_end = ALIGN(end, fs_info->sectorsize); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 327 | u64 data_len = inline_len; |
| 328 | int ret; |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 329 | struct btrfs_path *path; |
| 330 | int extent_inserted = 0; |
| 331 | u32 extent_item_size; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 332 | |
| 333 | if (compressed_size) |
| 334 | data_len = compressed_size; |
| 335 | |
| 336 | if (start > 0 || |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 337 | actual_end > fs_info->sectorsize || |
| 338 | data_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info) || |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 339 | (!compressed_size && |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 340 | (actual_end & (fs_info->sectorsize - 1)) == 0) || |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 341 | end + 1 < isize || |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 342 | data_len > fs_info->max_inline) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 343 | return 1; |
| 344 | } |
| 345 | |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 346 | path = btrfs_alloc_path(); |
| 347 | if (!path) |
| 348 | return -ENOMEM; |
| 349 | |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 350 | trans = btrfs_join_transaction(root); |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 351 | if (IS_ERR(trans)) { |
| 352 | btrfs_free_path(path); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 353 | return PTR_ERR(trans); |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 354 | } |
Nikolay Borisov | a034940 | 2020-06-03 08:55:12 +0300 | [diff] [blame] | 355 | trans->block_rsv = &inode->block_rsv; |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 356 | |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 357 | if (compressed_size && compressed_pages) |
| 358 | extent_item_size = btrfs_file_extent_calc_inline_size( |
| 359 | compressed_size); |
| 360 | else |
| 361 | extent_item_size = btrfs_file_extent_calc_inline_size( |
| 362 | inline_len); |
| 363 | |
Nikolay Borisov | a034940 | 2020-06-03 08:55:12 +0300 | [diff] [blame] | 364 | ret = __btrfs_drop_extents(trans, root, inode, path, start, aligned_end, |
| 365 | NULL, 1, 1, extent_item_size, |
| 366 | &extent_inserted); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 367 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 368 | btrfs_abort_transaction(trans, ret); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 369 | goto out; |
| 370 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 371 | |
| 372 | if (isize > actual_end) |
| 373 | inline_len = min_t(u64, isize, actual_end); |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 374 | ret = insert_inline_extent(trans, path, extent_inserted, |
Nikolay Borisov | a034940 | 2020-06-03 08:55:12 +0300 | [diff] [blame] | 375 | root, &inode->vfs_inode, start, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 376 | inline_len, compressed_size, |
Li Zefan | fe3f566 | 2011-03-28 08:30:38 +0000 | [diff] [blame] | 377 | compress_type, compressed_pages); |
Josef Bacik | 2adcac1 | 2012-05-23 16:10:14 -0400 | [diff] [blame] | 378 | if (ret && ret != -ENOSPC) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 379 | btrfs_abort_transaction(trans, ret); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 380 | goto out; |
Josef Bacik | 2adcac1 | 2012-05-23 16:10:14 -0400 | [diff] [blame] | 381 | } else if (ret == -ENOSPC) { |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 382 | ret = 1; |
| 383 | goto out; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 384 | } |
Josef Bacik | 2adcac1 | 2012-05-23 16:10:14 -0400 | [diff] [blame] | 385 | |
Nikolay Borisov | a034940 | 2020-06-03 08:55:12 +0300 | [diff] [blame] | 386 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &inode->runtime_flags); |
| 387 | btrfs_drop_extent_cache(inode, start, aligned_end - 1, 0); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 388 | out: |
Qu Wenruo | 94ed938 | 2015-09-08 17:25:56 +0800 | [diff] [blame] | 389 | /* |
| 390 | * Don't forget to free the reserved space, as for inlined extent |
| 391 | * it won't count as data extent, free them directly here. |
| 392 | * And at reserve time, it's always aligned to page size, so |
| 393 | * just free one page here. |
| 394 | */ |
Nikolay Borisov | a034940 | 2020-06-03 08:55:12 +0300 | [diff] [blame] | 395 | btrfs_qgroup_free_data(inode, NULL, 0, PAGE_SIZE); |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 396 | btrfs_free_path(path); |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 397 | btrfs_end_transaction(trans); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 398 | return ret; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 399 | } |
| 400 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 401 | struct async_extent { |
| 402 | u64 start; |
| 403 | u64 ram_size; |
| 404 | u64 compressed_size; |
| 405 | struct page **pages; |
| 406 | unsigned long nr_pages; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 407 | int compress_type; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 408 | struct list_head list; |
| 409 | }; |
| 410 | |
Nikolay Borisov | 97db120 | 2019-03-12 17:20:24 +0200 | [diff] [blame] | 411 | struct async_chunk { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 412 | struct inode *inode; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 413 | struct page *locked_page; |
| 414 | u64 start; |
| 415 | u64 end; |
Liu Bo | f82b735 | 2017-10-23 23:18:16 -0600 | [diff] [blame] | 416 | unsigned int write_flags; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 417 | struct list_head extents; |
Chris Mason | ec39f76 | 2019-07-10 12:28:17 -0700 | [diff] [blame] | 418 | struct cgroup_subsys_state *blkcg_css; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 419 | struct btrfs_work work; |
Nikolay Borisov | 97db120 | 2019-03-12 17:20:24 +0200 | [diff] [blame] | 420 | atomic_t *pending; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 421 | }; |
| 422 | |
Nikolay Borisov | 97db120 | 2019-03-12 17:20:24 +0200 | [diff] [blame] | 423 | struct async_cow { |
| 424 | /* Number of chunks in flight; must be first in the structure */ |
| 425 | atomic_t num_chunks; |
| 426 | struct async_chunk chunks[]; |
| 427 | }; |
| 428 | |
| 429 | static noinline int add_async_extent(struct async_chunk *cow, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 430 | u64 start, u64 ram_size, |
| 431 | u64 compressed_size, |
| 432 | struct page **pages, |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 433 | unsigned long nr_pages, |
| 434 | int compress_type) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 435 | { |
| 436 | struct async_extent *async_extent; |
| 437 | |
| 438 | async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 439 | BUG_ON(!async_extent); /* -ENOMEM */ |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 440 | async_extent->start = start; |
| 441 | async_extent->ram_size = ram_size; |
| 442 | async_extent->compressed_size = compressed_size; |
| 443 | async_extent->pages = pages; |
| 444 | async_extent->nr_pages = nr_pages; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 445 | async_extent->compress_type = compress_type; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 446 | list_add_tail(&async_extent->list, &cow->extents); |
| 447 | return 0; |
| 448 | } |
| 449 | |
Qu Wenruo | 42c16da | 2019-07-01 05:12:46 +0000 | [diff] [blame] | 450 | /* |
| 451 | * Check if the inode has flags compatible with compression |
| 452 | */ |
Nikolay Borisov | 99c88dc | 2020-06-03 08:55:26 +0300 | [diff] [blame] | 453 | static inline bool inode_can_compress(struct btrfs_inode *inode) |
Qu Wenruo | 42c16da | 2019-07-01 05:12:46 +0000 | [diff] [blame] | 454 | { |
Nikolay Borisov | 99c88dc | 2020-06-03 08:55:26 +0300 | [diff] [blame] | 455 | if (inode->flags & BTRFS_INODE_NODATACOW || |
| 456 | inode->flags & BTRFS_INODE_NODATASUM) |
Qu Wenruo | 42c16da | 2019-07-01 05:12:46 +0000 | [diff] [blame] | 457 | return false; |
| 458 | return true; |
| 459 | } |
| 460 | |
| 461 | /* |
| 462 | * Check if the inode needs to be submitted to compression, based on mount |
| 463 | * options, defragmentation, properties or heuristics. |
| 464 | */ |
Nikolay Borisov | 808a129 | 2020-06-03 08:55:27 +0300 | [diff] [blame] | 465 | static inline int inode_need_compress(struct btrfs_inode *inode, u64 start, |
| 466 | u64 end) |
Wang Shilong | f79707b | 2014-07-17 11:44:09 +0800 | [diff] [blame] | 467 | { |
Nikolay Borisov | 808a129 | 2020-06-03 08:55:27 +0300 | [diff] [blame] | 468 | struct btrfs_fs_info *fs_info = inode->root->fs_info; |
Wang Shilong | f79707b | 2014-07-17 11:44:09 +0800 | [diff] [blame] | 469 | |
Nikolay Borisov | 808a129 | 2020-06-03 08:55:27 +0300 | [diff] [blame] | 470 | if (!inode_can_compress(inode)) { |
Qu Wenruo | 42c16da | 2019-07-01 05:12:46 +0000 | [diff] [blame] | 471 | WARN(IS_ENABLED(CONFIG_BTRFS_DEBUG), |
| 472 | KERN_ERR "BTRFS: unexpected compression for ino %llu\n", |
Nikolay Borisov | 808a129 | 2020-06-03 08:55:27 +0300 | [diff] [blame] | 473 | btrfs_ino(inode)); |
Qu Wenruo | 42c16da | 2019-07-01 05:12:46 +0000 | [diff] [blame] | 474 | return 0; |
| 475 | } |
Wang Shilong | f79707b | 2014-07-17 11:44:09 +0800 | [diff] [blame] | 476 | /* force compress */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 477 | if (btrfs_test_opt(fs_info, FORCE_COMPRESS)) |
Wang Shilong | f79707b | 2014-07-17 11:44:09 +0800 | [diff] [blame] | 478 | return 1; |
David Sterba | eec63c6 | 2017-07-17 19:41:31 +0200 | [diff] [blame] | 479 | /* defrag ioctl */ |
Nikolay Borisov | 808a129 | 2020-06-03 08:55:27 +0300 | [diff] [blame] | 480 | if (inode->defrag_compress) |
David Sterba | eec63c6 | 2017-07-17 19:41:31 +0200 | [diff] [blame] | 481 | return 1; |
Wang Shilong | f79707b | 2014-07-17 11:44:09 +0800 | [diff] [blame] | 482 | /* bad compression ratios */ |
Nikolay Borisov | 808a129 | 2020-06-03 08:55:27 +0300 | [diff] [blame] | 483 | if (inode->flags & BTRFS_INODE_NOCOMPRESS) |
Wang Shilong | f79707b | 2014-07-17 11:44:09 +0800 | [diff] [blame] | 484 | return 0; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 485 | if (btrfs_test_opt(fs_info, COMPRESS) || |
Nikolay Borisov | 808a129 | 2020-06-03 08:55:27 +0300 | [diff] [blame] | 486 | inode->flags & BTRFS_INODE_COMPRESS || |
| 487 | inode->prop_compress) |
| 488 | return btrfs_compress_heuristic(&inode->vfs_inode, start, end); |
Wang Shilong | f79707b | 2014-07-17 11:44:09 +0800 | [diff] [blame] | 489 | return 0; |
| 490 | } |
| 491 | |
Nikolay Borisov | 6158e1c | 2017-02-20 13:50:43 +0200 | [diff] [blame] | 492 | static inline void inode_should_defrag(struct btrfs_inode *inode, |
Anand Jain | 26d30f8 | 2016-12-19 19:09:06 +0800 | [diff] [blame] | 493 | u64 start, u64 end, u64 num_bytes, u64 small_write) |
| 494 | { |
| 495 | /* If this is a small write inside eof, kick off a defrag */ |
| 496 | if (num_bytes < small_write && |
Nikolay Borisov | 6158e1c | 2017-02-20 13:50:43 +0200 | [diff] [blame] | 497 | (start > 0 || end + 1 < inode->disk_i_size)) |
Anand Jain | 26d30f8 | 2016-12-19 19:09:06 +0800 | [diff] [blame] | 498 | btrfs_add_inode_defrag(NULL, inode); |
| 499 | } |
| 500 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 501 | /* |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 502 | * we create compressed extents in two phases. The first |
| 503 | * phase compresses a range of pages that have already been |
| 504 | * locked (both pages and state bits are locked). |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 505 | * |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 506 | * This is done inside an ordered work queue, and the compression |
| 507 | * is spread across many cpus. The actual IO submission is step |
| 508 | * two, and the ordered work queue takes care of making sure that |
| 509 | * happens in the same order things were put onto the queue by |
| 510 | * writepages and friends. |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 511 | * |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 512 | * If this code finds it can't get good compression, it puts an |
| 513 | * entry onto the work queue to write the uncompressed bytes. This |
| 514 | * makes sure that both compressed inodes and uncompressed inodes |
Artem Bityutskiy | b257031 | 2012-07-25 18:12:06 +0300 | [diff] [blame] | 515 | * are written in the same order that the flusher thread sent them |
| 516 | * down. |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 517 | */ |
Nikolay Borisov | ac3e993 | 2019-07-17 14:41:44 +0300 | [diff] [blame] | 518 | static noinline int compress_file_range(struct async_chunk *async_chunk) |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 519 | { |
Nikolay Borisov | 1368c6d | 2019-03-12 17:20:27 +0200 | [diff] [blame] | 520 | struct inode *inode = async_chunk->inode; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 521 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 522 | u64 blocksize = fs_info->sectorsize; |
Nikolay Borisov | 1368c6d | 2019-03-12 17:20:27 +0200 | [diff] [blame] | 523 | u64 start = async_chunk->start; |
| 524 | u64 end = async_chunk->end; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 525 | u64 actual_end; |
Josef Bacik | d98da49 | 2019-10-11 09:03:54 -0400 | [diff] [blame] | 526 | u64 i_size; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 527 | int ret = 0; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 528 | struct page **pages = NULL; |
| 529 | unsigned long nr_pages; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 530 | unsigned long total_compressed = 0; |
| 531 | unsigned long total_in = 0; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 532 | int i; |
| 533 | int will_compress; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 534 | int compress_type = fs_info->compress_type; |
Nikolay Borisov | ac3e993 | 2019-07-17 14:41:44 +0300 | [diff] [blame] | 535 | int compressed_extents = 0; |
Chris Mason | 4adaa61 | 2013-03-26 13:07:00 -0400 | [diff] [blame] | 536 | int redirty = 0; |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 537 | |
Nikolay Borisov | 6158e1c | 2017-02-20 13:50:43 +0200 | [diff] [blame] | 538 | inode_should_defrag(BTRFS_I(inode), start, end, end - start + 1, |
| 539 | SZ_16K); |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 540 | |
Josef Bacik | d98da49 | 2019-10-11 09:03:54 -0400 | [diff] [blame] | 541 | /* |
| 542 | * We need to save i_size before now because it could change in between |
| 543 | * us evaluating the size and assigning it. This is because we lock and |
| 544 | * unlock the page in truncate and fallocate, and then modify the i_size |
| 545 | * later on. |
| 546 | * |
| 547 | * The barriers are to emulate READ_ONCE, remove that once i_size_read |
| 548 | * does that for us. |
| 549 | */ |
| 550 | barrier(); |
| 551 | i_size = i_size_read(inode); |
| 552 | barrier(); |
| 553 | actual_end = min_t(u64, i_size, end + 1); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 554 | again: |
| 555 | will_compress = 0; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 556 | nr_pages = (end >> PAGE_SHIFT) - (start >> PAGE_SHIFT) + 1; |
David Sterba | 069eac7 | 2017-02-14 19:36:54 +0100 | [diff] [blame] | 557 | BUILD_BUG_ON((BTRFS_MAX_COMPRESSED % PAGE_SIZE) != 0); |
| 558 | nr_pages = min_t(unsigned long, nr_pages, |
| 559 | BTRFS_MAX_COMPRESSED / PAGE_SIZE); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 560 | |
Chris Mason | f03d9301f | 2009-02-04 09:31:06 -0500 | [diff] [blame] | 561 | /* |
| 562 | * we don't want to send crud past the end of i_size through |
| 563 | * compression, that's just a waste of CPU time. So, if the |
| 564 | * end of the file is before the start of our current |
| 565 | * requested range of bytes, we bail out to the uncompressed |
| 566 | * cleanup code that can deal with all of this. |
| 567 | * |
| 568 | * It isn't really the fastest way to fix things, but this is a |
| 569 | * very uncommon corner. |
| 570 | */ |
| 571 | if (actual_end <= start) |
| 572 | goto cleanup_and_bail_uncompressed; |
| 573 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 574 | total_compressed = actual_end - start; |
| 575 | |
Shilong Wang | 4bcbb33 | 2014-10-07 18:44:35 -0400 | [diff] [blame] | 576 | /* |
| 577 | * skip compression for a small file range(<=blocksize) that |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 578 | * 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] | 579 | */ |
| 580 | if (total_compressed <= blocksize && |
| 581 | (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size)) |
| 582 | goto cleanup_and_bail_uncompressed; |
| 583 | |
David Sterba | 069eac7 | 2017-02-14 19:36:54 +0100 | [diff] [blame] | 584 | total_compressed = min_t(unsigned long, total_compressed, |
| 585 | BTRFS_MAX_UNCOMPRESSED); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 586 | total_in = 0; |
| 587 | ret = 0; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 588 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 589 | /* |
| 590 | * we do compression for mount -o compress and when the |
| 591 | * inode has not been flagged as nocompress. This flag can |
| 592 | * change at any time if we discover bad compression ratios. |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 593 | */ |
Nikolay Borisov | 808a129 | 2020-06-03 08:55:27 +0300 | [diff] [blame] | 594 | if (inode_need_compress(BTRFS_I(inode), start, end)) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 595 | WARN_ON(pages); |
David Sterba | 31e818f | 2015-02-20 18:00:26 +0100 | [diff] [blame] | 596 | pages = kcalloc(nr_pages, sizeof(struct page *), GFP_NOFS); |
Li Zefan | 560f7d7 | 2011-09-08 10:22:01 +0800 | [diff] [blame] | 597 | if (!pages) { |
| 598 | /* just bail out to the uncompressed code */ |
Filipe Manana | 3527a01 | 2018-10-13 00:37:25 +0100 | [diff] [blame] | 599 | nr_pages = 0; |
Li Zefan | 560f7d7 | 2011-09-08 10:22:01 +0800 | [diff] [blame] | 600 | goto cont; |
| 601 | } |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 602 | |
David Sterba | eec63c6 | 2017-07-17 19:41:31 +0200 | [diff] [blame] | 603 | if (BTRFS_I(inode)->defrag_compress) |
| 604 | compress_type = BTRFS_I(inode)->defrag_compress; |
| 605 | else if (BTRFS_I(inode)->prop_compress) |
David Sterba | b52aa8c | 2017-07-17 19:17:20 +0200 | [diff] [blame] | 606 | compress_type = BTRFS_I(inode)->prop_compress; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 607 | |
Chris Mason | 4adaa61 | 2013-03-26 13:07:00 -0400 | [diff] [blame] | 608 | /* |
| 609 | * we need to call clear_page_dirty_for_io on each |
| 610 | * page in the range. Otherwise applications with the file |
| 611 | * mmap'd can wander in and change the page contents while |
| 612 | * we are compressing them. |
| 613 | * |
| 614 | * If the compression fails for any reason, we set the pages |
| 615 | * dirty again later on. |
Timofey Titovets | e9679de | 2017-10-24 01:29:48 +0300 | [diff] [blame] | 616 | * |
| 617 | * Note that the remaining part is redirtied, the start pointer |
| 618 | * has moved, the end is the original one. |
Chris Mason | 4adaa61 | 2013-03-26 13:07:00 -0400 | [diff] [blame] | 619 | */ |
Timofey Titovets | e9679de | 2017-10-24 01:29:48 +0300 | [diff] [blame] | 620 | if (!redirty) { |
| 621 | extent_range_clear_dirty_for_io(inode, start, end); |
| 622 | redirty = 1; |
| 623 | } |
David Sterba | f51d2b5 | 2017-09-15 17:36:57 +0200 | [diff] [blame] | 624 | |
| 625 | /* Compression level is applied here and only here */ |
| 626 | ret = btrfs_compress_pages( |
| 627 | compress_type | (fs_info->compress_level << 4), |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 628 | inode->i_mapping, start, |
David Sterba | 38c3146 | 2017-02-14 19:04:07 +0100 | [diff] [blame] | 629 | pages, |
David Sterba | 4d3a800 | 2017-02-14 19:04:07 +0100 | [diff] [blame] | 630 | &nr_pages, |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 631 | &total_in, |
David Sterba | e5d7490 | 2017-02-14 19:45:05 +0100 | [diff] [blame] | 632 | &total_compressed); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 633 | |
| 634 | if (!ret) { |
Johannes Thumshirn | 7073017 | 2018-12-05 15:23:03 +0100 | [diff] [blame] | 635 | unsigned long offset = offset_in_page(total_compressed); |
David Sterba | 4d3a800 | 2017-02-14 19:04:07 +0100 | [diff] [blame] | 636 | struct page *page = pages[nr_pages - 1]; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 637 | char *kaddr; |
| 638 | |
| 639 | /* zero the tail end of the last page, we might be |
| 640 | * sending it down to disk |
| 641 | */ |
| 642 | if (offset) { |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 643 | kaddr = kmap_atomic(page); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 644 | memset(kaddr + offset, 0, |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 645 | PAGE_SIZE - offset); |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 646 | kunmap_atomic(kaddr); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 647 | } |
| 648 | will_compress = 1; |
| 649 | } |
| 650 | } |
Li Zefan | 560f7d7 | 2011-09-08 10:22:01 +0800 | [diff] [blame] | 651 | cont: |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 652 | if (start == 0) { |
| 653 | /* lets try to make an inline extent */ |
Timofey Titovets | 6018ba0 | 2017-09-15 01:57:26 +0300 | [diff] [blame] | 654 | if (ret || total_in < actual_end) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 655 | /* we didn't compress the entire range, try |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 656 | * to make an uncompressed inline extent. |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 657 | */ |
Nikolay Borisov | a034940 | 2020-06-03 08:55:12 +0300 | [diff] [blame] | 658 | ret = cow_file_range_inline(BTRFS_I(inode), start, end, |
| 659 | 0, BTRFS_COMPRESS_NONE, |
| 660 | NULL); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 661 | } else { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 662 | /* try making a compressed inline extent */ |
Nikolay Borisov | a034940 | 2020-06-03 08:55:12 +0300 | [diff] [blame] | 663 | ret = cow_file_range_inline(BTRFS_I(inode), start, end, |
Li Zefan | fe3f566 | 2011-03-28 08:30:38 +0000 | [diff] [blame] | 664 | total_compressed, |
| 665 | compress_type, pages); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 666 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 667 | if (ret <= 0) { |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 668 | unsigned long clear_flags = EXTENT_DELALLOC | |
Josef Bacik | 8b62f87 | 2017-10-19 14:15:55 -0400 | [diff] [blame] | 669 | EXTENT_DELALLOC_NEW | EXTENT_DEFRAG | |
| 670 | EXTENT_DO_ACCOUNTING; |
Filipe Manana | e6eb431 | 2014-10-10 10:45:12 +0100 | [diff] [blame] | 671 | unsigned long page_error_op; |
| 672 | |
Filipe Manana | e6eb431 | 2014-10-10 10:45:12 +0100 | [diff] [blame] | 673 | page_error_op = ret < 0 ? PAGE_SET_ERROR : 0; |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 674 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 675 | /* |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 676 | * inline extent creation worked or returned error, |
| 677 | * we don't need to create any more async work items. |
| 678 | * Unlock and free up our temp pages. |
Josef Bacik | 8b62f87 | 2017-10-19 14:15:55 -0400 | [diff] [blame] | 679 | * |
| 680 | * We use DO_ACCOUNTING here because we need the |
| 681 | * delalloc_release_metadata to be done _after_ we drop |
| 682 | * our outstanding extent for clearing delalloc for this |
| 683 | * range. |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 684 | */ |
Nikolay Borisov | ad7ff17 | 2020-06-03 08:55:06 +0300 | [diff] [blame] | 685 | extent_clear_unlock_delalloc(BTRFS_I(inode), start, end, |
| 686 | NULL, |
Nikolay Borisov | 74e9194 | 2019-07-17 16:18:16 +0300 | [diff] [blame] | 687 | clear_flags, |
Qu Wenruo | ba8b04c | 2016-07-19 16:50:36 +0800 | [diff] [blame] | 688 | PAGE_UNLOCK | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 689 | PAGE_CLEAR_DIRTY | |
| 690 | PAGE_SET_WRITEBACK | |
Filipe Manana | e6eb431 | 2014-10-10 10:45:12 +0100 | [diff] [blame] | 691 | page_error_op | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 692 | PAGE_END_WRITEBACK); |
Nikolay Borisov | cecc8d9 | 2019-07-17 14:41:45 +0300 | [diff] [blame] | 693 | |
Qu Wenruo | 1e6e238 | 2020-07-28 16:39:26 +0800 | [diff] [blame] | 694 | /* |
| 695 | * Ensure we only free the compressed pages if we have |
| 696 | * them allocated, as we can still reach here with |
| 697 | * inode_need_compress() == false. |
| 698 | */ |
| 699 | if (pages) { |
| 700 | for (i = 0; i < nr_pages; i++) { |
| 701 | WARN_ON(pages[i]->mapping); |
| 702 | put_page(pages[i]); |
| 703 | } |
| 704 | kfree(pages); |
Nikolay Borisov | cecc8d9 | 2019-07-17 14:41:45 +0300 | [diff] [blame] | 705 | } |
Nikolay Borisov | cecc8d9 | 2019-07-17 14:41:45 +0300 | [diff] [blame] | 706 | return 0; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 707 | } |
| 708 | } |
| 709 | |
| 710 | if (will_compress) { |
| 711 | /* |
| 712 | * we aren't doing an inline extent round the compressed size |
| 713 | * up to a block size boundary so the allocator does sane |
| 714 | * things |
| 715 | */ |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 716 | total_compressed = ALIGN(total_compressed, blocksize); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 717 | |
| 718 | /* |
| 719 | * one last check to make sure the compression is really a |
Timofey Titovets | 170607e | 2017-06-06 14:41:15 +0300 | [diff] [blame] | 720 | * win, compare the page count read with the blocks on disk, |
| 721 | * compression must free at least one sector size |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 722 | */ |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 723 | total_in = ALIGN(total_in, PAGE_SIZE); |
Timofey Titovets | 170607e | 2017-06-06 14:41:15 +0300 | [diff] [blame] | 724 | if (total_compressed + blocksize <= total_in) { |
Nikolay Borisov | ac3e993 | 2019-07-17 14:41:44 +0300 | [diff] [blame] | 725 | compressed_extents++; |
Ashish Samant | c8bb0c8 | 2016-03-25 19:01:33 -0700 | [diff] [blame] | 726 | |
| 727 | /* |
| 728 | * The async work queues will take care of doing actual |
| 729 | * allocation on disk for these compressed pages, and |
| 730 | * will submit them to the elevator. |
| 731 | */ |
Nikolay Borisov | b532627 | 2019-03-12 17:20:25 +0200 | [diff] [blame] | 732 | add_async_extent(async_chunk, start, total_in, |
David Sterba | 4d3a800 | 2017-02-14 19:04:07 +0100 | [diff] [blame] | 733 | total_compressed, pages, nr_pages, |
Ashish Samant | c8bb0c8 | 2016-03-25 19:01:33 -0700 | [diff] [blame] | 734 | compress_type); |
| 735 | |
Timofey Titovets | 1170862 | 2017-10-03 18:06:01 +0300 | [diff] [blame] | 736 | if (start + total_in < end) { |
| 737 | start += total_in; |
Ashish Samant | c8bb0c8 | 2016-03-25 19:01:33 -0700 | [diff] [blame] | 738 | pages = NULL; |
| 739 | cond_resched(); |
| 740 | goto again; |
| 741 | } |
Nikolay Borisov | ac3e993 | 2019-07-17 14:41:44 +0300 | [diff] [blame] | 742 | return compressed_extents; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 743 | } |
| 744 | } |
Ashish Samant | c8bb0c8 | 2016-03-25 19:01:33 -0700 | [diff] [blame] | 745 | if (pages) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 746 | /* |
| 747 | * the compression code ran but failed to make things smaller, |
| 748 | * free any pages it allocated and our page pointer array |
| 749 | */ |
David Sterba | 4d3a800 | 2017-02-14 19:04:07 +0100 | [diff] [blame] | 750 | for (i = 0; i < nr_pages; i++) { |
Chris Mason | 70b99e6 | 2008-10-31 12:46:39 -0400 | [diff] [blame] | 751 | WARN_ON(pages[i]->mapping); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 752 | put_page(pages[i]); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 753 | } |
| 754 | kfree(pages); |
| 755 | pages = NULL; |
| 756 | total_compressed = 0; |
David Sterba | 4d3a800 | 2017-02-14 19:04:07 +0100 | [diff] [blame] | 757 | nr_pages = 0; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 758 | |
| 759 | /* flag the file so we don't compress in the future */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 760 | if (!btrfs_test_opt(fs_info, FORCE_COMPRESS) && |
David Sterba | b52aa8c | 2017-07-17 19:17:20 +0200 | [diff] [blame] | 761 | !(BTRFS_I(inode)->prop_compress)) { |
Chris Mason | a555f81 | 2010-01-28 16:18:15 -0500 | [diff] [blame] | 762 | BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS; |
Chris Mason | 1e701a3 | 2010-03-11 09:42:04 -0500 | [diff] [blame] | 763 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 764 | } |
Chris Mason | f03d9301f | 2009-02-04 09:31:06 -0500 | [diff] [blame] | 765 | cleanup_and_bail_uncompressed: |
Ashish Samant | c8bb0c8 | 2016-03-25 19:01:33 -0700 | [diff] [blame] | 766 | /* |
| 767 | * No compression, but we still need to write the pages in the file |
| 768 | * we've been given so far. redirty the locked page if it corresponds |
| 769 | * to our extent and set things up for the async work queue to run |
| 770 | * cow_file_range to do the normal delalloc dance. |
| 771 | */ |
Chris Mason | 1d53c9e | 2019-07-10 12:28:16 -0700 | [diff] [blame] | 772 | if (async_chunk->locked_page && |
| 773 | (page_offset(async_chunk->locked_page) >= start && |
| 774 | page_offset(async_chunk->locked_page)) <= end) { |
Nikolay Borisov | 1368c6d | 2019-03-12 17:20:27 +0200 | [diff] [blame] | 775 | __set_page_dirty_nobuffers(async_chunk->locked_page); |
Ashish Samant | c8bb0c8 | 2016-03-25 19:01:33 -0700 | [diff] [blame] | 776 | /* unlocked later on in the async handlers */ |
Chris Mason | 1d53c9e | 2019-07-10 12:28:16 -0700 | [diff] [blame] | 777 | } |
Ashish Samant | c8bb0c8 | 2016-03-25 19:01:33 -0700 | [diff] [blame] | 778 | |
| 779 | if (redirty) |
| 780 | extent_range_redirty_for_io(inode, start, end); |
Nikolay Borisov | b532627 | 2019-03-12 17:20:25 +0200 | [diff] [blame] | 781 | add_async_extent(async_chunk, start, end - start + 1, 0, NULL, 0, |
Ashish Samant | c8bb0c8 | 2016-03-25 19:01:33 -0700 | [diff] [blame] | 782 | BTRFS_COMPRESS_NONE); |
Nikolay Borisov | ac3e993 | 2019-07-17 14:41:44 +0300 | [diff] [blame] | 783 | compressed_extents++; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 784 | |
Nikolay Borisov | ac3e993 | 2019-07-17 14:41:44 +0300 | [diff] [blame] | 785 | return compressed_extents; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 786 | } |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 787 | |
Filipe Manana | 40ae837 | 2014-10-06 22:14:24 +0100 | [diff] [blame] | 788 | static void free_async_extent_pages(struct async_extent *async_extent) |
| 789 | { |
| 790 | int i; |
| 791 | |
| 792 | if (!async_extent->pages) |
| 793 | return; |
| 794 | |
| 795 | for (i = 0; i < async_extent->nr_pages; i++) { |
| 796 | WARN_ON(async_extent->pages[i]->mapping); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 797 | put_page(async_extent->pages[i]); |
Filipe Manana | 40ae837 | 2014-10-06 22:14:24 +0100 | [diff] [blame] | 798 | } |
| 799 | kfree(async_extent->pages); |
| 800 | async_extent->nr_pages = 0; |
| 801 | async_extent->pages = NULL; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 802 | } |
| 803 | |
| 804 | /* |
| 805 | * phase two of compressed writeback. This is the ordered portion |
| 806 | * of the code, which only gets called in the order the work was |
| 807 | * queued. We walk all the async extents created by compress_file_range |
| 808 | * and send them down to the disk. |
| 809 | */ |
Nikolay Borisov | b532627 | 2019-03-12 17:20:25 +0200 | [diff] [blame] | 810 | static noinline void submit_compressed_extents(struct async_chunk *async_chunk) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 811 | { |
Nikolay Borisov | a0ff10d | 2020-06-03 08:55:17 +0300 | [diff] [blame] | 812 | struct btrfs_inode *inode = BTRFS_I(async_chunk->inode); |
| 813 | struct btrfs_fs_info *fs_info = inode->root->fs_info; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 814 | struct async_extent *async_extent; |
| 815 | u64 alloc_hint = 0; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 816 | struct btrfs_key ins; |
| 817 | struct extent_map *em; |
Nikolay Borisov | a0ff10d | 2020-06-03 08:55:17 +0300 | [diff] [blame] | 818 | struct btrfs_root *root = inode->root; |
| 819 | struct extent_io_tree *io_tree = &inode->io_tree; |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 820 | int ret = 0; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 821 | |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 822 | again: |
Nikolay Borisov | b532627 | 2019-03-12 17:20:25 +0200 | [diff] [blame] | 823 | while (!list_empty(&async_chunk->extents)) { |
| 824 | async_extent = list_entry(async_chunk->extents.next, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 825 | struct async_extent, list); |
| 826 | list_del(&async_extent->list); |
| 827 | |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 828 | retry: |
Nikolay Borisov | 7447555 | 2019-03-12 17:20:30 +0200 | [diff] [blame] | 829 | lock_extent(io_tree, async_extent->start, |
| 830 | async_extent->start + async_extent->ram_size - 1); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 831 | /* did the compression code fall back to uncompressed IO? */ |
| 832 | if (!async_extent->pages) { |
| 833 | int page_started = 0; |
| 834 | unsigned long nr_written = 0; |
| 835 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 836 | /* allocate blocks */ |
Nikolay Borisov | a0ff10d | 2020-06-03 08:55:17 +0300 | [diff] [blame] | 837 | ret = cow_file_range(inode, async_chunk->locked_page, |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 838 | async_extent->start, |
| 839 | async_extent->start + |
| 840 | async_extent->ram_size - 1, |
Nikolay Borisov | 330a582 | 2019-07-17 16:18:17 +0300 | [diff] [blame] | 841 | &page_started, &nr_written, 0); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 842 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 843 | /* JDM XXX */ |
| 844 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 845 | /* |
| 846 | * if page_started, cow_file_range inserted an |
| 847 | * inline extent and took care of all the unlocking |
| 848 | * and IO for us. Otherwise, we need to submit |
| 849 | * all those pages down to the drive. |
| 850 | */ |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 851 | if (!page_started && !ret) |
Nikolay Borisov | a0ff10d | 2020-06-03 08:55:17 +0300 | [diff] [blame] | 852 | extent_write_locked_range(&inode->vfs_inode, |
Nikolay Borisov | 5e3ee23 | 2017-12-08 15:55:58 +0200 | [diff] [blame] | 853 | async_extent->start, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 854 | async_extent->start + |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 855 | async_extent->ram_size - 1, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 856 | WB_SYNC_ALL); |
Chris Mason | 1d53c9e | 2019-07-10 12:28:16 -0700 | [diff] [blame] | 857 | else if (ret && async_chunk->locked_page) |
Nikolay Borisov | b532627 | 2019-03-12 17:20:25 +0200 | [diff] [blame] | 858 | unlock_page(async_chunk->locked_page); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 859 | kfree(async_extent); |
| 860 | cond_resched(); |
| 861 | continue; |
| 862 | } |
| 863 | |
Wang Xiaoguang | 1851309 | 2016-07-25 15:51:40 +0800 | [diff] [blame] | 864 | ret = btrfs_reserve_extent(root, async_extent->ram_size, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 865 | async_extent->compressed_size, |
| 866 | async_extent->compressed_size, |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 867 | 0, alloc_hint, &ins, 1, 1); |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 868 | if (ret) { |
Filipe Manana | 40ae837 | 2014-10-06 22:14:24 +0100 | [diff] [blame] | 869 | free_async_extent_pages(async_extent); |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 870 | |
Josef Bacik | fdf8e2e | 2013-06-14 16:58:23 -0400 | [diff] [blame] | 871 | if (ret == -ENOSPC) { |
| 872 | unlock_extent(io_tree, async_extent->start, |
| 873 | async_extent->start + |
| 874 | async_extent->ram_size - 1); |
Liu Bo | ce62003 | 2014-07-24 22:48:05 +0800 | [diff] [blame] | 875 | |
| 876 | /* |
| 877 | * we need to redirty the pages if we decide to |
| 878 | * fallback to uncompressed IO, otherwise we |
| 879 | * will not submit these pages down to lower |
| 880 | * layers. |
| 881 | */ |
Nikolay Borisov | a0ff10d | 2020-06-03 08:55:17 +0300 | [diff] [blame] | 882 | extent_range_redirty_for_io(&inode->vfs_inode, |
Liu Bo | ce62003 | 2014-07-24 22:48:05 +0800 | [diff] [blame] | 883 | async_extent->start, |
| 884 | async_extent->start + |
| 885 | async_extent->ram_size - 1); |
| 886 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 887 | goto retry; |
Josef Bacik | fdf8e2e | 2013-06-14 16:58:23 -0400 | [diff] [blame] | 888 | } |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 889 | goto out_free; |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 890 | } |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 891 | /* |
| 892 | * here we're doing allocation and writeback of the |
| 893 | * compressed pages |
| 894 | */ |
Nikolay Borisov | a0ff10d | 2020-06-03 08:55:17 +0300 | [diff] [blame] | 895 | em = create_io_em(inode, async_extent->start, |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 896 | async_extent->ram_size, /* len */ |
| 897 | async_extent->start, /* orig_start */ |
| 898 | ins.objectid, /* block_start */ |
| 899 | ins.offset, /* block_len */ |
| 900 | ins.offset, /* orig_block_len */ |
| 901 | async_extent->ram_size, /* ram_bytes */ |
| 902 | async_extent->compress_type, |
| 903 | BTRFS_ORDERED_COMPRESSED); |
| 904 | if (IS_ERR(em)) |
| 905 | /* ret value is not necessary due to void function */ |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 906 | goto out_free_reserve; |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 907 | free_extent_map(em); |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 908 | |
Nikolay Borisov | a0ff10d | 2020-06-03 08:55:17 +0300 | [diff] [blame] | 909 | ret = btrfs_add_ordered_extent_compress(inode, |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 910 | async_extent->start, |
| 911 | ins.objectid, |
| 912 | async_extent->ram_size, |
| 913 | ins.offset, |
| 914 | BTRFS_ORDERED_COMPRESSED, |
| 915 | async_extent->compress_type); |
Filipe Manana | d9f8596 | 2014-08-25 10:43:00 +0100 | [diff] [blame] | 916 | if (ret) { |
Nikolay Borisov | a0ff10d | 2020-06-03 08:55:17 +0300 | [diff] [blame] | 917 | btrfs_drop_extent_cache(inode, async_extent->start, |
Filipe Manana | d9f8596 | 2014-08-25 10:43:00 +0100 | [diff] [blame] | 918 | async_extent->start + |
| 919 | async_extent->ram_size - 1, 0); |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 920 | goto out_free_reserve; |
Filipe Manana | d9f8596 | 2014-08-25 10:43:00 +0100 | [diff] [blame] | 921 | } |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 922 | btrfs_dec_block_group_reservations(fs_info, ins.objectid); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 923 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 924 | /* |
| 925 | * clear dirty, set writeback and unlock the pages. |
| 926 | */ |
Nikolay Borisov | a0ff10d | 2020-06-03 08:55:17 +0300 | [diff] [blame] | 927 | extent_clear_unlock_delalloc(inode, async_extent->start, |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 928 | async_extent->start + |
| 929 | async_extent->ram_size - 1, |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 930 | NULL, EXTENT_LOCKED | EXTENT_DELALLOC, |
| 931 | PAGE_UNLOCK | PAGE_CLEAR_DIRTY | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 932 | PAGE_SET_WRITEBACK); |
Nikolay Borisov | a0ff10d | 2020-06-03 08:55:17 +0300 | [diff] [blame] | 933 | if (btrfs_submit_compressed_write(inode, async_extent->start, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 934 | async_extent->ram_size, |
| 935 | ins.objectid, |
| 936 | ins.offset, async_extent->pages, |
Liu Bo | f82b735 | 2017-10-23 23:18:16 -0600 | [diff] [blame] | 937 | async_extent->nr_pages, |
Chris Mason | ec39f76 | 2019-07-10 12:28:17 -0700 | [diff] [blame] | 938 | async_chunk->write_flags, |
| 939 | async_chunk->blkcg_css)) { |
Filipe Manana | fce2a4e | 2014-10-06 22:14:23 +0100 | [diff] [blame] | 940 | struct page *p = async_extent->pages[0]; |
| 941 | const u64 start = async_extent->start; |
| 942 | const u64 end = start + async_extent->ram_size - 1; |
| 943 | |
Nikolay Borisov | a0ff10d | 2020-06-03 08:55:17 +0300 | [diff] [blame] | 944 | p->mapping = inode->vfs_inode.i_mapping; |
Nikolay Borisov | c629732 | 2018-11-08 10:18:08 +0200 | [diff] [blame] | 945 | btrfs_writepage_endio_finish_ordered(p, start, end, 0); |
Nikolay Borisov | 7087a9d | 2018-11-01 14:09:48 +0200 | [diff] [blame] | 946 | |
Filipe Manana | fce2a4e | 2014-10-06 22:14:23 +0100 | [diff] [blame] | 947 | p->mapping = NULL; |
Nikolay Borisov | a0ff10d | 2020-06-03 08:55:17 +0300 | [diff] [blame] | 948 | extent_clear_unlock_delalloc(inode, start, end, NULL, 0, |
Filipe Manana | fce2a4e | 2014-10-06 22:14:23 +0100 | [diff] [blame] | 949 | PAGE_END_WRITEBACK | |
| 950 | PAGE_SET_ERROR); |
Filipe Manana | 40ae837 | 2014-10-06 22:14:24 +0100 | [diff] [blame] | 951 | free_async_extent_pages(async_extent); |
Filipe Manana | fce2a4e | 2014-10-06 22:14:23 +0100 | [diff] [blame] | 952 | } |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 953 | alloc_hint = ins.objectid + ins.offset; |
| 954 | kfree(async_extent); |
| 955 | cond_resched(); |
| 956 | } |
Filipe Manana | dec8f17 | 2014-10-06 22:14:26 +0100 | [diff] [blame] | 957 | return; |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 958 | out_free_reserve: |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 959 | btrfs_dec_block_group_reservations(fs_info, ins.objectid); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 960 | btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 961 | out_free: |
Nikolay Borisov | a0ff10d | 2020-06-03 08:55:17 +0300 | [diff] [blame] | 962 | extent_clear_unlock_delalloc(inode, async_extent->start, |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 963 | async_extent->start + |
| 964 | async_extent->ram_size - 1, |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 965 | NULL, EXTENT_LOCKED | EXTENT_DELALLOC | |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 966 | EXTENT_DELALLOC_NEW | |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 967 | EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING, |
| 968 | PAGE_UNLOCK | PAGE_CLEAR_DIRTY | |
Filipe Manana | 704de49 | 2014-10-06 22:14:22 +0100 | [diff] [blame] | 969 | PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK | |
| 970 | PAGE_SET_ERROR); |
Filipe Manana | 40ae837 | 2014-10-06 22:14:24 +0100 | [diff] [blame] | 971 | free_async_extent_pages(async_extent); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 972 | kfree(async_extent); |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 973 | goto again; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 974 | } |
| 975 | |
Nikolay Borisov | 43c6984 | 2020-06-03 08:55:02 +0300 | [diff] [blame] | 976 | static u64 get_extent_allocation_hint(struct btrfs_inode *inode, u64 start, |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 977 | u64 num_bytes) |
| 978 | { |
Nikolay Borisov | 43c6984 | 2020-06-03 08:55:02 +0300 | [diff] [blame] | 979 | struct extent_map_tree *em_tree = &inode->extent_tree; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 980 | struct extent_map *em; |
| 981 | u64 alloc_hint = 0; |
| 982 | |
| 983 | read_lock(&em_tree->lock); |
| 984 | em = search_extent_mapping(em_tree, start, num_bytes); |
| 985 | if (em) { |
| 986 | /* |
| 987 | * if block start isn't an actual block number then find the |
| 988 | * first block in this inode and use that as a hint. If that |
| 989 | * block is also bogus then just don't worry about it. |
| 990 | */ |
| 991 | if (em->block_start >= EXTENT_MAP_LAST_BYTE) { |
| 992 | free_extent_map(em); |
| 993 | em = search_extent_mapping(em_tree, 0, 0); |
| 994 | if (em && em->block_start < EXTENT_MAP_LAST_BYTE) |
| 995 | alloc_hint = em->block_start; |
| 996 | if (em) |
| 997 | free_extent_map(em); |
| 998 | } else { |
| 999 | alloc_hint = em->block_start; |
| 1000 | free_extent_map(em); |
| 1001 | } |
| 1002 | } |
| 1003 | read_unlock(&em_tree->lock); |
| 1004 | |
| 1005 | return alloc_hint; |
| 1006 | } |
| 1007 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1008 | /* |
| 1009 | * when extent_io.c finds a delayed allocation range in the file, |
| 1010 | * the call backs end up in this code. The basic idea is to |
| 1011 | * allocate extents on disk for the range, and create ordered data structs |
| 1012 | * in ram to track those extents. |
| 1013 | * |
| 1014 | * locked_page is the page that writepage had locked already. We use |
| 1015 | * it to make sure we don't do extra locks or unlocks. |
| 1016 | * |
| 1017 | * *page_started is set to one if we unlock locked_page and do everything |
| 1018 | * required to start IO on it. It may be clean and already done with |
| 1019 | * IO when we return. |
| 1020 | */ |
Nikolay Borisov | 6e26c44 | 2020-06-03 08:55:14 +0300 | [diff] [blame] | 1021 | static noinline int cow_file_range(struct btrfs_inode *inode, |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 1022 | struct page *locked_page, |
Nikolay Borisov | 74e9194 | 2019-07-17 16:18:16 +0300 | [diff] [blame] | 1023 | u64 start, u64 end, int *page_started, |
Nikolay Borisov | 330a582 | 2019-07-17 16:18:17 +0300 | [diff] [blame] | 1024 | unsigned long *nr_written, int unlock) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1025 | { |
Nikolay Borisov | 6e26c44 | 2020-06-03 08:55:14 +0300 | [diff] [blame] | 1026 | struct btrfs_root *root = inode->root; |
| 1027 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1028 | u64 alloc_hint = 0; |
| 1029 | u64 num_bytes; |
| 1030 | unsigned long ram_size; |
Filipe Manana | a315e68 | 2017-03-06 23:04:20 +0000 | [diff] [blame] | 1031 | u64 cur_alloc_size = 0; |
Filipe Manana | 432cd2a | 2020-06-08 13:32:55 +0100 | [diff] [blame] | 1032 | u64 min_alloc_size; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1033 | u64 blocksize = fs_info->sectorsize; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1034 | struct btrfs_key ins; |
| 1035 | struct extent_map *em; |
Filipe Manana | a315e68 | 2017-03-06 23:04:20 +0000 | [diff] [blame] | 1036 | unsigned clear_bits; |
| 1037 | unsigned long page_ops; |
| 1038 | bool extent_reserved = false; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1039 | int ret = 0; |
| 1040 | |
Nikolay Borisov | 6e26c44 | 2020-06-03 08:55:14 +0300 | [diff] [blame] | 1041 | if (btrfs_is_free_space_inode(inode)) { |
Josef Bacik | 02ecd2c | 2013-10-25 16:19:08 -0400 | [diff] [blame] | 1042 | WARN_ON_ONCE(1); |
Josef Bacik | 29bce2f | 2014-02-07 12:21:23 -0500 | [diff] [blame] | 1043 | ret = -EINVAL; |
| 1044 | goto out_unlock; |
Josef Bacik | 02ecd2c | 2013-10-25 16:19:08 -0400 | [diff] [blame] | 1045 | } |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1046 | |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 1047 | num_bytes = ALIGN(end - start + 1, blocksize); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1048 | num_bytes = max(blocksize, num_bytes); |
Anand Jain | 566b176 | 2018-02-15 18:07:59 +0800 | [diff] [blame] | 1049 | ASSERT(num_bytes <= btrfs_super_total_bytes(fs_info->super_copy)); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1050 | |
Nikolay Borisov | 6e26c44 | 2020-06-03 08:55:14 +0300 | [diff] [blame] | 1051 | inode_should_defrag(inode, start, end, num_bytes, SZ_64K); |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1052 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1053 | if (start == 0) { |
| 1054 | /* lets try to make an inline extent */ |
Nikolay Borisov | 6e26c44 | 2020-06-03 08:55:14 +0300 | [diff] [blame] | 1055 | ret = cow_file_range_inline(inode, start, end, 0, |
Nikolay Borisov | d02c0e2 | 2018-03-02 09:43:15 +0200 | [diff] [blame] | 1056 | BTRFS_COMPRESS_NONE, NULL); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1057 | if (ret == 0) { |
Josef Bacik | 8b62f87 | 2017-10-19 14:15:55 -0400 | [diff] [blame] | 1058 | /* |
| 1059 | * We use DO_ACCOUNTING here because we need the |
| 1060 | * delalloc_release_metadata to be run _after_ we drop |
| 1061 | * our outstanding extent for clearing delalloc for this |
| 1062 | * range. |
| 1063 | */ |
Nikolay Borisov | 6e26c44 | 2020-06-03 08:55:14 +0300 | [diff] [blame] | 1064 | extent_clear_unlock_delalloc(inode, start, end, NULL, |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1065 | EXTENT_LOCKED | EXTENT_DELALLOC | |
Josef Bacik | 8b62f87 | 2017-10-19 14:15:55 -0400 | [diff] [blame] | 1066 | EXTENT_DELALLOC_NEW | EXTENT_DEFRAG | |
| 1067 | EXTENT_DO_ACCOUNTING, PAGE_UNLOCK | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1068 | PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK | |
| 1069 | PAGE_END_WRITEBACK); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1070 | *nr_written = *nr_written + |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1071 | (end - start + PAGE_SIZE) / PAGE_SIZE; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1072 | *page_started = 1; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1073 | goto out; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1074 | } else if (ret < 0) { |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1075 | goto out_unlock; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1076 | } |
| 1077 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1078 | |
Nikolay Borisov | 6e26c44 | 2020-06-03 08:55:14 +0300 | [diff] [blame] | 1079 | alloc_hint = get_extent_allocation_hint(inode, start, num_bytes); |
| 1080 | btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 1081 | |
Filipe Manana | 432cd2a | 2020-06-08 13:32:55 +0100 | [diff] [blame] | 1082 | /* |
| 1083 | * Relocation relies on the relocated extents to have exactly the same |
| 1084 | * size as the original extents. Normally writeback for relocation data |
| 1085 | * extents follows a NOCOW path because relocation preallocates the |
| 1086 | * extents. However, due to an operation such as scrub turning a block |
| 1087 | * group to RO mode, it may fallback to COW mode, so we must make sure |
| 1088 | * an extent allocated during COW has exactly the requested size and can |
| 1089 | * not be split into smaller extents, otherwise relocation breaks and |
| 1090 | * fails during the stage where it updates the bytenr of file extent |
| 1091 | * items. |
| 1092 | */ |
| 1093 | if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID) |
| 1094 | min_alloc_size = num_bytes; |
| 1095 | else |
| 1096 | min_alloc_size = fs_info->sectorsize; |
| 1097 | |
Anand Jain | 3752d22 | 2018-02-15 12:29:38 +0800 | [diff] [blame] | 1098 | while (num_bytes > 0) { |
| 1099 | cur_alloc_size = num_bytes; |
Wang Xiaoguang | 1851309 | 2016-07-25 15:51:40 +0800 | [diff] [blame] | 1100 | ret = btrfs_reserve_extent(root, cur_alloc_size, cur_alloc_size, |
Filipe Manana | 432cd2a | 2020-06-08 13:32:55 +0100 | [diff] [blame] | 1101 | min_alloc_size, 0, alloc_hint, |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 1102 | &ins, 1, 1); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 1103 | if (ret < 0) |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1104 | goto out_unlock; |
Filipe Manana | a315e68 | 2017-03-06 23:04:20 +0000 | [diff] [blame] | 1105 | cur_alloc_size = ins.offset; |
| 1106 | extent_reserved = true; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1107 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1108 | ram_size = ins.offset; |
Nikolay Borisov | 6e26c44 | 2020-06-03 08:55:14 +0300 | [diff] [blame] | 1109 | em = create_io_em(inode, start, ins.offset, /* len */ |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 1110 | start, /* orig_start */ |
| 1111 | ins.objectid, /* block_start */ |
| 1112 | ins.offset, /* block_len */ |
| 1113 | ins.offset, /* orig_block_len */ |
| 1114 | ram_size, /* ram_bytes */ |
| 1115 | BTRFS_COMPRESS_NONE, /* compress_type */ |
Liu Bo | 1af4a0a | 2017-02-13 15:35:09 -0800 | [diff] [blame] | 1116 | BTRFS_ORDERED_REGULAR /* type */); |
Su Yue | 090a127a | 2018-05-30 16:48:56 +0800 | [diff] [blame] | 1117 | if (IS_ERR(em)) { |
| 1118 | ret = PTR_ERR(em); |
Liu Bo | ace68ba | 2013-04-22 10:53:47 +0000 | [diff] [blame] | 1119 | goto out_reserve; |
Su Yue | 090a127a | 2018-05-30 16:48:56 +0800 | [diff] [blame] | 1120 | } |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 1121 | free_extent_map(em); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1122 | |
Nikolay Borisov | 6e26c44 | 2020-06-03 08:55:14 +0300 | [diff] [blame] | 1123 | ret = btrfs_add_ordered_extent(inode, start, ins.objectid, |
| 1124 | ram_size, cur_alloc_size, 0); |
Liu Bo | ace68ba | 2013-04-22 10:53:47 +0000 | [diff] [blame] | 1125 | if (ret) |
Filipe Manana | d9f8596 | 2014-08-25 10:43:00 +0100 | [diff] [blame] | 1126 | goto out_drop_extent_cache; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1127 | |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1128 | if (root->root_key.objectid == |
| 1129 | BTRFS_DATA_RELOC_TREE_OBJECTID) { |
Nikolay Borisov | 6e26c44 | 2020-06-03 08:55:14 +0300 | [diff] [blame] | 1130 | ret = btrfs_reloc_clone_csums(inode, start, |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1131 | cur_alloc_size); |
Qu Wenruo | 4dbd80f | 2017-03-08 10:25:51 +0800 | [diff] [blame] | 1132 | /* |
| 1133 | * Only drop cache here, and process as normal. |
| 1134 | * |
| 1135 | * We must not allow extent_clear_unlock_delalloc() |
| 1136 | * at out_unlock label to free meta of this ordered |
| 1137 | * extent, as its meta should be freed by |
| 1138 | * btrfs_finish_ordered_io(). |
| 1139 | * |
| 1140 | * So we must continue until @start is increased to |
| 1141 | * skip current ordered extent. |
| 1142 | */ |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 1143 | if (ret) |
Nikolay Borisov | 6e26c44 | 2020-06-03 08:55:14 +0300 | [diff] [blame] | 1144 | btrfs_drop_extent_cache(inode, start, |
Qu Wenruo | 4dbd80f | 2017-03-08 10:25:51 +0800 | [diff] [blame] | 1145 | start + ram_size - 1, 0); |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1146 | } |
| 1147 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1148 | btrfs_dec_block_group_reservations(fs_info, ins.objectid); |
Filipe Manana | 9cfa3e3 | 2016-04-26 15:39:32 +0100 | [diff] [blame] | 1149 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1150 | /* we're not doing compressed IO, don't unlock the first |
| 1151 | * page (which the caller expects to stay locked), don't |
| 1152 | * clear any dirty bits and don't set any writeback bits |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 1153 | * |
| 1154 | * Do set the Private2 bit so we know this page was properly |
| 1155 | * setup for writepage |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1156 | */ |
Filipe Manana | a315e68 | 2017-03-06 23:04:20 +0000 | [diff] [blame] | 1157 | page_ops = unlock ? PAGE_UNLOCK : 0; |
| 1158 | page_ops |= PAGE_SET_PRIVATE2; |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 1159 | |
Nikolay Borisov | 6e26c44 | 2020-06-03 08:55:14 +0300 | [diff] [blame] | 1160 | extent_clear_unlock_delalloc(inode, start, start + ram_size - 1, |
Nikolay Borisov | 74e9194 | 2019-07-17 16:18:16 +0300 | [diff] [blame] | 1161 | locked_page, |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1162 | EXTENT_LOCKED | EXTENT_DELALLOC, |
Filipe Manana | a315e68 | 2017-03-06 23:04:20 +0000 | [diff] [blame] | 1163 | page_ops); |
Anand Jain | 3752d22 | 2018-02-15 12:29:38 +0800 | [diff] [blame] | 1164 | if (num_bytes < cur_alloc_size) |
| 1165 | num_bytes = 0; |
Qu Wenruo | 4dbd80f | 2017-03-08 10:25:51 +0800 | [diff] [blame] | 1166 | else |
Anand Jain | 3752d22 | 2018-02-15 12:29:38 +0800 | [diff] [blame] | 1167 | num_bytes -= cur_alloc_size; |
Chris Mason | c59f895 | 2007-12-17 20:14:04 -0500 | [diff] [blame] | 1168 | alloc_hint = ins.objectid + ins.offset; |
| 1169 | start += cur_alloc_size; |
Filipe Manana | a315e68 | 2017-03-06 23:04:20 +0000 | [diff] [blame] | 1170 | extent_reserved = false; |
Qu Wenruo | 4dbd80f | 2017-03-08 10:25:51 +0800 | [diff] [blame] | 1171 | |
| 1172 | /* |
| 1173 | * btrfs_reloc_clone_csums() error, since start is increased |
| 1174 | * extent_clear_unlock_delalloc() at out_unlock label won't |
| 1175 | * free metadata of current ordered extent, we're OK to exit. |
| 1176 | */ |
| 1177 | if (ret) |
| 1178 | goto out_unlock; |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 1179 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1180 | out: |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1181 | return ret; |
Miao Xie | b7d5b0a | 2012-11-01 07:32:18 +0000 | [diff] [blame] | 1182 | |
Filipe Manana | d9f8596 | 2014-08-25 10:43:00 +0100 | [diff] [blame] | 1183 | out_drop_extent_cache: |
Nikolay Borisov | 6e26c44 | 2020-06-03 08:55:14 +0300 | [diff] [blame] | 1184 | btrfs_drop_extent_cache(inode, start, start + ram_size - 1, 0); |
Liu Bo | ace68ba | 2013-04-22 10:53:47 +0000 | [diff] [blame] | 1185 | out_reserve: |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1186 | btrfs_dec_block_group_reservations(fs_info, ins.objectid); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 1187 | btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1188 | out_unlock: |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 1189 | clear_bits = EXTENT_LOCKED | EXTENT_DELALLOC | EXTENT_DELALLOC_NEW | |
| 1190 | EXTENT_DEFRAG | EXTENT_CLEAR_META_RESV; |
Filipe Manana | a315e68 | 2017-03-06 23:04:20 +0000 | [diff] [blame] | 1191 | page_ops = PAGE_UNLOCK | PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK | |
| 1192 | PAGE_END_WRITEBACK; |
| 1193 | /* |
| 1194 | * If we reserved an extent for our delalloc range (or a subrange) and |
| 1195 | * failed to create the respective ordered extent, then it means that |
| 1196 | * when we reserved the extent we decremented the extent's size from |
| 1197 | * the data space_info's bytes_may_use counter and incremented the |
| 1198 | * space_info's bytes_reserved counter by the same amount. We must make |
| 1199 | * sure extent_clear_unlock_delalloc() does not try to decrement again |
| 1200 | * the data space_info's bytes_may_use counter, therefore we do not pass |
| 1201 | * it the flag EXTENT_CLEAR_DATA_RESV. |
| 1202 | */ |
| 1203 | if (extent_reserved) { |
Nikolay Borisov | 6e26c44 | 2020-06-03 08:55:14 +0300 | [diff] [blame] | 1204 | extent_clear_unlock_delalloc(inode, start, |
Filipe Manana | e2c8e92 | 2020-05-27 11:15:53 +0100 | [diff] [blame] | 1205 | start + cur_alloc_size - 1, |
Filipe Manana | a315e68 | 2017-03-06 23:04:20 +0000 | [diff] [blame] | 1206 | locked_page, |
| 1207 | clear_bits, |
| 1208 | page_ops); |
| 1209 | start += cur_alloc_size; |
| 1210 | if (start >= end) |
| 1211 | goto out; |
| 1212 | } |
Nikolay Borisov | 6e26c44 | 2020-06-03 08:55:14 +0300 | [diff] [blame] | 1213 | extent_clear_unlock_delalloc(inode, start, end, locked_page, |
Filipe Manana | a315e68 | 2017-03-06 23:04:20 +0000 | [diff] [blame] | 1214 | clear_bits | EXTENT_CLEAR_DATA_RESV, |
| 1215 | page_ops); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1216 | goto out; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1217 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1218 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1219 | /* |
| 1220 | * work queue call back to started compression on a file and pages |
| 1221 | */ |
| 1222 | static noinline void async_cow_start(struct btrfs_work *work) |
| 1223 | { |
Nikolay Borisov | b532627 | 2019-03-12 17:20:25 +0200 | [diff] [blame] | 1224 | struct async_chunk *async_chunk; |
Nikolay Borisov | ac3e993 | 2019-07-17 14:41:44 +0300 | [diff] [blame] | 1225 | int compressed_extents; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1226 | |
Nikolay Borisov | b532627 | 2019-03-12 17:20:25 +0200 | [diff] [blame] | 1227 | async_chunk = container_of(work, struct async_chunk, work); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1228 | |
Nikolay Borisov | ac3e993 | 2019-07-17 14:41:44 +0300 | [diff] [blame] | 1229 | compressed_extents = compress_file_range(async_chunk); |
| 1230 | if (compressed_extents == 0) { |
Nikolay Borisov | b532627 | 2019-03-12 17:20:25 +0200 | [diff] [blame] | 1231 | btrfs_add_delayed_iput(async_chunk->inode); |
| 1232 | async_chunk->inode = NULL; |
Josef Bacik | 8180ef8 | 2012-06-08 15:16:12 -0400 | [diff] [blame] | 1233 | } |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1234 | } |
| 1235 | |
| 1236 | /* |
| 1237 | * work queue call back to submit previously compressed pages |
| 1238 | */ |
| 1239 | static noinline void async_cow_submit(struct btrfs_work *work) |
| 1240 | { |
Nikolay Borisov | c5a68ae | 2019-03-12 17:20:26 +0200 | [diff] [blame] | 1241 | struct async_chunk *async_chunk = container_of(work, struct async_chunk, |
| 1242 | work); |
| 1243 | struct btrfs_fs_info *fs_info = btrfs_work_owner(work); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1244 | unsigned long nr_pages; |
| 1245 | |
Nikolay Borisov | b532627 | 2019-03-12 17:20:25 +0200 | [diff] [blame] | 1246 | nr_pages = (async_chunk->end - async_chunk->start + PAGE_SIZE) >> |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1247 | PAGE_SHIFT; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1248 | |
David Sterba | 093258e | 2018-02-26 16:15:17 +0100 | [diff] [blame] | 1249 | /* atomic_sub_return implies a barrier */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1250 | if (atomic_sub_return(nr_pages, &fs_info->async_delalloc_pages) < |
David Sterba | 093258e | 2018-02-26 16:15:17 +0100 | [diff] [blame] | 1251 | 5 * SZ_1M) |
| 1252 | cond_wake_up_nomb(&fs_info->async_submit_wait); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1253 | |
Nikolay Borisov | 4546d17 | 2019-01-03 10:50:03 +0200 | [diff] [blame] | 1254 | /* |
Nikolay Borisov | b532627 | 2019-03-12 17:20:25 +0200 | [diff] [blame] | 1255 | * ->inode could be NULL if async_chunk_start has failed to compress, |
Nikolay Borisov | 4546d17 | 2019-01-03 10:50:03 +0200 | [diff] [blame] | 1256 | * in which case we don't have anything to submit, yet we need to |
| 1257 | * always adjust ->async_delalloc_pages as its paired with the init |
| 1258 | * happening in cow_file_range_async |
| 1259 | */ |
Nikolay Borisov | b532627 | 2019-03-12 17:20:25 +0200 | [diff] [blame] | 1260 | if (async_chunk->inode) |
| 1261 | submit_compressed_extents(async_chunk); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1262 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1263 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1264 | static noinline void async_cow_free(struct btrfs_work *work) |
| 1265 | { |
Nikolay Borisov | b532627 | 2019-03-12 17:20:25 +0200 | [diff] [blame] | 1266 | struct async_chunk *async_chunk; |
Nikolay Borisov | 97db120 | 2019-03-12 17:20:24 +0200 | [diff] [blame] | 1267 | |
Nikolay Borisov | b532627 | 2019-03-12 17:20:25 +0200 | [diff] [blame] | 1268 | async_chunk = container_of(work, struct async_chunk, work); |
| 1269 | if (async_chunk->inode) |
| 1270 | btrfs_add_delayed_iput(async_chunk->inode); |
Chris Mason | ec39f76 | 2019-07-10 12:28:17 -0700 | [diff] [blame] | 1271 | if (async_chunk->blkcg_css) |
| 1272 | css_put(async_chunk->blkcg_css); |
Nikolay Borisov | 97db120 | 2019-03-12 17:20:24 +0200 | [diff] [blame] | 1273 | /* |
| 1274 | * 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] | 1275 | * async_chunk's, freeing it ensures the whole array has been freed. |
Nikolay Borisov | 97db120 | 2019-03-12 17:20:24 +0200 | [diff] [blame] | 1276 | */ |
Nikolay Borisov | b532627 | 2019-03-12 17:20:25 +0200 | [diff] [blame] | 1277 | if (atomic_dec_and_test(async_chunk->pending)) |
Nikolay Borisov | b1c16ac | 2019-04-01 11:29:57 +0300 | [diff] [blame] | 1278 | kvfree(async_chunk->pending); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1279 | } |
| 1280 | |
Nikolay Borisov | 751b643 | 2020-06-03 08:55:22 +0300 | [diff] [blame] | 1281 | static int cow_file_range_async(struct btrfs_inode *inode, |
Chris Mason | ec39f76 | 2019-07-10 12:28:17 -0700 | [diff] [blame] | 1282 | struct writeback_control *wbc, |
| 1283 | struct page *locked_page, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1284 | u64 start, u64 end, int *page_started, |
David Sterba | fac07d2 | 2019-10-29 18:28:57 +0100 | [diff] [blame] | 1285 | unsigned long *nr_written) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1286 | { |
Nikolay Borisov | 751b643 | 2020-06-03 08:55:22 +0300 | [diff] [blame] | 1287 | struct btrfs_fs_info *fs_info = inode->root->fs_info; |
Chris Mason | ec39f76 | 2019-07-10 12:28:17 -0700 | [diff] [blame] | 1288 | struct cgroup_subsys_state *blkcg_css = wbc_blkcg_css(wbc); |
Nikolay Borisov | 97db120 | 2019-03-12 17:20:24 +0200 | [diff] [blame] | 1289 | struct async_cow *ctx; |
| 1290 | struct async_chunk *async_chunk; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1291 | unsigned long nr_pages; |
| 1292 | u64 cur_end; |
Nikolay Borisov | 97db120 | 2019-03-12 17:20:24 +0200 | [diff] [blame] | 1293 | u64 num_chunks = DIV_ROUND_UP(end - start, SZ_512K); |
| 1294 | int i; |
| 1295 | bool should_compress; |
Nikolay Borisov | b1c16ac | 2019-04-01 11:29:57 +0300 | [diff] [blame] | 1296 | unsigned nofs_flag; |
David Sterba | fac07d2 | 2019-10-29 18:28:57 +0100 | [diff] [blame] | 1297 | const unsigned int write_flags = wbc_to_write_flags(wbc); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1298 | |
Nikolay Borisov | 751b643 | 2020-06-03 08:55:22 +0300 | [diff] [blame] | 1299 | unlock_extent(&inode->io_tree, start, end); |
Nikolay Borisov | 97db120 | 2019-03-12 17:20:24 +0200 | [diff] [blame] | 1300 | |
Nikolay Borisov | 751b643 | 2020-06-03 08:55:22 +0300 | [diff] [blame] | 1301 | if (inode->flags & BTRFS_INODE_NOCOMPRESS && |
Nikolay Borisov | 97db120 | 2019-03-12 17:20:24 +0200 | [diff] [blame] | 1302 | !btrfs_test_opt(fs_info, FORCE_COMPRESS)) { |
| 1303 | num_chunks = 1; |
| 1304 | should_compress = false; |
| 1305 | } else { |
| 1306 | should_compress = true; |
| 1307 | } |
| 1308 | |
Nikolay Borisov | b1c16ac | 2019-04-01 11:29:57 +0300 | [diff] [blame] | 1309 | nofs_flag = memalloc_nofs_save(); |
| 1310 | ctx = kvmalloc(struct_size(ctx, chunks, num_chunks), GFP_KERNEL); |
| 1311 | memalloc_nofs_restore(nofs_flag); |
| 1312 | |
Nikolay Borisov | 97db120 | 2019-03-12 17:20:24 +0200 | [diff] [blame] | 1313 | if (!ctx) { |
| 1314 | unsigned clear_bits = EXTENT_LOCKED | EXTENT_DELALLOC | |
| 1315 | EXTENT_DELALLOC_NEW | EXTENT_DEFRAG | |
| 1316 | EXTENT_DO_ACCOUNTING; |
| 1317 | unsigned long page_ops = PAGE_UNLOCK | PAGE_CLEAR_DIRTY | |
| 1318 | PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK | |
| 1319 | PAGE_SET_ERROR; |
| 1320 | |
Nikolay Borisov | 751b643 | 2020-06-03 08:55:22 +0300 | [diff] [blame] | 1321 | extent_clear_unlock_delalloc(inode, start, end, locked_page, |
| 1322 | clear_bits, page_ops); |
Nikolay Borisov | 97db120 | 2019-03-12 17:20:24 +0200 | [diff] [blame] | 1323 | return -ENOMEM; |
| 1324 | } |
| 1325 | |
| 1326 | async_chunk = ctx->chunks; |
| 1327 | atomic_set(&ctx->num_chunks, num_chunks); |
| 1328 | |
| 1329 | for (i = 0; i < num_chunks; i++) { |
| 1330 | if (should_compress) |
| 1331 | cur_end = min(end, start + SZ_512K - 1); |
| 1332 | else |
| 1333 | cur_end = end; |
| 1334 | |
Nikolay Borisov | bd4691a | 2019-01-03 10:50:01 +0200 | [diff] [blame] | 1335 | /* |
| 1336 | * igrab is called higher up in the call chain, take only the |
| 1337 | * lightweight reference for the callback lifetime |
| 1338 | */ |
Nikolay Borisov | 751b643 | 2020-06-03 08:55:22 +0300 | [diff] [blame] | 1339 | ihold(&inode->vfs_inode); |
Nikolay Borisov | 97db120 | 2019-03-12 17:20:24 +0200 | [diff] [blame] | 1340 | async_chunk[i].pending = &ctx->num_chunks; |
Nikolay Borisov | 751b643 | 2020-06-03 08:55:22 +0300 | [diff] [blame] | 1341 | async_chunk[i].inode = &inode->vfs_inode; |
Nikolay Borisov | 97db120 | 2019-03-12 17:20:24 +0200 | [diff] [blame] | 1342 | async_chunk[i].start = start; |
| 1343 | async_chunk[i].end = cur_end; |
Nikolay Borisov | 97db120 | 2019-03-12 17:20:24 +0200 | [diff] [blame] | 1344 | async_chunk[i].write_flags = write_flags; |
| 1345 | INIT_LIST_HEAD(&async_chunk[i].extents); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1346 | |
Chris Mason | 1d53c9e | 2019-07-10 12:28:16 -0700 | [diff] [blame] | 1347 | /* |
| 1348 | * The locked_page comes all the way from writepage and its |
| 1349 | * the original page we were actually given. As we spread |
| 1350 | * this large delalloc region across multiple async_chunk |
| 1351 | * structs, only the first struct needs a pointer to locked_page |
| 1352 | * |
| 1353 | * This way we don't need racey decisions about who is supposed |
| 1354 | * to unlock it. |
| 1355 | */ |
| 1356 | if (locked_page) { |
Chris Mason | ec39f76 | 2019-07-10 12:28:17 -0700 | [diff] [blame] | 1357 | /* |
| 1358 | * Depending on the compressibility, the pages might or |
| 1359 | * might not go through async. We want all of them to |
| 1360 | * be accounted against wbc once. Let's do it here |
| 1361 | * before the paths diverge. wbc accounting is used |
| 1362 | * only for foreign writeback detection and doesn't |
| 1363 | * need full accuracy. Just account the whole thing |
| 1364 | * against the first page. |
| 1365 | */ |
| 1366 | wbc_account_cgroup_owner(wbc, locked_page, |
| 1367 | cur_end - start); |
Chris Mason | 1d53c9e | 2019-07-10 12:28:16 -0700 | [diff] [blame] | 1368 | async_chunk[i].locked_page = locked_page; |
| 1369 | locked_page = NULL; |
| 1370 | } else { |
| 1371 | async_chunk[i].locked_page = NULL; |
| 1372 | } |
| 1373 | |
Chris Mason | ec39f76 | 2019-07-10 12:28:17 -0700 | [diff] [blame] | 1374 | if (blkcg_css != blkcg_root_css) { |
| 1375 | css_get(blkcg_css); |
| 1376 | async_chunk[i].blkcg_css = blkcg_css; |
| 1377 | } else { |
| 1378 | async_chunk[i].blkcg_css = NULL; |
| 1379 | } |
| 1380 | |
Omar Sandoval | a0cac0e | 2019-09-16 11:30:57 -0700 | [diff] [blame] | 1381 | btrfs_init_work(&async_chunk[i].work, async_cow_start, |
| 1382 | async_cow_submit, async_cow_free); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1383 | |
Nikolay Borisov | 97db120 | 2019-03-12 17:20:24 +0200 | [diff] [blame] | 1384 | nr_pages = DIV_ROUND_UP(cur_end - start, PAGE_SIZE); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1385 | atomic_add(nr_pages, &fs_info->async_delalloc_pages); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1386 | |
Nikolay Borisov | 97db120 | 2019-03-12 17:20:24 +0200 | [diff] [blame] | 1387 | btrfs_queue_work(fs_info->delalloc_workers, &async_chunk[i].work); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1388 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1389 | *nr_written += nr_pages; |
| 1390 | start = cur_end + 1; |
| 1391 | } |
| 1392 | *page_started = 1; |
| 1393 | return 0; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1394 | } |
| 1395 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 1396 | 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] | 1397 | u64 bytenr, u64 num_bytes) |
| 1398 | { |
| 1399 | int ret; |
| 1400 | struct btrfs_ordered_sum *sums; |
| 1401 | LIST_HEAD(list); |
| 1402 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1403 | ret = btrfs_lookup_csums_range(fs_info->csum_root, bytenr, |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 1404 | bytenr + num_bytes - 1, &list, 0); |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1405 | if (ret == 0 && list_empty(&list)) |
| 1406 | return 0; |
| 1407 | |
| 1408 | while (!list_empty(&list)) { |
| 1409 | sums = list_entry(list.next, struct btrfs_ordered_sum, list); |
| 1410 | list_del(&sums->list); |
| 1411 | kfree(sums); |
| 1412 | } |
Liu Bo | 5811375 | 2018-01-31 17:09:13 -0700 | [diff] [blame] | 1413 | if (ret < 0) |
| 1414 | return ret; |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1415 | return 1; |
| 1416 | } |
| 1417 | |
Nikolay Borisov | 8ba96f3 | 2020-06-03 08:55:20 +0300 | [diff] [blame] | 1418 | 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] | 1419 | const u64 start, const u64 end, |
| 1420 | int *page_started, unsigned long *nr_written) |
| 1421 | { |
Nikolay Borisov | 8ba96f3 | 2020-06-03 08:55:20 +0300 | [diff] [blame] | 1422 | const bool is_space_ino = btrfs_is_free_space_inode(inode); |
| 1423 | const bool is_reloc_ino = (inode->root->root_key.objectid == |
Filipe Manana | 6bd335b | 2020-06-08 13:33:05 +0100 | [diff] [blame] | 1424 | BTRFS_DATA_RELOC_TREE_OBJECTID); |
Filipe Manana | 2166e5e | 2020-05-27 11:16:19 +0100 | [diff] [blame] | 1425 | const u64 range_bytes = end + 1 - start; |
Nikolay Borisov | 8ba96f3 | 2020-06-03 08:55:20 +0300 | [diff] [blame] | 1426 | struct extent_io_tree *io_tree = &inode->io_tree; |
Filipe Manana | 467dc47 | 2020-05-27 11:16:07 +0100 | [diff] [blame] | 1427 | u64 range_start = start; |
| 1428 | u64 count; |
| 1429 | |
| 1430 | /* |
| 1431 | * If EXTENT_NORESERVE is set it means that when the buffered write was |
| 1432 | * made we had not enough available data space and therefore we did not |
| 1433 | * reserve data space for it, since we though we could do NOCOW for the |
| 1434 | * respective file range (either there is prealloc extent or the inode |
| 1435 | * has the NOCOW bit set). |
| 1436 | * |
| 1437 | * However when we need to fallback to COW mode (because for example the |
| 1438 | * block group for the corresponding extent was turned to RO mode by a |
| 1439 | * scrub or relocation) we need to do the following: |
| 1440 | * |
| 1441 | * 1) We increment the bytes_may_use counter of the data space info. |
| 1442 | * If COW succeeds, it allocates a new data extent and after doing |
| 1443 | * that it decrements the space info's bytes_may_use counter and |
| 1444 | * increments its bytes_reserved counter by the same amount (we do |
| 1445 | * this at btrfs_add_reserved_bytes()). So we need to increment the |
| 1446 | * bytes_may_use counter to compensate (when space is reserved at |
| 1447 | * buffered write time, the bytes_may_use counter is incremented); |
| 1448 | * |
| 1449 | * 2) We clear the EXTENT_NORESERVE bit from the range. We do this so |
| 1450 | * that if the COW path fails for any reason, it decrements (through |
| 1451 | * extent_clear_unlock_delalloc()) the bytes_may_use counter of the |
| 1452 | * data space info, which we incremented in the step above. |
Filipe Manana | 2166e5e | 2020-05-27 11:16:19 +0100 | [diff] [blame] | 1453 | * |
| 1454 | * 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] | 1455 | * space cache inode or an inode of the data relocation tree, we must |
| 1456 | * also increment bytes_may_use of the data space_info for the same |
| 1457 | * reason. Space caches and relocated data extents always get a prealloc |
Filipe Manana | 2166e5e | 2020-05-27 11:16:19 +0100 | [diff] [blame] | 1458 | * extent for them, however scrub or balance may have set the block |
Filipe Manana | 6bd335b | 2020-06-08 13:33:05 +0100 | [diff] [blame] | 1459 | * group that contains that extent to RO mode and therefore force COW |
| 1460 | * when starting writeback. |
Filipe Manana | 467dc47 | 2020-05-27 11:16:07 +0100 | [diff] [blame] | 1461 | */ |
Filipe Manana | 2166e5e | 2020-05-27 11:16:19 +0100 | [diff] [blame] | 1462 | count = count_range_bits(io_tree, &range_start, end, range_bytes, |
Filipe Manana | 467dc47 | 2020-05-27 11:16:07 +0100 | [diff] [blame] | 1463 | EXTENT_NORESERVE, 0); |
Filipe Manana | 6bd335b | 2020-06-08 13:33:05 +0100 | [diff] [blame] | 1464 | if (count > 0 || is_space_ino || is_reloc_ino) { |
| 1465 | u64 bytes = count; |
Nikolay Borisov | 8ba96f3 | 2020-06-03 08:55:20 +0300 | [diff] [blame] | 1466 | struct btrfs_fs_info *fs_info = inode->root->fs_info; |
Filipe Manana | 467dc47 | 2020-05-27 11:16:07 +0100 | [diff] [blame] | 1467 | struct btrfs_space_info *sinfo = fs_info->data_sinfo; |
| 1468 | |
Filipe Manana | 6bd335b | 2020-06-08 13:33:05 +0100 | [diff] [blame] | 1469 | if (is_space_ino || is_reloc_ino) |
| 1470 | bytes = range_bytes; |
| 1471 | |
Filipe Manana | 467dc47 | 2020-05-27 11:16:07 +0100 | [diff] [blame] | 1472 | spin_lock(&sinfo->lock); |
Filipe Manana | 2166e5e | 2020-05-27 11:16:19 +0100 | [diff] [blame] | 1473 | btrfs_space_info_update_bytes_may_use(fs_info, sinfo, bytes); |
Filipe Manana | 467dc47 | 2020-05-27 11:16:07 +0100 | [diff] [blame] | 1474 | spin_unlock(&sinfo->lock); |
| 1475 | |
Filipe Manana | 2166e5e | 2020-05-27 11:16:19 +0100 | [diff] [blame] | 1476 | if (count > 0) |
| 1477 | clear_extent_bit(io_tree, start, end, EXTENT_NORESERVE, |
| 1478 | 0, 0, NULL); |
Filipe Manana | 467dc47 | 2020-05-27 11:16:07 +0100 | [diff] [blame] | 1479 | } |
| 1480 | |
Nikolay Borisov | 8ba96f3 | 2020-06-03 08:55:20 +0300 | [diff] [blame] | 1481 | return cow_file_range(inode, locked_page, start, end, page_started, |
| 1482 | nr_written, 1); |
Filipe Manana | 467dc47 | 2020-05-27 11:16:07 +0100 | [diff] [blame] | 1483 | } |
| 1484 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1485 | /* |
| 1486 | * when nowcow writeback call back. This checks for snapshots or COW copies |
| 1487 | * of the extents that exist in the file, and COWs the file as required. |
| 1488 | * |
| 1489 | * If no cow copies or snapshots exist, we write directly to the existing |
| 1490 | * blocks on disk |
| 1491 | */ |
Nikolay Borisov | 968322c | 2020-06-03 08:55:21 +0300 | [diff] [blame] | 1492 | static noinline int run_delalloc_nocow(struct btrfs_inode *inode, |
Chris Mason | 7f366cf | 2009-03-12 20:12:45 -0400 | [diff] [blame] | 1493 | struct page *locked_page, |
Nikolay Borisov | 3e02484 | 2019-08-21 10:42:03 +0300 | [diff] [blame] | 1494 | const u64 start, const u64 end, |
| 1495 | int *page_started, int force, |
| 1496 | unsigned long *nr_written) |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1497 | { |
Nikolay Borisov | 968322c | 2020-06-03 08:55:21 +0300 | [diff] [blame] | 1498 | struct btrfs_fs_info *fs_info = inode->root->fs_info; |
| 1499 | struct btrfs_root *root = inode->root; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1500 | struct btrfs_path *path; |
Nikolay Borisov | 3e02484 | 2019-08-21 10:42:03 +0300 | [diff] [blame] | 1501 | u64 cow_start = (u64)-1; |
| 1502 | u64 cur_offset = start; |
Robbie Ko | 8ecebf4d | 2018-08-06 10:30:30 +0800 | [diff] [blame] | 1503 | int ret; |
Nikolay Borisov | 3e02484 | 2019-08-21 10:42:03 +0300 | [diff] [blame] | 1504 | bool check_prev = true; |
Nikolay Borisov | 968322c | 2020-06-03 08:55:21 +0300 | [diff] [blame] | 1505 | const bool freespace_inode = btrfs_is_free_space_inode(inode); |
| 1506 | u64 ino = btrfs_ino(inode); |
Nikolay Borisov | 762bf09 | 2019-08-22 17:24:20 +0300 | [diff] [blame] | 1507 | bool nocow = false; |
| 1508 | u64 disk_bytenr = 0; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1509 | |
| 1510 | path = btrfs_alloc_path(); |
Josef Bacik | 17ca04a | 2012-05-31 15:58:55 -0400 | [diff] [blame] | 1511 | if (!path) { |
Nikolay Borisov | 968322c | 2020-06-03 08:55:21 +0300 | [diff] [blame] | 1512 | extent_clear_unlock_delalloc(inode, start, end, locked_page, |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1513 | EXTENT_LOCKED | EXTENT_DELALLOC | |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 1514 | EXTENT_DO_ACCOUNTING | |
| 1515 | EXTENT_DEFRAG, PAGE_UNLOCK | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1516 | PAGE_CLEAR_DIRTY | |
| 1517 | PAGE_SET_WRITEBACK | |
| 1518 | PAGE_END_WRITEBACK); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 1519 | return -ENOMEM; |
Josef Bacik | 17ca04a | 2012-05-31 15:58:55 -0400 | [diff] [blame] | 1520 | } |
Li Zefan | 82d5902 | 2011-04-20 10:33:24 +0800 | [diff] [blame] | 1521 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1522 | while (1) { |
Nikolay Borisov | 3e02484 | 2019-08-21 10:42:03 +0300 | [diff] [blame] | 1523 | struct btrfs_key found_key; |
| 1524 | struct btrfs_file_extent_item *fi; |
| 1525 | struct extent_buffer *leaf; |
| 1526 | u64 extent_end; |
| 1527 | u64 extent_offset; |
Nikolay Borisov | 3e02484 | 2019-08-21 10:42:03 +0300 | [diff] [blame] | 1528 | u64 num_bytes = 0; |
| 1529 | u64 disk_num_bytes; |
Nikolay Borisov | 3e02484 | 2019-08-21 10:42:03 +0300 | [diff] [blame] | 1530 | u64 ram_bytes; |
| 1531 | int extent_type; |
Nikolay Borisov | 762bf09 | 2019-08-22 17:24:20 +0300 | [diff] [blame] | 1532 | |
| 1533 | nocow = false; |
Nikolay Borisov | 3e02484 | 2019-08-21 10:42:03 +0300 | [diff] [blame] | 1534 | |
Liu Bo | e4c3b2d | 2017-01-30 12:25:28 -0800 | [diff] [blame] | 1535 | ret = btrfs_lookup_file_extent(NULL, root, path, ino, |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1536 | cur_offset, 0); |
Josef Bacik | d788a34 | 2013-10-25 16:55:08 -0400 | [diff] [blame] | 1537 | if (ret < 0) |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1538 | goto error; |
Nikolay Borisov | a6bd9cd | 2019-08-21 10:42:57 +0300 | [diff] [blame] | 1539 | |
| 1540 | /* |
| 1541 | * If there is no extent for our range when doing the initial |
| 1542 | * search, then go back to the previous slot as it will be the |
| 1543 | * one containing the search offset |
| 1544 | */ |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1545 | if (ret > 0 && path->slots[0] > 0 && check_prev) { |
| 1546 | leaf = path->nodes[0]; |
| 1547 | btrfs_item_key_to_cpu(leaf, &found_key, |
| 1548 | path->slots[0] - 1); |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 1549 | if (found_key.objectid == ino && |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1550 | found_key.type == BTRFS_EXTENT_DATA_KEY) |
| 1551 | path->slots[0]--; |
| 1552 | } |
Nikolay Borisov | 3e02484 | 2019-08-21 10:42:03 +0300 | [diff] [blame] | 1553 | check_prev = false; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1554 | next_slot: |
Nikolay Borisov | a6bd9cd | 2019-08-21 10:42:57 +0300 | [diff] [blame] | 1555 | /* Go to next leaf if we have exhausted the current one */ |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1556 | leaf = path->nodes[0]; |
| 1557 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { |
| 1558 | ret = btrfs_next_leaf(root, path); |
Liu Bo | e891669 | 2018-01-25 11:02:50 -0700 | [diff] [blame] | 1559 | if (ret < 0) { |
| 1560 | if (cow_start != (u64)-1) |
| 1561 | cur_offset = cow_start; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1562 | goto error; |
Liu Bo | e891669 | 2018-01-25 11:02:50 -0700 | [diff] [blame] | 1563 | } |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1564 | if (ret > 0) |
| 1565 | break; |
| 1566 | leaf = path->nodes[0]; |
| 1567 | } |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1568 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1569 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1570 | |
Nikolay Borisov | a6bd9cd | 2019-08-21 10:42:57 +0300 | [diff] [blame] | 1571 | /* Didn't find anything for our INO */ |
Filipe Manana | 1d512cb | 2015-11-09 00:33:58 +0000 | [diff] [blame] | 1572 | if (found_key.objectid > ino) |
| 1573 | break; |
Nikolay Borisov | a6bd9cd | 2019-08-21 10:42:57 +0300 | [diff] [blame] | 1574 | /* |
| 1575 | * Keep searching until we find an EXTENT_ITEM or there are no |
| 1576 | * more extents for this inode |
| 1577 | */ |
Filipe Manana | 1d512cb | 2015-11-09 00:33:58 +0000 | [diff] [blame] | 1578 | if (WARN_ON_ONCE(found_key.objectid < ino) || |
| 1579 | found_key.type < BTRFS_EXTENT_DATA_KEY) { |
| 1580 | path->slots[0]++; |
| 1581 | goto next_slot; |
| 1582 | } |
Nikolay Borisov | a6bd9cd | 2019-08-21 10:42:57 +0300 | [diff] [blame] | 1583 | |
| 1584 | /* Found key is not EXTENT_DATA_KEY or starts after req range */ |
Filipe Manana | 1d512cb | 2015-11-09 00:33:58 +0000 | [diff] [blame] | 1585 | if (found_key.type > BTRFS_EXTENT_DATA_KEY || |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1586 | found_key.offset > end) |
| 1587 | break; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1588 | |
Nikolay Borisov | a6bd9cd | 2019-08-21 10:42:57 +0300 | [diff] [blame] | 1589 | /* |
| 1590 | * If the found extent starts after requested offset, then |
| 1591 | * adjust extent_end to be right before this extent begins |
| 1592 | */ |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1593 | if (found_key.offset > cur_offset) { |
| 1594 | extent_end = found_key.offset; |
Chris Mason | e9061e2 | 2009-10-09 09:57:45 -0400 | [diff] [blame] | 1595 | extent_type = 0; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1596 | goto out_check; |
| 1597 | } |
Chris Mason | c31f883 | 2008-01-08 15:46:31 -0500 | [diff] [blame] | 1598 | |
Nikolay Borisov | a6bd9cd | 2019-08-21 10:42:57 +0300 | [diff] [blame] | 1599 | /* |
| 1600 | * Found extent which begins before our range and potentially |
| 1601 | * intersect it |
| 1602 | */ |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1603 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 1604 | struct btrfs_file_extent_item); |
| 1605 | extent_type = btrfs_file_extent_type(leaf, fi); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1606 | |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 1607 | ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1608 | if (extent_type == BTRFS_FILE_EXTENT_REG || |
| 1609 | extent_type == BTRFS_FILE_EXTENT_PREALLOC) { |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1610 | disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1611 | extent_offset = btrfs_file_extent_offset(leaf, fi); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1612 | extent_end = found_key.offset + |
| 1613 | btrfs_file_extent_num_bytes(leaf, fi); |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 1614 | disk_num_bytes = |
| 1615 | btrfs_file_extent_disk_num_bytes(leaf, fi); |
Nikolay Borisov | a6bd9cd | 2019-08-21 10:42:57 +0300 | [diff] [blame] | 1616 | /* |
Filipe Manana | de7999a | 2019-12-11 09:01:40 +0000 | [diff] [blame] | 1617 | * If the extent we got ends before our current offset, |
| 1618 | * skip to the next extent. |
Nikolay Borisov | a6bd9cd | 2019-08-21 10:42:57 +0300 | [diff] [blame] | 1619 | */ |
Filipe Manana | de7999a | 2019-12-11 09:01:40 +0000 | [diff] [blame] | 1620 | if (extent_end <= cur_offset) { |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1621 | path->slots[0]++; |
| 1622 | goto next_slot; |
| 1623 | } |
Nikolay Borisov | a6bd9cd | 2019-08-21 10:42:57 +0300 | [diff] [blame] | 1624 | /* Skip holes */ |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1625 | if (disk_bytenr == 0) |
| 1626 | goto out_check; |
Nikolay Borisov | a6bd9cd | 2019-08-21 10:42:57 +0300 | [diff] [blame] | 1627 | /* Skip compressed/encrypted/encoded extents */ |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1628 | if (btrfs_file_extent_compression(leaf, fi) || |
| 1629 | btrfs_file_extent_encryption(leaf, fi) || |
| 1630 | btrfs_file_extent_other_encoding(leaf, fi)) |
| 1631 | goto out_check; |
Ethan Lien | 78d4295 | 2018-05-17 14:58:29 +0800 | [diff] [blame] | 1632 | /* |
Nikolay Borisov | a6bd9cd | 2019-08-21 10:42:57 +0300 | [diff] [blame] | 1633 | * If extent is created before the last volume's snapshot |
| 1634 | * this implies the extent is shared, hence we can't do |
| 1635 | * nocow. This is the same check as in |
| 1636 | * btrfs_cross_ref_exist but without calling |
| 1637 | * btrfs_search_slot. |
Ethan Lien | 78d4295 | 2018-05-17 14:58:29 +0800 | [diff] [blame] | 1638 | */ |
Nikolay Borisov | 3e02484 | 2019-08-21 10:42:03 +0300 | [diff] [blame] | 1639 | if (!freespace_inode && |
Lu Fengqi | 27a7ff5 | 2018-11-29 17:31:32 +0800 | [diff] [blame] | 1640 | btrfs_file_extent_generation(leaf, fi) <= |
Ethan Lien | 78d4295 | 2018-05-17 14:58:29 +0800 | [diff] [blame] | 1641 | btrfs_root_last_snapshot(&root->root_item)) |
| 1642 | goto out_check; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1643 | if (extent_type == BTRFS_FILE_EXTENT_REG && !force) |
| 1644 | goto out_check; |
Nikolay Borisov | a6bd9cd | 2019-08-21 10:42:57 +0300 | [diff] [blame] | 1645 | /* If extent is RO, we must COW it */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 1646 | if (btrfs_extent_readonly(fs_info, disk_bytenr)) |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1647 | goto out_check; |
Liu Bo | 5811375 | 2018-01-31 17:09:13 -0700 | [diff] [blame] | 1648 | ret = btrfs_cross_ref_exist(root, ino, |
| 1649 | found_key.offset - |
Boris Burkov | a84d5d4 | 2020-08-18 11:00:05 -0700 | [diff] [blame] | 1650 | extent_offset, disk_bytenr, false); |
Liu Bo | 5811375 | 2018-01-31 17:09:13 -0700 | [diff] [blame] | 1651 | if (ret) { |
| 1652 | /* |
| 1653 | * ret could be -EIO if the above fails to read |
| 1654 | * metadata. |
| 1655 | */ |
| 1656 | if (ret < 0) { |
| 1657 | if (cow_start != (u64)-1) |
| 1658 | cur_offset = cow_start; |
| 1659 | goto error; |
| 1660 | } |
| 1661 | |
Nikolay Borisov | 3e02484 | 2019-08-21 10:42:03 +0300 | [diff] [blame] | 1662 | WARN_ON_ONCE(freespace_inode); |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1663 | goto out_check; |
Liu Bo | 5811375 | 2018-01-31 17:09:13 -0700 | [diff] [blame] | 1664 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1665 | disk_bytenr += extent_offset; |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1666 | disk_bytenr += cur_offset - found_key.offset; |
| 1667 | num_bytes = min(end + 1, extent_end) - cur_offset; |
| 1668 | /* |
Nikolay Borisov | a6bd9cd | 2019-08-21 10:42:57 +0300 | [diff] [blame] | 1669 | * If there are pending snapshots for this root, we |
| 1670 | * fall into common COW way |
Wang Shilong | e9894fd | 2014-03-27 11:12:25 +0800 | [diff] [blame] | 1671 | */ |
Nikolay Borisov | 3e02484 | 2019-08-21 10:42:03 +0300 | [diff] [blame] | 1672 | if (!freespace_inode && atomic_read(&root->snapshot_force_cow)) |
Robbie Ko | 8ecebf4d | 2018-08-06 10:30:30 +0800 | [diff] [blame] | 1673 | goto out_check; |
Wang Shilong | e9894fd | 2014-03-27 11:12:25 +0800 | [diff] [blame] | 1674 | /* |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1675 | * force cow if csum exists in the range. |
| 1676 | * this ensure that csum for a given extent are |
| 1677 | * either valid or do not exist. |
| 1678 | */ |
Liu Bo | 5811375 | 2018-01-31 17:09:13 -0700 | [diff] [blame] | 1679 | ret = csum_exist_in_range(fs_info, disk_bytenr, |
| 1680 | num_bytes); |
| 1681 | if (ret) { |
Liu Bo | 5811375 | 2018-01-31 17:09:13 -0700 | [diff] [blame] | 1682 | /* |
| 1683 | * ret could be -EIO if the above fails to read |
| 1684 | * metadata. |
| 1685 | */ |
| 1686 | if (ret < 0) { |
| 1687 | if (cow_start != (u64)-1) |
| 1688 | cur_offset = cow_start; |
| 1689 | goto error; |
| 1690 | } |
Nikolay Borisov | 3e02484 | 2019-08-21 10:42:03 +0300 | [diff] [blame] | 1691 | WARN_ON_ONCE(freespace_inode); |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1692 | goto out_check; |
Robbie Ko | 91e1f56 | 2016-10-07 10:01:29 +0800 | [diff] [blame] | 1693 | } |
Robbie Ko | 8ecebf4d | 2018-08-06 10:30:30 +0800 | [diff] [blame] | 1694 | if (!btrfs_inc_nocow_writers(fs_info, disk_bytenr)) |
Filipe Manana | f78c436 | 2016-05-09 13:15:41 +0100 | [diff] [blame] | 1695 | goto out_check; |
Nikolay Borisov | 3e02484 | 2019-08-21 10:42:03 +0300 | [diff] [blame] | 1696 | nocow = true; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1697 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
Nikolay Borisov | e8e2100 | 2019-08-22 17:25:23 +0300 | [diff] [blame] | 1698 | extent_end = found_key.offset + ram_bytes; |
| 1699 | extent_end = ALIGN(extent_end, fs_info->sectorsize); |
Nikolay Borisov | 922f051 | 2019-08-05 17:47:06 +0300 | [diff] [blame] | 1700 | /* Skip extents outside of our requested range */ |
| 1701 | if (extent_end <= start) { |
| 1702 | path->slots[0]++; |
| 1703 | goto next_slot; |
| 1704 | } |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1705 | } else { |
Nikolay Borisov | e8e2100 | 2019-08-22 17:25:23 +0300 | [diff] [blame] | 1706 | /* If this triggers then we have a memory corruption */ |
Arnd Bergmann | 290342f | 2019-03-25 14:02:25 +0100 | [diff] [blame] | 1707 | BUG(); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1708 | } |
| 1709 | out_check: |
Nikolay Borisov | a6bd9cd | 2019-08-21 10:42:57 +0300 | [diff] [blame] | 1710 | /* |
| 1711 | * If nocow is false then record the beginning of the range |
| 1712 | * that needs to be COWed |
| 1713 | */ |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1714 | if (!nocow) { |
| 1715 | if (cow_start == (u64)-1) |
| 1716 | cow_start = cur_offset; |
| 1717 | cur_offset = extent_end; |
| 1718 | if (cur_offset > end) |
| 1719 | break; |
| 1720 | path->slots[0]++; |
| 1721 | goto next_slot; |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1722 | } |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1723 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1724 | btrfs_release_path(path); |
Nikolay Borisov | a6bd9cd | 2019-08-21 10:42:57 +0300 | [diff] [blame] | 1725 | |
| 1726 | /* |
| 1727 | * COW range from cow_start to found_key.offset - 1. As the key |
| 1728 | * will contain the beginning of the first extent that can be |
| 1729 | * NOCOW, following one which needs to be COW'ed |
| 1730 | */ |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1731 | if (cow_start != (u64)-1) { |
Nikolay Borisov | 968322c | 2020-06-03 08:55:21 +0300 | [diff] [blame] | 1732 | ret = fallback_to_cow(inode, locked_page, |
Nikolay Borisov | 8ba96f3 | 2020-06-03 08:55:20 +0300 | [diff] [blame] | 1733 | cow_start, found_key.offset - 1, |
Filipe Manana | 467dc47 | 2020-05-27 11:16:07 +0100 | [diff] [blame] | 1734 | page_started, nr_written); |
Josef Bacik | 230ed39 | 2020-07-06 09:14:12 -0400 | [diff] [blame] | 1735 | if (ret) |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1736 | goto error; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1737 | cow_start = (u64)-1; |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1738 | } |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1739 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1740 | if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) { |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 1741 | u64 orig_start = found_key.offset - extent_offset; |
Nikolay Borisov | 3e02484 | 2019-08-21 10:42:03 +0300 | [diff] [blame] | 1742 | struct extent_map *em; |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 1743 | |
Nikolay Borisov | 968322c | 2020-06-03 08:55:21 +0300 | [diff] [blame] | 1744 | em = create_io_em(inode, cur_offset, num_bytes, |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 1745 | orig_start, |
| 1746 | disk_bytenr, /* block_start */ |
| 1747 | num_bytes, /* block_len */ |
| 1748 | disk_num_bytes, /* orig_block_len */ |
| 1749 | ram_bytes, BTRFS_COMPRESS_NONE, |
| 1750 | BTRFS_ORDERED_PREALLOC); |
| 1751 | if (IS_ERR(em)) { |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 1752 | ret = PTR_ERR(em); |
| 1753 | goto error; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1754 | } |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 1755 | free_extent_map(em); |
Nikolay Borisov | 968322c | 2020-06-03 08:55:21 +0300 | [diff] [blame] | 1756 | ret = btrfs_add_ordered_extent(inode, cur_offset, |
Nikolay Borisov | bb55f62 | 2019-08-05 17:47:05 +0300 | [diff] [blame] | 1757 | disk_bytenr, num_bytes, |
| 1758 | num_bytes, |
| 1759 | BTRFS_ORDERED_PREALLOC); |
Nikolay Borisov | 762bf09 | 2019-08-22 17:24:20 +0300 | [diff] [blame] | 1760 | if (ret) { |
Nikolay Borisov | 968322c | 2020-06-03 08:55:21 +0300 | [diff] [blame] | 1761 | btrfs_drop_extent_cache(inode, cur_offset, |
Nikolay Borisov | 762bf09 | 2019-08-22 17:24:20 +0300 | [diff] [blame] | 1762 | cur_offset + num_bytes - 1, |
| 1763 | 0); |
| 1764 | goto error; |
| 1765 | } |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1766 | } else { |
Nikolay Borisov | 968322c | 2020-06-03 08:55:21 +0300 | [diff] [blame] | 1767 | ret = btrfs_add_ordered_extent(inode, cur_offset, |
Nikolay Borisov | bb55f62 | 2019-08-05 17:47:05 +0300 | [diff] [blame] | 1768 | disk_bytenr, num_bytes, |
| 1769 | num_bytes, |
| 1770 | BTRFS_ORDERED_NOCOW); |
Nikolay Borisov | 762bf09 | 2019-08-22 17:24:20 +0300 | [diff] [blame] | 1771 | if (ret) |
| 1772 | goto error; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1773 | } |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1774 | |
Filipe Manana | f78c436 | 2016-05-09 13:15:41 +0100 | [diff] [blame] | 1775 | if (nocow) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1776 | btrfs_dec_nocow_writers(fs_info, disk_bytenr); |
Nikolay Borisov | 762bf09 | 2019-08-22 17:24:20 +0300 | [diff] [blame] | 1777 | nocow = false; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1778 | |
Yan, Zheng | efa5646 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 1779 | if (root->root_key.objectid == |
Qu Wenruo | 4dbd80f | 2017-03-08 10:25:51 +0800 | [diff] [blame] | 1780 | BTRFS_DATA_RELOC_TREE_OBJECTID) |
| 1781 | /* |
| 1782 | * Error handled later, as we must prevent |
| 1783 | * extent_clear_unlock_delalloc() in error handler |
| 1784 | * from freeing metadata of created ordered extent. |
| 1785 | */ |
Nikolay Borisov | 968322c | 2020-06-03 08:55:21 +0300 | [diff] [blame] | 1786 | ret = btrfs_reloc_clone_csums(inode, cur_offset, |
Yan, Zheng | efa5646 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 1787 | num_bytes); |
Yan, Zheng | efa5646 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 1788 | |
Nikolay Borisov | 968322c | 2020-06-03 08:55:21 +0300 | [diff] [blame] | 1789 | extent_clear_unlock_delalloc(inode, cur_offset, |
Nikolay Borisov | 74e9194 | 2019-07-17 16:18:16 +0300 | [diff] [blame] | 1790 | cur_offset + num_bytes - 1, |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1791 | locked_page, EXTENT_LOCKED | |
Wang Xiaoguang | 1851309 | 2016-07-25 15:51:40 +0800 | [diff] [blame] | 1792 | EXTENT_DELALLOC | |
| 1793 | EXTENT_CLEAR_DATA_RESV, |
| 1794 | PAGE_UNLOCK | PAGE_SET_PRIVATE2); |
| 1795 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1796 | cur_offset = extent_end; |
Qu Wenruo | 4dbd80f | 2017-03-08 10:25:51 +0800 | [diff] [blame] | 1797 | |
| 1798 | /* |
| 1799 | * btrfs_reloc_clone_csums() error, now we're OK to call error |
| 1800 | * handler, as metadata for created ordered extent will only |
| 1801 | * be freed by btrfs_finish_ordered_io(). |
| 1802 | */ |
| 1803 | if (ret) |
| 1804 | goto error; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1805 | if (cur_offset > end) |
| 1806 | break; |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1807 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1808 | btrfs_release_path(path); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1809 | |
Robbie Ko | 506481b | 2018-10-30 18:04:04 +0800 | [diff] [blame] | 1810 | if (cur_offset <= end && cow_start == (u64)-1) |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1811 | cow_start = cur_offset; |
Josef Bacik | 17ca04a | 2012-05-31 15:58:55 -0400 | [diff] [blame] | 1812 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1813 | if (cow_start != (u64)-1) { |
Robbie Ko | 506481b | 2018-10-30 18:04:04 +0800 | [diff] [blame] | 1814 | cur_offset = end; |
Nikolay Borisov | 968322c | 2020-06-03 08:55:21 +0300 | [diff] [blame] | 1815 | ret = fallback_to_cow(inode, locked_page, cow_start, end, |
| 1816 | page_started, nr_written); |
Josef Bacik | d788a34 | 2013-10-25 16:55:08 -0400 | [diff] [blame] | 1817 | if (ret) |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1818 | goto error; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1819 | } |
| 1820 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1821 | error: |
Nikolay Borisov | 762bf09 | 2019-08-22 17:24:20 +0300 | [diff] [blame] | 1822 | if (nocow) |
| 1823 | btrfs_dec_nocow_writers(fs_info, disk_bytenr); |
| 1824 | |
Josef Bacik | 17ca04a | 2012-05-31 15:58:55 -0400 | [diff] [blame] | 1825 | if (ret && cur_offset < end) |
Nikolay Borisov | 968322c | 2020-06-03 08:55:21 +0300 | [diff] [blame] | 1826 | extent_clear_unlock_delalloc(inode, cur_offset, end, |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1827 | locked_page, EXTENT_LOCKED | |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 1828 | EXTENT_DELALLOC | EXTENT_DEFRAG | |
| 1829 | EXTENT_DO_ACCOUNTING, PAGE_UNLOCK | |
| 1830 | PAGE_CLEAR_DIRTY | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1831 | PAGE_SET_WRITEBACK | |
| 1832 | PAGE_END_WRITEBACK); |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1833 | btrfs_free_path(path); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1834 | return ret; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1835 | } |
| 1836 | |
Nikolay Borisov | 0c49422 | 2020-06-03 08:55:28 +0300 | [diff] [blame] | 1837 | 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] | 1838 | { |
| 1839 | |
Nikolay Borisov | 0c49422 | 2020-06-03 08:55:28 +0300 | [diff] [blame] | 1840 | if (!(inode->flags & BTRFS_INODE_NODATACOW) && |
| 1841 | !(inode->flags & BTRFS_INODE_PREALLOC)) |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 1842 | return 0; |
| 1843 | |
| 1844 | /* |
| 1845 | * @defrag_bytes is a hint value, no spinlock held here, |
| 1846 | * if is not zero, it means the file is defragging. |
| 1847 | * Force cow if given extent needs to be defragged. |
| 1848 | */ |
Nikolay Borisov | 0c49422 | 2020-06-03 08:55:28 +0300 | [diff] [blame] | 1849 | if (inode->defrag_bytes && |
| 1850 | test_range_bit(&inode->io_tree, start, end, EXTENT_DEFRAG, 0, NULL)) |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 1851 | return 1; |
| 1852 | |
| 1853 | return 0; |
| 1854 | } |
| 1855 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1856 | /* |
Nikolay Borisov | 5eaad97 | 2018-11-01 14:09:46 +0200 | [diff] [blame] | 1857 | * Function to process delayed allocation (create CoW) for ranges which are |
| 1858 | * being touched for the first time. |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1859 | */ |
Nikolay Borisov | 98456b9 | 2020-06-03 08:55:29 +0300 | [diff] [blame] | 1860 | 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] | 1861 | u64 start, u64 end, int *page_started, unsigned long *nr_written, |
| 1862 | struct writeback_control *wbc) |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1863 | { |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1864 | int ret; |
Nikolay Borisov | 98456b9 | 2020-06-03 08:55:29 +0300 | [diff] [blame] | 1865 | int force_cow = need_force_cow(inode, start, end); |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1866 | |
Nikolay Borisov | 98456b9 | 2020-06-03 08:55:29 +0300 | [diff] [blame] | 1867 | if (inode->flags & BTRFS_INODE_NODATACOW && !force_cow) { |
| 1868 | ret = run_delalloc_nocow(inode, locked_page, start, end, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1869 | page_started, 1, nr_written); |
Nikolay Borisov | 98456b9 | 2020-06-03 08:55:29 +0300 | [diff] [blame] | 1870 | } else if (inode->flags & BTRFS_INODE_PREALLOC && !force_cow) { |
| 1871 | ret = run_delalloc_nocow(inode, locked_page, start, end, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1872 | page_started, 0, nr_written); |
Nikolay Borisov | 98456b9 | 2020-06-03 08:55:29 +0300 | [diff] [blame] | 1873 | } else if (!inode_can_compress(inode) || |
| 1874 | !inode_need_compress(inode, start, end)) { |
| 1875 | ret = cow_file_range(inode, locked_page, start, end, |
| 1876 | page_started, nr_written, 1); |
Josef Bacik | 7ddf5a4 | 2012-06-08 15:26:47 -0400 | [diff] [blame] | 1877 | } else { |
Nikolay Borisov | 98456b9 | 2020-06-03 08:55:29 +0300 | [diff] [blame] | 1878 | set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, &inode->runtime_flags); |
| 1879 | ret = cow_file_range_async(inode, wbc, locked_page, start, end, |
David Sterba | fac07d2 | 2019-10-29 18:28:57 +0100 | [diff] [blame] | 1880 | page_started, nr_written); |
Josef Bacik | 7ddf5a4 | 2012-06-08 15:26:47 -0400 | [diff] [blame] | 1881 | } |
Qu Wenruo | 52427260 | 2017-03-08 10:25:52 +0800 | [diff] [blame] | 1882 | if (ret) |
Nikolay Borisov | 98456b9 | 2020-06-03 08:55:29 +0300 | [diff] [blame] | 1883 | btrfs_cleanup_ordered_extents(inode, locked_page, start, |
Nikolay Borisov | d1051d6 | 2018-11-21 17:10:52 +0200 | [diff] [blame] | 1884 | end - start + 1); |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 1885 | return ret; |
| 1886 | } |
| 1887 | |
Nikolay Borisov | abbb55f | 2018-11-01 14:09:53 +0200 | [diff] [blame] | 1888 | void btrfs_split_delalloc_extent(struct inode *inode, |
| 1889 | struct extent_state *orig, u64 split) |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1890 | { |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1891 | u64 size; |
| 1892 | |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1893 | /* not delalloc, ignore it */ |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1894 | if (!(orig->state & EXTENT_DELALLOC)) |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 1895 | return; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1896 | |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1897 | size = orig->end - orig->start + 1; |
| 1898 | if (size > BTRFS_MAX_EXTENT_SIZE) { |
David Sterba | 823bb20 | 2017-01-04 11:09:51 +0100 | [diff] [blame] | 1899 | u32 num_extents; |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1900 | u64 new_size; |
| 1901 | |
| 1902 | /* |
Nikolay Borisov | 5c84819 | 2018-11-01 14:09:52 +0200 | [diff] [blame] | 1903 | * See the explanation in btrfs_merge_delalloc_extent, the same |
Josef Bacik | ba11721 | 2015-03-13 15:01:24 -0400 | [diff] [blame] | 1904 | * applies here, just in reverse. |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1905 | */ |
| 1906 | new_size = orig->end - split + 1; |
David Sterba | 823bb20 | 2017-01-04 11:09:51 +0100 | [diff] [blame] | 1907 | num_extents = count_max_extents(new_size); |
Josef Bacik | ba11721 | 2015-03-13 15:01:24 -0400 | [diff] [blame] | 1908 | new_size = split - orig->start; |
David Sterba | 823bb20 | 2017-01-04 11:09:51 +0100 | [diff] [blame] | 1909 | num_extents += count_max_extents(new_size); |
| 1910 | if (count_max_extents(size) >= num_extents) |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1911 | return; |
| 1912 | } |
| 1913 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1914 | spin_lock(&BTRFS_I(inode)->lock); |
Josef Bacik | 8b62f87 | 2017-10-19 14:15:55 -0400 | [diff] [blame] | 1915 | btrfs_mod_outstanding_extents(BTRFS_I(inode), 1); |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1916 | spin_unlock(&BTRFS_I(inode)->lock); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1917 | } |
| 1918 | |
| 1919 | /* |
Nikolay Borisov | 5c84819 | 2018-11-01 14:09:52 +0200 | [diff] [blame] | 1920 | * Handle merged delayed allocation extents so we can keep track of new extents |
| 1921 | * that are just merged onto old extents, such as when we are doing sequential |
| 1922 | * 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] | 1923 | */ |
Nikolay Borisov | 5c84819 | 2018-11-01 14:09:52 +0200 | [diff] [blame] | 1924 | void btrfs_merge_delalloc_extent(struct inode *inode, struct extent_state *new, |
| 1925 | struct extent_state *other) |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1926 | { |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1927 | u64 new_size, old_size; |
David Sterba | 823bb20 | 2017-01-04 11:09:51 +0100 | [diff] [blame] | 1928 | u32 num_extents; |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1929 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1930 | /* not delalloc, ignore it */ |
| 1931 | if (!(other->state & EXTENT_DELALLOC)) |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 1932 | return; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1933 | |
Josef Bacik | 8461a3d | 2015-03-13 15:12:08 -0400 | [diff] [blame] | 1934 | if (new->start > other->start) |
| 1935 | new_size = new->end - other->start + 1; |
| 1936 | else |
| 1937 | new_size = other->end - new->start + 1; |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1938 | |
| 1939 | /* we're not bigger than the max, unreserve the space and go */ |
| 1940 | if (new_size <= BTRFS_MAX_EXTENT_SIZE) { |
| 1941 | spin_lock(&BTRFS_I(inode)->lock); |
Josef Bacik | 8b62f87 | 2017-10-19 14:15:55 -0400 | [diff] [blame] | 1942 | btrfs_mod_outstanding_extents(BTRFS_I(inode), -1); |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1943 | spin_unlock(&BTRFS_I(inode)->lock); |
| 1944 | return; |
| 1945 | } |
| 1946 | |
| 1947 | /* |
Josef Bacik | ba11721 | 2015-03-13 15:01:24 -0400 | [diff] [blame] | 1948 | * We have to add up either side to figure out how many extents were |
| 1949 | * accounted for before we merged into one big extent. If the number of |
| 1950 | * extents we accounted for is <= the amount we need for the new range |
| 1951 | * then we can return, otherwise drop. Think of it like this |
| 1952 | * |
| 1953 | * [ 4k][MAX_SIZE] |
| 1954 | * |
| 1955 | * So we've grown the extent by a MAX_SIZE extent, this would mean we |
| 1956 | * need 2 outstanding extents, on one side we have 1 and the other side |
| 1957 | * we have 1 so they are == and we can return. But in this case |
| 1958 | * |
| 1959 | * [MAX_SIZE+4k][MAX_SIZE+4k] |
| 1960 | * |
| 1961 | * Each range on their own accounts for 2 extents, but merged together |
| 1962 | * they are only 3 extents worth of accounting, so we need to drop in |
| 1963 | * this case. |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1964 | */ |
Josef Bacik | ba11721 | 2015-03-13 15:01:24 -0400 | [diff] [blame] | 1965 | old_size = other->end - other->start + 1; |
David Sterba | 823bb20 | 2017-01-04 11:09:51 +0100 | [diff] [blame] | 1966 | num_extents = count_max_extents(old_size); |
Josef Bacik | ba11721 | 2015-03-13 15:01:24 -0400 | [diff] [blame] | 1967 | old_size = new->end - new->start + 1; |
David Sterba | 823bb20 | 2017-01-04 11:09:51 +0100 | [diff] [blame] | 1968 | num_extents += count_max_extents(old_size); |
| 1969 | if (count_max_extents(new_size) >= num_extents) |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1970 | return; |
| 1971 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1972 | spin_lock(&BTRFS_I(inode)->lock); |
Josef Bacik | 8b62f87 | 2017-10-19 14:15:55 -0400 | [diff] [blame] | 1973 | btrfs_mod_outstanding_extents(BTRFS_I(inode), -1); |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1974 | spin_unlock(&BTRFS_I(inode)->lock); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1975 | } |
| 1976 | |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 1977 | static void btrfs_add_delalloc_inodes(struct btrfs_root *root, |
| 1978 | struct inode *inode) |
| 1979 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1980 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
| 1981 | |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 1982 | spin_lock(&root->delalloc_lock); |
| 1983 | if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) { |
| 1984 | list_add_tail(&BTRFS_I(inode)->delalloc_inodes, |
| 1985 | &root->delalloc_inodes); |
| 1986 | set_bit(BTRFS_INODE_IN_DELALLOC_LIST, |
| 1987 | &BTRFS_I(inode)->runtime_flags); |
| 1988 | root->nr_delalloc_inodes++; |
| 1989 | if (root->nr_delalloc_inodes == 1) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1990 | spin_lock(&fs_info->delalloc_root_lock); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 1991 | BUG_ON(!list_empty(&root->delalloc_root)); |
| 1992 | list_add_tail(&root->delalloc_root, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1993 | &fs_info->delalloc_roots); |
| 1994 | spin_unlock(&fs_info->delalloc_root_lock); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 1995 | } |
| 1996 | } |
| 1997 | spin_unlock(&root->delalloc_lock); |
| 1998 | } |
| 1999 | |
Nikolay Borisov | 2b87733 | 2018-04-27 12:21:51 +0300 | [diff] [blame] | 2000 | |
| 2001 | void __btrfs_del_delalloc_inode(struct btrfs_root *root, |
| 2002 | struct btrfs_inode *inode) |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 2003 | { |
David Sterba | 3ffbd68 | 2018-06-29 10:56:42 +0200 | [diff] [blame] | 2004 | struct btrfs_fs_info *fs_info = root->fs_info; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2005 | |
Nikolay Borisov | 9e3e97f | 2017-02-20 13:51:07 +0200 | [diff] [blame] | 2006 | if (!list_empty(&inode->delalloc_inodes)) { |
| 2007 | list_del_init(&inode->delalloc_inodes); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 2008 | clear_bit(BTRFS_INODE_IN_DELALLOC_LIST, |
Nikolay Borisov | 9e3e97f | 2017-02-20 13:51:07 +0200 | [diff] [blame] | 2009 | &inode->runtime_flags); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 2010 | root->nr_delalloc_inodes--; |
| 2011 | if (!root->nr_delalloc_inodes) { |
Nikolay Borisov | 7c8a0d3 | 2018-04-27 12:21:52 +0300 | [diff] [blame] | 2012 | ASSERT(list_empty(&root->delalloc_inodes)); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2013 | spin_lock(&fs_info->delalloc_root_lock); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 2014 | BUG_ON(list_empty(&root->delalloc_root)); |
| 2015 | list_del_init(&root->delalloc_root); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2016 | spin_unlock(&fs_info->delalloc_root_lock); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 2017 | } |
| 2018 | } |
Nikolay Borisov | 2b87733 | 2018-04-27 12:21:51 +0300 | [diff] [blame] | 2019 | } |
| 2020 | |
| 2021 | static void btrfs_del_delalloc_inode(struct btrfs_root *root, |
| 2022 | struct btrfs_inode *inode) |
| 2023 | { |
| 2024 | spin_lock(&root->delalloc_lock); |
| 2025 | __btrfs_del_delalloc_inode(root, inode); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 2026 | spin_unlock(&root->delalloc_lock); |
| 2027 | } |
| 2028 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2029 | /* |
Nikolay Borisov | e06a1fc | 2018-11-01 14:09:50 +0200 | [diff] [blame] | 2030 | * Properly track delayed allocation bytes in the inode and to maintain the |
| 2031 | * list of inodes that have pending delalloc work to be done. |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2032 | */ |
Nikolay Borisov | e06a1fc | 2018-11-01 14:09:50 +0200 | [diff] [blame] | 2033 | void btrfs_set_delalloc_extent(struct inode *inode, struct extent_state *state, |
| 2034 | unsigned *bits) |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 2035 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2036 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
| 2037 | |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 2038 | if ((*bits & EXTENT_DEFRAG) && !(*bits & EXTENT_DELALLOC)) |
| 2039 | WARN_ON(1); |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 2040 | /* |
| 2041 | * set_bit and clear bit hooks normally require _irqsave/restore |
Sergei Trofimovich | 27160b6 | 2011-05-20 20:20:32 +0000 | [diff] [blame] | 2042 | * but in this case, we are only testing for the DELALLOC |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 2043 | * bit, which is only set or cleared with irqs on |
| 2044 | */ |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 2045 | if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) { |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 2046 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 2047 | u64 len = state->end + 1 - state->start; |
Josef Bacik | 8b62f87 | 2017-10-19 14:15:55 -0400 | [diff] [blame] | 2048 | u32 num_extents = count_max_extents(len); |
Nikolay Borisov | 70ddc55 | 2017-02-20 13:50:35 +0200 | [diff] [blame] | 2049 | bool do_list = !btrfs_is_free_space_inode(BTRFS_I(inode)); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 2050 | |
Josef Bacik | 8b62f87 | 2017-10-19 14:15:55 -0400 | [diff] [blame] | 2051 | spin_lock(&BTRFS_I(inode)->lock); |
| 2052 | btrfs_mod_outstanding_extents(BTRFS_I(inode), num_extents); |
| 2053 | spin_unlock(&BTRFS_I(inode)->lock); |
Josef Bacik | 287a0ab | 2010-03-19 18:07:23 +0000 | [diff] [blame] | 2054 | |
Josef Bacik | 6a3891c | 2015-03-16 17:38:52 -0400 | [diff] [blame] | 2055 | /* For sanity tests */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2056 | if (btrfs_is_testing(fs_info)) |
Josef Bacik | 6a3891c | 2015-03-16 17:38:52 -0400 | [diff] [blame] | 2057 | return; |
| 2058 | |
Nikolay Borisov | 104b4e5 | 2017-06-20 21:01:20 +0300 | [diff] [blame] | 2059 | percpu_counter_add_batch(&fs_info->delalloc_bytes, len, |
| 2060 | fs_info->delalloc_batch); |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 2061 | spin_lock(&BTRFS_I(inode)->lock); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 2062 | BTRFS_I(inode)->delalloc_bytes += len; |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 2063 | if (*bits & EXTENT_DEFRAG) |
| 2064 | BTRFS_I(inode)->defrag_bytes += len; |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 2065 | if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST, |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 2066 | &BTRFS_I(inode)->runtime_flags)) |
| 2067 | btrfs_add_delalloc_inodes(root, inode); |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 2068 | spin_unlock(&BTRFS_I(inode)->lock); |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 2069 | } |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 2070 | |
| 2071 | if (!(state->state & EXTENT_DELALLOC_NEW) && |
| 2072 | (*bits & EXTENT_DELALLOC_NEW)) { |
| 2073 | spin_lock(&BTRFS_I(inode)->lock); |
| 2074 | BTRFS_I(inode)->new_delalloc_bytes += state->end + 1 - |
| 2075 | state->start; |
| 2076 | spin_unlock(&BTRFS_I(inode)->lock); |
| 2077 | } |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 2078 | } |
| 2079 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2080 | /* |
Nikolay Borisov | a36bb5f | 2018-11-01 14:09:51 +0200 | [diff] [blame] | 2081 | * Once a range is no longer delalloc this function ensures that proper |
| 2082 | * accounting happens. |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2083 | */ |
Nikolay Borisov | a36bb5f | 2018-11-01 14:09:51 +0200 | [diff] [blame] | 2084 | void btrfs_clear_delalloc_extent(struct inode *vfs_inode, |
| 2085 | struct extent_state *state, unsigned *bits) |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 2086 | { |
Nikolay Borisov | a36bb5f | 2018-11-01 14:09:51 +0200 | [diff] [blame] | 2087 | struct btrfs_inode *inode = BTRFS_I(vfs_inode); |
| 2088 | struct btrfs_fs_info *fs_info = btrfs_sb(vfs_inode->i_sb); |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 2089 | u64 len = state->end + 1 - state->start; |
David Sterba | 823bb20 | 2017-01-04 11:09:51 +0100 | [diff] [blame] | 2090 | u32 num_extents = count_max_extents(len); |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 2091 | |
Filipe Manana | 4a4b964 | 2017-07-27 19:52:55 +0100 | [diff] [blame] | 2092 | if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG)) { |
| 2093 | spin_lock(&inode->lock); |
Nikolay Borisov | 6fc0ef6 | 2017-02-20 13:51:03 +0200 | [diff] [blame] | 2094 | inode->defrag_bytes -= len; |
Filipe Manana | 4a4b964 | 2017-07-27 19:52:55 +0100 | [diff] [blame] | 2095 | spin_unlock(&inode->lock); |
| 2096 | } |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 2097 | |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 2098 | /* |
| 2099 | * set_bit and clear bit hooks normally require _irqsave/restore |
Sergei Trofimovich | 27160b6 | 2011-05-20 20:20:32 +0000 | [diff] [blame] | 2100 | * but in this case, we are only testing for the DELALLOC |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 2101 | * bit, which is only set or cleared with irqs on |
| 2102 | */ |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 2103 | if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) { |
Nikolay Borisov | 6fc0ef6 | 2017-02-20 13:51:03 +0200 | [diff] [blame] | 2104 | struct btrfs_root *root = inode->root; |
Liu Bo | 83eea1f | 2012-07-10 05:28:39 -0600 | [diff] [blame] | 2105 | bool do_list = !btrfs_is_free_space_inode(inode); |
Chris Mason | bcbfce8 | 2008-04-22 13:26:47 -0400 | [diff] [blame] | 2106 | |
Josef Bacik | 8b62f87 | 2017-10-19 14:15:55 -0400 | [diff] [blame] | 2107 | spin_lock(&inode->lock); |
| 2108 | btrfs_mod_outstanding_extents(inode, -num_extents); |
| 2109 | spin_unlock(&inode->lock); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 2110 | |
Josef Bacik | b6d08f0 | 2013-09-27 14:57:43 -0400 | [diff] [blame] | 2111 | /* |
| 2112 | * We don't reserve metadata space for space cache inodes so we |
Andrea Gelmini | 52042d8 | 2018-11-28 12:05:13 +0100 | [diff] [blame] | 2113 | * don't need to call delalloc_release_metadata if there is an |
Josef Bacik | b6d08f0 | 2013-09-27 14:57:43 -0400 | [diff] [blame] | 2114 | * error. |
| 2115 | */ |
Filipe Manana | a315e68 | 2017-03-06 23:04:20 +0000 | [diff] [blame] | 2116 | if (*bits & EXTENT_CLEAR_META_RESV && |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2117 | root != fs_info->tree_root) |
Qu Wenruo | 43b1859 | 2017-12-12 15:34:32 +0800 | [diff] [blame] | 2118 | btrfs_delalloc_release_metadata(inode, len, false); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 2119 | |
Josef Bacik | 6a3891c | 2015-03-16 17:38:52 -0400 | [diff] [blame] | 2120 | /* For sanity tests. */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2121 | if (btrfs_is_testing(fs_info)) |
Josef Bacik | 6a3891c | 2015-03-16 17:38:52 -0400 | [diff] [blame] | 2122 | return; |
| 2123 | |
Filipe Manana | a315e68 | 2017-03-06 23:04:20 +0000 | [diff] [blame] | 2124 | if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID && |
| 2125 | do_list && !(state->state & EXTENT_NORESERVE) && |
| 2126 | (*bits & EXTENT_CLEAR_DATA_RESV)) |
Nikolay Borisov | 9db5d51 | 2020-06-03 08:55:38 +0300 | [diff] [blame] | 2127 | btrfs_free_reserved_data_space_noquota(fs_info, len); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 2128 | |
Nikolay Borisov | 104b4e5 | 2017-06-20 21:01:20 +0300 | [diff] [blame] | 2129 | percpu_counter_add_batch(&fs_info->delalloc_bytes, -len, |
| 2130 | fs_info->delalloc_batch); |
Nikolay Borisov | 6fc0ef6 | 2017-02-20 13:51:03 +0200 | [diff] [blame] | 2131 | spin_lock(&inode->lock); |
| 2132 | inode->delalloc_bytes -= len; |
| 2133 | if (do_list && inode->delalloc_bytes == 0 && |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 2134 | test_bit(BTRFS_INODE_IN_DELALLOC_LIST, |
Nikolay Borisov | 9e3e97f | 2017-02-20 13:51:07 +0200 | [diff] [blame] | 2135 | &inode->runtime_flags)) |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 2136 | btrfs_del_delalloc_inode(root, inode); |
Nikolay Borisov | 6fc0ef6 | 2017-02-20 13:51:03 +0200 | [diff] [blame] | 2137 | spin_unlock(&inode->lock); |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 2138 | } |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 2139 | |
| 2140 | if ((state->state & EXTENT_DELALLOC_NEW) && |
| 2141 | (*bits & EXTENT_DELALLOC_NEW)) { |
| 2142 | spin_lock(&inode->lock); |
| 2143 | ASSERT(inode->new_delalloc_bytes >= len); |
| 2144 | inode->new_delalloc_bytes -= len; |
| 2145 | spin_unlock(&inode->lock); |
| 2146 | } |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 2147 | } |
| 2148 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2149 | /* |
Nikolay Borisov | da12fe5 | 2018-11-27 20:57:58 +0200 | [diff] [blame] | 2150 | * btrfs_bio_fits_in_stripe - Checks whether the size of the given bio will fit |
| 2151 | * in a chunk's stripe. This function ensures that bios do not span a |
| 2152 | * stripe/chunk |
Liu Bo | 6f034ec | 2016-06-22 18:31:49 -0700 | [diff] [blame] | 2153 | * |
Nikolay Borisov | da12fe5 | 2018-11-27 20:57:58 +0200 | [diff] [blame] | 2154 | * @page - The page we are about to add to the bio |
| 2155 | * @size - size we want to add to the bio |
| 2156 | * @bio - bio we want to ensure is smaller than a stripe |
| 2157 | * @bio_flags - flags of the bio |
| 2158 | * |
| 2159 | * return 1 if page cannot be added to the bio |
| 2160 | * return 0 if page can be added to the bio |
Liu Bo | 6f034ec | 2016-06-22 18:31:49 -0700 | [diff] [blame] | 2161 | * return error otherwise |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2162 | */ |
Nikolay Borisov | da12fe5 | 2018-11-27 20:57:58 +0200 | [diff] [blame] | 2163 | int btrfs_bio_fits_in_stripe(struct page *page, size_t size, struct bio *bio, |
| 2164 | unsigned long bio_flags) |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 2165 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2166 | struct inode *inode = page->mapping->host; |
| 2167 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 2168 | u64 logical = (u64)bio->bi_iter.bi_sector << 9; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 2169 | u64 length = 0; |
| 2170 | u64 map_length; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 2171 | int ret; |
Nikolay Borisov | 89b798a | 2019-06-03 12:05:05 +0300 | [diff] [blame] | 2172 | struct btrfs_io_geometry geom; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 2173 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 2174 | if (bio_flags & EXTENT_BIO_COMPRESSED) |
| 2175 | return 0; |
| 2176 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 2177 | length = bio->bi_iter.bi_size; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 2178 | map_length = length; |
Nikolay Borisov | 89b798a | 2019-06-03 12:05:05 +0300 | [diff] [blame] | 2179 | ret = btrfs_get_io_geometry(fs_info, btrfs_op(bio), logical, map_length, |
| 2180 | &geom); |
Liu Bo | 6f034ec | 2016-06-22 18:31:49 -0700 | [diff] [blame] | 2181 | if (ret < 0) |
| 2182 | return ret; |
Nikolay Borisov | 89b798a | 2019-06-03 12:05:05 +0300 | [diff] [blame] | 2183 | |
| 2184 | if (geom.len < length + size) |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 2185 | return 1; |
Jeff Mahoney | 3444a97 | 2011-10-03 23:23:13 -0400 | [diff] [blame] | 2186 | return 0; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 2187 | } |
| 2188 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2189 | /* |
| 2190 | * in order to insert checksums into the metadata in large chunks, |
| 2191 | * we wait until bio submission time. All the pages in the bio are |
| 2192 | * checksummed and sums are attached onto the ordered extent record. |
| 2193 | * |
| 2194 | * At IO completion time the cums attached on the ordered extent record |
| 2195 | * are inserted into the btree |
| 2196 | */ |
Qu Wenruo | 8896a08 | 2020-10-21 14:24:53 +0800 | [diff] [blame^] | 2197 | static blk_status_t btrfs_submit_bio_start(struct inode *inode, struct bio *bio, |
| 2198 | u64 bio_offset) |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 2199 | { |
Johannes Thumshirn | c965d64 | 2020-07-28 20:25:41 +0900 | [diff] [blame] | 2200 | return btrfs_csum_one_bio(BTRFS_I(inode), bio, 0, 0); |
Chris Mason | 4a69a41 | 2008-11-06 22:03:00 -0500 | [diff] [blame] | 2201 | } |
Chris Mason | e015640 | 2008-04-16 11:15:20 -0400 | [diff] [blame] | 2202 | |
Chris Mason | 4a69a41 | 2008-11-06 22:03:00 -0500 | [diff] [blame] | 2203 | /* |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 2204 | * 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] | 2205 | * on write, or reading the csums from the tree before a read. |
| 2206 | * |
| 2207 | * Rules about async/sync submit, |
| 2208 | * a) read: sync submit |
| 2209 | * |
| 2210 | * b) write without checksum: sync submit |
| 2211 | * |
| 2212 | * c) write with checksum: |
| 2213 | * c-1) if bio is issued by fsync: sync submit |
| 2214 | * (sync_writers != 0) |
| 2215 | * |
| 2216 | * c-2) if root is reloc root: sync submit |
| 2217 | * (only in case of buffered IO) |
| 2218 | * |
| 2219 | * c-3) otherwise: async submit |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2220 | */ |
Nikolay Borisov | 908930f | 2020-09-18 16:34:37 +0300 | [diff] [blame] | 2221 | blk_status_t btrfs_submit_data_bio(struct inode *inode, struct bio *bio, |
| 2222 | int mirror_num, unsigned long bio_flags) |
Nikolay Borisov | 50489a5 | 2019-04-10 19:46:04 +0300 | [diff] [blame] | 2223 | |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 2224 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2225 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 2226 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chandan Rajendra | 0d51e28 | 2015-07-27 15:26:43 +0530 | [diff] [blame] | 2227 | enum btrfs_wq_endio_type metadata = BTRFS_WQ_ENDIO_DATA; |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 2228 | blk_status_t ret = 0; |
Chris Mason | 19b9bdb | 2008-10-30 14:23:13 -0400 | [diff] [blame] | 2229 | int skip_sum; |
Josef Bacik | b812ce2 | 2012-11-16 13:56:32 -0500 | [diff] [blame] | 2230 | int async = !atomic_read(&BTRFS_I(inode)->sync_writers); |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 2231 | |
Josef Bacik | 42437a6 | 2020-10-16 11:29:18 -0400 | [diff] [blame] | 2232 | skip_sum = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM) || |
| 2233 | !fs_info->csum_root; |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 2234 | |
Nikolay Borisov | 70ddc55 | 2017-02-20 13:50:35 +0200 | [diff] [blame] | 2235 | if (btrfs_is_free_space_inode(BTRFS_I(inode))) |
Chandan Rajendra | 0d51e28 | 2015-07-27 15:26:43 +0530 | [diff] [blame] | 2236 | metadata = BTRFS_WQ_ENDIO_FREE_SPACE; |
Jeff Mahoney | 0417341 | 2011-10-03 23:23:12 -0400 | [diff] [blame] | 2237 | |
Mike Christie | 37226b2 | 2016-06-05 14:31:52 -0500 | [diff] [blame] | 2238 | if (bio_op(bio) != REQ_OP_WRITE) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2239 | ret = btrfs_bio_wq_end_io(fs_info, bio, metadata); |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2240 | if (ret) |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 2241 | goto out; |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2242 | |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 2243 | if (bio_flags & EXTENT_BIO_COMPRESSED) { |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 2244 | ret = btrfs_submit_compressed_read(inode, bio, |
| 2245 | mirror_num, |
| 2246 | bio_flags); |
| 2247 | goto out; |
Josef Bacik | 334c16d | 2020-10-16 11:29:14 -0400 | [diff] [blame] | 2248 | } else { |
| 2249 | /* |
| 2250 | * Lookup bio sums does extra checks around whether we |
| 2251 | * need to csum or not, which is why we ignore skip_sum |
| 2252 | * here. |
| 2253 | */ |
Omar Sandoval | db72e47 | 2019-12-10 10:37:35 -0800 | [diff] [blame] | 2254 | ret = btrfs_lookup_bio_sums(inode, bio, (u64)-1, NULL); |
Tsutomu Itoh | c2db107 | 2011-03-01 06:48:31 +0000 | [diff] [blame] | 2255 | if (ret) |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 2256 | goto out; |
Tsutomu Itoh | c2db107 | 2011-03-01 06:48:31 +0000 | [diff] [blame] | 2257 | } |
Chris Mason | 4d1b5fb | 2008-08-20 09:44:52 -0400 | [diff] [blame] | 2258 | goto mapit; |
Josef Bacik | b812ce2 | 2012-11-16 13:56:32 -0500 | [diff] [blame] | 2259 | } else if (async && !skip_sum) { |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 2260 | /* csum items have already been cloned */ |
| 2261 | if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID) |
| 2262 | goto mapit; |
Chris Mason | 19b9bdb | 2008-10-30 14:23:13 -0400 | [diff] [blame] | 2263 | /* we're doing a write, do the async checksumming */ |
Qu Wenruo | 8896a08 | 2020-10-21 14:24:53 +0800 | [diff] [blame^] | 2264 | ret = btrfs_wq_submit_bio(inode, bio, mirror_num, bio_flags, |
| 2265 | 0, btrfs_submit_bio_start); |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 2266 | goto out; |
Josef Bacik | b812ce2 | 2012-11-16 13:56:32 -0500 | [diff] [blame] | 2267 | } else if (!skip_sum) { |
Nikolay Borisov | bd242a0 | 2020-06-03 08:55:07 +0300 | [diff] [blame] | 2268 | ret = btrfs_csum_one_bio(BTRFS_I(inode), bio, 0, 0); |
Josef Bacik | b812ce2 | 2012-11-16 13:56:32 -0500 | [diff] [blame] | 2269 | if (ret) |
| 2270 | goto out; |
Chris Mason | 19b9bdb | 2008-10-30 14:23:13 -0400 | [diff] [blame] | 2271 | } |
| 2272 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2273 | mapit: |
Chris Mason | 08635ba | 2019-07-10 12:28:14 -0700 | [diff] [blame] | 2274 | ret = btrfs_map_bio(fs_info, bio, mirror_num); |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 2275 | |
| 2276 | out: |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 2277 | if (ret) { |
| 2278 | bio->bi_status = ret; |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 2279 | bio_endio(bio); |
| 2280 | } |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 2281 | return ret; |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 2282 | } |
Chris Mason | 6885f30 | 2008-02-20 16:11:05 -0500 | [diff] [blame] | 2283 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2284 | /* |
| 2285 | * given a list of ordered sums record them in the inode. This happens |
| 2286 | * at IO completion time based on sums calculated at bio submission time. |
| 2287 | */ |
Nikolay Borisov | 510f85e | 2020-09-18 12:15:52 +0300 | [diff] [blame] | 2288 | static int add_pending_csums(struct btrfs_trans_handle *trans, |
| 2289 | struct list_head *list) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2290 | { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2291 | struct btrfs_ordered_sum *sum; |
Nikolay Borisov | ac01f26 | 2018-01-08 10:59:43 +0200 | [diff] [blame] | 2292 | int ret; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2293 | |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 2294 | list_for_each_entry(sum, list, list) { |
David Sterba | 7c2871a | 2017-11-08 01:07:43 +0100 | [diff] [blame] | 2295 | trans->adding_csums = true; |
Nikolay Borisov | 510f85e | 2020-09-18 12:15:52 +0300 | [diff] [blame] | 2296 | ret = btrfs_csum_file_blocks(trans, trans->fs_info->csum_root, sum); |
David Sterba | 7c2871a | 2017-11-08 01:07:43 +0100 | [diff] [blame] | 2297 | trans->adding_csums = false; |
Nikolay Borisov | ac01f26 | 2018-01-08 10:59:43 +0200 | [diff] [blame] | 2298 | if (ret) |
| 2299 | return ret; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2300 | } |
| 2301 | return 0; |
| 2302 | } |
| 2303 | |
Filipe Manana | c334730 | 2020-11-04 11:07:31 +0000 | [diff] [blame] | 2304 | static int btrfs_find_new_delalloc_bytes(struct btrfs_inode *inode, |
| 2305 | const u64 start, |
| 2306 | const u64 len, |
| 2307 | struct extent_state **cached_state) |
| 2308 | { |
| 2309 | u64 search_start = start; |
| 2310 | const u64 end = start + len - 1; |
| 2311 | |
| 2312 | while (search_start < end) { |
| 2313 | const u64 search_len = end - search_start + 1; |
| 2314 | struct extent_map *em; |
| 2315 | u64 em_len; |
| 2316 | int ret = 0; |
| 2317 | |
| 2318 | em = btrfs_get_extent(inode, NULL, 0, search_start, search_len); |
| 2319 | if (IS_ERR(em)) |
| 2320 | return PTR_ERR(em); |
| 2321 | |
| 2322 | if (em->block_start != EXTENT_MAP_HOLE) |
| 2323 | goto next; |
| 2324 | |
| 2325 | em_len = em->len; |
| 2326 | if (em->start < search_start) |
| 2327 | em_len -= search_start - em->start; |
| 2328 | if (em_len > search_len) |
| 2329 | em_len = search_len; |
| 2330 | |
| 2331 | ret = set_extent_bit(&inode->io_tree, search_start, |
| 2332 | search_start + em_len - 1, |
| 2333 | EXTENT_DELALLOC_NEW, |
| 2334 | NULL, cached_state, GFP_NOFS); |
| 2335 | next: |
| 2336 | search_start = extent_map_end(em); |
| 2337 | free_extent_map(em); |
| 2338 | if (ret) |
| 2339 | return ret; |
| 2340 | } |
| 2341 | return 0; |
| 2342 | } |
| 2343 | |
Nikolay Borisov | c2566f2 | 2020-06-03 08:55:35 +0300 | [diff] [blame] | 2344 | 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] | 2345 | unsigned int extra_bits, |
Nikolay Borisov | 330a582 | 2019-07-17 16:18:17 +0300 | [diff] [blame] | 2346 | struct extent_state **cached_state) |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 2347 | { |
Johannes Thumshirn | fdb1e12 | 2018-12-05 15:23:04 +0100 | [diff] [blame] | 2348 | WARN_ON(PAGE_ALIGNED(end)); |
Filipe Manana | c334730 | 2020-11-04 11:07:31 +0000 | [diff] [blame] | 2349 | |
| 2350 | if (start >= i_size_read(&inode->vfs_inode) && |
| 2351 | !(inode->flags & BTRFS_INODE_PREALLOC)) { |
| 2352 | /* |
| 2353 | * There can't be any extents following eof in this case so just |
| 2354 | * set the delalloc new bit for the range directly. |
| 2355 | */ |
| 2356 | extra_bits |= EXTENT_DELALLOC_NEW; |
| 2357 | } else { |
| 2358 | int ret; |
| 2359 | |
| 2360 | ret = btrfs_find_new_delalloc_bytes(inode, start, |
| 2361 | end + 1 - start, |
| 2362 | cached_state); |
| 2363 | if (ret) |
| 2364 | return ret; |
| 2365 | } |
| 2366 | |
Nikolay Borisov | c2566f2 | 2020-06-03 08:55:35 +0300 | [diff] [blame] | 2367 | return set_extent_delalloc(&inode->io_tree, start, end, extra_bits, |
| 2368 | cached_state); |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 2369 | } |
| 2370 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2371 | /* see btrfs_writepage_start_hook for details on why this is required */ |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2372 | struct btrfs_writepage_fixup { |
| 2373 | struct page *page; |
Josef Bacik | f4b1363 | 2020-01-21 14:34:52 -0500 | [diff] [blame] | 2374 | struct inode *inode; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2375 | struct btrfs_work work; |
| 2376 | }; |
| 2377 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 2378 | static void btrfs_writepage_fixup_worker(struct btrfs_work *work) |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2379 | { |
| 2380 | struct btrfs_writepage_fixup *fixup; |
| 2381 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 2382 | struct extent_state *cached_state = NULL; |
Qu Wenruo | 364ecf3 | 2017-02-27 15:10:38 +0800 | [diff] [blame] | 2383 | struct extent_changeset *data_reserved = NULL; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2384 | struct page *page; |
Nikolay Borisov | 65d87f7 | 2020-06-05 10:51:51 +0300 | [diff] [blame] | 2385 | struct btrfs_inode *inode; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2386 | u64 page_start; |
| 2387 | u64 page_end; |
Chris Mason | 25f3c50 | 2020-01-21 11:51:42 -0500 | [diff] [blame] | 2388 | int ret = 0; |
Josef Bacik | f4b1363 | 2020-01-21 14:34:52 -0500 | [diff] [blame] | 2389 | bool free_delalloc_space = true; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2390 | |
| 2391 | fixup = container_of(work, struct btrfs_writepage_fixup, work); |
| 2392 | page = fixup->page; |
Nikolay Borisov | 65d87f7 | 2020-06-05 10:51:51 +0300 | [diff] [blame] | 2393 | inode = BTRFS_I(fixup->inode); |
Josef Bacik | f4b1363 | 2020-01-21 14:34:52 -0500 | [diff] [blame] | 2394 | page_start = page_offset(page); |
| 2395 | page_end = page_offset(page) + PAGE_SIZE - 1; |
| 2396 | |
| 2397 | /* |
| 2398 | * This is similar to page_mkwrite, we need to reserve the space before |
| 2399 | * we take the page lock. |
| 2400 | */ |
Nikolay Borisov | 65d87f7 | 2020-06-05 10:51:51 +0300 | [diff] [blame] | 2401 | ret = btrfs_delalloc_reserve_space(inode, &data_reserved, page_start, |
| 2402 | PAGE_SIZE); |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 2403 | again: |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2404 | lock_page(page); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2405 | |
Chris Mason | 25f3c50 | 2020-01-21 11:51:42 -0500 | [diff] [blame] | 2406 | /* |
| 2407 | * Before we queued this fixup, we took a reference on the page. |
| 2408 | * page->mapping may go NULL, but it shouldn't be moved to a different |
| 2409 | * address space. |
| 2410 | */ |
Josef Bacik | f4b1363 | 2020-01-21 14:34:52 -0500 | [diff] [blame] | 2411 | if (!page->mapping || !PageDirty(page) || !PageChecked(page)) { |
| 2412 | /* |
| 2413 | * Unfortunately this is a little tricky, either |
| 2414 | * |
| 2415 | * 1) We got here and our page had already been dealt with and |
| 2416 | * we reserved our space, thus ret == 0, so we need to just |
| 2417 | * drop our space reservation and bail. This can happen the |
| 2418 | * first time we come into the fixup worker, or could happen |
| 2419 | * while waiting for the ordered extent. |
| 2420 | * 2) Our page was already dealt with, but we happened to get an |
| 2421 | * ENOSPC above from the btrfs_delalloc_reserve_space. In |
| 2422 | * this case we obviously don't have anything to release, but |
| 2423 | * because the page was already dealt with we don't want to |
| 2424 | * mark the page with an error, so make sure we're resetting |
| 2425 | * ret to 0. This is why we have this check _before_ the ret |
| 2426 | * check, because we do not want to have a surprise ENOSPC |
| 2427 | * when the page was already properly dealt with. |
| 2428 | */ |
| 2429 | if (!ret) { |
Nikolay Borisov | 65d87f7 | 2020-06-05 10:51:51 +0300 | [diff] [blame] | 2430 | btrfs_delalloc_release_extents(inode, PAGE_SIZE); |
| 2431 | btrfs_delalloc_release_space(inode, data_reserved, |
Josef Bacik | f4b1363 | 2020-01-21 14:34:52 -0500 | [diff] [blame] | 2432 | page_start, PAGE_SIZE, |
| 2433 | true); |
| 2434 | } |
| 2435 | ret = 0; |
Chris Mason | 25f3c50 | 2020-01-21 11:51:42 -0500 | [diff] [blame] | 2436 | goto out_page; |
Josef Bacik | f4b1363 | 2020-01-21 14:34:52 -0500 | [diff] [blame] | 2437 | } |
Chris Mason | 25f3c50 | 2020-01-21 11:51:42 -0500 | [diff] [blame] | 2438 | |
| 2439 | /* |
Josef Bacik | f4b1363 | 2020-01-21 14:34:52 -0500 | [diff] [blame] | 2440 | * We can't mess with the page state unless it is locked, so now that |
| 2441 | * it is locked bail if we failed to make our space reservation. |
Chris Mason | 25f3c50 | 2020-01-21 11:51:42 -0500 | [diff] [blame] | 2442 | */ |
Josef Bacik | f4b1363 | 2020-01-21 14:34:52 -0500 | [diff] [blame] | 2443 | if (ret) |
| 2444 | goto out_page; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2445 | |
Nikolay Borisov | 65d87f7 | 2020-06-05 10:51:51 +0300 | [diff] [blame] | 2446 | lock_extent_bits(&inode->io_tree, page_start, page_end, &cached_state); |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 2447 | |
| 2448 | /* already ordered? We're done */ |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 2449 | if (PagePrivate2(page)) |
Josef Bacik | f4b1363 | 2020-01-21 14:34:52 -0500 | [diff] [blame] | 2450 | goto out_reserved; |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 2451 | |
Nikolay Borisov | 65d87f7 | 2020-06-05 10:51:51 +0300 | [diff] [blame] | 2452 | ordered = btrfs_lookup_ordered_range(inode, page_start, PAGE_SIZE); |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 2453 | if (ordered) { |
Nikolay Borisov | 65d87f7 | 2020-06-05 10:51:51 +0300 | [diff] [blame] | 2454 | unlock_extent_cached(&inode->io_tree, page_start, page_end, |
| 2455 | &cached_state); |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 2456 | unlock_page(page); |
Nikolay Borisov | c0a4360 | 2020-09-18 12:15:53 +0300 | [diff] [blame] | 2457 | btrfs_start_ordered_extent(ordered, 1); |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 2458 | btrfs_put_ordered_extent(ordered); |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 2459 | goto again; |
| 2460 | } |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2461 | |
Nikolay Borisov | 65d87f7 | 2020-06-05 10:51:51 +0300 | [diff] [blame] | 2462 | ret = btrfs_set_extent_delalloc(inode, page_start, page_end, 0, |
Nikolay Borisov | 330a582 | 2019-07-17 16:18:17 +0300 | [diff] [blame] | 2463 | &cached_state); |
Chris Mason | 25f3c50 | 2020-01-21 11:51:42 -0500 | [diff] [blame] | 2464 | if (ret) |
Filipe Manana | 5368700 | 2019-10-09 17:43:59 +0100 | [diff] [blame] | 2465 | goto out_reserved; |
Nikolay Borisov | f3038ee | 2017-12-05 09:29:19 +0200 | [diff] [blame] | 2466 | |
Chris Mason | 25f3c50 | 2020-01-21 11:51:42 -0500 | [diff] [blame] | 2467 | /* |
| 2468 | * Everything went as planned, we're now the owner of a dirty page with |
| 2469 | * delayed allocation bits set and space reserved for our COW |
| 2470 | * destination. |
| 2471 | * |
| 2472 | * The page was dirty when we started, nothing should have cleaned it. |
| 2473 | */ |
| 2474 | BUG_ON(!PageDirty(page)); |
Josef Bacik | f4b1363 | 2020-01-21 14:34:52 -0500 | [diff] [blame] | 2475 | free_delalloc_space = false; |
Filipe Manana | 5368700 | 2019-10-09 17:43:59 +0100 | [diff] [blame] | 2476 | out_reserved: |
Nikolay Borisov | 65d87f7 | 2020-06-05 10:51:51 +0300 | [diff] [blame] | 2477 | btrfs_delalloc_release_extents(inode, PAGE_SIZE); |
Josef Bacik | f4b1363 | 2020-01-21 14:34:52 -0500 | [diff] [blame] | 2478 | if (free_delalloc_space) |
Nikolay Borisov | 65d87f7 | 2020-06-05 10:51:51 +0300 | [diff] [blame] | 2479 | btrfs_delalloc_release_space(inode, data_reserved, page_start, |
| 2480 | PAGE_SIZE, true); |
| 2481 | unlock_extent_cached(&inode->io_tree, page_start, page_end, |
David Sterba | e43bbe5 | 2017-12-12 21:43:52 +0100 | [diff] [blame] | 2482 | &cached_state); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2483 | out_page: |
Chris Mason | 25f3c50 | 2020-01-21 11:51:42 -0500 | [diff] [blame] | 2484 | if (ret) { |
| 2485 | /* |
| 2486 | * We hit ENOSPC or other errors. Update the mapping and page |
| 2487 | * to reflect the errors and clean the page. |
| 2488 | */ |
| 2489 | mapping_set_error(page->mapping, ret); |
| 2490 | end_extent_writepage(page, ret, page_start, page_end); |
| 2491 | clear_page_dirty_for_io(page); |
| 2492 | SetPageError(page); |
| 2493 | } |
| 2494 | ClearPageChecked(page); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2495 | unlock_page(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2496 | put_page(page); |
Miao Xie | b897abe | 2011-01-26 16:19:22 +0800 | [diff] [blame] | 2497 | kfree(fixup); |
Qu Wenruo | 364ecf3 | 2017-02-27 15:10:38 +0800 | [diff] [blame] | 2498 | extent_changeset_free(data_reserved); |
Josef Bacik | f4b1363 | 2020-01-21 14:34:52 -0500 | [diff] [blame] | 2499 | /* |
| 2500 | * As a precaution, do a delayed iput in case it would be the last iput |
| 2501 | * that could need flushing space. Recursing back to fixup worker would |
| 2502 | * deadlock. |
| 2503 | */ |
Nikolay Borisov | 65d87f7 | 2020-06-05 10:51:51 +0300 | [diff] [blame] | 2504 | btrfs_add_delayed_iput(&inode->vfs_inode); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2505 | } |
| 2506 | |
| 2507 | /* |
| 2508 | * There are a few paths in the higher layers of the kernel that directly |
| 2509 | * set the page dirty bit without asking the filesystem if it is a |
| 2510 | * good idea. This causes problems because we want to make sure COW |
| 2511 | * properly happens and the data=ordered rules are followed. |
| 2512 | * |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2513 | * 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] | 2514 | * hasn't been properly setup for IO. We kick off an async process |
| 2515 | * to fix it up. The async helper will wait for ordered extents, set |
| 2516 | * the delalloc bit and make it safe to write the page. |
| 2517 | */ |
Nikolay Borisov | d75855b | 2018-11-01 14:09:47 +0200 | [diff] [blame] | 2518 | int btrfs_writepage_cow_fixup(struct page *page, u64 start, u64 end) |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2519 | { |
| 2520 | struct inode *inode = page->mapping->host; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2521 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2522 | struct btrfs_writepage_fixup *fixup; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2523 | |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 2524 | /* this page is properly in the ordered list */ |
| 2525 | if (TestClearPagePrivate2(page)) |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2526 | return 0; |
| 2527 | |
Chris Mason | 25f3c50 | 2020-01-21 11:51:42 -0500 | [diff] [blame] | 2528 | /* |
| 2529 | * PageChecked is set below when we create a fixup worker for this page, |
| 2530 | * don't try to create another one if we're already PageChecked() |
| 2531 | * |
| 2532 | * The extent_io writepage code will redirty the page if we send back |
| 2533 | * EAGAIN. |
| 2534 | */ |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2535 | if (PageChecked(page)) |
| 2536 | return -EAGAIN; |
| 2537 | |
| 2538 | fixup = kzalloc(sizeof(*fixup), GFP_NOFS); |
| 2539 | if (!fixup) |
| 2540 | return -EAGAIN; |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 2541 | |
Josef Bacik | f4b1363 | 2020-01-21 14:34:52 -0500 | [diff] [blame] | 2542 | /* |
| 2543 | * We are already holding a reference to this inode from |
| 2544 | * write_cache_pages. We need to hold it because the space reservation |
| 2545 | * takes place outside of the page lock, and we can't trust |
| 2546 | * page->mapping outside of the page lock. |
| 2547 | */ |
| 2548 | ihold(inode); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2549 | SetPageChecked(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2550 | get_page(page); |
Omar Sandoval | a0cac0e | 2019-09-16 11:30:57 -0700 | [diff] [blame] | 2551 | btrfs_init_work(&fixup->work, btrfs_writepage_fixup_worker, NULL, NULL); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2552 | fixup->page = page; |
Josef Bacik | f4b1363 | 2020-01-21 14:34:52 -0500 | [diff] [blame] | 2553 | fixup->inode = inode; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2554 | btrfs_queue_work(fs_info->fixup_workers, &fixup->work); |
Chris Mason | 25f3c50 | 2020-01-21 11:51:42 -0500 | [diff] [blame] | 2555 | |
| 2556 | return -EAGAIN; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2557 | } |
| 2558 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2559 | static int insert_reserved_file_extent(struct btrfs_trans_handle *trans, |
Nikolay Borisov | c553f94 | 2020-06-03 08:55:19 +0300 | [diff] [blame] | 2560 | struct btrfs_inode *inode, u64 file_pos, |
Qu Wenruo | 9729f10 | 2020-06-10 09:04:41 +0800 | [diff] [blame] | 2561 | struct btrfs_file_extent_item *stack_fi, |
| 2562 | u64 qgroup_reserved) |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2563 | { |
Nikolay Borisov | c553f94 | 2020-06-03 08:55:19 +0300 | [diff] [blame] | 2564 | struct btrfs_root *root = inode->root; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2565 | struct btrfs_path *path; |
| 2566 | struct extent_buffer *leaf; |
| 2567 | struct btrfs_key ins; |
Qu Wenruo | 203f44c5 | 2020-06-10 09:04:40 +0800 | [diff] [blame] | 2568 | u64 disk_num_bytes = btrfs_stack_file_extent_disk_num_bytes(stack_fi); |
| 2569 | u64 disk_bytenr = btrfs_stack_file_extent_disk_bytenr(stack_fi); |
| 2570 | u64 num_bytes = btrfs_stack_file_extent_num_bytes(stack_fi); |
| 2571 | u64 ram_bytes = btrfs_stack_file_extent_ram_bytes(stack_fi); |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 2572 | int extent_inserted = 0; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2573 | int ret; |
| 2574 | |
| 2575 | path = btrfs_alloc_path(); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 2576 | if (!path) |
| 2577 | return -ENOMEM; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2578 | |
Chris Mason | a1ed835 | 2009-09-11 12:27:37 -0400 | [diff] [blame] | 2579 | /* |
| 2580 | * we may be replacing one extent in the tree with another. |
| 2581 | * The new extent is pinned in the extent map, and we don't want |
| 2582 | * to drop it from the cache until it is completely in the btree. |
| 2583 | * |
| 2584 | * So, tell btrfs_drop_extents to leave this extent in the cache. |
| 2585 | * the caller is expected to unpin it and allow it to be merged |
| 2586 | * with the others. |
| 2587 | */ |
Nikolay Borisov | c553f94 | 2020-06-03 08:55:19 +0300 | [diff] [blame] | 2588 | ret = __btrfs_drop_extents(trans, root, inode, path, file_pos, |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 2589 | file_pos + num_bytes, NULL, 0, |
Qu Wenruo | 203f44c5 | 2020-06-10 09:04:40 +0800 | [diff] [blame] | 2590 | 1, sizeof(*stack_fi), &extent_inserted); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2591 | if (ret) |
| 2592 | goto out; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2593 | |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 2594 | if (!extent_inserted) { |
Nikolay Borisov | c553f94 | 2020-06-03 08:55:19 +0300 | [diff] [blame] | 2595 | ins.objectid = btrfs_ino(inode); |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 2596 | ins.offset = file_pos; |
| 2597 | ins.type = BTRFS_EXTENT_DATA_KEY; |
| 2598 | |
| 2599 | path->leave_spinning = 1; |
| 2600 | ret = btrfs_insert_empty_item(trans, root, path, &ins, |
Qu Wenruo | 203f44c5 | 2020-06-10 09:04:40 +0800 | [diff] [blame] | 2601 | sizeof(*stack_fi)); |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 2602 | if (ret) |
| 2603 | goto out; |
| 2604 | } |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2605 | leaf = path->nodes[0]; |
Qu Wenruo | 203f44c5 | 2020-06-10 09:04:40 +0800 | [diff] [blame] | 2606 | btrfs_set_stack_file_extent_generation(stack_fi, trans->transid); |
| 2607 | write_extent_buffer(leaf, stack_fi, |
| 2608 | btrfs_item_ptr_offset(leaf, path->slots[0]), |
| 2609 | sizeof(struct btrfs_file_extent_item)); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 2610 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2611 | btrfs_mark_buffer_dirty(leaf); |
Josef Bacik | ce19533 | 2012-09-25 15:26:16 -0400 | [diff] [blame] | 2612 | btrfs_release_path(path); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2613 | |
Nikolay Borisov | c553f94 | 2020-06-03 08:55:19 +0300 | [diff] [blame] | 2614 | inode_add_bytes(&inode->vfs_inode, num_bytes); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2615 | |
| 2616 | ins.objectid = disk_bytenr; |
| 2617 | ins.offset = disk_num_bytes; |
| 2618 | ins.type = BTRFS_EXTENT_ITEM_KEY; |
Qu Wenruo | a12b877 | 2017-02-27 15:10:37 +0800 | [diff] [blame] | 2619 | |
Nikolay Borisov | c553f94 | 2020-06-03 08:55:19 +0300 | [diff] [blame] | 2620 | ret = btrfs_inode_set_file_extent_range(inode, file_pos, ram_bytes); |
Josef Bacik | 9ddc959 | 2020-01-17 09:02:22 -0500 | [diff] [blame] | 2621 | if (ret) |
| 2622 | goto out; |
| 2623 | |
Nikolay Borisov | c553f94 | 2020-06-03 08:55:19 +0300 | [diff] [blame] | 2624 | ret = btrfs_alloc_reserved_file_extent(trans, root, btrfs_ino(inode), |
Qu Wenruo | 9729f10 | 2020-06-10 09:04:41 +0800 | [diff] [blame] | 2625 | file_pos, qgroup_reserved, &ins); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2626 | out: |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2627 | btrfs_free_path(path); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 2628 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2629 | return ret; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2630 | } |
| 2631 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2632 | static void btrfs_release_delalloc_bytes(struct btrfs_fs_info *fs_info, |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 2633 | u64 start, u64 len) |
| 2634 | { |
David Sterba | 32da5386 | 2019-10-29 19:20:18 +0100 | [diff] [blame] | 2635 | struct btrfs_block_group *cache; |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 2636 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2637 | cache = btrfs_lookup_block_group(fs_info, start); |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 2638 | ASSERT(cache); |
| 2639 | |
| 2640 | spin_lock(&cache->lock); |
| 2641 | cache->delalloc_bytes -= len; |
| 2642 | spin_unlock(&cache->lock); |
| 2643 | |
| 2644 | btrfs_put_block_group(cache); |
| 2645 | } |
| 2646 | |
Qu Wenruo | 203f44c5 | 2020-06-10 09:04:40 +0800 | [diff] [blame] | 2647 | static int insert_ordered_extent_file_extent(struct btrfs_trans_handle *trans, |
Qu Wenruo | 203f44c5 | 2020-06-10 09:04:40 +0800 | [diff] [blame] | 2648 | struct btrfs_ordered_extent *oe) |
| 2649 | { |
| 2650 | struct btrfs_file_extent_item stack_fi; |
| 2651 | u64 logical_len; |
| 2652 | |
| 2653 | memset(&stack_fi, 0, sizeof(stack_fi)); |
| 2654 | btrfs_set_stack_file_extent_type(&stack_fi, BTRFS_FILE_EXTENT_REG); |
| 2655 | btrfs_set_stack_file_extent_disk_bytenr(&stack_fi, oe->disk_bytenr); |
| 2656 | btrfs_set_stack_file_extent_disk_num_bytes(&stack_fi, |
| 2657 | oe->disk_num_bytes); |
| 2658 | if (test_bit(BTRFS_ORDERED_TRUNCATED, &oe->flags)) |
| 2659 | logical_len = oe->truncated_len; |
| 2660 | else |
| 2661 | logical_len = oe->num_bytes; |
| 2662 | btrfs_set_stack_file_extent_num_bytes(&stack_fi, logical_len); |
| 2663 | btrfs_set_stack_file_extent_ram_bytes(&stack_fi, logical_len); |
| 2664 | btrfs_set_stack_file_extent_compression(&stack_fi, oe->compress_type); |
| 2665 | /* Encryption and other encoding is reserved and all 0 */ |
| 2666 | |
Nikolay Borisov | 3c38c87 | 2020-09-18 12:15:51 +0300 | [diff] [blame] | 2667 | return insert_reserved_file_extent(trans, BTRFS_I(oe->inode), |
| 2668 | oe->file_offset, &stack_fi, |
| 2669 | oe->qgroup_rsv); |
Qu Wenruo | 203f44c5 | 2020-06-10 09:04:40 +0800 | [diff] [blame] | 2670 | } |
| 2671 | |
| 2672 | /* |
| 2673 | * As ordered data IO finishes, this gets called so we can finish |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2674 | * an ordered extent if the range of bytes in the file it covers are |
| 2675 | * fully written. |
| 2676 | */ |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2677 | static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2678 | { |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2679 | struct inode *inode = ordered_extent->inode; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2680 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2681 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 2682 | struct btrfs_trans_handle *trans = NULL; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2683 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 2684 | struct extent_state *cached_state = NULL; |
Omar Sandoval | bffe633 | 2019-12-02 17:34:19 -0800 | [diff] [blame] | 2685 | u64 start, end; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 2686 | int compress_type = 0; |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 2687 | int ret = 0; |
Omar Sandoval | bffe633 | 2019-12-02 17:34:19 -0800 | [diff] [blame] | 2688 | u64 logical_len = ordered_extent->num_bytes; |
Nikolay Borisov | 8d51012 | 2019-10-08 20:43:06 +0300 | [diff] [blame] | 2689 | bool freespace_inode; |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 2690 | bool truncated = false; |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 2691 | bool range_locked = false; |
| 2692 | bool clear_new_delalloc_bytes = false; |
Josef Bacik | 49940bd | 2018-10-11 15:54:21 -0400 | [diff] [blame] | 2693 | bool clear_reserved_extent = true; |
Omar Sandoval | 313facc | 2019-12-02 17:34:18 -0800 | [diff] [blame] | 2694 | unsigned int clear_bits; |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 2695 | |
Omar Sandoval | bffe633 | 2019-12-02 17:34:19 -0800 | [diff] [blame] | 2696 | start = ordered_extent->file_offset; |
| 2697 | end = start + ordered_extent->num_bytes - 1; |
| 2698 | |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 2699 | if (!test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) && |
| 2700 | !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags) && |
| 2701 | !test_bit(BTRFS_ORDERED_DIRECT, &ordered_extent->flags)) |
| 2702 | clear_new_delalloc_bytes = true; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2703 | |
Nikolay Borisov | 8d51012 | 2019-10-08 20:43:06 +0300 | [diff] [blame] | 2704 | freespace_inode = btrfs_is_free_space_inode(BTRFS_I(inode)); |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 2705 | |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2706 | if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) { |
| 2707 | ret = -EIO; |
| 2708 | goto out; |
| 2709 | } |
| 2710 | |
Omar Sandoval | bffe633 | 2019-12-02 17:34:19 -0800 | [diff] [blame] | 2711 | btrfs_free_io_failure_record(BTRFS_I(inode), start, end); |
Miao Xie | f612496 | 2014-09-12 18:44:04 +0800 | [diff] [blame] | 2712 | |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 2713 | if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) { |
| 2714 | truncated = true; |
| 2715 | logical_len = ordered_extent->truncated_len; |
| 2716 | /* Truncated the entire extent, don't bother adding */ |
| 2717 | if (!logical_len) |
| 2718 | goto out; |
| 2719 | } |
| 2720 | |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 2721 | if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) { |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2722 | BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */ |
Qu Wenruo | 94ed938 | 2015-09-08 17:25:56 +0800 | [diff] [blame] | 2723 | |
Josef Bacik | d923afe | 2020-01-17 09:02:23 -0500 | [diff] [blame] | 2724 | btrfs_inode_safe_disk_i_size_write(inode, 0); |
Nikolay Borisov | 8d51012 | 2019-10-08 20:43:06 +0300 | [diff] [blame] | 2725 | if (freespace_inode) |
| 2726 | trans = btrfs_join_transaction_spacecache(root); |
Josef Bacik | 6c760c0 | 2012-11-09 10:53:21 -0500 | [diff] [blame] | 2727 | else |
| 2728 | trans = btrfs_join_transaction(root); |
| 2729 | if (IS_ERR(trans)) { |
| 2730 | ret = PTR_ERR(trans); |
| 2731 | trans = NULL; |
| 2732 | goto out; |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 2733 | } |
Josef Bacik | 69fe2d7 | 2017-10-19 14:15:57 -0400 | [diff] [blame] | 2734 | trans->block_rsv = &BTRFS_I(inode)->block_rsv; |
Josef Bacik | 6c760c0 | 2012-11-09 10:53:21 -0500 | [diff] [blame] | 2735 | ret = btrfs_update_inode_fallback(trans, root, inode); |
| 2736 | if (ret) /* -ENOMEM or corruption */ |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 2737 | btrfs_abort_transaction(trans, ret); |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 2738 | goto out; |
| 2739 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2740 | |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 2741 | range_locked = true; |
Omar Sandoval | bffe633 | 2019-12-02 17:34:19 -0800 | [diff] [blame] | 2742 | lock_extent_bits(io_tree, start, end, &cached_state); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2743 | |
Nikolay Borisov | 8d51012 | 2019-10-08 20:43:06 +0300 | [diff] [blame] | 2744 | if (freespace_inode) |
| 2745 | trans = btrfs_join_transaction_spacecache(root); |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 2746 | else |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 2747 | trans = btrfs_join_transaction(root); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2748 | if (IS_ERR(trans)) { |
| 2749 | ret = PTR_ERR(trans); |
| 2750 | trans = NULL; |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 2751 | goto out; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2752 | } |
Chris Mason | a79b7d4 | 2014-05-22 16:18:52 -0700 | [diff] [blame] | 2753 | |
Josef Bacik | 69fe2d7 | 2017-10-19 14:15:57 -0400 | [diff] [blame] | 2754 | trans->block_rsv = &BTRFS_I(inode)->block_rsv; |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 2755 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2756 | if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags)) |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 2757 | compress_type = ordered_extent->compress_type; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2758 | if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) { |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 2759 | BUG_ON(compress_type); |
Nikolay Borisov | 7a6d706 | 2017-02-20 13:50:48 +0200 | [diff] [blame] | 2760 | ret = btrfs_mark_extent_written(trans, BTRFS_I(inode), |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2761 | ordered_extent->file_offset, |
| 2762 | ordered_extent->file_offset + |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 2763 | logical_len); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2764 | } else { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2765 | BUG_ON(root == fs_info->tree_root); |
Nikolay Borisov | 3c38c87 | 2020-09-18 12:15:51 +0300 | [diff] [blame] | 2766 | ret = insert_ordered_extent_file_extent(trans, ordered_extent); |
Josef Bacik | 49940bd | 2018-10-11 15:54:21 -0400 | [diff] [blame] | 2767 | if (!ret) { |
| 2768 | clear_reserved_extent = false; |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2769 | btrfs_release_delalloc_bytes(fs_info, |
Omar Sandoval | bffe633 | 2019-12-02 17:34:19 -0800 | [diff] [blame] | 2770 | ordered_extent->disk_bytenr, |
| 2771 | ordered_extent->disk_num_bytes); |
Josef Bacik | 49940bd | 2018-10-11 15:54:21 -0400 | [diff] [blame] | 2772 | } |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2773 | } |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 2774 | unpin_extent_cache(&BTRFS_I(inode)->extent_tree, |
Omar Sandoval | bffe633 | 2019-12-02 17:34:19 -0800 | [diff] [blame] | 2775 | ordered_extent->file_offset, |
| 2776 | ordered_extent->num_bytes, trans->transid); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2777 | if (ret < 0) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 2778 | btrfs_abort_transaction(trans, ret); |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 2779 | goto out; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2780 | } |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 2781 | |
Nikolay Borisov | 510f85e | 2020-09-18 12:15:52 +0300 | [diff] [blame] | 2782 | ret = add_pending_csums(trans, &ordered_extent->list); |
Nikolay Borisov | ac01f26 | 2018-01-08 10:59:43 +0200 | [diff] [blame] | 2783 | if (ret) { |
| 2784 | btrfs_abort_transaction(trans, ret); |
| 2785 | goto out; |
| 2786 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2787 | |
Josef Bacik | d923afe | 2020-01-17 09:02:23 -0500 | [diff] [blame] | 2788 | btrfs_inode_safe_disk_i_size_write(inode, 0); |
Josef Bacik | 6c760c0 | 2012-11-09 10:53:21 -0500 | [diff] [blame] | 2789 | ret = btrfs_update_inode_fallback(trans, root, inode); |
| 2790 | if (ret) { /* -ENOMEM or corruption */ |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 2791 | btrfs_abort_transaction(trans, ret); |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 2792 | goto out; |
Josef Bacik | 1ef30be | 2011-04-05 19:25:36 -0400 | [diff] [blame] | 2793 | } |
| 2794 | ret = 0; |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 2795 | out: |
Omar Sandoval | 313facc | 2019-12-02 17:34:18 -0800 | [diff] [blame] | 2796 | clear_bits = EXTENT_DEFRAG; |
| 2797 | if (range_locked) |
| 2798 | clear_bits |= EXTENT_LOCKED; |
| 2799 | if (clear_new_delalloc_bytes) |
| 2800 | clear_bits |= EXTENT_DELALLOC_NEW; |
Omar Sandoval | bffe633 | 2019-12-02 17:34:19 -0800 | [diff] [blame] | 2801 | clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, clear_bits, |
| 2802 | (clear_bits & EXTENT_LOCKED) ? 1 : 0, 0, |
Omar Sandoval | 313facc | 2019-12-02 17:34:18 -0800 | [diff] [blame] | 2803 | &cached_state); |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 2804 | |
Miao Xie | a698d075 | 2012-09-20 01:51:59 -0600 | [diff] [blame] | 2805 | if (trans) |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 2806 | btrfs_end_transaction(trans); |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 2807 | |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 2808 | if (ret || truncated) { |
Omar Sandoval | bffe633 | 2019-12-02 17:34:19 -0800 | [diff] [blame] | 2809 | u64 unwritten_start = start; |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 2810 | |
| 2811 | if (truncated) |
Omar Sandoval | bffe633 | 2019-12-02 17:34:19 -0800 | [diff] [blame] | 2812 | unwritten_start += logical_len; |
| 2813 | clear_extent_uptodate(io_tree, unwritten_start, end, NULL); |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 2814 | |
| 2815 | /* Drop the cache for the part of the extent we didn't write. */ |
Omar Sandoval | bffe633 | 2019-12-02 17:34:19 -0800 | [diff] [blame] | 2816 | btrfs_drop_extent_cache(BTRFS_I(inode), unwritten_start, end, 0); |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2817 | |
Josef Bacik | 0bec9ef | 2013-01-31 14:58:00 -0500 | [diff] [blame] | 2818 | /* |
| 2819 | * If the ordered extent had an IOERR or something else went |
| 2820 | * wrong we need to return the space for this ordered extent |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 2821 | * back to the allocator. We only free the extent in the |
| 2822 | * truncated case if we didn't write out the extent at all. |
Josef Bacik | 49940bd | 2018-10-11 15:54:21 -0400 | [diff] [blame] | 2823 | * |
| 2824 | * If we made it past insert_reserved_file_extent before we |
| 2825 | * errored out then we don't need to do this as the accounting |
| 2826 | * has already been done. |
Josef Bacik | 0bec9ef | 2013-01-31 14:58:00 -0500 | [diff] [blame] | 2827 | */ |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 2828 | if ((ret || !logical_len) && |
Josef Bacik | 49940bd | 2018-10-11 15:54:21 -0400 | [diff] [blame] | 2829 | clear_reserved_extent && |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 2830 | !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) && |
Nikolay Borisov | 4eaaec2 | 2019-11-21 14:03:29 +0200 | [diff] [blame] | 2831 | !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) { |
| 2832 | /* |
| 2833 | * Discard the range before returning it back to the |
| 2834 | * free space pool |
| 2835 | */ |
Dennis Zhou | 46b27f5 | 2019-12-13 16:22:11 -0800 | [diff] [blame] | 2836 | if (ret && btrfs_test_opt(fs_info, DISCARD_SYNC)) |
Nikolay Borisov | 4eaaec2 | 2019-11-21 14:03:29 +0200 | [diff] [blame] | 2837 | btrfs_discard_extent(fs_info, |
Omar Sandoval | bffe633 | 2019-12-02 17:34:19 -0800 | [diff] [blame] | 2838 | ordered_extent->disk_bytenr, |
| 2839 | ordered_extent->disk_num_bytes, |
| 2840 | NULL); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2841 | btrfs_free_reserved_extent(fs_info, |
Omar Sandoval | bffe633 | 2019-12-02 17:34:19 -0800 | [diff] [blame] | 2842 | ordered_extent->disk_bytenr, |
| 2843 | ordered_extent->disk_num_bytes, 1); |
Nikolay Borisov | 4eaaec2 | 2019-11-21 14:03:29 +0200 | [diff] [blame] | 2844 | } |
Josef Bacik | 0bec9ef | 2013-01-31 14:58:00 -0500 | [diff] [blame] | 2845 | } |
| 2846 | |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2847 | /* |
Liu Bo | 8bad3c0 | 2012-06-18 12:14:23 +0800 | [diff] [blame] | 2848 | * This needs to be done to make sure anybody waiting knows we are done |
| 2849 | * updating everything for this ordered extent. |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2850 | */ |
Nikolay Borisov | 71fe0a5 | 2020-09-18 12:15:50 +0300 | [diff] [blame] | 2851 | btrfs_remove_ordered_extent(BTRFS_I(inode), ordered_extent); |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2852 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2853 | /* once for us */ |
| 2854 | btrfs_put_ordered_extent(ordered_extent); |
| 2855 | /* once for the tree */ |
| 2856 | btrfs_put_ordered_extent(ordered_extent); |
| 2857 | |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2858 | return ret; |
| 2859 | } |
| 2860 | |
| 2861 | static void finish_ordered_fn(struct btrfs_work *work) |
| 2862 | { |
| 2863 | struct btrfs_ordered_extent *ordered_extent; |
| 2864 | ordered_extent = container_of(work, struct btrfs_ordered_extent, work); |
| 2865 | btrfs_finish_ordered_io(ordered_extent); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2866 | } |
| 2867 | |
Nikolay Borisov | c629732 | 2018-11-08 10:18:08 +0200 | [diff] [blame] | 2868 | void btrfs_writepage_endio_finish_ordered(struct page *page, u64 start, |
| 2869 | u64 end, int uptodate) |
Chris Mason | 211f90e | 2008-07-18 11:56:15 -0400 | [diff] [blame] | 2870 | { |
Nikolay Borisov | 3347c48 | 2020-08-31 14:42:44 +0300 | [diff] [blame] | 2871 | struct btrfs_inode *inode = BTRFS_I(page->mapping->host); |
| 2872 | struct btrfs_fs_info *fs_info = inode->root->fs_info; |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2873 | struct btrfs_ordered_extent *ordered_extent = NULL; |
Liu Bo | 9e0af23 | 2014-08-15 23:36:53 +0800 | [diff] [blame] | 2874 | struct btrfs_workqueue *wq; |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2875 | |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 2876 | trace_btrfs_writepage_end_io_hook(page, start, end, uptodate); |
| 2877 | |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 2878 | ClearPagePrivate2(page); |
Nikolay Borisov | 3347c48 | 2020-08-31 14:42:44 +0300 | [diff] [blame] | 2879 | if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start, |
| 2880 | end - start + 1, uptodate)) |
David Sterba | c3988d6 | 2017-02-17 15:18:32 +0100 | [diff] [blame] | 2881 | return; |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2882 | |
Nikolay Borisov | 3347c48 | 2020-08-31 14:42:44 +0300 | [diff] [blame] | 2883 | if (btrfs_is_free_space_inode(inode)) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2884 | wq = fs_info->endio_freespace_worker; |
Omar Sandoval | a0cac0e | 2019-09-16 11:30:57 -0700 | [diff] [blame] | 2885 | else |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2886 | wq = fs_info->endio_write_workers; |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2887 | |
Omar Sandoval | a0cac0e | 2019-09-16 11:30:57 -0700 | [diff] [blame] | 2888 | btrfs_init_work(&ordered_extent->work, finish_ordered_fn, NULL, NULL); |
Liu Bo | 9e0af23 | 2014-08-15 23:36:53 +0800 | [diff] [blame] | 2889 | btrfs_queue_work(wq, &ordered_extent->work); |
Chris Mason | 211f90e | 2008-07-18 11:56:15 -0400 | [diff] [blame] | 2890 | } |
| 2891 | |
Omar Sandoval | 47df776 | 2020-04-16 14:46:17 -0700 | [diff] [blame] | 2892 | static int check_data_csum(struct inode *inode, struct btrfs_io_bio *io_bio, |
| 2893 | int icsum, struct page *page, int pgoff, u64 start, |
| 2894 | size_t len) |
Miao Xie | dc380ae | 2014-09-12 18:43:55 +0800 | [diff] [blame] | 2895 | { |
Johannes Thumshirn | d517857 | 2019-06-03 16:58:57 +0200 | [diff] [blame] | 2896 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
| 2897 | SHASH_DESC_ON_STACK(shash, fs_info->csum_shash); |
Miao Xie | dc380ae | 2014-09-12 18:43:55 +0800 | [diff] [blame] | 2898 | char *kaddr; |
Johannes Thumshirn | d517857 | 2019-06-03 16:58:57 +0200 | [diff] [blame] | 2899 | u16 csum_size = btrfs_super_csum_size(fs_info->super_copy); |
| 2900 | u8 *csum_expected; |
| 2901 | u8 csum[BTRFS_CSUM_SIZE]; |
Miao Xie | dc380ae | 2014-09-12 18:43:55 +0800 | [diff] [blame] | 2902 | |
Johannes Thumshirn | d517857 | 2019-06-03 16:58:57 +0200 | [diff] [blame] | 2903 | csum_expected = ((u8 *)io_bio->csum) + icsum * csum_size; |
Miao Xie | dc380ae | 2014-09-12 18:43:55 +0800 | [diff] [blame] | 2904 | |
| 2905 | kaddr = kmap_atomic(page); |
Johannes Thumshirn | d517857 | 2019-06-03 16:58:57 +0200 | [diff] [blame] | 2906 | shash->tfm = fs_info->csum_shash; |
| 2907 | |
Eric Biggers | fd08001 | 2020-04-30 23:51:59 -0700 | [diff] [blame] | 2908 | crypto_shash_digest(shash, kaddr + pgoff, len, csum); |
Johannes Thumshirn | d517857 | 2019-06-03 16:58:57 +0200 | [diff] [blame] | 2909 | |
| 2910 | if (memcmp(csum, csum_expected, csum_size)) |
Miao Xie | dc380ae | 2014-09-12 18:43:55 +0800 | [diff] [blame] | 2911 | goto zeroit; |
| 2912 | |
| 2913 | kunmap_atomic(kaddr); |
| 2914 | return 0; |
| 2915 | zeroit: |
Johannes Thumshirn | ea41d6b | 2019-06-03 16:58:58 +0200 | [diff] [blame] | 2916 | btrfs_print_data_csum_error(BTRFS_I(inode), start, csum, csum_expected, |
| 2917 | io_bio->mirror_num); |
Nikolay Borisov | 814723e | 2020-07-02 15:23:32 +0300 | [diff] [blame] | 2918 | if (io_bio->device) |
| 2919 | btrfs_dev_stat_inc_and_print(io_bio->device, |
| 2920 | BTRFS_DEV_STAT_CORRUPTION_ERRS); |
Miao Xie | dc380ae | 2014-09-12 18:43:55 +0800 | [diff] [blame] | 2921 | memset(kaddr + pgoff, 1, len); |
| 2922 | flush_dcache_page(page); |
| 2923 | kunmap_atomic(kaddr); |
Miao Xie | dc380ae | 2014-09-12 18:43:55 +0800 | [diff] [blame] | 2924 | return -EIO; |
| 2925 | } |
| 2926 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2927 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2928 | * 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] | 2929 | * if there's a match, we allow the bio to finish. If not, the code in |
| 2930 | * extent_io.c will try to find good copies for us. |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2931 | */ |
Nikolay Borisov | 9a446d6 | 2020-09-18 16:34:33 +0300 | [diff] [blame] | 2932 | int btrfs_verify_data_csum(struct btrfs_io_bio *io_bio, u64 phy_offset, |
| 2933 | struct page *page, u64 start, u64 end, int mirror) |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 2934 | { |
Miao Xie | 4eee4fa | 2012-12-21 09:17:45 +0000 | [diff] [blame] | 2935 | size_t offset = start - page_offset(page); |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 2936 | struct inode *inode = page->mapping->host; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2937 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | ff79f81 | 2007-10-15 16:22:25 -0400 | [diff] [blame] | 2938 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2939 | |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 2940 | if (PageChecked(page)) { |
| 2941 | ClearPageChecked(page); |
Miao Xie | dc380ae | 2014-09-12 18:43:55 +0800 | [diff] [blame] | 2942 | return 0; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 2943 | } |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 2944 | |
| 2945 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM) |
Miao Xie | dc380ae | 2014-09-12 18:43:55 +0800 | [diff] [blame] | 2946 | return 0; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 2947 | |
Josef Bacik | 42437a6 | 2020-10-16 11:29:18 -0400 | [diff] [blame] | 2948 | if (!root->fs_info->csum_root) |
| 2949 | return 0; |
| 2950 | |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 2951 | if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID && |
Chris Mason | 9655d29 | 2009-09-02 15:22:30 -0400 | [diff] [blame] | 2952 | test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) { |
David Sterba | 9116621 | 2016-04-26 23:54:39 +0200 | [diff] [blame] | 2953 | clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM); |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 2954 | return 0; |
| 2955 | } |
| 2956 | |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 2957 | phy_offset >>= inode->i_sb->s_blocksize_bits; |
Omar Sandoval | 47df776 | 2020-04-16 14:46:17 -0700 | [diff] [blame] | 2958 | return check_data_csum(inode, io_bio, phy_offset, page, offset, start, |
| 2959 | (size_t)(end - start + 1)); |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 2960 | } |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 2961 | |
Nikolay Borisov | c1c3fac | 2018-01-16 09:31:58 +0200 | [diff] [blame] | 2962 | /* |
| 2963 | * btrfs_add_delayed_iput - perform a delayed iput on @inode |
| 2964 | * |
| 2965 | * @inode: The inode we want to perform iput on |
| 2966 | * |
| 2967 | * This function uses the generic vfs_inode::i_count to track whether we should |
| 2968 | * just decrement it (in case it's > 1) or if this is the last iput then link |
| 2969 | * the inode to the delayed iput machinery. Delayed iputs are processed at |
| 2970 | * transaction commit time/superblock commit/cleaner kthread. |
| 2971 | */ |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 2972 | void btrfs_add_delayed_iput(struct inode *inode) |
| 2973 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2974 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
David Sterba | 8089fe6 | 2015-11-19 14:15:51 +0100 | [diff] [blame] | 2975 | struct btrfs_inode *binode = BTRFS_I(inode); |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 2976 | |
| 2977 | if (atomic_add_unless(&inode->i_count, -1, 1)) |
| 2978 | return; |
| 2979 | |
Josef Bacik | 034f784 | 2018-12-03 11:06:52 -0500 | [diff] [blame] | 2980 | atomic_inc(&fs_info->nr_delayed_iputs); |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 2981 | spin_lock(&fs_info->delayed_iput_lock); |
Nikolay Borisov | c1c3fac | 2018-01-16 09:31:58 +0200 | [diff] [blame] | 2982 | ASSERT(list_empty(&binode->delayed_iput)); |
| 2983 | list_add_tail(&binode->delayed_iput, &fs_info->delayed_iputs); |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 2984 | spin_unlock(&fs_info->delayed_iput_lock); |
Josef Bacik | fd340d0 | 2019-01-11 10:21:02 -0500 | [diff] [blame] | 2985 | if (!test_bit(BTRFS_FS_CLEANER_RUNNING, &fs_info->flags)) |
| 2986 | wake_up_process(fs_info->cleaner_kthread); |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 2987 | } |
| 2988 | |
Josef Bacik | 63611e7 | 2019-06-18 10:59:18 -0400 | [diff] [blame] | 2989 | static void run_delayed_iput_locked(struct btrfs_fs_info *fs_info, |
| 2990 | struct btrfs_inode *inode) |
| 2991 | { |
| 2992 | list_del_init(&inode->delayed_iput); |
| 2993 | spin_unlock(&fs_info->delayed_iput_lock); |
| 2994 | iput(&inode->vfs_inode); |
| 2995 | if (atomic_dec_and_test(&fs_info->nr_delayed_iputs)) |
| 2996 | wake_up(&fs_info->delayed_iputs_wait); |
| 2997 | spin_lock(&fs_info->delayed_iput_lock); |
| 2998 | } |
| 2999 | |
| 3000 | static void btrfs_run_delayed_iput(struct btrfs_fs_info *fs_info, |
| 3001 | struct btrfs_inode *inode) |
| 3002 | { |
| 3003 | if (!list_empty(&inode->delayed_iput)) { |
| 3004 | spin_lock(&fs_info->delayed_iput_lock); |
| 3005 | if (!list_empty(&inode->delayed_iput)) |
| 3006 | run_delayed_iput_locked(fs_info, inode); |
| 3007 | spin_unlock(&fs_info->delayed_iput_lock); |
| 3008 | } |
| 3009 | } |
| 3010 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3011 | void btrfs_run_delayed_iputs(struct btrfs_fs_info *fs_info) |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 3012 | { |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 3013 | |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 3014 | spin_lock(&fs_info->delayed_iput_lock); |
David Sterba | 8089fe6 | 2015-11-19 14:15:51 +0100 | [diff] [blame] | 3015 | while (!list_empty(&fs_info->delayed_iputs)) { |
| 3016 | struct btrfs_inode *inode; |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 3017 | |
David Sterba | 8089fe6 | 2015-11-19 14:15:51 +0100 | [diff] [blame] | 3018 | inode = list_first_entry(&fs_info->delayed_iputs, |
| 3019 | struct btrfs_inode, delayed_iput); |
Josef Bacik | 63611e7 | 2019-06-18 10:59:18 -0400 | [diff] [blame] | 3020 | run_delayed_iput_locked(fs_info, inode); |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 3021 | } |
David Sterba | 8089fe6 | 2015-11-19 14:15:51 +0100 | [diff] [blame] | 3022 | spin_unlock(&fs_info->delayed_iput_lock); |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 3023 | } |
| 3024 | |
Josef Bacik | 034f784 | 2018-12-03 11:06:52 -0500 | [diff] [blame] | 3025 | /** |
| 3026 | * btrfs_wait_on_delayed_iputs - wait on the delayed iputs to be done running |
| 3027 | * @fs_info - the fs_info for this fs |
| 3028 | * @return - EINTR if we were killed, 0 if nothing's pending |
| 3029 | * |
| 3030 | * This will wait on any delayed iputs that are currently running with KILLABLE |
| 3031 | * set. Once they are all done running we will return, unless we are killed in |
| 3032 | * which case we return EINTR. This helps in user operations like fallocate etc |
| 3033 | * that might get blocked on the iputs. |
| 3034 | */ |
| 3035 | int btrfs_wait_on_delayed_iputs(struct btrfs_fs_info *fs_info) |
| 3036 | { |
| 3037 | int ret = wait_event_killable(fs_info->delayed_iputs_wait, |
| 3038 | atomic_read(&fs_info->nr_delayed_iputs) == 0); |
| 3039 | if (ret) |
| 3040 | return -EINTR; |
| 3041 | return 0; |
| 3042 | } |
| 3043 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3044 | /* |
Omar Sandoval | f7e9e8f | 2018-05-11 13:13:32 -0700 | [diff] [blame] | 3045 | * This creates an orphan entry for the given inode in case something goes wrong |
| 3046 | * in the middle of an unlink. |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3047 | */ |
Nikolay Borisov | 73f2e54 | 2017-02-20 13:50:59 +0200 | [diff] [blame] | 3048 | int btrfs_orphan_add(struct btrfs_trans_handle *trans, |
Omar Sandoval | 2791906 | 2018-05-11 13:13:37 -0700 | [diff] [blame] | 3049 | struct btrfs_inode *inode) |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3050 | { |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3051 | int ret; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3052 | |
Omar Sandoval | 2791906 | 2018-05-11 13:13:37 -0700 | [diff] [blame] | 3053 | ret = btrfs_insert_orphan_item(trans, inode->root, btrfs_ino(inode)); |
| 3054 | if (ret && ret != -EEXIST) { |
| 3055 | btrfs_abort_transaction(trans, ret); |
| 3056 | return ret; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3057 | } |
| 3058 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3059 | return 0; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3060 | } |
| 3061 | |
| 3062 | /* |
Omar Sandoval | f7e9e8f | 2018-05-11 13:13:32 -0700 | [diff] [blame] | 3063 | * We have done the delete so we can go ahead and remove the orphan item for |
| 3064 | * this particular inode. |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3065 | */ |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 3066 | static int btrfs_orphan_del(struct btrfs_trans_handle *trans, |
Nikolay Borisov | 3d6ae7b | 2017-02-20 13:50:58 +0200 | [diff] [blame] | 3067 | struct btrfs_inode *inode) |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3068 | { |
Omar Sandoval | 2791906 | 2018-05-11 13:13:37 -0700 | [diff] [blame] | 3069 | return btrfs_del_orphan_item(trans, inode->root, btrfs_ino(inode)); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3070 | } |
| 3071 | |
| 3072 | /* |
| 3073 | * this cleans up any orphans that may be left on the list from the last use |
| 3074 | * of this root. |
| 3075 | */ |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3076 | int btrfs_orphan_cleanup(struct btrfs_root *root) |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3077 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3078 | struct btrfs_fs_info *fs_info = root->fs_info; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3079 | struct btrfs_path *path; |
| 3080 | struct extent_buffer *leaf; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3081 | struct btrfs_key key, found_key; |
| 3082 | struct btrfs_trans_handle *trans; |
| 3083 | struct inode *inode; |
Josef Bacik | 8f6d7f4 | 2011-09-26 15:55:20 -0400 | [diff] [blame] | 3084 | u64 last_objectid = 0; |
Omar Sandoval | f7e9e8f | 2018-05-11 13:13:32 -0700 | [diff] [blame] | 3085 | int ret = 0, nr_unlink = 0; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3086 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3087 | if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED)) |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3088 | return 0; |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 3089 | |
| 3090 | path = btrfs_alloc_path(); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3091 | if (!path) { |
| 3092 | ret = -ENOMEM; |
| 3093 | goto out; |
| 3094 | } |
David Sterba | e4058b5 | 2015-11-27 16:31:35 +0100 | [diff] [blame] | 3095 | path->reada = READA_BACK; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3096 | |
| 3097 | key.objectid = BTRFS_ORPHAN_OBJECTID; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 3098 | key.type = BTRFS_ORPHAN_ITEM_KEY; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3099 | key.offset = (u64)-1; |
| 3100 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3101 | while (1) { |
| 3102 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3103 | if (ret < 0) |
| 3104 | goto out; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3105 | |
| 3106 | /* |
| 3107 | * 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] | 3108 | * 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] | 3109 | * find the key and see if we have stuff that matches |
| 3110 | */ |
| 3111 | if (ret > 0) { |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3112 | ret = 0; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3113 | if (path->slots[0] == 0) |
| 3114 | break; |
| 3115 | path->slots[0]--; |
| 3116 | } |
| 3117 | |
| 3118 | /* pull out the item */ |
| 3119 | leaf = path->nodes[0]; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3120 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 3121 | |
| 3122 | /* make sure the item matches what we want */ |
| 3123 | if (found_key.objectid != BTRFS_ORPHAN_OBJECTID) |
| 3124 | break; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 3125 | if (found_key.type != BTRFS_ORPHAN_ITEM_KEY) |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3126 | break; |
| 3127 | |
| 3128 | /* release the path since we're done with it */ |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3129 | btrfs_release_path(path); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3130 | |
| 3131 | /* |
| 3132 | * this is where we are basically btrfs_lookup, without the |
| 3133 | * crossing root thing. we store the inode number in the |
| 3134 | * offset of the orphan item. |
| 3135 | */ |
Josef Bacik | 8f6d7f4 | 2011-09-26 15:55:20 -0400 | [diff] [blame] | 3136 | |
| 3137 | if (found_key.offset == last_objectid) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3138 | btrfs_err(fs_info, |
| 3139 | "Error removing orphan entry, stopping orphan cleanup"); |
Josef Bacik | 8f6d7f4 | 2011-09-26 15:55:20 -0400 | [diff] [blame] | 3140 | ret = -EINVAL; |
| 3141 | goto out; |
| 3142 | } |
| 3143 | |
| 3144 | last_objectid = found_key.offset; |
| 3145 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3146 | found_key.objectid = found_key.offset; |
| 3147 | found_key.type = BTRFS_INODE_ITEM_KEY; |
| 3148 | found_key.offset = 0; |
David Sterba | 0202e83 | 2020-05-15 19:35:59 +0200 | [diff] [blame] | 3149 | inode = btrfs_iget(fs_info->sb, last_objectid, root); |
Rusty Russell | 8c6ffba | 2013-07-15 11:20:32 +0930 | [diff] [blame] | 3150 | ret = PTR_ERR_OR_ZERO(inode); |
Filipe Manana | 6771089 | 2016-06-06 11:51:25 +0100 | [diff] [blame] | 3151 | if (ret && ret != -ENOENT) |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3152 | goto out; |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 3153 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3154 | if (ret == -ENOENT && root == fs_info->tree_root) { |
Arne Jansen | f8e9e0b | 2011-12-14 20:12:02 -0500 | [diff] [blame] | 3155 | struct btrfs_root *dead_root; |
Arne Jansen | f8e9e0b | 2011-12-14 20:12:02 -0500 | [diff] [blame] | 3156 | int is_dead_root = 0; |
| 3157 | |
| 3158 | /* |
| 3159 | * this is an orphan in the tree root. Currently these |
| 3160 | * could come from 2 sources: |
| 3161 | * a) a snapshot deletion in progress |
| 3162 | * b) a free space cache inode |
| 3163 | * We need to distinguish those two, as the snapshot |
| 3164 | * orphan must not get deleted. |
| 3165 | * find_dead_roots already ran before us, so if this |
| 3166 | * is a snapshot deletion, we should find the root |
Robbie Ko | a619b3c | 2020-05-07 10:54:40 +0800 | [diff] [blame] | 3167 | * in the fs_roots radix tree. |
Arne Jansen | f8e9e0b | 2011-12-14 20:12:02 -0500 | [diff] [blame] | 3168 | */ |
Robbie Ko | a619b3c | 2020-05-07 10:54:40 +0800 | [diff] [blame] | 3169 | |
| 3170 | spin_lock(&fs_info->fs_roots_radix_lock); |
| 3171 | dead_root = radix_tree_lookup(&fs_info->fs_roots_radix, |
| 3172 | (unsigned long)found_key.objectid); |
| 3173 | if (dead_root && btrfs_root_refs(&dead_root->root_item) == 0) |
| 3174 | is_dead_root = 1; |
| 3175 | spin_unlock(&fs_info->fs_roots_radix_lock); |
| 3176 | |
Arne Jansen | f8e9e0b | 2011-12-14 20:12:02 -0500 | [diff] [blame] | 3177 | if (is_dead_root) { |
| 3178 | /* prevent this orphan from being found again */ |
| 3179 | key.offset = found_key.objectid - 1; |
| 3180 | continue; |
| 3181 | } |
Omar Sandoval | f7e9e8f | 2018-05-11 13:13:32 -0700 | [diff] [blame] | 3182 | |
Arne Jansen | f8e9e0b | 2011-12-14 20:12:02 -0500 | [diff] [blame] | 3183 | } |
Omar Sandoval | f7e9e8f | 2018-05-11 13:13:32 -0700 | [diff] [blame] | 3184 | |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 3185 | /* |
Omar Sandoval | f7e9e8f | 2018-05-11 13:13:32 -0700 | [diff] [blame] | 3186 | * If we have an inode with links, there are a couple of |
| 3187 | * possibilities. Old kernels (before v3.12) used to create an |
| 3188 | * orphan item for truncate indicating that there were possibly |
| 3189 | * extent items past i_size that needed to be deleted. In v3.12, |
| 3190 | * truncate was changed to update i_size in sync with the extent |
| 3191 | * items, but the (useless) orphan item was still created. Since |
| 3192 | * v4.18, we don't create the orphan item for truncate at all. |
| 3193 | * |
| 3194 | * So, this item could mean that we need to do a truncate, but |
| 3195 | * only if this filesystem was last used on a pre-v3.12 kernel |
| 3196 | * and was not cleanly unmounted. The odds of that are quite |
| 3197 | * slim, and it's a pain to do the truncate now, so just delete |
| 3198 | * the orphan item. |
| 3199 | * |
| 3200 | * It's also possible that this orphan item was supposed to be |
| 3201 | * deleted but wasn't. The inode number may have been reused, |
| 3202 | * but either way, we can delete the orphan item. |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 3203 | */ |
Omar Sandoval | f7e9e8f | 2018-05-11 13:13:32 -0700 | [diff] [blame] | 3204 | if (ret == -ENOENT || inode->i_nlink) { |
| 3205 | if (!ret) |
| 3206 | iput(inode); |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 3207 | trans = btrfs_start_transaction(root, 1); |
| 3208 | if (IS_ERR(trans)) { |
| 3209 | ret = PTR_ERR(trans); |
| 3210 | goto out; |
| 3211 | } |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3212 | btrfs_debug(fs_info, "auto deleting %Lu", |
| 3213 | found_key.objectid); |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 3214 | ret = btrfs_del_orphan_item(trans, root, |
| 3215 | found_key.objectid); |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 3216 | btrfs_end_transaction(trans); |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 3217 | if (ret) |
| 3218 | goto out; |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 3219 | continue; |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3220 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3221 | |
Omar Sandoval | f7e9e8f | 2018-05-11 13:13:32 -0700 | [diff] [blame] | 3222 | nr_unlink++; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3223 | |
| 3224 | /* this will do delete_inode and everything for us */ |
| 3225 | iput(inode); |
| 3226 | } |
Miao Xie | 3254c87 | 2011-11-10 20:45:05 -0500 | [diff] [blame] | 3227 | /* release the path since we're done with it */ |
| 3228 | btrfs_release_path(path); |
| 3229 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3230 | root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE; |
| 3231 | |
Omar Sandoval | a575cee | 2018-05-11 13:13:38 -0700 | [diff] [blame] | 3232 | if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) { |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 3233 | trans = btrfs_join_transaction(root); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3234 | if (!IS_ERR(trans)) |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 3235 | btrfs_end_transaction(trans); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3236 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3237 | |
| 3238 | if (nr_unlink) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3239 | btrfs_debug(fs_info, "unlinked %d orphans", nr_unlink); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3240 | |
| 3241 | out: |
| 3242 | if (ret) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3243 | btrfs_err(fs_info, "could not do orphan cleanup %d", ret); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3244 | btrfs_free_path(path); |
| 3245 | return ret; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3246 | } |
| 3247 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3248 | /* |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3249 | * very simple check to peek ahead in the leaf looking for xattrs. If we |
| 3250 | * don't find any xattrs, we know there can't be any acls. |
| 3251 | * |
| 3252 | * slot is the slot the inode is in, objectid is the objectid of the inode |
| 3253 | */ |
| 3254 | 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] | 3255 | int slot, u64 objectid, |
| 3256 | int *first_xattr_slot) |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3257 | { |
| 3258 | u32 nritems = btrfs_header_nritems(leaf); |
| 3259 | struct btrfs_key found_key; |
Josef Bacik | f23b5a5 | 2013-06-19 10:16:26 -0400 | [diff] [blame] | 3260 | static u64 xattr_access = 0; |
| 3261 | static u64 xattr_default = 0; |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3262 | int scanned = 0; |
| 3263 | |
Josef Bacik | f23b5a5 | 2013-06-19 10:16:26 -0400 | [diff] [blame] | 3264 | if (!xattr_access) { |
Andreas Gruenbacher | 97d7929 | 2015-12-02 14:44:35 +0100 | [diff] [blame] | 3265 | xattr_access = btrfs_name_hash(XATTR_NAME_POSIX_ACL_ACCESS, |
| 3266 | strlen(XATTR_NAME_POSIX_ACL_ACCESS)); |
| 3267 | xattr_default = btrfs_name_hash(XATTR_NAME_POSIX_ACL_DEFAULT, |
| 3268 | strlen(XATTR_NAME_POSIX_ACL_DEFAULT)); |
Josef Bacik | f23b5a5 | 2013-06-19 10:16:26 -0400 | [diff] [blame] | 3269 | } |
| 3270 | |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3271 | slot++; |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 3272 | *first_xattr_slot = -1; |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3273 | while (slot < nritems) { |
| 3274 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
| 3275 | |
| 3276 | /* we found a different objectid, there must not be acls */ |
| 3277 | if (found_key.objectid != objectid) |
| 3278 | return 0; |
| 3279 | |
| 3280 | /* we found an xattr, assume we've got an acl */ |
Josef Bacik | f23b5a5 | 2013-06-19 10:16:26 -0400 | [diff] [blame] | 3281 | if (found_key.type == BTRFS_XATTR_ITEM_KEY) { |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 3282 | if (*first_xattr_slot == -1) |
| 3283 | *first_xattr_slot = slot; |
Josef Bacik | f23b5a5 | 2013-06-19 10:16:26 -0400 | [diff] [blame] | 3284 | if (found_key.offset == xattr_access || |
| 3285 | found_key.offset == xattr_default) |
| 3286 | return 1; |
| 3287 | } |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3288 | |
| 3289 | /* |
| 3290 | * we found a key greater than an xattr key, there can't |
| 3291 | * be any acls later on |
| 3292 | */ |
| 3293 | if (found_key.type > BTRFS_XATTR_ITEM_KEY) |
| 3294 | return 0; |
| 3295 | |
| 3296 | slot++; |
| 3297 | scanned++; |
| 3298 | |
| 3299 | /* |
| 3300 | * it goes inode, inode backrefs, xattrs, extents, |
| 3301 | * so if there are a ton of hard links to an inode there can |
| 3302 | * be a lot of backrefs. Don't waste time searching too hard, |
| 3303 | * this is just an optimization |
| 3304 | */ |
| 3305 | if (scanned >= 8) |
| 3306 | break; |
| 3307 | } |
| 3308 | /* we hit the end of the leaf before we found an xattr or |
| 3309 | * something larger than an xattr. We have to assume the inode |
| 3310 | * has acls |
| 3311 | */ |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 3312 | if (*first_xattr_slot == -1) |
| 3313 | *first_xattr_slot = slot; |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3314 | return 1; |
| 3315 | } |
| 3316 | |
| 3317 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3318 | * read an inode from the btree into the in-memory inode |
| 3319 | */ |
Filipe Manana | 4222ea7 | 2018-10-24 10:13:03 +0100 | [diff] [blame] | 3320 | static int btrfs_read_locked_inode(struct inode *inode, |
| 3321 | struct btrfs_path *in_path) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3322 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3323 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Filipe Manana | 4222ea7 | 2018-10-24 10:13:03 +0100 | [diff] [blame] | 3324 | struct btrfs_path *path = in_path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3325 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3326 | struct btrfs_inode_item *inode_item; |
| 3327 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 3328 | struct btrfs_key location; |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 3329 | unsigned long ptr; |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3330 | int maybe_acls; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 3331 | u32 rdev; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3332 | int ret; |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 3333 | bool filled = false; |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 3334 | int first_xattr_slot; |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 3335 | |
| 3336 | ret = btrfs_fill_inode(inode, &rdev); |
| 3337 | if (!ret) |
| 3338 | filled = true; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3339 | |
Filipe Manana | 4222ea7 | 2018-10-24 10:13:03 +0100 | [diff] [blame] | 3340 | if (!path) { |
| 3341 | path = btrfs_alloc_path(); |
| 3342 | if (!path) |
| 3343 | return -ENOMEM; |
| 3344 | } |
Mark Fasheh | 1748f84 | 2011-07-12 11:25:31 -0700 | [diff] [blame] | 3345 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3346 | memcpy(&location, &BTRFS_I(inode)->location, sizeof(location)); |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 3347 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3348 | ret = btrfs_lookup_inode(NULL, root, path, &location, 0); |
Filipe Manana | 6771089 | 2016-06-06 11:51:25 +0100 | [diff] [blame] | 3349 | if (ret) { |
Filipe Manana | 4222ea7 | 2018-10-24 10:13:03 +0100 | [diff] [blame] | 3350 | if (path != in_path) |
| 3351 | btrfs_free_path(path); |
Al Viro | f5b3a41 | 2018-07-29 23:04:51 +0100 | [diff] [blame] | 3352 | return ret; |
Filipe Manana | 6771089 | 2016-06-06 11:51:25 +0100 | [diff] [blame] | 3353 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3354 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3355 | leaf = path->nodes[0]; |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 3356 | |
| 3357 | if (filled) |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 3358 | goto cache_index; |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 3359 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3360 | inode_item = btrfs_item_ptr(leaf, path->slots[0], |
| 3361 | struct btrfs_inode_item); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3362 | inode->i_mode = btrfs_inode_mode(leaf, inode_item); |
Miklos Szeredi | bfe8684 | 2011-10-28 14:13:29 +0200 | [diff] [blame] | 3363 | set_nlink(inode, btrfs_inode_nlink(leaf, inode_item)); |
Eric W. Biederman | 2f2f43d | 2012-02-10 11:05:07 -0800 | [diff] [blame] | 3364 | i_uid_write(inode, btrfs_inode_uid(leaf, inode_item)); |
| 3365 | i_gid_write(inode, btrfs_inode_gid(leaf, inode_item)); |
Nikolay Borisov | 6ef06d2 | 2017-02-20 13:50:34 +0200 | [diff] [blame] | 3366 | 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] | 3367 | btrfs_inode_set_file_extent_range(BTRFS_I(inode), 0, |
| 3368 | round_up(i_size_read(inode), fs_info->sectorsize)); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3369 | |
David Sterba | a937b97 | 2014-12-12 17:39:12 +0100 | [diff] [blame] | 3370 | inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->atime); |
| 3371 | inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->atime); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3372 | |
David Sterba | a937b97 | 2014-12-12 17:39:12 +0100 | [diff] [blame] | 3373 | inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->mtime); |
| 3374 | inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->mtime); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3375 | |
David Sterba | a937b97 | 2014-12-12 17:39:12 +0100 | [diff] [blame] | 3376 | inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->ctime); |
| 3377 | inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->ctime); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3378 | |
chandan r | 9cc97d6 | 2012-07-04 12:48:07 +0530 | [diff] [blame] | 3379 | BTRFS_I(inode)->i_otime.tv_sec = |
| 3380 | btrfs_timespec_sec(leaf, &inode_item->otime); |
| 3381 | BTRFS_I(inode)->i_otime.tv_nsec = |
| 3382 | btrfs_timespec_nsec(leaf, &inode_item->otime); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3383 | |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 3384 | inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item)); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3385 | BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item); |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 3386 | BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item); |
| 3387 | |
Jeff Layton | c7f88c4 | 2017-12-11 06:35:12 -0500 | [diff] [blame] | 3388 | inode_set_iversion_queried(inode, |
| 3389 | btrfs_inode_sequence(leaf, inode_item)); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3390 | inode->i_generation = BTRFS_I(inode)->generation; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 3391 | inode->i_rdev = 0; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3392 | rdev = btrfs_inode_rdev(leaf, inode_item); |
| 3393 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 3394 | BTRFS_I(inode)->index_cnt = (u64)-1; |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 3395 | BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item); |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 3396 | |
| 3397 | cache_index: |
Yang Dongsheng | 6e17d30 | 2015-04-09 12:08:43 +0800 | [diff] [blame] | 3398 | /* |
| 3399 | * If we were modified in the current generation and evicted from memory |
| 3400 | * and then re-read we need to do a full sync since we don't have any |
| 3401 | * idea about which extents were modified before we were evicted from |
| 3402 | * cache. |
| 3403 | * |
| 3404 | * This is required for both inode re-read from disk and delayed inode |
| 3405 | * in delayed_nodes_tree. |
| 3406 | */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3407 | if (BTRFS_I(inode)->last_trans == fs_info->generation) |
Yang Dongsheng | 6e17d30 | 2015-04-09 12:08:43 +0800 | [diff] [blame] | 3408 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, |
| 3409 | &BTRFS_I(inode)->runtime_flags); |
| 3410 | |
Filipe Manana | bde6c24 | 2015-07-24 00:00:19 +0100 | [diff] [blame] | 3411 | /* |
| 3412 | * We don't persist the id of the transaction where an unlink operation |
| 3413 | * against the inode was last made. So here we assume the inode might |
| 3414 | * have been evicted, and therefore the exact value of last_unlink_trans |
| 3415 | * lost, and set it to last_trans to avoid metadata inconsistencies |
| 3416 | * between the inode and its parent if the inode is fsync'ed and the log |
| 3417 | * replayed. For example, in the scenario: |
| 3418 | * |
| 3419 | * touch mydir/foo |
| 3420 | * ln mydir/foo mydir/bar |
| 3421 | * sync |
| 3422 | * unlink mydir/bar |
| 3423 | * echo 2 > /proc/sys/vm/drop_caches # evicts inode |
| 3424 | * xfs_io -c fsync mydir/foo |
| 3425 | * <power failure> |
| 3426 | * mount fs, triggers fsync log replay |
| 3427 | * |
| 3428 | * We must make sure that when we fsync our inode foo we also log its |
| 3429 | * parent inode, otherwise after log replay the parent still has the |
| 3430 | * dentry with the "bar" name but our inode foo has a link count of 1 |
| 3431 | * and doesn't have an inode ref with the name "bar" anymore. |
| 3432 | * |
| 3433 | * Setting last_unlink_trans to last_trans is a pessimistic approach, |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 3434 | * but it guarantees correctness at the expense of occasional full |
Filipe Manana | bde6c24 | 2015-07-24 00:00:19 +0100 | [diff] [blame] | 3435 | * transaction commits on fsync if our inode is a directory, or if our |
| 3436 | * inode is not a directory, logging its parent unnecessarily. |
| 3437 | */ |
| 3438 | BTRFS_I(inode)->last_unlink_trans = BTRFS_I(inode)->last_trans; |
| 3439 | |
Filipe Manana | 3ebac17 | 2020-07-15 12:30:43 +0100 | [diff] [blame] | 3440 | /* |
| 3441 | * Same logic as for last_unlink_trans. We don't persist the generation |
| 3442 | * of the last transaction where this inode was used for a reflink |
| 3443 | * operation, so after eviction and reloading the inode we must be |
| 3444 | * pessimistic and assume the last transaction that modified the inode. |
| 3445 | */ |
| 3446 | BTRFS_I(inode)->last_reflink_trans = BTRFS_I(inode)->last_trans; |
| 3447 | |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 3448 | path->slots[0]++; |
| 3449 | if (inode->i_nlink != 1 || |
| 3450 | path->slots[0] >= btrfs_header_nritems(leaf)) |
| 3451 | goto cache_acl; |
| 3452 | |
| 3453 | btrfs_item_key_to_cpu(leaf, &location, path->slots[0]); |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 3454 | if (location.objectid != btrfs_ino(BTRFS_I(inode))) |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 3455 | goto cache_acl; |
| 3456 | |
| 3457 | ptr = btrfs_item_ptr_offset(leaf, path->slots[0]); |
| 3458 | if (location.type == BTRFS_INODE_REF_KEY) { |
| 3459 | struct btrfs_inode_ref *ref; |
| 3460 | |
| 3461 | ref = (struct btrfs_inode_ref *)ptr; |
| 3462 | BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref); |
| 3463 | } else if (location.type == BTRFS_INODE_EXTREF_KEY) { |
| 3464 | struct btrfs_inode_extref *extref; |
| 3465 | |
| 3466 | extref = (struct btrfs_inode_extref *)ptr; |
| 3467 | BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf, |
| 3468 | extref); |
| 3469 | } |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 3470 | cache_acl: |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3471 | /* |
| 3472 | * try to precache a NULL acl entry for files that don't have |
| 3473 | * any xattrs or acls |
| 3474 | */ |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3475 | maybe_acls = acls_after_inode_item(leaf, path->slots[0], |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 3476 | btrfs_ino(BTRFS_I(inode)), &first_xattr_slot); |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 3477 | if (first_xattr_slot != -1) { |
| 3478 | path->slots[0] = first_xattr_slot; |
| 3479 | ret = btrfs_load_inode_props(inode, path); |
| 3480 | if (ret) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3481 | btrfs_err(fs_info, |
David Sterba | 351fd35 | 2014-05-15 16:48:20 +0200 | [diff] [blame] | 3482 | "error loading props for ino %llu (root %llu): %d", |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 3483 | btrfs_ino(BTRFS_I(inode)), |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 3484 | root->root_key.objectid, ret); |
| 3485 | } |
Filipe Manana | 4222ea7 | 2018-10-24 10:13:03 +0100 | [diff] [blame] | 3486 | if (path != in_path) |
| 3487 | btrfs_free_path(path); |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 3488 | |
Al Viro | 72c0490 | 2009-06-24 16:58:48 -0400 | [diff] [blame] | 3489 | if (!maybe_acls) |
| 3490 | cache_no_acl(inode); |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3491 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3492 | switch (inode->i_mode & S_IFMT) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3493 | case S_IFREG: |
| 3494 | inode->i_mapping->a_ops = &btrfs_aops; |
| 3495 | inode->i_fop = &btrfs_file_operations; |
| 3496 | inode->i_op = &btrfs_file_inode_operations; |
| 3497 | break; |
| 3498 | case S_IFDIR: |
| 3499 | inode->i_fop = &btrfs_dir_file_operations; |
Omar Sandoval | 67ade05 | 2017-01-25 17:06:38 -0800 | [diff] [blame] | 3500 | inode->i_op = &btrfs_dir_inode_operations; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3501 | break; |
| 3502 | case S_IFLNK: |
| 3503 | inode->i_op = &btrfs_symlink_inode_operations; |
Al Viro | 21fc61c | 2015-11-17 01:07:57 -0500 | [diff] [blame] | 3504 | inode_nohighmem(inode); |
Omar Sandoval | 4779cc0 | 2018-09-24 15:16:55 -0700 | [diff] [blame] | 3505 | inode->i_mapping->a_ops = &btrfs_aops; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3506 | break; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 3507 | default: |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 3508 | inode->i_op = &btrfs_special_inode_operations; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 3509 | init_special_inode(inode, inode->i_mode, rdev); |
| 3510 | break; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3511 | } |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 3512 | |
David Sterba | 7b6a221 | 2018-03-26 18:40:21 +0200 | [diff] [blame] | 3513 | btrfs_sync_inode_flags_to_i_flags(inode); |
Filipe Manana | 6771089 | 2016-06-06 11:51:25 +0100 | [diff] [blame] | 3514 | return 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3515 | } |
| 3516 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3517 | /* |
| 3518 | * given a leaf and an inode, copy the inode fields into the leaf |
| 3519 | */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3520 | static void fill_inode_item(struct btrfs_trans_handle *trans, |
| 3521 | struct extent_buffer *leaf, |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3522 | struct btrfs_inode_item *item, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3523 | struct inode *inode) |
| 3524 | { |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3525 | struct btrfs_map_token token; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3526 | |
David Sterba | c82f823 | 2019-08-09 17:48:21 +0200 | [diff] [blame] | 3527 | btrfs_init_map_token(&token, leaf); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3528 | |
David Sterba | cc4c13d | 2020-04-29 02:15:56 +0200 | [diff] [blame] | 3529 | btrfs_set_token_inode_uid(&token, item, i_uid_read(inode)); |
| 3530 | btrfs_set_token_inode_gid(&token, item, i_gid_read(inode)); |
| 3531 | btrfs_set_token_inode_size(&token, item, BTRFS_I(inode)->disk_i_size); |
| 3532 | btrfs_set_token_inode_mode(&token, item, inode->i_mode); |
| 3533 | btrfs_set_token_inode_nlink(&token, item, inode->i_nlink); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3534 | |
David Sterba | cc4c13d | 2020-04-29 02:15:56 +0200 | [diff] [blame] | 3535 | btrfs_set_token_timespec_sec(&token, &item->atime, |
| 3536 | inode->i_atime.tv_sec); |
| 3537 | btrfs_set_token_timespec_nsec(&token, &item->atime, |
| 3538 | inode->i_atime.tv_nsec); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3539 | |
David Sterba | cc4c13d | 2020-04-29 02:15:56 +0200 | [diff] [blame] | 3540 | btrfs_set_token_timespec_sec(&token, &item->mtime, |
| 3541 | inode->i_mtime.tv_sec); |
| 3542 | btrfs_set_token_timespec_nsec(&token, &item->mtime, |
| 3543 | inode->i_mtime.tv_nsec); |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3544 | |
David Sterba | cc4c13d | 2020-04-29 02:15:56 +0200 | [diff] [blame] | 3545 | btrfs_set_token_timespec_sec(&token, &item->ctime, |
| 3546 | inode->i_ctime.tv_sec); |
| 3547 | btrfs_set_token_timespec_nsec(&token, &item->ctime, |
| 3548 | inode->i_ctime.tv_nsec); |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3549 | |
David Sterba | cc4c13d | 2020-04-29 02:15:56 +0200 | [diff] [blame] | 3550 | btrfs_set_token_timespec_sec(&token, &item->otime, |
| 3551 | BTRFS_I(inode)->i_otime.tv_sec); |
| 3552 | btrfs_set_token_timespec_nsec(&token, &item->otime, |
| 3553 | BTRFS_I(inode)->i_otime.tv_nsec); |
chandan r | 9cc97d6 | 2012-07-04 12:48:07 +0530 | [diff] [blame] | 3554 | |
David Sterba | cc4c13d | 2020-04-29 02:15:56 +0200 | [diff] [blame] | 3555 | btrfs_set_token_inode_nbytes(&token, item, inode_get_bytes(inode)); |
| 3556 | btrfs_set_token_inode_generation(&token, item, |
| 3557 | BTRFS_I(inode)->generation); |
| 3558 | btrfs_set_token_inode_sequence(&token, item, inode_peek_iversion(inode)); |
| 3559 | btrfs_set_token_inode_transid(&token, item, trans->transid); |
| 3560 | btrfs_set_token_inode_rdev(&token, item, inode->i_rdev); |
| 3561 | btrfs_set_token_inode_flags(&token, item, BTRFS_I(inode)->flags); |
| 3562 | btrfs_set_token_inode_block_group(&token, item, 0); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3563 | } |
| 3564 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3565 | /* |
| 3566 | * copy everything in the in-memory inode into the btree. |
| 3567 | */ |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 3568 | static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3569 | struct btrfs_root *root, struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3570 | { |
| 3571 | struct btrfs_inode_item *inode_item; |
| 3572 | struct btrfs_path *path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3573 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3574 | int ret; |
| 3575 | |
| 3576 | path = btrfs_alloc_path(); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 3577 | if (!path) |
| 3578 | return -ENOMEM; |
| 3579 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 3580 | path->leave_spinning = 1; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 3581 | ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location, |
| 3582 | 1); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3583 | if (ret) { |
| 3584 | if (ret > 0) |
| 3585 | ret = -ENOENT; |
| 3586 | goto failed; |
| 3587 | } |
| 3588 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3589 | leaf = path->nodes[0]; |
| 3590 | inode_item = btrfs_item_ptr(leaf, path->slots[0], |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 3591 | struct btrfs_inode_item); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3592 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3593 | fill_inode_item(trans, leaf, inode_item, inode); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3594 | btrfs_mark_buffer_dirty(leaf); |
Nikolay Borisov | d909441 | 2020-06-05 10:41:13 +0300 | [diff] [blame] | 3595 | btrfs_set_inode_last_trans(trans, BTRFS_I(inode)); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3596 | ret = 0; |
| 3597 | failed: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3598 | btrfs_free_path(path); |
| 3599 | return ret; |
| 3600 | } |
| 3601 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3602 | /* |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 3603 | * copy everything in the in-memory inode into the btree. |
| 3604 | */ |
| 3605 | noinline int btrfs_update_inode(struct btrfs_trans_handle *trans, |
| 3606 | struct btrfs_root *root, struct inode *inode) |
| 3607 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3608 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 3609 | int ret; |
| 3610 | |
| 3611 | /* |
| 3612 | * If the inode is a free space inode, we can deadlock during commit |
| 3613 | * if we put it into the delayed code. |
| 3614 | * |
| 3615 | * The data relocation inode should also be directly updated |
| 3616 | * without delay |
| 3617 | */ |
Nikolay Borisov | 70ddc55 | 2017-02-20 13:50:35 +0200 | [diff] [blame] | 3618 | if (!btrfs_is_free_space_inode(BTRFS_I(inode)) |
Josef Bacik | 1d52c78 | 2014-09-18 11:30:44 -0400 | [diff] [blame] | 3619 | && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3620 | && !test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) { |
Alexander Block | 8ea05e3 | 2012-07-25 17:35:53 +0200 | [diff] [blame] | 3621 | btrfs_update_root_times(trans, root); |
| 3622 | |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 3623 | ret = btrfs_delayed_update_inode(trans, root, inode); |
| 3624 | if (!ret) |
Nikolay Borisov | d909441 | 2020-06-05 10:41:13 +0300 | [diff] [blame] | 3625 | btrfs_set_inode_last_trans(trans, BTRFS_I(inode)); |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 3626 | return ret; |
| 3627 | } |
| 3628 | |
| 3629 | return btrfs_update_inode_item(trans, root, inode); |
| 3630 | } |
| 3631 | |
Josef Bacik | be6aef6 | 2012-10-22 15:43:12 -0400 | [diff] [blame] | 3632 | noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans, |
| 3633 | struct btrfs_root *root, |
| 3634 | struct inode *inode) |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 3635 | { |
| 3636 | int ret; |
| 3637 | |
| 3638 | ret = btrfs_update_inode(trans, root, inode); |
| 3639 | if (ret == -ENOSPC) |
| 3640 | return btrfs_update_inode_item(trans, root, inode); |
| 3641 | return ret; |
| 3642 | } |
| 3643 | |
| 3644 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3645 | * unlink helper that gets used here in inode.c and in the tree logging |
| 3646 | * recovery code. It remove a link in a directory with a given name, and |
| 3647 | * also drops the back refs in the inode to the directory |
| 3648 | */ |
Al Viro | 9298679 | 2011-03-04 17:14:37 +0000 | [diff] [blame] | 3649 | static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans, |
| 3650 | struct btrfs_root *root, |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 3651 | struct btrfs_inode *dir, |
| 3652 | struct btrfs_inode *inode, |
Al Viro | 9298679 | 2011-03-04 17:14:37 +0000 | [diff] [blame] | 3653 | const char *name, int name_len) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3654 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3655 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3656 | struct btrfs_path *path; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3657 | int ret = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3658 | struct btrfs_dir_item *di; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 3659 | u64 index; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3660 | u64 ino = btrfs_ino(inode); |
| 3661 | u64 dir_ino = btrfs_ino(dir); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3662 | |
| 3663 | path = btrfs_alloc_path(); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3664 | if (!path) { |
| 3665 | ret = -ENOMEM; |
Tsutomu Itoh | 554233a | 2011-02-03 03:16:25 +0000 | [diff] [blame] | 3666 | goto out; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3667 | } |
| 3668 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 3669 | path->leave_spinning = 1; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3670 | di = btrfs_lookup_dir_item(trans, root, path, dir_ino, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3671 | name, name_len, -1); |
Liu Bo | 3cf5068 | 2018-09-12 06:06:26 +0800 | [diff] [blame] | 3672 | if (IS_ERR_OR_NULL(di)) { |
| 3673 | ret = di ? PTR_ERR(di) : -ENOENT; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3674 | goto err; |
| 3675 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3676 | ret = btrfs_delete_one_dir_name(trans, root, path, di); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3677 | if (ret) |
| 3678 | goto err; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3679 | btrfs_release_path(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3680 | |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 3681 | /* |
| 3682 | * If we don't have dir index, we have to get it by looking up |
| 3683 | * the inode ref, since we get the inode ref, remove it directly, |
| 3684 | * it is unnecessary to do delayed deletion. |
| 3685 | * |
| 3686 | * But if we have dir index, needn't search inode ref to get it. |
| 3687 | * Since the inode ref is close to the inode item, it is better |
| 3688 | * that we delay to delete it, and just do this deletion when |
| 3689 | * we update the inode item. |
| 3690 | */ |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 3691 | if (inode->dir_index) { |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 3692 | ret = btrfs_delayed_delete_inode_ref(inode); |
| 3693 | if (!ret) { |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 3694 | index = inode->dir_index; |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 3695 | goto skip_backref; |
| 3696 | } |
| 3697 | } |
| 3698 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3699 | ret = btrfs_del_inode_ref(trans, root, name, name_len, ino, |
| 3700 | dir_ino, &index); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 3701 | if (ret) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3702 | btrfs_info(fs_info, |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 3703 | "failed to delete reference to %.*s, inode %llu parent %llu", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 3704 | name_len, name, ino, dir_ino); |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3705 | btrfs_abort_transaction(trans, ret); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 3706 | goto err; |
| 3707 | } |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 3708 | skip_backref: |
Lu Fengqi | 9add294 | 2018-08-01 11:32:26 +0800 | [diff] [blame] | 3709 | ret = btrfs_delete_delayed_dir_index(trans, dir, index); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3710 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3711 | btrfs_abort_transaction(trans, ret); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3712 | goto err; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3713 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3714 | |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 3715 | ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len, inode, |
| 3716 | dir_ino); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3717 | if (ret != 0 && ret != -ENOENT) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3718 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3719 | goto err; |
| 3720 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3721 | |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 3722 | ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len, dir, |
| 3723 | index); |
Chris Mason | 6418c96 | 2010-10-30 07:34:24 -0400 | [diff] [blame] | 3724 | if (ret == -ENOENT) |
| 3725 | ret = 0; |
Zach Brown | d4e3991 | 2013-04-02 21:02:16 +0000 | [diff] [blame] | 3726 | else if (ret) |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3727 | btrfs_abort_transaction(trans, ret); |
Josef Bacik | 63611e7 | 2019-06-18 10:59:18 -0400 | [diff] [blame] | 3728 | |
| 3729 | /* |
| 3730 | * If we have a pending delayed iput we could end up with the final iput |
| 3731 | * being run in btrfs-cleaner context. If we have enough of these built |
| 3732 | * up we can end up burning a lot of time in btrfs-cleaner without any |
| 3733 | * way to throttle the unlinks. Since we're currently holding a ref on |
| 3734 | * the inode we can run the delayed iput here without any issues as the |
| 3735 | * final iput won't be done until after we drop the ref we're currently |
| 3736 | * holding. |
| 3737 | */ |
| 3738 | btrfs_run_delayed_iput(fs_info, inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3739 | err: |
| 3740 | btrfs_free_path(path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3741 | if (ret) |
| 3742 | goto out; |
| 3743 | |
Nikolay Borisov | 6ef06d2 | 2017-02-20 13:50:34 +0200 | [diff] [blame] | 3744 | 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] | 3745 | inode_inc_iversion(&inode->vfs_inode); |
| 3746 | inode_inc_iversion(&dir->vfs_inode); |
| 3747 | inode->vfs_inode.i_ctime = dir->vfs_inode.i_mtime = |
| 3748 | dir->vfs_inode.i_ctime = current_time(&inode->vfs_inode); |
| 3749 | ret = btrfs_update_inode(trans, root, &dir->vfs_inode); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3750 | out: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3751 | return ret; |
| 3752 | } |
| 3753 | |
Al Viro | 9298679 | 2011-03-04 17:14:37 +0000 | [diff] [blame] | 3754 | int btrfs_unlink_inode(struct btrfs_trans_handle *trans, |
| 3755 | struct btrfs_root *root, |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 3756 | struct btrfs_inode *dir, struct btrfs_inode *inode, |
Al Viro | 9298679 | 2011-03-04 17:14:37 +0000 | [diff] [blame] | 3757 | const char *name, int name_len) |
| 3758 | { |
| 3759 | int ret; |
| 3760 | ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len); |
| 3761 | if (!ret) { |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 3762 | drop_nlink(&inode->vfs_inode); |
| 3763 | ret = btrfs_update_inode(trans, root, &inode->vfs_inode); |
Al Viro | 9298679 | 2011-03-04 17:14:37 +0000 | [diff] [blame] | 3764 | } |
| 3765 | return ret; |
| 3766 | } |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3767 | |
| 3768 | /* |
| 3769 | * helper to start transaction for unlink and rmdir. |
| 3770 | * |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 3771 | * unlink and rmdir are special in btrfs, they do not always free space, so |
| 3772 | * if we cannot make our reservations the normal way try and see if there is |
| 3773 | * plenty of slack room in the global reserve to migrate, otherwise we cannot |
| 3774 | * allow the unlink to occur. |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3775 | */ |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 3776 | static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir) |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3777 | { |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3778 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3779 | |
Josef Bacik | e70bea5 | 2011-10-11 14:18:24 -0400 | [diff] [blame] | 3780 | /* |
| 3781 | * 1 for the possible orphan item |
| 3782 | * 1 for the dir item |
| 3783 | * 1 for the dir index |
| 3784 | * 1 for the inode ref |
Josef Bacik | e70bea5 | 2011-10-11 14:18:24 -0400 | [diff] [blame] | 3785 | * 1 for the inode |
| 3786 | */ |
Josef Bacik | 7f9fe61 | 2020-03-13 15:58:05 -0400 | [diff] [blame] | 3787 | return btrfs_start_transaction_fallback_global_rsv(root, 5); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3788 | } |
| 3789 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3790 | static int btrfs_unlink(struct inode *dir, struct dentry *dentry) |
| 3791 | { |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3792 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3793 | struct btrfs_trans_handle *trans; |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 3794 | struct inode *inode = d_inode(dentry); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3795 | int ret; |
| 3796 | |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 3797 | trans = __unlink_start_trans(dir); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3798 | if (IS_ERR(trans)) |
Josef Bacik | 5df6a9f | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 3799 | return PTR_ERR(trans); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3800 | |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 3801 | btrfs_record_unlink_dir(trans, BTRFS_I(dir), BTRFS_I(d_inode(dentry)), |
| 3802 | 0); |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 3803 | |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 3804 | ret = btrfs_unlink_inode(trans, root, BTRFS_I(dir), |
| 3805 | BTRFS_I(d_inode(dentry)), dentry->d_name.name, |
| 3806 | dentry->d_name.len); |
Tsutomu Itoh | b532402 | 2011-07-19 07:27:20 +0000 | [diff] [blame] | 3807 | if (ret) |
| 3808 | goto out; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3809 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3810 | if (inode->i_nlink == 0) { |
Nikolay Borisov | 73f2e54 | 2017-02-20 13:50:59 +0200 | [diff] [blame] | 3811 | ret = btrfs_orphan_add(trans, BTRFS_I(inode)); |
Tsutomu Itoh | b532402 | 2011-07-19 07:27:20 +0000 | [diff] [blame] | 3812 | if (ret) |
| 3813 | goto out; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3814 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3815 | |
Tsutomu Itoh | b532402 | 2011-07-19 07:27:20 +0000 | [diff] [blame] | 3816 | out: |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 3817 | btrfs_end_transaction(trans); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3818 | btrfs_btree_balance_dirty(root->fs_info); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3819 | return ret; |
| 3820 | } |
| 3821 | |
Misono Tomohiro | f60a236 | 2018-04-18 11:34:52 +0900 | [diff] [blame] | 3822 | static int btrfs_unlink_subvol(struct btrfs_trans_handle *trans, |
Josef Bacik | 045d396 | 2019-12-18 17:20:27 -0500 | [diff] [blame] | 3823 | struct inode *dir, struct dentry *dentry) |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3824 | { |
Lu Fengqi | 401b3b1 | 2018-08-01 11:32:30 +0800 | [diff] [blame] | 3825 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Josef Bacik | 045d396 | 2019-12-18 17:20:27 -0500 | [diff] [blame] | 3826 | struct btrfs_inode *inode = BTRFS_I(d_inode(dentry)); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3827 | struct btrfs_path *path; |
| 3828 | struct extent_buffer *leaf; |
| 3829 | struct btrfs_dir_item *di; |
| 3830 | struct btrfs_key key; |
Josef Bacik | 045d396 | 2019-12-18 17:20:27 -0500 | [diff] [blame] | 3831 | const char *name = dentry->d_name.name; |
| 3832 | int name_len = dentry->d_name.len; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3833 | u64 index; |
| 3834 | int ret; |
Josef Bacik | 045d396 | 2019-12-18 17:20:27 -0500 | [diff] [blame] | 3835 | u64 objectid; |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 3836 | u64 dir_ino = btrfs_ino(BTRFS_I(dir)); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3837 | |
Josef Bacik | 045d396 | 2019-12-18 17:20:27 -0500 | [diff] [blame] | 3838 | if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID) { |
| 3839 | objectid = inode->root->root_key.objectid; |
| 3840 | } else if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) { |
| 3841 | objectid = inode->location.objectid; |
| 3842 | } else { |
| 3843 | WARN_ON(1); |
| 3844 | return -EINVAL; |
| 3845 | } |
| 3846 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3847 | path = btrfs_alloc_path(); |
| 3848 | if (!path) |
| 3849 | return -ENOMEM; |
| 3850 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3851 | di = btrfs_lookup_dir_item(trans, root, path, dir_ino, |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3852 | name, name_len, -1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3853 | if (IS_ERR_OR_NULL(di)) { |
Liu Bo | 3cf5068 | 2018-09-12 06:06:26 +0800 | [diff] [blame] | 3854 | ret = di ? PTR_ERR(di) : -ENOENT; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3855 | goto out; |
| 3856 | } |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3857 | |
| 3858 | leaf = path->nodes[0]; |
| 3859 | btrfs_dir_item_key_to_cpu(leaf, di, &key); |
| 3860 | WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid); |
| 3861 | ret = btrfs_delete_one_dir_name(trans, root, path, di); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3862 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3863 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3864 | goto out; |
| 3865 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3866 | btrfs_release_path(path); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3867 | |
Josef Bacik | d49d328 | 2019-12-18 17:20:28 -0500 | [diff] [blame] | 3868 | /* |
| 3869 | * This is a placeholder inode for a subvolume we didn't have a |
| 3870 | * reference to at the time of the snapshot creation. In the meantime |
| 3871 | * we could have renamed the real subvol link into our snapshot, so |
| 3872 | * depending on btrfs_del_root_ref to return -ENOENT here is incorret. |
| 3873 | * Instead simply lookup the dir_index_item for this entry so we can |
| 3874 | * remove it. Otherwise we know we have a ref to the root and we can |
| 3875 | * call btrfs_del_root_ref, and it _shouldn't_ fail. |
| 3876 | */ |
| 3877 | if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) { |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3878 | di = btrfs_search_dir_index_item(root, path, dir_ino, |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3879 | name, name_len); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3880 | if (IS_ERR_OR_NULL(di)) { |
| 3881 | if (!di) |
| 3882 | ret = -ENOENT; |
| 3883 | else |
| 3884 | ret = PTR_ERR(di); |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3885 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3886 | goto out; |
| 3887 | } |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3888 | |
| 3889 | leaf = path->nodes[0]; |
| 3890 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3891 | index = key.offset; |
Josef Bacik | d49d328 | 2019-12-18 17:20:28 -0500 | [diff] [blame] | 3892 | btrfs_release_path(path); |
| 3893 | } else { |
| 3894 | ret = btrfs_del_root_ref(trans, objectid, |
| 3895 | root->root_key.objectid, dir_ino, |
| 3896 | &index, name, name_len); |
| 3897 | if (ret) { |
| 3898 | btrfs_abort_transaction(trans, ret); |
| 3899 | goto out; |
| 3900 | } |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3901 | } |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3902 | |
Lu Fengqi | 9add294 | 2018-08-01 11:32:26 +0800 | [diff] [blame] | 3903 | ret = btrfs_delete_delayed_dir_index(trans, BTRFS_I(dir), index); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3904 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3905 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3906 | goto out; |
| 3907 | } |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 3908 | |
Nikolay Borisov | 6ef06d2 | 2017-02-20 13:50:34 +0200 | [diff] [blame] | 3909 | 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] | 3910 | inode_inc_iversion(dir); |
Deepa Dinamani | c2050a4 | 2016-09-14 07:48:06 -0700 | [diff] [blame] | 3911 | dir->i_mtime = dir->i_ctime = current_time(dir); |
Josef Bacik | 5a24e84 | 2012-08-08 10:12:59 -0600 | [diff] [blame] | 3912 | ret = btrfs_update_inode_fallback(trans, root, dir); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3913 | if (ret) |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3914 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3915 | out: |
Josef Bacik | 71d7aed | 2011-06-14 14:24:32 -0400 | [diff] [blame] | 3916 | btrfs_free_path(path); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3917 | return ret; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3918 | } |
| 3919 | |
Misono Tomohiro | ec42f16 | 2018-04-18 11:34:13 +0900 | [diff] [blame] | 3920 | /* |
| 3921 | * Helper to check if the subvolume references other subvolumes or if it's |
| 3922 | * default. |
| 3923 | */ |
Misono Tomohiro | f60a236 | 2018-04-18 11:34:52 +0900 | [diff] [blame] | 3924 | static noinline int may_destroy_subvol(struct btrfs_root *root) |
Misono Tomohiro | ec42f16 | 2018-04-18 11:34:13 +0900 | [diff] [blame] | 3925 | { |
| 3926 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 3927 | struct btrfs_path *path; |
| 3928 | struct btrfs_dir_item *di; |
| 3929 | struct btrfs_key key; |
| 3930 | u64 dir_id; |
| 3931 | int ret; |
| 3932 | |
| 3933 | path = btrfs_alloc_path(); |
| 3934 | if (!path) |
| 3935 | return -ENOMEM; |
| 3936 | |
| 3937 | /* Make sure this root isn't set as the default subvol */ |
| 3938 | dir_id = btrfs_super_root_dir(fs_info->super_copy); |
| 3939 | di = btrfs_lookup_dir_item(NULL, fs_info->tree_root, path, |
| 3940 | dir_id, "default", 7, 0); |
| 3941 | if (di && !IS_ERR(di)) { |
| 3942 | btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key); |
| 3943 | if (key.objectid == root->root_key.objectid) { |
| 3944 | ret = -EPERM; |
| 3945 | btrfs_err(fs_info, |
| 3946 | "deleting default subvolume %llu is not allowed", |
| 3947 | key.objectid); |
| 3948 | goto out; |
| 3949 | } |
| 3950 | btrfs_release_path(path); |
| 3951 | } |
| 3952 | |
| 3953 | key.objectid = root->root_key.objectid; |
| 3954 | key.type = BTRFS_ROOT_REF_KEY; |
| 3955 | key.offset = (u64)-1; |
| 3956 | |
| 3957 | ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0); |
| 3958 | if (ret < 0) |
| 3959 | goto out; |
| 3960 | BUG_ON(ret == 0); |
| 3961 | |
| 3962 | ret = 0; |
| 3963 | if (path->slots[0] > 0) { |
| 3964 | path->slots[0]--; |
| 3965 | btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]); |
| 3966 | if (key.objectid == root->root_key.objectid && |
| 3967 | key.type == BTRFS_ROOT_REF_KEY) |
| 3968 | ret = -ENOTEMPTY; |
| 3969 | } |
| 3970 | out: |
| 3971 | btrfs_free_path(path); |
| 3972 | return ret; |
| 3973 | } |
| 3974 | |
Nikolay Borisov | 20a6800 | 2018-04-27 14:36:24 +0300 | [diff] [blame] | 3975 | /* Delete all dentries for inodes belonging to the root */ |
| 3976 | static void btrfs_prune_dentries(struct btrfs_root *root) |
| 3977 | { |
| 3978 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 3979 | struct rb_node *node; |
| 3980 | struct rb_node *prev; |
| 3981 | struct btrfs_inode *entry; |
| 3982 | struct inode *inode; |
| 3983 | u64 objectid = 0; |
| 3984 | |
| 3985 | if (!test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) |
| 3986 | WARN_ON(btrfs_root_refs(&root->root_item) != 0); |
| 3987 | |
| 3988 | spin_lock(&root->inode_lock); |
| 3989 | again: |
| 3990 | node = root->inode_tree.rb_node; |
| 3991 | prev = NULL; |
| 3992 | while (node) { |
| 3993 | prev = node; |
| 3994 | entry = rb_entry(node, struct btrfs_inode, rb_node); |
| 3995 | |
David Sterba | 3750851 | 2018-06-29 10:56:40 +0200 | [diff] [blame] | 3996 | if (objectid < btrfs_ino(entry)) |
Nikolay Borisov | 20a6800 | 2018-04-27 14:36:24 +0300 | [diff] [blame] | 3997 | node = node->rb_left; |
David Sterba | 3750851 | 2018-06-29 10:56:40 +0200 | [diff] [blame] | 3998 | else if (objectid > btrfs_ino(entry)) |
Nikolay Borisov | 20a6800 | 2018-04-27 14:36:24 +0300 | [diff] [blame] | 3999 | node = node->rb_right; |
| 4000 | else |
| 4001 | break; |
| 4002 | } |
| 4003 | if (!node) { |
| 4004 | while (prev) { |
| 4005 | entry = rb_entry(prev, struct btrfs_inode, rb_node); |
David Sterba | 3750851 | 2018-06-29 10:56:40 +0200 | [diff] [blame] | 4006 | if (objectid <= btrfs_ino(entry)) { |
Nikolay Borisov | 20a6800 | 2018-04-27 14:36:24 +0300 | [diff] [blame] | 4007 | node = prev; |
| 4008 | break; |
| 4009 | } |
| 4010 | prev = rb_next(prev); |
| 4011 | } |
| 4012 | } |
| 4013 | while (node) { |
| 4014 | entry = rb_entry(node, struct btrfs_inode, rb_node); |
David Sterba | 3750851 | 2018-06-29 10:56:40 +0200 | [diff] [blame] | 4015 | objectid = btrfs_ino(entry) + 1; |
Nikolay Borisov | 20a6800 | 2018-04-27 14:36:24 +0300 | [diff] [blame] | 4016 | inode = igrab(&entry->vfs_inode); |
| 4017 | if (inode) { |
| 4018 | spin_unlock(&root->inode_lock); |
| 4019 | if (atomic_read(&inode->i_count) > 1) |
| 4020 | d_prune_aliases(inode); |
| 4021 | /* |
| 4022 | * btrfs_drop_inode will have it removed from the inode |
| 4023 | * cache when its usage count hits zero. |
| 4024 | */ |
| 4025 | iput(inode); |
| 4026 | cond_resched(); |
| 4027 | spin_lock(&root->inode_lock); |
| 4028 | goto again; |
| 4029 | } |
| 4030 | |
| 4031 | if (cond_resched_lock(&root->inode_lock)) |
| 4032 | goto again; |
| 4033 | |
| 4034 | node = rb_next(node); |
| 4035 | } |
| 4036 | spin_unlock(&root->inode_lock); |
| 4037 | } |
| 4038 | |
Misono Tomohiro | f60a236 | 2018-04-18 11:34:52 +0900 | [diff] [blame] | 4039 | int btrfs_delete_subvolume(struct inode *dir, struct dentry *dentry) |
| 4040 | { |
| 4041 | struct btrfs_fs_info *fs_info = btrfs_sb(dentry->d_sb); |
| 4042 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 4043 | struct inode *inode = d_inode(dentry); |
| 4044 | struct btrfs_root *dest = BTRFS_I(inode)->root; |
| 4045 | struct btrfs_trans_handle *trans; |
| 4046 | struct btrfs_block_rsv block_rsv; |
| 4047 | u64 root_flags; |
Misono Tomohiro | f60a236 | 2018-04-18 11:34:52 +0900 | [diff] [blame] | 4048 | int ret; |
| 4049 | int err; |
| 4050 | |
| 4051 | /* |
| 4052 | * Don't allow to delete a subvolume with send in progress. This is |
| 4053 | * inside the inode lock so the error handling that has to drop the bit |
| 4054 | * again is not run concurrently. |
| 4055 | */ |
| 4056 | spin_lock(&dest->root_item_lock); |
Lu Fengqi | a7176f7 | 2018-08-04 21:10:53 +0800 | [diff] [blame] | 4057 | if (dest->send_in_progress) { |
Misono Tomohiro | f60a236 | 2018-04-18 11:34:52 +0900 | [diff] [blame] | 4058 | spin_unlock(&dest->root_item_lock); |
| 4059 | btrfs_warn(fs_info, |
| 4060 | "attempt to delete subvolume %llu during send", |
| 4061 | dest->root_key.objectid); |
| 4062 | return -EPERM; |
| 4063 | } |
Lu Fengqi | a7176f7 | 2018-08-04 21:10:53 +0800 | [diff] [blame] | 4064 | root_flags = btrfs_root_flags(&dest->root_item); |
| 4065 | btrfs_set_root_flags(&dest->root_item, |
| 4066 | root_flags | BTRFS_ROOT_SUBVOL_DEAD); |
| 4067 | spin_unlock(&dest->root_item_lock); |
Misono Tomohiro | f60a236 | 2018-04-18 11:34:52 +0900 | [diff] [blame] | 4068 | |
| 4069 | down_write(&fs_info->subvol_sem); |
| 4070 | |
| 4071 | err = may_destroy_subvol(dest); |
| 4072 | if (err) |
| 4073 | goto out_up_write; |
| 4074 | |
| 4075 | btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP); |
| 4076 | /* |
| 4077 | * One for dir inode, |
| 4078 | * two for dir entries, |
| 4079 | * two for root ref/backref. |
| 4080 | */ |
Gu JinXiang | c4c129d | 2018-05-30 11:00:38 +0800 | [diff] [blame] | 4081 | err = btrfs_subvolume_reserve_metadata(root, &block_rsv, 5, true); |
Misono Tomohiro | f60a236 | 2018-04-18 11:34:52 +0900 | [diff] [blame] | 4082 | if (err) |
| 4083 | goto out_up_write; |
| 4084 | |
| 4085 | trans = btrfs_start_transaction(root, 0); |
| 4086 | if (IS_ERR(trans)) { |
| 4087 | err = PTR_ERR(trans); |
| 4088 | goto out_release; |
| 4089 | } |
| 4090 | trans->block_rsv = &block_rsv; |
| 4091 | trans->bytes_reserved = block_rsv.size; |
| 4092 | |
| 4093 | btrfs_record_snapshot_destroy(trans, BTRFS_I(dir)); |
| 4094 | |
Josef Bacik | 045d396 | 2019-12-18 17:20:27 -0500 | [diff] [blame] | 4095 | ret = btrfs_unlink_subvol(trans, dir, dentry); |
Misono Tomohiro | f60a236 | 2018-04-18 11:34:52 +0900 | [diff] [blame] | 4096 | if (ret) { |
| 4097 | err = ret; |
| 4098 | btrfs_abort_transaction(trans, ret); |
| 4099 | goto out_end_trans; |
| 4100 | } |
| 4101 | |
| 4102 | btrfs_record_root_in_trans(trans, dest); |
| 4103 | |
| 4104 | memset(&dest->root_item.drop_progress, 0, |
| 4105 | sizeof(dest->root_item.drop_progress)); |
David Sterba | c842268 | 2020-09-15 21:44:52 +0200 | [diff] [blame] | 4106 | btrfs_set_root_drop_level(&dest->root_item, 0); |
Misono Tomohiro | f60a236 | 2018-04-18 11:34:52 +0900 | [diff] [blame] | 4107 | btrfs_set_root_refs(&dest->root_item, 0); |
| 4108 | |
| 4109 | if (!test_and_set_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &dest->state)) { |
| 4110 | ret = btrfs_insert_orphan_item(trans, |
| 4111 | fs_info->tree_root, |
| 4112 | dest->root_key.objectid); |
| 4113 | if (ret) { |
| 4114 | btrfs_abort_transaction(trans, ret); |
| 4115 | err = ret; |
| 4116 | goto out_end_trans; |
| 4117 | } |
| 4118 | } |
| 4119 | |
Lu Fengqi | d195779 | 2018-05-29 15:01:54 +0800 | [diff] [blame] | 4120 | ret = btrfs_uuid_tree_remove(trans, dest->root_item.uuid, |
Misono Tomohiro | f60a236 | 2018-04-18 11:34:52 +0900 | [diff] [blame] | 4121 | BTRFS_UUID_KEY_SUBVOL, |
| 4122 | dest->root_key.objectid); |
| 4123 | if (ret && ret != -ENOENT) { |
| 4124 | btrfs_abort_transaction(trans, ret); |
| 4125 | err = ret; |
| 4126 | goto out_end_trans; |
| 4127 | } |
| 4128 | if (!btrfs_is_empty_uuid(dest->root_item.received_uuid)) { |
Lu Fengqi | d195779 | 2018-05-29 15:01:54 +0800 | [diff] [blame] | 4129 | ret = btrfs_uuid_tree_remove(trans, |
Misono Tomohiro | f60a236 | 2018-04-18 11:34:52 +0900 | [diff] [blame] | 4130 | dest->root_item.received_uuid, |
| 4131 | BTRFS_UUID_KEY_RECEIVED_SUBVOL, |
| 4132 | dest->root_key.objectid); |
| 4133 | if (ret && ret != -ENOENT) { |
| 4134 | btrfs_abort_transaction(trans, ret); |
| 4135 | err = ret; |
| 4136 | goto out_end_trans; |
| 4137 | } |
| 4138 | } |
| 4139 | |
Qu Wenruo | 082b6c9 | 2020-06-16 10:17:37 +0800 | [diff] [blame] | 4140 | free_anon_bdev(dest->anon_dev); |
| 4141 | dest->anon_dev = 0; |
Misono Tomohiro | f60a236 | 2018-04-18 11:34:52 +0900 | [diff] [blame] | 4142 | out_end_trans: |
| 4143 | trans->block_rsv = NULL; |
| 4144 | trans->bytes_reserved = 0; |
| 4145 | ret = btrfs_end_transaction(trans); |
| 4146 | if (ret && !err) |
| 4147 | err = ret; |
| 4148 | inode->i_flags |= S_DEAD; |
| 4149 | out_release: |
Qu Wenruo | e85fde5 | 2020-07-24 14:46:10 +0800 | [diff] [blame] | 4150 | btrfs_subvolume_release_metadata(root, &block_rsv); |
Misono Tomohiro | f60a236 | 2018-04-18 11:34:52 +0900 | [diff] [blame] | 4151 | out_up_write: |
| 4152 | up_write(&fs_info->subvol_sem); |
| 4153 | if (err) { |
| 4154 | spin_lock(&dest->root_item_lock); |
| 4155 | root_flags = btrfs_root_flags(&dest->root_item); |
| 4156 | btrfs_set_root_flags(&dest->root_item, |
| 4157 | root_flags & ~BTRFS_ROOT_SUBVOL_DEAD); |
| 4158 | spin_unlock(&dest->root_item_lock); |
| 4159 | } else { |
| 4160 | d_invalidate(dentry); |
Nikolay Borisov | 20a6800 | 2018-04-27 14:36:24 +0300 | [diff] [blame] | 4161 | btrfs_prune_dentries(dest); |
Misono Tomohiro | f60a236 | 2018-04-18 11:34:52 +0900 | [diff] [blame] | 4162 | ASSERT(dest->send_in_progress == 0); |
| 4163 | |
| 4164 | /* the last ref */ |
| 4165 | if (dest->ino_cache_inode) { |
| 4166 | iput(dest->ino_cache_inode); |
| 4167 | dest->ino_cache_inode = NULL; |
| 4168 | } |
| 4169 | } |
| 4170 | |
| 4171 | return err; |
| 4172 | } |
| 4173 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4174 | static int btrfs_rmdir(struct inode *dir, struct dentry *dentry) |
| 4175 | { |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 4176 | struct inode *inode = d_inode(dentry); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 4177 | int err = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4178 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4179 | struct btrfs_trans_handle *trans; |
Filipe Manana | 44f714d | 2016-06-06 16:11:13 +0100 | [diff] [blame] | 4180 | u64 last_unlink_trans; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4181 | |
David Sterba | b3ae244 | 2012-09-13 16:04:34 -0600 | [diff] [blame] | 4182 | if (inode->i_size > BTRFS_EMPTY_DIR_SIZE) |
Yan | 134d451 | 2007-10-25 15:49:25 -0400 | [diff] [blame] | 4183 | return -ENOTEMPTY; |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 4184 | if (btrfs_ino(BTRFS_I(inode)) == BTRFS_FIRST_FREE_OBJECTID) |
Misono Tomohiro | a79a464 | 2018-04-18 11:35:31 +0900 | [diff] [blame] | 4185 | return btrfs_delete_subvolume(dir, dentry); |
Yan | 134d451 | 2007-10-25 15:49:25 -0400 | [diff] [blame] | 4186 | |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 4187 | trans = __unlink_start_trans(dir); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4188 | if (IS_ERR(trans)) |
Josef Bacik | 5df6a9f | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4189 | return PTR_ERR(trans); |
Josef Bacik | 5df6a9f | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4190 | |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 4191 | if (unlikely(btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) { |
Josef Bacik | 045d396 | 2019-12-18 17:20:27 -0500 | [diff] [blame] | 4192 | err = btrfs_unlink_subvol(trans, dir, dentry); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4193 | goto out; |
| 4194 | } |
| 4195 | |
Nikolay Borisov | 73f2e54 | 2017-02-20 13:50:59 +0200 | [diff] [blame] | 4196 | err = btrfs_orphan_add(trans, BTRFS_I(inode)); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 4197 | if (err) |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4198 | goto out; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 4199 | |
Filipe Manana | 44f714d | 2016-06-06 16:11:13 +0100 | [diff] [blame] | 4200 | last_unlink_trans = BTRFS_I(inode)->last_unlink_trans; |
| 4201 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4202 | /* now the directory is empty */ |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 4203 | err = btrfs_unlink_inode(trans, root, BTRFS_I(dir), |
| 4204 | BTRFS_I(d_inode(dentry)), dentry->d_name.name, |
| 4205 | dentry->d_name.len); |
Filipe Manana | 44f714d | 2016-06-06 16:11:13 +0100 | [diff] [blame] | 4206 | if (!err) { |
Nikolay Borisov | 6ef06d2 | 2017-02-20 13:50:34 +0200 | [diff] [blame] | 4207 | btrfs_i_size_write(BTRFS_I(inode), 0); |
Filipe Manana | 44f714d | 2016-06-06 16:11:13 +0100 | [diff] [blame] | 4208 | /* |
| 4209 | * Propagate the last_unlink_trans value of the deleted dir to |
| 4210 | * its parent directory. This is to prevent an unrecoverable |
| 4211 | * log tree in the case we do something like this: |
| 4212 | * 1) create dir foo |
| 4213 | * 2) create snapshot under dir foo |
| 4214 | * 3) delete the snapshot |
| 4215 | * 4) rmdir foo |
| 4216 | * 5) mkdir foo |
| 4217 | * 6) fsync foo or some file inside foo |
| 4218 | */ |
| 4219 | if (last_unlink_trans >= trans->transid) |
| 4220 | BTRFS_I(dir)->last_unlink_trans = last_unlink_trans; |
| 4221 | } |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4222 | out: |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 4223 | btrfs_end_transaction(trans); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4224 | btrfs_btree_balance_dirty(root->fs_info); |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 4225 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4226 | return err; |
| 4227 | } |
| 4228 | |
Josef Bacik | ddfae63 | 2017-10-19 14:16:02 -0400 | [diff] [blame] | 4229 | /* |
| 4230 | * Return this if we need to call truncate_block for the last bit of the |
| 4231 | * truncate. |
| 4232 | */ |
| 4233 | #define NEED_TRUNCATE_BLOCK 1 |
Filipe Manana | 0305cd5 | 2015-10-16 12:34:25 +0100 | [diff] [blame] | 4234 | |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 4235 | /* |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4236 | * this can truncate away extent items, csum items and directory items. |
| 4237 | * 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] | 4238 | * any higher than new_size |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4239 | * |
| 4240 | * csum items that cross the new i_size are truncated to the new size |
| 4241 | * as well. |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 4242 | * |
| 4243 | * min_type is the minimum key type to truncate down to. If set to 0, this |
| 4244 | * 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] | 4245 | */ |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4246 | int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans, |
| 4247 | struct btrfs_root *root, |
| 4248 | struct inode *inode, |
| 4249 | u64 new_size, u32 min_type) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4250 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4251 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4252 | struct btrfs_path *path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4253 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4254 | struct btrfs_file_extent_item *fi; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4255 | struct btrfs_key key; |
| 4256 | struct btrfs_key found_key; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4257 | u64 extent_start = 0; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4258 | u64 extent_num_bytes = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4259 | u64 extent_offset = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4260 | u64 item_end = 0; |
Filipe Manana | c1aa457 | 2015-06-20 18:20:09 +0100 | [diff] [blame] | 4261 | u64 last_size = new_size; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4262 | u32 found_type = (u8)-1; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4263 | int found_extent; |
| 4264 | int del_item; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4265 | int pending_del_nr = 0; |
| 4266 | int pending_del_slot = 0; |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4267 | int extent_type = -1; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4268 | int ret; |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 4269 | u64 ino = btrfs_ino(BTRFS_I(inode)); |
Chris Mason | 28ed134 | 2014-12-17 09:41:04 -0800 | [diff] [blame] | 4270 | u64 bytes_deleted = 0; |
Thomas Meyer | 897ca81 | 2017-10-07 16:02:21 +0200 | [diff] [blame] | 4271 | bool be_nice = false; |
| 4272 | bool should_throttle = false; |
Filipe Manana | 28553fa | 2020-02-07 12:23:09 +0000 | [diff] [blame] | 4273 | const u64 lock_start = ALIGN_DOWN(new_size, fs_info->sectorsize); |
| 4274 | struct extent_state *cached_state = NULL; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4275 | |
| 4276 | BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4277 | |
Chris Mason | 28ed134 | 2014-12-17 09:41:04 -0800 | [diff] [blame] | 4278 | /* |
Qu Wenruo | 92a7cc4 | 2020-05-15 14:01:40 +0800 | [diff] [blame] | 4279 | * For non-free space inodes and non-shareable roots, we want to back |
| 4280 | * off from time to time. This means all inodes in subvolume roots, |
| 4281 | * reloc roots, and data reloc roots. |
Chris Mason | 28ed134 | 2014-12-17 09:41:04 -0800 | [diff] [blame] | 4282 | */ |
Nikolay Borisov | 70ddc55 | 2017-02-20 13:50:35 +0200 | [diff] [blame] | 4283 | if (!btrfs_is_free_space_inode(BTRFS_I(inode)) && |
Qu Wenruo | 92a7cc4 | 2020-05-15 14:01:40 +0800 | [diff] [blame] | 4284 | test_bit(BTRFS_ROOT_SHAREABLE, &root->state)) |
Thomas Meyer | 897ca81 | 2017-10-07 16:02:21 +0200 | [diff] [blame] | 4285 | be_nice = true; |
Chris Mason | 28ed134 | 2014-12-17 09:41:04 -0800 | [diff] [blame] | 4286 | |
Mark Fasheh | 0eb0e19 | 2011-07-12 16:44:10 -0700 | [diff] [blame] | 4287 | path = btrfs_alloc_path(); |
| 4288 | if (!path) |
| 4289 | return -ENOMEM; |
David Sterba | e4058b5 | 2015-11-27 16:31:35 +0100 | [diff] [blame] | 4290 | path->reada = READA_BACK; |
Mark Fasheh | 0eb0e19 | 2011-07-12 16:44:10 -0700 | [diff] [blame] | 4291 | |
Qu Wenruo | 82028e0a | 2020-05-15 14:01:41 +0800 | [diff] [blame] | 4292 | if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) { |
Filipe Manana | a5ae50d | 2020-02-20 13:29:49 +0000 | [diff] [blame] | 4293 | lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, (u64)-1, |
| 4294 | &cached_state); |
Filipe Manana | 28553fa | 2020-02-07 12:23:09 +0000 | [diff] [blame] | 4295 | |
Qu Wenruo | 82028e0a | 2020-05-15 14:01:41 +0800 | [diff] [blame] | 4296 | /* |
| 4297 | * We want to drop from the next block forward in case this |
| 4298 | * new size is not block aligned since we will be keeping the |
| 4299 | * last block of the extent just the way it is. |
| 4300 | */ |
Nikolay Borisov | dcdbc05 | 2017-02-20 13:50:45 +0200 | [diff] [blame] | 4301 | btrfs_drop_extent_cache(BTRFS_I(inode), ALIGN(new_size, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4302 | fs_info->sectorsize), |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 4303 | (u64)-1, 0); |
Qu Wenruo | 82028e0a | 2020-05-15 14:01:41 +0800 | [diff] [blame] | 4304 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4305 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 4306 | /* |
| 4307 | * This function is also used to drop the items in the log tree before |
| 4308 | * 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] | 4309 | * 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] | 4310 | * items. |
| 4311 | */ |
| 4312 | if (min_type == 0 && root == BTRFS_I(inode)->root) |
Nikolay Borisov | 4ccb5c7 | 2017-01-10 20:35:38 +0200 | [diff] [blame] | 4313 | btrfs_kill_delayed_inode_items(BTRFS_I(inode)); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 4314 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4315 | key.objectid = ino; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4316 | key.offset = (u64)-1; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4317 | key.type = (u8)-1; |
| 4318 | |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4319 | search_again: |
Chris Mason | 28ed134 | 2014-12-17 09:41:04 -0800 | [diff] [blame] | 4320 | /* |
| 4321 | * with a 16K leaf size and 128MB extents, you can actually queue |
| 4322 | * up a huge file in a single leaf. Most of the time that |
| 4323 | * bytes_deleted is > 0, it will be huge by the time we get here |
| 4324 | */ |
Omar Sandoval | fd86a3a | 2018-05-11 13:13:30 -0700 | [diff] [blame] | 4325 | if (be_nice && bytes_deleted > SZ_32M && |
| 4326 | btrfs_should_end_transaction(trans)) { |
| 4327 | ret = -EAGAIN; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4328 | goto out; |
| 4329 | } |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4330 | |
Omar Sandoval | fd86a3a | 2018-05-11 13:13:30 -0700 | [diff] [blame] | 4331 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 4332 | if (ret < 0) |
| 4333 | goto out; |
| 4334 | |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4335 | if (ret > 0) { |
Omar Sandoval | fd86a3a | 2018-05-11 13:13:30 -0700 | [diff] [blame] | 4336 | ret = 0; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4337 | /* there are no items in the tree for us to truncate, we're |
| 4338 | * done |
| 4339 | */ |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4340 | if (path->slots[0] == 0) |
| 4341 | goto out; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4342 | path->slots[0]--; |
| 4343 | } |
| 4344 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4345 | while (1) { |
Josef Bacik | 9ddc959 | 2020-01-17 09:02:22 -0500 | [diff] [blame] | 4346 | u64 clear_start = 0, clear_len = 0; |
| 4347 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4348 | fi = NULL; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4349 | leaf = path->nodes[0]; |
| 4350 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 4351 | found_type = found_key.type; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4352 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4353 | if (found_key.objectid != ino) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4354 | break; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4355 | |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4356 | if (found_type < min_type) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4357 | break; |
| 4358 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4359 | item_end = found_key.offset; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4360 | if (found_type == BTRFS_EXTENT_DATA_KEY) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4361 | fi = btrfs_item_ptr(leaf, path->slots[0], |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4362 | struct btrfs_file_extent_item); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4363 | extent_type = btrfs_file_extent_type(leaf, fi); |
| 4364 | if (extent_type != BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4365 | item_end += |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4366 | btrfs_file_extent_num_bytes(leaf, fi); |
Liu Bo | 09ed2f1 | 2017-03-10 11:09:48 -0800 | [diff] [blame] | 4367 | |
| 4368 | trace_btrfs_truncate_show_fi_regular( |
| 4369 | BTRFS_I(inode), leaf, fi, |
| 4370 | found_key.offset); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4371 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
Qu Wenruo | e41ca58 | 2018-06-06 15:41:49 +0800 | [diff] [blame] | 4372 | item_end += btrfs_file_extent_ram_bytes(leaf, |
| 4373 | fi); |
Liu Bo | 09ed2f1 | 2017-03-10 11:09:48 -0800 | [diff] [blame] | 4374 | |
| 4375 | trace_btrfs_truncate_show_fi_inline( |
| 4376 | BTRFS_I(inode), leaf, fi, path->slots[0], |
| 4377 | found_key.offset); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4378 | } |
Yan | 008630c | 2007-11-07 13:31:09 -0500 | [diff] [blame] | 4379 | item_end--; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4380 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4381 | if (found_type > min_type) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4382 | del_item = 1; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4383 | } else { |
Filipe Manana | 76b42ab | 2017-02-14 16:56:01 +0000 | [diff] [blame] | 4384 | if (item_end < new_size) |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4385 | break; |
| 4386 | if (found_key.offset >= new_size) |
| 4387 | del_item = 1; |
| 4388 | else |
| 4389 | del_item = 0; |
| 4390 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4391 | found_extent = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4392 | /* FIXME, shrink the extent if the ref count is only 1 */ |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4393 | if (found_type != BTRFS_EXTENT_DATA_KEY) |
| 4394 | goto delete; |
| 4395 | |
| 4396 | if (extent_type != BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4397 | u64 num_dec; |
Josef Bacik | 9ddc959 | 2020-01-17 09:02:22 -0500 | [diff] [blame] | 4398 | |
| 4399 | clear_start = found_key.offset; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4400 | extent_start = btrfs_file_extent_disk_bytenr(leaf, fi); |
Miao Xie | f70a9a6b | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 4401 | if (!del_item) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4402 | u64 orig_num_bytes = |
| 4403 | btrfs_file_extent_num_bytes(leaf, fi); |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 4404 | extent_num_bytes = ALIGN(new_size - |
| 4405 | found_key.offset, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4406 | fs_info->sectorsize); |
Josef Bacik | 9ddc959 | 2020-01-17 09:02:22 -0500 | [diff] [blame] | 4407 | clear_start = ALIGN(new_size, fs_info->sectorsize); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4408 | btrfs_set_file_extent_num_bytes(leaf, fi, |
| 4409 | extent_num_bytes); |
| 4410 | num_dec = (orig_num_bytes - |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 4411 | extent_num_bytes); |
Qu Wenruo | 92a7cc4 | 2020-05-15 14:01:40 +0800 | [diff] [blame] | 4412 | if (test_bit(BTRFS_ROOT_SHAREABLE, |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 4413 | &root->state) && |
| 4414 | extent_start != 0) |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 4415 | inode_sub_bytes(inode, num_dec); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4416 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4417 | } else { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4418 | extent_num_bytes = |
| 4419 | btrfs_file_extent_disk_num_bytes(leaf, |
| 4420 | fi); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4421 | extent_offset = found_key.offset - |
| 4422 | btrfs_file_extent_offset(leaf, fi); |
| 4423 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4424 | /* FIXME blocksize != 4096 */ |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 4425 | num_dec = btrfs_file_extent_num_bytes(leaf, fi); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4426 | if (extent_start != 0) { |
| 4427 | found_extent = 1; |
Qu Wenruo | 92a7cc4 | 2020-05-15 14:01:40 +0800 | [diff] [blame] | 4428 | if (test_bit(BTRFS_ROOT_SHAREABLE, |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 4429 | &root->state)) |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 4430 | inode_sub_bytes(inode, num_dec); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4431 | } |
| 4432 | } |
Josef Bacik | 9ddc959 | 2020-01-17 09:02:22 -0500 | [diff] [blame] | 4433 | clear_len = num_dec; |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 4434 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4435 | /* |
| 4436 | * we can't truncate inline items that have had |
| 4437 | * special encodings |
| 4438 | */ |
| 4439 | if (!del_item && |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4440 | btrfs_file_extent_encryption(leaf, fi) == 0 && |
Josef Bacik | ddfae63 | 2017-10-19 14:16:02 -0400 | [diff] [blame] | 4441 | btrfs_file_extent_other_encoding(leaf, fi) == 0 && |
| 4442 | btrfs_file_extent_compression(leaf, fi) == 0) { |
| 4443 | u32 size = (u32)(new_size - found_key.offset); |
Chris Mason | 514ac8a | 2014-01-03 21:07:00 -0800 | [diff] [blame] | 4444 | |
Josef Bacik | ddfae63 | 2017-10-19 14:16:02 -0400 | [diff] [blame] | 4445 | btrfs_set_file_extent_ram_bytes(leaf, fi, size); |
| 4446 | size = btrfs_file_extent_calc_inline_size(size); |
David Sterba | 78ac4f9 | 2019-03-20 14:49:12 +0100 | [diff] [blame] | 4447 | btrfs_truncate_item(path, size, 1); |
Josef Bacik | ddfae63 | 2017-10-19 14:16:02 -0400 | [diff] [blame] | 4448 | } else if (!del_item) { |
Chris Mason | 514ac8a | 2014-01-03 21:07:00 -0800 | [diff] [blame] | 4449 | /* |
Josef Bacik | ddfae63 | 2017-10-19 14:16:02 -0400 | [diff] [blame] | 4450 | * We have to bail so the last_size is set to |
| 4451 | * just before this extent. |
Chris Mason | 514ac8a | 2014-01-03 21:07:00 -0800 | [diff] [blame] | 4452 | */ |
Omar Sandoval | fd86a3a | 2018-05-11 13:13:30 -0700 | [diff] [blame] | 4453 | ret = NEED_TRUNCATE_BLOCK; |
Josef Bacik | ddfae63 | 2017-10-19 14:16:02 -0400 | [diff] [blame] | 4454 | break; |
Josef Bacik | 9ddc959 | 2020-01-17 09:02:22 -0500 | [diff] [blame] | 4455 | } else { |
| 4456 | /* |
| 4457 | * Inline extents are special, we just treat |
| 4458 | * them as a full sector worth in the file |
| 4459 | * extent tree just for simplicity sake. |
| 4460 | */ |
| 4461 | clear_len = fs_info->sectorsize; |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 4462 | } |
Josef Bacik | ddfae63 | 2017-10-19 14:16:02 -0400 | [diff] [blame] | 4463 | |
Qu Wenruo | 92a7cc4 | 2020-05-15 14:01:40 +0800 | [diff] [blame] | 4464 | if (test_bit(BTRFS_ROOT_SHAREABLE, &root->state)) |
Josef Bacik | ddfae63 | 2017-10-19 14:16:02 -0400 | [diff] [blame] | 4465 | inode_sub_bytes(inode, item_end + 1 - new_size); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4466 | } |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4467 | delete: |
Josef Bacik | 9ddc959 | 2020-01-17 09:02:22 -0500 | [diff] [blame] | 4468 | /* |
| 4469 | * We use btrfs_truncate_inode_items() to clean up log trees for |
| 4470 | * multiple fsyncs, and in this case we don't want to clear the |
| 4471 | * file extent range because it's just the log. |
| 4472 | */ |
| 4473 | if (root == BTRFS_I(inode)->root) { |
| 4474 | ret = btrfs_inode_clear_file_extent_range(BTRFS_I(inode), |
| 4475 | clear_start, clear_len); |
| 4476 | if (ret) { |
| 4477 | btrfs_abort_transaction(trans, ret); |
| 4478 | break; |
| 4479 | } |
| 4480 | } |
| 4481 | |
Josef Bacik | ddfae63 | 2017-10-19 14:16:02 -0400 | [diff] [blame] | 4482 | if (del_item) |
| 4483 | last_size = found_key.offset; |
| 4484 | else |
| 4485 | last_size = new_size; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4486 | if (del_item) { |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4487 | if (!pending_del_nr) { |
| 4488 | /* no pending yet, add ourselves */ |
| 4489 | pending_del_slot = path->slots[0]; |
| 4490 | pending_del_nr = 1; |
| 4491 | } else if (pending_del_nr && |
| 4492 | path->slots[0] + 1 == pending_del_slot) { |
| 4493 | /* hop on the pending chunk */ |
| 4494 | pending_del_nr++; |
| 4495 | pending_del_slot = path->slots[0]; |
| 4496 | } else { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4497 | BUG(); |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4498 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4499 | } else { |
| 4500 | break; |
| 4501 | } |
Thomas Meyer | 897ca81 | 2017-10-07 16:02:21 +0200 | [diff] [blame] | 4502 | should_throttle = false; |
Chris Mason | 28f75a0 | 2015-02-04 06:59:29 -0800 | [diff] [blame] | 4503 | |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 4504 | if (found_extent && |
Qu Wenruo | 82028e0a | 2020-05-15 14:01:41 +0800 | [diff] [blame] | 4505 | root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) { |
Qu Wenruo | ffd4bb2 | 2019-04-04 14:45:36 +0800 | [diff] [blame] | 4506 | struct btrfs_ref ref = { 0 }; |
| 4507 | |
Chris Mason | 28ed134 | 2014-12-17 09:41:04 -0800 | [diff] [blame] | 4508 | bytes_deleted += extent_num_bytes; |
Qu Wenruo | ffd4bb2 | 2019-04-04 14:45:36 +0800 | [diff] [blame] | 4509 | |
| 4510 | btrfs_init_generic_ref(&ref, BTRFS_DROP_DELAYED_REF, |
| 4511 | extent_start, extent_num_bytes, 0); |
| 4512 | ref.real_root = root->root_key.objectid; |
| 4513 | btrfs_init_data_ref(&ref, btrfs_header_owner(leaf), |
| 4514 | ino, extent_offset); |
| 4515 | ret = btrfs_free_extent(trans, &ref); |
Omar Sandoval | 0552210 | 2018-05-11 13:13:31 -0700 | [diff] [blame] | 4516 | if (ret) { |
| 4517 | btrfs_abort_transaction(trans, ret); |
| 4518 | break; |
| 4519 | } |
Chris Mason | 28f75a0 | 2015-02-04 06:59:29 -0800 | [diff] [blame] | 4520 | if (be_nice) { |
Lu Fengqi | 7c86162 | 2018-10-11 13:40:36 +0800 | [diff] [blame] | 4521 | if (btrfs_should_throttle_delayed_refs(trans)) |
Thomas Meyer | 897ca81 | 2017-10-07 16:02:21 +0200 | [diff] [blame] | 4522 | should_throttle = true; |
Chris Mason | 28f75a0 | 2015-02-04 06:59:29 -0800 | [diff] [blame] | 4523 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4524 | } |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4525 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4526 | if (found_type == BTRFS_INODE_ITEM_KEY) |
| 4527 | break; |
| 4528 | |
| 4529 | if (path->slots[0] == 0 || |
Josef Bacik | 1262133 | 2015-02-03 07:50:16 -0800 | [diff] [blame] | 4530 | path->slots[0] != pending_del_slot || |
Josef Bacik | 28bad21 | 2018-12-03 10:20:38 -0500 | [diff] [blame] | 4531 | should_throttle) { |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4532 | if (pending_del_nr) { |
| 4533 | ret = btrfs_del_items(trans, root, path, |
| 4534 | pending_del_slot, |
| 4535 | pending_del_nr); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4536 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 4537 | btrfs_abort_transaction(trans, ret); |
Omar Sandoval | fd86a3a | 2018-05-11 13:13:30 -0700 | [diff] [blame] | 4538 | break; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4539 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4540 | pending_del_nr = 0; |
| 4541 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4542 | btrfs_release_path(path); |
Josef Bacik | 28bad21 | 2018-12-03 10:20:38 -0500 | [diff] [blame] | 4543 | |
Chris Mason | 28f75a0 | 2015-02-04 06:59:29 -0800 | [diff] [blame] | 4544 | /* |
Josef Bacik | 28bad21 | 2018-12-03 10:20:38 -0500 | [diff] [blame] | 4545 | * We can generate a lot of delayed refs, so we need to |
| 4546 | * throttle every once and a while and make sure we're |
| 4547 | * adding enough space to keep up with the work we are |
| 4548 | * generating. Since we hold a transaction here we |
| 4549 | * can't flush, and we don't want to FLUSH_LIMIT because |
| 4550 | * we could have generated too many delayed refs to |
| 4551 | * actually allocate, so just bail if we're short and |
| 4552 | * let the normal reservation dance happen higher up. |
Chris Mason | 28f75a0 | 2015-02-04 06:59:29 -0800 | [diff] [blame] | 4553 | */ |
Josef Bacik | 28bad21 | 2018-12-03 10:20:38 -0500 | [diff] [blame] | 4554 | if (should_throttle) { |
| 4555 | ret = btrfs_delayed_refs_rsv_refill(fs_info, |
| 4556 | BTRFS_RESERVE_NO_FLUSH); |
| 4557 | if (ret) { |
| 4558 | ret = -EAGAIN; |
| 4559 | break; |
| 4560 | } |
Chris Mason | 28f75a0 | 2015-02-04 06:59:29 -0800 | [diff] [blame] | 4561 | } |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4562 | goto search_again; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4563 | } else { |
| 4564 | path->slots[0]--; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4565 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4566 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4567 | out: |
Omar Sandoval | fd86a3a | 2018-05-11 13:13:30 -0700 | [diff] [blame] | 4568 | if (ret >= 0 && pending_del_nr) { |
| 4569 | int err; |
| 4570 | |
| 4571 | err = btrfs_del_items(trans, root, path, pending_del_slot, |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4572 | pending_del_nr); |
Omar Sandoval | fd86a3a | 2018-05-11 13:13:30 -0700 | [diff] [blame] | 4573 | if (err) { |
| 4574 | btrfs_abort_transaction(trans, err); |
| 4575 | ret = err; |
| 4576 | } |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4577 | } |
Filipe Manana | 76b42ab | 2017-02-14 16:56:01 +0000 | [diff] [blame] | 4578 | if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) { |
| 4579 | ASSERT(last_size >= new_size); |
Omar Sandoval | fd86a3a | 2018-05-11 13:13:30 -0700 | [diff] [blame] | 4580 | if (!ret && last_size > new_size) |
Filipe Manana | 76b42ab | 2017-02-14 16:56:01 +0000 | [diff] [blame] | 4581 | last_size = new_size; |
Josef Bacik | d923afe | 2020-01-17 09:02:23 -0500 | [diff] [blame] | 4582 | btrfs_inode_safe_disk_i_size_write(inode, last_size); |
Filipe Manana | a5ae50d | 2020-02-20 13:29:49 +0000 | [diff] [blame] | 4583 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, |
| 4584 | (u64)-1, &cached_state); |
Filipe Manana | 76b42ab | 2017-02-14 16:56:01 +0000 | [diff] [blame] | 4585 | } |
Chris Mason | 28ed134 | 2014-12-17 09:41:04 -0800 | [diff] [blame] | 4586 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4587 | btrfs_free_path(path); |
Omar Sandoval | fd86a3a | 2018-05-11 13:13:30 -0700 | [diff] [blame] | 4588 | return ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4589 | } |
| 4590 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4591 | /* |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4592 | * btrfs_truncate_block - read, zero a chunk and write a block |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 4593 | * @inode - inode that we're zeroing |
| 4594 | * @from - the offset to start zeroing |
| 4595 | * @len - the length to zero, 0 to zero the entire range respective to the |
| 4596 | * offset |
| 4597 | * @front - zero up to the offset instead of from the offset on |
| 4598 | * |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4599 | * 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] | 4600 | * 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] | 4601 | */ |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4602 | int btrfs_truncate_block(struct inode *inode, loff_t from, loff_t len, |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 4603 | int front) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4604 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4605 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 4606 | struct address_space *mapping = inode->i_mapping; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4607 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
| 4608 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4609 | struct extent_state *cached_state = NULL; |
Qu Wenruo | 364ecf3 | 2017-02-27 15:10:38 +0800 | [diff] [blame] | 4610 | struct extent_changeset *data_reserved = NULL; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4611 | char *kaddr; |
Qu Wenruo | 6d4572a | 2020-06-24 07:23:50 +0800 | [diff] [blame] | 4612 | bool only_release_metadata = false; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4613 | u32 blocksize = fs_info->sectorsize; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 4614 | pgoff_t index = from >> PAGE_SHIFT; |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4615 | unsigned offset = from & (blocksize - 1); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4616 | struct page *page; |
Josef Bacik | 3b16a4e | 2011-09-21 15:05:58 -0400 | [diff] [blame] | 4617 | gfp_t mask = btrfs_alloc_write_mask(mapping); |
Qu Wenruo | 6d4572a | 2020-06-24 07:23:50 +0800 | [diff] [blame] | 4618 | size_t write_bytes = blocksize; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4619 | int ret = 0; |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4620 | u64 block_start; |
| 4621 | u64 block_end; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4622 | |
Nikolay Borisov | b03ebd9 | 2018-01-18 14:47:06 +0200 | [diff] [blame] | 4623 | if (IS_ALIGNED(offset, blocksize) && |
| 4624 | (!len || IS_ALIGNED(len, blocksize))) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4625 | goto out; |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4626 | |
Josef Bacik | 8b62f87 | 2017-10-19 14:15:55 -0400 | [diff] [blame] | 4627 | block_start = round_down(from, blocksize); |
| 4628 | block_end = block_start + blocksize - 1; |
| 4629 | |
Nikolay Borisov | 36ea6f3 | 2020-06-03 08:55:41 +0300 | [diff] [blame] | 4630 | ret = btrfs_check_data_free_space(BTRFS_I(inode), &data_reserved, |
| 4631 | block_start, blocksize); |
Qu Wenruo | 6d4572a | 2020-06-24 07:23:50 +0800 | [diff] [blame] | 4632 | if (ret < 0) { |
Qu Wenruo | 38d37aa | 2020-06-24 07:23:52 +0800 | [diff] [blame] | 4633 | if (btrfs_check_nocow_lock(BTRFS_I(inode), block_start, |
| 4634 | &write_bytes) > 0) { |
Qu Wenruo | 6d4572a | 2020-06-24 07:23:50 +0800 | [diff] [blame] | 4635 | /* For nocow case, no need to reserve data space */ |
| 4636 | only_release_metadata = true; |
| 4637 | } else { |
| 4638 | goto out; |
| 4639 | } |
| 4640 | } |
| 4641 | ret = btrfs_delalloc_reserve_metadata(BTRFS_I(inode), blocksize); |
| 4642 | if (ret < 0) { |
| 4643 | if (!only_release_metadata) |
Nikolay Borisov | 25ce28c | 2020-06-03 08:55:39 +0300 | [diff] [blame] | 4644 | btrfs_free_reserved_data_space(BTRFS_I(inode), |
| 4645 | data_reserved, block_start, blocksize); |
Qu Wenruo | 6d4572a | 2020-06-24 07:23:50 +0800 | [diff] [blame] | 4646 | goto out; |
| 4647 | } |
Chris Mason | 211c17f | 2008-05-15 09:13:45 -0400 | [diff] [blame] | 4648 | again: |
Josef Bacik | 3b16a4e | 2011-09-21 15:05:58 -0400 | [diff] [blame] | 4649 | page = find_or_create_page(mapping, index, mask); |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 4650 | if (!page) { |
Nikolay Borisov | 86d5292 | 2020-06-03 08:55:40 +0300 | [diff] [blame] | 4651 | btrfs_delalloc_release_space(BTRFS_I(inode), data_reserved, |
Qu Wenruo | 43b1859 | 2017-12-12 15:34:32 +0800 | [diff] [blame] | 4652 | block_start, blocksize, true); |
Qu Wenruo | 8702ba9 | 2019-10-14 14:34:51 +0800 | [diff] [blame] | 4653 | btrfs_delalloc_release_extents(BTRFS_I(inode), blocksize); |
Miao Xie | ac6a2b3 | 2012-12-05 10:56:13 +0000 | [diff] [blame] | 4654 | ret = -ENOMEM; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4655 | goto out; |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 4656 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4657 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4658 | if (!PageUptodate(page)) { |
| 4659 | ret = btrfs_readpage(NULL, page); |
| 4660 | lock_page(page); |
Chris Mason | 211c17f | 2008-05-15 09:13:45 -0400 | [diff] [blame] | 4661 | if (page->mapping != mapping) { |
| 4662 | unlock_page(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 4663 | put_page(page); |
Chris Mason | 211c17f | 2008-05-15 09:13:45 -0400 | [diff] [blame] | 4664 | goto again; |
| 4665 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4666 | if (!PageUptodate(page)) { |
| 4667 | ret = -EIO; |
Chris Mason | 8964222 | 2008-07-24 09:41:53 -0400 | [diff] [blame] | 4668 | goto out_unlock; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4669 | } |
| 4670 | } |
Chris Mason | 211c17f | 2008-05-15 09:13:45 -0400 | [diff] [blame] | 4671 | wait_on_page_writeback(page); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4672 | |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4673 | lock_extent_bits(io_tree, block_start, block_end, &cached_state); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4674 | set_page_extent_mapped(page); |
| 4675 | |
Nikolay Borisov | c350437 | 2020-06-03 08:55:03 +0300 | [diff] [blame] | 4676 | ordered = btrfs_lookup_ordered_extent(BTRFS_I(inode), block_start); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4677 | if (ordered) { |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4678 | unlock_extent_cached(io_tree, block_start, block_end, |
David Sterba | e43bbe5 | 2017-12-12 21:43:52 +0100 | [diff] [blame] | 4679 | &cached_state); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4680 | unlock_page(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 4681 | put_page(page); |
Nikolay Borisov | c0a4360 | 2020-09-18 12:15:53 +0300 | [diff] [blame] | 4682 | btrfs_start_ordered_extent(ordered, 1); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4683 | btrfs_put_ordered_extent(ordered); |
| 4684 | goto again; |
| 4685 | } |
| 4686 | |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4687 | clear_extent_bit(&BTRFS_I(inode)->io_tree, block_start, block_end, |
Omar Sandoval | e182163 | 2019-08-15 14:04:04 -0700 | [diff] [blame] | 4688 | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, |
| 4689 | 0, 0, &cached_state); |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 4690 | |
Nikolay Borisov | c2566f2 | 2020-06-03 08:55:35 +0300 | [diff] [blame] | 4691 | ret = btrfs_set_extent_delalloc(BTRFS_I(inode), block_start, block_end, 0, |
Nikolay Borisov | 330a582 | 2019-07-17 16:18:17 +0300 | [diff] [blame] | 4692 | &cached_state); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4693 | if (ret) { |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4694 | unlock_extent_cached(io_tree, block_start, block_end, |
David Sterba | e43bbe5 | 2017-12-12 21:43:52 +0100 | [diff] [blame] | 4695 | &cached_state); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4696 | goto out_unlock; |
| 4697 | } |
| 4698 | |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4699 | if (offset != blocksize) { |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 4700 | if (!len) |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4701 | len = blocksize - offset; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4702 | kaddr = kmap(page); |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 4703 | if (front) |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4704 | memset(kaddr + (block_start - page_offset(page)), |
| 4705 | 0, offset); |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 4706 | else |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4707 | memset(kaddr + (block_start - page_offset(page)) + offset, |
| 4708 | 0, len); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4709 | flush_dcache_page(page); |
| 4710 | kunmap(page); |
| 4711 | } |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 4712 | ClearPageChecked(page); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4713 | set_page_dirty(page); |
David Sterba | e43bbe5 | 2017-12-12 21:43:52 +0100 | [diff] [blame] | 4714 | unlock_extent_cached(io_tree, block_start, block_end, &cached_state); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4715 | |
Qu Wenruo | 6d4572a | 2020-06-24 07:23:50 +0800 | [diff] [blame] | 4716 | if (only_release_metadata) |
| 4717 | set_extent_bit(&BTRFS_I(inode)->io_tree, block_start, |
Qu Wenruo | 3f6bb4a | 2020-10-21 14:24:51 +0800 | [diff] [blame] | 4718 | block_end, EXTENT_NORESERVE, NULL, GFP_NOFS); |
Qu Wenruo | 6d4572a | 2020-06-24 07:23:50 +0800 | [diff] [blame] | 4719 | |
Chris Mason | 8964222 | 2008-07-24 09:41:53 -0400 | [diff] [blame] | 4720 | out_unlock: |
Qu Wenruo | 6d4572a | 2020-06-24 07:23:50 +0800 | [diff] [blame] | 4721 | if (ret) { |
| 4722 | if (only_release_metadata) |
| 4723 | btrfs_delalloc_release_metadata(BTRFS_I(inode), |
| 4724 | blocksize, true); |
| 4725 | else |
Nikolay Borisov | 86d5292 | 2020-06-03 08:55:40 +0300 | [diff] [blame] | 4726 | btrfs_delalloc_release_space(BTRFS_I(inode), data_reserved, |
Qu Wenruo | 6d4572a | 2020-06-24 07:23:50 +0800 | [diff] [blame] | 4727 | block_start, blocksize, true); |
| 4728 | } |
Qu Wenruo | 8702ba9 | 2019-10-14 14:34:51 +0800 | [diff] [blame] | 4729 | btrfs_delalloc_release_extents(BTRFS_I(inode), blocksize); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4730 | unlock_page(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 4731 | put_page(page); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4732 | out: |
Qu Wenruo | 6d4572a | 2020-06-24 07:23:50 +0800 | [diff] [blame] | 4733 | if (only_release_metadata) |
Qu Wenruo | 38d37aa | 2020-06-24 07:23:52 +0800 | [diff] [blame] | 4734 | btrfs_check_nocow_unlock(BTRFS_I(inode)); |
Qu Wenruo | 364ecf3 | 2017-02-27 15:10:38 +0800 | [diff] [blame] | 4735 | extent_changeset_free(data_reserved); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4736 | return ret; |
| 4737 | } |
| 4738 | |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4739 | static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode, |
| 4740 | u64 offset, u64 len) |
| 4741 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4742 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4743 | struct btrfs_trans_handle *trans; |
| 4744 | int ret; |
| 4745 | |
| 4746 | /* |
| 4747 | * Still need to make sure the inode looks like it's been updated so |
| 4748 | * that any holes get logged if we fsync. |
| 4749 | */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4750 | if (btrfs_fs_incompat(fs_info, NO_HOLES)) { |
| 4751 | BTRFS_I(inode)->last_trans = fs_info->generation; |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4752 | BTRFS_I(inode)->last_sub_trans = root->log_transid; |
| 4753 | BTRFS_I(inode)->last_log_commit = root->last_log_commit; |
| 4754 | return 0; |
| 4755 | } |
| 4756 | |
| 4757 | /* |
| 4758 | * 1 - for the one we're dropping |
| 4759 | * 1 - for the one we're adding |
| 4760 | * 1 - for updating the inode. |
| 4761 | */ |
| 4762 | trans = btrfs_start_transaction(root, 3); |
| 4763 | if (IS_ERR(trans)) |
| 4764 | return PTR_ERR(trans); |
| 4765 | |
| 4766 | ret = btrfs_drop_extents(trans, root, inode, offset, offset + len, 1); |
| 4767 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 4768 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 4769 | btrfs_end_transaction(trans); |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4770 | return ret; |
| 4771 | } |
| 4772 | |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 4773 | ret = btrfs_insert_file_extent(trans, root, btrfs_ino(BTRFS_I(inode)), |
| 4774 | offset, 0, 0, len, 0, len, 0, 0, 0); |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4775 | if (ret) |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 4776 | btrfs_abort_transaction(trans, ret); |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4777 | else |
| 4778 | btrfs_update_inode(trans, root, inode); |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 4779 | btrfs_end_transaction(trans); |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4780 | return ret; |
| 4781 | } |
| 4782 | |
Josef Bacik | 695a0d0 | 2011-03-04 15:46:53 -0500 | [diff] [blame] | 4783 | /* |
| 4784 | * This function puts in dummy file extents for the area we're creating a hole |
| 4785 | * for. So if we are truncating this file to a larger size we need to insert |
| 4786 | * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for |
| 4787 | * the range between oldsize and size |
| 4788 | */ |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 4789 | int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size) |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4790 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4791 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4792 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 4793 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4794 | struct extent_map *em = NULL; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4795 | struct extent_state *cached_state = NULL; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4796 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4797 | u64 hole_start = ALIGN(oldsize, fs_info->sectorsize); |
| 4798 | u64 block_end = ALIGN(size, fs_info->sectorsize); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4799 | u64 last_byte; |
| 4800 | u64 cur_offset; |
| 4801 | u64 hole_size; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4802 | int err = 0; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4803 | |
Josef Bacik | a71754f | 2013-06-17 17:14:39 -0400 | [diff] [blame] | 4804 | /* |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4805 | * If our size started in the middle of a block we need to zero out the |
| 4806 | * 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] | 4807 | * expose stale data. |
| 4808 | */ |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4809 | err = btrfs_truncate_block(inode, oldsize, 0, 0); |
Josef Bacik | a71754f | 2013-06-17 17:14:39 -0400 | [diff] [blame] | 4810 | if (err) |
| 4811 | return err; |
| 4812 | |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4813 | if (size <= hole_start) |
| 4814 | return 0; |
| 4815 | |
David Sterba | b272ae2 | 2020-02-05 19:09:33 +0100 | [diff] [blame] | 4816 | btrfs_lock_and_flush_ordered_range(BTRFS_I(inode), hole_start, |
Nikolay Borisov | 23d31bd | 2019-05-07 10:19:23 +0300 | [diff] [blame] | 4817 | block_end - 1, &cached_state); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4818 | cur_offset = hole_start; |
| 4819 | while (1) { |
Nikolay Borisov | fc4f21b1 | 2017-02-20 13:51:06 +0200 | [diff] [blame] | 4820 | em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, cur_offset, |
Omar Sandoval | 39b07b5 | 2019-12-02 17:34:23 -0800 | [diff] [blame] | 4821 | block_end - cur_offset); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4822 | if (IS_ERR(em)) { |
| 4823 | err = PTR_ERR(em); |
Zach Brown | f276795 | 2013-01-08 19:37:58 +0000 | [diff] [blame] | 4824 | em = NULL; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4825 | break; |
| 4826 | } |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4827 | last_byte = min(extent_map_end(em), block_end); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4828 | last_byte = ALIGN(last_byte, fs_info->sectorsize); |
Josef Bacik | 9ddc959 | 2020-01-17 09:02:22 -0500 | [diff] [blame] | 4829 | hole_size = last_byte - cur_offset; |
| 4830 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4831 | if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) { |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4832 | struct extent_map *hole_em; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4833 | |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4834 | err = maybe_insert_hole(root, inode, cur_offset, |
| 4835 | hole_size); |
| 4836 | if (err) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 4837 | break; |
Josef Bacik | 9ddc959 | 2020-01-17 09:02:22 -0500 | [diff] [blame] | 4838 | |
| 4839 | err = btrfs_inode_set_file_extent_range(BTRFS_I(inode), |
| 4840 | cur_offset, hole_size); |
| 4841 | if (err) |
| 4842 | break; |
| 4843 | |
Nikolay Borisov | dcdbc05 | 2017-02-20 13:50:45 +0200 | [diff] [blame] | 4844 | btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset, |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4845 | cur_offset + hole_size - 1, 0); |
| 4846 | hole_em = alloc_extent_map(); |
| 4847 | if (!hole_em) { |
| 4848 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, |
| 4849 | &BTRFS_I(inode)->runtime_flags); |
| 4850 | goto next; |
| 4851 | } |
| 4852 | hole_em->start = cur_offset; |
| 4853 | hole_em->len = hole_size; |
| 4854 | hole_em->orig_start = cur_offset; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4855 | |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4856 | hole_em->block_start = EXTENT_MAP_HOLE; |
| 4857 | hole_em->block_len = 0; |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 4858 | hole_em->orig_block_len = 0; |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 4859 | hole_em->ram_bytes = hole_size; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4860 | hole_em->compress_type = BTRFS_COMPRESS_NONE; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4861 | hole_em->generation = fs_info->generation; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4862 | |
| 4863 | while (1) { |
| 4864 | write_lock(&em_tree->lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 4865 | err = add_extent_mapping(em_tree, hole_em, 1); |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4866 | write_unlock(&em_tree->lock); |
| 4867 | if (err != -EEXIST) |
| 4868 | break; |
Nikolay Borisov | dcdbc05 | 2017-02-20 13:50:45 +0200 | [diff] [blame] | 4869 | btrfs_drop_extent_cache(BTRFS_I(inode), |
| 4870 | cur_offset, |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4871 | cur_offset + |
| 4872 | hole_size - 1, 0); |
| 4873 | } |
| 4874 | free_extent_map(hole_em); |
Josef Bacik | 9ddc959 | 2020-01-17 09:02:22 -0500 | [diff] [blame] | 4875 | } else { |
| 4876 | err = btrfs_inode_set_file_extent_range(BTRFS_I(inode), |
| 4877 | cur_offset, hole_size); |
| 4878 | if (err) |
| 4879 | break; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4880 | } |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4881 | next: |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4882 | free_extent_map(em); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4883 | em = NULL; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4884 | cur_offset = last_byte; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4885 | if (cur_offset >= block_end) |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4886 | break; |
| 4887 | } |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4888 | free_extent_map(em); |
David Sterba | e43bbe5 | 2017-12-12 21:43:52 +0100 | [diff] [blame] | 4889 | unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4890 | return err; |
| 4891 | } |
| 4892 | |
Eric Sandeen | 3972f26 | 2013-01-12 02:57:22 +0000 | [diff] [blame] | 4893 | static int btrfs_setsize(struct inode *inode, struct iattr *attr) |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4894 | { |
Miao Xie | f4a2f4c | 2011-12-14 20:12:01 -0500 | [diff] [blame] | 4895 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 4896 | struct btrfs_trans_handle *trans; |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 4897 | loff_t oldsize = i_size_read(inode); |
Eric Sandeen | 3972f26 | 2013-01-12 02:57:22 +0000 | [diff] [blame] | 4898 | loff_t newsize = attr->ia_size; |
| 4899 | int mask = attr->ia_valid; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4900 | int ret; |
| 4901 | |
Eric Sandeen | 3972f26 | 2013-01-12 02:57:22 +0000 | [diff] [blame] | 4902 | /* |
| 4903 | * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a |
| 4904 | * special case where we need to update the times despite not having |
| 4905 | * these flags set. For all other operations the VFS set these flags |
| 4906 | * explicitly if it wants a timestamp update. |
| 4907 | */ |
Christoph Hellwig | dff6efc | 2013-11-19 07:17:07 -0800 | [diff] [blame] | 4908 | if (newsize != oldsize) { |
| 4909 | inode_inc_iversion(inode); |
| 4910 | if (!(mask & (ATTR_CTIME | ATTR_MTIME))) |
| 4911 | inode->i_ctime = inode->i_mtime = |
Deepa Dinamani | c2050a4 | 2016-09-14 07:48:06 -0700 | [diff] [blame] | 4912 | current_time(inode); |
Christoph Hellwig | dff6efc | 2013-11-19 07:17:07 -0800 | [diff] [blame] | 4913 | } |
Eric Sandeen | 3972f26 | 2013-01-12 02:57:22 +0000 | [diff] [blame] | 4914 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 4915 | if (newsize > oldsize) { |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 4916 | /* |
David Sterba | ea14b57f | 2017-06-22 02:19:11 +0200 | [diff] [blame] | 4917 | * Don't do an expanding truncate while snapshotting is ongoing. |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 4918 | * This is to ensure the snapshot captures a fully consistent |
| 4919 | * state of this file - if the snapshot captures this expanding |
| 4920 | * truncation, it must capture all writes that happened before |
| 4921 | * this truncation. |
| 4922 | */ |
Nikolay Borisov | dcc3eb9 | 2020-01-30 14:59:45 +0200 | [diff] [blame] | 4923 | btrfs_drew_write_lock(&root->snapshot_lock); |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 4924 | ret = btrfs_cont_expand(inode, oldsize, newsize); |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 4925 | if (ret) { |
Nikolay Borisov | dcc3eb9 | 2020-01-30 14:59:45 +0200 | [diff] [blame] | 4926 | btrfs_drew_write_unlock(&root->snapshot_lock); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4927 | return ret; |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 4928 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4929 | |
Miao Xie | f4a2f4c | 2011-12-14 20:12:01 -0500 | [diff] [blame] | 4930 | trans = btrfs_start_transaction(root, 1); |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 4931 | if (IS_ERR(trans)) { |
Nikolay Borisov | dcc3eb9 | 2020-01-30 14:59:45 +0200 | [diff] [blame] | 4932 | btrfs_drew_write_unlock(&root->snapshot_lock); |
Miao Xie | f4a2f4c | 2011-12-14 20:12:01 -0500 | [diff] [blame] | 4933 | return PTR_ERR(trans); |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 4934 | } |
Miao Xie | f4a2f4c | 2011-12-14 20:12:01 -0500 | [diff] [blame] | 4935 | |
| 4936 | i_size_write(inode, newsize); |
Josef Bacik | d923afe | 2020-01-17 09:02:23 -0500 | [diff] [blame] | 4937 | btrfs_inode_safe_disk_i_size_write(inode, 0); |
Chandan Rajendra | 27772b6 | 2016-01-21 15:56:03 +0530 | [diff] [blame] | 4938 | pagecache_isize_extended(inode, oldsize, newsize); |
Miao Xie | f4a2f4c | 2011-12-14 20:12:01 -0500 | [diff] [blame] | 4939 | ret = btrfs_update_inode(trans, root, inode); |
Nikolay Borisov | dcc3eb9 | 2020-01-30 14:59:45 +0200 | [diff] [blame] | 4940 | btrfs_drew_write_unlock(&root->snapshot_lock); |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 4941 | btrfs_end_transaction(trans); |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 4942 | } else { |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4943 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 4944 | /* |
| 4945 | * 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] | 4946 | * zero. Make sure any new writes to the file get on disk |
| 4947 | * on close. |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 4948 | */ |
| 4949 | if (newsize == 0) |
Nikolay Borisov | 1fd4033 | 2020-10-01 09:40:39 +0300 | [diff] [blame] | 4950 | set_bit(BTRFS_INODE_FLUSH_ON_CLOSE, |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 4951 | &BTRFS_I(inode)->runtime_flags); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4952 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 4953 | truncate_setsize(inode, newsize); |
Miao Xie | 2e60a51 | 2013-02-08 07:01:08 +0000 | [diff] [blame] | 4954 | |
Miao Xie | 2e60a51 | 2013-02-08 07:01:08 +0000 | [diff] [blame] | 4955 | inode_dio_wait(inode); |
Miao Xie | 2e60a51 | 2013-02-08 07:01:08 +0000 | [diff] [blame] | 4956 | |
Filipe Manana | 213e8c5 | 2018-02-06 20:40:31 +0000 | [diff] [blame] | 4957 | ret = btrfs_truncate(inode, newsize == oldsize); |
Josef Bacik | 7f4f6e0 | 2013-08-29 16:43:28 -0400 | [diff] [blame] | 4958 | if (ret && inode->i_nlink) { |
| 4959 | int err; |
| 4960 | |
| 4961 | /* |
Omar Sandoval | f7e9e8f | 2018-05-11 13:13:32 -0700 | [diff] [blame] | 4962 | * Truncate failed, so fix up the in-memory size. We |
| 4963 | * adjusted disk_i_size down as we removed extents, so |
| 4964 | * wait for disk_i_size to be stable and then update the |
| 4965 | * in-memory size to match. |
Josef Bacik | 7f4f6e0 | 2013-08-29 16:43:28 -0400 | [diff] [blame] | 4966 | */ |
Omar Sandoval | f7e9e8f | 2018-05-11 13:13:32 -0700 | [diff] [blame] | 4967 | err = btrfs_wait_ordered_range(inode, 0, (u64)-1); |
Josef Bacik | 7f4f6e0 | 2013-08-29 16:43:28 -0400 | [diff] [blame] | 4968 | if (err) |
Omar Sandoval | f7e9e8f | 2018-05-11 13:13:32 -0700 | [diff] [blame] | 4969 | return err; |
| 4970 | i_size_write(inode, BTRFS_I(inode)->disk_i_size); |
Josef Bacik | 7f4f6e0 | 2013-08-29 16:43:28 -0400 | [diff] [blame] | 4971 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4972 | } |
| 4973 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 4974 | return ret; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4975 | } |
| 4976 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4977 | static int btrfs_setattr(struct dentry *dentry, struct iattr *attr) |
| 4978 | { |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 4979 | struct inode *inode = d_inode(dentry); |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 4980 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4981 | int err; |
| 4982 | |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 4983 | if (btrfs_root_readonly(root)) |
| 4984 | return -EROFS; |
| 4985 | |
Jan Kara | 31051c8 | 2016-05-26 16:55:18 +0200 | [diff] [blame] | 4986 | err = setattr_prepare(dentry, attr); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4987 | if (err) |
| 4988 | return err; |
| 4989 | |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 4990 | if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) { |
Eric Sandeen | 3972f26 | 2013-01-12 02:57:22 +0000 | [diff] [blame] | 4991 | err = btrfs_setsize(inode, attr); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4992 | if (err) |
| 4993 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4994 | } |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4995 | |
Christoph Hellwig | 1025774 | 2010-06-04 11:30:02 +0200 | [diff] [blame] | 4996 | if (attr->ia_valid) { |
| 4997 | setattr_copy(inode, attr); |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 4998 | inode_inc_iversion(inode); |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 4999 | err = btrfs_dirty_inode(inode); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 5000 | |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 5001 | if (!err && attr->ia_valid & ATTR_MODE) |
Christoph Hellwig | 996a710 | 2013-12-20 05:16:43 -0800 | [diff] [blame] | 5002 | err = posix_acl_chmod(inode, inode->i_mode); |
Christoph Hellwig | 1025774 | 2010-06-04 11:30:02 +0200 | [diff] [blame] | 5003 | } |
| 5004 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5005 | return err; |
| 5006 | } |
Chris Mason | 61295eb | 2008-01-14 16:24:38 -0500 | [diff] [blame] | 5007 | |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5008 | /* |
| 5009 | * While truncating the inode pages during eviction, we get the VFS calling |
| 5010 | * btrfs_invalidatepage() against each page of the inode. This is slow because |
| 5011 | * the calls to btrfs_invalidatepage() result in a huge amount of calls to |
| 5012 | * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting |
| 5013 | * extent_state structures over and over, wasting lots of time. |
| 5014 | * |
| 5015 | * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all |
| 5016 | * those expensive operations on a per page basis and do only the ordered io |
| 5017 | * finishing, while we release here the extent_map and extent_state structures, |
| 5018 | * without the excessive merging and splitting. |
| 5019 | */ |
| 5020 | static void evict_inode_truncate_pages(struct inode *inode) |
| 5021 | { |
| 5022 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
| 5023 | struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree; |
| 5024 | struct rb_node *node; |
| 5025 | |
| 5026 | ASSERT(inode->i_state & I_FREEING); |
Johannes Weiner | 91b0abe | 2014-04-03 14:47:49 -0700 | [diff] [blame] | 5027 | truncate_inode_pages_final(&inode->i_data); |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5028 | |
| 5029 | write_lock(&map_tree->lock); |
Liu Bo | 07e1ce0 | 2018-08-23 03:51:52 +0800 | [diff] [blame] | 5030 | while (!RB_EMPTY_ROOT(&map_tree->map.rb_root)) { |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5031 | struct extent_map *em; |
| 5032 | |
Liu Bo | 07e1ce0 | 2018-08-23 03:51:52 +0800 | [diff] [blame] | 5033 | node = rb_first_cached(&map_tree->map); |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5034 | em = rb_entry(node, struct extent_map, rb_node); |
Wang Shilong | 180589e | 2013-12-14 15:27:31 +0800 | [diff] [blame] | 5035 | clear_bit(EXTENT_FLAG_PINNED, &em->flags); |
| 5036 | clear_bit(EXTENT_FLAG_LOGGING, &em->flags); |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5037 | remove_extent_mapping(map_tree, em); |
| 5038 | free_extent_map(em); |
Filipe Manana | 7064dd5 | 2014-08-08 02:47:05 +0100 | [diff] [blame] | 5039 | if (need_resched()) { |
| 5040 | write_unlock(&map_tree->lock); |
| 5041 | cond_resched(); |
| 5042 | write_lock(&map_tree->lock); |
| 5043 | } |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5044 | } |
| 5045 | write_unlock(&map_tree->lock); |
| 5046 | |
Filipe Manana | 6ca0709 | 2015-05-26 00:55:42 +0100 | [diff] [blame] | 5047 | /* |
| 5048 | * 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] | 5049 | * We can have ongoing bios started by readahead that have |
| 5050 | * their endio callback (extent_io.c:end_bio_extent_readpage) |
Filipe Manana | 9c6429d | 2015-06-10 12:55:41 +0100 | [diff] [blame] | 5051 | * still in progress (unlocked the pages in the bio but did not yet |
| 5052 | * unlocked the ranges in the io tree). Therefore this means some |
Filipe Manana | 6ca0709 | 2015-05-26 00:55:42 +0100 | [diff] [blame] | 5053 | * ranges can still be locked and eviction started because before |
| 5054 | * submitting those bios, which are executed by a separate task (work |
| 5055 | * queue kthread), inode references (inode->i_count) were not taken |
| 5056 | * (which would be dropped in the end io callback of each bio). |
| 5057 | * Therefore here we effectively end up waiting for those bios and |
| 5058 | * anyone else holding locked ranges without having bumped the inode's |
| 5059 | * reference count - if we don't do it, when they access the inode's |
| 5060 | * io_tree to unlock a range it may be too late, leading to an |
| 5061 | * use-after-free issue. |
| 5062 | */ |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5063 | spin_lock(&io_tree->lock); |
| 5064 | while (!RB_EMPTY_ROOT(&io_tree->state)) { |
| 5065 | struct extent_state *state; |
| 5066 | struct extent_state *cached_state = NULL; |
Filipe Manana | 6ca0709 | 2015-05-26 00:55:42 +0100 | [diff] [blame] | 5067 | u64 start; |
| 5068 | u64 end; |
Filipe Manana | 421f092 | 2018-10-12 13:02:48 +0100 | [diff] [blame] | 5069 | unsigned state_flags; |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5070 | |
| 5071 | node = rb_first(&io_tree->state); |
| 5072 | state = rb_entry(node, struct extent_state, rb_node); |
Filipe Manana | 6ca0709 | 2015-05-26 00:55:42 +0100 | [diff] [blame] | 5073 | start = state->start; |
| 5074 | end = state->end; |
Filipe Manana | 421f092 | 2018-10-12 13:02:48 +0100 | [diff] [blame] | 5075 | state_flags = state->state; |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5076 | spin_unlock(&io_tree->lock); |
| 5077 | |
David Sterba | ff13db4 | 2015-12-03 14:30:40 +0100 | [diff] [blame] | 5078 | lock_extent_bits(io_tree, start, end, &cached_state); |
Qu Wenruo | b9d0b38 | 2015-09-29 10:35:16 +0800 | [diff] [blame] | 5079 | |
| 5080 | /* |
| 5081 | * If still has DELALLOC flag, the extent didn't reach disk, |
| 5082 | * and its reserved space won't be freed by delayed_ref. |
| 5083 | * So we need to free its reserved space here. |
| 5084 | * (Refer to comment in btrfs_invalidatepage, case 2) |
| 5085 | * |
| 5086 | * Note, end is the bytenr of last byte, so we need + 1 here. |
| 5087 | */ |
Filipe Manana | 421f092 | 2018-10-12 13:02:48 +0100 | [diff] [blame] | 5088 | if (state_flags & EXTENT_DELALLOC) |
Nikolay Borisov | 8b8a979 | 2020-06-03 08:55:11 +0300 | [diff] [blame] | 5089 | btrfs_qgroup_free_data(BTRFS_I(inode), NULL, start, |
| 5090 | end - start + 1); |
Qu Wenruo | b9d0b38 | 2015-09-29 10:35:16 +0800 | [diff] [blame] | 5091 | |
Filipe Manana | 6ca0709 | 2015-05-26 00:55:42 +0100 | [diff] [blame] | 5092 | clear_extent_bit(io_tree, start, end, |
Omar Sandoval | e182163 | 2019-08-15 14:04:04 -0700 | [diff] [blame] | 5093 | EXTENT_LOCKED | EXTENT_DELALLOC | |
| 5094 | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1, |
| 5095 | &cached_state); |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5096 | |
Filipe Manana | 7064dd5 | 2014-08-08 02:47:05 +0100 | [diff] [blame] | 5097 | cond_resched(); |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5098 | spin_lock(&io_tree->lock); |
| 5099 | } |
| 5100 | spin_unlock(&io_tree->lock); |
| 5101 | } |
| 5102 | |
Omar Sandoval | 4b9d7b5 | 2018-05-11 13:13:36 -0700 | [diff] [blame] | 5103 | 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] | 5104 | struct btrfs_block_rsv *rsv) |
Omar Sandoval | 4b9d7b5 | 2018-05-11 13:13:36 -0700 | [diff] [blame] | 5105 | { |
| 5106 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 5107 | struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv; |
Josef Bacik | d3984c9 | 2019-08-01 18:19:37 -0400 | [diff] [blame] | 5108 | struct btrfs_trans_handle *trans; |
Josef Bacik | 2bd36e7 | 2019-08-22 15:14:33 -0400 | [diff] [blame] | 5109 | u64 delayed_refs_extra = btrfs_calc_insert_metadata_size(fs_info, 1); |
Josef Bacik | d3984c9 | 2019-08-01 18:19:37 -0400 | [diff] [blame] | 5110 | int ret; |
Omar Sandoval | 4b9d7b5 | 2018-05-11 13:13:36 -0700 | [diff] [blame] | 5111 | |
Josef Bacik | d3984c9 | 2019-08-01 18:19:37 -0400 | [diff] [blame] | 5112 | /* |
| 5113 | * Eviction should be taking place at some place safe because of our |
| 5114 | * delayed iputs. However the normal flushing code will run delayed |
| 5115 | * iputs, so we cannot use FLUSH_ALL otherwise we'll deadlock. |
| 5116 | * |
| 5117 | * We reserve the delayed_refs_extra here again because we can't use |
| 5118 | * btrfs_start_transaction(root, 0) for the same deadlocky reason as |
| 5119 | * above. We reserve our extra bit here because we generate a ton of |
| 5120 | * delayed refs activity by truncating. |
| 5121 | * |
| 5122 | * If we cannot make our reservation we'll attempt to steal from the |
| 5123 | * global reserve, because we really want to be able to free up space. |
| 5124 | */ |
| 5125 | ret = btrfs_block_rsv_refill(root, rsv, rsv->size + delayed_refs_extra, |
| 5126 | BTRFS_RESERVE_FLUSH_EVICT); |
| 5127 | if (ret) { |
Omar Sandoval | 4b9d7b5 | 2018-05-11 13:13:36 -0700 | [diff] [blame] | 5128 | /* |
| 5129 | * Try to steal from the global reserve if there is space for |
| 5130 | * it. |
| 5131 | */ |
Josef Bacik | d3984c9 | 2019-08-01 18:19:37 -0400 | [diff] [blame] | 5132 | if (btrfs_check_space_for_delayed_refs(fs_info) || |
| 5133 | btrfs_block_rsv_migrate(global_rsv, rsv, rsv->size, 0)) { |
| 5134 | btrfs_warn(fs_info, |
| 5135 | "could not allocate space for delete; will truncate on mount"); |
| 5136 | return ERR_PTR(-ENOSPC); |
| 5137 | } |
| 5138 | delayed_refs_extra = 0; |
Omar Sandoval | 4b9d7b5 | 2018-05-11 13:13:36 -0700 | [diff] [blame] | 5139 | } |
Josef Bacik | d3984c9 | 2019-08-01 18:19:37 -0400 | [diff] [blame] | 5140 | |
| 5141 | trans = btrfs_join_transaction(root); |
| 5142 | if (IS_ERR(trans)) |
| 5143 | return trans; |
| 5144 | |
| 5145 | if (delayed_refs_extra) { |
| 5146 | trans->block_rsv = &fs_info->trans_block_rsv; |
| 5147 | trans->bytes_reserved = delayed_refs_extra; |
| 5148 | btrfs_block_rsv_migrate(rsv, trans->block_rsv, |
| 5149 | delayed_refs_extra, 1); |
| 5150 | } |
| 5151 | return trans; |
Omar Sandoval | 4b9d7b5 | 2018-05-11 13:13:36 -0700 | [diff] [blame] | 5152 | } |
| 5153 | |
Al Viro | bd55597 | 2010-06-07 11:35:40 -0400 | [diff] [blame] | 5154 | void btrfs_evict_inode(struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5155 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5156 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5157 | struct btrfs_trans_handle *trans; |
| 5158 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Omar Sandoval | 4b9d7b5 | 2018-05-11 13:13:36 -0700 | [diff] [blame] | 5159 | struct btrfs_block_rsv *rsv; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5160 | int ret; |
| 5161 | |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 5162 | trace_btrfs_inode_evict(inode); |
| 5163 | |
Nikolay Borisov | 3d48d98 | 2016-06-29 09:46:41 +0300 | [diff] [blame] | 5164 | if (!root) { |
Liu Bo | e8f1bc1 | 2018-01-25 11:02:53 -0700 | [diff] [blame] | 5165 | clear_inode(inode); |
Nikolay Borisov | 3d48d98 | 2016-06-29 09:46:41 +0300 | [diff] [blame] | 5166 | return; |
| 5167 | } |
| 5168 | |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5169 | evict_inode_truncate_pages(inode); |
| 5170 | |
Stefan Behrens | 69e9c6c | 2013-09-05 16:58:43 +0200 | [diff] [blame] | 5171 | if (inode->i_nlink && |
| 5172 | ((btrfs_root_refs(&root->root_item) != 0 && |
| 5173 | root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) || |
Nikolay Borisov | 70ddc55 | 2017-02-20 13:50:35 +0200 | [diff] [blame] | 5174 | btrfs_is_free_space_inode(BTRFS_I(inode)))) |
Al Viro | bd55597 | 2010-06-07 11:35:40 -0400 | [diff] [blame] | 5175 | goto no_delete; |
| 5176 | |
Omar Sandoval | 2791906 | 2018-05-11 13:13:37 -0700 | [diff] [blame] | 5177 | if (is_bad_inode(inode)) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5178 | goto no_delete; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5179 | |
Nikolay Borisov | 7ab7956 | 2017-02-20 13:50:57 +0200 | [diff] [blame] | 5180 | btrfs_free_io_failure_record(BTRFS_I(inode), 0, (u64)-1); |
Miao Xie | f612496 | 2014-09-12 18:44:04 +0800 | [diff] [blame] | 5181 | |
Omar Sandoval | 7b40b69 | 2018-05-11 13:13:33 -0700 | [diff] [blame] | 5182 | if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 5183 | goto no_delete; |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 5184 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5185 | if (inode->i_nlink > 0) { |
Stefan Behrens | 69e9c6c | 2013-09-05 16:58:43 +0200 | [diff] [blame] | 5186 | BUG_ON(btrfs_root_refs(&root->root_item) != 0 && |
| 5187 | root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5188 | goto no_delete; |
| 5189 | } |
| 5190 | |
Nikolay Borisov | aa79021 | 2017-01-10 20:35:40 +0200 | [diff] [blame] | 5191 | ret = btrfs_commit_inode_delayed_inode(BTRFS_I(inode)); |
Omar Sandoval | 2791906 | 2018-05-11 13:13:37 -0700 | [diff] [blame] | 5192 | if (ret) |
Miao Xie | 0e8c36a | 2012-12-19 06:59:51 +0000 | [diff] [blame] | 5193 | goto no_delete; |
Miao Xie | 0e8c36a | 2012-12-19 06:59:51 +0000 | [diff] [blame] | 5194 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5195 | rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP); |
Omar Sandoval | 2791906 | 2018-05-11 13:13:37 -0700 | [diff] [blame] | 5196 | if (!rsv) |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 5197 | goto no_delete; |
Josef Bacik | 2bd36e7 | 2019-08-22 15:14:33 -0400 | [diff] [blame] | 5198 | rsv->size = btrfs_calc_metadata_size(fs_info, 1); |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 5199 | rsv->failfast = 1; |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 5200 | |
Nikolay Borisov | 6ef06d2 | 2017-02-20 13:50:34 +0200 | [diff] [blame] | 5201 | btrfs_i_size_write(BTRFS_I(inode), 0); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5202 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5203 | while (1) { |
Josef Bacik | ad80cf5 | 2018-09-28 07:18:19 -0400 | [diff] [blame] | 5204 | trans = evict_refill_and_join(root, rsv); |
Omar Sandoval | 2791906 | 2018-05-11 13:13:37 -0700 | [diff] [blame] | 5205 | if (IS_ERR(trans)) |
| 5206 | goto free_rsv; |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 5207 | |
| 5208 | trans->block_rsv = rsv; |
| 5209 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5210 | ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0); |
Omar Sandoval | 2791906 | 2018-05-11 13:13:37 -0700 | [diff] [blame] | 5211 | trans->block_rsv = &fs_info->trans_block_rsv; |
| 5212 | btrfs_end_transaction(trans); |
| 5213 | btrfs_btree_balance_dirty(fs_info); |
| 5214 | if (ret && ret != -ENOSPC && ret != -EAGAIN) |
| 5215 | goto free_rsv; |
| 5216 | else if (!ret) |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5217 | break; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 5218 | } |
| 5219 | |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 5220 | /* |
Omar Sandoval | 2791906 | 2018-05-11 13:13:37 -0700 | [diff] [blame] | 5221 | * Errors here aren't a big deal, it just means we leave orphan items in |
| 5222 | * the tree. They will be cleaned up on the next mount. If the inode |
| 5223 | * number gets reused, cleanup deletes the orphan item without doing |
| 5224 | * anything, and unlink reuses the existing orphan item. |
| 5225 | * |
| 5226 | * If it turns out that we are dropping too many of these, we might want |
| 5227 | * to add a mechanism for retrying these after a commit. |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 5228 | */ |
Josef Bacik | ad80cf5 | 2018-09-28 07:18:19 -0400 | [diff] [blame] | 5229 | trans = evict_refill_and_join(root, rsv); |
Omar Sandoval | 2791906 | 2018-05-11 13:13:37 -0700 | [diff] [blame] | 5230 | if (!IS_ERR(trans)) { |
| 5231 | trans->block_rsv = rsv; |
| 5232 | btrfs_orphan_del(trans, BTRFS_I(inode)); |
| 5233 | trans->block_rsv = &fs_info->trans_block_rsv; |
| 5234 | btrfs_end_transaction(trans); |
| 5235 | } |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 5236 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5237 | if (!(root == fs_info->tree_root || |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 5238 | root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)) |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 5239 | btrfs_return_ino(root, btrfs_ino(BTRFS_I(inode))); |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 5240 | |
Omar Sandoval | 2791906 | 2018-05-11 13:13:37 -0700 | [diff] [blame] | 5241 | free_rsv: |
| 5242 | btrfs_free_block_rsv(fs_info, rsv); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5243 | no_delete: |
Omar Sandoval | 2791906 | 2018-05-11 13:13:37 -0700 | [diff] [blame] | 5244 | /* |
| 5245 | * If we didn't successfully delete, the orphan item will still be in |
| 5246 | * the tree and we'll retry on the next mount. Again, we might also want |
| 5247 | * to retry these periodically in the future. |
| 5248 | */ |
Nikolay Borisov | f48d1cf | 2017-01-10 20:35:39 +0200 | [diff] [blame] | 5249 | btrfs_remove_delayed_node(BTRFS_I(inode)); |
Jan Kara | dbd5768 | 2012-05-03 14:48:02 +0200 | [diff] [blame] | 5250 | clear_inode(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5251 | } |
| 5252 | |
| 5253 | /* |
Qu Wenruo | 6bf9e4b | 2019-03-13 13:55:11 +0800 | [diff] [blame] | 5254 | * Return the key found in the dir entry in the location pointer, fill @type |
| 5255 | * with BTRFS_FT_*, and return 0. |
| 5256 | * |
Su Yue | 005d671 | 2018-03-05 17:13:37 +0800 | [diff] [blame] | 5257 | * If no dir entries were found, returns -ENOENT. |
| 5258 | * If found a corrupted location in dir entry, returns -EUCLEAN. |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5259 | */ |
| 5260 | static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry, |
Qu Wenruo | 6bf9e4b | 2019-03-13 13:55:11 +0800 | [diff] [blame] | 5261 | struct btrfs_key *location, u8 *type) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5262 | { |
| 5263 | const char *name = dentry->d_name.name; |
| 5264 | int namelen = dentry->d_name.len; |
| 5265 | struct btrfs_dir_item *di; |
| 5266 | struct btrfs_path *path; |
| 5267 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Yan | 0d9f7f3 | 2007-10-25 15:48:28 -0400 | [diff] [blame] | 5268 | int ret = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5269 | |
| 5270 | path = btrfs_alloc_path(); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 5271 | if (!path) |
| 5272 | return -ENOMEM; |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 5273 | |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 5274 | di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(BTRFS_I(dir)), |
| 5275 | name, namelen, 0); |
Liu Bo | 3cf5068 | 2018-09-12 06:06:26 +0800 | [diff] [blame] | 5276 | if (IS_ERR_OR_NULL(di)) { |
| 5277 | ret = di ? PTR_ERR(di) : -ENOENT; |
Su Yue | 005d671 | 2018-03-05 17:13:37 +0800 | [diff] [blame] | 5278 | goto out; |
| 5279 | } |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5280 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5281 | btrfs_dir_item_key_to_cpu(path->nodes[0], di, location); |
Liu Bo | 56a0e70 | 2017-10-30 11:14:38 -0600 | [diff] [blame] | 5282 | if (location->type != BTRFS_INODE_ITEM_KEY && |
| 5283 | location->type != BTRFS_ROOT_ITEM_KEY) { |
Su Yue | 005d671 | 2018-03-05 17:13:37 +0800 | [diff] [blame] | 5284 | ret = -EUCLEAN; |
Liu Bo | 56a0e70 | 2017-10-30 11:14:38 -0600 | [diff] [blame] | 5285 | btrfs_warn(root->fs_info, |
| 5286 | "%s gets something invalid in DIR_ITEM (name %s, directory ino %llu, location(%llu %u %llu))", |
| 5287 | __func__, name, btrfs_ino(BTRFS_I(dir)), |
| 5288 | location->objectid, location->type, location->offset); |
Liu Bo | 56a0e70 | 2017-10-30 11:14:38 -0600 | [diff] [blame] | 5289 | } |
Qu Wenruo | 6bf9e4b | 2019-03-13 13:55:11 +0800 | [diff] [blame] | 5290 | if (!ret) |
| 5291 | *type = btrfs_dir_type(path->nodes[0], di); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5292 | out: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5293 | btrfs_free_path(path); |
| 5294 | return ret; |
| 5295 | } |
| 5296 | |
| 5297 | /* |
| 5298 | * when we hit a tree root in a directory, the btrfs part of the inode |
| 5299 | * needs to be changed to reflect the root directory of the tree root. This |
| 5300 | * is kind of like crossing a mount point. |
| 5301 | */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5302 | static int fixup_tree_root_location(struct btrfs_fs_info *fs_info, |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5303 | struct inode *dir, |
| 5304 | struct dentry *dentry, |
| 5305 | struct btrfs_key *location, |
| 5306 | struct btrfs_root **sub_root) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5307 | { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5308 | struct btrfs_path *path; |
| 5309 | struct btrfs_root *new_root; |
| 5310 | struct btrfs_root_ref *ref; |
| 5311 | struct extent_buffer *leaf; |
David Sterba | 1d4c08e | 2015-01-02 19:36:14 +0100 | [diff] [blame] | 5312 | struct btrfs_key key; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5313 | int ret; |
| 5314 | int err = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5315 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5316 | path = btrfs_alloc_path(); |
| 5317 | if (!path) { |
| 5318 | err = -ENOMEM; |
| 5319 | goto out; |
| 5320 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5321 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5322 | err = -ENOENT; |
David Sterba | 1d4c08e | 2015-01-02 19:36:14 +0100 | [diff] [blame] | 5323 | key.objectid = BTRFS_I(dir)->root->root_key.objectid; |
| 5324 | key.type = BTRFS_ROOT_REF_KEY; |
| 5325 | key.offset = location->objectid; |
| 5326 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5327 | 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] | 5328 | if (ret) { |
| 5329 | if (ret < 0) |
| 5330 | err = ret; |
| 5331 | goto out; |
| 5332 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5333 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5334 | leaf = path->nodes[0]; |
| 5335 | ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref); |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 5336 | if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(BTRFS_I(dir)) || |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5337 | btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len) |
| 5338 | goto out; |
| 5339 | |
| 5340 | ret = memcmp_extent_buffer(leaf, dentry->d_name.name, |
| 5341 | (unsigned long)(ref + 1), |
| 5342 | dentry->d_name.len); |
| 5343 | if (ret) |
| 5344 | goto out; |
| 5345 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 5346 | btrfs_release_path(path); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5347 | |
David Sterba | 56e9357 | 2020-05-15 19:35:55 +0200 | [diff] [blame] | 5348 | new_root = btrfs_get_fs_root(fs_info, location->objectid, true); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5349 | if (IS_ERR(new_root)) { |
| 5350 | err = PTR_ERR(new_root); |
| 5351 | goto out; |
| 5352 | } |
| 5353 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5354 | *sub_root = new_root; |
| 5355 | location->objectid = btrfs_root_dirid(&new_root->root_item); |
| 5356 | location->type = BTRFS_INODE_ITEM_KEY; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5357 | location->offset = 0; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5358 | err = 0; |
| 5359 | out: |
| 5360 | btrfs_free_path(path); |
| 5361 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5362 | } |
| 5363 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5364 | static void inode_tree_add(struct inode *inode) |
| 5365 | { |
| 5366 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5367 | struct btrfs_inode *entry; |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 5368 | struct rb_node **p; |
| 5369 | struct rb_node *parent; |
Filipe David Borba Manana | cef2193 | 2013-09-02 12:19:13 +0100 | [diff] [blame] | 5370 | struct rb_node *new = &BTRFS_I(inode)->rb_node; |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 5371 | u64 ino = btrfs_ino(BTRFS_I(inode)); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5372 | |
Al Viro | 1d3382cb | 2010-10-23 15:19:20 -0400 | [diff] [blame] | 5373 | if (inode_unhashed(inode)) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5374 | return; |
Miao Xie | e1409ce | 2013-05-15 07:48:16 +0000 | [diff] [blame] | 5375 | parent = NULL; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5376 | spin_lock(&root->inode_lock); |
Miao Xie | e1409ce | 2013-05-15 07:48:16 +0000 | [diff] [blame] | 5377 | p = &root->inode_tree.rb_node; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5378 | while (*p) { |
| 5379 | parent = *p; |
| 5380 | entry = rb_entry(parent, struct btrfs_inode, rb_node); |
| 5381 | |
David Sterba | 3750851 | 2018-06-29 10:56:40 +0200 | [diff] [blame] | 5382 | if (ino < btrfs_ino(entry)) |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 5383 | p = &parent->rb_left; |
David Sterba | 3750851 | 2018-06-29 10:56:40 +0200 | [diff] [blame] | 5384 | else if (ino > btrfs_ino(entry)) |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 5385 | p = &parent->rb_right; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5386 | else { |
| 5387 | WARN_ON(!(entry->vfs_inode.i_state & |
Al Viro | a4ffdde | 2010-06-02 17:38:30 -0400 | [diff] [blame] | 5388 | (I_WILL_FREE | I_FREEING))); |
Filipe David Borba Manana | cef2193 | 2013-09-02 12:19:13 +0100 | [diff] [blame] | 5389 | rb_replace_node(parent, new, &root->inode_tree); |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 5390 | RB_CLEAR_NODE(parent); |
| 5391 | spin_unlock(&root->inode_lock); |
Filipe David Borba Manana | cef2193 | 2013-09-02 12:19:13 +0100 | [diff] [blame] | 5392 | return; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5393 | } |
| 5394 | } |
Filipe David Borba Manana | cef2193 | 2013-09-02 12:19:13 +0100 | [diff] [blame] | 5395 | rb_link_node(new, parent, p); |
| 5396 | rb_insert_color(new, &root->inode_tree); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5397 | spin_unlock(&root->inode_lock); |
| 5398 | } |
| 5399 | |
Nikolay Borisov | b79b724 | 2020-08-31 14:42:38 +0300 | [diff] [blame] | 5400 | static void inode_tree_del(struct btrfs_inode *inode) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5401 | { |
Nikolay Borisov | b79b724 | 2020-08-31 14:42:38 +0300 | [diff] [blame] | 5402 | struct btrfs_root *root = inode->root; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5403 | int empty = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5404 | |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 5405 | spin_lock(&root->inode_lock); |
Nikolay Borisov | b79b724 | 2020-08-31 14:42:38 +0300 | [diff] [blame] | 5406 | if (!RB_EMPTY_NODE(&inode->rb_node)) { |
| 5407 | rb_erase(&inode->rb_node, &root->inode_tree); |
| 5408 | RB_CLEAR_NODE(&inode->rb_node); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5409 | empty = RB_EMPTY_ROOT(&root->inode_tree); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5410 | } |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 5411 | spin_unlock(&root->inode_lock); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5412 | |
Stefan Behrens | 69e9c6c | 2013-09-05 16:58:43 +0200 | [diff] [blame] | 5413 | if (empty && btrfs_root_refs(&root->root_item) == 0) { |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5414 | spin_lock(&root->inode_lock); |
| 5415 | empty = RB_EMPTY_ROOT(&root->inode_tree); |
| 5416 | spin_unlock(&root->inode_lock); |
| 5417 | if (empty) |
| 5418 | btrfs_add_dead_root(root); |
| 5419 | } |
| 5420 | } |
| 5421 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5422 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5423 | static int btrfs_init_locked_inode(struct inode *inode, void *p) |
| 5424 | { |
| 5425 | struct btrfs_iget_args *args = p; |
David Sterba | 0202e83 | 2020-05-15 19:35:59 +0200 | [diff] [blame] | 5426 | |
| 5427 | inode->i_ino = args->ino; |
| 5428 | BTRFS_I(inode)->location.objectid = args->ino; |
| 5429 | BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY; |
| 5430 | BTRFS_I(inode)->location.offset = 0; |
Josef Bacik | 5c8fd99 | 2020-02-14 16:11:43 -0500 | [diff] [blame] | 5431 | BTRFS_I(inode)->root = btrfs_grab_root(args->root); |
| 5432 | BUG_ON(args->root && !BTRFS_I(inode)->root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5433 | return 0; |
| 5434 | } |
| 5435 | |
| 5436 | static int btrfs_find_actor(struct inode *inode, void *opaque) |
| 5437 | { |
| 5438 | struct btrfs_iget_args *args = opaque; |
David Sterba | 0202e83 | 2020-05-15 19:35:59 +0200 | [diff] [blame] | 5439 | |
| 5440 | return args->ino == BTRFS_I(inode)->location.objectid && |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5441 | args->root == BTRFS_I(inode)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5442 | } |
| 5443 | |
David Sterba | 0202e83 | 2020-05-15 19:35:59 +0200 | [diff] [blame] | 5444 | static struct inode *btrfs_iget_locked(struct super_block *s, u64 ino, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5445 | struct btrfs_root *root) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5446 | { |
| 5447 | struct inode *inode; |
| 5448 | struct btrfs_iget_args args; |
David Sterba | 0202e83 | 2020-05-15 19:35:59 +0200 | [diff] [blame] | 5449 | unsigned long hashval = btrfs_inode_hash(ino, root); |
Filipe David Borba Manana | 778ba82 | 2013-10-06 22:22:33 +0100 | [diff] [blame] | 5450 | |
David Sterba | 0202e83 | 2020-05-15 19:35:59 +0200 | [diff] [blame] | 5451 | args.ino = ino; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5452 | args.root = root; |
| 5453 | |
Filipe David Borba Manana | 778ba82 | 2013-10-06 22:22:33 +0100 | [diff] [blame] | 5454 | inode = iget5_locked(s, hashval, btrfs_find_actor, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5455 | btrfs_init_locked_inode, |
| 5456 | (void *)&args); |
| 5457 | return inode; |
| 5458 | } |
| 5459 | |
David Sterba | 4c66e0d | 2019-10-03 19:09:35 +0200 | [diff] [blame] | 5460 | /* |
David Sterba | 0202e83 | 2020-05-15 19:35:59 +0200 | [diff] [blame] | 5461 | * Get an inode object given its inode number and corresponding root. |
David Sterba | 4c66e0d | 2019-10-03 19:09:35 +0200 | [diff] [blame] | 5462 | * Path can be preallocated to prevent recursing back to iget through |
| 5463 | * allocator. NULL is also valid but may require an additional allocation |
| 5464 | * later. |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 5465 | */ |
David Sterba | 0202e83 | 2020-05-15 19:35:59 +0200 | [diff] [blame] | 5466 | struct inode *btrfs_iget_path(struct super_block *s, u64 ino, |
David Sterba | 4c66e0d | 2019-10-03 19:09:35 +0200 | [diff] [blame] | 5467 | struct btrfs_root *root, struct btrfs_path *path) |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 5468 | { |
| 5469 | struct inode *inode; |
| 5470 | |
David Sterba | 0202e83 | 2020-05-15 19:35:59 +0200 | [diff] [blame] | 5471 | inode = btrfs_iget_locked(s, ino, root); |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 5472 | if (!inode) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5473 | return ERR_PTR(-ENOMEM); |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 5474 | |
| 5475 | if (inode->i_state & I_NEW) { |
Filipe Manana | 6771089 | 2016-06-06 11:51:25 +0100 | [diff] [blame] | 5476 | int ret; |
| 5477 | |
Filipe Manana | 4222ea7 | 2018-10-24 10:13:03 +0100 | [diff] [blame] | 5478 | ret = btrfs_read_locked_inode(inode, path); |
Al Viro | 9bc2cef | 2018-07-29 23:04:50 +0100 | [diff] [blame] | 5479 | if (!ret) { |
Mark Fasheh | 1748f84 | 2011-07-12 11:25:31 -0700 | [diff] [blame] | 5480 | inode_tree_add(inode); |
| 5481 | unlock_new_inode(inode); |
Mark Fasheh | 1748f84 | 2011-07-12 11:25:31 -0700 | [diff] [blame] | 5482 | } else { |
Al Viro | f5b3a41 | 2018-07-29 23:04:51 +0100 | [diff] [blame] | 5483 | iget_failed(inode); |
| 5484 | /* |
| 5485 | * ret > 0 can come from btrfs_search_slot called by |
| 5486 | * btrfs_read_locked_inode, this means the inode item |
| 5487 | * was not found. |
| 5488 | */ |
| 5489 | if (ret > 0) |
| 5490 | ret = -ENOENT; |
| 5491 | inode = ERR_PTR(ret); |
Mark Fasheh | 1748f84 | 2011-07-12 11:25:31 -0700 | [diff] [blame] | 5492 | } |
| 5493 | } |
| 5494 | |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 5495 | return inode; |
| 5496 | } |
| 5497 | |
David Sterba | 0202e83 | 2020-05-15 19:35:59 +0200 | [diff] [blame] | 5498 | 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] | 5499 | { |
David Sterba | 0202e83 | 2020-05-15 19:35:59 +0200 | [diff] [blame] | 5500 | return btrfs_iget_path(s, ino, root, NULL); |
Filipe Manana | 4222ea7 | 2018-10-24 10:13:03 +0100 | [diff] [blame] | 5501 | } |
| 5502 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5503 | static struct inode *new_simple_dir(struct super_block *s, |
| 5504 | struct btrfs_key *key, |
| 5505 | struct btrfs_root *root) |
| 5506 | { |
| 5507 | struct inode *inode = new_inode(s); |
| 5508 | |
| 5509 | if (!inode) |
| 5510 | return ERR_PTR(-ENOMEM); |
| 5511 | |
Josef Bacik | 5c8fd99 | 2020-02-14 16:11:43 -0500 | [diff] [blame] | 5512 | BTRFS_I(inode)->root = btrfs_grab_root(root); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5513 | memcpy(&BTRFS_I(inode)->location, key, sizeof(*key)); |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 5514 | set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5515 | |
| 5516 | inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID; |
Omar Sandoval | 6bb6b51 | 2019-12-05 10:36:04 -0800 | [diff] [blame] | 5517 | /* |
| 5518 | * We only need lookup, the rest is read-only and there's no inode |
| 5519 | * associated with the dentry |
| 5520 | */ |
| 5521 | inode->i_op = &simple_dir_inode_operations; |
Omar Sandoval | 1fdf419 | 2017-01-25 17:06:39 -0800 | [diff] [blame] | 5522 | inode->i_opflags &= ~IOP_XATTR; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5523 | inode->i_fop = &simple_dir_operations; |
| 5524 | inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO; |
Deepa Dinamani | c2050a4 | 2016-09-14 07:48:06 -0700 | [diff] [blame] | 5525 | inode->i_mtime = current_time(inode); |
chandan r | 9cc97d6 | 2012-07-04 12:48:07 +0530 | [diff] [blame] | 5526 | inode->i_atime = inode->i_mtime; |
| 5527 | inode->i_ctime = inode->i_mtime; |
Arnd Bergmann | d3c6be6f | 2018-06-21 18:04:06 +0200 | [diff] [blame] | 5528 | BTRFS_I(inode)->i_otime = inode->i_mtime; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5529 | |
| 5530 | return inode; |
| 5531 | } |
| 5532 | |
Qu Wenruo | 6bf9e4b | 2019-03-13 13:55:11 +0800 | [diff] [blame] | 5533 | static inline u8 btrfs_inode_type(struct inode *inode) |
| 5534 | { |
| 5535 | /* |
| 5536 | * Compile-time asserts that generic FT_* types still match |
| 5537 | * BTRFS_FT_* types |
| 5538 | */ |
| 5539 | BUILD_BUG_ON(BTRFS_FT_UNKNOWN != FT_UNKNOWN); |
| 5540 | BUILD_BUG_ON(BTRFS_FT_REG_FILE != FT_REG_FILE); |
| 5541 | BUILD_BUG_ON(BTRFS_FT_DIR != FT_DIR); |
| 5542 | BUILD_BUG_ON(BTRFS_FT_CHRDEV != FT_CHRDEV); |
| 5543 | BUILD_BUG_ON(BTRFS_FT_BLKDEV != FT_BLKDEV); |
| 5544 | BUILD_BUG_ON(BTRFS_FT_FIFO != FT_FIFO); |
| 5545 | BUILD_BUG_ON(BTRFS_FT_SOCK != FT_SOCK); |
| 5546 | BUILD_BUG_ON(BTRFS_FT_SYMLINK != FT_SYMLINK); |
| 5547 | |
| 5548 | return fs_umode_to_ftype(inode->i_mode); |
| 5549 | } |
| 5550 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5551 | struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5552 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5553 | struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5554 | struct inode *inode; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5555 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5556 | struct btrfs_root *sub_root = root; |
| 5557 | struct btrfs_key location; |
Qu Wenruo | 6bf9e4b | 2019-03-13 13:55:11 +0800 | [diff] [blame] | 5558 | u8 di_type = 0; |
Josef Bacik | b4aff1f | 2011-06-28 16:18:59 -0400 | [diff] [blame] | 5559 | int ret = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5560 | |
| 5561 | if (dentry->d_name.len > BTRFS_NAME_LEN) |
| 5562 | return ERR_PTR(-ENAMETOOLONG); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5563 | |
Qu Wenruo | 6bf9e4b | 2019-03-13 13:55:11 +0800 | [diff] [blame] | 5564 | ret = btrfs_inode_by_name(dir, dentry, &location, &di_type); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5565 | if (ret < 0) |
| 5566 | return ERR_PTR(ret); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5567 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5568 | if (location.type == BTRFS_INODE_ITEM_KEY) { |
David Sterba | 0202e83 | 2020-05-15 19:35:59 +0200 | [diff] [blame] | 5569 | inode = btrfs_iget(dir->i_sb, location.objectid, root); |
Qu Wenruo | 6bf9e4b | 2019-03-13 13:55:11 +0800 | [diff] [blame] | 5570 | if (IS_ERR(inode)) |
| 5571 | return inode; |
| 5572 | |
| 5573 | /* Do extra check against inode mode with di_type */ |
| 5574 | if (btrfs_inode_type(inode) != di_type) { |
| 5575 | btrfs_crit(fs_info, |
| 5576 | "inode mode mismatch with dir: inode mode=0%o btrfs type=%u dir type=%u", |
| 5577 | inode->i_mode, btrfs_inode_type(inode), |
| 5578 | di_type); |
| 5579 | iput(inode); |
| 5580 | return ERR_PTR(-EUCLEAN); |
| 5581 | } |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5582 | return inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5583 | } |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5584 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5585 | ret = fixup_tree_root_location(fs_info, dir, dentry, |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5586 | &location, &sub_root); |
| 5587 | if (ret < 0) { |
| 5588 | if (ret != -ENOENT) |
| 5589 | inode = ERR_PTR(ret); |
| 5590 | else |
| 5591 | inode = new_simple_dir(dir->i_sb, &location, sub_root); |
| 5592 | } else { |
David Sterba | 0202e83 | 2020-05-15 19:35:59 +0200 | [diff] [blame] | 5593 | inode = btrfs_iget(dir->i_sb, location.objectid, sub_root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5594 | } |
Josef Bacik | 8727002 | 2020-01-24 09:32:31 -0500 | [diff] [blame] | 5595 | if (root != sub_root) |
Josef Bacik | 0024652 | 2020-01-24 09:33:01 -0500 | [diff] [blame] | 5596 | btrfs_put_root(sub_root); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5597 | |
Julia Lawall | 34d19ba | 2011-01-24 19:55:19 +0000 | [diff] [blame] | 5598 | if (!IS_ERR(inode) && root != sub_root) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5599 | down_read(&fs_info->cleanup_work_sem); |
David Howells | bc98a42 | 2017-07-17 08:45:34 +0100 | [diff] [blame] | 5600 | if (!sb_rdonly(inode->i_sb)) |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 5601 | ret = btrfs_orphan_cleanup(sub_root); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5602 | up_read(&fs_info->cleanup_work_sem); |
Josef Bacik | 01cd336 | 2013-06-03 21:39:49 -0400 | [diff] [blame] | 5603 | if (ret) { |
| 5604 | iput(inode); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 5605 | inode = ERR_PTR(ret); |
Josef Bacik | 01cd336 | 2013-06-03 21:39:49 -0400 | [diff] [blame] | 5606 | } |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 5607 | } |
| 5608 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5609 | return inode; |
| 5610 | } |
| 5611 | |
Nick Piggin | fe15ce4 | 2011-01-07 17:49:23 +1100 | [diff] [blame] | 5612 | static int btrfs_dentry_delete(const struct dentry *dentry) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5613 | { |
| 5614 | struct btrfs_root *root; |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 5615 | struct inode *inode = d_inode(dentry); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5616 | |
Li Zefan | 848cce0 | 2012-02-21 17:04:28 +0800 | [diff] [blame] | 5617 | if (!inode && !IS_ROOT(dentry)) |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 5618 | inode = d_inode(dentry->d_parent); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5619 | |
Li Zefan | 848cce0 | 2012-02-21 17:04:28 +0800 | [diff] [blame] | 5620 | if (inode) { |
| 5621 | root = BTRFS_I(inode)->root; |
Yan, Zheng | efefb14 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5622 | if (btrfs_root_refs(&root->root_item) == 0) |
| 5623 | return 1; |
Li Zefan | 848cce0 | 2012-02-21 17:04:28 +0800 | [diff] [blame] | 5624 | |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 5625 | if (btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) |
Li Zefan | 848cce0 | 2012-02-21 17:04:28 +0800 | [diff] [blame] | 5626 | return 1; |
Yan, Zheng | efefb14 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5627 | } |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5628 | return 0; |
| 5629 | } |
| 5630 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5631 | static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry, |
Al Viro | 00cd8dd | 2012-06-10 17:13:09 -0400 | [diff] [blame] | 5632 | unsigned int flags) |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5633 | { |
Al Viro | 3837d20 | 2018-10-10 16:38:27 -0400 | [diff] [blame] | 5634 | struct inode *inode = btrfs_lookup_dentry(dir, dentry); |
Josef Bacik | a66e7cc | 2011-09-18 10:34:03 -0400 | [diff] [blame] | 5635 | |
Al Viro | 3837d20 | 2018-10-10 16:38:27 -0400 | [diff] [blame] | 5636 | if (inode == ERR_PTR(-ENOENT)) |
| 5637 | inode = NULL; |
Al Viro | 41d28bc | 2014-10-12 22:24:21 -0400 | [diff] [blame] | 5638 | return d_splice_alias(inode, dentry); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5639 | } |
| 5640 | |
Josef Bacik | 23b5ec7 | 2017-07-24 15:14:25 -0400 | [diff] [blame] | 5641 | /* |
| 5642 | * All this infrastructure exists because dir_emit can fault, and we are holding |
| 5643 | * the tree lock when doing readdir. For now just allocate a buffer and copy |
| 5644 | * our information into that, and then dir_emit from the buffer. This is |
| 5645 | * similar to what NFS does, only we don't keep the buffer around in pagecache |
| 5646 | * because I'm afraid I'll mess that up. Long term we need to make filldir do |
| 5647 | * copy_to_user_inatomic so we don't have to worry about page faulting under the |
| 5648 | * tree lock. |
| 5649 | */ |
| 5650 | static int btrfs_opendir(struct inode *inode, struct file *file) |
| 5651 | { |
| 5652 | struct btrfs_file_private *private; |
| 5653 | |
| 5654 | private = kzalloc(sizeof(struct btrfs_file_private), GFP_KERNEL); |
| 5655 | if (!private) |
| 5656 | return -ENOMEM; |
| 5657 | private->filldir_buf = kzalloc(PAGE_SIZE, GFP_KERNEL); |
| 5658 | if (!private->filldir_buf) { |
| 5659 | kfree(private); |
| 5660 | return -ENOMEM; |
| 5661 | } |
| 5662 | file->private_data = private; |
| 5663 | return 0; |
| 5664 | } |
| 5665 | |
| 5666 | struct dir_entry { |
| 5667 | u64 ino; |
| 5668 | u64 offset; |
| 5669 | unsigned type; |
| 5670 | int name_len; |
| 5671 | }; |
| 5672 | |
| 5673 | static int btrfs_filldir(void *addr, int entries, struct dir_context *ctx) |
| 5674 | { |
| 5675 | while (entries--) { |
| 5676 | struct dir_entry *entry = addr; |
| 5677 | char *name = (char *)(entry + 1); |
| 5678 | |
David Sterba | 92d3217 | 2018-04-16 21:10:14 +0200 | [diff] [blame] | 5679 | ctx->pos = get_unaligned(&entry->offset); |
| 5680 | if (!dir_emit(ctx, name, get_unaligned(&entry->name_len), |
| 5681 | get_unaligned(&entry->ino), |
| 5682 | get_unaligned(&entry->type))) |
Josef Bacik | 23b5ec7 | 2017-07-24 15:14:25 -0400 | [diff] [blame] | 5683 | return 1; |
David Sterba | 92d3217 | 2018-04-16 21:10:14 +0200 | [diff] [blame] | 5684 | addr += sizeof(struct dir_entry) + |
| 5685 | get_unaligned(&entry->name_len); |
Josef Bacik | 23b5ec7 | 2017-07-24 15:14:25 -0400 | [diff] [blame] | 5686 | ctx->pos++; |
| 5687 | } |
| 5688 | return 0; |
| 5689 | } |
| 5690 | |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5691 | static int btrfs_real_readdir(struct file *file, struct dir_context *ctx) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5692 | { |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5693 | struct inode *inode = file_inode(file); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5694 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Josef Bacik | 23b5ec7 | 2017-07-24 15:14:25 -0400 | [diff] [blame] | 5695 | struct btrfs_file_private *private = file->private_data; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5696 | struct btrfs_dir_item *di; |
| 5697 | struct btrfs_key key; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5698 | struct btrfs_key found_key; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5699 | struct btrfs_path *path; |
Josef Bacik | 23b5ec7 | 2017-07-24 15:14:25 -0400 | [diff] [blame] | 5700 | void *addr; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5701 | struct list_head ins_list; |
| 5702 | struct list_head del_list; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5703 | int ret; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5704 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5705 | int slot; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5706 | char *name_ptr; |
| 5707 | int name_len; |
Josef Bacik | 23b5ec7 | 2017-07-24 15:14:25 -0400 | [diff] [blame] | 5708 | int entries = 0; |
| 5709 | int total_len = 0; |
Omar Sandoval | 02dbfc9 | 2016-05-20 13:50:33 -0700 | [diff] [blame] | 5710 | bool put = false; |
Jeff Mahoney | c2951f3 | 2016-11-21 15:59:04 +0100 | [diff] [blame] | 5711 | struct btrfs_key location; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5712 | |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5713 | if (!dir_emit_dots(file, ctx)) |
| 5714 | return 0; |
| 5715 | |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5716 | path = btrfs_alloc_path(); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5717 | if (!path) |
| 5718 | return -ENOMEM; |
Chris Mason | ff5714c | 2011-05-28 07:00:39 -0400 | [diff] [blame] | 5719 | |
Josef Bacik | 23b5ec7 | 2017-07-24 15:14:25 -0400 | [diff] [blame] | 5720 | addr = private->filldir_buf; |
David Sterba | e4058b5 | 2015-11-27 16:31:35 +0100 | [diff] [blame] | 5721 | path->reada = READA_FORWARD; |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5722 | |
Jeff Mahoney | c2951f3 | 2016-11-21 15:59:04 +0100 | [diff] [blame] | 5723 | INIT_LIST_HEAD(&ins_list); |
| 5724 | INIT_LIST_HEAD(&del_list); |
| 5725 | put = btrfs_readdir_get_delayed_items(inode, &ins_list, &del_list); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5726 | |
Josef Bacik | 23b5ec7 | 2017-07-24 15:14:25 -0400 | [diff] [blame] | 5727 | again: |
Jeff Mahoney | c2951f3 | 2016-11-21 15:59:04 +0100 | [diff] [blame] | 5728 | key.type = BTRFS_DIR_INDEX_KEY; |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5729 | key.offset = ctx->pos; |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 5730 | key.objectid = btrfs_ino(BTRFS_I(inode)); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5731 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5732 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 5733 | if (ret < 0) |
| 5734 | goto err; |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5735 | |
| 5736 | while (1) { |
Josef Bacik | 23b5ec7 | 2017-07-24 15:14:25 -0400 | [diff] [blame] | 5737 | struct dir_entry *entry; |
| 5738 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5739 | leaf = path->nodes[0]; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5740 | slot = path->slots[0]; |
Li Zefan | b9e03af | 2011-03-23 10:43:58 +0800 | [diff] [blame] | 5741 | if (slot >= btrfs_header_nritems(leaf)) { |
| 5742 | ret = btrfs_next_leaf(root, path); |
| 5743 | if (ret < 0) |
| 5744 | goto err; |
| 5745 | else if (ret > 0) |
| 5746 | break; |
| 5747 | continue; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5748 | } |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5749 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5750 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
| 5751 | |
| 5752 | if (found_key.objectid != key.objectid) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5753 | break; |
Jeff Mahoney | c2951f3 | 2016-11-21 15:59:04 +0100 | [diff] [blame] | 5754 | if (found_key.type != BTRFS_DIR_INDEX_KEY) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5755 | break; |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5756 | if (found_key.offset < ctx->pos) |
Li Zefan | b9e03af | 2011-03-23 10:43:58 +0800 | [diff] [blame] | 5757 | goto next; |
Jeff Mahoney | c2951f3 | 2016-11-21 15:59:04 +0100 | [diff] [blame] | 5758 | if (btrfs_should_delete_dir_index(&del_list, found_key.offset)) |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5759 | goto next; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5760 | di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item); |
Jeff Mahoney | c2951f3 | 2016-11-21 15:59:04 +0100 | [diff] [blame] | 5761 | name_len = btrfs_dir_name_len(leaf, di); |
Josef Bacik | 23b5ec7 | 2017-07-24 15:14:25 -0400 | [diff] [blame] | 5762 | if ((total_len + sizeof(struct dir_entry) + name_len) >= |
| 5763 | PAGE_SIZE) { |
| 5764 | btrfs_release_path(path); |
| 5765 | ret = btrfs_filldir(private->filldir_buf, entries, ctx); |
| 5766 | if (ret) |
| 5767 | goto nopos; |
| 5768 | addr = private->filldir_buf; |
| 5769 | entries = 0; |
| 5770 | total_len = 0; |
| 5771 | goto again; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5772 | } |
Josef Bacik | 23b5ec7 | 2017-07-24 15:14:25 -0400 | [diff] [blame] | 5773 | |
| 5774 | entry = addr; |
David Sterba | 92d3217 | 2018-04-16 21:10:14 +0200 | [diff] [blame] | 5775 | put_unaligned(name_len, &entry->name_len); |
Josef Bacik | 23b5ec7 | 2017-07-24 15:14:25 -0400 | [diff] [blame] | 5776 | name_ptr = (char *)(entry + 1); |
Jeff Mahoney | c2951f3 | 2016-11-21 15:59:04 +0100 | [diff] [blame] | 5777 | read_extent_buffer(leaf, name_ptr, (unsigned long)(di + 1), |
| 5778 | name_len); |
Phillip Potter | 7d157c3 | 2019-03-26 21:39:34 +0000 | [diff] [blame] | 5779 | put_unaligned(fs_ftype_to_dtype(btrfs_dir_type(leaf, di)), |
David Sterba | 92d3217 | 2018-04-16 21:10:14 +0200 | [diff] [blame] | 5780 | &entry->type); |
Jeff Mahoney | c2951f3 | 2016-11-21 15:59:04 +0100 | [diff] [blame] | 5781 | btrfs_dir_item_key_to_cpu(leaf, di, &location); |
David Sterba | 92d3217 | 2018-04-16 21:10:14 +0200 | [diff] [blame] | 5782 | put_unaligned(location.objectid, &entry->ino); |
| 5783 | put_unaligned(found_key.offset, &entry->offset); |
Josef Bacik | 23b5ec7 | 2017-07-24 15:14:25 -0400 | [diff] [blame] | 5784 | entries++; |
| 5785 | addr += sizeof(struct dir_entry) + name_len; |
| 5786 | total_len += sizeof(struct dir_entry) + name_len; |
Li Zefan | b9e03af | 2011-03-23 10:43:58 +0800 | [diff] [blame] | 5787 | next: |
| 5788 | path->slots[0]++; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5789 | } |
Josef Bacik | 23b5ec7 | 2017-07-24 15:14:25 -0400 | [diff] [blame] | 5790 | btrfs_release_path(path); |
| 5791 | |
| 5792 | ret = btrfs_filldir(private->filldir_buf, entries, ctx); |
| 5793 | if (ret) |
| 5794 | goto nopos; |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5795 | |
Jeff Mahoney | d2fbb2b | 2016-11-05 13:26:35 -0400 | [diff] [blame] | 5796 | ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list); |
Jeff Mahoney | c2951f3 | 2016-11-21 15:59:04 +0100 | [diff] [blame] | 5797 | if (ret) |
David Sterba | bc4ef75 | 2015-11-13 13:44:28 +0100 | [diff] [blame] | 5798 | goto nopos; |
| 5799 | |
Zach Brown | db62efb | 2013-07-11 16:19:42 -0700 | [diff] [blame] | 5800 | /* |
| 5801 | * Stop new entries from being returned after we return the last |
| 5802 | * entry. |
| 5803 | * |
| 5804 | * New directory entries are assigned a strictly increasing |
| 5805 | * offset. This means that new entries created during readdir |
| 5806 | * are *guaranteed* to be seen in the future by that readdir. |
| 5807 | * This has broken buggy programs which operate on names as |
| 5808 | * they're returned by readdir. Until we re-use freed offsets |
| 5809 | * we have this hack to stop new entries from being returned |
| 5810 | * under the assumption that they'll never reach this huge |
| 5811 | * offset. |
| 5812 | * |
| 5813 | * This is being careful not to overflow 32bit loff_t unless the |
| 5814 | * last entry requires it because doing so has broken 32bit apps |
| 5815 | * in the past. |
| 5816 | */ |
Jeff Mahoney | c2951f3 | 2016-11-21 15:59:04 +0100 | [diff] [blame] | 5817 | if (ctx->pos >= INT_MAX) |
| 5818 | ctx->pos = LLONG_MAX; |
| 5819 | else |
| 5820 | ctx->pos = INT_MAX; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5821 | nopos: |
| 5822 | ret = 0; |
| 5823 | err: |
Omar Sandoval | 02dbfc9 | 2016-05-20 13:50:33 -0700 | [diff] [blame] | 5824 | if (put) |
| 5825 | btrfs_readdir_put_delayed_items(inode, &ins_list, &del_list); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5826 | btrfs_free_path(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5827 | return ret; |
| 5828 | } |
| 5829 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5830 | /* |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 5831 | * This is somewhat expensive, updating the tree every time the |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5832 | * inode changes. But, it is most likely to find the inode in cache. |
| 5833 | * FIXME, needs more benchmarking...there are no reasons other than performance |
| 5834 | * to keep or drop this code. |
| 5835 | */ |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 5836 | static int btrfs_dirty_inode(struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5837 | { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5838 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5839 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5840 | struct btrfs_trans_handle *trans; |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5841 | int ret; |
| 5842 | |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 5843 | if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags)) |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 5844 | return 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5845 | |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 5846 | trans = btrfs_join_transaction(root); |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 5847 | if (IS_ERR(trans)) |
| 5848 | return PTR_ERR(trans); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5849 | |
| 5850 | ret = btrfs_update_inode(trans, root, inode); |
Chris Mason | 94b6044 | 2010-05-26 11:02:00 -0400 | [diff] [blame] | 5851 | if (ret && ret == -ENOSPC) { |
| 5852 | /* whoops, lets try again with the full transaction */ |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 5853 | btrfs_end_transaction(trans); |
Chris Mason | 94b6044 | 2010-05-26 11:02:00 -0400 | [diff] [blame] | 5854 | trans = btrfs_start_transaction(root, 1); |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 5855 | if (IS_ERR(trans)) |
| 5856 | return PTR_ERR(trans); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5857 | |
Chris Mason | 94b6044 | 2010-05-26 11:02:00 -0400 | [diff] [blame] | 5858 | ret = btrfs_update_inode(trans, root, inode); |
Chris Mason | 94b6044 | 2010-05-26 11:02:00 -0400 | [diff] [blame] | 5859 | } |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 5860 | btrfs_end_transaction(trans); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5861 | if (BTRFS_I(inode)->delayed_node) |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5862 | btrfs_balance_delayed_items(fs_info); |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 5863 | |
| 5864 | return ret; |
| 5865 | } |
| 5866 | |
| 5867 | /* |
| 5868 | * This is a copy of file_update_time. We need this so we can return error on |
| 5869 | * ENOSPC for updating the inode in the case of file write and mmap writes. |
| 5870 | */ |
Deepa Dinamani | 95582b0 | 2018-05-08 19:36:02 -0700 | [diff] [blame] | 5871 | static int btrfs_update_time(struct inode *inode, struct timespec64 *now, |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 5872 | int flags) |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 5873 | { |
Alexander Block | 2bc556528 | 2012-06-15 09:49:33 +0200 | [diff] [blame] | 5874 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Jeff Layton | 3a8c723 | 2017-12-11 06:35:24 -0500 | [diff] [blame] | 5875 | bool dirty = flags & ~S_VERSION; |
Alexander Block | 2bc556528 | 2012-06-15 09:49:33 +0200 | [diff] [blame] | 5876 | |
| 5877 | if (btrfs_root_readonly(root)) |
| 5878 | return -EROFS; |
| 5879 | |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 5880 | if (flags & S_VERSION) |
Jeff Layton | 3a8c723 | 2017-12-11 06:35:24 -0500 | [diff] [blame] | 5881 | dirty |= inode_maybe_inc_iversion(inode, dirty); |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 5882 | if (flags & S_CTIME) |
| 5883 | inode->i_ctime = *now; |
| 5884 | if (flags & S_MTIME) |
| 5885 | inode->i_mtime = *now; |
| 5886 | if (flags & S_ATIME) |
| 5887 | inode->i_atime = *now; |
Jeff Layton | 3a8c723 | 2017-12-11 06:35:24 -0500 | [diff] [blame] | 5888 | return dirty ? btrfs_dirty_inode(inode) : 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5889 | } |
| 5890 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 5891 | /* |
| 5892 | * find the highest existing sequence number in a directory |
| 5893 | * and then set the in-memory index_cnt variable to reflect |
| 5894 | * free sequence numbers |
| 5895 | */ |
Nikolay Borisov | 4c570655 | 2017-02-20 13:50:32 +0200 | [diff] [blame] | 5896 | static int btrfs_set_inode_index_count(struct btrfs_inode *inode) |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5897 | { |
Nikolay Borisov | 4c570655 | 2017-02-20 13:50:32 +0200 | [diff] [blame] | 5898 | struct btrfs_root *root = inode->root; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5899 | struct btrfs_key key, found_key; |
| 5900 | struct btrfs_path *path; |
| 5901 | struct extent_buffer *leaf; |
| 5902 | int ret; |
| 5903 | |
Nikolay Borisov | 4c570655 | 2017-02-20 13:50:32 +0200 | [diff] [blame] | 5904 | key.objectid = btrfs_ino(inode); |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 5905 | key.type = BTRFS_DIR_INDEX_KEY; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5906 | key.offset = (u64)-1; |
| 5907 | |
| 5908 | path = btrfs_alloc_path(); |
| 5909 | if (!path) |
| 5910 | return -ENOMEM; |
| 5911 | |
| 5912 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 5913 | if (ret < 0) |
| 5914 | goto out; |
| 5915 | /* FIXME: we should be able to handle this */ |
| 5916 | if (ret == 0) |
| 5917 | goto out; |
| 5918 | ret = 0; |
| 5919 | |
| 5920 | /* |
| 5921 | * MAGIC NUMBER EXPLANATION: |
| 5922 | * since we search a directory based on f_pos we have to start at 2 |
| 5923 | * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody |
| 5924 | * else has to start at 2 |
| 5925 | */ |
| 5926 | if (path->slots[0] == 0) { |
Nikolay Borisov | 4c570655 | 2017-02-20 13:50:32 +0200 | [diff] [blame] | 5927 | inode->index_cnt = 2; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5928 | goto out; |
| 5929 | } |
| 5930 | |
| 5931 | path->slots[0]--; |
| 5932 | |
| 5933 | leaf = path->nodes[0]; |
| 5934 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 5935 | |
Nikolay Borisov | 4c570655 | 2017-02-20 13:50:32 +0200 | [diff] [blame] | 5936 | if (found_key.objectid != btrfs_ino(inode) || |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 5937 | found_key.type != BTRFS_DIR_INDEX_KEY) { |
Nikolay Borisov | 4c570655 | 2017-02-20 13:50:32 +0200 | [diff] [blame] | 5938 | inode->index_cnt = 2; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5939 | goto out; |
| 5940 | } |
| 5941 | |
Nikolay Borisov | 4c570655 | 2017-02-20 13:50:32 +0200 | [diff] [blame] | 5942 | inode->index_cnt = found_key.offset + 1; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5943 | out: |
| 5944 | btrfs_free_path(path); |
| 5945 | return ret; |
| 5946 | } |
| 5947 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 5948 | /* |
| 5949 | * helper to find a free sequence number in a given directory. This current |
| 5950 | * code is very simple, later versions will do smarter things in the btree |
| 5951 | */ |
Nikolay Borisov | 877574e | 2017-02-20 13:50:33 +0200 | [diff] [blame] | 5952 | int btrfs_set_inode_index(struct btrfs_inode *dir, u64 *index) |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5953 | { |
| 5954 | int ret = 0; |
| 5955 | |
Nikolay Borisov | 877574e | 2017-02-20 13:50:33 +0200 | [diff] [blame] | 5956 | if (dir->index_cnt == (u64)-1) { |
| 5957 | ret = btrfs_inode_delayed_dir_index_count(dir); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5958 | if (ret) { |
| 5959 | ret = btrfs_set_inode_index_count(dir); |
| 5960 | if (ret) |
| 5961 | return ret; |
| 5962 | } |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5963 | } |
| 5964 | |
Nikolay Borisov | 877574e | 2017-02-20 13:50:33 +0200 | [diff] [blame] | 5965 | *index = dir->index_cnt; |
| 5966 | dir->index_cnt++; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5967 | |
| 5968 | return ret; |
| 5969 | } |
| 5970 | |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 5971 | static int btrfs_insert_inode_locked(struct inode *inode) |
| 5972 | { |
| 5973 | struct btrfs_iget_args args; |
David Sterba | 0202e83 | 2020-05-15 19:35:59 +0200 | [diff] [blame] | 5974 | |
| 5975 | args.ino = BTRFS_I(inode)->location.objectid; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 5976 | args.root = BTRFS_I(inode)->root; |
| 5977 | |
| 5978 | return insert_inode_locked4(inode, |
| 5979 | btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root), |
| 5980 | btrfs_find_actor, &args); |
| 5981 | } |
| 5982 | |
Anand Jain | 19aee8d | 2017-07-18 17:37:05 +0800 | [diff] [blame] | 5983 | /* |
| 5984 | * Inherit flags from the parent inode. |
| 5985 | * |
| 5986 | * Currently only the compression flags and the cow flags are inherited. |
| 5987 | */ |
| 5988 | static void btrfs_inherit_iflags(struct inode *inode, struct inode *dir) |
| 5989 | { |
| 5990 | unsigned int flags; |
| 5991 | |
| 5992 | if (!dir) |
| 5993 | return; |
| 5994 | |
| 5995 | flags = BTRFS_I(dir)->flags; |
| 5996 | |
| 5997 | if (flags & BTRFS_INODE_NOCOMPRESS) { |
| 5998 | BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS; |
| 5999 | BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS; |
| 6000 | } else if (flags & BTRFS_INODE_COMPRESS) { |
| 6001 | BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS; |
| 6002 | BTRFS_I(inode)->flags |= BTRFS_INODE_COMPRESS; |
| 6003 | } |
| 6004 | |
| 6005 | if (flags & BTRFS_INODE_NODATACOW) { |
| 6006 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW; |
| 6007 | if (S_ISREG(inode->i_mode)) |
| 6008 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM; |
| 6009 | } |
| 6010 | |
David Sterba | 7b6a221 | 2018-03-26 18:40:21 +0200 | [diff] [blame] | 6011 | btrfs_sync_inode_flags_to_i_flags(inode); |
Anand Jain | 19aee8d | 2017-07-18 17:37:05 +0800 | [diff] [blame] | 6012 | } |
| 6013 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6014 | static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans, |
| 6015 | struct btrfs_root *root, |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6016 | struct inode *dir, |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 6017 | const char *name, int name_len, |
Al Viro | 175a4eb | 2011-07-26 03:30:54 -0400 | [diff] [blame] | 6018 | u64 ref_objectid, u64 objectid, |
| 6019 | umode_t mode, u64 *index) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6020 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6021 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6022 | struct inode *inode; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6023 | struct btrfs_inode_item *inode_item; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6024 | struct btrfs_key *location; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6025 | struct btrfs_path *path; |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 6026 | struct btrfs_inode_ref *ref; |
| 6027 | struct btrfs_key key[2]; |
| 6028 | u32 sizes[2]; |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 6029 | int nitems = name ? 2 : 1; |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 6030 | unsigned long ptr; |
Josef Bacik | 11a19a9 | 2019-09-09 10:12:04 -0400 | [diff] [blame] | 6031 | unsigned int nofs_flag; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6032 | int ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6033 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6034 | path = btrfs_alloc_path(); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 6035 | if (!path) |
| 6036 | return ERR_PTR(-ENOMEM); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6037 | |
Josef Bacik | 11a19a9 | 2019-09-09 10:12:04 -0400 | [diff] [blame] | 6038 | nofs_flag = memalloc_nofs_save(); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6039 | inode = new_inode(fs_info->sb); |
Josef Bacik | 11a19a9 | 2019-09-09 10:12:04 -0400 | [diff] [blame] | 6040 | memalloc_nofs_restore(nofs_flag); |
Yoshinori Sano | 8fb2764 | 2011-04-09 02:30:07 +0000 | [diff] [blame] | 6041 | if (!inode) { |
| 6042 | btrfs_free_path(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6043 | return ERR_PTR(-ENOMEM); |
Yoshinori Sano | 8fb2764 | 2011-04-09 02:30:07 +0000 | [diff] [blame] | 6044 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6045 | |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 6046 | /* |
Filipe Manana | 5762b5c | 2014-08-01 00:10:32 +0100 | [diff] [blame] | 6047 | * O_TMPFILE, set link count to 0, so that after this point, |
| 6048 | * we fill in an inode item with the correct link count. |
| 6049 | */ |
| 6050 | if (!name) |
| 6051 | set_nlink(inode, 0); |
| 6052 | |
| 6053 | /* |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 6054 | * we have to initialize this early, so we can reclaim the inode |
| 6055 | * number if we fail afterwards in this function. |
| 6056 | */ |
| 6057 | inode->i_ino = objectid; |
| 6058 | |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 6059 | if (dir && name) { |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 6060 | trace_btrfs_inode_request(dir); |
| 6061 | |
Nikolay Borisov | 877574e | 2017-02-20 13:50:33 +0200 | [diff] [blame] | 6062 | ret = btrfs_set_inode_index(BTRFS_I(dir), index); |
Shen Feng | 0977143 | 2009-04-02 16:46:06 -0400 | [diff] [blame] | 6063 | if (ret) { |
Yoshinori Sano | 8fb2764 | 2011-04-09 02:30:07 +0000 | [diff] [blame] | 6064 | btrfs_free_path(path); |
Shen Feng | 0977143 | 2009-04-02 16:46:06 -0400 | [diff] [blame] | 6065 | iput(inode); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6066 | return ERR_PTR(ret); |
Shen Feng | 0977143 | 2009-04-02 16:46:06 -0400 | [diff] [blame] | 6067 | } |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 6068 | } else if (dir) { |
| 6069 | *index = 0; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6070 | } |
| 6071 | /* |
| 6072 | * index_cnt is ignored for everything but a dir, |
Su Yue | df6703e | 2018-01-12 11:08:02 +0800 | [diff] [blame] | 6073 | * btrfs_set_inode_index_count has an explanation for the magic |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6074 | * number |
| 6075 | */ |
| 6076 | BTRFS_I(inode)->index_cnt = 2; |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 6077 | BTRFS_I(inode)->dir_index = *index; |
Josef Bacik | 5c8fd99 | 2020-02-14 16:11:43 -0500 | [diff] [blame] | 6078 | BTRFS_I(inode)->root = btrfs_grab_root(root); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 6079 | BTRFS_I(inode)->generation = trans->transid; |
Josef Bacik | 7619585 | 2010-11-19 02:18:02 +0000 | [diff] [blame] | 6080 | inode->i_generation = BTRFS_I(inode)->generation; |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6081 | |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 6082 | /* |
| 6083 | * We could have gotten an inode number from somebody who was fsynced |
| 6084 | * and then removed in this same transaction, so let's just set full |
| 6085 | * sync since it will be a full sync anyway and this will blow away the |
| 6086 | * old info in the log. |
| 6087 | */ |
| 6088 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags); |
| 6089 | |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 6090 | key[0].objectid = objectid; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 6091 | key[0].type = BTRFS_INODE_ITEM_KEY; |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 6092 | key[0].offset = 0; |
| 6093 | |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 6094 | sizes[0] = sizeof(struct btrfs_inode_item); |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 6095 | |
| 6096 | if (name) { |
| 6097 | /* |
| 6098 | * Start new inodes with an inode_ref. This is slightly more |
| 6099 | * efficient for small numbers of hard links since they will |
| 6100 | * be packed into one item. Extended refs will kick in if we |
| 6101 | * add more hard links than can fit in the ref item. |
| 6102 | */ |
| 6103 | key[1].objectid = objectid; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 6104 | key[1].type = BTRFS_INODE_REF_KEY; |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 6105 | key[1].offset = ref_objectid; |
| 6106 | |
| 6107 | sizes[1] = name_len + sizeof(*ref); |
| 6108 | } |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 6109 | |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6110 | location = &BTRFS_I(inode)->location; |
| 6111 | location->objectid = objectid; |
| 6112 | location->offset = 0; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 6113 | location->type = BTRFS_INODE_ITEM_KEY; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6114 | |
| 6115 | ret = btrfs_insert_inode_locked(inode); |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6116 | if (ret < 0) { |
| 6117 | iput(inode); |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6118 | goto fail; |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6119 | } |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6120 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 6121 | path->leave_spinning = 1; |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 6122 | ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems); |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 6123 | if (ret != 0) |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6124 | goto fail_unlock; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6125 | |
Dmitry Monakhov | ecc11fab | 2010-03-04 17:31:47 +0300 | [diff] [blame] | 6126 | inode_init_owner(inode, dir, mode); |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 6127 | inode_set_bytes(inode, 0); |
chandan r | 9cc97d6 | 2012-07-04 12:48:07 +0530 | [diff] [blame] | 6128 | |
Deepa Dinamani | c2050a4 | 2016-09-14 07:48:06 -0700 | [diff] [blame] | 6129 | inode->i_mtime = current_time(inode); |
chandan r | 9cc97d6 | 2012-07-04 12:48:07 +0530 | [diff] [blame] | 6130 | inode->i_atime = inode->i_mtime; |
| 6131 | inode->i_ctime = inode->i_mtime; |
Arnd Bergmann | d3c6be6f | 2018-06-21 18:04:06 +0200 | [diff] [blame] | 6132 | BTRFS_I(inode)->i_otime = inode->i_mtime; |
chandan r | 9cc97d6 | 2012-07-04 12:48:07 +0530 | [diff] [blame] | 6133 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6134 | inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 6135 | struct btrfs_inode_item); |
David Sterba | b159fa2 | 2016-11-08 18:09:03 +0100 | [diff] [blame] | 6136 | memzero_extent_buffer(path->nodes[0], (unsigned long)inode_item, |
Li Zefan | 293f7e0 | 2012-07-10 00:58:58 -0600 | [diff] [blame] | 6137 | sizeof(*inode_item)); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 6138 | fill_inode_item(trans, path->nodes[0], inode_item, inode); |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 6139 | |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 6140 | if (name) { |
| 6141 | ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1, |
| 6142 | struct btrfs_inode_ref); |
| 6143 | btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len); |
| 6144 | btrfs_set_inode_ref_index(path->nodes[0], ref, *index); |
| 6145 | ptr = (unsigned long)(ref + 1); |
| 6146 | write_extent_buffer(path->nodes[0], name, ptr, name_len); |
| 6147 | } |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 6148 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6149 | btrfs_mark_buffer_dirty(path->nodes[0]); |
| 6150 | btrfs_free_path(path); |
| 6151 | |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 6152 | btrfs_inherit_iflags(inode, dir); |
| 6153 | |
Al Viro | 569254b | 2011-07-24 17:08:40 -0400 | [diff] [blame] | 6154 | if (S_ISREG(mode)) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6155 | if (btrfs_test_opt(fs_info, NODATASUM)) |
Chris Mason | 9427216 | 2009-07-02 12:26:06 -0400 | [diff] [blame] | 6156 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6157 | if (btrfs_test_opt(fs_info, NODATACOW)) |
Josef Bacik | f2bdf9a | 2013-02-21 15:28:28 -0500 | [diff] [blame] | 6158 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW | |
| 6159 | BTRFS_INODE_NODATASUM; |
Chris Mason | 9427216 | 2009-07-02 12:26:06 -0400 | [diff] [blame] | 6160 | } |
| 6161 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6162 | inode_tree_add(inode); |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 6163 | |
| 6164 | trace_btrfs_inode_new(inode); |
Nikolay Borisov | d909441 | 2020-06-05 10:41:13 +0300 | [diff] [blame] | 6165 | btrfs_set_inode_last_trans(trans, BTRFS_I(inode)); |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 6166 | |
Alexander Block | 8ea05e3 | 2012-07-25 17:35:53 +0200 | [diff] [blame] | 6167 | btrfs_update_root_times(trans, root); |
| 6168 | |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 6169 | ret = btrfs_inode_inherit_props(trans, inode, dir); |
| 6170 | if (ret) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6171 | btrfs_err(fs_info, |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 6172 | "error inheriting props for ino %llu (root %llu): %d", |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 6173 | btrfs_ino(BTRFS_I(inode)), root->root_key.objectid, ret); |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 6174 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6175 | return inode; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6176 | |
| 6177 | fail_unlock: |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6178 | discard_new_inode(inode); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6179 | fail: |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 6180 | if (dir && name) |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6181 | BTRFS_I(dir)->index_cnt--; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6182 | btrfs_free_path(path); |
| 6183 | return ERR_PTR(ret); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6184 | } |
| 6185 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 6186 | /* |
| 6187 | * utility function to add 'inode' into 'parent_inode' with |
| 6188 | * a give name and a given sequence number. |
| 6189 | * if 'add_backref' is true, also insert a backref from the |
| 6190 | * inode to the parent directory. |
| 6191 | */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 6192 | int btrfs_add_link(struct btrfs_trans_handle *trans, |
Nikolay Borisov | db0a669 | 2017-02-20 13:51:08 +0200 | [diff] [blame] | 6193 | struct btrfs_inode *parent_inode, struct btrfs_inode *inode, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 6194 | const char *name, int name_len, int add_backref, u64 index) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6195 | { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6196 | int ret = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6197 | struct btrfs_key key; |
Nikolay Borisov | db0a669 | 2017-02-20 13:51:08 +0200 | [diff] [blame] | 6198 | struct btrfs_root *root = parent_inode->root; |
| 6199 | u64 ino = btrfs_ino(inode); |
| 6200 | u64 parent_ino = btrfs_ino(parent_inode); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6201 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6202 | if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) { |
Nikolay Borisov | db0a669 | 2017-02-20 13:51:08 +0200 | [diff] [blame] | 6203 | memcpy(&key, &inode->root->root_key, sizeof(key)); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6204 | } else { |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6205 | key.objectid = ino; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 6206 | key.type = BTRFS_INODE_ITEM_KEY; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6207 | key.offset = 0; |
| 6208 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6209 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6210 | if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) { |
Lu Fengqi | 6025c19 | 2018-08-01 11:32:29 +0800 | [diff] [blame] | 6211 | ret = btrfs_add_root_ref(trans, key.objectid, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6212 | root->root_key.objectid, parent_ino, |
| 6213 | index, name, name_len); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6214 | } else if (add_backref) { |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6215 | ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino, |
| 6216 | parent_ino, index); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6217 | } |
| 6218 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6219 | /* Nothing to clean up yet */ |
| 6220 | if (ret) |
| 6221 | return ret; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6222 | |
Lu Fengqi | 684572d | 2018-08-04 21:10:57 +0800 | [diff] [blame] | 6223 | ret = btrfs_insert_dir_item(trans, name, name_len, parent_inode, &key, |
Nikolay Borisov | db0a669 | 2017-02-20 13:51:08 +0200 | [diff] [blame] | 6224 | btrfs_inode_type(&inode->vfs_inode), index); |
Chris Mason | 9c52057 | 2012-12-17 14:26:57 -0500 | [diff] [blame] | 6225 | if (ret == -EEXIST || ret == -EOVERFLOW) |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6226 | goto fail_dir_item; |
| 6227 | else if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 6228 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6229 | return ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6230 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6231 | |
Nikolay Borisov | db0a669 | 2017-02-20 13:51:08 +0200 | [diff] [blame] | 6232 | btrfs_i_size_write(parent_inode, parent_inode->vfs_inode.i_size + |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6233 | name_len * 2); |
Nikolay Borisov | db0a669 | 2017-02-20 13:51:08 +0200 | [diff] [blame] | 6234 | inode_inc_iversion(&parent_inode->vfs_inode); |
Filipe Manana | 5338e43 | 2019-05-15 16:02:47 +0100 | [diff] [blame] | 6235 | /* |
| 6236 | * If we are replaying a log tree, we do not want to update the mtime |
| 6237 | * and ctime of the parent directory with the current time, since the |
| 6238 | * log replay procedure is responsible for setting them to their correct |
| 6239 | * values (the ones it had when the fsync was done). |
| 6240 | */ |
| 6241 | if (!test_bit(BTRFS_FS_LOG_RECOVERING, &root->fs_info->flags)) { |
| 6242 | struct timespec64 now = current_time(&parent_inode->vfs_inode); |
| 6243 | |
| 6244 | parent_inode->vfs_inode.i_mtime = now; |
| 6245 | parent_inode->vfs_inode.i_ctime = now; |
| 6246 | } |
Nikolay Borisov | db0a669 | 2017-02-20 13:51:08 +0200 | [diff] [blame] | 6247 | ret = btrfs_update_inode(trans, root, &parent_inode->vfs_inode); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6248 | if (ret) |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 6249 | btrfs_abort_transaction(trans, ret); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6250 | return ret; |
Chris Mason | fe66a05 | 2012-02-20 08:40:56 -0500 | [diff] [blame] | 6251 | |
| 6252 | fail_dir_item: |
| 6253 | if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) { |
| 6254 | u64 local_index; |
| 6255 | int err; |
Lu Fengqi | 3ee1c55 | 2018-08-01 11:32:28 +0800 | [diff] [blame] | 6256 | err = btrfs_del_root_ref(trans, key.objectid, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6257 | root->root_key.objectid, parent_ino, |
| 6258 | &local_index, name, name_len); |
Johannes Thumshirn | 1690dd41 | 2018-12-12 15:14:17 +0100 | [diff] [blame] | 6259 | if (err) |
| 6260 | btrfs_abort_transaction(trans, err); |
Chris Mason | fe66a05 | 2012-02-20 08:40:56 -0500 | [diff] [blame] | 6261 | } else if (add_backref) { |
| 6262 | u64 local_index; |
| 6263 | int err; |
| 6264 | |
| 6265 | err = btrfs_del_inode_ref(trans, root, name, name_len, |
| 6266 | ino, parent_ino, &local_index); |
Johannes Thumshirn | 1690dd41 | 2018-12-12 15:14:17 +0100 | [diff] [blame] | 6267 | if (err) |
| 6268 | btrfs_abort_transaction(trans, err); |
Chris Mason | fe66a05 | 2012-02-20 08:40:56 -0500 | [diff] [blame] | 6269 | } |
Johannes Thumshirn | 1690dd41 | 2018-12-12 15:14:17 +0100 | [diff] [blame] | 6270 | |
| 6271 | /* Return the original error code */ |
Chris Mason | fe66a05 | 2012-02-20 08:40:56 -0500 | [diff] [blame] | 6272 | return ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6273 | } |
| 6274 | |
| 6275 | static int btrfs_add_nondir(struct btrfs_trans_handle *trans, |
Nikolay Borisov | cef415a | 2017-02-20 13:51:09 +0200 | [diff] [blame] | 6276 | struct btrfs_inode *dir, struct dentry *dentry, |
| 6277 | struct btrfs_inode *inode, int backref, u64 index) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6278 | { |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 6279 | int err = btrfs_add_link(trans, dir, inode, |
| 6280 | dentry->d_name.name, dentry->d_name.len, |
| 6281 | backref, index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6282 | if (err > 0) |
| 6283 | err = -EEXIST; |
| 6284 | return err; |
| 6285 | } |
| 6286 | |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6287 | static int btrfs_mknod(struct inode *dir, struct dentry *dentry, |
Al Viro | 1a67aaf | 2011-07-26 01:52:52 -0400 | [diff] [blame] | 6288 | umode_t mode, dev_t rdev) |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6289 | { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6290 | struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6291 | struct btrfs_trans_handle *trans; |
| 6292 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 6293 | struct inode *inode = NULL; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6294 | int err; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6295 | u64 objectid; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 6296 | u64 index = 0; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6297 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 6298 | /* |
| 6299 | * 2 for inode item and ref |
| 6300 | * 2 for dir items |
| 6301 | * 1 for xattr if selinux is on |
| 6302 | */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6303 | trans = btrfs_start_transaction(root, 5); |
| 6304 | if (IS_ERR(trans)) |
| 6305 | return PTR_ERR(trans); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 6306 | |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 6307 | err = btrfs_find_free_ino(root, &objectid); |
| 6308 | if (err) |
| 6309 | goto out_unlock; |
| 6310 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6311 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 6312 | dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid, |
| 6313 | mode, &index); |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 6314 | if (IS_ERR(inode)) { |
| 6315 | err = PTR_ERR(inode); |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6316 | inode = NULL; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6317 | goto out_unlock; |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 6318 | } |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6319 | |
Casey Schaufler | ad19db7 | 2011-12-15 10:09:07 -0500 | [diff] [blame] | 6320 | /* |
| 6321 | * If the active LSM wants to access the inode during |
| 6322 | * d_instantiate it needs these. Smack checks to see |
| 6323 | * if the filesystem supports xattrs by looking at the |
| 6324 | * ops vector. |
| 6325 | */ |
Casey Schaufler | ad19db7 | 2011-12-15 10:09:07 -0500 | [diff] [blame] | 6326 | inode->i_op = &btrfs_special_inode_operations; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6327 | init_special_inode(inode, inode->i_mode, rdev); |
| 6328 | |
| 6329 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6330 | if (err) |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6331 | goto out_unlock; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6332 | |
Nikolay Borisov | cef415a | 2017-02-20 13:51:09 +0200 | [diff] [blame] | 6333 | err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode), |
| 6334 | 0, index); |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6335 | if (err) |
| 6336 | goto out_unlock; |
| 6337 | |
| 6338 | btrfs_update_inode(trans, root, inode); |
| 6339 | d_instantiate_new(dentry, inode); |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6340 | |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6341 | out_unlock: |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 6342 | btrfs_end_transaction(trans); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6343 | btrfs_btree_balance_dirty(fs_info); |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6344 | if (err && inode) { |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6345 | inode_dec_link_count(inode); |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6346 | discard_new_inode(inode); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6347 | } |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6348 | return err; |
| 6349 | } |
| 6350 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6351 | static int btrfs_create(struct inode *dir, struct dentry *dentry, |
Al Viro | ebfc3b4 | 2012-06-10 18:05:36 -0400 | [diff] [blame] | 6352 | umode_t mode, bool excl) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6353 | { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6354 | struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6355 | struct btrfs_trans_handle *trans; |
| 6356 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 6357 | struct inode *inode = NULL; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6358 | int err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6359 | u64 objectid; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 6360 | u64 index = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6361 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 6362 | /* |
| 6363 | * 2 for inode item and ref |
| 6364 | * 2 for dir items |
| 6365 | * 1 for xattr if selinux is on |
| 6366 | */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6367 | trans = btrfs_start_transaction(root, 5); |
| 6368 | if (IS_ERR(trans)) |
| 6369 | return PTR_ERR(trans); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 6370 | |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 6371 | err = btrfs_find_free_ino(root, &objectid); |
| 6372 | if (err) |
| 6373 | goto out_unlock; |
| 6374 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6375 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 6376 | dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid, |
| 6377 | mode, &index); |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 6378 | if (IS_ERR(inode)) { |
| 6379 | err = PTR_ERR(inode); |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6380 | inode = NULL; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6381 | goto out_unlock; |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 6382 | } |
Casey Schaufler | ad19db7 | 2011-12-15 10:09:07 -0500 | [diff] [blame] | 6383 | /* |
| 6384 | * If the active LSM wants to access the inode during |
| 6385 | * d_instantiate it needs these. Smack checks to see |
| 6386 | * if the filesystem supports xattrs by looking at the |
| 6387 | * ops vector. |
| 6388 | */ |
| 6389 | inode->i_fop = &btrfs_file_operations; |
| 6390 | inode->i_op = &btrfs_file_inode_operations; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6391 | inode->i_mapping->a_ops = &btrfs_aops; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6392 | |
| 6393 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); |
| 6394 | if (err) |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6395 | goto out_unlock; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6396 | |
| 6397 | err = btrfs_update_inode(trans, root, inode); |
| 6398 | if (err) |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6399 | goto out_unlock; |
Casey Schaufler | ad19db7 | 2011-12-15 10:09:07 -0500 | [diff] [blame] | 6400 | |
Nikolay Borisov | cef415a | 2017-02-20 13:51:09 +0200 | [diff] [blame] | 6401 | err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode), |
| 6402 | 0, index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6403 | if (err) |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6404 | goto out_unlock; |
Filipe Brandenburger | 43baa57 | 2012-11-30 03:40:09 +0000 | [diff] [blame] | 6405 | |
Al Viro | 1e2e547 | 2018-05-04 08:23:01 -0400 | [diff] [blame] | 6406 | d_instantiate_new(dentry, inode); |
Filipe Brandenburger | 43baa57 | 2012-11-30 03:40:09 +0000 | [diff] [blame] | 6407 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6408 | out_unlock: |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 6409 | btrfs_end_transaction(trans); |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6410 | if (err && inode) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6411 | inode_dec_link_count(inode); |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6412 | discard_new_inode(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6413 | } |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6414 | btrfs_btree_balance_dirty(fs_info); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6415 | return err; |
| 6416 | } |
| 6417 | |
| 6418 | static int btrfs_link(struct dentry *old_dentry, struct inode *dir, |
| 6419 | struct dentry *dentry) |
| 6420 | { |
Filipe Manana | 271dba45 | 2016-01-05 16:24:05 +0000 | [diff] [blame] | 6421 | struct btrfs_trans_handle *trans = NULL; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6422 | struct btrfs_root *root = BTRFS_I(dir)->root; |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 6423 | struct inode *inode = d_inode(old_dentry); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6424 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 6425 | u64 index; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6426 | int err; |
| 6427 | int drop_inode = 0; |
| 6428 | |
TARUISI Hiroaki | 4a8be42 | 2009-11-12 07:14:26 +0000 | [diff] [blame] | 6429 | /* 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] | 6430 | if (root->root_key.objectid != BTRFS_I(inode)->root->root_key.objectid) |
Mark Fasheh | 3ab3564 | 2011-03-22 17:20:26 +0000 | [diff] [blame] | 6431 | return -EXDEV; |
TARUISI Hiroaki | 4a8be42 | 2009-11-12 07:14:26 +0000 | [diff] [blame] | 6432 | |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 6433 | if (inode->i_nlink >= BTRFS_LINK_MAX) |
Al Viro | c055e99 | 2011-03-04 17:15:18 +0000 | [diff] [blame] | 6434 | return -EMLINK; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 6435 | |
Nikolay Borisov | 877574e | 2017-02-20 13:50:33 +0200 | [diff] [blame] | 6436 | err = btrfs_set_inode_index(BTRFS_I(dir), &index); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6437 | if (err) |
| 6438 | goto fail; |
| 6439 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6440 | /* |
Miao Xie | 7e6b646 | 2011-02-18 09:21:17 +0000 | [diff] [blame] | 6441 | * 2 items for inode and inode ref |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6442 | * 2 items for dir items |
Miao Xie | 7e6b646 | 2011-02-18 09:21:17 +0000 | [diff] [blame] | 6443 | * 1 item for parent inode |
Omar Sandoval | 399b0bb | 2018-05-11 13:13:40 -0700 | [diff] [blame] | 6444 | * 1 item for orphan item deletion if O_TMPFILE |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6445 | */ |
Omar Sandoval | 399b0bb | 2018-05-11 13:13:40 -0700 | [diff] [blame] | 6446 | trans = btrfs_start_transaction(root, inode->i_nlink ? 5 : 6); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6447 | if (IS_ERR(trans)) { |
| 6448 | err = PTR_ERR(trans); |
Filipe Manana | 271dba45 | 2016-01-05 16:24:05 +0000 | [diff] [blame] | 6449 | trans = NULL; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6450 | goto fail; |
| 6451 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6452 | |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 6453 | /* There are several dir indexes for this inode, clear the cache. */ |
| 6454 | BTRFS_I(inode)->dir_index = 0ULL; |
Zach Brown | 8b558c5 | 2013-10-16 12:10:34 -0700 | [diff] [blame] | 6455 | inc_nlink(inode); |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 6456 | inode_inc_iversion(inode); |
Deepa Dinamani | c2050a4 | 2016-09-14 07:48:06 -0700 | [diff] [blame] | 6457 | inode->i_ctime = current_time(inode); |
Al Viro | 7de9c6ee | 2010-10-23 11:11:40 -0400 | [diff] [blame] | 6458 | ihold(inode); |
Josef Bacik | e997615 | 2012-10-11 15:53:56 -0400 | [diff] [blame] | 6459 | set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6460 | |
Nikolay Borisov | cef415a | 2017-02-20 13:51:09 +0200 | [diff] [blame] | 6461 | err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode), |
| 6462 | 1, index); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6463 | |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 6464 | if (err) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6465 | drop_inode = 1; |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 6466 | } else { |
Al Viro | 10d9f30 | 2011-07-16 23:09:10 -0400 | [diff] [blame] | 6467 | struct dentry *parent = dentry->d_parent; |
Filipe Manana | d4682ba | 2018-06-11 19:24:28 +0100 | [diff] [blame] | 6468 | |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 6469 | err = btrfs_update_inode(trans, root, inode); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6470 | if (err) |
| 6471 | goto fail; |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 6472 | if (inode->i_nlink == 1) { |
| 6473 | /* |
| 6474 | * If new hard link count is 1, it's a file created |
| 6475 | * with open(2) O_TMPFILE flag. |
| 6476 | */ |
Nikolay Borisov | 3d6ae7b | 2017-02-20 13:50:58 +0200 | [diff] [blame] | 6477 | err = btrfs_orphan_del(trans, BTRFS_I(inode)); |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 6478 | if (err) |
| 6479 | goto fail; |
| 6480 | } |
Al Viro | 08c422c | 2011-12-23 07:58:13 -0500 | [diff] [blame] | 6481 | d_instantiate(dentry, inode); |
Filipe Manana | 75b463d | 2020-08-11 12:43:48 +0100 | [diff] [blame] | 6482 | btrfs_log_new_name(trans, BTRFS_I(inode), NULL, parent); |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 6483 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6484 | |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 6485 | fail: |
Filipe Manana | 271dba45 | 2016-01-05 16:24:05 +0000 | [diff] [blame] | 6486 | if (trans) |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 6487 | btrfs_end_transaction(trans); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6488 | if (drop_inode) { |
| 6489 | inode_dec_link_count(inode); |
| 6490 | iput(inode); |
| 6491 | } |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6492 | btrfs_btree_balance_dirty(fs_info); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6493 | return err; |
| 6494 | } |
| 6495 | |
Al Viro | 18bb1db | 2011-07-26 01:41:39 -0400 | [diff] [blame] | 6496 | 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] | 6497 | { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6498 | struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); |
Chris Mason | b9d8666 | 2008-05-02 16:13:49 -0400 | [diff] [blame] | 6499 | struct inode *inode = NULL; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6500 | struct btrfs_trans_handle *trans; |
| 6501 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 6502 | int err = 0; |
Chris Mason | b9d8666 | 2008-05-02 16:13:49 -0400 | [diff] [blame] | 6503 | u64 objectid = 0; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 6504 | u64 index = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6505 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 6506 | /* |
| 6507 | * 2 items for inode and ref |
| 6508 | * 2 items for dir items |
| 6509 | * 1 for xattr if selinux is on |
| 6510 | */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6511 | trans = btrfs_start_transaction(root, 5); |
| 6512 | if (IS_ERR(trans)) |
| 6513 | return PTR_ERR(trans); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6514 | |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 6515 | err = btrfs_find_free_ino(root, &objectid); |
| 6516 | if (err) |
| 6517 | goto out_fail; |
| 6518 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6519 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 6520 | dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid, |
| 6521 | S_IFDIR | mode, &index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6522 | if (IS_ERR(inode)) { |
| 6523 | err = PTR_ERR(inode); |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6524 | inode = NULL; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6525 | goto out_fail; |
| 6526 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6527 | |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6528 | /* these must be set before we unlock the inode */ |
| 6529 | inode->i_op = &btrfs_dir_inode_operations; |
| 6530 | inode->i_fop = &btrfs_dir_file_operations; |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 6531 | |
Eric Paris | 2a7dba3 | 2011-02-01 11:05:39 -0500 | [diff] [blame] | 6532 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 6533 | if (err) |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6534 | goto out_fail; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6535 | |
Nikolay Borisov | 6ef06d2 | 2017-02-20 13:50:34 +0200 | [diff] [blame] | 6536 | btrfs_i_size_write(BTRFS_I(inode), 0); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6537 | err = btrfs_update_inode(trans, root, inode); |
| 6538 | if (err) |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6539 | goto out_fail; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6540 | |
Nikolay Borisov | db0a669 | 2017-02-20 13:51:08 +0200 | [diff] [blame] | 6541 | err = btrfs_add_link(trans, BTRFS_I(dir), BTRFS_I(inode), |
| 6542 | dentry->d_name.name, |
| 6543 | dentry->d_name.len, 0, index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6544 | if (err) |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6545 | goto out_fail; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6546 | |
Al Viro | 1e2e547 | 2018-05-04 08:23:01 -0400 | [diff] [blame] | 6547 | d_instantiate_new(dentry, inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6548 | |
| 6549 | out_fail: |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 6550 | btrfs_end_transaction(trans); |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6551 | if (err && inode) { |
Wang Shilong | c7cfb8a | 2014-12-24 14:45:30 +0800 | [diff] [blame] | 6552 | inode_dec_link_count(inode); |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6553 | discard_new_inode(inode); |
Wang Shilong | c7cfb8a | 2014-12-24 14:45:30 +0800 | [diff] [blame] | 6554 | } |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6555 | btrfs_btree_balance_dirty(fs_info); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6556 | return err; |
| 6557 | } |
| 6558 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6559 | static noinline int uncompress_inline(struct btrfs_path *path, |
Byongho Lee | e40da0e | 2015-05-19 23:46:45 +0900 | [diff] [blame] | 6560 | struct page *page, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6561 | size_t pg_offset, u64 extent_offset, |
| 6562 | struct btrfs_file_extent_item *item) |
| 6563 | { |
| 6564 | int ret; |
| 6565 | struct extent_buffer *leaf = path->nodes[0]; |
| 6566 | char *tmp; |
| 6567 | size_t max_size; |
| 6568 | unsigned long inline_size; |
| 6569 | unsigned long ptr; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 6570 | int compress_type; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6571 | |
| 6572 | WARN_ON(pg_offset != 0); |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 6573 | compress_type = btrfs_file_extent_compression(leaf, item); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6574 | max_size = btrfs_file_extent_ram_bytes(leaf, item); |
| 6575 | inline_size = btrfs_file_extent_inline_item_len(leaf, |
Ross Kirk | dd3cc16 | 2013-09-16 15:58:09 +0100 | [diff] [blame] | 6576 | btrfs_item_nr(path->slots[0])); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6577 | tmp = kmalloc(inline_size, GFP_NOFS); |
Tsutomu Itoh | 8d41371 | 2011-04-25 19:43:52 -0400 | [diff] [blame] | 6578 | if (!tmp) |
| 6579 | return -ENOMEM; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6580 | ptr = btrfs_file_extent_inline_start(item); |
| 6581 | |
| 6582 | read_extent_buffer(leaf, tmp, ptr, inline_size); |
| 6583 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 6584 | max_size = min_t(unsigned long, PAGE_SIZE, max_size); |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 6585 | ret = btrfs_decompress(compress_type, tmp, page, |
| 6586 | extent_offset, inline_size, max_size); |
Zygo Blaxell | e1699d2 | 2017-03-10 16:45:44 -0500 | [diff] [blame] | 6587 | |
| 6588 | /* |
| 6589 | * decompression code contains a memset to fill in any space between the end |
| 6590 | * of the uncompressed data and the end of max_size in case the decompressed |
| 6591 | * data ends up shorter than ram_bytes. That doesn't cover the hole between |
| 6592 | * the end of an inline extent and the beginning of the next block, so we |
| 6593 | * cover that region here. |
| 6594 | */ |
| 6595 | |
| 6596 | if (max_size + pg_offset < PAGE_SIZE) { |
| 6597 | char *map = kmap(page); |
| 6598 | memset(map + pg_offset + max_size, 0, PAGE_SIZE - max_size - pg_offset); |
| 6599 | kunmap(page); |
| 6600 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6601 | kfree(tmp); |
Zach Brown | 166ae5a | 2014-05-09 17:15:10 -0400 | [diff] [blame] | 6602 | return ret; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6603 | } |
| 6604 | |
Omar Sandoval | 39b07b5 | 2019-12-02 17:34:23 -0800 | [diff] [blame] | 6605 | /** |
| 6606 | * btrfs_get_extent - Lookup the first extent overlapping a range in a file. |
| 6607 | * @inode: file to search in |
| 6608 | * @page: page to read extent data into if the extent is inline |
| 6609 | * @pg_offset: offset into @page to copy to |
| 6610 | * @start: file offset |
| 6611 | * @len: length of range starting at @start |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 6612 | * |
Omar Sandoval | 39b07b5 | 2019-12-02 17:34:23 -0800 | [diff] [blame] | 6613 | * This returns the first &struct extent_map which overlaps with the given |
| 6614 | * range, reading it from the B-tree and caching it if necessary. Note that |
| 6615 | * there may be more extents which overlap the given range after the returned |
| 6616 | * extent_map. |
| 6617 | * |
| 6618 | * If @page is not NULL and the extent is inline, this also reads the extent |
| 6619 | * data directly into the page and marks the extent up to date in the io_tree. |
| 6620 | * |
| 6621 | * Return: ERR_PTR on error, non-NULL extent_map on success. |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 6622 | */ |
Nikolay Borisov | fc4f21b1 | 2017-02-20 13:51:06 +0200 | [diff] [blame] | 6623 | struct extent_map *btrfs_get_extent(struct btrfs_inode *inode, |
Omar Sandoval | 39b07b5 | 2019-12-02 17:34:23 -0800 | [diff] [blame] | 6624 | struct page *page, size_t pg_offset, |
| 6625 | u64 start, u64 len) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6626 | { |
David Sterba | 3ffbd68 | 2018-06-29 10:56:42 +0200 | [diff] [blame] | 6627 | struct btrfs_fs_info *fs_info = inode->root->fs_info; |
Nikolay Borisov | 1028d1c | 2020-08-03 12:58:46 +0300 | [diff] [blame] | 6628 | int ret = 0; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6629 | u64 extent_start = 0; |
| 6630 | u64 extent_end = 0; |
Nikolay Borisov | fc4f21b1 | 2017-02-20 13:51:06 +0200 | [diff] [blame] | 6631 | u64 objectid = btrfs_ino(inode); |
Linus Torvalds | 7e74e23 | 2019-05-01 12:19:20 -0700 | [diff] [blame] | 6632 | int extent_type = -1; |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 6633 | struct btrfs_path *path = NULL; |
Nikolay Borisov | fc4f21b1 | 2017-02-20 13:51:06 +0200 | [diff] [blame] | 6634 | struct btrfs_root *root = inode->root; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6635 | struct btrfs_file_extent_item *item; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6636 | struct extent_buffer *leaf; |
| 6637 | struct btrfs_key found_key; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6638 | struct extent_map *em = NULL; |
Nikolay Borisov | fc4f21b1 | 2017-02-20 13:51:06 +0200 | [diff] [blame] | 6639 | struct extent_map_tree *em_tree = &inode->extent_tree; |
| 6640 | struct extent_io_tree *io_tree = &inode->io_tree; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6641 | |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 6642 | read_lock(&em_tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6643 | em = lookup_extent_mapping(em_tree, start, len); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 6644 | read_unlock(&em_tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6645 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6646 | if (em) { |
Chris Mason | e1c4b74 | 2008-04-22 13:26:46 -0400 | [diff] [blame] | 6647 | if (em->start > start || em->start + em->len <= start) |
| 6648 | free_extent_map(em); |
| 6649 | else if (em->block_start == EXTENT_MAP_INLINE && page) |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 6650 | free_extent_map(em); |
| 6651 | else |
| 6652 | goto out; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6653 | } |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 6654 | em = alloc_extent_map(); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6655 | if (!em) { |
Nikolay Borisov | 1028d1c | 2020-08-03 12:58:46 +0300 | [diff] [blame] | 6656 | ret = -ENOMEM; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6657 | goto out; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6658 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6659 | em->start = EXTENT_MAP_HOLE; |
Chris Mason | 445a694 | 2008-11-10 11:53:33 -0500 | [diff] [blame] | 6660 | em->orig_start = EXTENT_MAP_HOLE; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6661 | em->len = (u64)-1; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6662 | em->block_len = (u64)-1; |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 6663 | |
Liu Bo | bee6ec8 | 2018-08-17 05:05:28 +0800 | [diff] [blame] | 6664 | path = btrfs_alloc_path(); |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 6665 | if (!path) { |
Nikolay Borisov | 1028d1c | 2020-08-03 12:58:46 +0300 | [diff] [blame] | 6666 | ret = -ENOMEM; |
Liu Bo | bee6ec8 | 2018-08-17 05:05:28 +0800 | [diff] [blame] | 6667 | goto out; |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 6668 | } |
| 6669 | |
Liu Bo | bee6ec8 | 2018-08-17 05:05:28 +0800 | [diff] [blame] | 6670 | /* Chances are we'll be called again, so go ahead and do readahead */ |
| 6671 | path->reada = READA_FORWARD; |
| 6672 | |
Liu Bo | e49aabd | 2018-08-25 13:47:09 +0800 | [diff] [blame] | 6673 | /* |
| 6674 | * Unless we're going to uncompress the inline extent, no sleep would |
| 6675 | * happen. |
| 6676 | */ |
| 6677 | path->leave_spinning = 1; |
| 6678 | |
Josef Bacik | 5189941 | 2020-08-20 11:46:01 -0400 | [diff] [blame] | 6679 | path->recurse = btrfs_is_free_space_inode(inode); |
| 6680 | |
Nikolay Borisov | 5c9a702 | 2017-12-01 11:19:40 +0200 | [diff] [blame] | 6681 | ret = btrfs_lookup_file_extent(NULL, root, path, objectid, start, 0); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6682 | if (ret < 0) { |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6683 | goto out; |
Nikolay Borisov | b8eeab7 | 2018-12-17 11:49:00 +0200 | [diff] [blame] | 6684 | } else if (ret > 0) { |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6685 | if (path->slots[0] == 0) |
| 6686 | goto not_found; |
| 6687 | path->slots[0]--; |
Nikolay Borisov | 1028d1c | 2020-08-03 12:58:46 +0300 | [diff] [blame] | 6688 | ret = 0; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6689 | } |
| 6690 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6691 | leaf = path->nodes[0]; |
| 6692 | item = btrfs_item_ptr(leaf, path->slots[0], |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6693 | struct btrfs_file_extent_item); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6694 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6695 | if (found_key.objectid != objectid || |
Nikolay Borisov | 694c12e | 2018-12-17 10:35:59 +0200 | [diff] [blame] | 6696 | found_key.type != BTRFS_EXTENT_DATA_KEY) { |
Josef Bacik | 25a5034 | 2013-10-14 12:08:38 -0400 | [diff] [blame] | 6697 | /* |
| 6698 | * If we backup past the first extent we want to move forward |
| 6699 | * and see if there is an extent in front of us, otherwise we'll |
| 6700 | * say there is a hole for our whole search range which can |
| 6701 | * cause problems. |
| 6702 | */ |
| 6703 | extent_end = start; |
| 6704 | goto next; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6705 | } |
| 6706 | |
Nikolay Borisov | 694c12e | 2018-12-17 10:35:59 +0200 | [diff] [blame] | 6707 | extent_type = btrfs_file_extent_type(leaf, item); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6708 | extent_start = found_key.offset; |
Filipe Manana | a5eeb3d | 2020-03-09 12:41:06 +0000 | [diff] [blame] | 6709 | extent_end = btrfs_file_extent_end(path); |
Nikolay Borisov | 694c12e | 2018-12-17 10:35:59 +0200 | [diff] [blame] | 6710 | if (extent_type == BTRFS_FILE_EXTENT_REG || |
| 6711 | extent_type == BTRFS_FILE_EXTENT_PREALLOC) { |
Qu Wenruo | 6bf9e4b | 2019-03-13 13:55:11 +0800 | [diff] [blame] | 6712 | /* Only regular file could have regular/prealloc extent */ |
| 6713 | if (!S_ISREG(inode->vfs_inode.i_mode)) { |
Nikolay Borisov | 1028d1c | 2020-08-03 12:58:46 +0300 | [diff] [blame] | 6714 | ret = -EUCLEAN; |
Qu Wenruo | 6bf9e4b | 2019-03-13 13:55:11 +0800 | [diff] [blame] | 6715 | btrfs_crit(fs_info, |
| 6716 | "regular/prealloc extent found for non-regular inode %llu", |
| 6717 | btrfs_ino(inode)); |
| 6718 | goto out; |
| 6719 | } |
Liu Bo | 09ed2f1 | 2017-03-10 11:09:48 -0800 | [diff] [blame] | 6720 | trace_btrfs_get_extent_show_fi_regular(inode, leaf, item, |
| 6721 | extent_start); |
Nikolay Borisov | 694c12e | 2018-12-17 10:35:59 +0200 | [diff] [blame] | 6722 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
Liu Bo | 09ed2f1 | 2017-03-10 11:09:48 -0800 | [diff] [blame] | 6723 | trace_btrfs_get_extent_show_fi_inline(inode, leaf, item, |
| 6724 | path->slots[0], |
| 6725 | extent_start); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6726 | } |
Josef Bacik | 25a5034 | 2013-10-14 12:08:38 -0400 | [diff] [blame] | 6727 | next: |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6728 | if (start >= extent_end) { |
| 6729 | path->slots[0]++; |
| 6730 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { |
| 6731 | ret = btrfs_next_leaf(root, path); |
Nikolay Borisov | 1028d1c | 2020-08-03 12:58:46 +0300 | [diff] [blame] | 6732 | if (ret < 0) |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6733 | goto out; |
Nikolay Borisov | 1028d1c | 2020-08-03 12:58:46 +0300 | [diff] [blame] | 6734 | else if (ret > 0) |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6735 | goto not_found; |
Nikolay Borisov | 1028d1c | 2020-08-03 12:58:46 +0300 | [diff] [blame] | 6736 | |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6737 | leaf = path->nodes[0]; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6738 | } |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6739 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 6740 | if (found_key.objectid != objectid || |
| 6741 | found_key.type != BTRFS_EXTENT_DATA_KEY) |
| 6742 | goto not_found; |
| 6743 | if (start + len <= found_key.offset) |
| 6744 | goto not_found; |
Wang Shilong | e2eca69 | 2014-07-17 11:44:14 +0800 | [diff] [blame] | 6745 | if (start > found_key.offset) |
| 6746 | goto next; |
Nikolay Borisov | 02a033d | 2018-12-17 10:36:02 +0200 | [diff] [blame] | 6747 | |
| 6748 | /* New extent overlaps with existing one */ |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6749 | em->start = start; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 6750 | em->orig_start = start; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6751 | em->len = found_key.offset - start; |
Nikolay Borisov | 02a033d | 2018-12-17 10:36:02 +0200 | [diff] [blame] | 6752 | em->block_start = EXTENT_MAP_HOLE; |
| 6753 | goto insert; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6754 | } |
| 6755 | |
Omar Sandoval | 39b07b5 | 2019-12-02 17:34:23 -0800 | [diff] [blame] | 6756 | btrfs_extent_item_to_extent_map(inode, path, item, !page, em); |
Filipe Manana | 7ffbb59 | 2014-06-09 03:48:05 +0100 | [diff] [blame] | 6757 | |
Nikolay Borisov | 694c12e | 2018-12-17 10:35:59 +0200 | [diff] [blame] | 6758 | if (extent_type == BTRFS_FILE_EXTENT_REG || |
| 6759 | extent_type == BTRFS_FILE_EXTENT_PREALLOC) { |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6760 | goto insert; |
Nikolay Borisov | 694c12e | 2018-12-17 10:35:59 +0200 | [diff] [blame] | 6761 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6762 | unsigned long ptr; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6763 | char *map; |
Chris Mason | 3326d1b | 2007-10-15 16:18:25 -0400 | [diff] [blame] | 6764 | size_t size; |
| 6765 | size_t extent_offset; |
| 6766 | size_t copy_size; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6767 | |
Omar Sandoval | 39b07b5 | 2019-12-02 17:34:23 -0800 | [diff] [blame] | 6768 | if (!page) |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 6769 | goto out; |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 6770 | |
Qu Wenruo | e41ca58 | 2018-06-06 15:41:49 +0800 | [diff] [blame] | 6771 | size = btrfs_file_extent_ram_bytes(leaf, item); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6772 | extent_offset = page_offset(page) + pg_offset - extent_start; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 6773 | copy_size = min_t(u64, PAGE_SIZE - pg_offset, |
| 6774 | size - extent_offset); |
Chris Mason | 3326d1b | 2007-10-15 16:18:25 -0400 | [diff] [blame] | 6775 | em->start = extent_start + extent_offset; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6776 | em->len = ALIGN(copy_size, fs_info->sectorsize); |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 6777 | em->orig_block_len = em->len; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 6778 | em->orig_start = em->start; |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 6779 | ptr = btrfs_file_extent_inline_start(item) + extent_offset; |
Liu Bo | e49aabd | 2018-08-25 13:47:09 +0800 | [diff] [blame] | 6780 | |
| 6781 | btrfs_set_path_blocking(path); |
Edmund Nadolski | bf46f52 | 2017-11-20 13:24:49 -0700 | [diff] [blame] | 6782 | if (!PageUptodate(page)) { |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 6783 | if (btrfs_file_extent_compression(leaf, item) != |
| 6784 | BTRFS_COMPRESS_NONE) { |
Byongho Lee | e40da0e | 2015-05-19 23:46:45 +0900 | [diff] [blame] | 6785 | ret = uncompress_inline(path, page, pg_offset, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6786 | extent_offset, item); |
Nikolay Borisov | 1028d1c | 2020-08-03 12:58:46 +0300 | [diff] [blame] | 6787 | if (ret) |
Zach Brown | 166ae5a | 2014-05-09 17:15:10 -0400 | [diff] [blame] | 6788 | goto out; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6789 | } else { |
| 6790 | map = kmap(page); |
| 6791 | read_extent_buffer(leaf, map + pg_offset, ptr, |
| 6792 | copy_size); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 6793 | if (pg_offset + copy_size < PAGE_SIZE) { |
Chris Mason | 93c82d5 | 2009-09-11 12:36:29 -0400 | [diff] [blame] | 6794 | memset(map + pg_offset + copy_size, 0, |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 6795 | PAGE_SIZE - pg_offset - |
Chris Mason | 93c82d5 | 2009-09-11 12:36:29 -0400 | [diff] [blame] | 6796 | copy_size); |
| 6797 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6798 | kunmap(page); |
| 6799 | } |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 6800 | flush_dcache_page(page); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6801 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6802 | set_extent_uptodate(io_tree, em->start, |
Arne Jansen | 507903b | 2011-04-06 10:02:20 +0000 | [diff] [blame] | 6803 | extent_map_end(em) - 1, NULL, GFP_NOFS); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6804 | goto insert; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6805 | } |
| 6806 | not_found: |
| 6807 | em->start = start; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 6808 | em->orig_start = start; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6809 | em->len = len; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6810 | em->block_start = EXTENT_MAP_HOLE; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6811 | insert: |
Nikolay Borisov | 1028d1c | 2020-08-03 12:58:46 +0300 | [diff] [blame] | 6812 | ret = 0; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 6813 | btrfs_release_path(path); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6814 | if (em->start > start || extent_map_end(em) <= start) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6815 | btrfs_err(fs_info, |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 6816 | "bad extent! em: [%llu %llu] passed [%llu %llu]", |
| 6817 | em->start, em->len, start, len); |
Nikolay Borisov | 1028d1c | 2020-08-03 12:58:46 +0300 | [diff] [blame] | 6818 | ret = -EIO; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6819 | goto out; |
| 6820 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6821 | |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 6822 | write_lock(&em_tree->lock); |
Nikolay Borisov | 1028d1c | 2020-08-03 12:58:46 +0300 | [diff] [blame] | 6823 | ret = btrfs_add_extent_mapping(fs_info, em_tree, &em, start, len); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 6824 | write_unlock(&em_tree->lock); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6825 | out: |
Liu Bo | c641428 | 2018-08-23 07:36:17 +0800 | [diff] [blame] | 6826 | btrfs_free_path(path); |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 6827 | |
Nikolay Borisov | fc4f21b1 | 2017-02-20 13:51:06 +0200 | [diff] [blame] | 6828 | trace_btrfs_get_extent(root, inode, em); |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 6829 | |
Nikolay Borisov | 1028d1c | 2020-08-03 12:58:46 +0300 | [diff] [blame] | 6830 | if (ret) { |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6831 | free_extent_map(em); |
Nikolay Borisov | 1028d1c | 2020-08-03 12:58:46 +0300 | [diff] [blame] | 6832 | return ERR_PTR(ret); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6833 | } |
| 6834 | return em; |
| 6835 | } |
| 6836 | |
Nikolay Borisov | fc4f21b1 | 2017-02-20 13:51:06 +0200 | [diff] [blame] | 6837 | struct extent_map *btrfs_get_extent_fiemap(struct btrfs_inode *inode, |
Nikolay Borisov | 4ab47a8 | 2018-12-12 09:42:32 +0200 | [diff] [blame] | 6838 | u64 start, u64 len) |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6839 | { |
| 6840 | struct extent_map *em; |
| 6841 | struct extent_map *hole_em = NULL; |
Nikolay Borisov | f3714ef | 2018-12-12 09:42:33 +0200 | [diff] [blame] | 6842 | u64 delalloc_start = start; |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6843 | u64 end; |
Nikolay Borisov | f3714ef | 2018-12-12 09:42:33 +0200 | [diff] [blame] | 6844 | u64 delalloc_len; |
| 6845 | u64 delalloc_end; |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6846 | int err = 0; |
| 6847 | |
Omar Sandoval | 39b07b5 | 2019-12-02 17:34:23 -0800 | [diff] [blame] | 6848 | em = btrfs_get_extent(inode, NULL, 0, start, len); |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6849 | if (IS_ERR(em)) |
| 6850 | return em; |
Dan Carpenter | 9986277 | 2017-04-11 11:57:15 +0300 | [diff] [blame] | 6851 | /* |
| 6852 | * If our em maps to: |
| 6853 | * - a hole or |
| 6854 | * - a pre-alloc extent, |
| 6855 | * there might actually be delalloc bytes behind it. |
| 6856 | */ |
| 6857 | if (em->block_start != EXTENT_MAP_HOLE && |
| 6858 | !test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) |
| 6859 | return em; |
| 6860 | else |
| 6861 | hole_em = em; |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6862 | |
| 6863 | /* check to see if we've wrapped (len == -1 or similar) */ |
| 6864 | end = start + len; |
| 6865 | if (end < start) |
| 6866 | end = (u64)-1; |
| 6867 | else |
| 6868 | end -= 1; |
| 6869 | |
| 6870 | em = NULL; |
| 6871 | |
| 6872 | /* ok, we didn't find anything, lets look for delalloc */ |
Nikolay Borisov | f3714ef | 2018-12-12 09:42:33 +0200 | [diff] [blame] | 6873 | delalloc_len = count_range_bits(&inode->io_tree, &delalloc_start, |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6874 | end, len, EXTENT_DELALLOC, 1); |
Nikolay Borisov | f3714ef | 2018-12-12 09:42:33 +0200 | [diff] [blame] | 6875 | delalloc_end = delalloc_start + delalloc_len; |
| 6876 | if (delalloc_end < delalloc_start) |
| 6877 | delalloc_end = (u64)-1; |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6878 | |
| 6879 | /* |
Nikolay Borisov | f3714ef | 2018-12-12 09:42:33 +0200 | [diff] [blame] | 6880 | * We didn't find anything useful, return the original results from |
| 6881 | * get_extent() |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6882 | */ |
Nikolay Borisov | f3714ef | 2018-12-12 09:42:33 +0200 | [diff] [blame] | 6883 | if (delalloc_start > end || delalloc_end <= start) { |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6884 | em = hole_em; |
| 6885 | hole_em = NULL; |
| 6886 | goto out; |
| 6887 | } |
| 6888 | |
Nikolay Borisov | f3714ef | 2018-12-12 09:42:33 +0200 | [diff] [blame] | 6889 | /* |
| 6890 | * Adjust the delalloc_start to make sure it doesn't go backwards from |
| 6891 | * the start they passed in |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6892 | */ |
Nikolay Borisov | f3714ef | 2018-12-12 09:42:33 +0200 | [diff] [blame] | 6893 | delalloc_start = max(start, delalloc_start); |
| 6894 | delalloc_len = delalloc_end - delalloc_start; |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6895 | |
Nikolay Borisov | f3714ef | 2018-12-12 09:42:33 +0200 | [diff] [blame] | 6896 | if (delalloc_len > 0) { |
| 6897 | u64 hole_start; |
Nikolay Borisov | 02950af | 2018-12-12 09:42:34 +0200 | [diff] [blame] | 6898 | u64 hole_len; |
Nikolay Borisov | f3714ef | 2018-12-12 09:42:33 +0200 | [diff] [blame] | 6899 | const u64 hole_end = extent_map_end(hole_em); |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6900 | |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 6901 | em = alloc_extent_map(); |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6902 | if (!em) { |
| 6903 | err = -ENOMEM; |
| 6904 | goto out; |
| 6905 | } |
Nikolay Borisov | f3714ef | 2018-12-12 09:42:33 +0200 | [diff] [blame] | 6906 | |
| 6907 | ASSERT(hole_em); |
| 6908 | /* |
| 6909 | * When btrfs_get_extent can't find anything it returns one |
| 6910 | * huge hole |
| 6911 | * |
| 6912 | * Make sure what it found really fits our range, and adjust to |
| 6913 | * make sure it is based on the start from the caller |
| 6914 | */ |
| 6915 | if (hole_end <= start || hole_em->start > end) { |
| 6916 | free_extent_map(hole_em); |
| 6917 | hole_em = NULL; |
| 6918 | } else { |
| 6919 | hole_start = max(hole_em->start, start); |
| 6920 | hole_len = hole_end - hole_start; |
| 6921 | } |
| 6922 | |
| 6923 | if (hole_em && delalloc_start > hole_start) { |
| 6924 | /* |
| 6925 | * Our hole starts before our delalloc, so we have to |
| 6926 | * return just the parts of the hole that go until the |
| 6927 | * delalloc starts |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6928 | */ |
Nikolay Borisov | f3714ef | 2018-12-12 09:42:33 +0200 | [diff] [blame] | 6929 | em->len = min(hole_len, delalloc_start - hole_start); |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6930 | em->start = hole_start; |
| 6931 | em->orig_start = hole_start; |
| 6932 | /* |
Nikolay Borisov | f3714ef | 2018-12-12 09:42:33 +0200 | [diff] [blame] | 6933 | * Don't adjust block start at all, it is fixed at |
| 6934 | * EXTENT_MAP_HOLE |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6935 | */ |
| 6936 | em->block_start = hole_em->block_start; |
| 6937 | em->block_len = hole_len; |
Liu Bo | f9e4fb5 | 2013-01-07 10:10:12 +0000 | [diff] [blame] | 6938 | if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags)) |
| 6939 | set_bit(EXTENT_FLAG_PREALLOC, &em->flags); |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6940 | } else { |
Nikolay Borisov | f3714ef | 2018-12-12 09:42:33 +0200 | [diff] [blame] | 6941 | /* |
| 6942 | * Hole is out of passed range or it starts after |
| 6943 | * delalloc range |
| 6944 | */ |
| 6945 | em->start = delalloc_start; |
| 6946 | em->len = delalloc_len; |
| 6947 | em->orig_start = delalloc_start; |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6948 | em->block_start = EXTENT_MAP_DELALLOC; |
Nikolay Borisov | f3714ef | 2018-12-12 09:42:33 +0200 | [diff] [blame] | 6949 | em->block_len = delalloc_len; |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6950 | } |
Nikolay Borisov | bf8d32b | 2017-12-01 11:19:43 +0200 | [diff] [blame] | 6951 | } else { |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6952 | return hole_em; |
| 6953 | } |
| 6954 | out: |
| 6955 | |
| 6956 | free_extent_map(hole_em); |
| 6957 | if (err) { |
| 6958 | free_extent_map(em); |
| 6959 | return ERR_PTR(err); |
| 6960 | } |
| 6961 | return em; |
| 6962 | } |
| 6963 | |
Nikolay Borisov | 64f5418 | 2020-06-03 08:55:31 +0300 | [diff] [blame] | 6964 | static struct extent_map *btrfs_create_dio_extent(struct btrfs_inode *inode, |
Filipe Manana | 5f9a8a5 | 2016-05-12 13:53:36 +0100 | [diff] [blame] | 6965 | const u64 start, |
| 6966 | const u64 len, |
| 6967 | const u64 orig_start, |
| 6968 | const u64 block_start, |
| 6969 | const u64 block_len, |
| 6970 | const u64 orig_block_len, |
| 6971 | const u64 ram_bytes, |
| 6972 | const int type) |
| 6973 | { |
| 6974 | struct extent_map *em = NULL; |
| 6975 | int ret; |
| 6976 | |
Filipe Manana | 5f9a8a5 | 2016-05-12 13:53:36 +0100 | [diff] [blame] | 6977 | if (type != BTRFS_ORDERED_NOCOW) { |
Nikolay Borisov | 64f5418 | 2020-06-03 08:55:31 +0300 | [diff] [blame] | 6978 | em = create_io_em(inode, start, len, orig_start, block_start, |
| 6979 | block_len, orig_block_len, ram_bytes, |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 6980 | BTRFS_COMPRESS_NONE, /* compress_type */ |
| 6981 | type); |
Filipe Manana | 5f9a8a5 | 2016-05-12 13:53:36 +0100 | [diff] [blame] | 6982 | if (IS_ERR(em)) |
| 6983 | goto out; |
| 6984 | } |
Nikolay Borisov | 64f5418 | 2020-06-03 08:55:31 +0300 | [diff] [blame] | 6985 | ret = btrfs_add_ordered_extent_dio(inode, start, block_start, len, |
| 6986 | block_len, type); |
Filipe Manana | 5f9a8a5 | 2016-05-12 13:53:36 +0100 | [diff] [blame] | 6987 | if (ret) { |
| 6988 | if (em) { |
| 6989 | free_extent_map(em); |
Nikolay Borisov | 64f5418 | 2020-06-03 08:55:31 +0300 | [diff] [blame] | 6990 | btrfs_drop_extent_cache(inode, start, start + len - 1, 0); |
Filipe Manana | 5f9a8a5 | 2016-05-12 13:53:36 +0100 | [diff] [blame] | 6991 | } |
| 6992 | em = ERR_PTR(ret); |
| 6993 | } |
| 6994 | out: |
Filipe Manana | 5f9a8a5 | 2016-05-12 13:53:36 +0100 | [diff] [blame] | 6995 | |
| 6996 | return em; |
| 6997 | } |
| 6998 | |
Nikolay Borisov | 9fc6f91 | 2020-06-03 08:55:32 +0300 | [diff] [blame] | 6999 | static struct extent_map *btrfs_new_extent_direct(struct btrfs_inode *inode, |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7000 | u64 start, u64 len) |
| 7001 | { |
Nikolay Borisov | 9fc6f91 | 2020-06-03 08:55:32 +0300 | [diff] [blame] | 7002 | struct btrfs_root *root = inode->root; |
| 7003 | struct btrfs_fs_info *fs_info = root->fs_info; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 7004 | struct extent_map *em; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7005 | struct btrfs_key ins; |
| 7006 | u64 alloc_hint; |
| 7007 | int ret; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7008 | |
Nikolay Borisov | 9fc6f91 | 2020-06-03 08:55:32 +0300 | [diff] [blame] | 7009 | alloc_hint = get_extent_allocation_hint(inode, start, len); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7010 | ret = btrfs_reserve_extent(root, len, len, fs_info->sectorsize, |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 7011 | 0, alloc_hint, &ins, 1, 1); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 7012 | if (ret) |
| 7013 | return ERR_PTR(ret); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7014 | |
Nikolay Borisov | 9fc6f91 | 2020-06-03 08:55:32 +0300 | [diff] [blame] | 7015 | em = btrfs_create_dio_extent(inode, start, ins.offset, start, |
Filipe Manana | 5f9a8a5 | 2016-05-12 13:53:36 +0100 | [diff] [blame] | 7016 | ins.objectid, ins.offset, ins.offset, |
Liu Bo | 6288d6e | 2017-02-21 12:12:58 -0800 | [diff] [blame] | 7017 | ins.offset, BTRFS_ORDERED_REGULAR); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7018 | btrfs_dec_block_group_reservations(fs_info, ins.objectid); |
Filipe Manana | 5f9a8a5 | 2016-05-12 13:53:36 +0100 | [diff] [blame] | 7019 | if (IS_ERR(em)) |
Nikolay Borisov | 9fc6f91 | 2020-06-03 08:55:32 +0300 | [diff] [blame] | 7020 | btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, |
| 7021 | 1); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 7022 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7023 | return em; |
| 7024 | } |
| 7025 | |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7026 | /* |
Qu Wenruo | e4ecaf9 | 2020-06-24 07:23:51 +0800 | [diff] [blame] | 7027 | * Check if we can do nocow write into the range [@offset, @offset + @len) |
| 7028 | * |
| 7029 | * @offset: File offset |
| 7030 | * @len: The length to write, will be updated to the nocow writeable |
| 7031 | * range |
| 7032 | * @orig_start: (optional) Return the original file offset of the file extent |
| 7033 | * @orig_len: (optional) Return the original on-disk length of the file extent |
| 7034 | * @ram_bytes: (optional) Return the ram_bytes of the file extent |
Boris Burkov | a84d5d4 | 2020-08-18 11:00:05 -0700 | [diff] [blame] | 7035 | * @strict: if true, omit optimizations that might force us into unnecessary |
| 7036 | * cow. e.g., don't trust generation number. |
Qu Wenruo | e4ecaf9 | 2020-06-24 07:23:51 +0800 | [diff] [blame] | 7037 | * |
| 7038 | * This function will flush ordered extents in the range to ensure proper |
| 7039 | * nocow checks for (nowait == false) case. |
| 7040 | * |
| 7041 | * Return: |
| 7042 | * >0 and update @len if we can do nocow write |
| 7043 | * 0 if we can't do nocow write |
| 7044 | * <0 if error happened |
| 7045 | * |
| 7046 | * NOTE: This only checks the file extents, caller is responsible to wait for |
| 7047 | * any ordered extents. |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7048 | */ |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 7049 | noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len, |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 7050 | u64 *orig_start, u64 *orig_block_len, |
Boris Burkov | a84d5d4 | 2020-08-18 11:00:05 -0700 | [diff] [blame] | 7051 | u64 *ram_bytes, bool strict) |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7052 | { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 7053 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7054 | struct btrfs_path *path; |
| 7055 | int ret; |
| 7056 | struct extent_buffer *leaf; |
| 7057 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Miao Xie | 7b2b708 | 2014-02-27 13:58:05 +0800 | [diff] [blame] | 7058 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7059 | struct btrfs_file_extent_item *fi; |
| 7060 | struct btrfs_key key; |
| 7061 | u64 disk_bytenr; |
| 7062 | u64 backref_offset; |
| 7063 | u64 extent_end; |
| 7064 | u64 num_bytes; |
| 7065 | int slot; |
| 7066 | int found_type; |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 7067 | bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW); |
Miao Xie | e77751a | 2013-12-27 21:11:50 +0800 | [diff] [blame] | 7068 | |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7069 | path = btrfs_alloc_path(); |
| 7070 | if (!path) |
| 7071 | return -ENOMEM; |
| 7072 | |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 7073 | ret = btrfs_lookup_file_extent(NULL, root, path, |
| 7074 | btrfs_ino(BTRFS_I(inode)), offset, 0); |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7075 | if (ret < 0) |
| 7076 | goto out; |
| 7077 | |
| 7078 | slot = path->slots[0]; |
| 7079 | if (ret == 1) { |
| 7080 | if (slot == 0) { |
| 7081 | /* can't find the item, must cow */ |
| 7082 | ret = 0; |
| 7083 | goto out; |
| 7084 | } |
| 7085 | slot--; |
| 7086 | } |
| 7087 | ret = 0; |
| 7088 | leaf = path->nodes[0]; |
| 7089 | btrfs_item_key_to_cpu(leaf, &key, slot); |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 7090 | if (key.objectid != btrfs_ino(BTRFS_I(inode)) || |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7091 | key.type != BTRFS_EXTENT_DATA_KEY) { |
| 7092 | /* not our file or wrong item type, must cow */ |
| 7093 | goto out; |
| 7094 | } |
| 7095 | |
| 7096 | if (key.offset > offset) { |
| 7097 | /* Wrong offset, must cow */ |
| 7098 | goto out; |
| 7099 | } |
| 7100 | |
| 7101 | fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item); |
| 7102 | found_type = btrfs_file_extent_type(leaf, fi); |
| 7103 | if (found_type != BTRFS_FILE_EXTENT_REG && |
| 7104 | found_type != BTRFS_FILE_EXTENT_PREALLOC) { |
| 7105 | /* not a regular extent, must cow */ |
| 7106 | goto out; |
| 7107 | } |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 7108 | |
| 7109 | if (!nocow && found_type == BTRFS_FILE_EXTENT_REG) |
| 7110 | goto out; |
| 7111 | |
Miao Xie | e77751a | 2013-12-27 21:11:50 +0800 | [diff] [blame] | 7112 | extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi); |
| 7113 | if (extent_end <= offset) |
| 7114 | goto out; |
| 7115 | |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7116 | disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 7117 | if (disk_bytenr == 0) |
| 7118 | goto out; |
| 7119 | |
| 7120 | if (btrfs_file_extent_compression(leaf, fi) || |
| 7121 | btrfs_file_extent_encryption(leaf, fi) || |
| 7122 | btrfs_file_extent_other_encoding(leaf, fi)) |
| 7123 | goto out; |
| 7124 | |
Ethan Lien | 78d4295 | 2018-05-17 14:58:29 +0800 | [diff] [blame] | 7125 | /* |
| 7126 | * Do the same check as in btrfs_cross_ref_exist but without the |
| 7127 | * unnecessary search. |
| 7128 | */ |
Boris Burkov | a84d5d4 | 2020-08-18 11:00:05 -0700 | [diff] [blame] | 7129 | if (!strict && |
| 7130 | (btrfs_file_extent_generation(leaf, fi) <= |
| 7131 | btrfs_root_last_snapshot(&root->root_item))) |
Ethan Lien | 78d4295 | 2018-05-17 14:58:29 +0800 | [diff] [blame] | 7132 | goto out; |
| 7133 | |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7134 | backref_offset = btrfs_file_extent_offset(leaf, fi); |
| 7135 | |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 7136 | if (orig_start) { |
| 7137 | *orig_start = key.offset - backref_offset; |
| 7138 | *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi); |
| 7139 | *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi); |
| 7140 | } |
Josef Bacik | eb384b5 | 2013-04-24 16:32:55 -0400 | [diff] [blame] | 7141 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 7142 | if (btrfs_extent_readonly(fs_info, disk_bytenr)) |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7143 | goto out; |
Miao Xie | 7b2b708 | 2014-02-27 13:58:05 +0800 | [diff] [blame] | 7144 | |
| 7145 | num_bytes = min(offset + *len, extent_end) - offset; |
| 7146 | if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) { |
| 7147 | u64 range_end; |
| 7148 | |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 7149 | range_end = round_up(offset + num_bytes, |
| 7150 | root->fs_info->sectorsize) - 1; |
Miao Xie | 7b2b708 | 2014-02-27 13:58:05 +0800 | [diff] [blame] | 7151 | ret = test_range_bit(io_tree, offset, range_end, |
| 7152 | EXTENT_DELALLOC, 0, NULL); |
| 7153 | if (ret) { |
| 7154 | ret = -EAGAIN; |
| 7155 | goto out; |
| 7156 | } |
| 7157 | } |
| 7158 | |
Josef Bacik | 1bda19e | 2013-10-18 12:10:36 -0400 | [diff] [blame] | 7159 | btrfs_release_path(path); |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7160 | |
| 7161 | /* |
| 7162 | * look for other files referencing this extent, if we |
| 7163 | * find any we must cow |
| 7164 | */ |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 7165 | |
Liu Bo | e4c3b2d | 2017-01-30 12:25:28 -0800 | [diff] [blame] | 7166 | ret = btrfs_cross_ref_exist(root, btrfs_ino(BTRFS_I(inode)), |
Boris Burkov | a84d5d4 | 2020-08-18 11:00:05 -0700 | [diff] [blame] | 7167 | key.offset - backref_offset, disk_bytenr, |
| 7168 | strict); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 7169 | if (ret) { |
| 7170 | ret = 0; |
| 7171 | goto out; |
| 7172 | } |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7173 | |
| 7174 | /* |
| 7175 | * adjust disk_bytenr and num_bytes to cover just the bytes |
| 7176 | * in this extent we are about to write. If there |
| 7177 | * are any csums in that range we have to cow in order |
| 7178 | * to keep the csums correct |
| 7179 | */ |
| 7180 | disk_bytenr += backref_offset; |
| 7181 | disk_bytenr += offset - key.offset; |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 7182 | if (csum_exist_in_range(fs_info, disk_bytenr, num_bytes)) |
| 7183 | goto out; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7184 | /* |
| 7185 | * all of the above have passed, it is safe to overwrite this extent |
| 7186 | * without cow |
| 7187 | */ |
Josef Bacik | eb384b5 | 2013-04-24 16:32:55 -0400 | [diff] [blame] | 7188 | *len = num_bytes; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7189 | ret = 1; |
| 7190 | out: |
| 7191 | btrfs_free_path(path); |
| 7192 | return ret; |
| 7193 | } |
| 7194 | |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7195 | static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend, |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 7196 | struct extent_state **cached_state, bool writing) |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7197 | { |
| 7198 | struct btrfs_ordered_extent *ordered; |
| 7199 | int ret = 0; |
| 7200 | |
| 7201 | while (1) { |
| 7202 | lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend, |
David Sterba | ff13db4 | 2015-12-03 14:30:40 +0100 | [diff] [blame] | 7203 | cached_state); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7204 | /* |
| 7205 | * 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] | 7206 | * 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] | 7207 | * extents in this range. |
| 7208 | */ |
Nikolay Borisov | a776c6f | 2017-02-20 13:50:49 +0200 | [diff] [blame] | 7209 | ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), lockstart, |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7210 | lockend - lockstart + 1); |
| 7211 | |
| 7212 | /* |
| 7213 | * We need to make sure there are no buffered pages in this |
| 7214 | * range either, we could have raced between the invalidate in |
| 7215 | * generic_file_direct_write and locking the extent. The |
| 7216 | * invalidate needs to happen so that reads after a write do not |
| 7217 | * get stale data. |
| 7218 | */ |
Alex Gartrell | fc4adbff | 2014-05-20 13:07:56 -0700 | [diff] [blame] | 7219 | if (!ordered && |
David Sterba | 051c98e | 2018-03-07 15:33:22 +0100 | [diff] [blame] | 7220 | (!writing || !filemap_range_has_page(inode->i_mapping, |
| 7221 | lockstart, lockend))) |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7222 | break; |
| 7223 | |
| 7224 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend, |
David Sterba | e43bbe5 | 2017-12-12 21:43:52 +0100 | [diff] [blame] | 7225 | cached_state); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7226 | |
| 7227 | if (ordered) { |
Filipe Manana | ade7702 | 2016-02-18 14:28:55 +0000 | [diff] [blame] | 7228 | /* |
| 7229 | * If we are doing a DIO read and the ordered extent we |
| 7230 | * found is for a buffered write, we can not wait for it |
| 7231 | * to complete and retry, because if we do so we can |
| 7232 | * deadlock with concurrent buffered writes on page |
| 7233 | * locks. This happens only if our DIO read covers more |
| 7234 | * than one extent map, if at this point has already |
| 7235 | * created an ordered extent for a previous extent map |
| 7236 | * and locked its range in the inode's io tree, and a |
| 7237 | * concurrent write against that previous extent map's |
| 7238 | * range and this range started (we unlock the ranges |
| 7239 | * in the io tree only when the bios complete and |
| 7240 | * buffered writes always lock pages before attempting |
| 7241 | * to lock range in the io tree). |
| 7242 | */ |
| 7243 | if (writing || |
| 7244 | test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags)) |
Nikolay Borisov | c0a4360 | 2020-09-18 12:15:53 +0300 | [diff] [blame] | 7245 | btrfs_start_ordered_extent(ordered, 1); |
Filipe Manana | ade7702 | 2016-02-18 14:28:55 +0000 | [diff] [blame] | 7246 | else |
| 7247 | ret = -ENOTBLK; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7248 | btrfs_put_ordered_extent(ordered); |
| 7249 | } else { |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7250 | /* |
Filipe Manana | b850ae1 | 2015-12-08 16:23:16 +0000 | [diff] [blame] | 7251 | * We could trigger writeback for this range (and wait |
| 7252 | * for it to complete) and then invalidate the pages for |
| 7253 | * this range (through invalidate_inode_pages2_range()), |
| 7254 | * but that can lead us to a deadlock with a concurrent |
Matthew Wilcox (Oracle) | ba206a0 | 2020-06-01 21:47:05 -0700 | [diff] [blame] | 7255 | * call to readahead (a buffered read or a defrag call |
Filipe Manana | b850ae1 | 2015-12-08 16:23:16 +0000 | [diff] [blame] | 7256 | * triggered a readahead) on a page lock due to an |
| 7257 | * ordered dio extent we created before but did not have |
| 7258 | * yet a corresponding bio submitted (whence it can not |
Matthew Wilcox (Oracle) | ba206a0 | 2020-06-01 21:47:05 -0700 | [diff] [blame] | 7259 | * complete), which makes readahead wait for that |
Filipe Manana | b850ae1 | 2015-12-08 16:23:16 +0000 | [diff] [blame] | 7260 | * ordered extent to complete while holding a lock on |
| 7261 | * that page. |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7262 | */ |
Filipe Manana | b850ae1 | 2015-12-08 16:23:16 +0000 | [diff] [blame] | 7263 | ret = -ENOTBLK; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7264 | } |
| 7265 | |
Filipe Manana | ade7702 | 2016-02-18 14:28:55 +0000 | [diff] [blame] | 7266 | if (ret) |
| 7267 | break; |
| 7268 | |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7269 | cond_resched(); |
| 7270 | } |
| 7271 | |
| 7272 | return ret; |
| 7273 | } |
| 7274 | |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 7275 | /* The callers of this must take lock_extent() */ |
Nikolay Borisov | 4b67c11 | 2020-06-03 08:55:05 +0300 | [diff] [blame] | 7276 | static struct extent_map *create_io_em(struct btrfs_inode *inode, u64 start, |
| 7277 | u64 len, u64 orig_start, u64 block_start, |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 7278 | u64 block_len, u64 orig_block_len, |
| 7279 | u64 ram_bytes, int compress_type, |
| 7280 | int type) |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7281 | { |
| 7282 | struct extent_map_tree *em_tree; |
| 7283 | struct extent_map *em; |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7284 | int ret; |
| 7285 | |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 7286 | ASSERT(type == BTRFS_ORDERED_PREALLOC || |
| 7287 | type == BTRFS_ORDERED_COMPRESSED || |
| 7288 | type == BTRFS_ORDERED_NOCOW || |
Liu Bo | 1af4a0a | 2017-02-13 15:35:09 -0800 | [diff] [blame] | 7289 | type == BTRFS_ORDERED_REGULAR); |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 7290 | |
Nikolay Borisov | 4b67c11 | 2020-06-03 08:55:05 +0300 | [diff] [blame] | 7291 | em_tree = &inode->extent_tree; |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7292 | em = alloc_extent_map(); |
| 7293 | if (!em) |
| 7294 | return ERR_PTR(-ENOMEM); |
| 7295 | |
| 7296 | em->start = start; |
| 7297 | em->orig_start = orig_start; |
| 7298 | em->len = len; |
| 7299 | em->block_len = block_len; |
| 7300 | em->block_start = block_start; |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 7301 | em->orig_block_len = orig_block_len; |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 7302 | em->ram_bytes = ram_bytes; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 7303 | em->generation = -1; |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7304 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
Liu Bo | 1af4a0a | 2017-02-13 15:35:09 -0800 | [diff] [blame] | 7305 | if (type == BTRFS_ORDERED_PREALLOC) { |
Josef Bacik | b11e234 | 2012-12-03 10:58:15 -0500 | [diff] [blame] | 7306 | set_bit(EXTENT_FLAG_FILLING, &em->flags); |
Liu Bo | 1af4a0a | 2017-02-13 15:35:09 -0800 | [diff] [blame] | 7307 | } else if (type == BTRFS_ORDERED_COMPRESSED) { |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 7308 | set_bit(EXTENT_FLAG_COMPRESSED, &em->flags); |
| 7309 | em->compress_type = compress_type; |
| 7310 | } |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7311 | |
| 7312 | do { |
Nikolay Borisov | 4b67c11 | 2020-06-03 08:55:05 +0300 | [diff] [blame] | 7313 | btrfs_drop_extent_cache(inode, em->start, |
| 7314 | em->start + em->len - 1, 0); |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7315 | write_lock(&em_tree->lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 7316 | ret = add_extent_mapping(em_tree, em, 1); |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7317 | write_unlock(&em_tree->lock); |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 7318 | /* |
| 7319 | * The caller has taken lock_extent(), who could race with us |
| 7320 | * to add em? |
| 7321 | */ |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7322 | } while (ret == -EEXIST); |
| 7323 | |
| 7324 | if (ret) { |
| 7325 | free_extent_map(em); |
| 7326 | return ERR_PTR(ret); |
| 7327 | } |
| 7328 | |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 7329 | /* 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] | 7330 | return em; |
| 7331 | } |
| 7332 | |
Nikolay Borisov | 1c8d017 | 2018-05-02 15:19:32 +0300 | [diff] [blame] | 7333 | |
Nikolay Borisov | c5794e5 | 2018-05-02 15:19:33 +0300 | [diff] [blame] | 7334 | static int btrfs_get_blocks_direct_write(struct extent_map **map, |
Nikolay Borisov | c5794e5 | 2018-05-02 15:19:33 +0300 | [diff] [blame] | 7335 | struct inode *inode, |
| 7336 | struct btrfs_dio_data *dio_data, |
| 7337 | u64 start, u64 len) |
| 7338 | { |
| 7339 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
| 7340 | struct extent_map *em = *map; |
| 7341 | int ret = 0; |
| 7342 | |
| 7343 | /* |
| 7344 | * We don't allocate a new extent in the following cases |
| 7345 | * |
| 7346 | * 1) The inode is marked as NODATACOW. In this case we'll just use the |
| 7347 | * existing extent. |
| 7348 | * 2) The extent is marked as PREALLOC. We're good to go here and can |
| 7349 | * just use the extent. |
| 7350 | * |
| 7351 | */ |
| 7352 | if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) || |
| 7353 | ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) && |
| 7354 | em->block_start != EXTENT_MAP_HOLE)) { |
| 7355 | int type; |
| 7356 | u64 block_start, orig_start, orig_block_len, ram_bytes; |
| 7357 | |
| 7358 | if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) |
| 7359 | type = BTRFS_ORDERED_PREALLOC; |
| 7360 | else |
| 7361 | type = BTRFS_ORDERED_NOCOW; |
| 7362 | len = min(len, em->len - (start - em->start)); |
| 7363 | block_start = em->block_start + (start - em->start); |
| 7364 | |
| 7365 | if (can_nocow_extent(inode, start, &len, &orig_start, |
Boris Burkov | a84d5d4 | 2020-08-18 11:00:05 -0700 | [diff] [blame] | 7366 | &orig_block_len, &ram_bytes, false) == 1 && |
Nikolay Borisov | c5794e5 | 2018-05-02 15:19:33 +0300 | [diff] [blame] | 7367 | btrfs_inc_nocow_writers(fs_info, block_start)) { |
| 7368 | struct extent_map *em2; |
| 7369 | |
Nikolay Borisov | 64f5418 | 2020-06-03 08:55:31 +0300 | [diff] [blame] | 7370 | em2 = btrfs_create_dio_extent(BTRFS_I(inode), start, len, |
Nikolay Borisov | c5794e5 | 2018-05-02 15:19:33 +0300 | [diff] [blame] | 7371 | orig_start, block_start, |
| 7372 | len, orig_block_len, |
| 7373 | ram_bytes, type); |
| 7374 | btrfs_dec_nocow_writers(fs_info, block_start); |
| 7375 | if (type == BTRFS_ORDERED_PREALLOC) { |
| 7376 | free_extent_map(em); |
| 7377 | *map = em = em2; |
| 7378 | } |
| 7379 | |
| 7380 | if (em2 && IS_ERR(em2)) { |
| 7381 | ret = PTR_ERR(em2); |
| 7382 | goto out; |
| 7383 | } |
| 7384 | /* |
| 7385 | * For inode marked NODATACOW or extent marked PREALLOC, |
| 7386 | * use the existing or preallocated extent, so does not |
| 7387 | * need to adjust btrfs_space_info's bytes_may_use. |
| 7388 | */ |
Nikolay Borisov | 9db5d51 | 2020-06-03 08:55:38 +0300 | [diff] [blame] | 7389 | btrfs_free_reserved_data_space_noquota(fs_info, len); |
Nikolay Borisov | c5794e5 | 2018-05-02 15:19:33 +0300 | [diff] [blame] | 7390 | goto skip_cow; |
| 7391 | } |
| 7392 | } |
| 7393 | |
| 7394 | /* this will cow the extent */ |
Nikolay Borisov | c5794e5 | 2018-05-02 15:19:33 +0300 | [diff] [blame] | 7395 | free_extent_map(em); |
Nikolay Borisov | 9fc6f91 | 2020-06-03 08:55:32 +0300 | [diff] [blame] | 7396 | *map = em = btrfs_new_extent_direct(BTRFS_I(inode), start, len); |
Nikolay Borisov | c5794e5 | 2018-05-02 15:19:33 +0300 | [diff] [blame] | 7397 | if (IS_ERR(em)) { |
| 7398 | ret = PTR_ERR(em); |
| 7399 | goto out; |
| 7400 | } |
| 7401 | |
| 7402 | len = min(len, em->len - (start - em->start)); |
| 7403 | |
| 7404 | skip_cow: |
Nikolay Borisov | c5794e5 | 2018-05-02 15:19:33 +0300 | [diff] [blame] | 7405 | /* |
| 7406 | * Need to update the i_size under the extent lock so buffered |
| 7407 | * readers will get the updated i_size when we unlock. |
| 7408 | */ |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 7409 | if (start + len > i_size_read(inode)) |
Nikolay Borisov | c5794e5 | 2018-05-02 15:19:33 +0300 | [diff] [blame] | 7410 | i_size_write(inode, start + len); |
| 7411 | |
Nikolay Borisov | c5794e5 | 2018-05-02 15:19:33 +0300 | [diff] [blame] | 7412 | dio_data->reserve -= len; |
Nikolay Borisov | c5794e5 | 2018-05-02 15:19:33 +0300 | [diff] [blame] | 7413 | out: |
| 7414 | return ret; |
| 7415 | } |
| 7416 | |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 7417 | static int btrfs_dio_iomap_begin(struct inode *inode, loff_t start, |
| 7418 | loff_t length, unsigned int flags, struct iomap *iomap, |
| 7419 | struct iomap *srcmap) |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7420 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7421 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7422 | struct extent_map *em; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7423 | struct extent_state *cached_state = NULL; |
chandan | 50745b0 | 2015-08-28 21:10:13 +0530 | [diff] [blame] | 7424 | struct btrfs_dio_data *dio_data = NULL; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7425 | u64 lockstart, lockend; |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 7426 | const bool write = !!(flags & IOMAP_WRITE); |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 7427 | int ret = 0; |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 7428 | u64 len = length; |
| 7429 | bool unlock_extents = false; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7430 | |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 7431 | if (!write) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7432 | len = min_t(u64, len, fs_info->sectorsize); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7433 | |
Josef Bacik | c329861 | 2012-08-03 16:49:19 -0400 | [diff] [blame] | 7434 | lockstart = start; |
| 7435 | lockend = start + len - 1; |
| 7436 | |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 7437 | /* |
| 7438 | * The generic stuff only does filemap_write_and_wait_range, which |
| 7439 | * isn't enough if we've written compressed pages to this area, so we |
| 7440 | * need to flush the dirty pages again to make absolutely sure that any |
| 7441 | * outstanding dirty pages are on disk. |
| 7442 | */ |
| 7443 | if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, |
| 7444 | &BTRFS_I(inode)->runtime_flags)) { |
| 7445 | ret = filemap_fdatawrite_range(inode->i_mapping, start, |
| 7446 | start + length - 1); |
| 7447 | if (ret) |
| 7448 | return ret; |
Josef Bacik | e1cbbfa | 2015-03-17 10:52:28 -0400 | [diff] [blame] | 7449 | } |
| 7450 | |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 7451 | dio_data = kzalloc(sizeof(*dio_data), GFP_NOFS); |
| 7452 | if (!dio_data) |
| 7453 | return -ENOMEM; |
| 7454 | |
| 7455 | dio_data->length = length; |
| 7456 | if (write) { |
| 7457 | dio_data->reserve = round_up(length, fs_info->sectorsize); |
| 7458 | ret = btrfs_delalloc_reserve_space(BTRFS_I(inode), |
| 7459 | &dio_data->data_reserved, |
| 7460 | start, dio_data->reserve); |
| 7461 | if (ret) { |
| 7462 | extent_changeset_free(dio_data->data_reserved); |
| 7463 | kfree(dio_data); |
| 7464 | return ret; |
| 7465 | } |
| 7466 | } |
| 7467 | iomap->private = dio_data; |
| 7468 | |
| 7469 | |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7470 | /* |
| 7471 | * If this errors out it's because we couldn't invalidate pagecache for |
| 7472 | * this range and we need to fallback to buffered. |
| 7473 | */ |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 7474 | if (lock_extent_direct(inode, lockstart, lockend, &cached_state, write)) { |
Filipe Manana | 9c9464c | 2015-11-04 09:52:04 +0000 | [diff] [blame] | 7475 | ret = -ENOTBLK; |
| 7476 | goto err; |
| 7477 | } |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7478 | |
Omar Sandoval | 39b07b5 | 2019-12-02 17:34:23 -0800 | [diff] [blame] | 7479 | em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7480 | if (IS_ERR(em)) { |
| 7481 | ret = PTR_ERR(em); |
| 7482 | goto unlock_err; |
| 7483 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7484 | |
| 7485 | /* |
| 7486 | * Ok for INLINE and COMPRESSED extents we need to fallback on buffered |
| 7487 | * io. INLINE is special, and we could probably kludge it in here, but |
| 7488 | * it's still buffered so for safety lets just fall back to the generic |
| 7489 | * buffered path. |
| 7490 | * |
| 7491 | * For COMPRESSED we _have_ to read the entire extent in so we can |
| 7492 | * decompress it, so there will be buffering required no matter what we |
| 7493 | * do, so go ahead and fallback to buffered. |
| 7494 | * |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 7495 | * 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] | 7496 | * to buffered IO. Don't blame me, this is the price we pay for using |
| 7497 | * the generic code. |
| 7498 | */ |
| 7499 | if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) || |
| 7500 | em->block_start == EXTENT_MAP_INLINE) { |
| 7501 | free_extent_map(em); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7502 | ret = -ENOTBLK; |
| 7503 | goto unlock_err; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7504 | } |
| 7505 | |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 7506 | len = min(len, em->len - (start - em->start)); |
| 7507 | if (write) { |
| 7508 | ret = btrfs_get_blocks_direct_write(&em, inode, dio_data, |
| 7509 | start, len); |
Nikolay Borisov | c5794e5 | 2018-05-02 15:19:33 +0300 | [diff] [blame] | 7510 | if (ret < 0) |
| 7511 | goto unlock_err; |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 7512 | unlock_extents = true; |
| 7513 | /* Recalc len in case the new em is smaller than requested */ |
| 7514 | len = min(len, em->len - (start - em->start)); |
Nikolay Borisov | c5794e5 | 2018-05-02 15:19:33 +0300 | [diff] [blame] | 7515 | } else { |
Nikolay Borisov | 1c8d017 | 2018-05-02 15:19:32 +0300 | [diff] [blame] | 7516 | /* |
| 7517 | * We need to unlock only the end area that we aren't using. |
| 7518 | * The rest is going to be unlocked by the endio routine. |
| 7519 | */ |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 7520 | lockstart = start + len; |
| 7521 | if (lockstart < lockend) |
| 7522 | unlock_extents = true; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7523 | } |
| 7524 | |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 7525 | if (unlock_extents) |
| 7526 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, |
| 7527 | lockstart, lockend, &cached_state); |
| 7528 | else |
| 7529 | free_extent_state(cached_state); |
| 7530 | |
| 7531 | /* |
| 7532 | * Translate extent map information to iomap. |
| 7533 | * We trim the extents (and move the addr) even though iomap code does |
| 7534 | * that, since we have locked only the parts we are performing I/O in. |
| 7535 | */ |
| 7536 | if ((em->block_start == EXTENT_MAP_HOLE) || |
| 7537 | (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) && !write)) { |
| 7538 | iomap->addr = IOMAP_NULL_ADDR; |
| 7539 | iomap->type = IOMAP_HOLE; |
| 7540 | } else { |
| 7541 | iomap->addr = em->block_start + (start - em->start); |
| 7542 | iomap->type = IOMAP_MAPPED; |
| 7543 | } |
| 7544 | iomap->offset = start; |
| 7545 | iomap->bdev = fs_info->fs_devices->latest_bdev; |
| 7546 | iomap->length = len; |
| 7547 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7548 | free_extent_map(em); |
| 7549 | |
| 7550 | return 0; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7551 | |
| 7552 | unlock_err: |
Omar Sandoval | e182163 | 2019-08-15 14:04:04 -0700 | [diff] [blame] | 7553 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend, |
| 7554 | &cached_state); |
Filipe Manana | 9c9464c | 2015-11-04 09:52:04 +0000 | [diff] [blame] | 7555 | err: |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 7556 | if (dio_data) { |
| 7557 | btrfs_delalloc_release_space(BTRFS_I(inode), |
| 7558 | dio_data->data_reserved, start, |
| 7559 | dio_data->reserve, true); |
| 7560 | btrfs_delalloc_release_extents(BTRFS_I(inode), dio_data->reserve); |
| 7561 | extent_changeset_free(dio_data->data_reserved); |
| 7562 | kfree(dio_data); |
| 7563 | } |
| 7564 | return ret; |
| 7565 | } |
| 7566 | |
| 7567 | static int btrfs_dio_iomap_end(struct inode *inode, loff_t pos, loff_t length, |
| 7568 | ssize_t written, unsigned int flags, struct iomap *iomap) |
| 7569 | { |
| 7570 | int ret = 0; |
| 7571 | struct btrfs_dio_data *dio_data = iomap->private; |
| 7572 | size_t submitted = dio_data->submitted; |
| 7573 | const bool write = !!(flags & IOMAP_WRITE); |
| 7574 | |
| 7575 | if (!write && (iomap->type == IOMAP_HOLE)) { |
| 7576 | /* If reading from a hole, unlock and return */ |
| 7577 | unlock_extent(&BTRFS_I(inode)->io_tree, pos, pos + length - 1); |
| 7578 | goto out; |
| 7579 | } |
| 7580 | |
| 7581 | if (submitted < length) { |
| 7582 | pos += submitted; |
| 7583 | length -= submitted; |
| 7584 | if (write) |
| 7585 | __endio_write_update_ordered(BTRFS_I(inode), pos, |
| 7586 | length, false); |
| 7587 | else |
| 7588 | unlock_extent(&BTRFS_I(inode)->io_tree, pos, |
| 7589 | pos + length - 1); |
| 7590 | ret = -ENOTBLK; |
| 7591 | } |
| 7592 | |
| 7593 | if (write) { |
| 7594 | if (dio_data->reserve) |
| 7595 | btrfs_delalloc_release_space(BTRFS_I(inode), |
| 7596 | dio_data->data_reserved, pos, |
| 7597 | dio_data->reserve, true); |
| 7598 | btrfs_delalloc_release_extents(BTRFS_I(inode), dio_data->length); |
| 7599 | extent_changeset_free(dio_data->data_reserved); |
| 7600 | } |
| 7601 | out: |
| 7602 | kfree(dio_data); |
| 7603 | iomap->private = NULL; |
| 7604 | |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7605 | return ret; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7606 | } |
| 7607 | |
Omar Sandoval | 769b4f2 | 2020-04-16 14:46:22 -0700 | [diff] [blame] | 7608 | static void btrfs_dio_private_put(struct btrfs_dio_private *dip) |
| 7609 | { |
| 7610 | /* |
| 7611 | * This implies a barrier so that stores to dio_bio->bi_status before |
| 7612 | * this and loads of dio_bio->bi_status after this are fully ordered. |
| 7613 | */ |
| 7614 | if (!refcount_dec_and_test(&dip->refs)) |
| 7615 | return; |
| 7616 | |
| 7617 | if (bio_op(dip->dio_bio) == REQ_OP_WRITE) { |
Nikolay Borisov | b672b5c | 2020-06-03 08:55:24 +0300 | [diff] [blame] | 7618 | __endio_write_update_ordered(BTRFS_I(dip->inode), |
| 7619 | dip->logical_offset, |
Omar Sandoval | 769b4f2 | 2020-04-16 14:46:22 -0700 | [diff] [blame] | 7620 | dip->bytes, |
| 7621 | !dip->dio_bio->bi_status); |
| 7622 | } else { |
| 7623 | unlock_extent(&BTRFS_I(dip->inode)->io_tree, |
| 7624 | dip->logical_offset, |
| 7625 | dip->logical_offset + dip->bytes - 1); |
| 7626 | } |
| 7627 | |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 7628 | bio_endio(dip->dio_bio); |
Omar Sandoval | 769b4f2 | 2020-04-16 14:46:22 -0700 | [diff] [blame] | 7629 | kfree(dip); |
| 7630 | } |
| 7631 | |
Omar Sandoval | 77d5d68 | 2020-04-16 14:46:25 -0700 | [diff] [blame] | 7632 | static blk_status_t submit_dio_repair_bio(struct inode *inode, struct bio *bio, |
| 7633 | int mirror_num, |
| 7634 | unsigned long bio_flags) |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7635 | { |
Omar Sandoval | 77d5d68 | 2020-04-16 14:46:25 -0700 | [diff] [blame] | 7636 | struct btrfs_dio_private *dip = bio->bi_private; |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 7637 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Omar Sandoval | 58efbc9 | 2017-08-22 23:45:59 -0700 | [diff] [blame] | 7638 | blk_status_t ret; |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7639 | |
Mike Christie | 37226b2 | 2016-06-05 14:31:52 -0500 | [diff] [blame] | 7640 | BUG_ON(bio_op(bio) == REQ_OP_WRITE); |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7641 | |
Omar Sandoval | 5c047a6 | 2020-04-16 14:46:24 -0700 | [diff] [blame] | 7642 | 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] | 7643 | if (ret) |
Nikolay Borisov | ea057f6 | 2017-12-13 10:25:38 +0200 | [diff] [blame] | 7644 | return ret; |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7645 | |
Omar Sandoval | fd9d667 | 2020-04-16 14:46:23 -0700 | [diff] [blame] | 7646 | refcount_inc(&dip->refs); |
Chris Mason | 08635ba | 2019-07-10 12:28:14 -0700 | [diff] [blame] | 7647 | ret = btrfs_map_bio(fs_info, bio, mirror_num); |
Omar Sandoval | 77d5d68 | 2020-04-16 14:46:25 -0700 | [diff] [blame] | 7648 | if (ret) |
Omar Sandoval | fd9d667 | 2020-04-16 14:46:23 -0700 | [diff] [blame] | 7649 | refcount_dec(&dip->refs); |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7650 | return ret; |
| 7651 | } |
| 7652 | |
Omar Sandoval | 769b4f2 | 2020-04-16 14:46:22 -0700 | [diff] [blame] | 7653 | static blk_status_t btrfs_check_read_dio_bio(struct inode *inode, |
| 7654 | struct btrfs_io_bio *io_bio, |
Omar Sandoval | fd9d667 | 2020-04-16 14:46:23 -0700 | [diff] [blame] | 7655 | const bool uptodate) |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7656 | { |
Omar Sandoval | fd9d667 | 2020-04-16 14:46:23 -0700 | [diff] [blame] | 7657 | struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info; |
| 7658 | const u32 sectorsize = fs_info->sectorsize; |
Josef Bacik | 7870d08 | 2017-05-05 11:57:15 -0400 | [diff] [blame] | 7659 | struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree; |
Omar Sandoval | fd9d667 | 2020-04-16 14:46:23 -0700 | [diff] [blame] | 7660 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
| 7661 | const bool csum = !(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM); |
Liu Bo | 17347ce | 2017-05-15 15:33:27 -0700 | [diff] [blame] | 7662 | struct bio_vec bvec; |
| 7663 | struct bvec_iter iter; |
Omar Sandoval | fd9d667 | 2020-04-16 14:46:23 -0700 | [diff] [blame] | 7664 | u64 start = io_bio->logical; |
| 7665 | int icsum = 0; |
Omar Sandoval | 58efbc9 | 2017-08-22 23:45:59 -0700 | [diff] [blame] | 7666 | blk_status_t err = BLK_STS_OK; |
Miao Xie | dc380ae | 2014-09-12 18:43:55 +0800 | [diff] [blame] | 7667 | |
Omar Sandoval | fd9d667 | 2020-04-16 14:46:23 -0700 | [diff] [blame] | 7668 | __bio_for_each_segment(bvec, &io_bio->bio, iter, io_bio->iter) { |
| 7669 | unsigned int i, nr_sectors, pgoff; |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 7670 | |
Liu Bo | 17347ce | 2017-05-15 15:33:27 -0700 | [diff] [blame] | 7671 | nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec.bv_len); |
| 7672 | pgoff = bvec.bv_offset; |
Omar Sandoval | fd9d667 | 2020-04-16 14:46:23 -0700 | [diff] [blame] | 7673 | for (i = 0; i < nr_sectors; i++) { |
Liu Bo | 97bf5a5 | 2017-04-07 13:11:10 -0700 | [diff] [blame] | 7674 | ASSERT(pgoff < PAGE_SIZE); |
Omar Sandoval | fd9d667 | 2020-04-16 14:46:23 -0700 | [diff] [blame] | 7675 | if (uptodate && |
| 7676 | (!csum || !check_data_csum(inode, io_bio, icsum, |
| 7677 | bvec.bv_page, pgoff, |
| 7678 | start, sectorsize))) { |
| 7679 | clean_io_failure(fs_info, failure_tree, io_tree, |
| 7680 | start, bvec.bv_page, |
| 7681 | btrfs_ino(BTRFS_I(inode)), |
| 7682 | pgoff); |
| 7683 | } else { |
| 7684 | blk_status_t status; |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7685 | |
Omar Sandoval | 77d5d68 | 2020-04-16 14:46:25 -0700 | [diff] [blame] | 7686 | status = btrfs_submit_read_repair(inode, |
| 7687 | &io_bio->bio, |
| 7688 | start - io_bio->logical, |
Omar Sandoval | fd9d667 | 2020-04-16 14:46:23 -0700 | [diff] [blame] | 7689 | bvec.bv_page, pgoff, |
| 7690 | start, |
| 7691 | start + sectorsize - 1, |
Omar Sandoval | 77d5d68 | 2020-04-16 14:46:25 -0700 | [diff] [blame] | 7692 | io_bio->mirror_num, |
| 7693 | submit_dio_repair_bio); |
Omar Sandoval | fd9d667 | 2020-04-16 14:46:23 -0700 | [diff] [blame] | 7694 | if (status) |
| 7695 | err = status; |
| 7696 | } |
| 7697 | start += sectorsize; |
| 7698 | icsum++; |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 7699 | pgoff += sectorsize; |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 7700 | } |
Kent Overstreet | 2c30c71 | 2013-11-07 12:20:26 -0800 | [diff] [blame] | 7701 | } |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 7702 | return err; |
| 7703 | } |
| 7704 | |
Nikolay Borisov | b672b5c | 2020-06-03 08:55:24 +0300 | [diff] [blame] | 7705 | static void __endio_write_update_ordered(struct btrfs_inode *inode, |
Qu Wenruo | 52427260 | 2017-03-08 10:25:52 +0800 | [diff] [blame] | 7706 | const u64 offset, const u64 bytes, |
| 7707 | const bool uptodate) |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7708 | { |
Nikolay Borisov | b672b5c | 2020-06-03 08:55:24 +0300 | [diff] [blame] | 7709 | struct btrfs_fs_info *fs_info = inode->root->fs_info; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7710 | struct btrfs_ordered_extent *ordered = NULL; |
Qu Wenruo | 52427260 | 2017-03-08 10:25:52 +0800 | [diff] [blame] | 7711 | struct btrfs_workqueue *wq; |
Filipe Manana | 1454377 | 2015-11-24 16:23:54 +0000 | [diff] [blame] | 7712 | u64 ordered_offset = offset; |
| 7713 | u64 ordered_bytes = bytes; |
Naohiro Aota | 67c003f | 2017-09-01 17:59:07 +0900 | [diff] [blame] | 7714 | u64 last_offset; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7715 | |
Nikolay Borisov | b672b5c | 2020-06-03 08:55:24 +0300 | [diff] [blame] | 7716 | if (btrfs_is_free_space_inode(inode)) |
Qu Wenruo | 52427260 | 2017-03-08 10:25:52 +0800 | [diff] [blame] | 7717 | wq = fs_info->endio_freespace_worker; |
Omar Sandoval | a0cac0e | 2019-09-16 11:30:57 -0700 | [diff] [blame] | 7718 | else |
Qu Wenruo | 52427260 | 2017-03-08 10:25:52 +0800 | [diff] [blame] | 7719 | wq = fs_info->endio_write_workers; |
Qu Wenruo | 52427260 | 2017-03-08 10:25:52 +0800 | [diff] [blame] | 7720 | |
Nikolay Borisov | b25f0d0 | 2018-04-11 11:21:17 +0300 | [diff] [blame] | 7721 | while (ordered_offset < offset + bytes) { |
| 7722 | last_offset = ordered_offset; |
Nikolay Borisov | b672b5c | 2020-06-03 08:55:24 +0300 | [diff] [blame] | 7723 | if (btrfs_dec_test_first_ordered_pending(inode, &ordered, |
Nikolay Borisov | 7095821 | 2020-06-03 08:55:23 +0300 | [diff] [blame] | 7724 | &ordered_offset, |
| 7725 | ordered_bytes, |
| 7726 | uptodate)) { |
Omar Sandoval | a0cac0e | 2019-09-16 11:30:57 -0700 | [diff] [blame] | 7727 | btrfs_init_work(&ordered->work, finish_ordered_fn, NULL, |
| 7728 | NULL); |
Nikolay Borisov | b25f0d0 | 2018-04-11 11:21:17 +0300 | [diff] [blame] | 7729 | btrfs_queue_work(wq, &ordered->work); |
| 7730 | } |
| 7731 | /* |
| 7732 | * If btrfs_dec_test_ordered_pending does not find any ordered |
| 7733 | * extent in the range, we can exit. |
| 7734 | */ |
| 7735 | if (ordered_offset == last_offset) |
| 7736 | return; |
| 7737 | /* |
| 7738 | * Our bio might span multiple ordered extents. In this case |
Andrea Gelmini | 52042d8 | 2018-11-28 12:05:13 +0100 | [diff] [blame] | 7739 | * we keep going until we have accounted the whole dio. |
Nikolay Borisov | b25f0d0 | 2018-04-11 11:21:17 +0300 | [diff] [blame] | 7740 | */ |
| 7741 | if (ordered_offset < offset + bytes) { |
| 7742 | ordered_bytes = offset + bytes - ordered_offset; |
| 7743 | ordered = NULL; |
| 7744 | } |
Chris Mason | 163cf09 | 2010-11-28 19:56:33 -0500 | [diff] [blame] | 7745 | } |
Filipe Manana | 1454377 | 2015-11-24 16:23:54 +0000 | [diff] [blame] | 7746 | } |
| 7747 | |
Qu Wenruo | 8896a08 | 2020-10-21 14:24:53 +0800 | [diff] [blame^] | 7748 | static blk_status_t btrfs_submit_bio_start_direct_io(struct inode *inode, |
| 7749 | struct bio *bio, u64 offset) |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 7750 | { |
Johannes Thumshirn | c965d64 | 2020-07-28 20:25:41 +0900 | [diff] [blame] | 7751 | return btrfs_csum_one_bio(BTRFS_I(inode), bio, offset, 1); |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 7752 | } |
| 7753 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 7754 | static void btrfs_end_dio_bio(struct bio *bio) |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7755 | { |
| 7756 | struct btrfs_dio_private *dip = bio->bi_private; |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 7757 | blk_status_t err = bio->bi_status; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7758 | |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7759 | if (err) |
| 7760 | btrfs_warn(BTRFS_I(dip->inode)->root->fs_info, |
Mike Christie | 6296b96 | 2016-06-05 14:32:21 -0500 | [diff] [blame] | 7761 | "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] | 7762 | btrfs_ino(BTRFS_I(dip->inode)), bio_op(bio), |
| 7763 | bio->bi_opf, |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7764 | (unsigned long long)bio->bi_iter.bi_sector, |
| 7765 | bio->bi_iter.bi_size, err); |
| 7766 | |
Omar Sandoval | 769b4f2 | 2020-04-16 14:46:22 -0700 | [diff] [blame] | 7767 | if (bio_op(bio) == REQ_OP_READ) { |
| 7768 | err = btrfs_check_read_dio_bio(dip->inode, btrfs_io_bio(bio), |
Omar Sandoval | fd9d667 | 2020-04-16 14:46:23 -0700 | [diff] [blame] | 7769 | !err); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7770 | } |
| 7771 | |
Omar Sandoval | 769b4f2 | 2020-04-16 14:46:22 -0700 | [diff] [blame] | 7772 | if (err) |
| 7773 | dip->dio_bio->bi_status = err; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7774 | |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7775 | bio_put(bio); |
Omar Sandoval | 769b4f2 | 2020-04-16 14:46:22 -0700 | [diff] [blame] | 7776 | btrfs_dio_private_put(dip); |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 7777 | } |
| 7778 | |
David Sterba | d0ee393 | 2018-03-08 14:35:48 +0100 | [diff] [blame] | 7779 | static inline blk_status_t btrfs_submit_dio_bio(struct bio *bio, |
| 7780 | struct inode *inode, u64 file_offset, int async_submit) |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7781 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7782 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 7783 | struct btrfs_dio_private *dip = bio->bi_private; |
Mike Christie | 37226b2 | 2016-06-05 14:31:52 -0500 | [diff] [blame] | 7784 | bool write = bio_op(bio) == REQ_OP_WRITE; |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 7785 | blk_status_t ret; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7786 | |
Liu Bo | 4c274bc | 2017-11-01 17:19:27 -0600 | [diff] [blame] | 7787 | /* Check btrfs_submit_bio_hook() for rules about async submit. */ |
Josef Bacik | b812ce2 | 2012-11-16 13:56:32 -0500 | [diff] [blame] | 7788 | if (async_submit) |
| 7789 | async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers); |
| 7790 | |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 7791 | if (!write) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7792 | 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] | 7793 | if (ret) |
| 7794 | goto err; |
| 7795 | } |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7796 | |
Nikolay Borisov | e6961ca | 2017-08-03 15:44:58 +0300 | [diff] [blame] | 7797 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM) |
Josef Bacik | 1ae3993 | 2011-04-06 14:41:34 -0400 | [diff] [blame] | 7798 | goto map; |
| 7799 | |
| 7800 | if (write && async_submit) { |
Qu Wenruo | 8896a08 | 2020-10-21 14:24:53 +0800 | [diff] [blame^] | 7801 | ret = btrfs_wq_submit_bio(inode, bio, 0, 0, |
| 7802 | file_offset, |
David Sterba | e288c08 | 2018-07-18 17:36:24 +0200 | [diff] [blame] | 7803 | btrfs_submit_bio_start_direct_io); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7804 | goto err; |
Josef Bacik | 1ae3993 | 2011-04-06 14:41:34 -0400 | [diff] [blame] | 7805 | } else if (write) { |
| 7806 | /* |
| 7807 | * If we aren't doing async submit, calculate the csum of the |
| 7808 | * bio now. |
| 7809 | */ |
Nikolay Borisov | bd242a0 | 2020-06-03 08:55:07 +0300 | [diff] [blame] | 7810 | ret = btrfs_csum_one_bio(BTRFS_I(inode), bio, file_offset, 1); |
Josef Bacik | 1ae3993 | 2011-04-06 14:41:34 -0400 | [diff] [blame] | 7811 | if (ret) |
| 7812 | goto err; |
Miao Xie | 23ea8e5 | 2014-09-12 18:43:54 +0800 | [diff] [blame] | 7813 | } else { |
Omar Sandoval | 8587957 | 2020-04-16 14:46:21 -0700 | [diff] [blame] | 7814 | u64 csum_offset; |
| 7815 | |
| 7816 | csum_offset = file_offset - dip->logical_offset; |
| 7817 | csum_offset >>= inode->i_sb->s_blocksize_bits; |
| 7818 | csum_offset *= btrfs_super_csum_size(fs_info->super_copy); |
| 7819 | btrfs_io_bio(bio)->csum = dip->csums + csum_offset; |
Tsutomu Itoh | c2db107 | 2011-03-01 06:48:31 +0000 | [diff] [blame] | 7820 | } |
Josef Bacik | 1ae3993 | 2011-04-06 14:41:34 -0400 | [diff] [blame] | 7821 | map: |
Chris Mason | 08635ba | 2019-07-10 12:28:14 -0700 | [diff] [blame] | 7822 | ret = btrfs_map_bio(fs_info, bio, 0); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7823 | err: |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7824 | return ret; |
| 7825 | } |
| 7826 | |
Omar Sandoval | c36cac2 | 2020-04-16 14:46:13 -0700 | [diff] [blame] | 7827 | /* |
| 7828 | * If this succeeds, the btrfs_dio_private is responsible for cleaning up locked |
| 7829 | * or ordered extents whether or not we submit any bios. |
| 7830 | */ |
| 7831 | static struct btrfs_dio_private *btrfs_create_dio_private(struct bio *dio_bio, |
| 7832 | struct inode *inode, |
| 7833 | loff_t file_offset) |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7834 | { |
Omar Sandoval | c36cac2 | 2020-04-16 14:46:13 -0700 | [diff] [blame] | 7835 | const bool write = (bio_op(dio_bio) == REQ_OP_WRITE); |
Omar Sandoval | 8587957 | 2020-04-16 14:46:21 -0700 | [diff] [blame] | 7836 | const bool csum = !(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM); |
| 7837 | size_t dip_size; |
Omar Sandoval | c36cac2 | 2020-04-16 14:46:13 -0700 | [diff] [blame] | 7838 | struct btrfs_dio_private *dip; |
Omar Sandoval | c36cac2 | 2020-04-16 14:46:13 -0700 | [diff] [blame] | 7839 | |
Omar Sandoval | 8587957 | 2020-04-16 14:46:21 -0700 | [diff] [blame] | 7840 | dip_size = sizeof(*dip); |
| 7841 | if (!write && csum) { |
| 7842 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
| 7843 | const u16 csum_size = btrfs_super_csum_size(fs_info->super_copy); |
| 7844 | size_t nblocks; |
| 7845 | |
| 7846 | nblocks = dio_bio->bi_iter.bi_size >> inode->i_sb->s_blocksize_bits; |
| 7847 | dip_size += csum_size * nblocks; |
| 7848 | } |
| 7849 | |
| 7850 | dip = kzalloc(dip_size, GFP_NOFS); |
Omar Sandoval | c36cac2 | 2020-04-16 14:46:13 -0700 | [diff] [blame] | 7851 | if (!dip) |
| 7852 | return NULL; |
| 7853 | |
Omar Sandoval | c36cac2 | 2020-04-16 14:46:13 -0700 | [diff] [blame] | 7854 | dip->inode = inode; |
| 7855 | dip->logical_offset = file_offset; |
| 7856 | dip->bytes = dio_bio->bi_iter.bi_size; |
| 7857 | dip->disk_bytenr = (u64)dio_bio->bi_iter.bi_sector << 9; |
Omar Sandoval | c36cac2 | 2020-04-16 14:46:13 -0700 | [diff] [blame] | 7858 | dip->dio_bio = dio_bio; |
Omar Sandoval | e3b318d | 2020-04-16 14:46:20 -0700 | [diff] [blame] | 7859 | refcount_set(&dip->refs, 1); |
Omar Sandoval | c36cac2 | 2020-04-16 14:46:13 -0700 | [diff] [blame] | 7860 | return dip; |
| 7861 | } |
| 7862 | |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 7863 | static blk_qc_t btrfs_submit_direct(struct inode *inode, struct iomap *iomap, |
| 7864 | struct bio *dio_bio, loff_t file_offset) |
Omar Sandoval | c36cac2 | 2020-04-16 14:46:13 -0700 | [diff] [blame] | 7865 | { |
| 7866 | const bool write = (bio_op(dio_bio) == REQ_OP_WRITE); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7867 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Omar Sandoval | 769b4f2 | 2020-04-16 14:46:22 -0700 | [diff] [blame] | 7868 | const bool raid56 = (btrfs_data_alloc_profile(fs_info) & |
| 7869 | BTRFS_BLOCK_GROUP_RAID56_MASK); |
Omar Sandoval | c36cac2 | 2020-04-16 14:46:13 -0700 | [diff] [blame] | 7870 | struct btrfs_dio_private *dip; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7871 | struct bio *bio; |
Omar Sandoval | c36cac2 | 2020-04-16 14:46:13 -0700 | [diff] [blame] | 7872 | u64 start_sector; |
Josef Bacik | 1ae3993 | 2011-04-06 14:41:34 -0400 | [diff] [blame] | 7873 | int async_submit = 0; |
Liu Bo | 725130b | 2017-05-16 09:51:39 -0700 | [diff] [blame] | 7874 | u64 submit_len; |
| 7875 | int clone_offset = 0; |
| 7876 | int clone_len; |
Chandan Rajendra | 5f4dc8f | 2016-01-21 15:56:00 +0530 | [diff] [blame] | 7877 | int ret; |
Omar Sandoval | 58efbc9 | 2017-08-22 23:45:59 -0700 | [diff] [blame] | 7878 | blk_status_t status; |
Nikolay Borisov | 89b798a | 2019-06-03 12:05:05 +0300 | [diff] [blame] | 7879 | struct btrfs_io_geometry geom; |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 7880 | struct btrfs_dio_data *dio_data = iomap->private; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7881 | |
Omar Sandoval | c36cac2 | 2020-04-16 14:46:13 -0700 | [diff] [blame] | 7882 | dip = btrfs_create_dio_private(dio_bio, inode, file_offset); |
| 7883 | if (!dip) { |
| 7884 | if (!write) { |
| 7885 | unlock_extent(&BTRFS_I(inode)->io_tree, file_offset, |
| 7886 | file_offset + dio_bio->bi_iter.bi_size - 1); |
| 7887 | } |
| 7888 | dio_bio->bi_status = BLK_STS_RESOURCE; |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 7889 | bio_endio(dio_bio); |
| 7890 | return BLK_QC_T_NONE; |
Josef Bacik | 02f57c7 | 2011-04-06 14:25:44 -0400 | [diff] [blame] | 7891 | } |
| 7892 | |
Josef Bacik | 334c16d | 2020-10-16 11:29:14 -0400 | [diff] [blame] | 7893 | if (!write) { |
Omar Sandoval | 8587957 | 2020-04-16 14:46:21 -0700 | [diff] [blame] | 7894 | /* |
| 7895 | * Load the csums up front to reduce csum tree searches and |
| 7896 | * contention when submitting bios. |
Josef Bacik | 334c16d | 2020-10-16 11:29:14 -0400 | [diff] [blame] | 7897 | * |
| 7898 | * If we have csums disabled this will do nothing. |
Omar Sandoval | 8587957 | 2020-04-16 14:46:21 -0700 | [diff] [blame] | 7899 | */ |
| 7900 | status = btrfs_lookup_bio_sums(inode, dio_bio, file_offset, |
| 7901 | dip->csums); |
| 7902 | if (status != BLK_STS_OK) |
| 7903 | goto out_err; |
| 7904 | } |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 7905 | |
Omar Sandoval | 769b4f2 | 2020-04-16 14:46:22 -0700 | [diff] [blame] | 7906 | start_sector = dio_bio->bi_iter.bi_sector; |
| 7907 | submit_len = dio_bio->bi_iter.bi_size; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7908 | |
Arnd Bergmann | 3c91ee6 | 2017-05-18 15:33:29 +0200 | [diff] [blame] | 7909 | do { |
Omar Sandoval | 769b4f2 | 2020-04-16 14:46:22 -0700 | [diff] [blame] | 7910 | ret = btrfs_get_io_geometry(fs_info, btrfs_op(dio_bio), |
| 7911 | start_sector << 9, submit_len, |
| 7912 | &geom); |
| 7913 | if (ret) { |
| 7914 | status = errno_to_blk_status(ret); |
| 7915 | goto out_err; |
| 7916 | } |
| 7917 | ASSERT(geom.len <= INT_MAX); |
| 7918 | |
Nikolay Borisov | 89b798a | 2019-06-03 12:05:05 +0300 | [diff] [blame] | 7919 | clone_len = min_t(int, submit_len, geom.len); |
Josef Bacik | 02f57c7 | 2011-04-06 14:25:44 -0400 | [diff] [blame] | 7920 | |
Liu Bo | 725130b | 2017-05-16 09:51:39 -0700 | [diff] [blame] | 7921 | /* |
| 7922 | * This will never fail as it's passing GPF_NOFS and |
| 7923 | * the allocation is backed by btrfs_bioset. |
| 7924 | */ |
Omar Sandoval | 769b4f2 | 2020-04-16 14:46:22 -0700 | [diff] [blame] | 7925 | bio = btrfs_bio_clone_partial(dio_bio, clone_offset, clone_len); |
Liu Bo | 725130b | 2017-05-16 09:51:39 -0700 | [diff] [blame] | 7926 | bio->bi_private = dip; |
| 7927 | bio->bi_end_io = btrfs_end_dio_bio; |
| 7928 | btrfs_io_bio(bio)->logical = file_offset; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7929 | |
Liu Bo | 725130b | 2017-05-16 09:51:39 -0700 | [diff] [blame] | 7930 | ASSERT(submit_len >= clone_len); |
| 7931 | submit_len -= clone_len; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7932 | |
Liu Bo | 725130b | 2017-05-16 09:51:39 -0700 | [diff] [blame] | 7933 | /* |
| 7934 | * Increase the count before we submit the bio so we know |
| 7935 | * the end IO handler won't happen before we increase the |
| 7936 | * count. Otherwise, the dip might get freed before we're |
| 7937 | * done setting it up. |
Omar Sandoval | 769b4f2 | 2020-04-16 14:46:22 -0700 | [diff] [blame] | 7938 | * |
| 7939 | * We transfer the initial reference to the last bio, so we |
| 7940 | * don't need to increment the reference count for the last one. |
Liu Bo | 725130b | 2017-05-16 09:51:39 -0700 | [diff] [blame] | 7941 | */ |
Omar Sandoval | 769b4f2 | 2020-04-16 14:46:22 -0700 | [diff] [blame] | 7942 | if (submit_len > 0) { |
| 7943 | refcount_inc(&dip->refs); |
| 7944 | /* |
| 7945 | * If we are submitting more than one bio, submit them |
| 7946 | * all asynchronously. The exception is RAID 5 or 6, as |
| 7947 | * asynchronous checksums make it difficult to collect |
| 7948 | * full stripe writes. |
| 7949 | */ |
| 7950 | if (!raid56) |
| 7951 | async_submit = 1; |
| 7952 | } |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7953 | |
David Sterba | d0ee393 | 2018-03-08 14:35:48 +0100 | [diff] [blame] | 7954 | status = btrfs_submit_dio_bio(bio, inode, file_offset, |
Omar Sandoval | 58efbc9 | 2017-08-22 23:45:59 -0700 | [diff] [blame] | 7955 | async_submit); |
| 7956 | if (status) { |
Liu Bo | 725130b | 2017-05-16 09:51:39 -0700 | [diff] [blame] | 7957 | bio_put(bio); |
Omar Sandoval | 769b4f2 | 2020-04-16 14:46:22 -0700 | [diff] [blame] | 7958 | if (submit_len > 0) |
| 7959 | refcount_dec(&dip->refs); |
Liu Bo | 725130b | 2017-05-16 09:51:39 -0700 | [diff] [blame] | 7960 | goto out_err; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7961 | } |
Liu Bo | 725130b | 2017-05-16 09:51:39 -0700 | [diff] [blame] | 7962 | |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 7963 | dio_data->submitted += clone_len; |
Liu Bo | 725130b | 2017-05-16 09:51:39 -0700 | [diff] [blame] | 7964 | clone_offset += clone_len; |
| 7965 | start_sector += clone_len >> 9; |
| 7966 | file_offset += clone_len; |
Arnd Bergmann | 3c91ee6 | 2017-05-18 15:33:29 +0200 | [diff] [blame] | 7967 | } while (submit_len > 0); |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 7968 | return BLK_QC_T_NONE; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7969 | |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7970 | out_err: |
Omar Sandoval | 769b4f2 | 2020-04-16 14:46:22 -0700 | [diff] [blame] | 7971 | dip->dio_bio->bi_status = status; |
| 7972 | btrfs_dio_private_put(dip); |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 7973 | return BLK_QC_T_NONE; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7974 | } |
| 7975 | |
Goldwyn Rodrigues | 4e4cabe | 2020-09-24 11:39:12 -0500 | [diff] [blame] | 7976 | const struct iomap_ops btrfs_dio_iomap_ops = { |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 7977 | .iomap_begin = btrfs_dio_iomap_begin, |
| 7978 | .iomap_end = btrfs_dio_iomap_end, |
| 7979 | }; |
| 7980 | |
Goldwyn Rodrigues | 4e4cabe | 2020-09-24 11:39:12 -0500 | [diff] [blame] | 7981 | const struct iomap_dio_ops btrfs_dio_ops = { |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 7982 | .submit_io = btrfs_submit_direct, |
| 7983 | }; |
| 7984 | |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 7985 | static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, |
David Sterba | bab16e2 | 2020-06-23 20:56:12 +0200 | [diff] [blame] | 7986 | u64 start, u64 len) |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 7987 | { |
Tsutomu Itoh | 05dadc0 | 2012-11-29 05:08:26 +0000 | [diff] [blame] | 7988 | int ret; |
| 7989 | |
Christoph Hellwig | 45dd052 | 2020-05-23 09:30:14 +0200 | [diff] [blame] | 7990 | ret = fiemap_prep(inode, fieinfo, start, &len, 0); |
Tsutomu Itoh | 05dadc0 | 2012-11-29 05:08:26 +0000 | [diff] [blame] | 7991 | if (ret) |
| 7992 | return ret; |
| 7993 | |
Nikolay Borisov | facee0a | 2020-08-31 14:42:49 +0300 | [diff] [blame] | 7994 | return extent_fiemap(BTRFS_I(inode), fieinfo, start, len); |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 7995 | } |
| 7996 | |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 7997 | int btrfs_readpage(struct file *file, struct page *page) |
| 7998 | { |
Nikolay Borisov | 0f20881 | 2020-09-14 14:39:16 +0300 | [diff] [blame] | 7999 | struct btrfs_inode *inode = BTRFS_I(page->mapping->host); |
| 8000 | u64 start = page_offset(page); |
| 8001 | u64 end = start + PAGE_SIZE - 1; |
Nikolay Borisov | c1be9c1 | 2020-09-14 12:37:08 +0300 | [diff] [blame] | 8002 | unsigned long bio_flags = 0; |
Nikolay Borisov | 0f20881 | 2020-09-14 14:39:16 +0300 | [diff] [blame] | 8003 | struct bio *bio = NULL; |
Nikolay Borisov | c1be9c1 | 2020-09-14 12:37:08 +0300 | [diff] [blame] | 8004 | int ret; |
| 8005 | |
Nikolay Borisov | 0f20881 | 2020-09-14 14:39:16 +0300 | [diff] [blame] | 8006 | btrfs_lock_and_flush_ordered_range(inode, start, end, NULL); |
| 8007 | |
| 8008 | ret = btrfs_do_readpage(page, NULL, &bio, &bio_flags, 0, NULL); |
Nikolay Borisov | c1be9c1 | 2020-09-14 12:37:08 +0300 | [diff] [blame] | 8009 | if (bio) |
| 8010 | ret = submit_one_bio(bio, 0, bio_flags); |
| 8011 | return ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8012 | } |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 8013 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8014 | static int btrfs_writepage(struct page *page, struct writeback_control *wbc) |
| 8015 | { |
Josef Bacik | be7bd73 | 2015-10-22 15:05:09 -0400 | [diff] [blame] | 8016 | struct inode *inode = page->mapping->host; |
| 8017 | int ret; |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8018 | |
| 8019 | if (current->flags & PF_MEMALLOC) { |
| 8020 | redirty_page_for_writepage(wbc, page); |
| 8021 | unlock_page(page); |
| 8022 | return 0; |
| 8023 | } |
Josef Bacik | be7bd73 | 2015-10-22 15:05:09 -0400 | [diff] [blame] | 8024 | |
| 8025 | /* |
| 8026 | * If we are under memory pressure we will call this directly from the |
| 8027 | * VM, we need to make sure we have the inode referenced for the ordered |
| 8028 | * extent. If not just return like we didn't do anything. |
| 8029 | */ |
| 8030 | if (!igrab(inode)) { |
| 8031 | redirty_page_for_writepage(wbc, page); |
| 8032 | return AOP_WRITEPAGE_ACTIVATE; |
| 8033 | } |
Nikolay Borisov | 0a9b0e5 | 2017-12-08 15:55:59 +0200 | [diff] [blame] | 8034 | ret = extent_write_full_page(page, wbc); |
Josef Bacik | be7bd73 | 2015-10-22 15:05:09 -0400 | [diff] [blame] | 8035 | btrfs_add_delayed_iput(inode); |
| 8036 | return ret; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8037 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8038 | |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 8039 | static int btrfs_writepages(struct address_space *mapping, |
| 8040 | struct writeback_control *wbc) |
Chris Mason | b293f02e | 2007-11-01 19:45:34 -0400 | [diff] [blame] | 8041 | { |
Nikolay Borisov | 8ae225a | 2018-04-19 10:46:38 +0300 | [diff] [blame] | 8042 | return extent_writepages(mapping, wbc); |
Chris Mason | b293f02e | 2007-11-01 19:45:34 -0400 | [diff] [blame] | 8043 | } |
| 8044 | |
Matthew Wilcox (Oracle) | ba206a0 | 2020-06-01 21:47:05 -0700 | [diff] [blame] | 8045 | static void btrfs_readahead(struct readahead_control *rac) |
Chris Mason | 3ab2fb5 | 2007-11-08 10:59:22 -0500 | [diff] [blame] | 8046 | { |
Matthew Wilcox (Oracle) | ba206a0 | 2020-06-01 21:47:05 -0700 | [diff] [blame] | 8047 | extent_readahead(rac); |
Chris Mason | 3ab2fb5 | 2007-11-08 10:59:22 -0500 | [diff] [blame] | 8048 | } |
Nikolay Borisov | 2a3ff0a | 2018-04-19 10:46:36 +0300 | [diff] [blame] | 8049 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8050 | static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8051 | { |
Nikolay Borisov | 477a30b | 2018-04-19 10:46:34 +0300 | [diff] [blame] | 8052 | int ret = try_release_extent_mapping(page, gfp_flags); |
Guoqing Jiang | d1b89bc | 2020-06-01 21:47:45 -0700 | [diff] [blame] | 8053 | if (ret == 1) |
| 8054 | detach_page_private(page); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8055 | return ret; |
| 8056 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8057 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8058 | static int btrfs_releasepage(struct page *page, gfp_t gfp_flags) |
| 8059 | { |
Chris Mason | 98509cf | 2008-09-11 15:51:43 -0400 | [diff] [blame] | 8060 | if (PageWriteback(page) || PageDirty(page)) |
| 8061 | return 0; |
Michal Hocko | 3ba7ab2 | 2017-01-09 15:39:02 +0100 | [diff] [blame] | 8062 | return __btrfs_releasepage(page, gfp_flags); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8063 | } |
| 8064 | |
Roman Gushchin | f8e6608 | 2020-03-04 16:57:35 -0800 | [diff] [blame] | 8065 | #ifdef CONFIG_MIGRATION |
| 8066 | static int btrfs_migratepage(struct address_space *mapping, |
| 8067 | struct page *newpage, struct page *page, |
| 8068 | enum migrate_mode mode) |
| 8069 | { |
| 8070 | int ret; |
| 8071 | |
| 8072 | ret = migrate_page_move_mapping(mapping, newpage, page, 0); |
| 8073 | if (ret != MIGRATEPAGE_SUCCESS) |
| 8074 | return ret; |
| 8075 | |
Guoqing Jiang | d1b89bc | 2020-06-01 21:47:45 -0700 | [diff] [blame] | 8076 | if (page_has_private(page)) |
| 8077 | attach_page_private(newpage, detach_page_private(page)); |
Roman Gushchin | f8e6608 | 2020-03-04 16:57:35 -0800 | [diff] [blame] | 8078 | |
| 8079 | if (PagePrivate2(page)) { |
| 8080 | ClearPagePrivate2(page); |
| 8081 | SetPagePrivate2(newpage); |
| 8082 | } |
| 8083 | |
| 8084 | if (mode != MIGRATE_SYNC_NO_COPY) |
| 8085 | migrate_page_copy(newpage, page); |
| 8086 | else |
| 8087 | migrate_page_states(newpage, page); |
| 8088 | return MIGRATEPAGE_SUCCESS; |
| 8089 | } |
| 8090 | #endif |
| 8091 | |
Lukas Czerner | d47992f | 2013-05-21 23:17:23 -0400 | [diff] [blame] | 8092 | static void btrfs_invalidatepage(struct page *page, unsigned int offset, |
| 8093 | unsigned int length) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8094 | { |
Nikolay Borisov | 53ac7ea | 2020-08-31 14:42:43 +0300 | [diff] [blame] | 8095 | struct btrfs_inode *inode = BTRFS_I(page->mapping->host); |
| 8096 | struct extent_io_tree *tree = &inode->io_tree; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8097 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 8098 | struct extent_state *cached_state = NULL; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8099 | u64 page_start = page_offset(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 8100 | u64 page_end = page_start + PAGE_SIZE - 1; |
Chandan Rajendra | dbfdb6d | 2016-01-21 15:55:58 +0530 | [diff] [blame] | 8101 | u64 start; |
| 8102 | u64 end; |
Nikolay Borisov | 53ac7ea | 2020-08-31 14:42:43 +0300 | [diff] [blame] | 8103 | int inode_evicting = inode->vfs_inode.i_state & I_FREEING; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8104 | |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 8105 | /* |
| 8106 | * we have the page locked, so new writeback can't start, |
| 8107 | * and the dirty bit won't be cleared while we are here. |
| 8108 | * |
| 8109 | * Wait for IO on this page so that we can safely clear |
| 8110 | * the PagePrivate2 bit and do ordered accounting |
| 8111 | */ |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8112 | wait_on_page_writeback(page); |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 8113 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8114 | if (offset) { |
| 8115 | btrfs_releasepage(page, GFP_NOFS); |
| 8116 | return; |
| 8117 | } |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 8118 | |
| 8119 | if (!inode_evicting) |
David Sterba | ff13db4 | 2015-12-03 14:30:40 +0100 | [diff] [blame] | 8120 | lock_extent_bits(tree, page_start, page_end, &cached_state); |
Chandan Rajendra | dbfdb6d | 2016-01-21 15:55:58 +0530 | [diff] [blame] | 8121 | again: |
| 8122 | start = page_start; |
Nikolay Borisov | 53ac7ea | 2020-08-31 14:42:43 +0300 | [diff] [blame] | 8123 | ordered = btrfs_lookup_ordered_range(inode, start, page_end - start + 1); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8124 | if (ordered) { |
Omar Sandoval | bffe633 | 2019-12-02 17:34:19 -0800 | [diff] [blame] | 8125 | end = min(page_end, |
| 8126 | ordered->file_offset + ordered->num_bytes - 1); |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 8127 | /* |
| 8128 | * IO on this page will never be started, so we need |
| 8129 | * to account for any ordered extents now |
| 8130 | */ |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 8131 | if (!inode_evicting) |
Chandan Rajendra | dbfdb6d | 2016-01-21 15:55:58 +0530 | [diff] [blame] | 8132 | clear_extent_bit(tree, start, end, |
Omar Sandoval | e182163 | 2019-08-15 14:04:04 -0700 | [diff] [blame] | 8133 | EXTENT_DELALLOC | EXTENT_DELALLOC_NEW | |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 8134 | EXTENT_LOCKED | EXTENT_DO_ACCOUNTING | |
David Sterba | ae0f162 | 2017-10-31 16:37:52 +0100 | [diff] [blame] | 8135 | EXTENT_DEFRAG, 1, 0, &cached_state); |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 8136 | /* |
| 8137 | * whoever cleared the private bit is responsible |
| 8138 | * for the finish_ordered_io |
| 8139 | */ |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 8140 | if (TestClearPagePrivate2(page)) { |
| 8141 | struct btrfs_ordered_inode_tree *tree; |
| 8142 | u64 new_len; |
| 8143 | |
Nikolay Borisov | 53ac7ea | 2020-08-31 14:42:43 +0300 | [diff] [blame] | 8144 | tree = &inode->ordered_tree; |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 8145 | |
| 8146 | spin_lock_irq(&tree->lock); |
| 8147 | set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags); |
Chandan Rajendra | dbfdb6d | 2016-01-21 15:55:58 +0530 | [diff] [blame] | 8148 | new_len = start - ordered->file_offset; |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 8149 | if (new_len < ordered->truncated_len) |
| 8150 | ordered->truncated_len = new_len; |
| 8151 | spin_unlock_irq(&tree->lock); |
| 8152 | |
Nikolay Borisov | 53ac7ea | 2020-08-31 14:42:43 +0300 | [diff] [blame] | 8153 | if (btrfs_dec_test_ordered_pending(inode, &ordered, |
| 8154 | start, |
Chandan Rajendra | dbfdb6d | 2016-01-21 15:55:58 +0530 | [diff] [blame] | 8155 | end - start + 1, 1)) |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 8156 | btrfs_finish_ordered_io(ordered); |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 8157 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8158 | btrfs_put_ordered_extent(ordered); |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 8159 | if (!inode_evicting) { |
| 8160 | cached_state = NULL; |
Chandan Rajendra | dbfdb6d | 2016-01-21 15:55:58 +0530 | [diff] [blame] | 8161 | lock_extent_bits(tree, start, end, |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 8162 | &cached_state); |
| 8163 | } |
Chandan Rajendra | dbfdb6d | 2016-01-21 15:55:58 +0530 | [diff] [blame] | 8164 | |
| 8165 | start = end + 1; |
| 8166 | if (start < page_end) |
| 8167 | goto again; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8168 | } |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 8169 | |
Qu Wenruo | b9d0b38 | 2015-09-29 10:35:16 +0800 | [diff] [blame] | 8170 | /* |
| 8171 | * Qgroup reserved space handler |
| 8172 | * Page here will be either |
Qu Wenruo | fa91e4a | 2020-07-17 15:12:05 +0800 | [diff] [blame] | 8173 | * 1) Already written to disk or ordered extent already submitted |
| 8174 | * Then its QGROUP_RESERVED bit in io_tree is already cleaned. |
| 8175 | * Qgroup will be handled by its qgroup_record then. |
| 8176 | * btrfs_qgroup_free_data() call will do nothing here. |
| 8177 | * |
| 8178 | * 2) Not written to disk yet |
| 8179 | * Then btrfs_qgroup_free_data() call will clear the QGROUP_RESERVED |
| 8180 | * bit of its io_tree, and free the qgroup reserved data space. |
| 8181 | * Since the IO will never happen for this page. |
Qu Wenruo | b9d0b38 | 2015-09-29 10:35:16 +0800 | [diff] [blame] | 8182 | */ |
Nikolay Borisov | 53ac7ea | 2020-08-31 14:42:43 +0300 | [diff] [blame] | 8183 | btrfs_qgroup_free_data(inode, NULL, page_start, PAGE_SIZE); |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 8184 | if (!inode_evicting) { |
Omar Sandoval | e182163 | 2019-08-15 14:04:04 -0700 | [diff] [blame] | 8185 | clear_extent_bit(tree, page_start, page_end, EXTENT_LOCKED | |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 8186 | EXTENT_DELALLOC | EXTENT_DELALLOC_NEW | |
| 8187 | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1, |
David Sterba | ae0f162 | 2017-10-31 16:37:52 +0100 | [diff] [blame] | 8188 | &cached_state); |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 8189 | |
| 8190 | __btrfs_releasepage(page, GFP_NOFS); |
| 8191 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8192 | |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 8193 | ClearPageChecked(page); |
Guoqing Jiang | d1b89bc | 2020-06-01 21:47:45 -0700 | [diff] [blame] | 8194 | detach_page_private(page); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8195 | } |
| 8196 | |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8197 | /* |
| 8198 | * btrfs_page_mkwrite() is not allowed to change the file size as it gets |
| 8199 | * called from a page fault handler when a page is first dirtied. Hence we must |
| 8200 | * be careful to check for EOF conditions here. We set the page up correctly |
| 8201 | * for a written page which means we get ENOSPC checking when writing into |
| 8202 | * holes and correct delalloc and unwritten extent mapping on filesystems that |
| 8203 | * support these features. |
| 8204 | * |
| 8205 | * We are not allowed to take the i_mutex here so we have to play games to |
| 8206 | * 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] | 8207 | * truncate_setsize() writes the inode size before removing pages, once we have |
| 8208 | * 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] | 8209 | * beyond EOF, then the page is guaranteed safe against truncation until we |
| 8210 | * unlock the page. |
| 8211 | */ |
Souptick Joarder | a528a24 | 2018-06-06 19:54:44 +0530 | [diff] [blame] | 8212 | vm_fault_t btrfs_page_mkwrite(struct vm_fault *vmf) |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8213 | { |
Nick Piggin | c2ec175 | 2009-03-31 15:23:21 -0700 | [diff] [blame] | 8214 | struct page *page = vmf->page; |
Dave Jiang | 11bac80 | 2017-02-24 14:56:41 -0800 | [diff] [blame] | 8215 | struct inode *inode = file_inode(vmf->vma->vm_file); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8216 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8217 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
| 8218 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 8219 | struct extent_state *cached_state = NULL; |
Qu Wenruo | 364ecf3 | 2017-02-27 15:10:38 +0800 | [diff] [blame] | 8220 | struct extent_changeset *data_reserved = NULL; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8221 | char *kaddr; |
| 8222 | unsigned long zero_start; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8223 | loff_t size; |
Souptick Joarder | a528a24 | 2018-06-06 19:54:44 +0530 | [diff] [blame] | 8224 | vm_fault_t ret; |
| 8225 | int ret2; |
Chris Mason | 9998eb7 | 2012-01-25 13:47:40 -0500 | [diff] [blame] | 8226 | int reserved = 0; |
Chandan Rajendra | d0b7da88 | 2016-01-21 15:55:57 +0530 | [diff] [blame] | 8227 | u64 reserved_space; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8228 | u64 page_start; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8229 | u64 page_end; |
Chandan Rajendra | d0b7da88 | 2016-01-21 15:55:57 +0530 | [diff] [blame] | 8230 | u64 end; |
| 8231 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 8232 | reserved_space = PAGE_SIZE; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8233 | |
Jan Kara | b2b5ef5 | 2012-06-12 16:20:45 +0200 | [diff] [blame] | 8234 | sb_start_pagefault(inode->i_sb); |
Qu Wenruo | df48063 | 2015-09-08 17:25:54 +0800 | [diff] [blame] | 8235 | page_start = page_offset(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 8236 | page_end = page_start + PAGE_SIZE - 1; |
Chandan Rajendra | d0b7da88 | 2016-01-21 15:55:57 +0530 | [diff] [blame] | 8237 | end = page_end; |
Qu Wenruo | df48063 | 2015-09-08 17:25:54 +0800 | [diff] [blame] | 8238 | |
Chandan Rajendra | d0b7da88 | 2016-01-21 15:55:57 +0530 | [diff] [blame] | 8239 | /* |
| 8240 | * Reserving delalloc space after obtaining the page lock can lead to |
| 8241 | * deadlock. For example, if a dirty page is locked by this function |
| 8242 | * and the call to btrfs_delalloc_reserve_space() ends up triggering |
| 8243 | * dirty page write out, then the btrfs_writepage() function could |
| 8244 | * end up waiting indefinitely to get a lock on the page currently |
| 8245 | * being processed by btrfs_page_mkwrite() function. |
| 8246 | */ |
Nikolay Borisov | e5b7231e | 2020-06-03 08:55:42 +0300 | [diff] [blame] | 8247 | ret2 = btrfs_delalloc_reserve_space(BTRFS_I(inode), &data_reserved, |
| 8248 | page_start, reserved_space); |
Souptick Joarder | a528a24 | 2018-06-06 19:54:44 +0530 | [diff] [blame] | 8249 | if (!ret2) { |
| 8250 | ret2 = file_update_time(vmf->vma->vm_file); |
Chris Mason | 9998eb7 | 2012-01-25 13:47:40 -0500 | [diff] [blame] | 8251 | reserved = 1; |
| 8252 | } |
Souptick Joarder | a528a24 | 2018-06-06 19:54:44 +0530 | [diff] [blame] | 8253 | if (ret2) { |
| 8254 | ret = vmf_error(ret2); |
Chris Mason | 9998eb7 | 2012-01-25 13:47:40 -0500 | [diff] [blame] | 8255 | if (reserved) |
| 8256 | goto out; |
| 8257 | goto out_noreserve; |
Nick Piggin | 56a76f8 | 2009-03-31 15:23:23 -0700 | [diff] [blame] | 8258 | } |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 8259 | |
Nick Piggin | 56a76f8 | 2009-03-31 15:23:23 -0700 | [diff] [blame] | 8260 | ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */ |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8261 | again: |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8262 | lock_page(page); |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8263 | size = i_size_read(inode); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8264 | |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8265 | if ((page->mapping != inode->i_mapping) || |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8266 | (page_start >= size)) { |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8267 | /* page got truncated out from underneath us */ |
| 8268 | goto out_unlock; |
| 8269 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8270 | wait_on_page_writeback(page); |
| 8271 | |
David Sterba | ff13db4 | 2015-12-03 14:30:40 +0100 | [diff] [blame] | 8272 | lock_extent_bits(io_tree, page_start, page_end, &cached_state); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8273 | set_page_extent_mapped(page); |
| 8274 | |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 8275 | /* |
| 8276 | * we can't set the delalloc bits if there are pending ordered |
| 8277 | * extents. Drop our locks and wait for them to finish |
| 8278 | */ |
Nikolay Borisov | a776c6f | 2017-02-20 13:50:49 +0200 | [diff] [blame] | 8279 | ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start, |
| 8280 | PAGE_SIZE); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8281 | if (ordered) { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 8282 | unlock_extent_cached(io_tree, page_start, page_end, |
David Sterba | e43bbe5 | 2017-12-12 21:43:52 +0100 | [diff] [blame] | 8283 | &cached_state); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8284 | unlock_page(page); |
Nikolay Borisov | c0a4360 | 2020-09-18 12:15:53 +0300 | [diff] [blame] | 8285 | btrfs_start_ordered_extent(ordered, 1); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8286 | btrfs_put_ordered_extent(ordered); |
| 8287 | goto again; |
| 8288 | } |
| 8289 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 8290 | if (page->index == ((size - 1) >> PAGE_SHIFT)) { |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 8291 | reserved_space = round_up(size - page_start, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8292 | fs_info->sectorsize); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 8293 | if (reserved_space < PAGE_SIZE) { |
Chandan Rajendra | d0b7da88 | 2016-01-21 15:55:57 +0530 | [diff] [blame] | 8294 | end = page_start + reserved_space - 1; |
Nikolay Borisov | 86d5292 | 2020-06-03 08:55:40 +0300 | [diff] [blame] | 8295 | btrfs_delalloc_release_space(BTRFS_I(inode), |
| 8296 | data_reserved, page_start, |
| 8297 | PAGE_SIZE - reserved_space, true); |
Chandan Rajendra | d0b7da88 | 2016-01-21 15:55:57 +0530 | [diff] [blame] | 8298 | } |
| 8299 | } |
| 8300 | |
Josef Bacik | fbf1908 | 2009-10-01 17:10:23 -0400 | [diff] [blame] | 8301 | /* |
Liu Bo | 5416034 | 2016-12-13 12:15:19 -0800 | [diff] [blame] | 8302 | * page_mkwrite gets called when the page is firstly dirtied after it's |
| 8303 | * faulted in, but write(2) could also dirty a page and set delalloc |
| 8304 | * bits, thus in this case for space account reason, we still need to |
| 8305 | * clear any delalloc bits within this page range since we have to |
| 8306 | * reserve data&meta space before lock_page() (see above comments). |
Josef Bacik | fbf1908 | 2009-10-01 17:10:23 -0400 | [diff] [blame] | 8307 | */ |
Chandan Rajendra | d0b7da88 | 2016-01-21 15:55:57 +0530 | [diff] [blame] | 8308 | clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, end, |
Omar Sandoval | e182163 | 2019-08-15 14:04:04 -0700 | [diff] [blame] | 8309 | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING | |
| 8310 | EXTENT_DEFRAG, 0, 0, &cached_state); |
Josef Bacik | fbf1908 | 2009-10-01 17:10:23 -0400 | [diff] [blame] | 8311 | |
Nikolay Borisov | c2566f2 | 2020-06-03 08:55:35 +0300 | [diff] [blame] | 8312 | ret2 = btrfs_set_extent_delalloc(BTRFS_I(inode), page_start, end, 0, |
Nikolay Borisov | 330a582 | 2019-07-17 16:18:17 +0300 | [diff] [blame] | 8313 | &cached_state); |
Souptick Joarder | a528a24 | 2018-06-06 19:54:44 +0530 | [diff] [blame] | 8314 | if (ret2) { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 8315 | unlock_extent_cached(io_tree, page_start, page_end, |
David Sterba | e43bbe5 | 2017-12-12 21:43:52 +0100 | [diff] [blame] | 8316 | &cached_state); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 8317 | ret = VM_FAULT_SIGBUS; |
| 8318 | goto out_unlock; |
| 8319 | } |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8320 | |
| 8321 | /* page is wholly or partially inside EOF */ |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 8322 | if (page_start + PAGE_SIZE > size) |
Johannes Thumshirn | 7073017 | 2018-12-05 15:23:03 +0100 | [diff] [blame] | 8323 | zero_start = offset_in_page(size); |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8324 | else |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 8325 | zero_start = PAGE_SIZE; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8326 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 8327 | if (zero_start != PAGE_SIZE) { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8328 | kaddr = kmap(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 8329 | memset(kaddr + zero_start, 0, PAGE_SIZE - zero_start); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8330 | flush_dcache_page(page); |
| 8331 | kunmap(page); |
| 8332 | } |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 8333 | ClearPageChecked(page); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8334 | set_page_dirty(page); |
Chris Mason | 50a9b21 | 2009-09-11 12:33:12 -0400 | [diff] [blame] | 8335 | SetPageUptodate(page); |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 8336 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8337 | BTRFS_I(inode)->last_trans = fs_info->generation; |
Chris Mason | 257c62e | 2009-10-13 13:21:08 -0400 | [diff] [blame] | 8338 | BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid; |
Liu Bo | 46d8bc3 | 2012-08-29 01:07:55 -0600 | [diff] [blame] | 8339 | 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] | 8340 | |
David Sterba | e43bbe5 | 2017-12-12 21:43:52 +0100 | [diff] [blame] | 8341 | unlock_extent_cached(io_tree, page_start, page_end, &cached_state); |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8342 | |
Yunfeng Ye | 76de60e | 2019-12-03 16:59:25 +0800 | [diff] [blame] | 8343 | btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE); |
| 8344 | sb_end_pagefault(inode->i_sb); |
| 8345 | extent_changeset_free(data_reserved); |
| 8346 | return VM_FAULT_LOCKED; |
Chris Mason | 717beb9 | 2018-06-25 10:03:41 -0700 | [diff] [blame] | 8347 | |
| 8348 | out_unlock: |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8349 | unlock_page(page); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 8350 | out: |
Qu Wenruo | 8702ba9 | 2019-10-14 14:34:51 +0800 | [diff] [blame] | 8351 | btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE); |
Nikolay Borisov | 86d5292 | 2020-06-03 08:55:40 +0300 | [diff] [blame] | 8352 | btrfs_delalloc_release_space(BTRFS_I(inode), data_reserved, page_start, |
Qu Wenruo | 43b1859 | 2017-12-12 15:34:32 +0800 | [diff] [blame] | 8353 | reserved_space, (ret != 0)); |
Chris Mason | 9998eb7 | 2012-01-25 13:47:40 -0500 | [diff] [blame] | 8354 | out_noreserve: |
Jan Kara | b2b5ef5 | 2012-06-12 16:20:45 +0200 | [diff] [blame] | 8355 | sb_end_pagefault(inode->i_sb); |
Qu Wenruo | 364ecf3 | 2017-02-27 15:10:38 +0800 | [diff] [blame] | 8356 | extent_changeset_free(data_reserved); |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8357 | return ret; |
| 8358 | } |
| 8359 | |
Filipe Manana | 213e8c5 | 2018-02-06 20:40:31 +0000 | [diff] [blame] | 8360 | static int btrfs_truncate(struct inode *inode, bool skip_writeback) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8361 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8362 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8363 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 8364 | struct btrfs_block_rsv *rsv; |
Omar Sandoval | ad7e1a7 | 2018-05-22 09:59:50 -0700 | [diff] [blame] | 8365 | int ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8366 | struct btrfs_trans_handle *trans; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8367 | u64 mask = fs_info->sectorsize - 1; |
Josef Bacik | 2bd36e7 | 2019-08-22 15:14:33 -0400 | [diff] [blame] | 8368 | u64 min_size = btrfs_calc_metadata_size(fs_info, 1); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8369 | |
Filipe Manana | 213e8c5 | 2018-02-06 20:40:31 +0000 | [diff] [blame] | 8370 | if (!skip_writeback) { |
| 8371 | ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask), |
| 8372 | (u64)-1); |
| 8373 | if (ret) |
| 8374 | return ret; |
| 8375 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8376 | |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 8377 | /* |
Omar Sandoval | f7e9e8f | 2018-05-11 13:13:32 -0700 | [diff] [blame] | 8378 | * Yes ladies and gentlemen, this is indeed ugly. We have a couple of |
| 8379 | * things going on here: |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 8380 | * |
Omar Sandoval | f7e9e8f | 2018-05-11 13:13:32 -0700 | [diff] [blame] | 8381 | * 1) We need to reserve space to update our inode. |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 8382 | * |
Omar Sandoval | f7e9e8f | 2018-05-11 13:13:32 -0700 | [diff] [blame] | 8383 | * 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] | 8384 | * be free'd up by the truncate operation, but also have some slack |
| 8385 | * space reserved in case it uses space during the truncate (thank you |
| 8386 | * very much snapshotting). |
| 8387 | * |
Omar Sandoval | f7e9e8f | 2018-05-11 13:13:32 -0700 | [diff] [blame] | 8388 | * 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] | 8389 | * 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] | 8390 | * 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] | 8391 | * doesn't end up using space reserved for updating the inode. We also |
| 8392 | * need to be able to stop the transaction and start a new one, which |
| 8393 | * means we need to be able to update the inode several times, and we |
| 8394 | * have no idea of knowing how many times that will be, so we can't just |
| 8395 | * reserve 1 item for the entirety of the operation, so that has to be |
| 8396 | * done separately as well. |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 8397 | * |
| 8398 | * So that leaves us with |
| 8399 | * |
Omar Sandoval | f7e9e8f | 2018-05-11 13:13:32 -0700 | [diff] [blame] | 8400 | * 1) rsv - for the truncate reservation, which we will steal from the |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 8401 | * transaction reservation. |
Omar Sandoval | f7e9e8f | 2018-05-11 13:13:32 -0700 | [diff] [blame] | 8402 | * 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] | 8403 | * updating the inode. |
| 8404 | */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 8405 | rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP); |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 8406 | if (!rsv) |
| 8407 | return -ENOMEM; |
Josef Bacik | 4a33854 | 2011-08-29 11:01:31 -0400 | [diff] [blame] | 8408 | rsv->size = min_size; |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 8409 | rsv->failfast = 1; |
Josef Bacik | f0cd846 | 2011-03-04 14:37:08 -0500 | [diff] [blame] | 8410 | |
Josef Bacik | 907cbce | 2011-08-08 13:46:15 -0400 | [diff] [blame] | 8411 | /* |
Josef Bacik | 0712718 | 2011-08-19 10:29:59 -0400 | [diff] [blame] | 8412 | * 1 for the truncate slack space |
Josef Bacik | 907cbce | 2011-08-08 13:46:15 -0400 | [diff] [blame] | 8413 | * 1 for updating the inode. |
| 8414 | */ |
Josef Bacik | f3fe820 | 2013-01-07 17:03:21 -0500 | [diff] [blame] | 8415 | trans = btrfs_start_transaction(root, 2); |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 8416 | if (IS_ERR(trans)) { |
Omar Sandoval | ad7e1a7 | 2018-05-22 09:59:50 -0700 | [diff] [blame] | 8417 | ret = PTR_ERR(trans); |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 8418 | goto out; |
| 8419 | } |
Josef Bacik | f0cd846 | 2011-03-04 14:37:08 -0500 | [diff] [blame] | 8420 | |
Josef Bacik | 907cbce | 2011-08-08 13:46:15 -0400 | [diff] [blame] | 8421 | /* Migrate the slack space for the truncate to our reserve */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8422 | ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, rsv, |
Lu Fengqi | 3a58417 | 2018-08-04 21:10:55 +0800 | [diff] [blame] | 8423 | min_size, false); |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 8424 | BUG_ON(ret); |
Josef Bacik | f0cd846 | 2011-03-04 14:37:08 -0500 | [diff] [blame] | 8425 | |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 8426 | /* |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 8427 | * So if we truncate and then write and fsync we normally would just |
| 8428 | * write the extents that changed, which is a problem if we need to |
| 8429 | * first truncate that entire inode. So set this flag so we write out |
| 8430 | * all of the extents in the inode to the sync log so we're completely |
| 8431 | * safe. |
| 8432 | */ |
| 8433 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags); |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 8434 | trans->block_rsv = rsv; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 8435 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 8436 | while (1) { |
| 8437 | ret = btrfs_truncate_inode_items(trans, root, inode, |
| 8438 | inode->i_size, |
| 8439 | BTRFS_EXTENT_DATA_KEY); |
Josef Bacik | ddfae63 | 2017-10-19 14:16:02 -0400 | [diff] [blame] | 8440 | trans->block_rsv = &fs_info->trans_block_rsv; |
Omar Sandoval | ad7e1a7 | 2018-05-22 09:59:50 -0700 | [diff] [blame] | 8441 | if (ret != -ENOSPC && ret != -EAGAIN) |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 8442 | break; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8443 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 8444 | ret = btrfs_update_inode(trans, root, inode); |
Omar Sandoval | ad7e1a7 | 2018-05-22 09:59:50 -0700 | [diff] [blame] | 8445 | if (ret) |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 8446 | break; |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 8447 | |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 8448 | btrfs_end_transaction(trans); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 8449 | btrfs_btree_balance_dirty(fs_info); |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 8450 | |
| 8451 | trans = btrfs_start_transaction(root, 2); |
| 8452 | if (IS_ERR(trans)) { |
Omar Sandoval | ad7e1a7 | 2018-05-22 09:59:50 -0700 | [diff] [blame] | 8453 | ret = PTR_ERR(trans); |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 8454 | trans = NULL; |
| 8455 | break; |
| 8456 | } |
| 8457 | |
Nikolay Borisov | 63f018b | 2020-03-10 10:59:31 +0200 | [diff] [blame] | 8458 | btrfs_block_rsv_release(fs_info, rsv, -1, NULL); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8459 | ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, |
Lu Fengqi | 3a58417 | 2018-08-04 21:10:55 +0800 | [diff] [blame] | 8460 | rsv, min_size, false); |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 8461 | BUG_ON(ret); /* shouldn't happen */ |
| 8462 | trans->block_rsv = rsv; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 8463 | } |
| 8464 | |
Josef Bacik | ddfae63 | 2017-10-19 14:16:02 -0400 | [diff] [blame] | 8465 | /* |
| 8466 | * We can't call btrfs_truncate_block inside a trans handle as we could |
| 8467 | * deadlock with freeze, if we got NEED_TRUNCATE_BLOCK then we know |
| 8468 | * we've truncated everything except the last little bit, and can do |
| 8469 | * btrfs_truncate_block and then update the disk_i_size. |
| 8470 | */ |
| 8471 | if (ret == NEED_TRUNCATE_BLOCK) { |
| 8472 | btrfs_end_transaction(trans); |
| 8473 | btrfs_btree_balance_dirty(fs_info); |
| 8474 | |
| 8475 | ret = btrfs_truncate_block(inode, inode->i_size, 0, 0); |
| 8476 | if (ret) |
| 8477 | goto out; |
| 8478 | trans = btrfs_start_transaction(root, 1); |
| 8479 | if (IS_ERR(trans)) { |
| 8480 | ret = PTR_ERR(trans); |
| 8481 | goto out; |
| 8482 | } |
Josef Bacik | d923afe | 2020-01-17 09:02:23 -0500 | [diff] [blame] | 8483 | btrfs_inode_safe_disk_i_size_write(inode, 0); |
Josef Bacik | ddfae63 | 2017-10-19 14:16:02 -0400 | [diff] [blame] | 8484 | } |
| 8485 | |
Chris Mason | 917c16b | 2011-11-08 14:49:59 -0500 | [diff] [blame] | 8486 | if (trans) { |
Omar Sandoval | ad7e1a7 | 2018-05-22 09:59:50 -0700 | [diff] [blame] | 8487 | int ret2; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 8488 | |
Omar Sandoval | ad7e1a7 | 2018-05-22 09:59:50 -0700 | [diff] [blame] | 8489 | trans->block_rsv = &fs_info->trans_block_rsv; |
| 8490 | ret2 = btrfs_update_inode(trans, root, inode); |
| 8491 | if (ret2 && !ret) |
| 8492 | ret = ret2; |
| 8493 | |
| 8494 | ret2 = btrfs_end_transaction(trans); |
| 8495 | if (ret2 && !ret) |
| 8496 | ret = ret2; |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 8497 | btrfs_btree_balance_dirty(fs_info); |
Chris Mason | 917c16b | 2011-11-08 14:49:59 -0500 | [diff] [blame] | 8498 | } |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 8499 | out: |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 8500 | btrfs_free_block_rsv(fs_info, rsv); |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 8501 | |
Omar Sandoval | ad7e1a7 | 2018-05-22 09:59:50 -0700 | [diff] [blame] | 8502 | return ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8503 | } |
| 8504 | |
Sven Wegener | 3b96362 | 2008-06-09 21:57:42 -0400 | [diff] [blame] | 8505 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 8506 | * create a new subvolume directory/inode (helper for the ioctl). |
| 8507 | */ |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 8508 | int btrfs_create_subvol_root(struct btrfs_trans_handle *trans, |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 8509 | struct btrfs_root *new_root, |
| 8510 | struct btrfs_root *parent_root, |
| 8511 | u64 new_dirid) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8512 | { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8513 | struct inode *inode; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 8514 | int err; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 8515 | u64 index = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8516 | |
Florian Albrechtskirchinger | 12fc9d0 | 2012-02-10 22:15:54 +0100 | [diff] [blame] | 8517 | inode = btrfs_new_inode(trans, new_root, NULL, "..", 2, |
| 8518 | new_dirid, new_dirid, |
| 8519 | S_IFDIR | (~current_umask() & S_IRWXUGO), |
| 8520 | &index); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 8521 | if (IS_ERR(inode)) |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 8522 | return PTR_ERR(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8523 | inode->i_op = &btrfs_dir_inode_operations; |
| 8524 | inode->i_fop = &btrfs_dir_file_operations; |
| 8525 | |
Miklos Szeredi | bfe8684 | 2011-10-28 14:13:29 +0200 | [diff] [blame] | 8526 | set_nlink(inode, 1); |
Nikolay Borisov | 6ef06d2 | 2017-02-20 13:50:34 +0200 | [diff] [blame] | 8527 | btrfs_i_size_write(BTRFS_I(inode), 0); |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 8528 | unlock_new_inode(inode); |
Sven Wegener | 3b96362 | 2008-06-09 21:57:42 -0400 | [diff] [blame] | 8529 | |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 8530 | err = btrfs_subvol_inherit_props(trans, new_root, parent_root); |
| 8531 | if (err) |
| 8532 | btrfs_err(new_root->fs_info, |
David Sterba | 351fd35 | 2014-05-15 16:48:20 +0200 | [diff] [blame] | 8533 | "error inheriting subvolume %llu properties: %d", |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 8534 | new_root->root_key.objectid, err); |
| 8535 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 8536 | err = btrfs_update_inode(trans, new_root, inode); |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 8537 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 8538 | iput(inode); |
Mark Fasheh | ce59897 | 2011-07-26 11:32:23 -0700 | [diff] [blame] | 8539 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8540 | } |
| 8541 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8542 | struct inode *btrfs_alloc_inode(struct super_block *sb) |
| 8543 | { |
Josef Bacik | 69fe2d7 | 2017-10-19 14:15:57 -0400 | [diff] [blame] | 8544 | struct btrfs_fs_info *fs_info = btrfs_sb(sb); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8545 | struct btrfs_inode *ei; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8546 | struct inode *inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8547 | |
David Sterba | 712e36c | 2017-10-31 17:08:27 +0100 | [diff] [blame] | 8548 | ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_KERNEL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8549 | if (!ei) |
| 8550 | return NULL; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8551 | |
| 8552 | ei->root = NULL; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8553 | ei->generation = 0; |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 8554 | ei->last_trans = 0; |
Chris Mason | 257c62e | 2009-10-13 13:21:08 -0400 | [diff] [blame] | 8555 | ei->last_sub_trans = 0; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 8556 | ei->logged_trans = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8557 | ei->delalloc_bytes = 0; |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 8558 | ei->new_delalloc_bytes = 0; |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 8559 | ei->defrag_bytes = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8560 | ei->disk_i_size = 0; |
| 8561 | ei->flags = 0; |
Josef Bacik | 7709cde | 2011-08-04 10:25:02 -0400 | [diff] [blame] | 8562 | ei->csum_bytes = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8563 | ei->index_cnt = (u64)-1; |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 8564 | ei->dir_index = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8565 | ei->last_unlink_trans = 0; |
Filipe Manana | 3ebac17 | 2020-07-15 12:30:43 +0100 | [diff] [blame] | 8566 | ei->last_reflink_trans = 0; |
Liu Bo | 46d8bc3 | 2012-08-29 01:07:55 -0600 | [diff] [blame] | 8567 | ei->last_log_commit = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8568 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 8569 | spin_lock_init(&ei->lock); |
| 8570 | ei->outstanding_extents = 0; |
Josef Bacik | 69fe2d7 | 2017-10-19 14:15:57 -0400 | [diff] [blame] | 8571 | if (sb->s_magic != BTRFS_TEST_MAGIC) |
| 8572 | btrfs_init_metadata_block_rsv(fs_info, &ei->block_rsv, |
| 8573 | BTRFS_BLOCK_RSV_DELALLOC); |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 8574 | ei->runtime_flags = 0; |
David Sterba | b52aa8c | 2017-07-17 19:17:20 +0200 | [diff] [blame] | 8575 | ei->prop_compress = BTRFS_COMPRESS_NONE; |
David Sterba | eec63c6 | 2017-07-17 19:41:31 +0200 | [diff] [blame] | 8576 | ei->defrag_compress = BTRFS_COMPRESS_NONE; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8577 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 8578 | ei->delayed_node = NULL; |
| 8579 | |
chandan r | 9cc97d6 | 2012-07-04 12:48:07 +0530 | [diff] [blame] | 8580 | ei->i_otime.tv_sec = 0; |
| 8581 | ei->i_otime.tv_nsec = 0; |
| 8582 | |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8583 | inode = &ei->vfs_inode; |
David Sterba | a8067e0 | 2011-04-21 00:34:43 +0200 | [diff] [blame] | 8584 | extent_map_tree_init(&ei->extent_tree); |
Qu Wenruo | 43eb5f2 | 2019-03-01 10:47:59 +0800 | [diff] [blame] | 8585 | extent_io_tree_init(fs_info, &ei->io_tree, IO_TREE_INODE_IO, inode); |
| 8586 | extent_io_tree_init(fs_info, &ei->io_failure_tree, |
| 8587 | IO_TREE_INODE_IO_FAILURE, inode); |
Josef Bacik | 41a2ee7 | 2020-01-17 09:02:21 -0500 | [diff] [blame] | 8588 | extent_io_tree_init(fs_info, &ei->file_extent_tree, |
| 8589 | IO_TREE_INODE_FILE_EXTENT, inode); |
David Sterba | 7b43973 | 2019-03-11 15:58:30 +0100 | [diff] [blame] | 8590 | ei->io_tree.track_uptodate = true; |
| 8591 | ei->io_failure_tree.track_uptodate = true; |
Josef Bacik | b812ce2 | 2012-11-16 13:56:32 -0500 | [diff] [blame] | 8592 | atomic_set(&ei->sync_writers, 0); |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8593 | mutex_init(&ei->log_mutex); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8594 | btrfs_ordered_inode_tree_init(&ei->ordered_tree); |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8595 | INIT_LIST_HEAD(&ei->delalloc_inodes); |
David Sterba | 8089fe6 | 2015-11-19 14:15:51 +0100 | [diff] [blame] | 8596 | INIT_LIST_HEAD(&ei->delayed_iput); |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8597 | RB_CLEAR_NODE(&ei->rb_node); |
| 8598 | |
| 8599 | return inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8600 | } |
| 8601 | |
Josef Bacik | aaedb55 | 2013-10-11 14:44:09 -0400 | [diff] [blame] | 8602 | #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS |
| 8603 | void btrfs_test_destroy_inode(struct inode *inode) |
| 8604 | { |
Nikolay Borisov | dcdbc05 | 2017-02-20 13:50:45 +0200 | [diff] [blame] | 8605 | btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0); |
Josef Bacik | aaedb55 | 2013-10-11 14:44:09 -0400 | [diff] [blame] | 8606 | kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode)); |
| 8607 | } |
| 8608 | #endif |
| 8609 | |
Al Viro | 26602ca | 2019-04-10 15:14:41 -0400 | [diff] [blame] | 8610 | void btrfs_free_inode(struct inode *inode) |
Nick Piggin | fa0d7e3d | 2011-01-07 17:49:49 +1100 | [diff] [blame] | 8611 | { |
Nick Piggin | fa0d7e3d | 2011-01-07 17:49:49 +1100 | [diff] [blame] | 8612 | kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode)); |
| 8613 | } |
| 8614 | |
Nikolay Borisov | 633cc81 | 2020-09-18 12:15:49 +0300 | [diff] [blame] | 8615 | void btrfs_destroy_inode(struct inode *vfs_inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8616 | { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8617 | struct btrfs_ordered_extent *ordered; |
Nikolay Borisov | 633cc81 | 2020-09-18 12:15:49 +0300 | [diff] [blame] | 8618 | struct btrfs_inode *inode = BTRFS_I(vfs_inode); |
| 8619 | struct btrfs_root *root = inode->root; |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 8620 | |
Nikolay Borisov | 633cc81 | 2020-09-18 12:15:49 +0300 | [diff] [blame] | 8621 | WARN_ON(!hlist_empty(&vfs_inode->i_dentry)); |
| 8622 | WARN_ON(vfs_inode->i_data.nrpages); |
| 8623 | WARN_ON(inode->block_rsv.reserved); |
| 8624 | WARN_ON(inode->block_rsv.size); |
| 8625 | WARN_ON(inode->outstanding_extents); |
| 8626 | WARN_ON(inode->delalloc_bytes); |
| 8627 | WARN_ON(inode->new_delalloc_bytes); |
| 8628 | WARN_ON(inode->csum_bytes); |
| 8629 | WARN_ON(inode->defrag_bytes); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8630 | |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 8631 | /* |
Josef Bacik | a6dbd42 | 2009-11-11 15:53:34 -0500 | [diff] [blame] | 8632 | * This can happen where we create an inode, but somebody else also |
| 8633 | * created the same inode and we need to destroy the one we already |
| 8634 | * created. |
| 8635 | */ |
| 8636 | if (!root) |
Al Viro | 26602ca | 2019-04-10 15:14:41 -0400 | [diff] [blame] | 8637 | return; |
Josef Bacik | a6dbd42 | 2009-11-11 15:53:34 -0500 | [diff] [blame] | 8638 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 8639 | while (1) { |
Nikolay Borisov | 633cc81 | 2020-09-18 12:15:49 +0300 | [diff] [blame] | 8640 | ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8641 | if (!ordered) |
| 8642 | break; |
| 8643 | else { |
Nikolay Borisov | 633cc81 | 2020-09-18 12:15:49 +0300 | [diff] [blame] | 8644 | btrfs_err(root->fs_info, |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 8645 | "found ordered extent %llu %llu on inode cleanup", |
Omar Sandoval | bffe633 | 2019-12-02 17:34:19 -0800 | [diff] [blame] | 8646 | ordered->file_offset, ordered->num_bytes); |
Nikolay Borisov | 71fe0a5 | 2020-09-18 12:15:50 +0300 | [diff] [blame] | 8647 | btrfs_remove_ordered_extent(inode, ordered); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8648 | btrfs_put_ordered_extent(ordered); |
| 8649 | btrfs_put_ordered_extent(ordered); |
| 8650 | } |
| 8651 | } |
Nikolay Borisov | 633cc81 | 2020-09-18 12:15:49 +0300 | [diff] [blame] | 8652 | btrfs_qgroup_check_reserved_leak(inode); |
| 8653 | inode_tree_del(inode); |
| 8654 | btrfs_drop_extent_cache(inode, 0, (u64)-1, 0); |
| 8655 | btrfs_inode_clear_file_extent_range(inode, 0, (u64)-1); |
| 8656 | btrfs_put_root(inode->root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8657 | } |
| 8658 | |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 8659 | int btrfs_drop_inode(struct inode *inode) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 8660 | { |
| 8661 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 8662 | |
Naohiro Aota | 6379ef9 | 2013-06-06 09:56:34 +0000 | [diff] [blame] | 8663 | if (root == NULL) |
| 8664 | return 1; |
| 8665 | |
Liu Bo | fa6ac87 | 2013-02-20 14:10:23 +0000 | [diff] [blame] | 8666 | /* the snap/subvol tree is on deleting */ |
Stefan Behrens | 69e9c6c | 2013-09-05 16:58:43 +0200 | [diff] [blame] | 8667 | if (btrfs_root_refs(&root->root_item) == 0) |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 8668 | return 1; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 8669 | else |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 8670 | return generic_drop_inode(inode); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 8671 | } |
| 8672 | |
Sven Wegener | 0ee0fda | 2008-07-30 16:54:26 -0400 | [diff] [blame] | 8673 | static void init_once(void *foo) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8674 | { |
| 8675 | struct btrfs_inode *ei = (struct btrfs_inode *) foo; |
| 8676 | |
| 8677 | inode_init_once(&ei->vfs_inode); |
| 8678 | } |
| 8679 | |
David Sterba | e67c718 | 2018-02-19 17:24:18 +0100 | [diff] [blame] | 8680 | void __cold btrfs_destroy_cachep(void) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8681 | { |
Kirill A. Shutemov | 8c0a853 | 2012-09-26 11:33:07 +1000 | [diff] [blame] | 8682 | /* |
| 8683 | * Make sure all delayed rcu free inodes are flushed before we |
| 8684 | * destroy cache. |
| 8685 | */ |
| 8686 | rcu_barrier(); |
Kinglong Mee | 5598e90 | 2016-01-29 21:36:35 +0800 | [diff] [blame] | 8687 | kmem_cache_destroy(btrfs_inode_cachep); |
| 8688 | kmem_cache_destroy(btrfs_trans_handle_cachep); |
Kinglong Mee | 5598e90 | 2016-01-29 21:36:35 +0800 | [diff] [blame] | 8689 | kmem_cache_destroy(btrfs_path_cachep); |
| 8690 | kmem_cache_destroy(btrfs_free_space_cachep); |
Christophe Leroy | 3acd485 | 2019-08-21 15:05:55 +0000 | [diff] [blame] | 8691 | kmem_cache_destroy(btrfs_free_space_bitmap_cachep); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8692 | } |
| 8693 | |
Liu Bo | f5c29bd | 2017-11-02 17:21:50 -0600 | [diff] [blame] | 8694 | int __init btrfs_init_cachep(void) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8695 | { |
David Sterba | 837e197 | 2012-09-07 03:00:48 -0600 | [diff] [blame] | 8696 | btrfs_inode_cachep = kmem_cache_create("btrfs_inode", |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 8697 | sizeof(struct btrfs_inode), 0, |
Vladimir Davydov | 5d09705 | 2016-01-14 15:18:21 -0800 | [diff] [blame] | 8698 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT, |
| 8699 | init_once); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8700 | if (!btrfs_inode_cachep) |
| 8701 | goto fail; |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 8702 | |
David Sterba | 837e197 | 2012-09-07 03:00:48 -0600 | [diff] [blame] | 8703 | btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle", |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 8704 | sizeof(struct btrfs_trans_handle), 0, |
Nikolay Borisov | fba4b69 | 2016-06-23 21:17:08 +0300 | [diff] [blame] | 8705 | SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8706 | if (!btrfs_trans_handle_cachep) |
| 8707 | goto fail; |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 8708 | |
David Sterba | 837e197 | 2012-09-07 03:00:48 -0600 | [diff] [blame] | 8709 | btrfs_path_cachep = kmem_cache_create("btrfs_path", |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 8710 | sizeof(struct btrfs_path), 0, |
Nikolay Borisov | fba4b69 | 2016-06-23 21:17:08 +0300 | [diff] [blame] | 8711 | SLAB_MEM_SPREAD, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8712 | if (!btrfs_path_cachep) |
| 8713 | goto fail; |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 8714 | |
David Sterba | 837e197 | 2012-09-07 03:00:48 -0600 | [diff] [blame] | 8715 | btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space", |
Josef Bacik | dc89e98 | 2011-01-28 17:05:48 -0500 | [diff] [blame] | 8716 | sizeof(struct btrfs_free_space), 0, |
Nikolay Borisov | fba4b69 | 2016-06-23 21:17:08 +0300 | [diff] [blame] | 8717 | SLAB_MEM_SPREAD, NULL); |
Josef Bacik | dc89e98 | 2011-01-28 17:05:48 -0500 | [diff] [blame] | 8718 | if (!btrfs_free_space_cachep) |
| 8719 | goto fail; |
| 8720 | |
Christophe Leroy | 3acd485 | 2019-08-21 15:05:55 +0000 | [diff] [blame] | 8721 | btrfs_free_space_bitmap_cachep = kmem_cache_create("btrfs_free_space_bitmap", |
| 8722 | PAGE_SIZE, PAGE_SIZE, |
| 8723 | SLAB_RED_ZONE, NULL); |
| 8724 | if (!btrfs_free_space_bitmap_cachep) |
| 8725 | goto fail; |
| 8726 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8727 | return 0; |
| 8728 | fail: |
| 8729 | btrfs_destroy_cachep(); |
| 8730 | return -ENOMEM; |
| 8731 | } |
| 8732 | |
David Howells | a528d35 | 2017-01-31 16:46:22 +0000 | [diff] [blame] | 8733 | static int btrfs_getattr(const struct path *path, struct kstat *stat, |
| 8734 | u32 request_mask, unsigned int flags) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8735 | { |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 8736 | u64 delalloc_bytes; |
David Howells | a528d35 | 2017-01-31 16:46:22 +0000 | [diff] [blame] | 8737 | struct inode *inode = d_inode(path->dentry); |
David Sterba | fadc0d8 | 2011-11-20 07:33:38 -0500 | [diff] [blame] | 8738 | u32 blocksize = inode->i_sb->s_blocksize; |
Yonghong Song | 04a87e3 | 2017-05-12 15:07:43 -0700 | [diff] [blame] | 8739 | u32 bi_flags = BTRFS_I(inode)->flags; |
| 8740 | |
| 8741 | stat->result_mask |= STATX_BTIME; |
| 8742 | stat->btime.tv_sec = BTRFS_I(inode)->i_otime.tv_sec; |
| 8743 | stat->btime.tv_nsec = BTRFS_I(inode)->i_otime.tv_nsec; |
| 8744 | if (bi_flags & BTRFS_INODE_APPEND) |
| 8745 | stat->attributes |= STATX_ATTR_APPEND; |
| 8746 | if (bi_flags & BTRFS_INODE_COMPRESS) |
| 8747 | stat->attributes |= STATX_ATTR_COMPRESSED; |
| 8748 | if (bi_flags & BTRFS_INODE_IMMUTABLE) |
| 8749 | stat->attributes |= STATX_ATTR_IMMUTABLE; |
| 8750 | if (bi_flags & BTRFS_INODE_NODUMP) |
| 8751 | stat->attributes |= STATX_ATTR_NODUMP; |
| 8752 | |
| 8753 | stat->attributes_mask |= (STATX_ATTR_APPEND | |
| 8754 | STATX_ATTR_COMPRESSED | |
| 8755 | STATX_ATTR_IMMUTABLE | |
| 8756 | STATX_ATTR_NODUMP); |
David Sterba | fadc0d8 | 2011-11-20 07:33:38 -0500 | [diff] [blame] | 8757 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8758 | generic_fillattr(inode, stat); |
Al Viro | 0ee5dc6 | 2011-07-07 15:44:25 -0400 | [diff] [blame] | 8759 | stat->dev = BTRFS_I(inode)->root->anon_dev; |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 8760 | |
| 8761 | spin_lock(&BTRFS_I(inode)->lock); |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 8762 | delalloc_bytes = BTRFS_I(inode)->new_delalloc_bytes; |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 8763 | spin_unlock(&BTRFS_I(inode)->lock); |
David Sterba | fadc0d8 | 2011-11-20 07:33:38 -0500 | [diff] [blame] | 8764 | stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) + |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 8765 | ALIGN(delalloc_bytes, blocksize)) >> 9; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8766 | return 0; |
| 8767 | } |
| 8768 | |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 8769 | static int btrfs_rename_exchange(struct inode *old_dir, |
| 8770 | struct dentry *old_dentry, |
| 8771 | struct inode *new_dir, |
| 8772 | struct dentry *new_dentry) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8773 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8774 | struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8775 | struct btrfs_trans_handle *trans; |
| 8776 | struct btrfs_root *root = BTRFS_I(old_dir)->root; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 8777 | struct btrfs_root *dest = BTRFS_I(new_dir)->root; |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 8778 | struct inode *new_inode = new_dentry->d_inode; |
| 8779 | struct inode *old_inode = old_dentry->d_inode; |
Deepa Dinamani | 95582b0 | 2018-05-08 19:36:02 -0700 | [diff] [blame] | 8780 | struct timespec64 ctime = current_time(old_inode); |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 8781 | u64 old_ino = btrfs_ino(BTRFS_I(old_inode)); |
| 8782 | u64 new_ino = btrfs_ino(BTRFS_I(new_inode)); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 8783 | u64 old_idx = 0; |
| 8784 | u64 new_idx = 0; |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 8785 | int ret; |
Filipe Manana | 75b463d | 2020-08-11 12:43:48 +0100 | [diff] [blame] | 8786 | int ret2; |
Filipe Manana | 86e8aa0 | 2016-05-05 02:02:27 +0100 | [diff] [blame] | 8787 | bool root_log_pinned = false; |
| 8788 | bool dest_log_pinned = false; |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 8789 | |
| 8790 | /* we only allow rename subvolume link between subvolumes */ |
| 8791 | if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest) |
| 8792 | return -EXDEV; |
| 8793 | |
| 8794 | /* close the race window with snapshot create/destroy ioctl */ |
Josef Bacik | 943eb3b | 2019-11-19 13:59:20 -0500 | [diff] [blame] | 8795 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID || |
| 8796 | new_ino == BTRFS_FIRST_FREE_OBJECTID) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8797 | down_read(&fs_info->subvol_sem); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 8798 | |
| 8799 | /* |
| 8800 | * We want to reserve the absolute worst case amount of items. So if |
| 8801 | * both inodes are subvols and we need to unlink them then that would |
| 8802 | * require 4 item modifications, but if they are both normal inodes it |
| 8803 | * would require 5 item modifications, so we'll assume their normal |
| 8804 | * inodes. So 5 * 2 is 10, plus 2 for the new links, so 12 total items |
| 8805 | * should cover the worst case number of items we'll modify. |
| 8806 | */ |
| 8807 | trans = btrfs_start_transaction(root, 12); |
| 8808 | if (IS_ERR(trans)) { |
| 8809 | ret = PTR_ERR(trans); |
| 8810 | goto out_notrans; |
| 8811 | } |
| 8812 | |
Josef Bacik | 3e17409 | 2019-11-15 15:43:06 -0500 | [diff] [blame] | 8813 | if (dest != root) |
| 8814 | btrfs_record_root_in_trans(trans, dest); |
| 8815 | |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 8816 | /* |
| 8817 | * We need to find a free sequence number both in the source and |
| 8818 | * in the destination directory for the exchange. |
| 8819 | */ |
Nikolay Borisov | 877574e | 2017-02-20 13:50:33 +0200 | [diff] [blame] | 8820 | ret = btrfs_set_inode_index(BTRFS_I(new_dir), &old_idx); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 8821 | if (ret) |
| 8822 | goto out_fail; |
Nikolay Borisov | 877574e | 2017-02-20 13:50:33 +0200 | [diff] [blame] | 8823 | ret = btrfs_set_inode_index(BTRFS_I(old_dir), &new_idx); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 8824 | if (ret) |
| 8825 | goto out_fail; |
| 8826 | |
| 8827 | BTRFS_I(old_inode)->dir_index = 0ULL; |
| 8828 | BTRFS_I(new_inode)->dir_index = 0ULL; |
| 8829 | |
| 8830 | /* Reference for the source. */ |
| 8831 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) { |
| 8832 | /* force full log commit if subvolume involved. */ |
David Sterba | 9078776 | 2019-03-20 13:28:05 +0100 | [diff] [blame] | 8833 | btrfs_set_log_full_commit(trans); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 8834 | } else { |
Filipe Manana | 376e5a5 | 2016-05-05 02:08:56 +0100 | [diff] [blame] | 8835 | btrfs_pin_log_trans(root); |
| 8836 | root_log_pinned = true; |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 8837 | ret = btrfs_insert_inode_ref(trans, dest, |
| 8838 | new_dentry->d_name.name, |
| 8839 | new_dentry->d_name.len, |
| 8840 | old_ino, |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 8841 | btrfs_ino(BTRFS_I(new_dir)), |
| 8842 | old_idx); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 8843 | if (ret) |
| 8844 | goto out_fail; |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 8845 | } |
| 8846 | |
| 8847 | /* And now for the dest. */ |
| 8848 | if (new_ino == BTRFS_FIRST_FREE_OBJECTID) { |
| 8849 | /* force full log commit if subvolume involved. */ |
David Sterba | 9078776 | 2019-03-20 13:28:05 +0100 | [diff] [blame] | 8850 | btrfs_set_log_full_commit(trans); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 8851 | } else { |
Filipe Manana | 376e5a5 | 2016-05-05 02:08:56 +0100 | [diff] [blame] | 8852 | btrfs_pin_log_trans(dest); |
| 8853 | dest_log_pinned = true; |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 8854 | ret = btrfs_insert_inode_ref(trans, root, |
| 8855 | old_dentry->d_name.name, |
| 8856 | old_dentry->d_name.len, |
| 8857 | new_ino, |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 8858 | btrfs_ino(BTRFS_I(old_dir)), |
| 8859 | new_idx); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 8860 | if (ret) |
| 8861 | goto out_fail; |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 8862 | } |
| 8863 | |
| 8864 | /* Update inode version and ctime/mtime. */ |
| 8865 | inode_inc_iversion(old_dir); |
| 8866 | inode_inc_iversion(new_dir); |
| 8867 | inode_inc_iversion(old_inode); |
| 8868 | inode_inc_iversion(new_inode); |
| 8869 | old_dir->i_ctime = old_dir->i_mtime = ctime; |
| 8870 | new_dir->i_ctime = new_dir->i_mtime = ctime; |
| 8871 | old_inode->i_ctime = ctime; |
| 8872 | new_inode->i_ctime = ctime; |
| 8873 | |
| 8874 | if (old_dentry->d_parent != new_dentry->d_parent) { |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 8875 | btrfs_record_unlink_dir(trans, BTRFS_I(old_dir), |
| 8876 | BTRFS_I(old_inode), 1); |
| 8877 | btrfs_record_unlink_dir(trans, BTRFS_I(new_dir), |
| 8878 | BTRFS_I(new_inode), 1); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 8879 | } |
| 8880 | |
| 8881 | /* src is a subvolume */ |
| 8882 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) { |
Josef Bacik | 045d396 | 2019-12-18 17:20:27 -0500 | [diff] [blame] | 8883 | ret = btrfs_unlink_subvol(trans, old_dir, old_dentry); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 8884 | } else { /* src is an inode */ |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 8885 | ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir), |
| 8886 | BTRFS_I(old_dentry->d_inode), |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 8887 | old_dentry->d_name.name, |
| 8888 | old_dentry->d_name.len); |
| 8889 | if (!ret) |
| 8890 | ret = btrfs_update_inode(trans, root, old_inode); |
| 8891 | } |
| 8892 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 8893 | btrfs_abort_transaction(trans, ret); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 8894 | goto out_fail; |
| 8895 | } |
| 8896 | |
| 8897 | /* dest is a subvolume */ |
| 8898 | if (new_ino == BTRFS_FIRST_FREE_OBJECTID) { |
Josef Bacik | 045d396 | 2019-12-18 17:20:27 -0500 | [diff] [blame] | 8899 | ret = btrfs_unlink_subvol(trans, new_dir, new_dentry); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 8900 | } else { /* dest is an inode */ |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 8901 | ret = __btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir), |
| 8902 | BTRFS_I(new_dentry->d_inode), |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 8903 | new_dentry->d_name.name, |
| 8904 | new_dentry->d_name.len); |
| 8905 | if (!ret) |
| 8906 | ret = btrfs_update_inode(trans, dest, new_inode); |
| 8907 | } |
| 8908 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 8909 | btrfs_abort_transaction(trans, ret); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 8910 | goto out_fail; |
| 8911 | } |
| 8912 | |
Nikolay Borisov | db0a669 | 2017-02-20 13:51:08 +0200 | [diff] [blame] | 8913 | 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] | 8914 | new_dentry->d_name.name, |
| 8915 | new_dentry->d_name.len, 0, old_idx); |
| 8916 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 8917 | btrfs_abort_transaction(trans, ret); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 8918 | goto out_fail; |
| 8919 | } |
| 8920 | |
Nikolay Borisov | db0a669 | 2017-02-20 13:51:08 +0200 | [diff] [blame] | 8921 | 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] | 8922 | old_dentry->d_name.name, |
| 8923 | old_dentry->d_name.len, 0, new_idx); |
| 8924 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 8925 | btrfs_abort_transaction(trans, ret); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 8926 | goto out_fail; |
| 8927 | } |
| 8928 | |
| 8929 | if (old_inode->i_nlink == 1) |
| 8930 | BTRFS_I(old_inode)->dir_index = old_idx; |
| 8931 | if (new_inode->i_nlink == 1) |
| 8932 | BTRFS_I(new_inode)->dir_index = new_idx; |
| 8933 | |
Filipe Manana | 86e8aa0 | 2016-05-05 02:02:27 +0100 | [diff] [blame] | 8934 | if (root_log_pinned) { |
Filipe Manana | 75b463d | 2020-08-11 12:43:48 +0100 | [diff] [blame] | 8935 | btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir), |
| 8936 | new_dentry->d_parent); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 8937 | btrfs_end_log_trans(root); |
Filipe Manana | 86e8aa0 | 2016-05-05 02:02:27 +0100 | [diff] [blame] | 8938 | root_log_pinned = false; |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 8939 | } |
Filipe Manana | 86e8aa0 | 2016-05-05 02:02:27 +0100 | [diff] [blame] | 8940 | if (dest_log_pinned) { |
Filipe Manana | 75b463d | 2020-08-11 12:43:48 +0100 | [diff] [blame] | 8941 | btrfs_log_new_name(trans, BTRFS_I(new_inode), BTRFS_I(new_dir), |
| 8942 | old_dentry->d_parent); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 8943 | btrfs_end_log_trans(dest); |
Filipe Manana | 86e8aa0 | 2016-05-05 02:02:27 +0100 | [diff] [blame] | 8944 | dest_log_pinned = false; |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 8945 | } |
| 8946 | out_fail: |
Filipe Manana | 86e8aa0 | 2016-05-05 02:02:27 +0100 | [diff] [blame] | 8947 | /* |
| 8948 | * If we have pinned a log and an error happened, we unpin tasks |
| 8949 | * trying to sync the log and force them to fallback to a transaction |
| 8950 | * commit if the log currently contains any of the inodes involved in |
| 8951 | * this rename operation (to ensure we do not persist a log with an |
| 8952 | * inconsistent state for any of these inodes or leading to any |
| 8953 | * inconsistencies when replayed). If the transaction was aborted, the |
| 8954 | * abortion reason is propagated to userspace when attempting to commit |
| 8955 | * the transaction. If the log does not contain any of these inodes, we |
| 8956 | * allow the tasks to sync it. |
| 8957 | */ |
| 8958 | if (ret && (root_log_pinned || dest_log_pinned)) { |
Nikolay Borisov | 0f8939b | 2017-01-18 00:31:30 +0200 | [diff] [blame] | 8959 | if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) || |
| 8960 | btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) || |
| 8961 | btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) || |
Filipe Manana | 86e8aa0 | 2016-05-05 02:02:27 +0100 | [diff] [blame] | 8962 | (new_inode && |
Nikolay Borisov | 0f8939b | 2017-01-18 00:31:30 +0200 | [diff] [blame] | 8963 | btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation))) |
David Sterba | 9078776 | 2019-03-20 13:28:05 +0100 | [diff] [blame] | 8964 | btrfs_set_log_full_commit(trans); |
Filipe Manana | 86e8aa0 | 2016-05-05 02:02:27 +0100 | [diff] [blame] | 8965 | |
| 8966 | if (root_log_pinned) { |
| 8967 | btrfs_end_log_trans(root); |
| 8968 | root_log_pinned = false; |
| 8969 | } |
| 8970 | if (dest_log_pinned) { |
| 8971 | btrfs_end_log_trans(dest); |
| 8972 | dest_log_pinned = false; |
| 8973 | } |
| 8974 | } |
Filipe Manana | 75b463d | 2020-08-11 12:43:48 +0100 | [diff] [blame] | 8975 | ret2 = btrfs_end_transaction(trans); |
| 8976 | ret = ret ? ret : ret2; |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 8977 | out_notrans: |
Josef Bacik | 943eb3b | 2019-11-19 13:59:20 -0500 | [diff] [blame] | 8978 | if (new_ino == BTRFS_FIRST_FREE_OBJECTID || |
| 8979 | old_ino == BTRFS_FIRST_FREE_OBJECTID) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8980 | up_read(&fs_info->subvol_sem); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 8981 | |
| 8982 | return ret; |
| 8983 | } |
| 8984 | |
| 8985 | static int btrfs_whiteout_for_rename(struct btrfs_trans_handle *trans, |
| 8986 | struct btrfs_root *root, |
| 8987 | struct inode *dir, |
| 8988 | struct dentry *dentry) |
| 8989 | { |
| 8990 | int ret; |
| 8991 | struct inode *inode; |
| 8992 | u64 objectid; |
| 8993 | u64 index; |
| 8994 | |
| 8995 | ret = btrfs_find_free_ino(root, &objectid); |
| 8996 | if (ret) |
| 8997 | return ret; |
| 8998 | |
| 8999 | inode = btrfs_new_inode(trans, root, dir, |
| 9000 | dentry->d_name.name, |
| 9001 | dentry->d_name.len, |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 9002 | btrfs_ino(BTRFS_I(dir)), |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9003 | objectid, |
| 9004 | S_IFCHR | WHITEOUT_MODE, |
| 9005 | &index); |
| 9006 | |
| 9007 | if (IS_ERR(inode)) { |
| 9008 | ret = PTR_ERR(inode); |
| 9009 | return ret; |
| 9010 | } |
| 9011 | |
| 9012 | inode->i_op = &btrfs_special_inode_operations; |
| 9013 | init_special_inode(inode, inode->i_mode, |
| 9014 | WHITEOUT_DEV); |
| 9015 | |
| 9016 | ret = btrfs_init_inode_security(trans, inode, dir, |
| 9017 | &dentry->d_name); |
| 9018 | if (ret) |
Filipe Manana | c990161 | 2016-05-05 01:41:57 +0100 | [diff] [blame] | 9019 | goto out; |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9020 | |
Nikolay Borisov | cef415a | 2017-02-20 13:51:09 +0200 | [diff] [blame] | 9021 | ret = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, |
| 9022 | BTRFS_I(inode), 0, index); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9023 | if (ret) |
Filipe Manana | c990161 | 2016-05-05 01:41:57 +0100 | [diff] [blame] | 9024 | goto out; |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9025 | |
| 9026 | ret = btrfs_update_inode(trans, root, inode); |
Filipe Manana | c990161 | 2016-05-05 01:41:57 +0100 | [diff] [blame] | 9027 | out: |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9028 | unlock_new_inode(inode); |
Filipe Manana | c990161 | 2016-05-05 01:41:57 +0100 | [diff] [blame] | 9029 | if (ret) |
| 9030 | inode_dec_link_count(inode); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9031 | iput(inode); |
| 9032 | |
Filipe Manana | c990161 | 2016-05-05 01:41:57 +0100 | [diff] [blame] | 9033 | return ret; |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9034 | } |
| 9035 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9036 | static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry, |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9037 | struct inode *new_dir, struct dentry *new_dentry, |
| 9038 | unsigned int flags) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9039 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9040 | struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9041 | struct btrfs_trans_handle *trans; |
Filipe Manana | 5062af3 | 2016-05-05 10:26:26 +0100 | [diff] [blame] | 9042 | unsigned int trans_num_items; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9043 | struct btrfs_root *root = BTRFS_I(old_dir)->root; |
| 9044 | struct btrfs_root *dest = BTRFS_I(new_dir)->root; |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 9045 | struct inode *new_inode = d_inode(new_dentry); |
| 9046 | struct inode *old_inode = d_inode(old_dentry); |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 9047 | u64 index = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9048 | int ret; |
Filipe Manana | 75b463d | 2020-08-11 12:43:48 +0100 | [diff] [blame] | 9049 | int ret2; |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 9050 | u64 old_ino = btrfs_ino(BTRFS_I(old_inode)); |
Filipe Manana | 3dc9e8f7 | 2016-04-29 11:34:22 +0100 | [diff] [blame] | 9051 | bool log_pinned = false; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9052 | |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 9053 | if (btrfs_ino(BTRFS_I(new_dir)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) |
Yan, Zheng | f679a84 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 9054 | return -EPERM; |
| 9055 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9056 | /* we only allow rename subvolume link between subvolumes */ |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 9057 | if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest) |
Chris Mason | 3394e16 | 2008-11-17 20:42:26 -0500 | [diff] [blame] | 9058 | return -EXDEV; |
| 9059 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 9060 | if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID || |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 9061 | (new_inode && btrfs_ino(BTRFS_I(new_inode)) == BTRFS_FIRST_FREE_OBJECTID)) |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9062 | return -ENOTEMPTY; |
| 9063 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9064 | if (S_ISDIR(old_inode->i_mode) && new_inode && |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9065 | new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9066 | return -ENOTEMPTY; |
Chris Mason | 9c52057 | 2012-12-17 14:26:57 -0500 | [diff] [blame] | 9067 | |
| 9068 | |
| 9069 | /* check for collisions, even if the name isn't there */ |
Josef Bacik | 4871c15 | 2013-10-09 12:24:04 -0400 | [diff] [blame] | 9070 | ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino, |
Chris Mason | 9c52057 | 2012-12-17 14:26:57 -0500 | [diff] [blame] | 9071 | new_dentry->d_name.name, |
| 9072 | new_dentry->d_name.len); |
| 9073 | |
| 9074 | if (ret) { |
| 9075 | if (ret == -EEXIST) { |
| 9076 | /* we shouldn't get |
| 9077 | * eexist without a new_inode */ |
Dulshani Gunawardhana | fae7f21 | 2013-10-31 10:30:08 +0530 | [diff] [blame] | 9078 | if (WARN_ON(!new_inode)) { |
Chris Mason | 9c52057 | 2012-12-17 14:26:57 -0500 | [diff] [blame] | 9079 | return ret; |
| 9080 | } |
| 9081 | } else { |
| 9082 | /* maybe -EOVERFLOW */ |
| 9083 | return ret; |
| 9084 | } |
| 9085 | } |
| 9086 | ret = 0; |
| 9087 | |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 9088 | /* |
Chris Mason | 8d875f9 | 2014-08-12 10:47:42 -0700 | [diff] [blame] | 9089 | * we're using rename to replace one file with another. Start IO on it |
| 9090 | * 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] | 9091 | */ |
Chris Mason | 8d875f9 | 2014-08-12 10:47:42 -0700 | [diff] [blame] | 9092 | 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] | 9093 | filemap_flush(old_inode->i_mapping); |
| 9094 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 9095 | /* close the racy window with snapshot create/destroy ioctl */ |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 9096 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9097 | down_read(&fs_info->subvol_sem); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 9098 | /* |
| 9099 | * We want to reserve the absolute worst case amount of items. So if |
| 9100 | * both inodes are subvols and we need to unlink them then that would |
| 9101 | * require 4 item modifications, but if they are both normal inodes it |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9102 | * would require 5 item modifications, so we'll assume they are normal |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 9103 | * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items |
| 9104 | * should cover the worst case number of items we'll modify. |
Filipe Manana | 5062af3 | 2016-05-05 10:26:26 +0100 | [diff] [blame] | 9105 | * If our rename has the whiteout flag, we need more 5 units for the |
| 9106 | * new inode (1 inode item, 1 inode ref, 2 dir items and 1 xattr item |
| 9107 | * when selinux is enabled). |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 9108 | */ |
Filipe Manana | 5062af3 | 2016-05-05 10:26:26 +0100 | [diff] [blame] | 9109 | trans_num_items = 11; |
| 9110 | if (flags & RENAME_WHITEOUT) |
| 9111 | trans_num_items += 5; |
| 9112 | trans = btrfs_start_transaction(root, trans_num_items); |
Johann Lombardi | b44c59a | 2011-03-31 13:23:47 +0000 | [diff] [blame] | 9113 | if (IS_ERR(trans)) { |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9114 | ret = PTR_ERR(trans); |
| 9115 | goto out_notrans; |
| 9116 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 9117 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9118 | if (dest != root) |
| 9119 | btrfs_record_root_in_trans(trans, dest); |
| 9120 | |
Nikolay Borisov | 877574e | 2017-02-20 13:50:33 +0200 | [diff] [blame] | 9121 | ret = btrfs_set_inode_index(BTRFS_I(new_dir), &index); |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 9122 | if (ret) |
| 9123 | goto out_fail; |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 9124 | |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 9125 | BTRFS_I(old_inode)->dir_index = 0ULL; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 9126 | if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9127 | /* force full log commit if subvolume involved. */ |
David Sterba | 9078776 | 2019-03-20 13:28:05 +0100 | [diff] [blame] | 9128 | btrfs_set_log_full_commit(trans); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9129 | } else { |
Filipe Manana | c4aba95 | 2016-04-29 13:14:42 +0100 | [diff] [blame] | 9130 | btrfs_pin_log_trans(root); |
| 9131 | log_pinned = true; |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 9132 | ret = btrfs_insert_inode_ref(trans, dest, |
| 9133 | new_dentry->d_name.name, |
| 9134 | new_dentry->d_name.len, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 9135 | old_ino, |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 9136 | btrfs_ino(BTRFS_I(new_dir)), index); |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 9137 | if (ret) |
| 9138 | goto out_fail; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9139 | } |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 9140 | |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 9141 | inode_inc_iversion(old_dir); |
| 9142 | inode_inc_iversion(new_dir); |
| 9143 | inode_inc_iversion(old_inode); |
Deepa Dinamani | 04b285f | 2016-02-06 23:57:21 -0800 | [diff] [blame] | 9144 | old_dir->i_ctime = old_dir->i_mtime = |
| 9145 | new_dir->i_ctime = new_dir->i_mtime = |
Deepa Dinamani | c2050a4 | 2016-09-14 07:48:06 -0700 | [diff] [blame] | 9146 | old_inode->i_ctime = current_time(old_dir); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 9147 | |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 9148 | if (old_dentry->d_parent != new_dentry->d_parent) |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 9149 | btrfs_record_unlink_dir(trans, BTRFS_I(old_dir), |
| 9150 | BTRFS_I(old_inode), 1); |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 9151 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 9152 | if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) { |
Josef Bacik | 045d396 | 2019-12-18 17:20:27 -0500 | [diff] [blame] | 9153 | ret = btrfs_unlink_subvol(trans, old_dir, old_dentry); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9154 | } else { |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 9155 | ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir), |
| 9156 | BTRFS_I(d_inode(old_dentry)), |
Al Viro | 9298679 | 2011-03-04 17:14:37 +0000 | [diff] [blame] | 9157 | old_dentry->d_name.name, |
| 9158 | old_dentry->d_name.len); |
| 9159 | if (!ret) |
| 9160 | ret = btrfs_update_inode(trans, root, old_inode); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9161 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 9162 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 9163 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 9164 | goto out_fail; |
| 9165 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9166 | |
| 9167 | if (new_inode) { |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 9168 | inode_inc_iversion(new_inode); |
Deepa Dinamani | c2050a4 | 2016-09-14 07:48:06 -0700 | [diff] [blame] | 9169 | new_inode->i_ctime = current_time(new_inode); |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 9170 | if (unlikely(btrfs_ino(BTRFS_I(new_inode)) == |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9171 | BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) { |
Josef Bacik | 045d396 | 2019-12-18 17:20:27 -0500 | [diff] [blame] | 9172 | ret = btrfs_unlink_subvol(trans, new_dir, new_dentry); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9173 | BUG_ON(new_inode->i_nlink == 0); |
| 9174 | } else { |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 9175 | ret = btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir), |
| 9176 | BTRFS_I(d_inode(new_dentry)), |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9177 | new_dentry->d_name.name, |
| 9178 | new_dentry->d_name.len); |
| 9179 | } |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 9180 | if (!ret && new_inode->i_nlink == 0) |
Nikolay Borisov | 73f2e54 | 2017-02-20 13:50:59 +0200 | [diff] [blame] | 9181 | ret = btrfs_orphan_add(trans, |
| 9182 | BTRFS_I(d_inode(new_dentry))); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 9183 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 9184 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 9185 | goto out_fail; |
| 9186 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9187 | } |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 9188 | |
Nikolay Borisov | db0a669 | 2017-02-20 13:51:08 +0200 | [diff] [blame] | 9189 | 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] | 9190 | new_dentry->d_name.name, |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 9191 | new_dentry->d_name.len, 0, index); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 9192 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 9193 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 9194 | goto out_fail; |
| 9195 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9196 | |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 9197 | if (old_inode->i_nlink == 1) |
| 9198 | BTRFS_I(old_inode)->dir_index = index; |
| 9199 | |
Filipe Manana | 3dc9e8f7 | 2016-04-29 11:34:22 +0100 | [diff] [blame] | 9200 | if (log_pinned) { |
Filipe Manana | 75b463d | 2020-08-11 12:43:48 +0100 | [diff] [blame] | 9201 | btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir), |
| 9202 | new_dentry->d_parent); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9203 | btrfs_end_log_trans(root); |
Filipe Manana | 3dc9e8f7 | 2016-04-29 11:34:22 +0100 | [diff] [blame] | 9204 | log_pinned = false; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9205 | } |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9206 | |
| 9207 | if (flags & RENAME_WHITEOUT) { |
| 9208 | ret = btrfs_whiteout_for_rename(trans, root, old_dir, |
| 9209 | old_dentry); |
| 9210 | |
| 9211 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 9212 | btrfs_abort_transaction(trans, ret); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9213 | goto out_fail; |
| 9214 | } |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 9215 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9216 | out_fail: |
Filipe Manana | 3dc9e8f7 | 2016-04-29 11:34:22 +0100 | [diff] [blame] | 9217 | /* |
| 9218 | * If we have pinned the log and an error happened, we unpin tasks |
| 9219 | * trying to sync the log and force them to fallback to a transaction |
| 9220 | * commit if the log currently contains any of the inodes involved in |
| 9221 | * this rename operation (to ensure we do not persist a log with an |
| 9222 | * inconsistent state for any of these inodes or leading to any |
| 9223 | * inconsistencies when replayed). If the transaction was aborted, the |
| 9224 | * abortion reason is propagated to userspace when attempting to commit |
| 9225 | * the transaction. If the log does not contain any of these inodes, we |
| 9226 | * allow the tasks to sync it. |
| 9227 | */ |
| 9228 | if (ret && log_pinned) { |
Nikolay Borisov | 0f8939b | 2017-01-18 00:31:30 +0200 | [diff] [blame] | 9229 | if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) || |
| 9230 | btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) || |
| 9231 | btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) || |
Filipe Manana | 3dc9e8f7 | 2016-04-29 11:34:22 +0100 | [diff] [blame] | 9232 | (new_inode && |
Nikolay Borisov | 0f8939b | 2017-01-18 00:31:30 +0200 | [diff] [blame] | 9233 | btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation))) |
David Sterba | 9078776 | 2019-03-20 13:28:05 +0100 | [diff] [blame] | 9234 | btrfs_set_log_full_commit(trans); |
Filipe Manana | 3dc9e8f7 | 2016-04-29 11:34:22 +0100 | [diff] [blame] | 9235 | |
| 9236 | btrfs_end_log_trans(root); |
| 9237 | log_pinned = false; |
| 9238 | } |
Filipe Manana | 75b463d | 2020-08-11 12:43:48 +0100 | [diff] [blame] | 9239 | ret2 = btrfs_end_transaction(trans); |
| 9240 | ret = ret ? ret : ret2; |
Johann Lombardi | b44c59a | 2011-03-31 13:23:47 +0000 | [diff] [blame] | 9241 | out_notrans: |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 9242 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9243 | up_read(&fs_info->subvol_sem); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 9244 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9245 | return ret; |
| 9246 | } |
| 9247 | |
Miklos Szeredi | 80ace85 | 2014-07-23 15:15:32 +0200 | [diff] [blame] | 9248 | static int btrfs_rename2(struct inode *old_dir, struct dentry *old_dentry, |
| 9249 | struct inode *new_dir, struct dentry *new_dentry, |
| 9250 | unsigned int flags) |
| 9251 | { |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9252 | if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT)) |
Miklos Szeredi | 80ace85 | 2014-07-23 15:15:32 +0200 | [diff] [blame] | 9253 | return -EINVAL; |
| 9254 | |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9255 | if (flags & RENAME_EXCHANGE) |
| 9256 | return btrfs_rename_exchange(old_dir, old_dentry, new_dir, |
| 9257 | new_dentry); |
| 9258 | |
| 9259 | return btrfs_rename(old_dir, old_dentry, new_dir, new_dentry, flags); |
Miklos Szeredi | 80ace85 | 2014-07-23 15:15:32 +0200 | [diff] [blame] | 9260 | } |
| 9261 | |
Nikolay Borisov | 3a2f8c0 | 2018-04-24 17:23:59 +0300 | [diff] [blame] | 9262 | struct btrfs_delalloc_work { |
| 9263 | struct inode *inode; |
| 9264 | struct completion completion; |
| 9265 | struct list_head list; |
| 9266 | struct btrfs_work work; |
| 9267 | }; |
| 9268 | |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 9269 | static void btrfs_run_delalloc_work(struct btrfs_work *work) |
| 9270 | { |
| 9271 | struct btrfs_delalloc_work *delalloc_work; |
Josef Bacik | 9f23e28 | 2013-10-28 15:03:41 -0400 | [diff] [blame] | 9272 | struct inode *inode; |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 9273 | |
| 9274 | delalloc_work = container_of(work, struct btrfs_delalloc_work, |
| 9275 | work); |
Josef Bacik | 9f23e28 | 2013-10-28 15:03:41 -0400 | [diff] [blame] | 9276 | inode = delalloc_work->inode; |
David Sterba | 3042460 | 2015-11-27 19:27:11 +0100 | [diff] [blame] | 9277 | filemap_flush(inode->i_mapping); |
| 9278 | if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, |
| 9279 | &BTRFS_I(inode)->runtime_flags)) |
Josef Bacik | 9f23e28 | 2013-10-28 15:03:41 -0400 | [diff] [blame] | 9280 | filemap_flush(inode->i_mapping); |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 9281 | |
Nikolay Borisov | 076da91 | 2018-04-23 10:54:16 +0300 | [diff] [blame] | 9282 | iput(inode); |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 9283 | complete(&delalloc_work->completion); |
| 9284 | } |
| 9285 | |
Nikolay Borisov | 3a2f8c0 | 2018-04-24 17:23:59 +0300 | [diff] [blame] | 9286 | static struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode) |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 9287 | { |
| 9288 | struct btrfs_delalloc_work *work; |
| 9289 | |
David Sterba | 100d570 | 2015-12-08 14:39:32 +0100 | [diff] [blame] | 9290 | work = kmalloc(sizeof(*work), GFP_NOFS); |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 9291 | if (!work) |
| 9292 | return NULL; |
| 9293 | |
| 9294 | init_completion(&work->completion); |
| 9295 | INIT_LIST_HEAD(&work->list); |
| 9296 | work->inode = inode; |
Omar Sandoval | a0cac0e | 2019-09-16 11:30:57 -0700 | [diff] [blame] | 9297 | btrfs_init_work(&work->work, btrfs_run_delalloc_work, NULL, NULL); |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 9298 | |
| 9299 | return work; |
| 9300 | } |
| 9301 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 9302 | /* |
| 9303 | * some fairly slow code that needs optimization. This walks the list |
| 9304 | * of all the inodes with pending delalloc and forces them to disk. |
| 9305 | */ |
Josef Bacik | b491213 | 2020-07-21 10:22:12 -0400 | [diff] [blame] | 9306 | 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] | 9307 | { |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 9308 | struct btrfs_inode *binode; |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 9309 | struct inode *inode; |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 9310 | struct btrfs_delalloc_work *work, *next; |
| 9311 | struct list_head works; |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 9312 | struct list_head splice; |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 9313 | int ret = 0; |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 9314 | |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 9315 | INIT_LIST_HEAD(&works); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 9316 | INIT_LIST_HEAD(&splice); |
Miao Xie | 63607cc | 2013-01-22 10:50:35 +0000 | [diff] [blame] | 9317 | |
Miao Xie | 573bfb7 | 2014-03-06 13:55:03 +0800 | [diff] [blame] | 9318 | mutex_lock(&root->delalloc_mutex); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9319 | spin_lock(&root->delalloc_lock); |
| 9320 | list_splice_init(&root->delalloc_inodes, &splice); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 9321 | while (!list_empty(&splice)) { |
| 9322 | binode = list_entry(splice.next, struct btrfs_inode, |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 9323 | delalloc_inodes); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 9324 | |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9325 | list_move_tail(&binode->delalloc_inodes, |
| 9326 | &root->delalloc_inodes); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 9327 | inode = igrab(&binode->vfs_inode); |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 9328 | if (!inode) { |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9329 | cond_resched_lock(&root->delalloc_lock); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 9330 | continue; |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 9331 | } |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9332 | spin_unlock(&root->delalloc_lock); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 9333 | |
Ethan Lien | 3cd24c6 | 2018-11-01 14:49:03 +0800 | [diff] [blame] | 9334 | if (snapshot) |
| 9335 | set_bit(BTRFS_INODE_SNAPSHOT_FLUSH, |
| 9336 | &binode->runtime_flags); |
Nikolay Borisov | 076da91 | 2018-04-23 10:54:16 +0300 | [diff] [blame] | 9337 | work = btrfs_alloc_delalloc_work(inode); |
David Sterba | 5d99a998 | 2014-09-29 19:20:37 +0200 | [diff] [blame] | 9338 | if (!work) { |
Nikolay Borisov | 4fbb514 | 2018-04-23 10:54:15 +0300 | [diff] [blame] | 9339 | iput(inode); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 9340 | ret = -ENOMEM; |
Wang Shilong | a1ecaab | 2014-04-02 19:53:32 +0800 | [diff] [blame] | 9341 | goto out; |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 9342 | } |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 9343 | list_add_tail(&work->list, &works); |
Qu Wenruo | a44903a | 2014-02-28 10:46:09 +0800 | [diff] [blame] | 9344 | btrfs_queue_work(root->fs_info->flush_workers, |
| 9345 | &work->work); |
Josef Bacik | b491213 | 2020-07-21 10:22:12 -0400 | [diff] [blame] | 9346 | if (*nr != U64_MAX) { |
| 9347 | (*nr)--; |
| 9348 | if (*nr == 0) |
| 9349 | goto out; |
| 9350 | } |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 9351 | cond_resched(); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9352 | spin_lock(&root->delalloc_lock); |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 9353 | } |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9354 | spin_unlock(&root->delalloc_lock); |
Chris Mason | 8c8bee1 | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 9355 | |
Wang Shilong | a1ecaab | 2014-04-02 19:53:32 +0800 | [diff] [blame] | 9356 | out: |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 9357 | list_for_each_entry_safe(work, next, &works, list) { |
| 9358 | list_del_init(&work->list); |
Nikolay Borisov | 40012f9 | 2018-04-19 10:46:39 +0300 | [diff] [blame] | 9359 | wait_for_completion(&work->completion); |
| 9360 | kfree(work); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 9361 | } |
| 9362 | |
Nikolay Borisov | 81f1d39 | 2018-04-19 10:46:37 +0300 | [diff] [blame] | 9363 | if (!list_empty(&splice)) { |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9364 | spin_lock(&root->delalloc_lock); |
| 9365 | list_splice_tail(&splice, &root->delalloc_inodes); |
| 9366 | spin_unlock(&root->delalloc_lock); |
| 9367 | } |
Miao Xie | 573bfb7 | 2014-03-06 13:55:03 +0800 | [diff] [blame] | 9368 | mutex_unlock(&root->delalloc_mutex); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9369 | return ret; |
| 9370 | } |
| 9371 | |
Ethan Lien | 3cd24c6 | 2018-11-01 14:49:03 +0800 | [diff] [blame] | 9372 | int btrfs_start_delalloc_snapshot(struct btrfs_root *root) |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9373 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9374 | struct btrfs_fs_info *fs_info = root->fs_info; |
Josef Bacik | b491213 | 2020-07-21 10:22:12 -0400 | [diff] [blame] | 9375 | u64 nr = U64_MAX; |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9376 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9377 | if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9378 | return -EROFS; |
| 9379 | |
Josef Bacik | b491213 | 2020-07-21 10:22:12 -0400 | [diff] [blame] | 9380 | return start_delalloc_inodes(root, &nr, true); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9381 | } |
| 9382 | |
Josef Bacik | b491213 | 2020-07-21 10:22:12 -0400 | [diff] [blame] | 9383 | 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] | 9384 | { |
| 9385 | struct btrfs_root *root; |
| 9386 | struct list_head splice; |
| 9387 | int ret; |
| 9388 | |
Wang Shilong | 2c21b4d | 2014-01-14 19:42:20 +0800 | [diff] [blame] | 9389 | if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9390 | return -EROFS; |
| 9391 | |
| 9392 | INIT_LIST_HEAD(&splice); |
| 9393 | |
Miao Xie | 573bfb7 | 2014-03-06 13:55:03 +0800 | [diff] [blame] | 9394 | mutex_lock(&fs_info->delalloc_root_mutex); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9395 | spin_lock(&fs_info->delalloc_root_lock); |
| 9396 | list_splice_init(&fs_info->delalloc_roots, &splice); |
Miao Xie | 6c255e6 | 2014-03-06 13:55:01 +0800 | [diff] [blame] | 9397 | while (!list_empty(&splice) && nr) { |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9398 | root = list_first_entry(&splice, struct btrfs_root, |
| 9399 | delalloc_root); |
Josef Bacik | 0024652 | 2020-01-24 09:33:01 -0500 | [diff] [blame] | 9400 | root = btrfs_grab_root(root); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9401 | BUG_ON(!root); |
| 9402 | list_move_tail(&root->delalloc_root, |
| 9403 | &fs_info->delalloc_roots); |
| 9404 | spin_unlock(&fs_info->delalloc_root_lock); |
| 9405 | |
Josef Bacik | b491213 | 2020-07-21 10:22:12 -0400 | [diff] [blame] | 9406 | ret = start_delalloc_inodes(root, &nr, false); |
Josef Bacik | 0024652 | 2020-01-24 09:33:01 -0500 | [diff] [blame] | 9407 | btrfs_put_root(root); |
Miao Xie | 6c255e6 | 2014-03-06 13:55:01 +0800 | [diff] [blame] | 9408 | if (ret < 0) |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9409 | goto out; |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9410 | spin_lock(&fs_info->delalloc_root_lock); |
| 9411 | } |
| 9412 | spin_unlock(&fs_info->delalloc_root_lock); |
| 9413 | |
Miao Xie | 6c255e6 | 2014-03-06 13:55:01 +0800 | [diff] [blame] | 9414 | ret = 0; |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 9415 | out: |
Nikolay Borisov | 81f1d39 | 2018-04-19 10:46:37 +0300 | [diff] [blame] | 9416 | if (!list_empty(&splice)) { |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9417 | spin_lock(&fs_info->delalloc_root_lock); |
| 9418 | list_splice_tail(&splice, &fs_info->delalloc_roots); |
| 9419 | spin_unlock(&fs_info->delalloc_root_lock); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 9420 | } |
Miao Xie | 573bfb7 | 2014-03-06 13:55:03 +0800 | [diff] [blame] | 9421 | mutex_unlock(&fs_info->delalloc_root_mutex); |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 9422 | return ret; |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 9423 | } |
| 9424 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9425 | static int btrfs_symlink(struct inode *dir, struct dentry *dentry, |
| 9426 | const char *symname) |
| 9427 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9428 | struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9429 | struct btrfs_trans_handle *trans; |
| 9430 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 9431 | struct btrfs_path *path; |
| 9432 | struct btrfs_key key; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 9433 | struct inode *inode = NULL; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9434 | int err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9435 | u64 objectid; |
Dulshani Gunawardhana | 6787125 | 2013-10-31 10:33:04 +0530 | [diff] [blame] | 9436 | u64 index = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9437 | int name_len; |
| 9438 | int datasize; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 9439 | unsigned long ptr; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9440 | struct btrfs_file_extent_item *ei; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 9441 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9442 | |
Filipe David Borba Manana | f06becc | 2013-09-16 09:53:28 +0100 | [diff] [blame] | 9443 | name_len = strlen(symname); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9444 | if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info)) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9445 | return -ENAMETOOLONG; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 9446 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 9447 | /* |
| 9448 | * 2 items for inode item and ref |
| 9449 | * 2 items for dir items |
Filipe Manana | 9269d12 | 2015-12-31 18:16:29 +0000 | [diff] [blame] | 9450 | * 1 item for updating parent inode item |
| 9451 | * 1 item for the inline extent item |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 9452 | * 1 item for xattr if selinux is on |
| 9453 | */ |
Filipe Manana | 9269d12 | 2015-12-31 18:16:29 +0000 | [diff] [blame] | 9454 | trans = btrfs_start_transaction(root, 7); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 9455 | if (IS_ERR(trans)) |
| 9456 | return PTR_ERR(trans); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 9457 | |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 9458 | err = btrfs_find_free_ino(root, &objectid); |
| 9459 | if (err) |
| 9460 | goto out_unlock; |
| 9461 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 9462 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 9463 | dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), |
| 9464 | objectid, S_IFLNK|S_IRWXUGO, &index); |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 9465 | if (IS_ERR(inode)) { |
| 9466 | err = PTR_ERR(inode); |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 9467 | inode = NULL; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9468 | goto out_unlock; |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 9469 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9470 | |
Casey Schaufler | ad19db7 | 2011-12-15 10:09:07 -0500 | [diff] [blame] | 9471 | /* |
| 9472 | * If the active LSM wants to access the inode during |
| 9473 | * d_instantiate it needs these. Smack checks to see |
| 9474 | * if the filesystem supports xattrs by looking at the |
| 9475 | * ops vector. |
| 9476 | */ |
| 9477 | inode->i_fop = &btrfs_file_operations; |
| 9478 | inode->i_op = &btrfs_file_inode_operations; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 9479 | inode->i_mapping->a_ops = &btrfs_aops; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 9480 | |
| 9481 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); |
| 9482 | if (err) |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 9483 | goto out_unlock; |
Casey Schaufler | ad19db7 | 2011-12-15 10:09:07 -0500 | [diff] [blame] | 9484 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9485 | path = btrfs_alloc_path(); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 9486 | if (!path) { |
| 9487 | err = -ENOMEM; |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 9488 | goto out_unlock; |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 9489 | } |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 9490 | key.objectid = btrfs_ino(BTRFS_I(inode)); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9491 | key.offset = 0; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 9492 | key.type = BTRFS_EXTENT_DATA_KEY; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9493 | datasize = btrfs_file_extent_calc_inline_size(name_len); |
| 9494 | err = btrfs_insert_empty_item(trans, root, path, &key, |
| 9495 | datasize); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 9496 | if (err) { |
Julia Lawall | b083916 | 2011-05-14 07:10:51 +0000 | [diff] [blame] | 9497 | btrfs_free_path(path); |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 9498 | goto out_unlock; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 9499 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 9500 | leaf = path->nodes[0]; |
| 9501 | ei = btrfs_item_ptr(leaf, path->slots[0], |
| 9502 | struct btrfs_file_extent_item); |
| 9503 | btrfs_set_file_extent_generation(leaf, ei, trans->transid); |
| 9504 | btrfs_set_file_extent_type(leaf, ei, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9505 | BTRFS_FILE_EXTENT_INLINE); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 9506 | btrfs_set_file_extent_encryption(leaf, ei, 0); |
| 9507 | btrfs_set_file_extent_compression(leaf, ei, 0); |
| 9508 | btrfs_set_file_extent_other_encoding(leaf, ei, 0); |
| 9509 | btrfs_set_file_extent_ram_bytes(leaf, ei, name_len); |
| 9510 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9511 | ptr = btrfs_file_extent_inline_start(ei); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 9512 | write_extent_buffer(leaf, symname, ptr, name_len); |
| 9513 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9514 | btrfs_free_path(path); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 9515 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9516 | inode->i_op = &btrfs_symlink_inode_operations; |
Al Viro | 21fc61c | 2015-11-17 01:07:57 -0500 | [diff] [blame] | 9517 | inode_nohighmem(inode); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 9518 | inode_set_bytes(inode, name_len); |
Nikolay Borisov | 6ef06d2 | 2017-02-20 13:50:34 +0200 | [diff] [blame] | 9519 | btrfs_i_size_write(BTRFS_I(inode), name_len); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 9520 | err = btrfs_update_inode(trans, root, inode); |
Filipe Manana | d50866d | 2015-12-31 18:08:24 +0000 | [diff] [blame] | 9521 | /* |
| 9522 | * Last step, add directory indexes for our symlink inode. This is the |
| 9523 | * last step to avoid extra cleanup of these indexes if an error happens |
| 9524 | * elsewhere above. |
| 9525 | */ |
| 9526 | if (!err) |
Nikolay Borisov | cef415a | 2017-02-20 13:51:09 +0200 | [diff] [blame] | 9527 | err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, |
| 9528 | BTRFS_I(inode), 0, index); |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 9529 | if (err) |
| 9530 | goto out_unlock; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 9531 | |
Al Viro | 1e2e547 | 2018-05-04 08:23:01 -0400 | [diff] [blame] | 9532 | d_instantiate_new(dentry, inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9533 | |
| 9534 | out_unlock: |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 9535 | btrfs_end_transaction(trans); |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 9536 | if (err && inode) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9537 | inode_dec_link_count(inode); |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 9538 | discard_new_inode(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9539 | } |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 9540 | btrfs_btree_balance_dirty(fs_info); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9541 | return err; |
| 9542 | } |
Chris Mason | 1643298 | 2008-04-10 10:23:21 -0400 | [diff] [blame] | 9543 | |
Filipe Manana | 8fccebf | 2020-09-08 11:27:20 +0100 | [diff] [blame] | 9544 | static struct btrfs_trans_handle *insert_prealloc_file_extent( |
| 9545 | struct btrfs_trans_handle *trans_in, |
Qu Wenruo | 203f44c5 | 2020-06-10 09:04:40 +0800 | [diff] [blame] | 9546 | struct inode *inode, struct btrfs_key *ins, |
| 9547 | u64 file_offset) |
| 9548 | { |
| 9549 | struct btrfs_file_extent_item stack_fi; |
Filipe Manana | bf38564 | 2020-09-08 11:27:22 +0100 | [diff] [blame] | 9550 | struct btrfs_replace_extent_info extent_info; |
Filipe Manana | 8fccebf | 2020-09-08 11:27:20 +0100 | [diff] [blame] | 9551 | struct btrfs_trans_handle *trans = trans_in; |
| 9552 | struct btrfs_path *path; |
Qu Wenruo | 203f44c5 | 2020-06-10 09:04:40 +0800 | [diff] [blame] | 9553 | u64 start = ins->objectid; |
| 9554 | u64 len = ins->offset; |
Qu Wenruo | 9729f10 | 2020-06-10 09:04:41 +0800 | [diff] [blame] | 9555 | int ret; |
Qu Wenruo | 203f44c5 | 2020-06-10 09:04:40 +0800 | [diff] [blame] | 9556 | |
| 9557 | memset(&stack_fi, 0, sizeof(stack_fi)); |
| 9558 | |
| 9559 | btrfs_set_stack_file_extent_type(&stack_fi, BTRFS_FILE_EXTENT_PREALLOC); |
| 9560 | btrfs_set_stack_file_extent_disk_bytenr(&stack_fi, start); |
| 9561 | btrfs_set_stack_file_extent_disk_num_bytes(&stack_fi, len); |
| 9562 | btrfs_set_stack_file_extent_num_bytes(&stack_fi, len); |
| 9563 | btrfs_set_stack_file_extent_ram_bytes(&stack_fi, len); |
| 9564 | btrfs_set_stack_file_extent_compression(&stack_fi, BTRFS_COMPRESS_NONE); |
| 9565 | /* Encryption and other encoding is reserved and all 0 */ |
| 9566 | |
Nikolay Borisov | 72b7d15 | 2020-06-03 08:55:18 +0300 | [diff] [blame] | 9567 | ret = btrfs_qgroup_release_data(BTRFS_I(inode), file_offset, len); |
Qu Wenruo | 9729f10 | 2020-06-10 09:04:41 +0800 | [diff] [blame] | 9568 | if (ret < 0) |
Filipe Manana | 8fccebf | 2020-09-08 11:27:20 +0100 | [diff] [blame] | 9569 | return ERR_PTR(ret); |
| 9570 | |
| 9571 | if (trans) { |
| 9572 | ret = insert_reserved_file_extent(trans, BTRFS_I(inode), |
| 9573 | file_offset, &stack_fi, ret); |
| 9574 | if (ret) |
| 9575 | return ERR_PTR(ret); |
| 9576 | return trans; |
| 9577 | } |
| 9578 | |
| 9579 | extent_info.disk_offset = start; |
| 9580 | extent_info.disk_len = len; |
| 9581 | extent_info.data_offset = 0; |
| 9582 | extent_info.data_len = len; |
| 9583 | extent_info.file_offset = file_offset; |
| 9584 | extent_info.extent_buf = (char *)&stack_fi; |
Filipe Manana | 8fccebf | 2020-09-08 11:27:20 +0100 | [diff] [blame] | 9585 | extent_info.is_new_extent = true; |
| 9586 | extent_info.qgroup_reserved = ret; |
| 9587 | extent_info.insertions = 0; |
| 9588 | |
| 9589 | path = btrfs_alloc_path(); |
| 9590 | if (!path) |
| 9591 | return ERR_PTR(-ENOMEM); |
| 9592 | |
Filipe Manana | 306bfec | 2020-09-08 11:27:23 +0100 | [diff] [blame] | 9593 | ret = btrfs_replace_file_extents(inode, path, file_offset, |
Filipe Manana | 8fccebf | 2020-09-08 11:27:20 +0100 | [diff] [blame] | 9594 | file_offset + len - 1, &extent_info, |
| 9595 | &trans); |
| 9596 | btrfs_free_path(path); |
| 9597 | if (ret) |
| 9598 | return ERR_PTR(ret); |
| 9599 | |
| 9600 | return trans; |
Qu Wenruo | 203f44c5 | 2020-06-10 09:04:40 +0800 | [diff] [blame] | 9601 | } |
Filipe Manana | 8fccebf | 2020-09-08 11:27:20 +0100 | [diff] [blame] | 9602 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 9603 | static int __btrfs_prealloc_file_range(struct inode *inode, int mode, |
| 9604 | u64 start, u64 num_bytes, u64 min_size, |
| 9605 | loff_t actual_len, u64 *alloc_hint, |
| 9606 | struct btrfs_trans_handle *trans) |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 9607 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9608 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 9609 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
| 9610 | struct extent_map *em; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 9611 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 9612 | struct btrfs_key ins; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 9613 | u64 cur_offset = start; |
Josef Bacik | b778cf9 | 2020-02-13 10:47:31 -0500 | [diff] [blame] | 9614 | u64 clear_offset = start; |
Josef Bacik | 55a61d1 | 2010-11-22 18:50:32 +0000 | [diff] [blame] | 9615 | u64 i_size; |
Chris Mason | 154ea28 | 2013-03-05 11:11:26 -0500 | [diff] [blame] | 9616 | u64 cur_bytes; |
Josef Bacik | 0b670dc | 2015-09-23 17:11:16 -0400 | [diff] [blame] | 9617 | u64 last_alloc = (u64)-1; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 9618 | int ret = 0; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 9619 | bool own_trans = true; |
Wang Xiaoguang | 1851309 | 2016-07-25 15:51:40 +0800 | [diff] [blame] | 9620 | u64 end = start + num_bytes - 1; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 9621 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 9622 | if (trans) |
| 9623 | own_trans = false; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 9624 | while (num_bytes > 0) { |
Byongho Lee | ee22184 | 2015-12-15 01:42:10 +0900 | [diff] [blame] | 9625 | cur_bytes = min_t(u64, num_bytes, SZ_256M); |
Chris Mason | 154ea28 | 2013-03-05 11:11:26 -0500 | [diff] [blame] | 9626 | cur_bytes = max(cur_bytes, min_size); |
Josef Bacik | 0b670dc | 2015-09-23 17:11:16 -0400 | [diff] [blame] | 9627 | /* |
| 9628 | * If we are severely fragmented we could end up with really |
| 9629 | * small allocations, so if the allocator is returning small |
| 9630 | * chunks lets make its job easier by only searching for those |
| 9631 | * sized chunks. |
| 9632 | */ |
| 9633 | cur_bytes = min(cur_bytes, last_alloc); |
Wang Xiaoguang | 1851309 | 2016-07-25 15:51:40 +0800 | [diff] [blame] | 9634 | ret = btrfs_reserve_extent(root, cur_bytes, cur_bytes, |
| 9635 | min_size, 0, *alloc_hint, &ins, 1, 0); |
Filipe Manana | 8fccebf | 2020-09-08 11:27:20 +0100 | [diff] [blame] | 9636 | if (ret) |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 9637 | break; |
Josef Bacik | b778cf9 | 2020-02-13 10:47:31 -0500 | [diff] [blame] | 9638 | |
| 9639 | /* |
| 9640 | * We've reserved this space, and thus converted it from |
| 9641 | * ->bytes_may_use to ->bytes_reserved. Any error that happens |
| 9642 | * from here on out we will only need to clear our reservation |
| 9643 | * for the remaining unreserved area, so advance our |
| 9644 | * clear_offset by our extent size. |
| 9645 | */ |
| 9646 | clear_offset += ins.offset; |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 9647 | |
Josef Bacik | 0b670dc | 2015-09-23 17:11:16 -0400 | [diff] [blame] | 9648 | last_alloc = ins.offset; |
Filipe Manana | 8fccebf | 2020-09-08 11:27:20 +0100 | [diff] [blame] | 9649 | trans = insert_prealloc_file_extent(trans, inode, &ins, cur_offset); |
Filipe Manana | 1afc708 | 2020-10-14 10:10:36 +0100 | [diff] [blame] | 9650 | /* |
| 9651 | * Now that we inserted the prealloc extent we can finally |
| 9652 | * decrement the number of reservations in the block group. |
| 9653 | * If we did it before, we could race with relocation and have |
| 9654 | * relocation miss the reserved extent, making it fail later. |
| 9655 | */ |
| 9656 | btrfs_dec_block_group_reservations(fs_info, ins.objectid); |
Filipe Manana | 8fccebf | 2020-09-08 11:27:20 +0100 | [diff] [blame] | 9657 | if (IS_ERR(trans)) { |
| 9658 | ret = PTR_ERR(trans); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 9659 | btrfs_free_reserved_extent(fs_info, ins.objectid, |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 9660 | ins.offset, 0); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 9661 | break; |
| 9662 | } |
Dongsheng Yang | 3119321 | 2014-12-12 16:44:35 +0800 | [diff] [blame] | 9663 | |
Nikolay Borisov | dcdbc05 | 2017-02-20 13:50:45 +0200 | [diff] [blame] | 9664 | btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset, |
Chris Mason | a1ed835 | 2009-09-11 12:27:37 -0400 | [diff] [blame] | 9665 | cur_offset + ins.offset -1, 0); |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 9666 | |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 9667 | em = alloc_extent_map(); |
| 9668 | if (!em) { |
| 9669 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, |
| 9670 | &BTRFS_I(inode)->runtime_flags); |
| 9671 | goto next; |
| 9672 | } |
| 9673 | |
| 9674 | em->start = cur_offset; |
| 9675 | em->orig_start = cur_offset; |
| 9676 | em->len = ins.offset; |
| 9677 | em->block_start = ins.objectid; |
| 9678 | em->block_len = ins.offset; |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 9679 | em->orig_block_len = ins.offset; |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 9680 | em->ram_bytes = ins.offset; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 9681 | set_bit(EXTENT_FLAG_PREALLOC, &em->flags); |
| 9682 | em->generation = trans->transid; |
| 9683 | |
| 9684 | while (1) { |
| 9685 | write_lock(&em_tree->lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 9686 | ret = add_extent_mapping(em_tree, em, 1); |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 9687 | write_unlock(&em_tree->lock); |
| 9688 | if (ret != -EEXIST) |
| 9689 | break; |
Nikolay Borisov | dcdbc05 | 2017-02-20 13:50:45 +0200 | [diff] [blame] | 9690 | btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset, |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 9691 | cur_offset + ins.offset - 1, |
| 9692 | 0); |
| 9693 | } |
| 9694 | free_extent_map(em); |
| 9695 | next: |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 9696 | num_bytes -= ins.offset; |
| 9697 | cur_offset += ins.offset; |
Yan, Zheng | efa5646 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 9698 | *alloc_hint = ins.objectid + ins.offset; |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 9699 | |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 9700 | inode_inc_iversion(inode); |
Deepa Dinamani | c2050a4 | 2016-09-14 07:48:06 -0700 | [diff] [blame] | 9701 | inode->i_ctime = current_time(inode); |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 9702 | BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 9703 | if (!(mode & FALLOC_FL_KEEP_SIZE) && |
Yan, Zheng | efa5646 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 9704 | (actual_len > inode->i_size) && |
| 9705 | (cur_offset > inode->i_size)) { |
Aneesh Kumar K.V | d1ea6a6 | 2010-01-20 07:28:54 +0000 | [diff] [blame] | 9706 | if (cur_offset > actual_len) |
Josef Bacik | 55a61d1 | 2010-11-22 18:50:32 +0000 | [diff] [blame] | 9707 | i_size = actual_len; |
Aneesh Kumar K.V | d1ea6a6 | 2010-01-20 07:28:54 +0000 | [diff] [blame] | 9708 | else |
Josef Bacik | 55a61d1 | 2010-11-22 18:50:32 +0000 | [diff] [blame] | 9709 | i_size = cur_offset; |
| 9710 | i_size_write(inode, i_size); |
Josef Bacik | d923afe | 2020-01-17 09:02:23 -0500 | [diff] [blame] | 9711 | btrfs_inode_safe_disk_i_size_write(inode, 0); |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 9712 | } |
| 9713 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 9714 | ret = btrfs_update_inode(trans, root, inode); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 9715 | |
| 9716 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 9717 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 9718 | if (own_trans) |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 9719 | btrfs_end_transaction(trans); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 9720 | break; |
| 9721 | } |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 9722 | |
Filipe Manana | 8fccebf | 2020-09-08 11:27:20 +0100 | [diff] [blame] | 9723 | if (own_trans) { |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 9724 | btrfs_end_transaction(trans); |
Filipe Manana | 8fccebf | 2020-09-08 11:27:20 +0100 | [diff] [blame] | 9725 | trans = NULL; |
| 9726 | } |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 9727 | } |
Josef Bacik | b778cf9 | 2020-02-13 10:47:31 -0500 | [diff] [blame] | 9728 | if (clear_offset < end) |
Nikolay Borisov | 25ce28c | 2020-06-03 08:55:39 +0300 | [diff] [blame] | 9729 | btrfs_free_reserved_data_space(BTRFS_I(inode), NULL, clear_offset, |
Josef Bacik | b778cf9 | 2020-02-13 10:47:31 -0500 | [diff] [blame] | 9730 | end - clear_offset + 1); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 9731 | return ret; |
| 9732 | } |
| 9733 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 9734 | int btrfs_prealloc_file_range(struct inode *inode, int mode, |
| 9735 | u64 start, u64 num_bytes, u64 min_size, |
| 9736 | loff_t actual_len, u64 *alloc_hint) |
| 9737 | { |
| 9738 | return __btrfs_prealloc_file_range(inode, mode, start, num_bytes, |
| 9739 | min_size, actual_len, alloc_hint, |
| 9740 | NULL); |
| 9741 | } |
| 9742 | |
| 9743 | int btrfs_prealloc_file_range_trans(struct inode *inode, |
| 9744 | struct btrfs_trans_handle *trans, int mode, |
| 9745 | u64 start, u64 num_bytes, u64 min_size, |
| 9746 | loff_t actual_len, u64 *alloc_hint) |
| 9747 | { |
| 9748 | return __btrfs_prealloc_file_range(inode, mode, start, num_bytes, |
| 9749 | min_size, actual_len, alloc_hint, trans); |
| 9750 | } |
| 9751 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 9752 | static int btrfs_set_page_dirty(struct page *page) |
| 9753 | { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 9754 | return __set_page_dirty_nobuffers(page); |
| 9755 | } |
| 9756 | |
Al Viro | 10556cb2 | 2011-06-20 19:28:19 -0400 | [diff] [blame] | 9757 | static int btrfs_permission(struct inode *inode, int mask) |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 9758 | { |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 9759 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Jeff Mahoney | cb6db4e | 2011-08-15 17:27:21 +0000 | [diff] [blame] | 9760 | umode_t mode = inode->i_mode; |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 9761 | |
Jeff Mahoney | cb6db4e | 2011-08-15 17:27:21 +0000 | [diff] [blame] | 9762 | if (mask & MAY_WRITE && |
| 9763 | (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) { |
| 9764 | if (btrfs_root_readonly(root)) |
| 9765 | return -EROFS; |
| 9766 | if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY) |
| 9767 | return -EACCES; |
| 9768 | } |
Al Viro | 2830ba7 | 2011-06-20 19:16:29 -0400 | [diff] [blame] | 9769 | return generic_permission(inode, mask); |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 9770 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9771 | |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 9772 | static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode) |
| 9773 | { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 9774 | struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 9775 | struct btrfs_trans_handle *trans; |
| 9776 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 9777 | struct inode *inode = NULL; |
| 9778 | u64 objectid; |
| 9779 | u64 index; |
| 9780 | int ret = 0; |
| 9781 | |
| 9782 | /* |
| 9783 | * 5 units required for adding orphan entry |
| 9784 | */ |
| 9785 | trans = btrfs_start_transaction(root, 5); |
| 9786 | if (IS_ERR(trans)) |
| 9787 | return PTR_ERR(trans); |
| 9788 | |
| 9789 | ret = btrfs_find_free_ino(root, &objectid); |
| 9790 | if (ret) |
| 9791 | goto out; |
| 9792 | |
| 9793 | inode = btrfs_new_inode(trans, root, dir, NULL, 0, |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 9794 | btrfs_ino(BTRFS_I(dir)), objectid, mode, &index); |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 9795 | if (IS_ERR(inode)) { |
| 9796 | ret = PTR_ERR(inode); |
| 9797 | inode = NULL; |
| 9798 | goto out; |
| 9799 | } |
| 9800 | |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 9801 | inode->i_fop = &btrfs_file_operations; |
| 9802 | inode->i_op = &btrfs_file_inode_operations; |
| 9803 | |
| 9804 | inode->i_mapping->a_ops = &btrfs_aops; |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 9805 | |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 9806 | ret = btrfs_init_inode_security(trans, inode, dir, NULL); |
| 9807 | if (ret) |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 9808 | goto out; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 9809 | |
| 9810 | ret = btrfs_update_inode(trans, root, inode); |
| 9811 | if (ret) |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 9812 | goto out; |
Nikolay Borisov | 73f2e54 | 2017-02-20 13:50:59 +0200 | [diff] [blame] | 9813 | ret = btrfs_orphan_add(trans, BTRFS_I(inode)); |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 9814 | if (ret) |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 9815 | goto out; |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 9816 | |
Filipe Manana | 5762b5c | 2014-08-01 00:10:32 +0100 | [diff] [blame] | 9817 | /* |
| 9818 | * We set number of links to 0 in btrfs_new_inode(), and here we set |
| 9819 | * it to 1 because d_tmpfile() will issue a warning if the count is 0, |
| 9820 | * through: |
| 9821 | * |
| 9822 | * d_tmpfile() -> inode_dec_link_count() -> drop_nlink() |
| 9823 | */ |
| 9824 | set_nlink(inode, 1); |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 9825 | d_tmpfile(dentry, inode); |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 9826 | unlock_new_inode(inode); |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 9827 | mark_inode_dirty(inode); |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 9828 | out: |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 9829 | btrfs_end_transaction(trans); |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 9830 | if (ret && inode) |
| 9831 | discard_new_inode(inode); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 9832 | btrfs_btree_balance_dirty(fs_info); |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 9833 | return ret; |
| 9834 | } |
| 9835 | |
David Sterba | 5cdc84b | 2018-07-18 20:32:52 +0200 | [diff] [blame] | 9836 | 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] | 9837 | { |
David Sterba | 5cdc84b | 2018-07-18 20:32:52 +0200 | [diff] [blame] | 9838 | struct inode *inode = tree->private_data; |
Josef Bacik | c6100a4 | 2017-05-05 11:57:13 -0400 | [diff] [blame] | 9839 | unsigned long index = start >> PAGE_SHIFT; |
| 9840 | unsigned long end_index = end >> PAGE_SHIFT; |
| 9841 | struct page *page; |
| 9842 | |
| 9843 | while (index <= end_index) { |
| 9844 | page = find_get_page(inode->i_mapping, index); |
| 9845 | ASSERT(page); /* Pages should be in the extent_io_tree */ |
| 9846 | set_page_writeback(page); |
| 9847 | put_page(page); |
| 9848 | index++; |
| 9849 | } |
| 9850 | } |
| 9851 | |
Omar Sandoval | ed46ff3 | 2016-11-03 10:28:14 -0700 | [diff] [blame] | 9852 | #ifdef CONFIG_SWAP |
| 9853 | /* |
| 9854 | * Add an entry indicating a block group or device which is pinned by a |
| 9855 | * swapfile. Returns 0 on success, 1 if there is already an entry for it, or a |
| 9856 | * negative errno on failure. |
| 9857 | */ |
| 9858 | static int btrfs_add_swapfile_pin(struct inode *inode, void *ptr, |
| 9859 | bool is_block_group) |
| 9860 | { |
| 9861 | struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info; |
| 9862 | struct btrfs_swapfile_pin *sp, *entry; |
| 9863 | struct rb_node **p; |
| 9864 | struct rb_node *parent = NULL; |
| 9865 | |
| 9866 | sp = kmalloc(sizeof(*sp), GFP_NOFS); |
| 9867 | if (!sp) |
| 9868 | return -ENOMEM; |
| 9869 | sp->ptr = ptr; |
| 9870 | sp->inode = inode; |
| 9871 | sp->is_block_group = is_block_group; |
| 9872 | |
| 9873 | spin_lock(&fs_info->swapfile_pins_lock); |
| 9874 | p = &fs_info->swapfile_pins.rb_node; |
| 9875 | while (*p) { |
| 9876 | parent = *p; |
| 9877 | entry = rb_entry(parent, struct btrfs_swapfile_pin, node); |
| 9878 | if (sp->ptr < entry->ptr || |
| 9879 | (sp->ptr == entry->ptr && sp->inode < entry->inode)) { |
| 9880 | p = &(*p)->rb_left; |
| 9881 | } else if (sp->ptr > entry->ptr || |
| 9882 | (sp->ptr == entry->ptr && sp->inode > entry->inode)) { |
| 9883 | p = &(*p)->rb_right; |
| 9884 | } else { |
| 9885 | spin_unlock(&fs_info->swapfile_pins_lock); |
| 9886 | kfree(sp); |
| 9887 | return 1; |
| 9888 | } |
| 9889 | } |
| 9890 | rb_link_node(&sp->node, parent, p); |
| 9891 | rb_insert_color(&sp->node, &fs_info->swapfile_pins); |
| 9892 | spin_unlock(&fs_info->swapfile_pins_lock); |
| 9893 | return 0; |
| 9894 | } |
| 9895 | |
| 9896 | /* Free all of the entries pinned by this swapfile. */ |
| 9897 | static void btrfs_free_swapfile_pins(struct inode *inode) |
| 9898 | { |
| 9899 | struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info; |
| 9900 | struct btrfs_swapfile_pin *sp; |
| 9901 | struct rb_node *node, *next; |
| 9902 | |
| 9903 | spin_lock(&fs_info->swapfile_pins_lock); |
| 9904 | node = rb_first(&fs_info->swapfile_pins); |
| 9905 | while (node) { |
| 9906 | next = rb_next(node); |
| 9907 | sp = rb_entry(node, struct btrfs_swapfile_pin, node); |
| 9908 | if (sp->inode == inode) { |
| 9909 | rb_erase(&sp->node, &fs_info->swapfile_pins); |
| 9910 | if (sp->is_block_group) |
| 9911 | btrfs_put_block_group(sp->ptr); |
| 9912 | kfree(sp); |
| 9913 | } |
| 9914 | node = next; |
| 9915 | } |
| 9916 | spin_unlock(&fs_info->swapfile_pins_lock); |
| 9917 | } |
| 9918 | |
| 9919 | struct btrfs_swap_info { |
| 9920 | u64 start; |
| 9921 | u64 block_start; |
| 9922 | u64 block_len; |
| 9923 | u64 lowest_ppage; |
| 9924 | u64 highest_ppage; |
| 9925 | unsigned long nr_pages; |
| 9926 | int nr_extents; |
| 9927 | }; |
| 9928 | |
| 9929 | static int btrfs_add_swap_extent(struct swap_info_struct *sis, |
| 9930 | struct btrfs_swap_info *bsi) |
| 9931 | { |
| 9932 | unsigned long nr_pages; |
| 9933 | u64 first_ppage, first_ppage_reported, next_ppage; |
| 9934 | int ret; |
| 9935 | |
| 9936 | first_ppage = ALIGN(bsi->block_start, PAGE_SIZE) >> PAGE_SHIFT; |
| 9937 | next_ppage = ALIGN_DOWN(bsi->block_start + bsi->block_len, |
| 9938 | PAGE_SIZE) >> PAGE_SHIFT; |
| 9939 | |
| 9940 | if (first_ppage >= next_ppage) |
| 9941 | return 0; |
| 9942 | nr_pages = next_ppage - first_ppage; |
| 9943 | |
| 9944 | first_ppage_reported = first_ppage; |
| 9945 | if (bsi->start == 0) |
| 9946 | first_ppage_reported++; |
| 9947 | if (bsi->lowest_ppage > first_ppage_reported) |
| 9948 | bsi->lowest_ppage = first_ppage_reported; |
| 9949 | if (bsi->highest_ppage < (next_ppage - 1)) |
| 9950 | bsi->highest_ppage = next_ppage - 1; |
| 9951 | |
| 9952 | ret = add_swap_extent(sis, bsi->nr_pages, nr_pages, first_ppage); |
| 9953 | if (ret < 0) |
| 9954 | return ret; |
| 9955 | bsi->nr_extents += ret; |
| 9956 | bsi->nr_pages += nr_pages; |
| 9957 | return 0; |
| 9958 | } |
| 9959 | |
| 9960 | static void btrfs_swap_deactivate(struct file *file) |
| 9961 | { |
| 9962 | struct inode *inode = file_inode(file); |
| 9963 | |
| 9964 | btrfs_free_swapfile_pins(inode); |
| 9965 | atomic_dec(&BTRFS_I(inode)->root->nr_swapfiles); |
| 9966 | } |
| 9967 | |
| 9968 | static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file, |
| 9969 | sector_t *span) |
| 9970 | { |
| 9971 | struct inode *inode = file_inode(file); |
| 9972 | struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info; |
| 9973 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
| 9974 | struct extent_state *cached_state = NULL; |
| 9975 | struct extent_map *em = NULL; |
| 9976 | struct btrfs_device *device = NULL; |
| 9977 | struct btrfs_swap_info bsi = { |
| 9978 | .lowest_ppage = (sector_t)-1ULL, |
| 9979 | }; |
| 9980 | int ret = 0; |
| 9981 | u64 isize; |
| 9982 | u64 start; |
| 9983 | |
| 9984 | /* |
| 9985 | * If the swap file was just created, make sure delalloc is done. If the |
| 9986 | * file changes again after this, the user is doing something stupid and |
| 9987 | * we don't really care. |
| 9988 | */ |
| 9989 | ret = btrfs_wait_ordered_range(inode, 0, (u64)-1); |
| 9990 | if (ret) |
| 9991 | return ret; |
| 9992 | |
| 9993 | /* |
| 9994 | * The inode is locked, so these flags won't change after we check them. |
| 9995 | */ |
| 9996 | if (BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS) { |
| 9997 | btrfs_warn(fs_info, "swapfile must not be compressed"); |
| 9998 | return -EINVAL; |
| 9999 | } |
| 10000 | if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW)) { |
| 10001 | btrfs_warn(fs_info, "swapfile must not be copy-on-write"); |
| 10002 | return -EINVAL; |
| 10003 | } |
| 10004 | if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) { |
| 10005 | btrfs_warn(fs_info, "swapfile must not be checksummed"); |
| 10006 | return -EINVAL; |
| 10007 | } |
| 10008 | |
| 10009 | /* |
| 10010 | * Balance or device remove/replace/resize can move stuff around from |
Goldwyn Rodrigues | c3e1f96 | 2020-08-25 10:02:32 -0500 | [diff] [blame] | 10011 | * under us. The exclop protection makes sure they aren't running/won't |
| 10012 | * run concurrently while we are mapping the swap extents, and |
| 10013 | * fs_info->swapfile_pins prevents them from running while the swap |
| 10014 | * file is active and moving the extents. Note that this also prevents |
| 10015 | * 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] | 10016 | * really worth the trouble to allow it. |
| 10017 | */ |
Goldwyn Rodrigues | c3e1f96 | 2020-08-25 10:02:32 -0500 | [diff] [blame] | 10018 | if (!btrfs_exclop_start(fs_info, BTRFS_EXCLOP_SWAP_ACTIVATE)) { |
Omar Sandoval | ed46ff3 | 2016-11-03 10:28:14 -0700 | [diff] [blame] | 10019 | btrfs_warn(fs_info, |
| 10020 | "cannot activate swapfile while exclusive operation is running"); |
| 10021 | return -EBUSY; |
| 10022 | } |
| 10023 | /* |
| 10024 | * Snapshots can create extents which require COW even if NODATACOW is |
| 10025 | * set. We use this counter to prevent snapshots. We must increment it |
| 10026 | * before walking the extents because we don't want a concurrent |
| 10027 | * snapshot to run after we've already checked the extents. |
| 10028 | */ |
| 10029 | atomic_inc(&BTRFS_I(inode)->root->nr_swapfiles); |
| 10030 | |
| 10031 | isize = ALIGN_DOWN(inode->i_size, fs_info->sectorsize); |
| 10032 | |
| 10033 | lock_extent_bits(io_tree, 0, isize - 1, &cached_state); |
| 10034 | start = 0; |
| 10035 | while (start < isize) { |
| 10036 | u64 logical_block_start, physical_block_start; |
David Sterba | 32da5386 | 2019-10-29 19:20:18 +0100 | [diff] [blame] | 10037 | struct btrfs_block_group *bg; |
Omar Sandoval | ed46ff3 | 2016-11-03 10:28:14 -0700 | [diff] [blame] | 10038 | u64 len = isize - start; |
| 10039 | |
Omar Sandoval | 39b07b5 | 2019-12-02 17:34:23 -0800 | [diff] [blame] | 10040 | em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len); |
Omar Sandoval | ed46ff3 | 2016-11-03 10:28:14 -0700 | [diff] [blame] | 10041 | if (IS_ERR(em)) { |
| 10042 | ret = PTR_ERR(em); |
| 10043 | goto out; |
| 10044 | } |
| 10045 | |
| 10046 | if (em->block_start == EXTENT_MAP_HOLE) { |
| 10047 | btrfs_warn(fs_info, "swapfile must not have holes"); |
| 10048 | ret = -EINVAL; |
| 10049 | goto out; |
| 10050 | } |
| 10051 | if (em->block_start == EXTENT_MAP_INLINE) { |
| 10052 | /* |
| 10053 | * It's unlikely we'll ever actually find ourselves |
| 10054 | * here, as a file small enough to fit inline won't be |
| 10055 | * big enough to store more than the swap header, but in |
| 10056 | * case something changes in the future, let's catch it |
| 10057 | * here rather than later. |
| 10058 | */ |
| 10059 | btrfs_warn(fs_info, "swapfile must not be inline"); |
| 10060 | ret = -EINVAL; |
| 10061 | goto out; |
| 10062 | } |
| 10063 | if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) { |
| 10064 | btrfs_warn(fs_info, "swapfile must not be compressed"); |
| 10065 | ret = -EINVAL; |
| 10066 | goto out; |
| 10067 | } |
| 10068 | |
| 10069 | logical_block_start = em->block_start + (start - em->start); |
| 10070 | len = min(len, em->len - (start - em->start)); |
| 10071 | free_extent_map(em); |
| 10072 | em = NULL; |
| 10073 | |
Boris Burkov | a84d5d4 | 2020-08-18 11:00:05 -0700 | [diff] [blame] | 10074 | ret = can_nocow_extent(inode, start, &len, NULL, NULL, NULL, true); |
Omar Sandoval | ed46ff3 | 2016-11-03 10:28:14 -0700 | [diff] [blame] | 10075 | if (ret < 0) { |
| 10076 | goto out; |
| 10077 | } else if (ret) { |
| 10078 | ret = 0; |
| 10079 | } else { |
| 10080 | btrfs_warn(fs_info, |
| 10081 | "swapfile must not be copy-on-write"); |
| 10082 | ret = -EINVAL; |
| 10083 | goto out; |
| 10084 | } |
| 10085 | |
| 10086 | em = btrfs_get_chunk_map(fs_info, logical_block_start, len); |
| 10087 | if (IS_ERR(em)) { |
| 10088 | ret = PTR_ERR(em); |
| 10089 | goto out; |
| 10090 | } |
| 10091 | |
| 10092 | if (em->map_lookup->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) { |
| 10093 | btrfs_warn(fs_info, |
| 10094 | "swapfile must have single data profile"); |
| 10095 | ret = -EINVAL; |
| 10096 | goto out; |
| 10097 | } |
| 10098 | |
| 10099 | if (device == NULL) { |
| 10100 | device = em->map_lookup->stripes[0].dev; |
| 10101 | ret = btrfs_add_swapfile_pin(inode, device, false); |
| 10102 | if (ret == 1) |
| 10103 | ret = 0; |
| 10104 | else if (ret) |
| 10105 | goto out; |
| 10106 | } else if (device != em->map_lookup->stripes[0].dev) { |
| 10107 | btrfs_warn(fs_info, "swapfile must be on one device"); |
| 10108 | ret = -EINVAL; |
| 10109 | goto out; |
| 10110 | } |
| 10111 | |
| 10112 | physical_block_start = (em->map_lookup->stripes[0].physical + |
| 10113 | (logical_block_start - em->start)); |
| 10114 | len = min(len, em->len - (logical_block_start - em->start)); |
| 10115 | free_extent_map(em); |
| 10116 | em = NULL; |
| 10117 | |
| 10118 | bg = btrfs_lookup_block_group(fs_info, logical_block_start); |
| 10119 | if (!bg) { |
| 10120 | btrfs_warn(fs_info, |
| 10121 | "could not find block group containing swapfile"); |
| 10122 | ret = -EINVAL; |
| 10123 | goto out; |
| 10124 | } |
| 10125 | |
| 10126 | ret = btrfs_add_swapfile_pin(inode, bg, true); |
| 10127 | if (ret) { |
| 10128 | btrfs_put_block_group(bg); |
| 10129 | if (ret == 1) |
| 10130 | ret = 0; |
| 10131 | else |
| 10132 | goto out; |
| 10133 | } |
| 10134 | |
| 10135 | if (bsi.block_len && |
| 10136 | bsi.block_start + bsi.block_len == physical_block_start) { |
| 10137 | bsi.block_len += len; |
| 10138 | } else { |
| 10139 | if (bsi.block_len) { |
| 10140 | ret = btrfs_add_swap_extent(sis, &bsi); |
| 10141 | if (ret) |
| 10142 | goto out; |
| 10143 | } |
| 10144 | bsi.start = start; |
| 10145 | bsi.block_start = physical_block_start; |
| 10146 | bsi.block_len = len; |
| 10147 | } |
| 10148 | |
| 10149 | start += len; |
| 10150 | } |
| 10151 | |
| 10152 | if (bsi.block_len) |
| 10153 | ret = btrfs_add_swap_extent(sis, &bsi); |
| 10154 | |
| 10155 | out: |
| 10156 | if (!IS_ERR_OR_NULL(em)) |
| 10157 | free_extent_map(em); |
| 10158 | |
| 10159 | unlock_extent_cached(io_tree, 0, isize - 1, &cached_state); |
| 10160 | |
| 10161 | if (ret) |
| 10162 | btrfs_swap_deactivate(file); |
| 10163 | |
Goldwyn Rodrigues | c3e1f96 | 2020-08-25 10:02:32 -0500 | [diff] [blame] | 10164 | btrfs_exclop_finish(fs_info); |
Omar Sandoval | ed46ff3 | 2016-11-03 10:28:14 -0700 | [diff] [blame] | 10165 | |
| 10166 | if (ret) |
| 10167 | return ret; |
| 10168 | |
| 10169 | if (device) |
| 10170 | sis->bdev = device->bdev; |
| 10171 | *span = bsi.highest_ppage - bsi.lowest_ppage + 1; |
| 10172 | sis->max = bsi.nr_pages; |
| 10173 | sis->pages = bsi.nr_pages - 1; |
| 10174 | sis->highest_bit = bsi.nr_pages - 1; |
| 10175 | return bsi.nr_extents; |
| 10176 | } |
| 10177 | #else |
| 10178 | static void btrfs_swap_deactivate(struct file *file) |
| 10179 | { |
| 10180 | } |
| 10181 | |
| 10182 | static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file, |
| 10183 | sector_t *span) |
| 10184 | { |
| 10185 | return -EOPNOTSUPP; |
| 10186 | } |
| 10187 | #endif |
| 10188 | |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 10189 | static const struct inode_operations btrfs_dir_inode_operations = { |
Chris Mason | 3394e16 | 2008-11-17 20:42:26 -0500 | [diff] [blame] | 10190 | .getattr = btrfs_getattr, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10191 | .lookup = btrfs_lookup, |
| 10192 | .create = btrfs_create, |
| 10193 | .unlink = btrfs_unlink, |
| 10194 | .link = btrfs_link, |
| 10195 | .mkdir = btrfs_mkdir, |
| 10196 | .rmdir = btrfs_rmdir, |
Miklos Szeredi | 2773bf0 | 2016-09-27 11:03:58 +0200 | [diff] [blame] | 10197 | .rename = btrfs_rename2, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10198 | .symlink = btrfs_symlink, |
| 10199 | .setattr = btrfs_setattr, |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 10200 | .mknod = btrfs_mknod, |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 10201 | .listxattr = btrfs_listxattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 10202 | .permission = btrfs_permission, |
Christoph Hellwig | 4e34e71 | 2011-07-23 17:37:31 +0200 | [diff] [blame] | 10203 | .get_acl = btrfs_get_acl, |
Christoph Hellwig | 996a710 | 2013-12-20 05:16:43 -0800 | [diff] [blame] | 10204 | .set_acl = btrfs_set_acl, |
Guangyu Sun | 93fd63c | 2013-09-16 10:42:03 -0700 | [diff] [blame] | 10205 | .update_time = btrfs_update_time, |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 10206 | .tmpfile = btrfs_tmpfile, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10207 | }; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 10208 | |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 10209 | static const struct file_operations btrfs_dir_file_operations = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10210 | .llseek = generic_file_llseek, |
| 10211 | .read = generic_read_dir, |
Omar Sandoval | 02dbfc9 | 2016-05-20 13:50:33 -0700 | [diff] [blame] | 10212 | .iterate_shared = btrfs_real_readdir, |
Josef Bacik | 23b5ec7 | 2017-07-24 15:14:25 -0400 | [diff] [blame] | 10213 | .open = btrfs_opendir, |
Christoph Hellwig | 34287aa | 2007-09-14 10:22:47 -0400 | [diff] [blame] | 10214 | .unlocked_ioctl = btrfs_ioctl, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10215 | #ifdef CONFIG_COMPAT |
Luke Dashjr | 4c63c24 | 2015-10-29 08:22:21 +0000 | [diff] [blame] | 10216 | .compat_ioctl = btrfs_compat_ioctl, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10217 | #endif |
Sage Weil | 6bf13c0 | 2008-06-10 10:07:39 -0400 | [diff] [blame] | 10218 | .release = btrfs_release_file, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 10219 | .fsync = btrfs_sync_file, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10220 | }; |
| 10221 | |
Chris Mason | 3505439 | 2009-01-21 13:11:13 -0500 | [diff] [blame] | 10222 | /* |
| 10223 | * btrfs doesn't support the bmap operation because swapfiles |
| 10224 | * use bmap to make a mapping of extents in the file. They assume |
| 10225 | * these extents won't change over the life of the file and they |
| 10226 | * use the bmap result to do IO directly to the drive. |
| 10227 | * |
| 10228 | * the btrfs bmap call would return logical addresses that aren't |
| 10229 | * suitable for IO and they also will change frequently as COW |
| 10230 | * operations happen. So, swapfile + btrfs == corruption. |
| 10231 | * |
| 10232 | * For now we're avoiding this by dropping bmap. |
| 10233 | */ |
Alexey Dobriyan | 7f09410 | 2009-09-21 17:01:10 -0700 | [diff] [blame] | 10234 | static const struct address_space_operations btrfs_aops = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10235 | .readpage = btrfs_readpage, |
| 10236 | .writepage = btrfs_writepage, |
Chris Mason | b293f02e | 2007-11-01 19:45:34 -0400 | [diff] [blame] | 10237 | .writepages = btrfs_writepages, |
Matthew Wilcox (Oracle) | ba206a0 | 2020-06-01 21:47:05 -0700 | [diff] [blame] | 10238 | .readahead = btrfs_readahead, |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 10239 | .direct_IO = noop_direct_IO, |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 10240 | .invalidatepage = btrfs_invalidatepage, |
| 10241 | .releasepage = btrfs_releasepage, |
Roman Gushchin | f8e6608 | 2020-03-04 16:57:35 -0800 | [diff] [blame] | 10242 | #ifdef CONFIG_MIGRATION |
| 10243 | .migratepage = btrfs_migratepage, |
| 10244 | #endif |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 10245 | .set_page_dirty = btrfs_set_page_dirty, |
Andi Kleen | 465fdd9 | 2009-09-16 11:50:18 +0200 | [diff] [blame] | 10246 | .error_remove_page = generic_error_remove_page, |
Omar Sandoval | ed46ff3 | 2016-11-03 10:28:14 -0700 | [diff] [blame] | 10247 | .swap_activate = btrfs_swap_activate, |
| 10248 | .swap_deactivate = btrfs_swap_deactivate, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10249 | }; |
| 10250 | |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 10251 | static const struct inode_operations btrfs_file_inode_operations = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10252 | .getattr = btrfs_getattr, |
| 10253 | .setattr = btrfs_setattr, |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 10254 | .listxattr = btrfs_listxattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 10255 | .permission = btrfs_permission, |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 10256 | .fiemap = btrfs_fiemap, |
Christoph Hellwig | 4e34e71 | 2011-07-23 17:37:31 +0200 | [diff] [blame] | 10257 | .get_acl = btrfs_get_acl, |
Christoph Hellwig | 996a710 | 2013-12-20 05:16:43 -0800 | [diff] [blame] | 10258 | .set_acl = btrfs_set_acl, |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 10259 | .update_time = btrfs_update_time, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10260 | }; |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 10261 | static const struct inode_operations btrfs_special_inode_operations = { |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 10262 | .getattr = btrfs_getattr, |
| 10263 | .setattr = btrfs_setattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 10264 | .permission = btrfs_permission, |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 10265 | .listxattr = btrfs_listxattr, |
Christoph Hellwig | 4e34e71 | 2011-07-23 17:37:31 +0200 | [diff] [blame] | 10266 | .get_acl = btrfs_get_acl, |
Christoph Hellwig | 996a710 | 2013-12-20 05:16:43 -0800 | [diff] [blame] | 10267 | .set_acl = btrfs_set_acl, |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 10268 | .update_time = btrfs_update_time, |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 10269 | }; |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 10270 | static const struct inode_operations btrfs_symlink_inode_operations = { |
Al Viro | 6b25539 | 2015-11-17 10:20:54 -0500 | [diff] [blame] | 10271 | .get_link = page_get_link, |
Li Zefan | f209561 | 2010-11-19 02:05:24 +0000 | [diff] [blame] | 10272 | .getattr = btrfs_getattr, |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 10273 | .setattr = btrfs_setattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 10274 | .permission = btrfs_permission, |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 10275 | .listxattr = btrfs_listxattr, |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 10276 | .update_time = btrfs_update_time, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10277 | }; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 10278 | |
Alexey Dobriyan | 82d339d | 2009-10-09 09:54:36 -0400 | [diff] [blame] | 10279 | const struct dentry_operations btrfs_dentry_operations = { |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 10280 | .d_delete = btrfs_dentry_delete, |
| 10281 | }; |