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; |
| 89 | struct kmem_cache *btrfs_transaction_cachep; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 90 | struct kmem_cache *btrfs_path_cachep; |
Josef Bacik | dc89e98 | 2011-01-28 17:05:48 -0500 | [diff] [blame] | 91 | struct kmem_cache *btrfs_free_space_cachep; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 92 | |
| 93 | #define S_SHIFT 12 |
David Sterba | 4d4ab6d | 2015-11-19 11:42:31 +0100 | [diff] [blame] | 94 | static const unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 95 | [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE, |
| 96 | [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR, |
| 97 | [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV, |
| 98 | [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV, |
| 99 | [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO, |
| 100 | [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK, |
| 101 | [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK, |
| 102 | }; |
| 103 | |
Eric Sandeen | 3972f26 | 2013-01-12 02:57:22 +0000 | [diff] [blame] | 104 | static int btrfs_setsize(struct inode *inode, struct iattr *attr); |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 105 | static int btrfs_truncate(struct inode *inode); |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 106 | static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 107 | static noinline int cow_file_range(struct inode *inode, |
| 108 | struct page *locked_page, |
Wang Xiaoguang | dda3245 | 2016-07-11 11:05:29 +0800 | [diff] [blame] | 109 | u64 start, u64 end, u64 delalloc_end, |
| 110 | int *page_started, unsigned long *nr_written, |
| 111 | int unlock, struct btrfs_dedupe_hash *hash); |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 112 | static struct extent_map *create_io_em(struct inode *inode, u64 start, u64 len, |
| 113 | u64 orig_start, u64 block_start, |
| 114 | u64 block_len, u64 orig_block_len, |
| 115 | u64 ram_bytes, int compress_type, |
| 116 | int type); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 117 | |
Qu Wenruo | 52427260 | 2017-03-08 10:25:52 +0800 | [diff] [blame] | 118 | static void __endio_write_update_ordered(struct inode *inode, |
| 119 | const u64 offset, const u64 bytes, |
| 120 | const bool uptodate); |
| 121 | |
| 122 | /* |
| 123 | * Cleanup all submitted ordered extents in specified range to handle errors |
| 124 | * from the fill_dellaloc() callback. |
| 125 | * |
| 126 | * NOTE: caller must ensure that when an error happens, it can not call |
| 127 | * extent_clear_unlock_delalloc() to clear both the bits EXTENT_DO_ACCOUNTING |
| 128 | * and EXTENT_DELALLOC simultaneously, because that causes the reserved metadata |
| 129 | * to be released, which we want to happen only when finishing the ordered |
| 130 | * extent (btrfs_finish_ordered_io()). Also note that the caller of the |
| 131 | * fill_delalloc() callback already does proper cleanup for the first page of |
| 132 | * the range, that is, it invokes the callback writepage_end_io_hook() for the |
| 133 | * range of the first page. |
| 134 | */ |
| 135 | static inline void btrfs_cleanup_ordered_extents(struct inode *inode, |
| 136 | const u64 offset, |
| 137 | const u64 bytes) |
| 138 | { |
| 139 | return __endio_write_update_ordered(inode, offset + PAGE_SIZE, |
| 140 | bytes - PAGE_SIZE, false); |
| 141 | } |
| 142 | |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 143 | static int btrfs_dirty_inode(struct inode *inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 144 | |
Josef Bacik | 6a3891c | 2015-03-16 17:38:52 -0400 | [diff] [blame] | 145 | #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS |
| 146 | void btrfs_test_inode_set_ops(struct inode *inode) |
| 147 | { |
| 148 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
| 149 | } |
| 150 | #endif |
| 151 | |
Yan, Zheng | f34f57a | 2009-11-12 09:35:27 +0000 | [diff] [blame] | 152 | static int btrfs_init_inode_security(struct btrfs_trans_handle *trans, |
Eric Paris | 2a7dba3 | 2011-02-01 11:05:39 -0500 | [diff] [blame] | 153 | struct inode *inode, struct inode *dir, |
| 154 | const struct qstr *qstr) |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 155 | { |
| 156 | int err; |
| 157 | |
Yan, Zheng | f34f57a | 2009-11-12 09:35:27 +0000 | [diff] [blame] | 158 | err = btrfs_init_acl(trans, inode, dir); |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 159 | if (!err) |
Eric Paris | 2a7dba3 | 2011-02-01 11:05:39 -0500 | [diff] [blame] | 160 | err = btrfs_xattr_security_init(trans, inode, dir, qstr); |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 161 | return err; |
| 162 | } |
| 163 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 164 | /* |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 165 | * this does all the hard work for inserting an inline extent into |
| 166 | * the btree. The caller should have done a btrfs_drop_extents so that |
| 167 | * no overlapping inline items exist in the btree |
| 168 | */ |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 169 | static int insert_inline_extent(struct btrfs_trans_handle *trans, |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 170 | struct btrfs_path *path, int extent_inserted, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 171 | struct btrfs_root *root, struct inode *inode, |
| 172 | u64 start, size_t size, size_t compressed_size, |
Li Zefan | fe3f566 | 2011-03-28 08:30:38 +0000 | [diff] [blame] | 173 | int compress_type, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 174 | struct page **compressed_pages) |
| 175 | { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 176 | struct extent_buffer *leaf; |
| 177 | struct page *page = NULL; |
| 178 | char *kaddr; |
| 179 | unsigned long ptr; |
| 180 | struct btrfs_file_extent_item *ei; |
| 181 | int err = 0; |
| 182 | int ret; |
| 183 | size_t cur_size = size; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 184 | unsigned long offset; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 185 | |
Li Zefan | fe3f566 | 2011-03-28 08:30:38 +0000 | [diff] [blame] | 186 | if (compressed_size && compressed_pages) |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 187 | cur_size = compressed_size; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 188 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 189 | inode_add_bytes(inode, size); |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 190 | |
| 191 | if (!extent_inserted) { |
| 192 | struct btrfs_key key; |
| 193 | size_t datasize; |
| 194 | |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 195 | key.objectid = btrfs_ino(BTRFS_I(inode)); |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 196 | key.offset = start; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 197 | key.type = BTRFS_EXTENT_DATA_KEY; |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 198 | |
| 199 | datasize = btrfs_file_extent_calc_inline_size(cur_size); |
| 200 | path->leave_spinning = 1; |
| 201 | ret = btrfs_insert_empty_item(trans, root, path, &key, |
| 202 | datasize); |
| 203 | if (ret) { |
| 204 | err = ret; |
| 205 | goto fail; |
| 206 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 207 | } |
| 208 | leaf = path->nodes[0]; |
| 209 | ei = btrfs_item_ptr(leaf, path->slots[0], |
| 210 | struct btrfs_file_extent_item); |
| 211 | btrfs_set_file_extent_generation(leaf, ei, trans->transid); |
| 212 | btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE); |
| 213 | btrfs_set_file_extent_encryption(leaf, ei, 0); |
| 214 | btrfs_set_file_extent_other_encoding(leaf, ei, 0); |
| 215 | btrfs_set_file_extent_ram_bytes(leaf, ei, size); |
| 216 | ptr = btrfs_file_extent_inline_start(ei); |
| 217 | |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 218 | if (compress_type != BTRFS_COMPRESS_NONE) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 219 | struct page *cpage; |
| 220 | int i = 0; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 221 | while (compressed_size > 0) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 222 | cpage = compressed_pages[i]; |
Chris Mason | 5b050f0 | 2008-11-11 09:34:41 -0500 | [diff] [blame] | 223 | cur_size = min_t(unsigned long, compressed_size, |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 224 | PAGE_SIZE); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 225 | |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 226 | kaddr = kmap_atomic(cpage); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 227 | write_extent_buffer(leaf, kaddr, ptr, cur_size); |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 228 | kunmap_atomic(kaddr); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 229 | |
| 230 | i++; |
| 231 | ptr += cur_size; |
| 232 | compressed_size -= cur_size; |
| 233 | } |
| 234 | btrfs_set_file_extent_compression(leaf, ei, |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 235 | compress_type); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 236 | } else { |
| 237 | page = find_get_page(inode->i_mapping, |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 238 | start >> PAGE_SHIFT); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 239 | btrfs_set_file_extent_compression(leaf, ei, 0); |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 240 | kaddr = kmap_atomic(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 241 | offset = start & (PAGE_SIZE - 1); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 242 | write_extent_buffer(leaf, kaddr + offset, ptr, size); |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 243 | kunmap_atomic(kaddr); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 244 | put_page(page); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 245 | } |
| 246 | btrfs_mark_buffer_dirty(leaf); |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 247 | btrfs_release_path(path); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 248 | |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 249 | /* |
| 250 | * we're an inline extent, so nobody can |
| 251 | * extend the file past i_size without locking |
| 252 | * a page we already have locked. |
| 253 | * |
| 254 | * We must do any isize and inode updates |
| 255 | * before we unlock the pages. Otherwise we |
| 256 | * could end up racing with unlink. |
| 257 | */ |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 258 | BTRFS_I(inode)->disk_i_size = inode->i_size; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 259 | ret = btrfs_update_inode(trans, root, inode); |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 260 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 261 | return ret; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 262 | fail: |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 263 | return err; |
| 264 | } |
| 265 | |
| 266 | |
| 267 | /* |
| 268 | * conditionally insert an inline extent into the file. This |
| 269 | * does the checks required to make sure the data is small enough |
| 270 | * to fit as an inline extent. |
| 271 | */ |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 272 | static noinline int cow_file_range_inline(struct btrfs_root *root, |
| 273 | struct inode *inode, u64 start, |
| 274 | u64 end, size_t compressed_size, |
| 275 | int compress_type, |
| 276 | struct page **compressed_pages) |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 277 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 278 | struct btrfs_fs_info *fs_info = root->fs_info; |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 279 | struct btrfs_trans_handle *trans; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 280 | u64 isize = i_size_read(inode); |
| 281 | u64 actual_end = min(end + 1, isize); |
| 282 | u64 inline_len = actual_end - start; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 283 | u64 aligned_end = ALIGN(end, fs_info->sectorsize); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 284 | u64 data_len = inline_len; |
| 285 | int ret; |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 286 | struct btrfs_path *path; |
| 287 | int extent_inserted = 0; |
| 288 | u32 extent_item_size; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 289 | |
| 290 | if (compressed_size) |
| 291 | data_len = compressed_size; |
| 292 | |
| 293 | if (start > 0 || |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 294 | actual_end > fs_info->sectorsize || |
| 295 | data_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info) || |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 296 | (!compressed_size && |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 297 | (actual_end & (fs_info->sectorsize - 1)) == 0) || |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 298 | end + 1 < isize || |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 299 | data_len > fs_info->max_inline) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 300 | return 1; |
| 301 | } |
| 302 | |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 303 | path = btrfs_alloc_path(); |
| 304 | if (!path) |
| 305 | return -ENOMEM; |
| 306 | |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 307 | trans = btrfs_join_transaction(root); |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 308 | if (IS_ERR(trans)) { |
| 309 | btrfs_free_path(path); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 310 | return PTR_ERR(trans); |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 311 | } |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 312 | trans->block_rsv = &fs_info->delalloc_block_rsv; |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 313 | |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 314 | if (compressed_size && compressed_pages) |
| 315 | extent_item_size = btrfs_file_extent_calc_inline_size( |
| 316 | compressed_size); |
| 317 | else |
| 318 | extent_item_size = btrfs_file_extent_calc_inline_size( |
| 319 | inline_len); |
| 320 | |
| 321 | ret = __btrfs_drop_extents(trans, root, inode, path, |
| 322 | start, aligned_end, NULL, |
| 323 | 1, 1, extent_item_size, &extent_inserted); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 324 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 325 | btrfs_abort_transaction(trans, ret); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 326 | goto out; |
| 327 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 328 | |
| 329 | if (isize > actual_end) |
| 330 | inline_len = min_t(u64, isize, actual_end); |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 331 | ret = insert_inline_extent(trans, path, extent_inserted, |
| 332 | root, inode, start, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 333 | inline_len, compressed_size, |
Li Zefan | fe3f566 | 2011-03-28 08:30:38 +0000 | [diff] [blame] | 334 | compress_type, compressed_pages); |
Josef Bacik | 2adcac1 | 2012-05-23 16:10:14 -0400 | [diff] [blame] | 335 | if (ret && ret != -ENOSPC) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 336 | btrfs_abort_transaction(trans, ret); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 337 | goto out; |
Josef Bacik | 2adcac1 | 2012-05-23 16:10:14 -0400 | [diff] [blame] | 338 | } else if (ret == -ENOSPC) { |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 339 | ret = 1; |
| 340 | goto out; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 341 | } |
Josef Bacik | 2adcac1 | 2012-05-23 16:10:14 -0400 | [diff] [blame] | 342 | |
Josef Bacik | bdc20e6 | 2013-02-28 13:23:38 -0500 | [diff] [blame] | 343 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags); |
Nikolay Borisov | 691fa05 | 2017-02-20 13:50:42 +0200 | [diff] [blame] | 344 | btrfs_delalloc_release_metadata(BTRFS_I(inode), end + 1 - start); |
Nikolay Borisov | dcdbc05 | 2017-02-20 13:50:45 +0200 | [diff] [blame] | 345 | btrfs_drop_extent_cache(BTRFS_I(inode), start, aligned_end - 1, 0); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 346 | out: |
Qu Wenruo | 94ed938 | 2015-09-08 17:25:56 +0800 | [diff] [blame] | 347 | /* |
| 348 | * Don't forget to free the reserved space, as for inlined extent |
| 349 | * it won't count as data extent, free them directly here. |
| 350 | * And at reserve time, it's always aligned to page size, so |
| 351 | * just free one page here. |
| 352 | */ |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 353 | btrfs_qgroup_free_data(inode, 0, PAGE_SIZE); |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 354 | btrfs_free_path(path); |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 355 | btrfs_end_transaction(trans); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 356 | return ret; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 357 | } |
| 358 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 359 | struct async_extent { |
| 360 | u64 start; |
| 361 | u64 ram_size; |
| 362 | u64 compressed_size; |
| 363 | struct page **pages; |
| 364 | unsigned long nr_pages; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 365 | int compress_type; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 366 | struct list_head list; |
| 367 | }; |
| 368 | |
| 369 | struct async_cow { |
| 370 | struct inode *inode; |
| 371 | struct btrfs_root *root; |
| 372 | struct page *locked_page; |
| 373 | u64 start; |
| 374 | u64 end; |
| 375 | struct list_head extents; |
| 376 | struct btrfs_work work; |
| 377 | }; |
| 378 | |
| 379 | static noinline int add_async_extent(struct async_cow *cow, |
| 380 | u64 start, u64 ram_size, |
| 381 | u64 compressed_size, |
| 382 | struct page **pages, |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 383 | unsigned long nr_pages, |
| 384 | int compress_type) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 385 | { |
| 386 | struct async_extent *async_extent; |
| 387 | |
| 388 | async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 389 | BUG_ON(!async_extent); /* -ENOMEM */ |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 390 | async_extent->start = start; |
| 391 | async_extent->ram_size = ram_size; |
| 392 | async_extent->compressed_size = compressed_size; |
| 393 | async_extent->pages = pages; |
| 394 | async_extent->nr_pages = nr_pages; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 395 | async_extent->compress_type = compress_type; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 396 | list_add_tail(&async_extent->list, &cow->extents); |
| 397 | return 0; |
| 398 | } |
| 399 | |
Wang Shilong | f79707b | 2014-07-17 11:44:09 +0800 | [diff] [blame] | 400 | static inline int inode_need_compress(struct inode *inode) |
| 401 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 402 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Wang Shilong | f79707b | 2014-07-17 11:44:09 +0800 | [diff] [blame] | 403 | |
| 404 | /* force compress */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 405 | if (btrfs_test_opt(fs_info, FORCE_COMPRESS)) |
Wang Shilong | f79707b | 2014-07-17 11:44:09 +0800 | [diff] [blame] | 406 | return 1; |
| 407 | /* bad compression ratios */ |
| 408 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS) |
| 409 | return 0; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 410 | if (btrfs_test_opt(fs_info, COMPRESS) || |
Wang Shilong | f79707b | 2014-07-17 11:44:09 +0800 | [diff] [blame] | 411 | BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS || |
| 412 | BTRFS_I(inode)->force_compress) |
| 413 | return 1; |
| 414 | return 0; |
| 415 | } |
| 416 | |
Nikolay Borisov | 6158e1c | 2017-02-20 13:50:43 +0200 | [diff] [blame] | 417 | static inline void inode_should_defrag(struct btrfs_inode *inode, |
Anand Jain | 26d30f8 | 2016-12-19 19:09:06 +0800 | [diff] [blame] | 418 | u64 start, u64 end, u64 num_bytes, u64 small_write) |
| 419 | { |
| 420 | /* If this is a small write inside eof, kick off a defrag */ |
| 421 | if (num_bytes < small_write && |
Nikolay Borisov | 6158e1c | 2017-02-20 13:50:43 +0200 | [diff] [blame] | 422 | (start > 0 || end + 1 < inode->disk_i_size)) |
Anand Jain | 26d30f8 | 2016-12-19 19:09:06 +0800 | [diff] [blame] | 423 | btrfs_add_inode_defrag(NULL, inode); |
| 424 | } |
| 425 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 426 | /* |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 427 | * we create compressed extents in two phases. The first |
| 428 | * phase compresses a range of pages that have already been |
| 429 | * locked (both pages and state bits are locked). |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 430 | * |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 431 | * This is done inside an ordered work queue, and the compression |
| 432 | * is spread across many cpus. The actual IO submission is step |
| 433 | * two, and the ordered work queue takes care of making sure that |
| 434 | * happens in the same order things were put onto the queue by |
| 435 | * writepages and friends. |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 436 | * |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 437 | * If this code finds it can't get good compression, it puts an |
| 438 | * entry onto the work queue to write the uncompressed bytes. This |
| 439 | * makes sure that both compressed inodes and uncompressed inodes |
Artem Bityutskiy | b257031 | 2012-07-25 18:12:06 +0300 | [diff] [blame] | 440 | * are written in the same order that the flusher thread sent them |
| 441 | * down. |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 442 | */ |
Filipe Manana | c44f649 | 2014-10-09 21:15:44 +0100 | [diff] [blame] | 443 | static noinline void compress_file_range(struct inode *inode, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 444 | struct page *locked_page, |
| 445 | u64 start, u64 end, |
| 446 | struct async_cow *async_cow, |
| 447 | int *num_added) |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 448 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 449 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 450 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 451 | u64 num_bytes; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 452 | u64 blocksize = fs_info->sectorsize; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 453 | u64 actual_end; |
Chris Mason | 42dc7ba | 2008-12-15 11:44:56 -0500 | [diff] [blame] | 454 | u64 isize = i_size_read(inode); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 455 | int ret = 0; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 456 | struct page **pages = NULL; |
| 457 | unsigned long nr_pages; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 458 | unsigned long total_compressed = 0; |
| 459 | unsigned long total_in = 0; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 460 | int i; |
| 461 | int will_compress; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 462 | int compress_type = fs_info->compress_type; |
Chris Mason | 4adaa61 | 2013-03-26 13:07:00 -0400 | [diff] [blame] | 463 | int redirty = 0; |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 464 | |
Nikolay Borisov | 6158e1c | 2017-02-20 13:50:43 +0200 | [diff] [blame] | 465 | inode_should_defrag(BTRFS_I(inode), start, end, end - start + 1, |
| 466 | SZ_16K); |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 467 | |
Chris Mason | 42dc7ba | 2008-12-15 11:44:56 -0500 | [diff] [blame] | 468 | actual_end = min_t(u64, isize, end + 1); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 469 | again: |
| 470 | will_compress = 0; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 471 | nr_pages = (end >> PAGE_SHIFT) - (start >> PAGE_SHIFT) + 1; |
David Sterba | 069eac7 | 2017-02-14 19:36:54 +0100 | [diff] [blame] | 472 | BUILD_BUG_ON((BTRFS_MAX_COMPRESSED % PAGE_SIZE) != 0); |
| 473 | nr_pages = min_t(unsigned long, nr_pages, |
| 474 | BTRFS_MAX_COMPRESSED / PAGE_SIZE); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 475 | |
Chris Mason | f03d9301f | 2009-02-04 09:31:06 -0500 | [diff] [blame] | 476 | /* |
| 477 | * we don't want to send crud past the end of i_size through |
| 478 | * compression, that's just a waste of CPU time. So, if the |
| 479 | * end of the file is before the start of our current |
| 480 | * requested range of bytes, we bail out to the uncompressed |
| 481 | * cleanup code that can deal with all of this. |
| 482 | * |
| 483 | * It isn't really the fastest way to fix things, but this is a |
| 484 | * very uncommon corner. |
| 485 | */ |
| 486 | if (actual_end <= start) |
| 487 | goto cleanup_and_bail_uncompressed; |
| 488 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 489 | total_compressed = actual_end - start; |
| 490 | |
Shilong Wang | 4bcbb33 | 2014-10-07 18:44:35 -0400 | [diff] [blame] | 491 | /* |
| 492 | * skip compression for a small file range(<=blocksize) that |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 493 | * 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] | 494 | */ |
| 495 | if (total_compressed <= blocksize && |
| 496 | (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size)) |
| 497 | goto cleanup_and_bail_uncompressed; |
| 498 | |
David Sterba | 069eac7 | 2017-02-14 19:36:54 +0100 | [diff] [blame] | 499 | total_compressed = min_t(unsigned long, total_compressed, |
| 500 | BTRFS_MAX_UNCOMPRESSED); |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 501 | num_bytes = ALIGN(end - start + 1, blocksize); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 502 | num_bytes = max(blocksize, num_bytes); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 503 | total_in = 0; |
| 504 | ret = 0; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 505 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 506 | /* |
| 507 | * we do compression for mount -o compress and when the |
| 508 | * inode has not been flagged as nocompress. This flag can |
| 509 | * change at any time if we discover bad compression ratios. |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 510 | */ |
Wang Shilong | f79707b | 2014-07-17 11:44:09 +0800 | [diff] [blame] | 511 | if (inode_need_compress(inode)) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 512 | WARN_ON(pages); |
David Sterba | 31e818f | 2015-02-20 18:00:26 +0100 | [diff] [blame] | 513 | pages = kcalloc(nr_pages, sizeof(struct page *), GFP_NOFS); |
Li Zefan | 560f7d7 | 2011-09-08 10:22:01 +0800 | [diff] [blame] | 514 | if (!pages) { |
| 515 | /* just bail out to the uncompressed code */ |
| 516 | goto cont; |
| 517 | } |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 518 | |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 519 | if (BTRFS_I(inode)->force_compress) |
| 520 | compress_type = BTRFS_I(inode)->force_compress; |
| 521 | |
Chris Mason | 4adaa61 | 2013-03-26 13:07:00 -0400 | [diff] [blame] | 522 | /* |
| 523 | * we need to call clear_page_dirty_for_io on each |
| 524 | * page in the range. Otherwise applications with the file |
| 525 | * mmap'd can wander in and change the page contents while |
| 526 | * we are compressing them. |
| 527 | * |
| 528 | * If the compression fails for any reason, we set the pages |
| 529 | * dirty again later on. |
| 530 | */ |
| 531 | extent_range_clear_dirty_for_io(inode, start, end); |
| 532 | redirty = 1; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 533 | ret = btrfs_compress_pages(compress_type, |
| 534 | inode->i_mapping, start, |
David Sterba | 38c3146 | 2017-02-14 19:04:07 +0100 | [diff] [blame] | 535 | pages, |
David Sterba | 4d3a800 | 2017-02-14 19:04:07 +0100 | [diff] [blame] | 536 | &nr_pages, |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 537 | &total_in, |
David Sterba | e5d7490 | 2017-02-14 19:45:05 +0100 | [diff] [blame] | 538 | &total_compressed); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 539 | |
| 540 | if (!ret) { |
| 541 | unsigned long offset = total_compressed & |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 542 | (PAGE_SIZE - 1); |
David Sterba | 4d3a800 | 2017-02-14 19:04:07 +0100 | [diff] [blame] | 543 | struct page *page = pages[nr_pages - 1]; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 544 | char *kaddr; |
| 545 | |
| 546 | /* zero the tail end of the last page, we might be |
| 547 | * sending it down to disk |
| 548 | */ |
| 549 | if (offset) { |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 550 | kaddr = kmap_atomic(page); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 551 | memset(kaddr + offset, 0, |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 552 | PAGE_SIZE - offset); |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 553 | kunmap_atomic(kaddr); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 554 | } |
| 555 | will_compress = 1; |
| 556 | } |
| 557 | } |
Li Zefan | 560f7d7 | 2011-09-08 10:22:01 +0800 | [diff] [blame] | 558 | cont: |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 559 | if (start == 0) { |
| 560 | /* lets try to make an inline extent */ |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 561 | if (ret || total_in < (actual_end - start)) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 562 | /* we didn't compress the entire range, try |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 563 | * to make an uncompressed inline extent. |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 564 | */ |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 565 | ret = cow_file_range_inline(root, inode, start, end, |
Anand Jain | f74670f | 2016-12-06 12:43:07 +0800 | [diff] [blame] | 566 | 0, BTRFS_COMPRESS_NONE, NULL); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 567 | } else { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 568 | /* try making a compressed inline extent */ |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 569 | ret = cow_file_range_inline(root, inode, start, end, |
Li Zefan | fe3f566 | 2011-03-28 08:30:38 +0000 | [diff] [blame] | 570 | total_compressed, |
| 571 | compress_type, pages); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 572 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 573 | if (ret <= 0) { |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 574 | unsigned long clear_flags = EXTENT_DELALLOC | |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 575 | EXTENT_DELALLOC_NEW | EXTENT_DEFRAG; |
Filipe Manana | e6eb431 | 2014-10-10 10:45:12 +0100 | [diff] [blame] | 576 | unsigned long page_error_op; |
| 577 | |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 578 | clear_flags |= (ret < 0) ? EXTENT_DO_ACCOUNTING : 0; |
Filipe Manana | e6eb431 | 2014-10-10 10:45:12 +0100 | [diff] [blame] | 579 | page_error_op = ret < 0 ? PAGE_SET_ERROR : 0; |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 580 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 581 | /* |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 582 | * inline extent creation worked or returned error, |
| 583 | * we don't need to create any more async work items. |
| 584 | * Unlock and free up our temp pages. |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 585 | */ |
Qu Wenruo | ba8b04c | 2016-07-19 16:50:36 +0800 | [diff] [blame] | 586 | extent_clear_unlock_delalloc(inode, start, end, end, |
| 587 | NULL, clear_flags, |
| 588 | PAGE_UNLOCK | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 589 | PAGE_CLEAR_DIRTY | |
| 590 | PAGE_SET_WRITEBACK | |
Filipe Manana | e6eb431 | 2014-10-10 10:45:12 +0100 | [diff] [blame] | 591 | page_error_op | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 592 | PAGE_END_WRITEBACK); |
Filipe Manana | 1c81ba2 | 2017-03-08 16:43:49 +0000 | [diff] [blame] | 593 | if (ret == 0) |
| 594 | btrfs_free_reserved_data_space_noquota(inode, |
| 595 | start, |
| 596 | end - start + 1); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 597 | goto free_pages_out; |
| 598 | } |
| 599 | } |
| 600 | |
| 601 | if (will_compress) { |
| 602 | /* |
| 603 | * we aren't doing an inline extent round the compressed size |
| 604 | * up to a block size boundary so the allocator does sane |
| 605 | * things |
| 606 | */ |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 607 | total_compressed = ALIGN(total_compressed, blocksize); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 608 | |
| 609 | /* |
| 610 | * one last check to make sure the compression is really a |
| 611 | * win, compare the page count read with the blocks on disk |
| 612 | */ |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 613 | total_in = ALIGN(total_in, PAGE_SIZE); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 614 | if (total_compressed >= total_in) { |
| 615 | will_compress = 0; |
| 616 | } else { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 617 | num_bytes = total_in; |
Ashish Samant | c8bb0c8 | 2016-03-25 19:01:33 -0700 | [diff] [blame] | 618 | *num_added += 1; |
| 619 | |
| 620 | /* |
| 621 | * The async work queues will take care of doing actual |
| 622 | * allocation on disk for these compressed pages, and |
| 623 | * will submit them to the elevator. |
| 624 | */ |
| 625 | add_async_extent(async_cow, start, num_bytes, |
David Sterba | 4d3a800 | 2017-02-14 19:04:07 +0100 | [diff] [blame] | 626 | total_compressed, pages, nr_pages, |
Ashish Samant | c8bb0c8 | 2016-03-25 19:01:33 -0700 | [diff] [blame] | 627 | compress_type); |
| 628 | |
| 629 | if (start + num_bytes < end) { |
| 630 | start += num_bytes; |
| 631 | pages = NULL; |
| 632 | cond_resched(); |
| 633 | goto again; |
| 634 | } |
| 635 | return; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 636 | } |
| 637 | } |
Ashish Samant | c8bb0c8 | 2016-03-25 19:01:33 -0700 | [diff] [blame] | 638 | if (pages) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 639 | /* |
| 640 | * the compression code ran but failed to make things smaller, |
| 641 | * free any pages it allocated and our page pointer array |
| 642 | */ |
David Sterba | 4d3a800 | 2017-02-14 19:04:07 +0100 | [diff] [blame] | 643 | for (i = 0; i < nr_pages; i++) { |
Chris Mason | 70b99e6 | 2008-10-31 12:46:39 -0400 | [diff] [blame] | 644 | WARN_ON(pages[i]->mapping); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 645 | put_page(pages[i]); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 646 | } |
| 647 | kfree(pages); |
| 648 | pages = NULL; |
| 649 | total_compressed = 0; |
David Sterba | 4d3a800 | 2017-02-14 19:04:07 +0100 | [diff] [blame] | 650 | nr_pages = 0; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 651 | |
| 652 | /* flag the file so we don't compress in the future */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 653 | if (!btrfs_test_opt(fs_info, FORCE_COMPRESS) && |
Chris Mason | 1e701a3 | 2010-03-11 09:42:04 -0500 | [diff] [blame] | 654 | !(BTRFS_I(inode)->force_compress)) { |
Chris Mason | a555f81 | 2010-01-28 16:18:15 -0500 | [diff] [blame] | 655 | BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS; |
Chris Mason | 1e701a3 | 2010-03-11 09:42:04 -0500 | [diff] [blame] | 656 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 657 | } |
Chris Mason | f03d9301f | 2009-02-04 09:31:06 -0500 | [diff] [blame] | 658 | cleanup_and_bail_uncompressed: |
Ashish Samant | c8bb0c8 | 2016-03-25 19:01:33 -0700 | [diff] [blame] | 659 | /* |
| 660 | * No compression, but we still need to write the pages in the file |
| 661 | * we've been given so far. redirty the locked page if it corresponds |
| 662 | * to our extent and set things up for the async work queue to run |
| 663 | * cow_file_range to do the normal delalloc dance. |
| 664 | */ |
| 665 | if (page_offset(locked_page) >= start && |
| 666 | page_offset(locked_page) <= end) |
| 667 | __set_page_dirty_nobuffers(locked_page); |
| 668 | /* unlocked later on in the async handlers */ |
| 669 | |
| 670 | if (redirty) |
| 671 | extent_range_redirty_for_io(inode, start, end); |
| 672 | add_async_extent(async_cow, start, end - start + 1, 0, NULL, 0, |
| 673 | BTRFS_COMPRESS_NONE); |
| 674 | *num_added += 1; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 675 | |
Filipe Manana | c44f649 | 2014-10-09 21:15:44 +0100 | [diff] [blame] | 676 | return; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 677 | |
| 678 | free_pages_out: |
David Sterba | 4d3a800 | 2017-02-14 19:04:07 +0100 | [diff] [blame] | 679 | for (i = 0; i < nr_pages; i++) { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 680 | WARN_ON(pages[i]->mapping); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 681 | put_page(pages[i]); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 682 | } |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 683 | kfree(pages); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 684 | } |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 685 | |
Filipe Manana | 40ae837 | 2014-10-06 22:14:24 +0100 | [diff] [blame] | 686 | static void free_async_extent_pages(struct async_extent *async_extent) |
| 687 | { |
| 688 | int i; |
| 689 | |
| 690 | if (!async_extent->pages) |
| 691 | return; |
| 692 | |
| 693 | for (i = 0; i < async_extent->nr_pages; i++) { |
| 694 | WARN_ON(async_extent->pages[i]->mapping); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 695 | put_page(async_extent->pages[i]); |
Filipe Manana | 40ae837 | 2014-10-06 22:14:24 +0100 | [diff] [blame] | 696 | } |
| 697 | kfree(async_extent->pages); |
| 698 | async_extent->nr_pages = 0; |
| 699 | async_extent->pages = NULL; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 700 | } |
| 701 | |
| 702 | /* |
| 703 | * phase two of compressed writeback. This is the ordered portion |
| 704 | * of the code, which only gets called in the order the work was |
| 705 | * queued. We walk all the async extents created by compress_file_range |
| 706 | * and send them down to the disk. |
| 707 | */ |
Filipe Manana | dec8f17 | 2014-10-06 22:14:26 +0100 | [diff] [blame] | 708 | static noinline void submit_compressed_extents(struct inode *inode, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 709 | struct async_cow *async_cow) |
| 710 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 711 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 712 | struct async_extent *async_extent; |
| 713 | u64 alloc_hint = 0; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 714 | struct btrfs_key ins; |
| 715 | struct extent_map *em; |
| 716 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 717 | struct extent_io_tree *io_tree; |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 718 | int ret = 0; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 719 | |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 720 | again: |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 721 | while (!list_empty(&async_cow->extents)) { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 722 | async_extent = list_entry(async_cow->extents.next, |
| 723 | struct async_extent, list); |
| 724 | list_del(&async_extent->list); |
| 725 | |
| 726 | io_tree = &BTRFS_I(inode)->io_tree; |
| 727 | |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 728 | retry: |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 729 | /* did the compression code fall back to uncompressed IO? */ |
| 730 | if (!async_extent->pages) { |
| 731 | int page_started = 0; |
| 732 | unsigned long nr_written = 0; |
| 733 | |
| 734 | lock_extent(io_tree, async_extent->start, |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 735 | async_extent->start + |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 736 | async_extent->ram_size - 1); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 737 | |
| 738 | /* allocate blocks */ |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 739 | ret = cow_file_range(inode, async_cow->locked_page, |
| 740 | async_extent->start, |
| 741 | async_extent->start + |
| 742 | async_extent->ram_size - 1, |
Wang Xiaoguang | dda3245 | 2016-07-11 11:05:29 +0800 | [diff] [blame] | 743 | async_extent->start + |
| 744 | async_extent->ram_size - 1, |
| 745 | &page_started, &nr_written, 0, |
| 746 | NULL); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 747 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 748 | /* JDM XXX */ |
| 749 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 750 | /* |
| 751 | * if page_started, cow_file_range inserted an |
| 752 | * inline extent and took care of all the unlocking |
| 753 | * and IO for us. Otherwise, we need to submit |
| 754 | * all those pages down to the drive. |
| 755 | */ |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 756 | if (!page_started && !ret) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 757 | extent_write_locked_range(io_tree, |
| 758 | inode, async_extent->start, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 759 | async_extent->start + |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 760 | async_extent->ram_size - 1, |
| 761 | btrfs_get_extent, |
| 762 | WB_SYNC_ALL); |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 763 | else if (ret) |
| 764 | unlock_page(async_cow->locked_page); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 765 | kfree(async_extent); |
| 766 | cond_resched(); |
| 767 | continue; |
| 768 | } |
| 769 | |
| 770 | lock_extent(io_tree, async_extent->start, |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 771 | async_extent->start + async_extent->ram_size - 1); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 772 | |
Wang Xiaoguang | 1851309 | 2016-07-25 15:51:40 +0800 | [diff] [blame] | 773 | ret = btrfs_reserve_extent(root, async_extent->ram_size, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 774 | async_extent->compressed_size, |
| 775 | async_extent->compressed_size, |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 776 | 0, alloc_hint, &ins, 1, 1); |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 777 | if (ret) { |
Filipe Manana | 40ae837 | 2014-10-06 22:14:24 +0100 | [diff] [blame] | 778 | free_async_extent_pages(async_extent); |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 779 | |
Josef Bacik | fdf8e2e | 2013-06-14 16:58:23 -0400 | [diff] [blame] | 780 | if (ret == -ENOSPC) { |
| 781 | unlock_extent(io_tree, async_extent->start, |
| 782 | async_extent->start + |
| 783 | async_extent->ram_size - 1); |
Liu Bo | ce62003 | 2014-07-24 22:48:05 +0800 | [diff] [blame] | 784 | |
| 785 | /* |
| 786 | * we need to redirty the pages if we decide to |
| 787 | * fallback to uncompressed IO, otherwise we |
| 788 | * will not submit these pages down to lower |
| 789 | * layers. |
| 790 | */ |
| 791 | extent_range_redirty_for_io(inode, |
| 792 | async_extent->start, |
| 793 | async_extent->start + |
| 794 | async_extent->ram_size - 1); |
| 795 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 796 | goto retry; |
Josef Bacik | fdf8e2e | 2013-06-14 16:58:23 -0400 | [diff] [blame] | 797 | } |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 798 | goto out_free; |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 799 | } |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 800 | /* |
| 801 | * here we're doing allocation and writeback of the |
| 802 | * compressed pages |
| 803 | */ |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 804 | em = create_io_em(inode, async_extent->start, |
| 805 | async_extent->ram_size, /* len */ |
| 806 | async_extent->start, /* orig_start */ |
| 807 | ins.objectid, /* block_start */ |
| 808 | ins.offset, /* block_len */ |
| 809 | ins.offset, /* orig_block_len */ |
| 810 | async_extent->ram_size, /* ram_bytes */ |
| 811 | async_extent->compress_type, |
| 812 | BTRFS_ORDERED_COMPRESSED); |
| 813 | if (IS_ERR(em)) |
| 814 | /* ret value is not necessary due to void function */ |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 815 | goto out_free_reserve; |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 816 | free_extent_map(em); |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 817 | |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 818 | ret = btrfs_add_ordered_extent_compress(inode, |
| 819 | async_extent->start, |
| 820 | ins.objectid, |
| 821 | async_extent->ram_size, |
| 822 | ins.offset, |
| 823 | BTRFS_ORDERED_COMPRESSED, |
| 824 | async_extent->compress_type); |
Filipe Manana | d9f8596 | 2014-08-25 10:43:00 +0100 | [diff] [blame] | 825 | if (ret) { |
Nikolay Borisov | dcdbc05 | 2017-02-20 13:50:45 +0200 | [diff] [blame] | 826 | btrfs_drop_extent_cache(BTRFS_I(inode), |
| 827 | async_extent->start, |
Filipe Manana | d9f8596 | 2014-08-25 10:43:00 +0100 | [diff] [blame] | 828 | async_extent->start + |
| 829 | async_extent->ram_size - 1, 0); |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 830 | goto out_free_reserve; |
Filipe Manana | d9f8596 | 2014-08-25 10:43:00 +0100 | [diff] [blame] | 831 | } |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 832 | btrfs_dec_block_group_reservations(fs_info, ins.objectid); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 833 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 834 | /* |
| 835 | * clear dirty, set writeback and unlock the pages. |
| 836 | */ |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 837 | extent_clear_unlock_delalloc(inode, async_extent->start, |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 838 | async_extent->start + |
| 839 | async_extent->ram_size - 1, |
Qu Wenruo | ba8b04c | 2016-07-19 16:50:36 +0800 | [diff] [blame] | 840 | async_extent->start + |
| 841 | async_extent->ram_size - 1, |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 842 | NULL, EXTENT_LOCKED | EXTENT_DELALLOC, |
| 843 | PAGE_UNLOCK | PAGE_CLEAR_DIRTY | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 844 | PAGE_SET_WRITEBACK); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 845 | ret = btrfs_submit_compressed_write(inode, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 846 | async_extent->start, |
| 847 | async_extent->ram_size, |
| 848 | ins.objectid, |
| 849 | ins.offset, async_extent->pages, |
| 850 | async_extent->nr_pages); |
Filipe Manana | fce2a4e | 2014-10-06 22:14:23 +0100 | [diff] [blame] | 851 | if (ret) { |
| 852 | struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree; |
| 853 | struct page *p = async_extent->pages[0]; |
| 854 | const u64 start = async_extent->start; |
| 855 | const u64 end = start + async_extent->ram_size - 1; |
| 856 | |
| 857 | p->mapping = inode->i_mapping; |
| 858 | tree->ops->writepage_end_io_hook(p, start, end, |
| 859 | NULL, 0); |
| 860 | p->mapping = NULL; |
Qu Wenruo | ba8b04c | 2016-07-19 16:50:36 +0800 | [diff] [blame] | 861 | extent_clear_unlock_delalloc(inode, start, end, end, |
| 862 | NULL, 0, |
Filipe Manana | fce2a4e | 2014-10-06 22:14:23 +0100 | [diff] [blame] | 863 | PAGE_END_WRITEBACK | |
| 864 | PAGE_SET_ERROR); |
Filipe Manana | 40ae837 | 2014-10-06 22:14:24 +0100 | [diff] [blame] | 865 | free_async_extent_pages(async_extent); |
Filipe Manana | fce2a4e | 2014-10-06 22:14:23 +0100 | [diff] [blame] | 866 | } |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 867 | alloc_hint = ins.objectid + ins.offset; |
| 868 | kfree(async_extent); |
| 869 | cond_resched(); |
| 870 | } |
Filipe Manana | dec8f17 | 2014-10-06 22:14:26 +0100 | [diff] [blame] | 871 | return; |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 872 | out_free_reserve: |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 873 | btrfs_dec_block_group_reservations(fs_info, ins.objectid); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 874 | btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 875 | out_free: |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 876 | extent_clear_unlock_delalloc(inode, async_extent->start, |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 877 | async_extent->start + |
| 878 | async_extent->ram_size - 1, |
Qu Wenruo | ba8b04c | 2016-07-19 16:50:36 +0800 | [diff] [blame] | 879 | async_extent->start + |
| 880 | async_extent->ram_size - 1, |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 881 | NULL, EXTENT_LOCKED | EXTENT_DELALLOC | |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 882 | EXTENT_DELALLOC_NEW | |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 883 | EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING, |
| 884 | PAGE_UNLOCK | PAGE_CLEAR_DIRTY | |
Filipe Manana | 704de49 | 2014-10-06 22:14:22 +0100 | [diff] [blame] | 885 | PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK | |
| 886 | PAGE_SET_ERROR); |
Filipe Manana | 40ae837 | 2014-10-06 22:14:24 +0100 | [diff] [blame] | 887 | free_async_extent_pages(async_extent); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 888 | kfree(async_extent); |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 889 | goto again; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 890 | } |
| 891 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 892 | static u64 get_extent_allocation_hint(struct inode *inode, u64 start, |
| 893 | u64 num_bytes) |
| 894 | { |
| 895 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
| 896 | struct extent_map *em; |
| 897 | u64 alloc_hint = 0; |
| 898 | |
| 899 | read_lock(&em_tree->lock); |
| 900 | em = search_extent_mapping(em_tree, start, num_bytes); |
| 901 | if (em) { |
| 902 | /* |
| 903 | * if block start isn't an actual block number then find the |
| 904 | * first block in this inode and use that as a hint. If that |
| 905 | * block is also bogus then just don't worry about it. |
| 906 | */ |
| 907 | if (em->block_start >= EXTENT_MAP_LAST_BYTE) { |
| 908 | free_extent_map(em); |
| 909 | em = search_extent_mapping(em_tree, 0, 0); |
| 910 | if (em && em->block_start < EXTENT_MAP_LAST_BYTE) |
| 911 | alloc_hint = em->block_start; |
| 912 | if (em) |
| 913 | free_extent_map(em); |
| 914 | } else { |
| 915 | alloc_hint = em->block_start; |
| 916 | free_extent_map(em); |
| 917 | } |
| 918 | } |
| 919 | read_unlock(&em_tree->lock); |
| 920 | |
| 921 | return alloc_hint; |
| 922 | } |
| 923 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 924 | /* |
| 925 | * when extent_io.c finds a delayed allocation range in the file, |
| 926 | * the call backs end up in this code. The basic idea is to |
| 927 | * allocate extents on disk for the range, and create ordered data structs |
| 928 | * in ram to track those extents. |
| 929 | * |
| 930 | * locked_page is the page that writepage had locked already. We use |
| 931 | * it to make sure we don't do extra locks or unlocks. |
| 932 | * |
| 933 | * *page_started is set to one if we unlock locked_page and do everything |
| 934 | * required to start IO on it. It may be clean and already done with |
| 935 | * IO when we return. |
| 936 | */ |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 937 | static noinline int cow_file_range(struct inode *inode, |
| 938 | struct page *locked_page, |
Wang Xiaoguang | dda3245 | 2016-07-11 11:05:29 +0800 | [diff] [blame] | 939 | u64 start, u64 end, u64 delalloc_end, |
| 940 | int *page_started, unsigned long *nr_written, |
| 941 | int unlock, struct btrfs_dedupe_hash *hash) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 942 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 943 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 944 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 945 | u64 alloc_hint = 0; |
| 946 | u64 num_bytes; |
| 947 | unsigned long ram_size; |
| 948 | u64 disk_num_bytes; |
Filipe Manana | a315e68 | 2017-03-06 23:04:20 +0000 | [diff] [blame] | 949 | u64 cur_alloc_size = 0; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 950 | u64 blocksize = fs_info->sectorsize; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 951 | struct btrfs_key ins; |
| 952 | struct extent_map *em; |
Filipe Manana | a315e68 | 2017-03-06 23:04:20 +0000 | [diff] [blame] | 953 | unsigned clear_bits; |
| 954 | unsigned long page_ops; |
| 955 | bool extent_reserved = false; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 956 | int ret = 0; |
| 957 | |
Nikolay Borisov | 70ddc55 | 2017-02-20 13:50:35 +0200 | [diff] [blame] | 958 | if (btrfs_is_free_space_inode(BTRFS_I(inode))) { |
Josef Bacik | 02ecd2c | 2013-10-25 16:19:08 -0400 | [diff] [blame] | 959 | WARN_ON_ONCE(1); |
Josef Bacik | 29bce2f | 2014-02-07 12:21:23 -0500 | [diff] [blame] | 960 | ret = -EINVAL; |
| 961 | goto out_unlock; |
Josef Bacik | 02ecd2c | 2013-10-25 16:19:08 -0400 | [diff] [blame] | 962 | } |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 963 | |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 964 | num_bytes = ALIGN(end - start + 1, blocksize); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 965 | num_bytes = max(blocksize, num_bytes); |
| 966 | disk_num_bytes = num_bytes; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 967 | |
Nikolay Borisov | 6158e1c | 2017-02-20 13:50:43 +0200 | [diff] [blame] | 968 | inode_should_defrag(BTRFS_I(inode), start, end, num_bytes, SZ_64K); |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 969 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 970 | if (start == 0) { |
| 971 | /* lets try to make an inline extent */ |
Anand Jain | f74670f | 2016-12-06 12:43:07 +0800 | [diff] [blame] | 972 | ret = cow_file_range_inline(root, inode, start, end, 0, |
| 973 | BTRFS_COMPRESS_NONE, NULL); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 974 | if (ret == 0) { |
Qu Wenruo | ba8b04c | 2016-07-19 16:50:36 +0800 | [diff] [blame] | 975 | extent_clear_unlock_delalloc(inode, start, end, |
| 976 | delalloc_end, NULL, |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 977 | EXTENT_LOCKED | EXTENT_DELALLOC | |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 978 | EXTENT_DELALLOC_NEW | |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 979 | EXTENT_DEFRAG, PAGE_UNLOCK | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 980 | PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK | |
| 981 | PAGE_END_WRITEBACK); |
Wang Xiaoguang | 1851309 | 2016-07-25 15:51:40 +0800 | [diff] [blame] | 982 | btrfs_free_reserved_data_space_noquota(inode, start, |
| 983 | end - start + 1); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 984 | *nr_written = *nr_written + |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 985 | (end - start + PAGE_SIZE) / PAGE_SIZE; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 986 | *page_started = 1; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 987 | goto out; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 988 | } else if (ret < 0) { |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 989 | goto out_unlock; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 990 | } |
| 991 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 992 | |
| 993 | BUG_ON(disk_num_bytes > |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 994 | btrfs_super_total_bytes(fs_info->super_copy)); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 995 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 996 | alloc_hint = get_extent_allocation_hint(inode, start, num_bytes); |
Nikolay Borisov | dcdbc05 | 2017-02-20 13:50:45 +0200 | [diff] [blame] | 997 | btrfs_drop_extent_cache(BTRFS_I(inode), start, |
| 998 | start + num_bytes - 1, 0); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 999 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1000 | while (disk_num_bytes > 0) { |
Josef Bacik | 287a0ab | 2010-03-19 18:07:23 +0000 | [diff] [blame] | 1001 | cur_alloc_size = disk_num_bytes; |
Wang Xiaoguang | 1851309 | 2016-07-25 15:51:40 +0800 | [diff] [blame] | 1002 | ret = btrfs_reserve_extent(root, cur_alloc_size, cur_alloc_size, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1003 | fs_info->sectorsize, 0, alloc_hint, |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 1004 | &ins, 1, 1); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 1005 | if (ret < 0) |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1006 | goto out_unlock; |
Filipe Manana | a315e68 | 2017-03-06 23:04:20 +0000 | [diff] [blame] | 1007 | cur_alloc_size = ins.offset; |
| 1008 | extent_reserved = true; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1009 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1010 | ram_size = ins.offset; |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 1011 | em = create_io_em(inode, start, ins.offset, /* len */ |
| 1012 | start, /* orig_start */ |
| 1013 | ins.objectid, /* block_start */ |
| 1014 | ins.offset, /* block_len */ |
| 1015 | ins.offset, /* orig_block_len */ |
| 1016 | ram_size, /* ram_bytes */ |
| 1017 | BTRFS_COMPRESS_NONE, /* compress_type */ |
Liu Bo | 1af4a0a | 2017-02-13 15:35:09 -0800 | [diff] [blame] | 1018 | BTRFS_ORDERED_REGULAR /* type */); |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 1019 | if (IS_ERR(em)) |
Liu Bo | ace68ba | 2013-04-22 10:53:47 +0000 | [diff] [blame] | 1020 | goto out_reserve; |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 1021 | free_extent_map(em); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1022 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1023 | ret = btrfs_add_ordered_extent(inode, start, ins.objectid, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1024 | ram_size, cur_alloc_size, 0); |
Liu Bo | ace68ba | 2013-04-22 10:53:47 +0000 | [diff] [blame] | 1025 | if (ret) |
Filipe Manana | d9f8596 | 2014-08-25 10:43:00 +0100 | [diff] [blame] | 1026 | goto out_drop_extent_cache; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1027 | |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1028 | if (root->root_key.objectid == |
| 1029 | BTRFS_DATA_RELOC_TREE_OBJECTID) { |
| 1030 | ret = btrfs_reloc_clone_csums(inode, start, |
| 1031 | cur_alloc_size); |
Qu Wenruo | 4dbd80f | 2017-03-08 10:25:51 +0800 | [diff] [blame] | 1032 | /* |
| 1033 | * Only drop cache here, and process as normal. |
| 1034 | * |
| 1035 | * We must not allow extent_clear_unlock_delalloc() |
| 1036 | * at out_unlock label to free meta of this ordered |
| 1037 | * extent, as its meta should be freed by |
| 1038 | * btrfs_finish_ordered_io(). |
| 1039 | * |
| 1040 | * So we must continue until @start is increased to |
| 1041 | * skip current ordered extent. |
| 1042 | */ |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 1043 | if (ret) |
Qu Wenruo | 4dbd80f | 2017-03-08 10:25:51 +0800 | [diff] [blame] | 1044 | btrfs_drop_extent_cache(BTRFS_I(inode), start, |
| 1045 | start + ram_size - 1, 0); |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1046 | } |
| 1047 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1048 | btrfs_dec_block_group_reservations(fs_info, ins.objectid); |
Filipe Manana | 9cfa3e3 | 2016-04-26 15:39:32 +0100 | [diff] [blame] | 1049 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1050 | /* we're not doing compressed IO, don't unlock the first |
| 1051 | * page (which the caller expects to stay locked), don't |
| 1052 | * clear any dirty bits and don't set any writeback bits |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 1053 | * |
| 1054 | * Do set the Private2 bit so we know this page was properly |
| 1055 | * setup for writepage |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1056 | */ |
Filipe Manana | a315e68 | 2017-03-06 23:04:20 +0000 | [diff] [blame] | 1057 | page_ops = unlock ? PAGE_UNLOCK : 0; |
| 1058 | page_ops |= PAGE_SET_PRIVATE2; |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 1059 | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1060 | extent_clear_unlock_delalloc(inode, start, |
Qu Wenruo | ba8b04c | 2016-07-19 16:50:36 +0800 | [diff] [blame] | 1061 | start + ram_size - 1, |
| 1062 | delalloc_end, locked_page, |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1063 | EXTENT_LOCKED | EXTENT_DELALLOC, |
Filipe Manana | a315e68 | 2017-03-06 23:04:20 +0000 | [diff] [blame] | 1064 | page_ops); |
Qu Wenruo | 4dbd80f | 2017-03-08 10:25:51 +0800 | [diff] [blame] | 1065 | if (disk_num_bytes < cur_alloc_size) |
| 1066 | disk_num_bytes = 0; |
| 1067 | else |
| 1068 | disk_num_bytes -= cur_alloc_size; |
Chris Mason | c59f895 | 2007-12-17 20:14:04 -0500 | [diff] [blame] | 1069 | num_bytes -= cur_alloc_size; |
| 1070 | alloc_hint = ins.objectid + ins.offset; |
| 1071 | start += cur_alloc_size; |
Filipe Manana | a315e68 | 2017-03-06 23:04:20 +0000 | [diff] [blame] | 1072 | extent_reserved = false; |
Qu Wenruo | 4dbd80f | 2017-03-08 10:25:51 +0800 | [diff] [blame] | 1073 | |
| 1074 | /* |
| 1075 | * btrfs_reloc_clone_csums() error, since start is increased |
| 1076 | * extent_clear_unlock_delalloc() at out_unlock label won't |
| 1077 | * free metadata of current ordered extent, we're OK to exit. |
| 1078 | */ |
| 1079 | if (ret) |
| 1080 | goto out_unlock; |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 1081 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1082 | out: |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1083 | return ret; |
Miao Xie | b7d5b0a | 2012-11-01 07:32:18 +0000 | [diff] [blame] | 1084 | |
Filipe Manana | d9f8596 | 2014-08-25 10:43:00 +0100 | [diff] [blame] | 1085 | out_drop_extent_cache: |
Nikolay Borisov | dcdbc05 | 2017-02-20 13:50:45 +0200 | [diff] [blame] | 1086 | 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] | 1087 | out_reserve: |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1088 | btrfs_dec_block_group_reservations(fs_info, ins.objectid); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 1089 | btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1090 | out_unlock: |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 1091 | clear_bits = EXTENT_LOCKED | EXTENT_DELALLOC | EXTENT_DELALLOC_NEW | |
| 1092 | EXTENT_DEFRAG | EXTENT_CLEAR_META_RESV; |
Filipe Manana | a315e68 | 2017-03-06 23:04:20 +0000 | [diff] [blame] | 1093 | page_ops = PAGE_UNLOCK | PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK | |
| 1094 | PAGE_END_WRITEBACK; |
| 1095 | /* |
| 1096 | * If we reserved an extent for our delalloc range (or a subrange) and |
| 1097 | * failed to create the respective ordered extent, then it means that |
| 1098 | * when we reserved the extent we decremented the extent's size from |
| 1099 | * the data space_info's bytes_may_use counter and incremented the |
| 1100 | * space_info's bytes_reserved counter by the same amount. We must make |
| 1101 | * sure extent_clear_unlock_delalloc() does not try to decrement again |
| 1102 | * the data space_info's bytes_may_use counter, therefore we do not pass |
| 1103 | * it the flag EXTENT_CLEAR_DATA_RESV. |
| 1104 | */ |
| 1105 | if (extent_reserved) { |
| 1106 | extent_clear_unlock_delalloc(inode, start, |
| 1107 | start + cur_alloc_size, |
| 1108 | start + cur_alloc_size, |
| 1109 | locked_page, |
| 1110 | clear_bits, |
| 1111 | page_ops); |
| 1112 | start += cur_alloc_size; |
| 1113 | if (start >= end) |
| 1114 | goto out; |
| 1115 | } |
Qu Wenruo | ba8b04c | 2016-07-19 16:50:36 +0800 | [diff] [blame] | 1116 | extent_clear_unlock_delalloc(inode, start, end, delalloc_end, |
| 1117 | locked_page, |
Filipe Manana | a315e68 | 2017-03-06 23:04:20 +0000 | [diff] [blame] | 1118 | clear_bits | EXTENT_CLEAR_DATA_RESV, |
| 1119 | page_ops); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1120 | goto out; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1121 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1122 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1123 | /* |
| 1124 | * work queue call back to started compression on a file and pages |
| 1125 | */ |
| 1126 | static noinline void async_cow_start(struct btrfs_work *work) |
| 1127 | { |
| 1128 | struct async_cow *async_cow; |
| 1129 | int num_added = 0; |
| 1130 | async_cow = container_of(work, struct async_cow, work); |
| 1131 | |
| 1132 | compress_file_range(async_cow->inode, async_cow->locked_page, |
| 1133 | async_cow->start, async_cow->end, async_cow, |
| 1134 | &num_added); |
Josef Bacik | 8180ef8 | 2012-06-08 15:16:12 -0400 | [diff] [blame] | 1135 | if (num_added == 0) { |
Josef Bacik | cb77fcd | 2012-06-15 12:19:48 -0600 | [diff] [blame] | 1136 | btrfs_add_delayed_iput(async_cow->inode); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1137 | async_cow->inode = NULL; |
Josef Bacik | 8180ef8 | 2012-06-08 15:16:12 -0400 | [diff] [blame] | 1138 | } |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1139 | } |
| 1140 | |
| 1141 | /* |
| 1142 | * work queue call back to submit previously compressed pages |
| 1143 | */ |
| 1144 | static noinline void async_cow_submit(struct btrfs_work *work) |
| 1145 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1146 | struct btrfs_fs_info *fs_info; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1147 | struct async_cow *async_cow; |
| 1148 | struct btrfs_root *root; |
| 1149 | unsigned long nr_pages; |
| 1150 | |
| 1151 | async_cow = container_of(work, struct async_cow, work); |
| 1152 | |
| 1153 | root = async_cow->root; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1154 | fs_info = root->fs_info; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1155 | nr_pages = (async_cow->end - async_cow->start + PAGE_SIZE) >> |
| 1156 | PAGE_SHIFT; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1157 | |
David Sterba | ee86395 | 2015-02-16 19:41:40 +0100 | [diff] [blame] | 1158 | /* |
| 1159 | * atomic_sub_return implies a barrier for waitqueue_active |
| 1160 | */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1161 | if (atomic_sub_return(nr_pages, &fs_info->async_delalloc_pages) < |
Byongho Lee | ee22184 | 2015-12-15 01:42:10 +0900 | [diff] [blame] | 1162 | 5 * SZ_1M && |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1163 | waitqueue_active(&fs_info->async_submit_wait)) |
| 1164 | wake_up(&fs_info->async_submit_wait); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1165 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1166 | if (async_cow->inode) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1167 | submit_compressed_extents(async_cow->inode, async_cow); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1168 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1169 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1170 | static noinline void async_cow_free(struct btrfs_work *work) |
| 1171 | { |
| 1172 | struct async_cow *async_cow; |
| 1173 | async_cow = container_of(work, struct async_cow, work); |
Josef Bacik | 8180ef8 | 2012-06-08 15:16:12 -0400 | [diff] [blame] | 1174 | if (async_cow->inode) |
Josef Bacik | cb77fcd | 2012-06-15 12:19:48 -0600 | [diff] [blame] | 1175 | btrfs_add_delayed_iput(async_cow->inode); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1176 | kfree(async_cow); |
| 1177 | } |
| 1178 | |
| 1179 | static int cow_file_range_async(struct inode *inode, struct page *locked_page, |
| 1180 | u64 start, u64 end, int *page_started, |
| 1181 | unsigned long *nr_written) |
| 1182 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1183 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1184 | struct async_cow *async_cow; |
| 1185 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 1186 | unsigned long nr_pages; |
| 1187 | u64 cur_end; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1188 | |
Chris Mason | a3429ab | 2009-10-08 12:30:20 -0400 | [diff] [blame] | 1189 | clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED, |
| 1190 | 1, 0, NULL, GFP_NOFS); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1191 | while (start < end) { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1192 | async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1193 | BUG_ON(!async_cow); /* -ENOMEM */ |
Josef Bacik | 8180ef8 | 2012-06-08 15:16:12 -0400 | [diff] [blame] | 1194 | async_cow->inode = igrab(inode); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1195 | async_cow->root = root; |
| 1196 | async_cow->locked_page = locked_page; |
| 1197 | async_cow->start = start; |
| 1198 | |
Wang Shilong | f79707b | 2014-07-17 11:44:09 +0800 | [diff] [blame] | 1199 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS && |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1200 | !btrfs_test_opt(fs_info, FORCE_COMPRESS)) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1201 | cur_end = end; |
| 1202 | else |
Byongho Lee | ee22184 | 2015-12-15 01:42:10 +0900 | [diff] [blame] | 1203 | cur_end = min(end, start + SZ_512K - 1); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1204 | |
| 1205 | async_cow->end = cur_end; |
| 1206 | INIT_LIST_HEAD(&async_cow->extents); |
| 1207 | |
Liu Bo | 9e0af23 | 2014-08-15 23:36:53 +0800 | [diff] [blame] | 1208 | btrfs_init_work(&async_cow->work, |
| 1209 | btrfs_delalloc_helper, |
| 1210 | async_cow_start, async_cow_submit, |
| 1211 | async_cow_free); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1212 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1213 | nr_pages = (cur_end - start + PAGE_SIZE) >> |
| 1214 | PAGE_SHIFT; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1215 | atomic_add(nr_pages, &fs_info->async_delalloc_pages); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1216 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1217 | btrfs_queue_work(fs_info->delalloc_workers, &async_cow->work); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1218 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1219 | while (atomic_read(&fs_info->async_submit_draining) && |
| 1220 | atomic_read(&fs_info->async_delalloc_pages)) { |
| 1221 | wait_event(fs_info->async_submit_wait, |
| 1222 | (atomic_read(&fs_info->async_delalloc_pages) == |
| 1223 | 0)); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1224 | } |
| 1225 | |
| 1226 | *nr_written += nr_pages; |
| 1227 | start = cur_end + 1; |
| 1228 | } |
| 1229 | *page_started = 1; |
| 1230 | return 0; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1231 | } |
| 1232 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 1233 | 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] | 1234 | u64 bytenr, u64 num_bytes) |
| 1235 | { |
| 1236 | int ret; |
| 1237 | struct btrfs_ordered_sum *sums; |
| 1238 | LIST_HEAD(list); |
| 1239 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1240 | ret = btrfs_lookup_csums_range(fs_info->csum_root, bytenr, |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 1241 | bytenr + num_bytes - 1, &list, 0); |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1242 | if (ret == 0 && list_empty(&list)) |
| 1243 | return 0; |
| 1244 | |
| 1245 | while (!list_empty(&list)) { |
| 1246 | sums = list_entry(list.next, struct btrfs_ordered_sum, list); |
| 1247 | list_del(&sums->list); |
| 1248 | kfree(sums); |
| 1249 | } |
| 1250 | return 1; |
| 1251 | } |
| 1252 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1253 | /* |
| 1254 | * when nowcow writeback call back. This checks for snapshots or COW copies |
| 1255 | * of the extents that exist in the file, and COWs the file as required. |
| 1256 | * |
| 1257 | * If no cow copies or snapshots exist, we write directly to the existing |
| 1258 | * blocks on disk |
| 1259 | */ |
Chris Mason | 7f366cf | 2009-03-12 20:12:45 -0400 | [diff] [blame] | 1260 | static noinline int run_delalloc_nocow(struct inode *inode, |
| 1261 | struct page *locked_page, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1262 | u64 start, u64 end, int *page_started, int force, |
| 1263 | unsigned long *nr_written) |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1264 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1265 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1266 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 1267 | struct extent_buffer *leaf; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1268 | struct btrfs_path *path; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1269 | struct btrfs_file_extent_item *fi; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1270 | struct btrfs_key found_key; |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 1271 | struct extent_map *em; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1272 | u64 cow_start; |
| 1273 | u64 cur_offset; |
| 1274 | u64 extent_end; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1275 | u64 extent_offset; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1276 | u64 disk_bytenr; |
| 1277 | u64 num_bytes; |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 1278 | u64 disk_num_bytes; |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 1279 | u64 ram_bytes; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1280 | int extent_type; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1281 | int ret, err; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1282 | int type; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1283 | int nocow; |
| 1284 | int check_prev = 1; |
Li Zefan | 82d5902 | 2011-04-20 10:33:24 +0800 | [diff] [blame] | 1285 | bool nolock; |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 1286 | u64 ino = btrfs_ino(BTRFS_I(inode)); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1287 | |
| 1288 | path = btrfs_alloc_path(); |
Josef Bacik | 17ca04a | 2012-05-31 15:58:55 -0400 | [diff] [blame] | 1289 | if (!path) { |
Qu Wenruo | ba8b04c | 2016-07-19 16:50:36 +0800 | [diff] [blame] | 1290 | extent_clear_unlock_delalloc(inode, start, end, end, |
| 1291 | locked_page, |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1292 | EXTENT_LOCKED | EXTENT_DELALLOC | |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 1293 | EXTENT_DO_ACCOUNTING | |
| 1294 | EXTENT_DEFRAG, PAGE_UNLOCK | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1295 | PAGE_CLEAR_DIRTY | |
| 1296 | PAGE_SET_WRITEBACK | |
| 1297 | PAGE_END_WRITEBACK); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 1298 | return -ENOMEM; |
Josef Bacik | 17ca04a | 2012-05-31 15:58:55 -0400 | [diff] [blame] | 1299 | } |
Li Zefan | 82d5902 | 2011-04-20 10:33:24 +0800 | [diff] [blame] | 1300 | |
Nikolay Borisov | 70ddc55 | 2017-02-20 13:50:35 +0200 | [diff] [blame] | 1301 | nolock = btrfs_is_free_space_inode(BTRFS_I(inode)); |
Li Zefan | 82d5902 | 2011-04-20 10:33:24 +0800 | [diff] [blame] | 1302 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1303 | cow_start = (u64)-1; |
| 1304 | cur_offset = start; |
| 1305 | while (1) { |
Liu Bo | e4c3b2d | 2017-01-30 12:25:28 -0800 | [diff] [blame] | 1306 | ret = btrfs_lookup_file_extent(NULL, root, path, ino, |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1307 | cur_offset, 0); |
Josef Bacik | d788a34 | 2013-10-25 16:55:08 -0400 | [diff] [blame] | 1308 | if (ret < 0) |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1309 | goto error; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1310 | if (ret > 0 && path->slots[0] > 0 && check_prev) { |
| 1311 | leaf = path->nodes[0]; |
| 1312 | btrfs_item_key_to_cpu(leaf, &found_key, |
| 1313 | path->slots[0] - 1); |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 1314 | if (found_key.objectid == ino && |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1315 | found_key.type == BTRFS_EXTENT_DATA_KEY) |
| 1316 | path->slots[0]--; |
| 1317 | } |
| 1318 | check_prev = 0; |
| 1319 | next_slot: |
| 1320 | leaf = path->nodes[0]; |
| 1321 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { |
| 1322 | ret = btrfs_next_leaf(root, path); |
Josef Bacik | d788a34 | 2013-10-25 16:55:08 -0400 | [diff] [blame] | 1323 | if (ret < 0) |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1324 | goto error; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1325 | if (ret > 0) |
| 1326 | break; |
| 1327 | leaf = path->nodes[0]; |
| 1328 | } |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1329 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1330 | nocow = 0; |
| 1331 | disk_bytenr = 0; |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1332 | num_bytes = 0; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1333 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1334 | |
Filipe Manana | 1d512cb | 2015-11-09 00:33:58 +0000 | [diff] [blame] | 1335 | if (found_key.objectid > ino) |
| 1336 | break; |
| 1337 | if (WARN_ON_ONCE(found_key.objectid < ino) || |
| 1338 | found_key.type < BTRFS_EXTENT_DATA_KEY) { |
| 1339 | path->slots[0]++; |
| 1340 | goto next_slot; |
| 1341 | } |
| 1342 | if (found_key.type > BTRFS_EXTENT_DATA_KEY || |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1343 | found_key.offset > end) |
| 1344 | break; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1345 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1346 | if (found_key.offset > cur_offset) { |
| 1347 | extent_end = found_key.offset; |
Chris Mason | e9061e2 | 2009-10-09 09:57:45 -0400 | [diff] [blame] | 1348 | extent_type = 0; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1349 | goto out_check; |
| 1350 | } |
Chris Mason | c31f883 | 2008-01-08 15:46:31 -0500 | [diff] [blame] | 1351 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1352 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 1353 | struct btrfs_file_extent_item); |
| 1354 | extent_type = btrfs_file_extent_type(leaf, fi); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1355 | |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 1356 | ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1357 | if (extent_type == BTRFS_FILE_EXTENT_REG || |
| 1358 | extent_type == BTRFS_FILE_EXTENT_PREALLOC) { |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1359 | disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1360 | extent_offset = btrfs_file_extent_offset(leaf, fi); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1361 | extent_end = found_key.offset + |
| 1362 | btrfs_file_extent_num_bytes(leaf, fi); |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 1363 | disk_num_bytes = |
| 1364 | btrfs_file_extent_disk_num_bytes(leaf, fi); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1365 | if (extent_end <= start) { |
| 1366 | path->slots[0]++; |
| 1367 | goto next_slot; |
| 1368 | } |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1369 | if (disk_bytenr == 0) |
| 1370 | goto out_check; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1371 | if (btrfs_file_extent_compression(leaf, fi) || |
| 1372 | btrfs_file_extent_encryption(leaf, fi) || |
| 1373 | btrfs_file_extent_other_encoding(leaf, fi)) |
| 1374 | goto out_check; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1375 | if (extent_type == BTRFS_FILE_EXTENT_REG && !force) |
| 1376 | goto out_check; |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 1377 | if (btrfs_extent_readonly(fs_info, disk_bytenr)) |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1378 | goto out_check; |
Liu Bo | e4c3b2d | 2017-01-30 12:25:28 -0800 | [diff] [blame] | 1379 | if (btrfs_cross_ref_exist(root, ino, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1380 | found_key.offset - |
| 1381 | extent_offset, disk_bytenr)) |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1382 | goto out_check; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1383 | disk_bytenr += extent_offset; |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1384 | disk_bytenr += cur_offset - found_key.offset; |
| 1385 | num_bytes = min(end + 1, extent_end) - cur_offset; |
| 1386 | /* |
Wang Shilong | e9894fd | 2014-03-27 11:12:25 +0800 | [diff] [blame] | 1387 | * if there are pending snapshots for this root, |
| 1388 | * we fall into common COW way. |
| 1389 | */ |
| 1390 | if (!nolock) { |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 1391 | err = btrfs_start_write_no_snapshoting(root); |
Wang Shilong | e9894fd | 2014-03-27 11:12:25 +0800 | [diff] [blame] | 1392 | if (!err) |
| 1393 | goto out_check; |
| 1394 | } |
| 1395 | /* |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1396 | * force cow if csum exists in the range. |
| 1397 | * this ensure that csum for a given extent are |
| 1398 | * either valid or do not exist. |
| 1399 | */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 1400 | if (csum_exist_in_range(fs_info, disk_bytenr, |
Robbie Ko | 91e1f56 | 2016-10-07 10:01:29 +0800 | [diff] [blame] | 1401 | num_bytes)) { |
| 1402 | if (!nolock) |
| 1403 | btrfs_end_write_no_snapshoting(root); |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1404 | goto out_check; |
Robbie Ko | 91e1f56 | 2016-10-07 10:01:29 +0800 | [diff] [blame] | 1405 | } |
| 1406 | if (!btrfs_inc_nocow_writers(fs_info, disk_bytenr)) { |
| 1407 | if (!nolock) |
| 1408 | btrfs_end_write_no_snapshoting(root); |
Filipe Manana | f78c436 | 2016-05-09 13:15:41 +0100 | [diff] [blame] | 1409 | goto out_check; |
Robbie Ko | 91e1f56 | 2016-10-07 10:01:29 +0800 | [diff] [blame] | 1410 | } |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1411 | nocow = 1; |
| 1412 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
| 1413 | extent_end = found_key.offset + |
Chris Mason | 514ac8a | 2014-01-03 21:07:00 -0800 | [diff] [blame] | 1414 | btrfs_file_extent_inline_len(leaf, |
| 1415 | path->slots[0], fi); |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 1416 | extent_end = ALIGN(extent_end, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1417 | fs_info->sectorsize); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1418 | } else { |
| 1419 | BUG_ON(1); |
| 1420 | } |
| 1421 | out_check: |
| 1422 | if (extent_end <= start) { |
| 1423 | path->slots[0]++; |
Wang Shilong | e9894fd | 2014-03-27 11:12:25 +0800 | [diff] [blame] | 1424 | if (!nolock && nocow) |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 1425 | btrfs_end_write_no_snapshoting(root); |
Filipe Manana | f78c436 | 2016-05-09 13:15:41 +0100 | [diff] [blame] | 1426 | if (nocow) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1427 | btrfs_dec_nocow_writers(fs_info, disk_bytenr); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1428 | goto next_slot; |
| 1429 | } |
| 1430 | if (!nocow) { |
| 1431 | if (cow_start == (u64)-1) |
| 1432 | cow_start = cur_offset; |
| 1433 | cur_offset = extent_end; |
| 1434 | if (cur_offset > end) |
| 1435 | break; |
| 1436 | path->slots[0]++; |
| 1437 | goto next_slot; |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1438 | } |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1439 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1440 | btrfs_release_path(path); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1441 | if (cow_start != (u64)-1) { |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 1442 | ret = cow_file_range(inode, locked_page, |
| 1443 | cow_start, found_key.offset - 1, |
Wang Xiaoguang | dda3245 | 2016-07-11 11:05:29 +0800 | [diff] [blame] | 1444 | end, page_started, nr_written, 1, |
| 1445 | NULL); |
Wang Shilong | e9894fd | 2014-03-27 11:12:25 +0800 | [diff] [blame] | 1446 | if (ret) { |
| 1447 | if (!nolock && nocow) |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 1448 | btrfs_end_write_no_snapshoting(root); |
Filipe Manana | f78c436 | 2016-05-09 13:15:41 +0100 | [diff] [blame] | 1449 | if (nocow) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1450 | btrfs_dec_nocow_writers(fs_info, |
Filipe Manana | f78c436 | 2016-05-09 13:15:41 +0100 | [diff] [blame] | 1451 | disk_bytenr); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1452 | goto error; |
Wang Shilong | e9894fd | 2014-03-27 11:12:25 +0800 | [diff] [blame] | 1453 | } |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1454 | cow_start = (u64)-1; |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1455 | } |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1456 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1457 | if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) { |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 1458 | u64 orig_start = found_key.offset - extent_offset; |
| 1459 | |
| 1460 | em = create_io_em(inode, cur_offset, num_bytes, |
| 1461 | orig_start, |
| 1462 | disk_bytenr, /* block_start */ |
| 1463 | num_bytes, /* block_len */ |
| 1464 | disk_num_bytes, /* orig_block_len */ |
| 1465 | ram_bytes, BTRFS_COMPRESS_NONE, |
| 1466 | BTRFS_ORDERED_PREALLOC); |
| 1467 | if (IS_ERR(em)) { |
| 1468 | if (!nolock && nocow) |
| 1469 | btrfs_end_write_no_snapshoting(root); |
| 1470 | if (nocow) |
| 1471 | btrfs_dec_nocow_writers(fs_info, |
| 1472 | disk_bytenr); |
| 1473 | ret = PTR_ERR(em); |
| 1474 | goto error; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1475 | } |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 1476 | free_extent_map(em); |
| 1477 | } |
| 1478 | |
| 1479 | if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) { |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1480 | type = BTRFS_ORDERED_PREALLOC; |
| 1481 | } else { |
| 1482 | type = BTRFS_ORDERED_NOCOW; |
| 1483 | } |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1484 | |
| 1485 | ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1486 | num_bytes, num_bytes, type); |
Filipe Manana | f78c436 | 2016-05-09 13:15:41 +0100 | [diff] [blame] | 1487 | if (nocow) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1488 | btrfs_dec_nocow_writers(fs_info, disk_bytenr); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1489 | BUG_ON(ret); /* -ENOMEM */ |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1490 | |
Yan, Zheng | efa5646 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 1491 | if (root->root_key.objectid == |
Qu Wenruo | 4dbd80f | 2017-03-08 10:25:51 +0800 | [diff] [blame] | 1492 | BTRFS_DATA_RELOC_TREE_OBJECTID) |
| 1493 | /* |
| 1494 | * Error handled later, as we must prevent |
| 1495 | * extent_clear_unlock_delalloc() in error handler |
| 1496 | * from freeing metadata of created ordered extent. |
| 1497 | */ |
Yan, Zheng | efa5646 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 1498 | ret = btrfs_reloc_clone_csums(inode, cur_offset, |
| 1499 | num_bytes); |
Yan, Zheng | efa5646 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 1500 | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1501 | extent_clear_unlock_delalloc(inode, cur_offset, |
Qu Wenruo | ba8b04c | 2016-07-19 16:50:36 +0800 | [diff] [blame] | 1502 | cur_offset + num_bytes - 1, end, |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1503 | locked_page, EXTENT_LOCKED | |
Wang Xiaoguang | 1851309 | 2016-07-25 15:51:40 +0800 | [diff] [blame] | 1504 | EXTENT_DELALLOC | |
| 1505 | EXTENT_CLEAR_DATA_RESV, |
| 1506 | PAGE_UNLOCK | PAGE_SET_PRIVATE2); |
| 1507 | |
Wang Shilong | e9894fd | 2014-03-27 11:12:25 +0800 | [diff] [blame] | 1508 | if (!nolock && nocow) |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 1509 | btrfs_end_write_no_snapshoting(root); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1510 | cur_offset = extent_end; |
Qu Wenruo | 4dbd80f | 2017-03-08 10:25:51 +0800 | [diff] [blame] | 1511 | |
| 1512 | /* |
| 1513 | * btrfs_reloc_clone_csums() error, now we're OK to call error |
| 1514 | * handler, as metadata for created ordered extent will only |
| 1515 | * be freed by btrfs_finish_ordered_io(). |
| 1516 | */ |
| 1517 | if (ret) |
| 1518 | goto error; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1519 | if (cur_offset > end) |
| 1520 | break; |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1521 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1522 | btrfs_release_path(path); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1523 | |
Josef Bacik | 17ca04a | 2012-05-31 15:58:55 -0400 | [diff] [blame] | 1524 | if (cur_offset <= end && cow_start == (u64)-1) { |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1525 | cow_start = cur_offset; |
Josef Bacik | 17ca04a | 2012-05-31 15:58:55 -0400 | [diff] [blame] | 1526 | cur_offset = end; |
| 1527 | } |
| 1528 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1529 | if (cow_start != (u64)-1) { |
Wang Xiaoguang | dda3245 | 2016-07-11 11:05:29 +0800 | [diff] [blame] | 1530 | ret = cow_file_range(inode, locked_page, cow_start, end, end, |
| 1531 | page_started, nr_written, 1, NULL); |
Josef Bacik | d788a34 | 2013-10-25 16:55:08 -0400 | [diff] [blame] | 1532 | if (ret) |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1533 | goto error; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1534 | } |
| 1535 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1536 | error: |
Josef Bacik | 17ca04a | 2012-05-31 15:58:55 -0400 | [diff] [blame] | 1537 | if (ret && cur_offset < end) |
Qu Wenruo | ba8b04c | 2016-07-19 16:50:36 +0800 | [diff] [blame] | 1538 | extent_clear_unlock_delalloc(inode, cur_offset, end, end, |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1539 | locked_page, EXTENT_LOCKED | |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 1540 | EXTENT_DELALLOC | EXTENT_DEFRAG | |
| 1541 | EXTENT_DO_ACCOUNTING, PAGE_UNLOCK | |
| 1542 | PAGE_CLEAR_DIRTY | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1543 | PAGE_SET_WRITEBACK | |
| 1544 | PAGE_END_WRITEBACK); |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1545 | btrfs_free_path(path); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1546 | return ret; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1547 | } |
| 1548 | |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 1549 | static inline int need_force_cow(struct inode *inode, u64 start, u64 end) |
| 1550 | { |
| 1551 | |
| 1552 | if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) && |
| 1553 | !(BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC)) |
| 1554 | return 0; |
| 1555 | |
| 1556 | /* |
| 1557 | * @defrag_bytes is a hint value, no spinlock held here, |
| 1558 | * if is not zero, it means the file is defragging. |
| 1559 | * Force cow if given extent needs to be defragged. |
| 1560 | */ |
| 1561 | if (BTRFS_I(inode)->defrag_bytes && |
| 1562 | test_range_bit(&BTRFS_I(inode)->io_tree, start, end, |
| 1563 | EXTENT_DEFRAG, 0, NULL)) |
| 1564 | return 1; |
| 1565 | |
| 1566 | return 0; |
| 1567 | } |
| 1568 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1569 | /* |
| 1570 | * extent_io.c call back to do delayed allocation processing |
| 1571 | */ |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1572 | static int run_delalloc_range(struct inode *inode, struct page *locked_page, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1573 | u64 start, u64 end, int *page_started, |
| 1574 | unsigned long *nr_written) |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1575 | { |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1576 | int ret; |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 1577 | int force_cow = need_force_cow(inode, start, end); |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1578 | |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 1579 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW && !force_cow) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1580 | ret = run_delalloc_nocow(inode, locked_page, start, end, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1581 | page_started, 1, nr_written); |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 1582 | } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC && !force_cow) { |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1583 | ret = run_delalloc_nocow(inode, locked_page, start, end, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1584 | page_started, 0, nr_written); |
Wang Shilong | 7816030 | 2014-07-17 11:44:10 +0800 | [diff] [blame] | 1585 | } else if (!inode_need_compress(inode)) { |
Wang Xiaoguang | dda3245 | 2016-07-11 11:05:29 +0800 | [diff] [blame] | 1586 | ret = cow_file_range(inode, locked_page, start, end, end, |
| 1587 | page_started, nr_written, 1, NULL); |
Josef Bacik | 7ddf5a4 | 2012-06-08 15:26:47 -0400 | [diff] [blame] | 1588 | } else { |
| 1589 | set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, |
| 1590 | &BTRFS_I(inode)->runtime_flags); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1591 | ret = cow_file_range_async(inode, locked_page, start, end, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1592 | page_started, nr_written); |
Josef Bacik | 7ddf5a4 | 2012-06-08 15:26:47 -0400 | [diff] [blame] | 1593 | } |
Qu Wenruo | 52427260 | 2017-03-08 10:25:52 +0800 | [diff] [blame] | 1594 | if (ret) |
| 1595 | btrfs_cleanup_ordered_extents(inode, start, end - start + 1); |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 1596 | return ret; |
| 1597 | } |
| 1598 | |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 1599 | static void btrfs_split_extent_hook(struct inode *inode, |
| 1600 | struct extent_state *orig, u64 split) |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1601 | { |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1602 | u64 size; |
| 1603 | |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1604 | /* not delalloc, ignore it */ |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1605 | if (!(orig->state & EXTENT_DELALLOC)) |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 1606 | return; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1607 | |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1608 | size = orig->end - orig->start + 1; |
| 1609 | if (size > BTRFS_MAX_EXTENT_SIZE) { |
David Sterba | 823bb20 | 2017-01-04 11:09:51 +0100 | [diff] [blame] | 1610 | u32 num_extents; |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1611 | u64 new_size; |
| 1612 | |
| 1613 | /* |
Josef Bacik | ba11721 | 2015-03-13 15:01:24 -0400 | [diff] [blame] | 1614 | * See the explanation in btrfs_merge_extent_hook, the same |
| 1615 | * applies here, just in reverse. |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1616 | */ |
| 1617 | new_size = orig->end - split + 1; |
David Sterba | 823bb20 | 2017-01-04 11:09:51 +0100 | [diff] [blame] | 1618 | num_extents = count_max_extents(new_size); |
Josef Bacik | ba11721 | 2015-03-13 15:01:24 -0400 | [diff] [blame] | 1619 | new_size = split - orig->start; |
David Sterba | 823bb20 | 2017-01-04 11:09:51 +0100 | [diff] [blame] | 1620 | num_extents += count_max_extents(new_size); |
| 1621 | if (count_max_extents(size) >= num_extents) |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1622 | return; |
| 1623 | } |
| 1624 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1625 | spin_lock(&BTRFS_I(inode)->lock); |
| 1626 | BTRFS_I(inode)->outstanding_extents++; |
| 1627 | spin_unlock(&BTRFS_I(inode)->lock); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1628 | } |
| 1629 | |
| 1630 | /* |
| 1631 | * extent_io.c merge_extent_hook, used to track merged delayed allocation |
| 1632 | * extents so we can keep track of new extents that are just merged onto old |
| 1633 | * extents, such as when we are doing sequential writes, so we can properly |
| 1634 | * account for the metadata space we'll need. |
| 1635 | */ |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 1636 | static void btrfs_merge_extent_hook(struct inode *inode, |
| 1637 | struct extent_state *new, |
| 1638 | struct extent_state *other) |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1639 | { |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1640 | u64 new_size, old_size; |
David Sterba | 823bb20 | 2017-01-04 11:09:51 +0100 | [diff] [blame] | 1641 | u32 num_extents; |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1642 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1643 | /* not delalloc, ignore it */ |
| 1644 | if (!(other->state & EXTENT_DELALLOC)) |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 1645 | return; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1646 | |
Josef Bacik | 8461a3d | 2015-03-13 15:12:08 -0400 | [diff] [blame] | 1647 | if (new->start > other->start) |
| 1648 | new_size = new->end - other->start + 1; |
| 1649 | else |
| 1650 | new_size = other->end - new->start + 1; |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1651 | |
| 1652 | /* we're not bigger than the max, unreserve the space and go */ |
| 1653 | if (new_size <= BTRFS_MAX_EXTENT_SIZE) { |
| 1654 | spin_lock(&BTRFS_I(inode)->lock); |
| 1655 | BTRFS_I(inode)->outstanding_extents--; |
| 1656 | spin_unlock(&BTRFS_I(inode)->lock); |
| 1657 | return; |
| 1658 | } |
| 1659 | |
| 1660 | /* |
Josef Bacik | ba11721 | 2015-03-13 15:01:24 -0400 | [diff] [blame] | 1661 | * We have to add up either side to figure out how many extents were |
| 1662 | * accounted for before we merged into one big extent. If the number of |
| 1663 | * extents we accounted for is <= the amount we need for the new range |
| 1664 | * then we can return, otherwise drop. Think of it like this |
| 1665 | * |
| 1666 | * [ 4k][MAX_SIZE] |
| 1667 | * |
| 1668 | * So we've grown the extent by a MAX_SIZE extent, this would mean we |
| 1669 | * need 2 outstanding extents, on one side we have 1 and the other side |
| 1670 | * we have 1 so they are == and we can return. But in this case |
| 1671 | * |
| 1672 | * [MAX_SIZE+4k][MAX_SIZE+4k] |
| 1673 | * |
| 1674 | * Each range on their own accounts for 2 extents, but merged together |
| 1675 | * they are only 3 extents worth of accounting, so we need to drop in |
| 1676 | * this case. |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1677 | */ |
Josef Bacik | ba11721 | 2015-03-13 15:01:24 -0400 | [diff] [blame] | 1678 | old_size = other->end - other->start + 1; |
David Sterba | 823bb20 | 2017-01-04 11:09:51 +0100 | [diff] [blame] | 1679 | num_extents = count_max_extents(old_size); |
Josef Bacik | ba11721 | 2015-03-13 15:01:24 -0400 | [diff] [blame] | 1680 | old_size = new->end - new->start + 1; |
David Sterba | 823bb20 | 2017-01-04 11:09:51 +0100 | [diff] [blame] | 1681 | num_extents += count_max_extents(old_size); |
| 1682 | if (count_max_extents(new_size) >= num_extents) |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1683 | return; |
| 1684 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1685 | spin_lock(&BTRFS_I(inode)->lock); |
| 1686 | BTRFS_I(inode)->outstanding_extents--; |
| 1687 | spin_unlock(&BTRFS_I(inode)->lock); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1688 | } |
| 1689 | |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 1690 | static void btrfs_add_delalloc_inodes(struct btrfs_root *root, |
| 1691 | struct inode *inode) |
| 1692 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1693 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
| 1694 | |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 1695 | spin_lock(&root->delalloc_lock); |
| 1696 | if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) { |
| 1697 | list_add_tail(&BTRFS_I(inode)->delalloc_inodes, |
| 1698 | &root->delalloc_inodes); |
| 1699 | set_bit(BTRFS_INODE_IN_DELALLOC_LIST, |
| 1700 | &BTRFS_I(inode)->runtime_flags); |
| 1701 | root->nr_delalloc_inodes++; |
| 1702 | if (root->nr_delalloc_inodes == 1) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1703 | spin_lock(&fs_info->delalloc_root_lock); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 1704 | BUG_ON(!list_empty(&root->delalloc_root)); |
| 1705 | list_add_tail(&root->delalloc_root, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1706 | &fs_info->delalloc_roots); |
| 1707 | spin_unlock(&fs_info->delalloc_root_lock); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 1708 | } |
| 1709 | } |
| 1710 | spin_unlock(&root->delalloc_lock); |
| 1711 | } |
| 1712 | |
| 1713 | static void btrfs_del_delalloc_inode(struct btrfs_root *root, |
Nikolay Borisov | 9e3e97f | 2017-02-20 13:51:07 +0200 | [diff] [blame] | 1714 | struct btrfs_inode *inode) |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 1715 | { |
Nikolay Borisov | 9e3e97f | 2017-02-20 13:51:07 +0200 | [diff] [blame] | 1716 | 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] | 1717 | |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 1718 | spin_lock(&root->delalloc_lock); |
Nikolay Borisov | 9e3e97f | 2017-02-20 13:51:07 +0200 | [diff] [blame] | 1719 | if (!list_empty(&inode->delalloc_inodes)) { |
| 1720 | list_del_init(&inode->delalloc_inodes); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 1721 | clear_bit(BTRFS_INODE_IN_DELALLOC_LIST, |
Nikolay Borisov | 9e3e97f | 2017-02-20 13:51:07 +0200 | [diff] [blame] | 1722 | &inode->runtime_flags); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 1723 | root->nr_delalloc_inodes--; |
| 1724 | if (!root->nr_delalloc_inodes) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1725 | spin_lock(&fs_info->delalloc_root_lock); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 1726 | BUG_ON(list_empty(&root->delalloc_root)); |
| 1727 | list_del_init(&root->delalloc_root); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1728 | spin_unlock(&fs_info->delalloc_root_lock); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 1729 | } |
| 1730 | } |
| 1731 | spin_unlock(&root->delalloc_lock); |
| 1732 | } |
| 1733 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1734 | /* |
| 1735 | * extent_io.c set_bit_hook, used to track delayed allocation |
| 1736 | * bytes in this file, and to maintain the list of inodes that |
| 1737 | * have pending delalloc work to be done. |
| 1738 | */ |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 1739 | static void btrfs_set_bit_hook(struct inode *inode, |
David Sterba | 9ee49a04 | 2015-01-14 19:52:13 +0100 | [diff] [blame] | 1740 | struct extent_state *state, unsigned *bits) |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1741 | { |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1742 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1743 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
| 1744 | |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 1745 | if ((*bits & EXTENT_DEFRAG) && !(*bits & EXTENT_DELALLOC)) |
| 1746 | WARN_ON(1); |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1747 | /* |
| 1748 | * set_bit and clear bit hooks normally require _irqsave/restore |
Sergei Trofimovich | 27160b6 | 2011-05-20 20:20:32 +0000 | [diff] [blame] | 1749 | * but in this case, we are only testing for the DELALLOC |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1750 | * bit, which is only set or cleared with irqs on |
| 1751 | */ |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1752 | if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) { |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1753 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1754 | u64 len = state->end + 1 - state->start; |
Nikolay Borisov | 70ddc55 | 2017-02-20 13:50:35 +0200 | [diff] [blame] | 1755 | bool do_list = !btrfs_is_free_space_inode(BTRFS_I(inode)); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1756 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1757 | if (*bits & EXTENT_FIRST_DELALLOC) { |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1758 | *bits &= ~EXTENT_FIRST_DELALLOC; |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1759 | } else { |
| 1760 | spin_lock(&BTRFS_I(inode)->lock); |
| 1761 | BTRFS_I(inode)->outstanding_extents++; |
| 1762 | spin_unlock(&BTRFS_I(inode)->lock); |
| 1763 | } |
Josef Bacik | 287a0ab | 2010-03-19 18:07:23 +0000 | [diff] [blame] | 1764 | |
Josef Bacik | 6a3891c | 2015-03-16 17:38:52 -0400 | [diff] [blame] | 1765 | /* For sanity tests */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1766 | if (btrfs_is_testing(fs_info)) |
Josef Bacik | 6a3891c | 2015-03-16 17:38:52 -0400 | [diff] [blame] | 1767 | return; |
| 1768 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1769 | __percpu_counter_add(&fs_info->delalloc_bytes, len, |
| 1770 | fs_info->delalloc_batch); |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 1771 | spin_lock(&BTRFS_I(inode)->lock); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1772 | BTRFS_I(inode)->delalloc_bytes += len; |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 1773 | if (*bits & EXTENT_DEFRAG) |
| 1774 | BTRFS_I(inode)->defrag_bytes += len; |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 1775 | if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST, |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 1776 | &BTRFS_I(inode)->runtime_flags)) |
| 1777 | btrfs_add_delalloc_inodes(root, inode); |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 1778 | spin_unlock(&BTRFS_I(inode)->lock); |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1779 | } |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 1780 | |
| 1781 | if (!(state->state & EXTENT_DELALLOC_NEW) && |
| 1782 | (*bits & EXTENT_DELALLOC_NEW)) { |
| 1783 | spin_lock(&BTRFS_I(inode)->lock); |
| 1784 | BTRFS_I(inode)->new_delalloc_bytes += state->end + 1 - |
| 1785 | state->start; |
| 1786 | spin_unlock(&BTRFS_I(inode)->lock); |
| 1787 | } |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1788 | } |
| 1789 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1790 | /* |
| 1791 | * extent_io.c clear_bit_hook, see set_bit_hook for why |
| 1792 | */ |
Nikolay Borisov | 6fc0ef6 | 2017-02-20 13:51:03 +0200 | [diff] [blame] | 1793 | static void btrfs_clear_bit_hook(struct btrfs_inode *inode, |
David Sterba | 4107488 | 2013-04-29 13:38:46 +0000 | [diff] [blame] | 1794 | struct extent_state *state, |
David Sterba | 9ee49a04 | 2015-01-14 19:52:13 +0100 | [diff] [blame] | 1795 | unsigned *bits) |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1796 | { |
Nikolay Borisov | 6fc0ef6 | 2017-02-20 13:51:03 +0200 | [diff] [blame] | 1797 | 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] | 1798 | u64 len = state->end + 1 - state->start; |
David Sterba | 823bb20 | 2017-01-04 11:09:51 +0100 | [diff] [blame] | 1799 | u32 num_extents = count_max_extents(len); |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 1800 | |
Nikolay Borisov | 6fc0ef6 | 2017-02-20 13:51:03 +0200 | [diff] [blame] | 1801 | spin_lock(&inode->lock); |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 1802 | if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG)) |
Nikolay Borisov | 6fc0ef6 | 2017-02-20 13:51:03 +0200 | [diff] [blame] | 1803 | inode->defrag_bytes -= len; |
| 1804 | spin_unlock(&inode->lock); |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 1805 | |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1806 | /* |
| 1807 | * set_bit and clear bit hooks normally require _irqsave/restore |
Sergei Trofimovich | 27160b6 | 2011-05-20 20:20:32 +0000 | [diff] [blame] | 1808 | * but in this case, we are only testing for the DELALLOC |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1809 | * bit, which is only set or cleared with irqs on |
| 1810 | */ |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1811 | if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) { |
Nikolay Borisov | 6fc0ef6 | 2017-02-20 13:51:03 +0200 | [diff] [blame] | 1812 | struct btrfs_root *root = inode->root; |
Liu Bo | 83eea1f | 2012-07-10 05:28:39 -0600 | [diff] [blame] | 1813 | bool do_list = !btrfs_is_free_space_inode(inode); |
Chris Mason | bcbfce8 | 2008-04-22 13:26:47 -0400 | [diff] [blame] | 1814 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1815 | if (*bits & EXTENT_FIRST_DELALLOC) { |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1816 | *bits &= ~EXTENT_FIRST_DELALLOC; |
Filipe Manana | a315e68 | 2017-03-06 23:04:20 +0000 | [diff] [blame] | 1817 | } else if (!(*bits & EXTENT_CLEAR_META_RESV)) { |
Nikolay Borisov | 6fc0ef6 | 2017-02-20 13:51:03 +0200 | [diff] [blame] | 1818 | spin_lock(&inode->lock); |
| 1819 | inode->outstanding_extents -= num_extents; |
| 1820 | spin_unlock(&inode->lock); |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1821 | } |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1822 | |
Josef Bacik | b6d08f0 | 2013-09-27 14:57:43 -0400 | [diff] [blame] | 1823 | /* |
| 1824 | * We don't reserve metadata space for space cache inodes so we |
| 1825 | * don't need to call dellalloc_release_metadata if there is an |
| 1826 | * error. |
| 1827 | */ |
Filipe Manana | a315e68 | 2017-03-06 23:04:20 +0000 | [diff] [blame] | 1828 | if (*bits & EXTENT_CLEAR_META_RESV && |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1829 | root != fs_info->tree_root) |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1830 | btrfs_delalloc_release_metadata(inode, len); |
| 1831 | |
Josef Bacik | 6a3891c | 2015-03-16 17:38:52 -0400 | [diff] [blame] | 1832 | /* For sanity tests. */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1833 | if (btrfs_is_testing(fs_info)) |
Josef Bacik | 6a3891c | 2015-03-16 17:38:52 -0400 | [diff] [blame] | 1834 | return; |
| 1835 | |
Filipe Manana | a315e68 | 2017-03-06 23:04:20 +0000 | [diff] [blame] | 1836 | if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID && |
| 1837 | do_list && !(state->state & EXTENT_NORESERVE) && |
| 1838 | (*bits & EXTENT_CLEAR_DATA_RESV)) |
Nikolay Borisov | 6fc0ef6 | 2017-02-20 13:51:03 +0200 | [diff] [blame] | 1839 | btrfs_free_reserved_data_space_noquota( |
| 1840 | &inode->vfs_inode, |
Qu Wenruo | 51773be | 2015-10-08 18:19:37 +0800 | [diff] [blame] | 1841 | state->start, len); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1842 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1843 | __percpu_counter_add(&fs_info->delalloc_bytes, -len, |
| 1844 | fs_info->delalloc_batch); |
Nikolay Borisov | 6fc0ef6 | 2017-02-20 13:51:03 +0200 | [diff] [blame] | 1845 | spin_lock(&inode->lock); |
| 1846 | inode->delalloc_bytes -= len; |
| 1847 | if (do_list && inode->delalloc_bytes == 0 && |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 1848 | test_bit(BTRFS_INODE_IN_DELALLOC_LIST, |
Nikolay Borisov | 9e3e97f | 2017-02-20 13:51:07 +0200 | [diff] [blame] | 1849 | &inode->runtime_flags)) |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 1850 | btrfs_del_delalloc_inode(root, inode); |
Nikolay Borisov | 6fc0ef6 | 2017-02-20 13:51:03 +0200 | [diff] [blame] | 1851 | spin_unlock(&inode->lock); |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1852 | } |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 1853 | |
| 1854 | if ((state->state & EXTENT_DELALLOC_NEW) && |
| 1855 | (*bits & EXTENT_DELALLOC_NEW)) { |
| 1856 | spin_lock(&inode->lock); |
| 1857 | ASSERT(inode->new_delalloc_bytes >= len); |
| 1858 | inode->new_delalloc_bytes -= len; |
| 1859 | spin_unlock(&inode->lock); |
| 1860 | } |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1861 | } |
| 1862 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1863 | /* |
| 1864 | * extent_io.c merge_bio_hook, this must check the chunk tree to make sure |
| 1865 | * we don't create bios that span stripes or chunks |
Liu Bo | 6f034ec | 2016-06-22 18:31:49 -0700 | [diff] [blame] | 1866 | * |
| 1867 | * return 1 if page cannot be merged to bio |
| 1868 | * return 0 if page can be merged to bio |
| 1869 | * return error otherwise |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1870 | */ |
Mike Christie | 81a75f6 | 2016-06-05 14:31:54 -0500 | [diff] [blame] | 1871 | int btrfs_merge_bio_hook(struct page *page, unsigned long offset, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1872 | size_t size, struct bio *bio, |
| 1873 | unsigned long bio_flags) |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1874 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1875 | struct inode *inode = page->mapping->host; |
| 1876 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 1877 | u64 logical = (u64)bio->bi_iter.bi_sector << 9; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1878 | u64 length = 0; |
| 1879 | u64 map_length; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1880 | int ret; |
| 1881 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1882 | if (bio_flags & EXTENT_BIO_COMPRESSED) |
| 1883 | return 0; |
| 1884 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 1885 | length = bio->bi_iter.bi_size; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1886 | map_length = length; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1887 | ret = btrfs_map_block(fs_info, btrfs_op(bio), logical, &map_length, |
| 1888 | NULL, 0); |
Liu Bo | 6f034ec | 2016-06-22 18:31:49 -0700 | [diff] [blame] | 1889 | if (ret < 0) |
| 1890 | return ret; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1891 | if (map_length < length + size) |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1892 | return 1; |
Jeff Mahoney | 3444a97 | 2011-10-03 23:23:13 -0400 | [diff] [blame] | 1893 | return 0; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1894 | } |
| 1895 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1896 | /* |
| 1897 | * in order to insert checksums into the metadata in large chunks, |
| 1898 | * we wait until bio submission time. All the pages in the bio are |
| 1899 | * checksummed and sums are attached onto the ordered extent record. |
| 1900 | * |
| 1901 | * At IO completion time the cums attached on the ordered extent record |
| 1902 | * are inserted into the btree |
| 1903 | */ |
Mike Christie | 81a75f6 | 2016-06-05 14:31:54 -0500 | [diff] [blame] | 1904 | static int __btrfs_submit_bio_start(struct inode *inode, struct bio *bio, |
| 1905 | int mirror_num, unsigned long bio_flags, |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 1906 | u64 bio_offset) |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 1907 | { |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 1908 | int ret = 0; |
Chris Mason | e015640 | 2008-04-16 11:15:20 -0400 | [diff] [blame] | 1909 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 1910 | ret = btrfs_csum_one_bio(inode, bio, 0, 0); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1911 | BUG_ON(ret); /* -ENOMEM */ |
Chris Mason | 4a69a41 | 2008-11-06 22:03:00 -0500 | [diff] [blame] | 1912 | return 0; |
| 1913 | } |
Chris Mason | e015640 | 2008-04-16 11:15:20 -0400 | [diff] [blame] | 1914 | |
Chris Mason | 4a69a41 | 2008-11-06 22:03:00 -0500 | [diff] [blame] | 1915 | /* |
| 1916 | * in order to insert checksums into the metadata in large chunks, |
| 1917 | * we wait until bio submission time. All the pages in the bio are |
| 1918 | * checksummed and sums are attached onto the ordered extent record. |
| 1919 | * |
| 1920 | * At IO completion time the cums attached on the ordered extent record |
| 1921 | * are inserted into the btree |
| 1922 | */ |
Mike Christie | 81a75f6 | 2016-06-05 14:31:54 -0500 | [diff] [blame] | 1923 | static int __btrfs_submit_bio_done(struct inode *inode, struct bio *bio, |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 1924 | int mirror_num, unsigned long bio_flags, |
| 1925 | u64 bio_offset) |
Chris Mason | 4a69a41 | 2008-11-06 22:03:00 -0500 | [diff] [blame] | 1926 | { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 1927 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 1928 | int ret; |
| 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) { |
| 1932 | bio->bi_error = ret; |
| 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 | */ |
Mike Christie | 81a75f6 | 2016-06-05 14:31:54 -0500 | [diff] [blame] | 1942 | static int btrfs_submit_bio_hook(struct inode *inode, struct bio *bio, |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -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 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1946 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 1947 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chandan Rajendra | 0d51e28 | 2015-07-27 15:26:43 +0530 | [diff] [blame] | 1948 | enum btrfs_wq_endio_type metadata = BTRFS_WQ_ENDIO_DATA; |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 1949 | int ret = 0; |
Chris Mason | 19b9bdb | 2008-10-30 14:23:13 -0400 | [diff] [blame] | 1950 | int skip_sum; |
Josef Bacik | b812ce2 | 2012-11-16 13:56:32 -0500 | [diff] [blame] | 1951 | int async = !atomic_read(&BTRFS_I(inode)->sync_writers); |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 1952 | |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 1953 | skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM; |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1954 | |
Nikolay Borisov | 70ddc55 | 2017-02-20 13:50:35 +0200 | [diff] [blame] | 1955 | if (btrfs_is_free_space_inode(BTRFS_I(inode))) |
Chandan Rajendra | 0d51e28 | 2015-07-27 15:26:43 +0530 | [diff] [blame] | 1956 | metadata = BTRFS_WQ_ENDIO_FREE_SPACE; |
Jeff Mahoney | 0417341 | 2011-10-03 23:23:12 -0400 | [diff] [blame] | 1957 | |
Mike Christie | 37226b2 | 2016-06-05 14:31:52 -0500 | [diff] [blame] | 1958 | if (bio_op(bio) != REQ_OP_WRITE) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1959 | ret = btrfs_bio_wq_end_io(fs_info, bio, metadata); |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 1960 | if (ret) |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 1961 | goto out; |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 1962 | |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1963 | if (bio_flags & EXTENT_BIO_COMPRESSED) { |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 1964 | ret = btrfs_submit_compressed_read(inode, bio, |
| 1965 | mirror_num, |
| 1966 | bio_flags); |
| 1967 | goto out; |
Tsutomu Itoh | c2db107 | 2011-03-01 06:48:31 +0000 | [diff] [blame] | 1968 | } else if (!skip_sum) { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 1969 | ret = btrfs_lookup_bio_sums(inode, bio, NULL); |
Tsutomu Itoh | c2db107 | 2011-03-01 06:48:31 +0000 | [diff] [blame] | 1970 | if (ret) |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 1971 | goto out; |
Tsutomu Itoh | c2db107 | 2011-03-01 06:48:31 +0000 | [diff] [blame] | 1972 | } |
Chris Mason | 4d1b5fb | 2008-08-20 09:44:52 -0400 | [diff] [blame] | 1973 | goto mapit; |
Josef Bacik | b812ce2 | 2012-11-16 13:56:32 -0500 | [diff] [blame] | 1974 | } else if (async && !skip_sum) { |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1975 | /* csum items have already been cloned */ |
| 1976 | if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID) |
| 1977 | goto mapit; |
Chris Mason | 19b9bdb | 2008-10-30 14:23:13 -0400 | [diff] [blame] | 1978 | /* we're doing a write, do the async checksumming */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1979 | ret = btrfs_wq_submit_bio(fs_info, inode, bio, mirror_num, |
| 1980 | bio_flags, bio_offset, |
| 1981 | __btrfs_submit_bio_start, |
| 1982 | __btrfs_submit_bio_done); |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 1983 | goto out; |
Josef Bacik | b812ce2 | 2012-11-16 13:56:32 -0500 | [diff] [blame] | 1984 | } else if (!skip_sum) { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 1985 | ret = btrfs_csum_one_bio(inode, bio, 0, 0); |
Josef Bacik | b812ce2 | 2012-11-16 13:56:32 -0500 | [diff] [blame] | 1986 | if (ret) |
| 1987 | goto out; |
Chris Mason | 19b9bdb | 2008-10-30 14:23:13 -0400 | [diff] [blame] | 1988 | } |
| 1989 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1990 | mapit: |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 1991 | ret = btrfs_map_bio(fs_info, bio, mirror_num, 0); |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 1992 | |
| 1993 | out: |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 1994 | if (ret < 0) { |
| 1995 | bio->bi_error = ret; |
| 1996 | bio_endio(bio); |
| 1997 | } |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 1998 | return ret; |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 1999 | } |
Chris Mason | 6885f30 | 2008-02-20 16:11:05 -0500 | [diff] [blame] | 2000 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2001 | /* |
| 2002 | * given a list of ordered sums record them in the inode. This happens |
| 2003 | * at IO completion time based on sums calculated at bio submission time. |
| 2004 | */ |
Chris Mason | ba1da2f | 2008-07-17 12:54:15 -0400 | [diff] [blame] | 2005 | static noinline int add_pending_csums(struct btrfs_trans_handle *trans, |
David Sterba | df9f628 | 2017-02-10 19:35:37 +0100 | [diff] [blame] | 2006 | struct inode *inode, struct list_head *list) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2007 | { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2008 | struct btrfs_ordered_sum *sum; |
| 2009 | |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 2010 | list_for_each_entry(sum, list, list) { |
Miao Xie | 39847c4 | 2013-03-28 08:08:20 +0000 | [diff] [blame] | 2011 | trans->adding_csums = 1; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 2012 | btrfs_csum_file_blocks(trans, |
| 2013 | BTRFS_I(inode)->root->fs_info->csum_root, sum); |
Miao Xie | 39847c4 | 2013-03-28 08:08:20 +0000 | [diff] [blame] | 2014 | trans->adding_csums = 0; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2015 | } |
| 2016 | return 0; |
| 2017 | } |
| 2018 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 2019 | int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end, |
Qu Wenruo | ba8b04c | 2016-07-19 16:50:36 +0800 | [diff] [blame] | 2020 | struct extent_state **cached_state, int dedupe) |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 2021 | { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2022 | WARN_ON((end & (PAGE_SIZE - 1)) == 0); |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 2023 | return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end, |
David Sterba | 7cd8c75 | 2016-04-26 23:54:39 +0200 | [diff] [blame] | 2024 | cached_state); |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 2025 | } |
| 2026 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2027 | /* see btrfs_writepage_start_hook for details on why this is required */ |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2028 | struct btrfs_writepage_fixup { |
| 2029 | struct page *page; |
| 2030 | struct btrfs_work work; |
| 2031 | }; |
| 2032 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 2033 | static void btrfs_writepage_fixup_worker(struct btrfs_work *work) |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2034 | { |
| 2035 | struct btrfs_writepage_fixup *fixup; |
| 2036 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 2037 | struct extent_state *cached_state = NULL; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2038 | struct page *page; |
| 2039 | struct inode *inode; |
| 2040 | u64 page_start; |
| 2041 | u64 page_end; |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 2042 | int ret; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2043 | |
| 2044 | fixup = container_of(work, struct btrfs_writepage_fixup, work); |
| 2045 | page = fixup->page; |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 2046 | again: |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2047 | lock_page(page); |
| 2048 | if (!page->mapping || !PageDirty(page) || !PageChecked(page)) { |
| 2049 | ClearPageChecked(page); |
| 2050 | goto out_page; |
| 2051 | } |
| 2052 | |
| 2053 | inode = page->mapping->host; |
| 2054 | page_start = page_offset(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2055 | page_end = page_offset(page) + PAGE_SIZE - 1; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2056 | |
David Sterba | ff13db4 | 2015-12-03 14:30:40 +0100 | [diff] [blame] | 2057 | lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end, |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 2058 | &cached_state); |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 2059 | |
| 2060 | /* already ordered? We're done */ |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 2061 | if (PagePrivate2(page)) |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2062 | goto out; |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 2063 | |
Nikolay Borisov | a776c6f | 2017-02-20 13:50:49 +0200 | [diff] [blame] | 2064 | ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start, |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2065 | PAGE_SIZE); |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 2066 | if (ordered) { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 2067 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, |
| 2068 | page_end, &cached_state, GFP_NOFS); |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 2069 | unlock_page(page); |
| 2070 | btrfs_start_ordered_extent(inode, ordered, 1); |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 2071 | btrfs_put_ordered_extent(ordered); |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 2072 | goto again; |
| 2073 | } |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2074 | |
Qu Wenruo | 7cf5b97 | 2015-09-08 17:25:55 +0800 | [diff] [blame] | 2075 | ret = btrfs_delalloc_reserve_space(inode, page_start, |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2076 | PAGE_SIZE); |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 2077 | if (ret) { |
| 2078 | mapping_set_error(page->mapping, ret); |
| 2079 | end_extent_writepage(page, ret, page_start, page_end); |
| 2080 | ClearPageChecked(page); |
| 2081 | goto out; |
| 2082 | } |
| 2083 | |
Qu Wenruo | ba8b04c | 2016-07-19 16:50:36 +0800 | [diff] [blame] | 2084 | btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state, |
| 2085 | 0); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2086 | ClearPageChecked(page); |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 2087 | set_page_dirty(page); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2088 | out: |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 2089 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end, |
| 2090 | &cached_state, GFP_NOFS); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2091 | out_page: |
| 2092 | unlock_page(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2093 | put_page(page); |
Miao Xie | b897abe | 2011-01-26 16:19:22 +0800 | [diff] [blame] | 2094 | kfree(fixup); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2095 | } |
| 2096 | |
| 2097 | /* |
| 2098 | * There are a few paths in the higher layers of the kernel that directly |
| 2099 | * set the page dirty bit without asking the filesystem if it is a |
| 2100 | * good idea. This causes problems because we want to make sure COW |
| 2101 | * properly happens and the data=ordered rules are followed. |
| 2102 | * |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2103 | * 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] | 2104 | * hasn't been properly setup for IO. We kick off an async process |
| 2105 | * to fix it up. The async helper will wait for ordered extents, set |
| 2106 | * the delalloc bit and make it safe to write the page. |
| 2107 | */ |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 2108 | 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] | 2109 | { |
| 2110 | struct inode *inode = page->mapping->host; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2111 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2112 | struct btrfs_writepage_fixup *fixup; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2113 | |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 2114 | /* this page is properly in the ordered list */ |
| 2115 | if (TestClearPagePrivate2(page)) |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2116 | return 0; |
| 2117 | |
| 2118 | if (PageChecked(page)) |
| 2119 | return -EAGAIN; |
| 2120 | |
| 2121 | fixup = kzalloc(sizeof(*fixup), GFP_NOFS); |
| 2122 | if (!fixup) |
| 2123 | return -EAGAIN; |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 2124 | |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2125 | SetPageChecked(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2126 | get_page(page); |
Liu Bo | 9e0af23 | 2014-08-15 23:36:53 +0800 | [diff] [blame] | 2127 | btrfs_init_work(&fixup->work, btrfs_fixup_helper, |
| 2128 | btrfs_writepage_fixup_worker, NULL, NULL); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2129 | fixup->page = page; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2130 | btrfs_queue_work(fs_info->fixup_workers, &fixup->work); |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 2131 | return -EBUSY; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2132 | } |
| 2133 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2134 | static int insert_reserved_file_extent(struct btrfs_trans_handle *trans, |
| 2135 | struct inode *inode, u64 file_pos, |
| 2136 | u64 disk_bytenr, u64 disk_num_bytes, |
| 2137 | u64 num_bytes, u64 ram_bytes, |
| 2138 | u8 compression, u8 encryption, |
| 2139 | u16 other_encoding, int extent_type) |
| 2140 | { |
| 2141 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 2142 | struct btrfs_file_extent_item *fi; |
| 2143 | struct btrfs_path *path; |
| 2144 | struct extent_buffer *leaf; |
| 2145 | struct btrfs_key ins; |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 2146 | int extent_inserted = 0; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2147 | int ret; |
| 2148 | |
| 2149 | path = btrfs_alloc_path(); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 2150 | if (!path) |
| 2151 | return -ENOMEM; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2152 | |
Chris Mason | a1ed835 | 2009-09-11 12:27:37 -0400 | [diff] [blame] | 2153 | /* |
| 2154 | * we may be replacing one extent in the tree with another. |
| 2155 | * The new extent is pinned in the extent map, and we don't want |
| 2156 | * to drop it from the cache until it is completely in the btree. |
| 2157 | * |
| 2158 | * So, tell btrfs_drop_extents to leave this extent in the cache. |
| 2159 | * the caller is expected to unpin it and allow it to be merged |
| 2160 | * with the others. |
| 2161 | */ |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 2162 | ret = __btrfs_drop_extents(trans, root, inode, path, file_pos, |
| 2163 | file_pos + num_bytes, NULL, 0, |
| 2164 | 1, sizeof(*fi), &extent_inserted); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2165 | if (ret) |
| 2166 | goto out; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2167 | |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 2168 | if (!extent_inserted) { |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 2169 | ins.objectid = btrfs_ino(BTRFS_I(inode)); |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 2170 | ins.offset = file_pos; |
| 2171 | ins.type = BTRFS_EXTENT_DATA_KEY; |
| 2172 | |
| 2173 | path->leave_spinning = 1; |
| 2174 | ret = btrfs_insert_empty_item(trans, root, path, &ins, |
| 2175 | sizeof(*fi)); |
| 2176 | if (ret) |
| 2177 | goto out; |
| 2178 | } |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2179 | leaf = path->nodes[0]; |
| 2180 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 2181 | struct btrfs_file_extent_item); |
| 2182 | btrfs_set_file_extent_generation(leaf, fi, trans->transid); |
| 2183 | btrfs_set_file_extent_type(leaf, fi, extent_type); |
| 2184 | btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr); |
| 2185 | btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes); |
| 2186 | btrfs_set_file_extent_offset(leaf, fi, 0); |
| 2187 | btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes); |
| 2188 | btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes); |
| 2189 | btrfs_set_file_extent_compression(leaf, fi, compression); |
| 2190 | btrfs_set_file_extent_encryption(leaf, fi, encryption); |
| 2191 | btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 2192 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2193 | btrfs_mark_buffer_dirty(leaf); |
Josef Bacik | ce19533 | 2012-09-25 15:26:16 -0400 | [diff] [blame] | 2194 | btrfs_release_path(path); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2195 | |
| 2196 | inode_add_bytes(inode, num_bytes); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2197 | |
| 2198 | ins.objectid = disk_bytenr; |
| 2199 | ins.offset = disk_num_bytes; |
| 2200 | ins.type = BTRFS_EXTENT_ITEM_KEY; |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2201 | ret = btrfs_alloc_reserved_file_extent(trans, root->root_key.objectid, |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 2202 | btrfs_ino(BTRFS_I(inode)), file_pos, ram_bytes, &ins); |
Qu Wenruo | 297d750 | 2015-09-08 17:08:37 +0800 | [diff] [blame] | 2203 | /* |
Qu Wenruo | 5846a3c | 2015-10-26 14:11:18 +0800 | [diff] [blame] | 2204 | * Release the reserved range from inode dirty range map, as it is |
| 2205 | * already moved into delayed_ref_head |
Qu Wenruo | 297d750 | 2015-09-08 17:08:37 +0800 | [diff] [blame] | 2206 | */ |
| 2207 | btrfs_qgroup_release_data(inode, file_pos, ram_bytes); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2208 | out: |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2209 | btrfs_free_path(path); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 2210 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2211 | return ret; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2212 | } |
| 2213 | |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2214 | /* snapshot-aware defrag */ |
| 2215 | struct sa_defrag_extent_backref { |
| 2216 | struct rb_node node; |
| 2217 | struct old_sa_defrag_extent *old; |
| 2218 | u64 root_id; |
| 2219 | u64 inum; |
| 2220 | u64 file_pos; |
| 2221 | u64 extent_offset; |
| 2222 | u64 num_bytes; |
| 2223 | u64 generation; |
| 2224 | }; |
| 2225 | |
| 2226 | struct old_sa_defrag_extent { |
| 2227 | struct list_head list; |
| 2228 | struct new_sa_defrag_extent *new; |
| 2229 | |
| 2230 | u64 extent_offset; |
| 2231 | u64 bytenr; |
| 2232 | u64 offset; |
| 2233 | u64 len; |
| 2234 | int count; |
| 2235 | }; |
| 2236 | |
| 2237 | struct new_sa_defrag_extent { |
| 2238 | struct rb_root root; |
| 2239 | struct list_head head; |
| 2240 | struct btrfs_path *path; |
| 2241 | struct inode *inode; |
| 2242 | u64 file_pos; |
| 2243 | u64 len; |
| 2244 | u64 bytenr; |
| 2245 | u64 disk_len; |
| 2246 | u8 compress_type; |
| 2247 | }; |
| 2248 | |
| 2249 | static int backref_comp(struct sa_defrag_extent_backref *b1, |
| 2250 | struct sa_defrag_extent_backref *b2) |
| 2251 | { |
| 2252 | if (b1->root_id < b2->root_id) |
| 2253 | return -1; |
| 2254 | else if (b1->root_id > b2->root_id) |
| 2255 | return 1; |
| 2256 | |
| 2257 | if (b1->inum < b2->inum) |
| 2258 | return -1; |
| 2259 | else if (b1->inum > b2->inum) |
| 2260 | return 1; |
| 2261 | |
| 2262 | if (b1->file_pos < b2->file_pos) |
| 2263 | return -1; |
| 2264 | else if (b1->file_pos > b2->file_pos) |
| 2265 | return 1; |
| 2266 | |
| 2267 | /* |
| 2268 | * [------------------------------] ===> (a range of space) |
| 2269 | * |<--->| |<---->| =============> (fs/file tree A) |
| 2270 | * |<---------------------------->| ===> (fs/file tree B) |
| 2271 | * |
| 2272 | * A range of space can refer to two file extents in one tree while |
| 2273 | * refer to only one file extent in another tree. |
| 2274 | * |
| 2275 | * So we may process a disk offset more than one time(two extents in A) |
| 2276 | * and locate at the same extent(one extent in B), then insert two same |
| 2277 | * backrefs(both refer to the extent in B). |
| 2278 | */ |
| 2279 | return 0; |
| 2280 | } |
| 2281 | |
| 2282 | static void backref_insert(struct rb_root *root, |
| 2283 | struct sa_defrag_extent_backref *backref) |
| 2284 | { |
| 2285 | struct rb_node **p = &root->rb_node; |
| 2286 | struct rb_node *parent = NULL; |
| 2287 | struct sa_defrag_extent_backref *entry; |
| 2288 | int ret; |
| 2289 | |
| 2290 | while (*p) { |
| 2291 | parent = *p; |
| 2292 | entry = rb_entry(parent, struct sa_defrag_extent_backref, node); |
| 2293 | |
| 2294 | ret = backref_comp(backref, entry); |
| 2295 | if (ret < 0) |
| 2296 | p = &(*p)->rb_left; |
| 2297 | else |
| 2298 | p = &(*p)->rb_right; |
| 2299 | } |
| 2300 | |
| 2301 | rb_link_node(&backref->node, parent, p); |
| 2302 | rb_insert_color(&backref->node, root); |
| 2303 | } |
| 2304 | |
| 2305 | /* |
| 2306 | * Note the backref might has changed, and in this case we just return 0. |
| 2307 | */ |
| 2308 | static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id, |
| 2309 | void *ctx) |
| 2310 | { |
| 2311 | struct btrfs_file_extent_item *extent; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2312 | struct old_sa_defrag_extent *old = ctx; |
| 2313 | struct new_sa_defrag_extent *new = old->new; |
| 2314 | struct btrfs_path *path = new->path; |
| 2315 | struct btrfs_key key; |
| 2316 | struct btrfs_root *root; |
| 2317 | struct sa_defrag_extent_backref *backref; |
| 2318 | struct extent_buffer *leaf; |
| 2319 | struct inode *inode = new->inode; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2320 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2321 | int slot; |
| 2322 | int ret; |
| 2323 | u64 extent_offset; |
| 2324 | u64 num_bytes; |
| 2325 | |
| 2326 | if (BTRFS_I(inode)->root->root_key.objectid == root_id && |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 2327 | inum == btrfs_ino(BTRFS_I(inode))) |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2328 | return 0; |
| 2329 | |
| 2330 | key.objectid = root_id; |
| 2331 | key.type = BTRFS_ROOT_ITEM_KEY; |
| 2332 | key.offset = (u64)-1; |
| 2333 | |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2334 | root = btrfs_read_fs_root_no_name(fs_info, &key); |
| 2335 | if (IS_ERR(root)) { |
| 2336 | if (PTR_ERR(root) == -ENOENT) |
| 2337 | return 0; |
| 2338 | WARN_ON(1); |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 2339 | btrfs_debug(fs_info, "inum=%llu, offset=%llu, root_id=%llu", |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2340 | inum, offset, root_id); |
| 2341 | return PTR_ERR(root); |
| 2342 | } |
| 2343 | |
| 2344 | key.objectid = inum; |
| 2345 | key.type = BTRFS_EXTENT_DATA_KEY; |
| 2346 | if (offset > (u64)-1 << 32) |
| 2347 | key.offset = 0; |
| 2348 | else |
| 2349 | key.offset = offset; |
| 2350 | |
| 2351 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
Dulshani Gunawardhana | fae7f21 | 2013-10-31 10:30:08 +0530 | [diff] [blame] | 2352 | if (WARN_ON(ret < 0)) |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2353 | return ret; |
Josef Bacik | 50f1319 | 2013-07-22 12:50:37 -0400 | [diff] [blame] | 2354 | ret = 0; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2355 | |
| 2356 | while (1) { |
| 2357 | cond_resched(); |
| 2358 | |
| 2359 | leaf = path->nodes[0]; |
| 2360 | slot = path->slots[0]; |
| 2361 | |
| 2362 | if (slot >= btrfs_header_nritems(leaf)) { |
| 2363 | ret = btrfs_next_leaf(root, path); |
| 2364 | if (ret < 0) { |
| 2365 | goto out; |
| 2366 | } else if (ret > 0) { |
| 2367 | ret = 0; |
| 2368 | goto out; |
| 2369 | } |
| 2370 | continue; |
| 2371 | } |
| 2372 | |
| 2373 | path->slots[0]++; |
| 2374 | |
| 2375 | btrfs_item_key_to_cpu(leaf, &key, slot); |
| 2376 | |
| 2377 | if (key.objectid > inum) |
| 2378 | goto out; |
| 2379 | |
| 2380 | if (key.objectid < inum || key.type != BTRFS_EXTENT_DATA_KEY) |
| 2381 | continue; |
| 2382 | |
| 2383 | extent = btrfs_item_ptr(leaf, slot, |
| 2384 | struct btrfs_file_extent_item); |
| 2385 | |
| 2386 | if (btrfs_file_extent_disk_bytenr(leaf, extent) != old->bytenr) |
| 2387 | continue; |
| 2388 | |
Liu Bo | e68afa4 | 2013-07-01 22:13:26 +0800 | [diff] [blame] | 2389 | /* |
| 2390 | * 'offset' refers to the exact key.offset, |
| 2391 | * NOT the 'offset' field in btrfs_extent_data_ref, ie. |
| 2392 | * (key.offset - extent_offset). |
| 2393 | */ |
| 2394 | if (key.offset != offset) |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2395 | continue; |
| 2396 | |
Liu Bo | e68afa4 | 2013-07-01 22:13:26 +0800 | [diff] [blame] | 2397 | extent_offset = btrfs_file_extent_offset(leaf, extent); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2398 | num_bytes = btrfs_file_extent_num_bytes(leaf, extent); |
Liu Bo | e68afa4 | 2013-07-01 22:13:26 +0800 | [diff] [blame] | 2399 | |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2400 | if (extent_offset >= old->extent_offset + old->offset + |
| 2401 | old->len || extent_offset + num_bytes <= |
| 2402 | old->extent_offset + old->offset) |
| 2403 | continue; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2404 | break; |
| 2405 | } |
| 2406 | |
| 2407 | backref = kmalloc(sizeof(*backref), GFP_NOFS); |
| 2408 | if (!backref) { |
| 2409 | ret = -ENOENT; |
| 2410 | goto out; |
| 2411 | } |
| 2412 | |
| 2413 | backref->root_id = root_id; |
| 2414 | backref->inum = inum; |
Liu Bo | e68afa4 | 2013-07-01 22:13:26 +0800 | [diff] [blame] | 2415 | backref->file_pos = offset; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2416 | backref->num_bytes = num_bytes; |
| 2417 | backref->extent_offset = extent_offset; |
| 2418 | backref->generation = btrfs_file_extent_generation(leaf, extent); |
| 2419 | backref->old = old; |
| 2420 | backref_insert(&new->root, backref); |
| 2421 | old->count++; |
| 2422 | out: |
| 2423 | btrfs_release_path(path); |
| 2424 | WARN_ON(ret); |
| 2425 | return ret; |
| 2426 | } |
| 2427 | |
| 2428 | static noinline bool record_extent_backrefs(struct btrfs_path *path, |
| 2429 | struct new_sa_defrag_extent *new) |
| 2430 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2431 | struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2432 | struct old_sa_defrag_extent *old, *tmp; |
| 2433 | int ret; |
| 2434 | |
| 2435 | new->path = path; |
| 2436 | |
| 2437 | list_for_each_entry_safe(old, tmp, &new->head, list) { |
Liu Bo | e68afa4 | 2013-07-01 22:13:26 +0800 | [diff] [blame] | 2438 | ret = iterate_inodes_from_logical(old->bytenr + |
| 2439 | old->extent_offset, fs_info, |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2440 | path, record_one_backref, |
| 2441 | old); |
Josef Bacik | 4724b10 | 2013-11-05 11:11:40 -0500 | [diff] [blame] | 2442 | if (ret < 0 && ret != -ENOENT) |
| 2443 | return false; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2444 | |
| 2445 | /* no backref to be processed for this extent */ |
| 2446 | if (!old->count) { |
| 2447 | list_del(&old->list); |
| 2448 | kfree(old); |
| 2449 | } |
| 2450 | } |
| 2451 | |
| 2452 | if (list_empty(&new->head)) |
| 2453 | return false; |
| 2454 | |
| 2455 | return true; |
| 2456 | } |
| 2457 | |
| 2458 | static int relink_is_mergable(struct extent_buffer *leaf, |
| 2459 | struct btrfs_file_extent_item *fi, |
Liu Bo | 116e002 | 2013-08-02 16:30:40 +0800 | [diff] [blame] | 2460 | struct new_sa_defrag_extent *new) |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2461 | { |
Liu Bo | 116e002 | 2013-08-02 16:30:40 +0800 | [diff] [blame] | 2462 | if (btrfs_file_extent_disk_bytenr(leaf, fi) != new->bytenr) |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2463 | return 0; |
| 2464 | |
| 2465 | if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG) |
| 2466 | return 0; |
| 2467 | |
Liu Bo | 116e002 | 2013-08-02 16:30:40 +0800 | [diff] [blame] | 2468 | if (btrfs_file_extent_compression(leaf, fi) != new->compress_type) |
| 2469 | return 0; |
| 2470 | |
| 2471 | if (btrfs_file_extent_encryption(leaf, fi) || |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2472 | btrfs_file_extent_other_encoding(leaf, fi)) |
| 2473 | return 0; |
| 2474 | |
| 2475 | return 1; |
| 2476 | } |
| 2477 | |
| 2478 | /* |
| 2479 | * Note the backref might has changed, and in this case we just return 0. |
| 2480 | */ |
| 2481 | static noinline int relink_extent_backref(struct btrfs_path *path, |
| 2482 | struct sa_defrag_extent_backref *prev, |
| 2483 | struct sa_defrag_extent_backref *backref) |
| 2484 | { |
| 2485 | struct btrfs_file_extent_item *extent; |
| 2486 | struct btrfs_file_extent_item *item; |
| 2487 | struct btrfs_ordered_extent *ordered; |
| 2488 | struct btrfs_trans_handle *trans; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2489 | struct btrfs_root *root; |
| 2490 | struct btrfs_key key; |
| 2491 | struct extent_buffer *leaf; |
| 2492 | struct old_sa_defrag_extent *old = backref->old; |
| 2493 | struct new_sa_defrag_extent *new = old->new; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2494 | struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2495 | struct inode *inode; |
| 2496 | struct extent_state *cached = NULL; |
| 2497 | int ret = 0; |
| 2498 | u64 start; |
| 2499 | u64 len; |
| 2500 | u64 lock_start; |
| 2501 | u64 lock_end; |
| 2502 | bool merge = false; |
| 2503 | int index; |
| 2504 | |
| 2505 | if (prev && prev->root_id == backref->root_id && |
| 2506 | prev->inum == backref->inum && |
| 2507 | prev->file_pos + prev->num_bytes == backref->file_pos) |
| 2508 | merge = true; |
| 2509 | |
| 2510 | /* step 1: get root */ |
| 2511 | key.objectid = backref->root_id; |
| 2512 | key.type = BTRFS_ROOT_ITEM_KEY; |
| 2513 | key.offset = (u64)-1; |
| 2514 | |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2515 | index = srcu_read_lock(&fs_info->subvol_srcu); |
| 2516 | |
| 2517 | root = btrfs_read_fs_root_no_name(fs_info, &key); |
| 2518 | if (IS_ERR(root)) { |
| 2519 | srcu_read_unlock(&fs_info->subvol_srcu, index); |
| 2520 | if (PTR_ERR(root) == -ENOENT) |
| 2521 | return 0; |
| 2522 | return PTR_ERR(root); |
| 2523 | } |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2524 | |
Wang Shilong | bcbba5e | 2014-02-08 23:46:35 +0800 | [diff] [blame] | 2525 | if (btrfs_root_readonly(root)) { |
| 2526 | srcu_read_unlock(&fs_info->subvol_srcu, index); |
| 2527 | return 0; |
| 2528 | } |
| 2529 | |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2530 | /* step 2: get inode */ |
| 2531 | key.objectid = backref->inum; |
| 2532 | key.type = BTRFS_INODE_ITEM_KEY; |
| 2533 | key.offset = 0; |
| 2534 | |
| 2535 | inode = btrfs_iget(fs_info->sb, &key, root, NULL); |
| 2536 | if (IS_ERR(inode)) { |
| 2537 | srcu_read_unlock(&fs_info->subvol_srcu, index); |
| 2538 | return 0; |
| 2539 | } |
| 2540 | |
| 2541 | srcu_read_unlock(&fs_info->subvol_srcu, index); |
| 2542 | |
| 2543 | /* step 3: relink backref */ |
| 2544 | lock_start = backref->file_pos; |
| 2545 | lock_end = backref->file_pos + backref->num_bytes - 1; |
| 2546 | lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end, |
David Sterba | ff13db4 | 2015-12-03 14:30:40 +0100 | [diff] [blame] | 2547 | &cached); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2548 | |
| 2549 | ordered = btrfs_lookup_first_ordered_extent(inode, lock_end); |
| 2550 | if (ordered) { |
| 2551 | btrfs_put_ordered_extent(ordered); |
| 2552 | goto out_unlock; |
| 2553 | } |
| 2554 | |
| 2555 | trans = btrfs_join_transaction(root); |
| 2556 | if (IS_ERR(trans)) { |
| 2557 | ret = PTR_ERR(trans); |
| 2558 | goto out_unlock; |
| 2559 | } |
| 2560 | |
| 2561 | key.objectid = backref->inum; |
| 2562 | key.type = BTRFS_EXTENT_DATA_KEY; |
| 2563 | key.offset = backref->file_pos; |
| 2564 | |
| 2565 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 2566 | if (ret < 0) { |
| 2567 | goto out_free_path; |
| 2568 | } else if (ret > 0) { |
| 2569 | ret = 0; |
| 2570 | goto out_free_path; |
| 2571 | } |
| 2572 | |
| 2573 | extent = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 2574 | struct btrfs_file_extent_item); |
| 2575 | |
| 2576 | if (btrfs_file_extent_generation(path->nodes[0], extent) != |
| 2577 | backref->generation) |
| 2578 | goto out_free_path; |
| 2579 | |
| 2580 | btrfs_release_path(path); |
| 2581 | |
| 2582 | start = backref->file_pos; |
| 2583 | if (backref->extent_offset < old->extent_offset + old->offset) |
| 2584 | start += old->extent_offset + old->offset - |
| 2585 | backref->extent_offset; |
| 2586 | |
| 2587 | len = min(backref->extent_offset + backref->num_bytes, |
| 2588 | old->extent_offset + old->offset + old->len); |
| 2589 | len -= max(backref->extent_offset, old->extent_offset + old->offset); |
| 2590 | |
| 2591 | ret = btrfs_drop_extents(trans, root, inode, start, |
| 2592 | start + len, 1); |
| 2593 | if (ret) |
| 2594 | goto out_free_path; |
| 2595 | again: |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 2596 | key.objectid = btrfs_ino(BTRFS_I(inode)); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2597 | key.type = BTRFS_EXTENT_DATA_KEY; |
| 2598 | key.offset = start; |
| 2599 | |
Liu Bo | a09a0a7 | 2013-03-11 09:20:58 +0000 | [diff] [blame] | 2600 | path->leave_spinning = 1; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2601 | if (merge) { |
| 2602 | struct btrfs_file_extent_item *fi; |
| 2603 | u64 extent_len; |
| 2604 | struct btrfs_key found_key; |
| 2605 | |
Gui Hecheng | 3c9665d | 2014-01-23 13:41:09 +0800 | [diff] [blame] | 2606 | ret = btrfs_search_slot(trans, root, &key, path, 0, 1); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2607 | if (ret < 0) |
| 2608 | goto out_free_path; |
| 2609 | |
| 2610 | path->slots[0]--; |
| 2611 | leaf = path->nodes[0]; |
| 2612 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 2613 | |
| 2614 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 2615 | struct btrfs_file_extent_item); |
| 2616 | extent_len = btrfs_file_extent_num_bytes(leaf, fi); |
| 2617 | |
Liu Bo | 116e002 | 2013-08-02 16:30:40 +0800 | [diff] [blame] | 2618 | if (extent_len + found_key.offset == start && |
| 2619 | relink_is_mergable(leaf, fi, new)) { |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2620 | btrfs_set_file_extent_num_bytes(leaf, fi, |
| 2621 | extent_len + len); |
| 2622 | btrfs_mark_buffer_dirty(leaf); |
| 2623 | inode_add_bytes(inode, len); |
| 2624 | |
| 2625 | ret = 1; |
| 2626 | goto out_free_path; |
| 2627 | } else { |
| 2628 | merge = false; |
| 2629 | btrfs_release_path(path); |
| 2630 | goto again; |
| 2631 | } |
| 2632 | } |
| 2633 | |
| 2634 | ret = btrfs_insert_empty_item(trans, root, path, &key, |
| 2635 | sizeof(*extent)); |
| 2636 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 2637 | btrfs_abort_transaction(trans, ret); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2638 | goto out_free_path; |
| 2639 | } |
| 2640 | |
| 2641 | leaf = path->nodes[0]; |
| 2642 | item = btrfs_item_ptr(leaf, path->slots[0], |
| 2643 | struct btrfs_file_extent_item); |
| 2644 | btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr); |
| 2645 | btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len); |
| 2646 | btrfs_set_file_extent_offset(leaf, item, start - new->file_pos); |
| 2647 | btrfs_set_file_extent_num_bytes(leaf, item, len); |
| 2648 | btrfs_set_file_extent_ram_bytes(leaf, item, new->len); |
| 2649 | btrfs_set_file_extent_generation(leaf, item, trans->transid); |
| 2650 | btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG); |
| 2651 | btrfs_set_file_extent_compression(leaf, item, new->compress_type); |
| 2652 | btrfs_set_file_extent_encryption(leaf, item, 0); |
| 2653 | btrfs_set_file_extent_other_encoding(leaf, item, 0); |
| 2654 | |
| 2655 | btrfs_mark_buffer_dirty(leaf); |
| 2656 | inode_add_bytes(inode, len); |
Liu Bo | a09a0a7 | 2013-03-11 09:20:58 +0000 | [diff] [blame] | 2657 | btrfs_release_path(path); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2658 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2659 | ret = btrfs_inc_extent_ref(trans, fs_info, new->bytenr, |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2660 | new->disk_len, 0, |
| 2661 | backref->root_id, backref->inum, |
Filipe Manana | b06c4bf | 2015-10-23 07:52:54 +0100 | [diff] [blame] | 2662 | new->file_pos); /* start - extent_offset */ |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2663 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 2664 | btrfs_abort_transaction(trans, ret); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2665 | goto out_free_path; |
| 2666 | } |
| 2667 | |
| 2668 | ret = 1; |
| 2669 | out_free_path: |
| 2670 | btrfs_release_path(path); |
Liu Bo | a09a0a7 | 2013-03-11 09:20:58 +0000 | [diff] [blame] | 2671 | path->leave_spinning = 0; |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 2672 | btrfs_end_transaction(trans); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2673 | out_unlock: |
| 2674 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end, |
| 2675 | &cached, GFP_NOFS); |
| 2676 | iput(inode); |
| 2677 | return ret; |
| 2678 | } |
| 2679 | |
Liu Bo | 6f51956 | 2013-10-29 10:45:05 +0800 | [diff] [blame] | 2680 | static void free_sa_defrag_extent(struct new_sa_defrag_extent *new) |
| 2681 | { |
| 2682 | struct old_sa_defrag_extent *old, *tmp; |
| 2683 | |
| 2684 | if (!new) |
| 2685 | return; |
| 2686 | |
| 2687 | list_for_each_entry_safe(old, tmp, &new->head, list) { |
Liu Bo | 6f51956 | 2013-10-29 10:45:05 +0800 | [diff] [blame] | 2688 | kfree(old); |
| 2689 | } |
| 2690 | kfree(new); |
| 2691 | } |
| 2692 | |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2693 | static void relink_file_extents(struct new_sa_defrag_extent *new) |
| 2694 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2695 | struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2696 | struct btrfs_path *path; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2697 | struct sa_defrag_extent_backref *backref; |
| 2698 | struct sa_defrag_extent_backref *prev = NULL; |
| 2699 | struct inode *inode; |
| 2700 | struct btrfs_root *root; |
| 2701 | struct rb_node *node; |
| 2702 | int ret; |
| 2703 | |
| 2704 | inode = new->inode; |
| 2705 | root = BTRFS_I(inode)->root; |
| 2706 | |
| 2707 | path = btrfs_alloc_path(); |
| 2708 | if (!path) |
| 2709 | return; |
| 2710 | |
| 2711 | if (!record_extent_backrefs(path, new)) { |
| 2712 | btrfs_free_path(path); |
| 2713 | goto out; |
| 2714 | } |
| 2715 | btrfs_release_path(path); |
| 2716 | |
| 2717 | while (1) { |
| 2718 | node = rb_first(&new->root); |
| 2719 | if (!node) |
| 2720 | break; |
| 2721 | rb_erase(node, &new->root); |
| 2722 | |
| 2723 | backref = rb_entry(node, struct sa_defrag_extent_backref, node); |
| 2724 | |
| 2725 | ret = relink_extent_backref(path, prev, backref); |
| 2726 | WARN_ON(ret < 0); |
| 2727 | |
| 2728 | kfree(prev); |
| 2729 | |
| 2730 | if (ret == 1) |
| 2731 | prev = backref; |
| 2732 | else |
| 2733 | prev = NULL; |
| 2734 | cond_resched(); |
| 2735 | } |
| 2736 | kfree(prev); |
| 2737 | |
| 2738 | btrfs_free_path(path); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2739 | out: |
Liu Bo | 6f51956 | 2013-10-29 10:45:05 +0800 | [diff] [blame] | 2740 | free_sa_defrag_extent(new); |
| 2741 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2742 | atomic_dec(&fs_info->defrag_running); |
| 2743 | wake_up(&fs_info->transaction_wait); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2744 | } |
| 2745 | |
| 2746 | static struct new_sa_defrag_extent * |
| 2747 | record_old_file_extents(struct inode *inode, |
| 2748 | struct btrfs_ordered_extent *ordered) |
| 2749 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2750 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2751 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 2752 | struct btrfs_path *path; |
| 2753 | struct btrfs_key key; |
Liu Bo | 6f51956 | 2013-10-29 10:45:05 +0800 | [diff] [blame] | 2754 | struct old_sa_defrag_extent *old; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2755 | struct new_sa_defrag_extent *new; |
| 2756 | int ret; |
| 2757 | |
| 2758 | new = kmalloc(sizeof(*new), GFP_NOFS); |
| 2759 | if (!new) |
| 2760 | return NULL; |
| 2761 | |
| 2762 | new->inode = inode; |
| 2763 | new->file_pos = ordered->file_offset; |
| 2764 | new->len = ordered->len; |
| 2765 | new->bytenr = ordered->start; |
| 2766 | new->disk_len = ordered->disk_len; |
| 2767 | new->compress_type = ordered->compress_type; |
| 2768 | new->root = RB_ROOT; |
| 2769 | INIT_LIST_HEAD(&new->head); |
| 2770 | |
| 2771 | path = btrfs_alloc_path(); |
| 2772 | if (!path) |
| 2773 | goto out_kfree; |
| 2774 | |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 2775 | key.objectid = btrfs_ino(BTRFS_I(inode)); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2776 | key.type = BTRFS_EXTENT_DATA_KEY; |
| 2777 | key.offset = new->file_pos; |
| 2778 | |
| 2779 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 2780 | if (ret < 0) |
| 2781 | goto out_free_path; |
| 2782 | if (ret > 0 && path->slots[0] > 0) |
| 2783 | path->slots[0]--; |
| 2784 | |
| 2785 | /* find out all the old extents for the file range */ |
| 2786 | while (1) { |
| 2787 | struct btrfs_file_extent_item *extent; |
| 2788 | struct extent_buffer *l; |
| 2789 | int slot; |
| 2790 | u64 num_bytes; |
| 2791 | u64 offset; |
| 2792 | u64 end; |
| 2793 | u64 disk_bytenr; |
| 2794 | u64 extent_offset; |
| 2795 | |
| 2796 | l = path->nodes[0]; |
| 2797 | slot = path->slots[0]; |
| 2798 | |
| 2799 | if (slot >= btrfs_header_nritems(l)) { |
| 2800 | ret = btrfs_next_leaf(root, path); |
| 2801 | if (ret < 0) |
Liu Bo | 6f51956 | 2013-10-29 10:45:05 +0800 | [diff] [blame] | 2802 | goto out_free_path; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2803 | else if (ret > 0) |
| 2804 | break; |
| 2805 | continue; |
| 2806 | } |
| 2807 | |
| 2808 | btrfs_item_key_to_cpu(l, &key, slot); |
| 2809 | |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 2810 | if (key.objectid != btrfs_ino(BTRFS_I(inode))) |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2811 | break; |
| 2812 | if (key.type != BTRFS_EXTENT_DATA_KEY) |
| 2813 | break; |
| 2814 | if (key.offset >= new->file_pos + new->len) |
| 2815 | break; |
| 2816 | |
| 2817 | extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item); |
| 2818 | |
| 2819 | num_bytes = btrfs_file_extent_num_bytes(l, extent); |
| 2820 | if (key.offset + num_bytes < new->file_pos) |
| 2821 | goto next; |
| 2822 | |
| 2823 | disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent); |
| 2824 | if (!disk_bytenr) |
| 2825 | goto next; |
| 2826 | |
| 2827 | extent_offset = btrfs_file_extent_offset(l, extent); |
| 2828 | |
| 2829 | old = kmalloc(sizeof(*old), GFP_NOFS); |
| 2830 | if (!old) |
Liu Bo | 6f51956 | 2013-10-29 10:45:05 +0800 | [diff] [blame] | 2831 | goto out_free_path; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2832 | |
| 2833 | offset = max(new->file_pos, key.offset); |
| 2834 | end = min(new->file_pos + new->len, key.offset + num_bytes); |
| 2835 | |
| 2836 | old->bytenr = disk_bytenr; |
| 2837 | old->extent_offset = extent_offset; |
| 2838 | old->offset = offset - key.offset; |
| 2839 | old->len = end - offset; |
| 2840 | old->new = new; |
| 2841 | old->count = 0; |
| 2842 | list_add_tail(&old->list, &new->head); |
| 2843 | next: |
| 2844 | path->slots[0]++; |
| 2845 | cond_resched(); |
| 2846 | } |
| 2847 | |
| 2848 | btrfs_free_path(path); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2849 | atomic_inc(&fs_info->defrag_running); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2850 | |
| 2851 | return new; |
| 2852 | |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2853 | out_free_path: |
| 2854 | btrfs_free_path(path); |
| 2855 | out_kfree: |
Liu Bo | 6f51956 | 2013-10-29 10:45:05 +0800 | [diff] [blame] | 2856 | free_sa_defrag_extent(new); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2857 | return NULL; |
| 2858 | } |
| 2859 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2860 | static void btrfs_release_delalloc_bytes(struct btrfs_fs_info *fs_info, |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 2861 | u64 start, u64 len) |
| 2862 | { |
| 2863 | struct btrfs_block_group_cache *cache; |
| 2864 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2865 | cache = btrfs_lookup_block_group(fs_info, start); |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 2866 | ASSERT(cache); |
| 2867 | |
| 2868 | spin_lock(&cache->lock); |
| 2869 | cache->delalloc_bytes -= len; |
| 2870 | spin_unlock(&cache->lock); |
| 2871 | |
| 2872 | btrfs_put_block_group(cache); |
| 2873 | } |
| 2874 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2875 | /* as ordered data IO finishes, this gets called so we can finish |
| 2876 | * an ordered extent if the range of bytes in the file it covers are |
| 2877 | * fully written. |
| 2878 | */ |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2879 | static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2880 | { |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2881 | struct inode *inode = ordered_extent->inode; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2882 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2883 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 2884 | struct btrfs_trans_handle *trans = NULL; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2885 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 2886 | struct extent_state *cached_state = NULL; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2887 | struct new_sa_defrag_extent *new = NULL; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 2888 | int compress_type = 0; |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 2889 | int ret = 0; |
| 2890 | u64 logical_len = ordered_extent->len; |
Li Zefan | 82d5902 | 2011-04-20 10:33:24 +0800 | [diff] [blame] | 2891 | bool nolock; |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 2892 | bool truncated = false; |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 2893 | bool range_locked = false; |
| 2894 | bool clear_new_delalloc_bytes = false; |
| 2895 | |
| 2896 | if (!test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) && |
| 2897 | !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags) && |
| 2898 | !test_bit(BTRFS_ORDERED_DIRECT, &ordered_extent->flags)) |
| 2899 | clear_new_delalloc_bytes = true; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2900 | |
Nikolay Borisov | 70ddc55 | 2017-02-20 13:50:35 +0200 | [diff] [blame] | 2901 | nolock = btrfs_is_free_space_inode(BTRFS_I(inode)); |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 2902 | |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2903 | if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) { |
| 2904 | ret = -EIO; |
| 2905 | goto out; |
| 2906 | } |
| 2907 | |
Nikolay Borisov | 7ab7956 | 2017-02-20 13:50:57 +0200 | [diff] [blame] | 2908 | btrfs_free_io_failure_record(BTRFS_I(inode), |
| 2909 | ordered_extent->file_offset, |
| 2910 | ordered_extent->file_offset + |
| 2911 | ordered_extent->len - 1); |
Miao Xie | f612496 | 2014-09-12 18:44:04 +0800 | [diff] [blame] | 2912 | |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 2913 | if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) { |
| 2914 | truncated = true; |
| 2915 | logical_len = ordered_extent->truncated_len; |
| 2916 | /* Truncated the entire extent, don't bother adding */ |
| 2917 | if (!logical_len) |
| 2918 | goto out; |
| 2919 | } |
| 2920 | |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 2921 | if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) { |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2922 | BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */ |
Qu Wenruo | 94ed938 | 2015-09-08 17:25:56 +0800 | [diff] [blame] | 2923 | |
| 2924 | /* |
| 2925 | * For mwrite(mmap + memset to write) case, we still reserve |
| 2926 | * space for NOCOW range. |
| 2927 | * As NOCOW won't cause a new delayed ref, just free the space |
| 2928 | */ |
| 2929 | btrfs_qgroup_free_data(inode, ordered_extent->file_offset, |
| 2930 | ordered_extent->len); |
Josef Bacik | 6c760c0 | 2012-11-09 10:53:21 -0500 | [diff] [blame] | 2931 | btrfs_ordered_update_i_size(inode, 0, ordered_extent); |
| 2932 | if (nolock) |
| 2933 | trans = btrfs_join_transaction_nolock(root); |
| 2934 | else |
| 2935 | trans = btrfs_join_transaction(root); |
| 2936 | if (IS_ERR(trans)) { |
| 2937 | ret = PTR_ERR(trans); |
| 2938 | trans = NULL; |
| 2939 | goto out; |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 2940 | } |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2941 | trans->block_rsv = &fs_info->delalloc_block_rsv; |
Josef Bacik | 6c760c0 | 2012-11-09 10:53:21 -0500 | [diff] [blame] | 2942 | ret = btrfs_update_inode_fallback(trans, root, inode); |
| 2943 | if (ret) /* -ENOMEM or corruption */ |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 2944 | btrfs_abort_transaction(trans, ret); |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 2945 | goto out; |
| 2946 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2947 | |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 2948 | range_locked = true; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 2949 | lock_extent_bits(io_tree, ordered_extent->file_offset, |
| 2950 | ordered_extent->file_offset + ordered_extent->len - 1, |
David Sterba | ff13db4 | 2015-12-03 14:30:40 +0100 | [diff] [blame] | 2951 | &cached_state); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2952 | |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2953 | ret = test_range_bit(io_tree, ordered_extent->file_offset, |
| 2954 | ordered_extent->file_offset + ordered_extent->len - 1, |
| 2955 | EXTENT_DEFRAG, 1, cached_state); |
| 2956 | if (ret) { |
| 2957 | u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item); |
Josef Bacik | 8101c8d | 2014-01-29 16:05:30 -0500 | [diff] [blame] | 2958 | if (0 && last_snapshot >= BTRFS_I(inode)->generation) |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2959 | /* the inode is shared */ |
| 2960 | new = record_old_file_extents(inode, ordered_extent); |
| 2961 | |
| 2962 | clear_extent_bit(io_tree, ordered_extent->file_offset, |
| 2963 | ordered_extent->file_offset + ordered_extent->len - 1, |
| 2964 | EXTENT_DEFRAG, 0, 0, &cached_state, GFP_NOFS); |
| 2965 | } |
| 2966 | |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 2967 | if (nolock) |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 2968 | trans = btrfs_join_transaction_nolock(root); |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 2969 | else |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 2970 | trans = btrfs_join_transaction(root); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2971 | if (IS_ERR(trans)) { |
| 2972 | ret = PTR_ERR(trans); |
| 2973 | trans = NULL; |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 2974 | goto out; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2975 | } |
Chris Mason | a79b7d4 | 2014-05-22 16:18:52 -0700 | [diff] [blame] | 2976 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2977 | trans->block_rsv = &fs_info->delalloc_block_rsv; |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 2978 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2979 | if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags)) |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 2980 | compress_type = ordered_extent->compress_type; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2981 | if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) { |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 2982 | BUG_ON(compress_type); |
Nikolay Borisov | 7a6d706 | 2017-02-20 13:50:48 +0200 | [diff] [blame] | 2983 | ret = btrfs_mark_extent_written(trans, BTRFS_I(inode), |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2984 | ordered_extent->file_offset, |
| 2985 | ordered_extent->file_offset + |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 2986 | logical_len); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2987 | } else { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2988 | BUG_ON(root == fs_info->tree_root); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2989 | ret = insert_reserved_file_extent(trans, inode, |
| 2990 | ordered_extent->file_offset, |
| 2991 | ordered_extent->start, |
| 2992 | ordered_extent->disk_len, |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 2993 | logical_len, logical_len, |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 2994 | compress_type, 0, 0, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2995 | BTRFS_FILE_EXTENT_REG); |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 2996 | if (!ret) |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2997 | btrfs_release_delalloc_bytes(fs_info, |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 2998 | ordered_extent->start, |
| 2999 | ordered_extent->disk_len); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 3000 | } |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 3001 | unpin_extent_cache(&BTRFS_I(inode)->extent_tree, |
| 3002 | ordered_extent->file_offset, ordered_extent->len, |
| 3003 | trans->transid); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3004 | if (ret < 0) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3005 | btrfs_abort_transaction(trans, ret); |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 3006 | goto out; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3007 | } |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 3008 | |
David Sterba | df9f628 | 2017-02-10 19:35:37 +0100 | [diff] [blame] | 3009 | add_pending_csums(trans, inode, &ordered_extent->list); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3010 | |
Josef Bacik | 6c760c0 | 2012-11-09 10:53:21 -0500 | [diff] [blame] | 3011 | btrfs_ordered_update_i_size(inode, 0, ordered_extent); |
| 3012 | ret = btrfs_update_inode_fallback(trans, root, inode); |
| 3013 | if (ret) { /* -ENOMEM or corruption */ |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3014 | btrfs_abort_transaction(trans, ret); |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 3015 | goto out; |
Josef Bacik | 1ef30be | 2011-04-05 19:25:36 -0400 | [diff] [blame] | 3016 | } |
| 3017 | ret = 0; |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 3018 | out: |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 3019 | if (range_locked || clear_new_delalloc_bytes) { |
| 3020 | unsigned int clear_bits = 0; |
| 3021 | |
| 3022 | if (range_locked) |
| 3023 | clear_bits |= EXTENT_LOCKED; |
| 3024 | if (clear_new_delalloc_bytes) |
| 3025 | clear_bits |= EXTENT_DELALLOC_NEW; |
| 3026 | clear_extent_bit(&BTRFS_I(inode)->io_tree, |
| 3027 | ordered_extent->file_offset, |
| 3028 | ordered_extent->file_offset + |
| 3029 | ordered_extent->len - 1, |
| 3030 | clear_bits, |
| 3031 | (clear_bits & EXTENT_LOCKED) ? 1 : 0, |
| 3032 | 0, &cached_state, GFP_NOFS); |
| 3033 | } |
| 3034 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3035 | if (root != fs_info->tree_root) |
Nikolay Borisov | 691fa05 | 2017-02-20 13:50:42 +0200 | [diff] [blame] | 3036 | btrfs_delalloc_release_metadata(BTRFS_I(inode), |
| 3037 | ordered_extent->len); |
Miao Xie | a698d075 | 2012-09-20 01:51:59 -0600 | [diff] [blame] | 3038 | if (trans) |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 3039 | btrfs_end_transaction(trans); |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 3040 | |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 3041 | if (ret || truncated) { |
| 3042 | u64 start, end; |
| 3043 | |
| 3044 | if (truncated) |
| 3045 | start = ordered_extent->file_offset + logical_len; |
| 3046 | else |
| 3047 | start = ordered_extent->file_offset; |
| 3048 | end = ordered_extent->file_offset + ordered_extent->len - 1; |
| 3049 | clear_extent_uptodate(io_tree, start, end, NULL, GFP_NOFS); |
| 3050 | |
| 3051 | /* 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] | 3052 | btrfs_drop_extent_cache(BTRFS_I(inode), start, end, 0); |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 3053 | |
Josef Bacik | 0bec9ef | 2013-01-31 14:58:00 -0500 | [diff] [blame] | 3054 | /* |
| 3055 | * If the ordered extent had an IOERR or something else went |
| 3056 | * wrong we need to return the space for this ordered extent |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 3057 | * back to the allocator. We only free the extent in the |
| 3058 | * truncated case if we didn't write out the extent at all. |
Josef Bacik | 0bec9ef | 2013-01-31 14:58:00 -0500 | [diff] [blame] | 3059 | */ |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 3060 | if ((ret || !logical_len) && |
| 3061 | !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) && |
Josef Bacik | 0bec9ef | 2013-01-31 14:58:00 -0500 | [diff] [blame] | 3062 | !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3063 | btrfs_free_reserved_extent(fs_info, |
| 3064 | ordered_extent->start, |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 3065 | ordered_extent->disk_len, 1); |
Josef Bacik | 0bec9ef | 2013-01-31 14:58:00 -0500 | [diff] [blame] | 3066 | } |
| 3067 | |
| 3068 | |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 3069 | /* |
Liu Bo | 8bad3c0 | 2012-06-18 12:14:23 +0800 | [diff] [blame] | 3070 | * This needs to be done to make sure anybody waiting knows we are done |
| 3071 | * updating everything for this ordered extent. |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 3072 | */ |
| 3073 | btrfs_remove_ordered_extent(inode, ordered_extent); |
| 3074 | |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 3075 | /* for snapshot-aware defrag */ |
Liu Bo | 6f51956 | 2013-10-29 10:45:05 +0800 | [diff] [blame] | 3076 | if (new) { |
| 3077 | if (ret) { |
| 3078 | free_sa_defrag_extent(new); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3079 | atomic_dec(&fs_info->defrag_running); |
Liu Bo | 6f51956 | 2013-10-29 10:45:05 +0800 | [diff] [blame] | 3080 | } else { |
| 3081 | relink_file_extents(new); |
| 3082 | } |
| 3083 | } |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 3084 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3085 | /* once for us */ |
| 3086 | btrfs_put_ordered_extent(ordered_extent); |
| 3087 | /* once for the tree */ |
| 3088 | btrfs_put_ordered_extent(ordered_extent); |
| 3089 | |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 3090 | return ret; |
| 3091 | } |
| 3092 | |
| 3093 | static void finish_ordered_fn(struct btrfs_work *work) |
| 3094 | { |
| 3095 | struct btrfs_ordered_extent *ordered_extent; |
| 3096 | ordered_extent = container_of(work, struct btrfs_ordered_extent, work); |
| 3097 | btrfs_finish_ordered_io(ordered_extent); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3098 | } |
| 3099 | |
David Sterba | c3988d6 | 2017-02-17 15:18:32 +0100 | [diff] [blame] | 3100 | 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] | 3101 | struct extent_state *state, int uptodate) |
| 3102 | { |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 3103 | struct inode *inode = page->mapping->host; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3104 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 3105 | struct btrfs_ordered_extent *ordered_extent = NULL; |
Liu Bo | 9e0af23 | 2014-08-15 23:36:53 +0800 | [diff] [blame] | 3106 | struct btrfs_workqueue *wq; |
| 3107 | btrfs_work_func_t func; |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 3108 | |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 3109 | trace_btrfs_writepage_end_io_hook(page, start, end, uptodate); |
| 3110 | |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 3111 | ClearPagePrivate2(page); |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 3112 | if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start, |
| 3113 | end - start + 1, uptodate)) |
David Sterba | c3988d6 | 2017-02-17 15:18:32 +0100 | [diff] [blame] | 3114 | return; |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 3115 | |
Nikolay Borisov | 70ddc55 | 2017-02-20 13:50:35 +0200 | [diff] [blame] | 3116 | if (btrfs_is_free_space_inode(BTRFS_I(inode))) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3117 | wq = fs_info->endio_freespace_worker; |
Liu Bo | 9e0af23 | 2014-08-15 23:36:53 +0800 | [diff] [blame] | 3118 | func = btrfs_freespace_write_helper; |
| 3119 | } else { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3120 | wq = fs_info->endio_write_workers; |
Liu Bo | 9e0af23 | 2014-08-15 23:36:53 +0800 | [diff] [blame] | 3121 | func = btrfs_endio_write_helper; |
| 3122 | } |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 3123 | |
Liu Bo | 9e0af23 | 2014-08-15 23:36:53 +0800 | [diff] [blame] | 3124 | btrfs_init_work(&ordered_extent->work, func, finish_ordered_fn, NULL, |
| 3125 | NULL); |
| 3126 | btrfs_queue_work(wq, &ordered_extent->work); |
Chris Mason | 211f90e | 2008-07-18 11:56:15 -0400 | [diff] [blame] | 3127 | } |
| 3128 | |
Miao Xie | dc380ae | 2014-09-12 18:43:55 +0800 | [diff] [blame] | 3129 | static int __readpage_endio_check(struct inode *inode, |
| 3130 | struct btrfs_io_bio *io_bio, |
| 3131 | int icsum, struct page *page, |
| 3132 | int pgoff, u64 start, size_t len) |
| 3133 | { |
| 3134 | char *kaddr; |
| 3135 | u32 csum_expected; |
| 3136 | u32 csum = ~(u32)0; |
Miao Xie | dc380ae | 2014-09-12 18:43:55 +0800 | [diff] [blame] | 3137 | |
| 3138 | csum_expected = *(((u32 *)io_bio->csum) + icsum); |
| 3139 | |
| 3140 | kaddr = kmap_atomic(page); |
| 3141 | csum = btrfs_csum_data(kaddr + pgoff, csum, len); |
Domagoj Tršan | 0b5e3da | 2016-10-27 08:52:33 +0100 | [diff] [blame] | 3142 | btrfs_csum_final(csum, (u8 *)&csum); |
Miao Xie | dc380ae | 2014-09-12 18:43:55 +0800 | [diff] [blame] | 3143 | if (csum != csum_expected) |
| 3144 | goto zeroit; |
| 3145 | |
| 3146 | kunmap_atomic(kaddr); |
| 3147 | return 0; |
| 3148 | zeroit: |
Nikolay Borisov | 0970a22 | 2017-02-20 13:50:53 +0200 | [diff] [blame] | 3149 | btrfs_print_data_csum_error(BTRFS_I(inode), start, csum, csum_expected, |
Qu Wenruo | 6f6b643 | 2017-02-09 10:45:06 +0800 | [diff] [blame] | 3150 | io_bio->mirror_num); |
Miao Xie | dc380ae | 2014-09-12 18:43:55 +0800 | [diff] [blame] | 3151 | memset(kaddr + pgoff, 1, len); |
| 3152 | flush_dcache_page(page); |
| 3153 | kunmap_atomic(kaddr); |
| 3154 | if (csum_expected == 0) |
| 3155 | return 0; |
| 3156 | return -EIO; |
| 3157 | } |
| 3158 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3159 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3160 | * 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] | 3161 | * if there's a match, we allow the bio to finish. If not, the code in |
| 3162 | * extent_io.c will try to find good copies for us. |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3163 | */ |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 3164 | static int btrfs_readpage_end_io_hook(struct btrfs_io_bio *io_bio, |
| 3165 | u64 phy_offset, struct page *page, |
| 3166 | u64 start, u64 end, int mirror) |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 3167 | { |
Miao Xie | 4eee4fa | 2012-12-21 09:17:45 +0000 | [diff] [blame] | 3168 | size_t offset = start - page_offset(page); |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 3169 | struct inode *inode = page->mapping->host; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3170 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | ff79f81 | 2007-10-15 16:22:25 -0400 | [diff] [blame] | 3171 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3172 | |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 3173 | if (PageChecked(page)) { |
| 3174 | ClearPageChecked(page); |
Miao Xie | dc380ae | 2014-09-12 18:43:55 +0800 | [diff] [blame] | 3175 | return 0; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 3176 | } |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 3177 | |
| 3178 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM) |
Miao Xie | dc380ae | 2014-09-12 18:43:55 +0800 | [diff] [blame] | 3179 | return 0; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 3180 | |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 3181 | if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID && |
Chris Mason | 9655d29 | 2009-09-02 15:22:30 -0400 | [diff] [blame] | 3182 | test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) { |
David Sterba | 9116621 | 2016-04-26 23:54:39 +0200 | [diff] [blame] | 3183 | clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM); |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 3184 | return 0; |
| 3185 | } |
| 3186 | |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 3187 | phy_offset >>= inode->i_sb->s_blocksize_bits; |
Miao Xie | dc380ae | 2014-09-12 18:43:55 +0800 | [diff] [blame] | 3188 | return __readpage_endio_check(inode, io_bio, phy_offset, page, offset, |
| 3189 | start, (size_t)(end - start + 1)); |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 3190 | } |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3191 | |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 3192 | void btrfs_add_delayed_iput(struct inode *inode) |
| 3193 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3194 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
David Sterba | 8089fe6 | 2015-11-19 14:15:51 +0100 | [diff] [blame] | 3195 | struct btrfs_inode *binode = BTRFS_I(inode); |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 3196 | |
| 3197 | if (atomic_add_unless(&inode->i_count, -1, 1)) |
| 3198 | return; |
| 3199 | |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 3200 | spin_lock(&fs_info->delayed_iput_lock); |
David Sterba | 8089fe6 | 2015-11-19 14:15:51 +0100 | [diff] [blame] | 3201 | if (binode->delayed_iput_count == 0) { |
| 3202 | ASSERT(list_empty(&binode->delayed_iput)); |
| 3203 | list_add_tail(&binode->delayed_iput, &fs_info->delayed_iputs); |
| 3204 | } else { |
| 3205 | binode->delayed_iput_count++; |
| 3206 | } |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 3207 | spin_unlock(&fs_info->delayed_iput_lock); |
| 3208 | } |
| 3209 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3210 | void btrfs_run_delayed_iputs(struct btrfs_fs_info *fs_info) |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 3211 | { |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 3212 | |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 3213 | spin_lock(&fs_info->delayed_iput_lock); |
David Sterba | 8089fe6 | 2015-11-19 14:15:51 +0100 | [diff] [blame] | 3214 | while (!list_empty(&fs_info->delayed_iputs)) { |
| 3215 | struct btrfs_inode *inode; |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 3216 | |
David Sterba | 8089fe6 | 2015-11-19 14:15:51 +0100 | [diff] [blame] | 3217 | inode = list_first_entry(&fs_info->delayed_iputs, |
| 3218 | struct btrfs_inode, delayed_iput); |
| 3219 | if (inode->delayed_iput_count) { |
| 3220 | inode->delayed_iput_count--; |
| 3221 | list_move_tail(&inode->delayed_iput, |
| 3222 | &fs_info->delayed_iputs); |
| 3223 | } else { |
| 3224 | list_del_init(&inode->delayed_iput); |
| 3225 | } |
| 3226 | spin_unlock(&fs_info->delayed_iput_lock); |
| 3227 | iput(&inode->vfs_inode); |
| 3228 | spin_lock(&fs_info->delayed_iput_lock); |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 3229 | } |
David Sterba | 8089fe6 | 2015-11-19 14:15:51 +0100 | [diff] [blame] | 3230 | spin_unlock(&fs_info->delayed_iput_lock); |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 3231 | } |
| 3232 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3233 | /* |
Justin P. Mattock | 42b2aa8 | 2011-11-28 20:31:00 -0800 | [diff] [blame] | 3234 | * This is called in transaction commit time. If there are no orphan |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3235 | * files in the subvolume, it removes orphan item and frees block_rsv |
| 3236 | * structure. |
| 3237 | */ |
| 3238 | void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans, |
| 3239 | struct btrfs_root *root) |
| 3240 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3241 | struct btrfs_fs_info *fs_info = root->fs_info; |
Josef Bacik | 90290e1 | 2011-12-02 15:44:12 -0500 | [diff] [blame] | 3242 | struct btrfs_block_rsv *block_rsv; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3243 | int ret; |
| 3244 | |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 3245 | if (atomic_read(&root->orphan_inodes) || |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3246 | root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) |
| 3247 | return; |
| 3248 | |
Josef Bacik | 90290e1 | 2011-12-02 15:44:12 -0500 | [diff] [blame] | 3249 | spin_lock(&root->orphan_lock); |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 3250 | if (atomic_read(&root->orphan_inodes)) { |
Josef Bacik | 90290e1 | 2011-12-02 15:44:12 -0500 | [diff] [blame] | 3251 | spin_unlock(&root->orphan_lock); |
| 3252 | return; |
| 3253 | } |
| 3254 | |
| 3255 | if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) { |
| 3256 | spin_unlock(&root->orphan_lock); |
| 3257 | return; |
| 3258 | } |
| 3259 | |
| 3260 | block_rsv = root->orphan_block_rsv; |
| 3261 | root->orphan_block_rsv = NULL; |
| 3262 | spin_unlock(&root->orphan_lock); |
| 3263 | |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 3264 | if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state) && |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3265 | btrfs_root_refs(&root->root_item) > 0) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3266 | ret = btrfs_del_orphan_item(trans, fs_info->tree_root, |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3267 | root->root_key.objectid); |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 3268 | if (ret) |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3269 | btrfs_abort_transaction(trans, ret); |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 3270 | else |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 3271 | clear_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, |
| 3272 | &root->state); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3273 | } |
| 3274 | |
Josef Bacik | 90290e1 | 2011-12-02 15:44:12 -0500 | [diff] [blame] | 3275 | if (block_rsv) { |
| 3276 | WARN_ON(block_rsv->size > 0); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3277 | btrfs_free_block_rsv(fs_info, block_rsv); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3278 | } |
| 3279 | } |
| 3280 | |
| 3281 | /* |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3282 | * This creates an orphan entry for the given inode in case something goes |
| 3283 | * wrong in the middle of an unlink/truncate. |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3284 | * |
| 3285 | * NOTE: caller of this function should reserve 5 units of metadata for |
| 3286 | * this function. |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3287 | */ |
Nikolay Borisov | 73f2e54 | 2017-02-20 13:50:59 +0200 | [diff] [blame] | 3288 | int btrfs_orphan_add(struct btrfs_trans_handle *trans, |
| 3289 | struct btrfs_inode *inode) |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3290 | { |
Nikolay Borisov | 73f2e54 | 2017-02-20 13:50:59 +0200 | [diff] [blame] | 3291 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb); |
| 3292 | struct btrfs_root *root = inode->root; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3293 | struct btrfs_block_rsv *block_rsv = NULL; |
| 3294 | int reserve = 0; |
| 3295 | int insert = 0; |
| 3296 | int ret; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3297 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3298 | if (!root->orphan_block_rsv) { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3299 | block_rsv = btrfs_alloc_block_rsv(fs_info, |
| 3300 | BTRFS_BLOCK_RSV_TEMP); |
Tsutomu Itoh | b532402 | 2011-07-19 07:27:20 +0000 | [diff] [blame] | 3301 | if (!block_rsv) |
| 3302 | return -ENOMEM; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3303 | } |
| 3304 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3305 | spin_lock(&root->orphan_lock); |
| 3306 | if (!root->orphan_block_rsv) { |
| 3307 | root->orphan_block_rsv = block_rsv; |
| 3308 | } else if (block_rsv) { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3309 | btrfs_free_block_rsv(fs_info, block_rsv); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3310 | block_rsv = NULL; |
| 3311 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3312 | |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 3313 | if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
Nikolay Borisov | 73f2e54 | 2017-02-20 13:50:59 +0200 | [diff] [blame] | 3314 | &inode->runtime_flags)) { |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3315 | #if 0 |
| 3316 | /* |
| 3317 | * For proper ENOSPC handling, we should do orphan |
| 3318 | * cleanup when mounting. But this introduces backward |
| 3319 | * compatibility issue. |
| 3320 | */ |
| 3321 | if (!xchg(&root->orphan_item_inserted, 1)) |
| 3322 | insert = 2; |
| 3323 | else |
| 3324 | insert = 1; |
| 3325 | #endif |
| 3326 | insert = 1; |
Miao Xie | 321f0e7 | 2012-08-28 22:13:02 -0600 | [diff] [blame] | 3327 | atomic_inc(&root->orphan_inodes); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3328 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3329 | |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 3330 | if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED, |
Nikolay Borisov | 73f2e54 | 2017-02-20 13:50:59 +0200 | [diff] [blame] | 3331 | &inode->runtime_flags)) |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3332 | reserve = 1; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3333 | spin_unlock(&root->orphan_lock); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3334 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3335 | /* grab metadata reservation from transaction handle */ |
| 3336 | if (reserve) { |
| 3337 | ret = btrfs_orphan_reserve_metadata(trans, inode); |
Josef Bacik | 3b6571c | 2016-05-27 13:03:04 -0400 | [diff] [blame] | 3338 | ASSERT(!ret); |
| 3339 | if (ret) { |
| 3340 | atomic_dec(&root->orphan_inodes); |
| 3341 | clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED, |
Nikolay Borisov | 73f2e54 | 2017-02-20 13:50:59 +0200 | [diff] [blame] | 3342 | &inode->runtime_flags); |
Josef Bacik | 3b6571c | 2016-05-27 13:03:04 -0400 | [diff] [blame] | 3343 | if (insert) |
| 3344 | clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
Nikolay Borisov | 73f2e54 | 2017-02-20 13:50:59 +0200 | [diff] [blame] | 3345 | &inode->runtime_flags); |
Josef Bacik | 3b6571c | 2016-05-27 13:03:04 -0400 | [diff] [blame] | 3346 | return ret; |
| 3347 | } |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3348 | } |
| 3349 | |
| 3350 | /* insert an orphan item to track this unlinked/truncated file */ |
| 3351 | if (insert >= 1) { |
Nikolay Borisov | 73f2e54 | 2017-02-20 13:50:59 +0200 | [diff] [blame] | 3352 | ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode)); |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 3353 | if (ret) { |
Filipe David Borba Manana | 703c88e | 2013-09-22 21:54:55 +0100 | [diff] [blame] | 3354 | atomic_dec(&root->orphan_inodes); |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 3355 | if (reserve) { |
| 3356 | clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED, |
Nikolay Borisov | 73f2e54 | 2017-02-20 13:50:59 +0200 | [diff] [blame] | 3357 | &inode->runtime_flags); |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 3358 | btrfs_orphan_release_metadata(inode); |
| 3359 | } |
| 3360 | if (ret != -EEXIST) { |
Josef Bacik | e8e7cff | 2013-08-21 15:54:00 -0400 | [diff] [blame] | 3361 | clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
Nikolay Borisov | 73f2e54 | 2017-02-20 13:50:59 +0200 | [diff] [blame] | 3362 | &inode->runtime_flags); |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3363 | btrfs_abort_transaction(trans, ret); |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 3364 | return ret; |
| 3365 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3366 | } |
| 3367 | ret = 0; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3368 | } |
| 3369 | |
| 3370 | /* insert an orphan item to track subvolume contains orphan files */ |
| 3371 | if (insert >= 2) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3372 | ret = btrfs_insert_orphan_item(trans, fs_info->tree_root, |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3373 | root->root_key.objectid); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3374 | if (ret && ret != -EEXIST) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3375 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3376 | return ret; |
| 3377 | } |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3378 | } |
| 3379 | return 0; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3380 | } |
| 3381 | |
| 3382 | /* |
| 3383 | * We have done the truncate/delete so we can go ahead and remove the orphan |
| 3384 | * item for this particular inode. |
| 3385 | */ |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 3386 | static int btrfs_orphan_del(struct btrfs_trans_handle *trans, |
Nikolay Borisov | 3d6ae7b | 2017-02-20 13:50:58 +0200 | [diff] [blame] | 3387 | struct btrfs_inode *inode) |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3388 | { |
Nikolay Borisov | 3d6ae7b | 2017-02-20 13:50:58 +0200 | [diff] [blame] | 3389 | struct btrfs_root *root = inode->root; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3390 | int delete_item = 0; |
| 3391 | int release_rsv = 0; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3392 | int ret = 0; |
| 3393 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3394 | spin_lock(&root->orphan_lock); |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 3395 | if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
Nikolay Borisov | 3d6ae7b | 2017-02-20 13:50:58 +0200 | [diff] [blame] | 3396 | &inode->runtime_flags)) |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3397 | delete_item = 1; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3398 | |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 3399 | if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED, |
Nikolay Borisov | 3d6ae7b | 2017-02-20 13:50:58 +0200 | [diff] [blame] | 3400 | &inode->runtime_flags)) |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3401 | release_rsv = 1; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3402 | spin_unlock(&root->orphan_lock); |
| 3403 | |
Filipe David Borba Manana | 703c88e | 2013-09-22 21:54:55 +0100 | [diff] [blame] | 3404 | if (delete_item) { |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 3405 | atomic_dec(&root->orphan_inodes); |
Filipe David Borba Manana | 703c88e | 2013-09-22 21:54:55 +0100 | [diff] [blame] | 3406 | if (trans) |
| 3407 | ret = btrfs_del_orphan_item(trans, root, |
Nikolay Borisov | 3d6ae7b | 2017-02-20 13:50:58 +0200 | [diff] [blame] | 3408 | btrfs_ino(inode)); |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 3409 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3410 | |
Filipe David Borba Manana | 703c88e | 2013-09-22 21:54:55 +0100 | [diff] [blame] | 3411 | if (release_rsv) |
| 3412 | btrfs_orphan_release_metadata(inode); |
| 3413 | |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 3414 | return ret; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3415 | } |
| 3416 | |
| 3417 | /* |
| 3418 | * this cleans up any orphans that may be left on the list from the last use |
| 3419 | * of this root. |
| 3420 | */ |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3421 | int btrfs_orphan_cleanup(struct btrfs_root *root) |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3422 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3423 | struct btrfs_fs_info *fs_info = root->fs_info; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3424 | struct btrfs_path *path; |
| 3425 | struct extent_buffer *leaf; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3426 | struct btrfs_key key, found_key; |
| 3427 | struct btrfs_trans_handle *trans; |
| 3428 | struct inode *inode; |
Josef Bacik | 8f6d7f4 | 2011-09-26 15:55:20 -0400 | [diff] [blame] | 3429 | u64 last_objectid = 0; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3430 | int ret = 0, nr_unlink = 0, nr_truncate = 0; |
| 3431 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3432 | if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED)) |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3433 | return 0; |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 3434 | |
| 3435 | path = btrfs_alloc_path(); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3436 | if (!path) { |
| 3437 | ret = -ENOMEM; |
| 3438 | goto out; |
| 3439 | } |
David Sterba | e4058b5 | 2015-11-27 16:31:35 +0100 | [diff] [blame] | 3440 | path->reada = READA_BACK; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3441 | |
| 3442 | key.objectid = BTRFS_ORPHAN_OBJECTID; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 3443 | key.type = BTRFS_ORPHAN_ITEM_KEY; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3444 | key.offset = (u64)-1; |
| 3445 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3446 | while (1) { |
| 3447 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3448 | if (ret < 0) |
| 3449 | goto out; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3450 | |
| 3451 | /* |
| 3452 | * 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] | 3453 | * 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] | 3454 | * find the key and see if we have stuff that matches |
| 3455 | */ |
| 3456 | if (ret > 0) { |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3457 | ret = 0; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3458 | if (path->slots[0] == 0) |
| 3459 | break; |
| 3460 | path->slots[0]--; |
| 3461 | } |
| 3462 | |
| 3463 | /* pull out the item */ |
| 3464 | leaf = path->nodes[0]; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3465 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 3466 | |
| 3467 | /* make sure the item matches what we want */ |
| 3468 | if (found_key.objectid != BTRFS_ORPHAN_OBJECTID) |
| 3469 | break; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 3470 | if (found_key.type != BTRFS_ORPHAN_ITEM_KEY) |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3471 | break; |
| 3472 | |
| 3473 | /* release the path since we're done with it */ |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3474 | btrfs_release_path(path); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3475 | |
| 3476 | /* |
| 3477 | * this is where we are basically btrfs_lookup, without the |
| 3478 | * crossing root thing. we store the inode number in the |
| 3479 | * offset of the orphan item. |
| 3480 | */ |
Josef Bacik | 8f6d7f4 | 2011-09-26 15:55:20 -0400 | [diff] [blame] | 3481 | |
| 3482 | if (found_key.offset == last_objectid) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3483 | btrfs_err(fs_info, |
| 3484 | "Error removing orphan entry, stopping orphan cleanup"); |
Josef Bacik | 8f6d7f4 | 2011-09-26 15:55:20 -0400 | [diff] [blame] | 3485 | ret = -EINVAL; |
| 3486 | goto out; |
| 3487 | } |
| 3488 | |
| 3489 | last_objectid = found_key.offset; |
| 3490 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3491 | found_key.objectid = found_key.offset; |
| 3492 | found_key.type = BTRFS_INODE_ITEM_KEY; |
| 3493 | found_key.offset = 0; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3494 | inode = btrfs_iget(fs_info->sb, &found_key, root, NULL); |
Rusty Russell | 8c6ffba | 2013-07-15 11:20:32 +0930 | [diff] [blame] | 3495 | ret = PTR_ERR_OR_ZERO(inode); |
Filipe Manana | 6771089 | 2016-06-06 11:51:25 +0100 | [diff] [blame] | 3496 | if (ret && ret != -ENOENT) |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3497 | goto out; |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 3498 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3499 | if (ret == -ENOENT && root == fs_info->tree_root) { |
Arne Jansen | f8e9e0b | 2011-12-14 20:12:02 -0500 | [diff] [blame] | 3500 | struct btrfs_root *dead_root; |
| 3501 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 3502 | int is_dead_root = 0; |
| 3503 | |
| 3504 | /* |
| 3505 | * this is an orphan in the tree root. Currently these |
| 3506 | * could come from 2 sources: |
| 3507 | * a) a snapshot deletion in progress |
| 3508 | * b) a free space cache inode |
| 3509 | * We need to distinguish those two, as the snapshot |
| 3510 | * orphan must not get deleted. |
| 3511 | * find_dead_roots already ran before us, so if this |
| 3512 | * is a snapshot deletion, we should find the root |
| 3513 | * in the dead_roots list |
| 3514 | */ |
| 3515 | spin_lock(&fs_info->trans_lock); |
| 3516 | list_for_each_entry(dead_root, &fs_info->dead_roots, |
| 3517 | root_list) { |
| 3518 | if (dead_root->root_key.objectid == |
| 3519 | found_key.objectid) { |
| 3520 | is_dead_root = 1; |
| 3521 | break; |
| 3522 | } |
| 3523 | } |
| 3524 | spin_unlock(&fs_info->trans_lock); |
| 3525 | if (is_dead_root) { |
| 3526 | /* prevent this orphan from being found again */ |
| 3527 | key.offset = found_key.objectid - 1; |
| 3528 | continue; |
| 3529 | } |
| 3530 | } |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 3531 | /* |
| 3532 | * Inode is already gone but the orphan item is still there, |
| 3533 | * kill the orphan item. |
| 3534 | */ |
Filipe Manana | 6771089 | 2016-06-06 11:51:25 +0100 | [diff] [blame] | 3535 | if (ret == -ENOENT) { |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 3536 | trans = btrfs_start_transaction(root, 1); |
| 3537 | if (IS_ERR(trans)) { |
| 3538 | ret = PTR_ERR(trans); |
| 3539 | goto out; |
| 3540 | } |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3541 | btrfs_debug(fs_info, "auto deleting %Lu", |
| 3542 | found_key.objectid); |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 3543 | ret = btrfs_del_orphan_item(trans, root, |
| 3544 | found_key.objectid); |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 3545 | btrfs_end_transaction(trans); |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 3546 | if (ret) |
| 3547 | goto out; |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 3548 | continue; |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3549 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3550 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3551 | /* |
| 3552 | * add this inode to the orphan list so btrfs_orphan_del does |
| 3553 | * the proper thing when we hit it |
| 3554 | */ |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 3555 | set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
| 3556 | &BTRFS_I(inode)->runtime_flags); |
Josef Bacik | 925396e | 2013-02-01 15:57:47 -0500 | [diff] [blame] | 3557 | atomic_inc(&root->orphan_inodes); |
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 | /* if we have links, this was a truncate, lets do that */ |
| 3560 | if (inode->i_nlink) { |
Dulshani Gunawardhana | fae7f21 | 2013-10-31 10:30:08 +0530 | [diff] [blame] | 3561 | if (WARN_ON(!S_ISREG(inode->i_mode))) { |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 3562 | iput(inode); |
| 3563 | continue; |
| 3564 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3565 | nr_truncate++; |
Josef Bacik | f3fe820 | 2013-01-07 17:03:21 -0500 | [diff] [blame] | 3566 | |
| 3567 | /* 1 for the orphan item deletion. */ |
| 3568 | trans = btrfs_start_transaction(root, 1); |
| 3569 | if (IS_ERR(trans)) { |
Josef Bacik | c69b26b | 2013-06-03 16:51:23 -0400 | [diff] [blame] | 3570 | iput(inode); |
Josef Bacik | f3fe820 | 2013-01-07 17:03:21 -0500 | [diff] [blame] | 3571 | ret = PTR_ERR(trans); |
| 3572 | goto out; |
| 3573 | } |
Nikolay Borisov | 73f2e54 | 2017-02-20 13:50:59 +0200 | [diff] [blame] | 3574 | ret = btrfs_orphan_add(trans, BTRFS_I(inode)); |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 3575 | btrfs_end_transaction(trans); |
Josef Bacik | c69b26b | 2013-06-03 16:51:23 -0400 | [diff] [blame] | 3576 | if (ret) { |
| 3577 | iput(inode); |
Josef Bacik | f3fe820 | 2013-01-07 17:03:21 -0500 | [diff] [blame] | 3578 | goto out; |
Josef Bacik | c69b26b | 2013-06-03 16:51:23 -0400 | [diff] [blame] | 3579 | } |
Josef Bacik | f3fe820 | 2013-01-07 17:03:21 -0500 | [diff] [blame] | 3580 | |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3581 | ret = btrfs_truncate(inode); |
Josef Bacik | 4a7d0f6 | 2013-02-07 16:27:28 -0500 | [diff] [blame] | 3582 | if (ret) |
Nikolay Borisov | 3d6ae7b | 2017-02-20 13:50:58 +0200 | [diff] [blame] | 3583 | btrfs_orphan_del(NULL, BTRFS_I(inode)); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3584 | } else { |
| 3585 | nr_unlink++; |
| 3586 | } |
| 3587 | |
| 3588 | /* this will do delete_inode and everything for us */ |
| 3589 | iput(inode); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3590 | if (ret) |
| 3591 | goto out; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3592 | } |
Miao Xie | 3254c87 | 2011-11-10 20:45:05 -0500 | [diff] [blame] | 3593 | /* release the path since we're done with it */ |
| 3594 | btrfs_release_path(path); |
| 3595 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3596 | root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE; |
| 3597 | |
| 3598 | if (root->orphan_block_rsv) |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3599 | btrfs_block_rsv_release(fs_info, root->orphan_block_rsv, |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3600 | (u64)-1); |
| 3601 | |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 3602 | if (root->orphan_block_rsv || |
| 3603 | test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) { |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 3604 | trans = btrfs_join_transaction(root); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3605 | if (!IS_ERR(trans)) |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 3606 | btrfs_end_transaction(trans); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3607 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3608 | |
| 3609 | if (nr_unlink) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3610 | btrfs_debug(fs_info, "unlinked %d orphans", nr_unlink); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3611 | if (nr_truncate) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3612 | btrfs_debug(fs_info, "truncated %d orphans", nr_truncate); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3613 | |
| 3614 | out: |
| 3615 | if (ret) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3616 | btrfs_err(fs_info, "could not do orphan cleanup %d", ret); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3617 | btrfs_free_path(path); |
| 3618 | return ret; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3619 | } |
| 3620 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3621 | /* |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3622 | * very simple check to peek ahead in the leaf looking for xattrs. If we |
| 3623 | * don't find any xattrs, we know there can't be any acls. |
| 3624 | * |
| 3625 | * slot is the slot the inode is in, objectid is the objectid of the inode |
| 3626 | */ |
| 3627 | 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] | 3628 | int slot, u64 objectid, |
| 3629 | int *first_xattr_slot) |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3630 | { |
| 3631 | u32 nritems = btrfs_header_nritems(leaf); |
| 3632 | struct btrfs_key found_key; |
Josef Bacik | f23b5a5 | 2013-06-19 10:16:26 -0400 | [diff] [blame] | 3633 | static u64 xattr_access = 0; |
| 3634 | static u64 xattr_default = 0; |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3635 | int scanned = 0; |
| 3636 | |
Josef Bacik | f23b5a5 | 2013-06-19 10:16:26 -0400 | [diff] [blame] | 3637 | if (!xattr_access) { |
Andreas Gruenbacher | 97d7929 | 2015-12-02 14:44:35 +0100 | [diff] [blame] | 3638 | xattr_access = btrfs_name_hash(XATTR_NAME_POSIX_ACL_ACCESS, |
| 3639 | strlen(XATTR_NAME_POSIX_ACL_ACCESS)); |
| 3640 | xattr_default = btrfs_name_hash(XATTR_NAME_POSIX_ACL_DEFAULT, |
| 3641 | strlen(XATTR_NAME_POSIX_ACL_DEFAULT)); |
Josef Bacik | f23b5a5 | 2013-06-19 10:16:26 -0400 | [diff] [blame] | 3642 | } |
| 3643 | |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3644 | slot++; |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 3645 | *first_xattr_slot = -1; |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3646 | while (slot < nritems) { |
| 3647 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
| 3648 | |
| 3649 | /* we found a different objectid, there must not be acls */ |
| 3650 | if (found_key.objectid != objectid) |
| 3651 | return 0; |
| 3652 | |
| 3653 | /* we found an xattr, assume we've got an acl */ |
Josef Bacik | f23b5a5 | 2013-06-19 10:16:26 -0400 | [diff] [blame] | 3654 | if (found_key.type == BTRFS_XATTR_ITEM_KEY) { |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 3655 | if (*first_xattr_slot == -1) |
| 3656 | *first_xattr_slot = slot; |
Josef Bacik | f23b5a5 | 2013-06-19 10:16:26 -0400 | [diff] [blame] | 3657 | if (found_key.offset == xattr_access || |
| 3658 | found_key.offset == xattr_default) |
| 3659 | return 1; |
| 3660 | } |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3661 | |
| 3662 | /* |
| 3663 | * we found a key greater than an xattr key, there can't |
| 3664 | * be any acls later on |
| 3665 | */ |
| 3666 | if (found_key.type > BTRFS_XATTR_ITEM_KEY) |
| 3667 | return 0; |
| 3668 | |
| 3669 | slot++; |
| 3670 | scanned++; |
| 3671 | |
| 3672 | /* |
| 3673 | * it goes inode, inode backrefs, xattrs, extents, |
| 3674 | * so if there are a ton of hard links to an inode there can |
| 3675 | * be a lot of backrefs. Don't waste time searching too hard, |
| 3676 | * this is just an optimization |
| 3677 | */ |
| 3678 | if (scanned >= 8) |
| 3679 | break; |
| 3680 | } |
| 3681 | /* we hit the end of the leaf before we found an xattr or |
| 3682 | * something larger than an xattr. We have to assume the inode |
| 3683 | * has acls |
| 3684 | */ |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 3685 | if (*first_xattr_slot == -1) |
| 3686 | *first_xattr_slot = slot; |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3687 | return 1; |
| 3688 | } |
| 3689 | |
| 3690 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3691 | * read an inode from the btree into the in-memory inode |
| 3692 | */ |
Filipe Manana | 6771089 | 2016-06-06 11:51:25 +0100 | [diff] [blame] | 3693 | static int btrfs_read_locked_inode(struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3694 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3695 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3696 | struct btrfs_path *path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3697 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3698 | struct btrfs_inode_item *inode_item; |
| 3699 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 3700 | struct btrfs_key location; |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 3701 | unsigned long ptr; |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3702 | int maybe_acls; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 3703 | u32 rdev; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3704 | int ret; |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 3705 | bool filled = false; |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 3706 | int first_xattr_slot; |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 3707 | |
| 3708 | ret = btrfs_fill_inode(inode, &rdev); |
| 3709 | if (!ret) |
| 3710 | filled = true; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3711 | |
| 3712 | path = btrfs_alloc_path(); |
Filipe Manana | 6771089 | 2016-06-06 11:51:25 +0100 | [diff] [blame] | 3713 | if (!path) { |
| 3714 | ret = -ENOMEM; |
Mark Fasheh | 1748f84 | 2011-07-12 11:25:31 -0700 | [diff] [blame] | 3715 | goto make_bad; |
Filipe Manana | 6771089 | 2016-06-06 11:51:25 +0100 | [diff] [blame] | 3716 | } |
Mark Fasheh | 1748f84 | 2011-07-12 11:25:31 -0700 | [diff] [blame] | 3717 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3718 | memcpy(&location, &BTRFS_I(inode)->location, sizeof(location)); |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 3719 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3720 | ret = btrfs_lookup_inode(NULL, root, path, &location, 0); |
Filipe Manana | 6771089 | 2016-06-06 11:51:25 +0100 | [diff] [blame] | 3721 | if (ret) { |
| 3722 | if (ret > 0) |
| 3723 | ret = -ENOENT; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3724 | goto make_bad; |
Filipe Manana | 6771089 | 2016-06-06 11:51:25 +0100 | [diff] [blame] | 3725 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3726 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3727 | leaf = path->nodes[0]; |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 3728 | |
| 3729 | if (filled) |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 3730 | goto cache_index; |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 3731 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3732 | inode_item = btrfs_item_ptr(leaf, path->slots[0], |
| 3733 | struct btrfs_inode_item); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3734 | inode->i_mode = btrfs_inode_mode(leaf, inode_item); |
Miklos Szeredi | bfe8684 | 2011-10-28 14:13:29 +0200 | [diff] [blame] | 3735 | set_nlink(inode, btrfs_inode_nlink(leaf, inode_item)); |
Eric W. Biederman | 2f2f43d | 2012-02-10 11:05:07 -0800 | [diff] [blame] | 3736 | i_uid_write(inode, btrfs_inode_uid(leaf, inode_item)); |
| 3737 | i_gid_write(inode, btrfs_inode_gid(leaf, inode_item)); |
Nikolay Borisov | 6ef06d2 | 2017-02-20 13:50:34 +0200 | [diff] [blame] | 3738 | 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] | 3739 | |
David Sterba | a937b97 | 2014-12-12 17:39:12 +0100 | [diff] [blame] | 3740 | inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->atime); |
| 3741 | inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->atime); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3742 | |
David Sterba | a937b97 | 2014-12-12 17:39:12 +0100 | [diff] [blame] | 3743 | inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->mtime); |
| 3744 | inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->mtime); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3745 | |
David Sterba | a937b97 | 2014-12-12 17:39:12 +0100 | [diff] [blame] | 3746 | inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->ctime); |
| 3747 | inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->ctime); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3748 | |
chandan r | 9cc97d6 | 2012-07-04 12:48:07 +0530 | [diff] [blame] | 3749 | BTRFS_I(inode)->i_otime.tv_sec = |
| 3750 | btrfs_timespec_sec(leaf, &inode_item->otime); |
| 3751 | BTRFS_I(inode)->i_otime.tv_nsec = |
| 3752 | btrfs_timespec_nsec(leaf, &inode_item->otime); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3753 | |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 3754 | inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item)); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3755 | BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item); |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 3756 | BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item); |
| 3757 | |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 3758 | inode->i_version = btrfs_inode_sequence(leaf, inode_item); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3759 | inode->i_generation = BTRFS_I(inode)->generation; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 3760 | inode->i_rdev = 0; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3761 | rdev = btrfs_inode_rdev(leaf, inode_item); |
| 3762 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 3763 | BTRFS_I(inode)->index_cnt = (u64)-1; |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 3764 | BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item); |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 3765 | |
| 3766 | cache_index: |
Yang Dongsheng | 6e17d30 | 2015-04-09 12:08:43 +0800 | [diff] [blame] | 3767 | /* |
| 3768 | * If we were modified in the current generation and evicted from memory |
| 3769 | * and then re-read we need to do a full sync since we don't have any |
| 3770 | * idea about which extents were modified before we were evicted from |
| 3771 | * cache. |
| 3772 | * |
| 3773 | * This is required for both inode re-read from disk and delayed inode |
| 3774 | * in delayed_nodes_tree. |
| 3775 | */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3776 | if (BTRFS_I(inode)->last_trans == fs_info->generation) |
Yang Dongsheng | 6e17d30 | 2015-04-09 12:08:43 +0800 | [diff] [blame] | 3777 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, |
| 3778 | &BTRFS_I(inode)->runtime_flags); |
| 3779 | |
Filipe Manana | bde6c24 | 2015-07-24 00:00:19 +0100 | [diff] [blame] | 3780 | /* |
| 3781 | * We don't persist the id of the transaction where an unlink operation |
| 3782 | * against the inode was last made. So here we assume the inode might |
| 3783 | * have been evicted, and therefore the exact value of last_unlink_trans |
| 3784 | * lost, and set it to last_trans to avoid metadata inconsistencies |
| 3785 | * between the inode and its parent if the inode is fsync'ed and the log |
| 3786 | * replayed. For example, in the scenario: |
| 3787 | * |
| 3788 | * touch mydir/foo |
| 3789 | * ln mydir/foo mydir/bar |
| 3790 | * sync |
| 3791 | * unlink mydir/bar |
| 3792 | * echo 2 > /proc/sys/vm/drop_caches # evicts inode |
| 3793 | * xfs_io -c fsync mydir/foo |
| 3794 | * <power failure> |
| 3795 | * mount fs, triggers fsync log replay |
| 3796 | * |
| 3797 | * We must make sure that when we fsync our inode foo we also log its |
| 3798 | * parent inode, otherwise after log replay the parent still has the |
| 3799 | * dentry with the "bar" name but our inode foo has a link count of 1 |
| 3800 | * and doesn't have an inode ref with the name "bar" anymore. |
| 3801 | * |
| 3802 | * Setting last_unlink_trans to last_trans is a pessimistic approach, |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 3803 | * but it guarantees correctness at the expense of occasional full |
Filipe Manana | bde6c24 | 2015-07-24 00:00:19 +0100 | [diff] [blame] | 3804 | * transaction commits on fsync if our inode is a directory, or if our |
| 3805 | * inode is not a directory, logging its parent unnecessarily. |
| 3806 | */ |
| 3807 | BTRFS_I(inode)->last_unlink_trans = BTRFS_I(inode)->last_trans; |
| 3808 | |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 3809 | path->slots[0]++; |
| 3810 | if (inode->i_nlink != 1 || |
| 3811 | path->slots[0] >= btrfs_header_nritems(leaf)) |
| 3812 | goto cache_acl; |
| 3813 | |
| 3814 | btrfs_item_key_to_cpu(leaf, &location, path->slots[0]); |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 3815 | if (location.objectid != btrfs_ino(BTRFS_I(inode))) |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 3816 | goto cache_acl; |
| 3817 | |
| 3818 | ptr = btrfs_item_ptr_offset(leaf, path->slots[0]); |
| 3819 | if (location.type == BTRFS_INODE_REF_KEY) { |
| 3820 | struct btrfs_inode_ref *ref; |
| 3821 | |
| 3822 | ref = (struct btrfs_inode_ref *)ptr; |
| 3823 | BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref); |
| 3824 | } else if (location.type == BTRFS_INODE_EXTREF_KEY) { |
| 3825 | struct btrfs_inode_extref *extref; |
| 3826 | |
| 3827 | extref = (struct btrfs_inode_extref *)ptr; |
| 3828 | BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf, |
| 3829 | extref); |
| 3830 | } |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 3831 | cache_acl: |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3832 | /* |
| 3833 | * try to precache a NULL acl entry for files that don't have |
| 3834 | * any xattrs or acls |
| 3835 | */ |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3836 | maybe_acls = acls_after_inode_item(leaf, path->slots[0], |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 3837 | btrfs_ino(BTRFS_I(inode)), &first_xattr_slot); |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 3838 | if (first_xattr_slot != -1) { |
| 3839 | path->slots[0] = first_xattr_slot; |
| 3840 | ret = btrfs_load_inode_props(inode, path); |
| 3841 | if (ret) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3842 | btrfs_err(fs_info, |
David Sterba | 351fd35 | 2014-05-15 16:48:20 +0200 | [diff] [blame] | 3843 | "error loading props for ino %llu (root %llu): %d", |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 3844 | btrfs_ino(BTRFS_I(inode)), |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 3845 | root->root_key.objectid, ret); |
| 3846 | } |
| 3847 | btrfs_free_path(path); |
| 3848 | |
Al Viro | 72c0490 | 2009-06-24 16:58:48 -0400 | [diff] [blame] | 3849 | if (!maybe_acls) |
| 3850 | cache_no_acl(inode); |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3851 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3852 | switch (inode->i_mode & S_IFMT) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3853 | case S_IFREG: |
| 3854 | inode->i_mapping->a_ops = &btrfs_aops; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3855 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3856 | inode->i_fop = &btrfs_file_operations; |
| 3857 | inode->i_op = &btrfs_file_inode_operations; |
| 3858 | break; |
| 3859 | case S_IFDIR: |
| 3860 | inode->i_fop = &btrfs_dir_file_operations; |
Omar Sandoval | 67ade05 | 2017-01-25 17:06:38 -0800 | [diff] [blame] | 3861 | inode->i_op = &btrfs_dir_inode_operations; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3862 | break; |
| 3863 | case S_IFLNK: |
| 3864 | inode->i_op = &btrfs_symlink_inode_operations; |
Al Viro | 21fc61c | 2015-11-17 01:07:57 -0500 | [diff] [blame] | 3865 | inode_nohighmem(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3866 | inode->i_mapping->a_ops = &btrfs_symlink_aops; |
| 3867 | break; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 3868 | default: |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 3869 | inode->i_op = &btrfs_special_inode_operations; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 3870 | init_special_inode(inode, inode->i_mode, rdev); |
| 3871 | break; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3872 | } |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 3873 | |
| 3874 | btrfs_update_iflags(inode); |
Filipe Manana | 6771089 | 2016-06-06 11:51:25 +0100 | [diff] [blame] | 3875 | return 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3876 | |
| 3877 | make_bad: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3878 | btrfs_free_path(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3879 | make_bad_inode(inode); |
Filipe Manana | 6771089 | 2016-06-06 11:51:25 +0100 | [diff] [blame] | 3880 | return ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3881 | } |
| 3882 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3883 | /* |
| 3884 | * given a leaf and an inode, copy the inode fields into the leaf |
| 3885 | */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3886 | static void fill_inode_item(struct btrfs_trans_handle *trans, |
| 3887 | struct extent_buffer *leaf, |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3888 | struct btrfs_inode_item *item, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3889 | struct inode *inode) |
| 3890 | { |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3891 | struct btrfs_map_token token; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3892 | |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3893 | btrfs_init_map_token(&token); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3894 | |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3895 | btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token); |
| 3896 | btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token); |
| 3897 | btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size, |
| 3898 | &token); |
| 3899 | btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token); |
| 3900 | btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3901 | |
David Sterba | a937b97 | 2014-12-12 17:39:12 +0100 | [diff] [blame] | 3902 | btrfs_set_token_timespec_sec(leaf, &item->atime, |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3903 | inode->i_atime.tv_sec, &token); |
David Sterba | a937b97 | 2014-12-12 17:39:12 +0100 | [diff] [blame] | 3904 | btrfs_set_token_timespec_nsec(leaf, &item->atime, |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3905 | inode->i_atime.tv_nsec, &token); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3906 | |
David Sterba | a937b97 | 2014-12-12 17:39:12 +0100 | [diff] [blame] | 3907 | btrfs_set_token_timespec_sec(leaf, &item->mtime, |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3908 | inode->i_mtime.tv_sec, &token); |
David Sterba | a937b97 | 2014-12-12 17:39:12 +0100 | [diff] [blame] | 3909 | btrfs_set_token_timespec_nsec(leaf, &item->mtime, |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3910 | inode->i_mtime.tv_nsec, &token); |
| 3911 | |
David Sterba | a937b97 | 2014-12-12 17:39:12 +0100 | [diff] [blame] | 3912 | btrfs_set_token_timespec_sec(leaf, &item->ctime, |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3913 | inode->i_ctime.tv_sec, &token); |
David Sterba | a937b97 | 2014-12-12 17:39:12 +0100 | [diff] [blame] | 3914 | btrfs_set_token_timespec_nsec(leaf, &item->ctime, |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3915 | inode->i_ctime.tv_nsec, &token); |
| 3916 | |
chandan r | 9cc97d6 | 2012-07-04 12:48:07 +0530 | [diff] [blame] | 3917 | btrfs_set_token_timespec_sec(leaf, &item->otime, |
| 3918 | BTRFS_I(inode)->i_otime.tv_sec, &token); |
| 3919 | btrfs_set_token_timespec_nsec(leaf, &item->otime, |
| 3920 | BTRFS_I(inode)->i_otime.tv_nsec, &token); |
| 3921 | |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3922 | btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode), |
| 3923 | &token); |
| 3924 | btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation, |
| 3925 | &token); |
| 3926 | btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token); |
| 3927 | btrfs_set_token_inode_transid(leaf, item, trans->transid, &token); |
| 3928 | btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token); |
| 3929 | btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token); |
| 3930 | btrfs_set_token_inode_block_group(leaf, item, 0, &token); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3931 | } |
| 3932 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3933 | /* |
| 3934 | * copy everything in the in-memory inode into the btree. |
| 3935 | */ |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 3936 | static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3937 | struct btrfs_root *root, struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3938 | { |
| 3939 | struct btrfs_inode_item *inode_item; |
| 3940 | struct btrfs_path *path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3941 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3942 | int ret; |
| 3943 | |
| 3944 | path = btrfs_alloc_path(); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 3945 | if (!path) |
| 3946 | return -ENOMEM; |
| 3947 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 3948 | path->leave_spinning = 1; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 3949 | ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location, |
| 3950 | 1); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3951 | if (ret) { |
| 3952 | if (ret > 0) |
| 3953 | ret = -ENOENT; |
| 3954 | goto failed; |
| 3955 | } |
| 3956 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3957 | leaf = path->nodes[0]; |
| 3958 | inode_item = btrfs_item_ptr(leaf, path->slots[0], |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 3959 | struct btrfs_inode_item); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3960 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3961 | fill_inode_item(trans, leaf, inode_item, inode); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3962 | btrfs_mark_buffer_dirty(leaf); |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 3963 | btrfs_set_inode_last_trans(trans, inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3964 | ret = 0; |
| 3965 | failed: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3966 | btrfs_free_path(path); |
| 3967 | return ret; |
| 3968 | } |
| 3969 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3970 | /* |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 3971 | * copy everything in the in-memory inode into the btree. |
| 3972 | */ |
| 3973 | noinline int btrfs_update_inode(struct btrfs_trans_handle *trans, |
| 3974 | struct btrfs_root *root, struct inode *inode) |
| 3975 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3976 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 3977 | int ret; |
| 3978 | |
| 3979 | /* |
| 3980 | * If the inode is a free space inode, we can deadlock during commit |
| 3981 | * if we put it into the delayed code. |
| 3982 | * |
| 3983 | * The data relocation inode should also be directly updated |
| 3984 | * without delay |
| 3985 | */ |
Nikolay Borisov | 70ddc55 | 2017-02-20 13:50:35 +0200 | [diff] [blame] | 3986 | if (!btrfs_is_free_space_inode(BTRFS_I(inode)) |
Josef Bacik | 1d52c78 | 2014-09-18 11:30:44 -0400 | [diff] [blame] | 3987 | && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3988 | && !test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) { |
Alexander Block | 8ea05e3 | 2012-07-25 17:35:53 +0200 | [diff] [blame] | 3989 | btrfs_update_root_times(trans, root); |
| 3990 | |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 3991 | ret = btrfs_delayed_update_inode(trans, root, inode); |
| 3992 | if (!ret) |
| 3993 | btrfs_set_inode_last_trans(trans, inode); |
| 3994 | return ret; |
| 3995 | } |
| 3996 | |
| 3997 | return btrfs_update_inode_item(trans, root, inode); |
| 3998 | } |
| 3999 | |
Josef Bacik | be6aef6 | 2012-10-22 15:43:12 -0400 | [diff] [blame] | 4000 | noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans, |
| 4001 | struct btrfs_root *root, |
| 4002 | struct inode *inode) |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 4003 | { |
| 4004 | int ret; |
| 4005 | |
| 4006 | ret = btrfs_update_inode(trans, root, inode); |
| 4007 | if (ret == -ENOSPC) |
| 4008 | return btrfs_update_inode_item(trans, root, inode); |
| 4009 | return ret; |
| 4010 | } |
| 4011 | |
| 4012 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 4013 | * unlink helper that gets used here in inode.c and in the tree logging |
| 4014 | * recovery code. It remove a link in a directory with a given name, and |
| 4015 | * also drops the back refs in the inode to the directory |
| 4016 | */ |
Al Viro | 9298679 | 2011-03-04 17:14:37 +0000 | [diff] [blame] | 4017 | static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans, |
| 4018 | struct btrfs_root *root, |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 4019 | struct btrfs_inode *dir, |
| 4020 | struct btrfs_inode *inode, |
Al Viro | 9298679 | 2011-03-04 17:14:37 +0000 | [diff] [blame] | 4021 | const char *name, int name_len) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4022 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4023 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4024 | struct btrfs_path *path; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4025 | int ret = 0; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4026 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4027 | struct btrfs_dir_item *di; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4028 | struct btrfs_key key; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4029 | u64 index; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4030 | u64 ino = btrfs_ino(inode); |
| 4031 | u64 dir_ino = btrfs_ino(dir); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4032 | |
| 4033 | path = btrfs_alloc_path(); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 4034 | if (!path) { |
| 4035 | ret = -ENOMEM; |
Tsutomu Itoh | 554233a | 2011-02-03 03:16:25 +0000 | [diff] [blame] | 4036 | goto out; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 4037 | } |
| 4038 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 4039 | path->leave_spinning = 1; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4040 | di = btrfs_lookup_dir_item(trans, root, path, dir_ino, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4041 | name, name_len, -1); |
| 4042 | if (IS_ERR(di)) { |
| 4043 | ret = PTR_ERR(di); |
| 4044 | goto err; |
| 4045 | } |
| 4046 | if (!di) { |
| 4047 | ret = -ENOENT; |
| 4048 | goto err; |
| 4049 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4050 | leaf = path->nodes[0]; |
| 4051 | btrfs_dir_item_key_to_cpu(leaf, di, &key); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4052 | ret = btrfs_delete_one_dir_name(trans, root, path, di); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 4053 | if (ret) |
| 4054 | goto err; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4055 | btrfs_release_path(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4056 | |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 4057 | /* |
| 4058 | * If we don't have dir index, we have to get it by looking up |
| 4059 | * the inode ref, since we get the inode ref, remove it directly, |
| 4060 | * it is unnecessary to do delayed deletion. |
| 4061 | * |
| 4062 | * But if we have dir index, needn't search inode ref to get it. |
| 4063 | * Since the inode ref is close to the inode item, it is better |
| 4064 | * that we delay to delete it, and just do this deletion when |
| 4065 | * we update the inode item. |
| 4066 | */ |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 4067 | if (inode->dir_index) { |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 4068 | ret = btrfs_delayed_delete_inode_ref(inode); |
| 4069 | if (!ret) { |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 4070 | index = inode->dir_index; |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 4071 | goto skip_backref; |
| 4072 | } |
| 4073 | } |
| 4074 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4075 | ret = btrfs_del_inode_ref(trans, root, name, name_len, ino, |
| 4076 | dir_ino, &index); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4077 | if (ret) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4078 | btrfs_info(fs_info, |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 4079 | "failed to delete reference to %.*s, inode %llu parent %llu", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 4080 | name_len, name, ino, dir_ino); |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 4081 | btrfs_abort_transaction(trans, ret); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4082 | goto err; |
| 4083 | } |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 4084 | skip_backref: |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4085 | ret = btrfs_delete_delayed_dir_index(trans, fs_info, dir, index); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4086 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 4087 | btrfs_abort_transaction(trans, ret); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4088 | goto err; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4089 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4090 | |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 4091 | ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len, inode, |
| 4092 | dir_ino); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4093 | if (ret != 0 && ret != -ENOENT) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 4094 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4095 | goto err; |
| 4096 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4097 | |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 4098 | ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len, dir, |
| 4099 | index); |
Chris Mason | 6418c96 | 2010-10-30 07:34:24 -0400 | [diff] [blame] | 4100 | if (ret == -ENOENT) |
| 4101 | ret = 0; |
Zach Brown | d4e3991 | 2013-04-02 21:02:16 +0000 | [diff] [blame] | 4102 | else if (ret) |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 4103 | btrfs_abort_transaction(trans, ret); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4104 | err: |
| 4105 | btrfs_free_path(path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4106 | if (ret) |
| 4107 | goto out; |
| 4108 | |
Nikolay Borisov | 6ef06d2 | 2017-02-20 13:50:34 +0200 | [diff] [blame] | 4109 | 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] | 4110 | inode_inc_iversion(&inode->vfs_inode); |
| 4111 | inode_inc_iversion(&dir->vfs_inode); |
| 4112 | inode->vfs_inode.i_ctime = dir->vfs_inode.i_mtime = |
| 4113 | dir->vfs_inode.i_ctime = current_time(&inode->vfs_inode); |
| 4114 | ret = btrfs_update_inode(trans, root, &dir->vfs_inode); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4115 | out: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4116 | return ret; |
| 4117 | } |
| 4118 | |
Al Viro | 9298679 | 2011-03-04 17:14:37 +0000 | [diff] [blame] | 4119 | int btrfs_unlink_inode(struct btrfs_trans_handle *trans, |
| 4120 | struct btrfs_root *root, |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 4121 | struct btrfs_inode *dir, struct btrfs_inode *inode, |
Al Viro | 9298679 | 2011-03-04 17:14:37 +0000 | [diff] [blame] | 4122 | const char *name, int name_len) |
| 4123 | { |
| 4124 | int ret; |
| 4125 | ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len); |
| 4126 | if (!ret) { |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 4127 | drop_nlink(&inode->vfs_inode); |
| 4128 | ret = btrfs_update_inode(trans, root, &inode->vfs_inode); |
Al Viro | 9298679 | 2011-03-04 17:14:37 +0000 | [diff] [blame] | 4129 | } |
| 4130 | return ret; |
| 4131 | } |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4132 | |
| 4133 | /* |
| 4134 | * helper to start transaction for unlink and rmdir. |
| 4135 | * |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 4136 | * unlink and rmdir are special in btrfs, they do not always free space, so |
| 4137 | * if we cannot make our reservations the normal way try and see if there is |
| 4138 | * plenty of slack room in the global reserve to migrate, otherwise we cannot |
| 4139 | * allow the unlink to occur. |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4140 | */ |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 4141 | static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir) |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4142 | { |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4143 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4144 | |
Josef Bacik | e70bea5 | 2011-10-11 14:18:24 -0400 | [diff] [blame] | 4145 | /* |
| 4146 | * 1 for the possible orphan item |
| 4147 | * 1 for the dir item |
| 4148 | * 1 for the dir index |
| 4149 | * 1 for the inode ref |
Josef Bacik | e70bea5 | 2011-10-11 14:18:24 -0400 | [diff] [blame] | 4150 | * 1 for the inode |
| 4151 | */ |
Filipe Manana | 8eab77f | 2015-11-13 23:57:16 +0000 | [diff] [blame] | 4152 | return btrfs_start_transaction_fallback_global_rsv(root, 5, 5); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4153 | } |
| 4154 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4155 | static int btrfs_unlink(struct inode *dir, struct dentry *dentry) |
| 4156 | { |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4157 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4158 | struct btrfs_trans_handle *trans; |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 4159 | struct inode *inode = d_inode(dentry); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4160 | int ret; |
| 4161 | |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 4162 | trans = __unlink_start_trans(dir); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4163 | if (IS_ERR(trans)) |
Josef Bacik | 5df6a9f | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4164 | return PTR_ERR(trans); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4165 | |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 4166 | btrfs_record_unlink_dir(trans, BTRFS_I(dir), BTRFS_I(d_inode(dentry)), |
| 4167 | 0); |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 4168 | |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 4169 | ret = btrfs_unlink_inode(trans, root, BTRFS_I(dir), |
| 4170 | BTRFS_I(d_inode(dentry)), dentry->d_name.name, |
| 4171 | dentry->d_name.len); |
Tsutomu Itoh | b532402 | 2011-07-19 07:27:20 +0000 | [diff] [blame] | 4172 | if (ret) |
| 4173 | goto out; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 4174 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4175 | if (inode->i_nlink == 0) { |
Nikolay Borisov | 73f2e54 | 2017-02-20 13:50:59 +0200 | [diff] [blame] | 4176 | ret = btrfs_orphan_add(trans, BTRFS_I(inode)); |
Tsutomu Itoh | b532402 | 2011-07-19 07:27:20 +0000 | [diff] [blame] | 4177 | if (ret) |
| 4178 | goto out; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4179 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 4180 | |
Tsutomu Itoh | b532402 | 2011-07-19 07:27:20 +0000 | [diff] [blame] | 4181 | out: |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 4182 | btrfs_end_transaction(trans); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4183 | btrfs_btree_balance_dirty(root->fs_info); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4184 | return ret; |
| 4185 | } |
| 4186 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4187 | int btrfs_unlink_subvol(struct btrfs_trans_handle *trans, |
| 4188 | struct btrfs_root *root, |
| 4189 | struct inode *dir, u64 objectid, |
| 4190 | const char *name, int name_len) |
| 4191 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4192 | struct btrfs_fs_info *fs_info = root->fs_info; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4193 | struct btrfs_path *path; |
| 4194 | struct extent_buffer *leaf; |
| 4195 | struct btrfs_dir_item *di; |
| 4196 | struct btrfs_key key; |
| 4197 | u64 index; |
| 4198 | int ret; |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 4199 | u64 dir_ino = btrfs_ino(BTRFS_I(dir)); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4200 | |
| 4201 | path = btrfs_alloc_path(); |
| 4202 | if (!path) |
| 4203 | return -ENOMEM; |
| 4204 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4205 | di = btrfs_lookup_dir_item(trans, root, path, dir_ino, |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4206 | name, name_len, -1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4207 | if (IS_ERR_OR_NULL(di)) { |
| 4208 | if (!di) |
| 4209 | ret = -ENOENT; |
| 4210 | else |
| 4211 | ret = PTR_ERR(di); |
| 4212 | goto out; |
| 4213 | } |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4214 | |
| 4215 | leaf = path->nodes[0]; |
| 4216 | btrfs_dir_item_key_to_cpu(leaf, di, &key); |
| 4217 | WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid); |
| 4218 | ret = btrfs_delete_one_dir_name(trans, root, path, di); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4219 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 4220 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4221 | goto out; |
| 4222 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4223 | btrfs_release_path(path); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4224 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4225 | ret = btrfs_del_root_ref(trans, fs_info, objectid, |
| 4226 | root->root_key.objectid, dir_ino, |
| 4227 | &index, name, name_len); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4228 | if (ret < 0) { |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4229 | if (ret != -ENOENT) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 4230 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4231 | goto out; |
| 4232 | } |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4233 | di = btrfs_search_dir_index_item(root, path, dir_ino, |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4234 | name, name_len); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4235 | if (IS_ERR_OR_NULL(di)) { |
| 4236 | if (!di) |
| 4237 | ret = -ENOENT; |
| 4238 | else |
| 4239 | ret = PTR_ERR(di); |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 4240 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4241 | goto out; |
| 4242 | } |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4243 | |
| 4244 | leaf = path->nodes[0]; |
| 4245 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4246 | btrfs_release_path(path); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4247 | index = key.offset; |
| 4248 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4249 | btrfs_release_path(path); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4250 | |
Nikolay Borisov | e67bbbb | 2017-01-10 20:35:36 +0200 | [diff] [blame] | 4251 | 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] | 4252 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 4253 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4254 | goto out; |
| 4255 | } |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 4256 | |
Nikolay Borisov | 6ef06d2 | 2017-02-20 13:50:34 +0200 | [diff] [blame] | 4257 | 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] | 4258 | inode_inc_iversion(dir); |
Deepa Dinamani | c2050a4 | 2016-09-14 07:48:06 -0700 | [diff] [blame] | 4259 | dir->i_mtime = dir->i_ctime = current_time(dir); |
Josef Bacik | 5a24e84 | 2012-08-08 10:12:59 -0600 | [diff] [blame] | 4260 | ret = btrfs_update_inode_fallback(trans, root, dir); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4261 | if (ret) |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 4262 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4263 | out: |
Josef Bacik | 71d7aed | 2011-06-14 14:24:32 -0400 | [diff] [blame] | 4264 | btrfs_free_path(path); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4265 | return ret; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4266 | } |
| 4267 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4268 | static int btrfs_rmdir(struct inode *dir, struct dentry *dentry) |
| 4269 | { |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 4270 | struct inode *inode = d_inode(dentry); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 4271 | int err = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4272 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4273 | struct btrfs_trans_handle *trans; |
Filipe Manana | 44f714d | 2016-06-06 16:11:13 +0100 | [diff] [blame] | 4274 | u64 last_unlink_trans; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4275 | |
David Sterba | b3ae244 | 2012-09-13 16:04:34 -0600 | [diff] [blame] | 4276 | if (inode->i_size > BTRFS_EMPTY_DIR_SIZE) |
Yan | 134d451 | 2007-10-25 15:49:25 -0400 | [diff] [blame] | 4277 | return -ENOTEMPTY; |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 4278 | if (btrfs_ino(BTRFS_I(inode)) == BTRFS_FIRST_FREE_OBJECTID) |
David Sterba | b3ae244 | 2012-09-13 16:04:34 -0600 | [diff] [blame] | 4279 | return -EPERM; |
Yan | 134d451 | 2007-10-25 15:49:25 -0400 | [diff] [blame] | 4280 | |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 4281 | trans = __unlink_start_trans(dir); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4282 | if (IS_ERR(trans)) |
Josef Bacik | 5df6a9f | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4283 | return PTR_ERR(trans); |
Josef Bacik | 5df6a9f | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4284 | |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 4285 | if (unlikely(btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4286 | err = btrfs_unlink_subvol(trans, root, dir, |
| 4287 | BTRFS_I(inode)->location.objectid, |
| 4288 | dentry->d_name.name, |
| 4289 | dentry->d_name.len); |
| 4290 | goto out; |
| 4291 | } |
| 4292 | |
Nikolay Borisov | 73f2e54 | 2017-02-20 13:50:59 +0200 | [diff] [blame] | 4293 | err = btrfs_orphan_add(trans, BTRFS_I(inode)); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 4294 | if (err) |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4295 | goto out; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 4296 | |
Filipe Manana | 44f714d | 2016-06-06 16:11:13 +0100 | [diff] [blame] | 4297 | last_unlink_trans = BTRFS_I(inode)->last_unlink_trans; |
| 4298 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4299 | /* now the directory is empty */ |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 4300 | err = btrfs_unlink_inode(trans, root, BTRFS_I(dir), |
| 4301 | BTRFS_I(d_inode(dentry)), dentry->d_name.name, |
| 4302 | dentry->d_name.len); |
Filipe Manana | 44f714d | 2016-06-06 16:11:13 +0100 | [diff] [blame] | 4303 | if (!err) { |
Nikolay Borisov | 6ef06d2 | 2017-02-20 13:50:34 +0200 | [diff] [blame] | 4304 | btrfs_i_size_write(BTRFS_I(inode), 0); |
Filipe Manana | 44f714d | 2016-06-06 16:11:13 +0100 | [diff] [blame] | 4305 | /* |
| 4306 | * Propagate the last_unlink_trans value of the deleted dir to |
| 4307 | * its parent directory. This is to prevent an unrecoverable |
| 4308 | * log tree in the case we do something like this: |
| 4309 | * 1) create dir foo |
| 4310 | * 2) create snapshot under dir foo |
| 4311 | * 3) delete the snapshot |
| 4312 | * 4) rmdir foo |
| 4313 | * 5) mkdir foo |
| 4314 | * 6) fsync foo or some file inside foo |
| 4315 | */ |
| 4316 | if (last_unlink_trans >= trans->transid) |
| 4317 | BTRFS_I(dir)->last_unlink_trans = last_unlink_trans; |
| 4318 | } |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4319 | out: |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 4320 | btrfs_end_transaction(trans); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4321 | btrfs_btree_balance_dirty(root->fs_info); |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 4322 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4323 | return err; |
| 4324 | } |
| 4325 | |
Chris Mason | 28f75a0 | 2015-02-04 06:59:29 -0800 | [diff] [blame] | 4326 | static int truncate_space_check(struct btrfs_trans_handle *trans, |
| 4327 | struct btrfs_root *root, |
| 4328 | u64 bytes_deleted) |
| 4329 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4330 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | 28f75a0 | 2015-02-04 06:59:29 -0800 | [diff] [blame] | 4331 | int ret; |
| 4332 | |
Josef Bacik | dc95f7b | 2016-01-13 11:48:06 -0500 | [diff] [blame] | 4333 | /* |
| 4334 | * This is only used to apply pressure to the enospc system, we don't |
| 4335 | * intend to use this reservation at all. |
| 4336 | */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4337 | bytes_deleted = btrfs_csum_bytes_to_leaves(fs_info, bytes_deleted); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4338 | bytes_deleted *= fs_info->nodesize; |
| 4339 | ret = btrfs_block_rsv_add(root, &fs_info->trans_block_rsv, |
Chris Mason | 28f75a0 | 2015-02-04 06:59:29 -0800 | [diff] [blame] | 4340 | bytes_deleted, BTRFS_RESERVE_NO_FLUSH); |
Josef Bacik | dc95f7b | 2016-01-13 11:48:06 -0500 | [diff] [blame] | 4341 | if (!ret) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4342 | trace_btrfs_space_reservation(fs_info, "transaction", |
Josef Bacik | dc95f7b | 2016-01-13 11:48:06 -0500 | [diff] [blame] | 4343 | trans->transid, |
| 4344 | bytes_deleted, 1); |
Chris Mason | 28f75a0 | 2015-02-04 06:59:29 -0800 | [diff] [blame] | 4345 | trans->bytes_reserved += bytes_deleted; |
Josef Bacik | dc95f7b | 2016-01-13 11:48:06 -0500 | [diff] [blame] | 4346 | } |
Chris Mason | 28f75a0 | 2015-02-04 06:59:29 -0800 | [diff] [blame] | 4347 | return ret; |
| 4348 | |
| 4349 | } |
| 4350 | |
Filipe Manana | 0305cd5 | 2015-10-16 12:34:25 +0100 | [diff] [blame] | 4351 | static int truncate_inline_extent(struct inode *inode, |
| 4352 | struct btrfs_path *path, |
| 4353 | struct btrfs_key *found_key, |
| 4354 | const u64 item_end, |
| 4355 | const u64 new_size) |
| 4356 | { |
| 4357 | struct extent_buffer *leaf = path->nodes[0]; |
| 4358 | int slot = path->slots[0]; |
| 4359 | struct btrfs_file_extent_item *fi; |
| 4360 | u32 size = (u32)(new_size - found_key->offset); |
| 4361 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 4362 | |
| 4363 | fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item); |
| 4364 | |
| 4365 | if (btrfs_file_extent_compression(leaf, fi) != BTRFS_COMPRESS_NONE) { |
| 4366 | loff_t offset = new_size; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 4367 | loff_t page_end = ALIGN(offset, PAGE_SIZE); |
Filipe Manana | 0305cd5 | 2015-10-16 12:34:25 +0100 | [diff] [blame] | 4368 | |
| 4369 | /* |
| 4370 | * Zero out the remaining of the last page of our inline extent, |
| 4371 | * instead of directly truncating our inline extent here - that |
| 4372 | * would be much more complex (decompressing all the data, then |
| 4373 | * compressing the truncated data, which might be bigger than |
| 4374 | * the size of the inline extent, resize the extent, etc). |
| 4375 | * We release the path because to get the page we might need to |
| 4376 | * read the extent item from disk (data not in the page cache). |
| 4377 | */ |
| 4378 | btrfs_release_path(path); |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4379 | return btrfs_truncate_block(inode, offset, page_end - offset, |
| 4380 | 0); |
Filipe Manana | 0305cd5 | 2015-10-16 12:34:25 +0100 | [diff] [blame] | 4381 | } |
| 4382 | |
| 4383 | btrfs_set_file_extent_ram_bytes(leaf, fi, size); |
| 4384 | size = btrfs_file_extent_calc_inline_size(size); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4385 | btrfs_truncate_item(root->fs_info, path, size, 1); |
Filipe Manana | 0305cd5 | 2015-10-16 12:34:25 +0100 | [diff] [blame] | 4386 | |
| 4387 | if (test_bit(BTRFS_ROOT_REF_COWS, &root->state)) |
| 4388 | inode_sub_bytes(inode, item_end + 1 - new_size); |
| 4389 | |
| 4390 | return 0; |
| 4391 | } |
| 4392 | |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 4393 | /* |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4394 | * this can truncate away extent items, csum items and directory items. |
| 4395 | * 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] | 4396 | * any higher than new_size |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4397 | * |
| 4398 | * csum items that cross the new i_size are truncated to the new size |
| 4399 | * as well. |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 4400 | * |
| 4401 | * min_type is the minimum key type to truncate down to. If set to 0, this |
| 4402 | * 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] | 4403 | */ |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4404 | int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans, |
| 4405 | struct btrfs_root *root, |
| 4406 | struct inode *inode, |
| 4407 | u64 new_size, u32 min_type) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4408 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4409 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4410 | struct btrfs_path *path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4411 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4412 | struct btrfs_file_extent_item *fi; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4413 | struct btrfs_key key; |
| 4414 | struct btrfs_key found_key; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4415 | u64 extent_start = 0; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4416 | u64 extent_num_bytes = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4417 | u64 extent_offset = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4418 | u64 item_end = 0; |
Filipe Manana | c1aa457 | 2015-06-20 18:20:09 +0100 | [diff] [blame] | 4419 | u64 last_size = new_size; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4420 | u32 found_type = (u8)-1; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4421 | int found_extent; |
| 4422 | int del_item; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4423 | int pending_del_nr = 0; |
| 4424 | int pending_del_slot = 0; |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4425 | int extent_type = -1; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4426 | int ret; |
| 4427 | int err = 0; |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 4428 | u64 ino = btrfs_ino(BTRFS_I(inode)); |
Chris Mason | 28ed134 | 2014-12-17 09:41:04 -0800 | [diff] [blame] | 4429 | u64 bytes_deleted = 0; |
Josef Bacik | 1262133 | 2015-02-03 07:50:16 -0800 | [diff] [blame] | 4430 | bool be_nice = 0; |
| 4431 | bool should_throttle = 0; |
Chris Mason | 28f75a0 | 2015-02-04 06:59:29 -0800 | [diff] [blame] | 4432 | bool should_end = 0; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4433 | |
| 4434 | BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4435 | |
Chris Mason | 28ed134 | 2014-12-17 09:41:04 -0800 | [diff] [blame] | 4436 | /* |
| 4437 | * for non-free space inodes and ref cows, we want to back off from |
| 4438 | * time to time |
| 4439 | */ |
Nikolay Borisov | 70ddc55 | 2017-02-20 13:50:35 +0200 | [diff] [blame] | 4440 | if (!btrfs_is_free_space_inode(BTRFS_I(inode)) && |
Chris Mason | 28ed134 | 2014-12-17 09:41:04 -0800 | [diff] [blame] | 4441 | test_bit(BTRFS_ROOT_REF_COWS, &root->state)) |
| 4442 | be_nice = 1; |
| 4443 | |
Mark Fasheh | 0eb0e19 | 2011-07-12 16:44:10 -0700 | [diff] [blame] | 4444 | path = btrfs_alloc_path(); |
| 4445 | if (!path) |
| 4446 | return -ENOMEM; |
David Sterba | e4058b5 | 2015-11-27 16:31:35 +0100 | [diff] [blame] | 4447 | path->reada = READA_BACK; |
Mark Fasheh | 0eb0e19 | 2011-07-12 16:44:10 -0700 | [diff] [blame] | 4448 | |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4449 | /* |
| 4450 | * We want to drop from the next block forward in case this new size is |
| 4451 | * not block aligned since we will be keeping the last block of the |
| 4452 | * extent just the way it is. |
| 4453 | */ |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 4454 | if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) || |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4455 | root == fs_info->tree_root) |
Nikolay Borisov | dcdbc05 | 2017-02-20 13:50:45 +0200 | [diff] [blame] | 4456 | btrfs_drop_extent_cache(BTRFS_I(inode), ALIGN(new_size, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4457 | fs_info->sectorsize), |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 4458 | (u64)-1, 0); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4459 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 4460 | /* |
| 4461 | * This function is also used to drop the items in the log tree before |
| 4462 | * we relog the inode, so if root != BTRFS_I(inode)->root, it means |
| 4463 | * it is used to drop the loged items. So we shouldn't kill the delayed |
| 4464 | * items. |
| 4465 | */ |
| 4466 | if (min_type == 0 && root == BTRFS_I(inode)->root) |
Nikolay Borisov | 4ccb5c7 | 2017-01-10 20:35:38 +0200 | [diff] [blame] | 4467 | btrfs_kill_delayed_inode_items(BTRFS_I(inode)); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 4468 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4469 | key.objectid = ino; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4470 | key.offset = (u64)-1; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4471 | key.type = (u8)-1; |
| 4472 | |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4473 | search_again: |
Chris Mason | 28ed134 | 2014-12-17 09:41:04 -0800 | [diff] [blame] | 4474 | /* |
| 4475 | * with a 16K leaf size and 128MB extents, you can actually queue |
| 4476 | * up a huge file in a single leaf. Most of the time that |
| 4477 | * bytes_deleted is > 0, it will be huge by the time we get here |
| 4478 | */ |
Byongho Lee | ee22184 | 2015-12-15 01:42:10 +0900 | [diff] [blame] | 4479 | if (be_nice && bytes_deleted > SZ_32M) { |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 4480 | if (btrfs_should_end_transaction(trans)) { |
Chris Mason | 28ed134 | 2014-12-17 09:41:04 -0800 | [diff] [blame] | 4481 | err = -EAGAIN; |
| 4482 | goto error; |
| 4483 | } |
| 4484 | } |
| 4485 | |
| 4486 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 4487 | path->leave_spinning = 1; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4488 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4489 | if (ret < 0) { |
| 4490 | err = ret; |
| 4491 | goto out; |
| 4492 | } |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4493 | |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4494 | if (ret > 0) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4495 | /* there are no items in the tree for us to truncate, we're |
| 4496 | * done |
| 4497 | */ |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4498 | if (path->slots[0] == 0) |
| 4499 | goto out; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4500 | path->slots[0]--; |
| 4501 | } |
| 4502 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4503 | while (1) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4504 | fi = NULL; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4505 | leaf = path->nodes[0]; |
| 4506 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 4507 | found_type = found_key.type; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4508 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4509 | if (found_key.objectid != ino) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4510 | break; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4511 | |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4512 | if (found_type < min_type) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4513 | break; |
| 4514 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4515 | item_end = found_key.offset; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4516 | if (found_type == BTRFS_EXTENT_DATA_KEY) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4517 | fi = btrfs_item_ptr(leaf, path->slots[0], |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4518 | struct btrfs_file_extent_item); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4519 | extent_type = btrfs_file_extent_type(leaf, fi); |
| 4520 | if (extent_type != BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4521 | item_end += |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4522 | btrfs_file_extent_num_bytes(leaf, fi); |
Liu Bo | 09ed2f1 | 2017-03-10 11:09:48 -0800 | [diff] [blame] | 4523 | |
| 4524 | trace_btrfs_truncate_show_fi_regular( |
| 4525 | BTRFS_I(inode), leaf, fi, |
| 4526 | found_key.offset); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4527 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4528 | item_end += btrfs_file_extent_inline_len(leaf, |
Chris Mason | 514ac8a | 2014-01-03 21:07:00 -0800 | [diff] [blame] | 4529 | path->slots[0], fi); |
Liu Bo | 09ed2f1 | 2017-03-10 11:09:48 -0800 | [diff] [blame] | 4530 | |
| 4531 | trace_btrfs_truncate_show_fi_inline( |
| 4532 | BTRFS_I(inode), leaf, fi, path->slots[0], |
| 4533 | found_key.offset); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4534 | } |
Yan | 008630c | 2007-11-07 13:31:09 -0500 | [diff] [blame] | 4535 | item_end--; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4536 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4537 | if (found_type > min_type) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4538 | del_item = 1; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4539 | } else { |
Filipe Manana | 76b42ab | 2017-02-14 16:56:01 +0000 | [diff] [blame] | 4540 | if (item_end < new_size) |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4541 | break; |
| 4542 | if (found_key.offset >= new_size) |
| 4543 | del_item = 1; |
| 4544 | else |
| 4545 | del_item = 0; |
| 4546 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4547 | found_extent = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4548 | /* FIXME, shrink the extent if the ref count is only 1 */ |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4549 | if (found_type != BTRFS_EXTENT_DATA_KEY) |
| 4550 | goto delete; |
| 4551 | |
Josef Bacik | 7f4f6e0 | 2013-08-29 16:43:28 -0400 | [diff] [blame] | 4552 | if (del_item) |
| 4553 | last_size = found_key.offset; |
| 4554 | else |
| 4555 | last_size = new_size; |
| 4556 | |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4557 | if (extent_type != BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4558 | u64 num_dec; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4559 | extent_start = btrfs_file_extent_disk_bytenr(leaf, fi); |
Miao Xie | f70a9a6b | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 4560 | if (!del_item) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4561 | u64 orig_num_bytes = |
| 4562 | btrfs_file_extent_num_bytes(leaf, fi); |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 4563 | extent_num_bytes = ALIGN(new_size - |
| 4564 | found_key.offset, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4565 | fs_info->sectorsize); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4566 | btrfs_set_file_extent_num_bytes(leaf, fi, |
| 4567 | extent_num_bytes); |
| 4568 | num_dec = (orig_num_bytes - |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 4569 | extent_num_bytes); |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 4570 | if (test_bit(BTRFS_ROOT_REF_COWS, |
| 4571 | &root->state) && |
| 4572 | extent_start != 0) |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 4573 | inode_sub_bytes(inode, num_dec); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4574 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4575 | } else { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4576 | extent_num_bytes = |
| 4577 | btrfs_file_extent_disk_num_bytes(leaf, |
| 4578 | fi); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4579 | extent_offset = found_key.offset - |
| 4580 | btrfs_file_extent_offset(leaf, fi); |
| 4581 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4582 | /* FIXME blocksize != 4096 */ |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 4583 | num_dec = btrfs_file_extent_num_bytes(leaf, fi); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4584 | if (extent_start != 0) { |
| 4585 | found_extent = 1; |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 4586 | if (test_bit(BTRFS_ROOT_REF_COWS, |
| 4587 | &root->state)) |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 4588 | inode_sub_bytes(inode, num_dec); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4589 | } |
| 4590 | } |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 4591 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4592 | /* |
| 4593 | * we can't truncate inline items that have had |
| 4594 | * special encodings |
| 4595 | */ |
| 4596 | if (!del_item && |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4597 | btrfs_file_extent_encryption(leaf, fi) == 0 && |
| 4598 | btrfs_file_extent_other_encoding(leaf, fi) == 0) { |
Chris Mason | 514ac8a | 2014-01-03 21:07:00 -0800 | [diff] [blame] | 4599 | |
| 4600 | /* |
Filipe Manana | 0305cd5 | 2015-10-16 12:34:25 +0100 | [diff] [blame] | 4601 | * Need to release path in order to truncate a |
| 4602 | * compressed extent. So delete any accumulated |
| 4603 | * extent items so far. |
Chris Mason | 514ac8a | 2014-01-03 21:07:00 -0800 | [diff] [blame] | 4604 | */ |
Filipe Manana | 0305cd5 | 2015-10-16 12:34:25 +0100 | [diff] [blame] | 4605 | if (btrfs_file_extent_compression(leaf, fi) != |
| 4606 | BTRFS_COMPRESS_NONE && pending_del_nr) { |
| 4607 | err = btrfs_del_items(trans, root, path, |
| 4608 | pending_del_slot, |
| 4609 | pending_del_nr); |
| 4610 | if (err) { |
| 4611 | btrfs_abort_transaction(trans, |
Filipe Manana | 0305cd5 | 2015-10-16 12:34:25 +0100 | [diff] [blame] | 4612 | err); |
| 4613 | goto error; |
| 4614 | } |
| 4615 | pending_del_nr = 0; |
| 4616 | } |
| 4617 | |
| 4618 | err = truncate_inline_extent(inode, path, |
| 4619 | &found_key, |
| 4620 | item_end, |
| 4621 | new_size); |
| 4622 | if (err) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 4623 | btrfs_abort_transaction(trans, err); |
Filipe Manana | 0305cd5 | 2015-10-16 12:34:25 +0100 | [diff] [blame] | 4624 | goto error; |
| 4625 | } |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 4626 | } else if (test_bit(BTRFS_ROOT_REF_COWS, |
| 4627 | &root->state)) { |
Filipe Manana | 0305cd5 | 2015-10-16 12:34:25 +0100 | [diff] [blame] | 4628 | inode_sub_bytes(inode, item_end + 1 - new_size); |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 4629 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4630 | } |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4631 | delete: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4632 | if (del_item) { |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4633 | if (!pending_del_nr) { |
| 4634 | /* no pending yet, add ourselves */ |
| 4635 | pending_del_slot = path->slots[0]; |
| 4636 | pending_del_nr = 1; |
| 4637 | } else if (pending_del_nr && |
| 4638 | path->slots[0] + 1 == pending_del_slot) { |
| 4639 | /* hop on the pending chunk */ |
| 4640 | pending_del_nr++; |
| 4641 | pending_del_slot = path->slots[0]; |
| 4642 | } else { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4643 | BUG(); |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4644 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4645 | } else { |
| 4646 | break; |
| 4647 | } |
Chris Mason | 28f75a0 | 2015-02-04 06:59:29 -0800 | [diff] [blame] | 4648 | should_throttle = 0; |
| 4649 | |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 4650 | if (found_extent && |
| 4651 | (test_bit(BTRFS_ROOT_REF_COWS, &root->state) || |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4652 | root == fs_info->tree_root)) { |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 4653 | btrfs_set_path_blocking(path); |
Chris Mason | 28ed134 | 2014-12-17 09:41:04 -0800 | [diff] [blame] | 4654 | bytes_deleted += extent_num_bytes; |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4655 | ret = btrfs_free_extent(trans, fs_info, extent_start, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4656 | extent_num_bytes, 0, |
| 4657 | btrfs_header_owner(leaf), |
Filipe Manana | b06c4bf | 2015-10-23 07:52:54 +0100 | [diff] [blame] | 4658 | ino, extent_offset); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4659 | BUG_ON(ret); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4660 | if (btrfs_should_throttle_delayed_refs(trans, fs_info)) |
| 4661 | btrfs_async_run_delayed_refs(fs_info, |
Wang Xiaoguang | dd4b857 | 2016-10-18 15:56:13 +0800 | [diff] [blame] | 4662 | trans->delayed_ref_updates * 2, |
| 4663 | trans->transid, 0); |
Chris Mason | 28f75a0 | 2015-02-04 06:59:29 -0800 | [diff] [blame] | 4664 | if (be_nice) { |
| 4665 | if (truncate_space_check(trans, root, |
| 4666 | extent_num_bytes)) { |
| 4667 | should_end = 1; |
| 4668 | } |
| 4669 | if (btrfs_should_throttle_delayed_refs(trans, |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4670 | fs_info)) |
Chris Mason | 28f75a0 | 2015-02-04 06:59:29 -0800 | [diff] [blame] | 4671 | should_throttle = 1; |
Chris Mason | 28f75a0 | 2015-02-04 06:59:29 -0800 | [diff] [blame] | 4672 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4673 | } |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4674 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4675 | if (found_type == BTRFS_INODE_ITEM_KEY) |
| 4676 | break; |
| 4677 | |
| 4678 | if (path->slots[0] == 0 || |
Josef Bacik | 1262133 | 2015-02-03 07:50:16 -0800 | [diff] [blame] | 4679 | path->slots[0] != pending_del_slot || |
Chris Mason | 28f75a0 | 2015-02-04 06:59:29 -0800 | [diff] [blame] | 4680 | should_throttle || should_end) { |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4681 | if (pending_del_nr) { |
| 4682 | ret = btrfs_del_items(trans, root, path, |
| 4683 | pending_del_slot, |
| 4684 | pending_del_nr); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4685 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 4686 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4687 | goto error; |
| 4688 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4689 | pending_del_nr = 0; |
| 4690 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4691 | btrfs_release_path(path); |
Chris Mason | 28f75a0 | 2015-02-04 06:59:29 -0800 | [diff] [blame] | 4692 | if (should_throttle) { |
Josef Bacik | 1262133 | 2015-02-03 07:50:16 -0800 | [diff] [blame] | 4693 | unsigned long updates = trans->delayed_ref_updates; |
| 4694 | if (updates) { |
| 4695 | trans->delayed_ref_updates = 0; |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4696 | ret = btrfs_run_delayed_refs(trans, |
| 4697 | fs_info, |
| 4698 | updates * 2); |
Josef Bacik | 1262133 | 2015-02-03 07:50:16 -0800 | [diff] [blame] | 4699 | if (ret && !err) |
| 4700 | err = ret; |
| 4701 | } |
| 4702 | } |
Chris Mason | 28f75a0 | 2015-02-04 06:59:29 -0800 | [diff] [blame] | 4703 | /* |
| 4704 | * if we failed to refill our space rsv, bail out |
| 4705 | * and let the transaction restart |
| 4706 | */ |
| 4707 | if (should_end) { |
| 4708 | err = -EAGAIN; |
| 4709 | goto error; |
| 4710 | } |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4711 | goto search_again; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4712 | } else { |
| 4713 | path->slots[0]--; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4714 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4715 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4716 | out: |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4717 | if (pending_del_nr) { |
| 4718 | ret = btrfs_del_items(trans, root, path, pending_del_slot, |
| 4719 | pending_del_nr); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4720 | if (ret) |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 4721 | btrfs_abort_transaction(trans, ret); |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4722 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4723 | error: |
Filipe Manana | 76b42ab | 2017-02-14 16:56:01 +0000 | [diff] [blame] | 4724 | if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) { |
| 4725 | ASSERT(last_size >= new_size); |
| 4726 | if (!err && last_size > new_size) |
| 4727 | last_size = new_size; |
Josef Bacik | 7f4f6e0 | 2013-08-29 16:43:28 -0400 | [diff] [blame] | 4728 | btrfs_ordered_update_i_size(inode, last_size, NULL); |
Filipe Manana | 76b42ab | 2017-02-14 16:56:01 +0000 | [diff] [blame] | 4729 | } |
Chris Mason | 28ed134 | 2014-12-17 09:41:04 -0800 | [diff] [blame] | 4730 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4731 | btrfs_free_path(path); |
Chris Mason | 28ed134 | 2014-12-17 09:41:04 -0800 | [diff] [blame] | 4732 | |
Byongho Lee | ee22184 | 2015-12-15 01:42:10 +0900 | [diff] [blame] | 4733 | if (be_nice && bytes_deleted > SZ_32M) { |
Chris Mason | 28ed134 | 2014-12-17 09:41:04 -0800 | [diff] [blame] | 4734 | unsigned long updates = trans->delayed_ref_updates; |
| 4735 | if (updates) { |
| 4736 | trans->delayed_ref_updates = 0; |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4737 | ret = btrfs_run_delayed_refs(trans, fs_info, |
| 4738 | updates * 2); |
Chris Mason | 28ed134 | 2014-12-17 09:41:04 -0800 | [diff] [blame] | 4739 | if (ret && !err) |
| 4740 | err = ret; |
| 4741 | } |
| 4742 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4743 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4744 | } |
| 4745 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4746 | /* |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4747 | * btrfs_truncate_block - read, zero a chunk and write a block |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 4748 | * @inode - inode that we're zeroing |
| 4749 | * @from - the offset to start zeroing |
| 4750 | * @len - the length to zero, 0 to zero the entire range respective to the |
| 4751 | * offset |
| 4752 | * @front - zero up to the offset instead of from the offset on |
| 4753 | * |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4754 | * 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] | 4755 | * 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] | 4756 | */ |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4757 | 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] | 4758 | int front) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4759 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4760 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 4761 | struct address_space *mapping = inode->i_mapping; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4762 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
| 4763 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4764 | struct extent_state *cached_state = NULL; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4765 | char *kaddr; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4766 | u32 blocksize = fs_info->sectorsize; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 4767 | pgoff_t index = from >> PAGE_SHIFT; |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4768 | unsigned offset = from & (blocksize - 1); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4769 | struct page *page; |
Josef Bacik | 3b16a4e | 2011-09-21 15:05:58 -0400 | [diff] [blame] | 4770 | gfp_t mask = btrfs_alloc_write_mask(mapping); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4771 | int ret = 0; |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4772 | u64 block_start; |
| 4773 | u64 block_end; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4774 | |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 4775 | if ((offset & (blocksize - 1)) == 0 && |
| 4776 | (!len || ((len & (blocksize - 1)) == 0))) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4777 | goto out; |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4778 | |
Qu Wenruo | 7cf5b97 | 2015-09-08 17:25:55 +0800 | [diff] [blame] | 4779 | ret = btrfs_delalloc_reserve_space(inode, |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4780 | round_down(from, blocksize), blocksize); |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 4781 | if (ret) |
| 4782 | goto out; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4783 | |
Chris Mason | 211c17f | 2008-05-15 09:13:45 -0400 | [diff] [blame] | 4784 | again: |
Josef Bacik | 3b16a4e | 2011-09-21 15:05:58 -0400 | [diff] [blame] | 4785 | page = find_or_create_page(mapping, index, mask); |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 4786 | if (!page) { |
Qu Wenruo | 7cf5b97 | 2015-09-08 17:25:55 +0800 | [diff] [blame] | 4787 | btrfs_delalloc_release_space(inode, |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4788 | round_down(from, blocksize), |
| 4789 | blocksize); |
Miao Xie | ac6a2b3 | 2012-12-05 10:56:13 +0000 | [diff] [blame] | 4790 | ret = -ENOMEM; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4791 | goto out; |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 4792 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4793 | |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4794 | block_start = round_down(from, blocksize); |
| 4795 | block_end = block_start + blocksize - 1; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4796 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4797 | if (!PageUptodate(page)) { |
| 4798 | ret = btrfs_readpage(NULL, page); |
| 4799 | lock_page(page); |
Chris Mason | 211c17f | 2008-05-15 09:13:45 -0400 | [diff] [blame] | 4800 | if (page->mapping != mapping) { |
| 4801 | unlock_page(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 4802 | put_page(page); |
Chris Mason | 211c17f | 2008-05-15 09:13:45 -0400 | [diff] [blame] | 4803 | goto again; |
| 4804 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4805 | if (!PageUptodate(page)) { |
| 4806 | ret = -EIO; |
Chris Mason | 8964222 | 2008-07-24 09:41:53 -0400 | [diff] [blame] | 4807 | goto out_unlock; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4808 | } |
| 4809 | } |
Chris Mason | 211c17f | 2008-05-15 09:13:45 -0400 | [diff] [blame] | 4810 | wait_on_page_writeback(page); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4811 | |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4812 | lock_extent_bits(io_tree, block_start, block_end, &cached_state); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4813 | set_page_extent_mapped(page); |
| 4814 | |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4815 | ordered = btrfs_lookup_ordered_extent(inode, block_start); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4816 | if (ordered) { |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4817 | unlock_extent_cached(io_tree, block_start, block_end, |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4818 | &cached_state, GFP_NOFS); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4819 | unlock_page(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 4820 | put_page(page); |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 4821 | btrfs_start_ordered_extent(inode, ordered, 1); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4822 | btrfs_put_ordered_extent(ordered); |
| 4823 | goto again; |
| 4824 | } |
| 4825 | |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4826 | clear_extent_bit(&BTRFS_I(inode)->io_tree, block_start, block_end, |
Liu Bo | 9e8a4a8 | 2012-09-05 19:10:51 -0600 | [diff] [blame] | 4827 | EXTENT_DIRTY | EXTENT_DELALLOC | |
| 4828 | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4829 | 0, 0, &cached_state, GFP_NOFS); |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 4830 | |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4831 | ret = btrfs_set_extent_delalloc(inode, block_start, block_end, |
Qu Wenruo | ba8b04c | 2016-07-19 16:50:36 +0800 | [diff] [blame] | 4832 | &cached_state, 0); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4833 | if (ret) { |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4834 | unlock_extent_cached(io_tree, block_start, block_end, |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4835 | &cached_state, GFP_NOFS); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4836 | goto out_unlock; |
| 4837 | } |
| 4838 | |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4839 | if (offset != blocksize) { |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 4840 | if (!len) |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4841 | len = blocksize - offset; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4842 | kaddr = kmap(page); |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 4843 | if (front) |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4844 | memset(kaddr + (block_start - page_offset(page)), |
| 4845 | 0, offset); |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 4846 | else |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4847 | memset(kaddr + (block_start - page_offset(page)) + offset, |
| 4848 | 0, len); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4849 | flush_dcache_page(page); |
| 4850 | kunmap(page); |
| 4851 | } |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 4852 | ClearPageChecked(page); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4853 | set_page_dirty(page); |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4854 | unlock_extent_cached(io_tree, block_start, block_end, &cached_state, |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4855 | GFP_NOFS); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4856 | |
Chris Mason | 8964222 | 2008-07-24 09:41:53 -0400 | [diff] [blame] | 4857 | out_unlock: |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 4858 | if (ret) |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4859 | btrfs_delalloc_release_space(inode, block_start, |
| 4860 | blocksize); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4861 | unlock_page(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 4862 | put_page(page); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4863 | out: |
| 4864 | return ret; |
| 4865 | } |
| 4866 | |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4867 | static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode, |
| 4868 | u64 offset, u64 len) |
| 4869 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4870 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4871 | struct btrfs_trans_handle *trans; |
| 4872 | int ret; |
| 4873 | |
| 4874 | /* |
| 4875 | * Still need to make sure the inode looks like it's been updated so |
| 4876 | * that any holes get logged if we fsync. |
| 4877 | */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4878 | if (btrfs_fs_incompat(fs_info, NO_HOLES)) { |
| 4879 | BTRFS_I(inode)->last_trans = fs_info->generation; |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4880 | BTRFS_I(inode)->last_sub_trans = root->log_transid; |
| 4881 | BTRFS_I(inode)->last_log_commit = root->last_log_commit; |
| 4882 | return 0; |
| 4883 | } |
| 4884 | |
| 4885 | /* |
| 4886 | * 1 - for the one we're dropping |
| 4887 | * 1 - for the one we're adding |
| 4888 | * 1 - for updating the inode. |
| 4889 | */ |
| 4890 | trans = btrfs_start_transaction(root, 3); |
| 4891 | if (IS_ERR(trans)) |
| 4892 | return PTR_ERR(trans); |
| 4893 | |
| 4894 | ret = btrfs_drop_extents(trans, root, inode, offset, offset + len, 1); |
| 4895 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 4896 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 4897 | btrfs_end_transaction(trans); |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4898 | return ret; |
| 4899 | } |
| 4900 | |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 4901 | ret = btrfs_insert_file_extent(trans, root, btrfs_ino(BTRFS_I(inode)), |
| 4902 | offset, 0, 0, len, 0, len, 0, 0, 0); |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4903 | if (ret) |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 4904 | btrfs_abort_transaction(trans, ret); |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4905 | else |
| 4906 | btrfs_update_inode(trans, root, inode); |
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 | |
Josef Bacik | 695a0d0 | 2011-03-04 15:46:53 -0500 | [diff] [blame] | 4911 | /* |
| 4912 | * This function puts in dummy file extents for the area we're creating a hole |
| 4913 | * for. So if we are truncating this file to a larger size we need to insert |
| 4914 | * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for |
| 4915 | * the range between oldsize and size |
| 4916 | */ |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 4917 | 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] | 4918 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4919 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4920 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 4921 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4922 | struct extent_map *em = NULL; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4923 | struct extent_state *cached_state = NULL; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4924 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4925 | u64 hole_start = ALIGN(oldsize, fs_info->sectorsize); |
| 4926 | u64 block_end = ALIGN(size, fs_info->sectorsize); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4927 | u64 last_byte; |
| 4928 | u64 cur_offset; |
| 4929 | u64 hole_size; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4930 | int err = 0; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4931 | |
Josef Bacik | a71754f | 2013-06-17 17:14:39 -0400 | [diff] [blame] | 4932 | /* |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4933 | * If our size started in the middle of a block we need to zero out the |
| 4934 | * 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] | 4935 | * expose stale data. |
| 4936 | */ |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4937 | err = btrfs_truncate_block(inode, oldsize, 0, 0); |
Josef Bacik | a71754f | 2013-06-17 17:14:39 -0400 | [diff] [blame] | 4938 | if (err) |
| 4939 | return err; |
| 4940 | |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4941 | if (size <= hole_start) |
| 4942 | return 0; |
| 4943 | |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4944 | while (1) { |
| 4945 | struct btrfs_ordered_extent *ordered; |
Miao Xie | fa7c149 | 2013-09-26 13:15:27 +0800 | [diff] [blame] | 4946 | |
David Sterba | ff13db4 | 2015-12-03 14:30:40 +0100 | [diff] [blame] | 4947 | lock_extent_bits(io_tree, hole_start, block_end - 1, |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 4948 | &cached_state); |
Nikolay Borisov | a776c6f | 2017-02-20 13:50:49 +0200 | [diff] [blame] | 4949 | ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), hole_start, |
Miao Xie | fa7c149 | 2013-09-26 13:15:27 +0800 | [diff] [blame] | 4950 | block_end - hole_start); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4951 | if (!ordered) |
| 4952 | break; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4953 | unlock_extent_cached(io_tree, hole_start, block_end - 1, |
| 4954 | &cached_state, GFP_NOFS); |
Miao Xie | fa7c149 | 2013-09-26 13:15:27 +0800 | [diff] [blame] | 4955 | btrfs_start_ordered_extent(inode, ordered, 1); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4956 | btrfs_put_ordered_extent(ordered); |
| 4957 | } |
| 4958 | |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4959 | cur_offset = hole_start; |
| 4960 | while (1) { |
Nikolay Borisov | fc4f21b | 2017-02-20 13:51:06 +0200 | [diff] [blame] | 4961 | em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, cur_offset, |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4962 | block_end - cur_offset, 0); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4963 | if (IS_ERR(em)) { |
| 4964 | err = PTR_ERR(em); |
Zach Brown | f276795 | 2013-01-08 19:37:58 +0000 | [diff] [blame] | 4965 | em = NULL; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4966 | break; |
| 4967 | } |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4968 | last_byte = min(extent_map_end(em), block_end); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4969 | last_byte = ALIGN(last_byte, fs_info->sectorsize); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4970 | if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) { |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4971 | struct extent_map *hole_em; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4972 | hole_size = last_byte - cur_offset; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4973 | |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4974 | err = maybe_insert_hole(root, inode, cur_offset, |
| 4975 | hole_size); |
| 4976 | if (err) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 4977 | break; |
Nikolay Borisov | dcdbc05 | 2017-02-20 13:50:45 +0200 | [diff] [blame] | 4978 | btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset, |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4979 | cur_offset + hole_size - 1, 0); |
| 4980 | hole_em = alloc_extent_map(); |
| 4981 | if (!hole_em) { |
| 4982 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, |
| 4983 | &BTRFS_I(inode)->runtime_flags); |
| 4984 | goto next; |
| 4985 | } |
| 4986 | hole_em->start = cur_offset; |
| 4987 | hole_em->len = hole_size; |
| 4988 | hole_em->orig_start = cur_offset; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4989 | |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4990 | hole_em->block_start = EXTENT_MAP_HOLE; |
| 4991 | hole_em->block_len = 0; |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 4992 | hole_em->orig_block_len = 0; |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 4993 | hole_em->ram_bytes = hole_size; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4994 | hole_em->bdev = fs_info->fs_devices->latest_bdev; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4995 | hole_em->compress_type = BTRFS_COMPRESS_NONE; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4996 | hole_em->generation = fs_info->generation; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4997 | |
| 4998 | while (1) { |
| 4999 | write_lock(&em_tree->lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 5000 | err = add_extent_mapping(em_tree, hole_em, 1); |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 5001 | write_unlock(&em_tree->lock); |
| 5002 | if (err != -EEXIST) |
| 5003 | break; |
Nikolay Borisov | dcdbc05 | 2017-02-20 13:50:45 +0200 | [diff] [blame] | 5004 | btrfs_drop_extent_cache(BTRFS_I(inode), |
| 5005 | cur_offset, |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 5006 | cur_offset + |
| 5007 | hole_size - 1, 0); |
| 5008 | } |
| 5009 | free_extent_map(hole_em); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 5010 | } |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 5011 | next: |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 5012 | free_extent_map(em); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 5013 | em = NULL; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 5014 | cur_offset = last_byte; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5015 | if (cur_offset >= block_end) |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 5016 | break; |
| 5017 | } |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 5018 | free_extent_map(em); |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 5019 | unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state, |
| 5020 | GFP_NOFS); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 5021 | return err; |
| 5022 | } |
| 5023 | |
Eric Sandeen | 3972f26 | 2013-01-12 02:57:22 +0000 | [diff] [blame] | 5024 | static int btrfs_setsize(struct inode *inode, struct iattr *attr) |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5025 | { |
Miao Xie | f4a2f4c | 2011-12-14 20:12:01 -0500 | [diff] [blame] | 5026 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5027 | struct btrfs_trans_handle *trans; |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 5028 | loff_t oldsize = i_size_read(inode); |
Eric Sandeen | 3972f26 | 2013-01-12 02:57:22 +0000 | [diff] [blame] | 5029 | loff_t newsize = attr->ia_size; |
| 5030 | int mask = attr->ia_valid; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5031 | int ret; |
| 5032 | |
Eric Sandeen | 3972f26 | 2013-01-12 02:57:22 +0000 | [diff] [blame] | 5033 | /* |
| 5034 | * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a |
| 5035 | * special case where we need to update the times despite not having |
| 5036 | * these flags set. For all other operations the VFS set these flags |
| 5037 | * explicitly if it wants a timestamp update. |
| 5038 | */ |
Christoph Hellwig | dff6efc | 2013-11-19 07:17:07 -0800 | [diff] [blame] | 5039 | if (newsize != oldsize) { |
| 5040 | inode_inc_iversion(inode); |
| 5041 | if (!(mask & (ATTR_CTIME | ATTR_MTIME))) |
| 5042 | inode->i_ctime = inode->i_mtime = |
Deepa Dinamani | c2050a4 | 2016-09-14 07:48:06 -0700 | [diff] [blame] | 5043 | current_time(inode); |
Christoph Hellwig | dff6efc | 2013-11-19 07:17:07 -0800 | [diff] [blame] | 5044 | } |
Eric Sandeen | 3972f26 | 2013-01-12 02:57:22 +0000 | [diff] [blame] | 5045 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 5046 | if (newsize > oldsize) { |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 5047 | /* |
| 5048 | * Don't do an expanding truncate while snapshoting is ongoing. |
| 5049 | * This is to ensure the snapshot captures a fully consistent |
| 5050 | * state of this file - if the snapshot captures this expanding |
| 5051 | * truncation, it must capture all writes that happened before |
| 5052 | * this truncation. |
| 5053 | */ |
Zhao Lei | 0bc19f90 | 2016-01-06 18:56:36 +0800 | [diff] [blame] | 5054 | btrfs_wait_for_snapshot_creation(root); |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 5055 | ret = btrfs_cont_expand(inode, oldsize, newsize); |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 5056 | if (ret) { |
| 5057 | btrfs_end_write_no_snapshoting(root); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5058 | return ret; |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 5059 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5060 | |
Miao Xie | f4a2f4c | 2011-12-14 20:12:01 -0500 | [diff] [blame] | 5061 | trans = btrfs_start_transaction(root, 1); |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 5062 | if (IS_ERR(trans)) { |
| 5063 | btrfs_end_write_no_snapshoting(root); |
Miao Xie | f4a2f4c | 2011-12-14 20:12:01 -0500 | [diff] [blame] | 5064 | return PTR_ERR(trans); |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 5065 | } |
Miao Xie | f4a2f4c | 2011-12-14 20:12:01 -0500 | [diff] [blame] | 5066 | |
| 5067 | i_size_write(inode, newsize); |
| 5068 | btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL); |
Chandan Rajendra | 27772b6 | 2016-01-21 15:56:03 +0530 | [diff] [blame] | 5069 | pagecache_isize_extended(inode, oldsize, newsize); |
Miao Xie | f4a2f4c | 2011-12-14 20:12:01 -0500 | [diff] [blame] | 5070 | ret = btrfs_update_inode(trans, root, inode); |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 5071 | btrfs_end_write_no_snapshoting(root); |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 5072 | btrfs_end_transaction(trans); |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 5073 | } else { |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5074 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 5075 | /* |
| 5076 | * We're truncating a file that used to have good data down to |
| 5077 | * zero. Make sure it gets into the ordered flush list so that |
| 5078 | * any new writes get down to disk quickly. |
| 5079 | */ |
| 5080 | if (newsize == 0) |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 5081 | set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE, |
| 5082 | &BTRFS_I(inode)->runtime_flags); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5083 | |
Josef Bacik | f3fe820 | 2013-01-07 17:03:21 -0500 | [diff] [blame] | 5084 | /* |
| 5085 | * 1 for the orphan item we're going to add |
| 5086 | * 1 for the orphan item deletion. |
| 5087 | */ |
| 5088 | trans = btrfs_start_transaction(root, 2); |
| 5089 | if (IS_ERR(trans)) |
| 5090 | return PTR_ERR(trans); |
| 5091 | |
| 5092 | /* |
| 5093 | * We need to do this in case we fail at _any_ point during the |
| 5094 | * actual truncate. Once we do the truncate_setsize we could |
| 5095 | * invalidate pages which forces any outstanding ordered io to |
| 5096 | * be instantly completed which will give us extents that need |
| 5097 | * to be truncated. If we fail to get an orphan inode down we |
| 5098 | * could have left over extents that were never meant to live, |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 5099 | * so we need to guarantee from this point on that everything |
Josef Bacik | f3fe820 | 2013-01-07 17:03:21 -0500 | [diff] [blame] | 5100 | * will be consistent. |
| 5101 | */ |
Nikolay Borisov | 73f2e54 | 2017-02-20 13:50:59 +0200 | [diff] [blame] | 5102 | ret = btrfs_orphan_add(trans, BTRFS_I(inode)); |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 5103 | btrfs_end_transaction(trans); |
Josef Bacik | f3fe820 | 2013-01-07 17:03:21 -0500 | [diff] [blame] | 5104 | if (ret) |
| 5105 | return ret; |
| 5106 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 5107 | /* we don't support swapfiles, so vmtruncate shouldn't fail */ |
| 5108 | truncate_setsize(inode, newsize); |
Miao Xie | 2e60a51 | 2013-02-08 07:01:08 +0000 | [diff] [blame] | 5109 | |
| 5110 | /* Disable nonlocked read DIO to avoid the end less truncate */ |
Nikolay Borisov | abcefb1 | 2017-02-20 13:51:10 +0200 | [diff] [blame] | 5111 | btrfs_inode_block_unlocked_dio(BTRFS_I(inode)); |
Miao Xie | 2e60a51 | 2013-02-08 07:01:08 +0000 | [diff] [blame] | 5112 | inode_dio_wait(inode); |
Nikolay Borisov | 0b58170 | 2017-02-20 13:51:11 +0200 | [diff] [blame] | 5113 | btrfs_inode_resume_unlocked_dio(BTRFS_I(inode)); |
Miao Xie | 2e60a51 | 2013-02-08 07:01:08 +0000 | [diff] [blame] | 5114 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 5115 | ret = btrfs_truncate(inode); |
Josef Bacik | 7f4f6e0 | 2013-08-29 16:43:28 -0400 | [diff] [blame] | 5116 | if (ret && inode->i_nlink) { |
| 5117 | int err; |
| 5118 | |
Liu Bo | 19fd2df | 2016-12-01 13:01:02 -0800 | [diff] [blame] | 5119 | /* To get a stable disk_i_size */ |
| 5120 | err = btrfs_wait_ordered_range(inode, 0, (u64)-1); |
| 5121 | if (err) { |
Nikolay Borisov | 3d6ae7b | 2017-02-20 13:50:58 +0200 | [diff] [blame] | 5122 | btrfs_orphan_del(NULL, BTRFS_I(inode)); |
Liu Bo | 19fd2df | 2016-12-01 13:01:02 -0800 | [diff] [blame] | 5123 | return err; |
| 5124 | } |
| 5125 | |
Josef Bacik | 7f4f6e0 | 2013-08-29 16:43:28 -0400 | [diff] [blame] | 5126 | /* |
| 5127 | * failed to truncate, disk_i_size is only adjusted down |
| 5128 | * as we remove extents, so it should represent the true |
| 5129 | * size of the inode, so reset the in memory size and |
| 5130 | * delete our orphan entry. |
| 5131 | */ |
| 5132 | trans = btrfs_join_transaction(root); |
| 5133 | if (IS_ERR(trans)) { |
Nikolay Borisov | 3d6ae7b | 2017-02-20 13:50:58 +0200 | [diff] [blame] | 5134 | btrfs_orphan_del(NULL, BTRFS_I(inode)); |
Josef Bacik | 7f4f6e0 | 2013-08-29 16:43:28 -0400 | [diff] [blame] | 5135 | return ret; |
| 5136 | } |
| 5137 | i_size_write(inode, BTRFS_I(inode)->disk_i_size); |
Nikolay Borisov | 3d6ae7b | 2017-02-20 13:50:58 +0200 | [diff] [blame] | 5138 | err = btrfs_orphan_del(trans, BTRFS_I(inode)); |
Josef Bacik | 7f4f6e0 | 2013-08-29 16:43:28 -0400 | [diff] [blame] | 5139 | if (err) |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 5140 | btrfs_abort_transaction(trans, err); |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 5141 | btrfs_end_transaction(trans); |
Josef Bacik | 7f4f6e0 | 2013-08-29 16:43:28 -0400 | [diff] [blame] | 5142 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5143 | } |
| 5144 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 5145 | return ret; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5146 | } |
| 5147 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5148 | static int btrfs_setattr(struct dentry *dentry, struct iattr *attr) |
| 5149 | { |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 5150 | struct inode *inode = d_inode(dentry); |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 5151 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5152 | int err; |
| 5153 | |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 5154 | if (btrfs_root_readonly(root)) |
| 5155 | return -EROFS; |
| 5156 | |
Jan Kara | 31051c8 | 2016-05-26 16:55:18 +0200 | [diff] [blame] | 5157 | err = setattr_prepare(dentry, attr); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5158 | if (err) |
| 5159 | return err; |
| 5160 | |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 5161 | if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) { |
Eric Sandeen | 3972f26 | 2013-01-12 02:57:22 +0000 | [diff] [blame] | 5162 | err = btrfs_setsize(inode, attr); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5163 | if (err) |
| 5164 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5165 | } |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 5166 | |
Christoph Hellwig | 1025774 | 2010-06-04 11:30:02 +0200 | [diff] [blame] | 5167 | if (attr->ia_valid) { |
| 5168 | setattr_copy(inode, attr); |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 5169 | inode_inc_iversion(inode); |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 5170 | err = btrfs_dirty_inode(inode); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 5171 | |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 5172 | if (!err && attr->ia_valid & ATTR_MODE) |
Christoph Hellwig | 996a710 | 2013-12-20 05:16:43 -0800 | [diff] [blame] | 5173 | err = posix_acl_chmod(inode, inode->i_mode); |
Christoph Hellwig | 1025774 | 2010-06-04 11:30:02 +0200 | [diff] [blame] | 5174 | } |
| 5175 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5176 | return err; |
| 5177 | } |
Chris Mason | 61295eb | 2008-01-14 16:24:38 -0500 | [diff] [blame] | 5178 | |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5179 | /* |
| 5180 | * While truncating the inode pages during eviction, we get the VFS calling |
| 5181 | * btrfs_invalidatepage() against each page of the inode. This is slow because |
| 5182 | * the calls to btrfs_invalidatepage() result in a huge amount of calls to |
| 5183 | * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting |
| 5184 | * extent_state structures over and over, wasting lots of time. |
| 5185 | * |
| 5186 | * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all |
| 5187 | * those expensive operations on a per page basis and do only the ordered io |
| 5188 | * finishing, while we release here the extent_map and extent_state structures, |
| 5189 | * without the excessive merging and splitting. |
| 5190 | */ |
| 5191 | static void evict_inode_truncate_pages(struct inode *inode) |
| 5192 | { |
| 5193 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
| 5194 | struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree; |
| 5195 | struct rb_node *node; |
| 5196 | |
| 5197 | ASSERT(inode->i_state & I_FREEING); |
Johannes Weiner | 91b0abe | 2014-04-03 14:47:49 -0700 | [diff] [blame] | 5198 | truncate_inode_pages_final(&inode->i_data); |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5199 | |
| 5200 | write_lock(&map_tree->lock); |
| 5201 | while (!RB_EMPTY_ROOT(&map_tree->map)) { |
| 5202 | struct extent_map *em; |
| 5203 | |
| 5204 | node = rb_first(&map_tree->map); |
| 5205 | em = rb_entry(node, struct extent_map, rb_node); |
Wang Shilong | 180589e | 2013-12-14 15:27:31 +0800 | [diff] [blame] | 5206 | clear_bit(EXTENT_FLAG_PINNED, &em->flags); |
| 5207 | clear_bit(EXTENT_FLAG_LOGGING, &em->flags); |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5208 | remove_extent_mapping(map_tree, em); |
| 5209 | free_extent_map(em); |
Filipe Manana | 7064dd5 | 2014-08-08 02:47:05 +0100 | [diff] [blame] | 5210 | if (need_resched()) { |
| 5211 | write_unlock(&map_tree->lock); |
| 5212 | cond_resched(); |
| 5213 | write_lock(&map_tree->lock); |
| 5214 | } |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5215 | } |
| 5216 | write_unlock(&map_tree->lock); |
| 5217 | |
Filipe Manana | 6ca0709 | 2015-05-26 00:55:42 +0100 | [diff] [blame] | 5218 | /* |
| 5219 | * Keep looping until we have no more ranges in the io tree. |
| 5220 | * We can have ongoing bios started by readpages (called from readahead) |
Filipe Manana | 9c6429d | 2015-06-10 12:55:41 +0100 | [diff] [blame] | 5221 | * that have their endio callback (extent_io.c:end_bio_extent_readpage) |
| 5222 | * still in progress (unlocked the pages in the bio but did not yet |
| 5223 | * unlocked the ranges in the io tree). Therefore this means some |
Filipe Manana | 6ca0709 | 2015-05-26 00:55:42 +0100 | [diff] [blame] | 5224 | * ranges can still be locked and eviction started because before |
| 5225 | * submitting those bios, which are executed by a separate task (work |
| 5226 | * queue kthread), inode references (inode->i_count) were not taken |
| 5227 | * (which would be dropped in the end io callback of each bio). |
| 5228 | * Therefore here we effectively end up waiting for those bios and |
| 5229 | * anyone else holding locked ranges without having bumped the inode's |
| 5230 | * reference count - if we don't do it, when they access the inode's |
| 5231 | * io_tree to unlock a range it may be too late, leading to an |
| 5232 | * use-after-free issue. |
| 5233 | */ |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5234 | spin_lock(&io_tree->lock); |
| 5235 | while (!RB_EMPTY_ROOT(&io_tree->state)) { |
| 5236 | struct extent_state *state; |
| 5237 | struct extent_state *cached_state = NULL; |
Filipe Manana | 6ca0709 | 2015-05-26 00:55:42 +0100 | [diff] [blame] | 5238 | u64 start; |
| 5239 | u64 end; |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5240 | |
| 5241 | node = rb_first(&io_tree->state); |
| 5242 | state = rb_entry(node, struct extent_state, rb_node); |
Filipe Manana | 6ca0709 | 2015-05-26 00:55:42 +0100 | [diff] [blame] | 5243 | start = state->start; |
| 5244 | end = state->end; |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5245 | spin_unlock(&io_tree->lock); |
| 5246 | |
David Sterba | ff13db4 | 2015-12-03 14:30:40 +0100 | [diff] [blame] | 5247 | lock_extent_bits(io_tree, start, end, &cached_state); |
Qu Wenruo | b9d0b38 | 2015-09-29 10:35:16 +0800 | [diff] [blame] | 5248 | |
| 5249 | /* |
| 5250 | * If still has DELALLOC flag, the extent didn't reach disk, |
| 5251 | * and its reserved space won't be freed by delayed_ref. |
| 5252 | * So we need to free its reserved space here. |
| 5253 | * (Refer to comment in btrfs_invalidatepage, case 2) |
| 5254 | * |
| 5255 | * Note, end is the bytenr of last byte, so we need + 1 here. |
| 5256 | */ |
| 5257 | if (state->state & EXTENT_DELALLOC) |
| 5258 | btrfs_qgroup_free_data(inode, start, end - start + 1); |
| 5259 | |
Filipe Manana | 6ca0709 | 2015-05-26 00:55:42 +0100 | [diff] [blame] | 5260 | clear_extent_bit(io_tree, start, end, |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5261 | EXTENT_LOCKED | EXTENT_DIRTY | |
| 5262 | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING | |
| 5263 | EXTENT_DEFRAG, 1, 1, |
| 5264 | &cached_state, GFP_NOFS); |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5265 | |
Filipe Manana | 7064dd5 | 2014-08-08 02:47:05 +0100 | [diff] [blame] | 5266 | cond_resched(); |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5267 | spin_lock(&io_tree->lock); |
| 5268 | } |
| 5269 | spin_unlock(&io_tree->lock); |
| 5270 | } |
| 5271 | |
Al Viro | bd55597 | 2010-06-07 11:35:40 -0400 | [diff] [blame] | 5272 | void btrfs_evict_inode(struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5273 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5274 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5275 | struct btrfs_trans_handle *trans; |
| 5276 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Josef Bacik | 726c35f | 2011-09-26 15:46:06 -0400 | [diff] [blame] | 5277 | struct btrfs_block_rsv *rsv, *global_rsv; |
Josef Bacik | 3bce876 | 2015-02-24 12:35:51 -0800 | [diff] [blame] | 5278 | int steal_from_global = 0; |
Nikolay Borisov | 3d48d98 | 2016-06-29 09:46:41 +0300 | [diff] [blame] | 5279 | u64 min_size; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5280 | int ret; |
| 5281 | |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 5282 | trace_btrfs_inode_evict(inode); |
| 5283 | |
Nikolay Borisov | 3d48d98 | 2016-06-29 09:46:41 +0300 | [diff] [blame] | 5284 | if (!root) { |
| 5285 | kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode)); |
| 5286 | return; |
| 5287 | } |
| 5288 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5289 | min_size = btrfs_calc_trunc_metadata_size(fs_info, 1); |
Nikolay Borisov | 3d48d98 | 2016-06-29 09:46:41 +0300 | [diff] [blame] | 5290 | |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5291 | evict_inode_truncate_pages(inode); |
| 5292 | |
Stefan Behrens | 69e9c6c | 2013-09-05 16:58:43 +0200 | [diff] [blame] | 5293 | if (inode->i_nlink && |
| 5294 | ((btrfs_root_refs(&root->root_item) != 0 && |
| 5295 | root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) || |
Nikolay Borisov | 70ddc55 | 2017-02-20 13:50:35 +0200 | [diff] [blame] | 5296 | btrfs_is_free_space_inode(BTRFS_I(inode)))) |
Al Viro | bd55597 | 2010-06-07 11:35:40 -0400 | [diff] [blame] | 5297 | goto no_delete; |
| 5298 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5299 | if (is_bad_inode(inode)) { |
Nikolay Borisov | 3d6ae7b | 2017-02-20 13:50:58 +0200 | [diff] [blame] | 5300 | btrfs_orphan_del(NULL, BTRFS_I(inode)); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5301 | goto no_delete; |
| 5302 | } |
Al Viro | bd55597 | 2010-06-07 11:35:40 -0400 | [diff] [blame] | 5303 | /* 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] | 5304 | if (!special_file(inode->i_mode)) |
| 5305 | btrfs_wait_ordered_range(inode, 0, (u64)-1); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5306 | |
Nikolay Borisov | 7ab7956 | 2017-02-20 13:50:57 +0200 | [diff] [blame] | 5307 | btrfs_free_io_failure_record(BTRFS_I(inode), 0, (u64)-1); |
Miao Xie | f612496 | 2014-09-12 18:44:04 +0800 | [diff] [blame] | 5308 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5309 | if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) { |
Liu Bo | 6bf0231 | 2012-06-25 21:59:09 -0600 | [diff] [blame] | 5310 | BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 5311 | &BTRFS_I(inode)->runtime_flags)); |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 5312 | goto no_delete; |
| 5313 | } |
| 5314 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5315 | if (inode->i_nlink > 0) { |
Stefan Behrens | 69e9c6c | 2013-09-05 16:58:43 +0200 | [diff] [blame] | 5316 | BUG_ON(btrfs_root_refs(&root->root_item) != 0 && |
| 5317 | root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5318 | goto no_delete; |
| 5319 | } |
| 5320 | |
Nikolay Borisov | aa79021 | 2017-01-10 20:35:40 +0200 | [diff] [blame] | 5321 | ret = btrfs_commit_inode_delayed_inode(BTRFS_I(inode)); |
Miao Xie | 0e8c36a | 2012-12-19 06:59:51 +0000 | [diff] [blame] | 5322 | if (ret) { |
Nikolay Borisov | 3d6ae7b | 2017-02-20 13:50:58 +0200 | [diff] [blame] | 5323 | btrfs_orphan_del(NULL, BTRFS_I(inode)); |
Miao Xie | 0e8c36a | 2012-12-19 06:59:51 +0000 | [diff] [blame] | 5324 | goto no_delete; |
| 5325 | } |
| 5326 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5327 | rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP); |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 5328 | if (!rsv) { |
Nikolay Borisov | 3d6ae7b | 2017-02-20 13:50:58 +0200 | [diff] [blame] | 5329 | btrfs_orphan_del(NULL, BTRFS_I(inode)); |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 5330 | goto no_delete; |
| 5331 | } |
Josef Bacik | 4a33854 | 2011-08-29 11:01:31 -0400 | [diff] [blame] | 5332 | rsv->size = min_size; |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 5333 | rsv->failfast = 1; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5334 | global_rsv = &fs_info->global_block_rsv; |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 5335 | |
Nikolay Borisov | 6ef06d2 | 2017-02-20 13:50:34 +0200 | [diff] [blame] | 5336 | btrfs_i_size_write(BTRFS_I(inode), 0); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5337 | |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 5338 | /* |
Miao Xie | 8407aa4 | 2012-09-07 01:43:32 -0600 | [diff] [blame] | 5339 | * This is a bit simpler than btrfs_truncate since we've already |
| 5340 | * reserved our space for our orphan item in the unlink, so we just |
| 5341 | * need to reserve some slack space in case we add bytes and update |
| 5342 | * inode item when doing the truncate. |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 5343 | */ |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5344 | while (1) { |
Miao Xie | 08e007d | 2012-10-16 11:33:38 +0000 | [diff] [blame] | 5345 | ret = btrfs_block_rsv_refill(root, rsv, min_size, |
| 5346 | BTRFS_RESERVE_FLUSH_LIMIT); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5347 | |
Josef Bacik | 726c35f | 2011-09-26 15:46:06 -0400 | [diff] [blame] | 5348 | /* |
| 5349 | * Try and steal from the global reserve since we will |
| 5350 | * likely not use this space anyway, we want to try as |
| 5351 | * hard as possible to get this to work. |
| 5352 | */ |
| 5353 | if (ret) |
Josef Bacik | 3bce876 | 2015-02-24 12:35:51 -0800 | [diff] [blame] | 5354 | steal_from_global++; |
| 5355 | else |
| 5356 | steal_from_global = 0; |
| 5357 | ret = 0; |
Josef Bacik | 726c35f | 2011-09-26 15:46:06 -0400 | [diff] [blame] | 5358 | |
Josef Bacik | 3bce876 | 2015-02-24 12:35:51 -0800 | [diff] [blame] | 5359 | /* |
| 5360 | * steal_from_global == 0: we reserved stuff, hooray! |
| 5361 | * steal_from_global == 1: we didn't reserve stuff, boo! |
| 5362 | * steal_from_global == 2: we've committed, still not a lot of |
| 5363 | * room but maybe we'll have room in the global reserve this |
| 5364 | * time. |
| 5365 | * steal_from_global == 3: abandon all hope! |
| 5366 | */ |
| 5367 | if (steal_from_global > 2) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5368 | btrfs_warn(fs_info, |
| 5369 | "Could not get space for a delete, will truncate on mount %d", |
| 5370 | ret); |
Nikolay Borisov | 3d6ae7b | 2017-02-20 13:50:58 +0200 | [diff] [blame] | 5371 | btrfs_orphan_del(NULL, BTRFS_I(inode)); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5372 | btrfs_free_block_rsv(fs_info, rsv); |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 5373 | goto no_delete; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5374 | } |
| 5375 | |
Miao Xie | 0e8c36a | 2012-12-19 06:59:51 +0000 | [diff] [blame] | 5376 | trans = btrfs_join_transaction(root); |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 5377 | if (IS_ERR(trans)) { |
Nikolay Borisov | 3d6ae7b | 2017-02-20 13:50:58 +0200 | [diff] [blame] | 5378 | btrfs_orphan_del(NULL, BTRFS_I(inode)); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5379 | btrfs_free_block_rsv(fs_info, rsv); |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 5380 | goto no_delete; |
| 5381 | } |
| 5382 | |
Josef Bacik | 3bce876 | 2015-02-24 12:35:51 -0800 | [diff] [blame] | 5383 | /* |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 5384 | * 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] | 5385 | * sure there is room to do it, if not we need to commit and try |
| 5386 | * again. |
| 5387 | */ |
| 5388 | if (steal_from_global) { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5389 | if (!btrfs_check_space_for_delayed_refs(trans, fs_info)) |
Josef Bacik | 3bce876 | 2015-02-24 12:35:51 -0800 | [diff] [blame] | 5390 | ret = btrfs_block_rsv_migrate(global_rsv, rsv, |
Josef Bacik | 25d609f | 2016-03-25 13:25:48 -0400 | [diff] [blame] | 5391 | min_size, 0); |
Josef Bacik | 3bce876 | 2015-02-24 12:35:51 -0800 | [diff] [blame] | 5392 | else |
| 5393 | ret = -ENOSPC; |
| 5394 | } |
| 5395 | |
| 5396 | /* |
| 5397 | * Couldn't steal from the global reserve, we have too much |
| 5398 | * pending stuff built up, commit the transaction and try it |
| 5399 | * again. |
| 5400 | */ |
| 5401 | if (ret) { |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 5402 | ret = btrfs_commit_transaction(trans); |
Josef Bacik | 3bce876 | 2015-02-24 12:35:51 -0800 | [diff] [blame] | 5403 | if (ret) { |
Nikolay Borisov | 3d6ae7b | 2017-02-20 13:50:58 +0200 | [diff] [blame] | 5404 | btrfs_orphan_del(NULL, BTRFS_I(inode)); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5405 | btrfs_free_block_rsv(fs_info, rsv); |
Josef Bacik | 3bce876 | 2015-02-24 12:35:51 -0800 | [diff] [blame] | 5406 | goto no_delete; |
| 5407 | } |
| 5408 | continue; |
| 5409 | } else { |
| 5410 | steal_from_global = 0; |
| 5411 | } |
| 5412 | |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 5413 | trans->block_rsv = rsv; |
| 5414 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5415 | ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0); |
Chris Mason | 28ed134 | 2014-12-17 09:41:04 -0800 | [diff] [blame] | 5416 | if (ret != -ENOSPC && ret != -EAGAIN) |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5417 | break; |
| 5418 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5419 | trans->block_rsv = &fs_info->trans_block_rsv; |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 5420 | btrfs_end_transaction(trans); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5421 | trans = NULL; |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5422 | btrfs_btree_balance_dirty(fs_info); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 5423 | } |
| 5424 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5425 | btrfs_free_block_rsv(fs_info, rsv); |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 5426 | |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 5427 | /* |
| 5428 | * Errors here aren't a big deal, it just means we leave orphan items |
| 5429 | * in the tree. They will be cleaned up on the next mount. |
| 5430 | */ |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5431 | if (ret == 0) { |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 5432 | trans->block_rsv = root->orphan_block_rsv; |
Nikolay Borisov | 3d6ae7b | 2017-02-20 13:50:58 +0200 | [diff] [blame] | 5433 | btrfs_orphan_del(trans, BTRFS_I(inode)); |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 5434 | } else { |
Nikolay Borisov | 3d6ae7b | 2017-02-20 13:50:58 +0200 | [diff] [blame] | 5435 | btrfs_orphan_del(NULL, BTRFS_I(inode)); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5436 | } |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 5437 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5438 | trans->block_rsv = &fs_info->trans_block_rsv; |
| 5439 | if (!(root == fs_info->tree_root || |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 5440 | root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)) |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 5441 | btrfs_return_ino(root, btrfs_ino(BTRFS_I(inode))); |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 5442 | |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 5443 | btrfs_end_transaction(trans); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5444 | btrfs_btree_balance_dirty(fs_info); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5445 | no_delete: |
Nikolay Borisov | f48d1cf | 2017-01-10 20:35:39 +0200 | [diff] [blame] | 5446 | btrfs_remove_delayed_node(BTRFS_I(inode)); |
Jan Kara | dbd5768 | 2012-05-03 14:48:02 +0200 | [diff] [blame] | 5447 | clear_inode(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5448 | } |
| 5449 | |
| 5450 | /* |
| 5451 | * this returns the key found in the dir entry in the location pointer. |
| 5452 | * If no dir entries were found, location->objectid is 0. |
| 5453 | */ |
| 5454 | static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry, |
| 5455 | struct btrfs_key *location) |
| 5456 | { |
| 5457 | const char *name = dentry->d_name.name; |
| 5458 | int namelen = dentry->d_name.len; |
| 5459 | struct btrfs_dir_item *di; |
| 5460 | struct btrfs_path *path; |
| 5461 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Yan | 0d9f7f3 | 2007-10-25 15:48:28 -0400 | [diff] [blame] | 5462 | int ret = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5463 | |
| 5464 | path = btrfs_alloc_path(); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 5465 | if (!path) |
| 5466 | return -ENOMEM; |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 5467 | |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 5468 | di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(BTRFS_I(dir)), |
| 5469 | name, namelen, 0); |
Yan | 0d9f7f3 | 2007-10-25 15:48:28 -0400 | [diff] [blame] | 5470 | if (IS_ERR(di)) |
| 5471 | ret = PTR_ERR(di); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5472 | |
David Sterba | c704005 | 2011-04-19 18:00:01 +0200 | [diff] [blame] | 5473 | if (IS_ERR_OR_NULL(di)) |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 5474 | goto out_err; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5475 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5476 | btrfs_dir_item_key_to_cpu(path->nodes[0], di, location); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5477 | out: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5478 | btrfs_free_path(path); |
| 5479 | return ret; |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 5480 | out_err: |
| 5481 | location->objectid = 0; |
| 5482 | goto out; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5483 | } |
| 5484 | |
| 5485 | /* |
| 5486 | * when we hit a tree root in a directory, the btrfs part of the inode |
| 5487 | * needs to be changed to reflect the root directory of the tree root. This |
| 5488 | * is kind of like crossing a mount point. |
| 5489 | */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5490 | static int fixup_tree_root_location(struct btrfs_fs_info *fs_info, |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5491 | struct inode *dir, |
| 5492 | struct dentry *dentry, |
| 5493 | struct btrfs_key *location, |
| 5494 | struct btrfs_root **sub_root) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5495 | { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5496 | struct btrfs_path *path; |
| 5497 | struct btrfs_root *new_root; |
| 5498 | struct btrfs_root_ref *ref; |
| 5499 | struct extent_buffer *leaf; |
David Sterba | 1d4c08e | 2015-01-02 19:36:14 +0100 | [diff] [blame] | 5500 | struct btrfs_key key; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5501 | int ret; |
| 5502 | int err = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5503 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5504 | path = btrfs_alloc_path(); |
| 5505 | if (!path) { |
| 5506 | err = -ENOMEM; |
| 5507 | goto out; |
| 5508 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5509 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5510 | err = -ENOENT; |
David Sterba | 1d4c08e | 2015-01-02 19:36:14 +0100 | [diff] [blame] | 5511 | key.objectid = BTRFS_I(dir)->root->root_key.objectid; |
| 5512 | key.type = BTRFS_ROOT_REF_KEY; |
| 5513 | key.offset = location->objectid; |
| 5514 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5515 | 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] | 5516 | if (ret) { |
| 5517 | if (ret < 0) |
| 5518 | err = ret; |
| 5519 | goto out; |
| 5520 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5521 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5522 | leaf = path->nodes[0]; |
| 5523 | ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref); |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 5524 | if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(BTRFS_I(dir)) || |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5525 | btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len) |
| 5526 | goto out; |
| 5527 | |
| 5528 | ret = memcmp_extent_buffer(leaf, dentry->d_name.name, |
| 5529 | (unsigned long)(ref + 1), |
| 5530 | dentry->d_name.len); |
| 5531 | if (ret) |
| 5532 | goto out; |
| 5533 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 5534 | btrfs_release_path(path); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5535 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5536 | new_root = btrfs_read_fs_root_no_name(fs_info, location); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5537 | if (IS_ERR(new_root)) { |
| 5538 | err = PTR_ERR(new_root); |
| 5539 | goto out; |
| 5540 | } |
| 5541 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5542 | *sub_root = new_root; |
| 5543 | location->objectid = btrfs_root_dirid(&new_root->root_item); |
| 5544 | location->type = BTRFS_INODE_ITEM_KEY; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5545 | location->offset = 0; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5546 | err = 0; |
| 5547 | out: |
| 5548 | btrfs_free_path(path); |
| 5549 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5550 | } |
| 5551 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5552 | static void inode_tree_add(struct inode *inode) |
| 5553 | { |
| 5554 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5555 | struct btrfs_inode *entry; |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 5556 | struct rb_node **p; |
| 5557 | struct rb_node *parent; |
Filipe David Borba Manana | cef2193 | 2013-09-02 12:19:13 +0100 | [diff] [blame] | 5558 | struct rb_node *new = &BTRFS_I(inode)->rb_node; |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 5559 | u64 ino = btrfs_ino(BTRFS_I(inode)); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5560 | |
Al Viro | 1d3382cb | 2010-10-23 15:19:20 -0400 | [diff] [blame] | 5561 | if (inode_unhashed(inode)) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5562 | return; |
Miao Xie | e1409ce | 2013-05-15 07:48:16 +0000 | [diff] [blame] | 5563 | parent = NULL; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5564 | spin_lock(&root->inode_lock); |
Miao Xie | e1409ce | 2013-05-15 07:48:16 +0000 | [diff] [blame] | 5565 | p = &root->inode_tree.rb_node; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5566 | while (*p) { |
| 5567 | parent = *p; |
| 5568 | entry = rb_entry(parent, struct btrfs_inode, rb_node); |
| 5569 | |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 5570 | if (ino < btrfs_ino(BTRFS_I(&entry->vfs_inode))) |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 5571 | p = &parent->rb_left; |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 5572 | else if (ino > btrfs_ino(BTRFS_I(&entry->vfs_inode))) |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 5573 | p = &parent->rb_right; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5574 | else { |
| 5575 | WARN_ON(!(entry->vfs_inode.i_state & |
Al Viro | a4ffdde | 2010-06-02 17:38:30 -0400 | [diff] [blame] | 5576 | (I_WILL_FREE | I_FREEING))); |
Filipe David Borba Manana | cef2193 | 2013-09-02 12:19:13 +0100 | [diff] [blame] | 5577 | rb_replace_node(parent, new, &root->inode_tree); |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 5578 | RB_CLEAR_NODE(parent); |
| 5579 | spin_unlock(&root->inode_lock); |
Filipe David Borba Manana | cef2193 | 2013-09-02 12:19:13 +0100 | [diff] [blame] | 5580 | return; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5581 | } |
| 5582 | } |
Filipe David Borba Manana | cef2193 | 2013-09-02 12:19:13 +0100 | [diff] [blame] | 5583 | rb_link_node(new, parent, p); |
| 5584 | rb_insert_color(new, &root->inode_tree); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5585 | spin_unlock(&root->inode_lock); |
| 5586 | } |
| 5587 | |
| 5588 | static void inode_tree_del(struct inode *inode) |
| 5589 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5590 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5591 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5592 | int empty = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5593 | |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 5594 | spin_lock(&root->inode_lock); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5595 | if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5596 | rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5597 | RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5598 | empty = RB_EMPTY_ROOT(&root->inode_tree); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5599 | } |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 5600 | spin_unlock(&root->inode_lock); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5601 | |
Stefan Behrens | 69e9c6c | 2013-09-05 16:58:43 +0200 | [diff] [blame] | 5602 | if (empty && btrfs_root_refs(&root->root_item) == 0) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5603 | synchronize_srcu(&fs_info->subvol_srcu); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5604 | spin_lock(&root->inode_lock); |
| 5605 | empty = RB_EMPTY_ROOT(&root->inode_tree); |
| 5606 | spin_unlock(&root->inode_lock); |
| 5607 | if (empty) |
| 5608 | btrfs_add_dead_root(root); |
| 5609 | } |
| 5610 | } |
| 5611 | |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 5612 | void btrfs_invalidate_inodes(struct btrfs_root *root) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5613 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5614 | struct btrfs_fs_info *fs_info = root->fs_info; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5615 | struct rb_node *node; |
| 5616 | struct rb_node *prev; |
| 5617 | struct btrfs_inode *entry; |
| 5618 | struct inode *inode; |
| 5619 | u64 objectid = 0; |
| 5620 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5621 | if (!test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) |
Liu Bo | 7813b3d | 2014-02-10 17:37:25 +0800 | [diff] [blame] | 5622 | WARN_ON(btrfs_root_refs(&root->root_item) != 0); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5623 | |
| 5624 | spin_lock(&root->inode_lock); |
| 5625 | again: |
| 5626 | node = root->inode_tree.rb_node; |
| 5627 | prev = NULL; |
| 5628 | while (node) { |
| 5629 | prev = node; |
| 5630 | entry = rb_entry(node, struct btrfs_inode, rb_node); |
| 5631 | |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 5632 | if (objectid < btrfs_ino(BTRFS_I(&entry->vfs_inode))) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5633 | node = node->rb_left; |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 5634 | else if (objectid > btrfs_ino(BTRFS_I(&entry->vfs_inode))) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5635 | node = node->rb_right; |
| 5636 | else |
| 5637 | break; |
| 5638 | } |
| 5639 | if (!node) { |
| 5640 | while (prev) { |
| 5641 | entry = rb_entry(prev, struct btrfs_inode, rb_node); |
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 = prev; |
| 5644 | break; |
| 5645 | } |
| 5646 | prev = rb_next(prev); |
| 5647 | } |
| 5648 | } |
| 5649 | while (node) { |
| 5650 | entry = rb_entry(node, struct btrfs_inode, rb_node); |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 5651 | objectid = btrfs_ino(BTRFS_I(&entry->vfs_inode)) + 1; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5652 | inode = igrab(&entry->vfs_inode); |
| 5653 | if (inode) { |
| 5654 | spin_unlock(&root->inode_lock); |
| 5655 | if (atomic_read(&inode->i_count) > 1) |
| 5656 | d_prune_aliases(inode); |
| 5657 | /* |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 5658 | * btrfs_drop_inode will have it removed from |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5659 | * the inode cache when its usage count |
| 5660 | * hits zero. |
| 5661 | */ |
| 5662 | iput(inode); |
| 5663 | cond_resched(); |
| 5664 | spin_lock(&root->inode_lock); |
| 5665 | goto again; |
| 5666 | } |
| 5667 | |
| 5668 | if (cond_resched_lock(&root->inode_lock)) |
| 5669 | goto again; |
| 5670 | |
| 5671 | node = rb_next(node); |
| 5672 | } |
| 5673 | spin_unlock(&root->inode_lock); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5674 | } |
| 5675 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5676 | static int btrfs_init_locked_inode(struct inode *inode, void *p) |
| 5677 | { |
| 5678 | struct btrfs_iget_args *args = p; |
Chris Mason | 90d3e59 | 2014-01-09 17:28:00 -0800 | [diff] [blame] | 5679 | inode->i_ino = args->location->objectid; |
| 5680 | memcpy(&BTRFS_I(inode)->location, args->location, |
| 5681 | sizeof(*args->location)); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5682 | BTRFS_I(inode)->root = args->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5683 | return 0; |
| 5684 | } |
| 5685 | |
| 5686 | static int btrfs_find_actor(struct inode *inode, void *opaque) |
| 5687 | { |
| 5688 | struct btrfs_iget_args *args = opaque; |
Chris Mason | 90d3e59 | 2014-01-09 17:28:00 -0800 | [diff] [blame] | 5689 | return args->location->objectid == BTRFS_I(inode)->location.objectid && |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5690 | args->root == BTRFS_I(inode)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5691 | } |
| 5692 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5693 | static struct inode *btrfs_iget_locked(struct super_block *s, |
Chris Mason | 90d3e59 | 2014-01-09 17:28:00 -0800 | [diff] [blame] | 5694 | struct btrfs_key *location, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5695 | struct btrfs_root *root) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5696 | { |
| 5697 | struct inode *inode; |
| 5698 | struct btrfs_iget_args args; |
Chris Mason | 90d3e59 | 2014-01-09 17:28:00 -0800 | [diff] [blame] | 5699 | unsigned long hashval = btrfs_inode_hash(location->objectid, root); |
Filipe David Borba Manana | 778ba82 | 2013-10-06 22:22:33 +0100 | [diff] [blame] | 5700 | |
Chris Mason | 90d3e59 | 2014-01-09 17:28:00 -0800 | [diff] [blame] | 5701 | args.location = location; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5702 | args.root = root; |
| 5703 | |
Filipe David Borba Manana | 778ba82 | 2013-10-06 22:22:33 +0100 | [diff] [blame] | 5704 | inode = iget5_locked(s, hashval, btrfs_find_actor, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5705 | btrfs_init_locked_inode, |
| 5706 | (void *)&args); |
| 5707 | return inode; |
| 5708 | } |
| 5709 | |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 5710 | /* Get an inode object given its location and corresponding root. |
| 5711 | * Returns in *is_new if the inode was read from disk |
| 5712 | */ |
| 5713 | struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location, |
Josef Bacik | 73f7341 | 2009-12-04 17:38:27 +0000 | [diff] [blame] | 5714 | struct btrfs_root *root, int *new) |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 5715 | { |
| 5716 | struct inode *inode; |
| 5717 | |
Chris Mason | 90d3e59 | 2014-01-09 17:28:00 -0800 | [diff] [blame] | 5718 | inode = btrfs_iget_locked(s, location, root); |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 5719 | if (!inode) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5720 | return ERR_PTR(-ENOMEM); |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 5721 | |
| 5722 | if (inode->i_state & I_NEW) { |
Filipe Manana | 6771089 | 2016-06-06 11:51:25 +0100 | [diff] [blame] | 5723 | int ret; |
| 5724 | |
| 5725 | ret = btrfs_read_locked_inode(inode); |
Mark Fasheh | 1748f84 | 2011-07-12 11:25:31 -0700 | [diff] [blame] | 5726 | if (!is_bad_inode(inode)) { |
| 5727 | inode_tree_add(inode); |
| 5728 | unlock_new_inode(inode); |
| 5729 | if (new) |
| 5730 | *new = 1; |
| 5731 | } else { |
Sergei Trofimovich | e0b6d65 | 2011-09-11 10:52:24 -0400 | [diff] [blame] | 5732 | unlock_new_inode(inode); |
| 5733 | iput(inode); |
Filipe Manana | 6771089 | 2016-06-06 11:51:25 +0100 | [diff] [blame] | 5734 | ASSERT(ret < 0); |
| 5735 | inode = ERR_PTR(ret < 0 ? ret : -ESTALE); |
Mark Fasheh | 1748f84 | 2011-07-12 11:25:31 -0700 | [diff] [blame] | 5736 | } |
| 5737 | } |
| 5738 | |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 5739 | return inode; |
| 5740 | } |
| 5741 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5742 | static struct inode *new_simple_dir(struct super_block *s, |
| 5743 | struct btrfs_key *key, |
| 5744 | struct btrfs_root *root) |
| 5745 | { |
| 5746 | struct inode *inode = new_inode(s); |
| 5747 | |
| 5748 | if (!inode) |
| 5749 | return ERR_PTR(-ENOMEM); |
| 5750 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5751 | BTRFS_I(inode)->root = root; |
| 5752 | memcpy(&BTRFS_I(inode)->location, key, sizeof(*key)); |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 5753 | set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5754 | |
| 5755 | inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID; |
Li Zefan | 848cce0 | 2012-02-21 17:04:28 +0800 | [diff] [blame] | 5756 | inode->i_op = &btrfs_dir_ro_inode_operations; |
Omar Sandoval | 1fdf419 | 2017-01-25 17:06:39 -0800 | [diff] [blame] | 5757 | inode->i_opflags &= ~IOP_XATTR; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5758 | inode->i_fop = &simple_dir_operations; |
| 5759 | inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO; |
Deepa Dinamani | c2050a4 | 2016-09-14 07:48:06 -0700 | [diff] [blame] | 5760 | inode->i_mtime = current_time(inode); |
chandan r | 9cc97d6 | 2012-07-04 12:48:07 +0530 | [diff] [blame] | 5761 | inode->i_atime = inode->i_mtime; |
| 5762 | inode->i_ctime = inode->i_mtime; |
| 5763 | BTRFS_I(inode)->i_otime = inode->i_mtime; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5764 | |
| 5765 | return inode; |
| 5766 | } |
| 5767 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5768 | struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5769 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5770 | struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5771 | struct inode *inode; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5772 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5773 | struct btrfs_root *sub_root = root; |
| 5774 | struct btrfs_key location; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5775 | int index; |
Josef Bacik | b4aff1f | 2011-06-28 16:18:59 -0400 | [diff] [blame] | 5776 | int ret = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5777 | |
| 5778 | if (dentry->d_name.len > BTRFS_NAME_LEN) |
| 5779 | return ERR_PTR(-ENAMETOOLONG); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5780 | |
Jeff Layton | 39e3c95 | 2012-11-28 11:30:53 -0500 | [diff] [blame] | 5781 | ret = btrfs_inode_by_name(dir, dentry, &location); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5782 | if (ret < 0) |
| 5783 | return ERR_PTR(ret); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5784 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5785 | if (location.objectid == 0) |
Tsutomu Itoh | 5662344b3 | 2013-12-13 09:51:42 +0900 | [diff] [blame] | 5786 | return ERR_PTR(-ENOENT); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5787 | |
| 5788 | if (location.type == BTRFS_INODE_ITEM_KEY) { |
Josef Bacik | 73f7341 | 2009-12-04 17:38:27 +0000 | [diff] [blame] | 5789 | inode = btrfs_iget(dir->i_sb, &location, root, NULL); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5790 | return inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5791 | } |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5792 | |
| 5793 | BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY); |
| 5794 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5795 | index = srcu_read_lock(&fs_info->subvol_srcu); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5796 | ret = fixup_tree_root_location(fs_info, dir, dentry, |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5797 | &location, &sub_root); |
| 5798 | if (ret < 0) { |
| 5799 | if (ret != -ENOENT) |
| 5800 | inode = ERR_PTR(ret); |
| 5801 | else |
| 5802 | inode = new_simple_dir(dir->i_sb, &location, sub_root); |
| 5803 | } else { |
Josef Bacik | 73f7341 | 2009-12-04 17:38:27 +0000 | [diff] [blame] | 5804 | inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5805 | } |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5806 | srcu_read_unlock(&fs_info->subvol_srcu, index); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5807 | |
Julia Lawall | 34d19ba | 2011-01-24 19:55:19 +0000 | [diff] [blame] | 5808 | if (!IS_ERR(inode) && root != sub_root) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5809 | down_read(&fs_info->cleanup_work_sem); |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 5810 | if (!(inode->i_sb->s_flags & MS_RDONLY)) |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 5811 | ret = btrfs_orphan_cleanup(sub_root); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5812 | up_read(&fs_info->cleanup_work_sem); |
Josef Bacik | 01cd336 | 2013-06-03 21:39:49 -0400 | [diff] [blame] | 5813 | if (ret) { |
| 5814 | iput(inode); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 5815 | inode = ERR_PTR(ret); |
Josef Bacik | 01cd336 | 2013-06-03 21:39:49 -0400 | [diff] [blame] | 5816 | } |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 5817 | } |
| 5818 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5819 | return inode; |
| 5820 | } |
| 5821 | |
Nick Piggin | fe15ce4 | 2011-01-07 17:49:23 +1100 | [diff] [blame] | 5822 | static int btrfs_dentry_delete(const struct dentry *dentry) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5823 | { |
| 5824 | struct btrfs_root *root; |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 5825 | struct inode *inode = d_inode(dentry); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5826 | |
Li Zefan | 848cce0 | 2012-02-21 17:04:28 +0800 | [diff] [blame] | 5827 | if (!inode && !IS_ROOT(dentry)) |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 5828 | inode = d_inode(dentry->d_parent); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5829 | |
Li Zefan | 848cce0 | 2012-02-21 17:04:28 +0800 | [diff] [blame] | 5830 | if (inode) { |
| 5831 | root = BTRFS_I(inode)->root; |
Yan, Zheng | efefb14 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5832 | if (btrfs_root_refs(&root->root_item) == 0) |
| 5833 | return 1; |
Li Zefan | 848cce0 | 2012-02-21 17:04:28 +0800 | [diff] [blame] | 5834 | |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 5835 | if (btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) |
Li Zefan | 848cce0 | 2012-02-21 17:04:28 +0800 | [diff] [blame] | 5836 | return 1; |
Yan, Zheng | efefb14 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5837 | } |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5838 | return 0; |
| 5839 | } |
| 5840 | |
Josef Bacik | b4aff1f | 2011-06-28 16:18:59 -0400 | [diff] [blame] | 5841 | static void btrfs_dentry_release(struct dentry *dentry) |
| 5842 | { |
Daeseok Youn | 944a451 | 2014-04-14 15:37:02 +0900 | [diff] [blame] | 5843 | kfree(dentry->d_fsdata); |
Josef Bacik | b4aff1f | 2011-06-28 16:18:59 -0400 | [diff] [blame] | 5844 | } |
| 5845 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5846 | static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry, |
Al Viro | 00cd8dd | 2012-06-10 17:13:09 -0400 | [diff] [blame] | 5847 | unsigned int flags) |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5848 | { |
Tsutomu Itoh | 5662344b3 | 2013-12-13 09:51:42 +0900 | [diff] [blame] | 5849 | struct inode *inode; |
Josef Bacik | a66e7cc | 2011-09-18 10:34:03 -0400 | [diff] [blame] | 5850 | |
Tsutomu Itoh | 5662344b3 | 2013-12-13 09:51:42 +0900 | [diff] [blame] | 5851 | inode = btrfs_lookup_dentry(dir, dentry); |
| 5852 | if (IS_ERR(inode)) { |
| 5853 | if (PTR_ERR(inode) == -ENOENT) |
| 5854 | inode = NULL; |
| 5855 | else |
| 5856 | return ERR_CAST(inode); |
| 5857 | } |
| 5858 | |
Al Viro | 41d28bc | 2014-10-12 22:24:21 -0400 | [diff] [blame] | 5859 | return d_splice_alias(inode, dentry); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5860 | } |
| 5861 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5862 | unsigned char btrfs_filetype_table[] = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5863 | DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK |
| 5864 | }; |
| 5865 | |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5866 | static int btrfs_real_readdir(struct file *file, struct dir_context *ctx) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5867 | { |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5868 | struct inode *inode = file_inode(file); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5869 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5870 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5871 | struct btrfs_item *item; |
| 5872 | struct btrfs_dir_item *di; |
| 5873 | struct btrfs_key key; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5874 | struct btrfs_key found_key; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5875 | struct btrfs_path *path; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5876 | struct list_head ins_list; |
| 5877 | struct list_head del_list; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5878 | int ret; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5879 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5880 | int slot; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5881 | unsigned char d_type; |
| 5882 | int over = 0; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5883 | char tmp_name[32]; |
| 5884 | char *name_ptr; |
| 5885 | int name_len; |
Omar Sandoval | 02dbfc9 | 2016-05-20 13:50:33 -0700 | [diff] [blame] | 5886 | bool put = false; |
Jeff Mahoney | c2951f3 | 2016-11-21 15:59:04 +0100 | [diff] [blame] | 5887 | struct btrfs_key location; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5888 | |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5889 | if (!dir_emit_dots(file, ctx)) |
| 5890 | return 0; |
| 5891 | |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5892 | path = btrfs_alloc_path(); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5893 | if (!path) |
| 5894 | return -ENOMEM; |
Chris Mason | ff5714c | 2011-05-28 07:00:39 -0400 | [diff] [blame] | 5895 | |
David Sterba | e4058b5 | 2015-11-27 16:31:35 +0100 | [diff] [blame] | 5896 | path->reada = READA_FORWARD; |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5897 | |
Jeff Mahoney | c2951f3 | 2016-11-21 15:59:04 +0100 | [diff] [blame] | 5898 | INIT_LIST_HEAD(&ins_list); |
| 5899 | INIT_LIST_HEAD(&del_list); |
| 5900 | put = btrfs_readdir_get_delayed_items(inode, &ins_list, &del_list); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5901 | |
Jeff Mahoney | c2951f3 | 2016-11-21 15:59:04 +0100 | [diff] [blame] | 5902 | key.type = BTRFS_DIR_INDEX_KEY; |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5903 | key.offset = ctx->pos; |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 5904 | key.objectid = btrfs_ino(BTRFS_I(inode)); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5905 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5906 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 5907 | if (ret < 0) |
| 5908 | goto err; |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5909 | |
| 5910 | while (1) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5911 | leaf = path->nodes[0]; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5912 | slot = path->slots[0]; |
Li Zefan | b9e03af | 2011-03-23 10:43:58 +0800 | [diff] [blame] | 5913 | if (slot >= btrfs_header_nritems(leaf)) { |
| 5914 | ret = btrfs_next_leaf(root, path); |
| 5915 | if (ret < 0) |
| 5916 | goto err; |
| 5917 | else if (ret > 0) |
| 5918 | break; |
| 5919 | continue; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5920 | } |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5921 | |
Ross Kirk | dd3cc16 | 2013-09-16 15:58:09 +0100 | [diff] [blame] | 5922 | item = btrfs_item_nr(slot); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5923 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
| 5924 | |
| 5925 | if (found_key.objectid != key.objectid) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5926 | break; |
Jeff Mahoney | c2951f3 | 2016-11-21 15:59:04 +0100 | [diff] [blame] | 5927 | if (found_key.type != BTRFS_DIR_INDEX_KEY) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5928 | break; |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5929 | if (found_key.offset < ctx->pos) |
Li Zefan | b9e03af | 2011-03-23 10:43:58 +0800 | [diff] [blame] | 5930 | goto next; |
Jeff Mahoney | c2951f3 | 2016-11-21 15:59:04 +0100 | [diff] [blame] | 5931 | if (btrfs_should_delete_dir_index(&del_list, found_key.offset)) |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5932 | goto next; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5933 | |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5934 | ctx->pos = found_key.offset; |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5935 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5936 | di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5937 | if (verify_dir_item(fs_info, leaf, di)) |
Jeff Mahoney | c2951f3 | 2016-11-21 15:59:04 +0100 | [diff] [blame] | 5938 | goto next; |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5939 | |
Jeff Mahoney | c2951f3 | 2016-11-21 15:59:04 +0100 | [diff] [blame] | 5940 | name_len = btrfs_dir_name_len(leaf, di); |
| 5941 | if (name_len <= sizeof(tmp_name)) { |
| 5942 | name_ptr = tmp_name; |
| 5943 | } else { |
| 5944 | name_ptr = kmalloc(name_len, GFP_KERNEL); |
| 5945 | if (!name_ptr) { |
| 5946 | ret = -ENOMEM; |
| 5947 | goto err; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5948 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5949 | } |
Jeff Mahoney | c2951f3 | 2016-11-21 15:59:04 +0100 | [diff] [blame] | 5950 | read_extent_buffer(leaf, name_ptr, (unsigned long)(di + 1), |
| 5951 | name_len); |
| 5952 | |
| 5953 | d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)]; |
| 5954 | btrfs_dir_item_key_to_cpu(leaf, di, &location); |
| 5955 | |
| 5956 | over = !dir_emit(ctx, name_ptr, name_len, location.objectid, |
| 5957 | d_type); |
| 5958 | |
| 5959 | if (name_ptr != tmp_name) |
| 5960 | kfree(name_ptr); |
| 5961 | |
Jeff Mahoney | c2951f3 | 2016-11-21 15:59:04 +0100 | [diff] [blame] | 5962 | if (over) |
| 5963 | goto nopos; |
Jeff Mahoney | d2fbb2b | 2016-11-05 13:26:35 -0400 | [diff] [blame] | 5964 | ctx->pos++; |
Li Zefan | b9e03af | 2011-03-23 10:43:58 +0800 | [diff] [blame] | 5965 | next: |
| 5966 | path->slots[0]++; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5967 | } |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5968 | |
Jeff Mahoney | d2fbb2b | 2016-11-05 13:26:35 -0400 | [diff] [blame] | 5969 | ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list); |
Jeff Mahoney | c2951f3 | 2016-11-21 15:59:04 +0100 | [diff] [blame] | 5970 | if (ret) |
David Sterba | bc4ef75 | 2015-11-13 13:44:28 +0100 | [diff] [blame] | 5971 | goto nopos; |
| 5972 | |
Zach Brown | db62efb | 2013-07-11 16:19:42 -0700 | [diff] [blame] | 5973 | /* |
| 5974 | * Stop new entries from being returned after we return the last |
| 5975 | * entry. |
| 5976 | * |
| 5977 | * New directory entries are assigned a strictly increasing |
| 5978 | * offset. This means that new entries created during readdir |
| 5979 | * are *guaranteed* to be seen in the future by that readdir. |
| 5980 | * This has broken buggy programs which operate on names as |
| 5981 | * they're returned by readdir. Until we re-use freed offsets |
| 5982 | * we have this hack to stop new entries from being returned |
| 5983 | * under the assumption that they'll never reach this huge |
| 5984 | * offset. |
| 5985 | * |
| 5986 | * This is being careful not to overflow 32bit loff_t unless the |
| 5987 | * last entry requires it because doing so has broken 32bit apps |
| 5988 | * in the past. |
| 5989 | */ |
Jeff Mahoney | c2951f3 | 2016-11-21 15:59:04 +0100 | [diff] [blame] | 5990 | if (ctx->pos >= INT_MAX) |
| 5991 | ctx->pos = LLONG_MAX; |
| 5992 | else |
| 5993 | ctx->pos = INT_MAX; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5994 | nopos: |
| 5995 | ret = 0; |
| 5996 | err: |
Omar Sandoval | 02dbfc9 | 2016-05-20 13:50:33 -0700 | [diff] [blame] | 5997 | if (put) |
| 5998 | btrfs_readdir_put_delayed_items(inode, &ins_list, &del_list); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5999 | btrfs_free_path(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6000 | return ret; |
| 6001 | } |
| 6002 | |
Christoph Hellwig | a9185b4 | 2010-03-05 09:21:37 +0100 | [diff] [blame] | 6003 | int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6004 | { |
| 6005 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 6006 | struct btrfs_trans_handle *trans; |
| 6007 | int ret = 0; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 6008 | bool nolock = false; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6009 | |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 6010 | if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags)) |
Chris Mason | 4ca8b41 | 2008-08-05 13:30:48 -0400 | [diff] [blame] | 6011 | return 0; |
| 6012 | |
Nikolay Borisov | 70ddc55 | 2017-02-20 13:50:35 +0200 | [diff] [blame] | 6013 | if (btrfs_fs_closing(root->fs_info) && |
| 6014 | btrfs_is_free_space_inode(BTRFS_I(inode))) |
Li Zefan | 82d5902 | 2011-04-20 10:33:24 +0800 | [diff] [blame] | 6015 | nolock = true; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 6016 | |
Christoph Hellwig | a9185b4 | 2010-03-05 09:21:37 +0100 | [diff] [blame] | 6017 | if (wbc->sync_mode == WB_SYNC_ALL) { |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 6018 | if (nolock) |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 6019 | trans = btrfs_join_transaction_nolock(root); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 6020 | else |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 6021 | trans = btrfs_join_transaction(root); |
Tsutomu Itoh | 3612b49 | 2011-01-25 02:51:38 +0000 | [diff] [blame] | 6022 | if (IS_ERR(trans)) |
| 6023 | return PTR_ERR(trans); |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 6024 | ret = btrfs_commit_transaction(trans); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6025 | } |
| 6026 | return ret; |
| 6027 | } |
| 6028 | |
| 6029 | /* |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 6030 | * This is somewhat expensive, updating the tree every time the |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6031 | * inode changes. But, it is most likely to find the inode in cache. |
| 6032 | * FIXME, needs more benchmarking...there are no reasons other than performance |
| 6033 | * to keep or drop this code. |
| 6034 | */ |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 6035 | static int btrfs_dirty_inode(struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6036 | { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6037 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6038 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 6039 | struct btrfs_trans_handle *trans; |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 6040 | int ret; |
| 6041 | |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 6042 | if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags)) |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 6043 | return 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6044 | |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 6045 | trans = btrfs_join_transaction(root); |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 6046 | if (IS_ERR(trans)) |
| 6047 | return PTR_ERR(trans); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 6048 | |
| 6049 | ret = btrfs_update_inode(trans, root, inode); |
Chris Mason | 94b6044 | 2010-05-26 11:02:00 -0400 | [diff] [blame] | 6050 | if (ret && ret == -ENOSPC) { |
| 6051 | /* whoops, lets try again with the full transaction */ |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 6052 | btrfs_end_transaction(trans); |
Chris Mason | 94b6044 | 2010-05-26 11:02:00 -0400 | [diff] [blame] | 6053 | trans = btrfs_start_transaction(root, 1); |
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 | |
Chris Mason | 94b6044 | 2010-05-26 11:02:00 -0400 | [diff] [blame] | 6057 | ret = btrfs_update_inode(trans, root, inode); |
Chris Mason | 94b6044 | 2010-05-26 11:02:00 -0400 | [diff] [blame] | 6058 | } |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 6059 | btrfs_end_transaction(trans); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 6060 | if (BTRFS_I(inode)->delayed_node) |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6061 | btrfs_balance_delayed_items(fs_info); |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 6062 | |
| 6063 | return ret; |
| 6064 | } |
| 6065 | |
| 6066 | /* |
| 6067 | * This is a copy of file_update_time. We need this so we can return error on |
| 6068 | * ENOSPC for updating the inode in the case of file write and mmap writes. |
| 6069 | */ |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 6070 | static int btrfs_update_time(struct inode *inode, struct timespec *now, |
| 6071 | int flags) |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 6072 | { |
Alexander Block | 2bc556528 | 2012-06-15 09:49:33 +0200 | [diff] [blame] | 6073 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 6074 | |
| 6075 | if (btrfs_root_readonly(root)) |
| 6076 | return -EROFS; |
| 6077 | |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 6078 | if (flags & S_VERSION) |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 6079 | inode_inc_iversion(inode); |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 6080 | if (flags & S_CTIME) |
| 6081 | inode->i_ctime = *now; |
| 6082 | if (flags & S_MTIME) |
| 6083 | inode->i_mtime = *now; |
| 6084 | if (flags & S_ATIME) |
| 6085 | inode->i_atime = *now; |
| 6086 | return btrfs_dirty_inode(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6087 | } |
| 6088 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 6089 | /* |
| 6090 | * find the highest existing sequence number in a directory |
| 6091 | * and then set the in-memory index_cnt variable to reflect |
| 6092 | * free sequence numbers |
| 6093 | */ |
Nikolay Borisov | 4c570655 | 2017-02-20 13:50:32 +0200 | [diff] [blame] | 6094 | static int btrfs_set_inode_index_count(struct btrfs_inode *inode) |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6095 | { |
Nikolay Borisov | 4c570655 | 2017-02-20 13:50:32 +0200 | [diff] [blame] | 6096 | struct btrfs_root *root = inode->root; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6097 | struct btrfs_key key, found_key; |
| 6098 | struct btrfs_path *path; |
| 6099 | struct extent_buffer *leaf; |
| 6100 | int ret; |
| 6101 | |
Nikolay Borisov | 4c570655 | 2017-02-20 13:50:32 +0200 | [diff] [blame] | 6102 | key.objectid = btrfs_ino(inode); |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 6103 | key.type = BTRFS_DIR_INDEX_KEY; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6104 | key.offset = (u64)-1; |
| 6105 | |
| 6106 | path = btrfs_alloc_path(); |
| 6107 | if (!path) |
| 6108 | return -ENOMEM; |
| 6109 | |
| 6110 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 6111 | if (ret < 0) |
| 6112 | goto out; |
| 6113 | /* FIXME: we should be able to handle this */ |
| 6114 | if (ret == 0) |
| 6115 | goto out; |
| 6116 | ret = 0; |
| 6117 | |
| 6118 | /* |
| 6119 | * MAGIC NUMBER EXPLANATION: |
| 6120 | * since we search a directory based on f_pos we have to start at 2 |
| 6121 | * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody |
| 6122 | * else has to start at 2 |
| 6123 | */ |
| 6124 | if (path->slots[0] == 0) { |
Nikolay Borisov | 4c570655 | 2017-02-20 13:50:32 +0200 | [diff] [blame] | 6125 | inode->index_cnt = 2; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6126 | goto out; |
| 6127 | } |
| 6128 | |
| 6129 | path->slots[0]--; |
| 6130 | |
| 6131 | leaf = path->nodes[0]; |
| 6132 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 6133 | |
Nikolay Borisov | 4c570655 | 2017-02-20 13:50:32 +0200 | [diff] [blame] | 6134 | if (found_key.objectid != btrfs_ino(inode) || |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 6135 | found_key.type != BTRFS_DIR_INDEX_KEY) { |
Nikolay Borisov | 4c570655 | 2017-02-20 13:50:32 +0200 | [diff] [blame] | 6136 | inode->index_cnt = 2; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6137 | goto out; |
| 6138 | } |
| 6139 | |
Nikolay Borisov | 4c570655 | 2017-02-20 13:50:32 +0200 | [diff] [blame] | 6140 | inode->index_cnt = found_key.offset + 1; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6141 | out: |
| 6142 | btrfs_free_path(path); |
| 6143 | return ret; |
| 6144 | } |
| 6145 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 6146 | /* |
| 6147 | * helper to find a free sequence number in a given directory. This current |
| 6148 | * code is very simple, later versions will do smarter things in the btree |
| 6149 | */ |
Nikolay Borisov | 877574e | 2017-02-20 13:50:33 +0200 | [diff] [blame] | 6150 | int btrfs_set_inode_index(struct btrfs_inode *dir, u64 *index) |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6151 | { |
| 6152 | int ret = 0; |
| 6153 | |
Nikolay Borisov | 877574e | 2017-02-20 13:50:33 +0200 | [diff] [blame] | 6154 | if (dir->index_cnt == (u64)-1) { |
| 6155 | ret = btrfs_inode_delayed_dir_index_count(dir); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 6156 | if (ret) { |
| 6157 | ret = btrfs_set_inode_index_count(dir); |
| 6158 | if (ret) |
| 6159 | return ret; |
| 6160 | } |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6161 | } |
| 6162 | |
Nikolay Borisov | 877574e | 2017-02-20 13:50:33 +0200 | [diff] [blame] | 6163 | *index = dir->index_cnt; |
| 6164 | dir->index_cnt++; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6165 | |
| 6166 | return ret; |
| 6167 | } |
| 6168 | |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6169 | static int btrfs_insert_inode_locked(struct inode *inode) |
| 6170 | { |
| 6171 | struct btrfs_iget_args args; |
| 6172 | args.location = &BTRFS_I(inode)->location; |
| 6173 | args.root = BTRFS_I(inode)->root; |
| 6174 | |
| 6175 | return insert_inode_locked4(inode, |
| 6176 | btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root), |
| 6177 | btrfs_find_actor, &args); |
| 6178 | } |
| 6179 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6180 | static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans, |
| 6181 | struct btrfs_root *root, |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6182 | struct inode *dir, |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 6183 | const char *name, int name_len, |
Al Viro | 175a4eb | 2011-07-26 03:30:54 -0400 | [diff] [blame] | 6184 | u64 ref_objectid, u64 objectid, |
| 6185 | umode_t mode, u64 *index) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6186 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6187 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6188 | struct inode *inode; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6189 | struct btrfs_inode_item *inode_item; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6190 | struct btrfs_key *location; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6191 | struct btrfs_path *path; |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 6192 | struct btrfs_inode_ref *ref; |
| 6193 | struct btrfs_key key[2]; |
| 6194 | u32 sizes[2]; |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 6195 | int nitems = name ? 2 : 1; |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 6196 | unsigned long ptr; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6197 | int ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6198 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6199 | path = btrfs_alloc_path(); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 6200 | if (!path) |
| 6201 | return ERR_PTR(-ENOMEM); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6202 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6203 | inode = new_inode(fs_info->sb); |
Yoshinori Sano | 8fb2764 | 2011-04-09 02:30:07 +0000 | [diff] [blame] | 6204 | if (!inode) { |
| 6205 | btrfs_free_path(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6206 | return ERR_PTR(-ENOMEM); |
Yoshinori Sano | 8fb2764 | 2011-04-09 02:30:07 +0000 | [diff] [blame] | 6207 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6208 | |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 6209 | /* |
Filipe Manana | 5762b5c | 2014-08-01 00:10:32 +0100 | [diff] [blame] | 6210 | * O_TMPFILE, set link count to 0, so that after this point, |
| 6211 | * we fill in an inode item with the correct link count. |
| 6212 | */ |
| 6213 | if (!name) |
| 6214 | set_nlink(inode, 0); |
| 6215 | |
| 6216 | /* |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 6217 | * we have to initialize this early, so we can reclaim the inode |
| 6218 | * number if we fail afterwards in this function. |
| 6219 | */ |
| 6220 | inode->i_ino = objectid; |
| 6221 | |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 6222 | if (dir && name) { |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 6223 | trace_btrfs_inode_request(dir); |
| 6224 | |
Nikolay Borisov | 877574e | 2017-02-20 13:50:33 +0200 | [diff] [blame] | 6225 | ret = btrfs_set_inode_index(BTRFS_I(dir), index); |
Shen Feng | 0977143 | 2009-04-02 16:46:06 -0400 | [diff] [blame] | 6226 | if (ret) { |
Yoshinori Sano | 8fb2764 | 2011-04-09 02:30:07 +0000 | [diff] [blame] | 6227 | btrfs_free_path(path); |
Shen Feng | 0977143 | 2009-04-02 16:46:06 -0400 | [diff] [blame] | 6228 | iput(inode); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6229 | return ERR_PTR(ret); |
Shen Feng | 0977143 | 2009-04-02 16:46:06 -0400 | [diff] [blame] | 6230 | } |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 6231 | } else if (dir) { |
| 6232 | *index = 0; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6233 | } |
| 6234 | /* |
| 6235 | * index_cnt is ignored for everything but a dir, |
| 6236 | * btrfs_get_inode_index_count has an explanation for the magic |
| 6237 | * number |
| 6238 | */ |
| 6239 | BTRFS_I(inode)->index_cnt = 2; |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 6240 | BTRFS_I(inode)->dir_index = *index; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6241 | BTRFS_I(inode)->root = root; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 6242 | BTRFS_I(inode)->generation = trans->transid; |
Josef Bacik | 7619585 | 2010-11-19 02:18:02 +0000 | [diff] [blame] | 6243 | inode->i_generation = BTRFS_I(inode)->generation; |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6244 | |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 6245 | /* |
| 6246 | * We could have gotten an inode number from somebody who was fsynced |
| 6247 | * and then removed in this same transaction, so let's just set full |
| 6248 | * sync since it will be a full sync anyway and this will blow away the |
| 6249 | * old info in the log. |
| 6250 | */ |
| 6251 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags); |
| 6252 | |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 6253 | key[0].objectid = objectid; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 6254 | key[0].type = BTRFS_INODE_ITEM_KEY; |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 6255 | key[0].offset = 0; |
| 6256 | |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 6257 | sizes[0] = sizeof(struct btrfs_inode_item); |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 6258 | |
| 6259 | if (name) { |
| 6260 | /* |
| 6261 | * Start new inodes with an inode_ref. This is slightly more |
| 6262 | * efficient for small numbers of hard links since they will |
| 6263 | * be packed into one item. Extended refs will kick in if we |
| 6264 | * add more hard links than can fit in the ref item. |
| 6265 | */ |
| 6266 | key[1].objectid = objectid; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 6267 | key[1].type = BTRFS_INODE_REF_KEY; |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 6268 | key[1].offset = ref_objectid; |
| 6269 | |
| 6270 | sizes[1] = name_len + sizeof(*ref); |
| 6271 | } |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 6272 | |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6273 | location = &BTRFS_I(inode)->location; |
| 6274 | location->objectid = objectid; |
| 6275 | location->offset = 0; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 6276 | location->type = BTRFS_INODE_ITEM_KEY; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6277 | |
| 6278 | ret = btrfs_insert_inode_locked(inode); |
| 6279 | if (ret < 0) |
| 6280 | goto fail; |
| 6281 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 6282 | path->leave_spinning = 1; |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 6283 | ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems); |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 6284 | if (ret != 0) |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6285 | goto fail_unlock; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6286 | |
Dmitry Monakhov | ecc11fab | 2010-03-04 17:31:47 +0300 | [diff] [blame] | 6287 | inode_init_owner(inode, dir, mode); |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 6288 | inode_set_bytes(inode, 0); |
chandan r | 9cc97d6 | 2012-07-04 12:48:07 +0530 | [diff] [blame] | 6289 | |
Deepa Dinamani | c2050a4 | 2016-09-14 07:48:06 -0700 | [diff] [blame] | 6290 | inode->i_mtime = current_time(inode); |
chandan r | 9cc97d6 | 2012-07-04 12:48:07 +0530 | [diff] [blame] | 6291 | inode->i_atime = inode->i_mtime; |
| 6292 | inode->i_ctime = inode->i_mtime; |
| 6293 | BTRFS_I(inode)->i_otime = inode->i_mtime; |
| 6294 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6295 | inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 6296 | struct btrfs_inode_item); |
David Sterba | b159fa2 | 2016-11-08 18:09:03 +0100 | [diff] [blame] | 6297 | memzero_extent_buffer(path->nodes[0], (unsigned long)inode_item, |
Li Zefan | 293f7e0 | 2012-07-10 00:58:58 -0600 | [diff] [blame] | 6298 | sizeof(*inode_item)); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 6299 | fill_inode_item(trans, path->nodes[0], inode_item, inode); |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 6300 | |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 6301 | if (name) { |
| 6302 | ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1, |
| 6303 | struct btrfs_inode_ref); |
| 6304 | btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len); |
| 6305 | btrfs_set_inode_ref_index(path->nodes[0], ref, *index); |
| 6306 | ptr = (unsigned long)(ref + 1); |
| 6307 | write_extent_buffer(path->nodes[0], name, ptr, name_len); |
| 6308 | } |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 6309 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6310 | btrfs_mark_buffer_dirty(path->nodes[0]); |
| 6311 | btrfs_free_path(path); |
| 6312 | |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 6313 | btrfs_inherit_iflags(inode, dir); |
| 6314 | |
Al Viro | 569254b | 2011-07-24 17:08:40 -0400 | [diff] [blame] | 6315 | if (S_ISREG(mode)) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6316 | if (btrfs_test_opt(fs_info, NODATASUM)) |
Chris Mason | 9427216 | 2009-07-02 12:26:06 -0400 | [diff] [blame] | 6317 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6318 | if (btrfs_test_opt(fs_info, NODATACOW)) |
Josef Bacik | f2bdf9a | 2013-02-21 15:28:28 -0500 | [diff] [blame] | 6319 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW | |
| 6320 | BTRFS_INODE_NODATASUM; |
Chris Mason | 9427216 | 2009-07-02 12:26:06 -0400 | [diff] [blame] | 6321 | } |
| 6322 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6323 | inode_tree_add(inode); |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 6324 | |
| 6325 | trace_btrfs_inode_new(inode); |
Chris Mason | 1973f0f | 2011-06-24 13:13:29 -0400 | [diff] [blame] | 6326 | btrfs_set_inode_last_trans(trans, inode); |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 6327 | |
Alexander Block | 8ea05e3 | 2012-07-25 17:35:53 +0200 | [diff] [blame] | 6328 | btrfs_update_root_times(trans, root); |
| 6329 | |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 6330 | ret = btrfs_inode_inherit_props(trans, inode, dir); |
| 6331 | if (ret) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6332 | btrfs_err(fs_info, |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 6333 | "error inheriting props for ino %llu (root %llu): %d", |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 6334 | btrfs_ino(BTRFS_I(inode)), root->root_key.objectid, ret); |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 6335 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6336 | return inode; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6337 | |
| 6338 | fail_unlock: |
| 6339 | unlock_new_inode(inode); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6340 | fail: |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 6341 | if (dir && name) |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6342 | BTRFS_I(dir)->index_cnt--; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6343 | btrfs_free_path(path); |
Shen Feng | 0977143 | 2009-04-02 16:46:06 -0400 | [diff] [blame] | 6344 | iput(inode); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6345 | return ERR_PTR(ret); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6346 | } |
| 6347 | |
| 6348 | static inline u8 btrfs_inode_type(struct inode *inode) |
| 6349 | { |
| 6350 | return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT]; |
| 6351 | } |
| 6352 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 6353 | /* |
| 6354 | * utility function to add 'inode' into 'parent_inode' with |
| 6355 | * a give name and a given sequence number. |
| 6356 | * if 'add_backref' is true, also insert a backref from the |
| 6357 | * inode to the parent directory. |
| 6358 | */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 6359 | int btrfs_add_link(struct btrfs_trans_handle *trans, |
Nikolay Borisov | db0a669 | 2017-02-20 13:51:08 +0200 | [diff] [blame] | 6360 | struct btrfs_inode *parent_inode, struct btrfs_inode *inode, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 6361 | const char *name, int name_len, int add_backref, u64 index) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6362 | { |
Nikolay Borisov | db0a669 | 2017-02-20 13:51:08 +0200 | [diff] [blame] | 6363 | 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] | 6364 | int ret = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6365 | struct btrfs_key key; |
Nikolay Borisov | db0a669 | 2017-02-20 13:51:08 +0200 | [diff] [blame] | 6366 | struct btrfs_root *root = parent_inode->root; |
| 6367 | u64 ino = btrfs_ino(inode); |
| 6368 | u64 parent_ino = btrfs_ino(parent_inode); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6369 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6370 | if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) { |
Nikolay Borisov | db0a669 | 2017-02-20 13:51:08 +0200 | [diff] [blame] | 6371 | memcpy(&key, &inode->root->root_key, sizeof(key)); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6372 | } else { |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6373 | key.objectid = ino; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 6374 | key.type = BTRFS_INODE_ITEM_KEY; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6375 | key.offset = 0; |
| 6376 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6377 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6378 | if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6379 | ret = btrfs_add_root_ref(trans, fs_info, key.objectid, |
| 6380 | root->root_key.objectid, parent_ino, |
| 6381 | index, name, name_len); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6382 | } else if (add_backref) { |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6383 | ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino, |
| 6384 | parent_ino, index); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6385 | } |
| 6386 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6387 | /* Nothing to clean up yet */ |
| 6388 | if (ret) |
| 6389 | return ret; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6390 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6391 | ret = btrfs_insert_dir_item(trans, root, name, name_len, |
| 6392 | parent_inode, &key, |
Nikolay Borisov | db0a669 | 2017-02-20 13:51:08 +0200 | [diff] [blame] | 6393 | btrfs_inode_type(&inode->vfs_inode), index); |
Chris Mason | 9c52057 | 2012-12-17 14:26:57 -0500 | [diff] [blame] | 6394 | if (ret == -EEXIST || ret == -EOVERFLOW) |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6395 | goto fail_dir_item; |
| 6396 | else if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 6397 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6398 | return ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6399 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6400 | |
Nikolay Borisov | db0a669 | 2017-02-20 13:51:08 +0200 | [diff] [blame] | 6401 | btrfs_i_size_write(parent_inode, parent_inode->vfs_inode.i_size + |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6402 | name_len * 2); |
Nikolay Borisov | db0a669 | 2017-02-20 13:51:08 +0200 | [diff] [blame] | 6403 | inode_inc_iversion(&parent_inode->vfs_inode); |
| 6404 | parent_inode->vfs_inode.i_mtime = parent_inode->vfs_inode.i_ctime = |
| 6405 | current_time(&parent_inode->vfs_inode); |
| 6406 | ret = btrfs_update_inode(trans, root, &parent_inode->vfs_inode); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6407 | if (ret) |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 6408 | btrfs_abort_transaction(trans, ret); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6409 | return ret; |
Chris Mason | fe66a05 | 2012-02-20 08:40:56 -0500 | [diff] [blame] | 6410 | |
| 6411 | fail_dir_item: |
| 6412 | if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) { |
| 6413 | u64 local_index; |
| 6414 | int err; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6415 | err = btrfs_del_root_ref(trans, fs_info, key.objectid, |
| 6416 | root->root_key.objectid, parent_ino, |
| 6417 | &local_index, name, name_len); |
Chris Mason | fe66a05 | 2012-02-20 08:40:56 -0500 | [diff] [blame] | 6418 | |
| 6419 | } else if (add_backref) { |
| 6420 | u64 local_index; |
| 6421 | int err; |
| 6422 | |
| 6423 | err = btrfs_del_inode_ref(trans, root, name, name_len, |
| 6424 | ino, parent_ino, &local_index); |
| 6425 | } |
| 6426 | return ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6427 | } |
| 6428 | |
| 6429 | static int btrfs_add_nondir(struct btrfs_trans_handle *trans, |
Nikolay Borisov | cef415a | 2017-02-20 13:51:09 +0200 | [diff] [blame] | 6430 | struct btrfs_inode *dir, struct dentry *dentry, |
| 6431 | struct btrfs_inode *inode, int backref, u64 index) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6432 | { |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 6433 | int err = btrfs_add_link(trans, dir, inode, |
| 6434 | dentry->d_name.name, dentry->d_name.len, |
| 6435 | backref, index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6436 | if (err > 0) |
| 6437 | err = -EEXIST; |
| 6438 | return err; |
| 6439 | } |
| 6440 | |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6441 | static int btrfs_mknod(struct inode *dir, struct dentry *dentry, |
Al Viro | 1a67aaf | 2011-07-26 01:52:52 -0400 | [diff] [blame] | 6442 | umode_t mode, dev_t rdev) |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6443 | { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6444 | struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6445 | struct btrfs_trans_handle *trans; |
| 6446 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 6447 | struct inode *inode = NULL; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6448 | int err; |
| 6449 | int drop_inode = 0; |
| 6450 | u64 objectid; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 6451 | u64 index = 0; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6452 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 6453 | /* |
| 6454 | * 2 for inode item and ref |
| 6455 | * 2 for dir items |
| 6456 | * 1 for xattr if selinux is on |
| 6457 | */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6458 | trans = btrfs_start_transaction(root, 5); |
| 6459 | if (IS_ERR(trans)) |
| 6460 | return PTR_ERR(trans); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 6461 | |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 6462 | err = btrfs_find_free_ino(root, &objectid); |
| 6463 | if (err) |
| 6464 | goto out_unlock; |
| 6465 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6466 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 6467 | dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid, |
| 6468 | mode, &index); |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 6469 | if (IS_ERR(inode)) { |
| 6470 | err = PTR_ERR(inode); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6471 | goto out_unlock; |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 6472 | } |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6473 | |
Casey Schaufler | ad19db7 | 2011-12-15 10:09:07 -0500 | [diff] [blame] | 6474 | /* |
| 6475 | * If the active LSM wants to access the inode during |
| 6476 | * d_instantiate it needs these. Smack checks to see |
| 6477 | * if the filesystem supports xattrs by looking at the |
| 6478 | * ops vector. |
| 6479 | */ |
Casey Schaufler | ad19db7 | 2011-12-15 10:09:07 -0500 | [diff] [blame] | 6480 | inode->i_op = &btrfs_special_inode_operations; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6481 | init_special_inode(inode, inode->i_mode, rdev); |
| 6482 | |
| 6483 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6484 | if (err) |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6485 | goto out_unlock_inode; |
| 6486 | |
Nikolay Borisov | cef415a | 2017-02-20 13:51:09 +0200 | [diff] [blame] | 6487 | err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode), |
| 6488 | 0, index); |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6489 | if (err) { |
| 6490 | goto out_unlock_inode; |
| 6491 | } else { |
Yan | 1b4ab1b | 2007-08-29 09:11:44 -0400 | [diff] [blame] | 6492 | btrfs_update_inode(trans, root, inode); |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6493 | unlock_new_inode(inode); |
Al Viro | 08c422c | 2011-12-23 07:58:13 -0500 | [diff] [blame] | 6494 | d_instantiate(dentry, inode); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6495 | } |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6496 | |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6497 | out_unlock: |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 6498 | btrfs_end_transaction(trans); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6499 | btrfs_balance_delayed_items(fs_info); |
| 6500 | btrfs_btree_balance_dirty(fs_info); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6501 | if (drop_inode) { |
| 6502 | inode_dec_link_count(inode); |
| 6503 | iput(inode); |
| 6504 | } |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6505 | return err; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6506 | |
| 6507 | out_unlock_inode: |
| 6508 | drop_inode = 1; |
| 6509 | unlock_new_inode(inode); |
| 6510 | goto out_unlock; |
| 6511 | |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6512 | } |
| 6513 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6514 | static int btrfs_create(struct inode *dir, struct dentry *dentry, |
Al Viro | ebfc3b4 | 2012-06-10 18:05:36 -0400 | [diff] [blame] | 6515 | umode_t mode, bool excl) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6516 | { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6517 | struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6518 | struct btrfs_trans_handle *trans; |
| 6519 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 6520 | struct inode *inode = NULL; |
Filipe Brandenburger | 43baa57 | 2012-11-30 03:40:09 +0000 | [diff] [blame] | 6521 | int drop_inode_on_err = 0; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6522 | int err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6523 | u64 objectid; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 6524 | u64 index = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6525 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 6526 | /* |
| 6527 | * 2 for inode item and ref |
| 6528 | * 2 for dir items |
| 6529 | * 1 for xattr if selinux is on |
| 6530 | */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6531 | trans = btrfs_start_transaction(root, 5); |
| 6532 | if (IS_ERR(trans)) |
| 6533 | return PTR_ERR(trans); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 6534 | |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 6535 | err = btrfs_find_free_ino(root, &objectid); |
| 6536 | if (err) |
| 6537 | goto out_unlock; |
| 6538 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6539 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 6540 | dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid, |
| 6541 | mode, &index); |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 6542 | if (IS_ERR(inode)) { |
| 6543 | err = PTR_ERR(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6544 | goto out_unlock; |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 6545 | } |
Filipe Brandenburger | 43baa57 | 2012-11-30 03:40:09 +0000 | [diff] [blame] | 6546 | drop_inode_on_err = 1; |
Casey Schaufler | ad19db7 | 2011-12-15 10:09:07 -0500 | [diff] [blame] | 6547 | /* |
| 6548 | * If the active LSM wants to access the inode during |
| 6549 | * d_instantiate it needs these. Smack checks to see |
| 6550 | * if the filesystem supports xattrs by looking at the |
| 6551 | * ops vector. |
| 6552 | */ |
| 6553 | inode->i_fop = &btrfs_file_operations; |
| 6554 | inode->i_op = &btrfs_file_inode_operations; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6555 | inode->i_mapping->a_ops = &btrfs_aops; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6556 | |
| 6557 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); |
| 6558 | if (err) |
| 6559 | goto out_unlock_inode; |
| 6560 | |
| 6561 | err = btrfs_update_inode(trans, root, inode); |
| 6562 | if (err) |
| 6563 | goto out_unlock_inode; |
Casey Schaufler | ad19db7 | 2011-12-15 10:09:07 -0500 | [diff] [blame] | 6564 | |
Nikolay Borisov | cef415a | 2017-02-20 13:51:09 +0200 | [diff] [blame] | 6565 | err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode), |
| 6566 | 0, index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6567 | if (err) |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6568 | goto out_unlock_inode; |
Filipe Brandenburger | 43baa57 | 2012-11-30 03:40:09 +0000 | [diff] [blame] | 6569 | |
Filipe Brandenburger | 43baa57 | 2012-11-30 03:40:09 +0000 | [diff] [blame] | 6570 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6571 | unlock_new_inode(inode); |
Filipe Brandenburger | 43baa57 | 2012-11-30 03:40:09 +0000 | [diff] [blame] | 6572 | d_instantiate(dentry, inode); |
| 6573 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6574 | out_unlock: |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 6575 | btrfs_end_transaction(trans); |
Filipe Brandenburger | 43baa57 | 2012-11-30 03:40:09 +0000 | [diff] [blame] | 6576 | if (err && drop_inode_on_err) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6577 | inode_dec_link_count(inode); |
| 6578 | iput(inode); |
| 6579 | } |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6580 | btrfs_balance_delayed_items(fs_info); |
| 6581 | btrfs_btree_balance_dirty(fs_info); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6582 | return err; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6583 | |
| 6584 | out_unlock_inode: |
| 6585 | unlock_new_inode(inode); |
| 6586 | goto out_unlock; |
| 6587 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6588 | } |
| 6589 | |
| 6590 | static int btrfs_link(struct dentry *old_dentry, struct inode *dir, |
| 6591 | struct dentry *dentry) |
| 6592 | { |
Filipe Manana | 271dba4 | 2016-01-05 16:24:05 +0000 | [diff] [blame] | 6593 | struct btrfs_trans_handle *trans = NULL; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6594 | struct btrfs_root *root = BTRFS_I(dir)->root; |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 6595 | struct inode *inode = d_inode(old_dentry); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6596 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 6597 | u64 index; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6598 | int err; |
| 6599 | int drop_inode = 0; |
| 6600 | |
TARUISI Hiroaki | 4a8be42 | 2009-11-12 07:14:26 +0000 | [diff] [blame] | 6601 | /* do not allow sys_link's with other subvols of the same device */ |
| 6602 | if (root->objectid != BTRFS_I(inode)->root->objectid) |
Mark Fasheh | 3ab3564 | 2011-03-22 17:20:26 +0000 | [diff] [blame] | 6603 | return -EXDEV; |
TARUISI Hiroaki | 4a8be42 | 2009-11-12 07:14:26 +0000 | [diff] [blame] | 6604 | |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 6605 | if (inode->i_nlink >= BTRFS_LINK_MAX) |
Al Viro | c055e99 | 2011-03-04 17:15:18 +0000 | [diff] [blame] | 6606 | return -EMLINK; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 6607 | |
Nikolay Borisov | 877574e | 2017-02-20 13:50:33 +0200 | [diff] [blame] | 6608 | err = btrfs_set_inode_index(BTRFS_I(dir), &index); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6609 | if (err) |
| 6610 | goto fail; |
| 6611 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6612 | /* |
Miao Xie | 7e6b646 | 2011-02-18 09:21:17 +0000 | [diff] [blame] | 6613 | * 2 items for inode and inode ref |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6614 | * 2 items for dir items |
Miao Xie | 7e6b646 | 2011-02-18 09:21:17 +0000 | [diff] [blame] | 6615 | * 1 item for parent inode |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6616 | */ |
Miao Xie | 7e6b646 | 2011-02-18 09:21:17 +0000 | [diff] [blame] | 6617 | trans = btrfs_start_transaction(root, 5); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6618 | if (IS_ERR(trans)) { |
| 6619 | err = PTR_ERR(trans); |
Filipe Manana | 271dba4 | 2016-01-05 16:24:05 +0000 | [diff] [blame] | 6620 | trans = NULL; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6621 | goto fail; |
| 6622 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6623 | |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 6624 | /* There are several dir indexes for this inode, clear the cache. */ |
| 6625 | BTRFS_I(inode)->dir_index = 0ULL; |
Zach Brown | 8b558c5 | 2013-10-16 12:10:34 -0700 | [diff] [blame] | 6626 | inc_nlink(inode); |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 6627 | inode_inc_iversion(inode); |
Deepa Dinamani | c2050a4 | 2016-09-14 07:48:06 -0700 | [diff] [blame] | 6628 | inode->i_ctime = current_time(inode); |
Al Viro | 7de9c6ee | 2010-10-23 11:11:40 -0400 | [diff] [blame] | 6629 | ihold(inode); |
Josef Bacik | e997615 | 2012-10-11 15:53:56 -0400 | [diff] [blame] | 6630 | set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6631 | |
Nikolay Borisov | cef415a | 2017-02-20 13:51:09 +0200 | [diff] [blame] | 6632 | err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode), |
| 6633 | 1, index); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6634 | |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 6635 | if (err) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6636 | drop_inode = 1; |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 6637 | } else { |
Al Viro | 10d9f30 | 2011-07-16 23:09:10 -0400 | [diff] [blame] | 6638 | struct dentry *parent = dentry->d_parent; |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 6639 | err = btrfs_update_inode(trans, root, inode); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6640 | if (err) |
| 6641 | goto fail; |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 6642 | if (inode->i_nlink == 1) { |
| 6643 | /* |
| 6644 | * If new hard link count is 1, it's a file created |
| 6645 | * with open(2) O_TMPFILE flag. |
| 6646 | */ |
Nikolay Borisov | 3d6ae7b | 2017-02-20 13:50:58 +0200 | [diff] [blame] | 6647 | err = btrfs_orphan_del(trans, BTRFS_I(inode)); |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 6648 | if (err) |
| 6649 | goto fail; |
| 6650 | } |
Al Viro | 08c422c | 2011-12-23 07:58:13 -0500 | [diff] [blame] | 6651 | d_instantiate(dentry, inode); |
Nikolay Borisov | 9ca5fbfb | 2017-01-18 00:31:31 +0200 | [diff] [blame] | 6652 | btrfs_log_new_name(trans, BTRFS_I(inode), NULL, parent); |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 6653 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6654 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6655 | btrfs_balance_delayed_items(fs_info); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 6656 | fail: |
Filipe Manana | 271dba4 | 2016-01-05 16:24:05 +0000 | [diff] [blame] | 6657 | if (trans) |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 6658 | btrfs_end_transaction(trans); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6659 | if (drop_inode) { |
| 6660 | inode_dec_link_count(inode); |
| 6661 | iput(inode); |
| 6662 | } |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6663 | btrfs_btree_balance_dirty(fs_info); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6664 | return err; |
| 6665 | } |
| 6666 | |
Al Viro | 18bb1db | 2011-07-26 01:41:39 -0400 | [diff] [blame] | 6667 | 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] | 6668 | { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6669 | struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); |
Chris Mason | b9d8666 | 2008-05-02 16:13:49 -0400 | [diff] [blame] | 6670 | struct inode *inode = NULL; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6671 | struct btrfs_trans_handle *trans; |
| 6672 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 6673 | int err = 0; |
| 6674 | int drop_on_err = 0; |
Chris Mason | b9d8666 | 2008-05-02 16:13:49 -0400 | [diff] [blame] | 6675 | u64 objectid = 0; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 6676 | u64 index = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6677 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 6678 | /* |
| 6679 | * 2 items for inode and ref |
| 6680 | * 2 items for dir items |
| 6681 | * 1 for xattr if selinux is on |
| 6682 | */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6683 | trans = btrfs_start_transaction(root, 5); |
| 6684 | if (IS_ERR(trans)) |
| 6685 | return PTR_ERR(trans); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6686 | |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 6687 | err = btrfs_find_free_ino(root, &objectid); |
| 6688 | if (err) |
| 6689 | goto out_fail; |
| 6690 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6691 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 6692 | dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid, |
| 6693 | S_IFDIR | mode, &index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6694 | if (IS_ERR(inode)) { |
| 6695 | err = PTR_ERR(inode); |
| 6696 | goto out_fail; |
| 6697 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6698 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6699 | drop_on_err = 1; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6700 | /* these must be set before we unlock the inode */ |
| 6701 | inode->i_op = &btrfs_dir_inode_operations; |
| 6702 | inode->i_fop = &btrfs_dir_file_operations; |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 6703 | |
Eric Paris | 2a7dba3 | 2011-02-01 11:05:39 -0500 | [diff] [blame] | 6704 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 6705 | if (err) |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6706 | goto out_fail_inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6707 | |
Nikolay Borisov | 6ef06d2 | 2017-02-20 13:50:34 +0200 | [diff] [blame] | 6708 | btrfs_i_size_write(BTRFS_I(inode), 0); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6709 | err = btrfs_update_inode(trans, root, inode); |
| 6710 | if (err) |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6711 | goto out_fail_inode; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6712 | |
Nikolay Borisov | db0a669 | 2017-02-20 13:51:08 +0200 | [diff] [blame] | 6713 | err = btrfs_add_link(trans, BTRFS_I(dir), BTRFS_I(inode), |
| 6714 | dentry->d_name.name, |
| 6715 | dentry->d_name.len, 0, index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6716 | if (err) |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6717 | goto out_fail_inode; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6718 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6719 | d_instantiate(dentry, inode); |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6720 | /* |
| 6721 | * mkdir is special. We're unlocking after we call d_instantiate |
| 6722 | * to avoid a race with nfsd calling d_instantiate. |
| 6723 | */ |
| 6724 | unlock_new_inode(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6725 | drop_on_err = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6726 | |
| 6727 | out_fail: |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 6728 | btrfs_end_transaction(trans); |
Wang Shilong | c7cfb8a | 2014-12-24 14:45:30 +0800 | [diff] [blame] | 6729 | if (drop_on_err) { |
| 6730 | inode_dec_link_count(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6731 | iput(inode); |
Wang Shilong | c7cfb8a | 2014-12-24 14:45:30 +0800 | [diff] [blame] | 6732 | } |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6733 | btrfs_balance_delayed_items(fs_info); |
| 6734 | btrfs_btree_balance_dirty(fs_info); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6735 | return err; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6736 | |
| 6737 | out_fail_inode: |
| 6738 | unlock_new_inode(inode); |
| 6739 | goto out_fail; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6740 | } |
| 6741 | |
Qu Wenruo | e6c4efd | 2014-09-17 11:53:35 +0800 | [diff] [blame] | 6742 | /* Find next extent map of a given extent map, caller needs to ensure locks */ |
| 6743 | static struct extent_map *next_extent_map(struct extent_map *em) |
| 6744 | { |
| 6745 | struct rb_node *next; |
| 6746 | |
| 6747 | next = rb_next(&em->rb_node); |
| 6748 | if (!next) |
| 6749 | return NULL; |
| 6750 | return container_of(next, struct extent_map, rb_node); |
| 6751 | } |
| 6752 | |
| 6753 | static struct extent_map *prev_extent_map(struct extent_map *em) |
| 6754 | { |
| 6755 | struct rb_node *prev; |
| 6756 | |
| 6757 | prev = rb_prev(&em->rb_node); |
| 6758 | if (!prev) |
| 6759 | return NULL; |
| 6760 | return container_of(prev, struct extent_map, rb_node); |
| 6761 | } |
| 6762 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 6763 | /* helper for btfs_get_extent. Given an existing extent in the tree, |
Qu Wenruo | e6c4efd | 2014-09-17 11:53:35 +0800 | [diff] [blame] | 6764 | * the existing extent is the nearest extent to map_start, |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 6765 | * 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] | 6766 | * the best fitted new extent into the tree. |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 6767 | */ |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 6768 | static int merge_extent_mapping(struct extent_map_tree *em_tree, |
| 6769 | struct extent_map *existing, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6770 | struct extent_map *em, |
Qu Wenruo | 51f395a | 2014-08-08 13:06:20 +0800 | [diff] [blame] | 6771 | u64 map_start) |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 6772 | { |
Qu Wenruo | e6c4efd | 2014-09-17 11:53:35 +0800 | [diff] [blame] | 6773 | struct extent_map *prev; |
| 6774 | struct extent_map *next; |
| 6775 | u64 start; |
| 6776 | u64 end; |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 6777 | u64 start_diff; |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 6778 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6779 | BUG_ON(map_start < em->start || map_start >= extent_map_end(em)); |
Qu Wenruo | e6c4efd | 2014-09-17 11:53:35 +0800 | [diff] [blame] | 6780 | |
| 6781 | if (existing->start > map_start) { |
| 6782 | next = existing; |
| 6783 | prev = prev_extent_map(next); |
| 6784 | } else { |
| 6785 | prev = existing; |
| 6786 | next = next_extent_map(prev); |
| 6787 | } |
| 6788 | |
| 6789 | start = prev ? extent_map_end(prev) : em->start; |
| 6790 | start = max_t(u64, start, em->start); |
| 6791 | end = next ? next->start : extent_map_end(em); |
| 6792 | end = min_t(u64, end, extent_map_end(em)); |
| 6793 | start_diff = start - em->start; |
| 6794 | em->start = start; |
| 6795 | em->len = end - start; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6796 | if (em->block_start < EXTENT_MAP_LAST_BYTE && |
| 6797 | !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6798 | em->block_start += start_diff; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6799 | em->block_len -= start_diff; |
| 6800 | } |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 6801 | return add_extent_mapping(em_tree, em, 0); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 6802 | } |
| 6803 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6804 | static noinline int uncompress_inline(struct btrfs_path *path, |
Byongho Lee | e40da0e | 2015-05-19 23:46:45 +0900 | [diff] [blame] | 6805 | struct page *page, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6806 | size_t pg_offset, u64 extent_offset, |
| 6807 | struct btrfs_file_extent_item *item) |
| 6808 | { |
| 6809 | int ret; |
| 6810 | struct extent_buffer *leaf = path->nodes[0]; |
| 6811 | char *tmp; |
| 6812 | size_t max_size; |
| 6813 | unsigned long inline_size; |
| 6814 | unsigned long ptr; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 6815 | int compress_type; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6816 | |
| 6817 | WARN_ON(pg_offset != 0); |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 6818 | compress_type = btrfs_file_extent_compression(leaf, item); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6819 | max_size = btrfs_file_extent_ram_bytes(leaf, item); |
| 6820 | inline_size = btrfs_file_extent_inline_item_len(leaf, |
Ross Kirk | dd3cc16 | 2013-09-16 15:58:09 +0100 | [diff] [blame] | 6821 | btrfs_item_nr(path->slots[0])); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6822 | tmp = kmalloc(inline_size, GFP_NOFS); |
Tsutomu Itoh | 8d41371 | 2011-04-25 19:43:52 -0400 | [diff] [blame] | 6823 | if (!tmp) |
| 6824 | return -ENOMEM; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6825 | ptr = btrfs_file_extent_inline_start(item); |
| 6826 | |
| 6827 | read_extent_buffer(leaf, tmp, ptr, inline_size); |
| 6828 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 6829 | max_size = min_t(unsigned long, PAGE_SIZE, max_size); |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 6830 | ret = btrfs_decompress(compress_type, tmp, page, |
| 6831 | extent_offset, inline_size, max_size); |
Zygo Blaxell | e1699d2 | 2017-03-10 16:45:44 -0500 | [diff] [blame] | 6832 | |
| 6833 | /* |
| 6834 | * decompression code contains a memset to fill in any space between the end |
| 6835 | * of the uncompressed data and the end of max_size in case the decompressed |
| 6836 | * data ends up shorter than ram_bytes. That doesn't cover the hole between |
| 6837 | * the end of an inline extent and the beginning of the next block, so we |
| 6838 | * cover that region here. |
| 6839 | */ |
| 6840 | |
| 6841 | if (max_size + pg_offset < PAGE_SIZE) { |
| 6842 | char *map = kmap(page); |
| 6843 | memset(map + pg_offset + max_size, 0, PAGE_SIZE - max_size - pg_offset); |
| 6844 | kunmap(page); |
| 6845 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6846 | kfree(tmp); |
Zach Brown | 166ae5a | 2014-05-09 17:15:10 -0400 | [diff] [blame] | 6847 | return ret; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6848 | } |
| 6849 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 6850 | /* |
| 6851 | * 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] | 6852 | * the ugly parts come from merging extents from the disk with the in-ram |
| 6853 | * representation. This gets more complex because of the data=ordered code, |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 6854 | * where the in-ram extents might be locked pending data=ordered completion. |
| 6855 | * |
| 6856 | * This also copies inline extents directly into the page. |
| 6857 | */ |
Nikolay Borisov | fc4f21b | 2017-02-20 13:51:06 +0200 | [diff] [blame] | 6858 | struct extent_map *btrfs_get_extent(struct btrfs_inode *inode, |
| 6859 | struct page *page, |
| 6860 | size_t pg_offset, u64 start, u64 len, |
| 6861 | int create) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6862 | { |
Nikolay Borisov | fc4f21b | 2017-02-20 13:51:06 +0200 | [diff] [blame] | 6863 | 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] | 6864 | int ret; |
| 6865 | int err = 0; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6866 | u64 extent_start = 0; |
| 6867 | u64 extent_end = 0; |
Nikolay Borisov | fc4f21b | 2017-02-20 13:51:06 +0200 | [diff] [blame] | 6868 | u64 objectid = btrfs_ino(inode); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6869 | u32 found_type; |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 6870 | struct btrfs_path *path = NULL; |
Nikolay Borisov | fc4f21b | 2017-02-20 13:51:06 +0200 | [diff] [blame] | 6871 | struct btrfs_root *root = inode->root; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6872 | struct btrfs_file_extent_item *item; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6873 | struct extent_buffer *leaf; |
| 6874 | struct btrfs_key found_key; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6875 | struct extent_map *em = NULL; |
Nikolay Borisov | fc4f21b | 2017-02-20 13:51:06 +0200 | [diff] [blame] | 6876 | struct extent_map_tree *em_tree = &inode->extent_tree; |
| 6877 | struct extent_io_tree *io_tree = &inode->io_tree; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6878 | struct btrfs_trans_handle *trans = NULL; |
Filipe Manana | 7ffbb59 | 2014-06-09 03:48:05 +0100 | [diff] [blame] | 6879 | const bool new_inline = !page || create; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6880 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6881 | again: |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 6882 | read_lock(&em_tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6883 | em = lookup_extent_mapping(em_tree, start, len); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 6884 | if (em) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6885 | em->bdev = fs_info->fs_devices->latest_bdev; |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 6886 | read_unlock(&em_tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6887 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6888 | if (em) { |
Chris Mason | e1c4b74 | 2008-04-22 13:26:46 -0400 | [diff] [blame] | 6889 | if (em->start > start || em->start + em->len <= start) |
| 6890 | free_extent_map(em); |
| 6891 | else if (em->block_start == EXTENT_MAP_INLINE && page) |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 6892 | free_extent_map(em); |
| 6893 | else |
| 6894 | goto out; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6895 | } |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 6896 | em = alloc_extent_map(); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6897 | if (!em) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6898 | err = -ENOMEM; |
| 6899 | goto out; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6900 | } |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6901 | em->bdev = fs_info->fs_devices->latest_bdev; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6902 | em->start = EXTENT_MAP_HOLE; |
Chris Mason | 445a694 | 2008-11-10 11:53:33 -0500 | [diff] [blame] | 6903 | em->orig_start = EXTENT_MAP_HOLE; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6904 | em->len = (u64)-1; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6905 | em->block_len = (u64)-1; |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 6906 | |
| 6907 | if (!path) { |
| 6908 | path = btrfs_alloc_path(); |
Josef Bacik | 026fd31 | 2011-05-13 10:32:11 -0400 | [diff] [blame] | 6909 | if (!path) { |
| 6910 | err = -ENOMEM; |
| 6911 | goto out; |
| 6912 | } |
| 6913 | /* |
| 6914 | * Chances are we'll be called again, so go ahead and do |
| 6915 | * readahead |
| 6916 | */ |
David Sterba | e4058b5 | 2015-11-27 16:31:35 +0100 | [diff] [blame] | 6917 | path->reada = READA_FORWARD; |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 6918 | } |
| 6919 | |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 6920 | ret = btrfs_lookup_file_extent(trans, root, path, |
| 6921 | objectid, start, trans != NULL); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6922 | if (ret < 0) { |
| 6923 | err = ret; |
| 6924 | goto out; |
| 6925 | } |
| 6926 | |
| 6927 | if (ret != 0) { |
| 6928 | if (path->slots[0] == 0) |
| 6929 | goto not_found; |
| 6930 | path->slots[0]--; |
| 6931 | } |
| 6932 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6933 | leaf = path->nodes[0]; |
| 6934 | item = btrfs_item_ptr(leaf, path->slots[0], |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6935 | struct btrfs_file_extent_item); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6936 | /* are we inside the extent that was found? */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6937 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 6938 | found_type = found_key.type; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6939 | if (found_key.objectid != objectid || |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6940 | found_type != BTRFS_EXTENT_DATA_KEY) { |
Josef Bacik | 25a5034 | 2013-10-14 12:08:38 -0400 | [diff] [blame] | 6941 | /* |
| 6942 | * If we backup past the first extent we want to move forward |
| 6943 | * and see if there is an extent in front of us, otherwise we'll |
| 6944 | * say there is a hole for our whole search range which can |
| 6945 | * cause problems. |
| 6946 | */ |
| 6947 | extent_end = start; |
| 6948 | goto next; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6949 | } |
| 6950 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6951 | found_type = btrfs_file_extent_type(leaf, item); |
| 6952 | extent_start = found_key.offset; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 6953 | if (found_type == BTRFS_FILE_EXTENT_REG || |
| 6954 | found_type == BTRFS_FILE_EXTENT_PREALLOC) { |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6955 | extent_end = extent_start + |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 6956 | btrfs_file_extent_num_bytes(leaf, item); |
Liu Bo | 09ed2f1 | 2017-03-10 11:09:48 -0800 | [diff] [blame] | 6957 | |
| 6958 | trace_btrfs_get_extent_show_fi_regular(inode, leaf, item, |
| 6959 | extent_start); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6960 | } else if (found_type == BTRFS_FILE_EXTENT_INLINE) { |
| 6961 | size_t size; |
Chris Mason | 514ac8a | 2014-01-03 21:07:00 -0800 | [diff] [blame] | 6962 | size = btrfs_file_extent_inline_len(leaf, path->slots[0], item); |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 6963 | extent_end = ALIGN(extent_start + size, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6964 | fs_info->sectorsize); |
Liu Bo | 09ed2f1 | 2017-03-10 11:09:48 -0800 | [diff] [blame] | 6965 | |
| 6966 | trace_btrfs_get_extent_show_fi_inline(inode, leaf, item, |
| 6967 | path->slots[0], |
| 6968 | extent_start); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6969 | } |
Josef Bacik | 25a5034 | 2013-10-14 12:08:38 -0400 | [diff] [blame] | 6970 | next: |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6971 | if (start >= extent_end) { |
| 6972 | path->slots[0]++; |
| 6973 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { |
| 6974 | ret = btrfs_next_leaf(root, path); |
| 6975 | if (ret < 0) { |
| 6976 | err = ret; |
| 6977 | goto out; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6978 | } |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6979 | if (ret > 0) |
| 6980 | goto not_found; |
| 6981 | leaf = path->nodes[0]; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6982 | } |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6983 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 6984 | if (found_key.objectid != objectid || |
| 6985 | found_key.type != BTRFS_EXTENT_DATA_KEY) |
| 6986 | goto not_found; |
| 6987 | if (start + len <= found_key.offset) |
| 6988 | goto not_found; |
Wang Shilong | e2eca69 | 2014-07-17 11:44:14 +0800 | [diff] [blame] | 6989 | if (start > found_key.offset) |
| 6990 | goto next; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6991 | em->start = start; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 6992 | em->orig_start = start; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6993 | em->len = found_key.offset - start; |
| 6994 | goto not_found_em; |
| 6995 | } |
| 6996 | |
Nikolay Borisov | fc4f21b | 2017-02-20 13:51:06 +0200 | [diff] [blame] | 6997 | btrfs_extent_item_to_extent_map(inode, path, item, |
Nikolay Borisov | 9cdc512 | 2017-02-20 13:51:02 +0200 | [diff] [blame] | 6998 | new_inline, em); |
Filipe Manana | 7ffbb59 | 2014-06-09 03:48:05 +0100 | [diff] [blame] | 6999 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7000 | if (found_type == BTRFS_FILE_EXTENT_REG || |
| 7001 | found_type == BTRFS_FILE_EXTENT_PREALLOC) { |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7002 | goto insert; |
| 7003 | } else if (found_type == BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 7004 | unsigned long ptr; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7005 | char *map; |
Chris Mason | 3326d1b | 2007-10-15 16:18:25 -0400 | [diff] [blame] | 7006 | size_t size; |
| 7007 | size_t extent_offset; |
| 7008 | size_t copy_size; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7009 | |
Filipe Manana | 7ffbb59 | 2014-06-09 03:48:05 +0100 | [diff] [blame] | 7010 | if (new_inline) |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 7011 | goto out; |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 7012 | |
Chris Mason | 514ac8a | 2014-01-03 21:07:00 -0800 | [diff] [blame] | 7013 | size = btrfs_file_extent_inline_len(leaf, path->slots[0], item); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 7014 | extent_offset = page_offset(page) + pg_offset - extent_start; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 7015 | copy_size = min_t(u64, PAGE_SIZE - pg_offset, |
| 7016 | size - extent_offset); |
Chris Mason | 3326d1b | 2007-10-15 16:18:25 -0400 | [diff] [blame] | 7017 | em->start = extent_start + extent_offset; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7018 | em->len = ALIGN(copy_size, fs_info->sectorsize); |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 7019 | em->orig_block_len = em->len; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 7020 | em->orig_start = em->start; |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 7021 | ptr = btrfs_file_extent_inline_start(item) + extent_offset; |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 7022 | if (create == 0 && !PageUptodate(page)) { |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 7023 | if (btrfs_file_extent_compression(leaf, item) != |
| 7024 | BTRFS_COMPRESS_NONE) { |
Byongho Lee | e40da0e | 2015-05-19 23:46:45 +0900 | [diff] [blame] | 7025 | ret = uncompress_inline(path, page, pg_offset, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 7026 | extent_offset, item); |
Zach Brown | 166ae5a | 2014-05-09 17:15:10 -0400 | [diff] [blame] | 7027 | if (ret) { |
| 7028 | err = ret; |
| 7029 | goto out; |
| 7030 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 7031 | } else { |
| 7032 | map = kmap(page); |
| 7033 | read_extent_buffer(leaf, map + pg_offset, ptr, |
| 7034 | copy_size); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 7035 | if (pg_offset + copy_size < PAGE_SIZE) { |
Chris Mason | 93c82d5 | 2009-09-11 12:36:29 -0400 | [diff] [blame] | 7036 | memset(map + pg_offset + copy_size, 0, |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 7037 | PAGE_SIZE - pg_offset - |
Chris Mason | 93c82d5 | 2009-09-11 12:36:29 -0400 | [diff] [blame] | 7038 | copy_size); |
| 7039 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 7040 | kunmap(page); |
| 7041 | } |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 7042 | flush_dcache_page(page); |
| 7043 | } else if (create && PageUptodate(page)) { |
Jan Schmidt | 6bf7e08 | 2011-12-01 14:35:19 +0100 | [diff] [blame] | 7044 | BUG(); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 7045 | if (!trans) { |
| 7046 | kunmap(page); |
| 7047 | free_extent_map(em); |
| 7048 | em = NULL; |
Chris Mason | ff5714c | 2011-05-28 07:00:39 -0400 | [diff] [blame] | 7049 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 7050 | btrfs_release_path(path); |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 7051 | trans = btrfs_join_transaction(root); |
Chris Mason | ff5714c | 2011-05-28 07:00:39 -0400 | [diff] [blame] | 7052 | |
Tsutomu Itoh | 3612b49 | 2011-01-25 02:51:38 +0000 | [diff] [blame] | 7053 | if (IS_ERR(trans)) |
| 7054 | return ERR_CAST(trans); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 7055 | goto again; |
| 7056 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 7057 | map = kmap(page); |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 7058 | write_extent_buffer(leaf, map + pg_offset, ptr, |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 7059 | copy_size); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 7060 | kunmap(page); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 7061 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7062 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 7063 | set_extent_uptodate(io_tree, em->start, |
Arne Jansen | 507903b | 2011-04-06 10:02:20 +0000 | [diff] [blame] | 7064 | extent_map_end(em) - 1, NULL, GFP_NOFS); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7065 | goto insert; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7066 | } |
| 7067 | not_found: |
| 7068 | em->start = start; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 7069 | em->orig_start = start; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 7070 | em->len = len; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7071 | not_found_em: |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 7072 | em->block_start = EXTENT_MAP_HOLE; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 7073 | set_bit(EXTENT_FLAG_VACANCY, &em->flags); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7074 | insert: |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 7075 | btrfs_release_path(path); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 7076 | if (em->start > start || extent_map_end(em) <= start) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7077 | btrfs_err(fs_info, |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 7078 | "bad extent! em: [%llu %llu] passed [%llu %llu]", |
| 7079 | em->start, em->len, start, len); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7080 | err = -EIO; |
| 7081 | goto out; |
| 7082 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 7083 | |
| 7084 | err = 0; |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 7085 | write_lock(&em_tree->lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 7086 | ret = add_extent_mapping(em_tree, em, 0); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 7087 | /* it is possible that someone inserted the extent into the tree |
| 7088 | * while we had the lock dropped. It is also possible that |
| 7089 | * an overlapping map exists in the tree |
| 7090 | */ |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7091 | if (ret == -EEXIST) { |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 7092 | struct extent_map *existing; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7093 | |
| 7094 | ret = 0; |
| 7095 | |
Qu Wenruo | e6c4efd | 2014-09-17 11:53:35 +0800 | [diff] [blame] | 7096 | existing = search_extent_mapping(em_tree, start, len); |
| 7097 | /* |
| 7098 | * existing will always be non-NULL, since there must be |
| 7099 | * extent causing the -EEXIST. |
| 7100 | */ |
Chris Mason | 8dff9c8 | 2015-09-19 11:28:25 -0700 | [diff] [blame] | 7101 | if (existing->start == em->start && |
Omar Sandoval | 8e2bd3b | 2016-11-09 15:26:50 -0800 | [diff] [blame] | 7102 | extent_map_end(existing) >= extent_map_end(em) && |
Chris Mason | 8dff9c8 | 2015-09-19 11:28:25 -0700 | [diff] [blame] | 7103 | em->block_start == existing->block_start) { |
| 7104 | /* |
Omar Sandoval | 8e2bd3b | 2016-11-09 15:26:50 -0800 | [diff] [blame] | 7105 | * The existing extent map already encompasses the |
| 7106 | * entire extent map we tried to add. |
Chris Mason | 8dff9c8 | 2015-09-19 11:28:25 -0700 | [diff] [blame] | 7107 | */ |
| 7108 | free_extent_map(em); |
| 7109 | em = existing; |
| 7110 | err = 0; |
| 7111 | |
| 7112 | } else if (start >= extent_map_end(existing) || |
Qu Wenruo | 32be3a1 | 2014-09-22 09:13:03 +0800 | [diff] [blame] | 7113 | start <= existing->start) { |
Qu Wenruo | e6c4efd | 2014-09-17 11:53:35 +0800 | [diff] [blame] | 7114 | /* |
| 7115 | * The existing extent map is the one nearest to |
| 7116 | * the [start, start + len) range which overlaps |
| 7117 | */ |
| 7118 | err = merge_extent_mapping(em_tree, existing, |
| 7119 | em, start); |
Chris Mason | e1c4b74 | 2008-04-22 13:26:46 -0400 | [diff] [blame] | 7120 | free_extent_map(existing); |
Qu Wenruo | e6c4efd | 2014-09-17 11:53:35 +0800 | [diff] [blame] | 7121 | if (err) { |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 7122 | free_extent_map(em); |
| 7123 | em = NULL; |
| 7124 | } |
| 7125 | } else { |
| 7126 | free_extent_map(em); |
| 7127 | em = existing; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7128 | err = 0; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7129 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7130 | } |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 7131 | write_unlock(&em_tree->lock); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7132 | out: |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 7133 | |
Nikolay Borisov | fc4f21b | 2017-02-20 13:51:06 +0200 | [diff] [blame] | 7134 | trace_btrfs_get_extent(root, inode, em); |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 7135 | |
Tsutomu Itoh | 527afb4 | 2015-08-19 14:55:00 +0900 | [diff] [blame] | 7136 | btrfs_free_path(path); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7137 | if (trans) { |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 7138 | ret = btrfs_end_transaction(trans); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 7139 | if (!err) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7140 | err = ret; |
| 7141 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7142 | if (err) { |
| 7143 | free_extent_map(em); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7144 | return ERR_PTR(err); |
| 7145 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 7146 | BUG_ON(!em); /* Error is always set */ |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7147 | return em; |
| 7148 | } |
| 7149 | |
Nikolay Borisov | fc4f21b | 2017-02-20 13:51:06 +0200 | [diff] [blame] | 7150 | struct extent_map *btrfs_get_extent_fiemap(struct btrfs_inode *inode, |
| 7151 | struct page *page, |
| 7152 | size_t pg_offset, u64 start, u64 len, |
| 7153 | int create) |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 7154 | { |
| 7155 | struct extent_map *em; |
| 7156 | struct extent_map *hole_em = NULL; |
| 7157 | u64 range_start = start; |
| 7158 | u64 end; |
| 7159 | u64 found; |
| 7160 | u64 found_end; |
| 7161 | int err = 0; |
| 7162 | |
| 7163 | em = btrfs_get_extent(inode, page, pg_offset, start, len, create); |
| 7164 | if (IS_ERR(em)) |
| 7165 | return em; |
Dan Carpenter | 9986277 | 2017-04-11 11:57:15 +0300 | [diff] [blame] | 7166 | /* |
| 7167 | * If our em maps to: |
| 7168 | * - a hole or |
| 7169 | * - a pre-alloc extent, |
| 7170 | * there might actually be delalloc bytes behind it. |
| 7171 | */ |
| 7172 | if (em->block_start != EXTENT_MAP_HOLE && |
| 7173 | !test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) |
| 7174 | return em; |
| 7175 | else |
| 7176 | hole_em = em; |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 7177 | |
| 7178 | /* check to see if we've wrapped (len == -1 or similar) */ |
| 7179 | end = start + len; |
| 7180 | if (end < start) |
| 7181 | end = (u64)-1; |
| 7182 | else |
| 7183 | end -= 1; |
| 7184 | |
| 7185 | em = NULL; |
| 7186 | |
| 7187 | /* ok, we didn't find anything, lets look for delalloc */ |
Nikolay Borisov | fc4f21b | 2017-02-20 13:51:06 +0200 | [diff] [blame] | 7188 | found = count_range_bits(&inode->io_tree, &range_start, |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 7189 | end, len, EXTENT_DELALLOC, 1); |
| 7190 | found_end = range_start + found; |
| 7191 | if (found_end < range_start) |
| 7192 | found_end = (u64)-1; |
| 7193 | |
| 7194 | /* |
| 7195 | * we didn't find anything useful, return |
| 7196 | * the original results from get_extent() |
| 7197 | */ |
| 7198 | if (range_start > end || found_end <= start) { |
| 7199 | em = hole_em; |
| 7200 | hole_em = NULL; |
| 7201 | goto out; |
| 7202 | } |
| 7203 | |
| 7204 | /* adjust the range_start to make sure it doesn't |
| 7205 | * go backwards from the start they passed in |
| 7206 | */ |
Dulshani Gunawardhana | 6787125 | 2013-10-31 10:33:04 +0530 | [diff] [blame] | 7207 | range_start = max(start, range_start); |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 7208 | found = found_end - range_start; |
| 7209 | |
| 7210 | if (found > 0) { |
| 7211 | u64 hole_start = start; |
| 7212 | u64 hole_len = len; |
| 7213 | |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 7214 | em = alloc_extent_map(); |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 7215 | if (!em) { |
| 7216 | err = -ENOMEM; |
| 7217 | goto out; |
| 7218 | } |
| 7219 | /* |
| 7220 | * when btrfs_get_extent can't find anything it |
| 7221 | * returns one huge hole |
| 7222 | * |
| 7223 | * make sure what it found really fits our range, and |
| 7224 | * adjust to make sure it is based on the start from |
| 7225 | * the caller |
| 7226 | */ |
| 7227 | if (hole_em) { |
| 7228 | u64 calc_end = extent_map_end(hole_em); |
| 7229 | |
| 7230 | if (calc_end <= start || (hole_em->start > end)) { |
| 7231 | free_extent_map(hole_em); |
| 7232 | hole_em = NULL; |
| 7233 | } else { |
| 7234 | hole_start = max(hole_em->start, start); |
| 7235 | hole_len = calc_end - hole_start; |
| 7236 | } |
| 7237 | } |
| 7238 | em->bdev = NULL; |
| 7239 | if (hole_em && range_start > hole_start) { |
| 7240 | /* our hole starts before our delalloc, so we |
| 7241 | * have to return just the parts of the hole |
| 7242 | * that go until the delalloc starts |
| 7243 | */ |
| 7244 | em->len = min(hole_len, |
| 7245 | range_start - hole_start); |
| 7246 | em->start = hole_start; |
| 7247 | em->orig_start = hole_start; |
| 7248 | /* |
| 7249 | * don't adjust block start at all, |
| 7250 | * it is fixed at EXTENT_MAP_HOLE |
| 7251 | */ |
| 7252 | em->block_start = hole_em->block_start; |
| 7253 | em->block_len = hole_len; |
Liu Bo | f9e4fb5 | 2013-01-07 10:10:12 +0000 | [diff] [blame] | 7254 | if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags)) |
| 7255 | set_bit(EXTENT_FLAG_PREALLOC, &em->flags); |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 7256 | } else { |
| 7257 | em->start = range_start; |
| 7258 | em->len = found; |
| 7259 | em->orig_start = range_start; |
| 7260 | em->block_start = EXTENT_MAP_DELALLOC; |
| 7261 | em->block_len = found; |
| 7262 | } |
| 7263 | } else if (hole_em) { |
| 7264 | return hole_em; |
| 7265 | } |
| 7266 | out: |
| 7267 | |
| 7268 | free_extent_map(hole_em); |
| 7269 | if (err) { |
| 7270 | free_extent_map(em); |
| 7271 | return ERR_PTR(err); |
| 7272 | } |
| 7273 | return em; |
| 7274 | } |
| 7275 | |
Filipe Manana | 5f9a8a5 | 2016-05-12 13:53:36 +0100 | [diff] [blame] | 7276 | static struct extent_map *btrfs_create_dio_extent(struct inode *inode, |
| 7277 | const u64 start, |
| 7278 | const u64 len, |
| 7279 | const u64 orig_start, |
| 7280 | const u64 block_start, |
| 7281 | const u64 block_len, |
| 7282 | const u64 orig_block_len, |
| 7283 | const u64 ram_bytes, |
| 7284 | const int type) |
| 7285 | { |
| 7286 | struct extent_map *em = NULL; |
| 7287 | int ret; |
| 7288 | |
Filipe Manana | 5f9a8a5 | 2016-05-12 13:53:36 +0100 | [diff] [blame] | 7289 | if (type != BTRFS_ORDERED_NOCOW) { |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 7290 | em = create_io_em(inode, start, len, orig_start, |
| 7291 | block_start, block_len, orig_block_len, |
| 7292 | ram_bytes, |
| 7293 | BTRFS_COMPRESS_NONE, /* compress_type */ |
| 7294 | type); |
Filipe Manana | 5f9a8a5 | 2016-05-12 13:53:36 +0100 | [diff] [blame] | 7295 | if (IS_ERR(em)) |
| 7296 | goto out; |
| 7297 | } |
| 7298 | ret = btrfs_add_ordered_extent_dio(inode, start, block_start, |
| 7299 | len, block_len, type); |
| 7300 | if (ret) { |
| 7301 | if (em) { |
| 7302 | free_extent_map(em); |
Nikolay Borisov | dcdbc05 | 2017-02-20 13:50:45 +0200 | [diff] [blame] | 7303 | btrfs_drop_extent_cache(BTRFS_I(inode), start, |
Filipe Manana | 5f9a8a5 | 2016-05-12 13:53:36 +0100 | [diff] [blame] | 7304 | start + len - 1, 0); |
| 7305 | } |
| 7306 | em = ERR_PTR(ret); |
| 7307 | } |
| 7308 | out: |
Filipe Manana | 5f9a8a5 | 2016-05-12 13:53:36 +0100 | [diff] [blame] | 7309 | |
| 7310 | return em; |
| 7311 | } |
| 7312 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7313 | static struct extent_map *btrfs_new_extent_direct(struct inode *inode, |
| 7314 | u64 start, u64 len) |
| 7315 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7316 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7317 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 7318 | struct extent_map *em; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7319 | struct btrfs_key ins; |
| 7320 | u64 alloc_hint; |
| 7321 | int ret; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7322 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7323 | alloc_hint = get_extent_allocation_hint(inode, start, len); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7324 | ret = btrfs_reserve_extent(root, len, len, fs_info->sectorsize, |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 7325 | 0, alloc_hint, &ins, 1, 1); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 7326 | if (ret) |
| 7327 | return ERR_PTR(ret); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7328 | |
Filipe Manana | 5f9a8a5 | 2016-05-12 13:53:36 +0100 | [diff] [blame] | 7329 | em = btrfs_create_dio_extent(inode, start, ins.offset, start, |
| 7330 | ins.objectid, ins.offset, ins.offset, |
Liu Bo | 6288d6e | 2017-02-21 12:12:58 -0800 | [diff] [blame] | 7331 | ins.offset, BTRFS_ORDERED_REGULAR); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7332 | btrfs_dec_block_group_reservations(fs_info, ins.objectid); |
Filipe Manana | 5f9a8a5 | 2016-05-12 13:53:36 +0100 | [diff] [blame] | 7333 | if (IS_ERR(em)) |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 7334 | btrfs_free_reserved_extent(fs_info, ins.objectid, |
| 7335 | ins.offset, 1); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 7336 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7337 | return em; |
| 7338 | } |
| 7339 | |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7340 | /* |
| 7341 | * returns 1 when the nocow is safe, < 1 on error, 0 if the |
| 7342 | * block must be cow'd |
| 7343 | */ |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 7344 | noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len, |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 7345 | u64 *orig_start, u64 *orig_block_len, |
| 7346 | u64 *ram_bytes) |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7347 | { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 7348 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7349 | struct btrfs_path *path; |
| 7350 | int ret; |
| 7351 | struct extent_buffer *leaf; |
| 7352 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Miao Xie | 7b2b708 | 2014-02-27 13:58:05 +0800 | [diff] [blame] | 7353 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7354 | struct btrfs_file_extent_item *fi; |
| 7355 | struct btrfs_key key; |
| 7356 | u64 disk_bytenr; |
| 7357 | u64 backref_offset; |
| 7358 | u64 extent_end; |
| 7359 | u64 num_bytes; |
| 7360 | int slot; |
| 7361 | int found_type; |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 7362 | bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW); |
Miao Xie | e77751a | 2013-12-27 21:11:50 +0800 | [diff] [blame] | 7363 | |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7364 | path = btrfs_alloc_path(); |
| 7365 | if (!path) |
| 7366 | return -ENOMEM; |
| 7367 | |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 7368 | ret = btrfs_lookup_file_extent(NULL, root, path, |
| 7369 | btrfs_ino(BTRFS_I(inode)), offset, 0); |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7370 | if (ret < 0) |
| 7371 | goto out; |
| 7372 | |
| 7373 | slot = path->slots[0]; |
| 7374 | if (ret == 1) { |
| 7375 | if (slot == 0) { |
| 7376 | /* can't find the item, must cow */ |
| 7377 | ret = 0; |
| 7378 | goto out; |
| 7379 | } |
| 7380 | slot--; |
| 7381 | } |
| 7382 | ret = 0; |
| 7383 | leaf = path->nodes[0]; |
| 7384 | btrfs_item_key_to_cpu(leaf, &key, slot); |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 7385 | if (key.objectid != btrfs_ino(BTRFS_I(inode)) || |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7386 | key.type != BTRFS_EXTENT_DATA_KEY) { |
| 7387 | /* not our file or wrong item type, must cow */ |
| 7388 | goto out; |
| 7389 | } |
| 7390 | |
| 7391 | if (key.offset > offset) { |
| 7392 | /* Wrong offset, must cow */ |
| 7393 | goto out; |
| 7394 | } |
| 7395 | |
| 7396 | fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item); |
| 7397 | found_type = btrfs_file_extent_type(leaf, fi); |
| 7398 | if (found_type != BTRFS_FILE_EXTENT_REG && |
| 7399 | found_type != BTRFS_FILE_EXTENT_PREALLOC) { |
| 7400 | /* not a regular extent, must cow */ |
| 7401 | goto out; |
| 7402 | } |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 7403 | |
| 7404 | if (!nocow && found_type == BTRFS_FILE_EXTENT_REG) |
| 7405 | goto out; |
| 7406 | |
Miao Xie | e77751a | 2013-12-27 21:11:50 +0800 | [diff] [blame] | 7407 | extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi); |
| 7408 | if (extent_end <= offset) |
| 7409 | goto out; |
| 7410 | |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7411 | disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 7412 | if (disk_bytenr == 0) |
| 7413 | goto out; |
| 7414 | |
| 7415 | if (btrfs_file_extent_compression(leaf, fi) || |
| 7416 | btrfs_file_extent_encryption(leaf, fi) || |
| 7417 | btrfs_file_extent_other_encoding(leaf, fi)) |
| 7418 | goto out; |
| 7419 | |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7420 | backref_offset = btrfs_file_extent_offset(leaf, fi); |
| 7421 | |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 7422 | if (orig_start) { |
| 7423 | *orig_start = key.offset - backref_offset; |
| 7424 | *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi); |
| 7425 | *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi); |
| 7426 | } |
Josef Bacik | eb384b5 | 2013-04-24 16:32:55 -0400 | [diff] [blame] | 7427 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 7428 | if (btrfs_extent_readonly(fs_info, disk_bytenr)) |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7429 | goto out; |
Miao Xie | 7b2b708 | 2014-02-27 13:58:05 +0800 | [diff] [blame] | 7430 | |
| 7431 | num_bytes = min(offset + *len, extent_end) - offset; |
| 7432 | if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) { |
| 7433 | u64 range_end; |
| 7434 | |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 7435 | range_end = round_up(offset + num_bytes, |
| 7436 | root->fs_info->sectorsize) - 1; |
Miao Xie | 7b2b708 | 2014-02-27 13:58:05 +0800 | [diff] [blame] | 7437 | ret = test_range_bit(io_tree, offset, range_end, |
| 7438 | EXTENT_DELALLOC, 0, NULL); |
| 7439 | if (ret) { |
| 7440 | ret = -EAGAIN; |
| 7441 | goto out; |
| 7442 | } |
| 7443 | } |
| 7444 | |
Josef Bacik | 1bda19e | 2013-10-18 12:10:36 -0400 | [diff] [blame] | 7445 | btrfs_release_path(path); |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7446 | |
| 7447 | /* |
| 7448 | * look for other files referencing this extent, if we |
| 7449 | * find any we must cow |
| 7450 | */ |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 7451 | |
Liu Bo | e4c3b2d | 2017-01-30 12:25:28 -0800 | [diff] [blame] | 7452 | ret = btrfs_cross_ref_exist(root, btrfs_ino(BTRFS_I(inode)), |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 7453 | key.offset - backref_offset, disk_bytenr); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 7454 | if (ret) { |
| 7455 | ret = 0; |
| 7456 | goto out; |
| 7457 | } |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7458 | |
| 7459 | /* |
| 7460 | * adjust disk_bytenr and num_bytes to cover just the bytes |
| 7461 | * in this extent we are about to write. If there |
| 7462 | * are any csums in that range we have to cow in order |
| 7463 | * to keep the csums correct |
| 7464 | */ |
| 7465 | disk_bytenr += backref_offset; |
| 7466 | disk_bytenr += offset - key.offset; |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 7467 | if (csum_exist_in_range(fs_info, disk_bytenr, num_bytes)) |
| 7468 | goto out; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7469 | /* |
| 7470 | * all of the above have passed, it is safe to overwrite this extent |
| 7471 | * without cow |
| 7472 | */ |
Josef Bacik | eb384b5 | 2013-04-24 16:32:55 -0400 | [diff] [blame] | 7473 | *len = num_bytes; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7474 | ret = 1; |
| 7475 | out: |
| 7476 | btrfs_free_path(path); |
| 7477 | return ret; |
| 7478 | } |
| 7479 | |
Alex Gartrell | fc4adbf | 2014-05-20 13:07:56 -0700 | [diff] [blame] | 7480 | bool btrfs_page_exists_in_range(struct inode *inode, loff_t start, loff_t end) |
| 7481 | { |
| 7482 | struct radix_tree_root *root = &inode->i_mapping->page_tree; |
| 7483 | int found = false; |
| 7484 | void **pagep = NULL; |
| 7485 | struct page *page = NULL; |
| 7486 | int start_idx; |
| 7487 | int end_idx; |
| 7488 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 7489 | start_idx = start >> PAGE_SHIFT; |
Alex Gartrell | fc4adbf | 2014-05-20 13:07:56 -0700 | [diff] [blame] | 7490 | |
| 7491 | /* |
| 7492 | * end is the last byte in the last page. end == start is legal |
| 7493 | */ |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 7494 | end_idx = end >> PAGE_SHIFT; |
Alex Gartrell | fc4adbf | 2014-05-20 13:07:56 -0700 | [diff] [blame] | 7495 | |
| 7496 | rcu_read_lock(); |
| 7497 | |
| 7498 | /* Most of the code in this while loop is lifted from |
| 7499 | * find_get_page. It's been modified to begin searching from a |
| 7500 | * page and return just the first page found in that range. If the |
| 7501 | * found idx is less than or equal to the end idx then we know that |
| 7502 | * a page exists. If no pages are found or if those pages are |
| 7503 | * outside of the range then we're fine (yay!) */ |
| 7504 | while (page == NULL && |
| 7505 | radix_tree_gang_lookup_slot(root, &pagep, NULL, start_idx, 1)) { |
| 7506 | page = radix_tree_deref_slot(pagep); |
| 7507 | if (unlikely(!page)) |
| 7508 | break; |
| 7509 | |
| 7510 | if (radix_tree_exception(page)) { |
Filipe Manana | 809f901 | 2014-06-05 13:22:25 +0100 | [diff] [blame] | 7511 | if (radix_tree_deref_retry(page)) { |
| 7512 | page = NULL; |
Alex Gartrell | fc4adbf | 2014-05-20 13:07:56 -0700 | [diff] [blame] | 7513 | continue; |
Filipe Manana | 809f901 | 2014-06-05 13:22:25 +0100 | [diff] [blame] | 7514 | } |
Alex Gartrell | fc4adbf | 2014-05-20 13:07:56 -0700 | [diff] [blame] | 7515 | /* |
| 7516 | * Otherwise, shmem/tmpfs must be storing a swap entry |
| 7517 | * here as an exceptional entry: so return it without |
| 7518 | * attempting to raise page count. |
| 7519 | */ |
Filipe Manana | 6fdef6d | 2014-06-05 13:22:26 +0100 | [diff] [blame] | 7520 | page = NULL; |
Alex Gartrell | fc4adbf | 2014-05-20 13:07:56 -0700 | [diff] [blame] | 7521 | break; /* TODO: Is this relevant for this use case? */ |
| 7522 | } |
| 7523 | |
Filipe Manana | 9140515 | 2014-06-05 13:22:24 +0100 | [diff] [blame] | 7524 | if (!page_cache_get_speculative(page)) { |
| 7525 | page = NULL; |
Alex Gartrell | fc4adbf | 2014-05-20 13:07:56 -0700 | [diff] [blame] | 7526 | continue; |
Filipe Manana | 9140515 | 2014-06-05 13:22:24 +0100 | [diff] [blame] | 7527 | } |
Alex Gartrell | fc4adbf | 2014-05-20 13:07:56 -0700 | [diff] [blame] | 7528 | |
| 7529 | /* |
| 7530 | * Has the page moved? |
| 7531 | * This is part of the lockless pagecache protocol. See |
| 7532 | * include/linux/pagemap.h for details. |
| 7533 | */ |
| 7534 | if (unlikely(page != *pagep)) { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 7535 | put_page(page); |
Alex Gartrell | fc4adbf | 2014-05-20 13:07:56 -0700 | [diff] [blame] | 7536 | page = NULL; |
| 7537 | } |
| 7538 | } |
| 7539 | |
| 7540 | if (page) { |
| 7541 | if (page->index <= end_idx) |
| 7542 | found = true; |
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 | } |
| 7545 | |
| 7546 | rcu_read_unlock(); |
| 7547 | return found; |
| 7548 | } |
| 7549 | |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7550 | static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend, |
| 7551 | struct extent_state **cached_state, int writing) |
| 7552 | { |
| 7553 | struct btrfs_ordered_extent *ordered; |
| 7554 | int ret = 0; |
| 7555 | |
| 7556 | while (1) { |
| 7557 | lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend, |
David Sterba | ff13db4 | 2015-12-03 14:30:40 +0100 | [diff] [blame] | 7558 | cached_state); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7559 | /* |
| 7560 | * 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] | 7561 | * 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] | 7562 | * extents in this range. |
| 7563 | */ |
Nikolay Borisov | a776c6f | 2017-02-20 13:50:49 +0200 | [diff] [blame] | 7564 | ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), lockstart, |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7565 | lockend - lockstart + 1); |
| 7566 | |
| 7567 | /* |
| 7568 | * We need to make sure there are no buffered pages in this |
| 7569 | * range either, we could have raced between the invalidate in |
| 7570 | * generic_file_direct_write and locking the extent. The |
| 7571 | * invalidate needs to happen so that reads after a write do not |
| 7572 | * get stale data. |
| 7573 | */ |
Alex Gartrell | fc4adbf | 2014-05-20 13:07:56 -0700 | [diff] [blame] | 7574 | if (!ordered && |
| 7575 | (!writing || |
| 7576 | !btrfs_page_exists_in_range(inode, lockstart, lockend))) |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7577 | break; |
| 7578 | |
| 7579 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend, |
| 7580 | cached_state, GFP_NOFS); |
| 7581 | |
| 7582 | if (ordered) { |
Filipe Manana | ade7702 | 2016-02-18 14:28:55 +0000 | [diff] [blame] | 7583 | /* |
| 7584 | * If we are doing a DIO read and the ordered extent we |
| 7585 | * found is for a buffered write, we can not wait for it |
| 7586 | * to complete and retry, because if we do so we can |
| 7587 | * deadlock with concurrent buffered writes on page |
| 7588 | * locks. This happens only if our DIO read covers more |
| 7589 | * than one extent map, if at this point has already |
| 7590 | * created an ordered extent for a previous extent map |
| 7591 | * and locked its range in the inode's io tree, and a |
| 7592 | * concurrent write against that previous extent map's |
| 7593 | * range and this range started (we unlock the ranges |
| 7594 | * in the io tree only when the bios complete and |
| 7595 | * buffered writes always lock pages before attempting |
| 7596 | * to lock range in the io tree). |
| 7597 | */ |
| 7598 | if (writing || |
| 7599 | test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags)) |
| 7600 | btrfs_start_ordered_extent(inode, ordered, 1); |
| 7601 | else |
| 7602 | ret = -ENOTBLK; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7603 | btrfs_put_ordered_extent(ordered); |
| 7604 | } else { |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7605 | /* |
Filipe Manana | b850ae1 | 2015-12-08 16:23:16 +0000 | [diff] [blame] | 7606 | * We could trigger writeback for this range (and wait |
| 7607 | * for it to complete) and then invalidate the pages for |
| 7608 | * this range (through invalidate_inode_pages2_range()), |
| 7609 | * but that can lead us to a deadlock with a concurrent |
| 7610 | * call to readpages() (a buffered read or a defrag call |
| 7611 | * triggered a readahead) on a page lock due to an |
| 7612 | * ordered dio extent we created before but did not have |
| 7613 | * yet a corresponding bio submitted (whence it can not |
| 7614 | * complete), which makes readpages() wait for that |
| 7615 | * ordered extent to complete while holding a lock on |
| 7616 | * that page. |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7617 | */ |
Filipe Manana | b850ae1 | 2015-12-08 16:23:16 +0000 | [diff] [blame] | 7618 | ret = -ENOTBLK; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7619 | } |
| 7620 | |
Filipe Manana | ade7702 | 2016-02-18 14:28:55 +0000 | [diff] [blame] | 7621 | if (ret) |
| 7622 | break; |
| 7623 | |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7624 | cond_resched(); |
| 7625 | } |
| 7626 | |
| 7627 | return ret; |
| 7628 | } |
| 7629 | |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 7630 | /* The callers of this must take lock_extent() */ |
| 7631 | static struct extent_map *create_io_em(struct inode *inode, u64 start, u64 len, |
| 7632 | u64 orig_start, u64 block_start, |
| 7633 | u64 block_len, u64 orig_block_len, |
| 7634 | u64 ram_bytes, int compress_type, |
| 7635 | int type) |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7636 | { |
| 7637 | struct extent_map_tree *em_tree; |
| 7638 | struct extent_map *em; |
| 7639 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 7640 | int ret; |
| 7641 | |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 7642 | ASSERT(type == BTRFS_ORDERED_PREALLOC || |
| 7643 | type == BTRFS_ORDERED_COMPRESSED || |
| 7644 | type == BTRFS_ORDERED_NOCOW || |
Liu Bo | 1af4a0a | 2017-02-13 15:35:09 -0800 | [diff] [blame] | 7645 | type == BTRFS_ORDERED_REGULAR); |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 7646 | |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7647 | em_tree = &BTRFS_I(inode)->extent_tree; |
| 7648 | em = alloc_extent_map(); |
| 7649 | if (!em) |
| 7650 | return ERR_PTR(-ENOMEM); |
| 7651 | |
| 7652 | em->start = start; |
| 7653 | em->orig_start = orig_start; |
| 7654 | em->len = len; |
| 7655 | em->block_len = block_len; |
| 7656 | em->block_start = block_start; |
| 7657 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 7658 | em->orig_block_len = orig_block_len; |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 7659 | em->ram_bytes = ram_bytes; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 7660 | em->generation = -1; |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7661 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
Liu Bo | 1af4a0a | 2017-02-13 15:35:09 -0800 | [diff] [blame] | 7662 | if (type == BTRFS_ORDERED_PREALLOC) { |
Josef Bacik | b11e234 | 2012-12-03 10:58:15 -0500 | [diff] [blame] | 7663 | set_bit(EXTENT_FLAG_FILLING, &em->flags); |
Liu Bo | 1af4a0a | 2017-02-13 15:35:09 -0800 | [diff] [blame] | 7664 | } else if (type == BTRFS_ORDERED_COMPRESSED) { |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 7665 | set_bit(EXTENT_FLAG_COMPRESSED, &em->flags); |
| 7666 | em->compress_type = compress_type; |
| 7667 | } |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7668 | |
| 7669 | do { |
Nikolay Borisov | dcdbc05 | 2017-02-20 13:50:45 +0200 | [diff] [blame] | 7670 | btrfs_drop_extent_cache(BTRFS_I(inode), em->start, |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7671 | em->start + em->len - 1, 0); |
| 7672 | write_lock(&em_tree->lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 7673 | ret = add_extent_mapping(em_tree, em, 1); |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7674 | write_unlock(&em_tree->lock); |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 7675 | /* |
| 7676 | * The caller has taken lock_extent(), who could race with us |
| 7677 | * to add em? |
| 7678 | */ |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7679 | } while (ret == -EEXIST); |
| 7680 | |
| 7681 | if (ret) { |
| 7682 | free_extent_map(em); |
| 7683 | return ERR_PTR(ret); |
| 7684 | } |
| 7685 | |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 7686 | /* 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] | 7687 | return em; |
| 7688 | } |
| 7689 | |
Filipe Manana | 9c9464c | 2015-11-04 09:52:04 +0000 | [diff] [blame] | 7690 | static void adjust_dio_outstanding_extents(struct inode *inode, |
| 7691 | struct btrfs_dio_data *dio_data, |
| 7692 | const u64 len) |
| 7693 | { |
David Sterba | 823bb20 | 2017-01-04 11:09:51 +0100 | [diff] [blame] | 7694 | unsigned num_extents = count_max_extents(len); |
Filipe Manana | 9c9464c | 2015-11-04 09:52:04 +0000 | [diff] [blame] | 7695 | |
Filipe Manana | 9c9464c | 2015-11-04 09:52:04 +0000 | [diff] [blame] | 7696 | /* |
| 7697 | * If we have an outstanding_extents count still set then we're |
| 7698 | * within our reservation, otherwise we need to adjust our inode |
| 7699 | * counter appropriately. |
| 7700 | */ |
Liu Bo | c293166 | 2016-12-22 17:13:54 -0800 | [diff] [blame] | 7701 | if (dio_data->outstanding_extents >= num_extents) { |
Filipe Manana | 9c9464c | 2015-11-04 09:52:04 +0000 | [diff] [blame] | 7702 | dio_data->outstanding_extents -= num_extents; |
| 7703 | } else { |
Liu Bo | c293166 | 2016-12-22 17:13:54 -0800 | [diff] [blame] | 7704 | /* |
| 7705 | * If dio write length has been split due to no large enough |
| 7706 | * contiguous space, we need to compensate our inode counter |
| 7707 | * appropriately. |
| 7708 | */ |
| 7709 | u64 num_needed = num_extents - dio_data->outstanding_extents; |
| 7710 | |
Filipe Manana | 9c9464c | 2015-11-04 09:52:04 +0000 | [diff] [blame] | 7711 | spin_lock(&BTRFS_I(inode)->lock); |
Liu Bo | c293166 | 2016-12-22 17:13:54 -0800 | [diff] [blame] | 7712 | BTRFS_I(inode)->outstanding_extents += num_needed; |
Filipe Manana | 9c9464c | 2015-11-04 09:52:04 +0000 | [diff] [blame] | 7713 | spin_unlock(&BTRFS_I(inode)->lock); |
| 7714 | } |
| 7715 | } |
| 7716 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7717 | static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock, |
| 7718 | struct buffer_head *bh_result, int create) |
| 7719 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7720 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7721 | struct extent_map *em; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7722 | struct extent_state *cached_state = NULL; |
chandan | 50745b0 | 2015-08-28 21:10:13 +0530 | [diff] [blame] | 7723 | struct btrfs_dio_data *dio_data = NULL; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7724 | u64 start = iblock << inode->i_blkbits; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7725 | u64 lockstart, lockend; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7726 | u64 len = bh_result->b_size; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7727 | int unlock_bits = EXTENT_LOCKED; |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 7728 | int ret = 0; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7729 | |
Miao Xie | 172a504 | 2013-02-21 02:48:22 -0700 | [diff] [blame] | 7730 | if (create) |
Josef Bacik | 3266789 | 2015-02-11 15:08:58 -0500 | [diff] [blame] | 7731 | unlock_bits |= EXTENT_DIRTY; |
Miao Xie | 172a504 | 2013-02-21 02:48:22 -0700 | [diff] [blame] | 7732 | else |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7733 | len = min_t(u64, len, fs_info->sectorsize); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7734 | |
Josef Bacik | c329861 | 2012-08-03 16:49:19 -0400 | [diff] [blame] | 7735 | lockstart = start; |
| 7736 | lockend = start + len - 1; |
| 7737 | |
Josef Bacik | e1cbbfa | 2015-03-17 10:52:28 -0400 | [diff] [blame] | 7738 | if (current->journal_info) { |
| 7739 | /* |
| 7740 | * 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] | 7741 | * 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] | 7742 | * confused. |
| 7743 | */ |
chandan | 50745b0 | 2015-08-28 21:10:13 +0530 | [diff] [blame] | 7744 | dio_data = current->journal_info; |
Josef Bacik | e1cbbfa | 2015-03-17 10:52:28 -0400 | [diff] [blame] | 7745 | current->journal_info = NULL; |
| 7746 | } |
| 7747 | |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7748 | /* |
| 7749 | * If this errors out it's because we couldn't invalidate pagecache for |
| 7750 | * this range and we need to fallback to buffered. |
| 7751 | */ |
Filipe Manana | 9c9464c | 2015-11-04 09:52:04 +0000 | [diff] [blame] | 7752 | if (lock_extent_direct(inode, lockstart, lockend, &cached_state, |
| 7753 | create)) { |
| 7754 | ret = -ENOTBLK; |
| 7755 | goto err; |
| 7756 | } |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7757 | |
Nikolay Borisov | fc4f21b | 2017-02-20 13:51:06 +0200 | [diff] [blame] | 7758 | em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len, 0); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7759 | if (IS_ERR(em)) { |
| 7760 | ret = PTR_ERR(em); |
| 7761 | goto unlock_err; |
| 7762 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7763 | |
| 7764 | /* |
| 7765 | * Ok for INLINE and COMPRESSED extents we need to fallback on buffered |
| 7766 | * io. INLINE is special, and we could probably kludge it in here, but |
| 7767 | * it's still buffered so for safety lets just fall back to the generic |
| 7768 | * buffered path. |
| 7769 | * |
| 7770 | * For COMPRESSED we _have_ to read the entire extent in so we can |
| 7771 | * decompress it, so there will be buffering required no matter what we |
| 7772 | * do, so go ahead and fallback to buffered. |
| 7773 | * |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 7774 | * 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] | 7775 | * to buffered IO. Don't blame me, this is the price we pay for using |
| 7776 | * the generic code. |
| 7777 | */ |
| 7778 | if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) || |
| 7779 | em->block_start == EXTENT_MAP_INLINE) { |
| 7780 | free_extent_map(em); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7781 | ret = -ENOTBLK; |
| 7782 | goto unlock_err; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7783 | } |
| 7784 | |
| 7785 | /* Just a good old fashioned hole, return */ |
| 7786 | if (!create && (em->block_start == EXTENT_MAP_HOLE || |
| 7787 | test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) { |
| 7788 | free_extent_map(em); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7789 | goto unlock_err; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7790 | } |
| 7791 | |
| 7792 | /* |
| 7793 | * We don't allocate a new extent in the following cases |
| 7794 | * |
| 7795 | * 1) The inode is marked as NODATACOW. In this case we'll just use the |
| 7796 | * existing extent. |
| 7797 | * 2) The extent is marked as PREALLOC. We're good to go here and can |
| 7798 | * just use the extent. |
| 7799 | * |
| 7800 | */ |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7801 | if (!create) { |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7802 | len = min(len, em->len - (start - em->start)); |
| 7803 | lockstart = start + len; |
| 7804 | goto unlock; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7805 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7806 | |
| 7807 | if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) || |
| 7808 | ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) && |
| 7809 | em->block_start != EXTENT_MAP_HOLE)) { |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7810 | int type; |
Josef Bacik | eb384b5 | 2013-04-24 16:32:55 -0400 | [diff] [blame] | 7811 | u64 block_start, orig_start, orig_block_len, ram_bytes; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7812 | |
| 7813 | if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) |
| 7814 | type = BTRFS_ORDERED_PREALLOC; |
| 7815 | else |
| 7816 | type = BTRFS_ORDERED_NOCOW; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7817 | len = min(len, em->len - (start - em->start)); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7818 | block_start = em->block_start + (start - em->start); |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7819 | |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 7820 | if (can_nocow_extent(inode, start, &len, &orig_start, |
Filipe Manana | f78c436 | 2016-05-09 13:15:41 +0100 | [diff] [blame] | 7821 | &orig_block_len, &ram_bytes) == 1 && |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7822 | btrfs_inc_nocow_writers(fs_info, block_start)) { |
Filipe Manana | 5f9a8a5 | 2016-05-12 13:53:36 +0100 | [diff] [blame] | 7823 | struct extent_map *em2; |
Filipe Manana | 0b90191 | 2016-05-09 13:15:27 +0100 | [diff] [blame] | 7824 | |
Filipe Manana | 5f9a8a5 | 2016-05-12 13:53:36 +0100 | [diff] [blame] | 7825 | em2 = btrfs_create_dio_extent(inode, start, len, |
| 7826 | orig_start, block_start, |
| 7827 | len, orig_block_len, |
| 7828 | ram_bytes, type); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7829 | btrfs_dec_nocow_writers(fs_info, block_start); |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7830 | if (type == BTRFS_ORDERED_PREALLOC) { |
| 7831 | free_extent_map(em); |
Filipe Manana | 5f9a8a5 | 2016-05-12 13:53:36 +0100 | [diff] [blame] | 7832 | em = em2; |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7833 | } |
Filipe Manana | 5f9a8a5 | 2016-05-12 13:53:36 +0100 | [diff] [blame] | 7834 | if (em2 && IS_ERR(em2)) { |
| 7835 | ret = PTR_ERR(em2); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7836 | goto unlock_err; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7837 | } |
Wang Xiaoguang | 1851309 | 2016-07-25 15:51:40 +0800 | [diff] [blame] | 7838 | /* |
| 7839 | * For inode marked NODATACOW or extent marked PREALLOC, |
| 7840 | * use the existing or preallocated extent, so does not |
| 7841 | * need to adjust btrfs_space_info's bytes_may_use. |
| 7842 | */ |
| 7843 | btrfs_free_reserved_data_space_noquota(inode, |
| 7844 | start, len); |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7845 | goto unlock; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7846 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7847 | } |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 7848 | |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7849 | /* |
| 7850 | * this will cow the extent, reset the len in case we changed |
| 7851 | * it above |
| 7852 | */ |
| 7853 | len = bh_result->b_size; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 7854 | free_extent_map(em); |
| 7855 | em = btrfs_new_extent_direct(inode, start, len); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7856 | if (IS_ERR(em)) { |
| 7857 | ret = PTR_ERR(em); |
| 7858 | goto unlock_err; |
| 7859 | } |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7860 | len = min(len, em->len - (start - em->start)); |
| 7861 | unlock: |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7862 | bh_result->b_blocknr = (em->block_start + (start - em->start)) >> |
| 7863 | inode->i_blkbits; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7864 | bh_result->b_size = len; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7865 | bh_result->b_bdev = em->bdev; |
| 7866 | set_buffer_mapped(bh_result); |
Josef Bacik | c3473e8 | 2012-06-19 10:59:00 -0400 | [diff] [blame] | 7867 | if (create) { |
| 7868 | if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) |
| 7869 | set_buffer_new(bh_result); |
| 7870 | |
| 7871 | /* |
| 7872 | * Need to update the i_size under the extent lock so buffered |
| 7873 | * readers will get the updated i_size when we unlock. |
| 7874 | */ |
Liu Bo | 4aaedfb | 2016-12-14 22:36:05 -0800 | [diff] [blame] | 7875 | if (!dio_data->overwrite && start + len > i_size_read(inode)) |
Josef Bacik | c3473e8 | 2012-06-19 10:59:00 -0400 | [diff] [blame] | 7876 | i_size_write(inode, start + len); |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 7877 | |
Filipe Manana | 9c9464c | 2015-11-04 09:52:04 +0000 | [diff] [blame] | 7878 | adjust_dio_outstanding_extents(inode, dio_data, len); |
chandan | 50745b0 | 2015-08-28 21:10:13 +0530 | [diff] [blame] | 7879 | WARN_ON(dio_data->reserve < len); |
| 7880 | dio_data->reserve -= len; |
Filipe Manana | f28a492 | 2015-12-08 19:23:20 +0000 | [diff] [blame] | 7881 | dio_data->unsubmitted_oe_range_end = start + len; |
chandan | 50745b0 | 2015-08-28 21:10:13 +0530 | [diff] [blame] | 7882 | current->journal_info = dio_data; |
Josef Bacik | c3473e8 | 2012-06-19 10:59:00 -0400 | [diff] [blame] | 7883 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7884 | |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7885 | /* |
| 7886 | * In the case of write we need to clear and unlock the entire range, |
| 7887 | * in the case of read we need to unlock only the end area that we |
| 7888 | * aren't using if there is any left over space. |
| 7889 | */ |
Liu Bo | 24c03fa | 2012-08-22 20:10:38 -0600 | [diff] [blame] | 7890 | if (lockstart < lockend) { |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 7891 | clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, |
| 7892 | lockend, unlock_bits, 1, 0, |
| 7893 | &cached_state, GFP_NOFS); |
Liu Bo | 24c03fa | 2012-08-22 20:10:38 -0600 | [diff] [blame] | 7894 | } else { |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7895 | free_extent_state(cached_state); |
Liu Bo | 24c03fa | 2012-08-22 20:10:38 -0600 | [diff] [blame] | 7896 | } |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7897 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7898 | free_extent_map(em); |
| 7899 | |
| 7900 | return 0; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7901 | |
| 7902 | unlock_err: |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7903 | clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend, |
| 7904 | unlock_bits, 1, 0, &cached_state, GFP_NOFS); |
Filipe Manana | 9c9464c | 2015-11-04 09:52:04 +0000 | [diff] [blame] | 7905 | err: |
chandan | 50745b0 | 2015-08-28 21:10:13 +0530 | [diff] [blame] | 7906 | if (dio_data) |
| 7907 | current->journal_info = dio_data; |
Filipe Manana | 9c9464c | 2015-11-04 09:52:04 +0000 | [diff] [blame] | 7908 | /* |
| 7909 | * Compensate the delalloc release we do in btrfs_direct_IO() when we |
| 7910 | * write less data then expected, so that we don't underflow our inode's |
| 7911 | * outstanding extents counter. |
| 7912 | */ |
| 7913 | if (create && dio_data) |
| 7914 | adjust_dio_outstanding_extents(inode, dio_data, len); |
| 7915 | |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7916 | return ret; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7917 | } |
| 7918 | |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7919 | static inline int submit_dio_repair_bio(struct inode *inode, struct bio *bio, |
Mike Christie | 81a75f6 | 2016-06-05 14:31:54 -0500 | [diff] [blame] | 7920 | int mirror_num) |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7921 | { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 7922 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7923 | int ret; |
| 7924 | |
Mike Christie | 37226b2 | 2016-06-05 14:31:52 -0500 | [diff] [blame] | 7925 | BUG_ON(bio_op(bio) == REQ_OP_WRITE); |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7926 | |
| 7927 | bio_get(bio); |
| 7928 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 7929 | 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] | 7930 | if (ret) |
| 7931 | goto err; |
| 7932 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 7933 | ret = btrfs_map_bio(fs_info, bio, mirror_num, 0); |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7934 | err: |
| 7935 | bio_put(bio); |
| 7936 | return ret; |
| 7937 | } |
| 7938 | |
| 7939 | static int btrfs_check_dio_repairable(struct inode *inode, |
| 7940 | struct bio *failed_bio, |
| 7941 | struct io_failure_record *failrec, |
| 7942 | int failed_mirror) |
| 7943 | { |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 7944 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7945 | int num_copies; |
| 7946 | |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 7947 | num_copies = btrfs_num_copies(fs_info, failrec->logical, failrec->len); |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7948 | if (num_copies == 1) { |
| 7949 | /* |
| 7950 | * we only have a single copy of the data, so don't bother with |
| 7951 | * all the retry and error correction code that follows. no |
| 7952 | * matter what the error is, it is very likely to persist. |
| 7953 | */ |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 7954 | btrfs_debug(fs_info, |
| 7955 | "Check DIO Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d", |
| 7956 | num_copies, failrec->this_mirror, failed_mirror); |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7957 | return 0; |
| 7958 | } |
| 7959 | |
| 7960 | failrec->failed_mirror = failed_mirror; |
| 7961 | failrec->this_mirror++; |
| 7962 | if (failrec->this_mirror == failed_mirror) |
| 7963 | failrec->this_mirror++; |
| 7964 | |
| 7965 | if (failrec->this_mirror > num_copies) { |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 7966 | btrfs_debug(fs_info, |
| 7967 | "Check DIO Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d", |
| 7968 | num_copies, failrec->this_mirror, failed_mirror); |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7969 | return 0; |
| 7970 | } |
| 7971 | |
| 7972 | return 1; |
| 7973 | } |
| 7974 | |
| 7975 | static int dio_read_error(struct inode *inode, struct bio *failed_bio, |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 7976 | struct page *page, unsigned int pgoff, |
| 7977 | u64 start, u64 end, int failed_mirror, |
| 7978 | bio_end_io_t *repair_endio, void *repair_arg) |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7979 | { |
| 7980 | struct io_failure_record *failrec; |
| 7981 | struct bio *bio; |
| 7982 | int isector; |
Christoph Hellwig | 70fd761 | 2016-11-01 07:40:10 -0600 | [diff] [blame] | 7983 | int read_mode = 0; |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7984 | int ret; |
| 7985 | |
Mike Christie | 37226b2 | 2016-06-05 14:31:52 -0500 | [diff] [blame] | 7986 | BUG_ON(bio_op(failed_bio) == REQ_OP_WRITE); |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7987 | |
| 7988 | ret = btrfs_get_io_failure_record(inode, start, end, &failrec); |
| 7989 | if (ret) |
| 7990 | return ret; |
| 7991 | |
| 7992 | ret = btrfs_check_dio_repairable(inode, failed_bio, failrec, |
| 7993 | failed_mirror); |
| 7994 | if (!ret) { |
Nikolay Borisov | 4ac1f4a | 2017-02-20 13:50:52 +0200 | [diff] [blame] | 7995 | free_io_failure(BTRFS_I(inode), failrec); |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7996 | return -EIO; |
| 7997 | } |
| 7998 | |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 7999 | if ((failed_bio->bi_vcnt > 1) |
| 8000 | || (failed_bio->bi_io_vec->bv_len |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 8001 | > btrfs_inode_sectorsize(inode))) |
Christoph Hellwig | 70fd761 | 2016-11-01 07:40:10 -0600 | [diff] [blame] | 8002 | read_mode |= REQ_FAILFAST_DEV; |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8003 | |
| 8004 | isector = start - btrfs_io_bio(failed_bio)->logical; |
| 8005 | isector >>= inode->i_sb->s_blocksize_bits; |
| 8006 | bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page, |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 8007 | pgoff, isector, repair_endio, repair_arg); |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8008 | if (!bio) { |
Nikolay Borisov | 4ac1f4a | 2017-02-20 13:50:52 +0200 | [diff] [blame] | 8009 | free_io_failure(BTRFS_I(inode), failrec); |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8010 | return -EIO; |
| 8011 | } |
Mike Christie | 37226b2 | 2016-06-05 14:31:52 -0500 | [diff] [blame] | 8012 | bio_set_op_attrs(bio, REQ_OP_READ, read_mode); |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8013 | |
| 8014 | btrfs_debug(BTRFS_I(inode)->root->fs_info, |
| 8015 | "Repair DIO Read Error: submitting new dio read[%#x] to this_mirror=%d, in_validation=%d\n", |
| 8016 | read_mode, failrec->this_mirror, failrec->in_validation); |
| 8017 | |
Mike Christie | 81a75f6 | 2016-06-05 14:31:54 -0500 | [diff] [blame] | 8018 | ret = submit_dio_repair_bio(inode, bio, failrec->this_mirror); |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8019 | if (ret) { |
Nikolay Borisov | 4ac1f4a | 2017-02-20 13:50:52 +0200 | [diff] [blame] | 8020 | free_io_failure(BTRFS_I(inode), failrec); |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8021 | bio_put(bio); |
| 8022 | } |
| 8023 | |
| 8024 | return ret; |
| 8025 | } |
| 8026 | |
| 8027 | struct btrfs_retry_complete { |
| 8028 | struct completion done; |
| 8029 | struct inode *inode; |
| 8030 | u64 start; |
| 8031 | int uptodate; |
| 8032 | }; |
| 8033 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 8034 | static void btrfs_retry_endio_nocsum(struct bio *bio) |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8035 | { |
| 8036 | struct btrfs_retry_complete *done = bio->bi_private; |
| 8037 | struct bio_vec *bvec; |
| 8038 | int i; |
| 8039 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 8040 | if (bio->bi_error) |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8041 | goto end; |
| 8042 | |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 8043 | ASSERT(bio->bi_vcnt == 1); |
Liu Bo | 2e949b0 | 2017-04-05 14:04:19 -0700 | [diff] [blame] | 8044 | ASSERT(bio->bi_io_vec->bv_len == btrfs_inode_sectorsize(done->inode)); |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 8045 | |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8046 | done->uptodate = 1; |
| 8047 | bio_for_each_segment_all(bvec, bio, i) |
Liu Bo | 2e949b0 | 2017-04-05 14:04:19 -0700 | [diff] [blame] | 8048 | clean_io_failure(BTRFS_I(done->inode), done->start, |
| 8049 | bvec->bv_page, 0); |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8050 | end: |
| 8051 | complete(&done->done); |
| 8052 | bio_put(bio); |
| 8053 | } |
| 8054 | |
| 8055 | static int __btrfs_correct_data_nocsum(struct inode *inode, |
| 8056 | struct btrfs_io_bio *io_bio) |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8057 | { |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 8058 | struct btrfs_fs_info *fs_info; |
Kent Overstreet | 2c30c71 | 2013-11-07 12:20:26 -0800 | [diff] [blame] | 8059 | struct bio_vec *bvec; |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8060 | struct btrfs_retry_complete done; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8061 | u64 start; |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 8062 | unsigned int pgoff; |
| 8063 | u32 sectorsize; |
| 8064 | int nr_sectors; |
Kent Overstreet | 2c30c71 | 2013-11-07 12:20:26 -0800 | [diff] [blame] | 8065 | int i; |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 8066 | int ret; |
Miao Xie | dc380ae | 2014-09-12 18:43:55 +0800 | [diff] [blame] | 8067 | |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 8068 | fs_info = BTRFS_I(inode)->root->fs_info; |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 8069 | sectorsize = fs_info->sectorsize; |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 8070 | |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 8071 | start = io_bio->logical; |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8072 | done.inode = inode; |
| 8073 | |
| 8074 | bio_for_each_segment_all(bvec, &io_bio->bio, i) { |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 8075 | nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec->bv_len); |
| 8076 | pgoff = bvec->bv_offset; |
| 8077 | |
| 8078 | next_block_or_try_again: |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8079 | done.uptodate = 0; |
| 8080 | done.start = start; |
| 8081 | init_completion(&done.done); |
| 8082 | |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 8083 | ret = dio_read_error(inode, &io_bio->bio, bvec->bv_page, |
| 8084 | pgoff, start, start + sectorsize - 1, |
| 8085 | io_bio->mirror_num, |
| 8086 | btrfs_retry_endio_nocsum, &done); |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8087 | if (ret) |
| 8088 | return ret; |
| 8089 | |
| 8090 | wait_for_completion(&done.done); |
| 8091 | |
| 8092 | if (!done.uptodate) { |
| 8093 | /* We might have another mirror, so try again */ |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 8094 | goto next_block_or_try_again; |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8095 | } |
| 8096 | |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 8097 | start += sectorsize; |
| 8098 | |
Liu Bo | 97bf5a5 | 2017-04-07 13:11:10 -0700 | [diff] [blame] | 8099 | nr_sectors--; |
| 8100 | if (nr_sectors) { |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 8101 | pgoff += sectorsize; |
Liu Bo | 97bf5a5 | 2017-04-07 13:11:10 -0700 | [diff] [blame] | 8102 | ASSERT(pgoff < PAGE_SIZE); |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 8103 | goto next_block_or_try_again; |
| 8104 | } |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8105 | } |
| 8106 | |
| 8107 | return 0; |
| 8108 | } |
| 8109 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 8110 | static void btrfs_retry_endio(struct bio *bio) |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8111 | { |
| 8112 | struct btrfs_retry_complete *done = bio->bi_private; |
| 8113 | struct btrfs_io_bio *io_bio = btrfs_io_bio(bio); |
| 8114 | struct bio_vec *bvec; |
| 8115 | int uptodate; |
| 8116 | int ret; |
| 8117 | int i; |
| 8118 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 8119 | if (bio->bi_error) |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8120 | goto end; |
| 8121 | |
| 8122 | uptodate = 1; |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 8123 | |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 8124 | ASSERT(bio->bi_vcnt == 1); |
Liu Bo | 2e949b0 | 2017-04-05 14:04:19 -0700 | [diff] [blame] | 8125 | ASSERT(bio->bi_io_vec->bv_len == btrfs_inode_sectorsize(done->inode)); |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 8126 | |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8127 | bio_for_each_segment_all(bvec, bio, i) { |
| 8128 | ret = __readpage_endio_check(done->inode, io_bio, i, |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 8129 | bvec->bv_page, bvec->bv_offset, |
| 8130 | done->start, bvec->bv_len); |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8131 | if (!ret) |
Nikolay Borisov | b30cb44 | 2017-02-20 13:50:56 +0200 | [diff] [blame] | 8132 | clean_io_failure(BTRFS_I(done->inode), done->start, |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 8133 | bvec->bv_page, bvec->bv_offset); |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8134 | else |
| 8135 | uptodate = 0; |
| 8136 | } |
| 8137 | |
| 8138 | done->uptodate = uptodate; |
| 8139 | end: |
| 8140 | complete(&done->done); |
| 8141 | bio_put(bio); |
| 8142 | } |
| 8143 | |
| 8144 | static int __btrfs_subio_endio_read(struct inode *inode, |
| 8145 | struct btrfs_io_bio *io_bio, int err) |
| 8146 | { |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 8147 | struct btrfs_fs_info *fs_info; |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8148 | struct bio_vec *bvec; |
| 8149 | struct btrfs_retry_complete done; |
| 8150 | u64 start; |
| 8151 | u64 offset = 0; |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 8152 | u32 sectorsize; |
| 8153 | int nr_sectors; |
| 8154 | unsigned int pgoff; |
| 8155 | int csum_pos; |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8156 | int i; |
| 8157 | int ret; |
| 8158 | |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 8159 | fs_info = BTRFS_I(inode)->root->fs_info; |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 8160 | sectorsize = fs_info->sectorsize; |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 8161 | |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8162 | err = 0; |
| 8163 | start = io_bio->logical; |
| 8164 | done.inode = inode; |
| 8165 | |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 8166 | bio_for_each_segment_all(bvec, &io_bio->bio, i) { |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 8167 | nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec->bv_len); |
| 8168 | |
| 8169 | pgoff = bvec->bv_offset; |
| 8170 | next_block: |
| 8171 | csum_pos = BTRFS_BYTES_TO_BLKS(fs_info, offset); |
| 8172 | ret = __readpage_endio_check(inode, io_bio, csum_pos, |
| 8173 | bvec->bv_page, pgoff, start, |
| 8174 | sectorsize); |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8175 | if (likely(!ret)) |
| 8176 | goto next; |
| 8177 | try_again: |
| 8178 | done.uptodate = 0; |
| 8179 | done.start = start; |
| 8180 | init_completion(&done.done); |
| 8181 | |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 8182 | ret = dio_read_error(inode, &io_bio->bio, bvec->bv_page, |
| 8183 | pgoff, start, start + sectorsize - 1, |
| 8184 | io_bio->mirror_num, |
| 8185 | btrfs_retry_endio, &done); |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8186 | if (ret) { |
| 8187 | err = ret; |
| 8188 | goto next; |
| 8189 | } |
| 8190 | |
| 8191 | wait_for_completion(&done.done); |
| 8192 | |
| 8193 | if (!done.uptodate) { |
| 8194 | /* We might have another mirror, so try again */ |
| 8195 | goto try_again; |
| 8196 | } |
| 8197 | next: |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 8198 | offset += sectorsize; |
| 8199 | start += sectorsize; |
| 8200 | |
| 8201 | ASSERT(nr_sectors); |
| 8202 | |
Liu Bo | 97bf5a5 | 2017-04-07 13:11:10 -0700 | [diff] [blame] | 8203 | nr_sectors--; |
| 8204 | if (nr_sectors) { |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 8205 | pgoff += sectorsize; |
Liu Bo | 97bf5a5 | 2017-04-07 13:11:10 -0700 | [diff] [blame] | 8206 | ASSERT(pgoff < PAGE_SIZE); |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 8207 | goto next_block; |
| 8208 | } |
Kent Overstreet | 2c30c71 | 2013-11-07 12:20:26 -0800 | [diff] [blame] | 8209 | } |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 8210 | |
| 8211 | return err; |
| 8212 | } |
| 8213 | |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8214 | static int btrfs_subio_endio_read(struct inode *inode, |
| 8215 | struct btrfs_io_bio *io_bio, int err) |
| 8216 | { |
| 8217 | bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM; |
| 8218 | |
| 8219 | if (skip_csum) { |
| 8220 | if (unlikely(err)) |
| 8221 | return __btrfs_correct_data_nocsum(inode, io_bio); |
| 8222 | else |
| 8223 | return 0; |
| 8224 | } else { |
| 8225 | return __btrfs_subio_endio_read(inode, io_bio, err); |
| 8226 | } |
| 8227 | } |
| 8228 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 8229 | static void btrfs_endio_direct_read(struct bio *bio) |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 8230 | { |
| 8231 | struct btrfs_dio_private *dip = bio->bi_private; |
| 8232 | struct inode *inode = dip->inode; |
| 8233 | struct bio *dio_bio; |
| 8234 | struct btrfs_io_bio *io_bio = btrfs_io_bio(bio); |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 8235 | int err = bio->bi_error; |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 8236 | |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8237 | if (dip->flags & BTRFS_DIO_ORIG_BIO_SUBMITTED) |
| 8238 | err = btrfs_subio_endio_read(inode, io_bio, err); |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 8239 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8240 | unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset, |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 8241 | dip->logical_offset + dip->bytes - 1); |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 8242 | dio_bio = dip->dio_bio; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8243 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8244 | kfree(dip); |
Josef Bacik | c0da7aa | 2011-03-22 11:05:07 -0400 | [diff] [blame] | 8245 | |
Filipe Manana | 1636d1d | 2016-02-15 16:20:26 +0000 | [diff] [blame] | 8246 | dio_bio->bi_error = bio->bi_error; |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 8247 | dio_end_io(dio_bio, bio->bi_error); |
Miao Xie | 23ea8e5 | 2014-09-12 18:43:54 +0800 | [diff] [blame] | 8248 | |
| 8249 | if (io_bio->end_io) |
| 8250 | io_bio->end_io(io_bio, err); |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 8251 | bio_put(bio); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8252 | } |
| 8253 | |
Qu Wenruo | 52427260 | 2017-03-08 10:25:52 +0800 | [diff] [blame] | 8254 | static void __endio_write_update_ordered(struct inode *inode, |
| 8255 | const u64 offset, const u64 bytes, |
| 8256 | const bool uptodate) |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8257 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8258 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8259 | struct btrfs_ordered_extent *ordered = NULL; |
Qu Wenruo | 52427260 | 2017-03-08 10:25:52 +0800 | [diff] [blame] | 8260 | struct btrfs_workqueue *wq; |
| 8261 | btrfs_work_func_t func; |
Filipe Manana | 1454377 | 2015-11-24 16:23:54 +0000 | [diff] [blame] | 8262 | u64 ordered_offset = offset; |
| 8263 | u64 ordered_bytes = bytes; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8264 | int ret; |
| 8265 | |
Qu Wenruo | 52427260 | 2017-03-08 10:25:52 +0800 | [diff] [blame] | 8266 | if (btrfs_is_free_space_inode(BTRFS_I(inode))) { |
| 8267 | wq = fs_info->endio_freespace_worker; |
| 8268 | func = btrfs_freespace_write_helper; |
| 8269 | } else { |
| 8270 | wq = fs_info->endio_write_workers; |
| 8271 | func = btrfs_endio_write_helper; |
| 8272 | } |
| 8273 | |
Chris Mason | 163cf09 | 2010-11-28 19:56:33 -0500 | [diff] [blame] | 8274 | again: |
| 8275 | ret = btrfs_dec_test_first_ordered_pending(inode, &ordered, |
| 8276 | &ordered_offset, |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 8277 | ordered_bytes, |
Filipe Manana | 1454377 | 2015-11-24 16:23:54 +0000 | [diff] [blame] | 8278 | uptodate); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8279 | if (!ret) |
Chris Mason | 163cf09 | 2010-11-28 19:56:33 -0500 | [diff] [blame] | 8280 | goto out_test; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8281 | |
Qu Wenruo | 52427260 | 2017-03-08 10:25:52 +0800 | [diff] [blame] | 8282 | btrfs_init_work(&ordered->work, func, finish_ordered_fn, NULL, NULL); |
| 8283 | btrfs_queue_work(wq, &ordered->work); |
Chris Mason | 163cf09 | 2010-11-28 19:56:33 -0500 | [diff] [blame] | 8284 | out_test: |
| 8285 | /* |
| 8286 | * our bio might span multiple ordered extents. If we haven't |
| 8287 | * completed the accounting for the whole dio, go back and try again |
| 8288 | */ |
Filipe Manana | 1454377 | 2015-11-24 16:23:54 +0000 | [diff] [blame] | 8289 | if (ordered_offset < offset + bytes) { |
| 8290 | ordered_bytes = offset + bytes - ordered_offset; |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 8291 | ordered = NULL; |
Chris Mason | 163cf09 | 2010-11-28 19:56:33 -0500 | [diff] [blame] | 8292 | goto again; |
| 8293 | } |
Filipe Manana | 1454377 | 2015-11-24 16:23:54 +0000 | [diff] [blame] | 8294 | } |
| 8295 | |
| 8296 | static void btrfs_endio_direct_write(struct bio *bio) |
| 8297 | { |
| 8298 | struct btrfs_dio_private *dip = bio->bi_private; |
| 8299 | struct bio *dio_bio = dip->dio_bio; |
| 8300 | |
Qu Wenruo | 52427260 | 2017-03-08 10:25:52 +0800 | [diff] [blame] | 8301 | __endio_write_update_ordered(dip->inode, dip->logical_offset, |
| 8302 | dip->bytes, !bio->bi_error); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8303 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8304 | kfree(dip); |
Josef Bacik | c0da7aa | 2011-03-22 11:05:07 -0400 | [diff] [blame] | 8305 | |
Filipe Manana | 1636d1d | 2016-02-15 16:20:26 +0000 | [diff] [blame] | 8306 | dio_bio->bi_error = bio->bi_error; |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 8307 | dio_end_io(dio_bio, bio->bi_error); |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 8308 | bio_put(bio); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8309 | } |
| 8310 | |
Mike Christie | 81a75f6 | 2016-06-05 14:31:54 -0500 | [diff] [blame] | 8311 | static int __btrfs_submit_bio_start_direct_io(struct inode *inode, |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 8312 | struct bio *bio, int mirror_num, |
| 8313 | unsigned long bio_flags, u64 offset) |
| 8314 | { |
| 8315 | int ret; |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 8316 | ret = btrfs_csum_one_bio(inode, bio, offset, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 8317 | BUG_ON(ret); /* -ENOMEM */ |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 8318 | return 0; |
| 8319 | } |
| 8320 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 8321 | static void btrfs_end_dio_bio(struct bio *bio) |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8322 | { |
| 8323 | struct btrfs_dio_private *dip = bio->bi_private; |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 8324 | int err = bio->bi_error; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8325 | |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8326 | if (err) |
| 8327 | btrfs_warn(BTRFS_I(dip->inode)->root->fs_info, |
Mike Christie | 6296b96 | 2016-06-05 14:32:21 -0500 | [diff] [blame] | 8328 | "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] | 8329 | btrfs_ino(BTRFS_I(dip->inode)), bio_op(bio), |
| 8330 | bio->bi_opf, |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8331 | (unsigned long long)bio->bi_iter.bi_sector, |
| 8332 | bio->bi_iter.bi_size, err); |
| 8333 | |
| 8334 | if (dip->subio_endio) |
| 8335 | err = dip->subio_endio(dip->inode, btrfs_io_bio(bio), err); |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 8336 | |
| 8337 | if (err) { |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8338 | dip->errors = 1; |
| 8339 | |
| 8340 | /* |
| 8341 | * before atomic variable goto zero, we must make sure |
| 8342 | * dip->errors is perceived to be set. |
| 8343 | */ |
Peter Zijlstra | 4e857c5 | 2014-03-17 18:06:10 +0100 | [diff] [blame] | 8344 | smp_mb__before_atomic(); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8345 | } |
| 8346 | |
| 8347 | /* if there are more bios still pending for this dio, just exit */ |
| 8348 | if (!atomic_dec_and_test(&dip->pending_bios)) |
| 8349 | goto out; |
| 8350 | |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 8351 | if (dip->errors) { |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8352 | bio_io_error(dip->orig_bio); |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 8353 | } else { |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 8354 | dip->dio_bio->bi_error = 0; |
| 8355 | bio_endio(dip->orig_bio); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8356 | } |
| 8357 | out: |
| 8358 | bio_put(bio); |
| 8359 | } |
| 8360 | |
| 8361 | static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev, |
| 8362 | u64 first_sector, gfp_t gfp_flags) |
| 8363 | { |
Chris Mason | da2f0f7 | 2015-07-02 13:57:22 -0700 | [diff] [blame] | 8364 | struct bio *bio; |
Linus Torvalds | 2236597 | 2015-09-05 15:14:43 -0700 | [diff] [blame] | 8365 | bio = btrfs_bio_alloc(bdev, first_sector, BIO_MAX_PAGES, gfp_flags); |
Chris Mason | da2f0f7 | 2015-07-02 13:57:22 -0700 | [diff] [blame] | 8366 | if (bio) |
| 8367 | bio_associate_current(bio); |
| 8368 | return bio; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8369 | } |
| 8370 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 8371 | static inline int btrfs_lookup_and_bind_dio_csum(struct inode *inode, |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 8372 | struct btrfs_dio_private *dip, |
| 8373 | struct bio *bio, |
| 8374 | u64 file_offset) |
| 8375 | { |
| 8376 | struct btrfs_io_bio *io_bio = btrfs_io_bio(bio); |
| 8377 | struct btrfs_io_bio *orig_io_bio = btrfs_io_bio(dip->orig_bio); |
| 8378 | int ret; |
| 8379 | |
| 8380 | /* |
| 8381 | * We load all the csum data we need when we submit |
| 8382 | * the first bio to reduce the csum tree search and |
| 8383 | * contention. |
| 8384 | */ |
| 8385 | if (dip->logical_offset == file_offset) { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 8386 | ret = btrfs_lookup_bio_sums_dio(inode, dip->orig_bio, |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 8387 | file_offset); |
| 8388 | if (ret) |
| 8389 | return ret; |
| 8390 | } |
| 8391 | |
| 8392 | if (bio == dip->orig_bio) |
| 8393 | return 0; |
| 8394 | |
| 8395 | file_offset -= dip->logical_offset; |
| 8396 | file_offset >>= inode->i_sb->s_blocksize_bits; |
| 8397 | io_bio->csum = (u8 *)(((u32 *)orig_io_bio->csum) + file_offset); |
| 8398 | |
| 8399 | return 0; |
| 8400 | } |
| 8401 | |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8402 | static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode, |
Mike Christie | 81a75f6 | 2016-06-05 14:31:54 -0500 | [diff] [blame] | 8403 | u64 file_offset, int skip_sum, |
Josef Bacik | c329861 | 2012-08-03 16:49:19 -0400 | [diff] [blame] | 8404 | int async_submit) |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8405 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8406 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 8407 | struct btrfs_dio_private *dip = bio->bi_private; |
Mike Christie | 37226b2 | 2016-06-05 14:31:52 -0500 | [diff] [blame] | 8408 | bool write = bio_op(bio) == REQ_OP_WRITE; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8409 | int ret; |
| 8410 | |
Josef Bacik | b812ce2 | 2012-11-16 13:56:32 -0500 | [diff] [blame] | 8411 | if (async_submit) |
| 8412 | async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers); |
| 8413 | |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8414 | bio_get(bio); |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 8415 | |
| 8416 | if (!write) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8417 | 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] | 8418 | if (ret) |
| 8419 | goto err; |
| 8420 | } |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8421 | |
Josef Bacik | 1ae3993 | 2011-04-06 14:41:34 -0400 | [diff] [blame] | 8422 | if (skip_sum) |
| 8423 | goto map; |
| 8424 | |
| 8425 | if (write && async_submit) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8426 | ret = btrfs_wq_submit_bio(fs_info, inode, bio, 0, 0, |
| 8427 | file_offset, |
| 8428 | __btrfs_submit_bio_start_direct_io, |
| 8429 | __btrfs_submit_bio_done); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8430 | goto err; |
Josef Bacik | 1ae3993 | 2011-04-06 14:41:34 -0400 | [diff] [blame] | 8431 | } else if (write) { |
| 8432 | /* |
| 8433 | * If we aren't doing async submit, calculate the csum of the |
| 8434 | * bio now. |
| 8435 | */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 8436 | ret = btrfs_csum_one_bio(inode, bio, file_offset, 1); |
Josef Bacik | 1ae3993 | 2011-04-06 14:41:34 -0400 | [diff] [blame] | 8437 | if (ret) |
| 8438 | goto err; |
Miao Xie | 23ea8e5 | 2014-09-12 18:43:54 +0800 | [diff] [blame] | 8439 | } else { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 8440 | ret = btrfs_lookup_and_bind_dio_csum(inode, dip, bio, |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 8441 | file_offset); |
Tsutomu Itoh | c2db107 | 2011-03-01 06:48:31 +0000 | [diff] [blame] | 8442 | if (ret) |
| 8443 | goto err; |
| 8444 | } |
Josef Bacik | 1ae3993 | 2011-04-06 14:41:34 -0400 | [diff] [blame] | 8445 | map: |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 8446 | ret = btrfs_map_bio(fs_info, bio, 0, async_submit); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8447 | err: |
| 8448 | bio_put(bio); |
| 8449 | return ret; |
| 8450 | } |
| 8451 | |
Mike Christie | 81a75f6 | 2016-06-05 14:31:54 -0500 | [diff] [blame] | 8452 | static int btrfs_submit_direct_hook(struct btrfs_dio_private *dip, |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8453 | int skip_sum) |
| 8454 | { |
| 8455 | struct inode *inode = dip->inode; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8456 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8457 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8458 | struct bio *bio; |
| 8459 | struct bio *orig_bio = dip->orig_bio; |
Christoph Hellwig | 6a2de22 | 2016-11-25 09:07:48 +0100 | [diff] [blame] | 8460 | struct bio_vec *bvec; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 8461 | u64 start_sector = orig_bio->bi_iter.bi_sector; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8462 | u64 file_offset = dip->logical_offset; |
| 8463 | u64 submit_len = 0; |
| 8464 | u64 map_length; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8465 | u32 blocksize = fs_info->sectorsize; |
Josef Bacik | 1ae3993 | 2011-04-06 14:41:34 -0400 | [diff] [blame] | 8466 | int async_submit = 0; |
Chandan Rajendra | 5f4dc8f | 2016-01-21 15:56:00 +0530 | [diff] [blame] | 8467 | int nr_sectors; |
| 8468 | int ret; |
Christoph Hellwig | 6a2de22 | 2016-11-25 09:07:48 +0100 | [diff] [blame] | 8469 | int i, j; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8470 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 8471 | map_length = orig_bio->bi_iter.bi_size; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8472 | ret = btrfs_map_block(fs_info, btrfs_op(orig_bio), start_sector << 9, |
| 8473 | &map_length, NULL, 0); |
Miao Xie | 7a5c3c9 | 2014-06-17 18:58:59 +0800 | [diff] [blame] | 8474 | if (ret) |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8475 | return -EIO; |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 8476 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 8477 | if (map_length >= orig_bio->bi_iter.bi_size) { |
Josef Bacik | 02f57c7 | 2011-04-06 14:25:44 -0400 | [diff] [blame] | 8478 | bio = orig_bio; |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 8479 | dip->flags |= BTRFS_DIO_ORIG_BIO_SUBMITTED; |
Josef Bacik | 02f57c7 | 2011-04-06 14:25:44 -0400 | [diff] [blame] | 8480 | goto submit; |
| 8481 | } |
| 8482 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 8483 | /* async crcs make it difficult to collect full stripe writes. */ |
Zhao Lei | ffe2d20 | 2015-01-20 15:11:44 +0800 | [diff] [blame] | 8484 | if (btrfs_get_alloc_profile(root, 1) & BTRFS_BLOCK_GROUP_RAID56_MASK) |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 8485 | async_submit = 0; |
| 8486 | else |
| 8487 | async_submit = 1; |
| 8488 | |
Josef Bacik | 02f57c7 | 2011-04-06 14:25:44 -0400 | [diff] [blame] | 8489 | bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS); |
| 8490 | if (!bio) |
| 8491 | return -ENOMEM; |
Miao Xie | 7a5c3c9 | 2014-06-17 18:58:59 +0800 | [diff] [blame] | 8492 | |
Christoph Hellwig | ef295ec | 2016-10-28 08:48:16 -0600 | [diff] [blame] | 8493 | bio->bi_opf = orig_bio->bi_opf; |
Josef Bacik | 02f57c7 | 2011-04-06 14:25:44 -0400 | [diff] [blame] | 8494 | bio->bi_private = dip; |
| 8495 | bio->bi_end_io = btrfs_end_dio_bio; |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 8496 | btrfs_io_bio(bio)->logical = file_offset; |
Josef Bacik | 02f57c7 | 2011-04-06 14:25:44 -0400 | [diff] [blame] | 8497 | atomic_inc(&dip->pending_bios); |
| 8498 | |
Christoph Hellwig | 6a2de22 | 2016-11-25 09:07:48 +0100 | [diff] [blame] | 8499 | bio_for_each_segment_all(bvec, orig_bio, j) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8500 | nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec->bv_len); |
Chandan Rajendra | 5f4dc8f | 2016-01-21 15:56:00 +0530 | [diff] [blame] | 8501 | i = 0; |
| 8502 | next_block: |
| 8503 | if (unlikely(map_length < submit_len + blocksize || |
| 8504 | bio_add_page(bio, bvec->bv_page, blocksize, |
| 8505 | bvec->bv_offset + (i * blocksize)) < blocksize)) { |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8506 | /* |
| 8507 | * inc the count before we submit the bio so |
| 8508 | * we know the end IO handler won't happen before |
| 8509 | * we inc the count. Otherwise, the dip might get freed |
| 8510 | * before we're done setting it up |
| 8511 | */ |
| 8512 | atomic_inc(&dip->pending_bios); |
Mike Christie | 81a75f6 | 2016-06-05 14:31:54 -0500 | [diff] [blame] | 8513 | ret = __btrfs_submit_dio_bio(bio, inode, |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8514 | file_offset, skip_sum, |
Josef Bacik | c329861 | 2012-08-03 16:49:19 -0400 | [diff] [blame] | 8515 | async_submit); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8516 | if (ret) { |
| 8517 | bio_put(bio); |
| 8518 | atomic_dec(&dip->pending_bios); |
| 8519 | goto out_err; |
| 8520 | } |
| 8521 | |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8522 | start_sector += submit_len >> 9; |
| 8523 | file_offset += submit_len; |
| 8524 | |
| 8525 | submit_len = 0; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8526 | |
| 8527 | bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, |
| 8528 | start_sector, GFP_NOFS); |
| 8529 | if (!bio) |
| 8530 | goto out_err; |
Christoph Hellwig | ef295ec | 2016-10-28 08:48:16 -0600 | [diff] [blame] | 8531 | bio->bi_opf = orig_bio->bi_opf; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8532 | bio->bi_private = dip; |
| 8533 | bio->bi_end_io = btrfs_end_dio_bio; |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 8534 | btrfs_io_bio(bio)->logical = file_offset; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8535 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 8536 | map_length = orig_bio->bi_iter.bi_size; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8537 | ret = btrfs_map_block(fs_info, btrfs_op(orig_bio), |
Stefan Behrens | 3ec706c | 2012-11-05 15:46:42 +0100 | [diff] [blame] | 8538 | start_sector << 9, |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8539 | &map_length, NULL, 0); |
| 8540 | if (ret) { |
| 8541 | bio_put(bio); |
| 8542 | goto out_err; |
| 8543 | } |
Chandan Rajendra | 5f4dc8f | 2016-01-21 15:56:00 +0530 | [diff] [blame] | 8544 | |
| 8545 | goto next_block; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8546 | } else { |
Chandan Rajendra | 5f4dc8f | 2016-01-21 15:56:00 +0530 | [diff] [blame] | 8547 | submit_len += blocksize; |
| 8548 | if (--nr_sectors) { |
| 8549 | i++; |
| 8550 | goto next_block; |
| 8551 | } |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8552 | } |
| 8553 | } |
| 8554 | |
Josef Bacik | 02f57c7 | 2011-04-06 14:25:44 -0400 | [diff] [blame] | 8555 | submit: |
Mike Christie | 81a75f6 | 2016-06-05 14:31:54 -0500 | [diff] [blame] | 8556 | ret = __btrfs_submit_dio_bio(bio, inode, file_offset, skip_sum, |
Josef Bacik | c329861 | 2012-08-03 16:49:19 -0400 | [diff] [blame] | 8557 | async_submit); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8558 | if (!ret) |
| 8559 | return 0; |
| 8560 | |
| 8561 | bio_put(bio); |
| 8562 | out_err: |
| 8563 | dip->errors = 1; |
| 8564 | /* |
| 8565 | * before atomic variable goto zero, we must |
| 8566 | * make sure dip->errors is perceived to be set. |
| 8567 | */ |
Peter Zijlstra | 4e857c5 | 2014-03-17 18:06:10 +0100 | [diff] [blame] | 8568 | smp_mb__before_atomic(); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8569 | if (atomic_dec_and_test(&dip->pending_bios)) |
| 8570 | bio_io_error(dip->orig_bio); |
| 8571 | |
| 8572 | /* bio_end_io() will handle error, so we needn't return it */ |
| 8573 | return 0; |
| 8574 | } |
| 8575 | |
Mike Christie | 8a4c1e4 | 2016-06-05 14:31:50 -0500 | [diff] [blame] | 8576 | static void btrfs_submit_direct(struct bio *dio_bio, struct inode *inode, |
| 8577 | loff_t file_offset) |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8578 | { |
Filipe Manana | 61de718 | 2015-07-01 12:13:10 +0100 | [diff] [blame] | 8579 | struct btrfs_dio_private *dip = NULL; |
| 8580 | struct bio *io_bio = NULL; |
Miao Xie | 23ea8e5 | 2014-09-12 18:43:54 +0800 | [diff] [blame] | 8581 | struct btrfs_io_bio *btrfs_bio; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8582 | int skip_sum; |
Mike Christie | 8a4c1e4 | 2016-06-05 14:31:50 -0500 | [diff] [blame] | 8583 | bool write = (bio_op(dio_bio) == REQ_OP_WRITE); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8584 | int ret = 0; |
| 8585 | |
| 8586 | skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM; |
| 8587 | |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 8588 | io_bio = btrfs_bio_clone(dio_bio, GFP_NOFS); |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 8589 | if (!io_bio) { |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8590 | ret = -ENOMEM; |
| 8591 | goto free_ordered; |
| 8592 | } |
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; |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 8605 | io_bio->bi_private = dip; |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 8606 | dip->orig_bio = io_bio; |
| 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); |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 8609 | btrfs_bio = btrfs_io_bio(io_bio); |
| 8610 | btrfs_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) { |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 8613 | io_bio->bi_end_io = btrfs_endio_direct_write; |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 8614 | } else { |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 8615 | io_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 | 81a75f6 | 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 | |
Miao Xie | 23ea8e5 | 2014-09-12 18:43:54 +0800 | [diff] [blame] | 8638 | if (btrfs_bio->end_io) |
| 8639 | btrfs_bio->end_io(btrfs_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 | */ |
Filipe Manana | 61de718 | 2015-07-01 12:13:10 +0100 | [diff] [blame] | 8651 | if (io_bio && dip) { |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 8652 | io_bio->bi_error = -EIO; |
| 8653 | bio_endio(io_bio); |
Filipe Manana | 61de718 | 2015-07-01 12:13:10 +0100 | [diff] [blame] | 8654 | /* |
| 8655 | * The end io callbacks free our dip, do the final put on io_bio |
| 8656 | * and all the cleanup and final put for dio_bio (through |
| 8657 | * dio_end_io()). |
| 8658 | */ |
| 8659 | dip = NULL; |
| 8660 | io_bio = NULL; |
| 8661 | } else { |
Filipe Manana | 1454377 | 2015-11-24 16:23:54 +0000 | [diff] [blame] | 8662 | if (write) |
Qu Wenruo | 52427260 | 2017-03-08 10:25:52 +0800 | [diff] [blame] | 8663 | __endio_write_update_ordered(inode, |
Filipe Manana | 1454377 | 2015-11-24 16:23:54 +0000 | [diff] [blame] | 8664 | file_offset, |
| 8665 | dio_bio->bi_iter.bi_size, |
Qu Wenruo | 52427260 | 2017-03-08 10:25:52 +0800 | [diff] [blame] | 8666 | false); |
Filipe Manana | 1454377 | 2015-11-24 16:23:54 +0000 | [diff] [blame] | 8667 | else |
Filipe Manana | 61de718 | 2015-07-01 12:13:10 +0100 | [diff] [blame] | 8668 | unlock_extent(&BTRFS_I(inode)->io_tree, file_offset, |
| 8669 | file_offset + dio_bio->bi_iter.bi_size - 1); |
Filipe Manana | 1454377 | 2015-11-24 16:23:54 +0000 | [diff] [blame] | 8670 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 8671 | dio_bio->bi_error = -EIO; |
Filipe Manana | 61de718 | 2015-07-01 12:13:10 +0100 | [diff] [blame] | 8672 | /* |
| 8673 | * Releases and cleans up our dio_bio, no need to bio_put() |
| 8674 | * nor bio_endio()/bio_io_error() against dio_bio. |
| 8675 | */ |
| 8676 | dio_end_io(dio_bio, ret); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8677 | } |
Filipe Manana | 61de718 | 2015-07-01 12:13:10 +0100 | [diff] [blame] | 8678 | if (io_bio) |
| 8679 | bio_put(io_bio); |
| 8680 | kfree(dip); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8681 | } |
| 8682 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 8683 | static ssize_t check_direct_IO(struct btrfs_fs_info *fs_info, |
| 8684 | struct kiocb *iocb, |
| 8685 | const struct iov_iter *iter, loff_t offset) |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 8686 | { |
| 8687 | int seg; |
Josef Bacik | a1b75f7 | 2011-04-08 15:51:18 +0000 | [diff] [blame] | 8688 | int i; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8689 | unsigned int blocksize_mask = fs_info->sectorsize - 1; |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 8690 | ssize_t retval = -EINVAL; |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 8691 | |
| 8692 | if (offset & blocksize_mask) |
| 8693 | goto out; |
| 8694 | |
Al Viro | 28060d5 | 2014-03-22 05:15:17 -0400 | [diff] [blame] | 8695 | if (iov_iter_alignment(iter) & blocksize_mask) |
| 8696 | goto out; |
Josef Bacik | a1b75f7 | 2011-04-08 15:51:18 +0000 | [diff] [blame] | 8697 | |
Al Viro | 28060d5 | 2014-03-22 05:15:17 -0400 | [diff] [blame] | 8698 | /* If this is a write we don't need to check anymore */ |
Al Viro | cd27e45 | 2016-10-10 13:39:05 -0400 | [diff] [blame] | 8699 | if (iov_iter_rw(iter) != READ || !iter_is_iovec(iter)) |
Al Viro | 28060d5 | 2014-03-22 05:15:17 -0400 | [diff] [blame] | 8700 | return 0; |
| 8701 | /* |
| 8702 | * Check to make sure we don't have duplicate iov_base's in this |
| 8703 | * iovec, if so return EINVAL, otherwise we'll get csum errors |
| 8704 | * when reading back. |
| 8705 | */ |
| 8706 | for (seg = 0; seg < iter->nr_segs; seg++) { |
| 8707 | for (i = seg + 1; i < iter->nr_segs; i++) { |
| 8708 | if (iter->iov[seg].iov_base == iter->iov[i].iov_base) |
Josef Bacik | a1b75f7 | 2011-04-08 15:51:18 +0000 | [diff] [blame] | 8709 | goto out; |
| 8710 | } |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 8711 | } |
| 8712 | retval = 0; |
| 8713 | out: |
| 8714 | return retval; |
| 8715 | } |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 8716 | |
Christoph Hellwig | c8b8e32 | 2016-04-07 08:51:58 -0700 | [diff] [blame] | 8717 | 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] | 8718 | { |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8719 | struct file *file = iocb->ki_filp; |
| 8720 | struct inode *inode = file->f_mapping->host; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8721 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
chandan | 50745b0 | 2015-08-28 21:10:13 +0530 | [diff] [blame] | 8722 | struct btrfs_dio_data dio_data = { 0 }; |
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; |
| 8758 | } |
Qu Wenruo | 7cf5b97 | 2015-09-08 17:25:55 +0800 | [diff] [blame] | 8759 | ret = btrfs_delalloc_reserve_space(inode, offset, count); |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 8760 | if (ret) |
Miao Xie | 38851cc | 2013-02-08 07:04:11 +0000 | [diff] [blame] | 8761 | goto out; |
David Sterba | 823bb20 | 2017-01-04 11:09:51 +0100 | [diff] [blame] | 8762 | dio_data.outstanding_extents = count_max_extents(count); |
Josef Bacik | e1cbbfa | 2015-03-17 10:52:28 -0400 | [diff] [blame] | 8763 | |
| 8764 | /* |
| 8765 | * We need to know how many extents we reserved so that we can |
| 8766 | * do the accounting properly if we go over the number we |
| 8767 | * originally calculated. Abuse current->journal_info for this. |
| 8768 | */ |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 8769 | dio_data.reserve = round_up(count, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8770 | fs_info->sectorsize); |
Filipe Manana | f28a492 | 2015-12-08 19:23:20 +0000 | [diff] [blame] | 8771 | dio_data.unsubmitted_oe_range_start = (u64)offset; |
| 8772 | dio_data.unsubmitted_oe_range_end = (u64)offset; |
chandan | 50745b0 | 2015-08-28 21:10:13 +0530 | [diff] [blame] | 8773 | current->journal_info = &dio_data; |
Chandan Rajendra | 97dcdea | 2016-12-23 15:00:18 +0530 | [diff] [blame] | 8774 | down_read(&BTRFS_I(inode)->dio_sem); |
David Sterba | ee39b43 | 2014-09-30 01:33:33 +0200 | [diff] [blame] | 8775 | } else if (test_bit(BTRFS_INODE_READDIO_NEED_LOCK, |
| 8776 | &BTRFS_I(inode)->runtime_flags)) { |
Jens Axboe | fe0f07d | 2015-04-15 17:05:48 -0600 | [diff] [blame] | 8777 | inode_dio_end(inode); |
Miao Xie | 38851cc | 2013-02-08 07:04:11 +0000 | [diff] [blame] | 8778 | flags = DIO_LOCKING | DIO_SKIP_HOLES; |
| 8779 | wakeup = false; |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 8780 | } |
| 8781 | |
Omar Sandoval | 17f8c84 | 2015-03-16 04:33:50 -0700 | [diff] [blame] | 8782 | ret = __blockdev_direct_IO(iocb, inode, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8783 | fs_info->fs_devices->latest_bdev, |
Christoph Hellwig | c8b8e32 | 2016-04-07 08:51:58 -0700 | [diff] [blame] | 8784 | iter, btrfs_get_blocks_direct, NULL, |
Omar Sandoval | 17f8c84 | 2015-03-16 04:33:50 -0700 | [diff] [blame] | 8785 | btrfs_submit_direct, flags); |
Omar Sandoval | 6f67376 | 2015-03-16 04:33:52 -0700 | [diff] [blame] | 8786 | if (iov_iter_rw(iter) == WRITE) { |
Chandan Rajendra | 97dcdea | 2016-12-23 15:00:18 +0530 | [diff] [blame] | 8787 | up_read(&BTRFS_I(inode)->dio_sem); |
Josef Bacik | e1cbbfa | 2015-03-17 10:52:28 -0400 | [diff] [blame] | 8788 | current->journal_info = NULL; |
Liu Bo | ddba1bf | 2015-06-17 16:59:58 +0800 | [diff] [blame] | 8789 | if (ret < 0 && ret != -EIOCBQUEUED) { |
chandan | 50745b0 | 2015-08-28 21:10:13 +0530 | [diff] [blame] | 8790 | if (dio_data.reserve) |
Qu Wenruo | 7cf5b97 | 2015-09-08 17:25:55 +0800 | [diff] [blame] | 8791 | btrfs_delalloc_release_space(inode, offset, |
| 8792 | dio_data.reserve); |
Filipe Manana | f28a492 | 2015-12-08 19:23:20 +0000 | [diff] [blame] | 8793 | /* |
| 8794 | * On error we might have left some ordered extents |
| 8795 | * without submitting corresponding bios for them, so |
| 8796 | * cleanup them up to avoid other tasks getting them |
| 8797 | * and waiting for them to complete forever. |
| 8798 | */ |
| 8799 | if (dio_data.unsubmitted_oe_range_start < |
| 8800 | dio_data.unsubmitted_oe_range_end) |
Qu Wenruo | 52427260 | 2017-03-08 10:25:52 +0800 | [diff] [blame] | 8801 | __endio_write_update_ordered(inode, |
Filipe Manana | f28a492 | 2015-12-08 19:23:20 +0000 | [diff] [blame] | 8802 | dio_data.unsubmitted_oe_range_start, |
| 8803 | dio_data.unsubmitted_oe_range_end - |
| 8804 | dio_data.unsubmitted_oe_range_start, |
Qu Wenruo | 52427260 | 2017-03-08 10:25:52 +0800 | [diff] [blame] | 8805 | false); |
Liu Bo | ddba1bf | 2015-06-17 16:59:58 +0800 | [diff] [blame] | 8806 | } else if (ret >= 0 && (size_t)ret < count) |
Qu Wenruo | 7cf5b97 | 2015-09-08 17:25:55 +0800 | [diff] [blame] | 8807 | btrfs_delalloc_release_space(inode, offset, |
| 8808 | count - (size_t)ret); |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 8809 | } |
Miao Xie | 38851cc | 2013-02-08 07:04:11 +0000 | [diff] [blame] | 8810 | out: |
Miao Xie | 2e60a51 | 2013-02-08 07:01:08 +0000 | [diff] [blame] | 8811 | if (wakeup) |
Jens Axboe | fe0f07d | 2015-04-15 17:05:48 -0600 | [diff] [blame] | 8812 | inode_dio_end(inode); |
Miao Xie | 38851cc | 2013-02-08 07:04:11 +0000 | [diff] [blame] | 8813 | if (relock) |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 8814 | inode_lock(inode); |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 8815 | |
| 8816 | return ret; |
Chris Mason | 1643298 | 2008-04-10 10:23:21 -0400 | [diff] [blame] | 8817 | } |
| 8818 | |
Tsutomu Itoh | 05dadc0 | 2012-11-29 05:08:26 +0000 | [diff] [blame] | 8819 | #define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC) |
| 8820 | |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 8821 | static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, |
| 8822 | __u64 start, __u64 len) |
| 8823 | { |
Tsutomu Itoh | 05dadc0 | 2012-11-29 05:08:26 +0000 | [diff] [blame] | 8824 | int ret; |
| 8825 | |
| 8826 | ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS); |
| 8827 | if (ret) |
| 8828 | return ret; |
| 8829 | |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 8830 | return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap); |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 8831 | } |
| 8832 | |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8833 | int btrfs_readpage(struct file *file, struct page *page) |
| 8834 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 8835 | struct extent_io_tree *tree; |
| 8836 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
Jan Schmidt | 8ddc7d9 | 2011-06-13 20:02:58 +0200 | [diff] [blame] | 8837 | return extent_read_full_page(tree, page, btrfs_get_extent, 0); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8838 | } |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 8839 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8840 | static int btrfs_writepage(struct page *page, struct writeback_control *wbc) |
| 8841 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 8842 | struct extent_io_tree *tree; |
Josef Bacik | be7bd73 | 2015-10-22 15:05:09 -0400 | [diff] [blame] | 8843 | struct inode *inode = page->mapping->host; |
| 8844 | int ret; |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8845 | |
| 8846 | if (current->flags & PF_MEMALLOC) { |
| 8847 | redirty_page_for_writepage(wbc, page); |
| 8848 | unlock_page(page); |
| 8849 | return 0; |
| 8850 | } |
Josef Bacik | be7bd73 | 2015-10-22 15:05:09 -0400 | [diff] [blame] | 8851 | |
| 8852 | /* |
| 8853 | * If we are under memory pressure we will call this directly from the |
| 8854 | * VM, we need to make sure we have the inode referenced for the ordered |
| 8855 | * extent. If not just return like we didn't do anything. |
| 8856 | */ |
| 8857 | if (!igrab(inode)) { |
| 8858 | redirty_page_for_writepage(wbc, page); |
| 8859 | return AOP_WRITEPAGE_ACTIVATE; |
| 8860 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 8861 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
Josef Bacik | be7bd73 | 2015-10-22 15:05:09 -0400 | [diff] [blame] | 8862 | ret = extent_write_full_page(tree, page, btrfs_get_extent, wbc); |
| 8863 | btrfs_add_delayed_iput(inode); |
| 8864 | return ret; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8865 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8866 | |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 8867 | static int btrfs_writepages(struct address_space *mapping, |
| 8868 | struct writeback_control *wbc) |
Chris Mason | b293f02 | 2007-11-01 19:45:34 -0400 | [diff] [blame] | 8869 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 8870 | struct extent_io_tree *tree; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 8871 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 8872 | tree = &BTRFS_I(mapping->host)->io_tree; |
Chris Mason | b293f02 | 2007-11-01 19:45:34 -0400 | [diff] [blame] | 8873 | return extent_writepages(tree, mapping, btrfs_get_extent, wbc); |
| 8874 | } |
| 8875 | |
Chris Mason | 3ab2fb5 | 2007-11-08 10:59:22 -0500 | [diff] [blame] | 8876 | static int |
| 8877 | btrfs_readpages(struct file *file, struct address_space *mapping, |
| 8878 | struct list_head *pages, unsigned nr_pages) |
| 8879 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 8880 | struct extent_io_tree *tree; |
| 8881 | tree = &BTRFS_I(mapping->host)->io_tree; |
Chris Mason | 3ab2fb5 | 2007-11-08 10:59:22 -0500 | [diff] [blame] | 8882 | return extent_readpages(tree, mapping, pages, nr_pages, |
| 8883 | btrfs_get_extent); |
| 8884 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8885 | static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8886 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 8887 | struct extent_io_tree *tree; |
| 8888 | struct extent_map_tree *map; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8889 | int ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8890 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 8891 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
| 8892 | map = &BTRFS_I(page->mapping->host)->extent_tree; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 8893 | ret = try_release_extent_mapping(map, tree, page, gfp_flags); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8894 | if (ret == 1) { |
| 8895 | ClearPagePrivate(page); |
| 8896 | set_page_private(page, 0); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 8897 | put_page(page); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8898 | } |
| 8899 | return ret; |
| 8900 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8901 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8902 | static int btrfs_releasepage(struct page *page, gfp_t gfp_flags) |
| 8903 | { |
Chris Mason | 98509cf | 2008-09-11 15:51:43 -0400 | [diff] [blame] | 8904 | if (PageWriteback(page) || PageDirty(page)) |
| 8905 | return 0; |
Michal Hocko | 3ba7ab2 | 2017-01-09 15:39:02 +0100 | [diff] [blame] | 8906 | return __btrfs_releasepage(page, gfp_flags); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8907 | } |
| 8908 | |
Lukas Czerner | d47992f | 2013-05-21 23:17:23 -0400 | [diff] [blame] | 8909 | static void btrfs_invalidatepage(struct page *page, unsigned int offset, |
| 8910 | unsigned int length) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8911 | { |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 8912 | struct inode *inode = page->mapping->host; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 8913 | struct extent_io_tree *tree; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8914 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 8915 | struct extent_state *cached_state = NULL; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8916 | u64 page_start = page_offset(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 8917 | u64 page_end = page_start + PAGE_SIZE - 1; |
Chandan Rajendra | dbfdb6d | 2016-01-21 15:55:58 +0530 | [diff] [blame] | 8918 | u64 start; |
| 8919 | u64 end; |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 8920 | int inode_evicting = inode->i_state & I_FREEING; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8921 | |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 8922 | /* |
| 8923 | * we have the page locked, so new writeback can't start, |
| 8924 | * and the dirty bit won't be cleared while we are here. |
| 8925 | * |
| 8926 | * Wait for IO on this page so that we can safely clear |
| 8927 | * the PagePrivate2 bit and do ordered accounting |
| 8928 | */ |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8929 | wait_on_page_writeback(page); |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 8930 | |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 8931 | tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8932 | if (offset) { |
| 8933 | btrfs_releasepage(page, GFP_NOFS); |
| 8934 | return; |
| 8935 | } |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 8936 | |
| 8937 | if (!inode_evicting) |
David Sterba | ff13db4 | 2015-12-03 14:30:40 +0100 | [diff] [blame] | 8938 | lock_extent_bits(tree, page_start, page_end, &cached_state); |
Chandan Rajendra | dbfdb6d | 2016-01-21 15:55:58 +0530 | [diff] [blame] | 8939 | again: |
| 8940 | start = page_start; |
Nikolay Borisov | a776c6f | 2017-02-20 13:50:49 +0200 | [diff] [blame] | 8941 | ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), start, |
Chandan Rajendra | dbfdb6d | 2016-01-21 15:55:58 +0530 | [diff] [blame] | 8942 | page_end - start + 1); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8943 | if (ordered) { |
Chandan Rajendra | dbfdb6d | 2016-01-21 15:55:58 +0530 | [diff] [blame] | 8944 | end = min(page_end, ordered->file_offset + ordered->len - 1); |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 8945 | /* |
| 8946 | * IO on this page will never be started, so we need |
| 8947 | * to account for any ordered extents now |
| 8948 | */ |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 8949 | if (!inode_evicting) |
Chandan Rajendra | dbfdb6d | 2016-01-21 15:55:58 +0530 | [diff] [blame] | 8950 | clear_extent_bit(tree, start, end, |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 8951 | EXTENT_DIRTY | EXTENT_DELALLOC | |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 8952 | EXTENT_DELALLOC_NEW | |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 8953 | EXTENT_LOCKED | EXTENT_DO_ACCOUNTING | |
| 8954 | EXTENT_DEFRAG, 1, 0, &cached_state, |
| 8955 | GFP_NOFS); |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 8956 | /* |
| 8957 | * whoever cleared the private bit is responsible |
| 8958 | * for the finish_ordered_io |
| 8959 | */ |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 8960 | if (TestClearPagePrivate2(page)) { |
| 8961 | struct btrfs_ordered_inode_tree *tree; |
| 8962 | u64 new_len; |
| 8963 | |
| 8964 | tree = &BTRFS_I(inode)->ordered_tree; |
| 8965 | |
| 8966 | spin_lock_irq(&tree->lock); |
| 8967 | set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags); |
Chandan Rajendra | dbfdb6d | 2016-01-21 15:55:58 +0530 | [diff] [blame] | 8968 | new_len = start - ordered->file_offset; |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 8969 | if (new_len < ordered->truncated_len) |
| 8970 | ordered->truncated_len = new_len; |
| 8971 | spin_unlock_irq(&tree->lock); |
| 8972 | |
| 8973 | if (btrfs_dec_test_ordered_pending(inode, &ordered, |
Chandan Rajendra | dbfdb6d | 2016-01-21 15:55:58 +0530 | [diff] [blame] | 8974 | start, |
| 8975 | end - start + 1, 1)) |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 8976 | btrfs_finish_ordered_io(ordered); |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 8977 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8978 | btrfs_put_ordered_extent(ordered); |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 8979 | if (!inode_evicting) { |
| 8980 | cached_state = NULL; |
Chandan Rajendra | dbfdb6d | 2016-01-21 15:55:58 +0530 | [diff] [blame] | 8981 | lock_extent_bits(tree, start, end, |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 8982 | &cached_state); |
| 8983 | } |
Chandan Rajendra | dbfdb6d | 2016-01-21 15:55:58 +0530 | [diff] [blame] | 8984 | |
| 8985 | start = end + 1; |
| 8986 | if (start < page_end) |
| 8987 | goto again; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8988 | } |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 8989 | |
Qu Wenruo | b9d0b38 | 2015-09-29 10:35:16 +0800 | [diff] [blame] | 8990 | /* |
| 8991 | * Qgroup reserved space handler |
| 8992 | * Page here will be either |
| 8993 | * 1) Already written to disk |
| 8994 | * In this case, its reserved space is released from data rsv map |
| 8995 | * and will be freed by delayed_ref handler finally. |
| 8996 | * So even we call qgroup_free_data(), it won't decrease reserved |
| 8997 | * space. |
| 8998 | * 2) Not written to disk |
Goldwyn Rodrigues | 0b34c26 | 2016-09-30 10:40:52 -0500 | [diff] [blame] | 8999 | * This means the reserved space should be freed here. However, |
| 9000 | * if a truncate invalidates the page (by clearing PageDirty) |
| 9001 | * and the page is accounted for while allocating extent |
| 9002 | * in btrfs_check_data_free_space() we let delayed_ref to |
| 9003 | * free the entire extent. |
Qu Wenruo | b9d0b38 | 2015-09-29 10:35:16 +0800 | [diff] [blame] | 9004 | */ |
Goldwyn Rodrigues | 0b34c26 | 2016-09-30 10:40:52 -0500 | [diff] [blame] | 9005 | if (PageDirty(page)) |
| 9006 | btrfs_qgroup_free_data(inode, page_start, PAGE_SIZE); |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 9007 | if (!inode_evicting) { |
| 9008 | clear_extent_bit(tree, page_start, page_end, |
| 9009 | EXTENT_LOCKED | EXTENT_DIRTY | |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 9010 | EXTENT_DELALLOC | EXTENT_DELALLOC_NEW | |
| 9011 | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1, |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 9012 | &cached_state, GFP_NOFS); |
| 9013 | |
| 9014 | __btrfs_releasepage(page, GFP_NOFS); |
| 9015 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 9016 | |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 9017 | ClearPageChecked(page); |
Chris Mason | 9ad6b7b | 2008-04-18 16:11:30 -0400 | [diff] [blame] | 9018 | if (PagePrivate(page)) { |
Chris Mason | 9ad6b7b | 2008-04-18 16:11:30 -0400 | [diff] [blame] | 9019 | ClearPagePrivate(page); |
| 9020 | set_page_private(page, 0); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 9021 | put_page(page); |
Chris Mason | 9ad6b7b | 2008-04-18 16:11:30 -0400 | [diff] [blame] | 9022 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9023 | } |
| 9024 | |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 9025 | /* |
| 9026 | * btrfs_page_mkwrite() is not allowed to change the file size as it gets |
| 9027 | * called from a page fault handler when a page is first dirtied. Hence we must |
| 9028 | * be careful to check for EOF conditions here. We set the page up correctly |
| 9029 | * for a written page which means we get ENOSPC checking when writing into |
| 9030 | * holes and correct delalloc and unwritten extent mapping on filesystems that |
| 9031 | * support these features. |
| 9032 | * |
| 9033 | * We are not allowed to take the i_mutex here so we have to play games to |
| 9034 | * protect against truncate races as the page could now be beyond EOF. Because |
| 9035 | * vmtruncate() writes the inode size before removing pages, once we have the |
| 9036 | * page lock we can determine safely if the page is beyond EOF. If it is not |
| 9037 | * beyond EOF, then the page is guaranteed safe against truncation until we |
| 9038 | * unlock the page. |
| 9039 | */ |
Dave Jiang | 11bac80 | 2017-02-24 14:56:41 -0800 | [diff] [blame] | 9040 | int btrfs_page_mkwrite(struct vm_fault *vmf) |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 9041 | { |
Nick Piggin | c2ec175 | 2009-03-31 15:23:21 -0700 | [diff] [blame] | 9042 | struct page *page = vmf->page; |
Dave Jiang | 11bac80 | 2017-02-24 14:56:41 -0800 | [diff] [blame] | 9043 | struct inode *inode = file_inode(vmf->vma->vm_file); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9044 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 9045 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
| 9046 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 9047 | struct extent_state *cached_state = NULL; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 9048 | char *kaddr; |
| 9049 | unsigned long zero_start; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 9050 | loff_t size; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 9051 | int ret; |
Chris Mason | 9998eb7 | 2012-01-25 13:47:40 -0500 | [diff] [blame] | 9052 | int reserved = 0; |
Chandan Rajendra | d0b7da88 | 2016-01-21 15:55:57 +0530 | [diff] [blame] | 9053 | u64 reserved_space; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 9054 | u64 page_start; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 9055 | u64 page_end; |
Chandan Rajendra | d0b7da88 | 2016-01-21 15:55:57 +0530 | [diff] [blame] | 9056 | u64 end; |
| 9057 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 9058 | reserved_space = PAGE_SIZE; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 9059 | |
Jan Kara | b2b5ef5 | 2012-06-12 16:20:45 +0200 | [diff] [blame] | 9060 | sb_start_pagefault(inode->i_sb); |
Qu Wenruo | df48063 | 2015-09-08 17:25:54 +0800 | [diff] [blame] | 9061 | page_start = page_offset(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 9062 | page_end = page_start + PAGE_SIZE - 1; |
Chandan Rajendra | d0b7da88 | 2016-01-21 15:55:57 +0530 | [diff] [blame] | 9063 | end = page_end; |
Qu Wenruo | df48063 | 2015-09-08 17:25:54 +0800 | [diff] [blame] | 9064 | |
Chandan Rajendra | d0b7da88 | 2016-01-21 15:55:57 +0530 | [diff] [blame] | 9065 | /* |
| 9066 | * Reserving delalloc space after obtaining the page lock can lead to |
| 9067 | * deadlock. For example, if a dirty page is locked by this function |
| 9068 | * and the call to btrfs_delalloc_reserve_space() ends up triggering |
| 9069 | * dirty page write out, then the btrfs_writepage() function could |
| 9070 | * end up waiting indefinitely to get a lock on the page currently |
| 9071 | * being processed by btrfs_page_mkwrite() function. |
| 9072 | */ |
Qu Wenruo | 7cf5b97 | 2015-09-08 17:25:55 +0800 | [diff] [blame] | 9073 | ret = btrfs_delalloc_reserve_space(inode, page_start, |
Chandan Rajendra | d0b7da88 | 2016-01-21 15:55:57 +0530 | [diff] [blame] | 9074 | reserved_space); |
Chris Mason | 9998eb7 | 2012-01-25 13:47:40 -0500 | [diff] [blame] | 9075 | if (!ret) { |
Dave Jiang | 11bac80 | 2017-02-24 14:56:41 -0800 | [diff] [blame] | 9076 | ret = file_update_time(vmf->vma->vm_file); |
Chris Mason | 9998eb7 | 2012-01-25 13:47:40 -0500 | [diff] [blame] | 9077 | reserved = 1; |
| 9078 | } |
Nick Piggin | 56a76f8 | 2009-03-31 15:23:23 -0700 | [diff] [blame] | 9079 | if (ret) { |
| 9080 | if (ret == -ENOMEM) |
| 9081 | ret = VM_FAULT_OOM; |
| 9082 | else /* -ENOSPC, -EIO, etc */ |
| 9083 | ret = VM_FAULT_SIGBUS; |
Chris Mason | 9998eb7 | 2012-01-25 13:47:40 -0500 | [diff] [blame] | 9084 | if (reserved) |
| 9085 | goto out; |
| 9086 | goto out_noreserve; |
Nick Piggin | 56a76f8 | 2009-03-31 15:23:23 -0700 | [diff] [blame] | 9087 | } |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 9088 | |
Nick Piggin | 56a76f8 | 2009-03-31 15:23:23 -0700 | [diff] [blame] | 9089 | ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */ |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 9090 | again: |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 9091 | lock_page(page); |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 9092 | size = i_size_read(inode); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 9093 | |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 9094 | if ((page->mapping != inode->i_mapping) || |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 9095 | (page_start >= size)) { |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 9096 | /* page got truncated out from underneath us */ |
| 9097 | goto out_unlock; |
| 9098 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 9099 | wait_on_page_writeback(page); |
| 9100 | |
David Sterba | ff13db4 | 2015-12-03 14:30:40 +0100 | [diff] [blame] | 9101 | lock_extent_bits(io_tree, page_start, page_end, &cached_state); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 9102 | set_page_extent_mapped(page); |
| 9103 | |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 9104 | /* |
| 9105 | * we can't set the delalloc bits if there are pending ordered |
| 9106 | * extents. Drop our locks and wait for them to finish |
| 9107 | */ |
Nikolay Borisov | a776c6f | 2017-02-20 13:50:49 +0200 | [diff] [blame] | 9108 | ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start, |
| 9109 | PAGE_SIZE); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 9110 | if (ordered) { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 9111 | unlock_extent_cached(io_tree, page_start, page_end, |
| 9112 | &cached_state, GFP_NOFS); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 9113 | unlock_page(page); |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 9114 | btrfs_start_ordered_extent(inode, ordered, 1); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 9115 | btrfs_put_ordered_extent(ordered); |
| 9116 | goto again; |
| 9117 | } |
| 9118 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 9119 | if (page->index == ((size - 1) >> PAGE_SHIFT)) { |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 9120 | reserved_space = round_up(size - page_start, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9121 | fs_info->sectorsize); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 9122 | if (reserved_space < PAGE_SIZE) { |
Chandan Rajendra | d0b7da88 | 2016-01-21 15:55:57 +0530 | [diff] [blame] | 9123 | end = page_start + reserved_space - 1; |
| 9124 | spin_lock(&BTRFS_I(inode)->lock); |
| 9125 | BTRFS_I(inode)->outstanding_extents++; |
| 9126 | spin_unlock(&BTRFS_I(inode)->lock); |
| 9127 | btrfs_delalloc_release_space(inode, page_start, |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 9128 | PAGE_SIZE - reserved_space); |
Chandan Rajendra | d0b7da88 | 2016-01-21 15:55:57 +0530 | [diff] [blame] | 9129 | } |
| 9130 | } |
| 9131 | |
Josef Bacik | fbf1908 | 2009-10-01 17:10:23 -0400 | [diff] [blame] | 9132 | /* |
Liu Bo | 5416034 | 2016-12-13 12:15:19 -0800 | [diff] [blame] | 9133 | * page_mkwrite gets called when the page is firstly dirtied after it's |
| 9134 | * faulted in, but write(2) could also dirty a page and set delalloc |
| 9135 | * bits, thus in this case for space account reason, we still need to |
| 9136 | * clear any delalloc bits within this page range since we have to |
| 9137 | * reserve data&meta space before lock_page() (see above comments). |
Josef Bacik | fbf1908 | 2009-10-01 17:10:23 -0400 | [diff] [blame] | 9138 | */ |
Chandan Rajendra | d0b7da88 | 2016-01-21 15:55:57 +0530 | [diff] [blame] | 9139 | clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, end, |
Liu Bo | 9e8a4a8 | 2012-09-05 19:10:51 -0600 | [diff] [blame] | 9140 | EXTENT_DIRTY | EXTENT_DELALLOC | |
| 9141 | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 9142 | 0, 0, &cached_state, GFP_NOFS); |
Josef Bacik | fbf1908 | 2009-10-01 17:10:23 -0400 | [diff] [blame] | 9143 | |
Chandan Rajendra | d0b7da88 | 2016-01-21 15:55:57 +0530 | [diff] [blame] | 9144 | ret = btrfs_set_extent_delalloc(inode, page_start, end, |
Qu Wenruo | ba8b04c | 2016-07-19 16:50:36 +0800 | [diff] [blame] | 9145 | &cached_state, 0); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 9146 | if (ret) { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 9147 | unlock_extent_cached(io_tree, page_start, page_end, |
| 9148 | &cached_state, GFP_NOFS); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 9149 | ret = VM_FAULT_SIGBUS; |
| 9150 | goto out_unlock; |
| 9151 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 9152 | ret = 0; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 9153 | |
| 9154 | /* page is wholly or partially inside EOF */ |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 9155 | if (page_start + PAGE_SIZE > size) |
| 9156 | zero_start = size & ~PAGE_MASK; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 9157 | else |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 9158 | zero_start = PAGE_SIZE; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 9159 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 9160 | if (zero_start != PAGE_SIZE) { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 9161 | kaddr = kmap(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 9162 | memset(kaddr + zero_start, 0, PAGE_SIZE - zero_start); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 9163 | flush_dcache_page(page); |
| 9164 | kunmap(page); |
| 9165 | } |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 9166 | ClearPageChecked(page); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 9167 | set_page_dirty(page); |
Chris Mason | 50a9b21 | 2009-09-11 12:33:12 -0400 | [diff] [blame] | 9168 | SetPageUptodate(page); |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 9169 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9170 | BTRFS_I(inode)->last_trans = fs_info->generation; |
Chris Mason | 257c62e | 2009-10-13 13:21:08 -0400 | [diff] [blame] | 9171 | BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid; |
Liu Bo | 46d8bc3 | 2012-08-29 01:07:55 -0600 | [diff] [blame] | 9172 | 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] | 9173 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 9174 | 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] | 9175 | |
| 9176 | out_unlock: |
Jan Kara | b2b5ef5 | 2012-06-12 16:20:45 +0200 | [diff] [blame] | 9177 | if (!ret) { |
| 9178 | sb_end_pagefault(inode->i_sb); |
Chris Mason | 50a9b21 | 2009-09-11 12:33:12 -0400 | [diff] [blame] | 9179 | return VM_FAULT_LOCKED; |
Jan Kara | b2b5ef5 | 2012-06-12 16:20:45 +0200 | [diff] [blame] | 9180 | } |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 9181 | unlock_page(page); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 9182 | out: |
Chandan Rajendra | d0b7da88 | 2016-01-21 15:55:57 +0530 | [diff] [blame] | 9183 | btrfs_delalloc_release_space(inode, page_start, reserved_space); |
Chris Mason | 9998eb7 | 2012-01-25 13:47:40 -0500 | [diff] [blame] | 9184 | out_noreserve: |
Jan Kara | b2b5ef5 | 2012-06-12 16:20:45 +0200 | [diff] [blame] | 9185 | sb_end_pagefault(inode->i_sb); |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 9186 | return ret; |
| 9187 | } |
| 9188 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 9189 | static int btrfs_truncate(struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9190 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9191 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9192 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 9193 | struct btrfs_block_rsv *rsv; |
Josef Bacik | a71754f | 2013-06-17 17:14:39 -0400 | [diff] [blame] | 9194 | int ret = 0; |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 9195 | int err = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9196 | struct btrfs_trans_handle *trans; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9197 | u64 mask = fs_info->sectorsize - 1; |
| 9198 | u64 min_size = btrfs_calc_trunc_metadata_size(fs_info, 1); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9199 | |
Josef Bacik | 0ef8b72 | 2013-10-25 16:13:35 -0400 | [diff] [blame] | 9200 | ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask), |
| 9201 | (u64)-1); |
| 9202 | if (ret) |
| 9203 | return ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9204 | |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 9205 | /* |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 9206 | * 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] | 9207 | * 3 things going on here |
| 9208 | * |
| 9209 | * 1) We need to reserve space for our orphan item and the space to |
| 9210 | * delete our orphan item. Lord knows we don't want to have a dangling |
| 9211 | * orphan item because we didn't reserve space to remove it. |
| 9212 | * |
| 9213 | * 2) We need to reserve space to update our inode. |
| 9214 | * |
| 9215 | * 3) We need to have something to cache all the space that is going to |
| 9216 | * be free'd up by the truncate operation, but also have some slack |
| 9217 | * space reserved in case it uses space during the truncate (thank you |
| 9218 | * very much snapshotting). |
| 9219 | * |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 9220 | * 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] | 9221 | * 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] | 9222 | * 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] | 9223 | * doesn't end up using space reserved for updating the inode or |
| 9224 | * removing the orphan item. We also need to be able to stop the |
| 9225 | * transaction and start a new one, which means we need to be able to |
| 9226 | * update the inode several times, and we have no idea of knowing how |
| 9227 | * 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] | 9228 | * 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] | 9229 | * Then there is the orphan item, which does indeed need to be held on |
| 9230 | * to for the whole operation, and we need nobody to touch this reserved |
| 9231 | * space except the orphan code. |
| 9232 | * |
| 9233 | * So that leaves us with |
| 9234 | * |
| 9235 | * 1) root->orphan_block_rsv - for the orphan deletion. |
| 9236 | * 2) rsv - for the truncate reservation, which we will steal from the |
| 9237 | * transaction reservation. |
| 9238 | * 3) fs_info->trans_block_rsv - this will have 1 items worth left for |
| 9239 | * updating the inode. |
| 9240 | */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 9241 | rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP); |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 9242 | if (!rsv) |
| 9243 | return -ENOMEM; |
Josef Bacik | 4a33854 | 2011-08-29 11:01:31 -0400 | [diff] [blame] | 9244 | rsv->size = min_size; |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 9245 | rsv->failfast = 1; |
Josef Bacik | f0cd846 | 2011-03-04 14:37:08 -0500 | [diff] [blame] | 9246 | |
Josef Bacik | 907cbce | 2011-08-08 13:46:15 -0400 | [diff] [blame] | 9247 | /* |
Josef Bacik | 0712718 | 2011-08-19 10:29:59 -0400 | [diff] [blame] | 9248 | * 1 for the truncate slack space |
Josef Bacik | 907cbce | 2011-08-08 13:46:15 -0400 | [diff] [blame] | 9249 | * 1 for updating the inode. |
| 9250 | */ |
Josef Bacik | f3fe820 | 2013-01-07 17:03:21 -0500 | [diff] [blame] | 9251 | trans = btrfs_start_transaction(root, 2); |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 9252 | if (IS_ERR(trans)) { |
| 9253 | err = PTR_ERR(trans); |
| 9254 | goto out; |
| 9255 | } |
Josef Bacik | f0cd846 | 2011-03-04 14:37:08 -0500 | [diff] [blame] | 9256 | |
Josef Bacik | 907cbce | 2011-08-08 13:46:15 -0400 | [diff] [blame] | 9257 | /* Migrate the slack space for the truncate to our reserve */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9258 | ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, rsv, |
Josef Bacik | 25d609f | 2016-03-25 13:25:48 -0400 | [diff] [blame] | 9259 | min_size, 0); |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 9260 | BUG_ON(ret); |
Josef Bacik | f0cd846 | 2011-03-04 14:37:08 -0500 | [diff] [blame] | 9261 | |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 9262 | /* |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 9263 | * So if we truncate and then write and fsync we normally would just |
| 9264 | * write the extents that changed, which is a problem if we need to |
| 9265 | * first truncate that entire inode. So set this flag so we write out |
| 9266 | * all of the extents in the inode to the sync log so we're completely |
| 9267 | * safe. |
| 9268 | */ |
| 9269 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags); |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 9270 | trans->block_rsv = rsv; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 9271 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 9272 | while (1) { |
| 9273 | ret = btrfs_truncate_inode_items(trans, root, inode, |
| 9274 | inode->i_size, |
| 9275 | BTRFS_EXTENT_DATA_KEY); |
Chris Mason | 28ed134 | 2014-12-17 09:41:04 -0800 | [diff] [blame] | 9276 | if (ret != -ENOSPC && ret != -EAGAIN) { |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 9277 | err = ret; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 9278 | break; |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 9279 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9280 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9281 | trans->block_rsv = &fs_info->trans_block_rsv; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 9282 | ret = btrfs_update_inode(trans, root, inode); |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 9283 | if (ret) { |
| 9284 | err = ret; |
| 9285 | break; |
| 9286 | } |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 9287 | |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 9288 | btrfs_end_transaction(trans); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 9289 | btrfs_btree_balance_dirty(fs_info); |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 9290 | |
| 9291 | trans = btrfs_start_transaction(root, 2); |
| 9292 | if (IS_ERR(trans)) { |
| 9293 | ret = err = PTR_ERR(trans); |
| 9294 | trans = NULL; |
| 9295 | break; |
| 9296 | } |
| 9297 | |
Wang Xiaoguang | 47b5d64 | 2016-09-07 20:17:38 +0800 | [diff] [blame] | 9298 | btrfs_block_rsv_release(fs_info, rsv, -1); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9299 | ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, |
Josef Bacik | 25d609f | 2016-03-25 13:25:48 -0400 | [diff] [blame] | 9300 | rsv, min_size, 0); |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 9301 | BUG_ON(ret); /* shouldn't happen */ |
| 9302 | trans->block_rsv = rsv; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 9303 | } |
| 9304 | |
| 9305 | if (ret == 0 && inode->i_nlink > 0) { |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 9306 | trans->block_rsv = root->orphan_block_rsv; |
Nikolay Borisov | 3d6ae7b | 2017-02-20 13:50:58 +0200 | [diff] [blame] | 9307 | ret = btrfs_orphan_del(trans, BTRFS_I(inode)); |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 9308 | if (ret) |
| 9309 | err = ret; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 9310 | } |
| 9311 | |
Chris Mason | 917c16b | 2011-11-08 14:49:59 -0500 | [diff] [blame] | 9312 | if (trans) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9313 | trans->block_rsv = &fs_info->trans_block_rsv; |
Chris Mason | 917c16b | 2011-11-08 14:49:59 -0500 | [diff] [blame] | 9314 | ret = btrfs_update_inode(trans, root, inode); |
| 9315 | if (ret && !err) |
| 9316 | err = ret; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 9317 | |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 9318 | ret = btrfs_end_transaction(trans); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 9319 | btrfs_btree_balance_dirty(fs_info); |
Chris Mason | 917c16b | 2011-11-08 14:49:59 -0500 | [diff] [blame] | 9320 | } |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 9321 | out: |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 9322 | btrfs_free_block_rsv(fs_info, rsv); |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 9323 | |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 9324 | if (ret && !err) |
| 9325 | err = ret; |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 9326 | |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 9327 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9328 | } |
| 9329 | |
Sven Wegener | 3b96362 | 2008-06-09 21:57:42 -0400 | [diff] [blame] | 9330 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 9331 | * create a new subvolume directory/inode (helper for the ioctl). |
| 9332 | */ |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 9333 | int btrfs_create_subvol_root(struct btrfs_trans_handle *trans, |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 9334 | struct btrfs_root *new_root, |
| 9335 | struct btrfs_root *parent_root, |
| 9336 | u64 new_dirid) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9337 | { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9338 | struct inode *inode; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 9339 | int err; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 9340 | u64 index = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9341 | |
Florian Albrechtskirchinger | 12fc9d0 | 2012-02-10 22:15:54 +0100 | [diff] [blame] | 9342 | inode = btrfs_new_inode(trans, new_root, NULL, "..", 2, |
| 9343 | new_dirid, new_dirid, |
| 9344 | S_IFDIR | (~current_umask() & S_IRWXUGO), |
| 9345 | &index); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 9346 | if (IS_ERR(inode)) |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 9347 | return PTR_ERR(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9348 | inode->i_op = &btrfs_dir_inode_operations; |
| 9349 | inode->i_fop = &btrfs_dir_file_operations; |
| 9350 | |
Miklos Szeredi | bfe8684 | 2011-10-28 14:13:29 +0200 | [diff] [blame] | 9351 | set_nlink(inode, 1); |
Nikolay Borisov | 6ef06d2 | 2017-02-20 13:50:34 +0200 | [diff] [blame] | 9352 | btrfs_i_size_write(BTRFS_I(inode), 0); |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 9353 | unlock_new_inode(inode); |
Sven Wegener | 3b96362 | 2008-06-09 21:57:42 -0400 | [diff] [blame] | 9354 | |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 9355 | err = btrfs_subvol_inherit_props(trans, new_root, parent_root); |
| 9356 | if (err) |
| 9357 | btrfs_err(new_root->fs_info, |
David Sterba | 351fd35 | 2014-05-15 16:48:20 +0200 | [diff] [blame] | 9358 | "error inheriting subvolume %llu properties: %d", |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 9359 | new_root->root_key.objectid, err); |
| 9360 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 9361 | err = btrfs_update_inode(trans, new_root, inode); |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 9362 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 9363 | iput(inode); |
Mark Fasheh | ce59897 | 2011-07-26 11:32:23 -0700 | [diff] [blame] | 9364 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9365 | } |
| 9366 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9367 | struct inode *btrfs_alloc_inode(struct super_block *sb) |
| 9368 | { |
| 9369 | struct btrfs_inode *ei; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9370 | struct inode *inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9371 | |
| 9372 | ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS); |
| 9373 | if (!ei) |
| 9374 | return NULL; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9375 | |
| 9376 | ei->root = NULL; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9377 | ei->generation = 0; |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 9378 | ei->last_trans = 0; |
Chris Mason | 257c62e | 2009-10-13 13:21:08 -0400 | [diff] [blame] | 9379 | ei->last_sub_trans = 0; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 9380 | ei->logged_trans = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9381 | ei->delalloc_bytes = 0; |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 9382 | ei->new_delalloc_bytes = 0; |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 9383 | ei->defrag_bytes = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9384 | ei->disk_i_size = 0; |
| 9385 | ei->flags = 0; |
Josef Bacik | 7709cde | 2011-08-04 10:25:02 -0400 | [diff] [blame] | 9386 | ei->csum_bytes = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9387 | ei->index_cnt = (u64)-1; |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 9388 | ei->dir_index = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9389 | ei->last_unlink_trans = 0; |
Liu Bo | 46d8bc3 | 2012-08-29 01:07:55 -0600 | [diff] [blame] | 9390 | ei->last_log_commit = 0; |
David Sterba | 8089fe6 | 2015-11-19 14:15:51 +0100 | [diff] [blame] | 9391 | ei->delayed_iput_count = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9392 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 9393 | spin_lock_init(&ei->lock); |
| 9394 | ei->outstanding_extents = 0; |
| 9395 | ei->reserved_extents = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9396 | |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 9397 | ei->runtime_flags = 0; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 9398 | ei->force_compress = BTRFS_COMPRESS_NONE; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9399 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 9400 | ei->delayed_node = NULL; |
| 9401 | |
chandan r | 9cc97d6 | 2012-07-04 12:48:07 +0530 | [diff] [blame] | 9402 | ei->i_otime.tv_sec = 0; |
| 9403 | ei->i_otime.tv_nsec = 0; |
| 9404 | |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9405 | inode = &ei->vfs_inode; |
David Sterba | a8067e0 | 2011-04-21 00:34:43 +0200 | [diff] [blame] | 9406 | extent_map_tree_init(&ei->extent_tree); |
David Sterba | f993c88 | 2011-04-20 23:35:57 +0200 | [diff] [blame] | 9407 | extent_io_tree_init(&ei->io_tree, &inode->i_data); |
| 9408 | extent_io_tree_init(&ei->io_failure_tree, &inode->i_data); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 9409 | ei->io_tree.track_uptodate = 1; |
| 9410 | ei->io_failure_tree.track_uptodate = 1; |
Josef Bacik | b812ce2 | 2012-11-16 13:56:32 -0500 | [diff] [blame] | 9411 | atomic_set(&ei->sync_writers, 0); |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9412 | mutex_init(&ei->log_mutex); |
Josef Bacik | f248679 | 2012-01-13 12:09:22 -0500 | [diff] [blame] | 9413 | mutex_init(&ei->delalloc_mutex); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 9414 | btrfs_ordered_inode_tree_init(&ei->ordered_tree); |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9415 | INIT_LIST_HEAD(&ei->delalloc_inodes); |
David Sterba | 8089fe6 | 2015-11-19 14:15:51 +0100 | [diff] [blame] | 9416 | INIT_LIST_HEAD(&ei->delayed_iput); |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9417 | RB_CLEAR_NODE(&ei->rb_node); |
Filipe Manana | 5f9a8a5 | 2016-05-12 13:53:36 +0100 | [diff] [blame] | 9418 | init_rwsem(&ei->dio_sem); |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9419 | |
| 9420 | return inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9421 | } |
| 9422 | |
Josef Bacik | aaedb55 | 2013-10-11 14:44:09 -0400 | [diff] [blame] | 9423 | #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS |
| 9424 | void btrfs_test_destroy_inode(struct inode *inode) |
| 9425 | { |
Nikolay Borisov | dcdbc05 | 2017-02-20 13:50:45 +0200 | [diff] [blame] | 9426 | btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0); |
Josef Bacik | aaedb55 | 2013-10-11 14:44:09 -0400 | [diff] [blame] | 9427 | kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode)); |
| 9428 | } |
| 9429 | #endif |
| 9430 | |
Nick Piggin | fa0d7e3d | 2011-01-07 17:49:49 +1100 | [diff] [blame] | 9431 | static void btrfs_i_callback(struct rcu_head *head) |
| 9432 | { |
| 9433 | struct inode *inode = container_of(head, struct inode, i_rcu); |
Nick Piggin | fa0d7e3d | 2011-01-07 17:49:49 +1100 | [diff] [blame] | 9434 | kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode)); |
| 9435 | } |
| 9436 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9437 | void btrfs_destroy_inode(struct inode *inode) |
| 9438 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9439 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 9440 | struct btrfs_ordered_extent *ordered; |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 9441 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 9442 | |
Al Viro | b3d9b7a | 2012-06-09 13:51:19 -0400 | [diff] [blame] | 9443 | WARN_ON(!hlist_empty(&inode->i_dentry)); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9444 | WARN_ON(inode->i_data.nrpages); |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 9445 | WARN_ON(BTRFS_I(inode)->outstanding_extents); |
| 9446 | WARN_ON(BTRFS_I(inode)->reserved_extents); |
Josef Bacik | 7709cde | 2011-08-04 10:25:02 -0400 | [diff] [blame] | 9447 | WARN_ON(BTRFS_I(inode)->delalloc_bytes); |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 9448 | WARN_ON(BTRFS_I(inode)->new_delalloc_bytes); |
Josef Bacik | 7709cde | 2011-08-04 10:25:02 -0400 | [diff] [blame] | 9449 | WARN_ON(BTRFS_I(inode)->csum_bytes); |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 9450 | WARN_ON(BTRFS_I(inode)->defrag_bytes); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9451 | |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 9452 | /* |
Josef Bacik | a6dbd42 | 2009-11-11 15:53:34 -0500 | [diff] [blame] | 9453 | * This can happen where we create an inode, but somebody else also |
| 9454 | * created the same inode and we need to destroy the one we already |
| 9455 | * created. |
| 9456 | */ |
| 9457 | if (!root) |
| 9458 | goto free; |
| 9459 | |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 9460 | if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
| 9461 | &BTRFS_I(inode)->runtime_flags)) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9462 | btrfs_info(fs_info, "inode %llu still on the orphan list", |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 9463 | btrfs_ino(BTRFS_I(inode))); |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 9464 | atomic_dec(&root->orphan_inodes); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 9465 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 9466 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 9467 | while (1) { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 9468 | ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1); |
| 9469 | if (!ordered) |
| 9470 | break; |
| 9471 | else { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9472 | btrfs_err(fs_info, |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 9473 | "found ordered extent %llu %llu on inode cleanup", |
| 9474 | ordered->file_offset, ordered->len); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 9475 | btrfs_remove_ordered_extent(inode, ordered); |
| 9476 | btrfs_put_ordered_extent(ordered); |
| 9477 | btrfs_put_ordered_extent(ordered); |
| 9478 | } |
| 9479 | } |
Qu Wenruo | 56fa9d0 | 2015-10-13 09:53:10 +0800 | [diff] [blame] | 9480 | btrfs_qgroup_check_reserved_leak(inode); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 9481 | inode_tree_del(inode); |
Nikolay Borisov | dcdbc05 | 2017-02-20 13:50:45 +0200 | [diff] [blame] | 9482 | btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0); |
Josef Bacik | a6dbd42 | 2009-11-11 15:53:34 -0500 | [diff] [blame] | 9483 | free: |
Nick Piggin | fa0d7e3d | 2011-01-07 17:49:49 +1100 | [diff] [blame] | 9484 | call_rcu(&inode->i_rcu, btrfs_i_callback); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9485 | } |
| 9486 | |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 9487 | int btrfs_drop_inode(struct inode *inode) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 9488 | { |
| 9489 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 9490 | |
Naohiro Aota | 6379ef9 | 2013-06-06 09:56:34 +0000 | [diff] [blame] | 9491 | if (root == NULL) |
| 9492 | return 1; |
| 9493 | |
Liu Bo | fa6ac87 | 2013-02-20 14:10:23 +0000 | [diff] [blame] | 9494 | /* the snap/subvol tree is on deleting */ |
Stefan Behrens | 69e9c6c | 2013-09-05 16:58:43 +0200 | [diff] [blame] | 9495 | if (btrfs_root_refs(&root->root_item) == 0) |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 9496 | return 1; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 9497 | else |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 9498 | return generic_drop_inode(inode); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 9499 | } |
| 9500 | |
Sven Wegener | 0ee0fda | 2008-07-30 16:54:26 -0400 | [diff] [blame] | 9501 | static void init_once(void *foo) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9502 | { |
| 9503 | struct btrfs_inode *ei = (struct btrfs_inode *) foo; |
| 9504 | |
| 9505 | inode_init_once(&ei->vfs_inode); |
| 9506 | } |
| 9507 | |
| 9508 | void btrfs_destroy_cachep(void) |
| 9509 | { |
Kirill A. Shutemov | 8c0a853 | 2012-09-26 11:33:07 +1000 | [diff] [blame] | 9510 | /* |
| 9511 | * Make sure all delayed rcu free inodes are flushed before we |
| 9512 | * destroy cache. |
| 9513 | */ |
| 9514 | rcu_barrier(); |
Kinglong Mee | 5598e90 | 2016-01-29 21:36:35 +0800 | [diff] [blame] | 9515 | kmem_cache_destroy(btrfs_inode_cachep); |
| 9516 | kmem_cache_destroy(btrfs_trans_handle_cachep); |
| 9517 | kmem_cache_destroy(btrfs_transaction_cachep); |
| 9518 | kmem_cache_destroy(btrfs_path_cachep); |
| 9519 | kmem_cache_destroy(btrfs_free_space_cachep); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9520 | } |
| 9521 | |
| 9522 | int btrfs_init_cachep(void) |
| 9523 | { |
David Sterba | 837e197 | 2012-09-07 03:00:48 -0600 | [diff] [blame] | 9524 | btrfs_inode_cachep = kmem_cache_create("btrfs_inode", |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 9525 | sizeof(struct btrfs_inode), 0, |
Vladimir Davydov | 5d09705 | 2016-01-14 15:18:21 -0800 | [diff] [blame] | 9526 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT, |
| 9527 | init_once); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9528 | if (!btrfs_inode_cachep) |
| 9529 | goto fail; |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 9530 | |
David Sterba | 837e197 | 2012-09-07 03:00:48 -0600 | [diff] [blame] | 9531 | btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle", |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 9532 | sizeof(struct btrfs_trans_handle), 0, |
Nikolay Borisov | fba4b69 | 2016-06-23 21:17:08 +0300 | [diff] [blame] | 9533 | SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9534 | if (!btrfs_trans_handle_cachep) |
| 9535 | goto fail; |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 9536 | |
David Sterba | 837e197 | 2012-09-07 03:00:48 -0600 | [diff] [blame] | 9537 | btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction", |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 9538 | sizeof(struct btrfs_transaction), 0, |
Nikolay Borisov | fba4b69 | 2016-06-23 21:17:08 +0300 | [diff] [blame] | 9539 | SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9540 | if (!btrfs_transaction_cachep) |
| 9541 | goto fail; |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 9542 | |
David Sterba | 837e197 | 2012-09-07 03:00:48 -0600 | [diff] [blame] | 9543 | btrfs_path_cachep = kmem_cache_create("btrfs_path", |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 9544 | sizeof(struct btrfs_path), 0, |
Nikolay Borisov | fba4b69 | 2016-06-23 21:17:08 +0300 | [diff] [blame] | 9545 | SLAB_MEM_SPREAD, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9546 | if (!btrfs_path_cachep) |
| 9547 | goto fail; |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 9548 | |
David Sterba | 837e197 | 2012-09-07 03:00:48 -0600 | [diff] [blame] | 9549 | btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space", |
Josef Bacik | dc89e98 | 2011-01-28 17:05:48 -0500 | [diff] [blame] | 9550 | sizeof(struct btrfs_free_space), 0, |
Nikolay Borisov | fba4b69 | 2016-06-23 21:17:08 +0300 | [diff] [blame] | 9551 | SLAB_MEM_SPREAD, NULL); |
Josef Bacik | dc89e98 | 2011-01-28 17:05:48 -0500 | [diff] [blame] | 9552 | if (!btrfs_free_space_cachep) |
| 9553 | goto fail; |
| 9554 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9555 | return 0; |
| 9556 | fail: |
| 9557 | btrfs_destroy_cachep(); |
| 9558 | return -ENOMEM; |
| 9559 | } |
| 9560 | |
David Howells | a528d35 | 2017-01-31 16:46:22 +0000 | [diff] [blame] | 9561 | static int btrfs_getattr(const struct path *path, struct kstat *stat, |
| 9562 | u32 request_mask, unsigned int flags) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9563 | { |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 9564 | u64 delalloc_bytes; |
David Howells | a528d35 | 2017-01-31 16:46:22 +0000 | [diff] [blame] | 9565 | struct inode *inode = d_inode(path->dentry); |
David Sterba | fadc0d8 | 2011-11-20 07:33:38 -0500 | [diff] [blame] | 9566 | u32 blocksize = inode->i_sb->s_blocksize; |
| 9567 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9568 | generic_fillattr(inode, stat); |
Al Viro | 0ee5dc6 | 2011-07-07 15:44:25 -0400 | [diff] [blame] | 9569 | stat->dev = BTRFS_I(inode)->root->anon_dev; |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 9570 | |
| 9571 | spin_lock(&BTRFS_I(inode)->lock); |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 9572 | delalloc_bytes = BTRFS_I(inode)->new_delalloc_bytes; |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 9573 | spin_unlock(&BTRFS_I(inode)->lock); |
David Sterba | fadc0d8 | 2011-11-20 07:33:38 -0500 | [diff] [blame] | 9574 | stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) + |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 9575 | ALIGN(delalloc_bytes, blocksize)) >> 9; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9576 | return 0; |
| 9577 | } |
| 9578 | |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9579 | static int btrfs_rename_exchange(struct inode *old_dir, |
| 9580 | struct dentry *old_dentry, |
| 9581 | struct inode *new_dir, |
| 9582 | struct dentry *new_dentry) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9583 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9584 | struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9585 | struct btrfs_trans_handle *trans; |
| 9586 | struct btrfs_root *root = BTRFS_I(old_dir)->root; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9587 | struct btrfs_root *dest = BTRFS_I(new_dir)->root; |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9588 | struct inode *new_inode = new_dentry->d_inode; |
| 9589 | struct inode *old_inode = old_dentry->d_inode; |
Deepa Dinamani | c2050a4 | 2016-09-14 07:48:06 -0700 | [diff] [blame] | 9590 | struct timespec ctime = current_time(old_inode); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9591 | struct dentry *parent; |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 9592 | u64 old_ino = btrfs_ino(BTRFS_I(old_inode)); |
| 9593 | u64 new_ino = btrfs_ino(BTRFS_I(new_inode)); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9594 | u64 old_idx = 0; |
| 9595 | u64 new_idx = 0; |
| 9596 | u64 root_objectid; |
| 9597 | int ret; |
Filipe Manana | 86e8aa0 | 2016-05-05 02:02:27 +0100 | [diff] [blame] | 9598 | bool root_log_pinned = false; |
| 9599 | bool dest_log_pinned = false; |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9600 | |
| 9601 | /* we only allow rename subvolume link between subvolumes */ |
| 9602 | if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest) |
| 9603 | return -EXDEV; |
| 9604 | |
| 9605 | /* close the race window with snapshot create/destroy ioctl */ |
| 9606 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9607 | down_read(&fs_info->subvol_sem); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9608 | if (new_ino == BTRFS_FIRST_FREE_OBJECTID) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9609 | down_read(&fs_info->subvol_sem); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9610 | |
| 9611 | /* |
| 9612 | * We want to reserve the absolute worst case amount of items. So if |
| 9613 | * both inodes are subvols and we need to unlink them then that would |
| 9614 | * require 4 item modifications, but if they are both normal inodes it |
| 9615 | * would require 5 item modifications, so we'll assume their normal |
| 9616 | * inodes. So 5 * 2 is 10, plus 2 for the new links, so 12 total items |
| 9617 | * should cover the worst case number of items we'll modify. |
| 9618 | */ |
| 9619 | trans = btrfs_start_transaction(root, 12); |
| 9620 | if (IS_ERR(trans)) { |
| 9621 | ret = PTR_ERR(trans); |
| 9622 | goto out_notrans; |
| 9623 | } |
| 9624 | |
| 9625 | /* |
| 9626 | * We need to find a free sequence number both in the source and |
| 9627 | * in the destination directory for the exchange. |
| 9628 | */ |
Nikolay Borisov | 877574e | 2017-02-20 13:50:33 +0200 | [diff] [blame] | 9629 | ret = btrfs_set_inode_index(BTRFS_I(new_dir), &old_idx); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9630 | if (ret) |
| 9631 | goto out_fail; |
Nikolay Borisov | 877574e | 2017-02-20 13:50:33 +0200 | [diff] [blame] | 9632 | ret = btrfs_set_inode_index(BTRFS_I(old_dir), &new_idx); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9633 | if (ret) |
| 9634 | goto out_fail; |
| 9635 | |
| 9636 | BTRFS_I(old_inode)->dir_index = 0ULL; |
| 9637 | BTRFS_I(new_inode)->dir_index = 0ULL; |
| 9638 | |
| 9639 | /* Reference for the source. */ |
| 9640 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) { |
| 9641 | /* force full log commit if subvolume involved. */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9642 | btrfs_set_log_full_commit(fs_info, trans); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9643 | } else { |
Filipe Manana | 376e5a5 | 2016-05-05 02:08:56 +0100 | [diff] [blame] | 9644 | btrfs_pin_log_trans(root); |
| 9645 | root_log_pinned = true; |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9646 | ret = btrfs_insert_inode_ref(trans, dest, |
| 9647 | new_dentry->d_name.name, |
| 9648 | new_dentry->d_name.len, |
| 9649 | old_ino, |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 9650 | btrfs_ino(BTRFS_I(new_dir)), |
| 9651 | old_idx); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9652 | if (ret) |
| 9653 | goto out_fail; |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9654 | } |
| 9655 | |
| 9656 | /* And now for the dest. */ |
| 9657 | if (new_ino == BTRFS_FIRST_FREE_OBJECTID) { |
| 9658 | /* force full log commit if subvolume involved. */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9659 | btrfs_set_log_full_commit(fs_info, trans); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9660 | } else { |
Filipe Manana | 376e5a5 | 2016-05-05 02:08:56 +0100 | [diff] [blame] | 9661 | btrfs_pin_log_trans(dest); |
| 9662 | dest_log_pinned = true; |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9663 | ret = btrfs_insert_inode_ref(trans, root, |
| 9664 | old_dentry->d_name.name, |
| 9665 | old_dentry->d_name.len, |
| 9666 | new_ino, |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 9667 | btrfs_ino(BTRFS_I(old_dir)), |
| 9668 | new_idx); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9669 | if (ret) |
| 9670 | goto out_fail; |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9671 | } |
| 9672 | |
| 9673 | /* Update inode version and ctime/mtime. */ |
| 9674 | inode_inc_iversion(old_dir); |
| 9675 | inode_inc_iversion(new_dir); |
| 9676 | inode_inc_iversion(old_inode); |
| 9677 | inode_inc_iversion(new_inode); |
| 9678 | old_dir->i_ctime = old_dir->i_mtime = ctime; |
| 9679 | new_dir->i_ctime = new_dir->i_mtime = ctime; |
| 9680 | old_inode->i_ctime = ctime; |
| 9681 | new_inode->i_ctime = ctime; |
| 9682 | |
| 9683 | if (old_dentry->d_parent != new_dentry->d_parent) { |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 9684 | btrfs_record_unlink_dir(trans, BTRFS_I(old_dir), |
| 9685 | BTRFS_I(old_inode), 1); |
| 9686 | btrfs_record_unlink_dir(trans, BTRFS_I(new_dir), |
| 9687 | BTRFS_I(new_inode), 1); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9688 | } |
| 9689 | |
| 9690 | /* src is a subvolume */ |
| 9691 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) { |
| 9692 | root_objectid = BTRFS_I(old_inode)->root->root_key.objectid; |
| 9693 | ret = btrfs_unlink_subvol(trans, root, old_dir, |
| 9694 | root_objectid, |
| 9695 | old_dentry->d_name.name, |
| 9696 | old_dentry->d_name.len); |
| 9697 | } else { /* src is an inode */ |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 9698 | ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir), |
| 9699 | BTRFS_I(old_dentry->d_inode), |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9700 | old_dentry->d_name.name, |
| 9701 | old_dentry->d_name.len); |
| 9702 | if (!ret) |
| 9703 | ret = btrfs_update_inode(trans, root, old_inode); |
| 9704 | } |
| 9705 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 9706 | btrfs_abort_transaction(trans, ret); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9707 | goto out_fail; |
| 9708 | } |
| 9709 | |
| 9710 | /* dest is a subvolume */ |
| 9711 | if (new_ino == BTRFS_FIRST_FREE_OBJECTID) { |
| 9712 | root_objectid = BTRFS_I(new_inode)->root->root_key.objectid; |
| 9713 | ret = btrfs_unlink_subvol(trans, dest, new_dir, |
| 9714 | root_objectid, |
| 9715 | new_dentry->d_name.name, |
| 9716 | new_dentry->d_name.len); |
| 9717 | } else { /* dest is an inode */ |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 9718 | ret = __btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir), |
| 9719 | BTRFS_I(new_dentry->d_inode), |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9720 | new_dentry->d_name.name, |
| 9721 | new_dentry->d_name.len); |
| 9722 | if (!ret) |
| 9723 | ret = btrfs_update_inode(trans, dest, new_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 | |
Nikolay Borisov | db0a669 | 2017-02-20 13:51:08 +0200 | [diff] [blame] | 9730 | 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] | 9731 | new_dentry->d_name.name, |
| 9732 | new_dentry->d_name.len, 0, old_idx); |
| 9733 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 9734 | btrfs_abort_transaction(trans, ret); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9735 | goto out_fail; |
| 9736 | } |
| 9737 | |
Nikolay Borisov | db0a669 | 2017-02-20 13:51:08 +0200 | [diff] [blame] | 9738 | 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] | 9739 | old_dentry->d_name.name, |
| 9740 | old_dentry->d_name.len, 0, new_idx); |
| 9741 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 9742 | btrfs_abort_transaction(trans, ret); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9743 | goto out_fail; |
| 9744 | } |
| 9745 | |
| 9746 | if (old_inode->i_nlink == 1) |
| 9747 | BTRFS_I(old_inode)->dir_index = old_idx; |
| 9748 | if (new_inode->i_nlink == 1) |
| 9749 | BTRFS_I(new_inode)->dir_index = new_idx; |
| 9750 | |
Filipe Manana | 86e8aa0 | 2016-05-05 02:02:27 +0100 | [diff] [blame] | 9751 | if (root_log_pinned) { |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9752 | parent = new_dentry->d_parent; |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 9753 | btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir), |
| 9754 | parent); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9755 | btrfs_end_log_trans(root); |
Filipe Manana | 86e8aa0 | 2016-05-05 02:02:27 +0100 | [diff] [blame] | 9756 | root_log_pinned = false; |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9757 | } |
Filipe Manana | 86e8aa0 | 2016-05-05 02:02:27 +0100 | [diff] [blame] | 9758 | if (dest_log_pinned) { |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9759 | parent = old_dentry->d_parent; |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 9760 | btrfs_log_new_name(trans, BTRFS_I(new_inode), BTRFS_I(new_dir), |
| 9761 | parent); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9762 | btrfs_end_log_trans(dest); |
Filipe Manana | 86e8aa0 | 2016-05-05 02:02:27 +0100 | [diff] [blame] | 9763 | dest_log_pinned = false; |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9764 | } |
| 9765 | out_fail: |
Filipe Manana | 86e8aa0 | 2016-05-05 02:02:27 +0100 | [diff] [blame] | 9766 | /* |
| 9767 | * If we have pinned a log and an error happened, we unpin tasks |
| 9768 | * trying to sync the log and force them to fallback to a transaction |
| 9769 | * commit if the log currently contains any of the inodes involved in |
| 9770 | * this rename operation (to ensure we do not persist a log with an |
| 9771 | * inconsistent state for any of these inodes or leading to any |
| 9772 | * inconsistencies when replayed). If the transaction was aborted, the |
| 9773 | * abortion reason is propagated to userspace when attempting to commit |
| 9774 | * the transaction. If the log does not contain any of these inodes, we |
| 9775 | * allow the tasks to sync it. |
| 9776 | */ |
| 9777 | if (ret && (root_log_pinned || dest_log_pinned)) { |
Nikolay Borisov | 0f8939b | 2017-01-18 00:31:30 +0200 | [diff] [blame] | 9778 | if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) || |
| 9779 | btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) || |
| 9780 | btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) || |
Filipe Manana | 86e8aa0 | 2016-05-05 02:02:27 +0100 | [diff] [blame] | 9781 | (new_inode && |
Nikolay Borisov | 0f8939b | 2017-01-18 00:31:30 +0200 | [diff] [blame] | 9782 | btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation))) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9783 | btrfs_set_log_full_commit(fs_info, trans); |
Filipe Manana | 86e8aa0 | 2016-05-05 02:02:27 +0100 | [diff] [blame] | 9784 | |
| 9785 | if (root_log_pinned) { |
| 9786 | btrfs_end_log_trans(root); |
| 9787 | root_log_pinned = false; |
| 9788 | } |
| 9789 | if (dest_log_pinned) { |
| 9790 | btrfs_end_log_trans(dest); |
| 9791 | dest_log_pinned = false; |
| 9792 | } |
| 9793 | } |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 9794 | ret = btrfs_end_transaction(trans); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9795 | out_notrans: |
| 9796 | if (new_ino == BTRFS_FIRST_FREE_OBJECTID) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9797 | up_read(&fs_info->subvol_sem); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9798 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9799 | up_read(&fs_info->subvol_sem); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9800 | |
| 9801 | return ret; |
| 9802 | } |
| 9803 | |
| 9804 | static int btrfs_whiteout_for_rename(struct btrfs_trans_handle *trans, |
| 9805 | struct btrfs_root *root, |
| 9806 | struct inode *dir, |
| 9807 | struct dentry *dentry) |
| 9808 | { |
| 9809 | int ret; |
| 9810 | struct inode *inode; |
| 9811 | u64 objectid; |
| 9812 | u64 index; |
| 9813 | |
| 9814 | ret = btrfs_find_free_ino(root, &objectid); |
| 9815 | if (ret) |
| 9816 | return ret; |
| 9817 | |
| 9818 | inode = btrfs_new_inode(trans, root, dir, |
| 9819 | dentry->d_name.name, |
| 9820 | dentry->d_name.len, |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 9821 | btrfs_ino(BTRFS_I(dir)), |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9822 | objectid, |
| 9823 | S_IFCHR | WHITEOUT_MODE, |
| 9824 | &index); |
| 9825 | |
| 9826 | if (IS_ERR(inode)) { |
| 9827 | ret = PTR_ERR(inode); |
| 9828 | return ret; |
| 9829 | } |
| 9830 | |
| 9831 | inode->i_op = &btrfs_special_inode_operations; |
| 9832 | init_special_inode(inode, inode->i_mode, |
| 9833 | WHITEOUT_DEV); |
| 9834 | |
| 9835 | ret = btrfs_init_inode_security(trans, inode, dir, |
| 9836 | &dentry->d_name); |
| 9837 | if (ret) |
Filipe Manana | c990161 | 2016-05-05 01:41:57 +0100 | [diff] [blame] | 9838 | goto out; |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9839 | |
Nikolay Borisov | cef415a | 2017-02-20 13:51:09 +0200 | [diff] [blame] | 9840 | ret = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, |
| 9841 | BTRFS_I(inode), 0, index); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9842 | if (ret) |
Filipe Manana | c990161 | 2016-05-05 01:41:57 +0100 | [diff] [blame] | 9843 | goto out; |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9844 | |
| 9845 | ret = btrfs_update_inode(trans, root, inode); |
Filipe Manana | c990161 | 2016-05-05 01:41:57 +0100 | [diff] [blame] | 9846 | out: |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9847 | unlock_new_inode(inode); |
Filipe Manana | c990161 | 2016-05-05 01:41:57 +0100 | [diff] [blame] | 9848 | if (ret) |
| 9849 | inode_dec_link_count(inode); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9850 | iput(inode); |
| 9851 | |
Filipe Manana | c990161 | 2016-05-05 01:41:57 +0100 | [diff] [blame] | 9852 | return ret; |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9853 | } |
| 9854 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9855 | static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry, |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9856 | struct inode *new_dir, struct dentry *new_dentry, |
| 9857 | unsigned int flags) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9858 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9859 | struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9860 | struct btrfs_trans_handle *trans; |
Filipe Manana | 5062af3 | 2016-05-05 10:26:26 +0100 | [diff] [blame] | 9861 | unsigned int trans_num_items; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9862 | struct btrfs_root *root = BTRFS_I(old_dir)->root; |
| 9863 | struct btrfs_root *dest = BTRFS_I(new_dir)->root; |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 9864 | struct inode *new_inode = d_inode(new_dentry); |
| 9865 | struct inode *old_inode = d_inode(old_dentry); |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 9866 | u64 index = 0; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9867 | u64 root_objectid; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9868 | int ret; |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 9869 | u64 old_ino = btrfs_ino(BTRFS_I(old_inode)); |
Filipe Manana | 3dc9e8f | 2016-04-29 11:34:22 +0100 | [diff] [blame] | 9870 | bool log_pinned = false; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9871 | |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 9872 | if (btrfs_ino(BTRFS_I(new_dir)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) |
Yan, Zheng | f679a84 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 9873 | return -EPERM; |
| 9874 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9875 | /* we only allow rename subvolume link between subvolumes */ |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 9876 | if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest) |
Chris Mason | 3394e16 | 2008-11-17 20:42:26 -0500 | [diff] [blame] | 9877 | return -EXDEV; |
| 9878 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 9879 | if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID || |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 9880 | (new_inode && btrfs_ino(BTRFS_I(new_inode)) == BTRFS_FIRST_FREE_OBJECTID)) |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9881 | return -ENOTEMPTY; |
| 9882 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9883 | if (S_ISDIR(old_inode->i_mode) && new_inode && |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9884 | new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9885 | return -ENOTEMPTY; |
Chris Mason | 9c52057 | 2012-12-17 14:26:57 -0500 | [diff] [blame] | 9886 | |
| 9887 | |
| 9888 | /* check for collisions, even if the name isn't there */ |
Josef Bacik | 4871c15 | 2013-10-09 12:24:04 -0400 | [diff] [blame] | 9889 | ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino, |
Chris Mason | 9c52057 | 2012-12-17 14:26:57 -0500 | [diff] [blame] | 9890 | new_dentry->d_name.name, |
| 9891 | new_dentry->d_name.len); |
| 9892 | |
| 9893 | if (ret) { |
| 9894 | if (ret == -EEXIST) { |
| 9895 | /* we shouldn't get |
| 9896 | * eexist without a new_inode */ |
Dulshani Gunawardhana | fae7f21 | 2013-10-31 10:30:08 +0530 | [diff] [blame] | 9897 | if (WARN_ON(!new_inode)) { |
Chris Mason | 9c52057 | 2012-12-17 14:26:57 -0500 | [diff] [blame] | 9898 | return ret; |
| 9899 | } |
| 9900 | } else { |
| 9901 | /* maybe -EOVERFLOW */ |
| 9902 | return ret; |
| 9903 | } |
| 9904 | } |
| 9905 | ret = 0; |
| 9906 | |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 9907 | /* |
Chris Mason | 8d875f9 | 2014-08-12 10:47:42 -0700 | [diff] [blame] | 9908 | * we're using rename to replace one file with another. Start IO on it |
| 9909 | * 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] | 9910 | */ |
Chris Mason | 8d875f9 | 2014-08-12 10:47:42 -0700 | [diff] [blame] | 9911 | 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] | 9912 | filemap_flush(old_inode->i_mapping); |
| 9913 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 9914 | /* close the racy window with snapshot create/destroy ioctl */ |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 9915 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9916 | down_read(&fs_info->subvol_sem); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 9917 | /* |
| 9918 | * We want to reserve the absolute worst case amount of items. So if |
| 9919 | * both inodes are subvols and we need to unlink them then that would |
| 9920 | * require 4 item modifications, but if they are both normal inodes it |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9921 | * would require 5 item modifications, so we'll assume they are normal |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 9922 | * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items |
| 9923 | * should cover the worst case number of items we'll modify. |
Filipe Manana | 5062af3 | 2016-05-05 10:26:26 +0100 | [diff] [blame] | 9924 | * If our rename has the whiteout flag, we need more 5 units for the |
| 9925 | * new inode (1 inode item, 1 inode ref, 2 dir items and 1 xattr item |
| 9926 | * when selinux is enabled). |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 9927 | */ |
Filipe Manana | 5062af3 | 2016-05-05 10:26:26 +0100 | [diff] [blame] | 9928 | trans_num_items = 11; |
| 9929 | if (flags & RENAME_WHITEOUT) |
| 9930 | trans_num_items += 5; |
| 9931 | trans = btrfs_start_transaction(root, trans_num_items); |
Johann Lombardi | b44c59a | 2011-03-31 13:23:47 +0000 | [diff] [blame] | 9932 | if (IS_ERR(trans)) { |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9933 | ret = PTR_ERR(trans); |
| 9934 | goto out_notrans; |
| 9935 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 9936 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9937 | if (dest != root) |
| 9938 | btrfs_record_root_in_trans(trans, dest); |
| 9939 | |
Nikolay Borisov | 877574e | 2017-02-20 13:50:33 +0200 | [diff] [blame] | 9940 | ret = btrfs_set_inode_index(BTRFS_I(new_dir), &index); |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 9941 | if (ret) |
| 9942 | goto out_fail; |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 9943 | |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 9944 | BTRFS_I(old_inode)->dir_index = 0ULL; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 9945 | if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9946 | /* force full log commit if subvolume involved. */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9947 | btrfs_set_log_full_commit(fs_info, trans); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9948 | } else { |
Filipe Manana | c4aba95 | 2016-04-29 13:14:42 +0100 | [diff] [blame] | 9949 | btrfs_pin_log_trans(root); |
| 9950 | log_pinned = true; |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 9951 | ret = btrfs_insert_inode_ref(trans, dest, |
| 9952 | new_dentry->d_name.name, |
| 9953 | new_dentry->d_name.len, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 9954 | old_ino, |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 9955 | btrfs_ino(BTRFS_I(new_dir)), index); |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 9956 | if (ret) |
| 9957 | goto out_fail; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9958 | } |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 9959 | |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 9960 | inode_inc_iversion(old_dir); |
| 9961 | inode_inc_iversion(new_dir); |
| 9962 | inode_inc_iversion(old_inode); |
Deepa Dinamani | 04b285f | 2016-02-06 23:57:21 -0800 | [diff] [blame] | 9963 | old_dir->i_ctime = old_dir->i_mtime = |
| 9964 | new_dir->i_ctime = new_dir->i_mtime = |
Deepa Dinamani | c2050a4 | 2016-09-14 07:48:06 -0700 | [diff] [blame] | 9965 | old_inode->i_ctime = current_time(old_dir); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 9966 | |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 9967 | if (old_dentry->d_parent != new_dentry->d_parent) |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 9968 | btrfs_record_unlink_dir(trans, BTRFS_I(old_dir), |
| 9969 | BTRFS_I(old_inode), 1); |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 9970 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 9971 | if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9972 | root_objectid = BTRFS_I(old_inode)->root->root_key.objectid; |
| 9973 | ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid, |
| 9974 | old_dentry->d_name.name, |
| 9975 | old_dentry->d_name.len); |
| 9976 | } else { |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 9977 | ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir), |
| 9978 | BTRFS_I(d_inode(old_dentry)), |
Al Viro | 9298679 | 2011-03-04 17:14:37 +0000 | [diff] [blame] | 9979 | old_dentry->d_name.name, |
| 9980 | old_dentry->d_name.len); |
| 9981 | if (!ret) |
| 9982 | ret = btrfs_update_inode(trans, root, old_inode); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9983 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 9984 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 9985 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 9986 | goto out_fail; |
| 9987 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9988 | |
| 9989 | if (new_inode) { |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 9990 | inode_inc_iversion(new_inode); |
Deepa Dinamani | c2050a4 | 2016-09-14 07:48:06 -0700 | [diff] [blame] | 9991 | new_inode->i_ctime = current_time(new_inode); |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 9992 | if (unlikely(btrfs_ino(BTRFS_I(new_inode)) == |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9993 | BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) { |
| 9994 | root_objectid = BTRFS_I(new_inode)->location.objectid; |
| 9995 | ret = btrfs_unlink_subvol(trans, dest, new_dir, |
| 9996 | root_objectid, |
| 9997 | new_dentry->d_name.name, |
| 9998 | new_dentry->d_name.len); |
| 9999 | BUG_ON(new_inode->i_nlink == 0); |
| 10000 | } else { |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 10001 | ret = btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir), |
| 10002 | BTRFS_I(d_inode(new_dentry)), |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 10003 | new_dentry->d_name.name, |
| 10004 | new_dentry->d_name.len); |
| 10005 | } |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 10006 | if (!ret && new_inode->i_nlink == 0) |
Nikolay Borisov | 73f2e54 | 2017-02-20 13:50:59 +0200 | [diff] [blame] | 10007 | ret = btrfs_orphan_add(trans, |
| 10008 | BTRFS_I(d_inode(new_dentry))); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 10009 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 10010 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 10011 | goto out_fail; |
| 10012 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10013 | } |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 10014 | |
Nikolay Borisov | db0a669 | 2017-02-20 13:51:08 +0200 | [diff] [blame] | 10015 | 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] | 10016 | new_dentry->d_name.name, |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 10017 | new_dentry->d_name.len, 0, index); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 10018 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 10019 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 10020 | goto out_fail; |
| 10021 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10022 | |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 10023 | if (old_inode->i_nlink == 1) |
| 10024 | BTRFS_I(old_inode)->dir_index = index; |
| 10025 | |
Filipe Manana | 3dc9e8f | 2016-04-29 11:34:22 +0100 | [diff] [blame] | 10026 | if (log_pinned) { |
Al Viro | 10d9f30 | 2011-07-16 23:09:10 -0400 | [diff] [blame] | 10027 | struct dentry *parent = new_dentry->d_parent; |
Filipe Manana | 3dc9e8f | 2016-04-29 11:34:22 +0100 | [diff] [blame] | 10028 | |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 10029 | btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir), |
| 10030 | parent); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 10031 | btrfs_end_log_trans(root); |
Filipe Manana | 3dc9e8f | 2016-04-29 11:34:22 +0100 | [diff] [blame] | 10032 | log_pinned = false; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 10033 | } |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 10034 | |
| 10035 | if (flags & RENAME_WHITEOUT) { |
| 10036 | ret = btrfs_whiteout_for_rename(trans, root, old_dir, |
| 10037 | old_dentry); |
| 10038 | |
| 10039 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 10040 | btrfs_abort_transaction(trans, ret); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 10041 | goto out_fail; |
| 10042 | } |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 10043 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10044 | out_fail: |
Filipe Manana | 3dc9e8f | 2016-04-29 11:34:22 +0100 | [diff] [blame] | 10045 | /* |
| 10046 | * If we have pinned the log and an error happened, we unpin tasks |
| 10047 | * trying to sync the log and force them to fallback to a transaction |
| 10048 | * commit if the log currently contains any of the inodes involved in |
| 10049 | * this rename operation (to ensure we do not persist a log with an |
| 10050 | * inconsistent state for any of these inodes or leading to any |
| 10051 | * inconsistencies when replayed). If the transaction was aborted, the |
| 10052 | * abortion reason is propagated to userspace when attempting to commit |
| 10053 | * the transaction. If the log does not contain any of these inodes, we |
| 10054 | * allow the tasks to sync it. |
| 10055 | */ |
| 10056 | if (ret && log_pinned) { |
Nikolay Borisov | 0f8939b | 2017-01-18 00:31:30 +0200 | [diff] [blame] | 10057 | if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) || |
| 10058 | btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) || |
| 10059 | btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) || |
Filipe Manana | 3dc9e8f | 2016-04-29 11:34:22 +0100 | [diff] [blame] | 10060 | (new_inode && |
Nikolay Borisov | 0f8939b | 2017-01-18 00:31:30 +0200 | [diff] [blame] | 10061 | btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation))) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 10062 | btrfs_set_log_full_commit(fs_info, trans); |
Filipe Manana | 3dc9e8f | 2016-04-29 11:34:22 +0100 | [diff] [blame] | 10063 | |
| 10064 | btrfs_end_log_trans(root); |
| 10065 | log_pinned = false; |
| 10066 | } |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 10067 | btrfs_end_transaction(trans); |
Johann Lombardi | b44c59a | 2011-03-31 13:23:47 +0000 | [diff] [blame] | 10068 | out_notrans: |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 10069 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 10070 | up_read(&fs_info->subvol_sem); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 10071 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10072 | return ret; |
| 10073 | } |
| 10074 | |
Miklos Szeredi | 80ace85 | 2014-07-23 15:15:32 +0200 | [diff] [blame] | 10075 | static int btrfs_rename2(struct inode *old_dir, struct dentry *old_dentry, |
| 10076 | struct inode *new_dir, struct dentry *new_dentry, |
| 10077 | unsigned int flags) |
| 10078 | { |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 10079 | if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT)) |
Miklos Szeredi | 80ace85 | 2014-07-23 15:15:32 +0200 | [diff] [blame] | 10080 | return -EINVAL; |
| 10081 | |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 10082 | if (flags & RENAME_EXCHANGE) |
| 10083 | return btrfs_rename_exchange(old_dir, old_dentry, new_dir, |
| 10084 | new_dentry); |
| 10085 | |
| 10086 | return btrfs_rename(old_dir, old_dentry, new_dir, new_dentry, flags); |
Miklos Szeredi | 80ace85 | 2014-07-23 15:15:32 +0200 | [diff] [blame] | 10087 | } |
| 10088 | |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 10089 | static void btrfs_run_delalloc_work(struct btrfs_work *work) |
| 10090 | { |
| 10091 | struct btrfs_delalloc_work *delalloc_work; |
Josef Bacik | 9f23e28 | 2013-10-28 15:03:41 -0400 | [diff] [blame] | 10092 | struct inode *inode; |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 10093 | |
| 10094 | delalloc_work = container_of(work, struct btrfs_delalloc_work, |
| 10095 | work); |
Josef Bacik | 9f23e28 | 2013-10-28 15:03:41 -0400 | [diff] [blame] | 10096 | inode = delalloc_work->inode; |
David Sterba | 3042460 | 2015-11-27 19:27:11 +0100 | [diff] [blame] | 10097 | filemap_flush(inode->i_mapping); |
| 10098 | if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, |
| 10099 | &BTRFS_I(inode)->runtime_flags)) |
Josef Bacik | 9f23e28 | 2013-10-28 15:03:41 -0400 | [diff] [blame] | 10100 | filemap_flush(inode->i_mapping); |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 10101 | |
| 10102 | if (delalloc_work->delay_iput) |
Josef Bacik | 9f23e28 | 2013-10-28 15:03:41 -0400 | [diff] [blame] | 10103 | btrfs_add_delayed_iput(inode); |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 10104 | else |
Josef Bacik | 9f23e28 | 2013-10-28 15:03:41 -0400 | [diff] [blame] | 10105 | iput(inode); |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 10106 | complete(&delalloc_work->completion); |
| 10107 | } |
| 10108 | |
| 10109 | struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode, |
David Sterba | 651d494 | 2015-11-27 19:24:16 +0100 | [diff] [blame] | 10110 | int delay_iput) |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 10111 | { |
| 10112 | struct btrfs_delalloc_work *work; |
| 10113 | |
David Sterba | 100d570 | 2015-12-08 14:39:32 +0100 | [diff] [blame] | 10114 | work = kmalloc(sizeof(*work), GFP_NOFS); |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 10115 | if (!work) |
| 10116 | return NULL; |
| 10117 | |
| 10118 | init_completion(&work->completion); |
| 10119 | INIT_LIST_HEAD(&work->list); |
| 10120 | work->inode = inode; |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 10121 | work->delay_iput = delay_iput; |
Liu Bo | 9e0af23 | 2014-08-15 23:36:53 +0800 | [diff] [blame] | 10122 | WARN_ON_ONCE(!inode); |
| 10123 | btrfs_init_work(&work->work, btrfs_flush_delalloc_helper, |
| 10124 | btrfs_run_delalloc_work, NULL, NULL); |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 10125 | |
| 10126 | return work; |
| 10127 | } |
| 10128 | |
| 10129 | void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work) |
| 10130 | { |
| 10131 | wait_for_completion(&work->completion); |
David Sterba | 100d570 | 2015-12-08 14:39:32 +0100 | [diff] [blame] | 10132 | kfree(work); |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 10133 | } |
| 10134 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 10135 | /* |
| 10136 | * some fairly slow code that needs optimization. This walks the list |
| 10137 | * of all the inodes with pending delalloc and forces them to disk. |
| 10138 | */ |
Miao Xie | 6c255e6 | 2014-03-06 13:55:01 +0800 | [diff] [blame] | 10139 | static int __start_delalloc_inodes(struct btrfs_root *root, int delay_iput, |
| 10140 | int nr) |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 10141 | { |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 10142 | struct btrfs_inode *binode; |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 10143 | struct inode *inode; |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 10144 | struct btrfs_delalloc_work *work, *next; |
| 10145 | struct list_head works; |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 10146 | struct list_head splice; |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 10147 | int ret = 0; |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 10148 | |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 10149 | INIT_LIST_HEAD(&works); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 10150 | INIT_LIST_HEAD(&splice); |
Miao Xie | 63607cc | 2013-01-22 10:50:35 +0000 | [diff] [blame] | 10151 | |
Miao Xie | 573bfb7 | 2014-03-06 13:55:03 +0800 | [diff] [blame] | 10152 | mutex_lock(&root->delalloc_mutex); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 10153 | spin_lock(&root->delalloc_lock); |
| 10154 | list_splice_init(&root->delalloc_inodes, &splice); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 10155 | while (!list_empty(&splice)) { |
| 10156 | binode = list_entry(splice.next, struct btrfs_inode, |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 10157 | delalloc_inodes); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 10158 | |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 10159 | list_move_tail(&binode->delalloc_inodes, |
| 10160 | &root->delalloc_inodes); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 10161 | inode = igrab(&binode->vfs_inode); |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 10162 | if (!inode) { |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 10163 | cond_resched_lock(&root->delalloc_lock); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 10164 | continue; |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 10165 | } |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 10166 | spin_unlock(&root->delalloc_lock); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 10167 | |
David Sterba | 651d494 | 2015-11-27 19:24:16 +0100 | [diff] [blame] | 10168 | work = btrfs_alloc_delalloc_work(inode, delay_iput); |
David Sterba | 5d99a998 | 2014-09-29 19:20:37 +0200 | [diff] [blame] | 10169 | if (!work) { |
Josef Bacik | f4ab9ea | 2013-09-17 11:25:44 -0400 | [diff] [blame] | 10170 | if (delay_iput) |
| 10171 | btrfs_add_delayed_iput(inode); |
| 10172 | else |
| 10173 | iput(inode); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 10174 | ret = -ENOMEM; |
Wang Shilong | a1ecaab | 2014-04-02 19:53:32 +0800 | [diff] [blame] | 10175 | goto out; |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 10176 | } |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 10177 | list_add_tail(&work->list, &works); |
Qu Wenruo | a44903a | 2014-02-28 10:46:09 +0800 | [diff] [blame] | 10178 | btrfs_queue_work(root->fs_info->flush_workers, |
| 10179 | &work->work); |
Miao Xie | 6c255e6 | 2014-03-06 13:55:01 +0800 | [diff] [blame] | 10180 | ret++; |
| 10181 | if (nr != -1 && ret >= nr) |
Wang Shilong | a1ecaab | 2014-04-02 19:53:32 +0800 | [diff] [blame] | 10182 | goto out; |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 10183 | cond_resched(); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 10184 | spin_lock(&root->delalloc_lock); |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 10185 | } |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 10186 | spin_unlock(&root->delalloc_lock); |
Chris Mason | 8c8bee1 | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 10187 | |
Wang Shilong | a1ecaab | 2014-04-02 19:53:32 +0800 | [diff] [blame] | 10188 | out: |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 10189 | list_for_each_entry_safe(work, next, &works, list) { |
| 10190 | list_del_init(&work->list); |
| 10191 | btrfs_wait_and_free_delalloc_work(work); |
| 10192 | } |
| 10193 | |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 10194 | if (!list_empty_careful(&splice)) { |
| 10195 | spin_lock(&root->delalloc_lock); |
| 10196 | list_splice_tail(&splice, &root->delalloc_inodes); |
| 10197 | spin_unlock(&root->delalloc_lock); |
| 10198 | } |
Miao Xie | 573bfb7 | 2014-03-06 13:55:03 +0800 | [diff] [blame] | 10199 | mutex_unlock(&root->delalloc_mutex); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 10200 | return ret; |
| 10201 | } |
| 10202 | |
| 10203 | int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput) |
| 10204 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 10205 | struct btrfs_fs_info *fs_info = root->fs_info; |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 10206 | int ret; |
| 10207 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 10208 | if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 10209 | return -EROFS; |
| 10210 | |
Miao Xie | 6c255e6 | 2014-03-06 13:55:01 +0800 | [diff] [blame] | 10211 | ret = __start_delalloc_inodes(root, delay_iput, -1); |
| 10212 | if (ret > 0) |
| 10213 | ret = 0; |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 10214 | /* |
| 10215 | * the filemap_flush will queue IO into the worker threads, but |
Chris Mason | 8c8bee1 | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 10216 | * we have to make sure the IO is actually started and that |
| 10217 | * ordered extents get created before we return |
| 10218 | */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 10219 | atomic_inc(&fs_info->async_submit_draining); |
| 10220 | while (atomic_read(&fs_info->nr_async_submits) || |
| 10221 | atomic_read(&fs_info->async_delalloc_pages)) { |
| 10222 | wait_event(fs_info->async_submit_wait, |
| 10223 | (atomic_read(&fs_info->nr_async_submits) == 0 && |
| 10224 | atomic_read(&fs_info->async_delalloc_pages) == 0)); |
Chris Mason | 8c8bee1 | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 10225 | } |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 10226 | atomic_dec(&fs_info->async_submit_draining); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 10227 | return ret; |
| 10228 | } |
| 10229 | |
Miao Xie | 6c255e6 | 2014-03-06 13:55:01 +0800 | [diff] [blame] | 10230 | int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, int delay_iput, |
| 10231 | int nr) |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 10232 | { |
| 10233 | struct btrfs_root *root; |
| 10234 | struct list_head splice; |
| 10235 | int ret; |
| 10236 | |
Wang Shilong | 2c21b4d | 2014-01-14 19:42:20 +0800 | [diff] [blame] | 10237 | if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 10238 | return -EROFS; |
| 10239 | |
| 10240 | INIT_LIST_HEAD(&splice); |
| 10241 | |
Miao Xie | 573bfb7 | 2014-03-06 13:55:03 +0800 | [diff] [blame] | 10242 | mutex_lock(&fs_info->delalloc_root_mutex); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 10243 | spin_lock(&fs_info->delalloc_root_lock); |
| 10244 | list_splice_init(&fs_info->delalloc_roots, &splice); |
Miao Xie | 6c255e6 | 2014-03-06 13:55:01 +0800 | [diff] [blame] | 10245 | while (!list_empty(&splice) && nr) { |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 10246 | root = list_first_entry(&splice, struct btrfs_root, |
| 10247 | delalloc_root); |
| 10248 | root = btrfs_grab_fs_root(root); |
| 10249 | BUG_ON(!root); |
| 10250 | list_move_tail(&root->delalloc_root, |
| 10251 | &fs_info->delalloc_roots); |
| 10252 | spin_unlock(&fs_info->delalloc_root_lock); |
| 10253 | |
Miao Xie | 6c255e6 | 2014-03-06 13:55:01 +0800 | [diff] [blame] | 10254 | ret = __start_delalloc_inodes(root, delay_iput, nr); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 10255 | btrfs_put_fs_root(root); |
Miao Xie | 6c255e6 | 2014-03-06 13:55:01 +0800 | [diff] [blame] | 10256 | if (ret < 0) |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 10257 | goto out; |
| 10258 | |
Miao Xie | 6c255e6 | 2014-03-06 13:55:01 +0800 | [diff] [blame] | 10259 | if (nr != -1) { |
| 10260 | nr -= ret; |
| 10261 | WARN_ON(nr < 0); |
| 10262 | } |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 10263 | spin_lock(&fs_info->delalloc_root_lock); |
| 10264 | } |
| 10265 | spin_unlock(&fs_info->delalloc_root_lock); |
| 10266 | |
Miao Xie | 6c255e6 | 2014-03-06 13:55:01 +0800 | [diff] [blame] | 10267 | ret = 0; |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 10268 | atomic_inc(&fs_info->async_submit_draining); |
| 10269 | while (atomic_read(&fs_info->nr_async_submits) || |
| 10270 | atomic_read(&fs_info->async_delalloc_pages)) { |
| 10271 | wait_event(fs_info->async_submit_wait, |
| 10272 | (atomic_read(&fs_info->nr_async_submits) == 0 && |
| 10273 | atomic_read(&fs_info->async_delalloc_pages) == 0)); |
| 10274 | } |
| 10275 | atomic_dec(&fs_info->async_submit_draining); |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 10276 | out: |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 10277 | if (!list_empty_careful(&splice)) { |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 10278 | spin_lock(&fs_info->delalloc_root_lock); |
| 10279 | list_splice_tail(&splice, &fs_info->delalloc_roots); |
| 10280 | spin_unlock(&fs_info->delalloc_root_lock); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 10281 | } |
Miao Xie | 573bfb7 | 2014-03-06 13:55:03 +0800 | [diff] [blame] | 10282 | mutex_unlock(&fs_info->delalloc_root_mutex); |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 10283 | return ret; |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 10284 | } |
| 10285 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10286 | static int btrfs_symlink(struct inode *dir, struct dentry *dentry, |
| 10287 | const char *symname) |
| 10288 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 10289 | struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10290 | struct btrfs_trans_handle *trans; |
| 10291 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 10292 | struct btrfs_path *path; |
| 10293 | struct btrfs_key key; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 10294 | struct inode *inode = NULL; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10295 | int err; |
| 10296 | int drop_inode = 0; |
| 10297 | u64 objectid; |
Dulshani Gunawardhana | 6787125 | 2013-10-31 10:33:04 +0530 | [diff] [blame] | 10298 | u64 index = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10299 | int name_len; |
| 10300 | int datasize; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 10301 | unsigned long ptr; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10302 | struct btrfs_file_extent_item *ei; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 10303 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10304 | |
Filipe David Borba Manana | f06becc | 2013-09-16 09:53:28 +0100 | [diff] [blame] | 10305 | name_len = strlen(symname); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 10306 | if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info)) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10307 | return -ENAMETOOLONG; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 10308 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 10309 | /* |
| 10310 | * 2 items for inode item and ref |
| 10311 | * 2 items for dir items |
Filipe Manana | 9269d12 | 2015-12-31 18:16:29 +0000 | [diff] [blame] | 10312 | * 1 item for updating parent inode item |
| 10313 | * 1 item for the inline extent item |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 10314 | * 1 item for xattr if selinux is on |
| 10315 | */ |
Filipe Manana | 9269d12 | 2015-12-31 18:16:29 +0000 | [diff] [blame] | 10316 | trans = btrfs_start_transaction(root, 7); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 10317 | if (IS_ERR(trans)) |
| 10318 | return PTR_ERR(trans); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 10319 | |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 10320 | err = btrfs_find_free_ino(root, &objectid); |
| 10321 | if (err) |
| 10322 | goto out_unlock; |
| 10323 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 10324 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 10325 | dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), |
| 10326 | objectid, S_IFLNK|S_IRWXUGO, &index); |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 10327 | if (IS_ERR(inode)) { |
| 10328 | err = PTR_ERR(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10329 | goto out_unlock; |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 10330 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10331 | |
Casey Schaufler | ad19db7 | 2011-12-15 10:09:07 -0500 | [diff] [blame] | 10332 | /* |
| 10333 | * If the active LSM wants to access the inode during |
| 10334 | * d_instantiate it needs these. Smack checks to see |
| 10335 | * if the filesystem supports xattrs by looking at the |
| 10336 | * ops vector. |
| 10337 | */ |
| 10338 | inode->i_fop = &btrfs_file_operations; |
| 10339 | inode->i_op = &btrfs_file_inode_operations; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 10340 | inode->i_mapping->a_ops = &btrfs_aops; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 10341 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
| 10342 | |
| 10343 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); |
| 10344 | if (err) |
| 10345 | goto out_unlock_inode; |
Casey Schaufler | ad19db7 | 2011-12-15 10:09:07 -0500 | [diff] [blame] | 10346 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10347 | path = btrfs_alloc_path(); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 10348 | if (!path) { |
| 10349 | err = -ENOMEM; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 10350 | goto out_unlock_inode; |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 10351 | } |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 10352 | key.objectid = btrfs_ino(BTRFS_I(inode)); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10353 | key.offset = 0; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 10354 | key.type = BTRFS_EXTENT_DATA_KEY; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10355 | datasize = btrfs_file_extent_calc_inline_size(name_len); |
| 10356 | err = btrfs_insert_empty_item(trans, root, path, &key, |
| 10357 | datasize); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 10358 | if (err) { |
Julia Lawall | b083916 | 2011-05-14 07:10:51 +0000 | [diff] [blame] | 10359 | btrfs_free_path(path); |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 10360 | goto out_unlock_inode; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 10361 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 10362 | leaf = path->nodes[0]; |
| 10363 | ei = btrfs_item_ptr(leaf, path->slots[0], |
| 10364 | struct btrfs_file_extent_item); |
| 10365 | btrfs_set_file_extent_generation(leaf, ei, trans->transid); |
| 10366 | btrfs_set_file_extent_type(leaf, ei, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10367 | BTRFS_FILE_EXTENT_INLINE); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 10368 | btrfs_set_file_extent_encryption(leaf, ei, 0); |
| 10369 | btrfs_set_file_extent_compression(leaf, ei, 0); |
| 10370 | btrfs_set_file_extent_other_encoding(leaf, ei, 0); |
| 10371 | btrfs_set_file_extent_ram_bytes(leaf, ei, name_len); |
| 10372 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10373 | ptr = btrfs_file_extent_inline_start(ei); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 10374 | write_extent_buffer(leaf, symname, ptr, name_len); |
| 10375 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10376 | btrfs_free_path(path); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 10377 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10378 | inode->i_op = &btrfs_symlink_inode_operations; |
Al Viro | 21fc61c | 2015-11-17 01:07:57 -0500 | [diff] [blame] | 10379 | inode_nohighmem(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10380 | inode->i_mapping->a_ops = &btrfs_symlink_aops; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 10381 | inode_set_bytes(inode, name_len); |
Nikolay Borisov | 6ef06d2 | 2017-02-20 13:50:34 +0200 | [diff] [blame] | 10382 | btrfs_i_size_write(BTRFS_I(inode), name_len); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 10383 | err = btrfs_update_inode(trans, root, inode); |
Filipe Manana | d50866d | 2015-12-31 18:08:24 +0000 | [diff] [blame] | 10384 | /* |
| 10385 | * Last step, add directory indexes for our symlink inode. This is the |
| 10386 | * last step to avoid extra cleanup of these indexes if an error happens |
| 10387 | * elsewhere above. |
| 10388 | */ |
| 10389 | if (!err) |
Nikolay Borisov | cef415a | 2017-02-20 13:51:09 +0200 | [diff] [blame] | 10390 | err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, |
| 10391 | BTRFS_I(inode), 0, index); |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 10392 | if (err) { |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 10393 | drop_inode = 1; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 10394 | goto out_unlock_inode; |
| 10395 | } |
| 10396 | |
| 10397 | unlock_new_inode(inode); |
| 10398 | d_instantiate(dentry, inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10399 | |
| 10400 | out_unlock: |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 10401 | btrfs_end_transaction(trans); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10402 | if (drop_inode) { |
| 10403 | inode_dec_link_count(inode); |
| 10404 | iput(inode); |
| 10405 | } |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 10406 | btrfs_btree_balance_dirty(fs_info); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10407 | return err; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 10408 | |
| 10409 | out_unlock_inode: |
| 10410 | drop_inode = 1; |
| 10411 | unlock_new_inode(inode); |
| 10412 | goto out_unlock; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10413 | } |
Chris Mason | 1643298 | 2008-04-10 10:23:21 -0400 | [diff] [blame] | 10414 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 10415 | static int __btrfs_prealloc_file_range(struct inode *inode, int mode, |
| 10416 | u64 start, u64 num_bytes, u64 min_size, |
| 10417 | loff_t actual_len, u64 *alloc_hint, |
| 10418 | struct btrfs_trans_handle *trans) |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 10419 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 10420 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 10421 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
| 10422 | struct extent_map *em; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 10423 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 10424 | struct btrfs_key ins; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 10425 | u64 cur_offset = start; |
Josef Bacik | 55a61d1 | 2010-11-22 18:50:32 +0000 | [diff] [blame] | 10426 | u64 i_size; |
Chris Mason | 154ea28 | 2013-03-05 11:11:26 -0500 | [diff] [blame] | 10427 | u64 cur_bytes; |
Josef Bacik | 0b670dc | 2015-09-23 17:11:16 -0400 | [diff] [blame] | 10428 | u64 last_alloc = (u64)-1; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 10429 | int ret = 0; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 10430 | bool own_trans = true; |
Wang Xiaoguang | 1851309 | 2016-07-25 15:51:40 +0800 | [diff] [blame] | 10431 | u64 end = start + num_bytes - 1; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 10432 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 10433 | if (trans) |
| 10434 | own_trans = false; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 10435 | while (num_bytes > 0) { |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 10436 | if (own_trans) { |
| 10437 | trans = btrfs_start_transaction(root, 3); |
| 10438 | if (IS_ERR(trans)) { |
| 10439 | ret = PTR_ERR(trans); |
| 10440 | break; |
| 10441 | } |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 10442 | } |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 10443 | |
Byongho Lee | ee22184 | 2015-12-15 01:42:10 +0900 | [diff] [blame] | 10444 | cur_bytes = min_t(u64, num_bytes, SZ_256M); |
Chris Mason | 154ea28 | 2013-03-05 11:11:26 -0500 | [diff] [blame] | 10445 | cur_bytes = max(cur_bytes, min_size); |
Josef Bacik | 0b670dc | 2015-09-23 17:11:16 -0400 | [diff] [blame] | 10446 | /* |
| 10447 | * If we are severely fragmented we could end up with really |
| 10448 | * small allocations, so if the allocator is returning small |
| 10449 | * chunks lets make its job easier by only searching for those |
| 10450 | * sized chunks. |
| 10451 | */ |
| 10452 | cur_bytes = min(cur_bytes, last_alloc); |
Wang Xiaoguang | 1851309 | 2016-07-25 15:51:40 +0800 | [diff] [blame] | 10453 | ret = btrfs_reserve_extent(root, cur_bytes, cur_bytes, |
| 10454 | min_size, 0, *alloc_hint, &ins, 1, 0); |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 10455 | if (ret) { |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 10456 | if (own_trans) |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 10457 | btrfs_end_transaction(trans); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 10458 | break; |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 10459 | } |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 10460 | btrfs_dec_block_group_reservations(fs_info, ins.objectid); |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 10461 | |
Josef Bacik | 0b670dc | 2015-09-23 17:11:16 -0400 | [diff] [blame] | 10462 | last_alloc = ins.offset; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 10463 | ret = insert_reserved_file_extent(trans, inode, |
| 10464 | cur_offset, ins.objectid, |
| 10465 | ins.offset, ins.offset, |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 10466 | ins.offset, 0, 0, 0, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 10467 | BTRFS_FILE_EXTENT_PREALLOC); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 10468 | if (ret) { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 10469 | btrfs_free_reserved_extent(fs_info, ins.objectid, |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 10470 | ins.offset, 0); |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 10471 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 10472 | if (own_trans) |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 10473 | btrfs_end_transaction(trans); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 10474 | break; |
| 10475 | } |
Dongsheng Yang | 3119321 | 2014-12-12 16:44:35 +0800 | [diff] [blame] | 10476 | |
Nikolay Borisov | dcdbc05 | 2017-02-20 13:50:45 +0200 | [diff] [blame] | 10477 | btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset, |
Chris Mason | a1ed835 | 2009-09-11 12:27:37 -0400 | [diff] [blame] | 10478 | cur_offset + ins.offset -1, 0); |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 10479 | |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 10480 | em = alloc_extent_map(); |
| 10481 | if (!em) { |
| 10482 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, |
| 10483 | &BTRFS_I(inode)->runtime_flags); |
| 10484 | goto next; |
| 10485 | } |
| 10486 | |
| 10487 | em->start = cur_offset; |
| 10488 | em->orig_start = cur_offset; |
| 10489 | em->len = ins.offset; |
| 10490 | em->block_start = ins.objectid; |
| 10491 | em->block_len = ins.offset; |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 10492 | em->orig_block_len = ins.offset; |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 10493 | em->ram_bytes = ins.offset; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 10494 | em->bdev = fs_info->fs_devices->latest_bdev; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 10495 | set_bit(EXTENT_FLAG_PREALLOC, &em->flags); |
| 10496 | em->generation = trans->transid; |
| 10497 | |
| 10498 | while (1) { |
| 10499 | write_lock(&em_tree->lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 10500 | ret = add_extent_mapping(em_tree, em, 1); |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 10501 | write_unlock(&em_tree->lock); |
| 10502 | if (ret != -EEXIST) |
| 10503 | break; |
Nikolay Borisov | dcdbc05 | 2017-02-20 13:50:45 +0200 | [diff] [blame] | 10504 | btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset, |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 10505 | cur_offset + ins.offset - 1, |
| 10506 | 0); |
| 10507 | } |
| 10508 | free_extent_map(em); |
| 10509 | next: |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 10510 | num_bytes -= ins.offset; |
| 10511 | cur_offset += ins.offset; |
Yan, Zheng | efa5646 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 10512 | *alloc_hint = ins.objectid + ins.offset; |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 10513 | |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 10514 | inode_inc_iversion(inode); |
Deepa Dinamani | c2050a4 | 2016-09-14 07:48:06 -0700 | [diff] [blame] | 10515 | inode->i_ctime = current_time(inode); |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 10516 | BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 10517 | if (!(mode & FALLOC_FL_KEEP_SIZE) && |
Yan, Zheng | efa5646 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 10518 | (actual_len > inode->i_size) && |
| 10519 | (cur_offset > inode->i_size)) { |
Aneesh Kumar K.V | d1ea6a6 | 2010-01-20 07:28:54 +0000 | [diff] [blame] | 10520 | if (cur_offset > actual_len) |
Josef Bacik | 55a61d1 | 2010-11-22 18:50:32 +0000 | [diff] [blame] | 10521 | i_size = actual_len; |
Aneesh Kumar K.V | d1ea6a6 | 2010-01-20 07:28:54 +0000 | [diff] [blame] | 10522 | else |
Josef Bacik | 55a61d1 | 2010-11-22 18:50:32 +0000 | [diff] [blame] | 10523 | i_size = cur_offset; |
| 10524 | i_size_write(inode, i_size); |
| 10525 | btrfs_ordered_update_i_size(inode, i_size, NULL); |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 10526 | } |
| 10527 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 10528 | ret = btrfs_update_inode(trans, root, inode); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 10529 | |
| 10530 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 10531 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 10532 | if (own_trans) |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 10533 | btrfs_end_transaction(trans); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 10534 | break; |
| 10535 | } |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 10536 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 10537 | if (own_trans) |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 10538 | btrfs_end_transaction(trans); |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 10539 | } |
Wang Xiaoguang | 1851309 | 2016-07-25 15:51:40 +0800 | [diff] [blame] | 10540 | if (cur_offset < end) |
| 10541 | btrfs_free_reserved_data_space(inode, cur_offset, |
| 10542 | end - cur_offset + 1); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 10543 | return ret; |
| 10544 | } |
| 10545 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 10546 | int btrfs_prealloc_file_range(struct inode *inode, int mode, |
| 10547 | u64 start, u64 num_bytes, u64 min_size, |
| 10548 | loff_t actual_len, u64 *alloc_hint) |
| 10549 | { |
| 10550 | return __btrfs_prealloc_file_range(inode, mode, start, num_bytes, |
| 10551 | min_size, actual_len, alloc_hint, |
| 10552 | NULL); |
| 10553 | } |
| 10554 | |
| 10555 | int btrfs_prealloc_file_range_trans(struct inode *inode, |
| 10556 | struct btrfs_trans_handle *trans, int mode, |
| 10557 | u64 start, u64 num_bytes, u64 min_size, |
| 10558 | loff_t actual_len, u64 *alloc_hint) |
| 10559 | { |
| 10560 | return __btrfs_prealloc_file_range(inode, mode, start, num_bytes, |
| 10561 | min_size, actual_len, alloc_hint, trans); |
| 10562 | } |
| 10563 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 10564 | static int btrfs_set_page_dirty(struct page *page) |
| 10565 | { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 10566 | return __set_page_dirty_nobuffers(page); |
| 10567 | } |
| 10568 | |
Al Viro | 10556cb | 2011-06-20 19:28:19 -0400 | [diff] [blame] | 10569 | static int btrfs_permission(struct inode *inode, int mask) |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 10570 | { |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 10571 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Jeff Mahoney | cb6db4e | 2011-08-15 17:27:21 +0000 | [diff] [blame] | 10572 | umode_t mode = inode->i_mode; |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 10573 | |
Jeff Mahoney | cb6db4e | 2011-08-15 17:27:21 +0000 | [diff] [blame] | 10574 | if (mask & MAY_WRITE && |
| 10575 | (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) { |
| 10576 | if (btrfs_root_readonly(root)) |
| 10577 | return -EROFS; |
| 10578 | if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY) |
| 10579 | return -EACCES; |
| 10580 | } |
Al Viro | 2830ba7 | 2011-06-20 19:16:29 -0400 | [diff] [blame] | 10581 | return generic_permission(inode, mask); |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 10582 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10583 | |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 10584 | static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode) |
| 10585 | { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 10586 | struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 10587 | struct btrfs_trans_handle *trans; |
| 10588 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 10589 | struct inode *inode = NULL; |
| 10590 | u64 objectid; |
| 10591 | u64 index; |
| 10592 | int ret = 0; |
| 10593 | |
| 10594 | /* |
| 10595 | * 5 units required for adding orphan entry |
| 10596 | */ |
| 10597 | trans = btrfs_start_transaction(root, 5); |
| 10598 | if (IS_ERR(trans)) |
| 10599 | return PTR_ERR(trans); |
| 10600 | |
| 10601 | ret = btrfs_find_free_ino(root, &objectid); |
| 10602 | if (ret) |
| 10603 | goto out; |
| 10604 | |
| 10605 | inode = btrfs_new_inode(trans, root, dir, NULL, 0, |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 10606 | btrfs_ino(BTRFS_I(dir)), objectid, mode, &index); |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 10607 | if (IS_ERR(inode)) { |
| 10608 | ret = PTR_ERR(inode); |
| 10609 | inode = NULL; |
| 10610 | goto out; |
| 10611 | } |
| 10612 | |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 10613 | inode->i_fop = &btrfs_file_operations; |
| 10614 | inode->i_op = &btrfs_file_inode_operations; |
| 10615 | |
| 10616 | inode->i_mapping->a_ops = &btrfs_aops; |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 10617 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
| 10618 | |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 10619 | ret = btrfs_init_inode_security(trans, inode, dir, NULL); |
| 10620 | if (ret) |
| 10621 | goto out_inode; |
| 10622 | |
| 10623 | ret = btrfs_update_inode(trans, root, inode); |
| 10624 | if (ret) |
| 10625 | goto out_inode; |
Nikolay Borisov | 73f2e54 | 2017-02-20 13:50:59 +0200 | [diff] [blame] | 10626 | ret = btrfs_orphan_add(trans, BTRFS_I(inode)); |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 10627 | if (ret) |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 10628 | goto out_inode; |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 10629 | |
Filipe Manana | 5762b5c | 2014-08-01 00:10:32 +0100 | [diff] [blame] | 10630 | /* |
| 10631 | * We set number of links to 0 in btrfs_new_inode(), and here we set |
| 10632 | * it to 1 because d_tmpfile() will issue a warning if the count is 0, |
| 10633 | * through: |
| 10634 | * |
| 10635 | * d_tmpfile() -> inode_dec_link_count() -> drop_nlink() |
| 10636 | */ |
| 10637 | set_nlink(inode, 1); |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 10638 | unlock_new_inode(inode); |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 10639 | d_tmpfile(dentry, inode); |
| 10640 | mark_inode_dirty(inode); |
| 10641 | |
| 10642 | out: |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 10643 | btrfs_end_transaction(trans); |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 10644 | if (ret) |
| 10645 | iput(inode); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 10646 | btrfs_balance_delayed_items(fs_info); |
| 10647 | btrfs_btree_balance_dirty(fs_info); |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 10648 | return ret; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 10649 | |
| 10650 | out_inode: |
| 10651 | unlock_new_inode(inode); |
| 10652 | goto out; |
| 10653 | |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 10654 | } |
| 10655 | |
David Sterba | 20a7db8 | 2017-02-17 16:24:29 +0100 | [diff] [blame] | 10656 | __attribute__((const)) |
Liu Bo | 9d0d1c8 | 2017-03-24 15:04:50 -0700 | [diff] [blame] | 10657 | 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] | 10658 | { |
Liu Bo | 9d0d1c8 | 2017-03-24 15:04:50 -0700 | [diff] [blame] | 10659 | return -EAGAIN; |
David Sterba | 20a7db8 | 2017-02-17 16:24:29 +0100 | [diff] [blame] | 10660 | } |
| 10661 | |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 10662 | static const struct inode_operations btrfs_dir_inode_operations = { |
Chris Mason | 3394e16 | 2008-11-17 20:42:26 -0500 | [diff] [blame] | 10663 | .getattr = btrfs_getattr, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10664 | .lookup = btrfs_lookup, |
| 10665 | .create = btrfs_create, |
| 10666 | .unlink = btrfs_unlink, |
| 10667 | .link = btrfs_link, |
| 10668 | .mkdir = btrfs_mkdir, |
| 10669 | .rmdir = btrfs_rmdir, |
Miklos Szeredi | 2773bf0 | 2016-09-27 11:03:58 +0200 | [diff] [blame] | 10670 | .rename = btrfs_rename2, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10671 | .symlink = btrfs_symlink, |
| 10672 | .setattr = btrfs_setattr, |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 10673 | .mknod = btrfs_mknod, |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 10674 | .listxattr = btrfs_listxattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 10675 | .permission = btrfs_permission, |
Christoph Hellwig | 4e34e71 | 2011-07-23 17:37:31 +0200 | [diff] [blame] | 10676 | .get_acl = btrfs_get_acl, |
Christoph Hellwig | 996a710 | 2013-12-20 05:16:43 -0800 | [diff] [blame] | 10677 | .set_acl = btrfs_set_acl, |
Guangyu Sun | 93fd63c | 2013-09-16 10:42:03 -0700 | [diff] [blame] | 10678 | .update_time = btrfs_update_time, |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 10679 | .tmpfile = btrfs_tmpfile, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10680 | }; |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 10681 | static const struct inode_operations btrfs_dir_ro_inode_operations = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10682 | .lookup = btrfs_lookup, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 10683 | .permission = btrfs_permission, |
Guangyu Sun | 93fd63c | 2013-09-16 10:42:03 -0700 | [diff] [blame] | 10684 | .update_time = btrfs_update_time, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10685 | }; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 10686 | |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 10687 | static const struct file_operations btrfs_dir_file_operations = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10688 | .llseek = generic_file_llseek, |
| 10689 | .read = generic_read_dir, |
Omar Sandoval | 02dbfc9 | 2016-05-20 13:50:33 -0700 | [diff] [blame] | 10690 | .iterate_shared = btrfs_real_readdir, |
Christoph Hellwig | 34287aa | 2007-09-14 10:22:47 -0400 | [diff] [blame] | 10691 | .unlocked_ioctl = btrfs_ioctl, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10692 | #ifdef CONFIG_COMPAT |
Luke Dashjr | 4c63c24 | 2015-10-29 08:22:21 +0000 | [diff] [blame] | 10693 | .compat_ioctl = btrfs_compat_ioctl, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10694 | #endif |
Sage Weil | 6bf13c0 | 2008-06-10 10:07:39 -0400 | [diff] [blame] | 10695 | .release = btrfs_release_file, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 10696 | .fsync = btrfs_sync_file, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10697 | }; |
| 10698 | |
David Sterba | 20e5506 | 2015-11-19 11:42:28 +0100 | [diff] [blame] | 10699 | static const struct extent_io_ops btrfs_extent_io_ops = { |
David Sterba | 4d53ddd | 2017-02-17 15:27:44 +0100 | [diff] [blame] | 10700 | /* mandatory callbacks */ |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 10701 | .submit_bio_hook = btrfs_submit_bio_hook, |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 10702 | .readpage_end_io_hook = btrfs_readpage_end_io_hook, |
David Sterba | 4d53ddd | 2017-02-17 15:27:44 +0100 | [diff] [blame] | 10703 | .merge_bio_hook = btrfs_merge_bio_hook, |
Liu Bo | 9d0d1c8 | 2017-03-24 15:04:50 -0700 | [diff] [blame] | 10704 | .readpage_io_failed_hook = btrfs_readpage_io_failed_hook, |
David Sterba | 4d53ddd | 2017-02-17 15:27:44 +0100 | [diff] [blame] | 10705 | |
| 10706 | /* optional callbacks */ |
| 10707 | .fill_delalloc = run_delalloc_range, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 10708 | .writepage_end_io_hook = btrfs_writepage_end_io_hook, |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 10709 | .writepage_start_hook = btrfs_writepage_start_hook, |
Chris Mason | b0c68f8 | 2008-01-31 11:05:37 -0500 | [diff] [blame] | 10710 | .set_bit_hook = btrfs_set_bit_hook, |
| 10711 | .clear_bit_hook = btrfs_clear_bit_hook, |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 10712 | .merge_extent_hook = btrfs_merge_extent_hook, |
| 10713 | .split_extent_hook = btrfs_split_extent_hook, |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 10714 | }; |
| 10715 | |
Chris Mason | 3505439 | 2009-01-21 13:11:13 -0500 | [diff] [blame] | 10716 | /* |
| 10717 | * btrfs doesn't support the bmap operation because swapfiles |
| 10718 | * use bmap to make a mapping of extents in the file. They assume |
| 10719 | * these extents won't change over the life of the file and they |
| 10720 | * use the bmap result to do IO directly to the drive. |
| 10721 | * |
| 10722 | * the btrfs bmap call would return logical addresses that aren't |
| 10723 | * suitable for IO and they also will change frequently as COW |
| 10724 | * operations happen. So, swapfile + btrfs == corruption. |
| 10725 | * |
| 10726 | * For now we're avoiding this by dropping bmap. |
| 10727 | */ |
Alexey Dobriyan | 7f09410 | 2009-09-21 17:01:10 -0700 | [diff] [blame] | 10728 | static const struct address_space_operations btrfs_aops = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10729 | .readpage = btrfs_readpage, |
| 10730 | .writepage = btrfs_writepage, |
Chris Mason | b293f02 | 2007-11-01 19:45:34 -0400 | [diff] [blame] | 10731 | .writepages = btrfs_writepages, |
Chris Mason | 3ab2fb5 | 2007-11-08 10:59:22 -0500 | [diff] [blame] | 10732 | .readpages = btrfs_readpages, |
Chris Mason | 1643298 | 2008-04-10 10:23:21 -0400 | [diff] [blame] | 10733 | .direct_IO = btrfs_direct_IO, |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 10734 | .invalidatepage = btrfs_invalidatepage, |
| 10735 | .releasepage = btrfs_releasepage, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 10736 | .set_page_dirty = btrfs_set_page_dirty, |
Andi Kleen | 465fdd9 | 2009-09-16 11:50:18 +0200 | [diff] [blame] | 10737 | .error_remove_page = generic_error_remove_page, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10738 | }; |
| 10739 | |
Alexey Dobriyan | 7f09410 | 2009-09-21 17:01:10 -0700 | [diff] [blame] | 10740 | static const struct address_space_operations btrfs_symlink_aops = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10741 | .readpage = btrfs_readpage, |
| 10742 | .writepage = btrfs_writepage, |
Chris Mason | 2bf5a72 | 2007-08-30 11:54:02 -0400 | [diff] [blame] | 10743 | .invalidatepage = btrfs_invalidatepage, |
| 10744 | .releasepage = btrfs_releasepage, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10745 | }; |
| 10746 | |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 10747 | static const struct inode_operations btrfs_file_inode_operations = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10748 | .getattr = btrfs_getattr, |
| 10749 | .setattr = btrfs_setattr, |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 10750 | .listxattr = btrfs_listxattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 10751 | .permission = btrfs_permission, |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 10752 | .fiemap = btrfs_fiemap, |
Christoph Hellwig | 4e34e71 | 2011-07-23 17:37:31 +0200 | [diff] [blame] | 10753 | .get_acl = btrfs_get_acl, |
Christoph Hellwig | 996a710 | 2013-12-20 05:16:43 -0800 | [diff] [blame] | 10754 | .set_acl = btrfs_set_acl, |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 10755 | .update_time = btrfs_update_time, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10756 | }; |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 10757 | static const struct inode_operations btrfs_special_inode_operations = { |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 10758 | .getattr = btrfs_getattr, |
| 10759 | .setattr = btrfs_setattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 10760 | .permission = btrfs_permission, |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 10761 | .listxattr = btrfs_listxattr, |
Christoph Hellwig | 4e34e71 | 2011-07-23 17:37:31 +0200 | [diff] [blame] | 10762 | .get_acl = btrfs_get_acl, |
Christoph Hellwig | 996a710 | 2013-12-20 05:16:43 -0800 | [diff] [blame] | 10763 | .set_acl = btrfs_set_acl, |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 10764 | .update_time = btrfs_update_time, |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 10765 | }; |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 10766 | static const struct inode_operations btrfs_symlink_inode_operations = { |
Al Viro | 6b25539 | 2015-11-17 10:20:54 -0500 | [diff] [blame] | 10767 | .get_link = page_get_link, |
Li Zefan | f209561 | 2010-11-19 02:05:24 +0000 | [diff] [blame] | 10768 | .getattr = btrfs_getattr, |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 10769 | .setattr = btrfs_setattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 10770 | .permission = btrfs_permission, |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 10771 | .listxattr = btrfs_listxattr, |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 10772 | .update_time = btrfs_update_time, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10773 | }; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 10774 | |
Alexey Dobriyan | 82d339d | 2009-10-09 09:54:36 -0400 | [diff] [blame] | 10775 | const struct dentry_operations btrfs_dentry_operations = { |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 10776 | .d_delete = btrfs_dentry_delete, |
Josef Bacik | b4aff1f | 2011-06-28 16:18:59 -0400 | [diff] [blame] | 10777 | .d_release = btrfs_dentry_release, |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 10778 | }; |