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 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 6 | #include <linux/kernel.h> |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 7 | #include <linux/bio.h> |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8 | #include <linux/buffer_head.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> |
Nikolay Borisov | b1c16ac | 2019-04-01 11:29:57 +0300 | [diff] [blame] | 31 | #include <linux/sched/mm.h> |
David Sterba | 92d3217 | 2018-04-16 21:10:14 +0200 | [diff] [blame] | 32 | #include <asm/unaligned.h> |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 33 | #include "ctree.h" |
| 34 | #include "disk-io.h" |
| 35 | #include "transaction.h" |
| 36 | #include "btrfs_inode.h" |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 37 | #include "print-tree.h" |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 38 | #include "ordered-data.h" |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 39 | #include "xattr.h" |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 40 | #include "tree-log.h" |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 41 | #include "volumes.h" |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 42 | #include "compression.h" |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 43 | #include "locking.h" |
Josef Bacik | dc89e98 | 2011-01-28 17:05:48 -0500 | [diff] [blame] | 44 | #include "free-space-cache.h" |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 45 | #include "inode-map.h" |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 46 | #include "backref.h" |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 47 | #include "props.h" |
Dongsheng Yang | 3119321 | 2014-12-12 16:44:35 +0800 | [diff] [blame] | 48 | #include "qgroup.h" |
Wang Xiaoguang | dda3245 | 2016-07-11 11:05:29 +0800 | [diff] [blame] | 49 | #include "dedupe.h" |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 50 | |
| 51 | struct btrfs_iget_args { |
Chris Mason | 90d3e59 | 2014-01-09 17:28:00 -0800 | [diff] [blame] | 52 | struct btrfs_key *location; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 53 | struct btrfs_root *root; |
| 54 | }; |
| 55 | |
Filipe Manana | f28a492 | 2015-12-08 19:23:20 +0000 | [diff] [blame] | 56 | struct btrfs_dio_data { |
Filipe Manana | f28a492 | 2015-12-08 19:23:20 +0000 | [diff] [blame] | 57 | u64 reserve; |
| 58 | u64 unsubmitted_oe_range_start; |
| 59 | u64 unsubmitted_oe_range_end; |
Liu Bo | 4aaedfb | 2016-12-14 22:36:05 -0800 | [diff] [blame] | 60 | int overwrite; |
Filipe Manana | f28a492 | 2015-12-08 19:23:20 +0000 | [diff] [blame] | 61 | }; |
| 62 | |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 63 | static const struct inode_operations btrfs_dir_inode_operations; |
| 64 | static const struct inode_operations btrfs_symlink_inode_operations; |
| 65 | static const struct inode_operations btrfs_dir_ro_inode_operations; |
| 66 | static const struct inode_operations btrfs_special_inode_operations; |
| 67 | static const struct inode_operations btrfs_file_inode_operations; |
Alexey Dobriyan | 7f09410 | 2009-09-21 17:01:10 -0700 | [diff] [blame] | 68 | static const struct address_space_operations btrfs_aops; |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 69 | static const struct file_operations btrfs_dir_file_operations; |
David Sterba | 20e5506 | 2015-11-19 11:42:28 +0100 | [diff] [blame] | 70 | static const struct extent_io_ops btrfs_extent_io_ops; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 71 | |
| 72 | static struct kmem_cache *btrfs_inode_cachep; |
| 73 | struct kmem_cache *btrfs_trans_handle_cachep; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 74 | struct kmem_cache *btrfs_path_cachep; |
Josef Bacik | dc89e98 | 2011-01-28 17:05:48 -0500 | [diff] [blame] | 75 | struct kmem_cache *btrfs_free_space_cachep; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 76 | |
Eric Sandeen | 3972f26 | 2013-01-12 02:57:22 +0000 | [diff] [blame] | 77 | static int btrfs_setsize(struct inode *inode, struct iattr *attr); |
Filipe Manana | 213e8c5 | 2018-02-06 20:40:31 +0000 | [diff] [blame] | 78 | static int btrfs_truncate(struct inode *inode, bool skip_writeback); |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 79 | static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 80 | static noinline int cow_file_range(struct inode *inode, |
| 81 | struct page *locked_page, |
Wang Xiaoguang | dda3245 | 2016-07-11 11:05:29 +0800 | [diff] [blame] | 82 | u64 start, u64 end, u64 delalloc_end, |
| 83 | int *page_started, unsigned long *nr_written, |
| 84 | int unlock, struct btrfs_dedupe_hash *hash); |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 85 | static struct extent_map *create_io_em(struct inode *inode, u64 start, u64 len, |
| 86 | u64 orig_start, u64 block_start, |
| 87 | u64 block_len, u64 orig_block_len, |
| 88 | u64 ram_bytes, int compress_type, |
| 89 | int type); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 90 | |
Qu Wenruo | 52427260 | 2017-03-08 10:25:52 +0800 | [diff] [blame] | 91 | static void __endio_write_update_ordered(struct inode *inode, |
| 92 | const u64 offset, const u64 bytes, |
| 93 | const bool uptodate); |
| 94 | |
| 95 | /* |
| 96 | * Cleanup all submitted ordered extents in specified range to handle errors |
Andrea Gelmini | 52042d8 | 2018-11-28 12:05:13 +0100 | [diff] [blame] | 97 | * from the btrfs_run_delalloc_range() callback. |
Qu Wenruo | 52427260 | 2017-03-08 10:25:52 +0800 | [diff] [blame] | 98 | * |
| 99 | * NOTE: caller must ensure that when an error happens, it can not call |
| 100 | * extent_clear_unlock_delalloc() to clear both the bits EXTENT_DO_ACCOUNTING |
| 101 | * and EXTENT_DELALLOC simultaneously, because that causes the reserved metadata |
| 102 | * 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] | 103 | * extent (btrfs_finish_ordered_io()). |
Qu Wenruo | 52427260 | 2017-03-08 10:25:52 +0800 | [diff] [blame] | 104 | */ |
| 105 | static inline void btrfs_cleanup_ordered_extents(struct inode *inode, |
Nikolay Borisov | d1051d6 | 2018-11-21 17:10:52 +0200 | [diff] [blame] | 106 | struct page *locked_page, |
| 107 | u64 offset, u64 bytes) |
Qu Wenruo | 52427260 | 2017-03-08 10:25:52 +0800 | [diff] [blame] | 108 | { |
Naohiro Aota | 63d71450c | 2017-09-01 17:58:47 +0900 | [diff] [blame] | 109 | unsigned long index = offset >> PAGE_SHIFT; |
| 110 | unsigned long end_index = (offset + bytes - 1) >> PAGE_SHIFT; |
Nikolay Borisov | d1051d6 | 2018-11-21 17:10:52 +0200 | [diff] [blame] | 111 | u64 page_start = page_offset(locked_page); |
| 112 | u64 page_end = page_start + PAGE_SIZE - 1; |
| 113 | |
Naohiro Aota | 63d71450c | 2017-09-01 17:58:47 +0900 | [diff] [blame] | 114 | struct page *page; |
| 115 | |
| 116 | while (index <= end_index) { |
| 117 | page = find_get_page(inode->i_mapping, index); |
| 118 | index++; |
| 119 | if (!page) |
| 120 | continue; |
| 121 | ClearPagePrivate2(page); |
| 122 | put_page(page); |
| 123 | } |
Nikolay Borisov | d1051d6 | 2018-11-21 17:10:52 +0200 | [diff] [blame] | 124 | |
| 125 | /* |
| 126 | * In case this page belongs to the delalloc range being instantiated |
| 127 | * then skip it, since the first page of a range is going to be |
| 128 | * properly cleaned up by the caller of run_delalloc_range |
| 129 | */ |
| 130 | if (page_start >= offset && page_end <= (offset + bytes - 1)) { |
| 131 | offset += PAGE_SIZE; |
| 132 | bytes -= PAGE_SIZE; |
| 133 | } |
| 134 | |
| 135 | return __endio_write_update_ordered(inode, offset, bytes, false); |
Qu Wenruo | 52427260 | 2017-03-08 10:25:52 +0800 | [diff] [blame] | 136 | } |
| 137 | |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 138 | static int btrfs_dirty_inode(struct inode *inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 139 | |
Josef Bacik | 6a3891c | 2015-03-16 17:38:52 -0400 | [diff] [blame] | 140 | #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS |
| 141 | void btrfs_test_inode_set_ops(struct inode *inode) |
| 142 | { |
| 143 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
| 144 | } |
| 145 | #endif |
| 146 | |
Yan, Zheng | f34f57a | 2009-11-12 09:35:27 +0000 | [diff] [blame] | 147 | static int btrfs_init_inode_security(struct btrfs_trans_handle *trans, |
Eric Paris | 2a7dba3 | 2011-02-01 11:05:39 -0500 | [diff] [blame] | 148 | struct inode *inode, struct inode *dir, |
| 149 | const struct qstr *qstr) |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 150 | { |
| 151 | int err; |
| 152 | |
Yan, Zheng | f34f57a | 2009-11-12 09:35:27 +0000 | [diff] [blame] | 153 | err = btrfs_init_acl(trans, inode, dir); |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 154 | if (!err) |
Eric Paris | 2a7dba3 | 2011-02-01 11:05:39 -0500 | [diff] [blame] | 155 | err = btrfs_xattr_security_init(trans, inode, dir, qstr); |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 156 | return err; |
| 157 | } |
| 158 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 159 | /* |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 160 | * this does all the hard work for inserting an inline extent into |
| 161 | * the btree. The caller should have done a btrfs_drop_extents so that |
| 162 | * no overlapping inline items exist in the btree |
| 163 | */ |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 164 | static int insert_inline_extent(struct btrfs_trans_handle *trans, |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 165 | struct btrfs_path *path, int extent_inserted, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 166 | struct btrfs_root *root, struct inode *inode, |
| 167 | u64 start, size_t size, size_t compressed_size, |
Li Zefan | fe3f566 | 2011-03-28 08:30:38 +0000 | [diff] [blame] | 168 | int compress_type, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 169 | struct page **compressed_pages) |
| 170 | { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 171 | struct extent_buffer *leaf; |
| 172 | struct page *page = NULL; |
| 173 | char *kaddr; |
| 174 | unsigned long ptr; |
| 175 | struct btrfs_file_extent_item *ei; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 176 | int ret; |
| 177 | size_t cur_size = size; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 178 | unsigned long offset; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 179 | |
Li Zefan | fe3f566 | 2011-03-28 08:30:38 +0000 | [diff] [blame] | 180 | if (compressed_size && compressed_pages) |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 181 | cur_size = compressed_size; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 182 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 183 | inode_add_bytes(inode, size); |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 184 | |
| 185 | if (!extent_inserted) { |
| 186 | struct btrfs_key key; |
| 187 | size_t datasize; |
| 188 | |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 189 | key.objectid = btrfs_ino(BTRFS_I(inode)); |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 190 | key.offset = start; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 191 | key.type = BTRFS_EXTENT_DATA_KEY; |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 192 | |
| 193 | datasize = btrfs_file_extent_calc_inline_size(cur_size); |
| 194 | path->leave_spinning = 1; |
| 195 | ret = btrfs_insert_empty_item(trans, root, path, &key, |
| 196 | datasize); |
David Sterba | 79b4f4c | 2017-06-15 19:09:51 +0200 | [diff] [blame] | 197 | if (ret) |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 198 | goto fail; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 199 | } |
| 200 | leaf = path->nodes[0]; |
| 201 | ei = btrfs_item_ptr(leaf, path->slots[0], |
| 202 | struct btrfs_file_extent_item); |
| 203 | btrfs_set_file_extent_generation(leaf, ei, trans->transid); |
| 204 | btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE); |
| 205 | btrfs_set_file_extent_encryption(leaf, ei, 0); |
| 206 | btrfs_set_file_extent_other_encoding(leaf, ei, 0); |
| 207 | btrfs_set_file_extent_ram_bytes(leaf, ei, size); |
| 208 | ptr = btrfs_file_extent_inline_start(ei); |
| 209 | |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 210 | if (compress_type != BTRFS_COMPRESS_NONE) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 211 | struct page *cpage; |
| 212 | int i = 0; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 213 | while (compressed_size > 0) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 214 | cpage = compressed_pages[i]; |
Chris Mason | 5b050f0 | 2008-11-11 09:34:41 -0500 | [diff] [blame] | 215 | cur_size = min_t(unsigned long, compressed_size, |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 216 | PAGE_SIZE); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 217 | |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 218 | kaddr = kmap_atomic(cpage); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 219 | write_extent_buffer(leaf, kaddr, ptr, cur_size); |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 220 | kunmap_atomic(kaddr); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 221 | |
| 222 | i++; |
| 223 | ptr += cur_size; |
| 224 | compressed_size -= cur_size; |
| 225 | } |
| 226 | btrfs_set_file_extent_compression(leaf, ei, |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 227 | compress_type); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 228 | } else { |
| 229 | page = find_get_page(inode->i_mapping, |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 230 | start >> PAGE_SHIFT); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 231 | btrfs_set_file_extent_compression(leaf, ei, 0); |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 232 | kaddr = kmap_atomic(page); |
Johannes Thumshirn | 7073017 | 2018-12-05 15:23:03 +0100 | [diff] [blame] | 233 | offset = offset_in_page(start); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 234 | write_extent_buffer(leaf, kaddr + offset, ptr, size); |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 235 | kunmap_atomic(kaddr); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 236 | put_page(page); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 237 | } |
| 238 | btrfs_mark_buffer_dirty(leaf); |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 239 | btrfs_release_path(path); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 240 | |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 241 | /* |
| 242 | * we're an inline extent, so nobody can |
| 243 | * extend the file past i_size without locking |
| 244 | * a page we already have locked. |
| 245 | * |
| 246 | * We must do any isize and inode updates |
| 247 | * before we unlock the pages. Otherwise we |
| 248 | * could end up racing with unlink. |
| 249 | */ |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 250 | BTRFS_I(inode)->disk_i_size = inode->i_size; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 251 | ret = btrfs_update_inode(trans, root, inode); |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 252 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 253 | fail: |
David Sterba | 79b4f4c | 2017-06-15 19:09:51 +0200 | [diff] [blame] | 254 | return ret; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | |
| 258 | /* |
| 259 | * conditionally insert an inline extent into the file. This |
| 260 | * does the checks required to make sure the data is small enough |
| 261 | * to fit as an inline extent. |
| 262 | */ |
Nikolay Borisov | d02c0e2 | 2018-03-02 09:43:15 +0200 | [diff] [blame] | 263 | static noinline int cow_file_range_inline(struct inode *inode, u64 start, |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 264 | u64 end, size_t compressed_size, |
| 265 | int compress_type, |
| 266 | struct page **compressed_pages) |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 267 | { |
Nikolay Borisov | d02c0e2 | 2018-03-02 09:43:15 +0200 | [diff] [blame] | 268 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 269 | struct btrfs_fs_info *fs_info = root->fs_info; |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 270 | struct btrfs_trans_handle *trans; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 271 | u64 isize = i_size_read(inode); |
| 272 | u64 actual_end = min(end + 1, isize); |
| 273 | u64 inline_len = actual_end - start; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 274 | u64 aligned_end = ALIGN(end, fs_info->sectorsize); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 275 | u64 data_len = inline_len; |
| 276 | int ret; |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 277 | struct btrfs_path *path; |
| 278 | int extent_inserted = 0; |
| 279 | u32 extent_item_size; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 280 | |
| 281 | if (compressed_size) |
| 282 | data_len = compressed_size; |
| 283 | |
| 284 | if (start > 0 || |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 285 | actual_end > fs_info->sectorsize || |
| 286 | data_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info) || |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 287 | (!compressed_size && |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 288 | (actual_end & (fs_info->sectorsize - 1)) == 0) || |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 289 | end + 1 < isize || |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 290 | data_len > fs_info->max_inline) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 291 | return 1; |
| 292 | } |
| 293 | |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 294 | path = btrfs_alloc_path(); |
| 295 | if (!path) |
| 296 | return -ENOMEM; |
| 297 | |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 298 | trans = btrfs_join_transaction(root); |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 299 | if (IS_ERR(trans)) { |
| 300 | btrfs_free_path(path); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 301 | return PTR_ERR(trans); |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 302 | } |
Josef Bacik | 69fe2d7 | 2017-10-19 14:15:57 -0400 | [diff] [blame] | 303 | trans->block_rsv = &BTRFS_I(inode)->block_rsv; |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 304 | |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 305 | if (compressed_size && compressed_pages) |
| 306 | extent_item_size = btrfs_file_extent_calc_inline_size( |
| 307 | compressed_size); |
| 308 | else |
| 309 | extent_item_size = btrfs_file_extent_calc_inline_size( |
| 310 | inline_len); |
| 311 | |
| 312 | ret = __btrfs_drop_extents(trans, root, inode, path, |
| 313 | start, aligned_end, NULL, |
| 314 | 1, 1, extent_item_size, &extent_inserted); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 315 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 316 | btrfs_abort_transaction(trans, ret); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 317 | goto out; |
| 318 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 319 | |
| 320 | if (isize > actual_end) |
| 321 | inline_len = min_t(u64, isize, actual_end); |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 322 | ret = insert_inline_extent(trans, path, extent_inserted, |
| 323 | root, inode, start, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 324 | inline_len, compressed_size, |
Li Zefan | fe3f566 | 2011-03-28 08:30:38 +0000 | [diff] [blame] | 325 | compress_type, compressed_pages); |
Josef Bacik | 2adcac1 | 2012-05-23 16:10:14 -0400 | [diff] [blame] | 326 | if (ret && ret != -ENOSPC) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 327 | btrfs_abort_transaction(trans, ret); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 328 | goto out; |
Josef Bacik | 2adcac1 | 2012-05-23 16:10:14 -0400 | [diff] [blame] | 329 | } else if (ret == -ENOSPC) { |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 330 | ret = 1; |
| 331 | goto out; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 332 | } |
Josef Bacik | 2adcac1 | 2012-05-23 16:10:14 -0400 | [diff] [blame] | 333 | |
Josef Bacik | bdc20e6 | 2013-02-28 13:23:38 -0500 | [diff] [blame] | 334 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags); |
Nikolay Borisov | dcdbc05 | 2017-02-20 13:50:45 +0200 | [diff] [blame] | 335 | btrfs_drop_extent_cache(BTRFS_I(inode), start, aligned_end - 1, 0); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 336 | out: |
Qu Wenruo | 94ed938 | 2015-09-08 17:25:56 +0800 | [diff] [blame] | 337 | /* |
| 338 | * Don't forget to free the reserved space, as for inlined extent |
| 339 | * it won't count as data extent, free them directly here. |
| 340 | * And at reserve time, it's always aligned to page size, so |
| 341 | * just free one page here. |
| 342 | */ |
Qu Wenruo | bc42bda | 2017-02-27 15:10:39 +0800 | [diff] [blame] | 343 | btrfs_qgroup_free_data(inode, NULL, 0, PAGE_SIZE); |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 344 | btrfs_free_path(path); |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 345 | btrfs_end_transaction(trans); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 346 | return ret; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 347 | } |
| 348 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 349 | struct async_extent { |
| 350 | u64 start; |
| 351 | u64 ram_size; |
| 352 | u64 compressed_size; |
| 353 | struct page **pages; |
| 354 | unsigned long nr_pages; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 355 | int compress_type; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 356 | struct list_head list; |
| 357 | }; |
| 358 | |
Nikolay Borisov | 97db120 | 2019-03-12 17:20:24 +0200 | [diff] [blame] | 359 | struct async_chunk { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 360 | struct inode *inode; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 361 | struct page *locked_page; |
| 362 | u64 start; |
| 363 | u64 end; |
Liu Bo | f82b735 | 2017-10-23 23:18:16 -0600 | [diff] [blame] | 364 | unsigned int write_flags; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 365 | struct list_head extents; |
| 366 | struct btrfs_work work; |
Nikolay Borisov | 97db120 | 2019-03-12 17:20:24 +0200 | [diff] [blame] | 367 | atomic_t *pending; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 368 | }; |
| 369 | |
Nikolay Borisov | 97db120 | 2019-03-12 17:20:24 +0200 | [diff] [blame] | 370 | struct async_cow { |
| 371 | /* Number of chunks in flight; must be first in the structure */ |
| 372 | atomic_t num_chunks; |
| 373 | struct async_chunk chunks[]; |
| 374 | }; |
| 375 | |
| 376 | static noinline int add_async_extent(struct async_chunk *cow, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 377 | u64 start, u64 ram_size, |
| 378 | u64 compressed_size, |
| 379 | struct page **pages, |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 380 | unsigned long nr_pages, |
| 381 | int compress_type) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 382 | { |
| 383 | struct async_extent *async_extent; |
| 384 | |
| 385 | async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 386 | BUG_ON(!async_extent); /* -ENOMEM */ |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 387 | async_extent->start = start; |
| 388 | async_extent->ram_size = ram_size; |
| 389 | async_extent->compressed_size = compressed_size; |
| 390 | async_extent->pages = pages; |
| 391 | async_extent->nr_pages = nr_pages; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 392 | async_extent->compress_type = compress_type; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 393 | list_add_tail(&async_extent->list, &cow->extents); |
| 394 | return 0; |
| 395 | } |
| 396 | |
Timofey Titovets | c2fcdcd | 2017-07-17 16:52:58 +0300 | [diff] [blame] | 397 | static inline int inode_need_compress(struct inode *inode, u64 start, u64 end) |
Wang Shilong | f79707b | 2014-07-17 11:44:09 +0800 | [diff] [blame] | 398 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 399 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Wang Shilong | f79707b | 2014-07-17 11:44:09 +0800 | [diff] [blame] | 400 | |
| 401 | /* force compress */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 402 | if (btrfs_test_opt(fs_info, FORCE_COMPRESS)) |
Wang Shilong | f79707b | 2014-07-17 11:44:09 +0800 | [diff] [blame] | 403 | return 1; |
David Sterba | eec63c6 | 2017-07-17 19:41:31 +0200 | [diff] [blame] | 404 | /* defrag ioctl */ |
| 405 | if (BTRFS_I(inode)->defrag_compress) |
| 406 | return 1; |
Wang Shilong | f79707b | 2014-07-17 11:44:09 +0800 | [diff] [blame] | 407 | /* bad compression ratios */ |
| 408 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS) |
| 409 | return 0; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 410 | if (btrfs_test_opt(fs_info, COMPRESS) || |
Wang Shilong | f79707b | 2014-07-17 11:44:09 +0800 | [diff] [blame] | 411 | BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS || |
David Sterba | b52aa8c | 2017-07-17 19:17:20 +0200 | [diff] [blame] | 412 | BTRFS_I(inode)->prop_compress) |
Timofey Titovets | c2fcdcd | 2017-07-17 16:52:58 +0300 | [diff] [blame] | 413 | return btrfs_compress_heuristic(inode, start, end); |
Wang Shilong | f79707b | 2014-07-17 11:44:09 +0800 | [diff] [blame] | 414 | return 0; |
| 415 | } |
| 416 | |
Nikolay Borisov | 6158e1c | 2017-02-20 13:50:43 +0200 | [diff] [blame] | 417 | static inline void inode_should_defrag(struct btrfs_inode *inode, |
Anand Jain | 26d30f8 | 2016-12-19 19:09:06 +0800 | [diff] [blame] | 418 | u64 start, u64 end, u64 num_bytes, u64 small_write) |
| 419 | { |
| 420 | /* If this is a small write inside eof, kick off a defrag */ |
| 421 | if (num_bytes < small_write && |
Nikolay Borisov | 6158e1c | 2017-02-20 13:50:43 +0200 | [diff] [blame] | 422 | (start > 0 || end + 1 < inode->disk_i_size)) |
Anand Jain | 26d30f8 | 2016-12-19 19:09:06 +0800 | [diff] [blame] | 423 | btrfs_add_inode_defrag(NULL, inode); |
| 424 | } |
| 425 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 426 | /* |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 427 | * we create compressed extents in two phases. The first |
| 428 | * phase compresses a range of pages that have already been |
| 429 | * locked (both pages and state bits are locked). |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 430 | * |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 431 | * This is done inside an ordered work queue, and the compression |
| 432 | * is spread across many cpus. The actual IO submission is step |
| 433 | * two, and the ordered work queue takes care of making sure that |
| 434 | * happens in the same order things were put onto the queue by |
| 435 | * writepages and friends. |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 436 | * |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 437 | * If this code finds it can't get good compression, it puts an |
| 438 | * entry onto the work queue to write the uncompressed bytes. This |
| 439 | * makes sure that both compressed inodes and uncompressed inodes |
Artem Bityutskiy | b257031 | 2012-07-25 18:12:06 +0300 | [diff] [blame] | 440 | * are written in the same order that the flusher thread sent them |
| 441 | * down. |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 442 | */ |
Nikolay Borisov | 1368c6d | 2019-03-12 17:20:27 +0200 | [diff] [blame] | 443 | static noinline void compress_file_range(struct async_chunk *async_chunk, |
| 444 | int *num_added) |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 445 | { |
Nikolay Borisov | 1368c6d | 2019-03-12 17:20:27 +0200 | [diff] [blame] | 446 | struct inode *inode = async_chunk->inode; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 447 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 448 | u64 blocksize = fs_info->sectorsize; |
Nikolay Borisov | 1368c6d | 2019-03-12 17:20:27 +0200 | [diff] [blame] | 449 | u64 start = async_chunk->start; |
| 450 | u64 end = async_chunk->end; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 451 | u64 actual_end; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 452 | int ret = 0; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 453 | struct page **pages = NULL; |
| 454 | unsigned long nr_pages; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 455 | unsigned long total_compressed = 0; |
| 456 | unsigned long total_in = 0; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 457 | int i; |
| 458 | int will_compress; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 459 | int compress_type = fs_info->compress_type; |
Chris Mason | 4adaa61 | 2013-03-26 13:07:00 -0400 | [diff] [blame] | 460 | int redirty = 0; |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 461 | |
Nikolay Borisov | 6158e1c | 2017-02-20 13:50:43 +0200 | [diff] [blame] | 462 | inode_should_defrag(BTRFS_I(inode), start, end, end - start + 1, |
| 463 | SZ_16K); |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 464 | |
Nikolay Borisov | 62b3762 | 2019-01-03 10:50:00 +0200 | [diff] [blame] | 465 | actual_end = min_t(u64, i_size_read(inode), end + 1); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 466 | again: |
| 467 | will_compress = 0; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 468 | nr_pages = (end >> PAGE_SHIFT) - (start >> PAGE_SHIFT) + 1; |
David Sterba | 069eac7 | 2017-02-14 19:36:54 +0100 | [diff] [blame] | 469 | BUILD_BUG_ON((BTRFS_MAX_COMPRESSED % PAGE_SIZE) != 0); |
| 470 | nr_pages = min_t(unsigned long, nr_pages, |
| 471 | BTRFS_MAX_COMPRESSED / PAGE_SIZE); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 472 | |
Chris Mason | f03d9301f | 2009-02-04 09:31:06 -0500 | [diff] [blame] | 473 | /* |
| 474 | * we don't want to send crud past the end of i_size through |
| 475 | * compression, that's just a waste of CPU time. So, if the |
| 476 | * end of the file is before the start of our current |
| 477 | * requested range of bytes, we bail out to the uncompressed |
| 478 | * cleanup code that can deal with all of this. |
| 479 | * |
| 480 | * It isn't really the fastest way to fix things, but this is a |
| 481 | * very uncommon corner. |
| 482 | */ |
| 483 | if (actual_end <= start) |
| 484 | goto cleanup_and_bail_uncompressed; |
| 485 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 486 | total_compressed = actual_end - start; |
| 487 | |
Shilong Wang | 4bcbb33 | 2014-10-07 18:44:35 -0400 | [diff] [blame] | 488 | /* |
| 489 | * skip compression for a small file range(<=blocksize) that |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 490 | * 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] | 491 | */ |
| 492 | if (total_compressed <= blocksize && |
| 493 | (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size)) |
| 494 | goto cleanup_and_bail_uncompressed; |
| 495 | |
David Sterba | 069eac7 | 2017-02-14 19:36:54 +0100 | [diff] [blame] | 496 | total_compressed = min_t(unsigned long, total_compressed, |
| 497 | BTRFS_MAX_UNCOMPRESSED); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 498 | total_in = 0; |
| 499 | ret = 0; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 500 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 501 | /* |
| 502 | * we do compression for mount -o compress and when the |
| 503 | * inode has not been flagged as nocompress. This flag can |
| 504 | * change at any time if we discover bad compression ratios. |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 505 | */ |
Timofey Titovets | c2fcdcd | 2017-07-17 16:52:58 +0300 | [diff] [blame] | 506 | if (inode_need_compress(inode, start, end)) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 507 | WARN_ON(pages); |
David Sterba | 31e818f | 2015-02-20 18:00:26 +0100 | [diff] [blame] | 508 | pages = kcalloc(nr_pages, sizeof(struct page *), GFP_NOFS); |
Li Zefan | 560f7d7 | 2011-09-08 10:22:01 +0800 | [diff] [blame] | 509 | if (!pages) { |
| 510 | /* just bail out to the uncompressed code */ |
Filipe Manana | 3527a01 | 2018-10-13 00:37:25 +0100 | [diff] [blame] | 511 | nr_pages = 0; |
Li Zefan | 560f7d7 | 2011-09-08 10:22:01 +0800 | [diff] [blame] | 512 | goto cont; |
| 513 | } |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 514 | |
David Sterba | eec63c6 | 2017-07-17 19:41:31 +0200 | [diff] [blame] | 515 | if (BTRFS_I(inode)->defrag_compress) |
| 516 | compress_type = BTRFS_I(inode)->defrag_compress; |
| 517 | else if (BTRFS_I(inode)->prop_compress) |
David Sterba | b52aa8c | 2017-07-17 19:17:20 +0200 | [diff] [blame] | 518 | compress_type = BTRFS_I(inode)->prop_compress; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 519 | |
Chris Mason | 4adaa61 | 2013-03-26 13:07:00 -0400 | [diff] [blame] | 520 | /* |
| 521 | * we need to call clear_page_dirty_for_io on each |
| 522 | * page in the range. Otherwise applications with the file |
| 523 | * mmap'd can wander in and change the page contents while |
| 524 | * we are compressing them. |
| 525 | * |
| 526 | * If the compression fails for any reason, we set the pages |
| 527 | * dirty again later on. |
Timofey Titovets | e9679de | 2017-10-24 01:29:48 +0300 | [diff] [blame] | 528 | * |
| 529 | * Note that the remaining part is redirtied, the start pointer |
| 530 | * has moved, the end is the original one. |
Chris Mason | 4adaa61 | 2013-03-26 13:07:00 -0400 | [diff] [blame] | 531 | */ |
Timofey Titovets | e9679de | 2017-10-24 01:29:48 +0300 | [diff] [blame] | 532 | if (!redirty) { |
| 533 | extent_range_clear_dirty_for_io(inode, start, end); |
| 534 | redirty = 1; |
| 535 | } |
David Sterba | f51d2b5 | 2017-09-15 17:36:57 +0200 | [diff] [blame] | 536 | |
| 537 | /* Compression level is applied here and only here */ |
| 538 | ret = btrfs_compress_pages( |
| 539 | compress_type | (fs_info->compress_level << 4), |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 540 | inode->i_mapping, start, |
David Sterba | 38c3146 | 2017-02-14 19:04:07 +0100 | [diff] [blame] | 541 | pages, |
David Sterba | 4d3a800 | 2017-02-14 19:04:07 +0100 | [diff] [blame] | 542 | &nr_pages, |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 543 | &total_in, |
David Sterba | e5d7490 | 2017-02-14 19:45:05 +0100 | [diff] [blame] | 544 | &total_compressed); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 545 | |
| 546 | if (!ret) { |
Johannes Thumshirn | 7073017 | 2018-12-05 15:23:03 +0100 | [diff] [blame] | 547 | unsigned long offset = offset_in_page(total_compressed); |
David Sterba | 4d3a800 | 2017-02-14 19:04:07 +0100 | [diff] [blame] | 548 | struct page *page = pages[nr_pages - 1]; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 549 | char *kaddr; |
| 550 | |
| 551 | /* zero the tail end of the last page, we might be |
| 552 | * sending it down to disk |
| 553 | */ |
| 554 | if (offset) { |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 555 | kaddr = kmap_atomic(page); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 556 | memset(kaddr + offset, 0, |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 557 | PAGE_SIZE - offset); |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 558 | kunmap_atomic(kaddr); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 559 | } |
| 560 | will_compress = 1; |
| 561 | } |
| 562 | } |
Li Zefan | 560f7d7 | 2011-09-08 10:22:01 +0800 | [diff] [blame] | 563 | cont: |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 564 | if (start == 0) { |
| 565 | /* lets try to make an inline extent */ |
Timofey Titovets | 6018ba0 | 2017-09-15 01:57:26 +0300 | [diff] [blame] | 566 | if (ret || total_in < actual_end) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 567 | /* we didn't compress the entire range, try |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 568 | * to make an uncompressed inline extent. |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 569 | */ |
Nikolay Borisov | d02c0e2 | 2018-03-02 09:43:15 +0200 | [diff] [blame] | 570 | ret = cow_file_range_inline(inode, start, end, 0, |
| 571 | BTRFS_COMPRESS_NONE, NULL); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 572 | } else { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 573 | /* try making a compressed inline extent */ |
Nikolay Borisov | d02c0e2 | 2018-03-02 09:43:15 +0200 | [diff] [blame] | 574 | ret = cow_file_range_inline(inode, start, end, |
Li Zefan | fe3f566 | 2011-03-28 08:30:38 +0000 | [diff] [blame] | 575 | total_compressed, |
| 576 | compress_type, pages); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 577 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 578 | if (ret <= 0) { |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 579 | unsigned long clear_flags = EXTENT_DELALLOC | |
Josef Bacik | 8b62f87 | 2017-10-19 14:15:55 -0400 | [diff] [blame] | 580 | EXTENT_DELALLOC_NEW | EXTENT_DEFRAG | |
| 581 | EXTENT_DO_ACCOUNTING; |
Filipe Manana | e6eb431 | 2014-10-10 10:45:12 +0100 | [diff] [blame] | 582 | unsigned long page_error_op; |
| 583 | |
Filipe Manana | e6eb431 | 2014-10-10 10:45:12 +0100 | [diff] [blame] | 584 | page_error_op = ret < 0 ? PAGE_SET_ERROR : 0; |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 585 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 586 | /* |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 587 | * inline extent creation worked or returned error, |
| 588 | * we don't need to create any more async work items. |
| 589 | * Unlock and free up our temp pages. |
Josef Bacik | 8b62f87 | 2017-10-19 14:15:55 -0400 | [diff] [blame] | 590 | * |
| 591 | * We use DO_ACCOUNTING here because we need the |
| 592 | * delalloc_release_metadata to be done _after_ we drop |
| 593 | * our outstanding extent for clearing delalloc for this |
| 594 | * range. |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 595 | */ |
Qu Wenruo | ba8b04c | 2016-07-19 16:50:36 +0800 | [diff] [blame] | 596 | extent_clear_unlock_delalloc(inode, start, end, end, |
| 597 | NULL, clear_flags, |
| 598 | PAGE_UNLOCK | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 599 | PAGE_CLEAR_DIRTY | |
| 600 | PAGE_SET_WRITEBACK | |
Filipe Manana | e6eb431 | 2014-10-10 10:45:12 +0100 | [diff] [blame] | 601 | page_error_op | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 602 | PAGE_END_WRITEBACK); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 603 | goto free_pages_out; |
| 604 | } |
| 605 | } |
| 606 | |
| 607 | if (will_compress) { |
| 608 | /* |
| 609 | * we aren't doing an inline extent round the compressed size |
| 610 | * up to a block size boundary so the allocator does sane |
| 611 | * things |
| 612 | */ |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 613 | total_compressed = ALIGN(total_compressed, blocksize); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 614 | |
| 615 | /* |
| 616 | * one last check to make sure the compression is really a |
Timofey Titovets | 170607e | 2017-06-06 14:41:15 +0300 | [diff] [blame] | 617 | * win, compare the page count read with the blocks on disk, |
| 618 | * compression must free at least one sector size |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 619 | */ |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 620 | total_in = ALIGN(total_in, PAGE_SIZE); |
Timofey Titovets | 170607e | 2017-06-06 14:41:15 +0300 | [diff] [blame] | 621 | if (total_compressed + blocksize <= total_in) { |
Ashish Samant | c8bb0c8 | 2016-03-25 19:01:33 -0700 | [diff] [blame] | 622 | *num_added += 1; |
| 623 | |
| 624 | /* |
| 625 | * The async work queues will take care of doing actual |
| 626 | * allocation on disk for these compressed pages, and |
| 627 | * will submit them to the elevator. |
| 628 | */ |
Nikolay Borisov | b532627 | 2019-03-12 17:20:25 +0200 | [diff] [blame] | 629 | add_async_extent(async_chunk, start, total_in, |
David Sterba | 4d3a800 | 2017-02-14 19:04:07 +0100 | [diff] [blame] | 630 | total_compressed, pages, nr_pages, |
Ashish Samant | c8bb0c8 | 2016-03-25 19:01:33 -0700 | [diff] [blame] | 631 | compress_type); |
| 632 | |
Timofey Titovets | 1170862 | 2017-10-03 18:06:01 +0300 | [diff] [blame] | 633 | if (start + total_in < end) { |
| 634 | start += total_in; |
Ashish Samant | c8bb0c8 | 2016-03-25 19:01:33 -0700 | [diff] [blame] | 635 | pages = NULL; |
| 636 | cond_resched(); |
| 637 | goto again; |
| 638 | } |
| 639 | return; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 640 | } |
| 641 | } |
Ashish Samant | c8bb0c8 | 2016-03-25 19:01:33 -0700 | [diff] [blame] | 642 | if (pages) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 643 | /* |
| 644 | * the compression code ran but failed to make things smaller, |
| 645 | * free any pages it allocated and our page pointer array |
| 646 | */ |
David Sterba | 4d3a800 | 2017-02-14 19:04:07 +0100 | [diff] [blame] | 647 | for (i = 0; i < nr_pages; i++) { |
Chris Mason | 70b99e6 | 2008-10-31 12:46:39 -0400 | [diff] [blame] | 648 | WARN_ON(pages[i]->mapping); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 649 | put_page(pages[i]); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 650 | } |
| 651 | kfree(pages); |
| 652 | pages = NULL; |
| 653 | total_compressed = 0; |
David Sterba | 4d3a800 | 2017-02-14 19:04:07 +0100 | [diff] [blame] | 654 | nr_pages = 0; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 655 | |
| 656 | /* flag the file so we don't compress in the future */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 657 | if (!btrfs_test_opt(fs_info, FORCE_COMPRESS) && |
David Sterba | b52aa8c | 2017-07-17 19:17:20 +0200 | [diff] [blame] | 658 | !(BTRFS_I(inode)->prop_compress)) { |
Chris Mason | a555f81 | 2010-01-28 16:18:15 -0500 | [diff] [blame] | 659 | BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS; |
Chris Mason | 1e701a3 | 2010-03-11 09:42:04 -0500 | [diff] [blame] | 660 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 661 | } |
Chris Mason | f03d9301f | 2009-02-04 09:31:06 -0500 | [diff] [blame] | 662 | cleanup_and_bail_uncompressed: |
Ashish Samant | c8bb0c8 | 2016-03-25 19:01:33 -0700 | [diff] [blame] | 663 | /* |
| 664 | * No compression, but we still need to write the pages in the file |
| 665 | * we've been given so far. redirty the locked page if it corresponds |
| 666 | * to our extent and set things up for the async work queue to run |
| 667 | * cow_file_range to do the normal delalloc dance. |
| 668 | */ |
Nikolay Borisov | 1368c6d | 2019-03-12 17:20:27 +0200 | [diff] [blame] | 669 | if (page_offset(async_chunk->locked_page) >= start && |
| 670 | page_offset(async_chunk->locked_page) <= end) |
| 671 | __set_page_dirty_nobuffers(async_chunk->locked_page); |
Ashish Samant | c8bb0c8 | 2016-03-25 19:01:33 -0700 | [diff] [blame] | 672 | /* unlocked later on in the async handlers */ |
| 673 | |
| 674 | if (redirty) |
| 675 | extent_range_redirty_for_io(inode, start, end); |
Nikolay Borisov | b532627 | 2019-03-12 17:20:25 +0200 | [diff] [blame] | 676 | add_async_extent(async_chunk, start, end - start + 1, 0, NULL, 0, |
Ashish Samant | c8bb0c8 | 2016-03-25 19:01:33 -0700 | [diff] [blame] | 677 | BTRFS_COMPRESS_NONE); |
| 678 | *num_added += 1; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 679 | |
Filipe Manana | c44f649 | 2014-10-09 21:15:44 +0100 | [diff] [blame] | 680 | return; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 681 | |
| 682 | free_pages_out: |
David Sterba | 4d3a800 | 2017-02-14 19:04:07 +0100 | [diff] [blame] | 683 | for (i = 0; i < nr_pages; i++) { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 684 | WARN_ON(pages[i]->mapping); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 685 | put_page(pages[i]); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 686 | } |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 687 | kfree(pages); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 688 | } |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 689 | |
Filipe Manana | 40ae837 | 2014-10-06 22:14:24 +0100 | [diff] [blame] | 690 | static void free_async_extent_pages(struct async_extent *async_extent) |
| 691 | { |
| 692 | int i; |
| 693 | |
| 694 | if (!async_extent->pages) |
| 695 | return; |
| 696 | |
| 697 | for (i = 0; i < async_extent->nr_pages; i++) { |
| 698 | WARN_ON(async_extent->pages[i]->mapping); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 699 | put_page(async_extent->pages[i]); |
Filipe Manana | 40ae837 | 2014-10-06 22:14:24 +0100 | [diff] [blame] | 700 | } |
| 701 | kfree(async_extent->pages); |
| 702 | async_extent->nr_pages = 0; |
| 703 | async_extent->pages = NULL; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 704 | } |
| 705 | |
| 706 | /* |
| 707 | * phase two of compressed writeback. This is the ordered portion |
| 708 | * of the code, which only gets called in the order the work was |
| 709 | * queued. We walk all the async extents created by compress_file_range |
| 710 | * and send them down to the disk. |
| 711 | */ |
Nikolay Borisov | b532627 | 2019-03-12 17:20:25 +0200 | [diff] [blame] | 712 | static noinline void submit_compressed_extents(struct async_chunk *async_chunk) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 713 | { |
Nikolay Borisov | b532627 | 2019-03-12 17:20:25 +0200 | [diff] [blame] | 714 | struct inode *inode = async_chunk->inode; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 715 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 716 | struct async_extent *async_extent; |
| 717 | u64 alloc_hint = 0; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 718 | struct btrfs_key ins; |
| 719 | struct extent_map *em; |
| 720 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Nikolay Borisov | 4336650 | 2019-03-12 17:20:29 +0200 | [diff] [blame] | 721 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 722 | int ret = 0; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 723 | |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 724 | again: |
Nikolay Borisov | b532627 | 2019-03-12 17:20:25 +0200 | [diff] [blame] | 725 | while (!list_empty(&async_chunk->extents)) { |
| 726 | async_extent = list_entry(async_chunk->extents.next, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 727 | struct async_extent, list); |
| 728 | list_del(&async_extent->list); |
Nikolay Borisov | 7447555 | 2019-03-12 17:20:30 +0200 | [diff] [blame] | 729 | |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 730 | retry: |
Nikolay Borisov | 7447555 | 2019-03-12 17:20:30 +0200 | [diff] [blame] | 731 | lock_extent(io_tree, async_extent->start, |
| 732 | async_extent->start + async_extent->ram_size - 1); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 733 | /* did the compression code fall back to uncompressed IO? */ |
| 734 | if (!async_extent->pages) { |
| 735 | int page_started = 0; |
| 736 | unsigned long nr_written = 0; |
| 737 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 738 | /* allocate blocks */ |
Nikolay Borisov | b532627 | 2019-03-12 17:20:25 +0200 | [diff] [blame] | 739 | ret = cow_file_range(inode, async_chunk->locked_page, |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 740 | async_extent->start, |
| 741 | async_extent->start + |
| 742 | async_extent->ram_size - 1, |
Wang Xiaoguang | dda3245 | 2016-07-11 11:05:29 +0800 | [diff] [blame] | 743 | async_extent->start + |
| 744 | async_extent->ram_size - 1, |
| 745 | &page_started, &nr_written, 0, |
| 746 | NULL); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 747 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 748 | /* JDM XXX */ |
| 749 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 750 | /* |
| 751 | * if page_started, cow_file_range inserted an |
| 752 | * inline extent and took care of all the unlocking |
| 753 | * and IO for us. Otherwise, we need to submit |
| 754 | * all those pages down to the drive. |
| 755 | */ |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 756 | if (!page_started && !ret) |
Nikolay Borisov | 5e3ee23 | 2017-12-08 15:55:58 +0200 | [diff] [blame] | 757 | extent_write_locked_range(inode, |
| 758 | async_extent->start, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 759 | async_extent->start + |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 760 | async_extent->ram_size - 1, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 761 | WB_SYNC_ALL); |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 762 | else if (ret) |
Nikolay Borisov | b532627 | 2019-03-12 17:20:25 +0200 | [diff] [blame] | 763 | unlock_page(async_chunk->locked_page); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 764 | kfree(async_extent); |
| 765 | cond_resched(); |
| 766 | continue; |
| 767 | } |
| 768 | |
Wang Xiaoguang | 1851309 | 2016-07-25 15:51:40 +0800 | [diff] [blame] | 769 | ret = btrfs_reserve_extent(root, async_extent->ram_size, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 770 | async_extent->compressed_size, |
| 771 | async_extent->compressed_size, |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 772 | 0, alloc_hint, &ins, 1, 1); |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 773 | if (ret) { |
Filipe Manana | 40ae837 | 2014-10-06 22:14:24 +0100 | [diff] [blame] | 774 | free_async_extent_pages(async_extent); |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 775 | |
Josef Bacik | fdf8e2e | 2013-06-14 16:58:23 -0400 | [diff] [blame] | 776 | if (ret == -ENOSPC) { |
| 777 | unlock_extent(io_tree, async_extent->start, |
| 778 | async_extent->start + |
| 779 | async_extent->ram_size - 1); |
Liu Bo | ce62003 | 2014-07-24 22:48:05 +0800 | [diff] [blame] | 780 | |
| 781 | /* |
| 782 | * we need to redirty the pages if we decide to |
| 783 | * fallback to uncompressed IO, otherwise we |
| 784 | * will not submit these pages down to lower |
| 785 | * layers. |
| 786 | */ |
| 787 | extent_range_redirty_for_io(inode, |
| 788 | async_extent->start, |
| 789 | async_extent->start + |
| 790 | async_extent->ram_size - 1); |
| 791 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 792 | goto retry; |
Josef Bacik | fdf8e2e | 2013-06-14 16:58:23 -0400 | [diff] [blame] | 793 | } |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 794 | goto out_free; |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 795 | } |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 796 | /* |
| 797 | * here we're doing allocation and writeback of the |
| 798 | * compressed pages |
| 799 | */ |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 800 | em = create_io_em(inode, async_extent->start, |
| 801 | async_extent->ram_size, /* len */ |
| 802 | async_extent->start, /* orig_start */ |
| 803 | ins.objectid, /* block_start */ |
| 804 | ins.offset, /* block_len */ |
| 805 | ins.offset, /* orig_block_len */ |
| 806 | async_extent->ram_size, /* ram_bytes */ |
| 807 | async_extent->compress_type, |
| 808 | BTRFS_ORDERED_COMPRESSED); |
| 809 | if (IS_ERR(em)) |
| 810 | /* ret value is not necessary due to void function */ |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 811 | goto out_free_reserve; |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 812 | free_extent_map(em); |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 813 | |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 814 | ret = btrfs_add_ordered_extent_compress(inode, |
| 815 | async_extent->start, |
| 816 | ins.objectid, |
| 817 | async_extent->ram_size, |
| 818 | ins.offset, |
| 819 | BTRFS_ORDERED_COMPRESSED, |
| 820 | async_extent->compress_type); |
Filipe Manana | d9f8596 | 2014-08-25 10:43:00 +0100 | [diff] [blame] | 821 | if (ret) { |
Nikolay Borisov | dcdbc05 | 2017-02-20 13:50:45 +0200 | [diff] [blame] | 822 | btrfs_drop_extent_cache(BTRFS_I(inode), |
| 823 | async_extent->start, |
Filipe Manana | d9f8596 | 2014-08-25 10:43:00 +0100 | [diff] [blame] | 824 | async_extent->start + |
| 825 | async_extent->ram_size - 1, 0); |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 826 | goto out_free_reserve; |
Filipe Manana | d9f8596 | 2014-08-25 10:43:00 +0100 | [diff] [blame] | 827 | } |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 828 | btrfs_dec_block_group_reservations(fs_info, ins.objectid); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 829 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 830 | /* |
| 831 | * clear dirty, set writeback and unlock the pages. |
| 832 | */ |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 833 | extent_clear_unlock_delalloc(inode, async_extent->start, |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 834 | async_extent->start + |
| 835 | async_extent->ram_size - 1, |
Qu Wenruo | ba8b04c | 2016-07-19 16:50:36 +0800 | [diff] [blame] | 836 | async_extent->start + |
| 837 | async_extent->ram_size - 1, |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 838 | NULL, EXTENT_LOCKED | EXTENT_DELALLOC, |
| 839 | PAGE_UNLOCK | PAGE_CLEAR_DIRTY | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 840 | PAGE_SET_WRITEBACK); |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 841 | if (btrfs_submit_compressed_write(inode, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 842 | async_extent->start, |
| 843 | async_extent->ram_size, |
| 844 | ins.objectid, |
| 845 | ins.offset, async_extent->pages, |
Liu Bo | f82b735 | 2017-10-23 23:18:16 -0600 | [diff] [blame] | 846 | async_extent->nr_pages, |
Nikolay Borisov | b532627 | 2019-03-12 17:20:25 +0200 | [diff] [blame] | 847 | async_chunk->write_flags)) { |
Filipe Manana | fce2a4e | 2014-10-06 22:14:23 +0100 | [diff] [blame] | 848 | struct page *p = async_extent->pages[0]; |
| 849 | const u64 start = async_extent->start; |
| 850 | const u64 end = start + async_extent->ram_size - 1; |
| 851 | |
| 852 | p->mapping = inode->i_mapping; |
Nikolay Borisov | c629732 | 2018-11-08 10:18:08 +0200 | [diff] [blame] | 853 | btrfs_writepage_endio_finish_ordered(p, start, end, 0); |
Nikolay Borisov | 7087a9d | 2018-11-01 14:09:48 +0200 | [diff] [blame] | 854 | |
Filipe Manana | fce2a4e | 2014-10-06 22:14:23 +0100 | [diff] [blame] | 855 | p->mapping = NULL; |
Qu Wenruo | ba8b04c | 2016-07-19 16:50:36 +0800 | [diff] [blame] | 856 | extent_clear_unlock_delalloc(inode, start, end, end, |
| 857 | NULL, 0, |
Filipe Manana | fce2a4e | 2014-10-06 22:14:23 +0100 | [diff] [blame] | 858 | PAGE_END_WRITEBACK | |
| 859 | PAGE_SET_ERROR); |
Filipe Manana | 40ae837 | 2014-10-06 22:14:24 +0100 | [diff] [blame] | 860 | free_async_extent_pages(async_extent); |
Filipe Manana | fce2a4e | 2014-10-06 22:14:23 +0100 | [diff] [blame] | 861 | } |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 862 | alloc_hint = ins.objectid + ins.offset; |
| 863 | kfree(async_extent); |
| 864 | cond_resched(); |
| 865 | } |
Filipe Manana | dec8f17 | 2014-10-06 22:14:26 +0100 | [diff] [blame] | 866 | return; |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 867 | out_free_reserve: |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 868 | btrfs_dec_block_group_reservations(fs_info, ins.objectid); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 869 | btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 870 | out_free: |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 871 | extent_clear_unlock_delalloc(inode, async_extent->start, |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 872 | async_extent->start + |
| 873 | async_extent->ram_size - 1, |
Qu Wenruo | ba8b04c | 2016-07-19 16:50:36 +0800 | [diff] [blame] | 874 | async_extent->start + |
| 875 | async_extent->ram_size - 1, |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 876 | NULL, EXTENT_LOCKED | EXTENT_DELALLOC | |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 877 | EXTENT_DELALLOC_NEW | |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 878 | EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING, |
| 879 | PAGE_UNLOCK | PAGE_CLEAR_DIRTY | |
Filipe Manana | 704de49 | 2014-10-06 22:14:22 +0100 | [diff] [blame] | 880 | PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK | |
| 881 | PAGE_SET_ERROR); |
Filipe Manana | 40ae837 | 2014-10-06 22:14:24 +0100 | [diff] [blame] | 882 | free_async_extent_pages(async_extent); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 883 | kfree(async_extent); |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 884 | goto again; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 885 | } |
| 886 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 887 | static u64 get_extent_allocation_hint(struct inode *inode, u64 start, |
| 888 | u64 num_bytes) |
| 889 | { |
| 890 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
| 891 | struct extent_map *em; |
| 892 | u64 alloc_hint = 0; |
| 893 | |
| 894 | read_lock(&em_tree->lock); |
| 895 | em = search_extent_mapping(em_tree, start, num_bytes); |
| 896 | if (em) { |
| 897 | /* |
| 898 | * if block start isn't an actual block number then find the |
| 899 | * first block in this inode and use that as a hint. If that |
| 900 | * block is also bogus then just don't worry about it. |
| 901 | */ |
| 902 | if (em->block_start >= EXTENT_MAP_LAST_BYTE) { |
| 903 | free_extent_map(em); |
| 904 | em = search_extent_mapping(em_tree, 0, 0); |
| 905 | if (em && em->block_start < EXTENT_MAP_LAST_BYTE) |
| 906 | alloc_hint = em->block_start; |
| 907 | if (em) |
| 908 | free_extent_map(em); |
| 909 | } else { |
| 910 | alloc_hint = em->block_start; |
| 911 | free_extent_map(em); |
| 912 | } |
| 913 | } |
| 914 | read_unlock(&em_tree->lock); |
| 915 | |
| 916 | return alloc_hint; |
| 917 | } |
| 918 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 919 | /* |
| 920 | * when extent_io.c finds a delayed allocation range in the file, |
| 921 | * the call backs end up in this code. The basic idea is to |
| 922 | * allocate extents on disk for the range, and create ordered data structs |
| 923 | * in ram to track those extents. |
| 924 | * |
| 925 | * locked_page is the page that writepage had locked already. We use |
| 926 | * it to make sure we don't do extra locks or unlocks. |
| 927 | * |
| 928 | * *page_started is set to one if we unlock locked_page and do everything |
| 929 | * required to start IO on it. It may be clean and already done with |
| 930 | * IO when we return. |
| 931 | */ |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 932 | static noinline int cow_file_range(struct inode *inode, |
| 933 | struct page *locked_page, |
Wang Xiaoguang | dda3245 | 2016-07-11 11:05:29 +0800 | [diff] [blame] | 934 | u64 start, u64 end, u64 delalloc_end, |
| 935 | int *page_started, unsigned long *nr_written, |
| 936 | int unlock, struct btrfs_dedupe_hash *hash) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 937 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 938 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 939 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 940 | u64 alloc_hint = 0; |
| 941 | u64 num_bytes; |
| 942 | unsigned long ram_size; |
Filipe Manana | a315e68 | 2017-03-06 23:04:20 +0000 | [diff] [blame] | 943 | u64 cur_alloc_size = 0; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 944 | u64 blocksize = fs_info->sectorsize; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 945 | struct btrfs_key ins; |
| 946 | struct extent_map *em; |
Filipe Manana | a315e68 | 2017-03-06 23:04:20 +0000 | [diff] [blame] | 947 | unsigned clear_bits; |
| 948 | unsigned long page_ops; |
| 949 | bool extent_reserved = false; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 950 | int ret = 0; |
| 951 | |
Nikolay Borisov | 70ddc55 | 2017-02-20 13:50:35 +0200 | [diff] [blame] | 952 | if (btrfs_is_free_space_inode(BTRFS_I(inode))) { |
Josef Bacik | 02ecd2c | 2013-10-25 16:19:08 -0400 | [diff] [blame] | 953 | WARN_ON_ONCE(1); |
Josef Bacik | 29bce2f | 2014-02-07 12:21:23 -0500 | [diff] [blame] | 954 | ret = -EINVAL; |
| 955 | goto out_unlock; |
Josef Bacik | 02ecd2c | 2013-10-25 16:19:08 -0400 | [diff] [blame] | 956 | } |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 957 | |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 958 | num_bytes = ALIGN(end - start + 1, blocksize); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 959 | num_bytes = max(blocksize, num_bytes); |
Anand Jain | 566b176 | 2018-02-15 18:07:59 +0800 | [diff] [blame] | 960 | ASSERT(num_bytes <= btrfs_super_total_bytes(fs_info->super_copy)); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 961 | |
Nikolay Borisov | 6158e1c | 2017-02-20 13:50:43 +0200 | [diff] [blame] | 962 | inode_should_defrag(BTRFS_I(inode), start, end, num_bytes, SZ_64K); |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 963 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 964 | if (start == 0) { |
| 965 | /* lets try to make an inline extent */ |
Nikolay Borisov | d02c0e2 | 2018-03-02 09:43:15 +0200 | [diff] [blame] | 966 | ret = cow_file_range_inline(inode, start, end, 0, |
| 967 | BTRFS_COMPRESS_NONE, NULL); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 968 | if (ret == 0) { |
Josef Bacik | 8b62f87 | 2017-10-19 14:15:55 -0400 | [diff] [blame] | 969 | /* |
| 970 | * We use DO_ACCOUNTING here because we need the |
| 971 | * delalloc_release_metadata to be run _after_ we drop |
| 972 | * our outstanding extent for clearing delalloc for this |
| 973 | * range. |
| 974 | */ |
Qu Wenruo | ba8b04c | 2016-07-19 16:50:36 +0800 | [diff] [blame] | 975 | extent_clear_unlock_delalloc(inode, start, end, |
| 976 | delalloc_end, NULL, |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 977 | EXTENT_LOCKED | EXTENT_DELALLOC | |
Josef Bacik | 8b62f87 | 2017-10-19 14:15:55 -0400 | [diff] [blame] | 978 | EXTENT_DELALLOC_NEW | EXTENT_DEFRAG | |
| 979 | EXTENT_DO_ACCOUNTING, PAGE_UNLOCK | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 980 | PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK | |
| 981 | PAGE_END_WRITEBACK); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 982 | *nr_written = *nr_written + |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 983 | (end - start + PAGE_SIZE) / PAGE_SIZE; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 984 | *page_started = 1; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 985 | goto out; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 986 | } else if (ret < 0) { |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 987 | goto out_unlock; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 988 | } |
| 989 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 990 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 991 | alloc_hint = get_extent_allocation_hint(inode, start, num_bytes); |
Nikolay Borisov | dcdbc05 | 2017-02-20 13:50:45 +0200 | [diff] [blame] | 992 | btrfs_drop_extent_cache(BTRFS_I(inode), start, |
| 993 | start + num_bytes - 1, 0); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 994 | |
Anand Jain | 3752d22 | 2018-02-15 12:29:38 +0800 | [diff] [blame] | 995 | while (num_bytes > 0) { |
| 996 | cur_alloc_size = num_bytes; |
Wang Xiaoguang | 1851309 | 2016-07-25 15:51:40 +0800 | [diff] [blame] | 997 | ret = btrfs_reserve_extent(root, cur_alloc_size, cur_alloc_size, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 998 | fs_info->sectorsize, 0, alloc_hint, |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 999 | &ins, 1, 1); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 1000 | if (ret < 0) |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1001 | goto out_unlock; |
Filipe Manana | a315e68 | 2017-03-06 23:04:20 +0000 | [diff] [blame] | 1002 | cur_alloc_size = ins.offset; |
| 1003 | extent_reserved = true; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1004 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1005 | ram_size = ins.offset; |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 1006 | em = create_io_em(inode, start, ins.offset, /* len */ |
| 1007 | start, /* orig_start */ |
| 1008 | ins.objectid, /* block_start */ |
| 1009 | ins.offset, /* block_len */ |
| 1010 | ins.offset, /* orig_block_len */ |
| 1011 | ram_size, /* ram_bytes */ |
| 1012 | BTRFS_COMPRESS_NONE, /* compress_type */ |
Liu Bo | 1af4a0a | 2017-02-13 15:35:09 -0800 | [diff] [blame] | 1013 | BTRFS_ORDERED_REGULAR /* type */); |
Su Yue | 090a127a | 2018-05-30 16:48:56 +0800 | [diff] [blame] | 1014 | if (IS_ERR(em)) { |
| 1015 | ret = PTR_ERR(em); |
Liu Bo | ace68ba | 2013-04-22 10:53:47 +0000 | [diff] [blame] | 1016 | goto out_reserve; |
Su Yue | 090a127a | 2018-05-30 16:48:56 +0800 | [diff] [blame] | 1017 | } |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 1018 | free_extent_map(em); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1019 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1020 | ret = btrfs_add_ordered_extent(inode, start, ins.objectid, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1021 | ram_size, cur_alloc_size, 0); |
Liu Bo | ace68ba | 2013-04-22 10:53:47 +0000 | [diff] [blame] | 1022 | if (ret) |
Filipe Manana | d9f8596 | 2014-08-25 10:43:00 +0100 | [diff] [blame] | 1023 | goto out_drop_extent_cache; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1024 | |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1025 | if (root->root_key.objectid == |
| 1026 | BTRFS_DATA_RELOC_TREE_OBJECTID) { |
| 1027 | ret = btrfs_reloc_clone_csums(inode, start, |
| 1028 | cur_alloc_size); |
Qu Wenruo | 4dbd80f | 2017-03-08 10:25:51 +0800 | [diff] [blame] | 1029 | /* |
| 1030 | * Only drop cache here, and process as normal. |
| 1031 | * |
| 1032 | * We must not allow extent_clear_unlock_delalloc() |
| 1033 | * at out_unlock label to free meta of this ordered |
| 1034 | * extent, as its meta should be freed by |
| 1035 | * btrfs_finish_ordered_io(). |
| 1036 | * |
| 1037 | * So we must continue until @start is increased to |
| 1038 | * skip current ordered extent. |
| 1039 | */ |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 1040 | if (ret) |
Qu Wenruo | 4dbd80f | 2017-03-08 10:25:51 +0800 | [diff] [blame] | 1041 | btrfs_drop_extent_cache(BTRFS_I(inode), start, |
| 1042 | start + ram_size - 1, 0); |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1043 | } |
| 1044 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1045 | btrfs_dec_block_group_reservations(fs_info, ins.objectid); |
Filipe Manana | 9cfa3e3 | 2016-04-26 15:39:32 +0100 | [diff] [blame] | 1046 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1047 | /* we're not doing compressed IO, don't unlock the first |
| 1048 | * page (which the caller expects to stay locked), don't |
| 1049 | * clear any dirty bits and don't set any writeback bits |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 1050 | * |
| 1051 | * Do set the Private2 bit so we know this page was properly |
| 1052 | * setup for writepage |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1053 | */ |
Filipe Manana | a315e68 | 2017-03-06 23:04:20 +0000 | [diff] [blame] | 1054 | page_ops = unlock ? PAGE_UNLOCK : 0; |
| 1055 | page_ops |= PAGE_SET_PRIVATE2; |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 1056 | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1057 | extent_clear_unlock_delalloc(inode, start, |
Qu Wenruo | ba8b04c | 2016-07-19 16:50:36 +0800 | [diff] [blame] | 1058 | start + ram_size - 1, |
| 1059 | delalloc_end, locked_page, |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1060 | EXTENT_LOCKED | EXTENT_DELALLOC, |
Filipe Manana | a315e68 | 2017-03-06 23:04:20 +0000 | [diff] [blame] | 1061 | page_ops); |
Anand Jain | 3752d22 | 2018-02-15 12:29:38 +0800 | [diff] [blame] | 1062 | if (num_bytes < cur_alloc_size) |
| 1063 | num_bytes = 0; |
Qu Wenruo | 4dbd80f | 2017-03-08 10:25:51 +0800 | [diff] [blame] | 1064 | else |
Anand Jain | 3752d22 | 2018-02-15 12:29:38 +0800 | [diff] [blame] | 1065 | num_bytes -= cur_alloc_size; |
Chris Mason | c59f895 | 2007-12-17 20:14:04 -0500 | [diff] [blame] | 1066 | alloc_hint = ins.objectid + ins.offset; |
| 1067 | start += cur_alloc_size; |
Filipe Manana | a315e68 | 2017-03-06 23:04:20 +0000 | [diff] [blame] | 1068 | extent_reserved = false; |
Qu Wenruo | 4dbd80f | 2017-03-08 10:25:51 +0800 | [diff] [blame] | 1069 | |
| 1070 | /* |
| 1071 | * btrfs_reloc_clone_csums() error, since start is increased |
| 1072 | * extent_clear_unlock_delalloc() at out_unlock label won't |
| 1073 | * free metadata of current ordered extent, we're OK to exit. |
| 1074 | */ |
| 1075 | if (ret) |
| 1076 | goto out_unlock; |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 1077 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1078 | out: |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1079 | return ret; |
Miao Xie | b7d5b0a | 2012-11-01 07:32:18 +0000 | [diff] [blame] | 1080 | |
Filipe Manana | d9f8596 | 2014-08-25 10:43:00 +0100 | [diff] [blame] | 1081 | out_drop_extent_cache: |
Nikolay Borisov | dcdbc05 | 2017-02-20 13:50:45 +0200 | [diff] [blame] | 1082 | btrfs_drop_extent_cache(BTRFS_I(inode), start, start + ram_size - 1, 0); |
Liu Bo | ace68ba | 2013-04-22 10:53:47 +0000 | [diff] [blame] | 1083 | out_reserve: |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1084 | btrfs_dec_block_group_reservations(fs_info, ins.objectid); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 1085 | btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1086 | out_unlock: |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 1087 | clear_bits = EXTENT_LOCKED | EXTENT_DELALLOC | EXTENT_DELALLOC_NEW | |
| 1088 | EXTENT_DEFRAG | EXTENT_CLEAR_META_RESV; |
Filipe Manana | a315e68 | 2017-03-06 23:04:20 +0000 | [diff] [blame] | 1089 | page_ops = PAGE_UNLOCK | PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK | |
| 1090 | PAGE_END_WRITEBACK; |
| 1091 | /* |
| 1092 | * If we reserved an extent for our delalloc range (or a subrange) and |
| 1093 | * failed to create the respective ordered extent, then it means that |
| 1094 | * when we reserved the extent we decremented the extent's size from |
| 1095 | * the data space_info's bytes_may_use counter and incremented the |
| 1096 | * space_info's bytes_reserved counter by the same amount. We must make |
| 1097 | * sure extent_clear_unlock_delalloc() does not try to decrement again |
| 1098 | * the data space_info's bytes_may_use counter, therefore we do not pass |
| 1099 | * it the flag EXTENT_CLEAR_DATA_RESV. |
| 1100 | */ |
| 1101 | if (extent_reserved) { |
| 1102 | extent_clear_unlock_delalloc(inode, start, |
| 1103 | start + cur_alloc_size, |
| 1104 | start + cur_alloc_size, |
| 1105 | locked_page, |
| 1106 | clear_bits, |
| 1107 | page_ops); |
| 1108 | start += cur_alloc_size; |
| 1109 | if (start >= end) |
| 1110 | goto out; |
| 1111 | } |
Qu Wenruo | ba8b04c | 2016-07-19 16:50:36 +0800 | [diff] [blame] | 1112 | extent_clear_unlock_delalloc(inode, start, end, delalloc_end, |
| 1113 | locked_page, |
Filipe Manana | a315e68 | 2017-03-06 23:04:20 +0000 | [diff] [blame] | 1114 | clear_bits | EXTENT_CLEAR_DATA_RESV, |
| 1115 | page_ops); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1116 | goto out; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1117 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1118 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1119 | /* |
| 1120 | * work queue call back to started compression on a file and pages |
| 1121 | */ |
| 1122 | static noinline void async_cow_start(struct btrfs_work *work) |
| 1123 | { |
Nikolay Borisov | b532627 | 2019-03-12 17:20:25 +0200 | [diff] [blame] | 1124 | struct async_chunk *async_chunk; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1125 | int num_added = 0; |
Nikolay Borisov | 97db120 | 2019-03-12 17:20:24 +0200 | [diff] [blame] | 1126 | |
Nikolay Borisov | b532627 | 2019-03-12 17:20:25 +0200 | [diff] [blame] | 1127 | async_chunk = container_of(work, struct async_chunk, work); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1128 | |
Nikolay Borisov | 1368c6d | 2019-03-12 17:20:27 +0200 | [diff] [blame] | 1129 | compress_file_range(async_chunk, &num_added); |
Josef Bacik | 8180ef8 | 2012-06-08 15:16:12 -0400 | [diff] [blame] | 1130 | if (num_added == 0) { |
Nikolay Borisov | b532627 | 2019-03-12 17:20:25 +0200 | [diff] [blame] | 1131 | btrfs_add_delayed_iput(async_chunk->inode); |
| 1132 | async_chunk->inode = NULL; |
Josef Bacik | 8180ef8 | 2012-06-08 15:16:12 -0400 | [diff] [blame] | 1133 | } |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1134 | } |
| 1135 | |
| 1136 | /* |
| 1137 | * work queue call back to submit previously compressed pages |
| 1138 | */ |
| 1139 | static noinline void async_cow_submit(struct btrfs_work *work) |
| 1140 | { |
Nikolay Borisov | c5a68ae | 2019-03-12 17:20:26 +0200 | [diff] [blame] | 1141 | struct async_chunk *async_chunk = container_of(work, struct async_chunk, |
| 1142 | work); |
| 1143 | struct btrfs_fs_info *fs_info = btrfs_work_owner(work); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1144 | unsigned long nr_pages; |
| 1145 | |
Nikolay Borisov | b532627 | 2019-03-12 17:20:25 +0200 | [diff] [blame] | 1146 | nr_pages = (async_chunk->end - async_chunk->start + PAGE_SIZE) >> |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1147 | PAGE_SHIFT; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1148 | |
David Sterba | 093258e | 2018-02-26 16:15:17 +0100 | [diff] [blame] | 1149 | /* atomic_sub_return implies a barrier */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1150 | if (atomic_sub_return(nr_pages, &fs_info->async_delalloc_pages) < |
David Sterba | 093258e | 2018-02-26 16:15:17 +0100 | [diff] [blame] | 1151 | 5 * SZ_1M) |
| 1152 | cond_wake_up_nomb(&fs_info->async_submit_wait); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1153 | |
Nikolay Borisov | 4546d17 | 2019-01-03 10:50:03 +0200 | [diff] [blame] | 1154 | /* |
Nikolay Borisov | b532627 | 2019-03-12 17:20:25 +0200 | [diff] [blame] | 1155 | * ->inode could be NULL if async_chunk_start has failed to compress, |
Nikolay Borisov | 4546d17 | 2019-01-03 10:50:03 +0200 | [diff] [blame] | 1156 | * in which case we don't have anything to submit, yet we need to |
| 1157 | * always adjust ->async_delalloc_pages as its paired with the init |
| 1158 | * happening in cow_file_range_async |
| 1159 | */ |
Nikolay Borisov | b532627 | 2019-03-12 17:20:25 +0200 | [diff] [blame] | 1160 | if (async_chunk->inode) |
| 1161 | submit_compressed_extents(async_chunk); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1162 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1163 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1164 | static noinline void async_cow_free(struct btrfs_work *work) |
| 1165 | { |
Nikolay Borisov | b532627 | 2019-03-12 17:20:25 +0200 | [diff] [blame] | 1166 | struct async_chunk *async_chunk; |
Nikolay Borisov | 97db120 | 2019-03-12 17:20:24 +0200 | [diff] [blame] | 1167 | |
Nikolay Borisov | b532627 | 2019-03-12 17:20:25 +0200 | [diff] [blame] | 1168 | async_chunk = container_of(work, struct async_chunk, work); |
| 1169 | if (async_chunk->inode) |
| 1170 | btrfs_add_delayed_iput(async_chunk->inode); |
Nikolay Borisov | 97db120 | 2019-03-12 17:20:24 +0200 | [diff] [blame] | 1171 | /* |
| 1172 | * 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] | 1173 | * async_chunk's, freeing it ensures the whole array has been freed. |
Nikolay Borisov | 97db120 | 2019-03-12 17:20:24 +0200 | [diff] [blame] | 1174 | */ |
Nikolay Borisov | b532627 | 2019-03-12 17:20:25 +0200 | [diff] [blame] | 1175 | if (atomic_dec_and_test(async_chunk->pending)) |
Nikolay Borisov | b1c16ac | 2019-04-01 11:29:57 +0300 | [diff] [blame] | 1176 | kvfree(async_chunk->pending); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1177 | } |
| 1178 | |
| 1179 | static int cow_file_range_async(struct inode *inode, struct page *locked_page, |
| 1180 | u64 start, u64 end, int *page_started, |
Liu Bo | f82b735 | 2017-10-23 23:18:16 -0600 | [diff] [blame] | 1181 | unsigned long *nr_written, |
| 1182 | unsigned int write_flags) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1183 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1184 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Nikolay Borisov | 97db120 | 2019-03-12 17:20:24 +0200 | [diff] [blame] | 1185 | struct async_cow *ctx; |
| 1186 | struct async_chunk *async_chunk; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1187 | unsigned long nr_pages; |
| 1188 | u64 cur_end; |
Nikolay Borisov | 97db120 | 2019-03-12 17:20:24 +0200 | [diff] [blame] | 1189 | u64 num_chunks = DIV_ROUND_UP(end - start, SZ_512K); |
| 1190 | int i; |
| 1191 | bool should_compress; |
Nikolay Borisov | b1c16ac | 2019-04-01 11:29:57 +0300 | [diff] [blame] | 1192 | unsigned nofs_flag; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1193 | |
Nikolay Borisov | 69684c5 | 2019-03-12 17:20:28 +0200 | [diff] [blame] | 1194 | unlock_extent(&BTRFS_I(inode)->io_tree, start, end); |
Nikolay Borisov | 97db120 | 2019-03-12 17:20:24 +0200 | [diff] [blame] | 1195 | |
| 1196 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS && |
| 1197 | !btrfs_test_opt(fs_info, FORCE_COMPRESS)) { |
| 1198 | num_chunks = 1; |
| 1199 | should_compress = false; |
| 1200 | } else { |
| 1201 | should_compress = true; |
| 1202 | } |
| 1203 | |
Nikolay Borisov | b1c16ac | 2019-04-01 11:29:57 +0300 | [diff] [blame] | 1204 | nofs_flag = memalloc_nofs_save(); |
| 1205 | ctx = kvmalloc(struct_size(ctx, chunks, num_chunks), GFP_KERNEL); |
| 1206 | memalloc_nofs_restore(nofs_flag); |
| 1207 | |
Nikolay Borisov | 97db120 | 2019-03-12 17:20:24 +0200 | [diff] [blame] | 1208 | if (!ctx) { |
| 1209 | unsigned clear_bits = EXTENT_LOCKED | EXTENT_DELALLOC | |
| 1210 | EXTENT_DELALLOC_NEW | EXTENT_DEFRAG | |
| 1211 | EXTENT_DO_ACCOUNTING; |
| 1212 | unsigned long page_ops = PAGE_UNLOCK | PAGE_CLEAR_DIRTY | |
| 1213 | PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK | |
| 1214 | PAGE_SET_ERROR; |
| 1215 | |
| 1216 | extent_clear_unlock_delalloc(inode, start, end, 0, locked_page, |
| 1217 | clear_bits, page_ops); |
| 1218 | return -ENOMEM; |
| 1219 | } |
| 1220 | |
| 1221 | async_chunk = ctx->chunks; |
| 1222 | atomic_set(&ctx->num_chunks, num_chunks); |
| 1223 | |
| 1224 | for (i = 0; i < num_chunks; i++) { |
| 1225 | if (should_compress) |
| 1226 | cur_end = min(end, start + SZ_512K - 1); |
| 1227 | else |
| 1228 | cur_end = end; |
| 1229 | |
Nikolay Borisov | bd4691a | 2019-01-03 10:50:01 +0200 | [diff] [blame] | 1230 | /* |
| 1231 | * igrab is called higher up in the call chain, take only the |
| 1232 | * lightweight reference for the callback lifetime |
| 1233 | */ |
| 1234 | ihold(inode); |
Nikolay Borisov | 97db120 | 2019-03-12 17:20:24 +0200 | [diff] [blame] | 1235 | async_chunk[i].pending = &ctx->num_chunks; |
| 1236 | async_chunk[i].inode = inode; |
| 1237 | async_chunk[i].start = start; |
| 1238 | async_chunk[i].end = cur_end; |
Nikolay Borisov | 97db120 | 2019-03-12 17:20:24 +0200 | [diff] [blame] | 1239 | async_chunk[i].locked_page = locked_page; |
| 1240 | async_chunk[i].write_flags = write_flags; |
| 1241 | INIT_LIST_HEAD(&async_chunk[i].extents); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1242 | |
Nikolay Borisov | 97db120 | 2019-03-12 17:20:24 +0200 | [diff] [blame] | 1243 | btrfs_init_work(&async_chunk[i].work, |
Liu Bo | 9e0af23 | 2014-08-15 23:36:53 +0800 | [diff] [blame] | 1244 | btrfs_delalloc_helper, |
| 1245 | async_cow_start, async_cow_submit, |
| 1246 | async_cow_free); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1247 | |
Nikolay Borisov | 97db120 | 2019-03-12 17:20:24 +0200 | [diff] [blame] | 1248 | nr_pages = DIV_ROUND_UP(cur_end - start, PAGE_SIZE); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1249 | atomic_add(nr_pages, &fs_info->async_delalloc_pages); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1250 | |
Nikolay Borisov | 97db120 | 2019-03-12 17:20:24 +0200 | [diff] [blame] | 1251 | btrfs_queue_work(fs_info->delalloc_workers, &async_chunk[i].work); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1252 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1253 | *nr_written += nr_pages; |
| 1254 | start = cur_end + 1; |
| 1255 | } |
| 1256 | *page_started = 1; |
| 1257 | return 0; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1258 | } |
| 1259 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 1260 | 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] | 1261 | u64 bytenr, u64 num_bytes) |
| 1262 | { |
| 1263 | int ret; |
| 1264 | struct btrfs_ordered_sum *sums; |
| 1265 | LIST_HEAD(list); |
| 1266 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1267 | ret = btrfs_lookup_csums_range(fs_info->csum_root, bytenr, |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 1268 | bytenr + num_bytes - 1, &list, 0); |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1269 | if (ret == 0 && list_empty(&list)) |
| 1270 | return 0; |
| 1271 | |
| 1272 | while (!list_empty(&list)) { |
| 1273 | sums = list_entry(list.next, struct btrfs_ordered_sum, list); |
| 1274 | list_del(&sums->list); |
| 1275 | kfree(sums); |
| 1276 | } |
Liu Bo | 5811375 | 2018-01-31 17:09:13 -0700 | [diff] [blame] | 1277 | if (ret < 0) |
| 1278 | return ret; |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1279 | return 1; |
| 1280 | } |
| 1281 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1282 | /* |
| 1283 | * when nowcow writeback call back. This checks for snapshots or COW copies |
| 1284 | * of the extents that exist in the file, and COWs the file as required. |
| 1285 | * |
| 1286 | * If no cow copies or snapshots exist, we write directly to the existing |
| 1287 | * blocks on disk |
| 1288 | */ |
Chris Mason | 7f366cf | 2009-03-12 20:12:45 -0400 | [diff] [blame] | 1289 | static noinline int run_delalloc_nocow(struct inode *inode, |
| 1290 | struct page *locked_page, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1291 | u64 start, u64 end, int *page_started, int force, |
| 1292 | unsigned long *nr_written) |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1293 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1294 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1295 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 1296 | struct extent_buffer *leaf; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1297 | struct btrfs_path *path; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1298 | struct btrfs_file_extent_item *fi; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1299 | struct btrfs_key found_key; |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 1300 | struct extent_map *em; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1301 | u64 cow_start; |
| 1302 | u64 cur_offset; |
| 1303 | u64 extent_end; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1304 | u64 extent_offset; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1305 | u64 disk_bytenr; |
| 1306 | u64 num_bytes; |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 1307 | u64 disk_num_bytes; |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 1308 | u64 ram_bytes; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1309 | int extent_type; |
Robbie Ko | 8ecebf4d | 2018-08-06 10:30:30 +0800 | [diff] [blame] | 1310 | int ret; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1311 | int type; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1312 | int nocow; |
| 1313 | int check_prev = 1; |
Li Zefan | 82d5902 | 2011-04-20 10:33:24 +0800 | [diff] [blame] | 1314 | bool nolock; |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 1315 | u64 ino = btrfs_ino(BTRFS_I(inode)); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1316 | |
| 1317 | path = btrfs_alloc_path(); |
Josef Bacik | 17ca04a | 2012-05-31 15:58:55 -0400 | [diff] [blame] | 1318 | if (!path) { |
Qu Wenruo | ba8b04c | 2016-07-19 16:50:36 +0800 | [diff] [blame] | 1319 | extent_clear_unlock_delalloc(inode, start, end, end, |
| 1320 | locked_page, |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1321 | EXTENT_LOCKED | EXTENT_DELALLOC | |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 1322 | EXTENT_DO_ACCOUNTING | |
| 1323 | EXTENT_DEFRAG, PAGE_UNLOCK | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1324 | PAGE_CLEAR_DIRTY | |
| 1325 | PAGE_SET_WRITEBACK | |
| 1326 | PAGE_END_WRITEBACK); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 1327 | return -ENOMEM; |
Josef Bacik | 17ca04a | 2012-05-31 15:58:55 -0400 | [diff] [blame] | 1328 | } |
Li Zefan | 82d5902 | 2011-04-20 10:33:24 +0800 | [diff] [blame] | 1329 | |
Nikolay Borisov | 70ddc55 | 2017-02-20 13:50:35 +0200 | [diff] [blame] | 1330 | nolock = btrfs_is_free_space_inode(BTRFS_I(inode)); |
Li Zefan | 82d5902 | 2011-04-20 10:33:24 +0800 | [diff] [blame] | 1331 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1332 | cow_start = (u64)-1; |
| 1333 | cur_offset = start; |
| 1334 | while (1) { |
Liu Bo | e4c3b2d | 2017-01-30 12:25:28 -0800 | [diff] [blame] | 1335 | ret = btrfs_lookup_file_extent(NULL, root, path, ino, |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1336 | cur_offset, 0); |
Josef Bacik | d788a34 | 2013-10-25 16:55:08 -0400 | [diff] [blame] | 1337 | if (ret < 0) |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1338 | goto error; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1339 | if (ret > 0 && path->slots[0] > 0 && check_prev) { |
| 1340 | leaf = path->nodes[0]; |
| 1341 | btrfs_item_key_to_cpu(leaf, &found_key, |
| 1342 | path->slots[0] - 1); |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 1343 | if (found_key.objectid == ino && |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1344 | found_key.type == BTRFS_EXTENT_DATA_KEY) |
| 1345 | path->slots[0]--; |
| 1346 | } |
| 1347 | check_prev = 0; |
| 1348 | next_slot: |
| 1349 | leaf = path->nodes[0]; |
| 1350 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { |
| 1351 | ret = btrfs_next_leaf(root, path); |
Liu Bo | e891669 | 2018-01-25 11:02:50 -0700 | [diff] [blame] | 1352 | if (ret < 0) { |
| 1353 | if (cow_start != (u64)-1) |
| 1354 | cur_offset = cow_start; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1355 | goto error; |
Liu Bo | e891669 | 2018-01-25 11:02:50 -0700 | [diff] [blame] | 1356 | } |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1357 | if (ret > 0) |
| 1358 | break; |
| 1359 | leaf = path->nodes[0]; |
| 1360 | } |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1361 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1362 | nocow = 0; |
| 1363 | disk_bytenr = 0; |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1364 | num_bytes = 0; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1365 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1366 | |
Filipe Manana | 1d512cb | 2015-11-09 00:33:58 +0000 | [diff] [blame] | 1367 | if (found_key.objectid > ino) |
| 1368 | break; |
| 1369 | if (WARN_ON_ONCE(found_key.objectid < ino) || |
| 1370 | found_key.type < BTRFS_EXTENT_DATA_KEY) { |
| 1371 | path->slots[0]++; |
| 1372 | goto next_slot; |
| 1373 | } |
| 1374 | if (found_key.type > BTRFS_EXTENT_DATA_KEY || |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1375 | found_key.offset > end) |
| 1376 | break; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1377 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1378 | if (found_key.offset > cur_offset) { |
| 1379 | extent_end = found_key.offset; |
Chris Mason | e9061e2 | 2009-10-09 09:57:45 -0400 | [diff] [blame] | 1380 | extent_type = 0; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1381 | goto out_check; |
| 1382 | } |
Chris Mason | c31f883 | 2008-01-08 15:46:31 -0500 | [diff] [blame] | 1383 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1384 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 1385 | struct btrfs_file_extent_item); |
| 1386 | extent_type = btrfs_file_extent_type(leaf, fi); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1387 | |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 1388 | ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1389 | if (extent_type == BTRFS_FILE_EXTENT_REG || |
| 1390 | extent_type == BTRFS_FILE_EXTENT_PREALLOC) { |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1391 | disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1392 | extent_offset = btrfs_file_extent_offset(leaf, fi); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1393 | extent_end = found_key.offset + |
| 1394 | btrfs_file_extent_num_bytes(leaf, fi); |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 1395 | disk_num_bytes = |
| 1396 | btrfs_file_extent_disk_num_bytes(leaf, fi); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1397 | if (extent_end <= start) { |
| 1398 | path->slots[0]++; |
| 1399 | goto next_slot; |
| 1400 | } |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1401 | if (disk_bytenr == 0) |
| 1402 | goto out_check; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1403 | if (btrfs_file_extent_compression(leaf, fi) || |
| 1404 | btrfs_file_extent_encryption(leaf, fi) || |
| 1405 | btrfs_file_extent_other_encoding(leaf, fi)) |
| 1406 | goto out_check; |
Ethan Lien | 78d4295 | 2018-05-17 14:58:29 +0800 | [diff] [blame] | 1407 | /* |
| 1408 | * Do the same check as in btrfs_cross_ref_exist but |
| 1409 | * without the unnecessary search. |
| 1410 | */ |
Lu Fengqi | 27a7ff5 | 2018-11-29 17:31:32 +0800 | [diff] [blame] | 1411 | if (!nolock && |
| 1412 | btrfs_file_extent_generation(leaf, fi) <= |
Ethan Lien | 78d4295 | 2018-05-17 14:58:29 +0800 | [diff] [blame] | 1413 | btrfs_root_last_snapshot(&root->root_item)) |
| 1414 | goto out_check; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1415 | if (extent_type == BTRFS_FILE_EXTENT_REG && !force) |
| 1416 | goto out_check; |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 1417 | if (btrfs_extent_readonly(fs_info, disk_bytenr)) |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1418 | goto out_check; |
Liu Bo | 5811375 | 2018-01-31 17:09:13 -0700 | [diff] [blame] | 1419 | ret = btrfs_cross_ref_exist(root, ino, |
| 1420 | found_key.offset - |
| 1421 | extent_offset, disk_bytenr); |
| 1422 | if (ret) { |
| 1423 | /* |
| 1424 | * ret could be -EIO if the above fails to read |
| 1425 | * metadata. |
| 1426 | */ |
| 1427 | if (ret < 0) { |
| 1428 | if (cow_start != (u64)-1) |
| 1429 | cur_offset = cow_start; |
| 1430 | goto error; |
| 1431 | } |
| 1432 | |
| 1433 | WARN_ON_ONCE(nolock); |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1434 | goto out_check; |
Liu Bo | 5811375 | 2018-01-31 17:09:13 -0700 | [diff] [blame] | 1435 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1436 | disk_bytenr += extent_offset; |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1437 | disk_bytenr += cur_offset - found_key.offset; |
| 1438 | num_bytes = min(end + 1, extent_end) - cur_offset; |
| 1439 | /* |
Wang Shilong | e9894fd | 2014-03-27 11:12:25 +0800 | [diff] [blame] | 1440 | * if there are pending snapshots for this root, |
| 1441 | * we fall into common COW way. |
| 1442 | */ |
Robbie Ko | 8ecebf4d | 2018-08-06 10:30:30 +0800 | [diff] [blame] | 1443 | if (!nolock && atomic_read(&root->snapshot_force_cow)) |
| 1444 | goto out_check; |
Wang Shilong | e9894fd | 2014-03-27 11:12:25 +0800 | [diff] [blame] | 1445 | /* |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1446 | * force cow if csum exists in the range. |
| 1447 | * this ensure that csum for a given extent are |
| 1448 | * either valid or do not exist. |
| 1449 | */ |
Liu Bo | 5811375 | 2018-01-31 17:09:13 -0700 | [diff] [blame] | 1450 | ret = csum_exist_in_range(fs_info, disk_bytenr, |
| 1451 | num_bytes); |
| 1452 | if (ret) { |
Liu Bo | 5811375 | 2018-01-31 17:09:13 -0700 | [diff] [blame] | 1453 | /* |
| 1454 | * ret could be -EIO if the above fails to read |
| 1455 | * metadata. |
| 1456 | */ |
| 1457 | if (ret < 0) { |
| 1458 | if (cow_start != (u64)-1) |
| 1459 | cur_offset = cow_start; |
| 1460 | goto error; |
| 1461 | } |
| 1462 | WARN_ON_ONCE(nolock); |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1463 | goto out_check; |
Robbie Ko | 91e1f56 | 2016-10-07 10:01:29 +0800 | [diff] [blame] | 1464 | } |
Robbie Ko | 8ecebf4d | 2018-08-06 10:30:30 +0800 | [diff] [blame] | 1465 | if (!btrfs_inc_nocow_writers(fs_info, disk_bytenr)) |
Filipe Manana | f78c436 | 2016-05-09 13:15:41 +0100 | [diff] [blame] | 1466 | goto out_check; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1467 | nocow = 1; |
| 1468 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
| 1469 | extent_end = found_key.offset + |
Qu Wenruo | e41ca58 | 2018-06-06 15:41:49 +0800 | [diff] [blame] | 1470 | btrfs_file_extent_ram_bytes(leaf, fi); |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 1471 | extent_end = ALIGN(extent_end, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1472 | fs_info->sectorsize); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1473 | } else { |
Arnd Bergmann | 290342f | 2019-03-25 14:02:25 +0100 | [diff] [blame] | 1474 | BUG(); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1475 | } |
| 1476 | out_check: |
| 1477 | if (extent_end <= start) { |
| 1478 | path->slots[0]++; |
Filipe Manana | f78c436 | 2016-05-09 13:15:41 +0100 | [diff] [blame] | 1479 | if (nocow) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1480 | btrfs_dec_nocow_writers(fs_info, disk_bytenr); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1481 | goto next_slot; |
| 1482 | } |
| 1483 | if (!nocow) { |
| 1484 | if (cow_start == (u64)-1) |
| 1485 | cow_start = cur_offset; |
| 1486 | cur_offset = extent_end; |
| 1487 | if (cur_offset > end) |
| 1488 | break; |
| 1489 | path->slots[0]++; |
| 1490 | goto next_slot; |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1491 | } |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1492 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1493 | btrfs_release_path(path); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1494 | if (cow_start != (u64)-1) { |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 1495 | ret = cow_file_range(inode, locked_page, |
| 1496 | cow_start, found_key.offset - 1, |
Wang Xiaoguang | dda3245 | 2016-07-11 11:05:29 +0800 | [diff] [blame] | 1497 | end, page_started, nr_written, 1, |
| 1498 | NULL); |
Wang Shilong | e9894fd | 2014-03-27 11:12:25 +0800 | [diff] [blame] | 1499 | if (ret) { |
Filipe Manana | f78c436 | 2016-05-09 13:15:41 +0100 | [diff] [blame] | 1500 | if (nocow) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1501 | btrfs_dec_nocow_writers(fs_info, |
Filipe Manana | f78c436 | 2016-05-09 13:15:41 +0100 | [diff] [blame] | 1502 | disk_bytenr); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1503 | goto error; |
Wang Shilong | e9894fd | 2014-03-27 11:12:25 +0800 | [diff] [blame] | 1504 | } |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1505 | cow_start = (u64)-1; |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1506 | } |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1507 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1508 | if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) { |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 1509 | u64 orig_start = found_key.offset - extent_offset; |
| 1510 | |
| 1511 | em = create_io_em(inode, cur_offset, num_bytes, |
| 1512 | orig_start, |
| 1513 | disk_bytenr, /* block_start */ |
| 1514 | num_bytes, /* block_len */ |
| 1515 | disk_num_bytes, /* orig_block_len */ |
| 1516 | ram_bytes, BTRFS_COMPRESS_NONE, |
| 1517 | BTRFS_ORDERED_PREALLOC); |
| 1518 | if (IS_ERR(em)) { |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 1519 | if (nocow) |
| 1520 | btrfs_dec_nocow_writers(fs_info, |
| 1521 | disk_bytenr); |
| 1522 | ret = PTR_ERR(em); |
| 1523 | goto error; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1524 | } |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 1525 | free_extent_map(em); |
| 1526 | } |
| 1527 | |
| 1528 | if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) { |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1529 | type = BTRFS_ORDERED_PREALLOC; |
| 1530 | } else { |
| 1531 | type = BTRFS_ORDERED_NOCOW; |
| 1532 | } |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1533 | |
| 1534 | ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1535 | num_bytes, num_bytes, type); |
Filipe Manana | f78c436 | 2016-05-09 13:15:41 +0100 | [diff] [blame] | 1536 | if (nocow) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1537 | btrfs_dec_nocow_writers(fs_info, disk_bytenr); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1538 | BUG_ON(ret); /* -ENOMEM */ |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1539 | |
Yan, Zheng | efa5646 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 1540 | if (root->root_key.objectid == |
Qu Wenruo | 4dbd80f | 2017-03-08 10:25:51 +0800 | [diff] [blame] | 1541 | BTRFS_DATA_RELOC_TREE_OBJECTID) |
| 1542 | /* |
| 1543 | * Error handled later, as we must prevent |
| 1544 | * extent_clear_unlock_delalloc() in error handler |
| 1545 | * from freeing metadata of created ordered extent. |
| 1546 | */ |
Yan, Zheng | efa5646 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 1547 | ret = btrfs_reloc_clone_csums(inode, cur_offset, |
| 1548 | num_bytes); |
Yan, Zheng | efa5646 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 1549 | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1550 | extent_clear_unlock_delalloc(inode, cur_offset, |
Qu Wenruo | ba8b04c | 2016-07-19 16:50:36 +0800 | [diff] [blame] | 1551 | cur_offset + num_bytes - 1, end, |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1552 | locked_page, EXTENT_LOCKED | |
Wang Xiaoguang | 1851309 | 2016-07-25 15:51:40 +0800 | [diff] [blame] | 1553 | EXTENT_DELALLOC | |
| 1554 | EXTENT_CLEAR_DATA_RESV, |
| 1555 | PAGE_UNLOCK | PAGE_SET_PRIVATE2); |
| 1556 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1557 | cur_offset = extent_end; |
Qu Wenruo | 4dbd80f | 2017-03-08 10:25:51 +0800 | [diff] [blame] | 1558 | |
| 1559 | /* |
| 1560 | * btrfs_reloc_clone_csums() error, now we're OK to call error |
| 1561 | * handler, as metadata for created ordered extent will only |
| 1562 | * be freed by btrfs_finish_ordered_io(). |
| 1563 | */ |
| 1564 | if (ret) |
| 1565 | goto error; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1566 | if (cur_offset > end) |
| 1567 | break; |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1568 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1569 | btrfs_release_path(path); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1570 | |
Robbie Ko | 506481b | 2018-10-30 18:04:04 +0800 | [diff] [blame] | 1571 | if (cur_offset <= end && cow_start == (u64)-1) |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1572 | cow_start = cur_offset; |
Josef Bacik | 17ca04a | 2012-05-31 15:58:55 -0400 | [diff] [blame] | 1573 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1574 | if (cow_start != (u64)-1) { |
Robbie Ko | 506481b | 2018-10-30 18:04:04 +0800 | [diff] [blame] | 1575 | cur_offset = end; |
Wang Xiaoguang | dda3245 | 2016-07-11 11:05:29 +0800 | [diff] [blame] | 1576 | ret = cow_file_range(inode, locked_page, cow_start, end, end, |
| 1577 | page_started, nr_written, 1, NULL); |
Josef Bacik | d788a34 | 2013-10-25 16:55:08 -0400 | [diff] [blame] | 1578 | if (ret) |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1579 | goto error; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1580 | } |
| 1581 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1582 | error: |
Josef Bacik | 17ca04a | 2012-05-31 15:58:55 -0400 | [diff] [blame] | 1583 | if (ret && cur_offset < end) |
Qu Wenruo | ba8b04c | 2016-07-19 16:50:36 +0800 | [diff] [blame] | 1584 | extent_clear_unlock_delalloc(inode, cur_offset, end, end, |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1585 | locked_page, EXTENT_LOCKED | |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 1586 | EXTENT_DELALLOC | EXTENT_DEFRAG | |
| 1587 | EXTENT_DO_ACCOUNTING, PAGE_UNLOCK | |
| 1588 | PAGE_CLEAR_DIRTY | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1589 | PAGE_SET_WRITEBACK | |
| 1590 | PAGE_END_WRITEBACK); |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1591 | btrfs_free_path(path); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1592 | return ret; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1593 | } |
| 1594 | |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 1595 | static inline int need_force_cow(struct inode *inode, u64 start, u64 end) |
| 1596 | { |
| 1597 | |
| 1598 | if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) && |
| 1599 | !(BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC)) |
| 1600 | return 0; |
| 1601 | |
| 1602 | /* |
| 1603 | * @defrag_bytes is a hint value, no spinlock held here, |
| 1604 | * if is not zero, it means the file is defragging. |
| 1605 | * Force cow if given extent needs to be defragged. |
| 1606 | */ |
| 1607 | if (BTRFS_I(inode)->defrag_bytes && |
| 1608 | test_range_bit(&BTRFS_I(inode)->io_tree, start, end, |
| 1609 | EXTENT_DEFRAG, 0, NULL)) |
| 1610 | return 1; |
| 1611 | |
| 1612 | return 0; |
| 1613 | } |
| 1614 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1615 | /* |
Nikolay Borisov | 5eaad97 | 2018-11-01 14:09:46 +0200 | [diff] [blame] | 1616 | * Function to process delayed allocation (create CoW) for ranges which are |
| 1617 | * being touched for the first time. |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1618 | */ |
Nikolay Borisov | bc9a8bf | 2018-12-19 09:50:20 +0200 | [diff] [blame] | 1619 | int btrfs_run_delalloc_range(struct inode *inode, struct page *locked_page, |
Nikolay Borisov | 5eaad97 | 2018-11-01 14:09:46 +0200 | [diff] [blame] | 1620 | u64 start, u64 end, int *page_started, unsigned long *nr_written, |
| 1621 | struct writeback_control *wbc) |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1622 | { |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1623 | int ret; |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 1624 | int force_cow = need_force_cow(inode, start, end); |
Liu Bo | f82b735 | 2017-10-23 23:18:16 -0600 | [diff] [blame] | 1625 | unsigned int write_flags = wbc_to_write_flags(wbc); |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1626 | |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 1627 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW && !force_cow) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1628 | ret = run_delalloc_nocow(inode, locked_page, start, end, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1629 | page_started, 1, nr_written); |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 1630 | } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC && !force_cow) { |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1631 | ret = run_delalloc_nocow(inode, locked_page, start, end, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1632 | page_started, 0, nr_written); |
Timofey Titovets | c2fcdcd | 2017-07-17 16:52:58 +0300 | [diff] [blame] | 1633 | } else if (!inode_need_compress(inode, start, end)) { |
Wang Xiaoguang | dda3245 | 2016-07-11 11:05:29 +0800 | [diff] [blame] | 1634 | ret = cow_file_range(inode, locked_page, start, end, end, |
| 1635 | page_started, nr_written, 1, NULL); |
Josef Bacik | 7ddf5a4 | 2012-06-08 15:26:47 -0400 | [diff] [blame] | 1636 | } else { |
| 1637 | set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, |
| 1638 | &BTRFS_I(inode)->runtime_flags); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1639 | ret = cow_file_range_async(inode, locked_page, start, end, |
Liu Bo | f82b735 | 2017-10-23 23:18:16 -0600 | [diff] [blame] | 1640 | page_started, nr_written, |
| 1641 | write_flags); |
Josef Bacik | 7ddf5a4 | 2012-06-08 15:26:47 -0400 | [diff] [blame] | 1642 | } |
Qu Wenruo | 52427260 | 2017-03-08 10:25:52 +0800 | [diff] [blame] | 1643 | if (ret) |
Nikolay Borisov | d1051d6 | 2018-11-21 17:10:52 +0200 | [diff] [blame] | 1644 | btrfs_cleanup_ordered_extents(inode, locked_page, start, |
| 1645 | end - start + 1); |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 1646 | return ret; |
| 1647 | } |
| 1648 | |
Nikolay Borisov | abbb55f | 2018-11-01 14:09:53 +0200 | [diff] [blame] | 1649 | void btrfs_split_delalloc_extent(struct inode *inode, |
| 1650 | struct extent_state *orig, u64 split) |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1651 | { |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1652 | u64 size; |
| 1653 | |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1654 | /* not delalloc, ignore it */ |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1655 | if (!(orig->state & EXTENT_DELALLOC)) |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 1656 | return; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1657 | |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1658 | size = orig->end - orig->start + 1; |
| 1659 | if (size > BTRFS_MAX_EXTENT_SIZE) { |
David Sterba | 823bb20 | 2017-01-04 11:09:51 +0100 | [diff] [blame] | 1660 | u32 num_extents; |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1661 | u64 new_size; |
| 1662 | |
| 1663 | /* |
Nikolay Borisov | 5c84819 | 2018-11-01 14:09:52 +0200 | [diff] [blame] | 1664 | * See the explanation in btrfs_merge_delalloc_extent, the same |
Josef Bacik | ba11721 | 2015-03-13 15:01:24 -0400 | [diff] [blame] | 1665 | * applies here, just in reverse. |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1666 | */ |
| 1667 | new_size = orig->end - split + 1; |
David Sterba | 823bb20 | 2017-01-04 11:09:51 +0100 | [diff] [blame] | 1668 | num_extents = count_max_extents(new_size); |
Josef Bacik | ba11721 | 2015-03-13 15:01:24 -0400 | [diff] [blame] | 1669 | new_size = split - orig->start; |
David Sterba | 823bb20 | 2017-01-04 11:09:51 +0100 | [diff] [blame] | 1670 | num_extents += count_max_extents(new_size); |
| 1671 | if (count_max_extents(size) >= num_extents) |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1672 | return; |
| 1673 | } |
| 1674 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1675 | spin_lock(&BTRFS_I(inode)->lock); |
Josef Bacik | 8b62f87 | 2017-10-19 14:15:55 -0400 | [diff] [blame] | 1676 | btrfs_mod_outstanding_extents(BTRFS_I(inode), 1); |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1677 | spin_unlock(&BTRFS_I(inode)->lock); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1678 | } |
| 1679 | |
| 1680 | /* |
Nikolay Borisov | 5c84819 | 2018-11-01 14:09:52 +0200 | [diff] [blame] | 1681 | * Handle merged delayed allocation extents so we can keep track of new extents |
| 1682 | * that are just merged onto old extents, such as when we are doing sequential |
| 1683 | * 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] | 1684 | */ |
Nikolay Borisov | 5c84819 | 2018-11-01 14:09:52 +0200 | [diff] [blame] | 1685 | void btrfs_merge_delalloc_extent(struct inode *inode, struct extent_state *new, |
| 1686 | struct extent_state *other) |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1687 | { |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1688 | u64 new_size, old_size; |
David Sterba | 823bb20 | 2017-01-04 11:09:51 +0100 | [diff] [blame] | 1689 | u32 num_extents; |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1690 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1691 | /* not delalloc, ignore it */ |
| 1692 | if (!(other->state & EXTENT_DELALLOC)) |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 1693 | return; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1694 | |
Josef Bacik | 8461a3d | 2015-03-13 15:12:08 -0400 | [diff] [blame] | 1695 | if (new->start > other->start) |
| 1696 | new_size = new->end - other->start + 1; |
| 1697 | else |
| 1698 | new_size = other->end - new->start + 1; |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1699 | |
| 1700 | /* we're not bigger than the max, unreserve the space and go */ |
| 1701 | if (new_size <= BTRFS_MAX_EXTENT_SIZE) { |
| 1702 | spin_lock(&BTRFS_I(inode)->lock); |
Josef Bacik | 8b62f87 | 2017-10-19 14:15:55 -0400 | [diff] [blame] | 1703 | btrfs_mod_outstanding_extents(BTRFS_I(inode), -1); |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1704 | spin_unlock(&BTRFS_I(inode)->lock); |
| 1705 | return; |
| 1706 | } |
| 1707 | |
| 1708 | /* |
Josef Bacik | ba11721 | 2015-03-13 15:01:24 -0400 | [diff] [blame] | 1709 | * We have to add up either side to figure out how many extents were |
| 1710 | * accounted for before we merged into one big extent. If the number of |
| 1711 | * extents we accounted for is <= the amount we need for the new range |
| 1712 | * then we can return, otherwise drop. Think of it like this |
| 1713 | * |
| 1714 | * [ 4k][MAX_SIZE] |
| 1715 | * |
| 1716 | * So we've grown the extent by a MAX_SIZE extent, this would mean we |
| 1717 | * need 2 outstanding extents, on one side we have 1 and the other side |
| 1718 | * we have 1 so they are == and we can return. But in this case |
| 1719 | * |
| 1720 | * [MAX_SIZE+4k][MAX_SIZE+4k] |
| 1721 | * |
| 1722 | * Each range on their own accounts for 2 extents, but merged together |
| 1723 | * they are only 3 extents worth of accounting, so we need to drop in |
| 1724 | * this case. |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1725 | */ |
Josef Bacik | ba11721 | 2015-03-13 15:01:24 -0400 | [diff] [blame] | 1726 | old_size = other->end - other->start + 1; |
David Sterba | 823bb20 | 2017-01-04 11:09:51 +0100 | [diff] [blame] | 1727 | num_extents = count_max_extents(old_size); |
Josef Bacik | ba11721 | 2015-03-13 15:01:24 -0400 | [diff] [blame] | 1728 | old_size = new->end - new->start + 1; |
David Sterba | 823bb20 | 2017-01-04 11:09:51 +0100 | [diff] [blame] | 1729 | num_extents += count_max_extents(old_size); |
| 1730 | if (count_max_extents(new_size) >= num_extents) |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1731 | return; |
| 1732 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1733 | spin_lock(&BTRFS_I(inode)->lock); |
Josef Bacik | 8b62f87 | 2017-10-19 14:15:55 -0400 | [diff] [blame] | 1734 | btrfs_mod_outstanding_extents(BTRFS_I(inode), -1); |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1735 | spin_unlock(&BTRFS_I(inode)->lock); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1736 | } |
| 1737 | |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 1738 | static void btrfs_add_delalloc_inodes(struct btrfs_root *root, |
| 1739 | struct inode *inode) |
| 1740 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1741 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
| 1742 | |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 1743 | spin_lock(&root->delalloc_lock); |
| 1744 | if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) { |
| 1745 | list_add_tail(&BTRFS_I(inode)->delalloc_inodes, |
| 1746 | &root->delalloc_inodes); |
| 1747 | set_bit(BTRFS_INODE_IN_DELALLOC_LIST, |
| 1748 | &BTRFS_I(inode)->runtime_flags); |
| 1749 | root->nr_delalloc_inodes++; |
| 1750 | if (root->nr_delalloc_inodes == 1) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1751 | spin_lock(&fs_info->delalloc_root_lock); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 1752 | BUG_ON(!list_empty(&root->delalloc_root)); |
| 1753 | list_add_tail(&root->delalloc_root, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1754 | &fs_info->delalloc_roots); |
| 1755 | spin_unlock(&fs_info->delalloc_root_lock); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 1756 | } |
| 1757 | } |
| 1758 | spin_unlock(&root->delalloc_lock); |
| 1759 | } |
| 1760 | |
Nikolay Borisov | 2b87733 | 2018-04-27 12:21:51 +0300 | [diff] [blame] | 1761 | |
| 1762 | void __btrfs_del_delalloc_inode(struct btrfs_root *root, |
| 1763 | struct btrfs_inode *inode) |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 1764 | { |
David Sterba | 3ffbd68 | 2018-06-29 10:56:42 +0200 | [diff] [blame] | 1765 | struct btrfs_fs_info *fs_info = root->fs_info; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1766 | |
Nikolay Borisov | 9e3e97f | 2017-02-20 13:51:07 +0200 | [diff] [blame] | 1767 | if (!list_empty(&inode->delalloc_inodes)) { |
| 1768 | list_del_init(&inode->delalloc_inodes); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 1769 | clear_bit(BTRFS_INODE_IN_DELALLOC_LIST, |
Nikolay Borisov | 9e3e97f | 2017-02-20 13:51:07 +0200 | [diff] [blame] | 1770 | &inode->runtime_flags); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 1771 | root->nr_delalloc_inodes--; |
| 1772 | if (!root->nr_delalloc_inodes) { |
Nikolay Borisov | 7c8a0d3 | 2018-04-27 12:21:52 +0300 | [diff] [blame] | 1773 | ASSERT(list_empty(&root->delalloc_inodes)); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1774 | spin_lock(&fs_info->delalloc_root_lock); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 1775 | BUG_ON(list_empty(&root->delalloc_root)); |
| 1776 | list_del_init(&root->delalloc_root); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1777 | spin_unlock(&fs_info->delalloc_root_lock); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 1778 | } |
| 1779 | } |
Nikolay Borisov | 2b87733 | 2018-04-27 12:21:51 +0300 | [diff] [blame] | 1780 | } |
| 1781 | |
| 1782 | static void btrfs_del_delalloc_inode(struct btrfs_root *root, |
| 1783 | struct btrfs_inode *inode) |
| 1784 | { |
| 1785 | spin_lock(&root->delalloc_lock); |
| 1786 | __btrfs_del_delalloc_inode(root, inode); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 1787 | spin_unlock(&root->delalloc_lock); |
| 1788 | } |
| 1789 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1790 | /* |
Nikolay Borisov | e06a1fc | 2018-11-01 14:09:50 +0200 | [diff] [blame] | 1791 | * Properly track delayed allocation bytes in the inode and to maintain the |
| 1792 | * list of inodes that have pending delalloc work to be done. |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1793 | */ |
Nikolay Borisov | e06a1fc | 2018-11-01 14:09:50 +0200 | [diff] [blame] | 1794 | void btrfs_set_delalloc_extent(struct inode *inode, struct extent_state *state, |
| 1795 | unsigned *bits) |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1796 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1797 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
| 1798 | |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 1799 | if ((*bits & EXTENT_DEFRAG) && !(*bits & EXTENT_DELALLOC)) |
| 1800 | WARN_ON(1); |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1801 | /* |
| 1802 | * set_bit and clear bit hooks normally require _irqsave/restore |
Sergei Trofimovich | 27160b6 | 2011-05-20 20:20:32 +0000 | [diff] [blame] | 1803 | * but in this case, we are only testing for the DELALLOC |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1804 | * bit, which is only set or cleared with irqs on |
| 1805 | */ |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1806 | if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) { |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1807 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1808 | u64 len = state->end + 1 - state->start; |
Josef Bacik | 8b62f87 | 2017-10-19 14:15:55 -0400 | [diff] [blame] | 1809 | u32 num_extents = count_max_extents(len); |
Nikolay Borisov | 70ddc55 | 2017-02-20 13:50:35 +0200 | [diff] [blame] | 1810 | bool do_list = !btrfs_is_free_space_inode(BTRFS_I(inode)); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1811 | |
Josef Bacik | 8b62f87 | 2017-10-19 14:15:55 -0400 | [diff] [blame] | 1812 | spin_lock(&BTRFS_I(inode)->lock); |
| 1813 | btrfs_mod_outstanding_extents(BTRFS_I(inode), num_extents); |
| 1814 | spin_unlock(&BTRFS_I(inode)->lock); |
Josef Bacik | 287a0ab | 2010-03-19 18:07:23 +0000 | [diff] [blame] | 1815 | |
Josef Bacik | 6a3891c | 2015-03-16 17:38:52 -0400 | [diff] [blame] | 1816 | /* For sanity tests */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1817 | if (btrfs_is_testing(fs_info)) |
Josef Bacik | 6a3891c | 2015-03-16 17:38:52 -0400 | [diff] [blame] | 1818 | return; |
| 1819 | |
Nikolay Borisov | 104b4e5 | 2017-06-20 21:01:20 +0300 | [diff] [blame] | 1820 | percpu_counter_add_batch(&fs_info->delalloc_bytes, len, |
| 1821 | fs_info->delalloc_batch); |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 1822 | spin_lock(&BTRFS_I(inode)->lock); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1823 | BTRFS_I(inode)->delalloc_bytes += len; |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 1824 | if (*bits & EXTENT_DEFRAG) |
| 1825 | BTRFS_I(inode)->defrag_bytes += len; |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 1826 | if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST, |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 1827 | &BTRFS_I(inode)->runtime_flags)) |
| 1828 | btrfs_add_delalloc_inodes(root, inode); |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 1829 | spin_unlock(&BTRFS_I(inode)->lock); |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1830 | } |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 1831 | |
| 1832 | if (!(state->state & EXTENT_DELALLOC_NEW) && |
| 1833 | (*bits & EXTENT_DELALLOC_NEW)) { |
| 1834 | spin_lock(&BTRFS_I(inode)->lock); |
| 1835 | BTRFS_I(inode)->new_delalloc_bytes += state->end + 1 - |
| 1836 | state->start; |
| 1837 | spin_unlock(&BTRFS_I(inode)->lock); |
| 1838 | } |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1839 | } |
| 1840 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1841 | /* |
Nikolay Borisov | a36bb5f | 2018-11-01 14:09:51 +0200 | [diff] [blame] | 1842 | * Once a range is no longer delalloc this function ensures that proper |
| 1843 | * accounting happens. |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1844 | */ |
Nikolay Borisov | a36bb5f | 2018-11-01 14:09:51 +0200 | [diff] [blame] | 1845 | void btrfs_clear_delalloc_extent(struct inode *vfs_inode, |
| 1846 | struct extent_state *state, unsigned *bits) |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1847 | { |
Nikolay Borisov | a36bb5f | 2018-11-01 14:09:51 +0200 | [diff] [blame] | 1848 | struct btrfs_inode *inode = BTRFS_I(vfs_inode); |
| 1849 | struct btrfs_fs_info *fs_info = btrfs_sb(vfs_inode->i_sb); |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 1850 | u64 len = state->end + 1 - state->start; |
David Sterba | 823bb20 | 2017-01-04 11:09:51 +0100 | [diff] [blame] | 1851 | u32 num_extents = count_max_extents(len); |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 1852 | |
Filipe Manana | 4a4b964 | 2017-07-27 19:52:55 +0100 | [diff] [blame] | 1853 | if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG)) { |
| 1854 | spin_lock(&inode->lock); |
Nikolay Borisov | 6fc0ef6 | 2017-02-20 13:51:03 +0200 | [diff] [blame] | 1855 | inode->defrag_bytes -= len; |
Filipe Manana | 4a4b964 | 2017-07-27 19:52:55 +0100 | [diff] [blame] | 1856 | spin_unlock(&inode->lock); |
| 1857 | } |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 1858 | |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1859 | /* |
| 1860 | * set_bit and clear bit hooks normally require _irqsave/restore |
Sergei Trofimovich | 27160b6 | 2011-05-20 20:20:32 +0000 | [diff] [blame] | 1861 | * but in this case, we are only testing for the DELALLOC |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1862 | * bit, which is only set or cleared with irqs on |
| 1863 | */ |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1864 | if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) { |
Nikolay Borisov | 6fc0ef6 | 2017-02-20 13:51:03 +0200 | [diff] [blame] | 1865 | struct btrfs_root *root = inode->root; |
Liu Bo | 83eea1f | 2012-07-10 05:28:39 -0600 | [diff] [blame] | 1866 | bool do_list = !btrfs_is_free_space_inode(inode); |
Chris Mason | bcbfce8 | 2008-04-22 13:26:47 -0400 | [diff] [blame] | 1867 | |
Josef Bacik | 8b62f87 | 2017-10-19 14:15:55 -0400 | [diff] [blame] | 1868 | spin_lock(&inode->lock); |
| 1869 | btrfs_mod_outstanding_extents(inode, -num_extents); |
| 1870 | spin_unlock(&inode->lock); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1871 | |
Josef Bacik | b6d08f0 | 2013-09-27 14:57:43 -0400 | [diff] [blame] | 1872 | /* |
| 1873 | * We don't reserve metadata space for space cache inodes so we |
Andrea Gelmini | 52042d8 | 2018-11-28 12:05:13 +0100 | [diff] [blame] | 1874 | * don't need to call delalloc_release_metadata if there is an |
Josef Bacik | b6d08f0 | 2013-09-27 14:57:43 -0400 | [diff] [blame] | 1875 | * error. |
| 1876 | */ |
Filipe Manana | a315e68 | 2017-03-06 23:04:20 +0000 | [diff] [blame] | 1877 | if (*bits & EXTENT_CLEAR_META_RESV && |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1878 | root != fs_info->tree_root) |
Qu Wenruo | 43b1859 | 2017-12-12 15:34:32 +0800 | [diff] [blame] | 1879 | btrfs_delalloc_release_metadata(inode, len, false); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1880 | |
Josef Bacik | 6a3891c | 2015-03-16 17:38:52 -0400 | [diff] [blame] | 1881 | /* For sanity tests. */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1882 | if (btrfs_is_testing(fs_info)) |
Josef Bacik | 6a3891c | 2015-03-16 17:38:52 -0400 | [diff] [blame] | 1883 | return; |
| 1884 | |
Filipe Manana | a315e68 | 2017-03-06 23:04:20 +0000 | [diff] [blame] | 1885 | if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID && |
| 1886 | do_list && !(state->state & EXTENT_NORESERVE) && |
| 1887 | (*bits & EXTENT_CLEAR_DATA_RESV)) |
Nikolay Borisov | 6fc0ef6 | 2017-02-20 13:51:03 +0200 | [diff] [blame] | 1888 | btrfs_free_reserved_data_space_noquota( |
| 1889 | &inode->vfs_inode, |
Qu Wenruo | 51773be | 2015-10-08 18:19:37 +0800 | [diff] [blame] | 1890 | state->start, len); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1891 | |
Nikolay Borisov | 104b4e5 | 2017-06-20 21:01:20 +0300 | [diff] [blame] | 1892 | percpu_counter_add_batch(&fs_info->delalloc_bytes, -len, |
| 1893 | fs_info->delalloc_batch); |
Nikolay Borisov | 6fc0ef6 | 2017-02-20 13:51:03 +0200 | [diff] [blame] | 1894 | spin_lock(&inode->lock); |
| 1895 | inode->delalloc_bytes -= len; |
| 1896 | if (do_list && inode->delalloc_bytes == 0 && |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 1897 | test_bit(BTRFS_INODE_IN_DELALLOC_LIST, |
Nikolay Borisov | 9e3e97f | 2017-02-20 13:51:07 +0200 | [diff] [blame] | 1898 | &inode->runtime_flags)) |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 1899 | btrfs_del_delalloc_inode(root, inode); |
Nikolay Borisov | 6fc0ef6 | 2017-02-20 13:51:03 +0200 | [diff] [blame] | 1900 | spin_unlock(&inode->lock); |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1901 | } |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 1902 | |
| 1903 | if ((state->state & EXTENT_DELALLOC_NEW) && |
| 1904 | (*bits & EXTENT_DELALLOC_NEW)) { |
| 1905 | spin_lock(&inode->lock); |
| 1906 | ASSERT(inode->new_delalloc_bytes >= len); |
| 1907 | inode->new_delalloc_bytes -= len; |
| 1908 | spin_unlock(&inode->lock); |
| 1909 | } |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1910 | } |
| 1911 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1912 | /* |
Nikolay Borisov | da12fe5 | 2018-11-27 20:57:58 +0200 | [diff] [blame] | 1913 | * btrfs_bio_fits_in_stripe - Checks whether the size of the given bio will fit |
| 1914 | * in a chunk's stripe. This function ensures that bios do not span a |
| 1915 | * stripe/chunk |
Liu Bo | 6f034ec | 2016-06-22 18:31:49 -0700 | [diff] [blame] | 1916 | * |
Nikolay Borisov | da12fe5 | 2018-11-27 20:57:58 +0200 | [diff] [blame] | 1917 | * @page - The page we are about to add to the bio |
| 1918 | * @size - size we want to add to the bio |
| 1919 | * @bio - bio we want to ensure is smaller than a stripe |
| 1920 | * @bio_flags - flags of the bio |
| 1921 | * |
| 1922 | * return 1 if page cannot be added to the bio |
| 1923 | * return 0 if page can be added to the bio |
Liu Bo | 6f034ec | 2016-06-22 18:31:49 -0700 | [diff] [blame] | 1924 | * return error otherwise |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1925 | */ |
Nikolay Borisov | da12fe5 | 2018-11-27 20:57:58 +0200 | [diff] [blame] | 1926 | int btrfs_bio_fits_in_stripe(struct page *page, size_t size, struct bio *bio, |
| 1927 | unsigned long bio_flags) |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1928 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1929 | struct inode *inode = page->mapping->host; |
| 1930 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 1931 | u64 logical = (u64)bio->bi_iter.bi_sector << 9; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1932 | u64 length = 0; |
| 1933 | u64 map_length; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1934 | int ret; |
| 1935 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1936 | if (bio_flags & EXTENT_BIO_COMPRESSED) |
| 1937 | return 0; |
| 1938 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 1939 | length = bio->bi_iter.bi_size; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1940 | map_length = length; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1941 | ret = btrfs_map_block(fs_info, btrfs_op(bio), logical, &map_length, |
| 1942 | NULL, 0); |
Liu Bo | 6f034ec | 2016-06-22 18:31:49 -0700 | [diff] [blame] | 1943 | if (ret < 0) |
| 1944 | return ret; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1945 | if (map_length < length + size) |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1946 | return 1; |
Jeff Mahoney | 3444a97 | 2011-10-03 23:23:13 -0400 | [diff] [blame] | 1947 | return 0; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1948 | } |
| 1949 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1950 | /* |
| 1951 | * in order to insert checksums into the metadata in large chunks, |
| 1952 | * we wait until bio submission time. All the pages in the bio are |
| 1953 | * checksummed and sums are attached onto the ordered extent record. |
| 1954 | * |
| 1955 | * At IO completion time the cums attached on the ordered extent record |
| 1956 | * are inserted into the btree |
| 1957 | */ |
David Sterba | d0ee393 | 2018-03-08 14:35:48 +0100 | [diff] [blame] | 1958 | 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] | 1959 | u64 bio_offset) |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 1960 | { |
Josef Bacik | c6100a4 | 2017-05-05 11:57:13 -0400 | [diff] [blame] | 1961 | struct inode *inode = private_data; |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 1962 | blk_status_t ret = 0; |
Chris Mason | e015640 | 2008-04-16 11:15:20 -0400 | [diff] [blame] | 1963 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 1964 | ret = btrfs_csum_one_bio(inode, bio, 0, 0); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1965 | BUG_ON(ret); /* -ENOMEM */ |
Chris Mason | 4a69a41 | 2008-11-06 22:03:00 -0500 | [diff] [blame] | 1966 | return 0; |
| 1967 | } |
Chris Mason | e015640 | 2008-04-16 11:15:20 -0400 | [diff] [blame] | 1968 | |
Chris Mason | 4a69a41 | 2008-11-06 22:03:00 -0500 | [diff] [blame] | 1969 | /* |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1970 | * 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] | 1971 | * on write, or reading the csums from the tree before a read. |
| 1972 | * |
| 1973 | * Rules about async/sync submit, |
| 1974 | * a) read: sync submit |
| 1975 | * |
| 1976 | * b) write without checksum: sync submit |
| 1977 | * |
| 1978 | * c) write with checksum: |
| 1979 | * c-1) if bio is issued by fsync: sync submit |
| 1980 | * (sync_writers != 0) |
| 1981 | * |
| 1982 | * c-2) if root is reloc root: sync submit |
| 1983 | * (only in case of buffered IO) |
| 1984 | * |
| 1985 | * c-3) otherwise: async submit |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1986 | */ |
Nikolay Borisov | a56b1c7 | 2019-04-10 17:24:39 +0300 | [diff] [blame] | 1987 | static blk_status_t btrfs_submit_bio_hook(struct inode *inode, struct bio *bio, |
Nikolay Borisov | 50489a5 | 2019-04-10 19:46:04 +0300 | [diff] [blame] | 1988 | int mirror_num, |
| 1989 | unsigned long bio_flags) |
| 1990 | |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 1991 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1992 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 1993 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chandan Rajendra | 0d51e28 | 2015-07-27 15:26:43 +0530 | [diff] [blame] | 1994 | enum btrfs_wq_endio_type metadata = BTRFS_WQ_ENDIO_DATA; |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 1995 | blk_status_t ret = 0; |
Chris Mason | 19b9bdb | 2008-10-30 14:23:13 -0400 | [diff] [blame] | 1996 | int skip_sum; |
Josef Bacik | b812ce2 | 2012-11-16 13:56:32 -0500 | [diff] [blame] | 1997 | int async = !atomic_read(&BTRFS_I(inode)->sync_writers); |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 1998 | |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 1999 | skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM; |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 2000 | |
Nikolay Borisov | 70ddc55 | 2017-02-20 13:50:35 +0200 | [diff] [blame] | 2001 | if (btrfs_is_free_space_inode(BTRFS_I(inode))) |
Chandan Rajendra | 0d51e28 | 2015-07-27 15:26:43 +0530 | [diff] [blame] | 2002 | metadata = BTRFS_WQ_ENDIO_FREE_SPACE; |
Jeff Mahoney | 0417341 | 2011-10-03 23:23:12 -0400 | [diff] [blame] | 2003 | |
Mike Christie | 37226b2 | 2016-06-05 14:31:52 -0500 | [diff] [blame] | 2004 | if (bio_op(bio) != REQ_OP_WRITE) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2005 | ret = btrfs_bio_wq_end_io(fs_info, bio, metadata); |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2006 | if (ret) |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 2007 | goto out; |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2008 | |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 2009 | if (bio_flags & EXTENT_BIO_COMPRESSED) { |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 2010 | ret = btrfs_submit_compressed_read(inode, bio, |
| 2011 | mirror_num, |
| 2012 | bio_flags); |
| 2013 | goto out; |
Tsutomu Itoh | c2db107 | 2011-03-01 06:48:31 +0000 | [diff] [blame] | 2014 | } else if (!skip_sum) { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2015 | ret = btrfs_lookup_bio_sums(inode, bio, NULL); |
Tsutomu Itoh | c2db107 | 2011-03-01 06:48:31 +0000 | [diff] [blame] | 2016 | if (ret) |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 2017 | goto out; |
Tsutomu Itoh | c2db107 | 2011-03-01 06:48:31 +0000 | [diff] [blame] | 2018 | } |
Chris Mason | 4d1b5fb | 2008-08-20 09:44:52 -0400 | [diff] [blame] | 2019 | goto mapit; |
Josef Bacik | b812ce2 | 2012-11-16 13:56:32 -0500 | [diff] [blame] | 2020 | } else if (async && !skip_sum) { |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 2021 | /* csum items have already been cloned */ |
| 2022 | if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID) |
| 2023 | goto mapit; |
Chris Mason | 19b9bdb | 2008-10-30 14:23:13 -0400 | [diff] [blame] | 2024 | /* we're doing a write, do the async checksumming */ |
Josef Bacik | c6100a4 | 2017-05-05 11:57:13 -0400 | [diff] [blame] | 2025 | ret = btrfs_wq_submit_bio(fs_info, bio, mirror_num, bio_flags, |
Nikolay Borisov | e768116 | 2019-04-10 17:24:41 +0300 | [diff] [blame] | 2026 | 0, inode, btrfs_submit_bio_start); |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 2027 | goto out; |
Josef Bacik | b812ce2 | 2012-11-16 13:56:32 -0500 | [diff] [blame] | 2028 | } else if (!skip_sum) { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2029 | ret = btrfs_csum_one_bio(inode, bio, 0, 0); |
Josef Bacik | b812ce2 | 2012-11-16 13:56:32 -0500 | [diff] [blame] | 2030 | if (ret) |
| 2031 | goto out; |
Chris Mason | 19b9bdb | 2008-10-30 14:23:13 -0400 | [diff] [blame] | 2032 | } |
| 2033 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2034 | mapit: |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2035 | ret = btrfs_map_bio(fs_info, bio, mirror_num, 0); |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 2036 | |
| 2037 | out: |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 2038 | if (ret) { |
| 2039 | bio->bi_status = ret; |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 2040 | bio_endio(bio); |
| 2041 | } |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 2042 | return ret; |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 2043 | } |
Chris Mason | 6885f30 | 2008-02-20 16:11:05 -0500 | [diff] [blame] | 2044 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2045 | /* |
| 2046 | * given a list of ordered sums record them in the inode. This happens |
| 2047 | * at IO completion time based on sums calculated at bio submission time. |
| 2048 | */ |
Chris Mason | ba1da2f | 2008-07-17 12:54:15 -0400 | [diff] [blame] | 2049 | static noinline int add_pending_csums(struct btrfs_trans_handle *trans, |
David Sterba | df9f628 | 2017-02-10 19:35:37 +0100 | [diff] [blame] | 2050 | struct inode *inode, struct list_head *list) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2051 | { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2052 | struct btrfs_ordered_sum *sum; |
Nikolay Borisov | ac01f26 | 2018-01-08 10:59:43 +0200 | [diff] [blame] | 2053 | int ret; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2054 | |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 2055 | list_for_each_entry(sum, list, list) { |
David Sterba | 7c2871a | 2017-11-08 01:07:43 +0100 | [diff] [blame] | 2056 | trans->adding_csums = true; |
Nikolay Borisov | ac01f26 | 2018-01-08 10:59:43 +0200 | [diff] [blame] | 2057 | ret = btrfs_csum_file_blocks(trans, |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 2058 | BTRFS_I(inode)->root->fs_info->csum_root, sum); |
David Sterba | 7c2871a | 2017-11-08 01:07:43 +0100 | [diff] [blame] | 2059 | trans->adding_csums = false; |
Nikolay Borisov | ac01f26 | 2018-01-08 10:59:43 +0200 | [diff] [blame] | 2060 | if (ret) |
| 2061 | return ret; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2062 | } |
| 2063 | return 0; |
| 2064 | } |
| 2065 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 2066 | int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end, |
Filipe Manana | e3b8a48 | 2017-11-04 00:16:59 +0000 | [diff] [blame] | 2067 | unsigned int extra_bits, |
Qu Wenruo | ba8b04c | 2016-07-19 16:50:36 +0800 | [diff] [blame] | 2068 | struct extent_state **cached_state, int dedupe) |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 2069 | { |
Johannes Thumshirn | fdb1e12 | 2018-12-05 15:23:04 +0100 | [diff] [blame] | 2070 | WARN_ON(PAGE_ALIGNED(end)); |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 2071 | return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end, |
Filipe Manana | e3b8a48 | 2017-11-04 00:16:59 +0000 | [diff] [blame] | 2072 | extra_bits, cached_state); |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 2073 | } |
| 2074 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2075 | /* see btrfs_writepage_start_hook for details on why this is required */ |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2076 | struct btrfs_writepage_fixup { |
| 2077 | struct page *page; |
| 2078 | struct btrfs_work work; |
| 2079 | }; |
| 2080 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 2081 | static void btrfs_writepage_fixup_worker(struct btrfs_work *work) |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2082 | { |
| 2083 | struct btrfs_writepage_fixup *fixup; |
| 2084 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 2085 | struct extent_state *cached_state = NULL; |
Qu Wenruo | 364ecf3 | 2017-02-27 15:10:38 +0800 | [diff] [blame] | 2086 | struct extent_changeset *data_reserved = NULL; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2087 | struct page *page; |
| 2088 | struct inode *inode; |
| 2089 | u64 page_start; |
| 2090 | u64 page_end; |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 2091 | int ret; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2092 | |
| 2093 | fixup = container_of(work, struct btrfs_writepage_fixup, work); |
| 2094 | page = fixup->page; |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 2095 | again: |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2096 | lock_page(page); |
| 2097 | if (!page->mapping || !PageDirty(page) || !PageChecked(page)) { |
| 2098 | ClearPageChecked(page); |
| 2099 | goto out_page; |
| 2100 | } |
| 2101 | |
| 2102 | inode = page->mapping->host; |
| 2103 | page_start = page_offset(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2104 | page_end = page_offset(page) + PAGE_SIZE - 1; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2105 | |
David Sterba | ff13db4 | 2015-12-03 14:30:40 +0100 | [diff] [blame] | 2106 | lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end, |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 2107 | &cached_state); |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 2108 | |
| 2109 | /* already ordered? We're done */ |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 2110 | if (PagePrivate2(page)) |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2111 | goto out; |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 2112 | |
Nikolay Borisov | a776c6f | 2017-02-20 13:50:49 +0200 | [diff] [blame] | 2113 | ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start, |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2114 | PAGE_SIZE); |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 2115 | if (ordered) { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 2116 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, |
David Sterba | e43bbe5 | 2017-12-12 21:43:52 +0100 | [diff] [blame] | 2117 | page_end, &cached_state); |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 2118 | unlock_page(page); |
| 2119 | btrfs_start_ordered_extent(inode, ordered, 1); |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 2120 | btrfs_put_ordered_extent(ordered); |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 2121 | goto again; |
| 2122 | } |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2123 | |
Qu Wenruo | 364ecf3 | 2017-02-27 15:10:38 +0800 | [diff] [blame] | 2124 | ret = btrfs_delalloc_reserve_space(inode, &data_reserved, page_start, |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2125 | PAGE_SIZE); |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 2126 | if (ret) { |
| 2127 | mapping_set_error(page->mapping, ret); |
| 2128 | end_extent_writepage(page, ret, page_start, page_end); |
| 2129 | ClearPageChecked(page); |
| 2130 | goto out; |
| 2131 | } |
| 2132 | |
Nikolay Borisov | f3038ee | 2017-12-05 09:29:19 +0200 | [diff] [blame] | 2133 | ret = btrfs_set_extent_delalloc(inode, page_start, page_end, 0, |
| 2134 | &cached_state, 0); |
| 2135 | if (ret) { |
| 2136 | mapping_set_error(page->mapping, ret); |
| 2137 | end_extent_writepage(page, ret, page_start, page_end); |
| 2138 | ClearPageChecked(page); |
| 2139 | goto out; |
| 2140 | } |
| 2141 | |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2142 | ClearPageChecked(page); |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 2143 | set_page_dirty(page); |
Qu Wenruo | 43b1859 | 2017-12-12 15:34:32 +0800 | [diff] [blame] | 2144 | btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE, false); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2145 | out: |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 2146 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end, |
David Sterba | e43bbe5 | 2017-12-12 21:43:52 +0100 | [diff] [blame] | 2147 | &cached_state); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2148 | out_page: |
| 2149 | unlock_page(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2150 | put_page(page); |
Miao Xie | b897abe | 2011-01-26 16:19:22 +0800 | [diff] [blame] | 2151 | kfree(fixup); |
Qu Wenruo | 364ecf3 | 2017-02-27 15:10:38 +0800 | [diff] [blame] | 2152 | extent_changeset_free(data_reserved); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2153 | } |
| 2154 | |
| 2155 | /* |
| 2156 | * There are a few paths in the higher layers of the kernel that directly |
| 2157 | * set the page dirty bit without asking the filesystem if it is a |
| 2158 | * good idea. This causes problems because we want to make sure COW |
| 2159 | * properly happens and the data=ordered rules are followed. |
| 2160 | * |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2161 | * 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] | 2162 | * hasn't been properly setup for IO. We kick off an async process |
| 2163 | * to fix it up. The async helper will wait for ordered extents, set |
| 2164 | * the delalloc bit and make it safe to write the page. |
| 2165 | */ |
Nikolay Borisov | d75855b | 2018-11-01 14:09:47 +0200 | [diff] [blame] | 2166 | int btrfs_writepage_cow_fixup(struct page *page, u64 start, u64 end) |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2167 | { |
| 2168 | struct inode *inode = page->mapping->host; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2169 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2170 | struct btrfs_writepage_fixup *fixup; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2171 | |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 2172 | /* this page is properly in the ordered list */ |
| 2173 | if (TestClearPagePrivate2(page)) |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2174 | return 0; |
| 2175 | |
| 2176 | if (PageChecked(page)) |
| 2177 | return -EAGAIN; |
| 2178 | |
| 2179 | fixup = kzalloc(sizeof(*fixup), GFP_NOFS); |
| 2180 | if (!fixup) |
| 2181 | return -EAGAIN; |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 2182 | |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2183 | SetPageChecked(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2184 | get_page(page); |
Liu Bo | 9e0af23 | 2014-08-15 23:36:53 +0800 | [diff] [blame] | 2185 | btrfs_init_work(&fixup->work, btrfs_fixup_helper, |
| 2186 | btrfs_writepage_fixup_worker, NULL, NULL); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2187 | fixup->page = page; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2188 | btrfs_queue_work(fs_info->fixup_workers, &fixup->work); |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 2189 | return -EBUSY; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2190 | } |
| 2191 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2192 | static int insert_reserved_file_extent(struct btrfs_trans_handle *trans, |
| 2193 | struct inode *inode, u64 file_pos, |
| 2194 | u64 disk_bytenr, u64 disk_num_bytes, |
| 2195 | u64 num_bytes, u64 ram_bytes, |
| 2196 | u8 compression, u8 encryption, |
| 2197 | u16 other_encoding, int extent_type) |
| 2198 | { |
| 2199 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 2200 | struct btrfs_file_extent_item *fi; |
| 2201 | struct btrfs_path *path; |
| 2202 | struct extent_buffer *leaf; |
| 2203 | struct btrfs_key ins; |
Qu Wenruo | a12b877 | 2017-02-27 15:10:37 +0800 | [diff] [blame] | 2204 | u64 qg_released; |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 2205 | int extent_inserted = 0; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2206 | int ret; |
| 2207 | |
| 2208 | path = btrfs_alloc_path(); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 2209 | if (!path) |
| 2210 | return -ENOMEM; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2211 | |
Chris Mason | a1ed835 | 2009-09-11 12:27:37 -0400 | [diff] [blame] | 2212 | /* |
| 2213 | * we may be replacing one extent in the tree with another. |
| 2214 | * The new extent is pinned in the extent map, and we don't want |
| 2215 | * to drop it from the cache until it is completely in the btree. |
| 2216 | * |
| 2217 | * So, tell btrfs_drop_extents to leave this extent in the cache. |
| 2218 | * the caller is expected to unpin it and allow it to be merged |
| 2219 | * with the others. |
| 2220 | */ |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 2221 | ret = __btrfs_drop_extents(trans, root, inode, path, file_pos, |
| 2222 | file_pos + num_bytes, NULL, 0, |
| 2223 | 1, sizeof(*fi), &extent_inserted); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2224 | if (ret) |
| 2225 | goto out; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2226 | |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 2227 | if (!extent_inserted) { |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 2228 | ins.objectid = btrfs_ino(BTRFS_I(inode)); |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 2229 | ins.offset = file_pos; |
| 2230 | ins.type = BTRFS_EXTENT_DATA_KEY; |
| 2231 | |
| 2232 | path->leave_spinning = 1; |
| 2233 | ret = btrfs_insert_empty_item(trans, root, path, &ins, |
| 2234 | sizeof(*fi)); |
| 2235 | if (ret) |
| 2236 | goto out; |
| 2237 | } |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2238 | leaf = path->nodes[0]; |
| 2239 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 2240 | struct btrfs_file_extent_item); |
| 2241 | btrfs_set_file_extent_generation(leaf, fi, trans->transid); |
| 2242 | btrfs_set_file_extent_type(leaf, fi, extent_type); |
| 2243 | btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr); |
| 2244 | btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes); |
| 2245 | btrfs_set_file_extent_offset(leaf, fi, 0); |
| 2246 | btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes); |
| 2247 | btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes); |
| 2248 | btrfs_set_file_extent_compression(leaf, fi, compression); |
| 2249 | btrfs_set_file_extent_encryption(leaf, fi, encryption); |
| 2250 | btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 2251 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2252 | btrfs_mark_buffer_dirty(leaf); |
Josef Bacik | ce19533 | 2012-09-25 15:26:16 -0400 | [diff] [blame] | 2253 | btrfs_release_path(path); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2254 | |
| 2255 | inode_add_bytes(inode, num_bytes); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2256 | |
| 2257 | ins.objectid = disk_bytenr; |
| 2258 | ins.offset = disk_num_bytes; |
| 2259 | ins.type = BTRFS_EXTENT_ITEM_KEY; |
Qu Wenruo | a12b877 | 2017-02-27 15:10:37 +0800 | [diff] [blame] | 2260 | |
Qu Wenruo | 297d750 | 2015-09-08 17:08:37 +0800 | [diff] [blame] | 2261 | /* |
Qu Wenruo | 5846a3c | 2015-10-26 14:11:18 +0800 | [diff] [blame] | 2262 | * Release the reserved range from inode dirty range map, as it is |
| 2263 | * already moved into delayed_ref_head |
Qu Wenruo | 297d750 | 2015-09-08 17:08:37 +0800 | [diff] [blame] | 2264 | */ |
Qu Wenruo | a12b877 | 2017-02-27 15:10:37 +0800 | [diff] [blame] | 2265 | ret = btrfs_qgroup_release_data(inode, file_pos, ram_bytes); |
| 2266 | if (ret < 0) |
| 2267 | goto out; |
| 2268 | qg_released = ret; |
Josef Bacik | 84f7d8e | 2017-09-29 15:43:49 -0400 | [diff] [blame] | 2269 | ret = btrfs_alloc_reserved_file_extent(trans, root, |
| 2270 | btrfs_ino(BTRFS_I(inode)), |
| 2271 | file_pos, qg_released, &ins); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2272 | out: |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2273 | btrfs_free_path(path); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 2274 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2275 | return ret; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2276 | } |
| 2277 | |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2278 | /* snapshot-aware defrag */ |
| 2279 | struct sa_defrag_extent_backref { |
| 2280 | struct rb_node node; |
| 2281 | struct old_sa_defrag_extent *old; |
| 2282 | u64 root_id; |
| 2283 | u64 inum; |
| 2284 | u64 file_pos; |
| 2285 | u64 extent_offset; |
| 2286 | u64 num_bytes; |
| 2287 | u64 generation; |
| 2288 | }; |
| 2289 | |
| 2290 | struct old_sa_defrag_extent { |
| 2291 | struct list_head list; |
| 2292 | struct new_sa_defrag_extent *new; |
| 2293 | |
| 2294 | u64 extent_offset; |
| 2295 | u64 bytenr; |
| 2296 | u64 offset; |
| 2297 | u64 len; |
| 2298 | int count; |
| 2299 | }; |
| 2300 | |
| 2301 | struct new_sa_defrag_extent { |
| 2302 | struct rb_root root; |
| 2303 | struct list_head head; |
| 2304 | struct btrfs_path *path; |
| 2305 | struct inode *inode; |
| 2306 | u64 file_pos; |
| 2307 | u64 len; |
| 2308 | u64 bytenr; |
| 2309 | u64 disk_len; |
| 2310 | u8 compress_type; |
| 2311 | }; |
| 2312 | |
| 2313 | static int backref_comp(struct sa_defrag_extent_backref *b1, |
| 2314 | struct sa_defrag_extent_backref *b2) |
| 2315 | { |
| 2316 | if (b1->root_id < b2->root_id) |
| 2317 | return -1; |
| 2318 | else if (b1->root_id > b2->root_id) |
| 2319 | return 1; |
| 2320 | |
| 2321 | if (b1->inum < b2->inum) |
| 2322 | return -1; |
| 2323 | else if (b1->inum > b2->inum) |
| 2324 | return 1; |
| 2325 | |
| 2326 | if (b1->file_pos < b2->file_pos) |
| 2327 | return -1; |
| 2328 | else if (b1->file_pos > b2->file_pos) |
| 2329 | return 1; |
| 2330 | |
| 2331 | /* |
| 2332 | * [------------------------------] ===> (a range of space) |
| 2333 | * |<--->| |<---->| =============> (fs/file tree A) |
| 2334 | * |<---------------------------->| ===> (fs/file tree B) |
| 2335 | * |
| 2336 | * A range of space can refer to two file extents in one tree while |
| 2337 | * refer to only one file extent in another tree. |
| 2338 | * |
| 2339 | * So we may process a disk offset more than one time(two extents in A) |
| 2340 | * and locate at the same extent(one extent in B), then insert two same |
| 2341 | * backrefs(both refer to the extent in B). |
| 2342 | */ |
| 2343 | return 0; |
| 2344 | } |
| 2345 | |
| 2346 | static void backref_insert(struct rb_root *root, |
| 2347 | struct sa_defrag_extent_backref *backref) |
| 2348 | { |
| 2349 | struct rb_node **p = &root->rb_node; |
| 2350 | struct rb_node *parent = NULL; |
| 2351 | struct sa_defrag_extent_backref *entry; |
| 2352 | int ret; |
| 2353 | |
| 2354 | while (*p) { |
| 2355 | parent = *p; |
| 2356 | entry = rb_entry(parent, struct sa_defrag_extent_backref, node); |
| 2357 | |
| 2358 | ret = backref_comp(backref, entry); |
| 2359 | if (ret < 0) |
| 2360 | p = &(*p)->rb_left; |
| 2361 | else |
| 2362 | p = &(*p)->rb_right; |
| 2363 | } |
| 2364 | |
| 2365 | rb_link_node(&backref->node, parent, p); |
| 2366 | rb_insert_color(&backref->node, root); |
| 2367 | } |
| 2368 | |
| 2369 | /* |
| 2370 | * Note the backref might has changed, and in this case we just return 0. |
| 2371 | */ |
| 2372 | static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id, |
| 2373 | void *ctx) |
| 2374 | { |
| 2375 | struct btrfs_file_extent_item *extent; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2376 | struct old_sa_defrag_extent *old = ctx; |
| 2377 | struct new_sa_defrag_extent *new = old->new; |
| 2378 | struct btrfs_path *path = new->path; |
| 2379 | struct btrfs_key key; |
| 2380 | struct btrfs_root *root; |
| 2381 | struct sa_defrag_extent_backref *backref; |
| 2382 | struct extent_buffer *leaf; |
| 2383 | struct inode *inode = new->inode; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2384 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2385 | int slot; |
| 2386 | int ret; |
| 2387 | u64 extent_offset; |
| 2388 | u64 num_bytes; |
| 2389 | |
| 2390 | if (BTRFS_I(inode)->root->root_key.objectid == root_id && |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 2391 | inum == btrfs_ino(BTRFS_I(inode))) |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2392 | return 0; |
| 2393 | |
| 2394 | key.objectid = root_id; |
| 2395 | key.type = BTRFS_ROOT_ITEM_KEY; |
| 2396 | key.offset = (u64)-1; |
| 2397 | |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2398 | root = btrfs_read_fs_root_no_name(fs_info, &key); |
| 2399 | if (IS_ERR(root)) { |
| 2400 | if (PTR_ERR(root) == -ENOENT) |
| 2401 | return 0; |
| 2402 | WARN_ON(1); |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 2403 | btrfs_debug(fs_info, "inum=%llu, offset=%llu, root_id=%llu", |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2404 | inum, offset, root_id); |
| 2405 | return PTR_ERR(root); |
| 2406 | } |
| 2407 | |
| 2408 | key.objectid = inum; |
| 2409 | key.type = BTRFS_EXTENT_DATA_KEY; |
| 2410 | if (offset > (u64)-1 << 32) |
| 2411 | key.offset = 0; |
| 2412 | else |
| 2413 | key.offset = offset; |
| 2414 | |
| 2415 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
Dulshani Gunawardhana | fae7f21 | 2013-10-31 10:30:08 +0530 | [diff] [blame] | 2416 | if (WARN_ON(ret < 0)) |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2417 | return ret; |
Josef Bacik | 50f1319 | 2013-07-22 12:50:37 -0400 | [diff] [blame] | 2418 | ret = 0; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2419 | |
| 2420 | while (1) { |
| 2421 | cond_resched(); |
| 2422 | |
| 2423 | leaf = path->nodes[0]; |
| 2424 | slot = path->slots[0]; |
| 2425 | |
| 2426 | if (slot >= btrfs_header_nritems(leaf)) { |
| 2427 | ret = btrfs_next_leaf(root, path); |
| 2428 | if (ret < 0) { |
| 2429 | goto out; |
| 2430 | } else if (ret > 0) { |
| 2431 | ret = 0; |
| 2432 | goto out; |
| 2433 | } |
| 2434 | continue; |
| 2435 | } |
| 2436 | |
| 2437 | path->slots[0]++; |
| 2438 | |
| 2439 | btrfs_item_key_to_cpu(leaf, &key, slot); |
| 2440 | |
| 2441 | if (key.objectid > inum) |
| 2442 | goto out; |
| 2443 | |
| 2444 | if (key.objectid < inum || key.type != BTRFS_EXTENT_DATA_KEY) |
| 2445 | continue; |
| 2446 | |
| 2447 | extent = btrfs_item_ptr(leaf, slot, |
| 2448 | struct btrfs_file_extent_item); |
| 2449 | |
| 2450 | if (btrfs_file_extent_disk_bytenr(leaf, extent) != old->bytenr) |
| 2451 | continue; |
| 2452 | |
Liu Bo | e68afa4 | 2013-07-01 22:13:26 +0800 | [diff] [blame] | 2453 | /* |
| 2454 | * 'offset' refers to the exact key.offset, |
| 2455 | * NOT the 'offset' field in btrfs_extent_data_ref, ie. |
| 2456 | * (key.offset - extent_offset). |
| 2457 | */ |
| 2458 | if (key.offset != offset) |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2459 | continue; |
| 2460 | |
Liu Bo | e68afa4 | 2013-07-01 22:13:26 +0800 | [diff] [blame] | 2461 | extent_offset = btrfs_file_extent_offset(leaf, extent); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2462 | num_bytes = btrfs_file_extent_num_bytes(leaf, extent); |
Liu Bo | e68afa4 | 2013-07-01 22:13:26 +0800 | [diff] [blame] | 2463 | |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2464 | if (extent_offset >= old->extent_offset + old->offset + |
| 2465 | old->len || extent_offset + num_bytes <= |
| 2466 | old->extent_offset + old->offset) |
| 2467 | continue; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2468 | break; |
| 2469 | } |
| 2470 | |
| 2471 | backref = kmalloc(sizeof(*backref), GFP_NOFS); |
| 2472 | if (!backref) { |
| 2473 | ret = -ENOENT; |
| 2474 | goto out; |
| 2475 | } |
| 2476 | |
| 2477 | backref->root_id = root_id; |
| 2478 | backref->inum = inum; |
Liu Bo | e68afa4 | 2013-07-01 22:13:26 +0800 | [diff] [blame] | 2479 | backref->file_pos = offset; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2480 | backref->num_bytes = num_bytes; |
| 2481 | backref->extent_offset = extent_offset; |
| 2482 | backref->generation = btrfs_file_extent_generation(leaf, extent); |
| 2483 | backref->old = old; |
| 2484 | backref_insert(&new->root, backref); |
| 2485 | old->count++; |
| 2486 | out: |
| 2487 | btrfs_release_path(path); |
| 2488 | WARN_ON(ret); |
| 2489 | return ret; |
| 2490 | } |
| 2491 | |
| 2492 | static noinline bool record_extent_backrefs(struct btrfs_path *path, |
| 2493 | struct new_sa_defrag_extent *new) |
| 2494 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2495 | struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2496 | struct old_sa_defrag_extent *old, *tmp; |
| 2497 | int ret; |
| 2498 | |
| 2499 | new->path = path; |
| 2500 | |
| 2501 | list_for_each_entry_safe(old, tmp, &new->head, list) { |
Liu Bo | e68afa4 | 2013-07-01 22:13:26 +0800 | [diff] [blame] | 2502 | ret = iterate_inodes_from_logical(old->bytenr + |
| 2503 | old->extent_offset, fs_info, |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2504 | path, record_one_backref, |
Zygo Blaxell | c995ab3 | 2017-09-22 13:58:45 -0400 | [diff] [blame] | 2505 | old, false); |
Josef Bacik | 4724b10 | 2013-11-05 11:11:40 -0500 | [diff] [blame] | 2506 | if (ret < 0 && ret != -ENOENT) |
| 2507 | return false; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2508 | |
| 2509 | /* no backref to be processed for this extent */ |
| 2510 | if (!old->count) { |
| 2511 | list_del(&old->list); |
| 2512 | kfree(old); |
| 2513 | } |
| 2514 | } |
| 2515 | |
| 2516 | if (list_empty(&new->head)) |
| 2517 | return false; |
| 2518 | |
| 2519 | return true; |
| 2520 | } |
| 2521 | |
| 2522 | static int relink_is_mergable(struct extent_buffer *leaf, |
| 2523 | struct btrfs_file_extent_item *fi, |
Liu Bo | 116e002 | 2013-08-02 16:30:40 +0800 | [diff] [blame] | 2524 | struct new_sa_defrag_extent *new) |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2525 | { |
Liu Bo | 116e002 | 2013-08-02 16:30:40 +0800 | [diff] [blame] | 2526 | if (btrfs_file_extent_disk_bytenr(leaf, fi) != new->bytenr) |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2527 | return 0; |
| 2528 | |
| 2529 | if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG) |
| 2530 | return 0; |
| 2531 | |
Liu Bo | 116e002 | 2013-08-02 16:30:40 +0800 | [diff] [blame] | 2532 | if (btrfs_file_extent_compression(leaf, fi) != new->compress_type) |
| 2533 | return 0; |
| 2534 | |
| 2535 | if (btrfs_file_extent_encryption(leaf, fi) || |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2536 | btrfs_file_extent_other_encoding(leaf, fi)) |
| 2537 | return 0; |
| 2538 | |
| 2539 | return 1; |
| 2540 | } |
| 2541 | |
| 2542 | /* |
| 2543 | * Note the backref might has changed, and in this case we just return 0. |
| 2544 | */ |
| 2545 | static noinline int relink_extent_backref(struct btrfs_path *path, |
| 2546 | struct sa_defrag_extent_backref *prev, |
| 2547 | struct sa_defrag_extent_backref *backref) |
| 2548 | { |
| 2549 | struct btrfs_file_extent_item *extent; |
| 2550 | struct btrfs_file_extent_item *item; |
| 2551 | struct btrfs_ordered_extent *ordered; |
| 2552 | struct btrfs_trans_handle *trans; |
Qu Wenruo | 82fa113 | 2019-04-04 14:45:35 +0800 | [diff] [blame] | 2553 | struct btrfs_ref ref = { 0 }; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2554 | struct btrfs_root *root; |
| 2555 | struct btrfs_key key; |
| 2556 | struct extent_buffer *leaf; |
| 2557 | struct old_sa_defrag_extent *old = backref->old; |
| 2558 | struct new_sa_defrag_extent *new = old->new; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2559 | struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2560 | struct inode *inode; |
| 2561 | struct extent_state *cached = NULL; |
| 2562 | int ret = 0; |
| 2563 | u64 start; |
| 2564 | u64 len; |
| 2565 | u64 lock_start; |
| 2566 | u64 lock_end; |
| 2567 | bool merge = false; |
| 2568 | int index; |
| 2569 | |
| 2570 | if (prev && prev->root_id == backref->root_id && |
| 2571 | prev->inum == backref->inum && |
| 2572 | prev->file_pos + prev->num_bytes == backref->file_pos) |
| 2573 | merge = true; |
| 2574 | |
| 2575 | /* step 1: get root */ |
| 2576 | key.objectid = backref->root_id; |
| 2577 | key.type = BTRFS_ROOT_ITEM_KEY; |
| 2578 | key.offset = (u64)-1; |
| 2579 | |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2580 | index = srcu_read_lock(&fs_info->subvol_srcu); |
| 2581 | |
| 2582 | root = btrfs_read_fs_root_no_name(fs_info, &key); |
| 2583 | if (IS_ERR(root)) { |
| 2584 | srcu_read_unlock(&fs_info->subvol_srcu, index); |
| 2585 | if (PTR_ERR(root) == -ENOENT) |
| 2586 | return 0; |
| 2587 | return PTR_ERR(root); |
| 2588 | } |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2589 | |
Wang Shilong | bcbba5e | 2014-02-08 23:46:35 +0800 | [diff] [blame] | 2590 | if (btrfs_root_readonly(root)) { |
| 2591 | srcu_read_unlock(&fs_info->subvol_srcu, index); |
| 2592 | return 0; |
| 2593 | } |
| 2594 | |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2595 | /* step 2: get inode */ |
| 2596 | key.objectid = backref->inum; |
| 2597 | key.type = BTRFS_INODE_ITEM_KEY; |
| 2598 | key.offset = 0; |
| 2599 | |
| 2600 | inode = btrfs_iget(fs_info->sb, &key, root, NULL); |
| 2601 | if (IS_ERR(inode)) { |
| 2602 | srcu_read_unlock(&fs_info->subvol_srcu, index); |
| 2603 | return 0; |
| 2604 | } |
| 2605 | |
| 2606 | srcu_read_unlock(&fs_info->subvol_srcu, index); |
| 2607 | |
| 2608 | /* step 3: relink backref */ |
| 2609 | lock_start = backref->file_pos; |
| 2610 | lock_end = backref->file_pos + backref->num_bytes - 1; |
| 2611 | lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end, |
David Sterba | ff13db4 | 2015-12-03 14:30:40 +0100 | [diff] [blame] | 2612 | &cached); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2613 | |
| 2614 | ordered = btrfs_lookup_first_ordered_extent(inode, lock_end); |
| 2615 | if (ordered) { |
| 2616 | btrfs_put_ordered_extent(ordered); |
| 2617 | goto out_unlock; |
| 2618 | } |
| 2619 | |
| 2620 | trans = btrfs_join_transaction(root); |
| 2621 | if (IS_ERR(trans)) { |
| 2622 | ret = PTR_ERR(trans); |
| 2623 | goto out_unlock; |
| 2624 | } |
| 2625 | |
| 2626 | key.objectid = backref->inum; |
| 2627 | key.type = BTRFS_EXTENT_DATA_KEY; |
| 2628 | key.offset = backref->file_pos; |
| 2629 | |
| 2630 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 2631 | if (ret < 0) { |
| 2632 | goto out_free_path; |
| 2633 | } else if (ret > 0) { |
| 2634 | ret = 0; |
| 2635 | goto out_free_path; |
| 2636 | } |
| 2637 | |
| 2638 | extent = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 2639 | struct btrfs_file_extent_item); |
| 2640 | |
| 2641 | if (btrfs_file_extent_generation(path->nodes[0], extent) != |
| 2642 | backref->generation) |
| 2643 | goto out_free_path; |
| 2644 | |
| 2645 | btrfs_release_path(path); |
| 2646 | |
| 2647 | start = backref->file_pos; |
| 2648 | if (backref->extent_offset < old->extent_offset + old->offset) |
| 2649 | start += old->extent_offset + old->offset - |
| 2650 | backref->extent_offset; |
| 2651 | |
| 2652 | len = min(backref->extent_offset + backref->num_bytes, |
| 2653 | old->extent_offset + old->offset + old->len); |
| 2654 | len -= max(backref->extent_offset, old->extent_offset + old->offset); |
| 2655 | |
| 2656 | ret = btrfs_drop_extents(trans, root, inode, start, |
| 2657 | start + len, 1); |
| 2658 | if (ret) |
| 2659 | goto out_free_path; |
| 2660 | again: |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 2661 | key.objectid = btrfs_ino(BTRFS_I(inode)); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2662 | key.type = BTRFS_EXTENT_DATA_KEY; |
| 2663 | key.offset = start; |
| 2664 | |
Liu Bo | a09a0a7 | 2013-03-11 09:20:58 +0000 | [diff] [blame] | 2665 | path->leave_spinning = 1; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2666 | if (merge) { |
| 2667 | struct btrfs_file_extent_item *fi; |
| 2668 | u64 extent_len; |
| 2669 | struct btrfs_key found_key; |
| 2670 | |
Gui Hecheng | 3c9665d | 2014-01-23 13:41:09 +0800 | [diff] [blame] | 2671 | ret = btrfs_search_slot(trans, root, &key, path, 0, 1); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2672 | if (ret < 0) |
| 2673 | goto out_free_path; |
| 2674 | |
| 2675 | path->slots[0]--; |
| 2676 | leaf = path->nodes[0]; |
| 2677 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 2678 | |
| 2679 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 2680 | struct btrfs_file_extent_item); |
| 2681 | extent_len = btrfs_file_extent_num_bytes(leaf, fi); |
| 2682 | |
Liu Bo | 116e002 | 2013-08-02 16:30:40 +0800 | [diff] [blame] | 2683 | if (extent_len + found_key.offset == start && |
| 2684 | relink_is_mergable(leaf, fi, new)) { |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2685 | btrfs_set_file_extent_num_bytes(leaf, fi, |
| 2686 | extent_len + len); |
| 2687 | btrfs_mark_buffer_dirty(leaf); |
| 2688 | inode_add_bytes(inode, len); |
| 2689 | |
| 2690 | ret = 1; |
| 2691 | goto out_free_path; |
| 2692 | } else { |
| 2693 | merge = false; |
| 2694 | btrfs_release_path(path); |
| 2695 | goto again; |
| 2696 | } |
| 2697 | } |
| 2698 | |
| 2699 | ret = btrfs_insert_empty_item(trans, root, path, &key, |
| 2700 | sizeof(*extent)); |
| 2701 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 2702 | btrfs_abort_transaction(trans, ret); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2703 | goto out_free_path; |
| 2704 | } |
| 2705 | |
| 2706 | leaf = path->nodes[0]; |
| 2707 | item = btrfs_item_ptr(leaf, path->slots[0], |
| 2708 | struct btrfs_file_extent_item); |
| 2709 | btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr); |
| 2710 | btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len); |
| 2711 | btrfs_set_file_extent_offset(leaf, item, start - new->file_pos); |
| 2712 | btrfs_set_file_extent_num_bytes(leaf, item, len); |
| 2713 | btrfs_set_file_extent_ram_bytes(leaf, item, new->len); |
| 2714 | btrfs_set_file_extent_generation(leaf, item, trans->transid); |
| 2715 | btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG); |
| 2716 | btrfs_set_file_extent_compression(leaf, item, new->compress_type); |
| 2717 | btrfs_set_file_extent_encryption(leaf, item, 0); |
| 2718 | btrfs_set_file_extent_other_encoding(leaf, item, 0); |
| 2719 | |
| 2720 | btrfs_mark_buffer_dirty(leaf); |
| 2721 | inode_add_bytes(inode, len); |
Liu Bo | a09a0a7 | 2013-03-11 09:20:58 +0000 | [diff] [blame] | 2722 | btrfs_release_path(path); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2723 | |
Qu Wenruo | 82fa113 | 2019-04-04 14:45:35 +0800 | [diff] [blame] | 2724 | btrfs_init_generic_ref(&ref, BTRFS_ADD_DELAYED_REF, new->bytenr, |
| 2725 | new->disk_len, 0); |
| 2726 | btrfs_init_data_ref(&ref, backref->root_id, backref->inum, |
| 2727 | new->file_pos); /* start - extent_offset */ |
| 2728 | ret = btrfs_inc_extent_ref(trans, &ref); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2729 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 2730 | btrfs_abort_transaction(trans, ret); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2731 | goto out_free_path; |
| 2732 | } |
| 2733 | |
| 2734 | ret = 1; |
| 2735 | out_free_path: |
| 2736 | btrfs_release_path(path); |
Liu Bo | a09a0a7 | 2013-03-11 09:20:58 +0000 | [diff] [blame] | 2737 | path->leave_spinning = 0; |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 2738 | btrfs_end_transaction(trans); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2739 | out_unlock: |
| 2740 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end, |
David Sterba | e43bbe5 | 2017-12-12 21:43:52 +0100 | [diff] [blame] | 2741 | &cached); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2742 | iput(inode); |
| 2743 | return ret; |
| 2744 | } |
| 2745 | |
Liu Bo | 6f51956 | 2013-10-29 10:45:05 +0800 | [diff] [blame] | 2746 | static void free_sa_defrag_extent(struct new_sa_defrag_extent *new) |
| 2747 | { |
| 2748 | struct old_sa_defrag_extent *old, *tmp; |
| 2749 | |
| 2750 | if (!new) |
| 2751 | return; |
| 2752 | |
| 2753 | list_for_each_entry_safe(old, tmp, &new->head, list) { |
Liu Bo | 6f51956 | 2013-10-29 10:45:05 +0800 | [diff] [blame] | 2754 | kfree(old); |
| 2755 | } |
| 2756 | kfree(new); |
| 2757 | } |
| 2758 | |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2759 | static void relink_file_extents(struct new_sa_defrag_extent *new) |
| 2760 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2761 | struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2762 | struct btrfs_path *path; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2763 | struct sa_defrag_extent_backref *backref; |
| 2764 | struct sa_defrag_extent_backref *prev = NULL; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2765 | struct rb_node *node; |
| 2766 | int ret; |
| 2767 | |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2768 | path = btrfs_alloc_path(); |
| 2769 | if (!path) |
| 2770 | return; |
| 2771 | |
| 2772 | if (!record_extent_backrefs(path, new)) { |
| 2773 | btrfs_free_path(path); |
| 2774 | goto out; |
| 2775 | } |
| 2776 | btrfs_release_path(path); |
| 2777 | |
| 2778 | while (1) { |
| 2779 | node = rb_first(&new->root); |
| 2780 | if (!node) |
| 2781 | break; |
| 2782 | rb_erase(node, &new->root); |
| 2783 | |
| 2784 | backref = rb_entry(node, struct sa_defrag_extent_backref, node); |
| 2785 | |
| 2786 | ret = relink_extent_backref(path, prev, backref); |
| 2787 | WARN_ON(ret < 0); |
| 2788 | |
| 2789 | kfree(prev); |
| 2790 | |
| 2791 | if (ret == 1) |
| 2792 | prev = backref; |
| 2793 | else |
| 2794 | prev = NULL; |
| 2795 | cond_resched(); |
| 2796 | } |
| 2797 | kfree(prev); |
| 2798 | |
| 2799 | btrfs_free_path(path); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2800 | out: |
Liu Bo | 6f51956 | 2013-10-29 10:45:05 +0800 | [diff] [blame] | 2801 | free_sa_defrag_extent(new); |
| 2802 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2803 | atomic_dec(&fs_info->defrag_running); |
| 2804 | wake_up(&fs_info->transaction_wait); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2805 | } |
| 2806 | |
| 2807 | static struct new_sa_defrag_extent * |
| 2808 | record_old_file_extents(struct inode *inode, |
| 2809 | struct btrfs_ordered_extent *ordered) |
| 2810 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2811 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2812 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 2813 | struct btrfs_path *path; |
| 2814 | struct btrfs_key key; |
Liu Bo | 6f51956 | 2013-10-29 10:45:05 +0800 | [diff] [blame] | 2815 | struct old_sa_defrag_extent *old; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2816 | struct new_sa_defrag_extent *new; |
| 2817 | int ret; |
| 2818 | |
| 2819 | new = kmalloc(sizeof(*new), GFP_NOFS); |
| 2820 | if (!new) |
| 2821 | return NULL; |
| 2822 | |
| 2823 | new->inode = inode; |
| 2824 | new->file_pos = ordered->file_offset; |
| 2825 | new->len = ordered->len; |
| 2826 | new->bytenr = ordered->start; |
| 2827 | new->disk_len = ordered->disk_len; |
| 2828 | new->compress_type = ordered->compress_type; |
| 2829 | new->root = RB_ROOT; |
| 2830 | INIT_LIST_HEAD(&new->head); |
| 2831 | |
| 2832 | path = btrfs_alloc_path(); |
| 2833 | if (!path) |
| 2834 | goto out_kfree; |
| 2835 | |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 2836 | key.objectid = btrfs_ino(BTRFS_I(inode)); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2837 | key.type = BTRFS_EXTENT_DATA_KEY; |
| 2838 | key.offset = new->file_pos; |
| 2839 | |
| 2840 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 2841 | if (ret < 0) |
| 2842 | goto out_free_path; |
| 2843 | if (ret > 0 && path->slots[0] > 0) |
| 2844 | path->slots[0]--; |
| 2845 | |
| 2846 | /* find out all the old extents for the file range */ |
| 2847 | while (1) { |
| 2848 | struct btrfs_file_extent_item *extent; |
| 2849 | struct extent_buffer *l; |
| 2850 | int slot; |
| 2851 | u64 num_bytes; |
| 2852 | u64 offset; |
| 2853 | u64 end; |
| 2854 | u64 disk_bytenr; |
| 2855 | u64 extent_offset; |
| 2856 | |
| 2857 | l = path->nodes[0]; |
| 2858 | slot = path->slots[0]; |
| 2859 | |
| 2860 | if (slot >= btrfs_header_nritems(l)) { |
| 2861 | ret = btrfs_next_leaf(root, path); |
| 2862 | if (ret < 0) |
Liu Bo | 6f51956 | 2013-10-29 10:45:05 +0800 | [diff] [blame] | 2863 | goto out_free_path; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2864 | else if (ret > 0) |
| 2865 | break; |
| 2866 | continue; |
| 2867 | } |
| 2868 | |
| 2869 | btrfs_item_key_to_cpu(l, &key, slot); |
| 2870 | |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 2871 | if (key.objectid != btrfs_ino(BTRFS_I(inode))) |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2872 | break; |
| 2873 | if (key.type != BTRFS_EXTENT_DATA_KEY) |
| 2874 | break; |
| 2875 | if (key.offset >= new->file_pos + new->len) |
| 2876 | break; |
| 2877 | |
| 2878 | extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item); |
| 2879 | |
| 2880 | num_bytes = btrfs_file_extent_num_bytes(l, extent); |
| 2881 | if (key.offset + num_bytes < new->file_pos) |
| 2882 | goto next; |
| 2883 | |
| 2884 | disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent); |
| 2885 | if (!disk_bytenr) |
| 2886 | goto next; |
| 2887 | |
| 2888 | extent_offset = btrfs_file_extent_offset(l, extent); |
| 2889 | |
| 2890 | old = kmalloc(sizeof(*old), GFP_NOFS); |
| 2891 | if (!old) |
Liu Bo | 6f51956 | 2013-10-29 10:45:05 +0800 | [diff] [blame] | 2892 | goto out_free_path; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2893 | |
| 2894 | offset = max(new->file_pos, key.offset); |
| 2895 | end = min(new->file_pos + new->len, key.offset + num_bytes); |
| 2896 | |
| 2897 | old->bytenr = disk_bytenr; |
| 2898 | old->extent_offset = extent_offset; |
| 2899 | old->offset = offset - key.offset; |
| 2900 | old->len = end - offset; |
| 2901 | old->new = new; |
| 2902 | old->count = 0; |
| 2903 | list_add_tail(&old->list, &new->head); |
| 2904 | next: |
| 2905 | path->slots[0]++; |
| 2906 | cond_resched(); |
| 2907 | } |
| 2908 | |
| 2909 | btrfs_free_path(path); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2910 | atomic_inc(&fs_info->defrag_running); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2911 | |
| 2912 | return new; |
| 2913 | |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2914 | out_free_path: |
| 2915 | btrfs_free_path(path); |
| 2916 | out_kfree: |
Liu Bo | 6f51956 | 2013-10-29 10:45:05 +0800 | [diff] [blame] | 2917 | free_sa_defrag_extent(new); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2918 | return NULL; |
| 2919 | } |
| 2920 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2921 | static void btrfs_release_delalloc_bytes(struct btrfs_fs_info *fs_info, |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 2922 | u64 start, u64 len) |
| 2923 | { |
| 2924 | struct btrfs_block_group_cache *cache; |
| 2925 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2926 | cache = btrfs_lookup_block_group(fs_info, start); |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 2927 | ASSERT(cache); |
| 2928 | |
| 2929 | spin_lock(&cache->lock); |
| 2930 | cache->delalloc_bytes -= len; |
| 2931 | spin_unlock(&cache->lock); |
| 2932 | |
| 2933 | btrfs_put_block_group(cache); |
| 2934 | } |
| 2935 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2936 | /* as ordered data IO finishes, this gets called so we can finish |
| 2937 | * an ordered extent if the range of bytes in the file it covers are |
| 2938 | * fully written. |
| 2939 | */ |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2940 | static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2941 | { |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2942 | struct inode *inode = ordered_extent->inode; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2943 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2944 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 2945 | struct btrfs_trans_handle *trans = NULL; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2946 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 2947 | struct extent_state *cached_state = NULL; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2948 | struct new_sa_defrag_extent *new = NULL; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 2949 | int compress_type = 0; |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 2950 | int ret = 0; |
| 2951 | u64 logical_len = ordered_extent->len; |
Li Zefan | 82d5902 | 2011-04-20 10:33:24 +0800 | [diff] [blame] | 2952 | bool nolock; |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 2953 | bool truncated = false; |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 2954 | bool range_locked = false; |
| 2955 | bool clear_new_delalloc_bytes = false; |
Josef Bacik | 49940bd | 2018-10-11 15:54:21 -0400 | [diff] [blame] | 2956 | bool clear_reserved_extent = true; |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 2957 | |
| 2958 | if (!test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) && |
| 2959 | !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags) && |
| 2960 | !test_bit(BTRFS_ORDERED_DIRECT, &ordered_extent->flags)) |
| 2961 | clear_new_delalloc_bytes = true; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2962 | |
Nikolay Borisov | 70ddc55 | 2017-02-20 13:50:35 +0200 | [diff] [blame] | 2963 | nolock = btrfs_is_free_space_inode(BTRFS_I(inode)); |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 2964 | |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2965 | if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) { |
| 2966 | ret = -EIO; |
| 2967 | goto out; |
| 2968 | } |
| 2969 | |
Nikolay Borisov | 7ab7956 | 2017-02-20 13:50:57 +0200 | [diff] [blame] | 2970 | btrfs_free_io_failure_record(BTRFS_I(inode), |
| 2971 | ordered_extent->file_offset, |
| 2972 | ordered_extent->file_offset + |
| 2973 | ordered_extent->len - 1); |
Miao Xie | f612496 | 2014-09-12 18:44:04 +0800 | [diff] [blame] | 2974 | |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 2975 | if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) { |
| 2976 | truncated = true; |
| 2977 | logical_len = ordered_extent->truncated_len; |
| 2978 | /* Truncated the entire extent, don't bother adding */ |
| 2979 | if (!logical_len) |
| 2980 | goto out; |
| 2981 | } |
| 2982 | |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 2983 | if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) { |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2984 | BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */ |
Qu Wenruo | 94ed938 | 2015-09-08 17:25:56 +0800 | [diff] [blame] | 2985 | |
| 2986 | /* |
| 2987 | * For mwrite(mmap + memset to write) case, we still reserve |
| 2988 | * space for NOCOW range. |
| 2989 | * As NOCOW won't cause a new delayed ref, just free the space |
| 2990 | */ |
Qu Wenruo | bc42bda | 2017-02-27 15:10:39 +0800 | [diff] [blame] | 2991 | btrfs_qgroup_free_data(inode, NULL, ordered_extent->file_offset, |
Qu Wenruo | 94ed938 | 2015-09-08 17:25:56 +0800 | [diff] [blame] | 2992 | ordered_extent->len); |
Josef Bacik | 6c760c0 | 2012-11-09 10:53:21 -0500 | [diff] [blame] | 2993 | btrfs_ordered_update_i_size(inode, 0, ordered_extent); |
| 2994 | if (nolock) |
| 2995 | trans = btrfs_join_transaction_nolock(root); |
| 2996 | else |
| 2997 | trans = btrfs_join_transaction(root); |
| 2998 | if (IS_ERR(trans)) { |
| 2999 | ret = PTR_ERR(trans); |
| 3000 | trans = NULL; |
| 3001 | goto out; |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 3002 | } |
Josef Bacik | 69fe2d7 | 2017-10-19 14:15:57 -0400 | [diff] [blame] | 3003 | trans->block_rsv = &BTRFS_I(inode)->block_rsv; |
Josef Bacik | 6c760c0 | 2012-11-09 10:53:21 -0500 | [diff] [blame] | 3004 | ret = btrfs_update_inode_fallback(trans, root, inode); |
| 3005 | if (ret) /* -ENOMEM or corruption */ |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3006 | btrfs_abort_transaction(trans, ret); |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 3007 | goto out; |
| 3008 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3009 | |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 3010 | range_locked = true; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 3011 | lock_extent_bits(io_tree, ordered_extent->file_offset, |
| 3012 | ordered_extent->file_offset + ordered_extent->len - 1, |
David Sterba | ff13db4 | 2015-12-03 14:30:40 +0100 | [diff] [blame] | 3013 | &cached_state); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3014 | |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 3015 | ret = test_range_bit(io_tree, ordered_extent->file_offset, |
| 3016 | ordered_extent->file_offset + ordered_extent->len - 1, |
Liu Bo | 452e62b | 2017-05-26 17:44:23 -0600 | [diff] [blame] | 3017 | EXTENT_DEFRAG, 0, cached_state); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 3018 | if (ret) { |
| 3019 | u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item); |
Josef Bacik | 8101c8d | 2014-01-29 16:05:30 -0500 | [diff] [blame] | 3020 | if (0 && last_snapshot >= BTRFS_I(inode)->generation) |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 3021 | /* the inode is shared */ |
| 3022 | new = record_old_file_extents(inode, ordered_extent); |
| 3023 | |
| 3024 | clear_extent_bit(io_tree, ordered_extent->file_offset, |
| 3025 | ordered_extent->file_offset + ordered_extent->len - 1, |
David Sterba | ae0f162 | 2017-10-31 16:37:52 +0100 | [diff] [blame] | 3026 | EXTENT_DEFRAG, 0, 0, &cached_state); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 3027 | } |
| 3028 | |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 3029 | if (nolock) |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 3030 | trans = btrfs_join_transaction_nolock(root); |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 3031 | else |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 3032 | trans = btrfs_join_transaction(root); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3033 | if (IS_ERR(trans)) { |
| 3034 | ret = PTR_ERR(trans); |
| 3035 | trans = NULL; |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 3036 | goto out; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3037 | } |
Chris Mason | a79b7d4 | 2014-05-22 16:18:52 -0700 | [diff] [blame] | 3038 | |
Josef Bacik | 69fe2d7 | 2017-10-19 14:15:57 -0400 | [diff] [blame] | 3039 | trans->block_rsv = &BTRFS_I(inode)->block_rsv; |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 3040 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 3041 | if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags)) |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 3042 | compress_type = ordered_extent->compress_type; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 3043 | if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) { |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 3044 | BUG_ON(compress_type); |
Justin Maggard | b430b77 | 2017-10-30 15:29:10 -0700 | [diff] [blame] | 3045 | btrfs_qgroup_free_data(inode, NULL, ordered_extent->file_offset, |
| 3046 | ordered_extent->len); |
Nikolay Borisov | 7a6d706 | 2017-02-20 13:50:48 +0200 | [diff] [blame] | 3047 | ret = btrfs_mark_extent_written(trans, BTRFS_I(inode), |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 3048 | ordered_extent->file_offset, |
| 3049 | ordered_extent->file_offset + |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 3050 | logical_len); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 3051 | } else { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3052 | BUG_ON(root == fs_info->tree_root); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 3053 | ret = insert_reserved_file_extent(trans, inode, |
| 3054 | ordered_extent->file_offset, |
| 3055 | ordered_extent->start, |
| 3056 | ordered_extent->disk_len, |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 3057 | logical_len, logical_len, |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 3058 | compress_type, 0, 0, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 3059 | BTRFS_FILE_EXTENT_REG); |
Josef Bacik | 49940bd | 2018-10-11 15:54:21 -0400 | [diff] [blame] | 3060 | if (!ret) { |
| 3061 | clear_reserved_extent = false; |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3062 | btrfs_release_delalloc_bytes(fs_info, |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 3063 | ordered_extent->start, |
| 3064 | ordered_extent->disk_len); |
Josef Bacik | 49940bd | 2018-10-11 15:54:21 -0400 | [diff] [blame] | 3065 | } |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 3066 | } |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 3067 | unpin_extent_cache(&BTRFS_I(inode)->extent_tree, |
| 3068 | ordered_extent->file_offset, ordered_extent->len, |
| 3069 | trans->transid); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3070 | if (ret < 0) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3071 | btrfs_abort_transaction(trans, ret); |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 3072 | goto out; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3073 | } |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 3074 | |
Nikolay Borisov | ac01f26 | 2018-01-08 10:59:43 +0200 | [diff] [blame] | 3075 | ret = add_pending_csums(trans, inode, &ordered_extent->list); |
| 3076 | if (ret) { |
| 3077 | btrfs_abort_transaction(trans, ret); |
| 3078 | goto out; |
| 3079 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3080 | |
Josef Bacik | 6c760c0 | 2012-11-09 10:53:21 -0500 | [diff] [blame] | 3081 | btrfs_ordered_update_i_size(inode, 0, ordered_extent); |
| 3082 | ret = btrfs_update_inode_fallback(trans, root, inode); |
| 3083 | if (ret) { /* -ENOMEM or corruption */ |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3084 | btrfs_abort_transaction(trans, ret); |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 3085 | goto out; |
Josef Bacik | 1ef30be | 2011-04-05 19:25:36 -0400 | [diff] [blame] | 3086 | } |
| 3087 | ret = 0; |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 3088 | out: |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 3089 | if (range_locked || clear_new_delalloc_bytes) { |
| 3090 | unsigned int clear_bits = 0; |
| 3091 | |
| 3092 | if (range_locked) |
| 3093 | clear_bits |= EXTENT_LOCKED; |
| 3094 | if (clear_new_delalloc_bytes) |
| 3095 | clear_bits |= EXTENT_DELALLOC_NEW; |
| 3096 | clear_extent_bit(&BTRFS_I(inode)->io_tree, |
| 3097 | ordered_extent->file_offset, |
| 3098 | ordered_extent->file_offset + |
| 3099 | ordered_extent->len - 1, |
| 3100 | clear_bits, |
| 3101 | (clear_bits & EXTENT_LOCKED) ? 1 : 0, |
David Sterba | ae0f162 | 2017-10-31 16:37:52 +0100 | [diff] [blame] | 3102 | 0, &cached_state); |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 3103 | } |
| 3104 | |
Miao Xie | a698d075 | 2012-09-20 01:51:59 -0600 | [diff] [blame] | 3105 | if (trans) |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 3106 | btrfs_end_transaction(trans); |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 3107 | |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 3108 | if (ret || truncated) { |
| 3109 | u64 start, end; |
| 3110 | |
| 3111 | if (truncated) |
| 3112 | start = ordered_extent->file_offset + logical_len; |
| 3113 | else |
| 3114 | start = ordered_extent->file_offset; |
| 3115 | end = ordered_extent->file_offset + ordered_extent->len - 1; |
David Sterba | f08dc36 | 2017-10-31 17:02:39 +0100 | [diff] [blame] | 3116 | clear_extent_uptodate(io_tree, start, end, NULL); |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 3117 | |
| 3118 | /* Drop the cache for the part of the extent we didn't write. */ |
Nikolay Borisov | dcdbc05 | 2017-02-20 13:50:45 +0200 | [diff] [blame] | 3119 | btrfs_drop_extent_cache(BTRFS_I(inode), start, end, 0); |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 3120 | |
Josef Bacik | 0bec9ef | 2013-01-31 14:58:00 -0500 | [diff] [blame] | 3121 | /* |
| 3122 | * If the ordered extent had an IOERR or something else went |
| 3123 | * wrong we need to return the space for this ordered extent |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 3124 | * back to the allocator. We only free the extent in the |
| 3125 | * truncated case if we didn't write out the extent at all. |
Josef Bacik | 49940bd | 2018-10-11 15:54:21 -0400 | [diff] [blame] | 3126 | * |
| 3127 | * If we made it past insert_reserved_file_extent before we |
| 3128 | * errored out then we don't need to do this as the accounting |
| 3129 | * has already been done. |
Josef Bacik | 0bec9ef | 2013-01-31 14:58:00 -0500 | [diff] [blame] | 3130 | */ |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 3131 | if ((ret || !logical_len) && |
Josef Bacik | 49940bd | 2018-10-11 15:54:21 -0400 | [diff] [blame] | 3132 | clear_reserved_extent && |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 3133 | !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) && |
Josef Bacik | 0bec9ef | 2013-01-31 14:58:00 -0500 | [diff] [blame] | 3134 | !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3135 | btrfs_free_reserved_extent(fs_info, |
| 3136 | ordered_extent->start, |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 3137 | ordered_extent->disk_len, 1); |
Josef Bacik | 0bec9ef | 2013-01-31 14:58:00 -0500 | [diff] [blame] | 3138 | } |
| 3139 | |
| 3140 | |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 3141 | /* |
Liu Bo | 8bad3c0 | 2012-06-18 12:14:23 +0800 | [diff] [blame] | 3142 | * This needs to be done to make sure anybody waiting knows we are done |
| 3143 | * updating everything for this ordered extent. |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 3144 | */ |
| 3145 | btrfs_remove_ordered_extent(inode, ordered_extent); |
| 3146 | |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 3147 | /* for snapshot-aware defrag */ |
Liu Bo | 6f51956 | 2013-10-29 10:45:05 +0800 | [diff] [blame] | 3148 | if (new) { |
| 3149 | if (ret) { |
| 3150 | free_sa_defrag_extent(new); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3151 | atomic_dec(&fs_info->defrag_running); |
Liu Bo | 6f51956 | 2013-10-29 10:45:05 +0800 | [diff] [blame] | 3152 | } else { |
| 3153 | relink_file_extents(new); |
| 3154 | } |
| 3155 | } |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 3156 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3157 | /* once for us */ |
| 3158 | btrfs_put_ordered_extent(ordered_extent); |
| 3159 | /* once for the tree */ |
| 3160 | btrfs_put_ordered_extent(ordered_extent); |
| 3161 | |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 3162 | return ret; |
| 3163 | } |
| 3164 | |
| 3165 | static void finish_ordered_fn(struct btrfs_work *work) |
| 3166 | { |
| 3167 | struct btrfs_ordered_extent *ordered_extent; |
| 3168 | ordered_extent = container_of(work, struct btrfs_ordered_extent, work); |
| 3169 | btrfs_finish_ordered_io(ordered_extent); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3170 | } |
| 3171 | |
Nikolay Borisov | c629732 | 2018-11-08 10:18:08 +0200 | [diff] [blame] | 3172 | void btrfs_writepage_endio_finish_ordered(struct page *page, u64 start, |
| 3173 | u64 end, int uptodate) |
Chris Mason | 211f90e | 2008-07-18 11:56:15 -0400 | [diff] [blame] | 3174 | { |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 3175 | struct inode *inode = page->mapping->host; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3176 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 3177 | struct btrfs_ordered_extent *ordered_extent = NULL; |
Liu Bo | 9e0af23 | 2014-08-15 23:36:53 +0800 | [diff] [blame] | 3178 | struct btrfs_workqueue *wq; |
| 3179 | btrfs_work_func_t func; |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 3180 | |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 3181 | trace_btrfs_writepage_end_io_hook(page, start, end, uptodate); |
| 3182 | |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 3183 | ClearPagePrivate2(page); |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 3184 | if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start, |
| 3185 | end - start + 1, uptodate)) |
David Sterba | c3988d6 | 2017-02-17 15:18:32 +0100 | [diff] [blame] | 3186 | return; |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 3187 | |
Nikolay Borisov | 70ddc55 | 2017-02-20 13:50:35 +0200 | [diff] [blame] | 3188 | if (btrfs_is_free_space_inode(BTRFS_I(inode))) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3189 | wq = fs_info->endio_freespace_worker; |
Liu Bo | 9e0af23 | 2014-08-15 23:36:53 +0800 | [diff] [blame] | 3190 | func = btrfs_freespace_write_helper; |
| 3191 | } else { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3192 | wq = fs_info->endio_write_workers; |
Liu Bo | 9e0af23 | 2014-08-15 23:36:53 +0800 | [diff] [blame] | 3193 | func = btrfs_endio_write_helper; |
| 3194 | } |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 3195 | |
Liu Bo | 9e0af23 | 2014-08-15 23:36:53 +0800 | [diff] [blame] | 3196 | btrfs_init_work(&ordered_extent->work, func, finish_ordered_fn, NULL, |
| 3197 | NULL); |
| 3198 | btrfs_queue_work(wq, &ordered_extent->work); |
Chris Mason | 211f90e | 2008-07-18 11:56:15 -0400 | [diff] [blame] | 3199 | } |
| 3200 | |
Miao Xie | dc380ae | 2014-09-12 18:43:55 +0800 | [diff] [blame] | 3201 | static int __readpage_endio_check(struct inode *inode, |
| 3202 | struct btrfs_io_bio *io_bio, |
| 3203 | int icsum, struct page *page, |
| 3204 | int pgoff, u64 start, size_t len) |
| 3205 | { |
| 3206 | char *kaddr; |
| 3207 | u32 csum_expected; |
| 3208 | u32 csum = ~(u32)0; |
Miao Xie | dc380ae | 2014-09-12 18:43:55 +0800 | [diff] [blame] | 3209 | |
| 3210 | csum_expected = *(((u32 *)io_bio->csum) + icsum); |
| 3211 | |
| 3212 | kaddr = kmap_atomic(page); |
| 3213 | csum = btrfs_csum_data(kaddr + pgoff, csum, len); |
Domagoj Tršan | 0b5e3da | 2016-10-27 08:52:33 +0100 | [diff] [blame] | 3214 | btrfs_csum_final(csum, (u8 *)&csum); |
Miao Xie | dc380ae | 2014-09-12 18:43:55 +0800 | [diff] [blame] | 3215 | if (csum != csum_expected) |
| 3216 | goto zeroit; |
| 3217 | |
| 3218 | kunmap_atomic(kaddr); |
| 3219 | return 0; |
| 3220 | zeroit: |
Nikolay Borisov | 0970a22 | 2017-02-20 13:50:53 +0200 | [diff] [blame] | 3221 | btrfs_print_data_csum_error(BTRFS_I(inode), start, csum, csum_expected, |
Qu Wenruo | 6f6b643 | 2017-02-09 10:45:06 +0800 | [diff] [blame] | 3222 | io_bio->mirror_num); |
Miao Xie | dc380ae | 2014-09-12 18:43:55 +0800 | [diff] [blame] | 3223 | memset(kaddr + pgoff, 1, len); |
| 3224 | flush_dcache_page(page); |
| 3225 | kunmap_atomic(kaddr); |
Miao Xie | dc380ae | 2014-09-12 18:43:55 +0800 | [diff] [blame] | 3226 | return -EIO; |
| 3227 | } |
| 3228 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3229 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3230 | * 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] | 3231 | * if there's a match, we allow the bio to finish. If not, the code in |
| 3232 | * extent_io.c will try to find good copies for us. |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3233 | */ |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 3234 | static int btrfs_readpage_end_io_hook(struct btrfs_io_bio *io_bio, |
| 3235 | u64 phy_offset, struct page *page, |
| 3236 | u64 start, u64 end, int mirror) |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 3237 | { |
Miao Xie | 4eee4fa | 2012-12-21 09:17:45 +0000 | [diff] [blame] | 3238 | size_t offset = start - page_offset(page); |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 3239 | struct inode *inode = page->mapping->host; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3240 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | ff79f81 | 2007-10-15 16:22:25 -0400 | [diff] [blame] | 3241 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3242 | |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 3243 | if (PageChecked(page)) { |
| 3244 | ClearPageChecked(page); |
Miao Xie | dc380ae | 2014-09-12 18:43:55 +0800 | [diff] [blame] | 3245 | return 0; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 3246 | } |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 3247 | |
| 3248 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM) |
Miao Xie | dc380ae | 2014-09-12 18:43:55 +0800 | [diff] [blame] | 3249 | return 0; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 3250 | |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 3251 | if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID && |
Chris Mason | 9655d29 | 2009-09-02 15:22:30 -0400 | [diff] [blame] | 3252 | test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) { |
David Sterba | 9116621 | 2016-04-26 23:54:39 +0200 | [diff] [blame] | 3253 | clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM); |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 3254 | return 0; |
| 3255 | } |
| 3256 | |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 3257 | phy_offset >>= inode->i_sb->s_blocksize_bits; |
Miao Xie | dc380ae | 2014-09-12 18:43:55 +0800 | [diff] [blame] | 3258 | return __readpage_endio_check(inode, io_bio, phy_offset, page, offset, |
| 3259 | start, (size_t)(end - start + 1)); |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 3260 | } |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3261 | |
Nikolay Borisov | c1c3fac | 2018-01-16 09:31:58 +0200 | [diff] [blame] | 3262 | /* |
| 3263 | * btrfs_add_delayed_iput - perform a delayed iput on @inode |
| 3264 | * |
| 3265 | * @inode: The inode we want to perform iput on |
| 3266 | * |
| 3267 | * This function uses the generic vfs_inode::i_count to track whether we should |
| 3268 | * just decrement it (in case it's > 1) or if this is the last iput then link |
| 3269 | * the inode to the delayed iput machinery. Delayed iputs are processed at |
| 3270 | * transaction commit time/superblock commit/cleaner kthread. |
| 3271 | */ |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 3272 | void btrfs_add_delayed_iput(struct inode *inode) |
| 3273 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3274 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
David Sterba | 8089fe6 | 2015-11-19 14:15:51 +0100 | [diff] [blame] | 3275 | struct btrfs_inode *binode = BTRFS_I(inode); |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 3276 | |
| 3277 | if (atomic_add_unless(&inode->i_count, -1, 1)) |
| 3278 | return; |
| 3279 | |
Josef Bacik | 034f784 | 2018-12-03 11:06:52 -0500 | [diff] [blame] | 3280 | atomic_inc(&fs_info->nr_delayed_iputs); |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 3281 | spin_lock(&fs_info->delayed_iput_lock); |
Nikolay Borisov | c1c3fac | 2018-01-16 09:31:58 +0200 | [diff] [blame] | 3282 | ASSERT(list_empty(&binode->delayed_iput)); |
| 3283 | list_add_tail(&binode->delayed_iput, &fs_info->delayed_iputs); |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 3284 | spin_unlock(&fs_info->delayed_iput_lock); |
Josef Bacik | fd340d0 | 2019-01-11 10:21:02 -0500 | [diff] [blame] | 3285 | if (!test_bit(BTRFS_FS_CLEANER_RUNNING, &fs_info->flags)) |
| 3286 | wake_up_process(fs_info->cleaner_kthread); |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 3287 | } |
| 3288 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3289 | void btrfs_run_delayed_iputs(struct btrfs_fs_info *fs_info) |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 3290 | { |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 3291 | |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 3292 | spin_lock(&fs_info->delayed_iput_lock); |
David Sterba | 8089fe6 | 2015-11-19 14:15:51 +0100 | [diff] [blame] | 3293 | while (!list_empty(&fs_info->delayed_iputs)) { |
| 3294 | struct btrfs_inode *inode; |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 3295 | |
David Sterba | 8089fe6 | 2015-11-19 14:15:51 +0100 | [diff] [blame] | 3296 | inode = list_first_entry(&fs_info->delayed_iputs, |
| 3297 | struct btrfs_inode, delayed_iput); |
Nikolay Borisov | c1c3fac | 2018-01-16 09:31:58 +0200 | [diff] [blame] | 3298 | list_del_init(&inode->delayed_iput); |
David Sterba | 8089fe6 | 2015-11-19 14:15:51 +0100 | [diff] [blame] | 3299 | spin_unlock(&fs_info->delayed_iput_lock); |
| 3300 | iput(&inode->vfs_inode); |
Josef Bacik | 034f784 | 2018-12-03 11:06:52 -0500 | [diff] [blame] | 3301 | if (atomic_dec_and_test(&fs_info->nr_delayed_iputs)) |
| 3302 | wake_up(&fs_info->delayed_iputs_wait); |
David Sterba | 8089fe6 | 2015-11-19 14:15:51 +0100 | [diff] [blame] | 3303 | spin_lock(&fs_info->delayed_iput_lock); |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 3304 | } |
David Sterba | 8089fe6 | 2015-11-19 14:15:51 +0100 | [diff] [blame] | 3305 | spin_unlock(&fs_info->delayed_iput_lock); |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 3306 | } |
| 3307 | |
Josef Bacik | 034f784 | 2018-12-03 11:06:52 -0500 | [diff] [blame] | 3308 | /** |
| 3309 | * btrfs_wait_on_delayed_iputs - wait on the delayed iputs to be done running |
| 3310 | * @fs_info - the fs_info for this fs |
| 3311 | * @return - EINTR if we were killed, 0 if nothing's pending |
| 3312 | * |
| 3313 | * This will wait on any delayed iputs that are currently running with KILLABLE |
| 3314 | * set. Once they are all done running we will return, unless we are killed in |
| 3315 | * which case we return EINTR. This helps in user operations like fallocate etc |
| 3316 | * that might get blocked on the iputs. |
| 3317 | */ |
| 3318 | int btrfs_wait_on_delayed_iputs(struct btrfs_fs_info *fs_info) |
| 3319 | { |
| 3320 | int ret = wait_event_killable(fs_info->delayed_iputs_wait, |
| 3321 | atomic_read(&fs_info->nr_delayed_iputs) == 0); |
| 3322 | if (ret) |
| 3323 | return -EINTR; |
| 3324 | return 0; |
| 3325 | } |
| 3326 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3327 | /* |
Omar Sandoval | f7e9e8f | 2018-05-11 13:13:32 -0700 | [diff] [blame] | 3328 | * This creates an orphan entry for the given inode in case something goes wrong |
| 3329 | * in the middle of an unlink. |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3330 | */ |
Nikolay Borisov | 73f2e54 | 2017-02-20 13:50:59 +0200 | [diff] [blame] | 3331 | int btrfs_orphan_add(struct btrfs_trans_handle *trans, |
Omar Sandoval | 2791906 | 2018-05-11 13:13:37 -0700 | [diff] [blame] | 3332 | struct btrfs_inode *inode) |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3333 | { |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3334 | int ret; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3335 | |
Omar Sandoval | 2791906 | 2018-05-11 13:13:37 -0700 | [diff] [blame] | 3336 | ret = btrfs_insert_orphan_item(trans, inode->root, btrfs_ino(inode)); |
| 3337 | if (ret && ret != -EEXIST) { |
| 3338 | btrfs_abort_transaction(trans, ret); |
| 3339 | return ret; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3340 | } |
| 3341 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3342 | return 0; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3343 | } |
| 3344 | |
| 3345 | /* |
Omar Sandoval | f7e9e8f | 2018-05-11 13:13:32 -0700 | [diff] [blame] | 3346 | * We have done the delete so we can go ahead and remove the orphan item for |
| 3347 | * this particular inode. |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3348 | */ |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 3349 | static int btrfs_orphan_del(struct btrfs_trans_handle *trans, |
Nikolay Borisov | 3d6ae7b | 2017-02-20 13:50:58 +0200 | [diff] [blame] | 3350 | struct btrfs_inode *inode) |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3351 | { |
Omar Sandoval | 2791906 | 2018-05-11 13:13:37 -0700 | [diff] [blame] | 3352 | return btrfs_del_orphan_item(trans, inode->root, btrfs_ino(inode)); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3353 | } |
| 3354 | |
| 3355 | /* |
| 3356 | * this cleans up any orphans that may be left on the list from the last use |
| 3357 | * of this root. |
| 3358 | */ |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3359 | int btrfs_orphan_cleanup(struct btrfs_root *root) |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3360 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3361 | struct btrfs_fs_info *fs_info = root->fs_info; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3362 | struct btrfs_path *path; |
| 3363 | struct extent_buffer *leaf; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3364 | struct btrfs_key key, found_key; |
| 3365 | struct btrfs_trans_handle *trans; |
| 3366 | struct inode *inode; |
Josef Bacik | 8f6d7f4 | 2011-09-26 15:55:20 -0400 | [diff] [blame] | 3367 | u64 last_objectid = 0; |
Omar Sandoval | f7e9e8f | 2018-05-11 13:13:32 -0700 | [diff] [blame] | 3368 | int ret = 0, nr_unlink = 0; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3369 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3370 | if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED)) |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3371 | return 0; |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 3372 | |
| 3373 | path = btrfs_alloc_path(); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3374 | if (!path) { |
| 3375 | ret = -ENOMEM; |
| 3376 | goto out; |
| 3377 | } |
David Sterba | e4058b5 | 2015-11-27 16:31:35 +0100 | [diff] [blame] | 3378 | path->reada = READA_BACK; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3379 | |
| 3380 | key.objectid = BTRFS_ORPHAN_OBJECTID; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 3381 | key.type = BTRFS_ORPHAN_ITEM_KEY; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3382 | key.offset = (u64)-1; |
| 3383 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3384 | while (1) { |
| 3385 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3386 | if (ret < 0) |
| 3387 | goto out; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3388 | |
| 3389 | /* |
| 3390 | * 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] | 3391 | * 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] | 3392 | * find the key and see if we have stuff that matches |
| 3393 | */ |
| 3394 | if (ret > 0) { |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3395 | ret = 0; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3396 | if (path->slots[0] == 0) |
| 3397 | break; |
| 3398 | path->slots[0]--; |
| 3399 | } |
| 3400 | |
| 3401 | /* pull out the item */ |
| 3402 | leaf = path->nodes[0]; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3403 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 3404 | |
| 3405 | /* make sure the item matches what we want */ |
| 3406 | if (found_key.objectid != BTRFS_ORPHAN_OBJECTID) |
| 3407 | break; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 3408 | if (found_key.type != BTRFS_ORPHAN_ITEM_KEY) |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3409 | break; |
| 3410 | |
| 3411 | /* release the path since we're done with it */ |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3412 | btrfs_release_path(path); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3413 | |
| 3414 | /* |
| 3415 | * this is where we are basically btrfs_lookup, without the |
| 3416 | * crossing root thing. we store the inode number in the |
| 3417 | * offset of the orphan item. |
| 3418 | */ |
Josef Bacik | 8f6d7f4 | 2011-09-26 15:55:20 -0400 | [diff] [blame] | 3419 | |
| 3420 | if (found_key.offset == last_objectid) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3421 | btrfs_err(fs_info, |
| 3422 | "Error removing orphan entry, stopping orphan cleanup"); |
Josef Bacik | 8f6d7f4 | 2011-09-26 15:55:20 -0400 | [diff] [blame] | 3423 | ret = -EINVAL; |
| 3424 | goto out; |
| 3425 | } |
| 3426 | |
| 3427 | last_objectid = found_key.offset; |
| 3428 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3429 | found_key.objectid = found_key.offset; |
| 3430 | found_key.type = BTRFS_INODE_ITEM_KEY; |
| 3431 | found_key.offset = 0; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3432 | inode = btrfs_iget(fs_info->sb, &found_key, root, NULL); |
Rusty Russell | 8c6ffba | 2013-07-15 11:20:32 +0930 | [diff] [blame] | 3433 | ret = PTR_ERR_OR_ZERO(inode); |
Filipe Manana | 6771089 | 2016-06-06 11:51:25 +0100 | [diff] [blame] | 3434 | if (ret && ret != -ENOENT) |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3435 | goto out; |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 3436 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3437 | if (ret == -ENOENT && root == fs_info->tree_root) { |
Arne Jansen | f8e9e0b | 2011-12-14 20:12:02 -0500 | [diff] [blame] | 3438 | struct btrfs_root *dead_root; |
| 3439 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 3440 | int is_dead_root = 0; |
| 3441 | |
| 3442 | /* |
| 3443 | * this is an orphan in the tree root. Currently these |
| 3444 | * could come from 2 sources: |
| 3445 | * a) a snapshot deletion in progress |
| 3446 | * b) a free space cache inode |
| 3447 | * We need to distinguish those two, as the snapshot |
| 3448 | * orphan must not get deleted. |
| 3449 | * find_dead_roots already ran before us, so if this |
| 3450 | * is a snapshot deletion, we should find the root |
| 3451 | * in the dead_roots list |
| 3452 | */ |
| 3453 | spin_lock(&fs_info->trans_lock); |
| 3454 | list_for_each_entry(dead_root, &fs_info->dead_roots, |
| 3455 | root_list) { |
| 3456 | if (dead_root->root_key.objectid == |
| 3457 | found_key.objectid) { |
| 3458 | is_dead_root = 1; |
| 3459 | break; |
| 3460 | } |
| 3461 | } |
| 3462 | spin_unlock(&fs_info->trans_lock); |
| 3463 | if (is_dead_root) { |
| 3464 | /* prevent this orphan from being found again */ |
| 3465 | key.offset = found_key.objectid - 1; |
| 3466 | continue; |
| 3467 | } |
Omar Sandoval | f7e9e8f | 2018-05-11 13:13:32 -0700 | [diff] [blame] | 3468 | |
Arne Jansen | f8e9e0b | 2011-12-14 20:12:02 -0500 | [diff] [blame] | 3469 | } |
Omar Sandoval | f7e9e8f | 2018-05-11 13:13:32 -0700 | [diff] [blame] | 3470 | |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 3471 | /* |
Omar Sandoval | f7e9e8f | 2018-05-11 13:13:32 -0700 | [diff] [blame] | 3472 | * If we have an inode with links, there are a couple of |
| 3473 | * possibilities. Old kernels (before v3.12) used to create an |
| 3474 | * orphan item for truncate indicating that there were possibly |
| 3475 | * extent items past i_size that needed to be deleted. In v3.12, |
| 3476 | * truncate was changed to update i_size in sync with the extent |
| 3477 | * items, but the (useless) orphan item was still created. Since |
| 3478 | * v4.18, we don't create the orphan item for truncate at all. |
| 3479 | * |
| 3480 | * So, this item could mean that we need to do a truncate, but |
| 3481 | * only if this filesystem was last used on a pre-v3.12 kernel |
| 3482 | * and was not cleanly unmounted. The odds of that are quite |
| 3483 | * slim, and it's a pain to do the truncate now, so just delete |
| 3484 | * the orphan item. |
| 3485 | * |
| 3486 | * It's also possible that this orphan item was supposed to be |
| 3487 | * deleted but wasn't. The inode number may have been reused, |
| 3488 | * but either way, we can delete the orphan item. |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 3489 | */ |
Omar Sandoval | f7e9e8f | 2018-05-11 13:13:32 -0700 | [diff] [blame] | 3490 | if (ret == -ENOENT || inode->i_nlink) { |
| 3491 | if (!ret) |
| 3492 | iput(inode); |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 3493 | trans = btrfs_start_transaction(root, 1); |
| 3494 | if (IS_ERR(trans)) { |
| 3495 | ret = PTR_ERR(trans); |
| 3496 | goto out; |
| 3497 | } |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3498 | btrfs_debug(fs_info, "auto deleting %Lu", |
| 3499 | found_key.objectid); |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 3500 | ret = btrfs_del_orphan_item(trans, root, |
| 3501 | found_key.objectid); |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 3502 | btrfs_end_transaction(trans); |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 3503 | if (ret) |
| 3504 | goto out; |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 3505 | continue; |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3506 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3507 | |
Omar Sandoval | f7e9e8f | 2018-05-11 13:13:32 -0700 | [diff] [blame] | 3508 | nr_unlink++; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3509 | |
| 3510 | /* this will do delete_inode and everything for us */ |
| 3511 | iput(inode); |
| 3512 | } |
Miao Xie | 3254c87 | 2011-11-10 20:45:05 -0500 | [diff] [blame] | 3513 | /* release the path since we're done with it */ |
| 3514 | btrfs_release_path(path); |
| 3515 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3516 | root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE; |
| 3517 | |
Omar Sandoval | a575cee | 2018-05-11 13:13:38 -0700 | [diff] [blame] | 3518 | if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) { |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 3519 | trans = btrfs_join_transaction(root); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3520 | if (!IS_ERR(trans)) |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 3521 | btrfs_end_transaction(trans); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3522 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3523 | |
| 3524 | if (nr_unlink) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3525 | btrfs_debug(fs_info, "unlinked %d orphans", nr_unlink); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3526 | |
| 3527 | out: |
| 3528 | if (ret) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3529 | btrfs_err(fs_info, "could not do orphan cleanup %d", ret); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3530 | btrfs_free_path(path); |
| 3531 | return ret; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3532 | } |
| 3533 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3534 | /* |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3535 | * very simple check to peek ahead in the leaf looking for xattrs. If we |
| 3536 | * don't find any xattrs, we know there can't be any acls. |
| 3537 | * |
| 3538 | * slot is the slot the inode is in, objectid is the objectid of the inode |
| 3539 | */ |
| 3540 | 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] | 3541 | int slot, u64 objectid, |
| 3542 | int *first_xattr_slot) |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3543 | { |
| 3544 | u32 nritems = btrfs_header_nritems(leaf); |
| 3545 | struct btrfs_key found_key; |
Josef Bacik | f23b5a5 | 2013-06-19 10:16:26 -0400 | [diff] [blame] | 3546 | static u64 xattr_access = 0; |
| 3547 | static u64 xattr_default = 0; |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3548 | int scanned = 0; |
| 3549 | |
Josef Bacik | f23b5a5 | 2013-06-19 10:16:26 -0400 | [diff] [blame] | 3550 | if (!xattr_access) { |
Andreas Gruenbacher | 97d7929 | 2015-12-02 14:44:35 +0100 | [diff] [blame] | 3551 | xattr_access = btrfs_name_hash(XATTR_NAME_POSIX_ACL_ACCESS, |
| 3552 | strlen(XATTR_NAME_POSIX_ACL_ACCESS)); |
| 3553 | xattr_default = btrfs_name_hash(XATTR_NAME_POSIX_ACL_DEFAULT, |
| 3554 | strlen(XATTR_NAME_POSIX_ACL_DEFAULT)); |
Josef Bacik | f23b5a5 | 2013-06-19 10:16:26 -0400 | [diff] [blame] | 3555 | } |
| 3556 | |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3557 | slot++; |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 3558 | *first_xattr_slot = -1; |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3559 | while (slot < nritems) { |
| 3560 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
| 3561 | |
| 3562 | /* we found a different objectid, there must not be acls */ |
| 3563 | if (found_key.objectid != objectid) |
| 3564 | return 0; |
| 3565 | |
| 3566 | /* we found an xattr, assume we've got an acl */ |
Josef Bacik | f23b5a5 | 2013-06-19 10:16:26 -0400 | [diff] [blame] | 3567 | if (found_key.type == BTRFS_XATTR_ITEM_KEY) { |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 3568 | if (*first_xattr_slot == -1) |
| 3569 | *first_xattr_slot = slot; |
Josef Bacik | f23b5a5 | 2013-06-19 10:16:26 -0400 | [diff] [blame] | 3570 | if (found_key.offset == xattr_access || |
| 3571 | found_key.offset == xattr_default) |
| 3572 | return 1; |
| 3573 | } |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3574 | |
| 3575 | /* |
| 3576 | * we found a key greater than an xattr key, there can't |
| 3577 | * be any acls later on |
| 3578 | */ |
| 3579 | if (found_key.type > BTRFS_XATTR_ITEM_KEY) |
| 3580 | return 0; |
| 3581 | |
| 3582 | slot++; |
| 3583 | scanned++; |
| 3584 | |
| 3585 | /* |
| 3586 | * it goes inode, inode backrefs, xattrs, extents, |
| 3587 | * so if there are a ton of hard links to an inode there can |
| 3588 | * be a lot of backrefs. Don't waste time searching too hard, |
| 3589 | * this is just an optimization |
| 3590 | */ |
| 3591 | if (scanned >= 8) |
| 3592 | break; |
| 3593 | } |
| 3594 | /* we hit the end of the leaf before we found an xattr or |
| 3595 | * something larger than an xattr. We have to assume the inode |
| 3596 | * has acls |
| 3597 | */ |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 3598 | if (*first_xattr_slot == -1) |
| 3599 | *first_xattr_slot = slot; |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3600 | return 1; |
| 3601 | } |
| 3602 | |
| 3603 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3604 | * read an inode from the btree into the in-memory inode |
| 3605 | */ |
Filipe Manana | 4222ea7 | 2018-10-24 10:13:03 +0100 | [diff] [blame] | 3606 | static int btrfs_read_locked_inode(struct inode *inode, |
| 3607 | struct btrfs_path *in_path) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3608 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3609 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Filipe Manana | 4222ea7 | 2018-10-24 10:13:03 +0100 | [diff] [blame] | 3610 | struct btrfs_path *path = in_path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3611 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3612 | struct btrfs_inode_item *inode_item; |
| 3613 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 3614 | struct btrfs_key location; |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 3615 | unsigned long ptr; |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3616 | int maybe_acls; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 3617 | u32 rdev; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3618 | int ret; |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 3619 | bool filled = false; |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 3620 | int first_xattr_slot; |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 3621 | |
| 3622 | ret = btrfs_fill_inode(inode, &rdev); |
| 3623 | if (!ret) |
| 3624 | filled = true; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3625 | |
Filipe Manana | 4222ea7 | 2018-10-24 10:13:03 +0100 | [diff] [blame] | 3626 | if (!path) { |
| 3627 | path = btrfs_alloc_path(); |
| 3628 | if (!path) |
| 3629 | return -ENOMEM; |
| 3630 | } |
Mark Fasheh | 1748f84 | 2011-07-12 11:25:31 -0700 | [diff] [blame] | 3631 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3632 | memcpy(&location, &BTRFS_I(inode)->location, sizeof(location)); |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 3633 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3634 | ret = btrfs_lookup_inode(NULL, root, path, &location, 0); |
Filipe Manana | 6771089 | 2016-06-06 11:51:25 +0100 | [diff] [blame] | 3635 | if (ret) { |
Filipe Manana | 4222ea7 | 2018-10-24 10:13:03 +0100 | [diff] [blame] | 3636 | if (path != in_path) |
| 3637 | btrfs_free_path(path); |
Al Viro | f5b3a41 | 2018-07-29 23:04:51 +0100 | [diff] [blame] | 3638 | return ret; |
Filipe Manana | 6771089 | 2016-06-06 11:51:25 +0100 | [diff] [blame] | 3639 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3640 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3641 | leaf = path->nodes[0]; |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 3642 | |
| 3643 | if (filled) |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 3644 | goto cache_index; |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 3645 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3646 | inode_item = btrfs_item_ptr(leaf, path->slots[0], |
| 3647 | struct btrfs_inode_item); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3648 | inode->i_mode = btrfs_inode_mode(leaf, inode_item); |
Miklos Szeredi | bfe8684 | 2011-10-28 14:13:29 +0200 | [diff] [blame] | 3649 | set_nlink(inode, btrfs_inode_nlink(leaf, inode_item)); |
Eric W. Biederman | 2f2f43d | 2012-02-10 11:05:07 -0800 | [diff] [blame] | 3650 | i_uid_write(inode, btrfs_inode_uid(leaf, inode_item)); |
| 3651 | i_gid_write(inode, btrfs_inode_gid(leaf, inode_item)); |
Nikolay Borisov | 6ef06d2 | 2017-02-20 13:50:34 +0200 | [diff] [blame] | 3652 | btrfs_i_size_write(BTRFS_I(inode), btrfs_inode_size(leaf, inode_item)); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3653 | |
David Sterba | a937b97 | 2014-12-12 17:39:12 +0100 | [diff] [blame] | 3654 | inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->atime); |
| 3655 | inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->atime); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3656 | |
David Sterba | a937b97 | 2014-12-12 17:39:12 +0100 | [diff] [blame] | 3657 | inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->mtime); |
| 3658 | inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->mtime); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3659 | |
David Sterba | a937b97 | 2014-12-12 17:39:12 +0100 | [diff] [blame] | 3660 | inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->ctime); |
| 3661 | inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->ctime); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3662 | |
chandan r | 9cc97d6 | 2012-07-04 12:48:07 +0530 | [diff] [blame] | 3663 | BTRFS_I(inode)->i_otime.tv_sec = |
| 3664 | btrfs_timespec_sec(leaf, &inode_item->otime); |
| 3665 | BTRFS_I(inode)->i_otime.tv_nsec = |
| 3666 | btrfs_timespec_nsec(leaf, &inode_item->otime); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3667 | |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 3668 | inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item)); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3669 | BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item); |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 3670 | BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item); |
| 3671 | |
Jeff Layton | c7f88c4 | 2017-12-11 06:35:12 -0500 | [diff] [blame] | 3672 | inode_set_iversion_queried(inode, |
| 3673 | btrfs_inode_sequence(leaf, inode_item)); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3674 | inode->i_generation = BTRFS_I(inode)->generation; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 3675 | inode->i_rdev = 0; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3676 | rdev = btrfs_inode_rdev(leaf, inode_item); |
| 3677 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 3678 | BTRFS_I(inode)->index_cnt = (u64)-1; |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 3679 | BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item); |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 3680 | |
| 3681 | cache_index: |
Yang Dongsheng | 6e17d30 | 2015-04-09 12:08:43 +0800 | [diff] [blame] | 3682 | /* |
| 3683 | * If we were modified in the current generation and evicted from memory |
| 3684 | * and then re-read we need to do a full sync since we don't have any |
| 3685 | * idea about which extents were modified before we were evicted from |
| 3686 | * cache. |
| 3687 | * |
| 3688 | * This is required for both inode re-read from disk and delayed inode |
| 3689 | * in delayed_nodes_tree. |
| 3690 | */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3691 | if (BTRFS_I(inode)->last_trans == fs_info->generation) |
Yang Dongsheng | 6e17d30 | 2015-04-09 12:08:43 +0800 | [diff] [blame] | 3692 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, |
| 3693 | &BTRFS_I(inode)->runtime_flags); |
| 3694 | |
Filipe Manana | bde6c24 | 2015-07-24 00:00:19 +0100 | [diff] [blame] | 3695 | /* |
| 3696 | * We don't persist the id of the transaction where an unlink operation |
| 3697 | * against the inode was last made. So here we assume the inode might |
| 3698 | * have been evicted, and therefore the exact value of last_unlink_trans |
| 3699 | * lost, and set it to last_trans to avoid metadata inconsistencies |
| 3700 | * between the inode and its parent if the inode is fsync'ed and the log |
| 3701 | * replayed. For example, in the scenario: |
| 3702 | * |
| 3703 | * touch mydir/foo |
| 3704 | * ln mydir/foo mydir/bar |
| 3705 | * sync |
| 3706 | * unlink mydir/bar |
| 3707 | * echo 2 > /proc/sys/vm/drop_caches # evicts inode |
| 3708 | * xfs_io -c fsync mydir/foo |
| 3709 | * <power failure> |
| 3710 | * mount fs, triggers fsync log replay |
| 3711 | * |
| 3712 | * We must make sure that when we fsync our inode foo we also log its |
| 3713 | * parent inode, otherwise after log replay the parent still has the |
| 3714 | * dentry with the "bar" name but our inode foo has a link count of 1 |
| 3715 | * and doesn't have an inode ref with the name "bar" anymore. |
| 3716 | * |
| 3717 | * Setting last_unlink_trans to last_trans is a pessimistic approach, |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 3718 | * but it guarantees correctness at the expense of occasional full |
Filipe Manana | bde6c24 | 2015-07-24 00:00:19 +0100 | [diff] [blame] | 3719 | * transaction commits on fsync if our inode is a directory, or if our |
| 3720 | * inode is not a directory, logging its parent unnecessarily. |
| 3721 | */ |
| 3722 | BTRFS_I(inode)->last_unlink_trans = BTRFS_I(inode)->last_trans; |
| 3723 | |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 3724 | path->slots[0]++; |
| 3725 | if (inode->i_nlink != 1 || |
| 3726 | path->slots[0] >= btrfs_header_nritems(leaf)) |
| 3727 | goto cache_acl; |
| 3728 | |
| 3729 | btrfs_item_key_to_cpu(leaf, &location, path->slots[0]); |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 3730 | if (location.objectid != btrfs_ino(BTRFS_I(inode))) |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 3731 | goto cache_acl; |
| 3732 | |
| 3733 | ptr = btrfs_item_ptr_offset(leaf, path->slots[0]); |
| 3734 | if (location.type == BTRFS_INODE_REF_KEY) { |
| 3735 | struct btrfs_inode_ref *ref; |
| 3736 | |
| 3737 | ref = (struct btrfs_inode_ref *)ptr; |
| 3738 | BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref); |
| 3739 | } else if (location.type == BTRFS_INODE_EXTREF_KEY) { |
| 3740 | struct btrfs_inode_extref *extref; |
| 3741 | |
| 3742 | extref = (struct btrfs_inode_extref *)ptr; |
| 3743 | BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf, |
| 3744 | extref); |
| 3745 | } |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 3746 | cache_acl: |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3747 | /* |
| 3748 | * try to precache a NULL acl entry for files that don't have |
| 3749 | * any xattrs or acls |
| 3750 | */ |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3751 | maybe_acls = acls_after_inode_item(leaf, path->slots[0], |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 3752 | btrfs_ino(BTRFS_I(inode)), &first_xattr_slot); |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 3753 | if (first_xattr_slot != -1) { |
| 3754 | path->slots[0] = first_xattr_slot; |
| 3755 | ret = btrfs_load_inode_props(inode, path); |
| 3756 | if (ret) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3757 | btrfs_err(fs_info, |
David Sterba | 351fd35 | 2014-05-15 16:48:20 +0200 | [diff] [blame] | 3758 | "error loading props for ino %llu (root %llu): %d", |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 3759 | btrfs_ino(BTRFS_I(inode)), |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 3760 | root->root_key.objectid, ret); |
| 3761 | } |
Filipe Manana | 4222ea7 | 2018-10-24 10:13:03 +0100 | [diff] [blame] | 3762 | if (path != in_path) |
| 3763 | btrfs_free_path(path); |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 3764 | |
Al Viro | 72c0490 | 2009-06-24 16:58:48 -0400 | [diff] [blame] | 3765 | if (!maybe_acls) |
| 3766 | cache_no_acl(inode); |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3767 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3768 | switch (inode->i_mode & S_IFMT) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3769 | case S_IFREG: |
| 3770 | inode->i_mapping->a_ops = &btrfs_aops; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3771 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3772 | inode->i_fop = &btrfs_file_operations; |
| 3773 | inode->i_op = &btrfs_file_inode_operations; |
| 3774 | break; |
| 3775 | case S_IFDIR: |
| 3776 | inode->i_fop = &btrfs_dir_file_operations; |
Omar Sandoval | 67ade05 | 2017-01-25 17:06:38 -0800 | [diff] [blame] | 3777 | inode->i_op = &btrfs_dir_inode_operations; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3778 | break; |
| 3779 | case S_IFLNK: |
| 3780 | inode->i_op = &btrfs_symlink_inode_operations; |
Al Viro | 21fc61c | 2015-11-17 01:07:57 -0500 | [diff] [blame] | 3781 | inode_nohighmem(inode); |
Omar Sandoval | 4779cc0 | 2018-09-24 15:16:55 -0700 | [diff] [blame] | 3782 | inode->i_mapping->a_ops = &btrfs_aops; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3783 | break; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 3784 | default: |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 3785 | inode->i_op = &btrfs_special_inode_operations; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 3786 | init_special_inode(inode, inode->i_mode, rdev); |
| 3787 | break; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3788 | } |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 3789 | |
David Sterba | 7b6a221 | 2018-03-26 18:40:21 +0200 | [diff] [blame] | 3790 | btrfs_sync_inode_flags_to_i_flags(inode); |
Filipe Manana | 6771089 | 2016-06-06 11:51:25 +0100 | [diff] [blame] | 3791 | return 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3792 | } |
| 3793 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3794 | /* |
| 3795 | * given a leaf and an inode, copy the inode fields into the leaf |
| 3796 | */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3797 | static void fill_inode_item(struct btrfs_trans_handle *trans, |
| 3798 | struct extent_buffer *leaf, |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3799 | struct btrfs_inode_item *item, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3800 | struct inode *inode) |
| 3801 | { |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3802 | struct btrfs_map_token token; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3803 | |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3804 | btrfs_init_map_token(&token); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3805 | |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3806 | btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token); |
| 3807 | btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token); |
| 3808 | btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size, |
| 3809 | &token); |
| 3810 | btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token); |
| 3811 | btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3812 | |
David Sterba | a937b97 | 2014-12-12 17:39:12 +0100 | [diff] [blame] | 3813 | btrfs_set_token_timespec_sec(leaf, &item->atime, |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3814 | inode->i_atime.tv_sec, &token); |
David Sterba | a937b97 | 2014-12-12 17:39:12 +0100 | [diff] [blame] | 3815 | btrfs_set_token_timespec_nsec(leaf, &item->atime, |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3816 | inode->i_atime.tv_nsec, &token); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3817 | |
David Sterba | a937b97 | 2014-12-12 17:39:12 +0100 | [diff] [blame] | 3818 | btrfs_set_token_timespec_sec(leaf, &item->mtime, |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3819 | inode->i_mtime.tv_sec, &token); |
David Sterba | a937b97 | 2014-12-12 17:39:12 +0100 | [diff] [blame] | 3820 | btrfs_set_token_timespec_nsec(leaf, &item->mtime, |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3821 | inode->i_mtime.tv_nsec, &token); |
| 3822 | |
David Sterba | a937b97 | 2014-12-12 17:39:12 +0100 | [diff] [blame] | 3823 | btrfs_set_token_timespec_sec(leaf, &item->ctime, |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3824 | inode->i_ctime.tv_sec, &token); |
David Sterba | a937b97 | 2014-12-12 17:39:12 +0100 | [diff] [blame] | 3825 | btrfs_set_token_timespec_nsec(leaf, &item->ctime, |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3826 | inode->i_ctime.tv_nsec, &token); |
| 3827 | |
chandan r | 9cc97d6 | 2012-07-04 12:48:07 +0530 | [diff] [blame] | 3828 | btrfs_set_token_timespec_sec(leaf, &item->otime, |
| 3829 | BTRFS_I(inode)->i_otime.tv_sec, &token); |
| 3830 | btrfs_set_token_timespec_nsec(leaf, &item->otime, |
| 3831 | BTRFS_I(inode)->i_otime.tv_nsec, &token); |
| 3832 | |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3833 | btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode), |
| 3834 | &token); |
| 3835 | btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation, |
| 3836 | &token); |
Jeff Layton | c7f88c4 | 2017-12-11 06:35:12 -0500 | [diff] [blame] | 3837 | btrfs_set_token_inode_sequence(leaf, item, inode_peek_iversion(inode), |
| 3838 | &token); |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3839 | btrfs_set_token_inode_transid(leaf, item, trans->transid, &token); |
| 3840 | btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token); |
| 3841 | btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token); |
| 3842 | btrfs_set_token_inode_block_group(leaf, item, 0, &token); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3843 | } |
| 3844 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3845 | /* |
| 3846 | * copy everything in the in-memory inode into the btree. |
| 3847 | */ |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 3848 | static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3849 | struct btrfs_root *root, struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3850 | { |
| 3851 | struct btrfs_inode_item *inode_item; |
| 3852 | struct btrfs_path *path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3853 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3854 | int ret; |
| 3855 | |
| 3856 | path = btrfs_alloc_path(); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 3857 | if (!path) |
| 3858 | return -ENOMEM; |
| 3859 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 3860 | path->leave_spinning = 1; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 3861 | ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location, |
| 3862 | 1); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3863 | if (ret) { |
| 3864 | if (ret > 0) |
| 3865 | ret = -ENOENT; |
| 3866 | goto failed; |
| 3867 | } |
| 3868 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3869 | leaf = path->nodes[0]; |
| 3870 | inode_item = btrfs_item_ptr(leaf, path->slots[0], |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 3871 | struct btrfs_inode_item); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3872 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3873 | fill_inode_item(trans, leaf, inode_item, inode); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3874 | btrfs_mark_buffer_dirty(leaf); |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 3875 | btrfs_set_inode_last_trans(trans, inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3876 | ret = 0; |
| 3877 | failed: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3878 | btrfs_free_path(path); |
| 3879 | return ret; |
| 3880 | } |
| 3881 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3882 | /* |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 3883 | * copy everything in the in-memory inode into the btree. |
| 3884 | */ |
| 3885 | noinline int btrfs_update_inode(struct btrfs_trans_handle *trans, |
| 3886 | struct btrfs_root *root, struct inode *inode) |
| 3887 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3888 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 3889 | int ret; |
| 3890 | |
| 3891 | /* |
| 3892 | * If the inode is a free space inode, we can deadlock during commit |
| 3893 | * if we put it into the delayed code. |
| 3894 | * |
| 3895 | * The data relocation inode should also be directly updated |
| 3896 | * without delay |
| 3897 | */ |
Nikolay Borisov | 70ddc55 | 2017-02-20 13:50:35 +0200 | [diff] [blame] | 3898 | if (!btrfs_is_free_space_inode(BTRFS_I(inode)) |
Josef Bacik | 1d52c78 | 2014-09-18 11:30:44 -0400 | [diff] [blame] | 3899 | && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3900 | && !test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) { |
Alexander Block | 8ea05e3 | 2012-07-25 17:35:53 +0200 | [diff] [blame] | 3901 | btrfs_update_root_times(trans, root); |
| 3902 | |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 3903 | ret = btrfs_delayed_update_inode(trans, root, inode); |
| 3904 | if (!ret) |
| 3905 | btrfs_set_inode_last_trans(trans, inode); |
| 3906 | return ret; |
| 3907 | } |
| 3908 | |
| 3909 | return btrfs_update_inode_item(trans, root, inode); |
| 3910 | } |
| 3911 | |
Josef Bacik | be6aef6 | 2012-10-22 15:43:12 -0400 | [diff] [blame] | 3912 | noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans, |
| 3913 | struct btrfs_root *root, |
| 3914 | struct inode *inode) |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 3915 | { |
| 3916 | int ret; |
| 3917 | |
| 3918 | ret = btrfs_update_inode(trans, root, inode); |
| 3919 | if (ret == -ENOSPC) |
| 3920 | return btrfs_update_inode_item(trans, root, inode); |
| 3921 | return ret; |
| 3922 | } |
| 3923 | |
| 3924 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3925 | * unlink helper that gets used here in inode.c and in the tree logging |
| 3926 | * recovery code. It remove a link in a directory with a given name, and |
| 3927 | * also drops the back refs in the inode to the directory |
| 3928 | */ |
Al Viro | 9298679 | 2011-03-04 17:14:37 +0000 | [diff] [blame] | 3929 | static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans, |
| 3930 | struct btrfs_root *root, |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 3931 | struct btrfs_inode *dir, |
| 3932 | struct btrfs_inode *inode, |
Al Viro | 9298679 | 2011-03-04 17:14:37 +0000 | [diff] [blame] | 3933 | const char *name, int name_len) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3934 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3935 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3936 | struct btrfs_path *path; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3937 | int ret = 0; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3938 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3939 | struct btrfs_dir_item *di; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3940 | struct btrfs_key key; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 3941 | u64 index; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3942 | u64 ino = btrfs_ino(inode); |
| 3943 | u64 dir_ino = btrfs_ino(dir); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3944 | |
| 3945 | path = btrfs_alloc_path(); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3946 | if (!path) { |
| 3947 | ret = -ENOMEM; |
Tsutomu Itoh | 554233a | 2011-02-03 03:16:25 +0000 | [diff] [blame] | 3948 | goto out; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3949 | } |
| 3950 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 3951 | path->leave_spinning = 1; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3952 | di = btrfs_lookup_dir_item(trans, root, path, dir_ino, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3953 | name, name_len, -1); |
Liu Bo | 3cf5068 | 2018-09-12 06:06:26 +0800 | [diff] [blame] | 3954 | if (IS_ERR_OR_NULL(di)) { |
| 3955 | ret = di ? PTR_ERR(di) : -ENOENT; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3956 | goto err; |
| 3957 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3958 | leaf = path->nodes[0]; |
| 3959 | btrfs_dir_item_key_to_cpu(leaf, di, &key); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3960 | ret = btrfs_delete_one_dir_name(trans, root, path, di); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3961 | if (ret) |
| 3962 | goto err; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3963 | btrfs_release_path(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3964 | |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 3965 | /* |
| 3966 | * If we don't have dir index, we have to get it by looking up |
| 3967 | * the inode ref, since we get the inode ref, remove it directly, |
| 3968 | * it is unnecessary to do delayed deletion. |
| 3969 | * |
| 3970 | * But if we have dir index, needn't search inode ref to get it. |
| 3971 | * Since the inode ref is close to the inode item, it is better |
| 3972 | * that we delay to delete it, and just do this deletion when |
| 3973 | * we update the inode item. |
| 3974 | */ |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 3975 | if (inode->dir_index) { |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 3976 | ret = btrfs_delayed_delete_inode_ref(inode); |
| 3977 | if (!ret) { |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 3978 | index = inode->dir_index; |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 3979 | goto skip_backref; |
| 3980 | } |
| 3981 | } |
| 3982 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3983 | ret = btrfs_del_inode_ref(trans, root, name, name_len, ino, |
| 3984 | dir_ino, &index); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 3985 | if (ret) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3986 | btrfs_info(fs_info, |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 3987 | "failed to delete reference to %.*s, inode %llu parent %llu", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 3988 | name_len, name, ino, dir_ino); |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3989 | btrfs_abort_transaction(trans, ret); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 3990 | goto err; |
| 3991 | } |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 3992 | skip_backref: |
Lu Fengqi | 9add294 | 2018-08-01 11:32:26 +0800 | [diff] [blame] | 3993 | ret = btrfs_delete_delayed_dir_index(trans, dir, index); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3994 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3995 | btrfs_abort_transaction(trans, ret); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3996 | goto err; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3997 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3998 | |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 3999 | ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len, inode, |
| 4000 | dir_ino); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4001 | if (ret != 0 && ret != -ENOENT) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 4002 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4003 | goto err; |
| 4004 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4005 | |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 4006 | ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len, dir, |
| 4007 | index); |
Chris Mason | 6418c96 | 2010-10-30 07:34:24 -0400 | [diff] [blame] | 4008 | if (ret == -ENOENT) |
| 4009 | ret = 0; |
Zach Brown | d4e3991 | 2013-04-02 21:02:16 +0000 | [diff] [blame] | 4010 | else if (ret) |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 4011 | btrfs_abort_transaction(trans, ret); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4012 | err: |
| 4013 | btrfs_free_path(path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4014 | if (ret) |
| 4015 | goto out; |
| 4016 | |
Nikolay Borisov | 6ef06d2 | 2017-02-20 13:50:34 +0200 | [diff] [blame] | 4017 | 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] | 4018 | inode_inc_iversion(&inode->vfs_inode); |
| 4019 | inode_inc_iversion(&dir->vfs_inode); |
| 4020 | inode->vfs_inode.i_ctime = dir->vfs_inode.i_mtime = |
| 4021 | dir->vfs_inode.i_ctime = current_time(&inode->vfs_inode); |
| 4022 | ret = btrfs_update_inode(trans, root, &dir->vfs_inode); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4023 | out: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4024 | return ret; |
| 4025 | } |
| 4026 | |
Al Viro | 9298679 | 2011-03-04 17:14:37 +0000 | [diff] [blame] | 4027 | int btrfs_unlink_inode(struct btrfs_trans_handle *trans, |
| 4028 | struct btrfs_root *root, |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 4029 | struct btrfs_inode *dir, struct btrfs_inode *inode, |
Al Viro | 9298679 | 2011-03-04 17:14:37 +0000 | [diff] [blame] | 4030 | const char *name, int name_len) |
| 4031 | { |
| 4032 | int ret; |
| 4033 | ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len); |
| 4034 | if (!ret) { |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 4035 | drop_nlink(&inode->vfs_inode); |
| 4036 | ret = btrfs_update_inode(trans, root, &inode->vfs_inode); |
Al Viro | 9298679 | 2011-03-04 17:14:37 +0000 | [diff] [blame] | 4037 | } |
| 4038 | return ret; |
| 4039 | } |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4040 | |
| 4041 | /* |
| 4042 | * helper to start transaction for unlink and rmdir. |
| 4043 | * |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 4044 | * unlink and rmdir are special in btrfs, they do not always free space, so |
| 4045 | * if we cannot make our reservations the normal way try and see if there is |
| 4046 | * plenty of slack room in the global reserve to migrate, otherwise we cannot |
| 4047 | * allow the unlink to occur. |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4048 | */ |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 4049 | static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir) |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4050 | { |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4051 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4052 | |
Josef Bacik | e70bea5 | 2011-10-11 14:18:24 -0400 | [diff] [blame] | 4053 | /* |
| 4054 | * 1 for the possible orphan item |
| 4055 | * 1 for the dir item |
| 4056 | * 1 for the dir index |
| 4057 | * 1 for the inode ref |
Josef Bacik | e70bea5 | 2011-10-11 14:18:24 -0400 | [diff] [blame] | 4058 | * 1 for the inode |
| 4059 | */ |
Filipe Manana | 8eab77f | 2015-11-13 23:57:16 +0000 | [diff] [blame] | 4060 | return btrfs_start_transaction_fallback_global_rsv(root, 5, 5); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4061 | } |
| 4062 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4063 | static int btrfs_unlink(struct inode *dir, struct dentry *dentry) |
| 4064 | { |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4065 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4066 | struct btrfs_trans_handle *trans; |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 4067 | struct inode *inode = d_inode(dentry); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4068 | int ret; |
| 4069 | |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 4070 | trans = __unlink_start_trans(dir); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4071 | if (IS_ERR(trans)) |
Josef Bacik | 5df6a9f | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4072 | return PTR_ERR(trans); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4073 | |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 4074 | btrfs_record_unlink_dir(trans, BTRFS_I(dir), BTRFS_I(d_inode(dentry)), |
| 4075 | 0); |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 4076 | |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 4077 | ret = btrfs_unlink_inode(trans, root, BTRFS_I(dir), |
| 4078 | BTRFS_I(d_inode(dentry)), dentry->d_name.name, |
| 4079 | dentry->d_name.len); |
Tsutomu Itoh | b532402 | 2011-07-19 07:27:20 +0000 | [diff] [blame] | 4080 | if (ret) |
| 4081 | goto out; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 4082 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4083 | if (inode->i_nlink == 0) { |
Nikolay Borisov | 73f2e54 | 2017-02-20 13:50:59 +0200 | [diff] [blame] | 4084 | ret = btrfs_orphan_add(trans, BTRFS_I(inode)); |
Tsutomu Itoh | b532402 | 2011-07-19 07:27:20 +0000 | [diff] [blame] | 4085 | if (ret) |
| 4086 | goto out; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4087 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 4088 | |
Tsutomu Itoh | b532402 | 2011-07-19 07:27:20 +0000 | [diff] [blame] | 4089 | out: |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 4090 | btrfs_end_transaction(trans); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4091 | btrfs_btree_balance_dirty(root->fs_info); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4092 | return ret; |
| 4093 | } |
| 4094 | |
Misono Tomohiro | f60a236 | 2018-04-18 11:34:52 +0900 | [diff] [blame] | 4095 | static int btrfs_unlink_subvol(struct btrfs_trans_handle *trans, |
Lu Fengqi | 401b3b1 | 2018-08-01 11:32:30 +0800 | [diff] [blame] | 4096 | struct inode *dir, u64 objectid, |
| 4097 | const char *name, int name_len) |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4098 | { |
Lu Fengqi | 401b3b1 | 2018-08-01 11:32:30 +0800 | [diff] [blame] | 4099 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4100 | struct btrfs_path *path; |
| 4101 | struct extent_buffer *leaf; |
| 4102 | struct btrfs_dir_item *di; |
| 4103 | struct btrfs_key key; |
| 4104 | u64 index; |
| 4105 | int ret; |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 4106 | u64 dir_ino = btrfs_ino(BTRFS_I(dir)); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4107 | |
| 4108 | path = btrfs_alloc_path(); |
| 4109 | if (!path) |
| 4110 | return -ENOMEM; |
| 4111 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4112 | di = btrfs_lookup_dir_item(trans, root, path, dir_ino, |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4113 | name, name_len, -1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4114 | if (IS_ERR_OR_NULL(di)) { |
Liu Bo | 3cf5068 | 2018-09-12 06:06:26 +0800 | [diff] [blame] | 4115 | ret = di ? PTR_ERR(di) : -ENOENT; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4116 | goto out; |
| 4117 | } |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4118 | |
| 4119 | leaf = path->nodes[0]; |
| 4120 | btrfs_dir_item_key_to_cpu(leaf, di, &key); |
| 4121 | WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid); |
| 4122 | ret = btrfs_delete_one_dir_name(trans, root, path, di); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4123 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 4124 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4125 | goto out; |
| 4126 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4127 | btrfs_release_path(path); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4128 | |
Lu Fengqi | 3ee1c55 | 2018-08-01 11:32:28 +0800 | [diff] [blame] | 4129 | ret = btrfs_del_root_ref(trans, objectid, root->root_key.objectid, |
| 4130 | dir_ino, &index, name, name_len); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4131 | if (ret < 0) { |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4132 | if (ret != -ENOENT) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 4133 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4134 | goto out; |
| 4135 | } |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4136 | di = btrfs_search_dir_index_item(root, path, dir_ino, |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4137 | name, name_len); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4138 | if (IS_ERR_OR_NULL(di)) { |
| 4139 | if (!di) |
| 4140 | ret = -ENOENT; |
| 4141 | else |
| 4142 | ret = PTR_ERR(di); |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 4143 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4144 | goto out; |
| 4145 | } |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4146 | |
| 4147 | leaf = path->nodes[0]; |
| 4148 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4149 | index = key.offset; |
| 4150 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4151 | btrfs_release_path(path); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4152 | |
Lu Fengqi | 9add294 | 2018-08-01 11:32:26 +0800 | [diff] [blame] | 4153 | ret = btrfs_delete_delayed_dir_index(trans, BTRFS_I(dir), index); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4154 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 4155 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4156 | goto out; |
| 4157 | } |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 4158 | |
Nikolay Borisov | 6ef06d2 | 2017-02-20 13:50:34 +0200 | [diff] [blame] | 4159 | 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] | 4160 | inode_inc_iversion(dir); |
Deepa Dinamani | c2050a4 | 2016-09-14 07:48:06 -0700 | [diff] [blame] | 4161 | dir->i_mtime = dir->i_ctime = current_time(dir); |
Josef Bacik | 5a24e84 | 2012-08-08 10:12:59 -0600 | [diff] [blame] | 4162 | ret = btrfs_update_inode_fallback(trans, root, dir); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4163 | if (ret) |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 4164 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4165 | out: |
Josef Bacik | 71d7aed | 2011-06-14 14:24:32 -0400 | [diff] [blame] | 4166 | btrfs_free_path(path); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4167 | return ret; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4168 | } |
| 4169 | |
Misono Tomohiro | ec42f16 | 2018-04-18 11:34:13 +0900 | [diff] [blame] | 4170 | /* |
| 4171 | * Helper to check if the subvolume references other subvolumes or if it's |
| 4172 | * default. |
| 4173 | */ |
Misono Tomohiro | f60a236 | 2018-04-18 11:34:52 +0900 | [diff] [blame] | 4174 | static noinline int may_destroy_subvol(struct btrfs_root *root) |
Misono Tomohiro | ec42f16 | 2018-04-18 11:34:13 +0900 | [diff] [blame] | 4175 | { |
| 4176 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 4177 | struct btrfs_path *path; |
| 4178 | struct btrfs_dir_item *di; |
| 4179 | struct btrfs_key key; |
| 4180 | u64 dir_id; |
| 4181 | int ret; |
| 4182 | |
| 4183 | path = btrfs_alloc_path(); |
| 4184 | if (!path) |
| 4185 | return -ENOMEM; |
| 4186 | |
| 4187 | /* Make sure this root isn't set as the default subvol */ |
| 4188 | dir_id = btrfs_super_root_dir(fs_info->super_copy); |
| 4189 | di = btrfs_lookup_dir_item(NULL, fs_info->tree_root, path, |
| 4190 | dir_id, "default", 7, 0); |
| 4191 | if (di && !IS_ERR(di)) { |
| 4192 | btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key); |
| 4193 | if (key.objectid == root->root_key.objectid) { |
| 4194 | ret = -EPERM; |
| 4195 | btrfs_err(fs_info, |
| 4196 | "deleting default subvolume %llu is not allowed", |
| 4197 | key.objectid); |
| 4198 | goto out; |
| 4199 | } |
| 4200 | btrfs_release_path(path); |
| 4201 | } |
| 4202 | |
| 4203 | key.objectid = root->root_key.objectid; |
| 4204 | key.type = BTRFS_ROOT_REF_KEY; |
| 4205 | key.offset = (u64)-1; |
| 4206 | |
| 4207 | ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0); |
| 4208 | if (ret < 0) |
| 4209 | goto out; |
| 4210 | BUG_ON(ret == 0); |
| 4211 | |
| 4212 | ret = 0; |
| 4213 | if (path->slots[0] > 0) { |
| 4214 | path->slots[0]--; |
| 4215 | btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]); |
| 4216 | if (key.objectid == root->root_key.objectid && |
| 4217 | key.type == BTRFS_ROOT_REF_KEY) |
| 4218 | ret = -ENOTEMPTY; |
| 4219 | } |
| 4220 | out: |
| 4221 | btrfs_free_path(path); |
| 4222 | return ret; |
| 4223 | } |
| 4224 | |
Nikolay Borisov | 20a6800 | 2018-04-27 14:36:24 +0300 | [diff] [blame] | 4225 | /* Delete all dentries for inodes belonging to the root */ |
| 4226 | static void btrfs_prune_dentries(struct btrfs_root *root) |
| 4227 | { |
| 4228 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 4229 | struct rb_node *node; |
| 4230 | struct rb_node *prev; |
| 4231 | struct btrfs_inode *entry; |
| 4232 | struct inode *inode; |
| 4233 | u64 objectid = 0; |
| 4234 | |
| 4235 | if (!test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) |
| 4236 | WARN_ON(btrfs_root_refs(&root->root_item) != 0); |
| 4237 | |
| 4238 | spin_lock(&root->inode_lock); |
| 4239 | again: |
| 4240 | node = root->inode_tree.rb_node; |
| 4241 | prev = NULL; |
| 4242 | while (node) { |
| 4243 | prev = node; |
| 4244 | entry = rb_entry(node, struct btrfs_inode, rb_node); |
| 4245 | |
David Sterba | 3750851 | 2018-06-29 10:56:40 +0200 | [diff] [blame] | 4246 | if (objectid < btrfs_ino(entry)) |
Nikolay Borisov | 20a6800 | 2018-04-27 14:36:24 +0300 | [diff] [blame] | 4247 | node = node->rb_left; |
David Sterba | 3750851 | 2018-06-29 10:56:40 +0200 | [diff] [blame] | 4248 | else if (objectid > btrfs_ino(entry)) |
Nikolay Borisov | 20a6800 | 2018-04-27 14:36:24 +0300 | [diff] [blame] | 4249 | node = node->rb_right; |
| 4250 | else |
| 4251 | break; |
| 4252 | } |
| 4253 | if (!node) { |
| 4254 | while (prev) { |
| 4255 | entry = rb_entry(prev, struct btrfs_inode, rb_node); |
David Sterba | 3750851 | 2018-06-29 10:56:40 +0200 | [diff] [blame] | 4256 | if (objectid <= btrfs_ino(entry)) { |
Nikolay Borisov | 20a6800 | 2018-04-27 14:36:24 +0300 | [diff] [blame] | 4257 | node = prev; |
| 4258 | break; |
| 4259 | } |
| 4260 | prev = rb_next(prev); |
| 4261 | } |
| 4262 | } |
| 4263 | while (node) { |
| 4264 | entry = rb_entry(node, struct btrfs_inode, rb_node); |
David Sterba | 3750851 | 2018-06-29 10:56:40 +0200 | [diff] [blame] | 4265 | objectid = btrfs_ino(entry) + 1; |
Nikolay Borisov | 20a6800 | 2018-04-27 14:36:24 +0300 | [diff] [blame] | 4266 | inode = igrab(&entry->vfs_inode); |
| 4267 | if (inode) { |
| 4268 | spin_unlock(&root->inode_lock); |
| 4269 | if (atomic_read(&inode->i_count) > 1) |
| 4270 | d_prune_aliases(inode); |
| 4271 | /* |
| 4272 | * btrfs_drop_inode will have it removed from the inode |
| 4273 | * cache when its usage count hits zero. |
| 4274 | */ |
| 4275 | iput(inode); |
| 4276 | cond_resched(); |
| 4277 | spin_lock(&root->inode_lock); |
| 4278 | goto again; |
| 4279 | } |
| 4280 | |
| 4281 | if (cond_resched_lock(&root->inode_lock)) |
| 4282 | goto again; |
| 4283 | |
| 4284 | node = rb_next(node); |
| 4285 | } |
| 4286 | spin_unlock(&root->inode_lock); |
| 4287 | } |
| 4288 | |
Misono Tomohiro | f60a236 | 2018-04-18 11:34:52 +0900 | [diff] [blame] | 4289 | int btrfs_delete_subvolume(struct inode *dir, struct dentry *dentry) |
| 4290 | { |
| 4291 | struct btrfs_fs_info *fs_info = btrfs_sb(dentry->d_sb); |
| 4292 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 4293 | struct inode *inode = d_inode(dentry); |
| 4294 | struct btrfs_root *dest = BTRFS_I(inode)->root; |
| 4295 | struct btrfs_trans_handle *trans; |
| 4296 | struct btrfs_block_rsv block_rsv; |
| 4297 | u64 root_flags; |
Misono Tomohiro | f60a236 | 2018-04-18 11:34:52 +0900 | [diff] [blame] | 4298 | int ret; |
| 4299 | int err; |
| 4300 | |
| 4301 | /* |
| 4302 | * Don't allow to delete a subvolume with send in progress. This is |
| 4303 | * inside the inode lock so the error handling that has to drop the bit |
| 4304 | * again is not run concurrently. |
| 4305 | */ |
| 4306 | spin_lock(&dest->root_item_lock); |
Lu Fengqi | a7176f7 | 2018-08-04 21:10:53 +0800 | [diff] [blame] | 4307 | if (dest->send_in_progress) { |
Misono Tomohiro | f60a236 | 2018-04-18 11:34:52 +0900 | [diff] [blame] | 4308 | spin_unlock(&dest->root_item_lock); |
| 4309 | btrfs_warn(fs_info, |
| 4310 | "attempt to delete subvolume %llu during send", |
| 4311 | dest->root_key.objectid); |
| 4312 | return -EPERM; |
| 4313 | } |
Lu Fengqi | a7176f7 | 2018-08-04 21:10:53 +0800 | [diff] [blame] | 4314 | root_flags = btrfs_root_flags(&dest->root_item); |
| 4315 | btrfs_set_root_flags(&dest->root_item, |
| 4316 | root_flags | BTRFS_ROOT_SUBVOL_DEAD); |
| 4317 | spin_unlock(&dest->root_item_lock); |
Misono Tomohiro | f60a236 | 2018-04-18 11:34:52 +0900 | [diff] [blame] | 4318 | |
| 4319 | down_write(&fs_info->subvol_sem); |
| 4320 | |
| 4321 | err = may_destroy_subvol(dest); |
| 4322 | if (err) |
| 4323 | goto out_up_write; |
| 4324 | |
| 4325 | btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP); |
| 4326 | /* |
| 4327 | * One for dir inode, |
| 4328 | * two for dir entries, |
| 4329 | * two for root ref/backref. |
| 4330 | */ |
Gu JinXiang | c4c129d | 2018-05-30 11:00:38 +0800 | [diff] [blame] | 4331 | err = btrfs_subvolume_reserve_metadata(root, &block_rsv, 5, true); |
Misono Tomohiro | f60a236 | 2018-04-18 11:34:52 +0900 | [diff] [blame] | 4332 | if (err) |
| 4333 | goto out_up_write; |
| 4334 | |
| 4335 | trans = btrfs_start_transaction(root, 0); |
| 4336 | if (IS_ERR(trans)) { |
| 4337 | err = PTR_ERR(trans); |
| 4338 | goto out_release; |
| 4339 | } |
| 4340 | trans->block_rsv = &block_rsv; |
| 4341 | trans->bytes_reserved = block_rsv.size; |
| 4342 | |
| 4343 | btrfs_record_snapshot_destroy(trans, BTRFS_I(dir)); |
| 4344 | |
Lu Fengqi | 401b3b1 | 2018-08-01 11:32:30 +0800 | [diff] [blame] | 4345 | ret = btrfs_unlink_subvol(trans, dir, dest->root_key.objectid, |
| 4346 | dentry->d_name.name, dentry->d_name.len); |
Misono Tomohiro | f60a236 | 2018-04-18 11:34:52 +0900 | [diff] [blame] | 4347 | if (ret) { |
| 4348 | err = ret; |
| 4349 | btrfs_abort_transaction(trans, ret); |
| 4350 | goto out_end_trans; |
| 4351 | } |
| 4352 | |
| 4353 | btrfs_record_root_in_trans(trans, dest); |
| 4354 | |
| 4355 | memset(&dest->root_item.drop_progress, 0, |
| 4356 | sizeof(dest->root_item.drop_progress)); |
| 4357 | dest->root_item.drop_level = 0; |
| 4358 | btrfs_set_root_refs(&dest->root_item, 0); |
| 4359 | |
| 4360 | if (!test_and_set_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &dest->state)) { |
| 4361 | ret = btrfs_insert_orphan_item(trans, |
| 4362 | fs_info->tree_root, |
| 4363 | dest->root_key.objectid); |
| 4364 | if (ret) { |
| 4365 | btrfs_abort_transaction(trans, ret); |
| 4366 | err = ret; |
| 4367 | goto out_end_trans; |
| 4368 | } |
| 4369 | } |
| 4370 | |
Lu Fengqi | d195779 | 2018-05-29 15:01:54 +0800 | [diff] [blame] | 4371 | ret = btrfs_uuid_tree_remove(trans, dest->root_item.uuid, |
Misono Tomohiro | f60a236 | 2018-04-18 11:34:52 +0900 | [diff] [blame] | 4372 | BTRFS_UUID_KEY_SUBVOL, |
| 4373 | dest->root_key.objectid); |
| 4374 | if (ret && ret != -ENOENT) { |
| 4375 | btrfs_abort_transaction(trans, ret); |
| 4376 | err = ret; |
| 4377 | goto out_end_trans; |
| 4378 | } |
| 4379 | if (!btrfs_is_empty_uuid(dest->root_item.received_uuid)) { |
Lu Fengqi | d195779 | 2018-05-29 15:01:54 +0800 | [diff] [blame] | 4380 | ret = btrfs_uuid_tree_remove(trans, |
Misono Tomohiro | f60a236 | 2018-04-18 11:34:52 +0900 | [diff] [blame] | 4381 | dest->root_item.received_uuid, |
| 4382 | BTRFS_UUID_KEY_RECEIVED_SUBVOL, |
| 4383 | dest->root_key.objectid); |
| 4384 | if (ret && ret != -ENOENT) { |
| 4385 | btrfs_abort_transaction(trans, ret); |
| 4386 | err = ret; |
| 4387 | goto out_end_trans; |
| 4388 | } |
| 4389 | } |
| 4390 | |
| 4391 | out_end_trans: |
| 4392 | trans->block_rsv = NULL; |
| 4393 | trans->bytes_reserved = 0; |
| 4394 | ret = btrfs_end_transaction(trans); |
| 4395 | if (ret && !err) |
| 4396 | err = ret; |
| 4397 | inode->i_flags |= S_DEAD; |
| 4398 | out_release: |
| 4399 | btrfs_subvolume_release_metadata(fs_info, &block_rsv); |
| 4400 | out_up_write: |
| 4401 | up_write(&fs_info->subvol_sem); |
| 4402 | if (err) { |
| 4403 | spin_lock(&dest->root_item_lock); |
| 4404 | root_flags = btrfs_root_flags(&dest->root_item); |
| 4405 | btrfs_set_root_flags(&dest->root_item, |
| 4406 | root_flags & ~BTRFS_ROOT_SUBVOL_DEAD); |
| 4407 | spin_unlock(&dest->root_item_lock); |
| 4408 | } else { |
| 4409 | d_invalidate(dentry); |
Nikolay Borisov | 20a6800 | 2018-04-27 14:36:24 +0300 | [diff] [blame] | 4410 | btrfs_prune_dentries(dest); |
Misono Tomohiro | f60a236 | 2018-04-18 11:34:52 +0900 | [diff] [blame] | 4411 | ASSERT(dest->send_in_progress == 0); |
| 4412 | |
| 4413 | /* the last ref */ |
| 4414 | if (dest->ino_cache_inode) { |
| 4415 | iput(dest->ino_cache_inode); |
| 4416 | dest->ino_cache_inode = NULL; |
| 4417 | } |
| 4418 | } |
| 4419 | |
| 4420 | return err; |
| 4421 | } |
| 4422 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4423 | static int btrfs_rmdir(struct inode *dir, struct dentry *dentry) |
| 4424 | { |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 4425 | struct inode *inode = d_inode(dentry); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 4426 | int err = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4427 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4428 | struct btrfs_trans_handle *trans; |
Filipe Manana | 44f714d | 2016-06-06 16:11:13 +0100 | [diff] [blame] | 4429 | u64 last_unlink_trans; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4430 | |
David Sterba | b3ae244 | 2012-09-13 16:04:34 -0600 | [diff] [blame] | 4431 | if (inode->i_size > BTRFS_EMPTY_DIR_SIZE) |
Yan | 134d451 | 2007-10-25 15:49:25 -0400 | [diff] [blame] | 4432 | return -ENOTEMPTY; |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 4433 | if (btrfs_ino(BTRFS_I(inode)) == BTRFS_FIRST_FREE_OBJECTID) |
Misono Tomohiro | a79a464 | 2018-04-18 11:35:31 +0900 | [diff] [blame] | 4434 | return btrfs_delete_subvolume(dir, dentry); |
Yan | 134d451 | 2007-10-25 15:49:25 -0400 | [diff] [blame] | 4435 | |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 4436 | trans = __unlink_start_trans(dir); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4437 | if (IS_ERR(trans)) |
Josef Bacik | 5df6a9f | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4438 | return PTR_ERR(trans); |
Josef Bacik | 5df6a9f | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4439 | |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 4440 | if (unlikely(btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) { |
Lu Fengqi | 401b3b1 | 2018-08-01 11:32:30 +0800 | [diff] [blame] | 4441 | err = btrfs_unlink_subvol(trans, dir, |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4442 | BTRFS_I(inode)->location.objectid, |
| 4443 | dentry->d_name.name, |
| 4444 | dentry->d_name.len); |
| 4445 | goto out; |
| 4446 | } |
| 4447 | |
Nikolay Borisov | 73f2e54 | 2017-02-20 13:50:59 +0200 | [diff] [blame] | 4448 | err = btrfs_orphan_add(trans, BTRFS_I(inode)); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 4449 | if (err) |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4450 | goto out; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 4451 | |
Filipe Manana | 44f714d | 2016-06-06 16:11:13 +0100 | [diff] [blame] | 4452 | last_unlink_trans = BTRFS_I(inode)->last_unlink_trans; |
| 4453 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4454 | /* now the directory is empty */ |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 4455 | err = btrfs_unlink_inode(trans, root, BTRFS_I(dir), |
| 4456 | BTRFS_I(d_inode(dentry)), dentry->d_name.name, |
| 4457 | dentry->d_name.len); |
Filipe Manana | 44f714d | 2016-06-06 16:11:13 +0100 | [diff] [blame] | 4458 | if (!err) { |
Nikolay Borisov | 6ef06d2 | 2017-02-20 13:50:34 +0200 | [diff] [blame] | 4459 | btrfs_i_size_write(BTRFS_I(inode), 0); |
Filipe Manana | 44f714d | 2016-06-06 16:11:13 +0100 | [diff] [blame] | 4460 | /* |
| 4461 | * Propagate the last_unlink_trans value of the deleted dir to |
| 4462 | * its parent directory. This is to prevent an unrecoverable |
| 4463 | * log tree in the case we do something like this: |
| 4464 | * 1) create dir foo |
| 4465 | * 2) create snapshot under dir foo |
| 4466 | * 3) delete the snapshot |
| 4467 | * 4) rmdir foo |
| 4468 | * 5) mkdir foo |
| 4469 | * 6) fsync foo or some file inside foo |
| 4470 | */ |
| 4471 | if (last_unlink_trans >= trans->transid) |
| 4472 | BTRFS_I(dir)->last_unlink_trans = last_unlink_trans; |
| 4473 | } |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4474 | out: |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 4475 | btrfs_end_transaction(trans); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4476 | btrfs_btree_balance_dirty(root->fs_info); |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 4477 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4478 | return err; |
| 4479 | } |
| 4480 | |
Josef Bacik | ddfae63 | 2017-10-19 14:16:02 -0400 | [diff] [blame] | 4481 | /* |
| 4482 | * Return this if we need to call truncate_block for the last bit of the |
| 4483 | * truncate. |
| 4484 | */ |
| 4485 | #define NEED_TRUNCATE_BLOCK 1 |
Filipe Manana | 0305cd5 | 2015-10-16 12:34:25 +0100 | [diff] [blame] | 4486 | |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 4487 | /* |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4488 | * this can truncate away extent items, csum items and directory items. |
| 4489 | * 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] | 4490 | * any higher than new_size |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4491 | * |
| 4492 | * csum items that cross the new i_size are truncated to the new size |
| 4493 | * as well. |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 4494 | * |
| 4495 | * min_type is the minimum key type to truncate down to. If set to 0, this |
| 4496 | * 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] | 4497 | */ |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4498 | int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans, |
| 4499 | struct btrfs_root *root, |
| 4500 | struct inode *inode, |
| 4501 | u64 new_size, u32 min_type) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4502 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4503 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4504 | struct btrfs_path *path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4505 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4506 | struct btrfs_file_extent_item *fi; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4507 | struct btrfs_key key; |
| 4508 | struct btrfs_key found_key; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4509 | u64 extent_start = 0; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4510 | u64 extent_num_bytes = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4511 | u64 extent_offset = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4512 | u64 item_end = 0; |
Filipe Manana | c1aa457 | 2015-06-20 18:20:09 +0100 | [diff] [blame] | 4513 | u64 last_size = new_size; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4514 | u32 found_type = (u8)-1; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4515 | int found_extent; |
| 4516 | int del_item; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4517 | int pending_del_nr = 0; |
| 4518 | int pending_del_slot = 0; |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4519 | int extent_type = -1; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4520 | int ret; |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 4521 | u64 ino = btrfs_ino(BTRFS_I(inode)); |
Chris Mason | 28ed134 | 2014-12-17 09:41:04 -0800 | [diff] [blame] | 4522 | u64 bytes_deleted = 0; |
Thomas Meyer | 897ca81 | 2017-10-07 16:02:21 +0200 | [diff] [blame] | 4523 | bool be_nice = false; |
| 4524 | bool should_throttle = false; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4525 | |
| 4526 | BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4527 | |
Chris Mason | 28ed134 | 2014-12-17 09:41:04 -0800 | [diff] [blame] | 4528 | /* |
| 4529 | * for non-free space inodes and ref cows, we want to back off from |
| 4530 | * time to time |
| 4531 | */ |
Nikolay Borisov | 70ddc55 | 2017-02-20 13:50:35 +0200 | [diff] [blame] | 4532 | if (!btrfs_is_free_space_inode(BTRFS_I(inode)) && |
Chris Mason | 28ed134 | 2014-12-17 09:41:04 -0800 | [diff] [blame] | 4533 | test_bit(BTRFS_ROOT_REF_COWS, &root->state)) |
Thomas Meyer | 897ca81 | 2017-10-07 16:02:21 +0200 | [diff] [blame] | 4534 | be_nice = true; |
Chris Mason | 28ed134 | 2014-12-17 09:41:04 -0800 | [diff] [blame] | 4535 | |
Mark Fasheh | 0eb0e19 | 2011-07-12 16:44:10 -0700 | [diff] [blame] | 4536 | path = btrfs_alloc_path(); |
| 4537 | if (!path) |
| 4538 | return -ENOMEM; |
David Sterba | e4058b5 | 2015-11-27 16:31:35 +0100 | [diff] [blame] | 4539 | path->reada = READA_BACK; |
Mark Fasheh | 0eb0e19 | 2011-07-12 16:44:10 -0700 | [diff] [blame] | 4540 | |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4541 | /* |
| 4542 | * We want to drop from the next block forward in case this new size is |
| 4543 | * not block aligned since we will be keeping the last block of the |
| 4544 | * extent just the way it is. |
| 4545 | */ |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 4546 | if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) || |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4547 | root == fs_info->tree_root) |
Nikolay Borisov | dcdbc05 | 2017-02-20 13:50:45 +0200 | [diff] [blame] | 4548 | btrfs_drop_extent_cache(BTRFS_I(inode), ALIGN(new_size, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4549 | fs_info->sectorsize), |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 4550 | (u64)-1, 0); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4551 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 4552 | /* |
| 4553 | * This function is also used to drop the items in the log tree before |
| 4554 | * 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] | 4555 | * 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] | 4556 | * items. |
| 4557 | */ |
| 4558 | if (min_type == 0 && root == BTRFS_I(inode)->root) |
Nikolay Borisov | 4ccb5c7 | 2017-01-10 20:35:38 +0200 | [diff] [blame] | 4559 | btrfs_kill_delayed_inode_items(BTRFS_I(inode)); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 4560 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4561 | key.objectid = ino; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4562 | key.offset = (u64)-1; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4563 | key.type = (u8)-1; |
| 4564 | |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4565 | search_again: |
Chris Mason | 28ed134 | 2014-12-17 09:41:04 -0800 | [diff] [blame] | 4566 | /* |
| 4567 | * with a 16K leaf size and 128MB extents, you can actually queue |
| 4568 | * up a huge file in a single leaf. Most of the time that |
| 4569 | * bytes_deleted is > 0, it will be huge by the time we get here |
| 4570 | */ |
Omar Sandoval | fd86a3a | 2018-05-11 13:13:30 -0700 | [diff] [blame] | 4571 | if (be_nice && bytes_deleted > SZ_32M && |
| 4572 | btrfs_should_end_transaction(trans)) { |
| 4573 | ret = -EAGAIN; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4574 | goto out; |
| 4575 | } |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4576 | |
Omar Sandoval | fd86a3a | 2018-05-11 13:13:30 -0700 | [diff] [blame] | 4577 | path->leave_spinning = 1; |
| 4578 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 4579 | if (ret < 0) |
| 4580 | goto out; |
| 4581 | |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4582 | if (ret > 0) { |
Omar Sandoval | fd86a3a | 2018-05-11 13:13:30 -0700 | [diff] [blame] | 4583 | ret = 0; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4584 | /* there are no items in the tree for us to truncate, we're |
| 4585 | * done |
| 4586 | */ |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4587 | if (path->slots[0] == 0) |
| 4588 | goto out; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4589 | path->slots[0]--; |
| 4590 | } |
| 4591 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4592 | while (1) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4593 | fi = NULL; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4594 | leaf = path->nodes[0]; |
| 4595 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 4596 | found_type = found_key.type; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4597 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4598 | if (found_key.objectid != ino) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4599 | break; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4600 | |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4601 | if (found_type < min_type) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4602 | break; |
| 4603 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4604 | item_end = found_key.offset; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4605 | if (found_type == BTRFS_EXTENT_DATA_KEY) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4606 | fi = btrfs_item_ptr(leaf, path->slots[0], |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4607 | struct btrfs_file_extent_item); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4608 | extent_type = btrfs_file_extent_type(leaf, fi); |
| 4609 | if (extent_type != BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4610 | item_end += |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4611 | btrfs_file_extent_num_bytes(leaf, fi); |
Liu Bo | 09ed2f1 | 2017-03-10 11:09:48 -0800 | [diff] [blame] | 4612 | |
| 4613 | trace_btrfs_truncate_show_fi_regular( |
| 4614 | BTRFS_I(inode), leaf, fi, |
| 4615 | found_key.offset); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4616 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
Qu Wenruo | e41ca58 | 2018-06-06 15:41:49 +0800 | [diff] [blame] | 4617 | item_end += btrfs_file_extent_ram_bytes(leaf, |
| 4618 | fi); |
Liu Bo | 09ed2f1 | 2017-03-10 11:09:48 -0800 | [diff] [blame] | 4619 | |
| 4620 | trace_btrfs_truncate_show_fi_inline( |
| 4621 | BTRFS_I(inode), leaf, fi, path->slots[0], |
| 4622 | found_key.offset); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4623 | } |
Yan | 008630c | 2007-11-07 13:31:09 -0500 | [diff] [blame] | 4624 | item_end--; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4625 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4626 | if (found_type > min_type) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4627 | del_item = 1; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4628 | } else { |
Filipe Manana | 76b42ab | 2017-02-14 16:56:01 +0000 | [diff] [blame] | 4629 | if (item_end < new_size) |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4630 | break; |
| 4631 | if (found_key.offset >= new_size) |
| 4632 | del_item = 1; |
| 4633 | else |
| 4634 | del_item = 0; |
| 4635 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4636 | found_extent = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4637 | /* FIXME, shrink the extent if the ref count is only 1 */ |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4638 | if (found_type != BTRFS_EXTENT_DATA_KEY) |
| 4639 | goto delete; |
| 4640 | |
| 4641 | if (extent_type != BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4642 | u64 num_dec; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4643 | extent_start = btrfs_file_extent_disk_bytenr(leaf, fi); |
Miao Xie | f70a9a6b | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 4644 | if (!del_item) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4645 | u64 orig_num_bytes = |
| 4646 | btrfs_file_extent_num_bytes(leaf, fi); |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 4647 | extent_num_bytes = ALIGN(new_size - |
| 4648 | found_key.offset, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4649 | fs_info->sectorsize); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4650 | btrfs_set_file_extent_num_bytes(leaf, fi, |
| 4651 | extent_num_bytes); |
| 4652 | num_dec = (orig_num_bytes - |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 4653 | extent_num_bytes); |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 4654 | if (test_bit(BTRFS_ROOT_REF_COWS, |
| 4655 | &root->state) && |
| 4656 | extent_start != 0) |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 4657 | inode_sub_bytes(inode, num_dec); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4658 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4659 | } else { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4660 | extent_num_bytes = |
| 4661 | btrfs_file_extent_disk_num_bytes(leaf, |
| 4662 | fi); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4663 | extent_offset = found_key.offset - |
| 4664 | btrfs_file_extent_offset(leaf, fi); |
| 4665 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4666 | /* FIXME blocksize != 4096 */ |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 4667 | num_dec = btrfs_file_extent_num_bytes(leaf, fi); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4668 | if (extent_start != 0) { |
| 4669 | found_extent = 1; |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 4670 | if (test_bit(BTRFS_ROOT_REF_COWS, |
| 4671 | &root->state)) |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 4672 | inode_sub_bytes(inode, num_dec); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4673 | } |
| 4674 | } |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 4675 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4676 | /* |
| 4677 | * we can't truncate inline items that have had |
| 4678 | * special encodings |
| 4679 | */ |
| 4680 | if (!del_item && |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4681 | btrfs_file_extent_encryption(leaf, fi) == 0 && |
Josef Bacik | ddfae63 | 2017-10-19 14:16:02 -0400 | [diff] [blame] | 4682 | btrfs_file_extent_other_encoding(leaf, fi) == 0 && |
| 4683 | btrfs_file_extent_compression(leaf, fi) == 0) { |
| 4684 | u32 size = (u32)(new_size - found_key.offset); |
Chris Mason | 514ac8a | 2014-01-03 21:07:00 -0800 | [diff] [blame] | 4685 | |
Josef Bacik | ddfae63 | 2017-10-19 14:16:02 -0400 | [diff] [blame] | 4686 | btrfs_set_file_extent_ram_bytes(leaf, fi, size); |
| 4687 | size = btrfs_file_extent_calc_inline_size(size); |
David Sterba | 78ac4f9 | 2019-03-20 14:49:12 +0100 | [diff] [blame] | 4688 | btrfs_truncate_item(path, size, 1); |
Josef Bacik | ddfae63 | 2017-10-19 14:16:02 -0400 | [diff] [blame] | 4689 | } else if (!del_item) { |
Chris Mason | 514ac8a | 2014-01-03 21:07:00 -0800 | [diff] [blame] | 4690 | /* |
Josef Bacik | ddfae63 | 2017-10-19 14:16:02 -0400 | [diff] [blame] | 4691 | * We have to bail so the last_size is set to |
| 4692 | * just before this extent. |
Chris Mason | 514ac8a | 2014-01-03 21:07:00 -0800 | [diff] [blame] | 4693 | */ |
Omar Sandoval | fd86a3a | 2018-05-11 13:13:30 -0700 | [diff] [blame] | 4694 | ret = NEED_TRUNCATE_BLOCK; |
Josef Bacik | ddfae63 | 2017-10-19 14:16:02 -0400 | [diff] [blame] | 4695 | break; |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 4696 | } |
Josef Bacik | ddfae63 | 2017-10-19 14:16:02 -0400 | [diff] [blame] | 4697 | |
| 4698 | if (test_bit(BTRFS_ROOT_REF_COWS, &root->state)) |
| 4699 | inode_sub_bytes(inode, item_end + 1 - new_size); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4700 | } |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4701 | delete: |
Josef Bacik | ddfae63 | 2017-10-19 14:16:02 -0400 | [diff] [blame] | 4702 | if (del_item) |
| 4703 | last_size = found_key.offset; |
| 4704 | else |
| 4705 | last_size = new_size; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4706 | if (del_item) { |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4707 | if (!pending_del_nr) { |
| 4708 | /* no pending yet, add ourselves */ |
| 4709 | pending_del_slot = path->slots[0]; |
| 4710 | pending_del_nr = 1; |
| 4711 | } else if (pending_del_nr && |
| 4712 | path->slots[0] + 1 == pending_del_slot) { |
| 4713 | /* hop on the pending chunk */ |
| 4714 | pending_del_nr++; |
| 4715 | pending_del_slot = path->slots[0]; |
| 4716 | } else { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4717 | BUG(); |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4718 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4719 | } else { |
| 4720 | break; |
| 4721 | } |
Thomas Meyer | 897ca81 | 2017-10-07 16:02:21 +0200 | [diff] [blame] | 4722 | should_throttle = false; |
Chris Mason | 28f75a0 | 2015-02-04 06:59:29 -0800 | [diff] [blame] | 4723 | |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 4724 | if (found_extent && |
| 4725 | (test_bit(BTRFS_ROOT_REF_COWS, &root->state) || |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4726 | root == fs_info->tree_root)) { |
Qu Wenruo | ffd4bb2 | 2019-04-04 14:45:36 +0800 | [diff] [blame] | 4727 | struct btrfs_ref ref = { 0 }; |
| 4728 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 4729 | btrfs_set_path_blocking(path); |
Chris Mason | 28ed134 | 2014-12-17 09:41:04 -0800 | [diff] [blame] | 4730 | bytes_deleted += extent_num_bytes; |
Qu Wenruo | ffd4bb2 | 2019-04-04 14:45:36 +0800 | [diff] [blame] | 4731 | |
| 4732 | btrfs_init_generic_ref(&ref, BTRFS_DROP_DELAYED_REF, |
| 4733 | extent_start, extent_num_bytes, 0); |
| 4734 | ref.real_root = root->root_key.objectid; |
| 4735 | btrfs_init_data_ref(&ref, btrfs_header_owner(leaf), |
| 4736 | ino, extent_offset); |
| 4737 | ret = btrfs_free_extent(trans, &ref); |
Omar Sandoval | 0552210 | 2018-05-11 13:13:31 -0700 | [diff] [blame] | 4738 | if (ret) { |
| 4739 | btrfs_abort_transaction(trans, ret); |
| 4740 | break; |
| 4741 | } |
Chris Mason | 28f75a0 | 2015-02-04 06:59:29 -0800 | [diff] [blame] | 4742 | if (be_nice) { |
Lu Fengqi | 7c86162 | 2018-10-11 13:40:36 +0800 | [diff] [blame] | 4743 | if (btrfs_should_throttle_delayed_refs(trans)) |
Thomas Meyer | 897ca81 | 2017-10-07 16:02:21 +0200 | [diff] [blame] | 4744 | should_throttle = true; |
Chris Mason | 28f75a0 | 2015-02-04 06:59:29 -0800 | [diff] [blame] | 4745 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4746 | } |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4747 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4748 | if (found_type == BTRFS_INODE_ITEM_KEY) |
| 4749 | break; |
| 4750 | |
| 4751 | if (path->slots[0] == 0 || |
Josef Bacik | 1262133 | 2015-02-03 07:50:16 -0800 | [diff] [blame] | 4752 | path->slots[0] != pending_del_slot || |
Josef Bacik | 28bad21 | 2018-12-03 10:20:38 -0500 | [diff] [blame] | 4753 | should_throttle) { |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4754 | if (pending_del_nr) { |
| 4755 | ret = btrfs_del_items(trans, root, path, |
| 4756 | pending_del_slot, |
| 4757 | pending_del_nr); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4758 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 4759 | btrfs_abort_transaction(trans, ret); |
Omar Sandoval | fd86a3a | 2018-05-11 13:13:30 -0700 | [diff] [blame] | 4760 | break; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4761 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4762 | pending_del_nr = 0; |
| 4763 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4764 | btrfs_release_path(path); |
Josef Bacik | 28bad21 | 2018-12-03 10:20:38 -0500 | [diff] [blame] | 4765 | |
Chris Mason | 28f75a0 | 2015-02-04 06:59:29 -0800 | [diff] [blame] | 4766 | /* |
Josef Bacik | 28bad21 | 2018-12-03 10:20:38 -0500 | [diff] [blame] | 4767 | * We can generate a lot of delayed refs, so we need to |
| 4768 | * throttle every once and a while and make sure we're |
| 4769 | * adding enough space to keep up with the work we are |
| 4770 | * generating. Since we hold a transaction here we |
| 4771 | * can't flush, and we don't want to FLUSH_LIMIT because |
| 4772 | * we could have generated too many delayed refs to |
| 4773 | * actually allocate, so just bail if we're short and |
| 4774 | * let the normal reservation dance happen higher up. |
Chris Mason | 28f75a0 | 2015-02-04 06:59:29 -0800 | [diff] [blame] | 4775 | */ |
Josef Bacik | 28bad21 | 2018-12-03 10:20:38 -0500 | [diff] [blame] | 4776 | if (should_throttle) { |
| 4777 | ret = btrfs_delayed_refs_rsv_refill(fs_info, |
| 4778 | BTRFS_RESERVE_NO_FLUSH); |
| 4779 | if (ret) { |
| 4780 | ret = -EAGAIN; |
| 4781 | break; |
| 4782 | } |
Chris Mason | 28f75a0 | 2015-02-04 06:59:29 -0800 | [diff] [blame] | 4783 | } |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4784 | goto search_again; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4785 | } else { |
| 4786 | path->slots[0]--; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4787 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4788 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4789 | out: |
Omar Sandoval | fd86a3a | 2018-05-11 13:13:30 -0700 | [diff] [blame] | 4790 | if (ret >= 0 && pending_del_nr) { |
| 4791 | int err; |
| 4792 | |
| 4793 | err = btrfs_del_items(trans, root, path, pending_del_slot, |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4794 | pending_del_nr); |
Omar Sandoval | fd86a3a | 2018-05-11 13:13:30 -0700 | [diff] [blame] | 4795 | if (err) { |
| 4796 | btrfs_abort_transaction(trans, err); |
| 4797 | ret = err; |
| 4798 | } |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4799 | } |
Filipe Manana | 76b42ab | 2017-02-14 16:56:01 +0000 | [diff] [blame] | 4800 | if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) { |
| 4801 | ASSERT(last_size >= new_size); |
Omar Sandoval | fd86a3a | 2018-05-11 13:13:30 -0700 | [diff] [blame] | 4802 | if (!ret && last_size > new_size) |
Filipe Manana | 76b42ab | 2017-02-14 16:56:01 +0000 | [diff] [blame] | 4803 | last_size = new_size; |
Josef Bacik | 7f4f6e0 | 2013-08-29 16:43:28 -0400 | [diff] [blame] | 4804 | btrfs_ordered_update_i_size(inode, last_size, NULL); |
Filipe Manana | 76b42ab | 2017-02-14 16:56:01 +0000 | [diff] [blame] | 4805 | } |
Chris Mason | 28ed134 | 2014-12-17 09:41:04 -0800 | [diff] [blame] | 4806 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4807 | btrfs_free_path(path); |
Omar Sandoval | fd86a3a | 2018-05-11 13:13:30 -0700 | [diff] [blame] | 4808 | return ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4809 | } |
| 4810 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4811 | /* |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4812 | * btrfs_truncate_block - read, zero a chunk and write a block |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 4813 | * @inode - inode that we're zeroing |
| 4814 | * @from - the offset to start zeroing |
| 4815 | * @len - the length to zero, 0 to zero the entire range respective to the |
| 4816 | * offset |
| 4817 | * @front - zero up to the offset instead of from the offset on |
| 4818 | * |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4819 | * 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] | 4820 | * 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] | 4821 | */ |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4822 | 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] | 4823 | int front) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4824 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4825 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 4826 | struct address_space *mapping = inode->i_mapping; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4827 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
| 4828 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4829 | struct extent_state *cached_state = NULL; |
Qu Wenruo | 364ecf3 | 2017-02-27 15:10:38 +0800 | [diff] [blame] | 4830 | struct extent_changeset *data_reserved = NULL; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4831 | char *kaddr; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4832 | u32 blocksize = fs_info->sectorsize; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 4833 | pgoff_t index = from >> PAGE_SHIFT; |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4834 | unsigned offset = from & (blocksize - 1); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4835 | struct page *page; |
Josef Bacik | 3b16a4e | 2011-09-21 15:05:58 -0400 | [diff] [blame] | 4836 | gfp_t mask = btrfs_alloc_write_mask(mapping); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4837 | int ret = 0; |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4838 | u64 block_start; |
| 4839 | u64 block_end; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4840 | |
Nikolay Borisov | b03ebd9 | 2018-01-18 14:47:06 +0200 | [diff] [blame] | 4841 | if (IS_ALIGNED(offset, blocksize) && |
| 4842 | (!len || IS_ALIGNED(len, blocksize))) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4843 | goto out; |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4844 | |
Josef Bacik | 8b62f87 | 2017-10-19 14:15:55 -0400 | [diff] [blame] | 4845 | block_start = round_down(from, blocksize); |
| 4846 | block_end = block_start + blocksize - 1; |
| 4847 | |
Qu Wenruo | 364ecf3 | 2017-02-27 15:10:38 +0800 | [diff] [blame] | 4848 | ret = btrfs_delalloc_reserve_space(inode, &data_reserved, |
Josef Bacik | 8b62f87 | 2017-10-19 14:15:55 -0400 | [diff] [blame] | 4849 | block_start, blocksize); |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 4850 | if (ret) |
| 4851 | goto out; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4852 | |
Chris Mason | 211c17f | 2008-05-15 09:13:45 -0400 | [diff] [blame] | 4853 | again: |
Josef Bacik | 3b16a4e | 2011-09-21 15:05:58 -0400 | [diff] [blame] | 4854 | page = find_or_create_page(mapping, index, mask); |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 4855 | if (!page) { |
Qu Wenruo | bc42bda | 2017-02-27 15:10:39 +0800 | [diff] [blame] | 4856 | btrfs_delalloc_release_space(inode, data_reserved, |
Qu Wenruo | 43b1859 | 2017-12-12 15:34:32 +0800 | [diff] [blame] | 4857 | block_start, blocksize, true); |
| 4858 | btrfs_delalloc_release_extents(BTRFS_I(inode), blocksize, true); |
Miao Xie | ac6a2b3 | 2012-12-05 10:56:13 +0000 | [diff] [blame] | 4859 | ret = -ENOMEM; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4860 | goto out; |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 4861 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4862 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4863 | if (!PageUptodate(page)) { |
| 4864 | ret = btrfs_readpage(NULL, page); |
| 4865 | lock_page(page); |
Chris Mason | 211c17f | 2008-05-15 09:13:45 -0400 | [diff] [blame] | 4866 | if (page->mapping != mapping) { |
| 4867 | unlock_page(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 4868 | put_page(page); |
Chris Mason | 211c17f | 2008-05-15 09:13:45 -0400 | [diff] [blame] | 4869 | goto again; |
| 4870 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4871 | if (!PageUptodate(page)) { |
| 4872 | ret = -EIO; |
Chris Mason | 8964222 | 2008-07-24 09:41:53 -0400 | [diff] [blame] | 4873 | goto out_unlock; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4874 | } |
| 4875 | } |
Chris Mason | 211c17f | 2008-05-15 09:13:45 -0400 | [diff] [blame] | 4876 | wait_on_page_writeback(page); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4877 | |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4878 | lock_extent_bits(io_tree, block_start, block_end, &cached_state); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4879 | set_page_extent_mapped(page); |
| 4880 | |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4881 | ordered = btrfs_lookup_ordered_extent(inode, block_start); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4882 | if (ordered) { |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4883 | unlock_extent_cached(io_tree, block_start, block_end, |
David Sterba | e43bbe5 | 2017-12-12 21:43:52 +0100 | [diff] [blame] | 4884 | &cached_state); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4885 | unlock_page(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 4886 | put_page(page); |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 4887 | btrfs_start_ordered_extent(inode, ordered, 1); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4888 | btrfs_put_ordered_extent(ordered); |
| 4889 | goto again; |
| 4890 | } |
| 4891 | |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4892 | clear_extent_bit(&BTRFS_I(inode)->io_tree, block_start, block_end, |
Liu Bo | 9e8a4a8 | 2012-09-05 19:10:51 -0600 | [diff] [blame] | 4893 | EXTENT_DIRTY | EXTENT_DELALLOC | |
| 4894 | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, |
David Sterba | ae0f162 | 2017-10-31 16:37:52 +0100 | [diff] [blame] | 4895 | 0, 0, &cached_state); |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 4896 | |
Filipe Manana | e3b8a48 | 2017-11-04 00:16:59 +0000 | [diff] [blame] | 4897 | ret = btrfs_set_extent_delalloc(inode, block_start, block_end, 0, |
Qu Wenruo | ba8b04c | 2016-07-19 16:50:36 +0800 | [diff] [blame] | 4898 | &cached_state, 0); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4899 | if (ret) { |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4900 | unlock_extent_cached(io_tree, block_start, block_end, |
David Sterba | e43bbe5 | 2017-12-12 21:43:52 +0100 | [diff] [blame] | 4901 | &cached_state); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4902 | goto out_unlock; |
| 4903 | } |
| 4904 | |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4905 | if (offset != blocksize) { |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 4906 | if (!len) |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4907 | len = blocksize - offset; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4908 | kaddr = kmap(page); |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 4909 | if (front) |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4910 | memset(kaddr + (block_start - page_offset(page)), |
| 4911 | 0, offset); |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 4912 | else |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4913 | memset(kaddr + (block_start - page_offset(page)) + offset, |
| 4914 | 0, len); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4915 | flush_dcache_page(page); |
| 4916 | kunmap(page); |
| 4917 | } |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 4918 | ClearPageChecked(page); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4919 | set_page_dirty(page); |
David Sterba | e43bbe5 | 2017-12-12 21:43:52 +0100 | [diff] [blame] | 4920 | unlock_extent_cached(io_tree, block_start, block_end, &cached_state); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4921 | |
Chris Mason | 8964222 | 2008-07-24 09:41:53 -0400 | [diff] [blame] | 4922 | out_unlock: |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 4923 | if (ret) |
Qu Wenruo | bc42bda | 2017-02-27 15:10:39 +0800 | [diff] [blame] | 4924 | btrfs_delalloc_release_space(inode, data_reserved, block_start, |
Qu Wenruo | 43b1859 | 2017-12-12 15:34:32 +0800 | [diff] [blame] | 4925 | blocksize, true); |
| 4926 | btrfs_delalloc_release_extents(BTRFS_I(inode), blocksize, (ret != 0)); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4927 | unlock_page(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 4928 | put_page(page); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4929 | out: |
Qu Wenruo | 364ecf3 | 2017-02-27 15:10:38 +0800 | [diff] [blame] | 4930 | extent_changeset_free(data_reserved); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4931 | return ret; |
| 4932 | } |
| 4933 | |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4934 | static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode, |
| 4935 | u64 offset, u64 len) |
| 4936 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4937 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4938 | struct btrfs_trans_handle *trans; |
| 4939 | int ret; |
| 4940 | |
| 4941 | /* |
| 4942 | * Still need to make sure the inode looks like it's been updated so |
| 4943 | * that any holes get logged if we fsync. |
| 4944 | */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4945 | if (btrfs_fs_incompat(fs_info, NO_HOLES)) { |
| 4946 | BTRFS_I(inode)->last_trans = fs_info->generation; |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4947 | BTRFS_I(inode)->last_sub_trans = root->log_transid; |
| 4948 | BTRFS_I(inode)->last_log_commit = root->last_log_commit; |
| 4949 | return 0; |
| 4950 | } |
| 4951 | |
| 4952 | /* |
| 4953 | * 1 - for the one we're dropping |
| 4954 | * 1 - for the one we're adding |
| 4955 | * 1 - for updating the inode. |
| 4956 | */ |
| 4957 | trans = btrfs_start_transaction(root, 3); |
| 4958 | if (IS_ERR(trans)) |
| 4959 | return PTR_ERR(trans); |
| 4960 | |
| 4961 | ret = btrfs_drop_extents(trans, root, inode, offset, offset + len, 1); |
| 4962 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 4963 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 4964 | btrfs_end_transaction(trans); |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4965 | return ret; |
| 4966 | } |
| 4967 | |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 4968 | ret = btrfs_insert_file_extent(trans, root, btrfs_ino(BTRFS_I(inode)), |
| 4969 | offset, 0, 0, len, 0, len, 0, 0, 0); |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4970 | if (ret) |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 4971 | btrfs_abort_transaction(trans, ret); |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4972 | else |
| 4973 | btrfs_update_inode(trans, root, inode); |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 4974 | btrfs_end_transaction(trans); |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4975 | return ret; |
| 4976 | } |
| 4977 | |
Josef Bacik | 695a0d0 | 2011-03-04 15:46:53 -0500 | [diff] [blame] | 4978 | /* |
| 4979 | * This function puts in dummy file extents for the area we're creating a hole |
| 4980 | * for. So if we are truncating this file to a larger size we need to insert |
| 4981 | * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for |
| 4982 | * the range between oldsize and size |
| 4983 | */ |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 4984 | 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] | 4985 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4986 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4987 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 4988 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4989 | struct extent_map *em = NULL; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4990 | struct extent_state *cached_state = NULL; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4991 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4992 | u64 hole_start = ALIGN(oldsize, fs_info->sectorsize); |
| 4993 | u64 block_end = ALIGN(size, fs_info->sectorsize); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4994 | u64 last_byte; |
| 4995 | u64 cur_offset; |
| 4996 | u64 hole_size; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4997 | int err = 0; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4998 | |
Josef Bacik | a71754f | 2013-06-17 17:14:39 -0400 | [diff] [blame] | 4999 | /* |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 5000 | * If our size started in the middle of a block we need to zero out the |
| 5001 | * 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] | 5002 | * expose stale data. |
| 5003 | */ |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 5004 | err = btrfs_truncate_block(inode, oldsize, 0, 0); |
Josef Bacik | a71754f | 2013-06-17 17:14:39 -0400 | [diff] [blame] | 5005 | if (err) |
| 5006 | return err; |
| 5007 | |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 5008 | if (size <= hole_start) |
| 5009 | return 0; |
| 5010 | |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 5011 | while (1) { |
| 5012 | struct btrfs_ordered_extent *ordered; |
Miao Xie | fa7c149 | 2013-09-26 13:15:27 +0800 | [diff] [blame] | 5013 | |
David Sterba | ff13db4 | 2015-12-03 14:30:40 +0100 | [diff] [blame] | 5014 | lock_extent_bits(io_tree, hole_start, block_end - 1, |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 5015 | &cached_state); |
Nikolay Borisov | a776c6f | 2017-02-20 13:50:49 +0200 | [diff] [blame] | 5016 | ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), hole_start, |
Miao Xie | fa7c149 | 2013-09-26 13:15:27 +0800 | [diff] [blame] | 5017 | block_end - hole_start); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 5018 | if (!ordered) |
| 5019 | break; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 5020 | unlock_extent_cached(io_tree, hole_start, block_end - 1, |
David Sterba | e43bbe5 | 2017-12-12 21:43:52 +0100 | [diff] [blame] | 5021 | &cached_state); |
Miao Xie | fa7c149 | 2013-09-26 13:15:27 +0800 | [diff] [blame] | 5022 | btrfs_start_ordered_extent(inode, ordered, 1); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 5023 | btrfs_put_ordered_extent(ordered); |
| 5024 | } |
| 5025 | |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 5026 | cur_offset = hole_start; |
| 5027 | while (1) { |
Nikolay Borisov | fc4f21b1 | 2017-02-20 13:51:06 +0200 | [diff] [blame] | 5028 | em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, cur_offset, |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 5029 | block_end - cur_offset, 0); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5030 | if (IS_ERR(em)) { |
| 5031 | err = PTR_ERR(em); |
Zach Brown | f276795 | 2013-01-08 19:37:58 +0000 | [diff] [blame] | 5032 | em = NULL; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5033 | break; |
| 5034 | } |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 5035 | last_byte = min(extent_map_end(em), block_end); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5036 | last_byte = ALIGN(last_byte, fs_info->sectorsize); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5037 | if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) { |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 5038 | struct extent_map *hole_em; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 5039 | hole_size = last_byte - cur_offset; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5040 | |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 5041 | err = maybe_insert_hole(root, inode, cur_offset, |
| 5042 | hole_size); |
| 5043 | if (err) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 5044 | break; |
Nikolay Borisov | dcdbc05 | 2017-02-20 13:50:45 +0200 | [diff] [blame] | 5045 | btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset, |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 5046 | cur_offset + hole_size - 1, 0); |
| 5047 | hole_em = alloc_extent_map(); |
| 5048 | if (!hole_em) { |
| 5049 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, |
| 5050 | &BTRFS_I(inode)->runtime_flags); |
| 5051 | goto next; |
| 5052 | } |
| 5053 | hole_em->start = cur_offset; |
| 5054 | hole_em->len = hole_size; |
| 5055 | hole_em->orig_start = cur_offset; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5056 | |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 5057 | hole_em->block_start = EXTENT_MAP_HOLE; |
| 5058 | hole_em->block_len = 0; |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 5059 | hole_em->orig_block_len = 0; |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 5060 | hole_em->ram_bytes = hole_size; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5061 | hole_em->bdev = fs_info->fs_devices->latest_bdev; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 5062 | hole_em->compress_type = BTRFS_COMPRESS_NONE; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5063 | hole_em->generation = fs_info->generation; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 5064 | |
| 5065 | while (1) { |
| 5066 | write_lock(&em_tree->lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 5067 | err = add_extent_mapping(em_tree, hole_em, 1); |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 5068 | write_unlock(&em_tree->lock); |
| 5069 | if (err != -EEXIST) |
| 5070 | break; |
Nikolay Borisov | dcdbc05 | 2017-02-20 13:50:45 +0200 | [diff] [blame] | 5071 | btrfs_drop_extent_cache(BTRFS_I(inode), |
| 5072 | cur_offset, |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 5073 | cur_offset + |
| 5074 | hole_size - 1, 0); |
| 5075 | } |
| 5076 | free_extent_map(hole_em); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 5077 | } |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 5078 | next: |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 5079 | free_extent_map(em); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 5080 | em = NULL; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 5081 | cur_offset = last_byte; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5082 | if (cur_offset >= block_end) |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 5083 | break; |
| 5084 | } |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 5085 | free_extent_map(em); |
David Sterba | e43bbe5 | 2017-12-12 21:43:52 +0100 | [diff] [blame] | 5086 | unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 5087 | return err; |
| 5088 | } |
| 5089 | |
Eric Sandeen | 3972f26 | 2013-01-12 02:57:22 +0000 | [diff] [blame] | 5090 | static int btrfs_setsize(struct inode *inode, struct iattr *attr) |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5091 | { |
Miao Xie | f4a2f4c | 2011-12-14 20:12:01 -0500 | [diff] [blame] | 5092 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5093 | struct btrfs_trans_handle *trans; |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 5094 | loff_t oldsize = i_size_read(inode); |
Eric Sandeen | 3972f26 | 2013-01-12 02:57:22 +0000 | [diff] [blame] | 5095 | loff_t newsize = attr->ia_size; |
| 5096 | int mask = attr->ia_valid; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5097 | int ret; |
| 5098 | |
Eric Sandeen | 3972f26 | 2013-01-12 02:57:22 +0000 | [diff] [blame] | 5099 | /* |
| 5100 | * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a |
| 5101 | * special case where we need to update the times despite not having |
| 5102 | * these flags set. For all other operations the VFS set these flags |
| 5103 | * explicitly if it wants a timestamp update. |
| 5104 | */ |
Christoph Hellwig | dff6efc | 2013-11-19 07:17:07 -0800 | [diff] [blame] | 5105 | if (newsize != oldsize) { |
| 5106 | inode_inc_iversion(inode); |
| 5107 | if (!(mask & (ATTR_CTIME | ATTR_MTIME))) |
| 5108 | inode->i_ctime = inode->i_mtime = |
Deepa Dinamani | c2050a4 | 2016-09-14 07:48:06 -0700 | [diff] [blame] | 5109 | current_time(inode); |
Christoph Hellwig | dff6efc | 2013-11-19 07:17:07 -0800 | [diff] [blame] | 5110 | } |
Eric Sandeen | 3972f26 | 2013-01-12 02:57:22 +0000 | [diff] [blame] | 5111 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 5112 | if (newsize > oldsize) { |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 5113 | /* |
David Sterba | ea14b57f | 2017-06-22 02:19:11 +0200 | [diff] [blame] | 5114 | * Don't do an expanding truncate while snapshotting is ongoing. |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 5115 | * This is to ensure the snapshot captures a fully consistent |
| 5116 | * state of this file - if the snapshot captures this expanding |
| 5117 | * truncation, it must capture all writes that happened before |
| 5118 | * this truncation. |
| 5119 | */ |
Zhao Lei | 0bc19f90 | 2016-01-06 18:56:36 +0800 | [diff] [blame] | 5120 | btrfs_wait_for_snapshot_creation(root); |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 5121 | ret = btrfs_cont_expand(inode, oldsize, newsize); |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 5122 | if (ret) { |
David Sterba | ea14b57f | 2017-06-22 02:19:11 +0200 | [diff] [blame] | 5123 | btrfs_end_write_no_snapshotting(root); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5124 | return ret; |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 5125 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5126 | |
Miao Xie | f4a2f4c | 2011-12-14 20:12:01 -0500 | [diff] [blame] | 5127 | trans = btrfs_start_transaction(root, 1); |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 5128 | if (IS_ERR(trans)) { |
David Sterba | ea14b57f | 2017-06-22 02:19:11 +0200 | [diff] [blame] | 5129 | btrfs_end_write_no_snapshotting(root); |
Miao Xie | f4a2f4c | 2011-12-14 20:12:01 -0500 | [diff] [blame] | 5130 | return PTR_ERR(trans); |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 5131 | } |
Miao Xie | f4a2f4c | 2011-12-14 20:12:01 -0500 | [diff] [blame] | 5132 | |
| 5133 | i_size_write(inode, newsize); |
| 5134 | btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL); |
Chandan Rajendra | 27772b6 | 2016-01-21 15:56:03 +0530 | [diff] [blame] | 5135 | pagecache_isize_extended(inode, oldsize, newsize); |
Miao Xie | f4a2f4c | 2011-12-14 20:12:01 -0500 | [diff] [blame] | 5136 | ret = btrfs_update_inode(trans, root, inode); |
David Sterba | ea14b57f | 2017-06-22 02:19:11 +0200 | [diff] [blame] | 5137 | btrfs_end_write_no_snapshotting(root); |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 5138 | btrfs_end_transaction(trans); |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 5139 | } else { |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5140 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 5141 | /* |
| 5142 | * We're truncating a file that used to have good data down to |
| 5143 | * zero. Make sure it gets into the ordered flush list so that |
| 5144 | * any new writes get down to disk quickly. |
| 5145 | */ |
| 5146 | if (newsize == 0) |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 5147 | set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE, |
| 5148 | &BTRFS_I(inode)->runtime_flags); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5149 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 5150 | truncate_setsize(inode, newsize); |
Miao Xie | 2e60a51 | 2013-02-08 07:01:08 +0000 | [diff] [blame] | 5151 | |
Andrea Gelmini | 52042d8 | 2018-11-28 12:05:13 +0100 | [diff] [blame] | 5152 | /* Disable nonlocked read DIO to avoid the endless truncate */ |
Nikolay Borisov | abcefb1 | 2017-02-20 13:51:10 +0200 | [diff] [blame] | 5153 | btrfs_inode_block_unlocked_dio(BTRFS_I(inode)); |
Miao Xie | 2e60a51 | 2013-02-08 07:01:08 +0000 | [diff] [blame] | 5154 | inode_dio_wait(inode); |
Nikolay Borisov | 0b58170 | 2017-02-20 13:51:11 +0200 | [diff] [blame] | 5155 | btrfs_inode_resume_unlocked_dio(BTRFS_I(inode)); |
Miao Xie | 2e60a51 | 2013-02-08 07:01:08 +0000 | [diff] [blame] | 5156 | |
Filipe Manana | 213e8c5 | 2018-02-06 20:40:31 +0000 | [diff] [blame] | 5157 | ret = btrfs_truncate(inode, newsize == oldsize); |
Josef Bacik | 7f4f6e0 | 2013-08-29 16:43:28 -0400 | [diff] [blame] | 5158 | if (ret && inode->i_nlink) { |
| 5159 | int err; |
| 5160 | |
| 5161 | /* |
Omar Sandoval | f7e9e8f | 2018-05-11 13:13:32 -0700 | [diff] [blame] | 5162 | * Truncate failed, so fix up the in-memory size. We |
| 5163 | * adjusted disk_i_size down as we removed extents, so |
| 5164 | * wait for disk_i_size to be stable and then update the |
| 5165 | * in-memory size to match. |
Josef Bacik | 7f4f6e0 | 2013-08-29 16:43:28 -0400 | [diff] [blame] | 5166 | */ |
Omar Sandoval | f7e9e8f | 2018-05-11 13:13:32 -0700 | [diff] [blame] | 5167 | err = btrfs_wait_ordered_range(inode, 0, (u64)-1); |
Josef Bacik | 7f4f6e0 | 2013-08-29 16:43:28 -0400 | [diff] [blame] | 5168 | if (err) |
Omar Sandoval | f7e9e8f | 2018-05-11 13:13:32 -0700 | [diff] [blame] | 5169 | return err; |
| 5170 | i_size_write(inode, BTRFS_I(inode)->disk_i_size); |
Josef Bacik | 7f4f6e0 | 2013-08-29 16:43:28 -0400 | [diff] [blame] | 5171 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5172 | } |
| 5173 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 5174 | return ret; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5175 | } |
| 5176 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5177 | static int btrfs_setattr(struct dentry *dentry, struct iattr *attr) |
| 5178 | { |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 5179 | struct inode *inode = d_inode(dentry); |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 5180 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5181 | int err; |
| 5182 | |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 5183 | if (btrfs_root_readonly(root)) |
| 5184 | return -EROFS; |
| 5185 | |
Jan Kara | 31051c8 | 2016-05-26 16:55:18 +0200 | [diff] [blame] | 5186 | err = setattr_prepare(dentry, attr); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5187 | if (err) |
| 5188 | return err; |
| 5189 | |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 5190 | if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) { |
Eric Sandeen | 3972f26 | 2013-01-12 02:57:22 +0000 | [diff] [blame] | 5191 | err = btrfs_setsize(inode, attr); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5192 | if (err) |
| 5193 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5194 | } |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 5195 | |
Christoph Hellwig | 1025774 | 2010-06-04 11:30:02 +0200 | [diff] [blame] | 5196 | if (attr->ia_valid) { |
| 5197 | setattr_copy(inode, attr); |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 5198 | inode_inc_iversion(inode); |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 5199 | err = btrfs_dirty_inode(inode); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 5200 | |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 5201 | if (!err && attr->ia_valid & ATTR_MODE) |
Christoph Hellwig | 996a710 | 2013-12-20 05:16:43 -0800 | [diff] [blame] | 5202 | err = posix_acl_chmod(inode, inode->i_mode); |
Christoph Hellwig | 1025774 | 2010-06-04 11:30:02 +0200 | [diff] [blame] | 5203 | } |
| 5204 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5205 | return err; |
| 5206 | } |
Chris Mason | 61295eb | 2008-01-14 16:24:38 -0500 | [diff] [blame] | 5207 | |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5208 | /* |
| 5209 | * While truncating the inode pages during eviction, we get the VFS calling |
| 5210 | * btrfs_invalidatepage() against each page of the inode. This is slow because |
| 5211 | * the calls to btrfs_invalidatepage() result in a huge amount of calls to |
| 5212 | * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting |
| 5213 | * extent_state structures over and over, wasting lots of time. |
| 5214 | * |
| 5215 | * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all |
| 5216 | * those expensive operations on a per page basis and do only the ordered io |
| 5217 | * finishing, while we release here the extent_map and extent_state structures, |
| 5218 | * without the excessive merging and splitting. |
| 5219 | */ |
| 5220 | static void evict_inode_truncate_pages(struct inode *inode) |
| 5221 | { |
| 5222 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
| 5223 | struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree; |
| 5224 | struct rb_node *node; |
| 5225 | |
| 5226 | ASSERT(inode->i_state & I_FREEING); |
Johannes Weiner | 91b0abe | 2014-04-03 14:47:49 -0700 | [diff] [blame] | 5227 | truncate_inode_pages_final(&inode->i_data); |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5228 | |
| 5229 | write_lock(&map_tree->lock); |
Liu Bo | 07e1ce0 | 2018-08-23 03:51:52 +0800 | [diff] [blame] | 5230 | while (!RB_EMPTY_ROOT(&map_tree->map.rb_root)) { |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5231 | struct extent_map *em; |
| 5232 | |
Liu Bo | 07e1ce0 | 2018-08-23 03:51:52 +0800 | [diff] [blame] | 5233 | node = rb_first_cached(&map_tree->map); |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5234 | em = rb_entry(node, struct extent_map, rb_node); |
Wang Shilong | 180589e | 2013-12-14 15:27:31 +0800 | [diff] [blame] | 5235 | clear_bit(EXTENT_FLAG_PINNED, &em->flags); |
| 5236 | clear_bit(EXTENT_FLAG_LOGGING, &em->flags); |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5237 | remove_extent_mapping(map_tree, em); |
| 5238 | free_extent_map(em); |
Filipe Manana | 7064dd5 | 2014-08-08 02:47:05 +0100 | [diff] [blame] | 5239 | if (need_resched()) { |
| 5240 | write_unlock(&map_tree->lock); |
| 5241 | cond_resched(); |
| 5242 | write_lock(&map_tree->lock); |
| 5243 | } |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5244 | } |
| 5245 | write_unlock(&map_tree->lock); |
| 5246 | |
Filipe Manana | 6ca0709 | 2015-05-26 00:55:42 +0100 | [diff] [blame] | 5247 | /* |
| 5248 | * Keep looping until we have no more ranges in the io tree. |
| 5249 | * We can have ongoing bios started by readpages (called from readahead) |
Filipe Manana | 9c6429d | 2015-06-10 12:55:41 +0100 | [diff] [blame] | 5250 | * that have their endio callback (extent_io.c:end_bio_extent_readpage) |
| 5251 | * still in progress (unlocked the pages in the bio but did not yet |
| 5252 | * unlocked the ranges in the io tree). Therefore this means some |
Filipe Manana | 6ca0709 | 2015-05-26 00:55:42 +0100 | [diff] [blame] | 5253 | * ranges can still be locked and eviction started because before |
| 5254 | * submitting those bios, which are executed by a separate task (work |
| 5255 | * queue kthread), inode references (inode->i_count) were not taken |
| 5256 | * (which would be dropped in the end io callback of each bio). |
| 5257 | * Therefore here we effectively end up waiting for those bios and |
| 5258 | * anyone else holding locked ranges without having bumped the inode's |
| 5259 | * reference count - if we don't do it, when they access the inode's |
| 5260 | * io_tree to unlock a range it may be too late, leading to an |
| 5261 | * use-after-free issue. |
| 5262 | */ |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5263 | spin_lock(&io_tree->lock); |
| 5264 | while (!RB_EMPTY_ROOT(&io_tree->state)) { |
| 5265 | struct extent_state *state; |
| 5266 | struct extent_state *cached_state = NULL; |
Filipe Manana | 6ca0709 | 2015-05-26 00:55:42 +0100 | [diff] [blame] | 5267 | u64 start; |
| 5268 | u64 end; |
Filipe Manana | 421f092 | 2018-10-12 13:02:48 +0100 | [diff] [blame] | 5269 | unsigned state_flags; |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5270 | |
| 5271 | node = rb_first(&io_tree->state); |
| 5272 | state = rb_entry(node, struct extent_state, rb_node); |
Filipe Manana | 6ca0709 | 2015-05-26 00:55:42 +0100 | [diff] [blame] | 5273 | start = state->start; |
| 5274 | end = state->end; |
Filipe Manana | 421f092 | 2018-10-12 13:02:48 +0100 | [diff] [blame] | 5275 | state_flags = state->state; |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5276 | spin_unlock(&io_tree->lock); |
| 5277 | |
David Sterba | ff13db4 | 2015-12-03 14:30:40 +0100 | [diff] [blame] | 5278 | lock_extent_bits(io_tree, start, end, &cached_state); |
Qu Wenruo | b9d0b38 | 2015-09-29 10:35:16 +0800 | [diff] [blame] | 5279 | |
| 5280 | /* |
| 5281 | * If still has DELALLOC flag, the extent didn't reach disk, |
| 5282 | * and its reserved space won't be freed by delayed_ref. |
| 5283 | * So we need to free its reserved space here. |
| 5284 | * (Refer to comment in btrfs_invalidatepage, case 2) |
| 5285 | * |
| 5286 | * Note, end is the bytenr of last byte, so we need + 1 here. |
| 5287 | */ |
Filipe Manana | 421f092 | 2018-10-12 13:02:48 +0100 | [diff] [blame] | 5288 | if (state_flags & EXTENT_DELALLOC) |
Qu Wenruo | bc42bda | 2017-02-27 15:10:39 +0800 | [diff] [blame] | 5289 | btrfs_qgroup_free_data(inode, NULL, start, end - start + 1); |
Qu Wenruo | b9d0b38 | 2015-09-29 10:35:16 +0800 | [diff] [blame] | 5290 | |
Filipe Manana | 6ca0709 | 2015-05-26 00:55:42 +0100 | [diff] [blame] | 5291 | clear_extent_bit(io_tree, start, end, |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5292 | EXTENT_LOCKED | EXTENT_DIRTY | |
| 5293 | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING | |
David Sterba | ae0f162 | 2017-10-31 16:37:52 +0100 | [diff] [blame] | 5294 | EXTENT_DEFRAG, 1, 1, &cached_state); |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5295 | |
Filipe Manana | 7064dd5 | 2014-08-08 02:47:05 +0100 | [diff] [blame] | 5296 | cond_resched(); |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5297 | spin_lock(&io_tree->lock); |
| 5298 | } |
| 5299 | spin_unlock(&io_tree->lock); |
| 5300 | } |
| 5301 | |
Omar Sandoval | 4b9d7b5 | 2018-05-11 13:13:36 -0700 | [diff] [blame] | 5302 | 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] | 5303 | struct btrfs_block_rsv *rsv) |
Omar Sandoval | 4b9d7b5 | 2018-05-11 13:13:36 -0700 | [diff] [blame] | 5304 | { |
| 5305 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 5306 | struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv; |
Josef Bacik | 260e770 | 2018-11-21 14:03:13 -0500 | [diff] [blame] | 5307 | u64 delayed_refs_extra = btrfs_calc_trans_metadata_size(fs_info, 1); |
Omar Sandoval | 4b9d7b5 | 2018-05-11 13:13:36 -0700 | [diff] [blame] | 5308 | int failures = 0; |
| 5309 | |
| 5310 | for (;;) { |
| 5311 | struct btrfs_trans_handle *trans; |
| 5312 | int ret; |
| 5313 | |
Josef Bacik | 260e770 | 2018-11-21 14:03:13 -0500 | [diff] [blame] | 5314 | ret = btrfs_block_rsv_refill(root, rsv, |
| 5315 | rsv->size + delayed_refs_extra, |
Omar Sandoval | 4b9d7b5 | 2018-05-11 13:13:36 -0700 | [diff] [blame] | 5316 | BTRFS_RESERVE_FLUSH_LIMIT); |
| 5317 | |
| 5318 | if (ret && ++failures > 2) { |
| 5319 | btrfs_warn(fs_info, |
| 5320 | "could not allocate space for a delete; will truncate on mount"); |
| 5321 | return ERR_PTR(-ENOSPC); |
| 5322 | } |
| 5323 | |
Josef Bacik | 260e770 | 2018-11-21 14:03:13 -0500 | [diff] [blame] | 5324 | /* |
| 5325 | * Evict can generate a large amount of delayed refs without |
| 5326 | * having a way to add space back since we exhaust our temporary |
| 5327 | * block rsv. We aren't allowed to do FLUSH_ALL in this case |
| 5328 | * because we could deadlock with so many things in the flushing |
| 5329 | * code, so we have to try and hold some extra space to |
| 5330 | * compensate for our delayed ref generation. If we can't get |
| 5331 | * that space then we need see if we can steal our minimum from |
| 5332 | * the global reserve. We will be ratelimited by the amount of |
| 5333 | * space we have for the delayed refs rsv, so we'll end up |
| 5334 | * committing and trying again. |
| 5335 | */ |
Omar Sandoval | 4b9d7b5 | 2018-05-11 13:13:36 -0700 | [diff] [blame] | 5336 | trans = btrfs_join_transaction(root); |
Josef Bacik | 260e770 | 2018-11-21 14:03:13 -0500 | [diff] [blame] | 5337 | if (IS_ERR(trans) || !ret) { |
| 5338 | if (!IS_ERR(trans)) { |
| 5339 | trans->block_rsv = &fs_info->trans_block_rsv; |
| 5340 | trans->bytes_reserved = delayed_refs_extra; |
| 5341 | btrfs_block_rsv_migrate(rsv, trans->block_rsv, |
| 5342 | delayed_refs_extra, 1); |
| 5343 | } |
Omar Sandoval | 4b9d7b5 | 2018-05-11 13:13:36 -0700 | [diff] [blame] | 5344 | return trans; |
Josef Bacik | 260e770 | 2018-11-21 14:03:13 -0500 | [diff] [blame] | 5345 | } |
Omar Sandoval | 4b9d7b5 | 2018-05-11 13:13:36 -0700 | [diff] [blame] | 5346 | |
| 5347 | /* |
| 5348 | * Try to steal from the global reserve if there is space for |
| 5349 | * it. |
| 5350 | */ |
Josef Bacik | 64403612 | 2018-12-03 10:20:36 -0500 | [diff] [blame] | 5351 | if (!btrfs_check_space_for_delayed_refs(fs_info) && |
| 5352 | !btrfs_block_rsv_migrate(global_rsv, rsv, rsv->size, 0)) |
Omar Sandoval | 4b9d7b5 | 2018-05-11 13:13:36 -0700 | [diff] [blame] | 5353 | return trans; |
| 5354 | |
| 5355 | /* If not, commit and try again. */ |
| 5356 | ret = btrfs_commit_transaction(trans); |
| 5357 | if (ret) |
| 5358 | return ERR_PTR(ret); |
| 5359 | } |
| 5360 | } |
| 5361 | |
Al Viro | bd55597 | 2010-06-07 11:35:40 -0400 | [diff] [blame] | 5362 | void btrfs_evict_inode(struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5363 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5364 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5365 | struct btrfs_trans_handle *trans; |
| 5366 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Omar Sandoval | 4b9d7b5 | 2018-05-11 13:13:36 -0700 | [diff] [blame] | 5367 | struct btrfs_block_rsv *rsv; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5368 | int ret; |
| 5369 | |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 5370 | trace_btrfs_inode_evict(inode); |
| 5371 | |
Nikolay Borisov | 3d48d98 | 2016-06-29 09:46:41 +0300 | [diff] [blame] | 5372 | if (!root) { |
Liu Bo | e8f1bc1 | 2018-01-25 11:02:53 -0700 | [diff] [blame] | 5373 | clear_inode(inode); |
Nikolay Borisov | 3d48d98 | 2016-06-29 09:46:41 +0300 | [diff] [blame] | 5374 | return; |
| 5375 | } |
| 5376 | |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5377 | evict_inode_truncate_pages(inode); |
| 5378 | |
Stefan Behrens | 69e9c6c | 2013-09-05 16:58:43 +0200 | [diff] [blame] | 5379 | if (inode->i_nlink && |
| 5380 | ((btrfs_root_refs(&root->root_item) != 0 && |
| 5381 | root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) || |
Nikolay Borisov | 70ddc55 | 2017-02-20 13:50:35 +0200 | [diff] [blame] | 5382 | btrfs_is_free_space_inode(BTRFS_I(inode)))) |
Al Viro | bd55597 | 2010-06-07 11:35:40 -0400 | [diff] [blame] | 5383 | goto no_delete; |
| 5384 | |
Omar Sandoval | 2791906 | 2018-05-11 13:13:37 -0700 | [diff] [blame] | 5385 | if (is_bad_inode(inode)) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5386 | goto no_delete; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5387 | |
Nikolay Borisov | 7ab7956 | 2017-02-20 13:50:57 +0200 | [diff] [blame] | 5388 | btrfs_free_io_failure_record(BTRFS_I(inode), 0, (u64)-1); |
Miao Xie | f612496 | 2014-09-12 18:44:04 +0800 | [diff] [blame] | 5389 | |
Omar Sandoval | 7b40b69 | 2018-05-11 13:13:33 -0700 | [diff] [blame] | 5390 | if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 5391 | goto no_delete; |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 5392 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5393 | if (inode->i_nlink > 0) { |
Stefan Behrens | 69e9c6c | 2013-09-05 16:58:43 +0200 | [diff] [blame] | 5394 | BUG_ON(btrfs_root_refs(&root->root_item) != 0 && |
| 5395 | root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5396 | goto no_delete; |
| 5397 | } |
| 5398 | |
Nikolay Borisov | aa79021 | 2017-01-10 20:35:40 +0200 | [diff] [blame] | 5399 | ret = btrfs_commit_inode_delayed_inode(BTRFS_I(inode)); |
Omar Sandoval | 2791906 | 2018-05-11 13:13:37 -0700 | [diff] [blame] | 5400 | if (ret) |
Miao Xie | 0e8c36a | 2012-12-19 06:59:51 +0000 | [diff] [blame] | 5401 | goto no_delete; |
Miao Xie | 0e8c36a | 2012-12-19 06:59:51 +0000 | [diff] [blame] | 5402 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5403 | rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP); |
Omar Sandoval | 2791906 | 2018-05-11 13:13:37 -0700 | [diff] [blame] | 5404 | if (!rsv) |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 5405 | goto no_delete; |
Josef Bacik | ad80cf5 | 2018-09-28 07:18:19 -0400 | [diff] [blame] | 5406 | rsv->size = btrfs_calc_trunc_metadata_size(fs_info, 1); |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 5407 | rsv->failfast = 1; |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 5408 | |
Nikolay Borisov | 6ef06d2 | 2017-02-20 13:50:34 +0200 | [diff] [blame] | 5409 | btrfs_i_size_write(BTRFS_I(inode), 0); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5410 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5411 | while (1) { |
Josef Bacik | ad80cf5 | 2018-09-28 07:18:19 -0400 | [diff] [blame] | 5412 | trans = evict_refill_and_join(root, rsv); |
Omar Sandoval | 2791906 | 2018-05-11 13:13:37 -0700 | [diff] [blame] | 5413 | if (IS_ERR(trans)) |
| 5414 | goto free_rsv; |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 5415 | |
| 5416 | trans->block_rsv = rsv; |
| 5417 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5418 | ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0); |
Omar Sandoval | 2791906 | 2018-05-11 13:13:37 -0700 | [diff] [blame] | 5419 | trans->block_rsv = &fs_info->trans_block_rsv; |
| 5420 | btrfs_end_transaction(trans); |
| 5421 | btrfs_btree_balance_dirty(fs_info); |
| 5422 | if (ret && ret != -ENOSPC && ret != -EAGAIN) |
| 5423 | goto free_rsv; |
| 5424 | else if (!ret) |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5425 | break; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 5426 | } |
| 5427 | |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 5428 | /* |
Omar Sandoval | 2791906 | 2018-05-11 13:13:37 -0700 | [diff] [blame] | 5429 | * Errors here aren't a big deal, it just means we leave orphan items in |
| 5430 | * the tree. They will be cleaned up on the next mount. If the inode |
| 5431 | * number gets reused, cleanup deletes the orphan item without doing |
| 5432 | * anything, and unlink reuses the existing orphan item. |
| 5433 | * |
| 5434 | * If it turns out that we are dropping too many of these, we might want |
| 5435 | * to add a mechanism for retrying these after a commit. |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 5436 | */ |
Josef Bacik | ad80cf5 | 2018-09-28 07:18:19 -0400 | [diff] [blame] | 5437 | trans = evict_refill_and_join(root, rsv); |
Omar Sandoval | 2791906 | 2018-05-11 13:13:37 -0700 | [diff] [blame] | 5438 | if (!IS_ERR(trans)) { |
| 5439 | trans->block_rsv = rsv; |
| 5440 | btrfs_orphan_del(trans, BTRFS_I(inode)); |
| 5441 | trans->block_rsv = &fs_info->trans_block_rsv; |
| 5442 | btrfs_end_transaction(trans); |
| 5443 | } |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 5444 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5445 | if (!(root == fs_info->tree_root || |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 5446 | root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)) |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 5447 | btrfs_return_ino(root, btrfs_ino(BTRFS_I(inode))); |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 5448 | |
Omar Sandoval | 2791906 | 2018-05-11 13:13:37 -0700 | [diff] [blame] | 5449 | free_rsv: |
| 5450 | btrfs_free_block_rsv(fs_info, rsv); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5451 | no_delete: |
Omar Sandoval | 2791906 | 2018-05-11 13:13:37 -0700 | [diff] [blame] | 5452 | /* |
| 5453 | * If we didn't successfully delete, the orphan item will still be in |
| 5454 | * the tree and we'll retry on the next mount. Again, we might also want |
| 5455 | * to retry these periodically in the future. |
| 5456 | */ |
Nikolay Borisov | f48d1cf | 2017-01-10 20:35:39 +0200 | [diff] [blame] | 5457 | btrfs_remove_delayed_node(BTRFS_I(inode)); |
Jan Kara | dbd5768 | 2012-05-03 14:48:02 +0200 | [diff] [blame] | 5458 | clear_inode(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5459 | } |
| 5460 | |
| 5461 | /* |
Qu Wenruo | 6bf9e4b | 2019-03-13 13:55:11 +0800 | [diff] [blame] | 5462 | * Return the key found in the dir entry in the location pointer, fill @type |
| 5463 | * with BTRFS_FT_*, and return 0. |
| 5464 | * |
Su Yue | 005d671 | 2018-03-05 17:13:37 +0800 | [diff] [blame] | 5465 | * If no dir entries were found, returns -ENOENT. |
| 5466 | * If found a corrupted location in dir entry, returns -EUCLEAN. |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5467 | */ |
| 5468 | static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry, |
Qu Wenruo | 6bf9e4b | 2019-03-13 13:55:11 +0800 | [diff] [blame] | 5469 | struct btrfs_key *location, u8 *type) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5470 | { |
| 5471 | const char *name = dentry->d_name.name; |
| 5472 | int namelen = dentry->d_name.len; |
| 5473 | struct btrfs_dir_item *di; |
| 5474 | struct btrfs_path *path; |
| 5475 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Yan | 0d9f7f3 | 2007-10-25 15:48:28 -0400 | [diff] [blame] | 5476 | int ret = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5477 | |
| 5478 | path = btrfs_alloc_path(); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 5479 | if (!path) |
| 5480 | return -ENOMEM; |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 5481 | |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 5482 | di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(BTRFS_I(dir)), |
| 5483 | name, namelen, 0); |
Liu Bo | 3cf5068 | 2018-09-12 06:06:26 +0800 | [diff] [blame] | 5484 | if (IS_ERR_OR_NULL(di)) { |
| 5485 | ret = di ? PTR_ERR(di) : -ENOENT; |
Su Yue | 005d671 | 2018-03-05 17:13:37 +0800 | [diff] [blame] | 5486 | goto out; |
| 5487 | } |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5488 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5489 | btrfs_dir_item_key_to_cpu(path->nodes[0], di, location); |
Liu Bo | 56a0e70 | 2017-10-30 11:14:38 -0600 | [diff] [blame] | 5490 | if (location->type != BTRFS_INODE_ITEM_KEY && |
| 5491 | location->type != BTRFS_ROOT_ITEM_KEY) { |
Su Yue | 005d671 | 2018-03-05 17:13:37 +0800 | [diff] [blame] | 5492 | ret = -EUCLEAN; |
Liu Bo | 56a0e70 | 2017-10-30 11:14:38 -0600 | [diff] [blame] | 5493 | btrfs_warn(root->fs_info, |
| 5494 | "%s gets something invalid in DIR_ITEM (name %s, directory ino %llu, location(%llu %u %llu))", |
| 5495 | __func__, name, btrfs_ino(BTRFS_I(dir)), |
| 5496 | location->objectid, location->type, location->offset); |
Liu Bo | 56a0e70 | 2017-10-30 11:14:38 -0600 | [diff] [blame] | 5497 | } |
Qu Wenruo | 6bf9e4b | 2019-03-13 13:55:11 +0800 | [diff] [blame] | 5498 | if (!ret) |
| 5499 | *type = btrfs_dir_type(path->nodes[0], di); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5500 | out: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5501 | btrfs_free_path(path); |
| 5502 | return ret; |
| 5503 | } |
| 5504 | |
| 5505 | /* |
| 5506 | * when we hit a tree root in a directory, the btrfs part of the inode |
| 5507 | * needs to be changed to reflect the root directory of the tree root. This |
| 5508 | * is kind of like crossing a mount point. |
| 5509 | */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5510 | static int fixup_tree_root_location(struct btrfs_fs_info *fs_info, |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5511 | struct inode *dir, |
| 5512 | struct dentry *dentry, |
| 5513 | struct btrfs_key *location, |
| 5514 | struct btrfs_root **sub_root) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5515 | { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5516 | struct btrfs_path *path; |
| 5517 | struct btrfs_root *new_root; |
| 5518 | struct btrfs_root_ref *ref; |
| 5519 | struct extent_buffer *leaf; |
David Sterba | 1d4c08e | 2015-01-02 19:36:14 +0100 | [diff] [blame] | 5520 | struct btrfs_key key; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5521 | int ret; |
| 5522 | int err = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5523 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5524 | path = btrfs_alloc_path(); |
| 5525 | if (!path) { |
| 5526 | err = -ENOMEM; |
| 5527 | goto out; |
| 5528 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5529 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5530 | err = -ENOENT; |
David Sterba | 1d4c08e | 2015-01-02 19:36:14 +0100 | [diff] [blame] | 5531 | key.objectid = BTRFS_I(dir)->root->root_key.objectid; |
| 5532 | key.type = BTRFS_ROOT_REF_KEY; |
| 5533 | key.offset = location->objectid; |
| 5534 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5535 | 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] | 5536 | if (ret) { |
| 5537 | if (ret < 0) |
| 5538 | err = ret; |
| 5539 | goto out; |
| 5540 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5541 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5542 | leaf = path->nodes[0]; |
| 5543 | ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref); |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 5544 | if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(BTRFS_I(dir)) || |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5545 | btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len) |
| 5546 | goto out; |
| 5547 | |
| 5548 | ret = memcmp_extent_buffer(leaf, dentry->d_name.name, |
| 5549 | (unsigned long)(ref + 1), |
| 5550 | dentry->d_name.len); |
| 5551 | if (ret) |
| 5552 | goto out; |
| 5553 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 5554 | btrfs_release_path(path); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5555 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5556 | new_root = btrfs_read_fs_root_no_name(fs_info, location); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5557 | if (IS_ERR(new_root)) { |
| 5558 | err = PTR_ERR(new_root); |
| 5559 | goto out; |
| 5560 | } |
| 5561 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5562 | *sub_root = new_root; |
| 5563 | location->objectid = btrfs_root_dirid(&new_root->root_item); |
| 5564 | location->type = BTRFS_INODE_ITEM_KEY; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5565 | location->offset = 0; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5566 | err = 0; |
| 5567 | out: |
| 5568 | btrfs_free_path(path); |
| 5569 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5570 | } |
| 5571 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5572 | static void inode_tree_add(struct inode *inode) |
| 5573 | { |
| 5574 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5575 | struct btrfs_inode *entry; |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 5576 | struct rb_node **p; |
| 5577 | struct rb_node *parent; |
Filipe David Borba Manana | cef2193 | 2013-09-02 12:19:13 +0100 | [diff] [blame] | 5578 | struct rb_node *new = &BTRFS_I(inode)->rb_node; |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 5579 | u64 ino = btrfs_ino(BTRFS_I(inode)); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5580 | |
Al Viro | 1d3382cb | 2010-10-23 15:19:20 -0400 | [diff] [blame] | 5581 | if (inode_unhashed(inode)) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5582 | return; |
Miao Xie | e1409ce | 2013-05-15 07:48:16 +0000 | [diff] [blame] | 5583 | parent = NULL; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5584 | spin_lock(&root->inode_lock); |
Miao Xie | e1409ce | 2013-05-15 07:48:16 +0000 | [diff] [blame] | 5585 | p = &root->inode_tree.rb_node; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5586 | while (*p) { |
| 5587 | parent = *p; |
| 5588 | entry = rb_entry(parent, struct btrfs_inode, rb_node); |
| 5589 | |
David Sterba | 3750851 | 2018-06-29 10:56:40 +0200 | [diff] [blame] | 5590 | if (ino < btrfs_ino(entry)) |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 5591 | p = &parent->rb_left; |
David Sterba | 3750851 | 2018-06-29 10:56:40 +0200 | [diff] [blame] | 5592 | else if (ino > btrfs_ino(entry)) |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 5593 | p = &parent->rb_right; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5594 | else { |
| 5595 | WARN_ON(!(entry->vfs_inode.i_state & |
Al Viro | a4ffdde | 2010-06-02 17:38:30 -0400 | [diff] [blame] | 5596 | (I_WILL_FREE | I_FREEING))); |
Filipe David Borba Manana | cef2193 | 2013-09-02 12:19:13 +0100 | [diff] [blame] | 5597 | rb_replace_node(parent, new, &root->inode_tree); |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 5598 | RB_CLEAR_NODE(parent); |
| 5599 | spin_unlock(&root->inode_lock); |
Filipe David Borba Manana | cef2193 | 2013-09-02 12:19:13 +0100 | [diff] [blame] | 5600 | return; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5601 | } |
| 5602 | } |
Filipe David Borba Manana | cef2193 | 2013-09-02 12:19:13 +0100 | [diff] [blame] | 5603 | rb_link_node(new, parent, p); |
| 5604 | rb_insert_color(new, &root->inode_tree); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5605 | spin_unlock(&root->inode_lock); |
| 5606 | } |
| 5607 | |
| 5608 | static void inode_tree_del(struct inode *inode) |
| 5609 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5610 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5611 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5612 | int empty = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5613 | |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 5614 | spin_lock(&root->inode_lock); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5615 | if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5616 | rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5617 | RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5618 | empty = RB_EMPTY_ROOT(&root->inode_tree); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5619 | } |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 5620 | spin_unlock(&root->inode_lock); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5621 | |
Stefan Behrens | 69e9c6c | 2013-09-05 16:58:43 +0200 | [diff] [blame] | 5622 | if (empty && btrfs_root_refs(&root->root_item) == 0) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5623 | synchronize_srcu(&fs_info->subvol_srcu); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5624 | spin_lock(&root->inode_lock); |
| 5625 | empty = RB_EMPTY_ROOT(&root->inode_tree); |
| 5626 | spin_unlock(&root->inode_lock); |
| 5627 | if (empty) |
| 5628 | btrfs_add_dead_root(root); |
| 5629 | } |
| 5630 | } |
| 5631 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5632 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5633 | static int btrfs_init_locked_inode(struct inode *inode, void *p) |
| 5634 | { |
| 5635 | struct btrfs_iget_args *args = p; |
Chris Mason | 90d3e59 | 2014-01-09 17:28:00 -0800 | [diff] [blame] | 5636 | inode->i_ino = args->location->objectid; |
| 5637 | memcpy(&BTRFS_I(inode)->location, args->location, |
| 5638 | sizeof(*args->location)); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5639 | BTRFS_I(inode)->root = args->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5640 | return 0; |
| 5641 | } |
| 5642 | |
| 5643 | static int btrfs_find_actor(struct inode *inode, void *opaque) |
| 5644 | { |
| 5645 | struct btrfs_iget_args *args = opaque; |
Chris Mason | 90d3e59 | 2014-01-09 17:28:00 -0800 | [diff] [blame] | 5646 | return args->location->objectid == BTRFS_I(inode)->location.objectid && |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5647 | args->root == BTRFS_I(inode)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5648 | } |
| 5649 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5650 | static struct inode *btrfs_iget_locked(struct super_block *s, |
Chris Mason | 90d3e59 | 2014-01-09 17:28:00 -0800 | [diff] [blame] | 5651 | struct btrfs_key *location, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5652 | struct btrfs_root *root) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5653 | { |
| 5654 | struct inode *inode; |
| 5655 | struct btrfs_iget_args args; |
Chris Mason | 90d3e59 | 2014-01-09 17:28:00 -0800 | [diff] [blame] | 5656 | unsigned long hashval = btrfs_inode_hash(location->objectid, root); |
Filipe David Borba Manana | 778ba82 | 2013-10-06 22:22:33 +0100 | [diff] [blame] | 5657 | |
Chris Mason | 90d3e59 | 2014-01-09 17:28:00 -0800 | [diff] [blame] | 5658 | args.location = location; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5659 | args.root = root; |
| 5660 | |
Filipe David Borba Manana | 778ba82 | 2013-10-06 22:22:33 +0100 | [diff] [blame] | 5661 | inode = iget5_locked(s, hashval, btrfs_find_actor, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5662 | btrfs_init_locked_inode, |
| 5663 | (void *)&args); |
| 5664 | return inode; |
| 5665 | } |
| 5666 | |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 5667 | /* Get an inode object given its location and corresponding root. |
| 5668 | * Returns in *is_new if the inode was read from disk |
| 5669 | */ |
Filipe Manana | 4222ea7 | 2018-10-24 10:13:03 +0100 | [diff] [blame] | 5670 | struct inode *btrfs_iget_path(struct super_block *s, struct btrfs_key *location, |
| 5671 | struct btrfs_root *root, int *new, |
| 5672 | struct btrfs_path *path) |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 5673 | { |
| 5674 | struct inode *inode; |
| 5675 | |
Chris Mason | 90d3e59 | 2014-01-09 17:28:00 -0800 | [diff] [blame] | 5676 | inode = btrfs_iget_locked(s, location, root); |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 5677 | if (!inode) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5678 | return ERR_PTR(-ENOMEM); |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 5679 | |
| 5680 | if (inode->i_state & I_NEW) { |
Filipe Manana | 6771089 | 2016-06-06 11:51:25 +0100 | [diff] [blame] | 5681 | int ret; |
| 5682 | |
Filipe Manana | 4222ea7 | 2018-10-24 10:13:03 +0100 | [diff] [blame] | 5683 | ret = btrfs_read_locked_inode(inode, path); |
Al Viro | 9bc2cef | 2018-07-29 23:04:50 +0100 | [diff] [blame] | 5684 | if (!ret) { |
Mark Fasheh | 1748f84 | 2011-07-12 11:25:31 -0700 | [diff] [blame] | 5685 | inode_tree_add(inode); |
| 5686 | unlock_new_inode(inode); |
| 5687 | if (new) |
| 5688 | *new = 1; |
| 5689 | } else { |
Al Viro | f5b3a41 | 2018-07-29 23:04:51 +0100 | [diff] [blame] | 5690 | iget_failed(inode); |
| 5691 | /* |
| 5692 | * ret > 0 can come from btrfs_search_slot called by |
| 5693 | * btrfs_read_locked_inode, this means the inode item |
| 5694 | * was not found. |
| 5695 | */ |
| 5696 | if (ret > 0) |
| 5697 | ret = -ENOENT; |
| 5698 | inode = ERR_PTR(ret); |
Mark Fasheh | 1748f84 | 2011-07-12 11:25:31 -0700 | [diff] [blame] | 5699 | } |
| 5700 | } |
| 5701 | |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 5702 | return inode; |
| 5703 | } |
| 5704 | |
Filipe Manana | 4222ea7 | 2018-10-24 10:13:03 +0100 | [diff] [blame] | 5705 | struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location, |
| 5706 | struct btrfs_root *root, int *new) |
| 5707 | { |
| 5708 | return btrfs_iget_path(s, location, root, new, NULL); |
| 5709 | } |
| 5710 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5711 | static struct inode *new_simple_dir(struct super_block *s, |
| 5712 | struct btrfs_key *key, |
| 5713 | struct btrfs_root *root) |
| 5714 | { |
| 5715 | struct inode *inode = new_inode(s); |
| 5716 | |
| 5717 | if (!inode) |
| 5718 | return ERR_PTR(-ENOMEM); |
| 5719 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5720 | BTRFS_I(inode)->root = root; |
| 5721 | memcpy(&BTRFS_I(inode)->location, key, sizeof(*key)); |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 5722 | set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5723 | |
| 5724 | inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID; |
Li Zefan | 848cce0 | 2012-02-21 17:04:28 +0800 | [diff] [blame] | 5725 | inode->i_op = &btrfs_dir_ro_inode_operations; |
Omar Sandoval | 1fdf419 | 2017-01-25 17:06:39 -0800 | [diff] [blame] | 5726 | inode->i_opflags &= ~IOP_XATTR; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5727 | inode->i_fop = &simple_dir_operations; |
| 5728 | inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO; |
Deepa Dinamani | c2050a4 | 2016-09-14 07:48:06 -0700 | [diff] [blame] | 5729 | inode->i_mtime = current_time(inode); |
chandan r | 9cc97d6 | 2012-07-04 12:48:07 +0530 | [diff] [blame] | 5730 | inode->i_atime = inode->i_mtime; |
| 5731 | inode->i_ctime = inode->i_mtime; |
Arnd Bergmann | d3c6be6f | 2018-06-21 18:04:06 +0200 | [diff] [blame] | 5732 | BTRFS_I(inode)->i_otime = inode->i_mtime; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5733 | |
| 5734 | return inode; |
| 5735 | } |
| 5736 | |
Qu Wenruo | 6bf9e4b | 2019-03-13 13:55:11 +0800 | [diff] [blame] | 5737 | static inline u8 btrfs_inode_type(struct inode *inode) |
| 5738 | { |
| 5739 | /* |
| 5740 | * Compile-time asserts that generic FT_* types still match |
| 5741 | * BTRFS_FT_* types |
| 5742 | */ |
| 5743 | BUILD_BUG_ON(BTRFS_FT_UNKNOWN != FT_UNKNOWN); |
| 5744 | BUILD_BUG_ON(BTRFS_FT_REG_FILE != FT_REG_FILE); |
| 5745 | BUILD_BUG_ON(BTRFS_FT_DIR != FT_DIR); |
| 5746 | BUILD_BUG_ON(BTRFS_FT_CHRDEV != FT_CHRDEV); |
| 5747 | BUILD_BUG_ON(BTRFS_FT_BLKDEV != FT_BLKDEV); |
| 5748 | BUILD_BUG_ON(BTRFS_FT_FIFO != FT_FIFO); |
| 5749 | BUILD_BUG_ON(BTRFS_FT_SOCK != FT_SOCK); |
| 5750 | BUILD_BUG_ON(BTRFS_FT_SYMLINK != FT_SYMLINK); |
| 5751 | |
| 5752 | return fs_umode_to_ftype(inode->i_mode); |
| 5753 | } |
| 5754 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5755 | struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5756 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5757 | struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5758 | struct inode *inode; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5759 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5760 | struct btrfs_root *sub_root = root; |
| 5761 | struct btrfs_key location; |
Qu Wenruo | 6bf9e4b | 2019-03-13 13:55:11 +0800 | [diff] [blame] | 5762 | u8 di_type = 0; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5763 | int index; |
Josef Bacik | b4aff1f | 2011-06-28 16:18:59 -0400 | [diff] [blame] | 5764 | int ret = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5765 | |
| 5766 | if (dentry->d_name.len > BTRFS_NAME_LEN) |
| 5767 | return ERR_PTR(-ENAMETOOLONG); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5768 | |
Qu Wenruo | 6bf9e4b | 2019-03-13 13:55:11 +0800 | [diff] [blame] | 5769 | ret = btrfs_inode_by_name(dir, dentry, &location, &di_type); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5770 | if (ret < 0) |
| 5771 | return ERR_PTR(ret); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5772 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5773 | if (location.type == BTRFS_INODE_ITEM_KEY) { |
Josef Bacik | 73f7341 | 2009-12-04 17:38:27 +0000 | [diff] [blame] | 5774 | inode = btrfs_iget(dir->i_sb, &location, root, NULL); |
Qu Wenruo | 6bf9e4b | 2019-03-13 13:55:11 +0800 | [diff] [blame] | 5775 | if (IS_ERR(inode)) |
| 5776 | return inode; |
| 5777 | |
| 5778 | /* Do extra check against inode mode with di_type */ |
| 5779 | if (btrfs_inode_type(inode) != di_type) { |
| 5780 | btrfs_crit(fs_info, |
| 5781 | "inode mode mismatch with dir: inode mode=0%o btrfs type=%u dir type=%u", |
| 5782 | inode->i_mode, btrfs_inode_type(inode), |
| 5783 | di_type); |
| 5784 | iput(inode); |
| 5785 | return ERR_PTR(-EUCLEAN); |
| 5786 | } |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5787 | return inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5788 | } |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5789 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5790 | index = srcu_read_lock(&fs_info->subvol_srcu); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5791 | ret = fixup_tree_root_location(fs_info, dir, dentry, |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5792 | &location, &sub_root); |
| 5793 | if (ret < 0) { |
| 5794 | if (ret != -ENOENT) |
| 5795 | inode = ERR_PTR(ret); |
| 5796 | else |
| 5797 | inode = new_simple_dir(dir->i_sb, &location, sub_root); |
| 5798 | } else { |
Josef Bacik | 73f7341 | 2009-12-04 17:38:27 +0000 | [diff] [blame] | 5799 | inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5800 | } |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5801 | srcu_read_unlock(&fs_info->subvol_srcu, index); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5802 | |
Julia Lawall | 34d19ba | 2011-01-24 19:55:19 +0000 | [diff] [blame] | 5803 | if (!IS_ERR(inode) && root != sub_root) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5804 | down_read(&fs_info->cleanup_work_sem); |
David Howells | bc98a42 | 2017-07-17 08:45:34 +0100 | [diff] [blame] | 5805 | if (!sb_rdonly(inode->i_sb)) |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 5806 | ret = btrfs_orphan_cleanup(sub_root); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5807 | up_read(&fs_info->cleanup_work_sem); |
Josef Bacik | 01cd336 | 2013-06-03 21:39:49 -0400 | [diff] [blame] | 5808 | if (ret) { |
| 5809 | iput(inode); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 5810 | inode = ERR_PTR(ret); |
Josef Bacik | 01cd336 | 2013-06-03 21:39:49 -0400 | [diff] [blame] | 5811 | } |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 5812 | } |
| 5813 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5814 | return inode; |
| 5815 | } |
| 5816 | |
Nick Piggin | fe15ce4 | 2011-01-07 17:49:23 +1100 | [diff] [blame] | 5817 | static int btrfs_dentry_delete(const struct dentry *dentry) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5818 | { |
| 5819 | struct btrfs_root *root; |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 5820 | struct inode *inode = d_inode(dentry); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5821 | |
Li Zefan | 848cce0 | 2012-02-21 17:04:28 +0800 | [diff] [blame] | 5822 | if (!inode && !IS_ROOT(dentry)) |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 5823 | inode = d_inode(dentry->d_parent); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5824 | |
Li Zefan | 848cce0 | 2012-02-21 17:04:28 +0800 | [diff] [blame] | 5825 | if (inode) { |
| 5826 | root = BTRFS_I(inode)->root; |
Yan, Zheng | efefb14 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5827 | if (btrfs_root_refs(&root->root_item) == 0) |
| 5828 | return 1; |
Li Zefan | 848cce0 | 2012-02-21 17:04:28 +0800 | [diff] [blame] | 5829 | |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 5830 | if (btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) |
Li Zefan | 848cce0 | 2012-02-21 17:04:28 +0800 | [diff] [blame] | 5831 | return 1; |
Yan, Zheng | efefb14 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5832 | } |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5833 | return 0; |
| 5834 | } |
| 5835 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5836 | static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry, |
Al Viro | 00cd8dd | 2012-06-10 17:13:09 -0400 | [diff] [blame] | 5837 | unsigned int flags) |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5838 | { |
Al Viro | 3837d20 | 2018-10-10 16:38:27 -0400 | [diff] [blame] | 5839 | struct inode *inode = btrfs_lookup_dentry(dir, dentry); |
Josef Bacik | a66e7cc | 2011-09-18 10:34:03 -0400 | [diff] [blame] | 5840 | |
Al Viro | 3837d20 | 2018-10-10 16:38:27 -0400 | [diff] [blame] | 5841 | if (inode == ERR_PTR(-ENOENT)) |
| 5842 | inode = NULL; |
Al Viro | 41d28bc | 2014-10-12 22:24:21 -0400 | [diff] [blame] | 5843 | return d_splice_alias(inode, dentry); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5844 | } |
| 5845 | |
Josef Bacik | 23b5ec7 | 2017-07-24 15:14:25 -0400 | [diff] [blame] | 5846 | /* |
| 5847 | * All this infrastructure exists because dir_emit can fault, and we are holding |
| 5848 | * the tree lock when doing readdir. For now just allocate a buffer and copy |
| 5849 | * our information into that, and then dir_emit from the buffer. This is |
| 5850 | * similar to what NFS does, only we don't keep the buffer around in pagecache |
| 5851 | * because I'm afraid I'll mess that up. Long term we need to make filldir do |
| 5852 | * copy_to_user_inatomic so we don't have to worry about page faulting under the |
| 5853 | * tree lock. |
| 5854 | */ |
| 5855 | static int btrfs_opendir(struct inode *inode, struct file *file) |
| 5856 | { |
| 5857 | struct btrfs_file_private *private; |
| 5858 | |
| 5859 | private = kzalloc(sizeof(struct btrfs_file_private), GFP_KERNEL); |
| 5860 | if (!private) |
| 5861 | return -ENOMEM; |
| 5862 | private->filldir_buf = kzalloc(PAGE_SIZE, GFP_KERNEL); |
| 5863 | if (!private->filldir_buf) { |
| 5864 | kfree(private); |
| 5865 | return -ENOMEM; |
| 5866 | } |
| 5867 | file->private_data = private; |
| 5868 | return 0; |
| 5869 | } |
| 5870 | |
| 5871 | struct dir_entry { |
| 5872 | u64 ino; |
| 5873 | u64 offset; |
| 5874 | unsigned type; |
| 5875 | int name_len; |
| 5876 | }; |
| 5877 | |
| 5878 | static int btrfs_filldir(void *addr, int entries, struct dir_context *ctx) |
| 5879 | { |
| 5880 | while (entries--) { |
| 5881 | struct dir_entry *entry = addr; |
| 5882 | char *name = (char *)(entry + 1); |
| 5883 | |
David Sterba | 92d3217 | 2018-04-16 21:10:14 +0200 | [diff] [blame] | 5884 | ctx->pos = get_unaligned(&entry->offset); |
| 5885 | if (!dir_emit(ctx, name, get_unaligned(&entry->name_len), |
| 5886 | get_unaligned(&entry->ino), |
| 5887 | get_unaligned(&entry->type))) |
Josef Bacik | 23b5ec7 | 2017-07-24 15:14:25 -0400 | [diff] [blame] | 5888 | return 1; |
David Sterba | 92d3217 | 2018-04-16 21:10:14 +0200 | [diff] [blame] | 5889 | addr += sizeof(struct dir_entry) + |
| 5890 | get_unaligned(&entry->name_len); |
Josef Bacik | 23b5ec7 | 2017-07-24 15:14:25 -0400 | [diff] [blame] | 5891 | ctx->pos++; |
| 5892 | } |
| 5893 | return 0; |
| 5894 | } |
| 5895 | |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5896 | static int btrfs_real_readdir(struct file *file, struct dir_context *ctx) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5897 | { |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5898 | struct inode *inode = file_inode(file); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5899 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Josef Bacik | 23b5ec7 | 2017-07-24 15:14:25 -0400 | [diff] [blame] | 5900 | struct btrfs_file_private *private = file->private_data; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5901 | struct btrfs_dir_item *di; |
| 5902 | struct btrfs_key key; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5903 | struct btrfs_key found_key; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5904 | struct btrfs_path *path; |
Josef Bacik | 23b5ec7 | 2017-07-24 15:14:25 -0400 | [diff] [blame] | 5905 | void *addr; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5906 | struct list_head ins_list; |
| 5907 | struct list_head del_list; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5908 | int ret; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5909 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5910 | int slot; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5911 | char *name_ptr; |
| 5912 | int name_len; |
Josef Bacik | 23b5ec7 | 2017-07-24 15:14:25 -0400 | [diff] [blame] | 5913 | int entries = 0; |
| 5914 | int total_len = 0; |
Omar Sandoval | 02dbfc9 | 2016-05-20 13:50:33 -0700 | [diff] [blame] | 5915 | bool put = false; |
Jeff Mahoney | c2951f3 | 2016-11-21 15:59:04 +0100 | [diff] [blame] | 5916 | struct btrfs_key location; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5917 | |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5918 | if (!dir_emit_dots(file, ctx)) |
| 5919 | return 0; |
| 5920 | |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5921 | path = btrfs_alloc_path(); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5922 | if (!path) |
| 5923 | return -ENOMEM; |
Chris Mason | ff5714c | 2011-05-28 07:00:39 -0400 | [diff] [blame] | 5924 | |
Josef Bacik | 23b5ec7 | 2017-07-24 15:14:25 -0400 | [diff] [blame] | 5925 | addr = private->filldir_buf; |
David Sterba | e4058b5 | 2015-11-27 16:31:35 +0100 | [diff] [blame] | 5926 | path->reada = READA_FORWARD; |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5927 | |
Jeff Mahoney | c2951f3 | 2016-11-21 15:59:04 +0100 | [diff] [blame] | 5928 | INIT_LIST_HEAD(&ins_list); |
| 5929 | INIT_LIST_HEAD(&del_list); |
| 5930 | put = btrfs_readdir_get_delayed_items(inode, &ins_list, &del_list); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5931 | |
Josef Bacik | 23b5ec7 | 2017-07-24 15:14:25 -0400 | [diff] [blame] | 5932 | again: |
Jeff Mahoney | c2951f3 | 2016-11-21 15:59:04 +0100 | [diff] [blame] | 5933 | key.type = BTRFS_DIR_INDEX_KEY; |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5934 | key.offset = ctx->pos; |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 5935 | key.objectid = btrfs_ino(BTRFS_I(inode)); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5936 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5937 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 5938 | if (ret < 0) |
| 5939 | goto err; |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5940 | |
| 5941 | while (1) { |
Josef Bacik | 23b5ec7 | 2017-07-24 15:14:25 -0400 | [diff] [blame] | 5942 | struct dir_entry *entry; |
| 5943 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5944 | leaf = path->nodes[0]; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5945 | slot = path->slots[0]; |
Li Zefan | b9e03af | 2011-03-23 10:43:58 +0800 | [diff] [blame] | 5946 | if (slot >= btrfs_header_nritems(leaf)) { |
| 5947 | ret = btrfs_next_leaf(root, path); |
| 5948 | if (ret < 0) |
| 5949 | goto err; |
| 5950 | else if (ret > 0) |
| 5951 | break; |
| 5952 | continue; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5953 | } |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5954 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5955 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
| 5956 | |
| 5957 | if (found_key.objectid != key.objectid) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5958 | break; |
Jeff Mahoney | c2951f3 | 2016-11-21 15:59:04 +0100 | [diff] [blame] | 5959 | if (found_key.type != BTRFS_DIR_INDEX_KEY) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5960 | break; |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5961 | if (found_key.offset < ctx->pos) |
Li Zefan | b9e03af | 2011-03-23 10:43:58 +0800 | [diff] [blame] | 5962 | goto next; |
Jeff Mahoney | c2951f3 | 2016-11-21 15:59:04 +0100 | [diff] [blame] | 5963 | if (btrfs_should_delete_dir_index(&del_list, found_key.offset)) |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5964 | goto next; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5965 | di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item); |
Jeff Mahoney | c2951f3 | 2016-11-21 15:59:04 +0100 | [diff] [blame] | 5966 | name_len = btrfs_dir_name_len(leaf, di); |
Josef Bacik | 23b5ec7 | 2017-07-24 15:14:25 -0400 | [diff] [blame] | 5967 | if ((total_len + sizeof(struct dir_entry) + name_len) >= |
| 5968 | PAGE_SIZE) { |
| 5969 | btrfs_release_path(path); |
| 5970 | ret = btrfs_filldir(private->filldir_buf, entries, ctx); |
| 5971 | if (ret) |
| 5972 | goto nopos; |
| 5973 | addr = private->filldir_buf; |
| 5974 | entries = 0; |
| 5975 | total_len = 0; |
| 5976 | goto again; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5977 | } |
Josef Bacik | 23b5ec7 | 2017-07-24 15:14:25 -0400 | [diff] [blame] | 5978 | |
| 5979 | entry = addr; |
David Sterba | 92d3217 | 2018-04-16 21:10:14 +0200 | [diff] [blame] | 5980 | put_unaligned(name_len, &entry->name_len); |
Josef Bacik | 23b5ec7 | 2017-07-24 15:14:25 -0400 | [diff] [blame] | 5981 | name_ptr = (char *)(entry + 1); |
Jeff Mahoney | c2951f3 | 2016-11-21 15:59:04 +0100 | [diff] [blame] | 5982 | read_extent_buffer(leaf, name_ptr, (unsigned long)(di + 1), |
| 5983 | name_len); |
Phillip Potter | 7d157c3 | 2019-03-26 21:39:34 +0000 | [diff] [blame] | 5984 | put_unaligned(fs_ftype_to_dtype(btrfs_dir_type(leaf, di)), |
David Sterba | 92d3217 | 2018-04-16 21:10:14 +0200 | [diff] [blame] | 5985 | &entry->type); |
Jeff Mahoney | c2951f3 | 2016-11-21 15:59:04 +0100 | [diff] [blame] | 5986 | btrfs_dir_item_key_to_cpu(leaf, di, &location); |
David Sterba | 92d3217 | 2018-04-16 21:10:14 +0200 | [diff] [blame] | 5987 | put_unaligned(location.objectid, &entry->ino); |
| 5988 | put_unaligned(found_key.offset, &entry->offset); |
Josef Bacik | 23b5ec7 | 2017-07-24 15:14:25 -0400 | [diff] [blame] | 5989 | entries++; |
| 5990 | addr += sizeof(struct dir_entry) + name_len; |
| 5991 | total_len += sizeof(struct dir_entry) + name_len; |
Li Zefan | b9e03af | 2011-03-23 10:43:58 +0800 | [diff] [blame] | 5992 | next: |
| 5993 | path->slots[0]++; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5994 | } |
Josef Bacik | 23b5ec7 | 2017-07-24 15:14:25 -0400 | [diff] [blame] | 5995 | btrfs_release_path(path); |
| 5996 | |
| 5997 | ret = btrfs_filldir(private->filldir_buf, entries, ctx); |
| 5998 | if (ret) |
| 5999 | goto nopos; |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 6000 | |
Jeff Mahoney | d2fbb2b | 2016-11-05 13:26:35 -0400 | [diff] [blame] | 6001 | ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list); |
Jeff Mahoney | c2951f3 | 2016-11-21 15:59:04 +0100 | [diff] [blame] | 6002 | if (ret) |
David Sterba | bc4ef75 | 2015-11-13 13:44:28 +0100 | [diff] [blame] | 6003 | goto nopos; |
| 6004 | |
Zach Brown | db62efb | 2013-07-11 16:19:42 -0700 | [diff] [blame] | 6005 | /* |
| 6006 | * Stop new entries from being returned after we return the last |
| 6007 | * entry. |
| 6008 | * |
| 6009 | * New directory entries are assigned a strictly increasing |
| 6010 | * offset. This means that new entries created during readdir |
| 6011 | * are *guaranteed* to be seen in the future by that readdir. |
| 6012 | * This has broken buggy programs which operate on names as |
| 6013 | * they're returned by readdir. Until we re-use freed offsets |
| 6014 | * we have this hack to stop new entries from being returned |
| 6015 | * under the assumption that they'll never reach this huge |
| 6016 | * offset. |
| 6017 | * |
| 6018 | * This is being careful not to overflow 32bit loff_t unless the |
| 6019 | * last entry requires it because doing so has broken 32bit apps |
| 6020 | * in the past. |
| 6021 | */ |
Jeff Mahoney | c2951f3 | 2016-11-21 15:59:04 +0100 | [diff] [blame] | 6022 | if (ctx->pos >= INT_MAX) |
| 6023 | ctx->pos = LLONG_MAX; |
| 6024 | else |
| 6025 | ctx->pos = INT_MAX; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6026 | nopos: |
| 6027 | ret = 0; |
| 6028 | err: |
Omar Sandoval | 02dbfc9 | 2016-05-20 13:50:33 -0700 | [diff] [blame] | 6029 | if (put) |
| 6030 | btrfs_readdir_put_delayed_items(inode, &ins_list, &del_list); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6031 | btrfs_free_path(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6032 | return ret; |
| 6033 | } |
| 6034 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6035 | /* |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 6036 | * This is somewhat expensive, updating the tree every time the |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6037 | * inode changes. But, it is most likely to find the inode in cache. |
| 6038 | * FIXME, needs more benchmarking...there are no reasons other than performance |
| 6039 | * to keep or drop this code. |
| 6040 | */ |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 6041 | static int btrfs_dirty_inode(struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6042 | { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6043 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6044 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 6045 | struct btrfs_trans_handle *trans; |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 6046 | int ret; |
| 6047 | |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 6048 | if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags)) |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 6049 | return 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6050 | |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 6051 | trans = btrfs_join_transaction(root); |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 6052 | if (IS_ERR(trans)) |
| 6053 | return PTR_ERR(trans); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 6054 | |
| 6055 | ret = btrfs_update_inode(trans, root, inode); |
Chris Mason | 94b6044 | 2010-05-26 11:02:00 -0400 | [diff] [blame] | 6056 | if (ret && ret == -ENOSPC) { |
| 6057 | /* whoops, lets try again with the full transaction */ |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 6058 | btrfs_end_transaction(trans); |
Chris Mason | 94b6044 | 2010-05-26 11:02:00 -0400 | [diff] [blame] | 6059 | trans = btrfs_start_transaction(root, 1); |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 6060 | if (IS_ERR(trans)) |
| 6061 | return PTR_ERR(trans); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 6062 | |
Chris Mason | 94b6044 | 2010-05-26 11:02:00 -0400 | [diff] [blame] | 6063 | ret = btrfs_update_inode(trans, root, inode); |
Chris Mason | 94b6044 | 2010-05-26 11:02:00 -0400 | [diff] [blame] | 6064 | } |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 6065 | btrfs_end_transaction(trans); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 6066 | if (BTRFS_I(inode)->delayed_node) |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6067 | btrfs_balance_delayed_items(fs_info); |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 6068 | |
| 6069 | return ret; |
| 6070 | } |
| 6071 | |
| 6072 | /* |
| 6073 | * This is a copy of file_update_time. We need this so we can return error on |
| 6074 | * ENOSPC for updating the inode in the case of file write and mmap writes. |
| 6075 | */ |
Deepa Dinamani | 95582b0 | 2018-05-08 19:36:02 -0700 | [diff] [blame] | 6076 | static int btrfs_update_time(struct inode *inode, struct timespec64 *now, |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 6077 | int flags) |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 6078 | { |
Alexander Block | 2bc556528 | 2012-06-15 09:49:33 +0200 | [diff] [blame] | 6079 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Jeff Layton | 3a8c723 | 2017-12-11 06:35:24 -0500 | [diff] [blame] | 6080 | bool dirty = flags & ~S_VERSION; |
Alexander Block | 2bc556528 | 2012-06-15 09:49:33 +0200 | [diff] [blame] | 6081 | |
| 6082 | if (btrfs_root_readonly(root)) |
| 6083 | return -EROFS; |
| 6084 | |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 6085 | if (flags & S_VERSION) |
Jeff Layton | 3a8c723 | 2017-12-11 06:35:24 -0500 | [diff] [blame] | 6086 | dirty |= inode_maybe_inc_iversion(inode, dirty); |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 6087 | if (flags & S_CTIME) |
| 6088 | inode->i_ctime = *now; |
| 6089 | if (flags & S_MTIME) |
| 6090 | inode->i_mtime = *now; |
| 6091 | if (flags & S_ATIME) |
| 6092 | inode->i_atime = *now; |
Jeff Layton | 3a8c723 | 2017-12-11 06:35:24 -0500 | [diff] [blame] | 6093 | return dirty ? btrfs_dirty_inode(inode) : 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6094 | } |
| 6095 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 6096 | /* |
| 6097 | * find the highest existing sequence number in a directory |
| 6098 | * and then set the in-memory index_cnt variable to reflect |
| 6099 | * free sequence numbers |
| 6100 | */ |
Nikolay Borisov | 4c570655 | 2017-02-20 13:50:32 +0200 | [diff] [blame] | 6101 | static int btrfs_set_inode_index_count(struct btrfs_inode *inode) |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6102 | { |
Nikolay Borisov | 4c570655 | 2017-02-20 13:50:32 +0200 | [diff] [blame] | 6103 | struct btrfs_root *root = inode->root; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6104 | struct btrfs_key key, found_key; |
| 6105 | struct btrfs_path *path; |
| 6106 | struct extent_buffer *leaf; |
| 6107 | int ret; |
| 6108 | |
Nikolay Borisov | 4c570655 | 2017-02-20 13:50:32 +0200 | [diff] [blame] | 6109 | key.objectid = btrfs_ino(inode); |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 6110 | key.type = BTRFS_DIR_INDEX_KEY; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6111 | key.offset = (u64)-1; |
| 6112 | |
| 6113 | path = btrfs_alloc_path(); |
| 6114 | if (!path) |
| 6115 | return -ENOMEM; |
| 6116 | |
| 6117 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 6118 | if (ret < 0) |
| 6119 | goto out; |
| 6120 | /* FIXME: we should be able to handle this */ |
| 6121 | if (ret == 0) |
| 6122 | goto out; |
| 6123 | ret = 0; |
| 6124 | |
| 6125 | /* |
| 6126 | * MAGIC NUMBER EXPLANATION: |
| 6127 | * since we search a directory based on f_pos we have to start at 2 |
| 6128 | * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody |
| 6129 | * else has to start at 2 |
| 6130 | */ |
| 6131 | if (path->slots[0] == 0) { |
Nikolay Borisov | 4c570655 | 2017-02-20 13:50:32 +0200 | [diff] [blame] | 6132 | inode->index_cnt = 2; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6133 | goto out; |
| 6134 | } |
| 6135 | |
| 6136 | path->slots[0]--; |
| 6137 | |
| 6138 | leaf = path->nodes[0]; |
| 6139 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 6140 | |
Nikolay Borisov | 4c570655 | 2017-02-20 13:50:32 +0200 | [diff] [blame] | 6141 | if (found_key.objectid != btrfs_ino(inode) || |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 6142 | found_key.type != BTRFS_DIR_INDEX_KEY) { |
Nikolay Borisov | 4c570655 | 2017-02-20 13:50:32 +0200 | [diff] [blame] | 6143 | inode->index_cnt = 2; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6144 | goto out; |
| 6145 | } |
| 6146 | |
Nikolay Borisov | 4c570655 | 2017-02-20 13:50:32 +0200 | [diff] [blame] | 6147 | inode->index_cnt = found_key.offset + 1; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6148 | out: |
| 6149 | btrfs_free_path(path); |
| 6150 | return ret; |
| 6151 | } |
| 6152 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 6153 | /* |
| 6154 | * helper to find a free sequence number in a given directory. This current |
| 6155 | * code is very simple, later versions will do smarter things in the btree |
| 6156 | */ |
Nikolay Borisov | 877574e | 2017-02-20 13:50:33 +0200 | [diff] [blame] | 6157 | int btrfs_set_inode_index(struct btrfs_inode *dir, u64 *index) |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6158 | { |
| 6159 | int ret = 0; |
| 6160 | |
Nikolay Borisov | 877574e | 2017-02-20 13:50:33 +0200 | [diff] [blame] | 6161 | if (dir->index_cnt == (u64)-1) { |
| 6162 | ret = btrfs_inode_delayed_dir_index_count(dir); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 6163 | if (ret) { |
| 6164 | ret = btrfs_set_inode_index_count(dir); |
| 6165 | if (ret) |
| 6166 | return ret; |
| 6167 | } |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6168 | } |
| 6169 | |
Nikolay Borisov | 877574e | 2017-02-20 13:50:33 +0200 | [diff] [blame] | 6170 | *index = dir->index_cnt; |
| 6171 | dir->index_cnt++; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6172 | |
| 6173 | return ret; |
| 6174 | } |
| 6175 | |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6176 | static int btrfs_insert_inode_locked(struct inode *inode) |
| 6177 | { |
| 6178 | struct btrfs_iget_args args; |
| 6179 | args.location = &BTRFS_I(inode)->location; |
| 6180 | args.root = BTRFS_I(inode)->root; |
| 6181 | |
| 6182 | return insert_inode_locked4(inode, |
| 6183 | btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root), |
| 6184 | btrfs_find_actor, &args); |
| 6185 | } |
| 6186 | |
Anand Jain | 19aee8d | 2017-07-18 17:37:05 +0800 | [diff] [blame] | 6187 | /* |
| 6188 | * Inherit flags from the parent inode. |
| 6189 | * |
| 6190 | * Currently only the compression flags and the cow flags are inherited. |
| 6191 | */ |
| 6192 | static void btrfs_inherit_iflags(struct inode *inode, struct inode *dir) |
| 6193 | { |
| 6194 | unsigned int flags; |
| 6195 | |
| 6196 | if (!dir) |
| 6197 | return; |
| 6198 | |
| 6199 | flags = BTRFS_I(dir)->flags; |
| 6200 | |
| 6201 | if (flags & BTRFS_INODE_NOCOMPRESS) { |
| 6202 | BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS; |
| 6203 | BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS; |
| 6204 | } else if (flags & BTRFS_INODE_COMPRESS) { |
| 6205 | BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS; |
| 6206 | BTRFS_I(inode)->flags |= BTRFS_INODE_COMPRESS; |
| 6207 | } |
| 6208 | |
| 6209 | if (flags & BTRFS_INODE_NODATACOW) { |
| 6210 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW; |
| 6211 | if (S_ISREG(inode->i_mode)) |
| 6212 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM; |
| 6213 | } |
| 6214 | |
David Sterba | 7b6a221 | 2018-03-26 18:40:21 +0200 | [diff] [blame] | 6215 | btrfs_sync_inode_flags_to_i_flags(inode); |
Anand Jain | 19aee8d | 2017-07-18 17:37:05 +0800 | [diff] [blame] | 6216 | } |
| 6217 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6218 | static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans, |
| 6219 | struct btrfs_root *root, |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6220 | struct inode *dir, |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 6221 | const char *name, int name_len, |
Al Viro | 175a4eb | 2011-07-26 03:30:54 -0400 | [diff] [blame] | 6222 | u64 ref_objectid, u64 objectid, |
| 6223 | umode_t mode, u64 *index) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6224 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6225 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6226 | struct inode *inode; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6227 | struct btrfs_inode_item *inode_item; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6228 | struct btrfs_key *location; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6229 | struct btrfs_path *path; |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 6230 | struct btrfs_inode_ref *ref; |
| 6231 | struct btrfs_key key[2]; |
| 6232 | u32 sizes[2]; |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 6233 | int nitems = name ? 2 : 1; |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 6234 | unsigned long ptr; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6235 | int ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6236 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6237 | path = btrfs_alloc_path(); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 6238 | if (!path) |
| 6239 | return ERR_PTR(-ENOMEM); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6240 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6241 | inode = new_inode(fs_info->sb); |
Yoshinori Sano | 8fb2764 | 2011-04-09 02:30:07 +0000 | [diff] [blame] | 6242 | if (!inode) { |
| 6243 | btrfs_free_path(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6244 | return ERR_PTR(-ENOMEM); |
Yoshinori Sano | 8fb2764 | 2011-04-09 02:30:07 +0000 | [diff] [blame] | 6245 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6246 | |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 6247 | /* |
Filipe Manana | 5762b5c | 2014-08-01 00:10:32 +0100 | [diff] [blame] | 6248 | * O_TMPFILE, set link count to 0, so that after this point, |
| 6249 | * we fill in an inode item with the correct link count. |
| 6250 | */ |
| 6251 | if (!name) |
| 6252 | set_nlink(inode, 0); |
| 6253 | |
| 6254 | /* |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 6255 | * we have to initialize this early, so we can reclaim the inode |
| 6256 | * number if we fail afterwards in this function. |
| 6257 | */ |
| 6258 | inode->i_ino = objectid; |
| 6259 | |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 6260 | if (dir && name) { |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 6261 | trace_btrfs_inode_request(dir); |
| 6262 | |
Nikolay Borisov | 877574e | 2017-02-20 13:50:33 +0200 | [diff] [blame] | 6263 | ret = btrfs_set_inode_index(BTRFS_I(dir), index); |
Shen Feng | 0977143 | 2009-04-02 16:46:06 -0400 | [diff] [blame] | 6264 | if (ret) { |
Yoshinori Sano | 8fb2764 | 2011-04-09 02:30:07 +0000 | [diff] [blame] | 6265 | btrfs_free_path(path); |
Shen Feng | 0977143 | 2009-04-02 16:46:06 -0400 | [diff] [blame] | 6266 | iput(inode); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6267 | return ERR_PTR(ret); |
Shen Feng | 0977143 | 2009-04-02 16:46:06 -0400 | [diff] [blame] | 6268 | } |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 6269 | } else if (dir) { |
| 6270 | *index = 0; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6271 | } |
| 6272 | /* |
| 6273 | * index_cnt is ignored for everything but a dir, |
Su Yue | df6703e | 2018-01-12 11:08:02 +0800 | [diff] [blame] | 6274 | * btrfs_set_inode_index_count has an explanation for the magic |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6275 | * number |
| 6276 | */ |
| 6277 | BTRFS_I(inode)->index_cnt = 2; |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 6278 | BTRFS_I(inode)->dir_index = *index; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6279 | BTRFS_I(inode)->root = root; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 6280 | BTRFS_I(inode)->generation = trans->transid; |
Josef Bacik | 7619585 | 2010-11-19 02:18:02 +0000 | [diff] [blame] | 6281 | inode->i_generation = BTRFS_I(inode)->generation; |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6282 | |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 6283 | /* |
| 6284 | * We could have gotten an inode number from somebody who was fsynced |
| 6285 | * and then removed in this same transaction, so let's just set full |
| 6286 | * sync since it will be a full sync anyway and this will blow away the |
| 6287 | * old info in the log. |
| 6288 | */ |
| 6289 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags); |
| 6290 | |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 6291 | key[0].objectid = objectid; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 6292 | key[0].type = BTRFS_INODE_ITEM_KEY; |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 6293 | key[0].offset = 0; |
| 6294 | |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 6295 | sizes[0] = sizeof(struct btrfs_inode_item); |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 6296 | |
| 6297 | if (name) { |
| 6298 | /* |
| 6299 | * Start new inodes with an inode_ref. This is slightly more |
| 6300 | * efficient for small numbers of hard links since they will |
| 6301 | * be packed into one item. Extended refs will kick in if we |
| 6302 | * add more hard links than can fit in the ref item. |
| 6303 | */ |
| 6304 | key[1].objectid = objectid; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 6305 | key[1].type = BTRFS_INODE_REF_KEY; |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 6306 | key[1].offset = ref_objectid; |
| 6307 | |
| 6308 | sizes[1] = name_len + sizeof(*ref); |
| 6309 | } |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 6310 | |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6311 | location = &BTRFS_I(inode)->location; |
| 6312 | location->objectid = objectid; |
| 6313 | location->offset = 0; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 6314 | location->type = BTRFS_INODE_ITEM_KEY; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6315 | |
| 6316 | ret = btrfs_insert_inode_locked(inode); |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6317 | if (ret < 0) { |
| 6318 | iput(inode); |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6319 | goto fail; |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6320 | } |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6321 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 6322 | path->leave_spinning = 1; |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 6323 | ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems); |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 6324 | if (ret != 0) |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6325 | goto fail_unlock; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6326 | |
Dmitry Monakhov | ecc11fab | 2010-03-04 17:31:47 +0300 | [diff] [blame] | 6327 | inode_init_owner(inode, dir, mode); |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 6328 | inode_set_bytes(inode, 0); |
chandan r | 9cc97d6 | 2012-07-04 12:48:07 +0530 | [diff] [blame] | 6329 | |
Deepa Dinamani | c2050a4 | 2016-09-14 07:48:06 -0700 | [diff] [blame] | 6330 | inode->i_mtime = current_time(inode); |
chandan r | 9cc97d6 | 2012-07-04 12:48:07 +0530 | [diff] [blame] | 6331 | inode->i_atime = inode->i_mtime; |
| 6332 | inode->i_ctime = inode->i_mtime; |
Arnd Bergmann | d3c6be6f | 2018-06-21 18:04:06 +0200 | [diff] [blame] | 6333 | BTRFS_I(inode)->i_otime = inode->i_mtime; |
chandan r | 9cc97d6 | 2012-07-04 12:48:07 +0530 | [diff] [blame] | 6334 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6335 | inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 6336 | struct btrfs_inode_item); |
David Sterba | b159fa2 | 2016-11-08 18:09:03 +0100 | [diff] [blame] | 6337 | memzero_extent_buffer(path->nodes[0], (unsigned long)inode_item, |
Li Zefan | 293f7e0 | 2012-07-10 00:58:58 -0600 | [diff] [blame] | 6338 | sizeof(*inode_item)); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 6339 | fill_inode_item(trans, path->nodes[0], inode_item, inode); |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 6340 | |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 6341 | if (name) { |
| 6342 | ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1, |
| 6343 | struct btrfs_inode_ref); |
| 6344 | btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len); |
| 6345 | btrfs_set_inode_ref_index(path->nodes[0], ref, *index); |
| 6346 | ptr = (unsigned long)(ref + 1); |
| 6347 | write_extent_buffer(path->nodes[0], name, ptr, name_len); |
| 6348 | } |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 6349 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6350 | btrfs_mark_buffer_dirty(path->nodes[0]); |
| 6351 | btrfs_free_path(path); |
| 6352 | |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 6353 | btrfs_inherit_iflags(inode, dir); |
| 6354 | |
Al Viro | 569254b | 2011-07-24 17:08:40 -0400 | [diff] [blame] | 6355 | if (S_ISREG(mode)) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6356 | if (btrfs_test_opt(fs_info, NODATASUM)) |
Chris Mason | 9427216 | 2009-07-02 12:26:06 -0400 | [diff] [blame] | 6357 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6358 | if (btrfs_test_opt(fs_info, NODATACOW)) |
Josef Bacik | f2bdf9a | 2013-02-21 15:28:28 -0500 | [diff] [blame] | 6359 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW | |
| 6360 | BTRFS_INODE_NODATASUM; |
Chris Mason | 9427216 | 2009-07-02 12:26:06 -0400 | [diff] [blame] | 6361 | } |
| 6362 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6363 | inode_tree_add(inode); |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 6364 | |
| 6365 | trace_btrfs_inode_new(inode); |
Chris Mason | 1973f0f | 2011-06-24 13:13:29 -0400 | [diff] [blame] | 6366 | btrfs_set_inode_last_trans(trans, inode); |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 6367 | |
Alexander Block | 8ea05e3 | 2012-07-25 17:35:53 +0200 | [diff] [blame] | 6368 | btrfs_update_root_times(trans, root); |
| 6369 | |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 6370 | ret = btrfs_inode_inherit_props(trans, inode, dir); |
| 6371 | if (ret) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6372 | btrfs_err(fs_info, |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 6373 | "error inheriting props for ino %llu (root %llu): %d", |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 6374 | btrfs_ino(BTRFS_I(inode)), root->root_key.objectid, ret); |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 6375 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6376 | return inode; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6377 | |
| 6378 | fail_unlock: |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6379 | discard_new_inode(inode); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6380 | fail: |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 6381 | if (dir && name) |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6382 | BTRFS_I(dir)->index_cnt--; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6383 | btrfs_free_path(path); |
| 6384 | return ERR_PTR(ret); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6385 | } |
| 6386 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 6387 | /* |
| 6388 | * utility function to add 'inode' into 'parent_inode' with |
| 6389 | * a give name and a given sequence number. |
| 6390 | * if 'add_backref' is true, also insert a backref from the |
| 6391 | * inode to the parent directory. |
| 6392 | */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 6393 | int btrfs_add_link(struct btrfs_trans_handle *trans, |
Nikolay Borisov | db0a669 | 2017-02-20 13:51:08 +0200 | [diff] [blame] | 6394 | struct btrfs_inode *parent_inode, struct btrfs_inode *inode, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 6395 | const char *name, int name_len, int add_backref, u64 index) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6396 | { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6397 | int ret = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6398 | struct btrfs_key key; |
Nikolay Borisov | db0a669 | 2017-02-20 13:51:08 +0200 | [diff] [blame] | 6399 | struct btrfs_root *root = parent_inode->root; |
| 6400 | u64 ino = btrfs_ino(inode); |
| 6401 | u64 parent_ino = btrfs_ino(parent_inode); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6402 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6403 | if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) { |
Nikolay Borisov | db0a669 | 2017-02-20 13:51:08 +0200 | [diff] [blame] | 6404 | memcpy(&key, &inode->root->root_key, sizeof(key)); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6405 | } else { |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6406 | key.objectid = ino; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 6407 | key.type = BTRFS_INODE_ITEM_KEY; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6408 | key.offset = 0; |
| 6409 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6410 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6411 | if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) { |
Lu Fengqi | 6025c19 | 2018-08-01 11:32:29 +0800 | [diff] [blame] | 6412 | ret = btrfs_add_root_ref(trans, key.objectid, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6413 | root->root_key.objectid, parent_ino, |
| 6414 | index, name, name_len); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6415 | } else if (add_backref) { |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6416 | ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino, |
| 6417 | parent_ino, index); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6418 | } |
| 6419 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6420 | /* Nothing to clean up yet */ |
| 6421 | if (ret) |
| 6422 | return ret; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6423 | |
Lu Fengqi | 684572d | 2018-08-04 21:10:57 +0800 | [diff] [blame] | 6424 | ret = btrfs_insert_dir_item(trans, name, name_len, parent_inode, &key, |
Nikolay Borisov | db0a669 | 2017-02-20 13:51:08 +0200 | [diff] [blame] | 6425 | btrfs_inode_type(&inode->vfs_inode), index); |
Chris Mason | 9c52057 | 2012-12-17 14:26:57 -0500 | [diff] [blame] | 6426 | if (ret == -EEXIST || ret == -EOVERFLOW) |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6427 | goto fail_dir_item; |
| 6428 | else if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 6429 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6430 | return ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6431 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6432 | |
Nikolay Borisov | db0a669 | 2017-02-20 13:51:08 +0200 | [diff] [blame] | 6433 | btrfs_i_size_write(parent_inode, parent_inode->vfs_inode.i_size + |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6434 | name_len * 2); |
Nikolay Borisov | db0a669 | 2017-02-20 13:51:08 +0200 | [diff] [blame] | 6435 | inode_inc_iversion(&parent_inode->vfs_inode); |
Filipe Manana | 5338e43 | 2019-05-15 16:02:47 +0100 | [diff] [blame^] | 6436 | /* |
| 6437 | * If we are replaying a log tree, we do not want to update the mtime |
| 6438 | * and ctime of the parent directory with the current time, since the |
| 6439 | * log replay procedure is responsible for setting them to their correct |
| 6440 | * values (the ones it had when the fsync was done). |
| 6441 | */ |
| 6442 | if (!test_bit(BTRFS_FS_LOG_RECOVERING, &root->fs_info->flags)) { |
| 6443 | struct timespec64 now = current_time(&parent_inode->vfs_inode); |
| 6444 | |
| 6445 | parent_inode->vfs_inode.i_mtime = now; |
| 6446 | parent_inode->vfs_inode.i_ctime = now; |
| 6447 | } |
Nikolay Borisov | db0a669 | 2017-02-20 13:51:08 +0200 | [diff] [blame] | 6448 | ret = btrfs_update_inode(trans, root, &parent_inode->vfs_inode); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6449 | if (ret) |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 6450 | btrfs_abort_transaction(trans, ret); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6451 | return ret; |
Chris Mason | fe66a05 | 2012-02-20 08:40:56 -0500 | [diff] [blame] | 6452 | |
| 6453 | fail_dir_item: |
| 6454 | if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) { |
| 6455 | u64 local_index; |
| 6456 | int err; |
Lu Fengqi | 3ee1c55 | 2018-08-01 11:32:28 +0800 | [diff] [blame] | 6457 | err = btrfs_del_root_ref(trans, key.objectid, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6458 | root->root_key.objectid, parent_ino, |
| 6459 | &local_index, name, name_len); |
Johannes Thumshirn | 1690dd41 | 2018-12-12 15:14:17 +0100 | [diff] [blame] | 6460 | if (err) |
| 6461 | btrfs_abort_transaction(trans, err); |
Chris Mason | fe66a05 | 2012-02-20 08:40:56 -0500 | [diff] [blame] | 6462 | } else if (add_backref) { |
| 6463 | u64 local_index; |
| 6464 | int err; |
| 6465 | |
| 6466 | err = btrfs_del_inode_ref(trans, root, name, name_len, |
| 6467 | ino, parent_ino, &local_index); |
Johannes Thumshirn | 1690dd41 | 2018-12-12 15:14:17 +0100 | [diff] [blame] | 6468 | if (err) |
| 6469 | btrfs_abort_transaction(trans, err); |
Chris Mason | fe66a05 | 2012-02-20 08:40:56 -0500 | [diff] [blame] | 6470 | } |
Johannes Thumshirn | 1690dd41 | 2018-12-12 15:14:17 +0100 | [diff] [blame] | 6471 | |
| 6472 | /* Return the original error code */ |
Chris Mason | fe66a05 | 2012-02-20 08:40:56 -0500 | [diff] [blame] | 6473 | return ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6474 | } |
| 6475 | |
| 6476 | static int btrfs_add_nondir(struct btrfs_trans_handle *trans, |
Nikolay Borisov | cef415a | 2017-02-20 13:51:09 +0200 | [diff] [blame] | 6477 | struct btrfs_inode *dir, struct dentry *dentry, |
| 6478 | struct btrfs_inode *inode, int backref, u64 index) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6479 | { |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 6480 | int err = btrfs_add_link(trans, dir, inode, |
| 6481 | dentry->d_name.name, dentry->d_name.len, |
| 6482 | backref, index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6483 | if (err > 0) |
| 6484 | err = -EEXIST; |
| 6485 | return err; |
| 6486 | } |
| 6487 | |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6488 | static int btrfs_mknod(struct inode *dir, struct dentry *dentry, |
Al Viro | 1a67aaf | 2011-07-26 01:52:52 -0400 | [diff] [blame] | 6489 | umode_t mode, dev_t rdev) |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6490 | { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6491 | struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6492 | struct btrfs_trans_handle *trans; |
| 6493 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 6494 | struct inode *inode = NULL; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6495 | int err; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6496 | u64 objectid; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 6497 | u64 index = 0; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6498 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 6499 | /* |
| 6500 | * 2 for inode item and ref |
| 6501 | * 2 for dir items |
| 6502 | * 1 for xattr if selinux is on |
| 6503 | */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6504 | trans = btrfs_start_transaction(root, 5); |
| 6505 | if (IS_ERR(trans)) |
| 6506 | return PTR_ERR(trans); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 6507 | |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 6508 | err = btrfs_find_free_ino(root, &objectid); |
| 6509 | if (err) |
| 6510 | goto out_unlock; |
| 6511 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6512 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 6513 | dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid, |
| 6514 | mode, &index); |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 6515 | if (IS_ERR(inode)) { |
| 6516 | err = PTR_ERR(inode); |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6517 | inode = NULL; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6518 | goto out_unlock; |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 6519 | } |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6520 | |
Casey Schaufler | ad19db7 | 2011-12-15 10:09:07 -0500 | [diff] [blame] | 6521 | /* |
| 6522 | * If the active LSM wants to access the inode during |
| 6523 | * d_instantiate it needs these. Smack checks to see |
| 6524 | * if the filesystem supports xattrs by looking at the |
| 6525 | * ops vector. |
| 6526 | */ |
Casey Schaufler | ad19db7 | 2011-12-15 10:09:07 -0500 | [diff] [blame] | 6527 | inode->i_op = &btrfs_special_inode_operations; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6528 | init_special_inode(inode, inode->i_mode, rdev); |
| 6529 | |
| 6530 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6531 | if (err) |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6532 | goto out_unlock; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6533 | |
Nikolay Borisov | cef415a | 2017-02-20 13:51:09 +0200 | [diff] [blame] | 6534 | err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode), |
| 6535 | 0, index); |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6536 | if (err) |
| 6537 | goto out_unlock; |
| 6538 | |
| 6539 | btrfs_update_inode(trans, root, inode); |
| 6540 | d_instantiate_new(dentry, inode); |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6541 | |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6542 | out_unlock: |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 6543 | btrfs_end_transaction(trans); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6544 | btrfs_btree_balance_dirty(fs_info); |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6545 | if (err && inode) { |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6546 | inode_dec_link_count(inode); |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6547 | discard_new_inode(inode); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6548 | } |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6549 | return err; |
| 6550 | } |
| 6551 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6552 | static int btrfs_create(struct inode *dir, struct dentry *dentry, |
Al Viro | ebfc3b4 | 2012-06-10 18:05:36 -0400 | [diff] [blame] | 6553 | umode_t mode, bool excl) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6554 | { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6555 | struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6556 | struct btrfs_trans_handle *trans; |
| 6557 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 6558 | struct inode *inode = NULL; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6559 | int err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6560 | u64 objectid; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 6561 | u64 index = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6562 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 6563 | /* |
| 6564 | * 2 for inode item and ref |
| 6565 | * 2 for dir items |
| 6566 | * 1 for xattr if selinux is on |
| 6567 | */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6568 | trans = btrfs_start_transaction(root, 5); |
| 6569 | if (IS_ERR(trans)) |
| 6570 | return PTR_ERR(trans); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 6571 | |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 6572 | err = btrfs_find_free_ino(root, &objectid); |
| 6573 | if (err) |
| 6574 | goto out_unlock; |
| 6575 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6576 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 6577 | dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid, |
| 6578 | mode, &index); |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 6579 | if (IS_ERR(inode)) { |
| 6580 | err = PTR_ERR(inode); |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6581 | inode = NULL; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6582 | goto out_unlock; |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 6583 | } |
Casey Schaufler | ad19db7 | 2011-12-15 10:09:07 -0500 | [diff] [blame] | 6584 | /* |
| 6585 | * If the active LSM wants to access the inode during |
| 6586 | * d_instantiate it needs these. Smack checks to see |
| 6587 | * if the filesystem supports xattrs by looking at the |
| 6588 | * ops vector. |
| 6589 | */ |
| 6590 | inode->i_fop = &btrfs_file_operations; |
| 6591 | inode->i_op = &btrfs_file_inode_operations; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6592 | inode->i_mapping->a_ops = &btrfs_aops; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6593 | |
| 6594 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); |
| 6595 | if (err) |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6596 | goto out_unlock; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6597 | |
| 6598 | err = btrfs_update_inode(trans, root, inode); |
| 6599 | if (err) |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6600 | goto out_unlock; |
Casey Schaufler | ad19db7 | 2011-12-15 10:09:07 -0500 | [diff] [blame] | 6601 | |
Nikolay Borisov | cef415a | 2017-02-20 13:51:09 +0200 | [diff] [blame] | 6602 | err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode), |
| 6603 | 0, index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6604 | if (err) |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6605 | goto out_unlock; |
Filipe Brandenburger | 43baa57 | 2012-11-30 03:40:09 +0000 | [diff] [blame] | 6606 | |
Filipe Brandenburger | 43baa57 | 2012-11-30 03:40:09 +0000 | [diff] [blame] | 6607 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
Al Viro | 1e2e547 | 2018-05-04 08:23:01 -0400 | [diff] [blame] | 6608 | d_instantiate_new(dentry, inode); |
Filipe Brandenburger | 43baa57 | 2012-11-30 03:40:09 +0000 | [diff] [blame] | 6609 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6610 | out_unlock: |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 6611 | btrfs_end_transaction(trans); |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6612 | if (err && inode) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6613 | inode_dec_link_count(inode); |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6614 | discard_new_inode(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6615 | } |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6616 | btrfs_btree_balance_dirty(fs_info); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6617 | return err; |
| 6618 | } |
| 6619 | |
| 6620 | static int btrfs_link(struct dentry *old_dentry, struct inode *dir, |
| 6621 | struct dentry *dentry) |
| 6622 | { |
Filipe Manana | 271dba45 | 2016-01-05 16:24:05 +0000 | [diff] [blame] | 6623 | struct btrfs_trans_handle *trans = NULL; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6624 | struct btrfs_root *root = BTRFS_I(dir)->root; |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 6625 | struct inode *inode = d_inode(old_dentry); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6626 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 6627 | u64 index; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6628 | int err; |
| 6629 | int drop_inode = 0; |
| 6630 | |
TARUISI Hiroaki | 4a8be42 | 2009-11-12 07:14:26 +0000 | [diff] [blame] | 6631 | /* 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] | 6632 | if (root->root_key.objectid != BTRFS_I(inode)->root->root_key.objectid) |
Mark Fasheh | 3ab3564 | 2011-03-22 17:20:26 +0000 | [diff] [blame] | 6633 | return -EXDEV; |
TARUISI Hiroaki | 4a8be42 | 2009-11-12 07:14:26 +0000 | [diff] [blame] | 6634 | |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 6635 | if (inode->i_nlink >= BTRFS_LINK_MAX) |
Al Viro | c055e99 | 2011-03-04 17:15:18 +0000 | [diff] [blame] | 6636 | return -EMLINK; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 6637 | |
Nikolay Borisov | 877574e | 2017-02-20 13:50:33 +0200 | [diff] [blame] | 6638 | err = btrfs_set_inode_index(BTRFS_I(dir), &index); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6639 | if (err) |
| 6640 | goto fail; |
| 6641 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6642 | /* |
Miao Xie | 7e6b646 | 2011-02-18 09:21:17 +0000 | [diff] [blame] | 6643 | * 2 items for inode and inode ref |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6644 | * 2 items for dir items |
Miao Xie | 7e6b646 | 2011-02-18 09:21:17 +0000 | [diff] [blame] | 6645 | * 1 item for parent inode |
Omar Sandoval | 399b0bb | 2018-05-11 13:13:40 -0700 | [diff] [blame] | 6646 | * 1 item for orphan item deletion if O_TMPFILE |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6647 | */ |
Omar Sandoval | 399b0bb | 2018-05-11 13:13:40 -0700 | [diff] [blame] | 6648 | trans = btrfs_start_transaction(root, inode->i_nlink ? 5 : 6); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6649 | if (IS_ERR(trans)) { |
| 6650 | err = PTR_ERR(trans); |
Filipe Manana | 271dba45 | 2016-01-05 16:24:05 +0000 | [diff] [blame] | 6651 | trans = NULL; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6652 | goto fail; |
| 6653 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6654 | |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 6655 | /* There are several dir indexes for this inode, clear the cache. */ |
| 6656 | BTRFS_I(inode)->dir_index = 0ULL; |
Zach Brown | 8b558c5 | 2013-10-16 12:10:34 -0700 | [diff] [blame] | 6657 | inc_nlink(inode); |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 6658 | inode_inc_iversion(inode); |
Deepa Dinamani | c2050a4 | 2016-09-14 07:48:06 -0700 | [diff] [blame] | 6659 | inode->i_ctime = current_time(inode); |
Al Viro | 7de9c6ee | 2010-10-23 11:11:40 -0400 | [diff] [blame] | 6660 | ihold(inode); |
Josef Bacik | e997615 | 2012-10-11 15:53:56 -0400 | [diff] [blame] | 6661 | set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6662 | |
Nikolay Borisov | cef415a | 2017-02-20 13:51:09 +0200 | [diff] [blame] | 6663 | err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode), |
| 6664 | 1, index); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6665 | |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 6666 | if (err) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6667 | drop_inode = 1; |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 6668 | } else { |
Al Viro | 10d9f30 | 2011-07-16 23:09:10 -0400 | [diff] [blame] | 6669 | struct dentry *parent = dentry->d_parent; |
Filipe Manana | d4682ba | 2018-06-11 19:24:28 +0100 | [diff] [blame] | 6670 | int ret; |
| 6671 | |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 6672 | err = btrfs_update_inode(trans, root, inode); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6673 | if (err) |
| 6674 | goto fail; |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 6675 | if (inode->i_nlink == 1) { |
| 6676 | /* |
| 6677 | * If new hard link count is 1, it's a file created |
| 6678 | * with open(2) O_TMPFILE flag. |
| 6679 | */ |
Nikolay Borisov | 3d6ae7b | 2017-02-20 13:50:58 +0200 | [diff] [blame] | 6680 | err = btrfs_orphan_del(trans, BTRFS_I(inode)); |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 6681 | if (err) |
| 6682 | goto fail; |
| 6683 | } |
Al Viro | 08c422c | 2011-12-23 07:58:13 -0500 | [diff] [blame] | 6684 | d_instantiate(dentry, inode); |
Filipe Manana | d4682ba | 2018-06-11 19:24:28 +0100 | [diff] [blame] | 6685 | ret = btrfs_log_new_name(trans, BTRFS_I(inode), NULL, parent, |
| 6686 | true, NULL); |
| 6687 | if (ret == BTRFS_NEED_TRANS_COMMIT) { |
| 6688 | err = btrfs_commit_transaction(trans); |
| 6689 | trans = NULL; |
| 6690 | } |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 6691 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6692 | |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 6693 | fail: |
Filipe Manana | 271dba45 | 2016-01-05 16:24:05 +0000 | [diff] [blame] | 6694 | if (trans) |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 6695 | btrfs_end_transaction(trans); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6696 | if (drop_inode) { |
| 6697 | inode_dec_link_count(inode); |
| 6698 | iput(inode); |
| 6699 | } |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6700 | btrfs_btree_balance_dirty(fs_info); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6701 | return err; |
| 6702 | } |
| 6703 | |
Al Viro | 18bb1db | 2011-07-26 01:41:39 -0400 | [diff] [blame] | 6704 | 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] | 6705 | { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6706 | struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); |
Chris Mason | b9d8666 | 2008-05-02 16:13:49 -0400 | [diff] [blame] | 6707 | struct inode *inode = NULL; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6708 | struct btrfs_trans_handle *trans; |
| 6709 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 6710 | int err = 0; |
Chris Mason | b9d8666 | 2008-05-02 16:13:49 -0400 | [diff] [blame] | 6711 | u64 objectid = 0; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 6712 | u64 index = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6713 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 6714 | /* |
| 6715 | * 2 items for inode and ref |
| 6716 | * 2 items for dir items |
| 6717 | * 1 for xattr if selinux is on |
| 6718 | */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6719 | trans = btrfs_start_transaction(root, 5); |
| 6720 | if (IS_ERR(trans)) |
| 6721 | return PTR_ERR(trans); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6722 | |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 6723 | err = btrfs_find_free_ino(root, &objectid); |
| 6724 | if (err) |
| 6725 | goto out_fail; |
| 6726 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6727 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 6728 | dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid, |
| 6729 | S_IFDIR | mode, &index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6730 | if (IS_ERR(inode)) { |
| 6731 | err = PTR_ERR(inode); |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6732 | inode = NULL; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6733 | goto out_fail; |
| 6734 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6735 | |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6736 | /* these must be set before we unlock the inode */ |
| 6737 | inode->i_op = &btrfs_dir_inode_operations; |
| 6738 | inode->i_fop = &btrfs_dir_file_operations; |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 6739 | |
Eric Paris | 2a7dba3 | 2011-02-01 11:05:39 -0500 | [diff] [blame] | 6740 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 6741 | if (err) |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6742 | goto out_fail; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6743 | |
Nikolay Borisov | 6ef06d2 | 2017-02-20 13:50:34 +0200 | [diff] [blame] | 6744 | btrfs_i_size_write(BTRFS_I(inode), 0); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6745 | err = btrfs_update_inode(trans, root, inode); |
| 6746 | if (err) |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6747 | goto out_fail; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6748 | |
Nikolay Borisov | db0a669 | 2017-02-20 13:51:08 +0200 | [diff] [blame] | 6749 | err = btrfs_add_link(trans, BTRFS_I(dir), BTRFS_I(inode), |
| 6750 | dentry->d_name.name, |
| 6751 | dentry->d_name.len, 0, index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6752 | if (err) |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6753 | goto out_fail; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6754 | |
Al Viro | 1e2e547 | 2018-05-04 08:23:01 -0400 | [diff] [blame] | 6755 | d_instantiate_new(dentry, inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6756 | |
| 6757 | out_fail: |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 6758 | btrfs_end_transaction(trans); |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6759 | if (err && inode) { |
Wang Shilong | c7cfb8a | 2014-12-24 14:45:30 +0800 | [diff] [blame] | 6760 | inode_dec_link_count(inode); |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 6761 | discard_new_inode(inode); |
Wang Shilong | c7cfb8a | 2014-12-24 14:45:30 +0800 | [diff] [blame] | 6762 | } |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6763 | btrfs_btree_balance_dirty(fs_info); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6764 | return err; |
| 6765 | } |
| 6766 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6767 | static noinline int uncompress_inline(struct btrfs_path *path, |
Byongho Lee | e40da0e | 2015-05-19 23:46:45 +0900 | [diff] [blame] | 6768 | struct page *page, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6769 | size_t pg_offset, u64 extent_offset, |
| 6770 | struct btrfs_file_extent_item *item) |
| 6771 | { |
| 6772 | int ret; |
| 6773 | struct extent_buffer *leaf = path->nodes[0]; |
| 6774 | char *tmp; |
| 6775 | size_t max_size; |
| 6776 | unsigned long inline_size; |
| 6777 | unsigned long ptr; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 6778 | int compress_type; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6779 | |
| 6780 | WARN_ON(pg_offset != 0); |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 6781 | compress_type = btrfs_file_extent_compression(leaf, item); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6782 | max_size = btrfs_file_extent_ram_bytes(leaf, item); |
| 6783 | inline_size = btrfs_file_extent_inline_item_len(leaf, |
Ross Kirk | dd3cc16 | 2013-09-16 15:58:09 +0100 | [diff] [blame] | 6784 | btrfs_item_nr(path->slots[0])); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6785 | tmp = kmalloc(inline_size, GFP_NOFS); |
Tsutomu Itoh | 8d41371 | 2011-04-25 19:43:52 -0400 | [diff] [blame] | 6786 | if (!tmp) |
| 6787 | return -ENOMEM; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6788 | ptr = btrfs_file_extent_inline_start(item); |
| 6789 | |
| 6790 | read_extent_buffer(leaf, tmp, ptr, inline_size); |
| 6791 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 6792 | max_size = min_t(unsigned long, PAGE_SIZE, max_size); |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 6793 | ret = btrfs_decompress(compress_type, tmp, page, |
| 6794 | extent_offset, inline_size, max_size); |
Zygo Blaxell | e1699d2 | 2017-03-10 16:45:44 -0500 | [diff] [blame] | 6795 | |
| 6796 | /* |
| 6797 | * decompression code contains a memset to fill in any space between the end |
| 6798 | * of the uncompressed data and the end of max_size in case the decompressed |
| 6799 | * data ends up shorter than ram_bytes. That doesn't cover the hole between |
| 6800 | * the end of an inline extent and the beginning of the next block, so we |
| 6801 | * cover that region here. |
| 6802 | */ |
| 6803 | |
| 6804 | if (max_size + pg_offset < PAGE_SIZE) { |
| 6805 | char *map = kmap(page); |
| 6806 | memset(map + pg_offset + max_size, 0, PAGE_SIZE - max_size - pg_offset); |
| 6807 | kunmap(page); |
| 6808 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6809 | kfree(tmp); |
Zach Brown | 166ae5a | 2014-05-09 17:15:10 -0400 | [diff] [blame] | 6810 | return ret; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6811 | } |
| 6812 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 6813 | /* |
| 6814 | * a bit scary, this does extent mapping from logical file offset to the disk. |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6815 | * the ugly parts come from merging extents from the disk with the in-ram |
| 6816 | * representation. This gets more complex because of the data=ordered code, |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 6817 | * where the in-ram extents might be locked pending data=ordered completion. |
| 6818 | * |
| 6819 | * This also copies inline extents directly into the page. |
| 6820 | */ |
Nikolay Borisov | fc4f21b1 | 2017-02-20 13:51:06 +0200 | [diff] [blame] | 6821 | struct extent_map *btrfs_get_extent(struct btrfs_inode *inode, |
Liu Bo | de2c661 | 2018-08-25 13:47:59 +0800 | [diff] [blame] | 6822 | struct page *page, |
| 6823 | size_t pg_offset, u64 start, u64 len, |
| 6824 | int create) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6825 | { |
David Sterba | 3ffbd68 | 2018-06-29 10:56:42 +0200 | [diff] [blame] | 6826 | struct btrfs_fs_info *fs_info = inode->root->fs_info; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6827 | int ret; |
| 6828 | int err = 0; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6829 | u64 extent_start = 0; |
| 6830 | u64 extent_end = 0; |
Nikolay Borisov | fc4f21b1 | 2017-02-20 13:51:06 +0200 | [diff] [blame] | 6831 | u64 objectid = btrfs_ino(inode); |
Nikolay Borisov | 694c12e | 2018-12-17 10:35:59 +0200 | [diff] [blame] | 6832 | u8 extent_type; |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 6833 | struct btrfs_path *path = NULL; |
Nikolay Borisov | fc4f21b1 | 2017-02-20 13:51:06 +0200 | [diff] [blame] | 6834 | struct btrfs_root *root = inode->root; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6835 | struct btrfs_file_extent_item *item; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6836 | struct extent_buffer *leaf; |
| 6837 | struct btrfs_key found_key; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6838 | struct extent_map *em = NULL; |
Nikolay Borisov | fc4f21b1 | 2017-02-20 13:51:06 +0200 | [diff] [blame] | 6839 | struct extent_map_tree *em_tree = &inode->extent_tree; |
| 6840 | struct extent_io_tree *io_tree = &inode->io_tree; |
Filipe Manana | 7ffbb59 | 2014-06-09 03:48:05 +0100 | [diff] [blame] | 6841 | const bool new_inline = !page || create; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6842 | |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 6843 | read_lock(&em_tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6844 | em = lookup_extent_mapping(em_tree, start, len); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 6845 | if (em) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6846 | em->bdev = fs_info->fs_devices->latest_bdev; |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 6847 | read_unlock(&em_tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6848 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6849 | if (em) { |
Chris Mason | e1c4b74 | 2008-04-22 13:26:46 -0400 | [diff] [blame] | 6850 | if (em->start > start || em->start + em->len <= start) |
| 6851 | free_extent_map(em); |
| 6852 | else if (em->block_start == EXTENT_MAP_INLINE && page) |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 6853 | free_extent_map(em); |
| 6854 | else |
| 6855 | goto out; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6856 | } |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 6857 | em = alloc_extent_map(); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6858 | if (!em) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6859 | err = -ENOMEM; |
| 6860 | goto out; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6861 | } |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6862 | em->bdev = fs_info->fs_devices->latest_bdev; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6863 | em->start = EXTENT_MAP_HOLE; |
Chris Mason | 445a694 | 2008-11-10 11:53:33 -0500 | [diff] [blame] | 6864 | em->orig_start = EXTENT_MAP_HOLE; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6865 | em->len = (u64)-1; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6866 | em->block_len = (u64)-1; |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 6867 | |
Liu Bo | bee6ec8 | 2018-08-17 05:05:28 +0800 | [diff] [blame] | 6868 | path = btrfs_alloc_path(); |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 6869 | if (!path) { |
Liu Bo | bee6ec8 | 2018-08-17 05:05:28 +0800 | [diff] [blame] | 6870 | err = -ENOMEM; |
| 6871 | goto out; |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 6872 | } |
| 6873 | |
Liu Bo | bee6ec8 | 2018-08-17 05:05:28 +0800 | [diff] [blame] | 6874 | /* Chances are we'll be called again, so go ahead and do readahead */ |
| 6875 | path->reada = READA_FORWARD; |
| 6876 | |
Liu Bo | e49aabd | 2018-08-25 13:47:09 +0800 | [diff] [blame] | 6877 | /* |
| 6878 | * Unless we're going to uncompress the inline extent, no sleep would |
| 6879 | * happen. |
| 6880 | */ |
| 6881 | path->leave_spinning = 1; |
| 6882 | |
Nikolay Borisov | 5c9a702 | 2017-12-01 11:19:40 +0200 | [diff] [blame] | 6883 | ret = btrfs_lookup_file_extent(NULL, root, path, objectid, start, 0); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6884 | if (ret < 0) { |
| 6885 | err = ret; |
| 6886 | goto out; |
Nikolay Borisov | b8eeab7 | 2018-12-17 11:49:00 +0200 | [diff] [blame] | 6887 | } else if (ret > 0) { |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6888 | if (path->slots[0] == 0) |
| 6889 | goto not_found; |
| 6890 | path->slots[0]--; |
| 6891 | } |
| 6892 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6893 | leaf = path->nodes[0]; |
| 6894 | item = btrfs_item_ptr(leaf, path->slots[0], |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6895 | struct btrfs_file_extent_item); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6896 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6897 | if (found_key.objectid != objectid || |
Nikolay Borisov | 694c12e | 2018-12-17 10:35:59 +0200 | [diff] [blame] | 6898 | found_key.type != BTRFS_EXTENT_DATA_KEY) { |
Josef Bacik | 25a5034 | 2013-10-14 12:08:38 -0400 | [diff] [blame] | 6899 | /* |
| 6900 | * If we backup past the first extent we want to move forward |
| 6901 | * and see if there is an extent in front of us, otherwise we'll |
| 6902 | * say there is a hole for our whole search range which can |
| 6903 | * cause problems. |
| 6904 | */ |
| 6905 | extent_end = start; |
| 6906 | goto next; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6907 | } |
| 6908 | |
Nikolay Borisov | 694c12e | 2018-12-17 10:35:59 +0200 | [diff] [blame] | 6909 | extent_type = btrfs_file_extent_type(leaf, item); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6910 | extent_start = found_key.offset; |
Nikolay Borisov | 694c12e | 2018-12-17 10:35:59 +0200 | [diff] [blame] | 6911 | if (extent_type == BTRFS_FILE_EXTENT_REG || |
| 6912 | extent_type == BTRFS_FILE_EXTENT_PREALLOC) { |
Qu Wenruo | 6bf9e4b | 2019-03-13 13:55:11 +0800 | [diff] [blame] | 6913 | /* Only regular file could have regular/prealloc extent */ |
| 6914 | if (!S_ISREG(inode->vfs_inode.i_mode)) { |
| 6915 | ret = -EUCLEAN; |
| 6916 | btrfs_crit(fs_info, |
| 6917 | "regular/prealloc extent found for non-regular inode %llu", |
| 6918 | btrfs_ino(inode)); |
| 6919 | goto out; |
| 6920 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6921 | extent_end = extent_start + |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 6922 | btrfs_file_extent_num_bytes(leaf, item); |
Liu Bo | 09ed2f1 | 2017-03-10 11:09:48 -0800 | [diff] [blame] | 6923 | |
| 6924 | trace_btrfs_get_extent_show_fi_regular(inode, leaf, item, |
| 6925 | extent_start); |
Nikolay Borisov | 694c12e | 2018-12-17 10:35:59 +0200 | [diff] [blame] | 6926 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6927 | size_t size; |
Qu Wenruo | e41ca58 | 2018-06-06 15:41:49 +0800 | [diff] [blame] | 6928 | |
| 6929 | size = btrfs_file_extent_ram_bytes(leaf, item); |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 6930 | extent_end = ALIGN(extent_start + size, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6931 | fs_info->sectorsize); |
Liu Bo | 09ed2f1 | 2017-03-10 11:09:48 -0800 | [diff] [blame] | 6932 | |
| 6933 | trace_btrfs_get_extent_show_fi_inline(inode, leaf, item, |
| 6934 | path->slots[0], |
| 6935 | extent_start); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6936 | } |
Josef Bacik | 25a5034 | 2013-10-14 12:08:38 -0400 | [diff] [blame] | 6937 | next: |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6938 | if (start >= extent_end) { |
| 6939 | path->slots[0]++; |
| 6940 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { |
| 6941 | ret = btrfs_next_leaf(root, path); |
| 6942 | if (ret < 0) { |
| 6943 | err = ret; |
| 6944 | goto out; |
Nikolay Borisov | b8eeab7 | 2018-12-17 11:49:00 +0200 | [diff] [blame] | 6945 | } else if (ret > 0) { |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6946 | goto not_found; |
Nikolay Borisov | b8eeab7 | 2018-12-17 11:49:00 +0200 | [diff] [blame] | 6947 | } |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6948 | leaf = path->nodes[0]; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6949 | } |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6950 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 6951 | if (found_key.objectid != objectid || |
| 6952 | found_key.type != BTRFS_EXTENT_DATA_KEY) |
| 6953 | goto not_found; |
| 6954 | if (start + len <= found_key.offset) |
| 6955 | goto not_found; |
Wang Shilong | e2eca69 | 2014-07-17 11:44:14 +0800 | [diff] [blame] | 6956 | if (start > found_key.offset) |
| 6957 | goto next; |
Nikolay Borisov | 02a033d | 2018-12-17 10:36:02 +0200 | [diff] [blame] | 6958 | |
| 6959 | /* New extent overlaps with existing one */ |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6960 | em->start = start; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 6961 | em->orig_start = start; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6962 | em->len = found_key.offset - start; |
Nikolay Borisov | 02a033d | 2018-12-17 10:36:02 +0200 | [diff] [blame] | 6963 | em->block_start = EXTENT_MAP_HOLE; |
| 6964 | goto insert; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6965 | } |
| 6966 | |
Nikolay Borisov | fc4f21b1 | 2017-02-20 13:51:06 +0200 | [diff] [blame] | 6967 | btrfs_extent_item_to_extent_map(inode, path, item, |
Nikolay Borisov | 9cdc512 | 2017-02-20 13:51:02 +0200 | [diff] [blame] | 6968 | new_inline, em); |
Filipe Manana | 7ffbb59 | 2014-06-09 03:48:05 +0100 | [diff] [blame] | 6969 | |
Nikolay Borisov | 694c12e | 2018-12-17 10:35:59 +0200 | [diff] [blame] | 6970 | if (extent_type == BTRFS_FILE_EXTENT_REG || |
| 6971 | extent_type == BTRFS_FILE_EXTENT_PREALLOC) { |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6972 | goto insert; |
Nikolay Borisov | 694c12e | 2018-12-17 10:35:59 +0200 | [diff] [blame] | 6973 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6974 | unsigned long ptr; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6975 | char *map; |
Chris Mason | 3326d1b | 2007-10-15 16:18:25 -0400 | [diff] [blame] | 6976 | size_t size; |
| 6977 | size_t extent_offset; |
| 6978 | size_t copy_size; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6979 | |
Filipe Manana | 7ffbb59 | 2014-06-09 03:48:05 +0100 | [diff] [blame] | 6980 | if (new_inline) |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 6981 | goto out; |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 6982 | |
Qu Wenruo | e41ca58 | 2018-06-06 15:41:49 +0800 | [diff] [blame] | 6983 | size = btrfs_file_extent_ram_bytes(leaf, item); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6984 | extent_offset = page_offset(page) + pg_offset - extent_start; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 6985 | copy_size = min_t(u64, PAGE_SIZE - pg_offset, |
| 6986 | size - extent_offset); |
Chris Mason | 3326d1b | 2007-10-15 16:18:25 -0400 | [diff] [blame] | 6987 | em->start = extent_start + extent_offset; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6988 | em->len = ALIGN(copy_size, fs_info->sectorsize); |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 6989 | em->orig_block_len = em->len; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 6990 | em->orig_start = em->start; |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 6991 | ptr = btrfs_file_extent_inline_start(item) + extent_offset; |
Liu Bo | e49aabd | 2018-08-25 13:47:09 +0800 | [diff] [blame] | 6992 | |
| 6993 | btrfs_set_path_blocking(path); |
Edmund Nadolski | bf46f52 | 2017-11-20 13:24:49 -0700 | [diff] [blame] | 6994 | if (!PageUptodate(page)) { |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 6995 | if (btrfs_file_extent_compression(leaf, item) != |
| 6996 | BTRFS_COMPRESS_NONE) { |
Byongho Lee | e40da0e | 2015-05-19 23:46:45 +0900 | [diff] [blame] | 6997 | ret = uncompress_inline(path, page, pg_offset, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6998 | extent_offset, item); |
Zach Brown | 166ae5a | 2014-05-09 17:15:10 -0400 | [diff] [blame] | 6999 | if (ret) { |
| 7000 | err = ret; |
| 7001 | goto out; |
| 7002 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 7003 | } else { |
| 7004 | map = kmap(page); |
| 7005 | read_extent_buffer(leaf, map + pg_offset, ptr, |
| 7006 | copy_size); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 7007 | if (pg_offset + copy_size < PAGE_SIZE) { |
Chris Mason | 93c82d5 | 2009-09-11 12:36:29 -0400 | [diff] [blame] | 7008 | memset(map + pg_offset + copy_size, 0, |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 7009 | PAGE_SIZE - pg_offset - |
Chris Mason | 93c82d5 | 2009-09-11 12:36:29 -0400 | [diff] [blame] | 7010 | copy_size); |
| 7011 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 7012 | kunmap(page); |
| 7013 | } |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 7014 | flush_dcache_page(page); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7015 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 7016 | set_extent_uptodate(io_tree, em->start, |
Arne Jansen | 507903b | 2011-04-06 10:02:20 +0000 | [diff] [blame] | 7017 | extent_map_end(em) - 1, NULL, GFP_NOFS); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7018 | goto insert; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7019 | } |
| 7020 | not_found: |
| 7021 | em->start = start; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 7022 | em->orig_start = start; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 7023 | em->len = len; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 7024 | em->block_start = EXTENT_MAP_HOLE; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7025 | insert: |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 7026 | btrfs_release_path(path); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 7027 | if (em->start > start || extent_map_end(em) <= start) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7028 | btrfs_err(fs_info, |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 7029 | "bad extent! em: [%llu %llu] passed [%llu %llu]", |
| 7030 | em->start, em->len, start, len); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7031 | err = -EIO; |
| 7032 | goto out; |
| 7033 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 7034 | |
| 7035 | err = 0; |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 7036 | write_lock(&em_tree->lock); |
David Sterba | f46b24c | 2018-04-03 21:45:57 +0200 | [diff] [blame] | 7037 | err = btrfs_add_extent_mapping(fs_info, em_tree, &em, start, len); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 7038 | write_unlock(&em_tree->lock); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7039 | out: |
Liu Bo | c641428 | 2018-08-23 07:36:17 +0800 | [diff] [blame] | 7040 | btrfs_free_path(path); |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 7041 | |
Nikolay Borisov | fc4f21b1 | 2017-02-20 13:51:06 +0200 | [diff] [blame] | 7042 | trace_btrfs_get_extent(root, inode, em); |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 7043 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7044 | if (err) { |
| 7045 | free_extent_map(em); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7046 | return ERR_PTR(err); |
| 7047 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 7048 | BUG_ON(!em); /* Error is always set */ |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7049 | return em; |
| 7050 | } |
| 7051 | |
Nikolay Borisov | fc4f21b1 | 2017-02-20 13:51:06 +0200 | [diff] [blame] | 7052 | struct extent_map *btrfs_get_extent_fiemap(struct btrfs_inode *inode, |
Nikolay Borisov | 4ab47a8 | 2018-12-12 09:42:32 +0200 | [diff] [blame] | 7053 | u64 start, u64 len) |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 7054 | { |
| 7055 | struct extent_map *em; |
| 7056 | struct extent_map *hole_em = NULL; |
Nikolay Borisov | f3714ef | 2018-12-12 09:42:33 +0200 | [diff] [blame] | 7057 | u64 delalloc_start = start; |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 7058 | u64 end; |
Nikolay Borisov | f3714ef | 2018-12-12 09:42:33 +0200 | [diff] [blame] | 7059 | u64 delalloc_len; |
| 7060 | u64 delalloc_end; |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 7061 | int err = 0; |
| 7062 | |
Nikolay Borisov | 4ab47a8 | 2018-12-12 09:42:32 +0200 | [diff] [blame] | 7063 | em = btrfs_get_extent(inode, NULL, 0, start, len, 0); |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 7064 | if (IS_ERR(em)) |
| 7065 | return em; |
Dan Carpenter | 9986277 | 2017-04-11 11:57:15 +0300 | [diff] [blame] | 7066 | /* |
| 7067 | * If our em maps to: |
| 7068 | * - a hole or |
| 7069 | * - a pre-alloc extent, |
| 7070 | * there might actually be delalloc bytes behind it. |
| 7071 | */ |
| 7072 | if (em->block_start != EXTENT_MAP_HOLE && |
| 7073 | !test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) |
| 7074 | return em; |
| 7075 | else |
| 7076 | hole_em = em; |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 7077 | |
| 7078 | /* check to see if we've wrapped (len == -1 or similar) */ |
| 7079 | end = start + len; |
| 7080 | if (end < start) |
| 7081 | end = (u64)-1; |
| 7082 | else |
| 7083 | end -= 1; |
| 7084 | |
| 7085 | em = NULL; |
| 7086 | |
| 7087 | /* ok, we didn't find anything, lets look for delalloc */ |
Nikolay Borisov | f3714ef | 2018-12-12 09:42:33 +0200 | [diff] [blame] | 7088 | delalloc_len = count_range_bits(&inode->io_tree, &delalloc_start, |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 7089 | end, len, EXTENT_DELALLOC, 1); |
Nikolay Borisov | f3714ef | 2018-12-12 09:42:33 +0200 | [diff] [blame] | 7090 | delalloc_end = delalloc_start + delalloc_len; |
| 7091 | if (delalloc_end < delalloc_start) |
| 7092 | delalloc_end = (u64)-1; |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 7093 | |
| 7094 | /* |
Nikolay Borisov | f3714ef | 2018-12-12 09:42:33 +0200 | [diff] [blame] | 7095 | * We didn't find anything useful, return the original results from |
| 7096 | * get_extent() |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 7097 | */ |
Nikolay Borisov | f3714ef | 2018-12-12 09:42:33 +0200 | [diff] [blame] | 7098 | if (delalloc_start > end || delalloc_end <= start) { |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 7099 | em = hole_em; |
| 7100 | hole_em = NULL; |
| 7101 | goto out; |
| 7102 | } |
| 7103 | |
Nikolay Borisov | f3714ef | 2018-12-12 09:42:33 +0200 | [diff] [blame] | 7104 | /* |
| 7105 | * Adjust the delalloc_start to make sure it doesn't go backwards from |
| 7106 | * the start they passed in |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 7107 | */ |
Nikolay Borisov | f3714ef | 2018-12-12 09:42:33 +0200 | [diff] [blame] | 7108 | delalloc_start = max(start, delalloc_start); |
| 7109 | delalloc_len = delalloc_end - delalloc_start; |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 7110 | |
Nikolay Borisov | f3714ef | 2018-12-12 09:42:33 +0200 | [diff] [blame] | 7111 | if (delalloc_len > 0) { |
| 7112 | u64 hole_start; |
Nikolay Borisov | 02950af | 2018-12-12 09:42:34 +0200 | [diff] [blame] | 7113 | u64 hole_len; |
Nikolay Borisov | f3714ef | 2018-12-12 09:42:33 +0200 | [diff] [blame] | 7114 | const u64 hole_end = extent_map_end(hole_em); |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 7115 | |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 7116 | em = alloc_extent_map(); |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 7117 | if (!em) { |
| 7118 | err = -ENOMEM; |
| 7119 | goto out; |
| 7120 | } |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 7121 | em->bdev = NULL; |
Nikolay Borisov | f3714ef | 2018-12-12 09:42:33 +0200 | [diff] [blame] | 7122 | |
| 7123 | ASSERT(hole_em); |
| 7124 | /* |
| 7125 | * When btrfs_get_extent can't find anything it returns one |
| 7126 | * huge hole |
| 7127 | * |
| 7128 | * Make sure what it found really fits our range, and adjust to |
| 7129 | * make sure it is based on the start from the caller |
| 7130 | */ |
| 7131 | if (hole_end <= start || hole_em->start > end) { |
| 7132 | free_extent_map(hole_em); |
| 7133 | hole_em = NULL; |
| 7134 | } else { |
| 7135 | hole_start = max(hole_em->start, start); |
| 7136 | hole_len = hole_end - hole_start; |
| 7137 | } |
| 7138 | |
| 7139 | if (hole_em && delalloc_start > hole_start) { |
| 7140 | /* |
| 7141 | * Our hole starts before our delalloc, so we have to |
| 7142 | * return just the parts of the hole that go until the |
| 7143 | * delalloc starts |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 7144 | */ |
Nikolay Borisov | f3714ef | 2018-12-12 09:42:33 +0200 | [diff] [blame] | 7145 | em->len = min(hole_len, delalloc_start - hole_start); |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 7146 | em->start = hole_start; |
| 7147 | em->orig_start = hole_start; |
| 7148 | /* |
Nikolay Borisov | f3714ef | 2018-12-12 09:42:33 +0200 | [diff] [blame] | 7149 | * Don't adjust block start at all, it is fixed at |
| 7150 | * EXTENT_MAP_HOLE |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 7151 | */ |
| 7152 | em->block_start = hole_em->block_start; |
| 7153 | em->block_len = hole_len; |
Liu Bo | f9e4fb5 | 2013-01-07 10:10:12 +0000 | [diff] [blame] | 7154 | if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags)) |
| 7155 | set_bit(EXTENT_FLAG_PREALLOC, &em->flags); |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 7156 | } else { |
Nikolay Borisov | f3714ef | 2018-12-12 09:42:33 +0200 | [diff] [blame] | 7157 | /* |
| 7158 | * Hole is out of passed range or it starts after |
| 7159 | * delalloc range |
| 7160 | */ |
| 7161 | em->start = delalloc_start; |
| 7162 | em->len = delalloc_len; |
| 7163 | em->orig_start = delalloc_start; |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 7164 | em->block_start = EXTENT_MAP_DELALLOC; |
Nikolay Borisov | f3714ef | 2018-12-12 09:42:33 +0200 | [diff] [blame] | 7165 | em->block_len = delalloc_len; |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 7166 | } |
Nikolay Borisov | bf8d32b | 2017-12-01 11:19:43 +0200 | [diff] [blame] | 7167 | } else { |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 7168 | return hole_em; |
| 7169 | } |
| 7170 | out: |
| 7171 | |
| 7172 | free_extent_map(hole_em); |
| 7173 | if (err) { |
| 7174 | free_extent_map(em); |
| 7175 | return ERR_PTR(err); |
| 7176 | } |
| 7177 | return em; |
| 7178 | } |
| 7179 | |
Filipe Manana | 5f9a8a5 | 2016-05-12 13:53:36 +0100 | [diff] [blame] | 7180 | static struct extent_map *btrfs_create_dio_extent(struct inode *inode, |
| 7181 | const u64 start, |
| 7182 | const u64 len, |
| 7183 | const u64 orig_start, |
| 7184 | const u64 block_start, |
| 7185 | const u64 block_len, |
| 7186 | const u64 orig_block_len, |
| 7187 | const u64 ram_bytes, |
| 7188 | const int type) |
| 7189 | { |
| 7190 | struct extent_map *em = NULL; |
| 7191 | int ret; |
| 7192 | |
Filipe Manana | 5f9a8a5 | 2016-05-12 13:53:36 +0100 | [diff] [blame] | 7193 | if (type != BTRFS_ORDERED_NOCOW) { |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 7194 | em = create_io_em(inode, start, len, orig_start, |
| 7195 | block_start, block_len, orig_block_len, |
| 7196 | ram_bytes, |
| 7197 | BTRFS_COMPRESS_NONE, /* compress_type */ |
| 7198 | type); |
Filipe Manana | 5f9a8a5 | 2016-05-12 13:53:36 +0100 | [diff] [blame] | 7199 | if (IS_ERR(em)) |
| 7200 | goto out; |
| 7201 | } |
| 7202 | ret = btrfs_add_ordered_extent_dio(inode, start, block_start, |
| 7203 | len, block_len, type); |
| 7204 | if (ret) { |
| 7205 | if (em) { |
| 7206 | free_extent_map(em); |
Nikolay Borisov | dcdbc05 | 2017-02-20 13:50:45 +0200 | [diff] [blame] | 7207 | btrfs_drop_extent_cache(BTRFS_I(inode), start, |
Filipe Manana | 5f9a8a5 | 2016-05-12 13:53:36 +0100 | [diff] [blame] | 7208 | start + len - 1, 0); |
| 7209 | } |
| 7210 | em = ERR_PTR(ret); |
| 7211 | } |
| 7212 | out: |
Filipe Manana | 5f9a8a5 | 2016-05-12 13:53:36 +0100 | [diff] [blame] | 7213 | |
| 7214 | return em; |
| 7215 | } |
| 7216 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7217 | static struct extent_map *btrfs_new_extent_direct(struct inode *inode, |
| 7218 | u64 start, u64 len) |
| 7219 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7220 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7221 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 7222 | struct extent_map *em; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7223 | struct btrfs_key ins; |
| 7224 | u64 alloc_hint; |
| 7225 | int ret; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7226 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7227 | alloc_hint = get_extent_allocation_hint(inode, start, len); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7228 | ret = btrfs_reserve_extent(root, len, len, fs_info->sectorsize, |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 7229 | 0, alloc_hint, &ins, 1, 1); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 7230 | if (ret) |
| 7231 | return ERR_PTR(ret); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7232 | |
Filipe Manana | 5f9a8a5 | 2016-05-12 13:53:36 +0100 | [diff] [blame] | 7233 | em = btrfs_create_dio_extent(inode, start, ins.offset, start, |
| 7234 | ins.objectid, ins.offset, ins.offset, |
Liu Bo | 6288d6e | 2017-02-21 12:12:58 -0800 | [diff] [blame] | 7235 | ins.offset, BTRFS_ORDERED_REGULAR); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7236 | btrfs_dec_block_group_reservations(fs_info, ins.objectid); |
Filipe Manana | 5f9a8a5 | 2016-05-12 13:53:36 +0100 | [diff] [blame] | 7237 | if (IS_ERR(em)) |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 7238 | btrfs_free_reserved_extent(fs_info, ins.objectid, |
| 7239 | ins.offset, 1); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 7240 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7241 | return em; |
| 7242 | } |
| 7243 | |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7244 | /* |
| 7245 | * returns 1 when the nocow is safe, < 1 on error, 0 if the |
| 7246 | * block must be cow'd |
| 7247 | */ |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 7248 | noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len, |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 7249 | u64 *orig_start, u64 *orig_block_len, |
| 7250 | u64 *ram_bytes) |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7251 | { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 7252 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7253 | struct btrfs_path *path; |
| 7254 | int ret; |
| 7255 | struct extent_buffer *leaf; |
| 7256 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Miao Xie | 7b2b708 | 2014-02-27 13:58:05 +0800 | [diff] [blame] | 7257 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7258 | struct btrfs_file_extent_item *fi; |
| 7259 | struct btrfs_key key; |
| 7260 | u64 disk_bytenr; |
| 7261 | u64 backref_offset; |
| 7262 | u64 extent_end; |
| 7263 | u64 num_bytes; |
| 7264 | int slot; |
| 7265 | int found_type; |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 7266 | bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW); |
Miao Xie | e77751a | 2013-12-27 21:11:50 +0800 | [diff] [blame] | 7267 | |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7268 | path = btrfs_alloc_path(); |
| 7269 | if (!path) |
| 7270 | return -ENOMEM; |
| 7271 | |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 7272 | ret = btrfs_lookup_file_extent(NULL, root, path, |
| 7273 | btrfs_ino(BTRFS_I(inode)), offset, 0); |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7274 | if (ret < 0) |
| 7275 | goto out; |
| 7276 | |
| 7277 | slot = path->slots[0]; |
| 7278 | if (ret == 1) { |
| 7279 | if (slot == 0) { |
| 7280 | /* can't find the item, must cow */ |
| 7281 | ret = 0; |
| 7282 | goto out; |
| 7283 | } |
| 7284 | slot--; |
| 7285 | } |
| 7286 | ret = 0; |
| 7287 | leaf = path->nodes[0]; |
| 7288 | btrfs_item_key_to_cpu(leaf, &key, slot); |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 7289 | if (key.objectid != btrfs_ino(BTRFS_I(inode)) || |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7290 | key.type != BTRFS_EXTENT_DATA_KEY) { |
| 7291 | /* not our file or wrong item type, must cow */ |
| 7292 | goto out; |
| 7293 | } |
| 7294 | |
| 7295 | if (key.offset > offset) { |
| 7296 | /* Wrong offset, must cow */ |
| 7297 | goto out; |
| 7298 | } |
| 7299 | |
| 7300 | fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item); |
| 7301 | found_type = btrfs_file_extent_type(leaf, fi); |
| 7302 | if (found_type != BTRFS_FILE_EXTENT_REG && |
| 7303 | found_type != BTRFS_FILE_EXTENT_PREALLOC) { |
| 7304 | /* not a regular extent, must cow */ |
| 7305 | goto out; |
| 7306 | } |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 7307 | |
| 7308 | if (!nocow && found_type == BTRFS_FILE_EXTENT_REG) |
| 7309 | goto out; |
| 7310 | |
Miao Xie | e77751a | 2013-12-27 21:11:50 +0800 | [diff] [blame] | 7311 | extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi); |
| 7312 | if (extent_end <= offset) |
| 7313 | goto out; |
| 7314 | |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7315 | disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 7316 | if (disk_bytenr == 0) |
| 7317 | goto out; |
| 7318 | |
| 7319 | if (btrfs_file_extent_compression(leaf, fi) || |
| 7320 | btrfs_file_extent_encryption(leaf, fi) || |
| 7321 | btrfs_file_extent_other_encoding(leaf, fi)) |
| 7322 | goto out; |
| 7323 | |
Ethan Lien | 78d4295 | 2018-05-17 14:58:29 +0800 | [diff] [blame] | 7324 | /* |
| 7325 | * Do the same check as in btrfs_cross_ref_exist but without the |
| 7326 | * unnecessary search. |
| 7327 | */ |
| 7328 | if (btrfs_file_extent_generation(leaf, fi) <= |
| 7329 | btrfs_root_last_snapshot(&root->root_item)) |
| 7330 | goto out; |
| 7331 | |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7332 | backref_offset = btrfs_file_extent_offset(leaf, fi); |
| 7333 | |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 7334 | if (orig_start) { |
| 7335 | *orig_start = key.offset - backref_offset; |
| 7336 | *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi); |
| 7337 | *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi); |
| 7338 | } |
Josef Bacik | eb384b5 | 2013-04-24 16:32:55 -0400 | [diff] [blame] | 7339 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 7340 | if (btrfs_extent_readonly(fs_info, disk_bytenr)) |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7341 | goto out; |
Miao Xie | 7b2b708 | 2014-02-27 13:58:05 +0800 | [diff] [blame] | 7342 | |
| 7343 | num_bytes = min(offset + *len, extent_end) - offset; |
| 7344 | if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) { |
| 7345 | u64 range_end; |
| 7346 | |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 7347 | range_end = round_up(offset + num_bytes, |
| 7348 | root->fs_info->sectorsize) - 1; |
Miao Xie | 7b2b708 | 2014-02-27 13:58:05 +0800 | [diff] [blame] | 7349 | ret = test_range_bit(io_tree, offset, range_end, |
| 7350 | EXTENT_DELALLOC, 0, NULL); |
| 7351 | if (ret) { |
| 7352 | ret = -EAGAIN; |
| 7353 | goto out; |
| 7354 | } |
| 7355 | } |
| 7356 | |
Josef Bacik | 1bda19e | 2013-10-18 12:10:36 -0400 | [diff] [blame] | 7357 | btrfs_release_path(path); |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7358 | |
| 7359 | /* |
| 7360 | * look for other files referencing this extent, if we |
| 7361 | * find any we must cow |
| 7362 | */ |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 7363 | |
Liu Bo | e4c3b2d | 2017-01-30 12:25:28 -0800 | [diff] [blame] | 7364 | ret = btrfs_cross_ref_exist(root, btrfs_ino(BTRFS_I(inode)), |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 7365 | key.offset - backref_offset, disk_bytenr); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 7366 | if (ret) { |
| 7367 | ret = 0; |
| 7368 | goto out; |
| 7369 | } |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7370 | |
| 7371 | /* |
| 7372 | * adjust disk_bytenr and num_bytes to cover just the bytes |
| 7373 | * in this extent we are about to write. If there |
| 7374 | * are any csums in that range we have to cow in order |
| 7375 | * to keep the csums correct |
| 7376 | */ |
| 7377 | disk_bytenr += backref_offset; |
| 7378 | disk_bytenr += offset - key.offset; |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 7379 | if (csum_exist_in_range(fs_info, disk_bytenr, num_bytes)) |
| 7380 | goto out; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7381 | /* |
| 7382 | * all of the above have passed, it is safe to overwrite this extent |
| 7383 | * without cow |
| 7384 | */ |
Josef Bacik | eb384b5 | 2013-04-24 16:32:55 -0400 | [diff] [blame] | 7385 | *len = num_bytes; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7386 | ret = 1; |
| 7387 | out: |
| 7388 | btrfs_free_path(path); |
| 7389 | return ret; |
| 7390 | } |
| 7391 | |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7392 | static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend, |
| 7393 | struct extent_state **cached_state, int writing) |
| 7394 | { |
| 7395 | struct btrfs_ordered_extent *ordered; |
| 7396 | int ret = 0; |
| 7397 | |
| 7398 | while (1) { |
| 7399 | lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend, |
David Sterba | ff13db4 | 2015-12-03 14:30:40 +0100 | [diff] [blame] | 7400 | cached_state); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7401 | /* |
| 7402 | * 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] | 7403 | * 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] | 7404 | * extents in this range. |
| 7405 | */ |
Nikolay Borisov | a776c6f | 2017-02-20 13:50:49 +0200 | [diff] [blame] | 7406 | ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), lockstart, |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7407 | lockend - lockstart + 1); |
| 7408 | |
| 7409 | /* |
| 7410 | * We need to make sure there are no buffered pages in this |
| 7411 | * range either, we could have raced between the invalidate in |
| 7412 | * generic_file_direct_write and locking the extent. The |
| 7413 | * invalidate needs to happen so that reads after a write do not |
| 7414 | * get stale data. |
| 7415 | */ |
Alex Gartrell | fc4adbff | 2014-05-20 13:07:56 -0700 | [diff] [blame] | 7416 | if (!ordered && |
David Sterba | 051c98e | 2018-03-07 15:33:22 +0100 | [diff] [blame] | 7417 | (!writing || !filemap_range_has_page(inode->i_mapping, |
| 7418 | lockstart, lockend))) |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7419 | break; |
| 7420 | |
| 7421 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend, |
David Sterba | e43bbe5 | 2017-12-12 21:43:52 +0100 | [diff] [blame] | 7422 | cached_state); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7423 | |
| 7424 | if (ordered) { |
Filipe Manana | ade7702 | 2016-02-18 14:28:55 +0000 | [diff] [blame] | 7425 | /* |
| 7426 | * If we are doing a DIO read and the ordered extent we |
| 7427 | * found is for a buffered write, we can not wait for it |
| 7428 | * to complete and retry, because if we do so we can |
| 7429 | * deadlock with concurrent buffered writes on page |
| 7430 | * locks. This happens only if our DIO read covers more |
| 7431 | * than one extent map, if at this point has already |
| 7432 | * created an ordered extent for a previous extent map |
| 7433 | * and locked its range in the inode's io tree, and a |
| 7434 | * concurrent write against that previous extent map's |
| 7435 | * range and this range started (we unlock the ranges |
| 7436 | * in the io tree only when the bios complete and |
| 7437 | * buffered writes always lock pages before attempting |
| 7438 | * to lock range in the io tree). |
| 7439 | */ |
| 7440 | if (writing || |
| 7441 | test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags)) |
| 7442 | btrfs_start_ordered_extent(inode, ordered, 1); |
| 7443 | else |
| 7444 | ret = -ENOTBLK; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7445 | btrfs_put_ordered_extent(ordered); |
| 7446 | } else { |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7447 | /* |
Filipe Manana | b850ae1 | 2015-12-08 16:23:16 +0000 | [diff] [blame] | 7448 | * We could trigger writeback for this range (and wait |
| 7449 | * for it to complete) and then invalidate the pages for |
| 7450 | * this range (through invalidate_inode_pages2_range()), |
| 7451 | * but that can lead us to a deadlock with a concurrent |
| 7452 | * call to readpages() (a buffered read or a defrag call |
| 7453 | * triggered a readahead) on a page lock due to an |
| 7454 | * ordered dio extent we created before but did not have |
| 7455 | * yet a corresponding bio submitted (whence it can not |
| 7456 | * complete), which makes readpages() wait for that |
| 7457 | * ordered extent to complete while holding a lock on |
| 7458 | * that page. |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7459 | */ |
Filipe Manana | b850ae1 | 2015-12-08 16:23:16 +0000 | [diff] [blame] | 7460 | ret = -ENOTBLK; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7461 | } |
| 7462 | |
Filipe Manana | ade7702 | 2016-02-18 14:28:55 +0000 | [diff] [blame] | 7463 | if (ret) |
| 7464 | break; |
| 7465 | |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7466 | cond_resched(); |
| 7467 | } |
| 7468 | |
| 7469 | return ret; |
| 7470 | } |
| 7471 | |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 7472 | /* The callers of this must take lock_extent() */ |
| 7473 | static struct extent_map *create_io_em(struct inode *inode, u64 start, u64 len, |
| 7474 | u64 orig_start, u64 block_start, |
| 7475 | u64 block_len, u64 orig_block_len, |
| 7476 | u64 ram_bytes, int compress_type, |
| 7477 | int type) |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7478 | { |
| 7479 | struct extent_map_tree *em_tree; |
| 7480 | struct extent_map *em; |
| 7481 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 7482 | int ret; |
| 7483 | |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 7484 | ASSERT(type == BTRFS_ORDERED_PREALLOC || |
| 7485 | type == BTRFS_ORDERED_COMPRESSED || |
| 7486 | type == BTRFS_ORDERED_NOCOW || |
Liu Bo | 1af4a0a | 2017-02-13 15:35:09 -0800 | [diff] [blame] | 7487 | type == BTRFS_ORDERED_REGULAR); |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 7488 | |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7489 | em_tree = &BTRFS_I(inode)->extent_tree; |
| 7490 | em = alloc_extent_map(); |
| 7491 | if (!em) |
| 7492 | return ERR_PTR(-ENOMEM); |
| 7493 | |
| 7494 | em->start = start; |
| 7495 | em->orig_start = orig_start; |
| 7496 | em->len = len; |
| 7497 | em->block_len = block_len; |
| 7498 | em->block_start = block_start; |
| 7499 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 7500 | em->orig_block_len = orig_block_len; |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 7501 | em->ram_bytes = ram_bytes; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 7502 | em->generation = -1; |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7503 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
Liu Bo | 1af4a0a | 2017-02-13 15:35:09 -0800 | [diff] [blame] | 7504 | if (type == BTRFS_ORDERED_PREALLOC) { |
Josef Bacik | b11e234 | 2012-12-03 10:58:15 -0500 | [diff] [blame] | 7505 | set_bit(EXTENT_FLAG_FILLING, &em->flags); |
Liu Bo | 1af4a0a | 2017-02-13 15:35:09 -0800 | [diff] [blame] | 7506 | } else if (type == BTRFS_ORDERED_COMPRESSED) { |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 7507 | set_bit(EXTENT_FLAG_COMPRESSED, &em->flags); |
| 7508 | em->compress_type = compress_type; |
| 7509 | } |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7510 | |
| 7511 | do { |
Nikolay Borisov | dcdbc05 | 2017-02-20 13:50:45 +0200 | [diff] [blame] | 7512 | btrfs_drop_extent_cache(BTRFS_I(inode), em->start, |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7513 | em->start + em->len - 1, 0); |
| 7514 | write_lock(&em_tree->lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 7515 | ret = add_extent_mapping(em_tree, em, 1); |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7516 | write_unlock(&em_tree->lock); |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 7517 | /* |
| 7518 | * The caller has taken lock_extent(), who could race with us |
| 7519 | * to add em? |
| 7520 | */ |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7521 | } while (ret == -EEXIST); |
| 7522 | |
| 7523 | if (ret) { |
| 7524 | free_extent_map(em); |
| 7525 | return ERR_PTR(ret); |
| 7526 | } |
| 7527 | |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 7528 | /* 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] | 7529 | return em; |
| 7530 | } |
| 7531 | |
Nikolay Borisov | 1c8d017 | 2018-05-02 15:19:32 +0300 | [diff] [blame] | 7532 | |
| 7533 | static int btrfs_get_blocks_direct_read(struct extent_map *em, |
| 7534 | struct buffer_head *bh_result, |
| 7535 | struct inode *inode, |
| 7536 | u64 start, u64 len) |
| 7537 | { |
| 7538 | if (em->block_start == EXTENT_MAP_HOLE || |
| 7539 | test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) |
| 7540 | return -ENOENT; |
| 7541 | |
| 7542 | len = min(len, em->len - (start - em->start)); |
| 7543 | |
| 7544 | bh_result->b_blocknr = (em->block_start + (start - em->start)) >> |
| 7545 | inode->i_blkbits; |
| 7546 | bh_result->b_size = len; |
| 7547 | bh_result->b_bdev = em->bdev; |
| 7548 | set_buffer_mapped(bh_result); |
| 7549 | |
| 7550 | return 0; |
| 7551 | } |
| 7552 | |
Nikolay Borisov | c5794e5 | 2018-05-02 15:19:33 +0300 | [diff] [blame] | 7553 | static int btrfs_get_blocks_direct_write(struct extent_map **map, |
| 7554 | struct buffer_head *bh_result, |
| 7555 | struct inode *inode, |
| 7556 | struct btrfs_dio_data *dio_data, |
| 7557 | u64 start, u64 len) |
| 7558 | { |
| 7559 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
| 7560 | struct extent_map *em = *map; |
| 7561 | int ret = 0; |
| 7562 | |
| 7563 | /* |
| 7564 | * We don't allocate a new extent in the following cases |
| 7565 | * |
| 7566 | * 1) The inode is marked as NODATACOW. In this case we'll just use the |
| 7567 | * existing extent. |
| 7568 | * 2) The extent is marked as PREALLOC. We're good to go here and can |
| 7569 | * just use the extent. |
| 7570 | * |
| 7571 | */ |
| 7572 | if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) || |
| 7573 | ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) && |
| 7574 | em->block_start != EXTENT_MAP_HOLE)) { |
| 7575 | int type; |
| 7576 | u64 block_start, orig_start, orig_block_len, ram_bytes; |
| 7577 | |
| 7578 | if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) |
| 7579 | type = BTRFS_ORDERED_PREALLOC; |
| 7580 | else |
| 7581 | type = BTRFS_ORDERED_NOCOW; |
| 7582 | len = min(len, em->len - (start - em->start)); |
| 7583 | block_start = em->block_start + (start - em->start); |
| 7584 | |
| 7585 | if (can_nocow_extent(inode, start, &len, &orig_start, |
| 7586 | &orig_block_len, &ram_bytes) == 1 && |
| 7587 | btrfs_inc_nocow_writers(fs_info, block_start)) { |
| 7588 | struct extent_map *em2; |
| 7589 | |
| 7590 | em2 = btrfs_create_dio_extent(inode, start, len, |
| 7591 | orig_start, block_start, |
| 7592 | len, orig_block_len, |
| 7593 | ram_bytes, type); |
| 7594 | btrfs_dec_nocow_writers(fs_info, block_start); |
| 7595 | if (type == BTRFS_ORDERED_PREALLOC) { |
| 7596 | free_extent_map(em); |
| 7597 | *map = em = em2; |
| 7598 | } |
| 7599 | |
| 7600 | if (em2 && IS_ERR(em2)) { |
| 7601 | ret = PTR_ERR(em2); |
| 7602 | goto out; |
| 7603 | } |
| 7604 | /* |
| 7605 | * For inode marked NODATACOW or extent marked PREALLOC, |
| 7606 | * use the existing or preallocated extent, so does not |
| 7607 | * need to adjust btrfs_space_info's bytes_may_use. |
| 7608 | */ |
| 7609 | btrfs_free_reserved_data_space_noquota(inode, start, |
| 7610 | len); |
| 7611 | goto skip_cow; |
| 7612 | } |
| 7613 | } |
| 7614 | |
| 7615 | /* this will cow the extent */ |
| 7616 | len = bh_result->b_size; |
| 7617 | free_extent_map(em); |
| 7618 | *map = em = btrfs_new_extent_direct(inode, start, len); |
| 7619 | if (IS_ERR(em)) { |
| 7620 | ret = PTR_ERR(em); |
| 7621 | goto out; |
| 7622 | } |
| 7623 | |
| 7624 | len = min(len, em->len - (start - em->start)); |
| 7625 | |
| 7626 | skip_cow: |
| 7627 | bh_result->b_blocknr = (em->block_start + (start - em->start)) >> |
| 7628 | inode->i_blkbits; |
| 7629 | bh_result->b_size = len; |
| 7630 | bh_result->b_bdev = em->bdev; |
| 7631 | set_buffer_mapped(bh_result); |
| 7632 | |
| 7633 | if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) |
| 7634 | set_buffer_new(bh_result); |
| 7635 | |
| 7636 | /* |
| 7637 | * Need to update the i_size under the extent lock so buffered |
| 7638 | * readers will get the updated i_size when we unlock. |
| 7639 | */ |
| 7640 | if (!dio_data->overwrite && start + len > i_size_read(inode)) |
| 7641 | i_size_write(inode, start + len); |
| 7642 | |
| 7643 | WARN_ON(dio_data->reserve < len); |
| 7644 | dio_data->reserve -= len; |
| 7645 | dio_data->unsubmitted_oe_range_end = start + len; |
| 7646 | current->journal_info = dio_data; |
| 7647 | out: |
| 7648 | return ret; |
| 7649 | } |
| 7650 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7651 | static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock, |
| 7652 | struct buffer_head *bh_result, int create) |
| 7653 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7654 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7655 | struct extent_map *em; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7656 | struct extent_state *cached_state = NULL; |
chandan | 50745b0 | 2015-08-28 21:10:13 +0530 | [diff] [blame] | 7657 | struct btrfs_dio_data *dio_data = NULL; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7658 | u64 start = iblock << inode->i_blkbits; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7659 | u64 lockstart, lockend; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7660 | u64 len = bh_result->b_size; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7661 | int unlock_bits = EXTENT_LOCKED; |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 7662 | int ret = 0; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7663 | |
Miao Xie | 172a504 | 2013-02-21 02:48:22 -0700 | [diff] [blame] | 7664 | if (create) |
Josef Bacik | 3266789 | 2015-02-11 15:08:58 -0500 | [diff] [blame] | 7665 | unlock_bits |= EXTENT_DIRTY; |
Miao Xie | 172a504 | 2013-02-21 02:48:22 -0700 | [diff] [blame] | 7666 | else |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7667 | len = min_t(u64, len, fs_info->sectorsize); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7668 | |
Josef Bacik | c329861 | 2012-08-03 16:49:19 -0400 | [diff] [blame] | 7669 | lockstart = start; |
| 7670 | lockend = start + len - 1; |
| 7671 | |
Josef Bacik | e1cbbfa | 2015-03-17 10:52:28 -0400 | [diff] [blame] | 7672 | if (current->journal_info) { |
| 7673 | /* |
| 7674 | * Need to pull our outstanding extents and set journal_info to NULL so |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 7675 | * that anything that needs to check if there's a transaction doesn't get |
Josef Bacik | e1cbbfa | 2015-03-17 10:52:28 -0400 | [diff] [blame] | 7676 | * confused. |
| 7677 | */ |
chandan | 50745b0 | 2015-08-28 21:10:13 +0530 | [diff] [blame] | 7678 | dio_data = current->journal_info; |
Josef Bacik | e1cbbfa | 2015-03-17 10:52:28 -0400 | [diff] [blame] | 7679 | current->journal_info = NULL; |
| 7680 | } |
| 7681 | |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7682 | /* |
| 7683 | * If this errors out it's because we couldn't invalidate pagecache for |
| 7684 | * this range and we need to fallback to buffered. |
| 7685 | */ |
Filipe Manana | 9c9464c | 2015-11-04 09:52:04 +0000 | [diff] [blame] | 7686 | if (lock_extent_direct(inode, lockstart, lockend, &cached_state, |
| 7687 | create)) { |
| 7688 | ret = -ENOTBLK; |
| 7689 | goto err; |
| 7690 | } |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7691 | |
Nikolay Borisov | fc4f21b1 | 2017-02-20 13:51:06 +0200 | [diff] [blame] | 7692 | em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len, 0); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7693 | if (IS_ERR(em)) { |
| 7694 | ret = PTR_ERR(em); |
| 7695 | goto unlock_err; |
| 7696 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7697 | |
| 7698 | /* |
| 7699 | * Ok for INLINE and COMPRESSED extents we need to fallback on buffered |
| 7700 | * io. INLINE is special, and we could probably kludge it in here, but |
| 7701 | * it's still buffered so for safety lets just fall back to the generic |
| 7702 | * buffered path. |
| 7703 | * |
| 7704 | * For COMPRESSED we _have_ to read the entire extent in so we can |
| 7705 | * decompress it, so there will be buffering required no matter what we |
| 7706 | * do, so go ahead and fallback to buffered. |
| 7707 | * |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 7708 | * 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] | 7709 | * to buffered IO. Don't blame me, this is the price we pay for using |
| 7710 | * the generic code. |
| 7711 | */ |
| 7712 | if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) || |
| 7713 | em->block_start == EXTENT_MAP_INLINE) { |
| 7714 | free_extent_map(em); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7715 | ret = -ENOTBLK; |
| 7716 | goto unlock_err; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7717 | } |
| 7718 | |
Nikolay Borisov | c5794e5 | 2018-05-02 15:19:33 +0300 | [diff] [blame] | 7719 | if (create) { |
| 7720 | ret = btrfs_get_blocks_direct_write(&em, bh_result, inode, |
| 7721 | dio_data, start, len); |
| 7722 | if (ret < 0) |
| 7723 | goto unlock_err; |
| 7724 | |
| 7725 | /* clear and unlock the entire range */ |
| 7726 | clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend, |
| 7727 | unlock_bits, 1, 0, &cached_state); |
| 7728 | } else { |
Nikolay Borisov | 1c8d017 | 2018-05-02 15:19:32 +0300 | [diff] [blame] | 7729 | ret = btrfs_get_blocks_direct_read(em, bh_result, inode, |
| 7730 | start, len); |
| 7731 | /* Can be negative only if we read from a hole */ |
| 7732 | if (ret < 0) { |
| 7733 | ret = 0; |
| 7734 | free_extent_map(em); |
| 7735 | goto unlock_err; |
| 7736 | } |
| 7737 | /* |
| 7738 | * We need to unlock only the end area that we aren't using. |
| 7739 | * The rest is going to be unlocked by the endio routine. |
| 7740 | */ |
| 7741 | lockstart = start + bh_result->b_size; |
| 7742 | if (lockstart < lockend) { |
| 7743 | clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, |
| 7744 | lockend, unlock_bits, 1, 0, |
| 7745 | &cached_state); |
| 7746 | } else { |
| 7747 | free_extent_state(cached_state); |
| 7748 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7749 | } |
| 7750 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7751 | free_extent_map(em); |
| 7752 | |
| 7753 | return 0; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7754 | |
| 7755 | unlock_err: |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7756 | clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend, |
David Sterba | ae0f162 | 2017-10-31 16:37:52 +0100 | [diff] [blame] | 7757 | unlock_bits, 1, 0, &cached_state); |
Filipe Manana | 9c9464c | 2015-11-04 09:52:04 +0000 | [diff] [blame] | 7758 | err: |
chandan | 50745b0 | 2015-08-28 21:10:13 +0530 | [diff] [blame] | 7759 | if (dio_data) |
| 7760 | current->journal_info = dio_data; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7761 | return ret; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7762 | } |
| 7763 | |
Omar Sandoval | 58efbc9 | 2017-08-22 23:45:59 -0700 | [diff] [blame] | 7764 | static inline blk_status_t submit_dio_repair_bio(struct inode *inode, |
| 7765 | struct bio *bio, |
| 7766 | int mirror_num) |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7767 | { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 7768 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Omar Sandoval | 58efbc9 | 2017-08-22 23:45:59 -0700 | [diff] [blame] | 7769 | blk_status_t ret; |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7770 | |
Mike Christie | 37226b2 | 2016-06-05 14:31:52 -0500 | [diff] [blame] | 7771 | BUG_ON(bio_op(bio) == REQ_OP_WRITE); |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7772 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 7773 | ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DIO_REPAIR); |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7774 | if (ret) |
Nikolay Borisov | ea057f6 | 2017-12-13 10:25:38 +0200 | [diff] [blame] | 7775 | return ret; |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7776 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 7777 | ret = btrfs_map_bio(fs_info, bio, mirror_num, 0); |
Nikolay Borisov | ea057f6 | 2017-12-13 10:25:38 +0200 | [diff] [blame] | 7778 | |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7779 | return ret; |
| 7780 | } |
| 7781 | |
| 7782 | static int btrfs_check_dio_repairable(struct inode *inode, |
| 7783 | struct bio *failed_bio, |
| 7784 | struct io_failure_record *failrec, |
| 7785 | int failed_mirror) |
| 7786 | { |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 7787 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7788 | int num_copies; |
| 7789 | |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 7790 | num_copies = btrfs_num_copies(fs_info, failrec->logical, failrec->len); |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7791 | if (num_copies == 1) { |
| 7792 | /* |
| 7793 | * we only have a single copy of the data, so don't bother with |
| 7794 | * all the retry and error correction code that follows. no |
| 7795 | * matter what the error is, it is very likely to persist. |
| 7796 | */ |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 7797 | btrfs_debug(fs_info, |
| 7798 | "Check DIO Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d", |
| 7799 | num_copies, failrec->this_mirror, failed_mirror); |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7800 | return 0; |
| 7801 | } |
| 7802 | |
| 7803 | failrec->failed_mirror = failed_mirror; |
| 7804 | failrec->this_mirror++; |
| 7805 | if (failrec->this_mirror == failed_mirror) |
| 7806 | failrec->this_mirror++; |
| 7807 | |
| 7808 | if (failrec->this_mirror > num_copies) { |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 7809 | btrfs_debug(fs_info, |
| 7810 | "Check DIO Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d", |
| 7811 | num_copies, failrec->this_mirror, failed_mirror); |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7812 | return 0; |
| 7813 | } |
| 7814 | |
| 7815 | return 1; |
| 7816 | } |
| 7817 | |
Omar Sandoval | 58efbc9 | 2017-08-22 23:45:59 -0700 | [diff] [blame] | 7818 | static blk_status_t dio_read_error(struct inode *inode, struct bio *failed_bio, |
| 7819 | struct page *page, unsigned int pgoff, |
| 7820 | u64 start, u64 end, int failed_mirror, |
| 7821 | bio_end_io_t *repair_endio, void *repair_arg) |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7822 | { |
| 7823 | struct io_failure_record *failrec; |
Josef Bacik | 7870d08 | 2017-05-05 11:57:15 -0400 | [diff] [blame] | 7824 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
| 7825 | struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree; |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7826 | struct bio *bio; |
| 7827 | int isector; |
David Sterba | f1c77c5 | 2017-06-06 19:03:49 +0200 | [diff] [blame] | 7828 | unsigned int read_mode = 0; |
Liu Bo | 17347ce | 2017-05-15 15:33:27 -0700 | [diff] [blame] | 7829 | int segs; |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7830 | int ret; |
Omar Sandoval | 58efbc9 | 2017-08-22 23:45:59 -0700 | [diff] [blame] | 7831 | blk_status_t status; |
Ming Lei | c16a8ac | 2017-12-18 20:22:12 +0800 | [diff] [blame] | 7832 | struct bio_vec bvec; |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7833 | |
Mike Christie | 37226b2 | 2016-06-05 14:31:52 -0500 | [diff] [blame] | 7834 | BUG_ON(bio_op(failed_bio) == REQ_OP_WRITE); |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7835 | |
| 7836 | ret = btrfs_get_io_failure_record(inode, start, end, &failrec); |
| 7837 | if (ret) |
Omar Sandoval | 58efbc9 | 2017-08-22 23:45:59 -0700 | [diff] [blame] | 7838 | return errno_to_blk_status(ret); |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7839 | |
| 7840 | ret = btrfs_check_dio_repairable(inode, failed_bio, failrec, |
| 7841 | failed_mirror); |
| 7842 | if (!ret) { |
Josef Bacik | 7870d08 | 2017-05-05 11:57:15 -0400 | [diff] [blame] | 7843 | free_io_failure(failure_tree, io_tree, failrec); |
Omar Sandoval | 58efbc9 | 2017-08-22 23:45:59 -0700 | [diff] [blame] | 7844 | return BLK_STS_IOERR; |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7845 | } |
| 7846 | |
Liu Bo | 17347ce | 2017-05-15 15:33:27 -0700 | [diff] [blame] | 7847 | segs = bio_segments(failed_bio); |
Ming Lei | c16a8ac | 2017-12-18 20:22:12 +0800 | [diff] [blame] | 7848 | bio_get_first_bvec(failed_bio, &bvec); |
Liu Bo | 17347ce | 2017-05-15 15:33:27 -0700 | [diff] [blame] | 7849 | if (segs > 1 || |
Ming Lei | c16a8ac | 2017-12-18 20:22:12 +0800 | [diff] [blame] | 7850 | (bvec.bv_len > btrfs_inode_sectorsize(inode))) |
Christoph Hellwig | 70fd761 | 2016-11-01 07:40:10 -0600 | [diff] [blame] | 7851 | read_mode |= REQ_FAILFAST_DEV; |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7852 | |
| 7853 | isector = start - btrfs_io_bio(failed_bio)->logical; |
| 7854 | isector >>= inode->i_sb->s_blocksize_bits; |
| 7855 | bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page, |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 7856 | pgoff, isector, repair_endio, repair_arg); |
David Sterba | ebcc326 | 2018-06-29 10:56:53 +0200 | [diff] [blame] | 7857 | bio->bi_opf = REQ_OP_READ | read_mode; |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7858 | |
| 7859 | btrfs_debug(BTRFS_I(inode)->root->fs_info, |
David Sterba | 913e153 | 2017-07-13 15:32:18 +0200 | [diff] [blame] | 7860 | "repair DIO read error: submitting new dio read[%#x] to this_mirror=%d, in_validation=%d", |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7861 | read_mode, failrec->this_mirror, failrec->in_validation); |
| 7862 | |
Omar Sandoval | 58efbc9 | 2017-08-22 23:45:59 -0700 | [diff] [blame] | 7863 | status = submit_dio_repair_bio(inode, bio, failrec->this_mirror); |
| 7864 | if (status) { |
Josef Bacik | 7870d08 | 2017-05-05 11:57:15 -0400 | [diff] [blame] | 7865 | free_io_failure(failure_tree, io_tree, failrec); |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7866 | bio_put(bio); |
| 7867 | } |
| 7868 | |
Omar Sandoval | 58efbc9 | 2017-08-22 23:45:59 -0700 | [diff] [blame] | 7869 | return status; |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7870 | } |
| 7871 | |
| 7872 | struct btrfs_retry_complete { |
| 7873 | struct completion done; |
| 7874 | struct inode *inode; |
| 7875 | u64 start; |
| 7876 | int uptodate; |
| 7877 | }; |
| 7878 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 7879 | static void btrfs_retry_endio_nocsum(struct bio *bio) |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7880 | { |
| 7881 | struct btrfs_retry_complete *done = bio->bi_private; |
Josef Bacik | 7870d08 | 2017-05-05 11:57:15 -0400 | [diff] [blame] | 7882 | struct inode *inode = done->inode; |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7883 | struct bio_vec *bvec; |
Josef Bacik | 7870d08 | 2017-05-05 11:57:15 -0400 | [diff] [blame] | 7884 | struct extent_io_tree *io_tree, *failure_tree; |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7885 | int i; |
Ming Lei | 6dc4f10 | 2019-02-15 19:13:19 +0800 | [diff] [blame] | 7886 | struct bvec_iter_all iter_all; |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7887 | |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 7888 | if (bio->bi_status) |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7889 | goto end; |
| 7890 | |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 7891 | ASSERT(bio->bi_vcnt == 1); |
Josef Bacik | 7870d08 | 2017-05-05 11:57:15 -0400 | [diff] [blame] | 7892 | io_tree = &BTRFS_I(inode)->io_tree; |
| 7893 | failure_tree = &BTRFS_I(inode)->io_failure_tree; |
Ming Lei | 263663c | 2017-12-18 20:22:04 +0800 | [diff] [blame] | 7894 | ASSERT(bio_first_bvec_all(bio)->bv_len == btrfs_inode_sectorsize(inode)); |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 7895 | |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7896 | done->uptodate = 1; |
David Sterba | c09abff | 2017-07-13 18:10:07 +0200 | [diff] [blame] | 7897 | ASSERT(!bio_flagged(bio, BIO_CLONED)); |
Ming Lei | 6dc4f10 | 2019-02-15 19:13:19 +0800 | [diff] [blame] | 7898 | bio_for_each_segment_all(bvec, bio, i, iter_all) |
Josef Bacik | 7870d08 | 2017-05-05 11:57:15 -0400 | [diff] [blame] | 7899 | clean_io_failure(BTRFS_I(inode)->root->fs_info, failure_tree, |
| 7900 | io_tree, done->start, bvec->bv_page, |
| 7901 | btrfs_ino(BTRFS_I(inode)), 0); |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7902 | end: |
| 7903 | complete(&done->done); |
| 7904 | bio_put(bio); |
| 7905 | } |
| 7906 | |
Omar Sandoval | 58efbc9 | 2017-08-22 23:45:59 -0700 | [diff] [blame] | 7907 | static blk_status_t __btrfs_correct_data_nocsum(struct inode *inode, |
| 7908 | struct btrfs_io_bio *io_bio) |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7909 | { |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 7910 | struct btrfs_fs_info *fs_info; |
Liu Bo | 17347ce | 2017-05-15 15:33:27 -0700 | [diff] [blame] | 7911 | struct bio_vec bvec; |
| 7912 | struct bvec_iter iter; |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7913 | struct btrfs_retry_complete done; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7914 | u64 start; |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 7915 | unsigned int pgoff; |
| 7916 | u32 sectorsize; |
| 7917 | int nr_sectors; |
Omar Sandoval | 58efbc9 | 2017-08-22 23:45:59 -0700 | [diff] [blame] | 7918 | blk_status_t ret; |
| 7919 | blk_status_t err = BLK_STS_OK; |
Miao Xie | dc380ae | 2014-09-12 18:43:55 +0800 | [diff] [blame] | 7920 | |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 7921 | fs_info = BTRFS_I(inode)->root->fs_info; |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 7922 | sectorsize = fs_info->sectorsize; |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 7923 | |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 7924 | start = io_bio->logical; |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7925 | done.inode = inode; |
Liu Bo | 17347ce | 2017-05-15 15:33:27 -0700 | [diff] [blame] | 7926 | io_bio->bio.bi_iter = io_bio->iter; |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7927 | |
Liu Bo | 17347ce | 2017-05-15 15:33:27 -0700 | [diff] [blame] | 7928 | bio_for_each_segment(bvec, &io_bio->bio, iter) { |
| 7929 | nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec.bv_len); |
| 7930 | pgoff = bvec.bv_offset; |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 7931 | |
| 7932 | next_block_or_try_again: |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7933 | done.uptodate = 0; |
| 7934 | done.start = start; |
| 7935 | init_completion(&done.done); |
| 7936 | |
Liu Bo | 17347ce | 2017-05-15 15:33:27 -0700 | [diff] [blame] | 7937 | ret = dio_read_error(inode, &io_bio->bio, bvec.bv_page, |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 7938 | pgoff, start, start + sectorsize - 1, |
| 7939 | io_bio->mirror_num, |
| 7940 | btrfs_retry_endio_nocsum, &done); |
Liu Bo | 629ebf4 | 2017-05-15 17:20:07 -0700 | [diff] [blame] | 7941 | if (ret) { |
| 7942 | err = ret; |
| 7943 | goto next; |
| 7944 | } |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7945 | |
David Sterba | 9c17f6c | 2017-07-19 19:26:45 +0200 | [diff] [blame] | 7946 | wait_for_completion_io(&done.done); |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7947 | |
| 7948 | if (!done.uptodate) { |
| 7949 | /* We might have another mirror, so try again */ |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 7950 | goto next_block_or_try_again; |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7951 | } |
| 7952 | |
Liu Bo | 629ebf4 | 2017-05-15 17:20:07 -0700 | [diff] [blame] | 7953 | next: |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 7954 | start += sectorsize; |
| 7955 | |
Liu Bo | 97bf5a5 | 2017-04-07 13:11:10 -0700 | [diff] [blame] | 7956 | nr_sectors--; |
| 7957 | if (nr_sectors) { |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 7958 | pgoff += sectorsize; |
Liu Bo | 97bf5a5 | 2017-04-07 13:11:10 -0700 | [diff] [blame] | 7959 | ASSERT(pgoff < PAGE_SIZE); |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 7960 | goto next_block_or_try_again; |
| 7961 | } |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7962 | } |
| 7963 | |
Liu Bo | 629ebf4 | 2017-05-15 17:20:07 -0700 | [diff] [blame] | 7964 | return err; |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7965 | } |
| 7966 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 7967 | static void btrfs_retry_endio(struct bio *bio) |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7968 | { |
| 7969 | struct btrfs_retry_complete *done = bio->bi_private; |
| 7970 | struct btrfs_io_bio *io_bio = btrfs_io_bio(bio); |
Josef Bacik | 7870d08 | 2017-05-05 11:57:15 -0400 | [diff] [blame] | 7971 | struct extent_io_tree *io_tree, *failure_tree; |
| 7972 | struct inode *inode = done->inode; |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7973 | struct bio_vec *bvec; |
| 7974 | int uptodate; |
| 7975 | int ret; |
| 7976 | int i; |
Ming Lei | 6dc4f10 | 2019-02-15 19:13:19 +0800 | [diff] [blame] | 7977 | struct bvec_iter_all iter_all; |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7978 | |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 7979 | if (bio->bi_status) |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7980 | goto end; |
| 7981 | |
| 7982 | uptodate = 1; |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 7983 | |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 7984 | ASSERT(bio->bi_vcnt == 1); |
Ming Lei | 263663c | 2017-12-18 20:22:04 +0800 | [diff] [blame] | 7985 | ASSERT(bio_first_bvec_all(bio)->bv_len == btrfs_inode_sectorsize(done->inode)); |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 7986 | |
Josef Bacik | 7870d08 | 2017-05-05 11:57:15 -0400 | [diff] [blame] | 7987 | io_tree = &BTRFS_I(inode)->io_tree; |
| 7988 | failure_tree = &BTRFS_I(inode)->io_failure_tree; |
| 7989 | |
David Sterba | c09abff | 2017-07-13 18:10:07 +0200 | [diff] [blame] | 7990 | ASSERT(!bio_flagged(bio, BIO_CLONED)); |
Ming Lei | 6dc4f10 | 2019-02-15 19:13:19 +0800 | [diff] [blame] | 7991 | bio_for_each_segment_all(bvec, bio, i, iter_all) { |
Josef Bacik | 7870d08 | 2017-05-05 11:57:15 -0400 | [diff] [blame] | 7992 | ret = __readpage_endio_check(inode, io_bio, i, bvec->bv_page, |
| 7993 | bvec->bv_offset, done->start, |
| 7994 | bvec->bv_len); |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7995 | if (!ret) |
Josef Bacik | 7870d08 | 2017-05-05 11:57:15 -0400 | [diff] [blame] | 7996 | clean_io_failure(BTRFS_I(inode)->root->fs_info, |
| 7997 | failure_tree, io_tree, done->start, |
| 7998 | bvec->bv_page, |
| 7999 | btrfs_ino(BTRFS_I(inode)), |
| 8000 | bvec->bv_offset); |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8001 | else |
| 8002 | uptodate = 0; |
| 8003 | } |
| 8004 | |
| 8005 | done->uptodate = uptodate; |
| 8006 | end: |
| 8007 | complete(&done->done); |
| 8008 | bio_put(bio); |
| 8009 | } |
| 8010 | |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 8011 | static blk_status_t __btrfs_subio_endio_read(struct inode *inode, |
| 8012 | struct btrfs_io_bio *io_bio, blk_status_t err) |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8013 | { |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 8014 | struct btrfs_fs_info *fs_info; |
Liu Bo | 17347ce | 2017-05-15 15:33:27 -0700 | [diff] [blame] | 8015 | struct bio_vec bvec; |
| 8016 | struct bvec_iter iter; |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8017 | struct btrfs_retry_complete done; |
| 8018 | u64 start; |
| 8019 | u64 offset = 0; |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 8020 | u32 sectorsize; |
| 8021 | int nr_sectors; |
| 8022 | unsigned int pgoff; |
| 8023 | int csum_pos; |
Liu Bo | ef7cdac1 | 2017-04-13 18:11:48 -0700 | [diff] [blame] | 8024 | bool uptodate = (err == 0); |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8025 | int ret; |
Omar Sandoval | 58efbc9 | 2017-08-22 23:45:59 -0700 | [diff] [blame] | 8026 | blk_status_t status; |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8027 | |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 8028 | fs_info = BTRFS_I(inode)->root->fs_info; |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 8029 | sectorsize = fs_info->sectorsize; |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 8030 | |
Omar Sandoval | 58efbc9 | 2017-08-22 23:45:59 -0700 | [diff] [blame] | 8031 | err = BLK_STS_OK; |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8032 | start = io_bio->logical; |
| 8033 | done.inode = inode; |
Liu Bo | 17347ce | 2017-05-15 15:33:27 -0700 | [diff] [blame] | 8034 | io_bio->bio.bi_iter = io_bio->iter; |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8035 | |
Liu Bo | 17347ce | 2017-05-15 15:33:27 -0700 | [diff] [blame] | 8036 | bio_for_each_segment(bvec, &io_bio->bio, iter) { |
| 8037 | nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec.bv_len); |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 8038 | |
Liu Bo | 17347ce | 2017-05-15 15:33:27 -0700 | [diff] [blame] | 8039 | pgoff = bvec.bv_offset; |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 8040 | next_block: |
Liu Bo | ef7cdac1 | 2017-04-13 18:11:48 -0700 | [diff] [blame] | 8041 | if (uptodate) { |
| 8042 | csum_pos = BTRFS_BYTES_TO_BLKS(fs_info, offset); |
| 8043 | ret = __readpage_endio_check(inode, io_bio, csum_pos, |
| 8044 | bvec.bv_page, pgoff, start, sectorsize); |
| 8045 | if (likely(!ret)) |
| 8046 | goto next; |
| 8047 | } |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8048 | try_again: |
| 8049 | done.uptodate = 0; |
| 8050 | done.start = start; |
| 8051 | init_completion(&done.done); |
| 8052 | |
Omar Sandoval | 58efbc9 | 2017-08-22 23:45:59 -0700 | [diff] [blame] | 8053 | status = dio_read_error(inode, &io_bio->bio, bvec.bv_page, |
| 8054 | pgoff, start, start + sectorsize - 1, |
| 8055 | io_bio->mirror_num, btrfs_retry_endio, |
| 8056 | &done); |
| 8057 | if (status) { |
| 8058 | err = status; |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8059 | goto next; |
| 8060 | } |
| 8061 | |
David Sterba | 9c17f6c | 2017-07-19 19:26:45 +0200 | [diff] [blame] | 8062 | wait_for_completion_io(&done.done); |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8063 | |
| 8064 | if (!done.uptodate) { |
| 8065 | /* We might have another mirror, so try again */ |
| 8066 | goto try_again; |
| 8067 | } |
| 8068 | next: |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 8069 | offset += sectorsize; |
| 8070 | start += sectorsize; |
| 8071 | |
| 8072 | ASSERT(nr_sectors); |
| 8073 | |
Liu Bo | 97bf5a5 | 2017-04-07 13:11:10 -0700 | [diff] [blame] | 8074 | nr_sectors--; |
| 8075 | if (nr_sectors) { |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 8076 | pgoff += sectorsize; |
Liu Bo | 97bf5a5 | 2017-04-07 13:11:10 -0700 | [diff] [blame] | 8077 | ASSERT(pgoff < PAGE_SIZE); |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 8078 | goto next_block; |
| 8079 | } |
Kent Overstreet | 2c30c71 | 2013-11-07 12:20:26 -0800 | [diff] [blame] | 8080 | } |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 8081 | |
| 8082 | return err; |
| 8083 | } |
| 8084 | |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 8085 | static blk_status_t btrfs_subio_endio_read(struct inode *inode, |
| 8086 | struct btrfs_io_bio *io_bio, blk_status_t err) |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8087 | { |
| 8088 | bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM; |
| 8089 | |
| 8090 | if (skip_csum) { |
| 8091 | if (unlikely(err)) |
| 8092 | return __btrfs_correct_data_nocsum(inode, io_bio); |
| 8093 | else |
Omar Sandoval | 58efbc9 | 2017-08-22 23:45:59 -0700 | [diff] [blame] | 8094 | return BLK_STS_OK; |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8095 | } else { |
| 8096 | return __btrfs_subio_endio_read(inode, io_bio, err); |
| 8097 | } |
| 8098 | } |
| 8099 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 8100 | static void btrfs_endio_direct_read(struct bio *bio) |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 8101 | { |
| 8102 | struct btrfs_dio_private *dip = bio->bi_private; |
| 8103 | struct inode *inode = dip->inode; |
| 8104 | struct bio *dio_bio; |
| 8105 | struct btrfs_io_bio *io_bio = btrfs_io_bio(bio); |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 8106 | blk_status_t err = bio->bi_status; |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 8107 | |
Liu Bo | 99c4e3b9 | 2017-09-15 15:06:51 -0600 | [diff] [blame] | 8108 | if (dip->flags & BTRFS_DIO_ORIG_BIO_SUBMITTED) |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8109 | err = btrfs_subio_endio_read(inode, io_bio, err); |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 8110 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8111 | unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset, |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 8112 | dip->logical_offset + dip->bytes - 1); |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 8113 | dio_bio = dip->dio_bio; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8114 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8115 | kfree(dip); |
Josef Bacik | c0da7aa | 2011-03-22 11:05:07 -0400 | [diff] [blame] | 8116 | |
Liu Bo | 99c4e3b9 | 2017-09-15 15:06:51 -0600 | [diff] [blame] | 8117 | dio_bio->bi_status = err; |
Christoph Hellwig | 4055351 | 2017-06-03 09:37:58 +0200 | [diff] [blame] | 8118 | dio_end_io(dio_bio); |
David Sterba | b3a0dd5 | 2018-11-22 17:16:49 +0100 | [diff] [blame] | 8119 | btrfs_io_bio_free_csum(io_bio); |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 8120 | bio_put(bio); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8121 | } |
| 8122 | |
Qu Wenruo | 52427260 | 2017-03-08 10:25:52 +0800 | [diff] [blame] | 8123 | static void __endio_write_update_ordered(struct inode *inode, |
| 8124 | const u64 offset, const u64 bytes, |
| 8125 | const bool uptodate) |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8126 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8127 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8128 | struct btrfs_ordered_extent *ordered = NULL; |
Qu Wenruo | 52427260 | 2017-03-08 10:25:52 +0800 | [diff] [blame] | 8129 | struct btrfs_workqueue *wq; |
| 8130 | btrfs_work_func_t func; |
Filipe Manana | 1454377 | 2015-11-24 16:23:54 +0000 | [diff] [blame] | 8131 | u64 ordered_offset = offset; |
| 8132 | u64 ordered_bytes = bytes; |
Naohiro Aota | 67c003f | 2017-09-01 17:59:07 +0900 | [diff] [blame] | 8133 | u64 last_offset; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8134 | |
Qu Wenruo | 52427260 | 2017-03-08 10:25:52 +0800 | [diff] [blame] | 8135 | if (btrfs_is_free_space_inode(BTRFS_I(inode))) { |
| 8136 | wq = fs_info->endio_freespace_worker; |
| 8137 | func = btrfs_freespace_write_helper; |
| 8138 | } else { |
| 8139 | wq = fs_info->endio_write_workers; |
| 8140 | func = btrfs_endio_write_helper; |
| 8141 | } |
| 8142 | |
Nikolay Borisov | b25f0d0 | 2018-04-11 11:21:17 +0300 | [diff] [blame] | 8143 | while (ordered_offset < offset + bytes) { |
| 8144 | last_offset = ordered_offset; |
| 8145 | if (btrfs_dec_test_first_ordered_pending(inode, &ordered, |
| 8146 | &ordered_offset, |
| 8147 | ordered_bytes, |
| 8148 | uptodate)) { |
| 8149 | btrfs_init_work(&ordered->work, func, |
| 8150 | finish_ordered_fn, |
| 8151 | NULL, NULL); |
| 8152 | btrfs_queue_work(wq, &ordered->work); |
| 8153 | } |
| 8154 | /* |
| 8155 | * If btrfs_dec_test_ordered_pending does not find any ordered |
| 8156 | * extent in the range, we can exit. |
| 8157 | */ |
| 8158 | if (ordered_offset == last_offset) |
| 8159 | return; |
| 8160 | /* |
| 8161 | * Our bio might span multiple ordered extents. In this case |
Andrea Gelmini | 52042d8 | 2018-11-28 12:05:13 +0100 | [diff] [blame] | 8162 | * we keep going until we have accounted the whole dio. |
Nikolay Borisov | b25f0d0 | 2018-04-11 11:21:17 +0300 | [diff] [blame] | 8163 | */ |
| 8164 | if (ordered_offset < offset + bytes) { |
| 8165 | ordered_bytes = offset + bytes - ordered_offset; |
| 8166 | ordered = NULL; |
| 8167 | } |
Chris Mason | 163cf09 | 2010-11-28 19:56:33 -0500 | [diff] [blame] | 8168 | } |
Filipe Manana | 1454377 | 2015-11-24 16:23:54 +0000 | [diff] [blame] | 8169 | } |
| 8170 | |
| 8171 | static void btrfs_endio_direct_write(struct bio *bio) |
| 8172 | { |
| 8173 | struct btrfs_dio_private *dip = bio->bi_private; |
| 8174 | struct bio *dio_bio = dip->dio_bio; |
| 8175 | |
Qu Wenruo | 52427260 | 2017-03-08 10:25:52 +0800 | [diff] [blame] | 8176 | __endio_write_update_ordered(dip->inode, dip->logical_offset, |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 8177 | dip->bytes, !bio->bi_status); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8178 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8179 | kfree(dip); |
Josef Bacik | c0da7aa | 2011-03-22 11:05:07 -0400 | [diff] [blame] | 8180 | |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 8181 | dio_bio->bi_status = bio->bi_status; |
Christoph Hellwig | 4055351 | 2017-06-03 09:37:58 +0200 | [diff] [blame] | 8182 | dio_end_io(dio_bio); |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 8183 | bio_put(bio); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8184 | } |
| 8185 | |
David Sterba | d0ee393 | 2018-03-08 14:35:48 +0100 | [diff] [blame] | 8186 | 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] | 8187 | struct bio *bio, u64 offset) |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 8188 | { |
Josef Bacik | c6100a4 | 2017-05-05 11:57:13 -0400 | [diff] [blame] | 8189 | struct inode *inode = private_data; |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 8190 | blk_status_t ret; |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 8191 | ret = btrfs_csum_one_bio(inode, bio, offset, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 8192 | BUG_ON(ret); /* -ENOMEM */ |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 8193 | return 0; |
| 8194 | } |
| 8195 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 8196 | static void btrfs_end_dio_bio(struct bio *bio) |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8197 | { |
| 8198 | struct btrfs_dio_private *dip = bio->bi_private; |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 8199 | blk_status_t err = bio->bi_status; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8200 | |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8201 | if (err) |
| 8202 | btrfs_warn(BTRFS_I(dip->inode)->root->fs_info, |
Mike Christie | 6296b96 | 2016-06-05 14:32:21 -0500 | [diff] [blame] | 8203 | "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] | 8204 | btrfs_ino(BTRFS_I(dip->inode)), bio_op(bio), |
| 8205 | bio->bi_opf, |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8206 | (unsigned long long)bio->bi_iter.bi_sector, |
| 8207 | bio->bi_iter.bi_size, err); |
| 8208 | |
| 8209 | if (dip->subio_endio) |
| 8210 | err = dip->subio_endio(dip->inode, btrfs_io_bio(bio), err); |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 8211 | |
| 8212 | if (err) { |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8213 | /* |
Nikolay Borisov | de224b7 | 2018-02-14 10:53:36 +0200 | [diff] [blame] | 8214 | * We want to perceive the errors flag being set before |
| 8215 | * decrementing the reference count. We don't need a barrier |
| 8216 | * since atomic operations with a return value are fully |
| 8217 | * ordered as per atomic_t.txt |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8218 | */ |
Nikolay Borisov | de224b7 | 2018-02-14 10:53:36 +0200 | [diff] [blame] | 8219 | dip->errors = 1; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8220 | } |
| 8221 | |
| 8222 | /* if there are more bios still pending for this dio, just exit */ |
| 8223 | if (!atomic_dec_and_test(&dip->pending_bios)) |
| 8224 | goto out; |
| 8225 | |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 8226 | if (dip->errors) { |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8227 | bio_io_error(dip->orig_bio); |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 8228 | } else { |
Anand Jain | 2dbe0c7 | 2017-10-14 08:35:56 +0800 | [diff] [blame] | 8229 | dip->dio_bio->bi_status = BLK_STS_OK; |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 8230 | bio_endio(dip->orig_bio); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8231 | } |
| 8232 | out: |
| 8233 | bio_put(bio); |
| 8234 | } |
| 8235 | |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 8236 | static inline blk_status_t btrfs_lookup_and_bind_dio_csum(struct inode *inode, |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 8237 | struct btrfs_dio_private *dip, |
| 8238 | struct bio *bio, |
| 8239 | u64 file_offset) |
| 8240 | { |
| 8241 | struct btrfs_io_bio *io_bio = btrfs_io_bio(bio); |
| 8242 | struct btrfs_io_bio *orig_io_bio = btrfs_io_bio(dip->orig_bio); |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 8243 | blk_status_t ret; |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 8244 | |
| 8245 | /* |
| 8246 | * We load all the csum data we need when we submit |
| 8247 | * the first bio to reduce the csum tree search and |
| 8248 | * contention. |
| 8249 | */ |
| 8250 | if (dip->logical_offset == file_offset) { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 8251 | ret = btrfs_lookup_bio_sums_dio(inode, dip->orig_bio, |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 8252 | file_offset); |
| 8253 | if (ret) |
| 8254 | return ret; |
| 8255 | } |
| 8256 | |
| 8257 | if (bio == dip->orig_bio) |
| 8258 | return 0; |
| 8259 | |
| 8260 | file_offset -= dip->logical_offset; |
| 8261 | file_offset >>= inode->i_sb->s_blocksize_bits; |
| 8262 | io_bio->csum = (u8 *)(((u32 *)orig_io_bio->csum) + file_offset); |
| 8263 | |
| 8264 | return 0; |
| 8265 | } |
| 8266 | |
David Sterba | d0ee393 | 2018-03-08 14:35:48 +0100 | [diff] [blame] | 8267 | static inline blk_status_t btrfs_submit_dio_bio(struct bio *bio, |
| 8268 | struct inode *inode, u64 file_offset, int async_submit) |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8269 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8270 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 8271 | struct btrfs_dio_private *dip = bio->bi_private; |
Mike Christie | 37226b2 | 2016-06-05 14:31:52 -0500 | [diff] [blame] | 8272 | bool write = bio_op(bio) == REQ_OP_WRITE; |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 8273 | blk_status_t ret; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8274 | |
Liu Bo | 4c274bc | 2017-11-01 17:19:27 -0600 | [diff] [blame] | 8275 | /* Check btrfs_submit_bio_hook() for rules about async submit. */ |
Josef Bacik | b812ce2 | 2012-11-16 13:56:32 -0500 | [diff] [blame] | 8276 | if (async_submit) |
| 8277 | async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers); |
| 8278 | |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 8279 | if (!write) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8280 | 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] | 8281 | if (ret) |
| 8282 | goto err; |
| 8283 | } |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8284 | |
Nikolay Borisov | e6961ca | 2017-08-03 15:44:58 +0300 | [diff] [blame] | 8285 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM) |
Josef Bacik | 1ae3993 | 2011-04-06 14:41:34 -0400 | [diff] [blame] | 8286 | goto map; |
| 8287 | |
| 8288 | if (write && async_submit) { |
Josef Bacik | c6100a4 | 2017-05-05 11:57:13 -0400 | [diff] [blame] | 8289 | ret = btrfs_wq_submit_bio(fs_info, bio, 0, 0, |
| 8290 | file_offset, inode, |
David Sterba | e288c08 | 2018-07-18 17:36:24 +0200 | [diff] [blame] | 8291 | btrfs_submit_bio_start_direct_io); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8292 | goto err; |
Josef Bacik | 1ae3993 | 2011-04-06 14:41:34 -0400 | [diff] [blame] | 8293 | } else if (write) { |
| 8294 | /* |
| 8295 | * If we aren't doing async submit, calculate the csum of the |
| 8296 | * bio now. |
| 8297 | */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 8298 | ret = btrfs_csum_one_bio(inode, bio, file_offset, 1); |
Josef Bacik | 1ae3993 | 2011-04-06 14:41:34 -0400 | [diff] [blame] | 8299 | if (ret) |
| 8300 | goto err; |
Miao Xie | 23ea8e5 | 2014-09-12 18:43:54 +0800 | [diff] [blame] | 8301 | } else { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 8302 | ret = btrfs_lookup_and_bind_dio_csum(inode, dip, bio, |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 8303 | file_offset); |
Tsutomu Itoh | c2db107 | 2011-03-01 06:48:31 +0000 | [diff] [blame] | 8304 | if (ret) |
| 8305 | goto err; |
| 8306 | } |
Josef Bacik | 1ae3993 | 2011-04-06 14:41:34 -0400 | [diff] [blame] | 8307 | map: |
Liu Bo | 9b4a9b2 | 2017-08-18 11:54:02 -0600 | [diff] [blame] | 8308 | ret = btrfs_map_bio(fs_info, bio, 0, 0); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8309 | err: |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8310 | return ret; |
| 8311 | } |
| 8312 | |
Nikolay Borisov | e6961ca | 2017-08-03 15:44:58 +0300 | [diff] [blame] | 8313 | static int btrfs_submit_direct_hook(struct btrfs_dio_private *dip) |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8314 | { |
| 8315 | struct inode *inode = dip->inode; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8316 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8317 | struct bio *bio; |
| 8318 | struct bio *orig_bio = dip->orig_bio; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 8319 | u64 start_sector = orig_bio->bi_iter.bi_sector; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8320 | u64 file_offset = dip->logical_offset; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8321 | u64 map_length; |
Josef Bacik | 1ae3993 | 2011-04-06 14:41:34 -0400 | [diff] [blame] | 8322 | int async_submit = 0; |
Liu Bo | 725130b | 2017-05-16 09:51:39 -0700 | [diff] [blame] | 8323 | u64 submit_len; |
| 8324 | int clone_offset = 0; |
| 8325 | int clone_len; |
Chandan Rajendra | 5f4dc8f | 2016-01-21 15:56:00 +0530 | [diff] [blame] | 8326 | int ret; |
Omar Sandoval | 58efbc9 | 2017-08-22 23:45:59 -0700 | [diff] [blame] | 8327 | blk_status_t status; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8328 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 8329 | map_length = orig_bio->bi_iter.bi_size; |
Liu Bo | 725130b | 2017-05-16 09:51:39 -0700 | [diff] [blame] | 8330 | submit_len = map_length; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8331 | ret = btrfs_map_block(fs_info, btrfs_op(orig_bio), start_sector << 9, |
| 8332 | &map_length, NULL, 0); |
Miao Xie | 7a5c3c9 | 2014-06-17 18:58:59 +0800 | [diff] [blame] | 8333 | if (ret) |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8334 | return -EIO; |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 8335 | |
Liu Bo | 725130b | 2017-05-16 09:51:39 -0700 | [diff] [blame] | 8336 | if (map_length >= submit_len) { |
Josef Bacik | 02f57c7 | 2011-04-06 14:25:44 -0400 | [diff] [blame] | 8337 | bio = orig_bio; |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 8338 | dip->flags |= BTRFS_DIO_ORIG_BIO_SUBMITTED; |
Josef Bacik | 02f57c7 | 2011-04-06 14:25:44 -0400 | [diff] [blame] | 8339 | goto submit; |
| 8340 | } |
| 8341 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 8342 | /* async crcs make it difficult to collect full stripe writes. */ |
Jeff Mahoney | 1b86826 | 2017-05-17 11:38:35 -0400 | [diff] [blame] | 8343 | if (btrfs_data_alloc_profile(fs_info) & BTRFS_BLOCK_GROUP_RAID56_MASK) |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 8344 | async_submit = 0; |
| 8345 | else |
| 8346 | async_submit = 1; |
| 8347 | |
Liu Bo | 725130b | 2017-05-16 09:51:39 -0700 | [diff] [blame] | 8348 | /* bio split */ |
| 8349 | ASSERT(map_length <= INT_MAX); |
Josef Bacik | 02f57c7 | 2011-04-06 14:25:44 -0400 | [diff] [blame] | 8350 | atomic_inc(&dip->pending_bios); |
Arnd Bergmann | 3c91ee6 | 2017-05-18 15:33:29 +0200 | [diff] [blame] | 8351 | do { |
Liu Bo | 725130b | 2017-05-16 09:51:39 -0700 | [diff] [blame] | 8352 | clone_len = min_t(int, submit_len, map_length); |
Josef Bacik | 02f57c7 | 2011-04-06 14:25:44 -0400 | [diff] [blame] | 8353 | |
Liu Bo | 725130b | 2017-05-16 09:51:39 -0700 | [diff] [blame] | 8354 | /* |
| 8355 | * This will never fail as it's passing GPF_NOFS and |
| 8356 | * the allocation is backed by btrfs_bioset. |
| 8357 | */ |
Liu Bo | e477094 | 2017-05-16 10:57:14 -0700 | [diff] [blame] | 8358 | bio = btrfs_bio_clone_partial(orig_bio, clone_offset, |
Liu Bo | 725130b | 2017-05-16 09:51:39 -0700 | [diff] [blame] | 8359 | clone_len); |
| 8360 | bio->bi_private = dip; |
| 8361 | bio->bi_end_io = btrfs_end_dio_bio; |
| 8362 | btrfs_io_bio(bio)->logical = file_offset; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8363 | |
Liu Bo | 725130b | 2017-05-16 09:51:39 -0700 | [diff] [blame] | 8364 | ASSERT(submit_len >= clone_len); |
| 8365 | submit_len -= clone_len; |
| 8366 | if (submit_len == 0) |
| 8367 | break; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8368 | |
Liu Bo | 725130b | 2017-05-16 09:51:39 -0700 | [diff] [blame] | 8369 | /* |
| 8370 | * Increase the count before we submit the bio so we know |
| 8371 | * the end IO handler won't happen before we increase the |
| 8372 | * count. Otherwise, the dip might get freed before we're |
| 8373 | * done setting it up. |
| 8374 | */ |
| 8375 | atomic_inc(&dip->pending_bios); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8376 | |
David Sterba | d0ee393 | 2018-03-08 14:35:48 +0100 | [diff] [blame] | 8377 | status = btrfs_submit_dio_bio(bio, inode, file_offset, |
Omar Sandoval | 58efbc9 | 2017-08-22 23:45:59 -0700 | [diff] [blame] | 8378 | async_submit); |
| 8379 | if (status) { |
Liu Bo | 725130b | 2017-05-16 09:51:39 -0700 | [diff] [blame] | 8380 | bio_put(bio); |
| 8381 | atomic_dec(&dip->pending_bios); |
| 8382 | goto out_err; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8383 | } |
Liu Bo | 725130b | 2017-05-16 09:51:39 -0700 | [diff] [blame] | 8384 | |
| 8385 | clone_offset += clone_len; |
| 8386 | start_sector += clone_len >> 9; |
| 8387 | file_offset += clone_len; |
| 8388 | |
| 8389 | map_length = submit_len; |
| 8390 | ret = btrfs_map_block(fs_info, btrfs_op(orig_bio), |
| 8391 | start_sector << 9, &map_length, NULL, 0); |
| 8392 | if (ret) |
| 8393 | goto out_err; |
Arnd Bergmann | 3c91ee6 | 2017-05-18 15:33:29 +0200 | [diff] [blame] | 8394 | } while (submit_len > 0); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8395 | |
Josef Bacik | 02f57c7 | 2011-04-06 14:25:44 -0400 | [diff] [blame] | 8396 | submit: |
David Sterba | d0ee393 | 2018-03-08 14:35:48 +0100 | [diff] [blame] | 8397 | status = btrfs_submit_dio_bio(bio, inode, file_offset, async_submit); |
Omar Sandoval | 58efbc9 | 2017-08-22 23:45:59 -0700 | [diff] [blame] | 8398 | if (!status) |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8399 | return 0; |
| 8400 | |
| 8401 | bio_put(bio); |
| 8402 | out_err: |
| 8403 | dip->errors = 1; |
| 8404 | /* |
Nikolay Borisov | de224b7 | 2018-02-14 10:53:36 +0200 | [diff] [blame] | 8405 | * Before atomic variable goto zero, we must make sure dip->errors is |
| 8406 | * perceived to be set. This ordering is ensured by the fact that an |
| 8407 | * atomic operations with a return value are fully ordered as per |
| 8408 | * atomic_t.txt |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8409 | */ |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8410 | if (atomic_dec_and_test(&dip->pending_bios)) |
| 8411 | bio_io_error(dip->orig_bio); |
| 8412 | |
| 8413 | /* bio_end_io() will handle error, so we needn't return it */ |
| 8414 | return 0; |
| 8415 | } |
| 8416 | |
Mike Christie | 8a4c1e4 | 2016-06-05 14:31:50 -0500 | [diff] [blame] | 8417 | static void btrfs_submit_direct(struct bio *dio_bio, struct inode *inode, |
| 8418 | loff_t file_offset) |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8419 | { |
Filipe Manana | 61de718 | 2015-07-01 12:13:10 +0100 | [diff] [blame] | 8420 | struct btrfs_dio_private *dip = NULL; |
Liu Bo | 3892ac9 | 2017-04-17 15:00:28 -0700 | [diff] [blame] | 8421 | struct bio *bio = NULL; |
| 8422 | struct btrfs_io_bio *io_bio; |
Mike Christie | 8a4c1e4 | 2016-06-05 14:31:50 -0500 | [diff] [blame] | 8423 | bool write = (bio_op(dio_bio) == REQ_OP_WRITE); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8424 | int ret = 0; |
| 8425 | |
David Sterba | 8b6c1d5 | 2017-06-02 17:48:13 +0200 | [diff] [blame] | 8426 | bio = btrfs_bio_clone(dio_bio); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8427 | |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 8428 | dip = kzalloc(sizeof(*dip), GFP_NOFS); |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 8429 | if (!dip) { |
| 8430 | ret = -ENOMEM; |
Filipe Manana | 61de718 | 2015-07-01 12:13:10 +0100 | [diff] [blame] | 8431 | goto free_ordered; |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 8432 | } |
| 8433 | |
| 8434 | dip->private = dio_bio->bi_private; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8435 | dip->inode = inode; |
| 8436 | dip->logical_offset = file_offset; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 8437 | dip->bytes = dio_bio->bi_iter.bi_size; |
| 8438 | dip->disk_bytenr = (u64)dio_bio->bi_iter.bi_sector << 9; |
Liu Bo | 3892ac9 | 2017-04-17 15:00:28 -0700 | [diff] [blame] | 8439 | bio->bi_private = dip; |
| 8440 | dip->orig_bio = bio; |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 8441 | dip->dio_bio = dio_bio; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8442 | atomic_set(&dip->pending_bios, 0); |
Liu Bo | 3892ac9 | 2017-04-17 15:00:28 -0700 | [diff] [blame] | 8443 | io_bio = btrfs_io_bio(bio); |
| 8444 | io_bio->logical = file_offset; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8445 | |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 8446 | if (write) { |
Liu Bo | 3892ac9 | 2017-04-17 15:00:28 -0700 | [diff] [blame] | 8447 | bio->bi_end_io = btrfs_endio_direct_write; |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 8448 | } else { |
Liu Bo | 3892ac9 | 2017-04-17 15:00:28 -0700 | [diff] [blame] | 8449 | bio->bi_end_io = btrfs_endio_direct_read; |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 8450 | dip->subio_endio = btrfs_subio_endio_read; |
| 8451 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8452 | |
Filipe Manana | f28a492 | 2015-12-08 19:23:20 +0000 | [diff] [blame] | 8453 | /* |
| 8454 | * Reset the range for unsubmitted ordered extents (to a 0 length range) |
| 8455 | * even if we fail to submit a bio, because in such case we do the |
| 8456 | * corresponding error handling below and it must not be done a second |
| 8457 | * time by btrfs_direct_IO(). |
| 8458 | */ |
| 8459 | if (write) { |
| 8460 | struct btrfs_dio_data *dio_data = current->journal_info; |
| 8461 | |
| 8462 | dio_data->unsubmitted_oe_range_end = dip->logical_offset + |
| 8463 | dip->bytes; |
| 8464 | dio_data->unsubmitted_oe_range_start = |
| 8465 | dio_data->unsubmitted_oe_range_end; |
| 8466 | } |
| 8467 | |
Nikolay Borisov | e6961ca | 2017-08-03 15:44:58 +0300 | [diff] [blame] | 8468 | ret = btrfs_submit_direct_hook(dip); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8469 | if (!ret) |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 8470 | return; |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 8471 | |
David Sterba | b3a0dd5 | 2018-11-22 17:16:49 +0100 | [diff] [blame] | 8472 | btrfs_io_bio_free_csum(io_bio); |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 8473 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8474 | free_ordered: |
| 8475 | /* |
Filipe Manana | 61de718 | 2015-07-01 12:13:10 +0100 | [diff] [blame] | 8476 | * If we arrived here it means either we failed to submit the dip |
| 8477 | * or we either failed to clone the dio_bio or failed to allocate the |
| 8478 | * dip. If we cloned the dio_bio and allocated the dip, we can just |
| 8479 | * call bio_endio against our io_bio so that we get proper resource |
| 8480 | * cleanup if we fail to submit the dip, otherwise, we must do the |
| 8481 | * same as btrfs_endio_direct_[write|read] because we can't call these |
| 8482 | * callbacks - they require an allocated dip and a clone of dio_bio. |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8483 | */ |
Liu Bo | 3892ac9 | 2017-04-17 15:00:28 -0700 | [diff] [blame] | 8484 | if (bio && dip) { |
Guoqing Jiang | 054ec2f | 2017-06-02 16:08:50 +0800 | [diff] [blame] | 8485 | bio_io_error(bio); |
Filipe Manana | 61de718 | 2015-07-01 12:13:10 +0100 | [diff] [blame] | 8486 | /* |
Liu Bo | 3892ac9 | 2017-04-17 15:00:28 -0700 | [diff] [blame] | 8487 | * The end io callbacks free our dip, do the final put on bio |
Filipe Manana | 61de718 | 2015-07-01 12:13:10 +0100 | [diff] [blame] | 8488 | * and all the cleanup and final put for dio_bio (through |
| 8489 | * dio_end_io()). |
| 8490 | */ |
| 8491 | dip = NULL; |
Liu Bo | 3892ac9 | 2017-04-17 15:00:28 -0700 | [diff] [blame] | 8492 | bio = NULL; |
Filipe Manana | 61de718 | 2015-07-01 12:13:10 +0100 | [diff] [blame] | 8493 | } else { |
Filipe Manana | 1454377 | 2015-11-24 16:23:54 +0000 | [diff] [blame] | 8494 | if (write) |
Qu Wenruo | 52427260 | 2017-03-08 10:25:52 +0800 | [diff] [blame] | 8495 | __endio_write_update_ordered(inode, |
Filipe Manana | 1454377 | 2015-11-24 16:23:54 +0000 | [diff] [blame] | 8496 | file_offset, |
| 8497 | dio_bio->bi_iter.bi_size, |
Qu Wenruo | 52427260 | 2017-03-08 10:25:52 +0800 | [diff] [blame] | 8498 | false); |
Filipe Manana | 1454377 | 2015-11-24 16:23:54 +0000 | [diff] [blame] | 8499 | else |
Filipe Manana | 61de718 | 2015-07-01 12:13:10 +0100 | [diff] [blame] | 8500 | unlock_extent(&BTRFS_I(inode)->io_tree, file_offset, |
| 8501 | file_offset + dio_bio->bi_iter.bi_size - 1); |
Filipe Manana | 1454377 | 2015-11-24 16:23:54 +0000 | [diff] [blame] | 8502 | |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 8503 | dio_bio->bi_status = BLK_STS_IOERR; |
Filipe Manana | 61de718 | 2015-07-01 12:13:10 +0100 | [diff] [blame] | 8504 | /* |
| 8505 | * Releases and cleans up our dio_bio, no need to bio_put() |
| 8506 | * nor bio_endio()/bio_io_error() against dio_bio. |
| 8507 | */ |
Christoph Hellwig | 4055351 | 2017-06-03 09:37:58 +0200 | [diff] [blame] | 8508 | dio_end_io(dio_bio); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8509 | } |
Liu Bo | 3892ac9 | 2017-04-17 15:00:28 -0700 | [diff] [blame] | 8510 | if (bio) |
| 8511 | bio_put(bio); |
Filipe Manana | 61de718 | 2015-07-01 12:13:10 +0100 | [diff] [blame] | 8512 | kfree(dip); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8513 | } |
| 8514 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 8515 | static ssize_t check_direct_IO(struct btrfs_fs_info *fs_info, |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 8516 | const struct iov_iter *iter, loff_t offset) |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 8517 | { |
| 8518 | int seg; |
Josef Bacik | a1b75f7 | 2011-04-08 15:51:18 +0000 | [diff] [blame] | 8519 | int i; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8520 | unsigned int blocksize_mask = fs_info->sectorsize - 1; |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 8521 | ssize_t retval = -EINVAL; |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 8522 | |
| 8523 | if (offset & blocksize_mask) |
| 8524 | goto out; |
| 8525 | |
Al Viro | 28060d5 | 2014-03-22 05:15:17 -0400 | [diff] [blame] | 8526 | if (iov_iter_alignment(iter) & blocksize_mask) |
| 8527 | goto out; |
Josef Bacik | a1b75f7 | 2011-04-08 15:51:18 +0000 | [diff] [blame] | 8528 | |
Al Viro | 28060d5 | 2014-03-22 05:15:17 -0400 | [diff] [blame] | 8529 | /* If this is a write we don't need to check anymore */ |
Al Viro | cd27e45 | 2016-10-10 13:39:05 -0400 | [diff] [blame] | 8530 | if (iov_iter_rw(iter) != READ || !iter_is_iovec(iter)) |
Al Viro | 28060d5 | 2014-03-22 05:15:17 -0400 | [diff] [blame] | 8531 | return 0; |
| 8532 | /* |
| 8533 | * Check to make sure we don't have duplicate iov_base's in this |
| 8534 | * iovec, if so return EINVAL, otherwise we'll get csum errors |
| 8535 | * when reading back. |
| 8536 | */ |
| 8537 | for (seg = 0; seg < iter->nr_segs; seg++) { |
| 8538 | for (i = seg + 1; i < iter->nr_segs; i++) { |
| 8539 | if (iter->iov[seg].iov_base == iter->iov[i].iov_base) |
Josef Bacik | a1b75f7 | 2011-04-08 15:51:18 +0000 | [diff] [blame] | 8540 | goto out; |
| 8541 | } |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 8542 | } |
| 8543 | retval = 0; |
| 8544 | out: |
| 8545 | return retval; |
| 8546 | } |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 8547 | |
Christoph Hellwig | c8b8e32 | 2016-04-07 08:51:58 -0700 | [diff] [blame] | 8548 | static ssize_t btrfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter) |
Chris Mason | 1643298 | 2008-04-10 10:23:21 -0400 | [diff] [blame] | 8549 | { |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8550 | struct file *file = iocb->ki_filp; |
| 8551 | struct inode *inode = file->f_mapping->host; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8552 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
chandan | 50745b0 | 2015-08-28 21:10:13 +0530 | [diff] [blame] | 8553 | struct btrfs_dio_data dio_data = { 0 }; |
Qu Wenruo | 364ecf3 | 2017-02-27 15:10:38 +0800 | [diff] [blame] | 8554 | struct extent_changeset *data_reserved = NULL; |
Christoph Hellwig | c8b8e32 | 2016-04-07 08:51:58 -0700 | [diff] [blame] | 8555 | loff_t offset = iocb->ki_pos; |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 8556 | size_t count = 0; |
Miao Xie | 2e60a51 | 2013-02-08 07:01:08 +0000 | [diff] [blame] | 8557 | int flags = 0; |
Miao Xie | 38851cc | 2013-02-08 07:04:11 +0000 | [diff] [blame] | 8558 | bool wakeup = true; |
| 8559 | bool relock = false; |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 8560 | ssize_t ret; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8561 | |
Nikolay Borisov | 8c70c9f | 2017-08-21 12:43:46 +0300 | [diff] [blame] | 8562 | if (check_direct_IO(fs_info, iter, offset)) |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 8563 | return 0; |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 8564 | |
Jens Axboe | fe0f07d | 2015-04-15 17:05:48 -0600 | [diff] [blame] | 8565 | inode_dio_begin(inode); |
Miao Xie | 38851cc | 2013-02-08 07:04:11 +0000 | [diff] [blame] | 8566 | |
Josef Bacik | 0e267c4 | 2013-07-02 10:38:02 -0400 | [diff] [blame] | 8567 | /* |
Miao Xie | 41bd9ca | 2014-03-06 13:54:57 +0800 | [diff] [blame] | 8568 | * The generic stuff only does filemap_write_and_wait_range, which |
| 8569 | * isn't enough if we've written compressed pages to this area, so |
| 8570 | * we need to flush the dirty pages again to make absolutely sure |
| 8571 | * that any outstanding dirty pages are on disk. |
Josef Bacik | 0e267c4 | 2013-07-02 10:38:02 -0400 | [diff] [blame] | 8572 | */ |
Al Viro | a6cbcd4 | 2014-03-04 22:38:00 -0500 | [diff] [blame] | 8573 | count = iov_iter_count(iter); |
Miao Xie | 41bd9ca | 2014-03-06 13:54:57 +0800 | [diff] [blame] | 8574 | if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, |
| 8575 | &BTRFS_I(inode)->runtime_flags)) |
Wang Shilong | 9a025a0 | 2014-07-17 11:44:13 +0800 | [diff] [blame] | 8576 | filemap_fdatawrite_range(inode->i_mapping, offset, |
| 8577 | offset + count - 1); |
Josef Bacik | 0e267c4 | 2013-07-02 10:38:02 -0400 | [diff] [blame] | 8578 | |
Omar Sandoval | 6f67376 | 2015-03-16 04:33:52 -0700 | [diff] [blame] | 8579 | if (iov_iter_rw(iter) == WRITE) { |
Miao Xie | 38851cc | 2013-02-08 07:04:11 +0000 | [diff] [blame] | 8580 | /* |
| 8581 | * If the write DIO is beyond the EOF, we need update |
| 8582 | * the isize, but it is protected by i_mutex. So we can |
| 8583 | * not unlock the i_mutex at this case. |
| 8584 | */ |
| 8585 | if (offset + count <= inode->i_size) { |
Liu Bo | 4aaedfb | 2016-12-14 22:36:05 -0800 | [diff] [blame] | 8586 | dio_data.overwrite = 1; |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 8587 | inode_unlock(inode); |
Miao Xie | 38851cc | 2013-02-08 07:04:11 +0000 | [diff] [blame] | 8588 | relock = true; |
Goldwyn Rodrigues | edf064e | 2017-06-20 07:05:49 -0500 | [diff] [blame] | 8589 | } else if (iocb->ki_flags & IOCB_NOWAIT) { |
| 8590 | ret = -EAGAIN; |
| 8591 | goto out; |
Miao Xie | 38851cc | 2013-02-08 07:04:11 +0000 | [diff] [blame] | 8592 | } |
Qu Wenruo | 364ecf3 | 2017-02-27 15:10:38 +0800 | [diff] [blame] | 8593 | ret = btrfs_delalloc_reserve_space(inode, &data_reserved, |
| 8594 | offset, count); |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 8595 | if (ret) |
Miao Xie | 38851cc | 2013-02-08 07:04:11 +0000 | [diff] [blame] | 8596 | goto out; |
Josef Bacik | e1cbbfa | 2015-03-17 10:52:28 -0400 | [diff] [blame] | 8597 | |
| 8598 | /* |
| 8599 | * We need to know how many extents we reserved so that we can |
| 8600 | * do the accounting properly if we go over the number we |
| 8601 | * originally calculated. Abuse current->journal_info for this. |
| 8602 | */ |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 8603 | dio_data.reserve = round_up(count, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8604 | fs_info->sectorsize); |
Filipe Manana | f28a492 | 2015-12-08 19:23:20 +0000 | [diff] [blame] | 8605 | dio_data.unsubmitted_oe_range_start = (u64)offset; |
| 8606 | dio_data.unsubmitted_oe_range_end = (u64)offset; |
chandan | 50745b0 | 2015-08-28 21:10:13 +0530 | [diff] [blame] | 8607 | current->journal_info = &dio_data; |
Chandan Rajendra | 97dcdea | 2016-12-23 15:00:18 +0530 | [diff] [blame] | 8608 | down_read(&BTRFS_I(inode)->dio_sem); |
David Sterba | ee39b43 | 2014-09-30 01:33:33 +0200 | [diff] [blame] | 8609 | } else if (test_bit(BTRFS_INODE_READDIO_NEED_LOCK, |
| 8610 | &BTRFS_I(inode)->runtime_flags)) { |
Jens Axboe | fe0f07d | 2015-04-15 17:05:48 -0600 | [diff] [blame] | 8611 | inode_dio_end(inode); |
Miao Xie | 38851cc | 2013-02-08 07:04:11 +0000 | [diff] [blame] | 8612 | flags = DIO_LOCKING | DIO_SKIP_HOLES; |
| 8613 | wakeup = false; |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 8614 | } |
| 8615 | |
Omar Sandoval | 17f8c84 | 2015-03-16 04:33:50 -0700 | [diff] [blame] | 8616 | ret = __blockdev_direct_IO(iocb, inode, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8617 | fs_info->fs_devices->latest_bdev, |
Christoph Hellwig | c8b8e32 | 2016-04-07 08:51:58 -0700 | [diff] [blame] | 8618 | iter, btrfs_get_blocks_direct, NULL, |
Omar Sandoval | 17f8c84 | 2015-03-16 04:33:50 -0700 | [diff] [blame] | 8619 | btrfs_submit_direct, flags); |
Omar Sandoval | 6f67376 | 2015-03-16 04:33:52 -0700 | [diff] [blame] | 8620 | if (iov_iter_rw(iter) == WRITE) { |
Chandan Rajendra | 97dcdea | 2016-12-23 15:00:18 +0530 | [diff] [blame] | 8621 | up_read(&BTRFS_I(inode)->dio_sem); |
Josef Bacik | e1cbbfa | 2015-03-17 10:52:28 -0400 | [diff] [blame] | 8622 | current->journal_info = NULL; |
Liu Bo | ddba1bf | 2015-06-17 16:59:58 +0800 | [diff] [blame] | 8623 | if (ret < 0 && ret != -EIOCBQUEUED) { |
chandan | 50745b0 | 2015-08-28 21:10:13 +0530 | [diff] [blame] | 8624 | if (dio_data.reserve) |
Qu Wenruo | bc42bda | 2017-02-27 15:10:39 +0800 | [diff] [blame] | 8625 | btrfs_delalloc_release_space(inode, data_reserved, |
Qu Wenruo | 43b1859 | 2017-12-12 15:34:32 +0800 | [diff] [blame] | 8626 | offset, dio_data.reserve, true); |
Filipe Manana | f28a492 | 2015-12-08 19:23:20 +0000 | [diff] [blame] | 8627 | /* |
| 8628 | * On error we might have left some ordered extents |
| 8629 | * without submitting corresponding bios for them, so |
| 8630 | * cleanup them up to avoid other tasks getting them |
| 8631 | * and waiting for them to complete forever. |
| 8632 | */ |
| 8633 | if (dio_data.unsubmitted_oe_range_start < |
| 8634 | dio_data.unsubmitted_oe_range_end) |
Qu Wenruo | 52427260 | 2017-03-08 10:25:52 +0800 | [diff] [blame] | 8635 | __endio_write_update_ordered(inode, |
Filipe Manana | f28a492 | 2015-12-08 19:23:20 +0000 | [diff] [blame] | 8636 | dio_data.unsubmitted_oe_range_start, |
| 8637 | dio_data.unsubmitted_oe_range_end - |
| 8638 | dio_data.unsubmitted_oe_range_start, |
Qu Wenruo | 52427260 | 2017-03-08 10:25:52 +0800 | [diff] [blame] | 8639 | false); |
Liu Bo | ddba1bf | 2015-06-17 16:59:58 +0800 | [diff] [blame] | 8640 | } else if (ret >= 0 && (size_t)ret < count) |
Qu Wenruo | bc42bda | 2017-02-27 15:10:39 +0800 | [diff] [blame] | 8641 | btrfs_delalloc_release_space(inode, data_reserved, |
Qu Wenruo | 43b1859 | 2017-12-12 15:34:32 +0800 | [diff] [blame] | 8642 | offset, count - (size_t)ret, true); |
| 8643 | btrfs_delalloc_release_extents(BTRFS_I(inode), count, false); |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 8644 | } |
Miao Xie | 38851cc | 2013-02-08 07:04:11 +0000 | [diff] [blame] | 8645 | out: |
Miao Xie | 2e60a51 | 2013-02-08 07:01:08 +0000 | [diff] [blame] | 8646 | if (wakeup) |
Jens Axboe | fe0f07d | 2015-04-15 17:05:48 -0600 | [diff] [blame] | 8647 | inode_dio_end(inode); |
Miao Xie | 38851cc | 2013-02-08 07:04:11 +0000 | [diff] [blame] | 8648 | if (relock) |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 8649 | inode_lock(inode); |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 8650 | |
Qu Wenruo | 364ecf3 | 2017-02-27 15:10:38 +0800 | [diff] [blame] | 8651 | extent_changeset_free(data_reserved); |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 8652 | return ret; |
Chris Mason | 1643298 | 2008-04-10 10:23:21 -0400 | [diff] [blame] | 8653 | } |
| 8654 | |
Tsutomu Itoh | 05dadc0 | 2012-11-29 05:08:26 +0000 | [diff] [blame] | 8655 | #define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC) |
| 8656 | |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 8657 | static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, |
| 8658 | __u64 start, __u64 len) |
| 8659 | { |
Tsutomu Itoh | 05dadc0 | 2012-11-29 05:08:26 +0000 | [diff] [blame] | 8660 | int ret; |
| 8661 | |
| 8662 | ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS); |
| 8663 | if (ret) |
| 8664 | return ret; |
| 8665 | |
David Sterba | 2135fb9 | 2017-06-23 04:09:57 +0200 | [diff] [blame] | 8666 | return extent_fiemap(inode, fieinfo, start, len); |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 8667 | } |
| 8668 | |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8669 | int btrfs_readpage(struct file *file, struct page *page) |
| 8670 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 8671 | struct extent_io_tree *tree; |
| 8672 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
Jan Schmidt | 8ddc7d9 | 2011-06-13 20:02:58 +0200 | [diff] [blame] | 8673 | return extent_read_full_page(tree, page, btrfs_get_extent, 0); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8674 | } |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 8675 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8676 | static int btrfs_writepage(struct page *page, struct writeback_control *wbc) |
| 8677 | { |
Josef Bacik | be7bd73 | 2015-10-22 15:05:09 -0400 | [diff] [blame] | 8678 | struct inode *inode = page->mapping->host; |
| 8679 | int ret; |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8680 | |
| 8681 | if (current->flags & PF_MEMALLOC) { |
| 8682 | redirty_page_for_writepage(wbc, page); |
| 8683 | unlock_page(page); |
| 8684 | return 0; |
| 8685 | } |
Josef Bacik | be7bd73 | 2015-10-22 15:05:09 -0400 | [diff] [blame] | 8686 | |
| 8687 | /* |
| 8688 | * If we are under memory pressure we will call this directly from the |
| 8689 | * VM, we need to make sure we have the inode referenced for the ordered |
| 8690 | * extent. If not just return like we didn't do anything. |
| 8691 | */ |
| 8692 | if (!igrab(inode)) { |
| 8693 | redirty_page_for_writepage(wbc, page); |
| 8694 | return AOP_WRITEPAGE_ACTIVATE; |
| 8695 | } |
Nikolay Borisov | 0a9b0e5 | 2017-12-08 15:55:59 +0200 | [diff] [blame] | 8696 | ret = extent_write_full_page(page, wbc); |
Josef Bacik | be7bd73 | 2015-10-22 15:05:09 -0400 | [diff] [blame] | 8697 | btrfs_add_delayed_iput(inode); |
| 8698 | return ret; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8699 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8700 | |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 8701 | static int btrfs_writepages(struct address_space *mapping, |
| 8702 | struct writeback_control *wbc) |
Chris Mason | b293f02e | 2007-11-01 19:45:34 -0400 | [diff] [blame] | 8703 | { |
Nikolay Borisov | 8ae225a | 2018-04-19 10:46:38 +0300 | [diff] [blame] | 8704 | return extent_writepages(mapping, wbc); |
Chris Mason | b293f02e | 2007-11-01 19:45:34 -0400 | [diff] [blame] | 8705 | } |
| 8706 | |
Chris Mason | 3ab2fb5 | 2007-11-08 10:59:22 -0500 | [diff] [blame] | 8707 | static int |
| 8708 | btrfs_readpages(struct file *file, struct address_space *mapping, |
| 8709 | struct list_head *pages, unsigned nr_pages) |
| 8710 | { |
Nikolay Borisov | 2a3ff0a | 2018-04-19 10:46:36 +0300 | [diff] [blame] | 8711 | return extent_readpages(mapping, pages, nr_pages); |
Chris Mason | 3ab2fb5 | 2007-11-08 10:59:22 -0500 | [diff] [blame] | 8712 | } |
Nikolay Borisov | 2a3ff0a | 2018-04-19 10:46:36 +0300 | [diff] [blame] | 8713 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8714 | static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8715 | { |
Nikolay Borisov | 477a30b | 2018-04-19 10:46:34 +0300 | [diff] [blame] | 8716 | int ret = try_release_extent_mapping(page, gfp_flags); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8717 | if (ret == 1) { |
| 8718 | ClearPagePrivate(page); |
| 8719 | set_page_private(page, 0); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 8720 | put_page(page); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8721 | } |
| 8722 | return ret; |
| 8723 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8724 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8725 | static int btrfs_releasepage(struct page *page, gfp_t gfp_flags) |
| 8726 | { |
Chris Mason | 98509cf | 2008-09-11 15:51:43 -0400 | [diff] [blame] | 8727 | if (PageWriteback(page) || PageDirty(page)) |
| 8728 | return 0; |
Michal Hocko | 3ba7ab2 | 2017-01-09 15:39:02 +0100 | [diff] [blame] | 8729 | return __btrfs_releasepage(page, gfp_flags); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8730 | } |
| 8731 | |
Lukas Czerner | d47992f | 2013-05-21 23:17:23 -0400 | [diff] [blame] | 8732 | static void btrfs_invalidatepage(struct page *page, unsigned int offset, |
| 8733 | unsigned int length) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8734 | { |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 8735 | struct inode *inode = page->mapping->host; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 8736 | struct extent_io_tree *tree; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8737 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 8738 | struct extent_state *cached_state = NULL; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8739 | u64 page_start = page_offset(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 8740 | u64 page_end = page_start + PAGE_SIZE - 1; |
Chandan Rajendra | dbfdb6d | 2016-01-21 15:55:58 +0530 | [diff] [blame] | 8741 | u64 start; |
| 8742 | u64 end; |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 8743 | int inode_evicting = inode->i_state & I_FREEING; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8744 | |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 8745 | /* |
| 8746 | * we have the page locked, so new writeback can't start, |
| 8747 | * and the dirty bit won't be cleared while we are here. |
| 8748 | * |
| 8749 | * Wait for IO on this page so that we can safely clear |
| 8750 | * the PagePrivate2 bit and do ordered accounting |
| 8751 | */ |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8752 | wait_on_page_writeback(page); |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 8753 | |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 8754 | tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8755 | if (offset) { |
| 8756 | btrfs_releasepage(page, GFP_NOFS); |
| 8757 | return; |
| 8758 | } |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 8759 | |
| 8760 | if (!inode_evicting) |
David Sterba | ff13db4 | 2015-12-03 14:30:40 +0100 | [diff] [blame] | 8761 | lock_extent_bits(tree, page_start, page_end, &cached_state); |
Chandan Rajendra | dbfdb6d | 2016-01-21 15:55:58 +0530 | [diff] [blame] | 8762 | again: |
| 8763 | start = page_start; |
Nikolay Borisov | a776c6f | 2017-02-20 13:50:49 +0200 | [diff] [blame] | 8764 | ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), start, |
Chandan Rajendra | dbfdb6d | 2016-01-21 15:55:58 +0530 | [diff] [blame] | 8765 | page_end - start + 1); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8766 | if (ordered) { |
Chandan Rajendra | dbfdb6d | 2016-01-21 15:55:58 +0530 | [diff] [blame] | 8767 | end = min(page_end, ordered->file_offset + ordered->len - 1); |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 8768 | /* |
| 8769 | * IO on this page will never be started, so we need |
| 8770 | * to account for any ordered extents now |
| 8771 | */ |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 8772 | if (!inode_evicting) |
Chandan Rajendra | dbfdb6d | 2016-01-21 15:55:58 +0530 | [diff] [blame] | 8773 | clear_extent_bit(tree, start, end, |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 8774 | EXTENT_DIRTY | EXTENT_DELALLOC | |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 8775 | EXTENT_DELALLOC_NEW | |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 8776 | EXTENT_LOCKED | EXTENT_DO_ACCOUNTING | |
David Sterba | ae0f162 | 2017-10-31 16:37:52 +0100 | [diff] [blame] | 8777 | EXTENT_DEFRAG, 1, 0, &cached_state); |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 8778 | /* |
| 8779 | * whoever cleared the private bit is responsible |
| 8780 | * for the finish_ordered_io |
| 8781 | */ |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 8782 | if (TestClearPagePrivate2(page)) { |
| 8783 | struct btrfs_ordered_inode_tree *tree; |
| 8784 | u64 new_len; |
| 8785 | |
| 8786 | tree = &BTRFS_I(inode)->ordered_tree; |
| 8787 | |
| 8788 | spin_lock_irq(&tree->lock); |
| 8789 | set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags); |
Chandan Rajendra | dbfdb6d | 2016-01-21 15:55:58 +0530 | [diff] [blame] | 8790 | new_len = start - ordered->file_offset; |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 8791 | if (new_len < ordered->truncated_len) |
| 8792 | ordered->truncated_len = new_len; |
| 8793 | spin_unlock_irq(&tree->lock); |
| 8794 | |
| 8795 | if (btrfs_dec_test_ordered_pending(inode, &ordered, |
Chandan Rajendra | dbfdb6d | 2016-01-21 15:55:58 +0530 | [diff] [blame] | 8796 | start, |
| 8797 | end - start + 1, 1)) |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 8798 | btrfs_finish_ordered_io(ordered); |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 8799 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8800 | btrfs_put_ordered_extent(ordered); |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 8801 | if (!inode_evicting) { |
| 8802 | cached_state = NULL; |
Chandan Rajendra | dbfdb6d | 2016-01-21 15:55:58 +0530 | [diff] [blame] | 8803 | lock_extent_bits(tree, start, end, |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 8804 | &cached_state); |
| 8805 | } |
Chandan Rajendra | dbfdb6d | 2016-01-21 15:55:58 +0530 | [diff] [blame] | 8806 | |
| 8807 | start = end + 1; |
| 8808 | if (start < page_end) |
| 8809 | goto again; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8810 | } |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 8811 | |
Qu Wenruo | b9d0b38 | 2015-09-29 10:35:16 +0800 | [diff] [blame] | 8812 | /* |
| 8813 | * Qgroup reserved space handler |
| 8814 | * Page here will be either |
| 8815 | * 1) Already written to disk |
| 8816 | * In this case, its reserved space is released from data rsv map |
| 8817 | * and will be freed by delayed_ref handler finally. |
| 8818 | * So even we call qgroup_free_data(), it won't decrease reserved |
| 8819 | * space. |
| 8820 | * 2) Not written to disk |
Goldwyn Rodrigues | 0b34c26 | 2016-09-30 10:40:52 -0500 | [diff] [blame] | 8821 | * This means the reserved space should be freed here. However, |
| 8822 | * if a truncate invalidates the page (by clearing PageDirty) |
| 8823 | * and the page is accounted for while allocating extent |
| 8824 | * in btrfs_check_data_free_space() we let delayed_ref to |
| 8825 | * free the entire extent. |
Qu Wenruo | b9d0b38 | 2015-09-29 10:35:16 +0800 | [diff] [blame] | 8826 | */ |
Goldwyn Rodrigues | 0b34c26 | 2016-09-30 10:40:52 -0500 | [diff] [blame] | 8827 | if (PageDirty(page)) |
Qu Wenruo | bc42bda | 2017-02-27 15:10:39 +0800 | [diff] [blame] | 8828 | btrfs_qgroup_free_data(inode, NULL, page_start, PAGE_SIZE); |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 8829 | if (!inode_evicting) { |
| 8830 | clear_extent_bit(tree, page_start, page_end, |
| 8831 | EXTENT_LOCKED | EXTENT_DIRTY | |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 8832 | EXTENT_DELALLOC | EXTENT_DELALLOC_NEW | |
| 8833 | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1, |
David Sterba | ae0f162 | 2017-10-31 16:37:52 +0100 | [diff] [blame] | 8834 | &cached_state); |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 8835 | |
| 8836 | __btrfs_releasepage(page, GFP_NOFS); |
| 8837 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8838 | |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 8839 | ClearPageChecked(page); |
Chris Mason | 9ad6b7bc | 2008-04-18 16:11:30 -0400 | [diff] [blame] | 8840 | if (PagePrivate(page)) { |
Chris Mason | 9ad6b7bc | 2008-04-18 16:11:30 -0400 | [diff] [blame] | 8841 | ClearPagePrivate(page); |
| 8842 | set_page_private(page, 0); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 8843 | put_page(page); |
Chris Mason | 9ad6b7bc | 2008-04-18 16:11:30 -0400 | [diff] [blame] | 8844 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8845 | } |
| 8846 | |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8847 | /* |
| 8848 | * btrfs_page_mkwrite() is not allowed to change the file size as it gets |
| 8849 | * called from a page fault handler when a page is first dirtied. Hence we must |
| 8850 | * be careful to check for EOF conditions here. We set the page up correctly |
| 8851 | * for a written page which means we get ENOSPC checking when writing into |
| 8852 | * holes and correct delalloc and unwritten extent mapping on filesystems that |
| 8853 | * support these features. |
| 8854 | * |
| 8855 | * We are not allowed to take the i_mutex here so we have to play games to |
| 8856 | * 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] | 8857 | * truncate_setsize() writes the inode size before removing pages, once we have |
| 8858 | * 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] | 8859 | * beyond EOF, then the page is guaranteed safe against truncation until we |
| 8860 | * unlock the page. |
| 8861 | */ |
Souptick Joarder | a528a24 | 2018-06-06 19:54:44 +0530 | [diff] [blame] | 8862 | vm_fault_t btrfs_page_mkwrite(struct vm_fault *vmf) |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8863 | { |
Nick Piggin | c2ec175 | 2009-03-31 15:23:21 -0700 | [diff] [blame] | 8864 | struct page *page = vmf->page; |
Dave Jiang | 11bac80 | 2017-02-24 14:56:41 -0800 | [diff] [blame] | 8865 | struct inode *inode = file_inode(vmf->vma->vm_file); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8866 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8867 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
| 8868 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 8869 | struct extent_state *cached_state = NULL; |
Qu Wenruo | 364ecf3 | 2017-02-27 15:10:38 +0800 | [diff] [blame] | 8870 | struct extent_changeset *data_reserved = NULL; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8871 | char *kaddr; |
| 8872 | unsigned long zero_start; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8873 | loff_t size; |
Souptick Joarder | a528a24 | 2018-06-06 19:54:44 +0530 | [diff] [blame] | 8874 | vm_fault_t ret; |
| 8875 | int ret2; |
Chris Mason | 9998eb7 | 2012-01-25 13:47:40 -0500 | [diff] [blame] | 8876 | int reserved = 0; |
Chandan Rajendra | d0b7da88 | 2016-01-21 15:55:57 +0530 | [diff] [blame] | 8877 | u64 reserved_space; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8878 | u64 page_start; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8879 | u64 page_end; |
Chandan Rajendra | d0b7da88 | 2016-01-21 15:55:57 +0530 | [diff] [blame] | 8880 | u64 end; |
| 8881 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 8882 | reserved_space = PAGE_SIZE; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8883 | |
Jan Kara | b2b5ef5 | 2012-06-12 16:20:45 +0200 | [diff] [blame] | 8884 | sb_start_pagefault(inode->i_sb); |
Qu Wenruo | df48063 | 2015-09-08 17:25:54 +0800 | [diff] [blame] | 8885 | page_start = page_offset(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 8886 | page_end = page_start + PAGE_SIZE - 1; |
Chandan Rajendra | d0b7da88 | 2016-01-21 15:55:57 +0530 | [diff] [blame] | 8887 | end = page_end; |
Qu Wenruo | df48063 | 2015-09-08 17:25:54 +0800 | [diff] [blame] | 8888 | |
Chandan Rajendra | d0b7da88 | 2016-01-21 15:55:57 +0530 | [diff] [blame] | 8889 | /* |
| 8890 | * Reserving delalloc space after obtaining the page lock can lead to |
| 8891 | * deadlock. For example, if a dirty page is locked by this function |
| 8892 | * and the call to btrfs_delalloc_reserve_space() ends up triggering |
| 8893 | * dirty page write out, then the btrfs_writepage() function could |
| 8894 | * end up waiting indefinitely to get a lock on the page currently |
| 8895 | * being processed by btrfs_page_mkwrite() function. |
| 8896 | */ |
Souptick Joarder | a528a24 | 2018-06-06 19:54:44 +0530 | [diff] [blame] | 8897 | ret2 = btrfs_delalloc_reserve_space(inode, &data_reserved, page_start, |
Chandan Rajendra | d0b7da88 | 2016-01-21 15:55:57 +0530 | [diff] [blame] | 8898 | reserved_space); |
Souptick Joarder | a528a24 | 2018-06-06 19:54:44 +0530 | [diff] [blame] | 8899 | if (!ret2) { |
| 8900 | ret2 = file_update_time(vmf->vma->vm_file); |
Chris Mason | 9998eb7 | 2012-01-25 13:47:40 -0500 | [diff] [blame] | 8901 | reserved = 1; |
| 8902 | } |
Souptick Joarder | a528a24 | 2018-06-06 19:54:44 +0530 | [diff] [blame] | 8903 | if (ret2) { |
| 8904 | ret = vmf_error(ret2); |
Chris Mason | 9998eb7 | 2012-01-25 13:47:40 -0500 | [diff] [blame] | 8905 | if (reserved) |
| 8906 | goto out; |
| 8907 | goto out_noreserve; |
Nick Piggin | 56a76f8 | 2009-03-31 15:23:23 -0700 | [diff] [blame] | 8908 | } |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 8909 | |
Nick Piggin | 56a76f8 | 2009-03-31 15:23:23 -0700 | [diff] [blame] | 8910 | ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */ |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8911 | again: |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8912 | lock_page(page); |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8913 | size = i_size_read(inode); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8914 | |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8915 | if ((page->mapping != inode->i_mapping) || |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8916 | (page_start >= size)) { |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8917 | /* page got truncated out from underneath us */ |
| 8918 | goto out_unlock; |
| 8919 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8920 | wait_on_page_writeback(page); |
| 8921 | |
David Sterba | ff13db4 | 2015-12-03 14:30:40 +0100 | [diff] [blame] | 8922 | lock_extent_bits(io_tree, page_start, page_end, &cached_state); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8923 | set_page_extent_mapped(page); |
| 8924 | |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 8925 | /* |
| 8926 | * we can't set the delalloc bits if there are pending ordered |
| 8927 | * extents. Drop our locks and wait for them to finish |
| 8928 | */ |
Nikolay Borisov | a776c6f | 2017-02-20 13:50:49 +0200 | [diff] [blame] | 8929 | ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start, |
| 8930 | PAGE_SIZE); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8931 | if (ordered) { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 8932 | unlock_extent_cached(io_tree, page_start, page_end, |
David Sterba | e43bbe5 | 2017-12-12 21:43:52 +0100 | [diff] [blame] | 8933 | &cached_state); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8934 | unlock_page(page); |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 8935 | btrfs_start_ordered_extent(inode, ordered, 1); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8936 | btrfs_put_ordered_extent(ordered); |
| 8937 | goto again; |
| 8938 | } |
| 8939 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 8940 | if (page->index == ((size - 1) >> PAGE_SHIFT)) { |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 8941 | reserved_space = round_up(size - page_start, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8942 | fs_info->sectorsize); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 8943 | if (reserved_space < PAGE_SIZE) { |
Chandan Rajendra | d0b7da88 | 2016-01-21 15:55:57 +0530 | [diff] [blame] | 8944 | end = page_start + reserved_space - 1; |
Qu Wenruo | bc42bda | 2017-02-27 15:10:39 +0800 | [diff] [blame] | 8945 | btrfs_delalloc_release_space(inode, data_reserved, |
Qu Wenruo | 43b1859 | 2017-12-12 15:34:32 +0800 | [diff] [blame] | 8946 | page_start, PAGE_SIZE - reserved_space, |
| 8947 | true); |
Chandan Rajendra | d0b7da88 | 2016-01-21 15:55:57 +0530 | [diff] [blame] | 8948 | } |
| 8949 | } |
| 8950 | |
Josef Bacik | fbf1908 | 2009-10-01 17:10:23 -0400 | [diff] [blame] | 8951 | /* |
Liu Bo | 5416034 | 2016-12-13 12:15:19 -0800 | [diff] [blame] | 8952 | * page_mkwrite gets called when the page is firstly dirtied after it's |
| 8953 | * faulted in, but write(2) could also dirty a page and set delalloc |
| 8954 | * bits, thus in this case for space account reason, we still need to |
| 8955 | * clear any delalloc bits within this page range since we have to |
| 8956 | * reserve data&meta space before lock_page() (see above comments). |
Josef Bacik | fbf1908 | 2009-10-01 17:10:23 -0400 | [diff] [blame] | 8957 | */ |
Chandan Rajendra | d0b7da88 | 2016-01-21 15:55:57 +0530 | [diff] [blame] | 8958 | clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, end, |
Liu Bo | 9e8a4a8 | 2012-09-05 19:10:51 -0600 | [diff] [blame] | 8959 | EXTENT_DIRTY | EXTENT_DELALLOC | |
| 8960 | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, |
David Sterba | ae0f162 | 2017-10-31 16:37:52 +0100 | [diff] [blame] | 8961 | 0, 0, &cached_state); |
Josef Bacik | fbf1908 | 2009-10-01 17:10:23 -0400 | [diff] [blame] | 8962 | |
Souptick Joarder | a528a24 | 2018-06-06 19:54:44 +0530 | [diff] [blame] | 8963 | ret2 = btrfs_set_extent_delalloc(inode, page_start, end, 0, |
Qu Wenruo | ba8b04c | 2016-07-19 16:50:36 +0800 | [diff] [blame] | 8964 | &cached_state, 0); |
Souptick Joarder | a528a24 | 2018-06-06 19:54:44 +0530 | [diff] [blame] | 8965 | if (ret2) { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 8966 | unlock_extent_cached(io_tree, page_start, page_end, |
David Sterba | e43bbe5 | 2017-12-12 21:43:52 +0100 | [diff] [blame] | 8967 | &cached_state); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 8968 | ret = VM_FAULT_SIGBUS; |
| 8969 | goto out_unlock; |
| 8970 | } |
Souptick Joarder | a528a24 | 2018-06-06 19:54:44 +0530 | [diff] [blame] | 8971 | ret2 = 0; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8972 | |
| 8973 | /* page is wholly or partially inside EOF */ |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 8974 | if (page_start + PAGE_SIZE > size) |
Johannes Thumshirn | 7073017 | 2018-12-05 15:23:03 +0100 | [diff] [blame] | 8975 | zero_start = offset_in_page(size); |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8976 | else |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 8977 | zero_start = PAGE_SIZE; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8978 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 8979 | if (zero_start != PAGE_SIZE) { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8980 | kaddr = kmap(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 8981 | memset(kaddr + zero_start, 0, PAGE_SIZE - zero_start); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8982 | flush_dcache_page(page); |
| 8983 | kunmap(page); |
| 8984 | } |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 8985 | ClearPageChecked(page); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8986 | set_page_dirty(page); |
Chris Mason | 50a9b21 | 2009-09-11 12:33:12 -0400 | [diff] [blame] | 8987 | SetPageUptodate(page); |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 8988 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8989 | BTRFS_I(inode)->last_trans = fs_info->generation; |
Chris Mason | 257c62e | 2009-10-13 13:21:08 -0400 | [diff] [blame] | 8990 | BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid; |
Liu Bo | 46d8bc3 | 2012-08-29 01:07:55 -0600 | [diff] [blame] | 8991 | BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit; |
Chris Mason | 257c62e | 2009-10-13 13:21:08 -0400 | [diff] [blame] | 8992 | |
David Sterba | e43bbe5 | 2017-12-12 21:43:52 +0100 | [diff] [blame] | 8993 | unlock_extent_cached(io_tree, page_start, page_end, &cached_state); |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8994 | |
Souptick Joarder | a528a24 | 2018-06-06 19:54:44 +0530 | [diff] [blame] | 8995 | if (!ret2) { |
Qu Wenruo | 43b1859 | 2017-12-12 15:34:32 +0800 | [diff] [blame] | 8996 | btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE, true); |
Jan Kara | b2b5ef5 | 2012-06-12 16:20:45 +0200 | [diff] [blame] | 8997 | sb_end_pagefault(inode->i_sb); |
Qu Wenruo | 364ecf3 | 2017-02-27 15:10:38 +0800 | [diff] [blame] | 8998 | extent_changeset_free(data_reserved); |
Chris Mason | 50a9b21 | 2009-09-11 12:33:12 -0400 | [diff] [blame] | 8999 | return VM_FAULT_LOCKED; |
Jan Kara | b2b5ef5 | 2012-06-12 16:20:45 +0200 | [diff] [blame] | 9000 | } |
Chris Mason | 717beb9 | 2018-06-25 10:03:41 -0700 | [diff] [blame] | 9001 | |
| 9002 | out_unlock: |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 9003 | unlock_page(page); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 9004 | out: |
Qu Wenruo | 43b1859 | 2017-12-12 15:34:32 +0800 | [diff] [blame] | 9005 | btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE, (ret != 0)); |
Qu Wenruo | bc42bda | 2017-02-27 15:10:39 +0800 | [diff] [blame] | 9006 | btrfs_delalloc_release_space(inode, data_reserved, page_start, |
Qu Wenruo | 43b1859 | 2017-12-12 15:34:32 +0800 | [diff] [blame] | 9007 | reserved_space, (ret != 0)); |
Chris Mason | 9998eb7 | 2012-01-25 13:47:40 -0500 | [diff] [blame] | 9008 | out_noreserve: |
Jan Kara | b2b5ef5 | 2012-06-12 16:20:45 +0200 | [diff] [blame] | 9009 | sb_end_pagefault(inode->i_sb); |
Qu Wenruo | 364ecf3 | 2017-02-27 15:10:38 +0800 | [diff] [blame] | 9010 | extent_changeset_free(data_reserved); |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 9011 | return ret; |
| 9012 | } |
| 9013 | |
Filipe Manana | 213e8c5 | 2018-02-06 20:40:31 +0000 | [diff] [blame] | 9014 | static int btrfs_truncate(struct inode *inode, bool skip_writeback) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9015 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9016 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9017 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 9018 | struct btrfs_block_rsv *rsv; |
Omar Sandoval | ad7e1a7 | 2018-05-22 09:59:50 -0700 | [diff] [blame] | 9019 | int ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9020 | struct btrfs_trans_handle *trans; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9021 | u64 mask = fs_info->sectorsize - 1; |
| 9022 | u64 min_size = btrfs_calc_trunc_metadata_size(fs_info, 1); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9023 | |
Filipe Manana | 213e8c5 | 2018-02-06 20:40:31 +0000 | [diff] [blame] | 9024 | if (!skip_writeback) { |
| 9025 | ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask), |
| 9026 | (u64)-1); |
| 9027 | if (ret) |
| 9028 | return ret; |
| 9029 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9030 | |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 9031 | /* |
Omar Sandoval | f7e9e8f | 2018-05-11 13:13:32 -0700 | [diff] [blame] | 9032 | * Yes ladies and gentlemen, this is indeed ugly. We have a couple of |
| 9033 | * things going on here: |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 9034 | * |
Omar Sandoval | f7e9e8f | 2018-05-11 13:13:32 -0700 | [diff] [blame] | 9035 | * 1) We need to reserve space to update our inode. |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 9036 | * |
Omar Sandoval | f7e9e8f | 2018-05-11 13:13:32 -0700 | [diff] [blame] | 9037 | * 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] | 9038 | * be free'd up by the truncate operation, but also have some slack |
| 9039 | * space reserved in case it uses space during the truncate (thank you |
| 9040 | * very much snapshotting). |
| 9041 | * |
Omar Sandoval | f7e9e8f | 2018-05-11 13:13:32 -0700 | [diff] [blame] | 9042 | * 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] | 9043 | * 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] | 9044 | * 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] | 9045 | * doesn't end up using space reserved for updating the inode. We also |
| 9046 | * need to be able to stop the transaction and start a new one, which |
| 9047 | * means we need to be able to update the inode several times, and we |
| 9048 | * have no idea of knowing how many times that will be, so we can't just |
| 9049 | * reserve 1 item for the entirety of the operation, so that has to be |
| 9050 | * done separately as well. |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 9051 | * |
| 9052 | * So that leaves us with |
| 9053 | * |
Omar Sandoval | f7e9e8f | 2018-05-11 13:13:32 -0700 | [diff] [blame] | 9054 | * 1) rsv - for the truncate reservation, which we will steal from the |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 9055 | * transaction reservation. |
Omar Sandoval | f7e9e8f | 2018-05-11 13:13:32 -0700 | [diff] [blame] | 9056 | * 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] | 9057 | * updating the inode. |
| 9058 | */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 9059 | rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP); |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 9060 | if (!rsv) |
| 9061 | return -ENOMEM; |
Josef Bacik | 4a33854 | 2011-08-29 11:01:31 -0400 | [diff] [blame] | 9062 | rsv->size = min_size; |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 9063 | rsv->failfast = 1; |
Josef Bacik | f0cd846 | 2011-03-04 14:37:08 -0500 | [diff] [blame] | 9064 | |
Josef Bacik | 907cbce | 2011-08-08 13:46:15 -0400 | [diff] [blame] | 9065 | /* |
Josef Bacik | 0712718 | 2011-08-19 10:29:59 -0400 | [diff] [blame] | 9066 | * 1 for the truncate slack space |
Josef Bacik | 907cbce | 2011-08-08 13:46:15 -0400 | [diff] [blame] | 9067 | * 1 for updating the inode. |
| 9068 | */ |
Josef Bacik | f3fe820 | 2013-01-07 17:03:21 -0500 | [diff] [blame] | 9069 | trans = btrfs_start_transaction(root, 2); |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 9070 | if (IS_ERR(trans)) { |
Omar Sandoval | ad7e1a7 | 2018-05-22 09:59:50 -0700 | [diff] [blame] | 9071 | ret = PTR_ERR(trans); |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 9072 | goto out; |
| 9073 | } |
Josef Bacik | f0cd846 | 2011-03-04 14:37:08 -0500 | [diff] [blame] | 9074 | |
Josef Bacik | 907cbce | 2011-08-08 13:46:15 -0400 | [diff] [blame] | 9075 | /* Migrate the slack space for the truncate to our reserve */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9076 | ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, rsv, |
Lu Fengqi | 3a58417 | 2018-08-04 21:10:55 +0800 | [diff] [blame] | 9077 | min_size, false); |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 9078 | BUG_ON(ret); |
Josef Bacik | f0cd846 | 2011-03-04 14:37:08 -0500 | [diff] [blame] | 9079 | |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 9080 | /* |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 9081 | * So if we truncate and then write and fsync we normally would just |
| 9082 | * write the extents that changed, which is a problem if we need to |
| 9083 | * first truncate that entire inode. So set this flag so we write out |
| 9084 | * all of the extents in the inode to the sync log so we're completely |
| 9085 | * safe. |
| 9086 | */ |
| 9087 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags); |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 9088 | trans->block_rsv = rsv; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 9089 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 9090 | while (1) { |
| 9091 | ret = btrfs_truncate_inode_items(trans, root, inode, |
| 9092 | inode->i_size, |
| 9093 | BTRFS_EXTENT_DATA_KEY); |
Josef Bacik | ddfae63 | 2017-10-19 14:16:02 -0400 | [diff] [blame] | 9094 | trans->block_rsv = &fs_info->trans_block_rsv; |
Omar Sandoval | ad7e1a7 | 2018-05-22 09:59:50 -0700 | [diff] [blame] | 9095 | if (ret != -ENOSPC && ret != -EAGAIN) |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 9096 | break; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9097 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 9098 | ret = btrfs_update_inode(trans, root, inode); |
Omar Sandoval | ad7e1a7 | 2018-05-22 09:59:50 -0700 | [diff] [blame] | 9099 | if (ret) |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 9100 | break; |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 9101 | |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 9102 | btrfs_end_transaction(trans); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 9103 | btrfs_btree_balance_dirty(fs_info); |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 9104 | |
| 9105 | trans = btrfs_start_transaction(root, 2); |
| 9106 | if (IS_ERR(trans)) { |
Omar Sandoval | ad7e1a7 | 2018-05-22 09:59:50 -0700 | [diff] [blame] | 9107 | ret = PTR_ERR(trans); |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 9108 | trans = NULL; |
| 9109 | break; |
| 9110 | } |
| 9111 | |
Wang Xiaoguang | 47b5d64 | 2016-09-07 20:17:38 +0800 | [diff] [blame] | 9112 | btrfs_block_rsv_release(fs_info, rsv, -1); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9113 | ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, |
Lu Fengqi | 3a58417 | 2018-08-04 21:10:55 +0800 | [diff] [blame] | 9114 | rsv, min_size, false); |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 9115 | BUG_ON(ret); /* shouldn't happen */ |
| 9116 | trans->block_rsv = rsv; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 9117 | } |
| 9118 | |
Josef Bacik | ddfae63 | 2017-10-19 14:16:02 -0400 | [diff] [blame] | 9119 | /* |
| 9120 | * We can't call btrfs_truncate_block inside a trans handle as we could |
| 9121 | * deadlock with freeze, if we got NEED_TRUNCATE_BLOCK then we know |
| 9122 | * we've truncated everything except the last little bit, and can do |
| 9123 | * btrfs_truncate_block and then update the disk_i_size. |
| 9124 | */ |
| 9125 | if (ret == NEED_TRUNCATE_BLOCK) { |
| 9126 | btrfs_end_transaction(trans); |
| 9127 | btrfs_btree_balance_dirty(fs_info); |
| 9128 | |
| 9129 | ret = btrfs_truncate_block(inode, inode->i_size, 0, 0); |
| 9130 | if (ret) |
| 9131 | goto out; |
| 9132 | trans = btrfs_start_transaction(root, 1); |
| 9133 | if (IS_ERR(trans)) { |
| 9134 | ret = PTR_ERR(trans); |
| 9135 | goto out; |
| 9136 | } |
| 9137 | btrfs_ordered_update_i_size(inode, inode->i_size, NULL); |
| 9138 | } |
| 9139 | |
Chris Mason | 917c16b | 2011-11-08 14:49:59 -0500 | [diff] [blame] | 9140 | if (trans) { |
Omar Sandoval | ad7e1a7 | 2018-05-22 09:59:50 -0700 | [diff] [blame] | 9141 | int ret2; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 9142 | |
Omar Sandoval | ad7e1a7 | 2018-05-22 09:59:50 -0700 | [diff] [blame] | 9143 | trans->block_rsv = &fs_info->trans_block_rsv; |
| 9144 | ret2 = btrfs_update_inode(trans, root, inode); |
| 9145 | if (ret2 && !ret) |
| 9146 | ret = ret2; |
| 9147 | |
| 9148 | ret2 = btrfs_end_transaction(trans); |
| 9149 | if (ret2 && !ret) |
| 9150 | ret = ret2; |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 9151 | btrfs_btree_balance_dirty(fs_info); |
Chris Mason | 917c16b | 2011-11-08 14:49:59 -0500 | [diff] [blame] | 9152 | } |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 9153 | out: |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 9154 | btrfs_free_block_rsv(fs_info, rsv); |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 9155 | |
Omar Sandoval | ad7e1a7 | 2018-05-22 09:59:50 -0700 | [diff] [blame] | 9156 | return ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9157 | } |
| 9158 | |
Sven Wegener | 3b96362 | 2008-06-09 21:57:42 -0400 | [diff] [blame] | 9159 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 9160 | * create a new subvolume directory/inode (helper for the ioctl). |
| 9161 | */ |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 9162 | int btrfs_create_subvol_root(struct btrfs_trans_handle *trans, |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 9163 | struct btrfs_root *new_root, |
| 9164 | struct btrfs_root *parent_root, |
| 9165 | u64 new_dirid) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9166 | { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9167 | struct inode *inode; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 9168 | int err; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 9169 | u64 index = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9170 | |
Florian Albrechtskirchinger | 12fc9d0 | 2012-02-10 22:15:54 +0100 | [diff] [blame] | 9171 | inode = btrfs_new_inode(trans, new_root, NULL, "..", 2, |
| 9172 | new_dirid, new_dirid, |
| 9173 | S_IFDIR | (~current_umask() & S_IRWXUGO), |
| 9174 | &index); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 9175 | if (IS_ERR(inode)) |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 9176 | return PTR_ERR(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9177 | inode->i_op = &btrfs_dir_inode_operations; |
| 9178 | inode->i_fop = &btrfs_dir_file_operations; |
| 9179 | |
Miklos Szeredi | bfe8684 | 2011-10-28 14:13:29 +0200 | [diff] [blame] | 9180 | set_nlink(inode, 1); |
Nikolay Borisov | 6ef06d2 | 2017-02-20 13:50:34 +0200 | [diff] [blame] | 9181 | btrfs_i_size_write(BTRFS_I(inode), 0); |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 9182 | unlock_new_inode(inode); |
Sven Wegener | 3b96362 | 2008-06-09 21:57:42 -0400 | [diff] [blame] | 9183 | |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 9184 | err = btrfs_subvol_inherit_props(trans, new_root, parent_root); |
| 9185 | if (err) |
| 9186 | btrfs_err(new_root->fs_info, |
David Sterba | 351fd35 | 2014-05-15 16:48:20 +0200 | [diff] [blame] | 9187 | "error inheriting subvolume %llu properties: %d", |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 9188 | new_root->root_key.objectid, err); |
| 9189 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 9190 | err = btrfs_update_inode(trans, new_root, inode); |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 9191 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 9192 | iput(inode); |
Mark Fasheh | ce59897 | 2011-07-26 11:32:23 -0700 | [diff] [blame] | 9193 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9194 | } |
| 9195 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9196 | struct inode *btrfs_alloc_inode(struct super_block *sb) |
| 9197 | { |
Josef Bacik | 69fe2d7 | 2017-10-19 14:15:57 -0400 | [diff] [blame] | 9198 | struct btrfs_fs_info *fs_info = btrfs_sb(sb); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9199 | struct btrfs_inode *ei; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9200 | struct inode *inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9201 | |
David Sterba | 712e36c | 2017-10-31 17:08:27 +0100 | [diff] [blame] | 9202 | ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_KERNEL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9203 | if (!ei) |
| 9204 | return NULL; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9205 | |
| 9206 | ei->root = NULL; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9207 | ei->generation = 0; |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 9208 | ei->last_trans = 0; |
Chris Mason | 257c62e | 2009-10-13 13:21:08 -0400 | [diff] [blame] | 9209 | ei->last_sub_trans = 0; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 9210 | ei->logged_trans = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9211 | ei->delalloc_bytes = 0; |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 9212 | ei->new_delalloc_bytes = 0; |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 9213 | ei->defrag_bytes = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9214 | ei->disk_i_size = 0; |
| 9215 | ei->flags = 0; |
Josef Bacik | 7709cde | 2011-08-04 10:25:02 -0400 | [diff] [blame] | 9216 | ei->csum_bytes = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9217 | ei->index_cnt = (u64)-1; |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 9218 | ei->dir_index = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9219 | ei->last_unlink_trans = 0; |
Liu Bo | 46d8bc3 | 2012-08-29 01:07:55 -0600 | [diff] [blame] | 9220 | ei->last_log_commit = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9221 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 9222 | spin_lock_init(&ei->lock); |
| 9223 | ei->outstanding_extents = 0; |
Josef Bacik | 69fe2d7 | 2017-10-19 14:15:57 -0400 | [diff] [blame] | 9224 | if (sb->s_magic != BTRFS_TEST_MAGIC) |
| 9225 | btrfs_init_metadata_block_rsv(fs_info, &ei->block_rsv, |
| 9226 | BTRFS_BLOCK_RSV_DELALLOC); |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 9227 | ei->runtime_flags = 0; |
David Sterba | b52aa8c | 2017-07-17 19:17:20 +0200 | [diff] [blame] | 9228 | ei->prop_compress = BTRFS_COMPRESS_NONE; |
David Sterba | eec63c6 | 2017-07-17 19:41:31 +0200 | [diff] [blame] | 9229 | ei->defrag_compress = BTRFS_COMPRESS_NONE; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9230 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 9231 | ei->delayed_node = NULL; |
| 9232 | |
chandan r | 9cc97d6 | 2012-07-04 12:48:07 +0530 | [diff] [blame] | 9233 | ei->i_otime.tv_sec = 0; |
| 9234 | ei->i_otime.tv_nsec = 0; |
| 9235 | |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9236 | inode = &ei->vfs_inode; |
David Sterba | a8067e0 | 2011-04-21 00:34:43 +0200 | [diff] [blame] | 9237 | extent_map_tree_init(&ei->extent_tree); |
Qu Wenruo | 43eb5f2 | 2019-03-01 10:47:59 +0800 | [diff] [blame] | 9238 | extent_io_tree_init(fs_info, &ei->io_tree, IO_TREE_INODE_IO, inode); |
| 9239 | extent_io_tree_init(fs_info, &ei->io_failure_tree, |
| 9240 | IO_TREE_INODE_IO_FAILURE, inode); |
David Sterba | 7b43973 | 2019-03-11 15:58:30 +0100 | [diff] [blame] | 9241 | ei->io_tree.track_uptodate = true; |
| 9242 | ei->io_failure_tree.track_uptodate = true; |
Josef Bacik | b812ce2 | 2012-11-16 13:56:32 -0500 | [diff] [blame] | 9243 | atomic_set(&ei->sync_writers, 0); |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9244 | mutex_init(&ei->log_mutex); |
Josef Bacik | f248679 | 2012-01-13 12:09:22 -0500 | [diff] [blame] | 9245 | mutex_init(&ei->delalloc_mutex); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 9246 | btrfs_ordered_inode_tree_init(&ei->ordered_tree); |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9247 | INIT_LIST_HEAD(&ei->delalloc_inodes); |
David Sterba | 8089fe6 | 2015-11-19 14:15:51 +0100 | [diff] [blame] | 9248 | INIT_LIST_HEAD(&ei->delayed_iput); |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9249 | RB_CLEAR_NODE(&ei->rb_node); |
Filipe Manana | 5f9a8a5 | 2016-05-12 13:53:36 +0100 | [diff] [blame] | 9250 | init_rwsem(&ei->dio_sem); |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9251 | |
| 9252 | return inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9253 | } |
| 9254 | |
Josef Bacik | aaedb55 | 2013-10-11 14:44:09 -0400 | [diff] [blame] | 9255 | #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS |
| 9256 | void btrfs_test_destroy_inode(struct inode *inode) |
| 9257 | { |
Nikolay Borisov | dcdbc05 | 2017-02-20 13:50:45 +0200 | [diff] [blame] | 9258 | btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0); |
Josef Bacik | aaedb55 | 2013-10-11 14:44:09 -0400 | [diff] [blame] | 9259 | kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode)); |
| 9260 | } |
| 9261 | #endif |
| 9262 | |
Nick Piggin | fa0d7e3d | 2011-01-07 17:49:49 +1100 | [diff] [blame] | 9263 | static void btrfs_i_callback(struct rcu_head *head) |
| 9264 | { |
| 9265 | struct inode *inode = container_of(head, struct inode, i_rcu); |
Nick Piggin | fa0d7e3d | 2011-01-07 17:49:49 +1100 | [diff] [blame] | 9266 | kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode)); |
| 9267 | } |
| 9268 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9269 | void btrfs_destroy_inode(struct inode *inode) |
| 9270 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9271 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 9272 | struct btrfs_ordered_extent *ordered; |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 9273 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 9274 | |
Al Viro | b3d9b7a | 2012-06-09 13:51:19 -0400 | [diff] [blame] | 9275 | WARN_ON(!hlist_empty(&inode->i_dentry)); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9276 | WARN_ON(inode->i_data.nrpages); |
Josef Bacik | 69fe2d7 | 2017-10-19 14:15:57 -0400 | [diff] [blame] | 9277 | WARN_ON(BTRFS_I(inode)->block_rsv.reserved); |
| 9278 | WARN_ON(BTRFS_I(inode)->block_rsv.size); |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 9279 | WARN_ON(BTRFS_I(inode)->outstanding_extents); |
Josef Bacik | 7709cde | 2011-08-04 10:25:02 -0400 | [diff] [blame] | 9280 | WARN_ON(BTRFS_I(inode)->delalloc_bytes); |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 9281 | WARN_ON(BTRFS_I(inode)->new_delalloc_bytes); |
Josef Bacik | 7709cde | 2011-08-04 10:25:02 -0400 | [diff] [blame] | 9282 | WARN_ON(BTRFS_I(inode)->csum_bytes); |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 9283 | WARN_ON(BTRFS_I(inode)->defrag_bytes); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9284 | |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 9285 | /* |
Josef Bacik | a6dbd42 | 2009-11-11 15:53:34 -0500 | [diff] [blame] | 9286 | * This can happen where we create an inode, but somebody else also |
| 9287 | * created the same inode and we need to destroy the one we already |
| 9288 | * created. |
| 9289 | */ |
| 9290 | if (!root) |
| 9291 | goto free; |
| 9292 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 9293 | while (1) { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 9294 | ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1); |
| 9295 | if (!ordered) |
| 9296 | break; |
| 9297 | else { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9298 | btrfs_err(fs_info, |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 9299 | "found ordered extent %llu %llu on inode cleanup", |
| 9300 | ordered->file_offset, ordered->len); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 9301 | btrfs_remove_ordered_extent(inode, ordered); |
| 9302 | btrfs_put_ordered_extent(ordered); |
| 9303 | btrfs_put_ordered_extent(ordered); |
| 9304 | } |
| 9305 | } |
Qu Wenruo | 56fa9d0 | 2015-10-13 09:53:10 +0800 | [diff] [blame] | 9306 | btrfs_qgroup_check_reserved_leak(inode); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 9307 | inode_tree_del(inode); |
Nikolay Borisov | dcdbc05 | 2017-02-20 13:50:45 +0200 | [diff] [blame] | 9308 | btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0); |
Josef Bacik | a6dbd42 | 2009-11-11 15:53:34 -0500 | [diff] [blame] | 9309 | free: |
Nick Piggin | fa0d7e3d | 2011-01-07 17:49:49 +1100 | [diff] [blame] | 9310 | call_rcu(&inode->i_rcu, btrfs_i_callback); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9311 | } |
| 9312 | |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 9313 | int btrfs_drop_inode(struct inode *inode) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 9314 | { |
| 9315 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 9316 | |
Naohiro Aota | 6379ef9 | 2013-06-06 09:56:34 +0000 | [diff] [blame] | 9317 | if (root == NULL) |
| 9318 | return 1; |
| 9319 | |
Liu Bo | fa6ac87 | 2013-02-20 14:10:23 +0000 | [diff] [blame] | 9320 | /* the snap/subvol tree is on deleting */ |
Stefan Behrens | 69e9c6c | 2013-09-05 16:58:43 +0200 | [diff] [blame] | 9321 | if (btrfs_root_refs(&root->root_item) == 0) |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 9322 | return 1; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 9323 | else |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 9324 | return generic_drop_inode(inode); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 9325 | } |
| 9326 | |
Sven Wegener | 0ee0fda | 2008-07-30 16:54:26 -0400 | [diff] [blame] | 9327 | static void init_once(void *foo) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9328 | { |
| 9329 | struct btrfs_inode *ei = (struct btrfs_inode *) foo; |
| 9330 | |
| 9331 | inode_init_once(&ei->vfs_inode); |
| 9332 | } |
| 9333 | |
David Sterba | e67c718 | 2018-02-19 17:24:18 +0100 | [diff] [blame] | 9334 | void __cold btrfs_destroy_cachep(void) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9335 | { |
Kirill A. Shutemov | 8c0a853 | 2012-09-26 11:33:07 +1000 | [diff] [blame] | 9336 | /* |
| 9337 | * Make sure all delayed rcu free inodes are flushed before we |
| 9338 | * destroy cache. |
| 9339 | */ |
| 9340 | rcu_barrier(); |
Kinglong Mee | 5598e90 | 2016-01-29 21:36:35 +0800 | [diff] [blame] | 9341 | kmem_cache_destroy(btrfs_inode_cachep); |
| 9342 | kmem_cache_destroy(btrfs_trans_handle_cachep); |
Kinglong Mee | 5598e90 | 2016-01-29 21:36:35 +0800 | [diff] [blame] | 9343 | kmem_cache_destroy(btrfs_path_cachep); |
| 9344 | kmem_cache_destroy(btrfs_free_space_cachep); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9345 | } |
| 9346 | |
Liu Bo | f5c29bd | 2017-11-02 17:21:50 -0600 | [diff] [blame] | 9347 | int __init btrfs_init_cachep(void) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9348 | { |
David Sterba | 837e197 | 2012-09-07 03:00:48 -0600 | [diff] [blame] | 9349 | btrfs_inode_cachep = kmem_cache_create("btrfs_inode", |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 9350 | sizeof(struct btrfs_inode), 0, |
Vladimir Davydov | 5d09705 | 2016-01-14 15:18:21 -0800 | [diff] [blame] | 9351 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT, |
| 9352 | init_once); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9353 | if (!btrfs_inode_cachep) |
| 9354 | goto fail; |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 9355 | |
David Sterba | 837e197 | 2012-09-07 03:00:48 -0600 | [diff] [blame] | 9356 | btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle", |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 9357 | sizeof(struct btrfs_trans_handle), 0, |
Nikolay Borisov | fba4b69 | 2016-06-23 21:17:08 +0300 | [diff] [blame] | 9358 | SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9359 | if (!btrfs_trans_handle_cachep) |
| 9360 | goto fail; |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 9361 | |
David Sterba | 837e197 | 2012-09-07 03:00:48 -0600 | [diff] [blame] | 9362 | btrfs_path_cachep = kmem_cache_create("btrfs_path", |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 9363 | sizeof(struct btrfs_path), 0, |
Nikolay Borisov | fba4b69 | 2016-06-23 21:17:08 +0300 | [diff] [blame] | 9364 | SLAB_MEM_SPREAD, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9365 | if (!btrfs_path_cachep) |
| 9366 | goto fail; |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 9367 | |
David Sterba | 837e197 | 2012-09-07 03:00:48 -0600 | [diff] [blame] | 9368 | btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space", |
Josef Bacik | dc89e98 | 2011-01-28 17:05:48 -0500 | [diff] [blame] | 9369 | sizeof(struct btrfs_free_space), 0, |
Nikolay Borisov | fba4b69 | 2016-06-23 21:17:08 +0300 | [diff] [blame] | 9370 | SLAB_MEM_SPREAD, NULL); |
Josef Bacik | dc89e98 | 2011-01-28 17:05:48 -0500 | [diff] [blame] | 9371 | if (!btrfs_free_space_cachep) |
| 9372 | goto fail; |
| 9373 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9374 | return 0; |
| 9375 | fail: |
| 9376 | btrfs_destroy_cachep(); |
| 9377 | return -ENOMEM; |
| 9378 | } |
| 9379 | |
David Howells | a528d35 | 2017-01-31 16:46:22 +0000 | [diff] [blame] | 9380 | static int btrfs_getattr(const struct path *path, struct kstat *stat, |
| 9381 | u32 request_mask, unsigned int flags) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9382 | { |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 9383 | u64 delalloc_bytes; |
David Howells | a528d35 | 2017-01-31 16:46:22 +0000 | [diff] [blame] | 9384 | struct inode *inode = d_inode(path->dentry); |
David Sterba | fadc0d8 | 2011-11-20 07:33:38 -0500 | [diff] [blame] | 9385 | u32 blocksize = inode->i_sb->s_blocksize; |
Yonghong Song | 04a87e3 | 2017-05-12 15:07:43 -0700 | [diff] [blame] | 9386 | u32 bi_flags = BTRFS_I(inode)->flags; |
| 9387 | |
| 9388 | stat->result_mask |= STATX_BTIME; |
| 9389 | stat->btime.tv_sec = BTRFS_I(inode)->i_otime.tv_sec; |
| 9390 | stat->btime.tv_nsec = BTRFS_I(inode)->i_otime.tv_nsec; |
| 9391 | if (bi_flags & BTRFS_INODE_APPEND) |
| 9392 | stat->attributes |= STATX_ATTR_APPEND; |
| 9393 | if (bi_flags & BTRFS_INODE_COMPRESS) |
| 9394 | stat->attributes |= STATX_ATTR_COMPRESSED; |
| 9395 | if (bi_flags & BTRFS_INODE_IMMUTABLE) |
| 9396 | stat->attributes |= STATX_ATTR_IMMUTABLE; |
| 9397 | if (bi_flags & BTRFS_INODE_NODUMP) |
| 9398 | stat->attributes |= STATX_ATTR_NODUMP; |
| 9399 | |
| 9400 | stat->attributes_mask |= (STATX_ATTR_APPEND | |
| 9401 | STATX_ATTR_COMPRESSED | |
| 9402 | STATX_ATTR_IMMUTABLE | |
| 9403 | STATX_ATTR_NODUMP); |
David Sterba | fadc0d8 | 2011-11-20 07:33:38 -0500 | [diff] [blame] | 9404 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9405 | generic_fillattr(inode, stat); |
Al Viro | 0ee5dc6 | 2011-07-07 15:44:25 -0400 | [diff] [blame] | 9406 | stat->dev = BTRFS_I(inode)->root->anon_dev; |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 9407 | |
| 9408 | spin_lock(&BTRFS_I(inode)->lock); |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 9409 | delalloc_bytes = BTRFS_I(inode)->new_delalloc_bytes; |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 9410 | spin_unlock(&BTRFS_I(inode)->lock); |
David Sterba | fadc0d8 | 2011-11-20 07:33:38 -0500 | [diff] [blame] | 9411 | stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) + |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 9412 | ALIGN(delalloc_bytes, blocksize)) >> 9; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9413 | return 0; |
| 9414 | } |
| 9415 | |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9416 | static int btrfs_rename_exchange(struct inode *old_dir, |
| 9417 | struct dentry *old_dentry, |
| 9418 | struct inode *new_dir, |
| 9419 | struct dentry *new_dentry) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9420 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9421 | struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9422 | struct btrfs_trans_handle *trans; |
| 9423 | struct btrfs_root *root = BTRFS_I(old_dir)->root; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9424 | struct btrfs_root *dest = BTRFS_I(new_dir)->root; |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9425 | struct inode *new_inode = new_dentry->d_inode; |
| 9426 | struct inode *old_inode = old_dentry->d_inode; |
Deepa Dinamani | 95582b0 | 2018-05-08 19:36:02 -0700 | [diff] [blame] | 9427 | struct timespec64 ctime = current_time(old_inode); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9428 | struct dentry *parent; |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 9429 | u64 old_ino = btrfs_ino(BTRFS_I(old_inode)); |
| 9430 | u64 new_ino = btrfs_ino(BTRFS_I(new_inode)); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9431 | u64 old_idx = 0; |
| 9432 | u64 new_idx = 0; |
| 9433 | u64 root_objectid; |
| 9434 | int ret; |
Filipe Manana | 86e8aa0 | 2016-05-05 02:02:27 +0100 | [diff] [blame] | 9435 | bool root_log_pinned = false; |
| 9436 | bool dest_log_pinned = false; |
Filipe Manana | d4682ba | 2018-06-11 19:24:28 +0100 | [diff] [blame] | 9437 | struct btrfs_log_ctx ctx_root; |
| 9438 | struct btrfs_log_ctx ctx_dest; |
| 9439 | bool sync_log_root = false; |
| 9440 | bool sync_log_dest = false; |
| 9441 | bool commit_transaction = false; |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9442 | |
| 9443 | /* we only allow rename subvolume link between subvolumes */ |
| 9444 | if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest) |
| 9445 | return -EXDEV; |
| 9446 | |
Filipe Manana | d4682ba | 2018-06-11 19:24:28 +0100 | [diff] [blame] | 9447 | btrfs_init_log_ctx(&ctx_root, old_inode); |
| 9448 | btrfs_init_log_ctx(&ctx_dest, new_inode); |
| 9449 | |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9450 | /* close the race window with snapshot create/destroy ioctl */ |
| 9451 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9452 | down_read(&fs_info->subvol_sem); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9453 | if (new_ino == BTRFS_FIRST_FREE_OBJECTID) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9454 | down_read(&fs_info->subvol_sem); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9455 | |
| 9456 | /* |
| 9457 | * We want to reserve the absolute worst case amount of items. So if |
| 9458 | * both inodes are subvols and we need to unlink them then that would |
| 9459 | * require 4 item modifications, but if they are both normal inodes it |
| 9460 | * would require 5 item modifications, so we'll assume their normal |
| 9461 | * inodes. So 5 * 2 is 10, plus 2 for the new links, so 12 total items |
| 9462 | * should cover the worst case number of items we'll modify. |
| 9463 | */ |
| 9464 | trans = btrfs_start_transaction(root, 12); |
| 9465 | if (IS_ERR(trans)) { |
| 9466 | ret = PTR_ERR(trans); |
| 9467 | goto out_notrans; |
| 9468 | } |
| 9469 | |
| 9470 | /* |
| 9471 | * We need to find a free sequence number both in the source and |
| 9472 | * in the destination directory for the exchange. |
| 9473 | */ |
Nikolay Borisov | 877574e | 2017-02-20 13:50:33 +0200 | [diff] [blame] | 9474 | ret = btrfs_set_inode_index(BTRFS_I(new_dir), &old_idx); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9475 | if (ret) |
| 9476 | goto out_fail; |
Nikolay Borisov | 877574e | 2017-02-20 13:50:33 +0200 | [diff] [blame] | 9477 | ret = btrfs_set_inode_index(BTRFS_I(old_dir), &new_idx); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9478 | if (ret) |
| 9479 | goto out_fail; |
| 9480 | |
| 9481 | BTRFS_I(old_inode)->dir_index = 0ULL; |
| 9482 | BTRFS_I(new_inode)->dir_index = 0ULL; |
| 9483 | |
| 9484 | /* Reference for the source. */ |
| 9485 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) { |
| 9486 | /* force full log commit if subvolume involved. */ |
David Sterba | 9078776 | 2019-03-20 13:28:05 +0100 | [diff] [blame] | 9487 | btrfs_set_log_full_commit(trans); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9488 | } else { |
Filipe Manana | 376e5a5 | 2016-05-05 02:08:56 +0100 | [diff] [blame] | 9489 | btrfs_pin_log_trans(root); |
| 9490 | root_log_pinned = true; |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9491 | ret = btrfs_insert_inode_ref(trans, dest, |
| 9492 | new_dentry->d_name.name, |
| 9493 | new_dentry->d_name.len, |
| 9494 | old_ino, |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 9495 | btrfs_ino(BTRFS_I(new_dir)), |
| 9496 | old_idx); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9497 | if (ret) |
| 9498 | goto out_fail; |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9499 | } |
| 9500 | |
| 9501 | /* And now for the dest. */ |
| 9502 | if (new_ino == BTRFS_FIRST_FREE_OBJECTID) { |
| 9503 | /* force full log commit if subvolume involved. */ |
David Sterba | 9078776 | 2019-03-20 13:28:05 +0100 | [diff] [blame] | 9504 | btrfs_set_log_full_commit(trans); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9505 | } else { |
Filipe Manana | 376e5a5 | 2016-05-05 02:08:56 +0100 | [diff] [blame] | 9506 | btrfs_pin_log_trans(dest); |
| 9507 | dest_log_pinned = true; |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9508 | ret = btrfs_insert_inode_ref(trans, root, |
| 9509 | old_dentry->d_name.name, |
| 9510 | old_dentry->d_name.len, |
| 9511 | new_ino, |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 9512 | btrfs_ino(BTRFS_I(old_dir)), |
| 9513 | new_idx); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9514 | if (ret) |
| 9515 | goto out_fail; |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9516 | } |
| 9517 | |
| 9518 | /* Update inode version and ctime/mtime. */ |
| 9519 | inode_inc_iversion(old_dir); |
| 9520 | inode_inc_iversion(new_dir); |
| 9521 | inode_inc_iversion(old_inode); |
| 9522 | inode_inc_iversion(new_inode); |
| 9523 | old_dir->i_ctime = old_dir->i_mtime = ctime; |
| 9524 | new_dir->i_ctime = new_dir->i_mtime = ctime; |
| 9525 | old_inode->i_ctime = ctime; |
| 9526 | new_inode->i_ctime = ctime; |
| 9527 | |
| 9528 | if (old_dentry->d_parent != new_dentry->d_parent) { |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 9529 | btrfs_record_unlink_dir(trans, BTRFS_I(old_dir), |
| 9530 | BTRFS_I(old_inode), 1); |
| 9531 | btrfs_record_unlink_dir(trans, BTRFS_I(new_dir), |
| 9532 | BTRFS_I(new_inode), 1); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9533 | } |
| 9534 | |
| 9535 | /* src is a subvolume */ |
| 9536 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) { |
| 9537 | root_objectid = BTRFS_I(old_inode)->root->root_key.objectid; |
Lu Fengqi | 401b3b1 | 2018-08-01 11:32:30 +0800 | [diff] [blame] | 9538 | ret = btrfs_unlink_subvol(trans, old_dir, root_objectid, |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9539 | old_dentry->d_name.name, |
| 9540 | old_dentry->d_name.len); |
| 9541 | } else { /* src is an inode */ |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 9542 | ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir), |
| 9543 | BTRFS_I(old_dentry->d_inode), |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9544 | old_dentry->d_name.name, |
| 9545 | old_dentry->d_name.len); |
| 9546 | if (!ret) |
| 9547 | ret = btrfs_update_inode(trans, root, old_inode); |
| 9548 | } |
| 9549 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 9550 | btrfs_abort_transaction(trans, ret); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9551 | goto out_fail; |
| 9552 | } |
| 9553 | |
| 9554 | /* dest is a subvolume */ |
| 9555 | if (new_ino == BTRFS_FIRST_FREE_OBJECTID) { |
| 9556 | root_objectid = BTRFS_I(new_inode)->root->root_key.objectid; |
Lu Fengqi | 401b3b1 | 2018-08-01 11:32:30 +0800 | [diff] [blame] | 9557 | ret = btrfs_unlink_subvol(trans, new_dir, root_objectid, |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9558 | new_dentry->d_name.name, |
| 9559 | new_dentry->d_name.len); |
| 9560 | } else { /* dest is an inode */ |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 9561 | ret = __btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir), |
| 9562 | BTRFS_I(new_dentry->d_inode), |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9563 | new_dentry->d_name.name, |
| 9564 | new_dentry->d_name.len); |
| 9565 | if (!ret) |
| 9566 | ret = btrfs_update_inode(trans, dest, new_inode); |
| 9567 | } |
| 9568 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 9569 | btrfs_abort_transaction(trans, ret); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9570 | goto out_fail; |
| 9571 | } |
| 9572 | |
Nikolay Borisov | db0a669 | 2017-02-20 13:51:08 +0200 | [diff] [blame] | 9573 | 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] | 9574 | new_dentry->d_name.name, |
| 9575 | new_dentry->d_name.len, 0, old_idx); |
| 9576 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 9577 | btrfs_abort_transaction(trans, ret); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9578 | goto out_fail; |
| 9579 | } |
| 9580 | |
Nikolay Borisov | db0a669 | 2017-02-20 13:51:08 +0200 | [diff] [blame] | 9581 | 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] | 9582 | old_dentry->d_name.name, |
| 9583 | old_dentry->d_name.len, 0, new_idx); |
| 9584 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 9585 | btrfs_abort_transaction(trans, ret); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9586 | goto out_fail; |
| 9587 | } |
| 9588 | |
| 9589 | if (old_inode->i_nlink == 1) |
| 9590 | BTRFS_I(old_inode)->dir_index = old_idx; |
| 9591 | if (new_inode->i_nlink == 1) |
| 9592 | BTRFS_I(new_inode)->dir_index = new_idx; |
| 9593 | |
Filipe Manana | 86e8aa0 | 2016-05-05 02:02:27 +0100 | [diff] [blame] | 9594 | if (root_log_pinned) { |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9595 | parent = new_dentry->d_parent; |
Filipe Manana | d4682ba | 2018-06-11 19:24:28 +0100 | [diff] [blame] | 9596 | ret = btrfs_log_new_name(trans, BTRFS_I(old_inode), |
| 9597 | BTRFS_I(old_dir), parent, |
| 9598 | false, &ctx_root); |
| 9599 | if (ret == BTRFS_NEED_LOG_SYNC) |
| 9600 | sync_log_root = true; |
| 9601 | else if (ret == BTRFS_NEED_TRANS_COMMIT) |
| 9602 | commit_transaction = true; |
| 9603 | ret = 0; |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9604 | btrfs_end_log_trans(root); |
Filipe Manana | 86e8aa0 | 2016-05-05 02:02:27 +0100 | [diff] [blame] | 9605 | root_log_pinned = false; |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9606 | } |
Filipe Manana | 86e8aa0 | 2016-05-05 02:02:27 +0100 | [diff] [blame] | 9607 | if (dest_log_pinned) { |
Filipe Manana | d4682ba | 2018-06-11 19:24:28 +0100 | [diff] [blame] | 9608 | if (!commit_transaction) { |
| 9609 | parent = old_dentry->d_parent; |
| 9610 | ret = btrfs_log_new_name(trans, BTRFS_I(new_inode), |
| 9611 | BTRFS_I(new_dir), parent, |
| 9612 | false, &ctx_dest); |
| 9613 | if (ret == BTRFS_NEED_LOG_SYNC) |
| 9614 | sync_log_dest = true; |
| 9615 | else if (ret == BTRFS_NEED_TRANS_COMMIT) |
| 9616 | commit_transaction = true; |
| 9617 | ret = 0; |
| 9618 | } |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9619 | btrfs_end_log_trans(dest); |
Filipe Manana | 86e8aa0 | 2016-05-05 02:02:27 +0100 | [diff] [blame] | 9620 | dest_log_pinned = false; |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9621 | } |
| 9622 | out_fail: |
Filipe Manana | 86e8aa0 | 2016-05-05 02:02:27 +0100 | [diff] [blame] | 9623 | /* |
| 9624 | * If we have pinned a log and an error happened, we unpin tasks |
| 9625 | * trying to sync the log and force them to fallback to a transaction |
| 9626 | * commit if the log currently contains any of the inodes involved in |
| 9627 | * this rename operation (to ensure we do not persist a log with an |
| 9628 | * inconsistent state for any of these inodes or leading to any |
| 9629 | * inconsistencies when replayed). If the transaction was aborted, the |
| 9630 | * abortion reason is propagated to userspace when attempting to commit |
| 9631 | * the transaction. If the log does not contain any of these inodes, we |
| 9632 | * allow the tasks to sync it. |
| 9633 | */ |
| 9634 | if (ret && (root_log_pinned || dest_log_pinned)) { |
Nikolay Borisov | 0f8939b | 2017-01-18 00:31:30 +0200 | [diff] [blame] | 9635 | if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) || |
| 9636 | btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) || |
| 9637 | btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) || |
Filipe Manana | 86e8aa0 | 2016-05-05 02:02:27 +0100 | [diff] [blame] | 9638 | (new_inode && |
Nikolay Borisov | 0f8939b | 2017-01-18 00:31:30 +0200 | [diff] [blame] | 9639 | btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation))) |
David Sterba | 9078776 | 2019-03-20 13:28:05 +0100 | [diff] [blame] | 9640 | btrfs_set_log_full_commit(trans); |
Filipe Manana | 86e8aa0 | 2016-05-05 02:02:27 +0100 | [diff] [blame] | 9641 | |
| 9642 | if (root_log_pinned) { |
| 9643 | btrfs_end_log_trans(root); |
| 9644 | root_log_pinned = false; |
| 9645 | } |
| 9646 | if (dest_log_pinned) { |
| 9647 | btrfs_end_log_trans(dest); |
| 9648 | dest_log_pinned = false; |
| 9649 | } |
| 9650 | } |
Filipe Manana | d4682ba | 2018-06-11 19:24:28 +0100 | [diff] [blame] | 9651 | if (!ret && sync_log_root && !commit_transaction) { |
| 9652 | ret = btrfs_sync_log(trans, BTRFS_I(old_inode)->root, |
| 9653 | &ctx_root); |
| 9654 | if (ret) |
| 9655 | commit_transaction = true; |
| 9656 | } |
| 9657 | if (!ret && sync_log_dest && !commit_transaction) { |
| 9658 | ret = btrfs_sync_log(trans, BTRFS_I(new_inode)->root, |
| 9659 | &ctx_dest); |
| 9660 | if (ret) |
| 9661 | commit_transaction = true; |
| 9662 | } |
| 9663 | if (commit_transaction) { |
| 9664 | ret = btrfs_commit_transaction(trans); |
| 9665 | } else { |
| 9666 | int ret2; |
| 9667 | |
| 9668 | ret2 = btrfs_end_transaction(trans); |
| 9669 | ret = ret ? ret : ret2; |
| 9670 | } |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9671 | out_notrans: |
| 9672 | if (new_ino == BTRFS_FIRST_FREE_OBJECTID) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9673 | up_read(&fs_info->subvol_sem); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9674 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9675 | up_read(&fs_info->subvol_sem); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9676 | |
| 9677 | return ret; |
| 9678 | } |
| 9679 | |
| 9680 | static int btrfs_whiteout_for_rename(struct btrfs_trans_handle *trans, |
| 9681 | struct btrfs_root *root, |
| 9682 | struct inode *dir, |
| 9683 | struct dentry *dentry) |
| 9684 | { |
| 9685 | int ret; |
| 9686 | struct inode *inode; |
| 9687 | u64 objectid; |
| 9688 | u64 index; |
| 9689 | |
| 9690 | ret = btrfs_find_free_ino(root, &objectid); |
| 9691 | if (ret) |
| 9692 | return ret; |
| 9693 | |
| 9694 | inode = btrfs_new_inode(trans, root, dir, |
| 9695 | dentry->d_name.name, |
| 9696 | dentry->d_name.len, |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 9697 | btrfs_ino(BTRFS_I(dir)), |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9698 | objectid, |
| 9699 | S_IFCHR | WHITEOUT_MODE, |
| 9700 | &index); |
| 9701 | |
| 9702 | if (IS_ERR(inode)) { |
| 9703 | ret = PTR_ERR(inode); |
| 9704 | return ret; |
| 9705 | } |
| 9706 | |
| 9707 | inode->i_op = &btrfs_special_inode_operations; |
| 9708 | init_special_inode(inode, inode->i_mode, |
| 9709 | WHITEOUT_DEV); |
| 9710 | |
| 9711 | ret = btrfs_init_inode_security(trans, inode, dir, |
| 9712 | &dentry->d_name); |
| 9713 | if (ret) |
Filipe Manana | c990161 | 2016-05-05 01:41:57 +0100 | [diff] [blame] | 9714 | goto out; |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9715 | |
Nikolay Borisov | cef415a | 2017-02-20 13:51:09 +0200 | [diff] [blame] | 9716 | ret = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, |
| 9717 | BTRFS_I(inode), 0, index); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9718 | if (ret) |
Filipe Manana | c990161 | 2016-05-05 01:41:57 +0100 | [diff] [blame] | 9719 | goto out; |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9720 | |
| 9721 | ret = btrfs_update_inode(trans, root, inode); |
Filipe Manana | c990161 | 2016-05-05 01:41:57 +0100 | [diff] [blame] | 9722 | out: |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9723 | unlock_new_inode(inode); |
Filipe Manana | c990161 | 2016-05-05 01:41:57 +0100 | [diff] [blame] | 9724 | if (ret) |
| 9725 | inode_dec_link_count(inode); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9726 | iput(inode); |
| 9727 | |
Filipe Manana | c990161 | 2016-05-05 01:41:57 +0100 | [diff] [blame] | 9728 | return ret; |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9729 | } |
| 9730 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9731 | static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry, |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9732 | struct inode *new_dir, struct dentry *new_dentry, |
| 9733 | unsigned int flags) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9734 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9735 | struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9736 | struct btrfs_trans_handle *trans; |
Filipe Manana | 5062af3 | 2016-05-05 10:26:26 +0100 | [diff] [blame] | 9737 | unsigned int trans_num_items; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9738 | struct btrfs_root *root = BTRFS_I(old_dir)->root; |
| 9739 | struct btrfs_root *dest = BTRFS_I(new_dir)->root; |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 9740 | struct inode *new_inode = d_inode(new_dentry); |
| 9741 | struct inode *old_inode = d_inode(old_dentry); |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 9742 | u64 index = 0; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9743 | u64 root_objectid; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9744 | int ret; |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 9745 | u64 old_ino = btrfs_ino(BTRFS_I(old_inode)); |
Filipe Manana | 3dc9e8f7 | 2016-04-29 11:34:22 +0100 | [diff] [blame] | 9746 | bool log_pinned = false; |
Filipe Manana | d4682ba | 2018-06-11 19:24:28 +0100 | [diff] [blame] | 9747 | struct btrfs_log_ctx ctx; |
| 9748 | bool sync_log = false; |
| 9749 | bool commit_transaction = false; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9750 | |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 9751 | if (btrfs_ino(BTRFS_I(new_dir)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) |
Yan, Zheng | f679a84 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 9752 | return -EPERM; |
| 9753 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9754 | /* we only allow rename subvolume link between subvolumes */ |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 9755 | if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest) |
Chris Mason | 3394e16 | 2008-11-17 20:42:26 -0500 | [diff] [blame] | 9756 | return -EXDEV; |
| 9757 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 9758 | if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID || |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 9759 | (new_inode && btrfs_ino(BTRFS_I(new_inode)) == BTRFS_FIRST_FREE_OBJECTID)) |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9760 | return -ENOTEMPTY; |
| 9761 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9762 | if (S_ISDIR(old_inode->i_mode) && new_inode && |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9763 | new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9764 | return -ENOTEMPTY; |
Chris Mason | 9c52057 | 2012-12-17 14:26:57 -0500 | [diff] [blame] | 9765 | |
| 9766 | |
| 9767 | /* check for collisions, even if the name isn't there */ |
Josef Bacik | 4871c15 | 2013-10-09 12:24:04 -0400 | [diff] [blame] | 9768 | ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino, |
Chris Mason | 9c52057 | 2012-12-17 14:26:57 -0500 | [diff] [blame] | 9769 | new_dentry->d_name.name, |
| 9770 | new_dentry->d_name.len); |
| 9771 | |
| 9772 | if (ret) { |
| 9773 | if (ret == -EEXIST) { |
| 9774 | /* we shouldn't get |
| 9775 | * eexist without a new_inode */ |
Dulshani Gunawardhana | fae7f21 | 2013-10-31 10:30:08 +0530 | [diff] [blame] | 9776 | if (WARN_ON(!new_inode)) { |
Chris Mason | 9c52057 | 2012-12-17 14:26:57 -0500 | [diff] [blame] | 9777 | return ret; |
| 9778 | } |
| 9779 | } else { |
| 9780 | /* maybe -EOVERFLOW */ |
| 9781 | return ret; |
| 9782 | } |
| 9783 | } |
| 9784 | ret = 0; |
| 9785 | |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 9786 | /* |
Chris Mason | 8d875f9 | 2014-08-12 10:47:42 -0700 | [diff] [blame] | 9787 | * we're using rename to replace one file with another. Start IO on it |
| 9788 | * 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] | 9789 | */ |
Chris Mason | 8d875f9 | 2014-08-12 10:47:42 -0700 | [diff] [blame] | 9790 | 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] | 9791 | filemap_flush(old_inode->i_mapping); |
| 9792 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 9793 | /* close the racy window with snapshot create/destroy ioctl */ |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 9794 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9795 | down_read(&fs_info->subvol_sem); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 9796 | /* |
| 9797 | * We want to reserve the absolute worst case amount of items. So if |
| 9798 | * both inodes are subvols and we need to unlink them then that would |
| 9799 | * require 4 item modifications, but if they are both normal inodes it |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9800 | * would require 5 item modifications, so we'll assume they are normal |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 9801 | * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items |
| 9802 | * should cover the worst case number of items we'll modify. |
Filipe Manana | 5062af3 | 2016-05-05 10:26:26 +0100 | [diff] [blame] | 9803 | * If our rename has the whiteout flag, we need more 5 units for the |
| 9804 | * new inode (1 inode item, 1 inode ref, 2 dir items and 1 xattr item |
| 9805 | * when selinux is enabled). |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 9806 | */ |
Filipe Manana | 5062af3 | 2016-05-05 10:26:26 +0100 | [diff] [blame] | 9807 | trans_num_items = 11; |
| 9808 | if (flags & RENAME_WHITEOUT) |
| 9809 | trans_num_items += 5; |
| 9810 | trans = btrfs_start_transaction(root, trans_num_items); |
Johann Lombardi | b44c59a | 2011-03-31 13:23:47 +0000 | [diff] [blame] | 9811 | if (IS_ERR(trans)) { |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9812 | ret = PTR_ERR(trans); |
| 9813 | goto out_notrans; |
| 9814 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 9815 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9816 | if (dest != root) |
| 9817 | btrfs_record_root_in_trans(trans, dest); |
| 9818 | |
Nikolay Borisov | 877574e | 2017-02-20 13:50:33 +0200 | [diff] [blame] | 9819 | ret = btrfs_set_inode_index(BTRFS_I(new_dir), &index); |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 9820 | if (ret) |
| 9821 | goto out_fail; |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 9822 | |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 9823 | BTRFS_I(old_inode)->dir_index = 0ULL; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 9824 | if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9825 | /* force full log commit if subvolume involved. */ |
David Sterba | 9078776 | 2019-03-20 13:28:05 +0100 | [diff] [blame] | 9826 | btrfs_set_log_full_commit(trans); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9827 | } else { |
Filipe Manana | c4aba95 | 2016-04-29 13:14:42 +0100 | [diff] [blame] | 9828 | btrfs_pin_log_trans(root); |
| 9829 | log_pinned = true; |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 9830 | ret = btrfs_insert_inode_ref(trans, dest, |
| 9831 | new_dentry->d_name.name, |
| 9832 | new_dentry->d_name.len, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 9833 | old_ino, |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 9834 | btrfs_ino(BTRFS_I(new_dir)), index); |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 9835 | if (ret) |
| 9836 | goto out_fail; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9837 | } |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 9838 | |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 9839 | inode_inc_iversion(old_dir); |
| 9840 | inode_inc_iversion(new_dir); |
| 9841 | inode_inc_iversion(old_inode); |
Deepa Dinamani | 04b285f | 2016-02-06 23:57:21 -0800 | [diff] [blame] | 9842 | old_dir->i_ctime = old_dir->i_mtime = |
| 9843 | new_dir->i_ctime = new_dir->i_mtime = |
Deepa Dinamani | c2050a4 | 2016-09-14 07:48:06 -0700 | [diff] [blame] | 9844 | old_inode->i_ctime = current_time(old_dir); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 9845 | |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 9846 | if (old_dentry->d_parent != new_dentry->d_parent) |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 9847 | btrfs_record_unlink_dir(trans, BTRFS_I(old_dir), |
| 9848 | BTRFS_I(old_inode), 1); |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 9849 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 9850 | if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9851 | root_objectid = BTRFS_I(old_inode)->root->root_key.objectid; |
Lu Fengqi | 401b3b1 | 2018-08-01 11:32:30 +0800 | [diff] [blame] | 9852 | ret = btrfs_unlink_subvol(trans, old_dir, root_objectid, |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9853 | old_dentry->d_name.name, |
| 9854 | old_dentry->d_name.len); |
| 9855 | } else { |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 9856 | ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir), |
| 9857 | BTRFS_I(d_inode(old_dentry)), |
Al Viro | 9298679 | 2011-03-04 17:14:37 +0000 | [diff] [blame] | 9858 | old_dentry->d_name.name, |
| 9859 | old_dentry->d_name.len); |
| 9860 | if (!ret) |
| 9861 | ret = btrfs_update_inode(trans, root, old_inode); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9862 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 9863 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 9864 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 9865 | goto out_fail; |
| 9866 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9867 | |
| 9868 | if (new_inode) { |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 9869 | inode_inc_iversion(new_inode); |
Deepa Dinamani | c2050a4 | 2016-09-14 07:48:06 -0700 | [diff] [blame] | 9870 | new_inode->i_ctime = current_time(new_inode); |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 9871 | if (unlikely(btrfs_ino(BTRFS_I(new_inode)) == |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9872 | BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) { |
| 9873 | root_objectid = BTRFS_I(new_inode)->location.objectid; |
Lu Fengqi | 401b3b1 | 2018-08-01 11:32:30 +0800 | [diff] [blame] | 9874 | ret = btrfs_unlink_subvol(trans, new_dir, root_objectid, |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9875 | new_dentry->d_name.name, |
| 9876 | new_dentry->d_name.len); |
| 9877 | BUG_ON(new_inode->i_nlink == 0); |
| 9878 | } else { |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 9879 | ret = btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir), |
| 9880 | BTRFS_I(d_inode(new_dentry)), |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9881 | new_dentry->d_name.name, |
| 9882 | new_dentry->d_name.len); |
| 9883 | } |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 9884 | if (!ret && new_inode->i_nlink == 0) |
Nikolay Borisov | 73f2e54 | 2017-02-20 13:50:59 +0200 | [diff] [blame] | 9885 | ret = btrfs_orphan_add(trans, |
| 9886 | BTRFS_I(d_inode(new_dentry))); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 9887 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 9888 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 9889 | goto out_fail; |
| 9890 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9891 | } |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 9892 | |
Nikolay Borisov | db0a669 | 2017-02-20 13:51:08 +0200 | [diff] [blame] | 9893 | 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] | 9894 | new_dentry->d_name.name, |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 9895 | new_dentry->d_name.len, 0, index); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 9896 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 9897 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 9898 | goto out_fail; |
| 9899 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9900 | |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 9901 | if (old_inode->i_nlink == 1) |
| 9902 | BTRFS_I(old_inode)->dir_index = index; |
| 9903 | |
Filipe Manana | 3dc9e8f7 | 2016-04-29 11:34:22 +0100 | [diff] [blame] | 9904 | if (log_pinned) { |
Al Viro | 10d9f30 | 2011-07-16 23:09:10 -0400 | [diff] [blame] | 9905 | struct dentry *parent = new_dentry->d_parent; |
Filipe Manana | 3dc9e8f7 | 2016-04-29 11:34:22 +0100 | [diff] [blame] | 9906 | |
Filipe Manana | d4682ba | 2018-06-11 19:24:28 +0100 | [diff] [blame] | 9907 | btrfs_init_log_ctx(&ctx, old_inode); |
| 9908 | ret = btrfs_log_new_name(trans, BTRFS_I(old_inode), |
| 9909 | BTRFS_I(old_dir), parent, |
| 9910 | false, &ctx); |
| 9911 | if (ret == BTRFS_NEED_LOG_SYNC) |
| 9912 | sync_log = true; |
| 9913 | else if (ret == BTRFS_NEED_TRANS_COMMIT) |
| 9914 | commit_transaction = true; |
| 9915 | ret = 0; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9916 | btrfs_end_log_trans(root); |
Filipe Manana | 3dc9e8f7 | 2016-04-29 11:34:22 +0100 | [diff] [blame] | 9917 | log_pinned = false; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9918 | } |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9919 | |
| 9920 | if (flags & RENAME_WHITEOUT) { |
| 9921 | ret = btrfs_whiteout_for_rename(trans, root, old_dir, |
| 9922 | old_dentry); |
| 9923 | |
| 9924 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 9925 | btrfs_abort_transaction(trans, ret); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9926 | goto out_fail; |
| 9927 | } |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 9928 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9929 | out_fail: |
Filipe Manana | 3dc9e8f7 | 2016-04-29 11:34:22 +0100 | [diff] [blame] | 9930 | /* |
| 9931 | * If we have pinned the log and an error happened, we unpin tasks |
| 9932 | * trying to sync the log and force them to fallback to a transaction |
| 9933 | * commit if the log currently contains any of the inodes involved in |
| 9934 | * this rename operation (to ensure we do not persist a log with an |
| 9935 | * inconsistent state for any of these inodes or leading to any |
| 9936 | * inconsistencies when replayed). If the transaction was aborted, the |
| 9937 | * abortion reason is propagated to userspace when attempting to commit |
| 9938 | * the transaction. If the log does not contain any of these inodes, we |
| 9939 | * allow the tasks to sync it. |
| 9940 | */ |
| 9941 | if (ret && log_pinned) { |
Nikolay Borisov | 0f8939b | 2017-01-18 00:31:30 +0200 | [diff] [blame] | 9942 | if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) || |
| 9943 | btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) || |
| 9944 | btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) || |
Filipe Manana | 3dc9e8f7 | 2016-04-29 11:34:22 +0100 | [diff] [blame] | 9945 | (new_inode && |
Nikolay Borisov | 0f8939b | 2017-01-18 00:31:30 +0200 | [diff] [blame] | 9946 | btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation))) |
David Sterba | 9078776 | 2019-03-20 13:28:05 +0100 | [diff] [blame] | 9947 | btrfs_set_log_full_commit(trans); |
Filipe Manana | 3dc9e8f7 | 2016-04-29 11:34:22 +0100 | [diff] [blame] | 9948 | |
| 9949 | btrfs_end_log_trans(root); |
| 9950 | log_pinned = false; |
| 9951 | } |
Filipe Manana | d4682ba | 2018-06-11 19:24:28 +0100 | [diff] [blame] | 9952 | if (!ret && sync_log) { |
| 9953 | ret = btrfs_sync_log(trans, BTRFS_I(old_inode)->root, &ctx); |
| 9954 | if (ret) |
| 9955 | commit_transaction = true; |
| 9956 | } |
| 9957 | if (commit_transaction) { |
| 9958 | ret = btrfs_commit_transaction(trans); |
| 9959 | } else { |
| 9960 | int ret2; |
| 9961 | |
| 9962 | ret2 = btrfs_end_transaction(trans); |
| 9963 | ret = ret ? ret : ret2; |
| 9964 | } |
Johann Lombardi | b44c59a | 2011-03-31 13:23:47 +0000 | [diff] [blame] | 9965 | out_notrans: |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 9966 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9967 | up_read(&fs_info->subvol_sem); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 9968 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9969 | return ret; |
| 9970 | } |
| 9971 | |
Miklos Szeredi | 80ace85 | 2014-07-23 15:15:32 +0200 | [diff] [blame] | 9972 | static int btrfs_rename2(struct inode *old_dir, struct dentry *old_dentry, |
| 9973 | struct inode *new_dir, struct dentry *new_dentry, |
| 9974 | unsigned int flags) |
| 9975 | { |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9976 | if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT)) |
Miklos Szeredi | 80ace85 | 2014-07-23 15:15:32 +0200 | [diff] [blame] | 9977 | return -EINVAL; |
| 9978 | |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9979 | if (flags & RENAME_EXCHANGE) |
| 9980 | return btrfs_rename_exchange(old_dir, old_dentry, new_dir, |
| 9981 | new_dentry); |
| 9982 | |
| 9983 | return btrfs_rename(old_dir, old_dentry, new_dir, new_dentry, flags); |
Miklos Szeredi | 80ace85 | 2014-07-23 15:15:32 +0200 | [diff] [blame] | 9984 | } |
| 9985 | |
Nikolay Borisov | 3a2f8c0 | 2018-04-24 17:23:59 +0300 | [diff] [blame] | 9986 | struct btrfs_delalloc_work { |
| 9987 | struct inode *inode; |
| 9988 | struct completion completion; |
| 9989 | struct list_head list; |
| 9990 | struct btrfs_work work; |
| 9991 | }; |
| 9992 | |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 9993 | static void btrfs_run_delalloc_work(struct btrfs_work *work) |
| 9994 | { |
| 9995 | struct btrfs_delalloc_work *delalloc_work; |
Josef Bacik | 9f23e28 | 2013-10-28 15:03:41 -0400 | [diff] [blame] | 9996 | struct inode *inode; |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 9997 | |
| 9998 | delalloc_work = container_of(work, struct btrfs_delalloc_work, |
| 9999 | work); |
Josef Bacik | 9f23e28 | 2013-10-28 15:03:41 -0400 | [diff] [blame] | 10000 | inode = delalloc_work->inode; |
David Sterba | 3042460 | 2015-11-27 19:27:11 +0100 | [diff] [blame] | 10001 | filemap_flush(inode->i_mapping); |
| 10002 | if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, |
| 10003 | &BTRFS_I(inode)->runtime_flags)) |
Josef Bacik | 9f23e28 | 2013-10-28 15:03:41 -0400 | [diff] [blame] | 10004 | filemap_flush(inode->i_mapping); |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 10005 | |
Nikolay Borisov | 076da91 | 2018-04-23 10:54:16 +0300 | [diff] [blame] | 10006 | iput(inode); |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 10007 | complete(&delalloc_work->completion); |
| 10008 | } |
| 10009 | |
Nikolay Borisov | 3a2f8c0 | 2018-04-24 17:23:59 +0300 | [diff] [blame] | 10010 | static struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode) |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 10011 | { |
| 10012 | struct btrfs_delalloc_work *work; |
| 10013 | |
David Sterba | 100d570 | 2015-12-08 14:39:32 +0100 | [diff] [blame] | 10014 | work = kmalloc(sizeof(*work), GFP_NOFS); |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 10015 | if (!work) |
| 10016 | return NULL; |
| 10017 | |
| 10018 | init_completion(&work->completion); |
| 10019 | INIT_LIST_HEAD(&work->list); |
| 10020 | work->inode = inode; |
Liu Bo | 9e0af23 | 2014-08-15 23:36:53 +0800 | [diff] [blame] | 10021 | btrfs_init_work(&work->work, btrfs_flush_delalloc_helper, |
| 10022 | btrfs_run_delalloc_work, NULL, NULL); |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 10023 | |
| 10024 | return work; |
| 10025 | } |
| 10026 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 10027 | /* |
| 10028 | * some fairly slow code that needs optimization. This walks the list |
| 10029 | * of all the inodes with pending delalloc and forces them to disk. |
| 10030 | */ |
Ethan Lien | 3cd24c6 | 2018-11-01 14:49:03 +0800 | [diff] [blame] | 10031 | static int start_delalloc_inodes(struct btrfs_root *root, int nr, bool snapshot) |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 10032 | { |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 10033 | struct btrfs_inode *binode; |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 10034 | struct inode *inode; |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 10035 | struct btrfs_delalloc_work *work, *next; |
| 10036 | struct list_head works; |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 10037 | struct list_head splice; |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 10038 | int ret = 0; |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 10039 | |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 10040 | INIT_LIST_HEAD(&works); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 10041 | INIT_LIST_HEAD(&splice); |
Miao Xie | 63607cc | 2013-01-22 10:50:35 +0000 | [diff] [blame] | 10042 | |
Miao Xie | 573bfb7 | 2014-03-06 13:55:03 +0800 | [diff] [blame] | 10043 | mutex_lock(&root->delalloc_mutex); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 10044 | spin_lock(&root->delalloc_lock); |
| 10045 | list_splice_init(&root->delalloc_inodes, &splice); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 10046 | while (!list_empty(&splice)) { |
| 10047 | binode = list_entry(splice.next, struct btrfs_inode, |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 10048 | delalloc_inodes); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 10049 | |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 10050 | list_move_tail(&binode->delalloc_inodes, |
| 10051 | &root->delalloc_inodes); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 10052 | inode = igrab(&binode->vfs_inode); |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 10053 | if (!inode) { |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 10054 | cond_resched_lock(&root->delalloc_lock); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 10055 | continue; |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 10056 | } |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 10057 | spin_unlock(&root->delalloc_lock); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 10058 | |
Ethan Lien | 3cd24c6 | 2018-11-01 14:49:03 +0800 | [diff] [blame] | 10059 | if (snapshot) |
| 10060 | set_bit(BTRFS_INODE_SNAPSHOT_FLUSH, |
| 10061 | &binode->runtime_flags); |
Nikolay Borisov | 076da91 | 2018-04-23 10:54:16 +0300 | [diff] [blame] | 10062 | work = btrfs_alloc_delalloc_work(inode); |
David Sterba | 5d99a998 | 2014-09-29 19:20:37 +0200 | [diff] [blame] | 10063 | if (!work) { |
Nikolay Borisov | 4fbb514 | 2018-04-23 10:54:15 +0300 | [diff] [blame] | 10064 | iput(inode); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 10065 | ret = -ENOMEM; |
Wang Shilong | a1ecaab | 2014-04-02 19:53:32 +0800 | [diff] [blame] | 10066 | goto out; |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 10067 | } |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 10068 | list_add_tail(&work->list, &works); |
Qu Wenruo | a44903a | 2014-02-28 10:46:09 +0800 | [diff] [blame] | 10069 | btrfs_queue_work(root->fs_info->flush_workers, |
| 10070 | &work->work); |
Miao Xie | 6c255e6 | 2014-03-06 13:55:01 +0800 | [diff] [blame] | 10071 | ret++; |
| 10072 | if (nr != -1 && ret >= nr) |
Wang Shilong | a1ecaab | 2014-04-02 19:53:32 +0800 | [diff] [blame] | 10073 | goto out; |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 10074 | cond_resched(); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 10075 | spin_lock(&root->delalloc_lock); |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 10076 | } |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 10077 | spin_unlock(&root->delalloc_lock); |
Chris Mason | 8c8bee1 | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 10078 | |
Wang Shilong | a1ecaab | 2014-04-02 19:53:32 +0800 | [diff] [blame] | 10079 | out: |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 10080 | list_for_each_entry_safe(work, next, &works, list) { |
| 10081 | list_del_init(&work->list); |
Nikolay Borisov | 40012f9 | 2018-04-19 10:46:39 +0300 | [diff] [blame] | 10082 | wait_for_completion(&work->completion); |
| 10083 | kfree(work); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 10084 | } |
| 10085 | |
Nikolay Borisov | 81f1d39 | 2018-04-19 10:46:37 +0300 | [diff] [blame] | 10086 | if (!list_empty(&splice)) { |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 10087 | spin_lock(&root->delalloc_lock); |
| 10088 | list_splice_tail(&splice, &root->delalloc_inodes); |
| 10089 | spin_unlock(&root->delalloc_lock); |
| 10090 | } |
Miao Xie | 573bfb7 | 2014-03-06 13:55:03 +0800 | [diff] [blame] | 10091 | mutex_unlock(&root->delalloc_mutex); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 10092 | return ret; |
| 10093 | } |
| 10094 | |
Ethan Lien | 3cd24c6 | 2018-11-01 14:49:03 +0800 | [diff] [blame] | 10095 | int btrfs_start_delalloc_snapshot(struct btrfs_root *root) |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 10096 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 10097 | struct btrfs_fs_info *fs_info = root->fs_info; |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 10098 | int ret; |
| 10099 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 10100 | if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 10101 | return -EROFS; |
| 10102 | |
Ethan Lien | 3cd24c6 | 2018-11-01 14:49:03 +0800 | [diff] [blame] | 10103 | ret = start_delalloc_inodes(root, -1, true); |
Miao Xie | 6c255e6 | 2014-03-06 13:55:01 +0800 | [diff] [blame] | 10104 | if (ret > 0) |
| 10105 | ret = 0; |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 10106 | return ret; |
| 10107 | } |
| 10108 | |
Nikolay Borisov | 82b3e53 | 2018-04-23 10:54:13 +0300 | [diff] [blame] | 10109 | int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, int nr) |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 10110 | { |
| 10111 | struct btrfs_root *root; |
| 10112 | struct list_head splice; |
| 10113 | int ret; |
| 10114 | |
Wang Shilong | 2c21b4d | 2014-01-14 19:42:20 +0800 | [diff] [blame] | 10115 | if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 10116 | return -EROFS; |
| 10117 | |
| 10118 | INIT_LIST_HEAD(&splice); |
| 10119 | |
Miao Xie | 573bfb7 | 2014-03-06 13:55:03 +0800 | [diff] [blame] | 10120 | mutex_lock(&fs_info->delalloc_root_mutex); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 10121 | spin_lock(&fs_info->delalloc_root_lock); |
| 10122 | list_splice_init(&fs_info->delalloc_roots, &splice); |
Miao Xie | 6c255e6 | 2014-03-06 13:55:01 +0800 | [diff] [blame] | 10123 | while (!list_empty(&splice) && nr) { |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 10124 | root = list_first_entry(&splice, struct btrfs_root, |
| 10125 | delalloc_root); |
| 10126 | root = btrfs_grab_fs_root(root); |
| 10127 | BUG_ON(!root); |
| 10128 | list_move_tail(&root->delalloc_root, |
| 10129 | &fs_info->delalloc_roots); |
| 10130 | spin_unlock(&fs_info->delalloc_root_lock); |
| 10131 | |
Ethan Lien | 3cd24c6 | 2018-11-01 14:49:03 +0800 | [diff] [blame] | 10132 | ret = start_delalloc_inodes(root, nr, false); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 10133 | btrfs_put_fs_root(root); |
Miao Xie | 6c255e6 | 2014-03-06 13:55:01 +0800 | [diff] [blame] | 10134 | if (ret < 0) |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 10135 | goto out; |
| 10136 | |
Miao Xie | 6c255e6 | 2014-03-06 13:55:01 +0800 | [diff] [blame] | 10137 | if (nr != -1) { |
| 10138 | nr -= ret; |
| 10139 | WARN_ON(nr < 0); |
| 10140 | } |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 10141 | spin_lock(&fs_info->delalloc_root_lock); |
| 10142 | } |
| 10143 | spin_unlock(&fs_info->delalloc_root_lock); |
| 10144 | |
Miao Xie | 6c255e6 | 2014-03-06 13:55:01 +0800 | [diff] [blame] | 10145 | ret = 0; |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 10146 | out: |
Nikolay Borisov | 81f1d39 | 2018-04-19 10:46:37 +0300 | [diff] [blame] | 10147 | if (!list_empty(&splice)) { |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 10148 | spin_lock(&fs_info->delalloc_root_lock); |
| 10149 | list_splice_tail(&splice, &fs_info->delalloc_roots); |
| 10150 | spin_unlock(&fs_info->delalloc_root_lock); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 10151 | } |
Miao Xie | 573bfb7 | 2014-03-06 13:55:03 +0800 | [diff] [blame] | 10152 | mutex_unlock(&fs_info->delalloc_root_mutex); |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 10153 | return ret; |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 10154 | } |
| 10155 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10156 | static int btrfs_symlink(struct inode *dir, struct dentry *dentry, |
| 10157 | const char *symname) |
| 10158 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 10159 | struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10160 | struct btrfs_trans_handle *trans; |
| 10161 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 10162 | struct btrfs_path *path; |
| 10163 | struct btrfs_key key; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 10164 | struct inode *inode = NULL; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10165 | int err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10166 | u64 objectid; |
Dulshani Gunawardhana | 6787125 | 2013-10-31 10:33:04 +0530 | [diff] [blame] | 10167 | u64 index = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10168 | int name_len; |
| 10169 | int datasize; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 10170 | unsigned long ptr; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10171 | struct btrfs_file_extent_item *ei; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 10172 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10173 | |
Filipe David Borba Manana | f06becc | 2013-09-16 09:53:28 +0100 | [diff] [blame] | 10174 | name_len = strlen(symname); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 10175 | if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info)) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10176 | return -ENAMETOOLONG; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 10177 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 10178 | /* |
| 10179 | * 2 items for inode item and ref |
| 10180 | * 2 items for dir items |
Filipe Manana | 9269d12 | 2015-12-31 18:16:29 +0000 | [diff] [blame] | 10181 | * 1 item for updating parent inode item |
| 10182 | * 1 item for the inline extent item |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 10183 | * 1 item for xattr if selinux is on |
| 10184 | */ |
Filipe Manana | 9269d12 | 2015-12-31 18:16:29 +0000 | [diff] [blame] | 10185 | trans = btrfs_start_transaction(root, 7); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 10186 | if (IS_ERR(trans)) |
| 10187 | return PTR_ERR(trans); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 10188 | |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 10189 | err = btrfs_find_free_ino(root, &objectid); |
| 10190 | if (err) |
| 10191 | goto out_unlock; |
| 10192 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 10193 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 10194 | dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), |
| 10195 | objectid, S_IFLNK|S_IRWXUGO, &index); |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 10196 | if (IS_ERR(inode)) { |
| 10197 | err = PTR_ERR(inode); |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 10198 | inode = NULL; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10199 | goto out_unlock; |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 10200 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10201 | |
Casey Schaufler | ad19db7 | 2011-12-15 10:09:07 -0500 | [diff] [blame] | 10202 | /* |
| 10203 | * If the active LSM wants to access the inode during |
| 10204 | * d_instantiate it needs these. Smack checks to see |
| 10205 | * if the filesystem supports xattrs by looking at the |
| 10206 | * ops vector. |
| 10207 | */ |
| 10208 | inode->i_fop = &btrfs_file_operations; |
| 10209 | inode->i_op = &btrfs_file_inode_operations; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 10210 | inode->i_mapping->a_ops = &btrfs_aops; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 10211 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
| 10212 | |
| 10213 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); |
| 10214 | if (err) |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 10215 | goto out_unlock; |
Casey Schaufler | ad19db7 | 2011-12-15 10:09:07 -0500 | [diff] [blame] | 10216 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10217 | path = btrfs_alloc_path(); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 10218 | if (!path) { |
| 10219 | err = -ENOMEM; |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 10220 | goto out_unlock; |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 10221 | } |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 10222 | key.objectid = btrfs_ino(BTRFS_I(inode)); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10223 | key.offset = 0; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 10224 | key.type = BTRFS_EXTENT_DATA_KEY; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10225 | datasize = btrfs_file_extent_calc_inline_size(name_len); |
| 10226 | err = btrfs_insert_empty_item(trans, root, path, &key, |
| 10227 | datasize); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 10228 | if (err) { |
Julia Lawall | b083916 | 2011-05-14 07:10:51 +0000 | [diff] [blame] | 10229 | btrfs_free_path(path); |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 10230 | goto out_unlock; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 10231 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 10232 | leaf = path->nodes[0]; |
| 10233 | ei = btrfs_item_ptr(leaf, path->slots[0], |
| 10234 | struct btrfs_file_extent_item); |
| 10235 | btrfs_set_file_extent_generation(leaf, ei, trans->transid); |
| 10236 | btrfs_set_file_extent_type(leaf, ei, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10237 | BTRFS_FILE_EXTENT_INLINE); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 10238 | btrfs_set_file_extent_encryption(leaf, ei, 0); |
| 10239 | btrfs_set_file_extent_compression(leaf, ei, 0); |
| 10240 | btrfs_set_file_extent_other_encoding(leaf, ei, 0); |
| 10241 | btrfs_set_file_extent_ram_bytes(leaf, ei, name_len); |
| 10242 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10243 | ptr = btrfs_file_extent_inline_start(ei); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 10244 | write_extent_buffer(leaf, symname, ptr, name_len); |
| 10245 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10246 | btrfs_free_path(path); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 10247 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10248 | inode->i_op = &btrfs_symlink_inode_operations; |
Al Viro | 21fc61c | 2015-11-17 01:07:57 -0500 | [diff] [blame] | 10249 | inode_nohighmem(inode); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 10250 | inode_set_bytes(inode, name_len); |
Nikolay Borisov | 6ef06d2 | 2017-02-20 13:50:34 +0200 | [diff] [blame] | 10251 | btrfs_i_size_write(BTRFS_I(inode), name_len); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 10252 | err = btrfs_update_inode(trans, root, inode); |
Filipe Manana | d50866d | 2015-12-31 18:08:24 +0000 | [diff] [blame] | 10253 | /* |
| 10254 | * Last step, add directory indexes for our symlink inode. This is the |
| 10255 | * last step to avoid extra cleanup of these indexes if an error happens |
| 10256 | * elsewhere above. |
| 10257 | */ |
| 10258 | if (!err) |
Nikolay Borisov | cef415a | 2017-02-20 13:51:09 +0200 | [diff] [blame] | 10259 | err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, |
| 10260 | BTRFS_I(inode), 0, index); |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 10261 | if (err) |
| 10262 | goto out_unlock; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 10263 | |
Al Viro | 1e2e547 | 2018-05-04 08:23:01 -0400 | [diff] [blame] | 10264 | d_instantiate_new(dentry, inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10265 | |
| 10266 | out_unlock: |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 10267 | btrfs_end_transaction(trans); |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 10268 | if (err && inode) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10269 | inode_dec_link_count(inode); |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 10270 | discard_new_inode(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10271 | } |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 10272 | btrfs_btree_balance_dirty(fs_info); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10273 | return err; |
| 10274 | } |
Chris Mason | 1643298 | 2008-04-10 10:23:21 -0400 | [diff] [blame] | 10275 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 10276 | static int __btrfs_prealloc_file_range(struct inode *inode, int mode, |
| 10277 | u64 start, u64 num_bytes, u64 min_size, |
| 10278 | loff_t actual_len, u64 *alloc_hint, |
| 10279 | struct btrfs_trans_handle *trans) |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 10280 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 10281 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 10282 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
| 10283 | struct extent_map *em; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 10284 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 10285 | struct btrfs_key ins; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 10286 | u64 cur_offset = start; |
Josef Bacik | 55a61d1 | 2010-11-22 18:50:32 +0000 | [diff] [blame] | 10287 | u64 i_size; |
Chris Mason | 154ea28 | 2013-03-05 11:11:26 -0500 | [diff] [blame] | 10288 | u64 cur_bytes; |
Josef Bacik | 0b670dc | 2015-09-23 17:11:16 -0400 | [diff] [blame] | 10289 | u64 last_alloc = (u64)-1; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 10290 | int ret = 0; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 10291 | bool own_trans = true; |
Wang Xiaoguang | 1851309 | 2016-07-25 15:51:40 +0800 | [diff] [blame] | 10292 | u64 end = start + num_bytes - 1; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 10293 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 10294 | if (trans) |
| 10295 | own_trans = false; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 10296 | while (num_bytes > 0) { |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 10297 | if (own_trans) { |
| 10298 | trans = btrfs_start_transaction(root, 3); |
| 10299 | if (IS_ERR(trans)) { |
| 10300 | ret = PTR_ERR(trans); |
| 10301 | break; |
| 10302 | } |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 10303 | } |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 10304 | |
Byongho Lee | ee22184 | 2015-12-15 01:42:10 +0900 | [diff] [blame] | 10305 | cur_bytes = min_t(u64, num_bytes, SZ_256M); |
Chris Mason | 154ea28 | 2013-03-05 11:11:26 -0500 | [diff] [blame] | 10306 | cur_bytes = max(cur_bytes, min_size); |
Josef Bacik | 0b670dc | 2015-09-23 17:11:16 -0400 | [diff] [blame] | 10307 | /* |
| 10308 | * If we are severely fragmented we could end up with really |
| 10309 | * small allocations, so if the allocator is returning small |
| 10310 | * chunks lets make its job easier by only searching for those |
| 10311 | * sized chunks. |
| 10312 | */ |
| 10313 | cur_bytes = min(cur_bytes, last_alloc); |
Wang Xiaoguang | 1851309 | 2016-07-25 15:51:40 +0800 | [diff] [blame] | 10314 | ret = btrfs_reserve_extent(root, cur_bytes, cur_bytes, |
| 10315 | min_size, 0, *alloc_hint, &ins, 1, 0); |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 10316 | if (ret) { |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 10317 | if (own_trans) |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 10318 | btrfs_end_transaction(trans); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 10319 | break; |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 10320 | } |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 10321 | btrfs_dec_block_group_reservations(fs_info, ins.objectid); |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 10322 | |
Josef Bacik | 0b670dc | 2015-09-23 17:11:16 -0400 | [diff] [blame] | 10323 | last_alloc = ins.offset; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 10324 | ret = insert_reserved_file_extent(trans, inode, |
| 10325 | cur_offset, ins.objectid, |
| 10326 | ins.offset, ins.offset, |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 10327 | ins.offset, 0, 0, 0, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 10328 | BTRFS_FILE_EXTENT_PREALLOC); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 10329 | if (ret) { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 10330 | btrfs_free_reserved_extent(fs_info, ins.objectid, |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 10331 | ins.offset, 0); |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 10332 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 10333 | if (own_trans) |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 10334 | btrfs_end_transaction(trans); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 10335 | break; |
| 10336 | } |
Dongsheng Yang | 3119321 | 2014-12-12 16:44:35 +0800 | [diff] [blame] | 10337 | |
Nikolay Borisov | dcdbc05 | 2017-02-20 13:50:45 +0200 | [diff] [blame] | 10338 | btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset, |
Chris Mason | a1ed835 | 2009-09-11 12:27:37 -0400 | [diff] [blame] | 10339 | cur_offset + ins.offset -1, 0); |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 10340 | |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 10341 | em = alloc_extent_map(); |
| 10342 | if (!em) { |
| 10343 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, |
| 10344 | &BTRFS_I(inode)->runtime_flags); |
| 10345 | goto next; |
| 10346 | } |
| 10347 | |
| 10348 | em->start = cur_offset; |
| 10349 | em->orig_start = cur_offset; |
| 10350 | em->len = ins.offset; |
| 10351 | em->block_start = ins.objectid; |
| 10352 | em->block_len = ins.offset; |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 10353 | em->orig_block_len = ins.offset; |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 10354 | em->ram_bytes = ins.offset; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 10355 | em->bdev = fs_info->fs_devices->latest_bdev; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 10356 | set_bit(EXTENT_FLAG_PREALLOC, &em->flags); |
| 10357 | em->generation = trans->transid; |
| 10358 | |
| 10359 | while (1) { |
| 10360 | write_lock(&em_tree->lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 10361 | ret = add_extent_mapping(em_tree, em, 1); |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 10362 | write_unlock(&em_tree->lock); |
| 10363 | if (ret != -EEXIST) |
| 10364 | break; |
Nikolay Borisov | dcdbc05 | 2017-02-20 13:50:45 +0200 | [diff] [blame] | 10365 | btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset, |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 10366 | cur_offset + ins.offset - 1, |
| 10367 | 0); |
| 10368 | } |
| 10369 | free_extent_map(em); |
| 10370 | next: |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 10371 | num_bytes -= ins.offset; |
| 10372 | cur_offset += ins.offset; |
Yan, Zheng | efa5646 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 10373 | *alloc_hint = ins.objectid + ins.offset; |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 10374 | |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 10375 | inode_inc_iversion(inode); |
Deepa Dinamani | c2050a4 | 2016-09-14 07:48:06 -0700 | [diff] [blame] | 10376 | inode->i_ctime = current_time(inode); |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 10377 | BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 10378 | if (!(mode & FALLOC_FL_KEEP_SIZE) && |
Yan, Zheng | efa5646 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 10379 | (actual_len > inode->i_size) && |
| 10380 | (cur_offset > inode->i_size)) { |
Aneesh Kumar K.V | d1ea6a6 | 2010-01-20 07:28:54 +0000 | [diff] [blame] | 10381 | if (cur_offset > actual_len) |
Josef Bacik | 55a61d1 | 2010-11-22 18:50:32 +0000 | [diff] [blame] | 10382 | i_size = actual_len; |
Aneesh Kumar K.V | d1ea6a6 | 2010-01-20 07:28:54 +0000 | [diff] [blame] | 10383 | else |
Josef Bacik | 55a61d1 | 2010-11-22 18:50:32 +0000 | [diff] [blame] | 10384 | i_size = cur_offset; |
| 10385 | i_size_write(inode, i_size); |
| 10386 | btrfs_ordered_update_i_size(inode, i_size, NULL); |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 10387 | } |
| 10388 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 10389 | ret = btrfs_update_inode(trans, root, inode); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 10390 | |
| 10391 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 10392 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 10393 | if (own_trans) |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 10394 | btrfs_end_transaction(trans); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 10395 | break; |
| 10396 | } |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 10397 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 10398 | if (own_trans) |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 10399 | btrfs_end_transaction(trans); |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 10400 | } |
Wang Xiaoguang | 1851309 | 2016-07-25 15:51:40 +0800 | [diff] [blame] | 10401 | if (cur_offset < end) |
Qu Wenruo | bc42bda | 2017-02-27 15:10:39 +0800 | [diff] [blame] | 10402 | btrfs_free_reserved_data_space(inode, NULL, cur_offset, |
Wang Xiaoguang | 1851309 | 2016-07-25 15:51:40 +0800 | [diff] [blame] | 10403 | end - cur_offset + 1); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 10404 | return ret; |
| 10405 | } |
| 10406 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 10407 | int btrfs_prealloc_file_range(struct inode *inode, int mode, |
| 10408 | u64 start, u64 num_bytes, u64 min_size, |
| 10409 | loff_t actual_len, u64 *alloc_hint) |
| 10410 | { |
| 10411 | return __btrfs_prealloc_file_range(inode, mode, start, num_bytes, |
| 10412 | min_size, actual_len, alloc_hint, |
| 10413 | NULL); |
| 10414 | } |
| 10415 | |
| 10416 | int btrfs_prealloc_file_range_trans(struct inode *inode, |
| 10417 | struct btrfs_trans_handle *trans, int mode, |
| 10418 | u64 start, u64 num_bytes, u64 min_size, |
| 10419 | loff_t actual_len, u64 *alloc_hint) |
| 10420 | { |
| 10421 | return __btrfs_prealloc_file_range(inode, mode, start, num_bytes, |
| 10422 | min_size, actual_len, alloc_hint, trans); |
| 10423 | } |
| 10424 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 10425 | static int btrfs_set_page_dirty(struct page *page) |
| 10426 | { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 10427 | return __set_page_dirty_nobuffers(page); |
| 10428 | } |
| 10429 | |
Al Viro | 10556cb2 | 2011-06-20 19:28:19 -0400 | [diff] [blame] | 10430 | static int btrfs_permission(struct inode *inode, int mask) |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 10431 | { |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 10432 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Jeff Mahoney | cb6db4e | 2011-08-15 17:27:21 +0000 | [diff] [blame] | 10433 | umode_t mode = inode->i_mode; |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 10434 | |
Jeff Mahoney | cb6db4e | 2011-08-15 17:27:21 +0000 | [diff] [blame] | 10435 | if (mask & MAY_WRITE && |
| 10436 | (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) { |
| 10437 | if (btrfs_root_readonly(root)) |
| 10438 | return -EROFS; |
| 10439 | if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY) |
| 10440 | return -EACCES; |
| 10441 | } |
Al Viro | 2830ba7 | 2011-06-20 19:16:29 -0400 | [diff] [blame] | 10442 | return generic_permission(inode, mask); |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 10443 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10444 | |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 10445 | static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode) |
| 10446 | { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 10447 | struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 10448 | struct btrfs_trans_handle *trans; |
| 10449 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 10450 | struct inode *inode = NULL; |
| 10451 | u64 objectid; |
| 10452 | u64 index; |
| 10453 | int ret = 0; |
| 10454 | |
| 10455 | /* |
| 10456 | * 5 units required for adding orphan entry |
| 10457 | */ |
| 10458 | trans = btrfs_start_transaction(root, 5); |
| 10459 | if (IS_ERR(trans)) |
| 10460 | return PTR_ERR(trans); |
| 10461 | |
| 10462 | ret = btrfs_find_free_ino(root, &objectid); |
| 10463 | if (ret) |
| 10464 | goto out; |
| 10465 | |
| 10466 | inode = btrfs_new_inode(trans, root, dir, NULL, 0, |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 10467 | btrfs_ino(BTRFS_I(dir)), objectid, mode, &index); |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 10468 | if (IS_ERR(inode)) { |
| 10469 | ret = PTR_ERR(inode); |
| 10470 | inode = NULL; |
| 10471 | goto out; |
| 10472 | } |
| 10473 | |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 10474 | inode->i_fop = &btrfs_file_operations; |
| 10475 | inode->i_op = &btrfs_file_inode_operations; |
| 10476 | |
| 10477 | inode->i_mapping->a_ops = &btrfs_aops; |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 10478 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
| 10479 | |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 10480 | ret = btrfs_init_inode_security(trans, inode, dir, NULL); |
| 10481 | if (ret) |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 10482 | goto out; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 10483 | |
| 10484 | ret = btrfs_update_inode(trans, root, inode); |
| 10485 | if (ret) |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 10486 | goto out; |
Nikolay Borisov | 73f2e54 | 2017-02-20 13:50:59 +0200 | [diff] [blame] | 10487 | ret = btrfs_orphan_add(trans, BTRFS_I(inode)); |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 10488 | if (ret) |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 10489 | goto out; |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 10490 | |
Filipe Manana | 5762b5c | 2014-08-01 00:10:32 +0100 | [diff] [blame] | 10491 | /* |
| 10492 | * We set number of links to 0 in btrfs_new_inode(), and here we set |
| 10493 | * it to 1 because d_tmpfile() will issue a warning if the count is 0, |
| 10494 | * through: |
| 10495 | * |
| 10496 | * d_tmpfile() -> inode_dec_link_count() -> drop_nlink() |
| 10497 | */ |
| 10498 | set_nlink(inode, 1); |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 10499 | d_tmpfile(dentry, inode); |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 10500 | unlock_new_inode(inode); |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 10501 | mark_inode_dirty(inode); |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 10502 | out: |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 10503 | btrfs_end_transaction(trans); |
Al Viro | 32955c5 | 2018-05-16 12:20:05 -0400 | [diff] [blame] | 10504 | if (ret && inode) |
| 10505 | discard_new_inode(inode); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 10506 | btrfs_btree_balance_dirty(fs_info); |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 10507 | return ret; |
| 10508 | } |
| 10509 | |
David Sterba | 5cdc84b | 2018-07-18 20:32:52 +0200 | [diff] [blame] | 10510 | 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] | 10511 | { |
David Sterba | 5cdc84b | 2018-07-18 20:32:52 +0200 | [diff] [blame] | 10512 | struct inode *inode = tree->private_data; |
Josef Bacik | c6100a4 | 2017-05-05 11:57:13 -0400 | [diff] [blame] | 10513 | unsigned long index = start >> PAGE_SHIFT; |
| 10514 | unsigned long end_index = end >> PAGE_SHIFT; |
| 10515 | struct page *page; |
| 10516 | |
| 10517 | while (index <= end_index) { |
| 10518 | page = find_get_page(inode->i_mapping, index); |
| 10519 | ASSERT(page); /* Pages should be in the extent_io_tree */ |
| 10520 | set_page_writeback(page); |
| 10521 | put_page(page); |
| 10522 | index++; |
| 10523 | } |
| 10524 | } |
| 10525 | |
Omar Sandoval | ed46ff3 | 2016-11-03 10:28:14 -0700 | [diff] [blame] | 10526 | #ifdef CONFIG_SWAP |
| 10527 | /* |
| 10528 | * Add an entry indicating a block group or device which is pinned by a |
| 10529 | * swapfile. Returns 0 on success, 1 if there is already an entry for it, or a |
| 10530 | * negative errno on failure. |
| 10531 | */ |
| 10532 | static int btrfs_add_swapfile_pin(struct inode *inode, void *ptr, |
| 10533 | bool is_block_group) |
| 10534 | { |
| 10535 | struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info; |
| 10536 | struct btrfs_swapfile_pin *sp, *entry; |
| 10537 | struct rb_node **p; |
| 10538 | struct rb_node *parent = NULL; |
| 10539 | |
| 10540 | sp = kmalloc(sizeof(*sp), GFP_NOFS); |
| 10541 | if (!sp) |
| 10542 | return -ENOMEM; |
| 10543 | sp->ptr = ptr; |
| 10544 | sp->inode = inode; |
| 10545 | sp->is_block_group = is_block_group; |
| 10546 | |
| 10547 | spin_lock(&fs_info->swapfile_pins_lock); |
| 10548 | p = &fs_info->swapfile_pins.rb_node; |
| 10549 | while (*p) { |
| 10550 | parent = *p; |
| 10551 | entry = rb_entry(parent, struct btrfs_swapfile_pin, node); |
| 10552 | if (sp->ptr < entry->ptr || |
| 10553 | (sp->ptr == entry->ptr && sp->inode < entry->inode)) { |
| 10554 | p = &(*p)->rb_left; |
| 10555 | } else if (sp->ptr > entry->ptr || |
| 10556 | (sp->ptr == entry->ptr && sp->inode > entry->inode)) { |
| 10557 | p = &(*p)->rb_right; |
| 10558 | } else { |
| 10559 | spin_unlock(&fs_info->swapfile_pins_lock); |
| 10560 | kfree(sp); |
| 10561 | return 1; |
| 10562 | } |
| 10563 | } |
| 10564 | rb_link_node(&sp->node, parent, p); |
| 10565 | rb_insert_color(&sp->node, &fs_info->swapfile_pins); |
| 10566 | spin_unlock(&fs_info->swapfile_pins_lock); |
| 10567 | return 0; |
| 10568 | } |
| 10569 | |
| 10570 | /* Free all of the entries pinned by this swapfile. */ |
| 10571 | static void btrfs_free_swapfile_pins(struct inode *inode) |
| 10572 | { |
| 10573 | struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info; |
| 10574 | struct btrfs_swapfile_pin *sp; |
| 10575 | struct rb_node *node, *next; |
| 10576 | |
| 10577 | spin_lock(&fs_info->swapfile_pins_lock); |
| 10578 | node = rb_first(&fs_info->swapfile_pins); |
| 10579 | while (node) { |
| 10580 | next = rb_next(node); |
| 10581 | sp = rb_entry(node, struct btrfs_swapfile_pin, node); |
| 10582 | if (sp->inode == inode) { |
| 10583 | rb_erase(&sp->node, &fs_info->swapfile_pins); |
| 10584 | if (sp->is_block_group) |
| 10585 | btrfs_put_block_group(sp->ptr); |
| 10586 | kfree(sp); |
| 10587 | } |
| 10588 | node = next; |
| 10589 | } |
| 10590 | spin_unlock(&fs_info->swapfile_pins_lock); |
| 10591 | } |
| 10592 | |
| 10593 | struct btrfs_swap_info { |
| 10594 | u64 start; |
| 10595 | u64 block_start; |
| 10596 | u64 block_len; |
| 10597 | u64 lowest_ppage; |
| 10598 | u64 highest_ppage; |
| 10599 | unsigned long nr_pages; |
| 10600 | int nr_extents; |
| 10601 | }; |
| 10602 | |
| 10603 | static int btrfs_add_swap_extent(struct swap_info_struct *sis, |
| 10604 | struct btrfs_swap_info *bsi) |
| 10605 | { |
| 10606 | unsigned long nr_pages; |
| 10607 | u64 first_ppage, first_ppage_reported, next_ppage; |
| 10608 | int ret; |
| 10609 | |
| 10610 | first_ppage = ALIGN(bsi->block_start, PAGE_SIZE) >> PAGE_SHIFT; |
| 10611 | next_ppage = ALIGN_DOWN(bsi->block_start + bsi->block_len, |
| 10612 | PAGE_SIZE) >> PAGE_SHIFT; |
| 10613 | |
| 10614 | if (first_ppage >= next_ppage) |
| 10615 | return 0; |
| 10616 | nr_pages = next_ppage - first_ppage; |
| 10617 | |
| 10618 | first_ppage_reported = first_ppage; |
| 10619 | if (bsi->start == 0) |
| 10620 | first_ppage_reported++; |
| 10621 | if (bsi->lowest_ppage > first_ppage_reported) |
| 10622 | bsi->lowest_ppage = first_ppage_reported; |
| 10623 | if (bsi->highest_ppage < (next_ppage - 1)) |
| 10624 | bsi->highest_ppage = next_ppage - 1; |
| 10625 | |
| 10626 | ret = add_swap_extent(sis, bsi->nr_pages, nr_pages, first_ppage); |
| 10627 | if (ret < 0) |
| 10628 | return ret; |
| 10629 | bsi->nr_extents += ret; |
| 10630 | bsi->nr_pages += nr_pages; |
| 10631 | return 0; |
| 10632 | } |
| 10633 | |
| 10634 | static void btrfs_swap_deactivate(struct file *file) |
| 10635 | { |
| 10636 | struct inode *inode = file_inode(file); |
| 10637 | |
| 10638 | btrfs_free_swapfile_pins(inode); |
| 10639 | atomic_dec(&BTRFS_I(inode)->root->nr_swapfiles); |
| 10640 | } |
| 10641 | |
| 10642 | static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file, |
| 10643 | sector_t *span) |
| 10644 | { |
| 10645 | struct inode *inode = file_inode(file); |
| 10646 | struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info; |
| 10647 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
| 10648 | struct extent_state *cached_state = NULL; |
| 10649 | struct extent_map *em = NULL; |
| 10650 | struct btrfs_device *device = NULL; |
| 10651 | struct btrfs_swap_info bsi = { |
| 10652 | .lowest_ppage = (sector_t)-1ULL, |
| 10653 | }; |
| 10654 | int ret = 0; |
| 10655 | u64 isize; |
| 10656 | u64 start; |
| 10657 | |
| 10658 | /* |
| 10659 | * If the swap file was just created, make sure delalloc is done. If the |
| 10660 | * file changes again after this, the user is doing something stupid and |
| 10661 | * we don't really care. |
| 10662 | */ |
| 10663 | ret = btrfs_wait_ordered_range(inode, 0, (u64)-1); |
| 10664 | if (ret) |
| 10665 | return ret; |
| 10666 | |
| 10667 | /* |
| 10668 | * The inode is locked, so these flags won't change after we check them. |
| 10669 | */ |
| 10670 | if (BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS) { |
| 10671 | btrfs_warn(fs_info, "swapfile must not be compressed"); |
| 10672 | return -EINVAL; |
| 10673 | } |
| 10674 | if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW)) { |
| 10675 | btrfs_warn(fs_info, "swapfile must not be copy-on-write"); |
| 10676 | return -EINVAL; |
| 10677 | } |
| 10678 | if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) { |
| 10679 | btrfs_warn(fs_info, "swapfile must not be checksummed"); |
| 10680 | return -EINVAL; |
| 10681 | } |
| 10682 | |
| 10683 | /* |
| 10684 | * Balance or device remove/replace/resize can move stuff around from |
| 10685 | * under us. The EXCL_OP flag makes sure they aren't running/won't run |
| 10686 | * concurrently while we are mapping the swap extents, and |
| 10687 | * fs_info->swapfile_pins prevents them from running while the swap file |
| 10688 | * is active and moving the extents. Note that this also prevents a |
| 10689 | * concurrent device add which isn't actually necessary, but it's not |
| 10690 | * really worth the trouble to allow it. |
| 10691 | */ |
| 10692 | if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) { |
| 10693 | btrfs_warn(fs_info, |
| 10694 | "cannot activate swapfile while exclusive operation is running"); |
| 10695 | return -EBUSY; |
| 10696 | } |
| 10697 | /* |
| 10698 | * Snapshots can create extents which require COW even if NODATACOW is |
| 10699 | * set. We use this counter to prevent snapshots. We must increment it |
| 10700 | * before walking the extents because we don't want a concurrent |
| 10701 | * snapshot to run after we've already checked the extents. |
| 10702 | */ |
| 10703 | atomic_inc(&BTRFS_I(inode)->root->nr_swapfiles); |
| 10704 | |
| 10705 | isize = ALIGN_DOWN(inode->i_size, fs_info->sectorsize); |
| 10706 | |
| 10707 | lock_extent_bits(io_tree, 0, isize - 1, &cached_state); |
| 10708 | start = 0; |
| 10709 | while (start < isize) { |
| 10710 | u64 logical_block_start, physical_block_start; |
| 10711 | struct btrfs_block_group_cache *bg; |
| 10712 | u64 len = isize - start; |
| 10713 | |
| 10714 | em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len, 0); |
| 10715 | if (IS_ERR(em)) { |
| 10716 | ret = PTR_ERR(em); |
| 10717 | goto out; |
| 10718 | } |
| 10719 | |
| 10720 | if (em->block_start == EXTENT_MAP_HOLE) { |
| 10721 | btrfs_warn(fs_info, "swapfile must not have holes"); |
| 10722 | ret = -EINVAL; |
| 10723 | goto out; |
| 10724 | } |
| 10725 | if (em->block_start == EXTENT_MAP_INLINE) { |
| 10726 | /* |
| 10727 | * It's unlikely we'll ever actually find ourselves |
| 10728 | * here, as a file small enough to fit inline won't be |
| 10729 | * big enough to store more than the swap header, but in |
| 10730 | * case something changes in the future, let's catch it |
| 10731 | * here rather than later. |
| 10732 | */ |
| 10733 | btrfs_warn(fs_info, "swapfile must not be inline"); |
| 10734 | ret = -EINVAL; |
| 10735 | goto out; |
| 10736 | } |
| 10737 | if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) { |
| 10738 | btrfs_warn(fs_info, "swapfile must not be compressed"); |
| 10739 | ret = -EINVAL; |
| 10740 | goto out; |
| 10741 | } |
| 10742 | |
| 10743 | logical_block_start = em->block_start + (start - em->start); |
| 10744 | len = min(len, em->len - (start - em->start)); |
| 10745 | free_extent_map(em); |
| 10746 | em = NULL; |
| 10747 | |
| 10748 | ret = can_nocow_extent(inode, start, &len, NULL, NULL, NULL); |
| 10749 | if (ret < 0) { |
| 10750 | goto out; |
| 10751 | } else if (ret) { |
| 10752 | ret = 0; |
| 10753 | } else { |
| 10754 | btrfs_warn(fs_info, |
| 10755 | "swapfile must not be copy-on-write"); |
| 10756 | ret = -EINVAL; |
| 10757 | goto out; |
| 10758 | } |
| 10759 | |
| 10760 | em = btrfs_get_chunk_map(fs_info, logical_block_start, len); |
| 10761 | if (IS_ERR(em)) { |
| 10762 | ret = PTR_ERR(em); |
| 10763 | goto out; |
| 10764 | } |
| 10765 | |
| 10766 | if (em->map_lookup->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) { |
| 10767 | btrfs_warn(fs_info, |
| 10768 | "swapfile must have single data profile"); |
| 10769 | ret = -EINVAL; |
| 10770 | goto out; |
| 10771 | } |
| 10772 | |
| 10773 | if (device == NULL) { |
| 10774 | device = em->map_lookup->stripes[0].dev; |
| 10775 | ret = btrfs_add_swapfile_pin(inode, device, false); |
| 10776 | if (ret == 1) |
| 10777 | ret = 0; |
| 10778 | else if (ret) |
| 10779 | goto out; |
| 10780 | } else if (device != em->map_lookup->stripes[0].dev) { |
| 10781 | btrfs_warn(fs_info, "swapfile must be on one device"); |
| 10782 | ret = -EINVAL; |
| 10783 | goto out; |
| 10784 | } |
| 10785 | |
| 10786 | physical_block_start = (em->map_lookup->stripes[0].physical + |
| 10787 | (logical_block_start - em->start)); |
| 10788 | len = min(len, em->len - (logical_block_start - em->start)); |
| 10789 | free_extent_map(em); |
| 10790 | em = NULL; |
| 10791 | |
| 10792 | bg = btrfs_lookup_block_group(fs_info, logical_block_start); |
| 10793 | if (!bg) { |
| 10794 | btrfs_warn(fs_info, |
| 10795 | "could not find block group containing swapfile"); |
| 10796 | ret = -EINVAL; |
| 10797 | goto out; |
| 10798 | } |
| 10799 | |
| 10800 | ret = btrfs_add_swapfile_pin(inode, bg, true); |
| 10801 | if (ret) { |
| 10802 | btrfs_put_block_group(bg); |
| 10803 | if (ret == 1) |
| 10804 | ret = 0; |
| 10805 | else |
| 10806 | goto out; |
| 10807 | } |
| 10808 | |
| 10809 | if (bsi.block_len && |
| 10810 | bsi.block_start + bsi.block_len == physical_block_start) { |
| 10811 | bsi.block_len += len; |
| 10812 | } else { |
| 10813 | if (bsi.block_len) { |
| 10814 | ret = btrfs_add_swap_extent(sis, &bsi); |
| 10815 | if (ret) |
| 10816 | goto out; |
| 10817 | } |
| 10818 | bsi.start = start; |
| 10819 | bsi.block_start = physical_block_start; |
| 10820 | bsi.block_len = len; |
| 10821 | } |
| 10822 | |
| 10823 | start += len; |
| 10824 | } |
| 10825 | |
| 10826 | if (bsi.block_len) |
| 10827 | ret = btrfs_add_swap_extent(sis, &bsi); |
| 10828 | |
| 10829 | out: |
| 10830 | if (!IS_ERR_OR_NULL(em)) |
| 10831 | free_extent_map(em); |
| 10832 | |
| 10833 | unlock_extent_cached(io_tree, 0, isize - 1, &cached_state); |
| 10834 | |
| 10835 | if (ret) |
| 10836 | btrfs_swap_deactivate(file); |
| 10837 | |
| 10838 | clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags); |
| 10839 | |
| 10840 | if (ret) |
| 10841 | return ret; |
| 10842 | |
| 10843 | if (device) |
| 10844 | sis->bdev = device->bdev; |
| 10845 | *span = bsi.highest_ppage - bsi.lowest_ppage + 1; |
| 10846 | sis->max = bsi.nr_pages; |
| 10847 | sis->pages = bsi.nr_pages - 1; |
| 10848 | sis->highest_bit = bsi.nr_pages - 1; |
| 10849 | return bsi.nr_extents; |
| 10850 | } |
| 10851 | #else |
| 10852 | static void btrfs_swap_deactivate(struct file *file) |
| 10853 | { |
| 10854 | } |
| 10855 | |
| 10856 | static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file, |
| 10857 | sector_t *span) |
| 10858 | { |
| 10859 | return -EOPNOTSUPP; |
| 10860 | } |
| 10861 | #endif |
| 10862 | |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 10863 | static const struct inode_operations btrfs_dir_inode_operations = { |
Chris Mason | 3394e16 | 2008-11-17 20:42:26 -0500 | [diff] [blame] | 10864 | .getattr = btrfs_getattr, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10865 | .lookup = btrfs_lookup, |
| 10866 | .create = btrfs_create, |
| 10867 | .unlink = btrfs_unlink, |
| 10868 | .link = btrfs_link, |
| 10869 | .mkdir = btrfs_mkdir, |
| 10870 | .rmdir = btrfs_rmdir, |
Miklos Szeredi | 2773bf0 | 2016-09-27 11:03:58 +0200 | [diff] [blame] | 10871 | .rename = btrfs_rename2, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10872 | .symlink = btrfs_symlink, |
| 10873 | .setattr = btrfs_setattr, |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 10874 | .mknod = btrfs_mknod, |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 10875 | .listxattr = btrfs_listxattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 10876 | .permission = btrfs_permission, |
Christoph Hellwig | 4e34e71 | 2011-07-23 17:37:31 +0200 | [diff] [blame] | 10877 | .get_acl = btrfs_get_acl, |
Christoph Hellwig | 996a710 | 2013-12-20 05:16:43 -0800 | [diff] [blame] | 10878 | .set_acl = btrfs_set_acl, |
Guangyu Sun | 93fd63c | 2013-09-16 10:42:03 -0700 | [diff] [blame] | 10879 | .update_time = btrfs_update_time, |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 10880 | .tmpfile = btrfs_tmpfile, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10881 | }; |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 10882 | static const struct inode_operations btrfs_dir_ro_inode_operations = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10883 | .lookup = btrfs_lookup, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 10884 | .permission = btrfs_permission, |
Guangyu Sun | 93fd63c | 2013-09-16 10:42:03 -0700 | [diff] [blame] | 10885 | .update_time = btrfs_update_time, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10886 | }; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 10887 | |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 10888 | static const struct file_operations btrfs_dir_file_operations = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10889 | .llseek = generic_file_llseek, |
| 10890 | .read = generic_read_dir, |
Omar Sandoval | 02dbfc9 | 2016-05-20 13:50:33 -0700 | [diff] [blame] | 10891 | .iterate_shared = btrfs_real_readdir, |
Josef Bacik | 23b5ec7 | 2017-07-24 15:14:25 -0400 | [diff] [blame] | 10892 | .open = btrfs_opendir, |
Christoph Hellwig | 34287aa | 2007-09-14 10:22:47 -0400 | [diff] [blame] | 10893 | .unlocked_ioctl = btrfs_ioctl, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10894 | #ifdef CONFIG_COMPAT |
Luke Dashjr | 4c63c24 | 2015-10-29 08:22:21 +0000 | [diff] [blame] | 10895 | .compat_ioctl = btrfs_compat_ioctl, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10896 | #endif |
Sage Weil | 6bf13c0 | 2008-06-10 10:07:39 -0400 | [diff] [blame] | 10897 | .release = btrfs_release_file, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 10898 | .fsync = btrfs_sync_file, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10899 | }; |
| 10900 | |
David Sterba | 20e5506 | 2015-11-19 11:42:28 +0100 | [diff] [blame] | 10901 | static const struct extent_io_ops btrfs_extent_io_ops = { |
David Sterba | 4d53ddd | 2017-02-17 15:27:44 +0100 | [diff] [blame] | 10902 | /* mandatory callbacks */ |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 10903 | .submit_bio_hook = btrfs_submit_bio_hook, |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 10904 | .readpage_end_io_hook = btrfs_readpage_end_io_hook, |
| 10905 | }; |
| 10906 | |
Chris Mason | 3505439 | 2009-01-21 13:11:13 -0500 | [diff] [blame] | 10907 | /* |
| 10908 | * btrfs doesn't support the bmap operation because swapfiles |
| 10909 | * use bmap to make a mapping of extents in the file. They assume |
| 10910 | * these extents won't change over the life of the file and they |
| 10911 | * use the bmap result to do IO directly to the drive. |
| 10912 | * |
| 10913 | * the btrfs bmap call would return logical addresses that aren't |
| 10914 | * suitable for IO and they also will change frequently as COW |
| 10915 | * operations happen. So, swapfile + btrfs == corruption. |
| 10916 | * |
| 10917 | * For now we're avoiding this by dropping bmap. |
| 10918 | */ |
Alexey Dobriyan | 7f09410 | 2009-09-21 17:01:10 -0700 | [diff] [blame] | 10919 | static const struct address_space_operations btrfs_aops = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10920 | .readpage = btrfs_readpage, |
| 10921 | .writepage = btrfs_writepage, |
Chris Mason | b293f02e | 2007-11-01 19:45:34 -0400 | [diff] [blame] | 10922 | .writepages = btrfs_writepages, |
Chris Mason | 3ab2fb5 | 2007-11-08 10:59:22 -0500 | [diff] [blame] | 10923 | .readpages = btrfs_readpages, |
Chris Mason | 1643298 | 2008-04-10 10:23:21 -0400 | [diff] [blame] | 10924 | .direct_IO = btrfs_direct_IO, |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 10925 | .invalidatepage = btrfs_invalidatepage, |
| 10926 | .releasepage = btrfs_releasepage, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 10927 | .set_page_dirty = btrfs_set_page_dirty, |
Andi Kleen | 465fdd9 | 2009-09-16 11:50:18 +0200 | [diff] [blame] | 10928 | .error_remove_page = generic_error_remove_page, |
Omar Sandoval | ed46ff3 | 2016-11-03 10:28:14 -0700 | [diff] [blame] | 10929 | .swap_activate = btrfs_swap_activate, |
| 10930 | .swap_deactivate = btrfs_swap_deactivate, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10931 | }; |
| 10932 | |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 10933 | static const struct inode_operations btrfs_file_inode_operations = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10934 | .getattr = btrfs_getattr, |
| 10935 | .setattr = btrfs_setattr, |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 10936 | .listxattr = btrfs_listxattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 10937 | .permission = btrfs_permission, |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 10938 | .fiemap = btrfs_fiemap, |
Christoph Hellwig | 4e34e71 | 2011-07-23 17:37:31 +0200 | [diff] [blame] | 10939 | .get_acl = btrfs_get_acl, |
Christoph Hellwig | 996a710 | 2013-12-20 05:16:43 -0800 | [diff] [blame] | 10940 | .set_acl = btrfs_set_acl, |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 10941 | .update_time = btrfs_update_time, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10942 | }; |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 10943 | static const struct inode_operations btrfs_special_inode_operations = { |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 10944 | .getattr = btrfs_getattr, |
| 10945 | .setattr = btrfs_setattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 10946 | .permission = btrfs_permission, |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 10947 | .listxattr = btrfs_listxattr, |
Christoph Hellwig | 4e34e71 | 2011-07-23 17:37:31 +0200 | [diff] [blame] | 10948 | .get_acl = btrfs_get_acl, |
Christoph Hellwig | 996a710 | 2013-12-20 05:16:43 -0800 | [diff] [blame] | 10949 | .set_acl = btrfs_set_acl, |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 10950 | .update_time = btrfs_update_time, |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 10951 | }; |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 10952 | static const struct inode_operations btrfs_symlink_inode_operations = { |
Al Viro | 6b25539 | 2015-11-17 10:20:54 -0500 | [diff] [blame] | 10953 | .get_link = page_get_link, |
Li Zefan | f209561 | 2010-11-19 02:05:24 +0000 | [diff] [blame] | 10954 | .getattr = btrfs_getattr, |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 10955 | .setattr = btrfs_setattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 10956 | .permission = btrfs_permission, |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 10957 | .listxattr = btrfs_listxattr, |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 10958 | .update_time = btrfs_update_time, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10959 | }; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 10960 | |
Alexey Dobriyan | 82d339d | 2009-10-09 09:54:36 -0400 | [diff] [blame] | 10961 | const struct dentry_operations btrfs_dentry_operations = { |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 10962 | .d_delete = btrfs_dentry_delete, |
| 10963 | }; |