David Sterba | c1d7c51 | 2018-04-03 19:23:33 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Chris Mason | 6cbd557 | 2007-06-12 09:07:21 -0400 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2007 Oracle. All rights reserved. |
Chris Mason | 6cbd557 | 2007-06-12 09:07:21 -0400 | [diff] [blame] | 4 | */ |
| 5 | |
Herbert Xu | 7999096 | 2020-06-12 16:57:37 +1000 | [diff] [blame] | 6 | #include <crypto/hash.h> |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 7 | #include <linux/kernel.h> |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 8 | #include <linux/bio.h> |
Sage Weil | f2eb0a2 | 2008-05-02 14:43:14 -0400 | [diff] [blame] | 9 | #include <linux/file.h> |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10 | #include <linux/fs.h> |
| 11 | #include <linux/pagemap.h> |
| 12 | #include <linux/highmem.h> |
| 13 | #include <linux/time.h> |
| 14 | #include <linux/init.h> |
| 15 | #include <linux/string.h> |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 16 | #include <linux/backing-dev.h> |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 17 | #include <linux/writeback.h> |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 18 | #include <linux/compat.h> |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 19 | #include <linux/xattr.h> |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 20 | #include <linux/posix_acl.h> |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 21 | #include <linux/falloc.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 22 | #include <linux/slab.h> |
David Sterba | 7a36dde | 2011-05-06 15:33:15 +0200 | [diff] [blame] | 23 | #include <linux/ratelimit.h> |
Filipe Brandenburger | 55e301f | 2013-01-29 06:04:50 +0000 | [diff] [blame] | 24 | #include <linux/btrfs.h> |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 25 | #include <linux/blkdev.h> |
Josef Bacik | f23b5a5 | 2013-06-19 10:16:26 -0400 | [diff] [blame] | 26 | #include <linux/posix_acl_xattr.h> |
Christoph Hellwig | e2e40f2 | 2015-02-22 08:58:50 -0800 | [diff] [blame] | 27 | #include <linux/uio.h> |
Josef Bacik | 69fe2d7 | 2017-10-19 14:15:57 -0400 | [diff] [blame] | 28 | #include <linux/magic.h> |
Jeff Layton | ae5e165 | 2018-01-29 06:41:30 -0500 | [diff] [blame] | 29 | #include <linux/iversion.h> |
Omar Sandoval | ed46ff3 | 2016-11-03 10:28:14 -0700 | [diff] [blame] | 30 | #include <linux/swap.h> |
Roman Gushchin | f8e6608 | 2020-03-04 16:57:35 -0800 | [diff] [blame] | 31 | #include <linux/migrate.h> |
Nikolay Borisov | b1c16ac | 2019-04-01 11:29:57 +0300 | [diff] [blame] | 32 | #include <linux/sched/mm.h> |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 33 | #include <linux/iomap.h> |
David Sterba | 92d3217 | 2018-04-16 21:10:14 +0200 | [diff] [blame] | 34 | #include <asm/unaligned.h> |
David Sterba | 602cbe9 | 2019-08-21 18:48:25 +0200 | [diff] [blame] | 35 | #include "misc.h" |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 36 | #include "ctree.h" |
| 37 | #include "disk-io.h" |
| 38 | #include "transaction.h" |
| 39 | #include "btrfs_inode.h" |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 40 | #include "print-tree.h" |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 41 | #include "ordered-data.h" |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 42 | #include "xattr.h" |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 43 | #include "tree-log.h" |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 44 | #include "volumes.h" |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 45 | #include "compression.h" |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 46 | #include "locking.h" |
Josef Bacik | dc89e98 | 2011-01-28 17:05:48 -0500 | [diff] [blame] | 47 | #include "free-space-cache.h" |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 48 | #include "inode-map.h" |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 49 | #include "props.h" |
Dongsheng Yang | 3119321 | 2014-12-12 16:44:35 +0800 | [diff] [blame] | 50 | #include "qgroup.h" |
Josef Bacik | 8673634 | 2019-06-19 15:12:00 -0400 | [diff] [blame] | 51 | #include "delalloc-space.h" |
Josef Bacik | aac0023 | 2019-06-20 15:37:44 -0400 | [diff] [blame] | 52 | #include "block-group.h" |
Filipe Manana | 467dc47 | 2020-05-27 11:16:07 +0100 | [diff] [blame] | 53 | #include "space-info.h" |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 54 | |
| 55 | struct btrfs_iget_args { |
David Sterba | 0202e83 | 2020-05-15 19:35:59 +0200 | [diff] [blame] | 56 | u64 ino; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 57 | struct btrfs_root *root; |
| 58 | }; |
| 59 | |
Filipe Manana | f28a492 | 2015-12-08 19:23:20 +0000 | [diff] [blame] | 60 | struct btrfs_dio_data { |
Filipe Manana | f28a492 | 2015-12-08 19:23:20 +0000 | [diff] [blame] | 61 | u64 reserve; |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 62 | loff_t length; |
| 63 | ssize_t submitted; |
| 64 | struct extent_changeset *data_reserved; |
Filipe Manana | f28a492 | 2015-12-08 19:23:20 +0000 | [diff] [blame] | 65 | }; |
| 66 | |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 67 | static const struct inode_operations btrfs_dir_inode_operations; |
| 68 | static const struct inode_operations btrfs_symlink_inode_operations; |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 69 | static const struct inode_operations btrfs_special_inode_operations; |
| 70 | static const struct inode_operations btrfs_file_inode_operations; |
Alexey Dobriyan | 7f09410 | 2009-09-21 17:01:10 -0700 | [diff] [blame] | 71 | static const struct address_space_operations btrfs_aops; |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 72 | static const struct file_operations btrfs_dir_file_operations; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 73 | |
| 74 | static struct kmem_cache *btrfs_inode_cachep; |
| 75 | struct kmem_cache *btrfs_trans_handle_cachep; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 76 | struct kmem_cache *btrfs_path_cachep; |
Josef Bacik | dc89e98 | 2011-01-28 17:05:48 -0500 | [diff] [blame] | 77 | struct kmem_cache *btrfs_free_space_cachep; |
Christophe Leroy | 3acd485 | 2019-08-21 15:05:55 +0000 | [diff] [blame] | 78 | struct kmem_cache *btrfs_free_space_bitmap_cachep; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 79 | |
Eric Sandeen | 3972f26 | 2013-01-12 02:57:22 +0000 | [diff] [blame] | 80 | static int btrfs_setsize(struct inode *inode, struct iattr *attr); |
Filipe Manana | 213e8c5 | 2018-02-06 20:40:31 +0000 | [diff] [blame] | 81 | static int btrfs_truncate(struct inode *inode, bool skip_writeback); |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 82 | static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent); |
Nikolay Borisov | 6e26c44 | 2020-06-03 08:55:14 +0300 | [diff] [blame] | 83 | static noinline int cow_file_range(struct btrfs_inode *inode, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 84 | struct page *locked_page, |
Nikolay Borisov | 74e9194 | 2019-07-17 16:18:16 +0300 | [diff] [blame] | 85 | u64 start, u64 end, int *page_started, |
Nikolay Borisov | 330a582 | 2019-07-17 16:18:17 +0300 | [diff] [blame] | 86 | unsigned long *nr_written, int unlock); |
Nikolay Borisov | 4b67c11 | 2020-06-03 08:55:05 +0300 | [diff] [blame] | 87 | static struct extent_map *create_io_em(struct btrfs_inode *inode, u64 start, |
| 88 | u64 len, u64 orig_start, u64 block_start, |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 89 | u64 block_len, u64 orig_block_len, |
| 90 | u64 ram_bytes, int compress_type, |
| 91 | int type); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 92 | |
Nikolay Borisov | b672b5c | 2020-06-03 08:55:24 +0300 | [diff] [blame] | 93 | static void __endio_write_update_ordered(struct btrfs_inode *inode, |
Qu Wenruo | 52427260 | 2017-03-08 10:25:52 +0800 | [diff] [blame] | 94 | const u64 offset, const u64 bytes, |
| 95 | const bool uptodate); |
| 96 | |
| 97 | /* |
Goldwyn Rodrigues | a14b78a | 2020-09-24 11:39:16 -0500 | [diff] [blame] | 98 | * btrfs_inode_lock - lock inode i_rwsem based on arguments passed |
| 99 | * |
| 100 | * ilock_flags can have the following bit set: |
| 101 | * |
| 102 | * BTRFS_ILOCK_SHARED - acquire a shared lock on the inode |
| 103 | * BTRFS_ILOCK_TRY - try to acquire the lock, if fails on first attempt |
| 104 | * return -EAGAIN |
| 105 | */ |
| 106 | int btrfs_inode_lock(struct inode *inode, unsigned int ilock_flags) |
| 107 | { |
| 108 | if (ilock_flags & BTRFS_ILOCK_SHARED) { |
| 109 | if (ilock_flags & BTRFS_ILOCK_TRY) { |
| 110 | if (!inode_trylock_shared(inode)) |
| 111 | return -EAGAIN; |
| 112 | else |
| 113 | return 0; |
| 114 | } |
| 115 | inode_lock_shared(inode); |
| 116 | } else { |
| 117 | if (ilock_flags & BTRFS_ILOCK_TRY) { |
| 118 | if (!inode_trylock(inode)) |
| 119 | return -EAGAIN; |
| 120 | else |
| 121 | return 0; |
| 122 | } |
| 123 | inode_lock(inode); |
| 124 | } |
| 125 | return 0; |
| 126 | } |
| 127 | |
| 128 | /* |
| 129 | * btrfs_inode_unlock - unock inode i_rwsem |
| 130 | * |
| 131 | * ilock_flags should contain the same bits set as passed to btrfs_inode_lock() |
| 132 | * to decide whether the lock acquired is shared or exclusive. |
| 133 | */ |
| 134 | void btrfs_inode_unlock(struct inode *inode, unsigned int ilock_flags) |
| 135 | { |
| 136 | if (ilock_flags & BTRFS_ILOCK_SHARED) |
| 137 | inode_unlock_shared(inode); |
| 138 | else |
| 139 | inode_unlock(inode); |
| 140 | } |
| 141 | |
| 142 | /* |
Qu Wenruo | 52427260 | 2017-03-08 10:25:52 +0800 | [diff] [blame] | 143 | * Cleanup all submitted ordered extents in specified range to handle errors |
Andrea Gelmini | 52042d8 | 2018-11-28 12:05:13 +0100 | [diff] [blame] | 144 | * from the btrfs_run_delalloc_range() callback. |
Qu Wenruo | 52427260 | 2017-03-08 10:25:52 +0800 | [diff] [blame] | 145 | * |
| 146 | * NOTE: caller must ensure that when an error happens, it can not call |
| 147 | * extent_clear_unlock_delalloc() to clear both the bits EXTENT_DO_ACCOUNTING |
| 148 | * and EXTENT_DELALLOC simultaneously, because that causes the reserved metadata |
| 149 | * to be released, which we want to happen only when finishing the ordered |
Nikolay Borisov | d1051d6 | 2018-11-21 17:10:52 +0200 | [diff] [blame] | 150 | * extent (btrfs_finish_ordered_io()). |
Qu Wenruo | 52427260 | 2017-03-08 10:25:52 +0800 | [diff] [blame] | 151 | */ |
Nikolay Borisov | 64e1db5 | 2020-06-03 08:55:25 +0300 | [diff] [blame] | 152 | static inline void btrfs_cleanup_ordered_extents(struct btrfs_inode *inode, |
Nikolay Borisov | d1051d6 | 2018-11-21 17:10:52 +0200 | [diff] [blame] | 153 | struct page *locked_page, |
| 154 | u64 offset, u64 bytes) |
Qu Wenruo | 52427260 | 2017-03-08 10:25:52 +0800 | [diff] [blame] | 155 | { |
Naohiro Aota | 63d71450c | 2017-09-01 17:58:47 +0900 | [diff] [blame] | 156 | unsigned long index = offset >> PAGE_SHIFT; |
| 157 | unsigned long end_index = (offset + bytes - 1) >> PAGE_SHIFT; |
Nikolay Borisov | d1051d6 | 2018-11-21 17:10:52 +0200 | [diff] [blame] | 158 | u64 page_start = page_offset(locked_page); |
| 159 | u64 page_end = page_start + PAGE_SIZE - 1; |
| 160 | |
Naohiro Aota | 63d71450c | 2017-09-01 17:58:47 +0900 | [diff] [blame] | 161 | struct page *page; |
| 162 | |
| 163 | while (index <= end_index) { |
Nikolay Borisov | 64e1db5 | 2020-06-03 08:55:25 +0300 | [diff] [blame] | 164 | page = find_get_page(inode->vfs_inode.i_mapping, index); |
Naohiro Aota | 63d71450c | 2017-09-01 17:58:47 +0900 | [diff] [blame] | 165 | index++; |
| 166 | if (!page) |
| 167 | continue; |
| 168 | ClearPagePrivate2(page); |
| 169 | put_page(page); |
| 170 | } |
Nikolay Borisov | d1051d6 | 2018-11-21 17:10:52 +0200 | [diff] [blame] | 171 | |
| 172 | /* |
| 173 | * In case this page belongs to the delalloc range being instantiated |
| 174 | * then skip it, since the first page of a range is going to be |
| 175 | * properly cleaned up by the caller of run_delalloc_range |
| 176 | */ |
| 177 | if (page_start >= offset && page_end <= (offset + bytes - 1)) { |
| 178 | offset += PAGE_SIZE; |
| 179 | bytes -= PAGE_SIZE; |
| 180 | } |
| 181 | |
Nikolay Borisov | 64e1db5 | 2020-06-03 08:55:25 +0300 | [diff] [blame] | 182 | return __endio_write_update_ordered(inode, offset, bytes, false); |
Qu Wenruo | 52427260 | 2017-03-08 10:25:52 +0800 | [diff] [blame] | 183 | } |
| 184 | |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 185 | static int btrfs_dirty_inode(struct inode *inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 186 | |
Yan, Zheng | f34f57a | 2009-11-12 09:35:27 +0000 | [diff] [blame] | 187 | static int btrfs_init_inode_security(struct btrfs_trans_handle *trans, |
Eric Paris | 2a7dba3 | 2011-02-01 11:05:39 -0500 | [diff] [blame] | 188 | struct inode *inode, struct inode *dir, |
| 189 | const struct qstr *qstr) |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 190 | { |
| 191 | int err; |
| 192 | |
Yan, Zheng | f34f57a | 2009-11-12 09:35:27 +0000 | [diff] [blame] | 193 | err = btrfs_init_acl(trans, inode, dir); |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 194 | if (!err) |
Eric Paris | 2a7dba3 | 2011-02-01 11:05:39 -0500 | [diff] [blame] | 195 | err = btrfs_xattr_security_init(trans, inode, dir, qstr); |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 196 | return err; |
| 197 | } |
| 198 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 199 | /* |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 200 | * this does all the hard work for inserting an inline extent into |
| 201 | * the btree. The caller should have done a btrfs_drop_extents so that |
| 202 | * no overlapping inline items exist in the btree |
| 203 | */ |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 204 | static int insert_inline_extent(struct btrfs_trans_handle *trans, |
Filipe Manana | 5893dfb | 2020-11-04 11:07:32 +0000 | [diff] [blame^] | 205 | struct btrfs_path *path, bool extent_inserted, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 206 | struct btrfs_root *root, struct inode *inode, |
| 207 | u64 start, size_t size, size_t compressed_size, |
Li Zefan | fe3f566 | 2011-03-28 08:30:38 +0000 | [diff] [blame] | 208 | int compress_type, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 209 | struct page **compressed_pages) |
| 210 | { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 211 | struct extent_buffer *leaf; |
| 212 | struct page *page = NULL; |
| 213 | char *kaddr; |
| 214 | unsigned long ptr; |
| 215 | struct btrfs_file_extent_item *ei; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 216 | int ret; |
| 217 | size_t cur_size = size; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 218 | unsigned long offset; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 219 | |
Jia-Ju Bai | 982f1f5d | 2019-07-27 16:51:13 +0800 | [diff] [blame] | 220 | ASSERT((compressed_size > 0 && compressed_pages) || |
| 221 | (compressed_size == 0 && !compressed_pages)); |
| 222 | |
Li Zefan | fe3f566 | 2011-03-28 08:30:38 +0000 | [diff] [blame] | 223 | if (compressed_size && compressed_pages) |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 224 | cur_size = compressed_size; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 225 | |
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); |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 237 | ret = btrfs_insert_empty_item(trans, root, path, &key, |
| 238 | datasize); |
David Sterba | 79b4f4c | 2017-06-15 19:09:51 +0200 | [diff] [blame] | 239 | if (ret) |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 240 | goto fail; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 241 | } |
| 242 | leaf = path->nodes[0]; |
| 243 | ei = btrfs_item_ptr(leaf, path->slots[0], |
| 244 | struct btrfs_file_extent_item); |
| 245 | btrfs_set_file_extent_generation(leaf, ei, trans->transid); |
| 246 | btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE); |
| 247 | btrfs_set_file_extent_encryption(leaf, ei, 0); |
| 248 | btrfs_set_file_extent_other_encoding(leaf, ei, 0); |
| 249 | btrfs_set_file_extent_ram_bytes(leaf, ei, size); |
| 250 | ptr = btrfs_file_extent_inline_start(ei); |
| 251 | |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 252 | if (compress_type != BTRFS_COMPRESS_NONE) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 253 | struct page *cpage; |
| 254 | int i = 0; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 255 | while (compressed_size > 0) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 256 | cpage = compressed_pages[i]; |
Chris Mason | 5b050f0 | 2008-11-11 09:34:41 -0500 | [diff] [blame] | 257 | cur_size = min_t(unsigned long, compressed_size, |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 258 | PAGE_SIZE); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 259 | |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 260 | kaddr = kmap_atomic(cpage); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 261 | write_extent_buffer(leaf, kaddr, ptr, cur_size); |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 262 | kunmap_atomic(kaddr); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 263 | |
| 264 | i++; |
| 265 | ptr += cur_size; |
| 266 | compressed_size -= cur_size; |
| 267 | } |
| 268 | btrfs_set_file_extent_compression(leaf, ei, |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 269 | compress_type); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 270 | } else { |
| 271 | page = find_get_page(inode->i_mapping, |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 272 | start >> PAGE_SHIFT); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 273 | btrfs_set_file_extent_compression(leaf, ei, 0); |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 274 | kaddr = kmap_atomic(page); |
Johannes Thumshirn | 7073017 | 2018-12-05 15:23:03 +0100 | [diff] [blame] | 275 | offset = offset_in_page(start); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 276 | write_extent_buffer(leaf, kaddr + offset, ptr, size); |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 277 | kunmap_atomic(kaddr); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 278 | put_page(page); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 279 | } |
| 280 | btrfs_mark_buffer_dirty(leaf); |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 281 | btrfs_release_path(path); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 282 | |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 283 | /* |
Josef Bacik | 9ddc959 | 2020-01-17 09:02:22 -0500 | [diff] [blame] | 284 | * We align size to sectorsize for inline extents just for simplicity |
| 285 | * sake. |
| 286 | */ |
| 287 | size = ALIGN(size, root->fs_info->sectorsize); |
| 288 | ret = btrfs_inode_set_file_extent_range(BTRFS_I(inode), start, size); |
| 289 | if (ret) |
| 290 | goto fail; |
| 291 | |
| 292 | /* |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 293 | * we're an inline extent, so nobody can |
| 294 | * extend the file past i_size without locking |
| 295 | * a page we already have locked. |
| 296 | * |
| 297 | * We must do any isize and inode updates |
| 298 | * before we unlock the pages. Otherwise we |
| 299 | * could end up racing with unlink. |
| 300 | */ |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 301 | BTRFS_I(inode)->disk_i_size = inode->i_size; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 302 | ret = btrfs_update_inode(trans, root, inode); |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 303 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 304 | fail: |
David Sterba | 79b4f4c | 2017-06-15 19:09:51 +0200 | [diff] [blame] | 305 | return ret; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | |
| 309 | /* |
| 310 | * conditionally insert an inline extent into the file. This |
| 311 | * does the checks required to make sure the data is small enough |
| 312 | * to fit as an inline extent. |
| 313 | */ |
Nikolay Borisov | a034940 | 2020-06-03 08:55:12 +0300 | [diff] [blame] | 314 | 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] | 315 | u64 end, size_t compressed_size, |
| 316 | int compress_type, |
| 317 | struct page **compressed_pages) |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 318 | { |
Filipe Manana | 5893dfb | 2020-11-04 11:07:32 +0000 | [diff] [blame^] | 319 | struct btrfs_drop_extents_args drop_args = { 0 }; |
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; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 330 | |
| 331 | if (compressed_size) |
| 332 | data_len = compressed_size; |
| 333 | |
| 334 | if (start > 0 || |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 335 | actual_end > fs_info->sectorsize || |
| 336 | data_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info) || |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 337 | (!compressed_size && |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 338 | (actual_end & (fs_info->sectorsize - 1)) == 0) || |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 339 | end + 1 < isize || |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 340 | data_len > fs_info->max_inline) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 341 | return 1; |
| 342 | } |
| 343 | |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 344 | path = btrfs_alloc_path(); |
| 345 | if (!path) |
| 346 | return -ENOMEM; |
| 347 | |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 348 | trans = btrfs_join_transaction(root); |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 349 | if (IS_ERR(trans)) { |
| 350 | btrfs_free_path(path); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 351 | return PTR_ERR(trans); |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 352 | } |
Nikolay Borisov | a034940 | 2020-06-03 08:55:12 +0300 | [diff] [blame] | 353 | trans->block_rsv = &inode->block_rsv; |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 354 | |
Filipe Manana | 5893dfb | 2020-11-04 11:07:32 +0000 | [diff] [blame^] | 355 | drop_args.path = path; |
| 356 | drop_args.start = start; |
| 357 | drop_args.end = aligned_end; |
| 358 | drop_args.drop_cache = true; |
| 359 | drop_args.replace_extent = true; |
| 360 | |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 361 | if (compressed_size && compressed_pages) |
Filipe Manana | 5893dfb | 2020-11-04 11:07:32 +0000 | [diff] [blame^] | 362 | drop_args.extent_item_size = btrfs_file_extent_calc_inline_size( |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 363 | compressed_size); |
| 364 | else |
Filipe Manana | 5893dfb | 2020-11-04 11:07:32 +0000 | [diff] [blame^] | 365 | drop_args.extent_item_size = btrfs_file_extent_calc_inline_size( |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 366 | inline_len); |
| 367 | |
Filipe Manana | 5893dfb | 2020-11-04 11:07:32 +0000 | [diff] [blame^] | 368 | ret = btrfs_drop_extents(trans, root, inode, &drop_args); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 369 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 370 | btrfs_abort_transaction(trans, ret); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 371 | goto out; |
| 372 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 373 | |
| 374 | if (isize > actual_end) |
| 375 | inline_len = min_t(u64, isize, actual_end); |
Filipe Manana | 5893dfb | 2020-11-04 11:07:32 +0000 | [diff] [blame^] | 376 | ret = insert_inline_extent(trans, path, drop_args.extent_inserted, |
Nikolay Borisov | a034940 | 2020-06-03 08:55:12 +0300 | [diff] [blame] | 377 | root, &inode->vfs_inode, start, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 378 | inline_len, compressed_size, |
Li Zefan | fe3f566 | 2011-03-28 08:30:38 +0000 | [diff] [blame] | 379 | compress_type, compressed_pages); |
Josef Bacik | 2adcac1 | 2012-05-23 16:10:14 -0400 | [diff] [blame] | 380 | if (ret && ret != -ENOSPC) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 381 | btrfs_abort_transaction(trans, ret); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 382 | goto out; |
Josef Bacik | 2adcac1 | 2012-05-23 16:10:14 -0400 | [diff] [blame] | 383 | } else if (ret == -ENOSPC) { |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 384 | ret = 1; |
| 385 | goto out; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 386 | } |
Josef Bacik | 2adcac1 | 2012-05-23 16:10:14 -0400 | [diff] [blame] | 387 | |
Nikolay Borisov | a034940 | 2020-06-03 08:55:12 +0300 | [diff] [blame] | 388 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &inode->runtime_flags); |
| 389 | btrfs_drop_extent_cache(inode, start, aligned_end - 1, 0); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 390 | out: |
Qu Wenruo | 94ed938 | 2015-09-08 17:25:56 +0800 | [diff] [blame] | 391 | /* |
| 392 | * Don't forget to free the reserved space, as for inlined extent |
| 393 | * it won't count as data extent, free them directly here. |
| 394 | * And at reserve time, it's always aligned to page size, so |
| 395 | * just free one page here. |
| 396 | */ |
Nikolay Borisov | a034940 | 2020-06-03 08:55:12 +0300 | [diff] [blame] | 397 | btrfs_qgroup_free_data(inode, NULL, 0, PAGE_SIZE); |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 398 | btrfs_free_path(path); |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 399 | btrfs_end_transaction(trans); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 400 | return ret; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 401 | } |
| 402 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 403 | struct async_extent { |
| 404 | u64 start; |
| 405 | u64 ram_size; |
| 406 | u64 compressed_size; |
| 407 | struct page **pages; |
| 408 | unsigned long nr_pages; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 409 | int compress_type; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 410 | struct list_head list; |
| 411 | }; |
| 412 | |
Nikolay Borisov | 97db120 | 2019-03-12 17:20:24 +0200 | [diff] [blame] | 413 | struct async_chunk { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 414 | struct inode *inode; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 415 | struct page *locked_page; |
| 416 | u64 start; |
| 417 | u64 end; |
Liu Bo | f82b735 | 2017-10-23 23:18:16 -0600 | [diff] [blame] | 418 | unsigned int write_flags; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 419 | struct list_head extents; |
Chris Mason | ec39f76 | 2019-07-10 12:28:17 -0700 | [diff] [blame] | 420 | struct cgroup_subsys_state *blkcg_css; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 421 | struct btrfs_work work; |
Nikolay Borisov | 97db120 | 2019-03-12 17:20:24 +0200 | [diff] [blame] | 422 | atomic_t *pending; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 423 | }; |
| 424 | |
Nikolay Borisov | 97db120 | 2019-03-12 17:20:24 +0200 | [diff] [blame] | 425 | struct async_cow { |
| 426 | /* Number of chunks in flight; must be first in the structure */ |
| 427 | atomic_t num_chunks; |
| 428 | struct async_chunk chunks[]; |
| 429 | }; |
| 430 | |
| 431 | static noinline int add_async_extent(struct async_chunk *cow, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 432 | u64 start, u64 ram_size, |
| 433 | u64 compressed_size, |
| 434 | struct page **pages, |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 435 | unsigned long nr_pages, |
| 436 | int compress_type) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 437 | { |
| 438 | struct async_extent *async_extent; |
| 439 | |
| 440 | async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 441 | BUG_ON(!async_extent); /* -ENOMEM */ |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 442 | async_extent->start = start; |
| 443 | async_extent->ram_size = ram_size; |
| 444 | async_extent->compressed_size = compressed_size; |
| 445 | async_extent->pages = pages; |
| 446 | async_extent->nr_pages = nr_pages; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 447 | async_extent->compress_type = compress_type; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 448 | list_add_tail(&async_extent->list, &cow->extents); |
| 449 | return 0; |
| 450 | } |
| 451 | |
Qu Wenruo | 42c16da | 2019-07-01 05:12:46 +0000 | [diff] [blame] | 452 | /* |
| 453 | * Check if the inode has flags compatible with compression |
| 454 | */ |
Nikolay Borisov | 99c88dc | 2020-06-03 08:55:26 +0300 | [diff] [blame] | 455 | static inline bool inode_can_compress(struct btrfs_inode *inode) |
Qu Wenruo | 42c16da | 2019-07-01 05:12:46 +0000 | [diff] [blame] | 456 | { |
Nikolay Borisov | 99c88dc | 2020-06-03 08:55:26 +0300 | [diff] [blame] | 457 | if (inode->flags & BTRFS_INODE_NODATACOW || |
| 458 | inode->flags & BTRFS_INODE_NODATASUM) |
Qu Wenruo | 42c16da | 2019-07-01 05:12:46 +0000 | [diff] [blame] | 459 | return false; |
| 460 | return true; |
| 461 | } |
| 462 | |
| 463 | /* |
| 464 | * Check if the inode needs to be submitted to compression, based on mount |
| 465 | * options, defragmentation, properties or heuristics. |
| 466 | */ |
Nikolay Borisov | 808a129 | 2020-06-03 08:55:27 +0300 | [diff] [blame] | 467 | static inline int inode_need_compress(struct btrfs_inode *inode, u64 start, |
| 468 | u64 end) |
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 | struct btrfs_fs_info *fs_info = inode->root->fs_info; |
Wang Shilong | f79707b | 2014-07-17 11:44:09 +0800 | [diff] [blame] | 471 | |
Nikolay Borisov | 808a129 | 2020-06-03 08:55:27 +0300 | [diff] [blame] | 472 | if (!inode_can_compress(inode)) { |
Qu Wenruo | 42c16da | 2019-07-01 05:12:46 +0000 | [diff] [blame] | 473 | WARN(IS_ENABLED(CONFIG_BTRFS_DEBUG), |
| 474 | KERN_ERR "BTRFS: unexpected compression for ino %llu\n", |
Nikolay Borisov | 808a129 | 2020-06-03 08:55:27 +0300 | [diff] [blame] | 475 | btrfs_ino(inode)); |
Qu Wenruo | 42c16da | 2019-07-01 05:12:46 +0000 | [diff] [blame] | 476 | return 0; |
| 477 | } |
Wang Shilong | f79707b | 2014-07-17 11:44:09 +0800 | [diff] [blame] | 478 | /* force compress */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 479 | if (btrfs_test_opt(fs_info, FORCE_COMPRESS)) |
Wang Shilong | f79707b | 2014-07-17 11:44:09 +0800 | [diff] [blame] | 480 | return 1; |
David Sterba | eec63c6 | 2017-07-17 19:41:31 +0200 | [diff] [blame] | 481 | /* defrag ioctl */ |
Nikolay Borisov | 808a129 | 2020-06-03 08:55:27 +0300 | [diff] [blame] | 482 | if (inode->defrag_compress) |
David Sterba | eec63c6 | 2017-07-17 19:41:31 +0200 | [diff] [blame] | 483 | return 1; |
Wang Shilong | f79707b | 2014-07-17 11:44:09 +0800 | [diff] [blame] | 484 | /* bad compression ratios */ |
Nikolay Borisov | 808a129 | 2020-06-03 08:55:27 +0300 | [diff] [blame] | 485 | if (inode->flags & BTRFS_INODE_NOCOMPRESS) |
Wang Shilong | f79707b | 2014-07-17 11:44:09 +0800 | [diff] [blame] | 486 | return 0; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 487 | if (btrfs_test_opt(fs_info, COMPRESS) || |
Nikolay Borisov | 808a129 | 2020-06-03 08:55:27 +0300 | [diff] [blame] | 488 | inode->flags & BTRFS_INODE_COMPRESS || |
| 489 | inode->prop_compress) |
| 490 | return btrfs_compress_heuristic(&inode->vfs_inode, start, end); |
Wang Shilong | f79707b | 2014-07-17 11:44:09 +0800 | [diff] [blame] | 491 | return 0; |
| 492 | } |
| 493 | |
Nikolay Borisov | 6158e1c | 2017-02-20 13:50:43 +0200 | [diff] [blame] | 494 | static inline void inode_should_defrag(struct btrfs_inode *inode, |
Anand Jain | 26d30f8 | 2016-12-19 19:09:06 +0800 | [diff] [blame] | 495 | u64 start, u64 end, u64 num_bytes, u64 small_write) |
| 496 | { |
| 497 | /* If this is a small write inside eof, kick off a defrag */ |
| 498 | if (num_bytes < small_write && |
Nikolay Borisov | 6158e1c | 2017-02-20 13:50:43 +0200 | [diff] [blame] | 499 | (start > 0 || end + 1 < inode->disk_i_size)) |
Anand Jain | 26d30f8 | 2016-12-19 19:09:06 +0800 | [diff] [blame] | 500 | btrfs_add_inode_defrag(NULL, inode); |
| 501 | } |
| 502 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 503 | /* |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 504 | * we create compressed extents in two phases. The first |
| 505 | * phase compresses a range of pages that have already been |
| 506 | * locked (both pages and state bits are locked). |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 507 | * |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 508 | * This is done inside an ordered work queue, and the compression |
| 509 | * is spread across many cpus. The actual IO submission is step |
| 510 | * two, and the ordered work queue takes care of making sure that |
| 511 | * happens in the same order things were put onto the queue by |
| 512 | * writepages and friends. |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 513 | * |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 514 | * If this code finds it can't get good compression, it puts an |
| 515 | * entry onto the work queue to write the uncompressed bytes. This |
| 516 | * makes sure that both compressed inodes and uncompressed inodes |
Artem Bityutskiy | b257031 | 2012-07-25 18:12:06 +0300 | [diff] [blame] | 517 | * are written in the same order that the flusher thread sent them |
| 518 | * down. |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 519 | */ |
Nikolay Borisov | ac3e993 | 2019-07-17 14:41:44 +0300 | [diff] [blame] | 520 | static noinline int compress_file_range(struct async_chunk *async_chunk) |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 521 | { |
Nikolay Borisov | 1368c6d | 2019-03-12 17:20:27 +0200 | [diff] [blame] | 522 | struct inode *inode = async_chunk->inode; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 523 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 524 | u64 blocksize = fs_info->sectorsize; |
Nikolay Borisov | 1368c6d | 2019-03-12 17:20:27 +0200 | [diff] [blame] | 525 | u64 start = async_chunk->start; |
| 526 | u64 end = async_chunk->end; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 527 | u64 actual_end; |
Josef Bacik | d98da49 | 2019-10-11 09:03:54 -0400 | [diff] [blame] | 528 | u64 i_size; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 529 | int ret = 0; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 530 | struct page **pages = NULL; |
| 531 | unsigned long nr_pages; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 532 | unsigned long total_compressed = 0; |
| 533 | unsigned long total_in = 0; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 534 | int i; |
| 535 | int will_compress; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 536 | int compress_type = fs_info->compress_type; |
Nikolay Borisov | ac3e993 | 2019-07-17 14:41:44 +0300 | [diff] [blame] | 537 | int compressed_extents = 0; |
Chris Mason | 4adaa61 | 2013-03-26 13:07:00 -0400 | [diff] [blame] | 538 | int redirty = 0; |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 539 | |
Nikolay Borisov | 6158e1c | 2017-02-20 13:50:43 +0200 | [diff] [blame] | 540 | inode_should_defrag(BTRFS_I(inode), start, end, end - start + 1, |
| 541 | SZ_16K); |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 542 | |
Josef Bacik | d98da49 | 2019-10-11 09:03:54 -0400 | [diff] [blame] | 543 | /* |
| 544 | * We need to save i_size before now because it could change in between |
| 545 | * us evaluating the size and assigning it. This is because we lock and |
| 546 | * unlock the page in truncate and fallocate, and then modify the i_size |
| 547 | * later on. |
| 548 | * |
| 549 | * The barriers are to emulate READ_ONCE, remove that once i_size_read |
| 550 | * does that for us. |
| 551 | */ |
| 552 | barrier(); |
| 553 | i_size = i_size_read(inode); |
| 554 | barrier(); |
| 555 | actual_end = min_t(u64, i_size, end + 1); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 556 | again: |
| 557 | will_compress = 0; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 558 | nr_pages = (end >> PAGE_SHIFT) - (start >> PAGE_SHIFT) + 1; |
David Sterba | 069eac7 | 2017-02-14 19:36:54 +0100 | [diff] [blame] | 559 | BUILD_BUG_ON((BTRFS_MAX_COMPRESSED % PAGE_SIZE) != 0); |
| 560 | nr_pages = min_t(unsigned long, nr_pages, |
| 561 | BTRFS_MAX_COMPRESSED / PAGE_SIZE); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 562 | |
Chris Mason | f03d9301f | 2009-02-04 09:31:06 -0500 | [diff] [blame] | 563 | /* |
| 564 | * we don't want to send crud past the end of i_size through |
| 565 | * compression, that's just a waste of CPU time. So, if the |
| 566 | * end of the file is before the start of our current |
| 567 | * requested range of bytes, we bail out to the uncompressed |
| 568 | * cleanup code that can deal with all of this. |
| 569 | * |
| 570 | * It isn't really the fastest way to fix things, but this is a |
| 571 | * very uncommon corner. |
| 572 | */ |
| 573 | if (actual_end <= start) |
| 574 | goto cleanup_and_bail_uncompressed; |
| 575 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 576 | total_compressed = actual_end - start; |
| 577 | |
Shilong Wang | 4bcbb33 | 2014-10-07 18:44:35 -0400 | [diff] [blame] | 578 | /* |
| 579 | * skip compression for a small file range(<=blocksize) that |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 580 | * 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] | 581 | */ |
| 582 | if (total_compressed <= blocksize && |
| 583 | (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size)) |
| 584 | goto cleanup_and_bail_uncompressed; |
| 585 | |
David Sterba | 069eac7 | 2017-02-14 19:36:54 +0100 | [diff] [blame] | 586 | total_compressed = min_t(unsigned long, total_compressed, |
| 587 | BTRFS_MAX_UNCOMPRESSED); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 588 | total_in = 0; |
| 589 | ret = 0; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 590 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 591 | /* |
| 592 | * we do compression for mount -o compress and when the |
| 593 | * inode has not been flagged as nocompress. This flag can |
| 594 | * change at any time if we discover bad compression ratios. |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 595 | */ |
Nikolay Borisov | 808a129 | 2020-06-03 08:55:27 +0300 | [diff] [blame] | 596 | if (inode_need_compress(BTRFS_I(inode), start, end)) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 597 | WARN_ON(pages); |
David Sterba | 31e818f | 2015-02-20 18:00:26 +0100 | [diff] [blame] | 598 | pages = kcalloc(nr_pages, sizeof(struct page *), GFP_NOFS); |
Li Zefan | 560f7d7 | 2011-09-08 10:22:01 +0800 | [diff] [blame] | 599 | if (!pages) { |
| 600 | /* just bail out to the uncompressed code */ |
Filipe Manana | 3527a01 | 2018-10-13 00:37:25 +0100 | [diff] [blame] | 601 | nr_pages = 0; |
Li Zefan | 560f7d7 | 2011-09-08 10:22:01 +0800 | [diff] [blame] | 602 | goto cont; |
| 603 | } |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 604 | |
David Sterba | eec63c6 | 2017-07-17 19:41:31 +0200 | [diff] [blame] | 605 | if (BTRFS_I(inode)->defrag_compress) |
| 606 | compress_type = BTRFS_I(inode)->defrag_compress; |
| 607 | else if (BTRFS_I(inode)->prop_compress) |
David Sterba | b52aa8c | 2017-07-17 19:17:20 +0200 | [diff] [blame] | 608 | compress_type = BTRFS_I(inode)->prop_compress; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 609 | |
Chris Mason | 4adaa61 | 2013-03-26 13:07:00 -0400 | [diff] [blame] | 610 | /* |
| 611 | * we need to call clear_page_dirty_for_io on each |
| 612 | * page in the range. Otherwise applications with the file |
| 613 | * mmap'd can wander in and change the page contents while |
| 614 | * we are compressing them. |
| 615 | * |
| 616 | * If the compression fails for any reason, we set the pages |
| 617 | * dirty again later on. |
Timofey Titovets | e9679de | 2017-10-24 01:29:48 +0300 | [diff] [blame] | 618 | * |
| 619 | * Note that the remaining part is redirtied, the start pointer |
| 620 | * has moved, the end is the original one. |
Chris Mason | 4adaa61 | 2013-03-26 13:07:00 -0400 | [diff] [blame] | 621 | */ |
Timofey Titovets | e9679de | 2017-10-24 01:29:48 +0300 | [diff] [blame] | 622 | if (!redirty) { |
| 623 | extent_range_clear_dirty_for_io(inode, start, end); |
| 624 | redirty = 1; |
| 625 | } |
David Sterba | f51d2b5 | 2017-09-15 17:36:57 +0200 | [diff] [blame] | 626 | |
| 627 | /* Compression level is applied here and only here */ |
| 628 | ret = btrfs_compress_pages( |
| 629 | compress_type | (fs_info->compress_level << 4), |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 630 | inode->i_mapping, start, |
David Sterba | 38c3146 | 2017-02-14 19:04:07 +0100 | [diff] [blame] | 631 | pages, |
David Sterba | 4d3a800 | 2017-02-14 19:04:07 +0100 | [diff] [blame] | 632 | &nr_pages, |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 633 | &total_in, |
David Sterba | e5d7490 | 2017-02-14 19:45:05 +0100 | [diff] [blame] | 634 | &total_compressed); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 635 | |
| 636 | if (!ret) { |
Johannes Thumshirn | 7073017 | 2018-12-05 15:23:03 +0100 | [diff] [blame] | 637 | unsigned long offset = offset_in_page(total_compressed); |
David Sterba | 4d3a800 | 2017-02-14 19:04:07 +0100 | [diff] [blame] | 638 | struct page *page = pages[nr_pages - 1]; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 639 | char *kaddr; |
| 640 | |
| 641 | /* zero the tail end of the last page, we might be |
| 642 | * sending it down to disk |
| 643 | */ |
| 644 | if (offset) { |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 645 | kaddr = kmap_atomic(page); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 646 | memset(kaddr + offset, 0, |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 647 | PAGE_SIZE - offset); |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 648 | kunmap_atomic(kaddr); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 649 | } |
| 650 | will_compress = 1; |
| 651 | } |
| 652 | } |
Li Zefan | 560f7d7 | 2011-09-08 10:22:01 +0800 | [diff] [blame] | 653 | cont: |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 654 | if (start == 0) { |
| 655 | /* lets try to make an inline extent */ |
Timofey Titovets | 6018ba0 | 2017-09-15 01:57:26 +0300 | [diff] [blame] | 656 | if (ret || total_in < actual_end) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 657 | /* we didn't compress the entire range, try |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 658 | * to make an uncompressed inline extent. |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 659 | */ |
Nikolay Borisov | a034940 | 2020-06-03 08:55:12 +0300 | [diff] [blame] | 660 | ret = cow_file_range_inline(BTRFS_I(inode), start, end, |
| 661 | 0, BTRFS_COMPRESS_NONE, |
| 662 | NULL); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 663 | } else { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 664 | /* try making a compressed inline extent */ |
Nikolay Borisov | a034940 | 2020-06-03 08:55:12 +0300 | [diff] [blame] | 665 | ret = cow_file_range_inline(BTRFS_I(inode), start, end, |
Li Zefan | fe3f566 | 2011-03-28 08:30:38 +0000 | [diff] [blame] | 666 | total_compressed, |
| 667 | compress_type, pages); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 668 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 669 | if (ret <= 0) { |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 670 | unsigned long clear_flags = EXTENT_DELALLOC | |
Josef Bacik | 8b62f87 | 2017-10-19 14:15:55 -0400 | [diff] [blame] | 671 | EXTENT_DELALLOC_NEW | EXTENT_DEFRAG | |
| 672 | EXTENT_DO_ACCOUNTING; |
Filipe Manana | e6eb431 | 2014-10-10 10:45:12 +0100 | [diff] [blame] | 673 | unsigned long page_error_op; |
| 674 | |
Filipe Manana | e6eb431 | 2014-10-10 10:45:12 +0100 | [diff] [blame] | 675 | page_error_op = ret < 0 ? PAGE_SET_ERROR : 0; |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 676 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 677 | /* |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 678 | * inline extent creation worked or returned error, |
| 679 | * we don't need to create any more async work items. |
| 680 | * Unlock and free up our temp pages. |
Josef Bacik | 8b62f87 | 2017-10-19 14:15:55 -0400 | [diff] [blame] | 681 | * |
| 682 | * We use DO_ACCOUNTING here because we need the |
| 683 | * delalloc_release_metadata to be done _after_ we drop |
| 684 | * our outstanding extent for clearing delalloc for this |
| 685 | * range. |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 686 | */ |
Nikolay Borisov | ad7ff17 | 2020-06-03 08:55:06 +0300 | [diff] [blame] | 687 | extent_clear_unlock_delalloc(BTRFS_I(inode), start, end, |
| 688 | NULL, |
Nikolay Borisov | 74e9194 | 2019-07-17 16:18:16 +0300 | [diff] [blame] | 689 | clear_flags, |
Qu Wenruo | ba8b04c | 2016-07-19 16:50:36 +0800 | [diff] [blame] | 690 | PAGE_UNLOCK | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 691 | PAGE_CLEAR_DIRTY | |
| 692 | PAGE_SET_WRITEBACK | |
Filipe Manana | e6eb431 | 2014-10-10 10:45:12 +0100 | [diff] [blame] | 693 | page_error_op | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 694 | PAGE_END_WRITEBACK); |
Nikolay Borisov | cecc8d9 | 2019-07-17 14:41:45 +0300 | [diff] [blame] | 695 | |
Qu Wenruo | 1e6e238 | 2020-07-28 16:39:26 +0800 | [diff] [blame] | 696 | /* |
| 697 | * Ensure we only free the compressed pages if we have |
| 698 | * them allocated, as we can still reach here with |
| 699 | * inode_need_compress() == false. |
| 700 | */ |
| 701 | if (pages) { |
| 702 | for (i = 0; i < nr_pages; i++) { |
| 703 | WARN_ON(pages[i]->mapping); |
| 704 | put_page(pages[i]); |
| 705 | } |
| 706 | kfree(pages); |
Nikolay Borisov | cecc8d9 | 2019-07-17 14:41:45 +0300 | [diff] [blame] | 707 | } |
Nikolay Borisov | cecc8d9 | 2019-07-17 14:41:45 +0300 | [diff] [blame] | 708 | return 0; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 709 | } |
| 710 | } |
| 711 | |
| 712 | if (will_compress) { |
| 713 | /* |
| 714 | * we aren't doing an inline extent round the compressed size |
| 715 | * up to a block size boundary so the allocator does sane |
| 716 | * things |
| 717 | */ |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 718 | total_compressed = ALIGN(total_compressed, blocksize); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 719 | |
| 720 | /* |
| 721 | * one last check to make sure the compression is really a |
Timofey Titovets | 170607e | 2017-06-06 14:41:15 +0300 | [diff] [blame] | 722 | * win, compare the page count read with the blocks on disk, |
| 723 | * compression must free at least one sector size |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 724 | */ |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 725 | total_in = ALIGN(total_in, PAGE_SIZE); |
Timofey Titovets | 170607e | 2017-06-06 14:41:15 +0300 | [diff] [blame] | 726 | if (total_compressed + blocksize <= total_in) { |
Nikolay Borisov | ac3e993 | 2019-07-17 14:41:44 +0300 | [diff] [blame] | 727 | compressed_extents++; |
Ashish Samant | c8bb0c8 | 2016-03-25 19:01:33 -0700 | [diff] [blame] | 728 | |
| 729 | /* |
| 730 | * The async work queues will take care of doing actual |
| 731 | * allocation on disk for these compressed pages, and |
| 732 | * will submit them to the elevator. |
| 733 | */ |
Nikolay Borisov | b532627 | 2019-03-12 17:20:25 +0200 | [diff] [blame] | 734 | add_async_extent(async_chunk, start, total_in, |
David Sterba | 4d3a800 | 2017-02-14 19:04:07 +0100 | [diff] [blame] | 735 | total_compressed, pages, nr_pages, |
Ashish Samant | c8bb0c8 | 2016-03-25 19:01:33 -0700 | [diff] [blame] | 736 | compress_type); |
| 737 | |
Timofey Titovets | 1170862 | 2017-10-03 18:06:01 +0300 | [diff] [blame] | 738 | if (start + total_in < end) { |
| 739 | start += total_in; |
Ashish Samant | c8bb0c8 | 2016-03-25 19:01:33 -0700 | [diff] [blame] | 740 | pages = NULL; |
| 741 | cond_resched(); |
| 742 | goto again; |
| 743 | } |
Nikolay Borisov | ac3e993 | 2019-07-17 14:41:44 +0300 | [diff] [blame] | 744 | return compressed_extents; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 745 | } |
| 746 | } |
Ashish Samant | c8bb0c8 | 2016-03-25 19:01:33 -0700 | [diff] [blame] | 747 | if (pages) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 748 | /* |
| 749 | * the compression code ran but failed to make things smaller, |
| 750 | * free any pages it allocated and our page pointer array |
| 751 | */ |
David Sterba | 4d3a800 | 2017-02-14 19:04:07 +0100 | [diff] [blame] | 752 | for (i = 0; i < nr_pages; i++) { |
Chris Mason | 70b99e6 | 2008-10-31 12:46:39 -0400 | [diff] [blame] | 753 | WARN_ON(pages[i]->mapping); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 754 | put_page(pages[i]); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 755 | } |
| 756 | kfree(pages); |
| 757 | pages = NULL; |
| 758 | total_compressed = 0; |
David Sterba | 4d3a800 | 2017-02-14 19:04:07 +0100 | [diff] [blame] | 759 | nr_pages = 0; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 760 | |
| 761 | /* flag the file so we don't compress in the future */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 762 | if (!btrfs_test_opt(fs_info, FORCE_COMPRESS) && |
David Sterba | b52aa8c | 2017-07-17 19:17:20 +0200 | [diff] [blame] | 763 | !(BTRFS_I(inode)->prop_compress)) { |
Chris Mason | a555f81 | 2010-01-28 16:18:15 -0500 | [diff] [blame] | 764 | BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS; |
Chris Mason | 1e701a3 | 2010-03-11 09:42:04 -0500 | [diff] [blame] | 765 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 766 | } |
Chris Mason | f03d9301f | 2009-02-04 09:31:06 -0500 | [diff] [blame] | 767 | cleanup_and_bail_uncompressed: |
Ashish Samant | c8bb0c8 | 2016-03-25 19:01:33 -0700 | [diff] [blame] | 768 | /* |
| 769 | * No compression, but we still need to write the pages in the file |
| 770 | * we've been given so far. redirty the locked page if it corresponds |
| 771 | * to our extent and set things up for the async work queue to run |
| 772 | * cow_file_range to do the normal delalloc dance. |
| 773 | */ |
Chris Mason | 1d53c9e | 2019-07-10 12:28:16 -0700 | [diff] [blame] | 774 | if (async_chunk->locked_page && |
| 775 | (page_offset(async_chunk->locked_page) >= start && |
| 776 | page_offset(async_chunk->locked_page)) <= end) { |
Nikolay Borisov | 1368c6d | 2019-03-12 17:20:27 +0200 | [diff] [blame] | 777 | __set_page_dirty_nobuffers(async_chunk->locked_page); |
Ashish Samant | c8bb0c8 | 2016-03-25 19:01:33 -0700 | [diff] [blame] | 778 | /* unlocked later on in the async handlers */ |
Chris Mason | 1d53c9e | 2019-07-10 12:28:16 -0700 | [diff] [blame] | 779 | } |
Ashish Samant | c8bb0c8 | 2016-03-25 19:01:33 -0700 | [diff] [blame] | 780 | |
| 781 | if (redirty) |
| 782 | extent_range_redirty_for_io(inode, start, end); |
Nikolay Borisov | b532627 | 2019-03-12 17:20:25 +0200 | [diff] [blame] | 783 | add_async_extent(async_chunk, start, end - start + 1, 0, NULL, 0, |
Ashish Samant | c8bb0c8 | 2016-03-25 19:01:33 -0700 | [diff] [blame] | 784 | BTRFS_COMPRESS_NONE); |
Nikolay Borisov | ac3e993 | 2019-07-17 14:41:44 +0300 | [diff] [blame] | 785 | compressed_extents++; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 786 | |
Nikolay Borisov | ac3e993 | 2019-07-17 14:41:44 +0300 | [diff] [blame] | 787 | return compressed_extents; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 788 | } |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 789 | |
Filipe Manana | 40ae837 | 2014-10-06 22:14:24 +0100 | [diff] [blame] | 790 | static void free_async_extent_pages(struct async_extent *async_extent) |
| 791 | { |
| 792 | int i; |
| 793 | |
| 794 | if (!async_extent->pages) |
| 795 | return; |
| 796 | |
| 797 | for (i = 0; i < async_extent->nr_pages; i++) { |
| 798 | WARN_ON(async_extent->pages[i]->mapping); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 799 | put_page(async_extent->pages[i]); |
Filipe Manana | 40ae837 | 2014-10-06 22:14:24 +0100 | [diff] [blame] | 800 | } |
| 801 | kfree(async_extent->pages); |
| 802 | async_extent->nr_pages = 0; |
| 803 | async_extent->pages = NULL; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 804 | } |
| 805 | |
| 806 | /* |
| 807 | * phase two of compressed writeback. This is the ordered portion |
| 808 | * of the code, which only gets called in the order the work was |
| 809 | * queued. We walk all the async extents created by compress_file_range |
| 810 | * and send them down to the disk. |
| 811 | */ |
Nikolay Borisov | b532627 | 2019-03-12 17:20:25 +0200 | [diff] [blame] | 812 | static noinline void submit_compressed_extents(struct async_chunk *async_chunk) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 813 | { |
Nikolay Borisov | a0ff10d | 2020-06-03 08:55:17 +0300 | [diff] [blame] | 814 | struct btrfs_inode *inode = BTRFS_I(async_chunk->inode); |
| 815 | struct btrfs_fs_info *fs_info = inode->root->fs_info; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 816 | struct async_extent *async_extent; |
| 817 | u64 alloc_hint = 0; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 818 | struct btrfs_key ins; |
| 819 | struct extent_map *em; |
Nikolay Borisov | a0ff10d | 2020-06-03 08:55:17 +0300 | [diff] [blame] | 820 | struct btrfs_root *root = inode->root; |
| 821 | struct extent_io_tree *io_tree = &inode->io_tree; |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 822 | int ret = 0; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 823 | |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 824 | again: |
Nikolay Borisov | b532627 | 2019-03-12 17:20:25 +0200 | [diff] [blame] | 825 | while (!list_empty(&async_chunk->extents)) { |
| 826 | async_extent = list_entry(async_chunk->extents.next, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 827 | struct async_extent, list); |
| 828 | list_del(&async_extent->list); |
| 829 | |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 830 | retry: |
Nikolay Borisov | 7447555 | 2019-03-12 17:20:30 +0200 | [diff] [blame] | 831 | lock_extent(io_tree, async_extent->start, |
| 832 | async_extent->start + async_extent->ram_size - 1); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 833 | /* did the compression code fall back to uncompressed IO? */ |
| 834 | if (!async_extent->pages) { |
| 835 | int page_started = 0; |
| 836 | unsigned long nr_written = 0; |
| 837 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 838 | /* allocate blocks */ |
Nikolay Borisov | a0ff10d | 2020-06-03 08:55:17 +0300 | [diff] [blame] | 839 | ret = cow_file_range(inode, async_chunk->locked_page, |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 840 | async_extent->start, |
| 841 | async_extent->start + |
| 842 | async_extent->ram_size - 1, |
Nikolay Borisov | 330a582 | 2019-07-17 16:18:17 +0300 | [diff] [blame] | 843 | &page_started, &nr_written, 0); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 844 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 845 | /* JDM XXX */ |
| 846 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 847 | /* |
| 848 | * if page_started, cow_file_range inserted an |
| 849 | * inline extent and took care of all the unlocking |
| 850 | * and IO for us. Otherwise, we need to submit |
| 851 | * all those pages down to the drive. |
| 852 | */ |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 853 | if (!page_started && !ret) |
Nikolay Borisov | a0ff10d | 2020-06-03 08:55:17 +0300 | [diff] [blame] | 854 | extent_write_locked_range(&inode->vfs_inode, |
Nikolay Borisov | 5e3ee23 | 2017-12-08 15:55:58 +0200 | [diff] [blame] | 855 | async_extent->start, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 856 | async_extent->start + |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 857 | async_extent->ram_size - 1, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 858 | WB_SYNC_ALL); |
Chris Mason | 1d53c9e | 2019-07-10 12:28:16 -0700 | [diff] [blame] | 859 | else if (ret && async_chunk->locked_page) |
Nikolay Borisov | b532627 | 2019-03-12 17:20:25 +0200 | [diff] [blame] | 860 | unlock_page(async_chunk->locked_page); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 861 | kfree(async_extent); |
| 862 | cond_resched(); |
| 863 | continue; |
| 864 | } |
| 865 | |
Wang Xiaoguang | 1851309 | 2016-07-25 15:51:40 +0800 | [diff] [blame] | 866 | ret = btrfs_reserve_extent(root, async_extent->ram_size, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 867 | async_extent->compressed_size, |
| 868 | async_extent->compressed_size, |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 869 | 0, alloc_hint, &ins, 1, 1); |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 870 | if (ret) { |
Filipe Manana | 40ae837 | 2014-10-06 22:14:24 +0100 | [diff] [blame] | 871 | free_async_extent_pages(async_extent); |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 872 | |
Josef Bacik | fdf8e2e | 2013-06-14 16:58:23 -0400 | [diff] [blame] | 873 | if (ret == -ENOSPC) { |
| 874 | unlock_extent(io_tree, async_extent->start, |
| 875 | async_extent->start + |
| 876 | async_extent->ram_size - 1); |
Liu Bo | ce62003 | 2014-07-24 22:48:05 +0800 | [diff] [blame] | 877 | |
| 878 | /* |
| 879 | * we need to redirty the pages if we decide to |
| 880 | * fallback to uncompressed IO, otherwise we |
| 881 | * will not submit these pages down to lower |
| 882 | * layers. |
| 883 | */ |
Nikolay Borisov | a0ff10d | 2020-06-03 08:55:17 +0300 | [diff] [blame] | 884 | extent_range_redirty_for_io(&inode->vfs_inode, |
Liu Bo | ce62003 | 2014-07-24 22:48:05 +0800 | [diff] [blame] | 885 | async_extent->start, |
| 886 | async_extent->start + |
| 887 | async_extent->ram_size - 1); |
| 888 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 889 | goto retry; |
Josef Bacik | fdf8e2e | 2013-06-14 16:58:23 -0400 | [diff] [blame] | 890 | } |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 891 | goto out_free; |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 892 | } |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 893 | /* |
| 894 | * here we're doing allocation and writeback of the |
| 895 | * compressed pages |
| 896 | */ |
Nikolay Borisov | a0ff10d | 2020-06-03 08:55:17 +0300 | [diff] [blame] | 897 | em = create_io_em(inode, async_extent->start, |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 898 | async_extent->ram_size, /* len */ |
| 899 | async_extent->start, /* orig_start */ |
| 900 | ins.objectid, /* block_start */ |
| 901 | ins.offset, /* block_len */ |
| 902 | ins.offset, /* orig_block_len */ |
| 903 | async_extent->ram_size, /* ram_bytes */ |
| 904 | async_extent->compress_type, |
| 905 | BTRFS_ORDERED_COMPRESSED); |
| 906 | if (IS_ERR(em)) |
| 907 | /* ret value is not necessary due to void function */ |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 908 | goto out_free_reserve; |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 909 | free_extent_map(em); |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 910 | |
Nikolay Borisov | a0ff10d | 2020-06-03 08:55:17 +0300 | [diff] [blame] | 911 | ret = btrfs_add_ordered_extent_compress(inode, |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 912 | async_extent->start, |
| 913 | ins.objectid, |
| 914 | async_extent->ram_size, |
| 915 | ins.offset, |
| 916 | BTRFS_ORDERED_COMPRESSED, |
| 917 | async_extent->compress_type); |
Filipe Manana | d9f8596 | 2014-08-25 10:43:00 +0100 | [diff] [blame] | 918 | if (ret) { |
Nikolay Borisov | a0ff10d | 2020-06-03 08:55:17 +0300 | [diff] [blame] | 919 | btrfs_drop_extent_cache(inode, async_extent->start, |
Filipe Manana | d9f8596 | 2014-08-25 10:43:00 +0100 | [diff] [blame] | 920 | async_extent->start + |
| 921 | async_extent->ram_size - 1, 0); |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 922 | goto out_free_reserve; |
Filipe Manana | d9f8596 | 2014-08-25 10:43:00 +0100 | [diff] [blame] | 923 | } |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 924 | btrfs_dec_block_group_reservations(fs_info, ins.objectid); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 925 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 926 | /* |
| 927 | * clear dirty, set writeback and unlock the pages. |
| 928 | */ |
Nikolay Borisov | a0ff10d | 2020-06-03 08:55:17 +0300 | [diff] [blame] | 929 | extent_clear_unlock_delalloc(inode, async_extent->start, |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 930 | async_extent->start + |
| 931 | async_extent->ram_size - 1, |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 932 | NULL, EXTENT_LOCKED | EXTENT_DELALLOC, |
| 933 | PAGE_UNLOCK | PAGE_CLEAR_DIRTY | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 934 | PAGE_SET_WRITEBACK); |
Nikolay Borisov | a0ff10d | 2020-06-03 08:55:17 +0300 | [diff] [blame] | 935 | if (btrfs_submit_compressed_write(inode, async_extent->start, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 936 | async_extent->ram_size, |
| 937 | ins.objectid, |
| 938 | ins.offset, async_extent->pages, |
Liu Bo | f82b735 | 2017-10-23 23:18:16 -0600 | [diff] [blame] | 939 | async_extent->nr_pages, |
Chris Mason | ec39f76 | 2019-07-10 12:28:17 -0700 | [diff] [blame] | 940 | async_chunk->write_flags, |
| 941 | async_chunk->blkcg_css)) { |
Filipe Manana | fce2a4e | 2014-10-06 22:14:23 +0100 | [diff] [blame] | 942 | struct page *p = async_extent->pages[0]; |
| 943 | const u64 start = async_extent->start; |
| 944 | const u64 end = start + async_extent->ram_size - 1; |
| 945 | |
Nikolay Borisov | a0ff10d | 2020-06-03 08:55:17 +0300 | [diff] [blame] | 946 | p->mapping = inode->vfs_inode.i_mapping; |
Nikolay Borisov | c629732 | 2018-11-08 10:18:08 +0200 | [diff] [blame] | 947 | btrfs_writepage_endio_finish_ordered(p, start, end, 0); |
Nikolay Borisov | 7087a9d | 2018-11-01 14:09:48 +0200 | [diff] [blame] | 948 | |
Filipe Manana | fce2a4e | 2014-10-06 22:14:23 +0100 | [diff] [blame] | 949 | p->mapping = NULL; |
Nikolay Borisov | a0ff10d | 2020-06-03 08:55:17 +0300 | [diff] [blame] | 950 | extent_clear_unlock_delalloc(inode, start, end, NULL, 0, |
Filipe Manana | fce2a4e | 2014-10-06 22:14:23 +0100 | [diff] [blame] | 951 | PAGE_END_WRITEBACK | |
| 952 | PAGE_SET_ERROR); |
Filipe Manana | 40ae837 | 2014-10-06 22:14:24 +0100 | [diff] [blame] | 953 | free_async_extent_pages(async_extent); |
Filipe Manana | fce2a4e | 2014-10-06 22:14:23 +0100 | [diff] [blame] | 954 | } |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 955 | alloc_hint = ins.objectid + ins.offset; |
| 956 | kfree(async_extent); |
| 957 | cond_resched(); |
| 958 | } |
Filipe Manana | dec8f17 | 2014-10-06 22:14:26 +0100 | [diff] [blame] | 959 | return; |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 960 | out_free_reserve: |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 961 | btrfs_dec_block_group_reservations(fs_info, ins.objectid); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 962 | btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 963 | out_free: |
Nikolay Borisov | a0ff10d | 2020-06-03 08:55:17 +0300 | [diff] [blame] | 964 | extent_clear_unlock_delalloc(inode, async_extent->start, |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 965 | async_extent->start + |
| 966 | async_extent->ram_size - 1, |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 967 | NULL, EXTENT_LOCKED | EXTENT_DELALLOC | |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 968 | EXTENT_DELALLOC_NEW | |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 969 | EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING, |
| 970 | PAGE_UNLOCK | PAGE_CLEAR_DIRTY | |
Filipe Manana | 704de49 | 2014-10-06 22:14:22 +0100 | [diff] [blame] | 971 | PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK | |
| 972 | PAGE_SET_ERROR); |
Filipe Manana | 40ae837 | 2014-10-06 22:14:24 +0100 | [diff] [blame] | 973 | free_async_extent_pages(async_extent); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 974 | kfree(async_extent); |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 975 | goto again; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 976 | } |
| 977 | |
Nikolay Borisov | 43c6984 | 2020-06-03 08:55:02 +0300 | [diff] [blame] | 978 | static u64 get_extent_allocation_hint(struct btrfs_inode *inode, u64 start, |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 979 | u64 num_bytes) |
| 980 | { |
Nikolay Borisov | 43c6984 | 2020-06-03 08:55:02 +0300 | [diff] [blame] | 981 | struct extent_map_tree *em_tree = &inode->extent_tree; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 982 | struct extent_map *em; |
| 983 | u64 alloc_hint = 0; |
| 984 | |
| 985 | read_lock(&em_tree->lock); |
| 986 | em = search_extent_mapping(em_tree, start, num_bytes); |
| 987 | if (em) { |
| 988 | /* |
| 989 | * if block start isn't an actual block number then find the |
| 990 | * first block in this inode and use that as a hint. If that |
| 991 | * block is also bogus then just don't worry about it. |
| 992 | */ |
| 993 | if (em->block_start >= EXTENT_MAP_LAST_BYTE) { |
| 994 | free_extent_map(em); |
| 995 | em = search_extent_mapping(em_tree, 0, 0); |
| 996 | if (em && em->block_start < EXTENT_MAP_LAST_BYTE) |
| 997 | alloc_hint = em->block_start; |
| 998 | if (em) |
| 999 | free_extent_map(em); |
| 1000 | } else { |
| 1001 | alloc_hint = em->block_start; |
| 1002 | free_extent_map(em); |
| 1003 | } |
| 1004 | } |
| 1005 | read_unlock(&em_tree->lock); |
| 1006 | |
| 1007 | return alloc_hint; |
| 1008 | } |
| 1009 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1010 | /* |
| 1011 | * when extent_io.c finds a delayed allocation range in the file, |
| 1012 | * the call backs end up in this code. The basic idea is to |
| 1013 | * allocate extents on disk for the range, and create ordered data structs |
| 1014 | * in ram to track those extents. |
| 1015 | * |
| 1016 | * locked_page is the page that writepage had locked already. We use |
| 1017 | * it to make sure we don't do extra locks or unlocks. |
| 1018 | * |
| 1019 | * *page_started is set to one if we unlock locked_page and do everything |
| 1020 | * required to start IO on it. It may be clean and already done with |
| 1021 | * IO when we return. |
| 1022 | */ |
Nikolay Borisov | 6e26c44 | 2020-06-03 08:55:14 +0300 | [diff] [blame] | 1023 | static noinline int cow_file_range(struct btrfs_inode *inode, |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 1024 | struct page *locked_page, |
Nikolay Borisov | 74e9194 | 2019-07-17 16:18:16 +0300 | [diff] [blame] | 1025 | u64 start, u64 end, int *page_started, |
Nikolay Borisov | 330a582 | 2019-07-17 16:18:17 +0300 | [diff] [blame] | 1026 | unsigned long *nr_written, int unlock) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1027 | { |
Nikolay Borisov | 6e26c44 | 2020-06-03 08:55:14 +0300 | [diff] [blame] | 1028 | struct btrfs_root *root = inode->root; |
| 1029 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1030 | u64 alloc_hint = 0; |
| 1031 | u64 num_bytes; |
| 1032 | unsigned long ram_size; |
Filipe Manana | a315e68 | 2017-03-06 23:04:20 +0000 | [diff] [blame] | 1033 | u64 cur_alloc_size = 0; |
Filipe Manana | 432cd2a | 2020-06-08 13:32:55 +0100 | [diff] [blame] | 1034 | u64 min_alloc_size; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1035 | u64 blocksize = fs_info->sectorsize; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1036 | struct btrfs_key ins; |
| 1037 | struct extent_map *em; |
Filipe Manana | a315e68 | 2017-03-06 23:04:20 +0000 | [diff] [blame] | 1038 | unsigned clear_bits; |
| 1039 | unsigned long page_ops; |
| 1040 | bool extent_reserved = false; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1041 | int ret = 0; |
| 1042 | |
Nikolay Borisov | 6e26c44 | 2020-06-03 08:55:14 +0300 | [diff] [blame] | 1043 | if (btrfs_is_free_space_inode(inode)) { |
Josef Bacik | 02ecd2c | 2013-10-25 16:19:08 -0400 | [diff] [blame] | 1044 | WARN_ON_ONCE(1); |
Josef Bacik | 29bce2f | 2014-02-07 12:21:23 -0500 | [diff] [blame] | 1045 | ret = -EINVAL; |
| 1046 | goto out_unlock; |
Josef Bacik | 02ecd2c | 2013-10-25 16:19:08 -0400 | [diff] [blame] | 1047 | } |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1048 | |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 1049 | num_bytes = ALIGN(end - start + 1, blocksize); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1050 | num_bytes = max(blocksize, num_bytes); |
Anand Jain | 566b176 | 2018-02-15 18:07:59 +0800 | [diff] [blame] | 1051 | ASSERT(num_bytes <= btrfs_super_total_bytes(fs_info->super_copy)); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1052 | |
Nikolay Borisov | 6e26c44 | 2020-06-03 08:55:14 +0300 | [diff] [blame] | 1053 | inode_should_defrag(inode, start, end, num_bytes, SZ_64K); |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1054 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1055 | if (start == 0) { |
| 1056 | /* lets try to make an inline extent */ |
Nikolay Borisov | 6e26c44 | 2020-06-03 08:55:14 +0300 | [diff] [blame] | 1057 | ret = cow_file_range_inline(inode, start, end, 0, |
Nikolay Borisov | d02c0e2 | 2018-03-02 09:43:15 +0200 | [diff] [blame] | 1058 | BTRFS_COMPRESS_NONE, NULL); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1059 | if (ret == 0) { |
Josef Bacik | 8b62f87 | 2017-10-19 14:15:55 -0400 | [diff] [blame] | 1060 | /* |
| 1061 | * We use DO_ACCOUNTING here because we need the |
| 1062 | * delalloc_release_metadata to be run _after_ we drop |
| 1063 | * our outstanding extent for clearing delalloc for this |
| 1064 | * range. |
| 1065 | */ |
Nikolay Borisov | 6e26c44 | 2020-06-03 08:55:14 +0300 | [diff] [blame] | 1066 | extent_clear_unlock_delalloc(inode, start, end, NULL, |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1067 | EXTENT_LOCKED | EXTENT_DELALLOC | |
Josef Bacik | 8b62f87 | 2017-10-19 14:15:55 -0400 | [diff] [blame] | 1068 | EXTENT_DELALLOC_NEW | EXTENT_DEFRAG | |
| 1069 | EXTENT_DO_ACCOUNTING, PAGE_UNLOCK | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1070 | PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK | |
| 1071 | PAGE_END_WRITEBACK); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1072 | *nr_written = *nr_written + |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1073 | (end - start + PAGE_SIZE) / PAGE_SIZE; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1074 | *page_started = 1; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1075 | goto out; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1076 | } else if (ret < 0) { |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1077 | goto out_unlock; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1078 | } |
| 1079 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1080 | |
Nikolay Borisov | 6e26c44 | 2020-06-03 08:55:14 +0300 | [diff] [blame] | 1081 | alloc_hint = get_extent_allocation_hint(inode, start, num_bytes); |
| 1082 | btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 1083 | |
Filipe Manana | 432cd2a | 2020-06-08 13:32:55 +0100 | [diff] [blame] | 1084 | /* |
| 1085 | * Relocation relies on the relocated extents to have exactly the same |
| 1086 | * size as the original extents. Normally writeback for relocation data |
| 1087 | * extents follows a NOCOW path because relocation preallocates the |
| 1088 | * extents. However, due to an operation such as scrub turning a block |
| 1089 | * group to RO mode, it may fallback to COW mode, so we must make sure |
| 1090 | * an extent allocated during COW has exactly the requested size and can |
| 1091 | * not be split into smaller extents, otherwise relocation breaks and |
| 1092 | * fails during the stage where it updates the bytenr of file extent |
| 1093 | * items. |
| 1094 | */ |
| 1095 | if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID) |
| 1096 | min_alloc_size = num_bytes; |
| 1097 | else |
| 1098 | min_alloc_size = fs_info->sectorsize; |
| 1099 | |
Anand Jain | 3752d22 | 2018-02-15 12:29:38 +0800 | [diff] [blame] | 1100 | while (num_bytes > 0) { |
| 1101 | cur_alloc_size = num_bytes; |
Wang Xiaoguang | 1851309 | 2016-07-25 15:51:40 +0800 | [diff] [blame] | 1102 | ret = btrfs_reserve_extent(root, cur_alloc_size, cur_alloc_size, |
Filipe Manana | 432cd2a | 2020-06-08 13:32:55 +0100 | [diff] [blame] | 1103 | min_alloc_size, 0, alloc_hint, |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 1104 | &ins, 1, 1); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 1105 | if (ret < 0) |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1106 | goto out_unlock; |
Filipe Manana | a315e68 | 2017-03-06 23:04:20 +0000 | [diff] [blame] | 1107 | cur_alloc_size = ins.offset; |
| 1108 | extent_reserved = true; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1109 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1110 | ram_size = ins.offset; |
Nikolay Borisov | 6e26c44 | 2020-06-03 08:55:14 +0300 | [diff] [blame] | 1111 | em = create_io_em(inode, start, ins.offset, /* len */ |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 1112 | start, /* orig_start */ |
| 1113 | ins.objectid, /* block_start */ |
| 1114 | ins.offset, /* block_len */ |
| 1115 | ins.offset, /* orig_block_len */ |
| 1116 | ram_size, /* ram_bytes */ |
| 1117 | BTRFS_COMPRESS_NONE, /* compress_type */ |
Liu Bo | 1af4a0a | 2017-02-13 15:35:09 -0800 | [diff] [blame] | 1118 | BTRFS_ORDERED_REGULAR /* type */); |
Su Yue | 090a127a | 2018-05-30 16:48:56 +0800 | [diff] [blame] | 1119 | if (IS_ERR(em)) { |
| 1120 | ret = PTR_ERR(em); |
Liu Bo | ace68ba | 2013-04-22 10:53:47 +0000 | [diff] [blame] | 1121 | goto out_reserve; |
Su Yue | 090a127a | 2018-05-30 16:48:56 +0800 | [diff] [blame] | 1122 | } |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 1123 | free_extent_map(em); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1124 | |
Nikolay Borisov | 6e26c44 | 2020-06-03 08:55:14 +0300 | [diff] [blame] | 1125 | ret = btrfs_add_ordered_extent(inode, start, ins.objectid, |
| 1126 | ram_size, cur_alloc_size, 0); |
Liu Bo | ace68ba | 2013-04-22 10:53:47 +0000 | [diff] [blame] | 1127 | if (ret) |
Filipe Manana | d9f8596 | 2014-08-25 10:43:00 +0100 | [diff] [blame] | 1128 | goto out_drop_extent_cache; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1129 | |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1130 | if (root->root_key.objectid == |
| 1131 | BTRFS_DATA_RELOC_TREE_OBJECTID) { |
Nikolay Borisov | 6e26c44 | 2020-06-03 08:55:14 +0300 | [diff] [blame] | 1132 | ret = btrfs_reloc_clone_csums(inode, start, |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1133 | cur_alloc_size); |
Qu Wenruo | 4dbd80f | 2017-03-08 10:25:51 +0800 | [diff] [blame] | 1134 | /* |
| 1135 | * Only drop cache here, and process as normal. |
| 1136 | * |
| 1137 | * We must not allow extent_clear_unlock_delalloc() |
| 1138 | * at out_unlock label to free meta of this ordered |
| 1139 | * extent, as its meta should be freed by |
| 1140 | * btrfs_finish_ordered_io(). |
| 1141 | * |
| 1142 | * So we must continue until @start is increased to |
| 1143 | * skip current ordered extent. |
| 1144 | */ |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 1145 | if (ret) |
Nikolay Borisov | 6e26c44 | 2020-06-03 08:55:14 +0300 | [diff] [blame] | 1146 | btrfs_drop_extent_cache(inode, start, |
Qu Wenruo | 4dbd80f | 2017-03-08 10:25:51 +0800 | [diff] [blame] | 1147 | start + ram_size - 1, 0); |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1148 | } |
| 1149 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1150 | btrfs_dec_block_group_reservations(fs_info, ins.objectid); |
Filipe Manana | 9cfa3e3 | 2016-04-26 15:39:32 +0100 | [diff] [blame] | 1151 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1152 | /* we're not doing compressed IO, don't unlock the first |
| 1153 | * page (which the caller expects to stay locked), don't |
| 1154 | * clear any dirty bits and don't set any writeback bits |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 1155 | * |
| 1156 | * Do set the Private2 bit so we know this page was properly |
| 1157 | * setup for writepage |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1158 | */ |
Filipe Manana | a315e68 | 2017-03-06 23:04:20 +0000 | [diff] [blame] | 1159 | page_ops = unlock ? PAGE_UNLOCK : 0; |
| 1160 | page_ops |= PAGE_SET_PRIVATE2; |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 1161 | |
Nikolay Borisov | 6e26c44 | 2020-06-03 08:55:14 +0300 | [diff] [blame] | 1162 | extent_clear_unlock_delalloc(inode, start, start + ram_size - 1, |
Nikolay Borisov | 74e9194 | 2019-07-17 16:18:16 +0300 | [diff] [blame] | 1163 | locked_page, |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1164 | EXTENT_LOCKED | EXTENT_DELALLOC, |
Filipe Manana | a315e68 | 2017-03-06 23:04:20 +0000 | [diff] [blame] | 1165 | page_ops); |
Anand Jain | 3752d22 | 2018-02-15 12:29:38 +0800 | [diff] [blame] | 1166 | if (num_bytes < cur_alloc_size) |
| 1167 | num_bytes = 0; |
Qu Wenruo | 4dbd80f | 2017-03-08 10:25:51 +0800 | [diff] [blame] | 1168 | else |
Anand Jain | 3752d22 | 2018-02-15 12:29:38 +0800 | [diff] [blame] | 1169 | num_bytes -= cur_alloc_size; |
Chris Mason | c59f895 | 2007-12-17 20:14:04 -0500 | [diff] [blame] | 1170 | alloc_hint = ins.objectid + ins.offset; |
| 1171 | start += cur_alloc_size; |
Filipe Manana | a315e68 | 2017-03-06 23:04:20 +0000 | [diff] [blame] | 1172 | extent_reserved = false; |
Qu Wenruo | 4dbd80f | 2017-03-08 10:25:51 +0800 | [diff] [blame] | 1173 | |
| 1174 | /* |
| 1175 | * btrfs_reloc_clone_csums() error, since start is increased |
| 1176 | * extent_clear_unlock_delalloc() at out_unlock label won't |
| 1177 | * free metadata of current ordered extent, we're OK to exit. |
| 1178 | */ |
| 1179 | if (ret) |
| 1180 | goto out_unlock; |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 1181 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1182 | out: |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1183 | return ret; |
Miao Xie | b7d5b0a | 2012-11-01 07:32:18 +0000 | [diff] [blame] | 1184 | |
Filipe Manana | d9f8596 | 2014-08-25 10:43:00 +0100 | [diff] [blame] | 1185 | out_drop_extent_cache: |
Nikolay Borisov | 6e26c44 | 2020-06-03 08:55:14 +0300 | [diff] [blame] | 1186 | btrfs_drop_extent_cache(inode, start, start + ram_size - 1, 0); |
Liu Bo | ace68ba | 2013-04-22 10:53:47 +0000 | [diff] [blame] | 1187 | out_reserve: |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1188 | btrfs_dec_block_group_reservations(fs_info, ins.objectid); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 1189 | btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1190 | out_unlock: |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 1191 | clear_bits = EXTENT_LOCKED | EXTENT_DELALLOC | EXTENT_DELALLOC_NEW | |
| 1192 | EXTENT_DEFRAG | EXTENT_CLEAR_META_RESV; |
Filipe Manana | a315e68 | 2017-03-06 23:04:20 +0000 | [diff] [blame] | 1193 | page_ops = PAGE_UNLOCK | PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK | |
| 1194 | PAGE_END_WRITEBACK; |
| 1195 | /* |
| 1196 | * If we reserved an extent for our delalloc range (or a subrange) and |
| 1197 | * failed to create the respective ordered extent, then it means that |
| 1198 | * when we reserved the extent we decremented the extent's size from |
| 1199 | * the data space_info's bytes_may_use counter and incremented the |
| 1200 | * space_info's bytes_reserved counter by the same amount. We must make |
| 1201 | * sure extent_clear_unlock_delalloc() does not try to decrement again |
| 1202 | * the data space_info's bytes_may_use counter, therefore we do not pass |
| 1203 | * it the flag EXTENT_CLEAR_DATA_RESV. |
| 1204 | */ |
| 1205 | if (extent_reserved) { |
Nikolay Borisov | 6e26c44 | 2020-06-03 08:55:14 +0300 | [diff] [blame] | 1206 | extent_clear_unlock_delalloc(inode, start, |
Filipe Manana | e2c8e92 | 2020-05-27 11:15:53 +0100 | [diff] [blame] | 1207 | start + cur_alloc_size - 1, |
Filipe Manana | a315e68 | 2017-03-06 23:04:20 +0000 | [diff] [blame] | 1208 | locked_page, |
| 1209 | clear_bits, |
| 1210 | page_ops); |
| 1211 | start += cur_alloc_size; |
| 1212 | if (start >= end) |
| 1213 | goto out; |
| 1214 | } |
Nikolay Borisov | 6e26c44 | 2020-06-03 08:55:14 +0300 | [diff] [blame] | 1215 | extent_clear_unlock_delalloc(inode, start, end, locked_page, |
Filipe Manana | a315e68 | 2017-03-06 23:04:20 +0000 | [diff] [blame] | 1216 | clear_bits | EXTENT_CLEAR_DATA_RESV, |
| 1217 | page_ops); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1218 | goto out; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1219 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1220 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1221 | /* |
| 1222 | * work queue call back to started compression on a file and pages |
| 1223 | */ |
| 1224 | static noinline void async_cow_start(struct btrfs_work *work) |
| 1225 | { |
Nikolay Borisov | b532627 | 2019-03-12 17:20:25 +0200 | [diff] [blame] | 1226 | struct async_chunk *async_chunk; |
Nikolay Borisov | ac3e993 | 2019-07-17 14:41:44 +0300 | [diff] [blame] | 1227 | int compressed_extents; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1228 | |
Nikolay Borisov | b532627 | 2019-03-12 17:20:25 +0200 | [diff] [blame] | 1229 | async_chunk = container_of(work, struct async_chunk, work); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1230 | |
Nikolay Borisov | ac3e993 | 2019-07-17 14:41:44 +0300 | [diff] [blame] | 1231 | compressed_extents = compress_file_range(async_chunk); |
| 1232 | if (compressed_extents == 0) { |
Nikolay Borisov | b532627 | 2019-03-12 17:20:25 +0200 | [diff] [blame] | 1233 | btrfs_add_delayed_iput(async_chunk->inode); |
| 1234 | async_chunk->inode = NULL; |
Josef Bacik | 8180ef8 | 2012-06-08 15:16:12 -0400 | [diff] [blame] | 1235 | } |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1236 | } |
| 1237 | |
| 1238 | /* |
| 1239 | * work queue call back to submit previously compressed pages |
| 1240 | */ |
| 1241 | static noinline void async_cow_submit(struct btrfs_work *work) |
| 1242 | { |
Nikolay Borisov | c5a68ae | 2019-03-12 17:20:26 +0200 | [diff] [blame] | 1243 | struct async_chunk *async_chunk = container_of(work, struct async_chunk, |
| 1244 | work); |
| 1245 | struct btrfs_fs_info *fs_info = btrfs_work_owner(work); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1246 | unsigned long nr_pages; |
| 1247 | |
Nikolay Borisov | b532627 | 2019-03-12 17:20:25 +0200 | [diff] [blame] | 1248 | nr_pages = (async_chunk->end - async_chunk->start + PAGE_SIZE) >> |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1249 | PAGE_SHIFT; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1250 | |
David Sterba | 093258e | 2018-02-26 16:15:17 +0100 | [diff] [blame] | 1251 | /* atomic_sub_return implies a barrier */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1252 | if (atomic_sub_return(nr_pages, &fs_info->async_delalloc_pages) < |
David Sterba | 093258e | 2018-02-26 16:15:17 +0100 | [diff] [blame] | 1253 | 5 * SZ_1M) |
| 1254 | cond_wake_up_nomb(&fs_info->async_submit_wait); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1255 | |
Nikolay Borisov | 4546d17 | 2019-01-03 10:50:03 +0200 | [diff] [blame] | 1256 | /* |
Nikolay Borisov | b532627 | 2019-03-12 17:20:25 +0200 | [diff] [blame] | 1257 | * ->inode could be NULL if async_chunk_start has failed to compress, |
Nikolay Borisov | 4546d17 | 2019-01-03 10:50:03 +0200 | [diff] [blame] | 1258 | * in which case we don't have anything to submit, yet we need to |
| 1259 | * always adjust ->async_delalloc_pages as its paired with the init |
| 1260 | * happening in cow_file_range_async |
| 1261 | */ |
Nikolay Borisov | b532627 | 2019-03-12 17:20:25 +0200 | [diff] [blame] | 1262 | if (async_chunk->inode) |
| 1263 | submit_compressed_extents(async_chunk); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1264 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1265 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1266 | static noinline void async_cow_free(struct btrfs_work *work) |
| 1267 | { |
Nikolay Borisov | b532627 | 2019-03-12 17:20:25 +0200 | [diff] [blame] | 1268 | struct async_chunk *async_chunk; |
Nikolay Borisov | 97db120 | 2019-03-12 17:20:24 +0200 | [diff] [blame] | 1269 | |
Nikolay Borisov | b532627 | 2019-03-12 17:20:25 +0200 | [diff] [blame] | 1270 | async_chunk = container_of(work, struct async_chunk, work); |
| 1271 | if (async_chunk->inode) |
| 1272 | btrfs_add_delayed_iput(async_chunk->inode); |
Chris Mason | ec39f76 | 2019-07-10 12:28:17 -0700 | [diff] [blame] | 1273 | if (async_chunk->blkcg_css) |
| 1274 | css_put(async_chunk->blkcg_css); |
Nikolay Borisov | 97db120 | 2019-03-12 17:20:24 +0200 | [diff] [blame] | 1275 | /* |
| 1276 | * Since the pointer to 'pending' is at the beginning of the array of |
Nikolay Borisov | b532627 | 2019-03-12 17:20:25 +0200 | [diff] [blame] | 1277 | * async_chunk's, freeing it ensures the whole array has been freed. |
Nikolay Borisov | 97db120 | 2019-03-12 17:20:24 +0200 | [diff] [blame] | 1278 | */ |
Nikolay Borisov | b532627 | 2019-03-12 17:20:25 +0200 | [diff] [blame] | 1279 | if (atomic_dec_and_test(async_chunk->pending)) |
Nikolay Borisov | b1c16ac | 2019-04-01 11:29:57 +0300 | [diff] [blame] | 1280 | kvfree(async_chunk->pending); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1281 | } |
| 1282 | |
Nikolay Borisov | 751b643 | 2020-06-03 08:55:22 +0300 | [diff] [blame] | 1283 | static int cow_file_range_async(struct btrfs_inode *inode, |
Chris Mason | ec39f76 | 2019-07-10 12:28:17 -0700 | [diff] [blame] | 1284 | struct writeback_control *wbc, |
| 1285 | struct page *locked_page, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1286 | u64 start, u64 end, int *page_started, |
David Sterba | fac07d2 | 2019-10-29 18:28:57 +0100 | [diff] [blame] | 1287 | unsigned long *nr_written) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1288 | { |
Nikolay Borisov | 751b643 | 2020-06-03 08:55:22 +0300 | [diff] [blame] | 1289 | struct btrfs_fs_info *fs_info = inode->root->fs_info; |
Chris Mason | ec39f76 | 2019-07-10 12:28:17 -0700 | [diff] [blame] | 1290 | struct cgroup_subsys_state *blkcg_css = wbc_blkcg_css(wbc); |
Nikolay Borisov | 97db120 | 2019-03-12 17:20:24 +0200 | [diff] [blame] | 1291 | struct async_cow *ctx; |
| 1292 | struct async_chunk *async_chunk; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1293 | unsigned long nr_pages; |
| 1294 | u64 cur_end; |
Nikolay Borisov | 97db120 | 2019-03-12 17:20:24 +0200 | [diff] [blame] | 1295 | u64 num_chunks = DIV_ROUND_UP(end - start, SZ_512K); |
| 1296 | int i; |
| 1297 | bool should_compress; |
Nikolay Borisov | b1c16ac | 2019-04-01 11:29:57 +0300 | [diff] [blame] | 1298 | unsigned nofs_flag; |
David Sterba | fac07d2 | 2019-10-29 18:28:57 +0100 | [diff] [blame] | 1299 | const unsigned int write_flags = wbc_to_write_flags(wbc); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1300 | |
Nikolay Borisov | 751b643 | 2020-06-03 08:55:22 +0300 | [diff] [blame] | 1301 | unlock_extent(&inode->io_tree, start, end); |
Nikolay Borisov | 97db120 | 2019-03-12 17:20:24 +0200 | [diff] [blame] | 1302 | |
Nikolay Borisov | 751b643 | 2020-06-03 08:55:22 +0300 | [diff] [blame] | 1303 | if (inode->flags & BTRFS_INODE_NOCOMPRESS && |
Nikolay Borisov | 97db120 | 2019-03-12 17:20:24 +0200 | [diff] [blame] | 1304 | !btrfs_test_opt(fs_info, FORCE_COMPRESS)) { |
| 1305 | num_chunks = 1; |
| 1306 | should_compress = false; |
| 1307 | } else { |
| 1308 | should_compress = true; |
| 1309 | } |
| 1310 | |
Nikolay Borisov | b1c16ac | 2019-04-01 11:29:57 +0300 | [diff] [blame] | 1311 | nofs_flag = memalloc_nofs_save(); |
| 1312 | ctx = kvmalloc(struct_size(ctx, chunks, num_chunks), GFP_KERNEL); |
| 1313 | memalloc_nofs_restore(nofs_flag); |
| 1314 | |
Nikolay Borisov | 97db120 | 2019-03-12 17:20:24 +0200 | [diff] [blame] | 1315 | if (!ctx) { |
| 1316 | unsigned clear_bits = EXTENT_LOCKED | EXTENT_DELALLOC | |
| 1317 | EXTENT_DELALLOC_NEW | EXTENT_DEFRAG | |
| 1318 | EXTENT_DO_ACCOUNTING; |
| 1319 | unsigned long page_ops = PAGE_UNLOCK | PAGE_CLEAR_DIRTY | |
| 1320 | PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK | |
| 1321 | PAGE_SET_ERROR; |
| 1322 | |
Nikolay Borisov | 751b643 | 2020-06-03 08:55:22 +0300 | [diff] [blame] | 1323 | extent_clear_unlock_delalloc(inode, start, end, locked_page, |
| 1324 | clear_bits, page_ops); |
Nikolay Borisov | 97db120 | 2019-03-12 17:20:24 +0200 | [diff] [blame] | 1325 | return -ENOMEM; |
| 1326 | } |
| 1327 | |
| 1328 | async_chunk = ctx->chunks; |
| 1329 | atomic_set(&ctx->num_chunks, num_chunks); |
| 1330 | |
| 1331 | for (i = 0; i < num_chunks; i++) { |
| 1332 | if (should_compress) |
| 1333 | cur_end = min(end, start + SZ_512K - 1); |
| 1334 | else |
| 1335 | cur_end = end; |
| 1336 | |
Nikolay Borisov | bd4691a | 2019-01-03 10:50:01 +0200 | [diff] [blame] | 1337 | /* |
| 1338 | * igrab is called higher up in the call chain, take only the |
| 1339 | * lightweight reference for the callback lifetime |
| 1340 | */ |
Nikolay Borisov | 751b643 | 2020-06-03 08:55:22 +0300 | [diff] [blame] | 1341 | ihold(&inode->vfs_inode); |
Nikolay Borisov | 97db120 | 2019-03-12 17:20:24 +0200 | [diff] [blame] | 1342 | async_chunk[i].pending = &ctx->num_chunks; |
Nikolay Borisov | 751b643 | 2020-06-03 08:55:22 +0300 | [diff] [blame] | 1343 | async_chunk[i].inode = &inode->vfs_inode; |
Nikolay Borisov | 97db120 | 2019-03-12 17:20:24 +0200 | [diff] [blame] | 1344 | async_chunk[i].start = start; |
| 1345 | async_chunk[i].end = cur_end; |
Nikolay Borisov | 97db120 | 2019-03-12 17:20:24 +0200 | [diff] [blame] | 1346 | async_chunk[i].write_flags = write_flags; |
| 1347 | INIT_LIST_HEAD(&async_chunk[i].extents); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1348 | |
Chris Mason | 1d53c9e | 2019-07-10 12:28:16 -0700 | [diff] [blame] | 1349 | /* |
| 1350 | * The locked_page comes all the way from writepage and its |
| 1351 | * the original page we were actually given. As we spread |
| 1352 | * this large delalloc region across multiple async_chunk |
| 1353 | * structs, only the first struct needs a pointer to locked_page |
| 1354 | * |
| 1355 | * This way we don't need racey decisions about who is supposed |
| 1356 | * to unlock it. |
| 1357 | */ |
| 1358 | if (locked_page) { |
Chris Mason | ec39f76 | 2019-07-10 12:28:17 -0700 | [diff] [blame] | 1359 | /* |
| 1360 | * Depending on the compressibility, the pages might or |
| 1361 | * might not go through async. We want all of them to |
| 1362 | * be accounted against wbc once. Let's do it here |
| 1363 | * before the paths diverge. wbc accounting is used |
| 1364 | * only for foreign writeback detection and doesn't |
| 1365 | * need full accuracy. Just account the whole thing |
| 1366 | * against the first page. |
| 1367 | */ |
| 1368 | wbc_account_cgroup_owner(wbc, locked_page, |
| 1369 | cur_end - start); |
Chris Mason | 1d53c9e | 2019-07-10 12:28:16 -0700 | [diff] [blame] | 1370 | async_chunk[i].locked_page = locked_page; |
| 1371 | locked_page = NULL; |
| 1372 | } else { |
| 1373 | async_chunk[i].locked_page = NULL; |
| 1374 | } |
| 1375 | |
Chris Mason | ec39f76 | 2019-07-10 12:28:17 -0700 | [diff] [blame] | 1376 | if (blkcg_css != blkcg_root_css) { |
| 1377 | css_get(blkcg_css); |
| 1378 | async_chunk[i].blkcg_css = blkcg_css; |
| 1379 | } else { |
| 1380 | async_chunk[i].blkcg_css = NULL; |
| 1381 | } |
| 1382 | |
Omar Sandoval | a0cac0e | 2019-09-16 11:30:57 -0700 | [diff] [blame] | 1383 | btrfs_init_work(&async_chunk[i].work, async_cow_start, |
| 1384 | async_cow_submit, async_cow_free); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1385 | |
Nikolay Borisov | 97db120 | 2019-03-12 17:20:24 +0200 | [diff] [blame] | 1386 | nr_pages = DIV_ROUND_UP(cur_end - start, PAGE_SIZE); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1387 | atomic_add(nr_pages, &fs_info->async_delalloc_pages); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1388 | |
Nikolay Borisov | 97db120 | 2019-03-12 17:20:24 +0200 | [diff] [blame] | 1389 | btrfs_queue_work(fs_info->delalloc_workers, &async_chunk[i].work); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1390 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1391 | *nr_written += nr_pages; |
| 1392 | start = cur_end + 1; |
| 1393 | } |
| 1394 | *page_started = 1; |
| 1395 | return 0; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1396 | } |
| 1397 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 1398 | 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] | 1399 | u64 bytenr, u64 num_bytes) |
| 1400 | { |
| 1401 | int ret; |
| 1402 | struct btrfs_ordered_sum *sums; |
| 1403 | LIST_HEAD(list); |
| 1404 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1405 | ret = btrfs_lookup_csums_range(fs_info->csum_root, bytenr, |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 1406 | bytenr + num_bytes - 1, &list, 0); |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1407 | if (ret == 0 && list_empty(&list)) |
| 1408 | return 0; |
| 1409 | |
| 1410 | while (!list_empty(&list)) { |
| 1411 | sums = list_entry(list.next, struct btrfs_ordered_sum, list); |
| 1412 | list_del(&sums->list); |
| 1413 | kfree(sums); |
| 1414 | } |
Liu Bo | 5811375 | 2018-01-31 17:09:13 -0700 | [diff] [blame] | 1415 | if (ret < 0) |
| 1416 | return ret; |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1417 | return 1; |
| 1418 | } |
| 1419 | |
Nikolay Borisov | 8ba96f3 | 2020-06-03 08:55:20 +0300 | [diff] [blame] | 1420 | 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] | 1421 | const u64 start, const u64 end, |
| 1422 | int *page_started, unsigned long *nr_written) |
| 1423 | { |
Nikolay Borisov | 8ba96f3 | 2020-06-03 08:55:20 +0300 | [diff] [blame] | 1424 | const bool is_space_ino = btrfs_is_free_space_inode(inode); |
| 1425 | const bool is_reloc_ino = (inode->root->root_key.objectid == |
Filipe Manana | 6bd335b | 2020-06-08 13:33:05 +0100 | [diff] [blame] | 1426 | BTRFS_DATA_RELOC_TREE_OBJECTID); |
Filipe Manana | 2166e5e | 2020-05-27 11:16:19 +0100 | [diff] [blame] | 1427 | const u64 range_bytes = end + 1 - start; |
Nikolay Borisov | 8ba96f3 | 2020-06-03 08:55:20 +0300 | [diff] [blame] | 1428 | struct extent_io_tree *io_tree = &inode->io_tree; |
Filipe Manana | 467dc47 | 2020-05-27 11:16:07 +0100 | [diff] [blame] | 1429 | u64 range_start = start; |
| 1430 | u64 count; |
| 1431 | |
| 1432 | /* |
| 1433 | * If EXTENT_NORESERVE is set it means that when the buffered write was |
| 1434 | * made we had not enough available data space and therefore we did not |
| 1435 | * reserve data space for it, since we though we could do NOCOW for the |
| 1436 | * respective file range (either there is prealloc extent or the inode |
| 1437 | * has the NOCOW bit set). |
| 1438 | * |
| 1439 | * However when we need to fallback to COW mode (because for example the |
| 1440 | * block group for the corresponding extent was turned to RO mode by a |
| 1441 | * scrub or relocation) we need to do the following: |
| 1442 | * |
| 1443 | * 1) We increment the bytes_may_use counter of the data space info. |
| 1444 | * If COW succeeds, it allocates a new data extent and after doing |
| 1445 | * that it decrements the space info's bytes_may_use counter and |
| 1446 | * increments its bytes_reserved counter by the same amount (we do |
| 1447 | * this at btrfs_add_reserved_bytes()). So we need to increment the |
| 1448 | * bytes_may_use counter to compensate (when space is reserved at |
| 1449 | * buffered write time, the bytes_may_use counter is incremented); |
| 1450 | * |
| 1451 | * 2) We clear the EXTENT_NORESERVE bit from the range. We do this so |
| 1452 | * that if the COW path fails for any reason, it decrements (through |
| 1453 | * extent_clear_unlock_delalloc()) the bytes_may_use counter of the |
| 1454 | * data space info, which we incremented in the step above. |
Filipe Manana | 2166e5e | 2020-05-27 11:16:19 +0100 | [diff] [blame] | 1455 | * |
| 1456 | * 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] | 1457 | * space cache inode or an inode of the data relocation tree, we must |
| 1458 | * also increment bytes_may_use of the data space_info for the same |
| 1459 | * reason. Space caches and relocated data extents always get a prealloc |
Filipe Manana | 2166e5e | 2020-05-27 11:16:19 +0100 | [diff] [blame] | 1460 | * extent for them, however scrub or balance may have set the block |
Filipe Manana | 6bd335b | 2020-06-08 13:33:05 +0100 | [diff] [blame] | 1461 | * group that contains that extent to RO mode and therefore force COW |
| 1462 | * when starting writeback. |
Filipe Manana | 467dc47 | 2020-05-27 11:16:07 +0100 | [diff] [blame] | 1463 | */ |
Filipe Manana | 2166e5e | 2020-05-27 11:16:19 +0100 | [diff] [blame] | 1464 | count = count_range_bits(io_tree, &range_start, end, range_bytes, |
Filipe Manana | 467dc47 | 2020-05-27 11:16:07 +0100 | [diff] [blame] | 1465 | EXTENT_NORESERVE, 0); |
Filipe Manana | 6bd335b | 2020-06-08 13:33:05 +0100 | [diff] [blame] | 1466 | if (count > 0 || is_space_ino || is_reloc_ino) { |
| 1467 | u64 bytes = count; |
Nikolay Borisov | 8ba96f3 | 2020-06-03 08:55:20 +0300 | [diff] [blame] | 1468 | struct btrfs_fs_info *fs_info = inode->root->fs_info; |
Filipe Manana | 467dc47 | 2020-05-27 11:16:07 +0100 | [diff] [blame] | 1469 | struct btrfs_space_info *sinfo = fs_info->data_sinfo; |
| 1470 | |
Filipe Manana | 6bd335b | 2020-06-08 13:33:05 +0100 | [diff] [blame] | 1471 | if (is_space_ino || is_reloc_ino) |
| 1472 | bytes = range_bytes; |
| 1473 | |
Filipe Manana | 467dc47 | 2020-05-27 11:16:07 +0100 | [diff] [blame] | 1474 | spin_lock(&sinfo->lock); |
Filipe Manana | 2166e5e | 2020-05-27 11:16:19 +0100 | [diff] [blame] | 1475 | btrfs_space_info_update_bytes_may_use(fs_info, sinfo, bytes); |
Filipe Manana | 467dc47 | 2020-05-27 11:16:07 +0100 | [diff] [blame] | 1476 | spin_unlock(&sinfo->lock); |
| 1477 | |
Filipe Manana | 2166e5e | 2020-05-27 11:16:19 +0100 | [diff] [blame] | 1478 | if (count > 0) |
| 1479 | clear_extent_bit(io_tree, start, end, EXTENT_NORESERVE, |
| 1480 | 0, 0, NULL); |
Filipe Manana | 467dc47 | 2020-05-27 11:16:07 +0100 | [diff] [blame] | 1481 | } |
| 1482 | |
Nikolay Borisov | 8ba96f3 | 2020-06-03 08:55:20 +0300 | [diff] [blame] | 1483 | return cow_file_range(inode, locked_page, start, end, page_started, |
| 1484 | nr_written, 1); |
Filipe Manana | 467dc47 | 2020-05-27 11:16:07 +0100 | [diff] [blame] | 1485 | } |
| 1486 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1487 | /* |
| 1488 | * when nowcow writeback call back. This checks for snapshots or COW copies |
| 1489 | * of the extents that exist in the file, and COWs the file as required. |
| 1490 | * |
| 1491 | * If no cow copies or snapshots exist, we write directly to the existing |
| 1492 | * blocks on disk |
| 1493 | */ |
Nikolay Borisov | 968322c | 2020-06-03 08:55:21 +0300 | [diff] [blame] | 1494 | static noinline int run_delalloc_nocow(struct btrfs_inode *inode, |
Chris Mason | 7f366cf | 2009-03-12 20:12:45 -0400 | [diff] [blame] | 1495 | struct page *locked_page, |
Nikolay Borisov | 3e02484 | 2019-08-21 10:42:03 +0300 | [diff] [blame] | 1496 | const u64 start, const u64 end, |
| 1497 | int *page_started, int force, |
| 1498 | unsigned long *nr_written) |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1499 | { |
Nikolay Borisov | 968322c | 2020-06-03 08:55:21 +0300 | [diff] [blame] | 1500 | struct btrfs_fs_info *fs_info = inode->root->fs_info; |
| 1501 | struct btrfs_root *root = inode->root; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1502 | struct btrfs_path *path; |
Nikolay Borisov | 3e02484 | 2019-08-21 10:42:03 +0300 | [diff] [blame] | 1503 | u64 cow_start = (u64)-1; |
| 1504 | u64 cur_offset = start; |
Robbie Ko | 8ecebf4d | 2018-08-06 10:30:30 +0800 | [diff] [blame] | 1505 | int ret; |
Nikolay Borisov | 3e02484 | 2019-08-21 10:42:03 +0300 | [diff] [blame] | 1506 | bool check_prev = true; |
Nikolay Borisov | 968322c | 2020-06-03 08:55:21 +0300 | [diff] [blame] | 1507 | const bool freespace_inode = btrfs_is_free_space_inode(inode); |
| 1508 | u64 ino = btrfs_ino(inode); |
Nikolay Borisov | 762bf09 | 2019-08-22 17:24:20 +0300 | [diff] [blame] | 1509 | bool nocow = false; |
| 1510 | u64 disk_bytenr = 0; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1511 | |
| 1512 | path = btrfs_alloc_path(); |
Josef Bacik | 17ca04a | 2012-05-31 15:58:55 -0400 | [diff] [blame] | 1513 | if (!path) { |
Nikolay Borisov | 968322c | 2020-06-03 08:55:21 +0300 | [diff] [blame] | 1514 | extent_clear_unlock_delalloc(inode, start, end, locked_page, |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1515 | EXTENT_LOCKED | EXTENT_DELALLOC | |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 1516 | EXTENT_DO_ACCOUNTING | |
| 1517 | EXTENT_DEFRAG, PAGE_UNLOCK | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1518 | PAGE_CLEAR_DIRTY | |
| 1519 | PAGE_SET_WRITEBACK | |
| 1520 | PAGE_END_WRITEBACK); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 1521 | return -ENOMEM; |
Josef Bacik | 17ca04a | 2012-05-31 15:58:55 -0400 | [diff] [blame] | 1522 | } |
Li Zefan | 82d5902 | 2011-04-20 10:33:24 +0800 | [diff] [blame] | 1523 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1524 | while (1) { |
Nikolay Borisov | 3e02484 | 2019-08-21 10:42:03 +0300 | [diff] [blame] | 1525 | struct btrfs_key found_key; |
| 1526 | struct btrfs_file_extent_item *fi; |
| 1527 | struct extent_buffer *leaf; |
| 1528 | u64 extent_end; |
| 1529 | u64 extent_offset; |
Nikolay Borisov | 3e02484 | 2019-08-21 10:42:03 +0300 | [diff] [blame] | 1530 | u64 num_bytes = 0; |
| 1531 | u64 disk_num_bytes; |
Nikolay Borisov | 3e02484 | 2019-08-21 10:42:03 +0300 | [diff] [blame] | 1532 | u64 ram_bytes; |
| 1533 | int extent_type; |
Nikolay Borisov | 762bf09 | 2019-08-22 17:24:20 +0300 | [diff] [blame] | 1534 | |
| 1535 | nocow = false; |
Nikolay Borisov | 3e02484 | 2019-08-21 10:42:03 +0300 | [diff] [blame] | 1536 | |
Liu Bo | e4c3b2d | 2017-01-30 12:25:28 -0800 | [diff] [blame] | 1537 | ret = btrfs_lookup_file_extent(NULL, root, path, ino, |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1538 | cur_offset, 0); |
Josef Bacik | d788a34 | 2013-10-25 16:55:08 -0400 | [diff] [blame] | 1539 | if (ret < 0) |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1540 | goto error; |
Nikolay Borisov | a6bd9cd | 2019-08-21 10:42:57 +0300 | [diff] [blame] | 1541 | |
| 1542 | /* |
| 1543 | * If there is no extent for our range when doing the initial |
| 1544 | * search, then go back to the previous slot as it will be the |
| 1545 | * one containing the search offset |
| 1546 | */ |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1547 | if (ret > 0 && path->slots[0] > 0 && check_prev) { |
| 1548 | leaf = path->nodes[0]; |
| 1549 | btrfs_item_key_to_cpu(leaf, &found_key, |
| 1550 | path->slots[0] - 1); |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 1551 | if (found_key.objectid == ino && |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1552 | found_key.type == BTRFS_EXTENT_DATA_KEY) |
| 1553 | path->slots[0]--; |
| 1554 | } |
Nikolay Borisov | 3e02484 | 2019-08-21 10:42:03 +0300 | [diff] [blame] | 1555 | check_prev = false; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1556 | next_slot: |
Nikolay Borisov | a6bd9cd | 2019-08-21 10:42:57 +0300 | [diff] [blame] | 1557 | /* Go to next leaf if we have exhausted the current one */ |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1558 | leaf = path->nodes[0]; |
| 1559 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { |
| 1560 | ret = btrfs_next_leaf(root, path); |
Liu Bo | e891669 | 2018-01-25 11:02:50 -0700 | [diff] [blame] | 1561 | if (ret < 0) { |
| 1562 | if (cow_start != (u64)-1) |
| 1563 | cur_offset = cow_start; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1564 | goto error; |
Liu Bo | e891669 | 2018-01-25 11:02:50 -0700 | [diff] [blame] | 1565 | } |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1566 | if (ret > 0) |
| 1567 | break; |
| 1568 | leaf = path->nodes[0]; |
| 1569 | } |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1570 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1571 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1572 | |
Nikolay Borisov | a6bd9cd | 2019-08-21 10:42:57 +0300 | [diff] [blame] | 1573 | /* Didn't find anything for our INO */ |
Filipe Manana | 1d512cb | 2015-11-09 00:33:58 +0000 | [diff] [blame] | 1574 | if (found_key.objectid > ino) |
| 1575 | break; |
Nikolay Borisov | a6bd9cd | 2019-08-21 10:42:57 +0300 | [diff] [blame] | 1576 | /* |
| 1577 | * Keep searching until we find an EXTENT_ITEM or there are no |
| 1578 | * more extents for this inode |
| 1579 | */ |
Filipe Manana | 1d512cb | 2015-11-09 00:33:58 +0000 | [diff] [blame] | 1580 | if (WARN_ON_ONCE(found_key.objectid < ino) || |
| 1581 | found_key.type < BTRFS_EXTENT_DATA_KEY) { |
| 1582 | path->slots[0]++; |
| 1583 | goto next_slot; |
| 1584 | } |
Nikolay Borisov | a6bd9cd | 2019-08-21 10:42:57 +0300 | [diff] [blame] | 1585 | |
| 1586 | /* Found key is not EXTENT_DATA_KEY or starts after req range */ |
Filipe Manana | 1d512cb | 2015-11-09 00:33:58 +0000 | [diff] [blame] | 1587 | if (found_key.type > BTRFS_EXTENT_DATA_KEY || |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1588 | found_key.offset > end) |
| 1589 | break; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1590 | |
Nikolay Borisov | a6bd9cd | 2019-08-21 10:42:57 +0300 | [diff] [blame] | 1591 | /* |
| 1592 | * If the found extent starts after requested offset, then |
| 1593 | * adjust extent_end to be right before this extent begins |
| 1594 | */ |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1595 | if (found_key.offset > cur_offset) { |
| 1596 | extent_end = found_key.offset; |
Chris Mason | e9061e2 | 2009-10-09 09:57:45 -0400 | [diff] [blame] | 1597 | extent_type = 0; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1598 | goto out_check; |
| 1599 | } |
Chris Mason | c31f883 | 2008-01-08 15:46:31 -0500 | [diff] [blame] | 1600 | |
Nikolay Borisov | a6bd9cd | 2019-08-21 10:42:57 +0300 | [diff] [blame] | 1601 | /* |
| 1602 | * Found extent which begins before our range and potentially |
| 1603 | * intersect it |
| 1604 | */ |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1605 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 1606 | struct btrfs_file_extent_item); |
| 1607 | extent_type = btrfs_file_extent_type(leaf, fi); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1608 | |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 1609 | ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1610 | if (extent_type == BTRFS_FILE_EXTENT_REG || |
| 1611 | extent_type == BTRFS_FILE_EXTENT_PREALLOC) { |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1612 | disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1613 | extent_offset = btrfs_file_extent_offset(leaf, fi); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1614 | extent_end = found_key.offset + |
| 1615 | btrfs_file_extent_num_bytes(leaf, fi); |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 1616 | disk_num_bytes = |
| 1617 | btrfs_file_extent_disk_num_bytes(leaf, fi); |
Nikolay Borisov | a6bd9cd | 2019-08-21 10:42:57 +0300 | [diff] [blame] | 1618 | /* |
Filipe Manana | de7999a | 2019-12-11 09:01:40 +0000 | [diff] [blame] | 1619 | * If the extent we got ends before our current offset, |
| 1620 | * skip to the next extent. |
Nikolay Borisov | a6bd9cd | 2019-08-21 10:42:57 +0300 | [diff] [blame] | 1621 | */ |
Filipe Manana | de7999a | 2019-12-11 09:01:40 +0000 | [diff] [blame] | 1622 | if (extent_end <= cur_offset) { |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1623 | path->slots[0]++; |
| 1624 | goto next_slot; |
| 1625 | } |
Nikolay Borisov | a6bd9cd | 2019-08-21 10:42:57 +0300 | [diff] [blame] | 1626 | /* Skip holes */ |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1627 | if (disk_bytenr == 0) |
| 1628 | goto out_check; |
Nikolay Borisov | a6bd9cd | 2019-08-21 10:42:57 +0300 | [diff] [blame] | 1629 | /* Skip compressed/encrypted/encoded extents */ |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1630 | if (btrfs_file_extent_compression(leaf, fi) || |
| 1631 | btrfs_file_extent_encryption(leaf, fi) || |
| 1632 | btrfs_file_extent_other_encoding(leaf, fi)) |
| 1633 | goto out_check; |
Ethan Lien | 78d4295 | 2018-05-17 14:58:29 +0800 | [diff] [blame] | 1634 | /* |
Nikolay Borisov | a6bd9cd | 2019-08-21 10:42:57 +0300 | [diff] [blame] | 1635 | * If extent is created before the last volume's snapshot |
| 1636 | * this implies the extent is shared, hence we can't do |
| 1637 | * nocow. This is the same check as in |
| 1638 | * btrfs_cross_ref_exist but without calling |
| 1639 | * btrfs_search_slot. |
Ethan Lien | 78d4295 | 2018-05-17 14:58:29 +0800 | [diff] [blame] | 1640 | */ |
Nikolay Borisov | 3e02484 | 2019-08-21 10:42:03 +0300 | [diff] [blame] | 1641 | if (!freespace_inode && |
Lu Fengqi | 27a7ff5 | 2018-11-29 17:31:32 +0800 | [diff] [blame] | 1642 | btrfs_file_extent_generation(leaf, fi) <= |
Ethan Lien | 78d4295 | 2018-05-17 14:58:29 +0800 | [diff] [blame] | 1643 | btrfs_root_last_snapshot(&root->root_item)) |
| 1644 | goto out_check; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1645 | if (extent_type == BTRFS_FILE_EXTENT_REG && !force) |
| 1646 | goto out_check; |
Nikolay Borisov | a6bd9cd | 2019-08-21 10:42:57 +0300 | [diff] [blame] | 1647 | /* If extent is RO, we must COW it */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 1648 | if (btrfs_extent_readonly(fs_info, disk_bytenr)) |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1649 | goto out_check; |
Liu Bo | 5811375 | 2018-01-31 17:09:13 -0700 | [diff] [blame] | 1650 | ret = btrfs_cross_ref_exist(root, ino, |
| 1651 | found_key.offset - |
Boris Burkov | a84d5d4 | 2020-08-18 11:00:05 -0700 | [diff] [blame] | 1652 | extent_offset, disk_bytenr, false); |
Liu Bo | 5811375 | 2018-01-31 17:09:13 -0700 | [diff] [blame] | 1653 | if (ret) { |
| 1654 | /* |
| 1655 | * ret could be -EIO if the above fails to read |
| 1656 | * metadata. |
| 1657 | */ |
| 1658 | if (ret < 0) { |
| 1659 | if (cow_start != (u64)-1) |
| 1660 | cur_offset = cow_start; |
| 1661 | goto error; |
| 1662 | } |
| 1663 | |
Nikolay Borisov | 3e02484 | 2019-08-21 10:42:03 +0300 | [diff] [blame] | 1664 | WARN_ON_ONCE(freespace_inode); |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1665 | goto out_check; |
Liu Bo | 5811375 | 2018-01-31 17:09:13 -0700 | [diff] [blame] | 1666 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1667 | disk_bytenr += extent_offset; |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1668 | disk_bytenr += cur_offset - found_key.offset; |
| 1669 | num_bytes = min(end + 1, extent_end) - cur_offset; |
| 1670 | /* |
Nikolay Borisov | a6bd9cd | 2019-08-21 10:42:57 +0300 | [diff] [blame] | 1671 | * If there are pending snapshots for this root, we |
| 1672 | * fall into common COW way |
Wang Shilong | e9894fd | 2014-03-27 11:12:25 +0800 | [diff] [blame] | 1673 | */ |
Nikolay Borisov | 3e02484 | 2019-08-21 10:42:03 +0300 | [diff] [blame] | 1674 | if (!freespace_inode && atomic_read(&root->snapshot_force_cow)) |
Robbie Ko | 8ecebf4d | 2018-08-06 10:30:30 +0800 | [diff] [blame] | 1675 | goto out_check; |
Wang Shilong | e9894fd | 2014-03-27 11:12:25 +0800 | [diff] [blame] | 1676 | /* |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1677 | * force cow if csum exists in the range. |
| 1678 | * this ensure that csum for a given extent are |
| 1679 | * either valid or do not exist. |
| 1680 | */ |
Liu Bo | 5811375 | 2018-01-31 17:09:13 -0700 | [diff] [blame] | 1681 | ret = csum_exist_in_range(fs_info, disk_bytenr, |
| 1682 | num_bytes); |
| 1683 | if (ret) { |
Liu Bo | 5811375 | 2018-01-31 17:09:13 -0700 | [diff] [blame] | 1684 | /* |
| 1685 | * ret could be -EIO if the above fails to read |
| 1686 | * metadata. |
| 1687 | */ |
| 1688 | if (ret < 0) { |
| 1689 | if (cow_start != (u64)-1) |
| 1690 | cur_offset = cow_start; |
| 1691 | goto error; |
| 1692 | } |
Nikolay Borisov | 3e02484 | 2019-08-21 10:42:03 +0300 | [diff] [blame] | 1693 | WARN_ON_ONCE(freespace_inode); |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1694 | goto out_check; |
Robbie Ko | 91e1f56 | 2016-10-07 10:01:29 +0800 | [diff] [blame] | 1695 | } |
Robbie Ko | 8ecebf4d | 2018-08-06 10:30:30 +0800 | [diff] [blame] | 1696 | if (!btrfs_inc_nocow_writers(fs_info, disk_bytenr)) |
Filipe Manana | f78c436 | 2016-05-09 13:15:41 +0100 | [diff] [blame] | 1697 | goto out_check; |
Nikolay Borisov | 3e02484 | 2019-08-21 10:42:03 +0300 | [diff] [blame] | 1698 | nocow = true; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1699 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
Nikolay Borisov | e8e2100 | 2019-08-22 17:25:23 +0300 | [diff] [blame] | 1700 | extent_end = found_key.offset + ram_bytes; |
| 1701 | extent_end = ALIGN(extent_end, fs_info->sectorsize); |
Nikolay Borisov | 922f051 | 2019-08-05 17:47:06 +0300 | [diff] [blame] | 1702 | /* Skip extents outside of our requested range */ |
| 1703 | if (extent_end <= start) { |
| 1704 | path->slots[0]++; |
| 1705 | goto next_slot; |
| 1706 | } |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1707 | } else { |
Nikolay Borisov | e8e2100 | 2019-08-22 17:25:23 +0300 | [diff] [blame] | 1708 | /* If this triggers then we have a memory corruption */ |
Arnd Bergmann | 290342f | 2019-03-25 14:02:25 +0100 | [diff] [blame] | 1709 | BUG(); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1710 | } |
| 1711 | out_check: |
Nikolay Borisov | a6bd9cd | 2019-08-21 10:42:57 +0300 | [diff] [blame] | 1712 | /* |
| 1713 | * If nocow is false then record the beginning of the range |
| 1714 | * that needs to be COWed |
| 1715 | */ |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1716 | if (!nocow) { |
| 1717 | if (cow_start == (u64)-1) |
| 1718 | cow_start = cur_offset; |
| 1719 | cur_offset = extent_end; |
| 1720 | if (cur_offset > end) |
| 1721 | break; |
| 1722 | path->slots[0]++; |
| 1723 | goto next_slot; |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1724 | } |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1725 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1726 | btrfs_release_path(path); |
Nikolay Borisov | a6bd9cd | 2019-08-21 10:42:57 +0300 | [diff] [blame] | 1727 | |
| 1728 | /* |
| 1729 | * COW range from cow_start to found_key.offset - 1. As the key |
| 1730 | * will contain the beginning of the first extent that can be |
| 1731 | * NOCOW, following one which needs to be COW'ed |
| 1732 | */ |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1733 | if (cow_start != (u64)-1) { |
Nikolay Borisov | 968322c | 2020-06-03 08:55:21 +0300 | [diff] [blame] | 1734 | ret = fallback_to_cow(inode, locked_page, |
Nikolay Borisov | 8ba96f3 | 2020-06-03 08:55:20 +0300 | [diff] [blame] | 1735 | cow_start, found_key.offset - 1, |
Filipe Manana | 467dc47 | 2020-05-27 11:16:07 +0100 | [diff] [blame] | 1736 | page_started, nr_written); |
Josef Bacik | 230ed39 | 2020-07-06 09:14:12 -0400 | [diff] [blame] | 1737 | if (ret) |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1738 | goto error; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1739 | cow_start = (u64)-1; |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1740 | } |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1741 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1742 | if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) { |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 1743 | u64 orig_start = found_key.offset - extent_offset; |
Nikolay Borisov | 3e02484 | 2019-08-21 10:42:03 +0300 | [diff] [blame] | 1744 | struct extent_map *em; |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 1745 | |
Nikolay Borisov | 968322c | 2020-06-03 08:55:21 +0300 | [diff] [blame] | 1746 | em = create_io_em(inode, cur_offset, num_bytes, |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 1747 | orig_start, |
| 1748 | disk_bytenr, /* block_start */ |
| 1749 | num_bytes, /* block_len */ |
| 1750 | disk_num_bytes, /* orig_block_len */ |
| 1751 | ram_bytes, BTRFS_COMPRESS_NONE, |
| 1752 | BTRFS_ORDERED_PREALLOC); |
| 1753 | if (IS_ERR(em)) { |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 1754 | ret = PTR_ERR(em); |
| 1755 | goto error; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1756 | } |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 1757 | free_extent_map(em); |
Nikolay Borisov | 968322c | 2020-06-03 08:55:21 +0300 | [diff] [blame] | 1758 | ret = btrfs_add_ordered_extent(inode, cur_offset, |
Nikolay Borisov | bb55f62 | 2019-08-05 17:47:05 +0300 | [diff] [blame] | 1759 | disk_bytenr, num_bytes, |
| 1760 | num_bytes, |
| 1761 | BTRFS_ORDERED_PREALLOC); |
Nikolay Borisov | 762bf09 | 2019-08-22 17:24:20 +0300 | [diff] [blame] | 1762 | if (ret) { |
Nikolay Borisov | 968322c | 2020-06-03 08:55:21 +0300 | [diff] [blame] | 1763 | btrfs_drop_extent_cache(inode, cur_offset, |
Nikolay Borisov | 762bf09 | 2019-08-22 17:24:20 +0300 | [diff] [blame] | 1764 | cur_offset + num_bytes - 1, |
| 1765 | 0); |
| 1766 | goto error; |
| 1767 | } |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1768 | } else { |
Nikolay Borisov | 968322c | 2020-06-03 08:55:21 +0300 | [diff] [blame] | 1769 | ret = btrfs_add_ordered_extent(inode, cur_offset, |
Nikolay Borisov | bb55f62 | 2019-08-05 17:47:05 +0300 | [diff] [blame] | 1770 | disk_bytenr, num_bytes, |
| 1771 | num_bytes, |
| 1772 | BTRFS_ORDERED_NOCOW); |
Nikolay Borisov | 762bf09 | 2019-08-22 17:24:20 +0300 | [diff] [blame] | 1773 | if (ret) |
| 1774 | goto error; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1775 | } |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1776 | |
Filipe Manana | f78c436 | 2016-05-09 13:15:41 +0100 | [diff] [blame] | 1777 | if (nocow) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1778 | btrfs_dec_nocow_writers(fs_info, disk_bytenr); |
Nikolay Borisov | 762bf09 | 2019-08-22 17:24:20 +0300 | [diff] [blame] | 1779 | nocow = false; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1780 | |
Yan, Zheng | efa5646 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 1781 | if (root->root_key.objectid == |
Qu Wenruo | 4dbd80f | 2017-03-08 10:25:51 +0800 | [diff] [blame] | 1782 | BTRFS_DATA_RELOC_TREE_OBJECTID) |
| 1783 | /* |
| 1784 | * Error handled later, as we must prevent |
| 1785 | * extent_clear_unlock_delalloc() in error handler |
| 1786 | * from freeing metadata of created ordered extent. |
| 1787 | */ |
Nikolay Borisov | 968322c | 2020-06-03 08:55:21 +0300 | [diff] [blame] | 1788 | ret = btrfs_reloc_clone_csums(inode, cur_offset, |
Yan, Zheng | efa5646 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 1789 | num_bytes); |
Yan, Zheng | efa5646 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 1790 | |
Nikolay Borisov | 968322c | 2020-06-03 08:55:21 +0300 | [diff] [blame] | 1791 | extent_clear_unlock_delalloc(inode, cur_offset, |
Nikolay Borisov | 74e9194 | 2019-07-17 16:18:16 +0300 | [diff] [blame] | 1792 | cur_offset + num_bytes - 1, |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1793 | locked_page, EXTENT_LOCKED | |
Wang Xiaoguang | 1851309 | 2016-07-25 15:51:40 +0800 | [diff] [blame] | 1794 | EXTENT_DELALLOC | |
| 1795 | EXTENT_CLEAR_DATA_RESV, |
| 1796 | PAGE_UNLOCK | PAGE_SET_PRIVATE2); |
| 1797 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1798 | cur_offset = extent_end; |
Qu Wenruo | 4dbd80f | 2017-03-08 10:25:51 +0800 | [diff] [blame] | 1799 | |
| 1800 | /* |
| 1801 | * btrfs_reloc_clone_csums() error, now we're OK to call error |
| 1802 | * handler, as metadata for created ordered extent will only |
| 1803 | * be freed by btrfs_finish_ordered_io(). |
| 1804 | */ |
| 1805 | if (ret) |
| 1806 | goto error; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1807 | if (cur_offset > end) |
| 1808 | break; |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1809 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1810 | btrfs_release_path(path); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1811 | |
Robbie Ko | 506481b | 2018-10-30 18:04:04 +0800 | [diff] [blame] | 1812 | if (cur_offset <= end && cow_start == (u64)-1) |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1813 | cow_start = cur_offset; |
Josef Bacik | 17ca04a | 2012-05-31 15:58:55 -0400 | [diff] [blame] | 1814 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1815 | if (cow_start != (u64)-1) { |
Robbie Ko | 506481b | 2018-10-30 18:04:04 +0800 | [diff] [blame] | 1816 | cur_offset = end; |
Nikolay Borisov | 968322c | 2020-06-03 08:55:21 +0300 | [diff] [blame] | 1817 | ret = fallback_to_cow(inode, locked_page, cow_start, end, |
| 1818 | page_started, nr_written); |
Josef Bacik | d788a34 | 2013-10-25 16:55:08 -0400 | [diff] [blame] | 1819 | if (ret) |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1820 | goto error; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1821 | } |
| 1822 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1823 | error: |
Nikolay Borisov | 762bf09 | 2019-08-22 17:24:20 +0300 | [diff] [blame] | 1824 | if (nocow) |
| 1825 | btrfs_dec_nocow_writers(fs_info, disk_bytenr); |
| 1826 | |
Josef Bacik | 17ca04a | 2012-05-31 15:58:55 -0400 | [diff] [blame] | 1827 | if (ret && cur_offset < end) |
Nikolay Borisov | 968322c | 2020-06-03 08:55:21 +0300 | [diff] [blame] | 1828 | extent_clear_unlock_delalloc(inode, cur_offset, end, |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1829 | locked_page, EXTENT_LOCKED | |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 1830 | EXTENT_DELALLOC | EXTENT_DEFRAG | |
| 1831 | EXTENT_DO_ACCOUNTING, PAGE_UNLOCK | |
| 1832 | PAGE_CLEAR_DIRTY | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1833 | PAGE_SET_WRITEBACK | |
| 1834 | PAGE_END_WRITEBACK); |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1835 | btrfs_free_path(path); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1836 | return ret; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1837 | } |
| 1838 | |
Nikolay Borisov | 0c49422 | 2020-06-03 08:55:28 +0300 | [diff] [blame] | 1839 | 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] | 1840 | { |
| 1841 | |
Nikolay Borisov | 0c49422 | 2020-06-03 08:55:28 +0300 | [diff] [blame] | 1842 | if (!(inode->flags & BTRFS_INODE_NODATACOW) && |
| 1843 | !(inode->flags & BTRFS_INODE_PREALLOC)) |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 1844 | return 0; |
| 1845 | |
| 1846 | /* |
| 1847 | * @defrag_bytes is a hint value, no spinlock held here, |
| 1848 | * if is not zero, it means the file is defragging. |
| 1849 | * Force cow if given extent needs to be defragged. |
| 1850 | */ |
Nikolay Borisov | 0c49422 | 2020-06-03 08:55:28 +0300 | [diff] [blame] | 1851 | if (inode->defrag_bytes && |
| 1852 | test_range_bit(&inode->io_tree, start, end, EXTENT_DEFRAG, 0, NULL)) |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 1853 | return 1; |
| 1854 | |
| 1855 | return 0; |
| 1856 | } |
| 1857 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1858 | /* |
Nikolay Borisov | 5eaad97 | 2018-11-01 14:09:46 +0200 | [diff] [blame] | 1859 | * Function to process delayed allocation (create CoW) for ranges which are |
| 1860 | * being touched for the first time. |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1861 | */ |
Nikolay Borisov | 98456b9 | 2020-06-03 08:55:29 +0300 | [diff] [blame] | 1862 | 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] | 1863 | u64 start, u64 end, int *page_started, unsigned long *nr_written, |
| 1864 | struct writeback_control *wbc) |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1865 | { |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1866 | int ret; |
Nikolay Borisov | 98456b9 | 2020-06-03 08:55:29 +0300 | [diff] [blame] | 1867 | int force_cow = need_force_cow(inode, start, end); |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1868 | |
Nikolay Borisov | 98456b9 | 2020-06-03 08:55:29 +0300 | [diff] [blame] | 1869 | if (inode->flags & BTRFS_INODE_NODATACOW && !force_cow) { |
| 1870 | ret = run_delalloc_nocow(inode, locked_page, start, end, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1871 | page_started, 1, nr_written); |
Nikolay Borisov | 98456b9 | 2020-06-03 08:55:29 +0300 | [diff] [blame] | 1872 | } else if (inode->flags & BTRFS_INODE_PREALLOC && !force_cow) { |
| 1873 | ret = run_delalloc_nocow(inode, locked_page, start, end, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1874 | page_started, 0, nr_written); |
Nikolay Borisov | 98456b9 | 2020-06-03 08:55:29 +0300 | [diff] [blame] | 1875 | } else if (!inode_can_compress(inode) || |
| 1876 | !inode_need_compress(inode, start, end)) { |
| 1877 | ret = cow_file_range(inode, locked_page, start, end, |
| 1878 | page_started, nr_written, 1); |
Josef Bacik | 7ddf5a4 | 2012-06-08 15:26:47 -0400 | [diff] [blame] | 1879 | } else { |
Nikolay Borisov | 98456b9 | 2020-06-03 08:55:29 +0300 | [diff] [blame] | 1880 | set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, &inode->runtime_flags); |
| 1881 | ret = cow_file_range_async(inode, wbc, locked_page, start, end, |
David Sterba | fac07d2 | 2019-10-29 18:28:57 +0100 | [diff] [blame] | 1882 | page_started, nr_written); |
Josef Bacik | 7ddf5a4 | 2012-06-08 15:26:47 -0400 | [diff] [blame] | 1883 | } |
Qu Wenruo | 52427260 | 2017-03-08 10:25:52 +0800 | [diff] [blame] | 1884 | if (ret) |
Nikolay Borisov | 98456b9 | 2020-06-03 08:55:29 +0300 | [diff] [blame] | 1885 | btrfs_cleanup_ordered_extents(inode, locked_page, start, |
Nikolay Borisov | d1051d6 | 2018-11-21 17:10:52 +0200 | [diff] [blame] | 1886 | end - start + 1); |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 1887 | return ret; |
| 1888 | } |
| 1889 | |
Nikolay Borisov | abbb55f | 2018-11-01 14:09:53 +0200 | [diff] [blame] | 1890 | void btrfs_split_delalloc_extent(struct inode *inode, |
| 1891 | struct extent_state *orig, u64 split) |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1892 | { |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1893 | u64 size; |
| 1894 | |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1895 | /* not delalloc, ignore it */ |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1896 | if (!(orig->state & EXTENT_DELALLOC)) |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 1897 | return; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1898 | |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1899 | size = orig->end - orig->start + 1; |
| 1900 | if (size > BTRFS_MAX_EXTENT_SIZE) { |
David Sterba | 823bb20 | 2017-01-04 11:09:51 +0100 | [diff] [blame] | 1901 | u32 num_extents; |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1902 | u64 new_size; |
| 1903 | |
| 1904 | /* |
Nikolay Borisov | 5c84819 | 2018-11-01 14:09:52 +0200 | [diff] [blame] | 1905 | * See the explanation in btrfs_merge_delalloc_extent, the same |
Josef Bacik | ba11721 | 2015-03-13 15:01:24 -0400 | [diff] [blame] | 1906 | * applies here, just in reverse. |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1907 | */ |
| 1908 | new_size = orig->end - split + 1; |
David Sterba | 823bb20 | 2017-01-04 11:09:51 +0100 | [diff] [blame] | 1909 | num_extents = count_max_extents(new_size); |
Josef Bacik | ba11721 | 2015-03-13 15:01:24 -0400 | [diff] [blame] | 1910 | new_size = split - orig->start; |
David Sterba | 823bb20 | 2017-01-04 11:09:51 +0100 | [diff] [blame] | 1911 | num_extents += count_max_extents(new_size); |
| 1912 | if (count_max_extents(size) >= num_extents) |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1913 | return; |
| 1914 | } |
| 1915 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1916 | spin_lock(&BTRFS_I(inode)->lock); |
Josef Bacik | 8b62f87 | 2017-10-19 14:15:55 -0400 | [diff] [blame] | 1917 | btrfs_mod_outstanding_extents(BTRFS_I(inode), 1); |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1918 | spin_unlock(&BTRFS_I(inode)->lock); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1919 | } |
| 1920 | |
| 1921 | /* |
Nikolay Borisov | 5c84819 | 2018-11-01 14:09:52 +0200 | [diff] [blame] | 1922 | * Handle merged delayed allocation extents so we can keep track of new extents |
| 1923 | * that are just merged onto old extents, such as when we are doing sequential |
| 1924 | * 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] | 1925 | */ |
Nikolay Borisov | 5c84819 | 2018-11-01 14:09:52 +0200 | [diff] [blame] | 1926 | void btrfs_merge_delalloc_extent(struct inode *inode, struct extent_state *new, |
| 1927 | struct extent_state *other) |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1928 | { |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1929 | u64 new_size, old_size; |
David Sterba | 823bb20 | 2017-01-04 11:09:51 +0100 | [diff] [blame] | 1930 | u32 num_extents; |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1931 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1932 | /* not delalloc, ignore it */ |
| 1933 | if (!(other->state & EXTENT_DELALLOC)) |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 1934 | return; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1935 | |
Josef Bacik | 8461a3d | 2015-03-13 15:12:08 -0400 | [diff] [blame] | 1936 | if (new->start > other->start) |
| 1937 | new_size = new->end - other->start + 1; |
| 1938 | else |
| 1939 | new_size = other->end - new->start + 1; |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1940 | |
| 1941 | /* we're not bigger than the max, unreserve the space and go */ |
| 1942 | if (new_size <= BTRFS_MAX_EXTENT_SIZE) { |
| 1943 | spin_lock(&BTRFS_I(inode)->lock); |
Josef Bacik | 8b62f87 | 2017-10-19 14:15:55 -0400 | [diff] [blame] | 1944 | btrfs_mod_outstanding_extents(BTRFS_I(inode), -1); |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1945 | spin_unlock(&BTRFS_I(inode)->lock); |
| 1946 | return; |
| 1947 | } |
| 1948 | |
| 1949 | /* |
Josef Bacik | ba11721 | 2015-03-13 15:01:24 -0400 | [diff] [blame] | 1950 | * We have to add up either side to figure out how many extents were |
| 1951 | * accounted for before we merged into one big extent. If the number of |
| 1952 | * extents we accounted for is <= the amount we need for the new range |
| 1953 | * then we can return, otherwise drop. Think of it like this |
| 1954 | * |
| 1955 | * [ 4k][MAX_SIZE] |
| 1956 | * |
| 1957 | * So we've grown the extent by a MAX_SIZE extent, this would mean we |
| 1958 | * need 2 outstanding extents, on one side we have 1 and the other side |
| 1959 | * we have 1 so they are == and we can return. But in this case |
| 1960 | * |
| 1961 | * [MAX_SIZE+4k][MAX_SIZE+4k] |
| 1962 | * |
| 1963 | * Each range on their own accounts for 2 extents, but merged together |
| 1964 | * they are only 3 extents worth of accounting, so we need to drop in |
| 1965 | * this case. |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1966 | */ |
Josef Bacik | ba11721 | 2015-03-13 15:01:24 -0400 | [diff] [blame] | 1967 | old_size = other->end - other->start + 1; |
David Sterba | 823bb20 | 2017-01-04 11:09:51 +0100 | [diff] [blame] | 1968 | num_extents = count_max_extents(old_size); |
Josef Bacik | ba11721 | 2015-03-13 15:01:24 -0400 | [diff] [blame] | 1969 | old_size = new->end - new->start + 1; |
David Sterba | 823bb20 | 2017-01-04 11:09:51 +0100 | [diff] [blame] | 1970 | num_extents += count_max_extents(old_size); |
| 1971 | if (count_max_extents(new_size) >= num_extents) |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1972 | return; |
| 1973 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1974 | spin_lock(&BTRFS_I(inode)->lock); |
Josef Bacik | 8b62f87 | 2017-10-19 14:15:55 -0400 | [diff] [blame] | 1975 | btrfs_mod_outstanding_extents(BTRFS_I(inode), -1); |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1976 | spin_unlock(&BTRFS_I(inode)->lock); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1977 | } |
| 1978 | |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 1979 | static void btrfs_add_delalloc_inodes(struct btrfs_root *root, |
| 1980 | struct inode *inode) |
| 1981 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1982 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
| 1983 | |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 1984 | spin_lock(&root->delalloc_lock); |
| 1985 | if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) { |
| 1986 | list_add_tail(&BTRFS_I(inode)->delalloc_inodes, |
| 1987 | &root->delalloc_inodes); |
| 1988 | set_bit(BTRFS_INODE_IN_DELALLOC_LIST, |
| 1989 | &BTRFS_I(inode)->runtime_flags); |
| 1990 | root->nr_delalloc_inodes++; |
| 1991 | if (root->nr_delalloc_inodes == 1) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1992 | spin_lock(&fs_info->delalloc_root_lock); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 1993 | BUG_ON(!list_empty(&root->delalloc_root)); |
| 1994 | list_add_tail(&root->delalloc_root, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1995 | &fs_info->delalloc_roots); |
| 1996 | spin_unlock(&fs_info->delalloc_root_lock); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 1997 | } |
| 1998 | } |
| 1999 | spin_unlock(&root->delalloc_lock); |
| 2000 | } |
| 2001 | |
Nikolay Borisov | 2b87733 | 2018-04-27 12:21:51 +0300 | [diff] [blame] | 2002 | |
| 2003 | void __btrfs_del_delalloc_inode(struct btrfs_root *root, |
| 2004 | struct btrfs_inode *inode) |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 2005 | { |
David Sterba | 3ffbd68 | 2018-06-29 10:56:42 +0200 | [diff] [blame] | 2006 | struct btrfs_fs_info *fs_info = root->fs_info; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2007 | |
Nikolay Borisov | 9e3e97f | 2017-02-20 13:51:07 +0200 | [diff] [blame] | 2008 | if (!list_empty(&inode->delalloc_inodes)) { |
| 2009 | list_del_init(&inode->delalloc_inodes); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 2010 | clear_bit(BTRFS_INODE_IN_DELALLOC_LIST, |
Nikolay Borisov | 9e3e97f | 2017-02-20 13:51:07 +0200 | [diff] [blame] | 2011 | &inode->runtime_flags); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 2012 | root->nr_delalloc_inodes--; |
| 2013 | if (!root->nr_delalloc_inodes) { |
Nikolay Borisov | 7c8a0d3 | 2018-04-27 12:21:52 +0300 | [diff] [blame] | 2014 | ASSERT(list_empty(&root->delalloc_inodes)); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2015 | spin_lock(&fs_info->delalloc_root_lock); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 2016 | BUG_ON(list_empty(&root->delalloc_root)); |
| 2017 | list_del_init(&root->delalloc_root); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2018 | spin_unlock(&fs_info->delalloc_root_lock); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 2019 | } |
| 2020 | } |
Nikolay Borisov | 2b87733 | 2018-04-27 12:21:51 +0300 | [diff] [blame] | 2021 | } |
| 2022 | |
| 2023 | static void btrfs_del_delalloc_inode(struct btrfs_root *root, |
| 2024 | struct btrfs_inode *inode) |
| 2025 | { |
| 2026 | spin_lock(&root->delalloc_lock); |
| 2027 | __btrfs_del_delalloc_inode(root, inode); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 2028 | spin_unlock(&root->delalloc_lock); |
| 2029 | } |
| 2030 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2031 | /* |
Nikolay Borisov | e06a1fc | 2018-11-01 14:09:50 +0200 | [diff] [blame] | 2032 | * Properly track delayed allocation bytes in the inode and to maintain the |
| 2033 | * list of inodes that have pending delalloc work to be done. |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2034 | */ |
Nikolay Borisov | e06a1fc | 2018-11-01 14:09:50 +0200 | [diff] [blame] | 2035 | void btrfs_set_delalloc_extent(struct inode *inode, struct extent_state *state, |
| 2036 | unsigned *bits) |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 2037 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2038 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
| 2039 | |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 2040 | if ((*bits & EXTENT_DEFRAG) && !(*bits & EXTENT_DELALLOC)) |
| 2041 | WARN_ON(1); |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 2042 | /* |
| 2043 | * set_bit and clear bit hooks normally require _irqsave/restore |
Sergei Trofimovich | 27160b6 | 2011-05-20 20:20:32 +0000 | [diff] [blame] | 2044 | * but in this case, we are only testing for the DELALLOC |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 2045 | * bit, which is only set or cleared with irqs on |
| 2046 | */ |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 2047 | if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) { |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 2048 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 2049 | u64 len = state->end + 1 - state->start; |
Josef Bacik | 8b62f87 | 2017-10-19 14:15:55 -0400 | [diff] [blame] | 2050 | u32 num_extents = count_max_extents(len); |
Nikolay Borisov | 70ddc55 | 2017-02-20 13:50:35 +0200 | [diff] [blame] | 2051 | bool do_list = !btrfs_is_free_space_inode(BTRFS_I(inode)); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 2052 | |
Josef Bacik | 8b62f87 | 2017-10-19 14:15:55 -0400 | [diff] [blame] | 2053 | spin_lock(&BTRFS_I(inode)->lock); |
| 2054 | btrfs_mod_outstanding_extents(BTRFS_I(inode), num_extents); |
| 2055 | spin_unlock(&BTRFS_I(inode)->lock); |
Josef Bacik | 287a0ab | 2010-03-19 18:07:23 +0000 | [diff] [blame] | 2056 | |
Josef Bacik | 6a3891c | 2015-03-16 17:38:52 -0400 | [diff] [blame] | 2057 | /* For sanity tests */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2058 | if (btrfs_is_testing(fs_info)) |
Josef Bacik | 6a3891c | 2015-03-16 17:38:52 -0400 | [diff] [blame] | 2059 | return; |
| 2060 | |
Nikolay Borisov | 104b4e5 | 2017-06-20 21:01:20 +0300 | [diff] [blame] | 2061 | percpu_counter_add_batch(&fs_info->delalloc_bytes, len, |
| 2062 | fs_info->delalloc_batch); |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 2063 | spin_lock(&BTRFS_I(inode)->lock); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 2064 | BTRFS_I(inode)->delalloc_bytes += len; |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 2065 | if (*bits & EXTENT_DEFRAG) |
| 2066 | BTRFS_I(inode)->defrag_bytes += len; |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 2067 | if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST, |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 2068 | &BTRFS_I(inode)->runtime_flags)) |
| 2069 | btrfs_add_delalloc_inodes(root, inode); |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 2070 | spin_unlock(&BTRFS_I(inode)->lock); |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 2071 | } |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 2072 | |
| 2073 | if (!(state->state & EXTENT_DELALLOC_NEW) && |
| 2074 | (*bits & EXTENT_DELALLOC_NEW)) { |
| 2075 | spin_lock(&BTRFS_I(inode)->lock); |
| 2076 | BTRFS_I(inode)->new_delalloc_bytes += state->end + 1 - |
| 2077 | state->start; |
| 2078 | spin_unlock(&BTRFS_I(inode)->lock); |
| 2079 | } |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 2080 | } |
| 2081 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2082 | /* |
Nikolay Borisov | a36bb5f | 2018-11-01 14:09:51 +0200 | [diff] [blame] | 2083 | * Once a range is no longer delalloc this function ensures that proper |
| 2084 | * accounting happens. |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2085 | */ |
Nikolay Borisov | a36bb5f | 2018-11-01 14:09:51 +0200 | [diff] [blame] | 2086 | void btrfs_clear_delalloc_extent(struct inode *vfs_inode, |
| 2087 | struct extent_state *state, unsigned *bits) |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 2088 | { |
Nikolay Borisov | a36bb5f | 2018-11-01 14:09:51 +0200 | [diff] [blame] | 2089 | struct btrfs_inode *inode = BTRFS_I(vfs_inode); |
| 2090 | struct btrfs_fs_info *fs_info = btrfs_sb(vfs_inode->i_sb); |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 2091 | u64 len = state->end + 1 - state->start; |
David Sterba | 823bb20 | 2017-01-04 11:09:51 +0100 | [diff] [blame] | 2092 | u32 num_extents = count_max_extents(len); |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 2093 | |
Filipe Manana | 4a4b964 | 2017-07-27 19:52:55 +0100 | [diff] [blame] | 2094 | if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG)) { |
| 2095 | spin_lock(&inode->lock); |
Nikolay Borisov | 6fc0ef6 | 2017-02-20 13:51:03 +0200 | [diff] [blame] | 2096 | inode->defrag_bytes -= len; |
Filipe Manana | 4a4b964 | 2017-07-27 19:52:55 +0100 | [diff] [blame] | 2097 | spin_unlock(&inode->lock); |
| 2098 | } |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 2099 | |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 2100 | /* |
| 2101 | * set_bit and clear bit hooks normally require _irqsave/restore |
Sergei Trofimovich | 27160b6 | 2011-05-20 20:20:32 +0000 | [diff] [blame] | 2102 | * but in this case, we are only testing for the DELALLOC |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 2103 | * bit, which is only set or cleared with irqs on |
| 2104 | */ |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 2105 | if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) { |
Nikolay Borisov | 6fc0ef6 | 2017-02-20 13:51:03 +0200 | [diff] [blame] | 2106 | struct btrfs_root *root = inode->root; |
Liu Bo | 83eea1f | 2012-07-10 05:28:39 -0600 | [diff] [blame] | 2107 | bool do_list = !btrfs_is_free_space_inode(inode); |
Chris Mason | bcbfce8 | 2008-04-22 13:26:47 -0400 | [diff] [blame] | 2108 | |
Josef Bacik | 8b62f87 | 2017-10-19 14:15:55 -0400 | [diff] [blame] | 2109 | spin_lock(&inode->lock); |
| 2110 | btrfs_mod_outstanding_extents(inode, -num_extents); |
| 2111 | spin_unlock(&inode->lock); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 2112 | |
Josef Bacik | b6d08f0 | 2013-09-27 14:57:43 -0400 | [diff] [blame] | 2113 | /* |
| 2114 | * We don't reserve metadata space for space cache inodes so we |
Andrea Gelmini | 52042d8 | 2018-11-28 12:05:13 +0100 | [diff] [blame] | 2115 | * don't need to call delalloc_release_metadata if there is an |
Josef Bacik | b6d08f0 | 2013-09-27 14:57:43 -0400 | [diff] [blame] | 2116 | * error. |
| 2117 | */ |
Filipe Manana | a315e68 | 2017-03-06 23:04:20 +0000 | [diff] [blame] | 2118 | if (*bits & EXTENT_CLEAR_META_RESV && |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2119 | root != fs_info->tree_root) |
Qu Wenruo | 43b1859 | 2017-12-12 15:34:32 +0800 | [diff] [blame] | 2120 | btrfs_delalloc_release_metadata(inode, len, false); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 2121 | |
Josef Bacik | 6a3891c | 2015-03-16 17:38:52 -0400 | [diff] [blame] | 2122 | /* For sanity tests. */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2123 | if (btrfs_is_testing(fs_info)) |
Josef Bacik | 6a3891c | 2015-03-16 17:38:52 -0400 | [diff] [blame] | 2124 | return; |
| 2125 | |
Filipe Manana | a315e68 | 2017-03-06 23:04:20 +0000 | [diff] [blame] | 2126 | if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID && |
| 2127 | do_list && !(state->state & EXTENT_NORESERVE) && |
| 2128 | (*bits & EXTENT_CLEAR_DATA_RESV)) |
Nikolay Borisov | 9db5d51 | 2020-06-03 08:55:38 +0300 | [diff] [blame] | 2129 | btrfs_free_reserved_data_space_noquota(fs_info, len); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 2130 | |
Nikolay Borisov | 104b4e5 | 2017-06-20 21:01:20 +0300 | [diff] [blame] | 2131 | percpu_counter_add_batch(&fs_info->delalloc_bytes, -len, |
| 2132 | fs_info->delalloc_batch); |
Nikolay Borisov | 6fc0ef6 | 2017-02-20 13:51:03 +0200 | [diff] [blame] | 2133 | spin_lock(&inode->lock); |
| 2134 | inode->delalloc_bytes -= len; |
| 2135 | if (do_list && inode->delalloc_bytes == 0 && |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 2136 | test_bit(BTRFS_INODE_IN_DELALLOC_LIST, |
Nikolay Borisov | 9e3e97f | 2017-02-20 13:51:07 +0200 | [diff] [blame] | 2137 | &inode->runtime_flags)) |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 2138 | btrfs_del_delalloc_inode(root, inode); |
Nikolay Borisov | 6fc0ef6 | 2017-02-20 13:51:03 +0200 | [diff] [blame] | 2139 | spin_unlock(&inode->lock); |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 2140 | } |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 2141 | |
| 2142 | if ((state->state & EXTENT_DELALLOC_NEW) && |
| 2143 | (*bits & EXTENT_DELALLOC_NEW)) { |
| 2144 | spin_lock(&inode->lock); |
| 2145 | ASSERT(inode->new_delalloc_bytes >= len); |
| 2146 | inode->new_delalloc_bytes -= len; |
| 2147 | spin_unlock(&inode->lock); |
| 2148 | } |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 2149 | } |
| 2150 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2151 | /* |
Nikolay Borisov | da12fe5 | 2018-11-27 20:57:58 +0200 | [diff] [blame] | 2152 | * btrfs_bio_fits_in_stripe - Checks whether the size of the given bio will fit |
| 2153 | * in a chunk's stripe. This function ensures that bios do not span a |
| 2154 | * stripe/chunk |
Liu Bo | 6f034ec | 2016-06-22 18:31:49 -0700 | [diff] [blame] | 2155 | * |
Nikolay Borisov | da12fe5 | 2018-11-27 20:57:58 +0200 | [diff] [blame] | 2156 | * @page - The page we are about to add to the bio |
| 2157 | * @size - size we want to add to the bio |
| 2158 | * @bio - bio we want to ensure is smaller than a stripe |
| 2159 | * @bio_flags - flags of the bio |
| 2160 | * |
| 2161 | * return 1 if page cannot be added to the bio |
| 2162 | * return 0 if page can be added to the bio |
Liu Bo | 6f034ec | 2016-06-22 18:31:49 -0700 | [diff] [blame] | 2163 | * return error otherwise |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2164 | */ |
Nikolay Borisov | da12fe5 | 2018-11-27 20:57:58 +0200 | [diff] [blame] | 2165 | int btrfs_bio_fits_in_stripe(struct page *page, size_t size, struct bio *bio, |
| 2166 | unsigned long bio_flags) |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 2167 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2168 | struct inode *inode = page->mapping->host; |
| 2169 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 2170 | u64 logical = (u64)bio->bi_iter.bi_sector << 9; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 2171 | u64 length = 0; |
| 2172 | u64 map_length; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 2173 | int ret; |
Nikolay Borisov | 89b798a | 2019-06-03 12:05:05 +0300 | [diff] [blame] | 2174 | struct btrfs_io_geometry geom; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 2175 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 2176 | if (bio_flags & EXTENT_BIO_COMPRESSED) |
| 2177 | return 0; |
| 2178 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 2179 | length = bio->bi_iter.bi_size; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 2180 | map_length = length; |
Nikolay Borisov | 89b798a | 2019-06-03 12:05:05 +0300 | [diff] [blame] | 2181 | ret = btrfs_get_io_geometry(fs_info, btrfs_op(bio), logical, map_length, |
| 2182 | &geom); |
Liu Bo | 6f034ec | 2016-06-22 18:31:49 -0700 | [diff] [blame] | 2183 | if (ret < 0) |
| 2184 | return ret; |
Nikolay Borisov | 89b798a | 2019-06-03 12:05:05 +0300 | [diff] [blame] | 2185 | |
| 2186 | if (geom.len < length + size) |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 2187 | return 1; |
Jeff Mahoney | 3444a97 | 2011-10-03 23:23:13 -0400 | [diff] [blame] | 2188 | return 0; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 2189 | } |
| 2190 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2191 | /* |
| 2192 | * in order to insert checksums into the metadata in large chunks, |
| 2193 | * we wait until bio submission time. All the pages in the bio are |
| 2194 | * checksummed and sums are attached onto the ordered extent record. |
| 2195 | * |
| 2196 | * At IO completion time the cums attached on the ordered extent record |
| 2197 | * are inserted into the btree |
| 2198 | */ |
Qu Wenruo | 8896a08 | 2020-10-21 14:24:53 +0800 | [diff] [blame] | 2199 | static blk_status_t btrfs_submit_bio_start(struct inode *inode, struct bio *bio, |
| 2200 | u64 bio_offset) |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 2201 | { |
Johannes Thumshirn | c965d64 | 2020-07-28 20:25:41 +0900 | [diff] [blame] | 2202 | return btrfs_csum_one_bio(BTRFS_I(inode), bio, 0, 0); |
Chris Mason | 4a69a41 | 2008-11-06 22:03:00 -0500 | [diff] [blame] | 2203 | } |
Chris Mason | e015640 | 2008-04-16 11:15:20 -0400 | [diff] [blame] | 2204 | |
Chris Mason | 4a69a41 | 2008-11-06 22:03:00 -0500 | [diff] [blame] | 2205 | /* |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 2206 | * 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] | 2207 | * on write, or reading the csums from the tree before a read. |
| 2208 | * |
| 2209 | * Rules about async/sync submit, |
| 2210 | * a) read: sync submit |
| 2211 | * |
| 2212 | * b) write without checksum: sync submit |
| 2213 | * |
| 2214 | * c) write with checksum: |
| 2215 | * c-1) if bio is issued by fsync: sync submit |
| 2216 | * (sync_writers != 0) |
| 2217 | * |
| 2218 | * c-2) if root is reloc root: sync submit |
| 2219 | * (only in case of buffered IO) |
| 2220 | * |
| 2221 | * c-3) otherwise: async submit |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2222 | */ |
Nikolay Borisov | 908930f | 2020-09-18 16:34:37 +0300 | [diff] [blame] | 2223 | blk_status_t btrfs_submit_data_bio(struct inode *inode, struct bio *bio, |
| 2224 | int mirror_num, unsigned long bio_flags) |
Nikolay Borisov | 50489a5 | 2019-04-10 19:46:04 +0300 | [diff] [blame] | 2225 | |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 2226 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2227 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 2228 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chandan Rajendra | 0d51e28 | 2015-07-27 15:26:43 +0530 | [diff] [blame] | 2229 | enum btrfs_wq_endio_type metadata = BTRFS_WQ_ENDIO_DATA; |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 2230 | blk_status_t ret = 0; |
Chris Mason | 19b9bdb | 2008-10-30 14:23:13 -0400 | [diff] [blame] | 2231 | int skip_sum; |
Josef Bacik | b812ce2 | 2012-11-16 13:56:32 -0500 | [diff] [blame] | 2232 | int async = !atomic_read(&BTRFS_I(inode)->sync_writers); |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 2233 | |
Josef Bacik | 42437a6 | 2020-10-16 11:29:18 -0400 | [diff] [blame] | 2234 | skip_sum = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM) || |
| 2235 | !fs_info->csum_root; |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 2236 | |
Nikolay Borisov | 70ddc55 | 2017-02-20 13:50:35 +0200 | [diff] [blame] | 2237 | if (btrfs_is_free_space_inode(BTRFS_I(inode))) |
Chandan Rajendra | 0d51e28 | 2015-07-27 15:26:43 +0530 | [diff] [blame] | 2238 | metadata = BTRFS_WQ_ENDIO_FREE_SPACE; |
Jeff Mahoney | 0417341 | 2011-10-03 23:23:12 -0400 | [diff] [blame] | 2239 | |
Mike Christie | 37226b2 | 2016-06-05 14:31:52 -0500 | [diff] [blame] | 2240 | if (bio_op(bio) != REQ_OP_WRITE) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2241 | ret = btrfs_bio_wq_end_io(fs_info, bio, metadata); |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2242 | if (ret) |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 2243 | goto out; |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2244 | |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 2245 | if (bio_flags & EXTENT_BIO_COMPRESSED) { |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 2246 | ret = btrfs_submit_compressed_read(inode, bio, |
| 2247 | mirror_num, |
| 2248 | bio_flags); |
| 2249 | goto out; |
Josef Bacik | 334c16d | 2020-10-16 11:29:14 -0400 | [diff] [blame] | 2250 | } else { |
| 2251 | /* |
| 2252 | * Lookup bio sums does extra checks around whether we |
| 2253 | * need to csum or not, which is why we ignore skip_sum |
| 2254 | * here. |
| 2255 | */ |
Omar Sandoval | db72e47 | 2019-12-10 10:37:35 -0800 | [diff] [blame] | 2256 | ret = btrfs_lookup_bio_sums(inode, bio, (u64)-1, NULL); |
Tsutomu Itoh | c2db107 | 2011-03-01 06:48:31 +0000 | [diff] [blame] | 2257 | if (ret) |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 2258 | goto out; |
Tsutomu Itoh | c2db107 | 2011-03-01 06:48:31 +0000 | [diff] [blame] | 2259 | } |
Chris Mason | 4d1b5fb | 2008-08-20 09:44:52 -0400 | [diff] [blame] | 2260 | goto mapit; |
Josef Bacik | b812ce2 | 2012-11-16 13:56:32 -0500 | [diff] [blame] | 2261 | } else if (async && !skip_sum) { |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 2262 | /* csum items have already been cloned */ |
| 2263 | if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID) |
| 2264 | goto mapit; |
Chris Mason | 19b9bdb | 2008-10-30 14:23:13 -0400 | [diff] [blame] | 2265 | /* we're doing a write, do the async checksumming */ |
Qu Wenruo | 8896a08 | 2020-10-21 14:24:53 +0800 | [diff] [blame] | 2266 | ret = btrfs_wq_submit_bio(inode, bio, mirror_num, bio_flags, |
| 2267 | 0, btrfs_submit_bio_start); |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 2268 | goto out; |
Josef Bacik | b812ce2 | 2012-11-16 13:56:32 -0500 | [diff] [blame] | 2269 | } else if (!skip_sum) { |
Nikolay Borisov | bd242a0 | 2020-06-03 08:55:07 +0300 | [diff] [blame] | 2270 | ret = btrfs_csum_one_bio(BTRFS_I(inode), bio, 0, 0); |
Josef Bacik | b812ce2 | 2012-11-16 13:56:32 -0500 | [diff] [blame] | 2271 | if (ret) |
| 2272 | goto out; |
Chris Mason | 19b9bdb | 2008-10-30 14:23:13 -0400 | [diff] [blame] | 2273 | } |
| 2274 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2275 | mapit: |
Chris Mason | 08635ba | 2019-07-10 12:28:14 -0700 | [diff] [blame] | 2276 | ret = btrfs_map_bio(fs_info, bio, mirror_num); |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 2277 | |
| 2278 | out: |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 2279 | if (ret) { |
| 2280 | bio->bi_status = ret; |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 2281 | bio_endio(bio); |
| 2282 | } |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 2283 | return ret; |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 2284 | } |
Chris Mason | 6885f30 | 2008-02-20 16:11:05 -0500 | [diff] [blame] | 2285 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2286 | /* |
| 2287 | * given a list of ordered sums record them in the inode. This happens |
| 2288 | * at IO completion time based on sums calculated at bio submission time. |
| 2289 | */ |
Nikolay Borisov | 510f85e | 2020-09-18 12:15:52 +0300 | [diff] [blame] | 2290 | static int add_pending_csums(struct btrfs_trans_handle *trans, |
| 2291 | struct list_head *list) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2292 | { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2293 | struct btrfs_ordered_sum *sum; |
Nikolay Borisov | ac01f26 | 2018-01-08 10:59:43 +0200 | [diff] [blame] | 2294 | int ret; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2295 | |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 2296 | list_for_each_entry(sum, list, list) { |
David Sterba | 7c2871a | 2017-11-08 01:07:43 +0100 | [diff] [blame] | 2297 | trans->adding_csums = true; |
Nikolay Borisov | 510f85e | 2020-09-18 12:15:52 +0300 | [diff] [blame] | 2298 | ret = btrfs_csum_file_blocks(trans, trans->fs_info->csum_root, sum); |
David Sterba | 7c2871a | 2017-11-08 01:07:43 +0100 | [diff] [blame] | 2299 | trans->adding_csums = false; |
Nikolay Borisov | ac01f26 | 2018-01-08 10:59:43 +0200 | [diff] [blame] | 2300 | if (ret) |
| 2301 | return ret; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2302 | } |
| 2303 | return 0; |
| 2304 | } |
| 2305 | |
Filipe Manana | c334730 | 2020-11-04 11:07:31 +0000 | [diff] [blame] | 2306 | static int btrfs_find_new_delalloc_bytes(struct btrfs_inode *inode, |
| 2307 | const u64 start, |
| 2308 | const u64 len, |
| 2309 | struct extent_state **cached_state) |
| 2310 | { |
| 2311 | u64 search_start = start; |
| 2312 | const u64 end = start + len - 1; |
| 2313 | |
| 2314 | while (search_start < end) { |
| 2315 | const u64 search_len = end - search_start + 1; |
| 2316 | struct extent_map *em; |
| 2317 | u64 em_len; |
| 2318 | int ret = 0; |
| 2319 | |
| 2320 | em = btrfs_get_extent(inode, NULL, 0, search_start, search_len); |
| 2321 | if (IS_ERR(em)) |
| 2322 | return PTR_ERR(em); |
| 2323 | |
| 2324 | if (em->block_start != EXTENT_MAP_HOLE) |
| 2325 | goto next; |
| 2326 | |
| 2327 | em_len = em->len; |
| 2328 | if (em->start < search_start) |
| 2329 | em_len -= search_start - em->start; |
| 2330 | if (em_len > search_len) |
| 2331 | em_len = search_len; |
| 2332 | |
| 2333 | ret = set_extent_bit(&inode->io_tree, search_start, |
| 2334 | search_start + em_len - 1, |
| 2335 | EXTENT_DELALLOC_NEW, |
| 2336 | NULL, cached_state, GFP_NOFS); |
| 2337 | next: |
| 2338 | search_start = extent_map_end(em); |
| 2339 | free_extent_map(em); |
| 2340 | if (ret) |
| 2341 | return ret; |
| 2342 | } |
| 2343 | return 0; |
| 2344 | } |
| 2345 | |
Nikolay Borisov | c2566f2 | 2020-06-03 08:55:35 +0300 | [diff] [blame] | 2346 | 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] | 2347 | unsigned int extra_bits, |
Nikolay Borisov | 330a582 | 2019-07-17 16:18:17 +0300 | [diff] [blame] | 2348 | struct extent_state **cached_state) |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 2349 | { |
Johannes Thumshirn | fdb1e12 | 2018-12-05 15:23:04 +0100 | [diff] [blame] | 2350 | WARN_ON(PAGE_ALIGNED(end)); |
Filipe Manana | c334730 | 2020-11-04 11:07:31 +0000 | [diff] [blame] | 2351 | |
| 2352 | if (start >= i_size_read(&inode->vfs_inode) && |
| 2353 | !(inode->flags & BTRFS_INODE_PREALLOC)) { |
| 2354 | /* |
| 2355 | * There can't be any extents following eof in this case so just |
| 2356 | * set the delalloc new bit for the range directly. |
| 2357 | */ |
| 2358 | extra_bits |= EXTENT_DELALLOC_NEW; |
| 2359 | } else { |
| 2360 | int ret; |
| 2361 | |
| 2362 | ret = btrfs_find_new_delalloc_bytes(inode, start, |
| 2363 | end + 1 - start, |
| 2364 | cached_state); |
| 2365 | if (ret) |
| 2366 | return ret; |
| 2367 | } |
| 2368 | |
Nikolay Borisov | c2566f2 | 2020-06-03 08:55:35 +0300 | [diff] [blame] | 2369 | return set_extent_delalloc(&inode->io_tree, start, end, extra_bits, |
| 2370 | cached_state); |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 2371 | } |
| 2372 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2373 | /* see btrfs_writepage_start_hook for details on why this is required */ |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2374 | struct btrfs_writepage_fixup { |
| 2375 | struct page *page; |
Josef Bacik | f4b1363 | 2020-01-21 14:34:52 -0500 | [diff] [blame] | 2376 | struct inode *inode; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2377 | struct btrfs_work work; |
| 2378 | }; |
| 2379 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 2380 | static void btrfs_writepage_fixup_worker(struct btrfs_work *work) |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2381 | { |
| 2382 | struct btrfs_writepage_fixup *fixup; |
| 2383 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 2384 | struct extent_state *cached_state = NULL; |
Qu Wenruo | 364ecf3 | 2017-02-27 15:10:38 +0800 | [diff] [blame] | 2385 | struct extent_changeset *data_reserved = NULL; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2386 | struct page *page; |
Nikolay Borisov | 65d87f7 | 2020-06-05 10:51:51 +0300 | [diff] [blame] | 2387 | struct btrfs_inode *inode; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2388 | u64 page_start; |
| 2389 | u64 page_end; |
Chris Mason | 25f3c50 | 2020-01-21 11:51:42 -0500 | [diff] [blame] | 2390 | int ret = 0; |
Josef Bacik | f4b1363 | 2020-01-21 14:34:52 -0500 | [diff] [blame] | 2391 | bool free_delalloc_space = true; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2392 | |
| 2393 | fixup = container_of(work, struct btrfs_writepage_fixup, work); |
| 2394 | page = fixup->page; |
Nikolay Borisov | 65d87f7 | 2020-06-05 10:51:51 +0300 | [diff] [blame] | 2395 | inode = BTRFS_I(fixup->inode); |
Josef Bacik | f4b1363 | 2020-01-21 14:34:52 -0500 | [diff] [blame] | 2396 | page_start = page_offset(page); |
| 2397 | page_end = page_offset(page) + PAGE_SIZE - 1; |
| 2398 | |
| 2399 | /* |
| 2400 | * This is similar to page_mkwrite, we need to reserve the space before |
| 2401 | * we take the page lock. |
| 2402 | */ |
Nikolay Borisov | 65d87f7 | 2020-06-05 10:51:51 +0300 | [diff] [blame] | 2403 | ret = btrfs_delalloc_reserve_space(inode, &data_reserved, page_start, |
| 2404 | PAGE_SIZE); |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 2405 | again: |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2406 | lock_page(page); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2407 | |
Chris Mason | 25f3c50 | 2020-01-21 11:51:42 -0500 | [diff] [blame] | 2408 | /* |
| 2409 | * Before we queued this fixup, we took a reference on the page. |
| 2410 | * page->mapping may go NULL, but it shouldn't be moved to a different |
| 2411 | * address space. |
| 2412 | */ |
Josef Bacik | f4b1363 | 2020-01-21 14:34:52 -0500 | [diff] [blame] | 2413 | if (!page->mapping || !PageDirty(page) || !PageChecked(page)) { |
| 2414 | /* |
| 2415 | * Unfortunately this is a little tricky, either |
| 2416 | * |
| 2417 | * 1) We got here and our page had already been dealt with and |
| 2418 | * we reserved our space, thus ret == 0, so we need to just |
| 2419 | * drop our space reservation and bail. This can happen the |
| 2420 | * first time we come into the fixup worker, or could happen |
| 2421 | * while waiting for the ordered extent. |
| 2422 | * 2) Our page was already dealt with, but we happened to get an |
| 2423 | * ENOSPC above from the btrfs_delalloc_reserve_space. In |
| 2424 | * this case we obviously don't have anything to release, but |
| 2425 | * because the page was already dealt with we don't want to |
| 2426 | * mark the page with an error, so make sure we're resetting |
| 2427 | * ret to 0. This is why we have this check _before_ the ret |
| 2428 | * check, because we do not want to have a surprise ENOSPC |
| 2429 | * when the page was already properly dealt with. |
| 2430 | */ |
| 2431 | if (!ret) { |
Nikolay Borisov | 65d87f7 | 2020-06-05 10:51:51 +0300 | [diff] [blame] | 2432 | btrfs_delalloc_release_extents(inode, PAGE_SIZE); |
| 2433 | btrfs_delalloc_release_space(inode, data_reserved, |
Josef Bacik | f4b1363 | 2020-01-21 14:34:52 -0500 | [diff] [blame] | 2434 | page_start, PAGE_SIZE, |
| 2435 | true); |
| 2436 | } |
| 2437 | ret = 0; |
Chris Mason | 25f3c50 | 2020-01-21 11:51:42 -0500 | [diff] [blame] | 2438 | goto out_page; |
Josef Bacik | f4b1363 | 2020-01-21 14:34:52 -0500 | [diff] [blame] | 2439 | } |
Chris Mason | 25f3c50 | 2020-01-21 11:51:42 -0500 | [diff] [blame] | 2440 | |
| 2441 | /* |
Josef Bacik | f4b1363 | 2020-01-21 14:34:52 -0500 | [diff] [blame] | 2442 | * We can't mess with the page state unless it is locked, so now that |
| 2443 | * it is locked bail if we failed to make our space reservation. |
Chris Mason | 25f3c50 | 2020-01-21 11:51:42 -0500 | [diff] [blame] | 2444 | */ |
Josef Bacik | f4b1363 | 2020-01-21 14:34:52 -0500 | [diff] [blame] | 2445 | if (ret) |
| 2446 | goto out_page; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2447 | |
Nikolay Borisov | 65d87f7 | 2020-06-05 10:51:51 +0300 | [diff] [blame] | 2448 | lock_extent_bits(&inode->io_tree, page_start, page_end, &cached_state); |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 2449 | |
| 2450 | /* already ordered? We're done */ |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 2451 | if (PagePrivate2(page)) |
Josef Bacik | f4b1363 | 2020-01-21 14:34:52 -0500 | [diff] [blame] | 2452 | goto out_reserved; |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 2453 | |
Nikolay Borisov | 65d87f7 | 2020-06-05 10:51:51 +0300 | [diff] [blame] | 2454 | ordered = btrfs_lookup_ordered_range(inode, page_start, PAGE_SIZE); |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 2455 | if (ordered) { |
Nikolay Borisov | 65d87f7 | 2020-06-05 10:51:51 +0300 | [diff] [blame] | 2456 | unlock_extent_cached(&inode->io_tree, page_start, page_end, |
| 2457 | &cached_state); |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 2458 | unlock_page(page); |
Nikolay Borisov | c0a4360 | 2020-09-18 12:15:53 +0300 | [diff] [blame] | 2459 | btrfs_start_ordered_extent(ordered, 1); |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 2460 | btrfs_put_ordered_extent(ordered); |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 2461 | goto again; |
| 2462 | } |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2463 | |
Nikolay Borisov | 65d87f7 | 2020-06-05 10:51:51 +0300 | [diff] [blame] | 2464 | ret = btrfs_set_extent_delalloc(inode, page_start, page_end, 0, |
Nikolay Borisov | 330a582 | 2019-07-17 16:18:17 +0300 | [diff] [blame] | 2465 | &cached_state); |
Chris Mason | 25f3c50 | 2020-01-21 11:51:42 -0500 | [diff] [blame] | 2466 | if (ret) |
Filipe Manana | 5368700 | 2019-10-09 17:43:59 +0100 | [diff] [blame] | 2467 | goto out_reserved; |
Nikolay Borisov | f3038ee | 2017-12-05 09:29:19 +0200 | [diff] [blame] | 2468 | |
Chris Mason | 25f3c50 | 2020-01-21 11:51:42 -0500 | [diff] [blame] | 2469 | /* |
| 2470 | * Everything went as planned, we're now the owner of a dirty page with |
| 2471 | * delayed allocation bits set and space reserved for our COW |
| 2472 | * destination. |
| 2473 | * |
| 2474 | * The page was dirty when we started, nothing should have cleaned it. |
| 2475 | */ |
| 2476 | BUG_ON(!PageDirty(page)); |
Josef Bacik | f4b1363 | 2020-01-21 14:34:52 -0500 | [diff] [blame] | 2477 | free_delalloc_space = false; |
Filipe Manana | 5368700 | 2019-10-09 17:43:59 +0100 | [diff] [blame] | 2478 | out_reserved: |
Nikolay Borisov | 65d87f7 | 2020-06-05 10:51:51 +0300 | [diff] [blame] | 2479 | btrfs_delalloc_release_extents(inode, PAGE_SIZE); |
Josef Bacik | f4b1363 | 2020-01-21 14:34:52 -0500 | [diff] [blame] | 2480 | if (free_delalloc_space) |
Nikolay Borisov | 65d87f7 | 2020-06-05 10:51:51 +0300 | [diff] [blame] | 2481 | btrfs_delalloc_release_space(inode, data_reserved, page_start, |
| 2482 | PAGE_SIZE, true); |
| 2483 | unlock_extent_cached(&inode->io_tree, page_start, page_end, |
David Sterba | e43bbe5 | 2017-12-12 21:43:52 +0100 | [diff] [blame] | 2484 | &cached_state); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2485 | out_page: |
Chris Mason | 25f3c50 | 2020-01-21 11:51:42 -0500 | [diff] [blame] | 2486 | if (ret) { |
| 2487 | /* |
| 2488 | * We hit ENOSPC or other errors. Update the mapping and page |
| 2489 | * to reflect the errors and clean the page. |
| 2490 | */ |
| 2491 | mapping_set_error(page->mapping, ret); |
| 2492 | end_extent_writepage(page, ret, page_start, page_end); |
| 2493 | clear_page_dirty_for_io(page); |
| 2494 | SetPageError(page); |
| 2495 | } |
| 2496 | ClearPageChecked(page); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2497 | unlock_page(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2498 | put_page(page); |
Miao Xie | b897abe | 2011-01-26 16:19:22 +0800 | [diff] [blame] | 2499 | kfree(fixup); |
Qu Wenruo | 364ecf3 | 2017-02-27 15:10:38 +0800 | [diff] [blame] | 2500 | extent_changeset_free(data_reserved); |
Josef Bacik | f4b1363 | 2020-01-21 14:34:52 -0500 | [diff] [blame] | 2501 | /* |
| 2502 | * As a precaution, do a delayed iput in case it would be the last iput |
| 2503 | * that could need flushing space. Recursing back to fixup worker would |
| 2504 | * deadlock. |
| 2505 | */ |
Nikolay Borisov | 65d87f7 | 2020-06-05 10:51:51 +0300 | [diff] [blame] | 2506 | btrfs_add_delayed_iput(&inode->vfs_inode); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2507 | } |
| 2508 | |
| 2509 | /* |
| 2510 | * There are a few paths in the higher layers of the kernel that directly |
| 2511 | * set the page dirty bit without asking the filesystem if it is a |
| 2512 | * good idea. This causes problems because we want to make sure COW |
| 2513 | * properly happens and the data=ordered rules are followed. |
| 2514 | * |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2515 | * 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] | 2516 | * hasn't been properly setup for IO. We kick off an async process |
| 2517 | * to fix it up. The async helper will wait for ordered extents, set |
| 2518 | * the delalloc bit and make it safe to write the page. |
| 2519 | */ |
Nikolay Borisov | d75855b | 2018-11-01 14:09:47 +0200 | [diff] [blame] | 2520 | int btrfs_writepage_cow_fixup(struct page *page, u64 start, u64 end) |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2521 | { |
| 2522 | struct inode *inode = page->mapping->host; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2523 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2524 | struct btrfs_writepage_fixup *fixup; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2525 | |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 2526 | /* this page is properly in the ordered list */ |
| 2527 | if (TestClearPagePrivate2(page)) |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2528 | return 0; |
| 2529 | |
Chris Mason | 25f3c50 | 2020-01-21 11:51:42 -0500 | [diff] [blame] | 2530 | /* |
| 2531 | * PageChecked is set below when we create a fixup worker for this page, |
| 2532 | * don't try to create another one if we're already PageChecked() |
| 2533 | * |
| 2534 | * The extent_io writepage code will redirty the page if we send back |
| 2535 | * EAGAIN. |
| 2536 | */ |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2537 | if (PageChecked(page)) |
| 2538 | return -EAGAIN; |
| 2539 | |
| 2540 | fixup = kzalloc(sizeof(*fixup), GFP_NOFS); |
| 2541 | if (!fixup) |
| 2542 | return -EAGAIN; |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 2543 | |
Josef Bacik | f4b1363 | 2020-01-21 14:34:52 -0500 | [diff] [blame] | 2544 | /* |
| 2545 | * We are already holding a reference to this inode from |
| 2546 | * write_cache_pages. We need to hold it because the space reservation |
| 2547 | * takes place outside of the page lock, and we can't trust |
| 2548 | * page->mapping outside of the page lock. |
| 2549 | */ |
| 2550 | ihold(inode); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2551 | SetPageChecked(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2552 | get_page(page); |
Omar Sandoval | a0cac0e | 2019-09-16 11:30:57 -0700 | [diff] [blame] | 2553 | btrfs_init_work(&fixup->work, btrfs_writepage_fixup_worker, NULL, NULL); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2554 | fixup->page = page; |
Josef Bacik | f4b1363 | 2020-01-21 14:34:52 -0500 | [diff] [blame] | 2555 | fixup->inode = inode; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2556 | btrfs_queue_work(fs_info->fixup_workers, &fixup->work); |
Chris Mason | 25f3c50 | 2020-01-21 11:51:42 -0500 | [diff] [blame] | 2557 | |
| 2558 | return -EAGAIN; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2559 | } |
| 2560 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2561 | static int insert_reserved_file_extent(struct btrfs_trans_handle *trans, |
Nikolay Borisov | c553f94 | 2020-06-03 08:55:19 +0300 | [diff] [blame] | 2562 | struct btrfs_inode *inode, u64 file_pos, |
Qu Wenruo | 9729f10 | 2020-06-10 09:04:41 +0800 | [diff] [blame] | 2563 | struct btrfs_file_extent_item *stack_fi, |
| 2564 | u64 qgroup_reserved) |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2565 | { |
Nikolay Borisov | c553f94 | 2020-06-03 08:55:19 +0300 | [diff] [blame] | 2566 | struct btrfs_root *root = inode->root; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2567 | struct btrfs_path *path; |
| 2568 | struct extent_buffer *leaf; |
| 2569 | struct btrfs_key ins; |
Qu Wenruo | 203f44c5 | 2020-06-10 09:04:40 +0800 | [diff] [blame] | 2570 | u64 disk_num_bytes = btrfs_stack_file_extent_disk_num_bytes(stack_fi); |
| 2571 | u64 disk_bytenr = btrfs_stack_file_extent_disk_bytenr(stack_fi); |
| 2572 | u64 num_bytes = btrfs_stack_file_extent_num_bytes(stack_fi); |
| 2573 | u64 ram_bytes = btrfs_stack_file_extent_ram_bytes(stack_fi); |
Filipe Manana | 5893dfb | 2020-11-04 11:07:32 +0000 | [diff] [blame^] | 2574 | struct btrfs_drop_extents_args drop_args = { 0 }; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2575 | int ret; |
| 2576 | |
| 2577 | path = btrfs_alloc_path(); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 2578 | if (!path) |
| 2579 | return -ENOMEM; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2580 | |
Chris Mason | a1ed835 | 2009-09-11 12:27:37 -0400 | [diff] [blame] | 2581 | /* |
| 2582 | * we may be replacing one extent in the tree with another. |
| 2583 | * The new extent is pinned in the extent map, and we don't want |
| 2584 | * to drop it from the cache until it is completely in the btree. |
| 2585 | * |
| 2586 | * So, tell btrfs_drop_extents to leave this extent in the cache. |
| 2587 | * the caller is expected to unpin it and allow it to be merged |
| 2588 | * with the others. |
| 2589 | */ |
Filipe Manana | 5893dfb | 2020-11-04 11:07:32 +0000 | [diff] [blame^] | 2590 | drop_args.path = path; |
| 2591 | drop_args.start = file_pos; |
| 2592 | drop_args.end = file_pos + num_bytes; |
| 2593 | drop_args.replace_extent = true; |
| 2594 | drop_args.extent_item_size = sizeof(*stack_fi); |
| 2595 | ret = btrfs_drop_extents(trans, root, inode, &drop_args); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2596 | if (ret) |
| 2597 | goto out; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2598 | |
Filipe Manana | 5893dfb | 2020-11-04 11:07:32 +0000 | [diff] [blame^] | 2599 | if (!drop_args.extent_inserted) { |
Nikolay Borisov | c553f94 | 2020-06-03 08:55:19 +0300 | [diff] [blame] | 2600 | ins.objectid = btrfs_ino(inode); |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 2601 | ins.offset = file_pos; |
| 2602 | ins.type = BTRFS_EXTENT_DATA_KEY; |
| 2603 | |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 2604 | ret = btrfs_insert_empty_item(trans, root, path, &ins, |
Qu Wenruo | 203f44c5 | 2020-06-10 09:04:40 +0800 | [diff] [blame] | 2605 | sizeof(*stack_fi)); |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 2606 | if (ret) |
| 2607 | goto out; |
| 2608 | } |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2609 | leaf = path->nodes[0]; |
Qu Wenruo | 203f44c5 | 2020-06-10 09:04:40 +0800 | [diff] [blame] | 2610 | btrfs_set_stack_file_extent_generation(stack_fi, trans->transid); |
| 2611 | write_extent_buffer(leaf, stack_fi, |
| 2612 | btrfs_item_ptr_offset(leaf, path->slots[0]), |
| 2613 | sizeof(struct btrfs_file_extent_item)); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 2614 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2615 | btrfs_mark_buffer_dirty(leaf); |
Josef Bacik | ce19533 | 2012-09-25 15:26:16 -0400 | [diff] [blame] | 2616 | btrfs_release_path(path); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2617 | |
Nikolay Borisov | c553f94 | 2020-06-03 08:55:19 +0300 | [diff] [blame] | 2618 | inode_add_bytes(&inode->vfs_inode, num_bytes); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2619 | |
| 2620 | ins.objectid = disk_bytenr; |
| 2621 | ins.offset = disk_num_bytes; |
| 2622 | ins.type = BTRFS_EXTENT_ITEM_KEY; |
Qu Wenruo | a12b877 | 2017-02-27 15:10:37 +0800 | [diff] [blame] | 2623 | |
Nikolay Borisov | c553f94 | 2020-06-03 08:55:19 +0300 | [diff] [blame] | 2624 | ret = btrfs_inode_set_file_extent_range(inode, file_pos, ram_bytes); |
Josef Bacik | 9ddc959 | 2020-01-17 09:02:22 -0500 | [diff] [blame] | 2625 | if (ret) |
| 2626 | goto out; |
| 2627 | |
Nikolay Borisov | c553f94 | 2020-06-03 08:55:19 +0300 | [diff] [blame] | 2628 | ret = btrfs_alloc_reserved_file_extent(trans, root, btrfs_ino(inode), |
Qu Wenruo | 9729f10 | 2020-06-10 09:04:41 +0800 | [diff] [blame] | 2629 | file_pos, qgroup_reserved, &ins); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2630 | out: |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2631 | btrfs_free_path(path); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 2632 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2633 | return ret; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2634 | } |
| 2635 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2636 | static void btrfs_release_delalloc_bytes(struct btrfs_fs_info *fs_info, |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 2637 | u64 start, u64 len) |
| 2638 | { |
David Sterba | 32da5386 | 2019-10-29 19:20:18 +0100 | [diff] [blame] | 2639 | struct btrfs_block_group *cache; |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 2640 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2641 | cache = btrfs_lookup_block_group(fs_info, start); |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 2642 | ASSERT(cache); |
| 2643 | |
| 2644 | spin_lock(&cache->lock); |
| 2645 | cache->delalloc_bytes -= len; |
| 2646 | spin_unlock(&cache->lock); |
| 2647 | |
| 2648 | btrfs_put_block_group(cache); |
| 2649 | } |
| 2650 | |
Qu Wenruo | 203f44c5 | 2020-06-10 09:04:40 +0800 | [diff] [blame] | 2651 | static int insert_ordered_extent_file_extent(struct btrfs_trans_handle *trans, |
Qu Wenruo | 203f44c5 | 2020-06-10 09:04:40 +0800 | [diff] [blame] | 2652 | struct btrfs_ordered_extent *oe) |
| 2653 | { |
| 2654 | struct btrfs_file_extent_item stack_fi; |
| 2655 | u64 logical_len; |
| 2656 | |
| 2657 | memset(&stack_fi, 0, sizeof(stack_fi)); |
| 2658 | btrfs_set_stack_file_extent_type(&stack_fi, BTRFS_FILE_EXTENT_REG); |
| 2659 | btrfs_set_stack_file_extent_disk_bytenr(&stack_fi, oe->disk_bytenr); |
| 2660 | btrfs_set_stack_file_extent_disk_num_bytes(&stack_fi, |
| 2661 | oe->disk_num_bytes); |
| 2662 | if (test_bit(BTRFS_ORDERED_TRUNCATED, &oe->flags)) |
| 2663 | logical_len = oe->truncated_len; |
| 2664 | else |
| 2665 | logical_len = oe->num_bytes; |
| 2666 | btrfs_set_stack_file_extent_num_bytes(&stack_fi, logical_len); |
| 2667 | btrfs_set_stack_file_extent_ram_bytes(&stack_fi, logical_len); |
| 2668 | btrfs_set_stack_file_extent_compression(&stack_fi, oe->compress_type); |
| 2669 | /* Encryption and other encoding is reserved and all 0 */ |
| 2670 | |
Nikolay Borisov | 3c38c87 | 2020-09-18 12:15:51 +0300 | [diff] [blame] | 2671 | return insert_reserved_file_extent(trans, BTRFS_I(oe->inode), |
| 2672 | oe->file_offset, &stack_fi, |
| 2673 | oe->qgroup_rsv); |
Qu Wenruo | 203f44c5 | 2020-06-10 09:04:40 +0800 | [diff] [blame] | 2674 | } |
| 2675 | |
| 2676 | /* |
| 2677 | * As ordered data IO finishes, this gets called so we can finish |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2678 | * an ordered extent if the range of bytes in the file it covers are |
| 2679 | * fully written. |
| 2680 | */ |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2681 | static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2682 | { |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2683 | struct inode *inode = ordered_extent->inode; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2684 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2685 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 2686 | struct btrfs_trans_handle *trans = NULL; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2687 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 2688 | struct extent_state *cached_state = NULL; |
Omar Sandoval | bffe633 | 2019-12-02 17:34:19 -0800 | [diff] [blame] | 2689 | u64 start, end; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 2690 | int compress_type = 0; |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 2691 | int ret = 0; |
Omar Sandoval | bffe633 | 2019-12-02 17:34:19 -0800 | [diff] [blame] | 2692 | u64 logical_len = ordered_extent->num_bytes; |
Nikolay Borisov | 8d51012 | 2019-10-08 20:43:06 +0300 | [diff] [blame] | 2693 | bool freespace_inode; |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 2694 | bool truncated = false; |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 2695 | bool range_locked = false; |
| 2696 | bool clear_new_delalloc_bytes = false; |
Josef Bacik | 49940bd | 2018-10-11 15:54:21 -0400 | [diff] [blame] | 2697 | bool clear_reserved_extent = true; |
Omar Sandoval | 313facc | 2019-12-02 17:34:18 -0800 | [diff] [blame] | 2698 | unsigned int clear_bits; |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 2699 | |
Omar Sandoval | bffe633 | 2019-12-02 17:34:19 -0800 | [diff] [blame] | 2700 | start = ordered_extent->file_offset; |
| 2701 | end = start + ordered_extent->num_bytes - 1; |
| 2702 | |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 2703 | if (!test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) && |
| 2704 | !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags) && |
| 2705 | !test_bit(BTRFS_ORDERED_DIRECT, &ordered_extent->flags)) |
| 2706 | clear_new_delalloc_bytes = true; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2707 | |
Nikolay Borisov | 8d51012 | 2019-10-08 20:43:06 +0300 | [diff] [blame] | 2708 | freespace_inode = btrfs_is_free_space_inode(BTRFS_I(inode)); |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 2709 | |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2710 | if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) { |
| 2711 | ret = -EIO; |
| 2712 | goto out; |
| 2713 | } |
| 2714 | |
Omar Sandoval | bffe633 | 2019-12-02 17:34:19 -0800 | [diff] [blame] | 2715 | btrfs_free_io_failure_record(BTRFS_I(inode), start, end); |
Miao Xie | f612496 | 2014-09-12 18:44:04 +0800 | [diff] [blame] | 2716 | |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 2717 | if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) { |
| 2718 | truncated = true; |
| 2719 | logical_len = ordered_extent->truncated_len; |
| 2720 | /* Truncated the entire extent, don't bother adding */ |
| 2721 | if (!logical_len) |
| 2722 | goto out; |
| 2723 | } |
| 2724 | |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 2725 | if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) { |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2726 | BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */ |
Qu Wenruo | 94ed938 | 2015-09-08 17:25:56 +0800 | [diff] [blame] | 2727 | |
Josef Bacik | d923afe | 2020-01-17 09:02:23 -0500 | [diff] [blame] | 2728 | btrfs_inode_safe_disk_i_size_write(inode, 0); |
Nikolay Borisov | 8d51012 | 2019-10-08 20:43:06 +0300 | [diff] [blame] | 2729 | if (freespace_inode) |
| 2730 | trans = btrfs_join_transaction_spacecache(root); |
Josef Bacik | 6c760c0 | 2012-11-09 10:53:21 -0500 | [diff] [blame] | 2731 | else |
| 2732 | trans = btrfs_join_transaction(root); |
| 2733 | if (IS_ERR(trans)) { |
| 2734 | ret = PTR_ERR(trans); |
| 2735 | trans = NULL; |
| 2736 | goto out; |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 2737 | } |
Josef Bacik | 69fe2d7 | 2017-10-19 14:15:57 -0400 | [diff] [blame] | 2738 | trans->block_rsv = &BTRFS_I(inode)->block_rsv; |
Josef Bacik | 6c760c0 | 2012-11-09 10:53:21 -0500 | [diff] [blame] | 2739 | ret = btrfs_update_inode_fallback(trans, root, inode); |
| 2740 | if (ret) /* -ENOMEM or corruption */ |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 2741 | btrfs_abort_transaction(trans, ret); |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 2742 | goto out; |
| 2743 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2744 | |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 2745 | range_locked = true; |
Omar Sandoval | bffe633 | 2019-12-02 17:34:19 -0800 | [diff] [blame] | 2746 | lock_extent_bits(io_tree, start, end, &cached_state); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2747 | |
Nikolay Borisov | 8d51012 | 2019-10-08 20:43:06 +0300 | [diff] [blame] | 2748 | if (freespace_inode) |
| 2749 | trans = btrfs_join_transaction_spacecache(root); |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 2750 | else |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 2751 | trans = btrfs_join_transaction(root); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2752 | if (IS_ERR(trans)) { |
| 2753 | ret = PTR_ERR(trans); |
| 2754 | trans = NULL; |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 2755 | goto out; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2756 | } |
Chris Mason | a79b7d4 | 2014-05-22 16:18:52 -0700 | [diff] [blame] | 2757 | |
Josef Bacik | 69fe2d7 | 2017-10-19 14:15:57 -0400 | [diff] [blame] | 2758 | trans->block_rsv = &BTRFS_I(inode)->block_rsv; |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 2759 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2760 | if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags)) |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 2761 | compress_type = ordered_extent->compress_type; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2762 | if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) { |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 2763 | BUG_ON(compress_type); |
Nikolay Borisov | 7a6d706 | 2017-02-20 13:50:48 +0200 | [diff] [blame] | 2764 | ret = btrfs_mark_extent_written(trans, BTRFS_I(inode), |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2765 | ordered_extent->file_offset, |
| 2766 | ordered_extent->file_offset + |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 2767 | logical_len); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2768 | } else { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2769 | BUG_ON(root == fs_info->tree_root); |
Nikolay Borisov | 3c38c87 | 2020-09-18 12:15:51 +0300 | [diff] [blame] | 2770 | ret = insert_ordered_extent_file_extent(trans, ordered_extent); |
Josef Bacik | 49940bd | 2018-10-11 15:54:21 -0400 | [diff] [blame] | 2771 | if (!ret) { |
| 2772 | clear_reserved_extent = false; |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2773 | btrfs_release_delalloc_bytes(fs_info, |
Omar Sandoval | bffe633 | 2019-12-02 17:34:19 -0800 | [diff] [blame] | 2774 | ordered_extent->disk_bytenr, |
| 2775 | ordered_extent->disk_num_bytes); |
Josef Bacik | 49940bd | 2018-10-11 15:54:21 -0400 | [diff] [blame] | 2776 | } |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2777 | } |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 2778 | unpin_extent_cache(&BTRFS_I(inode)->extent_tree, |
Omar Sandoval | bffe633 | 2019-12-02 17:34:19 -0800 | [diff] [blame] | 2779 | ordered_extent->file_offset, |
| 2780 | ordered_extent->num_bytes, trans->transid); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2781 | if (ret < 0) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 2782 | btrfs_abort_transaction(trans, ret); |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 2783 | goto out; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2784 | } |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 2785 | |
Nikolay Borisov | 510f85e | 2020-09-18 12:15:52 +0300 | [diff] [blame] | 2786 | ret = add_pending_csums(trans, &ordered_extent->list); |
Nikolay Borisov | ac01f26 | 2018-01-08 10:59:43 +0200 | [diff] [blame] | 2787 | if (ret) { |
| 2788 | btrfs_abort_transaction(trans, ret); |
| 2789 | goto out; |
| 2790 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2791 | |
Josef Bacik | d923afe | 2020-01-17 09:02:23 -0500 | [diff] [blame] | 2792 | btrfs_inode_safe_disk_i_size_write(inode, 0); |
Josef Bacik | 6c760c0 | 2012-11-09 10:53:21 -0500 | [diff] [blame] | 2793 | ret = btrfs_update_inode_fallback(trans, root, inode); |
| 2794 | if (ret) { /* -ENOMEM or corruption */ |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 2795 | btrfs_abort_transaction(trans, ret); |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 2796 | goto out; |
Josef Bacik | 1ef30be | 2011-04-05 19:25:36 -0400 | [diff] [blame] | 2797 | } |
| 2798 | ret = 0; |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 2799 | out: |
Omar Sandoval | 313facc | 2019-12-02 17:34:18 -0800 | [diff] [blame] | 2800 | clear_bits = EXTENT_DEFRAG; |
| 2801 | if (range_locked) |
| 2802 | clear_bits |= EXTENT_LOCKED; |
| 2803 | if (clear_new_delalloc_bytes) |
| 2804 | clear_bits |= EXTENT_DELALLOC_NEW; |
Omar Sandoval | bffe633 | 2019-12-02 17:34:19 -0800 | [diff] [blame] | 2805 | clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, clear_bits, |
| 2806 | (clear_bits & EXTENT_LOCKED) ? 1 : 0, 0, |
Omar Sandoval | 313facc | 2019-12-02 17:34:18 -0800 | [diff] [blame] | 2807 | &cached_state); |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 2808 | |
Miao Xie | a698d075 | 2012-09-20 01:51:59 -0600 | [diff] [blame] | 2809 | if (trans) |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 2810 | btrfs_end_transaction(trans); |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 2811 | |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 2812 | if (ret || truncated) { |
Omar Sandoval | bffe633 | 2019-12-02 17:34:19 -0800 | [diff] [blame] | 2813 | u64 unwritten_start = start; |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 2814 | |
| 2815 | if (truncated) |
Omar Sandoval | bffe633 | 2019-12-02 17:34:19 -0800 | [diff] [blame] | 2816 | unwritten_start += logical_len; |
| 2817 | clear_extent_uptodate(io_tree, unwritten_start, end, NULL); |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 2818 | |
| 2819 | /* 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] | 2820 | btrfs_drop_extent_cache(BTRFS_I(inode), unwritten_start, end, 0); |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2821 | |
Josef Bacik | 0bec9ef | 2013-01-31 14:58:00 -0500 | [diff] [blame] | 2822 | /* |
| 2823 | * If the ordered extent had an IOERR or something else went |
| 2824 | * wrong we need to return the space for this ordered extent |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 2825 | * back to the allocator. We only free the extent in the |
| 2826 | * truncated case if we didn't write out the extent at all. |
Josef Bacik | 49940bd | 2018-10-11 15:54:21 -0400 | [diff] [blame] | 2827 | * |
| 2828 | * If we made it past insert_reserved_file_extent before we |
| 2829 | * errored out then we don't need to do this as the accounting |
| 2830 | * has already been done. |
Josef Bacik | 0bec9ef | 2013-01-31 14:58:00 -0500 | [diff] [blame] | 2831 | */ |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 2832 | if ((ret || !logical_len) && |
Josef Bacik | 49940bd | 2018-10-11 15:54:21 -0400 | [diff] [blame] | 2833 | clear_reserved_extent && |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 2834 | !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) && |
Nikolay Borisov | 4eaaec2 | 2019-11-21 14:03:29 +0200 | [diff] [blame] | 2835 | !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) { |
| 2836 | /* |
| 2837 | * Discard the range before returning it back to the |
| 2838 | * free space pool |
| 2839 | */ |
Dennis Zhou | 46b27f5 | 2019-12-13 16:22:11 -0800 | [diff] [blame] | 2840 | if (ret && btrfs_test_opt(fs_info, DISCARD_SYNC)) |
Nikolay Borisov | 4eaaec2 | 2019-11-21 14:03:29 +0200 | [diff] [blame] | 2841 | btrfs_discard_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, |
| 2844 | NULL); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2845 | btrfs_free_reserved_extent(fs_info, |
Omar Sandoval | bffe633 | 2019-12-02 17:34:19 -0800 | [diff] [blame] | 2846 | ordered_extent->disk_bytenr, |
| 2847 | ordered_extent->disk_num_bytes, 1); |
Nikolay Borisov | 4eaaec2 | 2019-11-21 14:03:29 +0200 | [diff] [blame] | 2848 | } |
Josef Bacik | 0bec9ef | 2013-01-31 14:58:00 -0500 | [diff] [blame] | 2849 | } |
| 2850 | |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2851 | /* |
Liu Bo | 8bad3c0 | 2012-06-18 12:14:23 +0800 | [diff] [blame] | 2852 | * This needs to be done to make sure anybody waiting knows we are done |
| 2853 | * updating everything for this ordered extent. |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2854 | */ |
Nikolay Borisov | 71fe0a5 | 2020-09-18 12:15:50 +0300 | [diff] [blame] | 2855 | btrfs_remove_ordered_extent(BTRFS_I(inode), ordered_extent); |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2856 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2857 | /* once for us */ |
| 2858 | btrfs_put_ordered_extent(ordered_extent); |
| 2859 | /* once for the tree */ |
| 2860 | btrfs_put_ordered_extent(ordered_extent); |
| 2861 | |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2862 | return ret; |
| 2863 | } |
| 2864 | |
| 2865 | static void finish_ordered_fn(struct btrfs_work *work) |
| 2866 | { |
| 2867 | struct btrfs_ordered_extent *ordered_extent; |
| 2868 | ordered_extent = container_of(work, struct btrfs_ordered_extent, work); |
| 2869 | btrfs_finish_ordered_io(ordered_extent); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2870 | } |
| 2871 | |
Nikolay Borisov | c629732 | 2018-11-08 10:18:08 +0200 | [diff] [blame] | 2872 | void btrfs_writepage_endio_finish_ordered(struct page *page, u64 start, |
| 2873 | u64 end, int uptodate) |
Chris Mason | 211f90e | 2008-07-18 11:56:15 -0400 | [diff] [blame] | 2874 | { |
Nikolay Borisov | 3347c48 | 2020-08-31 14:42:44 +0300 | [diff] [blame] | 2875 | struct btrfs_inode *inode = BTRFS_I(page->mapping->host); |
| 2876 | struct btrfs_fs_info *fs_info = inode->root->fs_info; |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2877 | struct btrfs_ordered_extent *ordered_extent = NULL; |
Liu Bo | 9e0af23 | 2014-08-15 23:36:53 +0800 | [diff] [blame] | 2878 | struct btrfs_workqueue *wq; |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2879 | |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 2880 | trace_btrfs_writepage_end_io_hook(page, start, end, uptodate); |
| 2881 | |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 2882 | ClearPagePrivate2(page); |
Nikolay Borisov | 3347c48 | 2020-08-31 14:42:44 +0300 | [diff] [blame] | 2883 | if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start, |
| 2884 | end - start + 1, uptodate)) |
David Sterba | c3988d6 | 2017-02-17 15:18:32 +0100 | [diff] [blame] | 2885 | return; |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2886 | |
Nikolay Borisov | 3347c48 | 2020-08-31 14:42:44 +0300 | [diff] [blame] | 2887 | if (btrfs_is_free_space_inode(inode)) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2888 | wq = fs_info->endio_freespace_worker; |
Omar Sandoval | a0cac0e | 2019-09-16 11:30:57 -0700 | [diff] [blame] | 2889 | else |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2890 | wq = fs_info->endio_write_workers; |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2891 | |
Omar Sandoval | a0cac0e | 2019-09-16 11:30:57 -0700 | [diff] [blame] | 2892 | btrfs_init_work(&ordered_extent->work, finish_ordered_fn, NULL, NULL); |
Liu Bo | 9e0af23 | 2014-08-15 23:36:53 +0800 | [diff] [blame] | 2893 | btrfs_queue_work(wq, &ordered_extent->work); |
Chris Mason | 211f90e | 2008-07-18 11:56:15 -0400 | [diff] [blame] | 2894 | } |
| 2895 | |
Qu Wenruo | 265d4ac | 2020-10-21 14:24:54 +0800 | [diff] [blame] | 2896 | /* |
| 2897 | * check_data_csum - verify checksum of one sector of uncompressed data |
| 2898 | * @inode: the inode |
| 2899 | * @io_bio: btrfs_io_bio which contains the csum |
| 2900 | * @icsum: checksum index in the io_bio->csum array, size of csum_size |
| 2901 | * @page: page where is the data to be verified |
| 2902 | * @pgoff: offset inside the page |
| 2903 | * |
| 2904 | * The length of such check is always one sector size. |
| 2905 | */ |
Omar Sandoval | 47df776 | 2020-04-16 14:46:17 -0700 | [diff] [blame] | 2906 | static int check_data_csum(struct inode *inode, struct btrfs_io_bio *io_bio, |
Qu Wenruo | 265d4ac | 2020-10-21 14:24:54 +0800 | [diff] [blame] | 2907 | int icsum, struct page *page, int pgoff) |
Miao Xie | dc380ae | 2014-09-12 18:43:55 +0800 | [diff] [blame] | 2908 | { |
Johannes Thumshirn | d517857 | 2019-06-03 16:58:57 +0200 | [diff] [blame] | 2909 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
| 2910 | SHASH_DESC_ON_STACK(shash, fs_info->csum_shash); |
Miao Xie | dc380ae | 2014-09-12 18:43:55 +0800 | [diff] [blame] | 2911 | char *kaddr; |
Qu Wenruo | 265d4ac | 2020-10-21 14:24:54 +0800 | [diff] [blame] | 2912 | u32 len = fs_info->sectorsize; |
David Sterba | 223486c | 2020-07-02 11:27:30 +0200 | [diff] [blame] | 2913 | const u32 csum_size = fs_info->csum_size; |
Johannes Thumshirn | d517857 | 2019-06-03 16:58:57 +0200 | [diff] [blame] | 2914 | u8 *csum_expected; |
| 2915 | u8 csum[BTRFS_CSUM_SIZE]; |
Miao Xie | dc380ae | 2014-09-12 18:43:55 +0800 | [diff] [blame] | 2916 | |
Qu Wenruo | 265d4ac | 2020-10-21 14:24:54 +0800 | [diff] [blame] | 2917 | ASSERT(pgoff + len <= PAGE_SIZE); |
| 2918 | |
Johannes Thumshirn | d517857 | 2019-06-03 16:58:57 +0200 | [diff] [blame] | 2919 | csum_expected = ((u8 *)io_bio->csum) + icsum * csum_size; |
Miao Xie | dc380ae | 2014-09-12 18:43:55 +0800 | [diff] [blame] | 2920 | |
| 2921 | kaddr = kmap_atomic(page); |
Johannes Thumshirn | d517857 | 2019-06-03 16:58:57 +0200 | [diff] [blame] | 2922 | shash->tfm = fs_info->csum_shash; |
| 2923 | |
Eric Biggers | fd08001 | 2020-04-30 23:51:59 -0700 | [diff] [blame] | 2924 | crypto_shash_digest(shash, kaddr + pgoff, len, csum); |
Johannes Thumshirn | d517857 | 2019-06-03 16:58:57 +0200 | [diff] [blame] | 2925 | |
| 2926 | if (memcmp(csum, csum_expected, csum_size)) |
Miao Xie | dc380ae | 2014-09-12 18:43:55 +0800 | [diff] [blame] | 2927 | goto zeroit; |
| 2928 | |
| 2929 | kunmap_atomic(kaddr); |
| 2930 | return 0; |
| 2931 | zeroit: |
Qu Wenruo | 265d4ac | 2020-10-21 14:24:54 +0800 | [diff] [blame] | 2932 | btrfs_print_data_csum_error(BTRFS_I(inode), page_offset(page) + pgoff, |
| 2933 | csum, csum_expected, io_bio->mirror_num); |
Nikolay Borisov | 814723e | 2020-07-02 15:23:32 +0300 | [diff] [blame] | 2934 | if (io_bio->device) |
| 2935 | btrfs_dev_stat_inc_and_print(io_bio->device, |
| 2936 | BTRFS_DEV_STAT_CORRUPTION_ERRS); |
Miao Xie | dc380ae | 2014-09-12 18:43:55 +0800 | [diff] [blame] | 2937 | memset(kaddr + pgoff, 1, len); |
| 2938 | flush_dcache_page(page); |
| 2939 | kunmap_atomic(kaddr); |
Miao Xie | dc380ae | 2014-09-12 18:43:55 +0800 | [diff] [blame] | 2940 | return -EIO; |
| 2941 | } |
| 2942 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2943 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2944 | * 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] | 2945 | * if there's a match, we allow the bio to finish. If not, the code in |
| 2946 | * extent_io.c will try to find good copies for us. |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2947 | */ |
Nikolay Borisov | 9a446d6 | 2020-09-18 16:34:33 +0300 | [diff] [blame] | 2948 | int btrfs_verify_data_csum(struct btrfs_io_bio *io_bio, u64 phy_offset, |
| 2949 | struct page *page, u64 start, u64 end, int mirror) |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 2950 | { |
Miao Xie | 4eee4fa | 2012-12-21 09:17:45 +0000 | [diff] [blame] | 2951 | size_t offset = start - page_offset(page); |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 2952 | struct inode *inode = page->mapping->host; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2953 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | ff79f81 | 2007-10-15 16:22:25 -0400 | [diff] [blame] | 2954 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2955 | |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 2956 | if (PageChecked(page)) { |
| 2957 | ClearPageChecked(page); |
Miao Xie | dc380ae | 2014-09-12 18:43:55 +0800 | [diff] [blame] | 2958 | return 0; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 2959 | } |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 2960 | |
| 2961 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM) |
Miao Xie | dc380ae | 2014-09-12 18:43:55 +0800 | [diff] [blame] | 2962 | return 0; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 2963 | |
Josef Bacik | 42437a6 | 2020-10-16 11:29:18 -0400 | [diff] [blame] | 2964 | if (!root->fs_info->csum_root) |
| 2965 | return 0; |
| 2966 | |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 2967 | if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID && |
Chris Mason | 9655d29 | 2009-09-02 15:22:30 -0400 | [diff] [blame] | 2968 | test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) { |
David Sterba | 9116621 | 2016-04-26 23:54:39 +0200 | [diff] [blame] | 2969 | clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM); |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 2970 | return 0; |
| 2971 | } |
| 2972 | |
David Sterba | 265fdfa | 2020-07-01 21:19:09 +0200 | [diff] [blame] | 2973 | phy_offset >>= root->fs_info->sectorsize_bits; |
Qu Wenruo | 265d4ac | 2020-10-21 14:24:54 +0800 | [diff] [blame] | 2974 | return check_data_csum(inode, io_bio, phy_offset, page, offset); |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 2975 | } |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 2976 | |
Nikolay Borisov | c1c3fac | 2018-01-16 09:31:58 +0200 | [diff] [blame] | 2977 | /* |
| 2978 | * btrfs_add_delayed_iput - perform a delayed iput on @inode |
| 2979 | * |
| 2980 | * @inode: The inode we want to perform iput on |
| 2981 | * |
| 2982 | * This function uses the generic vfs_inode::i_count to track whether we should |
| 2983 | * just decrement it (in case it's > 1) or if this is the last iput then link |
| 2984 | * the inode to the delayed iput machinery. Delayed iputs are processed at |
| 2985 | * transaction commit time/superblock commit/cleaner kthread. |
| 2986 | */ |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 2987 | void btrfs_add_delayed_iput(struct inode *inode) |
| 2988 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2989 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
David Sterba | 8089fe6 | 2015-11-19 14:15:51 +0100 | [diff] [blame] | 2990 | struct btrfs_inode *binode = BTRFS_I(inode); |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 2991 | |
| 2992 | if (atomic_add_unless(&inode->i_count, -1, 1)) |
| 2993 | return; |
| 2994 | |
Josef Bacik | 034f784 | 2018-12-03 11:06:52 -0500 | [diff] [blame] | 2995 | atomic_inc(&fs_info->nr_delayed_iputs); |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 2996 | spin_lock(&fs_info->delayed_iput_lock); |
Nikolay Borisov | c1c3fac | 2018-01-16 09:31:58 +0200 | [diff] [blame] | 2997 | ASSERT(list_empty(&binode->delayed_iput)); |
| 2998 | list_add_tail(&binode->delayed_iput, &fs_info->delayed_iputs); |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 2999 | spin_unlock(&fs_info->delayed_iput_lock); |
Josef Bacik | fd340d0 | 2019-01-11 10:21:02 -0500 | [diff] [blame] | 3000 | if (!test_bit(BTRFS_FS_CLEANER_RUNNING, &fs_info->flags)) |
| 3001 | wake_up_process(fs_info->cleaner_kthread); |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 3002 | } |
| 3003 | |
Josef Bacik | 63611e7 | 2019-06-18 10:59:18 -0400 | [diff] [blame] | 3004 | static void run_delayed_iput_locked(struct btrfs_fs_info *fs_info, |
| 3005 | struct btrfs_inode *inode) |
| 3006 | { |
| 3007 | list_del_init(&inode->delayed_iput); |
| 3008 | spin_unlock(&fs_info->delayed_iput_lock); |
| 3009 | iput(&inode->vfs_inode); |
| 3010 | if (atomic_dec_and_test(&fs_info->nr_delayed_iputs)) |
| 3011 | wake_up(&fs_info->delayed_iputs_wait); |
| 3012 | spin_lock(&fs_info->delayed_iput_lock); |
| 3013 | } |
| 3014 | |
| 3015 | static void btrfs_run_delayed_iput(struct btrfs_fs_info *fs_info, |
| 3016 | struct btrfs_inode *inode) |
| 3017 | { |
| 3018 | if (!list_empty(&inode->delayed_iput)) { |
| 3019 | spin_lock(&fs_info->delayed_iput_lock); |
| 3020 | if (!list_empty(&inode->delayed_iput)) |
| 3021 | run_delayed_iput_locked(fs_info, inode); |
| 3022 | spin_unlock(&fs_info->delayed_iput_lock); |
| 3023 | } |
| 3024 | } |
| 3025 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3026 | void btrfs_run_delayed_iputs(struct btrfs_fs_info *fs_info) |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 3027 | { |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 3028 | |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 3029 | spin_lock(&fs_info->delayed_iput_lock); |
David Sterba | 8089fe6 | 2015-11-19 14:15:51 +0100 | [diff] [blame] | 3030 | while (!list_empty(&fs_info->delayed_iputs)) { |
| 3031 | struct btrfs_inode *inode; |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 3032 | |
David Sterba | 8089fe6 | 2015-11-19 14:15:51 +0100 | [diff] [blame] | 3033 | inode = list_first_entry(&fs_info->delayed_iputs, |
| 3034 | struct btrfs_inode, delayed_iput); |
Josef Bacik | 63611e7 | 2019-06-18 10:59:18 -0400 | [diff] [blame] | 3035 | run_delayed_iput_locked(fs_info, inode); |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 3036 | } |
David Sterba | 8089fe6 | 2015-11-19 14:15:51 +0100 | [diff] [blame] | 3037 | spin_unlock(&fs_info->delayed_iput_lock); |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 3038 | } |
| 3039 | |
Josef Bacik | 034f784 | 2018-12-03 11:06:52 -0500 | [diff] [blame] | 3040 | /** |
| 3041 | * btrfs_wait_on_delayed_iputs - wait on the delayed iputs to be done running |
| 3042 | * @fs_info - the fs_info for this fs |
| 3043 | * @return - EINTR if we were killed, 0 if nothing's pending |
| 3044 | * |
| 3045 | * This will wait on any delayed iputs that are currently running with KILLABLE |
| 3046 | * set. Once they are all done running we will return, unless we are killed in |
| 3047 | * which case we return EINTR. This helps in user operations like fallocate etc |
| 3048 | * that might get blocked on the iputs. |
| 3049 | */ |
| 3050 | int btrfs_wait_on_delayed_iputs(struct btrfs_fs_info *fs_info) |
| 3051 | { |
| 3052 | int ret = wait_event_killable(fs_info->delayed_iputs_wait, |
| 3053 | atomic_read(&fs_info->nr_delayed_iputs) == 0); |
| 3054 | if (ret) |
| 3055 | return -EINTR; |
| 3056 | return 0; |
| 3057 | } |
| 3058 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3059 | /* |
Omar Sandoval | f7e9e8f | 2018-05-11 13:13:32 -0700 | [diff] [blame] | 3060 | * This creates an orphan entry for the given inode in case something goes wrong |
| 3061 | * in the middle of an unlink. |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3062 | */ |
Nikolay Borisov | 73f2e54 | 2017-02-20 13:50:59 +0200 | [diff] [blame] | 3063 | int btrfs_orphan_add(struct btrfs_trans_handle *trans, |
Omar Sandoval | 2791906 | 2018-05-11 13:13:37 -0700 | [diff] [blame] | 3064 | struct btrfs_inode *inode) |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3065 | { |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3066 | int ret; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3067 | |
Omar Sandoval | 2791906 | 2018-05-11 13:13:37 -0700 | [diff] [blame] | 3068 | ret = btrfs_insert_orphan_item(trans, inode->root, btrfs_ino(inode)); |
| 3069 | if (ret && ret != -EEXIST) { |
| 3070 | btrfs_abort_transaction(trans, ret); |
| 3071 | return ret; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3072 | } |
| 3073 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3074 | return 0; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3075 | } |
| 3076 | |
| 3077 | /* |
Omar Sandoval | f7e9e8f | 2018-05-11 13:13:32 -0700 | [diff] [blame] | 3078 | * We have done the delete so we can go ahead and remove the orphan item for |
| 3079 | * this particular inode. |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3080 | */ |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 3081 | static int btrfs_orphan_del(struct btrfs_trans_handle *trans, |
Nikolay Borisov | 3d6ae7b | 2017-02-20 13:50:58 +0200 | [diff] [blame] | 3082 | struct btrfs_inode *inode) |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3083 | { |
Omar Sandoval | 2791906 | 2018-05-11 13:13:37 -0700 | [diff] [blame] | 3084 | return btrfs_del_orphan_item(trans, inode->root, btrfs_ino(inode)); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3085 | } |
| 3086 | |
| 3087 | /* |
| 3088 | * this cleans up any orphans that may be left on the list from the last use |
| 3089 | * of this root. |
| 3090 | */ |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3091 | int btrfs_orphan_cleanup(struct btrfs_root *root) |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3092 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3093 | struct btrfs_fs_info *fs_info = root->fs_info; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3094 | struct btrfs_path *path; |
| 3095 | struct extent_buffer *leaf; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3096 | struct btrfs_key key, found_key; |
| 3097 | struct btrfs_trans_handle *trans; |
| 3098 | struct inode *inode; |
Josef Bacik | 8f6d7f4 | 2011-09-26 15:55:20 -0400 | [diff] [blame] | 3099 | u64 last_objectid = 0; |
Omar Sandoval | f7e9e8f | 2018-05-11 13:13:32 -0700 | [diff] [blame] | 3100 | int ret = 0, nr_unlink = 0; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3101 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3102 | if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED)) |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3103 | return 0; |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 3104 | |
| 3105 | path = btrfs_alloc_path(); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3106 | if (!path) { |
| 3107 | ret = -ENOMEM; |
| 3108 | goto out; |
| 3109 | } |
David Sterba | e4058b5 | 2015-11-27 16:31:35 +0100 | [diff] [blame] | 3110 | path->reada = READA_BACK; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3111 | |
| 3112 | key.objectid = BTRFS_ORPHAN_OBJECTID; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 3113 | key.type = BTRFS_ORPHAN_ITEM_KEY; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3114 | key.offset = (u64)-1; |
| 3115 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3116 | while (1) { |
| 3117 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3118 | if (ret < 0) |
| 3119 | goto out; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3120 | |
| 3121 | /* |
| 3122 | * 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] | 3123 | * 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] | 3124 | * find the key and see if we have stuff that matches |
| 3125 | */ |
| 3126 | if (ret > 0) { |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3127 | ret = 0; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3128 | if (path->slots[0] == 0) |
| 3129 | break; |
| 3130 | path->slots[0]--; |
| 3131 | } |
| 3132 | |
| 3133 | /* pull out the item */ |
| 3134 | leaf = path->nodes[0]; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3135 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 3136 | |
| 3137 | /* make sure the item matches what we want */ |
| 3138 | if (found_key.objectid != BTRFS_ORPHAN_OBJECTID) |
| 3139 | break; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 3140 | if (found_key.type != BTRFS_ORPHAN_ITEM_KEY) |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3141 | break; |
| 3142 | |
| 3143 | /* release the path since we're done with it */ |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3144 | btrfs_release_path(path); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3145 | |
| 3146 | /* |
| 3147 | * this is where we are basically btrfs_lookup, without the |
| 3148 | * crossing root thing. we store the inode number in the |
| 3149 | * offset of the orphan item. |
| 3150 | */ |
Josef Bacik | 8f6d7f4 | 2011-09-26 15:55:20 -0400 | [diff] [blame] | 3151 | |
| 3152 | if (found_key.offset == last_objectid) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3153 | btrfs_err(fs_info, |
| 3154 | "Error removing orphan entry, stopping orphan cleanup"); |
Josef Bacik | 8f6d7f4 | 2011-09-26 15:55:20 -0400 | [diff] [blame] | 3155 | ret = -EINVAL; |
| 3156 | goto out; |
| 3157 | } |
| 3158 | |
| 3159 | last_objectid = found_key.offset; |
| 3160 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3161 | found_key.objectid = found_key.offset; |
| 3162 | found_key.type = BTRFS_INODE_ITEM_KEY; |
| 3163 | found_key.offset = 0; |
David Sterba | 0202e83 | 2020-05-15 19:35:59 +0200 | [diff] [blame] | 3164 | inode = btrfs_iget(fs_info->sb, last_objectid, root); |
Rusty Russell | 8c6ffba | 2013-07-15 11:20:32 +0930 | [diff] [blame] | 3165 | ret = PTR_ERR_OR_ZERO(inode); |
Filipe Manana | 6771089 | 2016-06-06 11:51:25 +0100 | [diff] [blame] | 3166 | if (ret && ret != -ENOENT) |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3167 | goto out; |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 3168 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3169 | if (ret == -ENOENT && root == fs_info->tree_root) { |
Arne Jansen | f8e9e0b | 2011-12-14 20:12:02 -0500 | [diff] [blame] | 3170 | struct btrfs_root *dead_root; |
Arne Jansen | f8e9e0b | 2011-12-14 20:12:02 -0500 | [diff] [blame] | 3171 | int is_dead_root = 0; |
| 3172 | |
| 3173 | /* |
| 3174 | * this is an orphan in the tree root. Currently these |
| 3175 | * could come from 2 sources: |
| 3176 | * a) a snapshot deletion in progress |
| 3177 | * b) a free space cache inode |
| 3178 | * We need to distinguish those two, as the snapshot |
| 3179 | * orphan must not get deleted. |
| 3180 | * find_dead_roots already ran before us, so if this |
| 3181 | * is a snapshot deletion, we should find the root |
Robbie Ko | a619b3c | 2020-05-07 10:54:40 +0800 | [diff] [blame] | 3182 | * in the fs_roots radix tree. |
Arne Jansen | f8e9e0b | 2011-12-14 20:12:02 -0500 | [diff] [blame] | 3183 | */ |
Robbie Ko | a619b3c | 2020-05-07 10:54:40 +0800 | [diff] [blame] | 3184 | |
| 3185 | spin_lock(&fs_info->fs_roots_radix_lock); |
| 3186 | dead_root = radix_tree_lookup(&fs_info->fs_roots_radix, |
| 3187 | (unsigned long)found_key.objectid); |
| 3188 | if (dead_root && btrfs_root_refs(&dead_root->root_item) == 0) |
| 3189 | is_dead_root = 1; |
| 3190 | spin_unlock(&fs_info->fs_roots_radix_lock); |
| 3191 | |
Arne Jansen | f8e9e0b | 2011-12-14 20:12:02 -0500 | [diff] [blame] | 3192 | if (is_dead_root) { |
| 3193 | /* prevent this orphan from being found again */ |
| 3194 | key.offset = found_key.objectid - 1; |
| 3195 | continue; |
| 3196 | } |
Omar Sandoval | f7e9e8f | 2018-05-11 13:13:32 -0700 | [diff] [blame] | 3197 | |
Arne Jansen | f8e9e0b | 2011-12-14 20:12:02 -0500 | [diff] [blame] | 3198 | } |
Omar Sandoval | f7e9e8f | 2018-05-11 13:13:32 -0700 | [diff] [blame] | 3199 | |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 3200 | /* |
Omar Sandoval | f7e9e8f | 2018-05-11 13:13:32 -0700 | [diff] [blame] | 3201 | * If we have an inode with links, there are a couple of |
| 3202 | * possibilities. Old kernels (before v3.12) used to create an |
| 3203 | * orphan item for truncate indicating that there were possibly |
| 3204 | * extent items past i_size that needed to be deleted. In v3.12, |
| 3205 | * truncate was changed to update i_size in sync with the extent |
| 3206 | * items, but the (useless) orphan item was still created. Since |
| 3207 | * v4.18, we don't create the orphan item for truncate at all. |
| 3208 | * |
| 3209 | * So, this item could mean that we need to do a truncate, but |
| 3210 | * only if this filesystem was last used on a pre-v3.12 kernel |
| 3211 | * and was not cleanly unmounted. The odds of that are quite |
| 3212 | * slim, and it's a pain to do the truncate now, so just delete |
| 3213 | * the orphan item. |
| 3214 | * |
| 3215 | * It's also possible that this orphan item was supposed to be |
| 3216 | * deleted but wasn't. The inode number may have been reused, |
| 3217 | * but either way, we can delete the orphan item. |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 3218 | */ |
Omar Sandoval | f7e9e8f | 2018-05-11 13:13:32 -0700 | [diff] [blame] | 3219 | if (ret == -ENOENT || inode->i_nlink) { |
| 3220 | if (!ret) |
| 3221 | iput(inode); |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 3222 | trans = btrfs_start_transaction(root, 1); |
| 3223 | if (IS_ERR(trans)) { |
| 3224 | ret = PTR_ERR(trans); |
| 3225 | goto out; |
| 3226 | } |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3227 | btrfs_debug(fs_info, "auto deleting %Lu", |
| 3228 | found_key.objectid); |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 3229 | ret = btrfs_del_orphan_item(trans, root, |
| 3230 | found_key.objectid); |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 3231 | btrfs_end_transaction(trans); |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 3232 | if (ret) |
| 3233 | goto out; |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 3234 | continue; |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3235 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3236 | |
Omar Sandoval | f7e9e8f | 2018-05-11 13:13:32 -0700 | [diff] [blame] | 3237 | nr_unlink++; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3238 | |
| 3239 | /* this will do delete_inode and everything for us */ |
| 3240 | iput(inode); |
| 3241 | } |
Miao Xie | 3254c87 | 2011-11-10 20:45:05 -0500 | [diff] [blame] | 3242 | /* release the path since we're done with it */ |
| 3243 | btrfs_release_path(path); |
| 3244 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3245 | root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE; |
| 3246 | |
Omar Sandoval | a575cee | 2018-05-11 13:13:38 -0700 | [diff] [blame] | 3247 | if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) { |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 3248 | trans = btrfs_join_transaction(root); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3249 | if (!IS_ERR(trans)) |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 3250 | btrfs_end_transaction(trans); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3251 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3252 | |
| 3253 | if (nr_unlink) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3254 | btrfs_debug(fs_info, "unlinked %d orphans", nr_unlink); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3255 | |
| 3256 | out: |
| 3257 | if (ret) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3258 | btrfs_err(fs_info, "could not do orphan cleanup %d", ret); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3259 | btrfs_free_path(path); |
| 3260 | return ret; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3261 | } |
| 3262 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3263 | /* |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3264 | * very simple check to peek ahead in the leaf looking for xattrs. If we |
| 3265 | * don't find any xattrs, we know there can't be any acls. |
| 3266 | * |
| 3267 | * slot is the slot the inode is in, objectid is the objectid of the inode |
| 3268 | */ |
| 3269 | 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] | 3270 | int slot, u64 objectid, |
| 3271 | int *first_xattr_slot) |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3272 | { |
| 3273 | u32 nritems = btrfs_header_nritems(leaf); |
| 3274 | struct btrfs_key found_key; |
Josef Bacik | f23b5a5 | 2013-06-19 10:16:26 -0400 | [diff] [blame] | 3275 | static u64 xattr_access = 0; |
| 3276 | static u64 xattr_default = 0; |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3277 | int scanned = 0; |
| 3278 | |
Josef Bacik | f23b5a5 | 2013-06-19 10:16:26 -0400 | [diff] [blame] | 3279 | if (!xattr_access) { |
Andreas Gruenbacher | 97d7929 | 2015-12-02 14:44:35 +0100 | [diff] [blame] | 3280 | xattr_access = btrfs_name_hash(XATTR_NAME_POSIX_ACL_ACCESS, |
| 3281 | strlen(XATTR_NAME_POSIX_ACL_ACCESS)); |
| 3282 | xattr_default = btrfs_name_hash(XATTR_NAME_POSIX_ACL_DEFAULT, |
| 3283 | strlen(XATTR_NAME_POSIX_ACL_DEFAULT)); |
Josef Bacik | f23b5a5 | 2013-06-19 10:16:26 -0400 | [diff] [blame] | 3284 | } |
| 3285 | |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3286 | slot++; |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 3287 | *first_xattr_slot = -1; |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3288 | while (slot < nritems) { |
| 3289 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
| 3290 | |
| 3291 | /* we found a different objectid, there must not be acls */ |
| 3292 | if (found_key.objectid != objectid) |
| 3293 | return 0; |
| 3294 | |
| 3295 | /* we found an xattr, assume we've got an acl */ |
Josef Bacik | f23b5a5 | 2013-06-19 10:16:26 -0400 | [diff] [blame] | 3296 | if (found_key.type == BTRFS_XATTR_ITEM_KEY) { |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 3297 | if (*first_xattr_slot == -1) |
| 3298 | *first_xattr_slot = slot; |
Josef Bacik | f23b5a5 | 2013-06-19 10:16:26 -0400 | [diff] [blame] | 3299 | if (found_key.offset == xattr_access || |
| 3300 | found_key.offset == xattr_default) |
| 3301 | return 1; |
| 3302 | } |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3303 | |
| 3304 | /* |
| 3305 | * we found a key greater than an xattr key, there can't |
| 3306 | * be any acls later on |
| 3307 | */ |
| 3308 | if (found_key.type > BTRFS_XATTR_ITEM_KEY) |
| 3309 | return 0; |
| 3310 | |
| 3311 | slot++; |
| 3312 | scanned++; |
| 3313 | |
| 3314 | /* |
| 3315 | * it goes inode, inode backrefs, xattrs, extents, |
| 3316 | * so if there are a ton of hard links to an inode there can |
| 3317 | * be a lot of backrefs. Don't waste time searching too hard, |
| 3318 | * this is just an optimization |
| 3319 | */ |
| 3320 | if (scanned >= 8) |
| 3321 | break; |
| 3322 | } |
| 3323 | /* we hit the end of the leaf before we found an xattr or |
| 3324 | * something larger than an xattr. We have to assume the inode |
| 3325 | * has acls |
| 3326 | */ |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 3327 | if (*first_xattr_slot == -1) |
| 3328 | *first_xattr_slot = slot; |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3329 | return 1; |
| 3330 | } |
| 3331 | |
| 3332 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3333 | * read an inode from the btree into the in-memory inode |
| 3334 | */ |
Filipe Manana | 4222ea7 | 2018-10-24 10:13:03 +0100 | [diff] [blame] | 3335 | static int btrfs_read_locked_inode(struct inode *inode, |
| 3336 | struct btrfs_path *in_path) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3337 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3338 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Filipe Manana | 4222ea7 | 2018-10-24 10:13:03 +0100 | [diff] [blame] | 3339 | struct btrfs_path *path = in_path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3340 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3341 | struct btrfs_inode_item *inode_item; |
| 3342 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 3343 | struct btrfs_key location; |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 3344 | unsigned long ptr; |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3345 | int maybe_acls; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 3346 | u32 rdev; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3347 | int ret; |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 3348 | bool filled = false; |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 3349 | int first_xattr_slot; |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 3350 | |
| 3351 | ret = btrfs_fill_inode(inode, &rdev); |
| 3352 | if (!ret) |
| 3353 | filled = true; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3354 | |
Filipe Manana | 4222ea7 | 2018-10-24 10:13:03 +0100 | [diff] [blame] | 3355 | if (!path) { |
| 3356 | path = btrfs_alloc_path(); |
| 3357 | if (!path) |
| 3358 | return -ENOMEM; |
| 3359 | } |
Mark Fasheh | 1748f84 | 2011-07-12 11:25:31 -0700 | [diff] [blame] | 3360 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3361 | memcpy(&location, &BTRFS_I(inode)->location, sizeof(location)); |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 3362 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3363 | ret = btrfs_lookup_inode(NULL, root, path, &location, 0); |
Filipe Manana | 6771089 | 2016-06-06 11:51:25 +0100 | [diff] [blame] | 3364 | if (ret) { |
Filipe Manana | 4222ea7 | 2018-10-24 10:13:03 +0100 | [diff] [blame] | 3365 | if (path != in_path) |
| 3366 | btrfs_free_path(path); |
Al Viro | f5b3a41 | 2018-07-29 23:04:51 +0100 | [diff] [blame] | 3367 | return ret; |
Filipe Manana | 6771089 | 2016-06-06 11:51:25 +0100 | [diff] [blame] | 3368 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3369 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3370 | leaf = path->nodes[0]; |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 3371 | |
| 3372 | if (filled) |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 3373 | goto cache_index; |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 3374 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3375 | inode_item = btrfs_item_ptr(leaf, path->slots[0], |
| 3376 | struct btrfs_inode_item); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3377 | inode->i_mode = btrfs_inode_mode(leaf, inode_item); |
Miklos Szeredi | bfe8684 | 2011-10-28 14:13:29 +0200 | [diff] [blame] | 3378 | set_nlink(inode, btrfs_inode_nlink(leaf, inode_item)); |
Eric W. Biederman | 2f2f43d | 2012-02-10 11:05:07 -0800 | [diff] [blame] | 3379 | i_uid_write(inode, btrfs_inode_uid(leaf, inode_item)); |
| 3380 | i_gid_write(inode, btrfs_inode_gid(leaf, inode_item)); |
Nikolay Borisov | 6ef06d2 | 2017-02-20 13:50:34 +0200 | [diff] [blame] | 3381 | 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] | 3382 | btrfs_inode_set_file_extent_range(BTRFS_I(inode), 0, |
| 3383 | round_up(i_size_read(inode), fs_info->sectorsize)); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3384 | |
David Sterba | a937b97 | 2014-12-12 17:39:12 +0100 | [diff] [blame] | 3385 | inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->atime); |
| 3386 | inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->atime); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3387 | |
David Sterba | a937b97 | 2014-12-12 17:39:12 +0100 | [diff] [blame] | 3388 | inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->mtime); |
| 3389 | inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->mtime); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3390 | |
David Sterba | a937b97 | 2014-12-12 17:39:12 +0100 | [diff] [blame] | 3391 | inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->ctime); |
| 3392 | inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->ctime); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3393 | |
chandan r | 9cc97d6 | 2012-07-04 12:48:07 +0530 | [diff] [blame] | 3394 | BTRFS_I(inode)->i_otime.tv_sec = |
| 3395 | btrfs_timespec_sec(leaf, &inode_item->otime); |
| 3396 | BTRFS_I(inode)->i_otime.tv_nsec = |
| 3397 | btrfs_timespec_nsec(leaf, &inode_item->otime); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3398 | |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 3399 | inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item)); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3400 | BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item); |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 3401 | BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item); |
| 3402 | |
Jeff Layton | c7f88c4 | 2017-12-11 06:35:12 -0500 | [diff] [blame] | 3403 | inode_set_iversion_queried(inode, |
| 3404 | btrfs_inode_sequence(leaf, inode_item)); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3405 | inode->i_generation = BTRFS_I(inode)->generation; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 3406 | inode->i_rdev = 0; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3407 | rdev = btrfs_inode_rdev(leaf, inode_item); |
| 3408 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 3409 | BTRFS_I(inode)->index_cnt = (u64)-1; |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 3410 | BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item); |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 3411 | |
| 3412 | cache_index: |
Yang Dongsheng | 6e17d30 | 2015-04-09 12:08:43 +0800 | [diff] [blame] | 3413 | /* |
| 3414 | * If we were modified in the current generation and evicted from memory |
| 3415 | * and then re-read we need to do a full sync since we don't have any |
| 3416 | * idea about which extents were modified before we were evicted from |
| 3417 | * cache. |
| 3418 | * |
| 3419 | * This is required for both inode re-read from disk and delayed inode |
| 3420 | * in delayed_nodes_tree. |
| 3421 | */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3422 | if (BTRFS_I(inode)->last_trans == fs_info->generation) |
Yang Dongsheng | 6e17d30 | 2015-04-09 12:08:43 +0800 | [diff] [blame] | 3423 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, |
| 3424 | &BTRFS_I(inode)->runtime_flags); |
| 3425 | |
Filipe Manana | bde6c24 | 2015-07-24 00:00:19 +0100 | [diff] [blame] | 3426 | /* |
| 3427 | * We don't persist the id of the transaction where an unlink operation |
| 3428 | * against the inode was last made. So here we assume the inode might |
| 3429 | * have been evicted, and therefore the exact value of last_unlink_trans |
| 3430 | * lost, and set it to last_trans to avoid metadata inconsistencies |
| 3431 | * between the inode and its parent if the inode is fsync'ed and the log |
| 3432 | * replayed. For example, in the scenario: |
| 3433 | * |
| 3434 | * touch mydir/foo |
| 3435 | * ln mydir/foo mydir/bar |
| 3436 | * sync |
| 3437 | * unlink mydir/bar |
| 3438 | * echo 2 > /proc/sys/vm/drop_caches # evicts inode |
| 3439 | * xfs_io -c fsync mydir/foo |
| 3440 | * <power failure> |
| 3441 | * mount fs, triggers fsync log replay |
| 3442 | * |
| 3443 | * We must make sure that when we fsync our inode foo we also log its |
| 3444 | * parent inode, otherwise after log replay the parent still has the |
| 3445 | * dentry with the "bar" name but our inode foo has a link count of 1 |
| 3446 | * and doesn't have an inode ref with the name "bar" anymore. |
| 3447 | * |
| 3448 | * Setting last_unlink_trans to last_trans is a pessimistic approach, |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 3449 | * but it guarantees correctness at the expense of occasional full |
Filipe Manana | bde6c24 | 2015-07-24 00:00:19 +0100 | [diff] [blame] | 3450 | * transaction commits on fsync if our inode is a directory, or if our |
| 3451 | * inode is not a directory, logging its parent unnecessarily. |
| 3452 | */ |
| 3453 | BTRFS_I(inode)->last_unlink_trans = BTRFS_I(inode)->last_trans; |
| 3454 | |
Filipe Manana | 3ebac17 | 2020-07-15 12:30:43 +0100 | [diff] [blame] | 3455 | /* |
| 3456 | * Same logic as for last_unlink_trans. We don't persist the generation |
| 3457 | * of the last transaction where this inode was used for a reflink |
| 3458 | * operation, so after eviction and reloading the inode we must be |
| 3459 | * pessimistic and assume the last transaction that modified the inode. |
| 3460 | */ |
| 3461 | BTRFS_I(inode)->last_reflink_trans = BTRFS_I(inode)->last_trans; |
| 3462 | |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 3463 | path->slots[0]++; |
| 3464 | if (inode->i_nlink != 1 || |
| 3465 | path->slots[0] >= btrfs_header_nritems(leaf)) |
| 3466 | goto cache_acl; |
| 3467 | |
| 3468 | btrfs_item_key_to_cpu(leaf, &location, path->slots[0]); |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 3469 | if (location.objectid != btrfs_ino(BTRFS_I(inode))) |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 3470 | goto cache_acl; |
| 3471 | |
| 3472 | ptr = btrfs_item_ptr_offset(leaf, path->slots[0]); |
| 3473 | if (location.type == BTRFS_INODE_REF_KEY) { |
| 3474 | struct btrfs_inode_ref *ref; |
| 3475 | |
| 3476 | ref = (struct btrfs_inode_ref *)ptr; |
| 3477 | BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref); |
| 3478 | } else if (location.type == BTRFS_INODE_EXTREF_KEY) { |
| 3479 | struct btrfs_inode_extref *extref; |
| 3480 | |
| 3481 | extref = (struct btrfs_inode_extref *)ptr; |
| 3482 | BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf, |
| 3483 | extref); |
| 3484 | } |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 3485 | cache_acl: |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3486 | /* |
| 3487 | * try to precache a NULL acl entry for files that don't have |
| 3488 | * any xattrs or acls |
| 3489 | */ |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3490 | maybe_acls = acls_after_inode_item(leaf, path->slots[0], |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 3491 | btrfs_ino(BTRFS_I(inode)), &first_xattr_slot); |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 3492 | if (first_xattr_slot != -1) { |
| 3493 | path->slots[0] = first_xattr_slot; |
| 3494 | ret = btrfs_load_inode_props(inode, path); |
| 3495 | if (ret) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3496 | btrfs_err(fs_info, |
David Sterba | 351fd35 | 2014-05-15 16:48:20 +0200 | [diff] [blame] | 3497 | "error loading props for ino %llu (root %llu): %d", |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 3498 | btrfs_ino(BTRFS_I(inode)), |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 3499 | root->root_key.objectid, ret); |
| 3500 | } |
Filipe Manana | 4222ea7 | 2018-10-24 10:13:03 +0100 | [diff] [blame] | 3501 | if (path != in_path) |
| 3502 | btrfs_free_path(path); |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 3503 | |
Al Viro | 72c0490 | 2009-06-24 16:58:48 -0400 | [diff] [blame] | 3504 | if (!maybe_acls) |
| 3505 | cache_no_acl(inode); |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3506 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3507 | switch (inode->i_mode & S_IFMT) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3508 | case S_IFREG: |
| 3509 | inode->i_mapping->a_ops = &btrfs_aops; |
| 3510 | inode->i_fop = &btrfs_file_operations; |
| 3511 | inode->i_op = &btrfs_file_inode_operations; |
| 3512 | break; |
| 3513 | case S_IFDIR: |
| 3514 | inode->i_fop = &btrfs_dir_file_operations; |
Omar Sandoval | 67ade05 | 2017-01-25 17:06:38 -0800 | [diff] [blame] | 3515 | inode->i_op = &btrfs_dir_inode_operations; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3516 | break; |
| 3517 | case S_IFLNK: |
| 3518 | inode->i_op = &btrfs_symlink_inode_operations; |
Al Viro | 21fc61c | 2015-11-17 01:07:57 -0500 | [diff] [blame] | 3519 | inode_nohighmem(inode); |
Omar Sandoval | 4779cc0 | 2018-09-24 15:16:55 -0700 | [diff] [blame] | 3520 | inode->i_mapping->a_ops = &btrfs_aops; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3521 | break; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 3522 | default: |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 3523 | inode->i_op = &btrfs_special_inode_operations; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 3524 | init_special_inode(inode, inode->i_mode, rdev); |
| 3525 | break; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3526 | } |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 3527 | |
David Sterba | 7b6a221 | 2018-03-26 18:40:21 +0200 | [diff] [blame] | 3528 | btrfs_sync_inode_flags_to_i_flags(inode); |
Filipe Manana | 6771089 | 2016-06-06 11:51:25 +0100 | [diff] [blame] | 3529 | return 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3530 | } |
| 3531 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3532 | /* |
| 3533 | * given a leaf and an inode, copy the inode fields into the leaf |
| 3534 | */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3535 | static void fill_inode_item(struct btrfs_trans_handle *trans, |
| 3536 | struct extent_buffer *leaf, |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3537 | struct btrfs_inode_item *item, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3538 | struct inode *inode) |
| 3539 | { |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3540 | struct btrfs_map_token token; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3541 | |
David Sterba | c82f823 | 2019-08-09 17:48:21 +0200 | [diff] [blame] | 3542 | btrfs_init_map_token(&token, leaf); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3543 | |
David Sterba | cc4c13d | 2020-04-29 02:15:56 +0200 | [diff] [blame] | 3544 | btrfs_set_token_inode_uid(&token, item, i_uid_read(inode)); |
| 3545 | btrfs_set_token_inode_gid(&token, item, i_gid_read(inode)); |
| 3546 | btrfs_set_token_inode_size(&token, item, BTRFS_I(inode)->disk_i_size); |
| 3547 | btrfs_set_token_inode_mode(&token, item, inode->i_mode); |
| 3548 | btrfs_set_token_inode_nlink(&token, item, inode->i_nlink); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3549 | |
David Sterba | cc4c13d | 2020-04-29 02:15:56 +0200 | [diff] [blame] | 3550 | btrfs_set_token_timespec_sec(&token, &item->atime, |
| 3551 | inode->i_atime.tv_sec); |
| 3552 | btrfs_set_token_timespec_nsec(&token, &item->atime, |
| 3553 | inode->i_atime.tv_nsec); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3554 | |
David Sterba | cc4c13d | 2020-04-29 02:15:56 +0200 | [diff] [blame] | 3555 | btrfs_set_token_timespec_sec(&token, &item->mtime, |
| 3556 | inode->i_mtime.tv_sec); |
| 3557 | btrfs_set_token_timespec_nsec(&token, &item->mtime, |
| 3558 | inode->i_mtime.tv_nsec); |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3559 | |
David Sterba | cc4c13d | 2020-04-29 02:15:56 +0200 | [diff] [blame] | 3560 | btrfs_set_token_timespec_sec(&token, &item->ctime, |
| 3561 | inode->i_ctime.tv_sec); |
| 3562 | btrfs_set_token_timespec_nsec(&token, &item->ctime, |
| 3563 | inode->i_ctime.tv_nsec); |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3564 | |
David Sterba | cc4c13d | 2020-04-29 02:15:56 +0200 | [diff] [blame] | 3565 | btrfs_set_token_timespec_sec(&token, &item->otime, |
| 3566 | BTRFS_I(inode)->i_otime.tv_sec); |
| 3567 | btrfs_set_token_timespec_nsec(&token, &item->otime, |
| 3568 | BTRFS_I(inode)->i_otime.tv_nsec); |
chandan r | 9cc97d6 | 2012-07-04 12:48:07 +0530 | [diff] [blame] | 3569 | |
David Sterba | cc4c13d | 2020-04-29 02:15:56 +0200 | [diff] [blame] | 3570 | btrfs_set_token_inode_nbytes(&token, item, inode_get_bytes(inode)); |
| 3571 | btrfs_set_token_inode_generation(&token, item, |
| 3572 | BTRFS_I(inode)->generation); |
| 3573 | btrfs_set_token_inode_sequence(&token, item, inode_peek_iversion(inode)); |
| 3574 | btrfs_set_token_inode_transid(&token, item, trans->transid); |
| 3575 | btrfs_set_token_inode_rdev(&token, item, inode->i_rdev); |
| 3576 | btrfs_set_token_inode_flags(&token, item, BTRFS_I(inode)->flags); |
| 3577 | btrfs_set_token_inode_block_group(&token, item, 0); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3578 | } |
| 3579 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3580 | /* |
| 3581 | * copy everything in the in-memory inode into the btree. |
| 3582 | */ |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 3583 | static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3584 | struct btrfs_root *root, struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3585 | { |
| 3586 | struct btrfs_inode_item *inode_item; |
| 3587 | struct btrfs_path *path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3588 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3589 | int ret; |
| 3590 | |
| 3591 | path = btrfs_alloc_path(); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 3592 | if (!path) |
| 3593 | return -ENOMEM; |
| 3594 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 3595 | ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location, |
| 3596 | 1); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3597 | if (ret) { |
| 3598 | if (ret > 0) |
| 3599 | ret = -ENOENT; |
| 3600 | goto failed; |
| 3601 | } |
| 3602 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3603 | leaf = path->nodes[0]; |
| 3604 | inode_item = btrfs_item_ptr(leaf, path->slots[0], |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 3605 | struct btrfs_inode_item); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3606 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3607 | fill_inode_item(trans, leaf, inode_item, inode); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3608 | btrfs_mark_buffer_dirty(leaf); |
Nikolay Borisov | d909441 | 2020-06-05 10:41:13 +0300 | [diff] [blame] | 3609 | btrfs_set_inode_last_trans(trans, BTRFS_I(inode)); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3610 | ret = 0; |
| 3611 | failed: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3612 | btrfs_free_path(path); |
| 3613 | return ret; |
| 3614 | } |
| 3615 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3616 | /* |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 3617 | * copy everything in the in-memory inode into the btree. |
| 3618 | */ |
| 3619 | noinline int btrfs_update_inode(struct btrfs_trans_handle *trans, |
| 3620 | struct btrfs_root *root, struct inode *inode) |
| 3621 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3622 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 3623 | int ret; |
| 3624 | |
| 3625 | /* |
| 3626 | * If the inode is a free space inode, we can deadlock during commit |
| 3627 | * if we put it into the delayed code. |
| 3628 | * |
| 3629 | * The data relocation inode should also be directly updated |
| 3630 | * without delay |
| 3631 | */ |
Nikolay Borisov | 70ddc55 | 2017-02-20 13:50:35 +0200 | [diff] [blame] | 3632 | if (!btrfs_is_free_space_inode(BTRFS_I(inode)) |
Josef Bacik | 1d52c78 | 2014-09-18 11:30:44 -0400 | [diff] [blame] | 3633 | && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3634 | && !test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) { |
Alexander Block | 8ea05e3 | 2012-07-25 17:35:53 +0200 | [diff] [blame] | 3635 | btrfs_update_root_times(trans, root); |
| 3636 | |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 3637 | ret = btrfs_delayed_update_inode(trans, root, inode); |
| 3638 | if (!ret) |
Nikolay Borisov | d909441 | 2020-06-05 10:41:13 +0300 | [diff] [blame] | 3639 | btrfs_set_inode_last_trans(trans, BTRFS_I(inode)); |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 3640 | return ret; |
| 3641 | } |
| 3642 | |
| 3643 | return btrfs_update_inode_item(trans, root, inode); |
| 3644 | } |
| 3645 | |
Josef Bacik | be6aef6 | 2012-10-22 15:43:12 -0400 | [diff] [blame] | 3646 | noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans, |
| 3647 | struct btrfs_root *root, |
| 3648 | struct inode *inode) |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 3649 | { |
| 3650 | int ret; |
| 3651 | |
| 3652 | ret = btrfs_update_inode(trans, root, inode); |
| 3653 | if (ret == -ENOSPC) |
| 3654 | return btrfs_update_inode_item(trans, root, inode); |
| 3655 | return ret; |
| 3656 | } |
| 3657 | |
| 3658 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3659 | * unlink helper that gets used here in inode.c and in the tree logging |
| 3660 | * recovery code. It remove a link in a directory with a given name, and |
| 3661 | * also drops the back refs in the inode to the directory |
| 3662 | */ |
Al Viro | 9298679 | 2011-03-04 17:14:37 +0000 | [diff] [blame] | 3663 | static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans, |
| 3664 | struct btrfs_root *root, |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 3665 | struct btrfs_inode *dir, |
| 3666 | struct btrfs_inode *inode, |
Al Viro | 9298679 | 2011-03-04 17:14:37 +0000 | [diff] [blame] | 3667 | const char *name, int name_len) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3668 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3669 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3670 | struct btrfs_path *path; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3671 | int ret = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3672 | struct btrfs_dir_item *di; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 3673 | u64 index; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3674 | u64 ino = btrfs_ino(inode); |
| 3675 | u64 dir_ino = btrfs_ino(dir); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3676 | |
| 3677 | path = btrfs_alloc_path(); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3678 | if (!path) { |
| 3679 | ret = -ENOMEM; |
Tsutomu Itoh | 554233a | 2011-02-03 03:16:25 +0000 | [diff] [blame] | 3680 | goto out; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3681 | } |
| 3682 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3683 | di = btrfs_lookup_dir_item(trans, root, path, dir_ino, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3684 | name, name_len, -1); |
Liu Bo | 3cf5068 | 2018-09-12 06:06:26 +0800 | [diff] [blame] | 3685 | if (IS_ERR_OR_NULL(di)) { |
| 3686 | ret = di ? PTR_ERR(di) : -ENOENT; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3687 | goto err; |
| 3688 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3689 | ret = btrfs_delete_one_dir_name(trans, root, path, di); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3690 | if (ret) |
| 3691 | goto err; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3692 | btrfs_release_path(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3693 | |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 3694 | /* |
| 3695 | * If we don't have dir index, we have to get it by looking up |
| 3696 | * the inode ref, since we get the inode ref, remove it directly, |
| 3697 | * it is unnecessary to do delayed deletion. |
| 3698 | * |
| 3699 | * But if we have dir index, needn't search inode ref to get it. |
| 3700 | * Since the inode ref is close to the inode item, it is better |
| 3701 | * that we delay to delete it, and just do this deletion when |
| 3702 | * we update the inode item. |
| 3703 | */ |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 3704 | if (inode->dir_index) { |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 3705 | ret = btrfs_delayed_delete_inode_ref(inode); |
| 3706 | if (!ret) { |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 3707 | index = inode->dir_index; |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 3708 | goto skip_backref; |
| 3709 | } |
| 3710 | } |
| 3711 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3712 | ret = btrfs_del_inode_ref(trans, root, name, name_len, ino, |
| 3713 | dir_ino, &index); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 3714 | if (ret) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3715 | btrfs_info(fs_info, |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 3716 | "failed to delete reference to %.*s, inode %llu parent %llu", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 3717 | name_len, name, ino, dir_ino); |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3718 | btrfs_abort_transaction(trans, ret); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 3719 | goto err; |
| 3720 | } |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 3721 | skip_backref: |
Lu Fengqi | 9add294 | 2018-08-01 11:32:26 +0800 | [diff] [blame] | 3722 | ret = btrfs_delete_delayed_dir_index(trans, dir, index); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3723 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3724 | btrfs_abort_transaction(trans, ret); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3725 | goto err; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3726 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3727 | |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 3728 | ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len, inode, |
| 3729 | dir_ino); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3730 | if (ret != 0 && ret != -ENOENT) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3731 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3732 | goto err; |
| 3733 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3734 | |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 3735 | ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len, dir, |
| 3736 | index); |
Chris Mason | 6418c96 | 2010-10-30 07:34:24 -0400 | [diff] [blame] | 3737 | if (ret == -ENOENT) |
| 3738 | ret = 0; |
Zach Brown | d4e3991 | 2013-04-02 21:02:16 +0000 | [diff] [blame] | 3739 | else if (ret) |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3740 | btrfs_abort_transaction(trans, ret); |
Josef Bacik | 63611e7 | 2019-06-18 10:59:18 -0400 | [diff] [blame] | 3741 | |
| 3742 | /* |
| 3743 | * If we have a pending delayed iput we could end up with the final iput |
| 3744 | * being run in btrfs-cleaner context. If we have enough of these built |
| 3745 | * up we can end up burning a lot of time in btrfs-cleaner without any |
| 3746 | * way to throttle the unlinks. Since we're currently holding a ref on |
| 3747 | * the inode we can run the delayed iput here without any issues as the |
| 3748 | * final iput won't be done until after we drop the ref we're currently |
| 3749 | * holding. |
| 3750 | */ |
| 3751 | btrfs_run_delayed_iput(fs_info, inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3752 | err: |
| 3753 | btrfs_free_path(path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3754 | if (ret) |
| 3755 | goto out; |
| 3756 | |
Nikolay Borisov | 6ef06d2 | 2017-02-20 13:50:34 +0200 | [diff] [blame] | 3757 | 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] | 3758 | inode_inc_iversion(&inode->vfs_inode); |
| 3759 | inode_inc_iversion(&dir->vfs_inode); |
| 3760 | inode->vfs_inode.i_ctime = dir->vfs_inode.i_mtime = |
| 3761 | dir->vfs_inode.i_ctime = current_time(&inode->vfs_inode); |
| 3762 | ret = btrfs_update_inode(trans, root, &dir->vfs_inode); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3763 | out: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3764 | return ret; |
| 3765 | } |
| 3766 | |
Al Viro | 9298679 | 2011-03-04 17:14:37 +0000 | [diff] [blame] | 3767 | int btrfs_unlink_inode(struct btrfs_trans_handle *trans, |
| 3768 | struct btrfs_root *root, |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 3769 | struct btrfs_inode *dir, struct btrfs_inode *inode, |
Al Viro | 9298679 | 2011-03-04 17:14:37 +0000 | [diff] [blame] | 3770 | const char *name, int name_len) |
| 3771 | { |
| 3772 | int ret; |
| 3773 | ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len); |
| 3774 | if (!ret) { |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 3775 | drop_nlink(&inode->vfs_inode); |
| 3776 | ret = btrfs_update_inode(trans, root, &inode->vfs_inode); |
Al Viro | 9298679 | 2011-03-04 17:14:37 +0000 | [diff] [blame] | 3777 | } |
| 3778 | return ret; |
| 3779 | } |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3780 | |
| 3781 | /* |
| 3782 | * helper to start transaction for unlink and rmdir. |
| 3783 | * |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 3784 | * unlink and rmdir are special in btrfs, they do not always free space, so |
| 3785 | * if we cannot make our reservations the normal way try and see if there is |
| 3786 | * plenty of slack room in the global reserve to migrate, otherwise we cannot |
| 3787 | * allow the unlink to occur. |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3788 | */ |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 3789 | static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir) |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3790 | { |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3791 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3792 | |
Josef Bacik | e70bea5 | 2011-10-11 14:18:24 -0400 | [diff] [blame] | 3793 | /* |
| 3794 | * 1 for the possible orphan item |
| 3795 | * 1 for the dir item |
| 3796 | * 1 for the dir index |
| 3797 | * 1 for the inode ref |
Josef Bacik | e70bea5 | 2011-10-11 14:18:24 -0400 | [diff] [blame] | 3798 | * 1 for the inode |
| 3799 | */ |
Josef Bacik | 7f9fe61 | 2020-03-13 15:58:05 -0400 | [diff] [blame] | 3800 | return btrfs_start_transaction_fallback_global_rsv(root, 5); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3801 | } |
| 3802 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3803 | static int btrfs_unlink(struct inode *dir, struct dentry *dentry) |
| 3804 | { |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3805 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3806 | struct btrfs_trans_handle *trans; |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 3807 | struct inode *inode = d_inode(dentry); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3808 | int ret; |
| 3809 | |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 3810 | trans = __unlink_start_trans(dir); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3811 | if (IS_ERR(trans)) |
Josef Bacik | 5df6a9f | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 3812 | return PTR_ERR(trans); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3813 | |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 3814 | btrfs_record_unlink_dir(trans, BTRFS_I(dir), BTRFS_I(d_inode(dentry)), |
| 3815 | 0); |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 3816 | |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 3817 | ret = btrfs_unlink_inode(trans, root, BTRFS_I(dir), |
| 3818 | BTRFS_I(d_inode(dentry)), dentry->d_name.name, |
| 3819 | dentry->d_name.len); |
Tsutomu Itoh | b532402 | 2011-07-19 07:27:20 +0000 | [diff] [blame] | 3820 | if (ret) |
| 3821 | goto out; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3822 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3823 | if (inode->i_nlink == 0) { |
Nikolay Borisov | 73f2e54 | 2017-02-20 13:50:59 +0200 | [diff] [blame] | 3824 | ret = btrfs_orphan_add(trans, BTRFS_I(inode)); |
Tsutomu Itoh | b532402 | 2011-07-19 07:27:20 +0000 | [diff] [blame] | 3825 | if (ret) |
| 3826 | goto out; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3827 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3828 | |
Tsutomu Itoh | b532402 | 2011-07-19 07:27:20 +0000 | [diff] [blame] | 3829 | out: |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 3830 | btrfs_end_transaction(trans); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3831 | btrfs_btree_balance_dirty(root->fs_info); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3832 | return ret; |
| 3833 | } |
| 3834 | |
Misono Tomohiro | f60a236 | 2018-04-18 11:34:52 +0900 | [diff] [blame] | 3835 | static int btrfs_unlink_subvol(struct btrfs_trans_handle *trans, |
Josef Bacik | 045d396 | 2019-12-18 17:20:27 -0500 | [diff] [blame] | 3836 | struct inode *dir, struct dentry *dentry) |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3837 | { |
Lu Fengqi | 401b3b1 | 2018-08-01 11:32:30 +0800 | [diff] [blame] | 3838 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Josef Bacik | 045d396 | 2019-12-18 17:20:27 -0500 | [diff] [blame] | 3839 | struct btrfs_inode *inode = BTRFS_I(d_inode(dentry)); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3840 | struct btrfs_path *path; |
| 3841 | struct extent_buffer *leaf; |
| 3842 | struct btrfs_dir_item *di; |
| 3843 | struct btrfs_key key; |
Josef Bacik | 045d396 | 2019-12-18 17:20:27 -0500 | [diff] [blame] | 3844 | const char *name = dentry->d_name.name; |
| 3845 | int name_len = dentry->d_name.len; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3846 | u64 index; |
| 3847 | int ret; |
Josef Bacik | 045d396 | 2019-12-18 17:20:27 -0500 | [diff] [blame] | 3848 | u64 objectid; |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 3849 | u64 dir_ino = btrfs_ino(BTRFS_I(dir)); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3850 | |
Josef Bacik | 045d396 | 2019-12-18 17:20:27 -0500 | [diff] [blame] | 3851 | if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID) { |
| 3852 | objectid = inode->root->root_key.objectid; |
| 3853 | } else if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) { |
| 3854 | objectid = inode->location.objectid; |
| 3855 | } else { |
| 3856 | WARN_ON(1); |
| 3857 | return -EINVAL; |
| 3858 | } |
| 3859 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3860 | path = btrfs_alloc_path(); |
| 3861 | if (!path) |
| 3862 | return -ENOMEM; |
| 3863 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3864 | di = btrfs_lookup_dir_item(trans, root, path, dir_ino, |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3865 | name, name_len, -1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3866 | if (IS_ERR_OR_NULL(di)) { |
Liu Bo | 3cf5068 | 2018-09-12 06:06:26 +0800 | [diff] [blame] | 3867 | ret = di ? PTR_ERR(di) : -ENOENT; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3868 | goto out; |
| 3869 | } |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3870 | |
| 3871 | leaf = path->nodes[0]; |
| 3872 | btrfs_dir_item_key_to_cpu(leaf, di, &key); |
| 3873 | WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid); |
| 3874 | ret = btrfs_delete_one_dir_name(trans, root, path, di); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3875 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3876 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3877 | goto out; |
| 3878 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3879 | btrfs_release_path(path); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3880 | |
Josef Bacik | d49d328 | 2019-12-18 17:20:28 -0500 | [diff] [blame] | 3881 | /* |
| 3882 | * This is a placeholder inode for a subvolume we didn't have a |
| 3883 | * reference to at the time of the snapshot creation. In the meantime |
| 3884 | * we could have renamed the real subvol link into our snapshot, so |
| 3885 | * depending on btrfs_del_root_ref to return -ENOENT here is incorret. |
| 3886 | * Instead simply lookup the dir_index_item for this entry so we can |
| 3887 | * remove it. Otherwise we know we have a ref to the root and we can |
| 3888 | * call btrfs_del_root_ref, and it _shouldn't_ fail. |
| 3889 | */ |
| 3890 | if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) { |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3891 | di = btrfs_search_dir_index_item(root, path, dir_ino, |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3892 | name, name_len); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3893 | if (IS_ERR_OR_NULL(di)) { |
| 3894 | if (!di) |
| 3895 | ret = -ENOENT; |
| 3896 | else |
| 3897 | ret = PTR_ERR(di); |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3898 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3899 | goto out; |
| 3900 | } |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3901 | |
| 3902 | leaf = path->nodes[0]; |
| 3903 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3904 | index = key.offset; |
Josef Bacik | d49d328 | 2019-12-18 17:20:28 -0500 | [diff] [blame] | 3905 | btrfs_release_path(path); |
| 3906 | } else { |
| 3907 | ret = btrfs_del_root_ref(trans, objectid, |
| 3908 | root->root_key.objectid, dir_ino, |
| 3909 | &index, name, name_len); |
| 3910 | if (ret) { |
| 3911 | btrfs_abort_transaction(trans, ret); |
| 3912 | goto out; |
| 3913 | } |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3914 | } |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3915 | |
Lu Fengqi | 9add294 | 2018-08-01 11:32:26 +0800 | [diff] [blame] | 3916 | ret = btrfs_delete_delayed_dir_index(trans, BTRFS_I(dir), index); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3917 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3918 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3919 | goto out; |
| 3920 | } |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 3921 | |
Nikolay Borisov | 6ef06d2 | 2017-02-20 13:50:34 +0200 | [diff] [blame] | 3922 | 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] | 3923 | inode_inc_iversion(dir); |
Deepa Dinamani | c2050a4 | 2016-09-14 07:48:06 -0700 | [diff] [blame] | 3924 | dir->i_mtime = dir->i_ctime = current_time(dir); |
Josef Bacik | 5a24e84 | 2012-08-08 10:12:59 -0600 | [diff] [blame] | 3925 | ret = btrfs_update_inode_fallback(trans, root, dir); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3926 | if (ret) |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3927 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3928 | out: |
Josef Bacik | 71d7aed | 2011-06-14 14:24:32 -0400 | [diff] [blame] | 3929 | btrfs_free_path(path); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3930 | return ret; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3931 | } |
| 3932 | |
Misono Tomohiro | ec42f16 | 2018-04-18 11:34:13 +0900 | [diff] [blame] | 3933 | /* |
| 3934 | * Helper to check if the subvolume references other subvolumes or if it's |
| 3935 | * default. |
| 3936 | */ |
Misono Tomohiro | f60a236 | 2018-04-18 11:34:52 +0900 | [diff] [blame] | 3937 | static noinline int may_destroy_subvol(struct btrfs_root *root) |
Misono Tomohiro | ec42f16 | 2018-04-18 11:34:13 +0900 | [diff] [blame] | 3938 | { |
| 3939 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 3940 | struct btrfs_path *path; |
| 3941 | struct btrfs_dir_item *di; |
| 3942 | struct btrfs_key key; |
| 3943 | u64 dir_id; |
| 3944 | int ret; |
| 3945 | |
| 3946 | path = btrfs_alloc_path(); |
| 3947 | if (!path) |
| 3948 | return -ENOMEM; |
| 3949 | |
| 3950 | /* Make sure this root isn't set as the default subvol */ |
| 3951 | dir_id = btrfs_super_root_dir(fs_info->super_copy); |
| 3952 | di = btrfs_lookup_dir_item(NULL, fs_info->tree_root, path, |
| 3953 | dir_id, "default", 7, 0); |
| 3954 | if (di && !IS_ERR(di)) { |
| 3955 | btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key); |
| 3956 | if (key.objectid == root->root_key.objectid) { |
| 3957 | ret = -EPERM; |
| 3958 | btrfs_err(fs_info, |
| 3959 | "deleting default subvolume %llu is not allowed", |
| 3960 | key.objectid); |
| 3961 | goto out; |
| 3962 | } |
| 3963 | btrfs_release_path(path); |
| 3964 | } |
| 3965 | |
| 3966 | key.objectid = root->root_key.objectid; |
| 3967 | key.type = BTRFS_ROOT_REF_KEY; |
| 3968 | key.offset = (u64)-1; |
| 3969 | |
| 3970 | ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0); |
| 3971 | if (ret < 0) |
| 3972 | goto out; |
| 3973 | BUG_ON(ret == 0); |
| 3974 | |
| 3975 | ret = 0; |
| 3976 | if (path->slots[0] > 0) { |
| 3977 | path->slots[0]--; |
| 3978 | btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]); |
| 3979 | if (key.objectid == root->root_key.objectid && |
| 3980 | key.type == BTRFS_ROOT_REF_KEY) |
| 3981 | ret = -ENOTEMPTY; |
| 3982 | } |
| 3983 | out: |
| 3984 | btrfs_free_path(path); |
| 3985 | return ret; |
| 3986 | } |
| 3987 | |
Nikolay Borisov | 20a6800 | 2018-04-27 14:36:24 +0300 | [diff] [blame] | 3988 | /* Delete all dentries for inodes belonging to the root */ |
| 3989 | static void btrfs_prune_dentries(struct btrfs_root *root) |
| 3990 | { |
| 3991 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 3992 | struct rb_node *node; |
| 3993 | struct rb_node *prev; |
| 3994 | struct btrfs_inode *entry; |
| 3995 | struct inode *inode; |
| 3996 | u64 objectid = 0; |
| 3997 | |
| 3998 | if (!test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) |
| 3999 | WARN_ON(btrfs_root_refs(&root->root_item) != 0); |
| 4000 | |
| 4001 | spin_lock(&root->inode_lock); |
| 4002 | again: |
| 4003 | node = root->inode_tree.rb_node; |
| 4004 | prev = NULL; |
| 4005 | while (node) { |
| 4006 | prev = node; |
| 4007 | entry = rb_entry(node, struct btrfs_inode, rb_node); |
| 4008 | |
David Sterba | 3750851 | 2018-06-29 10:56:40 +0200 | [diff] [blame] | 4009 | if (objectid < btrfs_ino(entry)) |
Nikolay Borisov | 20a6800 | 2018-04-27 14:36:24 +0300 | [diff] [blame] | 4010 | node = node->rb_left; |
David Sterba | 3750851 | 2018-06-29 10:56:40 +0200 | [diff] [blame] | 4011 | else if (objectid > btrfs_ino(entry)) |
Nikolay Borisov | 20a6800 | 2018-04-27 14:36:24 +0300 | [diff] [blame] | 4012 | node = node->rb_right; |
| 4013 | else |
| 4014 | break; |
| 4015 | } |
| 4016 | if (!node) { |
| 4017 | while (prev) { |
| 4018 | entry = rb_entry(prev, struct btrfs_inode, rb_node); |
David Sterba | 3750851 | 2018-06-29 10:56:40 +0200 | [diff] [blame] | 4019 | if (objectid <= btrfs_ino(entry)) { |
Nikolay Borisov | 20a6800 | 2018-04-27 14:36:24 +0300 | [diff] [blame] | 4020 | node = prev; |
| 4021 | break; |
| 4022 | } |
| 4023 | prev = rb_next(prev); |
| 4024 | } |
| 4025 | } |
| 4026 | while (node) { |
| 4027 | entry = rb_entry(node, struct btrfs_inode, rb_node); |
David Sterba | 3750851 | 2018-06-29 10:56:40 +0200 | [diff] [blame] | 4028 | objectid = btrfs_ino(entry) + 1; |
Nikolay Borisov | 20a6800 | 2018-04-27 14:36:24 +0300 | [diff] [blame] | 4029 | inode = igrab(&entry->vfs_inode); |
| 4030 | if (inode) { |
| 4031 | spin_unlock(&root->inode_lock); |
| 4032 | if (atomic_read(&inode->i_count) > 1) |
| 4033 | d_prune_aliases(inode); |
| 4034 | /* |
| 4035 | * btrfs_drop_inode will have it removed from the inode |
| 4036 | * cache when its usage count hits zero. |
| 4037 | */ |
| 4038 | iput(inode); |
| 4039 | cond_resched(); |
| 4040 | spin_lock(&root->inode_lock); |
| 4041 | goto again; |
| 4042 | } |
| 4043 | |
| 4044 | if (cond_resched_lock(&root->inode_lock)) |
| 4045 | goto again; |
| 4046 | |
| 4047 | node = rb_next(node); |
| 4048 | } |
| 4049 | spin_unlock(&root->inode_lock); |
| 4050 | } |
| 4051 | |
Misono Tomohiro | f60a236 | 2018-04-18 11:34:52 +0900 | [diff] [blame] | 4052 | int btrfs_delete_subvolume(struct inode *dir, struct dentry *dentry) |
| 4053 | { |
| 4054 | struct btrfs_fs_info *fs_info = btrfs_sb(dentry->d_sb); |
| 4055 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 4056 | struct inode *inode = d_inode(dentry); |
| 4057 | struct btrfs_root *dest = BTRFS_I(inode)->root; |
| 4058 | struct btrfs_trans_handle *trans; |
| 4059 | struct btrfs_block_rsv block_rsv; |
| 4060 | u64 root_flags; |
Misono Tomohiro | f60a236 | 2018-04-18 11:34:52 +0900 | [diff] [blame] | 4061 | int ret; |
| 4062 | int err; |
| 4063 | |
| 4064 | /* |
| 4065 | * Don't allow to delete a subvolume with send in progress. This is |
| 4066 | * inside the inode lock so the error handling that has to drop the bit |
| 4067 | * again is not run concurrently. |
| 4068 | */ |
| 4069 | spin_lock(&dest->root_item_lock); |
Lu Fengqi | a7176f7 | 2018-08-04 21:10:53 +0800 | [diff] [blame] | 4070 | if (dest->send_in_progress) { |
Misono Tomohiro | f60a236 | 2018-04-18 11:34:52 +0900 | [diff] [blame] | 4071 | spin_unlock(&dest->root_item_lock); |
| 4072 | btrfs_warn(fs_info, |
| 4073 | "attempt to delete subvolume %llu during send", |
| 4074 | dest->root_key.objectid); |
| 4075 | return -EPERM; |
| 4076 | } |
Lu Fengqi | a7176f7 | 2018-08-04 21:10:53 +0800 | [diff] [blame] | 4077 | root_flags = btrfs_root_flags(&dest->root_item); |
| 4078 | btrfs_set_root_flags(&dest->root_item, |
| 4079 | root_flags | BTRFS_ROOT_SUBVOL_DEAD); |
| 4080 | spin_unlock(&dest->root_item_lock); |
Misono Tomohiro | f60a236 | 2018-04-18 11:34:52 +0900 | [diff] [blame] | 4081 | |
| 4082 | down_write(&fs_info->subvol_sem); |
| 4083 | |
| 4084 | err = may_destroy_subvol(dest); |
| 4085 | if (err) |
| 4086 | goto out_up_write; |
| 4087 | |
| 4088 | btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP); |
| 4089 | /* |
| 4090 | * One for dir inode, |
| 4091 | * two for dir entries, |
| 4092 | * two for root ref/backref. |
| 4093 | */ |
Gu JinXiang | c4c129d | 2018-05-30 11:00:38 +0800 | [diff] [blame] | 4094 | err = btrfs_subvolume_reserve_metadata(root, &block_rsv, 5, true); |
Misono Tomohiro | f60a236 | 2018-04-18 11:34:52 +0900 | [diff] [blame] | 4095 | if (err) |
| 4096 | goto out_up_write; |
| 4097 | |
| 4098 | trans = btrfs_start_transaction(root, 0); |
| 4099 | if (IS_ERR(trans)) { |
| 4100 | err = PTR_ERR(trans); |
| 4101 | goto out_release; |
| 4102 | } |
| 4103 | trans->block_rsv = &block_rsv; |
| 4104 | trans->bytes_reserved = block_rsv.size; |
| 4105 | |
| 4106 | btrfs_record_snapshot_destroy(trans, BTRFS_I(dir)); |
| 4107 | |
Josef Bacik | 045d396 | 2019-12-18 17:20:27 -0500 | [diff] [blame] | 4108 | ret = btrfs_unlink_subvol(trans, dir, dentry); |
Misono Tomohiro | f60a236 | 2018-04-18 11:34:52 +0900 | [diff] [blame] | 4109 | if (ret) { |
| 4110 | err = ret; |
| 4111 | btrfs_abort_transaction(trans, ret); |
| 4112 | goto out_end_trans; |
| 4113 | } |
| 4114 | |
| 4115 | btrfs_record_root_in_trans(trans, dest); |
| 4116 | |
| 4117 | memset(&dest->root_item.drop_progress, 0, |
| 4118 | sizeof(dest->root_item.drop_progress)); |
David Sterba | c842268 | 2020-09-15 21:44:52 +0200 | [diff] [blame] | 4119 | btrfs_set_root_drop_level(&dest->root_item, 0); |
Misono Tomohiro | f60a236 | 2018-04-18 11:34:52 +0900 | [diff] [blame] | 4120 | btrfs_set_root_refs(&dest->root_item, 0); |
| 4121 | |
| 4122 | if (!test_and_set_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &dest->state)) { |
| 4123 | ret = btrfs_insert_orphan_item(trans, |
| 4124 | fs_info->tree_root, |
| 4125 | dest->root_key.objectid); |
| 4126 | if (ret) { |
| 4127 | btrfs_abort_transaction(trans, ret); |
| 4128 | err = ret; |
| 4129 | goto out_end_trans; |
| 4130 | } |
| 4131 | } |
| 4132 | |
Lu Fengqi | d195779 | 2018-05-29 15:01:54 +0800 | [diff] [blame] | 4133 | ret = btrfs_uuid_tree_remove(trans, dest->root_item.uuid, |
Misono Tomohiro | f60a236 | 2018-04-18 11:34:52 +0900 | [diff] [blame] | 4134 | BTRFS_UUID_KEY_SUBVOL, |
| 4135 | dest->root_key.objectid); |
| 4136 | if (ret && ret != -ENOENT) { |
| 4137 | btrfs_abort_transaction(trans, ret); |
| 4138 | err = ret; |
| 4139 | goto out_end_trans; |
| 4140 | } |
| 4141 | if (!btrfs_is_empty_uuid(dest->root_item.received_uuid)) { |
Lu Fengqi | d195779 | 2018-05-29 15:01:54 +0800 | [diff] [blame] | 4142 | ret = btrfs_uuid_tree_remove(trans, |
Misono Tomohiro | f60a236 | 2018-04-18 11:34:52 +0900 | [diff] [blame] | 4143 | dest->root_item.received_uuid, |
| 4144 | BTRFS_UUID_KEY_RECEIVED_SUBVOL, |
| 4145 | dest->root_key.objectid); |
| 4146 | if (ret && ret != -ENOENT) { |
| 4147 | btrfs_abort_transaction(trans, ret); |
| 4148 | err = ret; |
| 4149 | goto out_end_trans; |
| 4150 | } |
| 4151 | } |
| 4152 | |
Qu Wenruo | 082b6c9 | 2020-06-16 10:17:37 +0800 | [diff] [blame] | 4153 | free_anon_bdev(dest->anon_dev); |
| 4154 | dest->anon_dev = 0; |
Misono Tomohiro | f60a236 | 2018-04-18 11:34:52 +0900 | [diff] [blame] | 4155 | out_end_trans: |
| 4156 | trans->block_rsv = NULL; |
| 4157 | trans->bytes_reserved = 0; |
| 4158 | ret = btrfs_end_transaction(trans); |
| 4159 | if (ret && !err) |
| 4160 | err = ret; |
| 4161 | inode->i_flags |= S_DEAD; |
| 4162 | out_release: |
Qu Wenruo | e85fde5 | 2020-07-24 14:46:10 +0800 | [diff] [blame] | 4163 | btrfs_subvolume_release_metadata(root, &block_rsv); |
Misono Tomohiro | f60a236 | 2018-04-18 11:34:52 +0900 | [diff] [blame] | 4164 | out_up_write: |
| 4165 | up_write(&fs_info->subvol_sem); |
| 4166 | if (err) { |
| 4167 | spin_lock(&dest->root_item_lock); |
| 4168 | root_flags = btrfs_root_flags(&dest->root_item); |
| 4169 | btrfs_set_root_flags(&dest->root_item, |
| 4170 | root_flags & ~BTRFS_ROOT_SUBVOL_DEAD); |
| 4171 | spin_unlock(&dest->root_item_lock); |
| 4172 | } else { |
| 4173 | d_invalidate(dentry); |
Nikolay Borisov | 20a6800 | 2018-04-27 14:36:24 +0300 | [diff] [blame] | 4174 | btrfs_prune_dentries(dest); |
Misono Tomohiro | f60a236 | 2018-04-18 11:34:52 +0900 | [diff] [blame] | 4175 | ASSERT(dest->send_in_progress == 0); |
| 4176 | |
| 4177 | /* the last ref */ |
| 4178 | if (dest->ino_cache_inode) { |
| 4179 | iput(dest->ino_cache_inode); |
| 4180 | dest->ino_cache_inode = NULL; |
| 4181 | } |
| 4182 | } |
| 4183 | |
| 4184 | return err; |
| 4185 | } |
| 4186 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4187 | static int btrfs_rmdir(struct inode *dir, struct dentry *dentry) |
| 4188 | { |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 4189 | struct inode *inode = d_inode(dentry); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 4190 | int err = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4191 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4192 | struct btrfs_trans_handle *trans; |
Filipe Manana | 44f714d | 2016-06-06 16:11:13 +0100 | [diff] [blame] | 4193 | u64 last_unlink_trans; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4194 | |
David Sterba | b3ae244 | 2012-09-13 16:04:34 -0600 | [diff] [blame] | 4195 | if (inode->i_size > BTRFS_EMPTY_DIR_SIZE) |
Yan | 134d451 | 2007-10-25 15:49:25 -0400 | [diff] [blame] | 4196 | return -ENOTEMPTY; |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 4197 | if (btrfs_ino(BTRFS_I(inode)) == BTRFS_FIRST_FREE_OBJECTID) |
Misono Tomohiro | a79a464 | 2018-04-18 11:35:31 +0900 | [diff] [blame] | 4198 | return btrfs_delete_subvolume(dir, dentry); |
Yan | 134d451 | 2007-10-25 15:49:25 -0400 | [diff] [blame] | 4199 | |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 4200 | trans = __unlink_start_trans(dir); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4201 | if (IS_ERR(trans)) |
Josef Bacik | 5df6a9f | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4202 | return PTR_ERR(trans); |
Josef Bacik | 5df6a9f | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4203 | |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 4204 | if (unlikely(btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) { |
Josef Bacik | 045d396 | 2019-12-18 17:20:27 -0500 | [diff] [blame] | 4205 | err = btrfs_unlink_subvol(trans, dir, dentry); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4206 | goto out; |
| 4207 | } |
| 4208 | |
Nikolay Borisov | 73f2e54 | 2017-02-20 13:50:59 +0200 | [diff] [blame] | 4209 | err = btrfs_orphan_add(trans, BTRFS_I(inode)); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 4210 | if (err) |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4211 | goto out; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 4212 | |
Filipe Manana | 44f714d | 2016-06-06 16:11:13 +0100 | [diff] [blame] | 4213 | last_unlink_trans = BTRFS_I(inode)->last_unlink_trans; |
| 4214 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4215 | /* now the directory is empty */ |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 4216 | err = btrfs_unlink_inode(trans, root, BTRFS_I(dir), |
| 4217 | BTRFS_I(d_inode(dentry)), dentry->d_name.name, |
| 4218 | dentry->d_name.len); |
Filipe Manana | 44f714d | 2016-06-06 16:11:13 +0100 | [diff] [blame] | 4219 | if (!err) { |
Nikolay Borisov | 6ef06d2 | 2017-02-20 13:50:34 +0200 | [diff] [blame] | 4220 | btrfs_i_size_write(BTRFS_I(inode), 0); |
Filipe Manana | 44f714d | 2016-06-06 16:11:13 +0100 | [diff] [blame] | 4221 | /* |
| 4222 | * Propagate the last_unlink_trans value of the deleted dir to |
| 4223 | * its parent directory. This is to prevent an unrecoverable |
| 4224 | * log tree in the case we do something like this: |
| 4225 | * 1) create dir foo |
| 4226 | * 2) create snapshot under dir foo |
| 4227 | * 3) delete the snapshot |
| 4228 | * 4) rmdir foo |
| 4229 | * 5) mkdir foo |
| 4230 | * 6) fsync foo or some file inside foo |
| 4231 | */ |
| 4232 | if (last_unlink_trans >= trans->transid) |
| 4233 | BTRFS_I(dir)->last_unlink_trans = last_unlink_trans; |
| 4234 | } |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4235 | out: |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 4236 | btrfs_end_transaction(trans); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4237 | btrfs_btree_balance_dirty(root->fs_info); |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 4238 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4239 | return err; |
| 4240 | } |
| 4241 | |
Josef Bacik | ddfae63 | 2017-10-19 14:16:02 -0400 | [diff] [blame] | 4242 | /* |
| 4243 | * Return this if we need to call truncate_block for the last bit of the |
| 4244 | * truncate. |
| 4245 | */ |
| 4246 | #define NEED_TRUNCATE_BLOCK 1 |
Filipe Manana | 0305cd5 | 2015-10-16 12:34:25 +0100 | [diff] [blame] | 4247 | |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 4248 | /* |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4249 | * this can truncate away extent items, csum items and directory items. |
| 4250 | * 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] | 4251 | * any higher than new_size |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4252 | * |
| 4253 | * csum items that cross the new i_size are truncated to the new size |
| 4254 | * as well. |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 4255 | * |
| 4256 | * min_type is the minimum key type to truncate down to. If set to 0, this |
| 4257 | * 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] | 4258 | */ |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4259 | int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans, |
| 4260 | struct btrfs_root *root, |
| 4261 | struct inode *inode, |
| 4262 | u64 new_size, u32 min_type) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4263 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4264 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4265 | struct btrfs_path *path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4266 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4267 | struct btrfs_file_extent_item *fi; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4268 | struct btrfs_key key; |
| 4269 | struct btrfs_key found_key; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4270 | u64 extent_start = 0; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4271 | u64 extent_num_bytes = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4272 | u64 extent_offset = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4273 | u64 item_end = 0; |
Filipe Manana | c1aa457 | 2015-06-20 18:20:09 +0100 | [diff] [blame] | 4274 | u64 last_size = new_size; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4275 | u32 found_type = (u8)-1; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4276 | int found_extent; |
| 4277 | int del_item; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4278 | int pending_del_nr = 0; |
| 4279 | int pending_del_slot = 0; |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4280 | int extent_type = -1; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4281 | int ret; |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 4282 | u64 ino = btrfs_ino(BTRFS_I(inode)); |
Chris Mason | 28ed134 | 2014-12-17 09:41:04 -0800 | [diff] [blame] | 4283 | u64 bytes_deleted = 0; |
Thomas Meyer | 897ca81 | 2017-10-07 16:02:21 +0200 | [diff] [blame] | 4284 | bool be_nice = false; |
| 4285 | bool should_throttle = false; |
Filipe Manana | 28553fa | 2020-02-07 12:23:09 +0000 | [diff] [blame] | 4286 | const u64 lock_start = ALIGN_DOWN(new_size, fs_info->sectorsize); |
| 4287 | struct extent_state *cached_state = NULL; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4288 | |
| 4289 | BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4290 | |
Chris Mason | 28ed134 | 2014-12-17 09:41:04 -0800 | [diff] [blame] | 4291 | /* |
Qu Wenruo | 92a7cc4 | 2020-05-15 14:01:40 +0800 | [diff] [blame] | 4292 | * For non-free space inodes and non-shareable roots, we want to back |
| 4293 | * off from time to time. This means all inodes in subvolume roots, |
| 4294 | * reloc roots, and data reloc roots. |
Chris Mason | 28ed134 | 2014-12-17 09:41:04 -0800 | [diff] [blame] | 4295 | */ |
Nikolay Borisov | 70ddc55 | 2017-02-20 13:50:35 +0200 | [diff] [blame] | 4296 | if (!btrfs_is_free_space_inode(BTRFS_I(inode)) && |
Qu Wenruo | 92a7cc4 | 2020-05-15 14:01:40 +0800 | [diff] [blame] | 4297 | test_bit(BTRFS_ROOT_SHAREABLE, &root->state)) |
Thomas Meyer | 897ca81 | 2017-10-07 16:02:21 +0200 | [diff] [blame] | 4298 | be_nice = true; |
Chris Mason | 28ed134 | 2014-12-17 09:41:04 -0800 | [diff] [blame] | 4299 | |
Mark Fasheh | 0eb0e19 | 2011-07-12 16:44:10 -0700 | [diff] [blame] | 4300 | path = btrfs_alloc_path(); |
| 4301 | if (!path) |
| 4302 | return -ENOMEM; |
David Sterba | e4058b5 | 2015-11-27 16:31:35 +0100 | [diff] [blame] | 4303 | path->reada = READA_BACK; |
Mark Fasheh | 0eb0e19 | 2011-07-12 16:44:10 -0700 | [diff] [blame] | 4304 | |
Qu Wenruo | 82028e0a | 2020-05-15 14:01:41 +0800 | [diff] [blame] | 4305 | if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) { |
Filipe Manana | a5ae50d | 2020-02-20 13:29:49 +0000 | [diff] [blame] | 4306 | lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, (u64)-1, |
| 4307 | &cached_state); |
Filipe Manana | 28553fa | 2020-02-07 12:23:09 +0000 | [diff] [blame] | 4308 | |
Qu Wenruo | 82028e0a | 2020-05-15 14:01:41 +0800 | [diff] [blame] | 4309 | /* |
| 4310 | * We want to drop from the next block forward in case this |
| 4311 | * new size is not block aligned since we will be keeping the |
| 4312 | * last block of the extent just the way it is. |
| 4313 | */ |
Nikolay Borisov | dcdbc05 | 2017-02-20 13:50:45 +0200 | [diff] [blame] | 4314 | btrfs_drop_extent_cache(BTRFS_I(inode), ALIGN(new_size, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4315 | fs_info->sectorsize), |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 4316 | (u64)-1, 0); |
Qu Wenruo | 82028e0a | 2020-05-15 14:01:41 +0800 | [diff] [blame] | 4317 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4318 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 4319 | /* |
| 4320 | * This function is also used to drop the items in the log tree before |
| 4321 | * 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] | 4322 | * 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] | 4323 | * items. |
| 4324 | */ |
| 4325 | if (min_type == 0 && root == BTRFS_I(inode)->root) |
Nikolay Borisov | 4ccb5c7 | 2017-01-10 20:35:38 +0200 | [diff] [blame] | 4326 | btrfs_kill_delayed_inode_items(BTRFS_I(inode)); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 4327 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4328 | key.objectid = ino; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4329 | key.offset = (u64)-1; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4330 | key.type = (u8)-1; |
| 4331 | |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4332 | search_again: |
Chris Mason | 28ed134 | 2014-12-17 09:41:04 -0800 | [diff] [blame] | 4333 | /* |
| 4334 | * with a 16K leaf size and 128MB extents, you can actually queue |
| 4335 | * up a huge file in a single leaf. Most of the time that |
| 4336 | * bytes_deleted is > 0, it will be huge by the time we get here |
| 4337 | */ |
Omar Sandoval | fd86a3a | 2018-05-11 13:13:30 -0700 | [diff] [blame] | 4338 | if (be_nice && bytes_deleted > SZ_32M && |
| 4339 | btrfs_should_end_transaction(trans)) { |
| 4340 | ret = -EAGAIN; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4341 | goto out; |
| 4342 | } |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4343 | |
Omar Sandoval | fd86a3a | 2018-05-11 13:13:30 -0700 | [diff] [blame] | 4344 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 4345 | if (ret < 0) |
| 4346 | goto out; |
| 4347 | |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4348 | if (ret > 0) { |
Omar Sandoval | fd86a3a | 2018-05-11 13:13:30 -0700 | [diff] [blame] | 4349 | ret = 0; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4350 | /* there are no items in the tree for us to truncate, we're |
| 4351 | * done |
| 4352 | */ |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4353 | if (path->slots[0] == 0) |
| 4354 | goto out; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4355 | path->slots[0]--; |
| 4356 | } |
| 4357 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4358 | while (1) { |
Josef Bacik | 9ddc959 | 2020-01-17 09:02:22 -0500 | [diff] [blame] | 4359 | u64 clear_start = 0, clear_len = 0; |
| 4360 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4361 | fi = NULL; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4362 | leaf = path->nodes[0]; |
| 4363 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 4364 | found_type = found_key.type; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4365 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4366 | if (found_key.objectid != ino) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4367 | break; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4368 | |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4369 | if (found_type < min_type) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4370 | break; |
| 4371 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4372 | item_end = found_key.offset; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4373 | if (found_type == BTRFS_EXTENT_DATA_KEY) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4374 | fi = btrfs_item_ptr(leaf, path->slots[0], |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4375 | struct btrfs_file_extent_item); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4376 | extent_type = btrfs_file_extent_type(leaf, fi); |
| 4377 | if (extent_type != BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4378 | item_end += |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4379 | btrfs_file_extent_num_bytes(leaf, fi); |
Liu Bo | 09ed2f1 | 2017-03-10 11:09:48 -0800 | [diff] [blame] | 4380 | |
| 4381 | trace_btrfs_truncate_show_fi_regular( |
| 4382 | BTRFS_I(inode), leaf, fi, |
| 4383 | found_key.offset); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4384 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
Qu Wenruo | e41ca58 | 2018-06-06 15:41:49 +0800 | [diff] [blame] | 4385 | item_end += btrfs_file_extent_ram_bytes(leaf, |
| 4386 | fi); |
Liu Bo | 09ed2f1 | 2017-03-10 11:09:48 -0800 | [diff] [blame] | 4387 | |
| 4388 | trace_btrfs_truncate_show_fi_inline( |
| 4389 | BTRFS_I(inode), leaf, fi, path->slots[0], |
| 4390 | found_key.offset); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4391 | } |
Yan | 008630c | 2007-11-07 13:31:09 -0500 | [diff] [blame] | 4392 | item_end--; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4393 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4394 | if (found_type > min_type) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4395 | del_item = 1; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4396 | } else { |
Filipe Manana | 76b42ab | 2017-02-14 16:56:01 +0000 | [diff] [blame] | 4397 | if (item_end < new_size) |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4398 | break; |
| 4399 | if (found_key.offset >= new_size) |
| 4400 | del_item = 1; |
| 4401 | else |
| 4402 | del_item = 0; |
| 4403 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4404 | found_extent = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4405 | /* FIXME, shrink the extent if the ref count is only 1 */ |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4406 | if (found_type != BTRFS_EXTENT_DATA_KEY) |
| 4407 | goto delete; |
| 4408 | |
| 4409 | if (extent_type != BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4410 | u64 num_dec; |
Josef Bacik | 9ddc959 | 2020-01-17 09:02:22 -0500 | [diff] [blame] | 4411 | |
| 4412 | clear_start = found_key.offset; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4413 | extent_start = btrfs_file_extent_disk_bytenr(leaf, fi); |
Miao Xie | f70a9a6b | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 4414 | if (!del_item) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4415 | u64 orig_num_bytes = |
| 4416 | btrfs_file_extent_num_bytes(leaf, fi); |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 4417 | extent_num_bytes = ALIGN(new_size - |
| 4418 | found_key.offset, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4419 | fs_info->sectorsize); |
Josef Bacik | 9ddc959 | 2020-01-17 09:02:22 -0500 | [diff] [blame] | 4420 | clear_start = ALIGN(new_size, fs_info->sectorsize); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4421 | btrfs_set_file_extent_num_bytes(leaf, fi, |
| 4422 | extent_num_bytes); |
| 4423 | num_dec = (orig_num_bytes - |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 4424 | extent_num_bytes); |
Qu Wenruo | 92a7cc4 | 2020-05-15 14:01:40 +0800 | [diff] [blame] | 4425 | if (test_bit(BTRFS_ROOT_SHAREABLE, |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 4426 | &root->state) && |
| 4427 | extent_start != 0) |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 4428 | inode_sub_bytes(inode, num_dec); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4429 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4430 | } else { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4431 | extent_num_bytes = |
| 4432 | btrfs_file_extent_disk_num_bytes(leaf, |
| 4433 | fi); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4434 | extent_offset = found_key.offset - |
| 4435 | btrfs_file_extent_offset(leaf, fi); |
| 4436 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4437 | /* FIXME blocksize != 4096 */ |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 4438 | num_dec = btrfs_file_extent_num_bytes(leaf, fi); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4439 | if (extent_start != 0) { |
| 4440 | found_extent = 1; |
Qu Wenruo | 92a7cc4 | 2020-05-15 14:01:40 +0800 | [diff] [blame] | 4441 | if (test_bit(BTRFS_ROOT_SHAREABLE, |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 4442 | &root->state)) |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 4443 | inode_sub_bytes(inode, num_dec); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4444 | } |
| 4445 | } |
Josef Bacik | 9ddc959 | 2020-01-17 09:02:22 -0500 | [diff] [blame] | 4446 | clear_len = num_dec; |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 4447 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4448 | /* |
| 4449 | * we can't truncate inline items that have had |
| 4450 | * special encodings |
| 4451 | */ |
| 4452 | if (!del_item && |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4453 | btrfs_file_extent_encryption(leaf, fi) == 0 && |
Josef Bacik | ddfae63 | 2017-10-19 14:16:02 -0400 | [diff] [blame] | 4454 | btrfs_file_extent_other_encoding(leaf, fi) == 0 && |
| 4455 | btrfs_file_extent_compression(leaf, fi) == 0) { |
| 4456 | u32 size = (u32)(new_size - found_key.offset); |
Chris Mason | 514ac8a | 2014-01-03 21:07:00 -0800 | [diff] [blame] | 4457 | |
Josef Bacik | ddfae63 | 2017-10-19 14:16:02 -0400 | [diff] [blame] | 4458 | btrfs_set_file_extent_ram_bytes(leaf, fi, size); |
| 4459 | size = btrfs_file_extent_calc_inline_size(size); |
David Sterba | 78ac4f9 | 2019-03-20 14:49:12 +0100 | [diff] [blame] | 4460 | btrfs_truncate_item(path, size, 1); |
Josef Bacik | ddfae63 | 2017-10-19 14:16:02 -0400 | [diff] [blame] | 4461 | } else if (!del_item) { |
Chris Mason | 514ac8a | 2014-01-03 21:07:00 -0800 | [diff] [blame] | 4462 | /* |
Josef Bacik | ddfae63 | 2017-10-19 14:16:02 -0400 | [diff] [blame] | 4463 | * We have to bail so the last_size is set to |
| 4464 | * just before this extent. |
Chris Mason | 514ac8a | 2014-01-03 21:07:00 -0800 | [diff] [blame] | 4465 | */ |
Omar Sandoval | fd86a3a | 2018-05-11 13:13:30 -0700 | [diff] [blame] | 4466 | ret = NEED_TRUNCATE_BLOCK; |
Josef Bacik | ddfae63 | 2017-10-19 14:16:02 -0400 | [diff] [blame] | 4467 | break; |
Josef Bacik | 9ddc959 | 2020-01-17 09:02:22 -0500 | [diff] [blame] | 4468 | } else { |
| 4469 | /* |
| 4470 | * Inline extents are special, we just treat |
| 4471 | * them as a full sector worth in the file |
| 4472 | * extent tree just for simplicity sake. |
| 4473 | */ |
| 4474 | clear_len = fs_info->sectorsize; |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 4475 | } |
Josef Bacik | ddfae63 | 2017-10-19 14:16:02 -0400 | [diff] [blame] | 4476 | |
Qu Wenruo | 92a7cc4 | 2020-05-15 14:01:40 +0800 | [diff] [blame] | 4477 | if (test_bit(BTRFS_ROOT_SHAREABLE, &root->state)) |
Josef Bacik | ddfae63 | 2017-10-19 14:16:02 -0400 | [diff] [blame] | 4478 | inode_sub_bytes(inode, item_end + 1 - new_size); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4479 | } |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4480 | delete: |
Josef Bacik | 9ddc959 | 2020-01-17 09:02:22 -0500 | [diff] [blame] | 4481 | /* |
| 4482 | * We use btrfs_truncate_inode_items() to clean up log trees for |
| 4483 | * multiple fsyncs, and in this case we don't want to clear the |
| 4484 | * file extent range because it's just the log. |
| 4485 | */ |
| 4486 | if (root == BTRFS_I(inode)->root) { |
| 4487 | ret = btrfs_inode_clear_file_extent_range(BTRFS_I(inode), |
| 4488 | clear_start, clear_len); |
| 4489 | if (ret) { |
| 4490 | btrfs_abort_transaction(trans, ret); |
| 4491 | break; |
| 4492 | } |
| 4493 | } |
| 4494 | |
Josef Bacik | ddfae63 | 2017-10-19 14:16:02 -0400 | [diff] [blame] | 4495 | if (del_item) |
| 4496 | last_size = found_key.offset; |
| 4497 | else |
| 4498 | last_size = new_size; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4499 | if (del_item) { |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4500 | if (!pending_del_nr) { |
| 4501 | /* no pending yet, add ourselves */ |
| 4502 | pending_del_slot = path->slots[0]; |
| 4503 | pending_del_nr = 1; |
| 4504 | } else if (pending_del_nr && |
| 4505 | path->slots[0] + 1 == pending_del_slot) { |
| 4506 | /* hop on the pending chunk */ |
| 4507 | pending_del_nr++; |
| 4508 | pending_del_slot = path->slots[0]; |
| 4509 | } else { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4510 | BUG(); |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4511 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4512 | } else { |
| 4513 | break; |
| 4514 | } |
Thomas Meyer | 897ca81 | 2017-10-07 16:02:21 +0200 | [diff] [blame] | 4515 | should_throttle = false; |
Chris Mason | 28f75a0 | 2015-02-04 06:59:29 -0800 | [diff] [blame] | 4516 | |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 4517 | if (found_extent && |
Qu Wenruo | 82028e0a | 2020-05-15 14:01:41 +0800 | [diff] [blame] | 4518 | root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) { |
Qu Wenruo | ffd4bb2 | 2019-04-04 14:45:36 +0800 | [diff] [blame] | 4519 | struct btrfs_ref ref = { 0 }; |
| 4520 | |
Chris Mason | 28ed134 | 2014-12-17 09:41:04 -0800 | [diff] [blame] | 4521 | bytes_deleted += extent_num_bytes; |
Qu Wenruo | ffd4bb2 | 2019-04-04 14:45:36 +0800 | [diff] [blame] | 4522 | |
| 4523 | btrfs_init_generic_ref(&ref, BTRFS_DROP_DELAYED_REF, |
| 4524 | extent_start, extent_num_bytes, 0); |
| 4525 | ref.real_root = root->root_key.objectid; |
| 4526 | btrfs_init_data_ref(&ref, btrfs_header_owner(leaf), |
| 4527 | ino, extent_offset); |
| 4528 | ret = btrfs_free_extent(trans, &ref); |
Omar Sandoval | 0552210 | 2018-05-11 13:13:31 -0700 | [diff] [blame] | 4529 | if (ret) { |
| 4530 | btrfs_abort_transaction(trans, ret); |
| 4531 | break; |
| 4532 | } |
Chris Mason | 28f75a0 | 2015-02-04 06:59:29 -0800 | [diff] [blame] | 4533 | if (be_nice) { |
Lu Fengqi | 7c86162 | 2018-10-11 13:40:36 +0800 | [diff] [blame] | 4534 | if (btrfs_should_throttle_delayed_refs(trans)) |
Thomas Meyer | 897ca81 | 2017-10-07 16:02:21 +0200 | [diff] [blame] | 4535 | should_throttle = true; |
Chris Mason | 28f75a0 | 2015-02-04 06:59:29 -0800 | [diff] [blame] | 4536 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4537 | } |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4538 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4539 | if (found_type == BTRFS_INODE_ITEM_KEY) |
| 4540 | break; |
| 4541 | |
| 4542 | if (path->slots[0] == 0 || |
Josef Bacik | 1262133 | 2015-02-03 07:50:16 -0800 | [diff] [blame] | 4543 | path->slots[0] != pending_del_slot || |
Josef Bacik | 28bad21 | 2018-12-03 10:20:38 -0500 | [diff] [blame] | 4544 | should_throttle) { |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4545 | if (pending_del_nr) { |
| 4546 | ret = btrfs_del_items(trans, root, path, |
| 4547 | pending_del_slot, |
| 4548 | pending_del_nr); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4549 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 4550 | btrfs_abort_transaction(trans, ret); |
Omar Sandoval | fd86a3a | 2018-05-11 13:13:30 -0700 | [diff] [blame] | 4551 | break; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4552 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4553 | pending_del_nr = 0; |
| 4554 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4555 | btrfs_release_path(path); |
Josef Bacik | 28bad21 | 2018-12-03 10:20:38 -0500 | [diff] [blame] | 4556 | |
Chris Mason | 28f75a0 | 2015-02-04 06:59:29 -0800 | [diff] [blame] | 4557 | /* |
Josef Bacik | 28bad21 | 2018-12-03 10:20:38 -0500 | [diff] [blame] | 4558 | * We can generate a lot of delayed refs, so we need to |
| 4559 | * throttle every once and a while and make sure we're |
| 4560 | * adding enough space to keep up with the work we are |
| 4561 | * generating. Since we hold a transaction here we |
| 4562 | * can't flush, and we don't want to FLUSH_LIMIT because |
| 4563 | * we could have generated too many delayed refs to |
| 4564 | * actually allocate, so just bail if we're short and |
| 4565 | * let the normal reservation dance happen higher up. |
Chris Mason | 28f75a0 | 2015-02-04 06:59:29 -0800 | [diff] [blame] | 4566 | */ |
Josef Bacik | 28bad21 | 2018-12-03 10:20:38 -0500 | [diff] [blame] | 4567 | if (should_throttle) { |
| 4568 | ret = btrfs_delayed_refs_rsv_refill(fs_info, |
| 4569 | BTRFS_RESERVE_NO_FLUSH); |
| 4570 | if (ret) { |
| 4571 | ret = -EAGAIN; |
| 4572 | break; |
| 4573 | } |
Chris Mason | 28f75a0 | 2015-02-04 06:59:29 -0800 | [diff] [blame] | 4574 | } |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4575 | goto search_again; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4576 | } else { |
| 4577 | path->slots[0]--; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4578 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4579 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4580 | out: |
Omar Sandoval | fd86a3a | 2018-05-11 13:13:30 -0700 | [diff] [blame] | 4581 | if (ret >= 0 && pending_del_nr) { |
| 4582 | int err; |
| 4583 | |
| 4584 | err = btrfs_del_items(trans, root, path, pending_del_slot, |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4585 | pending_del_nr); |
Omar Sandoval | fd86a3a | 2018-05-11 13:13:30 -0700 | [diff] [blame] | 4586 | if (err) { |
| 4587 | btrfs_abort_transaction(trans, err); |
| 4588 | ret = err; |
| 4589 | } |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4590 | } |
Filipe Manana | 76b42ab | 2017-02-14 16:56:01 +0000 | [diff] [blame] | 4591 | if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) { |
| 4592 | ASSERT(last_size >= new_size); |
Omar Sandoval | fd86a3a | 2018-05-11 13:13:30 -0700 | [diff] [blame] | 4593 | if (!ret && last_size > new_size) |
Filipe Manana | 76b42ab | 2017-02-14 16:56:01 +0000 | [diff] [blame] | 4594 | last_size = new_size; |
Josef Bacik | d923afe | 2020-01-17 09:02:23 -0500 | [diff] [blame] | 4595 | btrfs_inode_safe_disk_i_size_write(inode, last_size); |
Filipe Manana | a5ae50d | 2020-02-20 13:29:49 +0000 | [diff] [blame] | 4596 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, |
| 4597 | (u64)-1, &cached_state); |
Filipe Manana | 76b42ab | 2017-02-14 16:56:01 +0000 | [diff] [blame] | 4598 | } |
Chris Mason | 28ed134 | 2014-12-17 09:41:04 -0800 | [diff] [blame] | 4599 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4600 | btrfs_free_path(path); |
Omar Sandoval | fd86a3a | 2018-05-11 13:13:30 -0700 | [diff] [blame] | 4601 | return ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4602 | } |
| 4603 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4604 | /* |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4605 | * btrfs_truncate_block - read, zero a chunk and write a block |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 4606 | * @inode - inode that we're zeroing |
| 4607 | * @from - the offset to start zeroing |
| 4608 | * @len - the length to zero, 0 to zero the entire range respective to the |
| 4609 | * offset |
| 4610 | * @front - zero up to the offset instead of from the offset on |
| 4611 | * |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4612 | * 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] | 4613 | * 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] | 4614 | */ |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4615 | 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] | 4616 | int front) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4617 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4618 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 4619 | struct address_space *mapping = inode->i_mapping; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4620 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
| 4621 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4622 | struct extent_state *cached_state = NULL; |
Qu Wenruo | 364ecf3 | 2017-02-27 15:10:38 +0800 | [diff] [blame] | 4623 | struct extent_changeset *data_reserved = NULL; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4624 | char *kaddr; |
Qu Wenruo | 6d4572a | 2020-06-24 07:23:50 +0800 | [diff] [blame] | 4625 | bool only_release_metadata = false; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4626 | u32 blocksize = fs_info->sectorsize; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 4627 | pgoff_t index = from >> PAGE_SHIFT; |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4628 | unsigned offset = from & (blocksize - 1); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4629 | struct page *page; |
Josef Bacik | 3b16a4e | 2011-09-21 15:05:58 -0400 | [diff] [blame] | 4630 | gfp_t mask = btrfs_alloc_write_mask(mapping); |
Qu Wenruo | 6d4572a | 2020-06-24 07:23:50 +0800 | [diff] [blame] | 4631 | size_t write_bytes = blocksize; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4632 | int ret = 0; |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4633 | u64 block_start; |
| 4634 | u64 block_end; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4635 | |
Nikolay Borisov | b03ebd9 | 2018-01-18 14:47:06 +0200 | [diff] [blame] | 4636 | if (IS_ALIGNED(offset, blocksize) && |
| 4637 | (!len || IS_ALIGNED(len, blocksize))) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4638 | goto out; |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4639 | |
Josef Bacik | 8b62f87 | 2017-10-19 14:15:55 -0400 | [diff] [blame] | 4640 | block_start = round_down(from, blocksize); |
| 4641 | block_end = block_start + blocksize - 1; |
| 4642 | |
Nikolay Borisov | 36ea6f3 | 2020-06-03 08:55:41 +0300 | [diff] [blame] | 4643 | ret = btrfs_check_data_free_space(BTRFS_I(inode), &data_reserved, |
| 4644 | block_start, blocksize); |
Qu Wenruo | 6d4572a | 2020-06-24 07:23:50 +0800 | [diff] [blame] | 4645 | if (ret < 0) { |
Qu Wenruo | 38d37aa | 2020-06-24 07:23:52 +0800 | [diff] [blame] | 4646 | if (btrfs_check_nocow_lock(BTRFS_I(inode), block_start, |
| 4647 | &write_bytes) > 0) { |
Qu Wenruo | 6d4572a | 2020-06-24 07:23:50 +0800 | [diff] [blame] | 4648 | /* For nocow case, no need to reserve data space */ |
| 4649 | only_release_metadata = true; |
| 4650 | } else { |
| 4651 | goto out; |
| 4652 | } |
| 4653 | } |
| 4654 | ret = btrfs_delalloc_reserve_metadata(BTRFS_I(inode), blocksize); |
| 4655 | if (ret < 0) { |
| 4656 | if (!only_release_metadata) |
Nikolay Borisov | 25ce28c | 2020-06-03 08:55:39 +0300 | [diff] [blame] | 4657 | btrfs_free_reserved_data_space(BTRFS_I(inode), |
| 4658 | data_reserved, block_start, blocksize); |
Qu Wenruo | 6d4572a | 2020-06-24 07:23:50 +0800 | [diff] [blame] | 4659 | goto out; |
| 4660 | } |
Chris Mason | 211c17f | 2008-05-15 09:13:45 -0400 | [diff] [blame] | 4661 | again: |
Josef Bacik | 3b16a4e | 2011-09-21 15:05:58 -0400 | [diff] [blame] | 4662 | page = find_or_create_page(mapping, index, mask); |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 4663 | if (!page) { |
Nikolay Borisov | 86d5292 | 2020-06-03 08:55:40 +0300 | [diff] [blame] | 4664 | btrfs_delalloc_release_space(BTRFS_I(inode), data_reserved, |
Qu Wenruo | 43b1859 | 2017-12-12 15:34:32 +0800 | [diff] [blame] | 4665 | block_start, blocksize, true); |
Qu Wenruo | 8702ba9 | 2019-10-14 14:34:51 +0800 | [diff] [blame] | 4666 | btrfs_delalloc_release_extents(BTRFS_I(inode), blocksize); |
Miao Xie | ac6a2b3 | 2012-12-05 10:56:13 +0000 | [diff] [blame] | 4667 | ret = -ENOMEM; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4668 | goto out; |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 4669 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4670 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4671 | if (!PageUptodate(page)) { |
| 4672 | ret = btrfs_readpage(NULL, page); |
| 4673 | lock_page(page); |
Chris Mason | 211c17f | 2008-05-15 09:13:45 -0400 | [diff] [blame] | 4674 | if (page->mapping != mapping) { |
| 4675 | unlock_page(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 4676 | put_page(page); |
Chris Mason | 211c17f | 2008-05-15 09:13:45 -0400 | [diff] [blame] | 4677 | goto again; |
| 4678 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4679 | if (!PageUptodate(page)) { |
| 4680 | ret = -EIO; |
Chris Mason | 8964222 | 2008-07-24 09:41:53 -0400 | [diff] [blame] | 4681 | goto out_unlock; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4682 | } |
| 4683 | } |
Chris Mason | 211c17f | 2008-05-15 09:13:45 -0400 | [diff] [blame] | 4684 | wait_on_page_writeback(page); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4685 | |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4686 | lock_extent_bits(io_tree, block_start, block_end, &cached_state); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4687 | set_page_extent_mapped(page); |
| 4688 | |
Nikolay Borisov | c350437 | 2020-06-03 08:55:03 +0300 | [diff] [blame] | 4689 | ordered = btrfs_lookup_ordered_extent(BTRFS_I(inode), block_start); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4690 | if (ordered) { |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4691 | unlock_extent_cached(io_tree, block_start, block_end, |
David Sterba | e43bbe5 | 2017-12-12 21:43:52 +0100 | [diff] [blame] | 4692 | &cached_state); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4693 | unlock_page(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 4694 | put_page(page); |
Nikolay Borisov | c0a4360 | 2020-09-18 12:15:53 +0300 | [diff] [blame] | 4695 | btrfs_start_ordered_extent(ordered, 1); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4696 | btrfs_put_ordered_extent(ordered); |
| 4697 | goto again; |
| 4698 | } |
| 4699 | |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4700 | clear_extent_bit(&BTRFS_I(inode)->io_tree, block_start, block_end, |
Omar Sandoval | e182163 | 2019-08-15 14:04:04 -0700 | [diff] [blame] | 4701 | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, |
| 4702 | 0, 0, &cached_state); |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 4703 | |
Nikolay Borisov | c2566f2 | 2020-06-03 08:55:35 +0300 | [diff] [blame] | 4704 | 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] | 4705 | &cached_state); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4706 | if (ret) { |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4707 | unlock_extent_cached(io_tree, block_start, block_end, |
David Sterba | e43bbe5 | 2017-12-12 21:43:52 +0100 | [diff] [blame] | 4708 | &cached_state); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4709 | goto out_unlock; |
| 4710 | } |
| 4711 | |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4712 | if (offset != blocksize) { |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 4713 | if (!len) |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4714 | len = blocksize - offset; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4715 | kaddr = kmap(page); |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 4716 | if (front) |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4717 | memset(kaddr + (block_start - page_offset(page)), |
| 4718 | 0, offset); |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 4719 | else |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4720 | memset(kaddr + (block_start - page_offset(page)) + offset, |
| 4721 | 0, len); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4722 | flush_dcache_page(page); |
| 4723 | kunmap(page); |
| 4724 | } |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 4725 | ClearPageChecked(page); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4726 | set_page_dirty(page); |
David Sterba | e43bbe5 | 2017-12-12 21:43:52 +0100 | [diff] [blame] | 4727 | unlock_extent_cached(io_tree, block_start, block_end, &cached_state); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4728 | |
Qu Wenruo | 6d4572a | 2020-06-24 07:23:50 +0800 | [diff] [blame] | 4729 | if (only_release_metadata) |
| 4730 | set_extent_bit(&BTRFS_I(inode)->io_tree, block_start, |
Qu Wenruo | 3f6bb4a | 2020-10-21 14:24:51 +0800 | [diff] [blame] | 4731 | block_end, EXTENT_NORESERVE, NULL, GFP_NOFS); |
Qu Wenruo | 6d4572a | 2020-06-24 07:23:50 +0800 | [diff] [blame] | 4732 | |
Chris Mason | 8964222 | 2008-07-24 09:41:53 -0400 | [diff] [blame] | 4733 | out_unlock: |
Qu Wenruo | 6d4572a | 2020-06-24 07:23:50 +0800 | [diff] [blame] | 4734 | if (ret) { |
| 4735 | if (only_release_metadata) |
| 4736 | btrfs_delalloc_release_metadata(BTRFS_I(inode), |
| 4737 | blocksize, true); |
| 4738 | else |
Nikolay Borisov | 86d5292 | 2020-06-03 08:55:40 +0300 | [diff] [blame] | 4739 | btrfs_delalloc_release_space(BTRFS_I(inode), data_reserved, |
Qu Wenruo | 6d4572a | 2020-06-24 07:23:50 +0800 | [diff] [blame] | 4740 | block_start, blocksize, true); |
| 4741 | } |
Qu Wenruo | 8702ba9 | 2019-10-14 14:34:51 +0800 | [diff] [blame] | 4742 | btrfs_delalloc_release_extents(BTRFS_I(inode), blocksize); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4743 | unlock_page(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 4744 | put_page(page); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4745 | out: |
Qu Wenruo | 6d4572a | 2020-06-24 07:23:50 +0800 | [diff] [blame] | 4746 | if (only_release_metadata) |
Qu Wenruo | 38d37aa | 2020-06-24 07:23:52 +0800 | [diff] [blame] | 4747 | btrfs_check_nocow_unlock(BTRFS_I(inode)); |
Qu Wenruo | 364ecf3 | 2017-02-27 15:10:38 +0800 | [diff] [blame] | 4748 | extent_changeset_free(data_reserved); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4749 | return ret; |
| 4750 | } |
| 4751 | |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4752 | static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode, |
| 4753 | u64 offset, u64 len) |
| 4754 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4755 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4756 | struct btrfs_trans_handle *trans; |
Filipe Manana | 5893dfb | 2020-11-04 11:07:32 +0000 | [diff] [blame^] | 4757 | struct btrfs_drop_extents_args drop_args = { 0 }; |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4758 | int ret; |
| 4759 | |
| 4760 | /* |
| 4761 | * Still need to make sure the inode looks like it's been updated so |
| 4762 | * that any holes get logged if we fsync. |
| 4763 | */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4764 | if (btrfs_fs_incompat(fs_info, NO_HOLES)) { |
| 4765 | BTRFS_I(inode)->last_trans = fs_info->generation; |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4766 | BTRFS_I(inode)->last_sub_trans = root->log_transid; |
| 4767 | BTRFS_I(inode)->last_log_commit = root->last_log_commit; |
| 4768 | return 0; |
| 4769 | } |
| 4770 | |
| 4771 | /* |
| 4772 | * 1 - for the one we're dropping |
| 4773 | * 1 - for the one we're adding |
| 4774 | * 1 - for updating the inode. |
| 4775 | */ |
| 4776 | trans = btrfs_start_transaction(root, 3); |
| 4777 | if (IS_ERR(trans)) |
| 4778 | return PTR_ERR(trans); |
| 4779 | |
Filipe Manana | 5893dfb | 2020-11-04 11:07:32 +0000 | [diff] [blame^] | 4780 | drop_args.start = offset; |
| 4781 | drop_args.end = offset + len; |
| 4782 | drop_args.drop_cache = true; |
| 4783 | |
| 4784 | ret = btrfs_drop_extents(trans, root, BTRFS_I(inode), &drop_args); |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4785 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 4786 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 4787 | btrfs_end_transaction(trans); |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4788 | return ret; |
| 4789 | } |
| 4790 | |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 4791 | ret = btrfs_insert_file_extent(trans, root, btrfs_ino(BTRFS_I(inode)), |
| 4792 | offset, 0, 0, len, 0, len, 0, 0, 0); |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4793 | if (ret) |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 4794 | btrfs_abort_transaction(trans, ret); |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4795 | else |
| 4796 | btrfs_update_inode(trans, root, inode); |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 4797 | btrfs_end_transaction(trans); |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4798 | return ret; |
| 4799 | } |
| 4800 | |
Josef Bacik | 695a0d0 | 2011-03-04 15:46:53 -0500 | [diff] [blame] | 4801 | /* |
| 4802 | * This function puts in dummy file extents for the area we're creating a hole |
| 4803 | * for. So if we are truncating this file to a larger size we need to insert |
| 4804 | * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for |
| 4805 | * the range between oldsize and size |
| 4806 | */ |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 4807 | 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] | 4808 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4809 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4810 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 4811 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4812 | struct extent_map *em = NULL; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4813 | struct extent_state *cached_state = NULL; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4814 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4815 | u64 hole_start = ALIGN(oldsize, fs_info->sectorsize); |
| 4816 | u64 block_end = ALIGN(size, fs_info->sectorsize); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4817 | u64 last_byte; |
| 4818 | u64 cur_offset; |
| 4819 | u64 hole_size; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4820 | int err = 0; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4821 | |
Josef Bacik | a71754f | 2013-06-17 17:14:39 -0400 | [diff] [blame] | 4822 | /* |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4823 | * If our size started in the middle of a block we need to zero out the |
| 4824 | * 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] | 4825 | * expose stale data. |
| 4826 | */ |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4827 | err = btrfs_truncate_block(inode, oldsize, 0, 0); |
Josef Bacik | a71754f | 2013-06-17 17:14:39 -0400 | [diff] [blame] | 4828 | if (err) |
| 4829 | return err; |
| 4830 | |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4831 | if (size <= hole_start) |
| 4832 | return 0; |
| 4833 | |
David Sterba | b272ae2 | 2020-02-05 19:09:33 +0100 | [diff] [blame] | 4834 | btrfs_lock_and_flush_ordered_range(BTRFS_I(inode), hole_start, |
Nikolay Borisov | 23d31bd | 2019-05-07 10:19:23 +0300 | [diff] [blame] | 4835 | block_end - 1, &cached_state); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4836 | cur_offset = hole_start; |
| 4837 | while (1) { |
Nikolay Borisov | fc4f21b1 | 2017-02-20 13:51:06 +0200 | [diff] [blame] | 4838 | em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, cur_offset, |
Omar Sandoval | 39b07b5 | 2019-12-02 17:34:23 -0800 | [diff] [blame] | 4839 | block_end - cur_offset); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4840 | if (IS_ERR(em)) { |
| 4841 | err = PTR_ERR(em); |
Zach Brown | f276795 | 2013-01-08 19:37:58 +0000 | [diff] [blame] | 4842 | em = NULL; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4843 | break; |
| 4844 | } |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4845 | last_byte = min(extent_map_end(em), block_end); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4846 | last_byte = ALIGN(last_byte, fs_info->sectorsize); |
Josef Bacik | 9ddc959 | 2020-01-17 09:02:22 -0500 | [diff] [blame] | 4847 | hole_size = last_byte - cur_offset; |
| 4848 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4849 | if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) { |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4850 | struct extent_map *hole_em; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4851 | |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4852 | err = maybe_insert_hole(root, inode, cur_offset, |
| 4853 | hole_size); |
| 4854 | if (err) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 4855 | break; |
Josef Bacik | 9ddc959 | 2020-01-17 09:02:22 -0500 | [diff] [blame] | 4856 | |
| 4857 | err = btrfs_inode_set_file_extent_range(BTRFS_I(inode), |
| 4858 | cur_offset, hole_size); |
| 4859 | if (err) |
| 4860 | break; |
| 4861 | |
Nikolay Borisov | dcdbc05 | 2017-02-20 13:50:45 +0200 | [diff] [blame] | 4862 | btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset, |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4863 | cur_offset + hole_size - 1, 0); |
| 4864 | hole_em = alloc_extent_map(); |
| 4865 | if (!hole_em) { |
| 4866 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, |
| 4867 | &BTRFS_I(inode)->runtime_flags); |
| 4868 | goto next; |
| 4869 | } |
| 4870 | hole_em->start = cur_offset; |
| 4871 | hole_em->len = hole_size; |
| 4872 | hole_em->orig_start = cur_offset; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4873 | |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4874 | hole_em->block_start = EXTENT_MAP_HOLE; |
| 4875 | hole_em->block_len = 0; |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 4876 | hole_em->orig_block_len = 0; |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 4877 | hole_em->ram_bytes = hole_size; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4878 | hole_em->compress_type = BTRFS_COMPRESS_NONE; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4879 | hole_em->generation = fs_info->generation; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4880 | |
| 4881 | while (1) { |
| 4882 | write_lock(&em_tree->lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 4883 | err = add_extent_mapping(em_tree, hole_em, 1); |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4884 | write_unlock(&em_tree->lock); |
| 4885 | if (err != -EEXIST) |
| 4886 | break; |
Nikolay Borisov | dcdbc05 | 2017-02-20 13:50:45 +0200 | [diff] [blame] | 4887 | btrfs_drop_extent_cache(BTRFS_I(inode), |
| 4888 | cur_offset, |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4889 | cur_offset + |
| 4890 | hole_size - 1, 0); |
| 4891 | } |
| 4892 | free_extent_map(hole_em); |
Josef Bacik | 9ddc959 | 2020-01-17 09:02:22 -0500 | [diff] [blame] | 4893 | } else { |
| 4894 | err = btrfs_inode_set_file_extent_range(BTRFS_I(inode), |
| 4895 | cur_offset, hole_size); |
| 4896 | if (err) |
| 4897 | break; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4898 | } |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4899 | next: |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4900 | free_extent_map(em); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4901 | em = NULL; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4902 | cur_offset = last_byte; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4903 | if (cur_offset >= block_end) |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4904 | break; |
| 4905 | } |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4906 | free_extent_map(em); |
David Sterba | e43bbe5 | 2017-12-12 21:43:52 +0100 | [diff] [blame] | 4907 | unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4908 | return err; |
| 4909 | } |
| 4910 | |
Eric Sandeen | 3972f26 | 2013-01-12 02:57:22 +0000 | [diff] [blame] | 4911 | static int btrfs_setsize(struct inode *inode, struct iattr *attr) |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4912 | { |
Miao Xie | f4a2f4c | 2011-12-14 20:12:01 -0500 | [diff] [blame] | 4913 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 4914 | struct btrfs_trans_handle *trans; |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 4915 | loff_t oldsize = i_size_read(inode); |
Eric Sandeen | 3972f26 | 2013-01-12 02:57:22 +0000 | [diff] [blame] | 4916 | loff_t newsize = attr->ia_size; |
| 4917 | int mask = attr->ia_valid; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4918 | int ret; |
| 4919 | |
Eric Sandeen | 3972f26 | 2013-01-12 02:57:22 +0000 | [diff] [blame] | 4920 | /* |
| 4921 | * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a |
| 4922 | * special case where we need to update the times despite not having |
| 4923 | * these flags set. For all other operations the VFS set these flags |
| 4924 | * explicitly if it wants a timestamp update. |
| 4925 | */ |
Christoph Hellwig | dff6efc | 2013-11-19 07:17:07 -0800 | [diff] [blame] | 4926 | if (newsize != oldsize) { |
| 4927 | inode_inc_iversion(inode); |
| 4928 | if (!(mask & (ATTR_CTIME | ATTR_MTIME))) |
| 4929 | inode->i_ctime = inode->i_mtime = |
Deepa Dinamani | c2050a4 | 2016-09-14 07:48:06 -0700 | [diff] [blame] | 4930 | current_time(inode); |
Christoph Hellwig | dff6efc | 2013-11-19 07:17:07 -0800 | [diff] [blame] | 4931 | } |
Eric Sandeen | 3972f26 | 2013-01-12 02:57:22 +0000 | [diff] [blame] | 4932 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 4933 | if (newsize > oldsize) { |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 4934 | /* |
David Sterba | ea14b57f | 2017-06-22 02:19:11 +0200 | [diff] [blame] | 4935 | * Don't do an expanding truncate while snapshotting is ongoing. |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 4936 | * This is to ensure the snapshot captures a fully consistent |
| 4937 | * state of this file - if the snapshot captures this expanding |
| 4938 | * truncation, it must capture all writes that happened before |
| 4939 | * this truncation. |
| 4940 | */ |
Nikolay Borisov | dcc3eb9 | 2020-01-30 14:59:45 +0200 | [diff] [blame] | 4941 | btrfs_drew_write_lock(&root->snapshot_lock); |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 4942 | ret = btrfs_cont_expand(inode, oldsize, newsize); |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 4943 | if (ret) { |
Nikolay Borisov | dcc3eb9 | 2020-01-30 14:59:45 +0200 | [diff] [blame] | 4944 | btrfs_drew_write_unlock(&root->snapshot_lock); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4945 | return ret; |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 4946 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4947 | |
Miao Xie | f4a2f4c | 2011-12-14 20:12:01 -0500 | [diff] [blame] | 4948 | trans = btrfs_start_transaction(root, 1); |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 4949 | if (IS_ERR(trans)) { |
Nikolay Borisov | dcc3eb9 | 2020-01-30 14:59:45 +0200 | [diff] [blame] | 4950 | btrfs_drew_write_unlock(&root->snapshot_lock); |
Miao Xie | f4a2f4c | 2011-12-14 20:12:01 -0500 | [diff] [blame] | 4951 | return PTR_ERR(trans); |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 4952 | } |
Miao Xie | f4a2f4c | 2011-12-14 20:12:01 -0500 | [diff] [blame] | 4953 | |
| 4954 | i_size_write(inode, newsize); |
Josef Bacik | d923afe | 2020-01-17 09:02:23 -0500 | [diff] [blame] | 4955 | btrfs_inode_safe_disk_i_size_write(inode, 0); |
Chandan Rajendra | 27772b6 | 2016-01-21 15:56:03 +0530 | [diff] [blame] | 4956 | pagecache_isize_extended(inode, oldsize, newsize); |
Miao Xie | f4a2f4c | 2011-12-14 20:12:01 -0500 | [diff] [blame] | 4957 | ret = btrfs_update_inode(trans, root, inode); |
Nikolay Borisov | dcc3eb9 | 2020-01-30 14:59:45 +0200 | [diff] [blame] | 4958 | btrfs_drew_write_unlock(&root->snapshot_lock); |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 4959 | btrfs_end_transaction(trans); |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 4960 | } else { |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4961 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 4962 | /* |
| 4963 | * 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] | 4964 | * zero. Make sure any new writes to the file get on disk |
| 4965 | * on close. |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 4966 | */ |
| 4967 | if (newsize == 0) |
Nikolay Borisov | 1fd4033 | 2020-10-01 09:40:39 +0300 | [diff] [blame] | 4968 | set_bit(BTRFS_INODE_FLUSH_ON_CLOSE, |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 4969 | &BTRFS_I(inode)->runtime_flags); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4970 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 4971 | truncate_setsize(inode, newsize); |
Miao Xie | 2e60a51 | 2013-02-08 07:01:08 +0000 | [diff] [blame] | 4972 | |
Miao Xie | 2e60a51 | 2013-02-08 07:01:08 +0000 | [diff] [blame] | 4973 | inode_dio_wait(inode); |
Miao Xie | 2e60a51 | 2013-02-08 07:01:08 +0000 | [diff] [blame] | 4974 | |
Filipe Manana | 213e8c5 | 2018-02-06 20:40:31 +0000 | [diff] [blame] | 4975 | ret = btrfs_truncate(inode, newsize == oldsize); |
Josef Bacik | 7f4f6e0 | 2013-08-29 16:43:28 -0400 | [diff] [blame] | 4976 | if (ret && inode->i_nlink) { |
| 4977 | int err; |
| 4978 | |
| 4979 | /* |
Omar Sandoval | f7e9e8f | 2018-05-11 13:13:32 -0700 | [diff] [blame] | 4980 | * Truncate failed, so fix up the in-memory size. We |
| 4981 | * adjusted disk_i_size down as we removed extents, so |
| 4982 | * wait for disk_i_size to be stable and then update the |
| 4983 | * in-memory size to match. |
Josef Bacik | 7f4f6e0 | 2013-08-29 16:43:28 -0400 | [diff] [blame] | 4984 | */ |
Omar Sandoval | f7e9e8f | 2018-05-11 13:13:32 -0700 | [diff] [blame] | 4985 | err = btrfs_wait_ordered_range(inode, 0, (u64)-1); |
Josef Bacik | 7f4f6e0 | 2013-08-29 16:43:28 -0400 | [diff] [blame] | 4986 | if (err) |
Omar Sandoval | f7e9e8f | 2018-05-11 13:13:32 -0700 | [diff] [blame] | 4987 | return err; |
| 4988 | i_size_write(inode, BTRFS_I(inode)->disk_i_size); |
Josef Bacik | 7f4f6e0 | 2013-08-29 16:43:28 -0400 | [diff] [blame] | 4989 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4990 | } |
| 4991 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 4992 | return ret; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4993 | } |
| 4994 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4995 | static int btrfs_setattr(struct dentry *dentry, struct iattr *attr) |
| 4996 | { |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 4997 | struct inode *inode = d_inode(dentry); |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 4998 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4999 | int err; |
| 5000 | |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 5001 | if (btrfs_root_readonly(root)) |
| 5002 | return -EROFS; |
| 5003 | |
Jan Kara | 31051c8 | 2016-05-26 16:55:18 +0200 | [diff] [blame] | 5004 | err = setattr_prepare(dentry, attr); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5005 | if (err) |
| 5006 | return err; |
| 5007 | |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 5008 | if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) { |
Eric Sandeen | 3972f26 | 2013-01-12 02:57:22 +0000 | [diff] [blame] | 5009 | err = btrfs_setsize(inode, attr); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5010 | if (err) |
| 5011 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5012 | } |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 5013 | |
Christoph Hellwig | 1025774 | 2010-06-04 11:30:02 +0200 | [diff] [blame] | 5014 | if (attr->ia_valid) { |
| 5015 | setattr_copy(inode, attr); |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 5016 | inode_inc_iversion(inode); |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 5017 | err = btrfs_dirty_inode(inode); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 5018 | |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 5019 | if (!err && attr->ia_valid & ATTR_MODE) |
Christoph Hellwig | 996a710 | 2013-12-20 05:16:43 -0800 | [diff] [blame] | 5020 | err = posix_acl_chmod(inode, inode->i_mode); |
Christoph Hellwig | 1025774 | 2010-06-04 11:30:02 +0200 | [diff] [blame] | 5021 | } |
| 5022 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5023 | return err; |
| 5024 | } |
Chris Mason | 61295eb | 2008-01-14 16:24:38 -0500 | [diff] [blame] | 5025 | |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5026 | /* |
| 5027 | * While truncating the inode pages during eviction, we get the VFS calling |
| 5028 | * btrfs_invalidatepage() against each page of the inode. This is slow because |
| 5029 | * the calls to btrfs_invalidatepage() result in a huge amount of calls to |
| 5030 | * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting |
| 5031 | * extent_state structures over and over, wasting lots of time. |
| 5032 | * |
| 5033 | * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all |
| 5034 | * those expensive operations on a per page basis and do only the ordered io |
| 5035 | * finishing, while we release here the extent_map and extent_state structures, |
| 5036 | * without the excessive merging and splitting. |
| 5037 | */ |
| 5038 | static void evict_inode_truncate_pages(struct inode *inode) |
| 5039 | { |
| 5040 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
| 5041 | struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree; |
| 5042 | struct rb_node *node; |
| 5043 | |
| 5044 | ASSERT(inode->i_state & I_FREEING); |
Johannes Weiner | 91b0abe | 2014-04-03 14:47:49 -0700 | [diff] [blame] | 5045 | truncate_inode_pages_final(&inode->i_data); |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5046 | |
| 5047 | write_lock(&map_tree->lock); |
Liu Bo | 07e1ce0 | 2018-08-23 03:51:52 +0800 | [diff] [blame] | 5048 | while (!RB_EMPTY_ROOT(&map_tree->map.rb_root)) { |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5049 | struct extent_map *em; |
| 5050 | |
Liu Bo | 07e1ce0 | 2018-08-23 03:51:52 +0800 | [diff] [blame] | 5051 | node = rb_first_cached(&map_tree->map); |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5052 | em = rb_entry(node, struct extent_map, rb_node); |
Wang Shilong | 180589e | 2013-12-14 15:27:31 +0800 | [diff] [blame] | 5053 | clear_bit(EXTENT_FLAG_PINNED, &em->flags); |
| 5054 | clear_bit(EXTENT_FLAG_LOGGING, &em->flags); |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5055 | remove_extent_mapping(map_tree, em); |
| 5056 | free_extent_map(em); |
Filipe Manana | 7064dd5 | 2014-08-08 02:47:05 +0100 | [diff] [blame] | 5057 | if (need_resched()) { |
| 5058 | write_unlock(&map_tree->lock); |
| 5059 | cond_resched(); |
| 5060 | write_lock(&map_tree->lock); |
| 5061 | } |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5062 | } |
| 5063 | write_unlock(&map_tree->lock); |
| 5064 | |
Filipe Manana | 6ca0709 | 2015-05-26 00:55:42 +0100 | [diff] [blame] | 5065 | /* |
| 5066 | * 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] | 5067 | * We can have ongoing bios started by readahead that have |
| 5068 | * their endio callback (extent_io.c:end_bio_extent_readpage) |
Filipe Manana | 9c6429d | 2015-06-10 12:55:41 +0100 | [diff] [blame] | 5069 | * still in progress (unlocked the pages in the bio but did not yet |
| 5070 | * unlocked the ranges in the io tree). Therefore this means some |
Filipe Manana | 6ca0709 | 2015-05-26 00:55:42 +0100 | [diff] [blame] | 5071 | * ranges can still be locked and eviction started because before |
| 5072 | * submitting those bios, which are executed by a separate task (work |
| 5073 | * queue kthread), inode references (inode->i_count) were not taken |
| 5074 | * (which would be dropped in the end io callback of each bio). |
| 5075 | * Therefore here we effectively end up waiting for those bios and |
| 5076 | * anyone else holding locked ranges without having bumped the inode's |
| 5077 | * reference count - if we don't do it, when they access the inode's |
| 5078 | * io_tree to unlock a range it may be too late, leading to an |
| 5079 | * use-after-free issue. |
| 5080 | */ |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5081 | spin_lock(&io_tree->lock); |
| 5082 | while (!RB_EMPTY_ROOT(&io_tree->state)) { |
| 5083 | struct extent_state *state; |
| 5084 | struct extent_state *cached_state = NULL; |
Filipe Manana | 6ca0709 | 2015-05-26 00:55:42 +0100 | [diff] [blame] | 5085 | u64 start; |
| 5086 | u64 end; |
Filipe Manana | 421f092 | 2018-10-12 13:02:48 +0100 | [diff] [blame] | 5087 | unsigned state_flags; |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5088 | |
| 5089 | node = rb_first(&io_tree->state); |
| 5090 | state = rb_entry(node, struct extent_state, rb_node); |
Filipe Manana | 6ca0709 | 2015-05-26 00:55:42 +0100 | [diff] [blame] | 5091 | start = state->start; |
| 5092 | end = state->end; |
Filipe Manana | 421f092 | 2018-10-12 13:02:48 +0100 | [diff] [blame] | 5093 | state_flags = state->state; |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5094 | spin_unlock(&io_tree->lock); |
| 5095 | |
David Sterba | ff13db4 | 2015-12-03 14:30:40 +0100 | [diff] [blame] | 5096 | lock_extent_bits(io_tree, start, end, &cached_state); |
Qu Wenruo | b9d0b38 | 2015-09-29 10:35:16 +0800 | [diff] [blame] | 5097 | |
| 5098 | /* |
| 5099 | * If still has DELALLOC flag, the extent didn't reach disk, |
| 5100 | * and its reserved space won't be freed by delayed_ref. |
| 5101 | * So we need to free its reserved space here. |
| 5102 | * (Refer to comment in btrfs_invalidatepage, case 2) |
| 5103 | * |
| 5104 | * Note, end is the bytenr of last byte, so we need + 1 here. |
| 5105 | */ |
Filipe Manana | 421f092 | 2018-10-12 13:02:48 +0100 | [diff] [blame] | 5106 | if (state_flags & EXTENT_DELALLOC) |
Nikolay Borisov | 8b8a979 | 2020-06-03 08:55:11 +0300 | [diff] [blame] | 5107 | btrfs_qgroup_free_data(BTRFS_I(inode), NULL, start, |
| 5108 | end - start + 1); |
Qu Wenruo | b9d0b38 | 2015-09-29 10:35:16 +0800 | [diff] [blame] | 5109 | |
Filipe Manana | 6ca0709 | 2015-05-26 00:55:42 +0100 | [diff] [blame] | 5110 | clear_extent_bit(io_tree, start, end, |
Omar Sandoval | e182163 | 2019-08-15 14:04:04 -0700 | [diff] [blame] | 5111 | EXTENT_LOCKED | EXTENT_DELALLOC | |
| 5112 | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1, |
| 5113 | &cached_state); |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5114 | |
Filipe Manana | 7064dd5 | 2014-08-08 02:47:05 +0100 | [diff] [blame] | 5115 | cond_resched(); |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5116 | spin_lock(&io_tree->lock); |
| 5117 | } |
| 5118 | spin_unlock(&io_tree->lock); |
| 5119 | } |
| 5120 | |
Omar Sandoval | 4b9d7b5 | 2018-05-11 13:13:36 -0700 | [diff] [blame] | 5121 | 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] | 5122 | struct btrfs_block_rsv *rsv) |
Omar Sandoval | 4b9d7b5 | 2018-05-11 13:13:36 -0700 | [diff] [blame] | 5123 | { |
| 5124 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 5125 | struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv; |
Josef Bacik | d3984c9 | 2019-08-01 18:19:37 -0400 | [diff] [blame] | 5126 | struct btrfs_trans_handle *trans; |
Josef Bacik | 2bd36e7 | 2019-08-22 15:14:33 -0400 | [diff] [blame] | 5127 | u64 delayed_refs_extra = btrfs_calc_insert_metadata_size(fs_info, 1); |
Josef Bacik | d3984c9 | 2019-08-01 18:19:37 -0400 | [diff] [blame] | 5128 | int ret; |
Omar Sandoval | 4b9d7b5 | 2018-05-11 13:13:36 -0700 | [diff] [blame] | 5129 | |
Josef Bacik | d3984c9 | 2019-08-01 18:19:37 -0400 | [diff] [blame] | 5130 | /* |
| 5131 | * Eviction should be taking place at some place safe because of our |
| 5132 | * delayed iputs. However the normal flushing code will run delayed |
| 5133 | * iputs, so we cannot use FLUSH_ALL otherwise we'll deadlock. |
| 5134 | * |
| 5135 | * We reserve the delayed_refs_extra here again because we can't use |
| 5136 | * btrfs_start_transaction(root, 0) for the same deadlocky reason as |
| 5137 | * above. We reserve our extra bit here because we generate a ton of |
| 5138 | * delayed refs activity by truncating. |
| 5139 | * |
| 5140 | * If we cannot make our reservation we'll attempt to steal from the |
| 5141 | * global reserve, because we really want to be able to free up space. |
| 5142 | */ |
| 5143 | ret = btrfs_block_rsv_refill(root, rsv, rsv->size + delayed_refs_extra, |
| 5144 | BTRFS_RESERVE_FLUSH_EVICT); |
| 5145 | if (ret) { |
Omar Sandoval | 4b9d7b5 | 2018-05-11 13:13:36 -0700 | [diff] [blame] | 5146 | /* |
| 5147 | * Try to steal from the global reserve if there is space for |
| 5148 | * it. |
| 5149 | */ |
Josef Bacik | d3984c9 | 2019-08-01 18:19:37 -0400 | [diff] [blame] | 5150 | if (btrfs_check_space_for_delayed_refs(fs_info) || |
| 5151 | btrfs_block_rsv_migrate(global_rsv, rsv, rsv->size, 0)) { |
| 5152 | btrfs_warn(fs_info, |
| 5153 | "could not allocate space for delete; will truncate on mount"); |
| 5154 | return ERR_PTR(-ENOSPC); |
| 5155 | } |
| 5156 | delayed_refs_extra = 0; |
Omar Sandoval | 4b9d7b5 | 2018-05-11 13:13:36 -0700 | [diff] [blame] | 5157 | } |
Josef Bacik | d3984c9 | 2019-08-01 18:19:37 -0400 | [diff] [blame] | 5158 | |
| 5159 | trans = btrfs_join_transaction(root); |
| 5160 | if (IS_ERR(trans)) |
| 5161 | return trans; |
| 5162 | |
| 5163 | if (delayed_refs_extra) { |
| 5164 | trans->block_rsv = &fs_info->trans_block_rsv; |
| 5165 | trans->bytes_reserved = delayed_refs_extra; |
| 5166 | btrfs_block_rsv_migrate(rsv, trans->block_rsv, |
| 5167 | delayed_refs_extra, 1); |
| 5168 | } |
| 5169 | return trans; |
Omar Sandoval | 4b9d7b5 | 2018-05-11 13:13:36 -0700 | [diff] [blame] | 5170 | } |
| 5171 | |
Al Viro | bd55597 | 2010-06-07 11:35:40 -0400 | [diff] [blame] | 5172 | void btrfs_evict_inode(struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5173 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5174 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5175 | struct btrfs_trans_handle *trans; |
| 5176 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Omar Sandoval | 4b9d7b5 | 2018-05-11 13:13:36 -0700 | [diff] [blame] | 5177 | struct btrfs_block_rsv *rsv; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5178 | int ret; |
| 5179 | |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 5180 | trace_btrfs_inode_evict(inode); |
| 5181 | |
Nikolay Borisov | 3d48d98 | 2016-06-29 09:46:41 +0300 | [diff] [blame] | 5182 | if (!root) { |
Liu Bo | e8f1bc1 | 2018-01-25 11:02:53 -0700 | [diff] [blame] | 5183 | clear_inode(inode); |
Nikolay Borisov | 3d48d98 | 2016-06-29 09:46:41 +0300 | [diff] [blame] | 5184 | return; |
| 5185 | } |
| 5186 | |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5187 | evict_inode_truncate_pages(inode); |
| 5188 | |
Stefan Behrens | 69e9c6c | 2013-09-05 16:58:43 +0200 | [diff] [blame] | 5189 | if (inode->i_nlink && |
| 5190 | ((btrfs_root_refs(&root->root_item) != 0 && |
| 5191 | root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) || |
Nikolay Borisov | 70ddc55 | 2017-02-20 13:50:35 +0200 | [diff] [blame] | 5192 | btrfs_is_free_space_inode(BTRFS_I(inode)))) |
Al Viro | bd55597 | 2010-06-07 11:35:40 -0400 | [diff] [blame] | 5193 | goto no_delete; |
| 5194 | |
Omar Sandoval | 2791906 | 2018-05-11 13:13:37 -0700 | [diff] [blame] | 5195 | if (is_bad_inode(inode)) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5196 | goto no_delete; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5197 | |
Nikolay Borisov | 7ab7956 | 2017-02-20 13:50:57 +0200 | [diff] [blame] | 5198 | btrfs_free_io_failure_record(BTRFS_I(inode), 0, (u64)-1); |
Miao Xie | f612496 | 2014-09-12 18:44:04 +0800 | [diff] [blame] | 5199 | |
Omar Sandoval | 7b40b69 | 2018-05-11 13:13:33 -0700 | [diff] [blame] | 5200 | if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 5201 | goto no_delete; |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 5202 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5203 | if (inode->i_nlink > 0) { |
Stefan Behrens | 69e9c6c | 2013-09-05 16:58:43 +0200 | [diff] [blame] | 5204 | BUG_ON(btrfs_root_refs(&root->root_item) != 0 && |
| 5205 | root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5206 | goto no_delete; |
| 5207 | } |
| 5208 | |
Nikolay Borisov | aa79021 | 2017-01-10 20:35:40 +0200 | [diff] [blame] | 5209 | ret = btrfs_commit_inode_delayed_inode(BTRFS_I(inode)); |
Omar Sandoval | 2791906 | 2018-05-11 13:13:37 -0700 | [diff] [blame] | 5210 | if (ret) |
Miao Xie | 0e8c36a | 2012-12-19 06:59:51 +0000 | [diff] [blame] | 5211 | goto no_delete; |
Miao Xie | 0e8c36a | 2012-12-19 06:59:51 +0000 | [diff] [blame] | 5212 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5213 | rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP); |
Omar Sandoval | 2791906 | 2018-05-11 13:13:37 -0700 | [diff] [blame] | 5214 | if (!rsv) |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 5215 | goto no_delete; |
Josef Bacik | 2bd36e7 | 2019-08-22 15:14:33 -0400 | [diff] [blame] | 5216 | rsv->size = btrfs_calc_metadata_size(fs_info, 1); |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 5217 | rsv->failfast = 1; |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 5218 | |
Nikolay Borisov | 6ef06d2 | 2017-02-20 13:50:34 +0200 | [diff] [blame] | 5219 | btrfs_i_size_write(BTRFS_I(inode), 0); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5220 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5221 | while (1) { |
Josef Bacik | ad80cf5 | 2018-09-28 07:18:19 -0400 | [diff] [blame] | 5222 | trans = evict_refill_and_join(root, rsv); |
Omar Sandoval | 2791906 | 2018-05-11 13:13:37 -0700 | [diff] [blame] | 5223 | if (IS_ERR(trans)) |
| 5224 | goto free_rsv; |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 5225 | |
| 5226 | trans->block_rsv = rsv; |
| 5227 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5228 | ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0); |
Omar Sandoval | 2791906 | 2018-05-11 13:13:37 -0700 | [diff] [blame] | 5229 | trans->block_rsv = &fs_info->trans_block_rsv; |
| 5230 | btrfs_end_transaction(trans); |
| 5231 | btrfs_btree_balance_dirty(fs_info); |
| 5232 | if (ret && ret != -ENOSPC && ret != -EAGAIN) |
| 5233 | goto free_rsv; |
| 5234 | else if (!ret) |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5235 | break; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 5236 | } |
| 5237 | |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 5238 | /* |
Omar Sandoval | 2791906 | 2018-05-11 13:13:37 -0700 | [diff] [blame] | 5239 | * Errors here aren't a big deal, it just means we leave orphan items in |
| 5240 | * the tree. They will be cleaned up on the next mount. If the inode |
| 5241 | * number gets reused, cleanup deletes the orphan item without doing |
| 5242 | * anything, and unlink reuses the existing orphan item. |
| 5243 | * |
| 5244 | * If it turns out that we are dropping too many of these, we might want |
| 5245 | * to add a mechanism for retrying these after a commit. |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 5246 | */ |
Josef Bacik | ad80cf5 | 2018-09-28 07:18:19 -0400 | [diff] [blame] | 5247 | trans = evict_refill_and_join(root, rsv); |
Omar Sandoval | 2791906 | 2018-05-11 13:13:37 -0700 | [diff] [blame] | 5248 | if (!IS_ERR(trans)) { |
| 5249 | trans->block_rsv = rsv; |
| 5250 | btrfs_orphan_del(trans, BTRFS_I(inode)); |
| 5251 | trans->block_rsv = &fs_info->trans_block_rsv; |
| 5252 | btrfs_end_transaction(trans); |
| 5253 | } |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 5254 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5255 | if (!(root == fs_info->tree_root || |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 5256 | root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)) |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 5257 | btrfs_return_ino(root, btrfs_ino(BTRFS_I(inode))); |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 5258 | |
Omar Sandoval | 2791906 | 2018-05-11 13:13:37 -0700 | [diff] [blame] | 5259 | free_rsv: |
| 5260 | btrfs_free_block_rsv(fs_info, rsv); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5261 | no_delete: |
Omar Sandoval | 2791906 | 2018-05-11 13:13:37 -0700 | [diff] [blame] | 5262 | /* |
| 5263 | * If we didn't successfully delete, the orphan item will still be in |
| 5264 | * the tree and we'll retry on the next mount. Again, we might also want |
| 5265 | * to retry these periodically in the future. |
| 5266 | */ |
Nikolay Borisov | f48d1cf | 2017-01-10 20:35:39 +0200 | [diff] [blame] | 5267 | btrfs_remove_delayed_node(BTRFS_I(inode)); |
Jan Kara | dbd5768 | 2012-05-03 14:48:02 +0200 | [diff] [blame] | 5268 | clear_inode(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5269 | } |
| 5270 | |
| 5271 | /* |
Qu Wenruo | 6bf9e4b | 2019-03-13 13:55:11 +0800 | [diff] [blame] | 5272 | * Return the key found in the dir entry in the location pointer, fill @type |
| 5273 | * with BTRFS_FT_*, and return 0. |
| 5274 | * |
Su Yue | 005d671 | 2018-03-05 17:13:37 +0800 | [diff] [blame] | 5275 | * If no dir entries were found, returns -ENOENT. |
| 5276 | * If found a corrupted location in dir entry, returns -EUCLEAN. |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5277 | */ |
| 5278 | static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry, |
Qu Wenruo | 6bf9e4b | 2019-03-13 13:55:11 +0800 | [diff] [blame] | 5279 | struct btrfs_key *location, u8 *type) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5280 | { |
| 5281 | const char *name = dentry->d_name.name; |
| 5282 | int namelen = dentry->d_name.len; |
| 5283 | struct btrfs_dir_item *di; |
| 5284 | struct btrfs_path *path; |
| 5285 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Yan | 0d9f7f3 | 2007-10-25 15:48:28 -0400 | [diff] [blame] | 5286 | int ret = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5287 | |
| 5288 | path = btrfs_alloc_path(); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 5289 | if (!path) |
| 5290 | return -ENOMEM; |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 5291 | |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 5292 | di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(BTRFS_I(dir)), |
| 5293 | name, namelen, 0); |
Liu Bo | 3cf5068 | 2018-09-12 06:06:26 +0800 | [diff] [blame] | 5294 | if (IS_ERR_OR_NULL(di)) { |
| 5295 | ret = di ? PTR_ERR(di) : -ENOENT; |
Su Yue | 005d671 | 2018-03-05 17:13:37 +0800 | [diff] [blame] | 5296 | goto out; |
| 5297 | } |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5298 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5299 | btrfs_dir_item_key_to_cpu(path->nodes[0], di, location); |
Liu Bo | 56a0e70 | 2017-10-30 11:14:38 -0600 | [diff] [blame] | 5300 | if (location->type != BTRFS_INODE_ITEM_KEY && |
| 5301 | location->type != BTRFS_ROOT_ITEM_KEY) { |
Su Yue | 005d671 | 2018-03-05 17:13:37 +0800 | [diff] [blame] | 5302 | ret = -EUCLEAN; |
Liu Bo | 56a0e70 | 2017-10-30 11:14:38 -0600 | [diff] [blame] | 5303 | btrfs_warn(root->fs_info, |
| 5304 | "%s gets something invalid in DIR_ITEM (name %s, directory ino %llu, location(%llu %u %llu))", |
| 5305 | __func__, name, btrfs_ino(BTRFS_I(dir)), |
| 5306 | location->objectid, location->type, location->offset); |
Liu Bo | 56a0e70 | 2017-10-30 11:14:38 -0600 | [diff] [blame] | 5307 | } |
Qu Wenruo | 6bf9e4b | 2019-03-13 13:55:11 +0800 | [diff] [blame] | 5308 | if (!ret) |
| 5309 | *type = btrfs_dir_type(path->nodes[0], di); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5310 | out: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5311 | btrfs_free_path(path); |
| 5312 | return ret; |
| 5313 | } |
| 5314 | |
| 5315 | /* |
| 5316 | * when we hit a tree root in a directory, the btrfs part of the inode |
| 5317 | * needs to be changed to reflect the root directory of the tree root. This |
| 5318 | * is kind of like crossing a mount point. |
| 5319 | */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5320 | static int fixup_tree_root_location(struct btrfs_fs_info *fs_info, |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5321 | struct inode *dir, |
| 5322 | struct dentry *dentry, |
| 5323 | struct btrfs_key *location, |
| 5324 | struct btrfs_root **sub_root) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5325 | { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5326 | struct btrfs_path *path; |
| 5327 | struct btrfs_root *new_root; |
| 5328 | struct btrfs_root_ref *ref; |
| 5329 | struct extent_buffer *leaf; |
David Sterba | 1d4c08e | 2015-01-02 19:36:14 +0100 | [diff] [blame] | 5330 | struct btrfs_key key; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5331 | int ret; |
| 5332 | int err = 0; |
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 | path = btrfs_alloc_path(); |
| 5335 | if (!path) { |
| 5336 | err = -ENOMEM; |
| 5337 | goto out; |
| 5338 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5339 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5340 | err = -ENOENT; |
David Sterba | 1d4c08e | 2015-01-02 19:36:14 +0100 | [diff] [blame] | 5341 | key.objectid = BTRFS_I(dir)->root->root_key.objectid; |
| 5342 | key.type = BTRFS_ROOT_REF_KEY; |
| 5343 | key.offset = location->objectid; |
| 5344 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5345 | 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] | 5346 | if (ret) { |
| 5347 | if (ret < 0) |
| 5348 | err = ret; |
| 5349 | goto out; |
| 5350 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5351 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5352 | leaf = path->nodes[0]; |
| 5353 | ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref); |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 5354 | if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(BTRFS_I(dir)) || |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5355 | btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len) |
| 5356 | goto out; |
| 5357 | |
| 5358 | ret = memcmp_extent_buffer(leaf, dentry->d_name.name, |
| 5359 | (unsigned long)(ref + 1), |
| 5360 | dentry->d_name.len); |
| 5361 | if (ret) |
| 5362 | goto out; |
| 5363 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 5364 | btrfs_release_path(path); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5365 | |
David Sterba | 56e9357 | 2020-05-15 19:35:55 +0200 | [diff] [blame] | 5366 | new_root = btrfs_get_fs_root(fs_info, location->objectid, true); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5367 | if (IS_ERR(new_root)) { |
| 5368 | err = PTR_ERR(new_root); |
| 5369 | goto out; |
| 5370 | } |
| 5371 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5372 | *sub_root = new_root; |
| 5373 | location->objectid = btrfs_root_dirid(&new_root->root_item); |
| 5374 | location->type = BTRFS_INODE_ITEM_KEY; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5375 | location->offset = 0; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5376 | err = 0; |
| 5377 | out: |
| 5378 | btrfs_free_path(path); |
| 5379 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5380 | } |
| 5381 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5382 | static void inode_tree_add(struct inode *inode) |
| 5383 | { |
| 5384 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5385 | struct btrfs_inode *entry; |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 5386 | struct rb_node **p; |
| 5387 | struct rb_node *parent; |
Filipe David Borba Manana | cef2193 | 2013-09-02 12:19:13 +0100 | [diff] [blame] | 5388 | struct rb_node *new = &BTRFS_I(inode)->rb_node; |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 5389 | u64 ino = btrfs_ino(BTRFS_I(inode)); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5390 | |
Al Viro | 1d3382cb | 2010-10-23 15:19:20 -0400 | [diff] [blame] | 5391 | if (inode_unhashed(inode)) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5392 | return; |
Miao Xie | e1409ce | 2013-05-15 07:48:16 +0000 | [diff] [blame] | 5393 | parent = NULL; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5394 | spin_lock(&root->inode_lock); |
Miao Xie | e1409ce | 2013-05-15 07:48:16 +0000 | [diff] [blame] | 5395 | p = &root->inode_tree.rb_node; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5396 | while (*p) { |
| 5397 | parent = *p; |
| 5398 | entry = rb_entry(parent, struct btrfs_inode, rb_node); |
| 5399 | |
David Sterba | 3750851 | 2018-06-29 10:56:40 +0200 | [diff] [blame] | 5400 | if (ino < btrfs_ino(entry)) |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 5401 | p = &parent->rb_left; |
David Sterba | 3750851 | 2018-06-29 10:56:40 +0200 | [diff] [blame] | 5402 | else if (ino > btrfs_ino(entry)) |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 5403 | p = &parent->rb_right; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5404 | else { |
| 5405 | WARN_ON(!(entry->vfs_inode.i_state & |
Al Viro | a4ffdde | 2010-06-02 17:38:30 -0400 | [diff] [blame] | 5406 | (I_WILL_FREE | I_FREEING))); |
Filipe David Borba Manana | cef2193 | 2013-09-02 12:19:13 +0100 | [diff] [blame] | 5407 | rb_replace_node(parent, new, &root->inode_tree); |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 5408 | RB_CLEAR_NODE(parent); |
| 5409 | spin_unlock(&root->inode_lock); |
Filipe David Borba Manana | cef2193 | 2013-09-02 12:19:13 +0100 | [diff] [blame] | 5410 | return; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5411 | } |
| 5412 | } |
Filipe David Borba Manana | cef2193 | 2013-09-02 12:19:13 +0100 | [diff] [blame] | 5413 | rb_link_node(new, parent, p); |
| 5414 | rb_insert_color(new, &root->inode_tree); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5415 | spin_unlock(&root->inode_lock); |
| 5416 | } |
| 5417 | |
Nikolay Borisov | b79b724 | 2020-08-31 14:42:38 +0300 | [diff] [blame] | 5418 | static void inode_tree_del(struct btrfs_inode *inode) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5419 | { |
Nikolay Borisov | b79b724 | 2020-08-31 14:42:38 +0300 | [diff] [blame] | 5420 | struct btrfs_root *root = inode->root; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5421 | int empty = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5422 | |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 5423 | spin_lock(&root->inode_lock); |
Nikolay Borisov | b79b724 | 2020-08-31 14:42:38 +0300 | [diff] [blame] | 5424 | if (!RB_EMPTY_NODE(&inode->rb_node)) { |
| 5425 | rb_erase(&inode->rb_node, &root->inode_tree); |
| 5426 | RB_CLEAR_NODE(&inode->rb_node); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5427 | empty = RB_EMPTY_ROOT(&root->inode_tree); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5428 | } |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 5429 | spin_unlock(&root->inode_lock); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5430 | |
Stefan Behrens | 69e9c6c | 2013-09-05 16:58:43 +0200 | [diff] [blame] | 5431 | if (empty && btrfs_root_refs(&root->root_item) == 0) { |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5432 | spin_lock(&root->inode_lock); |
| 5433 | empty = RB_EMPTY_ROOT(&root->inode_tree); |
| 5434 | spin_unlock(&root->inode_lock); |
| 5435 | if (empty) |
| 5436 | btrfs_add_dead_root(root); |
| 5437 | } |
| 5438 | } |
| 5439 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5440 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5441 | static int btrfs_init_locked_inode(struct inode *inode, void *p) |
| 5442 | { |
| 5443 | struct btrfs_iget_args *args = p; |
David Sterba | 0202e83 | 2020-05-15 19:35:59 +0200 | [diff] [blame] | 5444 | |
| 5445 | inode->i_ino = args->ino; |
| 5446 | BTRFS_I(inode)->location.objectid = args->ino; |
| 5447 | BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY; |
| 5448 | BTRFS_I(inode)->location.offset = 0; |
Josef Bacik | 5c8fd99 | 2020-02-14 16:11:43 -0500 | [diff] [blame] | 5449 | BTRFS_I(inode)->root = btrfs_grab_root(args->root); |
| 5450 | BUG_ON(args->root && !BTRFS_I(inode)->root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5451 | return 0; |
| 5452 | } |
| 5453 | |
| 5454 | static int btrfs_find_actor(struct inode *inode, void *opaque) |
| 5455 | { |
| 5456 | struct btrfs_iget_args *args = opaque; |
David Sterba | 0202e83 | 2020-05-15 19:35:59 +0200 | [diff] [blame] | 5457 | |
| 5458 | return args->ino == BTRFS_I(inode)->location.objectid && |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5459 | args->root == BTRFS_I(inode)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5460 | } |
| 5461 | |
David Sterba | 0202e83 | 2020-05-15 19:35:59 +0200 | [diff] [blame] | 5462 | static struct inode *btrfs_iget_locked(struct super_block *s, u64 ino, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5463 | struct btrfs_root *root) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5464 | { |
| 5465 | struct inode *inode; |
| 5466 | struct btrfs_iget_args args; |
David Sterba | 0202e83 | 2020-05-15 19:35:59 +0200 | [diff] [blame] | 5467 | unsigned long hashval = btrfs_inode_hash(ino, root); |
Filipe David Borba Manana | 778ba82 | 2013-10-06 22:22:33 +0100 | [diff] [blame] | 5468 | |
David Sterba | 0202e83 | 2020-05-15 19:35:59 +0200 | [diff] [blame] | 5469 | args.ino = ino; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5470 | args.root = root; |
| 5471 | |
Filipe David Borba Manana | 778ba82 | 2013-10-06 22:22:33 +0100 | [diff] [blame] | 5472 | inode = iget5_locked(s, hashval, btrfs_find_actor, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5473 | btrfs_init_locked_inode, |
| 5474 | (void *)&args); |
| 5475 | return inode; |
| 5476 | } |
| 5477 | |
David Sterba | 4c66e0d | 2019-10-03 19:09:35 +0200 | [diff] [blame] | 5478 | /* |
David Sterba | 0202e83 | 2020-05-15 19:35:59 +0200 | [diff] [blame] | 5479 | * Get an inode object given its inode number and corresponding root. |
David Sterba | 4c66e0d | 2019-10-03 19:09:35 +0200 | [diff] [blame] | 5480 | * Path can be preallocated to prevent recursing back to iget through |
| 5481 | * allocator. NULL is also valid but may require an additional allocation |
| 5482 | * later. |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 5483 | */ |
David Sterba | 0202e83 | 2020-05-15 19:35:59 +0200 | [diff] [blame] | 5484 | struct inode *btrfs_iget_path(struct super_block *s, u64 ino, |
David Sterba | 4c66e0d | 2019-10-03 19:09:35 +0200 | [diff] [blame] | 5485 | struct btrfs_root *root, struct btrfs_path *path) |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 5486 | { |
| 5487 | struct inode *inode; |
| 5488 | |
David Sterba | 0202e83 | 2020-05-15 19:35:59 +0200 | [diff] [blame] | 5489 | inode = btrfs_iget_locked(s, ino, root); |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 5490 | if (!inode) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5491 | return ERR_PTR(-ENOMEM); |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 5492 | |
| 5493 | if (inode->i_state & I_NEW) { |
Filipe Manana | 6771089 | 2016-06-06 11:51:25 +0100 | [diff] [blame] | 5494 | int ret; |
| 5495 | |
Filipe Manana | 4222ea7 | 2018-10-24 10:13:03 +0100 | [diff] [blame] | 5496 | ret = btrfs_read_locked_inode(inode, path); |
Al Viro | 9bc2cef | 2018-07-29 23:04:50 +0100 | [diff] [blame] | 5497 | if (!ret) { |
Mark Fasheh | 1748f84 | 2011-07-12 11:25:31 -0700 | [diff] [blame] | 5498 | inode_tree_add(inode); |
| 5499 | unlock_new_inode(inode); |
Mark Fasheh | 1748f84 | 2011-07-12 11:25:31 -0700 | [diff] [blame] | 5500 | } else { |
Al Viro | f5b3a41 | 2018-07-29 23:04:51 +0100 | [diff] [blame] | 5501 | iget_failed(inode); |
| 5502 | /* |
| 5503 | * ret > 0 can come from btrfs_search_slot called by |
| 5504 | * btrfs_read_locked_inode, this means the inode item |
| 5505 | * was not found. |
| 5506 | */ |
| 5507 | if (ret > 0) |
| 5508 | ret = -ENOENT; |
| 5509 | inode = ERR_PTR(ret); |
Mark Fasheh | 1748f84 | 2011-07-12 11:25:31 -0700 | [diff] [blame] | 5510 | } |
| 5511 | } |
| 5512 | |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 5513 | return inode; |
| 5514 | } |
| 5515 | |
David Sterba | 0202e83 | 2020-05-15 19:35:59 +0200 | [diff] [blame] | 5516 | 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] | 5517 | { |
David Sterba | 0202e83 | 2020-05-15 19:35:59 +0200 | [diff] [blame] | 5518 | return btrfs_iget_path(s, ino, root, NULL); |
Filipe Manana | 4222ea7 | 2018-10-24 10:13:03 +0100 | [diff] [blame] | 5519 | } |
| 5520 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5521 | static struct inode *new_simple_dir(struct super_block *s, |
| 5522 | struct btrfs_key *key, |
| 5523 | struct btrfs_root *root) |
| 5524 | { |
| 5525 | struct inode *inode = new_inode(s); |
| 5526 | |
| 5527 | if (!inode) |
| 5528 | return ERR_PTR(-ENOMEM); |
| 5529 | |
Josef Bacik | 5c8fd99 | 2020-02-14 16:11:43 -0500 | [diff] [blame] | 5530 | BTRFS_I(inode)->root = btrfs_grab_root(root); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5531 | memcpy(&BTRFS_I(inode)->location, key, sizeof(*key)); |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 5532 | set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5533 | |
| 5534 | inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID; |
Omar Sandoval | 6bb6b51 | 2019-12-05 10:36:04 -0800 | [diff] [blame] | 5535 | /* |
| 5536 | * We only need lookup, the rest is read-only and there's no inode |
| 5537 | * associated with the dentry |
| 5538 | */ |
| 5539 | inode->i_op = &simple_dir_inode_operations; |
Omar Sandoval | 1fdf419 | 2017-01-25 17:06:39 -0800 | [diff] [blame] | 5540 | inode->i_opflags &= ~IOP_XATTR; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5541 | inode->i_fop = &simple_dir_operations; |
| 5542 | inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO; |
Deepa Dinamani | c2050a4 | 2016-09-14 07:48:06 -0700 | [diff] [blame] | 5543 | inode->i_mtime = current_time(inode); |
chandan r | 9cc97d6 | 2012-07-04 12:48:07 +0530 | [diff] [blame] | 5544 | inode->i_atime = inode->i_mtime; |
| 5545 | inode->i_ctime = inode->i_mtime; |
Arnd Bergmann | d3c6be6f | 2018-06-21 18:04:06 +0200 | [diff] [blame] | 5546 | BTRFS_I(inode)->i_otime = inode->i_mtime; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5547 | |
| 5548 | return inode; |
| 5549 | } |
| 5550 | |
Qu Wenruo | 6bf9e4b | 2019-03-13 13:55:11 +0800 | [diff] [blame] | 5551 | static inline u8 btrfs_inode_type(struct inode *inode) |
| 5552 | { |
| 5553 | /* |
| 5554 | * Compile-time asserts that generic FT_* types still match |
| 5555 | * BTRFS_FT_* types |
| 5556 | */ |
| 5557 | BUILD_BUG_ON(BTRFS_FT_UNKNOWN != FT_UNKNOWN); |
| 5558 | BUILD_BUG_ON(BTRFS_FT_REG_FILE != FT_REG_FILE); |
| 5559 | BUILD_BUG_ON(BTRFS_FT_DIR != FT_DIR); |
| 5560 | BUILD_BUG_ON(BTRFS_FT_CHRDEV != FT_CHRDEV); |
| 5561 | BUILD_BUG_ON(BTRFS_FT_BLKDEV != FT_BLKDEV); |
| 5562 | BUILD_BUG_ON(BTRFS_FT_FIFO != FT_FIFO); |
| 5563 | BUILD_BUG_ON(BTRFS_FT_SOCK != FT_SOCK); |
| 5564 | BUILD_BUG_ON(BTRFS_FT_SYMLINK != FT_SYMLINK); |
| 5565 | |
| 5566 | return fs_umode_to_ftype(inode->i_mode); |
| 5567 | } |
| 5568 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5569 | struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5570 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5571 | struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5572 | struct inode *inode; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5573 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5574 | struct btrfs_root *sub_root = root; |
| 5575 | struct btrfs_key location; |
Qu Wenruo | 6bf9e4b | 2019-03-13 13:55:11 +0800 | [diff] [blame] | 5576 | u8 di_type = 0; |
Josef Bacik | b4aff1f | 2011-06-28 16:18:59 -0400 | [diff] [blame] | 5577 | int ret = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5578 | |
| 5579 | if (dentry->d_name.len > BTRFS_NAME_LEN) |
| 5580 | return ERR_PTR(-ENAMETOOLONG); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5581 | |
Qu Wenruo | 6bf9e4b | 2019-03-13 13:55:11 +0800 | [diff] [blame] | 5582 | ret = btrfs_inode_by_name(dir, dentry, &location, &di_type); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5583 | if (ret < 0) |
| 5584 | return ERR_PTR(ret); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5585 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5586 | if (location.type == BTRFS_INODE_ITEM_KEY) { |
David Sterba | 0202e83 | 2020-05-15 19:35:59 +0200 | [diff] [blame] | 5587 | inode = btrfs_iget(dir->i_sb, location.objectid, root); |
Qu Wenruo | 6bf9e4b | 2019-03-13 13:55:11 +0800 | [diff] [blame] | 5588 | if (IS_ERR(inode)) |
| 5589 | return inode; |
| 5590 | |
| 5591 | /* Do extra check against inode mode with di_type */ |
| 5592 | if (btrfs_inode_type(inode) != di_type) { |
| 5593 | btrfs_crit(fs_info, |
| 5594 | "inode mode mismatch with dir: inode mode=0%o btrfs type=%u dir type=%u", |
| 5595 | inode->i_mode, btrfs_inode_type(inode), |
| 5596 | di_type); |
| 5597 | iput(inode); |
| 5598 | return ERR_PTR(-EUCLEAN); |
| 5599 | } |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5600 | return inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5601 | } |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5602 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5603 | ret = fixup_tree_root_location(fs_info, dir, dentry, |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5604 | &location, &sub_root); |
| 5605 | if (ret < 0) { |
| 5606 | if (ret != -ENOENT) |
| 5607 | inode = ERR_PTR(ret); |
| 5608 | else |
| 5609 | inode = new_simple_dir(dir->i_sb, &location, sub_root); |
| 5610 | } else { |
David Sterba | 0202e83 | 2020-05-15 19:35:59 +0200 | [diff] [blame] | 5611 | inode = btrfs_iget(dir->i_sb, location.objectid, sub_root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5612 | } |
Josef Bacik | 8727002 | 2020-01-24 09:32:31 -0500 | [diff] [blame] | 5613 | if (root != sub_root) |
Josef Bacik | 0024652 | 2020-01-24 09:33:01 -0500 | [diff] [blame] | 5614 | btrfs_put_root(sub_root); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5615 | |
Julia Lawall | 34d19ba | 2011-01-24 19:55:19 +0000 | [diff] [blame] | 5616 | if (!IS_ERR(inode) && root != sub_root) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5617 | down_read(&fs_info->cleanup_work_sem); |
David Howells | bc98a42 | 2017-07-17 08:45:34 +0100 | [diff] [blame] | 5618 | if (!sb_rdonly(inode->i_sb)) |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 5619 | ret = btrfs_orphan_cleanup(sub_root); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5620 | up_read(&fs_info->cleanup_work_sem); |
Josef Bacik | 01cd336 | 2013-06-03 21:39:49 -0400 | [diff] [blame] | 5621 | if (ret) { |
| 5622 | iput(inode); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 5623 | inode = ERR_PTR(ret); |
Josef Bacik | 01cd336 | 2013-06-03 21:39:49 -0400 | [diff] [blame] | 5624 | } |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 5625 | } |
| 5626 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5627 | return inode; |
| 5628 | } |
| 5629 | |
Nick Piggin | fe15ce4 | 2011-01-07 17:49:23 +1100 | [diff] [blame] | 5630 | static int btrfs_dentry_delete(const struct dentry *dentry) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5631 | { |
| 5632 | struct btrfs_root *root; |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 5633 | struct inode *inode = d_inode(dentry); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5634 | |
Li Zefan | 848cce0 | 2012-02-21 17:04:28 +0800 | [diff] [blame] | 5635 | if (!inode && !IS_ROOT(dentry)) |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 5636 | inode = d_inode(dentry->d_parent); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5637 | |
Li Zefan | 848cce0 | 2012-02-21 17:04:28 +0800 | [diff] [blame] | 5638 | if (inode) { |
| 5639 | root = BTRFS_I(inode)->root; |
Yan, Zheng | efefb14 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5640 | if (btrfs_root_refs(&root->root_item) == 0) |
| 5641 | return 1; |
Li Zefan | 848cce0 | 2012-02-21 17:04:28 +0800 | [diff] [blame] | 5642 | |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 5643 | if (btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) |
Li Zefan | 848cce0 | 2012-02-21 17:04:28 +0800 | [diff] [blame] | 5644 | return 1; |
Yan, Zheng | efefb14 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5645 | } |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5646 | return 0; |
| 5647 | } |
| 5648 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5649 | static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry, |
Al Viro | 00cd8dd | 2012-06-10 17:13:09 -0400 | [diff] [blame] | 5650 | unsigned int flags) |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5651 | { |
Al Viro | 3837d20 | 2018-10-10 16:38:27 -0400 | [diff] [blame] | 5652 | struct inode *inode = btrfs_lookup_dentry(dir, dentry); |
Josef Bacik | a66e7cc | 2011-09-18 10:34:03 -0400 | [diff] [blame] | 5653 | |
Al Viro | 3837d20 | 2018-10-10 16:38:27 -0400 | [diff] [blame] | 5654 | if (inode == ERR_PTR(-ENOENT)) |
| 5655 | inode = NULL; |
Al Viro | 41d28bc | 2014-10-12 22:24:21 -0400 | [diff] [blame] | 5656 | return d_splice_alias(inode, dentry); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5657 | } |
| 5658 | |
Josef Bacik | 23b5ec7 | 2017-07-24 15:14:25 -0400 | [diff] [blame] | 5659 | /* |
| 5660 | * All this infrastructure exists because dir_emit can fault, and we are holding |
| 5661 | * the tree lock when doing readdir. For now just allocate a buffer and copy |
| 5662 | * our information into that, and then dir_emit from the buffer. This is |
| 5663 | * similar to what NFS does, only we don't keep the buffer around in pagecache |
| 5664 | * because I'm afraid I'll mess that up. Long term we need to make filldir do |
| 5665 | * copy_to_user_inatomic so we don't have to worry about page faulting under the |
| 5666 | * tree lock. |
| 5667 | */ |
| 5668 | static int btrfs_opendir(struct inode *inode, struct file *file) |
| 5669 | { |
| 5670 | struct btrfs_file_private *private; |
| 5671 | |
| 5672 | private = kzalloc(sizeof(struct btrfs_file_private), GFP_KERNEL); |
| 5673 | if (!private) |
| 5674 | return -ENOMEM; |
| 5675 | private->filldir_buf = kzalloc(PAGE_SIZE, GFP_KERNEL); |
| 5676 | if (!private->filldir_buf) { |
| 5677 | kfree(private); |
| 5678 | return -ENOMEM; |
| 5679 | } |
| 5680 | file->private_data = private; |
| 5681 | return 0; |
| 5682 | } |
| 5683 | |
| 5684 | struct dir_entry { |
| 5685 | u64 ino; |
| 5686 | u64 offset; |
| 5687 | unsigned type; |
| 5688 | int name_len; |
| 5689 | }; |
| 5690 | |
| 5691 | static int btrfs_filldir(void *addr, int entries, struct dir_context *ctx) |
| 5692 | { |
| 5693 | while (entries--) { |
| 5694 | struct dir_entry *entry = addr; |
| 5695 | char *name = (char *)(entry + 1); |
| 5696 | |
David Sterba | 92d3217 | 2018-04-16 21:10:14 +0200 | [diff] [blame] | 5697 | ctx->pos = get_unaligned(&entry->offset); |
| 5698 | if (!dir_emit(ctx, name, get_unaligned(&entry->name_len), |
| 5699 | get_unaligned(&entry->ino), |
| 5700 | get_unaligned(&entry->type))) |
Josef Bacik | 23b5ec7 | 2017-07-24 15:14:25 -0400 | [diff] [blame] | 5701 | return 1; |
David Sterba | 92d3217 | 2018-04-16 21:10:14 +0200 | [diff] [blame] | 5702 | addr += sizeof(struct dir_entry) + |
| 5703 | get_unaligned(&entry->name_len); |
Josef Bacik | 23b5ec7 | 2017-07-24 15:14:25 -0400 | [diff] [blame] | 5704 | ctx->pos++; |
| 5705 | } |
| 5706 | return 0; |
| 5707 | } |
| 5708 | |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5709 | static int btrfs_real_readdir(struct file *file, struct dir_context *ctx) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5710 | { |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5711 | struct inode *inode = file_inode(file); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5712 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Josef Bacik | 23b5ec7 | 2017-07-24 15:14:25 -0400 | [diff] [blame] | 5713 | struct btrfs_file_private *private = file->private_data; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5714 | struct btrfs_dir_item *di; |
| 5715 | struct btrfs_key key; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5716 | struct btrfs_key found_key; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5717 | struct btrfs_path *path; |
Josef Bacik | 23b5ec7 | 2017-07-24 15:14:25 -0400 | [diff] [blame] | 5718 | void *addr; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5719 | struct list_head ins_list; |
| 5720 | struct list_head del_list; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5721 | int ret; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5722 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5723 | int slot; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5724 | char *name_ptr; |
| 5725 | int name_len; |
Josef Bacik | 23b5ec7 | 2017-07-24 15:14:25 -0400 | [diff] [blame] | 5726 | int entries = 0; |
| 5727 | int total_len = 0; |
Omar Sandoval | 02dbfc9 | 2016-05-20 13:50:33 -0700 | [diff] [blame] | 5728 | bool put = false; |
Jeff Mahoney | c2951f3 | 2016-11-21 15:59:04 +0100 | [diff] [blame] | 5729 | struct btrfs_key location; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5730 | |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5731 | if (!dir_emit_dots(file, ctx)) |
| 5732 | return 0; |
| 5733 | |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5734 | path = btrfs_alloc_path(); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5735 | if (!path) |
| 5736 | return -ENOMEM; |
Chris Mason | ff5714c | 2011-05-28 07:00:39 -0400 | [diff] [blame] | 5737 | |
Josef Bacik | 23b5ec7 | 2017-07-24 15:14:25 -0400 | [diff] [blame] | 5738 | addr = private->filldir_buf; |
David Sterba | e4058b5 | 2015-11-27 16:31:35 +0100 | [diff] [blame] | 5739 | path->reada = READA_FORWARD; |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5740 | |
Jeff Mahoney | c2951f3 | 2016-11-21 15:59:04 +0100 | [diff] [blame] | 5741 | INIT_LIST_HEAD(&ins_list); |
| 5742 | INIT_LIST_HEAD(&del_list); |
| 5743 | put = btrfs_readdir_get_delayed_items(inode, &ins_list, &del_list); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5744 | |
Josef Bacik | 23b5ec7 | 2017-07-24 15:14:25 -0400 | [diff] [blame] | 5745 | again: |
Jeff Mahoney | c2951f3 | 2016-11-21 15:59:04 +0100 | [diff] [blame] | 5746 | key.type = BTRFS_DIR_INDEX_KEY; |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5747 | key.offset = ctx->pos; |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 5748 | key.objectid = btrfs_ino(BTRFS_I(inode)); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5749 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5750 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 5751 | if (ret < 0) |
| 5752 | goto err; |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5753 | |
| 5754 | while (1) { |
Josef Bacik | 23b5ec7 | 2017-07-24 15:14:25 -0400 | [diff] [blame] | 5755 | struct dir_entry *entry; |
| 5756 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5757 | leaf = path->nodes[0]; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5758 | slot = path->slots[0]; |
Li Zefan | b9e03af | 2011-03-23 10:43:58 +0800 | [diff] [blame] | 5759 | if (slot >= btrfs_header_nritems(leaf)) { |
| 5760 | ret = btrfs_next_leaf(root, path); |
| 5761 | if (ret < 0) |
| 5762 | goto err; |
| 5763 | else if (ret > 0) |
| 5764 | break; |
| 5765 | continue; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5766 | } |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5767 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5768 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
| 5769 | |
| 5770 | if (found_key.objectid != key.objectid) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5771 | break; |
Jeff Mahoney | c2951f3 | 2016-11-21 15:59:04 +0100 | [diff] [blame] | 5772 | if (found_key.type != BTRFS_DIR_INDEX_KEY) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5773 | break; |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5774 | if (found_key.offset < ctx->pos) |
Li Zefan | b9e03af | 2011-03-23 10:43:58 +0800 | [diff] [blame] | 5775 | goto next; |
Jeff Mahoney | c2951f3 | 2016-11-21 15:59:04 +0100 | [diff] [blame] | 5776 | if (btrfs_should_delete_dir_index(&del_list, found_key.offset)) |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5777 | goto next; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5778 | di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item); |
Jeff Mahoney | c2951f3 | 2016-11-21 15:59:04 +0100 | [diff] [blame] | 5779 | name_len = btrfs_dir_name_len(leaf, di); |
Josef Bacik | 23b5ec7 | 2017-07-24 15:14:25 -0400 | [diff] [blame] | 5780 | if ((total_len + sizeof(struct dir_entry) + name_len) >= |
| 5781 | PAGE_SIZE) { |
| 5782 | btrfs_release_path(path); |
| 5783 | ret = btrfs_filldir(private->filldir_buf, entries, ctx); |
| 5784 | if (ret) |
| 5785 | goto nopos; |
| 5786 | addr = private->filldir_buf; |
| 5787 | entries = 0; |
| 5788 | total_len = 0; |
| 5789 | goto again; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5790 | } |
Josef Bacik | 23b5ec7 | 2017-07-24 15:14:25 -0400 | [diff] [blame] | 5791 | |
| 5792 | entry = addr; |
David Sterba | 92d3217 | 2018-04-16 21:10:14 +0200 | [diff] [blame] | 5793 | put_unaligned(name_len, &entry->name_len); |
Josef Bacik | 23b5ec7 | 2017-07-24 15:14:25 -0400 | [diff] [blame] | 5794 | name_ptr = (char *)(entry + 1); |
Jeff Mahoney | c2951f3 | 2016-11-21 15:59:04 +0100 | [diff] [blame] | 5795 | read_extent_buffer(leaf, name_ptr, (unsigned long)(di + 1), |
| 5796 | name_len); |
Phillip Potter | 7d157c3 | 2019-03-26 21:39:34 +0000 | [diff] [blame] | 5797 | put_unaligned(fs_ftype_to_dtype(btrfs_dir_type(leaf, di)), |
David Sterba | 92d3217 | 2018-04-16 21:10:14 +0200 | [diff] [blame] | 5798 | &entry->type); |
Jeff Mahoney | c2951f3 | 2016-11-21 15:59:04 +0100 | [diff] [blame] | 5799 | btrfs_dir_item_key_to_cpu(leaf, di, &location); |
David Sterba | 92d3217 | 2018-04-16 21:10:14 +0200 | [diff] [blame] | 5800 | put_unaligned(location.objectid, &entry->ino); |
| 5801 | put_unaligned(found_key.offset, &entry->offset); |
Josef Bacik | 23b5ec7 | 2017-07-24 15:14:25 -0400 | [diff] [blame] | 5802 | entries++; |
| 5803 | addr += sizeof(struct dir_entry) + name_len; |
| 5804 | total_len += sizeof(struct dir_entry) + name_len; |
Li Zefan | b9e03af | 2011-03-23 10:43:58 +0800 | [diff] [blame] | 5805 | next: |
| 5806 | path->slots[0]++; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5807 | } |
Josef Bacik | 23b5ec7 | 2017-07-24 15:14:25 -0400 | [diff] [blame] | 5808 | btrfs_release_path(path); |
| 5809 | |
| 5810 | ret = btrfs_filldir(private->filldir_buf, entries, ctx); |
| 5811 | if (ret) |
| 5812 | goto nopos; |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5813 | |
Jeff Mahoney | d2fbb2b | 2016-11-05 13:26:35 -0400 | [diff] [blame] | 5814 | ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list); |
Jeff Mahoney | c2951f3 | 2016-11-21 15:59:04 +0100 | [diff] [blame] | 5815 | if (ret) |
David Sterba | bc4ef75 | 2015-11-13 13:44:28 +0100 | [diff] [blame] | 5816 | goto nopos; |
| 5817 | |
Zach Brown | db62efb | 2013-07-11 16:19:42 -0700 | [diff] [blame] | 5818 | /* |
| 5819 | * Stop new entries from being returned after we return the last |
| 5820 | * entry. |
| 5821 | * |
| 5822 | * New directory entries are assigned a strictly increasing |
| 5823 | * offset. This means that new entries created during readdir |
| 5824 | * are *guaranteed* to be seen in the future by that readdir. |
| 5825 | * This has broken buggy programs which operate on names as |
| 5826 | * they're returned by readdir. Until we re-use freed offsets |
| 5827 | * we have this hack to stop new entries from being returned |
| 5828 | * under the assumption that they'll never reach this huge |
| 5829 | * offset. |
| 5830 | * |
| 5831 | * This is being careful not to overflow 32bit loff_t unless the |
| 5832 | * last entry requires it because doing so has broken 32bit apps |
| 5833 | * in the past. |
| 5834 | */ |
Jeff Mahoney | c2951f3 | 2016-11-21 15:59:04 +0100 | [diff] [blame] | 5835 | if (ctx->pos >= INT_MAX) |
| 5836 | ctx->pos = LLONG_MAX; |
| 5837 | else |
| 5838 | ctx->pos = INT_MAX; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5839 | nopos: |
| 5840 | ret = 0; |
| 5841 | err: |
Omar Sandoval | 02dbfc9 | 2016-05-20 13:50:33 -0700 | [diff] [blame] | 5842 | if (put) |
| 5843 | btrfs_readdir_put_delayed_items(inode, &ins_list, &del_list); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5844 | btrfs_free_path(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5845 | return ret; |
| 5846 | } |
| 5847 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5848 | /* |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 5849 | * This is somewhat expensive, updating the tree every time the |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5850 | * inode changes. But, it is most likely to find the inode in cache. |
| 5851 | * FIXME, needs more benchmarking...there are no reasons other than performance |
| 5852 | * to keep or drop this code. |
| 5853 | */ |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 5854 | static int btrfs_dirty_inode(struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5855 | { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5856 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5857 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5858 | struct btrfs_trans_handle *trans; |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5859 | int ret; |
| 5860 | |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 5861 | if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags)) |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 5862 | return 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5863 | |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 5864 | trans = btrfs_join_transaction(root); |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 5865 | if (IS_ERR(trans)) |
| 5866 | return PTR_ERR(trans); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5867 | |
| 5868 | ret = btrfs_update_inode(trans, root, inode); |
Chris Mason | 94b6044 | 2010-05-26 11:02:00 -0400 | [diff] [blame] | 5869 | if (ret && ret == -ENOSPC) { |
| 5870 | /* whoops, lets try again with the full transaction */ |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 5871 | btrfs_end_transaction(trans); |
Chris Mason | 94b6044 | 2010-05-26 11:02:00 -0400 | [diff] [blame] | 5872 | trans = btrfs_start_transaction(root, 1); |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 5873 | if (IS_ERR(trans)) |
| 5874 | return PTR_ERR(trans); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5875 | |
Chris Mason | 94b6044 | 2010-05-26 11:02:00 -0400 | [diff] [blame] | 5876 | ret = btrfs_update_inode(trans, root, inode); |
Chris Mason | 94b6044 | 2010-05-26 11:02:00 -0400 | [diff] [blame] | 5877 | } |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 5878 | btrfs_end_transaction(trans); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5879 | if (BTRFS_I(inode)->delayed_node) |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5880 | btrfs_balance_delayed_items(fs_info); |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 5881 | |
| 5882 | return ret; |
| 5883 | } |
| 5884 | |
| 5885 | /* |
| 5886 | * This is a copy of file_update_time. We need this so we can return error on |
| 5887 | * ENOSPC for updating the inode in the case of file write and mmap writes. |
| 5888 | */ |
Deepa Dinamani | 95582b0 | 2018-05-08 19:36:02 -0700 | [diff] [blame] | 5889 | static int btrfs_update_time(struct inode *inode, struct timespec64 *now, |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 5890 | int flags) |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 5891 | { |
Alexander Block | 2bc556528 | 2012-06-15 09:49:33 +0200 | [diff] [blame] | 5892 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Jeff Layton | 3a8c723 | 2017-12-11 06:35:24 -0500 | [diff] [blame] | 5893 | bool dirty = flags & ~S_VERSION; |
Alexander Block | 2bc556528 | 2012-06-15 09:49:33 +0200 | [diff] [blame] | 5894 | |
| 5895 | if (btrfs_root_readonly(root)) |
| 5896 | return -EROFS; |
| 5897 | |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 5898 | if (flags & S_VERSION) |
Jeff Layton | 3a8c723 | 2017-12-11 06:35:24 -0500 | [diff] [blame] | 5899 | dirty |= inode_maybe_inc_iversion(inode, dirty); |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 5900 | if (flags & S_CTIME) |
| 5901 | inode->i_ctime = *now; |
| 5902 | if (flags & S_MTIME) |
| 5903 | inode->i_mtime = *now; |
| 5904 | if (flags & S_ATIME) |
| 5905 | inode->i_atime = *now; |
Jeff Layton | 3a8c723 | 2017-12-11 06:35:24 -0500 | [diff] [blame] | 5906 | return dirty ? btrfs_dirty_inode(inode) : 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5907 | } |
| 5908 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 5909 | /* |
| 5910 | * find the highest existing sequence number in a directory |
| 5911 | * and then set the in-memory index_cnt variable to reflect |
| 5912 | * free sequence numbers |
| 5913 | */ |
Nikolay Borisov | 4c570655 | 2017-02-20 13:50:32 +0200 | [diff] [blame] | 5914 | static int btrfs_set_inode_index_count(struct btrfs_inode *inode) |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5915 | { |
Nikolay Borisov | 4c570655 | 2017-02-20 13:50:32 +0200 | [diff] [blame] | 5916 | struct btrfs_root *root = inode->root; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5917 | struct btrfs_key key, found_key; |
| 5918 | struct btrfs_path *path; |
| 5919 | struct extent_buffer *leaf; |
| 5920 | int ret; |
| 5921 | |
Nikolay Borisov | 4c570655 | 2017-02-20 13:50:32 +0200 | [diff] [blame] | 5922 | key.objectid = btrfs_ino(inode); |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 5923 | key.type = BTRFS_DIR_INDEX_KEY; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5924 | key.offset = (u64)-1; |
| 5925 | |
| 5926 | path = btrfs_alloc_path(); |
| 5927 | if (!path) |
| 5928 | return -ENOMEM; |
| 5929 | |
| 5930 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 5931 | if (ret < 0) |
| 5932 | goto out; |
| 5933 | /* FIXME: we should be able to handle this */ |
| 5934 | if (ret == 0) |
| 5935 | goto out; |
| 5936 | ret = 0; |
| 5937 | |
| 5938 | /* |
| 5939 | * MAGIC NUMBER EXPLANATION: |
| 5940 | * since we search a directory based on f_pos we have to start at 2 |
| 5941 | * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody |
| 5942 | * else has to start at 2 |
| 5943 | */ |
| 5944 | if (path->slots[0] == 0) { |
Nikolay Borisov | 4c570655 | 2017-02-20 13:50:32 +0200 | [diff] [blame] | 5945 | inode->index_cnt = 2; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5946 | goto out; |
| 5947 | } |
| 5948 | |
| 5949 | path->slots[0]--; |
| 5950 | |
| 5951 | leaf = path->nodes[0]; |
| 5952 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 5953 | |
Nikolay Borisov | 4c570655 | 2017-02-20 13:50:32 +0200 | [diff] [blame] | 5954 | if (found_key.objectid != btrfs_ino(inode) || |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 5955 | found_key.type != BTRFS_DIR_INDEX_KEY) { |
Nikolay Borisov | 4c570655 | 2017-02-20 13:50:32 +0200 | [diff] [blame] | 5956 | inode->index_cnt = 2; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5957 | goto out; |
| 5958 | } |
| 5959 | |
Nikolay Borisov | 4c570655 | 2017-02-20 13:50:32 +0200 | [diff] [blame] | 5960 | inode->index_cnt = found_key.offset + 1; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5961 | out: |
| 5962 | btrfs_free_path(path); |
| 5963 | return ret; |
| 5964 | } |
| 5965 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 5966 | /* |
| 5967 | * helper to find a free sequence number in a given directory. This current |
| 5968 | * code is very simple, later versions will do smarter things in the btree |
| 5969 | */ |
Nikolay Borisov | 877574e | 2017-02-20 13:50:33 +0200 | [diff] [blame] | 5970 | int btrfs_set_inode_index(struct btrfs_inode *dir, u64 *index) |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5971 | { |
| 5972 | int ret = 0; |
| 5973 | |
Nikolay Borisov | 877574e | 2017-02-20 13:50:33 +0200 | [diff] [blame] | 5974 | if (dir->index_cnt == (u64)-1) { |
| 5975 | ret = btrfs_inode_delayed_dir_index_count(dir); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5976 | if (ret) { |
| 5977 | ret = btrfs_set_inode_index_count(dir); |
| 5978 | if (ret) |
| 5979 | return ret; |
| 5980 | } |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5981 | } |
| 5982 | |
Nikolay Borisov | 877574e | 2017-02-20 13:50:33 +0200 | [diff] [blame] | 5983 | *index = dir->index_cnt; |
| 5984 | dir->index_cnt++; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5985 | |
| 5986 | return ret; |
| 5987 | } |
| 5988 | |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 5989 | static int btrfs_insert_inode_locked(struct inode *inode) |
| 5990 | { |
| 5991 | struct btrfs_iget_args args; |
David Sterba | 0202e83 | 2020-05-15 19:35:59 +0200 | [diff] [blame] | 5992 | |
| 5993 | args.ino = BTRFS_I(inode)->location.objectid; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 5994 | args.root = BTRFS_I(inode)->root; |
| 5995 | |
| 5996 | return insert_inode_locked4(inode, |
| 5997 | btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root), |
| 5998 | btrfs_find_actor, &args); |
| 5999 | } |
| 6000 | |
Anand Jain | 19aee8d | 2017-07-18 17:37:05 +0800 | [diff] [blame] | 6001 | /* |
| 6002 | * Inherit flags from the parent inode. |
| 6003 | * |
| 6004 | * Currently only the compression flags and the cow flags are inherited. |
| 6005 | */ |
| 6006 | static void btrfs_inherit_iflags(struct inode *inode, struct inode *dir) |
| 6007 | { |
| 6008 | unsigned int flags; |
| 6009 | |
| 6010 | if (!dir) |
| 6011 | return; |
| 6012 | |
| 6013 | flags = BTRFS_I(dir)->flags; |
| 6014 | |
| 6015 | if (flags & BTRFS_INODE_NOCOMPRESS) { |
| 6016 | BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS; |
| 6017 | BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS; |
| 6018 | } else if (flags & BTRFS_INODE_COMPRESS) { |
| 6019 | BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS; |
| 6020 | BTRFS_I(inode)->flags |= BTRFS_INODE_COMPRESS; |
| 6021 | } |
| 6022 | |
| 6023 | if (flags & BTRFS_INODE_NODATACOW) { |
| 6024 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW; |
| 6025 | if (S_ISREG(inode->i_mode)) |
| 6026 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM; |
| 6027 | } |
| 6028 | |
David Sterba | 7b6a221 | 2018-03-26 18:40:21 +0200 | [diff] [blame] | 6029 | btrfs_sync_inode_flags_to_i_flags(inode); |
Anand Jain | 19aee8d | 2017-07-18 17:37:05 +0800 | [diff] [blame] | 6030 | } |
| 6031 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6032 | static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans, |
| 6033 | struct btrfs_root *root, |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6034 | struct inode *dir, |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 6035 | const char *name, int name_len, |
Al Viro | 175a4eb | 2011-07-26 03:30:54 -0400 | [diff] [blame] | 6036 | u64 ref_objectid, u64 objectid, |
| 6037 | umode_t mode, u64 *index) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6038 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6039 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6040 | struct inode *inode; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6041 | struct btrfs_inode_item *inode_item; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6042 | struct btrfs_key *location; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6043 | struct btrfs_path *path; |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 6044 | struct btrfs_inode_ref *ref; |
| 6045 | struct btrfs_key key[2]; |
| 6046 | u32 sizes[2]; |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 6047 | int nitems = name ? 2 : 1; |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 6048 | unsigned long ptr; |
Josef Bacik | 11a19a9 | 2019-09-09 10:12:04 -0400 | [diff] [blame] | 6049 | unsigned int nofs_flag; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6050 | int ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6051 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6052 | path = btrfs_alloc_path(); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 6053 | if (!path) |
| 6054 | return ERR_PTR(-ENOMEM); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6055 | |
Josef Bacik | 11a19a9 | 2019-09-09 10:12:04 -0400 | [diff] [blame] | 6056 | nofs_flag = memalloc_nofs_save(); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6057 | inode = new_inode(fs_info->sb); |
Josef Bacik | 11a19a9 | 2019-09-09 10:12:04 -0400 | [diff] [blame] | 6058 | memalloc_nofs_restore(nofs_flag); |
Yoshinori Sano | 8fb2764 | 2011-04-09 02:30:07 +0000 | [diff] [blame] | 6059 | if (!inode) { |
| 6060 | btrfs_free_path(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6061 | return ERR_PTR(-ENOMEM); |
Yoshinori Sano | 8fb2764 | 2011-04-09 02:30:07 +0000 | [diff] [blame] | 6062 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6063 | |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 6064 | /* |
Filipe Manana | 5762b5c | 2014-08-01 00:10:32 +0100 | [diff] [blame] | 6065 | * O_TMPFILE, set link count to 0, so that after this point, |
| 6066 | * we fill in an inode item with the correct link count. |
| 6067 | */ |
| 6068 | if (!name) |
| 6069 | set_nlink(inode, 0); |
| 6070 | |
| 6071 | /* |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 6072 | * we have to initialize this early, so we can reclaim the inode |
| 6073 | * number if we fail afterwards in this function. |
| 6074 | */ |
| 6075 | inode->i_ino = objectid; |
| 6076 | |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 6077 | if (dir && name) { |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 6078 | trace_btrfs_inode_request(dir); |
| 6079 | |
Nikolay Borisov | 877574e | 2017-02-20 13:50:33 +0200 | [diff] [blame] | 6080 | ret = btrfs_set_inode_index(BTRFS_I(dir), index); |
Shen Feng | 0977143 | 2009-04-02 16:46:06 -0400 | [diff] [blame] | 6081 | if (ret) { |
Yoshinori Sano | 8fb2764 | 2011-04-09 02:30:07 +0000 | [diff] [blame] | 6082 | btrfs_free_path(path); |
Shen Feng | 0977143 | 2009-04-02 16:46:06 -0400 | [diff] [blame] | 6083 | iput(inode); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6084 | return ERR_PTR(ret); |
Shen Feng | 0977143 | 2009-04-02 16:46:06 -0400 | [diff] [blame] | 6085 | } |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 6086 | } else if (dir) { |
| 6087 | *index = 0; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6088 | } |
| 6089 | /* |
| 6090 | * index_cnt is ignored for everything but a dir, |
Su Yue | df6703e | 2018-01-12 11:08:02 +0800 | [diff] [blame] | 6091 | * btrfs_set_inode_index_count has an explanation for the magic |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6092 | * number |
| 6093 | */ |
| 6094 | BTRFS_I(inode)->index_cnt = 2; |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 6095 | BTRFS_I(inode)->dir_index = *index; |
Josef Bacik | 5c8fd99 | 2020-02-14 16:11:43 -0500 | [diff] [blame] | 6096 | BTRFS_I(inode)->root = btrfs_grab_root(root); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 6097 | BTRFS_I(inode)->generation = trans->transid; |
Josef Bacik | 7619585 | 2010-11-19 02:18:02 +0000 | [diff] [blame] | 6098 | inode->i_generation = BTRFS_I(inode)->generation; |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6099 | |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 6100 | /* |
| 6101 | * We could have gotten an inode number from somebody who was fsynced |
| 6102 | * and then removed in this same transaction, so let's just set full |
| 6103 | * sync since it will be a full sync anyway and this will blow away the |
| 6104 | * old info in the log. |
| 6105 | */ |
| 6106 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags); |
| 6107 | |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 6108 | key[0].objectid = objectid; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 6109 | key[0].type = BTRFS_INODE_ITEM_KEY; |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 6110 | key[0].offset = 0; |
| 6111 | |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 6112 | sizes[0] = sizeof(struct btrfs_inode_item); |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 6113 | |
| 6114 | if (name) { |
| 6115 | /* |
| 6116 | * Start new inodes with an inode_ref. This is slightly more |
| 6117 | * efficient for small numbers of hard links since they will |
| 6118 | * be packed into one item. Extended refs will kick in if we |
| 6119 | * add more hard links than can fit in the ref item. |
| 6120 | */ |
| 6121 | key[1].objectid = objectid; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 6122 | key[1].type = BTRFS_INODE_REF_KEY; |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 6123 | key[1].offset = ref_objectid; |
| 6124 | |
| 6125 | sizes[1] = name_len + sizeof(*ref); |
| 6126 | } |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 6127 | |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6128 | location = &BTRFS_I(inode)->location; |
| 6129 | location->objectid = objectid; |
| 6130 | location->offset = 0; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 6131 | location->type = BTRFS_INODE_ITEM_KEY; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6132 | |
| 6133 | ret = btrfs_insert_inode_locked(inode); |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6134 | if (ret < 0) { |
| 6135 | iput(inode); |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6136 | goto fail; |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6137 | } |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6138 | |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 6139 | ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems); |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 6140 | if (ret != 0) |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6141 | goto fail_unlock; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6142 | |
Dmitry Monakhov | ecc11fab | 2010-03-04 17:31:47 +0300 | [diff] [blame] | 6143 | inode_init_owner(inode, dir, mode); |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 6144 | inode_set_bytes(inode, 0); |
chandan r | 9cc97d6 | 2012-07-04 12:48:07 +0530 | [diff] [blame] | 6145 | |
Deepa Dinamani | c2050a4 | 2016-09-14 07:48:06 -0700 | [diff] [blame] | 6146 | inode->i_mtime = current_time(inode); |
chandan r | 9cc97d6 | 2012-07-04 12:48:07 +0530 | [diff] [blame] | 6147 | inode->i_atime = inode->i_mtime; |
| 6148 | inode->i_ctime = inode->i_mtime; |
Arnd Bergmann | d3c6be6f | 2018-06-21 18:04:06 +0200 | [diff] [blame] | 6149 | BTRFS_I(inode)->i_otime = inode->i_mtime; |
chandan r | 9cc97d6 | 2012-07-04 12:48:07 +0530 | [diff] [blame] | 6150 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6151 | inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 6152 | struct btrfs_inode_item); |
David Sterba | b159fa2 | 2016-11-08 18:09:03 +0100 | [diff] [blame] | 6153 | memzero_extent_buffer(path->nodes[0], (unsigned long)inode_item, |
Li Zefan | 293f7e0 | 2012-07-10 00:58:58 -0600 | [diff] [blame] | 6154 | sizeof(*inode_item)); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 6155 | fill_inode_item(trans, path->nodes[0], inode_item, inode); |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 6156 | |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 6157 | if (name) { |
| 6158 | ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1, |
| 6159 | struct btrfs_inode_ref); |
| 6160 | btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len); |
| 6161 | btrfs_set_inode_ref_index(path->nodes[0], ref, *index); |
| 6162 | ptr = (unsigned long)(ref + 1); |
| 6163 | write_extent_buffer(path->nodes[0], name, ptr, name_len); |
| 6164 | } |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 6165 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6166 | btrfs_mark_buffer_dirty(path->nodes[0]); |
| 6167 | btrfs_free_path(path); |
| 6168 | |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 6169 | btrfs_inherit_iflags(inode, dir); |
| 6170 | |
Al Viro | 569254b | 2011-07-24 17:08:40 -0400 | [diff] [blame] | 6171 | if (S_ISREG(mode)) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6172 | if (btrfs_test_opt(fs_info, NODATASUM)) |
Chris Mason | 9427216 | 2009-07-02 12:26:06 -0400 | [diff] [blame] | 6173 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6174 | if (btrfs_test_opt(fs_info, NODATACOW)) |
Josef Bacik | f2bdf9a | 2013-02-21 15:28:28 -0500 | [diff] [blame] | 6175 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW | |
| 6176 | BTRFS_INODE_NODATASUM; |
Chris Mason | 9427216 | 2009-07-02 12:26:06 -0400 | [diff] [blame] | 6177 | } |
| 6178 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6179 | inode_tree_add(inode); |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 6180 | |
| 6181 | trace_btrfs_inode_new(inode); |
Nikolay Borisov | d909441 | 2020-06-05 10:41:13 +0300 | [diff] [blame] | 6182 | btrfs_set_inode_last_trans(trans, BTRFS_I(inode)); |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 6183 | |
Alexander Block | 8ea05e3 | 2012-07-25 17:35:53 +0200 | [diff] [blame] | 6184 | btrfs_update_root_times(trans, root); |
| 6185 | |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 6186 | ret = btrfs_inode_inherit_props(trans, inode, dir); |
| 6187 | if (ret) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6188 | btrfs_err(fs_info, |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 6189 | "error inheriting props for ino %llu (root %llu): %d", |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 6190 | btrfs_ino(BTRFS_I(inode)), root->root_key.objectid, ret); |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 6191 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6192 | return inode; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6193 | |
| 6194 | fail_unlock: |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6195 | discard_new_inode(inode); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6196 | fail: |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 6197 | if (dir && name) |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6198 | BTRFS_I(dir)->index_cnt--; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6199 | btrfs_free_path(path); |
| 6200 | return ERR_PTR(ret); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6201 | } |
| 6202 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 6203 | /* |
| 6204 | * utility function to add 'inode' into 'parent_inode' with |
| 6205 | * a give name and a given sequence number. |
| 6206 | * if 'add_backref' is true, also insert a backref from the |
| 6207 | * inode to the parent directory. |
| 6208 | */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 6209 | int btrfs_add_link(struct btrfs_trans_handle *trans, |
Nikolay Borisov | db0a669 | 2017-02-20 13:51:08 +0200 | [diff] [blame] | 6210 | struct btrfs_inode *parent_inode, struct btrfs_inode *inode, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 6211 | const char *name, int name_len, int add_backref, u64 index) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6212 | { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6213 | int ret = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6214 | struct btrfs_key key; |
Nikolay Borisov | db0a669 | 2017-02-20 13:51:08 +0200 | [diff] [blame] | 6215 | struct btrfs_root *root = parent_inode->root; |
| 6216 | u64 ino = btrfs_ino(inode); |
| 6217 | u64 parent_ino = btrfs_ino(parent_inode); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6218 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6219 | if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) { |
Nikolay Borisov | db0a669 | 2017-02-20 13:51:08 +0200 | [diff] [blame] | 6220 | memcpy(&key, &inode->root->root_key, sizeof(key)); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6221 | } else { |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6222 | key.objectid = ino; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 6223 | key.type = BTRFS_INODE_ITEM_KEY; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6224 | key.offset = 0; |
| 6225 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6226 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6227 | if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) { |
Lu Fengqi | 6025c19 | 2018-08-01 11:32:29 +0800 | [diff] [blame] | 6228 | ret = btrfs_add_root_ref(trans, key.objectid, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6229 | root->root_key.objectid, parent_ino, |
| 6230 | index, name, name_len); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6231 | } else if (add_backref) { |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6232 | ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino, |
| 6233 | parent_ino, index); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6234 | } |
| 6235 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6236 | /* Nothing to clean up yet */ |
| 6237 | if (ret) |
| 6238 | return ret; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6239 | |
Lu Fengqi | 684572d | 2018-08-04 21:10:57 +0800 | [diff] [blame] | 6240 | ret = btrfs_insert_dir_item(trans, name, name_len, parent_inode, &key, |
Nikolay Borisov | db0a669 | 2017-02-20 13:51:08 +0200 | [diff] [blame] | 6241 | btrfs_inode_type(&inode->vfs_inode), index); |
Chris Mason | 9c52057 | 2012-12-17 14:26:57 -0500 | [diff] [blame] | 6242 | if (ret == -EEXIST || ret == -EOVERFLOW) |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6243 | goto fail_dir_item; |
| 6244 | else if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 6245 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6246 | return ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6247 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6248 | |
Nikolay Borisov | db0a669 | 2017-02-20 13:51:08 +0200 | [diff] [blame] | 6249 | btrfs_i_size_write(parent_inode, parent_inode->vfs_inode.i_size + |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6250 | name_len * 2); |
Nikolay Borisov | db0a669 | 2017-02-20 13:51:08 +0200 | [diff] [blame] | 6251 | inode_inc_iversion(&parent_inode->vfs_inode); |
Filipe Manana | 5338e43 | 2019-05-15 16:02:47 +0100 | [diff] [blame] | 6252 | /* |
| 6253 | * If we are replaying a log tree, we do not want to update the mtime |
| 6254 | * and ctime of the parent directory with the current time, since the |
| 6255 | * log replay procedure is responsible for setting them to their correct |
| 6256 | * values (the ones it had when the fsync was done). |
| 6257 | */ |
| 6258 | if (!test_bit(BTRFS_FS_LOG_RECOVERING, &root->fs_info->flags)) { |
| 6259 | struct timespec64 now = current_time(&parent_inode->vfs_inode); |
| 6260 | |
| 6261 | parent_inode->vfs_inode.i_mtime = now; |
| 6262 | parent_inode->vfs_inode.i_ctime = now; |
| 6263 | } |
Nikolay Borisov | db0a669 | 2017-02-20 13:51:08 +0200 | [diff] [blame] | 6264 | ret = btrfs_update_inode(trans, root, &parent_inode->vfs_inode); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6265 | if (ret) |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 6266 | btrfs_abort_transaction(trans, ret); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6267 | return ret; |
Chris Mason | fe66a05 | 2012-02-20 08:40:56 -0500 | [diff] [blame] | 6268 | |
| 6269 | fail_dir_item: |
| 6270 | if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) { |
| 6271 | u64 local_index; |
| 6272 | int err; |
Lu Fengqi | 3ee1c55 | 2018-08-01 11:32:28 +0800 | [diff] [blame] | 6273 | err = btrfs_del_root_ref(trans, key.objectid, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6274 | root->root_key.objectid, parent_ino, |
| 6275 | &local_index, name, name_len); |
Johannes Thumshirn | 1690dd41 | 2018-12-12 15:14:17 +0100 | [diff] [blame] | 6276 | if (err) |
| 6277 | btrfs_abort_transaction(trans, err); |
Chris Mason | fe66a05 | 2012-02-20 08:40:56 -0500 | [diff] [blame] | 6278 | } else if (add_backref) { |
| 6279 | u64 local_index; |
| 6280 | int err; |
| 6281 | |
| 6282 | err = btrfs_del_inode_ref(trans, root, name, name_len, |
| 6283 | ino, parent_ino, &local_index); |
Johannes Thumshirn | 1690dd41 | 2018-12-12 15:14:17 +0100 | [diff] [blame] | 6284 | if (err) |
| 6285 | btrfs_abort_transaction(trans, err); |
Chris Mason | fe66a05 | 2012-02-20 08:40:56 -0500 | [diff] [blame] | 6286 | } |
Johannes Thumshirn | 1690dd41 | 2018-12-12 15:14:17 +0100 | [diff] [blame] | 6287 | |
| 6288 | /* Return the original error code */ |
Chris Mason | fe66a05 | 2012-02-20 08:40:56 -0500 | [diff] [blame] | 6289 | return ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6290 | } |
| 6291 | |
| 6292 | static int btrfs_add_nondir(struct btrfs_trans_handle *trans, |
Nikolay Borisov | cef415a | 2017-02-20 13:51:09 +0200 | [diff] [blame] | 6293 | struct btrfs_inode *dir, struct dentry *dentry, |
| 6294 | struct btrfs_inode *inode, int backref, u64 index) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6295 | { |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 6296 | int err = btrfs_add_link(trans, dir, inode, |
| 6297 | dentry->d_name.name, dentry->d_name.len, |
| 6298 | backref, index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6299 | if (err > 0) |
| 6300 | err = -EEXIST; |
| 6301 | return err; |
| 6302 | } |
| 6303 | |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6304 | static int btrfs_mknod(struct inode *dir, struct dentry *dentry, |
Al Viro | 1a67aaf | 2011-07-26 01:52:52 -0400 | [diff] [blame] | 6305 | umode_t mode, dev_t rdev) |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6306 | { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6307 | struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6308 | struct btrfs_trans_handle *trans; |
| 6309 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 6310 | struct inode *inode = NULL; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6311 | int err; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6312 | u64 objectid; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 6313 | u64 index = 0; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6314 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 6315 | /* |
| 6316 | * 2 for inode item and ref |
| 6317 | * 2 for dir items |
| 6318 | * 1 for xattr if selinux is on |
| 6319 | */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6320 | trans = btrfs_start_transaction(root, 5); |
| 6321 | if (IS_ERR(trans)) |
| 6322 | return PTR_ERR(trans); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 6323 | |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 6324 | err = btrfs_find_free_ino(root, &objectid); |
| 6325 | if (err) |
| 6326 | goto out_unlock; |
| 6327 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6328 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 6329 | dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid, |
| 6330 | mode, &index); |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 6331 | if (IS_ERR(inode)) { |
| 6332 | err = PTR_ERR(inode); |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6333 | inode = NULL; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6334 | goto out_unlock; |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 6335 | } |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6336 | |
Casey Schaufler | ad19db7 | 2011-12-15 10:09:07 -0500 | [diff] [blame] | 6337 | /* |
| 6338 | * If the active LSM wants to access the inode during |
| 6339 | * d_instantiate it needs these. Smack checks to see |
| 6340 | * if the filesystem supports xattrs by looking at the |
| 6341 | * ops vector. |
| 6342 | */ |
Casey Schaufler | ad19db7 | 2011-12-15 10:09:07 -0500 | [diff] [blame] | 6343 | inode->i_op = &btrfs_special_inode_operations; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6344 | init_special_inode(inode, inode->i_mode, rdev); |
| 6345 | |
| 6346 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6347 | if (err) |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6348 | goto out_unlock; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6349 | |
Nikolay Borisov | cef415a | 2017-02-20 13:51:09 +0200 | [diff] [blame] | 6350 | err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode), |
| 6351 | 0, index); |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6352 | if (err) |
| 6353 | goto out_unlock; |
| 6354 | |
| 6355 | btrfs_update_inode(trans, root, inode); |
| 6356 | d_instantiate_new(dentry, inode); |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6357 | |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6358 | out_unlock: |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 6359 | btrfs_end_transaction(trans); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6360 | btrfs_btree_balance_dirty(fs_info); |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6361 | if (err && inode) { |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6362 | inode_dec_link_count(inode); |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6363 | discard_new_inode(inode); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6364 | } |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6365 | return err; |
| 6366 | } |
| 6367 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6368 | static int btrfs_create(struct inode *dir, struct dentry *dentry, |
Al Viro | ebfc3b4 | 2012-06-10 18:05:36 -0400 | [diff] [blame] | 6369 | umode_t mode, bool excl) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6370 | { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6371 | struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6372 | struct btrfs_trans_handle *trans; |
| 6373 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 6374 | struct inode *inode = NULL; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6375 | int err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6376 | u64 objectid; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 6377 | u64 index = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6378 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 6379 | /* |
| 6380 | * 2 for inode item and ref |
| 6381 | * 2 for dir items |
| 6382 | * 1 for xattr if selinux is on |
| 6383 | */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6384 | trans = btrfs_start_transaction(root, 5); |
| 6385 | if (IS_ERR(trans)) |
| 6386 | return PTR_ERR(trans); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 6387 | |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 6388 | err = btrfs_find_free_ino(root, &objectid); |
| 6389 | if (err) |
| 6390 | goto out_unlock; |
| 6391 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6392 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 6393 | dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid, |
| 6394 | mode, &index); |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 6395 | if (IS_ERR(inode)) { |
| 6396 | err = PTR_ERR(inode); |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6397 | inode = NULL; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6398 | goto out_unlock; |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 6399 | } |
Casey Schaufler | ad19db7 | 2011-12-15 10:09:07 -0500 | [diff] [blame] | 6400 | /* |
| 6401 | * If the active LSM wants to access the inode during |
| 6402 | * d_instantiate it needs these. Smack checks to see |
| 6403 | * if the filesystem supports xattrs by looking at the |
| 6404 | * ops vector. |
| 6405 | */ |
| 6406 | inode->i_fop = &btrfs_file_operations; |
| 6407 | inode->i_op = &btrfs_file_inode_operations; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6408 | inode->i_mapping->a_ops = &btrfs_aops; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6409 | |
| 6410 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); |
| 6411 | if (err) |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6412 | goto out_unlock; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6413 | |
| 6414 | err = btrfs_update_inode(trans, root, inode); |
| 6415 | if (err) |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6416 | goto out_unlock; |
Casey Schaufler | ad19db7 | 2011-12-15 10:09:07 -0500 | [diff] [blame] | 6417 | |
Nikolay Borisov | cef415a | 2017-02-20 13:51:09 +0200 | [diff] [blame] | 6418 | err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode), |
| 6419 | 0, index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6420 | if (err) |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6421 | goto out_unlock; |
Filipe Brandenburger | 43baa57 | 2012-11-30 03:40:09 +0000 | [diff] [blame] | 6422 | |
Al Viro | 1e2e547 | 2018-05-04 08:23:01 -0400 | [diff] [blame] | 6423 | d_instantiate_new(dentry, inode); |
Filipe Brandenburger | 43baa57 | 2012-11-30 03:40:09 +0000 | [diff] [blame] | 6424 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6425 | out_unlock: |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 6426 | btrfs_end_transaction(trans); |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6427 | if (err && inode) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6428 | inode_dec_link_count(inode); |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6429 | discard_new_inode(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6430 | } |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6431 | btrfs_btree_balance_dirty(fs_info); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6432 | return err; |
| 6433 | } |
| 6434 | |
| 6435 | static int btrfs_link(struct dentry *old_dentry, struct inode *dir, |
| 6436 | struct dentry *dentry) |
| 6437 | { |
Filipe Manana | 271dba45 | 2016-01-05 16:24:05 +0000 | [diff] [blame] | 6438 | struct btrfs_trans_handle *trans = NULL; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6439 | struct btrfs_root *root = BTRFS_I(dir)->root; |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 6440 | struct inode *inode = d_inode(old_dentry); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6441 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 6442 | u64 index; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6443 | int err; |
| 6444 | int drop_inode = 0; |
| 6445 | |
TARUISI Hiroaki | 4a8be42 | 2009-11-12 07:14:26 +0000 | [diff] [blame] | 6446 | /* 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] | 6447 | if (root->root_key.objectid != BTRFS_I(inode)->root->root_key.objectid) |
Mark Fasheh | 3ab3564 | 2011-03-22 17:20:26 +0000 | [diff] [blame] | 6448 | return -EXDEV; |
TARUISI Hiroaki | 4a8be42 | 2009-11-12 07:14:26 +0000 | [diff] [blame] | 6449 | |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 6450 | if (inode->i_nlink >= BTRFS_LINK_MAX) |
Al Viro | c055e99 | 2011-03-04 17:15:18 +0000 | [diff] [blame] | 6451 | return -EMLINK; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 6452 | |
Nikolay Borisov | 877574e | 2017-02-20 13:50:33 +0200 | [diff] [blame] | 6453 | err = btrfs_set_inode_index(BTRFS_I(dir), &index); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6454 | if (err) |
| 6455 | goto fail; |
| 6456 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6457 | /* |
Miao Xie | 7e6b646 | 2011-02-18 09:21:17 +0000 | [diff] [blame] | 6458 | * 2 items for inode and inode ref |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6459 | * 2 items for dir items |
Miao Xie | 7e6b646 | 2011-02-18 09:21:17 +0000 | [diff] [blame] | 6460 | * 1 item for parent inode |
Omar Sandoval | 399b0bb | 2018-05-11 13:13:40 -0700 | [diff] [blame] | 6461 | * 1 item for orphan item deletion if O_TMPFILE |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6462 | */ |
Omar Sandoval | 399b0bb | 2018-05-11 13:13:40 -0700 | [diff] [blame] | 6463 | trans = btrfs_start_transaction(root, inode->i_nlink ? 5 : 6); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6464 | if (IS_ERR(trans)) { |
| 6465 | err = PTR_ERR(trans); |
Filipe Manana | 271dba45 | 2016-01-05 16:24:05 +0000 | [diff] [blame] | 6466 | trans = NULL; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6467 | goto fail; |
| 6468 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6469 | |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 6470 | /* There are several dir indexes for this inode, clear the cache. */ |
| 6471 | BTRFS_I(inode)->dir_index = 0ULL; |
Zach Brown | 8b558c5 | 2013-10-16 12:10:34 -0700 | [diff] [blame] | 6472 | inc_nlink(inode); |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 6473 | inode_inc_iversion(inode); |
Deepa Dinamani | c2050a4 | 2016-09-14 07:48:06 -0700 | [diff] [blame] | 6474 | inode->i_ctime = current_time(inode); |
Al Viro | 7de9c6ee | 2010-10-23 11:11:40 -0400 | [diff] [blame] | 6475 | ihold(inode); |
Josef Bacik | e997615 | 2012-10-11 15:53:56 -0400 | [diff] [blame] | 6476 | set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6477 | |
Nikolay Borisov | cef415a | 2017-02-20 13:51:09 +0200 | [diff] [blame] | 6478 | err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode), |
| 6479 | 1, index); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6480 | |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 6481 | if (err) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6482 | drop_inode = 1; |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 6483 | } else { |
Al Viro | 10d9f30 | 2011-07-16 23:09:10 -0400 | [diff] [blame] | 6484 | struct dentry *parent = dentry->d_parent; |
Filipe Manana | d4682ba | 2018-06-11 19:24:28 +0100 | [diff] [blame] | 6485 | |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 6486 | err = btrfs_update_inode(trans, root, inode); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6487 | if (err) |
| 6488 | goto fail; |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 6489 | if (inode->i_nlink == 1) { |
| 6490 | /* |
| 6491 | * If new hard link count is 1, it's a file created |
| 6492 | * with open(2) O_TMPFILE flag. |
| 6493 | */ |
Nikolay Borisov | 3d6ae7b | 2017-02-20 13:50:58 +0200 | [diff] [blame] | 6494 | err = btrfs_orphan_del(trans, BTRFS_I(inode)); |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 6495 | if (err) |
| 6496 | goto fail; |
| 6497 | } |
Al Viro | 08c422c | 2011-12-23 07:58:13 -0500 | [diff] [blame] | 6498 | d_instantiate(dentry, inode); |
Filipe Manana | 75b463d | 2020-08-11 12:43:48 +0100 | [diff] [blame] | 6499 | btrfs_log_new_name(trans, BTRFS_I(inode), NULL, parent); |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 6500 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6501 | |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 6502 | fail: |
Filipe Manana | 271dba45 | 2016-01-05 16:24:05 +0000 | [diff] [blame] | 6503 | if (trans) |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 6504 | btrfs_end_transaction(trans); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6505 | if (drop_inode) { |
| 6506 | inode_dec_link_count(inode); |
| 6507 | iput(inode); |
| 6508 | } |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6509 | btrfs_btree_balance_dirty(fs_info); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6510 | return err; |
| 6511 | } |
| 6512 | |
Al Viro | 18bb1db | 2011-07-26 01:41:39 -0400 | [diff] [blame] | 6513 | 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] | 6514 | { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6515 | struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); |
Chris Mason | b9d8666 | 2008-05-02 16:13:49 -0400 | [diff] [blame] | 6516 | struct inode *inode = NULL; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6517 | struct btrfs_trans_handle *trans; |
| 6518 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 6519 | int err = 0; |
Chris Mason | b9d8666 | 2008-05-02 16:13:49 -0400 | [diff] [blame] | 6520 | u64 objectid = 0; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 6521 | u64 index = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6522 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 6523 | /* |
| 6524 | * 2 items for inode and ref |
| 6525 | * 2 items for dir items |
| 6526 | * 1 for xattr if selinux is on |
| 6527 | */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6528 | trans = btrfs_start_transaction(root, 5); |
| 6529 | if (IS_ERR(trans)) |
| 6530 | return PTR_ERR(trans); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6531 | |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 6532 | err = btrfs_find_free_ino(root, &objectid); |
| 6533 | if (err) |
| 6534 | goto out_fail; |
| 6535 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6536 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 6537 | dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid, |
| 6538 | S_IFDIR | mode, &index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6539 | if (IS_ERR(inode)) { |
| 6540 | err = PTR_ERR(inode); |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6541 | inode = NULL; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6542 | goto out_fail; |
| 6543 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6544 | |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6545 | /* these must be set before we unlock the inode */ |
| 6546 | inode->i_op = &btrfs_dir_inode_operations; |
| 6547 | inode->i_fop = &btrfs_dir_file_operations; |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 6548 | |
Eric Paris | 2a7dba3 | 2011-02-01 11:05:39 -0500 | [diff] [blame] | 6549 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 6550 | if (err) |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6551 | goto out_fail; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6552 | |
Nikolay Borisov | 6ef06d2 | 2017-02-20 13:50:34 +0200 | [diff] [blame] | 6553 | btrfs_i_size_write(BTRFS_I(inode), 0); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6554 | err = btrfs_update_inode(trans, root, inode); |
| 6555 | if (err) |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6556 | goto out_fail; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6557 | |
Nikolay Borisov | db0a669 | 2017-02-20 13:51:08 +0200 | [diff] [blame] | 6558 | err = btrfs_add_link(trans, BTRFS_I(dir), BTRFS_I(inode), |
| 6559 | dentry->d_name.name, |
| 6560 | dentry->d_name.len, 0, index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6561 | if (err) |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6562 | goto out_fail; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6563 | |
Al Viro | 1e2e547 | 2018-05-04 08:23:01 -0400 | [diff] [blame] | 6564 | d_instantiate_new(dentry, inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6565 | |
| 6566 | out_fail: |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 6567 | btrfs_end_transaction(trans); |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6568 | if (err && inode) { |
Wang Shilong | c7cfb8a | 2014-12-24 14:45:30 +0800 | [diff] [blame] | 6569 | inode_dec_link_count(inode); |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6570 | discard_new_inode(inode); |
Wang Shilong | c7cfb8a | 2014-12-24 14:45:30 +0800 | [diff] [blame] | 6571 | } |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6572 | btrfs_btree_balance_dirty(fs_info); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6573 | return err; |
| 6574 | } |
| 6575 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6576 | static noinline int uncompress_inline(struct btrfs_path *path, |
Byongho Lee | e40da0e | 2015-05-19 23:46:45 +0900 | [diff] [blame] | 6577 | struct page *page, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6578 | size_t pg_offset, u64 extent_offset, |
| 6579 | struct btrfs_file_extent_item *item) |
| 6580 | { |
| 6581 | int ret; |
| 6582 | struct extent_buffer *leaf = path->nodes[0]; |
| 6583 | char *tmp; |
| 6584 | size_t max_size; |
| 6585 | unsigned long inline_size; |
| 6586 | unsigned long ptr; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 6587 | int compress_type; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6588 | |
| 6589 | WARN_ON(pg_offset != 0); |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 6590 | compress_type = btrfs_file_extent_compression(leaf, item); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6591 | max_size = btrfs_file_extent_ram_bytes(leaf, item); |
| 6592 | inline_size = btrfs_file_extent_inline_item_len(leaf, |
Ross Kirk | dd3cc16 | 2013-09-16 15:58:09 +0100 | [diff] [blame] | 6593 | btrfs_item_nr(path->slots[0])); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6594 | tmp = kmalloc(inline_size, GFP_NOFS); |
Tsutomu Itoh | 8d41371 | 2011-04-25 19:43:52 -0400 | [diff] [blame] | 6595 | if (!tmp) |
| 6596 | return -ENOMEM; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6597 | ptr = btrfs_file_extent_inline_start(item); |
| 6598 | |
| 6599 | read_extent_buffer(leaf, tmp, ptr, inline_size); |
| 6600 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 6601 | max_size = min_t(unsigned long, PAGE_SIZE, max_size); |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 6602 | ret = btrfs_decompress(compress_type, tmp, page, |
| 6603 | extent_offset, inline_size, max_size); |
Zygo Blaxell | e1699d2 | 2017-03-10 16:45:44 -0500 | [diff] [blame] | 6604 | |
| 6605 | /* |
| 6606 | * decompression code contains a memset to fill in any space between the end |
| 6607 | * of the uncompressed data and the end of max_size in case the decompressed |
| 6608 | * data ends up shorter than ram_bytes. That doesn't cover the hole between |
| 6609 | * the end of an inline extent and the beginning of the next block, so we |
| 6610 | * cover that region here. |
| 6611 | */ |
| 6612 | |
| 6613 | if (max_size + pg_offset < PAGE_SIZE) { |
| 6614 | char *map = kmap(page); |
| 6615 | memset(map + pg_offset + max_size, 0, PAGE_SIZE - max_size - pg_offset); |
| 6616 | kunmap(page); |
| 6617 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6618 | kfree(tmp); |
Zach Brown | 166ae5a | 2014-05-09 17:15:10 -0400 | [diff] [blame] | 6619 | return ret; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6620 | } |
| 6621 | |
Omar Sandoval | 39b07b5 | 2019-12-02 17:34:23 -0800 | [diff] [blame] | 6622 | /** |
| 6623 | * btrfs_get_extent - Lookup the first extent overlapping a range in a file. |
| 6624 | * @inode: file to search in |
| 6625 | * @page: page to read extent data into if the extent is inline |
| 6626 | * @pg_offset: offset into @page to copy to |
| 6627 | * @start: file offset |
| 6628 | * @len: length of range starting at @start |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 6629 | * |
Omar Sandoval | 39b07b5 | 2019-12-02 17:34:23 -0800 | [diff] [blame] | 6630 | * This returns the first &struct extent_map which overlaps with the given |
| 6631 | * range, reading it from the B-tree and caching it if necessary. Note that |
| 6632 | * there may be more extents which overlap the given range after the returned |
| 6633 | * extent_map. |
| 6634 | * |
| 6635 | * If @page is not NULL and the extent is inline, this also reads the extent |
| 6636 | * data directly into the page and marks the extent up to date in the io_tree. |
| 6637 | * |
| 6638 | * Return: ERR_PTR on error, non-NULL extent_map on success. |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 6639 | */ |
Nikolay Borisov | fc4f21b1 | 2017-02-20 13:51:06 +0200 | [diff] [blame] | 6640 | struct extent_map *btrfs_get_extent(struct btrfs_inode *inode, |
Omar Sandoval | 39b07b5 | 2019-12-02 17:34:23 -0800 | [diff] [blame] | 6641 | struct page *page, size_t pg_offset, |
| 6642 | u64 start, u64 len) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6643 | { |
David Sterba | 3ffbd68 | 2018-06-29 10:56:42 +0200 | [diff] [blame] | 6644 | struct btrfs_fs_info *fs_info = inode->root->fs_info; |
Nikolay Borisov | 1028d1c | 2020-08-03 12:58:46 +0300 | [diff] [blame] | 6645 | int ret = 0; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6646 | u64 extent_start = 0; |
| 6647 | u64 extent_end = 0; |
Nikolay Borisov | fc4f21b1 | 2017-02-20 13:51:06 +0200 | [diff] [blame] | 6648 | u64 objectid = btrfs_ino(inode); |
Linus Torvalds | 7e74e23 | 2019-05-01 12:19:20 -0700 | [diff] [blame] | 6649 | int extent_type = -1; |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 6650 | struct btrfs_path *path = NULL; |
Nikolay Borisov | fc4f21b1 | 2017-02-20 13:51:06 +0200 | [diff] [blame] | 6651 | struct btrfs_root *root = inode->root; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6652 | struct btrfs_file_extent_item *item; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6653 | struct extent_buffer *leaf; |
| 6654 | struct btrfs_key found_key; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6655 | struct extent_map *em = NULL; |
Nikolay Borisov | fc4f21b1 | 2017-02-20 13:51:06 +0200 | [diff] [blame] | 6656 | struct extent_map_tree *em_tree = &inode->extent_tree; |
| 6657 | struct extent_io_tree *io_tree = &inode->io_tree; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6658 | |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 6659 | read_lock(&em_tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6660 | em = lookup_extent_mapping(em_tree, start, len); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 6661 | read_unlock(&em_tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6662 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6663 | if (em) { |
Chris Mason | e1c4b74 | 2008-04-22 13:26:46 -0400 | [diff] [blame] | 6664 | if (em->start > start || em->start + em->len <= start) |
| 6665 | free_extent_map(em); |
| 6666 | else if (em->block_start == EXTENT_MAP_INLINE && page) |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 6667 | free_extent_map(em); |
| 6668 | else |
| 6669 | goto out; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6670 | } |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 6671 | em = alloc_extent_map(); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6672 | if (!em) { |
Nikolay Borisov | 1028d1c | 2020-08-03 12:58:46 +0300 | [diff] [blame] | 6673 | ret = -ENOMEM; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6674 | goto out; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6675 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6676 | em->start = EXTENT_MAP_HOLE; |
Chris Mason | 445a694 | 2008-11-10 11:53:33 -0500 | [diff] [blame] | 6677 | em->orig_start = EXTENT_MAP_HOLE; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6678 | em->len = (u64)-1; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6679 | em->block_len = (u64)-1; |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 6680 | |
Liu Bo | bee6ec8 | 2018-08-17 05:05:28 +0800 | [diff] [blame] | 6681 | path = btrfs_alloc_path(); |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 6682 | if (!path) { |
Nikolay Borisov | 1028d1c | 2020-08-03 12:58:46 +0300 | [diff] [blame] | 6683 | ret = -ENOMEM; |
Liu Bo | bee6ec8 | 2018-08-17 05:05:28 +0800 | [diff] [blame] | 6684 | goto out; |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 6685 | } |
| 6686 | |
Liu Bo | bee6ec8 | 2018-08-17 05:05:28 +0800 | [diff] [blame] | 6687 | /* Chances are we'll be called again, so go ahead and do readahead */ |
| 6688 | path->reada = READA_FORWARD; |
Josef Bacik | 4d7240f | 2020-10-23 09:58:09 -0400 | [diff] [blame] | 6689 | |
| 6690 | /* |
| 6691 | * The same explanation in load_free_space_cache applies here as well, |
| 6692 | * we only read when we're loading the free space cache, and at that |
| 6693 | * point the commit_root has everything we need. |
| 6694 | */ |
| 6695 | if (btrfs_is_free_space_inode(inode)) { |
| 6696 | path->search_commit_root = 1; |
| 6697 | path->skip_locking = 1; |
| 6698 | } |
Josef Bacik | 5189941 | 2020-08-20 11:46:01 -0400 | [diff] [blame] | 6699 | |
Nikolay Borisov | 5c9a702 | 2017-12-01 11:19:40 +0200 | [diff] [blame] | 6700 | ret = btrfs_lookup_file_extent(NULL, root, path, objectid, start, 0); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6701 | if (ret < 0) { |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6702 | goto out; |
Nikolay Borisov | b8eeab7 | 2018-12-17 11:49:00 +0200 | [diff] [blame] | 6703 | } else if (ret > 0) { |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6704 | if (path->slots[0] == 0) |
| 6705 | goto not_found; |
| 6706 | path->slots[0]--; |
Nikolay Borisov | 1028d1c | 2020-08-03 12:58:46 +0300 | [diff] [blame] | 6707 | ret = 0; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6708 | } |
| 6709 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6710 | leaf = path->nodes[0]; |
| 6711 | item = btrfs_item_ptr(leaf, path->slots[0], |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6712 | struct btrfs_file_extent_item); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6713 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6714 | if (found_key.objectid != objectid || |
Nikolay Borisov | 694c12e | 2018-12-17 10:35:59 +0200 | [diff] [blame] | 6715 | found_key.type != BTRFS_EXTENT_DATA_KEY) { |
Josef Bacik | 25a5034 | 2013-10-14 12:08:38 -0400 | [diff] [blame] | 6716 | /* |
| 6717 | * If we backup past the first extent we want to move forward |
| 6718 | * and see if there is an extent in front of us, otherwise we'll |
| 6719 | * say there is a hole for our whole search range which can |
| 6720 | * cause problems. |
| 6721 | */ |
| 6722 | extent_end = start; |
| 6723 | goto next; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6724 | } |
| 6725 | |
Nikolay Borisov | 694c12e | 2018-12-17 10:35:59 +0200 | [diff] [blame] | 6726 | extent_type = btrfs_file_extent_type(leaf, item); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6727 | extent_start = found_key.offset; |
Filipe Manana | a5eeb3d | 2020-03-09 12:41:06 +0000 | [diff] [blame] | 6728 | extent_end = btrfs_file_extent_end(path); |
Nikolay Borisov | 694c12e | 2018-12-17 10:35:59 +0200 | [diff] [blame] | 6729 | if (extent_type == BTRFS_FILE_EXTENT_REG || |
| 6730 | extent_type == BTRFS_FILE_EXTENT_PREALLOC) { |
Qu Wenruo | 6bf9e4b | 2019-03-13 13:55:11 +0800 | [diff] [blame] | 6731 | /* Only regular file could have regular/prealloc extent */ |
| 6732 | if (!S_ISREG(inode->vfs_inode.i_mode)) { |
Nikolay Borisov | 1028d1c | 2020-08-03 12:58:46 +0300 | [diff] [blame] | 6733 | ret = -EUCLEAN; |
Qu Wenruo | 6bf9e4b | 2019-03-13 13:55:11 +0800 | [diff] [blame] | 6734 | btrfs_crit(fs_info, |
| 6735 | "regular/prealloc extent found for non-regular inode %llu", |
| 6736 | btrfs_ino(inode)); |
| 6737 | goto out; |
| 6738 | } |
Liu Bo | 09ed2f1 | 2017-03-10 11:09:48 -0800 | [diff] [blame] | 6739 | trace_btrfs_get_extent_show_fi_regular(inode, leaf, item, |
| 6740 | extent_start); |
Nikolay Borisov | 694c12e | 2018-12-17 10:35:59 +0200 | [diff] [blame] | 6741 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
Liu Bo | 09ed2f1 | 2017-03-10 11:09:48 -0800 | [diff] [blame] | 6742 | trace_btrfs_get_extent_show_fi_inline(inode, leaf, item, |
| 6743 | path->slots[0], |
| 6744 | extent_start); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6745 | } |
Josef Bacik | 25a5034 | 2013-10-14 12:08:38 -0400 | [diff] [blame] | 6746 | next: |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6747 | if (start >= extent_end) { |
| 6748 | path->slots[0]++; |
| 6749 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { |
| 6750 | ret = btrfs_next_leaf(root, path); |
Nikolay Borisov | 1028d1c | 2020-08-03 12:58:46 +0300 | [diff] [blame] | 6751 | if (ret < 0) |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6752 | goto out; |
Nikolay Borisov | 1028d1c | 2020-08-03 12:58:46 +0300 | [diff] [blame] | 6753 | else if (ret > 0) |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6754 | goto not_found; |
Nikolay Borisov | 1028d1c | 2020-08-03 12:58:46 +0300 | [diff] [blame] | 6755 | |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6756 | leaf = path->nodes[0]; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6757 | } |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6758 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 6759 | if (found_key.objectid != objectid || |
| 6760 | found_key.type != BTRFS_EXTENT_DATA_KEY) |
| 6761 | goto not_found; |
| 6762 | if (start + len <= found_key.offset) |
| 6763 | goto not_found; |
Wang Shilong | e2eca69 | 2014-07-17 11:44:14 +0800 | [diff] [blame] | 6764 | if (start > found_key.offset) |
| 6765 | goto next; |
Nikolay Borisov | 02a033d | 2018-12-17 10:36:02 +0200 | [diff] [blame] | 6766 | |
| 6767 | /* New extent overlaps with existing one */ |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6768 | em->start = start; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 6769 | em->orig_start = start; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6770 | em->len = found_key.offset - start; |
Nikolay Borisov | 02a033d | 2018-12-17 10:36:02 +0200 | [diff] [blame] | 6771 | em->block_start = EXTENT_MAP_HOLE; |
| 6772 | goto insert; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6773 | } |
| 6774 | |
Omar Sandoval | 39b07b5 | 2019-12-02 17:34:23 -0800 | [diff] [blame] | 6775 | btrfs_extent_item_to_extent_map(inode, path, item, !page, em); |
Filipe Manana | 7ffbb59 | 2014-06-09 03:48:05 +0100 | [diff] [blame] | 6776 | |
Nikolay Borisov | 694c12e | 2018-12-17 10:35:59 +0200 | [diff] [blame] | 6777 | if (extent_type == BTRFS_FILE_EXTENT_REG || |
| 6778 | extent_type == BTRFS_FILE_EXTENT_PREALLOC) { |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6779 | goto insert; |
Nikolay Borisov | 694c12e | 2018-12-17 10:35:59 +0200 | [diff] [blame] | 6780 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6781 | unsigned long ptr; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6782 | char *map; |
Chris Mason | 3326d1b | 2007-10-15 16:18:25 -0400 | [diff] [blame] | 6783 | size_t size; |
| 6784 | size_t extent_offset; |
| 6785 | size_t copy_size; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6786 | |
Omar Sandoval | 39b07b5 | 2019-12-02 17:34:23 -0800 | [diff] [blame] | 6787 | if (!page) |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 6788 | goto out; |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 6789 | |
Qu Wenruo | e41ca58 | 2018-06-06 15:41:49 +0800 | [diff] [blame] | 6790 | size = btrfs_file_extent_ram_bytes(leaf, item); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6791 | extent_offset = page_offset(page) + pg_offset - extent_start; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 6792 | copy_size = min_t(u64, PAGE_SIZE - pg_offset, |
| 6793 | size - extent_offset); |
Chris Mason | 3326d1b | 2007-10-15 16:18:25 -0400 | [diff] [blame] | 6794 | em->start = extent_start + extent_offset; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6795 | em->len = ALIGN(copy_size, fs_info->sectorsize); |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 6796 | em->orig_block_len = em->len; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 6797 | em->orig_start = em->start; |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 6798 | ptr = btrfs_file_extent_inline_start(item) + extent_offset; |
Liu Bo | e49aabd | 2018-08-25 13:47:09 +0800 | [diff] [blame] | 6799 | |
Edmund Nadolski | bf46f52 | 2017-11-20 13:24:49 -0700 | [diff] [blame] | 6800 | if (!PageUptodate(page)) { |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 6801 | if (btrfs_file_extent_compression(leaf, item) != |
| 6802 | BTRFS_COMPRESS_NONE) { |
Byongho Lee | e40da0e | 2015-05-19 23:46:45 +0900 | [diff] [blame] | 6803 | ret = uncompress_inline(path, page, pg_offset, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6804 | extent_offset, item); |
Nikolay Borisov | 1028d1c | 2020-08-03 12:58:46 +0300 | [diff] [blame] | 6805 | if (ret) |
Zach Brown | 166ae5a | 2014-05-09 17:15:10 -0400 | [diff] [blame] | 6806 | goto out; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6807 | } else { |
| 6808 | map = kmap(page); |
| 6809 | read_extent_buffer(leaf, map + pg_offset, ptr, |
| 6810 | copy_size); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 6811 | if (pg_offset + copy_size < PAGE_SIZE) { |
Chris Mason | 93c82d5 | 2009-09-11 12:36:29 -0400 | [diff] [blame] | 6812 | memset(map + pg_offset + copy_size, 0, |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 6813 | PAGE_SIZE - pg_offset - |
Chris Mason | 93c82d5 | 2009-09-11 12:36:29 -0400 | [diff] [blame] | 6814 | copy_size); |
| 6815 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6816 | kunmap(page); |
| 6817 | } |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 6818 | flush_dcache_page(page); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6819 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6820 | set_extent_uptodate(io_tree, em->start, |
Arne Jansen | 507903b | 2011-04-06 10:02:20 +0000 | [diff] [blame] | 6821 | extent_map_end(em) - 1, NULL, GFP_NOFS); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6822 | goto insert; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6823 | } |
| 6824 | not_found: |
| 6825 | em->start = start; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 6826 | em->orig_start = start; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6827 | em->len = len; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6828 | em->block_start = EXTENT_MAP_HOLE; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6829 | insert: |
Nikolay Borisov | 1028d1c | 2020-08-03 12:58:46 +0300 | [diff] [blame] | 6830 | ret = 0; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 6831 | btrfs_release_path(path); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6832 | if (em->start > start || extent_map_end(em) <= start) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6833 | btrfs_err(fs_info, |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 6834 | "bad extent! em: [%llu %llu] passed [%llu %llu]", |
| 6835 | em->start, em->len, start, len); |
Nikolay Borisov | 1028d1c | 2020-08-03 12:58:46 +0300 | [diff] [blame] | 6836 | ret = -EIO; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6837 | goto out; |
| 6838 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6839 | |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 6840 | write_lock(&em_tree->lock); |
Nikolay Borisov | 1028d1c | 2020-08-03 12:58:46 +0300 | [diff] [blame] | 6841 | ret = btrfs_add_extent_mapping(fs_info, em_tree, &em, start, len); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 6842 | write_unlock(&em_tree->lock); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6843 | out: |
Liu Bo | c641428 | 2018-08-23 07:36:17 +0800 | [diff] [blame] | 6844 | btrfs_free_path(path); |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 6845 | |
Nikolay Borisov | fc4f21b1 | 2017-02-20 13:51:06 +0200 | [diff] [blame] | 6846 | trace_btrfs_get_extent(root, inode, em); |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 6847 | |
Nikolay Borisov | 1028d1c | 2020-08-03 12:58:46 +0300 | [diff] [blame] | 6848 | if (ret) { |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6849 | free_extent_map(em); |
Nikolay Borisov | 1028d1c | 2020-08-03 12:58:46 +0300 | [diff] [blame] | 6850 | return ERR_PTR(ret); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6851 | } |
| 6852 | return em; |
| 6853 | } |
| 6854 | |
Nikolay Borisov | fc4f21b1 | 2017-02-20 13:51:06 +0200 | [diff] [blame] | 6855 | struct extent_map *btrfs_get_extent_fiemap(struct btrfs_inode *inode, |
Nikolay Borisov | 4ab47a8 | 2018-12-12 09:42:32 +0200 | [diff] [blame] | 6856 | u64 start, u64 len) |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6857 | { |
| 6858 | struct extent_map *em; |
| 6859 | struct extent_map *hole_em = NULL; |
Nikolay Borisov | f3714ef | 2018-12-12 09:42:33 +0200 | [diff] [blame] | 6860 | u64 delalloc_start = start; |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6861 | u64 end; |
Nikolay Borisov | f3714ef | 2018-12-12 09:42:33 +0200 | [diff] [blame] | 6862 | u64 delalloc_len; |
| 6863 | u64 delalloc_end; |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6864 | int err = 0; |
| 6865 | |
Omar Sandoval | 39b07b5 | 2019-12-02 17:34:23 -0800 | [diff] [blame] | 6866 | em = btrfs_get_extent(inode, NULL, 0, start, len); |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6867 | if (IS_ERR(em)) |
| 6868 | return em; |
Dan Carpenter | 9986277 | 2017-04-11 11:57:15 +0300 | [diff] [blame] | 6869 | /* |
| 6870 | * If our em maps to: |
| 6871 | * - a hole or |
| 6872 | * - a pre-alloc extent, |
| 6873 | * there might actually be delalloc bytes behind it. |
| 6874 | */ |
| 6875 | if (em->block_start != EXTENT_MAP_HOLE && |
| 6876 | !test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) |
| 6877 | return em; |
| 6878 | else |
| 6879 | hole_em = em; |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6880 | |
| 6881 | /* check to see if we've wrapped (len == -1 or similar) */ |
| 6882 | end = start + len; |
| 6883 | if (end < start) |
| 6884 | end = (u64)-1; |
| 6885 | else |
| 6886 | end -= 1; |
| 6887 | |
| 6888 | em = NULL; |
| 6889 | |
| 6890 | /* ok, we didn't find anything, lets look for delalloc */ |
Nikolay Borisov | f3714ef | 2018-12-12 09:42:33 +0200 | [diff] [blame] | 6891 | delalloc_len = count_range_bits(&inode->io_tree, &delalloc_start, |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6892 | end, len, EXTENT_DELALLOC, 1); |
Nikolay Borisov | f3714ef | 2018-12-12 09:42:33 +0200 | [diff] [blame] | 6893 | delalloc_end = delalloc_start + delalloc_len; |
| 6894 | if (delalloc_end < delalloc_start) |
| 6895 | delalloc_end = (u64)-1; |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6896 | |
| 6897 | /* |
Nikolay Borisov | f3714ef | 2018-12-12 09:42:33 +0200 | [diff] [blame] | 6898 | * We didn't find anything useful, return the original results from |
| 6899 | * get_extent() |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6900 | */ |
Nikolay Borisov | f3714ef | 2018-12-12 09:42:33 +0200 | [diff] [blame] | 6901 | if (delalloc_start > end || delalloc_end <= start) { |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6902 | em = hole_em; |
| 6903 | hole_em = NULL; |
| 6904 | goto out; |
| 6905 | } |
| 6906 | |
Nikolay Borisov | f3714ef | 2018-12-12 09:42:33 +0200 | [diff] [blame] | 6907 | /* |
| 6908 | * Adjust the delalloc_start to make sure it doesn't go backwards from |
| 6909 | * the start they passed in |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6910 | */ |
Nikolay Borisov | f3714ef | 2018-12-12 09:42:33 +0200 | [diff] [blame] | 6911 | delalloc_start = max(start, delalloc_start); |
| 6912 | delalloc_len = delalloc_end - delalloc_start; |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6913 | |
Nikolay Borisov | f3714ef | 2018-12-12 09:42:33 +0200 | [diff] [blame] | 6914 | if (delalloc_len > 0) { |
| 6915 | u64 hole_start; |
Nikolay Borisov | 02950af | 2018-12-12 09:42:34 +0200 | [diff] [blame] | 6916 | u64 hole_len; |
Nikolay Borisov | f3714ef | 2018-12-12 09:42:33 +0200 | [diff] [blame] | 6917 | const u64 hole_end = extent_map_end(hole_em); |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6918 | |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 6919 | em = alloc_extent_map(); |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6920 | if (!em) { |
| 6921 | err = -ENOMEM; |
| 6922 | goto out; |
| 6923 | } |
Nikolay Borisov | f3714ef | 2018-12-12 09:42:33 +0200 | [diff] [blame] | 6924 | |
| 6925 | ASSERT(hole_em); |
| 6926 | /* |
| 6927 | * When btrfs_get_extent can't find anything it returns one |
| 6928 | * huge hole |
| 6929 | * |
| 6930 | * Make sure what it found really fits our range, and adjust to |
| 6931 | * make sure it is based on the start from the caller |
| 6932 | */ |
| 6933 | if (hole_end <= start || hole_em->start > end) { |
| 6934 | free_extent_map(hole_em); |
| 6935 | hole_em = NULL; |
| 6936 | } else { |
| 6937 | hole_start = max(hole_em->start, start); |
| 6938 | hole_len = hole_end - hole_start; |
| 6939 | } |
| 6940 | |
| 6941 | if (hole_em && delalloc_start > hole_start) { |
| 6942 | /* |
| 6943 | * Our hole starts before our delalloc, so we have to |
| 6944 | * return just the parts of the hole that go until the |
| 6945 | * delalloc starts |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6946 | */ |
Nikolay Borisov | f3714ef | 2018-12-12 09:42:33 +0200 | [diff] [blame] | 6947 | em->len = min(hole_len, delalloc_start - hole_start); |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6948 | em->start = hole_start; |
| 6949 | em->orig_start = hole_start; |
| 6950 | /* |
Nikolay Borisov | f3714ef | 2018-12-12 09:42:33 +0200 | [diff] [blame] | 6951 | * Don't adjust block start at all, it is fixed at |
| 6952 | * EXTENT_MAP_HOLE |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6953 | */ |
| 6954 | em->block_start = hole_em->block_start; |
| 6955 | em->block_len = hole_len; |
Liu Bo | f9e4fb5 | 2013-01-07 10:10:12 +0000 | [diff] [blame] | 6956 | if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags)) |
| 6957 | set_bit(EXTENT_FLAG_PREALLOC, &em->flags); |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6958 | } else { |
Nikolay Borisov | f3714ef | 2018-12-12 09:42:33 +0200 | [diff] [blame] | 6959 | /* |
| 6960 | * Hole is out of passed range or it starts after |
| 6961 | * delalloc range |
| 6962 | */ |
| 6963 | em->start = delalloc_start; |
| 6964 | em->len = delalloc_len; |
| 6965 | em->orig_start = delalloc_start; |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6966 | em->block_start = EXTENT_MAP_DELALLOC; |
Nikolay Borisov | f3714ef | 2018-12-12 09:42:33 +0200 | [diff] [blame] | 6967 | em->block_len = delalloc_len; |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6968 | } |
Nikolay Borisov | bf8d32b | 2017-12-01 11:19:43 +0200 | [diff] [blame] | 6969 | } else { |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6970 | return hole_em; |
| 6971 | } |
| 6972 | out: |
| 6973 | |
| 6974 | free_extent_map(hole_em); |
| 6975 | if (err) { |
| 6976 | free_extent_map(em); |
| 6977 | return ERR_PTR(err); |
| 6978 | } |
| 6979 | return em; |
| 6980 | } |
| 6981 | |
Nikolay Borisov | 64f5418 | 2020-06-03 08:55:31 +0300 | [diff] [blame] | 6982 | static struct extent_map *btrfs_create_dio_extent(struct btrfs_inode *inode, |
Filipe Manana | 5f9a8a5 | 2016-05-12 13:53:36 +0100 | [diff] [blame] | 6983 | const u64 start, |
| 6984 | const u64 len, |
| 6985 | const u64 orig_start, |
| 6986 | const u64 block_start, |
| 6987 | const u64 block_len, |
| 6988 | const u64 orig_block_len, |
| 6989 | const u64 ram_bytes, |
| 6990 | const int type) |
| 6991 | { |
| 6992 | struct extent_map *em = NULL; |
| 6993 | int ret; |
| 6994 | |
Filipe Manana | 5f9a8a5 | 2016-05-12 13:53:36 +0100 | [diff] [blame] | 6995 | if (type != BTRFS_ORDERED_NOCOW) { |
Nikolay Borisov | 64f5418 | 2020-06-03 08:55:31 +0300 | [diff] [blame] | 6996 | em = create_io_em(inode, start, len, orig_start, block_start, |
| 6997 | block_len, orig_block_len, ram_bytes, |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 6998 | BTRFS_COMPRESS_NONE, /* compress_type */ |
| 6999 | type); |
Filipe Manana | 5f9a8a5 | 2016-05-12 13:53:36 +0100 | [diff] [blame] | 7000 | if (IS_ERR(em)) |
| 7001 | goto out; |
| 7002 | } |
Nikolay Borisov | 64f5418 | 2020-06-03 08:55:31 +0300 | [diff] [blame] | 7003 | ret = btrfs_add_ordered_extent_dio(inode, start, block_start, len, |
| 7004 | block_len, type); |
Filipe Manana | 5f9a8a5 | 2016-05-12 13:53:36 +0100 | [diff] [blame] | 7005 | if (ret) { |
| 7006 | if (em) { |
| 7007 | free_extent_map(em); |
Nikolay Borisov | 64f5418 | 2020-06-03 08:55:31 +0300 | [diff] [blame] | 7008 | btrfs_drop_extent_cache(inode, start, start + len - 1, 0); |
Filipe Manana | 5f9a8a5 | 2016-05-12 13:53:36 +0100 | [diff] [blame] | 7009 | } |
| 7010 | em = ERR_PTR(ret); |
| 7011 | } |
| 7012 | out: |
Filipe Manana | 5f9a8a5 | 2016-05-12 13:53:36 +0100 | [diff] [blame] | 7013 | |
| 7014 | return em; |
| 7015 | } |
| 7016 | |
Nikolay Borisov | 9fc6f91 | 2020-06-03 08:55:32 +0300 | [diff] [blame] | 7017 | static struct extent_map *btrfs_new_extent_direct(struct btrfs_inode *inode, |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7018 | u64 start, u64 len) |
| 7019 | { |
Nikolay Borisov | 9fc6f91 | 2020-06-03 08:55:32 +0300 | [diff] [blame] | 7020 | struct btrfs_root *root = inode->root; |
| 7021 | struct btrfs_fs_info *fs_info = root->fs_info; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 7022 | struct extent_map *em; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7023 | struct btrfs_key ins; |
| 7024 | u64 alloc_hint; |
| 7025 | int ret; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7026 | |
Nikolay Borisov | 9fc6f91 | 2020-06-03 08:55:32 +0300 | [diff] [blame] | 7027 | alloc_hint = get_extent_allocation_hint(inode, start, len); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7028 | ret = btrfs_reserve_extent(root, len, len, fs_info->sectorsize, |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 7029 | 0, alloc_hint, &ins, 1, 1); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 7030 | if (ret) |
| 7031 | return ERR_PTR(ret); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7032 | |
Nikolay Borisov | 9fc6f91 | 2020-06-03 08:55:32 +0300 | [diff] [blame] | 7033 | em = btrfs_create_dio_extent(inode, start, ins.offset, start, |
Filipe Manana | 5f9a8a5 | 2016-05-12 13:53:36 +0100 | [diff] [blame] | 7034 | ins.objectid, ins.offset, ins.offset, |
Liu Bo | 6288d6e | 2017-02-21 12:12:58 -0800 | [diff] [blame] | 7035 | ins.offset, BTRFS_ORDERED_REGULAR); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7036 | btrfs_dec_block_group_reservations(fs_info, ins.objectid); |
Filipe Manana | 5f9a8a5 | 2016-05-12 13:53:36 +0100 | [diff] [blame] | 7037 | if (IS_ERR(em)) |
Nikolay Borisov | 9fc6f91 | 2020-06-03 08:55:32 +0300 | [diff] [blame] | 7038 | btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, |
| 7039 | 1); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 7040 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7041 | return em; |
| 7042 | } |
| 7043 | |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7044 | /* |
Qu Wenruo | e4ecaf9 | 2020-06-24 07:23:51 +0800 | [diff] [blame] | 7045 | * Check if we can do nocow write into the range [@offset, @offset + @len) |
| 7046 | * |
| 7047 | * @offset: File offset |
| 7048 | * @len: The length to write, will be updated to the nocow writeable |
| 7049 | * range |
| 7050 | * @orig_start: (optional) Return the original file offset of the file extent |
| 7051 | * @orig_len: (optional) Return the original on-disk length of the file extent |
| 7052 | * @ram_bytes: (optional) Return the ram_bytes of the file extent |
Boris Burkov | a84d5d4 | 2020-08-18 11:00:05 -0700 | [diff] [blame] | 7053 | * @strict: if true, omit optimizations that might force us into unnecessary |
| 7054 | * cow. e.g., don't trust generation number. |
Qu Wenruo | e4ecaf9 | 2020-06-24 07:23:51 +0800 | [diff] [blame] | 7055 | * |
| 7056 | * This function will flush ordered extents in the range to ensure proper |
| 7057 | * nocow checks for (nowait == false) case. |
| 7058 | * |
| 7059 | * Return: |
| 7060 | * >0 and update @len if we can do nocow write |
| 7061 | * 0 if we can't do nocow write |
| 7062 | * <0 if error happened |
| 7063 | * |
| 7064 | * NOTE: This only checks the file extents, caller is responsible to wait for |
| 7065 | * any ordered extents. |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7066 | */ |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 7067 | noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len, |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 7068 | u64 *orig_start, u64 *orig_block_len, |
Boris Burkov | a84d5d4 | 2020-08-18 11:00:05 -0700 | [diff] [blame] | 7069 | u64 *ram_bytes, bool strict) |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7070 | { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 7071 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7072 | struct btrfs_path *path; |
| 7073 | int ret; |
| 7074 | struct extent_buffer *leaf; |
| 7075 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Miao Xie | 7b2b708 | 2014-02-27 13:58:05 +0800 | [diff] [blame] | 7076 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7077 | struct btrfs_file_extent_item *fi; |
| 7078 | struct btrfs_key key; |
| 7079 | u64 disk_bytenr; |
| 7080 | u64 backref_offset; |
| 7081 | u64 extent_end; |
| 7082 | u64 num_bytes; |
| 7083 | int slot; |
| 7084 | int found_type; |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 7085 | bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW); |
Miao Xie | e77751a | 2013-12-27 21:11:50 +0800 | [diff] [blame] | 7086 | |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7087 | path = btrfs_alloc_path(); |
| 7088 | if (!path) |
| 7089 | return -ENOMEM; |
| 7090 | |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 7091 | ret = btrfs_lookup_file_extent(NULL, root, path, |
| 7092 | btrfs_ino(BTRFS_I(inode)), offset, 0); |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7093 | if (ret < 0) |
| 7094 | goto out; |
| 7095 | |
| 7096 | slot = path->slots[0]; |
| 7097 | if (ret == 1) { |
| 7098 | if (slot == 0) { |
| 7099 | /* can't find the item, must cow */ |
| 7100 | ret = 0; |
| 7101 | goto out; |
| 7102 | } |
| 7103 | slot--; |
| 7104 | } |
| 7105 | ret = 0; |
| 7106 | leaf = path->nodes[0]; |
| 7107 | btrfs_item_key_to_cpu(leaf, &key, slot); |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 7108 | if (key.objectid != btrfs_ino(BTRFS_I(inode)) || |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7109 | key.type != BTRFS_EXTENT_DATA_KEY) { |
| 7110 | /* not our file or wrong item type, must cow */ |
| 7111 | goto out; |
| 7112 | } |
| 7113 | |
| 7114 | if (key.offset > offset) { |
| 7115 | /* Wrong offset, must cow */ |
| 7116 | goto out; |
| 7117 | } |
| 7118 | |
| 7119 | fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item); |
| 7120 | found_type = btrfs_file_extent_type(leaf, fi); |
| 7121 | if (found_type != BTRFS_FILE_EXTENT_REG && |
| 7122 | found_type != BTRFS_FILE_EXTENT_PREALLOC) { |
| 7123 | /* not a regular extent, must cow */ |
| 7124 | goto out; |
| 7125 | } |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 7126 | |
| 7127 | if (!nocow && found_type == BTRFS_FILE_EXTENT_REG) |
| 7128 | goto out; |
| 7129 | |
Miao Xie | e77751a | 2013-12-27 21:11:50 +0800 | [diff] [blame] | 7130 | extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi); |
| 7131 | if (extent_end <= offset) |
| 7132 | goto out; |
| 7133 | |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7134 | disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 7135 | if (disk_bytenr == 0) |
| 7136 | goto out; |
| 7137 | |
| 7138 | if (btrfs_file_extent_compression(leaf, fi) || |
| 7139 | btrfs_file_extent_encryption(leaf, fi) || |
| 7140 | btrfs_file_extent_other_encoding(leaf, fi)) |
| 7141 | goto out; |
| 7142 | |
Ethan Lien | 78d4295 | 2018-05-17 14:58:29 +0800 | [diff] [blame] | 7143 | /* |
| 7144 | * Do the same check as in btrfs_cross_ref_exist but without the |
| 7145 | * unnecessary search. |
| 7146 | */ |
Boris Burkov | a84d5d4 | 2020-08-18 11:00:05 -0700 | [diff] [blame] | 7147 | if (!strict && |
| 7148 | (btrfs_file_extent_generation(leaf, fi) <= |
| 7149 | btrfs_root_last_snapshot(&root->root_item))) |
Ethan Lien | 78d4295 | 2018-05-17 14:58:29 +0800 | [diff] [blame] | 7150 | goto out; |
| 7151 | |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7152 | backref_offset = btrfs_file_extent_offset(leaf, fi); |
| 7153 | |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 7154 | if (orig_start) { |
| 7155 | *orig_start = key.offset - backref_offset; |
| 7156 | *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi); |
| 7157 | *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi); |
| 7158 | } |
Josef Bacik | eb384b5 | 2013-04-24 16:32:55 -0400 | [diff] [blame] | 7159 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 7160 | if (btrfs_extent_readonly(fs_info, disk_bytenr)) |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7161 | goto out; |
Miao Xie | 7b2b708 | 2014-02-27 13:58:05 +0800 | [diff] [blame] | 7162 | |
| 7163 | num_bytes = min(offset + *len, extent_end) - offset; |
| 7164 | if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) { |
| 7165 | u64 range_end; |
| 7166 | |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 7167 | range_end = round_up(offset + num_bytes, |
| 7168 | root->fs_info->sectorsize) - 1; |
Miao Xie | 7b2b708 | 2014-02-27 13:58:05 +0800 | [diff] [blame] | 7169 | ret = test_range_bit(io_tree, offset, range_end, |
| 7170 | EXTENT_DELALLOC, 0, NULL); |
| 7171 | if (ret) { |
| 7172 | ret = -EAGAIN; |
| 7173 | goto out; |
| 7174 | } |
| 7175 | } |
| 7176 | |
Josef Bacik | 1bda19e | 2013-10-18 12:10:36 -0400 | [diff] [blame] | 7177 | btrfs_release_path(path); |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7178 | |
| 7179 | /* |
| 7180 | * look for other files referencing this extent, if we |
| 7181 | * find any we must cow |
| 7182 | */ |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 7183 | |
Liu Bo | e4c3b2d | 2017-01-30 12:25:28 -0800 | [diff] [blame] | 7184 | ret = btrfs_cross_ref_exist(root, btrfs_ino(BTRFS_I(inode)), |
Boris Burkov | a84d5d4 | 2020-08-18 11:00:05 -0700 | [diff] [blame] | 7185 | key.offset - backref_offset, disk_bytenr, |
| 7186 | strict); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 7187 | if (ret) { |
| 7188 | ret = 0; |
| 7189 | goto out; |
| 7190 | } |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7191 | |
| 7192 | /* |
| 7193 | * adjust disk_bytenr and num_bytes to cover just the bytes |
| 7194 | * in this extent we are about to write. If there |
| 7195 | * are any csums in that range we have to cow in order |
| 7196 | * to keep the csums correct |
| 7197 | */ |
| 7198 | disk_bytenr += backref_offset; |
| 7199 | disk_bytenr += offset - key.offset; |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 7200 | if (csum_exist_in_range(fs_info, disk_bytenr, num_bytes)) |
| 7201 | goto out; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7202 | /* |
| 7203 | * all of the above have passed, it is safe to overwrite this extent |
| 7204 | * without cow |
| 7205 | */ |
Josef Bacik | eb384b5 | 2013-04-24 16:32:55 -0400 | [diff] [blame] | 7206 | *len = num_bytes; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7207 | ret = 1; |
| 7208 | out: |
| 7209 | btrfs_free_path(path); |
| 7210 | return ret; |
| 7211 | } |
| 7212 | |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7213 | static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend, |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 7214 | struct extent_state **cached_state, bool writing) |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7215 | { |
| 7216 | struct btrfs_ordered_extent *ordered; |
| 7217 | int ret = 0; |
| 7218 | |
| 7219 | while (1) { |
| 7220 | lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend, |
David Sterba | ff13db4 | 2015-12-03 14:30:40 +0100 | [diff] [blame] | 7221 | cached_state); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7222 | /* |
| 7223 | * 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] | 7224 | * 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] | 7225 | * extents in this range. |
| 7226 | */ |
Nikolay Borisov | a776c6f | 2017-02-20 13:50:49 +0200 | [diff] [blame] | 7227 | ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), lockstart, |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7228 | lockend - lockstart + 1); |
| 7229 | |
| 7230 | /* |
| 7231 | * We need to make sure there are no buffered pages in this |
| 7232 | * range either, we could have raced between the invalidate in |
| 7233 | * generic_file_direct_write and locking the extent. The |
| 7234 | * invalidate needs to happen so that reads after a write do not |
| 7235 | * get stale data. |
| 7236 | */ |
Alex Gartrell | fc4adbff | 2014-05-20 13:07:56 -0700 | [diff] [blame] | 7237 | if (!ordered && |
David Sterba | 051c98e | 2018-03-07 15:33:22 +0100 | [diff] [blame] | 7238 | (!writing || !filemap_range_has_page(inode->i_mapping, |
| 7239 | lockstart, lockend))) |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7240 | break; |
| 7241 | |
| 7242 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend, |
David Sterba | e43bbe5 | 2017-12-12 21:43:52 +0100 | [diff] [blame] | 7243 | cached_state); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7244 | |
| 7245 | if (ordered) { |
Filipe Manana | ade7702 | 2016-02-18 14:28:55 +0000 | [diff] [blame] | 7246 | /* |
| 7247 | * If we are doing a DIO read and the ordered extent we |
| 7248 | * found is for a buffered write, we can not wait for it |
| 7249 | * to complete and retry, because if we do so we can |
| 7250 | * deadlock with concurrent buffered writes on page |
| 7251 | * locks. This happens only if our DIO read covers more |
| 7252 | * than one extent map, if at this point has already |
| 7253 | * created an ordered extent for a previous extent map |
| 7254 | * and locked its range in the inode's io tree, and a |
| 7255 | * concurrent write against that previous extent map's |
| 7256 | * range and this range started (we unlock the ranges |
| 7257 | * in the io tree only when the bios complete and |
| 7258 | * buffered writes always lock pages before attempting |
| 7259 | * to lock range in the io tree). |
| 7260 | */ |
| 7261 | if (writing || |
| 7262 | test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags)) |
Nikolay Borisov | c0a4360 | 2020-09-18 12:15:53 +0300 | [diff] [blame] | 7263 | btrfs_start_ordered_extent(ordered, 1); |
Filipe Manana | ade7702 | 2016-02-18 14:28:55 +0000 | [diff] [blame] | 7264 | else |
| 7265 | ret = -ENOTBLK; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7266 | btrfs_put_ordered_extent(ordered); |
| 7267 | } else { |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7268 | /* |
Filipe Manana | b850ae1 | 2015-12-08 16:23:16 +0000 | [diff] [blame] | 7269 | * We could trigger writeback for this range (and wait |
| 7270 | * for it to complete) and then invalidate the pages for |
| 7271 | * this range (through invalidate_inode_pages2_range()), |
| 7272 | * but that can lead us to a deadlock with a concurrent |
Matthew Wilcox (Oracle) | ba206a0 | 2020-06-01 21:47:05 -0700 | [diff] [blame] | 7273 | * call to readahead (a buffered read or a defrag call |
Filipe Manana | b850ae1 | 2015-12-08 16:23:16 +0000 | [diff] [blame] | 7274 | * triggered a readahead) on a page lock due to an |
| 7275 | * ordered dio extent we created before but did not have |
| 7276 | * yet a corresponding bio submitted (whence it can not |
Matthew Wilcox (Oracle) | ba206a0 | 2020-06-01 21:47:05 -0700 | [diff] [blame] | 7277 | * complete), which makes readahead wait for that |
Filipe Manana | b850ae1 | 2015-12-08 16:23:16 +0000 | [diff] [blame] | 7278 | * ordered extent to complete while holding a lock on |
| 7279 | * that page. |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7280 | */ |
Filipe Manana | b850ae1 | 2015-12-08 16:23:16 +0000 | [diff] [blame] | 7281 | ret = -ENOTBLK; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7282 | } |
| 7283 | |
Filipe Manana | ade7702 | 2016-02-18 14:28:55 +0000 | [diff] [blame] | 7284 | if (ret) |
| 7285 | break; |
| 7286 | |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7287 | cond_resched(); |
| 7288 | } |
| 7289 | |
| 7290 | return ret; |
| 7291 | } |
| 7292 | |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 7293 | /* The callers of this must take lock_extent() */ |
Nikolay Borisov | 4b67c11 | 2020-06-03 08:55:05 +0300 | [diff] [blame] | 7294 | static struct extent_map *create_io_em(struct btrfs_inode *inode, u64 start, |
| 7295 | u64 len, u64 orig_start, u64 block_start, |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 7296 | u64 block_len, u64 orig_block_len, |
| 7297 | u64 ram_bytes, int compress_type, |
| 7298 | int type) |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7299 | { |
| 7300 | struct extent_map_tree *em_tree; |
| 7301 | struct extent_map *em; |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7302 | int ret; |
| 7303 | |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 7304 | ASSERT(type == BTRFS_ORDERED_PREALLOC || |
| 7305 | type == BTRFS_ORDERED_COMPRESSED || |
| 7306 | type == BTRFS_ORDERED_NOCOW || |
Liu Bo | 1af4a0a | 2017-02-13 15:35:09 -0800 | [diff] [blame] | 7307 | type == BTRFS_ORDERED_REGULAR); |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 7308 | |
Nikolay Borisov | 4b67c11 | 2020-06-03 08:55:05 +0300 | [diff] [blame] | 7309 | em_tree = &inode->extent_tree; |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7310 | em = alloc_extent_map(); |
| 7311 | if (!em) |
| 7312 | return ERR_PTR(-ENOMEM); |
| 7313 | |
| 7314 | em->start = start; |
| 7315 | em->orig_start = orig_start; |
| 7316 | em->len = len; |
| 7317 | em->block_len = block_len; |
| 7318 | em->block_start = block_start; |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 7319 | em->orig_block_len = orig_block_len; |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 7320 | em->ram_bytes = ram_bytes; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 7321 | em->generation = -1; |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7322 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
Liu Bo | 1af4a0a | 2017-02-13 15:35:09 -0800 | [diff] [blame] | 7323 | if (type == BTRFS_ORDERED_PREALLOC) { |
Josef Bacik | b11e234 | 2012-12-03 10:58:15 -0500 | [diff] [blame] | 7324 | set_bit(EXTENT_FLAG_FILLING, &em->flags); |
Liu Bo | 1af4a0a | 2017-02-13 15:35:09 -0800 | [diff] [blame] | 7325 | } else if (type == BTRFS_ORDERED_COMPRESSED) { |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 7326 | set_bit(EXTENT_FLAG_COMPRESSED, &em->flags); |
| 7327 | em->compress_type = compress_type; |
| 7328 | } |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7329 | |
| 7330 | do { |
Nikolay Borisov | 4b67c11 | 2020-06-03 08:55:05 +0300 | [diff] [blame] | 7331 | btrfs_drop_extent_cache(inode, em->start, |
| 7332 | em->start + em->len - 1, 0); |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7333 | write_lock(&em_tree->lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 7334 | ret = add_extent_mapping(em_tree, em, 1); |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7335 | write_unlock(&em_tree->lock); |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 7336 | /* |
| 7337 | * The caller has taken lock_extent(), who could race with us |
| 7338 | * to add em? |
| 7339 | */ |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7340 | } while (ret == -EEXIST); |
| 7341 | |
| 7342 | if (ret) { |
| 7343 | free_extent_map(em); |
| 7344 | return ERR_PTR(ret); |
| 7345 | } |
| 7346 | |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 7347 | /* 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] | 7348 | return em; |
| 7349 | } |
| 7350 | |
Nikolay Borisov | 1c8d017 | 2018-05-02 15:19:32 +0300 | [diff] [blame] | 7351 | |
Nikolay Borisov | c5794e5 | 2018-05-02 15:19:33 +0300 | [diff] [blame] | 7352 | static int btrfs_get_blocks_direct_write(struct extent_map **map, |
Nikolay Borisov | c5794e5 | 2018-05-02 15:19:33 +0300 | [diff] [blame] | 7353 | struct inode *inode, |
| 7354 | struct btrfs_dio_data *dio_data, |
| 7355 | u64 start, u64 len) |
| 7356 | { |
| 7357 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
| 7358 | struct extent_map *em = *map; |
| 7359 | int ret = 0; |
| 7360 | |
| 7361 | /* |
| 7362 | * We don't allocate a new extent in the following cases |
| 7363 | * |
| 7364 | * 1) The inode is marked as NODATACOW. In this case we'll just use the |
| 7365 | * existing extent. |
| 7366 | * 2) The extent is marked as PREALLOC. We're good to go here and can |
| 7367 | * just use the extent. |
| 7368 | * |
| 7369 | */ |
| 7370 | if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) || |
| 7371 | ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) && |
| 7372 | em->block_start != EXTENT_MAP_HOLE)) { |
| 7373 | int type; |
| 7374 | u64 block_start, orig_start, orig_block_len, ram_bytes; |
| 7375 | |
| 7376 | if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) |
| 7377 | type = BTRFS_ORDERED_PREALLOC; |
| 7378 | else |
| 7379 | type = BTRFS_ORDERED_NOCOW; |
| 7380 | len = min(len, em->len - (start - em->start)); |
| 7381 | block_start = em->block_start + (start - em->start); |
| 7382 | |
| 7383 | if (can_nocow_extent(inode, start, &len, &orig_start, |
Boris Burkov | a84d5d4 | 2020-08-18 11:00:05 -0700 | [diff] [blame] | 7384 | &orig_block_len, &ram_bytes, false) == 1 && |
Nikolay Borisov | c5794e5 | 2018-05-02 15:19:33 +0300 | [diff] [blame] | 7385 | btrfs_inc_nocow_writers(fs_info, block_start)) { |
| 7386 | struct extent_map *em2; |
| 7387 | |
Nikolay Borisov | 64f5418 | 2020-06-03 08:55:31 +0300 | [diff] [blame] | 7388 | em2 = btrfs_create_dio_extent(BTRFS_I(inode), start, len, |
Nikolay Borisov | c5794e5 | 2018-05-02 15:19:33 +0300 | [diff] [blame] | 7389 | orig_start, block_start, |
| 7390 | len, orig_block_len, |
| 7391 | ram_bytes, type); |
| 7392 | btrfs_dec_nocow_writers(fs_info, block_start); |
| 7393 | if (type == BTRFS_ORDERED_PREALLOC) { |
| 7394 | free_extent_map(em); |
| 7395 | *map = em = em2; |
| 7396 | } |
| 7397 | |
| 7398 | if (em2 && IS_ERR(em2)) { |
| 7399 | ret = PTR_ERR(em2); |
| 7400 | goto out; |
| 7401 | } |
| 7402 | /* |
| 7403 | * For inode marked NODATACOW or extent marked PREALLOC, |
| 7404 | * use the existing or preallocated extent, so does not |
| 7405 | * need to adjust btrfs_space_info's bytes_may_use. |
| 7406 | */ |
Nikolay Borisov | 9db5d51 | 2020-06-03 08:55:38 +0300 | [diff] [blame] | 7407 | btrfs_free_reserved_data_space_noquota(fs_info, len); |
Nikolay Borisov | c5794e5 | 2018-05-02 15:19:33 +0300 | [diff] [blame] | 7408 | goto skip_cow; |
| 7409 | } |
| 7410 | } |
| 7411 | |
| 7412 | /* this will cow the extent */ |
Nikolay Borisov | c5794e5 | 2018-05-02 15:19:33 +0300 | [diff] [blame] | 7413 | free_extent_map(em); |
Nikolay Borisov | 9fc6f91 | 2020-06-03 08:55:32 +0300 | [diff] [blame] | 7414 | *map = em = btrfs_new_extent_direct(BTRFS_I(inode), start, len); |
Nikolay Borisov | c5794e5 | 2018-05-02 15:19:33 +0300 | [diff] [blame] | 7415 | if (IS_ERR(em)) { |
| 7416 | ret = PTR_ERR(em); |
| 7417 | goto out; |
| 7418 | } |
| 7419 | |
| 7420 | len = min(len, em->len - (start - em->start)); |
| 7421 | |
| 7422 | skip_cow: |
Nikolay Borisov | c5794e5 | 2018-05-02 15:19:33 +0300 | [diff] [blame] | 7423 | /* |
| 7424 | * Need to update the i_size under the extent lock so buffered |
| 7425 | * readers will get the updated i_size when we unlock. |
| 7426 | */ |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 7427 | if (start + len > i_size_read(inode)) |
Nikolay Borisov | c5794e5 | 2018-05-02 15:19:33 +0300 | [diff] [blame] | 7428 | i_size_write(inode, start + len); |
| 7429 | |
Nikolay Borisov | c5794e5 | 2018-05-02 15:19:33 +0300 | [diff] [blame] | 7430 | dio_data->reserve -= len; |
Nikolay Borisov | c5794e5 | 2018-05-02 15:19:33 +0300 | [diff] [blame] | 7431 | out: |
| 7432 | return ret; |
| 7433 | } |
| 7434 | |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 7435 | static int btrfs_dio_iomap_begin(struct inode *inode, loff_t start, |
| 7436 | loff_t length, unsigned int flags, struct iomap *iomap, |
| 7437 | struct iomap *srcmap) |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7438 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7439 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7440 | struct extent_map *em; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7441 | struct extent_state *cached_state = NULL; |
chandan | 50745b0 | 2015-08-28 21:10:13 +0530 | [diff] [blame] | 7442 | struct btrfs_dio_data *dio_data = NULL; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7443 | u64 lockstart, lockend; |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 7444 | const bool write = !!(flags & IOMAP_WRITE); |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 7445 | int ret = 0; |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 7446 | u64 len = length; |
| 7447 | bool unlock_extents = false; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7448 | |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 7449 | if (!write) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7450 | len = min_t(u64, len, fs_info->sectorsize); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7451 | |
Josef Bacik | c329861 | 2012-08-03 16:49:19 -0400 | [diff] [blame] | 7452 | lockstart = start; |
| 7453 | lockend = start + len - 1; |
| 7454 | |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 7455 | /* |
| 7456 | * The generic stuff only does filemap_write_and_wait_range, which |
| 7457 | * isn't enough if we've written compressed pages to this area, so we |
| 7458 | * need to flush the dirty pages again to make absolutely sure that any |
| 7459 | * outstanding dirty pages are on disk. |
| 7460 | */ |
| 7461 | if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, |
| 7462 | &BTRFS_I(inode)->runtime_flags)) { |
| 7463 | ret = filemap_fdatawrite_range(inode->i_mapping, start, |
| 7464 | start + length - 1); |
| 7465 | if (ret) |
| 7466 | return ret; |
Josef Bacik | e1cbbfa | 2015-03-17 10:52:28 -0400 | [diff] [blame] | 7467 | } |
| 7468 | |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 7469 | dio_data = kzalloc(sizeof(*dio_data), GFP_NOFS); |
| 7470 | if (!dio_data) |
| 7471 | return -ENOMEM; |
| 7472 | |
| 7473 | dio_data->length = length; |
| 7474 | if (write) { |
| 7475 | dio_data->reserve = round_up(length, fs_info->sectorsize); |
| 7476 | ret = btrfs_delalloc_reserve_space(BTRFS_I(inode), |
| 7477 | &dio_data->data_reserved, |
| 7478 | start, dio_data->reserve); |
| 7479 | if (ret) { |
| 7480 | extent_changeset_free(dio_data->data_reserved); |
| 7481 | kfree(dio_data); |
| 7482 | return ret; |
| 7483 | } |
| 7484 | } |
| 7485 | iomap->private = dio_data; |
| 7486 | |
| 7487 | |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7488 | /* |
| 7489 | * If this errors out it's because we couldn't invalidate pagecache for |
| 7490 | * this range and we need to fallback to buffered. |
| 7491 | */ |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 7492 | if (lock_extent_direct(inode, lockstart, lockend, &cached_state, write)) { |
Filipe Manana | 9c9464c | 2015-11-04 09:52:04 +0000 | [diff] [blame] | 7493 | ret = -ENOTBLK; |
| 7494 | goto err; |
| 7495 | } |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7496 | |
Omar Sandoval | 39b07b5 | 2019-12-02 17:34:23 -0800 | [diff] [blame] | 7497 | em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7498 | if (IS_ERR(em)) { |
| 7499 | ret = PTR_ERR(em); |
| 7500 | goto unlock_err; |
| 7501 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7502 | |
| 7503 | /* |
| 7504 | * Ok for INLINE and COMPRESSED extents we need to fallback on buffered |
| 7505 | * io. INLINE is special, and we could probably kludge it in here, but |
| 7506 | * it's still buffered so for safety lets just fall back to the generic |
| 7507 | * buffered path. |
| 7508 | * |
| 7509 | * For COMPRESSED we _have_ to read the entire extent in so we can |
| 7510 | * decompress it, so there will be buffering required no matter what we |
| 7511 | * do, so go ahead and fallback to buffered. |
| 7512 | * |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 7513 | * 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] | 7514 | * to buffered IO. Don't blame me, this is the price we pay for using |
| 7515 | * the generic code. |
| 7516 | */ |
| 7517 | if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) || |
| 7518 | em->block_start == EXTENT_MAP_INLINE) { |
| 7519 | free_extent_map(em); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7520 | ret = -ENOTBLK; |
| 7521 | goto unlock_err; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7522 | } |
| 7523 | |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 7524 | len = min(len, em->len - (start - em->start)); |
| 7525 | if (write) { |
| 7526 | ret = btrfs_get_blocks_direct_write(&em, inode, dio_data, |
| 7527 | start, len); |
Nikolay Borisov | c5794e5 | 2018-05-02 15:19:33 +0300 | [diff] [blame] | 7528 | if (ret < 0) |
| 7529 | goto unlock_err; |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 7530 | unlock_extents = true; |
| 7531 | /* Recalc len in case the new em is smaller than requested */ |
| 7532 | len = min(len, em->len - (start - em->start)); |
Nikolay Borisov | c5794e5 | 2018-05-02 15:19:33 +0300 | [diff] [blame] | 7533 | } else { |
Nikolay Borisov | 1c8d017 | 2018-05-02 15:19:32 +0300 | [diff] [blame] | 7534 | /* |
| 7535 | * We need to unlock only the end area that we aren't using. |
| 7536 | * The rest is going to be unlocked by the endio routine. |
| 7537 | */ |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 7538 | lockstart = start + len; |
| 7539 | if (lockstart < lockend) |
| 7540 | unlock_extents = true; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7541 | } |
| 7542 | |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 7543 | if (unlock_extents) |
| 7544 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, |
| 7545 | lockstart, lockend, &cached_state); |
| 7546 | else |
| 7547 | free_extent_state(cached_state); |
| 7548 | |
| 7549 | /* |
| 7550 | * Translate extent map information to iomap. |
| 7551 | * We trim the extents (and move the addr) even though iomap code does |
| 7552 | * that, since we have locked only the parts we are performing I/O in. |
| 7553 | */ |
| 7554 | if ((em->block_start == EXTENT_MAP_HOLE) || |
| 7555 | (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) && !write)) { |
| 7556 | iomap->addr = IOMAP_NULL_ADDR; |
| 7557 | iomap->type = IOMAP_HOLE; |
| 7558 | } else { |
| 7559 | iomap->addr = em->block_start + (start - em->start); |
| 7560 | iomap->type = IOMAP_MAPPED; |
| 7561 | } |
| 7562 | iomap->offset = start; |
| 7563 | iomap->bdev = fs_info->fs_devices->latest_bdev; |
| 7564 | iomap->length = len; |
| 7565 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7566 | free_extent_map(em); |
| 7567 | |
| 7568 | return 0; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7569 | |
| 7570 | unlock_err: |
Omar Sandoval | e182163 | 2019-08-15 14:04:04 -0700 | [diff] [blame] | 7571 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend, |
| 7572 | &cached_state); |
Filipe Manana | 9c9464c | 2015-11-04 09:52:04 +0000 | [diff] [blame] | 7573 | err: |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 7574 | if (dio_data) { |
| 7575 | btrfs_delalloc_release_space(BTRFS_I(inode), |
| 7576 | dio_data->data_reserved, start, |
| 7577 | dio_data->reserve, true); |
| 7578 | btrfs_delalloc_release_extents(BTRFS_I(inode), dio_data->reserve); |
| 7579 | extent_changeset_free(dio_data->data_reserved); |
| 7580 | kfree(dio_data); |
| 7581 | } |
| 7582 | return ret; |
| 7583 | } |
| 7584 | |
| 7585 | static int btrfs_dio_iomap_end(struct inode *inode, loff_t pos, loff_t length, |
| 7586 | ssize_t written, unsigned int flags, struct iomap *iomap) |
| 7587 | { |
| 7588 | int ret = 0; |
| 7589 | struct btrfs_dio_data *dio_data = iomap->private; |
| 7590 | size_t submitted = dio_data->submitted; |
| 7591 | const bool write = !!(flags & IOMAP_WRITE); |
| 7592 | |
| 7593 | if (!write && (iomap->type == IOMAP_HOLE)) { |
| 7594 | /* If reading from a hole, unlock and return */ |
| 7595 | unlock_extent(&BTRFS_I(inode)->io_tree, pos, pos + length - 1); |
| 7596 | goto out; |
| 7597 | } |
| 7598 | |
| 7599 | if (submitted < length) { |
| 7600 | pos += submitted; |
| 7601 | length -= submitted; |
| 7602 | if (write) |
| 7603 | __endio_write_update_ordered(BTRFS_I(inode), pos, |
| 7604 | length, false); |
| 7605 | else |
| 7606 | unlock_extent(&BTRFS_I(inode)->io_tree, pos, |
| 7607 | pos + length - 1); |
| 7608 | ret = -ENOTBLK; |
| 7609 | } |
| 7610 | |
| 7611 | if (write) { |
| 7612 | if (dio_data->reserve) |
| 7613 | btrfs_delalloc_release_space(BTRFS_I(inode), |
| 7614 | dio_data->data_reserved, pos, |
| 7615 | dio_data->reserve, true); |
| 7616 | btrfs_delalloc_release_extents(BTRFS_I(inode), dio_data->length); |
| 7617 | extent_changeset_free(dio_data->data_reserved); |
| 7618 | } |
| 7619 | out: |
| 7620 | kfree(dio_data); |
| 7621 | iomap->private = NULL; |
| 7622 | |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7623 | return ret; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7624 | } |
| 7625 | |
Omar Sandoval | 769b4f2 | 2020-04-16 14:46:22 -0700 | [diff] [blame] | 7626 | static void btrfs_dio_private_put(struct btrfs_dio_private *dip) |
| 7627 | { |
| 7628 | /* |
| 7629 | * This implies a barrier so that stores to dio_bio->bi_status before |
| 7630 | * this and loads of dio_bio->bi_status after this are fully ordered. |
| 7631 | */ |
| 7632 | if (!refcount_dec_and_test(&dip->refs)) |
| 7633 | return; |
| 7634 | |
| 7635 | if (bio_op(dip->dio_bio) == REQ_OP_WRITE) { |
Nikolay Borisov | b672b5c | 2020-06-03 08:55:24 +0300 | [diff] [blame] | 7636 | __endio_write_update_ordered(BTRFS_I(dip->inode), |
| 7637 | dip->logical_offset, |
Omar Sandoval | 769b4f2 | 2020-04-16 14:46:22 -0700 | [diff] [blame] | 7638 | dip->bytes, |
| 7639 | !dip->dio_bio->bi_status); |
| 7640 | } else { |
| 7641 | unlock_extent(&BTRFS_I(dip->inode)->io_tree, |
| 7642 | dip->logical_offset, |
| 7643 | dip->logical_offset + dip->bytes - 1); |
| 7644 | } |
| 7645 | |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 7646 | bio_endio(dip->dio_bio); |
Omar Sandoval | 769b4f2 | 2020-04-16 14:46:22 -0700 | [diff] [blame] | 7647 | kfree(dip); |
| 7648 | } |
| 7649 | |
Omar Sandoval | 77d5d68 | 2020-04-16 14:46:25 -0700 | [diff] [blame] | 7650 | static blk_status_t submit_dio_repair_bio(struct inode *inode, struct bio *bio, |
| 7651 | int mirror_num, |
| 7652 | unsigned long bio_flags) |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7653 | { |
Omar Sandoval | 77d5d68 | 2020-04-16 14:46:25 -0700 | [diff] [blame] | 7654 | struct btrfs_dio_private *dip = bio->bi_private; |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 7655 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Omar Sandoval | 58efbc9 | 2017-08-22 23:45:59 -0700 | [diff] [blame] | 7656 | blk_status_t ret; |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7657 | |
Mike Christie | 37226b2 | 2016-06-05 14:31:52 -0500 | [diff] [blame] | 7658 | BUG_ON(bio_op(bio) == REQ_OP_WRITE); |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7659 | |
Omar Sandoval | 5c047a6 | 2020-04-16 14:46:24 -0700 | [diff] [blame] | 7660 | 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] | 7661 | if (ret) |
Nikolay Borisov | ea057f6 | 2017-12-13 10:25:38 +0200 | [diff] [blame] | 7662 | return ret; |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7663 | |
Omar Sandoval | fd9d667 | 2020-04-16 14:46:23 -0700 | [diff] [blame] | 7664 | refcount_inc(&dip->refs); |
Chris Mason | 08635ba | 2019-07-10 12:28:14 -0700 | [diff] [blame] | 7665 | ret = btrfs_map_bio(fs_info, bio, mirror_num); |
Omar Sandoval | 77d5d68 | 2020-04-16 14:46:25 -0700 | [diff] [blame] | 7666 | if (ret) |
Omar Sandoval | fd9d667 | 2020-04-16 14:46:23 -0700 | [diff] [blame] | 7667 | refcount_dec(&dip->refs); |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7668 | return ret; |
| 7669 | } |
| 7670 | |
Omar Sandoval | 769b4f2 | 2020-04-16 14:46:22 -0700 | [diff] [blame] | 7671 | static blk_status_t btrfs_check_read_dio_bio(struct inode *inode, |
| 7672 | struct btrfs_io_bio *io_bio, |
Omar Sandoval | fd9d667 | 2020-04-16 14:46:23 -0700 | [diff] [blame] | 7673 | const bool uptodate) |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7674 | { |
Omar Sandoval | fd9d667 | 2020-04-16 14:46:23 -0700 | [diff] [blame] | 7675 | struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info; |
| 7676 | const u32 sectorsize = fs_info->sectorsize; |
Josef Bacik | 7870d08 | 2017-05-05 11:57:15 -0400 | [diff] [blame] | 7677 | struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree; |
Omar Sandoval | fd9d667 | 2020-04-16 14:46:23 -0700 | [diff] [blame] | 7678 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
| 7679 | const bool csum = !(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM); |
Liu Bo | 17347ce | 2017-05-15 15:33:27 -0700 | [diff] [blame] | 7680 | struct bio_vec bvec; |
| 7681 | struct bvec_iter iter; |
Omar Sandoval | fd9d667 | 2020-04-16 14:46:23 -0700 | [diff] [blame] | 7682 | u64 start = io_bio->logical; |
| 7683 | int icsum = 0; |
Omar Sandoval | 58efbc9 | 2017-08-22 23:45:59 -0700 | [diff] [blame] | 7684 | blk_status_t err = BLK_STS_OK; |
Miao Xie | dc380ae | 2014-09-12 18:43:55 +0800 | [diff] [blame] | 7685 | |
Omar Sandoval | fd9d667 | 2020-04-16 14:46:23 -0700 | [diff] [blame] | 7686 | __bio_for_each_segment(bvec, &io_bio->bio, iter, io_bio->iter) { |
| 7687 | unsigned int i, nr_sectors, pgoff; |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 7688 | |
Liu Bo | 17347ce | 2017-05-15 15:33:27 -0700 | [diff] [blame] | 7689 | nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec.bv_len); |
| 7690 | pgoff = bvec.bv_offset; |
Omar Sandoval | fd9d667 | 2020-04-16 14:46:23 -0700 | [diff] [blame] | 7691 | for (i = 0; i < nr_sectors; i++) { |
Liu Bo | 97bf5a5 | 2017-04-07 13:11:10 -0700 | [diff] [blame] | 7692 | ASSERT(pgoff < PAGE_SIZE); |
Omar Sandoval | fd9d667 | 2020-04-16 14:46:23 -0700 | [diff] [blame] | 7693 | if (uptodate && |
| 7694 | (!csum || !check_data_csum(inode, io_bio, icsum, |
Qu Wenruo | 265d4ac | 2020-10-21 14:24:54 +0800 | [diff] [blame] | 7695 | bvec.bv_page, pgoff))) { |
Omar Sandoval | fd9d667 | 2020-04-16 14:46:23 -0700 | [diff] [blame] | 7696 | clean_io_failure(fs_info, failure_tree, io_tree, |
| 7697 | start, bvec.bv_page, |
| 7698 | btrfs_ino(BTRFS_I(inode)), |
| 7699 | pgoff); |
| 7700 | } else { |
| 7701 | blk_status_t status; |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7702 | |
Omar Sandoval | 77d5d68 | 2020-04-16 14:46:25 -0700 | [diff] [blame] | 7703 | status = btrfs_submit_read_repair(inode, |
| 7704 | &io_bio->bio, |
| 7705 | start - io_bio->logical, |
Omar Sandoval | fd9d667 | 2020-04-16 14:46:23 -0700 | [diff] [blame] | 7706 | bvec.bv_page, pgoff, |
| 7707 | start, |
| 7708 | start + sectorsize - 1, |
Omar Sandoval | 77d5d68 | 2020-04-16 14:46:25 -0700 | [diff] [blame] | 7709 | io_bio->mirror_num, |
| 7710 | submit_dio_repair_bio); |
Omar Sandoval | fd9d667 | 2020-04-16 14:46:23 -0700 | [diff] [blame] | 7711 | if (status) |
| 7712 | err = status; |
| 7713 | } |
| 7714 | start += sectorsize; |
| 7715 | icsum++; |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 7716 | pgoff += sectorsize; |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 7717 | } |
Kent Overstreet | 2c30c71 | 2013-11-07 12:20:26 -0800 | [diff] [blame] | 7718 | } |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 7719 | return err; |
| 7720 | } |
| 7721 | |
Nikolay Borisov | b672b5c | 2020-06-03 08:55:24 +0300 | [diff] [blame] | 7722 | static void __endio_write_update_ordered(struct btrfs_inode *inode, |
Qu Wenruo | 52427260 | 2017-03-08 10:25:52 +0800 | [diff] [blame] | 7723 | const u64 offset, const u64 bytes, |
| 7724 | const bool uptodate) |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7725 | { |
Nikolay Borisov | b672b5c | 2020-06-03 08:55:24 +0300 | [diff] [blame] | 7726 | struct btrfs_fs_info *fs_info = inode->root->fs_info; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7727 | struct btrfs_ordered_extent *ordered = NULL; |
Qu Wenruo | 52427260 | 2017-03-08 10:25:52 +0800 | [diff] [blame] | 7728 | struct btrfs_workqueue *wq; |
Filipe Manana | 1454377 | 2015-11-24 16:23:54 +0000 | [diff] [blame] | 7729 | u64 ordered_offset = offset; |
| 7730 | u64 ordered_bytes = bytes; |
Naohiro Aota | 67c003f | 2017-09-01 17:59:07 +0900 | [diff] [blame] | 7731 | u64 last_offset; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7732 | |
Nikolay Borisov | b672b5c | 2020-06-03 08:55:24 +0300 | [diff] [blame] | 7733 | if (btrfs_is_free_space_inode(inode)) |
Qu Wenruo | 52427260 | 2017-03-08 10:25:52 +0800 | [diff] [blame] | 7734 | wq = fs_info->endio_freespace_worker; |
Omar Sandoval | a0cac0e | 2019-09-16 11:30:57 -0700 | [diff] [blame] | 7735 | else |
Qu Wenruo | 52427260 | 2017-03-08 10:25:52 +0800 | [diff] [blame] | 7736 | wq = fs_info->endio_write_workers; |
Qu Wenruo | 52427260 | 2017-03-08 10:25:52 +0800 | [diff] [blame] | 7737 | |
Nikolay Borisov | b25f0d0 | 2018-04-11 11:21:17 +0300 | [diff] [blame] | 7738 | while (ordered_offset < offset + bytes) { |
| 7739 | last_offset = ordered_offset; |
Nikolay Borisov | b672b5c | 2020-06-03 08:55:24 +0300 | [diff] [blame] | 7740 | if (btrfs_dec_test_first_ordered_pending(inode, &ordered, |
Nikolay Borisov | 7095821 | 2020-06-03 08:55:23 +0300 | [diff] [blame] | 7741 | &ordered_offset, |
| 7742 | ordered_bytes, |
| 7743 | uptodate)) { |
Omar Sandoval | a0cac0e | 2019-09-16 11:30:57 -0700 | [diff] [blame] | 7744 | btrfs_init_work(&ordered->work, finish_ordered_fn, NULL, |
| 7745 | NULL); |
Nikolay Borisov | b25f0d0 | 2018-04-11 11:21:17 +0300 | [diff] [blame] | 7746 | btrfs_queue_work(wq, &ordered->work); |
| 7747 | } |
| 7748 | /* |
| 7749 | * If btrfs_dec_test_ordered_pending does not find any ordered |
| 7750 | * extent in the range, we can exit. |
| 7751 | */ |
| 7752 | if (ordered_offset == last_offset) |
| 7753 | return; |
| 7754 | /* |
| 7755 | * Our bio might span multiple ordered extents. In this case |
Andrea Gelmini | 52042d8 | 2018-11-28 12:05:13 +0100 | [diff] [blame] | 7756 | * we keep going until we have accounted the whole dio. |
Nikolay Borisov | b25f0d0 | 2018-04-11 11:21:17 +0300 | [diff] [blame] | 7757 | */ |
| 7758 | if (ordered_offset < offset + bytes) { |
| 7759 | ordered_bytes = offset + bytes - ordered_offset; |
| 7760 | ordered = NULL; |
| 7761 | } |
Chris Mason | 163cf09 | 2010-11-28 19:56:33 -0500 | [diff] [blame] | 7762 | } |
Filipe Manana | 1454377 | 2015-11-24 16:23:54 +0000 | [diff] [blame] | 7763 | } |
| 7764 | |
Qu Wenruo | 8896a08 | 2020-10-21 14:24:53 +0800 | [diff] [blame] | 7765 | static blk_status_t btrfs_submit_bio_start_direct_io(struct inode *inode, |
| 7766 | struct bio *bio, u64 offset) |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 7767 | { |
Johannes Thumshirn | c965d64 | 2020-07-28 20:25:41 +0900 | [diff] [blame] | 7768 | return btrfs_csum_one_bio(BTRFS_I(inode), bio, offset, 1); |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 7769 | } |
| 7770 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 7771 | static void btrfs_end_dio_bio(struct bio *bio) |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7772 | { |
| 7773 | struct btrfs_dio_private *dip = bio->bi_private; |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 7774 | blk_status_t err = bio->bi_status; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7775 | |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7776 | if (err) |
| 7777 | btrfs_warn(BTRFS_I(dip->inode)->root->fs_info, |
Mike Christie | 6296b96 | 2016-06-05 14:32:21 -0500 | [diff] [blame] | 7778 | "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] | 7779 | btrfs_ino(BTRFS_I(dip->inode)), bio_op(bio), |
| 7780 | bio->bi_opf, |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7781 | (unsigned long long)bio->bi_iter.bi_sector, |
| 7782 | bio->bi_iter.bi_size, err); |
| 7783 | |
Omar Sandoval | 769b4f2 | 2020-04-16 14:46:22 -0700 | [diff] [blame] | 7784 | if (bio_op(bio) == REQ_OP_READ) { |
| 7785 | err = btrfs_check_read_dio_bio(dip->inode, btrfs_io_bio(bio), |
Omar Sandoval | fd9d667 | 2020-04-16 14:46:23 -0700 | [diff] [blame] | 7786 | !err); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7787 | } |
| 7788 | |
Omar Sandoval | 769b4f2 | 2020-04-16 14:46:22 -0700 | [diff] [blame] | 7789 | if (err) |
| 7790 | dip->dio_bio->bi_status = err; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7791 | |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7792 | bio_put(bio); |
Omar Sandoval | 769b4f2 | 2020-04-16 14:46:22 -0700 | [diff] [blame] | 7793 | btrfs_dio_private_put(dip); |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 7794 | } |
| 7795 | |
David Sterba | d0ee393 | 2018-03-08 14:35:48 +0100 | [diff] [blame] | 7796 | static inline blk_status_t btrfs_submit_dio_bio(struct bio *bio, |
| 7797 | struct inode *inode, u64 file_offset, int async_submit) |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7798 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7799 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 7800 | struct btrfs_dio_private *dip = bio->bi_private; |
Mike Christie | 37226b2 | 2016-06-05 14:31:52 -0500 | [diff] [blame] | 7801 | bool write = bio_op(bio) == REQ_OP_WRITE; |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 7802 | blk_status_t ret; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7803 | |
Liu Bo | 4c274bc | 2017-11-01 17:19:27 -0600 | [diff] [blame] | 7804 | /* Check btrfs_submit_bio_hook() for rules about async submit. */ |
Josef Bacik | b812ce2 | 2012-11-16 13:56:32 -0500 | [diff] [blame] | 7805 | if (async_submit) |
| 7806 | async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers); |
| 7807 | |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 7808 | if (!write) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7809 | 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] | 7810 | if (ret) |
| 7811 | goto err; |
| 7812 | } |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7813 | |
Nikolay Borisov | e6961ca | 2017-08-03 15:44:58 +0300 | [diff] [blame] | 7814 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM) |
Josef Bacik | 1ae3993 | 2011-04-06 14:41:34 -0400 | [diff] [blame] | 7815 | goto map; |
| 7816 | |
| 7817 | if (write && async_submit) { |
Qu Wenruo | 8896a08 | 2020-10-21 14:24:53 +0800 | [diff] [blame] | 7818 | ret = btrfs_wq_submit_bio(inode, bio, 0, 0, |
| 7819 | file_offset, |
David Sterba | e288c08 | 2018-07-18 17:36:24 +0200 | [diff] [blame] | 7820 | btrfs_submit_bio_start_direct_io); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7821 | goto err; |
Josef Bacik | 1ae3993 | 2011-04-06 14:41:34 -0400 | [diff] [blame] | 7822 | } else if (write) { |
| 7823 | /* |
| 7824 | * If we aren't doing async submit, calculate the csum of the |
| 7825 | * bio now. |
| 7826 | */ |
Nikolay Borisov | bd242a0 | 2020-06-03 08:55:07 +0300 | [diff] [blame] | 7827 | ret = btrfs_csum_one_bio(BTRFS_I(inode), bio, file_offset, 1); |
Josef Bacik | 1ae3993 | 2011-04-06 14:41:34 -0400 | [diff] [blame] | 7828 | if (ret) |
| 7829 | goto err; |
Miao Xie | 23ea8e5 | 2014-09-12 18:43:54 +0800 | [diff] [blame] | 7830 | } else { |
Omar Sandoval | 8587957 | 2020-04-16 14:46:21 -0700 | [diff] [blame] | 7831 | u64 csum_offset; |
| 7832 | |
| 7833 | csum_offset = file_offset - dip->logical_offset; |
David Sterba | 265fdfa | 2020-07-01 21:19:09 +0200 | [diff] [blame] | 7834 | csum_offset >>= fs_info->sectorsize_bits; |
David Sterba | 55fc29b | 2020-06-30 02:01:31 +0200 | [diff] [blame] | 7835 | csum_offset *= fs_info->csum_size; |
Omar Sandoval | 8587957 | 2020-04-16 14:46:21 -0700 | [diff] [blame] | 7836 | btrfs_io_bio(bio)->csum = dip->csums + csum_offset; |
Tsutomu Itoh | c2db107 | 2011-03-01 06:48:31 +0000 | [diff] [blame] | 7837 | } |
Josef Bacik | 1ae3993 | 2011-04-06 14:41:34 -0400 | [diff] [blame] | 7838 | map: |
Chris Mason | 08635ba | 2019-07-10 12:28:14 -0700 | [diff] [blame] | 7839 | ret = btrfs_map_bio(fs_info, bio, 0); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7840 | err: |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7841 | return ret; |
| 7842 | } |
| 7843 | |
Omar Sandoval | c36cac2 | 2020-04-16 14:46:13 -0700 | [diff] [blame] | 7844 | /* |
| 7845 | * If this succeeds, the btrfs_dio_private is responsible for cleaning up locked |
| 7846 | * or ordered extents whether or not we submit any bios. |
| 7847 | */ |
| 7848 | static struct btrfs_dio_private *btrfs_create_dio_private(struct bio *dio_bio, |
| 7849 | struct inode *inode, |
| 7850 | loff_t file_offset) |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7851 | { |
Omar Sandoval | c36cac2 | 2020-04-16 14:46:13 -0700 | [diff] [blame] | 7852 | const bool write = (bio_op(dio_bio) == REQ_OP_WRITE); |
Omar Sandoval | 8587957 | 2020-04-16 14:46:21 -0700 | [diff] [blame] | 7853 | const bool csum = !(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM); |
| 7854 | size_t dip_size; |
Omar Sandoval | c36cac2 | 2020-04-16 14:46:13 -0700 | [diff] [blame] | 7855 | struct btrfs_dio_private *dip; |
Omar Sandoval | c36cac2 | 2020-04-16 14:46:13 -0700 | [diff] [blame] | 7856 | |
Omar Sandoval | 8587957 | 2020-04-16 14:46:21 -0700 | [diff] [blame] | 7857 | dip_size = sizeof(*dip); |
| 7858 | if (!write && csum) { |
| 7859 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Omar Sandoval | 8587957 | 2020-04-16 14:46:21 -0700 | [diff] [blame] | 7860 | size_t nblocks; |
| 7861 | |
David Sterba | 265fdfa | 2020-07-01 21:19:09 +0200 | [diff] [blame] | 7862 | nblocks = dio_bio->bi_iter.bi_size >> fs_info->sectorsize_bits; |
David Sterba | 223486c | 2020-07-02 11:27:30 +0200 | [diff] [blame] | 7863 | dip_size += fs_info->csum_size * nblocks; |
Omar Sandoval | 8587957 | 2020-04-16 14:46:21 -0700 | [diff] [blame] | 7864 | } |
| 7865 | |
| 7866 | dip = kzalloc(dip_size, GFP_NOFS); |
Omar Sandoval | c36cac2 | 2020-04-16 14:46:13 -0700 | [diff] [blame] | 7867 | if (!dip) |
| 7868 | return NULL; |
| 7869 | |
Omar Sandoval | c36cac2 | 2020-04-16 14:46:13 -0700 | [diff] [blame] | 7870 | dip->inode = inode; |
| 7871 | dip->logical_offset = file_offset; |
| 7872 | dip->bytes = dio_bio->bi_iter.bi_size; |
| 7873 | dip->disk_bytenr = (u64)dio_bio->bi_iter.bi_sector << 9; |
Omar Sandoval | c36cac2 | 2020-04-16 14:46:13 -0700 | [diff] [blame] | 7874 | dip->dio_bio = dio_bio; |
Omar Sandoval | e3b318d | 2020-04-16 14:46:20 -0700 | [diff] [blame] | 7875 | refcount_set(&dip->refs, 1); |
Omar Sandoval | c36cac2 | 2020-04-16 14:46:13 -0700 | [diff] [blame] | 7876 | return dip; |
| 7877 | } |
| 7878 | |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 7879 | static blk_qc_t btrfs_submit_direct(struct inode *inode, struct iomap *iomap, |
| 7880 | struct bio *dio_bio, loff_t file_offset) |
Omar Sandoval | c36cac2 | 2020-04-16 14:46:13 -0700 | [diff] [blame] | 7881 | { |
| 7882 | const bool write = (bio_op(dio_bio) == REQ_OP_WRITE); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7883 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Omar Sandoval | 769b4f2 | 2020-04-16 14:46:22 -0700 | [diff] [blame] | 7884 | const bool raid56 = (btrfs_data_alloc_profile(fs_info) & |
| 7885 | BTRFS_BLOCK_GROUP_RAID56_MASK); |
Omar Sandoval | c36cac2 | 2020-04-16 14:46:13 -0700 | [diff] [blame] | 7886 | struct btrfs_dio_private *dip; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7887 | struct bio *bio; |
Omar Sandoval | c36cac2 | 2020-04-16 14:46:13 -0700 | [diff] [blame] | 7888 | u64 start_sector; |
Josef Bacik | 1ae3993 | 2011-04-06 14:41:34 -0400 | [diff] [blame] | 7889 | int async_submit = 0; |
Liu Bo | 725130b | 2017-05-16 09:51:39 -0700 | [diff] [blame] | 7890 | u64 submit_len; |
| 7891 | int clone_offset = 0; |
| 7892 | int clone_len; |
Chandan Rajendra | 5f4dc8f | 2016-01-21 15:56:00 +0530 | [diff] [blame] | 7893 | int ret; |
Omar Sandoval | 58efbc9 | 2017-08-22 23:45:59 -0700 | [diff] [blame] | 7894 | blk_status_t status; |
Nikolay Borisov | 89b798a | 2019-06-03 12:05:05 +0300 | [diff] [blame] | 7895 | struct btrfs_io_geometry geom; |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 7896 | struct btrfs_dio_data *dio_data = iomap->private; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7897 | |
Omar Sandoval | c36cac2 | 2020-04-16 14:46:13 -0700 | [diff] [blame] | 7898 | dip = btrfs_create_dio_private(dio_bio, inode, file_offset); |
| 7899 | if (!dip) { |
| 7900 | if (!write) { |
| 7901 | unlock_extent(&BTRFS_I(inode)->io_tree, file_offset, |
| 7902 | file_offset + dio_bio->bi_iter.bi_size - 1); |
| 7903 | } |
| 7904 | dio_bio->bi_status = BLK_STS_RESOURCE; |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 7905 | bio_endio(dio_bio); |
| 7906 | return BLK_QC_T_NONE; |
Josef Bacik | 02f57c7 | 2011-04-06 14:25:44 -0400 | [diff] [blame] | 7907 | } |
| 7908 | |
Josef Bacik | 334c16d | 2020-10-16 11:29:14 -0400 | [diff] [blame] | 7909 | if (!write) { |
Omar Sandoval | 8587957 | 2020-04-16 14:46:21 -0700 | [diff] [blame] | 7910 | /* |
| 7911 | * Load the csums up front to reduce csum tree searches and |
| 7912 | * contention when submitting bios. |
Josef Bacik | 334c16d | 2020-10-16 11:29:14 -0400 | [diff] [blame] | 7913 | * |
| 7914 | * If we have csums disabled this will do nothing. |
Omar Sandoval | 8587957 | 2020-04-16 14:46:21 -0700 | [diff] [blame] | 7915 | */ |
| 7916 | status = btrfs_lookup_bio_sums(inode, dio_bio, file_offset, |
| 7917 | dip->csums); |
| 7918 | if (status != BLK_STS_OK) |
| 7919 | goto out_err; |
| 7920 | } |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 7921 | |
Omar Sandoval | 769b4f2 | 2020-04-16 14:46:22 -0700 | [diff] [blame] | 7922 | start_sector = dio_bio->bi_iter.bi_sector; |
| 7923 | submit_len = dio_bio->bi_iter.bi_size; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7924 | |
Arnd Bergmann | 3c91ee6 | 2017-05-18 15:33:29 +0200 | [diff] [blame] | 7925 | do { |
Omar Sandoval | 769b4f2 | 2020-04-16 14:46:22 -0700 | [diff] [blame] | 7926 | ret = btrfs_get_io_geometry(fs_info, btrfs_op(dio_bio), |
| 7927 | start_sector << 9, submit_len, |
| 7928 | &geom); |
| 7929 | if (ret) { |
| 7930 | status = errno_to_blk_status(ret); |
| 7931 | goto out_err; |
| 7932 | } |
| 7933 | ASSERT(geom.len <= INT_MAX); |
| 7934 | |
Nikolay Borisov | 89b798a | 2019-06-03 12:05:05 +0300 | [diff] [blame] | 7935 | clone_len = min_t(int, submit_len, geom.len); |
Josef Bacik | 02f57c7 | 2011-04-06 14:25:44 -0400 | [diff] [blame] | 7936 | |
Liu Bo | 725130b | 2017-05-16 09:51:39 -0700 | [diff] [blame] | 7937 | /* |
| 7938 | * This will never fail as it's passing GPF_NOFS and |
| 7939 | * the allocation is backed by btrfs_bioset. |
| 7940 | */ |
Omar Sandoval | 769b4f2 | 2020-04-16 14:46:22 -0700 | [diff] [blame] | 7941 | bio = btrfs_bio_clone_partial(dio_bio, clone_offset, clone_len); |
Liu Bo | 725130b | 2017-05-16 09:51:39 -0700 | [diff] [blame] | 7942 | bio->bi_private = dip; |
| 7943 | bio->bi_end_io = btrfs_end_dio_bio; |
| 7944 | btrfs_io_bio(bio)->logical = file_offset; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7945 | |
Liu Bo | 725130b | 2017-05-16 09:51:39 -0700 | [diff] [blame] | 7946 | ASSERT(submit_len >= clone_len); |
| 7947 | submit_len -= clone_len; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7948 | |
Liu Bo | 725130b | 2017-05-16 09:51:39 -0700 | [diff] [blame] | 7949 | /* |
| 7950 | * Increase the count before we submit the bio so we know |
| 7951 | * the end IO handler won't happen before we increase the |
| 7952 | * count. Otherwise, the dip might get freed before we're |
| 7953 | * done setting it up. |
Omar Sandoval | 769b4f2 | 2020-04-16 14:46:22 -0700 | [diff] [blame] | 7954 | * |
| 7955 | * We transfer the initial reference to the last bio, so we |
| 7956 | * don't need to increment the reference count for the last one. |
Liu Bo | 725130b | 2017-05-16 09:51:39 -0700 | [diff] [blame] | 7957 | */ |
Omar Sandoval | 769b4f2 | 2020-04-16 14:46:22 -0700 | [diff] [blame] | 7958 | if (submit_len > 0) { |
| 7959 | refcount_inc(&dip->refs); |
| 7960 | /* |
| 7961 | * If we are submitting more than one bio, submit them |
| 7962 | * all asynchronously. The exception is RAID 5 or 6, as |
| 7963 | * asynchronous checksums make it difficult to collect |
| 7964 | * full stripe writes. |
| 7965 | */ |
| 7966 | if (!raid56) |
| 7967 | async_submit = 1; |
| 7968 | } |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7969 | |
David Sterba | d0ee393 | 2018-03-08 14:35:48 +0100 | [diff] [blame] | 7970 | status = btrfs_submit_dio_bio(bio, inode, file_offset, |
Omar Sandoval | 58efbc9 | 2017-08-22 23:45:59 -0700 | [diff] [blame] | 7971 | async_submit); |
| 7972 | if (status) { |
Liu Bo | 725130b | 2017-05-16 09:51:39 -0700 | [diff] [blame] | 7973 | bio_put(bio); |
Omar Sandoval | 769b4f2 | 2020-04-16 14:46:22 -0700 | [diff] [blame] | 7974 | if (submit_len > 0) |
| 7975 | refcount_dec(&dip->refs); |
Liu Bo | 725130b | 2017-05-16 09:51:39 -0700 | [diff] [blame] | 7976 | goto out_err; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7977 | } |
Liu Bo | 725130b | 2017-05-16 09:51:39 -0700 | [diff] [blame] | 7978 | |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 7979 | dio_data->submitted += clone_len; |
Liu Bo | 725130b | 2017-05-16 09:51:39 -0700 | [diff] [blame] | 7980 | clone_offset += clone_len; |
| 7981 | start_sector += clone_len >> 9; |
| 7982 | file_offset += clone_len; |
Arnd Bergmann | 3c91ee6 | 2017-05-18 15:33:29 +0200 | [diff] [blame] | 7983 | } while (submit_len > 0); |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 7984 | return BLK_QC_T_NONE; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7985 | |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7986 | out_err: |
Omar Sandoval | 769b4f2 | 2020-04-16 14:46:22 -0700 | [diff] [blame] | 7987 | dip->dio_bio->bi_status = status; |
| 7988 | btrfs_dio_private_put(dip); |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 7989 | return BLK_QC_T_NONE; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7990 | } |
| 7991 | |
Goldwyn Rodrigues | 4e4cabe | 2020-09-24 11:39:12 -0500 | [diff] [blame] | 7992 | const struct iomap_ops btrfs_dio_iomap_ops = { |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 7993 | .iomap_begin = btrfs_dio_iomap_begin, |
| 7994 | .iomap_end = btrfs_dio_iomap_end, |
| 7995 | }; |
| 7996 | |
Goldwyn Rodrigues | 4e4cabe | 2020-09-24 11:39:12 -0500 | [diff] [blame] | 7997 | const struct iomap_dio_ops btrfs_dio_ops = { |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 7998 | .submit_io = btrfs_submit_direct, |
| 7999 | }; |
| 8000 | |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 8001 | static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, |
David Sterba | bab16e2 | 2020-06-23 20:56:12 +0200 | [diff] [blame] | 8002 | u64 start, u64 len) |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 8003 | { |
Tsutomu Itoh | 05dadc0 | 2012-11-29 05:08:26 +0000 | [diff] [blame] | 8004 | int ret; |
| 8005 | |
Christoph Hellwig | 45dd052 | 2020-05-23 09:30:14 +0200 | [diff] [blame] | 8006 | ret = fiemap_prep(inode, fieinfo, start, &len, 0); |
Tsutomu Itoh | 05dadc0 | 2012-11-29 05:08:26 +0000 | [diff] [blame] | 8007 | if (ret) |
| 8008 | return ret; |
| 8009 | |
Nikolay Borisov | facee0a | 2020-08-31 14:42:49 +0300 | [diff] [blame] | 8010 | return extent_fiemap(BTRFS_I(inode), fieinfo, start, len); |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 8011 | } |
| 8012 | |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8013 | int btrfs_readpage(struct file *file, struct page *page) |
| 8014 | { |
Nikolay Borisov | 0f20881 | 2020-09-14 14:39:16 +0300 | [diff] [blame] | 8015 | struct btrfs_inode *inode = BTRFS_I(page->mapping->host); |
| 8016 | u64 start = page_offset(page); |
| 8017 | u64 end = start + PAGE_SIZE - 1; |
Nikolay Borisov | c1be9c1 | 2020-09-14 12:37:08 +0300 | [diff] [blame] | 8018 | unsigned long bio_flags = 0; |
Nikolay Borisov | 0f20881 | 2020-09-14 14:39:16 +0300 | [diff] [blame] | 8019 | struct bio *bio = NULL; |
Nikolay Borisov | c1be9c1 | 2020-09-14 12:37:08 +0300 | [diff] [blame] | 8020 | int ret; |
| 8021 | |
Nikolay Borisov | 0f20881 | 2020-09-14 14:39:16 +0300 | [diff] [blame] | 8022 | btrfs_lock_and_flush_ordered_range(inode, start, end, NULL); |
| 8023 | |
| 8024 | ret = btrfs_do_readpage(page, NULL, &bio, &bio_flags, 0, NULL); |
Nikolay Borisov | c1be9c1 | 2020-09-14 12:37:08 +0300 | [diff] [blame] | 8025 | if (bio) |
| 8026 | ret = submit_one_bio(bio, 0, bio_flags); |
| 8027 | return ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8028 | } |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 8029 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8030 | static int btrfs_writepage(struct page *page, struct writeback_control *wbc) |
| 8031 | { |
Josef Bacik | be7bd73 | 2015-10-22 15:05:09 -0400 | [diff] [blame] | 8032 | struct inode *inode = page->mapping->host; |
| 8033 | int ret; |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8034 | |
| 8035 | if (current->flags & PF_MEMALLOC) { |
| 8036 | redirty_page_for_writepage(wbc, page); |
| 8037 | unlock_page(page); |
| 8038 | return 0; |
| 8039 | } |
Josef Bacik | be7bd73 | 2015-10-22 15:05:09 -0400 | [diff] [blame] | 8040 | |
| 8041 | /* |
| 8042 | * If we are under memory pressure we will call this directly from the |
| 8043 | * VM, we need to make sure we have the inode referenced for the ordered |
| 8044 | * extent. If not just return like we didn't do anything. |
| 8045 | */ |
| 8046 | if (!igrab(inode)) { |
| 8047 | redirty_page_for_writepage(wbc, page); |
| 8048 | return AOP_WRITEPAGE_ACTIVATE; |
| 8049 | } |
Nikolay Borisov | 0a9b0e5 | 2017-12-08 15:55:59 +0200 | [diff] [blame] | 8050 | ret = extent_write_full_page(page, wbc); |
Josef Bacik | be7bd73 | 2015-10-22 15:05:09 -0400 | [diff] [blame] | 8051 | btrfs_add_delayed_iput(inode); |
| 8052 | return ret; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8053 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8054 | |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 8055 | static int btrfs_writepages(struct address_space *mapping, |
| 8056 | struct writeback_control *wbc) |
Chris Mason | b293f02e | 2007-11-01 19:45:34 -0400 | [diff] [blame] | 8057 | { |
Nikolay Borisov | 8ae225a | 2018-04-19 10:46:38 +0300 | [diff] [blame] | 8058 | return extent_writepages(mapping, wbc); |
Chris Mason | b293f02e | 2007-11-01 19:45:34 -0400 | [diff] [blame] | 8059 | } |
| 8060 | |
Matthew Wilcox (Oracle) | ba206a0 | 2020-06-01 21:47:05 -0700 | [diff] [blame] | 8061 | static void btrfs_readahead(struct readahead_control *rac) |
Chris Mason | 3ab2fb5 | 2007-11-08 10:59:22 -0500 | [diff] [blame] | 8062 | { |
Matthew Wilcox (Oracle) | ba206a0 | 2020-06-01 21:47:05 -0700 | [diff] [blame] | 8063 | extent_readahead(rac); |
Chris Mason | 3ab2fb5 | 2007-11-08 10:59:22 -0500 | [diff] [blame] | 8064 | } |
Nikolay Borisov | 2a3ff0a | 2018-04-19 10:46:36 +0300 | [diff] [blame] | 8065 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8066 | static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8067 | { |
Nikolay Borisov | 477a30b | 2018-04-19 10:46:34 +0300 | [diff] [blame] | 8068 | int ret = try_release_extent_mapping(page, gfp_flags); |
Guoqing Jiang | d1b89bc | 2020-06-01 21:47:45 -0700 | [diff] [blame] | 8069 | if (ret == 1) |
| 8070 | detach_page_private(page); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8071 | return ret; |
| 8072 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8073 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8074 | static int btrfs_releasepage(struct page *page, gfp_t gfp_flags) |
| 8075 | { |
Chris Mason | 98509cf | 2008-09-11 15:51:43 -0400 | [diff] [blame] | 8076 | if (PageWriteback(page) || PageDirty(page)) |
| 8077 | return 0; |
Michal Hocko | 3ba7ab2 | 2017-01-09 15:39:02 +0100 | [diff] [blame] | 8078 | return __btrfs_releasepage(page, gfp_flags); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8079 | } |
| 8080 | |
Roman Gushchin | f8e6608 | 2020-03-04 16:57:35 -0800 | [diff] [blame] | 8081 | #ifdef CONFIG_MIGRATION |
| 8082 | static int btrfs_migratepage(struct address_space *mapping, |
| 8083 | struct page *newpage, struct page *page, |
| 8084 | enum migrate_mode mode) |
| 8085 | { |
| 8086 | int ret; |
| 8087 | |
| 8088 | ret = migrate_page_move_mapping(mapping, newpage, page, 0); |
| 8089 | if (ret != MIGRATEPAGE_SUCCESS) |
| 8090 | return ret; |
| 8091 | |
Guoqing Jiang | d1b89bc | 2020-06-01 21:47:45 -0700 | [diff] [blame] | 8092 | if (page_has_private(page)) |
| 8093 | attach_page_private(newpage, detach_page_private(page)); |
Roman Gushchin | f8e6608 | 2020-03-04 16:57:35 -0800 | [diff] [blame] | 8094 | |
| 8095 | if (PagePrivate2(page)) { |
| 8096 | ClearPagePrivate2(page); |
| 8097 | SetPagePrivate2(newpage); |
| 8098 | } |
| 8099 | |
| 8100 | if (mode != MIGRATE_SYNC_NO_COPY) |
| 8101 | migrate_page_copy(newpage, page); |
| 8102 | else |
| 8103 | migrate_page_states(newpage, page); |
| 8104 | return MIGRATEPAGE_SUCCESS; |
| 8105 | } |
| 8106 | #endif |
| 8107 | |
Lukas Czerner | d47992f | 2013-05-21 23:17:23 -0400 | [diff] [blame] | 8108 | static void btrfs_invalidatepage(struct page *page, unsigned int offset, |
| 8109 | unsigned int length) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8110 | { |
Nikolay Borisov | 53ac7ea | 2020-08-31 14:42:43 +0300 | [diff] [blame] | 8111 | struct btrfs_inode *inode = BTRFS_I(page->mapping->host); |
| 8112 | struct extent_io_tree *tree = &inode->io_tree; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8113 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 8114 | struct extent_state *cached_state = NULL; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8115 | u64 page_start = page_offset(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 8116 | u64 page_end = page_start + PAGE_SIZE - 1; |
Chandan Rajendra | dbfdb6d | 2016-01-21 15:55:58 +0530 | [diff] [blame] | 8117 | u64 start; |
| 8118 | u64 end; |
Nikolay Borisov | 53ac7ea | 2020-08-31 14:42:43 +0300 | [diff] [blame] | 8119 | int inode_evicting = inode->vfs_inode.i_state & I_FREEING; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8120 | |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 8121 | /* |
| 8122 | * we have the page locked, so new writeback can't start, |
| 8123 | * and the dirty bit won't be cleared while we are here. |
| 8124 | * |
| 8125 | * Wait for IO on this page so that we can safely clear |
| 8126 | * the PagePrivate2 bit and do ordered accounting |
| 8127 | */ |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8128 | wait_on_page_writeback(page); |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 8129 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8130 | if (offset) { |
| 8131 | btrfs_releasepage(page, GFP_NOFS); |
| 8132 | return; |
| 8133 | } |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 8134 | |
| 8135 | if (!inode_evicting) |
David Sterba | ff13db4 | 2015-12-03 14:30:40 +0100 | [diff] [blame] | 8136 | lock_extent_bits(tree, page_start, page_end, &cached_state); |
Chandan Rajendra | dbfdb6d | 2016-01-21 15:55:58 +0530 | [diff] [blame] | 8137 | again: |
| 8138 | start = page_start; |
Nikolay Borisov | 53ac7ea | 2020-08-31 14:42:43 +0300 | [diff] [blame] | 8139 | ordered = btrfs_lookup_ordered_range(inode, start, page_end - start + 1); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8140 | if (ordered) { |
Omar Sandoval | bffe633 | 2019-12-02 17:34:19 -0800 | [diff] [blame] | 8141 | end = min(page_end, |
| 8142 | ordered->file_offset + ordered->num_bytes - 1); |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 8143 | /* |
| 8144 | * IO on this page will never be started, so we need |
| 8145 | * to account for any ordered extents now |
| 8146 | */ |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 8147 | if (!inode_evicting) |
Chandan Rajendra | dbfdb6d | 2016-01-21 15:55:58 +0530 | [diff] [blame] | 8148 | clear_extent_bit(tree, start, end, |
Omar Sandoval | e182163 | 2019-08-15 14:04:04 -0700 | [diff] [blame] | 8149 | EXTENT_DELALLOC | EXTENT_DELALLOC_NEW | |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 8150 | EXTENT_LOCKED | EXTENT_DO_ACCOUNTING | |
David Sterba | ae0f162 | 2017-10-31 16:37:52 +0100 | [diff] [blame] | 8151 | EXTENT_DEFRAG, 1, 0, &cached_state); |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 8152 | /* |
| 8153 | * whoever cleared the private bit is responsible |
| 8154 | * for the finish_ordered_io |
| 8155 | */ |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 8156 | if (TestClearPagePrivate2(page)) { |
| 8157 | struct btrfs_ordered_inode_tree *tree; |
| 8158 | u64 new_len; |
| 8159 | |
Nikolay Borisov | 53ac7ea | 2020-08-31 14:42:43 +0300 | [diff] [blame] | 8160 | tree = &inode->ordered_tree; |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 8161 | |
| 8162 | spin_lock_irq(&tree->lock); |
| 8163 | set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags); |
Chandan Rajendra | dbfdb6d | 2016-01-21 15:55:58 +0530 | [diff] [blame] | 8164 | new_len = start - ordered->file_offset; |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 8165 | if (new_len < ordered->truncated_len) |
| 8166 | ordered->truncated_len = new_len; |
| 8167 | spin_unlock_irq(&tree->lock); |
| 8168 | |
Nikolay Borisov | 53ac7ea | 2020-08-31 14:42:43 +0300 | [diff] [blame] | 8169 | if (btrfs_dec_test_ordered_pending(inode, &ordered, |
| 8170 | start, |
Chandan Rajendra | dbfdb6d | 2016-01-21 15:55:58 +0530 | [diff] [blame] | 8171 | end - start + 1, 1)) |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 8172 | btrfs_finish_ordered_io(ordered); |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 8173 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8174 | btrfs_put_ordered_extent(ordered); |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 8175 | if (!inode_evicting) { |
| 8176 | cached_state = NULL; |
Chandan Rajendra | dbfdb6d | 2016-01-21 15:55:58 +0530 | [diff] [blame] | 8177 | lock_extent_bits(tree, start, end, |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 8178 | &cached_state); |
| 8179 | } |
Chandan Rajendra | dbfdb6d | 2016-01-21 15:55:58 +0530 | [diff] [blame] | 8180 | |
| 8181 | start = end + 1; |
| 8182 | if (start < page_end) |
| 8183 | goto again; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8184 | } |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 8185 | |
Qu Wenruo | b9d0b38 | 2015-09-29 10:35:16 +0800 | [diff] [blame] | 8186 | /* |
| 8187 | * Qgroup reserved space handler |
| 8188 | * Page here will be either |
Qu Wenruo | fa91e4a | 2020-07-17 15:12:05 +0800 | [diff] [blame] | 8189 | * 1) Already written to disk or ordered extent already submitted |
| 8190 | * Then its QGROUP_RESERVED bit in io_tree is already cleaned. |
| 8191 | * Qgroup will be handled by its qgroup_record then. |
| 8192 | * btrfs_qgroup_free_data() call will do nothing here. |
| 8193 | * |
| 8194 | * 2) Not written to disk yet |
| 8195 | * Then btrfs_qgroup_free_data() call will clear the QGROUP_RESERVED |
| 8196 | * bit of its io_tree, and free the qgroup reserved data space. |
| 8197 | * Since the IO will never happen for this page. |
Qu Wenruo | b9d0b38 | 2015-09-29 10:35:16 +0800 | [diff] [blame] | 8198 | */ |
Nikolay Borisov | 53ac7ea | 2020-08-31 14:42:43 +0300 | [diff] [blame] | 8199 | btrfs_qgroup_free_data(inode, NULL, page_start, PAGE_SIZE); |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 8200 | if (!inode_evicting) { |
Omar Sandoval | e182163 | 2019-08-15 14:04:04 -0700 | [diff] [blame] | 8201 | clear_extent_bit(tree, page_start, page_end, EXTENT_LOCKED | |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 8202 | EXTENT_DELALLOC | EXTENT_DELALLOC_NEW | |
| 8203 | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1, |
David Sterba | ae0f162 | 2017-10-31 16:37:52 +0100 | [diff] [blame] | 8204 | &cached_state); |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 8205 | |
| 8206 | __btrfs_releasepage(page, GFP_NOFS); |
| 8207 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8208 | |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 8209 | ClearPageChecked(page); |
Guoqing Jiang | d1b89bc | 2020-06-01 21:47:45 -0700 | [diff] [blame] | 8210 | detach_page_private(page); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8211 | } |
| 8212 | |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8213 | /* |
| 8214 | * btrfs_page_mkwrite() is not allowed to change the file size as it gets |
| 8215 | * called from a page fault handler when a page is first dirtied. Hence we must |
| 8216 | * be careful to check for EOF conditions here. We set the page up correctly |
| 8217 | * for a written page which means we get ENOSPC checking when writing into |
| 8218 | * holes and correct delalloc and unwritten extent mapping on filesystems that |
| 8219 | * support these features. |
| 8220 | * |
| 8221 | * We are not allowed to take the i_mutex here so we have to play games to |
| 8222 | * 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] | 8223 | * truncate_setsize() writes the inode size before removing pages, once we have |
| 8224 | * 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] | 8225 | * beyond EOF, then the page is guaranteed safe against truncation until we |
| 8226 | * unlock the page. |
| 8227 | */ |
Souptick Joarder | a528a24 | 2018-06-06 19:54:44 +0530 | [diff] [blame] | 8228 | vm_fault_t btrfs_page_mkwrite(struct vm_fault *vmf) |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8229 | { |
Nick Piggin | c2ec175 | 2009-03-31 15:23:21 -0700 | [diff] [blame] | 8230 | struct page *page = vmf->page; |
Dave Jiang | 11bac80 | 2017-02-24 14:56:41 -0800 | [diff] [blame] | 8231 | struct inode *inode = file_inode(vmf->vma->vm_file); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8232 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8233 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
| 8234 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 8235 | struct extent_state *cached_state = NULL; |
Qu Wenruo | 364ecf3 | 2017-02-27 15:10:38 +0800 | [diff] [blame] | 8236 | struct extent_changeset *data_reserved = NULL; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8237 | char *kaddr; |
| 8238 | unsigned long zero_start; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8239 | loff_t size; |
Souptick Joarder | a528a24 | 2018-06-06 19:54:44 +0530 | [diff] [blame] | 8240 | vm_fault_t ret; |
| 8241 | int ret2; |
Chris Mason | 9998eb7 | 2012-01-25 13:47:40 -0500 | [diff] [blame] | 8242 | int reserved = 0; |
Chandan Rajendra | d0b7da88 | 2016-01-21 15:55:57 +0530 | [diff] [blame] | 8243 | u64 reserved_space; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8244 | u64 page_start; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8245 | u64 page_end; |
Chandan Rajendra | d0b7da88 | 2016-01-21 15:55:57 +0530 | [diff] [blame] | 8246 | u64 end; |
| 8247 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 8248 | reserved_space = PAGE_SIZE; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8249 | |
Jan Kara | b2b5ef5 | 2012-06-12 16:20:45 +0200 | [diff] [blame] | 8250 | sb_start_pagefault(inode->i_sb); |
Qu Wenruo | df48063 | 2015-09-08 17:25:54 +0800 | [diff] [blame] | 8251 | page_start = page_offset(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 8252 | page_end = page_start + PAGE_SIZE - 1; |
Chandan Rajendra | d0b7da88 | 2016-01-21 15:55:57 +0530 | [diff] [blame] | 8253 | end = page_end; |
Qu Wenruo | df48063 | 2015-09-08 17:25:54 +0800 | [diff] [blame] | 8254 | |
Chandan Rajendra | d0b7da88 | 2016-01-21 15:55:57 +0530 | [diff] [blame] | 8255 | /* |
| 8256 | * Reserving delalloc space after obtaining the page lock can lead to |
| 8257 | * deadlock. For example, if a dirty page is locked by this function |
| 8258 | * and the call to btrfs_delalloc_reserve_space() ends up triggering |
| 8259 | * dirty page write out, then the btrfs_writepage() function could |
| 8260 | * end up waiting indefinitely to get a lock on the page currently |
| 8261 | * being processed by btrfs_page_mkwrite() function. |
| 8262 | */ |
Nikolay Borisov | e5b7231e | 2020-06-03 08:55:42 +0300 | [diff] [blame] | 8263 | ret2 = btrfs_delalloc_reserve_space(BTRFS_I(inode), &data_reserved, |
| 8264 | page_start, reserved_space); |
Souptick Joarder | a528a24 | 2018-06-06 19:54:44 +0530 | [diff] [blame] | 8265 | if (!ret2) { |
| 8266 | ret2 = file_update_time(vmf->vma->vm_file); |
Chris Mason | 9998eb7 | 2012-01-25 13:47:40 -0500 | [diff] [blame] | 8267 | reserved = 1; |
| 8268 | } |
Souptick Joarder | a528a24 | 2018-06-06 19:54:44 +0530 | [diff] [blame] | 8269 | if (ret2) { |
| 8270 | ret = vmf_error(ret2); |
Chris Mason | 9998eb7 | 2012-01-25 13:47:40 -0500 | [diff] [blame] | 8271 | if (reserved) |
| 8272 | goto out; |
| 8273 | goto out_noreserve; |
Nick Piggin | 56a76f8 | 2009-03-31 15:23:23 -0700 | [diff] [blame] | 8274 | } |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 8275 | |
Nick Piggin | 56a76f8 | 2009-03-31 15:23:23 -0700 | [diff] [blame] | 8276 | ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */ |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8277 | again: |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8278 | lock_page(page); |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8279 | size = i_size_read(inode); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8280 | |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8281 | if ((page->mapping != inode->i_mapping) || |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8282 | (page_start >= size)) { |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8283 | /* page got truncated out from underneath us */ |
| 8284 | goto out_unlock; |
| 8285 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8286 | wait_on_page_writeback(page); |
| 8287 | |
David Sterba | ff13db4 | 2015-12-03 14:30:40 +0100 | [diff] [blame] | 8288 | lock_extent_bits(io_tree, page_start, page_end, &cached_state); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8289 | set_page_extent_mapped(page); |
| 8290 | |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 8291 | /* |
| 8292 | * we can't set the delalloc bits if there are pending ordered |
| 8293 | * extents. Drop our locks and wait for them to finish |
| 8294 | */ |
Nikolay Borisov | a776c6f | 2017-02-20 13:50:49 +0200 | [diff] [blame] | 8295 | ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start, |
| 8296 | PAGE_SIZE); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8297 | if (ordered) { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 8298 | unlock_extent_cached(io_tree, page_start, page_end, |
David Sterba | e43bbe5 | 2017-12-12 21:43:52 +0100 | [diff] [blame] | 8299 | &cached_state); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8300 | unlock_page(page); |
Nikolay Borisov | c0a4360 | 2020-09-18 12:15:53 +0300 | [diff] [blame] | 8301 | btrfs_start_ordered_extent(ordered, 1); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8302 | btrfs_put_ordered_extent(ordered); |
| 8303 | goto again; |
| 8304 | } |
| 8305 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 8306 | if (page->index == ((size - 1) >> PAGE_SHIFT)) { |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 8307 | reserved_space = round_up(size - page_start, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8308 | fs_info->sectorsize); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 8309 | if (reserved_space < PAGE_SIZE) { |
Chandan Rajendra | d0b7da88 | 2016-01-21 15:55:57 +0530 | [diff] [blame] | 8310 | end = page_start + reserved_space - 1; |
Nikolay Borisov | 86d5292 | 2020-06-03 08:55:40 +0300 | [diff] [blame] | 8311 | btrfs_delalloc_release_space(BTRFS_I(inode), |
| 8312 | data_reserved, page_start, |
| 8313 | PAGE_SIZE - reserved_space, true); |
Chandan Rajendra | d0b7da88 | 2016-01-21 15:55:57 +0530 | [diff] [blame] | 8314 | } |
| 8315 | } |
| 8316 | |
Josef Bacik | fbf1908 | 2009-10-01 17:10:23 -0400 | [diff] [blame] | 8317 | /* |
Liu Bo | 5416034 | 2016-12-13 12:15:19 -0800 | [diff] [blame] | 8318 | * page_mkwrite gets called when the page is firstly dirtied after it's |
| 8319 | * faulted in, but write(2) could also dirty a page and set delalloc |
| 8320 | * bits, thus in this case for space account reason, we still need to |
| 8321 | * clear any delalloc bits within this page range since we have to |
| 8322 | * reserve data&meta space before lock_page() (see above comments). |
Josef Bacik | fbf1908 | 2009-10-01 17:10:23 -0400 | [diff] [blame] | 8323 | */ |
Chandan Rajendra | d0b7da88 | 2016-01-21 15:55:57 +0530 | [diff] [blame] | 8324 | clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, end, |
Omar Sandoval | e182163 | 2019-08-15 14:04:04 -0700 | [diff] [blame] | 8325 | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING | |
| 8326 | EXTENT_DEFRAG, 0, 0, &cached_state); |
Josef Bacik | fbf1908 | 2009-10-01 17:10:23 -0400 | [diff] [blame] | 8327 | |
Nikolay Borisov | c2566f2 | 2020-06-03 08:55:35 +0300 | [diff] [blame] | 8328 | ret2 = btrfs_set_extent_delalloc(BTRFS_I(inode), page_start, end, 0, |
Nikolay Borisov | 330a582 | 2019-07-17 16:18:17 +0300 | [diff] [blame] | 8329 | &cached_state); |
Souptick Joarder | a528a24 | 2018-06-06 19:54:44 +0530 | [diff] [blame] | 8330 | if (ret2) { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 8331 | unlock_extent_cached(io_tree, page_start, page_end, |
David Sterba | e43bbe5 | 2017-12-12 21:43:52 +0100 | [diff] [blame] | 8332 | &cached_state); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 8333 | ret = VM_FAULT_SIGBUS; |
| 8334 | goto out_unlock; |
| 8335 | } |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8336 | |
| 8337 | /* page is wholly or partially inside EOF */ |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 8338 | if (page_start + PAGE_SIZE > size) |
Johannes Thumshirn | 7073017 | 2018-12-05 15:23:03 +0100 | [diff] [blame] | 8339 | zero_start = offset_in_page(size); |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8340 | else |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 8341 | zero_start = PAGE_SIZE; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8342 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 8343 | if (zero_start != PAGE_SIZE) { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8344 | kaddr = kmap(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 8345 | memset(kaddr + zero_start, 0, PAGE_SIZE - zero_start); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8346 | flush_dcache_page(page); |
| 8347 | kunmap(page); |
| 8348 | } |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 8349 | ClearPageChecked(page); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8350 | set_page_dirty(page); |
Chris Mason | 50a9b21 | 2009-09-11 12:33:12 -0400 | [diff] [blame] | 8351 | SetPageUptodate(page); |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 8352 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8353 | BTRFS_I(inode)->last_trans = fs_info->generation; |
Chris Mason | 257c62e | 2009-10-13 13:21:08 -0400 | [diff] [blame] | 8354 | BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid; |
Liu Bo | 46d8bc3 | 2012-08-29 01:07:55 -0600 | [diff] [blame] | 8355 | 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] | 8356 | |
David Sterba | e43bbe5 | 2017-12-12 21:43:52 +0100 | [diff] [blame] | 8357 | unlock_extent_cached(io_tree, page_start, page_end, &cached_state); |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8358 | |
Yunfeng Ye | 76de60e | 2019-12-03 16:59:25 +0800 | [diff] [blame] | 8359 | btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE); |
| 8360 | sb_end_pagefault(inode->i_sb); |
| 8361 | extent_changeset_free(data_reserved); |
| 8362 | return VM_FAULT_LOCKED; |
Chris Mason | 717beb9 | 2018-06-25 10:03:41 -0700 | [diff] [blame] | 8363 | |
| 8364 | out_unlock: |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8365 | unlock_page(page); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 8366 | out: |
Qu Wenruo | 8702ba9 | 2019-10-14 14:34:51 +0800 | [diff] [blame] | 8367 | btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE); |
Nikolay Borisov | 86d5292 | 2020-06-03 08:55:40 +0300 | [diff] [blame] | 8368 | btrfs_delalloc_release_space(BTRFS_I(inode), data_reserved, page_start, |
Qu Wenruo | 43b1859 | 2017-12-12 15:34:32 +0800 | [diff] [blame] | 8369 | reserved_space, (ret != 0)); |
Chris Mason | 9998eb7 | 2012-01-25 13:47:40 -0500 | [diff] [blame] | 8370 | out_noreserve: |
Jan Kara | b2b5ef5 | 2012-06-12 16:20:45 +0200 | [diff] [blame] | 8371 | sb_end_pagefault(inode->i_sb); |
Qu Wenruo | 364ecf3 | 2017-02-27 15:10:38 +0800 | [diff] [blame] | 8372 | extent_changeset_free(data_reserved); |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8373 | return ret; |
| 8374 | } |
| 8375 | |
Filipe Manana | 213e8c5 | 2018-02-06 20:40:31 +0000 | [diff] [blame] | 8376 | static int btrfs_truncate(struct inode *inode, bool skip_writeback) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8377 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8378 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8379 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 8380 | struct btrfs_block_rsv *rsv; |
Omar Sandoval | ad7e1a7 | 2018-05-22 09:59:50 -0700 | [diff] [blame] | 8381 | int ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8382 | struct btrfs_trans_handle *trans; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8383 | u64 mask = fs_info->sectorsize - 1; |
Josef Bacik | 2bd36e7 | 2019-08-22 15:14:33 -0400 | [diff] [blame] | 8384 | u64 min_size = btrfs_calc_metadata_size(fs_info, 1); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8385 | |
Filipe Manana | 213e8c5 | 2018-02-06 20:40:31 +0000 | [diff] [blame] | 8386 | if (!skip_writeback) { |
| 8387 | ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask), |
| 8388 | (u64)-1); |
| 8389 | if (ret) |
| 8390 | return ret; |
| 8391 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8392 | |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 8393 | /* |
Omar Sandoval | f7e9e8f | 2018-05-11 13:13:32 -0700 | [diff] [blame] | 8394 | * Yes ladies and gentlemen, this is indeed ugly. We have a couple of |
| 8395 | * things going on here: |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 8396 | * |
Omar Sandoval | f7e9e8f | 2018-05-11 13:13:32 -0700 | [diff] [blame] | 8397 | * 1) We need to reserve space to update our inode. |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 8398 | * |
Omar Sandoval | f7e9e8f | 2018-05-11 13:13:32 -0700 | [diff] [blame] | 8399 | * 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] | 8400 | * be free'd up by the truncate operation, but also have some slack |
| 8401 | * space reserved in case it uses space during the truncate (thank you |
| 8402 | * very much snapshotting). |
| 8403 | * |
Omar Sandoval | f7e9e8f | 2018-05-11 13:13:32 -0700 | [diff] [blame] | 8404 | * 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] | 8405 | * 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] | 8406 | * 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] | 8407 | * doesn't end up using space reserved for updating the inode. We also |
| 8408 | * need to be able to stop the transaction and start a new one, which |
| 8409 | * means we need to be able to update the inode several times, and we |
| 8410 | * have no idea of knowing how many times that will be, so we can't just |
| 8411 | * reserve 1 item for the entirety of the operation, so that has to be |
| 8412 | * done separately as well. |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 8413 | * |
| 8414 | * So that leaves us with |
| 8415 | * |
Omar Sandoval | f7e9e8f | 2018-05-11 13:13:32 -0700 | [diff] [blame] | 8416 | * 1) rsv - for the truncate reservation, which we will steal from the |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 8417 | * transaction reservation. |
Omar Sandoval | f7e9e8f | 2018-05-11 13:13:32 -0700 | [diff] [blame] | 8418 | * 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] | 8419 | * updating the inode. |
| 8420 | */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 8421 | rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP); |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 8422 | if (!rsv) |
| 8423 | return -ENOMEM; |
Josef Bacik | 4a33854 | 2011-08-29 11:01:31 -0400 | [diff] [blame] | 8424 | rsv->size = min_size; |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 8425 | rsv->failfast = 1; |
Josef Bacik | f0cd846 | 2011-03-04 14:37:08 -0500 | [diff] [blame] | 8426 | |
Josef Bacik | 907cbce | 2011-08-08 13:46:15 -0400 | [diff] [blame] | 8427 | /* |
Josef Bacik | 0712718 | 2011-08-19 10:29:59 -0400 | [diff] [blame] | 8428 | * 1 for the truncate slack space |
Josef Bacik | 907cbce | 2011-08-08 13:46:15 -0400 | [diff] [blame] | 8429 | * 1 for updating the inode. |
| 8430 | */ |
Josef Bacik | f3fe820 | 2013-01-07 17:03:21 -0500 | [diff] [blame] | 8431 | trans = btrfs_start_transaction(root, 2); |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 8432 | if (IS_ERR(trans)) { |
Omar Sandoval | ad7e1a7 | 2018-05-22 09:59:50 -0700 | [diff] [blame] | 8433 | ret = PTR_ERR(trans); |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 8434 | goto out; |
| 8435 | } |
Josef Bacik | f0cd846 | 2011-03-04 14:37:08 -0500 | [diff] [blame] | 8436 | |
Josef Bacik | 907cbce | 2011-08-08 13:46:15 -0400 | [diff] [blame] | 8437 | /* Migrate the slack space for the truncate to our reserve */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8438 | ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, rsv, |
Lu Fengqi | 3a58417 | 2018-08-04 21:10:55 +0800 | [diff] [blame] | 8439 | min_size, false); |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 8440 | BUG_ON(ret); |
Josef Bacik | f0cd846 | 2011-03-04 14:37:08 -0500 | [diff] [blame] | 8441 | |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 8442 | /* |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 8443 | * So if we truncate and then write and fsync we normally would just |
| 8444 | * write the extents that changed, which is a problem if we need to |
| 8445 | * first truncate that entire inode. So set this flag so we write out |
| 8446 | * all of the extents in the inode to the sync log so we're completely |
| 8447 | * safe. |
| 8448 | */ |
| 8449 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags); |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 8450 | trans->block_rsv = rsv; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 8451 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 8452 | while (1) { |
| 8453 | ret = btrfs_truncate_inode_items(trans, root, inode, |
| 8454 | inode->i_size, |
| 8455 | BTRFS_EXTENT_DATA_KEY); |
Josef Bacik | ddfae63 | 2017-10-19 14:16:02 -0400 | [diff] [blame] | 8456 | trans->block_rsv = &fs_info->trans_block_rsv; |
Omar Sandoval | ad7e1a7 | 2018-05-22 09:59:50 -0700 | [diff] [blame] | 8457 | if (ret != -ENOSPC && ret != -EAGAIN) |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 8458 | break; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8459 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 8460 | ret = btrfs_update_inode(trans, root, inode); |
Omar Sandoval | ad7e1a7 | 2018-05-22 09:59:50 -0700 | [diff] [blame] | 8461 | if (ret) |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 8462 | break; |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 8463 | |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 8464 | btrfs_end_transaction(trans); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 8465 | btrfs_btree_balance_dirty(fs_info); |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 8466 | |
| 8467 | trans = btrfs_start_transaction(root, 2); |
| 8468 | if (IS_ERR(trans)) { |
Omar Sandoval | ad7e1a7 | 2018-05-22 09:59:50 -0700 | [diff] [blame] | 8469 | ret = PTR_ERR(trans); |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 8470 | trans = NULL; |
| 8471 | break; |
| 8472 | } |
| 8473 | |
Nikolay Borisov | 63f018b | 2020-03-10 10:59:31 +0200 | [diff] [blame] | 8474 | btrfs_block_rsv_release(fs_info, rsv, -1, NULL); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8475 | ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, |
Lu Fengqi | 3a58417 | 2018-08-04 21:10:55 +0800 | [diff] [blame] | 8476 | rsv, min_size, false); |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 8477 | BUG_ON(ret); /* shouldn't happen */ |
| 8478 | trans->block_rsv = rsv; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 8479 | } |
| 8480 | |
Josef Bacik | ddfae63 | 2017-10-19 14:16:02 -0400 | [diff] [blame] | 8481 | /* |
| 8482 | * We can't call btrfs_truncate_block inside a trans handle as we could |
| 8483 | * deadlock with freeze, if we got NEED_TRUNCATE_BLOCK then we know |
| 8484 | * we've truncated everything except the last little bit, and can do |
| 8485 | * btrfs_truncate_block and then update the disk_i_size. |
| 8486 | */ |
| 8487 | if (ret == NEED_TRUNCATE_BLOCK) { |
| 8488 | btrfs_end_transaction(trans); |
| 8489 | btrfs_btree_balance_dirty(fs_info); |
| 8490 | |
| 8491 | ret = btrfs_truncate_block(inode, inode->i_size, 0, 0); |
| 8492 | if (ret) |
| 8493 | goto out; |
| 8494 | trans = btrfs_start_transaction(root, 1); |
| 8495 | if (IS_ERR(trans)) { |
| 8496 | ret = PTR_ERR(trans); |
| 8497 | goto out; |
| 8498 | } |
Josef Bacik | d923afe | 2020-01-17 09:02:23 -0500 | [diff] [blame] | 8499 | btrfs_inode_safe_disk_i_size_write(inode, 0); |
Josef Bacik | ddfae63 | 2017-10-19 14:16:02 -0400 | [diff] [blame] | 8500 | } |
| 8501 | |
Chris Mason | 917c16b | 2011-11-08 14:49:59 -0500 | [diff] [blame] | 8502 | if (trans) { |
Omar Sandoval | ad7e1a7 | 2018-05-22 09:59:50 -0700 | [diff] [blame] | 8503 | int ret2; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 8504 | |
Omar Sandoval | ad7e1a7 | 2018-05-22 09:59:50 -0700 | [diff] [blame] | 8505 | trans->block_rsv = &fs_info->trans_block_rsv; |
| 8506 | ret2 = btrfs_update_inode(trans, root, inode); |
| 8507 | if (ret2 && !ret) |
| 8508 | ret = ret2; |
| 8509 | |
| 8510 | ret2 = btrfs_end_transaction(trans); |
| 8511 | if (ret2 && !ret) |
| 8512 | ret = ret2; |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 8513 | btrfs_btree_balance_dirty(fs_info); |
Chris Mason | 917c16b | 2011-11-08 14:49:59 -0500 | [diff] [blame] | 8514 | } |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 8515 | out: |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 8516 | btrfs_free_block_rsv(fs_info, rsv); |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 8517 | |
Omar Sandoval | ad7e1a7 | 2018-05-22 09:59:50 -0700 | [diff] [blame] | 8518 | return ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8519 | } |
| 8520 | |
Sven Wegener | 3b96362 | 2008-06-09 21:57:42 -0400 | [diff] [blame] | 8521 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 8522 | * create a new subvolume directory/inode (helper for the ioctl). |
| 8523 | */ |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 8524 | int btrfs_create_subvol_root(struct btrfs_trans_handle *trans, |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 8525 | struct btrfs_root *new_root, |
| 8526 | struct btrfs_root *parent_root, |
| 8527 | u64 new_dirid) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8528 | { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8529 | struct inode *inode; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 8530 | int err; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 8531 | u64 index = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8532 | |
Florian Albrechtskirchinger | 12fc9d0 | 2012-02-10 22:15:54 +0100 | [diff] [blame] | 8533 | inode = btrfs_new_inode(trans, new_root, NULL, "..", 2, |
| 8534 | new_dirid, new_dirid, |
| 8535 | S_IFDIR | (~current_umask() & S_IRWXUGO), |
| 8536 | &index); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 8537 | if (IS_ERR(inode)) |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 8538 | return PTR_ERR(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8539 | inode->i_op = &btrfs_dir_inode_operations; |
| 8540 | inode->i_fop = &btrfs_dir_file_operations; |
| 8541 | |
Miklos Szeredi | bfe8684 | 2011-10-28 14:13:29 +0200 | [diff] [blame] | 8542 | set_nlink(inode, 1); |
Nikolay Borisov | 6ef06d2 | 2017-02-20 13:50:34 +0200 | [diff] [blame] | 8543 | btrfs_i_size_write(BTRFS_I(inode), 0); |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 8544 | unlock_new_inode(inode); |
Sven Wegener | 3b96362 | 2008-06-09 21:57:42 -0400 | [diff] [blame] | 8545 | |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 8546 | err = btrfs_subvol_inherit_props(trans, new_root, parent_root); |
| 8547 | if (err) |
| 8548 | btrfs_err(new_root->fs_info, |
David Sterba | 351fd35 | 2014-05-15 16:48:20 +0200 | [diff] [blame] | 8549 | "error inheriting subvolume %llu properties: %d", |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 8550 | new_root->root_key.objectid, err); |
| 8551 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 8552 | err = btrfs_update_inode(trans, new_root, inode); |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 8553 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 8554 | iput(inode); |
Mark Fasheh | ce59897 | 2011-07-26 11:32:23 -0700 | [diff] [blame] | 8555 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8556 | } |
| 8557 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8558 | struct inode *btrfs_alloc_inode(struct super_block *sb) |
| 8559 | { |
Josef Bacik | 69fe2d7 | 2017-10-19 14:15:57 -0400 | [diff] [blame] | 8560 | struct btrfs_fs_info *fs_info = btrfs_sb(sb); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8561 | struct btrfs_inode *ei; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8562 | struct inode *inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8563 | |
David Sterba | 712e36c | 2017-10-31 17:08:27 +0100 | [diff] [blame] | 8564 | ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_KERNEL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8565 | if (!ei) |
| 8566 | return NULL; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8567 | |
| 8568 | ei->root = NULL; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8569 | ei->generation = 0; |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 8570 | ei->last_trans = 0; |
Chris Mason | 257c62e | 2009-10-13 13:21:08 -0400 | [diff] [blame] | 8571 | ei->last_sub_trans = 0; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 8572 | ei->logged_trans = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8573 | ei->delalloc_bytes = 0; |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 8574 | ei->new_delalloc_bytes = 0; |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 8575 | ei->defrag_bytes = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8576 | ei->disk_i_size = 0; |
| 8577 | ei->flags = 0; |
Josef Bacik | 7709cde | 2011-08-04 10:25:02 -0400 | [diff] [blame] | 8578 | ei->csum_bytes = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8579 | ei->index_cnt = (u64)-1; |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 8580 | ei->dir_index = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8581 | ei->last_unlink_trans = 0; |
Filipe Manana | 3ebac17 | 2020-07-15 12:30:43 +0100 | [diff] [blame] | 8582 | ei->last_reflink_trans = 0; |
Liu Bo | 46d8bc3 | 2012-08-29 01:07:55 -0600 | [diff] [blame] | 8583 | ei->last_log_commit = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8584 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 8585 | spin_lock_init(&ei->lock); |
| 8586 | ei->outstanding_extents = 0; |
Josef Bacik | 69fe2d7 | 2017-10-19 14:15:57 -0400 | [diff] [blame] | 8587 | if (sb->s_magic != BTRFS_TEST_MAGIC) |
| 8588 | btrfs_init_metadata_block_rsv(fs_info, &ei->block_rsv, |
| 8589 | BTRFS_BLOCK_RSV_DELALLOC); |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 8590 | ei->runtime_flags = 0; |
David Sterba | b52aa8c | 2017-07-17 19:17:20 +0200 | [diff] [blame] | 8591 | ei->prop_compress = BTRFS_COMPRESS_NONE; |
David Sterba | eec63c6 | 2017-07-17 19:41:31 +0200 | [diff] [blame] | 8592 | ei->defrag_compress = BTRFS_COMPRESS_NONE; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8593 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 8594 | ei->delayed_node = NULL; |
| 8595 | |
chandan r | 9cc97d6 | 2012-07-04 12:48:07 +0530 | [diff] [blame] | 8596 | ei->i_otime.tv_sec = 0; |
| 8597 | ei->i_otime.tv_nsec = 0; |
| 8598 | |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8599 | inode = &ei->vfs_inode; |
David Sterba | a8067e0 | 2011-04-21 00:34:43 +0200 | [diff] [blame] | 8600 | extent_map_tree_init(&ei->extent_tree); |
Qu Wenruo | 43eb5f2 | 2019-03-01 10:47:59 +0800 | [diff] [blame] | 8601 | extent_io_tree_init(fs_info, &ei->io_tree, IO_TREE_INODE_IO, inode); |
| 8602 | extent_io_tree_init(fs_info, &ei->io_failure_tree, |
| 8603 | IO_TREE_INODE_IO_FAILURE, inode); |
Josef Bacik | 41a2ee7 | 2020-01-17 09:02:21 -0500 | [diff] [blame] | 8604 | extent_io_tree_init(fs_info, &ei->file_extent_tree, |
| 8605 | IO_TREE_INODE_FILE_EXTENT, inode); |
David Sterba | 7b43973 | 2019-03-11 15:58:30 +0100 | [diff] [blame] | 8606 | ei->io_tree.track_uptodate = true; |
| 8607 | ei->io_failure_tree.track_uptodate = true; |
Josef Bacik | b812ce2 | 2012-11-16 13:56:32 -0500 | [diff] [blame] | 8608 | atomic_set(&ei->sync_writers, 0); |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8609 | mutex_init(&ei->log_mutex); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8610 | btrfs_ordered_inode_tree_init(&ei->ordered_tree); |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8611 | INIT_LIST_HEAD(&ei->delalloc_inodes); |
David Sterba | 8089fe6 | 2015-11-19 14:15:51 +0100 | [diff] [blame] | 8612 | INIT_LIST_HEAD(&ei->delayed_iput); |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8613 | RB_CLEAR_NODE(&ei->rb_node); |
| 8614 | |
| 8615 | return inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8616 | } |
| 8617 | |
Josef Bacik | aaedb55 | 2013-10-11 14:44:09 -0400 | [diff] [blame] | 8618 | #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS |
| 8619 | void btrfs_test_destroy_inode(struct inode *inode) |
| 8620 | { |
Nikolay Borisov | dcdbc05 | 2017-02-20 13:50:45 +0200 | [diff] [blame] | 8621 | btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0); |
Josef Bacik | aaedb55 | 2013-10-11 14:44:09 -0400 | [diff] [blame] | 8622 | kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode)); |
| 8623 | } |
| 8624 | #endif |
| 8625 | |
Al Viro | 26602ca | 2019-04-10 15:14:41 -0400 | [diff] [blame] | 8626 | void btrfs_free_inode(struct inode *inode) |
Nick Piggin | fa0d7e3d | 2011-01-07 17:49:49 +1100 | [diff] [blame] | 8627 | { |
Nick Piggin | fa0d7e3d | 2011-01-07 17:49:49 +1100 | [diff] [blame] | 8628 | kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode)); |
| 8629 | } |
| 8630 | |
Nikolay Borisov | 633cc81 | 2020-09-18 12:15:49 +0300 | [diff] [blame] | 8631 | void btrfs_destroy_inode(struct inode *vfs_inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8632 | { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8633 | struct btrfs_ordered_extent *ordered; |
Nikolay Borisov | 633cc81 | 2020-09-18 12:15:49 +0300 | [diff] [blame] | 8634 | struct btrfs_inode *inode = BTRFS_I(vfs_inode); |
| 8635 | struct btrfs_root *root = inode->root; |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 8636 | |
Nikolay Borisov | 633cc81 | 2020-09-18 12:15:49 +0300 | [diff] [blame] | 8637 | WARN_ON(!hlist_empty(&vfs_inode->i_dentry)); |
| 8638 | WARN_ON(vfs_inode->i_data.nrpages); |
| 8639 | WARN_ON(inode->block_rsv.reserved); |
| 8640 | WARN_ON(inode->block_rsv.size); |
| 8641 | WARN_ON(inode->outstanding_extents); |
| 8642 | WARN_ON(inode->delalloc_bytes); |
| 8643 | WARN_ON(inode->new_delalloc_bytes); |
| 8644 | WARN_ON(inode->csum_bytes); |
| 8645 | WARN_ON(inode->defrag_bytes); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8646 | |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 8647 | /* |
Josef Bacik | a6dbd42 | 2009-11-11 15:53:34 -0500 | [diff] [blame] | 8648 | * This can happen where we create an inode, but somebody else also |
| 8649 | * created the same inode and we need to destroy the one we already |
| 8650 | * created. |
| 8651 | */ |
| 8652 | if (!root) |
Al Viro | 26602ca | 2019-04-10 15:14:41 -0400 | [diff] [blame] | 8653 | return; |
Josef Bacik | a6dbd42 | 2009-11-11 15:53:34 -0500 | [diff] [blame] | 8654 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 8655 | while (1) { |
Nikolay Borisov | 633cc81 | 2020-09-18 12:15:49 +0300 | [diff] [blame] | 8656 | ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8657 | if (!ordered) |
| 8658 | break; |
| 8659 | else { |
Nikolay Borisov | 633cc81 | 2020-09-18 12:15:49 +0300 | [diff] [blame] | 8660 | btrfs_err(root->fs_info, |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 8661 | "found ordered extent %llu %llu on inode cleanup", |
Omar Sandoval | bffe633 | 2019-12-02 17:34:19 -0800 | [diff] [blame] | 8662 | ordered->file_offset, ordered->num_bytes); |
Nikolay Borisov | 71fe0a5 | 2020-09-18 12:15:50 +0300 | [diff] [blame] | 8663 | btrfs_remove_ordered_extent(inode, ordered); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8664 | btrfs_put_ordered_extent(ordered); |
| 8665 | btrfs_put_ordered_extent(ordered); |
| 8666 | } |
| 8667 | } |
Nikolay Borisov | 633cc81 | 2020-09-18 12:15:49 +0300 | [diff] [blame] | 8668 | btrfs_qgroup_check_reserved_leak(inode); |
| 8669 | inode_tree_del(inode); |
| 8670 | btrfs_drop_extent_cache(inode, 0, (u64)-1, 0); |
| 8671 | btrfs_inode_clear_file_extent_range(inode, 0, (u64)-1); |
| 8672 | btrfs_put_root(inode->root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8673 | } |
| 8674 | |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 8675 | int btrfs_drop_inode(struct inode *inode) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 8676 | { |
| 8677 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 8678 | |
Naohiro Aota | 6379ef9 | 2013-06-06 09:56:34 +0000 | [diff] [blame] | 8679 | if (root == NULL) |
| 8680 | return 1; |
| 8681 | |
Liu Bo | fa6ac87 | 2013-02-20 14:10:23 +0000 | [diff] [blame] | 8682 | /* the snap/subvol tree is on deleting */ |
Stefan Behrens | 69e9c6c | 2013-09-05 16:58:43 +0200 | [diff] [blame] | 8683 | if (btrfs_root_refs(&root->root_item) == 0) |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 8684 | return 1; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 8685 | else |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 8686 | return generic_drop_inode(inode); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 8687 | } |
| 8688 | |
Sven Wegener | 0ee0fda | 2008-07-30 16:54:26 -0400 | [diff] [blame] | 8689 | static void init_once(void *foo) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8690 | { |
| 8691 | struct btrfs_inode *ei = (struct btrfs_inode *) foo; |
| 8692 | |
| 8693 | inode_init_once(&ei->vfs_inode); |
| 8694 | } |
| 8695 | |
David Sterba | e67c718 | 2018-02-19 17:24:18 +0100 | [diff] [blame] | 8696 | void __cold btrfs_destroy_cachep(void) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8697 | { |
Kirill A. Shutemov | 8c0a853 | 2012-09-26 11:33:07 +1000 | [diff] [blame] | 8698 | /* |
| 8699 | * Make sure all delayed rcu free inodes are flushed before we |
| 8700 | * destroy cache. |
| 8701 | */ |
| 8702 | rcu_barrier(); |
Kinglong Mee | 5598e90 | 2016-01-29 21:36:35 +0800 | [diff] [blame] | 8703 | kmem_cache_destroy(btrfs_inode_cachep); |
| 8704 | kmem_cache_destroy(btrfs_trans_handle_cachep); |
Kinglong Mee | 5598e90 | 2016-01-29 21:36:35 +0800 | [diff] [blame] | 8705 | kmem_cache_destroy(btrfs_path_cachep); |
| 8706 | kmem_cache_destroy(btrfs_free_space_cachep); |
Christophe Leroy | 3acd485 | 2019-08-21 15:05:55 +0000 | [diff] [blame] | 8707 | kmem_cache_destroy(btrfs_free_space_bitmap_cachep); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8708 | } |
| 8709 | |
Liu Bo | f5c29bd | 2017-11-02 17:21:50 -0600 | [diff] [blame] | 8710 | int __init btrfs_init_cachep(void) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8711 | { |
David Sterba | 837e197 | 2012-09-07 03:00:48 -0600 | [diff] [blame] | 8712 | btrfs_inode_cachep = kmem_cache_create("btrfs_inode", |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 8713 | sizeof(struct btrfs_inode), 0, |
Vladimir Davydov | 5d09705 | 2016-01-14 15:18:21 -0800 | [diff] [blame] | 8714 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT, |
| 8715 | init_once); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8716 | if (!btrfs_inode_cachep) |
| 8717 | goto fail; |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 8718 | |
David Sterba | 837e197 | 2012-09-07 03:00:48 -0600 | [diff] [blame] | 8719 | btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle", |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 8720 | sizeof(struct btrfs_trans_handle), 0, |
Nikolay Borisov | fba4b69 | 2016-06-23 21:17:08 +0300 | [diff] [blame] | 8721 | SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8722 | if (!btrfs_trans_handle_cachep) |
| 8723 | goto fail; |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 8724 | |
David Sterba | 837e197 | 2012-09-07 03:00:48 -0600 | [diff] [blame] | 8725 | btrfs_path_cachep = kmem_cache_create("btrfs_path", |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 8726 | sizeof(struct btrfs_path), 0, |
Nikolay Borisov | fba4b69 | 2016-06-23 21:17:08 +0300 | [diff] [blame] | 8727 | SLAB_MEM_SPREAD, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8728 | if (!btrfs_path_cachep) |
| 8729 | goto fail; |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 8730 | |
David Sterba | 837e197 | 2012-09-07 03:00:48 -0600 | [diff] [blame] | 8731 | btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space", |
Josef Bacik | dc89e98 | 2011-01-28 17:05:48 -0500 | [diff] [blame] | 8732 | sizeof(struct btrfs_free_space), 0, |
Nikolay Borisov | fba4b69 | 2016-06-23 21:17:08 +0300 | [diff] [blame] | 8733 | SLAB_MEM_SPREAD, NULL); |
Josef Bacik | dc89e98 | 2011-01-28 17:05:48 -0500 | [diff] [blame] | 8734 | if (!btrfs_free_space_cachep) |
| 8735 | goto fail; |
| 8736 | |
Christophe Leroy | 3acd485 | 2019-08-21 15:05:55 +0000 | [diff] [blame] | 8737 | btrfs_free_space_bitmap_cachep = kmem_cache_create("btrfs_free_space_bitmap", |
| 8738 | PAGE_SIZE, PAGE_SIZE, |
| 8739 | SLAB_RED_ZONE, NULL); |
| 8740 | if (!btrfs_free_space_bitmap_cachep) |
| 8741 | goto fail; |
| 8742 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8743 | return 0; |
| 8744 | fail: |
| 8745 | btrfs_destroy_cachep(); |
| 8746 | return -ENOMEM; |
| 8747 | } |
| 8748 | |
David Howells | a528d35 | 2017-01-31 16:46:22 +0000 | [diff] [blame] | 8749 | static int btrfs_getattr(const struct path *path, struct kstat *stat, |
| 8750 | u32 request_mask, unsigned int flags) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8751 | { |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 8752 | u64 delalloc_bytes; |
David Howells | a528d35 | 2017-01-31 16:46:22 +0000 | [diff] [blame] | 8753 | struct inode *inode = d_inode(path->dentry); |
David Sterba | fadc0d8 | 2011-11-20 07:33:38 -0500 | [diff] [blame] | 8754 | u32 blocksize = inode->i_sb->s_blocksize; |
Yonghong Song | 04a87e3 | 2017-05-12 15:07:43 -0700 | [diff] [blame] | 8755 | u32 bi_flags = BTRFS_I(inode)->flags; |
| 8756 | |
| 8757 | stat->result_mask |= STATX_BTIME; |
| 8758 | stat->btime.tv_sec = BTRFS_I(inode)->i_otime.tv_sec; |
| 8759 | stat->btime.tv_nsec = BTRFS_I(inode)->i_otime.tv_nsec; |
| 8760 | if (bi_flags & BTRFS_INODE_APPEND) |
| 8761 | stat->attributes |= STATX_ATTR_APPEND; |
| 8762 | if (bi_flags & BTRFS_INODE_COMPRESS) |
| 8763 | stat->attributes |= STATX_ATTR_COMPRESSED; |
| 8764 | if (bi_flags & BTRFS_INODE_IMMUTABLE) |
| 8765 | stat->attributes |= STATX_ATTR_IMMUTABLE; |
| 8766 | if (bi_flags & BTRFS_INODE_NODUMP) |
| 8767 | stat->attributes |= STATX_ATTR_NODUMP; |
| 8768 | |
| 8769 | stat->attributes_mask |= (STATX_ATTR_APPEND | |
| 8770 | STATX_ATTR_COMPRESSED | |
| 8771 | STATX_ATTR_IMMUTABLE | |
| 8772 | STATX_ATTR_NODUMP); |
David Sterba | fadc0d8 | 2011-11-20 07:33:38 -0500 | [diff] [blame] | 8773 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8774 | generic_fillattr(inode, stat); |
Al Viro | 0ee5dc6 | 2011-07-07 15:44:25 -0400 | [diff] [blame] | 8775 | stat->dev = BTRFS_I(inode)->root->anon_dev; |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 8776 | |
| 8777 | spin_lock(&BTRFS_I(inode)->lock); |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 8778 | delalloc_bytes = BTRFS_I(inode)->new_delalloc_bytes; |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 8779 | spin_unlock(&BTRFS_I(inode)->lock); |
David Sterba | fadc0d8 | 2011-11-20 07:33:38 -0500 | [diff] [blame] | 8780 | stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) + |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 8781 | ALIGN(delalloc_bytes, blocksize)) >> 9; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8782 | return 0; |
| 8783 | } |
| 8784 | |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 8785 | static int btrfs_rename_exchange(struct inode *old_dir, |
| 8786 | struct dentry *old_dentry, |
| 8787 | struct inode *new_dir, |
| 8788 | struct dentry *new_dentry) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8789 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8790 | struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8791 | struct btrfs_trans_handle *trans; |
| 8792 | struct btrfs_root *root = BTRFS_I(old_dir)->root; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 8793 | struct btrfs_root *dest = BTRFS_I(new_dir)->root; |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 8794 | struct inode *new_inode = new_dentry->d_inode; |
| 8795 | struct inode *old_inode = old_dentry->d_inode; |
Deepa Dinamani | 95582b0 | 2018-05-08 19:36:02 -0700 | [diff] [blame] | 8796 | struct timespec64 ctime = current_time(old_inode); |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 8797 | u64 old_ino = btrfs_ino(BTRFS_I(old_inode)); |
| 8798 | u64 new_ino = btrfs_ino(BTRFS_I(new_inode)); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 8799 | u64 old_idx = 0; |
| 8800 | u64 new_idx = 0; |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 8801 | int ret; |
Filipe Manana | 75b463d | 2020-08-11 12:43:48 +0100 | [diff] [blame] | 8802 | int ret2; |
Filipe Manana | 86e8aa0 | 2016-05-05 02:02:27 +0100 | [diff] [blame] | 8803 | bool root_log_pinned = false; |
| 8804 | bool dest_log_pinned = false; |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 8805 | |
| 8806 | /* we only allow rename subvolume link between subvolumes */ |
| 8807 | if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest) |
| 8808 | return -EXDEV; |
| 8809 | |
| 8810 | /* close the race window with snapshot create/destroy ioctl */ |
Josef Bacik | 943eb3b | 2019-11-19 13:59:20 -0500 | [diff] [blame] | 8811 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID || |
| 8812 | new_ino == BTRFS_FIRST_FREE_OBJECTID) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8813 | down_read(&fs_info->subvol_sem); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 8814 | |
| 8815 | /* |
| 8816 | * We want to reserve the absolute worst case amount of items. So if |
| 8817 | * both inodes are subvols and we need to unlink them then that would |
| 8818 | * require 4 item modifications, but if they are both normal inodes it |
| 8819 | * would require 5 item modifications, so we'll assume their normal |
| 8820 | * inodes. So 5 * 2 is 10, plus 2 for the new links, so 12 total items |
| 8821 | * should cover the worst case number of items we'll modify. |
| 8822 | */ |
| 8823 | trans = btrfs_start_transaction(root, 12); |
| 8824 | if (IS_ERR(trans)) { |
| 8825 | ret = PTR_ERR(trans); |
| 8826 | goto out_notrans; |
| 8827 | } |
| 8828 | |
Josef Bacik | 3e17409 | 2019-11-15 15:43:06 -0500 | [diff] [blame] | 8829 | if (dest != root) |
| 8830 | btrfs_record_root_in_trans(trans, dest); |
| 8831 | |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 8832 | /* |
| 8833 | * We need to find a free sequence number both in the source and |
| 8834 | * in the destination directory for the exchange. |
| 8835 | */ |
Nikolay Borisov | 877574e | 2017-02-20 13:50:33 +0200 | [diff] [blame] | 8836 | ret = btrfs_set_inode_index(BTRFS_I(new_dir), &old_idx); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 8837 | if (ret) |
| 8838 | goto out_fail; |
Nikolay Borisov | 877574e | 2017-02-20 13:50:33 +0200 | [diff] [blame] | 8839 | ret = btrfs_set_inode_index(BTRFS_I(old_dir), &new_idx); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 8840 | if (ret) |
| 8841 | goto out_fail; |
| 8842 | |
| 8843 | BTRFS_I(old_inode)->dir_index = 0ULL; |
| 8844 | BTRFS_I(new_inode)->dir_index = 0ULL; |
| 8845 | |
| 8846 | /* Reference for the source. */ |
| 8847 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) { |
| 8848 | /* force full log commit if subvolume involved. */ |
David Sterba | 9078776 | 2019-03-20 13:28:05 +0100 | [diff] [blame] | 8849 | btrfs_set_log_full_commit(trans); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 8850 | } else { |
Filipe Manana | 376e5a5 | 2016-05-05 02:08:56 +0100 | [diff] [blame] | 8851 | btrfs_pin_log_trans(root); |
| 8852 | root_log_pinned = true; |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 8853 | ret = btrfs_insert_inode_ref(trans, dest, |
| 8854 | new_dentry->d_name.name, |
| 8855 | new_dentry->d_name.len, |
| 8856 | old_ino, |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 8857 | btrfs_ino(BTRFS_I(new_dir)), |
| 8858 | old_idx); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 8859 | if (ret) |
| 8860 | goto out_fail; |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 8861 | } |
| 8862 | |
| 8863 | /* And now for the dest. */ |
| 8864 | if (new_ino == BTRFS_FIRST_FREE_OBJECTID) { |
| 8865 | /* force full log commit if subvolume involved. */ |
David Sterba | 9078776 | 2019-03-20 13:28:05 +0100 | [diff] [blame] | 8866 | btrfs_set_log_full_commit(trans); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 8867 | } else { |
Filipe Manana | 376e5a5 | 2016-05-05 02:08:56 +0100 | [diff] [blame] | 8868 | btrfs_pin_log_trans(dest); |
| 8869 | dest_log_pinned = true; |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 8870 | ret = btrfs_insert_inode_ref(trans, root, |
| 8871 | old_dentry->d_name.name, |
| 8872 | old_dentry->d_name.len, |
| 8873 | new_ino, |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 8874 | btrfs_ino(BTRFS_I(old_dir)), |
| 8875 | new_idx); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 8876 | if (ret) |
| 8877 | goto out_fail; |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 8878 | } |
| 8879 | |
| 8880 | /* Update inode version and ctime/mtime. */ |
| 8881 | inode_inc_iversion(old_dir); |
| 8882 | inode_inc_iversion(new_dir); |
| 8883 | inode_inc_iversion(old_inode); |
| 8884 | inode_inc_iversion(new_inode); |
| 8885 | old_dir->i_ctime = old_dir->i_mtime = ctime; |
| 8886 | new_dir->i_ctime = new_dir->i_mtime = ctime; |
| 8887 | old_inode->i_ctime = ctime; |
| 8888 | new_inode->i_ctime = ctime; |
| 8889 | |
| 8890 | if (old_dentry->d_parent != new_dentry->d_parent) { |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 8891 | btrfs_record_unlink_dir(trans, BTRFS_I(old_dir), |
| 8892 | BTRFS_I(old_inode), 1); |
| 8893 | btrfs_record_unlink_dir(trans, BTRFS_I(new_dir), |
| 8894 | BTRFS_I(new_inode), 1); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 8895 | } |
| 8896 | |
| 8897 | /* src is a subvolume */ |
| 8898 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) { |
Josef Bacik | 045d396 | 2019-12-18 17:20:27 -0500 | [diff] [blame] | 8899 | ret = btrfs_unlink_subvol(trans, old_dir, old_dentry); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 8900 | } else { /* src is an inode */ |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 8901 | ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir), |
| 8902 | BTRFS_I(old_dentry->d_inode), |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 8903 | old_dentry->d_name.name, |
| 8904 | old_dentry->d_name.len); |
| 8905 | if (!ret) |
| 8906 | ret = btrfs_update_inode(trans, root, old_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 | |
| 8913 | /* dest is a subvolume */ |
| 8914 | if (new_ino == BTRFS_FIRST_FREE_OBJECTID) { |
Josef Bacik | 045d396 | 2019-12-18 17:20:27 -0500 | [diff] [blame] | 8915 | ret = btrfs_unlink_subvol(trans, new_dir, new_dentry); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 8916 | } else { /* dest is an inode */ |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 8917 | ret = __btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir), |
| 8918 | BTRFS_I(new_dentry->d_inode), |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 8919 | new_dentry->d_name.name, |
| 8920 | new_dentry->d_name.len); |
| 8921 | if (!ret) |
| 8922 | ret = btrfs_update_inode(trans, dest, new_inode); |
| 8923 | } |
| 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 | |
Nikolay Borisov | db0a669 | 2017-02-20 13:51:08 +0200 | [diff] [blame] | 8929 | 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] | 8930 | new_dentry->d_name.name, |
| 8931 | new_dentry->d_name.len, 0, old_idx); |
| 8932 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 8933 | btrfs_abort_transaction(trans, ret); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 8934 | goto out_fail; |
| 8935 | } |
| 8936 | |
Nikolay Borisov | db0a669 | 2017-02-20 13:51:08 +0200 | [diff] [blame] | 8937 | 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] | 8938 | old_dentry->d_name.name, |
| 8939 | old_dentry->d_name.len, 0, new_idx); |
| 8940 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 8941 | btrfs_abort_transaction(trans, ret); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 8942 | goto out_fail; |
| 8943 | } |
| 8944 | |
| 8945 | if (old_inode->i_nlink == 1) |
| 8946 | BTRFS_I(old_inode)->dir_index = old_idx; |
| 8947 | if (new_inode->i_nlink == 1) |
| 8948 | BTRFS_I(new_inode)->dir_index = new_idx; |
| 8949 | |
Filipe Manana | 86e8aa0 | 2016-05-05 02:02:27 +0100 | [diff] [blame] | 8950 | if (root_log_pinned) { |
Filipe Manana | 75b463d | 2020-08-11 12:43:48 +0100 | [diff] [blame] | 8951 | btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir), |
| 8952 | new_dentry->d_parent); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 8953 | btrfs_end_log_trans(root); |
Filipe Manana | 86e8aa0 | 2016-05-05 02:02:27 +0100 | [diff] [blame] | 8954 | root_log_pinned = false; |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 8955 | } |
Filipe Manana | 86e8aa0 | 2016-05-05 02:02:27 +0100 | [diff] [blame] | 8956 | if (dest_log_pinned) { |
Filipe Manana | 75b463d | 2020-08-11 12:43:48 +0100 | [diff] [blame] | 8957 | btrfs_log_new_name(trans, BTRFS_I(new_inode), BTRFS_I(new_dir), |
| 8958 | old_dentry->d_parent); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 8959 | btrfs_end_log_trans(dest); |
Filipe Manana | 86e8aa0 | 2016-05-05 02:02:27 +0100 | [diff] [blame] | 8960 | dest_log_pinned = false; |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 8961 | } |
| 8962 | out_fail: |
Filipe Manana | 86e8aa0 | 2016-05-05 02:02:27 +0100 | [diff] [blame] | 8963 | /* |
| 8964 | * If we have pinned a log and an error happened, we unpin tasks |
| 8965 | * trying to sync the log and force them to fallback to a transaction |
| 8966 | * commit if the log currently contains any of the inodes involved in |
| 8967 | * this rename operation (to ensure we do not persist a log with an |
| 8968 | * inconsistent state for any of these inodes or leading to any |
| 8969 | * inconsistencies when replayed). If the transaction was aborted, the |
| 8970 | * abortion reason is propagated to userspace when attempting to commit |
| 8971 | * the transaction. If the log does not contain any of these inodes, we |
| 8972 | * allow the tasks to sync it. |
| 8973 | */ |
| 8974 | if (ret && (root_log_pinned || dest_log_pinned)) { |
Nikolay Borisov | 0f8939b | 2017-01-18 00:31:30 +0200 | [diff] [blame] | 8975 | if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) || |
| 8976 | btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) || |
| 8977 | btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) || |
Filipe Manana | 86e8aa0 | 2016-05-05 02:02:27 +0100 | [diff] [blame] | 8978 | (new_inode && |
Nikolay Borisov | 0f8939b | 2017-01-18 00:31:30 +0200 | [diff] [blame] | 8979 | btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation))) |
David Sterba | 9078776 | 2019-03-20 13:28:05 +0100 | [diff] [blame] | 8980 | btrfs_set_log_full_commit(trans); |
Filipe Manana | 86e8aa0 | 2016-05-05 02:02:27 +0100 | [diff] [blame] | 8981 | |
| 8982 | if (root_log_pinned) { |
| 8983 | btrfs_end_log_trans(root); |
| 8984 | root_log_pinned = false; |
| 8985 | } |
| 8986 | if (dest_log_pinned) { |
| 8987 | btrfs_end_log_trans(dest); |
| 8988 | dest_log_pinned = false; |
| 8989 | } |
| 8990 | } |
Filipe Manana | 75b463d | 2020-08-11 12:43:48 +0100 | [diff] [blame] | 8991 | ret2 = btrfs_end_transaction(trans); |
| 8992 | ret = ret ? ret : ret2; |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 8993 | out_notrans: |
Josef Bacik | 943eb3b | 2019-11-19 13:59:20 -0500 | [diff] [blame] | 8994 | if (new_ino == BTRFS_FIRST_FREE_OBJECTID || |
| 8995 | old_ino == BTRFS_FIRST_FREE_OBJECTID) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8996 | up_read(&fs_info->subvol_sem); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 8997 | |
| 8998 | return ret; |
| 8999 | } |
| 9000 | |
| 9001 | static int btrfs_whiteout_for_rename(struct btrfs_trans_handle *trans, |
| 9002 | struct btrfs_root *root, |
| 9003 | struct inode *dir, |
| 9004 | struct dentry *dentry) |
| 9005 | { |
| 9006 | int ret; |
| 9007 | struct inode *inode; |
| 9008 | u64 objectid; |
| 9009 | u64 index; |
| 9010 | |
| 9011 | ret = btrfs_find_free_ino(root, &objectid); |
| 9012 | if (ret) |
| 9013 | return ret; |
| 9014 | |
| 9015 | inode = btrfs_new_inode(trans, root, dir, |
| 9016 | dentry->d_name.name, |
| 9017 | dentry->d_name.len, |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 9018 | btrfs_ino(BTRFS_I(dir)), |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9019 | objectid, |
| 9020 | S_IFCHR | WHITEOUT_MODE, |
| 9021 | &index); |
| 9022 | |
| 9023 | if (IS_ERR(inode)) { |
| 9024 | ret = PTR_ERR(inode); |
| 9025 | return ret; |
| 9026 | } |
| 9027 | |
| 9028 | inode->i_op = &btrfs_special_inode_operations; |
| 9029 | init_special_inode(inode, inode->i_mode, |
| 9030 | WHITEOUT_DEV); |
| 9031 | |
| 9032 | ret = btrfs_init_inode_security(trans, inode, dir, |
| 9033 | &dentry->d_name); |
| 9034 | if (ret) |
Filipe Manana | c990161 | 2016-05-05 01:41:57 +0100 | [diff] [blame] | 9035 | goto out; |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9036 | |
Nikolay Borisov | cef415a | 2017-02-20 13:51:09 +0200 | [diff] [blame] | 9037 | ret = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, |
| 9038 | BTRFS_I(inode), 0, index); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9039 | if (ret) |
Filipe Manana | c990161 | 2016-05-05 01:41:57 +0100 | [diff] [blame] | 9040 | goto out; |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9041 | |
| 9042 | ret = btrfs_update_inode(trans, root, inode); |
Filipe Manana | c990161 | 2016-05-05 01:41:57 +0100 | [diff] [blame] | 9043 | out: |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9044 | unlock_new_inode(inode); |
Filipe Manana | c990161 | 2016-05-05 01:41:57 +0100 | [diff] [blame] | 9045 | if (ret) |
| 9046 | inode_dec_link_count(inode); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9047 | iput(inode); |
| 9048 | |
Filipe Manana | c990161 | 2016-05-05 01:41:57 +0100 | [diff] [blame] | 9049 | return ret; |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9050 | } |
| 9051 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9052 | static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry, |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9053 | struct inode *new_dir, struct dentry *new_dentry, |
| 9054 | unsigned int flags) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9055 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9056 | struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9057 | struct btrfs_trans_handle *trans; |
Filipe Manana | 5062af3 | 2016-05-05 10:26:26 +0100 | [diff] [blame] | 9058 | unsigned int trans_num_items; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9059 | struct btrfs_root *root = BTRFS_I(old_dir)->root; |
| 9060 | struct btrfs_root *dest = BTRFS_I(new_dir)->root; |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 9061 | struct inode *new_inode = d_inode(new_dentry); |
| 9062 | struct inode *old_inode = d_inode(old_dentry); |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 9063 | u64 index = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9064 | int ret; |
Filipe Manana | 75b463d | 2020-08-11 12:43:48 +0100 | [diff] [blame] | 9065 | int ret2; |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 9066 | u64 old_ino = btrfs_ino(BTRFS_I(old_inode)); |
Filipe Manana | 3dc9e8f7 | 2016-04-29 11:34:22 +0100 | [diff] [blame] | 9067 | bool log_pinned = false; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9068 | |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 9069 | if (btrfs_ino(BTRFS_I(new_dir)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) |
Yan, Zheng | f679a84 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 9070 | return -EPERM; |
| 9071 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9072 | /* we only allow rename subvolume link between subvolumes */ |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 9073 | if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest) |
Chris Mason | 3394e16 | 2008-11-17 20:42:26 -0500 | [diff] [blame] | 9074 | return -EXDEV; |
| 9075 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 9076 | if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID || |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 9077 | (new_inode && btrfs_ino(BTRFS_I(new_inode)) == BTRFS_FIRST_FREE_OBJECTID)) |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9078 | return -ENOTEMPTY; |
| 9079 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9080 | if (S_ISDIR(old_inode->i_mode) && new_inode && |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9081 | new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9082 | return -ENOTEMPTY; |
Chris Mason | 9c52057 | 2012-12-17 14:26:57 -0500 | [diff] [blame] | 9083 | |
| 9084 | |
| 9085 | /* check for collisions, even if the name isn't there */ |
Josef Bacik | 4871c15 | 2013-10-09 12:24:04 -0400 | [diff] [blame] | 9086 | ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino, |
Chris Mason | 9c52057 | 2012-12-17 14:26:57 -0500 | [diff] [blame] | 9087 | new_dentry->d_name.name, |
| 9088 | new_dentry->d_name.len); |
| 9089 | |
| 9090 | if (ret) { |
| 9091 | if (ret == -EEXIST) { |
| 9092 | /* we shouldn't get |
| 9093 | * eexist without a new_inode */ |
Dulshani Gunawardhana | fae7f21 | 2013-10-31 10:30:08 +0530 | [diff] [blame] | 9094 | if (WARN_ON(!new_inode)) { |
Chris Mason | 9c52057 | 2012-12-17 14:26:57 -0500 | [diff] [blame] | 9095 | return ret; |
| 9096 | } |
| 9097 | } else { |
| 9098 | /* maybe -EOVERFLOW */ |
| 9099 | return ret; |
| 9100 | } |
| 9101 | } |
| 9102 | ret = 0; |
| 9103 | |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 9104 | /* |
Chris Mason | 8d875f9 | 2014-08-12 10:47:42 -0700 | [diff] [blame] | 9105 | * we're using rename to replace one file with another. Start IO on it |
| 9106 | * 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] | 9107 | */ |
Chris Mason | 8d875f9 | 2014-08-12 10:47:42 -0700 | [diff] [blame] | 9108 | 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] | 9109 | filemap_flush(old_inode->i_mapping); |
| 9110 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 9111 | /* close the racy window with snapshot create/destroy ioctl */ |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 9112 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9113 | down_read(&fs_info->subvol_sem); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 9114 | /* |
| 9115 | * We want to reserve the absolute worst case amount of items. So if |
| 9116 | * both inodes are subvols and we need to unlink them then that would |
| 9117 | * require 4 item modifications, but if they are both normal inodes it |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9118 | * would require 5 item modifications, so we'll assume they are normal |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 9119 | * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items |
| 9120 | * should cover the worst case number of items we'll modify. |
Filipe Manana | 5062af3 | 2016-05-05 10:26:26 +0100 | [diff] [blame] | 9121 | * If our rename has the whiteout flag, we need more 5 units for the |
| 9122 | * new inode (1 inode item, 1 inode ref, 2 dir items and 1 xattr item |
| 9123 | * when selinux is enabled). |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 9124 | */ |
Filipe Manana | 5062af3 | 2016-05-05 10:26:26 +0100 | [diff] [blame] | 9125 | trans_num_items = 11; |
| 9126 | if (flags & RENAME_WHITEOUT) |
| 9127 | trans_num_items += 5; |
| 9128 | trans = btrfs_start_transaction(root, trans_num_items); |
Johann Lombardi | b44c59a | 2011-03-31 13:23:47 +0000 | [diff] [blame] | 9129 | if (IS_ERR(trans)) { |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9130 | ret = PTR_ERR(trans); |
| 9131 | goto out_notrans; |
| 9132 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 9133 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9134 | if (dest != root) |
| 9135 | btrfs_record_root_in_trans(trans, dest); |
| 9136 | |
Nikolay Borisov | 877574e | 2017-02-20 13:50:33 +0200 | [diff] [blame] | 9137 | ret = btrfs_set_inode_index(BTRFS_I(new_dir), &index); |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 9138 | if (ret) |
| 9139 | goto out_fail; |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 9140 | |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 9141 | BTRFS_I(old_inode)->dir_index = 0ULL; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 9142 | if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9143 | /* force full log commit if subvolume involved. */ |
David Sterba | 9078776 | 2019-03-20 13:28:05 +0100 | [diff] [blame] | 9144 | btrfs_set_log_full_commit(trans); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9145 | } else { |
Filipe Manana | c4aba95 | 2016-04-29 13:14:42 +0100 | [diff] [blame] | 9146 | btrfs_pin_log_trans(root); |
| 9147 | log_pinned = true; |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 9148 | ret = btrfs_insert_inode_ref(trans, dest, |
| 9149 | new_dentry->d_name.name, |
| 9150 | new_dentry->d_name.len, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 9151 | old_ino, |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 9152 | btrfs_ino(BTRFS_I(new_dir)), index); |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 9153 | if (ret) |
| 9154 | goto out_fail; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9155 | } |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 9156 | |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 9157 | inode_inc_iversion(old_dir); |
| 9158 | inode_inc_iversion(new_dir); |
| 9159 | inode_inc_iversion(old_inode); |
Deepa Dinamani | 04b285f | 2016-02-06 23:57:21 -0800 | [diff] [blame] | 9160 | old_dir->i_ctime = old_dir->i_mtime = |
| 9161 | new_dir->i_ctime = new_dir->i_mtime = |
Deepa Dinamani | c2050a4 | 2016-09-14 07:48:06 -0700 | [diff] [blame] | 9162 | old_inode->i_ctime = current_time(old_dir); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 9163 | |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 9164 | if (old_dentry->d_parent != new_dentry->d_parent) |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 9165 | btrfs_record_unlink_dir(trans, BTRFS_I(old_dir), |
| 9166 | BTRFS_I(old_inode), 1); |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 9167 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 9168 | if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) { |
Josef Bacik | 045d396 | 2019-12-18 17:20:27 -0500 | [diff] [blame] | 9169 | ret = btrfs_unlink_subvol(trans, old_dir, old_dentry); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9170 | } else { |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 9171 | ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir), |
| 9172 | BTRFS_I(d_inode(old_dentry)), |
Al Viro | 9298679 | 2011-03-04 17:14:37 +0000 | [diff] [blame] | 9173 | old_dentry->d_name.name, |
| 9174 | old_dentry->d_name.len); |
| 9175 | if (!ret) |
| 9176 | ret = btrfs_update_inode(trans, root, old_inode); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9177 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 9178 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 9179 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 9180 | goto out_fail; |
| 9181 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9182 | |
| 9183 | if (new_inode) { |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 9184 | inode_inc_iversion(new_inode); |
Deepa Dinamani | c2050a4 | 2016-09-14 07:48:06 -0700 | [diff] [blame] | 9185 | new_inode->i_ctime = current_time(new_inode); |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 9186 | if (unlikely(btrfs_ino(BTRFS_I(new_inode)) == |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9187 | BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) { |
Josef Bacik | 045d396 | 2019-12-18 17:20:27 -0500 | [diff] [blame] | 9188 | ret = btrfs_unlink_subvol(trans, new_dir, new_dentry); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9189 | BUG_ON(new_inode->i_nlink == 0); |
| 9190 | } else { |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 9191 | ret = btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir), |
| 9192 | BTRFS_I(d_inode(new_dentry)), |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9193 | new_dentry->d_name.name, |
| 9194 | new_dentry->d_name.len); |
| 9195 | } |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 9196 | if (!ret && new_inode->i_nlink == 0) |
Nikolay Borisov | 73f2e54 | 2017-02-20 13:50:59 +0200 | [diff] [blame] | 9197 | ret = btrfs_orphan_add(trans, |
| 9198 | BTRFS_I(d_inode(new_dentry))); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 9199 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 9200 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 9201 | goto out_fail; |
| 9202 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9203 | } |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 9204 | |
Nikolay Borisov | db0a669 | 2017-02-20 13:51:08 +0200 | [diff] [blame] | 9205 | 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] | 9206 | new_dentry->d_name.name, |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 9207 | new_dentry->d_name.len, 0, index); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 9208 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 9209 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 9210 | goto out_fail; |
| 9211 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9212 | |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 9213 | if (old_inode->i_nlink == 1) |
| 9214 | BTRFS_I(old_inode)->dir_index = index; |
| 9215 | |
Filipe Manana | 3dc9e8f7 | 2016-04-29 11:34:22 +0100 | [diff] [blame] | 9216 | if (log_pinned) { |
Filipe Manana | 75b463d | 2020-08-11 12:43:48 +0100 | [diff] [blame] | 9217 | btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir), |
| 9218 | new_dentry->d_parent); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9219 | btrfs_end_log_trans(root); |
Filipe Manana | 3dc9e8f7 | 2016-04-29 11:34:22 +0100 | [diff] [blame] | 9220 | log_pinned = false; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9221 | } |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9222 | |
| 9223 | if (flags & RENAME_WHITEOUT) { |
| 9224 | ret = btrfs_whiteout_for_rename(trans, root, old_dir, |
| 9225 | old_dentry); |
| 9226 | |
| 9227 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 9228 | btrfs_abort_transaction(trans, ret); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9229 | goto out_fail; |
| 9230 | } |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 9231 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9232 | out_fail: |
Filipe Manana | 3dc9e8f7 | 2016-04-29 11:34:22 +0100 | [diff] [blame] | 9233 | /* |
| 9234 | * If we have pinned the log and an error happened, we unpin tasks |
| 9235 | * trying to sync the log and force them to fallback to a transaction |
| 9236 | * commit if the log currently contains any of the inodes involved in |
| 9237 | * this rename operation (to ensure we do not persist a log with an |
| 9238 | * inconsistent state for any of these inodes or leading to any |
| 9239 | * inconsistencies when replayed). If the transaction was aborted, the |
| 9240 | * abortion reason is propagated to userspace when attempting to commit |
| 9241 | * the transaction. If the log does not contain any of these inodes, we |
| 9242 | * allow the tasks to sync it. |
| 9243 | */ |
| 9244 | if (ret && log_pinned) { |
Nikolay Borisov | 0f8939b | 2017-01-18 00:31:30 +0200 | [diff] [blame] | 9245 | if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) || |
| 9246 | btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) || |
| 9247 | btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) || |
Filipe Manana | 3dc9e8f7 | 2016-04-29 11:34:22 +0100 | [diff] [blame] | 9248 | (new_inode && |
Nikolay Borisov | 0f8939b | 2017-01-18 00:31:30 +0200 | [diff] [blame] | 9249 | btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation))) |
David Sterba | 9078776 | 2019-03-20 13:28:05 +0100 | [diff] [blame] | 9250 | btrfs_set_log_full_commit(trans); |
Filipe Manana | 3dc9e8f7 | 2016-04-29 11:34:22 +0100 | [diff] [blame] | 9251 | |
| 9252 | btrfs_end_log_trans(root); |
| 9253 | log_pinned = false; |
| 9254 | } |
Filipe Manana | 75b463d | 2020-08-11 12:43:48 +0100 | [diff] [blame] | 9255 | ret2 = btrfs_end_transaction(trans); |
| 9256 | ret = ret ? ret : ret2; |
Johann Lombardi | b44c59a | 2011-03-31 13:23:47 +0000 | [diff] [blame] | 9257 | out_notrans: |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 9258 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9259 | up_read(&fs_info->subvol_sem); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 9260 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9261 | return ret; |
| 9262 | } |
| 9263 | |
Miklos Szeredi | 80ace85 | 2014-07-23 15:15:32 +0200 | [diff] [blame] | 9264 | static int btrfs_rename2(struct inode *old_dir, struct dentry *old_dentry, |
| 9265 | struct inode *new_dir, struct dentry *new_dentry, |
| 9266 | unsigned int flags) |
| 9267 | { |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9268 | if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT)) |
Miklos Szeredi | 80ace85 | 2014-07-23 15:15:32 +0200 | [diff] [blame] | 9269 | return -EINVAL; |
| 9270 | |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9271 | if (flags & RENAME_EXCHANGE) |
| 9272 | return btrfs_rename_exchange(old_dir, old_dentry, new_dir, |
| 9273 | new_dentry); |
| 9274 | |
| 9275 | return btrfs_rename(old_dir, old_dentry, new_dir, new_dentry, flags); |
Miklos Szeredi | 80ace85 | 2014-07-23 15:15:32 +0200 | [diff] [blame] | 9276 | } |
| 9277 | |
Nikolay Borisov | 3a2f8c0 | 2018-04-24 17:23:59 +0300 | [diff] [blame] | 9278 | struct btrfs_delalloc_work { |
| 9279 | struct inode *inode; |
| 9280 | struct completion completion; |
| 9281 | struct list_head list; |
| 9282 | struct btrfs_work work; |
| 9283 | }; |
| 9284 | |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 9285 | static void btrfs_run_delalloc_work(struct btrfs_work *work) |
| 9286 | { |
| 9287 | struct btrfs_delalloc_work *delalloc_work; |
Josef Bacik | 9f23e28 | 2013-10-28 15:03:41 -0400 | [diff] [blame] | 9288 | struct inode *inode; |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 9289 | |
| 9290 | delalloc_work = container_of(work, struct btrfs_delalloc_work, |
| 9291 | work); |
Josef Bacik | 9f23e28 | 2013-10-28 15:03:41 -0400 | [diff] [blame] | 9292 | inode = delalloc_work->inode; |
David Sterba | 3042460 | 2015-11-27 19:27:11 +0100 | [diff] [blame] | 9293 | filemap_flush(inode->i_mapping); |
| 9294 | if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, |
| 9295 | &BTRFS_I(inode)->runtime_flags)) |
Josef Bacik | 9f23e28 | 2013-10-28 15:03:41 -0400 | [diff] [blame] | 9296 | filemap_flush(inode->i_mapping); |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 9297 | |
Nikolay Borisov | 076da91 | 2018-04-23 10:54:16 +0300 | [diff] [blame] | 9298 | iput(inode); |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 9299 | complete(&delalloc_work->completion); |
| 9300 | } |
| 9301 | |
Nikolay Borisov | 3a2f8c0 | 2018-04-24 17:23:59 +0300 | [diff] [blame] | 9302 | static struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode) |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 9303 | { |
| 9304 | struct btrfs_delalloc_work *work; |
| 9305 | |
David Sterba | 100d570 | 2015-12-08 14:39:32 +0100 | [diff] [blame] | 9306 | work = kmalloc(sizeof(*work), GFP_NOFS); |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 9307 | if (!work) |
| 9308 | return NULL; |
| 9309 | |
| 9310 | init_completion(&work->completion); |
| 9311 | INIT_LIST_HEAD(&work->list); |
| 9312 | work->inode = inode; |
Omar Sandoval | a0cac0e | 2019-09-16 11:30:57 -0700 | [diff] [blame] | 9313 | btrfs_init_work(&work->work, btrfs_run_delalloc_work, NULL, NULL); |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 9314 | |
| 9315 | return work; |
| 9316 | } |
| 9317 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 9318 | /* |
| 9319 | * some fairly slow code that needs optimization. This walks the list |
| 9320 | * of all the inodes with pending delalloc and forces them to disk. |
| 9321 | */ |
Josef Bacik | b491213 | 2020-07-21 10:22:12 -0400 | [diff] [blame] | 9322 | 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] | 9323 | { |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 9324 | struct btrfs_inode *binode; |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 9325 | struct inode *inode; |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 9326 | struct btrfs_delalloc_work *work, *next; |
| 9327 | struct list_head works; |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 9328 | struct list_head splice; |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 9329 | int ret = 0; |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 9330 | |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 9331 | INIT_LIST_HEAD(&works); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 9332 | INIT_LIST_HEAD(&splice); |
Miao Xie | 63607cc | 2013-01-22 10:50:35 +0000 | [diff] [blame] | 9333 | |
Miao Xie | 573bfb7 | 2014-03-06 13:55:03 +0800 | [diff] [blame] | 9334 | mutex_lock(&root->delalloc_mutex); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9335 | spin_lock(&root->delalloc_lock); |
| 9336 | list_splice_init(&root->delalloc_inodes, &splice); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 9337 | while (!list_empty(&splice)) { |
| 9338 | binode = list_entry(splice.next, struct btrfs_inode, |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 9339 | delalloc_inodes); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 9340 | |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9341 | list_move_tail(&binode->delalloc_inodes, |
| 9342 | &root->delalloc_inodes); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 9343 | inode = igrab(&binode->vfs_inode); |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 9344 | if (!inode) { |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9345 | cond_resched_lock(&root->delalloc_lock); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 9346 | continue; |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 9347 | } |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9348 | spin_unlock(&root->delalloc_lock); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 9349 | |
Ethan Lien | 3cd24c6 | 2018-11-01 14:49:03 +0800 | [diff] [blame] | 9350 | if (snapshot) |
| 9351 | set_bit(BTRFS_INODE_SNAPSHOT_FLUSH, |
| 9352 | &binode->runtime_flags); |
Nikolay Borisov | 076da91 | 2018-04-23 10:54:16 +0300 | [diff] [blame] | 9353 | work = btrfs_alloc_delalloc_work(inode); |
David Sterba | 5d99a998 | 2014-09-29 19:20:37 +0200 | [diff] [blame] | 9354 | if (!work) { |
Nikolay Borisov | 4fbb514 | 2018-04-23 10:54:15 +0300 | [diff] [blame] | 9355 | iput(inode); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 9356 | ret = -ENOMEM; |
Wang Shilong | a1ecaab | 2014-04-02 19:53:32 +0800 | [diff] [blame] | 9357 | goto out; |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 9358 | } |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 9359 | list_add_tail(&work->list, &works); |
Qu Wenruo | a44903a | 2014-02-28 10:46:09 +0800 | [diff] [blame] | 9360 | btrfs_queue_work(root->fs_info->flush_workers, |
| 9361 | &work->work); |
Josef Bacik | b491213 | 2020-07-21 10:22:12 -0400 | [diff] [blame] | 9362 | if (*nr != U64_MAX) { |
| 9363 | (*nr)--; |
| 9364 | if (*nr == 0) |
| 9365 | goto out; |
| 9366 | } |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 9367 | cond_resched(); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9368 | spin_lock(&root->delalloc_lock); |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 9369 | } |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9370 | spin_unlock(&root->delalloc_lock); |
Chris Mason | 8c8bee1 | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 9371 | |
Wang Shilong | a1ecaab | 2014-04-02 19:53:32 +0800 | [diff] [blame] | 9372 | out: |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 9373 | list_for_each_entry_safe(work, next, &works, list) { |
| 9374 | list_del_init(&work->list); |
Nikolay Borisov | 40012f9 | 2018-04-19 10:46:39 +0300 | [diff] [blame] | 9375 | wait_for_completion(&work->completion); |
| 9376 | kfree(work); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 9377 | } |
| 9378 | |
Nikolay Borisov | 81f1d39 | 2018-04-19 10:46:37 +0300 | [diff] [blame] | 9379 | if (!list_empty(&splice)) { |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9380 | spin_lock(&root->delalloc_lock); |
| 9381 | list_splice_tail(&splice, &root->delalloc_inodes); |
| 9382 | spin_unlock(&root->delalloc_lock); |
| 9383 | } |
Miao Xie | 573bfb7 | 2014-03-06 13:55:03 +0800 | [diff] [blame] | 9384 | mutex_unlock(&root->delalloc_mutex); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9385 | return ret; |
| 9386 | } |
| 9387 | |
Ethan Lien | 3cd24c6 | 2018-11-01 14:49:03 +0800 | [diff] [blame] | 9388 | int btrfs_start_delalloc_snapshot(struct btrfs_root *root) |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9389 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9390 | struct btrfs_fs_info *fs_info = root->fs_info; |
Josef Bacik | b491213 | 2020-07-21 10:22:12 -0400 | [diff] [blame] | 9391 | u64 nr = U64_MAX; |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9392 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9393 | if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9394 | return -EROFS; |
| 9395 | |
Josef Bacik | b491213 | 2020-07-21 10:22:12 -0400 | [diff] [blame] | 9396 | return start_delalloc_inodes(root, &nr, true); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9397 | } |
| 9398 | |
Josef Bacik | b491213 | 2020-07-21 10:22:12 -0400 | [diff] [blame] | 9399 | 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] | 9400 | { |
| 9401 | struct btrfs_root *root; |
| 9402 | struct list_head splice; |
| 9403 | int ret; |
| 9404 | |
Wang Shilong | 2c21b4d | 2014-01-14 19:42:20 +0800 | [diff] [blame] | 9405 | if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9406 | return -EROFS; |
| 9407 | |
| 9408 | INIT_LIST_HEAD(&splice); |
| 9409 | |
Miao Xie | 573bfb7 | 2014-03-06 13:55:03 +0800 | [diff] [blame] | 9410 | mutex_lock(&fs_info->delalloc_root_mutex); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9411 | spin_lock(&fs_info->delalloc_root_lock); |
| 9412 | list_splice_init(&fs_info->delalloc_roots, &splice); |
Miao Xie | 6c255e6 | 2014-03-06 13:55:01 +0800 | [diff] [blame] | 9413 | while (!list_empty(&splice) && nr) { |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9414 | root = list_first_entry(&splice, struct btrfs_root, |
| 9415 | delalloc_root); |
Josef Bacik | 0024652 | 2020-01-24 09:33:01 -0500 | [diff] [blame] | 9416 | root = btrfs_grab_root(root); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9417 | BUG_ON(!root); |
| 9418 | list_move_tail(&root->delalloc_root, |
| 9419 | &fs_info->delalloc_roots); |
| 9420 | spin_unlock(&fs_info->delalloc_root_lock); |
| 9421 | |
Josef Bacik | b491213 | 2020-07-21 10:22:12 -0400 | [diff] [blame] | 9422 | ret = start_delalloc_inodes(root, &nr, false); |
Josef Bacik | 0024652 | 2020-01-24 09:33:01 -0500 | [diff] [blame] | 9423 | btrfs_put_root(root); |
Miao Xie | 6c255e6 | 2014-03-06 13:55:01 +0800 | [diff] [blame] | 9424 | if (ret < 0) |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9425 | goto out; |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9426 | spin_lock(&fs_info->delalloc_root_lock); |
| 9427 | } |
| 9428 | spin_unlock(&fs_info->delalloc_root_lock); |
| 9429 | |
Miao Xie | 6c255e6 | 2014-03-06 13:55:01 +0800 | [diff] [blame] | 9430 | ret = 0; |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 9431 | out: |
Nikolay Borisov | 81f1d39 | 2018-04-19 10:46:37 +0300 | [diff] [blame] | 9432 | if (!list_empty(&splice)) { |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9433 | spin_lock(&fs_info->delalloc_root_lock); |
| 9434 | list_splice_tail(&splice, &fs_info->delalloc_roots); |
| 9435 | spin_unlock(&fs_info->delalloc_root_lock); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 9436 | } |
Miao Xie | 573bfb7 | 2014-03-06 13:55:03 +0800 | [diff] [blame] | 9437 | mutex_unlock(&fs_info->delalloc_root_mutex); |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 9438 | return ret; |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 9439 | } |
| 9440 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9441 | static int btrfs_symlink(struct inode *dir, struct dentry *dentry, |
| 9442 | const char *symname) |
| 9443 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9444 | struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9445 | struct btrfs_trans_handle *trans; |
| 9446 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 9447 | struct btrfs_path *path; |
| 9448 | struct btrfs_key key; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 9449 | struct inode *inode = NULL; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9450 | int err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9451 | u64 objectid; |
Dulshani Gunawardhana | 6787125 | 2013-10-31 10:33:04 +0530 | [diff] [blame] | 9452 | u64 index = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9453 | int name_len; |
| 9454 | int datasize; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 9455 | unsigned long ptr; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9456 | struct btrfs_file_extent_item *ei; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 9457 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9458 | |
Filipe David Borba Manana | f06becc | 2013-09-16 09:53:28 +0100 | [diff] [blame] | 9459 | name_len = strlen(symname); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9460 | if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info)) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9461 | return -ENAMETOOLONG; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 9462 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 9463 | /* |
| 9464 | * 2 items for inode item and ref |
| 9465 | * 2 items for dir items |
Filipe Manana | 9269d12 | 2015-12-31 18:16:29 +0000 | [diff] [blame] | 9466 | * 1 item for updating parent inode item |
| 9467 | * 1 item for the inline extent item |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 9468 | * 1 item for xattr if selinux is on |
| 9469 | */ |
Filipe Manana | 9269d12 | 2015-12-31 18:16:29 +0000 | [diff] [blame] | 9470 | trans = btrfs_start_transaction(root, 7); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 9471 | if (IS_ERR(trans)) |
| 9472 | return PTR_ERR(trans); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 9473 | |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 9474 | err = btrfs_find_free_ino(root, &objectid); |
| 9475 | if (err) |
| 9476 | goto out_unlock; |
| 9477 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 9478 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 9479 | dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), |
| 9480 | objectid, S_IFLNK|S_IRWXUGO, &index); |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 9481 | if (IS_ERR(inode)) { |
| 9482 | err = PTR_ERR(inode); |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 9483 | inode = NULL; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9484 | goto out_unlock; |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 9485 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9486 | |
Casey Schaufler | ad19db7 | 2011-12-15 10:09:07 -0500 | [diff] [blame] | 9487 | /* |
| 9488 | * If the active LSM wants to access the inode during |
| 9489 | * d_instantiate it needs these. Smack checks to see |
| 9490 | * if the filesystem supports xattrs by looking at the |
| 9491 | * ops vector. |
| 9492 | */ |
| 9493 | inode->i_fop = &btrfs_file_operations; |
| 9494 | inode->i_op = &btrfs_file_inode_operations; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 9495 | inode->i_mapping->a_ops = &btrfs_aops; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 9496 | |
| 9497 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); |
| 9498 | if (err) |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 9499 | goto out_unlock; |
Casey Schaufler | ad19db7 | 2011-12-15 10:09:07 -0500 | [diff] [blame] | 9500 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9501 | path = btrfs_alloc_path(); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 9502 | if (!path) { |
| 9503 | err = -ENOMEM; |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 9504 | goto out_unlock; |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 9505 | } |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 9506 | key.objectid = btrfs_ino(BTRFS_I(inode)); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9507 | key.offset = 0; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 9508 | key.type = BTRFS_EXTENT_DATA_KEY; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9509 | datasize = btrfs_file_extent_calc_inline_size(name_len); |
| 9510 | err = btrfs_insert_empty_item(trans, root, path, &key, |
| 9511 | datasize); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 9512 | if (err) { |
Julia Lawall | b083916 | 2011-05-14 07:10:51 +0000 | [diff] [blame] | 9513 | btrfs_free_path(path); |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 9514 | goto out_unlock; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 9515 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 9516 | leaf = path->nodes[0]; |
| 9517 | ei = btrfs_item_ptr(leaf, path->slots[0], |
| 9518 | struct btrfs_file_extent_item); |
| 9519 | btrfs_set_file_extent_generation(leaf, ei, trans->transid); |
| 9520 | btrfs_set_file_extent_type(leaf, ei, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9521 | BTRFS_FILE_EXTENT_INLINE); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 9522 | btrfs_set_file_extent_encryption(leaf, ei, 0); |
| 9523 | btrfs_set_file_extent_compression(leaf, ei, 0); |
| 9524 | btrfs_set_file_extent_other_encoding(leaf, ei, 0); |
| 9525 | btrfs_set_file_extent_ram_bytes(leaf, ei, name_len); |
| 9526 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9527 | ptr = btrfs_file_extent_inline_start(ei); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 9528 | write_extent_buffer(leaf, symname, ptr, name_len); |
| 9529 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9530 | btrfs_free_path(path); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 9531 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9532 | inode->i_op = &btrfs_symlink_inode_operations; |
Al Viro | 21fc61c | 2015-11-17 01:07:57 -0500 | [diff] [blame] | 9533 | inode_nohighmem(inode); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 9534 | inode_set_bytes(inode, name_len); |
Nikolay Borisov | 6ef06d2 | 2017-02-20 13:50:34 +0200 | [diff] [blame] | 9535 | btrfs_i_size_write(BTRFS_I(inode), name_len); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 9536 | err = btrfs_update_inode(trans, root, inode); |
Filipe Manana | d50866d | 2015-12-31 18:08:24 +0000 | [diff] [blame] | 9537 | /* |
| 9538 | * Last step, add directory indexes for our symlink inode. This is the |
| 9539 | * last step to avoid extra cleanup of these indexes if an error happens |
| 9540 | * elsewhere above. |
| 9541 | */ |
| 9542 | if (!err) |
Nikolay Borisov | cef415a | 2017-02-20 13:51:09 +0200 | [diff] [blame] | 9543 | err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, |
| 9544 | BTRFS_I(inode), 0, index); |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 9545 | if (err) |
| 9546 | goto out_unlock; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 9547 | |
Al Viro | 1e2e547 | 2018-05-04 08:23:01 -0400 | [diff] [blame] | 9548 | d_instantiate_new(dentry, inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9549 | |
| 9550 | out_unlock: |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 9551 | btrfs_end_transaction(trans); |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 9552 | if (err && inode) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9553 | inode_dec_link_count(inode); |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 9554 | discard_new_inode(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9555 | } |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 9556 | btrfs_btree_balance_dirty(fs_info); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9557 | return err; |
| 9558 | } |
Chris Mason | 1643298 | 2008-04-10 10:23:21 -0400 | [diff] [blame] | 9559 | |
Filipe Manana | 8fccebf | 2020-09-08 11:27:20 +0100 | [diff] [blame] | 9560 | static struct btrfs_trans_handle *insert_prealloc_file_extent( |
| 9561 | struct btrfs_trans_handle *trans_in, |
Qu Wenruo | 203f44c5 | 2020-06-10 09:04:40 +0800 | [diff] [blame] | 9562 | struct inode *inode, struct btrfs_key *ins, |
| 9563 | u64 file_offset) |
| 9564 | { |
| 9565 | struct btrfs_file_extent_item stack_fi; |
Filipe Manana | bf38564 | 2020-09-08 11:27:22 +0100 | [diff] [blame] | 9566 | struct btrfs_replace_extent_info extent_info; |
Filipe Manana | 8fccebf | 2020-09-08 11:27:20 +0100 | [diff] [blame] | 9567 | struct btrfs_trans_handle *trans = trans_in; |
| 9568 | struct btrfs_path *path; |
Qu Wenruo | 203f44c5 | 2020-06-10 09:04:40 +0800 | [diff] [blame] | 9569 | u64 start = ins->objectid; |
| 9570 | u64 len = ins->offset; |
Qu Wenruo | 9729f10 | 2020-06-10 09:04:41 +0800 | [diff] [blame] | 9571 | int ret; |
Qu Wenruo | 203f44c5 | 2020-06-10 09:04:40 +0800 | [diff] [blame] | 9572 | |
| 9573 | memset(&stack_fi, 0, sizeof(stack_fi)); |
| 9574 | |
| 9575 | btrfs_set_stack_file_extent_type(&stack_fi, BTRFS_FILE_EXTENT_PREALLOC); |
| 9576 | btrfs_set_stack_file_extent_disk_bytenr(&stack_fi, start); |
| 9577 | btrfs_set_stack_file_extent_disk_num_bytes(&stack_fi, len); |
| 9578 | btrfs_set_stack_file_extent_num_bytes(&stack_fi, len); |
| 9579 | btrfs_set_stack_file_extent_ram_bytes(&stack_fi, len); |
| 9580 | btrfs_set_stack_file_extent_compression(&stack_fi, BTRFS_COMPRESS_NONE); |
| 9581 | /* Encryption and other encoding is reserved and all 0 */ |
| 9582 | |
Nikolay Borisov | 72b7d15 | 2020-06-03 08:55:18 +0300 | [diff] [blame] | 9583 | ret = btrfs_qgroup_release_data(BTRFS_I(inode), file_offset, len); |
Qu Wenruo | 9729f10 | 2020-06-10 09:04:41 +0800 | [diff] [blame] | 9584 | if (ret < 0) |
Filipe Manana | 8fccebf | 2020-09-08 11:27:20 +0100 | [diff] [blame] | 9585 | return ERR_PTR(ret); |
| 9586 | |
| 9587 | if (trans) { |
| 9588 | ret = insert_reserved_file_extent(trans, BTRFS_I(inode), |
| 9589 | file_offset, &stack_fi, ret); |
| 9590 | if (ret) |
| 9591 | return ERR_PTR(ret); |
| 9592 | return trans; |
| 9593 | } |
| 9594 | |
| 9595 | extent_info.disk_offset = start; |
| 9596 | extent_info.disk_len = len; |
| 9597 | extent_info.data_offset = 0; |
| 9598 | extent_info.data_len = len; |
| 9599 | extent_info.file_offset = file_offset; |
| 9600 | extent_info.extent_buf = (char *)&stack_fi; |
Filipe Manana | 8fccebf | 2020-09-08 11:27:20 +0100 | [diff] [blame] | 9601 | extent_info.is_new_extent = true; |
| 9602 | extent_info.qgroup_reserved = ret; |
| 9603 | extent_info.insertions = 0; |
| 9604 | |
| 9605 | path = btrfs_alloc_path(); |
| 9606 | if (!path) |
| 9607 | return ERR_PTR(-ENOMEM); |
| 9608 | |
Filipe Manana | 306bfec | 2020-09-08 11:27:23 +0100 | [diff] [blame] | 9609 | ret = btrfs_replace_file_extents(inode, path, file_offset, |
Filipe Manana | 8fccebf | 2020-09-08 11:27:20 +0100 | [diff] [blame] | 9610 | file_offset + len - 1, &extent_info, |
| 9611 | &trans); |
| 9612 | btrfs_free_path(path); |
| 9613 | if (ret) |
| 9614 | return ERR_PTR(ret); |
| 9615 | |
| 9616 | return trans; |
Qu Wenruo | 203f44c5 | 2020-06-10 09:04:40 +0800 | [diff] [blame] | 9617 | } |
Filipe Manana | 8fccebf | 2020-09-08 11:27:20 +0100 | [diff] [blame] | 9618 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 9619 | static int __btrfs_prealloc_file_range(struct inode *inode, int mode, |
| 9620 | u64 start, u64 num_bytes, u64 min_size, |
| 9621 | loff_t actual_len, u64 *alloc_hint, |
| 9622 | struct btrfs_trans_handle *trans) |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 9623 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9624 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 9625 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
| 9626 | struct extent_map *em; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 9627 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 9628 | struct btrfs_key ins; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 9629 | u64 cur_offset = start; |
Josef Bacik | b778cf9 | 2020-02-13 10:47:31 -0500 | [diff] [blame] | 9630 | u64 clear_offset = start; |
Josef Bacik | 55a61d1 | 2010-11-22 18:50:32 +0000 | [diff] [blame] | 9631 | u64 i_size; |
Chris Mason | 154ea28 | 2013-03-05 11:11:26 -0500 | [diff] [blame] | 9632 | u64 cur_bytes; |
Josef Bacik | 0b670dc | 2015-09-23 17:11:16 -0400 | [diff] [blame] | 9633 | u64 last_alloc = (u64)-1; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 9634 | int ret = 0; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 9635 | bool own_trans = true; |
Wang Xiaoguang | 1851309 | 2016-07-25 15:51:40 +0800 | [diff] [blame] | 9636 | u64 end = start + num_bytes - 1; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 9637 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 9638 | if (trans) |
| 9639 | own_trans = false; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 9640 | while (num_bytes > 0) { |
Byongho Lee | ee22184 | 2015-12-15 01:42:10 +0900 | [diff] [blame] | 9641 | cur_bytes = min_t(u64, num_bytes, SZ_256M); |
Chris Mason | 154ea28 | 2013-03-05 11:11:26 -0500 | [diff] [blame] | 9642 | cur_bytes = max(cur_bytes, min_size); |
Josef Bacik | 0b670dc | 2015-09-23 17:11:16 -0400 | [diff] [blame] | 9643 | /* |
| 9644 | * If we are severely fragmented we could end up with really |
| 9645 | * small allocations, so if the allocator is returning small |
| 9646 | * chunks lets make its job easier by only searching for those |
| 9647 | * sized chunks. |
| 9648 | */ |
| 9649 | cur_bytes = min(cur_bytes, last_alloc); |
Wang Xiaoguang | 1851309 | 2016-07-25 15:51:40 +0800 | [diff] [blame] | 9650 | ret = btrfs_reserve_extent(root, cur_bytes, cur_bytes, |
| 9651 | min_size, 0, *alloc_hint, &ins, 1, 0); |
Filipe Manana | 8fccebf | 2020-09-08 11:27:20 +0100 | [diff] [blame] | 9652 | if (ret) |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 9653 | break; |
Josef Bacik | b778cf9 | 2020-02-13 10:47:31 -0500 | [diff] [blame] | 9654 | |
| 9655 | /* |
| 9656 | * We've reserved this space, and thus converted it from |
| 9657 | * ->bytes_may_use to ->bytes_reserved. Any error that happens |
| 9658 | * from here on out we will only need to clear our reservation |
| 9659 | * for the remaining unreserved area, so advance our |
| 9660 | * clear_offset by our extent size. |
| 9661 | */ |
| 9662 | clear_offset += ins.offset; |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 9663 | |
Josef Bacik | 0b670dc | 2015-09-23 17:11:16 -0400 | [diff] [blame] | 9664 | last_alloc = ins.offset; |
Filipe Manana | 8fccebf | 2020-09-08 11:27:20 +0100 | [diff] [blame] | 9665 | trans = insert_prealloc_file_extent(trans, inode, &ins, cur_offset); |
Filipe Manana | 1afc708 | 2020-10-14 10:10:36 +0100 | [diff] [blame] | 9666 | /* |
| 9667 | * Now that we inserted the prealloc extent we can finally |
| 9668 | * decrement the number of reservations in the block group. |
| 9669 | * If we did it before, we could race with relocation and have |
| 9670 | * relocation miss the reserved extent, making it fail later. |
| 9671 | */ |
| 9672 | btrfs_dec_block_group_reservations(fs_info, ins.objectid); |
Filipe Manana | 8fccebf | 2020-09-08 11:27:20 +0100 | [diff] [blame] | 9673 | if (IS_ERR(trans)) { |
| 9674 | ret = PTR_ERR(trans); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 9675 | btrfs_free_reserved_extent(fs_info, ins.objectid, |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 9676 | ins.offset, 0); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 9677 | break; |
| 9678 | } |
Dongsheng Yang | 3119321 | 2014-12-12 16:44:35 +0800 | [diff] [blame] | 9679 | |
Nikolay Borisov | dcdbc05 | 2017-02-20 13:50:45 +0200 | [diff] [blame] | 9680 | btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset, |
Chris Mason | a1ed835 | 2009-09-11 12:27:37 -0400 | [diff] [blame] | 9681 | cur_offset + ins.offset -1, 0); |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 9682 | |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 9683 | em = alloc_extent_map(); |
| 9684 | if (!em) { |
| 9685 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, |
| 9686 | &BTRFS_I(inode)->runtime_flags); |
| 9687 | goto next; |
| 9688 | } |
| 9689 | |
| 9690 | em->start = cur_offset; |
| 9691 | em->orig_start = cur_offset; |
| 9692 | em->len = ins.offset; |
| 9693 | em->block_start = ins.objectid; |
| 9694 | em->block_len = ins.offset; |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 9695 | em->orig_block_len = ins.offset; |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 9696 | em->ram_bytes = ins.offset; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 9697 | set_bit(EXTENT_FLAG_PREALLOC, &em->flags); |
| 9698 | em->generation = trans->transid; |
| 9699 | |
| 9700 | while (1) { |
| 9701 | write_lock(&em_tree->lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 9702 | ret = add_extent_mapping(em_tree, em, 1); |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 9703 | write_unlock(&em_tree->lock); |
| 9704 | if (ret != -EEXIST) |
| 9705 | break; |
Nikolay Borisov | dcdbc05 | 2017-02-20 13:50:45 +0200 | [diff] [blame] | 9706 | btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset, |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 9707 | cur_offset + ins.offset - 1, |
| 9708 | 0); |
| 9709 | } |
| 9710 | free_extent_map(em); |
| 9711 | next: |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 9712 | num_bytes -= ins.offset; |
| 9713 | cur_offset += ins.offset; |
Yan, Zheng | efa5646 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 9714 | *alloc_hint = ins.objectid + ins.offset; |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 9715 | |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 9716 | inode_inc_iversion(inode); |
Deepa Dinamani | c2050a4 | 2016-09-14 07:48:06 -0700 | [diff] [blame] | 9717 | inode->i_ctime = current_time(inode); |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 9718 | BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 9719 | if (!(mode & FALLOC_FL_KEEP_SIZE) && |
Yan, Zheng | efa5646 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 9720 | (actual_len > inode->i_size) && |
| 9721 | (cur_offset > inode->i_size)) { |
Aneesh Kumar K.V | d1ea6a6 | 2010-01-20 07:28:54 +0000 | [diff] [blame] | 9722 | if (cur_offset > actual_len) |
Josef Bacik | 55a61d1 | 2010-11-22 18:50:32 +0000 | [diff] [blame] | 9723 | i_size = actual_len; |
Aneesh Kumar K.V | d1ea6a6 | 2010-01-20 07:28:54 +0000 | [diff] [blame] | 9724 | else |
Josef Bacik | 55a61d1 | 2010-11-22 18:50:32 +0000 | [diff] [blame] | 9725 | i_size = cur_offset; |
| 9726 | i_size_write(inode, i_size); |
Josef Bacik | d923afe | 2020-01-17 09:02:23 -0500 | [diff] [blame] | 9727 | btrfs_inode_safe_disk_i_size_write(inode, 0); |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 9728 | } |
| 9729 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 9730 | ret = btrfs_update_inode(trans, root, inode); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 9731 | |
| 9732 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 9733 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 9734 | if (own_trans) |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 9735 | btrfs_end_transaction(trans); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 9736 | break; |
| 9737 | } |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 9738 | |
Filipe Manana | 8fccebf | 2020-09-08 11:27:20 +0100 | [diff] [blame] | 9739 | if (own_trans) { |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 9740 | btrfs_end_transaction(trans); |
Filipe Manana | 8fccebf | 2020-09-08 11:27:20 +0100 | [diff] [blame] | 9741 | trans = NULL; |
| 9742 | } |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 9743 | } |
Josef Bacik | b778cf9 | 2020-02-13 10:47:31 -0500 | [diff] [blame] | 9744 | if (clear_offset < end) |
Nikolay Borisov | 25ce28c | 2020-06-03 08:55:39 +0300 | [diff] [blame] | 9745 | btrfs_free_reserved_data_space(BTRFS_I(inode), NULL, clear_offset, |
Josef Bacik | b778cf9 | 2020-02-13 10:47:31 -0500 | [diff] [blame] | 9746 | end - clear_offset + 1); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 9747 | return ret; |
| 9748 | } |
| 9749 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 9750 | int btrfs_prealloc_file_range(struct inode *inode, int mode, |
| 9751 | u64 start, u64 num_bytes, u64 min_size, |
| 9752 | loff_t actual_len, u64 *alloc_hint) |
| 9753 | { |
| 9754 | return __btrfs_prealloc_file_range(inode, mode, start, num_bytes, |
| 9755 | min_size, actual_len, alloc_hint, |
| 9756 | NULL); |
| 9757 | } |
| 9758 | |
| 9759 | int btrfs_prealloc_file_range_trans(struct inode *inode, |
| 9760 | struct btrfs_trans_handle *trans, int mode, |
| 9761 | u64 start, u64 num_bytes, u64 min_size, |
| 9762 | loff_t actual_len, u64 *alloc_hint) |
| 9763 | { |
| 9764 | return __btrfs_prealloc_file_range(inode, mode, start, num_bytes, |
| 9765 | min_size, actual_len, alloc_hint, trans); |
| 9766 | } |
| 9767 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 9768 | static int btrfs_set_page_dirty(struct page *page) |
| 9769 | { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 9770 | return __set_page_dirty_nobuffers(page); |
| 9771 | } |
| 9772 | |
Al Viro | 10556cb2 | 2011-06-20 19:28:19 -0400 | [diff] [blame] | 9773 | static int btrfs_permission(struct inode *inode, int mask) |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 9774 | { |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 9775 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Jeff Mahoney | cb6db4e | 2011-08-15 17:27:21 +0000 | [diff] [blame] | 9776 | umode_t mode = inode->i_mode; |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 9777 | |
Jeff Mahoney | cb6db4e | 2011-08-15 17:27:21 +0000 | [diff] [blame] | 9778 | if (mask & MAY_WRITE && |
| 9779 | (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) { |
| 9780 | if (btrfs_root_readonly(root)) |
| 9781 | return -EROFS; |
| 9782 | if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY) |
| 9783 | return -EACCES; |
| 9784 | } |
Al Viro | 2830ba7 | 2011-06-20 19:16:29 -0400 | [diff] [blame] | 9785 | return generic_permission(inode, mask); |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 9786 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9787 | |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 9788 | static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode) |
| 9789 | { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 9790 | struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 9791 | struct btrfs_trans_handle *trans; |
| 9792 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 9793 | struct inode *inode = NULL; |
| 9794 | u64 objectid; |
| 9795 | u64 index; |
| 9796 | int ret = 0; |
| 9797 | |
| 9798 | /* |
| 9799 | * 5 units required for adding orphan entry |
| 9800 | */ |
| 9801 | trans = btrfs_start_transaction(root, 5); |
| 9802 | if (IS_ERR(trans)) |
| 9803 | return PTR_ERR(trans); |
| 9804 | |
| 9805 | ret = btrfs_find_free_ino(root, &objectid); |
| 9806 | if (ret) |
| 9807 | goto out; |
| 9808 | |
| 9809 | inode = btrfs_new_inode(trans, root, dir, NULL, 0, |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 9810 | btrfs_ino(BTRFS_I(dir)), objectid, mode, &index); |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 9811 | if (IS_ERR(inode)) { |
| 9812 | ret = PTR_ERR(inode); |
| 9813 | inode = NULL; |
| 9814 | goto out; |
| 9815 | } |
| 9816 | |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 9817 | inode->i_fop = &btrfs_file_operations; |
| 9818 | inode->i_op = &btrfs_file_inode_operations; |
| 9819 | |
| 9820 | inode->i_mapping->a_ops = &btrfs_aops; |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 9821 | |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 9822 | ret = btrfs_init_inode_security(trans, inode, dir, NULL); |
| 9823 | if (ret) |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 9824 | goto out; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 9825 | |
| 9826 | ret = btrfs_update_inode(trans, root, inode); |
| 9827 | if (ret) |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 9828 | goto out; |
Nikolay Borisov | 73f2e54 | 2017-02-20 13:50:59 +0200 | [diff] [blame] | 9829 | ret = btrfs_orphan_add(trans, BTRFS_I(inode)); |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 9830 | if (ret) |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 9831 | goto out; |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 9832 | |
Filipe Manana | 5762b5c | 2014-08-01 00:10:32 +0100 | [diff] [blame] | 9833 | /* |
| 9834 | * We set number of links to 0 in btrfs_new_inode(), and here we set |
| 9835 | * it to 1 because d_tmpfile() will issue a warning if the count is 0, |
| 9836 | * through: |
| 9837 | * |
| 9838 | * d_tmpfile() -> inode_dec_link_count() -> drop_nlink() |
| 9839 | */ |
| 9840 | set_nlink(inode, 1); |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 9841 | d_tmpfile(dentry, inode); |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 9842 | unlock_new_inode(inode); |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 9843 | mark_inode_dirty(inode); |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 9844 | out: |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 9845 | btrfs_end_transaction(trans); |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 9846 | if (ret && inode) |
| 9847 | discard_new_inode(inode); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 9848 | btrfs_btree_balance_dirty(fs_info); |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 9849 | return ret; |
| 9850 | } |
| 9851 | |
David Sterba | 5cdc84b | 2018-07-18 20:32:52 +0200 | [diff] [blame] | 9852 | 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] | 9853 | { |
David Sterba | 5cdc84b | 2018-07-18 20:32:52 +0200 | [diff] [blame] | 9854 | struct inode *inode = tree->private_data; |
Josef Bacik | c6100a4 | 2017-05-05 11:57:13 -0400 | [diff] [blame] | 9855 | unsigned long index = start >> PAGE_SHIFT; |
| 9856 | unsigned long end_index = end >> PAGE_SHIFT; |
| 9857 | struct page *page; |
| 9858 | |
| 9859 | while (index <= end_index) { |
| 9860 | page = find_get_page(inode->i_mapping, index); |
| 9861 | ASSERT(page); /* Pages should be in the extent_io_tree */ |
| 9862 | set_page_writeback(page); |
| 9863 | put_page(page); |
| 9864 | index++; |
| 9865 | } |
| 9866 | } |
| 9867 | |
Omar Sandoval | ed46ff3 | 2016-11-03 10:28:14 -0700 | [diff] [blame] | 9868 | #ifdef CONFIG_SWAP |
| 9869 | /* |
| 9870 | * Add an entry indicating a block group or device which is pinned by a |
| 9871 | * swapfile. Returns 0 on success, 1 if there is already an entry for it, or a |
| 9872 | * negative errno on failure. |
| 9873 | */ |
| 9874 | static int btrfs_add_swapfile_pin(struct inode *inode, void *ptr, |
| 9875 | bool is_block_group) |
| 9876 | { |
| 9877 | struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info; |
| 9878 | struct btrfs_swapfile_pin *sp, *entry; |
| 9879 | struct rb_node **p; |
| 9880 | struct rb_node *parent = NULL; |
| 9881 | |
| 9882 | sp = kmalloc(sizeof(*sp), GFP_NOFS); |
| 9883 | if (!sp) |
| 9884 | return -ENOMEM; |
| 9885 | sp->ptr = ptr; |
| 9886 | sp->inode = inode; |
| 9887 | sp->is_block_group = is_block_group; |
| 9888 | |
| 9889 | spin_lock(&fs_info->swapfile_pins_lock); |
| 9890 | p = &fs_info->swapfile_pins.rb_node; |
| 9891 | while (*p) { |
| 9892 | parent = *p; |
| 9893 | entry = rb_entry(parent, struct btrfs_swapfile_pin, node); |
| 9894 | if (sp->ptr < entry->ptr || |
| 9895 | (sp->ptr == entry->ptr && sp->inode < entry->inode)) { |
| 9896 | p = &(*p)->rb_left; |
| 9897 | } else if (sp->ptr > entry->ptr || |
| 9898 | (sp->ptr == entry->ptr && sp->inode > entry->inode)) { |
| 9899 | p = &(*p)->rb_right; |
| 9900 | } else { |
| 9901 | spin_unlock(&fs_info->swapfile_pins_lock); |
| 9902 | kfree(sp); |
| 9903 | return 1; |
| 9904 | } |
| 9905 | } |
| 9906 | rb_link_node(&sp->node, parent, p); |
| 9907 | rb_insert_color(&sp->node, &fs_info->swapfile_pins); |
| 9908 | spin_unlock(&fs_info->swapfile_pins_lock); |
| 9909 | return 0; |
| 9910 | } |
| 9911 | |
| 9912 | /* Free all of the entries pinned by this swapfile. */ |
| 9913 | static void btrfs_free_swapfile_pins(struct inode *inode) |
| 9914 | { |
| 9915 | struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info; |
| 9916 | struct btrfs_swapfile_pin *sp; |
| 9917 | struct rb_node *node, *next; |
| 9918 | |
| 9919 | spin_lock(&fs_info->swapfile_pins_lock); |
| 9920 | node = rb_first(&fs_info->swapfile_pins); |
| 9921 | while (node) { |
| 9922 | next = rb_next(node); |
| 9923 | sp = rb_entry(node, struct btrfs_swapfile_pin, node); |
| 9924 | if (sp->inode == inode) { |
| 9925 | rb_erase(&sp->node, &fs_info->swapfile_pins); |
| 9926 | if (sp->is_block_group) |
| 9927 | btrfs_put_block_group(sp->ptr); |
| 9928 | kfree(sp); |
| 9929 | } |
| 9930 | node = next; |
| 9931 | } |
| 9932 | spin_unlock(&fs_info->swapfile_pins_lock); |
| 9933 | } |
| 9934 | |
| 9935 | struct btrfs_swap_info { |
| 9936 | u64 start; |
| 9937 | u64 block_start; |
| 9938 | u64 block_len; |
| 9939 | u64 lowest_ppage; |
| 9940 | u64 highest_ppage; |
| 9941 | unsigned long nr_pages; |
| 9942 | int nr_extents; |
| 9943 | }; |
| 9944 | |
| 9945 | static int btrfs_add_swap_extent(struct swap_info_struct *sis, |
| 9946 | struct btrfs_swap_info *bsi) |
| 9947 | { |
| 9948 | unsigned long nr_pages; |
| 9949 | u64 first_ppage, first_ppage_reported, next_ppage; |
| 9950 | int ret; |
| 9951 | |
| 9952 | first_ppage = ALIGN(bsi->block_start, PAGE_SIZE) >> PAGE_SHIFT; |
| 9953 | next_ppage = ALIGN_DOWN(bsi->block_start + bsi->block_len, |
| 9954 | PAGE_SIZE) >> PAGE_SHIFT; |
| 9955 | |
| 9956 | if (first_ppage >= next_ppage) |
| 9957 | return 0; |
| 9958 | nr_pages = next_ppage - first_ppage; |
| 9959 | |
| 9960 | first_ppage_reported = first_ppage; |
| 9961 | if (bsi->start == 0) |
| 9962 | first_ppage_reported++; |
| 9963 | if (bsi->lowest_ppage > first_ppage_reported) |
| 9964 | bsi->lowest_ppage = first_ppage_reported; |
| 9965 | if (bsi->highest_ppage < (next_ppage - 1)) |
| 9966 | bsi->highest_ppage = next_ppage - 1; |
| 9967 | |
| 9968 | ret = add_swap_extent(sis, bsi->nr_pages, nr_pages, first_ppage); |
| 9969 | if (ret < 0) |
| 9970 | return ret; |
| 9971 | bsi->nr_extents += ret; |
| 9972 | bsi->nr_pages += nr_pages; |
| 9973 | return 0; |
| 9974 | } |
| 9975 | |
| 9976 | static void btrfs_swap_deactivate(struct file *file) |
| 9977 | { |
| 9978 | struct inode *inode = file_inode(file); |
| 9979 | |
| 9980 | btrfs_free_swapfile_pins(inode); |
| 9981 | atomic_dec(&BTRFS_I(inode)->root->nr_swapfiles); |
| 9982 | } |
| 9983 | |
| 9984 | static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file, |
| 9985 | sector_t *span) |
| 9986 | { |
| 9987 | struct inode *inode = file_inode(file); |
| 9988 | struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info; |
| 9989 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
| 9990 | struct extent_state *cached_state = NULL; |
| 9991 | struct extent_map *em = NULL; |
| 9992 | struct btrfs_device *device = NULL; |
| 9993 | struct btrfs_swap_info bsi = { |
| 9994 | .lowest_ppage = (sector_t)-1ULL, |
| 9995 | }; |
| 9996 | int ret = 0; |
| 9997 | u64 isize; |
| 9998 | u64 start; |
| 9999 | |
| 10000 | /* |
| 10001 | * If the swap file was just created, make sure delalloc is done. If the |
| 10002 | * file changes again after this, the user is doing something stupid and |
| 10003 | * we don't really care. |
| 10004 | */ |
| 10005 | ret = btrfs_wait_ordered_range(inode, 0, (u64)-1); |
| 10006 | if (ret) |
| 10007 | return ret; |
| 10008 | |
| 10009 | /* |
| 10010 | * The inode is locked, so these flags won't change after we check them. |
| 10011 | */ |
| 10012 | if (BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS) { |
| 10013 | btrfs_warn(fs_info, "swapfile must not be compressed"); |
| 10014 | return -EINVAL; |
| 10015 | } |
| 10016 | if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW)) { |
| 10017 | btrfs_warn(fs_info, "swapfile must not be copy-on-write"); |
| 10018 | return -EINVAL; |
| 10019 | } |
| 10020 | if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) { |
| 10021 | btrfs_warn(fs_info, "swapfile must not be checksummed"); |
| 10022 | return -EINVAL; |
| 10023 | } |
| 10024 | |
| 10025 | /* |
| 10026 | * Balance or device remove/replace/resize can move stuff around from |
Goldwyn Rodrigues | c3e1f96 | 2020-08-25 10:02:32 -0500 | [diff] [blame] | 10027 | * under us. The exclop protection makes sure they aren't running/won't |
| 10028 | * run concurrently while we are mapping the swap extents, and |
| 10029 | * fs_info->swapfile_pins prevents them from running while the swap |
| 10030 | * file is active and moving the extents. Note that this also prevents |
| 10031 | * 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] | 10032 | * really worth the trouble to allow it. |
| 10033 | */ |
Goldwyn Rodrigues | c3e1f96 | 2020-08-25 10:02:32 -0500 | [diff] [blame] | 10034 | if (!btrfs_exclop_start(fs_info, BTRFS_EXCLOP_SWAP_ACTIVATE)) { |
Omar Sandoval | ed46ff3 | 2016-11-03 10:28:14 -0700 | [diff] [blame] | 10035 | btrfs_warn(fs_info, |
| 10036 | "cannot activate swapfile while exclusive operation is running"); |
| 10037 | return -EBUSY; |
| 10038 | } |
| 10039 | /* |
| 10040 | * Snapshots can create extents which require COW even if NODATACOW is |
| 10041 | * set. We use this counter to prevent snapshots. We must increment it |
| 10042 | * before walking the extents because we don't want a concurrent |
| 10043 | * snapshot to run after we've already checked the extents. |
| 10044 | */ |
| 10045 | atomic_inc(&BTRFS_I(inode)->root->nr_swapfiles); |
| 10046 | |
| 10047 | isize = ALIGN_DOWN(inode->i_size, fs_info->sectorsize); |
| 10048 | |
| 10049 | lock_extent_bits(io_tree, 0, isize - 1, &cached_state); |
| 10050 | start = 0; |
| 10051 | while (start < isize) { |
| 10052 | u64 logical_block_start, physical_block_start; |
David Sterba | 32da5386 | 2019-10-29 19:20:18 +0100 | [diff] [blame] | 10053 | struct btrfs_block_group *bg; |
Omar Sandoval | ed46ff3 | 2016-11-03 10:28:14 -0700 | [diff] [blame] | 10054 | u64 len = isize - start; |
| 10055 | |
Omar Sandoval | 39b07b5 | 2019-12-02 17:34:23 -0800 | [diff] [blame] | 10056 | em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len); |
Omar Sandoval | ed46ff3 | 2016-11-03 10:28:14 -0700 | [diff] [blame] | 10057 | if (IS_ERR(em)) { |
| 10058 | ret = PTR_ERR(em); |
| 10059 | goto out; |
| 10060 | } |
| 10061 | |
| 10062 | if (em->block_start == EXTENT_MAP_HOLE) { |
| 10063 | btrfs_warn(fs_info, "swapfile must not have holes"); |
| 10064 | ret = -EINVAL; |
| 10065 | goto out; |
| 10066 | } |
| 10067 | if (em->block_start == EXTENT_MAP_INLINE) { |
| 10068 | /* |
| 10069 | * It's unlikely we'll ever actually find ourselves |
| 10070 | * here, as a file small enough to fit inline won't be |
| 10071 | * big enough to store more than the swap header, but in |
| 10072 | * case something changes in the future, let's catch it |
| 10073 | * here rather than later. |
| 10074 | */ |
| 10075 | btrfs_warn(fs_info, "swapfile must not be inline"); |
| 10076 | ret = -EINVAL; |
| 10077 | goto out; |
| 10078 | } |
| 10079 | if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) { |
| 10080 | btrfs_warn(fs_info, "swapfile must not be compressed"); |
| 10081 | ret = -EINVAL; |
| 10082 | goto out; |
| 10083 | } |
| 10084 | |
| 10085 | logical_block_start = em->block_start + (start - em->start); |
| 10086 | len = min(len, em->len - (start - em->start)); |
| 10087 | free_extent_map(em); |
| 10088 | em = NULL; |
| 10089 | |
Boris Burkov | a84d5d4 | 2020-08-18 11:00:05 -0700 | [diff] [blame] | 10090 | ret = can_nocow_extent(inode, start, &len, NULL, NULL, NULL, true); |
Omar Sandoval | ed46ff3 | 2016-11-03 10:28:14 -0700 | [diff] [blame] | 10091 | if (ret < 0) { |
| 10092 | goto out; |
| 10093 | } else if (ret) { |
| 10094 | ret = 0; |
| 10095 | } else { |
| 10096 | btrfs_warn(fs_info, |
| 10097 | "swapfile must not be copy-on-write"); |
| 10098 | ret = -EINVAL; |
| 10099 | goto out; |
| 10100 | } |
| 10101 | |
| 10102 | em = btrfs_get_chunk_map(fs_info, logical_block_start, len); |
| 10103 | if (IS_ERR(em)) { |
| 10104 | ret = PTR_ERR(em); |
| 10105 | goto out; |
| 10106 | } |
| 10107 | |
| 10108 | if (em->map_lookup->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) { |
| 10109 | btrfs_warn(fs_info, |
| 10110 | "swapfile must have single data profile"); |
| 10111 | ret = -EINVAL; |
| 10112 | goto out; |
| 10113 | } |
| 10114 | |
| 10115 | if (device == NULL) { |
| 10116 | device = em->map_lookup->stripes[0].dev; |
| 10117 | ret = btrfs_add_swapfile_pin(inode, device, false); |
| 10118 | if (ret == 1) |
| 10119 | ret = 0; |
| 10120 | else if (ret) |
| 10121 | goto out; |
| 10122 | } else if (device != em->map_lookup->stripes[0].dev) { |
| 10123 | btrfs_warn(fs_info, "swapfile must be on one device"); |
| 10124 | ret = -EINVAL; |
| 10125 | goto out; |
| 10126 | } |
| 10127 | |
| 10128 | physical_block_start = (em->map_lookup->stripes[0].physical + |
| 10129 | (logical_block_start - em->start)); |
| 10130 | len = min(len, em->len - (logical_block_start - em->start)); |
| 10131 | free_extent_map(em); |
| 10132 | em = NULL; |
| 10133 | |
| 10134 | bg = btrfs_lookup_block_group(fs_info, logical_block_start); |
| 10135 | if (!bg) { |
| 10136 | btrfs_warn(fs_info, |
| 10137 | "could not find block group containing swapfile"); |
| 10138 | ret = -EINVAL; |
| 10139 | goto out; |
| 10140 | } |
| 10141 | |
| 10142 | ret = btrfs_add_swapfile_pin(inode, bg, true); |
| 10143 | if (ret) { |
| 10144 | btrfs_put_block_group(bg); |
| 10145 | if (ret == 1) |
| 10146 | ret = 0; |
| 10147 | else |
| 10148 | goto out; |
| 10149 | } |
| 10150 | |
| 10151 | if (bsi.block_len && |
| 10152 | bsi.block_start + bsi.block_len == physical_block_start) { |
| 10153 | bsi.block_len += len; |
| 10154 | } else { |
| 10155 | if (bsi.block_len) { |
| 10156 | ret = btrfs_add_swap_extent(sis, &bsi); |
| 10157 | if (ret) |
| 10158 | goto out; |
| 10159 | } |
| 10160 | bsi.start = start; |
| 10161 | bsi.block_start = physical_block_start; |
| 10162 | bsi.block_len = len; |
| 10163 | } |
| 10164 | |
| 10165 | start += len; |
| 10166 | } |
| 10167 | |
| 10168 | if (bsi.block_len) |
| 10169 | ret = btrfs_add_swap_extent(sis, &bsi); |
| 10170 | |
| 10171 | out: |
| 10172 | if (!IS_ERR_OR_NULL(em)) |
| 10173 | free_extent_map(em); |
| 10174 | |
| 10175 | unlock_extent_cached(io_tree, 0, isize - 1, &cached_state); |
| 10176 | |
| 10177 | if (ret) |
| 10178 | btrfs_swap_deactivate(file); |
| 10179 | |
Goldwyn Rodrigues | c3e1f96 | 2020-08-25 10:02:32 -0500 | [diff] [blame] | 10180 | btrfs_exclop_finish(fs_info); |
Omar Sandoval | ed46ff3 | 2016-11-03 10:28:14 -0700 | [diff] [blame] | 10181 | |
| 10182 | if (ret) |
| 10183 | return ret; |
| 10184 | |
| 10185 | if (device) |
| 10186 | sis->bdev = device->bdev; |
| 10187 | *span = bsi.highest_ppage - bsi.lowest_ppage + 1; |
| 10188 | sis->max = bsi.nr_pages; |
| 10189 | sis->pages = bsi.nr_pages - 1; |
| 10190 | sis->highest_bit = bsi.nr_pages - 1; |
| 10191 | return bsi.nr_extents; |
| 10192 | } |
| 10193 | #else |
| 10194 | static void btrfs_swap_deactivate(struct file *file) |
| 10195 | { |
| 10196 | } |
| 10197 | |
| 10198 | static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file, |
| 10199 | sector_t *span) |
| 10200 | { |
| 10201 | return -EOPNOTSUPP; |
| 10202 | } |
| 10203 | #endif |
| 10204 | |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 10205 | static const struct inode_operations btrfs_dir_inode_operations = { |
Chris Mason | 3394e16 | 2008-11-17 20:42:26 -0500 | [diff] [blame] | 10206 | .getattr = btrfs_getattr, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10207 | .lookup = btrfs_lookup, |
| 10208 | .create = btrfs_create, |
| 10209 | .unlink = btrfs_unlink, |
| 10210 | .link = btrfs_link, |
| 10211 | .mkdir = btrfs_mkdir, |
| 10212 | .rmdir = btrfs_rmdir, |
Miklos Szeredi | 2773bf0 | 2016-09-27 11:03:58 +0200 | [diff] [blame] | 10213 | .rename = btrfs_rename2, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10214 | .symlink = btrfs_symlink, |
| 10215 | .setattr = btrfs_setattr, |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 10216 | .mknod = btrfs_mknod, |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 10217 | .listxattr = btrfs_listxattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 10218 | .permission = btrfs_permission, |
Christoph Hellwig | 4e34e71 | 2011-07-23 17:37:31 +0200 | [diff] [blame] | 10219 | .get_acl = btrfs_get_acl, |
Christoph Hellwig | 996a710 | 2013-12-20 05:16:43 -0800 | [diff] [blame] | 10220 | .set_acl = btrfs_set_acl, |
Guangyu Sun | 93fd63c | 2013-09-16 10:42:03 -0700 | [diff] [blame] | 10221 | .update_time = btrfs_update_time, |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 10222 | .tmpfile = btrfs_tmpfile, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10223 | }; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 10224 | |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 10225 | static const struct file_operations btrfs_dir_file_operations = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10226 | .llseek = generic_file_llseek, |
| 10227 | .read = generic_read_dir, |
Omar Sandoval | 02dbfc9 | 2016-05-20 13:50:33 -0700 | [diff] [blame] | 10228 | .iterate_shared = btrfs_real_readdir, |
Josef Bacik | 23b5ec7 | 2017-07-24 15:14:25 -0400 | [diff] [blame] | 10229 | .open = btrfs_opendir, |
Christoph Hellwig | 34287aa | 2007-09-14 10:22:47 -0400 | [diff] [blame] | 10230 | .unlocked_ioctl = btrfs_ioctl, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10231 | #ifdef CONFIG_COMPAT |
Luke Dashjr | 4c63c24 | 2015-10-29 08:22:21 +0000 | [diff] [blame] | 10232 | .compat_ioctl = btrfs_compat_ioctl, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10233 | #endif |
Sage Weil | 6bf13c0 | 2008-06-10 10:07:39 -0400 | [diff] [blame] | 10234 | .release = btrfs_release_file, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 10235 | .fsync = btrfs_sync_file, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10236 | }; |
| 10237 | |
Chris Mason | 3505439 | 2009-01-21 13:11:13 -0500 | [diff] [blame] | 10238 | /* |
| 10239 | * btrfs doesn't support the bmap operation because swapfiles |
| 10240 | * use bmap to make a mapping of extents in the file. They assume |
| 10241 | * these extents won't change over the life of the file and they |
| 10242 | * use the bmap result to do IO directly to the drive. |
| 10243 | * |
| 10244 | * the btrfs bmap call would return logical addresses that aren't |
| 10245 | * suitable for IO and they also will change frequently as COW |
| 10246 | * operations happen. So, swapfile + btrfs == corruption. |
| 10247 | * |
| 10248 | * For now we're avoiding this by dropping bmap. |
| 10249 | */ |
Alexey Dobriyan | 7f09410 | 2009-09-21 17:01:10 -0700 | [diff] [blame] | 10250 | static const struct address_space_operations btrfs_aops = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10251 | .readpage = btrfs_readpage, |
| 10252 | .writepage = btrfs_writepage, |
Chris Mason | b293f02e | 2007-11-01 19:45:34 -0400 | [diff] [blame] | 10253 | .writepages = btrfs_writepages, |
Matthew Wilcox (Oracle) | ba206a0 | 2020-06-01 21:47:05 -0700 | [diff] [blame] | 10254 | .readahead = btrfs_readahead, |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 10255 | .direct_IO = noop_direct_IO, |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 10256 | .invalidatepage = btrfs_invalidatepage, |
| 10257 | .releasepage = btrfs_releasepage, |
Roman Gushchin | f8e6608 | 2020-03-04 16:57:35 -0800 | [diff] [blame] | 10258 | #ifdef CONFIG_MIGRATION |
| 10259 | .migratepage = btrfs_migratepage, |
| 10260 | #endif |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 10261 | .set_page_dirty = btrfs_set_page_dirty, |
Andi Kleen | 465fdd9 | 2009-09-16 11:50:18 +0200 | [diff] [blame] | 10262 | .error_remove_page = generic_error_remove_page, |
Omar Sandoval | ed46ff3 | 2016-11-03 10:28:14 -0700 | [diff] [blame] | 10263 | .swap_activate = btrfs_swap_activate, |
| 10264 | .swap_deactivate = btrfs_swap_deactivate, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10265 | }; |
| 10266 | |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 10267 | static const struct inode_operations btrfs_file_inode_operations = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10268 | .getattr = btrfs_getattr, |
| 10269 | .setattr = btrfs_setattr, |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 10270 | .listxattr = btrfs_listxattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 10271 | .permission = btrfs_permission, |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 10272 | .fiemap = btrfs_fiemap, |
Christoph Hellwig | 4e34e71 | 2011-07-23 17:37:31 +0200 | [diff] [blame] | 10273 | .get_acl = btrfs_get_acl, |
Christoph Hellwig | 996a710 | 2013-12-20 05:16:43 -0800 | [diff] [blame] | 10274 | .set_acl = btrfs_set_acl, |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 10275 | .update_time = btrfs_update_time, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10276 | }; |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 10277 | static const struct inode_operations btrfs_special_inode_operations = { |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 10278 | .getattr = btrfs_getattr, |
| 10279 | .setattr = btrfs_setattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 10280 | .permission = btrfs_permission, |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 10281 | .listxattr = btrfs_listxattr, |
Christoph Hellwig | 4e34e71 | 2011-07-23 17:37:31 +0200 | [diff] [blame] | 10282 | .get_acl = btrfs_get_acl, |
Christoph Hellwig | 996a710 | 2013-12-20 05:16:43 -0800 | [diff] [blame] | 10283 | .set_acl = btrfs_set_acl, |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 10284 | .update_time = btrfs_update_time, |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 10285 | }; |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 10286 | static const struct inode_operations btrfs_symlink_inode_operations = { |
Al Viro | 6b25539 | 2015-11-17 10:20:54 -0500 | [diff] [blame] | 10287 | .get_link = page_get_link, |
Li Zefan | f209561 | 2010-11-19 02:05:24 +0000 | [diff] [blame] | 10288 | .getattr = btrfs_getattr, |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 10289 | .setattr = btrfs_setattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 10290 | .permission = btrfs_permission, |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 10291 | .listxattr = btrfs_listxattr, |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 10292 | .update_time = btrfs_update_time, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10293 | }; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 10294 | |
Alexey Dobriyan | 82d339d | 2009-10-09 09:54:36 -0400 | [diff] [blame] | 10295 | const struct dentry_operations btrfs_dentry_operations = { |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 10296 | .d_delete = btrfs_dentry_delete, |
| 10297 | }; |