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; |
Josef Bacik | 0eb7929 | 2020-09-03 11:16:51 -0400 | [diff] [blame] | 65 | bool sync; |
Filipe Manana | f28a492 | 2015-12-08 19:23:20 +0000 | [diff] [blame] | 66 | }; |
| 67 | |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 68 | static const struct inode_operations btrfs_dir_inode_operations; |
| 69 | static const struct inode_operations btrfs_symlink_inode_operations; |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 70 | static const struct inode_operations btrfs_special_inode_operations; |
| 71 | static const struct inode_operations btrfs_file_inode_operations; |
Alexey Dobriyan | 7f09410 | 2009-09-21 17:01:10 -0700 | [diff] [blame] | 72 | static const struct address_space_operations btrfs_aops; |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 73 | static const struct file_operations btrfs_dir_file_operations; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 74 | |
| 75 | static struct kmem_cache *btrfs_inode_cachep; |
| 76 | struct kmem_cache *btrfs_trans_handle_cachep; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 77 | struct kmem_cache *btrfs_path_cachep; |
Josef Bacik | dc89e98 | 2011-01-28 17:05:48 -0500 | [diff] [blame] | 78 | struct kmem_cache *btrfs_free_space_cachep; |
Christophe Leroy | 3acd485 | 2019-08-21 15:05:55 +0000 | [diff] [blame] | 79 | struct kmem_cache *btrfs_free_space_bitmap_cachep; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 80 | |
Eric Sandeen | 3972f26 | 2013-01-12 02:57:22 +0000 | [diff] [blame] | 81 | static int btrfs_setsize(struct inode *inode, struct iattr *attr); |
Filipe Manana | 213e8c5 | 2018-02-06 20:40:31 +0000 | [diff] [blame] | 82 | static int btrfs_truncate(struct inode *inode, bool skip_writeback); |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 83 | static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent); |
Nikolay Borisov | 6e26c44 | 2020-06-03 08:55:14 +0300 | [diff] [blame] | 84 | static noinline int cow_file_range(struct btrfs_inode *inode, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 85 | struct page *locked_page, |
Nikolay Borisov | 74e9194 | 2019-07-17 16:18:16 +0300 | [diff] [blame] | 86 | u64 start, u64 end, int *page_started, |
Nikolay Borisov | 330a582 | 2019-07-17 16:18:17 +0300 | [diff] [blame] | 87 | unsigned long *nr_written, int unlock); |
Nikolay Borisov | 4b67c11 | 2020-06-03 08:55:05 +0300 | [diff] [blame] | 88 | static struct extent_map *create_io_em(struct btrfs_inode *inode, u64 start, |
| 89 | u64 len, u64 orig_start, u64 block_start, |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 90 | u64 block_len, u64 orig_block_len, |
| 91 | u64 ram_bytes, int compress_type, |
| 92 | int type); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 93 | |
Nikolay Borisov | b672b5c | 2020-06-03 08:55:24 +0300 | [diff] [blame] | 94 | static void __endio_write_update_ordered(struct btrfs_inode *inode, |
Qu Wenruo | 52427260 | 2017-03-08 10:25:52 +0800 | [diff] [blame] | 95 | const u64 offset, const u64 bytes, |
| 96 | const bool uptodate); |
| 97 | |
| 98 | /* |
| 99 | * Cleanup all submitted ordered extents in specified range to handle errors |
Andrea Gelmini | 52042d8 | 2018-11-28 12:05:13 +0100 | [diff] [blame] | 100 | * from the btrfs_run_delalloc_range() callback. |
Qu Wenruo | 52427260 | 2017-03-08 10:25:52 +0800 | [diff] [blame] | 101 | * |
| 102 | * NOTE: caller must ensure that when an error happens, it can not call |
| 103 | * extent_clear_unlock_delalloc() to clear both the bits EXTENT_DO_ACCOUNTING |
| 104 | * and EXTENT_DELALLOC simultaneously, because that causes the reserved metadata |
| 105 | * 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] | 106 | * extent (btrfs_finish_ordered_io()). |
Qu Wenruo | 52427260 | 2017-03-08 10:25:52 +0800 | [diff] [blame] | 107 | */ |
Nikolay Borisov | 64e1db5 | 2020-06-03 08:55:25 +0300 | [diff] [blame] | 108 | static inline void btrfs_cleanup_ordered_extents(struct btrfs_inode *inode, |
Nikolay Borisov | d1051d6 | 2018-11-21 17:10:52 +0200 | [diff] [blame] | 109 | struct page *locked_page, |
| 110 | u64 offset, u64 bytes) |
Qu Wenruo | 52427260 | 2017-03-08 10:25:52 +0800 | [diff] [blame] | 111 | { |
Naohiro Aota | 63d71450c | 2017-09-01 17:58:47 +0900 | [diff] [blame] | 112 | unsigned long index = offset >> PAGE_SHIFT; |
| 113 | unsigned long end_index = (offset + bytes - 1) >> PAGE_SHIFT; |
Nikolay Borisov | d1051d6 | 2018-11-21 17:10:52 +0200 | [diff] [blame] | 114 | u64 page_start = page_offset(locked_page); |
| 115 | u64 page_end = page_start + PAGE_SIZE - 1; |
| 116 | |
Naohiro Aota | 63d71450c | 2017-09-01 17:58:47 +0900 | [diff] [blame] | 117 | struct page *page; |
| 118 | |
| 119 | while (index <= end_index) { |
Nikolay Borisov | 64e1db5 | 2020-06-03 08:55:25 +0300 | [diff] [blame] | 120 | page = find_get_page(inode->vfs_inode.i_mapping, index); |
Naohiro Aota | 63d71450c | 2017-09-01 17:58:47 +0900 | [diff] [blame] | 121 | index++; |
| 122 | if (!page) |
| 123 | continue; |
| 124 | ClearPagePrivate2(page); |
| 125 | put_page(page); |
| 126 | } |
Nikolay Borisov | d1051d6 | 2018-11-21 17:10:52 +0200 | [diff] [blame] | 127 | |
| 128 | /* |
| 129 | * In case this page belongs to the delalloc range being instantiated |
| 130 | * then skip it, since the first page of a range is going to be |
| 131 | * properly cleaned up by the caller of run_delalloc_range |
| 132 | */ |
| 133 | if (page_start >= offset && page_end <= (offset + bytes - 1)) { |
| 134 | offset += PAGE_SIZE; |
| 135 | bytes -= PAGE_SIZE; |
| 136 | } |
| 137 | |
Nikolay Borisov | 64e1db5 | 2020-06-03 08:55:25 +0300 | [diff] [blame] | 138 | return __endio_write_update_ordered(inode, offset, bytes, false); |
Qu Wenruo | 52427260 | 2017-03-08 10:25:52 +0800 | [diff] [blame] | 139 | } |
| 140 | |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 141 | static int btrfs_dirty_inode(struct inode *inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 142 | |
Yan, Zheng | f34f57a | 2009-11-12 09:35:27 +0000 | [diff] [blame] | 143 | static int btrfs_init_inode_security(struct btrfs_trans_handle *trans, |
Eric Paris | 2a7dba3 | 2011-02-01 11:05:39 -0500 | [diff] [blame] | 144 | struct inode *inode, struct inode *dir, |
| 145 | const struct qstr *qstr) |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 146 | { |
| 147 | int err; |
| 148 | |
Yan, Zheng | f34f57a | 2009-11-12 09:35:27 +0000 | [diff] [blame] | 149 | err = btrfs_init_acl(trans, inode, dir); |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 150 | if (!err) |
Eric Paris | 2a7dba3 | 2011-02-01 11:05:39 -0500 | [diff] [blame] | 151 | err = btrfs_xattr_security_init(trans, inode, dir, qstr); |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 152 | return err; |
| 153 | } |
| 154 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 155 | /* |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 156 | * this does all the hard work for inserting an inline extent into |
| 157 | * the btree. The caller should have done a btrfs_drop_extents so that |
| 158 | * no overlapping inline items exist in the btree |
| 159 | */ |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 160 | static int insert_inline_extent(struct btrfs_trans_handle *trans, |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 161 | struct btrfs_path *path, int extent_inserted, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 162 | struct btrfs_root *root, struct inode *inode, |
| 163 | u64 start, size_t size, size_t compressed_size, |
Li Zefan | fe3f566 | 2011-03-28 08:30:38 +0000 | [diff] [blame] | 164 | int compress_type, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 165 | struct page **compressed_pages) |
| 166 | { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 167 | struct extent_buffer *leaf; |
| 168 | struct page *page = NULL; |
| 169 | char *kaddr; |
| 170 | unsigned long ptr; |
| 171 | struct btrfs_file_extent_item *ei; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 172 | int ret; |
| 173 | size_t cur_size = size; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 174 | unsigned long offset; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 175 | |
Jia-Ju Bai | 982f1f5d | 2019-07-27 16:51:13 +0800 | [diff] [blame] | 176 | ASSERT((compressed_size > 0 && compressed_pages) || |
| 177 | (compressed_size == 0 && !compressed_pages)); |
| 178 | |
Li Zefan | fe3f566 | 2011-03-28 08:30:38 +0000 | [diff] [blame] | 179 | if (compressed_size && compressed_pages) |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 180 | cur_size = compressed_size; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 181 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 182 | inode_add_bytes(inode, size); |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 183 | |
| 184 | if (!extent_inserted) { |
| 185 | struct btrfs_key key; |
| 186 | size_t datasize; |
| 187 | |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 188 | key.objectid = btrfs_ino(BTRFS_I(inode)); |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 189 | key.offset = start; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 190 | key.type = BTRFS_EXTENT_DATA_KEY; |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 191 | |
| 192 | datasize = btrfs_file_extent_calc_inline_size(cur_size); |
| 193 | path->leave_spinning = 1; |
| 194 | ret = btrfs_insert_empty_item(trans, root, path, &key, |
| 195 | datasize); |
David Sterba | 79b4f4c | 2017-06-15 19:09:51 +0200 | [diff] [blame] | 196 | if (ret) |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 197 | goto fail; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 198 | } |
| 199 | leaf = path->nodes[0]; |
| 200 | ei = btrfs_item_ptr(leaf, path->slots[0], |
| 201 | struct btrfs_file_extent_item); |
| 202 | btrfs_set_file_extent_generation(leaf, ei, trans->transid); |
| 203 | btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE); |
| 204 | btrfs_set_file_extent_encryption(leaf, ei, 0); |
| 205 | btrfs_set_file_extent_other_encoding(leaf, ei, 0); |
| 206 | btrfs_set_file_extent_ram_bytes(leaf, ei, size); |
| 207 | ptr = btrfs_file_extent_inline_start(ei); |
| 208 | |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 209 | if (compress_type != BTRFS_COMPRESS_NONE) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 210 | struct page *cpage; |
| 211 | int i = 0; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 212 | while (compressed_size > 0) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 213 | cpage = compressed_pages[i]; |
Chris Mason | 5b050f0 | 2008-11-11 09:34:41 -0500 | [diff] [blame] | 214 | cur_size = min_t(unsigned long, compressed_size, |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 215 | PAGE_SIZE); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 216 | |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 217 | kaddr = kmap_atomic(cpage); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 218 | write_extent_buffer(leaf, kaddr, ptr, cur_size); |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 219 | kunmap_atomic(kaddr); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 220 | |
| 221 | i++; |
| 222 | ptr += cur_size; |
| 223 | compressed_size -= cur_size; |
| 224 | } |
| 225 | btrfs_set_file_extent_compression(leaf, ei, |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 226 | compress_type); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 227 | } else { |
| 228 | page = find_get_page(inode->i_mapping, |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 229 | start >> PAGE_SHIFT); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 230 | btrfs_set_file_extent_compression(leaf, ei, 0); |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 231 | kaddr = kmap_atomic(page); |
Johannes Thumshirn | 7073017 | 2018-12-05 15:23:03 +0100 | [diff] [blame] | 232 | offset = offset_in_page(start); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 233 | write_extent_buffer(leaf, kaddr + offset, ptr, size); |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 234 | kunmap_atomic(kaddr); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 235 | put_page(page); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 236 | } |
| 237 | btrfs_mark_buffer_dirty(leaf); |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 238 | btrfs_release_path(path); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 239 | |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 240 | /* |
Josef Bacik | 9ddc959 | 2020-01-17 09:02:22 -0500 | [diff] [blame] | 241 | * We align size to sectorsize for inline extents just for simplicity |
| 242 | * sake. |
| 243 | */ |
| 244 | size = ALIGN(size, root->fs_info->sectorsize); |
| 245 | ret = btrfs_inode_set_file_extent_range(BTRFS_I(inode), start, size); |
| 246 | if (ret) |
| 247 | goto fail; |
| 248 | |
| 249 | /* |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 250 | * we're an inline extent, so nobody can |
| 251 | * extend the file past i_size without locking |
| 252 | * a page we already have locked. |
| 253 | * |
| 254 | * We must do any isize and inode updates |
| 255 | * before we unlock the pages. Otherwise we |
| 256 | * could end up racing with unlink. |
| 257 | */ |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 258 | BTRFS_I(inode)->disk_i_size = inode->i_size; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 259 | ret = btrfs_update_inode(trans, root, inode); |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 260 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 261 | fail: |
David Sterba | 79b4f4c | 2017-06-15 19:09:51 +0200 | [diff] [blame] | 262 | return ret; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | |
| 266 | /* |
| 267 | * conditionally insert an inline extent into the file. This |
| 268 | * does the checks required to make sure the data is small enough |
| 269 | * to fit as an inline extent. |
| 270 | */ |
Nikolay Borisov | a034940 | 2020-06-03 08:55:12 +0300 | [diff] [blame] | 271 | 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] | 272 | u64 end, size_t compressed_size, |
| 273 | int compress_type, |
| 274 | struct page **compressed_pages) |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 275 | { |
Nikolay Borisov | a034940 | 2020-06-03 08:55:12 +0300 | [diff] [blame] | 276 | struct btrfs_root *root = inode->root; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 277 | struct btrfs_fs_info *fs_info = root->fs_info; |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 278 | struct btrfs_trans_handle *trans; |
Nikolay Borisov | a034940 | 2020-06-03 08:55:12 +0300 | [diff] [blame] | 279 | u64 isize = i_size_read(&inode->vfs_inode); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 280 | u64 actual_end = min(end + 1, isize); |
| 281 | u64 inline_len = actual_end - start; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 282 | u64 aligned_end = ALIGN(end, fs_info->sectorsize); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 283 | u64 data_len = inline_len; |
| 284 | int ret; |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 285 | struct btrfs_path *path; |
| 286 | int extent_inserted = 0; |
| 287 | u32 extent_item_size; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 288 | |
| 289 | if (compressed_size) |
| 290 | data_len = compressed_size; |
| 291 | |
| 292 | if (start > 0 || |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 293 | actual_end > fs_info->sectorsize || |
| 294 | data_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info) || |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 295 | (!compressed_size && |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 296 | (actual_end & (fs_info->sectorsize - 1)) == 0) || |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 297 | end + 1 < isize || |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 298 | data_len > fs_info->max_inline) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 299 | return 1; |
| 300 | } |
| 301 | |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 302 | path = btrfs_alloc_path(); |
| 303 | if (!path) |
| 304 | return -ENOMEM; |
| 305 | |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 306 | trans = btrfs_join_transaction(root); |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 307 | if (IS_ERR(trans)) { |
| 308 | btrfs_free_path(path); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 309 | return PTR_ERR(trans); |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 310 | } |
Nikolay Borisov | a034940 | 2020-06-03 08:55:12 +0300 | [diff] [blame] | 311 | trans->block_rsv = &inode->block_rsv; |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 312 | |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 313 | if (compressed_size && compressed_pages) |
| 314 | extent_item_size = btrfs_file_extent_calc_inline_size( |
| 315 | compressed_size); |
| 316 | else |
| 317 | extent_item_size = btrfs_file_extent_calc_inline_size( |
| 318 | inline_len); |
| 319 | |
Nikolay Borisov | a034940 | 2020-06-03 08:55:12 +0300 | [diff] [blame] | 320 | ret = __btrfs_drop_extents(trans, root, inode, path, start, aligned_end, |
| 321 | NULL, 1, 1, extent_item_size, |
| 322 | &extent_inserted); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 323 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 324 | btrfs_abort_transaction(trans, ret); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 325 | goto out; |
| 326 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 327 | |
| 328 | if (isize > actual_end) |
| 329 | inline_len = min_t(u64, isize, actual_end); |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 330 | ret = insert_inline_extent(trans, path, extent_inserted, |
Nikolay Borisov | a034940 | 2020-06-03 08:55:12 +0300 | [diff] [blame] | 331 | root, &inode->vfs_inode, start, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 332 | inline_len, compressed_size, |
Li Zefan | fe3f566 | 2011-03-28 08:30:38 +0000 | [diff] [blame] | 333 | compress_type, compressed_pages); |
Josef Bacik | 2adcac1 | 2012-05-23 16:10:14 -0400 | [diff] [blame] | 334 | if (ret && ret != -ENOSPC) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 335 | btrfs_abort_transaction(trans, ret); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 336 | goto out; |
Josef Bacik | 2adcac1 | 2012-05-23 16:10:14 -0400 | [diff] [blame] | 337 | } else if (ret == -ENOSPC) { |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 338 | ret = 1; |
| 339 | goto out; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 340 | } |
Josef Bacik | 2adcac1 | 2012-05-23 16:10:14 -0400 | [diff] [blame] | 341 | |
Nikolay Borisov | a034940 | 2020-06-03 08:55:12 +0300 | [diff] [blame] | 342 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &inode->runtime_flags); |
| 343 | btrfs_drop_extent_cache(inode, start, aligned_end - 1, 0); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 344 | out: |
Qu Wenruo | 94ed938 | 2015-09-08 17:25:56 +0800 | [diff] [blame] | 345 | /* |
| 346 | * Don't forget to free the reserved space, as for inlined extent |
| 347 | * it won't count as data extent, free them directly here. |
| 348 | * And at reserve time, it's always aligned to page size, so |
| 349 | * just free one page here. |
| 350 | */ |
Nikolay Borisov | a034940 | 2020-06-03 08:55:12 +0300 | [diff] [blame] | 351 | btrfs_qgroup_free_data(inode, NULL, 0, PAGE_SIZE); |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 352 | btrfs_free_path(path); |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 353 | btrfs_end_transaction(trans); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 354 | return ret; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 355 | } |
| 356 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 357 | struct async_extent { |
| 358 | u64 start; |
| 359 | u64 ram_size; |
| 360 | u64 compressed_size; |
| 361 | struct page **pages; |
| 362 | unsigned long nr_pages; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 363 | int compress_type; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 364 | struct list_head list; |
| 365 | }; |
| 366 | |
Nikolay Borisov | 97db120 | 2019-03-12 17:20:24 +0200 | [diff] [blame] | 367 | struct async_chunk { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 368 | struct inode *inode; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 369 | struct page *locked_page; |
| 370 | u64 start; |
| 371 | u64 end; |
Liu Bo | f82b735 | 2017-10-23 23:18:16 -0600 | [diff] [blame] | 372 | unsigned int write_flags; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 373 | struct list_head extents; |
Chris Mason | ec39f76 | 2019-07-10 12:28:17 -0700 | [diff] [blame] | 374 | struct cgroup_subsys_state *blkcg_css; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 375 | struct btrfs_work work; |
Nikolay Borisov | 97db120 | 2019-03-12 17:20:24 +0200 | [diff] [blame] | 376 | atomic_t *pending; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 377 | }; |
| 378 | |
Nikolay Borisov | 97db120 | 2019-03-12 17:20:24 +0200 | [diff] [blame] | 379 | struct async_cow { |
| 380 | /* Number of chunks in flight; must be first in the structure */ |
| 381 | atomic_t num_chunks; |
| 382 | struct async_chunk chunks[]; |
| 383 | }; |
| 384 | |
| 385 | static noinline int add_async_extent(struct async_chunk *cow, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 386 | u64 start, u64 ram_size, |
| 387 | u64 compressed_size, |
| 388 | struct page **pages, |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 389 | unsigned long nr_pages, |
| 390 | int compress_type) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 391 | { |
| 392 | struct async_extent *async_extent; |
| 393 | |
| 394 | async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 395 | BUG_ON(!async_extent); /* -ENOMEM */ |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 396 | async_extent->start = start; |
| 397 | async_extent->ram_size = ram_size; |
| 398 | async_extent->compressed_size = compressed_size; |
| 399 | async_extent->pages = pages; |
| 400 | async_extent->nr_pages = nr_pages; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 401 | async_extent->compress_type = compress_type; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 402 | list_add_tail(&async_extent->list, &cow->extents); |
| 403 | return 0; |
| 404 | } |
| 405 | |
Qu Wenruo | 42c16da | 2019-07-01 05:12:46 +0000 | [diff] [blame] | 406 | /* |
| 407 | * Check if the inode has flags compatible with compression |
| 408 | */ |
Nikolay Borisov | 99c88dc | 2020-06-03 08:55:26 +0300 | [diff] [blame] | 409 | static inline bool inode_can_compress(struct btrfs_inode *inode) |
Qu Wenruo | 42c16da | 2019-07-01 05:12:46 +0000 | [diff] [blame] | 410 | { |
Nikolay Borisov | 99c88dc | 2020-06-03 08:55:26 +0300 | [diff] [blame] | 411 | if (inode->flags & BTRFS_INODE_NODATACOW || |
| 412 | inode->flags & BTRFS_INODE_NODATASUM) |
Qu Wenruo | 42c16da | 2019-07-01 05:12:46 +0000 | [diff] [blame] | 413 | return false; |
| 414 | return true; |
| 415 | } |
| 416 | |
| 417 | /* |
| 418 | * Check if the inode needs to be submitted to compression, based on mount |
| 419 | * options, defragmentation, properties or heuristics. |
| 420 | */ |
Nikolay Borisov | 808a129 | 2020-06-03 08:55:27 +0300 | [diff] [blame] | 421 | static inline int inode_need_compress(struct btrfs_inode *inode, u64 start, |
| 422 | u64 end) |
Wang Shilong | f79707b | 2014-07-17 11:44:09 +0800 | [diff] [blame] | 423 | { |
Nikolay Borisov | 808a129 | 2020-06-03 08:55:27 +0300 | [diff] [blame] | 424 | struct btrfs_fs_info *fs_info = inode->root->fs_info; |
Wang Shilong | f79707b | 2014-07-17 11:44:09 +0800 | [diff] [blame] | 425 | |
Nikolay Borisov | 808a129 | 2020-06-03 08:55:27 +0300 | [diff] [blame] | 426 | if (!inode_can_compress(inode)) { |
Qu Wenruo | 42c16da | 2019-07-01 05:12:46 +0000 | [diff] [blame] | 427 | WARN(IS_ENABLED(CONFIG_BTRFS_DEBUG), |
| 428 | KERN_ERR "BTRFS: unexpected compression for ino %llu\n", |
Nikolay Borisov | 808a129 | 2020-06-03 08:55:27 +0300 | [diff] [blame] | 429 | btrfs_ino(inode)); |
Qu Wenruo | 42c16da | 2019-07-01 05:12:46 +0000 | [diff] [blame] | 430 | return 0; |
| 431 | } |
Wang Shilong | f79707b | 2014-07-17 11:44:09 +0800 | [diff] [blame] | 432 | /* force compress */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 433 | if (btrfs_test_opt(fs_info, FORCE_COMPRESS)) |
Wang Shilong | f79707b | 2014-07-17 11:44:09 +0800 | [diff] [blame] | 434 | return 1; |
David Sterba | eec63c6 | 2017-07-17 19:41:31 +0200 | [diff] [blame] | 435 | /* defrag ioctl */ |
Nikolay Borisov | 808a129 | 2020-06-03 08:55:27 +0300 | [diff] [blame] | 436 | if (inode->defrag_compress) |
David Sterba | eec63c6 | 2017-07-17 19:41:31 +0200 | [diff] [blame] | 437 | return 1; |
Wang Shilong | f79707b | 2014-07-17 11:44:09 +0800 | [diff] [blame] | 438 | /* bad compression ratios */ |
Nikolay Borisov | 808a129 | 2020-06-03 08:55:27 +0300 | [diff] [blame] | 439 | if (inode->flags & BTRFS_INODE_NOCOMPRESS) |
Wang Shilong | f79707b | 2014-07-17 11:44:09 +0800 | [diff] [blame] | 440 | return 0; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 441 | if (btrfs_test_opt(fs_info, COMPRESS) || |
Nikolay Borisov | 808a129 | 2020-06-03 08:55:27 +0300 | [diff] [blame] | 442 | inode->flags & BTRFS_INODE_COMPRESS || |
| 443 | inode->prop_compress) |
| 444 | return btrfs_compress_heuristic(&inode->vfs_inode, start, end); |
Wang Shilong | f79707b | 2014-07-17 11:44:09 +0800 | [diff] [blame] | 445 | return 0; |
| 446 | } |
| 447 | |
Nikolay Borisov | 6158e1c | 2017-02-20 13:50:43 +0200 | [diff] [blame] | 448 | static inline void inode_should_defrag(struct btrfs_inode *inode, |
Anand Jain | 26d30f8 | 2016-12-19 19:09:06 +0800 | [diff] [blame] | 449 | u64 start, u64 end, u64 num_bytes, u64 small_write) |
| 450 | { |
| 451 | /* If this is a small write inside eof, kick off a defrag */ |
| 452 | if (num_bytes < small_write && |
Nikolay Borisov | 6158e1c | 2017-02-20 13:50:43 +0200 | [diff] [blame] | 453 | (start > 0 || end + 1 < inode->disk_i_size)) |
Anand Jain | 26d30f8 | 2016-12-19 19:09:06 +0800 | [diff] [blame] | 454 | btrfs_add_inode_defrag(NULL, inode); |
| 455 | } |
| 456 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 457 | /* |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 458 | * we create compressed extents in two phases. The first |
| 459 | * phase compresses a range of pages that have already been |
| 460 | * locked (both pages and state bits are locked). |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 461 | * |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 462 | * This is done inside an ordered work queue, and the compression |
| 463 | * is spread across many cpus. The actual IO submission is step |
| 464 | * two, and the ordered work queue takes care of making sure that |
| 465 | * happens in the same order things were put onto the queue by |
| 466 | * writepages and friends. |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 467 | * |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 468 | * If this code finds it can't get good compression, it puts an |
| 469 | * entry onto the work queue to write the uncompressed bytes. This |
| 470 | * makes sure that both compressed inodes and uncompressed inodes |
Artem Bityutskiy | b257031 | 2012-07-25 18:12:06 +0300 | [diff] [blame] | 471 | * are written in the same order that the flusher thread sent them |
| 472 | * down. |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 473 | */ |
Nikolay Borisov | ac3e993 | 2019-07-17 14:41:44 +0300 | [diff] [blame] | 474 | static noinline int compress_file_range(struct async_chunk *async_chunk) |
Chris Mason | b888db2b | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 475 | { |
Nikolay Borisov | 1368c6d | 2019-03-12 17:20:27 +0200 | [diff] [blame] | 476 | struct inode *inode = async_chunk->inode; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 477 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 478 | u64 blocksize = fs_info->sectorsize; |
Nikolay Borisov | 1368c6d | 2019-03-12 17:20:27 +0200 | [diff] [blame] | 479 | u64 start = async_chunk->start; |
| 480 | u64 end = async_chunk->end; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 481 | u64 actual_end; |
Josef Bacik | d98da49 | 2019-10-11 09:03:54 -0400 | [diff] [blame] | 482 | u64 i_size; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 483 | int ret = 0; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 484 | struct page **pages = NULL; |
| 485 | unsigned long nr_pages; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 486 | unsigned long total_compressed = 0; |
| 487 | unsigned long total_in = 0; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 488 | int i; |
| 489 | int will_compress; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 490 | int compress_type = fs_info->compress_type; |
Nikolay Borisov | ac3e993 | 2019-07-17 14:41:44 +0300 | [diff] [blame] | 491 | int compressed_extents = 0; |
Chris Mason | 4adaa61 | 2013-03-26 13:07:00 -0400 | [diff] [blame] | 492 | int redirty = 0; |
Chris Mason | b888db2b | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 493 | |
Nikolay Borisov | 6158e1c | 2017-02-20 13:50:43 +0200 | [diff] [blame] | 494 | inode_should_defrag(BTRFS_I(inode), start, end, end - start + 1, |
| 495 | SZ_16K); |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 496 | |
Josef Bacik | d98da49 | 2019-10-11 09:03:54 -0400 | [diff] [blame] | 497 | /* |
| 498 | * We need to save i_size before now because it could change in between |
| 499 | * us evaluating the size and assigning it. This is because we lock and |
| 500 | * unlock the page in truncate and fallocate, and then modify the i_size |
| 501 | * later on. |
| 502 | * |
| 503 | * The barriers are to emulate READ_ONCE, remove that once i_size_read |
| 504 | * does that for us. |
| 505 | */ |
| 506 | barrier(); |
| 507 | i_size = i_size_read(inode); |
| 508 | barrier(); |
| 509 | actual_end = min_t(u64, i_size, end + 1); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 510 | again: |
| 511 | will_compress = 0; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 512 | nr_pages = (end >> PAGE_SHIFT) - (start >> PAGE_SHIFT) + 1; |
David Sterba | 069eac7 | 2017-02-14 19:36:54 +0100 | [diff] [blame] | 513 | BUILD_BUG_ON((BTRFS_MAX_COMPRESSED % PAGE_SIZE) != 0); |
| 514 | nr_pages = min_t(unsigned long, nr_pages, |
| 515 | BTRFS_MAX_COMPRESSED / PAGE_SIZE); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 516 | |
Chris Mason | f03d9301f | 2009-02-04 09:31:06 -0500 | [diff] [blame] | 517 | /* |
| 518 | * we don't want to send crud past the end of i_size through |
| 519 | * compression, that's just a waste of CPU time. So, if the |
| 520 | * end of the file is before the start of our current |
| 521 | * requested range of bytes, we bail out to the uncompressed |
| 522 | * cleanup code that can deal with all of this. |
| 523 | * |
| 524 | * It isn't really the fastest way to fix things, but this is a |
| 525 | * very uncommon corner. |
| 526 | */ |
| 527 | if (actual_end <= start) |
| 528 | goto cleanup_and_bail_uncompressed; |
| 529 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 530 | total_compressed = actual_end - start; |
| 531 | |
Shilong Wang | 4bcbb33 | 2014-10-07 18:44:35 -0400 | [diff] [blame] | 532 | /* |
| 533 | * skip compression for a small file range(<=blocksize) that |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 534 | * 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] | 535 | */ |
| 536 | if (total_compressed <= blocksize && |
| 537 | (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size)) |
| 538 | goto cleanup_and_bail_uncompressed; |
| 539 | |
David Sterba | 069eac7 | 2017-02-14 19:36:54 +0100 | [diff] [blame] | 540 | total_compressed = min_t(unsigned long, total_compressed, |
| 541 | BTRFS_MAX_UNCOMPRESSED); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 542 | total_in = 0; |
| 543 | ret = 0; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 544 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 545 | /* |
| 546 | * we do compression for mount -o compress and when the |
| 547 | * inode has not been flagged as nocompress. This flag can |
| 548 | * change at any time if we discover bad compression ratios. |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 549 | */ |
Qu Wenruo | 3134292 | 2021-08-25 13:41:42 +0800 | [diff] [blame] | 550 | if (inode_need_compress(BTRFS_I(inode), start, end)) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 551 | WARN_ON(pages); |
David Sterba | 31e818f | 2015-02-20 18:00:26 +0100 | [diff] [blame] | 552 | pages = kcalloc(nr_pages, sizeof(struct page *), GFP_NOFS); |
Li Zefan | 560f7d7 | 2011-09-08 10:22:01 +0800 | [diff] [blame] | 553 | if (!pages) { |
| 554 | /* just bail out to the uncompressed code */ |
Filipe Manana | 3527a01 | 2018-10-13 00:37:25 +0100 | [diff] [blame] | 555 | nr_pages = 0; |
Li Zefan | 560f7d7 | 2011-09-08 10:22:01 +0800 | [diff] [blame] | 556 | goto cont; |
| 557 | } |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 558 | |
David Sterba | eec63c6 | 2017-07-17 19:41:31 +0200 | [diff] [blame] | 559 | if (BTRFS_I(inode)->defrag_compress) |
| 560 | compress_type = BTRFS_I(inode)->defrag_compress; |
| 561 | else if (BTRFS_I(inode)->prop_compress) |
David Sterba | b52aa8c | 2017-07-17 19:17:20 +0200 | [diff] [blame] | 562 | compress_type = BTRFS_I(inode)->prop_compress; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 563 | |
Chris Mason | 4adaa61 | 2013-03-26 13:07:00 -0400 | [diff] [blame] | 564 | /* |
| 565 | * we need to call clear_page_dirty_for_io on each |
| 566 | * page in the range. Otherwise applications with the file |
| 567 | * mmap'd can wander in and change the page contents while |
| 568 | * we are compressing them. |
| 569 | * |
| 570 | * If the compression fails for any reason, we set the pages |
| 571 | * dirty again later on. |
Timofey Titovets | e9679de | 2017-10-24 01:29:48 +0300 | [diff] [blame] | 572 | * |
| 573 | * Note that the remaining part is redirtied, the start pointer |
| 574 | * has moved, the end is the original one. |
Chris Mason | 4adaa61 | 2013-03-26 13:07:00 -0400 | [diff] [blame] | 575 | */ |
Timofey Titovets | e9679de | 2017-10-24 01:29:48 +0300 | [diff] [blame] | 576 | if (!redirty) { |
| 577 | extent_range_clear_dirty_for_io(inode, start, end); |
| 578 | redirty = 1; |
| 579 | } |
David Sterba | f51d2b5 | 2017-09-15 17:36:57 +0200 | [diff] [blame] | 580 | |
| 581 | /* Compression level is applied here and only here */ |
| 582 | ret = btrfs_compress_pages( |
| 583 | compress_type | (fs_info->compress_level << 4), |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 584 | inode->i_mapping, start, |
David Sterba | 38c3146 | 2017-02-14 19:04:07 +0100 | [diff] [blame] | 585 | pages, |
David Sterba | 4d3a800 | 2017-02-14 19:04:07 +0100 | [diff] [blame] | 586 | &nr_pages, |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 587 | &total_in, |
David Sterba | e5d7490 | 2017-02-14 19:45:05 +0100 | [diff] [blame] | 588 | &total_compressed); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 589 | |
| 590 | if (!ret) { |
Johannes Thumshirn | 7073017 | 2018-12-05 15:23:03 +0100 | [diff] [blame] | 591 | unsigned long offset = offset_in_page(total_compressed); |
David Sterba | 4d3a800 | 2017-02-14 19:04:07 +0100 | [diff] [blame] | 592 | struct page *page = pages[nr_pages - 1]; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 593 | char *kaddr; |
| 594 | |
| 595 | /* zero the tail end of the last page, we might be |
| 596 | * sending it down to disk |
| 597 | */ |
| 598 | if (offset) { |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 599 | kaddr = kmap_atomic(page); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 600 | memset(kaddr + offset, 0, |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 601 | PAGE_SIZE - offset); |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 602 | kunmap_atomic(kaddr); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 603 | } |
| 604 | will_compress = 1; |
| 605 | } |
| 606 | } |
Li Zefan | 560f7d7 | 2011-09-08 10:22:01 +0800 | [diff] [blame] | 607 | cont: |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 608 | if (start == 0) { |
| 609 | /* lets try to make an inline extent */ |
Timofey Titovets | 6018ba0 | 2017-09-15 01:57:26 +0300 | [diff] [blame] | 610 | if (ret || total_in < actual_end) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 611 | /* we didn't compress the entire range, try |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 612 | * to make an uncompressed inline extent. |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 613 | */ |
Nikolay Borisov | a034940 | 2020-06-03 08:55:12 +0300 | [diff] [blame] | 614 | ret = cow_file_range_inline(BTRFS_I(inode), start, end, |
| 615 | 0, BTRFS_COMPRESS_NONE, |
| 616 | NULL); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 617 | } else { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 618 | /* try making a compressed inline extent */ |
Nikolay Borisov | a034940 | 2020-06-03 08:55:12 +0300 | [diff] [blame] | 619 | ret = cow_file_range_inline(BTRFS_I(inode), start, end, |
Li Zefan | fe3f566 | 2011-03-28 08:30:38 +0000 | [diff] [blame] | 620 | total_compressed, |
| 621 | compress_type, pages); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 622 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 623 | if (ret <= 0) { |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 624 | unsigned long clear_flags = EXTENT_DELALLOC | |
Josef Bacik | 8b62f87 | 2017-10-19 14:15:55 -0400 | [diff] [blame] | 625 | EXTENT_DELALLOC_NEW | EXTENT_DEFRAG | |
| 626 | EXTENT_DO_ACCOUNTING; |
Filipe Manana | e6eb431 | 2014-10-10 10:45:12 +0100 | [diff] [blame] | 627 | unsigned long page_error_op; |
| 628 | |
Filipe Manana | e6eb431 | 2014-10-10 10:45:12 +0100 | [diff] [blame] | 629 | page_error_op = ret < 0 ? PAGE_SET_ERROR : 0; |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 630 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 631 | /* |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 632 | * inline extent creation worked or returned error, |
| 633 | * we don't need to create any more async work items. |
| 634 | * Unlock and free up our temp pages. |
Josef Bacik | 8b62f87 | 2017-10-19 14:15:55 -0400 | [diff] [blame] | 635 | * |
| 636 | * We use DO_ACCOUNTING here because we need the |
| 637 | * delalloc_release_metadata to be done _after_ we drop |
| 638 | * our outstanding extent for clearing delalloc for this |
| 639 | * range. |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 640 | */ |
Nikolay Borisov | ad7ff17 | 2020-06-03 08:55:06 +0300 | [diff] [blame] | 641 | extent_clear_unlock_delalloc(BTRFS_I(inode), start, end, |
| 642 | NULL, |
Nikolay Borisov | 74e9194 | 2019-07-17 16:18:16 +0300 | [diff] [blame] | 643 | clear_flags, |
Qu Wenruo | ba8b04c | 2016-07-19 16:50:36 +0800 | [diff] [blame] | 644 | PAGE_UNLOCK | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 645 | PAGE_CLEAR_DIRTY | |
| 646 | PAGE_SET_WRITEBACK | |
Filipe Manana | e6eb431 | 2014-10-10 10:45:12 +0100 | [diff] [blame] | 647 | page_error_op | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 648 | PAGE_END_WRITEBACK); |
Nikolay Borisov | cecc8d9 | 2019-07-17 14:41:45 +0300 | [diff] [blame] | 649 | |
Qu Wenruo | 1e6e238 | 2020-07-28 16:39:26 +0800 | [diff] [blame] | 650 | /* |
| 651 | * Ensure we only free the compressed pages if we have |
| 652 | * them allocated, as we can still reach here with |
| 653 | * inode_need_compress() == false. |
| 654 | */ |
| 655 | if (pages) { |
| 656 | for (i = 0; i < nr_pages; i++) { |
| 657 | WARN_ON(pages[i]->mapping); |
| 658 | put_page(pages[i]); |
| 659 | } |
| 660 | kfree(pages); |
Nikolay Borisov | cecc8d9 | 2019-07-17 14:41:45 +0300 | [diff] [blame] | 661 | } |
Nikolay Borisov | cecc8d9 | 2019-07-17 14:41:45 +0300 | [diff] [blame] | 662 | return 0; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 663 | } |
| 664 | } |
| 665 | |
| 666 | if (will_compress) { |
| 667 | /* |
| 668 | * we aren't doing an inline extent round the compressed size |
| 669 | * up to a block size boundary so the allocator does sane |
| 670 | * things |
| 671 | */ |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 672 | total_compressed = ALIGN(total_compressed, blocksize); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 673 | |
| 674 | /* |
| 675 | * one last check to make sure the compression is really a |
Timofey Titovets | 170607e | 2017-06-06 14:41:15 +0300 | [diff] [blame] | 676 | * win, compare the page count read with the blocks on disk, |
| 677 | * compression must free at least one sector size |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 678 | */ |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 679 | total_in = ALIGN(total_in, PAGE_SIZE); |
Timofey Titovets | 170607e | 2017-06-06 14:41:15 +0300 | [diff] [blame] | 680 | if (total_compressed + blocksize <= total_in) { |
Nikolay Borisov | ac3e993 | 2019-07-17 14:41:44 +0300 | [diff] [blame] | 681 | compressed_extents++; |
Ashish Samant | c8bb0c8 | 2016-03-25 19:01:33 -0700 | [diff] [blame] | 682 | |
| 683 | /* |
| 684 | * The async work queues will take care of doing actual |
| 685 | * allocation on disk for these compressed pages, and |
| 686 | * will submit them to the elevator. |
| 687 | */ |
Nikolay Borisov | b532627 | 2019-03-12 17:20:25 +0200 | [diff] [blame] | 688 | add_async_extent(async_chunk, start, total_in, |
David Sterba | 4d3a800 | 2017-02-14 19:04:07 +0100 | [diff] [blame] | 689 | total_compressed, pages, nr_pages, |
Ashish Samant | c8bb0c8 | 2016-03-25 19:01:33 -0700 | [diff] [blame] | 690 | compress_type); |
| 691 | |
Timofey Titovets | 1170862 | 2017-10-03 18:06:01 +0300 | [diff] [blame] | 692 | if (start + total_in < end) { |
| 693 | start += total_in; |
Ashish Samant | c8bb0c8 | 2016-03-25 19:01:33 -0700 | [diff] [blame] | 694 | pages = NULL; |
| 695 | cond_resched(); |
| 696 | goto again; |
| 697 | } |
Nikolay Borisov | ac3e993 | 2019-07-17 14:41:44 +0300 | [diff] [blame] | 698 | return compressed_extents; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 699 | } |
| 700 | } |
Ashish Samant | c8bb0c8 | 2016-03-25 19:01:33 -0700 | [diff] [blame] | 701 | if (pages) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 702 | /* |
| 703 | * the compression code ran but failed to make things smaller, |
| 704 | * free any pages it allocated and our page pointer array |
| 705 | */ |
David Sterba | 4d3a800 | 2017-02-14 19:04:07 +0100 | [diff] [blame] | 706 | for (i = 0; i < nr_pages; i++) { |
Chris Mason | 70b99e6 | 2008-10-31 12:46:39 -0400 | [diff] [blame] | 707 | WARN_ON(pages[i]->mapping); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 708 | put_page(pages[i]); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 709 | } |
| 710 | kfree(pages); |
| 711 | pages = NULL; |
| 712 | total_compressed = 0; |
David Sterba | 4d3a800 | 2017-02-14 19:04:07 +0100 | [diff] [blame] | 713 | nr_pages = 0; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 714 | |
| 715 | /* flag the file so we don't compress in the future */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 716 | if (!btrfs_test_opt(fs_info, FORCE_COMPRESS) && |
David Sterba | b52aa8c | 2017-07-17 19:17:20 +0200 | [diff] [blame] | 717 | !(BTRFS_I(inode)->prop_compress)) { |
Chris Mason | a555f81 | 2010-01-28 16:18:15 -0500 | [diff] [blame] | 718 | BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS; |
Chris Mason | 1e701a3 | 2010-03-11 09:42:04 -0500 | [diff] [blame] | 719 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 720 | } |
Chris Mason | f03d9301f | 2009-02-04 09:31:06 -0500 | [diff] [blame] | 721 | cleanup_and_bail_uncompressed: |
Ashish Samant | c8bb0c8 | 2016-03-25 19:01:33 -0700 | [diff] [blame] | 722 | /* |
| 723 | * No compression, but we still need to write the pages in the file |
| 724 | * we've been given so far. redirty the locked page if it corresponds |
| 725 | * to our extent and set things up for the async work queue to run |
| 726 | * cow_file_range to do the normal delalloc dance. |
| 727 | */ |
Chris Mason | 1d53c9e | 2019-07-10 12:28:16 -0700 | [diff] [blame] | 728 | if (async_chunk->locked_page && |
| 729 | (page_offset(async_chunk->locked_page) >= start && |
| 730 | page_offset(async_chunk->locked_page)) <= end) { |
Nikolay Borisov | 1368c6d | 2019-03-12 17:20:27 +0200 | [diff] [blame] | 731 | __set_page_dirty_nobuffers(async_chunk->locked_page); |
Ashish Samant | c8bb0c8 | 2016-03-25 19:01:33 -0700 | [diff] [blame] | 732 | /* unlocked later on in the async handlers */ |
Chris Mason | 1d53c9e | 2019-07-10 12:28:16 -0700 | [diff] [blame] | 733 | } |
Ashish Samant | c8bb0c8 | 2016-03-25 19:01:33 -0700 | [diff] [blame] | 734 | |
| 735 | if (redirty) |
| 736 | extent_range_redirty_for_io(inode, start, end); |
Nikolay Borisov | b532627 | 2019-03-12 17:20:25 +0200 | [diff] [blame] | 737 | add_async_extent(async_chunk, start, end - start + 1, 0, NULL, 0, |
Ashish Samant | c8bb0c8 | 2016-03-25 19:01:33 -0700 | [diff] [blame] | 738 | BTRFS_COMPRESS_NONE); |
Nikolay Borisov | ac3e993 | 2019-07-17 14:41:44 +0300 | [diff] [blame] | 739 | compressed_extents++; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 740 | |
Nikolay Borisov | ac3e993 | 2019-07-17 14:41:44 +0300 | [diff] [blame] | 741 | return compressed_extents; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 742 | } |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 743 | |
Filipe Manana | 40ae837 | 2014-10-06 22:14:24 +0100 | [diff] [blame] | 744 | static void free_async_extent_pages(struct async_extent *async_extent) |
| 745 | { |
| 746 | int i; |
| 747 | |
| 748 | if (!async_extent->pages) |
| 749 | return; |
| 750 | |
| 751 | for (i = 0; i < async_extent->nr_pages; i++) { |
| 752 | WARN_ON(async_extent->pages[i]->mapping); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 753 | put_page(async_extent->pages[i]); |
Filipe Manana | 40ae837 | 2014-10-06 22:14:24 +0100 | [diff] [blame] | 754 | } |
| 755 | kfree(async_extent->pages); |
| 756 | async_extent->nr_pages = 0; |
| 757 | async_extent->pages = NULL; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 758 | } |
| 759 | |
| 760 | /* |
| 761 | * phase two of compressed writeback. This is the ordered portion |
| 762 | * of the code, which only gets called in the order the work was |
| 763 | * queued. We walk all the async extents created by compress_file_range |
| 764 | * and send them down to the disk. |
| 765 | */ |
Nikolay Borisov | b532627 | 2019-03-12 17:20:25 +0200 | [diff] [blame] | 766 | static noinline void submit_compressed_extents(struct async_chunk *async_chunk) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 767 | { |
Nikolay Borisov | a0ff10d | 2020-06-03 08:55:17 +0300 | [diff] [blame] | 768 | struct btrfs_inode *inode = BTRFS_I(async_chunk->inode); |
| 769 | struct btrfs_fs_info *fs_info = inode->root->fs_info; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 770 | struct async_extent *async_extent; |
| 771 | u64 alloc_hint = 0; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 772 | struct btrfs_key ins; |
| 773 | struct extent_map *em; |
Nikolay Borisov | a0ff10d | 2020-06-03 08:55:17 +0300 | [diff] [blame] | 774 | struct btrfs_root *root = inode->root; |
| 775 | struct extent_io_tree *io_tree = &inode->io_tree; |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 776 | int ret = 0; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 777 | |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 778 | again: |
Nikolay Borisov | b532627 | 2019-03-12 17:20:25 +0200 | [diff] [blame] | 779 | while (!list_empty(&async_chunk->extents)) { |
| 780 | async_extent = list_entry(async_chunk->extents.next, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 781 | struct async_extent, list); |
| 782 | list_del(&async_extent->list); |
| 783 | |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 784 | retry: |
Nikolay Borisov | 7447555 | 2019-03-12 17:20:30 +0200 | [diff] [blame] | 785 | lock_extent(io_tree, async_extent->start, |
| 786 | async_extent->start + async_extent->ram_size - 1); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 787 | /* did the compression code fall back to uncompressed IO? */ |
| 788 | if (!async_extent->pages) { |
| 789 | int page_started = 0; |
| 790 | unsigned long nr_written = 0; |
| 791 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 792 | /* allocate blocks */ |
Nikolay Borisov | a0ff10d | 2020-06-03 08:55:17 +0300 | [diff] [blame] | 793 | ret = cow_file_range(inode, async_chunk->locked_page, |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 794 | async_extent->start, |
| 795 | async_extent->start + |
| 796 | async_extent->ram_size - 1, |
Nikolay Borisov | 330a582 | 2019-07-17 16:18:17 +0300 | [diff] [blame] | 797 | &page_started, &nr_written, 0); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 798 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 799 | /* JDM XXX */ |
| 800 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 801 | /* |
| 802 | * if page_started, cow_file_range inserted an |
| 803 | * inline extent and took care of all the unlocking |
| 804 | * and IO for us. Otherwise, we need to submit |
| 805 | * all those pages down to the drive. |
| 806 | */ |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 807 | if (!page_started && !ret) |
Nikolay Borisov | a0ff10d | 2020-06-03 08:55:17 +0300 | [diff] [blame] | 808 | extent_write_locked_range(&inode->vfs_inode, |
Nikolay Borisov | 5e3ee23 | 2017-12-08 15:55:58 +0200 | [diff] [blame] | 809 | async_extent->start, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 810 | async_extent->start + |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 811 | async_extent->ram_size - 1, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 812 | WB_SYNC_ALL); |
Chris Mason | 1d53c9e | 2019-07-10 12:28:16 -0700 | [diff] [blame] | 813 | else if (ret && async_chunk->locked_page) |
Nikolay Borisov | b532627 | 2019-03-12 17:20:25 +0200 | [diff] [blame] | 814 | unlock_page(async_chunk->locked_page); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 815 | kfree(async_extent); |
| 816 | cond_resched(); |
| 817 | continue; |
| 818 | } |
| 819 | |
Wang Xiaoguang | 1851309 | 2016-07-25 15:51:40 +0800 | [diff] [blame] | 820 | ret = btrfs_reserve_extent(root, async_extent->ram_size, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 821 | async_extent->compressed_size, |
| 822 | async_extent->compressed_size, |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 823 | 0, alloc_hint, &ins, 1, 1); |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 824 | if (ret) { |
Filipe Manana | 40ae837 | 2014-10-06 22:14:24 +0100 | [diff] [blame] | 825 | free_async_extent_pages(async_extent); |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 826 | |
Josef Bacik | fdf8e2e | 2013-06-14 16:58:23 -0400 | [diff] [blame] | 827 | if (ret == -ENOSPC) { |
| 828 | unlock_extent(io_tree, async_extent->start, |
| 829 | async_extent->start + |
| 830 | async_extent->ram_size - 1); |
Liu Bo | ce62003 | 2014-07-24 22:48:05 +0800 | [diff] [blame] | 831 | |
| 832 | /* |
| 833 | * we need to redirty the pages if we decide to |
| 834 | * fallback to uncompressed IO, otherwise we |
| 835 | * will not submit these pages down to lower |
| 836 | * layers. |
| 837 | */ |
Nikolay Borisov | a0ff10d | 2020-06-03 08:55:17 +0300 | [diff] [blame] | 838 | extent_range_redirty_for_io(&inode->vfs_inode, |
Liu Bo | ce62003 | 2014-07-24 22:48:05 +0800 | [diff] [blame] | 839 | async_extent->start, |
| 840 | async_extent->start + |
| 841 | async_extent->ram_size - 1); |
| 842 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 843 | goto retry; |
Josef Bacik | fdf8e2e | 2013-06-14 16:58:23 -0400 | [diff] [blame] | 844 | } |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 845 | goto out_free; |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 846 | } |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 847 | /* |
| 848 | * here we're doing allocation and writeback of the |
| 849 | * compressed pages |
| 850 | */ |
Nikolay Borisov | a0ff10d | 2020-06-03 08:55:17 +0300 | [diff] [blame] | 851 | em = create_io_em(inode, async_extent->start, |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 852 | async_extent->ram_size, /* len */ |
| 853 | async_extent->start, /* orig_start */ |
| 854 | ins.objectid, /* block_start */ |
| 855 | ins.offset, /* block_len */ |
| 856 | ins.offset, /* orig_block_len */ |
| 857 | async_extent->ram_size, /* ram_bytes */ |
| 858 | async_extent->compress_type, |
| 859 | BTRFS_ORDERED_COMPRESSED); |
| 860 | if (IS_ERR(em)) |
| 861 | /* ret value is not necessary due to void function */ |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 862 | goto out_free_reserve; |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 863 | free_extent_map(em); |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 864 | |
Nikolay Borisov | a0ff10d | 2020-06-03 08:55:17 +0300 | [diff] [blame] | 865 | ret = btrfs_add_ordered_extent_compress(inode, |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 866 | async_extent->start, |
| 867 | ins.objectid, |
| 868 | async_extent->ram_size, |
| 869 | ins.offset, |
| 870 | BTRFS_ORDERED_COMPRESSED, |
| 871 | async_extent->compress_type); |
Filipe Manana | d9f8596 | 2014-08-25 10:43:00 +0100 | [diff] [blame] | 872 | if (ret) { |
Nikolay Borisov | a0ff10d | 2020-06-03 08:55:17 +0300 | [diff] [blame] | 873 | btrfs_drop_extent_cache(inode, async_extent->start, |
Filipe Manana | d9f8596 | 2014-08-25 10:43:00 +0100 | [diff] [blame] | 874 | async_extent->start + |
| 875 | async_extent->ram_size - 1, 0); |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 876 | goto out_free_reserve; |
Filipe Manana | d9f8596 | 2014-08-25 10:43:00 +0100 | [diff] [blame] | 877 | } |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 878 | btrfs_dec_block_group_reservations(fs_info, ins.objectid); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 879 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 880 | /* |
| 881 | * clear dirty, set writeback and unlock the pages. |
| 882 | */ |
Nikolay Borisov | a0ff10d | 2020-06-03 08:55:17 +0300 | [diff] [blame] | 883 | extent_clear_unlock_delalloc(inode, async_extent->start, |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 884 | async_extent->start + |
| 885 | async_extent->ram_size - 1, |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 886 | NULL, EXTENT_LOCKED | EXTENT_DELALLOC, |
| 887 | PAGE_UNLOCK | PAGE_CLEAR_DIRTY | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 888 | PAGE_SET_WRITEBACK); |
Nikolay Borisov | a0ff10d | 2020-06-03 08:55:17 +0300 | [diff] [blame] | 889 | if (btrfs_submit_compressed_write(inode, async_extent->start, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 890 | async_extent->ram_size, |
| 891 | ins.objectid, |
| 892 | ins.offset, async_extent->pages, |
Liu Bo | f82b735 | 2017-10-23 23:18:16 -0600 | [diff] [blame] | 893 | async_extent->nr_pages, |
Chris Mason | ec39f76 | 2019-07-10 12:28:17 -0700 | [diff] [blame] | 894 | async_chunk->write_flags, |
| 895 | async_chunk->blkcg_css)) { |
Filipe Manana | fce2a4e | 2014-10-06 22:14:23 +0100 | [diff] [blame] | 896 | struct page *p = async_extent->pages[0]; |
| 897 | const u64 start = async_extent->start; |
| 898 | const u64 end = start + async_extent->ram_size - 1; |
| 899 | |
Nikolay Borisov | a0ff10d | 2020-06-03 08:55:17 +0300 | [diff] [blame] | 900 | p->mapping = inode->vfs_inode.i_mapping; |
Nikolay Borisov | c629732 | 2018-11-08 10:18:08 +0200 | [diff] [blame] | 901 | btrfs_writepage_endio_finish_ordered(p, start, end, 0); |
Nikolay Borisov | 7087a9d | 2018-11-01 14:09:48 +0200 | [diff] [blame] | 902 | |
Filipe Manana | fce2a4e | 2014-10-06 22:14:23 +0100 | [diff] [blame] | 903 | p->mapping = NULL; |
Nikolay Borisov | a0ff10d | 2020-06-03 08:55:17 +0300 | [diff] [blame] | 904 | extent_clear_unlock_delalloc(inode, start, end, NULL, 0, |
Filipe Manana | fce2a4e | 2014-10-06 22:14:23 +0100 | [diff] [blame] | 905 | PAGE_END_WRITEBACK | |
| 906 | PAGE_SET_ERROR); |
Filipe Manana | 40ae837 | 2014-10-06 22:14:24 +0100 | [diff] [blame] | 907 | free_async_extent_pages(async_extent); |
Filipe Manana | fce2a4e | 2014-10-06 22:14:23 +0100 | [diff] [blame] | 908 | } |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 909 | alloc_hint = ins.objectid + ins.offset; |
| 910 | kfree(async_extent); |
| 911 | cond_resched(); |
| 912 | } |
Filipe Manana | dec8f17 | 2014-10-06 22:14:26 +0100 | [diff] [blame] | 913 | return; |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 914 | out_free_reserve: |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 915 | btrfs_dec_block_group_reservations(fs_info, ins.objectid); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 916 | btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 917 | out_free: |
Nikolay Borisov | a0ff10d | 2020-06-03 08:55:17 +0300 | [diff] [blame] | 918 | extent_clear_unlock_delalloc(inode, async_extent->start, |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 919 | async_extent->start + |
| 920 | async_extent->ram_size - 1, |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 921 | NULL, EXTENT_LOCKED | EXTENT_DELALLOC | |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 922 | EXTENT_DELALLOC_NEW | |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 923 | EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING, |
| 924 | PAGE_UNLOCK | PAGE_CLEAR_DIRTY | |
Filipe Manana | 704de49 | 2014-10-06 22:14:22 +0100 | [diff] [blame] | 925 | PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK | |
| 926 | PAGE_SET_ERROR); |
Filipe Manana | 40ae837 | 2014-10-06 22:14:24 +0100 | [diff] [blame] | 927 | free_async_extent_pages(async_extent); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 928 | kfree(async_extent); |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 929 | goto again; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 930 | } |
| 931 | |
Nikolay Borisov | 43c6984 | 2020-06-03 08:55:02 +0300 | [diff] [blame] | 932 | static u64 get_extent_allocation_hint(struct btrfs_inode *inode, u64 start, |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 933 | u64 num_bytes) |
| 934 | { |
Nikolay Borisov | 43c6984 | 2020-06-03 08:55:02 +0300 | [diff] [blame] | 935 | struct extent_map_tree *em_tree = &inode->extent_tree; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 936 | struct extent_map *em; |
| 937 | u64 alloc_hint = 0; |
| 938 | |
| 939 | read_lock(&em_tree->lock); |
| 940 | em = search_extent_mapping(em_tree, start, num_bytes); |
| 941 | if (em) { |
| 942 | /* |
| 943 | * if block start isn't an actual block number then find the |
| 944 | * first block in this inode and use that as a hint. If that |
| 945 | * block is also bogus then just don't worry about it. |
| 946 | */ |
| 947 | if (em->block_start >= EXTENT_MAP_LAST_BYTE) { |
| 948 | free_extent_map(em); |
| 949 | em = search_extent_mapping(em_tree, 0, 0); |
| 950 | if (em && em->block_start < EXTENT_MAP_LAST_BYTE) |
| 951 | alloc_hint = em->block_start; |
| 952 | if (em) |
| 953 | free_extent_map(em); |
| 954 | } else { |
| 955 | alloc_hint = em->block_start; |
| 956 | free_extent_map(em); |
| 957 | } |
| 958 | } |
| 959 | read_unlock(&em_tree->lock); |
| 960 | |
| 961 | return alloc_hint; |
| 962 | } |
| 963 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 964 | /* |
| 965 | * when extent_io.c finds a delayed allocation range in the file, |
| 966 | * the call backs end up in this code. The basic idea is to |
| 967 | * allocate extents on disk for the range, and create ordered data structs |
| 968 | * in ram to track those extents. |
| 969 | * |
| 970 | * locked_page is the page that writepage had locked already. We use |
| 971 | * it to make sure we don't do extra locks or unlocks. |
| 972 | * |
| 973 | * *page_started is set to one if we unlock locked_page and do everything |
| 974 | * required to start IO on it. It may be clean and already done with |
| 975 | * IO when we return. |
| 976 | */ |
Nikolay Borisov | 6e26c44 | 2020-06-03 08:55:14 +0300 | [diff] [blame] | 977 | static noinline int cow_file_range(struct btrfs_inode *inode, |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 978 | struct page *locked_page, |
Nikolay Borisov | 74e9194 | 2019-07-17 16:18:16 +0300 | [diff] [blame] | 979 | u64 start, u64 end, int *page_started, |
Nikolay Borisov | 330a582 | 2019-07-17 16:18:17 +0300 | [diff] [blame] | 980 | unsigned long *nr_written, int unlock) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 981 | { |
Nikolay Borisov | 6e26c44 | 2020-06-03 08:55:14 +0300 | [diff] [blame] | 982 | struct btrfs_root *root = inode->root; |
| 983 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 984 | u64 alloc_hint = 0; |
| 985 | u64 num_bytes; |
| 986 | unsigned long ram_size; |
Filipe Manana | a315e68 | 2017-03-06 23:04:20 +0000 | [diff] [blame] | 987 | u64 cur_alloc_size = 0; |
Filipe Manana | 432cd2a | 2020-06-08 13:32:55 +0100 | [diff] [blame] | 988 | u64 min_alloc_size; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 989 | u64 blocksize = fs_info->sectorsize; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 990 | struct btrfs_key ins; |
| 991 | struct extent_map *em; |
Filipe Manana | a315e68 | 2017-03-06 23:04:20 +0000 | [diff] [blame] | 992 | unsigned clear_bits; |
| 993 | unsigned long page_ops; |
| 994 | bool extent_reserved = false; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 995 | int ret = 0; |
| 996 | |
Nikolay Borisov | 6e26c44 | 2020-06-03 08:55:14 +0300 | [diff] [blame] | 997 | if (btrfs_is_free_space_inode(inode)) { |
Josef Bacik | 29bce2f | 2014-02-07 12:21:23 -0500 | [diff] [blame] | 998 | ret = -EINVAL; |
| 999 | goto out_unlock; |
Josef Bacik | 02ecd2c | 2013-10-25 16:19:08 -0400 | [diff] [blame] | 1000 | } |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1001 | |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 1002 | num_bytes = ALIGN(end - start + 1, blocksize); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1003 | num_bytes = max(blocksize, num_bytes); |
Anand Jain | 566b176 | 2018-02-15 18:07:59 +0800 | [diff] [blame] | 1004 | ASSERT(num_bytes <= btrfs_super_total_bytes(fs_info->super_copy)); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1005 | |
Nikolay Borisov | 6e26c44 | 2020-06-03 08:55:14 +0300 | [diff] [blame] | 1006 | inode_should_defrag(inode, start, end, num_bytes, SZ_64K); |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1007 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1008 | if (start == 0) { |
| 1009 | /* lets try to make an inline extent */ |
Nikolay Borisov | 6e26c44 | 2020-06-03 08:55:14 +0300 | [diff] [blame] | 1010 | ret = cow_file_range_inline(inode, start, end, 0, |
Nikolay Borisov | d02c0e2 | 2018-03-02 09:43:15 +0200 | [diff] [blame] | 1011 | BTRFS_COMPRESS_NONE, NULL); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1012 | if (ret == 0) { |
Josef Bacik | 8b62f87 | 2017-10-19 14:15:55 -0400 | [diff] [blame] | 1013 | /* |
| 1014 | * We use DO_ACCOUNTING here because we need the |
| 1015 | * delalloc_release_metadata to be run _after_ we drop |
| 1016 | * our outstanding extent for clearing delalloc for this |
| 1017 | * range. |
| 1018 | */ |
Nikolay Borisov | 6e26c44 | 2020-06-03 08:55:14 +0300 | [diff] [blame] | 1019 | extent_clear_unlock_delalloc(inode, start, end, NULL, |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1020 | EXTENT_LOCKED | EXTENT_DELALLOC | |
Josef Bacik | 8b62f87 | 2017-10-19 14:15:55 -0400 | [diff] [blame] | 1021 | EXTENT_DELALLOC_NEW | EXTENT_DEFRAG | |
| 1022 | EXTENT_DO_ACCOUNTING, PAGE_UNLOCK | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1023 | PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK | |
| 1024 | PAGE_END_WRITEBACK); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1025 | *nr_written = *nr_written + |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1026 | (end - start + PAGE_SIZE) / PAGE_SIZE; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1027 | *page_started = 1; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1028 | goto out; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1029 | } else if (ret < 0) { |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1030 | goto out_unlock; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1031 | } |
| 1032 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1033 | |
Nikolay Borisov | 6e26c44 | 2020-06-03 08:55:14 +0300 | [diff] [blame] | 1034 | alloc_hint = get_extent_allocation_hint(inode, start, num_bytes); |
| 1035 | btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 1036 | |
Filipe Manana | 432cd2a | 2020-06-08 13:32:55 +0100 | [diff] [blame] | 1037 | /* |
| 1038 | * Relocation relies on the relocated extents to have exactly the same |
| 1039 | * size as the original extents. Normally writeback for relocation data |
| 1040 | * extents follows a NOCOW path because relocation preallocates the |
| 1041 | * extents. However, due to an operation such as scrub turning a block |
| 1042 | * group to RO mode, it may fallback to COW mode, so we must make sure |
| 1043 | * an extent allocated during COW has exactly the requested size and can |
| 1044 | * not be split into smaller extents, otherwise relocation breaks and |
| 1045 | * fails during the stage where it updates the bytenr of file extent |
| 1046 | * items. |
| 1047 | */ |
| 1048 | if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID) |
| 1049 | min_alloc_size = num_bytes; |
| 1050 | else |
| 1051 | min_alloc_size = fs_info->sectorsize; |
| 1052 | |
Anand Jain | 3752d22 | 2018-02-15 12:29:38 +0800 | [diff] [blame] | 1053 | while (num_bytes > 0) { |
| 1054 | cur_alloc_size = num_bytes; |
Wang Xiaoguang | 1851309 | 2016-07-25 15:51:40 +0800 | [diff] [blame] | 1055 | ret = btrfs_reserve_extent(root, cur_alloc_size, cur_alloc_size, |
Filipe Manana | 432cd2a | 2020-06-08 13:32:55 +0100 | [diff] [blame] | 1056 | min_alloc_size, 0, alloc_hint, |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 1057 | &ins, 1, 1); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 1058 | if (ret < 0) |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1059 | goto out_unlock; |
Filipe Manana | a315e68 | 2017-03-06 23:04:20 +0000 | [diff] [blame] | 1060 | cur_alloc_size = ins.offset; |
| 1061 | extent_reserved = true; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1062 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1063 | ram_size = ins.offset; |
Nikolay Borisov | 6e26c44 | 2020-06-03 08:55:14 +0300 | [diff] [blame] | 1064 | em = create_io_em(inode, start, ins.offset, /* len */ |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 1065 | start, /* orig_start */ |
| 1066 | ins.objectid, /* block_start */ |
| 1067 | ins.offset, /* block_len */ |
| 1068 | ins.offset, /* orig_block_len */ |
| 1069 | ram_size, /* ram_bytes */ |
| 1070 | BTRFS_COMPRESS_NONE, /* compress_type */ |
Liu Bo | 1af4a0a | 2017-02-13 15:35:09 -0800 | [diff] [blame] | 1071 | BTRFS_ORDERED_REGULAR /* type */); |
Su Yue | 090a127a | 2018-05-30 16:48:56 +0800 | [diff] [blame] | 1072 | if (IS_ERR(em)) { |
| 1073 | ret = PTR_ERR(em); |
Liu Bo | ace68ba | 2013-04-22 10:53:47 +0000 | [diff] [blame] | 1074 | goto out_reserve; |
Su Yue | 090a127a | 2018-05-30 16:48:56 +0800 | [diff] [blame] | 1075 | } |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 1076 | free_extent_map(em); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1077 | |
Nikolay Borisov | 6e26c44 | 2020-06-03 08:55:14 +0300 | [diff] [blame] | 1078 | ret = btrfs_add_ordered_extent(inode, start, ins.objectid, |
| 1079 | ram_size, cur_alloc_size, 0); |
Liu Bo | ace68ba | 2013-04-22 10:53:47 +0000 | [diff] [blame] | 1080 | if (ret) |
Filipe Manana | d9f8596 | 2014-08-25 10:43:00 +0100 | [diff] [blame] | 1081 | goto out_drop_extent_cache; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1082 | |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1083 | if (root->root_key.objectid == |
| 1084 | BTRFS_DATA_RELOC_TREE_OBJECTID) { |
Nikolay Borisov | 6e26c44 | 2020-06-03 08:55:14 +0300 | [diff] [blame] | 1085 | ret = btrfs_reloc_clone_csums(inode, start, |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1086 | cur_alloc_size); |
Qu Wenruo | 4dbd80f | 2017-03-08 10:25:51 +0800 | [diff] [blame] | 1087 | /* |
| 1088 | * Only drop cache here, and process as normal. |
| 1089 | * |
| 1090 | * We must not allow extent_clear_unlock_delalloc() |
| 1091 | * at out_unlock label to free meta of this ordered |
| 1092 | * extent, as its meta should be freed by |
| 1093 | * btrfs_finish_ordered_io(). |
| 1094 | * |
| 1095 | * So we must continue until @start is increased to |
| 1096 | * skip current ordered extent. |
| 1097 | */ |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 1098 | if (ret) |
Nikolay Borisov | 6e26c44 | 2020-06-03 08:55:14 +0300 | [diff] [blame] | 1099 | btrfs_drop_extent_cache(inode, start, |
Qu Wenruo | 4dbd80f | 2017-03-08 10:25:51 +0800 | [diff] [blame] | 1100 | start + ram_size - 1, 0); |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1101 | } |
| 1102 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1103 | btrfs_dec_block_group_reservations(fs_info, ins.objectid); |
Filipe Manana | 9cfa3e3 | 2016-04-26 15:39:32 +0100 | [diff] [blame] | 1104 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1105 | /* we're not doing compressed IO, don't unlock the first |
| 1106 | * page (which the caller expects to stay locked), don't |
| 1107 | * clear any dirty bits and don't set any writeback bits |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 1108 | * |
| 1109 | * Do set the Private2 bit so we know this page was properly |
| 1110 | * setup for writepage |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1111 | */ |
Filipe Manana | a315e68 | 2017-03-06 23:04:20 +0000 | [diff] [blame] | 1112 | page_ops = unlock ? PAGE_UNLOCK : 0; |
| 1113 | page_ops |= PAGE_SET_PRIVATE2; |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 1114 | |
Nikolay Borisov | 6e26c44 | 2020-06-03 08:55:14 +0300 | [diff] [blame] | 1115 | extent_clear_unlock_delalloc(inode, start, start + ram_size - 1, |
Nikolay Borisov | 74e9194 | 2019-07-17 16:18:16 +0300 | [diff] [blame] | 1116 | locked_page, |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1117 | EXTENT_LOCKED | EXTENT_DELALLOC, |
Filipe Manana | a315e68 | 2017-03-06 23:04:20 +0000 | [diff] [blame] | 1118 | page_ops); |
Anand Jain | 3752d22 | 2018-02-15 12:29:38 +0800 | [diff] [blame] | 1119 | if (num_bytes < cur_alloc_size) |
| 1120 | num_bytes = 0; |
Qu Wenruo | 4dbd80f | 2017-03-08 10:25:51 +0800 | [diff] [blame] | 1121 | else |
Anand Jain | 3752d22 | 2018-02-15 12:29:38 +0800 | [diff] [blame] | 1122 | num_bytes -= cur_alloc_size; |
Chris Mason | c59f895 | 2007-12-17 20:14:04 -0500 | [diff] [blame] | 1123 | alloc_hint = ins.objectid + ins.offset; |
| 1124 | start += cur_alloc_size; |
Filipe Manana | a315e68 | 2017-03-06 23:04:20 +0000 | [diff] [blame] | 1125 | extent_reserved = false; |
Qu Wenruo | 4dbd80f | 2017-03-08 10:25:51 +0800 | [diff] [blame] | 1126 | |
| 1127 | /* |
| 1128 | * btrfs_reloc_clone_csums() error, since start is increased |
| 1129 | * extent_clear_unlock_delalloc() at out_unlock label won't |
| 1130 | * free metadata of current ordered extent, we're OK to exit. |
| 1131 | */ |
| 1132 | if (ret) |
| 1133 | goto out_unlock; |
Chris Mason | b888db2b | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 1134 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1135 | out: |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1136 | return ret; |
Miao Xie | b7d5b0a | 2012-11-01 07:32:18 +0000 | [diff] [blame] | 1137 | |
Filipe Manana | d9f8596 | 2014-08-25 10:43:00 +0100 | [diff] [blame] | 1138 | out_drop_extent_cache: |
Nikolay Borisov | 6e26c44 | 2020-06-03 08:55:14 +0300 | [diff] [blame] | 1139 | btrfs_drop_extent_cache(inode, start, start + ram_size - 1, 0); |
Liu Bo | ace68ba | 2013-04-22 10:53:47 +0000 | [diff] [blame] | 1140 | out_reserve: |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1141 | btrfs_dec_block_group_reservations(fs_info, ins.objectid); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 1142 | btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1143 | out_unlock: |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 1144 | clear_bits = EXTENT_LOCKED | EXTENT_DELALLOC | EXTENT_DELALLOC_NEW | |
| 1145 | EXTENT_DEFRAG | EXTENT_CLEAR_META_RESV; |
Filipe Manana | a315e68 | 2017-03-06 23:04:20 +0000 | [diff] [blame] | 1146 | page_ops = PAGE_UNLOCK | PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK | |
| 1147 | PAGE_END_WRITEBACK; |
| 1148 | /* |
| 1149 | * If we reserved an extent for our delalloc range (or a subrange) and |
| 1150 | * failed to create the respective ordered extent, then it means that |
| 1151 | * when we reserved the extent we decremented the extent's size from |
| 1152 | * the data space_info's bytes_may_use counter and incremented the |
| 1153 | * space_info's bytes_reserved counter by the same amount. We must make |
| 1154 | * sure extent_clear_unlock_delalloc() does not try to decrement again |
| 1155 | * the data space_info's bytes_may_use counter, therefore we do not pass |
| 1156 | * it the flag EXTENT_CLEAR_DATA_RESV. |
| 1157 | */ |
| 1158 | if (extent_reserved) { |
Nikolay Borisov | 6e26c44 | 2020-06-03 08:55:14 +0300 | [diff] [blame] | 1159 | extent_clear_unlock_delalloc(inode, start, |
Filipe Manana | e2c8e92 | 2020-05-27 11:15:53 +0100 | [diff] [blame] | 1160 | start + cur_alloc_size - 1, |
Filipe Manana | a315e68 | 2017-03-06 23:04:20 +0000 | [diff] [blame] | 1161 | locked_page, |
| 1162 | clear_bits, |
| 1163 | page_ops); |
| 1164 | start += cur_alloc_size; |
| 1165 | if (start >= end) |
| 1166 | goto out; |
| 1167 | } |
Nikolay Borisov | 6e26c44 | 2020-06-03 08:55:14 +0300 | [diff] [blame] | 1168 | extent_clear_unlock_delalloc(inode, start, end, locked_page, |
Filipe Manana | a315e68 | 2017-03-06 23:04:20 +0000 | [diff] [blame] | 1169 | clear_bits | EXTENT_CLEAR_DATA_RESV, |
| 1170 | page_ops); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1171 | goto out; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1172 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1173 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1174 | /* |
| 1175 | * work queue call back to started compression on a file and pages |
| 1176 | */ |
| 1177 | static noinline void async_cow_start(struct btrfs_work *work) |
| 1178 | { |
Nikolay Borisov | b532627 | 2019-03-12 17:20:25 +0200 | [diff] [blame] | 1179 | struct async_chunk *async_chunk; |
Nikolay Borisov | ac3e993 | 2019-07-17 14:41:44 +0300 | [diff] [blame] | 1180 | int compressed_extents; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1181 | |
Nikolay Borisov | b532627 | 2019-03-12 17:20:25 +0200 | [diff] [blame] | 1182 | async_chunk = container_of(work, struct async_chunk, work); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1183 | |
Nikolay Borisov | ac3e993 | 2019-07-17 14:41:44 +0300 | [diff] [blame] | 1184 | compressed_extents = compress_file_range(async_chunk); |
| 1185 | if (compressed_extents == 0) { |
Nikolay Borisov | b532627 | 2019-03-12 17:20:25 +0200 | [diff] [blame] | 1186 | btrfs_add_delayed_iput(async_chunk->inode); |
| 1187 | async_chunk->inode = NULL; |
Josef Bacik | 8180ef8 | 2012-06-08 15:16:12 -0400 | [diff] [blame] | 1188 | } |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1189 | } |
| 1190 | |
| 1191 | /* |
| 1192 | * work queue call back to submit previously compressed pages |
| 1193 | */ |
| 1194 | static noinline void async_cow_submit(struct btrfs_work *work) |
| 1195 | { |
Nikolay Borisov | c5a68ae | 2019-03-12 17:20:26 +0200 | [diff] [blame] | 1196 | struct async_chunk *async_chunk = container_of(work, struct async_chunk, |
| 1197 | work); |
| 1198 | struct btrfs_fs_info *fs_info = btrfs_work_owner(work); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1199 | unsigned long nr_pages; |
| 1200 | |
Nikolay Borisov | b532627 | 2019-03-12 17:20:25 +0200 | [diff] [blame] | 1201 | nr_pages = (async_chunk->end - async_chunk->start + PAGE_SIZE) >> |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1202 | PAGE_SHIFT; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1203 | |
Nikolay Borisov | 4546d17 | 2019-01-03 10:50:03 +0200 | [diff] [blame] | 1204 | /* |
Nikolay Borisov | b532627 | 2019-03-12 17:20:25 +0200 | [diff] [blame] | 1205 | * ->inode could be NULL if async_chunk_start has failed to compress, |
Nikolay Borisov | 4546d17 | 2019-01-03 10:50:03 +0200 | [diff] [blame] | 1206 | * in which case we don't have anything to submit, yet we need to |
| 1207 | * always adjust ->async_delalloc_pages as its paired with the init |
| 1208 | * happening in cow_file_range_async |
| 1209 | */ |
Nikolay Borisov | b532627 | 2019-03-12 17:20:25 +0200 | [diff] [blame] | 1210 | if (async_chunk->inode) |
| 1211 | submit_compressed_extents(async_chunk); |
Josef Bacik | 0901af5 | 2021-07-14 14:47:17 -0400 | [diff] [blame] | 1212 | |
| 1213 | /* atomic_sub_return implies a barrier */ |
| 1214 | if (atomic_sub_return(nr_pages, &fs_info->async_delalloc_pages) < |
| 1215 | 5 * SZ_1M) |
| 1216 | cond_wake_up_nomb(&fs_info->async_submit_wait); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1217 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1218 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1219 | static noinline void async_cow_free(struct btrfs_work *work) |
| 1220 | { |
Nikolay Borisov | b532627 | 2019-03-12 17:20:25 +0200 | [diff] [blame] | 1221 | struct async_chunk *async_chunk; |
Nikolay Borisov | 97db120 | 2019-03-12 17:20:24 +0200 | [diff] [blame] | 1222 | |
Nikolay Borisov | b532627 | 2019-03-12 17:20:25 +0200 | [diff] [blame] | 1223 | async_chunk = container_of(work, struct async_chunk, work); |
| 1224 | if (async_chunk->inode) |
| 1225 | btrfs_add_delayed_iput(async_chunk->inode); |
Chris Mason | ec39f76 | 2019-07-10 12:28:17 -0700 | [diff] [blame] | 1226 | if (async_chunk->blkcg_css) |
| 1227 | css_put(async_chunk->blkcg_css); |
Nikolay Borisov | 97db120 | 2019-03-12 17:20:24 +0200 | [diff] [blame] | 1228 | /* |
| 1229 | * 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] | 1230 | * async_chunk's, freeing it ensures the whole array has been freed. |
Nikolay Borisov | 97db120 | 2019-03-12 17:20:24 +0200 | [diff] [blame] | 1231 | */ |
Nikolay Borisov | b532627 | 2019-03-12 17:20:25 +0200 | [diff] [blame] | 1232 | if (atomic_dec_and_test(async_chunk->pending)) |
Nikolay Borisov | b1c16ac | 2019-04-01 11:29:57 +0300 | [diff] [blame] | 1233 | kvfree(async_chunk->pending); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1234 | } |
| 1235 | |
Nikolay Borisov | 751b643 | 2020-06-03 08:55:22 +0300 | [diff] [blame] | 1236 | static int cow_file_range_async(struct btrfs_inode *inode, |
Chris Mason | ec39f76 | 2019-07-10 12:28:17 -0700 | [diff] [blame] | 1237 | struct writeback_control *wbc, |
| 1238 | struct page *locked_page, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1239 | u64 start, u64 end, int *page_started, |
David Sterba | fac07d2 | 2019-10-29 18:28:57 +0100 | [diff] [blame] | 1240 | unsigned long *nr_written) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1241 | { |
Nikolay Borisov | 751b643 | 2020-06-03 08:55:22 +0300 | [diff] [blame] | 1242 | struct btrfs_fs_info *fs_info = inode->root->fs_info; |
Chris Mason | ec39f76 | 2019-07-10 12:28:17 -0700 | [diff] [blame] | 1243 | struct cgroup_subsys_state *blkcg_css = wbc_blkcg_css(wbc); |
Nikolay Borisov | 97db120 | 2019-03-12 17:20:24 +0200 | [diff] [blame] | 1244 | struct async_cow *ctx; |
| 1245 | struct async_chunk *async_chunk; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1246 | unsigned long nr_pages; |
| 1247 | u64 cur_end; |
Nikolay Borisov | 97db120 | 2019-03-12 17:20:24 +0200 | [diff] [blame] | 1248 | u64 num_chunks = DIV_ROUND_UP(end - start, SZ_512K); |
| 1249 | int i; |
| 1250 | bool should_compress; |
Nikolay Borisov | b1c16ac | 2019-04-01 11:29:57 +0300 | [diff] [blame] | 1251 | unsigned nofs_flag; |
David Sterba | fac07d2 | 2019-10-29 18:28:57 +0100 | [diff] [blame] | 1252 | const unsigned int write_flags = wbc_to_write_flags(wbc); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1253 | |
Nikolay Borisov | 751b643 | 2020-06-03 08:55:22 +0300 | [diff] [blame] | 1254 | unlock_extent(&inode->io_tree, start, end); |
Nikolay Borisov | 97db120 | 2019-03-12 17:20:24 +0200 | [diff] [blame] | 1255 | |
Nikolay Borisov | 751b643 | 2020-06-03 08:55:22 +0300 | [diff] [blame] | 1256 | if (inode->flags & BTRFS_INODE_NOCOMPRESS && |
Nikolay Borisov | 97db120 | 2019-03-12 17:20:24 +0200 | [diff] [blame] | 1257 | !btrfs_test_opt(fs_info, FORCE_COMPRESS)) { |
| 1258 | num_chunks = 1; |
| 1259 | should_compress = false; |
| 1260 | } else { |
| 1261 | should_compress = true; |
| 1262 | } |
| 1263 | |
Nikolay Borisov | b1c16ac | 2019-04-01 11:29:57 +0300 | [diff] [blame] | 1264 | nofs_flag = memalloc_nofs_save(); |
| 1265 | ctx = kvmalloc(struct_size(ctx, chunks, num_chunks), GFP_KERNEL); |
| 1266 | memalloc_nofs_restore(nofs_flag); |
| 1267 | |
Nikolay Borisov | 97db120 | 2019-03-12 17:20:24 +0200 | [diff] [blame] | 1268 | if (!ctx) { |
| 1269 | unsigned clear_bits = EXTENT_LOCKED | EXTENT_DELALLOC | |
| 1270 | EXTENT_DELALLOC_NEW | EXTENT_DEFRAG | |
| 1271 | EXTENT_DO_ACCOUNTING; |
| 1272 | unsigned long page_ops = PAGE_UNLOCK | PAGE_CLEAR_DIRTY | |
| 1273 | PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK | |
| 1274 | PAGE_SET_ERROR; |
| 1275 | |
Nikolay Borisov | 751b643 | 2020-06-03 08:55:22 +0300 | [diff] [blame] | 1276 | extent_clear_unlock_delalloc(inode, start, end, locked_page, |
| 1277 | clear_bits, page_ops); |
Nikolay Borisov | 97db120 | 2019-03-12 17:20:24 +0200 | [diff] [blame] | 1278 | return -ENOMEM; |
| 1279 | } |
| 1280 | |
| 1281 | async_chunk = ctx->chunks; |
| 1282 | atomic_set(&ctx->num_chunks, num_chunks); |
| 1283 | |
| 1284 | for (i = 0; i < num_chunks; i++) { |
| 1285 | if (should_compress) |
| 1286 | cur_end = min(end, start + SZ_512K - 1); |
| 1287 | else |
| 1288 | cur_end = end; |
| 1289 | |
Nikolay Borisov | bd4691a | 2019-01-03 10:50:01 +0200 | [diff] [blame] | 1290 | /* |
| 1291 | * igrab is called higher up in the call chain, take only the |
| 1292 | * lightweight reference for the callback lifetime |
| 1293 | */ |
Nikolay Borisov | 751b643 | 2020-06-03 08:55:22 +0300 | [diff] [blame] | 1294 | ihold(&inode->vfs_inode); |
Nikolay Borisov | 97db120 | 2019-03-12 17:20:24 +0200 | [diff] [blame] | 1295 | async_chunk[i].pending = &ctx->num_chunks; |
Nikolay Borisov | 751b643 | 2020-06-03 08:55:22 +0300 | [diff] [blame] | 1296 | async_chunk[i].inode = &inode->vfs_inode; |
Nikolay Borisov | 97db120 | 2019-03-12 17:20:24 +0200 | [diff] [blame] | 1297 | async_chunk[i].start = start; |
| 1298 | async_chunk[i].end = cur_end; |
Nikolay Borisov | 97db120 | 2019-03-12 17:20:24 +0200 | [diff] [blame] | 1299 | async_chunk[i].write_flags = write_flags; |
| 1300 | INIT_LIST_HEAD(&async_chunk[i].extents); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1301 | |
Chris Mason | 1d53c9e | 2019-07-10 12:28:16 -0700 | [diff] [blame] | 1302 | /* |
| 1303 | * The locked_page comes all the way from writepage and its |
| 1304 | * the original page we were actually given. As we spread |
| 1305 | * this large delalloc region across multiple async_chunk |
| 1306 | * structs, only the first struct needs a pointer to locked_page |
| 1307 | * |
| 1308 | * This way we don't need racey decisions about who is supposed |
| 1309 | * to unlock it. |
| 1310 | */ |
| 1311 | if (locked_page) { |
Chris Mason | ec39f76 | 2019-07-10 12:28:17 -0700 | [diff] [blame] | 1312 | /* |
| 1313 | * Depending on the compressibility, the pages might or |
| 1314 | * might not go through async. We want all of them to |
| 1315 | * be accounted against wbc once. Let's do it here |
| 1316 | * before the paths diverge. wbc accounting is used |
| 1317 | * only for foreign writeback detection and doesn't |
| 1318 | * need full accuracy. Just account the whole thing |
| 1319 | * against the first page. |
| 1320 | */ |
| 1321 | wbc_account_cgroup_owner(wbc, locked_page, |
| 1322 | cur_end - start); |
Chris Mason | 1d53c9e | 2019-07-10 12:28:16 -0700 | [diff] [blame] | 1323 | async_chunk[i].locked_page = locked_page; |
| 1324 | locked_page = NULL; |
| 1325 | } else { |
| 1326 | async_chunk[i].locked_page = NULL; |
| 1327 | } |
| 1328 | |
Chris Mason | ec39f76 | 2019-07-10 12:28:17 -0700 | [diff] [blame] | 1329 | if (blkcg_css != blkcg_root_css) { |
| 1330 | css_get(blkcg_css); |
| 1331 | async_chunk[i].blkcg_css = blkcg_css; |
| 1332 | } else { |
| 1333 | async_chunk[i].blkcg_css = NULL; |
| 1334 | } |
| 1335 | |
Omar Sandoval | a0cac0e | 2019-09-16 11:30:57 -0700 | [diff] [blame] | 1336 | btrfs_init_work(&async_chunk[i].work, async_cow_start, |
| 1337 | async_cow_submit, async_cow_free); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1338 | |
Nikolay Borisov | 97db120 | 2019-03-12 17:20:24 +0200 | [diff] [blame] | 1339 | nr_pages = DIV_ROUND_UP(cur_end - start, PAGE_SIZE); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1340 | atomic_add(nr_pages, &fs_info->async_delalloc_pages); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1341 | |
Nikolay Borisov | 97db120 | 2019-03-12 17:20:24 +0200 | [diff] [blame] | 1342 | btrfs_queue_work(fs_info->delalloc_workers, &async_chunk[i].work); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1343 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1344 | *nr_written += nr_pages; |
| 1345 | start = cur_end + 1; |
| 1346 | } |
| 1347 | *page_started = 1; |
| 1348 | return 0; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1349 | } |
| 1350 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 1351 | 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] | 1352 | u64 bytenr, u64 num_bytes) |
| 1353 | { |
| 1354 | int ret; |
| 1355 | struct btrfs_ordered_sum *sums; |
| 1356 | LIST_HEAD(list); |
| 1357 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1358 | ret = btrfs_lookup_csums_range(fs_info->csum_root, bytenr, |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 1359 | bytenr + num_bytes - 1, &list, 0); |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1360 | if (ret == 0 && list_empty(&list)) |
| 1361 | return 0; |
| 1362 | |
| 1363 | while (!list_empty(&list)) { |
| 1364 | sums = list_entry(list.next, struct btrfs_ordered_sum, list); |
| 1365 | list_del(&sums->list); |
| 1366 | kfree(sums); |
| 1367 | } |
Liu Bo | 5811375 | 2018-01-31 17:09:13 -0700 | [diff] [blame] | 1368 | if (ret < 0) |
| 1369 | return ret; |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1370 | return 1; |
| 1371 | } |
| 1372 | |
Nikolay Borisov | 8ba96f3 | 2020-06-03 08:55:20 +0300 | [diff] [blame] | 1373 | 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] | 1374 | const u64 start, const u64 end, |
| 1375 | int *page_started, unsigned long *nr_written) |
| 1376 | { |
Nikolay Borisov | 8ba96f3 | 2020-06-03 08:55:20 +0300 | [diff] [blame] | 1377 | const bool is_space_ino = btrfs_is_free_space_inode(inode); |
| 1378 | const bool is_reloc_ino = (inode->root->root_key.objectid == |
Filipe Manana | 6bd335b | 2020-06-08 13:33:05 +0100 | [diff] [blame] | 1379 | BTRFS_DATA_RELOC_TREE_OBJECTID); |
Filipe Manana | 2166e5e | 2020-05-27 11:16:19 +0100 | [diff] [blame] | 1380 | const u64 range_bytes = end + 1 - start; |
Nikolay Borisov | 8ba96f3 | 2020-06-03 08:55:20 +0300 | [diff] [blame] | 1381 | struct extent_io_tree *io_tree = &inode->io_tree; |
Filipe Manana | 467dc47 | 2020-05-27 11:16:07 +0100 | [diff] [blame] | 1382 | u64 range_start = start; |
| 1383 | u64 count; |
| 1384 | |
| 1385 | /* |
| 1386 | * If EXTENT_NORESERVE is set it means that when the buffered write was |
| 1387 | * made we had not enough available data space and therefore we did not |
| 1388 | * reserve data space for it, since we though we could do NOCOW for the |
| 1389 | * respective file range (either there is prealloc extent or the inode |
| 1390 | * has the NOCOW bit set). |
| 1391 | * |
| 1392 | * However when we need to fallback to COW mode (because for example the |
| 1393 | * block group for the corresponding extent was turned to RO mode by a |
| 1394 | * scrub or relocation) we need to do the following: |
| 1395 | * |
| 1396 | * 1) We increment the bytes_may_use counter of the data space info. |
| 1397 | * If COW succeeds, it allocates a new data extent and after doing |
| 1398 | * that it decrements the space info's bytes_may_use counter and |
| 1399 | * increments its bytes_reserved counter by the same amount (we do |
| 1400 | * this at btrfs_add_reserved_bytes()). So we need to increment the |
| 1401 | * bytes_may_use counter to compensate (when space is reserved at |
| 1402 | * buffered write time, the bytes_may_use counter is incremented); |
| 1403 | * |
| 1404 | * 2) We clear the EXTENT_NORESERVE bit from the range. We do this so |
| 1405 | * that if the COW path fails for any reason, it decrements (through |
| 1406 | * extent_clear_unlock_delalloc()) the bytes_may_use counter of the |
| 1407 | * data space info, which we incremented in the step above. |
Filipe Manana | 2166e5e | 2020-05-27 11:16:19 +0100 | [diff] [blame] | 1408 | * |
| 1409 | * 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] | 1410 | * space cache inode or an inode of the data relocation tree, we must |
| 1411 | * also increment bytes_may_use of the data space_info for the same |
| 1412 | * reason. Space caches and relocated data extents always get a prealloc |
Filipe Manana | 2166e5e | 2020-05-27 11:16:19 +0100 | [diff] [blame] | 1413 | * extent for them, however scrub or balance may have set the block |
Filipe Manana | 6bd335b | 2020-06-08 13:33:05 +0100 | [diff] [blame] | 1414 | * group that contains that extent to RO mode and therefore force COW |
| 1415 | * when starting writeback. |
Filipe Manana | 467dc47 | 2020-05-27 11:16:07 +0100 | [diff] [blame] | 1416 | */ |
Filipe Manana | 2166e5e | 2020-05-27 11:16:19 +0100 | [diff] [blame] | 1417 | count = count_range_bits(io_tree, &range_start, end, range_bytes, |
Filipe Manana | 467dc47 | 2020-05-27 11:16:07 +0100 | [diff] [blame] | 1418 | EXTENT_NORESERVE, 0); |
Filipe Manana | 6bd335b | 2020-06-08 13:33:05 +0100 | [diff] [blame] | 1419 | if (count > 0 || is_space_ino || is_reloc_ino) { |
| 1420 | u64 bytes = count; |
Nikolay Borisov | 8ba96f3 | 2020-06-03 08:55:20 +0300 | [diff] [blame] | 1421 | struct btrfs_fs_info *fs_info = inode->root->fs_info; |
Filipe Manana | 467dc47 | 2020-05-27 11:16:07 +0100 | [diff] [blame] | 1422 | struct btrfs_space_info *sinfo = fs_info->data_sinfo; |
| 1423 | |
Filipe Manana | 6bd335b | 2020-06-08 13:33:05 +0100 | [diff] [blame] | 1424 | if (is_space_ino || is_reloc_ino) |
| 1425 | bytes = range_bytes; |
| 1426 | |
Filipe Manana | 467dc47 | 2020-05-27 11:16:07 +0100 | [diff] [blame] | 1427 | spin_lock(&sinfo->lock); |
Filipe Manana | 2166e5e | 2020-05-27 11:16:19 +0100 | [diff] [blame] | 1428 | btrfs_space_info_update_bytes_may_use(fs_info, sinfo, bytes); |
Filipe Manana | 467dc47 | 2020-05-27 11:16:07 +0100 | [diff] [blame] | 1429 | spin_unlock(&sinfo->lock); |
| 1430 | |
Filipe Manana | 2166e5e | 2020-05-27 11:16:19 +0100 | [diff] [blame] | 1431 | if (count > 0) |
| 1432 | clear_extent_bit(io_tree, start, end, EXTENT_NORESERVE, |
| 1433 | 0, 0, NULL); |
Filipe Manana | 467dc47 | 2020-05-27 11:16:07 +0100 | [diff] [blame] | 1434 | } |
| 1435 | |
Nikolay Borisov | 8ba96f3 | 2020-06-03 08:55:20 +0300 | [diff] [blame] | 1436 | return cow_file_range(inode, locked_page, start, end, page_started, |
| 1437 | nr_written, 1); |
Filipe Manana | 467dc47 | 2020-05-27 11:16:07 +0100 | [diff] [blame] | 1438 | } |
| 1439 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1440 | /* |
| 1441 | * when nowcow writeback call back. This checks for snapshots or COW copies |
| 1442 | * of the extents that exist in the file, and COWs the file as required. |
| 1443 | * |
| 1444 | * If no cow copies or snapshots exist, we write directly to the existing |
| 1445 | * blocks on disk |
| 1446 | */ |
Nikolay Borisov | 968322c | 2020-06-03 08:55:21 +0300 | [diff] [blame] | 1447 | static noinline int run_delalloc_nocow(struct btrfs_inode *inode, |
Chris Mason | 7f366cf | 2009-03-12 20:12:45 -0400 | [diff] [blame] | 1448 | struct page *locked_page, |
Nikolay Borisov | 3e02484 | 2019-08-21 10:42:03 +0300 | [diff] [blame] | 1449 | const u64 start, const u64 end, |
| 1450 | int *page_started, int force, |
| 1451 | unsigned long *nr_written) |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1452 | { |
Nikolay Borisov | 968322c | 2020-06-03 08:55:21 +0300 | [diff] [blame] | 1453 | struct btrfs_fs_info *fs_info = inode->root->fs_info; |
| 1454 | struct btrfs_root *root = inode->root; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1455 | struct btrfs_path *path; |
Nikolay Borisov | 3e02484 | 2019-08-21 10:42:03 +0300 | [diff] [blame] | 1456 | u64 cow_start = (u64)-1; |
| 1457 | u64 cur_offset = start; |
Robbie Ko | 8ecebf4d | 2018-08-06 10:30:30 +0800 | [diff] [blame] | 1458 | int ret; |
Nikolay Borisov | 3e02484 | 2019-08-21 10:42:03 +0300 | [diff] [blame] | 1459 | bool check_prev = true; |
Nikolay Borisov | 968322c | 2020-06-03 08:55:21 +0300 | [diff] [blame] | 1460 | const bool freespace_inode = btrfs_is_free_space_inode(inode); |
| 1461 | u64 ino = btrfs_ino(inode); |
Nikolay Borisov | 762bf09 | 2019-08-22 17:24:20 +0300 | [diff] [blame] | 1462 | bool nocow = false; |
| 1463 | u64 disk_bytenr = 0; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1464 | |
| 1465 | path = btrfs_alloc_path(); |
Josef Bacik | 17ca04a | 2012-05-31 15:58:55 -0400 | [diff] [blame] | 1466 | if (!path) { |
Nikolay Borisov | 968322c | 2020-06-03 08:55:21 +0300 | [diff] [blame] | 1467 | extent_clear_unlock_delalloc(inode, start, end, locked_page, |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1468 | EXTENT_LOCKED | EXTENT_DELALLOC | |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 1469 | EXTENT_DO_ACCOUNTING | |
| 1470 | EXTENT_DEFRAG, PAGE_UNLOCK | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1471 | PAGE_CLEAR_DIRTY | |
| 1472 | PAGE_SET_WRITEBACK | |
| 1473 | PAGE_END_WRITEBACK); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 1474 | return -ENOMEM; |
Josef Bacik | 17ca04a | 2012-05-31 15:58:55 -0400 | [diff] [blame] | 1475 | } |
Li Zefan | 82d5902 | 2011-04-20 10:33:24 +0800 | [diff] [blame] | 1476 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1477 | while (1) { |
Nikolay Borisov | 3e02484 | 2019-08-21 10:42:03 +0300 | [diff] [blame] | 1478 | struct btrfs_key found_key; |
| 1479 | struct btrfs_file_extent_item *fi; |
| 1480 | struct extent_buffer *leaf; |
| 1481 | u64 extent_end; |
| 1482 | u64 extent_offset; |
Nikolay Borisov | 3e02484 | 2019-08-21 10:42:03 +0300 | [diff] [blame] | 1483 | u64 num_bytes = 0; |
| 1484 | u64 disk_num_bytes; |
Nikolay Borisov | 3e02484 | 2019-08-21 10:42:03 +0300 | [diff] [blame] | 1485 | u64 ram_bytes; |
| 1486 | int extent_type; |
Nikolay Borisov | 762bf09 | 2019-08-22 17:24:20 +0300 | [diff] [blame] | 1487 | |
| 1488 | nocow = false; |
Nikolay Borisov | 3e02484 | 2019-08-21 10:42:03 +0300 | [diff] [blame] | 1489 | |
Liu Bo | e4c3b2d | 2017-01-30 12:25:28 -0800 | [diff] [blame] | 1490 | ret = btrfs_lookup_file_extent(NULL, root, path, ino, |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1491 | cur_offset, 0); |
Josef Bacik | d788a34 | 2013-10-25 16:55:08 -0400 | [diff] [blame] | 1492 | if (ret < 0) |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1493 | goto error; |
Nikolay Borisov | a6bd9cd | 2019-08-21 10:42:57 +0300 | [diff] [blame] | 1494 | |
| 1495 | /* |
| 1496 | * If there is no extent for our range when doing the initial |
| 1497 | * search, then go back to the previous slot as it will be the |
| 1498 | * one containing the search offset |
| 1499 | */ |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1500 | if (ret > 0 && path->slots[0] > 0 && check_prev) { |
| 1501 | leaf = path->nodes[0]; |
| 1502 | btrfs_item_key_to_cpu(leaf, &found_key, |
| 1503 | path->slots[0] - 1); |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 1504 | if (found_key.objectid == ino && |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1505 | found_key.type == BTRFS_EXTENT_DATA_KEY) |
| 1506 | path->slots[0]--; |
| 1507 | } |
Nikolay Borisov | 3e02484 | 2019-08-21 10:42:03 +0300 | [diff] [blame] | 1508 | check_prev = false; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1509 | next_slot: |
Nikolay Borisov | a6bd9cd | 2019-08-21 10:42:57 +0300 | [diff] [blame] | 1510 | /* Go to next leaf if we have exhausted the current one */ |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1511 | leaf = path->nodes[0]; |
| 1512 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { |
| 1513 | ret = btrfs_next_leaf(root, path); |
Liu Bo | e891669 | 2018-01-25 11:02:50 -0700 | [diff] [blame] | 1514 | if (ret < 0) { |
| 1515 | if (cow_start != (u64)-1) |
| 1516 | cur_offset = cow_start; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1517 | goto error; |
Liu Bo | e891669 | 2018-01-25 11:02:50 -0700 | [diff] [blame] | 1518 | } |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1519 | if (ret > 0) |
| 1520 | break; |
| 1521 | leaf = path->nodes[0]; |
| 1522 | } |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1523 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1524 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1525 | |
Nikolay Borisov | a6bd9cd | 2019-08-21 10:42:57 +0300 | [diff] [blame] | 1526 | /* Didn't find anything for our INO */ |
Filipe Manana | 1d512cb | 2015-11-09 00:33:58 +0000 | [diff] [blame] | 1527 | if (found_key.objectid > ino) |
| 1528 | break; |
Nikolay Borisov | a6bd9cd | 2019-08-21 10:42:57 +0300 | [diff] [blame] | 1529 | /* |
| 1530 | * Keep searching until we find an EXTENT_ITEM or there are no |
| 1531 | * more extents for this inode |
| 1532 | */ |
Filipe Manana | 1d512cb | 2015-11-09 00:33:58 +0000 | [diff] [blame] | 1533 | if (WARN_ON_ONCE(found_key.objectid < ino) || |
| 1534 | found_key.type < BTRFS_EXTENT_DATA_KEY) { |
| 1535 | path->slots[0]++; |
| 1536 | goto next_slot; |
| 1537 | } |
Nikolay Borisov | a6bd9cd | 2019-08-21 10:42:57 +0300 | [diff] [blame] | 1538 | |
| 1539 | /* Found key is not EXTENT_DATA_KEY or starts after req range */ |
Filipe Manana | 1d512cb | 2015-11-09 00:33:58 +0000 | [diff] [blame] | 1540 | if (found_key.type > BTRFS_EXTENT_DATA_KEY || |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1541 | found_key.offset > end) |
| 1542 | break; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1543 | |
Nikolay Borisov | a6bd9cd | 2019-08-21 10:42:57 +0300 | [diff] [blame] | 1544 | /* |
| 1545 | * If the found extent starts after requested offset, then |
| 1546 | * adjust extent_end to be right before this extent begins |
| 1547 | */ |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1548 | if (found_key.offset > cur_offset) { |
| 1549 | extent_end = found_key.offset; |
Chris Mason | e9061e2 | 2009-10-09 09:57:45 -0400 | [diff] [blame] | 1550 | extent_type = 0; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1551 | goto out_check; |
| 1552 | } |
Chris Mason | c31f883 | 2008-01-08 15:46:31 -0500 | [diff] [blame] | 1553 | |
Nikolay Borisov | a6bd9cd | 2019-08-21 10:42:57 +0300 | [diff] [blame] | 1554 | /* |
| 1555 | * Found extent which begins before our range and potentially |
| 1556 | * intersect it |
| 1557 | */ |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1558 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 1559 | struct btrfs_file_extent_item); |
| 1560 | extent_type = btrfs_file_extent_type(leaf, fi); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1561 | |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 1562 | ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1563 | if (extent_type == BTRFS_FILE_EXTENT_REG || |
| 1564 | extent_type == BTRFS_FILE_EXTENT_PREALLOC) { |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1565 | disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1566 | extent_offset = btrfs_file_extent_offset(leaf, fi); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1567 | extent_end = found_key.offset + |
| 1568 | btrfs_file_extent_num_bytes(leaf, fi); |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 1569 | disk_num_bytes = |
| 1570 | btrfs_file_extent_disk_num_bytes(leaf, fi); |
Nikolay Borisov | a6bd9cd | 2019-08-21 10:42:57 +0300 | [diff] [blame] | 1571 | /* |
Filipe Manana | de7999a | 2019-12-11 09:01:40 +0000 | [diff] [blame] | 1572 | * If the extent we got ends before our current offset, |
| 1573 | * skip to the next extent. |
Nikolay Borisov | a6bd9cd | 2019-08-21 10:42:57 +0300 | [diff] [blame] | 1574 | */ |
Filipe Manana | de7999a | 2019-12-11 09:01:40 +0000 | [diff] [blame] | 1575 | if (extent_end <= cur_offset) { |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1576 | path->slots[0]++; |
| 1577 | goto next_slot; |
| 1578 | } |
Nikolay Borisov | a6bd9cd | 2019-08-21 10:42:57 +0300 | [diff] [blame] | 1579 | /* Skip holes */ |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1580 | if (disk_bytenr == 0) |
| 1581 | goto out_check; |
Nikolay Borisov | a6bd9cd | 2019-08-21 10:42:57 +0300 | [diff] [blame] | 1582 | /* Skip compressed/encrypted/encoded extents */ |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1583 | if (btrfs_file_extent_compression(leaf, fi) || |
| 1584 | btrfs_file_extent_encryption(leaf, fi) || |
| 1585 | btrfs_file_extent_other_encoding(leaf, fi)) |
| 1586 | goto out_check; |
Ethan Lien | 78d4295 | 2018-05-17 14:58:29 +0800 | [diff] [blame] | 1587 | /* |
Nikolay Borisov | a6bd9cd | 2019-08-21 10:42:57 +0300 | [diff] [blame] | 1588 | * If extent is created before the last volume's snapshot |
| 1589 | * this implies the extent is shared, hence we can't do |
| 1590 | * nocow. This is the same check as in |
| 1591 | * btrfs_cross_ref_exist but without calling |
| 1592 | * btrfs_search_slot. |
Ethan Lien | 78d4295 | 2018-05-17 14:58:29 +0800 | [diff] [blame] | 1593 | */ |
Nikolay Borisov | 3e02484 | 2019-08-21 10:42:03 +0300 | [diff] [blame] | 1594 | if (!freespace_inode && |
Lu Fengqi | 27a7ff5 | 2018-11-29 17:31:32 +0800 | [diff] [blame] | 1595 | btrfs_file_extent_generation(leaf, fi) <= |
Ethan Lien | 78d4295 | 2018-05-17 14:58:29 +0800 | [diff] [blame] | 1596 | btrfs_root_last_snapshot(&root->root_item)) |
| 1597 | goto out_check; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1598 | if (extent_type == BTRFS_FILE_EXTENT_REG && !force) |
| 1599 | goto out_check; |
Nikolay Borisov | a6bd9cd | 2019-08-21 10:42:57 +0300 | [diff] [blame] | 1600 | /* If extent is RO, we must COW it */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 1601 | if (btrfs_extent_readonly(fs_info, disk_bytenr)) |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1602 | goto out_check; |
Liu Bo | 5811375 | 2018-01-31 17:09:13 -0700 | [diff] [blame] | 1603 | ret = btrfs_cross_ref_exist(root, ino, |
| 1604 | found_key.offset - |
Boris Burkov | a84d5d4 | 2020-08-18 11:00:05 -0700 | [diff] [blame] | 1605 | extent_offset, disk_bytenr, false); |
Liu Bo | 5811375 | 2018-01-31 17:09:13 -0700 | [diff] [blame] | 1606 | if (ret) { |
| 1607 | /* |
| 1608 | * ret could be -EIO if the above fails to read |
| 1609 | * metadata. |
| 1610 | */ |
| 1611 | if (ret < 0) { |
| 1612 | if (cow_start != (u64)-1) |
| 1613 | cur_offset = cow_start; |
| 1614 | goto error; |
| 1615 | } |
| 1616 | |
Nikolay Borisov | 3e02484 | 2019-08-21 10:42:03 +0300 | [diff] [blame] | 1617 | WARN_ON_ONCE(freespace_inode); |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1618 | goto out_check; |
Liu Bo | 5811375 | 2018-01-31 17:09:13 -0700 | [diff] [blame] | 1619 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1620 | disk_bytenr += extent_offset; |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1621 | disk_bytenr += cur_offset - found_key.offset; |
| 1622 | num_bytes = min(end + 1, extent_end) - cur_offset; |
| 1623 | /* |
Nikolay Borisov | a6bd9cd | 2019-08-21 10:42:57 +0300 | [diff] [blame] | 1624 | * If there are pending snapshots for this root, we |
| 1625 | * fall into common COW way |
Wang Shilong | e9894fd | 2014-03-27 11:12:25 +0800 | [diff] [blame] | 1626 | */ |
Nikolay Borisov | 3e02484 | 2019-08-21 10:42:03 +0300 | [diff] [blame] | 1627 | if (!freespace_inode && atomic_read(&root->snapshot_force_cow)) |
Robbie Ko | 8ecebf4d | 2018-08-06 10:30:30 +0800 | [diff] [blame] | 1628 | goto out_check; |
Wang Shilong | e9894fd | 2014-03-27 11:12:25 +0800 | [diff] [blame] | 1629 | /* |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1630 | * force cow if csum exists in the range. |
| 1631 | * this ensure that csum for a given extent are |
| 1632 | * either valid or do not exist. |
| 1633 | */ |
Liu Bo | 5811375 | 2018-01-31 17:09:13 -0700 | [diff] [blame] | 1634 | ret = csum_exist_in_range(fs_info, disk_bytenr, |
| 1635 | num_bytes); |
| 1636 | if (ret) { |
Liu Bo | 5811375 | 2018-01-31 17:09:13 -0700 | [diff] [blame] | 1637 | /* |
| 1638 | * ret could be -EIO if the above fails to read |
| 1639 | * metadata. |
| 1640 | */ |
| 1641 | if (ret < 0) { |
| 1642 | if (cow_start != (u64)-1) |
| 1643 | cur_offset = cow_start; |
| 1644 | goto error; |
| 1645 | } |
Nikolay Borisov | 3e02484 | 2019-08-21 10:42:03 +0300 | [diff] [blame] | 1646 | WARN_ON_ONCE(freespace_inode); |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1647 | goto out_check; |
Robbie Ko | 91e1f56 | 2016-10-07 10:01:29 +0800 | [diff] [blame] | 1648 | } |
Robbie Ko | 8ecebf4d | 2018-08-06 10:30:30 +0800 | [diff] [blame] | 1649 | if (!btrfs_inc_nocow_writers(fs_info, disk_bytenr)) |
Filipe Manana | f78c436 | 2016-05-09 13:15:41 +0100 | [diff] [blame] | 1650 | goto out_check; |
Nikolay Borisov | 3e02484 | 2019-08-21 10:42:03 +0300 | [diff] [blame] | 1651 | nocow = true; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1652 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
Nikolay Borisov | e8e2100 | 2019-08-22 17:25:23 +0300 | [diff] [blame] | 1653 | extent_end = found_key.offset + ram_bytes; |
| 1654 | extent_end = ALIGN(extent_end, fs_info->sectorsize); |
Nikolay Borisov | 922f051 | 2019-08-05 17:47:06 +0300 | [diff] [blame] | 1655 | /* Skip extents outside of our requested range */ |
| 1656 | if (extent_end <= start) { |
| 1657 | path->slots[0]++; |
| 1658 | goto next_slot; |
| 1659 | } |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1660 | } else { |
Nikolay Borisov | e8e2100 | 2019-08-22 17:25:23 +0300 | [diff] [blame] | 1661 | /* If this triggers then we have a memory corruption */ |
Arnd Bergmann | 290342f | 2019-03-25 14:02:25 +0100 | [diff] [blame] | 1662 | BUG(); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1663 | } |
| 1664 | out_check: |
Nikolay Borisov | a6bd9cd | 2019-08-21 10:42:57 +0300 | [diff] [blame] | 1665 | /* |
| 1666 | * If nocow is false then record the beginning of the range |
| 1667 | * that needs to be COWed |
| 1668 | */ |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1669 | if (!nocow) { |
| 1670 | if (cow_start == (u64)-1) |
| 1671 | cow_start = cur_offset; |
| 1672 | cur_offset = extent_end; |
| 1673 | if (cur_offset > end) |
| 1674 | break; |
| 1675 | path->slots[0]++; |
| 1676 | goto next_slot; |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1677 | } |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1678 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1679 | btrfs_release_path(path); |
Nikolay Borisov | a6bd9cd | 2019-08-21 10:42:57 +0300 | [diff] [blame] | 1680 | |
| 1681 | /* |
| 1682 | * COW range from cow_start to found_key.offset - 1. As the key |
| 1683 | * will contain the beginning of the first extent that can be |
| 1684 | * NOCOW, following one which needs to be COW'ed |
| 1685 | */ |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1686 | if (cow_start != (u64)-1) { |
Nikolay Borisov | 968322c | 2020-06-03 08:55:21 +0300 | [diff] [blame] | 1687 | ret = fallback_to_cow(inode, locked_page, |
Nikolay Borisov | 8ba96f3 | 2020-06-03 08:55:20 +0300 | [diff] [blame] | 1688 | cow_start, found_key.offset - 1, |
Filipe Manana | 467dc47 | 2020-05-27 11:16:07 +0100 | [diff] [blame] | 1689 | page_started, nr_written); |
Josef Bacik | 230ed39 | 2020-07-06 09:14:12 -0400 | [diff] [blame] | 1690 | if (ret) |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1691 | goto error; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1692 | cow_start = (u64)-1; |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1693 | } |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1694 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1695 | if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) { |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 1696 | u64 orig_start = found_key.offset - extent_offset; |
Nikolay Borisov | 3e02484 | 2019-08-21 10:42:03 +0300 | [diff] [blame] | 1697 | struct extent_map *em; |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 1698 | |
Nikolay Borisov | 968322c | 2020-06-03 08:55:21 +0300 | [diff] [blame] | 1699 | em = create_io_em(inode, cur_offset, num_bytes, |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 1700 | orig_start, |
| 1701 | disk_bytenr, /* block_start */ |
| 1702 | num_bytes, /* block_len */ |
| 1703 | disk_num_bytes, /* orig_block_len */ |
| 1704 | ram_bytes, BTRFS_COMPRESS_NONE, |
| 1705 | BTRFS_ORDERED_PREALLOC); |
| 1706 | if (IS_ERR(em)) { |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 1707 | ret = PTR_ERR(em); |
| 1708 | goto error; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1709 | } |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 1710 | free_extent_map(em); |
Nikolay Borisov | 968322c | 2020-06-03 08:55:21 +0300 | [diff] [blame] | 1711 | ret = btrfs_add_ordered_extent(inode, cur_offset, |
Nikolay Borisov | bb55f62 | 2019-08-05 17:47:05 +0300 | [diff] [blame] | 1712 | disk_bytenr, num_bytes, |
| 1713 | num_bytes, |
| 1714 | BTRFS_ORDERED_PREALLOC); |
Nikolay Borisov | 762bf09 | 2019-08-22 17:24:20 +0300 | [diff] [blame] | 1715 | if (ret) { |
Nikolay Borisov | 968322c | 2020-06-03 08:55:21 +0300 | [diff] [blame] | 1716 | btrfs_drop_extent_cache(inode, cur_offset, |
Nikolay Borisov | 762bf09 | 2019-08-22 17:24:20 +0300 | [diff] [blame] | 1717 | cur_offset + num_bytes - 1, |
| 1718 | 0); |
| 1719 | goto error; |
| 1720 | } |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1721 | } else { |
Nikolay Borisov | 968322c | 2020-06-03 08:55:21 +0300 | [diff] [blame] | 1722 | ret = btrfs_add_ordered_extent(inode, cur_offset, |
Nikolay Borisov | bb55f62 | 2019-08-05 17:47:05 +0300 | [diff] [blame] | 1723 | disk_bytenr, num_bytes, |
| 1724 | num_bytes, |
| 1725 | BTRFS_ORDERED_NOCOW); |
Nikolay Borisov | 762bf09 | 2019-08-22 17:24:20 +0300 | [diff] [blame] | 1726 | if (ret) |
| 1727 | goto error; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1728 | } |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1729 | |
Filipe Manana | f78c436 | 2016-05-09 13:15:41 +0100 | [diff] [blame] | 1730 | if (nocow) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1731 | btrfs_dec_nocow_writers(fs_info, disk_bytenr); |
Nikolay Borisov | 762bf09 | 2019-08-22 17:24:20 +0300 | [diff] [blame] | 1732 | nocow = false; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1733 | |
Yan, Zheng | efa5646 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 1734 | if (root->root_key.objectid == |
Qu Wenruo | 4dbd80f | 2017-03-08 10:25:51 +0800 | [diff] [blame] | 1735 | BTRFS_DATA_RELOC_TREE_OBJECTID) |
| 1736 | /* |
| 1737 | * Error handled later, as we must prevent |
| 1738 | * extent_clear_unlock_delalloc() in error handler |
| 1739 | * from freeing metadata of created ordered extent. |
| 1740 | */ |
Nikolay Borisov | 968322c | 2020-06-03 08:55:21 +0300 | [diff] [blame] | 1741 | ret = btrfs_reloc_clone_csums(inode, cur_offset, |
Yan, Zheng | efa5646 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 1742 | num_bytes); |
Yan, Zheng | efa5646 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 1743 | |
Nikolay Borisov | 968322c | 2020-06-03 08:55:21 +0300 | [diff] [blame] | 1744 | extent_clear_unlock_delalloc(inode, cur_offset, |
Nikolay Borisov | 74e9194 | 2019-07-17 16:18:16 +0300 | [diff] [blame] | 1745 | cur_offset + num_bytes - 1, |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1746 | locked_page, EXTENT_LOCKED | |
Wang Xiaoguang | 1851309 | 2016-07-25 15:51:40 +0800 | [diff] [blame] | 1747 | EXTENT_DELALLOC | |
| 1748 | EXTENT_CLEAR_DATA_RESV, |
| 1749 | PAGE_UNLOCK | PAGE_SET_PRIVATE2); |
| 1750 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1751 | cur_offset = extent_end; |
Qu Wenruo | 4dbd80f | 2017-03-08 10:25:51 +0800 | [diff] [blame] | 1752 | |
| 1753 | /* |
| 1754 | * btrfs_reloc_clone_csums() error, now we're OK to call error |
| 1755 | * handler, as metadata for created ordered extent will only |
| 1756 | * be freed by btrfs_finish_ordered_io(). |
| 1757 | */ |
| 1758 | if (ret) |
| 1759 | goto error; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1760 | if (cur_offset > end) |
| 1761 | break; |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1762 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1763 | btrfs_release_path(path); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1764 | |
Robbie Ko | 506481b | 2018-10-30 18:04:04 +0800 | [diff] [blame] | 1765 | if (cur_offset <= end && cow_start == (u64)-1) |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1766 | cow_start = cur_offset; |
Josef Bacik | 17ca04a | 2012-05-31 15:58:55 -0400 | [diff] [blame] | 1767 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1768 | if (cow_start != (u64)-1) { |
Robbie Ko | 506481b | 2018-10-30 18:04:04 +0800 | [diff] [blame] | 1769 | cur_offset = end; |
Nikolay Borisov | 968322c | 2020-06-03 08:55:21 +0300 | [diff] [blame] | 1770 | ret = fallback_to_cow(inode, locked_page, cow_start, end, |
| 1771 | page_started, nr_written); |
Josef Bacik | d788a34 | 2013-10-25 16:55:08 -0400 | [diff] [blame] | 1772 | if (ret) |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1773 | goto error; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1774 | } |
| 1775 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1776 | error: |
Nikolay Borisov | 762bf09 | 2019-08-22 17:24:20 +0300 | [diff] [blame] | 1777 | if (nocow) |
| 1778 | btrfs_dec_nocow_writers(fs_info, disk_bytenr); |
| 1779 | |
Josef Bacik | 17ca04a | 2012-05-31 15:58:55 -0400 | [diff] [blame] | 1780 | if (ret && cur_offset < end) |
Nikolay Borisov | 968322c | 2020-06-03 08:55:21 +0300 | [diff] [blame] | 1781 | extent_clear_unlock_delalloc(inode, cur_offset, end, |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1782 | locked_page, EXTENT_LOCKED | |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 1783 | EXTENT_DELALLOC | EXTENT_DEFRAG | |
| 1784 | EXTENT_DO_ACCOUNTING, PAGE_UNLOCK | |
| 1785 | PAGE_CLEAR_DIRTY | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1786 | PAGE_SET_WRITEBACK | |
| 1787 | PAGE_END_WRITEBACK); |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1788 | btrfs_free_path(path); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1789 | return ret; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1790 | } |
| 1791 | |
Nikolay Borisov | 0c49422 | 2020-06-03 08:55:28 +0300 | [diff] [blame] | 1792 | 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] | 1793 | { |
| 1794 | |
Nikolay Borisov | 0c49422 | 2020-06-03 08:55:28 +0300 | [diff] [blame] | 1795 | if (!(inode->flags & BTRFS_INODE_NODATACOW) && |
| 1796 | !(inode->flags & BTRFS_INODE_PREALLOC)) |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 1797 | return 0; |
| 1798 | |
| 1799 | /* |
| 1800 | * @defrag_bytes is a hint value, no spinlock held here, |
| 1801 | * if is not zero, it means the file is defragging. |
| 1802 | * Force cow if given extent needs to be defragged. |
| 1803 | */ |
Nikolay Borisov | 0c49422 | 2020-06-03 08:55:28 +0300 | [diff] [blame] | 1804 | if (inode->defrag_bytes && |
| 1805 | test_range_bit(&inode->io_tree, start, end, EXTENT_DEFRAG, 0, NULL)) |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 1806 | return 1; |
| 1807 | |
| 1808 | return 0; |
| 1809 | } |
| 1810 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1811 | /* |
Nikolay Borisov | 5eaad97 | 2018-11-01 14:09:46 +0200 | [diff] [blame] | 1812 | * Function to process delayed allocation (create CoW) for ranges which are |
| 1813 | * being touched for the first time. |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1814 | */ |
Nikolay Borisov | 98456b9 | 2020-06-03 08:55:29 +0300 | [diff] [blame] | 1815 | 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] | 1816 | u64 start, u64 end, int *page_started, unsigned long *nr_written, |
| 1817 | struct writeback_control *wbc) |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1818 | { |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1819 | int ret; |
Nikolay Borisov | 98456b9 | 2020-06-03 08:55:29 +0300 | [diff] [blame] | 1820 | int force_cow = need_force_cow(inode, start, end); |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1821 | |
Nikolay Borisov | 98456b9 | 2020-06-03 08:55:29 +0300 | [diff] [blame] | 1822 | if (inode->flags & BTRFS_INODE_NODATACOW && !force_cow) { |
| 1823 | ret = run_delalloc_nocow(inode, locked_page, start, end, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1824 | page_started, 1, nr_written); |
Nikolay Borisov | 98456b9 | 2020-06-03 08:55:29 +0300 | [diff] [blame] | 1825 | } else if (inode->flags & BTRFS_INODE_PREALLOC && !force_cow) { |
| 1826 | ret = run_delalloc_nocow(inode, locked_page, start, end, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1827 | page_started, 0, nr_written); |
Nikolay Borisov | 98456b9 | 2020-06-03 08:55:29 +0300 | [diff] [blame] | 1828 | } else if (!inode_can_compress(inode) || |
| 1829 | !inode_need_compress(inode, start, end)) { |
| 1830 | ret = cow_file_range(inode, locked_page, start, end, |
| 1831 | page_started, nr_written, 1); |
Josef Bacik | 7ddf5a4 | 2012-06-08 15:26:47 -0400 | [diff] [blame] | 1832 | } else { |
Nikolay Borisov | 98456b9 | 2020-06-03 08:55:29 +0300 | [diff] [blame] | 1833 | set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, &inode->runtime_flags); |
| 1834 | ret = cow_file_range_async(inode, wbc, locked_page, start, end, |
David Sterba | fac07d2 | 2019-10-29 18:28:57 +0100 | [diff] [blame] | 1835 | page_started, nr_written); |
Josef Bacik | 7ddf5a4 | 2012-06-08 15:26:47 -0400 | [diff] [blame] | 1836 | } |
Qu Wenruo | 52427260 | 2017-03-08 10:25:52 +0800 | [diff] [blame] | 1837 | if (ret) |
Nikolay Borisov | 98456b9 | 2020-06-03 08:55:29 +0300 | [diff] [blame] | 1838 | btrfs_cleanup_ordered_extents(inode, locked_page, start, |
Nikolay Borisov | d1051d6 | 2018-11-21 17:10:52 +0200 | [diff] [blame] | 1839 | end - start + 1); |
Chris Mason | b888db2b | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 1840 | return ret; |
| 1841 | } |
| 1842 | |
Nikolay Borisov | abbb55f | 2018-11-01 14:09:53 +0200 | [diff] [blame] | 1843 | void btrfs_split_delalloc_extent(struct inode *inode, |
| 1844 | struct extent_state *orig, u64 split) |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1845 | { |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1846 | u64 size; |
| 1847 | |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1848 | /* not delalloc, ignore it */ |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1849 | if (!(orig->state & EXTENT_DELALLOC)) |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 1850 | return; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1851 | |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1852 | size = orig->end - orig->start + 1; |
| 1853 | if (size > BTRFS_MAX_EXTENT_SIZE) { |
David Sterba | 823bb20 | 2017-01-04 11:09:51 +0100 | [diff] [blame] | 1854 | u32 num_extents; |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1855 | u64 new_size; |
| 1856 | |
| 1857 | /* |
Nikolay Borisov | 5c84819 | 2018-11-01 14:09:52 +0200 | [diff] [blame] | 1858 | * See the explanation in btrfs_merge_delalloc_extent, the same |
Josef Bacik | ba11721 | 2015-03-13 15:01:24 -0400 | [diff] [blame] | 1859 | * applies here, just in reverse. |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1860 | */ |
| 1861 | new_size = orig->end - split + 1; |
David Sterba | 823bb20 | 2017-01-04 11:09:51 +0100 | [diff] [blame] | 1862 | num_extents = count_max_extents(new_size); |
Josef Bacik | ba11721 | 2015-03-13 15:01:24 -0400 | [diff] [blame] | 1863 | new_size = split - orig->start; |
David Sterba | 823bb20 | 2017-01-04 11:09:51 +0100 | [diff] [blame] | 1864 | num_extents += count_max_extents(new_size); |
| 1865 | if (count_max_extents(size) >= num_extents) |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1866 | return; |
| 1867 | } |
| 1868 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1869 | spin_lock(&BTRFS_I(inode)->lock); |
Josef Bacik | 8b62f87 | 2017-10-19 14:15:55 -0400 | [diff] [blame] | 1870 | btrfs_mod_outstanding_extents(BTRFS_I(inode), 1); |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1871 | spin_unlock(&BTRFS_I(inode)->lock); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1872 | } |
| 1873 | |
| 1874 | /* |
Nikolay Borisov | 5c84819 | 2018-11-01 14:09:52 +0200 | [diff] [blame] | 1875 | * Handle merged delayed allocation extents so we can keep track of new extents |
| 1876 | * that are just merged onto old extents, such as when we are doing sequential |
| 1877 | * 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] | 1878 | */ |
Nikolay Borisov | 5c84819 | 2018-11-01 14:09:52 +0200 | [diff] [blame] | 1879 | void btrfs_merge_delalloc_extent(struct inode *inode, struct extent_state *new, |
| 1880 | struct extent_state *other) |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1881 | { |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1882 | u64 new_size, old_size; |
David Sterba | 823bb20 | 2017-01-04 11:09:51 +0100 | [diff] [blame] | 1883 | u32 num_extents; |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1884 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1885 | /* not delalloc, ignore it */ |
| 1886 | if (!(other->state & EXTENT_DELALLOC)) |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 1887 | return; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1888 | |
Josef Bacik | 8461a3d | 2015-03-13 15:12:08 -0400 | [diff] [blame] | 1889 | if (new->start > other->start) |
| 1890 | new_size = new->end - other->start + 1; |
| 1891 | else |
| 1892 | new_size = other->end - new->start + 1; |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1893 | |
| 1894 | /* we're not bigger than the max, unreserve the space and go */ |
| 1895 | if (new_size <= BTRFS_MAX_EXTENT_SIZE) { |
| 1896 | spin_lock(&BTRFS_I(inode)->lock); |
Josef Bacik | 8b62f87 | 2017-10-19 14:15:55 -0400 | [diff] [blame] | 1897 | btrfs_mod_outstanding_extents(BTRFS_I(inode), -1); |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1898 | spin_unlock(&BTRFS_I(inode)->lock); |
| 1899 | return; |
| 1900 | } |
| 1901 | |
| 1902 | /* |
Josef Bacik | ba11721 | 2015-03-13 15:01:24 -0400 | [diff] [blame] | 1903 | * We have to add up either side to figure out how many extents were |
| 1904 | * accounted for before we merged into one big extent. If the number of |
| 1905 | * extents we accounted for is <= the amount we need for the new range |
| 1906 | * then we can return, otherwise drop. Think of it like this |
| 1907 | * |
| 1908 | * [ 4k][MAX_SIZE] |
| 1909 | * |
| 1910 | * So we've grown the extent by a MAX_SIZE extent, this would mean we |
| 1911 | * need 2 outstanding extents, on one side we have 1 and the other side |
| 1912 | * we have 1 so they are == and we can return. But in this case |
| 1913 | * |
| 1914 | * [MAX_SIZE+4k][MAX_SIZE+4k] |
| 1915 | * |
| 1916 | * Each range on their own accounts for 2 extents, but merged together |
| 1917 | * they are only 3 extents worth of accounting, so we need to drop in |
| 1918 | * this case. |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1919 | */ |
Josef Bacik | ba11721 | 2015-03-13 15:01:24 -0400 | [diff] [blame] | 1920 | old_size = other->end - other->start + 1; |
David Sterba | 823bb20 | 2017-01-04 11:09:51 +0100 | [diff] [blame] | 1921 | num_extents = count_max_extents(old_size); |
Josef Bacik | ba11721 | 2015-03-13 15:01:24 -0400 | [diff] [blame] | 1922 | old_size = new->end - new->start + 1; |
David Sterba | 823bb20 | 2017-01-04 11:09:51 +0100 | [diff] [blame] | 1923 | num_extents += count_max_extents(old_size); |
| 1924 | if (count_max_extents(new_size) >= num_extents) |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1925 | return; |
| 1926 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1927 | spin_lock(&BTRFS_I(inode)->lock); |
Josef Bacik | 8b62f87 | 2017-10-19 14:15:55 -0400 | [diff] [blame] | 1928 | btrfs_mod_outstanding_extents(BTRFS_I(inode), -1); |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1929 | spin_unlock(&BTRFS_I(inode)->lock); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1930 | } |
| 1931 | |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 1932 | static void btrfs_add_delalloc_inodes(struct btrfs_root *root, |
| 1933 | struct inode *inode) |
| 1934 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1935 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
| 1936 | |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 1937 | spin_lock(&root->delalloc_lock); |
| 1938 | if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) { |
| 1939 | list_add_tail(&BTRFS_I(inode)->delalloc_inodes, |
| 1940 | &root->delalloc_inodes); |
| 1941 | set_bit(BTRFS_INODE_IN_DELALLOC_LIST, |
| 1942 | &BTRFS_I(inode)->runtime_flags); |
| 1943 | root->nr_delalloc_inodes++; |
| 1944 | if (root->nr_delalloc_inodes == 1) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1945 | spin_lock(&fs_info->delalloc_root_lock); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 1946 | BUG_ON(!list_empty(&root->delalloc_root)); |
| 1947 | list_add_tail(&root->delalloc_root, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1948 | &fs_info->delalloc_roots); |
| 1949 | spin_unlock(&fs_info->delalloc_root_lock); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 1950 | } |
| 1951 | } |
| 1952 | spin_unlock(&root->delalloc_lock); |
| 1953 | } |
| 1954 | |
Nikolay Borisov | 2b87733 | 2018-04-27 12:21:51 +0300 | [diff] [blame] | 1955 | |
| 1956 | void __btrfs_del_delalloc_inode(struct btrfs_root *root, |
| 1957 | struct btrfs_inode *inode) |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 1958 | { |
David Sterba | 3ffbd68 | 2018-06-29 10:56:42 +0200 | [diff] [blame] | 1959 | struct btrfs_fs_info *fs_info = root->fs_info; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1960 | |
Nikolay Borisov | 9e3e97f | 2017-02-20 13:51:07 +0200 | [diff] [blame] | 1961 | if (!list_empty(&inode->delalloc_inodes)) { |
| 1962 | list_del_init(&inode->delalloc_inodes); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 1963 | clear_bit(BTRFS_INODE_IN_DELALLOC_LIST, |
Nikolay Borisov | 9e3e97f | 2017-02-20 13:51:07 +0200 | [diff] [blame] | 1964 | &inode->runtime_flags); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 1965 | root->nr_delalloc_inodes--; |
| 1966 | if (!root->nr_delalloc_inodes) { |
Nikolay Borisov | 7c8a0d3 | 2018-04-27 12:21:52 +0300 | [diff] [blame] | 1967 | ASSERT(list_empty(&root->delalloc_inodes)); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1968 | spin_lock(&fs_info->delalloc_root_lock); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 1969 | BUG_ON(list_empty(&root->delalloc_root)); |
| 1970 | list_del_init(&root->delalloc_root); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1971 | spin_unlock(&fs_info->delalloc_root_lock); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 1972 | } |
| 1973 | } |
Nikolay Borisov | 2b87733 | 2018-04-27 12:21:51 +0300 | [diff] [blame] | 1974 | } |
| 1975 | |
| 1976 | static void btrfs_del_delalloc_inode(struct btrfs_root *root, |
| 1977 | struct btrfs_inode *inode) |
| 1978 | { |
| 1979 | spin_lock(&root->delalloc_lock); |
| 1980 | __btrfs_del_delalloc_inode(root, inode); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 1981 | spin_unlock(&root->delalloc_lock); |
| 1982 | } |
| 1983 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1984 | /* |
Nikolay Borisov | e06a1fc | 2018-11-01 14:09:50 +0200 | [diff] [blame] | 1985 | * Properly track delayed allocation bytes in the inode and to maintain the |
| 1986 | * list of inodes that have pending delalloc work to be done. |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1987 | */ |
Nikolay Borisov | e06a1fc | 2018-11-01 14:09:50 +0200 | [diff] [blame] | 1988 | void btrfs_set_delalloc_extent(struct inode *inode, struct extent_state *state, |
| 1989 | unsigned *bits) |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1990 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1991 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
| 1992 | |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 1993 | if ((*bits & EXTENT_DEFRAG) && !(*bits & EXTENT_DELALLOC)) |
| 1994 | WARN_ON(1); |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1995 | /* |
| 1996 | * set_bit and clear bit hooks normally require _irqsave/restore |
Sergei Trofimovich | 27160b6 | 2011-05-20 20:20:32 +0000 | [diff] [blame] | 1997 | * but in this case, we are only testing for the DELALLOC |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1998 | * bit, which is only set or cleared with irqs on |
| 1999 | */ |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 2000 | if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) { |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 2001 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 2002 | u64 len = state->end + 1 - state->start; |
Josef Bacik | 8b62f87 | 2017-10-19 14:15:55 -0400 | [diff] [blame] | 2003 | u32 num_extents = count_max_extents(len); |
Nikolay Borisov | 70ddc55 | 2017-02-20 13:50:35 +0200 | [diff] [blame] | 2004 | bool do_list = !btrfs_is_free_space_inode(BTRFS_I(inode)); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 2005 | |
Josef Bacik | 8b62f87 | 2017-10-19 14:15:55 -0400 | [diff] [blame] | 2006 | spin_lock(&BTRFS_I(inode)->lock); |
| 2007 | btrfs_mod_outstanding_extents(BTRFS_I(inode), num_extents); |
| 2008 | spin_unlock(&BTRFS_I(inode)->lock); |
Josef Bacik | 287a0ab | 2010-03-19 18:07:23 +0000 | [diff] [blame] | 2009 | |
Josef Bacik | 6a3891c | 2015-03-16 17:38:52 -0400 | [diff] [blame] | 2010 | /* For sanity tests */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2011 | if (btrfs_is_testing(fs_info)) |
Josef Bacik | 6a3891c | 2015-03-16 17:38:52 -0400 | [diff] [blame] | 2012 | return; |
| 2013 | |
Nikolay Borisov | 104b4e5 | 2017-06-20 21:01:20 +0300 | [diff] [blame] | 2014 | percpu_counter_add_batch(&fs_info->delalloc_bytes, len, |
| 2015 | fs_info->delalloc_batch); |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 2016 | spin_lock(&BTRFS_I(inode)->lock); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 2017 | BTRFS_I(inode)->delalloc_bytes += len; |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 2018 | if (*bits & EXTENT_DEFRAG) |
| 2019 | BTRFS_I(inode)->defrag_bytes += len; |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 2020 | if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST, |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 2021 | &BTRFS_I(inode)->runtime_flags)) |
| 2022 | btrfs_add_delalloc_inodes(root, inode); |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 2023 | spin_unlock(&BTRFS_I(inode)->lock); |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 2024 | } |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 2025 | |
| 2026 | if (!(state->state & EXTENT_DELALLOC_NEW) && |
| 2027 | (*bits & EXTENT_DELALLOC_NEW)) { |
| 2028 | spin_lock(&BTRFS_I(inode)->lock); |
| 2029 | BTRFS_I(inode)->new_delalloc_bytes += state->end + 1 - |
| 2030 | state->start; |
| 2031 | spin_unlock(&BTRFS_I(inode)->lock); |
| 2032 | } |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 2033 | } |
| 2034 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2035 | /* |
Nikolay Borisov | a36bb5f | 2018-11-01 14:09:51 +0200 | [diff] [blame] | 2036 | * Once a range is no longer delalloc this function ensures that proper |
| 2037 | * accounting happens. |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2038 | */ |
Nikolay Borisov | a36bb5f | 2018-11-01 14:09:51 +0200 | [diff] [blame] | 2039 | void btrfs_clear_delalloc_extent(struct inode *vfs_inode, |
| 2040 | struct extent_state *state, unsigned *bits) |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 2041 | { |
Nikolay Borisov | a36bb5f | 2018-11-01 14:09:51 +0200 | [diff] [blame] | 2042 | struct btrfs_inode *inode = BTRFS_I(vfs_inode); |
| 2043 | struct btrfs_fs_info *fs_info = btrfs_sb(vfs_inode->i_sb); |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 2044 | u64 len = state->end + 1 - state->start; |
David Sterba | 823bb20 | 2017-01-04 11:09:51 +0100 | [diff] [blame] | 2045 | u32 num_extents = count_max_extents(len); |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 2046 | |
Filipe Manana | 4a4b964 | 2017-07-27 19:52:55 +0100 | [diff] [blame] | 2047 | if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG)) { |
| 2048 | spin_lock(&inode->lock); |
Nikolay Borisov | 6fc0ef6 | 2017-02-20 13:51:03 +0200 | [diff] [blame] | 2049 | inode->defrag_bytes -= len; |
Filipe Manana | 4a4b964 | 2017-07-27 19:52:55 +0100 | [diff] [blame] | 2050 | spin_unlock(&inode->lock); |
| 2051 | } |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 2052 | |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 2053 | /* |
| 2054 | * set_bit and clear bit hooks normally require _irqsave/restore |
Sergei Trofimovich | 27160b6 | 2011-05-20 20:20:32 +0000 | [diff] [blame] | 2055 | * but in this case, we are only testing for the DELALLOC |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 2056 | * bit, which is only set or cleared with irqs on |
| 2057 | */ |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 2058 | if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) { |
Nikolay Borisov | 6fc0ef6 | 2017-02-20 13:51:03 +0200 | [diff] [blame] | 2059 | struct btrfs_root *root = inode->root; |
Liu Bo | 83eea1f | 2012-07-10 05:28:39 -0600 | [diff] [blame] | 2060 | bool do_list = !btrfs_is_free_space_inode(inode); |
Chris Mason | bcbfce8 | 2008-04-22 13:26:47 -0400 | [diff] [blame] | 2061 | |
Josef Bacik | 8b62f87 | 2017-10-19 14:15:55 -0400 | [diff] [blame] | 2062 | spin_lock(&inode->lock); |
| 2063 | btrfs_mod_outstanding_extents(inode, -num_extents); |
| 2064 | spin_unlock(&inode->lock); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 2065 | |
Josef Bacik | b6d08f0 | 2013-09-27 14:57:43 -0400 | [diff] [blame] | 2066 | /* |
| 2067 | * We don't reserve metadata space for space cache inodes so we |
Andrea Gelmini | 52042d8 | 2018-11-28 12:05:13 +0100 | [diff] [blame] | 2068 | * don't need to call delalloc_release_metadata if there is an |
Josef Bacik | b6d08f0 | 2013-09-27 14:57:43 -0400 | [diff] [blame] | 2069 | * error. |
| 2070 | */ |
Filipe Manana | a315e68 | 2017-03-06 23:04:20 +0000 | [diff] [blame] | 2071 | if (*bits & EXTENT_CLEAR_META_RESV && |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2072 | root != fs_info->tree_root) |
Qu Wenruo | 43b1859 | 2017-12-12 15:34:32 +0800 | [diff] [blame] | 2073 | btrfs_delalloc_release_metadata(inode, len, false); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 2074 | |
Josef Bacik | 6a3891c | 2015-03-16 17:38:52 -0400 | [diff] [blame] | 2075 | /* For sanity tests. */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2076 | if (btrfs_is_testing(fs_info)) |
Josef Bacik | 6a3891c | 2015-03-16 17:38:52 -0400 | [diff] [blame] | 2077 | return; |
| 2078 | |
Filipe Manana | a315e68 | 2017-03-06 23:04:20 +0000 | [diff] [blame] | 2079 | if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID && |
| 2080 | do_list && !(state->state & EXTENT_NORESERVE) && |
| 2081 | (*bits & EXTENT_CLEAR_DATA_RESV)) |
Nikolay Borisov | 9db5d51 | 2020-06-03 08:55:38 +0300 | [diff] [blame] | 2082 | btrfs_free_reserved_data_space_noquota(fs_info, len); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 2083 | |
Nikolay Borisov | 104b4e5 | 2017-06-20 21:01:20 +0300 | [diff] [blame] | 2084 | percpu_counter_add_batch(&fs_info->delalloc_bytes, -len, |
| 2085 | fs_info->delalloc_batch); |
Nikolay Borisov | 6fc0ef6 | 2017-02-20 13:51:03 +0200 | [diff] [blame] | 2086 | spin_lock(&inode->lock); |
| 2087 | inode->delalloc_bytes -= len; |
| 2088 | if (do_list && inode->delalloc_bytes == 0 && |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 2089 | test_bit(BTRFS_INODE_IN_DELALLOC_LIST, |
Nikolay Borisov | 9e3e97f | 2017-02-20 13:51:07 +0200 | [diff] [blame] | 2090 | &inode->runtime_flags)) |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 2091 | btrfs_del_delalloc_inode(root, inode); |
Nikolay Borisov | 6fc0ef6 | 2017-02-20 13:51:03 +0200 | [diff] [blame] | 2092 | spin_unlock(&inode->lock); |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 2093 | } |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 2094 | |
| 2095 | if ((state->state & EXTENT_DELALLOC_NEW) && |
| 2096 | (*bits & EXTENT_DELALLOC_NEW)) { |
| 2097 | spin_lock(&inode->lock); |
| 2098 | ASSERT(inode->new_delalloc_bytes >= len); |
| 2099 | inode->new_delalloc_bytes -= len; |
| 2100 | spin_unlock(&inode->lock); |
| 2101 | } |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 2102 | } |
| 2103 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2104 | /* |
Nikolay Borisov | da12fe5 | 2018-11-27 20:57:58 +0200 | [diff] [blame] | 2105 | * btrfs_bio_fits_in_stripe - Checks whether the size of the given bio will fit |
| 2106 | * in a chunk's stripe. This function ensures that bios do not span a |
| 2107 | * stripe/chunk |
Liu Bo | 6f034ec | 2016-06-22 18:31:49 -0700 | [diff] [blame] | 2108 | * |
Nikolay Borisov | da12fe5 | 2018-11-27 20:57:58 +0200 | [diff] [blame] | 2109 | * @page - The page we are about to add to the bio |
| 2110 | * @size - size we want to add to the bio |
| 2111 | * @bio - bio we want to ensure is smaller than a stripe |
| 2112 | * @bio_flags - flags of the bio |
| 2113 | * |
| 2114 | * return 1 if page cannot be added to the bio |
| 2115 | * return 0 if page can be added to the bio |
Liu Bo | 6f034ec | 2016-06-22 18:31:49 -0700 | [diff] [blame] | 2116 | * return error otherwise |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2117 | */ |
Nikolay Borisov | da12fe5 | 2018-11-27 20:57:58 +0200 | [diff] [blame] | 2118 | int btrfs_bio_fits_in_stripe(struct page *page, size_t size, struct bio *bio, |
| 2119 | unsigned long bio_flags) |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 2120 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2121 | struct inode *inode = page->mapping->host; |
| 2122 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 2123 | u64 logical = (u64)bio->bi_iter.bi_sector << 9; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 2124 | u64 length = 0; |
| 2125 | u64 map_length; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 2126 | int ret; |
Nikolay Borisov | 89b798a | 2019-06-03 12:05:05 +0300 | [diff] [blame] | 2127 | struct btrfs_io_geometry geom; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 2128 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 2129 | if (bio_flags & EXTENT_BIO_COMPRESSED) |
| 2130 | return 0; |
| 2131 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 2132 | length = bio->bi_iter.bi_size; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 2133 | map_length = length; |
Nikolay Borisov | 89b798a | 2019-06-03 12:05:05 +0300 | [diff] [blame] | 2134 | ret = btrfs_get_io_geometry(fs_info, btrfs_op(bio), logical, map_length, |
| 2135 | &geom); |
Liu Bo | 6f034ec | 2016-06-22 18:31:49 -0700 | [diff] [blame] | 2136 | if (ret < 0) |
| 2137 | return ret; |
Nikolay Borisov | 89b798a | 2019-06-03 12:05:05 +0300 | [diff] [blame] | 2138 | |
| 2139 | if (geom.len < length + size) |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 2140 | return 1; |
Jeff Mahoney | 3444a97 | 2011-10-03 23:23:13 -0400 | [diff] [blame] | 2141 | return 0; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 2142 | } |
| 2143 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2144 | /* |
| 2145 | * in order to insert checksums into the metadata in large chunks, |
| 2146 | * we wait until bio submission time. All the pages in the bio are |
| 2147 | * checksummed and sums are attached onto the ordered extent record. |
| 2148 | * |
| 2149 | * At IO completion time the cums attached on the ordered extent record |
| 2150 | * are inserted into the btree |
| 2151 | */ |
David Sterba | d0ee393 | 2018-03-08 14:35:48 +0100 | [diff] [blame] | 2152 | static blk_status_t btrfs_submit_bio_start(void *private_data, struct bio *bio, |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 2153 | u64 bio_offset) |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 2154 | { |
Josef Bacik | c6100a4 | 2017-05-05 11:57:13 -0400 | [diff] [blame] | 2155 | struct inode *inode = private_data; |
Chris Mason | e015640 | 2008-04-16 11:15:20 -0400 | [diff] [blame] | 2156 | |
Johannes Thumshirn | c965d64 | 2020-07-28 20:25:41 +0900 | [diff] [blame] | 2157 | return btrfs_csum_one_bio(BTRFS_I(inode), bio, 0, 0); |
Chris Mason | 4a69a41 | 2008-11-06 22:03:00 -0500 | [diff] [blame] | 2158 | } |
Chris Mason | e015640 | 2008-04-16 11:15:20 -0400 | [diff] [blame] | 2159 | |
Chris Mason | 4a69a41 | 2008-11-06 22:03:00 -0500 | [diff] [blame] | 2160 | /* |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 2161 | * 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] | 2162 | * on write, or reading the csums from the tree before a read. |
| 2163 | * |
| 2164 | * Rules about async/sync submit, |
| 2165 | * a) read: sync submit |
| 2166 | * |
| 2167 | * b) write without checksum: sync submit |
| 2168 | * |
| 2169 | * c) write with checksum: |
| 2170 | * c-1) if bio is issued by fsync: sync submit |
| 2171 | * (sync_writers != 0) |
| 2172 | * |
| 2173 | * c-2) if root is reloc root: sync submit |
| 2174 | * (only in case of buffered IO) |
| 2175 | * |
| 2176 | * c-3) otherwise: async submit |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2177 | */ |
Nikolay Borisov | 908930f | 2020-09-18 16:34:37 +0300 | [diff] [blame] | 2178 | blk_status_t btrfs_submit_data_bio(struct inode *inode, struct bio *bio, |
| 2179 | int mirror_num, unsigned long bio_flags) |
Nikolay Borisov | 50489a5 | 2019-04-10 19:46:04 +0300 | [diff] [blame] | 2180 | |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 2181 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2182 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 2183 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chandan Rajendra | 0d51e28 | 2015-07-27 15:26:43 +0530 | [diff] [blame] | 2184 | enum btrfs_wq_endio_type metadata = BTRFS_WQ_ENDIO_DATA; |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 2185 | blk_status_t ret = 0; |
Chris Mason | 19b9bdb | 2008-10-30 14:23:13 -0400 | [diff] [blame] | 2186 | int skip_sum; |
Josef Bacik | b812ce2 | 2012-11-16 13:56:32 -0500 | [diff] [blame] | 2187 | int async = !atomic_read(&BTRFS_I(inode)->sync_writers); |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 2188 | |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 2189 | skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM; |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 2190 | |
Nikolay Borisov | 70ddc55 | 2017-02-20 13:50:35 +0200 | [diff] [blame] | 2191 | if (btrfs_is_free_space_inode(BTRFS_I(inode))) |
Chandan Rajendra | 0d51e28 | 2015-07-27 15:26:43 +0530 | [diff] [blame] | 2192 | metadata = BTRFS_WQ_ENDIO_FREE_SPACE; |
Jeff Mahoney | 0417341 | 2011-10-03 23:23:12 -0400 | [diff] [blame] | 2193 | |
Mike Christie | 37226b2 | 2016-06-05 14:31:52 -0500 | [diff] [blame] | 2194 | if (bio_op(bio) != REQ_OP_WRITE) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2195 | ret = btrfs_bio_wq_end_io(fs_info, bio, metadata); |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2196 | if (ret) |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 2197 | goto out; |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2198 | |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 2199 | if (bio_flags & EXTENT_BIO_COMPRESSED) { |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 2200 | ret = btrfs_submit_compressed_read(inode, bio, |
| 2201 | mirror_num, |
| 2202 | bio_flags); |
| 2203 | goto out; |
Tsutomu Itoh | c2db107 | 2011-03-01 06:48:31 +0000 | [diff] [blame] | 2204 | } else if (!skip_sum) { |
Omar Sandoval | db72e47 | 2019-12-10 10:37:35 -0800 | [diff] [blame] | 2205 | ret = btrfs_lookup_bio_sums(inode, bio, (u64)-1, NULL); |
Tsutomu Itoh | c2db107 | 2011-03-01 06:48:31 +0000 | [diff] [blame] | 2206 | if (ret) |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 2207 | goto out; |
Tsutomu Itoh | c2db107 | 2011-03-01 06:48:31 +0000 | [diff] [blame] | 2208 | } |
Chris Mason | 4d1b5fb | 2008-08-20 09:44:52 -0400 | [diff] [blame] | 2209 | goto mapit; |
Josef Bacik | b812ce2 | 2012-11-16 13:56:32 -0500 | [diff] [blame] | 2210 | } else if (async && !skip_sum) { |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 2211 | /* csum items have already been cloned */ |
| 2212 | if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID) |
| 2213 | goto mapit; |
Chris Mason | 19b9bdb | 2008-10-30 14:23:13 -0400 | [diff] [blame] | 2214 | /* we're doing a write, do the async checksumming */ |
Josef Bacik | c6100a4 | 2017-05-05 11:57:13 -0400 | [diff] [blame] | 2215 | ret = btrfs_wq_submit_bio(fs_info, bio, mirror_num, bio_flags, |
Nikolay Borisov | e768116 | 2019-04-10 17:24:41 +0300 | [diff] [blame] | 2216 | 0, inode, btrfs_submit_bio_start); |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 2217 | goto out; |
Josef Bacik | b812ce2 | 2012-11-16 13:56:32 -0500 | [diff] [blame] | 2218 | } else if (!skip_sum) { |
Nikolay Borisov | bd242a0 | 2020-06-03 08:55:07 +0300 | [diff] [blame] | 2219 | ret = btrfs_csum_one_bio(BTRFS_I(inode), bio, 0, 0); |
Josef Bacik | b812ce2 | 2012-11-16 13:56:32 -0500 | [diff] [blame] | 2220 | if (ret) |
| 2221 | goto out; |
Chris Mason | 19b9bdb | 2008-10-30 14:23:13 -0400 | [diff] [blame] | 2222 | } |
| 2223 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2224 | mapit: |
Chris Mason | 08635ba | 2019-07-10 12:28:14 -0700 | [diff] [blame] | 2225 | ret = btrfs_map_bio(fs_info, bio, mirror_num); |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 2226 | |
| 2227 | out: |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 2228 | if (ret) { |
| 2229 | bio->bi_status = ret; |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 2230 | bio_endio(bio); |
| 2231 | } |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 2232 | return ret; |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 2233 | } |
Chris Mason | 6885f30 | 2008-02-20 16:11:05 -0500 | [diff] [blame] | 2234 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2235 | /* |
| 2236 | * given a list of ordered sums record them in the inode. This happens |
| 2237 | * at IO completion time based on sums calculated at bio submission time. |
| 2238 | */ |
Nikolay Borisov | 510f85e | 2020-09-18 12:15:52 +0300 | [diff] [blame] | 2239 | static int add_pending_csums(struct btrfs_trans_handle *trans, |
| 2240 | struct list_head *list) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2241 | { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2242 | struct btrfs_ordered_sum *sum; |
Nikolay Borisov | ac01f26 | 2018-01-08 10:59:43 +0200 | [diff] [blame] | 2243 | int ret; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2244 | |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 2245 | list_for_each_entry(sum, list, list) { |
David Sterba | 7c2871a | 2017-11-08 01:07:43 +0100 | [diff] [blame] | 2246 | trans->adding_csums = true; |
Nikolay Borisov | 510f85e | 2020-09-18 12:15:52 +0300 | [diff] [blame] | 2247 | ret = btrfs_csum_file_blocks(trans, trans->fs_info->csum_root, sum); |
David Sterba | 7c2871a | 2017-11-08 01:07:43 +0100 | [diff] [blame] | 2248 | trans->adding_csums = false; |
Nikolay Borisov | ac01f26 | 2018-01-08 10:59:43 +0200 | [diff] [blame] | 2249 | if (ret) |
| 2250 | return ret; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2251 | } |
| 2252 | return 0; |
| 2253 | } |
| 2254 | |
Filipe Manana | c334730 | 2020-11-04 11:07:31 +0000 | [diff] [blame] | 2255 | static int btrfs_find_new_delalloc_bytes(struct btrfs_inode *inode, |
| 2256 | const u64 start, |
| 2257 | const u64 len, |
| 2258 | struct extent_state **cached_state) |
| 2259 | { |
| 2260 | u64 search_start = start; |
| 2261 | const u64 end = start + len - 1; |
| 2262 | |
| 2263 | while (search_start < end) { |
| 2264 | const u64 search_len = end - search_start + 1; |
| 2265 | struct extent_map *em; |
| 2266 | u64 em_len; |
| 2267 | int ret = 0; |
| 2268 | |
| 2269 | em = btrfs_get_extent(inode, NULL, 0, search_start, search_len); |
| 2270 | if (IS_ERR(em)) |
| 2271 | return PTR_ERR(em); |
| 2272 | |
| 2273 | if (em->block_start != EXTENT_MAP_HOLE) |
| 2274 | goto next; |
| 2275 | |
| 2276 | em_len = em->len; |
| 2277 | if (em->start < search_start) |
| 2278 | em_len -= search_start - em->start; |
| 2279 | if (em_len > search_len) |
| 2280 | em_len = search_len; |
| 2281 | |
| 2282 | ret = set_extent_bit(&inode->io_tree, search_start, |
| 2283 | search_start + em_len - 1, |
| 2284 | EXTENT_DELALLOC_NEW, |
| 2285 | NULL, cached_state, GFP_NOFS); |
| 2286 | next: |
| 2287 | search_start = extent_map_end(em); |
| 2288 | free_extent_map(em); |
| 2289 | if (ret) |
| 2290 | return ret; |
| 2291 | } |
| 2292 | return 0; |
| 2293 | } |
| 2294 | |
Nikolay Borisov | c2566f2 | 2020-06-03 08:55:35 +0300 | [diff] [blame] | 2295 | 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] | 2296 | unsigned int extra_bits, |
Nikolay Borisov | 330a582 | 2019-07-17 16:18:17 +0300 | [diff] [blame] | 2297 | struct extent_state **cached_state) |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 2298 | { |
Johannes Thumshirn | fdb1e12 | 2018-12-05 15:23:04 +0100 | [diff] [blame] | 2299 | WARN_ON(PAGE_ALIGNED(end)); |
Filipe Manana | c334730 | 2020-11-04 11:07:31 +0000 | [diff] [blame] | 2300 | |
| 2301 | if (start >= i_size_read(&inode->vfs_inode) && |
| 2302 | !(inode->flags & BTRFS_INODE_PREALLOC)) { |
| 2303 | /* |
| 2304 | * There can't be any extents following eof in this case so just |
| 2305 | * set the delalloc new bit for the range directly. |
| 2306 | */ |
| 2307 | extra_bits |= EXTENT_DELALLOC_NEW; |
| 2308 | } else { |
| 2309 | int ret; |
| 2310 | |
| 2311 | ret = btrfs_find_new_delalloc_bytes(inode, start, |
| 2312 | end + 1 - start, |
| 2313 | cached_state); |
| 2314 | if (ret) |
| 2315 | return ret; |
| 2316 | } |
| 2317 | |
Nikolay Borisov | c2566f2 | 2020-06-03 08:55:35 +0300 | [diff] [blame] | 2318 | return set_extent_delalloc(&inode->io_tree, start, end, extra_bits, |
| 2319 | cached_state); |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 2320 | } |
| 2321 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2322 | /* see btrfs_writepage_start_hook for details on why this is required */ |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2323 | struct btrfs_writepage_fixup { |
| 2324 | struct page *page; |
Josef Bacik | f4b1363 | 2020-01-21 14:34:52 -0500 | [diff] [blame] | 2325 | struct inode *inode; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2326 | struct btrfs_work work; |
| 2327 | }; |
| 2328 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 2329 | static void btrfs_writepage_fixup_worker(struct btrfs_work *work) |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2330 | { |
| 2331 | struct btrfs_writepage_fixup *fixup; |
| 2332 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 2333 | struct extent_state *cached_state = NULL; |
Qu Wenruo | 364ecf3 | 2017-02-27 15:10:38 +0800 | [diff] [blame] | 2334 | struct extent_changeset *data_reserved = NULL; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2335 | struct page *page; |
Nikolay Borisov | 65d87f7 | 2020-06-05 10:51:51 +0300 | [diff] [blame] | 2336 | struct btrfs_inode *inode; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2337 | u64 page_start; |
| 2338 | u64 page_end; |
Chris Mason | 25f3c50 | 2020-01-21 11:51:42 -0500 | [diff] [blame] | 2339 | int ret = 0; |
Josef Bacik | f4b1363 | 2020-01-21 14:34:52 -0500 | [diff] [blame] | 2340 | bool free_delalloc_space = true; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2341 | |
| 2342 | fixup = container_of(work, struct btrfs_writepage_fixup, work); |
| 2343 | page = fixup->page; |
Nikolay Borisov | 65d87f7 | 2020-06-05 10:51:51 +0300 | [diff] [blame] | 2344 | inode = BTRFS_I(fixup->inode); |
Josef Bacik | f4b1363 | 2020-01-21 14:34:52 -0500 | [diff] [blame] | 2345 | page_start = page_offset(page); |
| 2346 | page_end = page_offset(page) + PAGE_SIZE - 1; |
| 2347 | |
| 2348 | /* |
| 2349 | * This is similar to page_mkwrite, we need to reserve the space before |
| 2350 | * we take the page lock. |
| 2351 | */ |
Nikolay Borisov | 65d87f7 | 2020-06-05 10:51:51 +0300 | [diff] [blame] | 2352 | ret = btrfs_delalloc_reserve_space(inode, &data_reserved, page_start, |
| 2353 | PAGE_SIZE); |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 2354 | again: |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2355 | lock_page(page); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2356 | |
Chris Mason | 25f3c50 | 2020-01-21 11:51:42 -0500 | [diff] [blame] | 2357 | /* |
| 2358 | * Before we queued this fixup, we took a reference on the page. |
| 2359 | * page->mapping may go NULL, but it shouldn't be moved to a different |
| 2360 | * address space. |
| 2361 | */ |
Josef Bacik | f4b1363 | 2020-01-21 14:34:52 -0500 | [diff] [blame] | 2362 | if (!page->mapping || !PageDirty(page) || !PageChecked(page)) { |
| 2363 | /* |
| 2364 | * Unfortunately this is a little tricky, either |
| 2365 | * |
| 2366 | * 1) We got here and our page had already been dealt with and |
| 2367 | * we reserved our space, thus ret == 0, so we need to just |
| 2368 | * drop our space reservation and bail. This can happen the |
| 2369 | * first time we come into the fixup worker, or could happen |
| 2370 | * while waiting for the ordered extent. |
| 2371 | * 2) Our page was already dealt with, but we happened to get an |
| 2372 | * ENOSPC above from the btrfs_delalloc_reserve_space. In |
| 2373 | * this case we obviously don't have anything to release, but |
| 2374 | * because the page was already dealt with we don't want to |
| 2375 | * mark the page with an error, so make sure we're resetting |
| 2376 | * ret to 0. This is why we have this check _before_ the ret |
| 2377 | * check, because we do not want to have a surprise ENOSPC |
| 2378 | * when the page was already properly dealt with. |
| 2379 | */ |
| 2380 | if (!ret) { |
Nikolay Borisov | 65d87f7 | 2020-06-05 10:51:51 +0300 | [diff] [blame] | 2381 | btrfs_delalloc_release_extents(inode, PAGE_SIZE); |
| 2382 | btrfs_delalloc_release_space(inode, data_reserved, |
Josef Bacik | f4b1363 | 2020-01-21 14:34:52 -0500 | [diff] [blame] | 2383 | page_start, PAGE_SIZE, |
| 2384 | true); |
| 2385 | } |
| 2386 | ret = 0; |
Chris Mason | 25f3c50 | 2020-01-21 11:51:42 -0500 | [diff] [blame] | 2387 | goto out_page; |
Josef Bacik | f4b1363 | 2020-01-21 14:34:52 -0500 | [diff] [blame] | 2388 | } |
Chris Mason | 25f3c50 | 2020-01-21 11:51:42 -0500 | [diff] [blame] | 2389 | |
| 2390 | /* |
Josef Bacik | f4b1363 | 2020-01-21 14:34:52 -0500 | [diff] [blame] | 2391 | * We can't mess with the page state unless it is locked, so now that |
| 2392 | * it is locked bail if we failed to make our space reservation. |
Chris Mason | 25f3c50 | 2020-01-21 11:51:42 -0500 | [diff] [blame] | 2393 | */ |
Josef Bacik | f4b1363 | 2020-01-21 14:34:52 -0500 | [diff] [blame] | 2394 | if (ret) |
| 2395 | goto out_page; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2396 | |
Nikolay Borisov | 65d87f7 | 2020-06-05 10:51:51 +0300 | [diff] [blame] | 2397 | lock_extent_bits(&inode->io_tree, page_start, page_end, &cached_state); |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 2398 | |
| 2399 | /* already ordered? We're done */ |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 2400 | if (PagePrivate2(page)) |
Josef Bacik | f4b1363 | 2020-01-21 14:34:52 -0500 | [diff] [blame] | 2401 | goto out_reserved; |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 2402 | |
Nikolay Borisov | 65d87f7 | 2020-06-05 10:51:51 +0300 | [diff] [blame] | 2403 | ordered = btrfs_lookup_ordered_range(inode, page_start, PAGE_SIZE); |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 2404 | if (ordered) { |
Nikolay Borisov | 65d87f7 | 2020-06-05 10:51:51 +0300 | [diff] [blame] | 2405 | unlock_extent_cached(&inode->io_tree, page_start, page_end, |
| 2406 | &cached_state); |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 2407 | unlock_page(page); |
Nikolay Borisov | c0a4360 | 2020-09-18 12:15:53 +0300 | [diff] [blame] | 2408 | btrfs_start_ordered_extent(ordered, 1); |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 2409 | btrfs_put_ordered_extent(ordered); |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 2410 | goto again; |
| 2411 | } |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2412 | |
Nikolay Borisov | 65d87f7 | 2020-06-05 10:51:51 +0300 | [diff] [blame] | 2413 | ret = btrfs_set_extent_delalloc(inode, page_start, page_end, 0, |
Nikolay Borisov | 330a582 | 2019-07-17 16:18:17 +0300 | [diff] [blame] | 2414 | &cached_state); |
Chris Mason | 25f3c50 | 2020-01-21 11:51:42 -0500 | [diff] [blame] | 2415 | if (ret) |
Filipe Manana | 5368700 | 2019-10-09 17:43:59 +0100 | [diff] [blame] | 2416 | goto out_reserved; |
Nikolay Borisov | f3038ee | 2017-12-05 09:29:19 +0200 | [diff] [blame] | 2417 | |
Chris Mason | 25f3c50 | 2020-01-21 11:51:42 -0500 | [diff] [blame] | 2418 | /* |
| 2419 | * Everything went as planned, we're now the owner of a dirty page with |
| 2420 | * delayed allocation bits set and space reserved for our COW |
| 2421 | * destination. |
| 2422 | * |
| 2423 | * The page was dirty when we started, nothing should have cleaned it. |
| 2424 | */ |
| 2425 | BUG_ON(!PageDirty(page)); |
Josef Bacik | f4b1363 | 2020-01-21 14:34:52 -0500 | [diff] [blame] | 2426 | free_delalloc_space = false; |
Filipe Manana | 5368700 | 2019-10-09 17:43:59 +0100 | [diff] [blame] | 2427 | out_reserved: |
Nikolay Borisov | 65d87f7 | 2020-06-05 10:51:51 +0300 | [diff] [blame] | 2428 | btrfs_delalloc_release_extents(inode, PAGE_SIZE); |
Josef Bacik | f4b1363 | 2020-01-21 14:34:52 -0500 | [diff] [blame] | 2429 | if (free_delalloc_space) |
Nikolay Borisov | 65d87f7 | 2020-06-05 10:51:51 +0300 | [diff] [blame] | 2430 | btrfs_delalloc_release_space(inode, data_reserved, page_start, |
| 2431 | PAGE_SIZE, true); |
| 2432 | unlock_extent_cached(&inode->io_tree, page_start, page_end, |
David Sterba | e43bbe5 | 2017-12-12 21:43:52 +0100 | [diff] [blame] | 2433 | &cached_state); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2434 | out_page: |
Chris Mason | 25f3c50 | 2020-01-21 11:51:42 -0500 | [diff] [blame] | 2435 | if (ret) { |
| 2436 | /* |
| 2437 | * We hit ENOSPC or other errors. Update the mapping and page |
| 2438 | * to reflect the errors and clean the page. |
| 2439 | */ |
| 2440 | mapping_set_error(page->mapping, ret); |
| 2441 | end_extent_writepage(page, ret, page_start, page_end); |
| 2442 | clear_page_dirty_for_io(page); |
| 2443 | SetPageError(page); |
| 2444 | } |
| 2445 | ClearPageChecked(page); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2446 | unlock_page(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2447 | put_page(page); |
Miao Xie | b897abe | 2011-01-26 16:19:22 +0800 | [diff] [blame] | 2448 | kfree(fixup); |
Qu Wenruo | 364ecf3 | 2017-02-27 15:10:38 +0800 | [diff] [blame] | 2449 | extent_changeset_free(data_reserved); |
Josef Bacik | f4b1363 | 2020-01-21 14:34:52 -0500 | [diff] [blame] | 2450 | /* |
| 2451 | * As a precaution, do a delayed iput in case it would be the last iput |
| 2452 | * that could need flushing space. Recursing back to fixup worker would |
| 2453 | * deadlock. |
| 2454 | */ |
Nikolay Borisov | 65d87f7 | 2020-06-05 10:51:51 +0300 | [diff] [blame] | 2455 | btrfs_add_delayed_iput(&inode->vfs_inode); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2456 | } |
| 2457 | |
| 2458 | /* |
| 2459 | * There are a few paths in the higher layers of the kernel that directly |
| 2460 | * set the page dirty bit without asking the filesystem if it is a |
| 2461 | * good idea. This causes problems because we want to make sure COW |
| 2462 | * properly happens and the data=ordered rules are followed. |
| 2463 | * |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2464 | * 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] | 2465 | * hasn't been properly setup for IO. We kick off an async process |
| 2466 | * to fix it up. The async helper will wait for ordered extents, set |
| 2467 | * the delalloc bit and make it safe to write the page. |
| 2468 | */ |
Nikolay Borisov | d75855b | 2018-11-01 14:09:47 +0200 | [diff] [blame] | 2469 | int btrfs_writepage_cow_fixup(struct page *page, u64 start, u64 end) |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2470 | { |
| 2471 | struct inode *inode = page->mapping->host; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2472 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2473 | struct btrfs_writepage_fixup *fixup; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2474 | |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 2475 | /* this page is properly in the ordered list */ |
| 2476 | if (TestClearPagePrivate2(page)) |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2477 | return 0; |
| 2478 | |
Chris Mason | 25f3c50 | 2020-01-21 11:51:42 -0500 | [diff] [blame] | 2479 | /* |
| 2480 | * PageChecked is set below when we create a fixup worker for this page, |
| 2481 | * don't try to create another one if we're already PageChecked() |
| 2482 | * |
| 2483 | * The extent_io writepage code will redirty the page if we send back |
| 2484 | * EAGAIN. |
| 2485 | */ |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2486 | if (PageChecked(page)) |
| 2487 | return -EAGAIN; |
| 2488 | |
| 2489 | fixup = kzalloc(sizeof(*fixup), GFP_NOFS); |
| 2490 | if (!fixup) |
| 2491 | return -EAGAIN; |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 2492 | |
Josef Bacik | f4b1363 | 2020-01-21 14:34:52 -0500 | [diff] [blame] | 2493 | /* |
| 2494 | * We are already holding a reference to this inode from |
| 2495 | * write_cache_pages. We need to hold it because the space reservation |
| 2496 | * takes place outside of the page lock, and we can't trust |
| 2497 | * page->mapping outside of the page lock. |
| 2498 | */ |
| 2499 | ihold(inode); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2500 | SetPageChecked(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2501 | get_page(page); |
Omar Sandoval | a0cac0e | 2019-09-16 11:30:57 -0700 | [diff] [blame] | 2502 | btrfs_init_work(&fixup->work, btrfs_writepage_fixup_worker, NULL, NULL); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2503 | fixup->page = page; |
Josef Bacik | f4b1363 | 2020-01-21 14:34:52 -0500 | [diff] [blame] | 2504 | fixup->inode = inode; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2505 | btrfs_queue_work(fs_info->fixup_workers, &fixup->work); |
Chris Mason | 25f3c50 | 2020-01-21 11:51:42 -0500 | [diff] [blame] | 2506 | |
| 2507 | return -EAGAIN; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2508 | } |
| 2509 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2510 | static int insert_reserved_file_extent(struct btrfs_trans_handle *trans, |
Nikolay Borisov | c553f94 | 2020-06-03 08:55:19 +0300 | [diff] [blame] | 2511 | struct btrfs_inode *inode, u64 file_pos, |
Qu Wenruo | 9729f10 | 2020-06-10 09:04:41 +0800 | [diff] [blame] | 2512 | struct btrfs_file_extent_item *stack_fi, |
| 2513 | u64 qgroup_reserved) |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2514 | { |
Nikolay Borisov | c553f94 | 2020-06-03 08:55:19 +0300 | [diff] [blame] | 2515 | struct btrfs_root *root = inode->root; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2516 | struct btrfs_path *path; |
| 2517 | struct extent_buffer *leaf; |
| 2518 | struct btrfs_key ins; |
Qu Wenruo | 203f44c5 | 2020-06-10 09:04:40 +0800 | [diff] [blame] | 2519 | u64 disk_num_bytes = btrfs_stack_file_extent_disk_num_bytes(stack_fi); |
| 2520 | u64 disk_bytenr = btrfs_stack_file_extent_disk_bytenr(stack_fi); |
| 2521 | u64 num_bytes = btrfs_stack_file_extent_num_bytes(stack_fi); |
| 2522 | u64 ram_bytes = btrfs_stack_file_extent_ram_bytes(stack_fi); |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 2523 | int extent_inserted = 0; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2524 | int ret; |
| 2525 | |
| 2526 | path = btrfs_alloc_path(); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 2527 | if (!path) |
| 2528 | return -ENOMEM; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2529 | |
Chris Mason | a1ed835 | 2009-09-11 12:27:37 -0400 | [diff] [blame] | 2530 | /* |
| 2531 | * we may be replacing one extent in the tree with another. |
| 2532 | * The new extent is pinned in the extent map, and we don't want |
| 2533 | * to drop it from the cache until it is completely in the btree. |
| 2534 | * |
| 2535 | * So, tell btrfs_drop_extents to leave this extent in the cache. |
| 2536 | * the caller is expected to unpin it and allow it to be merged |
| 2537 | * with the others. |
| 2538 | */ |
Nikolay Borisov | c553f94 | 2020-06-03 08:55:19 +0300 | [diff] [blame] | 2539 | ret = __btrfs_drop_extents(trans, root, inode, path, file_pos, |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 2540 | file_pos + num_bytes, NULL, 0, |
Qu Wenruo | 203f44c5 | 2020-06-10 09:04:40 +0800 | [diff] [blame] | 2541 | 1, sizeof(*stack_fi), &extent_inserted); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2542 | if (ret) |
| 2543 | goto out; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2544 | |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 2545 | if (!extent_inserted) { |
Nikolay Borisov | c553f94 | 2020-06-03 08:55:19 +0300 | [diff] [blame] | 2546 | ins.objectid = btrfs_ino(inode); |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 2547 | ins.offset = file_pos; |
| 2548 | ins.type = BTRFS_EXTENT_DATA_KEY; |
| 2549 | |
| 2550 | path->leave_spinning = 1; |
| 2551 | ret = btrfs_insert_empty_item(trans, root, path, &ins, |
Qu Wenruo | 203f44c5 | 2020-06-10 09:04:40 +0800 | [diff] [blame] | 2552 | sizeof(*stack_fi)); |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 2553 | if (ret) |
| 2554 | goto out; |
| 2555 | } |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2556 | leaf = path->nodes[0]; |
Qu Wenruo | 203f44c5 | 2020-06-10 09:04:40 +0800 | [diff] [blame] | 2557 | btrfs_set_stack_file_extent_generation(stack_fi, trans->transid); |
| 2558 | write_extent_buffer(leaf, stack_fi, |
| 2559 | btrfs_item_ptr_offset(leaf, path->slots[0]), |
| 2560 | sizeof(struct btrfs_file_extent_item)); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 2561 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2562 | btrfs_mark_buffer_dirty(leaf); |
Josef Bacik | ce19533 | 2012-09-25 15:26:16 -0400 | [diff] [blame] | 2563 | btrfs_release_path(path); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2564 | |
Nikolay Borisov | c553f94 | 2020-06-03 08:55:19 +0300 | [diff] [blame] | 2565 | inode_add_bytes(&inode->vfs_inode, num_bytes); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2566 | |
| 2567 | ins.objectid = disk_bytenr; |
| 2568 | ins.offset = disk_num_bytes; |
| 2569 | ins.type = BTRFS_EXTENT_ITEM_KEY; |
Qu Wenruo | a12b877 | 2017-02-27 15:10:37 +0800 | [diff] [blame] | 2570 | |
Nikolay Borisov | c553f94 | 2020-06-03 08:55:19 +0300 | [diff] [blame] | 2571 | ret = btrfs_inode_set_file_extent_range(inode, file_pos, ram_bytes); |
Josef Bacik | 9ddc959 | 2020-01-17 09:02:22 -0500 | [diff] [blame] | 2572 | if (ret) |
| 2573 | goto out; |
| 2574 | |
Nikolay Borisov | c553f94 | 2020-06-03 08:55:19 +0300 | [diff] [blame] | 2575 | ret = btrfs_alloc_reserved_file_extent(trans, root, btrfs_ino(inode), |
Qu Wenruo | 9729f10 | 2020-06-10 09:04:41 +0800 | [diff] [blame] | 2576 | file_pos, qgroup_reserved, &ins); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2577 | out: |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2578 | btrfs_free_path(path); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 2579 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2580 | return ret; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2581 | } |
| 2582 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2583 | static void btrfs_release_delalloc_bytes(struct btrfs_fs_info *fs_info, |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 2584 | u64 start, u64 len) |
| 2585 | { |
David Sterba | 32da5386 | 2019-10-29 19:20:18 +0100 | [diff] [blame] | 2586 | struct btrfs_block_group *cache; |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 2587 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2588 | cache = btrfs_lookup_block_group(fs_info, start); |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 2589 | ASSERT(cache); |
| 2590 | |
| 2591 | spin_lock(&cache->lock); |
| 2592 | cache->delalloc_bytes -= len; |
| 2593 | spin_unlock(&cache->lock); |
| 2594 | |
| 2595 | btrfs_put_block_group(cache); |
| 2596 | } |
| 2597 | |
Qu Wenruo | 203f44c5 | 2020-06-10 09:04:40 +0800 | [diff] [blame] | 2598 | static int insert_ordered_extent_file_extent(struct btrfs_trans_handle *trans, |
Qu Wenruo | 203f44c5 | 2020-06-10 09:04:40 +0800 | [diff] [blame] | 2599 | struct btrfs_ordered_extent *oe) |
| 2600 | { |
| 2601 | struct btrfs_file_extent_item stack_fi; |
| 2602 | u64 logical_len; |
| 2603 | |
| 2604 | memset(&stack_fi, 0, sizeof(stack_fi)); |
| 2605 | btrfs_set_stack_file_extent_type(&stack_fi, BTRFS_FILE_EXTENT_REG); |
| 2606 | btrfs_set_stack_file_extent_disk_bytenr(&stack_fi, oe->disk_bytenr); |
| 2607 | btrfs_set_stack_file_extent_disk_num_bytes(&stack_fi, |
| 2608 | oe->disk_num_bytes); |
| 2609 | if (test_bit(BTRFS_ORDERED_TRUNCATED, &oe->flags)) |
| 2610 | logical_len = oe->truncated_len; |
| 2611 | else |
| 2612 | logical_len = oe->num_bytes; |
| 2613 | btrfs_set_stack_file_extent_num_bytes(&stack_fi, logical_len); |
| 2614 | btrfs_set_stack_file_extent_ram_bytes(&stack_fi, logical_len); |
| 2615 | btrfs_set_stack_file_extent_compression(&stack_fi, oe->compress_type); |
| 2616 | /* Encryption and other encoding is reserved and all 0 */ |
| 2617 | |
Nikolay Borisov | 3c38c87 | 2020-09-18 12:15:51 +0300 | [diff] [blame] | 2618 | return insert_reserved_file_extent(trans, BTRFS_I(oe->inode), |
| 2619 | oe->file_offset, &stack_fi, |
| 2620 | oe->qgroup_rsv); |
Qu Wenruo | 203f44c5 | 2020-06-10 09:04:40 +0800 | [diff] [blame] | 2621 | } |
| 2622 | |
| 2623 | /* |
| 2624 | * As ordered data IO finishes, this gets called so we can finish |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2625 | * an ordered extent if the range of bytes in the file it covers are |
| 2626 | * fully written. |
| 2627 | */ |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2628 | static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2629 | { |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2630 | struct inode *inode = ordered_extent->inode; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2631 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2632 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 2633 | struct btrfs_trans_handle *trans = NULL; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2634 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 2635 | struct extent_state *cached_state = NULL; |
Omar Sandoval | bffe633 | 2019-12-02 17:34:19 -0800 | [diff] [blame] | 2636 | u64 start, end; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 2637 | int compress_type = 0; |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 2638 | int ret = 0; |
Omar Sandoval | bffe633 | 2019-12-02 17:34:19 -0800 | [diff] [blame] | 2639 | u64 logical_len = ordered_extent->num_bytes; |
Nikolay Borisov | 8d51012 | 2019-10-08 20:43:06 +0300 | [diff] [blame] | 2640 | bool freespace_inode; |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 2641 | bool truncated = false; |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 2642 | bool range_locked = false; |
| 2643 | bool clear_new_delalloc_bytes = false; |
Josef Bacik | 49940bd | 2018-10-11 15:54:21 -0400 | [diff] [blame] | 2644 | bool clear_reserved_extent = true; |
Omar Sandoval | 313facc | 2019-12-02 17:34:18 -0800 | [diff] [blame] | 2645 | unsigned int clear_bits; |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 2646 | |
Omar Sandoval | bffe633 | 2019-12-02 17:34:19 -0800 | [diff] [blame] | 2647 | start = ordered_extent->file_offset; |
| 2648 | end = start + ordered_extent->num_bytes - 1; |
| 2649 | |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 2650 | if (!test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) && |
| 2651 | !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags) && |
| 2652 | !test_bit(BTRFS_ORDERED_DIRECT, &ordered_extent->flags)) |
| 2653 | clear_new_delalloc_bytes = true; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2654 | |
Nikolay Borisov | 8d51012 | 2019-10-08 20:43:06 +0300 | [diff] [blame] | 2655 | freespace_inode = btrfs_is_free_space_inode(BTRFS_I(inode)); |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 2656 | |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2657 | if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) { |
| 2658 | ret = -EIO; |
| 2659 | goto out; |
| 2660 | } |
| 2661 | |
Omar Sandoval | bffe633 | 2019-12-02 17:34:19 -0800 | [diff] [blame] | 2662 | btrfs_free_io_failure_record(BTRFS_I(inode), start, end); |
Miao Xie | f612496 | 2014-09-12 18:44:04 +0800 | [diff] [blame] | 2663 | |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 2664 | if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) { |
| 2665 | truncated = true; |
| 2666 | logical_len = ordered_extent->truncated_len; |
| 2667 | /* Truncated the entire extent, don't bother adding */ |
| 2668 | if (!logical_len) |
| 2669 | goto out; |
| 2670 | } |
| 2671 | |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 2672 | if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) { |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2673 | BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */ |
Qu Wenruo | 94ed938 | 2015-09-08 17:25:56 +0800 | [diff] [blame] | 2674 | |
Josef Bacik | d923afe | 2020-01-17 09:02:23 -0500 | [diff] [blame] | 2675 | btrfs_inode_safe_disk_i_size_write(inode, 0); |
Nikolay Borisov | 8d51012 | 2019-10-08 20:43:06 +0300 | [diff] [blame] | 2676 | if (freespace_inode) |
| 2677 | trans = btrfs_join_transaction_spacecache(root); |
Josef Bacik | 6c760c0 | 2012-11-09 10:53:21 -0500 | [diff] [blame] | 2678 | else |
| 2679 | trans = btrfs_join_transaction(root); |
| 2680 | if (IS_ERR(trans)) { |
| 2681 | ret = PTR_ERR(trans); |
| 2682 | trans = NULL; |
| 2683 | goto out; |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 2684 | } |
Josef Bacik | 69fe2d7 | 2017-10-19 14:15:57 -0400 | [diff] [blame] | 2685 | trans->block_rsv = &BTRFS_I(inode)->block_rsv; |
Josef Bacik | 6c760c0 | 2012-11-09 10:53:21 -0500 | [diff] [blame] | 2686 | ret = btrfs_update_inode_fallback(trans, root, inode); |
| 2687 | if (ret) /* -ENOMEM or corruption */ |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 2688 | btrfs_abort_transaction(trans, ret); |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 2689 | goto out; |
| 2690 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2691 | |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 2692 | range_locked = true; |
Omar Sandoval | bffe633 | 2019-12-02 17:34:19 -0800 | [diff] [blame] | 2693 | lock_extent_bits(io_tree, start, end, &cached_state); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2694 | |
Nikolay Borisov | 8d51012 | 2019-10-08 20:43:06 +0300 | [diff] [blame] | 2695 | if (freespace_inode) |
| 2696 | trans = btrfs_join_transaction_spacecache(root); |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 2697 | else |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 2698 | trans = btrfs_join_transaction(root); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2699 | if (IS_ERR(trans)) { |
| 2700 | ret = PTR_ERR(trans); |
| 2701 | trans = NULL; |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 2702 | goto out; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2703 | } |
Chris Mason | a79b7d4 | 2014-05-22 16:18:52 -0700 | [diff] [blame] | 2704 | |
Josef Bacik | 69fe2d7 | 2017-10-19 14:15:57 -0400 | [diff] [blame] | 2705 | trans->block_rsv = &BTRFS_I(inode)->block_rsv; |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 2706 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2707 | if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags)) |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 2708 | compress_type = ordered_extent->compress_type; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2709 | if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) { |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 2710 | BUG_ON(compress_type); |
Nikolay Borisov | 7a6d706 | 2017-02-20 13:50:48 +0200 | [diff] [blame] | 2711 | ret = btrfs_mark_extent_written(trans, BTRFS_I(inode), |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2712 | ordered_extent->file_offset, |
| 2713 | ordered_extent->file_offset + |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 2714 | logical_len); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2715 | } else { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2716 | BUG_ON(root == fs_info->tree_root); |
Nikolay Borisov | 3c38c87 | 2020-09-18 12:15:51 +0300 | [diff] [blame] | 2717 | ret = insert_ordered_extent_file_extent(trans, ordered_extent); |
Josef Bacik | 49940bd | 2018-10-11 15:54:21 -0400 | [diff] [blame] | 2718 | if (!ret) { |
| 2719 | clear_reserved_extent = false; |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2720 | btrfs_release_delalloc_bytes(fs_info, |
Omar Sandoval | bffe633 | 2019-12-02 17:34:19 -0800 | [diff] [blame] | 2721 | ordered_extent->disk_bytenr, |
| 2722 | ordered_extent->disk_num_bytes); |
Josef Bacik | 49940bd | 2018-10-11 15:54:21 -0400 | [diff] [blame] | 2723 | } |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2724 | } |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 2725 | unpin_extent_cache(&BTRFS_I(inode)->extent_tree, |
Omar Sandoval | bffe633 | 2019-12-02 17:34:19 -0800 | [diff] [blame] | 2726 | ordered_extent->file_offset, |
| 2727 | ordered_extent->num_bytes, trans->transid); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2728 | if (ret < 0) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 2729 | btrfs_abort_transaction(trans, ret); |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 2730 | goto out; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2731 | } |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 2732 | |
Nikolay Borisov | 510f85e | 2020-09-18 12:15:52 +0300 | [diff] [blame] | 2733 | ret = add_pending_csums(trans, &ordered_extent->list); |
Nikolay Borisov | ac01f26 | 2018-01-08 10:59:43 +0200 | [diff] [blame] | 2734 | if (ret) { |
| 2735 | btrfs_abort_transaction(trans, ret); |
| 2736 | goto out; |
| 2737 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2738 | |
Josef Bacik | d923afe | 2020-01-17 09:02:23 -0500 | [diff] [blame] | 2739 | btrfs_inode_safe_disk_i_size_write(inode, 0); |
Josef Bacik | 6c760c0 | 2012-11-09 10:53:21 -0500 | [diff] [blame] | 2740 | ret = btrfs_update_inode_fallback(trans, root, inode); |
| 2741 | if (ret) { /* -ENOMEM or corruption */ |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 2742 | btrfs_abort_transaction(trans, ret); |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 2743 | goto out; |
Josef Bacik | 1ef30be | 2011-04-05 19:25:36 -0400 | [diff] [blame] | 2744 | } |
| 2745 | ret = 0; |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 2746 | out: |
Omar Sandoval | 313facc | 2019-12-02 17:34:18 -0800 | [diff] [blame] | 2747 | clear_bits = EXTENT_DEFRAG; |
| 2748 | if (range_locked) |
| 2749 | clear_bits |= EXTENT_LOCKED; |
| 2750 | if (clear_new_delalloc_bytes) |
| 2751 | clear_bits |= EXTENT_DELALLOC_NEW; |
Omar Sandoval | bffe633 | 2019-12-02 17:34:19 -0800 | [diff] [blame] | 2752 | clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, clear_bits, |
| 2753 | (clear_bits & EXTENT_LOCKED) ? 1 : 0, 0, |
Omar Sandoval | 313facc | 2019-12-02 17:34:18 -0800 | [diff] [blame] | 2754 | &cached_state); |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 2755 | |
Miao Xie | a698d075 | 2012-09-20 01:51:59 -0600 | [diff] [blame] | 2756 | if (trans) |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 2757 | btrfs_end_transaction(trans); |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 2758 | |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 2759 | if (ret || truncated) { |
Omar Sandoval | bffe633 | 2019-12-02 17:34:19 -0800 | [diff] [blame] | 2760 | u64 unwritten_start = start; |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 2761 | |
Josef Bacik | b547a16 | 2021-05-19 09:38:27 -0400 | [diff] [blame] | 2762 | /* |
| 2763 | * If we failed to finish this ordered extent for any reason we |
| 2764 | * need to make sure BTRFS_ORDERED_IOERR is set on the ordered |
| 2765 | * extent, and mark the inode with the error if it wasn't |
| 2766 | * already set. Any error during writeback would have already |
| 2767 | * set the mapping error, so we need to set it if we're the ones |
| 2768 | * marking this ordered extent as failed. |
| 2769 | */ |
| 2770 | if (ret && !test_and_set_bit(BTRFS_ORDERED_IOERR, |
| 2771 | &ordered_extent->flags)) |
| 2772 | mapping_set_error(ordered_extent->inode->i_mapping, -EIO); |
| 2773 | |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 2774 | if (truncated) |
Omar Sandoval | bffe633 | 2019-12-02 17:34:19 -0800 | [diff] [blame] | 2775 | unwritten_start += logical_len; |
| 2776 | clear_extent_uptodate(io_tree, unwritten_start, end, NULL); |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 2777 | |
| 2778 | /* 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] | 2779 | btrfs_drop_extent_cache(BTRFS_I(inode), unwritten_start, end, 0); |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2780 | |
Josef Bacik | 0bec9ef | 2013-01-31 14:58:00 -0500 | [diff] [blame] | 2781 | /* |
| 2782 | * If the ordered extent had an IOERR or something else went |
| 2783 | * wrong we need to return the space for this ordered extent |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 2784 | * back to the allocator. We only free the extent in the |
| 2785 | * truncated case if we didn't write out the extent at all. |
Josef Bacik | 49940bd | 2018-10-11 15:54:21 -0400 | [diff] [blame] | 2786 | * |
| 2787 | * If we made it past insert_reserved_file_extent before we |
| 2788 | * errored out then we don't need to do this as the accounting |
| 2789 | * has already been done. |
Josef Bacik | 0bec9ef | 2013-01-31 14:58:00 -0500 | [diff] [blame] | 2790 | */ |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 2791 | if ((ret || !logical_len) && |
Josef Bacik | 49940bd | 2018-10-11 15:54:21 -0400 | [diff] [blame] | 2792 | clear_reserved_extent && |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 2793 | !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) && |
Nikolay Borisov | 4eaaec2 | 2019-11-21 14:03:29 +0200 | [diff] [blame] | 2794 | !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) { |
| 2795 | /* |
| 2796 | * Discard the range before returning it back to the |
| 2797 | * free space pool |
| 2798 | */ |
Dennis Zhou | 46b27f5 | 2019-12-13 16:22:11 -0800 | [diff] [blame] | 2799 | if (ret && btrfs_test_opt(fs_info, DISCARD_SYNC)) |
Nikolay Borisov | 4eaaec2 | 2019-11-21 14:03:29 +0200 | [diff] [blame] | 2800 | btrfs_discard_extent(fs_info, |
Omar Sandoval | bffe633 | 2019-12-02 17:34:19 -0800 | [diff] [blame] | 2801 | ordered_extent->disk_bytenr, |
| 2802 | ordered_extent->disk_num_bytes, |
| 2803 | NULL); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2804 | btrfs_free_reserved_extent(fs_info, |
Omar Sandoval | bffe633 | 2019-12-02 17:34:19 -0800 | [diff] [blame] | 2805 | ordered_extent->disk_bytenr, |
| 2806 | ordered_extent->disk_num_bytes, 1); |
Nikolay Borisov | 4eaaec2 | 2019-11-21 14:03:29 +0200 | [diff] [blame] | 2807 | } |
Josef Bacik | 0bec9ef | 2013-01-31 14:58:00 -0500 | [diff] [blame] | 2808 | } |
| 2809 | |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2810 | /* |
Liu Bo | 8bad3c0 | 2012-06-18 12:14:23 +0800 | [diff] [blame] | 2811 | * This needs to be done to make sure anybody waiting knows we are done |
| 2812 | * updating everything for this ordered extent. |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2813 | */ |
Nikolay Borisov | 71fe0a5 | 2020-09-18 12:15:50 +0300 | [diff] [blame] | 2814 | btrfs_remove_ordered_extent(BTRFS_I(inode), ordered_extent); |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2815 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2816 | /* once for us */ |
| 2817 | btrfs_put_ordered_extent(ordered_extent); |
| 2818 | /* once for the tree */ |
| 2819 | btrfs_put_ordered_extent(ordered_extent); |
| 2820 | |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2821 | return ret; |
| 2822 | } |
| 2823 | |
| 2824 | static void finish_ordered_fn(struct btrfs_work *work) |
| 2825 | { |
| 2826 | struct btrfs_ordered_extent *ordered_extent; |
| 2827 | ordered_extent = container_of(work, struct btrfs_ordered_extent, work); |
| 2828 | btrfs_finish_ordered_io(ordered_extent); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2829 | } |
| 2830 | |
Nikolay Borisov | c629732 | 2018-11-08 10:18:08 +0200 | [diff] [blame] | 2831 | void btrfs_writepage_endio_finish_ordered(struct page *page, u64 start, |
| 2832 | u64 end, int uptodate) |
Chris Mason | 211f90e | 2008-07-18 11:56:15 -0400 | [diff] [blame] | 2833 | { |
Nikolay Borisov | 3347c48 | 2020-08-31 14:42:44 +0300 | [diff] [blame] | 2834 | struct btrfs_inode *inode = BTRFS_I(page->mapping->host); |
| 2835 | struct btrfs_fs_info *fs_info = inode->root->fs_info; |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2836 | struct btrfs_ordered_extent *ordered_extent = NULL; |
Liu Bo | 9e0af23 | 2014-08-15 23:36:53 +0800 | [diff] [blame] | 2837 | struct btrfs_workqueue *wq; |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2838 | |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 2839 | trace_btrfs_writepage_end_io_hook(page, start, end, uptodate); |
| 2840 | |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 2841 | ClearPagePrivate2(page); |
Nikolay Borisov | 3347c48 | 2020-08-31 14:42:44 +0300 | [diff] [blame] | 2842 | if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start, |
| 2843 | end - start + 1, uptodate)) |
David Sterba | c3988d6 | 2017-02-17 15:18:32 +0100 | [diff] [blame] | 2844 | return; |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2845 | |
Nikolay Borisov | 3347c48 | 2020-08-31 14:42:44 +0300 | [diff] [blame] | 2846 | if (btrfs_is_free_space_inode(inode)) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2847 | wq = fs_info->endio_freespace_worker; |
Omar Sandoval | a0cac0e | 2019-09-16 11:30:57 -0700 | [diff] [blame] | 2848 | else |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2849 | wq = fs_info->endio_write_workers; |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2850 | |
Omar Sandoval | a0cac0e | 2019-09-16 11:30:57 -0700 | [diff] [blame] | 2851 | btrfs_init_work(&ordered_extent->work, finish_ordered_fn, NULL, NULL); |
Liu Bo | 9e0af23 | 2014-08-15 23:36:53 +0800 | [diff] [blame] | 2852 | btrfs_queue_work(wq, &ordered_extent->work); |
Chris Mason | 211f90e | 2008-07-18 11:56:15 -0400 | [diff] [blame] | 2853 | } |
| 2854 | |
Omar Sandoval | 47df776 | 2020-04-16 14:46:17 -0700 | [diff] [blame] | 2855 | static int check_data_csum(struct inode *inode, struct btrfs_io_bio *io_bio, |
| 2856 | int icsum, struct page *page, int pgoff, u64 start, |
| 2857 | size_t len) |
Miao Xie | dc380ae | 2014-09-12 18:43:55 +0800 | [diff] [blame] | 2858 | { |
Johannes Thumshirn | d517857 | 2019-06-03 16:58:57 +0200 | [diff] [blame] | 2859 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
| 2860 | SHASH_DESC_ON_STACK(shash, fs_info->csum_shash); |
Miao Xie | dc380ae | 2014-09-12 18:43:55 +0800 | [diff] [blame] | 2861 | char *kaddr; |
Johannes Thumshirn | d517857 | 2019-06-03 16:58:57 +0200 | [diff] [blame] | 2862 | u16 csum_size = btrfs_super_csum_size(fs_info->super_copy); |
| 2863 | u8 *csum_expected; |
| 2864 | u8 csum[BTRFS_CSUM_SIZE]; |
Miao Xie | dc380ae | 2014-09-12 18:43:55 +0800 | [diff] [blame] | 2865 | |
Johannes Thumshirn | d517857 | 2019-06-03 16:58:57 +0200 | [diff] [blame] | 2866 | csum_expected = ((u8 *)io_bio->csum) + icsum * csum_size; |
Miao Xie | dc380ae | 2014-09-12 18:43:55 +0800 | [diff] [blame] | 2867 | |
| 2868 | kaddr = kmap_atomic(page); |
Johannes Thumshirn | d517857 | 2019-06-03 16:58:57 +0200 | [diff] [blame] | 2869 | shash->tfm = fs_info->csum_shash; |
| 2870 | |
Eric Biggers | fd08001 | 2020-04-30 23:51:59 -0700 | [diff] [blame] | 2871 | crypto_shash_digest(shash, kaddr + pgoff, len, csum); |
Johannes Thumshirn | d517857 | 2019-06-03 16:58:57 +0200 | [diff] [blame] | 2872 | |
| 2873 | if (memcmp(csum, csum_expected, csum_size)) |
Miao Xie | dc380ae | 2014-09-12 18:43:55 +0800 | [diff] [blame] | 2874 | goto zeroit; |
| 2875 | |
| 2876 | kunmap_atomic(kaddr); |
| 2877 | return 0; |
| 2878 | zeroit: |
Johannes Thumshirn | ea41d6b | 2019-06-03 16:58:58 +0200 | [diff] [blame] | 2879 | btrfs_print_data_csum_error(BTRFS_I(inode), start, csum, csum_expected, |
| 2880 | io_bio->mirror_num); |
Nikolay Borisov | 814723e | 2020-07-02 15:23:32 +0300 | [diff] [blame] | 2881 | if (io_bio->device) |
| 2882 | btrfs_dev_stat_inc_and_print(io_bio->device, |
| 2883 | BTRFS_DEV_STAT_CORRUPTION_ERRS); |
Miao Xie | dc380ae | 2014-09-12 18:43:55 +0800 | [diff] [blame] | 2884 | memset(kaddr + pgoff, 1, len); |
| 2885 | flush_dcache_page(page); |
| 2886 | kunmap_atomic(kaddr); |
Miao Xie | dc380ae | 2014-09-12 18:43:55 +0800 | [diff] [blame] | 2887 | return -EIO; |
| 2888 | } |
| 2889 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2890 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2891 | * 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] | 2892 | * if there's a match, we allow the bio to finish. If not, the code in |
| 2893 | * extent_io.c will try to find good copies for us. |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2894 | */ |
Nikolay Borisov | 9a446d6 | 2020-09-18 16:34:33 +0300 | [diff] [blame] | 2895 | int btrfs_verify_data_csum(struct btrfs_io_bio *io_bio, u64 phy_offset, |
| 2896 | struct page *page, u64 start, u64 end, int mirror) |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 2897 | { |
Miao Xie | 4eee4fa | 2012-12-21 09:17:45 +0000 | [diff] [blame] | 2898 | size_t offset = start - page_offset(page); |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 2899 | struct inode *inode = page->mapping->host; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2900 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | ff79f81 | 2007-10-15 16:22:25 -0400 | [diff] [blame] | 2901 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2902 | |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 2903 | if (PageChecked(page)) { |
| 2904 | ClearPageChecked(page); |
Miao Xie | dc380ae | 2014-09-12 18:43:55 +0800 | [diff] [blame] | 2905 | return 0; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 2906 | } |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 2907 | |
| 2908 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM) |
Miao Xie | dc380ae | 2014-09-12 18:43:55 +0800 | [diff] [blame] | 2909 | return 0; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 2910 | |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 2911 | if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID && |
Chris Mason | 9655d29 | 2009-09-02 15:22:30 -0400 | [diff] [blame] | 2912 | test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) { |
David Sterba | 9116621 | 2016-04-26 23:54:39 +0200 | [diff] [blame] | 2913 | clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM); |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 2914 | return 0; |
| 2915 | } |
| 2916 | |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 2917 | phy_offset >>= inode->i_sb->s_blocksize_bits; |
Omar Sandoval | 47df776 | 2020-04-16 14:46:17 -0700 | [diff] [blame] | 2918 | return check_data_csum(inode, io_bio, phy_offset, page, offset, start, |
| 2919 | (size_t)(end - start + 1)); |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 2920 | } |
Chris Mason | b888db2b | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 2921 | |
Nikolay Borisov | c1c3fac | 2018-01-16 09:31:58 +0200 | [diff] [blame] | 2922 | /* |
| 2923 | * btrfs_add_delayed_iput - perform a delayed iput on @inode |
| 2924 | * |
| 2925 | * @inode: The inode we want to perform iput on |
| 2926 | * |
| 2927 | * This function uses the generic vfs_inode::i_count to track whether we should |
| 2928 | * just decrement it (in case it's > 1) or if this is the last iput then link |
| 2929 | * the inode to the delayed iput machinery. Delayed iputs are processed at |
| 2930 | * transaction commit time/superblock commit/cleaner kthread. |
| 2931 | */ |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 2932 | void btrfs_add_delayed_iput(struct inode *inode) |
| 2933 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2934 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
David Sterba | 8089fe6 | 2015-11-19 14:15:51 +0100 | [diff] [blame] | 2935 | struct btrfs_inode *binode = BTRFS_I(inode); |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 2936 | |
| 2937 | if (atomic_add_unless(&inode->i_count, -1, 1)) |
| 2938 | return; |
| 2939 | |
Josef Bacik | 034f784 | 2018-12-03 11:06:52 -0500 | [diff] [blame] | 2940 | atomic_inc(&fs_info->nr_delayed_iputs); |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 2941 | spin_lock(&fs_info->delayed_iput_lock); |
Nikolay Borisov | c1c3fac | 2018-01-16 09:31:58 +0200 | [diff] [blame] | 2942 | ASSERT(list_empty(&binode->delayed_iput)); |
| 2943 | list_add_tail(&binode->delayed_iput, &fs_info->delayed_iputs); |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 2944 | spin_unlock(&fs_info->delayed_iput_lock); |
Josef Bacik | fd340d0 | 2019-01-11 10:21:02 -0500 | [diff] [blame] | 2945 | if (!test_bit(BTRFS_FS_CLEANER_RUNNING, &fs_info->flags)) |
| 2946 | wake_up_process(fs_info->cleaner_kthread); |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 2947 | } |
| 2948 | |
Josef Bacik | 63611e7 | 2019-06-18 10:59:18 -0400 | [diff] [blame] | 2949 | static void run_delayed_iput_locked(struct btrfs_fs_info *fs_info, |
| 2950 | struct btrfs_inode *inode) |
| 2951 | { |
| 2952 | list_del_init(&inode->delayed_iput); |
| 2953 | spin_unlock(&fs_info->delayed_iput_lock); |
| 2954 | iput(&inode->vfs_inode); |
| 2955 | if (atomic_dec_and_test(&fs_info->nr_delayed_iputs)) |
| 2956 | wake_up(&fs_info->delayed_iputs_wait); |
| 2957 | spin_lock(&fs_info->delayed_iput_lock); |
| 2958 | } |
| 2959 | |
| 2960 | static void btrfs_run_delayed_iput(struct btrfs_fs_info *fs_info, |
| 2961 | struct btrfs_inode *inode) |
| 2962 | { |
| 2963 | if (!list_empty(&inode->delayed_iput)) { |
| 2964 | spin_lock(&fs_info->delayed_iput_lock); |
| 2965 | if (!list_empty(&inode->delayed_iput)) |
| 2966 | run_delayed_iput_locked(fs_info, inode); |
| 2967 | spin_unlock(&fs_info->delayed_iput_lock); |
| 2968 | } |
| 2969 | } |
| 2970 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2971 | void btrfs_run_delayed_iputs(struct btrfs_fs_info *fs_info) |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 2972 | { |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 2973 | |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 2974 | spin_lock(&fs_info->delayed_iput_lock); |
David Sterba | 8089fe6 | 2015-11-19 14:15:51 +0100 | [diff] [blame] | 2975 | while (!list_empty(&fs_info->delayed_iputs)) { |
| 2976 | struct btrfs_inode *inode; |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 2977 | |
David Sterba | 8089fe6 | 2015-11-19 14:15:51 +0100 | [diff] [blame] | 2978 | inode = list_first_entry(&fs_info->delayed_iputs, |
| 2979 | struct btrfs_inode, delayed_iput); |
Josef Bacik | 63611e7 | 2019-06-18 10:59:18 -0400 | [diff] [blame] | 2980 | run_delayed_iput_locked(fs_info, inode); |
Josef Bacik | 56001dd | 2021-04-29 10:51:34 -0400 | [diff] [blame] | 2981 | cond_resched_lock(&fs_info->delayed_iput_lock); |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 2982 | } |
David Sterba | 8089fe6 | 2015-11-19 14:15:51 +0100 | [diff] [blame] | 2983 | spin_unlock(&fs_info->delayed_iput_lock); |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 2984 | } |
| 2985 | |
Josef Bacik | 034f784 | 2018-12-03 11:06:52 -0500 | [diff] [blame] | 2986 | /** |
| 2987 | * btrfs_wait_on_delayed_iputs - wait on the delayed iputs to be done running |
| 2988 | * @fs_info - the fs_info for this fs |
| 2989 | * @return - EINTR if we were killed, 0 if nothing's pending |
| 2990 | * |
| 2991 | * This will wait on any delayed iputs that are currently running with KILLABLE |
| 2992 | * set. Once they are all done running we will return, unless we are killed in |
| 2993 | * which case we return EINTR. This helps in user operations like fallocate etc |
| 2994 | * that might get blocked on the iputs. |
| 2995 | */ |
| 2996 | int btrfs_wait_on_delayed_iputs(struct btrfs_fs_info *fs_info) |
| 2997 | { |
| 2998 | int ret = wait_event_killable(fs_info->delayed_iputs_wait, |
| 2999 | atomic_read(&fs_info->nr_delayed_iputs) == 0); |
| 3000 | if (ret) |
| 3001 | return -EINTR; |
| 3002 | return 0; |
| 3003 | } |
| 3004 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3005 | /* |
Omar Sandoval | f7e9e8f | 2018-05-11 13:13:32 -0700 | [diff] [blame] | 3006 | * This creates an orphan entry for the given inode in case something goes wrong |
| 3007 | * in the middle of an unlink. |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3008 | */ |
Nikolay Borisov | 73f2e54 | 2017-02-20 13:50:59 +0200 | [diff] [blame] | 3009 | int btrfs_orphan_add(struct btrfs_trans_handle *trans, |
Omar Sandoval | 2791906 | 2018-05-11 13:13:37 -0700 | [diff] [blame] | 3010 | struct btrfs_inode *inode) |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3011 | { |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3012 | int ret; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3013 | |
Omar Sandoval | 2791906 | 2018-05-11 13:13:37 -0700 | [diff] [blame] | 3014 | ret = btrfs_insert_orphan_item(trans, inode->root, btrfs_ino(inode)); |
| 3015 | if (ret && ret != -EEXIST) { |
| 3016 | btrfs_abort_transaction(trans, ret); |
| 3017 | return ret; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3018 | } |
| 3019 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3020 | return 0; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3021 | } |
| 3022 | |
| 3023 | /* |
Omar Sandoval | f7e9e8f | 2018-05-11 13:13:32 -0700 | [diff] [blame] | 3024 | * We have done the delete so we can go ahead and remove the orphan item for |
| 3025 | * this particular inode. |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3026 | */ |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 3027 | static int btrfs_orphan_del(struct btrfs_trans_handle *trans, |
Nikolay Borisov | 3d6ae7b | 2017-02-20 13:50:58 +0200 | [diff] [blame] | 3028 | struct btrfs_inode *inode) |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3029 | { |
Omar Sandoval | 2791906 | 2018-05-11 13:13:37 -0700 | [diff] [blame] | 3030 | return btrfs_del_orphan_item(trans, inode->root, btrfs_ino(inode)); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3031 | } |
| 3032 | |
| 3033 | /* |
| 3034 | * this cleans up any orphans that may be left on the list from the last use |
| 3035 | * of this root. |
| 3036 | */ |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3037 | int btrfs_orphan_cleanup(struct btrfs_root *root) |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3038 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3039 | struct btrfs_fs_info *fs_info = root->fs_info; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3040 | struct btrfs_path *path; |
| 3041 | struct extent_buffer *leaf; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3042 | struct btrfs_key key, found_key; |
| 3043 | struct btrfs_trans_handle *trans; |
| 3044 | struct inode *inode; |
Josef Bacik | 8f6d7f4 | 2011-09-26 15:55:20 -0400 | [diff] [blame] | 3045 | u64 last_objectid = 0; |
Omar Sandoval | f7e9e8f | 2018-05-11 13:13:32 -0700 | [diff] [blame] | 3046 | int ret = 0, nr_unlink = 0; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3047 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3048 | if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED)) |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3049 | return 0; |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 3050 | |
| 3051 | path = btrfs_alloc_path(); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3052 | if (!path) { |
| 3053 | ret = -ENOMEM; |
| 3054 | goto out; |
| 3055 | } |
David Sterba | e4058b5 | 2015-11-27 16:31:35 +0100 | [diff] [blame] | 3056 | path->reada = READA_BACK; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3057 | |
| 3058 | key.objectid = BTRFS_ORPHAN_OBJECTID; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 3059 | key.type = BTRFS_ORPHAN_ITEM_KEY; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3060 | key.offset = (u64)-1; |
| 3061 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3062 | while (1) { |
| 3063 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3064 | if (ret < 0) |
| 3065 | goto out; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3066 | |
| 3067 | /* |
| 3068 | * 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] | 3069 | * 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] | 3070 | * find the key and see if we have stuff that matches |
| 3071 | */ |
| 3072 | if (ret > 0) { |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3073 | ret = 0; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3074 | if (path->slots[0] == 0) |
| 3075 | break; |
| 3076 | path->slots[0]--; |
| 3077 | } |
| 3078 | |
| 3079 | /* pull out the item */ |
| 3080 | leaf = path->nodes[0]; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3081 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 3082 | |
| 3083 | /* make sure the item matches what we want */ |
| 3084 | if (found_key.objectid != BTRFS_ORPHAN_OBJECTID) |
| 3085 | break; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 3086 | if (found_key.type != BTRFS_ORPHAN_ITEM_KEY) |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3087 | break; |
| 3088 | |
| 3089 | /* release the path since we're done with it */ |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3090 | btrfs_release_path(path); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3091 | |
| 3092 | /* |
| 3093 | * this is where we are basically btrfs_lookup, without the |
| 3094 | * crossing root thing. we store the inode number in the |
| 3095 | * offset of the orphan item. |
| 3096 | */ |
Josef Bacik | 8f6d7f4 | 2011-09-26 15:55:20 -0400 | [diff] [blame] | 3097 | |
| 3098 | if (found_key.offset == last_objectid) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3099 | btrfs_err(fs_info, |
| 3100 | "Error removing orphan entry, stopping orphan cleanup"); |
Josef Bacik | 8f6d7f4 | 2011-09-26 15:55:20 -0400 | [diff] [blame] | 3101 | ret = -EINVAL; |
| 3102 | goto out; |
| 3103 | } |
| 3104 | |
| 3105 | last_objectid = found_key.offset; |
| 3106 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3107 | found_key.objectid = found_key.offset; |
| 3108 | found_key.type = BTRFS_INODE_ITEM_KEY; |
| 3109 | found_key.offset = 0; |
David Sterba | 0202e83 | 2020-05-15 19:35:59 +0200 | [diff] [blame] | 3110 | inode = btrfs_iget(fs_info->sb, last_objectid, root); |
Rusty Russell | 8c6ffba | 2013-07-15 11:20:32 +0930 | [diff] [blame] | 3111 | ret = PTR_ERR_OR_ZERO(inode); |
Filipe Manana | 6771089 | 2016-06-06 11:51:25 +0100 | [diff] [blame] | 3112 | if (ret && ret != -ENOENT) |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3113 | goto out; |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 3114 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3115 | if (ret == -ENOENT && root == fs_info->tree_root) { |
Arne Jansen | f8e9e0b | 2011-12-14 20:12:02 -0500 | [diff] [blame] | 3116 | struct btrfs_root *dead_root; |
Arne Jansen | f8e9e0b | 2011-12-14 20:12:02 -0500 | [diff] [blame] | 3117 | int is_dead_root = 0; |
| 3118 | |
| 3119 | /* |
| 3120 | * this is an orphan in the tree root. Currently these |
| 3121 | * could come from 2 sources: |
| 3122 | * a) a snapshot deletion in progress |
| 3123 | * b) a free space cache inode |
| 3124 | * We need to distinguish those two, as the snapshot |
| 3125 | * orphan must not get deleted. |
| 3126 | * find_dead_roots already ran before us, so if this |
| 3127 | * is a snapshot deletion, we should find the root |
Robbie Ko | a619b3c | 2020-05-07 10:54:40 +0800 | [diff] [blame] | 3128 | * in the fs_roots radix tree. |
Arne Jansen | f8e9e0b | 2011-12-14 20:12:02 -0500 | [diff] [blame] | 3129 | */ |
Robbie Ko | a619b3c | 2020-05-07 10:54:40 +0800 | [diff] [blame] | 3130 | |
| 3131 | spin_lock(&fs_info->fs_roots_radix_lock); |
| 3132 | dead_root = radix_tree_lookup(&fs_info->fs_roots_radix, |
| 3133 | (unsigned long)found_key.objectid); |
| 3134 | if (dead_root && btrfs_root_refs(&dead_root->root_item) == 0) |
| 3135 | is_dead_root = 1; |
| 3136 | spin_unlock(&fs_info->fs_roots_radix_lock); |
| 3137 | |
Arne Jansen | f8e9e0b | 2011-12-14 20:12:02 -0500 | [diff] [blame] | 3138 | if (is_dead_root) { |
| 3139 | /* prevent this orphan from being found again */ |
| 3140 | key.offset = found_key.objectid - 1; |
| 3141 | continue; |
| 3142 | } |
Omar Sandoval | f7e9e8f | 2018-05-11 13:13:32 -0700 | [diff] [blame] | 3143 | |
Arne Jansen | f8e9e0b | 2011-12-14 20:12:02 -0500 | [diff] [blame] | 3144 | } |
Omar Sandoval | f7e9e8f | 2018-05-11 13:13:32 -0700 | [diff] [blame] | 3145 | |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 3146 | /* |
Omar Sandoval | f7e9e8f | 2018-05-11 13:13:32 -0700 | [diff] [blame] | 3147 | * If we have an inode with links, there are a couple of |
| 3148 | * possibilities. Old kernels (before v3.12) used to create an |
| 3149 | * orphan item for truncate indicating that there were possibly |
| 3150 | * extent items past i_size that needed to be deleted. In v3.12, |
| 3151 | * truncate was changed to update i_size in sync with the extent |
| 3152 | * items, but the (useless) orphan item was still created. Since |
| 3153 | * v4.18, we don't create the orphan item for truncate at all. |
| 3154 | * |
| 3155 | * So, this item could mean that we need to do a truncate, but |
| 3156 | * only if this filesystem was last used on a pre-v3.12 kernel |
| 3157 | * and was not cleanly unmounted. The odds of that are quite |
| 3158 | * slim, and it's a pain to do the truncate now, so just delete |
| 3159 | * the orphan item. |
| 3160 | * |
| 3161 | * It's also possible that this orphan item was supposed to be |
| 3162 | * deleted but wasn't. The inode number may have been reused, |
| 3163 | * but either way, we can delete the orphan item. |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 3164 | */ |
Omar Sandoval | f7e9e8f | 2018-05-11 13:13:32 -0700 | [diff] [blame] | 3165 | if (ret == -ENOENT || inode->i_nlink) { |
| 3166 | if (!ret) |
| 3167 | iput(inode); |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 3168 | trans = btrfs_start_transaction(root, 1); |
| 3169 | if (IS_ERR(trans)) { |
| 3170 | ret = PTR_ERR(trans); |
| 3171 | goto out; |
| 3172 | } |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3173 | btrfs_debug(fs_info, "auto deleting %Lu", |
| 3174 | found_key.objectid); |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 3175 | ret = btrfs_del_orphan_item(trans, root, |
| 3176 | found_key.objectid); |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 3177 | btrfs_end_transaction(trans); |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 3178 | if (ret) |
| 3179 | goto out; |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 3180 | continue; |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3181 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3182 | |
Omar Sandoval | f7e9e8f | 2018-05-11 13:13:32 -0700 | [diff] [blame] | 3183 | nr_unlink++; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3184 | |
| 3185 | /* this will do delete_inode and everything for us */ |
| 3186 | iput(inode); |
| 3187 | } |
Miao Xie | 3254c87 | 2011-11-10 20:45:05 -0500 | [diff] [blame] | 3188 | /* release the path since we're done with it */ |
| 3189 | btrfs_release_path(path); |
| 3190 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3191 | root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE; |
| 3192 | |
Omar Sandoval | a575cee | 2018-05-11 13:13:38 -0700 | [diff] [blame] | 3193 | if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) { |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 3194 | trans = btrfs_join_transaction(root); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3195 | if (!IS_ERR(trans)) |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 3196 | btrfs_end_transaction(trans); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3197 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3198 | |
| 3199 | if (nr_unlink) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3200 | btrfs_debug(fs_info, "unlinked %d orphans", nr_unlink); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3201 | |
| 3202 | out: |
| 3203 | if (ret) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3204 | btrfs_err(fs_info, "could not do orphan cleanup %d", ret); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3205 | btrfs_free_path(path); |
| 3206 | return ret; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3207 | } |
| 3208 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3209 | /* |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3210 | * very simple check to peek ahead in the leaf looking for xattrs. If we |
| 3211 | * don't find any xattrs, we know there can't be any acls. |
| 3212 | * |
| 3213 | * slot is the slot the inode is in, objectid is the objectid of the inode |
| 3214 | */ |
| 3215 | 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] | 3216 | int slot, u64 objectid, |
| 3217 | int *first_xattr_slot) |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3218 | { |
| 3219 | u32 nritems = btrfs_header_nritems(leaf); |
| 3220 | struct btrfs_key found_key; |
Josef Bacik | f23b5a5 | 2013-06-19 10:16:26 -0400 | [diff] [blame] | 3221 | static u64 xattr_access = 0; |
| 3222 | static u64 xattr_default = 0; |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3223 | int scanned = 0; |
| 3224 | |
Josef Bacik | f23b5a5 | 2013-06-19 10:16:26 -0400 | [diff] [blame] | 3225 | if (!xattr_access) { |
Andreas Gruenbacher | 97d7929 | 2015-12-02 14:44:35 +0100 | [diff] [blame] | 3226 | xattr_access = btrfs_name_hash(XATTR_NAME_POSIX_ACL_ACCESS, |
| 3227 | strlen(XATTR_NAME_POSIX_ACL_ACCESS)); |
| 3228 | xattr_default = btrfs_name_hash(XATTR_NAME_POSIX_ACL_DEFAULT, |
| 3229 | strlen(XATTR_NAME_POSIX_ACL_DEFAULT)); |
Josef Bacik | f23b5a5 | 2013-06-19 10:16:26 -0400 | [diff] [blame] | 3230 | } |
| 3231 | |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3232 | slot++; |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 3233 | *first_xattr_slot = -1; |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3234 | while (slot < nritems) { |
| 3235 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
| 3236 | |
| 3237 | /* we found a different objectid, there must not be acls */ |
| 3238 | if (found_key.objectid != objectid) |
| 3239 | return 0; |
| 3240 | |
| 3241 | /* we found an xattr, assume we've got an acl */ |
Josef Bacik | f23b5a5 | 2013-06-19 10:16:26 -0400 | [diff] [blame] | 3242 | if (found_key.type == BTRFS_XATTR_ITEM_KEY) { |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 3243 | if (*first_xattr_slot == -1) |
| 3244 | *first_xattr_slot = slot; |
Josef Bacik | f23b5a5 | 2013-06-19 10:16:26 -0400 | [diff] [blame] | 3245 | if (found_key.offset == xattr_access || |
| 3246 | found_key.offset == xattr_default) |
| 3247 | return 1; |
| 3248 | } |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3249 | |
| 3250 | /* |
| 3251 | * we found a key greater than an xattr key, there can't |
| 3252 | * be any acls later on |
| 3253 | */ |
| 3254 | if (found_key.type > BTRFS_XATTR_ITEM_KEY) |
| 3255 | return 0; |
| 3256 | |
| 3257 | slot++; |
| 3258 | scanned++; |
| 3259 | |
| 3260 | /* |
| 3261 | * it goes inode, inode backrefs, xattrs, extents, |
| 3262 | * so if there are a ton of hard links to an inode there can |
| 3263 | * be a lot of backrefs. Don't waste time searching too hard, |
| 3264 | * this is just an optimization |
| 3265 | */ |
| 3266 | if (scanned >= 8) |
| 3267 | break; |
| 3268 | } |
| 3269 | /* we hit the end of the leaf before we found an xattr or |
| 3270 | * something larger than an xattr. We have to assume the inode |
| 3271 | * has acls |
| 3272 | */ |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 3273 | if (*first_xattr_slot == -1) |
| 3274 | *first_xattr_slot = slot; |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3275 | return 1; |
| 3276 | } |
| 3277 | |
| 3278 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3279 | * read an inode from the btree into the in-memory inode |
| 3280 | */ |
Filipe Manana | 4222ea7 | 2018-10-24 10:13:03 +0100 | [diff] [blame] | 3281 | static int btrfs_read_locked_inode(struct inode *inode, |
| 3282 | struct btrfs_path *in_path) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3283 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3284 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Filipe Manana | 4222ea7 | 2018-10-24 10:13:03 +0100 | [diff] [blame] | 3285 | struct btrfs_path *path = in_path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3286 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3287 | struct btrfs_inode_item *inode_item; |
| 3288 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 3289 | struct btrfs_key location; |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 3290 | unsigned long ptr; |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3291 | int maybe_acls; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 3292 | u32 rdev; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3293 | int ret; |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 3294 | bool filled = false; |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 3295 | int first_xattr_slot; |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 3296 | |
| 3297 | ret = btrfs_fill_inode(inode, &rdev); |
| 3298 | if (!ret) |
| 3299 | filled = true; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3300 | |
Filipe Manana | 4222ea7 | 2018-10-24 10:13:03 +0100 | [diff] [blame] | 3301 | if (!path) { |
| 3302 | path = btrfs_alloc_path(); |
| 3303 | if (!path) |
| 3304 | return -ENOMEM; |
| 3305 | } |
Mark Fasheh | 1748f84 | 2011-07-12 11:25:31 -0700 | [diff] [blame] | 3306 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3307 | memcpy(&location, &BTRFS_I(inode)->location, sizeof(location)); |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 3308 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3309 | ret = btrfs_lookup_inode(NULL, root, path, &location, 0); |
Filipe Manana | 6771089 | 2016-06-06 11:51:25 +0100 | [diff] [blame] | 3310 | if (ret) { |
Filipe Manana | 4222ea7 | 2018-10-24 10:13:03 +0100 | [diff] [blame] | 3311 | if (path != in_path) |
| 3312 | btrfs_free_path(path); |
Al Viro | f5b3a41 | 2018-07-29 23:04:51 +0100 | [diff] [blame] | 3313 | return ret; |
Filipe Manana | 6771089 | 2016-06-06 11:51:25 +0100 | [diff] [blame] | 3314 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3315 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3316 | leaf = path->nodes[0]; |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 3317 | |
| 3318 | if (filled) |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 3319 | goto cache_index; |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 3320 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3321 | inode_item = btrfs_item_ptr(leaf, path->slots[0], |
| 3322 | struct btrfs_inode_item); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3323 | inode->i_mode = btrfs_inode_mode(leaf, inode_item); |
Miklos Szeredi | bfe8684 | 2011-10-28 14:13:29 +0200 | [diff] [blame] | 3324 | set_nlink(inode, btrfs_inode_nlink(leaf, inode_item)); |
Eric W. Biederman | 2f2f43d | 2012-02-10 11:05:07 -0800 | [diff] [blame] | 3325 | i_uid_write(inode, btrfs_inode_uid(leaf, inode_item)); |
| 3326 | i_gid_write(inode, btrfs_inode_gid(leaf, inode_item)); |
Nikolay Borisov | 6ef06d2 | 2017-02-20 13:50:34 +0200 | [diff] [blame] | 3327 | 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] | 3328 | btrfs_inode_set_file_extent_range(BTRFS_I(inode), 0, |
| 3329 | round_up(i_size_read(inode), fs_info->sectorsize)); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3330 | |
David Sterba | a937b97 | 2014-12-12 17:39:12 +0100 | [diff] [blame] | 3331 | inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->atime); |
| 3332 | inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->atime); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3333 | |
David Sterba | a937b97 | 2014-12-12 17:39:12 +0100 | [diff] [blame] | 3334 | inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->mtime); |
| 3335 | inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->mtime); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3336 | |
David Sterba | a937b97 | 2014-12-12 17:39:12 +0100 | [diff] [blame] | 3337 | inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->ctime); |
| 3338 | inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->ctime); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3339 | |
chandan r | 9cc97d6 | 2012-07-04 12:48:07 +0530 | [diff] [blame] | 3340 | BTRFS_I(inode)->i_otime.tv_sec = |
| 3341 | btrfs_timespec_sec(leaf, &inode_item->otime); |
| 3342 | BTRFS_I(inode)->i_otime.tv_nsec = |
| 3343 | btrfs_timespec_nsec(leaf, &inode_item->otime); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3344 | |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 3345 | inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item)); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3346 | BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item); |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 3347 | BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item); |
| 3348 | |
Jeff Layton | c7f88c4 | 2017-12-11 06:35:12 -0500 | [diff] [blame] | 3349 | inode_set_iversion_queried(inode, |
| 3350 | btrfs_inode_sequence(leaf, inode_item)); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3351 | inode->i_generation = BTRFS_I(inode)->generation; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 3352 | inode->i_rdev = 0; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3353 | rdev = btrfs_inode_rdev(leaf, inode_item); |
| 3354 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 3355 | BTRFS_I(inode)->index_cnt = (u64)-1; |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 3356 | BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item); |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 3357 | |
| 3358 | cache_index: |
Yang Dongsheng | 6e17d30 | 2015-04-09 12:08:43 +0800 | [diff] [blame] | 3359 | /* |
| 3360 | * If we were modified in the current generation and evicted from memory |
| 3361 | * and then re-read we need to do a full sync since we don't have any |
| 3362 | * idea about which extents were modified before we were evicted from |
| 3363 | * cache. |
| 3364 | * |
| 3365 | * This is required for both inode re-read from disk and delayed inode |
| 3366 | * in delayed_nodes_tree. |
| 3367 | */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3368 | if (BTRFS_I(inode)->last_trans == fs_info->generation) |
Yang Dongsheng | 6e17d30 | 2015-04-09 12:08:43 +0800 | [diff] [blame] | 3369 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, |
| 3370 | &BTRFS_I(inode)->runtime_flags); |
| 3371 | |
Filipe Manana | bde6c24 | 2015-07-24 00:00:19 +0100 | [diff] [blame] | 3372 | /* |
| 3373 | * We don't persist the id of the transaction where an unlink operation |
| 3374 | * against the inode was last made. So here we assume the inode might |
| 3375 | * have been evicted, and therefore the exact value of last_unlink_trans |
| 3376 | * lost, and set it to last_trans to avoid metadata inconsistencies |
| 3377 | * between the inode and its parent if the inode is fsync'ed and the log |
| 3378 | * replayed. For example, in the scenario: |
| 3379 | * |
| 3380 | * touch mydir/foo |
| 3381 | * ln mydir/foo mydir/bar |
| 3382 | * sync |
| 3383 | * unlink mydir/bar |
| 3384 | * echo 2 > /proc/sys/vm/drop_caches # evicts inode |
| 3385 | * xfs_io -c fsync mydir/foo |
| 3386 | * <power failure> |
| 3387 | * mount fs, triggers fsync log replay |
| 3388 | * |
| 3389 | * We must make sure that when we fsync our inode foo we also log its |
| 3390 | * parent inode, otherwise after log replay the parent still has the |
| 3391 | * dentry with the "bar" name but our inode foo has a link count of 1 |
| 3392 | * and doesn't have an inode ref with the name "bar" anymore. |
| 3393 | * |
| 3394 | * Setting last_unlink_trans to last_trans is a pessimistic approach, |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 3395 | * but it guarantees correctness at the expense of occasional full |
Filipe Manana | bde6c24 | 2015-07-24 00:00:19 +0100 | [diff] [blame] | 3396 | * transaction commits on fsync if our inode is a directory, or if our |
| 3397 | * inode is not a directory, logging its parent unnecessarily. |
| 3398 | */ |
| 3399 | BTRFS_I(inode)->last_unlink_trans = BTRFS_I(inode)->last_trans; |
| 3400 | |
Filipe Manana | 3ebac17 | 2020-07-15 12:30:43 +0100 | [diff] [blame] | 3401 | /* |
| 3402 | * Same logic as for last_unlink_trans. We don't persist the generation |
| 3403 | * of the last transaction where this inode was used for a reflink |
| 3404 | * operation, so after eviction and reloading the inode we must be |
| 3405 | * pessimistic and assume the last transaction that modified the inode. |
| 3406 | */ |
| 3407 | BTRFS_I(inode)->last_reflink_trans = BTRFS_I(inode)->last_trans; |
| 3408 | |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 3409 | path->slots[0]++; |
| 3410 | if (inode->i_nlink != 1 || |
| 3411 | path->slots[0] >= btrfs_header_nritems(leaf)) |
| 3412 | goto cache_acl; |
| 3413 | |
| 3414 | btrfs_item_key_to_cpu(leaf, &location, path->slots[0]); |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 3415 | if (location.objectid != btrfs_ino(BTRFS_I(inode))) |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 3416 | goto cache_acl; |
| 3417 | |
| 3418 | ptr = btrfs_item_ptr_offset(leaf, path->slots[0]); |
| 3419 | if (location.type == BTRFS_INODE_REF_KEY) { |
| 3420 | struct btrfs_inode_ref *ref; |
| 3421 | |
| 3422 | ref = (struct btrfs_inode_ref *)ptr; |
| 3423 | BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref); |
| 3424 | } else if (location.type == BTRFS_INODE_EXTREF_KEY) { |
| 3425 | struct btrfs_inode_extref *extref; |
| 3426 | |
| 3427 | extref = (struct btrfs_inode_extref *)ptr; |
| 3428 | BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf, |
| 3429 | extref); |
| 3430 | } |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 3431 | cache_acl: |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3432 | /* |
| 3433 | * try to precache a NULL acl entry for files that don't have |
| 3434 | * any xattrs or acls |
| 3435 | */ |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3436 | maybe_acls = acls_after_inode_item(leaf, path->slots[0], |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 3437 | btrfs_ino(BTRFS_I(inode)), &first_xattr_slot); |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 3438 | if (first_xattr_slot != -1) { |
| 3439 | path->slots[0] = first_xattr_slot; |
| 3440 | ret = btrfs_load_inode_props(inode, path); |
| 3441 | if (ret) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3442 | btrfs_err(fs_info, |
David Sterba | 351fd35 | 2014-05-15 16:48:20 +0200 | [diff] [blame] | 3443 | "error loading props for ino %llu (root %llu): %d", |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 3444 | btrfs_ino(BTRFS_I(inode)), |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 3445 | root->root_key.objectid, ret); |
| 3446 | } |
Filipe Manana | 4222ea7 | 2018-10-24 10:13:03 +0100 | [diff] [blame] | 3447 | if (path != in_path) |
| 3448 | btrfs_free_path(path); |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 3449 | |
Al Viro | 72c0490 | 2009-06-24 16:58:48 -0400 | [diff] [blame] | 3450 | if (!maybe_acls) |
| 3451 | cache_no_acl(inode); |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3452 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3453 | switch (inode->i_mode & S_IFMT) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3454 | case S_IFREG: |
| 3455 | inode->i_mapping->a_ops = &btrfs_aops; |
| 3456 | inode->i_fop = &btrfs_file_operations; |
| 3457 | inode->i_op = &btrfs_file_inode_operations; |
| 3458 | break; |
| 3459 | case S_IFDIR: |
| 3460 | inode->i_fop = &btrfs_dir_file_operations; |
Omar Sandoval | 67ade05 | 2017-01-25 17:06:38 -0800 | [diff] [blame] | 3461 | inode->i_op = &btrfs_dir_inode_operations; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3462 | break; |
| 3463 | case S_IFLNK: |
| 3464 | inode->i_op = &btrfs_symlink_inode_operations; |
Al Viro | 21fc61c | 2015-11-17 01:07:57 -0500 | [diff] [blame] | 3465 | inode_nohighmem(inode); |
Omar Sandoval | 4779cc0 | 2018-09-24 15:16:55 -0700 | [diff] [blame] | 3466 | inode->i_mapping->a_ops = &btrfs_aops; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3467 | break; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 3468 | default: |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 3469 | inode->i_op = &btrfs_special_inode_operations; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 3470 | init_special_inode(inode, inode->i_mode, rdev); |
| 3471 | break; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3472 | } |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 3473 | |
David Sterba | 7b6a221 | 2018-03-26 18:40:21 +0200 | [diff] [blame] | 3474 | btrfs_sync_inode_flags_to_i_flags(inode); |
Filipe Manana | 6771089 | 2016-06-06 11:51:25 +0100 | [diff] [blame] | 3475 | return 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3476 | } |
| 3477 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3478 | /* |
| 3479 | * given a leaf and an inode, copy the inode fields into the leaf |
| 3480 | */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3481 | static void fill_inode_item(struct btrfs_trans_handle *trans, |
| 3482 | struct extent_buffer *leaf, |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3483 | struct btrfs_inode_item *item, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3484 | struct inode *inode) |
| 3485 | { |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3486 | struct btrfs_map_token token; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3487 | |
David Sterba | c82f823 | 2019-08-09 17:48:21 +0200 | [diff] [blame] | 3488 | btrfs_init_map_token(&token, leaf); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3489 | |
David Sterba | cc4c13d | 2020-04-29 02:15:56 +0200 | [diff] [blame] | 3490 | btrfs_set_token_inode_uid(&token, item, i_uid_read(inode)); |
| 3491 | btrfs_set_token_inode_gid(&token, item, i_gid_read(inode)); |
| 3492 | btrfs_set_token_inode_size(&token, item, BTRFS_I(inode)->disk_i_size); |
| 3493 | btrfs_set_token_inode_mode(&token, item, inode->i_mode); |
| 3494 | btrfs_set_token_inode_nlink(&token, item, inode->i_nlink); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3495 | |
David Sterba | cc4c13d | 2020-04-29 02:15:56 +0200 | [diff] [blame] | 3496 | btrfs_set_token_timespec_sec(&token, &item->atime, |
| 3497 | inode->i_atime.tv_sec); |
| 3498 | btrfs_set_token_timespec_nsec(&token, &item->atime, |
| 3499 | inode->i_atime.tv_nsec); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3500 | |
David Sterba | cc4c13d | 2020-04-29 02:15:56 +0200 | [diff] [blame] | 3501 | btrfs_set_token_timespec_sec(&token, &item->mtime, |
| 3502 | inode->i_mtime.tv_sec); |
| 3503 | btrfs_set_token_timespec_nsec(&token, &item->mtime, |
| 3504 | inode->i_mtime.tv_nsec); |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3505 | |
David Sterba | cc4c13d | 2020-04-29 02:15:56 +0200 | [diff] [blame] | 3506 | btrfs_set_token_timespec_sec(&token, &item->ctime, |
| 3507 | inode->i_ctime.tv_sec); |
| 3508 | btrfs_set_token_timespec_nsec(&token, &item->ctime, |
| 3509 | inode->i_ctime.tv_nsec); |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3510 | |
David Sterba | cc4c13d | 2020-04-29 02:15:56 +0200 | [diff] [blame] | 3511 | btrfs_set_token_timespec_sec(&token, &item->otime, |
| 3512 | BTRFS_I(inode)->i_otime.tv_sec); |
| 3513 | btrfs_set_token_timespec_nsec(&token, &item->otime, |
| 3514 | BTRFS_I(inode)->i_otime.tv_nsec); |
chandan r | 9cc97d6 | 2012-07-04 12:48:07 +0530 | [diff] [blame] | 3515 | |
David Sterba | cc4c13d | 2020-04-29 02:15:56 +0200 | [diff] [blame] | 3516 | btrfs_set_token_inode_nbytes(&token, item, inode_get_bytes(inode)); |
| 3517 | btrfs_set_token_inode_generation(&token, item, |
| 3518 | BTRFS_I(inode)->generation); |
| 3519 | btrfs_set_token_inode_sequence(&token, item, inode_peek_iversion(inode)); |
| 3520 | btrfs_set_token_inode_transid(&token, item, trans->transid); |
| 3521 | btrfs_set_token_inode_rdev(&token, item, inode->i_rdev); |
| 3522 | btrfs_set_token_inode_flags(&token, item, BTRFS_I(inode)->flags); |
| 3523 | btrfs_set_token_inode_block_group(&token, item, 0); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3524 | } |
| 3525 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3526 | /* |
| 3527 | * copy everything in the in-memory inode into the btree. |
| 3528 | */ |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 3529 | static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3530 | struct btrfs_root *root, struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3531 | { |
| 3532 | struct btrfs_inode_item *inode_item; |
| 3533 | struct btrfs_path *path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3534 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3535 | int ret; |
| 3536 | |
| 3537 | path = btrfs_alloc_path(); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 3538 | if (!path) |
| 3539 | return -ENOMEM; |
| 3540 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 3541 | path->leave_spinning = 1; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 3542 | ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location, |
| 3543 | 1); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3544 | if (ret) { |
| 3545 | if (ret > 0) |
| 3546 | ret = -ENOENT; |
| 3547 | goto failed; |
| 3548 | } |
| 3549 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3550 | leaf = path->nodes[0]; |
| 3551 | inode_item = btrfs_item_ptr(leaf, path->slots[0], |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 3552 | struct btrfs_inode_item); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3553 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3554 | fill_inode_item(trans, leaf, inode_item, inode); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3555 | btrfs_mark_buffer_dirty(leaf); |
Nikolay Borisov | d909441 | 2020-06-05 10:41:13 +0300 | [diff] [blame] | 3556 | btrfs_set_inode_last_trans(trans, BTRFS_I(inode)); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3557 | ret = 0; |
| 3558 | failed: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3559 | btrfs_free_path(path); |
| 3560 | return ret; |
| 3561 | } |
| 3562 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3563 | /* |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 3564 | * copy everything in the in-memory inode into the btree. |
| 3565 | */ |
| 3566 | noinline int btrfs_update_inode(struct btrfs_trans_handle *trans, |
| 3567 | struct btrfs_root *root, struct inode *inode) |
| 3568 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3569 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 3570 | int ret; |
| 3571 | |
| 3572 | /* |
| 3573 | * If the inode is a free space inode, we can deadlock during commit |
| 3574 | * if we put it into the delayed code. |
| 3575 | * |
| 3576 | * The data relocation inode should also be directly updated |
| 3577 | * without delay |
| 3578 | */ |
Nikolay Borisov | 70ddc55 | 2017-02-20 13:50:35 +0200 | [diff] [blame] | 3579 | if (!btrfs_is_free_space_inode(BTRFS_I(inode)) |
Josef Bacik | 1d52c78 | 2014-09-18 11:30:44 -0400 | [diff] [blame] | 3580 | && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3581 | && !test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) { |
Alexander Block | 8ea05e3 | 2012-07-25 17:35:53 +0200 | [diff] [blame] | 3582 | btrfs_update_root_times(trans, root); |
| 3583 | |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 3584 | ret = btrfs_delayed_update_inode(trans, root, inode); |
| 3585 | if (!ret) |
Nikolay Borisov | d909441 | 2020-06-05 10:41:13 +0300 | [diff] [blame] | 3586 | btrfs_set_inode_last_trans(trans, BTRFS_I(inode)); |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 3587 | return ret; |
| 3588 | } |
| 3589 | |
| 3590 | return btrfs_update_inode_item(trans, root, inode); |
| 3591 | } |
| 3592 | |
Josef Bacik | be6aef6 | 2012-10-22 15:43:12 -0400 | [diff] [blame] | 3593 | noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans, |
| 3594 | struct btrfs_root *root, |
| 3595 | struct inode *inode) |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 3596 | { |
| 3597 | int ret; |
| 3598 | |
| 3599 | ret = btrfs_update_inode(trans, root, inode); |
| 3600 | if (ret == -ENOSPC) |
| 3601 | return btrfs_update_inode_item(trans, root, inode); |
| 3602 | return ret; |
| 3603 | } |
| 3604 | |
| 3605 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3606 | * unlink helper that gets used here in inode.c and in the tree logging |
| 3607 | * recovery code. It remove a link in a directory with a given name, and |
| 3608 | * also drops the back refs in the inode to the directory |
| 3609 | */ |
Al Viro | 9298679 | 2011-03-04 17:14:37 +0000 | [diff] [blame] | 3610 | static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans, |
| 3611 | struct btrfs_root *root, |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 3612 | struct btrfs_inode *dir, |
| 3613 | struct btrfs_inode *inode, |
Al Viro | 9298679 | 2011-03-04 17:14:37 +0000 | [diff] [blame] | 3614 | const char *name, int name_len) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3615 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3616 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3617 | struct btrfs_path *path; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3618 | int ret = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3619 | struct btrfs_dir_item *di; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 3620 | u64 index; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3621 | u64 ino = btrfs_ino(inode); |
| 3622 | u64 dir_ino = btrfs_ino(dir); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3623 | |
| 3624 | path = btrfs_alloc_path(); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3625 | if (!path) { |
| 3626 | ret = -ENOMEM; |
Tsutomu Itoh | 554233a | 2011-02-03 03:16:25 +0000 | [diff] [blame] | 3627 | goto out; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3628 | } |
| 3629 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 3630 | path->leave_spinning = 1; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3631 | di = btrfs_lookup_dir_item(trans, root, path, dir_ino, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3632 | name, name_len, -1); |
Liu Bo | 3cf5068 | 2018-09-12 06:06:26 +0800 | [diff] [blame] | 3633 | if (IS_ERR_OR_NULL(di)) { |
| 3634 | ret = di ? PTR_ERR(di) : -ENOENT; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3635 | goto err; |
| 3636 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3637 | ret = btrfs_delete_one_dir_name(trans, root, path, di); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3638 | if (ret) |
| 3639 | goto err; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3640 | btrfs_release_path(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3641 | |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 3642 | /* |
| 3643 | * If we don't have dir index, we have to get it by looking up |
| 3644 | * the inode ref, since we get the inode ref, remove it directly, |
| 3645 | * it is unnecessary to do delayed deletion. |
| 3646 | * |
| 3647 | * But if we have dir index, needn't search inode ref to get it. |
| 3648 | * Since the inode ref is close to the inode item, it is better |
| 3649 | * that we delay to delete it, and just do this deletion when |
| 3650 | * we update the inode item. |
| 3651 | */ |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 3652 | if (inode->dir_index) { |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 3653 | ret = btrfs_delayed_delete_inode_ref(inode); |
| 3654 | if (!ret) { |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 3655 | index = inode->dir_index; |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 3656 | goto skip_backref; |
| 3657 | } |
| 3658 | } |
| 3659 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3660 | ret = btrfs_del_inode_ref(trans, root, name, name_len, ino, |
| 3661 | dir_ino, &index); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 3662 | if (ret) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3663 | btrfs_info(fs_info, |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 3664 | "failed to delete reference to %.*s, inode %llu parent %llu", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 3665 | name_len, name, ino, dir_ino); |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3666 | btrfs_abort_transaction(trans, ret); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 3667 | goto err; |
| 3668 | } |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 3669 | skip_backref: |
Lu Fengqi | 9add294 | 2018-08-01 11:32:26 +0800 | [diff] [blame] | 3670 | ret = btrfs_delete_delayed_dir_index(trans, dir, index); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3671 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3672 | btrfs_abort_transaction(trans, ret); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3673 | goto err; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3674 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3675 | |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 3676 | ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len, inode, |
| 3677 | dir_ino); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3678 | if (ret != 0 && ret != -ENOENT) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3679 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3680 | goto err; |
| 3681 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3682 | |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 3683 | ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len, dir, |
| 3684 | index); |
Chris Mason | 6418c96 | 2010-10-30 07:34:24 -0400 | [diff] [blame] | 3685 | if (ret == -ENOENT) |
| 3686 | ret = 0; |
Zach Brown | d4e3991 | 2013-04-02 21:02:16 +0000 | [diff] [blame] | 3687 | else if (ret) |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3688 | btrfs_abort_transaction(trans, ret); |
Josef Bacik | 63611e7 | 2019-06-18 10:59:18 -0400 | [diff] [blame] | 3689 | |
| 3690 | /* |
| 3691 | * If we have a pending delayed iput we could end up with the final iput |
| 3692 | * being run in btrfs-cleaner context. If we have enough of these built |
| 3693 | * up we can end up burning a lot of time in btrfs-cleaner without any |
| 3694 | * way to throttle the unlinks. Since we're currently holding a ref on |
| 3695 | * the inode we can run the delayed iput here without any issues as the |
| 3696 | * final iput won't be done until after we drop the ref we're currently |
| 3697 | * holding. |
| 3698 | */ |
| 3699 | btrfs_run_delayed_iput(fs_info, inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3700 | err: |
| 3701 | btrfs_free_path(path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3702 | if (ret) |
| 3703 | goto out; |
| 3704 | |
Nikolay Borisov | 6ef06d2 | 2017-02-20 13:50:34 +0200 | [diff] [blame] | 3705 | 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] | 3706 | inode_inc_iversion(&inode->vfs_inode); |
| 3707 | inode_inc_iversion(&dir->vfs_inode); |
| 3708 | inode->vfs_inode.i_ctime = dir->vfs_inode.i_mtime = |
| 3709 | dir->vfs_inode.i_ctime = current_time(&inode->vfs_inode); |
| 3710 | ret = btrfs_update_inode(trans, root, &dir->vfs_inode); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3711 | out: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3712 | return ret; |
| 3713 | } |
| 3714 | |
Al Viro | 9298679 | 2011-03-04 17:14:37 +0000 | [diff] [blame] | 3715 | int btrfs_unlink_inode(struct btrfs_trans_handle *trans, |
| 3716 | struct btrfs_root *root, |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 3717 | struct btrfs_inode *dir, struct btrfs_inode *inode, |
Al Viro | 9298679 | 2011-03-04 17:14:37 +0000 | [diff] [blame] | 3718 | const char *name, int name_len) |
| 3719 | { |
| 3720 | int ret; |
| 3721 | ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len); |
| 3722 | if (!ret) { |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 3723 | drop_nlink(&inode->vfs_inode); |
| 3724 | ret = btrfs_update_inode(trans, root, &inode->vfs_inode); |
Al Viro | 9298679 | 2011-03-04 17:14:37 +0000 | [diff] [blame] | 3725 | } |
| 3726 | return ret; |
| 3727 | } |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3728 | |
| 3729 | /* |
| 3730 | * helper to start transaction for unlink and rmdir. |
| 3731 | * |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 3732 | * unlink and rmdir are special in btrfs, they do not always free space, so |
| 3733 | * if we cannot make our reservations the normal way try and see if there is |
| 3734 | * plenty of slack room in the global reserve to migrate, otherwise we cannot |
| 3735 | * allow the unlink to occur. |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3736 | */ |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 3737 | static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir) |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3738 | { |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3739 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3740 | |
Josef Bacik | e70bea5 | 2011-10-11 14:18:24 -0400 | [diff] [blame] | 3741 | /* |
| 3742 | * 1 for the possible orphan item |
| 3743 | * 1 for the dir item |
| 3744 | * 1 for the dir index |
| 3745 | * 1 for the inode ref |
Josef Bacik | e70bea5 | 2011-10-11 14:18:24 -0400 | [diff] [blame] | 3746 | * 1 for the inode |
| 3747 | */ |
Josef Bacik | 7f9fe61 | 2020-03-13 15:58:05 -0400 | [diff] [blame] | 3748 | return btrfs_start_transaction_fallback_global_rsv(root, 5); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3749 | } |
| 3750 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3751 | static int btrfs_unlink(struct inode *dir, struct dentry *dentry) |
| 3752 | { |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3753 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3754 | struct btrfs_trans_handle *trans; |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 3755 | struct inode *inode = d_inode(dentry); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3756 | int ret; |
| 3757 | |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 3758 | trans = __unlink_start_trans(dir); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3759 | if (IS_ERR(trans)) |
Josef Bacik | 5df6a9f | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 3760 | return PTR_ERR(trans); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3761 | |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 3762 | btrfs_record_unlink_dir(trans, BTRFS_I(dir), BTRFS_I(d_inode(dentry)), |
| 3763 | 0); |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 3764 | |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 3765 | ret = btrfs_unlink_inode(trans, root, BTRFS_I(dir), |
| 3766 | BTRFS_I(d_inode(dentry)), dentry->d_name.name, |
| 3767 | dentry->d_name.len); |
Tsutomu Itoh | b532402 | 2011-07-19 07:27:20 +0000 | [diff] [blame] | 3768 | if (ret) |
| 3769 | goto out; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3770 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3771 | if (inode->i_nlink == 0) { |
Nikolay Borisov | 73f2e54 | 2017-02-20 13:50:59 +0200 | [diff] [blame] | 3772 | ret = btrfs_orphan_add(trans, BTRFS_I(inode)); |
Tsutomu Itoh | b532402 | 2011-07-19 07:27:20 +0000 | [diff] [blame] | 3773 | if (ret) |
| 3774 | goto out; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3775 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3776 | |
Tsutomu Itoh | b532402 | 2011-07-19 07:27:20 +0000 | [diff] [blame] | 3777 | out: |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 3778 | btrfs_end_transaction(trans); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3779 | btrfs_btree_balance_dirty(root->fs_info); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3780 | return ret; |
| 3781 | } |
| 3782 | |
Misono Tomohiro | f60a236 | 2018-04-18 11:34:52 +0900 | [diff] [blame] | 3783 | static int btrfs_unlink_subvol(struct btrfs_trans_handle *trans, |
Josef Bacik | 045d396 | 2019-12-18 17:20:27 -0500 | [diff] [blame] | 3784 | struct inode *dir, struct dentry *dentry) |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3785 | { |
Lu Fengqi | 401b3b1 | 2018-08-01 11:32:30 +0800 | [diff] [blame] | 3786 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Josef Bacik | 045d396 | 2019-12-18 17:20:27 -0500 | [diff] [blame] | 3787 | struct btrfs_inode *inode = BTRFS_I(d_inode(dentry)); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3788 | struct btrfs_path *path; |
| 3789 | struct extent_buffer *leaf; |
| 3790 | struct btrfs_dir_item *di; |
| 3791 | struct btrfs_key key; |
Josef Bacik | 045d396 | 2019-12-18 17:20:27 -0500 | [diff] [blame] | 3792 | const char *name = dentry->d_name.name; |
| 3793 | int name_len = dentry->d_name.len; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3794 | u64 index; |
| 3795 | int ret; |
Josef Bacik | 045d396 | 2019-12-18 17:20:27 -0500 | [diff] [blame] | 3796 | u64 objectid; |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 3797 | u64 dir_ino = btrfs_ino(BTRFS_I(dir)); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3798 | |
Josef Bacik | 045d396 | 2019-12-18 17:20:27 -0500 | [diff] [blame] | 3799 | if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID) { |
| 3800 | objectid = inode->root->root_key.objectid; |
| 3801 | } else if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) { |
| 3802 | objectid = inode->location.objectid; |
| 3803 | } else { |
| 3804 | WARN_ON(1); |
| 3805 | return -EINVAL; |
| 3806 | } |
| 3807 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3808 | path = btrfs_alloc_path(); |
| 3809 | if (!path) |
| 3810 | return -ENOMEM; |
| 3811 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3812 | di = btrfs_lookup_dir_item(trans, root, path, dir_ino, |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3813 | name, name_len, -1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3814 | if (IS_ERR_OR_NULL(di)) { |
Liu Bo | 3cf5068 | 2018-09-12 06:06:26 +0800 | [diff] [blame] | 3815 | ret = di ? PTR_ERR(di) : -ENOENT; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3816 | goto out; |
| 3817 | } |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3818 | |
| 3819 | leaf = path->nodes[0]; |
| 3820 | btrfs_dir_item_key_to_cpu(leaf, di, &key); |
| 3821 | WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid); |
| 3822 | ret = btrfs_delete_one_dir_name(trans, root, path, di); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3823 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3824 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3825 | goto out; |
| 3826 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3827 | btrfs_release_path(path); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3828 | |
Josef Bacik | d49d328 | 2019-12-18 17:20:28 -0500 | [diff] [blame] | 3829 | /* |
| 3830 | * This is a placeholder inode for a subvolume we didn't have a |
| 3831 | * reference to at the time of the snapshot creation. In the meantime |
| 3832 | * we could have renamed the real subvol link into our snapshot, so |
| 3833 | * depending on btrfs_del_root_ref to return -ENOENT here is incorret. |
| 3834 | * Instead simply lookup the dir_index_item for this entry so we can |
| 3835 | * remove it. Otherwise we know we have a ref to the root and we can |
| 3836 | * call btrfs_del_root_ref, and it _shouldn't_ fail. |
| 3837 | */ |
| 3838 | if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) { |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3839 | di = btrfs_search_dir_index_item(root, path, dir_ino, |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3840 | name, name_len); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3841 | if (IS_ERR_OR_NULL(di)) { |
| 3842 | if (!di) |
| 3843 | ret = -ENOENT; |
| 3844 | else |
| 3845 | ret = PTR_ERR(di); |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3846 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3847 | goto out; |
| 3848 | } |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3849 | |
| 3850 | leaf = path->nodes[0]; |
| 3851 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3852 | index = key.offset; |
Josef Bacik | d49d328 | 2019-12-18 17:20:28 -0500 | [diff] [blame] | 3853 | btrfs_release_path(path); |
| 3854 | } else { |
| 3855 | ret = btrfs_del_root_ref(trans, objectid, |
| 3856 | root->root_key.objectid, dir_ino, |
| 3857 | &index, name, name_len); |
| 3858 | if (ret) { |
| 3859 | btrfs_abort_transaction(trans, ret); |
| 3860 | goto out; |
| 3861 | } |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3862 | } |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3863 | |
Lu Fengqi | 9add294 | 2018-08-01 11:32:26 +0800 | [diff] [blame] | 3864 | ret = btrfs_delete_delayed_dir_index(trans, BTRFS_I(dir), index); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3865 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3866 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3867 | goto out; |
| 3868 | } |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 3869 | |
Nikolay Borisov | 6ef06d2 | 2017-02-20 13:50:34 +0200 | [diff] [blame] | 3870 | 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] | 3871 | inode_inc_iversion(dir); |
Deepa Dinamani | c2050a4 | 2016-09-14 07:48:06 -0700 | [diff] [blame] | 3872 | dir->i_mtime = dir->i_ctime = current_time(dir); |
Josef Bacik | 5a24e84 | 2012-08-08 10:12:59 -0600 | [diff] [blame] | 3873 | ret = btrfs_update_inode_fallback(trans, root, dir); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3874 | if (ret) |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3875 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3876 | out: |
Josef Bacik | 71d7aed | 2011-06-14 14:24:32 -0400 | [diff] [blame] | 3877 | btrfs_free_path(path); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3878 | return ret; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3879 | } |
| 3880 | |
Misono Tomohiro | ec42f16 | 2018-04-18 11:34:13 +0900 | [diff] [blame] | 3881 | /* |
| 3882 | * Helper to check if the subvolume references other subvolumes or if it's |
| 3883 | * default. |
| 3884 | */ |
Misono Tomohiro | f60a236 | 2018-04-18 11:34:52 +0900 | [diff] [blame] | 3885 | static noinline int may_destroy_subvol(struct btrfs_root *root) |
Misono Tomohiro | ec42f16 | 2018-04-18 11:34:13 +0900 | [diff] [blame] | 3886 | { |
| 3887 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 3888 | struct btrfs_path *path; |
| 3889 | struct btrfs_dir_item *di; |
| 3890 | struct btrfs_key key; |
| 3891 | u64 dir_id; |
| 3892 | int ret; |
| 3893 | |
| 3894 | path = btrfs_alloc_path(); |
| 3895 | if (!path) |
| 3896 | return -ENOMEM; |
| 3897 | |
| 3898 | /* Make sure this root isn't set as the default subvol */ |
| 3899 | dir_id = btrfs_super_root_dir(fs_info->super_copy); |
| 3900 | di = btrfs_lookup_dir_item(NULL, fs_info->tree_root, path, |
| 3901 | dir_id, "default", 7, 0); |
| 3902 | if (di && !IS_ERR(di)) { |
| 3903 | btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key); |
| 3904 | if (key.objectid == root->root_key.objectid) { |
| 3905 | ret = -EPERM; |
| 3906 | btrfs_err(fs_info, |
| 3907 | "deleting default subvolume %llu is not allowed", |
| 3908 | key.objectid); |
| 3909 | goto out; |
| 3910 | } |
| 3911 | btrfs_release_path(path); |
| 3912 | } |
| 3913 | |
| 3914 | key.objectid = root->root_key.objectid; |
| 3915 | key.type = BTRFS_ROOT_REF_KEY; |
| 3916 | key.offset = (u64)-1; |
| 3917 | |
| 3918 | ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0); |
| 3919 | if (ret < 0) |
| 3920 | goto out; |
| 3921 | BUG_ON(ret == 0); |
| 3922 | |
| 3923 | ret = 0; |
| 3924 | if (path->slots[0] > 0) { |
| 3925 | path->slots[0]--; |
| 3926 | btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]); |
| 3927 | if (key.objectid == root->root_key.objectid && |
| 3928 | key.type == BTRFS_ROOT_REF_KEY) |
| 3929 | ret = -ENOTEMPTY; |
| 3930 | } |
| 3931 | out: |
| 3932 | btrfs_free_path(path); |
| 3933 | return ret; |
| 3934 | } |
| 3935 | |
Nikolay Borisov | 20a6800 | 2018-04-27 14:36:24 +0300 | [diff] [blame] | 3936 | /* Delete all dentries for inodes belonging to the root */ |
| 3937 | static void btrfs_prune_dentries(struct btrfs_root *root) |
| 3938 | { |
| 3939 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 3940 | struct rb_node *node; |
| 3941 | struct rb_node *prev; |
| 3942 | struct btrfs_inode *entry; |
| 3943 | struct inode *inode; |
| 3944 | u64 objectid = 0; |
| 3945 | |
| 3946 | if (!test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) |
| 3947 | WARN_ON(btrfs_root_refs(&root->root_item) != 0); |
| 3948 | |
| 3949 | spin_lock(&root->inode_lock); |
| 3950 | again: |
| 3951 | node = root->inode_tree.rb_node; |
| 3952 | prev = NULL; |
| 3953 | while (node) { |
| 3954 | prev = node; |
| 3955 | entry = rb_entry(node, struct btrfs_inode, rb_node); |
| 3956 | |
David Sterba | 3750851 | 2018-06-29 10:56:40 +0200 | [diff] [blame] | 3957 | if (objectid < btrfs_ino(entry)) |
Nikolay Borisov | 20a6800 | 2018-04-27 14:36:24 +0300 | [diff] [blame] | 3958 | node = node->rb_left; |
David Sterba | 3750851 | 2018-06-29 10:56:40 +0200 | [diff] [blame] | 3959 | else if (objectid > btrfs_ino(entry)) |
Nikolay Borisov | 20a6800 | 2018-04-27 14:36:24 +0300 | [diff] [blame] | 3960 | node = node->rb_right; |
| 3961 | else |
| 3962 | break; |
| 3963 | } |
| 3964 | if (!node) { |
| 3965 | while (prev) { |
| 3966 | entry = rb_entry(prev, struct btrfs_inode, rb_node); |
David Sterba | 3750851 | 2018-06-29 10:56:40 +0200 | [diff] [blame] | 3967 | if (objectid <= btrfs_ino(entry)) { |
Nikolay Borisov | 20a6800 | 2018-04-27 14:36:24 +0300 | [diff] [blame] | 3968 | node = prev; |
| 3969 | break; |
| 3970 | } |
| 3971 | prev = rb_next(prev); |
| 3972 | } |
| 3973 | } |
| 3974 | while (node) { |
| 3975 | entry = rb_entry(node, struct btrfs_inode, rb_node); |
David Sterba | 3750851 | 2018-06-29 10:56:40 +0200 | [diff] [blame] | 3976 | objectid = btrfs_ino(entry) + 1; |
Nikolay Borisov | 20a6800 | 2018-04-27 14:36:24 +0300 | [diff] [blame] | 3977 | inode = igrab(&entry->vfs_inode); |
| 3978 | if (inode) { |
| 3979 | spin_unlock(&root->inode_lock); |
| 3980 | if (atomic_read(&inode->i_count) > 1) |
| 3981 | d_prune_aliases(inode); |
| 3982 | /* |
| 3983 | * btrfs_drop_inode will have it removed from the inode |
| 3984 | * cache when its usage count hits zero. |
| 3985 | */ |
| 3986 | iput(inode); |
| 3987 | cond_resched(); |
| 3988 | spin_lock(&root->inode_lock); |
| 3989 | goto again; |
| 3990 | } |
| 3991 | |
| 3992 | if (cond_resched_lock(&root->inode_lock)) |
| 3993 | goto again; |
| 3994 | |
| 3995 | node = rb_next(node); |
| 3996 | } |
| 3997 | spin_unlock(&root->inode_lock); |
| 3998 | } |
| 3999 | |
Misono Tomohiro | f60a236 | 2018-04-18 11:34:52 +0900 | [diff] [blame] | 4000 | int btrfs_delete_subvolume(struct inode *dir, struct dentry *dentry) |
| 4001 | { |
| 4002 | struct btrfs_fs_info *fs_info = btrfs_sb(dentry->d_sb); |
| 4003 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 4004 | struct inode *inode = d_inode(dentry); |
| 4005 | struct btrfs_root *dest = BTRFS_I(inode)->root; |
| 4006 | struct btrfs_trans_handle *trans; |
| 4007 | struct btrfs_block_rsv block_rsv; |
| 4008 | u64 root_flags; |
Misono Tomohiro | f60a236 | 2018-04-18 11:34:52 +0900 | [diff] [blame] | 4009 | int ret; |
| 4010 | int err; |
| 4011 | |
| 4012 | /* |
| 4013 | * Don't allow to delete a subvolume with send in progress. This is |
| 4014 | * inside the inode lock so the error handling that has to drop the bit |
| 4015 | * again is not run concurrently. |
| 4016 | */ |
| 4017 | spin_lock(&dest->root_item_lock); |
Lu Fengqi | a7176f7 | 2018-08-04 21:10:53 +0800 | [diff] [blame] | 4018 | if (dest->send_in_progress) { |
Misono Tomohiro | f60a236 | 2018-04-18 11:34:52 +0900 | [diff] [blame] | 4019 | spin_unlock(&dest->root_item_lock); |
| 4020 | btrfs_warn(fs_info, |
| 4021 | "attempt to delete subvolume %llu during send", |
| 4022 | dest->root_key.objectid); |
| 4023 | return -EPERM; |
| 4024 | } |
Kaiwen Hu | a044bca | 2022-03-23 15:10:32 +0800 | [diff] [blame] | 4025 | if (atomic_read(&dest->nr_swapfiles)) { |
| 4026 | spin_unlock(&dest->root_item_lock); |
| 4027 | btrfs_warn(fs_info, |
| 4028 | "attempt to delete subvolume %llu with active swapfile", |
| 4029 | root->root_key.objectid); |
| 4030 | return -EPERM; |
| 4031 | } |
Lu Fengqi | a7176f7 | 2018-08-04 21:10:53 +0800 | [diff] [blame] | 4032 | root_flags = btrfs_root_flags(&dest->root_item); |
| 4033 | btrfs_set_root_flags(&dest->root_item, |
| 4034 | root_flags | BTRFS_ROOT_SUBVOL_DEAD); |
| 4035 | spin_unlock(&dest->root_item_lock); |
Misono Tomohiro | f60a236 | 2018-04-18 11:34:52 +0900 | [diff] [blame] | 4036 | |
| 4037 | down_write(&fs_info->subvol_sem); |
| 4038 | |
| 4039 | err = may_destroy_subvol(dest); |
| 4040 | if (err) |
| 4041 | goto out_up_write; |
| 4042 | |
| 4043 | btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP); |
| 4044 | /* |
| 4045 | * One for dir inode, |
| 4046 | * two for dir entries, |
| 4047 | * two for root ref/backref. |
| 4048 | */ |
Gu JinXiang | c4c129d | 2018-05-30 11:00:38 +0800 | [diff] [blame] | 4049 | err = btrfs_subvolume_reserve_metadata(root, &block_rsv, 5, true); |
Misono Tomohiro | f60a236 | 2018-04-18 11:34:52 +0900 | [diff] [blame] | 4050 | if (err) |
| 4051 | goto out_up_write; |
| 4052 | |
| 4053 | trans = btrfs_start_transaction(root, 0); |
| 4054 | if (IS_ERR(trans)) { |
| 4055 | err = PTR_ERR(trans); |
| 4056 | goto out_release; |
| 4057 | } |
| 4058 | trans->block_rsv = &block_rsv; |
| 4059 | trans->bytes_reserved = block_rsv.size; |
| 4060 | |
| 4061 | btrfs_record_snapshot_destroy(trans, BTRFS_I(dir)); |
| 4062 | |
Josef Bacik | 045d396 | 2019-12-18 17:20:27 -0500 | [diff] [blame] | 4063 | ret = btrfs_unlink_subvol(trans, dir, dentry); |
Misono Tomohiro | f60a236 | 2018-04-18 11:34:52 +0900 | [diff] [blame] | 4064 | if (ret) { |
| 4065 | err = ret; |
| 4066 | btrfs_abort_transaction(trans, ret); |
| 4067 | goto out_end_trans; |
| 4068 | } |
| 4069 | |
| 4070 | btrfs_record_root_in_trans(trans, dest); |
| 4071 | |
| 4072 | memset(&dest->root_item.drop_progress, 0, |
| 4073 | sizeof(dest->root_item.drop_progress)); |
| 4074 | dest->root_item.drop_level = 0; |
| 4075 | btrfs_set_root_refs(&dest->root_item, 0); |
| 4076 | |
| 4077 | if (!test_and_set_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &dest->state)) { |
| 4078 | ret = btrfs_insert_orphan_item(trans, |
| 4079 | fs_info->tree_root, |
| 4080 | dest->root_key.objectid); |
| 4081 | if (ret) { |
| 4082 | btrfs_abort_transaction(trans, ret); |
| 4083 | err = ret; |
| 4084 | goto out_end_trans; |
| 4085 | } |
| 4086 | } |
| 4087 | |
Lu Fengqi | d195779 | 2018-05-29 15:01:54 +0800 | [diff] [blame] | 4088 | ret = btrfs_uuid_tree_remove(trans, dest->root_item.uuid, |
Misono Tomohiro | f60a236 | 2018-04-18 11:34:52 +0900 | [diff] [blame] | 4089 | BTRFS_UUID_KEY_SUBVOL, |
| 4090 | dest->root_key.objectid); |
| 4091 | if (ret && ret != -ENOENT) { |
| 4092 | btrfs_abort_transaction(trans, ret); |
| 4093 | err = ret; |
| 4094 | goto out_end_trans; |
| 4095 | } |
| 4096 | if (!btrfs_is_empty_uuid(dest->root_item.received_uuid)) { |
Lu Fengqi | d195779 | 2018-05-29 15:01:54 +0800 | [diff] [blame] | 4097 | ret = btrfs_uuid_tree_remove(trans, |
Misono Tomohiro | f60a236 | 2018-04-18 11:34:52 +0900 | [diff] [blame] | 4098 | dest->root_item.received_uuid, |
| 4099 | BTRFS_UUID_KEY_RECEIVED_SUBVOL, |
| 4100 | dest->root_key.objectid); |
| 4101 | if (ret && ret != -ENOENT) { |
| 4102 | btrfs_abort_transaction(trans, ret); |
| 4103 | err = ret; |
| 4104 | goto out_end_trans; |
| 4105 | } |
| 4106 | } |
| 4107 | |
Qu Wenruo | 082b6c9 | 2020-06-16 10:17:37 +0800 | [diff] [blame] | 4108 | free_anon_bdev(dest->anon_dev); |
| 4109 | dest->anon_dev = 0; |
Misono Tomohiro | f60a236 | 2018-04-18 11:34:52 +0900 | [diff] [blame] | 4110 | out_end_trans: |
| 4111 | trans->block_rsv = NULL; |
| 4112 | trans->bytes_reserved = 0; |
| 4113 | ret = btrfs_end_transaction(trans); |
| 4114 | if (ret && !err) |
| 4115 | err = ret; |
| 4116 | inode->i_flags |= S_DEAD; |
| 4117 | out_release: |
Qu Wenruo | e85fde5 | 2020-07-24 14:46:10 +0800 | [diff] [blame] | 4118 | btrfs_subvolume_release_metadata(root, &block_rsv); |
Misono Tomohiro | f60a236 | 2018-04-18 11:34:52 +0900 | [diff] [blame] | 4119 | out_up_write: |
| 4120 | up_write(&fs_info->subvol_sem); |
| 4121 | if (err) { |
| 4122 | spin_lock(&dest->root_item_lock); |
| 4123 | root_flags = btrfs_root_flags(&dest->root_item); |
| 4124 | btrfs_set_root_flags(&dest->root_item, |
| 4125 | root_flags & ~BTRFS_ROOT_SUBVOL_DEAD); |
| 4126 | spin_unlock(&dest->root_item_lock); |
| 4127 | } else { |
| 4128 | d_invalidate(dentry); |
Nikolay Borisov | 20a6800 | 2018-04-27 14:36:24 +0300 | [diff] [blame] | 4129 | btrfs_prune_dentries(dest); |
Misono Tomohiro | f60a236 | 2018-04-18 11:34:52 +0900 | [diff] [blame] | 4130 | ASSERT(dest->send_in_progress == 0); |
| 4131 | |
| 4132 | /* the last ref */ |
| 4133 | if (dest->ino_cache_inode) { |
| 4134 | iput(dest->ino_cache_inode); |
| 4135 | dest->ino_cache_inode = NULL; |
| 4136 | } |
| 4137 | } |
| 4138 | |
| 4139 | return err; |
| 4140 | } |
| 4141 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4142 | static int btrfs_rmdir(struct inode *dir, struct dentry *dentry) |
| 4143 | { |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 4144 | struct inode *inode = d_inode(dentry); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 4145 | int err = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4146 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4147 | struct btrfs_trans_handle *trans; |
Filipe Manana | 44f714d | 2016-06-06 16:11:13 +0100 | [diff] [blame] | 4148 | u64 last_unlink_trans; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4149 | |
David Sterba | b3ae244 | 2012-09-13 16:04:34 -0600 | [diff] [blame] | 4150 | if (inode->i_size > BTRFS_EMPTY_DIR_SIZE) |
Yan | 134d451 | 2007-10-25 15:49:25 -0400 | [diff] [blame] | 4151 | return -ENOTEMPTY; |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 4152 | if (btrfs_ino(BTRFS_I(inode)) == BTRFS_FIRST_FREE_OBJECTID) |
Misono Tomohiro | a79a464 | 2018-04-18 11:35:31 +0900 | [diff] [blame] | 4153 | return btrfs_delete_subvolume(dir, dentry); |
Yan | 134d451 | 2007-10-25 15:49:25 -0400 | [diff] [blame] | 4154 | |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 4155 | trans = __unlink_start_trans(dir); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4156 | if (IS_ERR(trans)) |
Josef Bacik | 5df6a9f | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4157 | return PTR_ERR(trans); |
Josef Bacik | 5df6a9f | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4158 | |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 4159 | if (unlikely(btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) { |
Josef Bacik | 045d396 | 2019-12-18 17:20:27 -0500 | [diff] [blame] | 4160 | err = btrfs_unlink_subvol(trans, dir, dentry); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4161 | goto out; |
| 4162 | } |
| 4163 | |
Nikolay Borisov | 73f2e54 | 2017-02-20 13:50:59 +0200 | [diff] [blame] | 4164 | err = btrfs_orphan_add(trans, BTRFS_I(inode)); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 4165 | if (err) |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4166 | goto out; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 4167 | |
Filipe Manana | 44f714d | 2016-06-06 16:11:13 +0100 | [diff] [blame] | 4168 | last_unlink_trans = BTRFS_I(inode)->last_unlink_trans; |
| 4169 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4170 | /* now the directory is empty */ |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 4171 | err = btrfs_unlink_inode(trans, root, BTRFS_I(dir), |
| 4172 | BTRFS_I(d_inode(dentry)), dentry->d_name.name, |
| 4173 | dentry->d_name.len); |
Filipe Manana | 44f714d | 2016-06-06 16:11:13 +0100 | [diff] [blame] | 4174 | if (!err) { |
Nikolay Borisov | 6ef06d2 | 2017-02-20 13:50:34 +0200 | [diff] [blame] | 4175 | btrfs_i_size_write(BTRFS_I(inode), 0); |
Filipe Manana | 44f714d | 2016-06-06 16:11:13 +0100 | [diff] [blame] | 4176 | /* |
| 4177 | * Propagate the last_unlink_trans value of the deleted dir to |
| 4178 | * its parent directory. This is to prevent an unrecoverable |
| 4179 | * log tree in the case we do something like this: |
| 4180 | * 1) create dir foo |
| 4181 | * 2) create snapshot under dir foo |
| 4182 | * 3) delete the snapshot |
| 4183 | * 4) rmdir foo |
| 4184 | * 5) mkdir foo |
| 4185 | * 6) fsync foo or some file inside foo |
| 4186 | */ |
| 4187 | if (last_unlink_trans >= trans->transid) |
| 4188 | BTRFS_I(dir)->last_unlink_trans = last_unlink_trans; |
| 4189 | } |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4190 | out: |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 4191 | btrfs_end_transaction(trans); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4192 | btrfs_btree_balance_dirty(root->fs_info); |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 4193 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4194 | return err; |
| 4195 | } |
| 4196 | |
Josef Bacik | ddfae63 | 2017-10-19 14:16:02 -0400 | [diff] [blame] | 4197 | /* |
| 4198 | * Return this if we need to call truncate_block for the last bit of the |
| 4199 | * truncate. |
| 4200 | */ |
| 4201 | #define NEED_TRUNCATE_BLOCK 1 |
Filipe Manana | 0305cd5 | 2015-10-16 12:34:25 +0100 | [diff] [blame] | 4202 | |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 4203 | /* |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4204 | * this can truncate away extent items, csum items and directory items. |
| 4205 | * 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] | 4206 | * any higher than new_size |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4207 | * |
| 4208 | * csum items that cross the new i_size are truncated to the new size |
| 4209 | * as well. |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 4210 | * |
| 4211 | * min_type is the minimum key type to truncate down to. If set to 0, this |
| 4212 | * 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] | 4213 | */ |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4214 | int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans, |
| 4215 | struct btrfs_root *root, |
| 4216 | struct inode *inode, |
| 4217 | u64 new_size, u32 min_type) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4218 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4219 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4220 | struct btrfs_path *path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4221 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4222 | struct btrfs_file_extent_item *fi; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4223 | struct btrfs_key key; |
| 4224 | struct btrfs_key found_key; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4225 | u64 extent_start = 0; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4226 | u64 extent_num_bytes = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4227 | u64 extent_offset = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4228 | u64 item_end = 0; |
Filipe Manana | c1aa457 | 2015-06-20 18:20:09 +0100 | [diff] [blame] | 4229 | u64 last_size = new_size; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4230 | u32 found_type = (u8)-1; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4231 | int found_extent; |
| 4232 | int del_item; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4233 | int pending_del_nr = 0; |
| 4234 | int pending_del_slot = 0; |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4235 | int extent_type = -1; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4236 | int ret; |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 4237 | u64 ino = btrfs_ino(BTRFS_I(inode)); |
Chris Mason | 28ed134 | 2014-12-17 09:41:04 -0800 | [diff] [blame] | 4238 | u64 bytes_deleted = 0; |
Thomas Meyer | 897ca81 | 2017-10-07 16:02:21 +0200 | [diff] [blame] | 4239 | bool be_nice = false; |
| 4240 | bool should_throttle = false; |
Filipe Manana | 28553fa | 2020-02-07 12:23:09 +0000 | [diff] [blame] | 4241 | const u64 lock_start = ALIGN_DOWN(new_size, fs_info->sectorsize); |
| 4242 | struct extent_state *cached_state = NULL; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4243 | |
| 4244 | BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4245 | |
Chris Mason | 28ed134 | 2014-12-17 09:41:04 -0800 | [diff] [blame] | 4246 | /* |
Qu Wenruo | 92a7cc4 | 2020-05-15 14:01:40 +0800 | [diff] [blame] | 4247 | * For non-free space inodes and non-shareable roots, we want to back |
| 4248 | * off from time to time. This means all inodes in subvolume roots, |
| 4249 | * reloc roots, and data reloc roots. |
Chris Mason | 28ed134 | 2014-12-17 09:41:04 -0800 | [diff] [blame] | 4250 | */ |
Nikolay Borisov | 70ddc55 | 2017-02-20 13:50:35 +0200 | [diff] [blame] | 4251 | if (!btrfs_is_free_space_inode(BTRFS_I(inode)) && |
Qu Wenruo | 92a7cc4 | 2020-05-15 14:01:40 +0800 | [diff] [blame] | 4252 | test_bit(BTRFS_ROOT_SHAREABLE, &root->state)) |
Thomas Meyer | 897ca81 | 2017-10-07 16:02:21 +0200 | [diff] [blame] | 4253 | be_nice = true; |
Chris Mason | 28ed134 | 2014-12-17 09:41:04 -0800 | [diff] [blame] | 4254 | |
Mark Fasheh | 0eb0e19 | 2011-07-12 16:44:10 -0700 | [diff] [blame] | 4255 | path = btrfs_alloc_path(); |
| 4256 | if (!path) |
| 4257 | return -ENOMEM; |
David Sterba | e4058b5 | 2015-11-27 16:31:35 +0100 | [diff] [blame] | 4258 | path->reada = READA_BACK; |
Mark Fasheh | 0eb0e19 | 2011-07-12 16:44:10 -0700 | [diff] [blame] | 4259 | |
Qu Wenruo | 82028e0a | 2020-05-15 14:01:41 +0800 | [diff] [blame] | 4260 | if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) { |
Filipe Manana | a5ae50d | 2020-02-20 13:29:49 +0000 | [diff] [blame] | 4261 | lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, (u64)-1, |
| 4262 | &cached_state); |
Filipe Manana | 28553fa | 2020-02-07 12:23:09 +0000 | [diff] [blame] | 4263 | |
Qu Wenruo | 82028e0a | 2020-05-15 14:01:41 +0800 | [diff] [blame] | 4264 | /* |
| 4265 | * We want to drop from the next block forward in case this |
| 4266 | * new size is not block aligned since we will be keeping the |
| 4267 | * last block of the extent just the way it is. |
| 4268 | */ |
Nikolay Borisov | dcdbc05 | 2017-02-20 13:50:45 +0200 | [diff] [blame] | 4269 | btrfs_drop_extent_cache(BTRFS_I(inode), ALIGN(new_size, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4270 | fs_info->sectorsize), |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 4271 | (u64)-1, 0); |
Qu Wenruo | 82028e0a | 2020-05-15 14:01:41 +0800 | [diff] [blame] | 4272 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4273 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 4274 | /* |
| 4275 | * This function is also used to drop the items in the log tree before |
| 4276 | * 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] | 4277 | * 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] | 4278 | * items. |
| 4279 | */ |
| 4280 | if (min_type == 0 && root == BTRFS_I(inode)->root) |
Nikolay Borisov | 4ccb5c7 | 2017-01-10 20:35:38 +0200 | [diff] [blame] | 4281 | btrfs_kill_delayed_inode_items(BTRFS_I(inode)); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 4282 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4283 | key.objectid = ino; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4284 | key.offset = (u64)-1; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4285 | key.type = (u8)-1; |
| 4286 | |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4287 | search_again: |
Chris Mason | 28ed134 | 2014-12-17 09:41:04 -0800 | [diff] [blame] | 4288 | /* |
| 4289 | * with a 16K leaf size and 128MB extents, you can actually queue |
| 4290 | * up a huge file in a single leaf. Most of the time that |
| 4291 | * bytes_deleted is > 0, it will be huge by the time we get here |
| 4292 | */ |
Omar Sandoval | fd86a3a | 2018-05-11 13:13:30 -0700 | [diff] [blame] | 4293 | if (be_nice && bytes_deleted > SZ_32M && |
| 4294 | btrfs_should_end_transaction(trans)) { |
| 4295 | ret = -EAGAIN; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4296 | goto out; |
| 4297 | } |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4298 | |
Omar Sandoval | fd86a3a | 2018-05-11 13:13:30 -0700 | [diff] [blame] | 4299 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 4300 | if (ret < 0) |
| 4301 | goto out; |
| 4302 | |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4303 | if (ret > 0) { |
Omar Sandoval | fd86a3a | 2018-05-11 13:13:30 -0700 | [diff] [blame] | 4304 | ret = 0; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4305 | /* there are no items in the tree for us to truncate, we're |
| 4306 | * done |
| 4307 | */ |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4308 | if (path->slots[0] == 0) |
| 4309 | goto out; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4310 | path->slots[0]--; |
| 4311 | } |
| 4312 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4313 | while (1) { |
Josef Bacik | 9ddc959 | 2020-01-17 09:02:22 -0500 | [diff] [blame] | 4314 | u64 clear_start = 0, clear_len = 0; |
| 4315 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4316 | fi = NULL; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4317 | leaf = path->nodes[0]; |
| 4318 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 4319 | found_type = found_key.type; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4320 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4321 | if (found_key.objectid != ino) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4322 | break; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4323 | |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4324 | if (found_type < min_type) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4325 | break; |
| 4326 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4327 | item_end = found_key.offset; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4328 | if (found_type == BTRFS_EXTENT_DATA_KEY) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4329 | fi = btrfs_item_ptr(leaf, path->slots[0], |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4330 | struct btrfs_file_extent_item); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4331 | extent_type = btrfs_file_extent_type(leaf, fi); |
| 4332 | if (extent_type != BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4333 | item_end += |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4334 | btrfs_file_extent_num_bytes(leaf, fi); |
Liu Bo | 09ed2f1 | 2017-03-10 11:09:48 -0800 | [diff] [blame] | 4335 | |
| 4336 | trace_btrfs_truncate_show_fi_regular( |
| 4337 | BTRFS_I(inode), leaf, fi, |
| 4338 | found_key.offset); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4339 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
Qu Wenruo | e41ca58 | 2018-06-06 15:41:49 +0800 | [diff] [blame] | 4340 | item_end += btrfs_file_extent_ram_bytes(leaf, |
| 4341 | fi); |
Liu Bo | 09ed2f1 | 2017-03-10 11:09:48 -0800 | [diff] [blame] | 4342 | |
| 4343 | trace_btrfs_truncate_show_fi_inline( |
| 4344 | BTRFS_I(inode), leaf, fi, path->slots[0], |
| 4345 | found_key.offset); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4346 | } |
Yan | 008630c | 2007-11-07 13:31:09 -0500 | [diff] [blame] | 4347 | item_end--; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4348 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4349 | if (found_type > min_type) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4350 | del_item = 1; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4351 | } else { |
Filipe Manana | 76b42ab | 2017-02-14 16:56:01 +0000 | [diff] [blame] | 4352 | if (item_end < new_size) |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4353 | break; |
| 4354 | if (found_key.offset >= new_size) |
| 4355 | del_item = 1; |
| 4356 | else |
| 4357 | del_item = 0; |
| 4358 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4359 | found_extent = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4360 | /* FIXME, shrink the extent if the ref count is only 1 */ |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4361 | if (found_type != BTRFS_EXTENT_DATA_KEY) |
| 4362 | goto delete; |
| 4363 | |
| 4364 | if (extent_type != BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4365 | u64 num_dec; |
Josef Bacik | 9ddc959 | 2020-01-17 09:02:22 -0500 | [diff] [blame] | 4366 | |
| 4367 | clear_start = found_key.offset; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4368 | extent_start = btrfs_file_extent_disk_bytenr(leaf, fi); |
Miao Xie | f70a9a6b | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 4369 | if (!del_item) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4370 | u64 orig_num_bytes = |
| 4371 | btrfs_file_extent_num_bytes(leaf, fi); |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 4372 | extent_num_bytes = ALIGN(new_size - |
| 4373 | found_key.offset, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4374 | fs_info->sectorsize); |
Josef Bacik | 9ddc959 | 2020-01-17 09:02:22 -0500 | [diff] [blame] | 4375 | clear_start = ALIGN(new_size, fs_info->sectorsize); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4376 | btrfs_set_file_extent_num_bytes(leaf, fi, |
| 4377 | extent_num_bytes); |
| 4378 | num_dec = (orig_num_bytes - |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 4379 | extent_num_bytes); |
Qu Wenruo | 92a7cc4 | 2020-05-15 14:01:40 +0800 | [diff] [blame] | 4380 | if (test_bit(BTRFS_ROOT_SHAREABLE, |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 4381 | &root->state) && |
| 4382 | extent_start != 0) |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 4383 | inode_sub_bytes(inode, num_dec); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4384 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4385 | } else { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4386 | extent_num_bytes = |
| 4387 | btrfs_file_extent_disk_num_bytes(leaf, |
| 4388 | fi); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4389 | extent_offset = found_key.offset - |
| 4390 | btrfs_file_extent_offset(leaf, fi); |
| 4391 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4392 | /* FIXME blocksize != 4096 */ |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 4393 | num_dec = btrfs_file_extent_num_bytes(leaf, fi); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4394 | if (extent_start != 0) { |
| 4395 | found_extent = 1; |
Qu Wenruo | 92a7cc4 | 2020-05-15 14:01:40 +0800 | [diff] [blame] | 4396 | if (test_bit(BTRFS_ROOT_SHAREABLE, |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 4397 | &root->state)) |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 4398 | inode_sub_bytes(inode, num_dec); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4399 | } |
| 4400 | } |
Josef Bacik | 9ddc959 | 2020-01-17 09:02:22 -0500 | [diff] [blame] | 4401 | clear_len = num_dec; |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 4402 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4403 | /* |
| 4404 | * we can't truncate inline items that have had |
| 4405 | * special encodings |
| 4406 | */ |
| 4407 | if (!del_item && |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4408 | btrfs_file_extent_encryption(leaf, fi) == 0 && |
Josef Bacik | ddfae63 | 2017-10-19 14:16:02 -0400 | [diff] [blame] | 4409 | btrfs_file_extent_other_encoding(leaf, fi) == 0 && |
| 4410 | btrfs_file_extent_compression(leaf, fi) == 0) { |
| 4411 | u32 size = (u32)(new_size - found_key.offset); |
Chris Mason | 514ac8a | 2014-01-03 21:07:00 -0800 | [diff] [blame] | 4412 | |
Josef Bacik | ddfae63 | 2017-10-19 14:16:02 -0400 | [diff] [blame] | 4413 | btrfs_set_file_extent_ram_bytes(leaf, fi, size); |
| 4414 | size = btrfs_file_extent_calc_inline_size(size); |
David Sterba | 78ac4f9 | 2019-03-20 14:49:12 +0100 | [diff] [blame] | 4415 | btrfs_truncate_item(path, size, 1); |
Josef Bacik | ddfae63 | 2017-10-19 14:16:02 -0400 | [diff] [blame] | 4416 | } else if (!del_item) { |
Chris Mason | 514ac8a | 2014-01-03 21:07:00 -0800 | [diff] [blame] | 4417 | /* |
Josef Bacik | ddfae63 | 2017-10-19 14:16:02 -0400 | [diff] [blame] | 4418 | * We have to bail so the last_size is set to |
| 4419 | * just before this extent. |
Chris Mason | 514ac8a | 2014-01-03 21:07:00 -0800 | [diff] [blame] | 4420 | */ |
Omar Sandoval | fd86a3a | 2018-05-11 13:13:30 -0700 | [diff] [blame] | 4421 | ret = NEED_TRUNCATE_BLOCK; |
Josef Bacik | ddfae63 | 2017-10-19 14:16:02 -0400 | [diff] [blame] | 4422 | break; |
Josef Bacik | 9ddc959 | 2020-01-17 09:02:22 -0500 | [diff] [blame] | 4423 | } else { |
| 4424 | /* |
| 4425 | * Inline extents are special, we just treat |
| 4426 | * them as a full sector worth in the file |
| 4427 | * extent tree just for simplicity sake. |
| 4428 | */ |
| 4429 | clear_len = fs_info->sectorsize; |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 4430 | } |
Josef Bacik | ddfae63 | 2017-10-19 14:16:02 -0400 | [diff] [blame] | 4431 | |
Qu Wenruo | 92a7cc4 | 2020-05-15 14:01:40 +0800 | [diff] [blame] | 4432 | if (test_bit(BTRFS_ROOT_SHAREABLE, &root->state)) |
Josef Bacik | ddfae63 | 2017-10-19 14:16:02 -0400 | [diff] [blame] | 4433 | inode_sub_bytes(inode, item_end + 1 - new_size); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4434 | } |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4435 | delete: |
Josef Bacik | 9ddc959 | 2020-01-17 09:02:22 -0500 | [diff] [blame] | 4436 | /* |
| 4437 | * We use btrfs_truncate_inode_items() to clean up log trees for |
| 4438 | * multiple fsyncs, and in this case we don't want to clear the |
| 4439 | * file extent range because it's just the log. |
| 4440 | */ |
| 4441 | if (root == BTRFS_I(inode)->root) { |
| 4442 | ret = btrfs_inode_clear_file_extent_range(BTRFS_I(inode), |
| 4443 | clear_start, clear_len); |
| 4444 | if (ret) { |
| 4445 | btrfs_abort_transaction(trans, ret); |
| 4446 | break; |
| 4447 | } |
| 4448 | } |
| 4449 | |
Josef Bacik | ddfae63 | 2017-10-19 14:16:02 -0400 | [diff] [blame] | 4450 | if (del_item) |
| 4451 | last_size = found_key.offset; |
| 4452 | else |
| 4453 | last_size = new_size; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4454 | if (del_item) { |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4455 | if (!pending_del_nr) { |
| 4456 | /* no pending yet, add ourselves */ |
| 4457 | pending_del_slot = path->slots[0]; |
| 4458 | pending_del_nr = 1; |
| 4459 | } else if (pending_del_nr && |
| 4460 | path->slots[0] + 1 == pending_del_slot) { |
| 4461 | /* hop on the pending chunk */ |
| 4462 | pending_del_nr++; |
| 4463 | pending_del_slot = path->slots[0]; |
| 4464 | } else { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4465 | BUG(); |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4466 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4467 | } else { |
| 4468 | break; |
| 4469 | } |
Thomas Meyer | 897ca81 | 2017-10-07 16:02:21 +0200 | [diff] [blame] | 4470 | should_throttle = false; |
Chris Mason | 28f75a0 | 2015-02-04 06:59:29 -0800 | [diff] [blame] | 4471 | |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 4472 | if (found_extent && |
Qu Wenruo | 82028e0a | 2020-05-15 14:01:41 +0800 | [diff] [blame] | 4473 | root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) { |
Qu Wenruo | ffd4bb2 | 2019-04-04 14:45:36 +0800 | [diff] [blame] | 4474 | struct btrfs_ref ref = { 0 }; |
| 4475 | |
Chris Mason | 28ed134 | 2014-12-17 09:41:04 -0800 | [diff] [blame] | 4476 | bytes_deleted += extent_num_bytes; |
Qu Wenruo | ffd4bb2 | 2019-04-04 14:45:36 +0800 | [diff] [blame] | 4477 | |
| 4478 | btrfs_init_generic_ref(&ref, BTRFS_DROP_DELAYED_REF, |
| 4479 | extent_start, extent_num_bytes, 0); |
| 4480 | ref.real_root = root->root_key.objectid; |
| 4481 | btrfs_init_data_ref(&ref, btrfs_header_owner(leaf), |
| 4482 | ino, extent_offset); |
| 4483 | ret = btrfs_free_extent(trans, &ref); |
Omar Sandoval | 0552210 | 2018-05-11 13:13:31 -0700 | [diff] [blame] | 4484 | if (ret) { |
| 4485 | btrfs_abort_transaction(trans, ret); |
| 4486 | break; |
| 4487 | } |
Chris Mason | 28f75a0 | 2015-02-04 06:59:29 -0800 | [diff] [blame] | 4488 | if (be_nice) { |
Lu Fengqi | 7c86162 | 2018-10-11 13:40:36 +0800 | [diff] [blame] | 4489 | if (btrfs_should_throttle_delayed_refs(trans)) |
Thomas Meyer | 897ca81 | 2017-10-07 16:02:21 +0200 | [diff] [blame] | 4490 | should_throttle = true; |
Chris Mason | 28f75a0 | 2015-02-04 06:59:29 -0800 | [diff] [blame] | 4491 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4492 | } |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4493 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4494 | if (found_type == BTRFS_INODE_ITEM_KEY) |
| 4495 | break; |
| 4496 | |
| 4497 | if (path->slots[0] == 0 || |
Josef Bacik | 1262133 | 2015-02-03 07:50:16 -0800 | [diff] [blame] | 4498 | path->slots[0] != pending_del_slot || |
Josef Bacik | 28bad21 | 2018-12-03 10:20:38 -0500 | [diff] [blame] | 4499 | should_throttle) { |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4500 | if (pending_del_nr) { |
| 4501 | ret = btrfs_del_items(trans, root, path, |
| 4502 | pending_del_slot, |
| 4503 | pending_del_nr); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4504 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 4505 | btrfs_abort_transaction(trans, ret); |
Omar Sandoval | fd86a3a | 2018-05-11 13:13:30 -0700 | [diff] [blame] | 4506 | break; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4507 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4508 | pending_del_nr = 0; |
| 4509 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4510 | btrfs_release_path(path); |
Josef Bacik | 28bad21 | 2018-12-03 10:20:38 -0500 | [diff] [blame] | 4511 | |
Chris Mason | 28f75a0 | 2015-02-04 06:59:29 -0800 | [diff] [blame] | 4512 | /* |
Josef Bacik | 28bad21 | 2018-12-03 10:20:38 -0500 | [diff] [blame] | 4513 | * We can generate a lot of delayed refs, so we need to |
| 4514 | * throttle every once and a while and make sure we're |
| 4515 | * adding enough space to keep up with the work we are |
| 4516 | * generating. Since we hold a transaction here we |
| 4517 | * can't flush, and we don't want to FLUSH_LIMIT because |
| 4518 | * we could have generated too many delayed refs to |
| 4519 | * actually allocate, so just bail if we're short and |
| 4520 | * let the normal reservation dance happen higher up. |
Chris Mason | 28f75a0 | 2015-02-04 06:59:29 -0800 | [diff] [blame] | 4521 | */ |
Josef Bacik | 28bad21 | 2018-12-03 10:20:38 -0500 | [diff] [blame] | 4522 | if (should_throttle) { |
| 4523 | ret = btrfs_delayed_refs_rsv_refill(fs_info, |
| 4524 | BTRFS_RESERVE_NO_FLUSH); |
| 4525 | if (ret) { |
| 4526 | ret = -EAGAIN; |
| 4527 | break; |
| 4528 | } |
Chris Mason | 28f75a0 | 2015-02-04 06:59:29 -0800 | [diff] [blame] | 4529 | } |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4530 | goto search_again; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4531 | } else { |
| 4532 | path->slots[0]--; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4533 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4534 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4535 | out: |
Omar Sandoval | fd86a3a | 2018-05-11 13:13:30 -0700 | [diff] [blame] | 4536 | if (ret >= 0 && pending_del_nr) { |
| 4537 | int err; |
| 4538 | |
| 4539 | err = btrfs_del_items(trans, root, path, pending_del_slot, |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4540 | pending_del_nr); |
Omar Sandoval | fd86a3a | 2018-05-11 13:13:30 -0700 | [diff] [blame] | 4541 | if (err) { |
| 4542 | btrfs_abort_transaction(trans, err); |
| 4543 | ret = err; |
| 4544 | } |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4545 | } |
Filipe Manana | 76b42ab | 2017-02-14 16:56:01 +0000 | [diff] [blame] | 4546 | if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) { |
| 4547 | ASSERT(last_size >= new_size); |
Omar Sandoval | fd86a3a | 2018-05-11 13:13:30 -0700 | [diff] [blame] | 4548 | if (!ret && last_size > new_size) |
Filipe Manana | 76b42ab | 2017-02-14 16:56:01 +0000 | [diff] [blame] | 4549 | last_size = new_size; |
Josef Bacik | d923afe | 2020-01-17 09:02:23 -0500 | [diff] [blame] | 4550 | btrfs_inode_safe_disk_i_size_write(inode, last_size); |
Filipe Manana | a5ae50d | 2020-02-20 13:29:49 +0000 | [diff] [blame] | 4551 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, |
| 4552 | (u64)-1, &cached_state); |
Filipe Manana | 76b42ab | 2017-02-14 16:56:01 +0000 | [diff] [blame] | 4553 | } |
Chris Mason | 28ed134 | 2014-12-17 09:41:04 -0800 | [diff] [blame] | 4554 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4555 | btrfs_free_path(path); |
Omar Sandoval | fd86a3a | 2018-05-11 13:13:30 -0700 | [diff] [blame] | 4556 | return ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4557 | } |
| 4558 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4559 | /* |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4560 | * btrfs_truncate_block - read, zero a chunk and write a block |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 4561 | * @inode - inode that we're zeroing |
| 4562 | * @from - the offset to start zeroing |
| 4563 | * @len - the length to zero, 0 to zero the entire range respective to the |
| 4564 | * offset |
| 4565 | * @front - zero up to the offset instead of from the offset on |
| 4566 | * |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4567 | * 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] | 4568 | * 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] | 4569 | */ |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4570 | 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] | 4571 | int front) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4572 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4573 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 4574 | struct address_space *mapping = inode->i_mapping; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4575 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
| 4576 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4577 | struct extent_state *cached_state = NULL; |
Qu Wenruo | 364ecf3 | 2017-02-27 15:10:38 +0800 | [diff] [blame] | 4578 | struct extent_changeset *data_reserved = NULL; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4579 | char *kaddr; |
Qu Wenruo | 6d4572a | 2020-06-24 07:23:50 +0800 | [diff] [blame] | 4580 | bool only_release_metadata = false; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4581 | u32 blocksize = fs_info->sectorsize; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 4582 | pgoff_t index = from >> PAGE_SHIFT; |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4583 | unsigned offset = from & (blocksize - 1); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4584 | struct page *page; |
Josef Bacik | 3b16a4e | 2011-09-21 15:05:58 -0400 | [diff] [blame] | 4585 | gfp_t mask = btrfs_alloc_write_mask(mapping); |
Qu Wenruo | 6d4572a | 2020-06-24 07:23:50 +0800 | [diff] [blame] | 4586 | size_t write_bytes = blocksize; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4587 | int ret = 0; |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4588 | u64 block_start; |
| 4589 | u64 block_end; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4590 | |
Nikolay Borisov | b03ebd9 | 2018-01-18 14:47:06 +0200 | [diff] [blame] | 4591 | if (IS_ALIGNED(offset, blocksize) && |
| 4592 | (!len || IS_ALIGNED(len, blocksize))) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4593 | goto out; |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4594 | |
Josef Bacik | 8b62f87 | 2017-10-19 14:15:55 -0400 | [diff] [blame] | 4595 | block_start = round_down(from, blocksize); |
| 4596 | block_end = block_start + blocksize - 1; |
| 4597 | |
Nikolay Borisov | 36ea6f3 | 2020-06-03 08:55:41 +0300 | [diff] [blame] | 4598 | ret = btrfs_check_data_free_space(BTRFS_I(inode), &data_reserved, |
| 4599 | block_start, blocksize); |
Qu Wenruo | 6d4572a | 2020-06-24 07:23:50 +0800 | [diff] [blame] | 4600 | if (ret < 0) { |
Qu Wenruo | 38d37aa | 2020-06-24 07:23:52 +0800 | [diff] [blame] | 4601 | if (btrfs_check_nocow_lock(BTRFS_I(inode), block_start, |
| 4602 | &write_bytes) > 0) { |
Qu Wenruo | 6d4572a | 2020-06-24 07:23:50 +0800 | [diff] [blame] | 4603 | /* For nocow case, no need to reserve data space */ |
| 4604 | only_release_metadata = true; |
| 4605 | } else { |
| 4606 | goto out; |
| 4607 | } |
| 4608 | } |
| 4609 | ret = btrfs_delalloc_reserve_metadata(BTRFS_I(inode), blocksize); |
| 4610 | if (ret < 0) { |
| 4611 | if (!only_release_metadata) |
Nikolay Borisov | 25ce28c | 2020-06-03 08:55:39 +0300 | [diff] [blame] | 4612 | btrfs_free_reserved_data_space(BTRFS_I(inode), |
| 4613 | data_reserved, block_start, blocksize); |
Qu Wenruo | 6d4572a | 2020-06-24 07:23:50 +0800 | [diff] [blame] | 4614 | goto out; |
| 4615 | } |
Chris Mason | 211c17f | 2008-05-15 09:13:45 -0400 | [diff] [blame] | 4616 | again: |
Josef Bacik | 3b16a4e | 2011-09-21 15:05:58 -0400 | [diff] [blame] | 4617 | page = find_or_create_page(mapping, index, mask); |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 4618 | if (!page) { |
Nikolay Borisov | 86d5292 | 2020-06-03 08:55:40 +0300 | [diff] [blame] | 4619 | btrfs_delalloc_release_space(BTRFS_I(inode), data_reserved, |
Qu Wenruo | 43b1859 | 2017-12-12 15:34:32 +0800 | [diff] [blame] | 4620 | block_start, blocksize, true); |
Qu Wenruo | 8702ba9 | 2019-10-14 14:34:51 +0800 | [diff] [blame] | 4621 | btrfs_delalloc_release_extents(BTRFS_I(inode), blocksize); |
Miao Xie | ac6a2b3 | 2012-12-05 10:56:13 +0000 | [diff] [blame] | 4622 | ret = -ENOMEM; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4623 | goto out; |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 4624 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4625 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4626 | if (!PageUptodate(page)) { |
| 4627 | ret = btrfs_readpage(NULL, page); |
| 4628 | lock_page(page); |
Chris Mason | 211c17f | 2008-05-15 09:13:45 -0400 | [diff] [blame] | 4629 | if (page->mapping != mapping) { |
| 4630 | unlock_page(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 4631 | put_page(page); |
Chris Mason | 211c17f | 2008-05-15 09:13:45 -0400 | [diff] [blame] | 4632 | goto again; |
| 4633 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4634 | if (!PageUptodate(page)) { |
| 4635 | ret = -EIO; |
Chris Mason | 8964222 | 2008-07-24 09:41:53 -0400 | [diff] [blame] | 4636 | goto out_unlock; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4637 | } |
| 4638 | } |
Chris Mason | 211c17f | 2008-05-15 09:13:45 -0400 | [diff] [blame] | 4639 | wait_on_page_writeback(page); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4640 | |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4641 | lock_extent_bits(io_tree, block_start, block_end, &cached_state); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4642 | set_page_extent_mapped(page); |
| 4643 | |
Nikolay Borisov | c350437 | 2020-06-03 08:55:03 +0300 | [diff] [blame] | 4644 | ordered = btrfs_lookup_ordered_extent(BTRFS_I(inode), block_start); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4645 | if (ordered) { |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4646 | unlock_extent_cached(io_tree, block_start, block_end, |
David Sterba | e43bbe5 | 2017-12-12 21:43:52 +0100 | [diff] [blame] | 4647 | &cached_state); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4648 | unlock_page(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 4649 | put_page(page); |
Nikolay Borisov | c0a4360 | 2020-09-18 12:15:53 +0300 | [diff] [blame] | 4650 | btrfs_start_ordered_extent(ordered, 1); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4651 | btrfs_put_ordered_extent(ordered); |
| 4652 | goto again; |
| 4653 | } |
| 4654 | |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4655 | clear_extent_bit(&BTRFS_I(inode)->io_tree, block_start, block_end, |
Omar Sandoval | e182163 | 2019-08-15 14:04:04 -0700 | [diff] [blame] | 4656 | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, |
| 4657 | 0, 0, &cached_state); |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 4658 | |
Nikolay Borisov | c2566f2 | 2020-06-03 08:55:35 +0300 | [diff] [blame] | 4659 | 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] | 4660 | &cached_state); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4661 | if (ret) { |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4662 | unlock_extent_cached(io_tree, block_start, block_end, |
David Sterba | e43bbe5 | 2017-12-12 21:43:52 +0100 | [diff] [blame] | 4663 | &cached_state); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4664 | goto out_unlock; |
| 4665 | } |
| 4666 | |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4667 | if (offset != blocksize) { |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 4668 | if (!len) |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4669 | len = blocksize - offset; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4670 | kaddr = kmap(page); |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 4671 | if (front) |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4672 | memset(kaddr + (block_start - page_offset(page)), |
| 4673 | 0, offset); |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 4674 | else |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4675 | memset(kaddr + (block_start - page_offset(page)) + offset, |
| 4676 | 0, len); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4677 | flush_dcache_page(page); |
| 4678 | kunmap(page); |
| 4679 | } |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 4680 | ClearPageChecked(page); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4681 | set_page_dirty(page); |
David Sterba | e43bbe5 | 2017-12-12 21:43:52 +0100 | [diff] [blame] | 4682 | unlock_extent_cached(io_tree, block_start, block_end, &cached_state); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4683 | |
Qu Wenruo | 6d4572a | 2020-06-24 07:23:50 +0800 | [diff] [blame] | 4684 | if (only_release_metadata) |
| 4685 | set_extent_bit(&BTRFS_I(inode)->io_tree, block_start, |
| 4686 | block_end, EXTENT_NORESERVE, NULL, NULL, |
| 4687 | GFP_NOFS); |
| 4688 | |
Chris Mason | 8964222 | 2008-07-24 09:41:53 -0400 | [diff] [blame] | 4689 | out_unlock: |
Qu Wenruo | 6d4572a | 2020-06-24 07:23:50 +0800 | [diff] [blame] | 4690 | if (ret) { |
| 4691 | if (only_release_metadata) |
| 4692 | btrfs_delalloc_release_metadata(BTRFS_I(inode), |
| 4693 | blocksize, true); |
| 4694 | else |
Nikolay Borisov | 86d5292 | 2020-06-03 08:55:40 +0300 | [diff] [blame] | 4695 | btrfs_delalloc_release_space(BTRFS_I(inode), data_reserved, |
Qu Wenruo | 6d4572a | 2020-06-24 07:23:50 +0800 | [diff] [blame] | 4696 | block_start, blocksize, true); |
| 4697 | } |
Qu Wenruo | 8702ba9 | 2019-10-14 14:34:51 +0800 | [diff] [blame] | 4698 | btrfs_delalloc_release_extents(BTRFS_I(inode), blocksize); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4699 | unlock_page(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 4700 | put_page(page); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4701 | out: |
Qu Wenruo | 6d4572a | 2020-06-24 07:23:50 +0800 | [diff] [blame] | 4702 | if (only_release_metadata) |
Qu Wenruo | 38d37aa | 2020-06-24 07:23:52 +0800 | [diff] [blame] | 4703 | btrfs_check_nocow_unlock(BTRFS_I(inode)); |
Qu Wenruo | 364ecf3 | 2017-02-27 15:10:38 +0800 | [diff] [blame] | 4704 | extent_changeset_free(data_reserved); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4705 | return ret; |
| 4706 | } |
| 4707 | |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4708 | static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode, |
| 4709 | u64 offset, u64 len) |
| 4710 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4711 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4712 | struct btrfs_trans_handle *trans; |
| 4713 | int ret; |
| 4714 | |
| 4715 | /* |
| 4716 | * Still need to make sure the inode looks like it's been updated so |
| 4717 | * that any holes get logged if we fsync. |
| 4718 | */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4719 | if (btrfs_fs_incompat(fs_info, NO_HOLES)) { |
| 4720 | BTRFS_I(inode)->last_trans = fs_info->generation; |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4721 | BTRFS_I(inode)->last_sub_trans = root->log_transid; |
| 4722 | BTRFS_I(inode)->last_log_commit = root->last_log_commit; |
| 4723 | return 0; |
| 4724 | } |
| 4725 | |
| 4726 | /* |
| 4727 | * 1 - for the one we're dropping |
| 4728 | * 1 - for the one we're adding |
| 4729 | * 1 - for updating the inode. |
| 4730 | */ |
| 4731 | trans = btrfs_start_transaction(root, 3); |
| 4732 | if (IS_ERR(trans)) |
| 4733 | return PTR_ERR(trans); |
| 4734 | |
| 4735 | ret = btrfs_drop_extents(trans, root, inode, offset, offset + len, 1); |
| 4736 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 4737 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 4738 | btrfs_end_transaction(trans); |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4739 | return ret; |
| 4740 | } |
| 4741 | |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 4742 | ret = btrfs_insert_file_extent(trans, root, btrfs_ino(BTRFS_I(inode)), |
| 4743 | offset, 0, 0, len, 0, len, 0, 0, 0); |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4744 | if (ret) |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 4745 | btrfs_abort_transaction(trans, ret); |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4746 | else |
| 4747 | btrfs_update_inode(trans, root, inode); |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 4748 | btrfs_end_transaction(trans); |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4749 | return ret; |
| 4750 | } |
| 4751 | |
Josef Bacik | 695a0d0 | 2011-03-04 15:46:53 -0500 | [diff] [blame] | 4752 | /* |
| 4753 | * This function puts in dummy file extents for the area we're creating a hole |
| 4754 | * for. So if we are truncating this file to a larger size we need to insert |
| 4755 | * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for |
| 4756 | * the range between oldsize and size |
| 4757 | */ |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 4758 | 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] | 4759 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4760 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4761 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 4762 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4763 | struct extent_map *em = NULL; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4764 | struct extent_state *cached_state = NULL; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4765 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4766 | u64 hole_start = ALIGN(oldsize, fs_info->sectorsize); |
| 4767 | u64 block_end = ALIGN(size, fs_info->sectorsize); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4768 | u64 last_byte; |
| 4769 | u64 cur_offset; |
| 4770 | u64 hole_size; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4771 | int err = 0; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4772 | |
Josef Bacik | a71754f | 2013-06-17 17:14:39 -0400 | [diff] [blame] | 4773 | /* |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4774 | * If our size started in the middle of a block we need to zero out the |
| 4775 | * 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] | 4776 | * expose stale data. |
| 4777 | */ |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4778 | err = btrfs_truncate_block(inode, oldsize, 0, 0); |
Josef Bacik | a71754f | 2013-06-17 17:14:39 -0400 | [diff] [blame] | 4779 | if (err) |
| 4780 | return err; |
| 4781 | |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4782 | if (size <= hole_start) |
| 4783 | return 0; |
| 4784 | |
David Sterba | b272ae2 | 2020-02-05 19:09:33 +0100 | [diff] [blame] | 4785 | btrfs_lock_and_flush_ordered_range(BTRFS_I(inode), hole_start, |
Nikolay Borisov | 23d31bd | 2019-05-07 10:19:23 +0300 | [diff] [blame] | 4786 | block_end - 1, &cached_state); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4787 | cur_offset = hole_start; |
| 4788 | while (1) { |
Nikolay Borisov | fc4f21b1 | 2017-02-20 13:51:06 +0200 | [diff] [blame] | 4789 | em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, cur_offset, |
Omar Sandoval | 39b07b5 | 2019-12-02 17:34:23 -0800 | [diff] [blame] | 4790 | block_end - cur_offset); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4791 | if (IS_ERR(em)) { |
| 4792 | err = PTR_ERR(em); |
Zach Brown | f276795 | 2013-01-08 19:37:58 +0000 | [diff] [blame] | 4793 | em = NULL; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4794 | break; |
| 4795 | } |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4796 | last_byte = min(extent_map_end(em), block_end); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4797 | last_byte = ALIGN(last_byte, fs_info->sectorsize); |
Josef Bacik | 9ddc959 | 2020-01-17 09:02:22 -0500 | [diff] [blame] | 4798 | hole_size = last_byte - cur_offset; |
| 4799 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4800 | if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) { |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4801 | struct extent_map *hole_em; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4802 | |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4803 | err = maybe_insert_hole(root, inode, cur_offset, |
| 4804 | hole_size); |
| 4805 | if (err) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 4806 | break; |
Josef Bacik | 9ddc959 | 2020-01-17 09:02:22 -0500 | [diff] [blame] | 4807 | |
| 4808 | err = btrfs_inode_set_file_extent_range(BTRFS_I(inode), |
| 4809 | cur_offset, hole_size); |
| 4810 | if (err) |
| 4811 | break; |
| 4812 | |
Nikolay Borisov | dcdbc05 | 2017-02-20 13:50:45 +0200 | [diff] [blame] | 4813 | btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset, |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4814 | cur_offset + hole_size - 1, 0); |
| 4815 | hole_em = alloc_extent_map(); |
| 4816 | if (!hole_em) { |
| 4817 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, |
| 4818 | &BTRFS_I(inode)->runtime_flags); |
| 4819 | goto next; |
| 4820 | } |
| 4821 | hole_em->start = cur_offset; |
| 4822 | hole_em->len = hole_size; |
| 4823 | hole_em->orig_start = cur_offset; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4824 | |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4825 | hole_em->block_start = EXTENT_MAP_HOLE; |
| 4826 | hole_em->block_len = 0; |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 4827 | hole_em->orig_block_len = 0; |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 4828 | hole_em->ram_bytes = hole_size; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4829 | hole_em->compress_type = BTRFS_COMPRESS_NONE; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4830 | hole_em->generation = fs_info->generation; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4831 | |
| 4832 | while (1) { |
| 4833 | write_lock(&em_tree->lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 4834 | err = add_extent_mapping(em_tree, hole_em, 1); |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4835 | write_unlock(&em_tree->lock); |
| 4836 | if (err != -EEXIST) |
| 4837 | break; |
Nikolay Borisov | dcdbc05 | 2017-02-20 13:50:45 +0200 | [diff] [blame] | 4838 | btrfs_drop_extent_cache(BTRFS_I(inode), |
| 4839 | cur_offset, |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4840 | cur_offset + |
| 4841 | hole_size - 1, 0); |
| 4842 | } |
| 4843 | free_extent_map(hole_em); |
Josef Bacik | 9ddc959 | 2020-01-17 09:02:22 -0500 | [diff] [blame] | 4844 | } else { |
| 4845 | err = btrfs_inode_set_file_extent_range(BTRFS_I(inode), |
| 4846 | cur_offset, hole_size); |
| 4847 | if (err) |
| 4848 | break; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4849 | } |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4850 | next: |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4851 | free_extent_map(em); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4852 | em = NULL; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4853 | cur_offset = last_byte; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4854 | if (cur_offset >= block_end) |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4855 | break; |
| 4856 | } |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4857 | free_extent_map(em); |
David Sterba | e43bbe5 | 2017-12-12 21:43:52 +0100 | [diff] [blame] | 4858 | unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4859 | return err; |
| 4860 | } |
| 4861 | |
Eric Sandeen | 3972f26 | 2013-01-12 02:57:22 +0000 | [diff] [blame] | 4862 | static int btrfs_setsize(struct inode *inode, struct iattr *attr) |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4863 | { |
Miao Xie | f4a2f4c | 2011-12-14 20:12:01 -0500 | [diff] [blame] | 4864 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 4865 | struct btrfs_trans_handle *trans; |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 4866 | loff_t oldsize = i_size_read(inode); |
Eric Sandeen | 3972f26 | 2013-01-12 02:57:22 +0000 | [diff] [blame] | 4867 | loff_t newsize = attr->ia_size; |
| 4868 | int mask = attr->ia_valid; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4869 | int ret; |
| 4870 | |
Eric Sandeen | 3972f26 | 2013-01-12 02:57:22 +0000 | [diff] [blame] | 4871 | /* |
| 4872 | * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a |
| 4873 | * special case where we need to update the times despite not having |
| 4874 | * these flags set. For all other operations the VFS set these flags |
| 4875 | * explicitly if it wants a timestamp update. |
| 4876 | */ |
Christoph Hellwig | dff6efc | 2013-11-19 07:17:07 -0800 | [diff] [blame] | 4877 | if (newsize != oldsize) { |
| 4878 | inode_inc_iversion(inode); |
| 4879 | if (!(mask & (ATTR_CTIME | ATTR_MTIME))) |
| 4880 | inode->i_ctime = inode->i_mtime = |
Deepa Dinamani | c2050a4 | 2016-09-14 07:48:06 -0700 | [diff] [blame] | 4881 | current_time(inode); |
Christoph Hellwig | dff6efc | 2013-11-19 07:17:07 -0800 | [diff] [blame] | 4882 | } |
Eric Sandeen | 3972f26 | 2013-01-12 02:57:22 +0000 | [diff] [blame] | 4883 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 4884 | if (newsize > oldsize) { |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 4885 | /* |
David Sterba | ea14b57f | 2017-06-22 02:19:11 +0200 | [diff] [blame] | 4886 | * Don't do an expanding truncate while snapshotting is ongoing. |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 4887 | * This is to ensure the snapshot captures a fully consistent |
| 4888 | * state of this file - if the snapshot captures this expanding |
| 4889 | * truncation, it must capture all writes that happened before |
| 4890 | * this truncation. |
| 4891 | */ |
Nikolay Borisov | dcc3eb9 | 2020-01-30 14:59:45 +0200 | [diff] [blame] | 4892 | btrfs_drew_write_lock(&root->snapshot_lock); |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 4893 | ret = btrfs_cont_expand(inode, oldsize, newsize); |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 4894 | if (ret) { |
Nikolay Borisov | dcc3eb9 | 2020-01-30 14:59:45 +0200 | [diff] [blame] | 4895 | btrfs_drew_write_unlock(&root->snapshot_lock); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4896 | return ret; |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 4897 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4898 | |
Miao Xie | f4a2f4c | 2011-12-14 20:12:01 -0500 | [diff] [blame] | 4899 | trans = btrfs_start_transaction(root, 1); |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 4900 | if (IS_ERR(trans)) { |
Nikolay Borisov | dcc3eb9 | 2020-01-30 14:59:45 +0200 | [diff] [blame] | 4901 | btrfs_drew_write_unlock(&root->snapshot_lock); |
Miao Xie | f4a2f4c | 2011-12-14 20:12:01 -0500 | [diff] [blame] | 4902 | return PTR_ERR(trans); |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 4903 | } |
Miao Xie | f4a2f4c | 2011-12-14 20:12:01 -0500 | [diff] [blame] | 4904 | |
| 4905 | i_size_write(inode, newsize); |
Josef Bacik | d923afe | 2020-01-17 09:02:23 -0500 | [diff] [blame] | 4906 | btrfs_inode_safe_disk_i_size_write(inode, 0); |
Chandan Rajendra | 27772b6 | 2016-01-21 15:56:03 +0530 | [diff] [blame] | 4907 | pagecache_isize_extended(inode, oldsize, newsize); |
Miao Xie | f4a2f4c | 2011-12-14 20:12:01 -0500 | [diff] [blame] | 4908 | ret = btrfs_update_inode(trans, root, inode); |
Nikolay Borisov | dcc3eb9 | 2020-01-30 14:59:45 +0200 | [diff] [blame] | 4909 | btrfs_drew_write_unlock(&root->snapshot_lock); |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 4910 | btrfs_end_transaction(trans); |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 4911 | } else { |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4912 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 4913 | /* |
| 4914 | * 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] | 4915 | * zero. Make sure any new writes to the file get on disk |
| 4916 | * on close. |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 4917 | */ |
| 4918 | if (newsize == 0) |
Nikolay Borisov | 1fd4033 | 2020-10-01 09:40:39 +0300 | [diff] [blame] | 4919 | set_bit(BTRFS_INODE_FLUSH_ON_CLOSE, |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 4920 | &BTRFS_I(inode)->runtime_flags); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4921 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 4922 | truncate_setsize(inode, newsize); |
Miao Xie | 2e60a51 | 2013-02-08 07:01:08 +0000 | [diff] [blame] | 4923 | |
Miao Xie | 2e60a51 | 2013-02-08 07:01:08 +0000 | [diff] [blame] | 4924 | inode_dio_wait(inode); |
Miao Xie | 2e60a51 | 2013-02-08 07:01:08 +0000 | [diff] [blame] | 4925 | |
Filipe Manana | 213e8c5 | 2018-02-06 20:40:31 +0000 | [diff] [blame] | 4926 | ret = btrfs_truncate(inode, newsize == oldsize); |
Josef Bacik | 7f4f6e0 | 2013-08-29 16:43:28 -0400 | [diff] [blame] | 4927 | if (ret && inode->i_nlink) { |
| 4928 | int err; |
| 4929 | |
| 4930 | /* |
Omar Sandoval | f7e9e8f | 2018-05-11 13:13:32 -0700 | [diff] [blame] | 4931 | * Truncate failed, so fix up the in-memory size. We |
| 4932 | * adjusted disk_i_size down as we removed extents, so |
| 4933 | * wait for disk_i_size to be stable and then update the |
| 4934 | * in-memory size to match. |
Josef Bacik | 7f4f6e0 | 2013-08-29 16:43:28 -0400 | [diff] [blame] | 4935 | */ |
Omar Sandoval | f7e9e8f | 2018-05-11 13:13:32 -0700 | [diff] [blame] | 4936 | err = btrfs_wait_ordered_range(inode, 0, (u64)-1); |
Josef Bacik | 7f4f6e0 | 2013-08-29 16:43:28 -0400 | [diff] [blame] | 4937 | if (err) |
Omar Sandoval | f7e9e8f | 2018-05-11 13:13:32 -0700 | [diff] [blame] | 4938 | return err; |
| 4939 | i_size_write(inode, BTRFS_I(inode)->disk_i_size); |
Josef Bacik | 7f4f6e0 | 2013-08-29 16:43:28 -0400 | [diff] [blame] | 4940 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4941 | } |
| 4942 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 4943 | return ret; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4944 | } |
| 4945 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4946 | static int btrfs_setattr(struct dentry *dentry, struct iattr *attr) |
| 4947 | { |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 4948 | struct inode *inode = d_inode(dentry); |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 4949 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4950 | int err; |
| 4951 | |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 4952 | if (btrfs_root_readonly(root)) |
| 4953 | return -EROFS; |
| 4954 | |
Jan Kara | 31051c8 | 2016-05-26 16:55:18 +0200 | [diff] [blame] | 4955 | err = setattr_prepare(dentry, attr); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4956 | if (err) |
| 4957 | return err; |
| 4958 | |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 4959 | if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) { |
Eric Sandeen | 3972f26 | 2013-01-12 02:57:22 +0000 | [diff] [blame] | 4960 | err = btrfs_setsize(inode, attr); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4961 | if (err) |
| 4962 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4963 | } |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4964 | |
Christoph Hellwig | 1025774 | 2010-06-04 11:30:02 +0200 | [diff] [blame] | 4965 | if (attr->ia_valid) { |
| 4966 | setattr_copy(inode, attr); |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 4967 | inode_inc_iversion(inode); |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 4968 | err = btrfs_dirty_inode(inode); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 4969 | |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 4970 | if (!err && attr->ia_valid & ATTR_MODE) |
Christoph Hellwig | 996a710 | 2013-12-20 05:16:43 -0800 | [diff] [blame] | 4971 | err = posix_acl_chmod(inode, inode->i_mode); |
Christoph Hellwig | 1025774 | 2010-06-04 11:30:02 +0200 | [diff] [blame] | 4972 | } |
| 4973 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4974 | return err; |
| 4975 | } |
Chris Mason | 61295eb | 2008-01-14 16:24:38 -0500 | [diff] [blame] | 4976 | |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 4977 | /* |
| 4978 | * While truncating the inode pages during eviction, we get the VFS calling |
| 4979 | * btrfs_invalidatepage() against each page of the inode. This is slow because |
| 4980 | * the calls to btrfs_invalidatepage() result in a huge amount of calls to |
| 4981 | * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting |
| 4982 | * extent_state structures over and over, wasting lots of time. |
| 4983 | * |
| 4984 | * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all |
| 4985 | * those expensive operations on a per page basis and do only the ordered io |
| 4986 | * finishing, while we release here the extent_map and extent_state structures, |
| 4987 | * without the excessive merging and splitting. |
| 4988 | */ |
| 4989 | static void evict_inode_truncate_pages(struct inode *inode) |
| 4990 | { |
| 4991 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
| 4992 | struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree; |
| 4993 | struct rb_node *node; |
| 4994 | |
| 4995 | ASSERT(inode->i_state & I_FREEING); |
Johannes Weiner | 91b0abe | 2014-04-03 14:47:49 -0700 | [diff] [blame] | 4996 | truncate_inode_pages_final(&inode->i_data); |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 4997 | |
| 4998 | write_lock(&map_tree->lock); |
Liu Bo | 07e1ce0 | 2018-08-23 03:51:52 +0800 | [diff] [blame] | 4999 | while (!RB_EMPTY_ROOT(&map_tree->map.rb_root)) { |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5000 | struct extent_map *em; |
| 5001 | |
Liu Bo | 07e1ce0 | 2018-08-23 03:51:52 +0800 | [diff] [blame] | 5002 | node = rb_first_cached(&map_tree->map); |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5003 | em = rb_entry(node, struct extent_map, rb_node); |
Wang Shilong | 180589e | 2013-12-14 15:27:31 +0800 | [diff] [blame] | 5004 | clear_bit(EXTENT_FLAG_PINNED, &em->flags); |
| 5005 | clear_bit(EXTENT_FLAG_LOGGING, &em->flags); |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5006 | remove_extent_mapping(map_tree, em); |
| 5007 | free_extent_map(em); |
Filipe Manana | 7064dd5 | 2014-08-08 02:47:05 +0100 | [diff] [blame] | 5008 | if (need_resched()) { |
| 5009 | write_unlock(&map_tree->lock); |
| 5010 | cond_resched(); |
| 5011 | write_lock(&map_tree->lock); |
| 5012 | } |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5013 | } |
| 5014 | write_unlock(&map_tree->lock); |
| 5015 | |
Filipe Manana | 6ca0709 | 2015-05-26 00:55:42 +0100 | [diff] [blame] | 5016 | /* |
| 5017 | * 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] | 5018 | * We can have ongoing bios started by readahead that have |
| 5019 | * their endio callback (extent_io.c:end_bio_extent_readpage) |
Filipe Manana | 9c6429d | 2015-06-10 12:55:41 +0100 | [diff] [blame] | 5020 | * still in progress (unlocked the pages in the bio but did not yet |
| 5021 | * unlocked the ranges in the io tree). Therefore this means some |
Filipe Manana | 6ca0709 | 2015-05-26 00:55:42 +0100 | [diff] [blame] | 5022 | * ranges can still be locked and eviction started because before |
| 5023 | * submitting those bios, which are executed by a separate task (work |
| 5024 | * queue kthread), inode references (inode->i_count) were not taken |
| 5025 | * (which would be dropped in the end io callback of each bio). |
| 5026 | * Therefore here we effectively end up waiting for those bios and |
| 5027 | * anyone else holding locked ranges without having bumped the inode's |
| 5028 | * reference count - if we don't do it, when they access the inode's |
| 5029 | * io_tree to unlock a range it may be too late, leading to an |
| 5030 | * use-after-free issue. |
| 5031 | */ |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5032 | spin_lock(&io_tree->lock); |
| 5033 | while (!RB_EMPTY_ROOT(&io_tree->state)) { |
| 5034 | struct extent_state *state; |
| 5035 | struct extent_state *cached_state = NULL; |
Filipe Manana | 6ca0709 | 2015-05-26 00:55:42 +0100 | [diff] [blame] | 5036 | u64 start; |
| 5037 | u64 end; |
Filipe Manana | 421f092 | 2018-10-12 13:02:48 +0100 | [diff] [blame] | 5038 | unsigned state_flags; |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5039 | |
| 5040 | node = rb_first(&io_tree->state); |
| 5041 | state = rb_entry(node, struct extent_state, rb_node); |
Filipe Manana | 6ca0709 | 2015-05-26 00:55:42 +0100 | [diff] [blame] | 5042 | start = state->start; |
| 5043 | end = state->end; |
Filipe Manana | 421f092 | 2018-10-12 13:02:48 +0100 | [diff] [blame] | 5044 | state_flags = state->state; |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5045 | spin_unlock(&io_tree->lock); |
| 5046 | |
David Sterba | ff13db4 | 2015-12-03 14:30:40 +0100 | [diff] [blame] | 5047 | lock_extent_bits(io_tree, start, end, &cached_state); |
Qu Wenruo | b9d0b38 | 2015-09-29 10:35:16 +0800 | [diff] [blame] | 5048 | |
| 5049 | /* |
| 5050 | * If still has DELALLOC flag, the extent didn't reach disk, |
| 5051 | * and its reserved space won't be freed by delayed_ref. |
| 5052 | * So we need to free its reserved space here. |
| 5053 | * (Refer to comment in btrfs_invalidatepage, case 2) |
| 5054 | * |
| 5055 | * Note, end is the bytenr of last byte, so we need + 1 here. |
| 5056 | */ |
Filipe Manana | 421f092 | 2018-10-12 13:02:48 +0100 | [diff] [blame] | 5057 | if (state_flags & EXTENT_DELALLOC) |
Nikolay Borisov | 8b8a979 | 2020-06-03 08:55:11 +0300 | [diff] [blame] | 5058 | btrfs_qgroup_free_data(BTRFS_I(inode), NULL, start, |
| 5059 | end - start + 1); |
Qu Wenruo | b9d0b38 | 2015-09-29 10:35:16 +0800 | [diff] [blame] | 5060 | |
Filipe Manana | 6ca0709 | 2015-05-26 00:55:42 +0100 | [diff] [blame] | 5061 | clear_extent_bit(io_tree, start, end, |
Omar Sandoval | e182163 | 2019-08-15 14:04:04 -0700 | [diff] [blame] | 5062 | EXTENT_LOCKED | EXTENT_DELALLOC | |
| 5063 | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1, |
| 5064 | &cached_state); |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5065 | |
Filipe Manana | 7064dd5 | 2014-08-08 02:47:05 +0100 | [diff] [blame] | 5066 | cond_resched(); |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5067 | spin_lock(&io_tree->lock); |
| 5068 | } |
| 5069 | spin_unlock(&io_tree->lock); |
| 5070 | } |
| 5071 | |
Omar Sandoval | 4b9d7b5 | 2018-05-11 13:13:36 -0700 | [diff] [blame] | 5072 | 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] | 5073 | struct btrfs_block_rsv *rsv) |
Omar Sandoval | 4b9d7b5 | 2018-05-11 13:13:36 -0700 | [diff] [blame] | 5074 | { |
| 5075 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 5076 | struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv; |
Josef Bacik | d3984c9 | 2019-08-01 18:19:37 -0400 | [diff] [blame] | 5077 | struct btrfs_trans_handle *trans; |
Josef Bacik | 2bd36e7 | 2019-08-22 15:14:33 -0400 | [diff] [blame] | 5078 | u64 delayed_refs_extra = btrfs_calc_insert_metadata_size(fs_info, 1); |
Josef Bacik | d3984c9 | 2019-08-01 18:19:37 -0400 | [diff] [blame] | 5079 | int ret; |
Omar Sandoval | 4b9d7b5 | 2018-05-11 13:13:36 -0700 | [diff] [blame] | 5080 | |
Josef Bacik | d3984c9 | 2019-08-01 18:19:37 -0400 | [diff] [blame] | 5081 | /* |
| 5082 | * Eviction should be taking place at some place safe because of our |
| 5083 | * delayed iputs. However the normal flushing code will run delayed |
| 5084 | * iputs, so we cannot use FLUSH_ALL otherwise we'll deadlock. |
| 5085 | * |
| 5086 | * We reserve the delayed_refs_extra here again because we can't use |
| 5087 | * btrfs_start_transaction(root, 0) for the same deadlocky reason as |
| 5088 | * above. We reserve our extra bit here because we generate a ton of |
| 5089 | * delayed refs activity by truncating. |
| 5090 | * |
| 5091 | * If we cannot make our reservation we'll attempt to steal from the |
| 5092 | * global reserve, because we really want to be able to free up space. |
| 5093 | */ |
| 5094 | ret = btrfs_block_rsv_refill(root, rsv, rsv->size + delayed_refs_extra, |
| 5095 | BTRFS_RESERVE_FLUSH_EVICT); |
| 5096 | if (ret) { |
Omar Sandoval | 4b9d7b5 | 2018-05-11 13:13:36 -0700 | [diff] [blame] | 5097 | /* |
| 5098 | * Try to steal from the global reserve if there is space for |
| 5099 | * it. |
| 5100 | */ |
Josef Bacik | d3984c9 | 2019-08-01 18:19:37 -0400 | [diff] [blame] | 5101 | if (btrfs_check_space_for_delayed_refs(fs_info) || |
| 5102 | btrfs_block_rsv_migrate(global_rsv, rsv, rsv->size, 0)) { |
| 5103 | btrfs_warn(fs_info, |
| 5104 | "could not allocate space for delete; will truncate on mount"); |
| 5105 | return ERR_PTR(-ENOSPC); |
| 5106 | } |
| 5107 | delayed_refs_extra = 0; |
Omar Sandoval | 4b9d7b5 | 2018-05-11 13:13:36 -0700 | [diff] [blame] | 5108 | } |
Josef Bacik | d3984c9 | 2019-08-01 18:19:37 -0400 | [diff] [blame] | 5109 | |
| 5110 | trans = btrfs_join_transaction(root); |
| 5111 | if (IS_ERR(trans)) |
| 5112 | return trans; |
| 5113 | |
| 5114 | if (delayed_refs_extra) { |
| 5115 | trans->block_rsv = &fs_info->trans_block_rsv; |
| 5116 | trans->bytes_reserved = delayed_refs_extra; |
| 5117 | btrfs_block_rsv_migrate(rsv, trans->block_rsv, |
| 5118 | delayed_refs_extra, 1); |
| 5119 | } |
| 5120 | return trans; |
Omar Sandoval | 4b9d7b5 | 2018-05-11 13:13:36 -0700 | [diff] [blame] | 5121 | } |
| 5122 | |
Al Viro | bd55597 | 2010-06-07 11:35:40 -0400 | [diff] [blame] | 5123 | void btrfs_evict_inode(struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5124 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5125 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5126 | struct btrfs_trans_handle *trans; |
| 5127 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Omar Sandoval | 4b9d7b5 | 2018-05-11 13:13:36 -0700 | [diff] [blame] | 5128 | struct btrfs_block_rsv *rsv; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5129 | int ret; |
| 5130 | |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 5131 | trace_btrfs_inode_evict(inode); |
| 5132 | |
Nikolay Borisov | 3d48d98 | 2016-06-29 09:46:41 +0300 | [diff] [blame] | 5133 | if (!root) { |
Liu Bo | e8f1bc1 | 2018-01-25 11:02:53 -0700 | [diff] [blame] | 5134 | clear_inode(inode); |
Nikolay Borisov | 3d48d98 | 2016-06-29 09:46:41 +0300 | [diff] [blame] | 5135 | return; |
| 5136 | } |
| 5137 | |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5138 | evict_inode_truncate_pages(inode); |
| 5139 | |
Stefan Behrens | 69e9c6c | 2013-09-05 16:58:43 +0200 | [diff] [blame] | 5140 | if (inode->i_nlink && |
| 5141 | ((btrfs_root_refs(&root->root_item) != 0 && |
| 5142 | root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) || |
Nikolay Borisov | 70ddc55 | 2017-02-20 13:50:35 +0200 | [diff] [blame] | 5143 | btrfs_is_free_space_inode(BTRFS_I(inode)))) |
Al Viro | bd55597 | 2010-06-07 11:35:40 -0400 | [diff] [blame] | 5144 | goto no_delete; |
| 5145 | |
Omar Sandoval | 2791906 | 2018-05-11 13:13:37 -0700 | [diff] [blame] | 5146 | if (is_bad_inode(inode)) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5147 | goto no_delete; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5148 | |
Nikolay Borisov | 7ab7956 | 2017-02-20 13:50:57 +0200 | [diff] [blame] | 5149 | btrfs_free_io_failure_record(BTRFS_I(inode), 0, (u64)-1); |
Miao Xie | f612496 | 2014-09-12 18:44:04 +0800 | [diff] [blame] | 5150 | |
Omar Sandoval | 7b40b69 | 2018-05-11 13:13:33 -0700 | [diff] [blame] | 5151 | if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 5152 | goto no_delete; |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 5153 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5154 | if (inode->i_nlink > 0) { |
Stefan Behrens | 69e9c6c | 2013-09-05 16:58:43 +0200 | [diff] [blame] | 5155 | BUG_ON(btrfs_root_refs(&root->root_item) != 0 && |
| 5156 | root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5157 | goto no_delete; |
| 5158 | } |
| 5159 | |
Nikolay Borisov | aa79021 | 2017-01-10 20:35:40 +0200 | [diff] [blame] | 5160 | ret = btrfs_commit_inode_delayed_inode(BTRFS_I(inode)); |
Omar Sandoval | 2791906 | 2018-05-11 13:13:37 -0700 | [diff] [blame] | 5161 | if (ret) |
Miao Xie | 0e8c36a | 2012-12-19 06:59:51 +0000 | [diff] [blame] | 5162 | goto no_delete; |
Miao Xie | 0e8c36a | 2012-12-19 06:59:51 +0000 | [diff] [blame] | 5163 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5164 | rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP); |
Omar Sandoval | 2791906 | 2018-05-11 13:13:37 -0700 | [diff] [blame] | 5165 | if (!rsv) |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 5166 | goto no_delete; |
Josef Bacik | 2bd36e7 | 2019-08-22 15:14:33 -0400 | [diff] [blame] | 5167 | rsv->size = btrfs_calc_metadata_size(fs_info, 1); |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 5168 | rsv->failfast = 1; |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 5169 | |
Nikolay Borisov | 6ef06d2 | 2017-02-20 13:50:34 +0200 | [diff] [blame] | 5170 | btrfs_i_size_write(BTRFS_I(inode), 0); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5171 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5172 | while (1) { |
Josef Bacik | ad80cf5 | 2018-09-28 07:18:19 -0400 | [diff] [blame] | 5173 | trans = evict_refill_and_join(root, rsv); |
Omar Sandoval | 2791906 | 2018-05-11 13:13:37 -0700 | [diff] [blame] | 5174 | if (IS_ERR(trans)) |
| 5175 | goto free_rsv; |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 5176 | |
| 5177 | trans->block_rsv = rsv; |
| 5178 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5179 | ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0); |
Omar Sandoval | 2791906 | 2018-05-11 13:13:37 -0700 | [diff] [blame] | 5180 | trans->block_rsv = &fs_info->trans_block_rsv; |
| 5181 | btrfs_end_transaction(trans); |
| 5182 | btrfs_btree_balance_dirty(fs_info); |
| 5183 | if (ret && ret != -ENOSPC && ret != -EAGAIN) |
| 5184 | goto free_rsv; |
| 5185 | else if (!ret) |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5186 | break; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 5187 | } |
| 5188 | |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 5189 | /* |
Omar Sandoval | 2791906 | 2018-05-11 13:13:37 -0700 | [diff] [blame] | 5190 | * Errors here aren't a big deal, it just means we leave orphan items in |
| 5191 | * the tree. They will be cleaned up on the next mount. If the inode |
| 5192 | * number gets reused, cleanup deletes the orphan item without doing |
| 5193 | * anything, and unlink reuses the existing orphan item. |
| 5194 | * |
| 5195 | * If it turns out that we are dropping too many of these, we might want |
| 5196 | * to add a mechanism for retrying these after a commit. |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 5197 | */ |
Josef Bacik | ad80cf5 | 2018-09-28 07:18:19 -0400 | [diff] [blame] | 5198 | trans = evict_refill_and_join(root, rsv); |
Omar Sandoval | 2791906 | 2018-05-11 13:13:37 -0700 | [diff] [blame] | 5199 | if (!IS_ERR(trans)) { |
| 5200 | trans->block_rsv = rsv; |
| 5201 | btrfs_orphan_del(trans, BTRFS_I(inode)); |
| 5202 | trans->block_rsv = &fs_info->trans_block_rsv; |
| 5203 | btrfs_end_transaction(trans); |
| 5204 | } |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 5205 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5206 | if (!(root == fs_info->tree_root || |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 5207 | root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)) |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 5208 | btrfs_return_ino(root, btrfs_ino(BTRFS_I(inode))); |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 5209 | |
Omar Sandoval | 2791906 | 2018-05-11 13:13:37 -0700 | [diff] [blame] | 5210 | free_rsv: |
| 5211 | btrfs_free_block_rsv(fs_info, rsv); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5212 | no_delete: |
Omar Sandoval | 2791906 | 2018-05-11 13:13:37 -0700 | [diff] [blame] | 5213 | /* |
| 5214 | * If we didn't successfully delete, the orphan item will still be in |
| 5215 | * the tree and we'll retry on the next mount. Again, we might also want |
| 5216 | * to retry these periodically in the future. |
| 5217 | */ |
Nikolay Borisov | f48d1cf | 2017-01-10 20:35:39 +0200 | [diff] [blame] | 5218 | btrfs_remove_delayed_node(BTRFS_I(inode)); |
Jan Kara | dbd5768 | 2012-05-03 14:48:02 +0200 | [diff] [blame] | 5219 | clear_inode(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5220 | } |
| 5221 | |
| 5222 | /* |
Qu Wenruo | 6bf9e4b | 2019-03-13 13:55:11 +0800 | [diff] [blame] | 5223 | * Return the key found in the dir entry in the location pointer, fill @type |
| 5224 | * with BTRFS_FT_*, and return 0. |
| 5225 | * |
Su Yue | 005d671 | 2018-03-05 17:13:37 +0800 | [diff] [blame] | 5226 | * If no dir entries were found, returns -ENOENT. |
| 5227 | * If found a corrupted location in dir entry, returns -EUCLEAN. |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5228 | */ |
| 5229 | static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry, |
Qu Wenruo | 6bf9e4b | 2019-03-13 13:55:11 +0800 | [diff] [blame] | 5230 | struct btrfs_key *location, u8 *type) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5231 | { |
| 5232 | const char *name = dentry->d_name.name; |
| 5233 | int namelen = dentry->d_name.len; |
| 5234 | struct btrfs_dir_item *di; |
| 5235 | struct btrfs_path *path; |
| 5236 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Yan | 0d9f7f3 | 2007-10-25 15:48:28 -0400 | [diff] [blame] | 5237 | int ret = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5238 | |
| 5239 | path = btrfs_alloc_path(); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 5240 | if (!path) |
| 5241 | return -ENOMEM; |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 5242 | |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 5243 | di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(BTRFS_I(dir)), |
| 5244 | name, namelen, 0); |
Liu Bo | 3cf5068 | 2018-09-12 06:06:26 +0800 | [diff] [blame] | 5245 | if (IS_ERR_OR_NULL(di)) { |
| 5246 | ret = di ? PTR_ERR(di) : -ENOENT; |
Su Yue | 005d671 | 2018-03-05 17:13:37 +0800 | [diff] [blame] | 5247 | goto out; |
| 5248 | } |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5249 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5250 | btrfs_dir_item_key_to_cpu(path->nodes[0], di, location); |
Liu Bo | 56a0e70 | 2017-10-30 11:14:38 -0600 | [diff] [blame] | 5251 | if (location->type != BTRFS_INODE_ITEM_KEY && |
| 5252 | location->type != BTRFS_ROOT_ITEM_KEY) { |
Su Yue | 005d671 | 2018-03-05 17:13:37 +0800 | [diff] [blame] | 5253 | ret = -EUCLEAN; |
Liu Bo | 56a0e70 | 2017-10-30 11:14:38 -0600 | [diff] [blame] | 5254 | btrfs_warn(root->fs_info, |
| 5255 | "%s gets something invalid in DIR_ITEM (name %s, directory ino %llu, location(%llu %u %llu))", |
| 5256 | __func__, name, btrfs_ino(BTRFS_I(dir)), |
| 5257 | location->objectid, location->type, location->offset); |
Liu Bo | 56a0e70 | 2017-10-30 11:14:38 -0600 | [diff] [blame] | 5258 | } |
Qu Wenruo | 6bf9e4b | 2019-03-13 13:55:11 +0800 | [diff] [blame] | 5259 | if (!ret) |
| 5260 | *type = btrfs_dir_type(path->nodes[0], di); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5261 | out: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5262 | btrfs_free_path(path); |
| 5263 | return ret; |
| 5264 | } |
| 5265 | |
| 5266 | /* |
| 5267 | * when we hit a tree root in a directory, the btrfs part of the inode |
| 5268 | * needs to be changed to reflect the root directory of the tree root. This |
| 5269 | * is kind of like crossing a mount point. |
| 5270 | */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5271 | static int fixup_tree_root_location(struct btrfs_fs_info *fs_info, |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5272 | struct inode *dir, |
| 5273 | struct dentry *dentry, |
| 5274 | struct btrfs_key *location, |
| 5275 | struct btrfs_root **sub_root) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5276 | { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5277 | struct btrfs_path *path; |
| 5278 | struct btrfs_root *new_root; |
| 5279 | struct btrfs_root_ref *ref; |
| 5280 | struct extent_buffer *leaf; |
David Sterba | 1d4c08e | 2015-01-02 19:36:14 +0100 | [diff] [blame] | 5281 | struct btrfs_key key; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5282 | int ret; |
| 5283 | int err = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5284 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5285 | path = btrfs_alloc_path(); |
| 5286 | if (!path) { |
| 5287 | err = -ENOMEM; |
| 5288 | goto out; |
| 5289 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5290 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5291 | err = -ENOENT; |
David Sterba | 1d4c08e | 2015-01-02 19:36:14 +0100 | [diff] [blame] | 5292 | key.objectid = BTRFS_I(dir)->root->root_key.objectid; |
| 5293 | key.type = BTRFS_ROOT_REF_KEY; |
| 5294 | key.offset = location->objectid; |
| 5295 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5296 | 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] | 5297 | if (ret) { |
| 5298 | if (ret < 0) |
| 5299 | err = ret; |
| 5300 | goto out; |
| 5301 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5302 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5303 | leaf = path->nodes[0]; |
| 5304 | ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref); |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 5305 | if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(BTRFS_I(dir)) || |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5306 | btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len) |
| 5307 | goto out; |
| 5308 | |
| 5309 | ret = memcmp_extent_buffer(leaf, dentry->d_name.name, |
| 5310 | (unsigned long)(ref + 1), |
| 5311 | dentry->d_name.len); |
| 5312 | if (ret) |
| 5313 | goto out; |
| 5314 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 5315 | btrfs_release_path(path); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5316 | |
David Sterba | 56e9357 | 2020-05-15 19:35:55 +0200 | [diff] [blame] | 5317 | new_root = btrfs_get_fs_root(fs_info, location->objectid, true); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5318 | if (IS_ERR(new_root)) { |
| 5319 | err = PTR_ERR(new_root); |
| 5320 | goto out; |
| 5321 | } |
| 5322 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5323 | *sub_root = new_root; |
| 5324 | location->objectid = btrfs_root_dirid(&new_root->root_item); |
| 5325 | location->type = BTRFS_INODE_ITEM_KEY; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5326 | location->offset = 0; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5327 | err = 0; |
| 5328 | out: |
| 5329 | btrfs_free_path(path); |
| 5330 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5331 | } |
| 5332 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5333 | static void inode_tree_add(struct inode *inode) |
| 5334 | { |
| 5335 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5336 | struct btrfs_inode *entry; |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 5337 | struct rb_node **p; |
| 5338 | struct rb_node *parent; |
Filipe David Borba Manana | cef2193 | 2013-09-02 12:19:13 +0100 | [diff] [blame] | 5339 | struct rb_node *new = &BTRFS_I(inode)->rb_node; |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 5340 | u64 ino = btrfs_ino(BTRFS_I(inode)); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5341 | |
Al Viro | 1d3382cb | 2010-10-23 15:19:20 -0400 | [diff] [blame] | 5342 | if (inode_unhashed(inode)) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5343 | return; |
Miao Xie | e1409ce | 2013-05-15 07:48:16 +0000 | [diff] [blame] | 5344 | parent = NULL; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5345 | spin_lock(&root->inode_lock); |
Miao Xie | e1409ce | 2013-05-15 07:48:16 +0000 | [diff] [blame] | 5346 | p = &root->inode_tree.rb_node; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5347 | while (*p) { |
| 5348 | parent = *p; |
| 5349 | entry = rb_entry(parent, struct btrfs_inode, rb_node); |
| 5350 | |
David Sterba | 3750851 | 2018-06-29 10:56:40 +0200 | [diff] [blame] | 5351 | if (ino < btrfs_ino(entry)) |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 5352 | p = &parent->rb_left; |
David Sterba | 3750851 | 2018-06-29 10:56:40 +0200 | [diff] [blame] | 5353 | else if (ino > btrfs_ino(entry)) |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 5354 | p = &parent->rb_right; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5355 | else { |
| 5356 | WARN_ON(!(entry->vfs_inode.i_state & |
Al Viro | a4ffdde | 2010-06-02 17:38:30 -0400 | [diff] [blame] | 5357 | (I_WILL_FREE | I_FREEING))); |
Filipe David Borba Manana | cef2193 | 2013-09-02 12:19:13 +0100 | [diff] [blame] | 5358 | rb_replace_node(parent, new, &root->inode_tree); |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 5359 | RB_CLEAR_NODE(parent); |
| 5360 | spin_unlock(&root->inode_lock); |
Filipe David Borba Manana | cef2193 | 2013-09-02 12:19:13 +0100 | [diff] [blame] | 5361 | return; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5362 | } |
| 5363 | } |
Filipe David Borba Manana | cef2193 | 2013-09-02 12:19:13 +0100 | [diff] [blame] | 5364 | rb_link_node(new, parent, p); |
| 5365 | rb_insert_color(new, &root->inode_tree); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5366 | spin_unlock(&root->inode_lock); |
| 5367 | } |
| 5368 | |
Nikolay Borisov | b79b724 | 2020-08-31 14:42:38 +0300 | [diff] [blame] | 5369 | static void inode_tree_del(struct btrfs_inode *inode) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5370 | { |
Nikolay Borisov | b79b724 | 2020-08-31 14:42:38 +0300 | [diff] [blame] | 5371 | struct btrfs_root *root = inode->root; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5372 | int empty = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5373 | |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 5374 | spin_lock(&root->inode_lock); |
Nikolay Borisov | b79b724 | 2020-08-31 14:42:38 +0300 | [diff] [blame] | 5375 | if (!RB_EMPTY_NODE(&inode->rb_node)) { |
| 5376 | rb_erase(&inode->rb_node, &root->inode_tree); |
| 5377 | RB_CLEAR_NODE(&inode->rb_node); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5378 | empty = RB_EMPTY_ROOT(&root->inode_tree); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5379 | } |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 5380 | spin_unlock(&root->inode_lock); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5381 | |
Stefan Behrens | 69e9c6c | 2013-09-05 16:58:43 +0200 | [diff] [blame] | 5382 | if (empty && btrfs_root_refs(&root->root_item) == 0) { |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5383 | spin_lock(&root->inode_lock); |
| 5384 | empty = RB_EMPTY_ROOT(&root->inode_tree); |
| 5385 | spin_unlock(&root->inode_lock); |
| 5386 | if (empty) |
| 5387 | btrfs_add_dead_root(root); |
| 5388 | } |
| 5389 | } |
| 5390 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5391 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5392 | static int btrfs_init_locked_inode(struct inode *inode, void *p) |
| 5393 | { |
| 5394 | struct btrfs_iget_args *args = p; |
David Sterba | 0202e83 | 2020-05-15 19:35:59 +0200 | [diff] [blame] | 5395 | |
| 5396 | inode->i_ino = args->ino; |
| 5397 | BTRFS_I(inode)->location.objectid = args->ino; |
| 5398 | BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY; |
| 5399 | BTRFS_I(inode)->location.offset = 0; |
Josef Bacik | 5c8fd99 | 2020-02-14 16:11:43 -0500 | [diff] [blame] | 5400 | BTRFS_I(inode)->root = btrfs_grab_root(args->root); |
| 5401 | BUG_ON(args->root && !BTRFS_I(inode)->root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5402 | return 0; |
| 5403 | } |
| 5404 | |
| 5405 | static int btrfs_find_actor(struct inode *inode, void *opaque) |
| 5406 | { |
| 5407 | struct btrfs_iget_args *args = opaque; |
David Sterba | 0202e83 | 2020-05-15 19:35:59 +0200 | [diff] [blame] | 5408 | |
| 5409 | return args->ino == BTRFS_I(inode)->location.objectid && |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5410 | args->root == BTRFS_I(inode)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5411 | } |
| 5412 | |
David Sterba | 0202e83 | 2020-05-15 19:35:59 +0200 | [diff] [blame] | 5413 | static struct inode *btrfs_iget_locked(struct super_block *s, u64 ino, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5414 | struct btrfs_root *root) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5415 | { |
| 5416 | struct inode *inode; |
| 5417 | struct btrfs_iget_args args; |
David Sterba | 0202e83 | 2020-05-15 19:35:59 +0200 | [diff] [blame] | 5418 | unsigned long hashval = btrfs_inode_hash(ino, root); |
Filipe David Borba Manana | 778ba82 | 2013-10-06 22:22:33 +0100 | [diff] [blame] | 5419 | |
David Sterba | 0202e83 | 2020-05-15 19:35:59 +0200 | [diff] [blame] | 5420 | args.ino = ino; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5421 | args.root = root; |
| 5422 | |
Filipe David Borba Manana | 778ba82 | 2013-10-06 22:22:33 +0100 | [diff] [blame] | 5423 | inode = iget5_locked(s, hashval, btrfs_find_actor, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5424 | btrfs_init_locked_inode, |
| 5425 | (void *)&args); |
| 5426 | return inode; |
| 5427 | } |
| 5428 | |
David Sterba | 4c66e0d | 2019-10-03 19:09:35 +0200 | [diff] [blame] | 5429 | /* |
David Sterba | 0202e83 | 2020-05-15 19:35:59 +0200 | [diff] [blame] | 5430 | * Get an inode object given its inode number and corresponding root. |
David Sterba | 4c66e0d | 2019-10-03 19:09:35 +0200 | [diff] [blame] | 5431 | * Path can be preallocated to prevent recursing back to iget through |
| 5432 | * allocator. NULL is also valid but may require an additional allocation |
| 5433 | * later. |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 5434 | */ |
David Sterba | 0202e83 | 2020-05-15 19:35:59 +0200 | [diff] [blame] | 5435 | struct inode *btrfs_iget_path(struct super_block *s, u64 ino, |
David Sterba | 4c66e0d | 2019-10-03 19:09:35 +0200 | [diff] [blame] | 5436 | struct btrfs_root *root, struct btrfs_path *path) |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 5437 | { |
| 5438 | struct inode *inode; |
| 5439 | |
David Sterba | 0202e83 | 2020-05-15 19:35:59 +0200 | [diff] [blame] | 5440 | inode = btrfs_iget_locked(s, ino, root); |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 5441 | if (!inode) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5442 | return ERR_PTR(-ENOMEM); |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 5443 | |
| 5444 | if (inode->i_state & I_NEW) { |
Filipe Manana | 6771089 | 2016-06-06 11:51:25 +0100 | [diff] [blame] | 5445 | int ret; |
| 5446 | |
Filipe Manana | 4222ea7 | 2018-10-24 10:13:03 +0100 | [diff] [blame] | 5447 | ret = btrfs_read_locked_inode(inode, path); |
Al Viro | 9bc2cef | 2018-07-29 23:04:50 +0100 | [diff] [blame] | 5448 | if (!ret) { |
Mark Fasheh | 1748f84 | 2011-07-12 11:25:31 -0700 | [diff] [blame] | 5449 | inode_tree_add(inode); |
| 5450 | unlock_new_inode(inode); |
Mark Fasheh | 1748f84 | 2011-07-12 11:25:31 -0700 | [diff] [blame] | 5451 | } else { |
Al Viro | f5b3a41 | 2018-07-29 23:04:51 +0100 | [diff] [blame] | 5452 | iget_failed(inode); |
| 5453 | /* |
| 5454 | * ret > 0 can come from btrfs_search_slot called by |
| 5455 | * btrfs_read_locked_inode, this means the inode item |
| 5456 | * was not found. |
| 5457 | */ |
| 5458 | if (ret > 0) |
| 5459 | ret = -ENOENT; |
| 5460 | inode = ERR_PTR(ret); |
Mark Fasheh | 1748f84 | 2011-07-12 11:25:31 -0700 | [diff] [blame] | 5461 | } |
| 5462 | } |
| 5463 | |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 5464 | return inode; |
| 5465 | } |
| 5466 | |
David Sterba | 0202e83 | 2020-05-15 19:35:59 +0200 | [diff] [blame] | 5467 | 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] | 5468 | { |
David Sterba | 0202e83 | 2020-05-15 19:35:59 +0200 | [diff] [blame] | 5469 | return btrfs_iget_path(s, ino, root, NULL); |
Filipe Manana | 4222ea7 | 2018-10-24 10:13:03 +0100 | [diff] [blame] | 5470 | } |
| 5471 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5472 | static struct inode *new_simple_dir(struct super_block *s, |
| 5473 | struct btrfs_key *key, |
| 5474 | struct btrfs_root *root) |
| 5475 | { |
| 5476 | struct inode *inode = new_inode(s); |
| 5477 | |
| 5478 | if (!inode) |
| 5479 | return ERR_PTR(-ENOMEM); |
| 5480 | |
Josef Bacik | 5c8fd99 | 2020-02-14 16:11:43 -0500 | [diff] [blame] | 5481 | BTRFS_I(inode)->root = btrfs_grab_root(root); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5482 | memcpy(&BTRFS_I(inode)->location, key, sizeof(*key)); |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 5483 | set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5484 | |
| 5485 | inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID; |
Omar Sandoval | 6bb6b51 | 2019-12-05 10:36:04 -0800 | [diff] [blame] | 5486 | /* |
| 5487 | * We only need lookup, the rest is read-only and there's no inode |
| 5488 | * associated with the dentry |
| 5489 | */ |
| 5490 | inode->i_op = &simple_dir_inode_operations; |
Omar Sandoval | 1fdf419 | 2017-01-25 17:06:39 -0800 | [diff] [blame] | 5491 | inode->i_opflags &= ~IOP_XATTR; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5492 | inode->i_fop = &simple_dir_operations; |
| 5493 | inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO; |
Deepa Dinamani | c2050a4 | 2016-09-14 07:48:06 -0700 | [diff] [blame] | 5494 | inode->i_mtime = current_time(inode); |
chandan r | 9cc97d6 | 2012-07-04 12:48:07 +0530 | [diff] [blame] | 5495 | inode->i_atime = inode->i_mtime; |
| 5496 | inode->i_ctime = inode->i_mtime; |
Arnd Bergmann | d3c6be6f | 2018-06-21 18:04:06 +0200 | [diff] [blame] | 5497 | BTRFS_I(inode)->i_otime = inode->i_mtime; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5498 | |
| 5499 | return inode; |
| 5500 | } |
| 5501 | |
Qu Wenruo | 6bf9e4b | 2019-03-13 13:55:11 +0800 | [diff] [blame] | 5502 | static inline u8 btrfs_inode_type(struct inode *inode) |
| 5503 | { |
| 5504 | /* |
| 5505 | * Compile-time asserts that generic FT_* types still match |
| 5506 | * BTRFS_FT_* types |
| 5507 | */ |
| 5508 | BUILD_BUG_ON(BTRFS_FT_UNKNOWN != FT_UNKNOWN); |
| 5509 | BUILD_BUG_ON(BTRFS_FT_REG_FILE != FT_REG_FILE); |
| 5510 | BUILD_BUG_ON(BTRFS_FT_DIR != FT_DIR); |
| 5511 | BUILD_BUG_ON(BTRFS_FT_CHRDEV != FT_CHRDEV); |
| 5512 | BUILD_BUG_ON(BTRFS_FT_BLKDEV != FT_BLKDEV); |
| 5513 | BUILD_BUG_ON(BTRFS_FT_FIFO != FT_FIFO); |
| 5514 | BUILD_BUG_ON(BTRFS_FT_SOCK != FT_SOCK); |
| 5515 | BUILD_BUG_ON(BTRFS_FT_SYMLINK != FT_SYMLINK); |
| 5516 | |
| 5517 | return fs_umode_to_ftype(inode->i_mode); |
| 5518 | } |
| 5519 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5520 | struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5521 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5522 | struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5523 | struct inode *inode; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5524 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5525 | struct btrfs_root *sub_root = root; |
| 5526 | struct btrfs_key location; |
Qu Wenruo | 6bf9e4b | 2019-03-13 13:55:11 +0800 | [diff] [blame] | 5527 | u8 di_type = 0; |
Josef Bacik | b4aff1f | 2011-06-28 16:18:59 -0400 | [diff] [blame] | 5528 | int ret = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5529 | |
| 5530 | if (dentry->d_name.len > BTRFS_NAME_LEN) |
| 5531 | return ERR_PTR(-ENAMETOOLONG); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5532 | |
Qu Wenruo | 6bf9e4b | 2019-03-13 13:55:11 +0800 | [diff] [blame] | 5533 | ret = btrfs_inode_by_name(dir, dentry, &location, &di_type); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5534 | if (ret < 0) |
| 5535 | return ERR_PTR(ret); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5536 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5537 | if (location.type == BTRFS_INODE_ITEM_KEY) { |
David Sterba | 0202e83 | 2020-05-15 19:35:59 +0200 | [diff] [blame] | 5538 | inode = btrfs_iget(dir->i_sb, location.objectid, root); |
Qu Wenruo | 6bf9e4b | 2019-03-13 13:55:11 +0800 | [diff] [blame] | 5539 | if (IS_ERR(inode)) |
| 5540 | return inode; |
| 5541 | |
| 5542 | /* Do extra check against inode mode with di_type */ |
| 5543 | if (btrfs_inode_type(inode) != di_type) { |
| 5544 | btrfs_crit(fs_info, |
| 5545 | "inode mode mismatch with dir: inode mode=0%o btrfs type=%u dir type=%u", |
| 5546 | inode->i_mode, btrfs_inode_type(inode), |
| 5547 | di_type); |
| 5548 | iput(inode); |
| 5549 | return ERR_PTR(-EUCLEAN); |
| 5550 | } |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5551 | return inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5552 | } |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5553 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5554 | ret = fixup_tree_root_location(fs_info, dir, dentry, |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5555 | &location, &sub_root); |
| 5556 | if (ret < 0) { |
| 5557 | if (ret != -ENOENT) |
| 5558 | inode = ERR_PTR(ret); |
| 5559 | else |
| 5560 | inode = new_simple_dir(dir->i_sb, &location, sub_root); |
| 5561 | } else { |
David Sterba | 0202e83 | 2020-05-15 19:35:59 +0200 | [diff] [blame] | 5562 | inode = btrfs_iget(dir->i_sb, location.objectid, sub_root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5563 | } |
Josef Bacik | 8727002 | 2020-01-24 09:32:31 -0500 | [diff] [blame] | 5564 | if (root != sub_root) |
Josef Bacik | 0024652 | 2020-01-24 09:33:01 -0500 | [diff] [blame] | 5565 | btrfs_put_root(sub_root); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5566 | |
Julia Lawall | 34d19ba | 2011-01-24 19:55:19 +0000 | [diff] [blame] | 5567 | if (!IS_ERR(inode) && root != sub_root) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5568 | down_read(&fs_info->cleanup_work_sem); |
David Howells | bc98a42 | 2017-07-17 08:45:34 +0100 | [diff] [blame] | 5569 | if (!sb_rdonly(inode->i_sb)) |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 5570 | ret = btrfs_orphan_cleanup(sub_root); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5571 | up_read(&fs_info->cleanup_work_sem); |
Josef Bacik | 01cd336 | 2013-06-03 21:39:49 -0400 | [diff] [blame] | 5572 | if (ret) { |
| 5573 | iput(inode); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 5574 | inode = ERR_PTR(ret); |
Josef Bacik | 01cd336 | 2013-06-03 21:39:49 -0400 | [diff] [blame] | 5575 | } |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 5576 | } |
| 5577 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5578 | return inode; |
| 5579 | } |
| 5580 | |
Nick Piggin | fe15ce4 | 2011-01-07 17:49:23 +1100 | [diff] [blame] | 5581 | static int btrfs_dentry_delete(const struct dentry *dentry) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5582 | { |
| 5583 | struct btrfs_root *root; |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 5584 | struct inode *inode = d_inode(dentry); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5585 | |
Li Zefan | 848cce0 | 2012-02-21 17:04:28 +0800 | [diff] [blame] | 5586 | if (!inode && !IS_ROOT(dentry)) |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 5587 | inode = d_inode(dentry->d_parent); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5588 | |
Li Zefan | 848cce0 | 2012-02-21 17:04:28 +0800 | [diff] [blame] | 5589 | if (inode) { |
| 5590 | root = BTRFS_I(inode)->root; |
Yan, Zheng | efefb14 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5591 | if (btrfs_root_refs(&root->root_item) == 0) |
| 5592 | return 1; |
Li Zefan | 848cce0 | 2012-02-21 17:04:28 +0800 | [diff] [blame] | 5593 | |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 5594 | if (btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) |
Li Zefan | 848cce0 | 2012-02-21 17:04:28 +0800 | [diff] [blame] | 5595 | return 1; |
Yan, Zheng | efefb14 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5596 | } |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5597 | return 0; |
| 5598 | } |
| 5599 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5600 | static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry, |
Al Viro | 00cd8dd | 2012-06-10 17:13:09 -0400 | [diff] [blame] | 5601 | unsigned int flags) |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5602 | { |
Al Viro | 3837d20 | 2018-10-10 16:38:27 -0400 | [diff] [blame] | 5603 | struct inode *inode = btrfs_lookup_dentry(dir, dentry); |
Josef Bacik | a66e7cc | 2011-09-18 10:34:03 -0400 | [diff] [blame] | 5604 | |
Al Viro | 3837d20 | 2018-10-10 16:38:27 -0400 | [diff] [blame] | 5605 | if (inode == ERR_PTR(-ENOENT)) |
| 5606 | inode = NULL; |
Al Viro | 41d28bc | 2014-10-12 22:24:21 -0400 | [diff] [blame] | 5607 | return d_splice_alias(inode, dentry); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5608 | } |
| 5609 | |
Josef Bacik | 23b5ec7 | 2017-07-24 15:14:25 -0400 | [diff] [blame] | 5610 | /* |
| 5611 | * All this infrastructure exists because dir_emit can fault, and we are holding |
| 5612 | * the tree lock when doing readdir. For now just allocate a buffer and copy |
| 5613 | * our information into that, and then dir_emit from the buffer. This is |
| 5614 | * similar to what NFS does, only we don't keep the buffer around in pagecache |
| 5615 | * because I'm afraid I'll mess that up. Long term we need to make filldir do |
| 5616 | * copy_to_user_inatomic so we don't have to worry about page faulting under the |
| 5617 | * tree lock. |
| 5618 | */ |
| 5619 | static int btrfs_opendir(struct inode *inode, struct file *file) |
| 5620 | { |
| 5621 | struct btrfs_file_private *private; |
| 5622 | |
| 5623 | private = kzalloc(sizeof(struct btrfs_file_private), GFP_KERNEL); |
| 5624 | if (!private) |
| 5625 | return -ENOMEM; |
| 5626 | private->filldir_buf = kzalloc(PAGE_SIZE, GFP_KERNEL); |
| 5627 | if (!private->filldir_buf) { |
| 5628 | kfree(private); |
| 5629 | return -ENOMEM; |
| 5630 | } |
| 5631 | file->private_data = private; |
| 5632 | return 0; |
| 5633 | } |
| 5634 | |
| 5635 | struct dir_entry { |
| 5636 | u64 ino; |
| 5637 | u64 offset; |
| 5638 | unsigned type; |
| 5639 | int name_len; |
| 5640 | }; |
| 5641 | |
| 5642 | static int btrfs_filldir(void *addr, int entries, struct dir_context *ctx) |
| 5643 | { |
| 5644 | while (entries--) { |
| 5645 | struct dir_entry *entry = addr; |
| 5646 | char *name = (char *)(entry + 1); |
| 5647 | |
David Sterba | 92d3217 | 2018-04-16 21:10:14 +0200 | [diff] [blame] | 5648 | ctx->pos = get_unaligned(&entry->offset); |
| 5649 | if (!dir_emit(ctx, name, get_unaligned(&entry->name_len), |
| 5650 | get_unaligned(&entry->ino), |
| 5651 | get_unaligned(&entry->type))) |
Josef Bacik | 23b5ec7 | 2017-07-24 15:14:25 -0400 | [diff] [blame] | 5652 | return 1; |
David Sterba | 92d3217 | 2018-04-16 21:10:14 +0200 | [diff] [blame] | 5653 | addr += sizeof(struct dir_entry) + |
| 5654 | get_unaligned(&entry->name_len); |
Josef Bacik | 23b5ec7 | 2017-07-24 15:14:25 -0400 | [diff] [blame] | 5655 | ctx->pos++; |
| 5656 | } |
| 5657 | return 0; |
| 5658 | } |
| 5659 | |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5660 | static int btrfs_real_readdir(struct file *file, struct dir_context *ctx) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5661 | { |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5662 | struct inode *inode = file_inode(file); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5663 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Josef Bacik | 23b5ec7 | 2017-07-24 15:14:25 -0400 | [diff] [blame] | 5664 | struct btrfs_file_private *private = file->private_data; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5665 | struct btrfs_dir_item *di; |
| 5666 | struct btrfs_key key; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5667 | struct btrfs_key found_key; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5668 | struct btrfs_path *path; |
Josef Bacik | 23b5ec7 | 2017-07-24 15:14:25 -0400 | [diff] [blame] | 5669 | void *addr; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5670 | struct list_head ins_list; |
| 5671 | struct list_head del_list; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5672 | int ret; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5673 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5674 | int slot; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5675 | char *name_ptr; |
| 5676 | int name_len; |
Josef Bacik | 23b5ec7 | 2017-07-24 15:14:25 -0400 | [diff] [blame] | 5677 | int entries = 0; |
| 5678 | int total_len = 0; |
Omar Sandoval | 02dbfc9 | 2016-05-20 13:50:33 -0700 | [diff] [blame] | 5679 | bool put = false; |
Jeff Mahoney | c2951f3 | 2016-11-21 15:59:04 +0100 | [diff] [blame] | 5680 | struct btrfs_key location; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5681 | |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5682 | if (!dir_emit_dots(file, ctx)) |
| 5683 | return 0; |
| 5684 | |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5685 | path = btrfs_alloc_path(); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5686 | if (!path) |
| 5687 | return -ENOMEM; |
Chris Mason | ff5714c | 2011-05-28 07:00:39 -0400 | [diff] [blame] | 5688 | |
Josef Bacik | 23b5ec7 | 2017-07-24 15:14:25 -0400 | [diff] [blame] | 5689 | addr = private->filldir_buf; |
David Sterba | e4058b5 | 2015-11-27 16:31:35 +0100 | [diff] [blame] | 5690 | path->reada = READA_FORWARD; |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5691 | |
Jeff Mahoney | c2951f3 | 2016-11-21 15:59:04 +0100 | [diff] [blame] | 5692 | INIT_LIST_HEAD(&ins_list); |
| 5693 | INIT_LIST_HEAD(&del_list); |
| 5694 | put = btrfs_readdir_get_delayed_items(inode, &ins_list, &del_list); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5695 | |
Josef Bacik | 23b5ec7 | 2017-07-24 15:14:25 -0400 | [diff] [blame] | 5696 | again: |
Jeff Mahoney | c2951f3 | 2016-11-21 15:59:04 +0100 | [diff] [blame] | 5697 | key.type = BTRFS_DIR_INDEX_KEY; |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5698 | key.offset = ctx->pos; |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 5699 | key.objectid = btrfs_ino(BTRFS_I(inode)); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5700 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5701 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 5702 | if (ret < 0) |
| 5703 | goto err; |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5704 | |
| 5705 | while (1) { |
Josef Bacik | 23b5ec7 | 2017-07-24 15:14:25 -0400 | [diff] [blame] | 5706 | struct dir_entry *entry; |
| 5707 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5708 | leaf = path->nodes[0]; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5709 | slot = path->slots[0]; |
Li Zefan | b9e03af | 2011-03-23 10:43:58 +0800 | [diff] [blame] | 5710 | if (slot >= btrfs_header_nritems(leaf)) { |
| 5711 | ret = btrfs_next_leaf(root, path); |
| 5712 | if (ret < 0) |
| 5713 | goto err; |
| 5714 | else if (ret > 0) |
| 5715 | break; |
| 5716 | continue; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5717 | } |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5718 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5719 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
| 5720 | |
| 5721 | if (found_key.objectid != key.objectid) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5722 | break; |
Jeff Mahoney | c2951f3 | 2016-11-21 15:59:04 +0100 | [diff] [blame] | 5723 | if (found_key.type != BTRFS_DIR_INDEX_KEY) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5724 | break; |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5725 | if (found_key.offset < ctx->pos) |
Li Zefan | b9e03af | 2011-03-23 10:43:58 +0800 | [diff] [blame] | 5726 | goto next; |
Jeff Mahoney | c2951f3 | 2016-11-21 15:59:04 +0100 | [diff] [blame] | 5727 | if (btrfs_should_delete_dir_index(&del_list, found_key.offset)) |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5728 | goto next; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5729 | di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item); |
Jeff Mahoney | c2951f3 | 2016-11-21 15:59:04 +0100 | [diff] [blame] | 5730 | name_len = btrfs_dir_name_len(leaf, di); |
Josef Bacik | 23b5ec7 | 2017-07-24 15:14:25 -0400 | [diff] [blame] | 5731 | if ((total_len + sizeof(struct dir_entry) + name_len) >= |
| 5732 | PAGE_SIZE) { |
| 5733 | btrfs_release_path(path); |
| 5734 | ret = btrfs_filldir(private->filldir_buf, entries, ctx); |
| 5735 | if (ret) |
| 5736 | goto nopos; |
| 5737 | addr = private->filldir_buf; |
| 5738 | entries = 0; |
| 5739 | total_len = 0; |
| 5740 | goto again; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5741 | } |
Josef Bacik | 23b5ec7 | 2017-07-24 15:14:25 -0400 | [diff] [blame] | 5742 | |
| 5743 | entry = addr; |
David Sterba | 92d3217 | 2018-04-16 21:10:14 +0200 | [diff] [blame] | 5744 | put_unaligned(name_len, &entry->name_len); |
Josef Bacik | 23b5ec7 | 2017-07-24 15:14:25 -0400 | [diff] [blame] | 5745 | name_ptr = (char *)(entry + 1); |
Jeff Mahoney | c2951f3 | 2016-11-21 15:59:04 +0100 | [diff] [blame] | 5746 | read_extent_buffer(leaf, name_ptr, (unsigned long)(di + 1), |
| 5747 | name_len); |
Phillip Potter | 7d157c3 | 2019-03-26 21:39:34 +0000 | [diff] [blame] | 5748 | put_unaligned(fs_ftype_to_dtype(btrfs_dir_type(leaf, di)), |
David Sterba | 92d3217 | 2018-04-16 21:10:14 +0200 | [diff] [blame] | 5749 | &entry->type); |
Jeff Mahoney | c2951f3 | 2016-11-21 15:59:04 +0100 | [diff] [blame] | 5750 | btrfs_dir_item_key_to_cpu(leaf, di, &location); |
David Sterba | 92d3217 | 2018-04-16 21:10:14 +0200 | [diff] [blame] | 5751 | put_unaligned(location.objectid, &entry->ino); |
| 5752 | put_unaligned(found_key.offset, &entry->offset); |
Josef Bacik | 23b5ec7 | 2017-07-24 15:14:25 -0400 | [diff] [blame] | 5753 | entries++; |
| 5754 | addr += sizeof(struct dir_entry) + name_len; |
| 5755 | total_len += sizeof(struct dir_entry) + name_len; |
Li Zefan | b9e03af | 2011-03-23 10:43:58 +0800 | [diff] [blame] | 5756 | next: |
| 5757 | path->slots[0]++; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5758 | } |
Josef Bacik | 23b5ec7 | 2017-07-24 15:14:25 -0400 | [diff] [blame] | 5759 | btrfs_release_path(path); |
| 5760 | |
| 5761 | ret = btrfs_filldir(private->filldir_buf, entries, ctx); |
| 5762 | if (ret) |
| 5763 | goto nopos; |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5764 | |
Jeff Mahoney | d2fbb2b | 2016-11-05 13:26:35 -0400 | [diff] [blame] | 5765 | ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list); |
Jeff Mahoney | c2951f3 | 2016-11-21 15:59:04 +0100 | [diff] [blame] | 5766 | if (ret) |
David Sterba | bc4ef75 | 2015-11-13 13:44:28 +0100 | [diff] [blame] | 5767 | goto nopos; |
| 5768 | |
Zach Brown | db62efb | 2013-07-11 16:19:42 -0700 | [diff] [blame] | 5769 | /* |
| 5770 | * Stop new entries from being returned after we return the last |
| 5771 | * entry. |
| 5772 | * |
| 5773 | * New directory entries are assigned a strictly increasing |
| 5774 | * offset. This means that new entries created during readdir |
| 5775 | * are *guaranteed* to be seen in the future by that readdir. |
| 5776 | * This has broken buggy programs which operate on names as |
| 5777 | * they're returned by readdir. Until we re-use freed offsets |
| 5778 | * we have this hack to stop new entries from being returned |
| 5779 | * under the assumption that they'll never reach this huge |
| 5780 | * offset. |
| 5781 | * |
| 5782 | * This is being careful not to overflow 32bit loff_t unless the |
| 5783 | * last entry requires it because doing so has broken 32bit apps |
| 5784 | * in the past. |
| 5785 | */ |
Jeff Mahoney | c2951f3 | 2016-11-21 15:59:04 +0100 | [diff] [blame] | 5786 | if (ctx->pos >= INT_MAX) |
| 5787 | ctx->pos = LLONG_MAX; |
| 5788 | else |
| 5789 | ctx->pos = INT_MAX; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5790 | nopos: |
| 5791 | ret = 0; |
| 5792 | err: |
Omar Sandoval | 02dbfc9 | 2016-05-20 13:50:33 -0700 | [diff] [blame] | 5793 | if (put) |
| 5794 | btrfs_readdir_put_delayed_items(inode, &ins_list, &del_list); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5795 | btrfs_free_path(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5796 | return ret; |
| 5797 | } |
| 5798 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5799 | /* |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 5800 | * This is somewhat expensive, updating the tree every time the |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5801 | * inode changes. But, it is most likely to find the inode in cache. |
| 5802 | * FIXME, needs more benchmarking...there are no reasons other than performance |
| 5803 | * to keep or drop this code. |
| 5804 | */ |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 5805 | static int btrfs_dirty_inode(struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5806 | { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5807 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5808 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5809 | struct btrfs_trans_handle *trans; |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5810 | int ret; |
| 5811 | |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 5812 | if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags)) |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 5813 | return 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5814 | |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 5815 | trans = btrfs_join_transaction(root); |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 5816 | if (IS_ERR(trans)) |
| 5817 | return PTR_ERR(trans); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5818 | |
| 5819 | ret = btrfs_update_inode(trans, root, inode); |
Nikolay Borisov | bf6dd43 | 2021-02-22 18:40:44 +0200 | [diff] [blame] | 5820 | if (ret && (ret == -ENOSPC || ret == -EDQUOT)) { |
Chris Mason | 94b6044 | 2010-05-26 11:02:00 -0400 | [diff] [blame] | 5821 | /* whoops, lets try again with the full transaction */ |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 5822 | btrfs_end_transaction(trans); |
Chris Mason | 94b6044 | 2010-05-26 11:02:00 -0400 | [diff] [blame] | 5823 | trans = btrfs_start_transaction(root, 1); |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 5824 | if (IS_ERR(trans)) |
| 5825 | return PTR_ERR(trans); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5826 | |
Chris Mason | 94b6044 | 2010-05-26 11:02:00 -0400 | [diff] [blame] | 5827 | ret = btrfs_update_inode(trans, root, inode); |
Chris Mason | 94b6044 | 2010-05-26 11:02:00 -0400 | [diff] [blame] | 5828 | } |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 5829 | btrfs_end_transaction(trans); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5830 | if (BTRFS_I(inode)->delayed_node) |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5831 | btrfs_balance_delayed_items(fs_info); |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 5832 | |
| 5833 | return ret; |
| 5834 | } |
| 5835 | |
| 5836 | /* |
| 5837 | * This is a copy of file_update_time. We need this so we can return error on |
| 5838 | * ENOSPC for updating the inode in the case of file write and mmap writes. |
| 5839 | */ |
Deepa Dinamani | 95582b0 | 2018-05-08 19:36:02 -0700 | [diff] [blame] | 5840 | static int btrfs_update_time(struct inode *inode, struct timespec64 *now, |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 5841 | int flags) |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 5842 | { |
Alexander Block | 2bc556528 | 2012-06-15 09:49:33 +0200 | [diff] [blame] | 5843 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Jeff Layton | 3a8c723 | 2017-12-11 06:35:24 -0500 | [diff] [blame] | 5844 | bool dirty = flags & ~S_VERSION; |
Alexander Block | 2bc556528 | 2012-06-15 09:49:33 +0200 | [diff] [blame] | 5845 | |
| 5846 | if (btrfs_root_readonly(root)) |
| 5847 | return -EROFS; |
| 5848 | |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 5849 | if (flags & S_VERSION) |
Jeff Layton | 3a8c723 | 2017-12-11 06:35:24 -0500 | [diff] [blame] | 5850 | dirty |= inode_maybe_inc_iversion(inode, dirty); |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 5851 | if (flags & S_CTIME) |
| 5852 | inode->i_ctime = *now; |
| 5853 | if (flags & S_MTIME) |
| 5854 | inode->i_mtime = *now; |
| 5855 | if (flags & S_ATIME) |
| 5856 | inode->i_atime = *now; |
Jeff Layton | 3a8c723 | 2017-12-11 06:35:24 -0500 | [diff] [blame] | 5857 | return dirty ? btrfs_dirty_inode(inode) : 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5858 | } |
| 5859 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 5860 | /* |
| 5861 | * find the highest existing sequence number in a directory |
| 5862 | * and then set the in-memory index_cnt variable to reflect |
| 5863 | * free sequence numbers |
| 5864 | */ |
Nikolay Borisov | 4c570655 | 2017-02-20 13:50:32 +0200 | [diff] [blame] | 5865 | static int btrfs_set_inode_index_count(struct btrfs_inode *inode) |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5866 | { |
Nikolay Borisov | 4c570655 | 2017-02-20 13:50:32 +0200 | [diff] [blame] | 5867 | struct btrfs_root *root = inode->root; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5868 | struct btrfs_key key, found_key; |
| 5869 | struct btrfs_path *path; |
| 5870 | struct extent_buffer *leaf; |
| 5871 | int ret; |
| 5872 | |
Nikolay Borisov | 4c570655 | 2017-02-20 13:50:32 +0200 | [diff] [blame] | 5873 | key.objectid = btrfs_ino(inode); |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 5874 | key.type = BTRFS_DIR_INDEX_KEY; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5875 | key.offset = (u64)-1; |
| 5876 | |
| 5877 | path = btrfs_alloc_path(); |
| 5878 | if (!path) |
| 5879 | return -ENOMEM; |
| 5880 | |
| 5881 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 5882 | if (ret < 0) |
| 5883 | goto out; |
| 5884 | /* FIXME: we should be able to handle this */ |
| 5885 | if (ret == 0) |
| 5886 | goto out; |
| 5887 | ret = 0; |
| 5888 | |
| 5889 | /* |
| 5890 | * MAGIC NUMBER EXPLANATION: |
| 5891 | * since we search a directory based on f_pos we have to start at 2 |
| 5892 | * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody |
| 5893 | * else has to start at 2 |
| 5894 | */ |
| 5895 | if (path->slots[0] == 0) { |
Nikolay Borisov | 4c570655 | 2017-02-20 13:50:32 +0200 | [diff] [blame] | 5896 | inode->index_cnt = 2; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5897 | goto out; |
| 5898 | } |
| 5899 | |
| 5900 | path->slots[0]--; |
| 5901 | |
| 5902 | leaf = path->nodes[0]; |
| 5903 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 5904 | |
Nikolay Borisov | 4c570655 | 2017-02-20 13:50:32 +0200 | [diff] [blame] | 5905 | if (found_key.objectid != btrfs_ino(inode) || |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 5906 | found_key.type != BTRFS_DIR_INDEX_KEY) { |
Nikolay Borisov | 4c570655 | 2017-02-20 13:50:32 +0200 | [diff] [blame] | 5907 | inode->index_cnt = 2; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5908 | goto out; |
| 5909 | } |
| 5910 | |
Nikolay Borisov | 4c570655 | 2017-02-20 13:50:32 +0200 | [diff] [blame] | 5911 | inode->index_cnt = found_key.offset + 1; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5912 | out: |
| 5913 | btrfs_free_path(path); |
| 5914 | return ret; |
| 5915 | } |
| 5916 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 5917 | /* |
| 5918 | * helper to find a free sequence number in a given directory. This current |
| 5919 | * code is very simple, later versions will do smarter things in the btree |
| 5920 | */ |
Nikolay Borisov | 877574e | 2017-02-20 13:50:33 +0200 | [diff] [blame] | 5921 | int btrfs_set_inode_index(struct btrfs_inode *dir, u64 *index) |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5922 | { |
| 5923 | int ret = 0; |
| 5924 | |
Nikolay Borisov | 877574e | 2017-02-20 13:50:33 +0200 | [diff] [blame] | 5925 | if (dir->index_cnt == (u64)-1) { |
| 5926 | ret = btrfs_inode_delayed_dir_index_count(dir); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5927 | if (ret) { |
| 5928 | ret = btrfs_set_inode_index_count(dir); |
| 5929 | if (ret) |
| 5930 | return ret; |
| 5931 | } |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5932 | } |
| 5933 | |
Nikolay Borisov | 877574e | 2017-02-20 13:50:33 +0200 | [diff] [blame] | 5934 | *index = dir->index_cnt; |
| 5935 | dir->index_cnt++; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5936 | |
| 5937 | return ret; |
| 5938 | } |
| 5939 | |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 5940 | static int btrfs_insert_inode_locked(struct inode *inode) |
| 5941 | { |
| 5942 | struct btrfs_iget_args args; |
David Sterba | 0202e83 | 2020-05-15 19:35:59 +0200 | [diff] [blame] | 5943 | |
| 5944 | args.ino = BTRFS_I(inode)->location.objectid; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 5945 | args.root = BTRFS_I(inode)->root; |
| 5946 | |
| 5947 | return insert_inode_locked4(inode, |
| 5948 | btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root), |
| 5949 | btrfs_find_actor, &args); |
| 5950 | } |
| 5951 | |
Anand Jain | 19aee8d | 2017-07-18 17:37:05 +0800 | [diff] [blame] | 5952 | /* |
| 5953 | * Inherit flags from the parent inode. |
| 5954 | * |
| 5955 | * Currently only the compression flags and the cow flags are inherited. |
| 5956 | */ |
| 5957 | static void btrfs_inherit_iflags(struct inode *inode, struct inode *dir) |
| 5958 | { |
| 5959 | unsigned int flags; |
| 5960 | |
| 5961 | if (!dir) |
| 5962 | return; |
| 5963 | |
| 5964 | flags = BTRFS_I(dir)->flags; |
| 5965 | |
| 5966 | if (flags & BTRFS_INODE_NOCOMPRESS) { |
| 5967 | BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS; |
| 5968 | BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS; |
| 5969 | } else if (flags & BTRFS_INODE_COMPRESS) { |
| 5970 | BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS; |
| 5971 | BTRFS_I(inode)->flags |= BTRFS_INODE_COMPRESS; |
| 5972 | } |
| 5973 | |
| 5974 | if (flags & BTRFS_INODE_NODATACOW) { |
| 5975 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW; |
| 5976 | if (S_ISREG(inode->i_mode)) |
| 5977 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM; |
| 5978 | } |
| 5979 | |
David Sterba | 7b6a221 | 2018-03-26 18:40:21 +0200 | [diff] [blame] | 5980 | btrfs_sync_inode_flags_to_i_flags(inode); |
Anand Jain | 19aee8d | 2017-07-18 17:37:05 +0800 | [diff] [blame] | 5981 | } |
| 5982 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5983 | static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans, |
| 5984 | struct btrfs_root *root, |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5985 | struct inode *dir, |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 5986 | const char *name, int name_len, |
Al Viro | 175a4eb | 2011-07-26 03:30:54 -0400 | [diff] [blame] | 5987 | u64 ref_objectid, u64 objectid, |
| 5988 | umode_t mode, u64 *index) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5989 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5990 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5991 | struct inode *inode; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5992 | struct btrfs_inode_item *inode_item; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5993 | struct btrfs_key *location; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5994 | struct btrfs_path *path; |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 5995 | struct btrfs_inode_ref *ref; |
| 5996 | struct btrfs_key key[2]; |
| 5997 | u32 sizes[2]; |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 5998 | int nitems = name ? 2 : 1; |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 5999 | unsigned long ptr; |
Josef Bacik | 11a19a9 | 2019-09-09 10:12:04 -0400 | [diff] [blame] | 6000 | unsigned int nofs_flag; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6001 | int ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6002 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6003 | path = btrfs_alloc_path(); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 6004 | if (!path) |
| 6005 | return ERR_PTR(-ENOMEM); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6006 | |
Josef Bacik | 11a19a9 | 2019-09-09 10:12:04 -0400 | [diff] [blame] | 6007 | nofs_flag = memalloc_nofs_save(); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6008 | inode = new_inode(fs_info->sb); |
Josef Bacik | 11a19a9 | 2019-09-09 10:12:04 -0400 | [diff] [blame] | 6009 | memalloc_nofs_restore(nofs_flag); |
Yoshinori Sano | 8fb2764 | 2011-04-09 02:30:07 +0000 | [diff] [blame] | 6010 | if (!inode) { |
| 6011 | btrfs_free_path(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6012 | return ERR_PTR(-ENOMEM); |
Yoshinori Sano | 8fb2764 | 2011-04-09 02:30:07 +0000 | [diff] [blame] | 6013 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6014 | |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 6015 | /* |
Filipe Manana | 5762b5c | 2014-08-01 00:10:32 +0100 | [diff] [blame] | 6016 | * O_TMPFILE, set link count to 0, so that after this point, |
| 6017 | * we fill in an inode item with the correct link count. |
| 6018 | */ |
| 6019 | if (!name) |
| 6020 | set_nlink(inode, 0); |
| 6021 | |
| 6022 | /* |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 6023 | * we have to initialize this early, so we can reclaim the inode |
| 6024 | * number if we fail afterwards in this function. |
| 6025 | */ |
| 6026 | inode->i_ino = objectid; |
| 6027 | |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 6028 | if (dir && name) { |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 6029 | trace_btrfs_inode_request(dir); |
| 6030 | |
Nikolay Borisov | 877574e | 2017-02-20 13:50:33 +0200 | [diff] [blame] | 6031 | ret = btrfs_set_inode_index(BTRFS_I(dir), index); |
Shen Feng | 0977143 | 2009-04-02 16:46:06 -0400 | [diff] [blame] | 6032 | if (ret) { |
Yoshinori Sano | 8fb2764 | 2011-04-09 02:30:07 +0000 | [diff] [blame] | 6033 | btrfs_free_path(path); |
Shen Feng | 0977143 | 2009-04-02 16:46:06 -0400 | [diff] [blame] | 6034 | iput(inode); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6035 | return ERR_PTR(ret); |
Shen Feng | 0977143 | 2009-04-02 16:46:06 -0400 | [diff] [blame] | 6036 | } |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 6037 | } else if (dir) { |
| 6038 | *index = 0; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6039 | } |
| 6040 | /* |
| 6041 | * index_cnt is ignored for everything but a dir, |
Su Yue | df6703e | 2018-01-12 11:08:02 +0800 | [diff] [blame] | 6042 | * btrfs_set_inode_index_count has an explanation for the magic |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6043 | * number |
| 6044 | */ |
| 6045 | BTRFS_I(inode)->index_cnt = 2; |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 6046 | BTRFS_I(inode)->dir_index = *index; |
Josef Bacik | 5c8fd99 | 2020-02-14 16:11:43 -0500 | [diff] [blame] | 6047 | BTRFS_I(inode)->root = btrfs_grab_root(root); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 6048 | BTRFS_I(inode)->generation = trans->transid; |
Josef Bacik | 7619585 | 2010-11-19 02:18:02 +0000 | [diff] [blame] | 6049 | inode->i_generation = BTRFS_I(inode)->generation; |
Chris Mason | b888db2b | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6050 | |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 6051 | /* |
| 6052 | * We could have gotten an inode number from somebody who was fsynced |
| 6053 | * and then removed in this same transaction, so let's just set full |
| 6054 | * sync since it will be a full sync anyway and this will blow away the |
| 6055 | * old info in the log. |
| 6056 | */ |
| 6057 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags); |
| 6058 | |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 6059 | key[0].objectid = objectid; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 6060 | key[0].type = BTRFS_INODE_ITEM_KEY; |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 6061 | key[0].offset = 0; |
| 6062 | |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 6063 | sizes[0] = sizeof(struct btrfs_inode_item); |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 6064 | |
| 6065 | if (name) { |
| 6066 | /* |
| 6067 | * Start new inodes with an inode_ref. This is slightly more |
| 6068 | * efficient for small numbers of hard links since they will |
| 6069 | * be packed into one item. Extended refs will kick in if we |
| 6070 | * add more hard links than can fit in the ref item. |
| 6071 | */ |
| 6072 | key[1].objectid = objectid; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 6073 | key[1].type = BTRFS_INODE_REF_KEY; |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 6074 | key[1].offset = ref_objectid; |
| 6075 | |
| 6076 | sizes[1] = name_len + sizeof(*ref); |
| 6077 | } |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 6078 | |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6079 | location = &BTRFS_I(inode)->location; |
| 6080 | location->objectid = objectid; |
| 6081 | location->offset = 0; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 6082 | location->type = BTRFS_INODE_ITEM_KEY; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6083 | |
| 6084 | ret = btrfs_insert_inode_locked(inode); |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6085 | if (ret < 0) { |
| 6086 | iput(inode); |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6087 | goto fail; |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6088 | } |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6089 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 6090 | path->leave_spinning = 1; |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 6091 | ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems); |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 6092 | if (ret != 0) |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6093 | goto fail_unlock; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6094 | |
Dmitry Monakhov | ecc11fab | 2010-03-04 17:31:47 +0300 | [diff] [blame] | 6095 | inode_init_owner(inode, dir, mode); |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 6096 | inode_set_bytes(inode, 0); |
chandan r | 9cc97d6 | 2012-07-04 12:48:07 +0530 | [diff] [blame] | 6097 | |
Deepa Dinamani | c2050a4 | 2016-09-14 07:48:06 -0700 | [diff] [blame] | 6098 | inode->i_mtime = current_time(inode); |
chandan r | 9cc97d6 | 2012-07-04 12:48:07 +0530 | [diff] [blame] | 6099 | inode->i_atime = inode->i_mtime; |
| 6100 | inode->i_ctime = inode->i_mtime; |
Arnd Bergmann | d3c6be6f | 2018-06-21 18:04:06 +0200 | [diff] [blame] | 6101 | BTRFS_I(inode)->i_otime = inode->i_mtime; |
chandan r | 9cc97d6 | 2012-07-04 12:48:07 +0530 | [diff] [blame] | 6102 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6103 | inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 6104 | struct btrfs_inode_item); |
David Sterba | b159fa2 | 2016-11-08 18:09:03 +0100 | [diff] [blame] | 6105 | memzero_extent_buffer(path->nodes[0], (unsigned long)inode_item, |
Li Zefan | 293f7e0 | 2012-07-10 00:58:58 -0600 | [diff] [blame] | 6106 | sizeof(*inode_item)); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 6107 | fill_inode_item(trans, path->nodes[0], inode_item, inode); |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 6108 | |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 6109 | if (name) { |
| 6110 | ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1, |
| 6111 | struct btrfs_inode_ref); |
| 6112 | btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len); |
| 6113 | btrfs_set_inode_ref_index(path->nodes[0], ref, *index); |
| 6114 | ptr = (unsigned long)(ref + 1); |
| 6115 | write_extent_buffer(path->nodes[0], name, ptr, name_len); |
| 6116 | } |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 6117 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6118 | btrfs_mark_buffer_dirty(path->nodes[0]); |
| 6119 | btrfs_free_path(path); |
| 6120 | |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 6121 | btrfs_inherit_iflags(inode, dir); |
| 6122 | |
Al Viro | 569254b | 2011-07-24 17:08:40 -0400 | [diff] [blame] | 6123 | if (S_ISREG(mode)) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6124 | if (btrfs_test_opt(fs_info, NODATASUM)) |
Chris Mason | 9427216 | 2009-07-02 12:26:06 -0400 | [diff] [blame] | 6125 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6126 | if (btrfs_test_opt(fs_info, NODATACOW)) |
Josef Bacik | f2bdf9a | 2013-02-21 15:28:28 -0500 | [diff] [blame] | 6127 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW | |
| 6128 | BTRFS_INODE_NODATASUM; |
Chris Mason | 9427216 | 2009-07-02 12:26:06 -0400 | [diff] [blame] | 6129 | } |
| 6130 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6131 | inode_tree_add(inode); |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 6132 | |
| 6133 | trace_btrfs_inode_new(inode); |
Nikolay Borisov | d909441 | 2020-06-05 10:41:13 +0300 | [diff] [blame] | 6134 | btrfs_set_inode_last_trans(trans, BTRFS_I(inode)); |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 6135 | |
Alexander Block | 8ea05e3 | 2012-07-25 17:35:53 +0200 | [diff] [blame] | 6136 | btrfs_update_root_times(trans, root); |
| 6137 | |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 6138 | ret = btrfs_inode_inherit_props(trans, inode, dir); |
| 6139 | if (ret) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6140 | btrfs_err(fs_info, |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 6141 | "error inheriting props for ino %llu (root %llu): %d", |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 6142 | btrfs_ino(BTRFS_I(inode)), root->root_key.objectid, ret); |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 6143 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6144 | return inode; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6145 | |
| 6146 | fail_unlock: |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6147 | discard_new_inode(inode); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6148 | fail: |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 6149 | if (dir && name) |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6150 | BTRFS_I(dir)->index_cnt--; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6151 | btrfs_free_path(path); |
| 6152 | return ERR_PTR(ret); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6153 | } |
| 6154 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 6155 | /* |
| 6156 | * utility function to add 'inode' into 'parent_inode' with |
| 6157 | * a give name and a given sequence number. |
| 6158 | * if 'add_backref' is true, also insert a backref from the |
| 6159 | * inode to the parent directory. |
| 6160 | */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 6161 | int btrfs_add_link(struct btrfs_trans_handle *trans, |
Nikolay Borisov | db0a669 | 2017-02-20 13:51:08 +0200 | [diff] [blame] | 6162 | struct btrfs_inode *parent_inode, struct btrfs_inode *inode, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 6163 | const char *name, int name_len, int add_backref, u64 index) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6164 | { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6165 | int ret = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6166 | struct btrfs_key key; |
Nikolay Borisov | db0a669 | 2017-02-20 13:51:08 +0200 | [diff] [blame] | 6167 | struct btrfs_root *root = parent_inode->root; |
| 6168 | u64 ino = btrfs_ino(inode); |
| 6169 | u64 parent_ino = btrfs_ino(parent_inode); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6170 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6171 | if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) { |
Nikolay Borisov | db0a669 | 2017-02-20 13:51:08 +0200 | [diff] [blame] | 6172 | memcpy(&key, &inode->root->root_key, sizeof(key)); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6173 | } else { |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6174 | key.objectid = ino; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 6175 | key.type = BTRFS_INODE_ITEM_KEY; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6176 | key.offset = 0; |
| 6177 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6178 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6179 | if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) { |
Lu Fengqi | 6025c19 | 2018-08-01 11:32:29 +0800 | [diff] [blame] | 6180 | ret = btrfs_add_root_ref(trans, key.objectid, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6181 | root->root_key.objectid, parent_ino, |
| 6182 | index, name, name_len); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6183 | } else if (add_backref) { |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6184 | ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino, |
| 6185 | parent_ino, index); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6186 | } |
| 6187 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6188 | /* Nothing to clean up yet */ |
| 6189 | if (ret) |
| 6190 | return ret; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6191 | |
Lu Fengqi | 684572d | 2018-08-04 21:10:57 +0800 | [diff] [blame] | 6192 | ret = btrfs_insert_dir_item(trans, name, name_len, parent_inode, &key, |
Nikolay Borisov | db0a669 | 2017-02-20 13:51:08 +0200 | [diff] [blame] | 6193 | btrfs_inode_type(&inode->vfs_inode), index); |
Chris Mason | 9c52057 | 2012-12-17 14:26:57 -0500 | [diff] [blame] | 6194 | if (ret == -EEXIST || ret == -EOVERFLOW) |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6195 | goto fail_dir_item; |
| 6196 | else if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 6197 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6198 | return ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6199 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6200 | |
Nikolay Borisov | db0a669 | 2017-02-20 13:51:08 +0200 | [diff] [blame] | 6201 | btrfs_i_size_write(parent_inode, parent_inode->vfs_inode.i_size + |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6202 | name_len * 2); |
Nikolay Borisov | db0a669 | 2017-02-20 13:51:08 +0200 | [diff] [blame] | 6203 | inode_inc_iversion(&parent_inode->vfs_inode); |
Filipe Manana | 5338e43 | 2019-05-15 16:02:47 +0100 | [diff] [blame] | 6204 | /* |
| 6205 | * If we are replaying a log tree, we do not want to update the mtime |
| 6206 | * and ctime of the parent directory with the current time, since the |
| 6207 | * log replay procedure is responsible for setting them to their correct |
| 6208 | * values (the ones it had when the fsync was done). |
| 6209 | */ |
| 6210 | if (!test_bit(BTRFS_FS_LOG_RECOVERING, &root->fs_info->flags)) { |
| 6211 | struct timespec64 now = current_time(&parent_inode->vfs_inode); |
| 6212 | |
| 6213 | parent_inode->vfs_inode.i_mtime = now; |
| 6214 | parent_inode->vfs_inode.i_ctime = now; |
| 6215 | } |
Nikolay Borisov | db0a669 | 2017-02-20 13:51:08 +0200 | [diff] [blame] | 6216 | ret = btrfs_update_inode(trans, root, &parent_inode->vfs_inode); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6217 | if (ret) |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 6218 | btrfs_abort_transaction(trans, ret); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6219 | return ret; |
Chris Mason | fe66a05 | 2012-02-20 08:40:56 -0500 | [diff] [blame] | 6220 | |
| 6221 | fail_dir_item: |
| 6222 | if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) { |
| 6223 | u64 local_index; |
| 6224 | int err; |
Lu Fengqi | 3ee1c55 | 2018-08-01 11:32:28 +0800 | [diff] [blame] | 6225 | err = btrfs_del_root_ref(trans, key.objectid, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6226 | root->root_key.objectid, parent_ino, |
| 6227 | &local_index, name, name_len); |
Johannes Thumshirn | 1690dd41 | 2018-12-12 15:14:17 +0100 | [diff] [blame] | 6228 | if (err) |
| 6229 | btrfs_abort_transaction(trans, err); |
Chris Mason | fe66a05 | 2012-02-20 08:40:56 -0500 | [diff] [blame] | 6230 | } else if (add_backref) { |
| 6231 | u64 local_index; |
| 6232 | int err; |
| 6233 | |
| 6234 | err = btrfs_del_inode_ref(trans, root, name, name_len, |
| 6235 | ino, parent_ino, &local_index); |
Johannes Thumshirn | 1690dd41 | 2018-12-12 15:14:17 +0100 | [diff] [blame] | 6236 | if (err) |
| 6237 | btrfs_abort_transaction(trans, err); |
Chris Mason | fe66a05 | 2012-02-20 08:40:56 -0500 | [diff] [blame] | 6238 | } |
Johannes Thumshirn | 1690dd41 | 2018-12-12 15:14:17 +0100 | [diff] [blame] | 6239 | |
| 6240 | /* Return the original error code */ |
Chris Mason | fe66a05 | 2012-02-20 08:40:56 -0500 | [diff] [blame] | 6241 | return ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6242 | } |
| 6243 | |
| 6244 | static int btrfs_add_nondir(struct btrfs_trans_handle *trans, |
Nikolay Borisov | cef415a | 2017-02-20 13:51:09 +0200 | [diff] [blame] | 6245 | struct btrfs_inode *dir, struct dentry *dentry, |
| 6246 | struct btrfs_inode *inode, int backref, u64 index) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6247 | { |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 6248 | int err = btrfs_add_link(trans, dir, inode, |
| 6249 | dentry->d_name.name, dentry->d_name.len, |
| 6250 | backref, index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6251 | if (err > 0) |
| 6252 | err = -EEXIST; |
| 6253 | return err; |
| 6254 | } |
| 6255 | |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6256 | static int btrfs_mknod(struct inode *dir, struct dentry *dentry, |
Al Viro | 1a67aaf | 2011-07-26 01:52:52 -0400 | [diff] [blame] | 6257 | umode_t mode, dev_t rdev) |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6258 | { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6259 | struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6260 | struct btrfs_trans_handle *trans; |
| 6261 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 6262 | struct inode *inode = NULL; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6263 | int err; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6264 | u64 objectid; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 6265 | u64 index = 0; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6266 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 6267 | /* |
| 6268 | * 2 for inode item and ref |
| 6269 | * 2 for dir items |
| 6270 | * 1 for xattr if selinux is on |
| 6271 | */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6272 | trans = btrfs_start_transaction(root, 5); |
| 6273 | if (IS_ERR(trans)) |
| 6274 | return PTR_ERR(trans); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 6275 | |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 6276 | err = btrfs_find_free_ino(root, &objectid); |
| 6277 | if (err) |
| 6278 | goto out_unlock; |
| 6279 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6280 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 6281 | dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid, |
| 6282 | mode, &index); |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 6283 | if (IS_ERR(inode)) { |
| 6284 | err = PTR_ERR(inode); |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6285 | inode = NULL; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6286 | goto out_unlock; |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 6287 | } |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6288 | |
Casey Schaufler | ad19db7 | 2011-12-15 10:09:07 -0500 | [diff] [blame] | 6289 | /* |
| 6290 | * If the active LSM wants to access the inode during |
| 6291 | * d_instantiate it needs these. Smack checks to see |
| 6292 | * if the filesystem supports xattrs by looking at the |
| 6293 | * ops vector. |
| 6294 | */ |
Casey Schaufler | ad19db7 | 2011-12-15 10:09:07 -0500 | [diff] [blame] | 6295 | inode->i_op = &btrfs_special_inode_operations; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6296 | init_special_inode(inode, inode->i_mode, rdev); |
| 6297 | |
| 6298 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6299 | if (err) |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6300 | goto out_unlock; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6301 | |
Nikolay Borisov | cef415a | 2017-02-20 13:51:09 +0200 | [diff] [blame] | 6302 | err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode), |
| 6303 | 0, index); |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6304 | if (err) |
| 6305 | goto out_unlock; |
| 6306 | |
| 6307 | btrfs_update_inode(trans, root, inode); |
| 6308 | d_instantiate_new(dentry, inode); |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6309 | |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6310 | out_unlock: |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 6311 | btrfs_end_transaction(trans); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6312 | btrfs_btree_balance_dirty(fs_info); |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6313 | if (err && inode) { |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6314 | inode_dec_link_count(inode); |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6315 | discard_new_inode(inode); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6316 | } |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6317 | return err; |
| 6318 | } |
| 6319 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6320 | static int btrfs_create(struct inode *dir, struct dentry *dentry, |
Al Viro | ebfc3b4 | 2012-06-10 18:05:36 -0400 | [diff] [blame] | 6321 | umode_t mode, bool excl) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6322 | { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6323 | struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6324 | struct btrfs_trans_handle *trans; |
| 6325 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 6326 | struct inode *inode = NULL; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6327 | int err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6328 | u64 objectid; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 6329 | u64 index = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6330 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 6331 | /* |
| 6332 | * 2 for inode item and ref |
| 6333 | * 2 for dir items |
| 6334 | * 1 for xattr if selinux is on |
| 6335 | */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6336 | trans = btrfs_start_transaction(root, 5); |
| 6337 | if (IS_ERR(trans)) |
| 6338 | return PTR_ERR(trans); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 6339 | |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 6340 | err = btrfs_find_free_ino(root, &objectid); |
| 6341 | if (err) |
| 6342 | goto out_unlock; |
| 6343 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6344 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 6345 | dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid, |
| 6346 | mode, &index); |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 6347 | if (IS_ERR(inode)) { |
| 6348 | err = PTR_ERR(inode); |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6349 | inode = NULL; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6350 | goto out_unlock; |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 6351 | } |
Casey Schaufler | ad19db7 | 2011-12-15 10:09:07 -0500 | [diff] [blame] | 6352 | /* |
| 6353 | * If the active LSM wants to access the inode during |
| 6354 | * d_instantiate it needs these. Smack checks to see |
| 6355 | * if the filesystem supports xattrs by looking at the |
| 6356 | * ops vector. |
| 6357 | */ |
| 6358 | inode->i_fop = &btrfs_file_operations; |
| 6359 | inode->i_op = &btrfs_file_inode_operations; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6360 | inode->i_mapping->a_ops = &btrfs_aops; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6361 | |
| 6362 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); |
| 6363 | if (err) |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6364 | goto out_unlock; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6365 | |
| 6366 | err = btrfs_update_inode(trans, root, inode); |
| 6367 | if (err) |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6368 | goto out_unlock; |
Casey Schaufler | ad19db7 | 2011-12-15 10:09:07 -0500 | [diff] [blame] | 6369 | |
Nikolay Borisov | cef415a | 2017-02-20 13:51:09 +0200 | [diff] [blame] | 6370 | err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode), |
| 6371 | 0, index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6372 | if (err) |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6373 | goto out_unlock; |
Filipe Brandenburger | 43baa57 | 2012-11-30 03:40:09 +0000 | [diff] [blame] | 6374 | |
Al Viro | 1e2e547 | 2018-05-04 08:23:01 -0400 | [diff] [blame] | 6375 | d_instantiate_new(dentry, inode); |
Filipe Brandenburger | 43baa57 | 2012-11-30 03:40:09 +0000 | [diff] [blame] | 6376 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6377 | out_unlock: |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 6378 | btrfs_end_transaction(trans); |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6379 | if (err && inode) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6380 | inode_dec_link_count(inode); |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6381 | discard_new_inode(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6382 | } |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6383 | btrfs_btree_balance_dirty(fs_info); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6384 | return err; |
| 6385 | } |
| 6386 | |
| 6387 | static int btrfs_link(struct dentry *old_dentry, struct inode *dir, |
| 6388 | struct dentry *dentry) |
| 6389 | { |
Filipe Manana | 271dba45 | 2016-01-05 16:24:05 +0000 | [diff] [blame] | 6390 | struct btrfs_trans_handle *trans = NULL; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6391 | struct btrfs_root *root = BTRFS_I(dir)->root; |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 6392 | struct inode *inode = d_inode(old_dentry); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6393 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 6394 | u64 index; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6395 | int err; |
| 6396 | int drop_inode = 0; |
| 6397 | |
TARUISI Hiroaki | 4a8be42 | 2009-11-12 07:14:26 +0000 | [diff] [blame] | 6398 | /* 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] | 6399 | if (root->root_key.objectid != BTRFS_I(inode)->root->root_key.objectid) |
Mark Fasheh | 3ab3564 | 2011-03-22 17:20:26 +0000 | [diff] [blame] | 6400 | return -EXDEV; |
TARUISI Hiroaki | 4a8be42 | 2009-11-12 07:14:26 +0000 | [diff] [blame] | 6401 | |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 6402 | if (inode->i_nlink >= BTRFS_LINK_MAX) |
Al Viro | c055e99 | 2011-03-04 17:15:18 +0000 | [diff] [blame] | 6403 | return -EMLINK; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 6404 | |
Nikolay Borisov | 877574e | 2017-02-20 13:50:33 +0200 | [diff] [blame] | 6405 | err = btrfs_set_inode_index(BTRFS_I(dir), &index); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6406 | if (err) |
| 6407 | goto fail; |
| 6408 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6409 | /* |
Miao Xie | 7e6b646 | 2011-02-18 09:21:17 +0000 | [diff] [blame] | 6410 | * 2 items for inode and inode ref |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6411 | * 2 items for dir items |
Miao Xie | 7e6b646 | 2011-02-18 09:21:17 +0000 | [diff] [blame] | 6412 | * 1 item for parent inode |
Omar Sandoval | 399b0bb | 2018-05-11 13:13:40 -0700 | [diff] [blame] | 6413 | * 1 item for orphan item deletion if O_TMPFILE |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6414 | */ |
Omar Sandoval | 399b0bb | 2018-05-11 13:13:40 -0700 | [diff] [blame] | 6415 | trans = btrfs_start_transaction(root, inode->i_nlink ? 5 : 6); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6416 | if (IS_ERR(trans)) { |
| 6417 | err = PTR_ERR(trans); |
Filipe Manana | 271dba45 | 2016-01-05 16:24:05 +0000 | [diff] [blame] | 6418 | trans = NULL; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6419 | goto fail; |
| 6420 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6421 | |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 6422 | /* There are several dir indexes for this inode, clear the cache. */ |
| 6423 | BTRFS_I(inode)->dir_index = 0ULL; |
Zach Brown | 8b558c5 | 2013-10-16 12:10:34 -0700 | [diff] [blame] | 6424 | inc_nlink(inode); |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 6425 | inode_inc_iversion(inode); |
Deepa Dinamani | c2050a4 | 2016-09-14 07:48:06 -0700 | [diff] [blame] | 6426 | inode->i_ctime = current_time(inode); |
Al Viro | 7de9c6ee | 2010-10-23 11:11:40 -0400 | [diff] [blame] | 6427 | ihold(inode); |
Josef Bacik | e997615 | 2012-10-11 15:53:56 -0400 | [diff] [blame] | 6428 | set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6429 | |
Nikolay Borisov | cef415a | 2017-02-20 13:51:09 +0200 | [diff] [blame] | 6430 | err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode), |
| 6431 | 1, index); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6432 | |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 6433 | if (err) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6434 | drop_inode = 1; |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 6435 | } else { |
Al Viro | 10d9f30 | 2011-07-16 23:09:10 -0400 | [diff] [blame] | 6436 | struct dentry *parent = dentry->d_parent; |
Filipe Manana | d4682ba | 2018-06-11 19:24:28 +0100 | [diff] [blame] | 6437 | |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 6438 | err = btrfs_update_inode(trans, root, inode); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6439 | if (err) |
| 6440 | goto fail; |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 6441 | if (inode->i_nlink == 1) { |
| 6442 | /* |
| 6443 | * If new hard link count is 1, it's a file created |
| 6444 | * with open(2) O_TMPFILE flag. |
| 6445 | */ |
Nikolay Borisov | 3d6ae7b | 2017-02-20 13:50:58 +0200 | [diff] [blame] | 6446 | err = btrfs_orphan_del(trans, BTRFS_I(inode)); |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 6447 | if (err) |
| 6448 | goto fail; |
| 6449 | } |
Al Viro | 08c422c | 2011-12-23 07:58:13 -0500 | [diff] [blame] | 6450 | d_instantiate(dentry, inode); |
Filipe Manana | 75b463d | 2020-08-11 12:43:48 +0100 | [diff] [blame] | 6451 | btrfs_log_new_name(trans, BTRFS_I(inode), NULL, parent); |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 6452 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6453 | |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 6454 | fail: |
Filipe Manana | 271dba45 | 2016-01-05 16:24:05 +0000 | [diff] [blame] | 6455 | if (trans) |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 6456 | btrfs_end_transaction(trans); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6457 | if (drop_inode) { |
| 6458 | inode_dec_link_count(inode); |
| 6459 | iput(inode); |
| 6460 | } |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6461 | btrfs_btree_balance_dirty(fs_info); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6462 | return err; |
| 6463 | } |
| 6464 | |
Al Viro | 18bb1db | 2011-07-26 01:41:39 -0400 | [diff] [blame] | 6465 | 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] | 6466 | { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6467 | struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); |
Chris Mason | b9d8666 | 2008-05-02 16:13:49 -0400 | [diff] [blame] | 6468 | struct inode *inode = NULL; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6469 | struct btrfs_trans_handle *trans; |
| 6470 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 6471 | int err = 0; |
Chris Mason | b9d8666 | 2008-05-02 16:13:49 -0400 | [diff] [blame] | 6472 | u64 objectid = 0; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 6473 | u64 index = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6474 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 6475 | /* |
| 6476 | * 2 items for inode and ref |
| 6477 | * 2 items for dir items |
| 6478 | * 1 for xattr if selinux is on |
| 6479 | */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6480 | trans = btrfs_start_transaction(root, 5); |
| 6481 | if (IS_ERR(trans)) |
| 6482 | return PTR_ERR(trans); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6483 | |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 6484 | err = btrfs_find_free_ino(root, &objectid); |
| 6485 | if (err) |
| 6486 | goto out_fail; |
| 6487 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6488 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 6489 | dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid, |
| 6490 | S_IFDIR | mode, &index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6491 | if (IS_ERR(inode)) { |
| 6492 | err = PTR_ERR(inode); |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6493 | inode = NULL; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6494 | goto out_fail; |
| 6495 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6496 | |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6497 | /* these must be set before we unlock the inode */ |
| 6498 | inode->i_op = &btrfs_dir_inode_operations; |
| 6499 | inode->i_fop = &btrfs_dir_file_operations; |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 6500 | |
Eric Paris | 2a7dba3 | 2011-02-01 11:05:39 -0500 | [diff] [blame] | 6501 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 6502 | if (err) |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6503 | goto out_fail; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6504 | |
Nikolay Borisov | 6ef06d2 | 2017-02-20 13:50:34 +0200 | [diff] [blame] | 6505 | btrfs_i_size_write(BTRFS_I(inode), 0); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6506 | err = btrfs_update_inode(trans, root, inode); |
| 6507 | if (err) |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6508 | goto out_fail; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6509 | |
Nikolay Borisov | db0a669 | 2017-02-20 13:51:08 +0200 | [diff] [blame] | 6510 | err = btrfs_add_link(trans, BTRFS_I(dir), BTRFS_I(inode), |
| 6511 | dentry->d_name.name, |
| 6512 | dentry->d_name.len, 0, index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6513 | if (err) |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6514 | goto out_fail; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6515 | |
Al Viro | 1e2e547 | 2018-05-04 08:23:01 -0400 | [diff] [blame] | 6516 | d_instantiate_new(dentry, inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6517 | |
| 6518 | out_fail: |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 6519 | btrfs_end_transaction(trans); |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6520 | if (err && inode) { |
Wang Shilong | c7cfb8a | 2014-12-24 14:45:30 +0800 | [diff] [blame] | 6521 | inode_dec_link_count(inode); |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6522 | discard_new_inode(inode); |
Wang Shilong | c7cfb8a | 2014-12-24 14:45:30 +0800 | [diff] [blame] | 6523 | } |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6524 | btrfs_btree_balance_dirty(fs_info); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6525 | return err; |
| 6526 | } |
| 6527 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6528 | static noinline int uncompress_inline(struct btrfs_path *path, |
Byongho Lee | e40da0e | 2015-05-19 23:46:45 +0900 | [diff] [blame] | 6529 | struct page *page, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6530 | size_t pg_offset, u64 extent_offset, |
| 6531 | struct btrfs_file_extent_item *item) |
| 6532 | { |
| 6533 | int ret; |
| 6534 | struct extent_buffer *leaf = path->nodes[0]; |
| 6535 | char *tmp; |
| 6536 | size_t max_size; |
| 6537 | unsigned long inline_size; |
| 6538 | unsigned long ptr; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 6539 | int compress_type; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6540 | |
| 6541 | WARN_ON(pg_offset != 0); |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 6542 | compress_type = btrfs_file_extent_compression(leaf, item); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6543 | max_size = btrfs_file_extent_ram_bytes(leaf, item); |
| 6544 | inline_size = btrfs_file_extent_inline_item_len(leaf, |
Ross Kirk | dd3cc16 | 2013-09-16 15:58:09 +0100 | [diff] [blame] | 6545 | btrfs_item_nr(path->slots[0])); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6546 | tmp = kmalloc(inline_size, GFP_NOFS); |
Tsutomu Itoh | 8d41371 | 2011-04-25 19:43:52 -0400 | [diff] [blame] | 6547 | if (!tmp) |
| 6548 | return -ENOMEM; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6549 | ptr = btrfs_file_extent_inline_start(item); |
| 6550 | |
| 6551 | read_extent_buffer(leaf, tmp, ptr, inline_size); |
| 6552 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 6553 | max_size = min_t(unsigned long, PAGE_SIZE, max_size); |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 6554 | ret = btrfs_decompress(compress_type, tmp, page, |
| 6555 | extent_offset, inline_size, max_size); |
Zygo Blaxell | e1699d2 | 2017-03-10 16:45:44 -0500 | [diff] [blame] | 6556 | |
| 6557 | /* |
| 6558 | * decompression code contains a memset to fill in any space between the end |
| 6559 | * of the uncompressed data and the end of max_size in case the decompressed |
| 6560 | * data ends up shorter than ram_bytes. That doesn't cover the hole between |
| 6561 | * the end of an inline extent and the beginning of the next block, so we |
| 6562 | * cover that region here. |
| 6563 | */ |
| 6564 | |
| 6565 | if (max_size + pg_offset < PAGE_SIZE) { |
| 6566 | char *map = kmap(page); |
| 6567 | memset(map + pg_offset + max_size, 0, PAGE_SIZE - max_size - pg_offset); |
| 6568 | kunmap(page); |
| 6569 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6570 | kfree(tmp); |
Zach Brown | 166ae5a | 2014-05-09 17:15:10 -0400 | [diff] [blame] | 6571 | return ret; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6572 | } |
| 6573 | |
Omar Sandoval | 39b07b5 | 2019-12-02 17:34:23 -0800 | [diff] [blame] | 6574 | /** |
| 6575 | * btrfs_get_extent - Lookup the first extent overlapping a range in a file. |
| 6576 | * @inode: file to search in |
| 6577 | * @page: page to read extent data into if the extent is inline |
| 6578 | * @pg_offset: offset into @page to copy to |
| 6579 | * @start: file offset |
| 6580 | * @len: length of range starting at @start |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 6581 | * |
Omar Sandoval | 39b07b5 | 2019-12-02 17:34:23 -0800 | [diff] [blame] | 6582 | * This returns the first &struct extent_map which overlaps with the given |
| 6583 | * range, reading it from the B-tree and caching it if necessary. Note that |
| 6584 | * there may be more extents which overlap the given range after the returned |
| 6585 | * extent_map. |
| 6586 | * |
| 6587 | * If @page is not NULL and the extent is inline, this also reads the extent |
| 6588 | * data directly into the page and marks the extent up to date in the io_tree. |
| 6589 | * |
| 6590 | * Return: ERR_PTR on error, non-NULL extent_map on success. |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 6591 | */ |
Nikolay Borisov | fc4f21b1 | 2017-02-20 13:51:06 +0200 | [diff] [blame] | 6592 | struct extent_map *btrfs_get_extent(struct btrfs_inode *inode, |
Omar Sandoval | 39b07b5 | 2019-12-02 17:34:23 -0800 | [diff] [blame] | 6593 | struct page *page, size_t pg_offset, |
| 6594 | u64 start, u64 len) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6595 | { |
David Sterba | 3ffbd68 | 2018-06-29 10:56:42 +0200 | [diff] [blame] | 6596 | struct btrfs_fs_info *fs_info = inode->root->fs_info; |
Nikolay Borisov | 1028d1c | 2020-08-03 12:58:46 +0300 | [diff] [blame] | 6597 | int ret = 0; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6598 | u64 extent_start = 0; |
| 6599 | u64 extent_end = 0; |
Nikolay Borisov | fc4f21b1 | 2017-02-20 13:51:06 +0200 | [diff] [blame] | 6600 | u64 objectid = btrfs_ino(inode); |
Linus Torvalds | 7e74e23 | 2019-05-01 12:19:20 -0700 | [diff] [blame] | 6601 | int extent_type = -1; |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 6602 | struct btrfs_path *path = NULL; |
Nikolay Borisov | fc4f21b1 | 2017-02-20 13:51:06 +0200 | [diff] [blame] | 6603 | struct btrfs_root *root = inode->root; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6604 | struct btrfs_file_extent_item *item; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6605 | struct extent_buffer *leaf; |
| 6606 | struct btrfs_key found_key; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6607 | struct extent_map *em = NULL; |
Nikolay Borisov | fc4f21b1 | 2017-02-20 13:51:06 +0200 | [diff] [blame] | 6608 | struct extent_map_tree *em_tree = &inode->extent_tree; |
| 6609 | struct extent_io_tree *io_tree = &inode->io_tree; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6610 | |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 6611 | read_lock(&em_tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6612 | em = lookup_extent_mapping(em_tree, start, len); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 6613 | read_unlock(&em_tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6614 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6615 | if (em) { |
Chris Mason | e1c4b74 | 2008-04-22 13:26:46 -0400 | [diff] [blame] | 6616 | if (em->start > start || em->start + em->len <= start) |
| 6617 | free_extent_map(em); |
| 6618 | else if (em->block_start == EXTENT_MAP_INLINE && page) |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 6619 | free_extent_map(em); |
| 6620 | else |
| 6621 | goto out; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6622 | } |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 6623 | em = alloc_extent_map(); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6624 | if (!em) { |
Nikolay Borisov | 1028d1c | 2020-08-03 12:58:46 +0300 | [diff] [blame] | 6625 | ret = -ENOMEM; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6626 | goto out; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6627 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6628 | em->start = EXTENT_MAP_HOLE; |
Chris Mason | 445a694 | 2008-11-10 11:53:33 -0500 | [diff] [blame] | 6629 | em->orig_start = EXTENT_MAP_HOLE; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6630 | em->len = (u64)-1; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6631 | em->block_len = (u64)-1; |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 6632 | |
Liu Bo | bee6ec8 | 2018-08-17 05:05:28 +0800 | [diff] [blame] | 6633 | path = btrfs_alloc_path(); |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 6634 | if (!path) { |
Nikolay Borisov | 1028d1c | 2020-08-03 12:58:46 +0300 | [diff] [blame] | 6635 | ret = -ENOMEM; |
Liu Bo | bee6ec8 | 2018-08-17 05:05:28 +0800 | [diff] [blame] | 6636 | goto out; |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 6637 | } |
| 6638 | |
Liu Bo | bee6ec8 | 2018-08-17 05:05:28 +0800 | [diff] [blame] | 6639 | /* Chances are we'll be called again, so go ahead and do readahead */ |
| 6640 | path->reada = READA_FORWARD; |
| 6641 | |
Liu Bo | e49aabd | 2018-08-25 13:47:09 +0800 | [diff] [blame] | 6642 | /* |
| 6643 | * Unless we're going to uncompress the inline extent, no sleep would |
| 6644 | * happen. |
| 6645 | */ |
| 6646 | path->leave_spinning = 1; |
| 6647 | |
Josef Bacik | 5189941 | 2020-08-20 11:46:01 -0400 | [diff] [blame] | 6648 | path->recurse = btrfs_is_free_space_inode(inode); |
| 6649 | |
Nikolay Borisov | 5c9a702 | 2017-12-01 11:19:40 +0200 | [diff] [blame] | 6650 | ret = btrfs_lookup_file_extent(NULL, root, path, objectid, start, 0); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6651 | if (ret < 0) { |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6652 | goto out; |
Nikolay Borisov | b8eeab7 | 2018-12-17 11:49:00 +0200 | [diff] [blame] | 6653 | } else if (ret > 0) { |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6654 | if (path->slots[0] == 0) |
| 6655 | goto not_found; |
| 6656 | path->slots[0]--; |
Nikolay Borisov | 1028d1c | 2020-08-03 12:58:46 +0300 | [diff] [blame] | 6657 | ret = 0; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6658 | } |
| 6659 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6660 | leaf = path->nodes[0]; |
| 6661 | item = btrfs_item_ptr(leaf, path->slots[0], |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6662 | struct btrfs_file_extent_item); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6663 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6664 | if (found_key.objectid != objectid || |
Nikolay Borisov | 694c12e | 2018-12-17 10:35:59 +0200 | [diff] [blame] | 6665 | found_key.type != BTRFS_EXTENT_DATA_KEY) { |
Josef Bacik | 25a5034 | 2013-10-14 12:08:38 -0400 | [diff] [blame] | 6666 | /* |
| 6667 | * If we backup past the first extent we want to move forward |
| 6668 | * and see if there is an extent in front of us, otherwise we'll |
| 6669 | * say there is a hole for our whole search range which can |
| 6670 | * cause problems. |
| 6671 | */ |
| 6672 | extent_end = start; |
| 6673 | goto next; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6674 | } |
| 6675 | |
Nikolay Borisov | 694c12e | 2018-12-17 10:35:59 +0200 | [diff] [blame] | 6676 | extent_type = btrfs_file_extent_type(leaf, item); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6677 | extent_start = found_key.offset; |
Filipe Manana | a5eeb3d | 2020-03-09 12:41:06 +0000 | [diff] [blame] | 6678 | extent_end = btrfs_file_extent_end(path); |
Nikolay Borisov | 694c12e | 2018-12-17 10:35:59 +0200 | [diff] [blame] | 6679 | if (extent_type == BTRFS_FILE_EXTENT_REG || |
| 6680 | extent_type == BTRFS_FILE_EXTENT_PREALLOC) { |
Qu Wenruo | 6bf9e4b | 2019-03-13 13:55:11 +0800 | [diff] [blame] | 6681 | /* Only regular file could have regular/prealloc extent */ |
| 6682 | if (!S_ISREG(inode->vfs_inode.i_mode)) { |
Nikolay Borisov | 1028d1c | 2020-08-03 12:58:46 +0300 | [diff] [blame] | 6683 | ret = -EUCLEAN; |
Qu Wenruo | 6bf9e4b | 2019-03-13 13:55:11 +0800 | [diff] [blame] | 6684 | btrfs_crit(fs_info, |
| 6685 | "regular/prealloc extent found for non-regular inode %llu", |
| 6686 | btrfs_ino(inode)); |
| 6687 | goto out; |
| 6688 | } |
Liu Bo | 09ed2f1 | 2017-03-10 11:09:48 -0800 | [diff] [blame] | 6689 | trace_btrfs_get_extent_show_fi_regular(inode, leaf, item, |
| 6690 | extent_start); |
Nikolay Borisov | 694c12e | 2018-12-17 10:35:59 +0200 | [diff] [blame] | 6691 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
Liu Bo | 09ed2f1 | 2017-03-10 11:09:48 -0800 | [diff] [blame] | 6692 | trace_btrfs_get_extent_show_fi_inline(inode, leaf, item, |
| 6693 | path->slots[0], |
| 6694 | extent_start); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6695 | } |
Josef Bacik | 25a5034 | 2013-10-14 12:08:38 -0400 | [diff] [blame] | 6696 | next: |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6697 | if (start >= extent_end) { |
| 6698 | path->slots[0]++; |
| 6699 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { |
| 6700 | ret = btrfs_next_leaf(root, path); |
Nikolay Borisov | 1028d1c | 2020-08-03 12:58:46 +0300 | [diff] [blame] | 6701 | if (ret < 0) |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6702 | goto out; |
Nikolay Borisov | 1028d1c | 2020-08-03 12:58:46 +0300 | [diff] [blame] | 6703 | else if (ret > 0) |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6704 | goto not_found; |
Nikolay Borisov | 1028d1c | 2020-08-03 12:58:46 +0300 | [diff] [blame] | 6705 | |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6706 | leaf = path->nodes[0]; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6707 | } |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6708 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 6709 | if (found_key.objectid != objectid || |
| 6710 | found_key.type != BTRFS_EXTENT_DATA_KEY) |
| 6711 | goto not_found; |
| 6712 | if (start + len <= found_key.offset) |
| 6713 | goto not_found; |
Wang Shilong | e2eca69 | 2014-07-17 11:44:14 +0800 | [diff] [blame] | 6714 | if (start > found_key.offset) |
| 6715 | goto next; |
Nikolay Borisov | 02a033d | 2018-12-17 10:36:02 +0200 | [diff] [blame] | 6716 | |
| 6717 | /* New extent overlaps with existing one */ |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6718 | em->start = start; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 6719 | em->orig_start = start; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6720 | em->len = found_key.offset - start; |
Nikolay Borisov | 02a033d | 2018-12-17 10:36:02 +0200 | [diff] [blame] | 6721 | em->block_start = EXTENT_MAP_HOLE; |
| 6722 | goto insert; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6723 | } |
| 6724 | |
Omar Sandoval | 39b07b5 | 2019-12-02 17:34:23 -0800 | [diff] [blame] | 6725 | btrfs_extent_item_to_extent_map(inode, path, item, !page, em); |
Filipe Manana | 7ffbb59 | 2014-06-09 03:48:05 +0100 | [diff] [blame] | 6726 | |
Nikolay Borisov | 694c12e | 2018-12-17 10:35:59 +0200 | [diff] [blame] | 6727 | if (extent_type == BTRFS_FILE_EXTENT_REG || |
| 6728 | extent_type == BTRFS_FILE_EXTENT_PREALLOC) { |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6729 | goto insert; |
Nikolay Borisov | 694c12e | 2018-12-17 10:35:59 +0200 | [diff] [blame] | 6730 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6731 | unsigned long ptr; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6732 | char *map; |
Chris Mason | 3326d1b | 2007-10-15 16:18:25 -0400 | [diff] [blame] | 6733 | size_t size; |
| 6734 | size_t extent_offset; |
| 6735 | size_t copy_size; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6736 | |
Omar Sandoval | 39b07b5 | 2019-12-02 17:34:23 -0800 | [diff] [blame] | 6737 | if (!page) |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 6738 | goto out; |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 6739 | |
Qu Wenruo | e41ca58 | 2018-06-06 15:41:49 +0800 | [diff] [blame] | 6740 | size = btrfs_file_extent_ram_bytes(leaf, item); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6741 | extent_offset = page_offset(page) + pg_offset - extent_start; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 6742 | copy_size = min_t(u64, PAGE_SIZE - pg_offset, |
| 6743 | size - extent_offset); |
Chris Mason | 3326d1b | 2007-10-15 16:18:25 -0400 | [diff] [blame] | 6744 | em->start = extent_start + extent_offset; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6745 | em->len = ALIGN(copy_size, fs_info->sectorsize); |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 6746 | em->orig_block_len = em->len; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 6747 | em->orig_start = em->start; |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 6748 | ptr = btrfs_file_extent_inline_start(item) + extent_offset; |
Liu Bo | e49aabd | 2018-08-25 13:47:09 +0800 | [diff] [blame] | 6749 | |
| 6750 | btrfs_set_path_blocking(path); |
Edmund Nadolski | bf46f52 | 2017-11-20 13:24:49 -0700 | [diff] [blame] | 6751 | if (!PageUptodate(page)) { |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 6752 | if (btrfs_file_extent_compression(leaf, item) != |
| 6753 | BTRFS_COMPRESS_NONE) { |
Byongho Lee | e40da0e | 2015-05-19 23:46:45 +0900 | [diff] [blame] | 6754 | ret = uncompress_inline(path, page, pg_offset, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6755 | extent_offset, item); |
Nikolay Borisov | 1028d1c | 2020-08-03 12:58:46 +0300 | [diff] [blame] | 6756 | if (ret) |
Zach Brown | 166ae5a | 2014-05-09 17:15:10 -0400 | [diff] [blame] | 6757 | goto out; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6758 | } else { |
| 6759 | map = kmap(page); |
| 6760 | read_extent_buffer(leaf, map + pg_offset, ptr, |
| 6761 | copy_size); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 6762 | if (pg_offset + copy_size < PAGE_SIZE) { |
Chris Mason | 93c82d5 | 2009-09-11 12:36:29 -0400 | [diff] [blame] | 6763 | memset(map + pg_offset + copy_size, 0, |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 6764 | PAGE_SIZE - pg_offset - |
Chris Mason | 93c82d5 | 2009-09-11 12:36:29 -0400 | [diff] [blame] | 6765 | copy_size); |
| 6766 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6767 | kunmap(page); |
| 6768 | } |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 6769 | flush_dcache_page(page); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6770 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6771 | set_extent_uptodate(io_tree, em->start, |
Arne Jansen | 507903b | 2011-04-06 10:02:20 +0000 | [diff] [blame] | 6772 | extent_map_end(em) - 1, NULL, GFP_NOFS); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6773 | goto insert; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6774 | } |
| 6775 | not_found: |
| 6776 | em->start = start; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 6777 | em->orig_start = start; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6778 | em->len = len; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6779 | em->block_start = EXTENT_MAP_HOLE; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6780 | insert: |
Nikolay Borisov | 1028d1c | 2020-08-03 12:58:46 +0300 | [diff] [blame] | 6781 | ret = 0; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 6782 | btrfs_release_path(path); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6783 | if (em->start > start || extent_map_end(em) <= start) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6784 | btrfs_err(fs_info, |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 6785 | "bad extent! em: [%llu %llu] passed [%llu %llu]", |
| 6786 | em->start, em->len, start, len); |
Nikolay Borisov | 1028d1c | 2020-08-03 12:58:46 +0300 | [diff] [blame] | 6787 | ret = -EIO; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6788 | goto out; |
| 6789 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6790 | |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 6791 | write_lock(&em_tree->lock); |
Nikolay Borisov | 1028d1c | 2020-08-03 12:58:46 +0300 | [diff] [blame] | 6792 | ret = btrfs_add_extent_mapping(fs_info, em_tree, &em, start, len); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 6793 | write_unlock(&em_tree->lock); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6794 | out: |
Liu Bo | c641428 | 2018-08-23 07:36:17 +0800 | [diff] [blame] | 6795 | btrfs_free_path(path); |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 6796 | |
Nikolay Borisov | fc4f21b1 | 2017-02-20 13:51:06 +0200 | [diff] [blame] | 6797 | trace_btrfs_get_extent(root, inode, em); |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 6798 | |
Nikolay Borisov | 1028d1c | 2020-08-03 12:58:46 +0300 | [diff] [blame] | 6799 | if (ret) { |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6800 | free_extent_map(em); |
Nikolay Borisov | 1028d1c | 2020-08-03 12:58:46 +0300 | [diff] [blame] | 6801 | return ERR_PTR(ret); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6802 | } |
| 6803 | return em; |
| 6804 | } |
| 6805 | |
Nikolay Borisov | fc4f21b1 | 2017-02-20 13:51:06 +0200 | [diff] [blame] | 6806 | struct extent_map *btrfs_get_extent_fiemap(struct btrfs_inode *inode, |
Nikolay Borisov | 4ab47a8 | 2018-12-12 09:42:32 +0200 | [diff] [blame] | 6807 | u64 start, u64 len) |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6808 | { |
| 6809 | struct extent_map *em; |
| 6810 | struct extent_map *hole_em = NULL; |
Nikolay Borisov | f3714ef | 2018-12-12 09:42:33 +0200 | [diff] [blame] | 6811 | u64 delalloc_start = start; |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6812 | u64 end; |
Nikolay Borisov | f3714ef | 2018-12-12 09:42:33 +0200 | [diff] [blame] | 6813 | u64 delalloc_len; |
| 6814 | u64 delalloc_end; |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6815 | int err = 0; |
| 6816 | |
Omar Sandoval | 39b07b5 | 2019-12-02 17:34:23 -0800 | [diff] [blame] | 6817 | em = btrfs_get_extent(inode, NULL, 0, start, len); |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6818 | if (IS_ERR(em)) |
| 6819 | return em; |
Dan Carpenter | 9986277 | 2017-04-11 11:57:15 +0300 | [diff] [blame] | 6820 | /* |
| 6821 | * If our em maps to: |
| 6822 | * - a hole or |
| 6823 | * - a pre-alloc extent, |
| 6824 | * there might actually be delalloc bytes behind it. |
| 6825 | */ |
| 6826 | if (em->block_start != EXTENT_MAP_HOLE && |
| 6827 | !test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) |
| 6828 | return em; |
| 6829 | else |
| 6830 | hole_em = em; |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6831 | |
| 6832 | /* check to see if we've wrapped (len == -1 or similar) */ |
| 6833 | end = start + len; |
| 6834 | if (end < start) |
| 6835 | end = (u64)-1; |
| 6836 | else |
| 6837 | end -= 1; |
| 6838 | |
| 6839 | em = NULL; |
| 6840 | |
| 6841 | /* ok, we didn't find anything, lets look for delalloc */ |
Nikolay Borisov | f3714ef | 2018-12-12 09:42:33 +0200 | [diff] [blame] | 6842 | delalloc_len = count_range_bits(&inode->io_tree, &delalloc_start, |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6843 | end, len, EXTENT_DELALLOC, 1); |
Nikolay Borisov | f3714ef | 2018-12-12 09:42:33 +0200 | [diff] [blame] | 6844 | delalloc_end = delalloc_start + delalloc_len; |
| 6845 | if (delalloc_end < delalloc_start) |
| 6846 | delalloc_end = (u64)-1; |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6847 | |
| 6848 | /* |
Nikolay Borisov | f3714ef | 2018-12-12 09:42:33 +0200 | [diff] [blame] | 6849 | * We didn't find anything useful, return the original results from |
| 6850 | * get_extent() |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6851 | */ |
Nikolay Borisov | f3714ef | 2018-12-12 09:42:33 +0200 | [diff] [blame] | 6852 | if (delalloc_start > end || delalloc_end <= start) { |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6853 | em = hole_em; |
| 6854 | hole_em = NULL; |
| 6855 | goto out; |
| 6856 | } |
| 6857 | |
Nikolay Borisov | f3714ef | 2018-12-12 09:42:33 +0200 | [diff] [blame] | 6858 | /* |
| 6859 | * Adjust the delalloc_start to make sure it doesn't go backwards from |
| 6860 | * the start they passed in |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6861 | */ |
Nikolay Borisov | f3714ef | 2018-12-12 09:42:33 +0200 | [diff] [blame] | 6862 | delalloc_start = max(start, delalloc_start); |
| 6863 | delalloc_len = delalloc_end - delalloc_start; |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6864 | |
Nikolay Borisov | f3714ef | 2018-12-12 09:42:33 +0200 | [diff] [blame] | 6865 | if (delalloc_len > 0) { |
| 6866 | u64 hole_start; |
Nikolay Borisov | 02950af | 2018-12-12 09:42:34 +0200 | [diff] [blame] | 6867 | u64 hole_len; |
Nikolay Borisov | f3714ef | 2018-12-12 09:42:33 +0200 | [diff] [blame] | 6868 | const u64 hole_end = extent_map_end(hole_em); |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6869 | |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 6870 | em = alloc_extent_map(); |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6871 | if (!em) { |
| 6872 | err = -ENOMEM; |
| 6873 | goto out; |
| 6874 | } |
Nikolay Borisov | f3714ef | 2018-12-12 09:42:33 +0200 | [diff] [blame] | 6875 | |
| 6876 | ASSERT(hole_em); |
| 6877 | /* |
| 6878 | * When btrfs_get_extent can't find anything it returns one |
| 6879 | * huge hole |
| 6880 | * |
| 6881 | * Make sure what it found really fits our range, and adjust to |
| 6882 | * make sure it is based on the start from the caller |
| 6883 | */ |
| 6884 | if (hole_end <= start || hole_em->start > end) { |
| 6885 | free_extent_map(hole_em); |
| 6886 | hole_em = NULL; |
| 6887 | } else { |
| 6888 | hole_start = max(hole_em->start, start); |
| 6889 | hole_len = hole_end - hole_start; |
| 6890 | } |
| 6891 | |
| 6892 | if (hole_em && delalloc_start > hole_start) { |
| 6893 | /* |
| 6894 | * Our hole starts before our delalloc, so we have to |
| 6895 | * return just the parts of the hole that go until the |
| 6896 | * delalloc starts |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6897 | */ |
Nikolay Borisov | f3714ef | 2018-12-12 09:42:33 +0200 | [diff] [blame] | 6898 | em->len = min(hole_len, delalloc_start - hole_start); |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6899 | em->start = hole_start; |
| 6900 | em->orig_start = hole_start; |
| 6901 | /* |
Nikolay Borisov | f3714ef | 2018-12-12 09:42:33 +0200 | [diff] [blame] | 6902 | * Don't adjust block start at all, it is fixed at |
| 6903 | * EXTENT_MAP_HOLE |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6904 | */ |
| 6905 | em->block_start = hole_em->block_start; |
| 6906 | em->block_len = hole_len; |
Liu Bo | f9e4fb5 | 2013-01-07 10:10:12 +0000 | [diff] [blame] | 6907 | if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags)) |
| 6908 | set_bit(EXTENT_FLAG_PREALLOC, &em->flags); |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6909 | } else { |
Nikolay Borisov | f3714ef | 2018-12-12 09:42:33 +0200 | [diff] [blame] | 6910 | /* |
| 6911 | * Hole is out of passed range or it starts after |
| 6912 | * delalloc range |
| 6913 | */ |
| 6914 | em->start = delalloc_start; |
| 6915 | em->len = delalloc_len; |
| 6916 | em->orig_start = delalloc_start; |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6917 | em->block_start = EXTENT_MAP_DELALLOC; |
Nikolay Borisov | f3714ef | 2018-12-12 09:42:33 +0200 | [diff] [blame] | 6918 | em->block_len = delalloc_len; |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6919 | } |
Nikolay Borisov | bf8d32b | 2017-12-01 11:19:43 +0200 | [diff] [blame] | 6920 | } else { |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6921 | return hole_em; |
| 6922 | } |
| 6923 | out: |
| 6924 | |
| 6925 | free_extent_map(hole_em); |
| 6926 | if (err) { |
| 6927 | free_extent_map(em); |
| 6928 | return ERR_PTR(err); |
| 6929 | } |
| 6930 | return em; |
| 6931 | } |
| 6932 | |
Nikolay Borisov | 64f5418 | 2020-06-03 08:55:31 +0300 | [diff] [blame] | 6933 | static struct extent_map *btrfs_create_dio_extent(struct btrfs_inode *inode, |
Filipe Manana | 5f9a8a5 | 2016-05-12 13:53:36 +0100 | [diff] [blame] | 6934 | const u64 start, |
| 6935 | const u64 len, |
| 6936 | const u64 orig_start, |
| 6937 | const u64 block_start, |
| 6938 | const u64 block_len, |
| 6939 | const u64 orig_block_len, |
| 6940 | const u64 ram_bytes, |
| 6941 | const int type) |
| 6942 | { |
| 6943 | struct extent_map *em = NULL; |
| 6944 | int ret; |
| 6945 | |
Filipe Manana | 5f9a8a5 | 2016-05-12 13:53:36 +0100 | [diff] [blame] | 6946 | if (type != BTRFS_ORDERED_NOCOW) { |
Nikolay Borisov | 64f5418 | 2020-06-03 08:55:31 +0300 | [diff] [blame] | 6947 | em = create_io_em(inode, start, len, orig_start, block_start, |
| 6948 | block_len, orig_block_len, ram_bytes, |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 6949 | BTRFS_COMPRESS_NONE, /* compress_type */ |
| 6950 | type); |
Filipe Manana | 5f9a8a5 | 2016-05-12 13:53:36 +0100 | [diff] [blame] | 6951 | if (IS_ERR(em)) |
| 6952 | goto out; |
| 6953 | } |
Nikolay Borisov | 64f5418 | 2020-06-03 08:55:31 +0300 | [diff] [blame] | 6954 | ret = btrfs_add_ordered_extent_dio(inode, start, block_start, len, |
| 6955 | block_len, type); |
Filipe Manana | 5f9a8a5 | 2016-05-12 13:53:36 +0100 | [diff] [blame] | 6956 | if (ret) { |
| 6957 | if (em) { |
| 6958 | free_extent_map(em); |
Nikolay Borisov | 64f5418 | 2020-06-03 08:55:31 +0300 | [diff] [blame] | 6959 | btrfs_drop_extent_cache(inode, start, start + len - 1, 0); |
Filipe Manana | 5f9a8a5 | 2016-05-12 13:53:36 +0100 | [diff] [blame] | 6960 | } |
| 6961 | em = ERR_PTR(ret); |
| 6962 | } |
| 6963 | out: |
Filipe Manana | 5f9a8a5 | 2016-05-12 13:53:36 +0100 | [diff] [blame] | 6964 | |
| 6965 | return em; |
| 6966 | } |
| 6967 | |
Nikolay Borisov | 9fc6f91 | 2020-06-03 08:55:32 +0300 | [diff] [blame] | 6968 | static struct extent_map *btrfs_new_extent_direct(struct btrfs_inode *inode, |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6969 | u64 start, u64 len) |
| 6970 | { |
Nikolay Borisov | 9fc6f91 | 2020-06-03 08:55:32 +0300 | [diff] [blame] | 6971 | struct btrfs_root *root = inode->root; |
| 6972 | struct btrfs_fs_info *fs_info = root->fs_info; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 6973 | struct extent_map *em; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6974 | struct btrfs_key ins; |
| 6975 | u64 alloc_hint; |
| 6976 | int ret; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6977 | |
Nikolay Borisov | 9fc6f91 | 2020-06-03 08:55:32 +0300 | [diff] [blame] | 6978 | alloc_hint = get_extent_allocation_hint(inode, start, len); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6979 | ret = btrfs_reserve_extent(root, len, len, fs_info->sectorsize, |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 6980 | 0, alloc_hint, &ins, 1, 1); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 6981 | if (ret) |
| 6982 | return ERR_PTR(ret); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6983 | |
Nikolay Borisov | 9fc6f91 | 2020-06-03 08:55:32 +0300 | [diff] [blame] | 6984 | em = btrfs_create_dio_extent(inode, start, ins.offset, start, |
Filipe Manana | 5f9a8a5 | 2016-05-12 13:53:36 +0100 | [diff] [blame] | 6985 | ins.objectid, ins.offset, ins.offset, |
Liu Bo | 6288d6e | 2017-02-21 12:12:58 -0800 | [diff] [blame] | 6986 | ins.offset, BTRFS_ORDERED_REGULAR); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6987 | btrfs_dec_block_group_reservations(fs_info, ins.objectid); |
Filipe Manana | 5f9a8a5 | 2016-05-12 13:53:36 +0100 | [diff] [blame] | 6988 | if (IS_ERR(em)) |
Nikolay Borisov | 9fc6f91 | 2020-06-03 08:55:32 +0300 | [diff] [blame] | 6989 | btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, |
| 6990 | 1); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 6991 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6992 | return em; |
| 6993 | } |
| 6994 | |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6995 | /* |
Qu Wenruo | e4ecaf9 | 2020-06-24 07:23:51 +0800 | [diff] [blame] | 6996 | * Check if we can do nocow write into the range [@offset, @offset + @len) |
| 6997 | * |
| 6998 | * @offset: File offset |
| 6999 | * @len: The length to write, will be updated to the nocow writeable |
| 7000 | * range |
| 7001 | * @orig_start: (optional) Return the original file offset of the file extent |
| 7002 | * @orig_len: (optional) Return the original on-disk length of the file extent |
| 7003 | * @ram_bytes: (optional) Return the ram_bytes of the file extent |
Boris Burkov | a84d5d4 | 2020-08-18 11:00:05 -0700 | [diff] [blame] | 7004 | * @strict: if true, omit optimizations that might force us into unnecessary |
| 7005 | * cow. e.g., don't trust generation number. |
Qu Wenruo | e4ecaf9 | 2020-06-24 07:23:51 +0800 | [diff] [blame] | 7006 | * |
| 7007 | * This function will flush ordered extents in the range to ensure proper |
| 7008 | * nocow checks for (nowait == false) case. |
| 7009 | * |
| 7010 | * Return: |
| 7011 | * >0 and update @len if we can do nocow write |
| 7012 | * 0 if we can't do nocow write |
| 7013 | * <0 if error happened |
| 7014 | * |
| 7015 | * NOTE: This only checks the file extents, caller is responsible to wait for |
| 7016 | * any ordered extents. |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7017 | */ |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 7018 | noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len, |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 7019 | u64 *orig_start, u64 *orig_block_len, |
Boris Burkov | a84d5d4 | 2020-08-18 11:00:05 -0700 | [diff] [blame] | 7020 | u64 *ram_bytes, bool strict) |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7021 | { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 7022 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7023 | struct btrfs_path *path; |
| 7024 | int ret; |
| 7025 | struct extent_buffer *leaf; |
| 7026 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Miao Xie | 7b2b708 | 2014-02-27 13:58:05 +0800 | [diff] [blame] | 7027 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7028 | struct btrfs_file_extent_item *fi; |
| 7029 | struct btrfs_key key; |
| 7030 | u64 disk_bytenr; |
| 7031 | u64 backref_offset; |
| 7032 | u64 extent_end; |
| 7033 | u64 num_bytes; |
| 7034 | int slot; |
| 7035 | int found_type; |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 7036 | bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW); |
Miao Xie | e77751a | 2013-12-27 21:11:50 +0800 | [diff] [blame] | 7037 | |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7038 | path = btrfs_alloc_path(); |
| 7039 | if (!path) |
| 7040 | return -ENOMEM; |
| 7041 | |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 7042 | ret = btrfs_lookup_file_extent(NULL, root, path, |
| 7043 | btrfs_ino(BTRFS_I(inode)), offset, 0); |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7044 | if (ret < 0) |
| 7045 | goto out; |
| 7046 | |
| 7047 | slot = path->slots[0]; |
| 7048 | if (ret == 1) { |
| 7049 | if (slot == 0) { |
| 7050 | /* can't find the item, must cow */ |
| 7051 | ret = 0; |
| 7052 | goto out; |
| 7053 | } |
| 7054 | slot--; |
| 7055 | } |
| 7056 | ret = 0; |
| 7057 | leaf = path->nodes[0]; |
| 7058 | btrfs_item_key_to_cpu(leaf, &key, slot); |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 7059 | if (key.objectid != btrfs_ino(BTRFS_I(inode)) || |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7060 | key.type != BTRFS_EXTENT_DATA_KEY) { |
| 7061 | /* not our file or wrong item type, must cow */ |
| 7062 | goto out; |
| 7063 | } |
| 7064 | |
| 7065 | if (key.offset > offset) { |
| 7066 | /* Wrong offset, must cow */ |
| 7067 | goto out; |
| 7068 | } |
| 7069 | |
| 7070 | fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item); |
| 7071 | found_type = btrfs_file_extent_type(leaf, fi); |
| 7072 | if (found_type != BTRFS_FILE_EXTENT_REG && |
| 7073 | found_type != BTRFS_FILE_EXTENT_PREALLOC) { |
| 7074 | /* not a regular extent, must cow */ |
| 7075 | goto out; |
| 7076 | } |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 7077 | |
| 7078 | if (!nocow && found_type == BTRFS_FILE_EXTENT_REG) |
| 7079 | goto out; |
| 7080 | |
Miao Xie | e77751a | 2013-12-27 21:11:50 +0800 | [diff] [blame] | 7081 | extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi); |
| 7082 | if (extent_end <= offset) |
| 7083 | goto out; |
| 7084 | |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7085 | disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 7086 | if (disk_bytenr == 0) |
| 7087 | goto out; |
| 7088 | |
| 7089 | if (btrfs_file_extent_compression(leaf, fi) || |
| 7090 | btrfs_file_extent_encryption(leaf, fi) || |
| 7091 | btrfs_file_extent_other_encoding(leaf, fi)) |
| 7092 | goto out; |
| 7093 | |
Ethan Lien | 78d4295 | 2018-05-17 14:58:29 +0800 | [diff] [blame] | 7094 | /* |
| 7095 | * Do the same check as in btrfs_cross_ref_exist but without the |
| 7096 | * unnecessary search. |
| 7097 | */ |
Boris Burkov | a84d5d4 | 2020-08-18 11:00:05 -0700 | [diff] [blame] | 7098 | if (!strict && |
| 7099 | (btrfs_file_extent_generation(leaf, fi) <= |
| 7100 | btrfs_root_last_snapshot(&root->root_item))) |
Ethan Lien | 78d4295 | 2018-05-17 14:58:29 +0800 | [diff] [blame] | 7101 | goto out; |
| 7102 | |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7103 | backref_offset = btrfs_file_extent_offset(leaf, fi); |
| 7104 | |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 7105 | if (orig_start) { |
| 7106 | *orig_start = key.offset - backref_offset; |
| 7107 | *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi); |
| 7108 | *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi); |
| 7109 | } |
Josef Bacik | eb384b5 | 2013-04-24 16:32:55 -0400 | [diff] [blame] | 7110 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 7111 | if (btrfs_extent_readonly(fs_info, disk_bytenr)) |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7112 | goto out; |
Miao Xie | 7b2b708 | 2014-02-27 13:58:05 +0800 | [diff] [blame] | 7113 | |
| 7114 | num_bytes = min(offset + *len, extent_end) - offset; |
| 7115 | if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) { |
| 7116 | u64 range_end; |
| 7117 | |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 7118 | range_end = round_up(offset + num_bytes, |
| 7119 | root->fs_info->sectorsize) - 1; |
Miao Xie | 7b2b708 | 2014-02-27 13:58:05 +0800 | [diff] [blame] | 7120 | ret = test_range_bit(io_tree, offset, range_end, |
| 7121 | EXTENT_DELALLOC, 0, NULL); |
| 7122 | if (ret) { |
| 7123 | ret = -EAGAIN; |
| 7124 | goto out; |
| 7125 | } |
| 7126 | } |
| 7127 | |
Josef Bacik | 1bda19e | 2013-10-18 12:10:36 -0400 | [diff] [blame] | 7128 | btrfs_release_path(path); |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7129 | |
| 7130 | /* |
| 7131 | * look for other files referencing this extent, if we |
| 7132 | * find any we must cow |
| 7133 | */ |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 7134 | |
Liu Bo | e4c3b2d | 2017-01-30 12:25:28 -0800 | [diff] [blame] | 7135 | ret = btrfs_cross_ref_exist(root, btrfs_ino(BTRFS_I(inode)), |
Boris Burkov | a84d5d4 | 2020-08-18 11:00:05 -0700 | [diff] [blame] | 7136 | key.offset - backref_offset, disk_bytenr, |
| 7137 | strict); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 7138 | if (ret) { |
| 7139 | ret = 0; |
| 7140 | goto out; |
| 7141 | } |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7142 | |
| 7143 | /* |
| 7144 | * adjust disk_bytenr and num_bytes to cover just the bytes |
| 7145 | * in this extent we are about to write. If there |
| 7146 | * are any csums in that range we have to cow in order |
| 7147 | * to keep the csums correct |
| 7148 | */ |
| 7149 | disk_bytenr += backref_offset; |
| 7150 | disk_bytenr += offset - key.offset; |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 7151 | if (csum_exist_in_range(fs_info, disk_bytenr, num_bytes)) |
| 7152 | goto out; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7153 | /* |
| 7154 | * all of the above have passed, it is safe to overwrite this extent |
| 7155 | * without cow |
| 7156 | */ |
Josef Bacik | eb384b5 | 2013-04-24 16:32:55 -0400 | [diff] [blame] | 7157 | *len = num_bytes; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7158 | ret = 1; |
| 7159 | out: |
| 7160 | btrfs_free_path(path); |
| 7161 | return ret; |
| 7162 | } |
| 7163 | |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7164 | static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend, |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 7165 | struct extent_state **cached_state, bool writing) |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7166 | { |
| 7167 | struct btrfs_ordered_extent *ordered; |
| 7168 | int ret = 0; |
| 7169 | |
| 7170 | while (1) { |
| 7171 | lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend, |
David Sterba | ff13db4 | 2015-12-03 14:30:40 +0100 | [diff] [blame] | 7172 | cached_state); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7173 | /* |
| 7174 | * 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] | 7175 | * 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] | 7176 | * extents in this range. |
| 7177 | */ |
Nikolay Borisov | a776c6f | 2017-02-20 13:50:49 +0200 | [diff] [blame] | 7178 | ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), lockstart, |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7179 | lockend - lockstart + 1); |
| 7180 | |
| 7181 | /* |
| 7182 | * We need to make sure there are no buffered pages in this |
| 7183 | * range either, we could have raced between the invalidate in |
| 7184 | * generic_file_direct_write and locking the extent. The |
| 7185 | * invalidate needs to happen so that reads after a write do not |
| 7186 | * get stale data. |
| 7187 | */ |
Alex Gartrell | fc4adbff | 2014-05-20 13:07:56 -0700 | [diff] [blame] | 7188 | if (!ordered && |
David Sterba | 051c98e | 2018-03-07 15:33:22 +0100 | [diff] [blame] | 7189 | (!writing || !filemap_range_has_page(inode->i_mapping, |
| 7190 | lockstart, lockend))) |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7191 | break; |
| 7192 | |
| 7193 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend, |
David Sterba | e43bbe5 | 2017-12-12 21:43:52 +0100 | [diff] [blame] | 7194 | cached_state); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7195 | |
| 7196 | if (ordered) { |
Filipe Manana | ade7702 | 2016-02-18 14:28:55 +0000 | [diff] [blame] | 7197 | /* |
| 7198 | * If we are doing a DIO read and the ordered extent we |
| 7199 | * found is for a buffered write, we can not wait for it |
| 7200 | * to complete and retry, because if we do so we can |
| 7201 | * deadlock with concurrent buffered writes on page |
| 7202 | * locks. This happens only if our DIO read covers more |
| 7203 | * than one extent map, if at this point has already |
| 7204 | * created an ordered extent for a previous extent map |
| 7205 | * and locked its range in the inode's io tree, and a |
| 7206 | * concurrent write against that previous extent map's |
| 7207 | * range and this range started (we unlock the ranges |
| 7208 | * in the io tree only when the bios complete and |
| 7209 | * buffered writes always lock pages before attempting |
| 7210 | * to lock range in the io tree). |
| 7211 | */ |
| 7212 | if (writing || |
| 7213 | test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags)) |
Nikolay Borisov | c0a4360 | 2020-09-18 12:15:53 +0300 | [diff] [blame] | 7214 | btrfs_start_ordered_extent(ordered, 1); |
Filipe Manana | ade7702 | 2016-02-18 14:28:55 +0000 | [diff] [blame] | 7215 | else |
| 7216 | ret = -ENOTBLK; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7217 | btrfs_put_ordered_extent(ordered); |
| 7218 | } else { |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7219 | /* |
Filipe Manana | b850ae1 | 2015-12-08 16:23:16 +0000 | [diff] [blame] | 7220 | * We could trigger writeback for this range (and wait |
| 7221 | * for it to complete) and then invalidate the pages for |
| 7222 | * this range (through invalidate_inode_pages2_range()), |
| 7223 | * but that can lead us to a deadlock with a concurrent |
Matthew Wilcox (Oracle) | ba206a0 | 2020-06-01 21:47:05 -0700 | [diff] [blame] | 7224 | * call to readahead (a buffered read or a defrag call |
Filipe Manana | b850ae1 | 2015-12-08 16:23:16 +0000 | [diff] [blame] | 7225 | * triggered a readahead) on a page lock due to an |
| 7226 | * ordered dio extent we created before but did not have |
| 7227 | * yet a corresponding bio submitted (whence it can not |
Matthew Wilcox (Oracle) | ba206a0 | 2020-06-01 21:47:05 -0700 | [diff] [blame] | 7228 | * complete), which makes readahead wait for that |
Filipe Manana | b850ae1 | 2015-12-08 16:23:16 +0000 | [diff] [blame] | 7229 | * ordered extent to complete while holding a lock on |
| 7230 | * that page. |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7231 | */ |
Filipe Manana | b850ae1 | 2015-12-08 16:23:16 +0000 | [diff] [blame] | 7232 | ret = -ENOTBLK; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7233 | } |
| 7234 | |
Filipe Manana | ade7702 | 2016-02-18 14:28:55 +0000 | [diff] [blame] | 7235 | if (ret) |
| 7236 | break; |
| 7237 | |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7238 | cond_resched(); |
| 7239 | } |
| 7240 | |
| 7241 | return ret; |
| 7242 | } |
| 7243 | |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 7244 | /* The callers of this must take lock_extent() */ |
Nikolay Borisov | 4b67c11 | 2020-06-03 08:55:05 +0300 | [diff] [blame] | 7245 | static struct extent_map *create_io_em(struct btrfs_inode *inode, u64 start, |
| 7246 | u64 len, u64 orig_start, u64 block_start, |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 7247 | u64 block_len, u64 orig_block_len, |
| 7248 | u64 ram_bytes, int compress_type, |
| 7249 | int type) |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7250 | { |
| 7251 | struct extent_map_tree *em_tree; |
| 7252 | struct extent_map *em; |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7253 | int ret; |
| 7254 | |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 7255 | ASSERT(type == BTRFS_ORDERED_PREALLOC || |
| 7256 | type == BTRFS_ORDERED_COMPRESSED || |
| 7257 | type == BTRFS_ORDERED_NOCOW || |
Liu Bo | 1af4a0a | 2017-02-13 15:35:09 -0800 | [diff] [blame] | 7258 | type == BTRFS_ORDERED_REGULAR); |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 7259 | |
Nikolay Borisov | 4b67c11 | 2020-06-03 08:55:05 +0300 | [diff] [blame] | 7260 | em_tree = &inode->extent_tree; |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7261 | em = alloc_extent_map(); |
| 7262 | if (!em) |
| 7263 | return ERR_PTR(-ENOMEM); |
| 7264 | |
| 7265 | em->start = start; |
| 7266 | em->orig_start = orig_start; |
| 7267 | em->len = len; |
| 7268 | em->block_len = block_len; |
| 7269 | em->block_start = block_start; |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 7270 | em->orig_block_len = orig_block_len; |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 7271 | em->ram_bytes = ram_bytes; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 7272 | em->generation = -1; |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7273 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
Liu Bo | 1af4a0a | 2017-02-13 15:35:09 -0800 | [diff] [blame] | 7274 | if (type == BTRFS_ORDERED_PREALLOC) { |
Josef Bacik | b11e234 | 2012-12-03 10:58:15 -0500 | [diff] [blame] | 7275 | set_bit(EXTENT_FLAG_FILLING, &em->flags); |
Liu Bo | 1af4a0a | 2017-02-13 15:35:09 -0800 | [diff] [blame] | 7276 | } else if (type == BTRFS_ORDERED_COMPRESSED) { |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 7277 | set_bit(EXTENT_FLAG_COMPRESSED, &em->flags); |
| 7278 | em->compress_type = compress_type; |
| 7279 | } |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7280 | |
| 7281 | do { |
Nikolay Borisov | 4b67c11 | 2020-06-03 08:55:05 +0300 | [diff] [blame] | 7282 | btrfs_drop_extent_cache(inode, em->start, |
| 7283 | em->start + em->len - 1, 0); |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7284 | write_lock(&em_tree->lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 7285 | ret = add_extent_mapping(em_tree, em, 1); |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7286 | write_unlock(&em_tree->lock); |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 7287 | /* |
| 7288 | * The caller has taken lock_extent(), who could race with us |
| 7289 | * to add em? |
| 7290 | */ |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7291 | } while (ret == -EEXIST); |
| 7292 | |
| 7293 | if (ret) { |
| 7294 | free_extent_map(em); |
| 7295 | return ERR_PTR(ret); |
| 7296 | } |
| 7297 | |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 7298 | /* 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] | 7299 | return em; |
| 7300 | } |
| 7301 | |
Nikolay Borisov | 1c8d017 | 2018-05-02 15:19:32 +0300 | [diff] [blame] | 7302 | |
Nikolay Borisov | c5794e5 | 2018-05-02 15:19:33 +0300 | [diff] [blame] | 7303 | static int btrfs_get_blocks_direct_write(struct extent_map **map, |
Nikolay Borisov | c5794e5 | 2018-05-02 15:19:33 +0300 | [diff] [blame] | 7304 | struct inode *inode, |
| 7305 | struct btrfs_dio_data *dio_data, |
| 7306 | u64 start, u64 len) |
| 7307 | { |
| 7308 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
| 7309 | struct extent_map *em = *map; |
| 7310 | int ret = 0; |
| 7311 | |
| 7312 | /* |
| 7313 | * We don't allocate a new extent in the following cases |
| 7314 | * |
| 7315 | * 1) The inode is marked as NODATACOW. In this case we'll just use the |
| 7316 | * existing extent. |
| 7317 | * 2) The extent is marked as PREALLOC. We're good to go here and can |
| 7318 | * just use the extent. |
| 7319 | * |
| 7320 | */ |
| 7321 | if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) || |
| 7322 | ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) && |
| 7323 | em->block_start != EXTENT_MAP_HOLE)) { |
| 7324 | int type; |
| 7325 | u64 block_start, orig_start, orig_block_len, ram_bytes; |
| 7326 | |
| 7327 | if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) |
| 7328 | type = BTRFS_ORDERED_PREALLOC; |
| 7329 | else |
| 7330 | type = BTRFS_ORDERED_NOCOW; |
| 7331 | len = min(len, em->len - (start - em->start)); |
| 7332 | block_start = em->block_start + (start - em->start); |
| 7333 | |
| 7334 | if (can_nocow_extent(inode, start, &len, &orig_start, |
Boris Burkov | a84d5d4 | 2020-08-18 11:00:05 -0700 | [diff] [blame] | 7335 | &orig_block_len, &ram_bytes, false) == 1 && |
Nikolay Borisov | c5794e5 | 2018-05-02 15:19:33 +0300 | [diff] [blame] | 7336 | btrfs_inc_nocow_writers(fs_info, block_start)) { |
| 7337 | struct extent_map *em2; |
| 7338 | |
Nikolay Borisov | 64f5418 | 2020-06-03 08:55:31 +0300 | [diff] [blame] | 7339 | em2 = btrfs_create_dio_extent(BTRFS_I(inode), start, len, |
Nikolay Borisov | c5794e5 | 2018-05-02 15:19:33 +0300 | [diff] [blame] | 7340 | orig_start, block_start, |
| 7341 | len, orig_block_len, |
| 7342 | ram_bytes, type); |
| 7343 | btrfs_dec_nocow_writers(fs_info, block_start); |
| 7344 | if (type == BTRFS_ORDERED_PREALLOC) { |
| 7345 | free_extent_map(em); |
| 7346 | *map = em = em2; |
| 7347 | } |
| 7348 | |
| 7349 | if (em2 && IS_ERR(em2)) { |
| 7350 | ret = PTR_ERR(em2); |
| 7351 | goto out; |
| 7352 | } |
| 7353 | /* |
| 7354 | * For inode marked NODATACOW or extent marked PREALLOC, |
| 7355 | * use the existing or preallocated extent, so does not |
| 7356 | * need to adjust btrfs_space_info's bytes_may_use. |
| 7357 | */ |
Nikolay Borisov | 9db5d51 | 2020-06-03 08:55:38 +0300 | [diff] [blame] | 7358 | btrfs_free_reserved_data_space_noquota(fs_info, len); |
Nikolay Borisov | c5794e5 | 2018-05-02 15:19:33 +0300 | [diff] [blame] | 7359 | goto skip_cow; |
| 7360 | } |
| 7361 | } |
| 7362 | |
| 7363 | /* this will cow the extent */ |
Nikolay Borisov | c5794e5 | 2018-05-02 15:19:33 +0300 | [diff] [blame] | 7364 | free_extent_map(em); |
Nikolay Borisov | 9fc6f91 | 2020-06-03 08:55:32 +0300 | [diff] [blame] | 7365 | *map = em = btrfs_new_extent_direct(BTRFS_I(inode), start, len); |
Nikolay Borisov | c5794e5 | 2018-05-02 15:19:33 +0300 | [diff] [blame] | 7366 | if (IS_ERR(em)) { |
| 7367 | ret = PTR_ERR(em); |
| 7368 | goto out; |
| 7369 | } |
| 7370 | |
| 7371 | len = min(len, em->len - (start - em->start)); |
| 7372 | |
| 7373 | skip_cow: |
Nikolay Borisov | c5794e5 | 2018-05-02 15:19:33 +0300 | [diff] [blame] | 7374 | /* |
| 7375 | * Need to update the i_size under the extent lock so buffered |
| 7376 | * readers will get the updated i_size when we unlock. |
| 7377 | */ |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 7378 | if (start + len > i_size_read(inode)) |
Nikolay Borisov | c5794e5 | 2018-05-02 15:19:33 +0300 | [diff] [blame] | 7379 | i_size_write(inode, start + len); |
| 7380 | |
Nikolay Borisov | c5794e5 | 2018-05-02 15:19:33 +0300 | [diff] [blame] | 7381 | dio_data->reserve -= len; |
Nikolay Borisov | c5794e5 | 2018-05-02 15:19:33 +0300 | [diff] [blame] | 7382 | out: |
| 7383 | return ret; |
| 7384 | } |
| 7385 | |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 7386 | static int btrfs_dio_iomap_begin(struct inode *inode, loff_t start, |
| 7387 | loff_t length, unsigned int flags, struct iomap *iomap, |
| 7388 | struct iomap *srcmap) |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7389 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7390 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7391 | struct extent_map *em; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7392 | struct extent_state *cached_state = NULL; |
chandan | 50745b0 | 2015-08-28 21:10:13 +0530 | [diff] [blame] | 7393 | struct btrfs_dio_data *dio_data = NULL; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7394 | u64 lockstart, lockend; |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 7395 | const bool write = !!(flags & IOMAP_WRITE); |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 7396 | int ret = 0; |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 7397 | u64 len = length; |
| 7398 | bool unlock_extents = false; |
Josef Bacik | 0eb7929 | 2020-09-03 11:16:51 -0400 | [diff] [blame] | 7399 | bool sync = (current->journal_info == BTRFS_DIO_SYNC_STUB); |
| 7400 | |
| 7401 | /* |
| 7402 | * We used current->journal_info here to see if we were sync, but |
| 7403 | * there's a lot of tests in the enospc machinery to not do flushing if |
| 7404 | * we have a journal_info set, so we need to clear this out and re-set |
| 7405 | * it in iomap_end. |
| 7406 | */ |
| 7407 | ASSERT(current->journal_info == NULL || |
| 7408 | current->journal_info == BTRFS_DIO_SYNC_STUB); |
| 7409 | current->journal_info = NULL; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7410 | |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 7411 | if (!write) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7412 | len = min_t(u64, len, fs_info->sectorsize); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7413 | |
Josef Bacik | c329861 | 2012-08-03 16:49:19 -0400 | [diff] [blame] | 7414 | lockstart = start; |
| 7415 | lockend = start + len - 1; |
| 7416 | |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 7417 | /* |
| 7418 | * The generic stuff only does filemap_write_and_wait_range, which |
| 7419 | * isn't enough if we've written compressed pages to this area, so we |
| 7420 | * need to flush the dirty pages again to make absolutely sure that any |
| 7421 | * outstanding dirty pages are on disk. |
| 7422 | */ |
| 7423 | if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, |
| 7424 | &BTRFS_I(inode)->runtime_flags)) { |
| 7425 | ret = filemap_fdatawrite_range(inode->i_mapping, start, |
| 7426 | start + length - 1); |
| 7427 | if (ret) |
| 7428 | return ret; |
Josef Bacik | e1cbbfa | 2015-03-17 10:52:28 -0400 | [diff] [blame] | 7429 | } |
| 7430 | |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 7431 | dio_data = kzalloc(sizeof(*dio_data), GFP_NOFS); |
| 7432 | if (!dio_data) |
| 7433 | return -ENOMEM; |
| 7434 | |
Josef Bacik | 0eb7929 | 2020-09-03 11:16:51 -0400 | [diff] [blame] | 7435 | dio_data->sync = sync; |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 7436 | dio_data->length = length; |
| 7437 | if (write) { |
| 7438 | dio_data->reserve = round_up(length, fs_info->sectorsize); |
| 7439 | ret = btrfs_delalloc_reserve_space(BTRFS_I(inode), |
| 7440 | &dio_data->data_reserved, |
| 7441 | start, dio_data->reserve); |
| 7442 | if (ret) { |
| 7443 | extent_changeset_free(dio_data->data_reserved); |
| 7444 | kfree(dio_data); |
| 7445 | return ret; |
| 7446 | } |
| 7447 | } |
| 7448 | iomap->private = dio_data; |
| 7449 | |
| 7450 | |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7451 | /* |
| 7452 | * If this errors out it's because we couldn't invalidate pagecache for |
| 7453 | * this range and we need to fallback to buffered. |
| 7454 | */ |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 7455 | if (lock_extent_direct(inode, lockstart, lockend, &cached_state, write)) { |
Filipe Manana | 9c9464c | 2015-11-04 09:52:04 +0000 | [diff] [blame] | 7456 | ret = -ENOTBLK; |
| 7457 | goto err; |
| 7458 | } |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7459 | |
Omar Sandoval | 39b07b5 | 2019-12-02 17:34:23 -0800 | [diff] [blame] | 7460 | em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7461 | if (IS_ERR(em)) { |
| 7462 | ret = PTR_ERR(em); |
| 7463 | goto unlock_err; |
| 7464 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7465 | |
| 7466 | /* |
| 7467 | * Ok for INLINE and COMPRESSED extents we need to fallback on buffered |
| 7468 | * io. INLINE is special, and we could probably kludge it in here, but |
| 7469 | * it's still buffered so for safety lets just fall back to the generic |
| 7470 | * buffered path. |
| 7471 | * |
| 7472 | * For COMPRESSED we _have_ to read the entire extent in so we can |
| 7473 | * decompress it, so there will be buffering required no matter what we |
| 7474 | * do, so go ahead and fallback to buffered. |
| 7475 | * |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 7476 | * 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] | 7477 | * to buffered IO. Don't blame me, this is the price we pay for using |
| 7478 | * the generic code. |
| 7479 | */ |
| 7480 | if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) || |
| 7481 | em->block_start == EXTENT_MAP_INLINE) { |
| 7482 | free_extent_map(em); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7483 | ret = -ENOTBLK; |
| 7484 | goto unlock_err; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7485 | } |
| 7486 | |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 7487 | len = min(len, em->len - (start - em->start)); |
| 7488 | if (write) { |
| 7489 | ret = btrfs_get_blocks_direct_write(&em, inode, dio_data, |
| 7490 | start, len); |
Nikolay Borisov | c5794e5 | 2018-05-02 15:19:33 +0300 | [diff] [blame] | 7491 | if (ret < 0) |
| 7492 | goto unlock_err; |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 7493 | unlock_extents = true; |
| 7494 | /* Recalc len in case the new em is smaller than requested */ |
| 7495 | len = min(len, em->len - (start - em->start)); |
Nikolay Borisov | c5794e5 | 2018-05-02 15:19:33 +0300 | [diff] [blame] | 7496 | } else { |
Nikolay Borisov | 1c8d017 | 2018-05-02 15:19:32 +0300 | [diff] [blame] | 7497 | /* |
| 7498 | * We need to unlock only the end area that we aren't using. |
| 7499 | * The rest is going to be unlocked by the endio routine. |
| 7500 | */ |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 7501 | lockstart = start + len; |
| 7502 | if (lockstart < lockend) |
| 7503 | unlock_extents = true; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7504 | } |
| 7505 | |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 7506 | if (unlock_extents) |
| 7507 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, |
| 7508 | lockstart, lockend, &cached_state); |
| 7509 | else |
| 7510 | free_extent_state(cached_state); |
| 7511 | |
| 7512 | /* |
| 7513 | * Translate extent map information to iomap. |
| 7514 | * We trim the extents (and move the addr) even though iomap code does |
| 7515 | * that, since we have locked only the parts we are performing I/O in. |
| 7516 | */ |
| 7517 | if ((em->block_start == EXTENT_MAP_HOLE) || |
| 7518 | (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) && !write)) { |
| 7519 | iomap->addr = IOMAP_NULL_ADDR; |
| 7520 | iomap->type = IOMAP_HOLE; |
| 7521 | } else { |
| 7522 | iomap->addr = em->block_start + (start - em->start); |
| 7523 | iomap->type = IOMAP_MAPPED; |
| 7524 | } |
| 7525 | iomap->offset = start; |
| 7526 | iomap->bdev = fs_info->fs_devices->latest_bdev; |
| 7527 | iomap->length = len; |
| 7528 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7529 | free_extent_map(em); |
| 7530 | |
| 7531 | return 0; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7532 | |
| 7533 | unlock_err: |
Omar Sandoval | e182163 | 2019-08-15 14:04:04 -0700 | [diff] [blame] | 7534 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend, |
| 7535 | &cached_state); |
Filipe Manana | 9c9464c | 2015-11-04 09:52:04 +0000 | [diff] [blame] | 7536 | err: |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 7537 | if (dio_data) { |
| 7538 | btrfs_delalloc_release_space(BTRFS_I(inode), |
| 7539 | dio_data->data_reserved, start, |
| 7540 | dio_data->reserve, true); |
| 7541 | btrfs_delalloc_release_extents(BTRFS_I(inode), dio_data->reserve); |
| 7542 | extent_changeset_free(dio_data->data_reserved); |
| 7543 | kfree(dio_data); |
| 7544 | } |
| 7545 | return ret; |
| 7546 | } |
| 7547 | |
| 7548 | static int btrfs_dio_iomap_end(struct inode *inode, loff_t pos, loff_t length, |
| 7549 | ssize_t written, unsigned int flags, struct iomap *iomap) |
| 7550 | { |
| 7551 | int ret = 0; |
| 7552 | struct btrfs_dio_data *dio_data = iomap->private; |
| 7553 | size_t submitted = dio_data->submitted; |
| 7554 | const bool write = !!(flags & IOMAP_WRITE); |
| 7555 | |
| 7556 | if (!write && (iomap->type == IOMAP_HOLE)) { |
| 7557 | /* If reading from a hole, unlock and return */ |
| 7558 | unlock_extent(&BTRFS_I(inode)->io_tree, pos, pos + length - 1); |
| 7559 | goto out; |
| 7560 | } |
| 7561 | |
| 7562 | if (submitted < length) { |
| 7563 | pos += submitted; |
| 7564 | length -= submitted; |
| 7565 | if (write) |
| 7566 | __endio_write_update_ordered(BTRFS_I(inode), pos, |
| 7567 | length, false); |
| 7568 | else |
| 7569 | unlock_extent(&BTRFS_I(inode)->io_tree, pos, |
| 7570 | pos + length - 1); |
| 7571 | ret = -ENOTBLK; |
| 7572 | } |
| 7573 | |
| 7574 | if (write) { |
| 7575 | if (dio_data->reserve) |
| 7576 | btrfs_delalloc_release_space(BTRFS_I(inode), |
| 7577 | dio_data->data_reserved, pos, |
| 7578 | dio_data->reserve, true); |
| 7579 | btrfs_delalloc_release_extents(BTRFS_I(inode), dio_data->length); |
| 7580 | extent_changeset_free(dio_data->data_reserved); |
| 7581 | } |
| 7582 | out: |
Josef Bacik | 0eb7929 | 2020-09-03 11:16:51 -0400 | [diff] [blame] | 7583 | /* |
| 7584 | * We're all done, we can re-set the current->journal_info now safely |
| 7585 | * for our endio. |
| 7586 | */ |
| 7587 | if (dio_data->sync) { |
| 7588 | ASSERT(current->journal_info == NULL); |
| 7589 | current->journal_info = BTRFS_DIO_SYNC_STUB; |
| 7590 | } |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 7591 | kfree(dio_data); |
| 7592 | iomap->private = NULL; |
| 7593 | |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7594 | return ret; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7595 | } |
| 7596 | |
Omar Sandoval | 769b4f2 | 2020-04-16 14:46:22 -0700 | [diff] [blame] | 7597 | static void btrfs_dio_private_put(struct btrfs_dio_private *dip) |
| 7598 | { |
| 7599 | /* |
| 7600 | * This implies a barrier so that stores to dio_bio->bi_status before |
| 7601 | * this and loads of dio_bio->bi_status after this are fully ordered. |
| 7602 | */ |
| 7603 | if (!refcount_dec_and_test(&dip->refs)) |
| 7604 | return; |
| 7605 | |
| 7606 | if (bio_op(dip->dio_bio) == REQ_OP_WRITE) { |
Nikolay Borisov | b672b5c | 2020-06-03 08:55:24 +0300 | [diff] [blame] | 7607 | __endio_write_update_ordered(BTRFS_I(dip->inode), |
| 7608 | dip->logical_offset, |
Omar Sandoval | 769b4f2 | 2020-04-16 14:46:22 -0700 | [diff] [blame] | 7609 | dip->bytes, |
| 7610 | !dip->dio_bio->bi_status); |
| 7611 | } else { |
| 7612 | unlock_extent(&BTRFS_I(dip->inode)->io_tree, |
| 7613 | dip->logical_offset, |
| 7614 | dip->logical_offset + dip->bytes - 1); |
| 7615 | } |
| 7616 | |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 7617 | bio_endio(dip->dio_bio); |
Omar Sandoval | 769b4f2 | 2020-04-16 14:46:22 -0700 | [diff] [blame] | 7618 | kfree(dip); |
| 7619 | } |
| 7620 | |
Omar Sandoval | 77d5d68 | 2020-04-16 14:46:25 -0700 | [diff] [blame] | 7621 | static blk_status_t submit_dio_repair_bio(struct inode *inode, struct bio *bio, |
| 7622 | int mirror_num, |
| 7623 | unsigned long bio_flags) |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7624 | { |
Omar Sandoval | 77d5d68 | 2020-04-16 14:46:25 -0700 | [diff] [blame] | 7625 | struct btrfs_dio_private *dip = bio->bi_private; |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 7626 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Omar Sandoval | 58efbc9 | 2017-08-22 23:45:59 -0700 | [diff] [blame] | 7627 | blk_status_t ret; |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7628 | |
Mike Christie | 37226b2 | 2016-06-05 14:31:52 -0500 | [diff] [blame] | 7629 | BUG_ON(bio_op(bio) == REQ_OP_WRITE); |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7630 | |
Omar Sandoval | 5c047a6 | 2020-04-16 14:46:24 -0700 | [diff] [blame] | 7631 | 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] | 7632 | if (ret) |
Nikolay Borisov | ea057f6 | 2017-12-13 10:25:38 +0200 | [diff] [blame] | 7633 | return ret; |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7634 | |
Omar Sandoval | fd9d667 | 2020-04-16 14:46:23 -0700 | [diff] [blame] | 7635 | refcount_inc(&dip->refs); |
Chris Mason | 08635ba | 2019-07-10 12:28:14 -0700 | [diff] [blame] | 7636 | ret = btrfs_map_bio(fs_info, bio, mirror_num); |
Omar Sandoval | 77d5d68 | 2020-04-16 14:46:25 -0700 | [diff] [blame] | 7637 | if (ret) |
Omar Sandoval | fd9d667 | 2020-04-16 14:46:23 -0700 | [diff] [blame] | 7638 | refcount_dec(&dip->refs); |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7639 | return ret; |
| 7640 | } |
| 7641 | |
Omar Sandoval | 769b4f2 | 2020-04-16 14:46:22 -0700 | [diff] [blame] | 7642 | static blk_status_t btrfs_check_read_dio_bio(struct inode *inode, |
| 7643 | struct btrfs_io_bio *io_bio, |
Omar Sandoval | fd9d667 | 2020-04-16 14:46:23 -0700 | [diff] [blame] | 7644 | const bool uptodate) |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7645 | { |
Omar Sandoval | fd9d667 | 2020-04-16 14:46:23 -0700 | [diff] [blame] | 7646 | struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info; |
| 7647 | const u32 sectorsize = fs_info->sectorsize; |
Josef Bacik | 7870d08 | 2017-05-05 11:57:15 -0400 | [diff] [blame] | 7648 | struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree; |
Omar Sandoval | fd9d667 | 2020-04-16 14:46:23 -0700 | [diff] [blame] | 7649 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
| 7650 | const bool csum = !(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM); |
Liu Bo | 17347ce | 2017-05-15 15:33:27 -0700 | [diff] [blame] | 7651 | struct bio_vec bvec; |
| 7652 | struct bvec_iter iter; |
Omar Sandoval | fd9d667 | 2020-04-16 14:46:23 -0700 | [diff] [blame] | 7653 | u64 start = io_bio->logical; |
| 7654 | int icsum = 0; |
Omar Sandoval | 58efbc9 | 2017-08-22 23:45:59 -0700 | [diff] [blame] | 7655 | blk_status_t err = BLK_STS_OK; |
Miao Xie | dc380ae | 2014-09-12 18:43:55 +0800 | [diff] [blame] | 7656 | |
Omar Sandoval | fd9d667 | 2020-04-16 14:46:23 -0700 | [diff] [blame] | 7657 | __bio_for_each_segment(bvec, &io_bio->bio, iter, io_bio->iter) { |
| 7658 | unsigned int i, nr_sectors, pgoff; |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 7659 | |
Liu Bo | 17347ce | 2017-05-15 15:33:27 -0700 | [diff] [blame] | 7660 | nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec.bv_len); |
| 7661 | pgoff = bvec.bv_offset; |
Omar Sandoval | fd9d667 | 2020-04-16 14:46:23 -0700 | [diff] [blame] | 7662 | for (i = 0; i < nr_sectors; i++) { |
Liu Bo | 97bf5a5 | 2017-04-07 13:11:10 -0700 | [diff] [blame] | 7663 | ASSERT(pgoff < PAGE_SIZE); |
Omar Sandoval | fd9d667 | 2020-04-16 14:46:23 -0700 | [diff] [blame] | 7664 | if (uptodate && |
| 7665 | (!csum || !check_data_csum(inode, io_bio, icsum, |
| 7666 | bvec.bv_page, pgoff, |
| 7667 | start, sectorsize))) { |
| 7668 | clean_io_failure(fs_info, failure_tree, io_tree, |
| 7669 | start, bvec.bv_page, |
| 7670 | btrfs_ino(BTRFS_I(inode)), |
| 7671 | pgoff); |
| 7672 | } else { |
| 7673 | blk_status_t status; |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7674 | |
Omar Sandoval | 77d5d68 | 2020-04-16 14:46:25 -0700 | [diff] [blame] | 7675 | status = btrfs_submit_read_repair(inode, |
| 7676 | &io_bio->bio, |
| 7677 | start - io_bio->logical, |
Omar Sandoval | fd9d667 | 2020-04-16 14:46:23 -0700 | [diff] [blame] | 7678 | bvec.bv_page, pgoff, |
| 7679 | start, |
| 7680 | start + sectorsize - 1, |
Omar Sandoval | 77d5d68 | 2020-04-16 14:46:25 -0700 | [diff] [blame] | 7681 | io_bio->mirror_num, |
| 7682 | submit_dio_repair_bio); |
Omar Sandoval | fd9d667 | 2020-04-16 14:46:23 -0700 | [diff] [blame] | 7683 | if (status) |
| 7684 | err = status; |
| 7685 | } |
| 7686 | start += sectorsize; |
| 7687 | icsum++; |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 7688 | pgoff += sectorsize; |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 7689 | } |
Kent Overstreet | 2c30c71 | 2013-11-07 12:20:26 -0800 | [diff] [blame] | 7690 | } |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 7691 | return err; |
| 7692 | } |
| 7693 | |
Nikolay Borisov | b672b5c | 2020-06-03 08:55:24 +0300 | [diff] [blame] | 7694 | static void __endio_write_update_ordered(struct btrfs_inode *inode, |
Qu Wenruo | 52427260 | 2017-03-08 10:25:52 +0800 | [diff] [blame] | 7695 | const u64 offset, const u64 bytes, |
| 7696 | const bool uptodate) |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7697 | { |
Nikolay Borisov | b672b5c | 2020-06-03 08:55:24 +0300 | [diff] [blame] | 7698 | struct btrfs_fs_info *fs_info = inode->root->fs_info; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7699 | struct btrfs_ordered_extent *ordered = NULL; |
Qu Wenruo | 52427260 | 2017-03-08 10:25:52 +0800 | [diff] [blame] | 7700 | struct btrfs_workqueue *wq; |
Filipe Manana | 1454377 | 2015-11-24 16:23:54 +0000 | [diff] [blame] | 7701 | u64 ordered_offset = offset; |
| 7702 | u64 ordered_bytes = bytes; |
Naohiro Aota | 67c003f | 2017-09-01 17:59:07 +0900 | [diff] [blame] | 7703 | u64 last_offset; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7704 | |
Nikolay Borisov | b672b5c | 2020-06-03 08:55:24 +0300 | [diff] [blame] | 7705 | if (btrfs_is_free_space_inode(inode)) |
Qu Wenruo | 52427260 | 2017-03-08 10:25:52 +0800 | [diff] [blame] | 7706 | wq = fs_info->endio_freespace_worker; |
Omar Sandoval | a0cac0e | 2019-09-16 11:30:57 -0700 | [diff] [blame] | 7707 | else |
Qu Wenruo | 52427260 | 2017-03-08 10:25:52 +0800 | [diff] [blame] | 7708 | wq = fs_info->endio_write_workers; |
Qu Wenruo | 52427260 | 2017-03-08 10:25:52 +0800 | [diff] [blame] | 7709 | |
Nikolay Borisov | b25f0d0 | 2018-04-11 11:21:17 +0300 | [diff] [blame] | 7710 | while (ordered_offset < offset + bytes) { |
| 7711 | last_offset = ordered_offset; |
Nikolay Borisov | b672b5c | 2020-06-03 08:55:24 +0300 | [diff] [blame] | 7712 | if (btrfs_dec_test_first_ordered_pending(inode, &ordered, |
Nikolay Borisov | 7095821 | 2020-06-03 08:55:23 +0300 | [diff] [blame] | 7713 | &ordered_offset, |
| 7714 | ordered_bytes, |
| 7715 | uptodate)) { |
Omar Sandoval | a0cac0e | 2019-09-16 11:30:57 -0700 | [diff] [blame] | 7716 | btrfs_init_work(&ordered->work, finish_ordered_fn, NULL, |
| 7717 | NULL); |
Nikolay Borisov | b25f0d0 | 2018-04-11 11:21:17 +0300 | [diff] [blame] | 7718 | btrfs_queue_work(wq, &ordered->work); |
| 7719 | } |
| 7720 | /* |
| 7721 | * If btrfs_dec_test_ordered_pending does not find any ordered |
| 7722 | * extent in the range, we can exit. |
| 7723 | */ |
| 7724 | if (ordered_offset == last_offset) |
| 7725 | return; |
| 7726 | /* |
| 7727 | * Our bio might span multiple ordered extents. In this case |
Andrea Gelmini | 52042d8 | 2018-11-28 12:05:13 +0100 | [diff] [blame] | 7728 | * we keep going until we have accounted the whole dio. |
Nikolay Borisov | b25f0d0 | 2018-04-11 11:21:17 +0300 | [diff] [blame] | 7729 | */ |
| 7730 | if (ordered_offset < offset + bytes) { |
| 7731 | ordered_bytes = offset + bytes - ordered_offset; |
| 7732 | ordered = NULL; |
| 7733 | } |
Chris Mason | 163cf09 | 2010-11-28 19:56:33 -0500 | [diff] [blame] | 7734 | } |
Filipe Manana | 1454377 | 2015-11-24 16:23:54 +0000 | [diff] [blame] | 7735 | } |
| 7736 | |
David Sterba | d0ee393 | 2018-03-08 14:35:48 +0100 | [diff] [blame] | 7737 | static blk_status_t btrfs_submit_bio_start_direct_io(void *private_data, |
David Sterba | d077929 | 2018-03-08 13:47:33 +0100 | [diff] [blame] | 7738 | struct bio *bio, u64 offset) |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 7739 | { |
Josef Bacik | c6100a4 | 2017-05-05 11:57:13 -0400 | [diff] [blame] | 7740 | struct inode *inode = private_data; |
Johannes Thumshirn | c965d64 | 2020-07-28 20:25:41 +0900 | [diff] [blame] | 7741 | |
| 7742 | return btrfs_csum_one_bio(BTRFS_I(inode), bio, offset, 1); |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 7743 | } |
| 7744 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 7745 | static void btrfs_end_dio_bio(struct bio *bio) |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7746 | { |
| 7747 | struct btrfs_dio_private *dip = bio->bi_private; |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 7748 | blk_status_t err = bio->bi_status; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7749 | |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7750 | if (err) |
| 7751 | btrfs_warn(BTRFS_I(dip->inode)->root->fs_info, |
Mike Christie | 6296b96 | 2016-06-05 14:32:21 -0500 | [diff] [blame] | 7752 | "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] | 7753 | btrfs_ino(BTRFS_I(dip->inode)), bio_op(bio), |
| 7754 | bio->bi_opf, |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7755 | (unsigned long long)bio->bi_iter.bi_sector, |
| 7756 | bio->bi_iter.bi_size, err); |
| 7757 | |
Omar Sandoval | 769b4f2 | 2020-04-16 14:46:22 -0700 | [diff] [blame] | 7758 | if (bio_op(bio) == REQ_OP_READ) { |
| 7759 | err = btrfs_check_read_dio_bio(dip->inode, btrfs_io_bio(bio), |
Omar Sandoval | fd9d667 | 2020-04-16 14:46:23 -0700 | [diff] [blame] | 7760 | !err); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7761 | } |
| 7762 | |
Omar Sandoval | 769b4f2 | 2020-04-16 14:46:22 -0700 | [diff] [blame] | 7763 | if (err) |
| 7764 | dip->dio_bio->bi_status = err; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7765 | |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7766 | bio_put(bio); |
Omar Sandoval | 769b4f2 | 2020-04-16 14:46:22 -0700 | [diff] [blame] | 7767 | btrfs_dio_private_put(dip); |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 7768 | } |
| 7769 | |
David Sterba | d0ee393 | 2018-03-08 14:35:48 +0100 | [diff] [blame] | 7770 | static inline blk_status_t btrfs_submit_dio_bio(struct bio *bio, |
| 7771 | struct inode *inode, u64 file_offset, int async_submit) |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7772 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7773 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 7774 | struct btrfs_dio_private *dip = bio->bi_private; |
Mike Christie | 37226b2 | 2016-06-05 14:31:52 -0500 | [diff] [blame] | 7775 | bool write = bio_op(bio) == REQ_OP_WRITE; |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 7776 | blk_status_t ret; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7777 | |
Liu Bo | 4c274bc | 2017-11-01 17:19:27 -0600 | [diff] [blame] | 7778 | /* Check btrfs_submit_bio_hook() for rules about async submit. */ |
Josef Bacik | b812ce2 | 2012-11-16 13:56:32 -0500 | [diff] [blame] | 7779 | if (async_submit) |
| 7780 | async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers); |
| 7781 | |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 7782 | if (!write) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7783 | 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] | 7784 | if (ret) |
| 7785 | goto err; |
| 7786 | } |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7787 | |
Nikolay Borisov | e6961ca | 2017-08-03 15:44:58 +0300 | [diff] [blame] | 7788 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM) |
Josef Bacik | 1ae3993 | 2011-04-06 14:41:34 -0400 | [diff] [blame] | 7789 | goto map; |
| 7790 | |
| 7791 | if (write && async_submit) { |
Josef Bacik | c6100a4 | 2017-05-05 11:57:13 -0400 | [diff] [blame] | 7792 | ret = btrfs_wq_submit_bio(fs_info, bio, 0, 0, |
| 7793 | file_offset, inode, |
David Sterba | e288c08 | 2018-07-18 17:36:24 +0200 | [diff] [blame] | 7794 | btrfs_submit_bio_start_direct_io); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7795 | goto err; |
Josef Bacik | 1ae3993 | 2011-04-06 14:41:34 -0400 | [diff] [blame] | 7796 | } else if (write) { |
| 7797 | /* |
| 7798 | * If we aren't doing async submit, calculate the csum of the |
| 7799 | * bio now. |
| 7800 | */ |
Nikolay Borisov | bd242a0 | 2020-06-03 08:55:07 +0300 | [diff] [blame] | 7801 | ret = btrfs_csum_one_bio(BTRFS_I(inode), bio, file_offset, 1); |
Josef Bacik | 1ae3993 | 2011-04-06 14:41:34 -0400 | [diff] [blame] | 7802 | if (ret) |
| 7803 | goto err; |
Miao Xie | 23ea8e5 | 2014-09-12 18:43:54 +0800 | [diff] [blame] | 7804 | } else { |
Omar Sandoval | 8587957 | 2020-04-16 14:46:21 -0700 | [diff] [blame] | 7805 | u64 csum_offset; |
| 7806 | |
| 7807 | csum_offset = file_offset - dip->logical_offset; |
| 7808 | csum_offset >>= inode->i_sb->s_blocksize_bits; |
| 7809 | csum_offset *= btrfs_super_csum_size(fs_info->super_copy); |
| 7810 | btrfs_io_bio(bio)->csum = dip->csums + csum_offset; |
Tsutomu Itoh | c2db107 | 2011-03-01 06:48:31 +0000 | [diff] [blame] | 7811 | } |
Josef Bacik | 1ae3993 | 2011-04-06 14:41:34 -0400 | [diff] [blame] | 7812 | map: |
Chris Mason | 08635ba | 2019-07-10 12:28:14 -0700 | [diff] [blame] | 7813 | ret = btrfs_map_bio(fs_info, bio, 0); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7814 | err: |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7815 | return ret; |
| 7816 | } |
| 7817 | |
Omar Sandoval | c36cac2 | 2020-04-16 14:46:13 -0700 | [diff] [blame] | 7818 | /* |
| 7819 | * If this succeeds, the btrfs_dio_private is responsible for cleaning up locked |
| 7820 | * or ordered extents whether or not we submit any bios. |
| 7821 | */ |
| 7822 | static struct btrfs_dio_private *btrfs_create_dio_private(struct bio *dio_bio, |
| 7823 | struct inode *inode, |
| 7824 | loff_t file_offset) |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7825 | { |
Omar Sandoval | c36cac2 | 2020-04-16 14:46:13 -0700 | [diff] [blame] | 7826 | const bool write = (bio_op(dio_bio) == REQ_OP_WRITE); |
Omar Sandoval | 8587957 | 2020-04-16 14:46:21 -0700 | [diff] [blame] | 7827 | const bool csum = !(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM); |
| 7828 | size_t dip_size; |
Omar Sandoval | c36cac2 | 2020-04-16 14:46:13 -0700 | [diff] [blame] | 7829 | struct btrfs_dio_private *dip; |
Omar Sandoval | c36cac2 | 2020-04-16 14:46:13 -0700 | [diff] [blame] | 7830 | |
Omar Sandoval | 8587957 | 2020-04-16 14:46:21 -0700 | [diff] [blame] | 7831 | dip_size = sizeof(*dip); |
| 7832 | if (!write && csum) { |
| 7833 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
| 7834 | const u16 csum_size = btrfs_super_csum_size(fs_info->super_copy); |
| 7835 | size_t nblocks; |
| 7836 | |
| 7837 | nblocks = dio_bio->bi_iter.bi_size >> inode->i_sb->s_blocksize_bits; |
| 7838 | dip_size += csum_size * nblocks; |
| 7839 | } |
| 7840 | |
| 7841 | dip = kzalloc(dip_size, GFP_NOFS); |
Omar Sandoval | c36cac2 | 2020-04-16 14:46:13 -0700 | [diff] [blame] | 7842 | if (!dip) |
| 7843 | return NULL; |
| 7844 | |
Omar Sandoval | c36cac2 | 2020-04-16 14:46:13 -0700 | [diff] [blame] | 7845 | dip->inode = inode; |
| 7846 | dip->logical_offset = file_offset; |
| 7847 | dip->bytes = dio_bio->bi_iter.bi_size; |
| 7848 | dip->disk_bytenr = (u64)dio_bio->bi_iter.bi_sector << 9; |
Omar Sandoval | c36cac2 | 2020-04-16 14:46:13 -0700 | [diff] [blame] | 7849 | dip->dio_bio = dio_bio; |
Omar Sandoval | e3b318d | 2020-04-16 14:46:20 -0700 | [diff] [blame] | 7850 | refcount_set(&dip->refs, 1); |
Omar Sandoval | c36cac2 | 2020-04-16 14:46:13 -0700 | [diff] [blame] | 7851 | return dip; |
| 7852 | } |
| 7853 | |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 7854 | static blk_qc_t btrfs_submit_direct(struct inode *inode, struct iomap *iomap, |
| 7855 | struct bio *dio_bio, loff_t file_offset) |
Omar Sandoval | c36cac2 | 2020-04-16 14:46:13 -0700 | [diff] [blame] | 7856 | { |
| 7857 | const bool write = (bio_op(dio_bio) == REQ_OP_WRITE); |
Omar Sandoval | 8587957 | 2020-04-16 14:46:21 -0700 | [diff] [blame] | 7858 | const bool csum = !(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7859 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Omar Sandoval | 769b4f2 | 2020-04-16 14:46:22 -0700 | [diff] [blame] | 7860 | const bool raid56 = (btrfs_data_alloc_profile(fs_info) & |
| 7861 | BTRFS_BLOCK_GROUP_RAID56_MASK); |
Omar Sandoval | c36cac2 | 2020-04-16 14:46:13 -0700 | [diff] [blame] | 7862 | struct btrfs_dio_private *dip; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7863 | struct bio *bio; |
Omar Sandoval | c36cac2 | 2020-04-16 14:46:13 -0700 | [diff] [blame] | 7864 | u64 start_sector; |
Josef Bacik | 1ae3993 | 2011-04-06 14:41:34 -0400 | [diff] [blame] | 7865 | int async_submit = 0; |
Liu Bo | 725130b | 2017-05-16 09:51:39 -0700 | [diff] [blame] | 7866 | u64 submit_len; |
| 7867 | int clone_offset = 0; |
| 7868 | int clone_len; |
Chandan Rajendra | 5f4dc8f | 2016-01-21 15:56:00 +0530 | [diff] [blame] | 7869 | int ret; |
Omar Sandoval | 58efbc9 | 2017-08-22 23:45:59 -0700 | [diff] [blame] | 7870 | blk_status_t status; |
Nikolay Borisov | 89b798a | 2019-06-03 12:05:05 +0300 | [diff] [blame] | 7871 | struct btrfs_io_geometry geom; |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 7872 | struct btrfs_dio_data *dio_data = iomap->private; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7873 | |
Omar Sandoval | c36cac2 | 2020-04-16 14:46:13 -0700 | [diff] [blame] | 7874 | dip = btrfs_create_dio_private(dio_bio, inode, file_offset); |
| 7875 | if (!dip) { |
| 7876 | if (!write) { |
| 7877 | unlock_extent(&BTRFS_I(inode)->io_tree, file_offset, |
| 7878 | file_offset + dio_bio->bi_iter.bi_size - 1); |
| 7879 | } |
| 7880 | dio_bio->bi_status = BLK_STS_RESOURCE; |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 7881 | bio_endio(dio_bio); |
| 7882 | return BLK_QC_T_NONE; |
Josef Bacik | 02f57c7 | 2011-04-06 14:25:44 -0400 | [diff] [blame] | 7883 | } |
| 7884 | |
Omar Sandoval | 8587957 | 2020-04-16 14:46:21 -0700 | [diff] [blame] | 7885 | if (!write && csum) { |
| 7886 | /* |
| 7887 | * Load the csums up front to reduce csum tree searches and |
| 7888 | * contention when submitting bios. |
| 7889 | */ |
| 7890 | status = btrfs_lookup_bio_sums(inode, dio_bio, file_offset, |
| 7891 | dip->csums); |
| 7892 | if (status != BLK_STS_OK) |
| 7893 | goto out_err; |
| 7894 | } |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 7895 | |
Omar Sandoval | 769b4f2 | 2020-04-16 14:46:22 -0700 | [diff] [blame] | 7896 | start_sector = dio_bio->bi_iter.bi_sector; |
| 7897 | submit_len = dio_bio->bi_iter.bi_size; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7898 | |
Arnd Bergmann | 3c91ee6 | 2017-05-18 15:33:29 +0200 | [diff] [blame] | 7899 | do { |
Omar Sandoval | 769b4f2 | 2020-04-16 14:46:22 -0700 | [diff] [blame] | 7900 | ret = btrfs_get_io_geometry(fs_info, btrfs_op(dio_bio), |
| 7901 | start_sector << 9, submit_len, |
| 7902 | &geom); |
| 7903 | if (ret) { |
| 7904 | status = errno_to_blk_status(ret); |
| 7905 | goto out_err; |
| 7906 | } |
| 7907 | ASSERT(geom.len <= INT_MAX); |
| 7908 | |
Nikolay Borisov | 89b798a | 2019-06-03 12:05:05 +0300 | [diff] [blame] | 7909 | clone_len = min_t(int, submit_len, geom.len); |
Josef Bacik | 02f57c7 | 2011-04-06 14:25:44 -0400 | [diff] [blame] | 7910 | |
Liu Bo | 725130b | 2017-05-16 09:51:39 -0700 | [diff] [blame] | 7911 | /* |
| 7912 | * This will never fail as it's passing GPF_NOFS and |
| 7913 | * the allocation is backed by btrfs_bioset. |
| 7914 | */ |
Omar Sandoval | 769b4f2 | 2020-04-16 14:46:22 -0700 | [diff] [blame] | 7915 | bio = btrfs_bio_clone_partial(dio_bio, clone_offset, clone_len); |
Liu Bo | 725130b | 2017-05-16 09:51:39 -0700 | [diff] [blame] | 7916 | bio->bi_private = dip; |
| 7917 | bio->bi_end_io = btrfs_end_dio_bio; |
| 7918 | btrfs_io_bio(bio)->logical = file_offset; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7919 | |
Liu Bo | 725130b | 2017-05-16 09:51:39 -0700 | [diff] [blame] | 7920 | ASSERT(submit_len >= clone_len); |
| 7921 | submit_len -= clone_len; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7922 | |
Liu Bo | 725130b | 2017-05-16 09:51:39 -0700 | [diff] [blame] | 7923 | /* |
| 7924 | * Increase the count before we submit the bio so we know |
| 7925 | * the end IO handler won't happen before we increase the |
| 7926 | * count. Otherwise, the dip might get freed before we're |
| 7927 | * done setting it up. |
Omar Sandoval | 769b4f2 | 2020-04-16 14:46:22 -0700 | [diff] [blame] | 7928 | * |
| 7929 | * We transfer the initial reference to the last bio, so we |
| 7930 | * don't need to increment the reference count for the last one. |
Liu Bo | 725130b | 2017-05-16 09:51:39 -0700 | [diff] [blame] | 7931 | */ |
Omar Sandoval | 769b4f2 | 2020-04-16 14:46:22 -0700 | [diff] [blame] | 7932 | if (submit_len > 0) { |
| 7933 | refcount_inc(&dip->refs); |
| 7934 | /* |
| 7935 | * If we are submitting more than one bio, submit them |
| 7936 | * all asynchronously. The exception is RAID 5 or 6, as |
| 7937 | * asynchronous checksums make it difficult to collect |
| 7938 | * full stripe writes. |
| 7939 | */ |
| 7940 | if (!raid56) |
| 7941 | async_submit = 1; |
| 7942 | } |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7943 | |
David Sterba | d0ee393 | 2018-03-08 14:35:48 +0100 | [diff] [blame] | 7944 | status = btrfs_submit_dio_bio(bio, inode, file_offset, |
Omar Sandoval | 58efbc9 | 2017-08-22 23:45:59 -0700 | [diff] [blame] | 7945 | async_submit); |
| 7946 | if (status) { |
Liu Bo | 725130b | 2017-05-16 09:51:39 -0700 | [diff] [blame] | 7947 | bio_put(bio); |
Omar Sandoval | 769b4f2 | 2020-04-16 14:46:22 -0700 | [diff] [blame] | 7948 | if (submit_len > 0) |
| 7949 | refcount_dec(&dip->refs); |
Liu Bo | 725130b | 2017-05-16 09:51:39 -0700 | [diff] [blame] | 7950 | goto out_err; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7951 | } |
Liu Bo | 725130b | 2017-05-16 09:51:39 -0700 | [diff] [blame] | 7952 | |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 7953 | dio_data->submitted += clone_len; |
Liu Bo | 725130b | 2017-05-16 09:51:39 -0700 | [diff] [blame] | 7954 | clone_offset += clone_len; |
| 7955 | start_sector += clone_len >> 9; |
| 7956 | file_offset += clone_len; |
Arnd Bergmann | 3c91ee6 | 2017-05-18 15:33:29 +0200 | [diff] [blame] | 7957 | } while (submit_len > 0); |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 7958 | return BLK_QC_T_NONE; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7959 | |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7960 | out_err: |
Omar Sandoval | 769b4f2 | 2020-04-16 14:46:22 -0700 | [diff] [blame] | 7961 | dip->dio_bio->bi_status = status; |
| 7962 | btrfs_dio_private_put(dip); |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 7963 | return BLK_QC_T_NONE; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7964 | } |
| 7965 | |
David Sterba | f4c48b4 | 2020-06-09 19:19:27 +0200 | [diff] [blame] | 7966 | static ssize_t check_direct_IO(struct btrfs_fs_info *fs_info, |
| 7967 | const struct iov_iter *iter, loff_t offset) |
| 7968 | { |
| 7969 | int seg; |
| 7970 | int i; |
| 7971 | unsigned int blocksize_mask = fs_info->sectorsize - 1; |
| 7972 | ssize_t retval = -EINVAL; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7973 | |
David Sterba | f4c48b4 | 2020-06-09 19:19:27 +0200 | [diff] [blame] | 7974 | if (offset & blocksize_mask) |
| 7975 | goto out; |
| 7976 | |
| 7977 | if (iov_iter_alignment(iter) & blocksize_mask) |
| 7978 | goto out; |
| 7979 | |
| 7980 | /* If this is a write we don't need to check anymore */ |
| 7981 | if (iov_iter_rw(iter) != READ || !iter_is_iovec(iter)) |
| 7982 | return 0; |
| 7983 | /* |
| 7984 | * Check to make sure we don't have duplicate iov_base's in this |
| 7985 | * iovec, if so return EINVAL, otherwise we'll get csum errors |
| 7986 | * when reading back. |
| 7987 | */ |
| 7988 | for (seg = 0; seg < iter->nr_segs; seg++) { |
| 7989 | for (i = seg + 1; i < iter->nr_segs; i++) { |
| 7990 | if (iter->iov[seg].iov_base == iter->iov[i].iov_base) |
| 7991 | goto out; |
| 7992 | } |
| 7993 | } |
| 7994 | retval = 0; |
| 7995 | out: |
| 7996 | return retval; |
| 7997 | } |
| 7998 | |
Josef Bacik | 0eb7929 | 2020-09-03 11:16:51 -0400 | [diff] [blame] | 7999 | static inline int btrfs_maybe_fsync_end_io(struct kiocb *iocb, ssize_t size, |
| 8000 | int error, unsigned flags) |
| 8001 | { |
| 8002 | /* |
| 8003 | * Now if we're still in the context of our submitter we know we can't |
| 8004 | * safely run generic_write_sync(), so clear our flag here so that the |
| 8005 | * caller knows to follow up with a sync. |
| 8006 | */ |
| 8007 | if (current->journal_info == BTRFS_DIO_SYNC_STUB) { |
| 8008 | current->journal_info = NULL; |
| 8009 | return error; |
| 8010 | } |
| 8011 | |
| 8012 | if (error) |
| 8013 | return error; |
| 8014 | |
| 8015 | if (size) { |
| 8016 | iocb->ki_flags |= IOCB_DSYNC; |
| 8017 | return generic_write_sync(iocb, size); |
| 8018 | } |
| 8019 | |
| 8020 | return 0; |
| 8021 | } |
| 8022 | |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 8023 | static const struct iomap_ops btrfs_dio_iomap_ops = { |
| 8024 | .iomap_begin = btrfs_dio_iomap_begin, |
| 8025 | .iomap_end = btrfs_dio_iomap_end, |
| 8026 | }; |
| 8027 | |
| 8028 | static const struct iomap_dio_ops btrfs_dio_ops = { |
| 8029 | .submit_io = btrfs_submit_direct, |
| 8030 | }; |
| 8031 | |
Josef Bacik | 0eb7929 | 2020-09-03 11:16:51 -0400 | [diff] [blame] | 8032 | static const struct iomap_dio_ops btrfs_sync_dops = { |
| 8033 | .submit_io = btrfs_submit_direct, |
| 8034 | .end_io = btrfs_maybe_fsync_end_io, |
| 8035 | }; |
| 8036 | |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 8037 | ssize_t btrfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter) |
David Sterba | f4c48b4 | 2020-06-09 19:19:27 +0200 | [diff] [blame] | 8038 | { |
| 8039 | struct file *file = iocb->ki_filp; |
| 8040 | struct inode *inode = file->f_mapping->host; |
| 8041 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
| 8042 | struct extent_changeset *data_reserved = NULL; |
| 8043 | loff_t offset = iocb->ki_pos; |
| 8044 | size_t count = 0; |
| 8045 | bool relock = false; |
| 8046 | ssize_t ret; |
| 8047 | |
Filipe Manana | a6703c7 | 2021-02-16 14:40:31 +0000 | [diff] [blame] | 8048 | if (check_direct_IO(fs_info, iter, offset)) { |
| 8049 | ASSERT(current->journal_info == NULL || |
| 8050 | current->journal_info == BTRFS_DIO_SYNC_STUB); |
| 8051 | current->journal_info = NULL; |
David Sterba | f4c48b4 | 2020-06-09 19:19:27 +0200 | [diff] [blame] | 8052 | return 0; |
Filipe Manana | a6703c7 | 2021-02-16 14:40:31 +0000 | [diff] [blame] | 8053 | } |
David Sterba | f4c48b4 | 2020-06-09 19:19:27 +0200 | [diff] [blame] | 8054 | |
| 8055 | count = iov_iter_count(iter); |
| 8056 | if (iov_iter_rw(iter) == WRITE) { |
| 8057 | /* |
| 8058 | * If the write DIO is beyond the EOF, we need update |
| 8059 | * the isize, but it is protected by i_mutex. So we can |
| 8060 | * not unlock the i_mutex at this case. |
| 8061 | */ |
| 8062 | if (offset + count <= inode->i_size) { |
| 8063 | inode_unlock(inode); |
| 8064 | relock = true; |
David Sterba | f4c48b4 | 2020-06-09 19:19:27 +0200 | [diff] [blame] | 8065 | } |
| 8066 | down_read(&BTRFS_I(inode)->dio_sem); |
| 8067 | } |
| 8068 | |
Josef Bacik | 0eb7929 | 2020-09-03 11:16:51 -0400 | [diff] [blame] | 8069 | /* |
| 8070 | * We have are actually a sync iocb, so we need our fancy endio to know |
| 8071 | * if we need to sync. |
| 8072 | */ |
| 8073 | if (current->journal_info) |
| 8074 | ret = iomap_dio_rw(iocb, iter, &btrfs_dio_iomap_ops, |
| 8075 | &btrfs_sync_dops, is_sync_kiocb(iocb)); |
| 8076 | else |
| 8077 | ret = iomap_dio_rw(iocb, iter, &btrfs_dio_iomap_ops, |
| 8078 | &btrfs_dio_ops, is_sync_kiocb(iocb)); |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 8079 | |
| 8080 | if (ret == -ENOTBLK) |
| 8081 | ret = 0; |
| 8082 | |
| 8083 | if (iov_iter_rw(iter) == WRITE) |
David Sterba | f4c48b4 | 2020-06-09 19:19:27 +0200 | [diff] [blame] | 8084 | up_read(&BTRFS_I(inode)->dio_sem); |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 8085 | |
David Sterba | f4c48b4 | 2020-06-09 19:19:27 +0200 | [diff] [blame] | 8086 | if (relock) |
| 8087 | inode_lock(inode); |
David Sterba | 55e20bd | 2020-06-09 19:56:06 +0200 | [diff] [blame] | 8088 | |
David Sterba | f4c48b4 | 2020-06-09 19:19:27 +0200 | [diff] [blame] | 8089 | extent_changeset_free(data_reserved); |
| 8090 | return ret; |
| 8091 | } |
Chris Mason | 1643298 | 2008-04-10 10:23:21 -0400 | [diff] [blame] | 8092 | |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 8093 | static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, |
David Sterba | bab16e2 | 2020-06-23 20:56:12 +0200 | [diff] [blame] | 8094 | u64 start, u64 len) |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 8095 | { |
Tsutomu Itoh | 05dadc0 | 2012-11-29 05:08:26 +0000 | [diff] [blame] | 8096 | int ret; |
| 8097 | |
Christoph Hellwig | 45dd052 | 2020-05-23 09:30:14 +0200 | [diff] [blame] | 8098 | ret = fiemap_prep(inode, fieinfo, start, &len, 0); |
Tsutomu Itoh | 05dadc0 | 2012-11-29 05:08:26 +0000 | [diff] [blame] | 8099 | if (ret) |
| 8100 | return ret; |
| 8101 | |
Nikolay Borisov | facee0a | 2020-08-31 14:42:49 +0300 | [diff] [blame] | 8102 | return extent_fiemap(BTRFS_I(inode), fieinfo, start, len); |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 8103 | } |
| 8104 | |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8105 | int btrfs_readpage(struct file *file, struct page *page) |
| 8106 | { |
Nikolay Borisov | 0f20881 | 2020-09-14 14:39:16 +0300 | [diff] [blame] | 8107 | struct btrfs_inode *inode = BTRFS_I(page->mapping->host); |
| 8108 | u64 start = page_offset(page); |
| 8109 | u64 end = start + PAGE_SIZE - 1; |
Nikolay Borisov | c1be9c1 | 2020-09-14 12:37:08 +0300 | [diff] [blame] | 8110 | unsigned long bio_flags = 0; |
Nikolay Borisov | 0f20881 | 2020-09-14 14:39:16 +0300 | [diff] [blame] | 8111 | struct bio *bio = NULL; |
Nikolay Borisov | c1be9c1 | 2020-09-14 12:37:08 +0300 | [diff] [blame] | 8112 | int ret; |
| 8113 | |
Nikolay Borisov | 0f20881 | 2020-09-14 14:39:16 +0300 | [diff] [blame] | 8114 | btrfs_lock_and_flush_ordered_range(inode, start, end, NULL); |
| 8115 | |
| 8116 | ret = btrfs_do_readpage(page, NULL, &bio, &bio_flags, 0, NULL); |
Nikolay Borisov | c1be9c1 | 2020-09-14 12:37:08 +0300 | [diff] [blame] | 8117 | if (bio) |
| 8118 | ret = submit_one_bio(bio, 0, bio_flags); |
| 8119 | return ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8120 | } |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 8121 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8122 | static int btrfs_writepage(struct page *page, struct writeback_control *wbc) |
| 8123 | { |
Josef Bacik | be7bd73 | 2015-10-22 15:05:09 -0400 | [diff] [blame] | 8124 | struct inode *inode = page->mapping->host; |
| 8125 | int ret; |
Chris Mason | b888db2b | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8126 | |
| 8127 | if (current->flags & PF_MEMALLOC) { |
| 8128 | redirty_page_for_writepage(wbc, page); |
| 8129 | unlock_page(page); |
| 8130 | return 0; |
| 8131 | } |
Josef Bacik | be7bd73 | 2015-10-22 15:05:09 -0400 | [diff] [blame] | 8132 | |
| 8133 | /* |
| 8134 | * If we are under memory pressure we will call this directly from the |
| 8135 | * VM, we need to make sure we have the inode referenced for the ordered |
| 8136 | * extent. If not just return like we didn't do anything. |
| 8137 | */ |
| 8138 | if (!igrab(inode)) { |
| 8139 | redirty_page_for_writepage(wbc, page); |
| 8140 | return AOP_WRITEPAGE_ACTIVATE; |
| 8141 | } |
Nikolay Borisov | 0a9b0e5 | 2017-12-08 15:55:59 +0200 | [diff] [blame] | 8142 | ret = extent_write_full_page(page, wbc); |
Josef Bacik | be7bd73 | 2015-10-22 15:05:09 -0400 | [diff] [blame] | 8143 | btrfs_add_delayed_iput(inode); |
| 8144 | return ret; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8145 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8146 | |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 8147 | static int btrfs_writepages(struct address_space *mapping, |
| 8148 | struct writeback_control *wbc) |
Chris Mason | b293f02e | 2007-11-01 19:45:34 -0400 | [diff] [blame] | 8149 | { |
Nikolay Borisov | 8ae225a | 2018-04-19 10:46:38 +0300 | [diff] [blame] | 8150 | return extent_writepages(mapping, wbc); |
Chris Mason | b293f02e | 2007-11-01 19:45:34 -0400 | [diff] [blame] | 8151 | } |
| 8152 | |
Matthew Wilcox (Oracle) | ba206a0 | 2020-06-01 21:47:05 -0700 | [diff] [blame] | 8153 | static void btrfs_readahead(struct readahead_control *rac) |
Chris Mason | 3ab2fb5 | 2007-11-08 10:59:22 -0500 | [diff] [blame] | 8154 | { |
Matthew Wilcox (Oracle) | ba206a0 | 2020-06-01 21:47:05 -0700 | [diff] [blame] | 8155 | extent_readahead(rac); |
Chris Mason | 3ab2fb5 | 2007-11-08 10:59:22 -0500 | [diff] [blame] | 8156 | } |
Nikolay Borisov | 2a3ff0a | 2018-04-19 10:46:36 +0300 | [diff] [blame] | 8157 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8158 | static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8159 | { |
Nikolay Borisov | 477a30b | 2018-04-19 10:46:34 +0300 | [diff] [blame] | 8160 | int ret = try_release_extent_mapping(page, gfp_flags); |
Guoqing Jiang | d1b89bc | 2020-06-01 21:47:45 -0700 | [diff] [blame] | 8161 | if (ret == 1) |
| 8162 | detach_page_private(page); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8163 | return ret; |
| 8164 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8165 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8166 | static int btrfs_releasepage(struct page *page, gfp_t gfp_flags) |
| 8167 | { |
Chris Mason | 98509cf | 2008-09-11 15:51:43 -0400 | [diff] [blame] | 8168 | if (PageWriteback(page) || PageDirty(page)) |
| 8169 | return 0; |
Michal Hocko | 3ba7ab2 | 2017-01-09 15:39:02 +0100 | [diff] [blame] | 8170 | return __btrfs_releasepage(page, gfp_flags); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8171 | } |
| 8172 | |
Roman Gushchin | f8e6608 | 2020-03-04 16:57:35 -0800 | [diff] [blame] | 8173 | #ifdef CONFIG_MIGRATION |
| 8174 | static int btrfs_migratepage(struct address_space *mapping, |
| 8175 | struct page *newpage, struct page *page, |
| 8176 | enum migrate_mode mode) |
| 8177 | { |
| 8178 | int ret; |
| 8179 | |
| 8180 | ret = migrate_page_move_mapping(mapping, newpage, page, 0); |
| 8181 | if (ret != MIGRATEPAGE_SUCCESS) |
| 8182 | return ret; |
| 8183 | |
Guoqing Jiang | d1b89bc | 2020-06-01 21:47:45 -0700 | [diff] [blame] | 8184 | if (page_has_private(page)) |
| 8185 | attach_page_private(newpage, detach_page_private(page)); |
Roman Gushchin | f8e6608 | 2020-03-04 16:57:35 -0800 | [diff] [blame] | 8186 | |
| 8187 | if (PagePrivate2(page)) { |
| 8188 | ClearPagePrivate2(page); |
| 8189 | SetPagePrivate2(newpage); |
| 8190 | } |
| 8191 | |
| 8192 | if (mode != MIGRATE_SYNC_NO_COPY) |
| 8193 | migrate_page_copy(newpage, page); |
| 8194 | else |
| 8195 | migrate_page_states(newpage, page); |
| 8196 | return MIGRATEPAGE_SUCCESS; |
| 8197 | } |
| 8198 | #endif |
| 8199 | |
Lukas Czerner | d47992f | 2013-05-21 23:17:23 -0400 | [diff] [blame] | 8200 | static void btrfs_invalidatepage(struct page *page, unsigned int offset, |
| 8201 | unsigned int length) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8202 | { |
Nikolay Borisov | 53ac7ea | 2020-08-31 14:42:43 +0300 | [diff] [blame] | 8203 | struct btrfs_inode *inode = BTRFS_I(page->mapping->host); |
| 8204 | struct extent_io_tree *tree = &inode->io_tree; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8205 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 8206 | struct extent_state *cached_state = NULL; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8207 | u64 page_start = page_offset(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 8208 | u64 page_end = page_start + PAGE_SIZE - 1; |
Chandan Rajendra | dbfdb6d | 2016-01-21 15:55:58 +0530 | [diff] [blame] | 8209 | u64 start; |
| 8210 | u64 end; |
Nikolay Borisov | 53ac7ea | 2020-08-31 14:42:43 +0300 | [diff] [blame] | 8211 | int inode_evicting = inode->vfs_inode.i_state & I_FREEING; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8212 | |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 8213 | /* |
| 8214 | * we have the page locked, so new writeback can't start, |
| 8215 | * and the dirty bit won't be cleared while we are here. |
| 8216 | * |
| 8217 | * Wait for IO on this page so that we can safely clear |
| 8218 | * the PagePrivate2 bit and do ordered accounting |
| 8219 | */ |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8220 | wait_on_page_writeback(page); |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 8221 | |
Qu Wenruo | ad71a9a | 2021-05-31 16:50:55 +0800 | [diff] [blame] | 8222 | /* |
| 8223 | * For subpage case, we have call sites like |
| 8224 | * btrfs_punch_hole_lock_range() which passes range not aligned to |
| 8225 | * sectorsize. |
| 8226 | * If the range doesn't cover the full page, we don't need to and |
| 8227 | * shouldn't clear page extent mapped, as page->private can still |
| 8228 | * record subpage dirty bits for other part of the range. |
| 8229 | * |
| 8230 | * For cases that can invalidate the full even the range doesn't |
| 8231 | * cover the full page, like invalidating the last page, we're |
| 8232 | * still safe to wait for ordered extent to finish. |
| 8233 | */ |
| 8234 | if (!(offset == 0 && length == PAGE_SIZE)) { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8235 | btrfs_releasepage(page, GFP_NOFS); |
| 8236 | return; |
| 8237 | } |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 8238 | |
| 8239 | if (!inode_evicting) |
David Sterba | ff13db4 | 2015-12-03 14:30:40 +0100 | [diff] [blame] | 8240 | lock_extent_bits(tree, page_start, page_end, &cached_state); |
Qu Wenruo | 6a402b9 | 2021-01-27 14:38:48 +0800 | [diff] [blame] | 8241 | |
Chandan Rajendra | dbfdb6d | 2016-01-21 15:55:58 +0530 | [diff] [blame] | 8242 | start = page_start; |
Qu Wenruo | 6a402b9 | 2021-01-27 14:38:48 +0800 | [diff] [blame] | 8243 | again: |
Nikolay Borisov | 53ac7ea | 2020-08-31 14:42:43 +0300 | [diff] [blame] | 8244 | ordered = btrfs_lookup_ordered_range(inode, start, page_end - start + 1); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8245 | if (ordered) { |
Omar Sandoval | bffe633 | 2019-12-02 17:34:19 -0800 | [diff] [blame] | 8246 | end = min(page_end, |
| 8247 | ordered->file_offset + ordered->num_bytes - 1); |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 8248 | /* |
| 8249 | * IO on this page will never be started, so we need |
| 8250 | * to account for any ordered extents now |
| 8251 | */ |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 8252 | if (!inode_evicting) |
Chandan Rajendra | dbfdb6d | 2016-01-21 15:55:58 +0530 | [diff] [blame] | 8253 | clear_extent_bit(tree, start, end, |
Omar Sandoval | e182163 | 2019-08-15 14:04:04 -0700 | [diff] [blame] | 8254 | EXTENT_DELALLOC | EXTENT_DELALLOC_NEW | |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 8255 | EXTENT_LOCKED | EXTENT_DO_ACCOUNTING | |
David Sterba | ae0f162 | 2017-10-31 16:37:52 +0100 | [diff] [blame] | 8256 | EXTENT_DEFRAG, 1, 0, &cached_state); |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 8257 | /* |
| 8258 | * whoever cleared the private bit is responsible |
| 8259 | * for the finish_ordered_io |
| 8260 | */ |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 8261 | if (TestClearPagePrivate2(page)) { |
| 8262 | struct btrfs_ordered_inode_tree *tree; |
| 8263 | u64 new_len; |
| 8264 | |
Nikolay Borisov | 53ac7ea | 2020-08-31 14:42:43 +0300 | [diff] [blame] | 8265 | tree = &inode->ordered_tree; |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 8266 | |
| 8267 | spin_lock_irq(&tree->lock); |
| 8268 | set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags); |
Chandan Rajendra | dbfdb6d | 2016-01-21 15:55:58 +0530 | [diff] [blame] | 8269 | new_len = start - ordered->file_offset; |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 8270 | if (new_len < ordered->truncated_len) |
| 8271 | ordered->truncated_len = new_len; |
| 8272 | spin_unlock_irq(&tree->lock); |
| 8273 | |
Nikolay Borisov | 53ac7ea | 2020-08-31 14:42:43 +0300 | [diff] [blame] | 8274 | if (btrfs_dec_test_ordered_pending(inode, &ordered, |
| 8275 | start, |
Chandan Rajendra | dbfdb6d | 2016-01-21 15:55:58 +0530 | [diff] [blame] | 8276 | end - start + 1, 1)) |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 8277 | btrfs_finish_ordered_io(ordered); |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 8278 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8279 | btrfs_put_ordered_extent(ordered); |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 8280 | if (!inode_evicting) { |
| 8281 | cached_state = NULL; |
Chandan Rajendra | dbfdb6d | 2016-01-21 15:55:58 +0530 | [diff] [blame] | 8282 | lock_extent_bits(tree, start, end, |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 8283 | &cached_state); |
| 8284 | } |
Chandan Rajendra | dbfdb6d | 2016-01-21 15:55:58 +0530 | [diff] [blame] | 8285 | |
| 8286 | start = end + 1; |
| 8287 | if (start < page_end) |
| 8288 | goto again; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8289 | } |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 8290 | |
Qu Wenruo | b9d0b38 | 2015-09-29 10:35:16 +0800 | [diff] [blame] | 8291 | /* |
| 8292 | * Qgroup reserved space handler |
| 8293 | * Page here will be either |
Qu Wenruo | fa91e4a | 2020-07-17 15:12:05 +0800 | [diff] [blame] | 8294 | * 1) Already written to disk or ordered extent already submitted |
| 8295 | * Then its QGROUP_RESERVED bit in io_tree is already cleaned. |
| 8296 | * Qgroup will be handled by its qgroup_record then. |
| 8297 | * btrfs_qgroup_free_data() call will do nothing here. |
| 8298 | * |
| 8299 | * 2) Not written to disk yet |
| 8300 | * Then btrfs_qgroup_free_data() call will clear the QGROUP_RESERVED |
| 8301 | * bit of its io_tree, and free the qgroup reserved data space. |
| 8302 | * Since the IO will never happen for this page. |
Qu Wenruo | b9d0b38 | 2015-09-29 10:35:16 +0800 | [diff] [blame] | 8303 | */ |
Nikolay Borisov | 53ac7ea | 2020-08-31 14:42:43 +0300 | [diff] [blame] | 8304 | btrfs_qgroup_free_data(inode, NULL, page_start, PAGE_SIZE); |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 8305 | if (!inode_evicting) { |
Omar Sandoval | e182163 | 2019-08-15 14:04:04 -0700 | [diff] [blame] | 8306 | clear_extent_bit(tree, page_start, page_end, EXTENT_LOCKED | |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 8307 | EXTENT_DELALLOC | EXTENT_DELALLOC_NEW | |
| 8308 | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1, |
David Sterba | ae0f162 | 2017-10-31 16:37:52 +0100 | [diff] [blame] | 8309 | &cached_state); |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 8310 | |
| 8311 | __btrfs_releasepage(page, GFP_NOFS); |
| 8312 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8313 | |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 8314 | ClearPageChecked(page); |
Guoqing Jiang | d1b89bc | 2020-06-01 21:47:45 -0700 | [diff] [blame] | 8315 | detach_page_private(page); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8316 | } |
| 8317 | |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8318 | /* |
| 8319 | * btrfs_page_mkwrite() is not allowed to change the file size as it gets |
| 8320 | * called from a page fault handler when a page is first dirtied. Hence we must |
| 8321 | * be careful to check for EOF conditions here. We set the page up correctly |
| 8322 | * for a written page which means we get ENOSPC checking when writing into |
| 8323 | * holes and correct delalloc and unwritten extent mapping on filesystems that |
| 8324 | * support these features. |
| 8325 | * |
| 8326 | * We are not allowed to take the i_mutex here so we have to play games to |
| 8327 | * 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] | 8328 | * truncate_setsize() writes the inode size before removing pages, once we have |
| 8329 | * 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] | 8330 | * beyond EOF, then the page is guaranteed safe against truncation until we |
| 8331 | * unlock the page. |
| 8332 | */ |
Souptick Joarder | a528a24 | 2018-06-06 19:54:44 +0530 | [diff] [blame] | 8333 | vm_fault_t btrfs_page_mkwrite(struct vm_fault *vmf) |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8334 | { |
Nick Piggin | c2ec175 | 2009-03-31 15:23:21 -0700 | [diff] [blame] | 8335 | struct page *page = vmf->page; |
Dave Jiang | 11bac80 | 2017-02-24 14:56:41 -0800 | [diff] [blame] | 8336 | struct inode *inode = file_inode(vmf->vma->vm_file); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8337 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8338 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
| 8339 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 8340 | struct extent_state *cached_state = NULL; |
Qu Wenruo | 364ecf3 | 2017-02-27 15:10:38 +0800 | [diff] [blame] | 8341 | struct extent_changeset *data_reserved = NULL; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8342 | char *kaddr; |
| 8343 | unsigned long zero_start; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8344 | loff_t size; |
Souptick Joarder | a528a24 | 2018-06-06 19:54:44 +0530 | [diff] [blame] | 8345 | vm_fault_t ret; |
| 8346 | int ret2; |
Chris Mason | 9998eb7 | 2012-01-25 13:47:40 -0500 | [diff] [blame] | 8347 | int reserved = 0; |
Chandan Rajendra | d0b7da88 | 2016-01-21 15:55:57 +0530 | [diff] [blame] | 8348 | u64 reserved_space; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8349 | u64 page_start; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8350 | u64 page_end; |
Chandan Rajendra | d0b7da88 | 2016-01-21 15:55:57 +0530 | [diff] [blame] | 8351 | u64 end; |
| 8352 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 8353 | reserved_space = PAGE_SIZE; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8354 | |
Jan Kara | b2b5ef5 | 2012-06-12 16:20:45 +0200 | [diff] [blame] | 8355 | sb_start_pagefault(inode->i_sb); |
Qu Wenruo | df48063 | 2015-09-08 17:25:54 +0800 | [diff] [blame] | 8356 | page_start = page_offset(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 8357 | page_end = page_start + PAGE_SIZE - 1; |
Chandan Rajendra | d0b7da88 | 2016-01-21 15:55:57 +0530 | [diff] [blame] | 8358 | end = page_end; |
Qu Wenruo | df48063 | 2015-09-08 17:25:54 +0800 | [diff] [blame] | 8359 | |
Chandan Rajendra | d0b7da88 | 2016-01-21 15:55:57 +0530 | [diff] [blame] | 8360 | /* |
| 8361 | * Reserving delalloc space after obtaining the page lock can lead to |
| 8362 | * deadlock. For example, if a dirty page is locked by this function |
| 8363 | * and the call to btrfs_delalloc_reserve_space() ends up triggering |
| 8364 | * dirty page write out, then the btrfs_writepage() function could |
| 8365 | * end up waiting indefinitely to get a lock on the page currently |
| 8366 | * being processed by btrfs_page_mkwrite() function. |
| 8367 | */ |
Nikolay Borisov | e5b7231e | 2020-06-03 08:55:42 +0300 | [diff] [blame] | 8368 | ret2 = btrfs_delalloc_reserve_space(BTRFS_I(inode), &data_reserved, |
| 8369 | page_start, reserved_space); |
Souptick Joarder | a528a24 | 2018-06-06 19:54:44 +0530 | [diff] [blame] | 8370 | if (!ret2) { |
| 8371 | ret2 = file_update_time(vmf->vma->vm_file); |
Chris Mason | 9998eb7 | 2012-01-25 13:47:40 -0500 | [diff] [blame] | 8372 | reserved = 1; |
| 8373 | } |
Souptick Joarder | a528a24 | 2018-06-06 19:54:44 +0530 | [diff] [blame] | 8374 | if (ret2) { |
| 8375 | ret = vmf_error(ret2); |
Chris Mason | 9998eb7 | 2012-01-25 13:47:40 -0500 | [diff] [blame] | 8376 | if (reserved) |
| 8377 | goto out; |
| 8378 | goto out_noreserve; |
Nick Piggin | 56a76f8 | 2009-03-31 15:23:23 -0700 | [diff] [blame] | 8379 | } |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 8380 | |
Nick Piggin | 56a76f8 | 2009-03-31 15:23:23 -0700 | [diff] [blame] | 8381 | ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */ |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8382 | again: |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8383 | lock_page(page); |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8384 | size = i_size_read(inode); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8385 | |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8386 | if ((page->mapping != inode->i_mapping) || |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8387 | (page_start >= size)) { |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8388 | /* page got truncated out from underneath us */ |
| 8389 | goto out_unlock; |
| 8390 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8391 | wait_on_page_writeback(page); |
| 8392 | |
David Sterba | ff13db4 | 2015-12-03 14:30:40 +0100 | [diff] [blame] | 8393 | lock_extent_bits(io_tree, page_start, page_end, &cached_state); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8394 | set_page_extent_mapped(page); |
| 8395 | |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 8396 | /* |
| 8397 | * we can't set the delalloc bits if there are pending ordered |
| 8398 | * extents. Drop our locks and wait for them to finish |
| 8399 | */ |
Nikolay Borisov | a776c6f | 2017-02-20 13:50:49 +0200 | [diff] [blame] | 8400 | ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start, |
| 8401 | PAGE_SIZE); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8402 | if (ordered) { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 8403 | unlock_extent_cached(io_tree, page_start, page_end, |
David Sterba | e43bbe5 | 2017-12-12 21:43:52 +0100 | [diff] [blame] | 8404 | &cached_state); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8405 | unlock_page(page); |
Nikolay Borisov | c0a4360 | 2020-09-18 12:15:53 +0300 | [diff] [blame] | 8406 | btrfs_start_ordered_extent(ordered, 1); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8407 | btrfs_put_ordered_extent(ordered); |
| 8408 | goto again; |
| 8409 | } |
| 8410 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 8411 | if (page->index == ((size - 1) >> PAGE_SHIFT)) { |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 8412 | reserved_space = round_up(size - page_start, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8413 | fs_info->sectorsize); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 8414 | if (reserved_space < PAGE_SIZE) { |
Chandan Rajendra | d0b7da88 | 2016-01-21 15:55:57 +0530 | [diff] [blame] | 8415 | end = page_start + reserved_space - 1; |
Nikolay Borisov | 86d5292 | 2020-06-03 08:55:40 +0300 | [diff] [blame] | 8416 | btrfs_delalloc_release_space(BTRFS_I(inode), |
| 8417 | data_reserved, page_start, |
| 8418 | PAGE_SIZE - reserved_space, true); |
Chandan Rajendra | d0b7da88 | 2016-01-21 15:55:57 +0530 | [diff] [blame] | 8419 | } |
| 8420 | } |
| 8421 | |
Josef Bacik | fbf1908 | 2009-10-01 17:10:23 -0400 | [diff] [blame] | 8422 | /* |
Liu Bo | 5416034 | 2016-12-13 12:15:19 -0800 | [diff] [blame] | 8423 | * page_mkwrite gets called when the page is firstly dirtied after it's |
| 8424 | * faulted in, but write(2) could also dirty a page and set delalloc |
| 8425 | * bits, thus in this case for space account reason, we still need to |
| 8426 | * clear any delalloc bits within this page range since we have to |
| 8427 | * reserve data&meta space before lock_page() (see above comments). |
Josef Bacik | fbf1908 | 2009-10-01 17:10:23 -0400 | [diff] [blame] | 8428 | */ |
Chandan Rajendra | d0b7da88 | 2016-01-21 15:55:57 +0530 | [diff] [blame] | 8429 | clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, end, |
Omar Sandoval | e182163 | 2019-08-15 14:04:04 -0700 | [diff] [blame] | 8430 | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING | |
| 8431 | EXTENT_DEFRAG, 0, 0, &cached_state); |
Josef Bacik | fbf1908 | 2009-10-01 17:10:23 -0400 | [diff] [blame] | 8432 | |
Nikolay Borisov | c2566f2 | 2020-06-03 08:55:35 +0300 | [diff] [blame] | 8433 | ret2 = btrfs_set_extent_delalloc(BTRFS_I(inode), page_start, end, 0, |
Nikolay Borisov | 330a582 | 2019-07-17 16:18:17 +0300 | [diff] [blame] | 8434 | &cached_state); |
Souptick Joarder | a528a24 | 2018-06-06 19:54:44 +0530 | [diff] [blame] | 8435 | if (ret2) { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 8436 | unlock_extent_cached(io_tree, page_start, page_end, |
David Sterba | e43bbe5 | 2017-12-12 21:43:52 +0100 | [diff] [blame] | 8437 | &cached_state); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 8438 | ret = VM_FAULT_SIGBUS; |
| 8439 | goto out_unlock; |
| 8440 | } |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8441 | |
| 8442 | /* page is wholly or partially inside EOF */ |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 8443 | if (page_start + PAGE_SIZE > size) |
Johannes Thumshirn | 7073017 | 2018-12-05 15:23:03 +0100 | [diff] [blame] | 8444 | zero_start = offset_in_page(size); |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8445 | else |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 8446 | zero_start = PAGE_SIZE; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8447 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 8448 | if (zero_start != PAGE_SIZE) { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8449 | kaddr = kmap(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 8450 | memset(kaddr + zero_start, 0, PAGE_SIZE - zero_start); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8451 | flush_dcache_page(page); |
| 8452 | kunmap(page); |
| 8453 | } |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 8454 | ClearPageChecked(page); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8455 | set_page_dirty(page); |
Chris Mason | 50a9b21 | 2009-09-11 12:33:12 -0400 | [diff] [blame] | 8456 | SetPageUptodate(page); |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 8457 | |
Filipe Manana | d845f89 | 2021-02-23 12:08:48 +0000 | [diff] [blame] | 8458 | btrfs_set_inode_last_sub_trans(BTRFS_I(inode)); |
Chris Mason | 257c62e | 2009-10-13 13:21:08 -0400 | [diff] [blame] | 8459 | |
David Sterba | e43bbe5 | 2017-12-12 21:43:52 +0100 | [diff] [blame] | 8460 | unlock_extent_cached(io_tree, page_start, page_end, &cached_state); |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8461 | |
Yunfeng Ye | 76de60e | 2019-12-03 16:59:25 +0800 | [diff] [blame] | 8462 | btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE); |
| 8463 | sb_end_pagefault(inode->i_sb); |
| 8464 | extent_changeset_free(data_reserved); |
| 8465 | return VM_FAULT_LOCKED; |
Chris Mason | 717beb9 | 2018-06-25 10:03:41 -0700 | [diff] [blame] | 8466 | |
| 8467 | out_unlock: |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8468 | unlock_page(page); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 8469 | out: |
Qu Wenruo | 8702ba9 | 2019-10-14 14:34:51 +0800 | [diff] [blame] | 8470 | btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE); |
Nikolay Borisov | 86d5292 | 2020-06-03 08:55:40 +0300 | [diff] [blame] | 8471 | btrfs_delalloc_release_space(BTRFS_I(inode), data_reserved, page_start, |
Qu Wenruo | 43b1859 | 2017-12-12 15:34:32 +0800 | [diff] [blame] | 8472 | reserved_space, (ret != 0)); |
Chris Mason | 9998eb7 | 2012-01-25 13:47:40 -0500 | [diff] [blame] | 8473 | out_noreserve: |
Jan Kara | b2b5ef5 | 2012-06-12 16:20:45 +0200 | [diff] [blame] | 8474 | sb_end_pagefault(inode->i_sb); |
Qu Wenruo | 364ecf3 | 2017-02-27 15:10:38 +0800 | [diff] [blame] | 8475 | extent_changeset_free(data_reserved); |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8476 | return ret; |
| 8477 | } |
| 8478 | |
Filipe Manana | 213e8c5 | 2018-02-06 20:40:31 +0000 | [diff] [blame] | 8479 | static int btrfs_truncate(struct inode *inode, bool skip_writeback) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8480 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8481 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8482 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 8483 | struct btrfs_block_rsv *rsv; |
Omar Sandoval | ad7e1a7 | 2018-05-22 09:59:50 -0700 | [diff] [blame] | 8484 | int ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8485 | struct btrfs_trans_handle *trans; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8486 | u64 mask = fs_info->sectorsize - 1; |
Josef Bacik | 2bd36e7 | 2019-08-22 15:14:33 -0400 | [diff] [blame] | 8487 | u64 min_size = btrfs_calc_metadata_size(fs_info, 1); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8488 | |
Filipe Manana | 213e8c5 | 2018-02-06 20:40:31 +0000 | [diff] [blame] | 8489 | if (!skip_writeback) { |
| 8490 | ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask), |
| 8491 | (u64)-1); |
| 8492 | if (ret) |
| 8493 | return ret; |
| 8494 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8495 | |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 8496 | /* |
Omar Sandoval | f7e9e8f | 2018-05-11 13:13:32 -0700 | [diff] [blame] | 8497 | * Yes ladies and gentlemen, this is indeed ugly. We have a couple of |
| 8498 | * things going on here: |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 8499 | * |
Omar Sandoval | f7e9e8f | 2018-05-11 13:13:32 -0700 | [diff] [blame] | 8500 | * 1) We need to reserve space to update our inode. |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 8501 | * |
Omar Sandoval | f7e9e8f | 2018-05-11 13:13:32 -0700 | [diff] [blame] | 8502 | * 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] | 8503 | * be free'd up by the truncate operation, but also have some slack |
| 8504 | * space reserved in case it uses space during the truncate (thank you |
| 8505 | * very much snapshotting). |
| 8506 | * |
Omar Sandoval | f7e9e8f | 2018-05-11 13:13:32 -0700 | [diff] [blame] | 8507 | * 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] | 8508 | * 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] | 8509 | * 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] | 8510 | * doesn't end up using space reserved for updating the inode. We also |
| 8511 | * need to be able to stop the transaction and start a new one, which |
| 8512 | * means we need to be able to update the inode several times, and we |
| 8513 | * have no idea of knowing how many times that will be, so we can't just |
| 8514 | * reserve 1 item for the entirety of the operation, so that has to be |
| 8515 | * done separately as well. |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 8516 | * |
| 8517 | * So that leaves us with |
| 8518 | * |
Omar Sandoval | f7e9e8f | 2018-05-11 13:13:32 -0700 | [diff] [blame] | 8519 | * 1) rsv - for the truncate reservation, which we will steal from the |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 8520 | * transaction reservation. |
Omar Sandoval | f7e9e8f | 2018-05-11 13:13:32 -0700 | [diff] [blame] | 8521 | * 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] | 8522 | * updating the inode. |
| 8523 | */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 8524 | rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP); |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 8525 | if (!rsv) |
| 8526 | return -ENOMEM; |
Josef Bacik | 4a33854 | 2011-08-29 11:01:31 -0400 | [diff] [blame] | 8527 | rsv->size = min_size; |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 8528 | rsv->failfast = 1; |
Josef Bacik | f0cd846 | 2011-03-04 14:37:08 -0500 | [diff] [blame] | 8529 | |
Josef Bacik | 907cbce | 2011-08-08 13:46:15 -0400 | [diff] [blame] | 8530 | /* |
Josef Bacik | 0712718 | 2011-08-19 10:29:59 -0400 | [diff] [blame] | 8531 | * 1 for the truncate slack space |
Josef Bacik | 907cbce | 2011-08-08 13:46:15 -0400 | [diff] [blame] | 8532 | * 1 for updating the inode. |
| 8533 | */ |
Josef Bacik | f3fe820 | 2013-01-07 17:03:21 -0500 | [diff] [blame] | 8534 | trans = btrfs_start_transaction(root, 2); |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 8535 | if (IS_ERR(trans)) { |
Omar Sandoval | ad7e1a7 | 2018-05-22 09:59:50 -0700 | [diff] [blame] | 8536 | ret = PTR_ERR(trans); |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 8537 | goto out; |
| 8538 | } |
Josef Bacik | f0cd846 | 2011-03-04 14:37:08 -0500 | [diff] [blame] | 8539 | |
Josef Bacik | 907cbce | 2011-08-08 13:46:15 -0400 | [diff] [blame] | 8540 | /* Migrate the slack space for the truncate to our reserve */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8541 | ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, rsv, |
Lu Fengqi | 3a58417 | 2018-08-04 21:10:55 +0800 | [diff] [blame] | 8542 | min_size, false); |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 8543 | BUG_ON(ret); |
Josef Bacik | f0cd846 | 2011-03-04 14:37:08 -0500 | [diff] [blame] | 8544 | |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 8545 | /* |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 8546 | * So if we truncate and then write and fsync we normally would just |
| 8547 | * write the extents that changed, which is a problem if we need to |
| 8548 | * first truncate that entire inode. So set this flag so we write out |
| 8549 | * all of the extents in the inode to the sync log so we're completely |
| 8550 | * safe. |
| 8551 | */ |
| 8552 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags); |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 8553 | trans->block_rsv = rsv; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 8554 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 8555 | while (1) { |
| 8556 | ret = btrfs_truncate_inode_items(trans, root, inode, |
| 8557 | inode->i_size, |
| 8558 | BTRFS_EXTENT_DATA_KEY); |
Josef Bacik | ddfae63 | 2017-10-19 14:16:02 -0400 | [diff] [blame] | 8559 | trans->block_rsv = &fs_info->trans_block_rsv; |
Omar Sandoval | ad7e1a7 | 2018-05-22 09:59:50 -0700 | [diff] [blame] | 8560 | if (ret != -ENOSPC && ret != -EAGAIN) |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 8561 | break; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8562 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 8563 | ret = btrfs_update_inode(trans, root, inode); |
Omar Sandoval | ad7e1a7 | 2018-05-22 09:59:50 -0700 | [diff] [blame] | 8564 | if (ret) |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 8565 | break; |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 8566 | |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 8567 | btrfs_end_transaction(trans); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 8568 | btrfs_btree_balance_dirty(fs_info); |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 8569 | |
| 8570 | trans = btrfs_start_transaction(root, 2); |
| 8571 | if (IS_ERR(trans)) { |
Omar Sandoval | ad7e1a7 | 2018-05-22 09:59:50 -0700 | [diff] [blame] | 8572 | ret = PTR_ERR(trans); |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 8573 | trans = NULL; |
| 8574 | break; |
| 8575 | } |
| 8576 | |
Nikolay Borisov | 63f018b | 2020-03-10 10:59:31 +0200 | [diff] [blame] | 8577 | btrfs_block_rsv_release(fs_info, rsv, -1, NULL); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8578 | ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, |
Lu Fengqi | 3a58417 | 2018-08-04 21:10:55 +0800 | [diff] [blame] | 8579 | rsv, min_size, false); |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 8580 | BUG_ON(ret); /* shouldn't happen */ |
| 8581 | trans->block_rsv = rsv; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 8582 | } |
| 8583 | |
Josef Bacik | ddfae63 | 2017-10-19 14:16:02 -0400 | [diff] [blame] | 8584 | /* |
| 8585 | * We can't call btrfs_truncate_block inside a trans handle as we could |
| 8586 | * deadlock with freeze, if we got NEED_TRUNCATE_BLOCK then we know |
| 8587 | * we've truncated everything except the last little bit, and can do |
| 8588 | * btrfs_truncate_block and then update the disk_i_size. |
| 8589 | */ |
| 8590 | if (ret == NEED_TRUNCATE_BLOCK) { |
| 8591 | btrfs_end_transaction(trans); |
| 8592 | btrfs_btree_balance_dirty(fs_info); |
| 8593 | |
| 8594 | ret = btrfs_truncate_block(inode, inode->i_size, 0, 0); |
| 8595 | if (ret) |
| 8596 | goto out; |
| 8597 | trans = btrfs_start_transaction(root, 1); |
| 8598 | if (IS_ERR(trans)) { |
| 8599 | ret = PTR_ERR(trans); |
| 8600 | goto out; |
| 8601 | } |
Josef Bacik | d923afe | 2020-01-17 09:02:23 -0500 | [diff] [blame] | 8602 | btrfs_inode_safe_disk_i_size_write(inode, 0); |
Josef Bacik | ddfae63 | 2017-10-19 14:16:02 -0400 | [diff] [blame] | 8603 | } |
| 8604 | |
Chris Mason | 917c16b | 2011-11-08 14:49:59 -0500 | [diff] [blame] | 8605 | if (trans) { |
Omar Sandoval | ad7e1a7 | 2018-05-22 09:59:50 -0700 | [diff] [blame] | 8606 | int ret2; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 8607 | |
Omar Sandoval | ad7e1a7 | 2018-05-22 09:59:50 -0700 | [diff] [blame] | 8608 | trans->block_rsv = &fs_info->trans_block_rsv; |
| 8609 | ret2 = btrfs_update_inode(trans, root, inode); |
| 8610 | if (ret2 && !ret) |
| 8611 | ret = ret2; |
| 8612 | |
| 8613 | ret2 = btrfs_end_transaction(trans); |
| 8614 | if (ret2 && !ret) |
| 8615 | ret = ret2; |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 8616 | btrfs_btree_balance_dirty(fs_info); |
Chris Mason | 917c16b | 2011-11-08 14:49:59 -0500 | [diff] [blame] | 8617 | } |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 8618 | out: |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 8619 | btrfs_free_block_rsv(fs_info, rsv); |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 8620 | |
Omar Sandoval | ad7e1a7 | 2018-05-22 09:59:50 -0700 | [diff] [blame] | 8621 | return ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8622 | } |
| 8623 | |
Sven Wegener | 3b96362 | 2008-06-09 21:57:42 -0400 | [diff] [blame] | 8624 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 8625 | * create a new subvolume directory/inode (helper for the ioctl). |
| 8626 | */ |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 8627 | int btrfs_create_subvol_root(struct btrfs_trans_handle *trans, |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 8628 | struct btrfs_root *new_root, |
| 8629 | struct btrfs_root *parent_root, |
| 8630 | u64 new_dirid) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8631 | { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8632 | struct inode *inode; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 8633 | int err; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 8634 | u64 index = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8635 | |
Florian Albrechtskirchinger | 12fc9d0 | 2012-02-10 22:15:54 +0100 | [diff] [blame] | 8636 | inode = btrfs_new_inode(trans, new_root, NULL, "..", 2, |
| 8637 | new_dirid, new_dirid, |
| 8638 | S_IFDIR | (~current_umask() & S_IRWXUGO), |
| 8639 | &index); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 8640 | if (IS_ERR(inode)) |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 8641 | return PTR_ERR(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8642 | inode->i_op = &btrfs_dir_inode_operations; |
| 8643 | inode->i_fop = &btrfs_dir_file_operations; |
| 8644 | |
Miklos Szeredi | bfe8684 | 2011-10-28 14:13:29 +0200 | [diff] [blame] | 8645 | set_nlink(inode, 1); |
Nikolay Borisov | 6ef06d2 | 2017-02-20 13:50:34 +0200 | [diff] [blame] | 8646 | btrfs_i_size_write(BTRFS_I(inode), 0); |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 8647 | unlock_new_inode(inode); |
Sven Wegener | 3b96362 | 2008-06-09 21:57:42 -0400 | [diff] [blame] | 8648 | |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 8649 | err = btrfs_subvol_inherit_props(trans, new_root, parent_root); |
| 8650 | if (err) |
| 8651 | btrfs_err(new_root->fs_info, |
David Sterba | 351fd35 | 2014-05-15 16:48:20 +0200 | [diff] [blame] | 8652 | "error inheriting subvolume %llu properties: %d", |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 8653 | new_root->root_key.objectid, err); |
| 8654 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 8655 | err = btrfs_update_inode(trans, new_root, inode); |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 8656 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 8657 | iput(inode); |
Mark Fasheh | ce59897 | 2011-07-26 11:32:23 -0700 | [diff] [blame] | 8658 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8659 | } |
| 8660 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8661 | struct inode *btrfs_alloc_inode(struct super_block *sb) |
| 8662 | { |
Josef Bacik | 69fe2d7 | 2017-10-19 14:15:57 -0400 | [diff] [blame] | 8663 | struct btrfs_fs_info *fs_info = btrfs_sb(sb); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8664 | struct btrfs_inode *ei; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8665 | struct inode *inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8666 | |
David Sterba | 712e36c | 2017-10-31 17:08:27 +0100 | [diff] [blame] | 8667 | ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_KERNEL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8668 | if (!ei) |
| 8669 | return NULL; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8670 | |
| 8671 | ei->root = NULL; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8672 | ei->generation = 0; |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 8673 | ei->last_trans = 0; |
Chris Mason | 257c62e | 2009-10-13 13:21:08 -0400 | [diff] [blame] | 8674 | ei->last_sub_trans = 0; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 8675 | ei->logged_trans = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8676 | ei->delalloc_bytes = 0; |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 8677 | ei->new_delalloc_bytes = 0; |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 8678 | ei->defrag_bytes = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8679 | ei->disk_i_size = 0; |
| 8680 | ei->flags = 0; |
Josef Bacik | 7709cde | 2011-08-04 10:25:02 -0400 | [diff] [blame] | 8681 | ei->csum_bytes = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8682 | ei->index_cnt = (u64)-1; |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 8683 | ei->dir_index = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8684 | ei->last_unlink_trans = 0; |
Filipe Manana | 3ebac17 | 2020-07-15 12:30:43 +0100 | [diff] [blame] | 8685 | ei->last_reflink_trans = 0; |
Liu Bo | 46d8bc3 | 2012-08-29 01:07:55 -0600 | [diff] [blame] | 8686 | ei->last_log_commit = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8687 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 8688 | spin_lock_init(&ei->lock); |
| 8689 | ei->outstanding_extents = 0; |
Josef Bacik | 69fe2d7 | 2017-10-19 14:15:57 -0400 | [diff] [blame] | 8690 | if (sb->s_magic != BTRFS_TEST_MAGIC) |
| 8691 | btrfs_init_metadata_block_rsv(fs_info, &ei->block_rsv, |
| 8692 | BTRFS_BLOCK_RSV_DELALLOC); |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 8693 | ei->runtime_flags = 0; |
David Sterba | b52aa8c | 2017-07-17 19:17:20 +0200 | [diff] [blame] | 8694 | ei->prop_compress = BTRFS_COMPRESS_NONE; |
David Sterba | eec63c6 | 2017-07-17 19:41:31 +0200 | [diff] [blame] | 8695 | ei->defrag_compress = BTRFS_COMPRESS_NONE; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8696 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 8697 | ei->delayed_node = NULL; |
| 8698 | |
chandan r | 9cc97d6 | 2012-07-04 12:48:07 +0530 | [diff] [blame] | 8699 | ei->i_otime.tv_sec = 0; |
| 8700 | ei->i_otime.tv_nsec = 0; |
| 8701 | |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8702 | inode = &ei->vfs_inode; |
David Sterba | a8067e0 | 2011-04-21 00:34:43 +0200 | [diff] [blame] | 8703 | extent_map_tree_init(&ei->extent_tree); |
Qu Wenruo | 43eb5f2 | 2019-03-01 10:47:59 +0800 | [diff] [blame] | 8704 | extent_io_tree_init(fs_info, &ei->io_tree, IO_TREE_INODE_IO, inode); |
| 8705 | extent_io_tree_init(fs_info, &ei->io_failure_tree, |
| 8706 | IO_TREE_INODE_IO_FAILURE, inode); |
Josef Bacik | 41a2ee7 | 2020-01-17 09:02:21 -0500 | [diff] [blame] | 8707 | extent_io_tree_init(fs_info, &ei->file_extent_tree, |
| 8708 | IO_TREE_INODE_FILE_EXTENT, inode); |
David Sterba | 7b43973 | 2019-03-11 15:58:30 +0100 | [diff] [blame] | 8709 | ei->io_tree.track_uptodate = true; |
| 8710 | ei->io_failure_tree.track_uptodate = true; |
Josef Bacik | b812ce2 | 2012-11-16 13:56:32 -0500 | [diff] [blame] | 8711 | atomic_set(&ei->sync_writers, 0); |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8712 | mutex_init(&ei->log_mutex); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8713 | btrfs_ordered_inode_tree_init(&ei->ordered_tree); |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8714 | INIT_LIST_HEAD(&ei->delalloc_inodes); |
David Sterba | 8089fe6 | 2015-11-19 14:15:51 +0100 | [diff] [blame] | 8715 | INIT_LIST_HEAD(&ei->delayed_iput); |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8716 | RB_CLEAR_NODE(&ei->rb_node); |
Filipe Manana | 5f9a8a5 | 2016-05-12 13:53:36 +0100 | [diff] [blame] | 8717 | init_rwsem(&ei->dio_sem); |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8718 | |
| 8719 | return inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8720 | } |
| 8721 | |
Josef Bacik | aaedb55 | 2013-10-11 14:44:09 -0400 | [diff] [blame] | 8722 | #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS |
| 8723 | void btrfs_test_destroy_inode(struct inode *inode) |
| 8724 | { |
Nikolay Borisov | dcdbc05 | 2017-02-20 13:50:45 +0200 | [diff] [blame] | 8725 | btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0); |
Josef Bacik | aaedb55 | 2013-10-11 14:44:09 -0400 | [diff] [blame] | 8726 | kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode)); |
| 8727 | } |
| 8728 | #endif |
| 8729 | |
Al Viro | 26602ca | 2019-04-10 15:14:41 -0400 | [diff] [blame] | 8730 | void btrfs_free_inode(struct inode *inode) |
Nick Piggin | fa0d7e3d | 2011-01-07 17:49:49 +1100 | [diff] [blame] | 8731 | { |
Nick Piggin | fa0d7e3d | 2011-01-07 17:49:49 +1100 | [diff] [blame] | 8732 | kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode)); |
| 8733 | } |
| 8734 | |
Nikolay Borisov | 633cc81 | 2020-09-18 12:15:49 +0300 | [diff] [blame] | 8735 | void btrfs_destroy_inode(struct inode *vfs_inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8736 | { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8737 | struct btrfs_ordered_extent *ordered; |
Nikolay Borisov | 633cc81 | 2020-09-18 12:15:49 +0300 | [diff] [blame] | 8738 | struct btrfs_inode *inode = BTRFS_I(vfs_inode); |
| 8739 | struct btrfs_root *root = inode->root; |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 8740 | |
Nikolay Borisov | 633cc81 | 2020-09-18 12:15:49 +0300 | [diff] [blame] | 8741 | WARN_ON(!hlist_empty(&vfs_inode->i_dentry)); |
| 8742 | WARN_ON(vfs_inode->i_data.nrpages); |
| 8743 | WARN_ON(inode->block_rsv.reserved); |
| 8744 | WARN_ON(inode->block_rsv.size); |
| 8745 | WARN_ON(inode->outstanding_extents); |
| 8746 | WARN_ON(inode->delalloc_bytes); |
| 8747 | WARN_ON(inode->new_delalloc_bytes); |
| 8748 | WARN_ON(inode->csum_bytes); |
| 8749 | WARN_ON(inode->defrag_bytes); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8750 | |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 8751 | /* |
Josef Bacik | a6dbd42 | 2009-11-11 15:53:34 -0500 | [diff] [blame] | 8752 | * This can happen where we create an inode, but somebody else also |
| 8753 | * created the same inode and we need to destroy the one we already |
| 8754 | * created. |
| 8755 | */ |
| 8756 | if (!root) |
Al Viro | 26602ca | 2019-04-10 15:14:41 -0400 | [diff] [blame] | 8757 | return; |
Josef Bacik | a6dbd42 | 2009-11-11 15:53:34 -0500 | [diff] [blame] | 8758 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 8759 | while (1) { |
Nikolay Borisov | 633cc81 | 2020-09-18 12:15:49 +0300 | [diff] [blame] | 8760 | ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8761 | if (!ordered) |
| 8762 | break; |
| 8763 | else { |
Nikolay Borisov | 633cc81 | 2020-09-18 12:15:49 +0300 | [diff] [blame] | 8764 | btrfs_err(root->fs_info, |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 8765 | "found ordered extent %llu %llu on inode cleanup", |
Omar Sandoval | bffe633 | 2019-12-02 17:34:19 -0800 | [diff] [blame] | 8766 | ordered->file_offset, ordered->num_bytes); |
Nikolay Borisov | 71fe0a5 | 2020-09-18 12:15:50 +0300 | [diff] [blame] | 8767 | btrfs_remove_ordered_extent(inode, ordered); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8768 | btrfs_put_ordered_extent(ordered); |
| 8769 | btrfs_put_ordered_extent(ordered); |
| 8770 | } |
| 8771 | } |
Nikolay Borisov | 633cc81 | 2020-09-18 12:15:49 +0300 | [diff] [blame] | 8772 | btrfs_qgroup_check_reserved_leak(inode); |
| 8773 | inode_tree_del(inode); |
| 8774 | btrfs_drop_extent_cache(inode, 0, (u64)-1, 0); |
| 8775 | btrfs_inode_clear_file_extent_range(inode, 0, (u64)-1); |
| 8776 | btrfs_put_root(inode->root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8777 | } |
| 8778 | |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 8779 | int btrfs_drop_inode(struct inode *inode) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 8780 | { |
| 8781 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 8782 | |
Naohiro Aota | 6379ef9 | 2013-06-06 09:56:34 +0000 | [diff] [blame] | 8783 | if (root == NULL) |
| 8784 | return 1; |
| 8785 | |
Liu Bo | fa6ac87 | 2013-02-20 14:10:23 +0000 | [diff] [blame] | 8786 | /* the snap/subvol tree is on deleting */ |
Stefan Behrens | 69e9c6c | 2013-09-05 16:58:43 +0200 | [diff] [blame] | 8787 | if (btrfs_root_refs(&root->root_item) == 0) |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 8788 | return 1; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 8789 | else |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 8790 | return generic_drop_inode(inode); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 8791 | } |
| 8792 | |
Sven Wegener | 0ee0fda | 2008-07-30 16:54:26 -0400 | [diff] [blame] | 8793 | static void init_once(void *foo) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8794 | { |
| 8795 | struct btrfs_inode *ei = (struct btrfs_inode *) foo; |
| 8796 | |
| 8797 | inode_init_once(&ei->vfs_inode); |
| 8798 | } |
| 8799 | |
David Sterba | e67c718 | 2018-02-19 17:24:18 +0100 | [diff] [blame] | 8800 | void __cold btrfs_destroy_cachep(void) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8801 | { |
Kirill A. Shutemov | 8c0a853 | 2012-09-26 11:33:07 +1000 | [diff] [blame] | 8802 | /* |
| 8803 | * Make sure all delayed rcu free inodes are flushed before we |
| 8804 | * destroy cache. |
| 8805 | */ |
| 8806 | rcu_barrier(); |
Kinglong Mee | 5598e90 | 2016-01-29 21:36:35 +0800 | [diff] [blame] | 8807 | kmem_cache_destroy(btrfs_inode_cachep); |
| 8808 | kmem_cache_destroy(btrfs_trans_handle_cachep); |
Kinglong Mee | 5598e90 | 2016-01-29 21:36:35 +0800 | [diff] [blame] | 8809 | kmem_cache_destroy(btrfs_path_cachep); |
| 8810 | kmem_cache_destroy(btrfs_free_space_cachep); |
Christophe Leroy | 3acd485 | 2019-08-21 15:05:55 +0000 | [diff] [blame] | 8811 | kmem_cache_destroy(btrfs_free_space_bitmap_cachep); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8812 | } |
| 8813 | |
Liu Bo | f5c29bd | 2017-11-02 17:21:50 -0600 | [diff] [blame] | 8814 | int __init btrfs_init_cachep(void) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8815 | { |
David Sterba | 837e197 | 2012-09-07 03:00:48 -0600 | [diff] [blame] | 8816 | btrfs_inode_cachep = kmem_cache_create("btrfs_inode", |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 8817 | sizeof(struct btrfs_inode), 0, |
Vladimir Davydov | 5d09705 | 2016-01-14 15:18:21 -0800 | [diff] [blame] | 8818 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT, |
| 8819 | init_once); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8820 | if (!btrfs_inode_cachep) |
| 8821 | goto fail; |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 8822 | |
David Sterba | 837e197 | 2012-09-07 03:00:48 -0600 | [diff] [blame] | 8823 | btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle", |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 8824 | sizeof(struct btrfs_trans_handle), 0, |
Nikolay Borisov | fba4b69 | 2016-06-23 21:17:08 +0300 | [diff] [blame] | 8825 | SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8826 | if (!btrfs_trans_handle_cachep) |
| 8827 | goto fail; |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 8828 | |
David Sterba | 837e197 | 2012-09-07 03:00:48 -0600 | [diff] [blame] | 8829 | btrfs_path_cachep = kmem_cache_create("btrfs_path", |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 8830 | sizeof(struct btrfs_path), 0, |
Nikolay Borisov | fba4b69 | 2016-06-23 21:17:08 +0300 | [diff] [blame] | 8831 | SLAB_MEM_SPREAD, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8832 | if (!btrfs_path_cachep) |
| 8833 | goto fail; |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 8834 | |
David Sterba | 837e197 | 2012-09-07 03:00:48 -0600 | [diff] [blame] | 8835 | btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space", |
Josef Bacik | dc89e98 | 2011-01-28 17:05:48 -0500 | [diff] [blame] | 8836 | sizeof(struct btrfs_free_space), 0, |
Nikolay Borisov | fba4b69 | 2016-06-23 21:17:08 +0300 | [diff] [blame] | 8837 | SLAB_MEM_SPREAD, NULL); |
Josef Bacik | dc89e98 | 2011-01-28 17:05:48 -0500 | [diff] [blame] | 8838 | if (!btrfs_free_space_cachep) |
| 8839 | goto fail; |
| 8840 | |
Christophe Leroy | 3acd485 | 2019-08-21 15:05:55 +0000 | [diff] [blame] | 8841 | btrfs_free_space_bitmap_cachep = kmem_cache_create("btrfs_free_space_bitmap", |
| 8842 | PAGE_SIZE, PAGE_SIZE, |
David Sterba | 2c8d6a9 | 2021-03-15 15:18:24 +0100 | [diff] [blame] | 8843 | SLAB_MEM_SPREAD, NULL); |
Christophe Leroy | 3acd485 | 2019-08-21 15:05:55 +0000 | [diff] [blame] | 8844 | if (!btrfs_free_space_bitmap_cachep) |
| 8845 | goto fail; |
| 8846 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8847 | return 0; |
| 8848 | fail: |
| 8849 | btrfs_destroy_cachep(); |
| 8850 | return -ENOMEM; |
| 8851 | } |
| 8852 | |
David Howells | a528d35 | 2017-01-31 16:46:22 +0000 | [diff] [blame] | 8853 | static int btrfs_getattr(const struct path *path, struct kstat *stat, |
| 8854 | u32 request_mask, unsigned int flags) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8855 | { |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 8856 | u64 delalloc_bytes; |
David Howells | a528d35 | 2017-01-31 16:46:22 +0000 | [diff] [blame] | 8857 | struct inode *inode = d_inode(path->dentry); |
David Sterba | fadc0d8 | 2011-11-20 07:33:38 -0500 | [diff] [blame] | 8858 | u32 blocksize = inode->i_sb->s_blocksize; |
Yonghong Song | 04a87e3 | 2017-05-12 15:07:43 -0700 | [diff] [blame] | 8859 | u32 bi_flags = BTRFS_I(inode)->flags; |
| 8860 | |
| 8861 | stat->result_mask |= STATX_BTIME; |
| 8862 | stat->btime.tv_sec = BTRFS_I(inode)->i_otime.tv_sec; |
| 8863 | stat->btime.tv_nsec = BTRFS_I(inode)->i_otime.tv_nsec; |
| 8864 | if (bi_flags & BTRFS_INODE_APPEND) |
| 8865 | stat->attributes |= STATX_ATTR_APPEND; |
| 8866 | if (bi_flags & BTRFS_INODE_COMPRESS) |
| 8867 | stat->attributes |= STATX_ATTR_COMPRESSED; |
| 8868 | if (bi_flags & BTRFS_INODE_IMMUTABLE) |
| 8869 | stat->attributes |= STATX_ATTR_IMMUTABLE; |
| 8870 | if (bi_flags & BTRFS_INODE_NODUMP) |
| 8871 | stat->attributes |= STATX_ATTR_NODUMP; |
| 8872 | |
| 8873 | stat->attributes_mask |= (STATX_ATTR_APPEND | |
| 8874 | STATX_ATTR_COMPRESSED | |
| 8875 | STATX_ATTR_IMMUTABLE | |
| 8876 | STATX_ATTR_NODUMP); |
David Sterba | fadc0d8 | 2011-11-20 07:33:38 -0500 | [diff] [blame] | 8877 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8878 | generic_fillattr(inode, stat); |
Al Viro | 0ee5dc6 | 2011-07-07 15:44:25 -0400 | [diff] [blame] | 8879 | stat->dev = BTRFS_I(inode)->root->anon_dev; |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 8880 | |
| 8881 | spin_lock(&BTRFS_I(inode)->lock); |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 8882 | delalloc_bytes = BTRFS_I(inode)->new_delalloc_bytes; |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 8883 | spin_unlock(&BTRFS_I(inode)->lock); |
David Sterba | fadc0d8 | 2011-11-20 07:33:38 -0500 | [diff] [blame] | 8884 | stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) + |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 8885 | ALIGN(delalloc_bytes, blocksize)) >> 9; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8886 | return 0; |
| 8887 | } |
| 8888 | |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 8889 | static int btrfs_rename_exchange(struct inode *old_dir, |
| 8890 | struct dentry *old_dentry, |
| 8891 | struct inode *new_dir, |
| 8892 | struct dentry *new_dentry) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8893 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8894 | struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8895 | struct btrfs_trans_handle *trans; |
| 8896 | struct btrfs_root *root = BTRFS_I(old_dir)->root; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 8897 | struct btrfs_root *dest = BTRFS_I(new_dir)->root; |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 8898 | struct inode *new_inode = new_dentry->d_inode; |
| 8899 | struct inode *old_inode = old_dentry->d_inode; |
Deepa Dinamani | 95582b0 | 2018-05-08 19:36:02 -0700 | [diff] [blame] | 8900 | struct timespec64 ctime = current_time(old_inode); |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 8901 | u64 old_ino = btrfs_ino(BTRFS_I(old_inode)); |
| 8902 | u64 new_ino = btrfs_ino(BTRFS_I(new_inode)); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 8903 | u64 old_idx = 0; |
| 8904 | u64 new_idx = 0; |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 8905 | int ret; |
Filipe Manana | 75b463d | 2020-08-11 12:43:48 +0100 | [diff] [blame] | 8906 | int ret2; |
Filipe Manana | 86e8aa0 | 2016-05-05 02:02:27 +0100 | [diff] [blame] | 8907 | bool root_log_pinned = false; |
| 8908 | bool dest_log_pinned = false; |
Josef Bacik | 0df50d4 | 2021-05-19 14:04:21 -0400 | [diff] [blame] | 8909 | bool need_abort = false; |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 8910 | |
NeilBrown | 67fece6 | 2021-08-06 14:26:24 +1000 | [diff] [blame] | 8911 | /* |
| 8912 | * For non-subvolumes allow exchange only within one subvolume, in the |
| 8913 | * same inode namespace. Two subvolumes (represented as directory) can |
| 8914 | * be exchanged as they're a logical link and have a fixed inode number. |
| 8915 | */ |
| 8916 | if (root != dest && |
| 8917 | (old_ino != BTRFS_FIRST_FREE_OBJECTID || |
| 8918 | new_ino != BTRFS_FIRST_FREE_OBJECTID)) |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 8919 | return -EXDEV; |
| 8920 | |
| 8921 | /* close the race window with snapshot create/destroy ioctl */ |
Josef Bacik | 943eb3b | 2019-11-19 13:59:20 -0500 | [diff] [blame] | 8922 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID || |
| 8923 | new_ino == BTRFS_FIRST_FREE_OBJECTID) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8924 | down_read(&fs_info->subvol_sem); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 8925 | |
| 8926 | /* |
| 8927 | * We want to reserve the absolute worst case amount of items. So if |
| 8928 | * both inodes are subvols and we need to unlink them then that would |
| 8929 | * require 4 item modifications, but if they are both normal inodes it |
| 8930 | * would require 5 item modifications, so we'll assume their normal |
| 8931 | * inodes. So 5 * 2 is 10, plus 2 for the new links, so 12 total items |
| 8932 | * should cover the worst case number of items we'll modify. |
| 8933 | */ |
| 8934 | trans = btrfs_start_transaction(root, 12); |
| 8935 | if (IS_ERR(trans)) { |
| 8936 | ret = PTR_ERR(trans); |
| 8937 | goto out_notrans; |
| 8938 | } |
| 8939 | |
Josef Bacik | 3e17409 | 2019-11-15 15:43:06 -0500 | [diff] [blame] | 8940 | if (dest != root) |
| 8941 | btrfs_record_root_in_trans(trans, dest); |
| 8942 | |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 8943 | /* |
| 8944 | * We need to find a free sequence number both in the source and |
| 8945 | * in the destination directory for the exchange. |
| 8946 | */ |
Nikolay Borisov | 877574e | 2017-02-20 13:50:33 +0200 | [diff] [blame] | 8947 | ret = btrfs_set_inode_index(BTRFS_I(new_dir), &old_idx); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 8948 | if (ret) |
| 8949 | goto out_fail; |
Nikolay Borisov | 877574e | 2017-02-20 13:50:33 +0200 | [diff] [blame] | 8950 | ret = btrfs_set_inode_index(BTRFS_I(old_dir), &new_idx); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 8951 | if (ret) |
| 8952 | goto out_fail; |
| 8953 | |
| 8954 | BTRFS_I(old_inode)->dir_index = 0ULL; |
| 8955 | BTRFS_I(new_inode)->dir_index = 0ULL; |
| 8956 | |
| 8957 | /* Reference for the source. */ |
| 8958 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) { |
| 8959 | /* force full log commit if subvolume involved. */ |
David Sterba | 9078776 | 2019-03-20 13:28:05 +0100 | [diff] [blame] | 8960 | btrfs_set_log_full_commit(trans); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 8961 | } else { |
Filipe Manana | 376e5a5 | 2016-05-05 02:08:56 +0100 | [diff] [blame] | 8962 | btrfs_pin_log_trans(root); |
| 8963 | root_log_pinned = true; |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 8964 | ret = btrfs_insert_inode_ref(trans, dest, |
| 8965 | new_dentry->d_name.name, |
| 8966 | new_dentry->d_name.len, |
| 8967 | old_ino, |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 8968 | btrfs_ino(BTRFS_I(new_dir)), |
| 8969 | old_idx); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 8970 | if (ret) |
| 8971 | goto out_fail; |
Josef Bacik | 0df50d4 | 2021-05-19 14:04:21 -0400 | [diff] [blame] | 8972 | need_abort = true; |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 8973 | } |
| 8974 | |
| 8975 | /* And now for the dest. */ |
| 8976 | if (new_ino == BTRFS_FIRST_FREE_OBJECTID) { |
| 8977 | /* force full log commit if subvolume involved. */ |
David Sterba | 9078776 | 2019-03-20 13:28:05 +0100 | [diff] [blame] | 8978 | btrfs_set_log_full_commit(trans); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 8979 | } else { |
Filipe Manana | 376e5a5 | 2016-05-05 02:08:56 +0100 | [diff] [blame] | 8980 | btrfs_pin_log_trans(dest); |
| 8981 | dest_log_pinned = true; |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 8982 | ret = btrfs_insert_inode_ref(trans, root, |
| 8983 | old_dentry->d_name.name, |
| 8984 | old_dentry->d_name.len, |
| 8985 | new_ino, |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 8986 | btrfs_ino(BTRFS_I(old_dir)), |
| 8987 | new_idx); |
Josef Bacik | 0df50d4 | 2021-05-19 14:04:21 -0400 | [diff] [blame] | 8988 | if (ret) { |
| 8989 | if (need_abort) |
| 8990 | btrfs_abort_transaction(trans, ret); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 8991 | goto out_fail; |
Josef Bacik | 0df50d4 | 2021-05-19 14:04:21 -0400 | [diff] [blame] | 8992 | } |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 8993 | } |
| 8994 | |
| 8995 | /* Update inode version and ctime/mtime. */ |
| 8996 | inode_inc_iversion(old_dir); |
| 8997 | inode_inc_iversion(new_dir); |
| 8998 | inode_inc_iversion(old_inode); |
| 8999 | inode_inc_iversion(new_inode); |
| 9000 | old_dir->i_ctime = old_dir->i_mtime = ctime; |
| 9001 | new_dir->i_ctime = new_dir->i_mtime = ctime; |
| 9002 | old_inode->i_ctime = ctime; |
| 9003 | new_inode->i_ctime = ctime; |
| 9004 | |
| 9005 | if (old_dentry->d_parent != new_dentry->d_parent) { |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 9006 | btrfs_record_unlink_dir(trans, BTRFS_I(old_dir), |
| 9007 | BTRFS_I(old_inode), 1); |
| 9008 | btrfs_record_unlink_dir(trans, BTRFS_I(new_dir), |
| 9009 | BTRFS_I(new_inode), 1); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9010 | } |
| 9011 | |
| 9012 | /* src is a subvolume */ |
| 9013 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) { |
Josef Bacik | 045d396 | 2019-12-18 17:20:27 -0500 | [diff] [blame] | 9014 | ret = btrfs_unlink_subvol(trans, old_dir, old_dentry); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9015 | } else { /* src is an inode */ |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 9016 | ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir), |
| 9017 | BTRFS_I(old_dentry->d_inode), |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9018 | old_dentry->d_name.name, |
| 9019 | old_dentry->d_name.len); |
| 9020 | if (!ret) |
| 9021 | ret = btrfs_update_inode(trans, root, old_inode); |
| 9022 | } |
| 9023 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 9024 | btrfs_abort_transaction(trans, ret); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9025 | goto out_fail; |
| 9026 | } |
| 9027 | |
| 9028 | /* dest is a subvolume */ |
| 9029 | if (new_ino == BTRFS_FIRST_FREE_OBJECTID) { |
Josef Bacik | 045d396 | 2019-12-18 17:20:27 -0500 | [diff] [blame] | 9030 | ret = btrfs_unlink_subvol(trans, new_dir, new_dentry); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9031 | } else { /* dest is an inode */ |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 9032 | ret = __btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir), |
| 9033 | BTRFS_I(new_dentry->d_inode), |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9034 | new_dentry->d_name.name, |
| 9035 | new_dentry->d_name.len); |
| 9036 | if (!ret) |
| 9037 | ret = btrfs_update_inode(trans, dest, new_inode); |
| 9038 | } |
| 9039 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 9040 | btrfs_abort_transaction(trans, ret); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9041 | goto out_fail; |
| 9042 | } |
| 9043 | |
Nikolay Borisov | db0a669 | 2017-02-20 13:51:08 +0200 | [diff] [blame] | 9044 | 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] | 9045 | new_dentry->d_name.name, |
| 9046 | new_dentry->d_name.len, 0, old_idx); |
| 9047 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 9048 | btrfs_abort_transaction(trans, ret); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9049 | goto out_fail; |
| 9050 | } |
| 9051 | |
Nikolay Borisov | db0a669 | 2017-02-20 13:51:08 +0200 | [diff] [blame] | 9052 | 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] | 9053 | old_dentry->d_name.name, |
| 9054 | old_dentry->d_name.len, 0, new_idx); |
| 9055 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 9056 | btrfs_abort_transaction(trans, ret); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9057 | goto out_fail; |
| 9058 | } |
| 9059 | |
| 9060 | if (old_inode->i_nlink == 1) |
| 9061 | BTRFS_I(old_inode)->dir_index = old_idx; |
| 9062 | if (new_inode->i_nlink == 1) |
| 9063 | BTRFS_I(new_inode)->dir_index = new_idx; |
| 9064 | |
Filipe Manana | 86e8aa0 | 2016-05-05 02:02:27 +0100 | [diff] [blame] | 9065 | if (root_log_pinned) { |
Filipe Manana | 75b463d | 2020-08-11 12:43:48 +0100 | [diff] [blame] | 9066 | btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir), |
| 9067 | new_dentry->d_parent); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9068 | btrfs_end_log_trans(root); |
Filipe Manana | 86e8aa0 | 2016-05-05 02:02:27 +0100 | [diff] [blame] | 9069 | root_log_pinned = false; |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9070 | } |
Filipe Manana | 86e8aa0 | 2016-05-05 02:02:27 +0100 | [diff] [blame] | 9071 | if (dest_log_pinned) { |
Filipe Manana | 75b463d | 2020-08-11 12:43:48 +0100 | [diff] [blame] | 9072 | btrfs_log_new_name(trans, BTRFS_I(new_inode), BTRFS_I(new_dir), |
| 9073 | old_dentry->d_parent); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9074 | btrfs_end_log_trans(dest); |
Filipe Manana | 86e8aa0 | 2016-05-05 02:02:27 +0100 | [diff] [blame] | 9075 | dest_log_pinned = false; |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9076 | } |
| 9077 | out_fail: |
Filipe Manana | 86e8aa0 | 2016-05-05 02:02:27 +0100 | [diff] [blame] | 9078 | /* |
| 9079 | * If we have pinned a log and an error happened, we unpin tasks |
| 9080 | * trying to sync the log and force them to fallback to a transaction |
| 9081 | * commit if the log currently contains any of the inodes involved in |
| 9082 | * this rename operation (to ensure we do not persist a log with an |
| 9083 | * inconsistent state for any of these inodes or leading to any |
| 9084 | * inconsistencies when replayed). If the transaction was aborted, the |
| 9085 | * abortion reason is propagated to userspace when attempting to commit |
| 9086 | * the transaction. If the log does not contain any of these inodes, we |
| 9087 | * allow the tasks to sync it. |
| 9088 | */ |
| 9089 | if (ret && (root_log_pinned || dest_log_pinned)) { |
Nikolay Borisov | 0f8939b | 2017-01-18 00:31:30 +0200 | [diff] [blame] | 9090 | if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) || |
| 9091 | btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) || |
| 9092 | btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) || |
Filipe Manana | 86e8aa0 | 2016-05-05 02:02:27 +0100 | [diff] [blame] | 9093 | (new_inode && |
Nikolay Borisov | 0f8939b | 2017-01-18 00:31:30 +0200 | [diff] [blame] | 9094 | btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation))) |
David Sterba | 9078776 | 2019-03-20 13:28:05 +0100 | [diff] [blame] | 9095 | btrfs_set_log_full_commit(trans); |
Filipe Manana | 86e8aa0 | 2016-05-05 02:02:27 +0100 | [diff] [blame] | 9096 | |
| 9097 | if (root_log_pinned) { |
| 9098 | btrfs_end_log_trans(root); |
| 9099 | root_log_pinned = false; |
| 9100 | } |
| 9101 | if (dest_log_pinned) { |
| 9102 | btrfs_end_log_trans(dest); |
| 9103 | dest_log_pinned = false; |
| 9104 | } |
| 9105 | } |
Filipe Manana | 75b463d | 2020-08-11 12:43:48 +0100 | [diff] [blame] | 9106 | ret2 = btrfs_end_transaction(trans); |
| 9107 | ret = ret ? ret : ret2; |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9108 | out_notrans: |
Josef Bacik | 943eb3b | 2019-11-19 13:59:20 -0500 | [diff] [blame] | 9109 | if (new_ino == BTRFS_FIRST_FREE_OBJECTID || |
| 9110 | old_ino == BTRFS_FIRST_FREE_OBJECTID) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9111 | up_read(&fs_info->subvol_sem); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9112 | |
| 9113 | return ret; |
| 9114 | } |
| 9115 | |
| 9116 | static int btrfs_whiteout_for_rename(struct btrfs_trans_handle *trans, |
| 9117 | struct btrfs_root *root, |
| 9118 | struct inode *dir, |
| 9119 | struct dentry *dentry) |
| 9120 | { |
| 9121 | int ret; |
| 9122 | struct inode *inode; |
| 9123 | u64 objectid; |
| 9124 | u64 index; |
| 9125 | |
| 9126 | ret = btrfs_find_free_ino(root, &objectid); |
| 9127 | if (ret) |
| 9128 | return ret; |
| 9129 | |
| 9130 | inode = btrfs_new_inode(trans, root, dir, |
| 9131 | dentry->d_name.name, |
| 9132 | dentry->d_name.len, |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 9133 | btrfs_ino(BTRFS_I(dir)), |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9134 | objectid, |
| 9135 | S_IFCHR | WHITEOUT_MODE, |
| 9136 | &index); |
| 9137 | |
| 9138 | if (IS_ERR(inode)) { |
| 9139 | ret = PTR_ERR(inode); |
| 9140 | return ret; |
| 9141 | } |
| 9142 | |
| 9143 | inode->i_op = &btrfs_special_inode_operations; |
| 9144 | init_special_inode(inode, inode->i_mode, |
| 9145 | WHITEOUT_DEV); |
| 9146 | |
| 9147 | ret = btrfs_init_inode_security(trans, inode, dir, |
| 9148 | &dentry->d_name); |
| 9149 | if (ret) |
Filipe Manana | c990161 | 2016-05-05 01:41:57 +0100 | [diff] [blame] | 9150 | goto out; |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9151 | |
Nikolay Borisov | cef415a | 2017-02-20 13:51:09 +0200 | [diff] [blame] | 9152 | ret = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, |
| 9153 | BTRFS_I(inode), 0, index); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9154 | if (ret) |
Filipe Manana | c990161 | 2016-05-05 01:41:57 +0100 | [diff] [blame] | 9155 | goto out; |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9156 | |
| 9157 | ret = btrfs_update_inode(trans, root, inode); |
Filipe Manana | c990161 | 2016-05-05 01:41:57 +0100 | [diff] [blame] | 9158 | out: |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9159 | unlock_new_inode(inode); |
Filipe Manana | c990161 | 2016-05-05 01:41:57 +0100 | [diff] [blame] | 9160 | if (ret) |
| 9161 | inode_dec_link_count(inode); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9162 | iput(inode); |
| 9163 | |
Filipe Manana | c990161 | 2016-05-05 01:41:57 +0100 | [diff] [blame] | 9164 | return ret; |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9165 | } |
| 9166 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9167 | static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry, |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9168 | struct inode *new_dir, struct dentry *new_dentry, |
| 9169 | unsigned int flags) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9170 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9171 | struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9172 | struct btrfs_trans_handle *trans; |
Filipe Manana | 5062af3 | 2016-05-05 10:26:26 +0100 | [diff] [blame] | 9173 | unsigned int trans_num_items; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9174 | struct btrfs_root *root = BTRFS_I(old_dir)->root; |
| 9175 | struct btrfs_root *dest = BTRFS_I(new_dir)->root; |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 9176 | struct inode *new_inode = d_inode(new_dentry); |
| 9177 | struct inode *old_inode = d_inode(old_dentry); |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 9178 | u64 index = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9179 | int ret; |
Filipe Manana | 75b463d | 2020-08-11 12:43:48 +0100 | [diff] [blame] | 9180 | int ret2; |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 9181 | u64 old_ino = btrfs_ino(BTRFS_I(old_inode)); |
Filipe Manana | 3dc9e8f7 | 2016-04-29 11:34:22 +0100 | [diff] [blame] | 9182 | bool log_pinned = false; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9183 | |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 9184 | if (btrfs_ino(BTRFS_I(new_dir)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) |
Yan, Zheng | f679a84 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 9185 | return -EPERM; |
| 9186 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9187 | /* we only allow rename subvolume link between subvolumes */ |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 9188 | if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest) |
Chris Mason | 3394e16 | 2008-11-17 20:42:26 -0500 | [diff] [blame] | 9189 | return -EXDEV; |
| 9190 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 9191 | if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID || |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 9192 | (new_inode && btrfs_ino(BTRFS_I(new_inode)) == BTRFS_FIRST_FREE_OBJECTID)) |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9193 | return -ENOTEMPTY; |
| 9194 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9195 | if (S_ISDIR(old_inode->i_mode) && new_inode && |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9196 | new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9197 | return -ENOTEMPTY; |
Chris Mason | 9c52057 | 2012-12-17 14:26:57 -0500 | [diff] [blame] | 9198 | |
| 9199 | |
| 9200 | /* check for collisions, even if the name isn't there */ |
Josef Bacik | 4871c15 | 2013-10-09 12:24:04 -0400 | [diff] [blame] | 9201 | ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino, |
Chris Mason | 9c52057 | 2012-12-17 14:26:57 -0500 | [diff] [blame] | 9202 | new_dentry->d_name.name, |
| 9203 | new_dentry->d_name.len); |
| 9204 | |
| 9205 | if (ret) { |
| 9206 | if (ret == -EEXIST) { |
| 9207 | /* we shouldn't get |
| 9208 | * eexist without a new_inode */ |
Dulshani Gunawardhana | fae7f21 | 2013-10-31 10:30:08 +0530 | [diff] [blame] | 9209 | if (WARN_ON(!new_inode)) { |
Chris Mason | 9c52057 | 2012-12-17 14:26:57 -0500 | [diff] [blame] | 9210 | return ret; |
| 9211 | } |
| 9212 | } else { |
| 9213 | /* maybe -EOVERFLOW */ |
| 9214 | return ret; |
| 9215 | } |
| 9216 | } |
| 9217 | ret = 0; |
| 9218 | |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 9219 | /* |
Chris Mason | 8d875f9 | 2014-08-12 10:47:42 -0700 | [diff] [blame] | 9220 | * we're using rename to replace one file with another. Start IO on it |
| 9221 | * 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] | 9222 | */ |
Chris Mason | 8d875f9 | 2014-08-12 10:47:42 -0700 | [diff] [blame] | 9223 | 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] | 9224 | filemap_flush(old_inode->i_mapping); |
| 9225 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 9226 | /* close the racy window with snapshot create/destroy ioctl */ |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 9227 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9228 | down_read(&fs_info->subvol_sem); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 9229 | /* |
| 9230 | * We want to reserve the absolute worst case amount of items. So if |
| 9231 | * both inodes are subvols and we need to unlink them then that would |
| 9232 | * require 4 item modifications, but if they are both normal inodes it |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9233 | * would require 5 item modifications, so we'll assume they are normal |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 9234 | * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items |
| 9235 | * should cover the worst case number of items we'll modify. |
Filipe Manana | 5062af3 | 2016-05-05 10:26:26 +0100 | [diff] [blame] | 9236 | * If our rename has the whiteout flag, we need more 5 units for the |
| 9237 | * new inode (1 inode item, 1 inode ref, 2 dir items and 1 xattr item |
| 9238 | * when selinux is enabled). |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 9239 | */ |
Filipe Manana | 5062af3 | 2016-05-05 10:26:26 +0100 | [diff] [blame] | 9240 | trans_num_items = 11; |
| 9241 | if (flags & RENAME_WHITEOUT) |
| 9242 | trans_num_items += 5; |
| 9243 | trans = btrfs_start_transaction(root, trans_num_items); |
Johann Lombardi | b44c59a | 2011-03-31 13:23:47 +0000 | [diff] [blame] | 9244 | if (IS_ERR(trans)) { |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9245 | ret = PTR_ERR(trans); |
| 9246 | goto out_notrans; |
| 9247 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 9248 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9249 | if (dest != root) |
| 9250 | btrfs_record_root_in_trans(trans, dest); |
| 9251 | |
Nikolay Borisov | 877574e | 2017-02-20 13:50:33 +0200 | [diff] [blame] | 9252 | ret = btrfs_set_inode_index(BTRFS_I(new_dir), &index); |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 9253 | if (ret) |
| 9254 | goto out_fail; |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 9255 | |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 9256 | BTRFS_I(old_inode)->dir_index = 0ULL; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 9257 | if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9258 | /* force full log commit if subvolume involved. */ |
David Sterba | 9078776 | 2019-03-20 13:28:05 +0100 | [diff] [blame] | 9259 | btrfs_set_log_full_commit(trans); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9260 | } else { |
Filipe Manana | c4aba95 | 2016-04-29 13:14:42 +0100 | [diff] [blame] | 9261 | btrfs_pin_log_trans(root); |
| 9262 | log_pinned = true; |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 9263 | ret = btrfs_insert_inode_ref(trans, dest, |
| 9264 | new_dentry->d_name.name, |
| 9265 | new_dentry->d_name.len, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 9266 | old_ino, |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 9267 | btrfs_ino(BTRFS_I(new_dir)), index); |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 9268 | if (ret) |
| 9269 | goto out_fail; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9270 | } |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 9271 | |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 9272 | inode_inc_iversion(old_dir); |
| 9273 | inode_inc_iversion(new_dir); |
| 9274 | inode_inc_iversion(old_inode); |
Deepa Dinamani | 04b285f | 2016-02-06 23:57:21 -0800 | [diff] [blame] | 9275 | old_dir->i_ctime = old_dir->i_mtime = |
| 9276 | new_dir->i_ctime = new_dir->i_mtime = |
Deepa Dinamani | c2050a4 | 2016-09-14 07:48:06 -0700 | [diff] [blame] | 9277 | old_inode->i_ctime = current_time(old_dir); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 9278 | |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 9279 | if (old_dentry->d_parent != new_dentry->d_parent) |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 9280 | btrfs_record_unlink_dir(trans, BTRFS_I(old_dir), |
| 9281 | BTRFS_I(old_inode), 1); |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 9282 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 9283 | if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) { |
Josef Bacik | 045d396 | 2019-12-18 17:20:27 -0500 | [diff] [blame] | 9284 | ret = btrfs_unlink_subvol(trans, old_dir, old_dentry); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9285 | } else { |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 9286 | ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir), |
| 9287 | BTRFS_I(d_inode(old_dentry)), |
Al Viro | 9298679 | 2011-03-04 17:14:37 +0000 | [diff] [blame] | 9288 | old_dentry->d_name.name, |
| 9289 | old_dentry->d_name.len); |
| 9290 | if (!ret) |
| 9291 | ret = btrfs_update_inode(trans, root, old_inode); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9292 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 9293 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 9294 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 9295 | goto out_fail; |
| 9296 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9297 | |
| 9298 | if (new_inode) { |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 9299 | inode_inc_iversion(new_inode); |
Deepa Dinamani | c2050a4 | 2016-09-14 07:48:06 -0700 | [diff] [blame] | 9300 | new_inode->i_ctime = current_time(new_inode); |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 9301 | if (unlikely(btrfs_ino(BTRFS_I(new_inode)) == |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9302 | BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) { |
Josef Bacik | 045d396 | 2019-12-18 17:20:27 -0500 | [diff] [blame] | 9303 | ret = btrfs_unlink_subvol(trans, new_dir, new_dentry); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9304 | BUG_ON(new_inode->i_nlink == 0); |
| 9305 | } else { |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 9306 | ret = btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir), |
| 9307 | BTRFS_I(d_inode(new_dentry)), |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9308 | new_dentry->d_name.name, |
| 9309 | new_dentry->d_name.len); |
| 9310 | } |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 9311 | if (!ret && new_inode->i_nlink == 0) |
Nikolay Borisov | 73f2e54 | 2017-02-20 13:50:59 +0200 | [diff] [blame] | 9312 | ret = btrfs_orphan_add(trans, |
| 9313 | BTRFS_I(d_inode(new_dentry))); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 9314 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 9315 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 9316 | goto out_fail; |
| 9317 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9318 | } |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 9319 | |
Nikolay Borisov | db0a669 | 2017-02-20 13:51:08 +0200 | [diff] [blame] | 9320 | 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] | 9321 | new_dentry->d_name.name, |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 9322 | new_dentry->d_name.len, 0, index); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 9323 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 9324 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 9325 | goto out_fail; |
| 9326 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9327 | |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 9328 | if (old_inode->i_nlink == 1) |
| 9329 | BTRFS_I(old_inode)->dir_index = index; |
| 9330 | |
Filipe Manana | 3dc9e8f7 | 2016-04-29 11:34:22 +0100 | [diff] [blame] | 9331 | if (log_pinned) { |
Filipe Manana | 75b463d | 2020-08-11 12:43:48 +0100 | [diff] [blame] | 9332 | btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir), |
| 9333 | new_dentry->d_parent); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9334 | btrfs_end_log_trans(root); |
Filipe Manana | 3dc9e8f7 | 2016-04-29 11:34:22 +0100 | [diff] [blame] | 9335 | log_pinned = false; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9336 | } |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9337 | |
| 9338 | if (flags & RENAME_WHITEOUT) { |
| 9339 | ret = btrfs_whiteout_for_rename(trans, root, old_dir, |
| 9340 | old_dentry); |
| 9341 | |
| 9342 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 9343 | btrfs_abort_transaction(trans, ret); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9344 | goto out_fail; |
| 9345 | } |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 9346 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9347 | out_fail: |
Filipe Manana | 3dc9e8f7 | 2016-04-29 11:34:22 +0100 | [diff] [blame] | 9348 | /* |
| 9349 | * If we have pinned the log and an error happened, we unpin tasks |
| 9350 | * trying to sync the log and force them to fallback to a transaction |
| 9351 | * commit if the log currently contains any of the inodes involved in |
| 9352 | * this rename operation (to ensure we do not persist a log with an |
| 9353 | * inconsistent state for any of these inodes or leading to any |
| 9354 | * inconsistencies when replayed). If the transaction was aborted, the |
| 9355 | * abortion reason is propagated to userspace when attempting to commit |
| 9356 | * the transaction. If the log does not contain any of these inodes, we |
| 9357 | * allow the tasks to sync it. |
| 9358 | */ |
| 9359 | if (ret && log_pinned) { |
Nikolay Borisov | 0f8939b | 2017-01-18 00:31:30 +0200 | [diff] [blame] | 9360 | if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) || |
| 9361 | btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) || |
| 9362 | btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) || |
Filipe Manana | 3dc9e8f7 | 2016-04-29 11:34:22 +0100 | [diff] [blame] | 9363 | (new_inode && |
Nikolay Borisov | 0f8939b | 2017-01-18 00:31:30 +0200 | [diff] [blame] | 9364 | btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation))) |
David Sterba | 9078776 | 2019-03-20 13:28:05 +0100 | [diff] [blame] | 9365 | btrfs_set_log_full_commit(trans); |
Filipe Manana | 3dc9e8f7 | 2016-04-29 11:34:22 +0100 | [diff] [blame] | 9366 | |
| 9367 | btrfs_end_log_trans(root); |
| 9368 | log_pinned = false; |
| 9369 | } |
Filipe Manana | 75b463d | 2020-08-11 12:43:48 +0100 | [diff] [blame] | 9370 | ret2 = btrfs_end_transaction(trans); |
| 9371 | ret = ret ? ret : ret2; |
Johann Lombardi | b44c59a | 2011-03-31 13:23:47 +0000 | [diff] [blame] | 9372 | out_notrans: |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 9373 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9374 | up_read(&fs_info->subvol_sem); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 9375 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9376 | return ret; |
| 9377 | } |
| 9378 | |
Miklos Szeredi | 80ace85 | 2014-07-23 15:15:32 +0200 | [diff] [blame] | 9379 | static int btrfs_rename2(struct inode *old_dir, struct dentry *old_dentry, |
| 9380 | struct inode *new_dir, struct dentry *new_dentry, |
| 9381 | unsigned int flags) |
| 9382 | { |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9383 | if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT)) |
Miklos Szeredi | 80ace85 | 2014-07-23 15:15:32 +0200 | [diff] [blame] | 9384 | return -EINVAL; |
| 9385 | |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9386 | if (flags & RENAME_EXCHANGE) |
| 9387 | return btrfs_rename_exchange(old_dir, old_dentry, new_dir, |
| 9388 | new_dentry); |
| 9389 | |
| 9390 | return btrfs_rename(old_dir, old_dentry, new_dir, new_dentry, flags); |
Miklos Szeredi | 80ace85 | 2014-07-23 15:15:32 +0200 | [diff] [blame] | 9391 | } |
| 9392 | |
Nikolay Borisov | 3a2f8c0 | 2018-04-24 17:23:59 +0300 | [diff] [blame] | 9393 | struct btrfs_delalloc_work { |
| 9394 | struct inode *inode; |
| 9395 | struct completion completion; |
| 9396 | struct list_head list; |
| 9397 | struct btrfs_work work; |
| 9398 | }; |
| 9399 | |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 9400 | static void btrfs_run_delalloc_work(struct btrfs_work *work) |
| 9401 | { |
| 9402 | struct btrfs_delalloc_work *delalloc_work; |
Josef Bacik | 9f23e28 | 2013-10-28 15:03:41 -0400 | [diff] [blame] | 9403 | struct inode *inode; |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 9404 | |
| 9405 | delalloc_work = container_of(work, struct btrfs_delalloc_work, |
| 9406 | work); |
Josef Bacik | 9f23e28 | 2013-10-28 15:03:41 -0400 | [diff] [blame] | 9407 | inode = delalloc_work->inode; |
David Sterba | 3042460 | 2015-11-27 19:27:11 +0100 | [diff] [blame] | 9408 | filemap_flush(inode->i_mapping); |
| 9409 | if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, |
| 9410 | &BTRFS_I(inode)->runtime_flags)) |
Josef Bacik | 9f23e28 | 2013-10-28 15:03:41 -0400 | [diff] [blame] | 9411 | filemap_flush(inode->i_mapping); |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 9412 | |
Nikolay Borisov | 076da91 | 2018-04-23 10:54:16 +0300 | [diff] [blame] | 9413 | iput(inode); |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 9414 | complete(&delalloc_work->completion); |
| 9415 | } |
| 9416 | |
Nikolay Borisov | 3a2f8c0 | 2018-04-24 17:23:59 +0300 | [diff] [blame] | 9417 | static struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode) |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 9418 | { |
| 9419 | struct btrfs_delalloc_work *work; |
| 9420 | |
David Sterba | 100d570 | 2015-12-08 14:39:32 +0100 | [diff] [blame] | 9421 | work = kmalloc(sizeof(*work), GFP_NOFS); |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 9422 | if (!work) |
| 9423 | return NULL; |
| 9424 | |
| 9425 | init_completion(&work->completion); |
| 9426 | INIT_LIST_HEAD(&work->list); |
| 9427 | work->inode = inode; |
Omar Sandoval | a0cac0e | 2019-09-16 11:30:57 -0700 | [diff] [blame] | 9428 | btrfs_init_work(&work->work, btrfs_run_delalloc_work, NULL, NULL); |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 9429 | |
| 9430 | return work; |
| 9431 | } |
| 9432 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 9433 | /* |
| 9434 | * some fairly slow code that needs optimization. This walks the list |
| 9435 | * of all the inodes with pending delalloc and forces them to disk. |
| 9436 | */ |
Josef Bacik | e3b5252 | 2021-01-07 17:08:30 -0500 | [diff] [blame] | 9437 | static int start_delalloc_inodes(struct btrfs_root *root, |
| 9438 | struct writeback_control *wbc, bool snapshot, |
Filipe Manana | 17243f7 | 2020-12-02 11:55:58 +0000 | [diff] [blame] | 9439 | bool in_reclaim_context) |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 9440 | { |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 9441 | struct btrfs_inode *binode; |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 9442 | struct inode *inode; |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 9443 | struct btrfs_delalloc_work *work, *next; |
| 9444 | struct list_head works; |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 9445 | struct list_head splice; |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 9446 | int ret = 0; |
Josef Bacik | e3b5252 | 2021-01-07 17:08:30 -0500 | [diff] [blame] | 9447 | bool full_flush = wbc->nr_to_write == LONG_MAX; |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 9448 | |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 9449 | INIT_LIST_HEAD(&works); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 9450 | INIT_LIST_HEAD(&splice); |
Miao Xie | 63607cc | 2013-01-22 10:50:35 +0000 | [diff] [blame] | 9451 | |
Miao Xie | 573bfb7 | 2014-03-06 13:55:03 +0800 | [diff] [blame] | 9452 | mutex_lock(&root->delalloc_mutex); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9453 | spin_lock(&root->delalloc_lock); |
| 9454 | list_splice_init(&root->delalloc_inodes, &splice); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 9455 | while (!list_empty(&splice)) { |
| 9456 | binode = list_entry(splice.next, struct btrfs_inode, |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 9457 | delalloc_inodes); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 9458 | |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9459 | list_move_tail(&binode->delalloc_inodes, |
| 9460 | &root->delalloc_inodes); |
Filipe Manana | 17243f7 | 2020-12-02 11:55:58 +0000 | [diff] [blame] | 9461 | |
| 9462 | if (in_reclaim_context && |
| 9463 | test_bit(BTRFS_INODE_NO_DELALLOC_FLUSH, &binode->runtime_flags)) |
| 9464 | continue; |
| 9465 | |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 9466 | inode = igrab(&binode->vfs_inode); |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 9467 | if (!inode) { |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9468 | cond_resched_lock(&root->delalloc_lock); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 9469 | continue; |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 9470 | } |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9471 | spin_unlock(&root->delalloc_lock); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 9472 | |
Ethan Lien | 3cd24c6 | 2018-11-01 14:49:03 +0800 | [diff] [blame] | 9473 | if (snapshot) |
| 9474 | set_bit(BTRFS_INODE_SNAPSHOT_FLUSH, |
| 9475 | &binode->runtime_flags); |
Josef Bacik | e3b5252 | 2021-01-07 17:08:30 -0500 | [diff] [blame] | 9476 | if (full_flush) { |
| 9477 | work = btrfs_alloc_delalloc_work(inode); |
| 9478 | if (!work) { |
| 9479 | iput(inode); |
| 9480 | ret = -ENOMEM; |
| 9481 | goto out; |
| 9482 | } |
| 9483 | list_add_tail(&work->list, &works); |
| 9484 | btrfs_queue_work(root->fs_info->flush_workers, |
| 9485 | &work->work); |
| 9486 | } else { |
| 9487 | ret = sync_inode(inode, wbc); |
| 9488 | if (!ret && |
| 9489 | test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, |
| 9490 | &BTRFS_I(inode)->runtime_flags)) |
| 9491 | ret = sync_inode(inode, wbc); |
| 9492 | btrfs_add_delayed_iput(inode); |
| 9493 | if (ret || wbc->nr_to_write <= 0) |
Josef Bacik | b491213 | 2020-07-21 10:22:12 -0400 | [diff] [blame] | 9494 | goto out; |
| 9495 | } |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 9496 | cond_resched(); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9497 | spin_lock(&root->delalloc_lock); |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 9498 | } |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9499 | spin_unlock(&root->delalloc_lock); |
Chris Mason | 8c8bee1 | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 9500 | |
Wang Shilong | a1ecaab | 2014-04-02 19:53:32 +0800 | [diff] [blame] | 9501 | out: |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 9502 | list_for_each_entry_safe(work, next, &works, list) { |
| 9503 | list_del_init(&work->list); |
Nikolay Borisov | 40012f9 | 2018-04-19 10:46:39 +0300 | [diff] [blame] | 9504 | wait_for_completion(&work->completion); |
| 9505 | kfree(work); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 9506 | } |
| 9507 | |
Nikolay Borisov | 81f1d39 | 2018-04-19 10:46:37 +0300 | [diff] [blame] | 9508 | if (!list_empty(&splice)) { |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9509 | spin_lock(&root->delalloc_lock); |
| 9510 | list_splice_tail(&splice, &root->delalloc_inodes); |
| 9511 | spin_unlock(&root->delalloc_lock); |
| 9512 | } |
Miao Xie | 573bfb7 | 2014-03-06 13:55:03 +0800 | [diff] [blame] | 9513 | mutex_unlock(&root->delalloc_mutex); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9514 | return ret; |
| 9515 | } |
| 9516 | |
Ethan Lien | 3cd24c6 | 2018-11-01 14:49:03 +0800 | [diff] [blame] | 9517 | int btrfs_start_delalloc_snapshot(struct btrfs_root *root) |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9518 | { |
Josef Bacik | e3b5252 | 2021-01-07 17:08:30 -0500 | [diff] [blame] | 9519 | struct writeback_control wbc = { |
| 9520 | .nr_to_write = LONG_MAX, |
| 9521 | .sync_mode = WB_SYNC_NONE, |
| 9522 | .range_start = 0, |
| 9523 | .range_end = LLONG_MAX, |
| 9524 | }; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9525 | struct btrfs_fs_info *fs_info = root->fs_info; |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9526 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9527 | if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9528 | return -EROFS; |
| 9529 | |
Josef Bacik | e3b5252 | 2021-01-07 17:08:30 -0500 | [diff] [blame] | 9530 | return start_delalloc_inodes(root, &wbc, true, false); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9531 | } |
| 9532 | |
Filipe Manana | 17243f7 | 2020-12-02 11:55:58 +0000 | [diff] [blame] | 9533 | int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, u64 nr, |
| 9534 | bool in_reclaim_context) |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9535 | { |
Josef Bacik | e3b5252 | 2021-01-07 17:08:30 -0500 | [diff] [blame] | 9536 | struct writeback_control wbc = { |
| 9537 | .nr_to_write = (nr == U64_MAX) ? LONG_MAX : (unsigned long)nr, |
| 9538 | .sync_mode = WB_SYNC_NONE, |
| 9539 | .range_start = 0, |
| 9540 | .range_end = LLONG_MAX, |
| 9541 | }; |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9542 | struct btrfs_root *root; |
| 9543 | struct list_head splice; |
| 9544 | int ret; |
| 9545 | |
Wang Shilong | 2c21b4d | 2014-01-14 19:42:20 +0800 | [diff] [blame] | 9546 | if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9547 | return -EROFS; |
| 9548 | |
| 9549 | INIT_LIST_HEAD(&splice); |
| 9550 | |
Miao Xie | 573bfb7 | 2014-03-06 13:55:03 +0800 | [diff] [blame] | 9551 | mutex_lock(&fs_info->delalloc_root_mutex); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9552 | spin_lock(&fs_info->delalloc_root_lock); |
| 9553 | list_splice_init(&fs_info->delalloc_roots, &splice); |
Miao Xie | 6c255e6 | 2014-03-06 13:55:01 +0800 | [diff] [blame] | 9554 | while (!list_empty(&splice) && nr) { |
Josef Bacik | e3b5252 | 2021-01-07 17:08:30 -0500 | [diff] [blame] | 9555 | /* |
| 9556 | * Reset nr_to_write here so we know that we're doing a full |
| 9557 | * flush. |
| 9558 | */ |
| 9559 | if (nr == U64_MAX) |
| 9560 | wbc.nr_to_write = LONG_MAX; |
| 9561 | |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9562 | root = list_first_entry(&splice, struct btrfs_root, |
| 9563 | delalloc_root); |
Josef Bacik | 0024652 | 2020-01-24 09:33:01 -0500 | [diff] [blame] | 9564 | root = btrfs_grab_root(root); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9565 | BUG_ON(!root); |
| 9566 | list_move_tail(&root->delalloc_root, |
| 9567 | &fs_info->delalloc_roots); |
| 9568 | spin_unlock(&fs_info->delalloc_root_lock); |
| 9569 | |
Josef Bacik | e3b5252 | 2021-01-07 17:08:30 -0500 | [diff] [blame] | 9570 | ret = start_delalloc_inodes(root, &wbc, false, in_reclaim_context); |
Josef Bacik | 0024652 | 2020-01-24 09:33:01 -0500 | [diff] [blame] | 9571 | btrfs_put_root(root); |
Josef Bacik | e3b5252 | 2021-01-07 17:08:30 -0500 | [diff] [blame] | 9572 | if (ret < 0 || wbc.nr_to_write <= 0) |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9573 | goto out; |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9574 | spin_lock(&fs_info->delalloc_root_lock); |
| 9575 | } |
| 9576 | spin_unlock(&fs_info->delalloc_root_lock); |
| 9577 | |
Miao Xie | 6c255e6 | 2014-03-06 13:55:01 +0800 | [diff] [blame] | 9578 | ret = 0; |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 9579 | out: |
Nikolay Borisov | 81f1d39 | 2018-04-19 10:46:37 +0300 | [diff] [blame] | 9580 | if (!list_empty(&splice)) { |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 9581 | spin_lock(&fs_info->delalloc_root_lock); |
| 9582 | list_splice_tail(&splice, &fs_info->delalloc_roots); |
| 9583 | spin_unlock(&fs_info->delalloc_root_lock); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 9584 | } |
Miao Xie | 573bfb7 | 2014-03-06 13:55:03 +0800 | [diff] [blame] | 9585 | mutex_unlock(&fs_info->delalloc_root_mutex); |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 9586 | return ret; |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 9587 | } |
| 9588 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9589 | static int btrfs_symlink(struct inode *dir, struct dentry *dentry, |
| 9590 | const char *symname) |
| 9591 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9592 | struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9593 | struct btrfs_trans_handle *trans; |
| 9594 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 9595 | struct btrfs_path *path; |
| 9596 | struct btrfs_key key; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 9597 | struct inode *inode = NULL; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9598 | int err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9599 | u64 objectid; |
Dulshani Gunawardhana | 6787125 | 2013-10-31 10:33:04 +0530 | [diff] [blame] | 9600 | u64 index = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9601 | int name_len; |
| 9602 | int datasize; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 9603 | unsigned long ptr; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9604 | struct btrfs_file_extent_item *ei; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 9605 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9606 | |
Filipe David Borba Manana | f06becc | 2013-09-16 09:53:28 +0100 | [diff] [blame] | 9607 | name_len = strlen(symname); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9608 | if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info)) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9609 | return -ENAMETOOLONG; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 9610 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 9611 | /* |
| 9612 | * 2 items for inode item and ref |
| 9613 | * 2 items for dir items |
Filipe Manana | 9269d12 | 2015-12-31 18:16:29 +0000 | [diff] [blame] | 9614 | * 1 item for updating parent inode item |
| 9615 | * 1 item for the inline extent item |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 9616 | * 1 item for xattr if selinux is on |
| 9617 | */ |
Filipe Manana | 9269d12 | 2015-12-31 18:16:29 +0000 | [diff] [blame] | 9618 | trans = btrfs_start_transaction(root, 7); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 9619 | if (IS_ERR(trans)) |
| 9620 | return PTR_ERR(trans); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 9621 | |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 9622 | err = btrfs_find_free_ino(root, &objectid); |
| 9623 | if (err) |
| 9624 | goto out_unlock; |
| 9625 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 9626 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 9627 | dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), |
| 9628 | objectid, S_IFLNK|S_IRWXUGO, &index); |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 9629 | if (IS_ERR(inode)) { |
| 9630 | err = PTR_ERR(inode); |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 9631 | inode = NULL; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9632 | goto out_unlock; |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 9633 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9634 | |
Casey Schaufler | ad19db7 | 2011-12-15 10:09:07 -0500 | [diff] [blame] | 9635 | /* |
| 9636 | * If the active LSM wants to access the inode during |
| 9637 | * d_instantiate it needs these. Smack checks to see |
| 9638 | * if the filesystem supports xattrs by looking at the |
| 9639 | * ops vector. |
| 9640 | */ |
| 9641 | inode->i_fop = &btrfs_file_operations; |
| 9642 | inode->i_op = &btrfs_file_inode_operations; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 9643 | inode->i_mapping->a_ops = &btrfs_aops; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 9644 | |
| 9645 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); |
| 9646 | if (err) |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 9647 | goto out_unlock; |
Casey Schaufler | ad19db7 | 2011-12-15 10:09:07 -0500 | [diff] [blame] | 9648 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9649 | path = btrfs_alloc_path(); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 9650 | if (!path) { |
| 9651 | err = -ENOMEM; |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 9652 | goto out_unlock; |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 9653 | } |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 9654 | key.objectid = btrfs_ino(BTRFS_I(inode)); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9655 | key.offset = 0; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 9656 | key.type = BTRFS_EXTENT_DATA_KEY; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9657 | datasize = btrfs_file_extent_calc_inline_size(name_len); |
| 9658 | err = btrfs_insert_empty_item(trans, root, path, &key, |
| 9659 | datasize); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 9660 | if (err) { |
Julia Lawall | b083916 | 2011-05-14 07:10:51 +0000 | [diff] [blame] | 9661 | btrfs_free_path(path); |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 9662 | goto out_unlock; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 9663 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 9664 | leaf = path->nodes[0]; |
| 9665 | ei = btrfs_item_ptr(leaf, path->slots[0], |
| 9666 | struct btrfs_file_extent_item); |
| 9667 | btrfs_set_file_extent_generation(leaf, ei, trans->transid); |
| 9668 | btrfs_set_file_extent_type(leaf, ei, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9669 | BTRFS_FILE_EXTENT_INLINE); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 9670 | btrfs_set_file_extent_encryption(leaf, ei, 0); |
| 9671 | btrfs_set_file_extent_compression(leaf, ei, 0); |
| 9672 | btrfs_set_file_extent_other_encoding(leaf, ei, 0); |
| 9673 | btrfs_set_file_extent_ram_bytes(leaf, ei, name_len); |
| 9674 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9675 | ptr = btrfs_file_extent_inline_start(ei); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 9676 | write_extent_buffer(leaf, symname, ptr, name_len); |
| 9677 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9678 | btrfs_free_path(path); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 9679 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9680 | inode->i_op = &btrfs_symlink_inode_operations; |
Al Viro | 21fc61c | 2015-11-17 01:07:57 -0500 | [diff] [blame] | 9681 | inode_nohighmem(inode); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 9682 | inode_set_bytes(inode, name_len); |
Nikolay Borisov | 6ef06d2 | 2017-02-20 13:50:34 +0200 | [diff] [blame] | 9683 | btrfs_i_size_write(BTRFS_I(inode), name_len); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 9684 | err = btrfs_update_inode(trans, root, inode); |
Filipe Manana | d50866d | 2015-12-31 18:08:24 +0000 | [diff] [blame] | 9685 | /* |
| 9686 | * Last step, add directory indexes for our symlink inode. This is the |
| 9687 | * last step to avoid extra cleanup of these indexes if an error happens |
| 9688 | * elsewhere above. |
| 9689 | */ |
| 9690 | if (!err) |
Nikolay Borisov | cef415a | 2017-02-20 13:51:09 +0200 | [diff] [blame] | 9691 | err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, |
| 9692 | BTRFS_I(inode), 0, index); |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 9693 | if (err) |
| 9694 | goto out_unlock; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 9695 | |
Al Viro | 1e2e547 | 2018-05-04 08:23:01 -0400 | [diff] [blame] | 9696 | d_instantiate_new(dentry, inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9697 | |
| 9698 | out_unlock: |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 9699 | btrfs_end_transaction(trans); |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 9700 | if (err && inode) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9701 | inode_dec_link_count(inode); |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 9702 | discard_new_inode(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9703 | } |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 9704 | btrfs_btree_balance_dirty(fs_info); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9705 | return err; |
| 9706 | } |
Chris Mason | 1643298 | 2008-04-10 10:23:21 -0400 | [diff] [blame] | 9707 | |
Filipe Manana | 8fccebf | 2020-09-08 11:27:20 +0100 | [diff] [blame] | 9708 | static struct btrfs_trans_handle *insert_prealloc_file_extent( |
| 9709 | struct btrfs_trans_handle *trans_in, |
Qu Wenruo | 203f44c5 | 2020-06-10 09:04:40 +0800 | [diff] [blame] | 9710 | struct inode *inode, struct btrfs_key *ins, |
| 9711 | u64 file_offset) |
| 9712 | { |
| 9713 | struct btrfs_file_extent_item stack_fi; |
Filipe Manana | bf38564 | 2020-09-08 11:27:22 +0100 | [diff] [blame] | 9714 | struct btrfs_replace_extent_info extent_info; |
Filipe Manana | 8fccebf | 2020-09-08 11:27:20 +0100 | [diff] [blame] | 9715 | struct btrfs_trans_handle *trans = trans_in; |
| 9716 | struct btrfs_path *path; |
Qu Wenruo | 203f44c5 | 2020-06-10 09:04:40 +0800 | [diff] [blame] | 9717 | u64 start = ins->objectid; |
| 9718 | u64 len = ins->offset; |
Qu Wenruo | 9729f10 | 2020-06-10 09:04:41 +0800 | [diff] [blame] | 9719 | int ret; |
Qu Wenruo | 203f44c5 | 2020-06-10 09:04:40 +0800 | [diff] [blame] | 9720 | |
| 9721 | memset(&stack_fi, 0, sizeof(stack_fi)); |
| 9722 | |
| 9723 | btrfs_set_stack_file_extent_type(&stack_fi, BTRFS_FILE_EXTENT_PREALLOC); |
| 9724 | btrfs_set_stack_file_extent_disk_bytenr(&stack_fi, start); |
| 9725 | btrfs_set_stack_file_extent_disk_num_bytes(&stack_fi, len); |
| 9726 | btrfs_set_stack_file_extent_num_bytes(&stack_fi, len); |
| 9727 | btrfs_set_stack_file_extent_ram_bytes(&stack_fi, len); |
| 9728 | btrfs_set_stack_file_extent_compression(&stack_fi, BTRFS_COMPRESS_NONE); |
| 9729 | /* Encryption and other encoding is reserved and all 0 */ |
| 9730 | |
Nikolay Borisov | 72b7d15 | 2020-06-03 08:55:18 +0300 | [diff] [blame] | 9731 | ret = btrfs_qgroup_release_data(BTRFS_I(inode), file_offset, len); |
Qu Wenruo | 9729f10 | 2020-06-10 09:04:41 +0800 | [diff] [blame] | 9732 | if (ret < 0) |
Filipe Manana | 8fccebf | 2020-09-08 11:27:20 +0100 | [diff] [blame] | 9733 | return ERR_PTR(ret); |
| 9734 | |
| 9735 | if (trans) { |
| 9736 | ret = insert_reserved_file_extent(trans, BTRFS_I(inode), |
| 9737 | file_offset, &stack_fi, ret); |
| 9738 | if (ret) |
| 9739 | return ERR_PTR(ret); |
| 9740 | return trans; |
| 9741 | } |
| 9742 | |
| 9743 | extent_info.disk_offset = start; |
| 9744 | extent_info.disk_len = len; |
| 9745 | extent_info.data_offset = 0; |
| 9746 | extent_info.data_len = len; |
| 9747 | extent_info.file_offset = file_offset; |
| 9748 | extent_info.extent_buf = (char *)&stack_fi; |
Filipe Manana | 8fccebf | 2020-09-08 11:27:20 +0100 | [diff] [blame] | 9749 | extent_info.is_new_extent = true; |
| 9750 | extent_info.qgroup_reserved = ret; |
| 9751 | extent_info.insertions = 0; |
| 9752 | |
| 9753 | path = btrfs_alloc_path(); |
| 9754 | if (!path) |
| 9755 | return ERR_PTR(-ENOMEM); |
| 9756 | |
Filipe Manana | 306bfec | 2020-09-08 11:27:23 +0100 | [diff] [blame] | 9757 | ret = btrfs_replace_file_extents(inode, path, file_offset, |
Filipe Manana | 8fccebf | 2020-09-08 11:27:20 +0100 | [diff] [blame] | 9758 | file_offset + len - 1, &extent_info, |
| 9759 | &trans); |
| 9760 | btrfs_free_path(path); |
| 9761 | if (ret) |
| 9762 | return ERR_PTR(ret); |
| 9763 | |
| 9764 | return trans; |
Qu Wenruo | 203f44c5 | 2020-06-10 09:04:40 +0800 | [diff] [blame] | 9765 | } |
Filipe Manana | 8fccebf | 2020-09-08 11:27:20 +0100 | [diff] [blame] | 9766 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 9767 | static int __btrfs_prealloc_file_range(struct inode *inode, int mode, |
| 9768 | u64 start, u64 num_bytes, u64 min_size, |
| 9769 | loff_t actual_len, u64 *alloc_hint, |
| 9770 | struct btrfs_trans_handle *trans) |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 9771 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9772 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 9773 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
| 9774 | struct extent_map *em; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 9775 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 9776 | struct btrfs_key ins; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 9777 | u64 cur_offset = start; |
Josef Bacik | b778cf9 | 2020-02-13 10:47:31 -0500 | [diff] [blame] | 9778 | u64 clear_offset = start; |
Josef Bacik | 55a61d1 | 2010-11-22 18:50:32 +0000 | [diff] [blame] | 9779 | u64 i_size; |
Chris Mason | 154ea28 | 2013-03-05 11:11:26 -0500 | [diff] [blame] | 9780 | u64 cur_bytes; |
Josef Bacik | 0b670dc | 2015-09-23 17:11:16 -0400 | [diff] [blame] | 9781 | u64 last_alloc = (u64)-1; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 9782 | int ret = 0; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 9783 | bool own_trans = true; |
Wang Xiaoguang | 1851309 | 2016-07-25 15:51:40 +0800 | [diff] [blame] | 9784 | u64 end = start + num_bytes - 1; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 9785 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 9786 | if (trans) |
| 9787 | own_trans = false; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 9788 | while (num_bytes > 0) { |
Byongho Lee | ee22184 | 2015-12-15 01:42:10 +0900 | [diff] [blame] | 9789 | cur_bytes = min_t(u64, num_bytes, SZ_256M); |
Chris Mason | 154ea28 | 2013-03-05 11:11:26 -0500 | [diff] [blame] | 9790 | cur_bytes = max(cur_bytes, min_size); |
Josef Bacik | 0b670dc | 2015-09-23 17:11:16 -0400 | [diff] [blame] | 9791 | /* |
| 9792 | * If we are severely fragmented we could end up with really |
| 9793 | * small allocations, so if the allocator is returning small |
| 9794 | * chunks lets make its job easier by only searching for those |
| 9795 | * sized chunks. |
| 9796 | */ |
| 9797 | cur_bytes = min(cur_bytes, last_alloc); |
Wang Xiaoguang | 1851309 | 2016-07-25 15:51:40 +0800 | [diff] [blame] | 9798 | ret = btrfs_reserve_extent(root, cur_bytes, cur_bytes, |
| 9799 | min_size, 0, *alloc_hint, &ins, 1, 0); |
Filipe Manana | 8fccebf | 2020-09-08 11:27:20 +0100 | [diff] [blame] | 9800 | if (ret) |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 9801 | break; |
Josef Bacik | b778cf9 | 2020-02-13 10:47:31 -0500 | [diff] [blame] | 9802 | |
| 9803 | /* |
| 9804 | * We've reserved this space, and thus converted it from |
| 9805 | * ->bytes_may_use to ->bytes_reserved. Any error that happens |
| 9806 | * from here on out we will only need to clear our reservation |
| 9807 | * for the remaining unreserved area, so advance our |
| 9808 | * clear_offset by our extent size. |
| 9809 | */ |
| 9810 | clear_offset += ins.offset; |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 9811 | |
Josef Bacik | 0b670dc | 2015-09-23 17:11:16 -0400 | [diff] [blame] | 9812 | last_alloc = ins.offset; |
Filipe Manana | 8fccebf | 2020-09-08 11:27:20 +0100 | [diff] [blame] | 9813 | trans = insert_prealloc_file_extent(trans, inode, &ins, cur_offset); |
Filipe Manana | 1afc708 | 2020-10-14 10:10:36 +0100 | [diff] [blame] | 9814 | /* |
| 9815 | * Now that we inserted the prealloc extent we can finally |
| 9816 | * decrement the number of reservations in the block group. |
| 9817 | * If we did it before, we could race with relocation and have |
| 9818 | * relocation miss the reserved extent, making it fail later. |
| 9819 | */ |
| 9820 | btrfs_dec_block_group_reservations(fs_info, ins.objectid); |
Filipe Manana | 8fccebf | 2020-09-08 11:27:20 +0100 | [diff] [blame] | 9821 | if (IS_ERR(trans)) { |
| 9822 | ret = PTR_ERR(trans); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 9823 | btrfs_free_reserved_extent(fs_info, ins.objectid, |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 9824 | ins.offset, 0); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 9825 | break; |
| 9826 | } |
Dongsheng Yang | 3119321 | 2014-12-12 16:44:35 +0800 | [diff] [blame] | 9827 | |
Nikolay Borisov | dcdbc05 | 2017-02-20 13:50:45 +0200 | [diff] [blame] | 9828 | btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset, |
Chris Mason | a1ed835 | 2009-09-11 12:27:37 -0400 | [diff] [blame] | 9829 | cur_offset + ins.offset -1, 0); |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 9830 | |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 9831 | em = alloc_extent_map(); |
| 9832 | if (!em) { |
| 9833 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, |
| 9834 | &BTRFS_I(inode)->runtime_flags); |
| 9835 | goto next; |
| 9836 | } |
| 9837 | |
| 9838 | em->start = cur_offset; |
| 9839 | em->orig_start = cur_offset; |
| 9840 | em->len = ins.offset; |
| 9841 | em->block_start = ins.objectid; |
| 9842 | em->block_len = ins.offset; |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 9843 | em->orig_block_len = ins.offset; |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 9844 | em->ram_bytes = ins.offset; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 9845 | set_bit(EXTENT_FLAG_PREALLOC, &em->flags); |
| 9846 | em->generation = trans->transid; |
| 9847 | |
| 9848 | while (1) { |
| 9849 | write_lock(&em_tree->lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 9850 | ret = add_extent_mapping(em_tree, em, 1); |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 9851 | write_unlock(&em_tree->lock); |
| 9852 | if (ret != -EEXIST) |
| 9853 | break; |
Nikolay Borisov | dcdbc05 | 2017-02-20 13:50:45 +0200 | [diff] [blame] | 9854 | btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset, |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 9855 | cur_offset + ins.offset - 1, |
| 9856 | 0); |
| 9857 | } |
| 9858 | free_extent_map(em); |
| 9859 | next: |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 9860 | num_bytes -= ins.offset; |
| 9861 | cur_offset += ins.offset; |
Yan, Zheng | efa5646 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 9862 | *alloc_hint = ins.objectid + ins.offset; |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 9863 | |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 9864 | inode_inc_iversion(inode); |
Deepa Dinamani | c2050a4 | 2016-09-14 07:48:06 -0700 | [diff] [blame] | 9865 | inode->i_ctime = current_time(inode); |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 9866 | BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 9867 | if (!(mode & FALLOC_FL_KEEP_SIZE) && |
Yan, Zheng | efa5646 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 9868 | (actual_len > inode->i_size) && |
| 9869 | (cur_offset > inode->i_size)) { |
Aneesh Kumar K.V | d1ea6a6 | 2010-01-20 07:28:54 +0000 | [diff] [blame] | 9870 | if (cur_offset > actual_len) |
Josef Bacik | 55a61d1 | 2010-11-22 18:50:32 +0000 | [diff] [blame] | 9871 | i_size = actual_len; |
Aneesh Kumar K.V | d1ea6a6 | 2010-01-20 07:28:54 +0000 | [diff] [blame] | 9872 | else |
Josef Bacik | 55a61d1 | 2010-11-22 18:50:32 +0000 | [diff] [blame] | 9873 | i_size = cur_offset; |
| 9874 | i_size_write(inode, i_size); |
Josef Bacik | d923afe | 2020-01-17 09:02:23 -0500 | [diff] [blame] | 9875 | btrfs_inode_safe_disk_i_size_write(inode, 0); |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 9876 | } |
| 9877 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 9878 | ret = btrfs_update_inode(trans, root, inode); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 9879 | |
| 9880 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 9881 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 9882 | if (own_trans) |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 9883 | btrfs_end_transaction(trans); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 9884 | break; |
| 9885 | } |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 9886 | |
Filipe Manana | 8fccebf | 2020-09-08 11:27:20 +0100 | [diff] [blame] | 9887 | if (own_trans) { |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 9888 | btrfs_end_transaction(trans); |
Filipe Manana | 8fccebf | 2020-09-08 11:27:20 +0100 | [diff] [blame] | 9889 | trans = NULL; |
| 9890 | } |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 9891 | } |
Josef Bacik | b778cf9 | 2020-02-13 10:47:31 -0500 | [diff] [blame] | 9892 | if (clear_offset < end) |
Nikolay Borisov | 25ce28c | 2020-06-03 08:55:39 +0300 | [diff] [blame] | 9893 | btrfs_free_reserved_data_space(BTRFS_I(inode), NULL, clear_offset, |
Josef Bacik | b778cf9 | 2020-02-13 10:47:31 -0500 | [diff] [blame] | 9894 | end - clear_offset + 1); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 9895 | return ret; |
| 9896 | } |
| 9897 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 9898 | int btrfs_prealloc_file_range(struct inode *inode, int mode, |
| 9899 | u64 start, u64 num_bytes, u64 min_size, |
| 9900 | loff_t actual_len, u64 *alloc_hint) |
| 9901 | { |
| 9902 | return __btrfs_prealloc_file_range(inode, mode, start, num_bytes, |
| 9903 | min_size, actual_len, alloc_hint, |
| 9904 | NULL); |
| 9905 | } |
| 9906 | |
| 9907 | int btrfs_prealloc_file_range_trans(struct inode *inode, |
| 9908 | struct btrfs_trans_handle *trans, int mode, |
| 9909 | u64 start, u64 num_bytes, u64 min_size, |
| 9910 | loff_t actual_len, u64 *alloc_hint) |
| 9911 | { |
| 9912 | return __btrfs_prealloc_file_range(inode, mode, start, num_bytes, |
| 9913 | min_size, actual_len, alloc_hint, trans); |
| 9914 | } |
| 9915 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 9916 | static int btrfs_set_page_dirty(struct page *page) |
| 9917 | { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 9918 | return __set_page_dirty_nobuffers(page); |
| 9919 | } |
| 9920 | |
Al Viro | 10556cb2 | 2011-06-20 19:28:19 -0400 | [diff] [blame] | 9921 | static int btrfs_permission(struct inode *inode, int mask) |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 9922 | { |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 9923 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Jeff Mahoney | cb6db4e | 2011-08-15 17:27:21 +0000 | [diff] [blame] | 9924 | umode_t mode = inode->i_mode; |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 9925 | |
Jeff Mahoney | cb6db4e | 2011-08-15 17:27:21 +0000 | [diff] [blame] | 9926 | if (mask & MAY_WRITE && |
| 9927 | (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) { |
| 9928 | if (btrfs_root_readonly(root)) |
| 9929 | return -EROFS; |
| 9930 | if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY) |
| 9931 | return -EACCES; |
| 9932 | } |
Al Viro | 2830ba7 | 2011-06-20 19:16:29 -0400 | [diff] [blame] | 9933 | return generic_permission(inode, mask); |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 9934 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9935 | |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 9936 | static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode) |
| 9937 | { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 9938 | struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 9939 | struct btrfs_trans_handle *trans; |
| 9940 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 9941 | struct inode *inode = NULL; |
| 9942 | u64 objectid; |
| 9943 | u64 index; |
| 9944 | int ret = 0; |
| 9945 | |
| 9946 | /* |
| 9947 | * 5 units required for adding orphan entry |
| 9948 | */ |
| 9949 | trans = btrfs_start_transaction(root, 5); |
| 9950 | if (IS_ERR(trans)) |
| 9951 | return PTR_ERR(trans); |
| 9952 | |
| 9953 | ret = btrfs_find_free_ino(root, &objectid); |
| 9954 | if (ret) |
| 9955 | goto out; |
| 9956 | |
| 9957 | inode = btrfs_new_inode(trans, root, dir, NULL, 0, |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 9958 | btrfs_ino(BTRFS_I(dir)), objectid, mode, &index); |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 9959 | if (IS_ERR(inode)) { |
| 9960 | ret = PTR_ERR(inode); |
| 9961 | inode = NULL; |
| 9962 | goto out; |
| 9963 | } |
| 9964 | |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 9965 | inode->i_fop = &btrfs_file_operations; |
| 9966 | inode->i_op = &btrfs_file_inode_operations; |
| 9967 | |
| 9968 | inode->i_mapping->a_ops = &btrfs_aops; |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 9969 | |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 9970 | ret = btrfs_init_inode_security(trans, inode, dir, NULL); |
| 9971 | if (ret) |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 9972 | goto out; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 9973 | |
| 9974 | ret = btrfs_update_inode(trans, root, inode); |
| 9975 | if (ret) |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 9976 | goto out; |
Nikolay Borisov | 73f2e54 | 2017-02-20 13:50:59 +0200 | [diff] [blame] | 9977 | ret = btrfs_orphan_add(trans, BTRFS_I(inode)); |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 9978 | if (ret) |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 9979 | goto out; |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 9980 | |
Filipe Manana | 5762b5c | 2014-08-01 00:10:32 +0100 | [diff] [blame] | 9981 | /* |
| 9982 | * We set number of links to 0 in btrfs_new_inode(), and here we set |
| 9983 | * it to 1 because d_tmpfile() will issue a warning if the count is 0, |
| 9984 | * through: |
| 9985 | * |
| 9986 | * d_tmpfile() -> inode_dec_link_count() -> drop_nlink() |
| 9987 | */ |
| 9988 | set_nlink(inode, 1); |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 9989 | d_tmpfile(dentry, inode); |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 9990 | unlock_new_inode(inode); |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 9991 | mark_inode_dirty(inode); |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 9992 | out: |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 9993 | btrfs_end_transaction(trans); |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 9994 | if (ret && inode) |
| 9995 | discard_new_inode(inode); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 9996 | btrfs_btree_balance_dirty(fs_info); |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 9997 | return ret; |
| 9998 | } |
| 9999 | |
David Sterba | 5cdc84b | 2018-07-18 20:32:52 +0200 | [diff] [blame] | 10000 | 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] | 10001 | { |
David Sterba | 5cdc84b | 2018-07-18 20:32:52 +0200 | [diff] [blame] | 10002 | struct inode *inode = tree->private_data; |
Josef Bacik | c6100a4 | 2017-05-05 11:57:13 -0400 | [diff] [blame] | 10003 | unsigned long index = start >> PAGE_SHIFT; |
| 10004 | unsigned long end_index = end >> PAGE_SHIFT; |
| 10005 | struct page *page; |
| 10006 | |
| 10007 | while (index <= end_index) { |
| 10008 | page = find_get_page(inode->i_mapping, index); |
| 10009 | ASSERT(page); /* Pages should be in the extent_io_tree */ |
| 10010 | set_page_writeback(page); |
| 10011 | put_page(page); |
| 10012 | index++; |
| 10013 | } |
| 10014 | } |
| 10015 | |
Omar Sandoval | ed46ff3 | 2016-11-03 10:28:14 -0700 | [diff] [blame] | 10016 | #ifdef CONFIG_SWAP |
| 10017 | /* |
| 10018 | * Add an entry indicating a block group or device which is pinned by a |
| 10019 | * swapfile. Returns 0 on success, 1 if there is already an entry for it, or a |
| 10020 | * negative errno on failure. |
| 10021 | */ |
| 10022 | static int btrfs_add_swapfile_pin(struct inode *inode, void *ptr, |
| 10023 | bool is_block_group) |
| 10024 | { |
| 10025 | struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info; |
| 10026 | struct btrfs_swapfile_pin *sp, *entry; |
| 10027 | struct rb_node **p; |
| 10028 | struct rb_node *parent = NULL; |
| 10029 | |
| 10030 | sp = kmalloc(sizeof(*sp), GFP_NOFS); |
| 10031 | if (!sp) |
| 10032 | return -ENOMEM; |
| 10033 | sp->ptr = ptr; |
| 10034 | sp->inode = inode; |
| 10035 | sp->is_block_group = is_block_group; |
Filipe Manana | 501fdd1 | 2021-02-05 12:55:37 +0000 | [diff] [blame] | 10036 | sp->bg_extent_count = 1; |
Omar Sandoval | ed46ff3 | 2016-11-03 10:28:14 -0700 | [diff] [blame] | 10037 | |
| 10038 | spin_lock(&fs_info->swapfile_pins_lock); |
| 10039 | p = &fs_info->swapfile_pins.rb_node; |
| 10040 | while (*p) { |
| 10041 | parent = *p; |
| 10042 | entry = rb_entry(parent, struct btrfs_swapfile_pin, node); |
| 10043 | if (sp->ptr < entry->ptr || |
| 10044 | (sp->ptr == entry->ptr && sp->inode < entry->inode)) { |
| 10045 | p = &(*p)->rb_left; |
| 10046 | } else if (sp->ptr > entry->ptr || |
| 10047 | (sp->ptr == entry->ptr && sp->inode > entry->inode)) { |
| 10048 | p = &(*p)->rb_right; |
| 10049 | } else { |
Filipe Manana | 501fdd1 | 2021-02-05 12:55:37 +0000 | [diff] [blame] | 10050 | if (is_block_group) |
| 10051 | entry->bg_extent_count++; |
Omar Sandoval | ed46ff3 | 2016-11-03 10:28:14 -0700 | [diff] [blame] | 10052 | spin_unlock(&fs_info->swapfile_pins_lock); |
| 10053 | kfree(sp); |
| 10054 | return 1; |
| 10055 | } |
| 10056 | } |
| 10057 | rb_link_node(&sp->node, parent, p); |
| 10058 | rb_insert_color(&sp->node, &fs_info->swapfile_pins); |
| 10059 | spin_unlock(&fs_info->swapfile_pins_lock); |
| 10060 | return 0; |
| 10061 | } |
| 10062 | |
| 10063 | /* Free all of the entries pinned by this swapfile. */ |
| 10064 | static void btrfs_free_swapfile_pins(struct inode *inode) |
| 10065 | { |
| 10066 | struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info; |
| 10067 | struct btrfs_swapfile_pin *sp; |
| 10068 | struct rb_node *node, *next; |
| 10069 | |
| 10070 | spin_lock(&fs_info->swapfile_pins_lock); |
| 10071 | node = rb_first(&fs_info->swapfile_pins); |
| 10072 | while (node) { |
| 10073 | next = rb_next(node); |
| 10074 | sp = rb_entry(node, struct btrfs_swapfile_pin, node); |
| 10075 | if (sp->inode == inode) { |
| 10076 | rb_erase(&sp->node, &fs_info->swapfile_pins); |
Filipe Manana | 501fdd1 | 2021-02-05 12:55:37 +0000 | [diff] [blame] | 10077 | if (sp->is_block_group) { |
| 10078 | btrfs_dec_block_group_swap_extents(sp->ptr, |
| 10079 | sp->bg_extent_count); |
Omar Sandoval | ed46ff3 | 2016-11-03 10:28:14 -0700 | [diff] [blame] | 10080 | btrfs_put_block_group(sp->ptr); |
Filipe Manana | 501fdd1 | 2021-02-05 12:55:37 +0000 | [diff] [blame] | 10081 | } |
Omar Sandoval | ed46ff3 | 2016-11-03 10:28:14 -0700 | [diff] [blame] | 10082 | kfree(sp); |
| 10083 | } |
| 10084 | node = next; |
| 10085 | } |
| 10086 | spin_unlock(&fs_info->swapfile_pins_lock); |
| 10087 | } |
| 10088 | |
| 10089 | struct btrfs_swap_info { |
| 10090 | u64 start; |
| 10091 | u64 block_start; |
| 10092 | u64 block_len; |
| 10093 | u64 lowest_ppage; |
| 10094 | u64 highest_ppage; |
| 10095 | unsigned long nr_pages; |
| 10096 | int nr_extents; |
| 10097 | }; |
| 10098 | |
| 10099 | static int btrfs_add_swap_extent(struct swap_info_struct *sis, |
| 10100 | struct btrfs_swap_info *bsi) |
| 10101 | { |
| 10102 | unsigned long nr_pages; |
Filipe Manana | f8c3ec2 | 2021-12-16 15:00:32 +0000 | [diff] [blame] | 10103 | unsigned long max_pages; |
Omar Sandoval | ed46ff3 | 2016-11-03 10:28:14 -0700 | [diff] [blame] | 10104 | u64 first_ppage, first_ppage_reported, next_ppage; |
| 10105 | int ret; |
| 10106 | |
Filipe Manana | f8c3ec2 | 2021-12-16 15:00:32 +0000 | [diff] [blame] | 10107 | /* |
| 10108 | * Our swapfile may have had its size extended after the swap header was |
| 10109 | * written. In that case activating the swapfile should not go beyond |
| 10110 | * the max size set in the swap header. |
| 10111 | */ |
| 10112 | if (bsi->nr_pages >= sis->max) |
| 10113 | return 0; |
| 10114 | |
| 10115 | max_pages = sis->max - bsi->nr_pages; |
Omar Sandoval | ed46ff3 | 2016-11-03 10:28:14 -0700 | [diff] [blame] | 10116 | first_ppage = ALIGN(bsi->block_start, PAGE_SIZE) >> PAGE_SHIFT; |
| 10117 | next_ppage = ALIGN_DOWN(bsi->block_start + bsi->block_len, |
| 10118 | PAGE_SIZE) >> PAGE_SHIFT; |
| 10119 | |
| 10120 | if (first_ppage >= next_ppage) |
| 10121 | return 0; |
| 10122 | nr_pages = next_ppage - first_ppage; |
Filipe Manana | f8c3ec2 | 2021-12-16 15:00:32 +0000 | [diff] [blame] | 10123 | nr_pages = min(nr_pages, max_pages); |
Omar Sandoval | ed46ff3 | 2016-11-03 10:28:14 -0700 | [diff] [blame] | 10124 | |
| 10125 | first_ppage_reported = first_ppage; |
| 10126 | if (bsi->start == 0) |
| 10127 | first_ppage_reported++; |
| 10128 | if (bsi->lowest_ppage > first_ppage_reported) |
| 10129 | bsi->lowest_ppage = first_ppage_reported; |
| 10130 | if (bsi->highest_ppage < (next_ppage - 1)) |
| 10131 | bsi->highest_ppage = next_ppage - 1; |
| 10132 | |
| 10133 | ret = add_swap_extent(sis, bsi->nr_pages, nr_pages, first_ppage); |
| 10134 | if (ret < 0) |
| 10135 | return ret; |
| 10136 | bsi->nr_extents += ret; |
| 10137 | bsi->nr_pages += nr_pages; |
| 10138 | return 0; |
| 10139 | } |
| 10140 | |
| 10141 | static void btrfs_swap_deactivate(struct file *file) |
| 10142 | { |
| 10143 | struct inode *inode = file_inode(file); |
| 10144 | |
| 10145 | btrfs_free_swapfile_pins(inode); |
| 10146 | atomic_dec(&BTRFS_I(inode)->root->nr_swapfiles); |
| 10147 | } |
| 10148 | |
| 10149 | static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file, |
| 10150 | sector_t *span) |
| 10151 | { |
| 10152 | struct inode *inode = file_inode(file); |
Filipe Manana | 6fc9e58 | 2021-02-05 12:55:38 +0000 | [diff] [blame] | 10153 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 10154 | struct btrfs_fs_info *fs_info = root->fs_info; |
Omar Sandoval | ed46ff3 | 2016-11-03 10:28:14 -0700 | [diff] [blame] | 10155 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
| 10156 | struct extent_state *cached_state = NULL; |
| 10157 | struct extent_map *em = NULL; |
| 10158 | struct btrfs_device *device = NULL; |
| 10159 | struct btrfs_swap_info bsi = { |
| 10160 | .lowest_ppage = (sector_t)-1ULL, |
| 10161 | }; |
| 10162 | int ret = 0; |
| 10163 | u64 isize; |
| 10164 | u64 start; |
| 10165 | |
| 10166 | /* |
| 10167 | * If the swap file was just created, make sure delalloc is done. If the |
| 10168 | * file changes again after this, the user is doing something stupid and |
| 10169 | * we don't really care. |
| 10170 | */ |
| 10171 | ret = btrfs_wait_ordered_range(inode, 0, (u64)-1); |
| 10172 | if (ret) |
| 10173 | return ret; |
| 10174 | |
| 10175 | /* |
| 10176 | * The inode is locked, so these flags won't change after we check them. |
| 10177 | */ |
| 10178 | if (BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS) { |
| 10179 | btrfs_warn(fs_info, "swapfile must not be compressed"); |
| 10180 | return -EINVAL; |
| 10181 | } |
| 10182 | if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW)) { |
| 10183 | btrfs_warn(fs_info, "swapfile must not be copy-on-write"); |
| 10184 | return -EINVAL; |
| 10185 | } |
| 10186 | if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) { |
| 10187 | btrfs_warn(fs_info, "swapfile must not be checksummed"); |
| 10188 | return -EINVAL; |
| 10189 | } |
| 10190 | |
| 10191 | /* |
| 10192 | * Balance or device remove/replace/resize can move stuff around from |
Goldwyn Rodrigues | c3e1f96 | 2020-08-25 10:02:32 -0500 | [diff] [blame] | 10193 | * under us. The exclop protection makes sure they aren't running/won't |
| 10194 | * run concurrently while we are mapping the swap extents, and |
| 10195 | * fs_info->swapfile_pins prevents them from running while the swap |
| 10196 | * file is active and moving the extents. Note that this also prevents |
| 10197 | * 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] | 10198 | * really worth the trouble to allow it. |
| 10199 | */ |
Goldwyn Rodrigues | c3e1f96 | 2020-08-25 10:02:32 -0500 | [diff] [blame] | 10200 | if (!btrfs_exclop_start(fs_info, BTRFS_EXCLOP_SWAP_ACTIVATE)) { |
Omar Sandoval | ed46ff3 | 2016-11-03 10:28:14 -0700 | [diff] [blame] | 10201 | btrfs_warn(fs_info, |
| 10202 | "cannot activate swapfile while exclusive operation is running"); |
| 10203 | return -EBUSY; |
| 10204 | } |
Filipe Manana | 6fc9e58 | 2021-02-05 12:55:38 +0000 | [diff] [blame] | 10205 | |
| 10206 | /* |
| 10207 | * Prevent snapshot creation while we are activating the swap file. |
| 10208 | * We do not want to race with snapshot creation. If snapshot creation |
| 10209 | * already started before we bumped nr_swapfiles from 0 to 1 and |
| 10210 | * completes before the first write into the swap file after it is |
| 10211 | * activated, than that write would fallback to COW. |
| 10212 | */ |
| 10213 | if (!btrfs_drew_try_write_lock(&root->snapshot_lock)) { |
| 10214 | btrfs_exclop_finish(fs_info); |
| 10215 | btrfs_warn(fs_info, |
| 10216 | "cannot activate swapfile because snapshot creation is in progress"); |
| 10217 | return -EINVAL; |
| 10218 | } |
Omar Sandoval | ed46ff3 | 2016-11-03 10:28:14 -0700 | [diff] [blame] | 10219 | /* |
| 10220 | * Snapshots can create extents which require COW even if NODATACOW is |
| 10221 | * set. We use this counter to prevent snapshots. We must increment it |
| 10222 | * before walking the extents because we don't want a concurrent |
| 10223 | * snapshot to run after we've already checked the extents. |
Kaiwen Hu | a044bca | 2022-03-23 15:10:32 +0800 | [diff] [blame] | 10224 | * |
| 10225 | * It is possible that subvolume is marked for deletion but still not |
| 10226 | * removed yet. To prevent this race, we check the root status before |
| 10227 | * activating the swapfile. |
Omar Sandoval | ed46ff3 | 2016-11-03 10:28:14 -0700 | [diff] [blame] | 10228 | */ |
Kaiwen Hu | a044bca | 2022-03-23 15:10:32 +0800 | [diff] [blame] | 10229 | spin_lock(&root->root_item_lock); |
| 10230 | if (btrfs_root_dead(root)) { |
| 10231 | spin_unlock(&root->root_item_lock); |
| 10232 | |
| 10233 | btrfs_exclop_finish(fs_info); |
| 10234 | btrfs_warn(fs_info, |
| 10235 | "cannot activate swapfile because subvolume %llu is being deleted", |
| 10236 | root->root_key.objectid); |
| 10237 | return -EPERM; |
| 10238 | } |
Filipe Manana | 6fc9e58 | 2021-02-05 12:55:38 +0000 | [diff] [blame] | 10239 | atomic_inc(&root->nr_swapfiles); |
Kaiwen Hu | a044bca | 2022-03-23 15:10:32 +0800 | [diff] [blame] | 10240 | spin_unlock(&root->root_item_lock); |
Omar Sandoval | ed46ff3 | 2016-11-03 10:28:14 -0700 | [diff] [blame] | 10241 | |
| 10242 | isize = ALIGN_DOWN(inode->i_size, fs_info->sectorsize); |
| 10243 | |
| 10244 | lock_extent_bits(io_tree, 0, isize - 1, &cached_state); |
| 10245 | start = 0; |
| 10246 | while (start < isize) { |
| 10247 | u64 logical_block_start, physical_block_start; |
David Sterba | 32da5386 | 2019-10-29 19:20:18 +0100 | [diff] [blame] | 10248 | struct btrfs_block_group *bg; |
Omar Sandoval | ed46ff3 | 2016-11-03 10:28:14 -0700 | [diff] [blame] | 10249 | u64 len = isize - start; |
| 10250 | |
Omar Sandoval | 39b07b5 | 2019-12-02 17:34:23 -0800 | [diff] [blame] | 10251 | em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len); |
Omar Sandoval | ed46ff3 | 2016-11-03 10:28:14 -0700 | [diff] [blame] | 10252 | if (IS_ERR(em)) { |
| 10253 | ret = PTR_ERR(em); |
| 10254 | goto out; |
| 10255 | } |
| 10256 | |
| 10257 | if (em->block_start == EXTENT_MAP_HOLE) { |
| 10258 | btrfs_warn(fs_info, "swapfile must not have holes"); |
| 10259 | ret = -EINVAL; |
| 10260 | goto out; |
| 10261 | } |
| 10262 | if (em->block_start == EXTENT_MAP_INLINE) { |
| 10263 | /* |
| 10264 | * It's unlikely we'll ever actually find ourselves |
| 10265 | * here, as a file small enough to fit inline won't be |
| 10266 | * big enough to store more than the swap header, but in |
| 10267 | * case something changes in the future, let's catch it |
| 10268 | * here rather than later. |
| 10269 | */ |
| 10270 | btrfs_warn(fs_info, "swapfile must not be inline"); |
| 10271 | ret = -EINVAL; |
| 10272 | goto out; |
| 10273 | } |
| 10274 | if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) { |
| 10275 | btrfs_warn(fs_info, "swapfile must not be compressed"); |
| 10276 | ret = -EINVAL; |
| 10277 | goto out; |
| 10278 | } |
| 10279 | |
| 10280 | logical_block_start = em->block_start + (start - em->start); |
| 10281 | len = min(len, em->len - (start - em->start)); |
| 10282 | free_extent_map(em); |
| 10283 | em = NULL; |
| 10284 | |
Boris Burkov | a84d5d4 | 2020-08-18 11:00:05 -0700 | [diff] [blame] | 10285 | ret = can_nocow_extent(inode, start, &len, NULL, NULL, NULL, true); |
Omar Sandoval | ed46ff3 | 2016-11-03 10:28:14 -0700 | [diff] [blame] | 10286 | if (ret < 0) { |
| 10287 | goto out; |
| 10288 | } else if (ret) { |
| 10289 | ret = 0; |
| 10290 | } else { |
| 10291 | btrfs_warn(fs_info, |
| 10292 | "swapfile must not be copy-on-write"); |
| 10293 | ret = -EINVAL; |
| 10294 | goto out; |
| 10295 | } |
| 10296 | |
| 10297 | em = btrfs_get_chunk_map(fs_info, logical_block_start, len); |
| 10298 | if (IS_ERR(em)) { |
| 10299 | ret = PTR_ERR(em); |
| 10300 | goto out; |
| 10301 | } |
| 10302 | |
| 10303 | if (em->map_lookup->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) { |
| 10304 | btrfs_warn(fs_info, |
| 10305 | "swapfile must have single data profile"); |
| 10306 | ret = -EINVAL; |
| 10307 | goto out; |
| 10308 | } |
| 10309 | |
| 10310 | if (device == NULL) { |
| 10311 | device = em->map_lookup->stripes[0].dev; |
| 10312 | ret = btrfs_add_swapfile_pin(inode, device, false); |
| 10313 | if (ret == 1) |
| 10314 | ret = 0; |
| 10315 | else if (ret) |
| 10316 | goto out; |
| 10317 | } else if (device != em->map_lookup->stripes[0].dev) { |
| 10318 | btrfs_warn(fs_info, "swapfile must be on one device"); |
| 10319 | ret = -EINVAL; |
| 10320 | goto out; |
| 10321 | } |
| 10322 | |
| 10323 | physical_block_start = (em->map_lookup->stripes[0].physical + |
| 10324 | (logical_block_start - em->start)); |
| 10325 | len = min(len, em->len - (logical_block_start - em->start)); |
| 10326 | free_extent_map(em); |
| 10327 | em = NULL; |
| 10328 | |
| 10329 | bg = btrfs_lookup_block_group(fs_info, logical_block_start); |
| 10330 | if (!bg) { |
| 10331 | btrfs_warn(fs_info, |
| 10332 | "could not find block group containing swapfile"); |
| 10333 | ret = -EINVAL; |
| 10334 | goto out; |
| 10335 | } |
| 10336 | |
Filipe Manana | 501fdd1 | 2021-02-05 12:55:37 +0000 | [diff] [blame] | 10337 | if (!btrfs_inc_block_group_swap_extents(bg)) { |
| 10338 | btrfs_warn(fs_info, |
| 10339 | "block group for swapfile at %llu is read-only%s", |
| 10340 | bg->start, |
| 10341 | atomic_read(&fs_info->scrubs_running) ? |
| 10342 | " (scrub running)" : ""); |
| 10343 | btrfs_put_block_group(bg); |
| 10344 | ret = -EINVAL; |
| 10345 | goto out; |
| 10346 | } |
| 10347 | |
Omar Sandoval | ed46ff3 | 2016-11-03 10:28:14 -0700 | [diff] [blame] | 10348 | ret = btrfs_add_swapfile_pin(inode, bg, true); |
| 10349 | if (ret) { |
| 10350 | btrfs_put_block_group(bg); |
| 10351 | if (ret == 1) |
| 10352 | ret = 0; |
| 10353 | else |
| 10354 | goto out; |
| 10355 | } |
| 10356 | |
| 10357 | if (bsi.block_len && |
| 10358 | bsi.block_start + bsi.block_len == physical_block_start) { |
| 10359 | bsi.block_len += len; |
| 10360 | } else { |
| 10361 | if (bsi.block_len) { |
| 10362 | ret = btrfs_add_swap_extent(sis, &bsi); |
| 10363 | if (ret) |
| 10364 | goto out; |
| 10365 | } |
| 10366 | bsi.start = start; |
| 10367 | bsi.block_start = physical_block_start; |
| 10368 | bsi.block_len = len; |
| 10369 | } |
| 10370 | |
| 10371 | start += len; |
| 10372 | } |
| 10373 | |
| 10374 | if (bsi.block_len) |
| 10375 | ret = btrfs_add_swap_extent(sis, &bsi); |
| 10376 | |
| 10377 | out: |
| 10378 | if (!IS_ERR_OR_NULL(em)) |
| 10379 | free_extent_map(em); |
| 10380 | |
| 10381 | unlock_extent_cached(io_tree, 0, isize - 1, &cached_state); |
| 10382 | |
| 10383 | if (ret) |
| 10384 | btrfs_swap_deactivate(file); |
| 10385 | |
Filipe Manana | 6fc9e58 | 2021-02-05 12:55:38 +0000 | [diff] [blame] | 10386 | btrfs_drew_write_unlock(&root->snapshot_lock); |
| 10387 | |
Goldwyn Rodrigues | c3e1f96 | 2020-08-25 10:02:32 -0500 | [diff] [blame] | 10388 | btrfs_exclop_finish(fs_info); |
Omar Sandoval | ed46ff3 | 2016-11-03 10:28:14 -0700 | [diff] [blame] | 10389 | |
| 10390 | if (ret) |
| 10391 | return ret; |
| 10392 | |
| 10393 | if (device) |
| 10394 | sis->bdev = device->bdev; |
| 10395 | *span = bsi.highest_ppage - bsi.lowest_ppage + 1; |
| 10396 | sis->max = bsi.nr_pages; |
| 10397 | sis->pages = bsi.nr_pages - 1; |
| 10398 | sis->highest_bit = bsi.nr_pages - 1; |
| 10399 | return bsi.nr_extents; |
| 10400 | } |
| 10401 | #else |
| 10402 | static void btrfs_swap_deactivate(struct file *file) |
| 10403 | { |
| 10404 | } |
| 10405 | |
| 10406 | static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file, |
| 10407 | sector_t *span) |
| 10408 | { |
| 10409 | return -EOPNOTSUPP; |
| 10410 | } |
| 10411 | #endif |
| 10412 | |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 10413 | static const struct inode_operations btrfs_dir_inode_operations = { |
Chris Mason | 3394e16 | 2008-11-17 20:42:26 -0500 | [diff] [blame] | 10414 | .getattr = btrfs_getattr, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10415 | .lookup = btrfs_lookup, |
| 10416 | .create = btrfs_create, |
| 10417 | .unlink = btrfs_unlink, |
| 10418 | .link = btrfs_link, |
| 10419 | .mkdir = btrfs_mkdir, |
| 10420 | .rmdir = btrfs_rmdir, |
Miklos Szeredi | 2773bf0 | 2016-09-27 11:03:58 +0200 | [diff] [blame] | 10421 | .rename = btrfs_rename2, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10422 | .symlink = btrfs_symlink, |
| 10423 | .setattr = btrfs_setattr, |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 10424 | .mknod = btrfs_mknod, |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 10425 | .listxattr = btrfs_listxattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 10426 | .permission = btrfs_permission, |
Christoph Hellwig | 4e34e71 | 2011-07-23 17:37:31 +0200 | [diff] [blame] | 10427 | .get_acl = btrfs_get_acl, |
Christoph Hellwig | 996a710 | 2013-12-20 05:16:43 -0800 | [diff] [blame] | 10428 | .set_acl = btrfs_set_acl, |
Guangyu Sun | 93fd63c | 2013-09-16 10:42:03 -0700 | [diff] [blame] | 10429 | .update_time = btrfs_update_time, |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 10430 | .tmpfile = btrfs_tmpfile, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10431 | }; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 10432 | |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 10433 | static const struct file_operations btrfs_dir_file_operations = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10434 | .llseek = generic_file_llseek, |
| 10435 | .read = generic_read_dir, |
Omar Sandoval | 02dbfc9 | 2016-05-20 13:50:33 -0700 | [diff] [blame] | 10436 | .iterate_shared = btrfs_real_readdir, |
Josef Bacik | 23b5ec7 | 2017-07-24 15:14:25 -0400 | [diff] [blame] | 10437 | .open = btrfs_opendir, |
Christoph Hellwig | 34287aa | 2007-09-14 10:22:47 -0400 | [diff] [blame] | 10438 | .unlocked_ioctl = btrfs_ioctl, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10439 | #ifdef CONFIG_COMPAT |
Luke Dashjr | 4c63c24 | 2015-10-29 08:22:21 +0000 | [diff] [blame] | 10440 | .compat_ioctl = btrfs_compat_ioctl, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10441 | #endif |
Sage Weil | 6bf13c0 | 2008-06-10 10:07:39 -0400 | [diff] [blame] | 10442 | .release = btrfs_release_file, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 10443 | .fsync = btrfs_sync_file, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10444 | }; |
| 10445 | |
Chris Mason | 3505439 | 2009-01-21 13:11:13 -0500 | [diff] [blame] | 10446 | /* |
| 10447 | * btrfs doesn't support the bmap operation because swapfiles |
| 10448 | * use bmap to make a mapping of extents in the file. They assume |
| 10449 | * these extents won't change over the life of the file and they |
| 10450 | * use the bmap result to do IO directly to the drive. |
| 10451 | * |
| 10452 | * the btrfs bmap call would return logical addresses that aren't |
| 10453 | * suitable for IO and they also will change frequently as COW |
| 10454 | * operations happen. So, swapfile + btrfs == corruption. |
| 10455 | * |
| 10456 | * For now we're avoiding this by dropping bmap. |
| 10457 | */ |
Alexey Dobriyan | 7f09410 | 2009-09-21 17:01:10 -0700 | [diff] [blame] | 10458 | static const struct address_space_operations btrfs_aops = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10459 | .readpage = btrfs_readpage, |
| 10460 | .writepage = btrfs_writepage, |
Chris Mason | b293f02e | 2007-11-01 19:45:34 -0400 | [diff] [blame] | 10461 | .writepages = btrfs_writepages, |
Matthew Wilcox (Oracle) | ba206a0 | 2020-06-01 21:47:05 -0700 | [diff] [blame] | 10462 | .readahead = btrfs_readahead, |
Goldwyn Rodrigues | f85781f | 2020-08-17 11:18:21 -0500 | [diff] [blame] | 10463 | .direct_IO = noop_direct_IO, |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 10464 | .invalidatepage = btrfs_invalidatepage, |
| 10465 | .releasepage = btrfs_releasepage, |
Roman Gushchin | f8e6608 | 2020-03-04 16:57:35 -0800 | [diff] [blame] | 10466 | #ifdef CONFIG_MIGRATION |
| 10467 | .migratepage = btrfs_migratepage, |
| 10468 | #endif |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 10469 | .set_page_dirty = btrfs_set_page_dirty, |
Andi Kleen | 465fdd9 | 2009-09-16 11:50:18 +0200 | [diff] [blame] | 10470 | .error_remove_page = generic_error_remove_page, |
Omar Sandoval | ed46ff3 | 2016-11-03 10:28:14 -0700 | [diff] [blame] | 10471 | .swap_activate = btrfs_swap_activate, |
| 10472 | .swap_deactivate = btrfs_swap_deactivate, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10473 | }; |
| 10474 | |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 10475 | static const struct inode_operations btrfs_file_inode_operations = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10476 | .getattr = btrfs_getattr, |
| 10477 | .setattr = btrfs_setattr, |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 10478 | .listxattr = btrfs_listxattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 10479 | .permission = btrfs_permission, |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 10480 | .fiemap = btrfs_fiemap, |
Christoph Hellwig | 4e34e71 | 2011-07-23 17:37:31 +0200 | [diff] [blame] | 10481 | .get_acl = btrfs_get_acl, |
Christoph Hellwig | 996a710 | 2013-12-20 05:16:43 -0800 | [diff] [blame] | 10482 | .set_acl = btrfs_set_acl, |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 10483 | .update_time = btrfs_update_time, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10484 | }; |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 10485 | static const struct inode_operations btrfs_special_inode_operations = { |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 10486 | .getattr = btrfs_getattr, |
| 10487 | .setattr = btrfs_setattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 10488 | .permission = btrfs_permission, |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 10489 | .listxattr = btrfs_listxattr, |
Christoph Hellwig | 4e34e71 | 2011-07-23 17:37:31 +0200 | [diff] [blame] | 10490 | .get_acl = btrfs_get_acl, |
Christoph Hellwig | 996a710 | 2013-12-20 05:16:43 -0800 | [diff] [blame] | 10491 | .set_acl = btrfs_set_acl, |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 10492 | .update_time = btrfs_update_time, |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 10493 | }; |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 10494 | static const struct inode_operations btrfs_symlink_inode_operations = { |
Al Viro | 6b25539 | 2015-11-17 10:20:54 -0500 | [diff] [blame] | 10495 | .get_link = page_get_link, |
Li Zefan | f209561 | 2010-11-19 02:05:24 +0000 | [diff] [blame] | 10496 | .getattr = btrfs_getattr, |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 10497 | .setattr = btrfs_setattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 10498 | .permission = btrfs_permission, |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 10499 | .listxattr = btrfs_listxattr, |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 10500 | .update_time = btrfs_update_time, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10501 | }; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 10502 | |
Alexey Dobriyan | 82d339d | 2009-10-09 09:54:36 -0400 | [diff] [blame] | 10503 | const struct dentry_operations btrfs_dentry_operations = { |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 10504 | .d_delete = btrfs_dentry_delete, |
| 10505 | }; |