Chris Mason | 6cbd557 | 2007-06-12 09:07:21 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 Oracle. All rights reserved. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public |
| 6 | * License v2 as published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 11 | * General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public |
| 14 | * License along with this program; if not, write to the |
| 15 | * Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
| 16 | * Boston, MA 021110-1307, USA. |
| 17 | */ |
| 18 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 19 | #include <linux/kernel.h> |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 20 | #include <linux/bio.h> |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 21 | #include <linux/buffer_head.h> |
Sage Weil | f2eb0a2 | 2008-05-02 14:43:14 -0400 | [diff] [blame] | 22 | #include <linux/file.h> |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 23 | #include <linux/fs.h> |
| 24 | #include <linux/pagemap.h> |
| 25 | #include <linux/highmem.h> |
| 26 | #include <linux/time.h> |
| 27 | #include <linux/init.h> |
| 28 | #include <linux/string.h> |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 29 | #include <linux/backing-dev.h> |
| 30 | #include <linux/mpage.h> |
| 31 | #include <linux/swap.h> |
| 32 | #include <linux/writeback.h> |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 33 | #include <linux/compat.h> |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 34 | #include <linux/bit_spinlock.h> |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 35 | #include <linux/xattr.h> |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 36 | #include <linux/posix_acl.h> |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 37 | #include <linux/falloc.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 38 | #include <linux/slab.h> |
David Sterba | 7a36dde | 2011-05-06 15:33:15 +0200 | [diff] [blame] | 39 | #include <linux/ratelimit.h> |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 40 | #include <linux/mount.h> |
Filipe Brandenburger | 55e301f | 2013-01-29 06:04:50 +0000 | [diff] [blame] | 41 | #include <linux/btrfs.h> |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 42 | #include <linux/blkdev.h> |
Josef Bacik | f23b5a5 | 2013-06-19 10:16:26 -0400 | [diff] [blame] | 43 | #include <linux/posix_acl_xattr.h> |
Christoph Hellwig | e2e40f2 | 2015-02-22 08:58:50 -0800 | [diff] [blame] | 44 | #include <linux/uio.h> |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 45 | #include "ctree.h" |
| 46 | #include "disk-io.h" |
| 47 | #include "transaction.h" |
| 48 | #include "btrfs_inode.h" |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 49 | #include "print-tree.h" |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 50 | #include "ordered-data.h" |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 51 | #include "xattr.h" |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 52 | #include "tree-log.h" |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 53 | #include "volumes.h" |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 54 | #include "compression.h" |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 55 | #include "locking.h" |
Josef Bacik | dc89e98 | 2011-01-28 17:05:48 -0500 | [diff] [blame] | 56 | #include "free-space-cache.h" |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 57 | #include "inode-map.h" |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 58 | #include "backref.h" |
Josef Bacik | f23b5a5 | 2013-06-19 10:16:26 -0400 | [diff] [blame] | 59 | #include "hash.h" |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 60 | #include "props.h" |
Dongsheng Yang | 3119321 | 2014-12-12 16:44:35 +0800 | [diff] [blame] | 61 | #include "qgroup.h" |
Wang Xiaoguang | dda3245 | 2016-07-11 11:05:29 +0800 | [diff] [blame] | 62 | #include "dedupe.h" |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 63 | |
| 64 | struct btrfs_iget_args { |
Chris Mason | 90d3e59 | 2014-01-09 17:28:00 -0800 | [diff] [blame] | 65 | struct btrfs_key *location; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 66 | struct btrfs_root *root; |
| 67 | }; |
| 68 | |
Filipe Manana | f28a492 | 2015-12-08 19:23:20 +0000 | [diff] [blame] | 69 | struct btrfs_dio_data { |
| 70 | u64 outstanding_extents; |
| 71 | u64 reserve; |
| 72 | u64 unsubmitted_oe_range_start; |
| 73 | u64 unsubmitted_oe_range_end; |
Liu Bo | 4aaedfb | 2016-12-14 22:36:05 -0800 | [diff] [blame] | 74 | int overwrite; |
Filipe Manana | f28a492 | 2015-12-08 19:23:20 +0000 | [diff] [blame] | 75 | }; |
| 76 | |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 77 | static const struct inode_operations btrfs_dir_inode_operations; |
| 78 | static const struct inode_operations btrfs_symlink_inode_operations; |
| 79 | static const struct inode_operations btrfs_dir_ro_inode_operations; |
| 80 | static const struct inode_operations btrfs_special_inode_operations; |
| 81 | static const struct inode_operations btrfs_file_inode_operations; |
Alexey Dobriyan | 7f09410 | 2009-09-21 17:01:10 -0700 | [diff] [blame] | 82 | static const struct address_space_operations btrfs_aops; |
| 83 | static const struct address_space_operations btrfs_symlink_aops; |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 84 | static const struct file_operations btrfs_dir_file_operations; |
David Sterba | 20e5506 | 2015-11-19 11:42:28 +0100 | [diff] [blame] | 85 | static const struct extent_io_ops btrfs_extent_io_ops; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 86 | |
| 87 | static struct kmem_cache *btrfs_inode_cachep; |
| 88 | struct kmem_cache *btrfs_trans_handle_cachep; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 89 | struct kmem_cache *btrfs_path_cachep; |
Josef Bacik | dc89e98 | 2011-01-28 17:05:48 -0500 | [diff] [blame] | 90 | struct kmem_cache *btrfs_free_space_cachep; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 91 | |
| 92 | #define S_SHIFT 12 |
David Sterba | 4d4ab6d | 2015-11-19 11:42:31 +0100 | [diff] [blame] | 93 | static const unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 94 | [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE, |
| 95 | [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR, |
| 96 | [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV, |
| 97 | [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV, |
| 98 | [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO, |
| 99 | [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK, |
| 100 | [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK, |
| 101 | }; |
| 102 | |
Eric Sandeen | 3972f26 | 2013-01-12 02:57:22 +0000 | [diff] [blame] | 103 | static int btrfs_setsize(struct inode *inode, struct iattr *attr); |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 104 | static int btrfs_truncate(struct inode *inode); |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 105 | static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 106 | static noinline int cow_file_range(struct inode *inode, |
| 107 | struct page *locked_page, |
Wang Xiaoguang | dda3245 | 2016-07-11 11:05:29 +0800 | [diff] [blame] | 108 | u64 start, u64 end, u64 delalloc_end, |
| 109 | int *page_started, unsigned long *nr_written, |
| 110 | int unlock, struct btrfs_dedupe_hash *hash); |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 111 | static struct extent_map *create_io_em(struct inode *inode, u64 start, u64 len, |
| 112 | u64 orig_start, u64 block_start, |
| 113 | u64 block_len, u64 orig_block_len, |
| 114 | u64 ram_bytes, int compress_type, |
| 115 | int type); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 116 | |
Qu Wenruo | 52427260 | 2017-03-08 10:25:52 +0800 | [diff] [blame] | 117 | static void __endio_write_update_ordered(struct inode *inode, |
| 118 | const u64 offset, const u64 bytes, |
| 119 | const bool uptodate); |
| 120 | |
| 121 | /* |
| 122 | * Cleanup all submitted ordered extents in specified range to handle errors |
| 123 | * from the fill_dellaloc() callback. |
| 124 | * |
| 125 | * NOTE: caller must ensure that when an error happens, it can not call |
| 126 | * extent_clear_unlock_delalloc() to clear both the bits EXTENT_DO_ACCOUNTING |
| 127 | * and EXTENT_DELALLOC simultaneously, because that causes the reserved metadata |
| 128 | * to be released, which we want to happen only when finishing the ordered |
| 129 | * extent (btrfs_finish_ordered_io()). Also note that the caller of the |
| 130 | * fill_delalloc() callback already does proper cleanup for the first page of |
| 131 | * the range, that is, it invokes the callback writepage_end_io_hook() for the |
| 132 | * range of the first page. |
| 133 | */ |
| 134 | static inline void btrfs_cleanup_ordered_extents(struct inode *inode, |
| 135 | const u64 offset, |
| 136 | const u64 bytes) |
| 137 | { |
| 138 | return __endio_write_update_ordered(inode, offset + PAGE_SIZE, |
| 139 | bytes - PAGE_SIZE, false); |
| 140 | } |
| 141 | |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 142 | static int btrfs_dirty_inode(struct inode *inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 143 | |
Josef Bacik | 6a3891c | 2015-03-16 17:38:52 -0400 | [diff] [blame] | 144 | #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS |
| 145 | void btrfs_test_inode_set_ops(struct inode *inode) |
| 146 | { |
| 147 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
| 148 | } |
| 149 | #endif |
| 150 | |
Yan, Zheng | f34f57a | 2009-11-12 09:35:27 +0000 | [diff] [blame] | 151 | static int btrfs_init_inode_security(struct btrfs_trans_handle *trans, |
Eric Paris | 2a7dba3 | 2011-02-01 11:05:39 -0500 | [diff] [blame] | 152 | struct inode *inode, struct inode *dir, |
| 153 | const struct qstr *qstr) |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 154 | { |
| 155 | int err; |
| 156 | |
Yan, Zheng | f34f57a | 2009-11-12 09:35:27 +0000 | [diff] [blame] | 157 | err = btrfs_init_acl(trans, inode, dir); |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 158 | if (!err) |
Eric Paris | 2a7dba3 | 2011-02-01 11:05:39 -0500 | [diff] [blame] | 159 | err = btrfs_xattr_security_init(trans, inode, dir, qstr); |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 160 | return err; |
| 161 | } |
| 162 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 163 | /* |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 164 | * this does all the hard work for inserting an inline extent into |
| 165 | * the btree. The caller should have done a btrfs_drop_extents so that |
| 166 | * no overlapping inline items exist in the btree |
| 167 | */ |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 168 | static int insert_inline_extent(struct btrfs_trans_handle *trans, |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 169 | struct btrfs_path *path, int extent_inserted, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 170 | struct btrfs_root *root, struct inode *inode, |
| 171 | u64 start, size_t size, size_t compressed_size, |
Li Zefan | fe3f566 | 2011-03-28 08:30:38 +0000 | [diff] [blame] | 172 | int compress_type, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 173 | struct page **compressed_pages) |
| 174 | { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 175 | struct extent_buffer *leaf; |
| 176 | struct page *page = NULL; |
| 177 | char *kaddr; |
| 178 | unsigned long ptr; |
| 179 | struct btrfs_file_extent_item *ei; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 180 | int ret; |
| 181 | size_t cur_size = size; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 182 | unsigned long offset; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 183 | |
Li Zefan | fe3f566 | 2011-03-28 08:30:38 +0000 | [diff] [blame] | 184 | if (compressed_size && compressed_pages) |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 185 | cur_size = compressed_size; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 186 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 187 | inode_add_bytes(inode, size); |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 188 | |
| 189 | if (!extent_inserted) { |
| 190 | struct btrfs_key key; |
| 191 | size_t datasize; |
| 192 | |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 193 | key.objectid = btrfs_ino(BTRFS_I(inode)); |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 194 | key.offset = start; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 195 | key.type = BTRFS_EXTENT_DATA_KEY; |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 196 | |
| 197 | datasize = btrfs_file_extent_calc_inline_size(cur_size); |
| 198 | path->leave_spinning = 1; |
| 199 | ret = btrfs_insert_empty_item(trans, root, path, &key, |
| 200 | datasize); |
David Sterba | 79b4f4c | 2017-06-15 19:09:51 +0200 | [diff] [blame] | 201 | if (ret) |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 202 | goto fail; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 203 | } |
| 204 | leaf = path->nodes[0]; |
| 205 | ei = btrfs_item_ptr(leaf, path->slots[0], |
| 206 | struct btrfs_file_extent_item); |
| 207 | btrfs_set_file_extent_generation(leaf, ei, trans->transid); |
| 208 | btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE); |
| 209 | btrfs_set_file_extent_encryption(leaf, ei, 0); |
| 210 | btrfs_set_file_extent_other_encoding(leaf, ei, 0); |
| 211 | btrfs_set_file_extent_ram_bytes(leaf, ei, size); |
| 212 | ptr = btrfs_file_extent_inline_start(ei); |
| 213 | |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 214 | if (compress_type != BTRFS_COMPRESS_NONE) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 215 | struct page *cpage; |
| 216 | int i = 0; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 217 | while (compressed_size > 0) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 218 | cpage = compressed_pages[i]; |
Chris Mason | 5b050f0 | 2008-11-11 09:34:41 -0500 | [diff] [blame] | 219 | cur_size = min_t(unsigned long, compressed_size, |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 220 | PAGE_SIZE); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 221 | |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 222 | kaddr = kmap_atomic(cpage); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 223 | write_extent_buffer(leaf, kaddr, ptr, cur_size); |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 224 | kunmap_atomic(kaddr); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 225 | |
| 226 | i++; |
| 227 | ptr += cur_size; |
| 228 | compressed_size -= cur_size; |
| 229 | } |
| 230 | btrfs_set_file_extent_compression(leaf, ei, |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 231 | compress_type); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 232 | } else { |
| 233 | page = find_get_page(inode->i_mapping, |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 234 | start >> PAGE_SHIFT); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 235 | btrfs_set_file_extent_compression(leaf, ei, 0); |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 236 | kaddr = kmap_atomic(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 237 | offset = start & (PAGE_SIZE - 1); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 238 | write_extent_buffer(leaf, kaddr + offset, ptr, size); |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 239 | kunmap_atomic(kaddr); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 240 | put_page(page); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 241 | } |
| 242 | btrfs_mark_buffer_dirty(leaf); |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 243 | btrfs_release_path(path); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 244 | |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 245 | /* |
| 246 | * we're an inline extent, so nobody can |
| 247 | * extend the file past i_size without locking |
| 248 | * a page we already have locked. |
| 249 | * |
| 250 | * We must do any isize and inode updates |
| 251 | * before we unlock the pages. Otherwise we |
| 252 | * could end up racing with unlink. |
| 253 | */ |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 254 | BTRFS_I(inode)->disk_i_size = inode->i_size; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 255 | ret = btrfs_update_inode(trans, root, inode); |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 256 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 257 | fail: |
David Sterba | 79b4f4c | 2017-06-15 19:09:51 +0200 | [diff] [blame] | 258 | return ret; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | |
| 262 | /* |
| 263 | * conditionally insert an inline extent into the file. This |
| 264 | * does the checks required to make sure the data is small enough |
| 265 | * to fit as an inline extent. |
| 266 | */ |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 267 | static noinline int cow_file_range_inline(struct btrfs_root *root, |
| 268 | struct inode *inode, u64 start, |
| 269 | u64 end, size_t compressed_size, |
| 270 | int compress_type, |
| 271 | struct page **compressed_pages) |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 272 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 273 | struct btrfs_fs_info *fs_info = root->fs_info; |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 274 | struct btrfs_trans_handle *trans; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 275 | u64 isize = i_size_read(inode); |
| 276 | u64 actual_end = min(end + 1, isize); |
| 277 | u64 inline_len = actual_end - start; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 278 | u64 aligned_end = ALIGN(end, fs_info->sectorsize); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 279 | u64 data_len = inline_len; |
| 280 | int ret; |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 281 | struct btrfs_path *path; |
| 282 | int extent_inserted = 0; |
| 283 | u32 extent_item_size; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 284 | |
| 285 | if (compressed_size) |
| 286 | data_len = compressed_size; |
| 287 | |
| 288 | if (start > 0 || |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 289 | actual_end > fs_info->sectorsize || |
| 290 | data_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info) || |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 291 | (!compressed_size && |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 292 | (actual_end & (fs_info->sectorsize - 1)) == 0) || |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 293 | end + 1 < isize || |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 294 | data_len > fs_info->max_inline) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 295 | return 1; |
| 296 | } |
| 297 | |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 298 | path = btrfs_alloc_path(); |
| 299 | if (!path) |
| 300 | return -ENOMEM; |
| 301 | |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 302 | trans = btrfs_join_transaction(root); |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 303 | if (IS_ERR(trans)) { |
| 304 | btrfs_free_path(path); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 305 | return PTR_ERR(trans); |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 306 | } |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 307 | trans->block_rsv = &fs_info->delalloc_block_rsv; |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 308 | |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 309 | if (compressed_size && compressed_pages) |
| 310 | extent_item_size = btrfs_file_extent_calc_inline_size( |
| 311 | compressed_size); |
| 312 | else |
| 313 | extent_item_size = btrfs_file_extent_calc_inline_size( |
| 314 | inline_len); |
| 315 | |
| 316 | ret = __btrfs_drop_extents(trans, root, inode, path, |
| 317 | start, aligned_end, NULL, |
| 318 | 1, 1, extent_item_size, &extent_inserted); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 319 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 320 | btrfs_abort_transaction(trans, ret); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 321 | goto out; |
| 322 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 323 | |
| 324 | if (isize > actual_end) |
| 325 | inline_len = min_t(u64, isize, actual_end); |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 326 | ret = insert_inline_extent(trans, path, extent_inserted, |
| 327 | root, inode, start, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 328 | inline_len, compressed_size, |
Li Zefan | fe3f566 | 2011-03-28 08:30:38 +0000 | [diff] [blame] | 329 | compress_type, compressed_pages); |
Josef Bacik | 2adcac1 | 2012-05-23 16:10:14 -0400 | [diff] [blame] | 330 | if (ret && ret != -ENOSPC) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 331 | btrfs_abort_transaction(trans, ret); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 332 | goto out; |
Josef Bacik | 2adcac1 | 2012-05-23 16:10:14 -0400 | [diff] [blame] | 333 | } else if (ret == -ENOSPC) { |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 334 | ret = 1; |
| 335 | goto out; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 336 | } |
Josef Bacik | 2adcac1 | 2012-05-23 16:10:14 -0400 | [diff] [blame] | 337 | |
Josef Bacik | bdc20e6 | 2013-02-28 13:23:38 -0500 | [diff] [blame] | 338 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags); |
Nikolay Borisov | 691fa05 | 2017-02-20 13:50:42 +0200 | [diff] [blame] | 339 | btrfs_delalloc_release_metadata(BTRFS_I(inode), end + 1 - start); |
Nikolay Borisov | dcdbc05 | 2017-02-20 13:50:45 +0200 | [diff] [blame] | 340 | btrfs_drop_extent_cache(BTRFS_I(inode), start, aligned_end - 1, 0); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 341 | out: |
Qu Wenruo | 94ed938 | 2015-09-08 17:25:56 +0800 | [diff] [blame] | 342 | /* |
| 343 | * Don't forget to free the reserved space, as for inlined extent |
| 344 | * it won't count as data extent, free them directly here. |
| 345 | * And at reserve time, it's always aligned to page size, so |
| 346 | * just free one page here. |
| 347 | */ |
Qu Wenruo | bc42bda | 2017-02-27 15:10:39 +0800 | [diff] [blame] | 348 | btrfs_qgroup_free_data(inode, NULL, 0, PAGE_SIZE); |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 349 | btrfs_free_path(path); |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 350 | btrfs_end_transaction(trans); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 351 | return ret; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 352 | } |
| 353 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 354 | struct async_extent { |
| 355 | u64 start; |
| 356 | u64 ram_size; |
| 357 | u64 compressed_size; |
| 358 | struct page **pages; |
| 359 | unsigned long nr_pages; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 360 | int compress_type; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 361 | struct list_head list; |
| 362 | }; |
| 363 | |
| 364 | struct async_cow { |
| 365 | struct inode *inode; |
| 366 | struct btrfs_root *root; |
| 367 | struct page *locked_page; |
| 368 | u64 start; |
| 369 | u64 end; |
| 370 | struct list_head extents; |
| 371 | struct btrfs_work work; |
| 372 | }; |
| 373 | |
| 374 | static noinline int add_async_extent(struct async_cow *cow, |
| 375 | u64 start, u64 ram_size, |
| 376 | u64 compressed_size, |
| 377 | struct page **pages, |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 378 | unsigned long nr_pages, |
| 379 | int compress_type) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 380 | { |
| 381 | struct async_extent *async_extent; |
| 382 | |
| 383 | async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 384 | BUG_ON(!async_extent); /* -ENOMEM */ |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 385 | async_extent->start = start; |
| 386 | async_extent->ram_size = ram_size; |
| 387 | async_extent->compressed_size = compressed_size; |
| 388 | async_extent->pages = pages; |
| 389 | async_extent->nr_pages = nr_pages; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 390 | async_extent->compress_type = compress_type; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 391 | list_add_tail(&async_extent->list, &cow->extents); |
| 392 | return 0; |
| 393 | } |
| 394 | |
Wang Shilong | f79707b | 2014-07-17 11:44:09 +0800 | [diff] [blame] | 395 | static inline int inode_need_compress(struct inode *inode) |
| 396 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 397 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Wang Shilong | f79707b | 2014-07-17 11:44:09 +0800 | [diff] [blame] | 398 | |
| 399 | /* force compress */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 400 | if (btrfs_test_opt(fs_info, FORCE_COMPRESS)) |
Wang Shilong | f79707b | 2014-07-17 11:44:09 +0800 | [diff] [blame] | 401 | return 1; |
| 402 | /* bad compression ratios */ |
| 403 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS) |
| 404 | return 0; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 405 | if (btrfs_test_opt(fs_info, COMPRESS) || |
Wang Shilong | f79707b | 2014-07-17 11:44:09 +0800 | [diff] [blame] | 406 | BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS || |
| 407 | BTRFS_I(inode)->force_compress) |
| 408 | return 1; |
| 409 | return 0; |
| 410 | } |
| 411 | |
Nikolay Borisov | 6158e1c | 2017-02-20 13:50:43 +0200 | [diff] [blame] | 412 | static inline void inode_should_defrag(struct btrfs_inode *inode, |
Anand Jain | 26d30f8 | 2016-12-19 19:09:06 +0800 | [diff] [blame] | 413 | u64 start, u64 end, u64 num_bytes, u64 small_write) |
| 414 | { |
| 415 | /* If this is a small write inside eof, kick off a defrag */ |
| 416 | if (num_bytes < small_write && |
Nikolay Borisov | 6158e1c | 2017-02-20 13:50:43 +0200 | [diff] [blame] | 417 | (start > 0 || end + 1 < inode->disk_i_size)) |
Anand Jain | 26d30f8 | 2016-12-19 19:09:06 +0800 | [diff] [blame] | 418 | btrfs_add_inode_defrag(NULL, inode); |
| 419 | } |
| 420 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 421 | /* |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 422 | * we create compressed extents in two phases. The first |
| 423 | * phase compresses a range of pages that have already been |
| 424 | * locked (both pages and state bits are locked). |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 425 | * |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 426 | * This is done inside an ordered work queue, and the compression |
| 427 | * is spread across many cpus. The actual IO submission is step |
| 428 | * two, and the ordered work queue takes care of making sure that |
| 429 | * happens in the same order things were put onto the queue by |
| 430 | * writepages and friends. |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 431 | * |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 432 | * If this code finds it can't get good compression, it puts an |
| 433 | * entry onto the work queue to write the uncompressed bytes. This |
| 434 | * makes sure that both compressed inodes and uncompressed inodes |
Artem Bityutskiy | b257031 | 2012-07-25 18:12:06 +0300 | [diff] [blame] | 435 | * are written in the same order that the flusher thread sent them |
| 436 | * down. |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 437 | */ |
Filipe Manana | c44f649 | 2014-10-09 21:15:44 +0100 | [diff] [blame] | 438 | static noinline void compress_file_range(struct inode *inode, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 439 | struct page *locked_page, |
| 440 | u64 start, u64 end, |
| 441 | struct async_cow *async_cow, |
| 442 | int *num_added) |
Chris Mason | b888db2b | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 443 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 444 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Chris Mason | b888db2b | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 445 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 446 | u64 num_bytes; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 447 | u64 blocksize = fs_info->sectorsize; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 448 | u64 actual_end; |
Chris Mason | 42dc7ba | 2008-12-15 11:44:56 -0500 | [diff] [blame] | 449 | u64 isize = i_size_read(inode); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 450 | int ret = 0; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 451 | struct page **pages = NULL; |
| 452 | unsigned long nr_pages; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 453 | unsigned long total_compressed = 0; |
| 454 | unsigned long total_in = 0; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 455 | int i; |
| 456 | int will_compress; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 457 | int compress_type = fs_info->compress_type; |
Chris Mason | 4adaa61 | 2013-03-26 13:07:00 -0400 | [diff] [blame] | 458 | int redirty = 0; |
Chris Mason | b888db2b | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 459 | |
Nikolay Borisov | 6158e1c | 2017-02-20 13:50:43 +0200 | [diff] [blame] | 460 | inode_should_defrag(BTRFS_I(inode), start, end, end - start + 1, |
| 461 | SZ_16K); |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 462 | |
Chris Mason | 42dc7ba | 2008-12-15 11:44:56 -0500 | [diff] [blame] | 463 | actual_end = min_t(u64, isize, end + 1); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 464 | again: |
| 465 | will_compress = 0; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 466 | nr_pages = (end >> PAGE_SHIFT) - (start >> PAGE_SHIFT) + 1; |
David Sterba | 069eac7 | 2017-02-14 19:36:54 +0100 | [diff] [blame] | 467 | BUILD_BUG_ON((BTRFS_MAX_COMPRESSED % PAGE_SIZE) != 0); |
| 468 | nr_pages = min_t(unsigned long, nr_pages, |
| 469 | BTRFS_MAX_COMPRESSED / PAGE_SIZE); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 470 | |
Chris Mason | f03d9301f | 2009-02-04 09:31:06 -0500 | [diff] [blame] | 471 | /* |
| 472 | * we don't want to send crud past the end of i_size through |
| 473 | * compression, that's just a waste of CPU time. So, if the |
| 474 | * end of the file is before the start of our current |
| 475 | * requested range of bytes, we bail out to the uncompressed |
| 476 | * cleanup code that can deal with all of this. |
| 477 | * |
| 478 | * It isn't really the fastest way to fix things, but this is a |
| 479 | * very uncommon corner. |
| 480 | */ |
| 481 | if (actual_end <= start) |
| 482 | goto cleanup_and_bail_uncompressed; |
| 483 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 484 | total_compressed = actual_end - start; |
| 485 | |
Shilong Wang | 4bcbb33 | 2014-10-07 18:44:35 -0400 | [diff] [blame] | 486 | /* |
| 487 | * skip compression for a small file range(<=blocksize) that |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 488 | * 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] | 489 | */ |
| 490 | if (total_compressed <= blocksize && |
| 491 | (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size)) |
| 492 | goto cleanup_and_bail_uncompressed; |
| 493 | |
David Sterba | 069eac7 | 2017-02-14 19:36:54 +0100 | [diff] [blame] | 494 | total_compressed = min_t(unsigned long, total_compressed, |
| 495 | BTRFS_MAX_UNCOMPRESSED); |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 496 | num_bytes = ALIGN(end - start + 1, blocksize); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 497 | num_bytes = max(blocksize, num_bytes); |
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 | */ |
Wang Shilong | f79707b | 2014-07-17 11:44:09 +0800 | [diff] [blame] | 506 | if (inode_need_compress(inode)) { |
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 */ |
| 511 | goto cont; |
| 512 | } |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 513 | |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 514 | if (BTRFS_I(inode)->force_compress) |
| 515 | compress_type = BTRFS_I(inode)->force_compress; |
| 516 | |
Chris Mason | 4adaa61 | 2013-03-26 13:07:00 -0400 | [diff] [blame] | 517 | /* |
| 518 | * we need to call clear_page_dirty_for_io on each |
| 519 | * page in the range. Otherwise applications with the file |
| 520 | * mmap'd can wander in and change the page contents while |
| 521 | * we are compressing them. |
| 522 | * |
| 523 | * If the compression fails for any reason, we set the pages |
| 524 | * dirty again later on. |
| 525 | */ |
| 526 | extent_range_clear_dirty_for_io(inode, start, end); |
| 527 | redirty = 1; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 528 | ret = btrfs_compress_pages(compress_type, |
| 529 | inode->i_mapping, start, |
David Sterba | 38c3146 | 2017-02-14 19:04:07 +0100 | [diff] [blame] | 530 | pages, |
David Sterba | 4d3a800 | 2017-02-14 19:04:07 +0100 | [diff] [blame] | 531 | &nr_pages, |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 532 | &total_in, |
David Sterba | e5d7490 | 2017-02-14 19:45:05 +0100 | [diff] [blame] | 533 | &total_compressed); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 534 | |
| 535 | if (!ret) { |
| 536 | unsigned long offset = total_compressed & |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 537 | (PAGE_SIZE - 1); |
David Sterba | 4d3a800 | 2017-02-14 19:04:07 +0100 | [diff] [blame] | 538 | struct page *page = pages[nr_pages - 1]; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 539 | char *kaddr; |
| 540 | |
| 541 | /* zero the tail end of the last page, we might be |
| 542 | * sending it down to disk |
| 543 | */ |
| 544 | if (offset) { |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 545 | kaddr = kmap_atomic(page); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 546 | memset(kaddr + offset, 0, |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 547 | PAGE_SIZE - offset); |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 548 | kunmap_atomic(kaddr); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 549 | } |
| 550 | will_compress = 1; |
| 551 | } |
| 552 | } |
Li Zefan | 560f7d7 | 2011-09-08 10:22:01 +0800 | [diff] [blame] | 553 | cont: |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 554 | if (start == 0) { |
| 555 | /* lets try to make an inline extent */ |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 556 | if (ret || total_in < (actual_end - start)) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 557 | /* we didn't compress the entire range, try |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 558 | * to make an uncompressed inline extent. |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 559 | */ |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 560 | ret = cow_file_range_inline(root, inode, start, end, |
Anand Jain | f74670f | 2016-12-06 12:43:07 +0800 | [diff] [blame] | 561 | 0, BTRFS_COMPRESS_NONE, NULL); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 562 | } else { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 563 | /* try making a compressed inline extent */ |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 564 | ret = cow_file_range_inline(root, inode, start, end, |
Li Zefan | fe3f566 | 2011-03-28 08:30:38 +0000 | [diff] [blame] | 565 | total_compressed, |
| 566 | compress_type, pages); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 567 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 568 | if (ret <= 0) { |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 569 | unsigned long clear_flags = EXTENT_DELALLOC | |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 570 | EXTENT_DELALLOC_NEW | EXTENT_DEFRAG; |
Filipe Manana | e6eb431 | 2014-10-10 10:45:12 +0100 | [diff] [blame] | 571 | unsigned long page_error_op; |
| 572 | |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 573 | clear_flags |= (ret < 0) ? EXTENT_DO_ACCOUNTING : 0; |
Filipe Manana | e6eb431 | 2014-10-10 10:45:12 +0100 | [diff] [blame] | 574 | page_error_op = ret < 0 ? PAGE_SET_ERROR : 0; |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 575 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 576 | /* |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 577 | * inline extent creation worked or returned error, |
| 578 | * we don't need to create any more async work items. |
| 579 | * Unlock and free up our temp pages. |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 580 | */ |
Qu Wenruo | ba8b04c | 2016-07-19 16:50:36 +0800 | [diff] [blame] | 581 | extent_clear_unlock_delalloc(inode, start, end, end, |
| 582 | NULL, clear_flags, |
| 583 | PAGE_UNLOCK | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 584 | PAGE_CLEAR_DIRTY | |
| 585 | PAGE_SET_WRITEBACK | |
Filipe Manana | e6eb431 | 2014-10-10 10:45:12 +0100 | [diff] [blame] | 586 | page_error_op | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 587 | PAGE_END_WRITEBACK); |
Filipe Manana | 1c81ba2 | 2017-03-08 16:43:49 +0000 | [diff] [blame] | 588 | if (ret == 0) |
| 589 | btrfs_free_reserved_data_space_noquota(inode, |
| 590 | start, |
| 591 | end - start + 1); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 592 | goto free_pages_out; |
| 593 | } |
| 594 | } |
| 595 | |
| 596 | if (will_compress) { |
| 597 | /* |
| 598 | * we aren't doing an inline extent round the compressed size |
| 599 | * up to a block size boundary so the allocator does sane |
| 600 | * things |
| 601 | */ |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 602 | total_compressed = ALIGN(total_compressed, blocksize); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 603 | |
| 604 | /* |
| 605 | * one last check to make sure the compression is really a |
Timofey Titovets | 170607e | 2017-06-06 14:41:15 +0300 | [diff] [blame] | 606 | * win, compare the page count read with the blocks on disk, |
| 607 | * compression must free at least one sector size |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 608 | */ |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 609 | total_in = ALIGN(total_in, PAGE_SIZE); |
Timofey Titovets | 170607e | 2017-06-06 14:41:15 +0300 | [diff] [blame] | 610 | if (total_compressed + blocksize <= total_in) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 611 | num_bytes = total_in; |
Ashish Samant | c8bb0c8 | 2016-03-25 19:01:33 -0700 | [diff] [blame] | 612 | *num_added += 1; |
| 613 | |
| 614 | /* |
| 615 | * The async work queues will take care of doing actual |
| 616 | * allocation on disk for these compressed pages, and |
| 617 | * will submit them to the elevator. |
| 618 | */ |
| 619 | add_async_extent(async_cow, start, num_bytes, |
David Sterba | 4d3a800 | 2017-02-14 19:04:07 +0100 | [diff] [blame] | 620 | total_compressed, pages, nr_pages, |
Ashish Samant | c8bb0c8 | 2016-03-25 19:01:33 -0700 | [diff] [blame] | 621 | compress_type); |
| 622 | |
| 623 | if (start + num_bytes < end) { |
| 624 | start += num_bytes; |
| 625 | pages = NULL; |
| 626 | cond_resched(); |
| 627 | goto again; |
| 628 | } |
| 629 | return; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 630 | } |
| 631 | } |
Ashish Samant | c8bb0c8 | 2016-03-25 19:01:33 -0700 | [diff] [blame] | 632 | if (pages) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 633 | /* |
| 634 | * the compression code ran but failed to make things smaller, |
| 635 | * free any pages it allocated and our page pointer array |
| 636 | */ |
David Sterba | 4d3a800 | 2017-02-14 19:04:07 +0100 | [diff] [blame] | 637 | for (i = 0; i < nr_pages; i++) { |
Chris Mason | 70b99e6 | 2008-10-31 12:46:39 -0400 | [diff] [blame] | 638 | WARN_ON(pages[i]->mapping); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 639 | put_page(pages[i]); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 640 | } |
| 641 | kfree(pages); |
| 642 | pages = NULL; |
| 643 | total_compressed = 0; |
David Sterba | 4d3a800 | 2017-02-14 19:04:07 +0100 | [diff] [blame] | 644 | nr_pages = 0; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 645 | |
| 646 | /* flag the file so we don't compress in the future */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 647 | if (!btrfs_test_opt(fs_info, FORCE_COMPRESS) && |
Chris Mason | 1e701a3 | 2010-03-11 09:42:04 -0500 | [diff] [blame] | 648 | !(BTRFS_I(inode)->force_compress)) { |
Chris Mason | a555f81 | 2010-01-28 16:18:15 -0500 | [diff] [blame] | 649 | BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS; |
Chris Mason | 1e701a3 | 2010-03-11 09:42:04 -0500 | [diff] [blame] | 650 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 651 | } |
Chris Mason | f03d9301f | 2009-02-04 09:31:06 -0500 | [diff] [blame] | 652 | cleanup_and_bail_uncompressed: |
Ashish Samant | c8bb0c8 | 2016-03-25 19:01:33 -0700 | [diff] [blame] | 653 | /* |
| 654 | * No compression, but we still need to write the pages in the file |
| 655 | * we've been given so far. redirty the locked page if it corresponds |
| 656 | * to our extent and set things up for the async work queue to run |
| 657 | * cow_file_range to do the normal delalloc dance. |
| 658 | */ |
| 659 | if (page_offset(locked_page) >= start && |
| 660 | page_offset(locked_page) <= end) |
| 661 | __set_page_dirty_nobuffers(locked_page); |
| 662 | /* unlocked later on in the async handlers */ |
| 663 | |
| 664 | if (redirty) |
| 665 | extent_range_redirty_for_io(inode, start, end); |
| 666 | add_async_extent(async_cow, start, end - start + 1, 0, NULL, 0, |
| 667 | BTRFS_COMPRESS_NONE); |
| 668 | *num_added += 1; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 669 | |
Filipe Manana | c44f649 | 2014-10-09 21:15:44 +0100 | [diff] [blame] | 670 | return; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 671 | |
| 672 | free_pages_out: |
David Sterba | 4d3a800 | 2017-02-14 19:04:07 +0100 | [diff] [blame] | 673 | for (i = 0; i < nr_pages; i++) { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 674 | WARN_ON(pages[i]->mapping); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 675 | put_page(pages[i]); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 676 | } |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 677 | kfree(pages); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 678 | } |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 679 | |
Filipe Manana | 40ae837 | 2014-10-06 22:14:24 +0100 | [diff] [blame] | 680 | static void free_async_extent_pages(struct async_extent *async_extent) |
| 681 | { |
| 682 | int i; |
| 683 | |
| 684 | if (!async_extent->pages) |
| 685 | return; |
| 686 | |
| 687 | for (i = 0; i < async_extent->nr_pages; i++) { |
| 688 | WARN_ON(async_extent->pages[i]->mapping); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 689 | put_page(async_extent->pages[i]); |
Filipe Manana | 40ae837 | 2014-10-06 22:14:24 +0100 | [diff] [blame] | 690 | } |
| 691 | kfree(async_extent->pages); |
| 692 | async_extent->nr_pages = 0; |
| 693 | async_extent->pages = NULL; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 694 | } |
| 695 | |
| 696 | /* |
| 697 | * phase two of compressed writeback. This is the ordered portion |
| 698 | * of the code, which only gets called in the order the work was |
| 699 | * queued. We walk all the async extents created by compress_file_range |
| 700 | * and send them down to the disk. |
| 701 | */ |
Filipe Manana | dec8f17 | 2014-10-06 22:14:26 +0100 | [diff] [blame] | 702 | static noinline void submit_compressed_extents(struct inode *inode, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 703 | struct async_cow *async_cow) |
| 704 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 705 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 706 | struct async_extent *async_extent; |
| 707 | u64 alloc_hint = 0; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 708 | struct btrfs_key ins; |
| 709 | struct extent_map *em; |
| 710 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 711 | struct extent_io_tree *io_tree; |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 712 | int ret = 0; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 713 | |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 714 | again: |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 715 | while (!list_empty(&async_cow->extents)) { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 716 | async_extent = list_entry(async_cow->extents.next, |
| 717 | struct async_extent, list); |
| 718 | list_del(&async_extent->list); |
| 719 | |
| 720 | io_tree = &BTRFS_I(inode)->io_tree; |
| 721 | |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 722 | retry: |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 723 | /* did the compression code fall back to uncompressed IO? */ |
| 724 | if (!async_extent->pages) { |
| 725 | int page_started = 0; |
| 726 | unsigned long nr_written = 0; |
| 727 | |
| 728 | lock_extent(io_tree, async_extent->start, |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 729 | async_extent->start + |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 730 | async_extent->ram_size - 1); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 731 | |
| 732 | /* allocate blocks */ |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 733 | ret = cow_file_range(inode, async_cow->locked_page, |
| 734 | async_extent->start, |
| 735 | async_extent->start + |
| 736 | async_extent->ram_size - 1, |
Wang Xiaoguang | dda3245 | 2016-07-11 11:05:29 +0800 | [diff] [blame] | 737 | async_extent->start + |
| 738 | async_extent->ram_size - 1, |
| 739 | &page_started, &nr_written, 0, |
| 740 | NULL); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 741 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 742 | /* JDM XXX */ |
| 743 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 744 | /* |
| 745 | * if page_started, cow_file_range inserted an |
| 746 | * inline extent and took care of all the unlocking |
| 747 | * and IO for us. Otherwise, we need to submit |
| 748 | * all those pages down to the drive. |
| 749 | */ |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 750 | if (!page_started && !ret) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 751 | extent_write_locked_range(io_tree, |
| 752 | inode, async_extent->start, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 753 | async_extent->start + |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 754 | async_extent->ram_size - 1, |
| 755 | btrfs_get_extent, |
| 756 | WB_SYNC_ALL); |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 757 | else if (ret) |
| 758 | unlock_page(async_cow->locked_page); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 759 | kfree(async_extent); |
| 760 | cond_resched(); |
| 761 | continue; |
| 762 | } |
| 763 | |
| 764 | lock_extent(io_tree, async_extent->start, |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 765 | async_extent->start + async_extent->ram_size - 1); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 766 | |
Wang Xiaoguang | 1851309 | 2016-07-25 15:51:40 +0800 | [diff] [blame] | 767 | ret = btrfs_reserve_extent(root, async_extent->ram_size, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 768 | async_extent->compressed_size, |
| 769 | async_extent->compressed_size, |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 770 | 0, alloc_hint, &ins, 1, 1); |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 771 | if (ret) { |
Filipe Manana | 40ae837 | 2014-10-06 22:14:24 +0100 | [diff] [blame] | 772 | free_async_extent_pages(async_extent); |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 773 | |
Josef Bacik | fdf8e2e | 2013-06-14 16:58:23 -0400 | [diff] [blame] | 774 | if (ret == -ENOSPC) { |
| 775 | unlock_extent(io_tree, async_extent->start, |
| 776 | async_extent->start + |
| 777 | async_extent->ram_size - 1); |
Liu Bo | ce62003 | 2014-07-24 22:48:05 +0800 | [diff] [blame] | 778 | |
| 779 | /* |
| 780 | * we need to redirty the pages if we decide to |
| 781 | * fallback to uncompressed IO, otherwise we |
| 782 | * will not submit these pages down to lower |
| 783 | * layers. |
| 784 | */ |
| 785 | extent_range_redirty_for_io(inode, |
| 786 | async_extent->start, |
| 787 | async_extent->start + |
| 788 | async_extent->ram_size - 1); |
| 789 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 790 | goto retry; |
Josef Bacik | fdf8e2e | 2013-06-14 16:58:23 -0400 | [diff] [blame] | 791 | } |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 792 | goto out_free; |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 793 | } |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 794 | /* |
| 795 | * here we're doing allocation and writeback of the |
| 796 | * compressed pages |
| 797 | */ |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 798 | em = create_io_em(inode, async_extent->start, |
| 799 | async_extent->ram_size, /* len */ |
| 800 | async_extent->start, /* orig_start */ |
| 801 | ins.objectid, /* block_start */ |
| 802 | ins.offset, /* block_len */ |
| 803 | ins.offset, /* orig_block_len */ |
| 804 | async_extent->ram_size, /* ram_bytes */ |
| 805 | async_extent->compress_type, |
| 806 | BTRFS_ORDERED_COMPRESSED); |
| 807 | if (IS_ERR(em)) |
| 808 | /* ret value is not necessary due to void function */ |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 809 | goto out_free_reserve; |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 810 | free_extent_map(em); |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 811 | |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 812 | ret = btrfs_add_ordered_extent_compress(inode, |
| 813 | async_extent->start, |
| 814 | ins.objectid, |
| 815 | async_extent->ram_size, |
| 816 | ins.offset, |
| 817 | BTRFS_ORDERED_COMPRESSED, |
| 818 | async_extent->compress_type); |
Filipe Manana | d9f8596 | 2014-08-25 10:43:00 +0100 | [diff] [blame] | 819 | if (ret) { |
Nikolay Borisov | dcdbc05 | 2017-02-20 13:50:45 +0200 | [diff] [blame] | 820 | btrfs_drop_extent_cache(BTRFS_I(inode), |
| 821 | async_extent->start, |
Filipe Manana | d9f8596 | 2014-08-25 10:43:00 +0100 | [diff] [blame] | 822 | async_extent->start + |
| 823 | async_extent->ram_size - 1, 0); |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 824 | goto out_free_reserve; |
Filipe Manana | d9f8596 | 2014-08-25 10:43:00 +0100 | [diff] [blame] | 825 | } |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 826 | btrfs_dec_block_group_reservations(fs_info, ins.objectid); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 827 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 828 | /* |
| 829 | * clear dirty, set writeback and unlock the pages. |
| 830 | */ |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 831 | extent_clear_unlock_delalloc(inode, async_extent->start, |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 832 | async_extent->start + |
| 833 | async_extent->ram_size - 1, |
Qu Wenruo | ba8b04c | 2016-07-19 16:50:36 +0800 | [diff] [blame] | 834 | async_extent->start + |
| 835 | async_extent->ram_size - 1, |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 836 | NULL, EXTENT_LOCKED | EXTENT_DELALLOC, |
| 837 | PAGE_UNLOCK | PAGE_CLEAR_DIRTY | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 838 | PAGE_SET_WRITEBACK); |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 839 | if (btrfs_submit_compressed_write(inode, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 840 | async_extent->start, |
| 841 | async_extent->ram_size, |
| 842 | ins.objectid, |
| 843 | ins.offset, async_extent->pages, |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 844 | async_extent->nr_pages)) { |
Filipe Manana | fce2a4e | 2014-10-06 22:14:23 +0100 | [diff] [blame] | 845 | struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree; |
| 846 | struct page *p = async_extent->pages[0]; |
| 847 | const u64 start = async_extent->start; |
| 848 | const u64 end = start + async_extent->ram_size - 1; |
| 849 | |
| 850 | p->mapping = inode->i_mapping; |
| 851 | tree->ops->writepage_end_io_hook(p, start, end, |
| 852 | NULL, 0); |
| 853 | p->mapping = NULL; |
Qu Wenruo | ba8b04c | 2016-07-19 16:50:36 +0800 | [diff] [blame] | 854 | extent_clear_unlock_delalloc(inode, start, end, end, |
| 855 | NULL, 0, |
Filipe Manana | fce2a4e | 2014-10-06 22:14:23 +0100 | [diff] [blame] | 856 | PAGE_END_WRITEBACK | |
| 857 | PAGE_SET_ERROR); |
Filipe Manana | 40ae837 | 2014-10-06 22:14:24 +0100 | [diff] [blame] | 858 | free_async_extent_pages(async_extent); |
Filipe Manana | fce2a4e | 2014-10-06 22:14:23 +0100 | [diff] [blame] | 859 | } |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 860 | alloc_hint = ins.objectid + ins.offset; |
| 861 | kfree(async_extent); |
| 862 | cond_resched(); |
| 863 | } |
Filipe Manana | dec8f17 | 2014-10-06 22:14:26 +0100 | [diff] [blame] | 864 | return; |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 865 | out_free_reserve: |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 866 | btrfs_dec_block_group_reservations(fs_info, ins.objectid); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 867 | btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 868 | out_free: |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 869 | extent_clear_unlock_delalloc(inode, async_extent->start, |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 870 | async_extent->start + |
| 871 | async_extent->ram_size - 1, |
Qu Wenruo | ba8b04c | 2016-07-19 16:50:36 +0800 | [diff] [blame] | 872 | async_extent->start + |
| 873 | async_extent->ram_size - 1, |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 874 | NULL, EXTENT_LOCKED | EXTENT_DELALLOC | |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 875 | EXTENT_DELALLOC_NEW | |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 876 | EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING, |
| 877 | PAGE_UNLOCK | PAGE_CLEAR_DIRTY | |
Filipe Manana | 704de49 | 2014-10-06 22:14:22 +0100 | [diff] [blame] | 878 | PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK | |
| 879 | PAGE_SET_ERROR); |
Filipe Manana | 40ae837 | 2014-10-06 22:14:24 +0100 | [diff] [blame] | 880 | free_async_extent_pages(async_extent); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 881 | kfree(async_extent); |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 882 | goto again; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 883 | } |
| 884 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 885 | static u64 get_extent_allocation_hint(struct inode *inode, u64 start, |
| 886 | u64 num_bytes) |
| 887 | { |
| 888 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
| 889 | struct extent_map *em; |
| 890 | u64 alloc_hint = 0; |
| 891 | |
| 892 | read_lock(&em_tree->lock); |
| 893 | em = search_extent_mapping(em_tree, start, num_bytes); |
| 894 | if (em) { |
| 895 | /* |
| 896 | * if block start isn't an actual block number then find the |
| 897 | * first block in this inode and use that as a hint. If that |
| 898 | * block is also bogus then just don't worry about it. |
| 899 | */ |
| 900 | if (em->block_start >= EXTENT_MAP_LAST_BYTE) { |
| 901 | free_extent_map(em); |
| 902 | em = search_extent_mapping(em_tree, 0, 0); |
| 903 | if (em && em->block_start < EXTENT_MAP_LAST_BYTE) |
| 904 | alloc_hint = em->block_start; |
| 905 | if (em) |
| 906 | free_extent_map(em); |
| 907 | } else { |
| 908 | alloc_hint = em->block_start; |
| 909 | free_extent_map(em); |
| 910 | } |
| 911 | } |
| 912 | read_unlock(&em_tree->lock); |
| 913 | |
| 914 | return alloc_hint; |
| 915 | } |
| 916 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 917 | /* |
| 918 | * when extent_io.c finds a delayed allocation range in the file, |
| 919 | * the call backs end up in this code. The basic idea is to |
| 920 | * allocate extents on disk for the range, and create ordered data structs |
| 921 | * in ram to track those extents. |
| 922 | * |
| 923 | * locked_page is the page that writepage had locked already. We use |
| 924 | * it to make sure we don't do extra locks or unlocks. |
| 925 | * |
| 926 | * *page_started is set to one if we unlock locked_page and do everything |
| 927 | * required to start IO on it. It may be clean and already done with |
| 928 | * IO when we return. |
| 929 | */ |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 930 | static noinline int cow_file_range(struct inode *inode, |
| 931 | struct page *locked_page, |
Wang Xiaoguang | dda3245 | 2016-07-11 11:05:29 +0800 | [diff] [blame] | 932 | u64 start, u64 end, u64 delalloc_end, |
| 933 | int *page_started, unsigned long *nr_written, |
| 934 | int unlock, struct btrfs_dedupe_hash *hash) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 935 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 936 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 937 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 938 | u64 alloc_hint = 0; |
| 939 | u64 num_bytes; |
| 940 | unsigned long ram_size; |
| 941 | u64 disk_num_bytes; |
Filipe Manana | a315e68 | 2017-03-06 23:04:20 +0000 | [diff] [blame] | 942 | u64 cur_alloc_size = 0; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 943 | u64 blocksize = fs_info->sectorsize; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 944 | struct btrfs_key ins; |
| 945 | struct extent_map *em; |
Filipe Manana | a315e68 | 2017-03-06 23:04:20 +0000 | [diff] [blame] | 946 | unsigned clear_bits; |
| 947 | unsigned long page_ops; |
| 948 | bool extent_reserved = false; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 949 | int ret = 0; |
| 950 | |
Nikolay Borisov | 70ddc55 | 2017-02-20 13:50:35 +0200 | [diff] [blame] | 951 | if (btrfs_is_free_space_inode(BTRFS_I(inode))) { |
Josef Bacik | 02ecd2c | 2013-10-25 16:19:08 -0400 | [diff] [blame] | 952 | WARN_ON_ONCE(1); |
Josef Bacik | 29bce2f | 2014-02-07 12:21:23 -0500 | [diff] [blame] | 953 | ret = -EINVAL; |
| 954 | goto out_unlock; |
Josef Bacik | 02ecd2c | 2013-10-25 16:19:08 -0400 | [diff] [blame] | 955 | } |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 956 | |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 957 | num_bytes = ALIGN(end - start + 1, blocksize); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 958 | num_bytes = max(blocksize, num_bytes); |
| 959 | disk_num_bytes = num_bytes; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 960 | |
Nikolay Borisov | 6158e1c | 2017-02-20 13:50:43 +0200 | [diff] [blame] | 961 | inode_should_defrag(BTRFS_I(inode), start, end, num_bytes, SZ_64K); |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 962 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 963 | if (start == 0) { |
| 964 | /* lets try to make an inline extent */ |
Anand Jain | f74670f | 2016-12-06 12:43:07 +0800 | [diff] [blame] | 965 | ret = cow_file_range_inline(root, inode, start, end, 0, |
| 966 | BTRFS_COMPRESS_NONE, NULL); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 967 | if (ret == 0) { |
Qu Wenruo | ba8b04c | 2016-07-19 16:50:36 +0800 | [diff] [blame] | 968 | extent_clear_unlock_delalloc(inode, start, end, |
| 969 | delalloc_end, NULL, |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 970 | EXTENT_LOCKED | EXTENT_DELALLOC | |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 971 | EXTENT_DELALLOC_NEW | |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 972 | EXTENT_DEFRAG, PAGE_UNLOCK | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 973 | PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK | |
| 974 | PAGE_END_WRITEBACK); |
Wang Xiaoguang | 1851309 | 2016-07-25 15:51:40 +0800 | [diff] [blame] | 975 | btrfs_free_reserved_data_space_noquota(inode, start, |
| 976 | end - start + 1); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 977 | *nr_written = *nr_written + |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 978 | (end - start + PAGE_SIZE) / PAGE_SIZE; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 979 | *page_started = 1; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 980 | goto out; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 981 | } else if (ret < 0) { |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 982 | goto out_unlock; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 983 | } |
| 984 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 985 | |
| 986 | BUG_ON(disk_num_bytes > |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 987 | btrfs_super_total_bytes(fs_info->super_copy)); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 988 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 989 | alloc_hint = get_extent_allocation_hint(inode, start, num_bytes); |
Nikolay Borisov | dcdbc05 | 2017-02-20 13:50:45 +0200 | [diff] [blame] | 990 | btrfs_drop_extent_cache(BTRFS_I(inode), start, |
| 991 | start + num_bytes - 1, 0); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 992 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 993 | while (disk_num_bytes > 0) { |
Josef Bacik | 287a0ab | 2010-03-19 18:07:23 +0000 | [diff] [blame] | 994 | cur_alloc_size = disk_num_bytes; |
Wang Xiaoguang | 1851309 | 2016-07-25 15:51:40 +0800 | [diff] [blame] | 995 | ret = btrfs_reserve_extent(root, cur_alloc_size, cur_alloc_size, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 996 | fs_info->sectorsize, 0, alloc_hint, |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 997 | &ins, 1, 1); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 998 | if (ret < 0) |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 999 | goto out_unlock; |
Filipe Manana | a315e68 | 2017-03-06 23:04:20 +0000 | [diff] [blame] | 1000 | cur_alloc_size = ins.offset; |
| 1001 | extent_reserved = true; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1002 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1003 | ram_size = ins.offset; |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 1004 | em = create_io_em(inode, start, ins.offset, /* len */ |
| 1005 | start, /* orig_start */ |
| 1006 | ins.objectid, /* block_start */ |
| 1007 | ins.offset, /* block_len */ |
| 1008 | ins.offset, /* orig_block_len */ |
| 1009 | ram_size, /* ram_bytes */ |
| 1010 | BTRFS_COMPRESS_NONE, /* compress_type */ |
Liu Bo | 1af4a0a | 2017-02-13 15:35:09 -0800 | [diff] [blame] | 1011 | BTRFS_ORDERED_REGULAR /* type */); |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 1012 | if (IS_ERR(em)) |
Liu Bo | ace68ba | 2013-04-22 10:53:47 +0000 | [diff] [blame] | 1013 | goto out_reserve; |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 1014 | free_extent_map(em); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1015 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1016 | ret = btrfs_add_ordered_extent(inode, start, ins.objectid, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1017 | ram_size, cur_alloc_size, 0); |
Liu Bo | ace68ba | 2013-04-22 10:53:47 +0000 | [diff] [blame] | 1018 | if (ret) |
Filipe Manana | d9f8596 | 2014-08-25 10:43:00 +0100 | [diff] [blame] | 1019 | goto out_drop_extent_cache; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1020 | |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1021 | if (root->root_key.objectid == |
| 1022 | BTRFS_DATA_RELOC_TREE_OBJECTID) { |
| 1023 | ret = btrfs_reloc_clone_csums(inode, start, |
| 1024 | cur_alloc_size); |
Qu Wenruo | 4dbd80f | 2017-03-08 10:25:51 +0800 | [diff] [blame] | 1025 | /* |
| 1026 | * Only drop cache here, and process as normal. |
| 1027 | * |
| 1028 | * We must not allow extent_clear_unlock_delalloc() |
| 1029 | * at out_unlock label to free meta of this ordered |
| 1030 | * extent, as its meta should be freed by |
| 1031 | * btrfs_finish_ordered_io(). |
| 1032 | * |
| 1033 | * So we must continue until @start is increased to |
| 1034 | * skip current ordered extent. |
| 1035 | */ |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 1036 | if (ret) |
Qu Wenruo | 4dbd80f | 2017-03-08 10:25:51 +0800 | [diff] [blame] | 1037 | btrfs_drop_extent_cache(BTRFS_I(inode), start, |
| 1038 | start + ram_size - 1, 0); |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1039 | } |
| 1040 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1041 | btrfs_dec_block_group_reservations(fs_info, ins.objectid); |
Filipe Manana | 9cfa3e3 | 2016-04-26 15:39:32 +0100 | [diff] [blame] | 1042 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1043 | /* we're not doing compressed IO, don't unlock the first |
| 1044 | * page (which the caller expects to stay locked), don't |
| 1045 | * clear any dirty bits and don't set any writeback bits |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 1046 | * |
| 1047 | * Do set the Private2 bit so we know this page was properly |
| 1048 | * setup for writepage |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1049 | */ |
Filipe Manana | a315e68 | 2017-03-06 23:04:20 +0000 | [diff] [blame] | 1050 | page_ops = unlock ? PAGE_UNLOCK : 0; |
| 1051 | page_ops |= PAGE_SET_PRIVATE2; |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 1052 | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1053 | extent_clear_unlock_delalloc(inode, start, |
Qu Wenruo | ba8b04c | 2016-07-19 16:50:36 +0800 | [diff] [blame] | 1054 | start + ram_size - 1, |
| 1055 | delalloc_end, locked_page, |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1056 | EXTENT_LOCKED | EXTENT_DELALLOC, |
Filipe Manana | a315e68 | 2017-03-06 23:04:20 +0000 | [diff] [blame] | 1057 | page_ops); |
Qu Wenruo | 4dbd80f | 2017-03-08 10:25:51 +0800 | [diff] [blame] | 1058 | if (disk_num_bytes < cur_alloc_size) |
| 1059 | disk_num_bytes = 0; |
| 1060 | else |
| 1061 | disk_num_bytes -= cur_alloc_size; |
Chris Mason | c59f895 | 2007-12-17 20:14:04 -0500 | [diff] [blame] | 1062 | num_bytes -= cur_alloc_size; |
| 1063 | alloc_hint = ins.objectid + ins.offset; |
| 1064 | start += cur_alloc_size; |
Filipe Manana | a315e68 | 2017-03-06 23:04:20 +0000 | [diff] [blame] | 1065 | extent_reserved = false; |
Qu Wenruo | 4dbd80f | 2017-03-08 10:25:51 +0800 | [diff] [blame] | 1066 | |
| 1067 | /* |
| 1068 | * btrfs_reloc_clone_csums() error, since start is increased |
| 1069 | * extent_clear_unlock_delalloc() at out_unlock label won't |
| 1070 | * free metadata of current ordered extent, we're OK to exit. |
| 1071 | */ |
| 1072 | if (ret) |
| 1073 | goto out_unlock; |
Chris Mason | b888db2b | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 1074 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1075 | out: |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1076 | return ret; |
Miao Xie | b7d5b0a | 2012-11-01 07:32:18 +0000 | [diff] [blame] | 1077 | |
Filipe Manana | d9f8596 | 2014-08-25 10:43:00 +0100 | [diff] [blame] | 1078 | out_drop_extent_cache: |
Nikolay Borisov | dcdbc05 | 2017-02-20 13:50:45 +0200 | [diff] [blame] | 1079 | 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] | 1080 | out_reserve: |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1081 | btrfs_dec_block_group_reservations(fs_info, ins.objectid); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 1082 | btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1083 | out_unlock: |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 1084 | clear_bits = EXTENT_LOCKED | EXTENT_DELALLOC | EXTENT_DELALLOC_NEW | |
| 1085 | EXTENT_DEFRAG | EXTENT_CLEAR_META_RESV; |
Filipe Manana | a315e68 | 2017-03-06 23:04:20 +0000 | [diff] [blame] | 1086 | page_ops = PAGE_UNLOCK | PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK | |
| 1087 | PAGE_END_WRITEBACK; |
| 1088 | /* |
| 1089 | * If we reserved an extent for our delalloc range (or a subrange) and |
| 1090 | * failed to create the respective ordered extent, then it means that |
| 1091 | * when we reserved the extent we decremented the extent's size from |
| 1092 | * the data space_info's bytes_may_use counter and incremented the |
| 1093 | * space_info's bytes_reserved counter by the same amount. We must make |
| 1094 | * sure extent_clear_unlock_delalloc() does not try to decrement again |
| 1095 | * the data space_info's bytes_may_use counter, therefore we do not pass |
| 1096 | * it the flag EXTENT_CLEAR_DATA_RESV. |
| 1097 | */ |
| 1098 | if (extent_reserved) { |
| 1099 | extent_clear_unlock_delalloc(inode, start, |
| 1100 | start + cur_alloc_size, |
| 1101 | start + cur_alloc_size, |
| 1102 | locked_page, |
| 1103 | clear_bits, |
| 1104 | page_ops); |
| 1105 | start += cur_alloc_size; |
| 1106 | if (start >= end) |
| 1107 | goto out; |
| 1108 | } |
Qu Wenruo | ba8b04c | 2016-07-19 16:50:36 +0800 | [diff] [blame] | 1109 | extent_clear_unlock_delalloc(inode, start, end, delalloc_end, |
| 1110 | locked_page, |
Filipe Manana | a315e68 | 2017-03-06 23:04:20 +0000 | [diff] [blame] | 1111 | clear_bits | EXTENT_CLEAR_DATA_RESV, |
| 1112 | page_ops); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1113 | goto out; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1114 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1115 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1116 | /* |
| 1117 | * work queue call back to started compression on a file and pages |
| 1118 | */ |
| 1119 | static noinline void async_cow_start(struct btrfs_work *work) |
| 1120 | { |
| 1121 | struct async_cow *async_cow; |
| 1122 | int num_added = 0; |
| 1123 | async_cow = container_of(work, struct async_cow, work); |
| 1124 | |
| 1125 | compress_file_range(async_cow->inode, async_cow->locked_page, |
| 1126 | async_cow->start, async_cow->end, async_cow, |
| 1127 | &num_added); |
Josef Bacik | 8180ef8 | 2012-06-08 15:16:12 -0400 | [diff] [blame] | 1128 | if (num_added == 0) { |
Josef Bacik | cb77fcd | 2012-06-15 12:19:48 -0600 | [diff] [blame] | 1129 | btrfs_add_delayed_iput(async_cow->inode); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1130 | async_cow->inode = NULL; |
Josef Bacik | 8180ef8 | 2012-06-08 15:16:12 -0400 | [diff] [blame] | 1131 | } |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1132 | } |
| 1133 | |
| 1134 | /* |
| 1135 | * work queue call back to submit previously compressed pages |
| 1136 | */ |
| 1137 | static noinline void async_cow_submit(struct btrfs_work *work) |
| 1138 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1139 | struct btrfs_fs_info *fs_info; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1140 | struct async_cow *async_cow; |
| 1141 | struct btrfs_root *root; |
| 1142 | unsigned long nr_pages; |
| 1143 | |
| 1144 | async_cow = container_of(work, struct async_cow, work); |
| 1145 | |
| 1146 | root = async_cow->root; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1147 | fs_info = root->fs_info; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1148 | nr_pages = (async_cow->end - async_cow->start + PAGE_SIZE) >> |
| 1149 | PAGE_SHIFT; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1150 | |
David Sterba | ee86395 | 2015-02-16 19:41:40 +0100 | [diff] [blame] | 1151 | /* |
| 1152 | * atomic_sub_return implies a barrier for waitqueue_active |
| 1153 | */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1154 | if (atomic_sub_return(nr_pages, &fs_info->async_delalloc_pages) < |
Byongho Lee | ee22184 | 2015-12-15 01:42:10 +0900 | [diff] [blame] | 1155 | 5 * SZ_1M && |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1156 | waitqueue_active(&fs_info->async_submit_wait)) |
| 1157 | wake_up(&fs_info->async_submit_wait); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1158 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1159 | if (async_cow->inode) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1160 | submit_compressed_extents(async_cow->inode, async_cow); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1161 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1162 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1163 | static noinline void async_cow_free(struct btrfs_work *work) |
| 1164 | { |
| 1165 | struct async_cow *async_cow; |
| 1166 | async_cow = container_of(work, struct async_cow, work); |
Josef Bacik | 8180ef8 | 2012-06-08 15:16:12 -0400 | [diff] [blame] | 1167 | if (async_cow->inode) |
Josef Bacik | cb77fcd | 2012-06-15 12:19:48 -0600 | [diff] [blame] | 1168 | btrfs_add_delayed_iput(async_cow->inode); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1169 | kfree(async_cow); |
| 1170 | } |
| 1171 | |
| 1172 | static int cow_file_range_async(struct inode *inode, struct page *locked_page, |
| 1173 | u64 start, u64 end, int *page_started, |
| 1174 | unsigned long *nr_written) |
| 1175 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1176 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1177 | struct async_cow *async_cow; |
| 1178 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 1179 | unsigned long nr_pages; |
| 1180 | u64 cur_end; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1181 | |
Chris Mason | a3429ab | 2009-10-08 12:30:20 -0400 | [diff] [blame] | 1182 | clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED, |
| 1183 | 1, 0, NULL, GFP_NOFS); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1184 | while (start < end) { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1185 | async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1186 | BUG_ON(!async_cow); /* -ENOMEM */ |
Josef Bacik | 8180ef8 | 2012-06-08 15:16:12 -0400 | [diff] [blame] | 1187 | async_cow->inode = igrab(inode); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1188 | async_cow->root = root; |
| 1189 | async_cow->locked_page = locked_page; |
| 1190 | async_cow->start = start; |
| 1191 | |
Wang Shilong | f79707b | 2014-07-17 11:44:09 +0800 | [diff] [blame] | 1192 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS && |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1193 | !btrfs_test_opt(fs_info, FORCE_COMPRESS)) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1194 | cur_end = end; |
| 1195 | else |
Byongho Lee | ee22184 | 2015-12-15 01:42:10 +0900 | [diff] [blame] | 1196 | cur_end = min(end, start + SZ_512K - 1); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1197 | |
| 1198 | async_cow->end = cur_end; |
| 1199 | INIT_LIST_HEAD(&async_cow->extents); |
| 1200 | |
Liu Bo | 9e0af23 | 2014-08-15 23:36:53 +0800 | [diff] [blame] | 1201 | btrfs_init_work(&async_cow->work, |
| 1202 | btrfs_delalloc_helper, |
| 1203 | async_cow_start, async_cow_submit, |
| 1204 | async_cow_free); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1205 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1206 | nr_pages = (cur_end - start + PAGE_SIZE) >> |
| 1207 | PAGE_SHIFT; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1208 | atomic_add(nr_pages, &fs_info->async_delalloc_pages); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1209 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1210 | btrfs_queue_work(fs_info->delalloc_workers, &async_cow->work); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1211 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1212 | while (atomic_read(&fs_info->async_submit_draining) && |
| 1213 | atomic_read(&fs_info->async_delalloc_pages)) { |
| 1214 | wait_event(fs_info->async_submit_wait, |
| 1215 | (atomic_read(&fs_info->async_delalloc_pages) == |
| 1216 | 0)); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1217 | } |
| 1218 | |
| 1219 | *nr_written += nr_pages; |
| 1220 | start = cur_end + 1; |
| 1221 | } |
| 1222 | *page_started = 1; |
| 1223 | return 0; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1224 | } |
| 1225 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 1226 | 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] | 1227 | u64 bytenr, u64 num_bytes) |
| 1228 | { |
| 1229 | int ret; |
| 1230 | struct btrfs_ordered_sum *sums; |
| 1231 | LIST_HEAD(list); |
| 1232 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1233 | ret = btrfs_lookup_csums_range(fs_info->csum_root, bytenr, |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 1234 | bytenr + num_bytes - 1, &list, 0); |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1235 | if (ret == 0 && list_empty(&list)) |
| 1236 | return 0; |
| 1237 | |
| 1238 | while (!list_empty(&list)) { |
| 1239 | sums = list_entry(list.next, struct btrfs_ordered_sum, list); |
| 1240 | list_del(&sums->list); |
| 1241 | kfree(sums); |
| 1242 | } |
| 1243 | return 1; |
| 1244 | } |
| 1245 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1246 | /* |
| 1247 | * when nowcow writeback call back. This checks for snapshots or COW copies |
| 1248 | * of the extents that exist in the file, and COWs the file as required. |
| 1249 | * |
| 1250 | * If no cow copies or snapshots exist, we write directly to the existing |
| 1251 | * blocks on disk |
| 1252 | */ |
Chris Mason | 7f366cf | 2009-03-12 20:12:45 -0400 | [diff] [blame] | 1253 | static noinline int run_delalloc_nocow(struct inode *inode, |
| 1254 | struct page *locked_page, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1255 | u64 start, u64 end, int *page_started, int force, |
| 1256 | unsigned long *nr_written) |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1257 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1258 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1259 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 1260 | struct extent_buffer *leaf; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1261 | struct btrfs_path *path; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1262 | struct btrfs_file_extent_item *fi; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1263 | struct btrfs_key found_key; |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 1264 | struct extent_map *em; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1265 | u64 cow_start; |
| 1266 | u64 cur_offset; |
| 1267 | u64 extent_end; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1268 | u64 extent_offset; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1269 | u64 disk_bytenr; |
| 1270 | u64 num_bytes; |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 1271 | u64 disk_num_bytes; |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 1272 | u64 ram_bytes; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1273 | int extent_type; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1274 | int ret, err; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1275 | int type; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1276 | int nocow; |
| 1277 | int check_prev = 1; |
Li Zefan | 82d5902 | 2011-04-20 10:33:24 +0800 | [diff] [blame] | 1278 | bool nolock; |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 1279 | u64 ino = btrfs_ino(BTRFS_I(inode)); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1280 | |
| 1281 | path = btrfs_alloc_path(); |
Josef Bacik | 17ca04a | 2012-05-31 15:58:55 -0400 | [diff] [blame] | 1282 | if (!path) { |
Qu Wenruo | ba8b04c | 2016-07-19 16:50:36 +0800 | [diff] [blame] | 1283 | extent_clear_unlock_delalloc(inode, start, end, end, |
| 1284 | locked_page, |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1285 | EXTENT_LOCKED | EXTENT_DELALLOC | |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 1286 | EXTENT_DO_ACCOUNTING | |
| 1287 | EXTENT_DEFRAG, PAGE_UNLOCK | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1288 | PAGE_CLEAR_DIRTY | |
| 1289 | PAGE_SET_WRITEBACK | |
| 1290 | PAGE_END_WRITEBACK); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 1291 | return -ENOMEM; |
Josef Bacik | 17ca04a | 2012-05-31 15:58:55 -0400 | [diff] [blame] | 1292 | } |
Li Zefan | 82d5902 | 2011-04-20 10:33:24 +0800 | [diff] [blame] | 1293 | |
Nikolay Borisov | 70ddc55 | 2017-02-20 13:50:35 +0200 | [diff] [blame] | 1294 | nolock = btrfs_is_free_space_inode(BTRFS_I(inode)); |
Li Zefan | 82d5902 | 2011-04-20 10:33:24 +0800 | [diff] [blame] | 1295 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1296 | cow_start = (u64)-1; |
| 1297 | cur_offset = start; |
| 1298 | while (1) { |
Liu Bo | e4c3b2d | 2017-01-30 12:25:28 -0800 | [diff] [blame] | 1299 | ret = btrfs_lookup_file_extent(NULL, root, path, ino, |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1300 | cur_offset, 0); |
Josef Bacik | d788a34 | 2013-10-25 16:55:08 -0400 | [diff] [blame] | 1301 | if (ret < 0) |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1302 | goto error; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1303 | if (ret > 0 && path->slots[0] > 0 && check_prev) { |
| 1304 | leaf = path->nodes[0]; |
| 1305 | btrfs_item_key_to_cpu(leaf, &found_key, |
| 1306 | path->slots[0] - 1); |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 1307 | if (found_key.objectid == ino && |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1308 | found_key.type == BTRFS_EXTENT_DATA_KEY) |
| 1309 | path->slots[0]--; |
| 1310 | } |
| 1311 | check_prev = 0; |
| 1312 | next_slot: |
| 1313 | leaf = path->nodes[0]; |
| 1314 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { |
| 1315 | ret = btrfs_next_leaf(root, path); |
Josef Bacik | d788a34 | 2013-10-25 16:55:08 -0400 | [diff] [blame] | 1316 | if (ret < 0) |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1317 | goto error; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1318 | if (ret > 0) |
| 1319 | break; |
| 1320 | leaf = path->nodes[0]; |
| 1321 | } |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1322 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1323 | nocow = 0; |
| 1324 | disk_bytenr = 0; |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1325 | num_bytes = 0; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1326 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1327 | |
Filipe Manana | 1d512cb | 2015-11-09 00:33:58 +0000 | [diff] [blame] | 1328 | if (found_key.objectid > ino) |
| 1329 | break; |
| 1330 | if (WARN_ON_ONCE(found_key.objectid < ino) || |
| 1331 | found_key.type < BTRFS_EXTENT_DATA_KEY) { |
| 1332 | path->slots[0]++; |
| 1333 | goto next_slot; |
| 1334 | } |
| 1335 | if (found_key.type > BTRFS_EXTENT_DATA_KEY || |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1336 | found_key.offset > end) |
| 1337 | break; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1338 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1339 | if (found_key.offset > cur_offset) { |
| 1340 | extent_end = found_key.offset; |
Chris Mason | e9061e2 | 2009-10-09 09:57:45 -0400 | [diff] [blame] | 1341 | extent_type = 0; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1342 | goto out_check; |
| 1343 | } |
Chris Mason | c31f883 | 2008-01-08 15:46:31 -0500 | [diff] [blame] | 1344 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1345 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 1346 | struct btrfs_file_extent_item); |
| 1347 | extent_type = btrfs_file_extent_type(leaf, fi); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1348 | |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 1349 | ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1350 | if (extent_type == BTRFS_FILE_EXTENT_REG || |
| 1351 | extent_type == BTRFS_FILE_EXTENT_PREALLOC) { |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1352 | disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1353 | extent_offset = btrfs_file_extent_offset(leaf, fi); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1354 | extent_end = found_key.offset + |
| 1355 | btrfs_file_extent_num_bytes(leaf, fi); |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 1356 | disk_num_bytes = |
| 1357 | btrfs_file_extent_disk_num_bytes(leaf, fi); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1358 | if (extent_end <= start) { |
| 1359 | path->slots[0]++; |
| 1360 | goto next_slot; |
| 1361 | } |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1362 | if (disk_bytenr == 0) |
| 1363 | goto out_check; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1364 | if (btrfs_file_extent_compression(leaf, fi) || |
| 1365 | btrfs_file_extent_encryption(leaf, fi) || |
| 1366 | btrfs_file_extent_other_encoding(leaf, fi)) |
| 1367 | goto out_check; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1368 | if (extent_type == BTRFS_FILE_EXTENT_REG && !force) |
| 1369 | goto out_check; |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 1370 | if (btrfs_extent_readonly(fs_info, disk_bytenr)) |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1371 | goto out_check; |
Liu Bo | e4c3b2d | 2017-01-30 12:25:28 -0800 | [diff] [blame] | 1372 | if (btrfs_cross_ref_exist(root, ino, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1373 | found_key.offset - |
| 1374 | extent_offset, disk_bytenr)) |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1375 | goto out_check; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1376 | disk_bytenr += extent_offset; |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1377 | disk_bytenr += cur_offset - found_key.offset; |
| 1378 | num_bytes = min(end + 1, extent_end) - cur_offset; |
| 1379 | /* |
Wang Shilong | e9894fd | 2014-03-27 11:12:25 +0800 | [diff] [blame] | 1380 | * if there are pending snapshots for this root, |
| 1381 | * we fall into common COW way. |
| 1382 | */ |
| 1383 | if (!nolock) { |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 1384 | err = btrfs_start_write_no_snapshoting(root); |
Wang Shilong | e9894fd | 2014-03-27 11:12:25 +0800 | [diff] [blame] | 1385 | if (!err) |
| 1386 | goto out_check; |
| 1387 | } |
| 1388 | /* |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1389 | * force cow if csum exists in the range. |
| 1390 | * this ensure that csum for a given extent are |
| 1391 | * either valid or do not exist. |
| 1392 | */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 1393 | if (csum_exist_in_range(fs_info, disk_bytenr, |
Robbie Ko | 91e1f56 | 2016-10-07 10:01:29 +0800 | [diff] [blame] | 1394 | num_bytes)) { |
| 1395 | if (!nolock) |
| 1396 | btrfs_end_write_no_snapshoting(root); |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1397 | goto out_check; |
Robbie Ko | 91e1f56 | 2016-10-07 10:01:29 +0800 | [diff] [blame] | 1398 | } |
| 1399 | if (!btrfs_inc_nocow_writers(fs_info, disk_bytenr)) { |
| 1400 | if (!nolock) |
| 1401 | btrfs_end_write_no_snapshoting(root); |
Filipe Manana | f78c436 | 2016-05-09 13:15:41 +0100 | [diff] [blame] | 1402 | goto out_check; |
Robbie Ko | 91e1f56 | 2016-10-07 10:01:29 +0800 | [diff] [blame] | 1403 | } |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1404 | nocow = 1; |
| 1405 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
| 1406 | extent_end = found_key.offset + |
Chris Mason | 514ac8a | 2014-01-03 21:07:00 -0800 | [diff] [blame] | 1407 | btrfs_file_extent_inline_len(leaf, |
| 1408 | path->slots[0], fi); |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 1409 | extent_end = ALIGN(extent_end, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1410 | fs_info->sectorsize); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1411 | } else { |
| 1412 | BUG_ON(1); |
| 1413 | } |
| 1414 | out_check: |
| 1415 | if (extent_end <= start) { |
| 1416 | path->slots[0]++; |
Wang Shilong | e9894fd | 2014-03-27 11:12:25 +0800 | [diff] [blame] | 1417 | if (!nolock && nocow) |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 1418 | btrfs_end_write_no_snapshoting(root); |
Filipe Manana | f78c436 | 2016-05-09 13:15:41 +0100 | [diff] [blame] | 1419 | if (nocow) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1420 | btrfs_dec_nocow_writers(fs_info, disk_bytenr); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1421 | goto next_slot; |
| 1422 | } |
| 1423 | if (!nocow) { |
| 1424 | if (cow_start == (u64)-1) |
| 1425 | cow_start = cur_offset; |
| 1426 | cur_offset = extent_end; |
| 1427 | if (cur_offset > end) |
| 1428 | break; |
| 1429 | path->slots[0]++; |
| 1430 | goto next_slot; |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1431 | } |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1432 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1433 | btrfs_release_path(path); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1434 | if (cow_start != (u64)-1) { |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 1435 | ret = cow_file_range(inode, locked_page, |
| 1436 | cow_start, found_key.offset - 1, |
Wang Xiaoguang | dda3245 | 2016-07-11 11:05:29 +0800 | [diff] [blame] | 1437 | end, page_started, nr_written, 1, |
| 1438 | NULL); |
Wang Shilong | e9894fd | 2014-03-27 11:12:25 +0800 | [diff] [blame] | 1439 | if (ret) { |
| 1440 | if (!nolock && nocow) |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 1441 | btrfs_end_write_no_snapshoting(root); |
Filipe Manana | f78c436 | 2016-05-09 13:15:41 +0100 | [diff] [blame] | 1442 | if (nocow) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1443 | btrfs_dec_nocow_writers(fs_info, |
Filipe Manana | f78c436 | 2016-05-09 13:15:41 +0100 | [diff] [blame] | 1444 | disk_bytenr); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1445 | goto error; |
Wang Shilong | e9894fd | 2014-03-27 11:12:25 +0800 | [diff] [blame] | 1446 | } |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1447 | cow_start = (u64)-1; |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1448 | } |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1449 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1450 | if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) { |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 1451 | u64 orig_start = found_key.offset - extent_offset; |
| 1452 | |
| 1453 | em = create_io_em(inode, cur_offset, num_bytes, |
| 1454 | orig_start, |
| 1455 | disk_bytenr, /* block_start */ |
| 1456 | num_bytes, /* block_len */ |
| 1457 | disk_num_bytes, /* orig_block_len */ |
| 1458 | ram_bytes, BTRFS_COMPRESS_NONE, |
| 1459 | BTRFS_ORDERED_PREALLOC); |
| 1460 | if (IS_ERR(em)) { |
| 1461 | if (!nolock && nocow) |
| 1462 | btrfs_end_write_no_snapshoting(root); |
| 1463 | if (nocow) |
| 1464 | btrfs_dec_nocow_writers(fs_info, |
| 1465 | disk_bytenr); |
| 1466 | ret = PTR_ERR(em); |
| 1467 | goto error; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1468 | } |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 1469 | free_extent_map(em); |
| 1470 | } |
| 1471 | |
| 1472 | if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) { |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1473 | type = BTRFS_ORDERED_PREALLOC; |
| 1474 | } else { |
| 1475 | type = BTRFS_ORDERED_NOCOW; |
| 1476 | } |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1477 | |
| 1478 | ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1479 | num_bytes, num_bytes, type); |
Filipe Manana | f78c436 | 2016-05-09 13:15:41 +0100 | [diff] [blame] | 1480 | if (nocow) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1481 | btrfs_dec_nocow_writers(fs_info, disk_bytenr); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1482 | BUG_ON(ret); /* -ENOMEM */ |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1483 | |
Yan, Zheng | efa5646 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 1484 | if (root->root_key.objectid == |
Qu Wenruo | 4dbd80f | 2017-03-08 10:25:51 +0800 | [diff] [blame] | 1485 | BTRFS_DATA_RELOC_TREE_OBJECTID) |
| 1486 | /* |
| 1487 | * Error handled later, as we must prevent |
| 1488 | * extent_clear_unlock_delalloc() in error handler |
| 1489 | * from freeing metadata of created ordered extent. |
| 1490 | */ |
Yan, Zheng | efa5646 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 1491 | ret = btrfs_reloc_clone_csums(inode, cur_offset, |
| 1492 | num_bytes); |
Yan, Zheng | efa5646 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 1493 | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1494 | extent_clear_unlock_delalloc(inode, cur_offset, |
Qu Wenruo | ba8b04c | 2016-07-19 16:50:36 +0800 | [diff] [blame] | 1495 | cur_offset + num_bytes - 1, end, |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1496 | locked_page, EXTENT_LOCKED | |
Wang Xiaoguang | 1851309 | 2016-07-25 15:51:40 +0800 | [diff] [blame] | 1497 | EXTENT_DELALLOC | |
| 1498 | EXTENT_CLEAR_DATA_RESV, |
| 1499 | PAGE_UNLOCK | PAGE_SET_PRIVATE2); |
| 1500 | |
Wang Shilong | e9894fd | 2014-03-27 11:12:25 +0800 | [diff] [blame] | 1501 | if (!nolock && nocow) |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 1502 | btrfs_end_write_no_snapshoting(root); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1503 | cur_offset = extent_end; |
Qu Wenruo | 4dbd80f | 2017-03-08 10:25:51 +0800 | [diff] [blame] | 1504 | |
| 1505 | /* |
| 1506 | * btrfs_reloc_clone_csums() error, now we're OK to call error |
| 1507 | * handler, as metadata for created ordered extent will only |
| 1508 | * be freed by btrfs_finish_ordered_io(). |
| 1509 | */ |
| 1510 | if (ret) |
| 1511 | goto error; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1512 | if (cur_offset > end) |
| 1513 | break; |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1514 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1515 | btrfs_release_path(path); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1516 | |
Josef Bacik | 17ca04a | 2012-05-31 15:58:55 -0400 | [diff] [blame] | 1517 | if (cur_offset <= end && cow_start == (u64)-1) { |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1518 | cow_start = cur_offset; |
Josef Bacik | 17ca04a | 2012-05-31 15:58:55 -0400 | [diff] [blame] | 1519 | cur_offset = end; |
| 1520 | } |
| 1521 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1522 | if (cow_start != (u64)-1) { |
Wang Xiaoguang | dda3245 | 2016-07-11 11:05:29 +0800 | [diff] [blame] | 1523 | ret = cow_file_range(inode, locked_page, cow_start, end, end, |
| 1524 | page_started, nr_written, 1, NULL); |
Josef Bacik | d788a34 | 2013-10-25 16:55:08 -0400 | [diff] [blame] | 1525 | if (ret) |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1526 | goto error; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1527 | } |
| 1528 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1529 | error: |
Josef Bacik | 17ca04a | 2012-05-31 15:58:55 -0400 | [diff] [blame] | 1530 | if (ret && cur_offset < end) |
Qu Wenruo | ba8b04c | 2016-07-19 16:50:36 +0800 | [diff] [blame] | 1531 | extent_clear_unlock_delalloc(inode, cur_offset, end, end, |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1532 | locked_page, EXTENT_LOCKED | |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 1533 | EXTENT_DELALLOC | EXTENT_DEFRAG | |
| 1534 | EXTENT_DO_ACCOUNTING, PAGE_UNLOCK | |
| 1535 | PAGE_CLEAR_DIRTY | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1536 | PAGE_SET_WRITEBACK | |
| 1537 | PAGE_END_WRITEBACK); |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1538 | btrfs_free_path(path); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1539 | return ret; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1540 | } |
| 1541 | |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 1542 | static inline int need_force_cow(struct inode *inode, u64 start, u64 end) |
| 1543 | { |
| 1544 | |
| 1545 | if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) && |
| 1546 | !(BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC)) |
| 1547 | return 0; |
| 1548 | |
| 1549 | /* |
| 1550 | * @defrag_bytes is a hint value, no spinlock held here, |
| 1551 | * if is not zero, it means the file is defragging. |
| 1552 | * Force cow if given extent needs to be defragged. |
| 1553 | */ |
| 1554 | if (BTRFS_I(inode)->defrag_bytes && |
| 1555 | test_range_bit(&BTRFS_I(inode)->io_tree, start, end, |
| 1556 | EXTENT_DEFRAG, 0, NULL)) |
| 1557 | return 1; |
| 1558 | |
| 1559 | return 0; |
| 1560 | } |
| 1561 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1562 | /* |
| 1563 | * extent_io.c call back to do delayed allocation processing |
| 1564 | */ |
Josef Bacik | c6100a4 | 2017-05-05 11:57:13 -0400 | [diff] [blame] | 1565 | static int run_delalloc_range(void *private_data, struct page *locked_page, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1566 | u64 start, u64 end, int *page_started, |
| 1567 | unsigned long *nr_written) |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1568 | { |
Josef Bacik | c6100a4 | 2017-05-05 11:57:13 -0400 | [diff] [blame] | 1569 | struct inode *inode = private_data; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1570 | int ret; |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 1571 | int force_cow = need_force_cow(inode, start, end); |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1572 | |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 1573 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW && !force_cow) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1574 | ret = run_delalloc_nocow(inode, locked_page, start, end, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1575 | page_started, 1, nr_written); |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 1576 | } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC && !force_cow) { |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1577 | ret = run_delalloc_nocow(inode, locked_page, start, end, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1578 | page_started, 0, nr_written); |
Wang Shilong | 7816030 | 2014-07-17 11:44:10 +0800 | [diff] [blame] | 1579 | } else if (!inode_need_compress(inode)) { |
Wang Xiaoguang | dda3245 | 2016-07-11 11:05:29 +0800 | [diff] [blame] | 1580 | ret = cow_file_range(inode, locked_page, start, end, end, |
| 1581 | page_started, nr_written, 1, NULL); |
Josef Bacik | 7ddf5a4 | 2012-06-08 15:26:47 -0400 | [diff] [blame] | 1582 | } else { |
| 1583 | set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, |
| 1584 | &BTRFS_I(inode)->runtime_flags); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1585 | ret = cow_file_range_async(inode, locked_page, start, end, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1586 | page_started, nr_written); |
Josef Bacik | 7ddf5a4 | 2012-06-08 15:26:47 -0400 | [diff] [blame] | 1587 | } |
Qu Wenruo | 52427260 | 2017-03-08 10:25:52 +0800 | [diff] [blame] | 1588 | if (ret) |
| 1589 | btrfs_cleanup_ordered_extents(inode, start, end - start + 1); |
Chris Mason | b888db2b | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 1590 | return ret; |
| 1591 | } |
| 1592 | |
Josef Bacik | c6100a4 | 2017-05-05 11:57:13 -0400 | [diff] [blame] | 1593 | static void btrfs_split_extent_hook(void *private_data, |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 1594 | struct extent_state *orig, u64 split) |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1595 | { |
Josef Bacik | c6100a4 | 2017-05-05 11:57:13 -0400 | [diff] [blame] | 1596 | struct inode *inode = private_data; |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1597 | u64 size; |
| 1598 | |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1599 | /* not delalloc, ignore it */ |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1600 | if (!(orig->state & EXTENT_DELALLOC)) |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 1601 | return; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1602 | |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1603 | size = orig->end - orig->start + 1; |
| 1604 | if (size > BTRFS_MAX_EXTENT_SIZE) { |
David Sterba | 823bb20 | 2017-01-04 11:09:51 +0100 | [diff] [blame] | 1605 | u32 num_extents; |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1606 | u64 new_size; |
| 1607 | |
| 1608 | /* |
Josef Bacik | ba11721 | 2015-03-13 15:01:24 -0400 | [diff] [blame] | 1609 | * See the explanation in btrfs_merge_extent_hook, the same |
| 1610 | * applies here, just in reverse. |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1611 | */ |
| 1612 | new_size = orig->end - split + 1; |
David Sterba | 823bb20 | 2017-01-04 11:09:51 +0100 | [diff] [blame] | 1613 | num_extents = count_max_extents(new_size); |
Josef Bacik | ba11721 | 2015-03-13 15:01:24 -0400 | [diff] [blame] | 1614 | new_size = split - orig->start; |
David Sterba | 823bb20 | 2017-01-04 11:09:51 +0100 | [diff] [blame] | 1615 | num_extents += count_max_extents(new_size); |
| 1616 | if (count_max_extents(size) >= num_extents) |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1617 | return; |
| 1618 | } |
| 1619 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1620 | spin_lock(&BTRFS_I(inode)->lock); |
| 1621 | BTRFS_I(inode)->outstanding_extents++; |
| 1622 | spin_unlock(&BTRFS_I(inode)->lock); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1623 | } |
| 1624 | |
| 1625 | /* |
| 1626 | * extent_io.c merge_extent_hook, used to track merged delayed allocation |
| 1627 | * extents so we can keep track of new extents that are just merged onto old |
| 1628 | * extents, such as when we are doing sequential writes, so we can properly |
| 1629 | * account for the metadata space we'll need. |
| 1630 | */ |
Josef Bacik | c6100a4 | 2017-05-05 11:57:13 -0400 | [diff] [blame] | 1631 | static void btrfs_merge_extent_hook(void *private_data, |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 1632 | struct extent_state *new, |
| 1633 | struct extent_state *other) |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1634 | { |
Josef Bacik | c6100a4 | 2017-05-05 11:57:13 -0400 | [diff] [blame] | 1635 | struct inode *inode = private_data; |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1636 | u64 new_size, old_size; |
David Sterba | 823bb20 | 2017-01-04 11:09:51 +0100 | [diff] [blame] | 1637 | u32 num_extents; |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1638 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1639 | /* not delalloc, ignore it */ |
| 1640 | if (!(other->state & EXTENT_DELALLOC)) |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 1641 | return; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1642 | |
Josef Bacik | 8461a3d | 2015-03-13 15:12:08 -0400 | [diff] [blame] | 1643 | if (new->start > other->start) |
| 1644 | new_size = new->end - other->start + 1; |
| 1645 | else |
| 1646 | new_size = other->end - new->start + 1; |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1647 | |
| 1648 | /* we're not bigger than the max, unreserve the space and go */ |
| 1649 | if (new_size <= BTRFS_MAX_EXTENT_SIZE) { |
| 1650 | spin_lock(&BTRFS_I(inode)->lock); |
| 1651 | BTRFS_I(inode)->outstanding_extents--; |
| 1652 | spin_unlock(&BTRFS_I(inode)->lock); |
| 1653 | return; |
| 1654 | } |
| 1655 | |
| 1656 | /* |
Josef Bacik | ba11721 | 2015-03-13 15:01:24 -0400 | [diff] [blame] | 1657 | * We have to add up either side to figure out how many extents were |
| 1658 | * accounted for before we merged into one big extent. If the number of |
| 1659 | * extents we accounted for is <= the amount we need for the new range |
| 1660 | * then we can return, otherwise drop. Think of it like this |
| 1661 | * |
| 1662 | * [ 4k][MAX_SIZE] |
| 1663 | * |
| 1664 | * So we've grown the extent by a MAX_SIZE extent, this would mean we |
| 1665 | * need 2 outstanding extents, on one side we have 1 and the other side |
| 1666 | * we have 1 so they are == and we can return. But in this case |
| 1667 | * |
| 1668 | * [MAX_SIZE+4k][MAX_SIZE+4k] |
| 1669 | * |
| 1670 | * Each range on their own accounts for 2 extents, but merged together |
| 1671 | * they are only 3 extents worth of accounting, so we need to drop in |
| 1672 | * this case. |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1673 | */ |
Josef Bacik | ba11721 | 2015-03-13 15:01:24 -0400 | [diff] [blame] | 1674 | old_size = other->end - other->start + 1; |
David Sterba | 823bb20 | 2017-01-04 11:09:51 +0100 | [diff] [blame] | 1675 | num_extents = count_max_extents(old_size); |
Josef Bacik | ba11721 | 2015-03-13 15:01:24 -0400 | [diff] [blame] | 1676 | old_size = new->end - new->start + 1; |
David Sterba | 823bb20 | 2017-01-04 11:09:51 +0100 | [diff] [blame] | 1677 | num_extents += count_max_extents(old_size); |
| 1678 | if (count_max_extents(new_size) >= num_extents) |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1679 | return; |
| 1680 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1681 | spin_lock(&BTRFS_I(inode)->lock); |
| 1682 | BTRFS_I(inode)->outstanding_extents--; |
| 1683 | spin_unlock(&BTRFS_I(inode)->lock); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1684 | } |
| 1685 | |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 1686 | static void btrfs_add_delalloc_inodes(struct btrfs_root *root, |
| 1687 | struct inode *inode) |
| 1688 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1689 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
| 1690 | |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 1691 | spin_lock(&root->delalloc_lock); |
| 1692 | if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) { |
| 1693 | list_add_tail(&BTRFS_I(inode)->delalloc_inodes, |
| 1694 | &root->delalloc_inodes); |
| 1695 | set_bit(BTRFS_INODE_IN_DELALLOC_LIST, |
| 1696 | &BTRFS_I(inode)->runtime_flags); |
| 1697 | root->nr_delalloc_inodes++; |
| 1698 | if (root->nr_delalloc_inodes == 1) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1699 | spin_lock(&fs_info->delalloc_root_lock); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 1700 | BUG_ON(!list_empty(&root->delalloc_root)); |
| 1701 | list_add_tail(&root->delalloc_root, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1702 | &fs_info->delalloc_roots); |
| 1703 | spin_unlock(&fs_info->delalloc_root_lock); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 1704 | } |
| 1705 | } |
| 1706 | spin_unlock(&root->delalloc_lock); |
| 1707 | } |
| 1708 | |
| 1709 | static void btrfs_del_delalloc_inode(struct btrfs_root *root, |
Nikolay Borisov | 9e3e97f | 2017-02-20 13:51:07 +0200 | [diff] [blame] | 1710 | struct btrfs_inode *inode) |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 1711 | { |
Nikolay Borisov | 9e3e97f | 2017-02-20 13:51:07 +0200 | [diff] [blame] | 1712 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1713 | |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 1714 | spin_lock(&root->delalloc_lock); |
Nikolay Borisov | 9e3e97f | 2017-02-20 13:51:07 +0200 | [diff] [blame] | 1715 | if (!list_empty(&inode->delalloc_inodes)) { |
| 1716 | list_del_init(&inode->delalloc_inodes); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 1717 | clear_bit(BTRFS_INODE_IN_DELALLOC_LIST, |
Nikolay Borisov | 9e3e97f | 2017-02-20 13:51:07 +0200 | [diff] [blame] | 1718 | &inode->runtime_flags); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 1719 | root->nr_delalloc_inodes--; |
| 1720 | if (!root->nr_delalloc_inodes) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1721 | spin_lock(&fs_info->delalloc_root_lock); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 1722 | BUG_ON(list_empty(&root->delalloc_root)); |
| 1723 | list_del_init(&root->delalloc_root); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1724 | spin_unlock(&fs_info->delalloc_root_lock); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 1725 | } |
| 1726 | } |
| 1727 | spin_unlock(&root->delalloc_lock); |
| 1728 | } |
| 1729 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1730 | /* |
| 1731 | * extent_io.c set_bit_hook, used to track delayed allocation |
| 1732 | * bytes in this file, and to maintain the list of inodes that |
| 1733 | * have pending delalloc work to be done. |
| 1734 | */ |
Josef Bacik | c6100a4 | 2017-05-05 11:57:13 -0400 | [diff] [blame] | 1735 | static void btrfs_set_bit_hook(void *private_data, |
David Sterba | 9ee49a04 | 2015-01-14 19:52:13 +0100 | [diff] [blame] | 1736 | struct extent_state *state, unsigned *bits) |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1737 | { |
Josef Bacik | c6100a4 | 2017-05-05 11:57:13 -0400 | [diff] [blame] | 1738 | struct inode *inode = private_data; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1739 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1740 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
| 1741 | |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 1742 | if ((*bits & EXTENT_DEFRAG) && !(*bits & EXTENT_DELALLOC)) |
| 1743 | WARN_ON(1); |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1744 | /* |
| 1745 | * set_bit and clear bit hooks normally require _irqsave/restore |
Sergei Trofimovich | 27160b6 | 2011-05-20 20:20:32 +0000 | [diff] [blame] | 1746 | * but in this case, we are only testing for the DELALLOC |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1747 | * bit, which is only set or cleared with irqs on |
| 1748 | */ |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1749 | if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) { |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1750 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1751 | u64 len = state->end + 1 - state->start; |
Nikolay Borisov | 70ddc55 | 2017-02-20 13:50:35 +0200 | [diff] [blame] | 1752 | bool do_list = !btrfs_is_free_space_inode(BTRFS_I(inode)); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1753 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1754 | if (*bits & EXTENT_FIRST_DELALLOC) { |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1755 | *bits &= ~EXTENT_FIRST_DELALLOC; |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1756 | } else { |
| 1757 | spin_lock(&BTRFS_I(inode)->lock); |
| 1758 | BTRFS_I(inode)->outstanding_extents++; |
| 1759 | spin_unlock(&BTRFS_I(inode)->lock); |
| 1760 | } |
Josef Bacik | 287a0ab | 2010-03-19 18:07:23 +0000 | [diff] [blame] | 1761 | |
Josef Bacik | 6a3891c | 2015-03-16 17:38:52 -0400 | [diff] [blame] | 1762 | /* For sanity tests */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1763 | if (btrfs_is_testing(fs_info)) |
Josef Bacik | 6a3891c | 2015-03-16 17:38:52 -0400 | [diff] [blame] | 1764 | return; |
| 1765 | |
Nikolay Borisov | 104b4e5 | 2017-06-20 21:01:20 +0300 | [diff] [blame] | 1766 | percpu_counter_add_batch(&fs_info->delalloc_bytes, len, |
| 1767 | fs_info->delalloc_batch); |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 1768 | spin_lock(&BTRFS_I(inode)->lock); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1769 | BTRFS_I(inode)->delalloc_bytes += len; |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 1770 | if (*bits & EXTENT_DEFRAG) |
| 1771 | BTRFS_I(inode)->defrag_bytes += len; |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 1772 | if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST, |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 1773 | &BTRFS_I(inode)->runtime_flags)) |
| 1774 | btrfs_add_delalloc_inodes(root, inode); |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 1775 | spin_unlock(&BTRFS_I(inode)->lock); |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1776 | } |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 1777 | |
| 1778 | if (!(state->state & EXTENT_DELALLOC_NEW) && |
| 1779 | (*bits & EXTENT_DELALLOC_NEW)) { |
| 1780 | spin_lock(&BTRFS_I(inode)->lock); |
| 1781 | BTRFS_I(inode)->new_delalloc_bytes += state->end + 1 - |
| 1782 | state->start; |
| 1783 | spin_unlock(&BTRFS_I(inode)->lock); |
| 1784 | } |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1785 | } |
| 1786 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1787 | /* |
| 1788 | * extent_io.c clear_bit_hook, see set_bit_hook for why |
| 1789 | */ |
Josef Bacik | c6100a4 | 2017-05-05 11:57:13 -0400 | [diff] [blame] | 1790 | static void btrfs_clear_bit_hook(void *private_data, |
David Sterba | 4107488 | 2013-04-29 13:38:46 +0000 | [diff] [blame] | 1791 | struct extent_state *state, |
David Sterba | 9ee49a04 | 2015-01-14 19:52:13 +0100 | [diff] [blame] | 1792 | unsigned *bits) |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1793 | { |
Josef Bacik | c6100a4 | 2017-05-05 11:57:13 -0400 | [diff] [blame] | 1794 | struct btrfs_inode *inode = BTRFS_I((struct inode *)private_data); |
Nikolay Borisov | 6fc0ef6 | 2017-02-20 13:51:03 +0200 | [diff] [blame] | 1795 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb); |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 1796 | u64 len = state->end + 1 - state->start; |
David Sterba | 823bb20 | 2017-01-04 11:09:51 +0100 | [diff] [blame] | 1797 | u32 num_extents = count_max_extents(len); |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 1798 | |
Nikolay Borisov | 6fc0ef6 | 2017-02-20 13:51:03 +0200 | [diff] [blame] | 1799 | spin_lock(&inode->lock); |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 1800 | if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG)) |
Nikolay Borisov | 6fc0ef6 | 2017-02-20 13:51:03 +0200 | [diff] [blame] | 1801 | inode->defrag_bytes -= len; |
| 1802 | spin_unlock(&inode->lock); |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 1803 | |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1804 | /* |
| 1805 | * set_bit and clear bit hooks normally require _irqsave/restore |
Sergei Trofimovich | 27160b6 | 2011-05-20 20:20:32 +0000 | [diff] [blame] | 1806 | * but in this case, we are only testing for the DELALLOC |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1807 | * bit, which is only set or cleared with irqs on |
| 1808 | */ |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1809 | if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) { |
Nikolay Borisov | 6fc0ef6 | 2017-02-20 13:51:03 +0200 | [diff] [blame] | 1810 | struct btrfs_root *root = inode->root; |
Liu Bo | 83eea1f | 2012-07-10 05:28:39 -0600 | [diff] [blame] | 1811 | bool do_list = !btrfs_is_free_space_inode(inode); |
Chris Mason | bcbfce8 | 2008-04-22 13:26:47 -0400 | [diff] [blame] | 1812 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1813 | if (*bits & EXTENT_FIRST_DELALLOC) { |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1814 | *bits &= ~EXTENT_FIRST_DELALLOC; |
Filipe Manana | a315e68 | 2017-03-06 23:04:20 +0000 | [diff] [blame] | 1815 | } else if (!(*bits & EXTENT_CLEAR_META_RESV)) { |
Nikolay Borisov | 6fc0ef6 | 2017-02-20 13:51:03 +0200 | [diff] [blame] | 1816 | spin_lock(&inode->lock); |
| 1817 | inode->outstanding_extents -= num_extents; |
| 1818 | spin_unlock(&inode->lock); |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1819 | } |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1820 | |
Josef Bacik | b6d08f0 | 2013-09-27 14:57:43 -0400 | [diff] [blame] | 1821 | /* |
| 1822 | * We don't reserve metadata space for space cache inodes so we |
| 1823 | * don't need to call dellalloc_release_metadata if there is an |
| 1824 | * error. |
| 1825 | */ |
Filipe Manana | a315e68 | 2017-03-06 23:04:20 +0000 | [diff] [blame] | 1826 | if (*bits & EXTENT_CLEAR_META_RESV && |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1827 | root != fs_info->tree_root) |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1828 | btrfs_delalloc_release_metadata(inode, len); |
| 1829 | |
Josef Bacik | 6a3891c | 2015-03-16 17:38:52 -0400 | [diff] [blame] | 1830 | /* For sanity tests. */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1831 | if (btrfs_is_testing(fs_info)) |
Josef Bacik | 6a3891c | 2015-03-16 17:38:52 -0400 | [diff] [blame] | 1832 | return; |
| 1833 | |
Filipe Manana | a315e68 | 2017-03-06 23:04:20 +0000 | [diff] [blame] | 1834 | if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID && |
| 1835 | do_list && !(state->state & EXTENT_NORESERVE) && |
| 1836 | (*bits & EXTENT_CLEAR_DATA_RESV)) |
Nikolay Borisov | 6fc0ef6 | 2017-02-20 13:51:03 +0200 | [diff] [blame] | 1837 | btrfs_free_reserved_data_space_noquota( |
| 1838 | &inode->vfs_inode, |
Qu Wenruo | 51773be | 2015-10-08 18:19:37 +0800 | [diff] [blame] | 1839 | state->start, len); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1840 | |
Nikolay Borisov | 104b4e5 | 2017-06-20 21:01:20 +0300 | [diff] [blame] | 1841 | percpu_counter_add_batch(&fs_info->delalloc_bytes, -len, |
| 1842 | fs_info->delalloc_batch); |
Nikolay Borisov | 6fc0ef6 | 2017-02-20 13:51:03 +0200 | [diff] [blame] | 1843 | spin_lock(&inode->lock); |
| 1844 | inode->delalloc_bytes -= len; |
| 1845 | if (do_list && inode->delalloc_bytes == 0 && |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 1846 | test_bit(BTRFS_INODE_IN_DELALLOC_LIST, |
Nikolay Borisov | 9e3e97f | 2017-02-20 13:51:07 +0200 | [diff] [blame] | 1847 | &inode->runtime_flags)) |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 1848 | btrfs_del_delalloc_inode(root, inode); |
Nikolay Borisov | 6fc0ef6 | 2017-02-20 13:51:03 +0200 | [diff] [blame] | 1849 | spin_unlock(&inode->lock); |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1850 | } |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 1851 | |
| 1852 | if ((state->state & EXTENT_DELALLOC_NEW) && |
| 1853 | (*bits & EXTENT_DELALLOC_NEW)) { |
| 1854 | spin_lock(&inode->lock); |
| 1855 | ASSERT(inode->new_delalloc_bytes >= len); |
| 1856 | inode->new_delalloc_bytes -= len; |
| 1857 | spin_unlock(&inode->lock); |
| 1858 | } |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1859 | } |
| 1860 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1861 | /* |
| 1862 | * extent_io.c merge_bio_hook, this must check the chunk tree to make sure |
| 1863 | * we don't create bios that span stripes or chunks |
Liu Bo | 6f034ec | 2016-06-22 18:31:49 -0700 | [diff] [blame] | 1864 | * |
| 1865 | * return 1 if page cannot be merged to bio |
| 1866 | * return 0 if page can be merged to bio |
| 1867 | * return error otherwise |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1868 | */ |
Mike Christie | 81a75f67 | 2016-06-05 14:31:54 -0500 | [diff] [blame] | 1869 | int btrfs_merge_bio_hook(struct page *page, unsigned long offset, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1870 | size_t size, struct bio *bio, |
| 1871 | unsigned long bio_flags) |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1872 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1873 | struct inode *inode = page->mapping->host; |
| 1874 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 1875 | u64 logical = (u64)bio->bi_iter.bi_sector << 9; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1876 | u64 length = 0; |
| 1877 | u64 map_length; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1878 | int ret; |
| 1879 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1880 | if (bio_flags & EXTENT_BIO_COMPRESSED) |
| 1881 | return 0; |
| 1882 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 1883 | length = bio->bi_iter.bi_size; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1884 | map_length = length; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1885 | ret = btrfs_map_block(fs_info, btrfs_op(bio), logical, &map_length, |
| 1886 | NULL, 0); |
Liu Bo | 6f034ec | 2016-06-22 18:31:49 -0700 | [diff] [blame] | 1887 | if (ret < 0) |
| 1888 | return ret; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1889 | if (map_length < length + size) |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1890 | return 1; |
Jeff Mahoney | 3444a97 | 2011-10-03 23:23:13 -0400 | [diff] [blame] | 1891 | return 0; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1892 | } |
| 1893 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1894 | /* |
| 1895 | * in order to insert checksums into the metadata in large chunks, |
| 1896 | * we wait until bio submission time. All the pages in the bio are |
| 1897 | * checksummed and sums are attached onto the ordered extent record. |
| 1898 | * |
| 1899 | * At IO completion time the cums attached on the ordered extent record |
| 1900 | * are inserted into the btree |
| 1901 | */ |
Linus Torvalds | 8c27cb3 | 2017-07-05 16:41:23 -0700 | [diff] [blame] | 1902 | static blk_status_t __btrfs_submit_bio_start(void *private_data, struct bio *bio, |
Mike Christie | 81a75f67 | 2016-06-05 14:31:54 -0500 | [diff] [blame] | 1903 | int mirror_num, unsigned long bio_flags, |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 1904 | u64 bio_offset) |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 1905 | { |
Josef Bacik | c6100a4 | 2017-05-05 11:57:13 -0400 | [diff] [blame] | 1906 | struct inode *inode = private_data; |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 1907 | blk_status_t ret = 0; |
Chris Mason | e015640 | 2008-04-16 11:15:20 -0400 | [diff] [blame] | 1908 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 1909 | ret = btrfs_csum_one_bio(inode, bio, 0, 0); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1910 | BUG_ON(ret); /* -ENOMEM */ |
Chris Mason | 4a69a41 | 2008-11-06 22:03:00 -0500 | [diff] [blame] | 1911 | return 0; |
| 1912 | } |
Chris Mason | e015640 | 2008-04-16 11:15:20 -0400 | [diff] [blame] | 1913 | |
Chris Mason | 4a69a41 | 2008-11-06 22:03:00 -0500 | [diff] [blame] | 1914 | /* |
| 1915 | * in order to insert checksums into the metadata in large chunks, |
| 1916 | * we wait until bio submission time. All the pages in the bio are |
| 1917 | * checksummed and sums are attached onto the ordered extent record. |
| 1918 | * |
| 1919 | * At IO completion time the cums attached on the ordered extent record |
| 1920 | * are inserted into the btree |
| 1921 | */ |
Linus Torvalds | 8c27cb3 | 2017-07-05 16:41:23 -0700 | [diff] [blame] | 1922 | static blk_status_t __btrfs_submit_bio_done(void *private_data, struct bio *bio, |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 1923 | int mirror_num, unsigned long bio_flags, |
| 1924 | u64 bio_offset) |
Chris Mason | 4a69a41 | 2008-11-06 22:03:00 -0500 | [diff] [blame] | 1925 | { |
Josef Bacik | c6100a4 | 2017-05-05 11:57:13 -0400 | [diff] [blame] | 1926 | struct inode *inode = private_data; |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 1927 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 1928 | blk_status_t ret; |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 1929 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 1930 | ret = btrfs_map_bio(fs_info, bio, mirror_num, 1); |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 1931 | if (ret) { |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 1932 | bio->bi_status = ret; |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 1933 | bio_endio(bio); |
| 1934 | } |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 1935 | return ret; |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 1936 | } |
| 1937 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1938 | /* |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1939 | * extent_io.c submission hook. This does the right thing for csum calculation |
| 1940 | * on write, or reading the csums from the tree before a read |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1941 | */ |
Linus Torvalds | 8c27cb3 | 2017-07-05 16:41:23 -0700 | [diff] [blame] | 1942 | static blk_status_t btrfs_submit_bio_hook(void *private_data, struct bio *bio, |
Josef Bacik | c6100a4 | 2017-05-05 11:57:13 -0400 | [diff] [blame] | 1943 | int mirror_num, unsigned long bio_flags, |
| 1944 | u64 bio_offset) |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 1945 | { |
Josef Bacik | c6100a4 | 2017-05-05 11:57:13 -0400 | [diff] [blame] | 1946 | struct inode *inode = private_data; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1947 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 1948 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chandan Rajendra | 0d51e28 | 2015-07-27 15:26:43 +0530 | [diff] [blame] | 1949 | enum btrfs_wq_endio_type metadata = BTRFS_WQ_ENDIO_DATA; |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 1950 | blk_status_t ret = 0; |
Chris Mason | 19b9bdb | 2008-10-30 14:23:13 -0400 | [diff] [blame] | 1951 | int skip_sum; |
Josef Bacik | b812ce2 | 2012-11-16 13:56:32 -0500 | [diff] [blame] | 1952 | int async = !atomic_read(&BTRFS_I(inode)->sync_writers); |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 1953 | |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 1954 | skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM; |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1955 | |
Nikolay Borisov | 70ddc55 | 2017-02-20 13:50:35 +0200 | [diff] [blame] | 1956 | if (btrfs_is_free_space_inode(BTRFS_I(inode))) |
Chandan Rajendra | 0d51e28 | 2015-07-27 15:26:43 +0530 | [diff] [blame] | 1957 | metadata = BTRFS_WQ_ENDIO_FREE_SPACE; |
Jeff Mahoney | 0417341 | 2011-10-03 23:23:12 -0400 | [diff] [blame] | 1958 | |
Mike Christie | 37226b2 | 2016-06-05 14:31:52 -0500 | [diff] [blame] | 1959 | if (bio_op(bio) != REQ_OP_WRITE) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1960 | ret = btrfs_bio_wq_end_io(fs_info, bio, metadata); |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 1961 | if (ret) |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 1962 | goto out; |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 1963 | |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1964 | if (bio_flags & EXTENT_BIO_COMPRESSED) { |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 1965 | ret = btrfs_submit_compressed_read(inode, bio, |
| 1966 | mirror_num, |
| 1967 | bio_flags); |
| 1968 | goto out; |
Tsutomu Itoh | c2db107 | 2011-03-01 06:48:31 +0000 | [diff] [blame] | 1969 | } else if (!skip_sum) { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 1970 | ret = btrfs_lookup_bio_sums(inode, bio, NULL); |
Tsutomu Itoh | c2db107 | 2011-03-01 06:48:31 +0000 | [diff] [blame] | 1971 | if (ret) |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 1972 | goto out; |
Tsutomu Itoh | c2db107 | 2011-03-01 06:48:31 +0000 | [diff] [blame] | 1973 | } |
Chris Mason | 4d1b5fb | 2008-08-20 09:44:52 -0400 | [diff] [blame] | 1974 | goto mapit; |
Josef Bacik | b812ce2 | 2012-11-16 13:56:32 -0500 | [diff] [blame] | 1975 | } else if (async && !skip_sum) { |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1976 | /* csum items have already been cloned */ |
| 1977 | if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID) |
| 1978 | goto mapit; |
Chris Mason | 19b9bdb | 2008-10-30 14:23:13 -0400 | [diff] [blame] | 1979 | /* we're doing a write, do the async checksumming */ |
Josef Bacik | c6100a4 | 2017-05-05 11:57:13 -0400 | [diff] [blame] | 1980 | ret = btrfs_wq_submit_bio(fs_info, bio, mirror_num, bio_flags, |
| 1981 | bio_offset, inode, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1982 | __btrfs_submit_bio_start, |
| 1983 | __btrfs_submit_bio_done); |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 1984 | goto out; |
Josef Bacik | b812ce2 | 2012-11-16 13:56:32 -0500 | [diff] [blame] | 1985 | } else if (!skip_sum) { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 1986 | ret = btrfs_csum_one_bio(inode, bio, 0, 0); |
Josef Bacik | b812ce2 | 2012-11-16 13:56:32 -0500 | [diff] [blame] | 1987 | if (ret) |
| 1988 | goto out; |
Chris Mason | 19b9bdb | 2008-10-30 14:23:13 -0400 | [diff] [blame] | 1989 | } |
| 1990 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1991 | mapit: |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 1992 | ret = btrfs_map_bio(fs_info, bio, mirror_num, 0); |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 1993 | |
| 1994 | out: |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 1995 | if (ret) { |
| 1996 | bio->bi_status = ret; |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 1997 | bio_endio(bio); |
| 1998 | } |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 1999 | return ret; |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 2000 | } |
Chris Mason | 6885f30 | 2008-02-20 16:11:05 -0500 | [diff] [blame] | 2001 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2002 | /* |
| 2003 | * given a list of ordered sums record them in the inode. This happens |
| 2004 | * at IO completion time based on sums calculated at bio submission time. |
| 2005 | */ |
Chris Mason | ba1da2f | 2008-07-17 12:54:15 -0400 | [diff] [blame] | 2006 | static noinline int add_pending_csums(struct btrfs_trans_handle *trans, |
David Sterba | df9f628 | 2017-02-10 19:35:37 +0100 | [diff] [blame] | 2007 | struct inode *inode, struct list_head *list) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2008 | { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2009 | struct btrfs_ordered_sum *sum; |
| 2010 | |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 2011 | list_for_each_entry(sum, list, list) { |
Miao Xie | 39847c4 | 2013-03-28 08:08:20 +0000 | [diff] [blame] | 2012 | trans->adding_csums = 1; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 2013 | btrfs_csum_file_blocks(trans, |
| 2014 | BTRFS_I(inode)->root->fs_info->csum_root, sum); |
Miao Xie | 39847c4 | 2013-03-28 08:08:20 +0000 | [diff] [blame] | 2015 | trans->adding_csums = 0; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2016 | } |
| 2017 | return 0; |
| 2018 | } |
| 2019 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 2020 | int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end, |
Qu Wenruo | ba8b04c | 2016-07-19 16:50:36 +0800 | [diff] [blame] | 2021 | struct extent_state **cached_state, int dedupe) |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 2022 | { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2023 | WARN_ON((end & (PAGE_SIZE - 1)) == 0); |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 2024 | return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end, |
David Sterba | 7cd8c75 | 2016-04-26 23:54:39 +0200 | [diff] [blame] | 2025 | cached_state); |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 2026 | } |
| 2027 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2028 | /* see btrfs_writepage_start_hook for details on why this is required */ |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2029 | struct btrfs_writepage_fixup { |
| 2030 | struct page *page; |
| 2031 | struct btrfs_work work; |
| 2032 | }; |
| 2033 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 2034 | static void btrfs_writepage_fixup_worker(struct btrfs_work *work) |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2035 | { |
| 2036 | struct btrfs_writepage_fixup *fixup; |
| 2037 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 2038 | struct extent_state *cached_state = NULL; |
Qu Wenruo | 364ecf3 | 2017-02-27 15:10:38 +0800 | [diff] [blame] | 2039 | struct extent_changeset *data_reserved = NULL; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2040 | struct page *page; |
| 2041 | struct inode *inode; |
| 2042 | u64 page_start; |
| 2043 | u64 page_end; |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 2044 | int ret; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2045 | |
| 2046 | fixup = container_of(work, struct btrfs_writepage_fixup, work); |
| 2047 | page = fixup->page; |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 2048 | again: |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2049 | lock_page(page); |
| 2050 | if (!page->mapping || !PageDirty(page) || !PageChecked(page)) { |
| 2051 | ClearPageChecked(page); |
| 2052 | goto out_page; |
| 2053 | } |
| 2054 | |
| 2055 | inode = page->mapping->host; |
| 2056 | page_start = page_offset(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2057 | page_end = page_offset(page) + PAGE_SIZE - 1; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2058 | |
David Sterba | ff13db4 | 2015-12-03 14:30:40 +0100 | [diff] [blame] | 2059 | lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end, |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 2060 | &cached_state); |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 2061 | |
| 2062 | /* already ordered? We're done */ |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 2063 | if (PagePrivate2(page)) |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2064 | goto out; |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 2065 | |
Nikolay Borisov | a776c6f | 2017-02-20 13:50:49 +0200 | [diff] [blame] | 2066 | ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start, |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2067 | PAGE_SIZE); |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 2068 | if (ordered) { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 2069 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, |
| 2070 | page_end, &cached_state, GFP_NOFS); |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 2071 | unlock_page(page); |
| 2072 | btrfs_start_ordered_extent(inode, ordered, 1); |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 2073 | btrfs_put_ordered_extent(ordered); |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 2074 | goto again; |
| 2075 | } |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2076 | |
Qu Wenruo | 364ecf3 | 2017-02-27 15:10:38 +0800 | [diff] [blame] | 2077 | ret = btrfs_delalloc_reserve_space(inode, &data_reserved, page_start, |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2078 | PAGE_SIZE); |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 2079 | if (ret) { |
| 2080 | mapping_set_error(page->mapping, ret); |
| 2081 | end_extent_writepage(page, ret, page_start, page_end); |
| 2082 | ClearPageChecked(page); |
| 2083 | goto out; |
| 2084 | } |
| 2085 | |
Qu Wenruo | ba8b04c | 2016-07-19 16:50:36 +0800 | [diff] [blame] | 2086 | btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state, |
| 2087 | 0); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2088 | ClearPageChecked(page); |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 2089 | set_page_dirty(page); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2090 | out: |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 2091 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end, |
| 2092 | &cached_state, GFP_NOFS); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2093 | out_page: |
| 2094 | unlock_page(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2095 | put_page(page); |
Miao Xie | b897abe | 2011-01-26 16:19:22 +0800 | [diff] [blame] | 2096 | kfree(fixup); |
Qu Wenruo | 364ecf3 | 2017-02-27 15:10:38 +0800 | [diff] [blame] | 2097 | extent_changeset_free(data_reserved); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2098 | } |
| 2099 | |
| 2100 | /* |
| 2101 | * There are a few paths in the higher layers of the kernel that directly |
| 2102 | * set the page dirty bit without asking the filesystem if it is a |
| 2103 | * good idea. This causes problems because we want to make sure COW |
| 2104 | * properly happens and the data=ordered rules are followed. |
| 2105 | * |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2106 | * 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] | 2107 | * hasn't been properly setup for IO. We kick off an async process |
| 2108 | * to fix it up. The async helper will wait for ordered extents, set |
| 2109 | * the delalloc bit and make it safe to write the page. |
| 2110 | */ |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 2111 | static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end) |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2112 | { |
| 2113 | struct inode *inode = page->mapping->host; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2114 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2115 | struct btrfs_writepage_fixup *fixup; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2116 | |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 2117 | /* this page is properly in the ordered list */ |
| 2118 | if (TestClearPagePrivate2(page)) |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2119 | return 0; |
| 2120 | |
| 2121 | if (PageChecked(page)) |
| 2122 | return -EAGAIN; |
| 2123 | |
| 2124 | fixup = kzalloc(sizeof(*fixup), GFP_NOFS); |
| 2125 | if (!fixup) |
| 2126 | return -EAGAIN; |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 2127 | |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2128 | SetPageChecked(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2129 | get_page(page); |
Liu Bo | 9e0af23 | 2014-08-15 23:36:53 +0800 | [diff] [blame] | 2130 | btrfs_init_work(&fixup->work, btrfs_fixup_helper, |
| 2131 | btrfs_writepage_fixup_worker, NULL, NULL); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2132 | fixup->page = page; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2133 | btrfs_queue_work(fs_info->fixup_workers, &fixup->work); |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 2134 | return -EBUSY; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2135 | } |
| 2136 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2137 | static int insert_reserved_file_extent(struct btrfs_trans_handle *trans, |
| 2138 | struct inode *inode, u64 file_pos, |
| 2139 | u64 disk_bytenr, u64 disk_num_bytes, |
| 2140 | u64 num_bytes, u64 ram_bytes, |
| 2141 | u8 compression, u8 encryption, |
| 2142 | u16 other_encoding, int extent_type) |
| 2143 | { |
| 2144 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 2145 | struct btrfs_file_extent_item *fi; |
| 2146 | struct btrfs_path *path; |
| 2147 | struct extent_buffer *leaf; |
| 2148 | struct btrfs_key ins; |
Qu Wenruo | a12b877 | 2017-02-27 15:10:37 +0800 | [diff] [blame] | 2149 | u64 qg_released; |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 2150 | int extent_inserted = 0; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2151 | int ret; |
| 2152 | |
| 2153 | path = btrfs_alloc_path(); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 2154 | if (!path) |
| 2155 | return -ENOMEM; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2156 | |
Chris Mason | a1ed835 | 2009-09-11 12:27:37 -0400 | [diff] [blame] | 2157 | /* |
| 2158 | * we may be replacing one extent in the tree with another. |
| 2159 | * The new extent is pinned in the extent map, and we don't want |
| 2160 | * to drop it from the cache until it is completely in the btree. |
| 2161 | * |
| 2162 | * So, tell btrfs_drop_extents to leave this extent in the cache. |
| 2163 | * the caller is expected to unpin it and allow it to be merged |
| 2164 | * with the others. |
| 2165 | */ |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 2166 | ret = __btrfs_drop_extents(trans, root, inode, path, file_pos, |
| 2167 | file_pos + num_bytes, NULL, 0, |
| 2168 | 1, sizeof(*fi), &extent_inserted); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2169 | if (ret) |
| 2170 | goto out; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2171 | |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 2172 | if (!extent_inserted) { |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 2173 | ins.objectid = btrfs_ino(BTRFS_I(inode)); |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 2174 | ins.offset = file_pos; |
| 2175 | ins.type = BTRFS_EXTENT_DATA_KEY; |
| 2176 | |
| 2177 | path->leave_spinning = 1; |
| 2178 | ret = btrfs_insert_empty_item(trans, root, path, &ins, |
| 2179 | sizeof(*fi)); |
| 2180 | if (ret) |
| 2181 | goto out; |
| 2182 | } |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2183 | leaf = path->nodes[0]; |
| 2184 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 2185 | struct btrfs_file_extent_item); |
| 2186 | btrfs_set_file_extent_generation(leaf, fi, trans->transid); |
| 2187 | btrfs_set_file_extent_type(leaf, fi, extent_type); |
| 2188 | btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr); |
| 2189 | btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes); |
| 2190 | btrfs_set_file_extent_offset(leaf, fi, 0); |
| 2191 | btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes); |
| 2192 | btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes); |
| 2193 | btrfs_set_file_extent_compression(leaf, fi, compression); |
| 2194 | btrfs_set_file_extent_encryption(leaf, fi, encryption); |
| 2195 | btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 2196 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2197 | btrfs_mark_buffer_dirty(leaf); |
Josef Bacik | ce19533 | 2012-09-25 15:26:16 -0400 | [diff] [blame] | 2198 | btrfs_release_path(path); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2199 | |
| 2200 | inode_add_bytes(inode, num_bytes); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2201 | |
| 2202 | ins.objectid = disk_bytenr; |
| 2203 | ins.offset = disk_num_bytes; |
| 2204 | ins.type = BTRFS_EXTENT_ITEM_KEY; |
Qu Wenruo | a12b877 | 2017-02-27 15:10:37 +0800 | [diff] [blame] | 2205 | |
Qu Wenruo | 297d750 | 2015-09-08 17:08:37 +0800 | [diff] [blame] | 2206 | /* |
Qu Wenruo | 5846a3c | 2015-10-26 14:11:18 +0800 | [diff] [blame] | 2207 | * Release the reserved range from inode dirty range map, as it is |
| 2208 | * already moved into delayed_ref_head |
Qu Wenruo | 297d750 | 2015-09-08 17:08:37 +0800 | [diff] [blame] | 2209 | */ |
Qu Wenruo | a12b877 | 2017-02-27 15:10:37 +0800 | [diff] [blame] | 2210 | ret = btrfs_qgroup_release_data(inode, file_pos, ram_bytes); |
| 2211 | if (ret < 0) |
| 2212 | goto out; |
| 2213 | qg_released = ret; |
| 2214 | ret = btrfs_alloc_reserved_file_extent(trans, root->root_key.objectid, |
| 2215 | btrfs_ino(BTRFS_I(inode)), file_pos, qg_released, &ins); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2216 | out: |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2217 | btrfs_free_path(path); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 2218 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2219 | return ret; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2220 | } |
| 2221 | |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2222 | /* snapshot-aware defrag */ |
| 2223 | struct sa_defrag_extent_backref { |
| 2224 | struct rb_node node; |
| 2225 | struct old_sa_defrag_extent *old; |
| 2226 | u64 root_id; |
| 2227 | u64 inum; |
| 2228 | u64 file_pos; |
| 2229 | u64 extent_offset; |
| 2230 | u64 num_bytes; |
| 2231 | u64 generation; |
| 2232 | }; |
| 2233 | |
| 2234 | struct old_sa_defrag_extent { |
| 2235 | struct list_head list; |
| 2236 | struct new_sa_defrag_extent *new; |
| 2237 | |
| 2238 | u64 extent_offset; |
| 2239 | u64 bytenr; |
| 2240 | u64 offset; |
| 2241 | u64 len; |
| 2242 | int count; |
| 2243 | }; |
| 2244 | |
| 2245 | struct new_sa_defrag_extent { |
| 2246 | struct rb_root root; |
| 2247 | struct list_head head; |
| 2248 | struct btrfs_path *path; |
| 2249 | struct inode *inode; |
| 2250 | u64 file_pos; |
| 2251 | u64 len; |
| 2252 | u64 bytenr; |
| 2253 | u64 disk_len; |
| 2254 | u8 compress_type; |
| 2255 | }; |
| 2256 | |
| 2257 | static int backref_comp(struct sa_defrag_extent_backref *b1, |
| 2258 | struct sa_defrag_extent_backref *b2) |
| 2259 | { |
| 2260 | if (b1->root_id < b2->root_id) |
| 2261 | return -1; |
| 2262 | else if (b1->root_id > b2->root_id) |
| 2263 | return 1; |
| 2264 | |
| 2265 | if (b1->inum < b2->inum) |
| 2266 | return -1; |
| 2267 | else if (b1->inum > b2->inum) |
| 2268 | return 1; |
| 2269 | |
| 2270 | if (b1->file_pos < b2->file_pos) |
| 2271 | return -1; |
| 2272 | else if (b1->file_pos > b2->file_pos) |
| 2273 | return 1; |
| 2274 | |
| 2275 | /* |
| 2276 | * [------------------------------] ===> (a range of space) |
| 2277 | * |<--->| |<---->| =============> (fs/file tree A) |
| 2278 | * |<---------------------------->| ===> (fs/file tree B) |
| 2279 | * |
| 2280 | * A range of space can refer to two file extents in one tree while |
| 2281 | * refer to only one file extent in another tree. |
| 2282 | * |
| 2283 | * So we may process a disk offset more than one time(two extents in A) |
| 2284 | * and locate at the same extent(one extent in B), then insert two same |
| 2285 | * backrefs(both refer to the extent in B). |
| 2286 | */ |
| 2287 | return 0; |
| 2288 | } |
| 2289 | |
| 2290 | static void backref_insert(struct rb_root *root, |
| 2291 | struct sa_defrag_extent_backref *backref) |
| 2292 | { |
| 2293 | struct rb_node **p = &root->rb_node; |
| 2294 | struct rb_node *parent = NULL; |
| 2295 | struct sa_defrag_extent_backref *entry; |
| 2296 | int ret; |
| 2297 | |
| 2298 | while (*p) { |
| 2299 | parent = *p; |
| 2300 | entry = rb_entry(parent, struct sa_defrag_extent_backref, node); |
| 2301 | |
| 2302 | ret = backref_comp(backref, entry); |
| 2303 | if (ret < 0) |
| 2304 | p = &(*p)->rb_left; |
| 2305 | else |
| 2306 | p = &(*p)->rb_right; |
| 2307 | } |
| 2308 | |
| 2309 | rb_link_node(&backref->node, parent, p); |
| 2310 | rb_insert_color(&backref->node, root); |
| 2311 | } |
| 2312 | |
| 2313 | /* |
| 2314 | * Note the backref might has changed, and in this case we just return 0. |
| 2315 | */ |
| 2316 | static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id, |
| 2317 | void *ctx) |
| 2318 | { |
| 2319 | struct btrfs_file_extent_item *extent; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2320 | struct old_sa_defrag_extent *old = ctx; |
| 2321 | struct new_sa_defrag_extent *new = old->new; |
| 2322 | struct btrfs_path *path = new->path; |
| 2323 | struct btrfs_key key; |
| 2324 | struct btrfs_root *root; |
| 2325 | struct sa_defrag_extent_backref *backref; |
| 2326 | struct extent_buffer *leaf; |
| 2327 | struct inode *inode = new->inode; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2328 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2329 | int slot; |
| 2330 | int ret; |
| 2331 | u64 extent_offset; |
| 2332 | u64 num_bytes; |
| 2333 | |
| 2334 | if (BTRFS_I(inode)->root->root_key.objectid == root_id && |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 2335 | inum == btrfs_ino(BTRFS_I(inode))) |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2336 | return 0; |
| 2337 | |
| 2338 | key.objectid = root_id; |
| 2339 | key.type = BTRFS_ROOT_ITEM_KEY; |
| 2340 | key.offset = (u64)-1; |
| 2341 | |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2342 | root = btrfs_read_fs_root_no_name(fs_info, &key); |
| 2343 | if (IS_ERR(root)) { |
| 2344 | if (PTR_ERR(root) == -ENOENT) |
| 2345 | return 0; |
| 2346 | WARN_ON(1); |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 2347 | btrfs_debug(fs_info, "inum=%llu, offset=%llu, root_id=%llu", |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2348 | inum, offset, root_id); |
| 2349 | return PTR_ERR(root); |
| 2350 | } |
| 2351 | |
| 2352 | key.objectid = inum; |
| 2353 | key.type = BTRFS_EXTENT_DATA_KEY; |
| 2354 | if (offset > (u64)-1 << 32) |
| 2355 | key.offset = 0; |
| 2356 | else |
| 2357 | key.offset = offset; |
| 2358 | |
| 2359 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
Dulshani Gunawardhana | fae7f21 | 2013-10-31 10:30:08 +0530 | [diff] [blame] | 2360 | if (WARN_ON(ret < 0)) |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2361 | return ret; |
Josef Bacik | 50f1319 | 2013-07-22 12:50:37 -0400 | [diff] [blame] | 2362 | ret = 0; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2363 | |
| 2364 | while (1) { |
| 2365 | cond_resched(); |
| 2366 | |
| 2367 | leaf = path->nodes[0]; |
| 2368 | slot = path->slots[0]; |
| 2369 | |
| 2370 | if (slot >= btrfs_header_nritems(leaf)) { |
| 2371 | ret = btrfs_next_leaf(root, path); |
| 2372 | if (ret < 0) { |
| 2373 | goto out; |
| 2374 | } else if (ret > 0) { |
| 2375 | ret = 0; |
| 2376 | goto out; |
| 2377 | } |
| 2378 | continue; |
| 2379 | } |
| 2380 | |
| 2381 | path->slots[0]++; |
| 2382 | |
| 2383 | btrfs_item_key_to_cpu(leaf, &key, slot); |
| 2384 | |
| 2385 | if (key.objectid > inum) |
| 2386 | goto out; |
| 2387 | |
| 2388 | if (key.objectid < inum || key.type != BTRFS_EXTENT_DATA_KEY) |
| 2389 | continue; |
| 2390 | |
| 2391 | extent = btrfs_item_ptr(leaf, slot, |
| 2392 | struct btrfs_file_extent_item); |
| 2393 | |
| 2394 | if (btrfs_file_extent_disk_bytenr(leaf, extent) != old->bytenr) |
| 2395 | continue; |
| 2396 | |
Liu Bo | e68afa4 | 2013-07-01 22:13:26 +0800 | [diff] [blame] | 2397 | /* |
| 2398 | * 'offset' refers to the exact key.offset, |
| 2399 | * NOT the 'offset' field in btrfs_extent_data_ref, ie. |
| 2400 | * (key.offset - extent_offset). |
| 2401 | */ |
| 2402 | if (key.offset != offset) |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2403 | continue; |
| 2404 | |
Liu Bo | e68afa4 | 2013-07-01 22:13:26 +0800 | [diff] [blame] | 2405 | extent_offset = btrfs_file_extent_offset(leaf, extent); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2406 | num_bytes = btrfs_file_extent_num_bytes(leaf, extent); |
Liu Bo | e68afa4 | 2013-07-01 22:13:26 +0800 | [diff] [blame] | 2407 | |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2408 | if (extent_offset >= old->extent_offset + old->offset + |
| 2409 | old->len || extent_offset + num_bytes <= |
| 2410 | old->extent_offset + old->offset) |
| 2411 | continue; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2412 | break; |
| 2413 | } |
| 2414 | |
| 2415 | backref = kmalloc(sizeof(*backref), GFP_NOFS); |
| 2416 | if (!backref) { |
| 2417 | ret = -ENOENT; |
| 2418 | goto out; |
| 2419 | } |
| 2420 | |
| 2421 | backref->root_id = root_id; |
| 2422 | backref->inum = inum; |
Liu Bo | e68afa4 | 2013-07-01 22:13:26 +0800 | [diff] [blame] | 2423 | backref->file_pos = offset; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2424 | backref->num_bytes = num_bytes; |
| 2425 | backref->extent_offset = extent_offset; |
| 2426 | backref->generation = btrfs_file_extent_generation(leaf, extent); |
| 2427 | backref->old = old; |
| 2428 | backref_insert(&new->root, backref); |
| 2429 | old->count++; |
| 2430 | out: |
| 2431 | btrfs_release_path(path); |
| 2432 | WARN_ON(ret); |
| 2433 | return ret; |
| 2434 | } |
| 2435 | |
| 2436 | static noinline bool record_extent_backrefs(struct btrfs_path *path, |
| 2437 | struct new_sa_defrag_extent *new) |
| 2438 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2439 | struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2440 | struct old_sa_defrag_extent *old, *tmp; |
| 2441 | int ret; |
| 2442 | |
| 2443 | new->path = path; |
| 2444 | |
| 2445 | list_for_each_entry_safe(old, tmp, &new->head, list) { |
Liu Bo | e68afa4 | 2013-07-01 22:13:26 +0800 | [diff] [blame] | 2446 | ret = iterate_inodes_from_logical(old->bytenr + |
| 2447 | old->extent_offset, fs_info, |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2448 | path, record_one_backref, |
| 2449 | old); |
Josef Bacik | 4724b10 | 2013-11-05 11:11:40 -0500 | [diff] [blame] | 2450 | if (ret < 0 && ret != -ENOENT) |
| 2451 | return false; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2452 | |
| 2453 | /* no backref to be processed for this extent */ |
| 2454 | if (!old->count) { |
| 2455 | list_del(&old->list); |
| 2456 | kfree(old); |
| 2457 | } |
| 2458 | } |
| 2459 | |
| 2460 | if (list_empty(&new->head)) |
| 2461 | return false; |
| 2462 | |
| 2463 | return true; |
| 2464 | } |
| 2465 | |
| 2466 | static int relink_is_mergable(struct extent_buffer *leaf, |
| 2467 | struct btrfs_file_extent_item *fi, |
Liu Bo | 116e002 | 2013-08-02 16:30:40 +0800 | [diff] [blame] | 2468 | struct new_sa_defrag_extent *new) |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2469 | { |
Liu Bo | 116e002 | 2013-08-02 16:30:40 +0800 | [diff] [blame] | 2470 | if (btrfs_file_extent_disk_bytenr(leaf, fi) != new->bytenr) |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2471 | return 0; |
| 2472 | |
| 2473 | if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG) |
| 2474 | return 0; |
| 2475 | |
Liu Bo | 116e002 | 2013-08-02 16:30:40 +0800 | [diff] [blame] | 2476 | if (btrfs_file_extent_compression(leaf, fi) != new->compress_type) |
| 2477 | return 0; |
| 2478 | |
| 2479 | if (btrfs_file_extent_encryption(leaf, fi) || |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2480 | btrfs_file_extent_other_encoding(leaf, fi)) |
| 2481 | return 0; |
| 2482 | |
| 2483 | return 1; |
| 2484 | } |
| 2485 | |
| 2486 | /* |
| 2487 | * Note the backref might has changed, and in this case we just return 0. |
| 2488 | */ |
| 2489 | static noinline int relink_extent_backref(struct btrfs_path *path, |
| 2490 | struct sa_defrag_extent_backref *prev, |
| 2491 | struct sa_defrag_extent_backref *backref) |
| 2492 | { |
| 2493 | struct btrfs_file_extent_item *extent; |
| 2494 | struct btrfs_file_extent_item *item; |
| 2495 | struct btrfs_ordered_extent *ordered; |
| 2496 | struct btrfs_trans_handle *trans; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2497 | struct btrfs_root *root; |
| 2498 | struct btrfs_key key; |
| 2499 | struct extent_buffer *leaf; |
| 2500 | struct old_sa_defrag_extent *old = backref->old; |
| 2501 | struct new_sa_defrag_extent *new = old->new; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2502 | struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2503 | struct inode *inode; |
| 2504 | struct extent_state *cached = NULL; |
| 2505 | int ret = 0; |
| 2506 | u64 start; |
| 2507 | u64 len; |
| 2508 | u64 lock_start; |
| 2509 | u64 lock_end; |
| 2510 | bool merge = false; |
| 2511 | int index; |
| 2512 | |
| 2513 | if (prev && prev->root_id == backref->root_id && |
| 2514 | prev->inum == backref->inum && |
| 2515 | prev->file_pos + prev->num_bytes == backref->file_pos) |
| 2516 | merge = true; |
| 2517 | |
| 2518 | /* step 1: get root */ |
| 2519 | key.objectid = backref->root_id; |
| 2520 | key.type = BTRFS_ROOT_ITEM_KEY; |
| 2521 | key.offset = (u64)-1; |
| 2522 | |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2523 | index = srcu_read_lock(&fs_info->subvol_srcu); |
| 2524 | |
| 2525 | root = btrfs_read_fs_root_no_name(fs_info, &key); |
| 2526 | if (IS_ERR(root)) { |
| 2527 | srcu_read_unlock(&fs_info->subvol_srcu, index); |
| 2528 | if (PTR_ERR(root) == -ENOENT) |
| 2529 | return 0; |
| 2530 | return PTR_ERR(root); |
| 2531 | } |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2532 | |
Wang Shilong | bcbba5e | 2014-02-08 23:46:35 +0800 | [diff] [blame] | 2533 | if (btrfs_root_readonly(root)) { |
| 2534 | srcu_read_unlock(&fs_info->subvol_srcu, index); |
| 2535 | return 0; |
| 2536 | } |
| 2537 | |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2538 | /* step 2: get inode */ |
| 2539 | key.objectid = backref->inum; |
| 2540 | key.type = BTRFS_INODE_ITEM_KEY; |
| 2541 | key.offset = 0; |
| 2542 | |
| 2543 | inode = btrfs_iget(fs_info->sb, &key, root, NULL); |
| 2544 | if (IS_ERR(inode)) { |
| 2545 | srcu_read_unlock(&fs_info->subvol_srcu, index); |
| 2546 | return 0; |
| 2547 | } |
| 2548 | |
| 2549 | srcu_read_unlock(&fs_info->subvol_srcu, index); |
| 2550 | |
| 2551 | /* step 3: relink backref */ |
| 2552 | lock_start = backref->file_pos; |
| 2553 | lock_end = backref->file_pos + backref->num_bytes - 1; |
| 2554 | lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end, |
David Sterba | ff13db4 | 2015-12-03 14:30:40 +0100 | [diff] [blame] | 2555 | &cached); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2556 | |
| 2557 | ordered = btrfs_lookup_first_ordered_extent(inode, lock_end); |
| 2558 | if (ordered) { |
| 2559 | btrfs_put_ordered_extent(ordered); |
| 2560 | goto out_unlock; |
| 2561 | } |
| 2562 | |
| 2563 | trans = btrfs_join_transaction(root); |
| 2564 | if (IS_ERR(trans)) { |
| 2565 | ret = PTR_ERR(trans); |
| 2566 | goto out_unlock; |
| 2567 | } |
| 2568 | |
| 2569 | key.objectid = backref->inum; |
| 2570 | key.type = BTRFS_EXTENT_DATA_KEY; |
| 2571 | key.offset = backref->file_pos; |
| 2572 | |
| 2573 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 2574 | if (ret < 0) { |
| 2575 | goto out_free_path; |
| 2576 | } else if (ret > 0) { |
| 2577 | ret = 0; |
| 2578 | goto out_free_path; |
| 2579 | } |
| 2580 | |
| 2581 | extent = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 2582 | struct btrfs_file_extent_item); |
| 2583 | |
| 2584 | if (btrfs_file_extent_generation(path->nodes[0], extent) != |
| 2585 | backref->generation) |
| 2586 | goto out_free_path; |
| 2587 | |
| 2588 | btrfs_release_path(path); |
| 2589 | |
| 2590 | start = backref->file_pos; |
| 2591 | if (backref->extent_offset < old->extent_offset + old->offset) |
| 2592 | start += old->extent_offset + old->offset - |
| 2593 | backref->extent_offset; |
| 2594 | |
| 2595 | len = min(backref->extent_offset + backref->num_bytes, |
| 2596 | old->extent_offset + old->offset + old->len); |
| 2597 | len -= max(backref->extent_offset, old->extent_offset + old->offset); |
| 2598 | |
| 2599 | ret = btrfs_drop_extents(trans, root, inode, start, |
| 2600 | start + len, 1); |
| 2601 | if (ret) |
| 2602 | goto out_free_path; |
| 2603 | again: |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 2604 | key.objectid = btrfs_ino(BTRFS_I(inode)); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2605 | key.type = BTRFS_EXTENT_DATA_KEY; |
| 2606 | key.offset = start; |
| 2607 | |
Liu Bo | a09a0a7 | 2013-03-11 09:20:58 +0000 | [diff] [blame] | 2608 | path->leave_spinning = 1; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2609 | if (merge) { |
| 2610 | struct btrfs_file_extent_item *fi; |
| 2611 | u64 extent_len; |
| 2612 | struct btrfs_key found_key; |
| 2613 | |
Gui Hecheng | 3c9665d | 2014-01-23 13:41:09 +0800 | [diff] [blame] | 2614 | ret = btrfs_search_slot(trans, root, &key, path, 0, 1); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2615 | if (ret < 0) |
| 2616 | goto out_free_path; |
| 2617 | |
| 2618 | path->slots[0]--; |
| 2619 | leaf = path->nodes[0]; |
| 2620 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 2621 | |
| 2622 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 2623 | struct btrfs_file_extent_item); |
| 2624 | extent_len = btrfs_file_extent_num_bytes(leaf, fi); |
| 2625 | |
Liu Bo | 116e002 | 2013-08-02 16:30:40 +0800 | [diff] [blame] | 2626 | if (extent_len + found_key.offset == start && |
| 2627 | relink_is_mergable(leaf, fi, new)) { |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2628 | btrfs_set_file_extent_num_bytes(leaf, fi, |
| 2629 | extent_len + len); |
| 2630 | btrfs_mark_buffer_dirty(leaf); |
| 2631 | inode_add_bytes(inode, len); |
| 2632 | |
| 2633 | ret = 1; |
| 2634 | goto out_free_path; |
| 2635 | } else { |
| 2636 | merge = false; |
| 2637 | btrfs_release_path(path); |
| 2638 | goto again; |
| 2639 | } |
| 2640 | } |
| 2641 | |
| 2642 | ret = btrfs_insert_empty_item(trans, root, path, &key, |
| 2643 | sizeof(*extent)); |
| 2644 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 2645 | btrfs_abort_transaction(trans, ret); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2646 | goto out_free_path; |
| 2647 | } |
| 2648 | |
| 2649 | leaf = path->nodes[0]; |
| 2650 | item = btrfs_item_ptr(leaf, path->slots[0], |
| 2651 | struct btrfs_file_extent_item); |
| 2652 | btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr); |
| 2653 | btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len); |
| 2654 | btrfs_set_file_extent_offset(leaf, item, start - new->file_pos); |
| 2655 | btrfs_set_file_extent_num_bytes(leaf, item, len); |
| 2656 | btrfs_set_file_extent_ram_bytes(leaf, item, new->len); |
| 2657 | btrfs_set_file_extent_generation(leaf, item, trans->transid); |
| 2658 | btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG); |
| 2659 | btrfs_set_file_extent_compression(leaf, item, new->compress_type); |
| 2660 | btrfs_set_file_extent_encryption(leaf, item, 0); |
| 2661 | btrfs_set_file_extent_other_encoding(leaf, item, 0); |
| 2662 | |
| 2663 | btrfs_mark_buffer_dirty(leaf); |
| 2664 | inode_add_bytes(inode, len); |
Liu Bo | a09a0a7 | 2013-03-11 09:20:58 +0000 | [diff] [blame] | 2665 | btrfs_release_path(path); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2666 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2667 | ret = btrfs_inc_extent_ref(trans, fs_info, new->bytenr, |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2668 | new->disk_len, 0, |
| 2669 | backref->root_id, backref->inum, |
Filipe Manana | b06c4bf | 2015-10-23 07:52:54 +0100 | [diff] [blame] | 2670 | new->file_pos); /* start - extent_offset */ |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2671 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 2672 | btrfs_abort_transaction(trans, ret); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2673 | goto out_free_path; |
| 2674 | } |
| 2675 | |
| 2676 | ret = 1; |
| 2677 | out_free_path: |
| 2678 | btrfs_release_path(path); |
Liu Bo | a09a0a7 | 2013-03-11 09:20:58 +0000 | [diff] [blame] | 2679 | path->leave_spinning = 0; |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 2680 | btrfs_end_transaction(trans); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2681 | out_unlock: |
| 2682 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end, |
| 2683 | &cached, GFP_NOFS); |
| 2684 | iput(inode); |
| 2685 | return ret; |
| 2686 | } |
| 2687 | |
Liu Bo | 6f51956 | 2013-10-29 10:45:05 +0800 | [diff] [blame] | 2688 | static void free_sa_defrag_extent(struct new_sa_defrag_extent *new) |
| 2689 | { |
| 2690 | struct old_sa_defrag_extent *old, *tmp; |
| 2691 | |
| 2692 | if (!new) |
| 2693 | return; |
| 2694 | |
| 2695 | list_for_each_entry_safe(old, tmp, &new->head, list) { |
Liu Bo | 6f51956 | 2013-10-29 10:45:05 +0800 | [diff] [blame] | 2696 | kfree(old); |
| 2697 | } |
| 2698 | kfree(new); |
| 2699 | } |
| 2700 | |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2701 | static void relink_file_extents(struct new_sa_defrag_extent *new) |
| 2702 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2703 | struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2704 | struct btrfs_path *path; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2705 | struct sa_defrag_extent_backref *backref; |
| 2706 | struct sa_defrag_extent_backref *prev = NULL; |
| 2707 | struct inode *inode; |
| 2708 | struct btrfs_root *root; |
| 2709 | struct rb_node *node; |
| 2710 | int ret; |
| 2711 | |
| 2712 | inode = new->inode; |
| 2713 | root = BTRFS_I(inode)->root; |
| 2714 | |
| 2715 | path = btrfs_alloc_path(); |
| 2716 | if (!path) |
| 2717 | return; |
| 2718 | |
| 2719 | if (!record_extent_backrefs(path, new)) { |
| 2720 | btrfs_free_path(path); |
| 2721 | goto out; |
| 2722 | } |
| 2723 | btrfs_release_path(path); |
| 2724 | |
| 2725 | while (1) { |
| 2726 | node = rb_first(&new->root); |
| 2727 | if (!node) |
| 2728 | break; |
| 2729 | rb_erase(node, &new->root); |
| 2730 | |
| 2731 | backref = rb_entry(node, struct sa_defrag_extent_backref, node); |
| 2732 | |
| 2733 | ret = relink_extent_backref(path, prev, backref); |
| 2734 | WARN_ON(ret < 0); |
| 2735 | |
| 2736 | kfree(prev); |
| 2737 | |
| 2738 | if (ret == 1) |
| 2739 | prev = backref; |
| 2740 | else |
| 2741 | prev = NULL; |
| 2742 | cond_resched(); |
| 2743 | } |
| 2744 | kfree(prev); |
| 2745 | |
| 2746 | btrfs_free_path(path); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2747 | out: |
Liu Bo | 6f51956 | 2013-10-29 10:45:05 +0800 | [diff] [blame] | 2748 | free_sa_defrag_extent(new); |
| 2749 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2750 | atomic_dec(&fs_info->defrag_running); |
| 2751 | wake_up(&fs_info->transaction_wait); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2752 | } |
| 2753 | |
| 2754 | static struct new_sa_defrag_extent * |
| 2755 | record_old_file_extents(struct inode *inode, |
| 2756 | struct btrfs_ordered_extent *ordered) |
| 2757 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2758 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2759 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 2760 | struct btrfs_path *path; |
| 2761 | struct btrfs_key key; |
Liu Bo | 6f51956 | 2013-10-29 10:45:05 +0800 | [diff] [blame] | 2762 | struct old_sa_defrag_extent *old; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2763 | struct new_sa_defrag_extent *new; |
| 2764 | int ret; |
| 2765 | |
| 2766 | new = kmalloc(sizeof(*new), GFP_NOFS); |
| 2767 | if (!new) |
| 2768 | return NULL; |
| 2769 | |
| 2770 | new->inode = inode; |
| 2771 | new->file_pos = ordered->file_offset; |
| 2772 | new->len = ordered->len; |
| 2773 | new->bytenr = ordered->start; |
| 2774 | new->disk_len = ordered->disk_len; |
| 2775 | new->compress_type = ordered->compress_type; |
| 2776 | new->root = RB_ROOT; |
| 2777 | INIT_LIST_HEAD(&new->head); |
| 2778 | |
| 2779 | path = btrfs_alloc_path(); |
| 2780 | if (!path) |
| 2781 | goto out_kfree; |
| 2782 | |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 2783 | key.objectid = btrfs_ino(BTRFS_I(inode)); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2784 | key.type = BTRFS_EXTENT_DATA_KEY; |
| 2785 | key.offset = new->file_pos; |
| 2786 | |
| 2787 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 2788 | if (ret < 0) |
| 2789 | goto out_free_path; |
| 2790 | if (ret > 0 && path->slots[0] > 0) |
| 2791 | path->slots[0]--; |
| 2792 | |
| 2793 | /* find out all the old extents for the file range */ |
| 2794 | while (1) { |
| 2795 | struct btrfs_file_extent_item *extent; |
| 2796 | struct extent_buffer *l; |
| 2797 | int slot; |
| 2798 | u64 num_bytes; |
| 2799 | u64 offset; |
| 2800 | u64 end; |
| 2801 | u64 disk_bytenr; |
| 2802 | u64 extent_offset; |
| 2803 | |
| 2804 | l = path->nodes[0]; |
| 2805 | slot = path->slots[0]; |
| 2806 | |
| 2807 | if (slot >= btrfs_header_nritems(l)) { |
| 2808 | ret = btrfs_next_leaf(root, path); |
| 2809 | if (ret < 0) |
Liu Bo | 6f51956 | 2013-10-29 10:45:05 +0800 | [diff] [blame] | 2810 | goto out_free_path; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2811 | else if (ret > 0) |
| 2812 | break; |
| 2813 | continue; |
| 2814 | } |
| 2815 | |
| 2816 | btrfs_item_key_to_cpu(l, &key, slot); |
| 2817 | |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 2818 | if (key.objectid != btrfs_ino(BTRFS_I(inode))) |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2819 | break; |
| 2820 | if (key.type != BTRFS_EXTENT_DATA_KEY) |
| 2821 | break; |
| 2822 | if (key.offset >= new->file_pos + new->len) |
| 2823 | break; |
| 2824 | |
| 2825 | extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item); |
| 2826 | |
| 2827 | num_bytes = btrfs_file_extent_num_bytes(l, extent); |
| 2828 | if (key.offset + num_bytes < new->file_pos) |
| 2829 | goto next; |
| 2830 | |
| 2831 | disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent); |
| 2832 | if (!disk_bytenr) |
| 2833 | goto next; |
| 2834 | |
| 2835 | extent_offset = btrfs_file_extent_offset(l, extent); |
| 2836 | |
| 2837 | old = kmalloc(sizeof(*old), GFP_NOFS); |
| 2838 | if (!old) |
Liu Bo | 6f51956 | 2013-10-29 10:45:05 +0800 | [diff] [blame] | 2839 | goto out_free_path; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2840 | |
| 2841 | offset = max(new->file_pos, key.offset); |
| 2842 | end = min(new->file_pos + new->len, key.offset + num_bytes); |
| 2843 | |
| 2844 | old->bytenr = disk_bytenr; |
| 2845 | old->extent_offset = extent_offset; |
| 2846 | old->offset = offset - key.offset; |
| 2847 | old->len = end - offset; |
| 2848 | old->new = new; |
| 2849 | old->count = 0; |
| 2850 | list_add_tail(&old->list, &new->head); |
| 2851 | next: |
| 2852 | path->slots[0]++; |
| 2853 | cond_resched(); |
| 2854 | } |
| 2855 | |
| 2856 | btrfs_free_path(path); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2857 | atomic_inc(&fs_info->defrag_running); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2858 | |
| 2859 | return new; |
| 2860 | |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2861 | out_free_path: |
| 2862 | btrfs_free_path(path); |
| 2863 | out_kfree: |
Liu Bo | 6f51956 | 2013-10-29 10:45:05 +0800 | [diff] [blame] | 2864 | free_sa_defrag_extent(new); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2865 | return NULL; |
| 2866 | } |
| 2867 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2868 | static void btrfs_release_delalloc_bytes(struct btrfs_fs_info *fs_info, |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 2869 | u64 start, u64 len) |
| 2870 | { |
| 2871 | struct btrfs_block_group_cache *cache; |
| 2872 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2873 | cache = btrfs_lookup_block_group(fs_info, start); |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 2874 | ASSERT(cache); |
| 2875 | |
| 2876 | spin_lock(&cache->lock); |
| 2877 | cache->delalloc_bytes -= len; |
| 2878 | spin_unlock(&cache->lock); |
| 2879 | |
| 2880 | btrfs_put_block_group(cache); |
| 2881 | } |
| 2882 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2883 | /* as ordered data IO finishes, this gets called so we can finish |
| 2884 | * an ordered extent if the range of bytes in the file it covers are |
| 2885 | * fully written. |
| 2886 | */ |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2887 | static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2888 | { |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2889 | struct inode *inode = ordered_extent->inode; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2890 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2891 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 2892 | struct btrfs_trans_handle *trans = NULL; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2893 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 2894 | struct extent_state *cached_state = NULL; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2895 | struct new_sa_defrag_extent *new = NULL; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 2896 | int compress_type = 0; |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 2897 | int ret = 0; |
| 2898 | u64 logical_len = ordered_extent->len; |
Li Zefan | 82d5902 | 2011-04-20 10:33:24 +0800 | [diff] [blame] | 2899 | bool nolock; |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 2900 | bool truncated = false; |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 2901 | bool range_locked = false; |
| 2902 | bool clear_new_delalloc_bytes = false; |
| 2903 | |
| 2904 | if (!test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) && |
| 2905 | !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags) && |
| 2906 | !test_bit(BTRFS_ORDERED_DIRECT, &ordered_extent->flags)) |
| 2907 | clear_new_delalloc_bytes = true; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2908 | |
Nikolay Borisov | 70ddc55 | 2017-02-20 13:50:35 +0200 | [diff] [blame] | 2909 | nolock = btrfs_is_free_space_inode(BTRFS_I(inode)); |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 2910 | |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2911 | if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) { |
| 2912 | ret = -EIO; |
| 2913 | goto out; |
| 2914 | } |
| 2915 | |
Nikolay Borisov | 7ab7956 | 2017-02-20 13:50:57 +0200 | [diff] [blame] | 2916 | btrfs_free_io_failure_record(BTRFS_I(inode), |
| 2917 | ordered_extent->file_offset, |
| 2918 | ordered_extent->file_offset + |
| 2919 | ordered_extent->len - 1); |
Miao Xie | f612496 | 2014-09-12 18:44:04 +0800 | [diff] [blame] | 2920 | |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 2921 | if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) { |
| 2922 | truncated = true; |
| 2923 | logical_len = ordered_extent->truncated_len; |
| 2924 | /* Truncated the entire extent, don't bother adding */ |
| 2925 | if (!logical_len) |
| 2926 | goto out; |
| 2927 | } |
| 2928 | |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 2929 | if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) { |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2930 | BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */ |
Qu Wenruo | 94ed938 | 2015-09-08 17:25:56 +0800 | [diff] [blame] | 2931 | |
| 2932 | /* |
| 2933 | * For mwrite(mmap + memset to write) case, we still reserve |
| 2934 | * space for NOCOW range. |
| 2935 | * As NOCOW won't cause a new delayed ref, just free the space |
| 2936 | */ |
Qu Wenruo | bc42bda | 2017-02-27 15:10:39 +0800 | [diff] [blame] | 2937 | btrfs_qgroup_free_data(inode, NULL, ordered_extent->file_offset, |
Qu Wenruo | 94ed938 | 2015-09-08 17:25:56 +0800 | [diff] [blame] | 2938 | ordered_extent->len); |
Josef Bacik | 6c760c0 | 2012-11-09 10:53:21 -0500 | [diff] [blame] | 2939 | btrfs_ordered_update_i_size(inode, 0, ordered_extent); |
| 2940 | if (nolock) |
| 2941 | trans = btrfs_join_transaction_nolock(root); |
| 2942 | else |
| 2943 | trans = btrfs_join_transaction(root); |
| 2944 | if (IS_ERR(trans)) { |
| 2945 | ret = PTR_ERR(trans); |
| 2946 | trans = NULL; |
| 2947 | goto out; |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 2948 | } |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2949 | trans->block_rsv = &fs_info->delalloc_block_rsv; |
Josef Bacik | 6c760c0 | 2012-11-09 10:53:21 -0500 | [diff] [blame] | 2950 | ret = btrfs_update_inode_fallback(trans, root, inode); |
| 2951 | if (ret) /* -ENOMEM or corruption */ |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 2952 | btrfs_abort_transaction(trans, ret); |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 2953 | goto out; |
| 2954 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2955 | |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 2956 | range_locked = true; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 2957 | lock_extent_bits(io_tree, ordered_extent->file_offset, |
| 2958 | ordered_extent->file_offset + ordered_extent->len - 1, |
David Sterba | ff13db4 | 2015-12-03 14:30:40 +0100 | [diff] [blame] | 2959 | &cached_state); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2960 | |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2961 | ret = test_range_bit(io_tree, ordered_extent->file_offset, |
| 2962 | ordered_extent->file_offset + ordered_extent->len - 1, |
Liu Bo | 452e62b | 2017-05-26 17:44:23 -0600 | [diff] [blame] | 2963 | EXTENT_DEFRAG, 0, cached_state); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2964 | if (ret) { |
| 2965 | u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item); |
Josef Bacik | 8101c8d | 2014-01-29 16:05:30 -0500 | [diff] [blame] | 2966 | if (0 && last_snapshot >= BTRFS_I(inode)->generation) |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2967 | /* the inode is shared */ |
| 2968 | new = record_old_file_extents(inode, ordered_extent); |
| 2969 | |
| 2970 | clear_extent_bit(io_tree, ordered_extent->file_offset, |
| 2971 | ordered_extent->file_offset + ordered_extent->len - 1, |
| 2972 | EXTENT_DEFRAG, 0, 0, &cached_state, GFP_NOFS); |
| 2973 | } |
| 2974 | |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 2975 | if (nolock) |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 2976 | trans = btrfs_join_transaction_nolock(root); |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 2977 | else |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 2978 | trans = btrfs_join_transaction(root); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2979 | if (IS_ERR(trans)) { |
| 2980 | ret = PTR_ERR(trans); |
| 2981 | trans = NULL; |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 2982 | goto out; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2983 | } |
Chris Mason | a79b7d4 | 2014-05-22 16:18:52 -0700 | [diff] [blame] | 2984 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2985 | trans->block_rsv = &fs_info->delalloc_block_rsv; |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 2986 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2987 | if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags)) |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 2988 | compress_type = ordered_extent->compress_type; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2989 | if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) { |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 2990 | BUG_ON(compress_type); |
Nikolay Borisov | 7a6d706 | 2017-02-20 13:50:48 +0200 | [diff] [blame] | 2991 | ret = btrfs_mark_extent_written(trans, BTRFS_I(inode), |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2992 | ordered_extent->file_offset, |
| 2993 | ordered_extent->file_offset + |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 2994 | logical_len); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2995 | } else { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2996 | BUG_ON(root == fs_info->tree_root); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2997 | ret = insert_reserved_file_extent(trans, inode, |
| 2998 | ordered_extent->file_offset, |
| 2999 | ordered_extent->start, |
| 3000 | ordered_extent->disk_len, |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 3001 | logical_len, logical_len, |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 3002 | compress_type, 0, 0, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 3003 | BTRFS_FILE_EXTENT_REG); |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 3004 | if (!ret) |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3005 | btrfs_release_delalloc_bytes(fs_info, |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 3006 | ordered_extent->start, |
| 3007 | ordered_extent->disk_len); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 3008 | } |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 3009 | unpin_extent_cache(&BTRFS_I(inode)->extent_tree, |
| 3010 | ordered_extent->file_offset, ordered_extent->len, |
| 3011 | trans->transid); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3012 | if (ret < 0) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3013 | btrfs_abort_transaction(trans, ret); |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 3014 | goto out; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3015 | } |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 3016 | |
David Sterba | df9f628 | 2017-02-10 19:35:37 +0100 | [diff] [blame] | 3017 | add_pending_csums(trans, inode, &ordered_extent->list); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3018 | |
Josef Bacik | 6c760c0 | 2012-11-09 10:53:21 -0500 | [diff] [blame] | 3019 | btrfs_ordered_update_i_size(inode, 0, ordered_extent); |
| 3020 | ret = btrfs_update_inode_fallback(trans, root, inode); |
| 3021 | if (ret) { /* -ENOMEM or corruption */ |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3022 | btrfs_abort_transaction(trans, ret); |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 3023 | goto out; |
Josef Bacik | 1ef30be | 2011-04-05 19:25:36 -0400 | [diff] [blame] | 3024 | } |
| 3025 | ret = 0; |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 3026 | out: |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 3027 | if (range_locked || clear_new_delalloc_bytes) { |
| 3028 | unsigned int clear_bits = 0; |
| 3029 | |
| 3030 | if (range_locked) |
| 3031 | clear_bits |= EXTENT_LOCKED; |
| 3032 | if (clear_new_delalloc_bytes) |
| 3033 | clear_bits |= EXTENT_DELALLOC_NEW; |
| 3034 | clear_extent_bit(&BTRFS_I(inode)->io_tree, |
| 3035 | ordered_extent->file_offset, |
| 3036 | ordered_extent->file_offset + |
| 3037 | ordered_extent->len - 1, |
| 3038 | clear_bits, |
| 3039 | (clear_bits & EXTENT_LOCKED) ? 1 : 0, |
| 3040 | 0, &cached_state, GFP_NOFS); |
| 3041 | } |
| 3042 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3043 | if (root != fs_info->tree_root) |
Nikolay Borisov | 691fa05 | 2017-02-20 13:50:42 +0200 | [diff] [blame] | 3044 | btrfs_delalloc_release_metadata(BTRFS_I(inode), |
| 3045 | ordered_extent->len); |
Miao Xie | a698d075 | 2012-09-20 01:51:59 -0600 | [diff] [blame] | 3046 | if (trans) |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 3047 | btrfs_end_transaction(trans); |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 3048 | |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 3049 | if (ret || truncated) { |
| 3050 | u64 start, end; |
| 3051 | |
| 3052 | if (truncated) |
| 3053 | start = ordered_extent->file_offset + logical_len; |
| 3054 | else |
| 3055 | start = ordered_extent->file_offset; |
| 3056 | end = ordered_extent->file_offset + ordered_extent->len - 1; |
| 3057 | clear_extent_uptodate(io_tree, start, end, NULL, GFP_NOFS); |
| 3058 | |
| 3059 | /* 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] | 3060 | btrfs_drop_extent_cache(BTRFS_I(inode), start, end, 0); |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 3061 | |
Josef Bacik | 0bec9ef | 2013-01-31 14:58:00 -0500 | [diff] [blame] | 3062 | /* |
| 3063 | * If the ordered extent had an IOERR or something else went |
| 3064 | * wrong we need to return the space for this ordered extent |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 3065 | * back to the allocator. We only free the extent in the |
| 3066 | * truncated case if we didn't write out the extent at all. |
Josef Bacik | 0bec9ef | 2013-01-31 14:58:00 -0500 | [diff] [blame] | 3067 | */ |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 3068 | if ((ret || !logical_len) && |
| 3069 | !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) && |
Josef Bacik | 0bec9ef | 2013-01-31 14:58:00 -0500 | [diff] [blame] | 3070 | !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3071 | btrfs_free_reserved_extent(fs_info, |
| 3072 | ordered_extent->start, |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 3073 | ordered_extent->disk_len, 1); |
Josef Bacik | 0bec9ef | 2013-01-31 14:58:00 -0500 | [diff] [blame] | 3074 | } |
| 3075 | |
| 3076 | |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 3077 | /* |
Liu Bo | 8bad3c0 | 2012-06-18 12:14:23 +0800 | [diff] [blame] | 3078 | * This needs to be done to make sure anybody waiting knows we are done |
| 3079 | * updating everything for this ordered extent. |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 3080 | */ |
| 3081 | btrfs_remove_ordered_extent(inode, ordered_extent); |
| 3082 | |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 3083 | /* for snapshot-aware defrag */ |
Liu Bo | 6f51956 | 2013-10-29 10:45:05 +0800 | [diff] [blame] | 3084 | if (new) { |
| 3085 | if (ret) { |
| 3086 | free_sa_defrag_extent(new); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3087 | atomic_dec(&fs_info->defrag_running); |
Liu Bo | 6f51956 | 2013-10-29 10:45:05 +0800 | [diff] [blame] | 3088 | } else { |
| 3089 | relink_file_extents(new); |
| 3090 | } |
| 3091 | } |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 3092 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3093 | /* once for us */ |
| 3094 | btrfs_put_ordered_extent(ordered_extent); |
| 3095 | /* once for the tree */ |
| 3096 | btrfs_put_ordered_extent(ordered_extent); |
| 3097 | |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 3098 | return ret; |
| 3099 | } |
| 3100 | |
| 3101 | static void finish_ordered_fn(struct btrfs_work *work) |
| 3102 | { |
| 3103 | struct btrfs_ordered_extent *ordered_extent; |
| 3104 | ordered_extent = container_of(work, struct btrfs_ordered_extent, work); |
| 3105 | btrfs_finish_ordered_io(ordered_extent); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3106 | } |
| 3107 | |
David Sterba | c3988d6 | 2017-02-17 15:18:32 +0100 | [diff] [blame] | 3108 | static void btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end, |
Chris Mason | 211f90e | 2008-07-18 11:56:15 -0400 | [diff] [blame] | 3109 | struct extent_state *state, int uptodate) |
| 3110 | { |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 3111 | struct inode *inode = page->mapping->host; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3112 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 3113 | struct btrfs_ordered_extent *ordered_extent = NULL; |
Liu Bo | 9e0af23 | 2014-08-15 23:36:53 +0800 | [diff] [blame] | 3114 | struct btrfs_workqueue *wq; |
| 3115 | btrfs_work_func_t func; |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 3116 | |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 3117 | trace_btrfs_writepage_end_io_hook(page, start, end, uptodate); |
| 3118 | |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 3119 | ClearPagePrivate2(page); |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 3120 | if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start, |
| 3121 | end - start + 1, uptodate)) |
David Sterba | c3988d6 | 2017-02-17 15:18:32 +0100 | [diff] [blame] | 3122 | return; |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 3123 | |
Nikolay Borisov | 70ddc55 | 2017-02-20 13:50:35 +0200 | [diff] [blame] | 3124 | if (btrfs_is_free_space_inode(BTRFS_I(inode))) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3125 | wq = fs_info->endio_freespace_worker; |
Liu Bo | 9e0af23 | 2014-08-15 23:36:53 +0800 | [diff] [blame] | 3126 | func = btrfs_freespace_write_helper; |
| 3127 | } else { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3128 | wq = fs_info->endio_write_workers; |
Liu Bo | 9e0af23 | 2014-08-15 23:36:53 +0800 | [diff] [blame] | 3129 | func = btrfs_endio_write_helper; |
| 3130 | } |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 3131 | |
Liu Bo | 9e0af23 | 2014-08-15 23:36:53 +0800 | [diff] [blame] | 3132 | btrfs_init_work(&ordered_extent->work, func, finish_ordered_fn, NULL, |
| 3133 | NULL); |
| 3134 | btrfs_queue_work(wq, &ordered_extent->work); |
Chris Mason | 211f90e | 2008-07-18 11:56:15 -0400 | [diff] [blame] | 3135 | } |
| 3136 | |
Miao Xie | dc380ae | 2014-09-12 18:43:55 +0800 | [diff] [blame] | 3137 | static int __readpage_endio_check(struct inode *inode, |
| 3138 | struct btrfs_io_bio *io_bio, |
| 3139 | int icsum, struct page *page, |
| 3140 | int pgoff, u64 start, size_t len) |
| 3141 | { |
| 3142 | char *kaddr; |
| 3143 | u32 csum_expected; |
| 3144 | u32 csum = ~(u32)0; |
Miao Xie | dc380ae | 2014-09-12 18:43:55 +0800 | [diff] [blame] | 3145 | |
| 3146 | csum_expected = *(((u32 *)io_bio->csum) + icsum); |
| 3147 | |
| 3148 | kaddr = kmap_atomic(page); |
| 3149 | csum = btrfs_csum_data(kaddr + pgoff, csum, len); |
Domagoj Tršan | 0b5e3da | 2016-10-27 08:52:33 +0100 | [diff] [blame] | 3150 | btrfs_csum_final(csum, (u8 *)&csum); |
Miao Xie | dc380ae | 2014-09-12 18:43:55 +0800 | [diff] [blame] | 3151 | if (csum != csum_expected) |
| 3152 | goto zeroit; |
| 3153 | |
| 3154 | kunmap_atomic(kaddr); |
| 3155 | return 0; |
| 3156 | zeroit: |
Nikolay Borisov | 0970a22 | 2017-02-20 13:50:53 +0200 | [diff] [blame] | 3157 | btrfs_print_data_csum_error(BTRFS_I(inode), start, csum, csum_expected, |
Qu Wenruo | 6f6b643 | 2017-02-09 10:45:06 +0800 | [diff] [blame] | 3158 | io_bio->mirror_num); |
Miao Xie | dc380ae | 2014-09-12 18:43:55 +0800 | [diff] [blame] | 3159 | memset(kaddr + pgoff, 1, len); |
| 3160 | flush_dcache_page(page); |
| 3161 | kunmap_atomic(kaddr); |
| 3162 | if (csum_expected == 0) |
| 3163 | return 0; |
| 3164 | return -EIO; |
| 3165 | } |
| 3166 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3167 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3168 | * 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] | 3169 | * if there's a match, we allow the bio to finish. If not, the code in |
| 3170 | * extent_io.c will try to find good copies for us. |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3171 | */ |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 3172 | static int btrfs_readpage_end_io_hook(struct btrfs_io_bio *io_bio, |
| 3173 | u64 phy_offset, struct page *page, |
| 3174 | u64 start, u64 end, int mirror) |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 3175 | { |
Miao Xie | 4eee4fa | 2012-12-21 09:17:45 +0000 | [diff] [blame] | 3176 | size_t offset = start - page_offset(page); |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 3177 | struct inode *inode = page->mapping->host; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3178 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | ff79f81 | 2007-10-15 16:22:25 -0400 | [diff] [blame] | 3179 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3180 | |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 3181 | if (PageChecked(page)) { |
| 3182 | ClearPageChecked(page); |
Miao Xie | dc380ae | 2014-09-12 18:43:55 +0800 | [diff] [blame] | 3183 | return 0; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 3184 | } |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 3185 | |
| 3186 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM) |
Miao Xie | dc380ae | 2014-09-12 18:43:55 +0800 | [diff] [blame] | 3187 | return 0; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 3188 | |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 3189 | if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID && |
Chris Mason | 9655d29 | 2009-09-02 15:22:30 -0400 | [diff] [blame] | 3190 | test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) { |
David Sterba | 9116621 | 2016-04-26 23:54:39 +0200 | [diff] [blame] | 3191 | clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM); |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 3192 | return 0; |
| 3193 | } |
| 3194 | |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 3195 | phy_offset >>= inode->i_sb->s_blocksize_bits; |
Miao Xie | dc380ae | 2014-09-12 18:43:55 +0800 | [diff] [blame] | 3196 | return __readpage_endio_check(inode, io_bio, phy_offset, page, offset, |
| 3197 | start, (size_t)(end - start + 1)); |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 3198 | } |
Chris Mason | b888db2b | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3199 | |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 3200 | void btrfs_add_delayed_iput(struct inode *inode) |
| 3201 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3202 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
David Sterba | 8089fe6 | 2015-11-19 14:15:51 +0100 | [diff] [blame] | 3203 | struct btrfs_inode *binode = BTRFS_I(inode); |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 3204 | |
| 3205 | if (atomic_add_unless(&inode->i_count, -1, 1)) |
| 3206 | return; |
| 3207 | |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 3208 | spin_lock(&fs_info->delayed_iput_lock); |
David Sterba | 8089fe6 | 2015-11-19 14:15:51 +0100 | [diff] [blame] | 3209 | if (binode->delayed_iput_count == 0) { |
| 3210 | ASSERT(list_empty(&binode->delayed_iput)); |
| 3211 | list_add_tail(&binode->delayed_iput, &fs_info->delayed_iputs); |
| 3212 | } else { |
| 3213 | binode->delayed_iput_count++; |
| 3214 | } |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 3215 | spin_unlock(&fs_info->delayed_iput_lock); |
| 3216 | } |
| 3217 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3218 | void btrfs_run_delayed_iputs(struct btrfs_fs_info *fs_info) |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 3219 | { |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 3220 | |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 3221 | spin_lock(&fs_info->delayed_iput_lock); |
David Sterba | 8089fe6 | 2015-11-19 14:15:51 +0100 | [diff] [blame] | 3222 | while (!list_empty(&fs_info->delayed_iputs)) { |
| 3223 | struct btrfs_inode *inode; |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 3224 | |
David Sterba | 8089fe6 | 2015-11-19 14:15:51 +0100 | [diff] [blame] | 3225 | inode = list_first_entry(&fs_info->delayed_iputs, |
| 3226 | struct btrfs_inode, delayed_iput); |
| 3227 | if (inode->delayed_iput_count) { |
| 3228 | inode->delayed_iput_count--; |
| 3229 | list_move_tail(&inode->delayed_iput, |
| 3230 | &fs_info->delayed_iputs); |
| 3231 | } else { |
| 3232 | list_del_init(&inode->delayed_iput); |
| 3233 | } |
| 3234 | spin_unlock(&fs_info->delayed_iput_lock); |
| 3235 | iput(&inode->vfs_inode); |
| 3236 | spin_lock(&fs_info->delayed_iput_lock); |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 3237 | } |
David Sterba | 8089fe6 | 2015-11-19 14:15:51 +0100 | [diff] [blame] | 3238 | spin_unlock(&fs_info->delayed_iput_lock); |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 3239 | } |
| 3240 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3241 | /* |
Justin P. Mattock | 42b2aa8 | 2011-11-28 20:31:00 -0800 | [diff] [blame] | 3242 | * This is called in transaction commit time. If there are no orphan |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3243 | * files in the subvolume, it removes orphan item and frees block_rsv |
| 3244 | * structure. |
| 3245 | */ |
| 3246 | void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans, |
| 3247 | struct btrfs_root *root) |
| 3248 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3249 | struct btrfs_fs_info *fs_info = root->fs_info; |
Josef Bacik | 90290e1 | 2011-12-02 15:44:12 -0500 | [diff] [blame] | 3250 | struct btrfs_block_rsv *block_rsv; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3251 | int ret; |
| 3252 | |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 3253 | if (atomic_read(&root->orphan_inodes) || |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3254 | root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) |
| 3255 | return; |
| 3256 | |
Josef Bacik | 90290e1 | 2011-12-02 15:44:12 -0500 | [diff] [blame] | 3257 | spin_lock(&root->orphan_lock); |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 3258 | if (atomic_read(&root->orphan_inodes)) { |
Josef Bacik | 90290e1 | 2011-12-02 15:44:12 -0500 | [diff] [blame] | 3259 | spin_unlock(&root->orphan_lock); |
| 3260 | return; |
| 3261 | } |
| 3262 | |
| 3263 | if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) { |
| 3264 | spin_unlock(&root->orphan_lock); |
| 3265 | return; |
| 3266 | } |
| 3267 | |
| 3268 | block_rsv = root->orphan_block_rsv; |
| 3269 | root->orphan_block_rsv = NULL; |
| 3270 | spin_unlock(&root->orphan_lock); |
| 3271 | |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 3272 | if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state) && |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3273 | btrfs_root_refs(&root->root_item) > 0) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3274 | ret = btrfs_del_orphan_item(trans, fs_info->tree_root, |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3275 | root->root_key.objectid); |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 3276 | if (ret) |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3277 | btrfs_abort_transaction(trans, ret); |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 3278 | else |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 3279 | clear_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, |
| 3280 | &root->state); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3281 | } |
| 3282 | |
Josef Bacik | 90290e1 | 2011-12-02 15:44:12 -0500 | [diff] [blame] | 3283 | if (block_rsv) { |
| 3284 | WARN_ON(block_rsv->size > 0); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3285 | btrfs_free_block_rsv(fs_info, block_rsv); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3286 | } |
| 3287 | } |
| 3288 | |
| 3289 | /* |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3290 | * This creates an orphan entry for the given inode in case something goes |
| 3291 | * wrong in the middle of an unlink/truncate. |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3292 | * |
| 3293 | * NOTE: caller of this function should reserve 5 units of metadata for |
| 3294 | * this function. |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3295 | */ |
Nikolay Borisov | 73f2e54 | 2017-02-20 13:50:59 +0200 | [diff] [blame] | 3296 | int btrfs_orphan_add(struct btrfs_trans_handle *trans, |
| 3297 | struct btrfs_inode *inode) |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3298 | { |
Nikolay Borisov | 73f2e54 | 2017-02-20 13:50:59 +0200 | [diff] [blame] | 3299 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb); |
| 3300 | struct btrfs_root *root = inode->root; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3301 | struct btrfs_block_rsv *block_rsv = NULL; |
| 3302 | int reserve = 0; |
| 3303 | int insert = 0; |
| 3304 | int ret; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3305 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3306 | if (!root->orphan_block_rsv) { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3307 | block_rsv = btrfs_alloc_block_rsv(fs_info, |
| 3308 | BTRFS_BLOCK_RSV_TEMP); |
Tsutomu Itoh | b532402 | 2011-07-19 07:27:20 +0000 | [diff] [blame] | 3309 | if (!block_rsv) |
| 3310 | return -ENOMEM; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3311 | } |
| 3312 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3313 | spin_lock(&root->orphan_lock); |
| 3314 | if (!root->orphan_block_rsv) { |
| 3315 | root->orphan_block_rsv = block_rsv; |
| 3316 | } else if (block_rsv) { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3317 | btrfs_free_block_rsv(fs_info, block_rsv); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3318 | block_rsv = NULL; |
| 3319 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3320 | |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 3321 | if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
Nikolay Borisov | 73f2e54 | 2017-02-20 13:50:59 +0200 | [diff] [blame] | 3322 | &inode->runtime_flags)) { |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3323 | #if 0 |
| 3324 | /* |
| 3325 | * For proper ENOSPC handling, we should do orphan |
| 3326 | * cleanup when mounting. But this introduces backward |
| 3327 | * compatibility issue. |
| 3328 | */ |
| 3329 | if (!xchg(&root->orphan_item_inserted, 1)) |
| 3330 | insert = 2; |
| 3331 | else |
| 3332 | insert = 1; |
| 3333 | #endif |
| 3334 | insert = 1; |
Miao Xie | 321f0e7 | 2012-08-28 22:13:02 -0600 | [diff] [blame] | 3335 | atomic_inc(&root->orphan_inodes); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3336 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3337 | |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 3338 | if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED, |
Nikolay Borisov | 73f2e54 | 2017-02-20 13:50:59 +0200 | [diff] [blame] | 3339 | &inode->runtime_flags)) |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3340 | reserve = 1; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3341 | spin_unlock(&root->orphan_lock); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3342 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3343 | /* grab metadata reservation from transaction handle */ |
| 3344 | if (reserve) { |
| 3345 | ret = btrfs_orphan_reserve_metadata(trans, inode); |
Josef Bacik | 3b6571c | 2016-05-27 13:03:04 -0400 | [diff] [blame] | 3346 | ASSERT(!ret); |
| 3347 | if (ret) { |
| 3348 | atomic_dec(&root->orphan_inodes); |
| 3349 | clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED, |
Nikolay Borisov | 73f2e54 | 2017-02-20 13:50:59 +0200 | [diff] [blame] | 3350 | &inode->runtime_flags); |
Josef Bacik | 3b6571c | 2016-05-27 13:03:04 -0400 | [diff] [blame] | 3351 | if (insert) |
| 3352 | clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
Nikolay Borisov | 73f2e54 | 2017-02-20 13:50:59 +0200 | [diff] [blame] | 3353 | &inode->runtime_flags); |
Josef Bacik | 3b6571c | 2016-05-27 13:03:04 -0400 | [diff] [blame] | 3354 | return ret; |
| 3355 | } |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3356 | } |
| 3357 | |
| 3358 | /* insert an orphan item to track this unlinked/truncated file */ |
| 3359 | if (insert >= 1) { |
Nikolay Borisov | 73f2e54 | 2017-02-20 13:50:59 +0200 | [diff] [blame] | 3360 | ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode)); |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 3361 | if (ret) { |
Filipe David Borba Manana | 703c88e | 2013-09-22 21:54:55 +0100 | [diff] [blame] | 3362 | atomic_dec(&root->orphan_inodes); |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 3363 | if (reserve) { |
| 3364 | clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED, |
Nikolay Borisov | 73f2e54 | 2017-02-20 13:50:59 +0200 | [diff] [blame] | 3365 | &inode->runtime_flags); |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 3366 | btrfs_orphan_release_metadata(inode); |
| 3367 | } |
| 3368 | if (ret != -EEXIST) { |
Josef Bacik | e8e7cff | 2013-08-21 15:54:00 -0400 | [diff] [blame] | 3369 | clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
Nikolay Borisov | 73f2e54 | 2017-02-20 13:50:59 +0200 | [diff] [blame] | 3370 | &inode->runtime_flags); |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3371 | btrfs_abort_transaction(trans, ret); |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 3372 | return ret; |
| 3373 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3374 | } |
| 3375 | ret = 0; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3376 | } |
| 3377 | |
| 3378 | /* insert an orphan item to track subvolume contains orphan files */ |
| 3379 | if (insert >= 2) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3380 | ret = btrfs_insert_orphan_item(trans, fs_info->tree_root, |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3381 | root->root_key.objectid); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3382 | if (ret && ret != -EEXIST) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3383 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3384 | return ret; |
| 3385 | } |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3386 | } |
| 3387 | return 0; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3388 | } |
| 3389 | |
| 3390 | /* |
| 3391 | * We have done the truncate/delete so we can go ahead and remove the orphan |
| 3392 | * item for this particular inode. |
| 3393 | */ |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 3394 | static int btrfs_orphan_del(struct btrfs_trans_handle *trans, |
Nikolay Borisov | 3d6ae7b | 2017-02-20 13:50:58 +0200 | [diff] [blame] | 3395 | struct btrfs_inode *inode) |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3396 | { |
Nikolay Borisov | 3d6ae7b | 2017-02-20 13:50:58 +0200 | [diff] [blame] | 3397 | struct btrfs_root *root = inode->root; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3398 | int delete_item = 0; |
| 3399 | int release_rsv = 0; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3400 | int ret = 0; |
| 3401 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3402 | spin_lock(&root->orphan_lock); |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 3403 | if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
Nikolay Borisov | 3d6ae7b | 2017-02-20 13:50:58 +0200 | [diff] [blame] | 3404 | &inode->runtime_flags)) |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3405 | delete_item = 1; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3406 | |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 3407 | if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED, |
Nikolay Borisov | 3d6ae7b | 2017-02-20 13:50:58 +0200 | [diff] [blame] | 3408 | &inode->runtime_flags)) |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3409 | release_rsv = 1; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3410 | spin_unlock(&root->orphan_lock); |
| 3411 | |
Filipe David Borba Manana | 703c88e | 2013-09-22 21:54:55 +0100 | [diff] [blame] | 3412 | if (delete_item) { |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 3413 | atomic_dec(&root->orphan_inodes); |
Filipe David Borba Manana | 703c88e | 2013-09-22 21:54:55 +0100 | [diff] [blame] | 3414 | if (trans) |
| 3415 | ret = btrfs_del_orphan_item(trans, root, |
Nikolay Borisov | 3d6ae7b | 2017-02-20 13:50:58 +0200 | [diff] [blame] | 3416 | btrfs_ino(inode)); |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 3417 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3418 | |
Filipe David Borba Manana | 703c88e | 2013-09-22 21:54:55 +0100 | [diff] [blame] | 3419 | if (release_rsv) |
| 3420 | btrfs_orphan_release_metadata(inode); |
| 3421 | |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 3422 | return ret; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3423 | } |
| 3424 | |
| 3425 | /* |
| 3426 | * this cleans up any orphans that may be left on the list from the last use |
| 3427 | * of this root. |
| 3428 | */ |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3429 | int btrfs_orphan_cleanup(struct btrfs_root *root) |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3430 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3431 | struct btrfs_fs_info *fs_info = root->fs_info; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3432 | struct btrfs_path *path; |
| 3433 | struct extent_buffer *leaf; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3434 | struct btrfs_key key, found_key; |
| 3435 | struct btrfs_trans_handle *trans; |
| 3436 | struct inode *inode; |
Josef Bacik | 8f6d7f4 | 2011-09-26 15:55:20 -0400 | [diff] [blame] | 3437 | u64 last_objectid = 0; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3438 | int ret = 0, nr_unlink = 0, nr_truncate = 0; |
| 3439 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3440 | if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED)) |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3441 | return 0; |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 3442 | |
| 3443 | path = btrfs_alloc_path(); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3444 | if (!path) { |
| 3445 | ret = -ENOMEM; |
| 3446 | goto out; |
| 3447 | } |
David Sterba | e4058b5 | 2015-11-27 16:31:35 +0100 | [diff] [blame] | 3448 | path->reada = READA_BACK; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3449 | |
| 3450 | key.objectid = BTRFS_ORPHAN_OBJECTID; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 3451 | key.type = BTRFS_ORPHAN_ITEM_KEY; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3452 | key.offset = (u64)-1; |
| 3453 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3454 | while (1) { |
| 3455 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3456 | if (ret < 0) |
| 3457 | goto out; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3458 | |
| 3459 | /* |
| 3460 | * 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] | 3461 | * 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] | 3462 | * find the key and see if we have stuff that matches |
| 3463 | */ |
| 3464 | if (ret > 0) { |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3465 | ret = 0; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3466 | if (path->slots[0] == 0) |
| 3467 | break; |
| 3468 | path->slots[0]--; |
| 3469 | } |
| 3470 | |
| 3471 | /* pull out the item */ |
| 3472 | leaf = path->nodes[0]; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3473 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 3474 | |
| 3475 | /* make sure the item matches what we want */ |
| 3476 | if (found_key.objectid != BTRFS_ORPHAN_OBJECTID) |
| 3477 | break; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 3478 | if (found_key.type != BTRFS_ORPHAN_ITEM_KEY) |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3479 | break; |
| 3480 | |
| 3481 | /* release the path since we're done with it */ |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3482 | btrfs_release_path(path); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3483 | |
| 3484 | /* |
| 3485 | * this is where we are basically btrfs_lookup, without the |
| 3486 | * crossing root thing. we store the inode number in the |
| 3487 | * offset of the orphan item. |
| 3488 | */ |
Josef Bacik | 8f6d7f4 | 2011-09-26 15:55:20 -0400 | [diff] [blame] | 3489 | |
| 3490 | if (found_key.offset == last_objectid) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3491 | btrfs_err(fs_info, |
| 3492 | "Error removing orphan entry, stopping orphan cleanup"); |
Josef Bacik | 8f6d7f4 | 2011-09-26 15:55:20 -0400 | [diff] [blame] | 3493 | ret = -EINVAL; |
| 3494 | goto out; |
| 3495 | } |
| 3496 | |
| 3497 | last_objectid = found_key.offset; |
| 3498 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3499 | found_key.objectid = found_key.offset; |
| 3500 | found_key.type = BTRFS_INODE_ITEM_KEY; |
| 3501 | found_key.offset = 0; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3502 | inode = btrfs_iget(fs_info->sb, &found_key, root, NULL); |
Rusty Russell | 8c6ffba | 2013-07-15 11:20:32 +0930 | [diff] [blame] | 3503 | ret = PTR_ERR_OR_ZERO(inode); |
Filipe Manana | 6771089 | 2016-06-06 11:51:25 +0100 | [diff] [blame] | 3504 | if (ret && ret != -ENOENT) |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3505 | goto out; |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 3506 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3507 | if (ret == -ENOENT && root == fs_info->tree_root) { |
Arne Jansen | f8e9e0b | 2011-12-14 20:12:02 -0500 | [diff] [blame] | 3508 | struct btrfs_root *dead_root; |
| 3509 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 3510 | int is_dead_root = 0; |
| 3511 | |
| 3512 | /* |
| 3513 | * this is an orphan in the tree root. Currently these |
| 3514 | * could come from 2 sources: |
| 3515 | * a) a snapshot deletion in progress |
| 3516 | * b) a free space cache inode |
| 3517 | * We need to distinguish those two, as the snapshot |
| 3518 | * orphan must not get deleted. |
| 3519 | * find_dead_roots already ran before us, so if this |
| 3520 | * is a snapshot deletion, we should find the root |
| 3521 | * in the dead_roots list |
| 3522 | */ |
| 3523 | spin_lock(&fs_info->trans_lock); |
| 3524 | list_for_each_entry(dead_root, &fs_info->dead_roots, |
| 3525 | root_list) { |
| 3526 | if (dead_root->root_key.objectid == |
| 3527 | found_key.objectid) { |
| 3528 | is_dead_root = 1; |
| 3529 | break; |
| 3530 | } |
| 3531 | } |
| 3532 | spin_unlock(&fs_info->trans_lock); |
| 3533 | if (is_dead_root) { |
| 3534 | /* prevent this orphan from being found again */ |
| 3535 | key.offset = found_key.objectid - 1; |
| 3536 | continue; |
| 3537 | } |
| 3538 | } |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 3539 | /* |
| 3540 | * Inode is already gone but the orphan item is still there, |
| 3541 | * kill the orphan item. |
| 3542 | */ |
Filipe Manana | 6771089 | 2016-06-06 11:51:25 +0100 | [diff] [blame] | 3543 | if (ret == -ENOENT) { |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 3544 | trans = btrfs_start_transaction(root, 1); |
| 3545 | if (IS_ERR(trans)) { |
| 3546 | ret = PTR_ERR(trans); |
| 3547 | goto out; |
| 3548 | } |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3549 | btrfs_debug(fs_info, "auto deleting %Lu", |
| 3550 | found_key.objectid); |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 3551 | ret = btrfs_del_orphan_item(trans, root, |
| 3552 | found_key.objectid); |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 3553 | btrfs_end_transaction(trans); |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 3554 | if (ret) |
| 3555 | goto out; |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 3556 | continue; |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3557 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3558 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3559 | /* |
| 3560 | * add this inode to the orphan list so btrfs_orphan_del does |
| 3561 | * the proper thing when we hit it |
| 3562 | */ |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 3563 | set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
| 3564 | &BTRFS_I(inode)->runtime_flags); |
Josef Bacik | 925396e | 2013-02-01 15:57:47 -0500 | [diff] [blame] | 3565 | atomic_inc(&root->orphan_inodes); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3566 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3567 | /* if we have links, this was a truncate, lets do that */ |
| 3568 | if (inode->i_nlink) { |
Dulshani Gunawardhana | fae7f21 | 2013-10-31 10:30:08 +0530 | [diff] [blame] | 3569 | if (WARN_ON(!S_ISREG(inode->i_mode))) { |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 3570 | iput(inode); |
| 3571 | continue; |
| 3572 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3573 | nr_truncate++; |
Josef Bacik | f3fe820 | 2013-01-07 17:03:21 -0500 | [diff] [blame] | 3574 | |
| 3575 | /* 1 for the orphan item deletion. */ |
| 3576 | trans = btrfs_start_transaction(root, 1); |
| 3577 | if (IS_ERR(trans)) { |
Josef Bacik | c69b26b | 2013-06-03 16:51:23 -0400 | [diff] [blame] | 3578 | iput(inode); |
Josef Bacik | f3fe820 | 2013-01-07 17:03:21 -0500 | [diff] [blame] | 3579 | ret = PTR_ERR(trans); |
| 3580 | goto out; |
| 3581 | } |
Nikolay Borisov | 73f2e54 | 2017-02-20 13:50:59 +0200 | [diff] [blame] | 3582 | ret = btrfs_orphan_add(trans, BTRFS_I(inode)); |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 3583 | btrfs_end_transaction(trans); |
Josef Bacik | c69b26b | 2013-06-03 16:51:23 -0400 | [diff] [blame] | 3584 | if (ret) { |
| 3585 | iput(inode); |
Josef Bacik | f3fe820 | 2013-01-07 17:03:21 -0500 | [diff] [blame] | 3586 | goto out; |
Josef Bacik | c69b26b | 2013-06-03 16:51:23 -0400 | [diff] [blame] | 3587 | } |
Josef Bacik | f3fe820 | 2013-01-07 17:03:21 -0500 | [diff] [blame] | 3588 | |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3589 | ret = btrfs_truncate(inode); |
Josef Bacik | 4a7d0f6 | 2013-02-07 16:27:28 -0500 | [diff] [blame] | 3590 | if (ret) |
Nikolay Borisov | 3d6ae7b | 2017-02-20 13:50:58 +0200 | [diff] [blame] | 3591 | btrfs_orphan_del(NULL, BTRFS_I(inode)); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3592 | } else { |
| 3593 | nr_unlink++; |
| 3594 | } |
| 3595 | |
| 3596 | /* this will do delete_inode and everything for us */ |
| 3597 | iput(inode); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3598 | if (ret) |
| 3599 | goto out; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3600 | } |
Miao Xie | 3254c87 | 2011-11-10 20:45:05 -0500 | [diff] [blame] | 3601 | /* release the path since we're done with it */ |
| 3602 | btrfs_release_path(path); |
| 3603 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3604 | root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE; |
| 3605 | |
| 3606 | if (root->orphan_block_rsv) |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3607 | btrfs_block_rsv_release(fs_info, root->orphan_block_rsv, |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3608 | (u64)-1); |
| 3609 | |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 3610 | if (root->orphan_block_rsv || |
| 3611 | test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) { |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 3612 | trans = btrfs_join_transaction(root); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3613 | if (!IS_ERR(trans)) |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 3614 | btrfs_end_transaction(trans); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3615 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3616 | |
| 3617 | if (nr_unlink) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3618 | btrfs_debug(fs_info, "unlinked %d orphans", nr_unlink); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3619 | if (nr_truncate) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3620 | btrfs_debug(fs_info, "truncated %d orphans", nr_truncate); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3621 | |
| 3622 | out: |
| 3623 | if (ret) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3624 | btrfs_err(fs_info, "could not do orphan cleanup %d", ret); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3625 | btrfs_free_path(path); |
| 3626 | return ret; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3627 | } |
| 3628 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3629 | /* |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3630 | * very simple check to peek ahead in the leaf looking for xattrs. If we |
| 3631 | * don't find any xattrs, we know there can't be any acls. |
| 3632 | * |
| 3633 | * slot is the slot the inode is in, objectid is the objectid of the inode |
| 3634 | */ |
| 3635 | 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] | 3636 | int slot, u64 objectid, |
| 3637 | int *first_xattr_slot) |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3638 | { |
| 3639 | u32 nritems = btrfs_header_nritems(leaf); |
| 3640 | struct btrfs_key found_key; |
Josef Bacik | f23b5a5 | 2013-06-19 10:16:26 -0400 | [diff] [blame] | 3641 | static u64 xattr_access = 0; |
| 3642 | static u64 xattr_default = 0; |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3643 | int scanned = 0; |
| 3644 | |
Josef Bacik | f23b5a5 | 2013-06-19 10:16:26 -0400 | [diff] [blame] | 3645 | if (!xattr_access) { |
Andreas Gruenbacher | 97d7929 | 2015-12-02 14:44:35 +0100 | [diff] [blame] | 3646 | xattr_access = btrfs_name_hash(XATTR_NAME_POSIX_ACL_ACCESS, |
| 3647 | strlen(XATTR_NAME_POSIX_ACL_ACCESS)); |
| 3648 | xattr_default = btrfs_name_hash(XATTR_NAME_POSIX_ACL_DEFAULT, |
| 3649 | strlen(XATTR_NAME_POSIX_ACL_DEFAULT)); |
Josef Bacik | f23b5a5 | 2013-06-19 10:16:26 -0400 | [diff] [blame] | 3650 | } |
| 3651 | |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3652 | slot++; |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 3653 | *first_xattr_slot = -1; |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3654 | while (slot < nritems) { |
| 3655 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
| 3656 | |
| 3657 | /* we found a different objectid, there must not be acls */ |
| 3658 | if (found_key.objectid != objectid) |
| 3659 | return 0; |
| 3660 | |
| 3661 | /* we found an xattr, assume we've got an acl */ |
Josef Bacik | f23b5a5 | 2013-06-19 10:16:26 -0400 | [diff] [blame] | 3662 | if (found_key.type == BTRFS_XATTR_ITEM_KEY) { |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 3663 | if (*first_xattr_slot == -1) |
| 3664 | *first_xattr_slot = slot; |
Josef Bacik | f23b5a5 | 2013-06-19 10:16:26 -0400 | [diff] [blame] | 3665 | if (found_key.offset == xattr_access || |
| 3666 | found_key.offset == xattr_default) |
| 3667 | return 1; |
| 3668 | } |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3669 | |
| 3670 | /* |
| 3671 | * we found a key greater than an xattr key, there can't |
| 3672 | * be any acls later on |
| 3673 | */ |
| 3674 | if (found_key.type > BTRFS_XATTR_ITEM_KEY) |
| 3675 | return 0; |
| 3676 | |
| 3677 | slot++; |
| 3678 | scanned++; |
| 3679 | |
| 3680 | /* |
| 3681 | * it goes inode, inode backrefs, xattrs, extents, |
| 3682 | * so if there are a ton of hard links to an inode there can |
| 3683 | * be a lot of backrefs. Don't waste time searching too hard, |
| 3684 | * this is just an optimization |
| 3685 | */ |
| 3686 | if (scanned >= 8) |
| 3687 | break; |
| 3688 | } |
| 3689 | /* we hit the end of the leaf before we found an xattr or |
| 3690 | * something larger than an xattr. We have to assume the inode |
| 3691 | * has acls |
| 3692 | */ |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 3693 | if (*first_xattr_slot == -1) |
| 3694 | *first_xattr_slot = slot; |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3695 | return 1; |
| 3696 | } |
| 3697 | |
| 3698 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3699 | * read an inode from the btree into the in-memory inode |
| 3700 | */ |
Filipe Manana | 6771089 | 2016-06-06 11:51:25 +0100 | [diff] [blame] | 3701 | static int btrfs_read_locked_inode(struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3702 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3703 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3704 | struct btrfs_path *path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3705 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3706 | struct btrfs_inode_item *inode_item; |
| 3707 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 3708 | struct btrfs_key location; |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 3709 | unsigned long ptr; |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3710 | int maybe_acls; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 3711 | u32 rdev; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3712 | int ret; |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 3713 | bool filled = false; |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 3714 | int first_xattr_slot; |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 3715 | |
| 3716 | ret = btrfs_fill_inode(inode, &rdev); |
| 3717 | if (!ret) |
| 3718 | filled = true; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3719 | |
| 3720 | path = btrfs_alloc_path(); |
Filipe Manana | 6771089 | 2016-06-06 11:51:25 +0100 | [diff] [blame] | 3721 | if (!path) { |
| 3722 | ret = -ENOMEM; |
Mark Fasheh | 1748f84 | 2011-07-12 11:25:31 -0700 | [diff] [blame] | 3723 | goto make_bad; |
Filipe Manana | 6771089 | 2016-06-06 11:51:25 +0100 | [diff] [blame] | 3724 | } |
Mark Fasheh | 1748f84 | 2011-07-12 11:25:31 -0700 | [diff] [blame] | 3725 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3726 | memcpy(&location, &BTRFS_I(inode)->location, sizeof(location)); |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 3727 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3728 | ret = btrfs_lookup_inode(NULL, root, path, &location, 0); |
Filipe Manana | 6771089 | 2016-06-06 11:51:25 +0100 | [diff] [blame] | 3729 | if (ret) { |
| 3730 | if (ret > 0) |
| 3731 | ret = -ENOENT; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3732 | goto make_bad; |
Filipe Manana | 6771089 | 2016-06-06 11:51:25 +0100 | [diff] [blame] | 3733 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3734 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3735 | leaf = path->nodes[0]; |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 3736 | |
| 3737 | if (filled) |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 3738 | goto cache_index; |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 3739 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3740 | inode_item = btrfs_item_ptr(leaf, path->slots[0], |
| 3741 | struct btrfs_inode_item); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3742 | inode->i_mode = btrfs_inode_mode(leaf, inode_item); |
Miklos Szeredi | bfe8684 | 2011-10-28 14:13:29 +0200 | [diff] [blame] | 3743 | set_nlink(inode, btrfs_inode_nlink(leaf, inode_item)); |
Eric W. Biederman | 2f2f43d | 2012-02-10 11:05:07 -0800 | [diff] [blame] | 3744 | i_uid_write(inode, btrfs_inode_uid(leaf, inode_item)); |
| 3745 | i_gid_write(inode, btrfs_inode_gid(leaf, inode_item)); |
Nikolay Borisov | 6ef06d2 | 2017-02-20 13:50:34 +0200 | [diff] [blame] | 3746 | 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] | 3747 | |
David Sterba | a937b97 | 2014-12-12 17:39:12 +0100 | [diff] [blame] | 3748 | inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->atime); |
| 3749 | inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->atime); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3750 | |
David Sterba | a937b97 | 2014-12-12 17:39:12 +0100 | [diff] [blame] | 3751 | inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->mtime); |
| 3752 | inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->mtime); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3753 | |
David Sterba | a937b97 | 2014-12-12 17:39:12 +0100 | [diff] [blame] | 3754 | inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->ctime); |
| 3755 | inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->ctime); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3756 | |
chandan r | 9cc97d6 | 2012-07-04 12:48:07 +0530 | [diff] [blame] | 3757 | BTRFS_I(inode)->i_otime.tv_sec = |
| 3758 | btrfs_timespec_sec(leaf, &inode_item->otime); |
| 3759 | BTRFS_I(inode)->i_otime.tv_nsec = |
| 3760 | btrfs_timespec_nsec(leaf, &inode_item->otime); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3761 | |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 3762 | inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item)); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3763 | BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item); |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 3764 | BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item); |
| 3765 | |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 3766 | inode->i_version = btrfs_inode_sequence(leaf, inode_item); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3767 | inode->i_generation = BTRFS_I(inode)->generation; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 3768 | inode->i_rdev = 0; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3769 | rdev = btrfs_inode_rdev(leaf, inode_item); |
| 3770 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 3771 | BTRFS_I(inode)->index_cnt = (u64)-1; |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 3772 | BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item); |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 3773 | |
| 3774 | cache_index: |
Yang Dongsheng | 6e17d30 | 2015-04-09 12:08:43 +0800 | [diff] [blame] | 3775 | /* |
| 3776 | * If we were modified in the current generation and evicted from memory |
| 3777 | * and then re-read we need to do a full sync since we don't have any |
| 3778 | * idea about which extents were modified before we were evicted from |
| 3779 | * cache. |
| 3780 | * |
| 3781 | * This is required for both inode re-read from disk and delayed inode |
| 3782 | * in delayed_nodes_tree. |
| 3783 | */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3784 | if (BTRFS_I(inode)->last_trans == fs_info->generation) |
Yang Dongsheng | 6e17d30 | 2015-04-09 12:08:43 +0800 | [diff] [blame] | 3785 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, |
| 3786 | &BTRFS_I(inode)->runtime_flags); |
| 3787 | |
Filipe Manana | bde6c24 | 2015-07-24 00:00:19 +0100 | [diff] [blame] | 3788 | /* |
| 3789 | * We don't persist the id of the transaction where an unlink operation |
| 3790 | * against the inode was last made. So here we assume the inode might |
| 3791 | * have been evicted, and therefore the exact value of last_unlink_trans |
| 3792 | * lost, and set it to last_trans to avoid metadata inconsistencies |
| 3793 | * between the inode and its parent if the inode is fsync'ed and the log |
| 3794 | * replayed. For example, in the scenario: |
| 3795 | * |
| 3796 | * touch mydir/foo |
| 3797 | * ln mydir/foo mydir/bar |
| 3798 | * sync |
| 3799 | * unlink mydir/bar |
| 3800 | * echo 2 > /proc/sys/vm/drop_caches # evicts inode |
| 3801 | * xfs_io -c fsync mydir/foo |
| 3802 | * <power failure> |
| 3803 | * mount fs, triggers fsync log replay |
| 3804 | * |
| 3805 | * We must make sure that when we fsync our inode foo we also log its |
| 3806 | * parent inode, otherwise after log replay the parent still has the |
| 3807 | * dentry with the "bar" name but our inode foo has a link count of 1 |
| 3808 | * and doesn't have an inode ref with the name "bar" anymore. |
| 3809 | * |
| 3810 | * Setting last_unlink_trans to last_trans is a pessimistic approach, |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 3811 | * but it guarantees correctness at the expense of occasional full |
Filipe Manana | bde6c24 | 2015-07-24 00:00:19 +0100 | [diff] [blame] | 3812 | * transaction commits on fsync if our inode is a directory, or if our |
| 3813 | * inode is not a directory, logging its parent unnecessarily. |
| 3814 | */ |
| 3815 | BTRFS_I(inode)->last_unlink_trans = BTRFS_I(inode)->last_trans; |
| 3816 | |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 3817 | path->slots[0]++; |
| 3818 | if (inode->i_nlink != 1 || |
| 3819 | path->slots[0] >= btrfs_header_nritems(leaf)) |
| 3820 | goto cache_acl; |
| 3821 | |
| 3822 | btrfs_item_key_to_cpu(leaf, &location, path->slots[0]); |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 3823 | if (location.objectid != btrfs_ino(BTRFS_I(inode))) |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 3824 | goto cache_acl; |
| 3825 | |
| 3826 | ptr = btrfs_item_ptr_offset(leaf, path->slots[0]); |
| 3827 | if (location.type == BTRFS_INODE_REF_KEY) { |
| 3828 | struct btrfs_inode_ref *ref; |
| 3829 | |
| 3830 | ref = (struct btrfs_inode_ref *)ptr; |
| 3831 | BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref); |
| 3832 | } else if (location.type == BTRFS_INODE_EXTREF_KEY) { |
| 3833 | struct btrfs_inode_extref *extref; |
| 3834 | |
| 3835 | extref = (struct btrfs_inode_extref *)ptr; |
| 3836 | BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf, |
| 3837 | extref); |
| 3838 | } |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 3839 | cache_acl: |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3840 | /* |
| 3841 | * try to precache a NULL acl entry for files that don't have |
| 3842 | * any xattrs or acls |
| 3843 | */ |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3844 | maybe_acls = acls_after_inode_item(leaf, path->slots[0], |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 3845 | btrfs_ino(BTRFS_I(inode)), &first_xattr_slot); |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 3846 | if (first_xattr_slot != -1) { |
| 3847 | path->slots[0] = first_xattr_slot; |
| 3848 | ret = btrfs_load_inode_props(inode, path); |
| 3849 | if (ret) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3850 | btrfs_err(fs_info, |
David Sterba | 351fd35 | 2014-05-15 16:48:20 +0200 | [diff] [blame] | 3851 | "error loading props for ino %llu (root %llu): %d", |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 3852 | btrfs_ino(BTRFS_I(inode)), |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 3853 | root->root_key.objectid, ret); |
| 3854 | } |
| 3855 | btrfs_free_path(path); |
| 3856 | |
Al Viro | 72c0490 | 2009-06-24 16:58:48 -0400 | [diff] [blame] | 3857 | if (!maybe_acls) |
| 3858 | cache_no_acl(inode); |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3859 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3860 | switch (inode->i_mode & S_IFMT) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3861 | case S_IFREG: |
| 3862 | inode->i_mapping->a_ops = &btrfs_aops; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3863 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3864 | inode->i_fop = &btrfs_file_operations; |
| 3865 | inode->i_op = &btrfs_file_inode_operations; |
| 3866 | break; |
| 3867 | case S_IFDIR: |
| 3868 | inode->i_fop = &btrfs_dir_file_operations; |
Omar Sandoval | 67ade05 | 2017-01-25 17:06:38 -0800 | [diff] [blame] | 3869 | inode->i_op = &btrfs_dir_inode_operations; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3870 | break; |
| 3871 | case S_IFLNK: |
| 3872 | inode->i_op = &btrfs_symlink_inode_operations; |
Al Viro | 21fc61c | 2015-11-17 01:07:57 -0500 | [diff] [blame] | 3873 | inode_nohighmem(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3874 | inode->i_mapping->a_ops = &btrfs_symlink_aops; |
| 3875 | break; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 3876 | default: |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 3877 | inode->i_op = &btrfs_special_inode_operations; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 3878 | init_special_inode(inode, inode->i_mode, rdev); |
| 3879 | break; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3880 | } |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 3881 | |
| 3882 | btrfs_update_iflags(inode); |
Filipe Manana | 6771089 | 2016-06-06 11:51:25 +0100 | [diff] [blame] | 3883 | return 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3884 | |
| 3885 | make_bad: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3886 | btrfs_free_path(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3887 | make_bad_inode(inode); |
Filipe Manana | 6771089 | 2016-06-06 11:51:25 +0100 | [diff] [blame] | 3888 | return ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3889 | } |
| 3890 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3891 | /* |
| 3892 | * given a leaf and an inode, copy the inode fields into the leaf |
| 3893 | */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3894 | static void fill_inode_item(struct btrfs_trans_handle *trans, |
| 3895 | struct extent_buffer *leaf, |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3896 | struct btrfs_inode_item *item, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3897 | struct inode *inode) |
| 3898 | { |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3899 | struct btrfs_map_token token; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3900 | |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3901 | btrfs_init_map_token(&token); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3902 | |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3903 | btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token); |
| 3904 | btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token); |
| 3905 | btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size, |
| 3906 | &token); |
| 3907 | btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token); |
| 3908 | btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3909 | |
David Sterba | a937b97 | 2014-12-12 17:39:12 +0100 | [diff] [blame] | 3910 | btrfs_set_token_timespec_sec(leaf, &item->atime, |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3911 | inode->i_atime.tv_sec, &token); |
David Sterba | a937b97 | 2014-12-12 17:39:12 +0100 | [diff] [blame] | 3912 | btrfs_set_token_timespec_nsec(leaf, &item->atime, |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3913 | inode->i_atime.tv_nsec, &token); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3914 | |
David Sterba | a937b97 | 2014-12-12 17:39:12 +0100 | [diff] [blame] | 3915 | btrfs_set_token_timespec_sec(leaf, &item->mtime, |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3916 | inode->i_mtime.tv_sec, &token); |
David Sterba | a937b97 | 2014-12-12 17:39:12 +0100 | [diff] [blame] | 3917 | btrfs_set_token_timespec_nsec(leaf, &item->mtime, |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3918 | inode->i_mtime.tv_nsec, &token); |
| 3919 | |
David Sterba | a937b97 | 2014-12-12 17:39:12 +0100 | [diff] [blame] | 3920 | btrfs_set_token_timespec_sec(leaf, &item->ctime, |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3921 | inode->i_ctime.tv_sec, &token); |
David Sterba | a937b97 | 2014-12-12 17:39:12 +0100 | [diff] [blame] | 3922 | btrfs_set_token_timespec_nsec(leaf, &item->ctime, |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3923 | inode->i_ctime.tv_nsec, &token); |
| 3924 | |
chandan r | 9cc97d6 | 2012-07-04 12:48:07 +0530 | [diff] [blame] | 3925 | btrfs_set_token_timespec_sec(leaf, &item->otime, |
| 3926 | BTRFS_I(inode)->i_otime.tv_sec, &token); |
| 3927 | btrfs_set_token_timespec_nsec(leaf, &item->otime, |
| 3928 | BTRFS_I(inode)->i_otime.tv_nsec, &token); |
| 3929 | |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3930 | btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode), |
| 3931 | &token); |
| 3932 | btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation, |
| 3933 | &token); |
| 3934 | btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token); |
| 3935 | btrfs_set_token_inode_transid(leaf, item, trans->transid, &token); |
| 3936 | btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token); |
| 3937 | btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token); |
| 3938 | btrfs_set_token_inode_block_group(leaf, item, 0, &token); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3939 | } |
| 3940 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3941 | /* |
| 3942 | * copy everything in the in-memory inode into the btree. |
| 3943 | */ |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 3944 | static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3945 | struct btrfs_root *root, struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3946 | { |
| 3947 | struct btrfs_inode_item *inode_item; |
| 3948 | struct btrfs_path *path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3949 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3950 | int ret; |
| 3951 | |
| 3952 | path = btrfs_alloc_path(); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 3953 | if (!path) |
| 3954 | return -ENOMEM; |
| 3955 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 3956 | path->leave_spinning = 1; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 3957 | ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location, |
| 3958 | 1); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3959 | if (ret) { |
| 3960 | if (ret > 0) |
| 3961 | ret = -ENOENT; |
| 3962 | goto failed; |
| 3963 | } |
| 3964 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3965 | leaf = path->nodes[0]; |
| 3966 | inode_item = btrfs_item_ptr(leaf, path->slots[0], |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 3967 | struct btrfs_inode_item); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3968 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3969 | fill_inode_item(trans, leaf, inode_item, inode); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3970 | btrfs_mark_buffer_dirty(leaf); |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 3971 | btrfs_set_inode_last_trans(trans, inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3972 | ret = 0; |
| 3973 | failed: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3974 | btrfs_free_path(path); |
| 3975 | return ret; |
| 3976 | } |
| 3977 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3978 | /* |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 3979 | * copy everything in the in-memory inode into the btree. |
| 3980 | */ |
| 3981 | noinline int btrfs_update_inode(struct btrfs_trans_handle *trans, |
| 3982 | struct btrfs_root *root, struct inode *inode) |
| 3983 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3984 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 3985 | int ret; |
| 3986 | |
| 3987 | /* |
| 3988 | * If the inode is a free space inode, we can deadlock during commit |
| 3989 | * if we put it into the delayed code. |
| 3990 | * |
| 3991 | * The data relocation inode should also be directly updated |
| 3992 | * without delay |
| 3993 | */ |
Nikolay Borisov | 70ddc55 | 2017-02-20 13:50:35 +0200 | [diff] [blame] | 3994 | if (!btrfs_is_free_space_inode(BTRFS_I(inode)) |
Josef Bacik | 1d52c78 | 2014-09-18 11:30:44 -0400 | [diff] [blame] | 3995 | && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3996 | && !test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) { |
Alexander Block | 8ea05e3 | 2012-07-25 17:35:53 +0200 | [diff] [blame] | 3997 | btrfs_update_root_times(trans, root); |
| 3998 | |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 3999 | ret = btrfs_delayed_update_inode(trans, root, inode); |
| 4000 | if (!ret) |
| 4001 | btrfs_set_inode_last_trans(trans, inode); |
| 4002 | return ret; |
| 4003 | } |
| 4004 | |
| 4005 | return btrfs_update_inode_item(trans, root, inode); |
| 4006 | } |
| 4007 | |
Josef Bacik | be6aef6 | 2012-10-22 15:43:12 -0400 | [diff] [blame] | 4008 | noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans, |
| 4009 | struct btrfs_root *root, |
| 4010 | struct inode *inode) |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 4011 | { |
| 4012 | int ret; |
| 4013 | |
| 4014 | ret = btrfs_update_inode(trans, root, inode); |
| 4015 | if (ret == -ENOSPC) |
| 4016 | return btrfs_update_inode_item(trans, root, inode); |
| 4017 | return ret; |
| 4018 | } |
| 4019 | |
| 4020 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 4021 | * unlink helper that gets used here in inode.c and in the tree logging |
| 4022 | * recovery code. It remove a link in a directory with a given name, and |
| 4023 | * also drops the back refs in the inode to the directory |
| 4024 | */ |
Al Viro | 9298679 | 2011-03-04 17:14:37 +0000 | [diff] [blame] | 4025 | static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans, |
| 4026 | struct btrfs_root *root, |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 4027 | struct btrfs_inode *dir, |
| 4028 | struct btrfs_inode *inode, |
Al Viro | 9298679 | 2011-03-04 17:14:37 +0000 | [diff] [blame] | 4029 | const char *name, int name_len) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4030 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4031 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4032 | struct btrfs_path *path; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4033 | int ret = 0; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4034 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4035 | struct btrfs_dir_item *di; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4036 | struct btrfs_key key; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4037 | u64 index; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4038 | u64 ino = btrfs_ino(inode); |
| 4039 | u64 dir_ino = btrfs_ino(dir); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4040 | |
| 4041 | path = btrfs_alloc_path(); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 4042 | if (!path) { |
| 4043 | ret = -ENOMEM; |
Tsutomu Itoh | 554233a | 2011-02-03 03:16:25 +0000 | [diff] [blame] | 4044 | goto out; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 4045 | } |
| 4046 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 4047 | path->leave_spinning = 1; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4048 | di = btrfs_lookup_dir_item(trans, root, path, dir_ino, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4049 | name, name_len, -1); |
| 4050 | if (IS_ERR(di)) { |
| 4051 | ret = PTR_ERR(di); |
| 4052 | goto err; |
| 4053 | } |
| 4054 | if (!di) { |
| 4055 | ret = -ENOENT; |
| 4056 | goto err; |
| 4057 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4058 | leaf = path->nodes[0]; |
| 4059 | btrfs_dir_item_key_to_cpu(leaf, di, &key); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4060 | ret = btrfs_delete_one_dir_name(trans, root, path, di); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 4061 | if (ret) |
| 4062 | goto err; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4063 | btrfs_release_path(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4064 | |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 4065 | /* |
| 4066 | * If we don't have dir index, we have to get it by looking up |
| 4067 | * the inode ref, since we get the inode ref, remove it directly, |
| 4068 | * it is unnecessary to do delayed deletion. |
| 4069 | * |
| 4070 | * But if we have dir index, needn't search inode ref to get it. |
| 4071 | * Since the inode ref is close to the inode item, it is better |
| 4072 | * that we delay to delete it, and just do this deletion when |
| 4073 | * we update the inode item. |
| 4074 | */ |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 4075 | if (inode->dir_index) { |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 4076 | ret = btrfs_delayed_delete_inode_ref(inode); |
| 4077 | if (!ret) { |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 4078 | index = inode->dir_index; |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 4079 | goto skip_backref; |
| 4080 | } |
| 4081 | } |
| 4082 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4083 | ret = btrfs_del_inode_ref(trans, root, name, name_len, ino, |
| 4084 | dir_ino, &index); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4085 | if (ret) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4086 | btrfs_info(fs_info, |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 4087 | "failed to delete reference to %.*s, inode %llu parent %llu", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 4088 | name_len, name, ino, dir_ino); |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 4089 | btrfs_abort_transaction(trans, ret); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4090 | goto err; |
| 4091 | } |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 4092 | skip_backref: |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4093 | ret = btrfs_delete_delayed_dir_index(trans, fs_info, dir, index); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4094 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 4095 | btrfs_abort_transaction(trans, ret); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4096 | goto err; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4097 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4098 | |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 4099 | ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len, inode, |
| 4100 | dir_ino); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4101 | if (ret != 0 && ret != -ENOENT) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 4102 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4103 | goto err; |
| 4104 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4105 | |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 4106 | ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len, dir, |
| 4107 | index); |
Chris Mason | 6418c96 | 2010-10-30 07:34:24 -0400 | [diff] [blame] | 4108 | if (ret == -ENOENT) |
| 4109 | ret = 0; |
Zach Brown | d4e3991 | 2013-04-02 21:02:16 +0000 | [diff] [blame] | 4110 | else if (ret) |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 4111 | btrfs_abort_transaction(trans, ret); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4112 | err: |
| 4113 | btrfs_free_path(path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4114 | if (ret) |
| 4115 | goto out; |
| 4116 | |
Nikolay Borisov | 6ef06d2 | 2017-02-20 13:50:34 +0200 | [diff] [blame] | 4117 | 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] | 4118 | inode_inc_iversion(&inode->vfs_inode); |
| 4119 | inode_inc_iversion(&dir->vfs_inode); |
| 4120 | inode->vfs_inode.i_ctime = dir->vfs_inode.i_mtime = |
| 4121 | dir->vfs_inode.i_ctime = current_time(&inode->vfs_inode); |
| 4122 | ret = btrfs_update_inode(trans, root, &dir->vfs_inode); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4123 | out: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4124 | return ret; |
| 4125 | } |
| 4126 | |
Al Viro | 9298679 | 2011-03-04 17:14:37 +0000 | [diff] [blame] | 4127 | int btrfs_unlink_inode(struct btrfs_trans_handle *trans, |
| 4128 | struct btrfs_root *root, |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 4129 | struct btrfs_inode *dir, struct btrfs_inode *inode, |
Al Viro | 9298679 | 2011-03-04 17:14:37 +0000 | [diff] [blame] | 4130 | const char *name, int name_len) |
| 4131 | { |
| 4132 | int ret; |
| 4133 | ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len); |
| 4134 | if (!ret) { |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 4135 | drop_nlink(&inode->vfs_inode); |
| 4136 | ret = btrfs_update_inode(trans, root, &inode->vfs_inode); |
Al Viro | 9298679 | 2011-03-04 17:14:37 +0000 | [diff] [blame] | 4137 | } |
| 4138 | return ret; |
| 4139 | } |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4140 | |
| 4141 | /* |
| 4142 | * helper to start transaction for unlink and rmdir. |
| 4143 | * |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 4144 | * unlink and rmdir are special in btrfs, they do not always free space, so |
| 4145 | * if we cannot make our reservations the normal way try and see if there is |
| 4146 | * plenty of slack room in the global reserve to migrate, otherwise we cannot |
| 4147 | * allow the unlink to occur. |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4148 | */ |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 4149 | static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir) |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4150 | { |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4151 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4152 | |
Josef Bacik | e70bea5 | 2011-10-11 14:18:24 -0400 | [diff] [blame] | 4153 | /* |
| 4154 | * 1 for the possible orphan item |
| 4155 | * 1 for the dir item |
| 4156 | * 1 for the dir index |
| 4157 | * 1 for the inode ref |
Josef Bacik | e70bea5 | 2011-10-11 14:18:24 -0400 | [diff] [blame] | 4158 | * 1 for the inode |
| 4159 | */ |
Filipe Manana | 8eab77f | 2015-11-13 23:57:16 +0000 | [diff] [blame] | 4160 | return btrfs_start_transaction_fallback_global_rsv(root, 5, 5); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4161 | } |
| 4162 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4163 | static int btrfs_unlink(struct inode *dir, struct dentry *dentry) |
| 4164 | { |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4165 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4166 | struct btrfs_trans_handle *trans; |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 4167 | struct inode *inode = d_inode(dentry); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4168 | int ret; |
| 4169 | |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 4170 | trans = __unlink_start_trans(dir); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4171 | if (IS_ERR(trans)) |
Josef Bacik | 5df6a9f | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4172 | return PTR_ERR(trans); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4173 | |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 4174 | btrfs_record_unlink_dir(trans, BTRFS_I(dir), BTRFS_I(d_inode(dentry)), |
| 4175 | 0); |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 4176 | |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 4177 | ret = btrfs_unlink_inode(trans, root, BTRFS_I(dir), |
| 4178 | BTRFS_I(d_inode(dentry)), dentry->d_name.name, |
| 4179 | dentry->d_name.len); |
Tsutomu Itoh | b532402 | 2011-07-19 07:27:20 +0000 | [diff] [blame] | 4180 | if (ret) |
| 4181 | goto out; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 4182 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4183 | if (inode->i_nlink == 0) { |
Nikolay Borisov | 73f2e54 | 2017-02-20 13:50:59 +0200 | [diff] [blame] | 4184 | ret = btrfs_orphan_add(trans, BTRFS_I(inode)); |
Tsutomu Itoh | b532402 | 2011-07-19 07:27:20 +0000 | [diff] [blame] | 4185 | if (ret) |
| 4186 | goto out; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4187 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 4188 | |
Tsutomu Itoh | b532402 | 2011-07-19 07:27:20 +0000 | [diff] [blame] | 4189 | out: |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 4190 | btrfs_end_transaction(trans); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4191 | btrfs_btree_balance_dirty(root->fs_info); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4192 | return ret; |
| 4193 | } |
| 4194 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4195 | int btrfs_unlink_subvol(struct btrfs_trans_handle *trans, |
| 4196 | struct btrfs_root *root, |
| 4197 | struct inode *dir, u64 objectid, |
| 4198 | const char *name, int name_len) |
| 4199 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4200 | struct btrfs_fs_info *fs_info = root->fs_info; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4201 | struct btrfs_path *path; |
| 4202 | struct extent_buffer *leaf; |
| 4203 | struct btrfs_dir_item *di; |
| 4204 | struct btrfs_key key; |
| 4205 | u64 index; |
| 4206 | int ret; |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 4207 | u64 dir_ino = btrfs_ino(BTRFS_I(dir)); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4208 | |
| 4209 | path = btrfs_alloc_path(); |
| 4210 | if (!path) |
| 4211 | return -ENOMEM; |
| 4212 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4213 | di = btrfs_lookup_dir_item(trans, root, path, dir_ino, |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4214 | name, name_len, -1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4215 | if (IS_ERR_OR_NULL(di)) { |
| 4216 | if (!di) |
| 4217 | ret = -ENOENT; |
| 4218 | else |
| 4219 | ret = PTR_ERR(di); |
| 4220 | goto out; |
| 4221 | } |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4222 | |
| 4223 | leaf = path->nodes[0]; |
| 4224 | btrfs_dir_item_key_to_cpu(leaf, di, &key); |
| 4225 | WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid); |
| 4226 | ret = btrfs_delete_one_dir_name(trans, root, path, di); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4227 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 4228 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4229 | goto out; |
| 4230 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4231 | btrfs_release_path(path); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4232 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4233 | ret = btrfs_del_root_ref(trans, fs_info, objectid, |
| 4234 | root->root_key.objectid, dir_ino, |
| 4235 | &index, name, name_len); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4236 | if (ret < 0) { |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4237 | if (ret != -ENOENT) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 4238 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4239 | goto out; |
| 4240 | } |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4241 | di = btrfs_search_dir_index_item(root, path, dir_ino, |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4242 | name, name_len); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4243 | if (IS_ERR_OR_NULL(di)) { |
| 4244 | if (!di) |
| 4245 | ret = -ENOENT; |
| 4246 | else |
| 4247 | ret = PTR_ERR(di); |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 4248 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4249 | goto out; |
| 4250 | } |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4251 | |
| 4252 | leaf = path->nodes[0]; |
| 4253 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4254 | btrfs_release_path(path); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4255 | index = key.offset; |
| 4256 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4257 | btrfs_release_path(path); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4258 | |
Nikolay Borisov | e67bbbb | 2017-01-10 20:35:36 +0200 | [diff] [blame] | 4259 | ret = btrfs_delete_delayed_dir_index(trans, fs_info, BTRFS_I(dir), index); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4260 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 4261 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4262 | goto out; |
| 4263 | } |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 4264 | |
Nikolay Borisov | 6ef06d2 | 2017-02-20 13:50:34 +0200 | [diff] [blame] | 4265 | 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] | 4266 | inode_inc_iversion(dir); |
Deepa Dinamani | c2050a4 | 2016-09-14 07:48:06 -0700 | [diff] [blame] | 4267 | dir->i_mtime = dir->i_ctime = current_time(dir); |
Josef Bacik | 5a24e84 | 2012-08-08 10:12:59 -0600 | [diff] [blame] | 4268 | ret = btrfs_update_inode_fallback(trans, root, dir); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4269 | if (ret) |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 4270 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4271 | out: |
Josef Bacik | 71d7aed | 2011-06-14 14:24:32 -0400 | [diff] [blame] | 4272 | btrfs_free_path(path); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4273 | return ret; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4274 | } |
| 4275 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4276 | static int btrfs_rmdir(struct inode *dir, struct dentry *dentry) |
| 4277 | { |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 4278 | struct inode *inode = d_inode(dentry); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 4279 | int err = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4280 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4281 | struct btrfs_trans_handle *trans; |
Filipe Manana | 44f714d | 2016-06-06 16:11:13 +0100 | [diff] [blame] | 4282 | u64 last_unlink_trans; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4283 | |
David Sterba | b3ae244 | 2012-09-13 16:04:34 -0600 | [diff] [blame] | 4284 | if (inode->i_size > BTRFS_EMPTY_DIR_SIZE) |
Yan | 134d451 | 2007-10-25 15:49:25 -0400 | [diff] [blame] | 4285 | return -ENOTEMPTY; |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 4286 | if (btrfs_ino(BTRFS_I(inode)) == BTRFS_FIRST_FREE_OBJECTID) |
David Sterba | b3ae244 | 2012-09-13 16:04:34 -0600 | [diff] [blame] | 4287 | return -EPERM; |
Yan | 134d451 | 2007-10-25 15:49:25 -0400 | [diff] [blame] | 4288 | |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 4289 | trans = __unlink_start_trans(dir); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4290 | if (IS_ERR(trans)) |
Josef Bacik | 5df6a9f | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4291 | return PTR_ERR(trans); |
Josef Bacik | 5df6a9f | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4292 | |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 4293 | if (unlikely(btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4294 | err = btrfs_unlink_subvol(trans, root, dir, |
| 4295 | BTRFS_I(inode)->location.objectid, |
| 4296 | dentry->d_name.name, |
| 4297 | dentry->d_name.len); |
| 4298 | goto out; |
| 4299 | } |
| 4300 | |
Nikolay Borisov | 73f2e54 | 2017-02-20 13:50:59 +0200 | [diff] [blame] | 4301 | err = btrfs_orphan_add(trans, BTRFS_I(inode)); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 4302 | if (err) |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4303 | goto out; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 4304 | |
Filipe Manana | 44f714d | 2016-06-06 16:11:13 +0100 | [diff] [blame] | 4305 | last_unlink_trans = BTRFS_I(inode)->last_unlink_trans; |
| 4306 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4307 | /* now the directory is empty */ |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 4308 | err = btrfs_unlink_inode(trans, root, BTRFS_I(dir), |
| 4309 | BTRFS_I(d_inode(dentry)), dentry->d_name.name, |
| 4310 | dentry->d_name.len); |
Filipe Manana | 44f714d | 2016-06-06 16:11:13 +0100 | [diff] [blame] | 4311 | if (!err) { |
Nikolay Borisov | 6ef06d2 | 2017-02-20 13:50:34 +0200 | [diff] [blame] | 4312 | btrfs_i_size_write(BTRFS_I(inode), 0); |
Filipe Manana | 44f714d | 2016-06-06 16:11:13 +0100 | [diff] [blame] | 4313 | /* |
| 4314 | * Propagate the last_unlink_trans value of the deleted dir to |
| 4315 | * its parent directory. This is to prevent an unrecoverable |
| 4316 | * log tree in the case we do something like this: |
| 4317 | * 1) create dir foo |
| 4318 | * 2) create snapshot under dir foo |
| 4319 | * 3) delete the snapshot |
| 4320 | * 4) rmdir foo |
| 4321 | * 5) mkdir foo |
| 4322 | * 6) fsync foo or some file inside foo |
| 4323 | */ |
| 4324 | if (last_unlink_trans >= trans->transid) |
| 4325 | BTRFS_I(dir)->last_unlink_trans = last_unlink_trans; |
| 4326 | } |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4327 | out: |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 4328 | btrfs_end_transaction(trans); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4329 | btrfs_btree_balance_dirty(root->fs_info); |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 4330 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4331 | return err; |
| 4332 | } |
| 4333 | |
Chris Mason | 28f75a0 | 2015-02-04 06:59:29 -0800 | [diff] [blame] | 4334 | static int truncate_space_check(struct btrfs_trans_handle *trans, |
| 4335 | struct btrfs_root *root, |
| 4336 | u64 bytes_deleted) |
| 4337 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4338 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | 28f75a0 | 2015-02-04 06:59:29 -0800 | [diff] [blame] | 4339 | int ret; |
| 4340 | |
Josef Bacik | dc95f7b | 2016-01-13 11:48:06 -0500 | [diff] [blame] | 4341 | /* |
| 4342 | * This is only used to apply pressure to the enospc system, we don't |
| 4343 | * intend to use this reservation at all. |
| 4344 | */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4345 | bytes_deleted = btrfs_csum_bytes_to_leaves(fs_info, bytes_deleted); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4346 | bytes_deleted *= fs_info->nodesize; |
| 4347 | ret = btrfs_block_rsv_add(root, &fs_info->trans_block_rsv, |
Chris Mason | 28f75a0 | 2015-02-04 06:59:29 -0800 | [diff] [blame] | 4348 | bytes_deleted, BTRFS_RESERVE_NO_FLUSH); |
Josef Bacik | dc95f7b | 2016-01-13 11:48:06 -0500 | [diff] [blame] | 4349 | if (!ret) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4350 | trace_btrfs_space_reservation(fs_info, "transaction", |
Josef Bacik | dc95f7b | 2016-01-13 11:48:06 -0500 | [diff] [blame] | 4351 | trans->transid, |
| 4352 | bytes_deleted, 1); |
Chris Mason | 28f75a0 | 2015-02-04 06:59:29 -0800 | [diff] [blame] | 4353 | trans->bytes_reserved += bytes_deleted; |
Josef Bacik | dc95f7b | 2016-01-13 11:48:06 -0500 | [diff] [blame] | 4354 | } |
Chris Mason | 28f75a0 | 2015-02-04 06:59:29 -0800 | [diff] [blame] | 4355 | return ret; |
| 4356 | |
| 4357 | } |
| 4358 | |
Filipe Manana | 0305cd5 | 2015-10-16 12:34:25 +0100 | [diff] [blame] | 4359 | static int truncate_inline_extent(struct inode *inode, |
| 4360 | struct btrfs_path *path, |
| 4361 | struct btrfs_key *found_key, |
| 4362 | const u64 item_end, |
| 4363 | const u64 new_size) |
| 4364 | { |
| 4365 | struct extent_buffer *leaf = path->nodes[0]; |
| 4366 | int slot = path->slots[0]; |
| 4367 | struct btrfs_file_extent_item *fi; |
| 4368 | u32 size = (u32)(new_size - found_key->offset); |
| 4369 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 4370 | |
| 4371 | fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item); |
| 4372 | |
| 4373 | if (btrfs_file_extent_compression(leaf, fi) != BTRFS_COMPRESS_NONE) { |
| 4374 | loff_t offset = new_size; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 4375 | loff_t page_end = ALIGN(offset, PAGE_SIZE); |
Filipe Manana | 0305cd5 | 2015-10-16 12:34:25 +0100 | [diff] [blame] | 4376 | |
| 4377 | /* |
| 4378 | * Zero out the remaining of the last page of our inline extent, |
| 4379 | * instead of directly truncating our inline extent here - that |
| 4380 | * would be much more complex (decompressing all the data, then |
| 4381 | * compressing the truncated data, which might be bigger than |
| 4382 | * the size of the inline extent, resize the extent, etc). |
| 4383 | * We release the path because to get the page we might need to |
| 4384 | * read the extent item from disk (data not in the page cache). |
| 4385 | */ |
| 4386 | btrfs_release_path(path); |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4387 | return btrfs_truncate_block(inode, offset, page_end - offset, |
| 4388 | 0); |
Filipe Manana | 0305cd5 | 2015-10-16 12:34:25 +0100 | [diff] [blame] | 4389 | } |
| 4390 | |
| 4391 | btrfs_set_file_extent_ram_bytes(leaf, fi, size); |
| 4392 | size = btrfs_file_extent_calc_inline_size(size); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4393 | btrfs_truncate_item(root->fs_info, path, size, 1); |
Filipe Manana | 0305cd5 | 2015-10-16 12:34:25 +0100 | [diff] [blame] | 4394 | |
| 4395 | if (test_bit(BTRFS_ROOT_REF_COWS, &root->state)) |
| 4396 | inode_sub_bytes(inode, item_end + 1 - new_size); |
| 4397 | |
| 4398 | return 0; |
| 4399 | } |
| 4400 | |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 4401 | /* |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4402 | * this can truncate away extent items, csum items and directory items. |
| 4403 | * 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] | 4404 | * any higher than new_size |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4405 | * |
| 4406 | * csum items that cross the new i_size are truncated to the new size |
| 4407 | * as well. |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 4408 | * |
| 4409 | * min_type is the minimum key type to truncate down to. If set to 0, this |
| 4410 | * 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] | 4411 | */ |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4412 | int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans, |
| 4413 | struct btrfs_root *root, |
| 4414 | struct inode *inode, |
| 4415 | u64 new_size, u32 min_type) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4416 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4417 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4418 | struct btrfs_path *path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4419 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4420 | struct btrfs_file_extent_item *fi; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4421 | struct btrfs_key key; |
| 4422 | struct btrfs_key found_key; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4423 | u64 extent_start = 0; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4424 | u64 extent_num_bytes = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4425 | u64 extent_offset = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4426 | u64 item_end = 0; |
Filipe Manana | c1aa457 | 2015-06-20 18:20:09 +0100 | [diff] [blame] | 4427 | u64 last_size = new_size; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4428 | u32 found_type = (u8)-1; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4429 | int found_extent; |
| 4430 | int del_item; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4431 | int pending_del_nr = 0; |
| 4432 | int pending_del_slot = 0; |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4433 | int extent_type = -1; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4434 | int ret; |
| 4435 | int err = 0; |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 4436 | u64 ino = btrfs_ino(BTRFS_I(inode)); |
Chris Mason | 28ed134 | 2014-12-17 09:41:04 -0800 | [diff] [blame] | 4437 | u64 bytes_deleted = 0; |
Josef Bacik | 1262133 | 2015-02-03 07:50:16 -0800 | [diff] [blame] | 4438 | bool be_nice = 0; |
| 4439 | bool should_throttle = 0; |
Chris Mason | 28f75a0 | 2015-02-04 06:59:29 -0800 | [diff] [blame] | 4440 | bool should_end = 0; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4441 | |
| 4442 | BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4443 | |
Chris Mason | 28ed134 | 2014-12-17 09:41:04 -0800 | [diff] [blame] | 4444 | /* |
| 4445 | * for non-free space inodes and ref cows, we want to back off from |
| 4446 | * time to time |
| 4447 | */ |
Nikolay Borisov | 70ddc55 | 2017-02-20 13:50:35 +0200 | [diff] [blame] | 4448 | if (!btrfs_is_free_space_inode(BTRFS_I(inode)) && |
Chris Mason | 28ed134 | 2014-12-17 09:41:04 -0800 | [diff] [blame] | 4449 | test_bit(BTRFS_ROOT_REF_COWS, &root->state)) |
| 4450 | be_nice = 1; |
| 4451 | |
Mark Fasheh | 0eb0e19 | 2011-07-12 16:44:10 -0700 | [diff] [blame] | 4452 | path = btrfs_alloc_path(); |
| 4453 | if (!path) |
| 4454 | return -ENOMEM; |
David Sterba | e4058b5 | 2015-11-27 16:31:35 +0100 | [diff] [blame] | 4455 | path->reada = READA_BACK; |
Mark Fasheh | 0eb0e19 | 2011-07-12 16:44:10 -0700 | [diff] [blame] | 4456 | |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4457 | /* |
| 4458 | * We want to drop from the next block forward in case this new size is |
| 4459 | * not block aligned since we will be keeping the last block of the |
| 4460 | * extent just the way it is. |
| 4461 | */ |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 4462 | if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) || |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4463 | root == fs_info->tree_root) |
Nikolay Borisov | dcdbc05 | 2017-02-20 13:50:45 +0200 | [diff] [blame] | 4464 | btrfs_drop_extent_cache(BTRFS_I(inode), ALIGN(new_size, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4465 | fs_info->sectorsize), |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 4466 | (u64)-1, 0); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4467 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 4468 | /* |
| 4469 | * This function is also used to drop the items in the log tree before |
| 4470 | * we relog the inode, so if root != BTRFS_I(inode)->root, it means |
| 4471 | * it is used to drop the loged items. So we shouldn't kill the delayed |
| 4472 | * items. |
| 4473 | */ |
| 4474 | if (min_type == 0 && root == BTRFS_I(inode)->root) |
Nikolay Borisov | 4ccb5c7 | 2017-01-10 20:35:38 +0200 | [diff] [blame] | 4475 | btrfs_kill_delayed_inode_items(BTRFS_I(inode)); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 4476 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4477 | key.objectid = ino; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4478 | key.offset = (u64)-1; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4479 | key.type = (u8)-1; |
| 4480 | |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4481 | search_again: |
Chris Mason | 28ed134 | 2014-12-17 09:41:04 -0800 | [diff] [blame] | 4482 | /* |
| 4483 | * with a 16K leaf size and 128MB extents, you can actually queue |
| 4484 | * up a huge file in a single leaf. Most of the time that |
| 4485 | * bytes_deleted is > 0, it will be huge by the time we get here |
| 4486 | */ |
Byongho Lee | ee22184 | 2015-12-15 01:42:10 +0900 | [diff] [blame] | 4487 | if (be_nice && bytes_deleted > SZ_32M) { |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 4488 | if (btrfs_should_end_transaction(trans)) { |
Chris Mason | 28ed134 | 2014-12-17 09:41:04 -0800 | [diff] [blame] | 4489 | err = -EAGAIN; |
| 4490 | goto error; |
| 4491 | } |
| 4492 | } |
| 4493 | |
| 4494 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 4495 | path->leave_spinning = 1; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4496 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4497 | if (ret < 0) { |
| 4498 | err = ret; |
| 4499 | goto out; |
| 4500 | } |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4501 | |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4502 | if (ret > 0) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4503 | /* there are no items in the tree for us to truncate, we're |
| 4504 | * done |
| 4505 | */ |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4506 | if (path->slots[0] == 0) |
| 4507 | goto out; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4508 | path->slots[0]--; |
| 4509 | } |
| 4510 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4511 | while (1) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4512 | fi = NULL; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4513 | leaf = path->nodes[0]; |
| 4514 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 4515 | found_type = found_key.type; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4516 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4517 | if (found_key.objectid != ino) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4518 | break; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4519 | |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4520 | if (found_type < min_type) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4521 | break; |
| 4522 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4523 | item_end = found_key.offset; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4524 | if (found_type == BTRFS_EXTENT_DATA_KEY) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4525 | fi = btrfs_item_ptr(leaf, path->slots[0], |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4526 | struct btrfs_file_extent_item); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4527 | extent_type = btrfs_file_extent_type(leaf, fi); |
| 4528 | if (extent_type != BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4529 | item_end += |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4530 | btrfs_file_extent_num_bytes(leaf, fi); |
Liu Bo | 09ed2f1 | 2017-03-10 11:09:48 -0800 | [diff] [blame] | 4531 | |
| 4532 | trace_btrfs_truncate_show_fi_regular( |
| 4533 | BTRFS_I(inode), leaf, fi, |
| 4534 | found_key.offset); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4535 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4536 | item_end += btrfs_file_extent_inline_len(leaf, |
Chris Mason | 514ac8a | 2014-01-03 21:07:00 -0800 | [diff] [blame] | 4537 | path->slots[0], fi); |
Liu Bo | 09ed2f1 | 2017-03-10 11:09:48 -0800 | [diff] [blame] | 4538 | |
| 4539 | trace_btrfs_truncate_show_fi_inline( |
| 4540 | BTRFS_I(inode), leaf, fi, path->slots[0], |
| 4541 | found_key.offset); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4542 | } |
Yan | 008630c | 2007-11-07 13:31:09 -0500 | [diff] [blame] | 4543 | item_end--; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4544 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4545 | if (found_type > min_type) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4546 | del_item = 1; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4547 | } else { |
Filipe Manana | 76b42ab | 2017-02-14 16:56:01 +0000 | [diff] [blame] | 4548 | if (item_end < new_size) |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4549 | break; |
| 4550 | if (found_key.offset >= new_size) |
| 4551 | del_item = 1; |
| 4552 | else |
| 4553 | del_item = 0; |
| 4554 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4555 | found_extent = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4556 | /* FIXME, shrink the extent if the ref count is only 1 */ |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4557 | if (found_type != BTRFS_EXTENT_DATA_KEY) |
| 4558 | goto delete; |
| 4559 | |
Josef Bacik | 7f4f6e0 | 2013-08-29 16:43:28 -0400 | [diff] [blame] | 4560 | if (del_item) |
| 4561 | last_size = found_key.offset; |
| 4562 | else |
| 4563 | last_size = new_size; |
| 4564 | |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4565 | if (extent_type != BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4566 | u64 num_dec; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4567 | extent_start = btrfs_file_extent_disk_bytenr(leaf, fi); |
Miao Xie | f70a9a6b | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 4568 | if (!del_item) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4569 | u64 orig_num_bytes = |
| 4570 | btrfs_file_extent_num_bytes(leaf, fi); |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 4571 | extent_num_bytes = ALIGN(new_size - |
| 4572 | found_key.offset, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4573 | fs_info->sectorsize); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4574 | btrfs_set_file_extent_num_bytes(leaf, fi, |
| 4575 | extent_num_bytes); |
| 4576 | num_dec = (orig_num_bytes - |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 4577 | extent_num_bytes); |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 4578 | if (test_bit(BTRFS_ROOT_REF_COWS, |
| 4579 | &root->state) && |
| 4580 | extent_start != 0) |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 4581 | inode_sub_bytes(inode, num_dec); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4582 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4583 | } else { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4584 | extent_num_bytes = |
| 4585 | btrfs_file_extent_disk_num_bytes(leaf, |
| 4586 | fi); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4587 | extent_offset = found_key.offset - |
| 4588 | btrfs_file_extent_offset(leaf, fi); |
| 4589 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4590 | /* FIXME blocksize != 4096 */ |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 4591 | num_dec = btrfs_file_extent_num_bytes(leaf, fi); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4592 | if (extent_start != 0) { |
| 4593 | found_extent = 1; |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 4594 | if (test_bit(BTRFS_ROOT_REF_COWS, |
| 4595 | &root->state)) |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 4596 | inode_sub_bytes(inode, num_dec); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4597 | } |
| 4598 | } |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 4599 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4600 | /* |
| 4601 | * we can't truncate inline items that have had |
| 4602 | * special encodings |
| 4603 | */ |
| 4604 | if (!del_item && |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4605 | btrfs_file_extent_encryption(leaf, fi) == 0 && |
| 4606 | btrfs_file_extent_other_encoding(leaf, fi) == 0) { |
Chris Mason | 514ac8a | 2014-01-03 21:07:00 -0800 | [diff] [blame] | 4607 | |
| 4608 | /* |
Filipe Manana | 0305cd5 | 2015-10-16 12:34:25 +0100 | [diff] [blame] | 4609 | * Need to release path in order to truncate a |
| 4610 | * compressed extent. So delete any accumulated |
| 4611 | * extent items so far. |
Chris Mason | 514ac8a | 2014-01-03 21:07:00 -0800 | [diff] [blame] | 4612 | */ |
Filipe Manana | 0305cd5 | 2015-10-16 12:34:25 +0100 | [diff] [blame] | 4613 | if (btrfs_file_extent_compression(leaf, fi) != |
| 4614 | BTRFS_COMPRESS_NONE && pending_del_nr) { |
| 4615 | err = btrfs_del_items(trans, root, path, |
| 4616 | pending_del_slot, |
| 4617 | pending_del_nr); |
| 4618 | if (err) { |
| 4619 | btrfs_abort_transaction(trans, |
Filipe Manana | 0305cd5 | 2015-10-16 12:34:25 +0100 | [diff] [blame] | 4620 | err); |
| 4621 | goto error; |
| 4622 | } |
| 4623 | pending_del_nr = 0; |
| 4624 | } |
| 4625 | |
| 4626 | err = truncate_inline_extent(inode, path, |
| 4627 | &found_key, |
| 4628 | item_end, |
| 4629 | new_size); |
| 4630 | if (err) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 4631 | btrfs_abort_transaction(trans, err); |
Filipe Manana | 0305cd5 | 2015-10-16 12:34:25 +0100 | [diff] [blame] | 4632 | goto error; |
| 4633 | } |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 4634 | } else if (test_bit(BTRFS_ROOT_REF_COWS, |
| 4635 | &root->state)) { |
Filipe Manana | 0305cd5 | 2015-10-16 12:34:25 +0100 | [diff] [blame] | 4636 | inode_sub_bytes(inode, item_end + 1 - new_size); |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 4637 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4638 | } |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4639 | delete: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4640 | if (del_item) { |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4641 | if (!pending_del_nr) { |
| 4642 | /* no pending yet, add ourselves */ |
| 4643 | pending_del_slot = path->slots[0]; |
| 4644 | pending_del_nr = 1; |
| 4645 | } else if (pending_del_nr && |
| 4646 | path->slots[0] + 1 == pending_del_slot) { |
| 4647 | /* hop on the pending chunk */ |
| 4648 | pending_del_nr++; |
| 4649 | pending_del_slot = path->slots[0]; |
| 4650 | } else { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4651 | BUG(); |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4652 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4653 | } else { |
| 4654 | break; |
| 4655 | } |
Chris Mason | 28f75a0 | 2015-02-04 06:59:29 -0800 | [diff] [blame] | 4656 | should_throttle = 0; |
| 4657 | |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 4658 | if (found_extent && |
| 4659 | (test_bit(BTRFS_ROOT_REF_COWS, &root->state) || |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4660 | root == fs_info->tree_root)) { |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 4661 | btrfs_set_path_blocking(path); |
Chris Mason | 28ed134 | 2014-12-17 09:41:04 -0800 | [diff] [blame] | 4662 | bytes_deleted += extent_num_bytes; |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4663 | ret = btrfs_free_extent(trans, fs_info, extent_start, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4664 | extent_num_bytes, 0, |
| 4665 | btrfs_header_owner(leaf), |
Filipe Manana | b06c4bf | 2015-10-23 07:52:54 +0100 | [diff] [blame] | 4666 | ino, extent_offset); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4667 | BUG_ON(ret); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4668 | if (btrfs_should_throttle_delayed_refs(trans, fs_info)) |
| 4669 | btrfs_async_run_delayed_refs(fs_info, |
Wang Xiaoguang | dd4b857 | 2016-10-18 15:56:13 +0800 | [diff] [blame] | 4670 | trans->delayed_ref_updates * 2, |
| 4671 | trans->transid, 0); |
Chris Mason | 28f75a0 | 2015-02-04 06:59:29 -0800 | [diff] [blame] | 4672 | if (be_nice) { |
| 4673 | if (truncate_space_check(trans, root, |
| 4674 | extent_num_bytes)) { |
| 4675 | should_end = 1; |
| 4676 | } |
| 4677 | if (btrfs_should_throttle_delayed_refs(trans, |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4678 | fs_info)) |
Chris Mason | 28f75a0 | 2015-02-04 06:59:29 -0800 | [diff] [blame] | 4679 | should_throttle = 1; |
Chris Mason | 28f75a0 | 2015-02-04 06:59:29 -0800 | [diff] [blame] | 4680 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4681 | } |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4682 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4683 | if (found_type == BTRFS_INODE_ITEM_KEY) |
| 4684 | break; |
| 4685 | |
| 4686 | if (path->slots[0] == 0 || |
Josef Bacik | 1262133 | 2015-02-03 07:50:16 -0800 | [diff] [blame] | 4687 | path->slots[0] != pending_del_slot || |
Chris Mason | 28f75a0 | 2015-02-04 06:59:29 -0800 | [diff] [blame] | 4688 | should_throttle || should_end) { |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4689 | if (pending_del_nr) { |
| 4690 | ret = btrfs_del_items(trans, root, path, |
| 4691 | pending_del_slot, |
| 4692 | pending_del_nr); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4693 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 4694 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4695 | goto error; |
| 4696 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4697 | pending_del_nr = 0; |
| 4698 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4699 | btrfs_release_path(path); |
Chris Mason | 28f75a0 | 2015-02-04 06:59:29 -0800 | [diff] [blame] | 4700 | if (should_throttle) { |
Josef Bacik | 1262133 | 2015-02-03 07:50:16 -0800 | [diff] [blame] | 4701 | unsigned long updates = trans->delayed_ref_updates; |
| 4702 | if (updates) { |
| 4703 | trans->delayed_ref_updates = 0; |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4704 | ret = btrfs_run_delayed_refs(trans, |
| 4705 | fs_info, |
| 4706 | updates * 2); |
Josef Bacik | 1262133 | 2015-02-03 07:50:16 -0800 | [diff] [blame] | 4707 | if (ret && !err) |
| 4708 | err = ret; |
| 4709 | } |
| 4710 | } |
Chris Mason | 28f75a0 | 2015-02-04 06:59:29 -0800 | [diff] [blame] | 4711 | /* |
| 4712 | * if we failed to refill our space rsv, bail out |
| 4713 | * and let the transaction restart |
| 4714 | */ |
| 4715 | if (should_end) { |
| 4716 | err = -EAGAIN; |
| 4717 | goto error; |
| 4718 | } |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4719 | goto search_again; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4720 | } else { |
| 4721 | path->slots[0]--; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4722 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4723 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4724 | out: |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4725 | if (pending_del_nr) { |
| 4726 | ret = btrfs_del_items(trans, root, path, pending_del_slot, |
| 4727 | pending_del_nr); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4728 | if (ret) |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 4729 | btrfs_abort_transaction(trans, ret); |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4730 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4731 | error: |
Filipe Manana | 76b42ab | 2017-02-14 16:56:01 +0000 | [diff] [blame] | 4732 | if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) { |
| 4733 | ASSERT(last_size >= new_size); |
| 4734 | if (!err && last_size > new_size) |
| 4735 | last_size = new_size; |
Josef Bacik | 7f4f6e0 | 2013-08-29 16:43:28 -0400 | [diff] [blame] | 4736 | btrfs_ordered_update_i_size(inode, last_size, NULL); |
Filipe Manana | 76b42ab | 2017-02-14 16:56:01 +0000 | [diff] [blame] | 4737 | } |
Chris Mason | 28ed134 | 2014-12-17 09:41:04 -0800 | [diff] [blame] | 4738 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4739 | btrfs_free_path(path); |
Chris Mason | 28ed134 | 2014-12-17 09:41:04 -0800 | [diff] [blame] | 4740 | |
Byongho Lee | ee22184 | 2015-12-15 01:42:10 +0900 | [diff] [blame] | 4741 | if (be_nice && bytes_deleted > SZ_32M) { |
Chris Mason | 28ed134 | 2014-12-17 09:41:04 -0800 | [diff] [blame] | 4742 | unsigned long updates = trans->delayed_ref_updates; |
| 4743 | if (updates) { |
| 4744 | trans->delayed_ref_updates = 0; |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4745 | ret = btrfs_run_delayed_refs(trans, fs_info, |
| 4746 | updates * 2); |
Chris Mason | 28ed134 | 2014-12-17 09:41:04 -0800 | [diff] [blame] | 4747 | if (ret && !err) |
| 4748 | err = ret; |
| 4749 | } |
| 4750 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4751 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4752 | } |
| 4753 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4754 | /* |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4755 | * btrfs_truncate_block - read, zero a chunk and write a block |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 4756 | * @inode - inode that we're zeroing |
| 4757 | * @from - the offset to start zeroing |
| 4758 | * @len - the length to zero, 0 to zero the entire range respective to the |
| 4759 | * offset |
| 4760 | * @front - zero up to the offset instead of from the offset on |
| 4761 | * |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4762 | * 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] | 4763 | * 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] | 4764 | */ |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4765 | 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] | 4766 | int front) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4767 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4768 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 4769 | struct address_space *mapping = inode->i_mapping; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4770 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
| 4771 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4772 | struct extent_state *cached_state = NULL; |
Qu Wenruo | 364ecf3 | 2017-02-27 15:10:38 +0800 | [diff] [blame] | 4773 | struct extent_changeset *data_reserved = NULL; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4774 | char *kaddr; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4775 | u32 blocksize = fs_info->sectorsize; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 4776 | pgoff_t index = from >> PAGE_SHIFT; |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4777 | unsigned offset = from & (blocksize - 1); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4778 | struct page *page; |
Josef Bacik | 3b16a4e | 2011-09-21 15:05:58 -0400 | [diff] [blame] | 4779 | gfp_t mask = btrfs_alloc_write_mask(mapping); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4780 | int ret = 0; |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4781 | u64 block_start; |
| 4782 | u64 block_end; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4783 | |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 4784 | if ((offset & (blocksize - 1)) == 0 && |
| 4785 | (!len || ((len & (blocksize - 1)) == 0))) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4786 | goto out; |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4787 | |
Qu Wenruo | 364ecf3 | 2017-02-27 15:10:38 +0800 | [diff] [blame] | 4788 | ret = btrfs_delalloc_reserve_space(inode, &data_reserved, |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4789 | round_down(from, blocksize), blocksize); |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 4790 | if (ret) |
| 4791 | goto out; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4792 | |
Chris Mason | 211c17f | 2008-05-15 09:13:45 -0400 | [diff] [blame] | 4793 | again: |
Josef Bacik | 3b16a4e | 2011-09-21 15:05:58 -0400 | [diff] [blame] | 4794 | page = find_or_create_page(mapping, index, mask); |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 4795 | if (!page) { |
Qu Wenruo | bc42bda | 2017-02-27 15:10:39 +0800 | [diff] [blame] | 4796 | btrfs_delalloc_release_space(inode, data_reserved, |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4797 | round_down(from, blocksize), |
| 4798 | blocksize); |
Miao Xie | ac6a2b3 | 2012-12-05 10:56:13 +0000 | [diff] [blame] | 4799 | ret = -ENOMEM; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4800 | goto out; |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 4801 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4802 | |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4803 | block_start = round_down(from, blocksize); |
| 4804 | block_end = block_start + blocksize - 1; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4805 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4806 | if (!PageUptodate(page)) { |
| 4807 | ret = btrfs_readpage(NULL, page); |
| 4808 | lock_page(page); |
Chris Mason | 211c17f | 2008-05-15 09:13:45 -0400 | [diff] [blame] | 4809 | if (page->mapping != mapping) { |
| 4810 | unlock_page(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 4811 | put_page(page); |
Chris Mason | 211c17f | 2008-05-15 09:13:45 -0400 | [diff] [blame] | 4812 | goto again; |
| 4813 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4814 | if (!PageUptodate(page)) { |
| 4815 | ret = -EIO; |
Chris Mason | 8964222 | 2008-07-24 09:41:53 -0400 | [diff] [blame] | 4816 | goto out_unlock; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4817 | } |
| 4818 | } |
Chris Mason | 211c17f | 2008-05-15 09:13:45 -0400 | [diff] [blame] | 4819 | wait_on_page_writeback(page); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4820 | |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4821 | lock_extent_bits(io_tree, block_start, block_end, &cached_state); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4822 | set_page_extent_mapped(page); |
| 4823 | |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4824 | ordered = btrfs_lookup_ordered_extent(inode, block_start); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4825 | if (ordered) { |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4826 | unlock_extent_cached(io_tree, block_start, block_end, |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4827 | &cached_state, GFP_NOFS); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4828 | unlock_page(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 4829 | put_page(page); |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 4830 | btrfs_start_ordered_extent(inode, ordered, 1); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4831 | btrfs_put_ordered_extent(ordered); |
| 4832 | goto again; |
| 4833 | } |
| 4834 | |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4835 | clear_extent_bit(&BTRFS_I(inode)->io_tree, block_start, block_end, |
Liu Bo | 9e8a4a8 | 2012-09-05 19:10:51 -0600 | [diff] [blame] | 4836 | EXTENT_DIRTY | EXTENT_DELALLOC | |
| 4837 | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4838 | 0, 0, &cached_state, GFP_NOFS); |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 4839 | |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4840 | ret = btrfs_set_extent_delalloc(inode, block_start, block_end, |
Qu Wenruo | ba8b04c | 2016-07-19 16:50:36 +0800 | [diff] [blame] | 4841 | &cached_state, 0); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4842 | if (ret) { |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4843 | unlock_extent_cached(io_tree, block_start, block_end, |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4844 | &cached_state, GFP_NOFS); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4845 | goto out_unlock; |
| 4846 | } |
| 4847 | |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4848 | if (offset != blocksize) { |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 4849 | if (!len) |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4850 | len = blocksize - offset; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4851 | kaddr = kmap(page); |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 4852 | if (front) |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4853 | memset(kaddr + (block_start - page_offset(page)), |
| 4854 | 0, offset); |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 4855 | else |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4856 | memset(kaddr + (block_start - page_offset(page)) + offset, |
| 4857 | 0, len); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4858 | flush_dcache_page(page); |
| 4859 | kunmap(page); |
| 4860 | } |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 4861 | ClearPageChecked(page); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4862 | set_page_dirty(page); |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4863 | unlock_extent_cached(io_tree, block_start, block_end, &cached_state, |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4864 | GFP_NOFS); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4865 | |
Chris Mason | 8964222 | 2008-07-24 09:41:53 -0400 | [diff] [blame] | 4866 | out_unlock: |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 4867 | if (ret) |
Qu Wenruo | bc42bda | 2017-02-27 15:10:39 +0800 | [diff] [blame] | 4868 | btrfs_delalloc_release_space(inode, data_reserved, block_start, |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4869 | blocksize); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4870 | unlock_page(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 4871 | put_page(page); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4872 | out: |
Qu Wenruo | 364ecf3 | 2017-02-27 15:10:38 +0800 | [diff] [blame] | 4873 | extent_changeset_free(data_reserved); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4874 | return ret; |
| 4875 | } |
| 4876 | |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4877 | static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode, |
| 4878 | u64 offset, u64 len) |
| 4879 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4880 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4881 | struct btrfs_trans_handle *trans; |
| 4882 | int ret; |
| 4883 | |
| 4884 | /* |
| 4885 | * Still need to make sure the inode looks like it's been updated so |
| 4886 | * that any holes get logged if we fsync. |
| 4887 | */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4888 | if (btrfs_fs_incompat(fs_info, NO_HOLES)) { |
| 4889 | BTRFS_I(inode)->last_trans = fs_info->generation; |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4890 | BTRFS_I(inode)->last_sub_trans = root->log_transid; |
| 4891 | BTRFS_I(inode)->last_log_commit = root->last_log_commit; |
| 4892 | return 0; |
| 4893 | } |
| 4894 | |
| 4895 | /* |
| 4896 | * 1 - for the one we're dropping |
| 4897 | * 1 - for the one we're adding |
| 4898 | * 1 - for updating the inode. |
| 4899 | */ |
| 4900 | trans = btrfs_start_transaction(root, 3); |
| 4901 | if (IS_ERR(trans)) |
| 4902 | return PTR_ERR(trans); |
| 4903 | |
| 4904 | ret = btrfs_drop_extents(trans, root, inode, offset, offset + len, 1); |
| 4905 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 4906 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 4907 | btrfs_end_transaction(trans); |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4908 | return ret; |
| 4909 | } |
| 4910 | |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 4911 | ret = btrfs_insert_file_extent(trans, root, btrfs_ino(BTRFS_I(inode)), |
| 4912 | offset, 0, 0, len, 0, len, 0, 0, 0); |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4913 | if (ret) |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 4914 | btrfs_abort_transaction(trans, ret); |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4915 | else |
| 4916 | btrfs_update_inode(trans, root, inode); |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 4917 | btrfs_end_transaction(trans); |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4918 | return ret; |
| 4919 | } |
| 4920 | |
Josef Bacik | 695a0d0 | 2011-03-04 15:46:53 -0500 | [diff] [blame] | 4921 | /* |
| 4922 | * This function puts in dummy file extents for the area we're creating a hole |
| 4923 | * for. So if we are truncating this file to a larger size we need to insert |
| 4924 | * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for |
| 4925 | * the range between oldsize and size |
| 4926 | */ |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 4927 | 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] | 4928 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4929 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4930 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 4931 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4932 | struct extent_map *em = NULL; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4933 | struct extent_state *cached_state = NULL; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4934 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4935 | u64 hole_start = ALIGN(oldsize, fs_info->sectorsize); |
| 4936 | u64 block_end = ALIGN(size, fs_info->sectorsize); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4937 | u64 last_byte; |
| 4938 | u64 cur_offset; |
| 4939 | u64 hole_size; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4940 | int err = 0; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4941 | |
Josef Bacik | a71754f | 2013-06-17 17:14:39 -0400 | [diff] [blame] | 4942 | /* |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4943 | * If our size started in the middle of a block we need to zero out the |
| 4944 | * 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] | 4945 | * expose stale data. |
| 4946 | */ |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4947 | err = btrfs_truncate_block(inode, oldsize, 0, 0); |
Josef Bacik | a71754f | 2013-06-17 17:14:39 -0400 | [diff] [blame] | 4948 | if (err) |
| 4949 | return err; |
| 4950 | |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4951 | if (size <= hole_start) |
| 4952 | return 0; |
| 4953 | |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4954 | while (1) { |
| 4955 | struct btrfs_ordered_extent *ordered; |
Miao Xie | fa7c149 | 2013-09-26 13:15:27 +0800 | [diff] [blame] | 4956 | |
David Sterba | ff13db4 | 2015-12-03 14:30:40 +0100 | [diff] [blame] | 4957 | lock_extent_bits(io_tree, hole_start, block_end - 1, |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 4958 | &cached_state); |
Nikolay Borisov | a776c6f | 2017-02-20 13:50:49 +0200 | [diff] [blame] | 4959 | ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), hole_start, |
Miao Xie | fa7c149 | 2013-09-26 13:15:27 +0800 | [diff] [blame] | 4960 | block_end - hole_start); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4961 | if (!ordered) |
| 4962 | break; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4963 | unlock_extent_cached(io_tree, hole_start, block_end - 1, |
| 4964 | &cached_state, GFP_NOFS); |
Miao Xie | fa7c149 | 2013-09-26 13:15:27 +0800 | [diff] [blame] | 4965 | btrfs_start_ordered_extent(inode, ordered, 1); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4966 | btrfs_put_ordered_extent(ordered); |
| 4967 | } |
| 4968 | |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4969 | cur_offset = hole_start; |
| 4970 | while (1) { |
Nikolay Borisov | fc4f21b1 | 2017-02-20 13:51:06 +0200 | [diff] [blame] | 4971 | em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, cur_offset, |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4972 | block_end - cur_offset, 0); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4973 | if (IS_ERR(em)) { |
| 4974 | err = PTR_ERR(em); |
Zach Brown | f276795 | 2013-01-08 19:37:58 +0000 | [diff] [blame] | 4975 | em = NULL; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4976 | break; |
| 4977 | } |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4978 | last_byte = min(extent_map_end(em), block_end); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4979 | last_byte = ALIGN(last_byte, fs_info->sectorsize); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4980 | if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) { |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4981 | struct extent_map *hole_em; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4982 | hole_size = last_byte - cur_offset; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4983 | |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4984 | err = maybe_insert_hole(root, inode, cur_offset, |
| 4985 | hole_size); |
| 4986 | if (err) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 4987 | break; |
Nikolay Borisov | dcdbc05 | 2017-02-20 13:50:45 +0200 | [diff] [blame] | 4988 | btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset, |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4989 | cur_offset + hole_size - 1, 0); |
| 4990 | hole_em = alloc_extent_map(); |
| 4991 | if (!hole_em) { |
| 4992 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, |
| 4993 | &BTRFS_I(inode)->runtime_flags); |
| 4994 | goto next; |
| 4995 | } |
| 4996 | hole_em->start = cur_offset; |
| 4997 | hole_em->len = hole_size; |
| 4998 | hole_em->orig_start = cur_offset; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4999 | |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 5000 | hole_em->block_start = EXTENT_MAP_HOLE; |
| 5001 | hole_em->block_len = 0; |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 5002 | hole_em->orig_block_len = 0; |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 5003 | hole_em->ram_bytes = hole_size; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5004 | hole_em->bdev = fs_info->fs_devices->latest_bdev; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 5005 | hole_em->compress_type = BTRFS_COMPRESS_NONE; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5006 | hole_em->generation = fs_info->generation; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 5007 | |
| 5008 | while (1) { |
| 5009 | write_lock(&em_tree->lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 5010 | err = add_extent_mapping(em_tree, hole_em, 1); |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 5011 | write_unlock(&em_tree->lock); |
| 5012 | if (err != -EEXIST) |
| 5013 | break; |
Nikolay Borisov | dcdbc05 | 2017-02-20 13:50:45 +0200 | [diff] [blame] | 5014 | btrfs_drop_extent_cache(BTRFS_I(inode), |
| 5015 | cur_offset, |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 5016 | cur_offset + |
| 5017 | hole_size - 1, 0); |
| 5018 | } |
| 5019 | free_extent_map(hole_em); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 5020 | } |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 5021 | next: |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 5022 | free_extent_map(em); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 5023 | em = NULL; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 5024 | cur_offset = last_byte; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5025 | if (cur_offset >= block_end) |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 5026 | break; |
| 5027 | } |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 5028 | free_extent_map(em); |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 5029 | unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state, |
| 5030 | GFP_NOFS); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 5031 | return err; |
| 5032 | } |
| 5033 | |
Eric Sandeen | 3972f26 | 2013-01-12 02:57:22 +0000 | [diff] [blame] | 5034 | static int btrfs_setsize(struct inode *inode, struct iattr *attr) |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5035 | { |
Miao Xie | f4a2f4c | 2011-12-14 20:12:01 -0500 | [diff] [blame] | 5036 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5037 | struct btrfs_trans_handle *trans; |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 5038 | loff_t oldsize = i_size_read(inode); |
Eric Sandeen | 3972f26 | 2013-01-12 02:57:22 +0000 | [diff] [blame] | 5039 | loff_t newsize = attr->ia_size; |
| 5040 | int mask = attr->ia_valid; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5041 | int ret; |
| 5042 | |
Eric Sandeen | 3972f26 | 2013-01-12 02:57:22 +0000 | [diff] [blame] | 5043 | /* |
| 5044 | * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a |
| 5045 | * special case where we need to update the times despite not having |
| 5046 | * these flags set. For all other operations the VFS set these flags |
| 5047 | * explicitly if it wants a timestamp update. |
| 5048 | */ |
Christoph Hellwig | dff6efc | 2013-11-19 07:17:07 -0800 | [diff] [blame] | 5049 | if (newsize != oldsize) { |
| 5050 | inode_inc_iversion(inode); |
| 5051 | if (!(mask & (ATTR_CTIME | ATTR_MTIME))) |
| 5052 | inode->i_ctime = inode->i_mtime = |
Deepa Dinamani | c2050a4 | 2016-09-14 07:48:06 -0700 | [diff] [blame] | 5053 | current_time(inode); |
Christoph Hellwig | dff6efc | 2013-11-19 07:17:07 -0800 | [diff] [blame] | 5054 | } |
Eric Sandeen | 3972f26 | 2013-01-12 02:57:22 +0000 | [diff] [blame] | 5055 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 5056 | if (newsize > oldsize) { |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 5057 | /* |
| 5058 | * Don't do an expanding truncate while snapshoting is ongoing. |
| 5059 | * This is to ensure the snapshot captures a fully consistent |
| 5060 | * state of this file - if the snapshot captures this expanding |
| 5061 | * truncation, it must capture all writes that happened before |
| 5062 | * this truncation. |
| 5063 | */ |
Zhao Lei | 0bc19f90 | 2016-01-06 18:56:36 +0800 | [diff] [blame] | 5064 | btrfs_wait_for_snapshot_creation(root); |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 5065 | ret = btrfs_cont_expand(inode, oldsize, newsize); |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 5066 | if (ret) { |
| 5067 | btrfs_end_write_no_snapshoting(root); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5068 | return ret; |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 5069 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5070 | |
Miao Xie | f4a2f4c | 2011-12-14 20:12:01 -0500 | [diff] [blame] | 5071 | trans = btrfs_start_transaction(root, 1); |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 5072 | if (IS_ERR(trans)) { |
| 5073 | btrfs_end_write_no_snapshoting(root); |
Miao Xie | f4a2f4c | 2011-12-14 20:12:01 -0500 | [diff] [blame] | 5074 | return PTR_ERR(trans); |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 5075 | } |
Miao Xie | f4a2f4c | 2011-12-14 20:12:01 -0500 | [diff] [blame] | 5076 | |
| 5077 | i_size_write(inode, newsize); |
| 5078 | btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL); |
Chandan Rajendra | 27772b6 | 2016-01-21 15:56:03 +0530 | [diff] [blame] | 5079 | pagecache_isize_extended(inode, oldsize, newsize); |
Miao Xie | f4a2f4c | 2011-12-14 20:12:01 -0500 | [diff] [blame] | 5080 | ret = btrfs_update_inode(trans, root, inode); |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 5081 | btrfs_end_write_no_snapshoting(root); |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 5082 | btrfs_end_transaction(trans); |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 5083 | } else { |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5084 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 5085 | /* |
| 5086 | * We're truncating a file that used to have good data down to |
| 5087 | * zero. Make sure it gets into the ordered flush list so that |
| 5088 | * any new writes get down to disk quickly. |
| 5089 | */ |
| 5090 | if (newsize == 0) |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 5091 | set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE, |
| 5092 | &BTRFS_I(inode)->runtime_flags); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5093 | |
Josef Bacik | f3fe820 | 2013-01-07 17:03:21 -0500 | [diff] [blame] | 5094 | /* |
| 5095 | * 1 for the orphan item we're going to add |
| 5096 | * 1 for the orphan item deletion. |
| 5097 | */ |
| 5098 | trans = btrfs_start_transaction(root, 2); |
| 5099 | if (IS_ERR(trans)) |
| 5100 | return PTR_ERR(trans); |
| 5101 | |
| 5102 | /* |
| 5103 | * We need to do this in case we fail at _any_ point during the |
| 5104 | * actual truncate. Once we do the truncate_setsize we could |
| 5105 | * invalidate pages which forces any outstanding ordered io to |
| 5106 | * be instantly completed which will give us extents that need |
| 5107 | * to be truncated. If we fail to get an orphan inode down we |
| 5108 | * could have left over extents that were never meant to live, |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 5109 | * so we need to guarantee from this point on that everything |
Josef Bacik | f3fe820 | 2013-01-07 17:03:21 -0500 | [diff] [blame] | 5110 | * will be consistent. |
| 5111 | */ |
Nikolay Borisov | 73f2e54 | 2017-02-20 13:50:59 +0200 | [diff] [blame] | 5112 | ret = btrfs_orphan_add(trans, BTRFS_I(inode)); |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 5113 | btrfs_end_transaction(trans); |
Josef Bacik | f3fe820 | 2013-01-07 17:03:21 -0500 | [diff] [blame] | 5114 | if (ret) |
| 5115 | return ret; |
| 5116 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 5117 | /* we don't support swapfiles, so vmtruncate shouldn't fail */ |
| 5118 | truncate_setsize(inode, newsize); |
Miao Xie | 2e60a51 | 2013-02-08 07:01:08 +0000 | [diff] [blame] | 5119 | |
| 5120 | /* Disable nonlocked read DIO to avoid the end less truncate */ |
Nikolay Borisov | abcefb1 | 2017-02-20 13:51:10 +0200 | [diff] [blame] | 5121 | btrfs_inode_block_unlocked_dio(BTRFS_I(inode)); |
Miao Xie | 2e60a51 | 2013-02-08 07:01:08 +0000 | [diff] [blame] | 5122 | inode_dio_wait(inode); |
Nikolay Borisov | 0b58170 | 2017-02-20 13:51:11 +0200 | [diff] [blame] | 5123 | btrfs_inode_resume_unlocked_dio(BTRFS_I(inode)); |
Miao Xie | 2e60a51 | 2013-02-08 07:01:08 +0000 | [diff] [blame] | 5124 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 5125 | ret = btrfs_truncate(inode); |
Josef Bacik | 7f4f6e0 | 2013-08-29 16:43:28 -0400 | [diff] [blame] | 5126 | if (ret && inode->i_nlink) { |
| 5127 | int err; |
| 5128 | |
Liu Bo | 19fd2df | 2016-12-01 13:01:02 -0800 | [diff] [blame] | 5129 | /* To get a stable disk_i_size */ |
| 5130 | err = btrfs_wait_ordered_range(inode, 0, (u64)-1); |
| 5131 | if (err) { |
Nikolay Borisov | 3d6ae7b | 2017-02-20 13:50:58 +0200 | [diff] [blame] | 5132 | btrfs_orphan_del(NULL, BTRFS_I(inode)); |
Liu Bo | 19fd2df | 2016-12-01 13:01:02 -0800 | [diff] [blame] | 5133 | return err; |
| 5134 | } |
| 5135 | |
Josef Bacik | 7f4f6e0 | 2013-08-29 16:43:28 -0400 | [diff] [blame] | 5136 | /* |
| 5137 | * failed to truncate, disk_i_size is only adjusted down |
| 5138 | * as we remove extents, so it should represent the true |
| 5139 | * size of the inode, so reset the in memory size and |
| 5140 | * delete our orphan entry. |
| 5141 | */ |
| 5142 | trans = btrfs_join_transaction(root); |
| 5143 | if (IS_ERR(trans)) { |
Nikolay Borisov | 3d6ae7b | 2017-02-20 13:50:58 +0200 | [diff] [blame] | 5144 | btrfs_orphan_del(NULL, BTRFS_I(inode)); |
Josef Bacik | 7f4f6e0 | 2013-08-29 16:43:28 -0400 | [diff] [blame] | 5145 | return ret; |
| 5146 | } |
| 5147 | i_size_write(inode, BTRFS_I(inode)->disk_i_size); |
Nikolay Borisov | 3d6ae7b | 2017-02-20 13:50:58 +0200 | [diff] [blame] | 5148 | err = btrfs_orphan_del(trans, BTRFS_I(inode)); |
Josef Bacik | 7f4f6e0 | 2013-08-29 16:43:28 -0400 | [diff] [blame] | 5149 | if (err) |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 5150 | btrfs_abort_transaction(trans, err); |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 5151 | btrfs_end_transaction(trans); |
Josef Bacik | 7f4f6e0 | 2013-08-29 16:43:28 -0400 | [diff] [blame] | 5152 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5153 | } |
| 5154 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 5155 | return ret; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5156 | } |
| 5157 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5158 | static int btrfs_setattr(struct dentry *dentry, struct iattr *attr) |
| 5159 | { |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 5160 | struct inode *inode = d_inode(dentry); |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 5161 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5162 | int err; |
| 5163 | |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 5164 | if (btrfs_root_readonly(root)) |
| 5165 | return -EROFS; |
| 5166 | |
Jan Kara | 31051c8 | 2016-05-26 16:55:18 +0200 | [diff] [blame] | 5167 | err = setattr_prepare(dentry, attr); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5168 | if (err) |
| 5169 | return err; |
| 5170 | |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 5171 | if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) { |
Eric Sandeen | 3972f26 | 2013-01-12 02:57:22 +0000 | [diff] [blame] | 5172 | err = btrfs_setsize(inode, attr); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5173 | if (err) |
| 5174 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5175 | } |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 5176 | |
Christoph Hellwig | 1025774 | 2010-06-04 11:30:02 +0200 | [diff] [blame] | 5177 | if (attr->ia_valid) { |
| 5178 | setattr_copy(inode, attr); |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 5179 | inode_inc_iversion(inode); |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 5180 | err = btrfs_dirty_inode(inode); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 5181 | |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 5182 | if (!err && attr->ia_valid & ATTR_MODE) |
Christoph Hellwig | 996a710 | 2013-12-20 05:16:43 -0800 | [diff] [blame] | 5183 | err = posix_acl_chmod(inode, inode->i_mode); |
Christoph Hellwig | 1025774 | 2010-06-04 11:30:02 +0200 | [diff] [blame] | 5184 | } |
| 5185 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5186 | return err; |
| 5187 | } |
Chris Mason | 61295eb | 2008-01-14 16:24:38 -0500 | [diff] [blame] | 5188 | |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5189 | /* |
| 5190 | * While truncating the inode pages during eviction, we get the VFS calling |
| 5191 | * btrfs_invalidatepage() against each page of the inode. This is slow because |
| 5192 | * the calls to btrfs_invalidatepage() result in a huge amount of calls to |
| 5193 | * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting |
| 5194 | * extent_state structures over and over, wasting lots of time. |
| 5195 | * |
| 5196 | * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all |
| 5197 | * those expensive operations on a per page basis and do only the ordered io |
| 5198 | * finishing, while we release here the extent_map and extent_state structures, |
| 5199 | * without the excessive merging and splitting. |
| 5200 | */ |
| 5201 | static void evict_inode_truncate_pages(struct inode *inode) |
| 5202 | { |
| 5203 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
| 5204 | struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree; |
| 5205 | struct rb_node *node; |
| 5206 | |
| 5207 | ASSERT(inode->i_state & I_FREEING); |
Johannes Weiner | 91b0abe | 2014-04-03 14:47:49 -0700 | [diff] [blame] | 5208 | truncate_inode_pages_final(&inode->i_data); |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5209 | |
| 5210 | write_lock(&map_tree->lock); |
| 5211 | while (!RB_EMPTY_ROOT(&map_tree->map)) { |
| 5212 | struct extent_map *em; |
| 5213 | |
| 5214 | node = rb_first(&map_tree->map); |
| 5215 | em = rb_entry(node, struct extent_map, rb_node); |
Wang Shilong | 180589e | 2013-12-14 15:27:31 +0800 | [diff] [blame] | 5216 | clear_bit(EXTENT_FLAG_PINNED, &em->flags); |
| 5217 | clear_bit(EXTENT_FLAG_LOGGING, &em->flags); |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5218 | remove_extent_mapping(map_tree, em); |
| 5219 | free_extent_map(em); |
Filipe Manana | 7064dd5 | 2014-08-08 02:47:05 +0100 | [diff] [blame] | 5220 | if (need_resched()) { |
| 5221 | write_unlock(&map_tree->lock); |
| 5222 | cond_resched(); |
| 5223 | write_lock(&map_tree->lock); |
| 5224 | } |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5225 | } |
| 5226 | write_unlock(&map_tree->lock); |
| 5227 | |
Filipe Manana | 6ca0709 | 2015-05-26 00:55:42 +0100 | [diff] [blame] | 5228 | /* |
| 5229 | * Keep looping until we have no more ranges in the io tree. |
| 5230 | * We can have ongoing bios started by readpages (called from readahead) |
Filipe Manana | 9c6429d | 2015-06-10 12:55:41 +0100 | [diff] [blame] | 5231 | * that have their endio callback (extent_io.c:end_bio_extent_readpage) |
| 5232 | * still in progress (unlocked the pages in the bio but did not yet |
| 5233 | * unlocked the ranges in the io tree). Therefore this means some |
Filipe Manana | 6ca0709 | 2015-05-26 00:55:42 +0100 | [diff] [blame] | 5234 | * ranges can still be locked and eviction started because before |
| 5235 | * submitting those bios, which are executed by a separate task (work |
| 5236 | * queue kthread), inode references (inode->i_count) were not taken |
| 5237 | * (which would be dropped in the end io callback of each bio). |
| 5238 | * Therefore here we effectively end up waiting for those bios and |
| 5239 | * anyone else holding locked ranges without having bumped the inode's |
| 5240 | * reference count - if we don't do it, when they access the inode's |
| 5241 | * io_tree to unlock a range it may be too late, leading to an |
| 5242 | * use-after-free issue. |
| 5243 | */ |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5244 | spin_lock(&io_tree->lock); |
| 5245 | while (!RB_EMPTY_ROOT(&io_tree->state)) { |
| 5246 | struct extent_state *state; |
| 5247 | struct extent_state *cached_state = NULL; |
Filipe Manana | 6ca0709 | 2015-05-26 00:55:42 +0100 | [diff] [blame] | 5248 | u64 start; |
| 5249 | u64 end; |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5250 | |
| 5251 | node = rb_first(&io_tree->state); |
| 5252 | state = rb_entry(node, struct extent_state, rb_node); |
Filipe Manana | 6ca0709 | 2015-05-26 00:55:42 +0100 | [diff] [blame] | 5253 | start = state->start; |
| 5254 | end = state->end; |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5255 | spin_unlock(&io_tree->lock); |
| 5256 | |
David Sterba | ff13db4 | 2015-12-03 14:30:40 +0100 | [diff] [blame] | 5257 | lock_extent_bits(io_tree, start, end, &cached_state); |
Qu Wenruo | b9d0b38 | 2015-09-29 10:35:16 +0800 | [diff] [blame] | 5258 | |
| 5259 | /* |
| 5260 | * If still has DELALLOC flag, the extent didn't reach disk, |
| 5261 | * and its reserved space won't be freed by delayed_ref. |
| 5262 | * So we need to free its reserved space here. |
| 5263 | * (Refer to comment in btrfs_invalidatepage, case 2) |
| 5264 | * |
| 5265 | * Note, end is the bytenr of last byte, so we need + 1 here. |
| 5266 | */ |
| 5267 | if (state->state & EXTENT_DELALLOC) |
Qu Wenruo | bc42bda | 2017-02-27 15:10:39 +0800 | [diff] [blame] | 5268 | btrfs_qgroup_free_data(inode, NULL, start, end - start + 1); |
Qu Wenruo | b9d0b38 | 2015-09-29 10:35:16 +0800 | [diff] [blame] | 5269 | |
Filipe Manana | 6ca0709 | 2015-05-26 00:55:42 +0100 | [diff] [blame] | 5270 | clear_extent_bit(io_tree, start, end, |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5271 | EXTENT_LOCKED | EXTENT_DIRTY | |
| 5272 | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING | |
| 5273 | EXTENT_DEFRAG, 1, 1, |
| 5274 | &cached_state, GFP_NOFS); |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5275 | |
Filipe Manana | 7064dd5 | 2014-08-08 02:47:05 +0100 | [diff] [blame] | 5276 | cond_resched(); |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5277 | spin_lock(&io_tree->lock); |
| 5278 | } |
| 5279 | spin_unlock(&io_tree->lock); |
| 5280 | } |
| 5281 | |
Al Viro | bd55597 | 2010-06-07 11:35:40 -0400 | [diff] [blame] | 5282 | void btrfs_evict_inode(struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5283 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5284 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5285 | struct btrfs_trans_handle *trans; |
| 5286 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Josef Bacik | 726c35f | 2011-09-26 15:46:06 -0400 | [diff] [blame] | 5287 | struct btrfs_block_rsv *rsv, *global_rsv; |
Josef Bacik | 3bce876 | 2015-02-24 12:35:51 -0800 | [diff] [blame] | 5288 | int steal_from_global = 0; |
Nikolay Borisov | 3d48d98 | 2016-06-29 09:46:41 +0300 | [diff] [blame] | 5289 | u64 min_size; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5290 | int ret; |
| 5291 | |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 5292 | trace_btrfs_inode_evict(inode); |
| 5293 | |
Nikolay Borisov | 3d48d98 | 2016-06-29 09:46:41 +0300 | [diff] [blame] | 5294 | if (!root) { |
| 5295 | kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode)); |
| 5296 | return; |
| 5297 | } |
| 5298 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5299 | min_size = btrfs_calc_trunc_metadata_size(fs_info, 1); |
Nikolay Borisov | 3d48d98 | 2016-06-29 09:46:41 +0300 | [diff] [blame] | 5300 | |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5301 | evict_inode_truncate_pages(inode); |
| 5302 | |
Stefan Behrens | 69e9c6c | 2013-09-05 16:58:43 +0200 | [diff] [blame] | 5303 | if (inode->i_nlink && |
| 5304 | ((btrfs_root_refs(&root->root_item) != 0 && |
| 5305 | root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) || |
Nikolay Borisov | 70ddc55 | 2017-02-20 13:50:35 +0200 | [diff] [blame] | 5306 | btrfs_is_free_space_inode(BTRFS_I(inode)))) |
Al Viro | bd55597 | 2010-06-07 11:35:40 -0400 | [diff] [blame] | 5307 | goto no_delete; |
| 5308 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5309 | if (is_bad_inode(inode)) { |
Nikolay Borisov | 3d6ae7b | 2017-02-20 13:50:58 +0200 | [diff] [blame] | 5310 | btrfs_orphan_del(NULL, BTRFS_I(inode)); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5311 | goto no_delete; |
| 5312 | } |
Al Viro | bd55597 | 2010-06-07 11:35:40 -0400 | [diff] [blame] | 5313 | /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */ |
Jeff Mahoney | a30e577 | 2015-09-11 21:44:17 -0400 | [diff] [blame] | 5314 | if (!special_file(inode->i_mode)) |
| 5315 | btrfs_wait_ordered_range(inode, 0, (u64)-1); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5316 | |
Nikolay Borisov | 7ab7956 | 2017-02-20 13:50:57 +0200 | [diff] [blame] | 5317 | btrfs_free_io_failure_record(BTRFS_I(inode), 0, (u64)-1); |
Miao Xie | f612496 | 2014-09-12 18:44:04 +0800 | [diff] [blame] | 5318 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5319 | if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) { |
Liu Bo | 6bf0231 | 2012-06-25 21:59:09 -0600 | [diff] [blame] | 5320 | BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 5321 | &BTRFS_I(inode)->runtime_flags)); |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 5322 | goto no_delete; |
| 5323 | } |
| 5324 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5325 | if (inode->i_nlink > 0) { |
Stefan Behrens | 69e9c6c | 2013-09-05 16:58:43 +0200 | [diff] [blame] | 5326 | BUG_ON(btrfs_root_refs(&root->root_item) != 0 && |
| 5327 | root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5328 | goto no_delete; |
| 5329 | } |
| 5330 | |
Nikolay Borisov | aa79021 | 2017-01-10 20:35:40 +0200 | [diff] [blame] | 5331 | ret = btrfs_commit_inode_delayed_inode(BTRFS_I(inode)); |
Miao Xie | 0e8c36a | 2012-12-19 06:59:51 +0000 | [diff] [blame] | 5332 | if (ret) { |
Nikolay Borisov | 3d6ae7b | 2017-02-20 13:50:58 +0200 | [diff] [blame] | 5333 | btrfs_orphan_del(NULL, BTRFS_I(inode)); |
Miao Xie | 0e8c36a | 2012-12-19 06:59:51 +0000 | [diff] [blame] | 5334 | goto no_delete; |
| 5335 | } |
| 5336 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5337 | rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP); |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 5338 | if (!rsv) { |
Nikolay Borisov | 3d6ae7b | 2017-02-20 13:50:58 +0200 | [diff] [blame] | 5339 | btrfs_orphan_del(NULL, BTRFS_I(inode)); |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 5340 | goto no_delete; |
| 5341 | } |
Josef Bacik | 4a33854 | 2011-08-29 11:01:31 -0400 | [diff] [blame] | 5342 | rsv->size = min_size; |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 5343 | rsv->failfast = 1; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5344 | global_rsv = &fs_info->global_block_rsv; |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 5345 | |
Nikolay Borisov | 6ef06d2 | 2017-02-20 13:50:34 +0200 | [diff] [blame] | 5346 | btrfs_i_size_write(BTRFS_I(inode), 0); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5347 | |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 5348 | /* |
Miao Xie | 8407aa4 | 2012-09-07 01:43:32 -0600 | [diff] [blame] | 5349 | * This is a bit simpler than btrfs_truncate since we've already |
| 5350 | * reserved our space for our orphan item in the unlink, so we just |
| 5351 | * need to reserve some slack space in case we add bytes and update |
| 5352 | * inode item when doing the truncate. |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 5353 | */ |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5354 | while (1) { |
Miao Xie | 08e007d | 2012-10-16 11:33:38 +0000 | [diff] [blame] | 5355 | ret = btrfs_block_rsv_refill(root, rsv, min_size, |
| 5356 | BTRFS_RESERVE_FLUSH_LIMIT); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5357 | |
Josef Bacik | 726c35f | 2011-09-26 15:46:06 -0400 | [diff] [blame] | 5358 | /* |
| 5359 | * Try and steal from the global reserve since we will |
| 5360 | * likely not use this space anyway, we want to try as |
| 5361 | * hard as possible to get this to work. |
| 5362 | */ |
| 5363 | if (ret) |
Josef Bacik | 3bce876 | 2015-02-24 12:35:51 -0800 | [diff] [blame] | 5364 | steal_from_global++; |
| 5365 | else |
| 5366 | steal_from_global = 0; |
| 5367 | ret = 0; |
Josef Bacik | 726c35f | 2011-09-26 15:46:06 -0400 | [diff] [blame] | 5368 | |
Josef Bacik | 3bce876 | 2015-02-24 12:35:51 -0800 | [diff] [blame] | 5369 | /* |
| 5370 | * steal_from_global == 0: we reserved stuff, hooray! |
| 5371 | * steal_from_global == 1: we didn't reserve stuff, boo! |
| 5372 | * steal_from_global == 2: we've committed, still not a lot of |
| 5373 | * room but maybe we'll have room in the global reserve this |
| 5374 | * time. |
| 5375 | * steal_from_global == 3: abandon all hope! |
| 5376 | */ |
| 5377 | if (steal_from_global > 2) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5378 | btrfs_warn(fs_info, |
| 5379 | "Could not get space for a delete, will truncate on mount %d", |
| 5380 | ret); |
Nikolay Borisov | 3d6ae7b | 2017-02-20 13:50:58 +0200 | [diff] [blame] | 5381 | btrfs_orphan_del(NULL, BTRFS_I(inode)); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5382 | btrfs_free_block_rsv(fs_info, rsv); |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 5383 | goto no_delete; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5384 | } |
| 5385 | |
Miao Xie | 0e8c36a | 2012-12-19 06:59:51 +0000 | [diff] [blame] | 5386 | trans = btrfs_join_transaction(root); |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 5387 | if (IS_ERR(trans)) { |
Nikolay Borisov | 3d6ae7b | 2017-02-20 13:50:58 +0200 | [diff] [blame] | 5388 | btrfs_orphan_del(NULL, BTRFS_I(inode)); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5389 | btrfs_free_block_rsv(fs_info, rsv); |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 5390 | goto no_delete; |
| 5391 | } |
| 5392 | |
Josef Bacik | 3bce876 | 2015-02-24 12:35:51 -0800 | [diff] [blame] | 5393 | /* |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 5394 | * We can't just steal from the global reserve, we need to make |
Josef Bacik | 3bce876 | 2015-02-24 12:35:51 -0800 | [diff] [blame] | 5395 | * sure there is room to do it, if not we need to commit and try |
| 5396 | * again. |
| 5397 | */ |
| 5398 | if (steal_from_global) { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5399 | if (!btrfs_check_space_for_delayed_refs(trans, fs_info)) |
Josef Bacik | 3bce876 | 2015-02-24 12:35:51 -0800 | [diff] [blame] | 5400 | ret = btrfs_block_rsv_migrate(global_rsv, rsv, |
Josef Bacik | 25d609f | 2016-03-25 13:25:48 -0400 | [diff] [blame] | 5401 | min_size, 0); |
Josef Bacik | 3bce876 | 2015-02-24 12:35:51 -0800 | [diff] [blame] | 5402 | else |
| 5403 | ret = -ENOSPC; |
| 5404 | } |
| 5405 | |
| 5406 | /* |
| 5407 | * Couldn't steal from the global reserve, we have too much |
| 5408 | * pending stuff built up, commit the transaction and try it |
| 5409 | * again. |
| 5410 | */ |
| 5411 | if (ret) { |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 5412 | ret = btrfs_commit_transaction(trans); |
Josef Bacik | 3bce876 | 2015-02-24 12:35:51 -0800 | [diff] [blame] | 5413 | if (ret) { |
Nikolay Borisov | 3d6ae7b | 2017-02-20 13:50:58 +0200 | [diff] [blame] | 5414 | btrfs_orphan_del(NULL, BTRFS_I(inode)); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5415 | btrfs_free_block_rsv(fs_info, rsv); |
Josef Bacik | 3bce876 | 2015-02-24 12:35:51 -0800 | [diff] [blame] | 5416 | goto no_delete; |
| 5417 | } |
| 5418 | continue; |
| 5419 | } else { |
| 5420 | steal_from_global = 0; |
| 5421 | } |
| 5422 | |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 5423 | trans->block_rsv = rsv; |
| 5424 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5425 | ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0); |
Chris Mason | 28ed134 | 2014-12-17 09:41:04 -0800 | [diff] [blame] | 5426 | if (ret != -ENOSPC && ret != -EAGAIN) |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5427 | break; |
| 5428 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5429 | trans->block_rsv = &fs_info->trans_block_rsv; |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 5430 | btrfs_end_transaction(trans); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5431 | trans = NULL; |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5432 | btrfs_btree_balance_dirty(fs_info); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 5433 | } |
| 5434 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5435 | btrfs_free_block_rsv(fs_info, rsv); |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 5436 | |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 5437 | /* |
| 5438 | * Errors here aren't a big deal, it just means we leave orphan items |
| 5439 | * in the tree. They will be cleaned up on the next mount. |
| 5440 | */ |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5441 | if (ret == 0) { |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 5442 | trans->block_rsv = root->orphan_block_rsv; |
Nikolay Borisov | 3d6ae7b | 2017-02-20 13:50:58 +0200 | [diff] [blame] | 5443 | btrfs_orphan_del(trans, BTRFS_I(inode)); |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 5444 | } else { |
Nikolay Borisov | 3d6ae7b | 2017-02-20 13:50:58 +0200 | [diff] [blame] | 5445 | btrfs_orphan_del(NULL, BTRFS_I(inode)); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5446 | } |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 5447 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5448 | trans->block_rsv = &fs_info->trans_block_rsv; |
| 5449 | if (!(root == fs_info->tree_root || |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 5450 | root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)) |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 5451 | btrfs_return_ino(root, btrfs_ino(BTRFS_I(inode))); |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 5452 | |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 5453 | btrfs_end_transaction(trans); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5454 | btrfs_btree_balance_dirty(fs_info); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5455 | no_delete: |
Nikolay Borisov | f48d1cf | 2017-01-10 20:35:39 +0200 | [diff] [blame] | 5456 | btrfs_remove_delayed_node(BTRFS_I(inode)); |
Jan Kara | dbd5768 | 2012-05-03 14:48:02 +0200 | [diff] [blame] | 5457 | clear_inode(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5458 | } |
| 5459 | |
| 5460 | /* |
| 5461 | * this returns the key found in the dir entry in the location pointer. |
| 5462 | * If no dir entries were found, location->objectid is 0. |
| 5463 | */ |
| 5464 | static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry, |
| 5465 | struct btrfs_key *location) |
| 5466 | { |
| 5467 | const char *name = dentry->d_name.name; |
| 5468 | int namelen = dentry->d_name.len; |
| 5469 | struct btrfs_dir_item *di; |
| 5470 | struct btrfs_path *path; |
| 5471 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Yan | 0d9f7f3 | 2007-10-25 15:48:28 -0400 | [diff] [blame] | 5472 | int ret = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5473 | |
| 5474 | path = btrfs_alloc_path(); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 5475 | if (!path) |
| 5476 | return -ENOMEM; |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 5477 | |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 5478 | di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(BTRFS_I(dir)), |
| 5479 | name, namelen, 0); |
Yan | 0d9f7f3 | 2007-10-25 15:48:28 -0400 | [diff] [blame] | 5480 | if (IS_ERR(di)) |
| 5481 | ret = PTR_ERR(di); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5482 | |
David Sterba | c704005 | 2011-04-19 18:00:01 +0200 | [diff] [blame] | 5483 | if (IS_ERR_OR_NULL(di)) |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 5484 | goto out_err; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5485 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5486 | btrfs_dir_item_key_to_cpu(path->nodes[0], di, location); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5487 | out: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5488 | btrfs_free_path(path); |
| 5489 | return ret; |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 5490 | out_err: |
| 5491 | location->objectid = 0; |
| 5492 | goto out; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5493 | } |
| 5494 | |
| 5495 | /* |
| 5496 | * when we hit a tree root in a directory, the btrfs part of the inode |
| 5497 | * needs to be changed to reflect the root directory of the tree root. This |
| 5498 | * is kind of like crossing a mount point. |
| 5499 | */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5500 | static int fixup_tree_root_location(struct btrfs_fs_info *fs_info, |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5501 | struct inode *dir, |
| 5502 | struct dentry *dentry, |
| 5503 | struct btrfs_key *location, |
| 5504 | struct btrfs_root **sub_root) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5505 | { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5506 | struct btrfs_path *path; |
| 5507 | struct btrfs_root *new_root; |
| 5508 | struct btrfs_root_ref *ref; |
| 5509 | struct extent_buffer *leaf; |
David Sterba | 1d4c08e | 2015-01-02 19:36:14 +0100 | [diff] [blame] | 5510 | struct btrfs_key key; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5511 | int ret; |
| 5512 | int err = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5513 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5514 | path = btrfs_alloc_path(); |
| 5515 | if (!path) { |
| 5516 | err = -ENOMEM; |
| 5517 | goto out; |
| 5518 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5519 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5520 | err = -ENOENT; |
David Sterba | 1d4c08e | 2015-01-02 19:36:14 +0100 | [diff] [blame] | 5521 | key.objectid = BTRFS_I(dir)->root->root_key.objectid; |
| 5522 | key.type = BTRFS_ROOT_REF_KEY; |
| 5523 | key.offset = location->objectid; |
| 5524 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5525 | 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] | 5526 | if (ret) { |
| 5527 | if (ret < 0) |
| 5528 | err = ret; |
| 5529 | goto out; |
| 5530 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5531 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5532 | leaf = path->nodes[0]; |
| 5533 | ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref); |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 5534 | if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(BTRFS_I(dir)) || |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5535 | btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len) |
| 5536 | goto out; |
| 5537 | |
| 5538 | ret = memcmp_extent_buffer(leaf, dentry->d_name.name, |
| 5539 | (unsigned long)(ref + 1), |
| 5540 | dentry->d_name.len); |
| 5541 | if (ret) |
| 5542 | goto out; |
| 5543 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 5544 | btrfs_release_path(path); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5545 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5546 | new_root = btrfs_read_fs_root_no_name(fs_info, location); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5547 | if (IS_ERR(new_root)) { |
| 5548 | err = PTR_ERR(new_root); |
| 5549 | goto out; |
| 5550 | } |
| 5551 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5552 | *sub_root = new_root; |
| 5553 | location->objectid = btrfs_root_dirid(&new_root->root_item); |
| 5554 | location->type = BTRFS_INODE_ITEM_KEY; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5555 | location->offset = 0; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5556 | err = 0; |
| 5557 | out: |
| 5558 | btrfs_free_path(path); |
| 5559 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5560 | } |
| 5561 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5562 | static void inode_tree_add(struct inode *inode) |
| 5563 | { |
| 5564 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5565 | struct btrfs_inode *entry; |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 5566 | struct rb_node **p; |
| 5567 | struct rb_node *parent; |
Filipe David Borba Manana | cef2193 | 2013-09-02 12:19:13 +0100 | [diff] [blame] | 5568 | struct rb_node *new = &BTRFS_I(inode)->rb_node; |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 5569 | u64 ino = btrfs_ino(BTRFS_I(inode)); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5570 | |
Al Viro | 1d3382cb | 2010-10-23 15:19:20 -0400 | [diff] [blame] | 5571 | if (inode_unhashed(inode)) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5572 | return; |
Miao Xie | e1409ce | 2013-05-15 07:48:16 +0000 | [diff] [blame] | 5573 | parent = NULL; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5574 | spin_lock(&root->inode_lock); |
Miao Xie | e1409ce | 2013-05-15 07:48:16 +0000 | [diff] [blame] | 5575 | p = &root->inode_tree.rb_node; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5576 | while (*p) { |
| 5577 | parent = *p; |
| 5578 | entry = rb_entry(parent, struct btrfs_inode, rb_node); |
| 5579 | |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 5580 | if (ino < btrfs_ino(BTRFS_I(&entry->vfs_inode))) |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 5581 | p = &parent->rb_left; |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 5582 | else if (ino > btrfs_ino(BTRFS_I(&entry->vfs_inode))) |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 5583 | p = &parent->rb_right; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5584 | else { |
| 5585 | WARN_ON(!(entry->vfs_inode.i_state & |
Al Viro | a4ffdde | 2010-06-02 17:38:30 -0400 | [diff] [blame] | 5586 | (I_WILL_FREE | I_FREEING))); |
Filipe David Borba Manana | cef2193 | 2013-09-02 12:19:13 +0100 | [diff] [blame] | 5587 | rb_replace_node(parent, new, &root->inode_tree); |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 5588 | RB_CLEAR_NODE(parent); |
| 5589 | spin_unlock(&root->inode_lock); |
Filipe David Borba Manana | cef2193 | 2013-09-02 12:19:13 +0100 | [diff] [blame] | 5590 | return; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5591 | } |
| 5592 | } |
Filipe David Borba Manana | cef2193 | 2013-09-02 12:19:13 +0100 | [diff] [blame] | 5593 | rb_link_node(new, parent, p); |
| 5594 | rb_insert_color(new, &root->inode_tree); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5595 | spin_unlock(&root->inode_lock); |
| 5596 | } |
| 5597 | |
| 5598 | static void inode_tree_del(struct inode *inode) |
| 5599 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5600 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5601 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5602 | int empty = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5603 | |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 5604 | spin_lock(&root->inode_lock); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5605 | if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5606 | rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5607 | RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5608 | empty = RB_EMPTY_ROOT(&root->inode_tree); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5609 | } |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 5610 | spin_unlock(&root->inode_lock); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5611 | |
Stefan Behrens | 69e9c6c | 2013-09-05 16:58:43 +0200 | [diff] [blame] | 5612 | if (empty && btrfs_root_refs(&root->root_item) == 0) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5613 | synchronize_srcu(&fs_info->subvol_srcu); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5614 | spin_lock(&root->inode_lock); |
| 5615 | empty = RB_EMPTY_ROOT(&root->inode_tree); |
| 5616 | spin_unlock(&root->inode_lock); |
| 5617 | if (empty) |
| 5618 | btrfs_add_dead_root(root); |
| 5619 | } |
| 5620 | } |
| 5621 | |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 5622 | void btrfs_invalidate_inodes(struct btrfs_root *root) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5623 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5624 | struct btrfs_fs_info *fs_info = root->fs_info; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5625 | struct rb_node *node; |
| 5626 | struct rb_node *prev; |
| 5627 | struct btrfs_inode *entry; |
| 5628 | struct inode *inode; |
| 5629 | u64 objectid = 0; |
| 5630 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5631 | if (!test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) |
Liu Bo | 7813b3d | 2014-02-10 17:37:25 +0800 | [diff] [blame] | 5632 | WARN_ON(btrfs_root_refs(&root->root_item) != 0); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5633 | |
| 5634 | spin_lock(&root->inode_lock); |
| 5635 | again: |
| 5636 | node = root->inode_tree.rb_node; |
| 5637 | prev = NULL; |
| 5638 | while (node) { |
| 5639 | prev = node; |
| 5640 | entry = rb_entry(node, struct btrfs_inode, rb_node); |
| 5641 | |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 5642 | if (objectid < btrfs_ino(BTRFS_I(&entry->vfs_inode))) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5643 | node = node->rb_left; |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 5644 | else if (objectid > btrfs_ino(BTRFS_I(&entry->vfs_inode))) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5645 | node = node->rb_right; |
| 5646 | else |
| 5647 | break; |
| 5648 | } |
| 5649 | if (!node) { |
| 5650 | while (prev) { |
| 5651 | entry = rb_entry(prev, struct btrfs_inode, rb_node); |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 5652 | if (objectid <= btrfs_ino(BTRFS_I(&entry->vfs_inode))) { |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5653 | node = prev; |
| 5654 | break; |
| 5655 | } |
| 5656 | prev = rb_next(prev); |
| 5657 | } |
| 5658 | } |
| 5659 | while (node) { |
| 5660 | entry = rb_entry(node, struct btrfs_inode, rb_node); |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 5661 | objectid = btrfs_ino(BTRFS_I(&entry->vfs_inode)) + 1; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5662 | inode = igrab(&entry->vfs_inode); |
| 5663 | if (inode) { |
| 5664 | spin_unlock(&root->inode_lock); |
| 5665 | if (atomic_read(&inode->i_count) > 1) |
| 5666 | d_prune_aliases(inode); |
| 5667 | /* |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 5668 | * btrfs_drop_inode will have it removed from |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5669 | * the inode cache when its usage count |
| 5670 | * hits zero. |
| 5671 | */ |
| 5672 | iput(inode); |
| 5673 | cond_resched(); |
| 5674 | spin_lock(&root->inode_lock); |
| 5675 | goto again; |
| 5676 | } |
| 5677 | |
| 5678 | if (cond_resched_lock(&root->inode_lock)) |
| 5679 | goto again; |
| 5680 | |
| 5681 | node = rb_next(node); |
| 5682 | } |
| 5683 | spin_unlock(&root->inode_lock); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5684 | } |
| 5685 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5686 | static int btrfs_init_locked_inode(struct inode *inode, void *p) |
| 5687 | { |
| 5688 | struct btrfs_iget_args *args = p; |
Chris Mason | 90d3e59 | 2014-01-09 17:28:00 -0800 | [diff] [blame] | 5689 | inode->i_ino = args->location->objectid; |
| 5690 | memcpy(&BTRFS_I(inode)->location, args->location, |
| 5691 | sizeof(*args->location)); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5692 | BTRFS_I(inode)->root = args->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5693 | return 0; |
| 5694 | } |
| 5695 | |
| 5696 | static int btrfs_find_actor(struct inode *inode, void *opaque) |
| 5697 | { |
| 5698 | struct btrfs_iget_args *args = opaque; |
Chris Mason | 90d3e59 | 2014-01-09 17:28:00 -0800 | [diff] [blame] | 5699 | return args->location->objectid == BTRFS_I(inode)->location.objectid && |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5700 | args->root == BTRFS_I(inode)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5701 | } |
| 5702 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5703 | static struct inode *btrfs_iget_locked(struct super_block *s, |
Chris Mason | 90d3e59 | 2014-01-09 17:28:00 -0800 | [diff] [blame] | 5704 | struct btrfs_key *location, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5705 | struct btrfs_root *root) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5706 | { |
| 5707 | struct inode *inode; |
| 5708 | struct btrfs_iget_args args; |
Chris Mason | 90d3e59 | 2014-01-09 17:28:00 -0800 | [diff] [blame] | 5709 | unsigned long hashval = btrfs_inode_hash(location->objectid, root); |
Filipe David Borba Manana | 778ba82 | 2013-10-06 22:22:33 +0100 | [diff] [blame] | 5710 | |
Chris Mason | 90d3e59 | 2014-01-09 17:28:00 -0800 | [diff] [blame] | 5711 | args.location = location; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5712 | args.root = root; |
| 5713 | |
Filipe David Borba Manana | 778ba82 | 2013-10-06 22:22:33 +0100 | [diff] [blame] | 5714 | inode = iget5_locked(s, hashval, btrfs_find_actor, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5715 | btrfs_init_locked_inode, |
| 5716 | (void *)&args); |
| 5717 | return inode; |
| 5718 | } |
| 5719 | |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 5720 | /* Get an inode object given its location and corresponding root. |
| 5721 | * Returns in *is_new if the inode was read from disk |
| 5722 | */ |
| 5723 | struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location, |
Josef Bacik | 73f7341 | 2009-12-04 17:38:27 +0000 | [diff] [blame] | 5724 | struct btrfs_root *root, int *new) |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 5725 | { |
| 5726 | struct inode *inode; |
| 5727 | |
Chris Mason | 90d3e59 | 2014-01-09 17:28:00 -0800 | [diff] [blame] | 5728 | inode = btrfs_iget_locked(s, location, root); |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 5729 | if (!inode) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5730 | return ERR_PTR(-ENOMEM); |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 5731 | |
| 5732 | if (inode->i_state & I_NEW) { |
Filipe Manana | 6771089 | 2016-06-06 11:51:25 +0100 | [diff] [blame] | 5733 | int ret; |
| 5734 | |
| 5735 | ret = btrfs_read_locked_inode(inode); |
Mark Fasheh | 1748f84 | 2011-07-12 11:25:31 -0700 | [diff] [blame] | 5736 | if (!is_bad_inode(inode)) { |
| 5737 | inode_tree_add(inode); |
| 5738 | unlock_new_inode(inode); |
| 5739 | if (new) |
| 5740 | *new = 1; |
| 5741 | } else { |
Sergei Trofimovich | e0b6d65 | 2011-09-11 10:52:24 -0400 | [diff] [blame] | 5742 | unlock_new_inode(inode); |
| 5743 | iput(inode); |
Filipe Manana | 6771089 | 2016-06-06 11:51:25 +0100 | [diff] [blame] | 5744 | ASSERT(ret < 0); |
| 5745 | inode = ERR_PTR(ret < 0 ? ret : -ESTALE); |
Mark Fasheh | 1748f84 | 2011-07-12 11:25:31 -0700 | [diff] [blame] | 5746 | } |
| 5747 | } |
| 5748 | |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 5749 | return inode; |
| 5750 | } |
| 5751 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5752 | static struct inode *new_simple_dir(struct super_block *s, |
| 5753 | struct btrfs_key *key, |
| 5754 | struct btrfs_root *root) |
| 5755 | { |
| 5756 | struct inode *inode = new_inode(s); |
| 5757 | |
| 5758 | if (!inode) |
| 5759 | return ERR_PTR(-ENOMEM); |
| 5760 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5761 | BTRFS_I(inode)->root = root; |
| 5762 | memcpy(&BTRFS_I(inode)->location, key, sizeof(*key)); |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 5763 | set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5764 | |
| 5765 | inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID; |
Li Zefan | 848cce0 | 2012-02-21 17:04:28 +0800 | [diff] [blame] | 5766 | inode->i_op = &btrfs_dir_ro_inode_operations; |
Omar Sandoval | 1fdf419 | 2017-01-25 17:06:39 -0800 | [diff] [blame] | 5767 | inode->i_opflags &= ~IOP_XATTR; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5768 | inode->i_fop = &simple_dir_operations; |
| 5769 | inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO; |
Deepa Dinamani | c2050a4 | 2016-09-14 07:48:06 -0700 | [diff] [blame] | 5770 | inode->i_mtime = current_time(inode); |
chandan r | 9cc97d6 | 2012-07-04 12:48:07 +0530 | [diff] [blame] | 5771 | inode->i_atime = inode->i_mtime; |
| 5772 | inode->i_ctime = inode->i_mtime; |
| 5773 | BTRFS_I(inode)->i_otime = inode->i_mtime; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5774 | |
| 5775 | return inode; |
| 5776 | } |
| 5777 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5778 | struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5779 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5780 | struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5781 | struct inode *inode; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5782 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5783 | struct btrfs_root *sub_root = root; |
| 5784 | struct btrfs_key location; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5785 | int index; |
Josef Bacik | b4aff1f | 2011-06-28 16:18:59 -0400 | [diff] [blame] | 5786 | int ret = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5787 | |
| 5788 | if (dentry->d_name.len > BTRFS_NAME_LEN) |
| 5789 | return ERR_PTR(-ENAMETOOLONG); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5790 | |
Jeff Layton | 39e3c95 | 2012-11-28 11:30:53 -0500 | [diff] [blame] | 5791 | ret = btrfs_inode_by_name(dir, dentry, &location); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5792 | if (ret < 0) |
| 5793 | return ERR_PTR(ret); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5794 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5795 | if (location.objectid == 0) |
Tsutomu Itoh | 5662344b3 | 2013-12-13 09:51:42 +0900 | [diff] [blame] | 5796 | return ERR_PTR(-ENOENT); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5797 | |
| 5798 | if (location.type == BTRFS_INODE_ITEM_KEY) { |
Josef Bacik | 73f7341 | 2009-12-04 17:38:27 +0000 | [diff] [blame] | 5799 | inode = btrfs_iget(dir->i_sb, &location, root, NULL); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5800 | return inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5801 | } |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5802 | |
| 5803 | BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY); |
| 5804 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5805 | index = srcu_read_lock(&fs_info->subvol_srcu); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5806 | ret = fixup_tree_root_location(fs_info, dir, dentry, |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5807 | &location, &sub_root); |
| 5808 | if (ret < 0) { |
| 5809 | if (ret != -ENOENT) |
| 5810 | inode = ERR_PTR(ret); |
| 5811 | else |
| 5812 | inode = new_simple_dir(dir->i_sb, &location, sub_root); |
| 5813 | } else { |
Josef Bacik | 73f7341 | 2009-12-04 17:38:27 +0000 | [diff] [blame] | 5814 | inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5815 | } |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5816 | srcu_read_unlock(&fs_info->subvol_srcu, index); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5817 | |
Julia Lawall | 34d19ba | 2011-01-24 19:55:19 +0000 | [diff] [blame] | 5818 | if (!IS_ERR(inode) && root != sub_root) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5819 | down_read(&fs_info->cleanup_work_sem); |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 5820 | if (!(inode->i_sb->s_flags & MS_RDONLY)) |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 5821 | ret = btrfs_orphan_cleanup(sub_root); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5822 | up_read(&fs_info->cleanup_work_sem); |
Josef Bacik | 01cd336 | 2013-06-03 21:39:49 -0400 | [diff] [blame] | 5823 | if (ret) { |
| 5824 | iput(inode); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 5825 | inode = ERR_PTR(ret); |
Josef Bacik | 01cd336 | 2013-06-03 21:39:49 -0400 | [diff] [blame] | 5826 | } |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 5827 | } |
| 5828 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5829 | return inode; |
| 5830 | } |
| 5831 | |
Nick Piggin | fe15ce4 | 2011-01-07 17:49:23 +1100 | [diff] [blame] | 5832 | static int btrfs_dentry_delete(const struct dentry *dentry) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5833 | { |
| 5834 | struct btrfs_root *root; |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 5835 | struct inode *inode = d_inode(dentry); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5836 | |
Li Zefan | 848cce0 | 2012-02-21 17:04:28 +0800 | [diff] [blame] | 5837 | if (!inode && !IS_ROOT(dentry)) |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 5838 | inode = d_inode(dentry->d_parent); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5839 | |
Li Zefan | 848cce0 | 2012-02-21 17:04:28 +0800 | [diff] [blame] | 5840 | if (inode) { |
| 5841 | root = BTRFS_I(inode)->root; |
Yan, Zheng | efefb14 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5842 | if (btrfs_root_refs(&root->root_item) == 0) |
| 5843 | return 1; |
Li Zefan | 848cce0 | 2012-02-21 17:04:28 +0800 | [diff] [blame] | 5844 | |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 5845 | if (btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) |
Li Zefan | 848cce0 | 2012-02-21 17:04:28 +0800 | [diff] [blame] | 5846 | return 1; |
Yan, Zheng | efefb14 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5847 | } |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5848 | return 0; |
| 5849 | } |
| 5850 | |
Josef Bacik | b4aff1f | 2011-06-28 16:18:59 -0400 | [diff] [blame] | 5851 | static void btrfs_dentry_release(struct dentry *dentry) |
| 5852 | { |
Daeseok Youn | 944a451 | 2014-04-14 15:37:02 +0900 | [diff] [blame] | 5853 | kfree(dentry->d_fsdata); |
Josef Bacik | b4aff1f | 2011-06-28 16:18:59 -0400 | [diff] [blame] | 5854 | } |
| 5855 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5856 | static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry, |
Al Viro | 00cd8dd | 2012-06-10 17:13:09 -0400 | [diff] [blame] | 5857 | unsigned int flags) |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5858 | { |
Tsutomu Itoh | 5662344b3 | 2013-12-13 09:51:42 +0900 | [diff] [blame] | 5859 | struct inode *inode; |
Josef Bacik | a66e7cc | 2011-09-18 10:34:03 -0400 | [diff] [blame] | 5860 | |
Tsutomu Itoh | 5662344b3 | 2013-12-13 09:51:42 +0900 | [diff] [blame] | 5861 | inode = btrfs_lookup_dentry(dir, dentry); |
| 5862 | if (IS_ERR(inode)) { |
| 5863 | if (PTR_ERR(inode) == -ENOENT) |
| 5864 | inode = NULL; |
| 5865 | else |
| 5866 | return ERR_CAST(inode); |
| 5867 | } |
| 5868 | |
Al Viro | 41d28bc | 2014-10-12 22:24:21 -0400 | [diff] [blame] | 5869 | return d_splice_alias(inode, dentry); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5870 | } |
| 5871 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5872 | unsigned char btrfs_filetype_table[] = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5873 | DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK |
| 5874 | }; |
| 5875 | |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5876 | static int btrfs_real_readdir(struct file *file, struct dir_context *ctx) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5877 | { |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5878 | struct inode *inode = file_inode(file); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5879 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5880 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5881 | struct btrfs_dir_item *di; |
| 5882 | struct btrfs_key key; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5883 | struct btrfs_key found_key; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5884 | struct btrfs_path *path; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5885 | struct list_head ins_list; |
| 5886 | struct list_head del_list; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5887 | int ret; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5888 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5889 | int slot; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5890 | unsigned char d_type; |
| 5891 | int over = 0; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5892 | char tmp_name[32]; |
| 5893 | char *name_ptr; |
| 5894 | int name_len; |
Omar Sandoval | 02dbfc9 | 2016-05-20 13:50:33 -0700 | [diff] [blame] | 5895 | bool put = false; |
Jeff Mahoney | c2951f3 | 2016-11-21 15:59:04 +0100 | [diff] [blame] | 5896 | struct btrfs_key location; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5897 | |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5898 | if (!dir_emit_dots(file, ctx)) |
| 5899 | return 0; |
| 5900 | |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5901 | path = btrfs_alloc_path(); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5902 | if (!path) |
| 5903 | return -ENOMEM; |
Chris Mason | ff5714c | 2011-05-28 07:00:39 -0400 | [diff] [blame] | 5904 | |
David Sterba | e4058b5 | 2015-11-27 16:31:35 +0100 | [diff] [blame] | 5905 | path->reada = READA_FORWARD; |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5906 | |
Jeff Mahoney | c2951f3 | 2016-11-21 15:59:04 +0100 | [diff] [blame] | 5907 | INIT_LIST_HEAD(&ins_list); |
| 5908 | INIT_LIST_HEAD(&del_list); |
| 5909 | put = btrfs_readdir_get_delayed_items(inode, &ins_list, &del_list); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5910 | |
Jeff Mahoney | c2951f3 | 2016-11-21 15:59:04 +0100 | [diff] [blame] | 5911 | key.type = BTRFS_DIR_INDEX_KEY; |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5912 | key.offset = ctx->pos; |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 5913 | key.objectid = btrfs_ino(BTRFS_I(inode)); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5914 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5915 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 5916 | if (ret < 0) |
| 5917 | goto err; |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5918 | |
| 5919 | while (1) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5920 | leaf = path->nodes[0]; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5921 | slot = path->slots[0]; |
Li Zefan | b9e03af | 2011-03-23 10:43:58 +0800 | [diff] [blame] | 5922 | if (slot >= btrfs_header_nritems(leaf)) { |
| 5923 | ret = btrfs_next_leaf(root, path); |
| 5924 | if (ret < 0) |
| 5925 | goto err; |
| 5926 | else if (ret > 0) |
| 5927 | break; |
| 5928 | continue; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5929 | } |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5930 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5931 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
| 5932 | |
| 5933 | if (found_key.objectid != key.objectid) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5934 | break; |
Jeff Mahoney | c2951f3 | 2016-11-21 15:59:04 +0100 | [diff] [blame] | 5935 | if (found_key.type != BTRFS_DIR_INDEX_KEY) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5936 | break; |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5937 | if (found_key.offset < ctx->pos) |
Li Zefan | b9e03af | 2011-03-23 10:43:58 +0800 | [diff] [blame] | 5938 | goto next; |
Jeff Mahoney | c2951f3 | 2016-11-21 15:59:04 +0100 | [diff] [blame] | 5939 | if (btrfs_should_delete_dir_index(&del_list, found_key.offset)) |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5940 | goto next; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5941 | |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5942 | ctx->pos = found_key.offset; |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5943 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5944 | di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item); |
Su Yue | e79a332 | 2017-06-06 17:57:01 +0800 | [diff] [blame] | 5945 | if (verify_dir_item(fs_info, leaf, slot, di)) |
Jeff Mahoney | c2951f3 | 2016-11-21 15:59:04 +0100 | [diff] [blame] | 5946 | goto next; |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5947 | |
Jeff Mahoney | c2951f3 | 2016-11-21 15:59:04 +0100 | [diff] [blame] | 5948 | name_len = btrfs_dir_name_len(leaf, di); |
| 5949 | if (name_len <= sizeof(tmp_name)) { |
| 5950 | name_ptr = tmp_name; |
| 5951 | } else { |
| 5952 | name_ptr = kmalloc(name_len, GFP_KERNEL); |
| 5953 | if (!name_ptr) { |
| 5954 | ret = -ENOMEM; |
| 5955 | goto err; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5956 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5957 | } |
Jeff Mahoney | c2951f3 | 2016-11-21 15:59:04 +0100 | [diff] [blame] | 5958 | read_extent_buffer(leaf, name_ptr, (unsigned long)(di + 1), |
| 5959 | name_len); |
| 5960 | |
| 5961 | d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)]; |
| 5962 | btrfs_dir_item_key_to_cpu(leaf, di, &location); |
| 5963 | |
| 5964 | over = !dir_emit(ctx, name_ptr, name_len, location.objectid, |
| 5965 | d_type); |
| 5966 | |
| 5967 | if (name_ptr != tmp_name) |
| 5968 | kfree(name_ptr); |
| 5969 | |
Jeff Mahoney | c2951f3 | 2016-11-21 15:59:04 +0100 | [diff] [blame] | 5970 | if (over) |
| 5971 | goto nopos; |
Jeff Mahoney | d2fbb2b | 2016-11-05 13:26:35 -0400 | [diff] [blame] | 5972 | ctx->pos++; |
Li Zefan | b9e03af | 2011-03-23 10:43:58 +0800 | [diff] [blame] | 5973 | next: |
| 5974 | path->slots[0]++; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5975 | } |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5976 | |
Jeff Mahoney | d2fbb2b | 2016-11-05 13:26:35 -0400 | [diff] [blame] | 5977 | ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list); |
Jeff Mahoney | c2951f3 | 2016-11-21 15:59:04 +0100 | [diff] [blame] | 5978 | if (ret) |
David Sterba | bc4ef75 | 2015-11-13 13:44:28 +0100 | [diff] [blame] | 5979 | goto nopos; |
| 5980 | |
Zach Brown | db62efb | 2013-07-11 16:19:42 -0700 | [diff] [blame] | 5981 | /* |
| 5982 | * Stop new entries from being returned after we return the last |
| 5983 | * entry. |
| 5984 | * |
| 5985 | * New directory entries are assigned a strictly increasing |
| 5986 | * offset. This means that new entries created during readdir |
| 5987 | * are *guaranteed* to be seen in the future by that readdir. |
| 5988 | * This has broken buggy programs which operate on names as |
| 5989 | * they're returned by readdir. Until we re-use freed offsets |
| 5990 | * we have this hack to stop new entries from being returned |
| 5991 | * under the assumption that they'll never reach this huge |
| 5992 | * offset. |
| 5993 | * |
| 5994 | * This is being careful not to overflow 32bit loff_t unless the |
| 5995 | * last entry requires it because doing so has broken 32bit apps |
| 5996 | * in the past. |
| 5997 | */ |
Jeff Mahoney | c2951f3 | 2016-11-21 15:59:04 +0100 | [diff] [blame] | 5998 | if (ctx->pos >= INT_MAX) |
| 5999 | ctx->pos = LLONG_MAX; |
| 6000 | else |
| 6001 | ctx->pos = INT_MAX; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6002 | nopos: |
| 6003 | ret = 0; |
| 6004 | err: |
Omar Sandoval | 02dbfc9 | 2016-05-20 13:50:33 -0700 | [diff] [blame] | 6005 | if (put) |
| 6006 | btrfs_readdir_put_delayed_items(inode, &ins_list, &del_list); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6007 | btrfs_free_path(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6008 | return ret; |
| 6009 | } |
| 6010 | |
Christoph Hellwig | a9185b4 | 2010-03-05 09:21:37 +0100 | [diff] [blame] | 6011 | int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6012 | { |
| 6013 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 6014 | struct btrfs_trans_handle *trans; |
| 6015 | int ret = 0; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 6016 | bool nolock = false; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6017 | |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 6018 | if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags)) |
Chris Mason | 4ca8b41 | 2008-08-05 13:30:48 -0400 | [diff] [blame] | 6019 | return 0; |
| 6020 | |
Nikolay Borisov | 70ddc55 | 2017-02-20 13:50:35 +0200 | [diff] [blame] | 6021 | if (btrfs_fs_closing(root->fs_info) && |
| 6022 | btrfs_is_free_space_inode(BTRFS_I(inode))) |
Li Zefan | 82d5902 | 2011-04-20 10:33:24 +0800 | [diff] [blame] | 6023 | nolock = true; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 6024 | |
Christoph Hellwig | a9185b4 | 2010-03-05 09:21:37 +0100 | [diff] [blame] | 6025 | if (wbc->sync_mode == WB_SYNC_ALL) { |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 6026 | if (nolock) |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 6027 | trans = btrfs_join_transaction_nolock(root); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 6028 | else |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 6029 | trans = btrfs_join_transaction(root); |
Tsutomu Itoh | 3612b49 | 2011-01-25 02:51:38 +0000 | [diff] [blame] | 6030 | if (IS_ERR(trans)) |
| 6031 | return PTR_ERR(trans); |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 6032 | ret = btrfs_commit_transaction(trans); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6033 | } |
| 6034 | return ret; |
| 6035 | } |
| 6036 | |
| 6037 | /* |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 6038 | * This is somewhat expensive, updating the tree every time the |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6039 | * inode changes. But, it is most likely to find the inode in cache. |
| 6040 | * FIXME, needs more benchmarking...there are no reasons other than performance |
| 6041 | * to keep or drop this code. |
| 6042 | */ |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 6043 | static int btrfs_dirty_inode(struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6044 | { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6045 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6046 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 6047 | struct btrfs_trans_handle *trans; |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 6048 | int ret; |
| 6049 | |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 6050 | if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags)) |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 6051 | return 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6052 | |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 6053 | trans = btrfs_join_transaction(root); |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 6054 | if (IS_ERR(trans)) |
| 6055 | return PTR_ERR(trans); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 6056 | |
| 6057 | ret = btrfs_update_inode(trans, root, inode); |
Chris Mason | 94b6044 | 2010-05-26 11:02:00 -0400 | [diff] [blame] | 6058 | if (ret && ret == -ENOSPC) { |
| 6059 | /* whoops, lets try again with the full transaction */ |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 6060 | btrfs_end_transaction(trans); |
Chris Mason | 94b6044 | 2010-05-26 11:02:00 -0400 | [diff] [blame] | 6061 | trans = btrfs_start_transaction(root, 1); |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 6062 | if (IS_ERR(trans)) |
| 6063 | return PTR_ERR(trans); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 6064 | |
Chris Mason | 94b6044 | 2010-05-26 11:02:00 -0400 | [diff] [blame] | 6065 | ret = btrfs_update_inode(trans, root, inode); |
Chris Mason | 94b6044 | 2010-05-26 11:02:00 -0400 | [diff] [blame] | 6066 | } |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 6067 | btrfs_end_transaction(trans); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 6068 | if (BTRFS_I(inode)->delayed_node) |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6069 | btrfs_balance_delayed_items(fs_info); |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 6070 | |
| 6071 | return ret; |
| 6072 | } |
| 6073 | |
| 6074 | /* |
| 6075 | * This is a copy of file_update_time. We need this so we can return error on |
| 6076 | * ENOSPC for updating the inode in the case of file write and mmap writes. |
| 6077 | */ |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 6078 | static int btrfs_update_time(struct inode *inode, struct timespec *now, |
| 6079 | int flags) |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 6080 | { |
Alexander Block | 2bc556528 | 2012-06-15 09:49:33 +0200 | [diff] [blame] | 6081 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 6082 | |
| 6083 | if (btrfs_root_readonly(root)) |
| 6084 | return -EROFS; |
| 6085 | |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 6086 | if (flags & S_VERSION) |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 6087 | inode_inc_iversion(inode); |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 6088 | if (flags & S_CTIME) |
| 6089 | inode->i_ctime = *now; |
| 6090 | if (flags & S_MTIME) |
| 6091 | inode->i_mtime = *now; |
| 6092 | if (flags & S_ATIME) |
| 6093 | inode->i_atime = *now; |
| 6094 | return btrfs_dirty_inode(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6095 | } |
| 6096 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 6097 | /* |
| 6098 | * find the highest existing sequence number in a directory |
| 6099 | * and then set the in-memory index_cnt variable to reflect |
| 6100 | * free sequence numbers |
| 6101 | */ |
Nikolay Borisov | 4c570655 | 2017-02-20 13:50:32 +0200 | [diff] [blame] | 6102 | static int btrfs_set_inode_index_count(struct btrfs_inode *inode) |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6103 | { |
Nikolay Borisov | 4c570655 | 2017-02-20 13:50:32 +0200 | [diff] [blame] | 6104 | struct btrfs_root *root = inode->root; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6105 | struct btrfs_key key, found_key; |
| 6106 | struct btrfs_path *path; |
| 6107 | struct extent_buffer *leaf; |
| 6108 | int ret; |
| 6109 | |
Nikolay Borisov | 4c570655 | 2017-02-20 13:50:32 +0200 | [diff] [blame] | 6110 | key.objectid = btrfs_ino(inode); |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 6111 | key.type = BTRFS_DIR_INDEX_KEY; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6112 | key.offset = (u64)-1; |
| 6113 | |
| 6114 | path = btrfs_alloc_path(); |
| 6115 | if (!path) |
| 6116 | return -ENOMEM; |
| 6117 | |
| 6118 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 6119 | if (ret < 0) |
| 6120 | goto out; |
| 6121 | /* FIXME: we should be able to handle this */ |
| 6122 | if (ret == 0) |
| 6123 | goto out; |
| 6124 | ret = 0; |
| 6125 | |
| 6126 | /* |
| 6127 | * MAGIC NUMBER EXPLANATION: |
| 6128 | * since we search a directory based on f_pos we have to start at 2 |
| 6129 | * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody |
| 6130 | * else has to start at 2 |
| 6131 | */ |
| 6132 | if (path->slots[0] == 0) { |
Nikolay Borisov | 4c570655 | 2017-02-20 13:50:32 +0200 | [diff] [blame] | 6133 | inode->index_cnt = 2; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6134 | goto out; |
| 6135 | } |
| 6136 | |
| 6137 | path->slots[0]--; |
| 6138 | |
| 6139 | leaf = path->nodes[0]; |
| 6140 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 6141 | |
Nikolay Borisov | 4c570655 | 2017-02-20 13:50:32 +0200 | [diff] [blame] | 6142 | if (found_key.objectid != btrfs_ino(inode) || |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 6143 | found_key.type != BTRFS_DIR_INDEX_KEY) { |
Nikolay Borisov | 4c570655 | 2017-02-20 13:50:32 +0200 | [diff] [blame] | 6144 | inode->index_cnt = 2; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6145 | goto out; |
| 6146 | } |
| 6147 | |
Nikolay Borisov | 4c570655 | 2017-02-20 13:50:32 +0200 | [diff] [blame] | 6148 | inode->index_cnt = found_key.offset + 1; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6149 | out: |
| 6150 | btrfs_free_path(path); |
| 6151 | return ret; |
| 6152 | } |
| 6153 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 6154 | /* |
| 6155 | * helper to find a free sequence number in a given directory. This current |
| 6156 | * code is very simple, later versions will do smarter things in the btree |
| 6157 | */ |
Nikolay Borisov | 877574e | 2017-02-20 13:50:33 +0200 | [diff] [blame] | 6158 | int btrfs_set_inode_index(struct btrfs_inode *dir, u64 *index) |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6159 | { |
| 6160 | int ret = 0; |
| 6161 | |
Nikolay Borisov | 877574e | 2017-02-20 13:50:33 +0200 | [diff] [blame] | 6162 | if (dir->index_cnt == (u64)-1) { |
| 6163 | ret = btrfs_inode_delayed_dir_index_count(dir); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 6164 | if (ret) { |
| 6165 | ret = btrfs_set_inode_index_count(dir); |
| 6166 | if (ret) |
| 6167 | return ret; |
| 6168 | } |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6169 | } |
| 6170 | |
Nikolay Borisov | 877574e | 2017-02-20 13:50:33 +0200 | [diff] [blame] | 6171 | *index = dir->index_cnt; |
| 6172 | dir->index_cnt++; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6173 | |
| 6174 | return ret; |
| 6175 | } |
| 6176 | |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6177 | static int btrfs_insert_inode_locked(struct inode *inode) |
| 6178 | { |
| 6179 | struct btrfs_iget_args args; |
| 6180 | args.location = &BTRFS_I(inode)->location; |
| 6181 | args.root = BTRFS_I(inode)->root; |
| 6182 | |
| 6183 | return insert_inode_locked4(inode, |
| 6184 | btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root), |
| 6185 | btrfs_find_actor, &args); |
| 6186 | } |
| 6187 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6188 | static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans, |
| 6189 | struct btrfs_root *root, |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6190 | struct inode *dir, |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 6191 | const char *name, int name_len, |
Al Viro | 175a4eb | 2011-07-26 03:30:54 -0400 | [diff] [blame] | 6192 | u64 ref_objectid, u64 objectid, |
| 6193 | umode_t mode, u64 *index) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6194 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6195 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6196 | struct inode *inode; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6197 | struct btrfs_inode_item *inode_item; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6198 | struct btrfs_key *location; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6199 | struct btrfs_path *path; |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 6200 | struct btrfs_inode_ref *ref; |
| 6201 | struct btrfs_key key[2]; |
| 6202 | u32 sizes[2]; |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 6203 | int nitems = name ? 2 : 1; |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 6204 | unsigned long ptr; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6205 | int ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6206 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6207 | path = btrfs_alloc_path(); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 6208 | if (!path) |
| 6209 | return ERR_PTR(-ENOMEM); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6210 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6211 | inode = new_inode(fs_info->sb); |
Yoshinori Sano | 8fb2764 | 2011-04-09 02:30:07 +0000 | [diff] [blame] | 6212 | if (!inode) { |
| 6213 | btrfs_free_path(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6214 | return ERR_PTR(-ENOMEM); |
Yoshinori Sano | 8fb2764 | 2011-04-09 02:30:07 +0000 | [diff] [blame] | 6215 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6216 | |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 6217 | /* |
Filipe Manana | 5762b5c | 2014-08-01 00:10:32 +0100 | [diff] [blame] | 6218 | * O_TMPFILE, set link count to 0, so that after this point, |
| 6219 | * we fill in an inode item with the correct link count. |
| 6220 | */ |
| 6221 | if (!name) |
| 6222 | set_nlink(inode, 0); |
| 6223 | |
| 6224 | /* |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 6225 | * we have to initialize this early, so we can reclaim the inode |
| 6226 | * number if we fail afterwards in this function. |
| 6227 | */ |
| 6228 | inode->i_ino = objectid; |
| 6229 | |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 6230 | if (dir && name) { |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 6231 | trace_btrfs_inode_request(dir); |
| 6232 | |
Nikolay Borisov | 877574e | 2017-02-20 13:50:33 +0200 | [diff] [blame] | 6233 | ret = btrfs_set_inode_index(BTRFS_I(dir), index); |
Shen Feng | 0977143 | 2009-04-02 16:46:06 -0400 | [diff] [blame] | 6234 | if (ret) { |
Yoshinori Sano | 8fb2764 | 2011-04-09 02:30:07 +0000 | [diff] [blame] | 6235 | btrfs_free_path(path); |
Shen Feng | 0977143 | 2009-04-02 16:46:06 -0400 | [diff] [blame] | 6236 | iput(inode); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6237 | return ERR_PTR(ret); |
Shen Feng | 0977143 | 2009-04-02 16:46:06 -0400 | [diff] [blame] | 6238 | } |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 6239 | } else if (dir) { |
| 6240 | *index = 0; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6241 | } |
| 6242 | /* |
| 6243 | * index_cnt is ignored for everything but a dir, |
| 6244 | * btrfs_get_inode_index_count has an explanation for the magic |
| 6245 | * number |
| 6246 | */ |
| 6247 | BTRFS_I(inode)->index_cnt = 2; |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 6248 | BTRFS_I(inode)->dir_index = *index; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6249 | BTRFS_I(inode)->root = root; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 6250 | BTRFS_I(inode)->generation = trans->transid; |
Josef Bacik | 7619585 | 2010-11-19 02:18:02 +0000 | [diff] [blame] | 6251 | inode->i_generation = BTRFS_I(inode)->generation; |
Chris Mason | b888db2b | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6252 | |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 6253 | /* |
| 6254 | * We could have gotten an inode number from somebody who was fsynced |
| 6255 | * and then removed in this same transaction, so let's just set full |
| 6256 | * sync since it will be a full sync anyway and this will blow away the |
| 6257 | * old info in the log. |
| 6258 | */ |
| 6259 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags); |
| 6260 | |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 6261 | key[0].objectid = objectid; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 6262 | key[0].type = BTRFS_INODE_ITEM_KEY; |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 6263 | key[0].offset = 0; |
| 6264 | |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 6265 | sizes[0] = sizeof(struct btrfs_inode_item); |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 6266 | |
| 6267 | if (name) { |
| 6268 | /* |
| 6269 | * Start new inodes with an inode_ref. This is slightly more |
| 6270 | * efficient for small numbers of hard links since they will |
| 6271 | * be packed into one item. Extended refs will kick in if we |
| 6272 | * add more hard links than can fit in the ref item. |
| 6273 | */ |
| 6274 | key[1].objectid = objectid; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 6275 | key[1].type = BTRFS_INODE_REF_KEY; |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 6276 | key[1].offset = ref_objectid; |
| 6277 | |
| 6278 | sizes[1] = name_len + sizeof(*ref); |
| 6279 | } |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 6280 | |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6281 | location = &BTRFS_I(inode)->location; |
| 6282 | location->objectid = objectid; |
| 6283 | location->offset = 0; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 6284 | location->type = BTRFS_INODE_ITEM_KEY; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6285 | |
| 6286 | ret = btrfs_insert_inode_locked(inode); |
| 6287 | if (ret < 0) |
| 6288 | goto fail; |
| 6289 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 6290 | path->leave_spinning = 1; |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 6291 | ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems); |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 6292 | if (ret != 0) |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6293 | goto fail_unlock; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6294 | |
Dmitry Monakhov | ecc11fab | 2010-03-04 17:31:47 +0300 | [diff] [blame] | 6295 | inode_init_owner(inode, dir, mode); |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 6296 | inode_set_bytes(inode, 0); |
chandan r | 9cc97d6 | 2012-07-04 12:48:07 +0530 | [diff] [blame] | 6297 | |
Deepa Dinamani | c2050a4 | 2016-09-14 07:48:06 -0700 | [diff] [blame] | 6298 | inode->i_mtime = current_time(inode); |
chandan r | 9cc97d6 | 2012-07-04 12:48:07 +0530 | [diff] [blame] | 6299 | inode->i_atime = inode->i_mtime; |
| 6300 | inode->i_ctime = inode->i_mtime; |
| 6301 | BTRFS_I(inode)->i_otime = inode->i_mtime; |
| 6302 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6303 | inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 6304 | struct btrfs_inode_item); |
David Sterba | b159fa2 | 2016-11-08 18:09:03 +0100 | [diff] [blame] | 6305 | memzero_extent_buffer(path->nodes[0], (unsigned long)inode_item, |
Li Zefan | 293f7e0 | 2012-07-10 00:58:58 -0600 | [diff] [blame] | 6306 | sizeof(*inode_item)); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 6307 | fill_inode_item(trans, path->nodes[0], inode_item, inode); |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 6308 | |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 6309 | if (name) { |
| 6310 | ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1, |
| 6311 | struct btrfs_inode_ref); |
| 6312 | btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len); |
| 6313 | btrfs_set_inode_ref_index(path->nodes[0], ref, *index); |
| 6314 | ptr = (unsigned long)(ref + 1); |
| 6315 | write_extent_buffer(path->nodes[0], name, ptr, name_len); |
| 6316 | } |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 6317 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6318 | btrfs_mark_buffer_dirty(path->nodes[0]); |
| 6319 | btrfs_free_path(path); |
| 6320 | |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 6321 | btrfs_inherit_iflags(inode, dir); |
| 6322 | |
Al Viro | 569254b | 2011-07-24 17:08:40 -0400 | [diff] [blame] | 6323 | if (S_ISREG(mode)) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6324 | if (btrfs_test_opt(fs_info, NODATASUM)) |
Chris Mason | 9427216 | 2009-07-02 12:26:06 -0400 | [diff] [blame] | 6325 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6326 | if (btrfs_test_opt(fs_info, NODATACOW)) |
Josef Bacik | f2bdf9a | 2013-02-21 15:28:28 -0500 | [diff] [blame] | 6327 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW | |
| 6328 | BTRFS_INODE_NODATASUM; |
Chris Mason | 9427216 | 2009-07-02 12:26:06 -0400 | [diff] [blame] | 6329 | } |
| 6330 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6331 | inode_tree_add(inode); |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 6332 | |
| 6333 | trace_btrfs_inode_new(inode); |
Chris Mason | 1973f0f | 2011-06-24 13:13:29 -0400 | [diff] [blame] | 6334 | btrfs_set_inode_last_trans(trans, inode); |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 6335 | |
Alexander Block | 8ea05e3 | 2012-07-25 17:35:53 +0200 | [diff] [blame] | 6336 | btrfs_update_root_times(trans, root); |
| 6337 | |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 6338 | ret = btrfs_inode_inherit_props(trans, inode, dir); |
| 6339 | if (ret) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6340 | btrfs_err(fs_info, |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 6341 | "error inheriting props for ino %llu (root %llu): %d", |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 6342 | btrfs_ino(BTRFS_I(inode)), root->root_key.objectid, ret); |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 6343 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6344 | return inode; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6345 | |
| 6346 | fail_unlock: |
| 6347 | unlock_new_inode(inode); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6348 | fail: |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 6349 | if (dir && name) |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6350 | BTRFS_I(dir)->index_cnt--; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6351 | btrfs_free_path(path); |
Shen Feng | 0977143 | 2009-04-02 16:46:06 -0400 | [diff] [blame] | 6352 | iput(inode); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6353 | return ERR_PTR(ret); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6354 | } |
| 6355 | |
| 6356 | static inline u8 btrfs_inode_type(struct inode *inode) |
| 6357 | { |
| 6358 | return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT]; |
| 6359 | } |
| 6360 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 6361 | /* |
| 6362 | * utility function to add 'inode' into 'parent_inode' with |
| 6363 | * a give name and a given sequence number. |
| 6364 | * if 'add_backref' is true, also insert a backref from the |
| 6365 | * inode to the parent directory. |
| 6366 | */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 6367 | int btrfs_add_link(struct btrfs_trans_handle *trans, |
Nikolay Borisov | db0a669 | 2017-02-20 13:51:08 +0200 | [diff] [blame] | 6368 | struct btrfs_inode *parent_inode, struct btrfs_inode *inode, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 6369 | const char *name, int name_len, int add_backref, u64 index) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6370 | { |
Nikolay Borisov | db0a669 | 2017-02-20 13:51:08 +0200 | [diff] [blame] | 6371 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6372 | int ret = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6373 | struct btrfs_key key; |
Nikolay Borisov | db0a669 | 2017-02-20 13:51:08 +0200 | [diff] [blame] | 6374 | struct btrfs_root *root = parent_inode->root; |
| 6375 | u64 ino = btrfs_ino(inode); |
| 6376 | u64 parent_ino = btrfs_ino(parent_inode); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6377 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6378 | if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) { |
Nikolay Borisov | db0a669 | 2017-02-20 13:51:08 +0200 | [diff] [blame] | 6379 | memcpy(&key, &inode->root->root_key, sizeof(key)); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6380 | } else { |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6381 | key.objectid = ino; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 6382 | key.type = BTRFS_INODE_ITEM_KEY; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6383 | key.offset = 0; |
| 6384 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6385 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6386 | if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6387 | ret = btrfs_add_root_ref(trans, fs_info, key.objectid, |
| 6388 | root->root_key.objectid, parent_ino, |
| 6389 | index, name, name_len); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6390 | } else if (add_backref) { |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6391 | ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino, |
| 6392 | parent_ino, index); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6393 | } |
| 6394 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6395 | /* Nothing to clean up yet */ |
| 6396 | if (ret) |
| 6397 | return ret; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6398 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6399 | ret = btrfs_insert_dir_item(trans, root, name, name_len, |
| 6400 | parent_inode, &key, |
Nikolay Borisov | db0a669 | 2017-02-20 13:51:08 +0200 | [diff] [blame] | 6401 | btrfs_inode_type(&inode->vfs_inode), index); |
Chris Mason | 9c52057 | 2012-12-17 14:26:57 -0500 | [diff] [blame] | 6402 | if (ret == -EEXIST || ret == -EOVERFLOW) |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6403 | goto fail_dir_item; |
| 6404 | else if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 6405 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6406 | return ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6407 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6408 | |
Nikolay Borisov | db0a669 | 2017-02-20 13:51:08 +0200 | [diff] [blame] | 6409 | btrfs_i_size_write(parent_inode, parent_inode->vfs_inode.i_size + |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6410 | name_len * 2); |
Nikolay Borisov | db0a669 | 2017-02-20 13:51:08 +0200 | [diff] [blame] | 6411 | inode_inc_iversion(&parent_inode->vfs_inode); |
| 6412 | parent_inode->vfs_inode.i_mtime = parent_inode->vfs_inode.i_ctime = |
| 6413 | current_time(&parent_inode->vfs_inode); |
| 6414 | ret = btrfs_update_inode(trans, root, &parent_inode->vfs_inode); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6415 | if (ret) |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 6416 | btrfs_abort_transaction(trans, ret); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6417 | return ret; |
Chris Mason | fe66a05 | 2012-02-20 08:40:56 -0500 | [diff] [blame] | 6418 | |
| 6419 | fail_dir_item: |
| 6420 | if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) { |
| 6421 | u64 local_index; |
| 6422 | int err; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6423 | err = btrfs_del_root_ref(trans, fs_info, key.objectid, |
| 6424 | root->root_key.objectid, parent_ino, |
| 6425 | &local_index, name, name_len); |
Chris Mason | fe66a05 | 2012-02-20 08:40:56 -0500 | [diff] [blame] | 6426 | |
| 6427 | } else if (add_backref) { |
| 6428 | u64 local_index; |
| 6429 | int err; |
| 6430 | |
| 6431 | err = btrfs_del_inode_ref(trans, root, name, name_len, |
| 6432 | ino, parent_ino, &local_index); |
| 6433 | } |
| 6434 | return ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6435 | } |
| 6436 | |
| 6437 | static int btrfs_add_nondir(struct btrfs_trans_handle *trans, |
Nikolay Borisov | cef415a | 2017-02-20 13:51:09 +0200 | [diff] [blame] | 6438 | struct btrfs_inode *dir, struct dentry *dentry, |
| 6439 | struct btrfs_inode *inode, int backref, u64 index) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6440 | { |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 6441 | int err = btrfs_add_link(trans, dir, inode, |
| 6442 | dentry->d_name.name, dentry->d_name.len, |
| 6443 | backref, index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6444 | if (err > 0) |
| 6445 | err = -EEXIST; |
| 6446 | return err; |
| 6447 | } |
| 6448 | |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6449 | static int btrfs_mknod(struct inode *dir, struct dentry *dentry, |
Al Viro | 1a67aaf | 2011-07-26 01:52:52 -0400 | [diff] [blame] | 6450 | umode_t mode, dev_t rdev) |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6451 | { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6452 | struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6453 | struct btrfs_trans_handle *trans; |
| 6454 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 6455 | struct inode *inode = NULL; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6456 | int err; |
| 6457 | int drop_inode = 0; |
| 6458 | u64 objectid; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 6459 | u64 index = 0; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6460 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 6461 | /* |
| 6462 | * 2 for inode item and ref |
| 6463 | * 2 for dir items |
| 6464 | * 1 for xattr if selinux is on |
| 6465 | */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6466 | trans = btrfs_start_transaction(root, 5); |
| 6467 | if (IS_ERR(trans)) |
| 6468 | return PTR_ERR(trans); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 6469 | |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 6470 | err = btrfs_find_free_ino(root, &objectid); |
| 6471 | if (err) |
| 6472 | goto out_unlock; |
| 6473 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6474 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 6475 | dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid, |
| 6476 | mode, &index); |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 6477 | if (IS_ERR(inode)) { |
| 6478 | err = PTR_ERR(inode); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6479 | goto out_unlock; |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 6480 | } |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6481 | |
Casey Schaufler | ad19db7 | 2011-12-15 10:09:07 -0500 | [diff] [blame] | 6482 | /* |
| 6483 | * If the active LSM wants to access the inode during |
| 6484 | * d_instantiate it needs these. Smack checks to see |
| 6485 | * if the filesystem supports xattrs by looking at the |
| 6486 | * ops vector. |
| 6487 | */ |
Casey Schaufler | ad19db7 | 2011-12-15 10:09:07 -0500 | [diff] [blame] | 6488 | inode->i_op = &btrfs_special_inode_operations; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6489 | init_special_inode(inode, inode->i_mode, rdev); |
| 6490 | |
| 6491 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6492 | if (err) |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6493 | goto out_unlock_inode; |
| 6494 | |
Nikolay Borisov | cef415a | 2017-02-20 13:51:09 +0200 | [diff] [blame] | 6495 | err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode), |
| 6496 | 0, index); |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6497 | if (err) { |
| 6498 | goto out_unlock_inode; |
| 6499 | } else { |
Yan | 1b4ab1b | 2007-08-29 09:11:44 -0400 | [diff] [blame] | 6500 | btrfs_update_inode(trans, root, inode); |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6501 | unlock_new_inode(inode); |
Al Viro | 08c422c | 2011-12-23 07:58:13 -0500 | [diff] [blame] | 6502 | d_instantiate(dentry, inode); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6503 | } |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6504 | |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6505 | out_unlock: |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 6506 | btrfs_end_transaction(trans); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6507 | btrfs_balance_delayed_items(fs_info); |
| 6508 | btrfs_btree_balance_dirty(fs_info); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6509 | if (drop_inode) { |
| 6510 | inode_dec_link_count(inode); |
| 6511 | iput(inode); |
| 6512 | } |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6513 | return err; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6514 | |
| 6515 | out_unlock_inode: |
| 6516 | drop_inode = 1; |
| 6517 | unlock_new_inode(inode); |
| 6518 | goto out_unlock; |
| 6519 | |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6520 | } |
| 6521 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6522 | static int btrfs_create(struct inode *dir, struct dentry *dentry, |
Al Viro | ebfc3b4 | 2012-06-10 18:05:36 -0400 | [diff] [blame] | 6523 | umode_t mode, bool excl) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6524 | { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6525 | struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6526 | struct btrfs_trans_handle *trans; |
| 6527 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 6528 | struct inode *inode = NULL; |
Filipe Brandenburger | 43baa57 | 2012-11-30 03:40:09 +0000 | [diff] [blame] | 6529 | int drop_inode_on_err = 0; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6530 | int err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6531 | u64 objectid; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 6532 | u64 index = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6533 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 6534 | /* |
| 6535 | * 2 for inode item and ref |
| 6536 | * 2 for dir items |
| 6537 | * 1 for xattr if selinux is on |
| 6538 | */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6539 | trans = btrfs_start_transaction(root, 5); |
| 6540 | if (IS_ERR(trans)) |
| 6541 | return PTR_ERR(trans); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 6542 | |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 6543 | err = btrfs_find_free_ino(root, &objectid); |
| 6544 | if (err) |
| 6545 | goto out_unlock; |
| 6546 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6547 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 6548 | dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid, |
| 6549 | mode, &index); |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 6550 | if (IS_ERR(inode)) { |
| 6551 | err = PTR_ERR(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6552 | goto out_unlock; |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 6553 | } |
Filipe Brandenburger | 43baa57 | 2012-11-30 03:40:09 +0000 | [diff] [blame] | 6554 | drop_inode_on_err = 1; |
Casey Schaufler | ad19db7 | 2011-12-15 10:09:07 -0500 | [diff] [blame] | 6555 | /* |
| 6556 | * If the active LSM wants to access the inode during |
| 6557 | * d_instantiate it needs these. Smack checks to see |
| 6558 | * if the filesystem supports xattrs by looking at the |
| 6559 | * ops vector. |
| 6560 | */ |
| 6561 | inode->i_fop = &btrfs_file_operations; |
| 6562 | inode->i_op = &btrfs_file_inode_operations; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6563 | inode->i_mapping->a_ops = &btrfs_aops; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6564 | |
| 6565 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); |
| 6566 | if (err) |
| 6567 | goto out_unlock_inode; |
| 6568 | |
| 6569 | err = btrfs_update_inode(trans, root, inode); |
| 6570 | if (err) |
| 6571 | goto out_unlock_inode; |
Casey Schaufler | ad19db7 | 2011-12-15 10:09:07 -0500 | [diff] [blame] | 6572 | |
Nikolay Borisov | cef415a | 2017-02-20 13:51:09 +0200 | [diff] [blame] | 6573 | err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode), |
| 6574 | 0, index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6575 | if (err) |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6576 | goto out_unlock_inode; |
Filipe Brandenburger | 43baa57 | 2012-11-30 03:40:09 +0000 | [diff] [blame] | 6577 | |
Filipe Brandenburger | 43baa57 | 2012-11-30 03:40:09 +0000 | [diff] [blame] | 6578 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6579 | unlock_new_inode(inode); |
Filipe Brandenburger | 43baa57 | 2012-11-30 03:40:09 +0000 | [diff] [blame] | 6580 | d_instantiate(dentry, inode); |
| 6581 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6582 | out_unlock: |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 6583 | btrfs_end_transaction(trans); |
Filipe Brandenburger | 43baa57 | 2012-11-30 03:40:09 +0000 | [diff] [blame] | 6584 | if (err && drop_inode_on_err) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6585 | inode_dec_link_count(inode); |
| 6586 | iput(inode); |
| 6587 | } |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6588 | btrfs_balance_delayed_items(fs_info); |
| 6589 | btrfs_btree_balance_dirty(fs_info); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6590 | return err; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6591 | |
| 6592 | out_unlock_inode: |
| 6593 | unlock_new_inode(inode); |
| 6594 | goto out_unlock; |
| 6595 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6596 | } |
| 6597 | |
| 6598 | static int btrfs_link(struct dentry *old_dentry, struct inode *dir, |
| 6599 | struct dentry *dentry) |
| 6600 | { |
Filipe Manana | 271dba45 | 2016-01-05 16:24:05 +0000 | [diff] [blame] | 6601 | struct btrfs_trans_handle *trans = NULL; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6602 | struct btrfs_root *root = BTRFS_I(dir)->root; |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 6603 | struct inode *inode = d_inode(old_dentry); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6604 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 6605 | u64 index; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6606 | int err; |
| 6607 | int drop_inode = 0; |
| 6608 | |
TARUISI Hiroaki | 4a8be42 | 2009-11-12 07:14:26 +0000 | [diff] [blame] | 6609 | /* do not allow sys_link's with other subvols of the same device */ |
| 6610 | if (root->objectid != BTRFS_I(inode)->root->objectid) |
Mark Fasheh | 3ab3564 | 2011-03-22 17:20:26 +0000 | [diff] [blame] | 6611 | return -EXDEV; |
TARUISI Hiroaki | 4a8be42 | 2009-11-12 07:14:26 +0000 | [diff] [blame] | 6612 | |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 6613 | if (inode->i_nlink >= BTRFS_LINK_MAX) |
Al Viro | c055e99 | 2011-03-04 17:15:18 +0000 | [diff] [blame] | 6614 | return -EMLINK; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 6615 | |
Nikolay Borisov | 877574e | 2017-02-20 13:50:33 +0200 | [diff] [blame] | 6616 | err = btrfs_set_inode_index(BTRFS_I(dir), &index); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6617 | if (err) |
| 6618 | goto fail; |
| 6619 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6620 | /* |
Miao Xie | 7e6b646 | 2011-02-18 09:21:17 +0000 | [diff] [blame] | 6621 | * 2 items for inode and inode ref |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6622 | * 2 items for dir items |
Miao Xie | 7e6b646 | 2011-02-18 09:21:17 +0000 | [diff] [blame] | 6623 | * 1 item for parent inode |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6624 | */ |
Miao Xie | 7e6b646 | 2011-02-18 09:21:17 +0000 | [diff] [blame] | 6625 | trans = btrfs_start_transaction(root, 5); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6626 | if (IS_ERR(trans)) { |
| 6627 | err = PTR_ERR(trans); |
Filipe Manana | 271dba45 | 2016-01-05 16:24:05 +0000 | [diff] [blame] | 6628 | trans = NULL; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6629 | goto fail; |
| 6630 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6631 | |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 6632 | /* There are several dir indexes for this inode, clear the cache. */ |
| 6633 | BTRFS_I(inode)->dir_index = 0ULL; |
Zach Brown | 8b558c5 | 2013-10-16 12:10:34 -0700 | [diff] [blame] | 6634 | inc_nlink(inode); |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 6635 | inode_inc_iversion(inode); |
Deepa Dinamani | c2050a4 | 2016-09-14 07:48:06 -0700 | [diff] [blame] | 6636 | inode->i_ctime = current_time(inode); |
Al Viro | 7de9c6ee | 2010-10-23 11:11:40 -0400 | [diff] [blame] | 6637 | ihold(inode); |
Josef Bacik | e997615 | 2012-10-11 15:53:56 -0400 | [diff] [blame] | 6638 | set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6639 | |
Nikolay Borisov | cef415a | 2017-02-20 13:51:09 +0200 | [diff] [blame] | 6640 | err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode), |
| 6641 | 1, index); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6642 | |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 6643 | if (err) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6644 | drop_inode = 1; |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 6645 | } else { |
Al Viro | 10d9f30 | 2011-07-16 23:09:10 -0400 | [diff] [blame] | 6646 | struct dentry *parent = dentry->d_parent; |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 6647 | err = btrfs_update_inode(trans, root, inode); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6648 | if (err) |
| 6649 | goto fail; |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 6650 | if (inode->i_nlink == 1) { |
| 6651 | /* |
| 6652 | * If new hard link count is 1, it's a file created |
| 6653 | * with open(2) O_TMPFILE flag. |
| 6654 | */ |
Nikolay Borisov | 3d6ae7b | 2017-02-20 13:50:58 +0200 | [diff] [blame] | 6655 | err = btrfs_orphan_del(trans, BTRFS_I(inode)); |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 6656 | if (err) |
| 6657 | goto fail; |
| 6658 | } |
Al Viro | 08c422c | 2011-12-23 07:58:13 -0500 | [diff] [blame] | 6659 | d_instantiate(dentry, inode); |
Nikolay Borisov | 9ca5fbfb | 2017-01-18 00:31:31 +0200 | [diff] [blame] | 6660 | btrfs_log_new_name(trans, BTRFS_I(inode), NULL, parent); |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 6661 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6662 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6663 | btrfs_balance_delayed_items(fs_info); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 6664 | fail: |
Filipe Manana | 271dba45 | 2016-01-05 16:24:05 +0000 | [diff] [blame] | 6665 | if (trans) |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 6666 | btrfs_end_transaction(trans); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6667 | if (drop_inode) { |
| 6668 | inode_dec_link_count(inode); |
| 6669 | iput(inode); |
| 6670 | } |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6671 | btrfs_btree_balance_dirty(fs_info); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6672 | return err; |
| 6673 | } |
| 6674 | |
Al Viro | 18bb1db | 2011-07-26 01:41:39 -0400 | [diff] [blame] | 6675 | 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] | 6676 | { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6677 | struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); |
Chris Mason | b9d8666 | 2008-05-02 16:13:49 -0400 | [diff] [blame] | 6678 | struct inode *inode = NULL; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6679 | struct btrfs_trans_handle *trans; |
| 6680 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 6681 | int err = 0; |
| 6682 | int drop_on_err = 0; |
Chris Mason | b9d8666 | 2008-05-02 16:13:49 -0400 | [diff] [blame] | 6683 | u64 objectid = 0; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 6684 | u64 index = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6685 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 6686 | /* |
| 6687 | * 2 items for inode and ref |
| 6688 | * 2 items for dir items |
| 6689 | * 1 for xattr if selinux is on |
| 6690 | */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6691 | trans = btrfs_start_transaction(root, 5); |
| 6692 | if (IS_ERR(trans)) |
| 6693 | return PTR_ERR(trans); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6694 | |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 6695 | err = btrfs_find_free_ino(root, &objectid); |
| 6696 | if (err) |
| 6697 | goto out_fail; |
| 6698 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6699 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 6700 | dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid, |
| 6701 | S_IFDIR | mode, &index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6702 | if (IS_ERR(inode)) { |
| 6703 | err = PTR_ERR(inode); |
| 6704 | goto out_fail; |
| 6705 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6706 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6707 | drop_on_err = 1; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6708 | /* these must be set before we unlock the inode */ |
| 6709 | inode->i_op = &btrfs_dir_inode_operations; |
| 6710 | inode->i_fop = &btrfs_dir_file_operations; |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 6711 | |
Eric Paris | 2a7dba3 | 2011-02-01 11:05:39 -0500 | [diff] [blame] | 6712 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 6713 | if (err) |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6714 | goto out_fail_inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6715 | |
Nikolay Borisov | 6ef06d2 | 2017-02-20 13:50:34 +0200 | [diff] [blame] | 6716 | btrfs_i_size_write(BTRFS_I(inode), 0); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6717 | err = btrfs_update_inode(trans, root, inode); |
| 6718 | if (err) |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6719 | goto out_fail_inode; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6720 | |
Nikolay Borisov | db0a669 | 2017-02-20 13:51:08 +0200 | [diff] [blame] | 6721 | err = btrfs_add_link(trans, BTRFS_I(dir), BTRFS_I(inode), |
| 6722 | dentry->d_name.name, |
| 6723 | dentry->d_name.len, 0, index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6724 | if (err) |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6725 | goto out_fail_inode; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6726 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6727 | d_instantiate(dentry, inode); |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6728 | /* |
| 6729 | * mkdir is special. We're unlocking after we call d_instantiate |
| 6730 | * to avoid a race with nfsd calling d_instantiate. |
| 6731 | */ |
| 6732 | unlock_new_inode(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6733 | drop_on_err = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6734 | |
| 6735 | out_fail: |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 6736 | btrfs_end_transaction(trans); |
Wang Shilong | c7cfb8a | 2014-12-24 14:45:30 +0800 | [diff] [blame] | 6737 | if (drop_on_err) { |
| 6738 | inode_dec_link_count(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6739 | iput(inode); |
Wang Shilong | c7cfb8a | 2014-12-24 14:45:30 +0800 | [diff] [blame] | 6740 | } |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6741 | btrfs_balance_delayed_items(fs_info); |
| 6742 | btrfs_btree_balance_dirty(fs_info); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6743 | return err; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6744 | |
| 6745 | out_fail_inode: |
| 6746 | unlock_new_inode(inode); |
| 6747 | goto out_fail; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6748 | } |
| 6749 | |
Qu Wenruo | e6c4efd | 2014-09-17 11:53:35 +0800 | [diff] [blame] | 6750 | /* Find next extent map of a given extent map, caller needs to ensure locks */ |
| 6751 | static struct extent_map *next_extent_map(struct extent_map *em) |
| 6752 | { |
| 6753 | struct rb_node *next; |
| 6754 | |
| 6755 | next = rb_next(&em->rb_node); |
| 6756 | if (!next) |
| 6757 | return NULL; |
| 6758 | return container_of(next, struct extent_map, rb_node); |
| 6759 | } |
| 6760 | |
| 6761 | static struct extent_map *prev_extent_map(struct extent_map *em) |
| 6762 | { |
| 6763 | struct rb_node *prev; |
| 6764 | |
| 6765 | prev = rb_prev(&em->rb_node); |
| 6766 | if (!prev) |
| 6767 | return NULL; |
| 6768 | return container_of(prev, struct extent_map, rb_node); |
| 6769 | } |
| 6770 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 6771 | /* helper for btfs_get_extent. Given an existing extent in the tree, |
Qu Wenruo | e6c4efd | 2014-09-17 11:53:35 +0800 | [diff] [blame] | 6772 | * the existing extent is the nearest extent to map_start, |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 6773 | * and an extent that you want to insert, deal with overlap and insert |
Qu Wenruo | e6c4efd | 2014-09-17 11:53:35 +0800 | [diff] [blame] | 6774 | * the best fitted new extent into the tree. |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 6775 | */ |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 6776 | static int merge_extent_mapping(struct extent_map_tree *em_tree, |
| 6777 | struct extent_map *existing, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6778 | struct extent_map *em, |
Qu Wenruo | 51f395a | 2014-08-08 13:06:20 +0800 | [diff] [blame] | 6779 | u64 map_start) |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 6780 | { |
Qu Wenruo | e6c4efd | 2014-09-17 11:53:35 +0800 | [diff] [blame] | 6781 | struct extent_map *prev; |
| 6782 | struct extent_map *next; |
| 6783 | u64 start; |
| 6784 | u64 end; |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 6785 | u64 start_diff; |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 6786 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6787 | BUG_ON(map_start < em->start || map_start >= extent_map_end(em)); |
Qu Wenruo | e6c4efd | 2014-09-17 11:53:35 +0800 | [diff] [blame] | 6788 | |
| 6789 | if (existing->start > map_start) { |
| 6790 | next = existing; |
| 6791 | prev = prev_extent_map(next); |
| 6792 | } else { |
| 6793 | prev = existing; |
| 6794 | next = next_extent_map(prev); |
| 6795 | } |
| 6796 | |
| 6797 | start = prev ? extent_map_end(prev) : em->start; |
| 6798 | start = max_t(u64, start, em->start); |
| 6799 | end = next ? next->start : extent_map_end(em); |
| 6800 | end = min_t(u64, end, extent_map_end(em)); |
| 6801 | start_diff = start - em->start; |
| 6802 | em->start = start; |
| 6803 | em->len = end - start; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6804 | if (em->block_start < EXTENT_MAP_LAST_BYTE && |
| 6805 | !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6806 | em->block_start += start_diff; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6807 | em->block_len -= start_diff; |
| 6808 | } |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 6809 | return add_extent_mapping(em_tree, em, 0); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 6810 | } |
| 6811 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6812 | static noinline int uncompress_inline(struct btrfs_path *path, |
Byongho Lee | e40da0e | 2015-05-19 23:46:45 +0900 | [diff] [blame] | 6813 | struct page *page, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6814 | size_t pg_offset, u64 extent_offset, |
| 6815 | struct btrfs_file_extent_item *item) |
| 6816 | { |
| 6817 | int ret; |
| 6818 | struct extent_buffer *leaf = path->nodes[0]; |
| 6819 | char *tmp; |
| 6820 | size_t max_size; |
| 6821 | unsigned long inline_size; |
| 6822 | unsigned long ptr; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 6823 | int compress_type; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6824 | |
| 6825 | WARN_ON(pg_offset != 0); |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 6826 | compress_type = btrfs_file_extent_compression(leaf, item); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6827 | max_size = btrfs_file_extent_ram_bytes(leaf, item); |
| 6828 | inline_size = btrfs_file_extent_inline_item_len(leaf, |
Ross Kirk | dd3cc16 | 2013-09-16 15:58:09 +0100 | [diff] [blame] | 6829 | btrfs_item_nr(path->slots[0])); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6830 | tmp = kmalloc(inline_size, GFP_NOFS); |
Tsutomu Itoh | 8d41371 | 2011-04-25 19:43:52 -0400 | [diff] [blame] | 6831 | if (!tmp) |
| 6832 | return -ENOMEM; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6833 | ptr = btrfs_file_extent_inline_start(item); |
| 6834 | |
| 6835 | read_extent_buffer(leaf, tmp, ptr, inline_size); |
| 6836 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 6837 | max_size = min_t(unsigned long, PAGE_SIZE, max_size); |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 6838 | ret = btrfs_decompress(compress_type, tmp, page, |
| 6839 | extent_offset, inline_size, max_size); |
Zygo Blaxell | e1699d2 | 2017-03-10 16:45:44 -0500 | [diff] [blame] | 6840 | |
| 6841 | /* |
| 6842 | * decompression code contains a memset to fill in any space between the end |
| 6843 | * of the uncompressed data and the end of max_size in case the decompressed |
| 6844 | * data ends up shorter than ram_bytes. That doesn't cover the hole between |
| 6845 | * the end of an inline extent and the beginning of the next block, so we |
| 6846 | * cover that region here. |
| 6847 | */ |
| 6848 | |
| 6849 | if (max_size + pg_offset < PAGE_SIZE) { |
| 6850 | char *map = kmap(page); |
| 6851 | memset(map + pg_offset + max_size, 0, PAGE_SIZE - max_size - pg_offset); |
| 6852 | kunmap(page); |
| 6853 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6854 | kfree(tmp); |
Zach Brown | 166ae5a | 2014-05-09 17:15:10 -0400 | [diff] [blame] | 6855 | return ret; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6856 | } |
| 6857 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 6858 | /* |
| 6859 | * 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] | 6860 | * the ugly parts come from merging extents from the disk with the in-ram |
| 6861 | * representation. This gets more complex because of the data=ordered code, |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 6862 | * where the in-ram extents might be locked pending data=ordered completion. |
| 6863 | * |
| 6864 | * This also copies inline extents directly into the page. |
| 6865 | */ |
Nikolay Borisov | fc4f21b1 | 2017-02-20 13:51:06 +0200 | [diff] [blame] | 6866 | struct extent_map *btrfs_get_extent(struct btrfs_inode *inode, |
| 6867 | struct page *page, |
| 6868 | size_t pg_offset, u64 start, u64 len, |
| 6869 | int create) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6870 | { |
Nikolay Borisov | fc4f21b1 | 2017-02-20 13:51:06 +0200 | [diff] [blame] | 6871 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6872 | int ret; |
| 6873 | int err = 0; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6874 | u64 extent_start = 0; |
| 6875 | u64 extent_end = 0; |
Nikolay Borisov | fc4f21b1 | 2017-02-20 13:51:06 +0200 | [diff] [blame] | 6876 | u64 objectid = btrfs_ino(inode); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6877 | u32 found_type; |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 6878 | struct btrfs_path *path = NULL; |
Nikolay Borisov | fc4f21b1 | 2017-02-20 13:51:06 +0200 | [diff] [blame] | 6879 | struct btrfs_root *root = inode->root; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6880 | struct btrfs_file_extent_item *item; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6881 | struct extent_buffer *leaf; |
| 6882 | struct btrfs_key found_key; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6883 | struct extent_map *em = NULL; |
Nikolay Borisov | fc4f21b1 | 2017-02-20 13:51:06 +0200 | [diff] [blame] | 6884 | struct extent_map_tree *em_tree = &inode->extent_tree; |
| 6885 | struct extent_io_tree *io_tree = &inode->io_tree; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6886 | struct btrfs_trans_handle *trans = NULL; |
Filipe Manana | 7ffbb59 | 2014-06-09 03:48:05 +0100 | [diff] [blame] | 6887 | const bool new_inline = !page || create; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6888 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6889 | again: |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 6890 | read_lock(&em_tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6891 | em = lookup_extent_mapping(em_tree, start, len); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 6892 | if (em) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6893 | em->bdev = fs_info->fs_devices->latest_bdev; |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 6894 | read_unlock(&em_tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6895 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6896 | if (em) { |
Chris Mason | e1c4b74 | 2008-04-22 13:26:46 -0400 | [diff] [blame] | 6897 | if (em->start > start || em->start + em->len <= start) |
| 6898 | free_extent_map(em); |
| 6899 | else if (em->block_start == EXTENT_MAP_INLINE && page) |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 6900 | free_extent_map(em); |
| 6901 | else |
| 6902 | goto out; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6903 | } |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 6904 | em = alloc_extent_map(); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6905 | if (!em) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6906 | err = -ENOMEM; |
| 6907 | goto out; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6908 | } |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6909 | em->bdev = fs_info->fs_devices->latest_bdev; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6910 | em->start = EXTENT_MAP_HOLE; |
Chris Mason | 445a694 | 2008-11-10 11:53:33 -0500 | [diff] [blame] | 6911 | em->orig_start = EXTENT_MAP_HOLE; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6912 | em->len = (u64)-1; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6913 | em->block_len = (u64)-1; |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 6914 | |
| 6915 | if (!path) { |
| 6916 | path = btrfs_alloc_path(); |
Josef Bacik | 026fd31 | 2011-05-13 10:32:11 -0400 | [diff] [blame] | 6917 | if (!path) { |
| 6918 | err = -ENOMEM; |
| 6919 | goto out; |
| 6920 | } |
| 6921 | /* |
| 6922 | * Chances are we'll be called again, so go ahead and do |
| 6923 | * readahead |
| 6924 | */ |
David Sterba | e4058b5 | 2015-11-27 16:31:35 +0100 | [diff] [blame] | 6925 | path->reada = READA_FORWARD; |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 6926 | } |
| 6927 | |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 6928 | ret = btrfs_lookup_file_extent(trans, root, path, |
| 6929 | objectid, start, trans != NULL); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6930 | if (ret < 0) { |
| 6931 | err = ret; |
| 6932 | goto out; |
| 6933 | } |
| 6934 | |
| 6935 | if (ret != 0) { |
| 6936 | if (path->slots[0] == 0) |
| 6937 | goto not_found; |
| 6938 | path->slots[0]--; |
| 6939 | } |
| 6940 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6941 | leaf = path->nodes[0]; |
| 6942 | item = btrfs_item_ptr(leaf, path->slots[0], |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6943 | struct btrfs_file_extent_item); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6944 | /* are we inside the extent that was found? */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6945 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 6946 | found_type = found_key.type; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6947 | if (found_key.objectid != objectid || |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6948 | found_type != BTRFS_EXTENT_DATA_KEY) { |
Josef Bacik | 25a5034 | 2013-10-14 12:08:38 -0400 | [diff] [blame] | 6949 | /* |
| 6950 | * If we backup past the first extent we want to move forward |
| 6951 | * and see if there is an extent in front of us, otherwise we'll |
| 6952 | * say there is a hole for our whole search range which can |
| 6953 | * cause problems. |
| 6954 | */ |
| 6955 | extent_end = start; |
| 6956 | goto next; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6957 | } |
| 6958 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6959 | found_type = btrfs_file_extent_type(leaf, item); |
| 6960 | extent_start = found_key.offset; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 6961 | if (found_type == BTRFS_FILE_EXTENT_REG || |
| 6962 | found_type == BTRFS_FILE_EXTENT_PREALLOC) { |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6963 | extent_end = extent_start + |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 6964 | btrfs_file_extent_num_bytes(leaf, item); |
Liu Bo | 09ed2f1 | 2017-03-10 11:09:48 -0800 | [diff] [blame] | 6965 | |
| 6966 | trace_btrfs_get_extent_show_fi_regular(inode, leaf, item, |
| 6967 | extent_start); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6968 | } else if (found_type == BTRFS_FILE_EXTENT_INLINE) { |
| 6969 | size_t size; |
Chris Mason | 514ac8a | 2014-01-03 21:07:00 -0800 | [diff] [blame] | 6970 | size = btrfs_file_extent_inline_len(leaf, path->slots[0], item); |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 6971 | extent_end = ALIGN(extent_start + size, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6972 | fs_info->sectorsize); |
Liu Bo | 09ed2f1 | 2017-03-10 11:09:48 -0800 | [diff] [blame] | 6973 | |
| 6974 | trace_btrfs_get_extent_show_fi_inline(inode, leaf, item, |
| 6975 | path->slots[0], |
| 6976 | extent_start); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6977 | } |
Josef Bacik | 25a5034 | 2013-10-14 12:08:38 -0400 | [diff] [blame] | 6978 | next: |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6979 | if (start >= extent_end) { |
| 6980 | path->slots[0]++; |
| 6981 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { |
| 6982 | ret = btrfs_next_leaf(root, path); |
| 6983 | if (ret < 0) { |
| 6984 | err = ret; |
| 6985 | goto out; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6986 | } |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6987 | if (ret > 0) |
| 6988 | goto not_found; |
| 6989 | leaf = path->nodes[0]; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6990 | } |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6991 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 6992 | if (found_key.objectid != objectid || |
| 6993 | found_key.type != BTRFS_EXTENT_DATA_KEY) |
| 6994 | goto not_found; |
| 6995 | if (start + len <= found_key.offset) |
| 6996 | goto not_found; |
Wang Shilong | e2eca69 | 2014-07-17 11:44:14 +0800 | [diff] [blame] | 6997 | if (start > found_key.offset) |
| 6998 | goto next; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6999 | em->start = start; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 7000 | em->orig_start = start; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 7001 | em->len = found_key.offset - start; |
| 7002 | goto not_found_em; |
| 7003 | } |
| 7004 | |
Nikolay Borisov | fc4f21b1 | 2017-02-20 13:51:06 +0200 | [diff] [blame] | 7005 | btrfs_extent_item_to_extent_map(inode, path, item, |
Nikolay Borisov | 9cdc512 | 2017-02-20 13:51:02 +0200 | [diff] [blame] | 7006 | new_inline, em); |
Filipe Manana | 7ffbb59 | 2014-06-09 03:48:05 +0100 | [diff] [blame] | 7007 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7008 | if (found_type == BTRFS_FILE_EXTENT_REG || |
| 7009 | found_type == BTRFS_FILE_EXTENT_PREALLOC) { |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7010 | goto insert; |
| 7011 | } else if (found_type == BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 7012 | unsigned long ptr; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7013 | char *map; |
Chris Mason | 3326d1b | 2007-10-15 16:18:25 -0400 | [diff] [blame] | 7014 | size_t size; |
| 7015 | size_t extent_offset; |
| 7016 | size_t copy_size; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7017 | |
Filipe Manana | 7ffbb59 | 2014-06-09 03:48:05 +0100 | [diff] [blame] | 7018 | if (new_inline) |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 7019 | goto out; |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 7020 | |
Chris Mason | 514ac8a | 2014-01-03 21:07:00 -0800 | [diff] [blame] | 7021 | size = btrfs_file_extent_inline_len(leaf, path->slots[0], item); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 7022 | extent_offset = page_offset(page) + pg_offset - extent_start; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 7023 | copy_size = min_t(u64, PAGE_SIZE - pg_offset, |
| 7024 | size - extent_offset); |
Chris Mason | 3326d1b | 2007-10-15 16:18:25 -0400 | [diff] [blame] | 7025 | em->start = extent_start + extent_offset; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7026 | em->len = ALIGN(copy_size, fs_info->sectorsize); |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 7027 | em->orig_block_len = em->len; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 7028 | em->orig_start = em->start; |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 7029 | ptr = btrfs_file_extent_inline_start(item) + extent_offset; |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 7030 | if (create == 0 && !PageUptodate(page)) { |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 7031 | if (btrfs_file_extent_compression(leaf, item) != |
| 7032 | BTRFS_COMPRESS_NONE) { |
Byongho Lee | e40da0e | 2015-05-19 23:46:45 +0900 | [diff] [blame] | 7033 | ret = uncompress_inline(path, page, pg_offset, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 7034 | extent_offset, item); |
Zach Brown | 166ae5a | 2014-05-09 17:15:10 -0400 | [diff] [blame] | 7035 | if (ret) { |
| 7036 | err = ret; |
| 7037 | goto out; |
| 7038 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 7039 | } else { |
| 7040 | map = kmap(page); |
| 7041 | read_extent_buffer(leaf, map + pg_offset, ptr, |
| 7042 | copy_size); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 7043 | if (pg_offset + copy_size < PAGE_SIZE) { |
Chris Mason | 93c82d5 | 2009-09-11 12:36:29 -0400 | [diff] [blame] | 7044 | memset(map + pg_offset + copy_size, 0, |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 7045 | PAGE_SIZE - pg_offset - |
Chris Mason | 93c82d5 | 2009-09-11 12:36:29 -0400 | [diff] [blame] | 7046 | copy_size); |
| 7047 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 7048 | kunmap(page); |
| 7049 | } |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 7050 | flush_dcache_page(page); |
| 7051 | } else if (create && PageUptodate(page)) { |
Jan Schmidt | 6bf7e08 | 2011-12-01 14:35:19 +0100 | [diff] [blame] | 7052 | BUG(); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 7053 | if (!trans) { |
| 7054 | kunmap(page); |
| 7055 | free_extent_map(em); |
| 7056 | em = NULL; |
Chris Mason | ff5714c | 2011-05-28 07:00:39 -0400 | [diff] [blame] | 7057 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 7058 | btrfs_release_path(path); |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 7059 | trans = btrfs_join_transaction(root); |
Chris Mason | ff5714c | 2011-05-28 07:00:39 -0400 | [diff] [blame] | 7060 | |
Tsutomu Itoh | 3612b49 | 2011-01-25 02:51:38 +0000 | [diff] [blame] | 7061 | if (IS_ERR(trans)) |
| 7062 | return ERR_CAST(trans); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 7063 | goto again; |
| 7064 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 7065 | map = kmap(page); |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 7066 | write_extent_buffer(leaf, map + pg_offset, ptr, |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 7067 | copy_size); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 7068 | kunmap(page); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 7069 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7070 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 7071 | set_extent_uptodate(io_tree, em->start, |
Arne Jansen | 507903b | 2011-04-06 10:02:20 +0000 | [diff] [blame] | 7072 | extent_map_end(em) - 1, NULL, GFP_NOFS); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7073 | goto insert; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7074 | } |
| 7075 | not_found: |
| 7076 | em->start = start; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 7077 | em->orig_start = start; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 7078 | em->len = len; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7079 | not_found_em: |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 7080 | em->block_start = EXTENT_MAP_HOLE; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 7081 | set_bit(EXTENT_FLAG_VACANCY, &em->flags); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7082 | insert: |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 7083 | btrfs_release_path(path); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 7084 | if (em->start > start || extent_map_end(em) <= start) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7085 | btrfs_err(fs_info, |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 7086 | "bad extent! em: [%llu %llu] passed [%llu %llu]", |
| 7087 | em->start, em->len, start, len); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7088 | err = -EIO; |
| 7089 | goto out; |
| 7090 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 7091 | |
| 7092 | err = 0; |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 7093 | write_lock(&em_tree->lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 7094 | ret = add_extent_mapping(em_tree, em, 0); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 7095 | /* it is possible that someone inserted the extent into the tree |
| 7096 | * while we had the lock dropped. It is also possible that |
| 7097 | * an overlapping map exists in the tree |
| 7098 | */ |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7099 | if (ret == -EEXIST) { |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 7100 | struct extent_map *existing; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7101 | |
| 7102 | ret = 0; |
| 7103 | |
Qu Wenruo | e6c4efd | 2014-09-17 11:53:35 +0800 | [diff] [blame] | 7104 | existing = search_extent_mapping(em_tree, start, len); |
| 7105 | /* |
| 7106 | * existing will always be non-NULL, since there must be |
| 7107 | * extent causing the -EEXIST. |
| 7108 | */ |
Chris Mason | 8dff9c8 | 2015-09-19 11:28:25 -0700 | [diff] [blame] | 7109 | if (existing->start == em->start && |
Omar Sandoval | 8e2bd3b | 2016-11-09 15:26:50 -0800 | [diff] [blame] | 7110 | extent_map_end(existing) >= extent_map_end(em) && |
Chris Mason | 8dff9c8 | 2015-09-19 11:28:25 -0700 | [diff] [blame] | 7111 | em->block_start == existing->block_start) { |
| 7112 | /* |
Omar Sandoval | 8e2bd3b | 2016-11-09 15:26:50 -0800 | [diff] [blame] | 7113 | * The existing extent map already encompasses the |
| 7114 | * entire extent map we tried to add. |
Chris Mason | 8dff9c8 | 2015-09-19 11:28:25 -0700 | [diff] [blame] | 7115 | */ |
| 7116 | free_extent_map(em); |
| 7117 | em = existing; |
| 7118 | err = 0; |
| 7119 | |
| 7120 | } else if (start >= extent_map_end(existing) || |
Qu Wenruo | 32be3a1 | 2014-09-22 09:13:03 +0800 | [diff] [blame] | 7121 | start <= existing->start) { |
Qu Wenruo | e6c4efd | 2014-09-17 11:53:35 +0800 | [diff] [blame] | 7122 | /* |
| 7123 | * The existing extent map is the one nearest to |
| 7124 | * the [start, start + len) range which overlaps |
| 7125 | */ |
| 7126 | err = merge_extent_mapping(em_tree, existing, |
| 7127 | em, start); |
Chris Mason | e1c4b74 | 2008-04-22 13:26:46 -0400 | [diff] [blame] | 7128 | free_extent_map(existing); |
Qu Wenruo | e6c4efd | 2014-09-17 11:53:35 +0800 | [diff] [blame] | 7129 | if (err) { |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 7130 | free_extent_map(em); |
| 7131 | em = NULL; |
| 7132 | } |
| 7133 | } else { |
| 7134 | free_extent_map(em); |
| 7135 | em = existing; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7136 | err = 0; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7137 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7138 | } |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 7139 | write_unlock(&em_tree->lock); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7140 | out: |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 7141 | |
Nikolay Borisov | fc4f21b1 | 2017-02-20 13:51:06 +0200 | [diff] [blame] | 7142 | trace_btrfs_get_extent(root, inode, em); |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 7143 | |
Tsutomu Itoh | 527afb4 | 2015-08-19 14:55:00 +0900 | [diff] [blame] | 7144 | btrfs_free_path(path); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7145 | if (trans) { |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 7146 | ret = btrfs_end_transaction(trans); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 7147 | if (!err) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7148 | err = ret; |
| 7149 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7150 | if (err) { |
| 7151 | free_extent_map(em); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7152 | return ERR_PTR(err); |
| 7153 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 7154 | BUG_ON(!em); /* Error is always set */ |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7155 | return em; |
| 7156 | } |
| 7157 | |
Nikolay Borisov | fc4f21b1 | 2017-02-20 13:51:06 +0200 | [diff] [blame] | 7158 | struct extent_map *btrfs_get_extent_fiemap(struct btrfs_inode *inode, |
| 7159 | struct page *page, |
| 7160 | size_t pg_offset, u64 start, u64 len, |
| 7161 | int create) |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 7162 | { |
| 7163 | struct extent_map *em; |
| 7164 | struct extent_map *hole_em = NULL; |
| 7165 | u64 range_start = start; |
| 7166 | u64 end; |
| 7167 | u64 found; |
| 7168 | u64 found_end; |
| 7169 | int err = 0; |
| 7170 | |
| 7171 | em = btrfs_get_extent(inode, page, pg_offset, start, len, create); |
| 7172 | if (IS_ERR(em)) |
| 7173 | return em; |
Dan Carpenter | 9986277 | 2017-04-11 11:57:15 +0300 | [diff] [blame] | 7174 | /* |
| 7175 | * If our em maps to: |
| 7176 | * - a hole or |
| 7177 | * - a pre-alloc extent, |
| 7178 | * there might actually be delalloc bytes behind it. |
| 7179 | */ |
| 7180 | if (em->block_start != EXTENT_MAP_HOLE && |
| 7181 | !test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) |
| 7182 | return em; |
| 7183 | else |
| 7184 | hole_em = em; |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 7185 | |
| 7186 | /* check to see if we've wrapped (len == -1 or similar) */ |
| 7187 | end = start + len; |
| 7188 | if (end < start) |
| 7189 | end = (u64)-1; |
| 7190 | else |
| 7191 | end -= 1; |
| 7192 | |
| 7193 | em = NULL; |
| 7194 | |
| 7195 | /* ok, we didn't find anything, lets look for delalloc */ |
Nikolay Borisov | fc4f21b1 | 2017-02-20 13:51:06 +0200 | [diff] [blame] | 7196 | found = count_range_bits(&inode->io_tree, &range_start, |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 7197 | end, len, EXTENT_DELALLOC, 1); |
| 7198 | found_end = range_start + found; |
| 7199 | if (found_end < range_start) |
| 7200 | found_end = (u64)-1; |
| 7201 | |
| 7202 | /* |
| 7203 | * we didn't find anything useful, return |
| 7204 | * the original results from get_extent() |
| 7205 | */ |
| 7206 | if (range_start > end || found_end <= start) { |
| 7207 | em = hole_em; |
| 7208 | hole_em = NULL; |
| 7209 | goto out; |
| 7210 | } |
| 7211 | |
| 7212 | /* adjust the range_start to make sure it doesn't |
| 7213 | * go backwards from the start they passed in |
| 7214 | */ |
Dulshani Gunawardhana | 6787125 | 2013-10-31 10:33:04 +0530 | [diff] [blame] | 7215 | range_start = max(start, range_start); |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 7216 | found = found_end - range_start; |
| 7217 | |
| 7218 | if (found > 0) { |
| 7219 | u64 hole_start = start; |
| 7220 | u64 hole_len = len; |
| 7221 | |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 7222 | em = alloc_extent_map(); |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 7223 | if (!em) { |
| 7224 | err = -ENOMEM; |
| 7225 | goto out; |
| 7226 | } |
| 7227 | /* |
| 7228 | * when btrfs_get_extent can't find anything it |
| 7229 | * returns one huge hole |
| 7230 | * |
| 7231 | * make sure what it found really fits our range, and |
| 7232 | * adjust to make sure it is based on the start from |
| 7233 | * the caller |
| 7234 | */ |
| 7235 | if (hole_em) { |
| 7236 | u64 calc_end = extent_map_end(hole_em); |
| 7237 | |
| 7238 | if (calc_end <= start || (hole_em->start > end)) { |
| 7239 | free_extent_map(hole_em); |
| 7240 | hole_em = NULL; |
| 7241 | } else { |
| 7242 | hole_start = max(hole_em->start, start); |
| 7243 | hole_len = calc_end - hole_start; |
| 7244 | } |
| 7245 | } |
| 7246 | em->bdev = NULL; |
| 7247 | if (hole_em && range_start > hole_start) { |
| 7248 | /* our hole starts before our delalloc, so we |
| 7249 | * have to return just the parts of the hole |
| 7250 | * that go until the delalloc starts |
| 7251 | */ |
| 7252 | em->len = min(hole_len, |
| 7253 | range_start - hole_start); |
| 7254 | em->start = hole_start; |
| 7255 | em->orig_start = hole_start; |
| 7256 | /* |
| 7257 | * don't adjust block start at all, |
| 7258 | * it is fixed at EXTENT_MAP_HOLE |
| 7259 | */ |
| 7260 | em->block_start = hole_em->block_start; |
| 7261 | em->block_len = hole_len; |
Liu Bo | f9e4fb5 | 2013-01-07 10:10:12 +0000 | [diff] [blame] | 7262 | if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags)) |
| 7263 | set_bit(EXTENT_FLAG_PREALLOC, &em->flags); |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 7264 | } else { |
| 7265 | em->start = range_start; |
| 7266 | em->len = found; |
| 7267 | em->orig_start = range_start; |
| 7268 | em->block_start = EXTENT_MAP_DELALLOC; |
| 7269 | em->block_len = found; |
| 7270 | } |
| 7271 | } else if (hole_em) { |
| 7272 | return hole_em; |
| 7273 | } |
| 7274 | out: |
| 7275 | |
| 7276 | free_extent_map(hole_em); |
| 7277 | if (err) { |
| 7278 | free_extent_map(em); |
| 7279 | return ERR_PTR(err); |
| 7280 | } |
| 7281 | return em; |
| 7282 | } |
| 7283 | |
Filipe Manana | 5f9a8a5 | 2016-05-12 13:53:36 +0100 | [diff] [blame] | 7284 | static struct extent_map *btrfs_create_dio_extent(struct inode *inode, |
| 7285 | const u64 start, |
| 7286 | const u64 len, |
| 7287 | const u64 orig_start, |
| 7288 | const u64 block_start, |
| 7289 | const u64 block_len, |
| 7290 | const u64 orig_block_len, |
| 7291 | const u64 ram_bytes, |
| 7292 | const int type) |
| 7293 | { |
| 7294 | struct extent_map *em = NULL; |
| 7295 | int ret; |
| 7296 | |
Filipe Manana | 5f9a8a5 | 2016-05-12 13:53:36 +0100 | [diff] [blame] | 7297 | if (type != BTRFS_ORDERED_NOCOW) { |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 7298 | em = create_io_em(inode, start, len, orig_start, |
| 7299 | block_start, block_len, orig_block_len, |
| 7300 | ram_bytes, |
| 7301 | BTRFS_COMPRESS_NONE, /* compress_type */ |
| 7302 | type); |
Filipe Manana | 5f9a8a5 | 2016-05-12 13:53:36 +0100 | [diff] [blame] | 7303 | if (IS_ERR(em)) |
| 7304 | goto out; |
| 7305 | } |
| 7306 | ret = btrfs_add_ordered_extent_dio(inode, start, block_start, |
| 7307 | len, block_len, type); |
| 7308 | if (ret) { |
| 7309 | if (em) { |
| 7310 | free_extent_map(em); |
Nikolay Borisov | dcdbc05 | 2017-02-20 13:50:45 +0200 | [diff] [blame] | 7311 | btrfs_drop_extent_cache(BTRFS_I(inode), start, |
Filipe Manana | 5f9a8a5 | 2016-05-12 13:53:36 +0100 | [diff] [blame] | 7312 | start + len - 1, 0); |
| 7313 | } |
| 7314 | em = ERR_PTR(ret); |
| 7315 | } |
| 7316 | out: |
Filipe Manana | 5f9a8a5 | 2016-05-12 13:53:36 +0100 | [diff] [blame] | 7317 | |
| 7318 | return em; |
| 7319 | } |
| 7320 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7321 | static struct extent_map *btrfs_new_extent_direct(struct inode *inode, |
| 7322 | u64 start, u64 len) |
| 7323 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7324 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7325 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 7326 | struct extent_map *em; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7327 | struct btrfs_key ins; |
| 7328 | u64 alloc_hint; |
| 7329 | int ret; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7330 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7331 | alloc_hint = get_extent_allocation_hint(inode, start, len); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7332 | ret = btrfs_reserve_extent(root, len, len, fs_info->sectorsize, |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 7333 | 0, alloc_hint, &ins, 1, 1); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 7334 | if (ret) |
| 7335 | return ERR_PTR(ret); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7336 | |
Filipe Manana | 5f9a8a5 | 2016-05-12 13:53:36 +0100 | [diff] [blame] | 7337 | em = btrfs_create_dio_extent(inode, start, ins.offset, start, |
| 7338 | ins.objectid, ins.offset, ins.offset, |
Liu Bo | 6288d6e | 2017-02-21 12:12:58 -0800 | [diff] [blame] | 7339 | ins.offset, BTRFS_ORDERED_REGULAR); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7340 | btrfs_dec_block_group_reservations(fs_info, ins.objectid); |
Filipe Manana | 5f9a8a5 | 2016-05-12 13:53:36 +0100 | [diff] [blame] | 7341 | if (IS_ERR(em)) |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 7342 | btrfs_free_reserved_extent(fs_info, ins.objectid, |
| 7343 | ins.offset, 1); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 7344 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7345 | return em; |
| 7346 | } |
| 7347 | |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7348 | /* |
| 7349 | * returns 1 when the nocow is safe, < 1 on error, 0 if the |
| 7350 | * block must be cow'd |
| 7351 | */ |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 7352 | noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len, |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 7353 | u64 *orig_start, u64 *orig_block_len, |
| 7354 | u64 *ram_bytes) |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7355 | { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 7356 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7357 | struct btrfs_path *path; |
| 7358 | int ret; |
| 7359 | struct extent_buffer *leaf; |
| 7360 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Miao Xie | 7b2b708 | 2014-02-27 13:58:05 +0800 | [diff] [blame] | 7361 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7362 | struct btrfs_file_extent_item *fi; |
| 7363 | struct btrfs_key key; |
| 7364 | u64 disk_bytenr; |
| 7365 | u64 backref_offset; |
| 7366 | u64 extent_end; |
| 7367 | u64 num_bytes; |
| 7368 | int slot; |
| 7369 | int found_type; |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 7370 | bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW); |
Miao Xie | e77751a | 2013-12-27 21:11:50 +0800 | [diff] [blame] | 7371 | |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7372 | path = btrfs_alloc_path(); |
| 7373 | if (!path) |
| 7374 | return -ENOMEM; |
| 7375 | |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 7376 | ret = btrfs_lookup_file_extent(NULL, root, path, |
| 7377 | btrfs_ino(BTRFS_I(inode)), offset, 0); |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7378 | if (ret < 0) |
| 7379 | goto out; |
| 7380 | |
| 7381 | slot = path->slots[0]; |
| 7382 | if (ret == 1) { |
| 7383 | if (slot == 0) { |
| 7384 | /* can't find the item, must cow */ |
| 7385 | ret = 0; |
| 7386 | goto out; |
| 7387 | } |
| 7388 | slot--; |
| 7389 | } |
| 7390 | ret = 0; |
| 7391 | leaf = path->nodes[0]; |
| 7392 | btrfs_item_key_to_cpu(leaf, &key, slot); |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 7393 | if (key.objectid != btrfs_ino(BTRFS_I(inode)) || |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7394 | key.type != BTRFS_EXTENT_DATA_KEY) { |
| 7395 | /* not our file or wrong item type, must cow */ |
| 7396 | goto out; |
| 7397 | } |
| 7398 | |
| 7399 | if (key.offset > offset) { |
| 7400 | /* Wrong offset, must cow */ |
| 7401 | goto out; |
| 7402 | } |
| 7403 | |
| 7404 | fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item); |
| 7405 | found_type = btrfs_file_extent_type(leaf, fi); |
| 7406 | if (found_type != BTRFS_FILE_EXTENT_REG && |
| 7407 | found_type != BTRFS_FILE_EXTENT_PREALLOC) { |
| 7408 | /* not a regular extent, must cow */ |
| 7409 | goto out; |
| 7410 | } |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 7411 | |
| 7412 | if (!nocow && found_type == BTRFS_FILE_EXTENT_REG) |
| 7413 | goto out; |
| 7414 | |
Miao Xie | e77751a | 2013-12-27 21:11:50 +0800 | [diff] [blame] | 7415 | extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi); |
| 7416 | if (extent_end <= offset) |
| 7417 | goto out; |
| 7418 | |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7419 | disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 7420 | if (disk_bytenr == 0) |
| 7421 | goto out; |
| 7422 | |
| 7423 | if (btrfs_file_extent_compression(leaf, fi) || |
| 7424 | btrfs_file_extent_encryption(leaf, fi) || |
| 7425 | btrfs_file_extent_other_encoding(leaf, fi)) |
| 7426 | goto out; |
| 7427 | |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7428 | backref_offset = btrfs_file_extent_offset(leaf, fi); |
| 7429 | |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 7430 | if (orig_start) { |
| 7431 | *orig_start = key.offset - backref_offset; |
| 7432 | *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi); |
| 7433 | *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi); |
| 7434 | } |
Josef Bacik | eb384b5 | 2013-04-24 16:32:55 -0400 | [diff] [blame] | 7435 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 7436 | if (btrfs_extent_readonly(fs_info, disk_bytenr)) |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7437 | goto out; |
Miao Xie | 7b2b708 | 2014-02-27 13:58:05 +0800 | [diff] [blame] | 7438 | |
| 7439 | num_bytes = min(offset + *len, extent_end) - offset; |
| 7440 | if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) { |
| 7441 | u64 range_end; |
| 7442 | |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 7443 | range_end = round_up(offset + num_bytes, |
| 7444 | root->fs_info->sectorsize) - 1; |
Miao Xie | 7b2b708 | 2014-02-27 13:58:05 +0800 | [diff] [blame] | 7445 | ret = test_range_bit(io_tree, offset, range_end, |
| 7446 | EXTENT_DELALLOC, 0, NULL); |
| 7447 | if (ret) { |
| 7448 | ret = -EAGAIN; |
| 7449 | goto out; |
| 7450 | } |
| 7451 | } |
| 7452 | |
Josef Bacik | 1bda19e | 2013-10-18 12:10:36 -0400 | [diff] [blame] | 7453 | btrfs_release_path(path); |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7454 | |
| 7455 | /* |
| 7456 | * look for other files referencing this extent, if we |
| 7457 | * find any we must cow |
| 7458 | */ |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 7459 | |
Liu Bo | e4c3b2d | 2017-01-30 12:25:28 -0800 | [diff] [blame] | 7460 | ret = btrfs_cross_ref_exist(root, btrfs_ino(BTRFS_I(inode)), |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 7461 | key.offset - backref_offset, disk_bytenr); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 7462 | if (ret) { |
| 7463 | ret = 0; |
| 7464 | goto out; |
| 7465 | } |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7466 | |
| 7467 | /* |
| 7468 | * adjust disk_bytenr and num_bytes to cover just the bytes |
| 7469 | * in this extent we are about to write. If there |
| 7470 | * are any csums in that range we have to cow in order |
| 7471 | * to keep the csums correct |
| 7472 | */ |
| 7473 | disk_bytenr += backref_offset; |
| 7474 | disk_bytenr += offset - key.offset; |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 7475 | if (csum_exist_in_range(fs_info, disk_bytenr, num_bytes)) |
| 7476 | goto out; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7477 | /* |
| 7478 | * all of the above have passed, it is safe to overwrite this extent |
| 7479 | * without cow |
| 7480 | */ |
Josef Bacik | eb384b5 | 2013-04-24 16:32:55 -0400 | [diff] [blame] | 7481 | *len = num_bytes; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7482 | ret = 1; |
| 7483 | out: |
| 7484 | btrfs_free_path(path); |
| 7485 | return ret; |
| 7486 | } |
| 7487 | |
Alex Gartrell | fc4adbf | 2014-05-20 13:07:56 -0700 | [diff] [blame] | 7488 | bool btrfs_page_exists_in_range(struct inode *inode, loff_t start, loff_t end) |
| 7489 | { |
| 7490 | struct radix_tree_root *root = &inode->i_mapping->page_tree; |
David Sterba | e03733d | 2017-05-12 01:02:22 +0200 | [diff] [blame] | 7491 | bool found = false; |
Alex Gartrell | fc4adbf | 2014-05-20 13:07:56 -0700 | [diff] [blame] | 7492 | void **pagep = NULL; |
| 7493 | struct page *page = NULL; |
David Sterba | cc2b702 | 2017-05-12 01:03:52 +0200 | [diff] [blame] | 7494 | unsigned long start_idx; |
| 7495 | unsigned long end_idx; |
Alex Gartrell | fc4adbf | 2014-05-20 13:07:56 -0700 | [diff] [blame] | 7496 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 7497 | start_idx = start >> PAGE_SHIFT; |
Alex Gartrell | fc4adbf | 2014-05-20 13:07:56 -0700 | [diff] [blame] | 7498 | |
| 7499 | /* |
| 7500 | * end is the last byte in the last page. end == start is legal |
| 7501 | */ |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 7502 | end_idx = end >> PAGE_SHIFT; |
Alex Gartrell | fc4adbf | 2014-05-20 13:07:56 -0700 | [diff] [blame] | 7503 | |
| 7504 | rcu_read_lock(); |
| 7505 | |
| 7506 | /* Most of the code in this while loop is lifted from |
| 7507 | * find_get_page. It's been modified to begin searching from a |
| 7508 | * page and return just the first page found in that range. If the |
| 7509 | * found idx is less than or equal to the end idx then we know that |
| 7510 | * a page exists. If no pages are found or if those pages are |
| 7511 | * outside of the range then we're fine (yay!) */ |
| 7512 | while (page == NULL && |
| 7513 | radix_tree_gang_lookup_slot(root, &pagep, NULL, start_idx, 1)) { |
| 7514 | page = radix_tree_deref_slot(pagep); |
| 7515 | if (unlikely(!page)) |
| 7516 | break; |
| 7517 | |
| 7518 | if (radix_tree_exception(page)) { |
Filipe Manana | 809f901 | 2014-06-05 13:22:25 +0100 | [diff] [blame] | 7519 | if (radix_tree_deref_retry(page)) { |
| 7520 | page = NULL; |
Alex Gartrell | fc4adbf | 2014-05-20 13:07:56 -0700 | [diff] [blame] | 7521 | continue; |
Filipe Manana | 809f901 | 2014-06-05 13:22:25 +0100 | [diff] [blame] | 7522 | } |
Alex Gartrell | fc4adbf | 2014-05-20 13:07:56 -0700 | [diff] [blame] | 7523 | /* |
| 7524 | * Otherwise, shmem/tmpfs must be storing a swap entry |
| 7525 | * here as an exceptional entry: so return it without |
| 7526 | * attempting to raise page count. |
| 7527 | */ |
Filipe Manana | 6fdef6d | 2014-06-05 13:22:26 +0100 | [diff] [blame] | 7528 | page = NULL; |
Alex Gartrell | fc4adbf | 2014-05-20 13:07:56 -0700 | [diff] [blame] | 7529 | break; /* TODO: Is this relevant for this use case? */ |
| 7530 | } |
| 7531 | |
Filipe Manana | 9140515 | 2014-06-05 13:22:24 +0100 | [diff] [blame] | 7532 | if (!page_cache_get_speculative(page)) { |
| 7533 | page = NULL; |
Alex Gartrell | fc4adbf | 2014-05-20 13:07:56 -0700 | [diff] [blame] | 7534 | continue; |
Filipe Manana | 9140515 | 2014-06-05 13:22:24 +0100 | [diff] [blame] | 7535 | } |
Alex Gartrell | fc4adbf | 2014-05-20 13:07:56 -0700 | [diff] [blame] | 7536 | |
| 7537 | /* |
| 7538 | * Has the page moved? |
| 7539 | * This is part of the lockless pagecache protocol. See |
| 7540 | * include/linux/pagemap.h for details. |
| 7541 | */ |
| 7542 | if (unlikely(page != *pagep)) { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 7543 | put_page(page); |
Alex Gartrell | fc4adbf | 2014-05-20 13:07:56 -0700 | [diff] [blame] | 7544 | page = NULL; |
| 7545 | } |
| 7546 | } |
| 7547 | |
| 7548 | if (page) { |
| 7549 | if (page->index <= end_idx) |
| 7550 | found = true; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 7551 | put_page(page); |
Alex Gartrell | fc4adbf | 2014-05-20 13:07:56 -0700 | [diff] [blame] | 7552 | } |
| 7553 | |
| 7554 | rcu_read_unlock(); |
| 7555 | return found; |
| 7556 | } |
| 7557 | |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7558 | static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend, |
| 7559 | struct extent_state **cached_state, int writing) |
| 7560 | { |
| 7561 | struct btrfs_ordered_extent *ordered; |
| 7562 | int ret = 0; |
| 7563 | |
| 7564 | while (1) { |
| 7565 | lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend, |
David Sterba | ff13db4 | 2015-12-03 14:30:40 +0100 | [diff] [blame] | 7566 | cached_state); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7567 | /* |
| 7568 | * 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] | 7569 | * 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] | 7570 | * extents in this range. |
| 7571 | */ |
Nikolay Borisov | a776c6f | 2017-02-20 13:50:49 +0200 | [diff] [blame] | 7572 | ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), lockstart, |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7573 | lockend - lockstart + 1); |
| 7574 | |
| 7575 | /* |
| 7576 | * We need to make sure there are no buffered pages in this |
| 7577 | * range either, we could have raced between the invalidate in |
| 7578 | * generic_file_direct_write and locking the extent. The |
| 7579 | * invalidate needs to happen so that reads after a write do not |
| 7580 | * get stale data. |
| 7581 | */ |
Alex Gartrell | fc4adbf | 2014-05-20 13:07:56 -0700 | [diff] [blame] | 7582 | if (!ordered && |
| 7583 | (!writing || |
| 7584 | !btrfs_page_exists_in_range(inode, lockstart, lockend))) |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7585 | break; |
| 7586 | |
| 7587 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend, |
| 7588 | cached_state, GFP_NOFS); |
| 7589 | |
| 7590 | if (ordered) { |
Filipe Manana | ade7702 | 2016-02-18 14:28:55 +0000 | [diff] [blame] | 7591 | /* |
| 7592 | * If we are doing a DIO read and the ordered extent we |
| 7593 | * found is for a buffered write, we can not wait for it |
| 7594 | * to complete and retry, because if we do so we can |
| 7595 | * deadlock with concurrent buffered writes on page |
| 7596 | * locks. This happens only if our DIO read covers more |
| 7597 | * than one extent map, if at this point has already |
| 7598 | * created an ordered extent for a previous extent map |
| 7599 | * and locked its range in the inode's io tree, and a |
| 7600 | * concurrent write against that previous extent map's |
| 7601 | * range and this range started (we unlock the ranges |
| 7602 | * in the io tree only when the bios complete and |
| 7603 | * buffered writes always lock pages before attempting |
| 7604 | * to lock range in the io tree). |
| 7605 | */ |
| 7606 | if (writing || |
| 7607 | test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags)) |
| 7608 | btrfs_start_ordered_extent(inode, ordered, 1); |
| 7609 | else |
| 7610 | ret = -ENOTBLK; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7611 | btrfs_put_ordered_extent(ordered); |
| 7612 | } else { |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7613 | /* |
Filipe Manana | b850ae1 | 2015-12-08 16:23:16 +0000 | [diff] [blame] | 7614 | * We could trigger writeback for this range (and wait |
| 7615 | * for it to complete) and then invalidate the pages for |
| 7616 | * this range (through invalidate_inode_pages2_range()), |
| 7617 | * but that can lead us to a deadlock with a concurrent |
| 7618 | * call to readpages() (a buffered read or a defrag call |
| 7619 | * triggered a readahead) on a page lock due to an |
| 7620 | * ordered dio extent we created before but did not have |
| 7621 | * yet a corresponding bio submitted (whence it can not |
| 7622 | * complete), which makes readpages() wait for that |
| 7623 | * ordered extent to complete while holding a lock on |
| 7624 | * that page. |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7625 | */ |
Filipe Manana | b850ae1 | 2015-12-08 16:23:16 +0000 | [diff] [blame] | 7626 | ret = -ENOTBLK; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7627 | } |
| 7628 | |
Filipe Manana | ade7702 | 2016-02-18 14:28:55 +0000 | [diff] [blame] | 7629 | if (ret) |
| 7630 | break; |
| 7631 | |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7632 | cond_resched(); |
| 7633 | } |
| 7634 | |
| 7635 | return ret; |
| 7636 | } |
| 7637 | |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 7638 | /* The callers of this must take lock_extent() */ |
| 7639 | static struct extent_map *create_io_em(struct inode *inode, u64 start, u64 len, |
| 7640 | u64 orig_start, u64 block_start, |
| 7641 | u64 block_len, u64 orig_block_len, |
| 7642 | u64 ram_bytes, int compress_type, |
| 7643 | int type) |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7644 | { |
| 7645 | struct extent_map_tree *em_tree; |
| 7646 | struct extent_map *em; |
| 7647 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 7648 | int ret; |
| 7649 | |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 7650 | ASSERT(type == BTRFS_ORDERED_PREALLOC || |
| 7651 | type == BTRFS_ORDERED_COMPRESSED || |
| 7652 | type == BTRFS_ORDERED_NOCOW || |
Liu Bo | 1af4a0a | 2017-02-13 15:35:09 -0800 | [diff] [blame] | 7653 | type == BTRFS_ORDERED_REGULAR); |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 7654 | |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7655 | em_tree = &BTRFS_I(inode)->extent_tree; |
| 7656 | em = alloc_extent_map(); |
| 7657 | if (!em) |
| 7658 | return ERR_PTR(-ENOMEM); |
| 7659 | |
| 7660 | em->start = start; |
| 7661 | em->orig_start = orig_start; |
| 7662 | em->len = len; |
| 7663 | em->block_len = block_len; |
| 7664 | em->block_start = block_start; |
| 7665 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 7666 | em->orig_block_len = orig_block_len; |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 7667 | em->ram_bytes = ram_bytes; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 7668 | em->generation = -1; |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7669 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
Liu Bo | 1af4a0a | 2017-02-13 15:35:09 -0800 | [diff] [blame] | 7670 | if (type == BTRFS_ORDERED_PREALLOC) { |
Josef Bacik | b11e234 | 2012-12-03 10:58:15 -0500 | [diff] [blame] | 7671 | set_bit(EXTENT_FLAG_FILLING, &em->flags); |
Liu Bo | 1af4a0a | 2017-02-13 15:35:09 -0800 | [diff] [blame] | 7672 | } else if (type == BTRFS_ORDERED_COMPRESSED) { |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 7673 | set_bit(EXTENT_FLAG_COMPRESSED, &em->flags); |
| 7674 | em->compress_type = compress_type; |
| 7675 | } |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7676 | |
| 7677 | do { |
Nikolay Borisov | dcdbc05 | 2017-02-20 13:50:45 +0200 | [diff] [blame] | 7678 | btrfs_drop_extent_cache(BTRFS_I(inode), em->start, |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7679 | em->start + em->len - 1, 0); |
| 7680 | write_lock(&em_tree->lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 7681 | ret = add_extent_mapping(em_tree, em, 1); |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7682 | write_unlock(&em_tree->lock); |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 7683 | /* |
| 7684 | * The caller has taken lock_extent(), who could race with us |
| 7685 | * to add em? |
| 7686 | */ |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7687 | } while (ret == -EEXIST); |
| 7688 | |
| 7689 | if (ret) { |
| 7690 | free_extent_map(em); |
| 7691 | return ERR_PTR(ret); |
| 7692 | } |
| 7693 | |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 7694 | /* 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] | 7695 | return em; |
| 7696 | } |
| 7697 | |
Filipe Manana | 9c9464c | 2015-11-04 09:52:04 +0000 | [diff] [blame] | 7698 | static void adjust_dio_outstanding_extents(struct inode *inode, |
| 7699 | struct btrfs_dio_data *dio_data, |
| 7700 | const u64 len) |
| 7701 | { |
David Sterba | 823bb20 | 2017-01-04 11:09:51 +0100 | [diff] [blame] | 7702 | unsigned num_extents = count_max_extents(len); |
Filipe Manana | 9c9464c | 2015-11-04 09:52:04 +0000 | [diff] [blame] | 7703 | |
Filipe Manana | 9c9464c | 2015-11-04 09:52:04 +0000 | [diff] [blame] | 7704 | /* |
| 7705 | * If we have an outstanding_extents count still set then we're |
| 7706 | * within our reservation, otherwise we need to adjust our inode |
| 7707 | * counter appropriately. |
| 7708 | */ |
Liu Bo | c293166 | 2016-12-22 17:13:54 -0800 | [diff] [blame] | 7709 | if (dio_data->outstanding_extents >= num_extents) { |
Filipe Manana | 9c9464c | 2015-11-04 09:52:04 +0000 | [diff] [blame] | 7710 | dio_data->outstanding_extents -= num_extents; |
| 7711 | } else { |
Liu Bo | c293166 | 2016-12-22 17:13:54 -0800 | [diff] [blame] | 7712 | /* |
| 7713 | * If dio write length has been split due to no large enough |
| 7714 | * contiguous space, we need to compensate our inode counter |
| 7715 | * appropriately. |
| 7716 | */ |
| 7717 | u64 num_needed = num_extents - dio_data->outstanding_extents; |
| 7718 | |
Filipe Manana | 9c9464c | 2015-11-04 09:52:04 +0000 | [diff] [blame] | 7719 | spin_lock(&BTRFS_I(inode)->lock); |
Liu Bo | c293166 | 2016-12-22 17:13:54 -0800 | [diff] [blame] | 7720 | BTRFS_I(inode)->outstanding_extents += num_needed; |
Filipe Manana | 9c9464c | 2015-11-04 09:52:04 +0000 | [diff] [blame] | 7721 | spin_unlock(&BTRFS_I(inode)->lock); |
| 7722 | } |
| 7723 | } |
| 7724 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7725 | static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock, |
| 7726 | struct buffer_head *bh_result, int create) |
| 7727 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7728 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7729 | struct extent_map *em; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7730 | struct extent_state *cached_state = NULL; |
chandan | 50745b0 | 2015-08-28 21:10:13 +0530 | [diff] [blame] | 7731 | struct btrfs_dio_data *dio_data = NULL; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7732 | u64 start = iblock << inode->i_blkbits; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7733 | u64 lockstart, lockend; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7734 | u64 len = bh_result->b_size; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7735 | int unlock_bits = EXTENT_LOCKED; |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 7736 | int ret = 0; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7737 | |
Miao Xie | 172a504 | 2013-02-21 02:48:22 -0700 | [diff] [blame] | 7738 | if (create) |
Josef Bacik | 3266789 | 2015-02-11 15:08:58 -0500 | [diff] [blame] | 7739 | unlock_bits |= EXTENT_DIRTY; |
Miao Xie | 172a504 | 2013-02-21 02:48:22 -0700 | [diff] [blame] | 7740 | else |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7741 | len = min_t(u64, len, fs_info->sectorsize); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7742 | |
Josef Bacik | c329861 | 2012-08-03 16:49:19 -0400 | [diff] [blame] | 7743 | lockstart = start; |
| 7744 | lockend = start + len - 1; |
| 7745 | |
Josef Bacik | e1cbbfa | 2015-03-17 10:52:28 -0400 | [diff] [blame] | 7746 | if (current->journal_info) { |
| 7747 | /* |
| 7748 | * 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] | 7749 | * 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] | 7750 | * confused. |
| 7751 | */ |
chandan | 50745b0 | 2015-08-28 21:10:13 +0530 | [diff] [blame] | 7752 | dio_data = current->journal_info; |
Josef Bacik | e1cbbfa | 2015-03-17 10:52:28 -0400 | [diff] [blame] | 7753 | current->journal_info = NULL; |
| 7754 | } |
| 7755 | |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7756 | /* |
| 7757 | * If this errors out it's because we couldn't invalidate pagecache for |
| 7758 | * this range and we need to fallback to buffered. |
| 7759 | */ |
Filipe Manana | 9c9464c | 2015-11-04 09:52:04 +0000 | [diff] [blame] | 7760 | if (lock_extent_direct(inode, lockstart, lockend, &cached_state, |
| 7761 | create)) { |
| 7762 | ret = -ENOTBLK; |
| 7763 | goto err; |
| 7764 | } |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7765 | |
Nikolay Borisov | fc4f21b1 | 2017-02-20 13:51:06 +0200 | [diff] [blame] | 7766 | em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len, 0); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7767 | if (IS_ERR(em)) { |
| 7768 | ret = PTR_ERR(em); |
| 7769 | goto unlock_err; |
| 7770 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7771 | |
| 7772 | /* |
| 7773 | * Ok for INLINE and COMPRESSED extents we need to fallback on buffered |
| 7774 | * io. INLINE is special, and we could probably kludge it in here, but |
| 7775 | * it's still buffered so for safety lets just fall back to the generic |
| 7776 | * buffered path. |
| 7777 | * |
| 7778 | * For COMPRESSED we _have_ to read the entire extent in so we can |
| 7779 | * decompress it, so there will be buffering required no matter what we |
| 7780 | * do, so go ahead and fallback to buffered. |
| 7781 | * |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 7782 | * 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] | 7783 | * to buffered IO. Don't blame me, this is the price we pay for using |
| 7784 | * the generic code. |
| 7785 | */ |
| 7786 | if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) || |
| 7787 | em->block_start == EXTENT_MAP_INLINE) { |
| 7788 | free_extent_map(em); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7789 | ret = -ENOTBLK; |
| 7790 | goto unlock_err; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7791 | } |
| 7792 | |
| 7793 | /* Just a good old fashioned hole, return */ |
| 7794 | if (!create && (em->block_start == EXTENT_MAP_HOLE || |
| 7795 | test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) { |
| 7796 | free_extent_map(em); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7797 | goto unlock_err; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7798 | } |
| 7799 | |
| 7800 | /* |
| 7801 | * We don't allocate a new extent in the following cases |
| 7802 | * |
| 7803 | * 1) The inode is marked as NODATACOW. In this case we'll just use the |
| 7804 | * existing extent. |
| 7805 | * 2) The extent is marked as PREALLOC. We're good to go here and can |
| 7806 | * just use the extent. |
| 7807 | * |
| 7808 | */ |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7809 | if (!create) { |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7810 | len = min(len, em->len - (start - em->start)); |
| 7811 | lockstart = start + len; |
| 7812 | goto unlock; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7813 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7814 | |
| 7815 | if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) || |
| 7816 | ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) && |
| 7817 | em->block_start != EXTENT_MAP_HOLE)) { |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7818 | int type; |
Josef Bacik | eb384b5 | 2013-04-24 16:32:55 -0400 | [diff] [blame] | 7819 | u64 block_start, orig_start, orig_block_len, ram_bytes; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7820 | |
| 7821 | if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) |
| 7822 | type = BTRFS_ORDERED_PREALLOC; |
| 7823 | else |
| 7824 | type = BTRFS_ORDERED_NOCOW; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7825 | len = min(len, em->len - (start - em->start)); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7826 | block_start = em->block_start + (start - em->start); |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7827 | |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 7828 | if (can_nocow_extent(inode, start, &len, &orig_start, |
Filipe Manana | f78c436 | 2016-05-09 13:15:41 +0100 | [diff] [blame] | 7829 | &orig_block_len, &ram_bytes) == 1 && |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7830 | btrfs_inc_nocow_writers(fs_info, block_start)) { |
Filipe Manana | 5f9a8a5 | 2016-05-12 13:53:36 +0100 | [diff] [blame] | 7831 | struct extent_map *em2; |
Filipe Manana | 0b90191 | 2016-05-09 13:15:27 +0100 | [diff] [blame] | 7832 | |
Filipe Manana | 5f9a8a5 | 2016-05-12 13:53:36 +0100 | [diff] [blame] | 7833 | em2 = btrfs_create_dio_extent(inode, start, len, |
| 7834 | orig_start, block_start, |
| 7835 | len, orig_block_len, |
| 7836 | ram_bytes, type); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7837 | btrfs_dec_nocow_writers(fs_info, block_start); |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7838 | if (type == BTRFS_ORDERED_PREALLOC) { |
| 7839 | free_extent_map(em); |
Filipe Manana | 5f9a8a5 | 2016-05-12 13:53:36 +0100 | [diff] [blame] | 7840 | em = em2; |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7841 | } |
Filipe Manana | 5f9a8a5 | 2016-05-12 13:53:36 +0100 | [diff] [blame] | 7842 | if (em2 && IS_ERR(em2)) { |
| 7843 | ret = PTR_ERR(em2); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7844 | goto unlock_err; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7845 | } |
Wang Xiaoguang | 1851309 | 2016-07-25 15:51:40 +0800 | [diff] [blame] | 7846 | /* |
| 7847 | * For inode marked NODATACOW or extent marked PREALLOC, |
| 7848 | * use the existing or preallocated extent, so does not |
| 7849 | * need to adjust btrfs_space_info's bytes_may_use. |
| 7850 | */ |
| 7851 | btrfs_free_reserved_data_space_noquota(inode, |
| 7852 | start, len); |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7853 | goto unlock; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7854 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7855 | } |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 7856 | |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7857 | /* |
| 7858 | * this will cow the extent, reset the len in case we changed |
| 7859 | * it above |
| 7860 | */ |
| 7861 | len = bh_result->b_size; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 7862 | free_extent_map(em); |
| 7863 | em = btrfs_new_extent_direct(inode, start, len); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7864 | if (IS_ERR(em)) { |
| 7865 | ret = PTR_ERR(em); |
| 7866 | goto unlock_err; |
| 7867 | } |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7868 | len = min(len, em->len - (start - em->start)); |
| 7869 | unlock: |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7870 | bh_result->b_blocknr = (em->block_start + (start - em->start)) >> |
| 7871 | inode->i_blkbits; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7872 | bh_result->b_size = len; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7873 | bh_result->b_bdev = em->bdev; |
| 7874 | set_buffer_mapped(bh_result); |
Josef Bacik | c3473e8 | 2012-06-19 10:59:00 -0400 | [diff] [blame] | 7875 | if (create) { |
| 7876 | if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) |
| 7877 | set_buffer_new(bh_result); |
| 7878 | |
| 7879 | /* |
| 7880 | * Need to update the i_size under the extent lock so buffered |
| 7881 | * readers will get the updated i_size when we unlock. |
| 7882 | */ |
Liu Bo | 4aaedfb | 2016-12-14 22:36:05 -0800 | [diff] [blame] | 7883 | if (!dio_data->overwrite && start + len > i_size_read(inode)) |
Josef Bacik | c3473e8 | 2012-06-19 10:59:00 -0400 | [diff] [blame] | 7884 | i_size_write(inode, start + len); |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 7885 | |
Filipe Manana | 9c9464c | 2015-11-04 09:52:04 +0000 | [diff] [blame] | 7886 | adjust_dio_outstanding_extents(inode, dio_data, len); |
chandan | 50745b0 | 2015-08-28 21:10:13 +0530 | [diff] [blame] | 7887 | WARN_ON(dio_data->reserve < len); |
| 7888 | dio_data->reserve -= len; |
Filipe Manana | f28a492 | 2015-12-08 19:23:20 +0000 | [diff] [blame] | 7889 | dio_data->unsubmitted_oe_range_end = start + len; |
chandan | 50745b0 | 2015-08-28 21:10:13 +0530 | [diff] [blame] | 7890 | current->journal_info = dio_data; |
Josef Bacik | c3473e8 | 2012-06-19 10:59:00 -0400 | [diff] [blame] | 7891 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7892 | |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7893 | /* |
| 7894 | * In the case of write we need to clear and unlock the entire range, |
| 7895 | * in the case of read we need to unlock only the end area that we |
| 7896 | * aren't using if there is any left over space. |
| 7897 | */ |
Liu Bo | 24c03fa | 2012-08-22 20:10:38 -0600 | [diff] [blame] | 7898 | if (lockstart < lockend) { |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 7899 | clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, |
| 7900 | lockend, unlock_bits, 1, 0, |
| 7901 | &cached_state, GFP_NOFS); |
Liu Bo | 24c03fa | 2012-08-22 20:10:38 -0600 | [diff] [blame] | 7902 | } else { |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7903 | free_extent_state(cached_state); |
Liu Bo | 24c03fa | 2012-08-22 20:10:38 -0600 | [diff] [blame] | 7904 | } |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7905 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7906 | free_extent_map(em); |
| 7907 | |
| 7908 | return 0; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7909 | |
| 7910 | unlock_err: |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7911 | clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend, |
| 7912 | unlock_bits, 1, 0, &cached_state, GFP_NOFS); |
Filipe Manana | 9c9464c | 2015-11-04 09:52:04 +0000 | [diff] [blame] | 7913 | err: |
chandan | 50745b0 | 2015-08-28 21:10:13 +0530 | [diff] [blame] | 7914 | if (dio_data) |
| 7915 | current->journal_info = dio_data; |
Filipe Manana | 9c9464c | 2015-11-04 09:52:04 +0000 | [diff] [blame] | 7916 | /* |
| 7917 | * Compensate the delalloc release we do in btrfs_direct_IO() when we |
| 7918 | * write less data then expected, so that we don't underflow our inode's |
| 7919 | * outstanding extents counter. |
| 7920 | */ |
| 7921 | if (create && dio_data) |
| 7922 | adjust_dio_outstanding_extents(inode, dio_data, len); |
| 7923 | |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7924 | return ret; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7925 | } |
| 7926 | |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7927 | static inline int submit_dio_repair_bio(struct inode *inode, struct bio *bio, |
Mike Christie | 81a75f67 | 2016-06-05 14:31:54 -0500 | [diff] [blame] | 7928 | int mirror_num) |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7929 | { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 7930 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7931 | int ret; |
| 7932 | |
Mike Christie | 37226b2 | 2016-06-05 14:31:52 -0500 | [diff] [blame] | 7933 | BUG_ON(bio_op(bio) == REQ_OP_WRITE); |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7934 | |
| 7935 | bio_get(bio); |
| 7936 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 7937 | 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] | 7938 | if (ret) |
| 7939 | goto err; |
| 7940 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 7941 | ret = btrfs_map_bio(fs_info, bio, mirror_num, 0); |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7942 | err: |
| 7943 | bio_put(bio); |
| 7944 | return ret; |
| 7945 | } |
| 7946 | |
| 7947 | static int btrfs_check_dio_repairable(struct inode *inode, |
| 7948 | struct bio *failed_bio, |
| 7949 | struct io_failure_record *failrec, |
| 7950 | int failed_mirror) |
| 7951 | { |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 7952 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7953 | int num_copies; |
| 7954 | |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 7955 | num_copies = btrfs_num_copies(fs_info, failrec->logical, failrec->len); |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7956 | if (num_copies == 1) { |
| 7957 | /* |
| 7958 | * we only have a single copy of the data, so don't bother with |
| 7959 | * all the retry and error correction code that follows. no |
| 7960 | * matter what the error is, it is very likely to persist. |
| 7961 | */ |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 7962 | btrfs_debug(fs_info, |
| 7963 | "Check DIO Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d", |
| 7964 | num_copies, failrec->this_mirror, failed_mirror); |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7965 | return 0; |
| 7966 | } |
| 7967 | |
| 7968 | failrec->failed_mirror = failed_mirror; |
| 7969 | failrec->this_mirror++; |
| 7970 | if (failrec->this_mirror == failed_mirror) |
| 7971 | failrec->this_mirror++; |
| 7972 | |
| 7973 | if (failrec->this_mirror > num_copies) { |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 7974 | btrfs_debug(fs_info, |
| 7975 | "Check DIO Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d", |
| 7976 | num_copies, failrec->this_mirror, failed_mirror); |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7977 | return 0; |
| 7978 | } |
| 7979 | |
| 7980 | return 1; |
| 7981 | } |
| 7982 | |
| 7983 | static int dio_read_error(struct inode *inode, struct bio *failed_bio, |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 7984 | struct page *page, unsigned int pgoff, |
| 7985 | u64 start, u64 end, int failed_mirror, |
| 7986 | bio_end_io_t *repair_endio, void *repair_arg) |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7987 | { |
| 7988 | struct io_failure_record *failrec; |
Josef Bacik | 7870d08 | 2017-05-05 11:57:15 -0400 | [diff] [blame] | 7989 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
| 7990 | struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree; |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7991 | struct bio *bio; |
| 7992 | int isector; |
Christoph Hellwig | 70fd761 | 2016-11-01 07:40:10 -0600 | [diff] [blame] | 7993 | int read_mode = 0; |
Liu Bo | 17347ce | 2017-05-15 15:33:27 -0700 | [diff] [blame] | 7994 | int segs; |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7995 | int ret; |
| 7996 | |
Mike Christie | 37226b2 | 2016-06-05 14:31:52 -0500 | [diff] [blame] | 7997 | BUG_ON(bio_op(failed_bio) == REQ_OP_WRITE); |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7998 | |
| 7999 | ret = btrfs_get_io_failure_record(inode, start, end, &failrec); |
| 8000 | if (ret) |
| 8001 | return ret; |
| 8002 | |
| 8003 | ret = btrfs_check_dio_repairable(inode, failed_bio, failrec, |
| 8004 | failed_mirror); |
| 8005 | if (!ret) { |
Josef Bacik | 7870d08 | 2017-05-05 11:57:15 -0400 | [diff] [blame] | 8006 | free_io_failure(failure_tree, io_tree, failrec); |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8007 | return -EIO; |
| 8008 | } |
| 8009 | |
Liu Bo | 17347ce | 2017-05-15 15:33:27 -0700 | [diff] [blame] | 8010 | segs = bio_segments(failed_bio); |
| 8011 | if (segs > 1 || |
| 8012 | (failed_bio->bi_io_vec->bv_len > btrfs_inode_sectorsize(inode))) |
Christoph Hellwig | 70fd761 | 2016-11-01 07:40:10 -0600 | [diff] [blame] | 8013 | read_mode |= REQ_FAILFAST_DEV; |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8014 | |
| 8015 | isector = start - btrfs_io_bio(failed_bio)->logical; |
| 8016 | isector >>= inode->i_sb->s_blocksize_bits; |
| 8017 | bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page, |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 8018 | pgoff, isector, repair_endio, repair_arg); |
Mike Christie | 37226b2 | 2016-06-05 14:31:52 -0500 | [diff] [blame] | 8019 | bio_set_op_attrs(bio, REQ_OP_READ, read_mode); |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8020 | |
| 8021 | btrfs_debug(BTRFS_I(inode)->root->fs_info, |
| 8022 | "Repair DIO Read Error: submitting new dio read[%#x] to this_mirror=%d, in_validation=%d\n", |
| 8023 | read_mode, failrec->this_mirror, failrec->in_validation); |
| 8024 | |
Mike Christie | 81a75f67 | 2016-06-05 14:31:54 -0500 | [diff] [blame] | 8025 | ret = submit_dio_repair_bio(inode, bio, failrec->this_mirror); |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8026 | if (ret) { |
Josef Bacik | 7870d08 | 2017-05-05 11:57:15 -0400 | [diff] [blame] | 8027 | free_io_failure(failure_tree, io_tree, failrec); |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8028 | bio_put(bio); |
| 8029 | } |
| 8030 | |
| 8031 | return ret; |
| 8032 | } |
| 8033 | |
| 8034 | struct btrfs_retry_complete { |
| 8035 | struct completion done; |
| 8036 | struct inode *inode; |
| 8037 | u64 start; |
| 8038 | int uptodate; |
| 8039 | }; |
| 8040 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 8041 | static void btrfs_retry_endio_nocsum(struct bio *bio) |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8042 | { |
| 8043 | struct btrfs_retry_complete *done = bio->bi_private; |
Josef Bacik | 7870d08 | 2017-05-05 11:57:15 -0400 | [diff] [blame] | 8044 | struct inode *inode = done->inode; |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8045 | struct bio_vec *bvec; |
Josef Bacik | 7870d08 | 2017-05-05 11:57:15 -0400 | [diff] [blame] | 8046 | struct extent_io_tree *io_tree, *failure_tree; |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8047 | int i; |
| 8048 | |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 8049 | if (bio->bi_status) |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8050 | goto end; |
| 8051 | |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 8052 | ASSERT(bio->bi_vcnt == 1); |
Josef Bacik | 7870d08 | 2017-05-05 11:57:15 -0400 | [diff] [blame] | 8053 | io_tree = &BTRFS_I(inode)->io_tree; |
| 8054 | failure_tree = &BTRFS_I(inode)->io_failure_tree; |
| 8055 | ASSERT(bio->bi_io_vec->bv_len == btrfs_inode_sectorsize(inode)); |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 8056 | |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8057 | done->uptodate = 1; |
David Sterba | c09abff | 2017-07-13 18:10:07 +0200 | [diff] [blame] | 8058 | ASSERT(!bio_flagged(bio, BIO_CLONED)); |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8059 | bio_for_each_segment_all(bvec, bio, i) |
Josef Bacik | 7870d08 | 2017-05-05 11:57:15 -0400 | [diff] [blame] | 8060 | clean_io_failure(BTRFS_I(inode)->root->fs_info, failure_tree, |
| 8061 | io_tree, done->start, bvec->bv_page, |
| 8062 | btrfs_ino(BTRFS_I(inode)), 0); |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8063 | end: |
| 8064 | complete(&done->done); |
| 8065 | bio_put(bio); |
| 8066 | } |
| 8067 | |
| 8068 | static int __btrfs_correct_data_nocsum(struct inode *inode, |
| 8069 | struct btrfs_io_bio *io_bio) |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8070 | { |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 8071 | struct btrfs_fs_info *fs_info; |
Liu Bo | 17347ce | 2017-05-15 15:33:27 -0700 | [diff] [blame] | 8072 | struct bio_vec bvec; |
| 8073 | struct bvec_iter iter; |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8074 | struct btrfs_retry_complete done; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8075 | u64 start; |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 8076 | unsigned int pgoff; |
| 8077 | u32 sectorsize; |
| 8078 | int nr_sectors; |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 8079 | int ret; |
Liu Bo | 629ebf4 | 2017-05-15 17:20:07 -0700 | [diff] [blame] | 8080 | int err = 0; |
Miao Xie | dc380ae | 2014-09-12 18:43:55 +0800 | [diff] [blame] | 8081 | |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 8082 | fs_info = BTRFS_I(inode)->root->fs_info; |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 8083 | sectorsize = fs_info->sectorsize; |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 8084 | |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 8085 | start = io_bio->logical; |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8086 | done.inode = inode; |
Liu Bo | 17347ce | 2017-05-15 15:33:27 -0700 | [diff] [blame] | 8087 | io_bio->bio.bi_iter = io_bio->iter; |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8088 | |
Liu Bo | 17347ce | 2017-05-15 15:33:27 -0700 | [diff] [blame] | 8089 | bio_for_each_segment(bvec, &io_bio->bio, iter) { |
| 8090 | nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec.bv_len); |
| 8091 | pgoff = bvec.bv_offset; |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 8092 | |
| 8093 | next_block_or_try_again: |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8094 | done.uptodate = 0; |
| 8095 | done.start = start; |
| 8096 | init_completion(&done.done); |
| 8097 | |
Liu Bo | 17347ce | 2017-05-15 15:33:27 -0700 | [diff] [blame] | 8098 | ret = dio_read_error(inode, &io_bio->bio, bvec.bv_page, |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 8099 | pgoff, start, start + sectorsize - 1, |
| 8100 | io_bio->mirror_num, |
| 8101 | btrfs_retry_endio_nocsum, &done); |
Liu Bo | 629ebf4 | 2017-05-15 17:20:07 -0700 | [diff] [blame] | 8102 | if (ret) { |
| 8103 | err = ret; |
| 8104 | goto next; |
| 8105 | } |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8106 | |
| 8107 | wait_for_completion(&done.done); |
| 8108 | |
| 8109 | if (!done.uptodate) { |
| 8110 | /* We might have another mirror, so try again */ |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 8111 | goto next_block_or_try_again; |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8112 | } |
| 8113 | |
Liu Bo | 629ebf4 | 2017-05-15 17:20:07 -0700 | [diff] [blame] | 8114 | next: |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 8115 | start += sectorsize; |
| 8116 | |
Liu Bo | 97bf5a5 | 2017-04-07 13:11:10 -0700 | [diff] [blame] | 8117 | nr_sectors--; |
| 8118 | if (nr_sectors) { |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 8119 | pgoff += sectorsize; |
Liu Bo | 97bf5a5 | 2017-04-07 13:11:10 -0700 | [diff] [blame] | 8120 | ASSERT(pgoff < PAGE_SIZE); |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 8121 | goto next_block_or_try_again; |
| 8122 | } |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8123 | } |
| 8124 | |
Liu Bo | 629ebf4 | 2017-05-15 17:20:07 -0700 | [diff] [blame] | 8125 | return err; |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8126 | } |
| 8127 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 8128 | static void btrfs_retry_endio(struct bio *bio) |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8129 | { |
| 8130 | struct btrfs_retry_complete *done = bio->bi_private; |
| 8131 | struct btrfs_io_bio *io_bio = btrfs_io_bio(bio); |
Josef Bacik | 7870d08 | 2017-05-05 11:57:15 -0400 | [diff] [blame] | 8132 | struct extent_io_tree *io_tree, *failure_tree; |
| 8133 | struct inode *inode = done->inode; |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8134 | struct bio_vec *bvec; |
| 8135 | int uptodate; |
| 8136 | int ret; |
| 8137 | int i; |
| 8138 | |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 8139 | if (bio->bi_status) |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8140 | goto end; |
| 8141 | |
| 8142 | uptodate = 1; |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 8143 | |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 8144 | ASSERT(bio->bi_vcnt == 1); |
Liu Bo | 2e949b0 | 2017-04-05 14:04:19 -0700 | [diff] [blame] | 8145 | ASSERT(bio->bi_io_vec->bv_len == btrfs_inode_sectorsize(done->inode)); |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 8146 | |
Josef Bacik | 7870d08 | 2017-05-05 11:57:15 -0400 | [diff] [blame] | 8147 | io_tree = &BTRFS_I(inode)->io_tree; |
| 8148 | failure_tree = &BTRFS_I(inode)->io_failure_tree; |
| 8149 | |
David Sterba | c09abff | 2017-07-13 18:10:07 +0200 | [diff] [blame] | 8150 | ASSERT(!bio_flagged(bio, BIO_CLONED)); |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8151 | bio_for_each_segment_all(bvec, bio, i) { |
Josef Bacik | 7870d08 | 2017-05-05 11:57:15 -0400 | [diff] [blame] | 8152 | ret = __readpage_endio_check(inode, io_bio, i, bvec->bv_page, |
| 8153 | bvec->bv_offset, done->start, |
| 8154 | bvec->bv_len); |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8155 | if (!ret) |
Josef Bacik | 7870d08 | 2017-05-05 11:57:15 -0400 | [diff] [blame] | 8156 | clean_io_failure(BTRFS_I(inode)->root->fs_info, |
| 8157 | failure_tree, io_tree, done->start, |
| 8158 | bvec->bv_page, |
| 8159 | btrfs_ino(BTRFS_I(inode)), |
| 8160 | bvec->bv_offset); |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8161 | else |
| 8162 | uptodate = 0; |
| 8163 | } |
| 8164 | |
| 8165 | done->uptodate = uptodate; |
| 8166 | end: |
| 8167 | complete(&done->done); |
| 8168 | bio_put(bio); |
| 8169 | } |
| 8170 | |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 8171 | static blk_status_t __btrfs_subio_endio_read(struct inode *inode, |
| 8172 | struct btrfs_io_bio *io_bio, blk_status_t err) |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8173 | { |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 8174 | struct btrfs_fs_info *fs_info; |
Liu Bo | 17347ce | 2017-05-15 15:33:27 -0700 | [diff] [blame] | 8175 | struct bio_vec bvec; |
| 8176 | struct bvec_iter iter; |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8177 | struct btrfs_retry_complete done; |
| 8178 | u64 start; |
| 8179 | u64 offset = 0; |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 8180 | u32 sectorsize; |
| 8181 | int nr_sectors; |
| 8182 | unsigned int pgoff; |
| 8183 | int csum_pos; |
Liu Bo | ef7cdac1 | 2017-04-13 18:11:48 -0700 | [diff] [blame] | 8184 | bool uptodate = (err == 0); |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8185 | int ret; |
| 8186 | |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 8187 | fs_info = BTRFS_I(inode)->root->fs_info; |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 8188 | sectorsize = fs_info->sectorsize; |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 8189 | |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8190 | err = 0; |
| 8191 | start = io_bio->logical; |
| 8192 | done.inode = inode; |
Liu Bo | 17347ce | 2017-05-15 15:33:27 -0700 | [diff] [blame] | 8193 | io_bio->bio.bi_iter = io_bio->iter; |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8194 | |
Liu Bo | 17347ce | 2017-05-15 15:33:27 -0700 | [diff] [blame] | 8195 | bio_for_each_segment(bvec, &io_bio->bio, iter) { |
| 8196 | nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec.bv_len); |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 8197 | |
Liu Bo | 17347ce | 2017-05-15 15:33:27 -0700 | [diff] [blame] | 8198 | pgoff = bvec.bv_offset; |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 8199 | next_block: |
Liu Bo | ef7cdac1 | 2017-04-13 18:11:48 -0700 | [diff] [blame] | 8200 | if (uptodate) { |
| 8201 | csum_pos = BTRFS_BYTES_TO_BLKS(fs_info, offset); |
| 8202 | ret = __readpage_endio_check(inode, io_bio, csum_pos, |
| 8203 | bvec.bv_page, pgoff, start, sectorsize); |
| 8204 | if (likely(!ret)) |
| 8205 | goto next; |
| 8206 | } |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8207 | try_again: |
| 8208 | done.uptodate = 0; |
| 8209 | done.start = start; |
| 8210 | init_completion(&done.done); |
| 8211 | |
Liu Bo | 17347ce | 2017-05-15 15:33:27 -0700 | [diff] [blame] | 8212 | ret = dio_read_error(inode, &io_bio->bio, bvec.bv_page, |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 8213 | pgoff, start, start + sectorsize - 1, |
| 8214 | io_bio->mirror_num, |
| 8215 | btrfs_retry_endio, &done); |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8216 | if (ret) { |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 8217 | err = errno_to_blk_status(ret); |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8218 | goto next; |
| 8219 | } |
| 8220 | |
| 8221 | wait_for_completion(&done.done); |
| 8222 | |
| 8223 | if (!done.uptodate) { |
| 8224 | /* We might have another mirror, so try again */ |
| 8225 | goto try_again; |
| 8226 | } |
| 8227 | next: |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 8228 | offset += sectorsize; |
| 8229 | start += sectorsize; |
| 8230 | |
| 8231 | ASSERT(nr_sectors); |
| 8232 | |
Liu Bo | 97bf5a5 | 2017-04-07 13:11:10 -0700 | [diff] [blame] | 8233 | nr_sectors--; |
| 8234 | if (nr_sectors) { |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 8235 | pgoff += sectorsize; |
Liu Bo | 97bf5a5 | 2017-04-07 13:11:10 -0700 | [diff] [blame] | 8236 | ASSERT(pgoff < PAGE_SIZE); |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 8237 | goto next_block; |
| 8238 | } |
Kent Overstreet | 2c30c71 | 2013-11-07 12:20:26 -0800 | [diff] [blame] | 8239 | } |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 8240 | |
| 8241 | return err; |
| 8242 | } |
| 8243 | |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 8244 | static blk_status_t btrfs_subio_endio_read(struct inode *inode, |
| 8245 | struct btrfs_io_bio *io_bio, blk_status_t err) |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8246 | { |
| 8247 | bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM; |
| 8248 | |
| 8249 | if (skip_csum) { |
| 8250 | if (unlikely(err)) |
| 8251 | return __btrfs_correct_data_nocsum(inode, io_bio); |
| 8252 | else |
| 8253 | return 0; |
| 8254 | } else { |
| 8255 | return __btrfs_subio_endio_read(inode, io_bio, err); |
| 8256 | } |
| 8257 | } |
| 8258 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 8259 | static void btrfs_endio_direct_read(struct bio *bio) |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 8260 | { |
| 8261 | struct btrfs_dio_private *dip = bio->bi_private; |
| 8262 | struct inode *inode = dip->inode; |
| 8263 | struct bio *dio_bio; |
| 8264 | struct btrfs_io_bio *io_bio = btrfs_io_bio(bio); |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 8265 | blk_status_t err = bio->bi_status; |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 8266 | |
Liu Bo | e3d37fa | 2017-05-17 15:42:00 -0600 | [diff] [blame] | 8267 | if (dip->flags & BTRFS_DIO_ORIG_BIO_SUBMITTED) { |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8268 | err = btrfs_subio_endio_read(inode, io_bio, err); |
Liu Bo | e3d37fa | 2017-05-17 15:42:00 -0600 | [diff] [blame] | 8269 | if (!err) |
Linus Torvalds | 8c27cb3 | 2017-07-05 16:41:23 -0700 | [diff] [blame] | 8270 | bio->bi_status = 0; |
Liu Bo | e3d37fa | 2017-05-17 15:42:00 -0600 | [diff] [blame] | 8271 | } |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 8272 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8273 | unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset, |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 8274 | dip->logical_offset + dip->bytes - 1); |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 8275 | dio_bio = dip->dio_bio; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8276 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8277 | kfree(dip); |
Josef Bacik | c0da7aa | 2011-03-22 11:05:07 -0400 | [diff] [blame] | 8278 | |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 8279 | dio_bio->bi_status = bio->bi_status; |
Christoph Hellwig | 4055351 | 2017-06-03 09:37:58 +0200 | [diff] [blame] | 8280 | dio_end_io(dio_bio); |
Miao Xie | 23ea8e5 | 2014-09-12 18:43:54 +0800 | [diff] [blame] | 8281 | |
| 8282 | if (io_bio->end_io) |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 8283 | io_bio->end_io(io_bio, blk_status_to_errno(err)); |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 8284 | bio_put(bio); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8285 | } |
| 8286 | |
Qu Wenruo | 52427260 | 2017-03-08 10:25:52 +0800 | [diff] [blame] | 8287 | static void __endio_write_update_ordered(struct inode *inode, |
| 8288 | const u64 offset, const u64 bytes, |
| 8289 | const bool uptodate) |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8290 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8291 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8292 | struct btrfs_ordered_extent *ordered = NULL; |
Qu Wenruo | 52427260 | 2017-03-08 10:25:52 +0800 | [diff] [blame] | 8293 | struct btrfs_workqueue *wq; |
| 8294 | btrfs_work_func_t func; |
Filipe Manana | 1454377 | 2015-11-24 16:23:54 +0000 | [diff] [blame] | 8295 | u64 ordered_offset = offset; |
| 8296 | u64 ordered_bytes = bytes; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8297 | int ret; |
| 8298 | |
Qu Wenruo | 52427260 | 2017-03-08 10:25:52 +0800 | [diff] [blame] | 8299 | if (btrfs_is_free_space_inode(BTRFS_I(inode))) { |
| 8300 | wq = fs_info->endio_freespace_worker; |
| 8301 | func = btrfs_freespace_write_helper; |
| 8302 | } else { |
| 8303 | wq = fs_info->endio_write_workers; |
| 8304 | func = btrfs_endio_write_helper; |
| 8305 | } |
| 8306 | |
Chris Mason | 163cf09 | 2010-11-28 19:56:33 -0500 | [diff] [blame] | 8307 | again: |
| 8308 | ret = btrfs_dec_test_first_ordered_pending(inode, &ordered, |
| 8309 | &ordered_offset, |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 8310 | ordered_bytes, |
Filipe Manana | 1454377 | 2015-11-24 16:23:54 +0000 | [diff] [blame] | 8311 | uptodate); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8312 | if (!ret) |
Chris Mason | 163cf09 | 2010-11-28 19:56:33 -0500 | [diff] [blame] | 8313 | goto out_test; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8314 | |
Qu Wenruo | 52427260 | 2017-03-08 10:25:52 +0800 | [diff] [blame] | 8315 | btrfs_init_work(&ordered->work, func, finish_ordered_fn, NULL, NULL); |
| 8316 | btrfs_queue_work(wq, &ordered->work); |
Chris Mason | 163cf09 | 2010-11-28 19:56:33 -0500 | [diff] [blame] | 8317 | out_test: |
| 8318 | /* |
| 8319 | * our bio might span multiple ordered extents. If we haven't |
| 8320 | * completed the accounting for the whole dio, go back and try again |
| 8321 | */ |
Filipe Manana | 1454377 | 2015-11-24 16:23:54 +0000 | [diff] [blame] | 8322 | if (ordered_offset < offset + bytes) { |
| 8323 | ordered_bytes = offset + bytes - ordered_offset; |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 8324 | ordered = NULL; |
Chris Mason | 163cf09 | 2010-11-28 19:56:33 -0500 | [diff] [blame] | 8325 | goto again; |
| 8326 | } |
Filipe Manana | 1454377 | 2015-11-24 16:23:54 +0000 | [diff] [blame] | 8327 | } |
| 8328 | |
| 8329 | static void btrfs_endio_direct_write(struct bio *bio) |
| 8330 | { |
| 8331 | struct btrfs_dio_private *dip = bio->bi_private; |
| 8332 | struct bio *dio_bio = dip->dio_bio; |
| 8333 | |
Qu Wenruo | 52427260 | 2017-03-08 10:25:52 +0800 | [diff] [blame] | 8334 | __endio_write_update_ordered(dip->inode, dip->logical_offset, |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 8335 | dip->bytes, !bio->bi_status); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8336 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8337 | kfree(dip); |
Josef Bacik | c0da7aa | 2011-03-22 11:05:07 -0400 | [diff] [blame] | 8338 | |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 8339 | dio_bio->bi_status = bio->bi_status; |
Christoph Hellwig | 4055351 | 2017-06-03 09:37:58 +0200 | [diff] [blame] | 8340 | dio_end_io(dio_bio); |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 8341 | bio_put(bio); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8342 | } |
| 8343 | |
Linus Torvalds | 8c27cb3 | 2017-07-05 16:41:23 -0700 | [diff] [blame] | 8344 | static blk_status_t __btrfs_submit_bio_start_direct_io(void *private_data, |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 8345 | struct bio *bio, int mirror_num, |
| 8346 | unsigned long bio_flags, u64 offset) |
| 8347 | { |
Josef Bacik | c6100a4 | 2017-05-05 11:57:13 -0400 | [diff] [blame] | 8348 | struct inode *inode = private_data; |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 8349 | blk_status_t ret; |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 8350 | ret = btrfs_csum_one_bio(inode, bio, offset, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 8351 | BUG_ON(ret); /* -ENOMEM */ |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 8352 | return 0; |
| 8353 | } |
| 8354 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 8355 | static void btrfs_end_dio_bio(struct bio *bio) |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8356 | { |
| 8357 | struct btrfs_dio_private *dip = bio->bi_private; |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 8358 | blk_status_t err = bio->bi_status; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8359 | |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8360 | if (err) |
| 8361 | btrfs_warn(BTRFS_I(dip->inode)->root->fs_info, |
Mike Christie | 6296b96 | 2016-06-05 14:32:21 -0500 | [diff] [blame] | 8362 | "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] | 8363 | btrfs_ino(BTRFS_I(dip->inode)), bio_op(bio), |
| 8364 | bio->bi_opf, |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8365 | (unsigned long long)bio->bi_iter.bi_sector, |
| 8366 | bio->bi_iter.bi_size, err); |
| 8367 | |
| 8368 | if (dip->subio_endio) |
| 8369 | err = dip->subio_endio(dip->inode, btrfs_io_bio(bio), err); |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 8370 | |
| 8371 | if (err) { |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8372 | dip->errors = 1; |
| 8373 | |
| 8374 | /* |
| 8375 | * before atomic variable goto zero, we must make sure |
| 8376 | * dip->errors is perceived to be set. |
| 8377 | */ |
Peter Zijlstra | 4e857c5 | 2014-03-17 18:06:10 +0100 | [diff] [blame] | 8378 | smp_mb__before_atomic(); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8379 | } |
| 8380 | |
| 8381 | /* if there are more bios still pending for this dio, just exit */ |
| 8382 | if (!atomic_dec_and_test(&dip->pending_bios)) |
| 8383 | goto out; |
| 8384 | |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 8385 | if (dip->errors) { |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8386 | bio_io_error(dip->orig_bio); |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 8387 | } else { |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 8388 | dip->dio_bio->bi_status = 0; |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 8389 | bio_endio(dip->orig_bio); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8390 | } |
| 8391 | out: |
| 8392 | bio_put(bio); |
| 8393 | } |
| 8394 | |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 8395 | 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] | 8396 | struct btrfs_dio_private *dip, |
| 8397 | struct bio *bio, |
| 8398 | u64 file_offset) |
| 8399 | { |
| 8400 | struct btrfs_io_bio *io_bio = btrfs_io_bio(bio); |
| 8401 | 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] | 8402 | blk_status_t ret; |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 8403 | |
| 8404 | /* |
| 8405 | * We load all the csum data we need when we submit |
| 8406 | * the first bio to reduce the csum tree search and |
| 8407 | * contention. |
| 8408 | */ |
| 8409 | if (dip->logical_offset == file_offset) { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 8410 | ret = btrfs_lookup_bio_sums_dio(inode, dip->orig_bio, |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 8411 | file_offset); |
| 8412 | if (ret) |
| 8413 | return ret; |
| 8414 | } |
| 8415 | |
| 8416 | if (bio == dip->orig_bio) |
| 8417 | return 0; |
| 8418 | |
| 8419 | file_offset -= dip->logical_offset; |
| 8420 | file_offset >>= inode->i_sb->s_blocksize_bits; |
| 8421 | io_bio->csum = (u8 *)(((u32 *)orig_io_bio->csum) + file_offset); |
| 8422 | |
| 8423 | return 0; |
| 8424 | } |
| 8425 | |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8426 | static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode, |
Mike Christie | 81a75f67 | 2016-06-05 14:31:54 -0500 | [diff] [blame] | 8427 | u64 file_offset, int skip_sum, |
Josef Bacik | c329861 | 2012-08-03 16:49:19 -0400 | [diff] [blame] | 8428 | int async_submit) |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8429 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8430 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 8431 | struct btrfs_dio_private *dip = bio->bi_private; |
Mike Christie | 37226b2 | 2016-06-05 14:31:52 -0500 | [diff] [blame] | 8432 | bool write = bio_op(bio) == REQ_OP_WRITE; |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 8433 | blk_status_t ret; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8434 | |
Josef Bacik | b812ce2 | 2012-11-16 13:56:32 -0500 | [diff] [blame] | 8435 | if (async_submit) |
| 8436 | async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers); |
| 8437 | |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8438 | bio_get(bio); |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 8439 | |
| 8440 | if (!write) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8441 | 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] | 8442 | if (ret) |
| 8443 | goto err; |
| 8444 | } |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8445 | |
Josef Bacik | 1ae3993 | 2011-04-06 14:41:34 -0400 | [diff] [blame] | 8446 | if (skip_sum) |
| 8447 | goto map; |
| 8448 | |
| 8449 | if (write && async_submit) { |
Josef Bacik | c6100a4 | 2017-05-05 11:57:13 -0400 | [diff] [blame] | 8450 | ret = btrfs_wq_submit_bio(fs_info, bio, 0, 0, |
| 8451 | file_offset, inode, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8452 | __btrfs_submit_bio_start_direct_io, |
| 8453 | __btrfs_submit_bio_done); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8454 | goto err; |
Josef Bacik | 1ae3993 | 2011-04-06 14:41:34 -0400 | [diff] [blame] | 8455 | } else if (write) { |
| 8456 | /* |
| 8457 | * If we aren't doing async submit, calculate the csum of the |
| 8458 | * bio now. |
| 8459 | */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 8460 | ret = btrfs_csum_one_bio(inode, bio, file_offset, 1); |
Josef Bacik | 1ae3993 | 2011-04-06 14:41:34 -0400 | [diff] [blame] | 8461 | if (ret) |
| 8462 | goto err; |
Miao Xie | 23ea8e5 | 2014-09-12 18:43:54 +0800 | [diff] [blame] | 8463 | } else { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 8464 | ret = btrfs_lookup_and_bind_dio_csum(inode, dip, bio, |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 8465 | file_offset); |
Tsutomu Itoh | c2db107 | 2011-03-01 06:48:31 +0000 | [diff] [blame] | 8466 | if (ret) |
| 8467 | goto err; |
| 8468 | } |
Josef Bacik | 1ae3993 | 2011-04-06 14:41:34 -0400 | [diff] [blame] | 8469 | map: |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 8470 | ret = btrfs_map_bio(fs_info, bio, 0, async_submit); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8471 | err: |
| 8472 | bio_put(bio); |
| 8473 | return ret; |
| 8474 | } |
| 8475 | |
Mike Christie | 81a75f67 | 2016-06-05 14:31:54 -0500 | [diff] [blame] | 8476 | static int btrfs_submit_direct_hook(struct btrfs_dio_private *dip, |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8477 | int skip_sum) |
| 8478 | { |
| 8479 | struct inode *inode = dip->inode; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8480 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8481 | struct bio *bio; |
| 8482 | struct bio *orig_bio = dip->orig_bio; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 8483 | u64 start_sector = orig_bio->bi_iter.bi_sector; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8484 | u64 file_offset = dip->logical_offset; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8485 | u64 map_length; |
Josef Bacik | 1ae3993 | 2011-04-06 14:41:34 -0400 | [diff] [blame] | 8486 | int async_submit = 0; |
Liu Bo | 725130b | 2017-05-16 09:51:39 -0700 | [diff] [blame] | 8487 | u64 submit_len; |
| 8488 | int clone_offset = 0; |
| 8489 | int clone_len; |
Chandan Rajendra | 5f4dc8f | 2016-01-21 15:56:00 +0530 | [diff] [blame] | 8490 | int ret; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8491 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 8492 | map_length = orig_bio->bi_iter.bi_size; |
Liu Bo | 725130b | 2017-05-16 09:51:39 -0700 | [diff] [blame] | 8493 | submit_len = map_length; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8494 | ret = btrfs_map_block(fs_info, btrfs_op(orig_bio), start_sector << 9, |
| 8495 | &map_length, NULL, 0); |
Miao Xie | 7a5c3c9 | 2014-06-17 18:58:59 +0800 | [diff] [blame] | 8496 | if (ret) |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8497 | return -EIO; |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 8498 | |
Liu Bo | 725130b | 2017-05-16 09:51:39 -0700 | [diff] [blame] | 8499 | if (map_length >= submit_len) { |
Josef Bacik | 02f57c7 | 2011-04-06 14:25:44 -0400 | [diff] [blame] | 8500 | bio = orig_bio; |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 8501 | dip->flags |= BTRFS_DIO_ORIG_BIO_SUBMITTED; |
Josef Bacik | 02f57c7 | 2011-04-06 14:25:44 -0400 | [diff] [blame] | 8502 | goto submit; |
| 8503 | } |
| 8504 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 8505 | /* async crcs make it difficult to collect full stripe writes. */ |
Jeff Mahoney | 1b86826 | 2017-05-17 11:38:35 -0400 | [diff] [blame] | 8506 | if (btrfs_data_alloc_profile(fs_info) & BTRFS_BLOCK_GROUP_RAID56_MASK) |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 8507 | async_submit = 0; |
| 8508 | else |
| 8509 | async_submit = 1; |
| 8510 | |
Liu Bo | 725130b | 2017-05-16 09:51:39 -0700 | [diff] [blame] | 8511 | /* bio split */ |
| 8512 | ASSERT(map_length <= INT_MAX); |
Josef Bacik | 02f57c7 | 2011-04-06 14:25:44 -0400 | [diff] [blame] | 8513 | atomic_inc(&dip->pending_bios); |
Arnd Bergmann | 3c91ee6 | 2017-05-18 15:33:29 +0200 | [diff] [blame] | 8514 | do { |
Liu Bo | 725130b | 2017-05-16 09:51:39 -0700 | [diff] [blame] | 8515 | clone_len = min_t(int, submit_len, map_length); |
Josef Bacik | 02f57c7 | 2011-04-06 14:25:44 -0400 | [diff] [blame] | 8516 | |
Liu Bo | 725130b | 2017-05-16 09:51:39 -0700 | [diff] [blame] | 8517 | /* |
| 8518 | * This will never fail as it's passing GPF_NOFS and |
| 8519 | * the allocation is backed by btrfs_bioset. |
| 8520 | */ |
Liu Bo | e477094 | 2017-05-16 10:57:14 -0700 | [diff] [blame] | 8521 | bio = btrfs_bio_clone_partial(orig_bio, clone_offset, |
Liu Bo | 725130b | 2017-05-16 09:51:39 -0700 | [diff] [blame] | 8522 | clone_len); |
| 8523 | bio->bi_private = dip; |
| 8524 | bio->bi_end_io = btrfs_end_dio_bio; |
| 8525 | btrfs_io_bio(bio)->logical = file_offset; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8526 | |
Liu Bo | 725130b | 2017-05-16 09:51:39 -0700 | [diff] [blame] | 8527 | ASSERT(submit_len >= clone_len); |
| 8528 | submit_len -= clone_len; |
| 8529 | if (submit_len == 0) |
| 8530 | break; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8531 | |
Liu Bo | 725130b | 2017-05-16 09:51:39 -0700 | [diff] [blame] | 8532 | /* |
| 8533 | * Increase the count before we submit the bio so we know |
| 8534 | * the end IO handler won't happen before we increase the |
| 8535 | * count. Otherwise, the dip might get freed before we're |
| 8536 | * done setting it up. |
| 8537 | */ |
| 8538 | atomic_inc(&dip->pending_bios); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8539 | |
Liu Bo | 725130b | 2017-05-16 09:51:39 -0700 | [diff] [blame] | 8540 | ret = __btrfs_submit_dio_bio(bio, inode, file_offset, skip_sum, |
| 8541 | async_submit); |
| 8542 | if (ret) { |
| 8543 | bio_put(bio); |
| 8544 | atomic_dec(&dip->pending_bios); |
| 8545 | goto out_err; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8546 | } |
Liu Bo | 725130b | 2017-05-16 09:51:39 -0700 | [diff] [blame] | 8547 | |
| 8548 | clone_offset += clone_len; |
| 8549 | start_sector += clone_len >> 9; |
| 8550 | file_offset += clone_len; |
| 8551 | |
| 8552 | map_length = submit_len; |
| 8553 | ret = btrfs_map_block(fs_info, btrfs_op(orig_bio), |
| 8554 | start_sector << 9, &map_length, NULL, 0); |
| 8555 | if (ret) |
| 8556 | goto out_err; |
Arnd Bergmann | 3c91ee6 | 2017-05-18 15:33:29 +0200 | [diff] [blame] | 8557 | } while (submit_len > 0); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8558 | |
Josef Bacik | 02f57c7 | 2011-04-06 14:25:44 -0400 | [diff] [blame] | 8559 | submit: |
Mike Christie | 81a75f67 | 2016-06-05 14:31:54 -0500 | [diff] [blame] | 8560 | ret = __btrfs_submit_dio_bio(bio, inode, file_offset, skip_sum, |
Josef Bacik | c329861 | 2012-08-03 16:49:19 -0400 | [diff] [blame] | 8561 | async_submit); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8562 | if (!ret) |
| 8563 | return 0; |
| 8564 | |
| 8565 | bio_put(bio); |
| 8566 | out_err: |
| 8567 | dip->errors = 1; |
| 8568 | /* |
| 8569 | * before atomic variable goto zero, we must |
| 8570 | * make sure dip->errors is perceived to be set. |
| 8571 | */ |
Peter Zijlstra | 4e857c5 | 2014-03-17 18:06:10 +0100 | [diff] [blame] | 8572 | smp_mb__before_atomic(); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8573 | if (atomic_dec_and_test(&dip->pending_bios)) |
| 8574 | bio_io_error(dip->orig_bio); |
| 8575 | |
| 8576 | /* bio_end_io() will handle error, so we needn't return it */ |
| 8577 | return 0; |
| 8578 | } |
| 8579 | |
Mike Christie | 8a4c1e4 | 2016-06-05 14:31:50 -0500 | [diff] [blame] | 8580 | static void btrfs_submit_direct(struct bio *dio_bio, struct inode *inode, |
| 8581 | loff_t file_offset) |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8582 | { |
Filipe Manana | 61de718 | 2015-07-01 12:13:10 +0100 | [diff] [blame] | 8583 | struct btrfs_dio_private *dip = NULL; |
Liu Bo | 3892ac9 | 2017-04-17 15:00:28 -0700 | [diff] [blame] | 8584 | struct bio *bio = NULL; |
| 8585 | struct btrfs_io_bio *io_bio; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8586 | int skip_sum; |
Mike Christie | 8a4c1e4 | 2016-06-05 14:31:50 -0500 | [diff] [blame] | 8587 | bool write = (bio_op(dio_bio) == REQ_OP_WRITE); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8588 | int ret = 0; |
| 8589 | |
| 8590 | skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM; |
| 8591 | |
David Sterba | 8b6c1d5 | 2017-06-02 17:48:13 +0200 | [diff] [blame] | 8592 | bio = btrfs_bio_clone(dio_bio); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8593 | |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 8594 | dip = kzalloc(sizeof(*dip), GFP_NOFS); |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 8595 | if (!dip) { |
| 8596 | ret = -ENOMEM; |
Filipe Manana | 61de718 | 2015-07-01 12:13:10 +0100 | [diff] [blame] | 8597 | goto free_ordered; |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 8598 | } |
| 8599 | |
| 8600 | dip->private = dio_bio->bi_private; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8601 | dip->inode = inode; |
| 8602 | dip->logical_offset = file_offset; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 8603 | dip->bytes = dio_bio->bi_iter.bi_size; |
| 8604 | dip->disk_bytenr = (u64)dio_bio->bi_iter.bi_sector << 9; |
Liu Bo | 3892ac9 | 2017-04-17 15:00:28 -0700 | [diff] [blame] | 8605 | bio->bi_private = dip; |
| 8606 | dip->orig_bio = bio; |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 8607 | dip->dio_bio = dio_bio; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8608 | atomic_set(&dip->pending_bios, 0); |
Liu Bo | 3892ac9 | 2017-04-17 15:00:28 -0700 | [diff] [blame] | 8609 | io_bio = btrfs_io_bio(bio); |
| 8610 | io_bio->logical = file_offset; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8611 | |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 8612 | if (write) { |
Liu Bo | 3892ac9 | 2017-04-17 15:00:28 -0700 | [diff] [blame] | 8613 | bio->bi_end_io = btrfs_endio_direct_write; |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 8614 | } else { |
Liu Bo | 3892ac9 | 2017-04-17 15:00:28 -0700 | [diff] [blame] | 8615 | bio->bi_end_io = btrfs_endio_direct_read; |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 8616 | dip->subio_endio = btrfs_subio_endio_read; |
| 8617 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8618 | |
Filipe Manana | f28a492 | 2015-12-08 19:23:20 +0000 | [diff] [blame] | 8619 | /* |
| 8620 | * Reset the range for unsubmitted ordered extents (to a 0 length range) |
| 8621 | * even if we fail to submit a bio, because in such case we do the |
| 8622 | * corresponding error handling below and it must not be done a second |
| 8623 | * time by btrfs_direct_IO(). |
| 8624 | */ |
| 8625 | if (write) { |
| 8626 | struct btrfs_dio_data *dio_data = current->journal_info; |
| 8627 | |
| 8628 | dio_data->unsubmitted_oe_range_end = dip->logical_offset + |
| 8629 | dip->bytes; |
| 8630 | dio_data->unsubmitted_oe_range_start = |
| 8631 | dio_data->unsubmitted_oe_range_end; |
| 8632 | } |
| 8633 | |
Mike Christie | 81a75f67 | 2016-06-05 14:31:54 -0500 | [diff] [blame] | 8634 | ret = btrfs_submit_direct_hook(dip, skip_sum); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8635 | if (!ret) |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 8636 | return; |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 8637 | |
Liu Bo | 3892ac9 | 2017-04-17 15:00:28 -0700 | [diff] [blame] | 8638 | if (io_bio->end_io) |
| 8639 | io_bio->end_io(io_bio, ret); |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 8640 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8641 | free_ordered: |
| 8642 | /* |
Filipe Manana | 61de718 | 2015-07-01 12:13:10 +0100 | [diff] [blame] | 8643 | * If we arrived here it means either we failed to submit the dip |
| 8644 | * or we either failed to clone the dio_bio or failed to allocate the |
| 8645 | * dip. If we cloned the dio_bio and allocated the dip, we can just |
| 8646 | * call bio_endio against our io_bio so that we get proper resource |
| 8647 | * cleanup if we fail to submit the dip, otherwise, we must do the |
| 8648 | * same as btrfs_endio_direct_[write|read] because we can't call these |
| 8649 | * callbacks - they require an allocated dip and a clone of dio_bio. |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8650 | */ |
Liu Bo | 3892ac9 | 2017-04-17 15:00:28 -0700 | [diff] [blame] | 8651 | if (bio && dip) { |
Guoqing Jiang | 054ec2f | 2017-06-02 16:08:50 +0800 | [diff] [blame] | 8652 | bio_io_error(bio); |
Filipe Manana | 61de718 | 2015-07-01 12:13:10 +0100 | [diff] [blame] | 8653 | /* |
Liu Bo | 3892ac9 | 2017-04-17 15:00:28 -0700 | [diff] [blame] | 8654 | * 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] | 8655 | * and all the cleanup and final put for dio_bio (through |
| 8656 | * dio_end_io()). |
| 8657 | */ |
| 8658 | dip = NULL; |
Liu Bo | 3892ac9 | 2017-04-17 15:00:28 -0700 | [diff] [blame] | 8659 | bio = NULL; |
Filipe Manana | 61de718 | 2015-07-01 12:13:10 +0100 | [diff] [blame] | 8660 | } else { |
Filipe Manana | 1454377 | 2015-11-24 16:23:54 +0000 | [diff] [blame] | 8661 | if (write) |
Qu Wenruo | 52427260 | 2017-03-08 10:25:52 +0800 | [diff] [blame] | 8662 | __endio_write_update_ordered(inode, |
Filipe Manana | 1454377 | 2015-11-24 16:23:54 +0000 | [diff] [blame] | 8663 | file_offset, |
| 8664 | dio_bio->bi_iter.bi_size, |
Qu Wenruo | 52427260 | 2017-03-08 10:25:52 +0800 | [diff] [blame] | 8665 | false); |
Filipe Manana | 1454377 | 2015-11-24 16:23:54 +0000 | [diff] [blame] | 8666 | else |
Filipe Manana | 61de718 | 2015-07-01 12:13:10 +0100 | [diff] [blame] | 8667 | unlock_extent(&BTRFS_I(inode)->io_tree, file_offset, |
| 8668 | file_offset + dio_bio->bi_iter.bi_size - 1); |
Filipe Manana | 1454377 | 2015-11-24 16:23:54 +0000 | [diff] [blame] | 8669 | |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 8670 | dio_bio->bi_status = BLK_STS_IOERR; |
Filipe Manana | 61de718 | 2015-07-01 12:13:10 +0100 | [diff] [blame] | 8671 | /* |
| 8672 | * Releases and cleans up our dio_bio, no need to bio_put() |
| 8673 | * nor bio_endio()/bio_io_error() against dio_bio. |
| 8674 | */ |
Christoph Hellwig | 4055351 | 2017-06-03 09:37:58 +0200 | [diff] [blame] | 8675 | dio_end_io(dio_bio); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8676 | } |
Liu Bo | 3892ac9 | 2017-04-17 15:00:28 -0700 | [diff] [blame] | 8677 | if (bio) |
| 8678 | bio_put(bio); |
Filipe Manana | 61de718 | 2015-07-01 12:13:10 +0100 | [diff] [blame] | 8679 | kfree(dip); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8680 | } |
| 8681 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 8682 | static ssize_t check_direct_IO(struct btrfs_fs_info *fs_info, |
| 8683 | struct kiocb *iocb, |
| 8684 | const struct iov_iter *iter, loff_t offset) |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 8685 | { |
| 8686 | int seg; |
Josef Bacik | a1b75f7 | 2011-04-08 15:51:18 +0000 | [diff] [blame] | 8687 | int i; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8688 | unsigned int blocksize_mask = fs_info->sectorsize - 1; |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 8689 | ssize_t retval = -EINVAL; |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 8690 | |
| 8691 | if (offset & blocksize_mask) |
| 8692 | goto out; |
| 8693 | |
Al Viro | 28060d5 | 2014-03-22 05:15:17 -0400 | [diff] [blame] | 8694 | if (iov_iter_alignment(iter) & blocksize_mask) |
| 8695 | goto out; |
Josef Bacik | a1b75f7 | 2011-04-08 15:51:18 +0000 | [diff] [blame] | 8696 | |
Al Viro | 28060d5 | 2014-03-22 05:15:17 -0400 | [diff] [blame] | 8697 | /* If this is a write we don't need to check anymore */ |
Al Viro | cd27e45 | 2016-10-10 13:39:05 -0400 | [diff] [blame] | 8698 | if (iov_iter_rw(iter) != READ || !iter_is_iovec(iter)) |
Al Viro | 28060d5 | 2014-03-22 05:15:17 -0400 | [diff] [blame] | 8699 | return 0; |
| 8700 | /* |
| 8701 | * Check to make sure we don't have duplicate iov_base's in this |
| 8702 | * iovec, if so return EINVAL, otherwise we'll get csum errors |
| 8703 | * when reading back. |
| 8704 | */ |
| 8705 | for (seg = 0; seg < iter->nr_segs; seg++) { |
| 8706 | for (i = seg + 1; i < iter->nr_segs; i++) { |
| 8707 | if (iter->iov[seg].iov_base == iter->iov[i].iov_base) |
Josef Bacik | a1b75f7 | 2011-04-08 15:51:18 +0000 | [diff] [blame] | 8708 | goto out; |
| 8709 | } |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 8710 | } |
| 8711 | retval = 0; |
| 8712 | out: |
| 8713 | return retval; |
| 8714 | } |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 8715 | |
Christoph Hellwig | c8b8e32 | 2016-04-07 08:51:58 -0700 | [diff] [blame] | 8716 | 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] | 8717 | { |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8718 | struct file *file = iocb->ki_filp; |
| 8719 | struct inode *inode = file->f_mapping->host; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8720 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
chandan | 50745b0 | 2015-08-28 21:10:13 +0530 | [diff] [blame] | 8721 | struct btrfs_dio_data dio_data = { 0 }; |
Qu Wenruo | 364ecf3 | 2017-02-27 15:10:38 +0800 | [diff] [blame] | 8722 | struct extent_changeset *data_reserved = NULL; |
Christoph Hellwig | c8b8e32 | 2016-04-07 08:51:58 -0700 | [diff] [blame] | 8723 | loff_t offset = iocb->ki_pos; |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 8724 | size_t count = 0; |
Miao Xie | 2e60a51 | 2013-02-08 07:01:08 +0000 | [diff] [blame] | 8725 | int flags = 0; |
Miao Xie | 38851cc | 2013-02-08 07:04:11 +0000 | [diff] [blame] | 8726 | bool wakeup = true; |
| 8727 | bool relock = false; |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 8728 | ssize_t ret; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8729 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 8730 | if (check_direct_IO(fs_info, iocb, iter, offset)) |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 8731 | return 0; |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 8732 | |
Jens Axboe | fe0f07d | 2015-04-15 17:05:48 -0600 | [diff] [blame] | 8733 | inode_dio_begin(inode); |
Peter Zijlstra | 4e857c5 | 2014-03-17 18:06:10 +0100 | [diff] [blame] | 8734 | smp_mb__after_atomic(); |
Miao Xie | 38851cc | 2013-02-08 07:04:11 +0000 | [diff] [blame] | 8735 | |
Josef Bacik | 0e267c4 | 2013-07-02 10:38:02 -0400 | [diff] [blame] | 8736 | /* |
Miao Xie | 41bd9ca | 2014-03-06 13:54:57 +0800 | [diff] [blame] | 8737 | * The generic stuff only does filemap_write_and_wait_range, which |
| 8738 | * isn't enough if we've written compressed pages to this area, so |
| 8739 | * we need to flush the dirty pages again to make absolutely sure |
| 8740 | * that any outstanding dirty pages are on disk. |
Josef Bacik | 0e267c4 | 2013-07-02 10:38:02 -0400 | [diff] [blame] | 8741 | */ |
Al Viro | a6cbcd4 | 2014-03-04 22:38:00 -0500 | [diff] [blame] | 8742 | count = iov_iter_count(iter); |
Miao Xie | 41bd9ca | 2014-03-06 13:54:57 +0800 | [diff] [blame] | 8743 | if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, |
| 8744 | &BTRFS_I(inode)->runtime_flags)) |
Wang Shilong | 9a025a0 | 2014-07-17 11:44:13 +0800 | [diff] [blame] | 8745 | filemap_fdatawrite_range(inode->i_mapping, offset, |
| 8746 | offset + count - 1); |
Josef Bacik | 0e267c4 | 2013-07-02 10:38:02 -0400 | [diff] [blame] | 8747 | |
Omar Sandoval | 6f67376 | 2015-03-16 04:33:52 -0700 | [diff] [blame] | 8748 | if (iov_iter_rw(iter) == WRITE) { |
Miao Xie | 38851cc | 2013-02-08 07:04:11 +0000 | [diff] [blame] | 8749 | /* |
| 8750 | * If the write DIO is beyond the EOF, we need update |
| 8751 | * the isize, but it is protected by i_mutex. So we can |
| 8752 | * not unlock the i_mutex at this case. |
| 8753 | */ |
| 8754 | if (offset + count <= inode->i_size) { |
Liu Bo | 4aaedfb | 2016-12-14 22:36:05 -0800 | [diff] [blame] | 8755 | dio_data.overwrite = 1; |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 8756 | inode_unlock(inode); |
Miao Xie | 38851cc | 2013-02-08 07:04:11 +0000 | [diff] [blame] | 8757 | relock = true; |
Goldwyn Rodrigues | edf064e | 2017-06-20 07:05:49 -0500 | [diff] [blame] | 8758 | } else if (iocb->ki_flags & IOCB_NOWAIT) { |
| 8759 | ret = -EAGAIN; |
| 8760 | goto out; |
Miao Xie | 38851cc | 2013-02-08 07:04:11 +0000 | [diff] [blame] | 8761 | } |
Qu Wenruo | 364ecf3 | 2017-02-27 15:10:38 +0800 | [diff] [blame] | 8762 | ret = btrfs_delalloc_reserve_space(inode, &data_reserved, |
| 8763 | offset, count); |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 8764 | if (ret) |
Miao Xie | 38851cc | 2013-02-08 07:04:11 +0000 | [diff] [blame] | 8765 | goto out; |
David Sterba | 823bb20 | 2017-01-04 11:09:51 +0100 | [diff] [blame] | 8766 | dio_data.outstanding_extents = count_max_extents(count); |
Josef Bacik | e1cbbfa | 2015-03-17 10:52:28 -0400 | [diff] [blame] | 8767 | |
| 8768 | /* |
| 8769 | * We need to know how many extents we reserved so that we can |
| 8770 | * do the accounting properly if we go over the number we |
| 8771 | * originally calculated. Abuse current->journal_info for this. |
| 8772 | */ |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 8773 | dio_data.reserve = round_up(count, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8774 | fs_info->sectorsize); |
Filipe Manana | f28a492 | 2015-12-08 19:23:20 +0000 | [diff] [blame] | 8775 | dio_data.unsubmitted_oe_range_start = (u64)offset; |
| 8776 | dio_data.unsubmitted_oe_range_end = (u64)offset; |
chandan | 50745b0 | 2015-08-28 21:10:13 +0530 | [diff] [blame] | 8777 | current->journal_info = &dio_data; |
Chandan Rajendra | 97dcdea | 2016-12-23 15:00:18 +0530 | [diff] [blame] | 8778 | down_read(&BTRFS_I(inode)->dio_sem); |
David Sterba | ee39b43 | 2014-09-30 01:33:33 +0200 | [diff] [blame] | 8779 | } else if (test_bit(BTRFS_INODE_READDIO_NEED_LOCK, |
| 8780 | &BTRFS_I(inode)->runtime_flags)) { |
Jens Axboe | fe0f07d | 2015-04-15 17:05:48 -0600 | [diff] [blame] | 8781 | inode_dio_end(inode); |
Miao Xie | 38851cc | 2013-02-08 07:04:11 +0000 | [diff] [blame] | 8782 | flags = DIO_LOCKING | DIO_SKIP_HOLES; |
| 8783 | wakeup = false; |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 8784 | } |
| 8785 | |
Omar Sandoval | 17f8c84 | 2015-03-16 04:33:50 -0700 | [diff] [blame] | 8786 | ret = __blockdev_direct_IO(iocb, inode, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8787 | fs_info->fs_devices->latest_bdev, |
Christoph Hellwig | c8b8e32 | 2016-04-07 08:51:58 -0700 | [diff] [blame] | 8788 | iter, btrfs_get_blocks_direct, NULL, |
Omar Sandoval | 17f8c84 | 2015-03-16 04:33:50 -0700 | [diff] [blame] | 8789 | btrfs_submit_direct, flags); |
Omar Sandoval | 6f67376 | 2015-03-16 04:33:52 -0700 | [diff] [blame] | 8790 | if (iov_iter_rw(iter) == WRITE) { |
Chandan Rajendra | 97dcdea | 2016-12-23 15:00:18 +0530 | [diff] [blame] | 8791 | up_read(&BTRFS_I(inode)->dio_sem); |
Josef Bacik | e1cbbfa | 2015-03-17 10:52:28 -0400 | [diff] [blame] | 8792 | current->journal_info = NULL; |
Liu Bo | ddba1bf | 2015-06-17 16:59:58 +0800 | [diff] [blame] | 8793 | if (ret < 0 && ret != -EIOCBQUEUED) { |
chandan | 50745b0 | 2015-08-28 21:10:13 +0530 | [diff] [blame] | 8794 | if (dio_data.reserve) |
Qu Wenruo | bc42bda | 2017-02-27 15:10:39 +0800 | [diff] [blame] | 8795 | btrfs_delalloc_release_space(inode, data_reserved, |
| 8796 | offset, dio_data.reserve); |
Filipe Manana | f28a492 | 2015-12-08 19:23:20 +0000 | [diff] [blame] | 8797 | /* |
| 8798 | * On error we might have left some ordered extents |
| 8799 | * without submitting corresponding bios for them, so |
| 8800 | * cleanup them up to avoid other tasks getting them |
| 8801 | * and waiting for them to complete forever. |
| 8802 | */ |
| 8803 | if (dio_data.unsubmitted_oe_range_start < |
| 8804 | dio_data.unsubmitted_oe_range_end) |
Qu Wenruo | 52427260 | 2017-03-08 10:25:52 +0800 | [diff] [blame] | 8805 | __endio_write_update_ordered(inode, |
Filipe Manana | f28a492 | 2015-12-08 19:23:20 +0000 | [diff] [blame] | 8806 | dio_data.unsubmitted_oe_range_start, |
| 8807 | dio_data.unsubmitted_oe_range_end - |
| 8808 | dio_data.unsubmitted_oe_range_start, |
Qu Wenruo | 52427260 | 2017-03-08 10:25:52 +0800 | [diff] [blame] | 8809 | false); |
Liu Bo | ddba1bf | 2015-06-17 16:59:58 +0800 | [diff] [blame] | 8810 | } else if (ret >= 0 && (size_t)ret < count) |
Qu Wenruo | bc42bda | 2017-02-27 15:10:39 +0800 | [diff] [blame] | 8811 | btrfs_delalloc_release_space(inode, data_reserved, |
| 8812 | offset, count - (size_t)ret); |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 8813 | } |
Miao Xie | 38851cc | 2013-02-08 07:04:11 +0000 | [diff] [blame] | 8814 | out: |
Miao Xie | 2e60a51 | 2013-02-08 07:01:08 +0000 | [diff] [blame] | 8815 | if (wakeup) |
Jens Axboe | fe0f07d | 2015-04-15 17:05:48 -0600 | [diff] [blame] | 8816 | inode_dio_end(inode); |
Miao Xie | 38851cc | 2013-02-08 07:04:11 +0000 | [diff] [blame] | 8817 | if (relock) |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 8818 | inode_lock(inode); |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 8819 | |
Qu Wenruo | 364ecf3 | 2017-02-27 15:10:38 +0800 | [diff] [blame] | 8820 | extent_changeset_free(data_reserved); |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 8821 | return ret; |
Chris Mason | 1643298 | 2008-04-10 10:23:21 -0400 | [diff] [blame] | 8822 | } |
| 8823 | |
Tsutomu Itoh | 05dadc0 | 2012-11-29 05:08:26 +0000 | [diff] [blame] | 8824 | #define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC) |
| 8825 | |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 8826 | static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, |
| 8827 | __u64 start, __u64 len) |
| 8828 | { |
Tsutomu Itoh | 05dadc0 | 2012-11-29 05:08:26 +0000 | [diff] [blame] | 8829 | int ret; |
| 8830 | |
| 8831 | ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS); |
| 8832 | if (ret) |
| 8833 | return ret; |
| 8834 | |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 8835 | return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap); |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 8836 | } |
| 8837 | |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8838 | int btrfs_readpage(struct file *file, struct page *page) |
| 8839 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 8840 | struct extent_io_tree *tree; |
| 8841 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
Jan Schmidt | 8ddc7d9 | 2011-06-13 20:02:58 +0200 | [diff] [blame] | 8842 | return extent_read_full_page(tree, page, btrfs_get_extent, 0); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8843 | } |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 8844 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8845 | static int btrfs_writepage(struct page *page, struct writeback_control *wbc) |
| 8846 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 8847 | struct extent_io_tree *tree; |
Josef Bacik | be7bd73 | 2015-10-22 15:05:09 -0400 | [diff] [blame] | 8848 | struct inode *inode = page->mapping->host; |
| 8849 | int ret; |
Chris Mason | b888db2b | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8850 | |
| 8851 | if (current->flags & PF_MEMALLOC) { |
| 8852 | redirty_page_for_writepage(wbc, page); |
| 8853 | unlock_page(page); |
| 8854 | return 0; |
| 8855 | } |
Josef Bacik | be7bd73 | 2015-10-22 15:05:09 -0400 | [diff] [blame] | 8856 | |
| 8857 | /* |
| 8858 | * If we are under memory pressure we will call this directly from the |
| 8859 | * VM, we need to make sure we have the inode referenced for the ordered |
| 8860 | * extent. If not just return like we didn't do anything. |
| 8861 | */ |
| 8862 | if (!igrab(inode)) { |
| 8863 | redirty_page_for_writepage(wbc, page); |
| 8864 | return AOP_WRITEPAGE_ACTIVATE; |
| 8865 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 8866 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
Josef Bacik | be7bd73 | 2015-10-22 15:05:09 -0400 | [diff] [blame] | 8867 | ret = extent_write_full_page(tree, page, btrfs_get_extent, wbc); |
| 8868 | btrfs_add_delayed_iput(inode); |
| 8869 | return ret; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8870 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8871 | |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 8872 | static int btrfs_writepages(struct address_space *mapping, |
| 8873 | struct writeback_control *wbc) |
Chris Mason | b293f02e | 2007-11-01 19:45:34 -0400 | [diff] [blame] | 8874 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 8875 | struct extent_io_tree *tree; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 8876 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 8877 | tree = &BTRFS_I(mapping->host)->io_tree; |
Chris Mason | b293f02e | 2007-11-01 19:45:34 -0400 | [diff] [blame] | 8878 | return extent_writepages(tree, mapping, btrfs_get_extent, wbc); |
| 8879 | } |
| 8880 | |
Chris Mason | 3ab2fb5 | 2007-11-08 10:59:22 -0500 | [diff] [blame] | 8881 | static int |
| 8882 | btrfs_readpages(struct file *file, struct address_space *mapping, |
| 8883 | struct list_head *pages, unsigned nr_pages) |
| 8884 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 8885 | struct extent_io_tree *tree; |
| 8886 | tree = &BTRFS_I(mapping->host)->io_tree; |
Chris Mason | 3ab2fb5 | 2007-11-08 10:59:22 -0500 | [diff] [blame] | 8887 | return extent_readpages(tree, mapping, pages, nr_pages, |
| 8888 | btrfs_get_extent); |
| 8889 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8890 | static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8891 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 8892 | struct extent_io_tree *tree; |
| 8893 | struct extent_map_tree *map; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8894 | int ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8895 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 8896 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
| 8897 | map = &BTRFS_I(page->mapping->host)->extent_tree; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 8898 | ret = try_release_extent_mapping(map, tree, page, gfp_flags); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8899 | if (ret == 1) { |
| 8900 | ClearPagePrivate(page); |
| 8901 | set_page_private(page, 0); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 8902 | put_page(page); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8903 | } |
| 8904 | return ret; |
| 8905 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8906 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8907 | static int btrfs_releasepage(struct page *page, gfp_t gfp_flags) |
| 8908 | { |
Chris Mason | 98509cf | 2008-09-11 15:51:43 -0400 | [diff] [blame] | 8909 | if (PageWriteback(page) || PageDirty(page)) |
| 8910 | return 0; |
Michal Hocko | 3ba7ab2 | 2017-01-09 15:39:02 +0100 | [diff] [blame] | 8911 | return __btrfs_releasepage(page, gfp_flags); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8912 | } |
| 8913 | |
Lukas Czerner | d47992f | 2013-05-21 23:17:23 -0400 | [diff] [blame] | 8914 | static void btrfs_invalidatepage(struct page *page, unsigned int offset, |
| 8915 | unsigned int length) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8916 | { |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 8917 | struct inode *inode = page->mapping->host; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 8918 | struct extent_io_tree *tree; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8919 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 8920 | struct extent_state *cached_state = NULL; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8921 | u64 page_start = page_offset(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 8922 | u64 page_end = page_start + PAGE_SIZE - 1; |
Chandan Rajendra | dbfdb6d | 2016-01-21 15:55:58 +0530 | [diff] [blame] | 8923 | u64 start; |
| 8924 | u64 end; |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 8925 | int inode_evicting = inode->i_state & I_FREEING; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8926 | |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 8927 | /* |
| 8928 | * we have the page locked, so new writeback can't start, |
| 8929 | * and the dirty bit won't be cleared while we are here. |
| 8930 | * |
| 8931 | * Wait for IO on this page so that we can safely clear |
| 8932 | * the PagePrivate2 bit and do ordered accounting |
| 8933 | */ |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8934 | wait_on_page_writeback(page); |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 8935 | |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 8936 | tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8937 | if (offset) { |
| 8938 | btrfs_releasepage(page, GFP_NOFS); |
| 8939 | return; |
| 8940 | } |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 8941 | |
| 8942 | if (!inode_evicting) |
David Sterba | ff13db4 | 2015-12-03 14:30:40 +0100 | [diff] [blame] | 8943 | lock_extent_bits(tree, page_start, page_end, &cached_state); |
Chandan Rajendra | dbfdb6d | 2016-01-21 15:55:58 +0530 | [diff] [blame] | 8944 | again: |
| 8945 | start = page_start; |
Nikolay Borisov | a776c6f | 2017-02-20 13:50:49 +0200 | [diff] [blame] | 8946 | ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), start, |
Chandan Rajendra | dbfdb6d | 2016-01-21 15:55:58 +0530 | [diff] [blame] | 8947 | page_end - start + 1); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8948 | if (ordered) { |
Chandan Rajendra | dbfdb6d | 2016-01-21 15:55:58 +0530 | [diff] [blame] | 8949 | end = min(page_end, ordered->file_offset + ordered->len - 1); |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 8950 | /* |
| 8951 | * IO on this page will never be started, so we need |
| 8952 | * to account for any ordered extents now |
| 8953 | */ |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 8954 | if (!inode_evicting) |
Chandan Rajendra | dbfdb6d | 2016-01-21 15:55:58 +0530 | [diff] [blame] | 8955 | clear_extent_bit(tree, start, end, |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 8956 | EXTENT_DIRTY | EXTENT_DELALLOC | |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 8957 | EXTENT_DELALLOC_NEW | |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 8958 | EXTENT_LOCKED | EXTENT_DO_ACCOUNTING | |
| 8959 | EXTENT_DEFRAG, 1, 0, &cached_state, |
| 8960 | GFP_NOFS); |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 8961 | /* |
| 8962 | * whoever cleared the private bit is responsible |
| 8963 | * for the finish_ordered_io |
| 8964 | */ |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 8965 | if (TestClearPagePrivate2(page)) { |
| 8966 | struct btrfs_ordered_inode_tree *tree; |
| 8967 | u64 new_len; |
| 8968 | |
| 8969 | tree = &BTRFS_I(inode)->ordered_tree; |
| 8970 | |
| 8971 | spin_lock_irq(&tree->lock); |
| 8972 | set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags); |
Chandan Rajendra | dbfdb6d | 2016-01-21 15:55:58 +0530 | [diff] [blame] | 8973 | new_len = start - ordered->file_offset; |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 8974 | if (new_len < ordered->truncated_len) |
| 8975 | ordered->truncated_len = new_len; |
| 8976 | spin_unlock_irq(&tree->lock); |
| 8977 | |
| 8978 | if (btrfs_dec_test_ordered_pending(inode, &ordered, |
Chandan Rajendra | dbfdb6d | 2016-01-21 15:55:58 +0530 | [diff] [blame] | 8979 | start, |
| 8980 | end - start + 1, 1)) |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 8981 | btrfs_finish_ordered_io(ordered); |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 8982 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8983 | btrfs_put_ordered_extent(ordered); |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 8984 | if (!inode_evicting) { |
| 8985 | cached_state = NULL; |
Chandan Rajendra | dbfdb6d | 2016-01-21 15:55:58 +0530 | [diff] [blame] | 8986 | lock_extent_bits(tree, start, end, |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 8987 | &cached_state); |
| 8988 | } |
Chandan Rajendra | dbfdb6d | 2016-01-21 15:55:58 +0530 | [diff] [blame] | 8989 | |
| 8990 | start = end + 1; |
| 8991 | if (start < page_end) |
| 8992 | goto again; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8993 | } |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 8994 | |
Qu Wenruo | b9d0b38 | 2015-09-29 10:35:16 +0800 | [diff] [blame] | 8995 | /* |
| 8996 | * Qgroup reserved space handler |
| 8997 | * Page here will be either |
| 8998 | * 1) Already written to disk |
| 8999 | * In this case, its reserved space is released from data rsv map |
| 9000 | * and will be freed by delayed_ref handler finally. |
| 9001 | * So even we call qgroup_free_data(), it won't decrease reserved |
| 9002 | * space. |
| 9003 | * 2) Not written to disk |
Goldwyn Rodrigues | 0b34c26 | 2016-09-30 10:40:52 -0500 | [diff] [blame] | 9004 | * This means the reserved space should be freed here. However, |
| 9005 | * if a truncate invalidates the page (by clearing PageDirty) |
| 9006 | * and the page is accounted for while allocating extent |
| 9007 | * in btrfs_check_data_free_space() we let delayed_ref to |
| 9008 | * free the entire extent. |
Qu Wenruo | b9d0b38 | 2015-09-29 10:35:16 +0800 | [diff] [blame] | 9009 | */ |
Goldwyn Rodrigues | 0b34c26 | 2016-09-30 10:40:52 -0500 | [diff] [blame] | 9010 | if (PageDirty(page)) |
Qu Wenruo | bc42bda | 2017-02-27 15:10:39 +0800 | [diff] [blame] | 9011 | btrfs_qgroup_free_data(inode, NULL, page_start, PAGE_SIZE); |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 9012 | if (!inode_evicting) { |
| 9013 | clear_extent_bit(tree, page_start, page_end, |
| 9014 | EXTENT_LOCKED | EXTENT_DIRTY | |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 9015 | EXTENT_DELALLOC | EXTENT_DELALLOC_NEW | |
| 9016 | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1, |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 9017 | &cached_state, GFP_NOFS); |
| 9018 | |
| 9019 | __btrfs_releasepage(page, GFP_NOFS); |
| 9020 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 9021 | |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 9022 | ClearPageChecked(page); |
Chris Mason | 9ad6b7b | 2008-04-18 16:11:30 -0400 | [diff] [blame] | 9023 | if (PagePrivate(page)) { |
Chris Mason | 9ad6b7b | 2008-04-18 16:11:30 -0400 | [diff] [blame] | 9024 | ClearPagePrivate(page); |
| 9025 | set_page_private(page, 0); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 9026 | put_page(page); |
Chris Mason | 9ad6b7b | 2008-04-18 16:11:30 -0400 | [diff] [blame] | 9027 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9028 | } |
| 9029 | |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 9030 | /* |
| 9031 | * btrfs_page_mkwrite() is not allowed to change the file size as it gets |
| 9032 | * called from a page fault handler when a page is first dirtied. Hence we must |
| 9033 | * be careful to check for EOF conditions here. We set the page up correctly |
| 9034 | * for a written page which means we get ENOSPC checking when writing into |
| 9035 | * holes and correct delalloc and unwritten extent mapping on filesystems that |
| 9036 | * support these features. |
| 9037 | * |
| 9038 | * We are not allowed to take the i_mutex here so we have to play games to |
| 9039 | * protect against truncate races as the page could now be beyond EOF. Because |
| 9040 | * vmtruncate() writes the inode size before removing pages, once we have the |
| 9041 | * page lock we can determine safely if the page is beyond EOF. If it is not |
| 9042 | * beyond EOF, then the page is guaranteed safe against truncation until we |
| 9043 | * unlock the page. |
| 9044 | */ |
Dave Jiang | 11bac80 | 2017-02-24 14:56:41 -0800 | [diff] [blame] | 9045 | int btrfs_page_mkwrite(struct vm_fault *vmf) |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 9046 | { |
Nick Piggin | c2ec175 | 2009-03-31 15:23:21 -0700 | [diff] [blame] | 9047 | struct page *page = vmf->page; |
Dave Jiang | 11bac80 | 2017-02-24 14:56:41 -0800 | [diff] [blame] | 9048 | struct inode *inode = file_inode(vmf->vma->vm_file); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9049 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 9050 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
| 9051 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 9052 | struct extent_state *cached_state = NULL; |
Qu Wenruo | 364ecf3 | 2017-02-27 15:10:38 +0800 | [diff] [blame] | 9053 | struct extent_changeset *data_reserved = NULL; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 9054 | char *kaddr; |
| 9055 | unsigned long zero_start; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 9056 | loff_t size; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 9057 | int ret; |
Chris Mason | 9998eb7 | 2012-01-25 13:47:40 -0500 | [diff] [blame] | 9058 | int reserved = 0; |
Chandan Rajendra | d0b7da88 | 2016-01-21 15:55:57 +0530 | [diff] [blame] | 9059 | u64 reserved_space; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 9060 | u64 page_start; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 9061 | u64 page_end; |
Chandan Rajendra | d0b7da88 | 2016-01-21 15:55:57 +0530 | [diff] [blame] | 9062 | u64 end; |
| 9063 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 9064 | reserved_space = PAGE_SIZE; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 9065 | |
Jan Kara | b2b5ef5 | 2012-06-12 16:20:45 +0200 | [diff] [blame] | 9066 | sb_start_pagefault(inode->i_sb); |
Qu Wenruo | df48063 | 2015-09-08 17:25:54 +0800 | [diff] [blame] | 9067 | page_start = page_offset(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 9068 | page_end = page_start + PAGE_SIZE - 1; |
Chandan Rajendra | d0b7da88 | 2016-01-21 15:55:57 +0530 | [diff] [blame] | 9069 | end = page_end; |
Qu Wenruo | df48063 | 2015-09-08 17:25:54 +0800 | [diff] [blame] | 9070 | |
Chandan Rajendra | d0b7da88 | 2016-01-21 15:55:57 +0530 | [diff] [blame] | 9071 | /* |
| 9072 | * Reserving delalloc space after obtaining the page lock can lead to |
| 9073 | * deadlock. For example, if a dirty page is locked by this function |
| 9074 | * and the call to btrfs_delalloc_reserve_space() ends up triggering |
| 9075 | * dirty page write out, then the btrfs_writepage() function could |
| 9076 | * end up waiting indefinitely to get a lock on the page currently |
| 9077 | * being processed by btrfs_page_mkwrite() function. |
| 9078 | */ |
Qu Wenruo | 364ecf3 | 2017-02-27 15:10:38 +0800 | [diff] [blame] | 9079 | ret = btrfs_delalloc_reserve_space(inode, &data_reserved, page_start, |
Chandan Rajendra | d0b7da88 | 2016-01-21 15:55:57 +0530 | [diff] [blame] | 9080 | reserved_space); |
Chris Mason | 9998eb7 | 2012-01-25 13:47:40 -0500 | [diff] [blame] | 9081 | if (!ret) { |
Dave Jiang | 11bac80 | 2017-02-24 14:56:41 -0800 | [diff] [blame] | 9082 | ret = file_update_time(vmf->vma->vm_file); |
Chris Mason | 9998eb7 | 2012-01-25 13:47:40 -0500 | [diff] [blame] | 9083 | reserved = 1; |
| 9084 | } |
Nick Piggin | 56a76f8 | 2009-03-31 15:23:23 -0700 | [diff] [blame] | 9085 | if (ret) { |
| 9086 | if (ret == -ENOMEM) |
| 9087 | ret = VM_FAULT_OOM; |
| 9088 | else /* -ENOSPC, -EIO, etc */ |
| 9089 | ret = VM_FAULT_SIGBUS; |
Chris Mason | 9998eb7 | 2012-01-25 13:47:40 -0500 | [diff] [blame] | 9090 | if (reserved) |
| 9091 | goto out; |
| 9092 | goto out_noreserve; |
Nick Piggin | 56a76f8 | 2009-03-31 15:23:23 -0700 | [diff] [blame] | 9093 | } |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 9094 | |
Nick Piggin | 56a76f8 | 2009-03-31 15:23:23 -0700 | [diff] [blame] | 9095 | ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */ |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 9096 | again: |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 9097 | lock_page(page); |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 9098 | size = i_size_read(inode); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 9099 | |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 9100 | if ((page->mapping != inode->i_mapping) || |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 9101 | (page_start >= size)) { |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 9102 | /* page got truncated out from underneath us */ |
| 9103 | goto out_unlock; |
| 9104 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 9105 | wait_on_page_writeback(page); |
| 9106 | |
David Sterba | ff13db4 | 2015-12-03 14:30:40 +0100 | [diff] [blame] | 9107 | lock_extent_bits(io_tree, page_start, page_end, &cached_state); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 9108 | set_page_extent_mapped(page); |
| 9109 | |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 9110 | /* |
| 9111 | * we can't set the delalloc bits if there are pending ordered |
| 9112 | * extents. Drop our locks and wait for them to finish |
| 9113 | */ |
Nikolay Borisov | a776c6f | 2017-02-20 13:50:49 +0200 | [diff] [blame] | 9114 | ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start, |
| 9115 | PAGE_SIZE); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 9116 | if (ordered) { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 9117 | unlock_extent_cached(io_tree, page_start, page_end, |
| 9118 | &cached_state, GFP_NOFS); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 9119 | unlock_page(page); |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 9120 | btrfs_start_ordered_extent(inode, ordered, 1); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 9121 | btrfs_put_ordered_extent(ordered); |
| 9122 | goto again; |
| 9123 | } |
| 9124 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 9125 | if (page->index == ((size - 1) >> PAGE_SHIFT)) { |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 9126 | reserved_space = round_up(size - page_start, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9127 | fs_info->sectorsize); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 9128 | if (reserved_space < PAGE_SIZE) { |
Chandan Rajendra | d0b7da88 | 2016-01-21 15:55:57 +0530 | [diff] [blame] | 9129 | end = page_start + reserved_space - 1; |
| 9130 | spin_lock(&BTRFS_I(inode)->lock); |
| 9131 | BTRFS_I(inode)->outstanding_extents++; |
| 9132 | spin_unlock(&BTRFS_I(inode)->lock); |
Qu Wenruo | bc42bda | 2017-02-27 15:10:39 +0800 | [diff] [blame] | 9133 | btrfs_delalloc_release_space(inode, data_reserved, |
| 9134 | page_start, PAGE_SIZE - reserved_space); |
Chandan Rajendra | d0b7da88 | 2016-01-21 15:55:57 +0530 | [diff] [blame] | 9135 | } |
| 9136 | } |
| 9137 | |
Josef Bacik | fbf1908 | 2009-10-01 17:10:23 -0400 | [diff] [blame] | 9138 | /* |
Liu Bo | 5416034 | 2016-12-13 12:15:19 -0800 | [diff] [blame] | 9139 | * page_mkwrite gets called when the page is firstly dirtied after it's |
| 9140 | * faulted in, but write(2) could also dirty a page and set delalloc |
| 9141 | * bits, thus in this case for space account reason, we still need to |
| 9142 | * clear any delalloc bits within this page range since we have to |
| 9143 | * reserve data&meta space before lock_page() (see above comments). |
Josef Bacik | fbf1908 | 2009-10-01 17:10:23 -0400 | [diff] [blame] | 9144 | */ |
Chandan Rajendra | d0b7da88 | 2016-01-21 15:55:57 +0530 | [diff] [blame] | 9145 | clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, end, |
Liu Bo | 9e8a4a8 | 2012-09-05 19:10:51 -0600 | [diff] [blame] | 9146 | EXTENT_DIRTY | EXTENT_DELALLOC | |
| 9147 | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 9148 | 0, 0, &cached_state, GFP_NOFS); |
Josef Bacik | fbf1908 | 2009-10-01 17:10:23 -0400 | [diff] [blame] | 9149 | |
Chandan Rajendra | d0b7da88 | 2016-01-21 15:55:57 +0530 | [diff] [blame] | 9150 | ret = btrfs_set_extent_delalloc(inode, page_start, end, |
Qu Wenruo | ba8b04c | 2016-07-19 16:50:36 +0800 | [diff] [blame] | 9151 | &cached_state, 0); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 9152 | if (ret) { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 9153 | unlock_extent_cached(io_tree, page_start, page_end, |
| 9154 | &cached_state, GFP_NOFS); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 9155 | ret = VM_FAULT_SIGBUS; |
| 9156 | goto out_unlock; |
| 9157 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 9158 | ret = 0; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 9159 | |
| 9160 | /* page is wholly or partially inside EOF */ |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 9161 | if (page_start + PAGE_SIZE > size) |
| 9162 | zero_start = size & ~PAGE_MASK; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 9163 | else |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 9164 | zero_start = PAGE_SIZE; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 9165 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 9166 | if (zero_start != PAGE_SIZE) { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 9167 | kaddr = kmap(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 9168 | memset(kaddr + zero_start, 0, PAGE_SIZE - zero_start); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 9169 | flush_dcache_page(page); |
| 9170 | kunmap(page); |
| 9171 | } |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 9172 | ClearPageChecked(page); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 9173 | set_page_dirty(page); |
Chris Mason | 50a9b21 | 2009-09-11 12:33:12 -0400 | [diff] [blame] | 9174 | SetPageUptodate(page); |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 9175 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9176 | BTRFS_I(inode)->last_trans = fs_info->generation; |
Chris Mason | 257c62e | 2009-10-13 13:21:08 -0400 | [diff] [blame] | 9177 | BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid; |
Liu Bo | 46d8bc3 | 2012-08-29 01:07:55 -0600 | [diff] [blame] | 9178 | 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] | 9179 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 9180 | unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS); |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 9181 | |
| 9182 | out_unlock: |
Jan Kara | b2b5ef5 | 2012-06-12 16:20:45 +0200 | [diff] [blame] | 9183 | if (!ret) { |
| 9184 | sb_end_pagefault(inode->i_sb); |
Qu Wenruo | 364ecf3 | 2017-02-27 15:10:38 +0800 | [diff] [blame] | 9185 | extent_changeset_free(data_reserved); |
Chris Mason | 50a9b21 | 2009-09-11 12:33:12 -0400 | [diff] [blame] | 9186 | return VM_FAULT_LOCKED; |
Jan Kara | b2b5ef5 | 2012-06-12 16:20:45 +0200 | [diff] [blame] | 9187 | } |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 9188 | unlock_page(page); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 9189 | out: |
Qu Wenruo | bc42bda | 2017-02-27 15:10:39 +0800 | [diff] [blame] | 9190 | btrfs_delalloc_release_space(inode, data_reserved, page_start, |
| 9191 | reserved_space); |
Chris Mason | 9998eb7 | 2012-01-25 13:47:40 -0500 | [diff] [blame] | 9192 | out_noreserve: |
Jan Kara | b2b5ef5 | 2012-06-12 16:20:45 +0200 | [diff] [blame] | 9193 | sb_end_pagefault(inode->i_sb); |
Qu Wenruo | 364ecf3 | 2017-02-27 15:10:38 +0800 | [diff] [blame] | 9194 | extent_changeset_free(data_reserved); |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 9195 | return ret; |
| 9196 | } |
| 9197 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 9198 | static int btrfs_truncate(struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9199 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9200 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9201 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 9202 | struct btrfs_block_rsv *rsv; |
Josef Bacik | a71754f | 2013-06-17 17:14:39 -0400 | [diff] [blame] | 9203 | int ret = 0; |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 9204 | int err = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9205 | struct btrfs_trans_handle *trans; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9206 | u64 mask = fs_info->sectorsize - 1; |
| 9207 | u64 min_size = btrfs_calc_trunc_metadata_size(fs_info, 1); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9208 | |
Josef Bacik | 0ef8b72 | 2013-10-25 16:13:35 -0400 | [diff] [blame] | 9209 | ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask), |
| 9210 | (u64)-1); |
| 9211 | if (ret) |
| 9212 | return ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9213 | |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 9214 | /* |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 9215 | * Yes ladies and gentlemen, this is indeed ugly. The fact is we have |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 9216 | * 3 things going on here |
| 9217 | * |
| 9218 | * 1) We need to reserve space for our orphan item and the space to |
| 9219 | * delete our orphan item. Lord knows we don't want to have a dangling |
| 9220 | * orphan item because we didn't reserve space to remove it. |
| 9221 | * |
| 9222 | * 2) We need to reserve space to update our inode. |
| 9223 | * |
| 9224 | * 3) We need to have something to cache all the space that is going to |
| 9225 | * be free'd up by the truncate operation, but also have some slack |
| 9226 | * space reserved in case it uses space during the truncate (thank you |
| 9227 | * very much snapshotting). |
| 9228 | * |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 9229 | * And we need these to all be separate. The fact is we can use a lot of |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 9230 | * 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] | 9231 | * we will use, so we need the truncate reservation to be separate so it |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 9232 | * doesn't end up using space reserved for updating the inode or |
| 9233 | * removing the orphan item. We also need to be able to stop the |
| 9234 | * transaction and start a new one, which means we need to be able to |
| 9235 | * update the inode several times, and we have no idea of knowing how |
| 9236 | * many times that will be, so we can't just reserve 1 item for the |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 9237 | * entirety of the operation, so that has to be done separately as well. |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 9238 | * Then there is the orphan item, which does indeed need to be held on |
| 9239 | * to for the whole operation, and we need nobody to touch this reserved |
| 9240 | * space except the orphan code. |
| 9241 | * |
| 9242 | * So that leaves us with |
| 9243 | * |
| 9244 | * 1) root->orphan_block_rsv - for the orphan deletion. |
| 9245 | * 2) rsv - for the truncate reservation, which we will steal from the |
| 9246 | * transaction reservation. |
| 9247 | * 3) fs_info->trans_block_rsv - this will have 1 items worth left for |
| 9248 | * updating the inode. |
| 9249 | */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 9250 | rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP); |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 9251 | if (!rsv) |
| 9252 | return -ENOMEM; |
Josef Bacik | 4a33854 | 2011-08-29 11:01:31 -0400 | [diff] [blame] | 9253 | rsv->size = min_size; |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 9254 | rsv->failfast = 1; |
Josef Bacik | f0cd846 | 2011-03-04 14:37:08 -0500 | [diff] [blame] | 9255 | |
Josef Bacik | 907cbce | 2011-08-08 13:46:15 -0400 | [diff] [blame] | 9256 | /* |
Josef Bacik | 0712718 | 2011-08-19 10:29:59 -0400 | [diff] [blame] | 9257 | * 1 for the truncate slack space |
Josef Bacik | 907cbce | 2011-08-08 13:46:15 -0400 | [diff] [blame] | 9258 | * 1 for updating the inode. |
| 9259 | */ |
Josef Bacik | f3fe820 | 2013-01-07 17:03:21 -0500 | [diff] [blame] | 9260 | trans = btrfs_start_transaction(root, 2); |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 9261 | if (IS_ERR(trans)) { |
| 9262 | err = PTR_ERR(trans); |
| 9263 | goto out; |
| 9264 | } |
Josef Bacik | f0cd846 | 2011-03-04 14:37:08 -0500 | [diff] [blame] | 9265 | |
Josef Bacik | 907cbce | 2011-08-08 13:46:15 -0400 | [diff] [blame] | 9266 | /* Migrate the slack space for the truncate to our reserve */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9267 | ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, rsv, |
Josef Bacik | 25d609f | 2016-03-25 13:25:48 -0400 | [diff] [blame] | 9268 | min_size, 0); |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 9269 | BUG_ON(ret); |
Josef Bacik | f0cd846 | 2011-03-04 14:37:08 -0500 | [diff] [blame] | 9270 | |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 9271 | /* |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 9272 | * So if we truncate and then write and fsync we normally would just |
| 9273 | * write the extents that changed, which is a problem if we need to |
| 9274 | * first truncate that entire inode. So set this flag so we write out |
| 9275 | * all of the extents in the inode to the sync log so we're completely |
| 9276 | * safe. |
| 9277 | */ |
| 9278 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags); |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 9279 | trans->block_rsv = rsv; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 9280 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 9281 | while (1) { |
| 9282 | ret = btrfs_truncate_inode_items(trans, root, inode, |
| 9283 | inode->i_size, |
| 9284 | BTRFS_EXTENT_DATA_KEY); |
Chris Mason | 28ed134 | 2014-12-17 09:41:04 -0800 | [diff] [blame] | 9285 | if (ret != -ENOSPC && ret != -EAGAIN) { |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 9286 | err = ret; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 9287 | break; |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 9288 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9289 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9290 | trans->block_rsv = &fs_info->trans_block_rsv; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 9291 | ret = btrfs_update_inode(trans, root, inode); |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 9292 | if (ret) { |
| 9293 | err = ret; |
| 9294 | break; |
| 9295 | } |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 9296 | |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 9297 | btrfs_end_transaction(trans); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 9298 | btrfs_btree_balance_dirty(fs_info); |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 9299 | |
| 9300 | trans = btrfs_start_transaction(root, 2); |
| 9301 | if (IS_ERR(trans)) { |
| 9302 | ret = err = PTR_ERR(trans); |
| 9303 | trans = NULL; |
| 9304 | break; |
| 9305 | } |
| 9306 | |
Wang Xiaoguang | 47b5d64 | 2016-09-07 20:17:38 +0800 | [diff] [blame] | 9307 | btrfs_block_rsv_release(fs_info, rsv, -1); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9308 | ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, |
Josef Bacik | 25d609f | 2016-03-25 13:25:48 -0400 | [diff] [blame] | 9309 | rsv, min_size, 0); |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 9310 | BUG_ON(ret); /* shouldn't happen */ |
| 9311 | trans->block_rsv = rsv; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 9312 | } |
| 9313 | |
| 9314 | if (ret == 0 && inode->i_nlink > 0) { |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 9315 | trans->block_rsv = root->orphan_block_rsv; |
Nikolay Borisov | 3d6ae7b | 2017-02-20 13:50:58 +0200 | [diff] [blame] | 9316 | ret = btrfs_orphan_del(trans, BTRFS_I(inode)); |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 9317 | if (ret) |
| 9318 | err = ret; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 9319 | } |
| 9320 | |
Chris Mason | 917c16b | 2011-11-08 14:49:59 -0500 | [diff] [blame] | 9321 | if (trans) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9322 | trans->block_rsv = &fs_info->trans_block_rsv; |
Chris Mason | 917c16b | 2011-11-08 14:49:59 -0500 | [diff] [blame] | 9323 | ret = btrfs_update_inode(trans, root, inode); |
| 9324 | if (ret && !err) |
| 9325 | err = ret; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 9326 | |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 9327 | ret = btrfs_end_transaction(trans); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 9328 | btrfs_btree_balance_dirty(fs_info); |
Chris Mason | 917c16b | 2011-11-08 14:49:59 -0500 | [diff] [blame] | 9329 | } |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 9330 | out: |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 9331 | btrfs_free_block_rsv(fs_info, rsv); |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 9332 | |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 9333 | if (ret && !err) |
| 9334 | err = ret; |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 9335 | |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 9336 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9337 | } |
| 9338 | |
Sven Wegener | 3b96362 | 2008-06-09 21:57:42 -0400 | [diff] [blame] | 9339 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 9340 | * create a new subvolume directory/inode (helper for the ioctl). |
| 9341 | */ |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 9342 | int btrfs_create_subvol_root(struct btrfs_trans_handle *trans, |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 9343 | struct btrfs_root *new_root, |
| 9344 | struct btrfs_root *parent_root, |
| 9345 | u64 new_dirid) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9346 | { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9347 | struct inode *inode; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 9348 | int err; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 9349 | u64 index = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9350 | |
Florian Albrechtskirchinger | 12fc9d0 | 2012-02-10 22:15:54 +0100 | [diff] [blame] | 9351 | inode = btrfs_new_inode(trans, new_root, NULL, "..", 2, |
| 9352 | new_dirid, new_dirid, |
| 9353 | S_IFDIR | (~current_umask() & S_IRWXUGO), |
| 9354 | &index); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 9355 | if (IS_ERR(inode)) |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 9356 | return PTR_ERR(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9357 | inode->i_op = &btrfs_dir_inode_operations; |
| 9358 | inode->i_fop = &btrfs_dir_file_operations; |
| 9359 | |
Miklos Szeredi | bfe8684 | 2011-10-28 14:13:29 +0200 | [diff] [blame] | 9360 | set_nlink(inode, 1); |
Nikolay Borisov | 6ef06d2 | 2017-02-20 13:50:34 +0200 | [diff] [blame] | 9361 | btrfs_i_size_write(BTRFS_I(inode), 0); |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 9362 | unlock_new_inode(inode); |
Sven Wegener | 3b96362 | 2008-06-09 21:57:42 -0400 | [diff] [blame] | 9363 | |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 9364 | err = btrfs_subvol_inherit_props(trans, new_root, parent_root); |
| 9365 | if (err) |
| 9366 | btrfs_err(new_root->fs_info, |
David Sterba | 351fd35 | 2014-05-15 16:48:20 +0200 | [diff] [blame] | 9367 | "error inheriting subvolume %llu properties: %d", |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 9368 | new_root->root_key.objectid, err); |
| 9369 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 9370 | err = btrfs_update_inode(trans, new_root, inode); |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 9371 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 9372 | iput(inode); |
Mark Fasheh | ce59897 | 2011-07-26 11:32:23 -0700 | [diff] [blame] | 9373 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9374 | } |
| 9375 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9376 | struct inode *btrfs_alloc_inode(struct super_block *sb) |
| 9377 | { |
| 9378 | struct btrfs_inode *ei; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9379 | struct inode *inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9380 | |
| 9381 | ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS); |
| 9382 | if (!ei) |
| 9383 | return NULL; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9384 | |
| 9385 | ei->root = NULL; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9386 | ei->generation = 0; |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 9387 | ei->last_trans = 0; |
Chris Mason | 257c62e | 2009-10-13 13:21:08 -0400 | [diff] [blame] | 9388 | ei->last_sub_trans = 0; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 9389 | ei->logged_trans = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9390 | ei->delalloc_bytes = 0; |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 9391 | ei->new_delalloc_bytes = 0; |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 9392 | ei->defrag_bytes = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9393 | ei->disk_i_size = 0; |
| 9394 | ei->flags = 0; |
Josef Bacik | 7709cde | 2011-08-04 10:25:02 -0400 | [diff] [blame] | 9395 | ei->csum_bytes = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9396 | ei->index_cnt = (u64)-1; |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 9397 | ei->dir_index = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9398 | ei->last_unlink_trans = 0; |
Liu Bo | 46d8bc3 | 2012-08-29 01:07:55 -0600 | [diff] [blame] | 9399 | ei->last_log_commit = 0; |
David Sterba | 8089fe6 | 2015-11-19 14:15:51 +0100 | [diff] [blame] | 9400 | ei->delayed_iput_count = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9401 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 9402 | spin_lock_init(&ei->lock); |
| 9403 | ei->outstanding_extents = 0; |
| 9404 | ei->reserved_extents = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9405 | |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 9406 | ei->runtime_flags = 0; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 9407 | ei->force_compress = BTRFS_COMPRESS_NONE; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9408 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 9409 | ei->delayed_node = NULL; |
| 9410 | |
chandan r | 9cc97d6 | 2012-07-04 12:48:07 +0530 | [diff] [blame] | 9411 | ei->i_otime.tv_sec = 0; |
| 9412 | ei->i_otime.tv_nsec = 0; |
| 9413 | |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9414 | inode = &ei->vfs_inode; |
David Sterba | a8067e0 | 2011-04-21 00:34:43 +0200 | [diff] [blame] | 9415 | extent_map_tree_init(&ei->extent_tree); |
Josef Bacik | c6100a4 | 2017-05-05 11:57:13 -0400 | [diff] [blame] | 9416 | extent_io_tree_init(&ei->io_tree, inode); |
| 9417 | extent_io_tree_init(&ei->io_failure_tree, inode); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 9418 | ei->io_tree.track_uptodate = 1; |
| 9419 | ei->io_failure_tree.track_uptodate = 1; |
Josef Bacik | b812ce2 | 2012-11-16 13:56:32 -0500 | [diff] [blame] | 9420 | atomic_set(&ei->sync_writers, 0); |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9421 | mutex_init(&ei->log_mutex); |
Josef Bacik | f248679 | 2012-01-13 12:09:22 -0500 | [diff] [blame] | 9422 | mutex_init(&ei->delalloc_mutex); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 9423 | btrfs_ordered_inode_tree_init(&ei->ordered_tree); |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9424 | INIT_LIST_HEAD(&ei->delalloc_inodes); |
David Sterba | 8089fe6 | 2015-11-19 14:15:51 +0100 | [diff] [blame] | 9425 | INIT_LIST_HEAD(&ei->delayed_iput); |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9426 | RB_CLEAR_NODE(&ei->rb_node); |
Filipe Manana | 5f9a8a5 | 2016-05-12 13:53:36 +0100 | [diff] [blame] | 9427 | init_rwsem(&ei->dio_sem); |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9428 | |
| 9429 | return inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9430 | } |
| 9431 | |
Josef Bacik | aaedb55 | 2013-10-11 14:44:09 -0400 | [diff] [blame] | 9432 | #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS |
| 9433 | void btrfs_test_destroy_inode(struct inode *inode) |
| 9434 | { |
Nikolay Borisov | dcdbc05 | 2017-02-20 13:50:45 +0200 | [diff] [blame] | 9435 | btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0); |
Josef Bacik | aaedb55 | 2013-10-11 14:44:09 -0400 | [diff] [blame] | 9436 | kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode)); |
| 9437 | } |
| 9438 | #endif |
| 9439 | |
Nick Piggin | fa0d7e3d | 2011-01-07 17:49:49 +1100 | [diff] [blame] | 9440 | static void btrfs_i_callback(struct rcu_head *head) |
| 9441 | { |
| 9442 | struct inode *inode = container_of(head, struct inode, i_rcu); |
Nick Piggin | fa0d7e3d | 2011-01-07 17:49:49 +1100 | [diff] [blame] | 9443 | kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode)); |
| 9444 | } |
| 9445 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9446 | void btrfs_destroy_inode(struct inode *inode) |
| 9447 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9448 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 9449 | struct btrfs_ordered_extent *ordered; |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 9450 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 9451 | |
Al Viro | b3d9b7a | 2012-06-09 13:51:19 -0400 | [diff] [blame] | 9452 | WARN_ON(!hlist_empty(&inode->i_dentry)); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9453 | WARN_ON(inode->i_data.nrpages); |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 9454 | WARN_ON(BTRFS_I(inode)->outstanding_extents); |
| 9455 | WARN_ON(BTRFS_I(inode)->reserved_extents); |
Josef Bacik | 7709cde | 2011-08-04 10:25:02 -0400 | [diff] [blame] | 9456 | WARN_ON(BTRFS_I(inode)->delalloc_bytes); |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 9457 | WARN_ON(BTRFS_I(inode)->new_delalloc_bytes); |
Josef Bacik | 7709cde | 2011-08-04 10:25:02 -0400 | [diff] [blame] | 9458 | WARN_ON(BTRFS_I(inode)->csum_bytes); |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 9459 | WARN_ON(BTRFS_I(inode)->defrag_bytes); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9460 | |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 9461 | /* |
Josef Bacik | a6dbd42 | 2009-11-11 15:53:34 -0500 | [diff] [blame] | 9462 | * This can happen where we create an inode, but somebody else also |
| 9463 | * created the same inode and we need to destroy the one we already |
| 9464 | * created. |
| 9465 | */ |
| 9466 | if (!root) |
| 9467 | goto free; |
| 9468 | |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 9469 | if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
| 9470 | &BTRFS_I(inode)->runtime_flags)) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9471 | btrfs_info(fs_info, "inode %llu still on the orphan list", |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 9472 | btrfs_ino(BTRFS_I(inode))); |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 9473 | atomic_dec(&root->orphan_inodes); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 9474 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 9475 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 9476 | while (1) { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 9477 | ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1); |
| 9478 | if (!ordered) |
| 9479 | break; |
| 9480 | else { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9481 | btrfs_err(fs_info, |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 9482 | "found ordered extent %llu %llu on inode cleanup", |
| 9483 | ordered->file_offset, ordered->len); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 9484 | btrfs_remove_ordered_extent(inode, ordered); |
| 9485 | btrfs_put_ordered_extent(ordered); |
| 9486 | btrfs_put_ordered_extent(ordered); |
| 9487 | } |
| 9488 | } |
Qu Wenruo | 56fa9d0 | 2015-10-13 09:53:10 +0800 | [diff] [blame] | 9489 | btrfs_qgroup_check_reserved_leak(inode); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 9490 | inode_tree_del(inode); |
Nikolay Borisov | dcdbc05 | 2017-02-20 13:50:45 +0200 | [diff] [blame] | 9491 | btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0); |
Josef Bacik | a6dbd42 | 2009-11-11 15:53:34 -0500 | [diff] [blame] | 9492 | free: |
Nick Piggin | fa0d7e3d | 2011-01-07 17:49:49 +1100 | [diff] [blame] | 9493 | call_rcu(&inode->i_rcu, btrfs_i_callback); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9494 | } |
| 9495 | |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 9496 | int btrfs_drop_inode(struct inode *inode) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 9497 | { |
| 9498 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 9499 | |
Naohiro Aota | 6379ef9 | 2013-06-06 09:56:34 +0000 | [diff] [blame] | 9500 | if (root == NULL) |
| 9501 | return 1; |
| 9502 | |
Liu Bo | fa6ac87 | 2013-02-20 14:10:23 +0000 | [diff] [blame] | 9503 | /* the snap/subvol tree is on deleting */ |
Stefan Behrens | 69e9c6c | 2013-09-05 16:58:43 +0200 | [diff] [blame] | 9504 | if (btrfs_root_refs(&root->root_item) == 0) |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 9505 | return 1; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 9506 | else |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 9507 | return generic_drop_inode(inode); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 9508 | } |
| 9509 | |
Sven Wegener | 0ee0fda | 2008-07-30 16:54:26 -0400 | [diff] [blame] | 9510 | static void init_once(void *foo) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9511 | { |
| 9512 | struct btrfs_inode *ei = (struct btrfs_inode *) foo; |
| 9513 | |
| 9514 | inode_init_once(&ei->vfs_inode); |
| 9515 | } |
| 9516 | |
| 9517 | void btrfs_destroy_cachep(void) |
| 9518 | { |
Kirill A. Shutemov | 8c0a853 | 2012-09-26 11:33:07 +1000 | [diff] [blame] | 9519 | /* |
| 9520 | * Make sure all delayed rcu free inodes are flushed before we |
| 9521 | * destroy cache. |
| 9522 | */ |
| 9523 | rcu_barrier(); |
Kinglong Mee | 5598e90 | 2016-01-29 21:36:35 +0800 | [diff] [blame] | 9524 | kmem_cache_destroy(btrfs_inode_cachep); |
| 9525 | kmem_cache_destroy(btrfs_trans_handle_cachep); |
Kinglong Mee | 5598e90 | 2016-01-29 21:36:35 +0800 | [diff] [blame] | 9526 | kmem_cache_destroy(btrfs_path_cachep); |
| 9527 | kmem_cache_destroy(btrfs_free_space_cachep); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9528 | } |
| 9529 | |
| 9530 | int btrfs_init_cachep(void) |
| 9531 | { |
David Sterba | 837e197 | 2012-09-07 03:00:48 -0600 | [diff] [blame] | 9532 | btrfs_inode_cachep = kmem_cache_create("btrfs_inode", |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 9533 | sizeof(struct btrfs_inode), 0, |
Vladimir Davydov | 5d09705 | 2016-01-14 15:18:21 -0800 | [diff] [blame] | 9534 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT, |
| 9535 | init_once); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9536 | if (!btrfs_inode_cachep) |
| 9537 | goto fail; |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 9538 | |
David Sterba | 837e197 | 2012-09-07 03:00:48 -0600 | [diff] [blame] | 9539 | btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle", |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 9540 | sizeof(struct btrfs_trans_handle), 0, |
Nikolay Borisov | fba4b69 | 2016-06-23 21:17:08 +0300 | [diff] [blame] | 9541 | SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9542 | if (!btrfs_trans_handle_cachep) |
| 9543 | goto fail; |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 9544 | |
David Sterba | 837e197 | 2012-09-07 03:00:48 -0600 | [diff] [blame] | 9545 | btrfs_path_cachep = kmem_cache_create("btrfs_path", |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 9546 | sizeof(struct btrfs_path), 0, |
Nikolay Borisov | fba4b69 | 2016-06-23 21:17:08 +0300 | [diff] [blame] | 9547 | SLAB_MEM_SPREAD, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9548 | if (!btrfs_path_cachep) |
| 9549 | goto fail; |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 9550 | |
David Sterba | 837e197 | 2012-09-07 03:00:48 -0600 | [diff] [blame] | 9551 | btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space", |
Josef Bacik | dc89e98 | 2011-01-28 17:05:48 -0500 | [diff] [blame] | 9552 | sizeof(struct btrfs_free_space), 0, |
Nikolay Borisov | fba4b69 | 2016-06-23 21:17:08 +0300 | [diff] [blame] | 9553 | SLAB_MEM_SPREAD, NULL); |
Josef Bacik | dc89e98 | 2011-01-28 17:05:48 -0500 | [diff] [blame] | 9554 | if (!btrfs_free_space_cachep) |
| 9555 | goto fail; |
| 9556 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9557 | return 0; |
| 9558 | fail: |
| 9559 | btrfs_destroy_cachep(); |
| 9560 | return -ENOMEM; |
| 9561 | } |
| 9562 | |
David Howells | a528d35 | 2017-01-31 16:46:22 +0000 | [diff] [blame] | 9563 | static int btrfs_getattr(const struct path *path, struct kstat *stat, |
| 9564 | u32 request_mask, unsigned int flags) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9565 | { |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 9566 | u64 delalloc_bytes; |
David Howells | a528d35 | 2017-01-31 16:46:22 +0000 | [diff] [blame] | 9567 | struct inode *inode = d_inode(path->dentry); |
David Sterba | fadc0d8 | 2011-11-20 07:33:38 -0500 | [diff] [blame] | 9568 | u32 blocksize = inode->i_sb->s_blocksize; |
Yonghong Song | 04a87e3 | 2017-05-12 15:07:43 -0700 | [diff] [blame] | 9569 | u32 bi_flags = BTRFS_I(inode)->flags; |
| 9570 | |
| 9571 | stat->result_mask |= STATX_BTIME; |
| 9572 | stat->btime.tv_sec = BTRFS_I(inode)->i_otime.tv_sec; |
| 9573 | stat->btime.tv_nsec = BTRFS_I(inode)->i_otime.tv_nsec; |
| 9574 | if (bi_flags & BTRFS_INODE_APPEND) |
| 9575 | stat->attributes |= STATX_ATTR_APPEND; |
| 9576 | if (bi_flags & BTRFS_INODE_COMPRESS) |
| 9577 | stat->attributes |= STATX_ATTR_COMPRESSED; |
| 9578 | if (bi_flags & BTRFS_INODE_IMMUTABLE) |
| 9579 | stat->attributes |= STATX_ATTR_IMMUTABLE; |
| 9580 | if (bi_flags & BTRFS_INODE_NODUMP) |
| 9581 | stat->attributes |= STATX_ATTR_NODUMP; |
| 9582 | |
| 9583 | stat->attributes_mask |= (STATX_ATTR_APPEND | |
| 9584 | STATX_ATTR_COMPRESSED | |
| 9585 | STATX_ATTR_IMMUTABLE | |
| 9586 | STATX_ATTR_NODUMP); |
David Sterba | fadc0d8 | 2011-11-20 07:33:38 -0500 | [diff] [blame] | 9587 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9588 | generic_fillattr(inode, stat); |
Al Viro | 0ee5dc6 | 2011-07-07 15:44:25 -0400 | [diff] [blame] | 9589 | stat->dev = BTRFS_I(inode)->root->anon_dev; |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 9590 | |
| 9591 | spin_lock(&BTRFS_I(inode)->lock); |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 9592 | delalloc_bytes = BTRFS_I(inode)->new_delalloc_bytes; |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 9593 | spin_unlock(&BTRFS_I(inode)->lock); |
David Sterba | fadc0d8 | 2011-11-20 07:33:38 -0500 | [diff] [blame] | 9594 | stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) + |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 9595 | ALIGN(delalloc_bytes, blocksize)) >> 9; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9596 | return 0; |
| 9597 | } |
| 9598 | |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9599 | static int btrfs_rename_exchange(struct inode *old_dir, |
| 9600 | struct dentry *old_dentry, |
| 9601 | struct inode *new_dir, |
| 9602 | struct dentry *new_dentry) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9603 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9604 | struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9605 | struct btrfs_trans_handle *trans; |
| 9606 | struct btrfs_root *root = BTRFS_I(old_dir)->root; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9607 | struct btrfs_root *dest = BTRFS_I(new_dir)->root; |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9608 | struct inode *new_inode = new_dentry->d_inode; |
| 9609 | struct inode *old_inode = old_dentry->d_inode; |
Deepa Dinamani | c2050a4 | 2016-09-14 07:48:06 -0700 | [diff] [blame] | 9610 | struct timespec ctime = current_time(old_inode); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9611 | struct dentry *parent; |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 9612 | u64 old_ino = btrfs_ino(BTRFS_I(old_inode)); |
| 9613 | u64 new_ino = btrfs_ino(BTRFS_I(new_inode)); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9614 | u64 old_idx = 0; |
| 9615 | u64 new_idx = 0; |
| 9616 | u64 root_objectid; |
| 9617 | int ret; |
Filipe Manana | 86e8aa0 | 2016-05-05 02:02:27 +0100 | [diff] [blame] | 9618 | bool root_log_pinned = false; |
| 9619 | bool dest_log_pinned = false; |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9620 | |
| 9621 | /* we only allow rename subvolume link between subvolumes */ |
| 9622 | if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest) |
| 9623 | return -EXDEV; |
| 9624 | |
| 9625 | /* close the race window with snapshot create/destroy ioctl */ |
| 9626 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9627 | down_read(&fs_info->subvol_sem); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9628 | if (new_ino == BTRFS_FIRST_FREE_OBJECTID) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9629 | down_read(&fs_info->subvol_sem); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9630 | |
| 9631 | /* |
| 9632 | * We want to reserve the absolute worst case amount of items. So if |
| 9633 | * both inodes are subvols and we need to unlink them then that would |
| 9634 | * require 4 item modifications, but if they are both normal inodes it |
| 9635 | * would require 5 item modifications, so we'll assume their normal |
| 9636 | * inodes. So 5 * 2 is 10, plus 2 for the new links, so 12 total items |
| 9637 | * should cover the worst case number of items we'll modify. |
| 9638 | */ |
| 9639 | trans = btrfs_start_transaction(root, 12); |
| 9640 | if (IS_ERR(trans)) { |
| 9641 | ret = PTR_ERR(trans); |
| 9642 | goto out_notrans; |
| 9643 | } |
| 9644 | |
| 9645 | /* |
| 9646 | * We need to find a free sequence number both in the source and |
| 9647 | * in the destination directory for the exchange. |
| 9648 | */ |
Nikolay Borisov | 877574e | 2017-02-20 13:50:33 +0200 | [diff] [blame] | 9649 | ret = btrfs_set_inode_index(BTRFS_I(new_dir), &old_idx); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9650 | if (ret) |
| 9651 | goto out_fail; |
Nikolay Borisov | 877574e | 2017-02-20 13:50:33 +0200 | [diff] [blame] | 9652 | ret = btrfs_set_inode_index(BTRFS_I(old_dir), &new_idx); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9653 | if (ret) |
| 9654 | goto out_fail; |
| 9655 | |
| 9656 | BTRFS_I(old_inode)->dir_index = 0ULL; |
| 9657 | BTRFS_I(new_inode)->dir_index = 0ULL; |
| 9658 | |
| 9659 | /* Reference for the source. */ |
| 9660 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) { |
| 9661 | /* force full log commit if subvolume involved. */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9662 | btrfs_set_log_full_commit(fs_info, trans); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9663 | } else { |
Filipe Manana | 376e5a5 | 2016-05-05 02:08:56 +0100 | [diff] [blame] | 9664 | btrfs_pin_log_trans(root); |
| 9665 | root_log_pinned = true; |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9666 | ret = btrfs_insert_inode_ref(trans, dest, |
| 9667 | new_dentry->d_name.name, |
| 9668 | new_dentry->d_name.len, |
| 9669 | old_ino, |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 9670 | btrfs_ino(BTRFS_I(new_dir)), |
| 9671 | old_idx); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9672 | if (ret) |
| 9673 | goto out_fail; |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9674 | } |
| 9675 | |
| 9676 | /* And now for the dest. */ |
| 9677 | if (new_ino == BTRFS_FIRST_FREE_OBJECTID) { |
| 9678 | /* force full log commit if subvolume involved. */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9679 | btrfs_set_log_full_commit(fs_info, trans); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9680 | } else { |
Filipe Manana | 376e5a5 | 2016-05-05 02:08:56 +0100 | [diff] [blame] | 9681 | btrfs_pin_log_trans(dest); |
| 9682 | dest_log_pinned = true; |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9683 | ret = btrfs_insert_inode_ref(trans, root, |
| 9684 | old_dentry->d_name.name, |
| 9685 | old_dentry->d_name.len, |
| 9686 | new_ino, |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 9687 | btrfs_ino(BTRFS_I(old_dir)), |
| 9688 | new_idx); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9689 | if (ret) |
| 9690 | goto out_fail; |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9691 | } |
| 9692 | |
| 9693 | /* Update inode version and ctime/mtime. */ |
| 9694 | inode_inc_iversion(old_dir); |
| 9695 | inode_inc_iversion(new_dir); |
| 9696 | inode_inc_iversion(old_inode); |
| 9697 | inode_inc_iversion(new_inode); |
| 9698 | old_dir->i_ctime = old_dir->i_mtime = ctime; |
| 9699 | new_dir->i_ctime = new_dir->i_mtime = ctime; |
| 9700 | old_inode->i_ctime = ctime; |
| 9701 | new_inode->i_ctime = ctime; |
| 9702 | |
| 9703 | if (old_dentry->d_parent != new_dentry->d_parent) { |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 9704 | btrfs_record_unlink_dir(trans, BTRFS_I(old_dir), |
| 9705 | BTRFS_I(old_inode), 1); |
| 9706 | btrfs_record_unlink_dir(trans, BTRFS_I(new_dir), |
| 9707 | BTRFS_I(new_inode), 1); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9708 | } |
| 9709 | |
| 9710 | /* src is a subvolume */ |
| 9711 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) { |
| 9712 | root_objectid = BTRFS_I(old_inode)->root->root_key.objectid; |
| 9713 | ret = btrfs_unlink_subvol(trans, root, old_dir, |
| 9714 | root_objectid, |
| 9715 | old_dentry->d_name.name, |
| 9716 | old_dentry->d_name.len); |
| 9717 | } else { /* src is an inode */ |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 9718 | ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir), |
| 9719 | BTRFS_I(old_dentry->d_inode), |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9720 | old_dentry->d_name.name, |
| 9721 | old_dentry->d_name.len); |
| 9722 | if (!ret) |
| 9723 | ret = btrfs_update_inode(trans, root, old_inode); |
| 9724 | } |
| 9725 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 9726 | btrfs_abort_transaction(trans, ret); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9727 | goto out_fail; |
| 9728 | } |
| 9729 | |
| 9730 | /* dest is a subvolume */ |
| 9731 | if (new_ino == BTRFS_FIRST_FREE_OBJECTID) { |
| 9732 | root_objectid = BTRFS_I(new_inode)->root->root_key.objectid; |
| 9733 | ret = btrfs_unlink_subvol(trans, dest, new_dir, |
| 9734 | root_objectid, |
| 9735 | new_dentry->d_name.name, |
| 9736 | new_dentry->d_name.len); |
| 9737 | } else { /* dest is an inode */ |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 9738 | ret = __btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir), |
| 9739 | BTRFS_I(new_dentry->d_inode), |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9740 | new_dentry->d_name.name, |
| 9741 | new_dentry->d_name.len); |
| 9742 | if (!ret) |
| 9743 | ret = btrfs_update_inode(trans, dest, new_inode); |
| 9744 | } |
| 9745 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 9746 | btrfs_abort_transaction(trans, ret); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9747 | goto out_fail; |
| 9748 | } |
| 9749 | |
Nikolay Borisov | db0a669 | 2017-02-20 13:51:08 +0200 | [diff] [blame] | 9750 | 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] | 9751 | new_dentry->d_name.name, |
| 9752 | new_dentry->d_name.len, 0, old_idx); |
| 9753 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 9754 | btrfs_abort_transaction(trans, ret); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9755 | goto out_fail; |
| 9756 | } |
| 9757 | |
Nikolay Borisov | db0a669 | 2017-02-20 13:51:08 +0200 | [diff] [blame] | 9758 | 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] | 9759 | old_dentry->d_name.name, |
| 9760 | old_dentry->d_name.len, 0, new_idx); |
| 9761 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 9762 | btrfs_abort_transaction(trans, ret); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9763 | goto out_fail; |
| 9764 | } |
| 9765 | |
| 9766 | if (old_inode->i_nlink == 1) |
| 9767 | BTRFS_I(old_inode)->dir_index = old_idx; |
| 9768 | if (new_inode->i_nlink == 1) |
| 9769 | BTRFS_I(new_inode)->dir_index = new_idx; |
| 9770 | |
Filipe Manana | 86e8aa0 | 2016-05-05 02:02:27 +0100 | [diff] [blame] | 9771 | if (root_log_pinned) { |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9772 | parent = new_dentry->d_parent; |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 9773 | btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir), |
| 9774 | parent); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9775 | btrfs_end_log_trans(root); |
Filipe Manana | 86e8aa0 | 2016-05-05 02:02:27 +0100 | [diff] [blame] | 9776 | root_log_pinned = false; |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9777 | } |
Filipe Manana | 86e8aa0 | 2016-05-05 02:02:27 +0100 | [diff] [blame] | 9778 | if (dest_log_pinned) { |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9779 | parent = old_dentry->d_parent; |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 9780 | btrfs_log_new_name(trans, BTRFS_I(new_inode), BTRFS_I(new_dir), |
| 9781 | parent); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9782 | btrfs_end_log_trans(dest); |
Filipe Manana | 86e8aa0 | 2016-05-05 02:02:27 +0100 | [diff] [blame] | 9783 | dest_log_pinned = false; |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9784 | } |
| 9785 | out_fail: |
Filipe Manana | 86e8aa0 | 2016-05-05 02:02:27 +0100 | [diff] [blame] | 9786 | /* |
| 9787 | * If we have pinned a log and an error happened, we unpin tasks |
| 9788 | * trying to sync the log and force them to fallback to a transaction |
| 9789 | * commit if the log currently contains any of the inodes involved in |
| 9790 | * this rename operation (to ensure we do not persist a log with an |
| 9791 | * inconsistent state for any of these inodes or leading to any |
| 9792 | * inconsistencies when replayed). If the transaction was aborted, the |
| 9793 | * abortion reason is propagated to userspace when attempting to commit |
| 9794 | * the transaction. If the log does not contain any of these inodes, we |
| 9795 | * allow the tasks to sync it. |
| 9796 | */ |
| 9797 | if (ret && (root_log_pinned || dest_log_pinned)) { |
Nikolay Borisov | 0f8939b | 2017-01-18 00:31:30 +0200 | [diff] [blame] | 9798 | if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) || |
| 9799 | btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) || |
| 9800 | btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) || |
Filipe Manana | 86e8aa0 | 2016-05-05 02:02:27 +0100 | [diff] [blame] | 9801 | (new_inode && |
Nikolay Borisov | 0f8939b | 2017-01-18 00:31:30 +0200 | [diff] [blame] | 9802 | btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation))) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9803 | btrfs_set_log_full_commit(fs_info, trans); |
Filipe Manana | 86e8aa0 | 2016-05-05 02:02:27 +0100 | [diff] [blame] | 9804 | |
| 9805 | if (root_log_pinned) { |
| 9806 | btrfs_end_log_trans(root); |
| 9807 | root_log_pinned = false; |
| 9808 | } |
| 9809 | if (dest_log_pinned) { |
| 9810 | btrfs_end_log_trans(dest); |
| 9811 | dest_log_pinned = false; |
| 9812 | } |
| 9813 | } |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 9814 | ret = btrfs_end_transaction(trans); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9815 | out_notrans: |
| 9816 | if (new_ino == BTRFS_FIRST_FREE_OBJECTID) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9817 | up_read(&fs_info->subvol_sem); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9818 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9819 | up_read(&fs_info->subvol_sem); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9820 | |
| 9821 | return ret; |
| 9822 | } |
| 9823 | |
| 9824 | static int btrfs_whiteout_for_rename(struct btrfs_trans_handle *trans, |
| 9825 | struct btrfs_root *root, |
| 9826 | struct inode *dir, |
| 9827 | struct dentry *dentry) |
| 9828 | { |
| 9829 | int ret; |
| 9830 | struct inode *inode; |
| 9831 | u64 objectid; |
| 9832 | u64 index; |
| 9833 | |
| 9834 | ret = btrfs_find_free_ino(root, &objectid); |
| 9835 | if (ret) |
| 9836 | return ret; |
| 9837 | |
| 9838 | inode = btrfs_new_inode(trans, root, dir, |
| 9839 | dentry->d_name.name, |
| 9840 | dentry->d_name.len, |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 9841 | btrfs_ino(BTRFS_I(dir)), |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9842 | objectid, |
| 9843 | S_IFCHR | WHITEOUT_MODE, |
| 9844 | &index); |
| 9845 | |
| 9846 | if (IS_ERR(inode)) { |
| 9847 | ret = PTR_ERR(inode); |
| 9848 | return ret; |
| 9849 | } |
| 9850 | |
| 9851 | inode->i_op = &btrfs_special_inode_operations; |
| 9852 | init_special_inode(inode, inode->i_mode, |
| 9853 | WHITEOUT_DEV); |
| 9854 | |
| 9855 | ret = btrfs_init_inode_security(trans, inode, dir, |
| 9856 | &dentry->d_name); |
| 9857 | if (ret) |
Filipe Manana | c990161 | 2016-05-05 01:41:57 +0100 | [diff] [blame] | 9858 | goto out; |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9859 | |
Nikolay Borisov | cef415a | 2017-02-20 13:51:09 +0200 | [diff] [blame] | 9860 | ret = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, |
| 9861 | BTRFS_I(inode), 0, index); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9862 | if (ret) |
Filipe Manana | c990161 | 2016-05-05 01:41:57 +0100 | [diff] [blame] | 9863 | goto out; |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9864 | |
| 9865 | ret = btrfs_update_inode(trans, root, inode); |
Filipe Manana | c990161 | 2016-05-05 01:41:57 +0100 | [diff] [blame] | 9866 | out: |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9867 | unlock_new_inode(inode); |
Filipe Manana | c990161 | 2016-05-05 01:41:57 +0100 | [diff] [blame] | 9868 | if (ret) |
| 9869 | inode_dec_link_count(inode); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9870 | iput(inode); |
| 9871 | |
Filipe Manana | c990161 | 2016-05-05 01:41:57 +0100 | [diff] [blame] | 9872 | return ret; |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9873 | } |
| 9874 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9875 | static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry, |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9876 | struct inode *new_dir, struct dentry *new_dentry, |
| 9877 | unsigned int flags) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9878 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9879 | struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9880 | struct btrfs_trans_handle *trans; |
Filipe Manana | 5062af3 | 2016-05-05 10:26:26 +0100 | [diff] [blame] | 9881 | unsigned int trans_num_items; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9882 | struct btrfs_root *root = BTRFS_I(old_dir)->root; |
| 9883 | struct btrfs_root *dest = BTRFS_I(new_dir)->root; |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 9884 | struct inode *new_inode = d_inode(new_dentry); |
| 9885 | struct inode *old_inode = d_inode(old_dentry); |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 9886 | u64 index = 0; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9887 | u64 root_objectid; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9888 | int ret; |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 9889 | u64 old_ino = btrfs_ino(BTRFS_I(old_inode)); |
Filipe Manana | 3dc9e8f7 | 2016-04-29 11:34:22 +0100 | [diff] [blame] | 9890 | bool log_pinned = false; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9891 | |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 9892 | if (btrfs_ino(BTRFS_I(new_dir)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) |
Yan, Zheng | f679a84 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 9893 | return -EPERM; |
| 9894 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9895 | /* we only allow rename subvolume link between subvolumes */ |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 9896 | if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest) |
Chris Mason | 3394e16 | 2008-11-17 20:42:26 -0500 | [diff] [blame] | 9897 | return -EXDEV; |
| 9898 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 9899 | if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID || |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 9900 | (new_inode && btrfs_ino(BTRFS_I(new_inode)) == BTRFS_FIRST_FREE_OBJECTID)) |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9901 | return -ENOTEMPTY; |
| 9902 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9903 | if (S_ISDIR(old_inode->i_mode) && new_inode && |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9904 | new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9905 | return -ENOTEMPTY; |
Chris Mason | 9c52057 | 2012-12-17 14:26:57 -0500 | [diff] [blame] | 9906 | |
| 9907 | |
| 9908 | /* check for collisions, even if the name isn't there */ |
Josef Bacik | 4871c15 | 2013-10-09 12:24:04 -0400 | [diff] [blame] | 9909 | ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino, |
Chris Mason | 9c52057 | 2012-12-17 14:26:57 -0500 | [diff] [blame] | 9910 | new_dentry->d_name.name, |
| 9911 | new_dentry->d_name.len); |
| 9912 | |
| 9913 | if (ret) { |
| 9914 | if (ret == -EEXIST) { |
| 9915 | /* we shouldn't get |
| 9916 | * eexist without a new_inode */ |
Dulshani Gunawardhana | fae7f21 | 2013-10-31 10:30:08 +0530 | [diff] [blame] | 9917 | if (WARN_ON(!new_inode)) { |
Chris Mason | 9c52057 | 2012-12-17 14:26:57 -0500 | [diff] [blame] | 9918 | return ret; |
| 9919 | } |
| 9920 | } else { |
| 9921 | /* maybe -EOVERFLOW */ |
| 9922 | return ret; |
| 9923 | } |
| 9924 | } |
| 9925 | ret = 0; |
| 9926 | |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 9927 | /* |
Chris Mason | 8d875f9 | 2014-08-12 10:47:42 -0700 | [diff] [blame] | 9928 | * we're using rename to replace one file with another. Start IO on it |
| 9929 | * 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] | 9930 | */ |
Chris Mason | 8d875f9 | 2014-08-12 10:47:42 -0700 | [diff] [blame] | 9931 | 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] | 9932 | filemap_flush(old_inode->i_mapping); |
| 9933 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 9934 | /* close the racy window with snapshot create/destroy ioctl */ |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 9935 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9936 | down_read(&fs_info->subvol_sem); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 9937 | /* |
| 9938 | * We want to reserve the absolute worst case amount of items. So if |
| 9939 | * both inodes are subvols and we need to unlink them then that would |
| 9940 | * require 4 item modifications, but if they are both normal inodes it |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9941 | * would require 5 item modifications, so we'll assume they are normal |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 9942 | * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items |
| 9943 | * should cover the worst case number of items we'll modify. |
Filipe Manana | 5062af3 | 2016-05-05 10:26:26 +0100 | [diff] [blame] | 9944 | * If our rename has the whiteout flag, we need more 5 units for the |
| 9945 | * new inode (1 inode item, 1 inode ref, 2 dir items and 1 xattr item |
| 9946 | * when selinux is enabled). |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 9947 | */ |
Filipe Manana | 5062af3 | 2016-05-05 10:26:26 +0100 | [diff] [blame] | 9948 | trans_num_items = 11; |
| 9949 | if (flags & RENAME_WHITEOUT) |
| 9950 | trans_num_items += 5; |
| 9951 | trans = btrfs_start_transaction(root, trans_num_items); |
Johann Lombardi | b44c59a | 2011-03-31 13:23:47 +0000 | [diff] [blame] | 9952 | if (IS_ERR(trans)) { |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9953 | ret = PTR_ERR(trans); |
| 9954 | goto out_notrans; |
| 9955 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 9956 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9957 | if (dest != root) |
| 9958 | btrfs_record_root_in_trans(trans, dest); |
| 9959 | |
Nikolay Borisov | 877574e | 2017-02-20 13:50:33 +0200 | [diff] [blame] | 9960 | ret = btrfs_set_inode_index(BTRFS_I(new_dir), &index); |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 9961 | if (ret) |
| 9962 | goto out_fail; |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 9963 | |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 9964 | BTRFS_I(old_inode)->dir_index = 0ULL; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 9965 | if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9966 | /* force full log commit if subvolume involved. */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9967 | btrfs_set_log_full_commit(fs_info, trans); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9968 | } else { |
Filipe Manana | c4aba95 | 2016-04-29 13:14:42 +0100 | [diff] [blame] | 9969 | btrfs_pin_log_trans(root); |
| 9970 | log_pinned = true; |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 9971 | ret = btrfs_insert_inode_ref(trans, dest, |
| 9972 | new_dentry->d_name.name, |
| 9973 | new_dentry->d_name.len, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 9974 | old_ino, |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 9975 | btrfs_ino(BTRFS_I(new_dir)), index); |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 9976 | if (ret) |
| 9977 | goto out_fail; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9978 | } |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 9979 | |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 9980 | inode_inc_iversion(old_dir); |
| 9981 | inode_inc_iversion(new_dir); |
| 9982 | inode_inc_iversion(old_inode); |
Deepa Dinamani | 04b285f | 2016-02-06 23:57:21 -0800 | [diff] [blame] | 9983 | old_dir->i_ctime = old_dir->i_mtime = |
| 9984 | new_dir->i_ctime = new_dir->i_mtime = |
Deepa Dinamani | c2050a4 | 2016-09-14 07:48:06 -0700 | [diff] [blame] | 9985 | old_inode->i_ctime = current_time(old_dir); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 9986 | |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 9987 | if (old_dentry->d_parent != new_dentry->d_parent) |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 9988 | btrfs_record_unlink_dir(trans, BTRFS_I(old_dir), |
| 9989 | BTRFS_I(old_inode), 1); |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 9990 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 9991 | if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9992 | root_objectid = BTRFS_I(old_inode)->root->root_key.objectid; |
| 9993 | ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid, |
| 9994 | old_dentry->d_name.name, |
| 9995 | old_dentry->d_name.len); |
| 9996 | } else { |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 9997 | ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir), |
| 9998 | BTRFS_I(d_inode(old_dentry)), |
Al Viro | 9298679 | 2011-03-04 17:14:37 +0000 | [diff] [blame] | 9999 | old_dentry->d_name.name, |
| 10000 | old_dentry->d_name.len); |
| 10001 | if (!ret) |
| 10002 | ret = btrfs_update_inode(trans, root, old_inode); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 10003 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 10004 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 10005 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 10006 | goto out_fail; |
| 10007 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10008 | |
| 10009 | if (new_inode) { |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 10010 | inode_inc_iversion(new_inode); |
Deepa Dinamani | c2050a4 | 2016-09-14 07:48:06 -0700 | [diff] [blame] | 10011 | new_inode->i_ctime = current_time(new_inode); |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 10012 | if (unlikely(btrfs_ino(BTRFS_I(new_inode)) == |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 10013 | BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) { |
| 10014 | root_objectid = BTRFS_I(new_inode)->location.objectid; |
| 10015 | ret = btrfs_unlink_subvol(trans, dest, new_dir, |
| 10016 | root_objectid, |
| 10017 | new_dentry->d_name.name, |
| 10018 | new_dentry->d_name.len); |
| 10019 | BUG_ON(new_inode->i_nlink == 0); |
| 10020 | } else { |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 10021 | ret = btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir), |
| 10022 | BTRFS_I(d_inode(new_dentry)), |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 10023 | new_dentry->d_name.name, |
| 10024 | new_dentry->d_name.len); |
| 10025 | } |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 10026 | if (!ret && new_inode->i_nlink == 0) |
Nikolay Borisov | 73f2e54 | 2017-02-20 13:50:59 +0200 | [diff] [blame] | 10027 | ret = btrfs_orphan_add(trans, |
| 10028 | BTRFS_I(d_inode(new_dentry))); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 10029 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 10030 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 10031 | goto out_fail; |
| 10032 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10033 | } |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 10034 | |
Nikolay Borisov | db0a669 | 2017-02-20 13:51:08 +0200 | [diff] [blame] | 10035 | 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] | 10036 | new_dentry->d_name.name, |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 10037 | new_dentry->d_name.len, 0, index); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 10038 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 10039 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 10040 | goto out_fail; |
| 10041 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10042 | |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 10043 | if (old_inode->i_nlink == 1) |
| 10044 | BTRFS_I(old_inode)->dir_index = index; |
| 10045 | |
Filipe Manana | 3dc9e8f7 | 2016-04-29 11:34:22 +0100 | [diff] [blame] | 10046 | if (log_pinned) { |
Al Viro | 10d9f30 | 2011-07-16 23:09:10 -0400 | [diff] [blame] | 10047 | struct dentry *parent = new_dentry->d_parent; |
Filipe Manana | 3dc9e8f7 | 2016-04-29 11:34:22 +0100 | [diff] [blame] | 10048 | |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 10049 | btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir), |
| 10050 | parent); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 10051 | btrfs_end_log_trans(root); |
Filipe Manana | 3dc9e8f7 | 2016-04-29 11:34:22 +0100 | [diff] [blame] | 10052 | log_pinned = false; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 10053 | } |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 10054 | |
| 10055 | if (flags & RENAME_WHITEOUT) { |
| 10056 | ret = btrfs_whiteout_for_rename(trans, root, old_dir, |
| 10057 | old_dentry); |
| 10058 | |
| 10059 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 10060 | btrfs_abort_transaction(trans, ret); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 10061 | goto out_fail; |
| 10062 | } |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 10063 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10064 | out_fail: |
Filipe Manana | 3dc9e8f7 | 2016-04-29 11:34:22 +0100 | [diff] [blame] | 10065 | /* |
| 10066 | * If we have pinned the log and an error happened, we unpin tasks |
| 10067 | * trying to sync the log and force them to fallback to a transaction |
| 10068 | * commit if the log currently contains any of the inodes involved in |
| 10069 | * this rename operation (to ensure we do not persist a log with an |
| 10070 | * inconsistent state for any of these inodes or leading to any |
| 10071 | * inconsistencies when replayed). If the transaction was aborted, the |
| 10072 | * abortion reason is propagated to userspace when attempting to commit |
| 10073 | * the transaction. If the log does not contain any of these inodes, we |
| 10074 | * allow the tasks to sync it. |
| 10075 | */ |
| 10076 | if (ret && log_pinned) { |
Nikolay Borisov | 0f8939b | 2017-01-18 00:31:30 +0200 | [diff] [blame] | 10077 | if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) || |
| 10078 | btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) || |
| 10079 | btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) || |
Filipe Manana | 3dc9e8f7 | 2016-04-29 11:34:22 +0100 | [diff] [blame] | 10080 | (new_inode && |
Nikolay Borisov | 0f8939b | 2017-01-18 00:31:30 +0200 | [diff] [blame] | 10081 | btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation))) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 10082 | btrfs_set_log_full_commit(fs_info, trans); |
Filipe Manana | 3dc9e8f7 | 2016-04-29 11:34:22 +0100 | [diff] [blame] | 10083 | |
| 10084 | btrfs_end_log_trans(root); |
| 10085 | log_pinned = false; |
| 10086 | } |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 10087 | btrfs_end_transaction(trans); |
Johann Lombardi | b44c59a | 2011-03-31 13:23:47 +0000 | [diff] [blame] | 10088 | out_notrans: |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 10089 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 10090 | up_read(&fs_info->subvol_sem); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 10091 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10092 | return ret; |
| 10093 | } |
| 10094 | |
Miklos Szeredi | 80ace85 | 2014-07-23 15:15:32 +0200 | [diff] [blame] | 10095 | static int btrfs_rename2(struct inode *old_dir, struct dentry *old_dentry, |
| 10096 | struct inode *new_dir, struct dentry *new_dentry, |
| 10097 | unsigned int flags) |
| 10098 | { |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 10099 | if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT)) |
Miklos Szeredi | 80ace85 | 2014-07-23 15:15:32 +0200 | [diff] [blame] | 10100 | return -EINVAL; |
| 10101 | |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 10102 | if (flags & RENAME_EXCHANGE) |
| 10103 | return btrfs_rename_exchange(old_dir, old_dentry, new_dir, |
| 10104 | new_dentry); |
| 10105 | |
| 10106 | return btrfs_rename(old_dir, old_dentry, new_dir, new_dentry, flags); |
Miklos Szeredi | 80ace85 | 2014-07-23 15:15:32 +0200 | [diff] [blame] | 10107 | } |
| 10108 | |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 10109 | static void btrfs_run_delalloc_work(struct btrfs_work *work) |
| 10110 | { |
| 10111 | struct btrfs_delalloc_work *delalloc_work; |
Josef Bacik | 9f23e28 | 2013-10-28 15:03:41 -0400 | [diff] [blame] | 10112 | struct inode *inode; |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 10113 | |
| 10114 | delalloc_work = container_of(work, struct btrfs_delalloc_work, |
| 10115 | work); |
Josef Bacik | 9f23e28 | 2013-10-28 15:03:41 -0400 | [diff] [blame] | 10116 | inode = delalloc_work->inode; |
David Sterba | 3042460 | 2015-11-27 19:27:11 +0100 | [diff] [blame] | 10117 | filemap_flush(inode->i_mapping); |
| 10118 | if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, |
| 10119 | &BTRFS_I(inode)->runtime_flags)) |
Josef Bacik | 9f23e28 | 2013-10-28 15:03:41 -0400 | [diff] [blame] | 10120 | filemap_flush(inode->i_mapping); |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 10121 | |
| 10122 | if (delalloc_work->delay_iput) |
Josef Bacik | 9f23e28 | 2013-10-28 15:03:41 -0400 | [diff] [blame] | 10123 | btrfs_add_delayed_iput(inode); |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 10124 | else |
Josef Bacik | 9f23e28 | 2013-10-28 15:03:41 -0400 | [diff] [blame] | 10125 | iput(inode); |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 10126 | complete(&delalloc_work->completion); |
| 10127 | } |
| 10128 | |
| 10129 | struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode, |
David Sterba | 651d494 | 2015-11-27 19:24:16 +0100 | [diff] [blame] | 10130 | int delay_iput) |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 10131 | { |
| 10132 | struct btrfs_delalloc_work *work; |
| 10133 | |
David Sterba | 100d570 | 2015-12-08 14:39:32 +0100 | [diff] [blame] | 10134 | work = kmalloc(sizeof(*work), GFP_NOFS); |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 10135 | if (!work) |
| 10136 | return NULL; |
| 10137 | |
| 10138 | init_completion(&work->completion); |
| 10139 | INIT_LIST_HEAD(&work->list); |
| 10140 | work->inode = inode; |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 10141 | work->delay_iput = delay_iput; |
Liu Bo | 9e0af23 | 2014-08-15 23:36:53 +0800 | [diff] [blame] | 10142 | WARN_ON_ONCE(!inode); |
| 10143 | btrfs_init_work(&work->work, btrfs_flush_delalloc_helper, |
| 10144 | btrfs_run_delalloc_work, NULL, NULL); |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 10145 | |
| 10146 | return work; |
| 10147 | } |
| 10148 | |
| 10149 | void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work) |
| 10150 | { |
| 10151 | wait_for_completion(&work->completion); |
David Sterba | 100d570 | 2015-12-08 14:39:32 +0100 | [diff] [blame] | 10152 | kfree(work); |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 10153 | } |
| 10154 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 10155 | /* |
| 10156 | * some fairly slow code that needs optimization. This walks the list |
| 10157 | * of all the inodes with pending delalloc and forces them to disk. |
| 10158 | */ |
Miao Xie | 6c255e6 | 2014-03-06 13:55:01 +0800 | [diff] [blame] | 10159 | static int __start_delalloc_inodes(struct btrfs_root *root, int delay_iput, |
| 10160 | int nr) |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 10161 | { |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 10162 | struct btrfs_inode *binode; |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 10163 | struct inode *inode; |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 10164 | struct btrfs_delalloc_work *work, *next; |
| 10165 | struct list_head works; |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 10166 | struct list_head splice; |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 10167 | int ret = 0; |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 10168 | |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 10169 | INIT_LIST_HEAD(&works); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 10170 | INIT_LIST_HEAD(&splice); |
Miao Xie | 63607cc | 2013-01-22 10:50:35 +0000 | [diff] [blame] | 10171 | |
Miao Xie | 573bfb7 | 2014-03-06 13:55:03 +0800 | [diff] [blame] | 10172 | mutex_lock(&root->delalloc_mutex); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 10173 | spin_lock(&root->delalloc_lock); |
| 10174 | list_splice_init(&root->delalloc_inodes, &splice); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 10175 | while (!list_empty(&splice)) { |
| 10176 | binode = list_entry(splice.next, struct btrfs_inode, |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 10177 | delalloc_inodes); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 10178 | |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 10179 | list_move_tail(&binode->delalloc_inodes, |
| 10180 | &root->delalloc_inodes); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 10181 | inode = igrab(&binode->vfs_inode); |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 10182 | if (!inode) { |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 10183 | cond_resched_lock(&root->delalloc_lock); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 10184 | continue; |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 10185 | } |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 10186 | spin_unlock(&root->delalloc_lock); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 10187 | |
David Sterba | 651d494 | 2015-11-27 19:24:16 +0100 | [diff] [blame] | 10188 | work = btrfs_alloc_delalloc_work(inode, delay_iput); |
David Sterba | 5d99a998 | 2014-09-29 19:20:37 +0200 | [diff] [blame] | 10189 | if (!work) { |
Josef Bacik | f4ab9ea | 2013-09-17 11:25:44 -0400 | [diff] [blame] | 10190 | if (delay_iput) |
| 10191 | btrfs_add_delayed_iput(inode); |
| 10192 | else |
| 10193 | iput(inode); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 10194 | ret = -ENOMEM; |
Wang Shilong | a1ecaab | 2014-04-02 19:53:32 +0800 | [diff] [blame] | 10195 | goto out; |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 10196 | } |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 10197 | list_add_tail(&work->list, &works); |
Qu Wenruo | a44903a | 2014-02-28 10:46:09 +0800 | [diff] [blame] | 10198 | btrfs_queue_work(root->fs_info->flush_workers, |
| 10199 | &work->work); |
Miao Xie | 6c255e6 | 2014-03-06 13:55:01 +0800 | [diff] [blame] | 10200 | ret++; |
| 10201 | if (nr != -1 && ret >= nr) |
Wang Shilong | a1ecaab | 2014-04-02 19:53:32 +0800 | [diff] [blame] | 10202 | goto out; |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 10203 | cond_resched(); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 10204 | spin_lock(&root->delalloc_lock); |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 10205 | } |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 10206 | spin_unlock(&root->delalloc_lock); |
Chris Mason | 8c8bee1 | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 10207 | |
Wang Shilong | a1ecaab | 2014-04-02 19:53:32 +0800 | [diff] [blame] | 10208 | out: |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 10209 | list_for_each_entry_safe(work, next, &works, list) { |
| 10210 | list_del_init(&work->list); |
| 10211 | btrfs_wait_and_free_delalloc_work(work); |
| 10212 | } |
| 10213 | |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 10214 | if (!list_empty_careful(&splice)) { |
| 10215 | spin_lock(&root->delalloc_lock); |
| 10216 | list_splice_tail(&splice, &root->delalloc_inodes); |
| 10217 | spin_unlock(&root->delalloc_lock); |
| 10218 | } |
Miao Xie | 573bfb7 | 2014-03-06 13:55:03 +0800 | [diff] [blame] | 10219 | mutex_unlock(&root->delalloc_mutex); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 10220 | return ret; |
| 10221 | } |
| 10222 | |
| 10223 | int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput) |
| 10224 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 10225 | struct btrfs_fs_info *fs_info = root->fs_info; |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 10226 | int ret; |
| 10227 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 10228 | if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 10229 | return -EROFS; |
| 10230 | |
Miao Xie | 6c255e6 | 2014-03-06 13:55:01 +0800 | [diff] [blame] | 10231 | ret = __start_delalloc_inodes(root, delay_iput, -1); |
| 10232 | if (ret > 0) |
| 10233 | ret = 0; |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 10234 | /* |
| 10235 | * the filemap_flush will queue IO into the worker threads, but |
Chris Mason | 8c8bee1 | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 10236 | * we have to make sure the IO is actually started and that |
| 10237 | * ordered extents get created before we return |
| 10238 | */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 10239 | atomic_inc(&fs_info->async_submit_draining); |
| 10240 | while (atomic_read(&fs_info->nr_async_submits) || |
| 10241 | atomic_read(&fs_info->async_delalloc_pages)) { |
| 10242 | wait_event(fs_info->async_submit_wait, |
| 10243 | (atomic_read(&fs_info->nr_async_submits) == 0 && |
| 10244 | atomic_read(&fs_info->async_delalloc_pages) == 0)); |
Chris Mason | 8c8bee1 | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 10245 | } |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 10246 | atomic_dec(&fs_info->async_submit_draining); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 10247 | return ret; |
| 10248 | } |
| 10249 | |
Miao Xie | 6c255e6 | 2014-03-06 13:55:01 +0800 | [diff] [blame] | 10250 | int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, int delay_iput, |
| 10251 | int nr) |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 10252 | { |
| 10253 | struct btrfs_root *root; |
| 10254 | struct list_head splice; |
| 10255 | int ret; |
| 10256 | |
Wang Shilong | 2c21b4d | 2014-01-14 19:42:20 +0800 | [diff] [blame] | 10257 | if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 10258 | return -EROFS; |
| 10259 | |
| 10260 | INIT_LIST_HEAD(&splice); |
| 10261 | |
Miao Xie | 573bfb7 | 2014-03-06 13:55:03 +0800 | [diff] [blame] | 10262 | mutex_lock(&fs_info->delalloc_root_mutex); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 10263 | spin_lock(&fs_info->delalloc_root_lock); |
| 10264 | list_splice_init(&fs_info->delalloc_roots, &splice); |
Miao Xie | 6c255e6 | 2014-03-06 13:55:01 +0800 | [diff] [blame] | 10265 | while (!list_empty(&splice) && nr) { |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 10266 | root = list_first_entry(&splice, struct btrfs_root, |
| 10267 | delalloc_root); |
| 10268 | root = btrfs_grab_fs_root(root); |
| 10269 | BUG_ON(!root); |
| 10270 | list_move_tail(&root->delalloc_root, |
| 10271 | &fs_info->delalloc_roots); |
| 10272 | spin_unlock(&fs_info->delalloc_root_lock); |
| 10273 | |
Miao Xie | 6c255e6 | 2014-03-06 13:55:01 +0800 | [diff] [blame] | 10274 | ret = __start_delalloc_inodes(root, delay_iput, nr); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 10275 | btrfs_put_fs_root(root); |
Miao Xie | 6c255e6 | 2014-03-06 13:55:01 +0800 | [diff] [blame] | 10276 | if (ret < 0) |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 10277 | goto out; |
| 10278 | |
Miao Xie | 6c255e6 | 2014-03-06 13:55:01 +0800 | [diff] [blame] | 10279 | if (nr != -1) { |
| 10280 | nr -= ret; |
| 10281 | WARN_ON(nr < 0); |
| 10282 | } |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 10283 | spin_lock(&fs_info->delalloc_root_lock); |
| 10284 | } |
| 10285 | spin_unlock(&fs_info->delalloc_root_lock); |
| 10286 | |
Miao Xie | 6c255e6 | 2014-03-06 13:55:01 +0800 | [diff] [blame] | 10287 | ret = 0; |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 10288 | atomic_inc(&fs_info->async_submit_draining); |
| 10289 | while (atomic_read(&fs_info->nr_async_submits) || |
| 10290 | atomic_read(&fs_info->async_delalloc_pages)) { |
| 10291 | wait_event(fs_info->async_submit_wait, |
| 10292 | (atomic_read(&fs_info->nr_async_submits) == 0 && |
| 10293 | atomic_read(&fs_info->async_delalloc_pages) == 0)); |
| 10294 | } |
| 10295 | atomic_dec(&fs_info->async_submit_draining); |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 10296 | out: |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 10297 | if (!list_empty_careful(&splice)) { |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 10298 | spin_lock(&fs_info->delalloc_root_lock); |
| 10299 | list_splice_tail(&splice, &fs_info->delalloc_roots); |
| 10300 | spin_unlock(&fs_info->delalloc_root_lock); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 10301 | } |
Miao Xie | 573bfb7 | 2014-03-06 13:55:03 +0800 | [diff] [blame] | 10302 | mutex_unlock(&fs_info->delalloc_root_mutex); |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 10303 | return ret; |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 10304 | } |
| 10305 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10306 | static int btrfs_symlink(struct inode *dir, struct dentry *dentry, |
| 10307 | const char *symname) |
| 10308 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 10309 | struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10310 | struct btrfs_trans_handle *trans; |
| 10311 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 10312 | struct btrfs_path *path; |
| 10313 | struct btrfs_key key; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 10314 | struct inode *inode = NULL; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10315 | int err; |
| 10316 | int drop_inode = 0; |
| 10317 | u64 objectid; |
Dulshani Gunawardhana | 6787125 | 2013-10-31 10:33:04 +0530 | [diff] [blame] | 10318 | u64 index = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10319 | int name_len; |
| 10320 | int datasize; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 10321 | unsigned long ptr; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10322 | struct btrfs_file_extent_item *ei; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 10323 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10324 | |
Filipe David Borba Manana | f06becc | 2013-09-16 09:53:28 +0100 | [diff] [blame] | 10325 | name_len = strlen(symname); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 10326 | if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info)) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10327 | return -ENAMETOOLONG; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 10328 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 10329 | /* |
| 10330 | * 2 items for inode item and ref |
| 10331 | * 2 items for dir items |
Filipe Manana | 9269d12 | 2015-12-31 18:16:29 +0000 | [diff] [blame] | 10332 | * 1 item for updating parent inode item |
| 10333 | * 1 item for the inline extent item |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 10334 | * 1 item for xattr if selinux is on |
| 10335 | */ |
Filipe Manana | 9269d12 | 2015-12-31 18:16:29 +0000 | [diff] [blame] | 10336 | trans = btrfs_start_transaction(root, 7); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 10337 | if (IS_ERR(trans)) |
| 10338 | return PTR_ERR(trans); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 10339 | |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 10340 | err = btrfs_find_free_ino(root, &objectid); |
| 10341 | if (err) |
| 10342 | goto out_unlock; |
| 10343 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 10344 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 10345 | dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), |
| 10346 | objectid, S_IFLNK|S_IRWXUGO, &index); |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 10347 | if (IS_ERR(inode)) { |
| 10348 | err = PTR_ERR(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10349 | goto out_unlock; |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 10350 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10351 | |
Casey Schaufler | ad19db7 | 2011-12-15 10:09:07 -0500 | [diff] [blame] | 10352 | /* |
| 10353 | * If the active LSM wants to access the inode during |
| 10354 | * d_instantiate it needs these. Smack checks to see |
| 10355 | * if the filesystem supports xattrs by looking at the |
| 10356 | * ops vector. |
| 10357 | */ |
| 10358 | inode->i_fop = &btrfs_file_operations; |
| 10359 | inode->i_op = &btrfs_file_inode_operations; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 10360 | inode->i_mapping->a_ops = &btrfs_aops; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 10361 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
| 10362 | |
| 10363 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); |
| 10364 | if (err) |
| 10365 | goto out_unlock_inode; |
Casey Schaufler | ad19db7 | 2011-12-15 10:09:07 -0500 | [diff] [blame] | 10366 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10367 | path = btrfs_alloc_path(); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 10368 | if (!path) { |
| 10369 | err = -ENOMEM; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 10370 | goto out_unlock_inode; |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 10371 | } |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 10372 | key.objectid = btrfs_ino(BTRFS_I(inode)); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10373 | key.offset = 0; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 10374 | key.type = BTRFS_EXTENT_DATA_KEY; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10375 | datasize = btrfs_file_extent_calc_inline_size(name_len); |
| 10376 | err = btrfs_insert_empty_item(trans, root, path, &key, |
| 10377 | datasize); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 10378 | if (err) { |
Julia Lawall | b083916 | 2011-05-14 07:10:51 +0000 | [diff] [blame] | 10379 | btrfs_free_path(path); |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 10380 | goto out_unlock_inode; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 10381 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 10382 | leaf = path->nodes[0]; |
| 10383 | ei = btrfs_item_ptr(leaf, path->slots[0], |
| 10384 | struct btrfs_file_extent_item); |
| 10385 | btrfs_set_file_extent_generation(leaf, ei, trans->transid); |
| 10386 | btrfs_set_file_extent_type(leaf, ei, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10387 | BTRFS_FILE_EXTENT_INLINE); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 10388 | btrfs_set_file_extent_encryption(leaf, ei, 0); |
| 10389 | btrfs_set_file_extent_compression(leaf, ei, 0); |
| 10390 | btrfs_set_file_extent_other_encoding(leaf, ei, 0); |
| 10391 | btrfs_set_file_extent_ram_bytes(leaf, ei, name_len); |
| 10392 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10393 | ptr = btrfs_file_extent_inline_start(ei); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 10394 | write_extent_buffer(leaf, symname, ptr, name_len); |
| 10395 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10396 | btrfs_free_path(path); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 10397 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10398 | inode->i_op = &btrfs_symlink_inode_operations; |
Al Viro | 21fc61c | 2015-11-17 01:07:57 -0500 | [diff] [blame] | 10399 | inode_nohighmem(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10400 | inode->i_mapping->a_ops = &btrfs_symlink_aops; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 10401 | inode_set_bytes(inode, name_len); |
Nikolay Borisov | 6ef06d2 | 2017-02-20 13:50:34 +0200 | [diff] [blame] | 10402 | btrfs_i_size_write(BTRFS_I(inode), name_len); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 10403 | err = btrfs_update_inode(trans, root, inode); |
Filipe Manana | d50866d | 2015-12-31 18:08:24 +0000 | [diff] [blame] | 10404 | /* |
| 10405 | * Last step, add directory indexes for our symlink inode. This is the |
| 10406 | * last step to avoid extra cleanup of these indexes if an error happens |
| 10407 | * elsewhere above. |
| 10408 | */ |
| 10409 | if (!err) |
Nikolay Borisov | cef415a | 2017-02-20 13:51:09 +0200 | [diff] [blame] | 10410 | err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, |
| 10411 | BTRFS_I(inode), 0, index); |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 10412 | if (err) { |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 10413 | drop_inode = 1; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 10414 | goto out_unlock_inode; |
| 10415 | } |
| 10416 | |
| 10417 | unlock_new_inode(inode); |
| 10418 | d_instantiate(dentry, inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10419 | |
| 10420 | out_unlock: |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 10421 | btrfs_end_transaction(trans); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10422 | if (drop_inode) { |
| 10423 | inode_dec_link_count(inode); |
| 10424 | iput(inode); |
| 10425 | } |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 10426 | btrfs_btree_balance_dirty(fs_info); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10427 | return err; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 10428 | |
| 10429 | out_unlock_inode: |
| 10430 | drop_inode = 1; |
| 10431 | unlock_new_inode(inode); |
| 10432 | goto out_unlock; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10433 | } |
Chris Mason | 1643298 | 2008-04-10 10:23:21 -0400 | [diff] [blame] | 10434 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 10435 | static int __btrfs_prealloc_file_range(struct inode *inode, int mode, |
| 10436 | u64 start, u64 num_bytes, u64 min_size, |
| 10437 | loff_t actual_len, u64 *alloc_hint, |
| 10438 | struct btrfs_trans_handle *trans) |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 10439 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 10440 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 10441 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
| 10442 | struct extent_map *em; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 10443 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 10444 | struct btrfs_key ins; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 10445 | u64 cur_offset = start; |
Josef Bacik | 55a61d1 | 2010-11-22 18:50:32 +0000 | [diff] [blame] | 10446 | u64 i_size; |
Chris Mason | 154ea28 | 2013-03-05 11:11:26 -0500 | [diff] [blame] | 10447 | u64 cur_bytes; |
Josef Bacik | 0b670dc | 2015-09-23 17:11:16 -0400 | [diff] [blame] | 10448 | u64 last_alloc = (u64)-1; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 10449 | int ret = 0; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 10450 | bool own_trans = true; |
Wang Xiaoguang | 1851309 | 2016-07-25 15:51:40 +0800 | [diff] [blame] | 10451 | u64 end = start + num_bytes - 1; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 10452 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 10453 | if (trans) |
| 10454 | own_trans = false; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 10455 | while (num_bytes > 0) { |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 10456 | if (own_trans) { |
| 10457 | trans = btrfs_start_transaction(root, 3); |
| 10458 | if (IS_ERR(trans)) { |
| 10459 | ret = PTR_ERR(trans); |
| 10460 | break; |
| 10461 | } |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 10462 | } |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 10463 | |
Byongho Lee | ee22184 | 2015-12-15 01:42:10 +0900 | [diff] [blame] | 10464 | cur_bytes = min_t(u64, num_bytes, SZ_256M); |
Chris Mason | 154ea28 | 2013-03-05 11:11:26 -0500 | [diff] [blame] | 10465 | cur_bytes = max(cur_bytes, min_size); |
Josef Bacik | 0b670dc | 2015-09-23 17:11:16 -0400 | [diff] [blame] | 10466 | /* |
| 10467 | * If we are severely fragmented we could end up with really |
| 10468 | * small allocations, so if the allocator is returning small |
| 10469 | * chunks lets make its job easier by only searching for those |
| 10470 | * sized chunks. |
| 10471 | */ |
| 10472 | cur_bytes = min(cur_bytes, last_alloc); |
Wang Xiaoguang | 1851309 | 2016-07-25 15:51:40 +0800 | [diff] [blame] | 10473 | ret = btrfs_reserve_extent(root, cur_bytes, cur_bytes, |
| 10474 | min_size, 0, *alloc_hint, &ins, 1, 0); |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 10475 | if (ret) { |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 10476 | if (own_trans) |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 10477 | btrfs_end_transaction(trans); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 10478 | break; |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 10479 | } |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 10480 | btrfs_dec_block_group_reservations(fs_info, ins.objectid); |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 10481 | |
Josef Bacik | 0b670dc | 2015-09-23 17:11:16 -0400 | [diff] [blame] | 10482 | last_alloc = ins.offset; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 10483 | ret = insert_reserved_file_extent(trans, inode, |
| 10484 | cur_offset, ins.objectid, |
| 10485 | ins.offset, ins.offset, |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 10486 | ins.offset, 0, 0, 0, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 10487 | BTRFS_FILE_EXTENT_PREALLOC); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 10488 | if (ret) { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 10489 | btrfs_free_reserved_extent(fs_info, ins.objectid, |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 10490 | ins.offset, 0); |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 10491 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 10492 | if (own_trans) |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 10493 | btrfs_end_transaction(trans); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 10494 | break; |
| 10495 | } |
Dongsheng Yang | 3119321 | 2014-12-12 16:44:35 +0800 | [diff] [blame] | 10496 | |
Nikolay Borisov | dcdbc05 | 2017-02-20 13:50:45 +0200 | [diff] [blame] | 10497 | btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset, |
Chris Mason | a1ed835 | 2009-09-11 12:27:37 -0400 | [diff] [blame] | 10498 | cur_offset + ins.offset -1, 0); |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 10499 | |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 10500 | em = alloc_extent_map(); |
| 10501 | if (!em) { |
| 10502 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, |
| 10503 | &BTRFS_I(inode)->runtime_flags); |
| 10504 | goto next; |
| 10505 | } |
| 10506 | |
| 10507 | em->start = cur_offset; |
| 10508 | em->orig_start = cur_offset; |
| 10509 | em->len = ins.offset; |
| 10510 | em->block_start = ins.objectid; |
| 10511 | em->block_len = ins.offset; |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 10512 | em->orig_block_len = ins.offset; |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 10513 | em->ram_bytes = ins.offset; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 10514 | em->bdev = fs_info->fs_devices->latest_bdev; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 10515 | set_bit(EXTENT_FLAG_PREALLOC, &em->flags); |
| 10516 | em->generation = trans->transid; |
| 10517 | |
| 10518 | while (1) { |
| 10519 | write_lock(&em_tree->lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 10520 | ret = add_extent_mapping(em_tree, em, 1); |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 10521 | write_unlock(&em_tree->lock); |
| 10522 | if (ret != -EEXIST) |
| 10523 | break; |
Nikolay Borisov | dcdbc05 | 2017-02-20 13:50:45 +0200 | [diff] [blame] | 10524 | btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset, |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 10525 | cur_offset + ins.offset - 1, |
| 10526 | 0); |
| 10527 | } |
| 10528 | free_extent_map(em); |
| 10529 | next: |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 10530 | num_bytes -= ins.offset; |
| 10531 | cur_offset += ins.offset; |
Yan, Zheng | efa5646 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 10532 | *alloc_hint = ins.objectid + ins.offset; |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 10533 | |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 10534 | inode_inc_iversion(inode); |
Deepa Dinamani | c2050a4 | 2016-09-14 07:48:06 -0700 | [diff] [blame] | 10535 | inode->i_ctime = current_time(inode); |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 10536 | BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 10537 | if (!(mode & FALLOC_FL_KEEP_SIZE) && |
Yan, Zheng | efa5646 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 10538 | (actual_len > inode->i_size) && |
| 10539 | (cur_offset > inode->i_size)) { |
Aneesh Kumar K.V | d1ea6a6 | 2010-01-20 07:28:54 +0000 | [diff] [blame] | 10540 | if (cur_offset > actual_len) |
Josef Bacik | 55a61d1 | 2010-11-22 18:50:32 +0000 | [diff] [blame] | 10541 | i_size = actual_len; |
Aneesh Kumar K.V | d1ea6a6 | 2010-01-20 07:28:54 +0000 | [diff] [blame] | 10542 | else |
Josef Bacik | 55a61d1 | 2010-11-22 18:50:32 +0000 | [diff] [blame] | 10543 | i_size = cur_offset; |
| 10544 | i_size_write(inode, i_size); |
| 10545 | btrfs_ordered_update_i_size(inode, i_size, NULL); |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 10546 | } |
| 10547 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 10548 | ret = btrfs_update_inode(trans, root, inode); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 10549 | |
| 10550 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 10551 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 10552 | if (own_trans) |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 10553 | btrfs_end_transaction(trans); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 10554 | break; |
| 10555 | } |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 10556 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 10557 | if (own_trans) |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 10558 | btrfs_end_transaction(trans); |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 10559 | } |
Wang Xiaoguang | 1851309 | 2016-07-25 15:51:40 +0800 | [diff] [blame] | 10560 | if (cur_offset < end) |
Qu Wenruo | bc42bda | 2017-02-27 15:10:39 +0800 | [diff] [blame] | 10561 | btrfs_free_reserved_data_space(inode, NULL, cur_offset, |
Wang Xiaoguang | 1851309 | 2016-07-25 15:51:40 +0800 | [diff] [blame] | 10562 | end - cur_offset + 1); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 10563 | return ret; |
| 10564 | } |
| 10565 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 10566 | int btrfs_prealloc_file_range(struct inode *inode, int mode, |
| 10567 | u64 start, u64 num_bytes, u64 min_size, |
| 10568 | loff_t actual_len, u64 *alloc_hint) |
| 10569 | { |
| 10570 | return __btrfs_prealloc_file_range(inode, mode, start, num_bytes, |
| 10571 | min_size, actual_len, alloc_hint, |
| 10572 | NULL); |
| 10573 | } |
| 10574 | |
| 10575 | int btrfs_prealloc_file_range_trans(struct inode *inode, |
| 10576 | struct btrfs_trans_handle *trans, int mode, |
| 10577 | u64 start, u64 num_bytes, u64 min_size, |
| 10578 | loff_t actual_len, u64 *alloc_hint) |
| 10579 | { |
| 10580 | return __btrfs_prealloc_file_range(inode, mode, start, num_bytes, |
| 10581 | min_size, actual_len, alloc_hint, trans); |
| 10582 | } |
| 10583 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 10584 | static int btrfs_set_page_dirty(struct page *page) |
| 10585 | { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 10586 | return __set_page_dirty_nobuffers(page); |
| 10587 | } |
| 10588 | |
Al Viro | 10556cb | 2011-06-20 19:28:19 -0400 | [diff] [blame] | 10589 | static int btrfs_permission(struct inode *inode, int mask) |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 10590 | { |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 10591 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Jeff Mahoney | cb6db4e | 2011-08-15 17:27:21 +0000 | [diff] [blame] | 10592 | umode_t mode = inode->i_mode; |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 10593 | |
Jeff Mahoney | cb6db4e | 2011-08-15 17:27:21 +0000 | [diff] [blame] | 10594 | if (mask & MAY_WRITE && |
| 10595 | (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) { |
| 10596 | if (btrfs_root_readonly(root)) |
| 10597 | return -EROFS; |
| 10598 | if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY) |
| 10599 | return -EACCES; |
| 10600 | } |
Al Viro | 2830ba7 | 2011-06-20 19:16:29 -0400 | [diff] [blame] | 10601 | return generic_permission(inode, mask); |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 10602 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10603 | |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 10604 | static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode) |
| 10605 | { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 10606 | struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 10607 | struct btrfs_trans_handle *trans; |
| 10608 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 10609 | struct inode *inode = NULL; |
| 10610 | u64 objectid; |
| 10611 | u64 index; |
| 10612 | int ret = 0; |
| 10613 | |
| 10614 | /* |
| 10615 | * 5 units required for adding orphan entry |
| 10616 | */ |
| 10617 | trans = btrfs_start_transaction(root, 5); |
| 10618 | if (IS_ERR(trans)) |
| 10619 | return PTR_ERR(trans); |
| 10620 | |
| 10621 | ret = btrfs_find_free_ino(root, &objectid); |
| 10622 | if (ret) |
| 10623 | goto out; |
| 10624 | |
| 10625 | inode = btrfs_new_inode(trans, root, dir, NULL, 0, |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 10626 | btrfs_ino(BTRFS_I(dir)), objectid, mode, &index); |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 10627 | if (IS_ERR(inode)) { |
| 10628 | ret = PTR_ERR(inode); |
| 10629 | inode = NULL; |
| 10630 | goto out; |
| 10631 | } |
| 10632 | |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 10633 | inode->i_fop = &btrfs_file_operations; |
| 10634 | inode->i_op = &btrfs_file_inode_operations; |
| 10635 | |
| 10636 | inode->i_mapping->a_ops = &btrfs_aops; |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 10637 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
| 10638 | |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 10639 | ret = btrfs_init_inode_security(trans, inode, dir, NULL); |
| 10640 | if (ret) |
| 10641 | goto out_inode; |
| 10642 | |
| 10643 | ret = btrfs_update_inode(trans, root, inode); |
| 10644 | if (ret) |
| 10645 | goto out_inode; |
Nikolay Borisov | 73f2e54 | 2017-02-20 13:50:59 +0200 | [diff] [blame] | 10646 | ret = btrfs_orphan_add(trans, BTRFS_I(inode)); |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 10647 | if (ret) |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 10648 | goto out_inode; |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 10649 | |
Filipe Manana | 5762b5c | 2014-08-01 00:10:32 +0100 | [diff] [blame] | 10650 | /* |
| 10651 | * We set number of links to 0 in btrfs_new_inode(), and here we set |
| 10652 | * it to 1 because d_tmpfile() will issue a warning if the count is 0, |
| 10653 | * through: |
| 10654 | * |
| 10655 | * d_tmpfile() -> inode_dec_link_count() -> drop_nlink() |
| 10656 | */ |
| 10657 | set_nlink(inode, 1); |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 10658 | unlock_new_inode(inode); |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 10659 | d_tmpfile(dentry, inode); |
| 10660 | mark_inode_dirty(inode); |
| 10661 | |
| 10662 | out: |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 10663 | btrfs_end_transaction(trans); |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 10664 | if (ret) |
| 10665 | iput(inode); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 10666 | btrfs_balance_delayed_items(fs_info); |
| 10667 | btrfs_btree_balance_dirty(fs_info); |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 10668 | return ret; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 10669 | |
| 10670 | out_inode: |
| 10671 | unlock_new_inode(inode); |
| 10672 | goto out; |
| 10673 | |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 10674 | } |
| 10675 | |
David Sterba | 20a7db8 | 2017-02-17 16:24:29 +0100 | [diff] [blame] | 10676 | __attribute__((const)) |
Liu Bo | 9d0d1c8 | 2017-03-24 15:04:50 -0700 | [diff] [blame] | 10677 | static int btrfs_readpage_io_failed_hook(struct page *page, int failed_mirror) |
David Sterba | 20a7db8 | 2017-02-17 16:24:29 +0100 | [diff] [blame] | 10678 | { |
Liu Bo | 9d0d1c8 | 2017-03-24 15:04:50 -0700 | [diff] [blame] | 10679 | return -EAGAIN; |
David Sterba | 20a7db8 | 2017-02-17 16:24:29 +0100 | [diff] [blame] | 10680 | } |
| 10681 | |
Josef Bacik | c6100a4 | 2017-05-05 11:57:13 -0400 | [diff] [blame] | 10682 | static struct btrfs_fs_info *iotree_fs_info(void *private_data) |
| 10683 | { |
| 10684 | struct inode *inode = private_data; |
| 10685 | return btrfs_sb(inode->i_sb); |
| 10686 | } |
| 10687 | |
| 10688 | static void btrfs_check_extent_io_range(void *private_data, const char *caller, |
| 10689 | u64 start, u64 end) |
| 10690 | { |
| 10691 | struct inode *inode = private_data; |
| 10692 | u64 isize; |
| 10693 | |
| 10694 | isize = i_size_read(inode); |
| 10695 | if (end >= PAGE_SIZE && (end % 2) == 0 && end != isize - 1) { |
| 10696 | btrfs_debug_rl(BTRFS_I(inode)->root->fs_info, |
| 10697 | "%s: ino %llu isize %llu odd range [%llu,%llu]", |
| 10698 | caller, btrfs_ino(BTRFS_I(inode)), isize, start, end); |
| 10699 | } |
| 10700 | } |
| 10701 | |
| 10702 | void btrfs_set_range_writeback(void *private_data, u64 start, u64 end) |
| 10703 | { |
| 10704 | struct inode *inode = private_data; |
| 10705 | unsigned long index = start >> PAGE_SHIFT; |
| 10706 | unsigned long end_index = end >> PAGE_SHIFT; |
| 10707 | struct page *page; |
| 10708 | |
| 10709 | while (index <= end_index) { |
| 10710 | page = find_get_page(inode->i_mapping, index); |
| 10711 | ASSERT(page); /* Pages should be in the extent_io_tree */ |
| 10712 | set_page_writeback(page); |
| 10713 | put_page(page); |
| 10714 | index++; |
| 10715 | } |
| 10716 | } |
| 10717 | |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 10718 | static const struct inode_operations btrfs_dir_inode_operations = { |
Chris Mason | 3394e16 | 2008-11-17 20:42:26 -0500 | [diff] [blame] | 10719 | .getattr = btrfs_getattr, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10720 | .lookup = btrfs_lookup, |
| 10721 | .create = btrfs_create, |
| 10722 | .unlink = btrfs_unlink, |
| 10723 | .link = btrfs_link, |
| 10724 | .mkdir = btrfs_mkdir, |
| 10725 | .rmdir = btrfs_rmdir, |
Miklos Szeredi | 2773bf0 | 2016-09-27 11:03:58 +0200 | [diff] [blame] | 10726 | .rename = btrfs_rename2, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10727 | .symlink = btrfs_symlink, |
| 10728 | .setattr = btrfs_setattr, |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 10729 | .mknod = btrfs_mknod, |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 10730 | .listxattr = btrfs_listxattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 10731 | .permission = btrfs_permission, |
Christoph Hellwig | 4e34e71 | 2011-07-23 17:37:31 +0200 | [diff] [blame] | 10732 | .get_acl = btrfs_get_acl, |
Christoph Hellwig | 996a710 | 2013-12-20 05:16:43 -0800 | [diff] [blame] | 10733 | .set_acl = btrfs_set_acl, |
Guangyu Sun | 93fd63c | 2013-09-16 10:42:03 -0700 | [diff] [blame] | 10734 | .update_time = btrfs_update_time, |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 10735 | .tmpfile = btrfs_tmpfile, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10736 | }; |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 10737 | static const struct inode_operations btrfs_dir_ro_inode_operations = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10738 | .lookup = btrfs_lookup, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 10739 | .permission = btrfs_permission, |
Guangyu Sun | 93fd63c | 2013-09-16 10:42:03 -0700 | [diff] [blame] | 10740 | .update_time = btrfs_update_time, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10741 | }; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 10742 | |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 10743 | static const struct file_operations btrfs_dir_file_operations = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10744 | .llseek = generic_file_llseek, |
| 10745 | .read = generic_read_dir, |
Omar Sandoval | 02dbfc9 | 2016-05-20 13:50:33 -0700 | [diff] [blame] | 10746 | .iterate_shared = btrfs_real_readdir, |
Christoph Hellwig | 34287aa | 2007-09-14 10:22:47 -0400 | [diff] [blame] | 10747 | .unlocked_ioctl = btrfs_ioctl, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10748 | #ifdef CONFIG_COMPAT |
Luke Dashjr | 4c63c24 | 2015-10-29 08:22:21 +0000 | [diff] [blame] | 10749 | .compat_ioctl = btrfs_compat_ioctl, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10750 | #endif |
Sage Weil | 6bf13c0 | 2008-06-10 10:07:39 -0400 | [diff] [blame] | 10751 | .release = btrfs_release_file, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 10752 | .fsync = btrfs_sync_file, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10753 | }; |
| 10754 | |
David Sterba | 20e5506 | 2015-11-19 11:42:28 +0100 | [diff] [blame] | 10755 | static const struct extent_io_ops btrfs_extent_io_ops = { |
David Sterba | 4d53ddd | 2017-02-17 15:27:44 +0100 | [diff] [blame] | 10756 | /* mandatory callbacks */ |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 10757 | .submit_bio_hook = btrfs_submit_bio_hook, |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 10758 | .readpage_end_io_hook = btrfs_readpage_end_io_hook, |
David Sterba | 4d53ddd | 2017-02-17 15:27:44 +0100 | [diff] [blame] | 10759 | .merge_bio_hook = btrfs_merge_bio_hook, |
Liu Bo | 9d0d1c8 | 2017-03-24 15:04:50 -0700 | [diff] [blame] | 10760 | .readpage_io_failed_hook = btrfs_readpage_io_failed_hook, |
Josef Bacik | c6100a4 | 2017-05-05 11:57:13 -0400 | [diff] [blame] | 10761 | .tree_fs_info = iotree_fs_info, |
| 10762 | .set_range_writeback = btrfs_set_range_writeback, |
David Sterba | 4d53ddd | 2017-02-17 15:27:44 +0100 | [diff] [blame] | 10763 | |
| 10764 | /* optional callbacks */ |
| 10765 | .fill_delalloc = run_delalloc_range, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 10766 | .writepage_end_io_hook = btrfs_writepage_end_io_hook, |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 10767 | .writepage_start_hook = btrfs_writepage_start_hook, |
Chris Mason | b0c68f8 | 2008-01-31 11:05:37 -0500 | [diff] [blame] | 10768 | .set_bit_hook = btrfs_set_bit_hook, |
| 10769 | .clear_bit_hook = btrfs_clear_bit_hook, |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 10770 | .merge_extent_hook = btrfs_merge_extent_hook, |
| 10771 | .split_extent_hook = btrfs_split_extent_hook, |
Josef Bacik | c6100a4 | 2017-05-05 11:57:13 -0400 | [diff] [blame] | 10772 | .check_extent_io_range = btrfs_check_extent_io_range, |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 10773 | }; |
| 10774 | |
Chris Mason | 3505439 | 2009-01-21 13:11:13 -0500 | [diff] [blame] | 10775 | /* |
| 10776 | * btrfs doesn't support the bmap operation because swapfiles |
| 10777 | * use bmap to make a mapping of extents in the file. They assume |
| 10778 | * these extents won't change over the life of the file and they |
| 10779 | * use the bmap result to do IO directly to the drive. |
| 10780 | * |
| 10781 | * the btrfs bmap call would return logical addresses that aren't |
| 10782 | * suitable for IO and they also will change frequently as COW |
| 10783 | * operations happen. So, swapfile + btrfs == corruption. |
| 10784 | * |
| 10785 | * For now we're avoiding this by dropping bmap. |
| 10786 | */ |
Alexey Dobriyan | 7f09410 | 2009-09-21 17:01:10 -0700 | [diff] [blame] | 10787 | static const struct address_space_operations btrfs_aops = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10788 | .readpage = btrfs_readpage, |
| 10789 | .writepage = btrfs_writepage, |
Chris Mason | b293f02e | 2007-11-01 19:45:34 -0400 | [diff] [blame] | 10790 | .writepages = btrfs_writepages, |
Chris Mason | 3ab2fb5 | 2007-11-08 10:59:22 -0500 | [diff] [blame] | 10791 | .readpages = btrfs_readpages, |
Chris Mason | 1643298 | 2008-04-10 10:23:21 -0400 | [diff] [blame] | 10792 | .direct_IO = btrfs_direct_IO, |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 10793 | .invalidatepage = btrfs_invalidatepage, |
| 10794 | .releasepage = btrfs_releasepage, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 10795 | .set_page_dirty = btrfs_set_page_dirty, |
Andi Kleen | 465fdd9 | 2009-09-16 11:50:18 +0200 | [diff] [blame] | 10796 | .error_remove_page = generic_error_remove_page, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10797 | }; |
| 10798 | |
Alexey Dobriyan | 7f09410 | 2009-09-21 17:01:10 -0700 | [diff] [blame] | 10799 | static const struct address_space_operations btrfs_symlink_aops = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10800 | .readpage = btrfs_readpage, |
| 10801 | .writepage = btrfs_writepage, |
Chris Mason | 2bf5a72 | 2007-08-30 11:54:02 -0400 | [diff] [blame] | 10802 | .invalidatepage = btrfs_invalidatepage, |
| 10803 | .releasepage = btrfs_releasepage, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10804 | }; |
| 10805 | |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 10806 | static const struct inode_operations btrfs_file_inode_operations = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10807 | .getattr = btrfs_getattr, |
| 10808 | .setattr = btrfs_setattr, |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 10809 | .listxattr = btrfs_listxattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 10810 | .permission = btrfs_permission, |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 10811 | .fiemap = btrfs_fiemap, |
Christoph Hellwig | 4e34e71 | 2011-07-23 17:37:31 +0200 | [diff] [blame] | 10812 | .get_acl = btrfs_get_acl, |
Christoph Hellwig | 996a710 | 2013-12-20 05:16:43 -0800 | [diff] [blame] | 10813 | .set_acl = btrfs_set_acl, |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 10814 | .update_time = btrfs_update_time, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10815 | }; |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 10816 | static const struct inode_operations btrfs_special_inode_operations = { |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 10817 | .getattr = btrfs_getattr, |
| 10818 | .setattr = btrfs_setattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 10819 | .permission = btrfs_permission, |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 10820 | .listxattr = btrfs_listxattr, |
Christoph Hellwig | 4e34e71 | 2011-07-23 17:37:31 +0200 | [diff] [blame] | 10821 | .get_acl = btrfs_get_acl, |
Christoph Hellwig | 996a710 | 2013-12-20 05:16:43 -0800 | [diff] [blame] | 10822 | .set_acl = btrfs_set_acl, |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 10823 | .update_time = btrfs_update_time, |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 10824 | }; |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 10825 | static const struct inode_operations btrfs_symlink_inode_operations = { |
Al Viro | 6b25539 | 2015-11-17 10:20:54 -0500 | [diff] [blame] | 10826 | .get_link = page_get_link, |
Li Zefan | f209561 | 2010-11-19 02:05:24 +0000 | [diff] [blame] | 10827 | .getattr = btrfs_getattr, |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 10828 | .setattr = btrfs_setattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 10829 | .permission = btrfs_permission, |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 10830 | .listxattr = btrfs_listxattr, |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 10831 | .update_time = btrfs_update_time, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10832 | }; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 10833 | |
Alexey Dobriyan | 82d339d | 2009-10-09 09:54:36 -0400 | [diff] [blame] | 10834 | const struct dentry_operations btrfs_dentry_operations = { |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 10835 | .d_delete = btrfs_dentry_delete, |
Josef Bacik | b4aff1f | 2011-06-28 16:18:59 -0400 | [diff] [blame] | 10836 | .d_release = btrfs_dentry_release, |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 10837 | }; |