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> |
Josef Bacik | 69fe2d7 | 2017-10-19 14:15:57 -0400 | [diff] [blame] | 45 | #include <linux/magic.h> |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 46 | #include "ctree.h" |
| 47 | #include "disk-io.h" |
| 48 | #include "transaction.h" |
| 49 | #include "btrfs_inode.h" |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 50 | #include "print-tree.h" |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 51 | #include "ordered-data.h" |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 52 | #include "xattr.h" |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 53 | #include "tree-log.h" |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 54 | #include "volumes.h" |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 55 | #include "compression.h" |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 56 | #include "locking.h" |
Josef Bacik | dc89e98 | 2011-01-28 17:05:48 -0500 | [diff] [blame] | 57 | #include "free-space-cache.h" |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 58 | #include "inode-map.h" |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 59 | #include "backref.h" |
Josef Bacik | f23b5a5 | 2013-06-19 10:16:26 -0400 | [diff] [blame] | 60 | #include "hash.h" |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 61 | #include "props.h" |
Dongsheng Yang | 3119321 | 2014-12-12 16:44:35 +0800 | [diff] [blame] | 62 | #include "qgroup.h" |
Wang Xiaoguang | dda3245 | 2016-07-11 11:05:29 +0800 | [diff] [blame] | 63 | #include "dedupe.h" |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 64 | |
| 65 | struct btrfs_iget_args { |
Chris Mason | 90d3e59 | 2014-01-09 17:28:00 -0800 | [diff] [blame] | 66 | struct btrfs_key *location; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 67 | struct btrfs_root *root; |
| 68 | }; |
| 69 | |
Filipe Manana | f28a492 | 2015-12-08 19:23:20 +0000 | [diff] [blame] | 70 | struct btrfs_dio_data { |
Filipe Manana | f28a492 | 2015-12-08 19:23:20 +0000 | [diff] [blame] | 71 | u64 reserve; |
| 72 | u64 unsubmitted_oe_range_start; |
| 73 | u64 unsubmitted_oe_range_end; |
Liu Bo | 4aaedfb | 2016-12-14 22:36:05 -0800 | [diff] [blame] | 74 | int overwrite; |
Filipe Manana | f28a492 | 2015-12-08 19:23:20 +0000 | [diff] [blame] | 75 | }; |
| 76 | |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 77 | static const struct inode_operations btrfs_dir_inode_operations; |
| 78 | static const struct inode_operations btrfs_symlink_inode_operations; |
| 79 | static const struct inode_operations btrfs_dir_ro_inode_operations; |
| 80 | static const struct inode_operations btrfs_special_inode_operations; |
| 81 | static const struct inode_operations btrfs_file_inode_operations; |
Alexey Dobriyan | 7f09410 | 2009-09-21 17:01:10 -0700 | [diff] [blame] | 82 | static const struct address_space_operations btrfs_aops; |
| 83 | static const struct address_space_operations btrfs_symlink_aops; |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 84 | static const struct file_operations btrfs_dir_file_operations; |
David Sterba | 20e5506 | 2015-11-19 11:42:28 +0100 | [diff] [blame] | 85 | static const struct extent_io_ops btrfs_extent_io_ops; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 86 | |
| 87 | static struct kmem_cache *btrfs_inode_cachep; |
| 88 | struct kmem_cache *btrfs_trans_handle_cachep; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 89 | struct kmem_cache *btrfs_path_cachep; |
Josef Bacik | dc89e98 | 2011-01-28 17:05:48 -0500 | [diff] [blame] | 90 | struct kmem_cache *btrfs_free_space_cachep; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 91 | |
| 92 | #define S_SHIFT 12 |
David Sterba | 4d4ab6d | 2015-11-19 11:42:31 +0100 | [diff] [blame] | 93 | static const unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 94 | [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE, |
| 95 | [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR, |
| 96 | [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV, |
| 97 | [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV, |
| 98 | [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO, |
| 99 | [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK, |
| 100 | [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK, |
| 101 | }; |
| 102 | |
Eric Sandeen | 3972f26 | 2013-01-12 02:57:22 +0000 | [diff] [blame] | 103 | static int btrfs_setsize(struct inode *inode, struct iattr *attr); |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 104 | static int btrfs_truncate(struct inode *inode); |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 105 | static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 106 | static noinline int cow_file_range(struct inode *inode, |
| 107 | struct page *locked_page, |
Wang Xiaoguang | dda3245 | 2016-07-11 11:05:29 +0800 | [diff] [blame] | 108 | u64 start, u64 end, u64 delalloc_end, |
| 109 | int *page_started, unsigned long *nr_written, |
| 110 | int unlock, struct btrfs_dedupe_hash *hash); |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 111 | static struct extent_map *create_io_em(struct inode *inode, u64 start, u64 len, |
| 112 | u64 orig_start, u64 block_start, |
| 113 | u64 block_len, u64 orig_block_len, |
| 114 | u64 ram_bytes, int compress_type, |
| 115 | int type); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 116 | |
Qu Wenruo | 52427260 | 2017-03-08 10:25:52 +0800 | [diff] [blame] | 117 | static void __endio_write_update_ordered(struct inode *inode, |
| 118 | const u64 offset, const u64 bytes, |
| 119 | const bool uptodate); |
| 120 | |
| 121 | /* |
| 122 | * Cleanup all submitted ordered extents in specified range to handle errors |
| 123 | * from the fill_dellaloc() callback. |
| 124 | * |
| 125 | * NOTE: caller must ensure that when an error happens, it can not call |
| 126 | * extent_clear_unlock_delalloc() to clear both the bits EXTENT_DO_ACCOUNTING |
| 127 | * and EXTENT_DELALLOC simultaneously, because that causes the reserved metadata |
| 128 | * to be released, which we want to happen only when finishing the ordered |
| 129 | * extent (btrfs_finish_ordered_io()). Also note that the caller of the |
| 130 | * fill_delalloc() callback already does proper cleanup for the first page of |
| 131 | * the range, that is, it invokes the callback writepage_end_io_hook() for the |
| 132 | * range of the first page. |
| 133 | */ |
| 134 | static inline void btrfs_cleanup_ordered_extents(struct inode *inode, |
| 135 | const u64 offset, |
| 136 | const u64 bytes) |
| 137 | { |
Naohiro Aota | 63d71450c | 2017-09-01 17:58:47 +0900 | [diff] [blame] | 138 | unsigned long index = offset >> PAGE_SHIFT; |
| 139 | unsigned long end_index = (offset + bytes - 1) >> PAGE_SHIFT; |
| 140 | struct page *page; |
| 141 | |
| 142 | while (index <= end_index) { |
| 143 | page = find_get_page(inode->i_mapping, index); |
| 144 | index++; |
| 145 | if (!page) |
| 146 | continue; |
| 147 | ClearPagePrivate2(page); |
| 148 | put_page(page); |
| 149 | } |
Qu Wenruo | 52427260 | 2017-03-08 10:25:52 +0800 | [diff] [blame] | 150 | return __endio_write_update_ordered(inode, offset + PAGE_SIZE, |
| 151 | bytes - PAGE_SIZE, false); |
| 152 | } |
| 153 | |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 154 | static int btrfs_dirty_inode(struct inode *inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 155 | |
Josef Bacik | 6a3891c | 2015-03-16 17:38:52 -0400 | [diff] [blame] | 156 | #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS |
| 157 | void btrfs_test_inode_set_ops(struct inode *inode) |
| 158 | { |
| 159 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
| 160 | } |
| 161 | #endif |
| 162 | |
Yan, Zheng | f34f57a | 2009-11-12 09:35:27 +0000 | [diff] [blame] | 163 | static int btrfs_init_inode_security(struct btrfs_trans_handle *trans, |
Eric Paris | 2a7dba3 | 2011-02-01 11:05:39 -0500 | [diff] [blame] | 164 | struct inode *inode, struct inode *dir, |
| 165 | const struct qstr *qstr) |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 166 | { |
| 167 | int err; |
| 168 | |
Yan, Zheng | f34f57a | 2009-11-12 09:35:27 +0000 | [diff] [blame] | 169 | err = btrfs_init_acl(trans, inode, dir); |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 170 | if (!err) |
Eric Paris | 2a7dba3 | 2011-02-01 11:05:39 -0500 | [diff] [blame] | 171 | err = btrfs_xattr_security_init(trans, inode, dir, qstr); |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 172 | return err; |
| 173 | } |
| 174 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 175 | /* |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 176 | * this does all the hard work for inserting an inline extent into |
| 177 | * the btree. The caller should have done a btrfs_drop_extents so that |
| 178 | * no overlapping inline items exist in the btree |
| 179 | */ |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 180 | static int insert_inline_extent(struct btrfs_trans_handle *trans, |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 181 | struct btrfs_path *path, int extent_inserted, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 182 | struct btrfs_root *root, struct inode *inode, |
| 183 | u64 start, size_t size, size_t compressed_size, |
Li Zefan | fe3f566 | 2011-03-28 08:30:38 +0000 | [diff] [blame] | 184 | int compress_type, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 185 | struct page **compressed_pages) |
| 186 | { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 187 | struct extent_buffer *leaf; |
| 188 | struct page *page = NULL; |
| 189 | char *kaddr; |
| 190 | unsigned long ptr; |
| 191 | struct btrfs_file_extent_item *ei; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 192 | int ret; |
| 193 | size_t cur_size = size; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 194 | unsigned long offset; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 195 | |
Li Zefan | fe3f566 | 2011-03-28 08:30:38 +0000 | [diff] [blame] | 196 | if (compressed_size && compressed_pages) |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 197 | cur_size = compressed_size; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 198 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 199 | inode_add_bytes(inode, size); |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 200 | |
| 201 | if (!extent_inserted) { |
| 202 | struct btrfs_key key; |
| 203 | size_t datasize; |
| 204 | |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 205 | key.objectid = btrfs_ino(BTRFS_I(inode)); |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 206 | key.offset = start; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 207 | key.type = BTRFS_EXTENT_DATA_KEY; |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 208 | |
| 209 | datasize = btrfs_file_extent_calc_inline_size(cur_size); |
| 210 | path->leave_spinning = 1; |
| 211 | ret = btrfs_insert_empty_item(trans, root, path, &key, |
| 212 | datasize); |
David Sterba | 79b4f4c | 2017-06-15 19:09:51 +0200 | [diff] [blame] | 213 | if (ret) |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 214 | goto fail; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 215 | } |
| 216 | leaf = path->nodes[0]; |
| 217 | ei = btrfs_item_ptr(leaf, path->slots[0], |
| 218 | struct btrfs_file_extent_item); |
| 219 | btrfs_set_file_extent_generation(leaf, ei, trans->transid); |
| 220 | btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE); |
| 221 | btrfs_set_file_extent_encryption(leaf, ei, 0); |
| 222 | btrfs_set_file_extent_other_encoding(leaf, ei, 0); |
| 223 | btrfs_set_file_extent_ram_bytes(leaf, ei, size); |
| 224 | ptr = btrfs_file_extent_inline_start(ei); |
| 225 | |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 226 | if (compress_type != BTRFS_COMPRESS_NONE) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 227 | struct page *cpage; |
| 228 | int i = 0; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 229 | while (compressed_size > 0) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 230 | cpage = compressed_pages[i]; |
Chris Mason | 5b050f0 | 2008-11-11 09:34:41 -0500 | [diff] [blame] | 231 | cur_size = min_t(unsigned long, compressed_size, |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 232 | PAGE_SIZE); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 233 | |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 234 | kaddr = kmap_atomic(cpage); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 235 | write_extent_buffer(leaf, kaddr, ptr, cur_size); |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 236 | kunmap_atomic(kaddr); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 237 | |
| 238 | i++; |
| 239 | ptr += cur_size; |
| 240 | compressed_size -= cur_size; |
| 241 | } |
| 242 | btrfs_set_file_extent_compression(leaf, ei, |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 243 | compress_type); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 244 | } else { |
| 245 | page = find_get_page(inode->i_mapping, |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 246 | start >> PAGE_SHIFT); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 247 | btrfs_set_file_extent_compression(leaf, ei, 0); |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 248 | kaddr = kmap_atomic(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 249 | offset = start & (PAGE_SIZE - 1); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 250 | write_extent_buffer(leaf, kaddr + offset, ptr, size); |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 251 | kunmap_atomic(kaddr); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 252 | put_page(page); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 253 | } |
| 254 | btrfs_mark_buffer_dirty(leaf); |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 255 | btrfs_release_path(path); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 256 | |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 257 | /* |
| 258 | * we're an inline extent, so nobody can |
| 259 | * extend the file past i_size without locking |
| 260 | * a page we already have locked. |
| 261 | * |
| 262 | * We must do any isize and inode updates |
| 263 | * before we unlock the pages. Otherwise we |
| 264 | * could end up racing with unlink. |
| 265 | */ |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 266 | BTRFS_I(inode)->disk_i_size = inode->i_size; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 267 | ret = btrfs_update_inode(trans, root, inode); |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 268 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 269 | fail: |
David Sterba | 79b4f4c | 2017-06-15 19:09:51 +0200 | [diff] [blame] | 270 | return ret; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | |
| 274 | /* |
| 275 | * conditionally insert an inline extent into the file. This |
| 276 | * does the checks required to make sure the data is small enough |
| 277 | * to fit as an inline extent. |
| 278 | */ |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 279 | static noinline int cow_file_range_inline(struct btrfs_root *root, |
| 280 | struct inode *inode, u64 start, |
| 281 | u64 end, size_t compressed_size, |
| 282 | int compress_type, |
| 283 | struct page **compressed_pages) |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 284 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 285 | struct btrfs_fs_info *fs_info = root->fs_info; |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 286 | struct btrfs_trans_handle *trans; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 287 | u64 isize = i_size_read(inode); |
| 288 | u64 actual_end = min(end + 1, isize); |
| 289 | u64 inline_len = actual_end - start; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 290 | u64 aligned_end = ALIGN(end, fs_info->sectorsize); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 291 | u64 data_len = inline_len; |
| 292 | int ret; |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 293 | struct btrfs_path *path; |
| 294 | int extent_inserted = 0; |
| 295 | u32 extent_item_size; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 296 | |
| 297 | if (compressed_size) |
| 298 | data_len = compressed_size; |
| 299 | |
| 300 | if (start > 0 || |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 301 | actual_end > fs_info->sectorsize || |
| 302 | data_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info) || |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 303 | (!compressed_size && |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 304 | (actual_end & (fs_info->sectorsize - 1)) == 0) || |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 305 | end + 1 < isize || |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 306 | data_len > fs_info->max_inline) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 307 | return 1; |
| 308 | } |
| 309 | |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 310 | path = btrfs_alloc_path(); |
| 311 | if (!path) |
| 312 | return -ENOMEM; |
| 313 | |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 314 | trans = btrfs_join_transaction(root); |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 315 | if (IS_ERR(trans)) { |
| 316 | btrfs_free_path(path); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 317 | return PTR_ERR(trans); |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 318 | } |
Josef Bacik | 69fe2d7 | 2017-10-19 14:15:57 -0400 | [diff] [blame] | 319 | trans->block_rsv = &BTRFS_I(inode)->block_rsv; |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 320 | |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 321 | if (compressed_size && compressed_pages) |
| 322 | extent_item_size = btrfs_file_extent_calc_inline_size( |
| 323 | compressed_size); |
| 324 | else |
| 325 | extent_item_size = btrfs_file_extent_calc_inline_size( |
| 326 | inline_len); |
| 327 | |
| 328 | ret = __btrfs_drop_extents(trans, root, inode, path, |
| 329 | start, aligned_end, NULL, |
| 330 | 1, 1, extent_item_size, &extent_inserted); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 331 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 332 | btrfs_abort_transaction(trans, ret); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 333 | goto out; |
| 334 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 335 | |
| 336 | if (isize > actual_end) |
| 337 | inline_len = min_t(u64, isize, actual_end); |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 338 | ret = insert_inline_extent(trans, path, extent_inserted, |
| 339 | root, inode, start, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 340 | inline_len, compressed_size, |
Li Zefan | fe3f566 | 2011-03-28 08:30:38 +0000 | [diff] [blame] | 341 | compress_type, compressed_pages); |
Josef Bacik | 2adcac1 | 2012-05-23 16:10:14 -0400 | [diff] [blame] | 342 | if (ret && ret != -ENOSPC) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 343 | btrfs_abort_transaction(trans, ret); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 344 | goto out; |
Josef Bacik | 2adcac1 | 2012-05-23 16:10:14 -0400 | [diff] [blame] | 345 | } else if (ret == -ENOSPC) { |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 346 | ret = 1; |
| 347 | goto out; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 348 | } |
Josef Bacik | 2adcac1 | 2012-05-23 16:10:14 -0400 | [diff] [blame] | 349 | |
Josef Bacik | bdc20e6 | 2013-02-28 13:23:38 -0500 | [diff] [blame] | 350 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags); |
Nikolay Borisov | dcdbc05 | 2017-02-20 13:50:45 +0200 | [diff] [blame] | 351 | btrfs_drop_extent_cache(BTRFS_I(inode), start, aligned_end - 1, 0); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 352 | out: |
Qu Wenruo | 94ed938 | 2015-09-08 17:25:56 +0800 | [diff] [blame] | 353 | /* |
| 354 | * Don't forget to free the reserved space, as for inlined extent |
| 355 | * it won't count as data extent, free them directly here. |
| 356 | * And at reserve time, it's always aligned to page size, so |
| 357 | * just free one page here. |
| 358 | */ |
Qu Wenruo | bc42bda | 2017-02-27 15:10:39 +0800 | [diff] [blame] | 359 | btrfs_qgroup_free_data(inode, NULL, 0, PAGE_SIZE); |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 360 | btrfs_free_path(path); |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 361 | btrfs_end_transaction(trans); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 362 | return ret; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 363 | } |
| 364 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 365 | struct async_extent { |
| 366 | u64 start; |
| 367 | u64 ram_size; |
| 368 | u64 compressed_size; |
| 369 | struct page **pages; |
| 370 | unsigned long nr_pages; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 371 | int compress_type; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 372 | struct list_head list; |
| 373 | }; |
| 374 | |
| 375 | struct async_cow { |
| 376 | struct inode *inode; |
| 377 | struct btrfs_root *root; |
| 378 | struct page *locked_page; |
| 379 | u64 start; |
| 380 | u64 end; |
Liu Bo | f82b735 | 2017-10-23 23:18:16 -0600 | [diff] [blame] | 381 | unsigned int write_flags; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 382 | struct list_head extents; |
| 383 | struct btrfs_work work; |
| 384 | }; |
| 385 | |
| 386 | static noinline int add_async_extent(struct async_cow *cow, |
| 387 | u64 start, u64 ram_size, |
| 388 | u64 compressed_size, |
| 389 | struct page **pages, |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 390 | unsigned long nr_pages, |
| 391 | int compress_type) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 392 | { |
| 393 | struct async_extent *async_extent; |
| 394 | |
| 395 | async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 396 | BUG_ON(!async_extent); /* -ENOMEM */ |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 397 | async_extent->start = start; |
| 398 | async_extent->ram_size = ram_size; |
| 399 | async_extent->compressed_size = compressed_size; |
| 400 | async_extent->pages = pages; |
| 401 | async_extent->nr_pages = nr_pages; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 402 | async_extent->compress_type = compress_type; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 403 | list_add_tail(&async_extent->list, &cow->extents); |
| 404 | return 0; |
| 405 | } |
| 406 | |
Timofey Titovets | c2fcdcd | 2017-07-17 16:52:58 +0300 | [diff] [blame] | 407 | static inline int inode_need_compress(struct inode *inode, u64 start, u64 end) |
Wang Shilong | f79707b | 2014-07-17 11:44:09 +0800 | [diff] [blame] | 408 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 409 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Wang Shilong | f79707b | 2014-07-17 11:44:09 +0800 | [diff] [blame] | 410 | |
| 411 | /* force compress */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 412 | if (btrfs_test_opt(fs_info, FORCE_COMPRESS)) |
Wang Shilong | f79707b | 2014-07-17 11:44:09 +0800 | [diff] [blame] | 413 | return 1; |
David Sterba | eec63c6 | 2017-07-17 19:41:31 +0200 | [diff] [blame] | 414 | /* defrag ioctl */ |
| 415 | if (BTRFS_I(inode)->defrag_compress) |
| 416 | return 1; |
Wang Shilong | f79707b | 2014-07-17 11:44:09 +0800 | [diff] [blame] | 417 | /* bad compression ratios */ |
| 418 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS) |
| 419 | return 0; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 420 | if (btrfs_test_opt(fs_info, COMPRESS) || |
Wang Shilong | f79707b | 2014-07-17 11:44:09 +0800 | [diff] [blame] | 421 | BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS || |
David Sterba | b52aa8c | 2017-07-17 19:17:20 +0200 | [diff] [blame] | 422 | BTRFS_I(inode)->prop_compress) |
Timofey Titovets | c2fcdcd | 2017-07-17 16:52:58 +0300 | [diff] [blame] | 423 | return btrfs_compress_heuristic(inode, start, end); |
Wang Shilong | f79707b | 2014-07-17 11:44:09 +0800 | [diff] [blame] | 424 | return 0; |
| 425 | } |
| 426 | |
Nikolay Borisov | 6158e1c | 2017-02-20 13:50:43 +0200 | [diff] [blame] | 427 | static inline void inode_should_defrag(struct btrfs_inode *inode, |
Anand Jain | 26d30f8 | 2016-12-19 19:09:06 +0800 | [diff] [blame] | 428 | u64 start, u64 end, u64 num_bytes, u64 small_write) |
| 429 | { |
| 430 | /* If this is a small write inside eof, kick off a defrag */ |
| 431 | if (num_bytes < small_write && |
Nikolay Borisov | 6158e1c | 2017-02-20 13:50:43 +0200 | [diff] [blame] | 432 | (start > 0 || end + 1 < inode->disk_i_size)) |
Anand Jain | 26d30f8 | 2016-12-19 19:09:06 +0800 | [diff] [blame] | 433 | btrfs_add_inode_defrag(NULL, inode); |
| 434 | } |
| 435 | |
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 | * we create compressed extents in two phases. The first |
| 438 | * phase compresses a range of pages that have already been |
| 439 | * locked (both pages and state bits are locked). |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 440 | * |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 441 | * This is done inside an ordered work queue, and the compression |
| 442 | * is spread across many cpus. The actual IO submission is step |
| 443 | * two, and the ordered work queue takes care of making sure that |
| 444 | * happens in the same order things were put onto the queue by |
| 445 | * writepages and friends. |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 446 | * |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 447 | * If this code finds it can't get good compression, it puts an |
| 448 | * entry onto the work queue to write the uncompressed bytes. This |
| 449 | * makes sure that both compressed inodes and uncompressed inodes |
Artem Bityutskiy | b257031 | 2012-07-25 18:12:06 +0300 | [diff] [blame] | 450 | * are written in the same order that the flusher thread sent them |
| 451 | * down. |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 452 | */ |
Filipe Manana | c44f649 | 2014-10-09 21:15:44 +0100 | [diff] [blame] | 453 | static noinline void compress_file_range(struct inode *inode, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 454 | struct page *locked_page, |
| 455 | u64 start, u64 end, |
| 456 | struct async_cow *async_cow, |
| 457 | int *num_added) |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 458 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 459 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 460 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 461 | u64 blocksize = fs_info->sectorsize; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 462 | u64 actual_end; |
Chris Mason | 42dc7ba | 2008-12-15 11:44:56 -0500 | [diff] [blame] | 463 | u64 isize = i_size_read(inode); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 464 | int ret = 0; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 465 | struct page **pages = NULL; |
| 466 | unsigned long nr_pages; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 467 | unsigned long total_compressed = 0; |
| 468 | unsigned long total_in = 0; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 469 | int i; |
| 470 | int will_compress; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 471 | int compress_type = fs_info->compress_type; |
Chris Mason | 4adaa61 | 2013-03-26 13:07:00 -0400 | [diff] [blame] | 472 | int redirty = 0; |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 473 | |
Nikolay Borisov | 6158e1c | 2017-02-20 13:50:43 +0200 | [diff] [blame] | 474 | inode_should_defrag(BTRFS_I(inode), start, end, end - start + 1, |
| 475 | SZ_16K); |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 476 | |
Chris Mason | 42dc7ba | 2008-12-15 11:44:56 -0500 | [diff] [blame] | 477 | actual_end = min_t(u64, isize, end + 1); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 478 | again: |
| 479 | will_compress = 0; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 480 | nr_pages = (end >> PAGE_SHIFT) - (start >> PAGE_SHIFT) + 1; |
David Sterba | 069eac7 | 2017-02-14 19:36:54 +0100 | [diff] [blame] | 481 | BUILD_BUG_ON((BTRFS_MAX_COMPRESSED % PAGE_SIZE) != 0); |
| 482 | nr_pages = min_t(unsigned long, nr_pages, |
| 483 | BTRFS_MAX_COMPRESSED / PAGE_SIZE); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 484 | |
Chris Mason | f03d9301f | 2009-02-04 09:31:06 -0500 | [diff] [blame] | 485 | /* |
| 486 | * we don't want to send crud past the end of i_size through |
| 487 | * compression, that's just a waste of CPU time. So, if the |
| 488 | * end of the file is before the start of our current |
| 489 | * requested range of bytes, we bail out to the uncompressed |
| 490 | * cleanup code that can deal with all of this. |
| 491 | * |
| 492 | * It isn't really the fastest way to fix things, but this is a |
| 493 | * very uncommon corner. |
| 494 | */ |
| 495 | if (actual_end <= start) |
| 496 | goto cleanup_and_bail_uncompressed; |
| 497 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 498 | total_compressed = actual_end - start; |
| 499 | |
Shilong Wang | 4bcbb33 | 2014-10-07 18:44:35 -0400 | [diff] [blame] | 500 | /* |
| 501 | * skip compression for a small file range(<=blocksize) that |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 502 | * 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] | 503 | */ |
| 504 | if (total_compressed <= blocksize && |
| 505 | (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size)) |
| 506 | goto cleanup_and_bail_uncompressed; |
| 507 | |
David Sterba | 069eac7 | 2017-02-14 19:36:54 +0100 | [diff] [blame] | 508 | total_compressed = min_t(unsigned long, total_compressed, |
| 509 | BTRFS_MAX_UNCOMPRESSED); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 510 | total_in = 0; |
| 511 | ret = 0; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 512 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 513 | /* |
| 514 | * we do compression for mount -o compress and when the |
| 515 | * inode has not been flagged as nocompress. This flag can |
| 516 | * change at any time if we discover bad compression ratios. |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 517 | */ |
Timofey Titovets | c2fcdcd | 2017-07-17 16:52:58 +0300 | [diff] [blame] | 518 | if (inode_need_compress(inode, start, end)) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 519 | WARN_ON(pages); |
David Sterba | 31e818f | 2015-02-20 18:00:26 +0100 | [diff] [blame] | 520 | pages = kcalloc(nr_pages, sizeof(struct page *), GFP_NOFS); |
Li Zefan | 560f7d7 | 2011-09-08 10:22:01 +0800 | [diff] [blame] | 521 | if (!pages) { |
| 522 | /* just bail out to the uncompressed code */ |
| 523 | goto cont; |
| 524 | } |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 525 | |
David Sterba | eec63c6 | 2017-07-17 19:41:31 +0200 | [diff] [blame] | 526 | if (BTRFS_I(inode)->defrag_compress) |
| 527 | compress_type = BTRFS_I(inode)->defrag_compress; |
| 528 | else if (BTRFS_I(inode)->prop_compress) |
David Sterba | b52aa8c | 2017-07-17 19:17:20 +0200 | [diff] [blame] | 529 | compress_type = BTRFS_I(inode)->prop_compress; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 530 | |
Chris Mason | 4adaa61 | 2013-03-26 13:07:00 -0400 | [diff] [blame] | 531 | /* |
| 532 | * we need to call clear_page_dirty_for_io on each |
| 533 | * page in the range. Otherwise applications with the file |
| 534 | * mmap'd can wander in and change the page contents while |
| 535 | * we are compressing them. |
| 536 | * |
| 537 | * If the compression fails for any reason, we set the pages |
| 538 | * dirty again later on. |
Timofey Titovets | e9679de | 2017-10-24 01:29:48 +0300 | [diff] [blame] | 539 | * |
| 540 | * Note that the remaining part is redirtied, the start pointer |
| 541 | * has moved, the end is the original one. |
Chris Mason | 4adaa61 | 2013-03-26 13:07:00 -0400 | [diff] [blame] | 542 | */ |
Timofey Titovets | e9679de | 2017-10-24 01:29:48 +0300 | [diff] [blame] | 543 | if (!redirty) { |
| 544 | extent_range_clear_dirty_for_io(inode, start, end); |
| 545 | redirty = 1; |
| 546 | } |
David Sterba | f51d2b5 | 2017-09-15 17:36:57 +0200 | [diff] [blame] | 547 | |
| 548 | /* Compression level is applied here and only here */ |
| 549 | ret = btrfs_compress_pages( |
| 550 | compress_type | (fs_info->compress_level << 4), |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 551 | inode->i_mapping, start, |
David Sterba | 38c3146 | 2017-02-14 19:04:07 +0100 | [diff] [blame] | 552 | pages, |
David Sterba | 4d3a800 | 2017-02-14 19:04:07 +0100 | [diff] [blame] | 553 | &nr_pages, |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 554 | &total_in, |
David Sterba | e5d7490 | 2017-02-14 19:45:05 +0100 | [diff] [blame] | 555 | &total_compressed); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 556 | |
| 557 | if (!ret) { |
| 558 | unsigned long offset = total_compressed & |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 559 | (PAGE_SIZE - 1); |
David Sterba | 4d3a800 | 2017-02-14 19:04:07 +0100 | [diff] [blame] | 560 | struct page *page = pages[nr_pages - 1]; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 561 | char *kaddr; |
| 562 | |
| 563 | /* zero the tail end of the last page, we might be |
| 564 | * sending it down to disk |
| 565 | */ |
| 566 | if (offset) { |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 567 | kaddr = kmap_atomic(page); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 568 | memset(kaddr + offset, 0, |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 569 | PAGE_SIZE - offset); |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 570 | kunmap_atomic(kaddr); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 571 | } |
| 572 | will_compress = 1; |
| 573 | } |
| 574 | } |
Li Zefan | 560f7d7 | 2011-09-08 10:22:01 +0800 | [diff] [blame] | 575 | cont: |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 576 | if (start == 0) { |
| 577 | /* lets try to make an inline extent */ |
Timofey Titovets | 6018ba0 | 2017-09-15 01:57:26 +0300 | [diff] [blame] | 578 | if (ret || total_in < actual_end) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 579 | /* we didn't compress the entire range, try |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 580 | * to make an uncompressed inline extent. |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 581 | */ |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 582 | ret = cow_file_range_inline(root, inode, start, end, |
Anand Jain | f74670f | 2016-12-06 12:43:07 +0800 | [diff] [blame] | 583 | 0, BTRFS_COMPRESS_NONE, NULL); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 584 | } else { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 585 | /* try making a compressed inline extent */ |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 586 | ret = cow_file_range_inline(root, inode, start, end, |
Li Zefan | fe3f566 | 2011-03-28 08:30:38 +0000 | [diff] [blame] | 587 | total_compressed, |
| 588 | compress_type, pages); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 589 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 590 | if (ret <= 0) { |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 591 | unsigned long clear_flags = EXTENT_DELALLOC | |
Josef Bacik | 8b62f87 | 2017-10-19 14:15:55 -0400 | [diff] [blame] | 592 | EXTENT_DELALLOC_NEW | EXTENT_DEFRAG | |
| 593 | EXTENT_DO_ACCOUNTING; |
Filipe Manana | e6eb431 | 2014-10-10 10:45:12 +0100 | [diff] [blame] | 594 | unsigned long page_error_op; |
| 595 | |
Filipe Manana | e6eb431 | 2014-10-10 10:45:12 +0100 | [diff] [blame] | 596 | page_error_op = ret < 0 ? PAGE_SET_ERROR : 0; |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 597 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 598 | /* |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 599 | * inline extent creation worked or returned error, |
| 600 | * we don't need to create any more async work items. |
| 601 | * Unlock and free up our temp pages. |
Josef Bacik | 8b62f87 | 2017-10-19 14:15:55 -0400 | [diff] [blame] | 602 | * |
| 603 | * We use DO_ACCOUNTING here because we need the |
| 604 | * delalloc_release_metadata to be done _after_ we drop |
| 605 | * our outstanding extent for clearing delalloc for this |
| 606 | * range. |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 607 | */ |
Qu Wenruo | ba8b04c | 2016-07-19 16:50:36 +0800 | [diff] [blame] | 608 | extent_clear_unlock_delalloc(inode, start, end, end, |
| 609 | NULL, clear_flags, |
| 610 | PAGE_UNLOCK | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 611 | PAGE_CLEAR_DIRTY | |
| 612 | PAGE_SET_WRITEBACK | |
Filipe Manana | e6eb431 | 2014-10-10 10:45:12 +0100 | [diff] [blame] | 613 | page_error_op | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 614 | PAGE_END_WRITEBACK); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 615 | goto free_pages_out; |
| 616 | } |
| 617 | } |
| 618 | |
| 619 | if (will_compress) { |
| 620 | /* |
| 621 | * we aren't doing an inline extent round the compressed size |
| 622 | * up to a block size boundary so the allocator does sane |
| 623 | * things |
| 624 | */ |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 625 | total_compressed = ALIGN(total_compressed, blocksize); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 626 | |
| 627 | /* |
| 628 | * one last check to make sure the compression is really a |
Timofey Titovets | 170607e | 2017-06-06 14:41:15 +0300 | [diff] [blame] | 629 | * win, compare the page count read with the blocks on disk, |
| 630 | * compression must free at least one sector size |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 631 | */ |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 632 | total_in = ALIGN(total_in, PAGE_SIZE); |
Timofey Titovets | 170607e | 2017-06-06 14:41:15 +0300 | [diff] [blame] | 633 | if (total_compressed + blocksize <= total_in) { |
Ashish Samant | c8bb0c8 | 2016-03-25 19:01:33 -0700 | [diff] [blame] | 634 | *num_added += 1; |
| 635 | |
| 636 | /* |
| 637 | * The async work queues will take care of doing actual |
| 638 | * allocation on disk for these compressed pages, and |
| 639 | * will submit them to the elevator. |
| 640 | */ |
Timofey Titovets | 1170862 | 2017-10-03 18:06:01 +0300 | [diff] [blame] | 641 | add_async_extent(async_cow, start, total_in, |
David Sterba | 4d3a800 | 2017-02-14 19:04:07 +0100 | [diff] [blame] | 642 | total_compressed, pages, nr_pages, |
Ashish Samant | c8bb0c8 | 2016-03-25 19:01:33 -0700 | [diff] [blame] | 643 | compress_type); |
| 644 | |
Timofey Titovets | 1170862 | 2017-10-03 18:06:01 +0300 | [diff] [blame] | 645 | if (start + total_in < end) { |
| 646 | start += total_in; |
Ashish Samant | c8bb0c8 | 2016-03-25 19:01:33 -0700 | [diff] [blame] | 647 | pages = NULL; |
| 648 | cond_resched(); |
| 649 | goto again; |
| 650 | } |
| 651 | return; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 652 | } |
| 653 | } |
Ashish Samant | c8bb0c8 | 2016-03-25 19:01:33 -0700 | [diff] [blame] | 654 | if (pages) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 655 | /* |
| 656 | * the compression code ran but failed to make things smaller, |
| 657 | * free any pages it allocated and our page pointer array |
| 658 | */ |
David Sterba | 4d3a800 | 2017-02-14 19:04:07 +0100 | [diff] [blame] | 659 | for (i = 0; i < nr_pages; i++) { |
Chris Mason | 70b99e6 | 2008-10-31 12:46:39 -0400 | [diff] [blame] | 660 | WARN_ON(pages[i]->mapping); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 661 | put_page(pages[i]); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 662 | } |
| 663 | kfree(pages); |
| 664 | pages = NULL; |
| 665 | total_compressed = 0; |
David Sterba | 4d3a800 | 2017-02-14 19:04:07 +0100 | [diff] [blame] | 666 | nr_pages = 0; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 667 | |
| 668 | /* flag the file so we don't compress in the future */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 669 | if (!btrfs_test_opt(fs_info, FORCE_COMPRESS) && |
David Sterba | b52aa8c | 2017-07-17 19:17:20 +0200 | [diff] [blame] | 670 | !(BTRFS_I(inode)->prop_compress)) { |
Chris Mason | a555f81 | 2010-01-28 16:18:15 -0500 | [diff] [blame] | 671 | BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS; |
Chris Mason | 1e701a3 | 2010-03-11 09:42:04 -0500 | [diff] [blame] | 672 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 673 | } |
Chris Mason | f03d9301f | 2009-02-04 09:31:06 -0500 | [diff] [blame] | 674 | cleanup_and_bail_uncompressed: |
Ashish Samant | c8bb0c8 | 2016-03-25 19:01:33 -0700 | [diff] [blame] | 675 | /* |
| 676 | * No compression, but we still need to write the pages in the file |
| 677 | * we've been given so far. redirty the locked page if it corresponds |
| 678 | * to our extent and set things up for the async work queue to run |
| 679 | * cow_file_range to do the normal delalloc dance. |
| 680 | */ |
| 681 | if (page_offset(locked_page) >= start && |
| 682 | page_offset(locked_page) <= end) |
| 683 | __set_page_dirty_nobuffers(locked_page); |
| 684 | /* unlocked later on in the async handlers */ |
| 685 | |
| 686 | if (redirty) |
| 687 | extent_range_redirty_for_io(inode, start, end); |
| 688 | add_async_extent(async_cow, start, end - start + 1, 0, NULL, 0, |
| 689 | BTRFS_COMPRESS_NONE); |
| 690 | *num_added += 1; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 691 | |
Filipe Manana | c44f649 | 2014-10-09 21:15:44 +0100 | [diff] [blame] | 692 | return; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 693 | |
| 694 | free_pages_out: |
David Sterba | 4d3a800 | 2017-02-14 19:04:07 +0100 | [diff] [blame] | 695 | for (i = 0; i < nr_pages; i++) { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 696 | WARN_ON(pages[i]->mapping); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 697 | put_page(pages[i]); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 698 | } |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 699 | kfree(pages); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 700 | } |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 701 | |
Filipe Manana | 40ae837 | 2014-10-06 22:14:24 +0100 | [diff] [blame] | 702 | static void free_async_extent_pages(struct async_extent *async_extent) |
| 703 | { |
| 704 | int i; |
| 705 | |
| 706 | if (!async_extent->pages) |
| 707 | return; |
| 708 | |
| 709 | for (i = 0; i < async_extent->nr_pages; i++) { |
| 710 | WARN_ON(async_extent->pages[i]->mapping); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 711 | put_page(async_extent->pages[i]); |
Filipe Manana | 40ae837 | 2014-10-06 22:14:24 +0100 | [diff] [blame] | 712 | } |
| 713 | kfree(async_extent->pages); |
| 714 | async_extent->nr_pages = 0; |
| 715 | async_extent->pages = NULL; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 716 | } |
| 717 | |
| 718 | /* |
| 719 | * phase two of compressed writeback. This is the ordered portion |
| 720 | * of the code, which only gets called in the order the work was |
| 721 | * queued. We walk all the async extents created by compress_file_range |
| 722 | * and send them down to the disk. |
| 723 | */ |
Filipe Manana | dec8f17 | 2014-10-06 22:14:26 +0100 | [diff] [blame] | 724 | static noinline void submit_compressed_extents(struct inode *inode, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 725 | struct async_cow *async_cow) |
| 726 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 727 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 728 | struct async_extent *async_extent; |
| 729 | u64 alloc_hint = 0; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 730 | struct btrfs_key ins; |
| 731 | struct extent_map *em; |
| 732 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 733 | struct extent_io_tree *io_tree; |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 734 | int ret = 0; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 735 | |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 736 | again: |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 737 | while (!list_empty(&async_cow->extents)) { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 738 | async_extent = list_entry(async_cow->extents.next, |
| 739 | struct async_extent, list); |
| 740 | list_del(&async_extent->list); |
| 741 | |
| 742 | io_tree = &BTRFS_I(inode)->io_tree; |
| 743 | |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 744 | retry: |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 745 | /* did the compression code fall back to uncompressed IO? */ |
| 746 | if (!async_extent->pages) { |
| 747 | int page_started = 0; |
| 748 | unsigned long nr_written = 0; |
| 749 | |
| 750 | lock_extent(io_tree, async_extent->start, |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 751 | async_extent->start + |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 752 | async_extent->ram_size - 1); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 753 | |
| 754 | /* allocate blocks */ |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 755 | ret = cow_file_range(inode, async_cow->locked_page, |
| 756 | async_extent->start, |
| 757 | async_extent->start + |
| 758 | async_extent->ram_size - 1, |
Wang Xiaoguang | dda3245 | 2016-07-11 11:05:29 +0800 | [diff] [blame] | 759 | async_extent->start + |
| 760 | async_extent->ram_size - 1, |
| 761 | &page_started, &nr_written, 0, |
| 762 | NULL); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 763 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 764 | /* JDM XXX */ |
| 765 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 766 | /* |
| 767 | * if page_started, cow_file_range inserted an |
| 768 | * inline extent and took care of all the unlocking |
| 769 | * and IO for us. Otherwise, we need to submit |
| 770 | * all those pages down to the drive. |
| 771 | */ |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 772 | if (!page_started && !ret) |
Nikolay Borisov | 5e3ee23 | 2017-12-08 15:55:58 +0200 | [diff] [blame] | 773 | extent_write_locked_range(inode, |
| 774 | async_extent->start, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 775 | async_extent->start + |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 776 | async_extent->ram_size - 1, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 777 | WB_SYNC_ALL); |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 778 | else if (ret) |
| 779 | unlock_page(async_cow->locked_page); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 780 | kfree(async_extent); |
| 781 | cond_resched(); |
| 782 | continue; |
| 783 | } |
| 784 | |
| 785 | lock_extent(io_tree, async_extent->start, |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 786 | async_extent->start + async_extent->ram_size - 1); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 787 | |
Wang Xiaoguang | 1851309 | 2016-07-25 15:51:40 +0800 | [diff] [blame] | 788 | ret = btrfs_reserve_extent(root, async_extent->ram_size, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 789 | async_extent->compressed_size, |
| 790 | async_extent->compressed_size, |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 791 | 0, alloc_hint, &ins, 1, 1); |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 792 | if (ret) { |
Filipe Manana | 40ae837 | 2014-10-06 22:14:24 +0100 | [diff] [blame] | 793 | free_async_extent_pages(async_extent); |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 794 | |
Josef Bacik | fdf8e2e | 2013-06-14 16:58:23 -0400 | [diff] [blame] | 795 | if (ret == -ENOSPC) { |
| 796 | unlock_extent(io_tree, async_extent->start, |
| 797 | async_extent->start + |
| 798 | async_extent->ram_size - 1); |
Liu Bo | ce62003 | 2014-07-24 22:48:05 +0800 | [diff] [blame] | 799 | |
| 800 | /* |
| 801 | * we need to redirty the pages if we decide to |
| 802 | * fallback to uncompressed IO, otherwise we |
| 803 | * will not submit these pages down to lower |
| 804 | * layers. |
| 805 | */ |
| 806 | extent_range_redirty_for_io(inode, |
| 807 | async_extent->start, |
| 808 | async_extent->start + |
| 809 | async_extent->ram_size - 1); |
| 810 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 811 | goto retry; |
Josef Bacik | fdf8e2e | 2013-06-14 16:58:23 -0400 | [diff] [blame] | 812 | } |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 813 | goto out_free; |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 814 | } |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 815 | /* |
| 816 | * here we're doing allocation and writeback of the |
| 817 | * compressed pages |
| 818 | */ |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 819 | em = create_io_em(inode, async_extent->start, |
| 820 | async_extent->ram_size, /* len */ |
| 821 | async_extent->start, /* orig_start */ |
| 822 | ins.objectid, /* block_start */ |
| 823 | ins.offset, /* block_len */ |
| 824 | ins.offset, /* orig_block_len */ |
| 825 | async_extent->ram_size, /* ram_bytes */ |
| 826 | async_extent->compress_type, |
| 827 | BTRFS_ORDERED_COMPRESSED); |
| 828 | if (IS_ERR(em)) |
| 829 | /* ret value is not necessary due to void function */ |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 830 | goto out_free_reserve; |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 831 | free_extent_map(em); |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 832 | |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 833 | ret = btrfs_add_ordered_extent_compress(inode, |
| 834 | async_extent->start, |
| 835 | ins.objectid, |
| 836 | async_extent->ram_size, |
| 837 | ins.offset, |
| 838 | BTRFS_ORDERED_COMPRESSED, |
| 839 | async_extent->compress_type); |
Filipe Manana | d9f8596 | 2014-08-25 10:43:00 +0100 | [diff] [blame] | 840 | if (ret) { |
Nikolay Borisov | dcdbc05 | 2017-02-20 13:50:45 +0200 | [diff] [blame] | 841 | btrfs_drop_extent_cache(BTRFS_I(inode), |
| 842 | async_extent->start, |
Filipe Manana | d9f8596 | 2014-08-25 10:43:00 +0100 | [diff] [blame] | 843 | async_extent->start + |
| 844 | async_extent->ram_size - 1, 0); |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 845 | goto out_free_reserve; |
Filipe Manana | d9f8596 | 2014-08-25 10:43:00 +0100 | [diff] [blame] | 846 | } |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 847 | btrfs_dec_block_group_reservations(fs_info, ins.objectid); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 848 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 849 | /* |
| 850 | * clear dirty, set writeback and unlock the pages. |
| 851 | */ |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 852 | extent_clear_unlock_delalloc(inode, async_extent->start, |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 853 | async_extent->start + |
| 854 | async_extent->ram_size - 1, |
Qu Wenruo | ba8b04c | 2016-07-19 16:50:36 +0800 | [diff] [blame] | 855 | async_extent->start + |
| 856 | async_extent->ram_size - 1, |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 857 | NULL, EXTENT_LOCKED | EXTENT_DELALLOC, |
| 858 | PAGE_UNLOCK | PAGE_CLEAR_DIRTY | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 859 | PAGE_SET_WRITEBACK); |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 860 | if (btrfs_submit_compressed_write(inode, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 861 | async_extent->start, |
| 862 | async_extent->ram_size, |
| 863 | ins.objectid, |
| 864 | ins.offset, async_extent->pages, |
Liu Bo | f82b735 | 2017-10-23 23:18:16 -0600 | [diff] [blame] | 865 | async_extent->nr_pages, |
| 866 | async_cow->write_flags)) { |
Filipe Manana | fce2a4e | 2014-10-06 22:14:23 +0100 | [diff] [blame] | 867 | struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree; |
| 868 | struct page *p = async_extent->pages[0]; |
| 869 | const u64 start = async_extent->start; |
| 870 | const u64 end = start + async_extent->ram_size - 1; |
| 871 | |
| 872 | p->mapping = inode->i_mapping; |
| 873 | tree->ops->writepage_end_io_hook(p, start, end, |
| 874 | NULL, 0); |
| 875 | p->mapping = NULL; |
Qu Wenruo | ba8b04c | 2016-07-19 16:50:36 +0800 | [diff] [blame] | 876 | extent_clear_unlock_delalloc(inode, start, end, end, |
| 877 | NULL, 0, |
Filipe Manana | fce2a4e | 2014-10-06 22:14:23 +0100 | [diff] [blame] | 878 | PAGE_END_WRITEBACK | |
| 879 | PAGE_SET_ERROR); |
Filipe Manana | 40ae837 | 2014-10-06 22:14:24 +0100 | [diff] [blame] | 880 | free_async_extent_pages(async_extent); |
Filipe Manana | fce2a4e | 2014-10-06 22:14:23 +0100 | [diff] [blame] | 881 | } |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 882 | alloc_hint = ins.objectid + ins.offset; |
| 883 | kfree(async_extent); |
| 884 | cond_resched(); |
| 885 | } |
Filipe Manana | dec8f17 | 2014-10-06 22:14:26 +0100 | [diff] [blame] | 886 | return; |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 887 | out_free_reserve: |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 888 | btrfs_dec_block_group_reservations(fs_info, ins.objectid); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 889 | btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 890 | out_free: |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 891 | extent_clear_unlock_delalloc(inode, async_extent->start, |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 892 | async_extent->start + |
| 893 | async_extent->ram_size - 1, |
Qu Wenruo | ba8b04c | 2016-07-19 16:50:36 +0800 | [diff] [blame] | 894 | async_extent->start + |
| 895 | async_extent->ram_size - 1, |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 896 | NULL, EXTENT_LOCKED | EXTENT_DELALLOC | |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 897 | EXTENT_DELALLOC_NEW | |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 898 | EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING, |
| 899 | PAGE_UNLOCK | PAGE_CLEAR_DIRTY | |
Filipe Manana | 704de49 | 2014-10-06 22:14:22 +0100 | [diff] [blame] | 900 | PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK | |
| 901 | PAGE_SET_ERROR); |
Filipe Manana | 40ae837 | 2014-10-06 22:14:24 +0100 | [diff] [blame] | 902 | free_async_extent_pages(async_extent); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 903 | kfree(async_extent); |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 904 | goto again; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 905 | } |
| 906 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 907 | static u64 get_extent_allocation_hint(struct inode *inode, u64 start, |
| 908 | u64 num_bytes) |
| 909 | { |
| 910 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
| 911 | struct extent_map *em; |
| 912 | u64 alloc_hint = 0; |
| 913 | |
| 914 | read_lock(&em_tree->lock); |
| 915 | em = search_extent_mapping(em_tree, start, num_bytes); |
| 916 | if (em) { |
| 917 | /* |
| 918 | * if block start isn't an actual block number then find the |
| 919 | * first block in this inode and use that as a hint. If that |
| 920 | * block is also bogus then just don't worry about it. |
| 921 | */ |
| 922 | if (em->block_start >= EXTENT_MAP_LAST_BYTE) { |
| 923 | free_extent_map(em); |
| 924 | em = search_extent_mapping(em_tree, 0, 0); |
| 925 | if (em && em->block_start < EXTENT_MAP_LAST_BYTE) |
| 926 | alloc_hint = em->block_start; |
| 927 | if (em) |
| 928 | free_extent_map(em); |
| 929 | } else { |
| 930 | alloc_hint = em->block_start; |
| 931 | free_extent_map(em); |
| 932 | } |
| 933 | } |
| 934 | read_unlock(&em_tree->lock); |
| 935 | |
| 936 | return alloc_hint; |
| 937 | } |
| 938 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 939 | /* |
| 940 | * when extent_io.c finds a delayed allocation range in the file, |
| 941 | * the call backs end up in this code. The basic idea is to |
| 942 | * allocate extents on disk for the range, and create ordered data structs |
| 943 | * in ram to track those extents. |
| 944 | * |
| 945 | * locked_page is the page that writepage had locked already. We use |
| 946 | * it to make sure we don't do extra locks or unlocks. |
| 947 | * |
| 948 | * *page_started is set to one if we unlock locked_page and do everything |
| 949 | * required to start IO on it. It may be clean and already done with |
| 950 | * IO when we return. |
| 951 | */ |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 952 | static noinline int cow_file_range(struct inode *inode, |
| 953 | struct page *locked_page, |
Wang Xiaoguang | dda3245 | 2016-07-11 11:05:29 +0800 | [diff] [blame] | 954 | u64 start, u64 end, u64 delalloc_end, |
| 955 | int *page_started, unsigned long *nr_written, |
| 956 | int unlock, struct btrfs_dedupe_hash *hash) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 957 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 958 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 959 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 960 | u64 alloc_hint = 0; |
| 961 | u64 num_bytes; |
| 962 | unsigned long ram_size; |
| 963 | u64 disk_num_bytes; |
Filipe Manana | a315e68 | 2017-03-06 23:04:20 +0000 | [diff] [blame] | 964 | u64 cur_alloc_size = 0; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 965 | u64 blocksize = fs_info->sectorsize; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 966 | struct btrfs_key ins; |
| 967 | struct extent_map *em; |
Filipe Manana | a315e68 | 2017-03-06 23:04:20 +0000 | [diff] [blame] | 968 | unsigned clear_bits; |
| 969 | unsigned long page_ops; |
| 970 | bool extent_reserved = false; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 971 | int ret = 0; |
| 972 | |
Nikolay Borisov | 70ddc55 | 2017-02-20 13:50:35 +0200 | [diff] [blame] | 973 | if (btrfs_is_free_space_inode(BTRFS_I(inode))) { |
Josef Bacik | 02ecd2c | 2013-10-25 16:19:08 -0400 | [diff] [blame] | 974 | WARN_ON_ONCE(1); |
Josef Bacik | 29bce2f | 2014-02-07 12:21:23 -0500 | [diff] [blame] | 975 | ret = -EINVAL; |
| 976 | goto out_unlock; |
Josef Bacik | 02ecd2c | 2013-10-25 16:19:08 -0400 | [diff] [blame] | 977 | } |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 978 | |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 979 | num_bytes = ALIGN(end - start + 1, blocksize); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 980 | num_bytes = max(blocksize, num_bytes); |
| 981 | disk_num_bytes = num_bytes; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 982 | |
Nikolay Borisov | 6158e1c | 2017-02-20 13:50:43 +0200 | [diff] [blame] | 983 | inode_should_defrag(BTRFS_I(inode), start, end, num_bytes, SZ_64K); |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 984 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 985 | if (start == 0) { |
| 986 | /* lets try to make an inline extent */ |
Anand Jain | f74670f | 2016-12-06 12:43:07 +0800 | [diff] [blame] | 987 | ret = cow_file_range_inline(root, inode, start, end, 0, |
| 988 | BTRFS_COMPRESS_NONE, NULL); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 989 | if (ret == 0) { |
Josef Bacik | 8b62f87 | 2017-10-19 14:15:55 -0400 | [diff] [blame] | 990 | /* |
| 991 | * We use DO_ACCOUNTING here because we need the |
| 992 | * delalloc_release_metadata to be run _after_ we drop |
| 993 | * our outstanding extent for clearing delalloc for this |
| 994 | * range. |
| 995 | */ |
Qu Wenruo | ba8b04c | 2016-07-19 16:50:36 +0800 | [diff] [blame] | 996 | extent_clear_unlock_delalloc(inode, start, end, |
| 997 | delalloc_end, NULL, |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 998 | EXTENT_LOCKED | EXTENT_DELALLOC | |
Josef Bacik | 8b62f87 | 2017-10-19 14:15:55 -0400 | [diff] [blame] | 999 | EXTENT_DELALLOC_NEW | EXTENT_DEFRAG | |
| 1000 | EXTENT_DO_ACCOUNTING, PAGE_UNLOCK | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1001 | PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK | |
| 1002 | PAGE_END_WRITEBACK); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1003 | *nr_written = *nr_written + |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1004 | (end - start + PAGE_SIZE) / PAGE_SIZE; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1005 | *page_started = 1; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1006 | goto out; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1007 | } else if (ret < 0) { |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1008 | goto out_unlock; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1009 | } |
| 1010 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1011 | |
| 1012 | BUG_ON(disk_num_bytes > |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1013 | btrfs_super_total_bytes(fs_info->super_copy)); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1014 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 1015 | alloc_hint = get_extent_allocation_hint(inode, start, num_bytes); |
Nikolay Borisov | dcdbc05 | 2017-02-20 13:50:45 +0200 | [diff] [blame] | 1016 | btrfs_drop_extent_cache(BTRFS_I(inode), start, |
| 1017 | start + num_bytes - 1, 0); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 1018 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1019 | while (disk_num_bytes > 0) { |
Josef Bacik | 287a0ab | 2010-03-19 18:07:23 +0000 | [diff] [blame] | 1020 | cur_alloc_size = disk_num_bytes; |
Wang Xiaoguang | 1851309 | 2016-07-25 15:51:40 +0800 | [diff] [blame] | 1021 | ret = btrfs_reserve_extent(root, cur_alloc_size, cur_alloc_size, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1022 | fs_info->sectorsize, 0, alloc_hint, |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 1023 | &ins, 1, 1); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 1024 | if (ret < 0) |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1025 | goto out_unlock; |
Filipe Manana | a315e68 | 2017-03-06 23:04:20 +0000 | [diff] [blame] | 1026 | cur_alloc_size = ins.offset; |
| 1027 | extent_reserved = true; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1028 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1029 | ram_size = ins.offset; |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 1030 | em = create_io_em(inode, start, ins.offset, /* len */ |
| 1031 | start, /* orig_start */ |
| 1032 | ins.objectid, /* block_start */ |
| 1033 | ins.offset, /* block_len */ |
| 1034 | ins.offset, /* orig_block_len */ |
| 1035 | ram_size, /* ram_bytes */ |
| 1036 | BTRFS_COMPRESS_NONE, /* compress_type */ |
Liu Bo | 1af4a0a | 2017-02-13 15:35:09 -0800 | [diff] [blame] | 1037 | BTRFS_ORDERED_REGULAR /* type */); |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 1038 | if (IS_ERR(em)) |
Liu Bo | ace68ba | 2013-04-22 10:53:47 +0000 | [diff] [blame] | 1039 | goto out_reserve; |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 1040 | free_extent_map(em); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1041 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1042 | ret = btrfs_add_ordered_extent(inode, start, ins.objectid, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1043 | ram_size, cur_alloc_size, 0); |
Liu Bo | ace68ba | 2013-04-22 10:53:47 +0000 | [diff] [blame] | 1044 | if (ret) |
Filipe Manana | d9f8596 | 2014-08-25 10:43:00 +0100 | [diff] [blame] | 1045 | goto out_drop_extent_cache; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1046 | |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1047 | if (root->root_key.objectid == |
| 1048 | BTRFS_DATA_RELOC_TREE_OBJECTID) { |
| 1049 | ret = btrfs_reloc_clone_csums(inode, start, |
| 1050 | cur_alloc_size); |
Qu Wenruo | 4dbd80f | 2017-03-08 10:25:51 +0800 | [diff] [blame] | 1051 | /* |
| 1052 | * Only drop cache here, and process as normal. |
| 1053 | * |
| 1054 | * We must not allow extent_clear_unlock_delalloc() |
| 1055 | * at out_unlock label to free meta of this ordered |
| 1056 | * extent, as its meta should be freed by |
| 1057 | * btrfs_finish_ordered_io(). |
| 1058 | * |
| 1059 | * So we must continue until @start is increased to |
| 1060 | * skip current ordered extent. |
| 1061 | */ |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 1062 | if (ret) |
Qu Wenruo | 4dbd80f | 2017-03-08 10:25:51 +0800 | [diff] [blame] | 1063 | btrfs_drop_extent_cache(BTRFS_I(inode), start, |
| 1064 | start + ram_size - 1, 0); |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1065 | } |
| 1066 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1067 | btrfs_dec_block_group_reservations(fs_info, ins.objectid); |
Filipe Manana | 9cfa3e3 | 2016-04-26 15:39:32 +0100 | [diff] [blame] | 1068 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1069 | /* we're not doing compressed IO, don't unlock the first |
| 1070 | * page (which the caller expects to stay locked), don't |
| 1071 | * clear any dirty bits and don't set any writeback bits |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 1072 | * |
| 1073 | * Do set the Private2 bit so we know this page was properly |
| 1074 | * setup for writepage |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1075 | */ |
Filipe Manana | a315e68 | 2017-03-06 23:04:20 +0000 | [diff] [blame] | 1076 | page_ops = unlock ? PAGE_UNLOCK : 0; |
| 1077 | page_ops |= PAGE_SET_PRIVATE2; |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 1078 | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1079 | extent_clear_unlock_delalloc(inode, start, |
Qu Wenruo | ba8b04c | 2016-07-19 16:50:36 +0800 | [diff] [blame] | 1080 | start + ram_size - 1, |
| 1081 | delalloc_end, locked_page, |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1082 | EXTENT_LOCKED | EXTENT_DELALLOC, |
Filipe Manana | a315e68 | 2017-03-06 23:04:20 +0000 | [diff] [blame] | 1083 | page_ops); |
Qu Wenruo | 4dbd80f | 2017-03-08 10:25:51 +0800 | [diff] [blame] | 1084 | if (disk_num_bytes < cur_alloc_size) |
| 1085 | disk_num_bytes = 0; |
| 1086 | else |
| 1087 | disk_num_bytes -= cur_alloc_size; |
Chris Mason | c59f895 | 2007-12-17 20:14:04 -0500 | [diff] [blame] | 1088 | num_bytes -= cur_alloc_size; |
| 1089 | alloc_hint = ins.objectid + ins.offset; |
| 1090 | start += cur_alloc_size; |
Filipe Manana | a315e68 | 2017-03-06 23:04:20 +0000 | [diff] [blame] | 1091 | extent_reserved = false; |
Qu Wenruo | 4dbd80f | 2017-03-08 10:25:51 +0800 | [diff] [blame] | 1092 | |
| 1093 | /* |
| 1094 | * btrfs_reloc_clone_csums() error, since start is increased |
| 1095 | * extent_clear_unlock_delalloc() at out_unlock label won't |
| 1096 | * free metadata of current ordered extent, we're OK to exit. |
| 1097 | */ |
| 1098 | if (ret) |
| 1099 | goto out_unlock; |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 1100 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1101 | out: |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1102 | return ret; |
Miao Xie | b7d5b0a | 2012-11-01 07:32:18 +0000 | [diff] [blame] | 1103 | |
Filipe Manana | d9f8596 | 2014-08-25 10:43:00 +0100 | [diff] [blame] | 1104 | out_drop_extent_cache: |
Nikolay Borisov | dcdbc05 | 2017-02-20 13:50:45 +0200 | [diff] [blame] | 1105 | 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] | 1106 | out_reserve: |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1107 | btrfs_dec_block_group_reservations(fs_info, ins.objectid); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 1108 | btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1109 | out_unlock: |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 1110 | clear_bits = EXTENT_LOCKED | EXTENT_DELALLOC | EXTENT_DELALLOC_NEW | |
| 1111 | EXTENT_DEFRAG | EXTENT_CLEAR_META_RESV; |
Filipe Manana | a315e68 | 2017-03-06 23:04:20 +0000 | [diff] [blame] | 1112 | page_ops = PAGE_UNLOCK | PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK | |
| 1113 | PAGE_END_WRITEBACK; |
| 1114 | /* |
| 1115 | * If we reserved an extent for our delalloc range (or a subrange) and |
| 1116 | * failed to create the respective ordered extent, then it means that |
| 1117 | * when we reserved the extent we decremented the extent's size from |
| 1118 | * the data space_info's bytes_may_use counter and incremented the |
| 1119 | * space_info's bytes_reserved counter by the same amount. We must make |
| 1120 | * sure extent_clear_unlock_delalloc() does not try to decrement again |
| 1121 | * the data space_info's bytes_may_use counter, therefore we do not pass |
| 1122 | * it the flag EXTENT_CLEAR_DATA_RESV. |
| 1123 | */ |
| 1124 | if (extent_reserved) { |
| 1125 | extent_clear_unlock_delalloc(inode, start, |
| 1126 | start + cur_alloc_size, |
| 1127 | start + cur_alloc_size, |
| 1128 | locked_page, |
| 1129 | clear_bits, |
| 1130 | page_ops); |
| 1131 | start += cur_alloc_size; |
| 1132 | if (start >= end) |
| 1133 | goto out; |
| 1134 | } |
Qu Wenruo | ba8b04c | 2016-07-19 16:50:36 +0800 | [diff] [blame] | 1135 | extent_clear_unlock_delalloc(inode, start, end, delalloc_end, |
| 1136 | locked_page, |
Filipe Manana | a315e68 | 2017-03-06 23:04:20 +0000 | [diff] [blame] | 1137 | clear_bits | EXTENT_CLEAR_DATA_RESV, |
| 1138 | page_ops); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1139 | goto out; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1140 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1141 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1142 | /* |
| 1143 | * work queue call back to started compression on a file and pages |
| 1144 | */ |
| 1145 | static noinline void async_cow_start(struct btrfs_work *work) |
| 1146 | { |
| 1147 | struct async_cow *async_cow; |
| 1148 | int num_added = 0; |
| 1149 | async_cow = container_of(work, struct async_cow, work); |
| 1150 | |
| 1151 | compress_file_range(async_cow->inode, async_cow->locked_page, |
| 1152 | async_cow->start, async_cow->end, async_cow, |
| 1153 | &num_added); |
Josef Bacik | 8180ef8 | 2012-06-08 15:16:12 -0400 | [diff] [blame] | 1154 | if (num_added == 0) { |
Josef Bacik | cb77fcd | 2012-06-15 12:19:48 -0600 | [diff] [blame] | 1155 | btrfs_add_delayed_iput(async_cow->inode); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1156 | async_cow->inode = NULL; |
Josef Bacik | 8180ef8 | 2012-06-08 15:16:12 -0400 | [diff] [blame] | 1157 | } |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1158 | } |
| 1159 | |
| 1160 | /* |
| 1161 | * work queue call back to submit previously compressed pages |
| 1162 | */ |
| 1163 | static noinline void async_cow_submit(struct btrfs_work *work) |
| 1164 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1165 | struct btrfs_fs_info *fs_info; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1166 | struct async_cow *async_cow; |
| 1167 | struct btrfs_root *root; |
| 1168 | unsigned long nr_pages; |
| 1169 | |
| 1170 | async_cow = container_of(work, struct async_cow, work); |
| 1171 | |
| 1172 | root = async_cow->root; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1173 | fs_info = root->fs_info; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1174 | nr_pages = (async_cow->end - async_cow->start + PAGE_SIZE) >> |
| 1175 | PAGE_SHIFT; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1176 | |
David Sterba | ee86395 | 2015-02-16 19:41:40 +0100 | [diff] [blame] | 1177 | /* |
| 1178 | * atomic_sub_return implies a barrier for waitqueue_active |
| 1179 | */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1180 | if (atomic_sub_return(nr_pages, &fs_info->async_delalloc_pages) < |
Byongho Lee | ee22184 | 2015-12-15 01:42:10 +0900 | [diff] [blame] | 1181 | 5 * SZ_1M && |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1182 | waitqueue_active(&fs_info->async_submit_wait)) |
| 1183 | wake_up(&fs_info->async_submit_wait); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1184 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1185 | if (async_cow->inode) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1186 | submit_compressed_extents(async_cow->inode, async_cow); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1187 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1188 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1189 | static noinline void async_cow_free(struct btrfs_work *work) |
| 1190 | { |
| 1191 | struct async_cow *async_cow; |
| 1192 | async_cow = container_of(work, struct async_cow, work); |
Josef Bacik | 8180ef8 | 2012-06-08 15:16:12 -0400 | [diff] [blame] | 1193 | if (async_cow->inode) |
Josef Bacik | cb77fcd | 2012-06-15 12:19:48 -0600 | [diff] [blame] | 1194 | btrfs_add_delayed_iput(async_cow->inode); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1195 | kfree(async_cow); |
| 1196 | } |
| 1197 | |
| 1198 | static int cow_file_range_async(struct inode *inode, struct page *locked_page, |
| 1199 | u64 start, u64 end, int *page_started, |
Liu Bo | f82b735 | 2017-10-23 23:18:16 -0600 | [diff] [blame] | 1200 | unsigned long *nr_written, |
| 1201 | unsigned int write_flags) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1202 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1203 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1204 | struct async_cow *async_cow; |
| 1205 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 1206 | unsigned long nr_pages; |
| 1207 | u64 cur_end; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1208 | |
Chris Mason | a3429ab | 2009-10-08 12:30:20 -0400 | [diff] [blame] | 1209 | clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED, |
David Sterba | ae0f162 | 2017-10-31 16:37:52 +0100 | [diff] [blame] | 1210 | 1, 0, NULL); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1211 | while (start < end) { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1212 | async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1213 | BUG_ON(!async_cow); /* -ENOMEM */ |
Josef Bacik | 8180ef8 | 2012-06-08 15:16:12 -0400 | [diff] [blame] | 1214 | async_cow->inode = igrab(inode); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1215 | async_cow->root = root; |
| 1216 | async_cow->locked_page = locked_page; |
| 1217 | async_cow->start = start; |
Liu Bo | f82b735 | 2017-10-23 23:18:16 -0600 | [diff] [blame] | 1218 | async_cow->write_flags = write_flags; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1219 | |
Wang Shilong | f79707b | 2014-07-17 11:44:09 +0800 | [diff] [blame] | 1220 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS && |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1221 | !btrfs_test_opt(fs_info, FORCE_COMPRESS)) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1222 | cur_end = end; |
| 1223 | else |
Byongho Lee | ee22184 | 2015-12-15 01:42:10 +0900 | [diff] [blame] | 1224 | cur_end = min(end, start + SZ_512K - 1); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1225 | |
| 1226 | async_cow->end = cur_end; |
| 1227 | INIT_LIST_HEAD(&async_cow->extents); |
| 1228 | |
Liu Bo | 9e0af23 | 2014-08-15 23:36:53 +0800 | [diff] [blame] | 1229 | btrfs_init_work(&async_cow->work, |
| 1230 | btrfs_delalloc_helper, |
| 1231 | async_cow_start, async_cow_submit, |
| 1232 | async_cow_free); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1233 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1234 | nr_pages = (cur_end - start + PAGE_SIZE) >> |
| 1235 | PAGE_SHIFT; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1236 | atomic_add(nr_pages, &fs_info->async_delalloc_pages); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1237 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1238 | btrfs_queue_work(fs_info->delalloc_workers, &async_cow->work); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1239 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1240 | *nr_written += nr_pages; |
| 1241 | start = cur_end + 1; |
| 1242 | } |
| 1243 | *page_started = 1; |
| 1244 | return 0; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1245 | } |
| 1246 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 1247 | 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] | 1248 | u64 bytenr, u64 num_bytes) |
| 1249 | { |
| 1250 | int ret; |
| 1251 | struct btrfs_ordered_sum *sums; |
| 1252 | LIST_HEAD(list); |
| 1253 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1254 | ret = btrfs_lookup_csums_range(fs_info->csum_root, bytenr, |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 1255 | bytenr + num_bytes - 1, &list, 0); |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1256 | if (ret == 0 && list_empty(&list)) |
| 1257 | return 0; |
| 1258 | |
| 1259 | while (!list_empty(&list)) { |
| 1260 | sums = list_entry(list.next, struct btrfs_ordered_sum, list); |
| 1261 | list_del(&sums->list); |
| 1262 | kfree(sums); |
| 1263 | } |
| 1264 | return 1; |
| 1265 | } |
| 1266 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1267 | /* |
| 1268 | * when nowcow writeback call back. This checks for snapshots or COW copies |
| 1269 | * of the extents that exist in the file, and COWs the file as required. |
| 1270 | * |
| 1271 | * If no cow copies or snapshots exist, we write directly to the existing |
| 1272 | * blocks on disk |
| 1273 | */ |
Chris Mason | 7f366cf | 2009-03-12 20:12:45 -0400 | [diff] [blame] | 1274 | static noinline int run_delalloc_nocow(struct inode *inode, |
| 1275 | struct page *locked_page, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1276 | u64 start, u64 end, int *page_started, int force, |
| 1277 | unsigned long *nr_written) |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1278 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1279 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1280 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 1281 | struct extent_buffer *leaf; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1282 | struct btrfs_path *path; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1283 | struct btrfs_file_extent_item *fi; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1284 | struct btrfs_key found_key; |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 1285 | struct extent_map *em; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1286 | u64 cow_start; |
| 1287 | u64 cur_offset; |
| 1288 | u64 extent_end; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1289 | u64 extent_offset; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1290 | u64 disk_bytenr; |
| 1291 | u64 num_bytes; |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 1292 | u64 disk_num_bytes; |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 1293 | u64 ram_bytes; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1294 | int extent_type; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1295 | int ret, err; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1296 | int type; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1297 | int nocow; |
| 1298 | int check_prev = 1; |
Li Zefan | 82d5902 | 2011-04-20 10:33:24 +0800 | [diff] [blame] | 1299 | bool nolock; |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 1300 | u64 ino = btrfs_ino(BTRFS_I(inode)); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1301 | |
| 1302 | path = btrfs_alloc_path(); |
Josef Bacik | 17ca04a | 2012-05-31 15:58:55 -0400 | [diff] [blame] | 1303 | if (!path) { |
Qu Wenruo | ba8b04c | 2016-07-19 16:50:36 +0800 | [diff] [blame] | 1304 | extent_clear_unlock_delalloc(inode, start, end, end, |
| 1305 | locked_page, |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1306 | EXTENT_LOCKED | EXTENT_DELALLOC | |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 1307 | EXTENT_DO_ACCOUNTING | |
| 1308 | EXTENT_DEFRAG, PAGE_UNLOCK | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1309 | PAGE_CLEAR_DIRTY | |
| 1310 | PAGE_SET_WRITEBACK | |
| 1311 | PAGE_END_WRITEBACK); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 1312 | return -ENOMEM; |
Josef Bacik | 17ca04a | 2012-05-31 15:58:55 -0400 | [diff] [blame] | 1313 | } |
Li Zefan | 82d5902 | 2011-04-20 10:33:24 +0800 | [diff] [blame] | 1314 | |
Nikolay Borisov | 70ddc55 | 2017-02-20 13:50:35 +0200 | [diff] [blame] | 1315 | nolock = btrfs_is_free_space_inode(BTRFS_I(inode)); |
Li Zefan | 82d5902 | 2011-04-20 10:33:24 +0800 | [diff] [blame] | 1316 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1317 | cow_start = (u64)-1; |
| 1318 | cur_offset = start; |
| 1319 | while (1) { |
Liu Bo | e4c3b2d | 2017-01-30 12:25:28 -0800 | [diff] [blame] | 1320 | ret = btrfs_lookup_file_extent(NULL, root, path, ino, |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1321 | cur_offset, 0); |
Josef Bacik | d788a34 | 2013-10-25 16:55:08 -0400 | [diff] [blame] | 1322 | if (ret < 0) |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1323 | goto error; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1324 | if (ret > 0 && path->slots[0] > 0 && check_prev) { |
| 1325 | leaf = path->nodes[0]; |
| 1326 | btrfs_item_key_to_cpu(leaf, &found_key, |
| 1327 | path->slots[0] - 1); |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 1328 | if (found_key.objectid == ino && |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1329 | found_key.type == BTRFS_EXTENT_DATA_KEY) |
| 1330 | path->slots[0]--; |
| 1331 | } |
| 1332 | check_prev = 0; |
| 1333 | next_slot: |
| 1334 | leaf = path->nodes[0]; |
| 1335 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { |
| 1336 | ret = btrfs_next_leaf(root, path); |
Josef Bacik | d788a34 | 2013-10-25 16:55:08 -0400 | [diff] [blame] | 1337 | if (ret < 0) |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1338 | goto error; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1339 | if (ret > 0) |
| 1340 | break; |
| 1341 | leaf = path->nodes[0]; |
| 1342 | } |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1343 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1344 | nocow = 0; |
| 1345 | disk_bytenr = 0; |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1346 | num_bytes = 0; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1347 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1348 | |
Filipe Manana | 1d512cb | 2015-11-09 00:33:58 +0000 | [diff] [blame] | 1349 | if (found_key.objectid > ino) |
| 1350 | break; |
| 1351 | if (WARN_ON_ONCE(found_key.objectid < ino) || |
| 1352 | found_key.type < BTRFS_EXTENT_DATA_KEY) { |
| 1353 | path->slots[0]++; |
| 1354 | goto next_slot; |
| 1355 | } |
| 1356 | if (found_key.type > BTRFS_EXTENT_DATA_KEY || |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1357 | found_key.offset > end) |
| 1358 | break; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1359 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1360 | if (found_key.offset > cur_offset) { |
| 1361 | extent_end = found_key.offset; |
Chris Mason | e9061e2 | 2009-10-09 09:57:45 -0400 | [diff] [blame] | 1362 | extent_type = 0; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1363 | goto out_check; |
| 1364 | } |
Chris Mason | c31f883 | 2008-01-08 15:46:31 -0500 | [diff] [blame] | 1365 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1366 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 1367 | struct btrfs_file_extent_item); |
| 1368 | extent_type = btrfs_file_extent_type(leaf, fi); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1369 | |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 1370 | ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1371 | if (extent_type == BTRFS_FILE_EXTENT_REG || |
| 1372 | extent_type == BTRFS_FILE_EXTENT_PREALLOC) { |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1373 | disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1374 | extent_offset = btrfs_file_extent_offset(leaf, fi); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1375 | extent_end = found_key.offset + |
| 1376 | btrfs_file_extent_num_bytes(leaf, fi); |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 1377 | disk_num_bytes = |
| 1378 | btrfs_file_extent_disk_num_bytes(leaf, fi); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1379 | if (extent_end <= start) { |
| 1380 | path->slots[0]++; |
| 1381 | goto next_slot; |
| 1382 | } |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1383 | if (disk_bytenr == 0) |
| 1384 | goto out_check; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1385 | if (btrfs_file_extent_compression(leaf, fi) || |
| 1386 | btrfs_file_extent_encryption(leaf, fi) || |
| 1387 | btrfs_file_extent_other_encoding(leaf, fi)) |
| 1388 | goto out_check; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1389 | if (extent_type == BTRFS_FILE_EXTENT_REG && !force) |
| 1390 | goto out_check; |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 1391 | if (btrfs_extent_readonly(fs_info, disk_bytenr)) |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1392 | goto out_check; |
Liu Bo | e4c3b2d | 2017-01-30 12:25:28 -0800 | [diff] [blame] | 1393 | if (btrfs_cross_ref_exist(root, ino, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1394 | found_key.offset - |
| 1395 | extent_offset, disk_bytenr)) |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1396 | goto out_check; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1397 | disk_bytenr += extent_offset; |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1398 | disk_bytenr += cur_offset - found_key.offset; |
| 1399 | num_bytes = min(end + 1, extent_end) - cur_offset; |
| 1400 | /* |
Wang Shilong | e9894fd | 2014-03-27 11:12:25 +0800 | [diff] [blame] | 1401 | * if there are pending snapshots for this root, |
| 1402 | * we fall into common COW way. |
| 1403 | */ |
| 1404 | if (!nolock) { |
David Sterba | ea14b57f | 2017-06-22 02:19:11 +0200 | [diff] [blame] | 1405 | err = btrfs_start_write_no_snapshotting(root); |
Wang Shilong | e9894fd | 2014-03-27 11:12:25 +0800 | [diff] [blame] | 1406 | if (!err) |
| 1407 | goto out_check; |
| 1408 | } |
| 1409 | /* |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1410 | * force cow if csum exists in the range. |
| 1411 | * this ensure that csum for a given extent are |
| 1412 | * either valid or do not exist. |
| 1413 | */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 1414 | if (csum_exist_in_range(fs_info, disk_bytenr, |
Robbie Ko | 91e1f56 | 2016-10-07 10:01:29 +0800 | [diff] [blame] | 1415 | num_bytes)) { |
| 1416 | if (!nolock) |
David Sterba | ea14b57f | 2017-06-22 02:19:11 +0200 | [diff] [blame] | 1417 | btrfs_end_write_no_snapshotting(root); |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1418 | goto out_check; |
Robbie Ko | 91e1f56 | 2016-10-07 10:01:29 +0800 | [diff] [blame] | 1419 | } |
| 1420 | if (!btrfs_inc_nocow_writers(fs_info, disk_bytenr)) { |
| 1421 | if (!nolock) |
David Sterba | ea14b57f | 2017-06-22 02:19:11 +0200 | [diff] [blame] | 1422 | btrfs_end_write_no_snapshotting(root); |
Filipe Manana | f78c436 | 2016-05-09 13:15:41 +0100 | [diff] [blame] | 1423 | goto out_check; |
Robbie Ko | 91e1f56 | 2016-10-07 10:01:29 +0800 | [diff] [blame] | 1424 | } |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1425 | nocow = 1; |
| 1426 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
| 1427 | extent_end = found_key.offset + |
Chris Mason | 514ac8a | 2014-01-03 21:07:00 -0800 | [diff] [blame] | 1428 | btrfs_file_extent_inline_len(leaf, |
| 1429 | path->slots[0], fi); |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 1430 | extent_end = ALIGN(extent_end, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1431 | fs_info->sectorsize); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1432 | } else { |
| 1433 | BUG_ON(1); |
| 1434 | } |
| 1435 | out_check: |
| 1436 | if (extent_end <= start) { |
| 1437 | path->slots[0]++; |
Wang Shilong | e9894fd | 2014-03-27 11:12:25 +0800 | [diff] [blame] | 1438 | if (!nolock && nocow) |
David Sterba | ea14b57f | 2017-06-22 02:19:11 +0200 | [diff] [blame] | 1439 | btrfs_end_write_no_snapshotting(root); |
Filipe Manana | f78c436 | 2016-05-09 13:15:41 +0100 | [diff] [blame] | 1440 | if (nocow) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1441 | btrfs_dec_nocow_writers(fs_info, disk_bytenr); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1442 | goto next_slot; |
| 1443 | } |
| 1444 | if (!nocow) { |
| 1445 | if (cow_start == (u64)-1) |
| 1446 | cow_start = cur_offset; |
| 1447 | cur_offset = extent_end; |
| 1448 | if (cur_offset > end) |
| 1449 | break; |
| 1450 | path->slots[0]++; |
| 1451 | goto next_slot; |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1452 | } |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1453 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1454 | btrfs_release_path(path); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1455 | if (cow_start != (u64)-1) { |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 1456 | ret = cow_file_range(inode, locked_page, |
| 1457 | cow_start, found_key.offset - 1, |
Wang Xiaoguang | dda3245 | 2016-07-11 11:05:29 +0800 | [diff] [blame] | 1458 | end, page_started, nr_written, 1, |
| 1459 | NULL); |
Wang Shilong | e9894fd | 2014-03-27 11:12:25 +0800 | [diff] [blame] | 1460 | if (ret) { |
| 1461 | if (!nolock && nocow) |
David Sterba | ea14b57f | 2017-06-22 02:19:11 +0200 | [diff] [blame] | 1462 | btrfs_end_write_no_snapshotting(root); |
Filipe Manana | f78c436 | 2016-05-09 13:15:41 +0100 | [diff] [blame] | 1463 | if (nocow) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1464 | btrfs_dec_nocow_writers(fs_info, |
Filipe Manana | f78c436 | 2016-05-09 13:15:41 +0100 | [diff] [blame] | 1465 | disk_bytenr); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1466 | goto error; |
Wang Shilong | e9894fd | 2014-03-27 11:12:25 +0800 | [diff] [blame] | 1467 | } |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1468 | cow_start = (u64)-1; |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1469 | } |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1470 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1471 | if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) { |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 1472 | u64 orig_start = found_key.offset - extent_offset; |
| 1473 | |
| 1474 | em = create_io_em(inode, cur_offset, num_bytes, |
| 1475 | orig_start, |
| 1476 | disk_bytenr, /* block_start */ |
| 1477 | num_bytes, /* block_len */ |
| 1478 | disk_num_bytes, /* orig_block_len */ |
| 1479 | ram_bytes, BTRFS_COMPRESS_NONE, |
| 1480 | BTRFS_ORDERED_PREALLOC); |
| 1481 | if (IS_ERR(em)) { |
| 1482 | if (!nolock && nocow) |
David Sterba | ea14b57f | 2017-06-22 02:19:11 +0200 | [diff] [blame] | 1483 | btrfs_end_write_no_snapshotting(root); |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 1484 | if (nocow) |
| 1485 | btrfs_dec_nocow_writers(fs_info, |
| 1486 | disk_bytenr); |
| 1487 | ret = PTR_ERR(em); |
| 1488 | goto error; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1489 | } |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 1490 | free_extent_map(em); |
| 1491 | } |
| 1492 | |
| 1493 | if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) { |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1494 | type = BTRFS_ORDERED_PREALLOC; |
| 1495 | } else { |
| 1496 | type = BTRFS_ORDERED_NOCOW; |
| 1497 | } |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1498 | |
| 1499 | ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1500 | num_bytes, num_bytes, type); |
Filipe Manana | f78c436 | 2016-05-09 13:15:41 +0100 | [diff] [blame] | 1501 | if (nocow) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1502 | btrfs_dec_nocow_writers(fs_info, disk_bytenr); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1503 | BUG_ON(ret); /* -ENOMEM */ |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1504 | |
Yan, Zheng | efa5646 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 1505 | if (root->root_key.objectid == |
Qu Wenruo | 4dbd80f | 2017-03-08 10:25:51 +0800 | [diff] [blame] | 1506 | BTRFS_DATA_RELOC_TREE_OBJECTID) |
| 1507 | /* |
| 1508 | * Error handled later, as we must prevent |
| 1509 | * extent_clear_unlock_delalloc() in error handler |
| 1510 | * from freeing metadata of created ordered extent. |
| 1511 | */ |
Yan, Zheng | efa5646 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 1512 | ret = btrfs_reloc_clone_csums(inode, cur_offset, |
| 1513 | num_bytes); |
Yan, Zheng | efa5646 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 1514 | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1515 | extent_clear_unlock_delalloc(inode, cur_offset, |
Qu Wenruo | ba8b04c | 2016-07-19 16:50:36 +0800 | [diff] [blame] | 1516 | cur_offset + num_bytes - 1, end, |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1517 | locked_page, EXTENT_LOCKED | |
Wang Xiaoguang | 1851309 | 2016-07-25 15:51:40 +0800 | [diff] [blame] | 1518 | EXTENT_DELALLOC | |
| 1519 | EXTENT_CLEAR_DATA_RESV, |
| 1520 | PAGE_UNLOCK | PAGE_SET_PRIVATE2); |
| 1521 | |
Wang Shilong | e9894fd | 2014-03-27 11:12:25 +0800 | [diff] [blame] | 1522 | if (!nolock && nocow) |
David Sterba | ea14b57f | 2017-06-22 02:19:11 +0200 | [diff] [blame] | 1523 | btrfs_end_write_no_snapshotting(root); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1524 | cur_offset = extent_end; |
Qu Wenruo | 4dbd80f | 2017-03-08 10:25:51 +0800 | [diff] [blame] | 1525 | |
| 1526 | /* |
| 1527 | * btrfs_reloc_clone_csums() error, now we're OK to call error |
| 1528 | * handler, as metadata for created ordered extent will only |
| 1529 | * be freed by btrfs_finish_ordered_io(). |
| 1530 | */ |
| 1531 | if (ret) |
| 1532 | goto error; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1533 | if (cur_offset > end) |
| 1534 | break; |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1535 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1536 | btrfs_release_path(path); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1537 | |
Josef Bacik | 17ca04a | 2012-05-31 15:58:55 -0400 | [diff] [blame] | 1538 | if (cur_offset <= end && cow_start == (u64)-1) { |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1539 | cow_start = cur_offset; |
Josef Bacik | 17ca04a | 2012-05-31 15:58:55 -0400 | [diff] [blame] | 1540 | cur_offset = end; |
| 1541 | } |
| 1542 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1543 | if (cow_start != (u64)-1) { |
Wang Xiaoguang | dda3245 | 2016-07-11 11:05:29 +0800 | [diff] [blame] | 1544 | ret = cow_file_range(inode, locked_page, cow_start, end, end, |
| 1545 | page_started, nr_written, 1, NULL); |
Josef Bacik | d788a34 | 2013-10-25 16:55:08 -0400 | [diff] [blame] | 1546 | if (ret) |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1547 | goto error; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1548 | } |
| 1549 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1550 | error: |
Josef Bacik | 17ca04a | 2012-05-31 15:58:55 -0400 | [diff] [blame] | 1551 | if (ret && cur_offset < end) |
Qu Wenruo | ba8b04c | 2016-07-19 16:50:36 +0800 | [diff] [blame] | 1552 | extent_clear_unlock_delalloc(inode, cur_offset, end, end, |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1553 | locked_page, EXTENT_LOCKED | |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 1554 | EXTENT_DELALLOC | EXTENT_DEFRAG | |
| 1555 | EXTENT_DO_ACCOUNTING, PAGE_UNLOCK | |
| 1556 | PAGE_CLEAR_DIRTY | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1557 | PAGE_SET_WRITEBACK | |
| 1558 | PAGE_END_WRITEBACK); |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1559 | btrfs_free_path(path); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1560 | return ret; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1561 | } |
| 1562 | |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 1563 | static inline int need_force_cow(struct inode *inode, u64 start, u64 end) |
| 1564 | { |
| 1565 | |
| 1566 | if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) && |
| 1567 | !(BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC)) |
| 1568 | return 0; |
| 1569 | |
| 1570 | /* |
| 1571 | * @defrag_bytes is a hint value, no spinlock held here, |
| 1572 | * if is not zero, it means the file is defragging. |
| 1573 | * Force cow if given extent needs to be defragged. |
| 1574 | */ |
| 1575 | if (BTRFS_I(inode)->defrag_bytes && |
| 1576 | test_range_bit(&BTRFS_I(inode)->io_tree, start, end, |
| 1577 | EXTENT_DEFRAG, 0, NULL)) |
| 1578 | return 1; |
| 1579 | |
| 1580 | return 0; |
| 1581 | } |
| 1582 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1583 | /* |
| 1584 | * extent_io.c call back to do delayed allocation processing |
| 1585 | */ |
Josef Bacik | c6100a4 | 2017-05-05 11:57:13 -0400 | [diff] [blame] | 1586 | static int run_delalloc_range(void *private_data, struct page *locked_page, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1587 | u64 start, u64 end, int *page_started, |
Liu Bo | f82b735 | 2017-10-23 23:18:16 -0600 | [diff] [blame] | 1588 | unsigned long *nr_written, |
| 1589 | struct writeback_control *wbc) |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1590 | { |
Josef Bacik | c6100a4 | 2017-05-05 11:57:13 -0400 | [diff] [blame] | 1591 | struct inode *inode = private_data; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1592 | int ret; |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 1593 | int force_cow = need_force_cow(inode, start, end); |
Liu Bo | f82b735 | 2017-10-23 23:18:16 -0600 | [diff] [blame] | 1594 | unsigned int write_flags = wbc_to_write_flags(wbc); |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1595 | |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 1596 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW && !force_cow) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1597 | ret = run_delalloc_nocow(inode, locked_page, start, end, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1598 | page_started, 1, nr_written); |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 1599 | } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC && !force_cow) { |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1600 | ret = run_delalloc_nocow(inode, locked_page, start, end, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1601 | page_started, 0, nr_written); |
Timofey Titovets | c2fcdcd | 2017-07-17 16:52:58 +0300 | [diff] [blame] | 1602 | } else if (!inode_need_compress(inode, start, end)) { |
Wang Xiaoguang | dda3245 | 2016-07-11 11:05:29 +0800 | [diff] [blame] | 1603 | ret = cow_file_range(inode, locked_page, start, end, end, |
| 1604 | page_started, nr_written, 1, NULL); |
Josef Bacik | 7ddf5a4 | 2012-06-08 15:26:47 -0400 | [diff] [blame] | 1605 | } else { |
| 1606 | set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, |
| 1607 | &BTRFS_I(inode)->runtime_flags); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1608 | ret = cow_file_range_async(inode, locked_page, start, end, |
Liu Bo | f82b735 | 2017-10-23 23:18:16 -0600 | [diff] [blame] | 1609 | page_started, nr_written, |
| 1610 | write_flags); |
Josef Bacik | 7ddf5a4 | 2012-06-08 15:26:47 -0400 | [diff] [blame] | 1611 | } |
Qu Wenruo | 52427260 | 2017-03-08 10:25:52 +0800 | [diff] [blame] | 1612 | if (ret) |
| 1613 | btrfs_cleanup_ordered_extents(inode, start, end - start + 1); |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 1614 | return ret; |
| 1615 | } |
| 1616 | |
Josef Bacik | c6100a4 | 2017-05-05 11:57:13 -0400 | [diff] [blame] | 1617 | static void btrfs_split_extent_hook(void *private_data, |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 1618 | struct extent_state *orig, u64 split) |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1619 | { |
Josef Bacik | c6100a4 | 2017-05-05 11:57:13 -0400 | [diff] [blame] | 1620 | struct inode *inode = private_data; |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1621 | u64 size; |
| 1622 | |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1623 | /* not delalloc, ignore it */ |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1624 | if (!(orig->state & EXTENT_DELALLOC)) |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 1625 | return; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1626 | |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1627 | size = orig->end - orig->start + 1; |
| 1628 | if (size > BTRFS_MAX_EXTENT_SIZE) { |
David Sterba | 823bb20 | 2017-01-04 11:09:51 +0100 | [diff] [blame] | 1629 | u32 num_extents; |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1630 | u64 new_size; |
| 1631 | |
| 1632 | /* |
Josef Bacik | ba11721 | 2015-03-13 15:01:24 -0400 | [diff] [blame] | 1633 | * See the explanation in btrfs_merge_extent_hook, the same |
| 1634 | * applies here, just in reverse. |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1635 | */ |
| 1636 | new_size = orig->end - split + 1; |
David Sterba | 823bb20 | 2017-01-04 11:09:51 +0100 | [diff] [blame] | 1637 | num_extents = count_max_extents(new_size); |
Josef Bacik | ba11721 | 2015-03-13 15:01:24 -0400 | [diff] [blame] | 1638 | new_size = split - orig->start; |
David Sterba | 823bb20 | 2017-01-04 11:09:51 +0100 | [diff] [blame] | 1639 | num_extents += count_max_extents(new_size); |
| 1640 | if (count_max_extents(size) >= num_extents) |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1641 | return; |
| 1642 | } |
| 1643 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1644 | spin_lock(&BTRFS_I(inode)->lock); |
Josef Bacik | 8b62f87 | 2017-10-19 14:15:55 -0400 | [diff] [blame] | 1645 | btrfs_mod_outstanding_extents(BTRFS_I(inode), 1); |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1646 | spin_unlock(&BTRFS_I(inode)->lock); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1647 | } |
| 1648 | |
| 1649 | /* |
| 1650 | * extent_io.c merge_extent_hook, used to track merged delayed allocation |
| 1651 | * extents so we can keep track of new extents that are just merged onto old |
| 1652 | * extents, such as when we are doing sequential writes, so we can properly |
| 1653 | * account for the metadata space we'll need. |
| 1654 | */ |
Josef Bacik | c6100a4 | 2017-05-05 11:57:13 -0400 | [diff] [blame] | 1655 | static void btrfs_merge_extent_hook(void *private_data, |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 1656 | struct extent_state *new, |
| 1657 | struct extent_state *other) |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1658 | { |
Josef Bacik | c6100a4 | 2017-05-05 11:57:13 -0400 | [diff] [blame] | 1659 | struct inode *inode = private_data; |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1660 | u64 new_size, old_size; |
David Sterba | 823bb20 | 2017-01-04 11:09:51 +0100 | [diff] [blame] | 1661 | u32 num_extents; |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1662 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1663 | /* not delalloc, ignore it */ |
| 1664 | if (!(other->state & EXTENT_DELALLOC)) |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 1665 | return; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1666 | |
Josef Bacik | 8461a3d | 2015-03-13 15:12:08 -0400 | [diff] [blame] | 1667 | if (new->start > other->start) |
| 1668 | new_size = new->end - other->start + 1; |
| 1669 | else |
| 1670 | new_size = other->end - new->start + 1; |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1671 | |
| 1672 | /* we're not bigger than the max, unreserve the space and go */ |
| 1673 | if (new_size <= BTRFS_MAX_EXTENT_SIZE) { |
| 1674 | spin_lock(&BTRFS_I(inode)->lock); |
Josef Bacik | 8b62f87 | 2017-10-19 14:15:55 -0400 | [diff] [blame] | 1675 | btrfs_mod_outstanding_extents(BTRFS_I(inode), -1); |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1676 | spin_unlock(&BTRFS_I(inode)->lock); |
| 1677 | return; |
| 1678 | } |
| 1679 | |
| 1680 | /* |
Josef Bacik | ba11721 | 2015-03-13 15:01:24 -0400 | [diff] [blame] | 1681 | * We have to add up either side to figure out how many extents were |
| 1682 | * accounted for before we merged into one big extent. If the number of |
| 1683 | * extents we accounted for is <= the amount we need for the new range |
| 1684 | * then we can return, otherwise drop. Think of it like this |
| 1685 | * |
| 1686 | * [ 4k][MAX_SIZE] |
| 1687 | * |
| 1688 | * So we've grown the extent by a MAX_SIZE extent, this would mean we |
| 1689 | * need 2 outstanding extents, on one side we have 1 and the other side |
| 1690 | * we have 1 so they are == and we can return. But in this case |
| 1691 | * |
| 1692 | * [MAX_SIZE+4k][MAX_SIZE+4k] |
| 1693 | * |
| 1694 | * Each range on their own accounts for 2 extents, but merged together |
| 1695 | * they are only 3 extents worth of accounting, so we need to drop in |
| 1696 | * this case. |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1697 | */ |
Josef Bacik | ba11721 | 2015-03-13 15:01:24 -0400 | [diff] [blame] | 1698 | old_size = other->end - other->start + 1; |
David Sterba | 823bb20 | 2017-01-04 11:09:51 +0100 | [diff] [blame] | 1699 | num_extents = count_max_extents(old_size); |
Josef Bacik | ba11721 | 2015-03-13 15:01:24 -0400 | [diff] [blame] | 1700 | old_size = new->end - new->start + 1; |
David Sterba | 823bb20 | 2017-01-04 11:09:51 +0100 | [diff] [blame] | 1701 | num_extents += count_max_extents(old_size); |
| 1702 | if (count_max_extents(new_size) >= num_extents) |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 1703 | return; |
| 1704 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1705 | spin_lock(&BTRFS_I(inode)->lock); |
Josef Bacik | 8b62f87 | 2017-10-19 14:15:55 -0400 | [diff] [blame] | 1706 | btrfs_mod_outstanding_extents(BTRFS_I(inode), -1); |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1707 | spin_unlock(&BTRFS_I(inode)->lock); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1708 | } |
| 1709 | |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 1710 | static void btrfs_add_delalloc_inodes(struct btrfs_root *root, |
| 1711 | struct inode *inode) |
| 1712 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1713 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
| 1714 | |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 1715 | spin_lock(&root->delalloc_lock); |
| 1716 | if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) { |
| 1717 | list_add_tail(&BTRFS_I(inode)->delalloc_inodes, |
| 1718 | &root->delalloc_inodes); |
| 1719 | set_bit(BTRFS_INODE_IN_DELALLOC_LIST, |
| 1720 | &BTRFS_I(inode)->runtime_flags); |
| 1721 | root->nr_delalloc_inodes++; |
| 1722 | if (root->nr_delalloc_inodes == 1) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1723 | spin_lock(&fs_info->delalloc_root_lock); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 1724 | BUG_ON(!list_empty(&root->delalloc_root)); |
| 1725 | list_add_tail(&root->delalloc_root, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1726 | &fs_info->delalloc_roots); |
| 1727 | spin_unlock(&fs_info->delalloc_root_lock); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 1728 | } |
| 1729 | } |
| 1730 | spin_unlock(&root->delalloc_lock); |
| 1731 | } |
| 1732 | |
| 1733 | static void btrfs_del_delalloc_inode(struct btrfs_root *root, |
Nikolay Borisov | 9e3e97f | 2017-02-20 13:51:07 +0200 | [diff] [blame] | 1734 | struct btrfs_inode *inode) |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 1735 | { |
Nikolay Borisov | 9e3e97f | 2017-02-20 13:51:07 +0200 | [diff] [blame] | 1736 | 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] | 1737 | |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 1738 | spin_lock(&root->delalloc_lock); |
Nikolay Borisov | 9e3e97f | 2017-02-20 13:51:07 +0200 | [diff] [blame] | 1739 | if (!list_empty(&inode->delalloc_inodes)) { |
| 1740 | list_del_init(&inode->delalloc_inodes); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 1741 | clear_bit(BTRFS_INODE_IN_DELALLOC_LIST, |
Nikolay Borisov | 9e3e97f | 2017-02-20 13:51:07 +0200 | [diff] [blame] | 1742 | &inode->runtime_flags); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 1743 | root->nr_delalloc_inodes--; |
| 1744 | if (!root->nr_delalloc_inodes) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1745 | spin_lock(&fs_info->delalloc_root_lock); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 1746 | BUG_ON(list_empty(&root->delalloc_root)); |
| 1747 | list_del_init(&root->delalloc_root); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1748 | spin_unlock(&fs_info->delalloc_root_lock); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 1749 | } |
| 1750 | } |
| 1751 | spin_unlock(&root->delalloc_lock); |
| 1752 | } |
| 1753 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1754 | /* |
| 1755 | * extent_io.c set_bit_hook, used to track delayed allocation |
| 1756 | * bytes in this file, and to maintain the list of inodes that |
| 1757 | * have pending delalloc work to be done. |
| 1758 | */ |
Josef Bacik | c6100a4 | 2017-05-05 11:57:13 -0400 | [diff] [blame] | 1759 | static void btrfs_set_bit_hook(void *private_data, |
David Sterba | 9ee49a04 | 2015-01-14 19:52:13 +0100 | [diff] [blame] | 1760 | struct extent_state *state, unsigned *bits) |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1761 | { |
Josef Bacik | c6100a4 | 2017-05-05 11:57:13 -0400 | [diff] [blame] | 1762 | struct inode *inode = private_data; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1763 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1764 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
| 1765 | |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 1766 | if ((*bits & EXTENT_DEFRAG) && !(*bits & EXTENT_DELALLOC)) |
| 1767 | WARN_ON(1); |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1768 | /* |
| 1769 | * set_bit and clear bit hooks normally require _irqsave/restore |
Sergei Trofimovich | 27160b6 | 2011-05-20 20:20:32 +0000 | [diff] [blame] | 1770 | * but in this case, we are only testing for the DELALLOC |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1771 | * bit, which is only set or cleared with irqs on |
| 1772 | */ |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1773 | if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) { |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1774 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1775 | u64 len = state->end + 1 - state->start; |
Josef Bacik | 8b62f87 | 2017-10-19 14:15:55 -0400 | [diff] [blame] | 1776 | u32 num_extents = count_max_extents(len); |
Nikolay Borisov | 70ddc55 | 2017-02-20 13:50:35 +0200 | [diff] [blame] | 1777 | bool do_list = !btrfs_is_free_space_inode(BTRFS_I(inode)); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1778 | |
Josef Bacik | 8b62f87 | 2017-10-19 14:15:55 -0400 | [diff] [blame] | 1779 | spin_lock(&BTRFS_I(inode)->lock); |
| 1780 | btrfs_mod_outstanding_extents(BTRFS_I(inode), num_extents); |
| 1781 | spin_unlock(&BTRFS_I(inode)->lock); |
Josef Bacik | 287a0ab | 2010-03-19 18:07:23 +0000 | [diff] [blame] | 1782 | |
Josef Bacik | 6a3891c | 2015-03-16 17:38:52 -0400 | [diff] [blame] | 1783 | /* For sanity tests */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1784 | if (btrfs_is_testing(fs_info)) |
Josef Bacik | 6a3891c | 2015-03-16 17:38:52 -0400 | [diff] [blame] | 1785 | return; |
| 1786 | |
Nikolay Borisov | 104b4e5 | 2017-06-20 21:01:20 +0300 | [diff] [blame] | 1787 | percpu_counter_add_batch(&fs_info->delalloc_bytes, len, |
| 1788 | fs_info->delalloc_batch); |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 1789 | spin_lock(&BTRFS_I(inode)->lock); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1790 | BTRFS_I(inode)->delalloc_bytes += len; |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 1791 | if (*bits & EXTENT_DEFRAG) |
| 1792 | BTRFS_I(inode)->defrag_bytes += len; |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 1793 | if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST, |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 1794 | &BTRFS_I(inode)->runtime_flags)) |
| 1795 | btrfs_add_delalloc_inodes(root, inode); |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 1796 | spin_unlock(&BTRFS_I(inode)->lock); |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1797 | } |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 1798 | |
| 1799 | if (!(state->state & EXTENT_DELALLOC_NEW) && |
| 1800 | (*bits & EXTENT_DELALLOC_NEW)) { |
| 1801 | spin_lock(&BTRFS_I(inode)->lock); |
| 1802 | BTRFS_I(inode)->new_delalloc_bytes += state->end + 1 - |
| 1803 | state->start; |
| 1804 | spin_unlock(&BTRFS_I(inode)->lock); |
| 1805 | } |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1806 | } |
| 1807 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1808 | /* |
| 1809 | * extent_io.c clear_bit_hook, see set_bit_hook for why |
| 1810 | */ |
Josef Bacik | c6100a4 | 2017-05-05 11:57:13 -0400 | [diff] [blame] | 1811 | static void btrfs_clear_bit_hook(void *private_data, |
David Sterba | 4107488 | 2013-04-29 13:38:46 +0000 | [diff] [blame] | 1812 | struct extent_state *state, |
David Sterba | 9ee49a04 | 2015-01-14 19:52:13 +0100 | [diff] [blame] | 1813 | unsigned *bits) |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1814 | { |
Josef Bacik | c6100a4 | 2017-05-05 11:57:13 -0400 | [diff] [blame] | 1815 | struct btrfs_inode *inode = BTRFS_I((struct inode *)private_data); |
Nikolay Borisov | 6fc0ef6 | 2017-02-20 13:51:03 +0200 | [diff] [blame] | 1816 | 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] | 1817 | u64 len = state->end + 1 - state->start; |
David Sterba | 823bb20 | 2017-01-04 11:09:51 +0100 | [diff] [blame] | 1818 | u32 num_extents = count_max_extents(len); |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 1819 | |
Filipe Manana | 4a4b964 | 2017-07-27 19:52:55 +0100 | [diff] [blame] | 1820 | if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG)) { |
| 1821 | spin_lock(&inode->lock); |
Nikolay Borisov | 6fc0ef6 | 2017-02-20 13:51:03 +0200 | [diff] [blame] | 1822 | inode->defrag_bytes -= len; |
Filipe Manana | 4a4b964 | 2017-07-27 19:52:55 +0100 | [diff] [blame] | 1823 | spin_unlock(&inode->lock); |
| 1824 | } |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 1825 | |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1826 | /* |
| 1827 | * set_bit and clear bit hooks normally require _irqsave/restore |
Sergei Trofimovich | 27160b6 | 2011-05-20 20:20:32 +0000 | [diff] [blame] | 1828 | * but in this case, we are only testing for the DELALLOC |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1829 | * bit, which is only set or cleared with irqs on |
| 1830 | */ |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1831 | if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) { |
Nikolay Borisov | 6fc0ef6 | 2017-02-20 13:51:03 +0200 | [diff] [blame] | 1832 | struct btrfs_root *root = inode->root; |
Liu Bo | 83eea1f | 2012-07-10 05:28:39 -0600 | [diff] [blame] | 1833 | bool do_list = !btrfs_is_free_space_inode(inode); |
Chris Mason | bcbfce8 | 2008-04-22 13:26:47 -0400 | [diff] [blame] | 1834 | |
Josef Bacik | 8b62f87 | 2017-10-19 14:15:55 -0400 | [diff] [blame] | 1835 | spin_lock(&inode->lock); |
| 1836 | btrfs_mod_outstanding_extents(inode, -num_extents); |
| 1837 | spin_unlock(&inode->lock); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1838 | |
Josef Bacik | b6d08f0 | 2013-09-27 14:57:43 -0400 | [diff] [blame] | 1839 | /* |
| 1840 | * We don't reserve metadata space for space cache inodes so we |
| 1841 | * don't need to call dellalloc_release_metadata if there is an |
| 1842 | * error. |
| 1843 | */ |
Filipe Manana | a315e68 | 2017-03-06 23:04:20 +0000 | [diff] [blame] | 1844 | if (*bits & EXTENT_CLEAR_META_RESV && |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1845 | root != fs_info->tree_root) |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1846 | btrfs_delalloc_release_metadata(inode, len); |
| 1847 | |
Josef Bacik | 6a3891c | 2015-03-16 17:38:52 -0400 | [diff] [blame] | 1848 | /* For sanity tests. */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1849 | if (btrfs_is_testing(fs_info)) |
Josef Bacik | 6a3891c | 2015-03-16 17:38:52 -0400 | [diff] [blame] | 1850 | return; |
| 1851 | |
Filipe Manana | a315e68 | 2017-03-06 23:04:20 +0000 | [diff] [blame] | 1852 | if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID && |
| 1853 | do_list && !(state->state & EXTENT_NORESERVE) && |
| 1854 | (*bits & EXTENT_CLEAR_DATA_RESV)) |
Nikolay Borisov | 6fc0ef6 | 2017-02-20 13:51:03 +0200 | [diff] [blame] | 1855 | btrfs_free_reserved_data_space_noquota( |
| 1856 | &inode->vfs_inode, |
Qu Wenruo | 51773be | 2015-10-08 18:19:37 +0800 | [diff] [blame] | 1857 | state->start, len); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1858 | |
Nikolay Borisov | 104b4e5 | 2017-06-20 21:01:20 +0300 | [diff] [blame] | 1859 | percpu_counter_add_batch(&fs_info->delalloc_bytes, -len, |
| 1860 | fs_info->delalloc_batch); |
Nikolay Borisov | 6fc0ef6 | 2017-02-20 13:51:03 +0200 | [diff] [blame] | 1861 | spin_lock(&inode->lock); |
| 1862 | inode->delalloc_bytes -= len; |
| 1863 | if (do_list && inode->delalloc_bytes == 0 && |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 1864 | test_bit(BTRFS_INODE_IN_DELALLOC_LIST, |
Nikolay Borisov | 9e3e97f | 2017-02-20 13:51:07 +0200 | [diff] [blame] | 1865 | &inode->runtime_flags)) |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 1866 | btrfs_del_delalloc_inode(root, inode); |
Nikolay Borisov | 6fc0ef6 | 2017-02-20 13:51:03 +0200 | [diff] [blame] | 1867 | spin_unlock(&inode->lock); |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1868 | } |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 1869 | |
| 1870 | if ((state->state & EXTENT_DELALLOC_NEW) && |
| 1871 | (*bits & EXTENT_DELALLOC_NEW)) { |
| 1872 | spin_lock(&inode->lock); |
| 1873 | ASSERT(inode->new_delalloc_bytes >= len); |
| 1874 | inode->new_delalloc_bytes -= len; |
| 1875 | spin_unlock(&inode->lock); |
| 1876 | } |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1877 | } |
| 1878 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1879 | /* |
| 1880 | * extent_io.c merge_bio_hook, this must check the chunk tree to make sure |
| 1881 | * we don't create bios that span stripes or chunks |
Liu Bo | 6f034ec | 2016-06-22 18:31:49 -0700 | [diff] [blame] | 1882 | * |
| 1883 | * return 1 if page cannot be merged to bio |
| 1884 | * return 0 if page can be merged to bio |
| 1885 | * return error otherwise |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1886 | */ |
Mike Christie | 81a75f67 | 2016-06-05 14:31:54 -0500 | [diff] [blame] | 1887 | int btrfs_merge_bio_hook(struct page *page, unsigned long offset, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1888 | size_t size, struct bio *bio, |
| 1889 | unsigned long bio_flags) |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1890 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1891 | struct inode *inode = page->mapping->host; |
| 1892 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 1893 | u64 logical = (u64)bio->bi_iter.bi_sector << 9; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1894 | u64 length = 0; |
| 1895 | u64 map_length; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1896 | int ret; |
| 1897 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1898 | if (bio_flags & EXTENT_BIO_COMPRESSED) |
| 1899 | return 0; |
| 1900 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 1901 | length = bio->bi_iter.bi_size; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1902 | map_length = length; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1903 | ret = btrfs_map_block(fs_info, btrfs_op(bio), logical, &map_length, |
| 1904 | NULL, 0); |
Liu Bo | 6f034ec | 2016-06-22 18:31:49 -0700 | [diff] [blame] | 1905 | if (ret < 0) |
| 1906 | return ret; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1907 | if (map_length < length + size) |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1908 | return 1; |
Jeff Mahoney | 3444a97 | 2011-10-03 23:23:13 -0400 | [diff] [blame] | 1909 | return 0; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1910 | } |
| 1911 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1912 | /* |
| 1913 | * in order to insert checksums into the metadata in large chunks, |
| 1914 | * we wait until bio submission time. All the pages in the bio are |
| 1915 | * checksummed and sums are attached onto the ordered extent record. |
| 1916 | * |
| 1917 | * At IO completion time the cums attached on the ordered extent record |
| 1918 | * are inserted into the btree |
| 1919 | */ |
Linus Torvalds | 8c27cb3 | 2017-07-05 16:41:23 -0700 | [diff] [blame] | 1920 | static blk_status_t __btrfs_submit_bio_start(void *private_data, struct bio *bio, |
Mike Christie | 81a75f67 | 2016-06-05 14:31:54 -0500 | [diff] [blame] | 1921 | int mirror_num, unsigned long bio_flags, |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 1922 | u64 bio_offset) |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 1923 | { |
Josef Bacik | c6100a4 | 2017-05-05 11:57:13 -0400 | [diff] [blame] | 1924 | struct inode *inode = private_data; |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 1925 | blk_status_t ret = 0; |
Chris Mason | e015640 | 2008-04-16 11:15:20 -0400 | [diff] [blame] | 1926 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 1927 | ret = btrfs_csum_one_bio(inode, bio, 0, 0); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1928 | BUG_ON(ret); /* -ENOMEM */ |
Chris Mason | 4a69a41 | 2008-11-06 22:03:00 -0500 | [diff] [blame] | 1929 | return 0; |
| 1930 | } |
Chris Mason | e015640 | 2008-04-16 11:15:20 -0400 | [diff] [blame] | 1931 | |
Chris Mason | 4a69a41 | 2008-11-06 22:03:00 -0500 | [diff] [blame] | 1932 | /* |
| 1933 | * in order to insert checksums into the metadata in large chunks, |
| 1934 | * we wait until bio submission time. All the pages in the bio are |
| 1935 | * checksummed and sums are attached onto the ordered extent record. |
| 1936 | * |
| 1937 | * At IO completion time the cums attached on the ordered extent record |
| 1938 | * are inserted into the btree |
| 1939 | */ |
Linus Torvalds | 8c27cb3 | 2017-07-05 16:41:23 -0700 | [diff] [blame] | 1940 | static blk_status_t __btrfs_submit_bio_done(void *private_data, struct bio *bio, |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 1941 | int mirror_num, unsigned long bio_flags, |
| 1942 | u64 bio_offset) |
Chris Mason | 4a69a41 | 2008-11-06 22:03:00 -0500 | [diff] [blame] | 1943 | { |
Josef Bacik | c6100a4 | 2017-05-05 11:57:13 -0400 | [diff] [blame] | 1944 | struct inode *inode = private_data; |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 1945 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 1946 | blk_status_t ret; |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 1947 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 1948 | ret = btrfs_map_bio(fs_info, bio, mirror_num, 1); |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 1949 | if (ret) { |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 1950 | bio->bi_status = ret; |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 1951 | bio_endio(bio); |
| 1952 | } |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 1953 | return ret; |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 1954 | } |
| 1955 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1956 | /* |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1957 | * extent_io.c submission hook. This does the right thing for csum calculation |
Liu Bo | 4c274bc | 2017-11-01 17:19:27 -0600 | [diff] [blame] | 1958 | * on write, or reading the csums from the tree before a read. |
| 1959 | * |
| 1960 | * Rules about async/sync submit, |
| 1961 | * a) read: sync submit |
| 1962 | * |
| 1963 | * b) write without checksum: sync submit |
| 1964 | * |
| 1965 | * c) write with checksum: |
| 1966 | * c-1) if bio is issued by fsync: sync submit |
| 1967 | * (sync_writers != 0) |
| 1968 | * |
| 1969 | * c-2) if root is reloc root: sync submit |
| 1970 | * (only in case of buffered IO) |
| 1971 | * |
| 1972 | * c-3) otherwise: async submit |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1973 | */ |
Linus Torvalds | 8c27cb3 | 2017-07-05 16:41:23 -0700 | [diff] [blame] | 1974 | static blk_status_t btrfs_submit_bio_hook(void *private_data, struct bio *bio, |
Josef Bacik | c6100a4 | 2017-05-05 11:57:13 -0400 | [diff] [blame] | 1975 | int mirror_num, unsigned long bio_flags, |
| 1976 | u64 bio_offset) |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 1977 | { |
Josef Bacik | c6100a4 | 2017-05-05 11:57:13 -0400 | [diff] [blame] | 1978 | struct inode *inode = private_data; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1979 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 1980 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chandan Rajendra | 0d51e28 | 2015-07-27 15:26:43 +0530 | [diff] [blame] | 1981 | enum btrfs_wq_endio_type metadata = BTRFS_WQ_ENDIO_DATA; |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 1982 | blk_status_t ret = 0; |
Chris Mason | 19b9bdb | 2008-10-30 14:23:13 -0400 | [diff] [blame] | 1983 | int skip_sum; |
Josef Bacik | b812ce2 | 2012-11-16 13:56:32 -0500 | [diff] [blame] | 1984 | int async = !atomic_read(&BTRFS_I(inode)->sync_writers); |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 1985 | |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 1986 | skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM; |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1987 | |
Nikolay Borisov | 70ddc55 | 2017-02-20 13:50:35 +0200 | [diff] [blame] | 1988 | if (btrfs_is_free_space_inode(BTRFS_I(inode))) |
Chandan Rajendra | 0d51e28 | 2015-07-27 15:26:43 +0530 | [diff] [blame] | 1989 | metadata = BTRFS_WQ_ENDIO_FREE_SPACE; |
Jeff Mahoney | 0417341 | 2011-10-03 23:23:12 -0400 | [diff] [blame] | 1990 | |
Mike Christie | 37226b2 | 2016-06-05 14:31:52 -0500 | [diff] [blame] | 1991 | if (bio_op(bio) != REQ_OP_WRITE) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1992 | ret = btrfs_bio_wq_end_io(fs_info, bio, metadata); |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 1993 | if (ret) |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 1994 | goto out; |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 1995 | |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1996 | if (bio_flags & EXTENT_BIO_COMPRESSED) { |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 1997 | ret = btrfs_submit_compressed_read(inode, bio, |
| 1998 | mirror_num, |
| 1999 | bio_flags); |
| 2000 | goto out; |
Tsutomu Itoh | c2db107 | 2011-03-01 06:48:31 +0000 | [diff] [blame] | 2001 | } else if (!skip_sum) { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2002 | ret = btrfs_lookup_bio_sums(inode, bio, NULL); |
Tsutomu Itoh | c2db107 | 2011-03-01 06:48:31 +0000 | [diff] [blame] | 2003 | if (ret) |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 2004 | goto out; |
Tsutomu Itoh | c2db107 | 2011-03-01 06:48:31 +0000 | [diff] [blame] | 2005 | } |
Chris Mason | 4d1b5fb | 2008-08-20 09:44:52 -0400 | [diff] [blame] | 2006 | goto mapit; |
Josef Bacik | b812ce2 | 2012-11-16 13:56:32 -0500 | [diff] [blame] | 2007 | } else if (async && !skip_sum) { |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 2008 | /* csum items have already been cloned */ |
| 2009 | if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID) |
| 2010 | goto mapit; |
Chris Mason | 19b9bdb | 2008-10-30 14:23:13 -0400 | [diff] [blame] | 2011 | /* we're doing a write, do the async checksumming */ |
Josef Bacik | c6100a4 | 2017-05-05 11:57:13 -0400 | [diff] [blame] | 2012 | ret = btrfs_wq_submit_bio(fs_info, bio, mirror_num, bio_flags, |
| 2013 | bio_offset, inode, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2014 | __btrfs_submit_bio_start, |
| 2015 | __btrfs_submit_bio_done); |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 2016 | goto out; |
Josef Bacik | b812ce2 | 2012-11-16 13:56:32 -0500 | [diff] [blame] | 2017 | } else if (!skip_sum) { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2018 | ret = btrfs_csum_one_bio(inode, bio, 0, 0); |
Josef Bacik | b812ce2 | 2012-11-16 13:56:32 -0500 | [diff] [blame] | 2019 | if (ret) |
| 2020 | goto out; |
Chris Mason | 19b9bdb | 2008-10-30 14:23:13 -0400 | [diff] [blame] | 2021 | } |
| 2022 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2023 | mapit: |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2024 | ret = btrfs_map_bio(fs_info, bio, mirror_num, 0); |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 2025 | |
| 2026 | out: |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 2027 | if (ret) { |
| 2028 | bio->bi_status = ret; |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 2029 | bio_endio(bio); |
| 2030 | } |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 2031 | return ret; |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 2032 | } |
Chris Mason | 6885f30 | 2008-02-20 16:11:05 -0500 | [diff] [blame] | 2033 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2034 | /* |
| 2035 | * given a list of ordered sums record them in the inode. This happens |
| 2036 | * at IO completion time based on sums calculated at bio submission time. |
| 2037 | */ |
Chris Mason | ba1da2f | 2008-07-17 12:54:15 -0400 | [diff] [blame] | 2038 | static noinline int add_pending_csums(struct btrfs_trans_handle *trans, |
David Sterba | df9f628 | 2017-02-10 19:35:37 +0100 | [diff] [blame] | 2039 | struct inode *inode, struct list_head *list) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2040 | { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2041 | struct btrfs_ordered_sum *sum; |
| 2042 | |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 2043 | list_for_each_entry(sum, list, list) { |
David Sterba | 7c2871a | 2017-11-08 01:07:43 +0100 | [diff] [blame] | 2044 | trans->adding_csums = true; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 2045 | btrfs_csum_file_blocks(trans, |
| 2046 | BTRFS_I(inode)->root->fs_info->csum_root, sum); |
David Sterba | 7c2871a | 2017-11-08 01:07:43 +0100 | [diff] [blame] | 2047 | trans->adding_csums = false; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2048 | } |
| 2049 | return 0; |
| 2050 | } |
| 2051 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 2052 | int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end, |
Filipe Manana | e3b8a48 | 2017-11-04 00:16:59 +0000 | [diff] [blame] | 2053 | unsigned int extra_bits, |
Qu Wenruo | ba8b04c | 2016-07-19 16:50:36 +0800 | [diff] [blame] | 2054 | struct extent_state **cached_state, int dedupe) |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 2055 | { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2056 | WARN_ON((end & (PAGE_SIZE - 1)) == 0); |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 2057 | return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end, |
Filipe Manana | e3b8a48 | 2017-11-04 00:16:59 +0000 | [diff] [blame] | 2058 | extra_bits, cached_state); |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 2059 | } |
| 2060 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2061 | /* see btrfs_writepage_start_hook for details on why this is required */ |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2062 | struct btrfs_writepage_fixup { |
| 2063 | struct page *page; |
| 2064 | struct btrfs_work work; |
| 2065 | }; |
| 2066 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 2067 | static void btrfs_writepage_fixup_worker(struct btrfs_work *work) |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2068 | { |
| 2069 | struct btrfs_writepage_fixup *fixup; |
| 2070 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 2071 | struct extent_state *cached_state = NULL; |
Qu Wenruo | 364ecf3 | 2017-02-27 15:10:38 +0800 | [diff] [blame] | 2072 | struct extent_changeset *data_reserved = NULL; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2073 | struct page *page; |
| 2074 | struct inode *inode; |
| 2075 | u64 page_start; |
| 2076 | u64 page_end; |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 2077 | int ret; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2078 | |
| 2079 | fixup = container_of(work, struct btrfs_writepage_fixup, work); |
| 2080 | page = fixup->page; |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 2081 | again: |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2082 | lock_page(page); |
| 2083 | if (!page->mapping || !PageDirty(page) || !PageChecked(page)) { |
| 2084 | ClearPageChecked(page); |
| 2085 | goto out_page; |
| 2086 | } |
| 2087 | |
| 2088 | inode = page->mapping->host; |
| 2089 | page_start = page_offset(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2090 | page_end = page_offset(page) + PAGE_SIZE - 1; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2091 | |
David Sterba | ff13db4 | 2015-12-03 14:30:40 +0100 | [diff] [blame] | 2092 | lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end, |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 2093 | &cached_state); |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 2094 | |
| 2095 | /* already ordered? We're done */ |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 2096 | if (PagePrivate2(page)) |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2097 | goto out; |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 2098 | |
Nikolay Borisov | a776c6f | 2017-02-20 13:50:49 +0200 | [diff] [blame] | 2099 | ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start, |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2100 | PAGE_SIZE); |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 2101 | if (ordered) { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 2102 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, |
| 2103 | page_end, &cached_state, GFP_NOFS); |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 2104 | unlock_page(page); |
| 2105 | btrfs_start_ordered_extent(inode, ordered, 1); |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 2106 | btrfs_put_ordered_extent(ordered); |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 2107 | goto again; |
| 2108 | } |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2109 | |
Qu Wenruo | 364ecf3 | 2017-02-27 15:10:38 +0800 | [diff] [blame] | 2110 | ret = btrfs_delalloc_reserve_space(inode, &data_reserved, page_start, |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2111 | PAGE_SIZE); |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 2112 | if (ret) { |
| 2113 | mapping_set_error(page->mapping, ret); |
| 2114 | end_extent_writepage(page, ret, page_start, page_end); |
| 2115 | ClearPageChecked(page); |
| 2116 | goto out; |
| 2117 | } |
| 2118 | |
Nikolay Borisov | f3038ee | 2017-12-05 09:29:19 +0200 | [diff] [blame] | 2119 | ret = btrfs_set_extent_delalloc(inode, page_start, page_end, 0, |
| 2120 | &cached_state, 0); |
| 2121 | if (ret) { |
| 2122 | mapping_set_error(page->mapping, ret); |
| 2123 | end_extent_writepage(page, ret, page_start, page_end); |
| 2124 | ClearPageChecked(page); |
| 2125 | goto out; |
| 2126 | } |
| 2127 | |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2128 | ClearPageChecked(page); |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 2129 | set_page_dirty(page); |
Josef Bacik | 8b62f87 | 2017-10-19 14:15:55 -0400 | [diff] [blame] | 2130 | btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2131 | out: |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 2132 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end, |
| 2133 | &cached_state, GFP_NOFS); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2134 | out_page: |
| 2135 | unlock_page(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2136 | put_page(page); |
Miao Xie | b897abe | 2011-01-26 16:19:22 +0800 | [diff] [blame] | 2137 | kfree(fixup); |
Qu Wenruo | 364ecf3 | 2017-02-27 15:10:38 +0800 | [diff] [blame] | 2138 | extent_changeset_free(data_reserved); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2139 | } |
| 2140 | |
| 2141 | /* |
| 2142 | * There are a few paths in the higher layers of the kernel that directly |
| 2143 | * set the page dirty bit without asking the filesystem if it is a |
| 2144 | * good idea. This causes problems because we want to make sure COW |
| 2145 | * properly happens and the data=ordered rules are followed. |
| 2146 | * |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2147 | * 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] | 2148 | * hasn't been properly setup for IO. We kick off an async process |
| 2149 | * to fix it up. The async helper will wait for ordered extents, set |
| 2150 | * the delalloc bit and make it safe to write the page. |
| 2151 | */ |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 2152 | 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] | 2153 | { |
| 2154 | struct inode *inode = page->mapping->host; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2155 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2156 | struct btrfs_writepage_fixup *fixup; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2157 | |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 2158 | /* this page is properly in the ordered list */ |
| 2159 | if (TestClearPagePrivate2(page)) |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2160 | return 0; |
| 2161 | |
| 2162 | if (PageChecked(page)) |
| 2163 | return -EAGAIN; |
| 2164 | |
| 2165 | fixup = kzalloc(sizeof(*fixup), GFP_NOFS); |
| 2166 | if (!fixup) |
| 2167 | return -EAGAIN; |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 2168 | |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2169 | SetPageChecked(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2170 | get_page(page); |
Liu Bo | 9e0af23 | 2014-08-15 23:36:53 +0800 | [diff] [blame] | 2171 | btrfs_init_work(&fixup->work, btrfs_fixup_helper, |
| 2172 | btrfs_writepage_fixup_worker, NULL, NULL); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2173 | fixup->page = page; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2174 | btrfs_queue_work(fs_info->fixup_workers, &fixup->work); |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 2175 | return -EBUSY; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 2176 | } |
| 2177 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2178 | static int insert_reserved_file_extent(struct btrfs_trans_handle *trans, |
| 2179 | struct inode *inode, u64 file_pos, |
| 2180 | u64 disk_bytenr, u64 disk_num_bytes, |
| 2181 | u64 num_bytes, u64 ram_bytes, |
| 2182 | u8 compression, u8 encryption, |
| 2183 | u16 other_encoding, int extent_type) |
| 2184 | { |
| 2185 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 2186 | struct btrfs_file_extent_item *fi; |
| 2187 | struct btrfs_path *path; |
| 2188 | struct extent_buffer *leaf; |
| 2189 | struct btrfs_key ins; |
Qu Wenruo | a12b877 | 2017-02-27 15:10:37 +0800 | [diff] [blame] | 2190 | u64 qg_released; |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 2191 | int extent_inserted = 0; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2192 | int ret; |
| 2193 | |
| 2194 | path = btrfs_alloc_path(); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 2195 | if (!path) |
| 2196 | return -ENOMEM; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2197 | |
Chris Mason | a1ed835 | 2009-09-11 12:27:37 -0400 | [diff] [blame] | 2198 | /* |
| 2199 | * we may be replacing one extent in the tree with another. |
| 2200 | * The new extent is pinned in the extent map, and we don't want |
| 2201 | * to drop it from the cache until it is completely in the btree. |
| 2202 | * |
| 2203 | * So, tell btrfs_drop_extents to leave this extent in the cache. |
| 2204 | * the caller is expected to unpin it and allow it to be merged |
| 2205 | * with the others. |
| 2206 | */ |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 2207 | ret = __btrfs_drop_extents(trans, root, inode, path, file_pos, |
| 2208 | file_pos + num_bytes, NULL, 0, |
| 2209 | 1, sizeof(*fi), &extent_inserted); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2210 | if (ret) |
| 2211 | goto out; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2212 | |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 2213 | if (!extent_inserted) { |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 2214 | ins.objectid = btrfs_ino(BTRFS_I(inode)); |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 2215 | ins.offset = file_pos; |
| 2216 | ins.type = BTRFS_EXTENT_DATA_KEY; |
| 2217 | |
| 2218 | path->leave_spinning = 1; |
| 2219 | ret = btrfs_insert_empty_item(trans, root, path, &ins, |
| 2220 | sizeof(*fi)); |
| 2221 | if (ret) |
| 2222 | goto out; |
| 2223 | } |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2224 | leaf = path->nodes[0]; |
| 2225 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 2226 | struct btrfs_file_extent_item); |
| 2227 | btrfs_set_file_extent_generation(leaf, fi, trans->transid); |
| 2228 | btrfs_set_file_extent_type(leaf, fi, extent_type); |
| 2229 | btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr); |
| 2230 | btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes); |
| 2231 | btrfs_set_file_extent_offset(leaf, fi, 0); |
| 2232 | btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes); |
| 2233 | btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes); |
| 2234 | btrfs_set_file_extent_compression(leaf, fi, compression); |
| 2235 | btrfs_set_file_extent_encryption(leaf, fi, encryption); |
| 2236 | btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 2237 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2238 | btrfs_mark_buffer_dirty(leaf); |
Josef Bacik | ce19533 | 2012-09-25 15:26:16 -0400 | [diff] [blame] | 2239 | btrfs_release_path(path); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2240 | |
| 2241 | inode_add_bytes(inode, num_bytes); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2242 | |
| 2243 | ins.objectid = disk_bytenr; |
| 2244 | ins.offset = disk_num_bytes; |
| 2245 | ins.type = BTRFS_EXTENT_ITEM_KEY; |
Qu Wenruo | a12b877 | 2017-02-27 15:10:37 +0800 | [diff] [blame] | 2246 | |
Qu Wenruo | 297d750 | 2015-09-08 17:08:37 +0800 | [diff] [blame] | 2247 | /* |
Qu Wenruo | 5846a3c | 2015-10-26 14:11:18 +0800 | [diff] [blame] | 2248 | * Release the reserved range from inode dirty range map, as it is |
| 2249 | * already moved into delayed_ref_head |
Qu Wenruo | 297d750 | 2015-09-08 17:08:37 +0800 | [diff] [blame] | 2250 | */ |
Qu Wenruo | a12b877 | 2017-02-27 15:10:37 +0800 | [diff] [blame] | 2251 | ret = btrfs_qgroup_release_data(inode, file_pos, ram_bytes); |
| 2252 | if (ret < 0) |
| 2253 | goto out; |
| 2254 | qg_released = ret; |
Josef Bacik | 84f7d8e | 2017-09-29 15:43:49 -0400 | [diff] [blame] | 2255 | ret = btrfs_alloc_reserved_file_extent(trans, root, |
| 2256 | btrfs_ino(BTRFS_I(inode)), |
| 2257 | file_pos, qg_released, &ins); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2258 | out: |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2259 | btrfs_free_path(path); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 2260 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2261 | return ret; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2262 | } |
| 2263 | |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2264 | /* snapshot-aware defrag */ |
| 2265 | struct sa_defrag_extent_backref { |
| 2266 | struct rb_node node; |
| 2267 | struct old_sa_defrag_extent *old; |
| 2268 | u64 root_id; |
| 2269 | u64 inum; |
| 2270 | u64 file_pos; |
| 2271 | u64 extent_offset; |
| 2272 | u64 num_bytes; |
| 2273 | u64 generation; |
| 2274 | }; |
| 2275 | |
| 2276 | struct old_sa_defrag_extent { |
| 2277 | struct list_head list; |
| 2278 | struct new_sa_defrag_extent *new; |
| 2279 | |
| 2280 | u64 extent_offset; |
| 2281 | u64 bytenr; |
| 2282 | u64 offset; |
| 2283 | u64 len; |
| 2284 | int count; |
| 2285 | }; |
| 2286 | |
| 2287 | struct new_sa_defrag_extent { |
| 2288 | struct rb_root root; |
| 2289 | struct list_head head; |
| 2290 | struct btrfs_path *path; |
| 2291 | struct inode *inode; |
| 2292 | u64 file_pos; |
| 2293 | u64 len; |
| 2294 | u64 bytenr; |
| 2295 | u64 disk_len; |
| 2296 | u8 compress_type; |
| 2297 | }; |
| 2298 | |
| 2299 | static int backref_comp(struct sa_defrag_extent_backref *b1, |
| 2300 | struct sa_defrag_extent_backref *b2) |
| 2301 | { |
| 2302 | if (b1->root_id < b2->root_id) |
| 2303 | return -1; |
| 2304 | else if (b1->root_id > b2->root_id) |
| 2305 | return 1; |
| 2306 | |
| 2307 | if (b1->inum < b2->inum) |
| 2308 | return -1; |
| 2309 | else if (b1->inum > b2->inum) |
| 2310 | return 1; |
| 2311 | |
| 2312 | if (b1->file_pos < b2->file_pos) |
| 2313 | return -1; |
| 2314 | else if (b1->file_pos > b2->file_pos) |
| 2315 | return 1; |
| 2316 | |
| 2317 | /* |
| 2318 | * [------------------------------] ===> (a range of space) |
| 2319 | * |<--->| |<---->| =============> (fs/file tree A) |
| 2320 | * |<---------------------------->| ===> (fs/file tree B) |
| 2321 | * |
| 2322 | * A range of space can refer to two file extents in one tree while |
| 2323 | * refer to only one file extent in another tree. |
| 2324 | * |
| 2325 | * So we may process a disk offset more than one time(two extents in A) |
| 2326 | * and locate at the same extent(one extent in B), then insert two same |
| 2327 | * backrefs(both refer to the extent in B). |
| 2328 | */ |
| 2329 | return 0; |
| 2330 | } |
| 2331 | |
| 2332 | static void backref_insert(struct rb_root *root, |
| 2333 | struct sa_defrag_extent_backref *backref) |
| 2334 | { |
| 2335 | struct rb_node **p = &root->rb_node; |
| 2336 | struct rb_node *parent = NULL; |
| 2337 | struct sa_defrag_extent_backref *entry; |
| 2338 | int ret; |
| 2339 | |
| 2340 | while (*p) { |
| 2341 | parent = *p; |
| 2342 | entry = rb_entry(parent, struct sa_defrag_extent_backref, node); |
| 2343 | |
| 2344 | ret = backref_comp(backref, entry); |
| 2345 | if (ret < 0) |
| 2346 | p = &(*p)->rb_left; |
| 2347 | else |
| 2348 | p = &(*p)->rb_right; |
| 2349 | } |
| 2350 | |
| 2351 | rb_link_node(&backref->node, parent, p); |
| 2352 | rb_insert_color(&backref->node, root); |
| 2353 | } |
| 2354 | |
| 2355 | /* |
| 2356 | * Note the backref might has changed, and in this case we just return 0. |
| 2357 | */ |
| 2358 | static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id, |
| 2359 | void *ctx) |
| 2360 | { |
| 2361 | struct btrfs_file_extent_item *extent; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2362 | struct old_sa_defrag_extent *old = ctx; |
| 2363 | struct new_sa_defrag_extent *new = old->new; |
| 2364 | struct btrfs_path *path = new->path; |
| 2365 | struct btrfs_key key; |
| 2366 | struct btrfs_root *root; |
| 2367 | struct sa_defrag_extent_backref *backref; |
| 2368 | struct extent_buffer *leaf; |
| 2369 | struct inode *inode = new->inode; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2370 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2371 | int slot; |
| 2372 | int ret; |
| 2373 | u64 extent_offset; |
| 2374 | u64 num_bytes; |
| 2375 | |
| 2376 | if (BTRFS_I(inode)->root->root_key.objectid == root_id && |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 2377 | inum == btrfs_ino(BTRFS_I(inode))) |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2378 | return 0; |
| 2379 | |
| 2380 | key.objectid = root_id; |
| 2381 | key.type = BTRFS_ROOT_ITEM_KEY; |
| 2382 | key.offset = (u64)-1; |
| 2383 | |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2384 | root = btrfs_read_fs_root_no_name(fs_info, &key); |
| 2385 | if (IS_ERR(root)) { |
| 2386 | if (PTR_ERR(root) == -ENOENT) |
| 2387 | return 0; |
| 2388 | WARN_ON(1); |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 2389 | btrfs_debug(fs_info, "inum=%llu, offset=%llu, root_id=%llu", |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2390 | inum, offset, root_id); |
| 2391 | return PTR_ERR(root); |
| 2392 | } |
| 2393 | |
| 2394 | key.objectid = inum; |
| 2395 | key.type = BTRFS_EXTENT_DATA_KEY; |
| 2396 | if (offset > (u64)-1 << 32) |
| 2397 | key.offset = 0; |
| 2398 | else |
| 2399 | key.offset = offset; |
| 2400 | |
| 2401 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
Dulshani Gunawardhana | fae7f21 | 2013-10-31 10:30:08 +0530 | [diff] [blame] | 2402 | if (WARN_ON(ret < 0)) |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2403 | return ret; |
Josef Bacik | 50f1319 | 2013-07-22 12:50:37 -0400 | [diff] [blame] | 2404 | ret = 0; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2405 | |
| 2406 | while (1) { |
| 2407 | cond_resched(); |
| 2408 | |
| 2409 | leaf = path->nodes[0]; |
| 2410 | slot = path->slots[0]; |
| 2411 | |
| 2412 | if (slot >= btrfs_header_nritems(leaf)) { |
| 2413 | ret = btrfs_next_leaf(root, path); |
| 2414 | if (ret < 0) { |
| 2415 | goto out; |
| 2416 | } else if (ret > 0) { |
| 2417 | ret = 0; |
| 2418 | goto out; |
| 2419 | } |
| 2420 | continue; |
| 2421 | } |
| 2422 | |
| 2423 | path->slots[0]++; |
| 2424 | |
| 2425 | btrfs_item_key_to_cpu(leaf, &key, slot); |
| 2426 | |
| 2427 | if (key.objectid > inum) |
| 2428 | goto out; |
| 2429 | |
| 2430 | if (key.objectid < inum || key.type != BTRFS_EXTENT_DATA_KEY) |
| 2431 | continue; |
| 2432 | |
| 2433 | extent = btrfs_item_ptr(leaf, slot, |
| 2434 | struct btrfs_file_extent_item); |
| 2435 | |
| 2436 | if (btrfs_file_extent_disk_bytenr(leaf, extent) != old->bytenr) |
| 2437 | continue; |
| 2438 | |
Liu Bo | e68afa4 | 2013-07-01 22:13:26 +0800 | [diff] [blame] | 2439 | /* |
| 2440 | * 'offset' refers to the exact key.offset, |
| 2441 | * NOT the 'offset' field in btrfs_extent_data_ref, ie. |
| 2442 | * (key.offset - extent_offset). |
| 2443 | */ |
| 2444 | if (key.offset != offset) |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2445 | continue; |
| 2446 | |
Liu Bo | e68afa4 | 2013-07-01 22:13:26 +0800 | [diff] [blame] | 2447 | extent_offset = btrfs_file_extent_offset(leaf, extent); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2448 | num_bytes = btrfs_file_extent_num_bytes(leaf, extent); |
Liu Bo | e68afa4 | 2013-07-01 22:13:26 +0800 | [diff] [blame] | 2449 | |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2450 | if (extent_offset >= old->extent_offset + old->offset + |
| 2451 | old->len || extent_offset + num_bytes <= |
| 2452 | old->extent_offset + old->offset) |
| 2453 | continue; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2454 | break; |
| 2455 | } |
| 2456 | |
| 2457 | backref = kmalloc(sizeof(*backref), GFP_NOFS); |
| 2458 | if (!backref) { |
| 2459 | ret = -ENOENT; |
| 2460 | goto out; |
| 2461 | } |
| 2462 | |
| 2463 | backref->root_id = root_id; |
| 2464 | backref->inum = inum; |
Liu Bo | e68afa4 | 2013-07-01 22:13:26 +0800 | [diff] [blame] | 2465 | backref->file_pos = offset; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2466 | backref->num_bytes = num_bytes; |
| 2467 | backref->extent_offset = extent_offset; |
| 2468 | backref->generation = btrfs_file_extent_generation(leaf, extent); |
| 2469 | backref->old = old; |
| 2470 | backref_insert(&new->root, backref); |
| 2471 | old->count++; |
| 2472 | out: |
| 2473 | btrfs_release_path(path); |
| 2474 | WARN_ON(ret); |
| 2475 | return ret; |
| 2476 | } |
| 2477 | |
| 2478 | static noinline bool record_extent_backrefs(struct btrfs_path *path, |
| 2479 | struct new_sa_defrag_extent *new) |
| 2480 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2481 | struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2482 | struct old_sa_defrag_extent *old, *tmp; |
| 2483 | int ret; |
| 2484 | |
| 2485 | new->path = path; |
| 2486 | |
| 2487 | list_for_each_entry_safe(old, tmp, &new->head, list) { |
Liu Bo | e68afa4 | 2013-07-01 22:13:26 +0800 | [diff] [blame] | 2488 | ret = iterate_inodes_from_logical(old->bytenr + |
| 2489 | old->extent_offset, fs_info, |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2490 | path, record_one_backref, |
Zygo Blaxell | c995ab3 | 2017-09-22 13:58:45 -0400 | [diff] [blame] | 2491 | old, false); |
Josef Bacik | 4724b10 | 2013-11-05 11:11:40 -0500 | [diff] [blame] | 2492 | if (ret < 0 && ret != -ENOENT) |
| 2493 | return false; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2494 | |
| 2495 | /* no backref to be processed for this extent */ |
| 2496 | if (!old->count) { |
| 2497 | list_del(&old->list); |
| 2498 | kfree(old); |
| 2499 | } |
| 2500 | } |
| 2501 | |
| 2502 | if (list_empty(&new->head)) |
| 2503 | return false; |
| 2504 | |
| 2505 | return true; |
| 2506 | } |
| 2507 | |
| 2508 | static int relink_is_mergable(struct extent_buffer *leaf, |
| 2509 | struct btrfs_file_extent_item *fi, |
Liu Bo | 116e002 | 2013-08-02 16:30:40 +0800 | [diff] [blame] | 2510 | struct new_sa_defrag_extent *new) |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2511 | { |
Liu Bo | 116e002 | 2013-08-02 16:30:40 +0800 | [diff] [blame] | 2512 | if (btrfs_file_extent_disk_bytenr(leaf, fi) != new->bytenr) |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2513 | return 0; |
| 2514 | |
| 2515 | if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG) |
| 2516 | return 0; |
| 2517 | |
Liu Bo | 116e002 | 2013-08-02 16:30:40 +0800 | [diff] [blame] | 2518 | if (btrfs_file_extent_compression(leaf, fi) != new->compress_type) |
| 2519 | return 0; |
| 2520 | |
| 2521 | if (btrfs_file_extent_encryption(leaf, fi) || |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2522 | btrfs_file_extent_other_encoding(leaf, fi)) |
| 2523 | return 0; |
| 2524 | |
| 2525 | return 1; |
| 2526 | } |
| 2527 | |
| 2528 | /* |
| 2529 | * Note the backref might has changed, and in this case we just return 0. |
| 2530 | */ |
| 2531 | static noinline int relink_extent_backref(struct btrfs_path *path, |
| 2532 | struct sa_defrag_extent_backref *prev, |
| 2533 | struct sa_defrag_extent_backref *backref) |
| 2534 | { |
| 2535 | struct btrfs_file_extent_item *extent; |
| 2536 | struct btrfs_file_extent_item *item; |
| 2537 | struct btrfs_ordered_extent *ordered; |
| 2538 | struct btrfs_trans_handle *trans; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2539 | struct btrfs_root *root; |
| 2540 | struct btrfs_key key; |
| 2541 | struct extent_buffer *leaf; |
| 2542 | struct old_sa_defrag_extent *old = backref->old; |
| 2543 | struct new_sa_defrag_extent *new = old->new; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2544 | struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2545 | struct inode *inode; |
| 2546 | struct extent_state *cached = NULL; |
| 2547 | int ret = 0; |
| 2548 | u64 start; |
| 2549 | u64 len; |
| 2550 | u64 lock_start; |
| 2551 | u64 lock_end; |
| 2552 | bool merge = false; |
| 2553 | int index; |
| 2554 | |
| 2555 | if (prev && prev->root_id == backref->root_id && |
| 2556 | prev->inum == backref->inum && |
| 2557 | prev->file_pos + prev->num_bytes == backref->file_pos) |
| 2558 | merge = true; |
| 2559 | |
| 2560 | /* step 1: get root */ |
| 2561 | key.objectid = backref->root_id; |
| 2562 | key.type = BTRFS_ROOT_ITEM_KEY; |
| 2563 | key.offset = (u64)-1; |
| 2564 | |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2565 | index = srcu_read_lock(&fs_info->subvol_srcu); |
| 2566 | |
| 2567 | root = btrfs_read_fs_root_no_name(fs_info, &key); |
| 2568 | if (IS_ERR(root)) { |
| 2569 | srcu_read_unlock(&fs_info->subvol_srcu, index); |
| 2570 | if (PTR_ERR(root) == -ENOENT) |
| 2571 | return 0; |
| 2572 | return PTR_ERR(root); |
| 2573 | } |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2574 | |
Wang Shilong | bcbba5e | 2014-02-08 23:46:35 +0800 | [diff] [blame] | 2575 | if (btrfs_root_readonly(root)) { |
| 2576 | srcu_read_unlock(&fs_info->subvol_srcu, index); |
| 2577 | return 0; |
| 2578 | } |
| 2579 | |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2580 | /* step 2: get inode */ |
| 2581 | key.objectid = backref->inum; |
| 2582 | key.type = BTRFS_INODE_ITEM_KEY; |
| 2583 | key.offset = 0; |
| 2584 | |
| 2585 | inode = btrfs_iget(fs_info->sb, &key, root, NULL); |
| 2586 | if (IS_ERR(inode)) { |
| 2587 | srcu_read_unlock(&fs_info->subvol_srcu, index); |
| 2588 | return 0; |
| 2589 | } |
| 2590 | |
| 2591 | srcu_read_unlock(&fs_info->subvol_srcu, index); |
| 2592 | |
| 2593 | /* step 3: relink backref */ |
| 2594 | lock_start = backref->file_pos; |
| 2595 | lock_end = backref->file_pos + backref->num_bytes - 1; |
| 2596 | lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end, |
David Sterba | ff13db4 | 2015-12-03 14:30:40 +0100 | [diff] [blame] | 2597 | &cached); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2598 | |
| 2599 | ordered = btrfs_lookup_first_ordered_extent(inode, lock_end); |
| 2600 | if (ordered) { |
| 2601 | btrfs_put_ordered_extent(ordered); |
| 2602 | goto out_unlock; |
| 2603 | } |
| 2604 | |
| 2605 | trans = btrfs_join_transaction(root); |
| 2606 | if (IS_ERR(trans)) { |
| 2607 | ret = PTR_ERR(trans); |
| 2608 | goto out_unlock; |
| 2609 | } |
| 2610 | |
| 2611 | key.objectid = backref->inum; |
| 2612 | key.type = BTRFS_EXTENT_DATA_KEY; |
| 2613 | key.offset = backref->file_pos; |
| 2614 | |
| 2615 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 2616 | if (ret < 0) { |
| 2617 | goto out_free_path; |
| 2618 | } else if (ret > 0) { |
| 2619 | ret = 0; |
| 2620 | goto out_free_path; |
| 2621 | } |
| 2622 | |
| 2623 | extent = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 2624 | struct btrfs_file_extent_item); |
| 2625 | |
| 2626 | if (btrfs_file_extent_generation(path->nodes[0], extent) != |
| 2627 | backref->generation) |
| 2628 | goto out_free_path; |
| 2629 | |
| 2630 | btrfs_release_path(path); |
| 2631 | |
| 2632 | start = backref->file_pos; |
| 2633 | if (backref->extent_offset < old->extent_offset + old->offset) |
| 2634 | start += old->extent_offset + old->offset - |
| 2635 | backref->extent_offset; |
| 2636 | |
| 2637 | len = min(backref->extent_offset + backref->num_bytes, |
| 2638 | old->extent_offset + old->offset + old->len); |
| 2639 | len -= max(backref->extent_offset, old->extent_offset + old->offset); |
| 2640 | |
| 2641 | ret = btrfs_drop_extents(trans, root, inode, start, |
| 2642 | start + len, 1); |
| 2643 | if (ret) |
| 2644 | goto out_free_path; |
| 2645 | again: |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 2646 | key.objectid = btrfs_ino(BTRFS_I(inode)); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2647 | key.type = BTRFS_EXTENT_DATA_KEY; |
| 2648 | key.offset = start; |
| 2649 | |
Liu Bo | a09a0a7 | 2013-03-11 09:20:58 +0000 | [diff] [blame] | 2650 | path->leave_spinning = 1; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2651 | if (merge) { |
| 2652 | struct btrfs_file_extent_item *fi; |
| 2653 | u64 extent_len; |
| 2654 | struct btrfs_key found_key; |
| 2655 | |
Gui Hecheng | 3c9665d | 2014-01-23 13:41:09 +0800 | [diff] [blame] | 2656 | ret = btrfs_search_slot(trans, root, &key, path, 0, 1); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2657 | if (ret < 0) |
| 2658 | goto out_free_path; |
| 2659 | |
| 2660 | path->slots[0]--; |
| 2661 | leaf = path->nodes[0]; |
| 2662 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 2663 | |
| 2664 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 2665 | struct btrfs_file_extent_item); |
| 2666 | extent_len = btrfs_file_extent_num_bytes(leaf, fi); |
| 2667 | |
Liu Bo | 116e002 | 2013-08-02 16:30:40 +0800 | [diff] [blame] | 2668 | if (extent_len + found_key.offset == start && |
| 2669 | relink_is_mergable(leaf, fi, new)) { |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2670 | btrfs_set_file_extent_num_bytes(leaf, fi, |
| 2671 | extent_len + len); |
| 2672 | btrfs_mark_buffer_dirty(leaf); |
| 2673 | inode_add_bytes(inode, len); |
| 2674 | |
| 2675 | ret = 1; |
| 2676 | goto out_free_path; |
| 2677 | } else { |
| 2678 | merge = false; |
| 2679 | btrfs_release_path(path); |
| 2680 | goto again; |
| 2681 | } |
| 2682 | } |
| 2683 | |
| 2684 | ret = btrfs_insert_empty_item(trans, root, path, &key, |
| 2685 | sizeof(*extent)); |
| 2686 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 2687 | btrfs_abort_transaction(trans, ret); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2688 | goto out_free_path; |
| 2689 | } |
| 2690 | |
| 2691 | leaf = path->nodes[0]; |
| 2692 | item = btrfs_item_ptr(leaf, path->slots[0], |
| 2693 | struct btrfs_file_extent_item); |
| 2694 | btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr); |
| 2695 | btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len); |
| 2696 | btrfs_set_file_extent_offset(leaf, item, start - new->file_pos); |
| 2697 | btrfs_set_file_extent_num_bytes(leaf, item, len); |
| 2698 | btrfs_set_file_extent_ram_bytes(leaf, item, new->len); |
| 2699 | btrfs_set_file_extent_generation(leaf, item, trans->transid); |
| 2700 | btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG); |
| 2701 | btrfs_set_file_extent_compression(leaf, item, new->compress_type); |
| 2702 | btrfs_set_file_extent_encryption(leaf, item, 0); |
| 2703 | btrfs_set_file_extent_other_encoding(leaf, item, 0); |
| 2704 | |
| 2705 | btrfs_mark_buffer_dirty(leaf); |
| 2706 | inode_add_bytes(inode, len); |
Liu Bo | a09a0a7 | 2013-03-11 09:20:58 +0000 | [diff] [blame] | 2707 | btrfs_release_path(path); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2708 | |
Josef Bacik | 84f7d8e | 2017-09-29 15:43:49 -0400 | [diff] [blame] | 2709 | ret = btrfs_inc_extent_ref(trans, root, new->bytenr, |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2710 | new->disk_len, 0, |
| 2711 | backref->root_id, backref->inum, |
Filipe Manana | b06c4bf | 2015-10-23 07:52:54 +0100 | [diff] [blame] | 2712 | new->file_pos); /* start - extent_offset */ |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2713 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 2714 | btrfs_abort_transaction(trans, ret); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2715 | goto out_free_path; |
| 2716 | } |
| 2717 | |
| 2718 | ret = 1; |
| 2719 | out_free_path: |
| 2720 | btrfs_release_path(path); |
Liu Bo | a09a0a7 | 2013-03-11 09:20:58 +0000 | [diff] [blame] | 2721 | path->leave_spinning = 0; |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 2722 | btrfs_end_transaction(trans); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2723 | out_unlock: |
| 2724 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end, |
| 2725 | &cached, GFP_NOFS); |
| 2726 | iput(inode); |
| 2727 | return ret; |
| 2728 | } |
| 2729 | |
Liu Bo | 6f51956 | 2013-10-29 10:45:05 +0800 | [diff] [blame] | 2730 | static void free_sa_defrag_extent(struct new_sa_defrag_extent *new) |
| 2731 | { |
| 2732 | struct old_sa_defrag_extent *old, *tmp; |
| 2733 | |
| 2734 | if (!new) |
| 2735 | return; |
| 2736 | |
| 2737 | list_for_each_entry_safe(old, tmp, &new->head, list) { |
Liu Bo | 6f51956 | 2013-10-29 10:45:05 +0800 | [diff] [blame] | 2738 | kfree(old); |
| 2739 | } |
| 2740 | kfree(new); |
| 2741 | } |
| 2742 | |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2743 | static void relink_file_extents(struct new_sa_defrag_extent *new) |
| 2744 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2745 | struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2746 | struct btrfs_path *path; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2747 | struct sa_defrag_extent_backref *backref; |
| 2748 | struct sa_defrag_extent_backref *prev = NULL; |
| 2749 | struct inode *inode; |
| 2750 | struct btrfs_root *root; |
| 2751 | struct rb_node *node; |
| 2752 | int ret; |
| 2753 | |
| 2754 | inode = new->inode; |
| 2755 | root = BTRFS_I(inode)->root; |
| 2756 | |
| 2757 | path = btrfs_alloc_path(); |
| 2758 | if (!path) |
| 2759 | return; |
| 2760 | |
| 2761 | if (!record_extent_backrefs(path, new)) { |
| 2762 | btrfs_free_path(path); |
| 2763 | goto out; |
| 2764 | } |
| 2765 | btrfs_release_path(path); |
| 2766 | |
| 2767 | while (1) { |
| 2768 | node = rb_first(&new->root); |
| 2769 | if (!node) |
| 2770 | break; |
| 2771 | rb_erase(node, &new->root); |
| 2772 | |
| 2773 | backref = rb_entry(node, struct sa_defrag_extent_backref, node); |
| 2774 | |
| 2775 | ret = relink_extent_backref(path, prev, backref); |
| 2776 | WARN_ON(ret < 0); |
| 2777 | |
| 2778 | kfree(prev); |
| 2779 | |
| 2780 | if (ret == 1) |
| 2781 | prev = backref; |
| 2782 | else |
| 2783 | prev = NULL; |
| 2784 | cond_resched(); |
| 2785 | } |
| 2786 | kfree(prev); |
| 2787 | |
| 2788 | btrfs_free_path(path); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2789 | out: |
Liu Bo | 6f51956 | 2013-10-29 10:45:05 +0800 | [diff] [blame] | 2790 | free_sa_defrag_extent(new); |
| 2791 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2792 | atomic_dec(&fs_info->defrag_running); |
| 2793 | wake_up(&fs_info->transaction_wait); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2794 | } |
| 2795 | |
| 2796 | static struct new_sa_defrag_extent * |
| 2797 | record_old_file_extents(struct inode *inode, |
| 2798 | struct btrfs_ordered_extent *ordered) |
| 2799 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2800 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2801 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 2802 | struct btrfs_path *path; |
| 2803 | struct btrfs_key key; |
Liu Bo | 6f51956 | 2013-10-29 10:45:05 +0800 | [diff] [blame] | 2804 | struct old_sa_defrag_extent *old; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2805 | struct new_sa_defrag_extent *new; |
| 2806 | int ret; |
| 2807 | |
| 2808 | new = kmalloc(sizeof(*new), GFP_NOFS); |
| 2809 | if (!new) |
| 2810 | return NULL; |
| 2811 | |
| 2812 | new->inode = inode; |
| 2813 | new->file_pos = ordered->file_offset; |
| 2814 | new->len = ordered->len; |
| 2815 | new->bytenr = ordered->start; |
| 2816 | new->disk_len = ordered->disk_len; |
| 2817 | new->compress_type = ordered->compress_type; |
| 2818 | new->root = RB_ROOT; |
| 2819 | INIT_LIST_HEAD(&new->head); |
| 2820 | |
| 2821 | path = btrfs_alloc_path(); |
| 2822 | if (!path) |
| 2823 | goto out_kfree; |
| 2824 | |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 2825 | key.objectid = btrfs_ino(BTRFS_I(inode)); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2826 | key.type = BTRFS_EXTENT_DATA_KEY; |
| 2827 | key.offset = new->file_pos; |
| 2828 | |
| 2829 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 2830 | if (ret < 0) |
| 2831 | goto out_free_path; |
| 2832 | if (ret > 0 && path->slots[0] > 0) |
| 2833 | path->slots[0]--; |
| 2834 | |
| 2835 | /* find out all the old extents for the file range */ |
| 2836 | while (1) { |
| 2837 | struct btrfs_file_extent_item *extent; |
| 2838 | struct extent_buffer *l; |
| 2839 | int slot; |
| 2840 | u64 num_bytes; |
| 2841 | u64 offset; |
| 2842 | u64 end; |
| 2843 | u64 disk_bytenr; |
| 2844 | u64 extent_offset; |
| 2845 | |
| 2846 | l = path->nodes[0]; |
| 2847 | slot = path->slots[0]; |
| 2848 | |
| 2849 | if (slot >= btrfs_header_nritems(l)) { |
| 2850 | ret = btrfs_next_leaf(root, path); |
| 2851 | if (ret < 0) |
Liu Bo | 6f51956 | 2013-10-29 10:45:05 +0800 | [diff] [blame] | 2852 | goto out_free_path; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2853 | else if (ret > 0) |
| 2854 | break; |
| 2855 | continue; |
| 2856 | } |
| 2857 | |
| 2858 | btrfs_item_key_to_cpu(l, &key, slot); |
| 2859 | |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 2860 | if (key.objectid != btrfs_ino(BTRFS_I(inode))) |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2861 | break; |
| 2862 | if (key.type != BTRFS_EXTENT_DATA_KEY) |
| 2863 | break; |
| 2864 | if (key.offset >= new->file_pos + new->len) |
| 2865 | break; |
| 2866 | |
| 2867 | extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item); |
| 2868 | |
| 2869 | num_bytes = btrfs_file_extent_num_bytes(l, extent); |
| 2870 | if (key.offset + num_bytes < new->file_pos) |
| 2871 | goto next; |
| 2872 | |
| 2873 | disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent); |
| 2874 | if (!disk_bytenr) |
| 2875 | goto next; |
| 2876 | |
| 2877 | extent_offset = btrfs_file_extent_offset(l, extent); |
| 2878 | |
| 2879 | old = kmalloc(sizeof(*old), GFP_NOFS); |
| 2880 | if (!old) |
Liu Bo | 6f51956 | 2013-10-29 10:45:05 +0800 | [diff] [blame] | 2881 | goto out_free_path; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2882 | |
| 2883 | offset = max(new->file_pos, key.offset); |
| 2884 | end = min(new->file_pos + new->len, key.offset + num_bytes); |
| 2885 | |
| 2886 | old->bytenr = disk_bytenr; |
| 2887 | old->extent_offset = extent_offset; |
| 2888 | old->offset = offset - key.offset; |
| 2889 | old->len = end - offset; |
| 2890 | old->new = new; |
| 2891 | old->count = 0; |
| 2892 | list_add_tail(&old->list, &new->head); |
| 2893 | next: |
| 2894 | path->slots[0]++; |
| 2895 | cond_resched(); |
| 2896 | } |
| 2897 | |
| 2898 | btrfs_free_path(path); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2899 | atomic_inc(&fs_info->defrag_running); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2900 | |
| 2901 | return new; |
| 2902 | |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2903 | out_free_path: |
| 2904 | btrfs_free_path(path); |
| 2905 | out_kfree: |
Liu Bo | 6f51956 | 2013-10-29 10:45:05 +0800 | [diff] [blame] | 2906 | free_sa_defrag_extent(new); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2907 | return NULL; |
| 2908 | } |
| 2909 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2910 | static void btrfs_release_delalloc_bytes(struct btrfs_fs_info *fs_info, |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 2911 | u64 start, u64 len) |
| 2912 | { |
| 2913 | struct btrfs_block_group_cache *cache; |
| 2914 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2915 | cache = btrfs_lookup_block_group(fs_info, start); |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 2916 | ASSERT(cache); |
| 2917 | |
| 2918 | spin_lock(&cache->lock); |
| 2919 | cache->delalloc_bytes -= len; |
| 2920 | spin_unlock(&cache->lock); |
| 2921 | |
| 2922 | btrfs_put_block_group(cache); |
| 2923 | } |
| 2924 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2925 | /* as ordered data IO finishes, this gets called so we can finish |
| 2926 | * an ordered extent if the range of bytes in the file it covers are |
| 2927 | * fully written. |
| 2928 | */ |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2929 | static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2930 | { |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2931 | struct inode *inode = ordered_extent->inode; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2932 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2933 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 2934 | struct btrfs_trans_handle *trans = NULL; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2935 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 2936 | struct extent_state *cached_state = NULL; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2937 | struct new_sa_defrag_extent *new = NULL; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 2938 | int compress_type = 0; |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 2939 | int ret = 0; |
| 2940 | u64 logical_len = ordered_extent->len; |
Li Zefan | 82d5902 | 2011-04-20 10:33:24 +0800 | [diff] [blame] | 2941 | bool nolock; |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 2942 | bool truncated = false; |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 2943 | bool range_locked = false; |
| 2944 | bool clear_new_delalloc_bytes = false; |
| 2945 | |
| 2946 | if (!test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) && |
| 2947 | !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags) && |
| 2948 | !test_bit(BTRFS_ORDERED_DIRECT, &ordered_extent->flags)) |
| 2949 | clear_new_delalloc_bytes = true; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2950 | |
Nikolay Borisov | 70ddc55 | 2017-02-20 13:50:35 +0200 | [diff] [blame] | 2951 | nolock = btrfs_is_free_space_inode(BTRFS_I(inode)); |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 2952 | |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2953 | if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) { |
| 2954 | ret = -EIO; |
| 2955 | goto out; |
| 2956 | } |
| 2957 | |
Nikolay Borisov | 7ab7956 | 2017-02-20 13:50:57 +0200 | [diff] [blame] | 2958 | btrfs_free_io_failure_record(BTRFS_I(inode), |
| 2959 | ordered_extent->file_offset, |
| 2960 | ordered_extent->file_offset + |
| 2961 | ordered_extent->len - 1); |
Miao Xie | f612496 | 2014-09-12 18:44:04 +0800 | [diff] [blame] | 2962 | |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 2963 | if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) { |
| 2964 | truncated = true; |
| 2965 | logical_len = ordered_extent->truncated_len; |
| 2966 | /* Truncated the entire extent, don't bother adding */ |
| 2967 | if (!logical_len) |
| 2968 | goto out; |
| 2969 | } |
| 2970 | |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 2971 | if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) { |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2972 | BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */ |
Qu Wenruo | 94ed938 | 2015-09-08 17:25:56 +0800 | [diff] [blame] | 2973 | |
| 2974 | /* |
| 2975 | * For mwrite(mmap + memset to write) case, we still reserve |
| 2976 | * space for NOCOW range. |
| 2977 | * As NOCOW won't cause a new delayed ref, just free the space |
| 2978 | */ |
Qu Wenruo | bc42bda | 2017-02-27 15:10:39 +0800 | [diff] [blame] | 2979 | btrfs_qgroup_free_data(inode, NULL, ordered_extent->file_offset, |
Qu Wenruo | 94ed938 | 2015-09-08 17:25:56 +0800 | [diff] [blame] | 2980 | ordered_extent->len); |
Josef Bacik | 6c760c0 | 2012-11-09 10:53:21 -0500 | [diff] [blame] | 2981 | btrfs_ordered_update_i_size(inode, 0, ordered_extent); |
| 2982 | if (nolock) |
| 2983 | trans = btrfs_join_transaction_nolock(root); |
| 2984 | else |
| 2985 | trans = btrfs_join_transaction(root); |
| 2986 | if (IS_ERR(trans)) { |
| 2987 | ret = PTR_ERR(trans); |
| 2988 | trans = NULL; |
| 2989 | goto out; |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 2990 | } |
Josef Bacik | 69fe2d7 | 2017-10-19 14:15:57 -0400 | [diff] [blame] | 2991 | trans->block_rsv = &BTRFS_I(inode)->block_rsv; |
Josef Bacik | 6c760c0 | 2012-11-09 10:53:21 -0500 | [diff] [blame] | 2992 | ret = btrfs_update_inode_fallback(trans, root, inode); |
| 2993 | if (ret) /* -ENOMEM or corruption */ |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 2994 | btrfs_abort_transaction(trans, ret); |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 2995 | goto out; |
| 2996 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2997 | |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 2998 | range_locked = true; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 2999 | lock_extent_bits(io_tree, ordered_extent->file_offset, |
| 3000 | ordered_extent->file_offset + ordered_extent->len - 1, |
David Sterba | ff13db4 | 2015-12-03 14:30:40 +0100 | [diff] [blame] | 3001 | &cached_state); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3002 | |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 3003 | ret = test_range_bit(io_tree, ordered_extent->file_offset, |
| 3004 | ordered_extent->file_offset + ordered_extent->len - 1, |
Liu Bo | 452e62b | 2017-05-26 17:44:23 -0600 | [diff] [blame] | 3005 | EXTENT_DEFRAG, 0, cached_state); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 3006 | if (ret) { |
| 3007 | u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item); |
Josef Bacik | 8101c8d | 2014-01-29 16:05:30 -0500 | [diff] [blame] | 3008 | if (0 && last_snapshot >= BTRFS_I(inode)->generation) |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 3009 | /* the inode is shared */ |
| 3010 | new = record_old_file_extents(inode, ordered_extent); |
| 3011 | |
| 3012 | clear_extent_bit(io_tree, ordered_extent->file_offset, |
| 3013 | ordered_extent->file_offset + ordered_extent->len - 1, |
David Sterba | ae0f162 | 2017-10-31 16:37:52 +0100 | [diff] [blame] | 3014 | EXTENT_DEFRAG, 0, 0, &cached_state); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 3015 | } |
| 3016 | |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 3017 | if (nolock) |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 3018 | trans = btrfs_join_transaction_nolock(root); |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 3019 | else |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 3020 | trans = btrfs_join_transaction(root); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3021 | if (IS_ERR(trans)) { |
| 3022 | ret = PTR_ERR(trans); |
| 3023 | trans = NULL; |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 3024 | goto out; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3025 | } |
Chris Mason | a79b7d4 | 2014-05-22 16:18:52 -0700 | [diff] [blame] | 3026 | |
Josef Bacik | 69fe2d7 | 2017-10-19 14:15:57 -0400 | [diff] [blame] | 3027 | trans->block_rsv = &BTRFS_I(inode)->block_rsv; |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 3028 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 3029 | if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags)) |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 3030 | compress_type = ordered_extent->compress_type; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 3031 | if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) { |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 3032 | BUG_ON(compress_type); |
Justin Maggard | b430b77 | 2017-10-30 15:29:10 -0700 | [diff] [blame] | 3033 | btrfs_qgroup_free_data(inode, NULL, ordered_extent->file_offset, |
| 3034 | ordered_extent->len); |
Nikolay Borisov | 7a6d706 | 2017-02-20 13:50:48 +0200 | [diff] [blame] | 3035 | ret = btrfs_mark_extent_written(trans, BTRFS_I(inode), |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 3036 | ordered_extent->file_offset, |
| 3037 | ordered_extent->file_offset + |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 3038 | logical_len); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 3039 | } else { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3040 | BUG_ON(root == fs_info->tree_root); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 3041 | ret = insert_reserved_file_extent(trans, inode, |
| 3042 | ordered_extent->file_offset, |
| 3043 | ordered_extent->start, |
| 3044 | ordered_extent->disk_len, |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 3045 | logical_len, logical_len, |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 3046 | compress_type, 0, 0, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 3047 | BTRFS_FILE_EXTENT_REG); |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 3048 | if (!ret) |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3049 | btrfs_release_delalloc_bytes(fs_info, |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 3050 | ordered_extent->start, |
| 3051 | ordered_extent->disk_len); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 3052 | } |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 3053 | unpin_extent_cache(&BTRFS_I(inode)->extent_tree, |
| 3054 | ordered_extent->file_offset, ordered_extent->len, |
| 3055 | trans->transid); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3056 | if (ret < 0) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3057 | btrfs_abort_transaction(trans, ret); |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 3058 | goto out; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3059 | } |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 3060 | |
David Sterba | df9f628 | 2017-02-10 19:35:37 +0100 | [diff] [blame] | 3061 | add_pending_csums(trans, inode, &ordered_extent->list); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3062 | |
Josef Bacik | 6c760c0 | 2012-11-09 10:53:21 -0500 | [diff] [blame] | 3063 | btrfs_ordered_update_i_size(inode, 0, ordered_extent); |
| 3064 | ret = btrfs_update_inode_fallback(trans, root, inode); |
| 3065 | if (ret) { /* -ENOMEM or corruption */ |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3066 | btrfs_abort_transaction(trans, ret); |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 3067 | goto out; |
Josef Bacik | 1ef30be | 2011-04-05 19:25:36 -0400 | [diff] [blame] | 3068 | } |
| 3069 | ret = 0; |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 3070 | out: |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 3071 | if (range_locked || clear_new_delalloc_bytes) { |
| 3072 | unsigned int clear_bits = 0; |
| 3073 | |
| 3074 | if (range_locked) |
| 3075 | clear_bits |= EXTENT_LOCKED; |
| 3076 | if (clear_new_delalloc_bytes) |
| 3077 | clear_bits |= EXTENT_DELALLOC_NEW; |
| 3078 | clear_extent_bit(&BTRFS_I(inode)->io_tree, |
| 3079 | ordered_extent->file_offset, |
| 3080 | ordered_extent->file_offset + |
| 3081 | ordered_extent->len - 1, |
| 3082 | clear_bits, |
| 3083 | (clear_bits & EXTENT_LOCKED) ? 1 : 0, |
David Sterba | ae0f162 | 2017-10-31 16:37:52 +0100 | [diff] [blame] | 3084 | 0, &cached_state); |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 3085 | } |
| 3086 | |
Miao Xie | a698d075 | 2012-09-20 01:51:59 -0600 | [diff] [blame] | 3087 | if (trans) |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 3088 | btrfs_end_transaction(trans); |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 3089 | |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 3090 | if (ret || truncated) { |
| 3091 | u64 start, end; |
| 3092 | |
| 3093 | if (truncated) |
| 3094 | start = ordered_extent->file_offset + logical_len; |
| 3095 | else |
| 3096 | start = ordered_extent->file_offset; |
| 3097 | end = ordered_extent->file_offset + ordered_extent->len - 1; |
David Sterba | f08dc36 | 2017-10-31 17:02:39 +0100 | [diff] [blame] | 3098 | clear_extent_uptodate(io_tree, start, end, NULL); |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 3099 | |
| 3100 | /* 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] | 3101 | btrfs_drop_extent_cache(BTRFS_I(inode), start, end, 0); |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 3102 | |
Josef Bacik | 0bec9ef | 2013-01-31 14:58:00 -0500 | [diff] [blame] | 3103 | /* |
| 3104 | * If the ordered extent had an IOERR or something else went |
| 3105 | * wrong we need to return the space for this ordered extent |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 3106 | * back to the allocator. We only free the extent in the |
| 3107 | * truncated case if we didn't write out the extent at all. |
Josef Bacik | 0bec9ef | 2013-01-31 14:58:00 -0500 | [diff] [blame] | 3108 | */ |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 3109 | if ((ret || !logical_len) && |
| 3110 | !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) && |
Josef Bacik | 0bec9ef | 2013-01-31 14:58:00 -0500 | [diff] [blame] | 3111 | !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3112 | btrfs_free_reserved_extent(fs_info, |
| 3113 | ordered_extent->start, |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 3114 | ordered_extent->disk_len, 1); |
Josef Bacik | 0bec9ef | 2013-01-31 14:58:00 -0500 | [diff] [blame] | 3115 | } |
| 3116 | |
| 3117 | |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 3118 | /* |
Liu Bo | 8bad3c0 | 2012-06-18 12:14:23 +0800 | [diff] [blame] | 3119 | * This needs to be done to make sure anybody waiting knows we are done |
| 3120 | * updating everything for this ordered extent. |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 3121 | */ |
| 3122 | btrfs_remove_ordered_extent(inode, ordered_extent); |
| 3123 | |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 3124 | /* for snapshot-aware defrag */ |
Liu Bo | 6f51956 | 2013-10-29 10:45:05 +0800 | [diff] [blame] | 3125 | if (new) { |
| 3126 | if (ret) { |
| 3127 | free_sa_defrag_extent(new); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3128 | atomic_dec(&fs_info->defrag_running); |
Liu Bo | 6f51956 | 2013-10-29 10:45:05 +0800 | [diff] [blame] | 3129 | } else { |
| 3130 | relink_file_extents(new); |
| 3131 | } |
| 3132 | } |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 3133 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3134 | /* once for us */ |
| 3135 | btrfs_put_ordered_extent(ordered_extent); |
| 3136 | /* once for the tree */ |
| 3137 | btrfs_put_ordered_extent(ordered_extent); |
| 3138 | |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 3139 | return ret; |
| 3140 | } |
| 3141 | |
| 3142 | static void finish_ordered_fn(struct btrfs_work *work) |
| 3143 | { |
| 3144 | struct btrfs_ordered_extent *ordered_extent; |
| 3145 | ordered_extent = container_of(work, struct btrfs_ordered_extent, work); |
| 3146 | btrfs_finish_ordered_io(ordered_extent); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3147 | } |
| 3148 | |
David Sterba | c3988d6 | 2017-02-17 15:18:32 +0100 | [diff] [blame] | 3149 | 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] | 3150 | struct extent_state *state, int uptodate) |
| 3151 | { |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 3152 | struct inode *inode = page->mapping->host; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3153 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 3154 | struct btrfs_ordered_extent *ordered_extent = NULL; |
Liu Bo | 9e0af23 | 2014-08-15 23:36:53 +0800 | [diff] [blame] | 3155 | struct btrfs_workqueue *wq; |
| 3156 | btrfs_work_func_t func; |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 3157 | |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 3158 | trace_btrfs_writepage_end_io_hook(page, start, end, uptodate); |
| 3159 | |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 3160 | ClearPagePrivate2(page); |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 3161 | if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start, |
| 3162 | end - start + 1, uptodate)) |
David Sterba | c3988d6 | 2017-02-17 15:18:32 +0100 | [diff] [blame] | 3163 | return; |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 3164 | |
Nikolay Borisov | 70ddc55 | 2017-02-20 13:50:35 +0200 | [diff] [blame] | 3165 | if (btrfs_is_free_space_inode(BTRFS_I(inode))) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3166 | wq = fs_info->endio_freespace_worker; |
Liu Bo | 9e0af23 | 2014-08-15 23:36:53 +0800 | [diff] [blame] | 3167 | func = btrfs_freespace_write_helper; |
| 3168 | } else { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3169 | wq = fs_info->endio_write_workers; |
Liu Bo | 9e0af23 | 2014-08-15 23:36:53 +0800 | [diff] [blame] | 3170 | func = btrfs_endio_write_helper; |
| 3171 | } |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 3172 | |
Liu Bo | 9e0af23 | 2014-08-15 23:36:53 +0800 | [diff] [blame] | 3173 | btrfs_init_work(&ordered_extent->work, func, finish_ordered_fn, NULL, |
| 3174 | NULL); |
| 3175 | btrfs_queue_work(wq, &ordered_extent->work); |
Chris Mason | 211f90e | 2008-07-18 11:56:15 -0400 | [diff] [blame] | 3176 | } |
| 3177 | |
Miao Xie | dc380ae | 2014-09-12 18:43:55 +0800 | [diff] [blame] | 3178 | static int __readpage_endio_check(struct inode *inode, |
| 3179 | struct btrfs_io_bio *io_bio, |
| 3180 | int icsum, struct page *page, |
| 3181 | int pgoff, u64 start, size_t len) |
| 3182 | { |
| 3183 | char *kaddr; |
| 3184 | u32 csum_expected; |
| 3185 | u32 csum = ~(u32)0; |
Miao Xie | dc380ae | 2014-09-12 18:43:55 +0800 | [diff] [blame] | 3186 | |
| 3187 | csum_expected = *(((u32 *)io_bio->csum) + icsum); |
| 3188 | |
| 3189 | kaddr = kmap_atomic(page); |
| 3190 | csum = btrfs_csum_data(kaddr + pgoff, csum, len); |
Domagoj Tršan | 0b5e3da | 2016-10-27 08:52:33 +0100 | [diff] [blame] | 3191 | btrfs_csum_final(csum, (u8 *)&csum); |
Miao Xie | dc380ae | 2014-09-12 18:43:55 +0800 | [diff] [blame] | 3192 | if (csum != csum_expected) |
| 3193 | goto zeroit; |
| 3194 | |
| 3195 | kunmap_atomic(kaddr); |
| 3196 | return 0; |
| 3197 | zeroit: |
Nikolay Borisov | 0970a22 | 2017-02-20 13:50:53 +0200 | [diff] [blame] | 3198 | btrfs_print_data_csum_error(BTRFS_I(inode), start, csum, csum_expected, |
Qu Wenruo | 6f6b643 | 2017-02-09 10:45:06 +0800 | [diff] [blame] | 3199 | io_bio->mirror_num); |
Miao Xie | dc380ae | 2014-09-12 18:43:55 +0800 | [diff] [blame] | 3200 | memset(kaddr + pgoff, 1, len); |
| 3201 | flush_dcache_page(page); |
| 3202 | kunmap_atomic(kaddr); |
Miao Xie | dc380ae | 2014-09-12 18:43:55 +0800 | [diff] [blame] | 3203 | return -EIO; |
| 3204 | } |
| 3205 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3206 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3207 | * 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] | 3208 | * if there's a match, we allow the bio to finish. If not, the code in |
| 3209 | * extent_io.c will try to find good copies for us. |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3210 | */ |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 3211 | static int btrfs_readpage_end_io_hook(struct btrfs_io_bio *io_bio, |
| 3212 | u64 phy_offset, struct page *page, |
| 3213 | u64 start, u64 end, int mirror) |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 3214 | { |
Miao Xie | 4eee4fa | 2012-12-21 09:17:45 +0000 | [diff] [blame] | 3215 | size_t offset = start - page_offset(page); |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 3216 | struct inode *inode = page->mapping->host; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3217 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | ff79f81 | 2007-10-15 16:22:25 -0400 | [diff] [blame] | 3218 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3219 | |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 3220 | if (PageChecked(page)) { |
| 3221 | ClearPageChecked(page); |
Miao Xie | dc380ae | 2014-09-12 18:43:55 +0800 | [diff] [blame] | 3222 | return 0; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 3223 | } |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 3224 | |
| 3225 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM) |
Miao Xie | dc380ae | 2014-09-12 18:43:55 +0800 | [diff] [blame] | 3226 | return 0; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 3227 | |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 3228 | if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID && |
Chris Mason | 9655d29 | 2009-09-02 15:22:30 -0400 | [diff] [blame] | 3229 | test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) { |
David Sterba | 9116621 | 2016-04-26 23:54:39 +0200 | [diff] [blame] | 3230 | clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM); |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 3231 | return 0; |
| 3232 | } |
| 3233 | |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 3234 | phy_offset >>= inode->i_sb->s_blocksize_bits; |
Miao Xie | dc380ae | 2014-09-12 18:43:55 +0800 | [diff] [blame] | 3235 | return __readpage_endio_check(inode, io_bio, phy_offset, page, offset, |
| 3236 | start, (size_t)(end - start + 1)); |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 3237 | } |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3238 | |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 3239 | void btrfs_add_delayed_iput(struct inode *inode) |
| 3240 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3241 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
David Sterba | 8089fe6 | 2015-11-19 14:15:51 +0100 | [diff] [blame] | 3242 | struct btrfs_inode *binode = BTRFS_I(inode); |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 3243 | |
| 3244 | if (atomic_add_unless(&inode->i_count, -1, 1)) |
| 3245 | return; |
| 3246 | |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 3247 | spin_lock(&fs_info->delayed_iput_lock); |
David Sterba | 8089fe6 | 2015-11-19 14:15:51 +0100 | [diff] [blame] | 3248 | if (binode->delayed_iput_count == 0) { |
| 3249 | ASSERT(list_empty(&binode->delayed_iput)); |
| 3250 | list_add_tail(&binode->delayed_iput, &fs_info->delayed_iputs); |
| 3251 | } else { |
| 3252 | binode->delayed_iput_count++; |
| 3253 | } |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 3254 | spin_unlock(&fs_info->delayed_iput_lock); |
| 3255 | } |
| 3256 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3257 | void btrfs_run_delayed_iputs(struct btrfs_fs_info *fs_info) |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 3258 | { |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 3259 | |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 3260 | spin_lock(&fs_info->delayed_iput_lock); |
David Sterba | 8089fe6 | 2015-11-19 14:15:51 +0100 | [diff] [blame] | 3261 | while (!list_empty(&fs_info->delayed_iputs)) { |
| 3262 | struct btrfs_inode *inode; |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 3263 | |
David Sterba | 8089fe6 | 2015-11-19 14:15:51 +0100 | [diff] [blame] | 3264 | inode = list_first_entry(&fs_info->delayed_iputs, |
| 3265 | struct btrfs_inode, delayed_iput); |
| 3266 | if (inode->delayed_iput_count) { |
| 3267 | inode->delayed_iput_count--; |
| 3268 | list_move_tail(&inode->delayed_iput, |
| 3269 | &fs_info->delayed_iputs); |
| 3270 | } else { |
| 3271 | list_del_init(&inode->delayed_iput); |
| 3272 | } |
| 3273 | spin_unlock(&fs_info->delayed_iput_lock); |
| 3274 | iput(&inode->vfs_inode); |
| 3275 | spin_lock(&fs_info->delayed_iput_lock); |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 3276 | } |
David Sterba | 8089fe6 | 2015-11-19 14:15:51 +0100 | [diff] [blame] | 3277 | spin_unlock(&fs_info->delayed_iput_lock); |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 3278 | } |
| 3279 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3280 | /* |
Justin P. Mattock | 42b2aa8 | 2011-11-28 20:31:00 -0800 | [diff] [blame] | 3281 | * This is called in transaction commit time. If there are no orphan |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3282 | * files in the subvolume, it removes orphan item and frees block_rsv |
| 3283 | * structure. |
| 3284 | */ |
| 3285 | void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans, |
| 3286 | struct btrfs_root *root) |
| 3287 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3288 | struct btrfs_fs_info *fs_info = root->fs_info; |
Josef Bacik | 90290e1 | 2011-12-02 15:44:12 -0500 | [diff] [blame] | 3289 | struct btrfs_block_rsv *block_rsv; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3290 | int ret; |
| 3291 | |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 3292 | if (atomic_read(&root->orphan_inodes) || |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3293 | root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) |
| 3294 | return; |
| 3295 | |
Josef Bacik | 90290e1 | 2011-12-02 15:44:12 -0500 | [diff] [blame] | 3296 | spin_lock(&root->orphan_lock); |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 3297 | if (atomic_read(&root->orphan_inodes)) { |
Josef Bacik | 90290e1 | 2011-12-02 15:44:12 -0500 | [diff] [blame] | 3298 | spin_unlock(&root->orphan_lock); |
| 3299 | return; |
| 3300 | } |
| 3301 | |
| 3302 | if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) { |
| 3303 | spin_unlock(&root->orphan_lock); |
| 3304 | return; |
| 3305 | } |
| 3306 | |
| 3307 | block_rsv = root->orphan_block_rsv; |
| 3308 | root->orphan_block_rsv = NULL; |
| 3309 | spin_unlock(&root->orphan_lock); |
| 3310 | |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 3311 | if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state) && |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3312 | btrfs_root_refs(&root->root_item) > 0) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3313 | ret = btrfs_del_orphan_item(trans, fs_info->tree_root, |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3314 | root->root_key.objectid); |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 3315 | if (ret) |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3316 | btrfs_abort_transaction(trans, ret); |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 3317 | else |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 3318 | clear_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, |
| 3319 | &root->state); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3320 | } |
| 3321 | |
Josef Bacik | 90290e1 | 2011-12-02 15:44:12 -0500 | [diff] [blame] | 3322 | if (block_rsv) { |
| 3323 | WARN_ON(block_rsv->size > 0); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3324 | btrfs_free_block_rsv(fs_info, block_rsv); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3325 | } |
| 3326 | } |
| 3327 | |
| 3328 | /* |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3329 | * This creates an orphan entry for the given inode in case something goes |
| 3330 | * wrong in the middle of an unlink/truncate. |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3331 | * |
| 3332 | * NOTE: caller of this function should reserve 5 units of metadata for |
| 3333 | * this function. |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3334 | */ |
Nikolay Borisov | 73f2e54 | 2017-02-20 13:50:59 +0200 | [diff] [blame] | 3335 | int btrfs_orphan_add(struct btrfs_trans_handle *trans, |
| 3336 | struct btrfs_inode *inode) |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3337 | { |
Nikolay Borisov | 73f2e54 | 2017-02-20 13:50:59 +0200 | [diff] [blame] | 3338 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb); |
| 3339 | struct btrfs_root *root = inode->root; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3340 | struct btrfs_block_rsv *block_rsv = NULL; |
| 3341 | int reserve = 0; |
| 3342 | int insert = 0; |
| 3343 | int ret; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3344 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3345 | if (!root->orphan_block_rsv) { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3346 | block_rsv = btrfs_alloc_block_rsv(fs_info, |
| 3347 | BTRFS_BLOCK_RSV_TEMP); |
Tsutomu Itoh | b532402 | 2011-07-19 07:27:20 +0000 | [diff] [blame] | 3348 | if (!block_rsv) |
| 3349 | return -ENOMEM; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3350 | } |
| 3351 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3352 | spin_lock(&root->orphan_lock); |
| 3353 | if (!root->orphan_block_rsv) { |
| 3354 | root->orphan_block_rsv = block_rsv; |
| 3355 | } else if (block_rsv) { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3356 | btrfs_free_block_rsv(fs_info, block_rsv); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3357 | block_rsv = NULL; |
| 3358 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3359 | |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 3360 | if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
Nikolay Borisov | 73f2e54 | 2017-02-20 13:50:59 +0200 | [diff] [blame] | 3361 | &inode->runtime_flags)) { |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3362 | #if 0 |
| 3363 | /* |
| 3364 | * For proper ENOSPC handling, we should do orphan |
| 3365 | * cleanup when mounting. But this introduces backward |
| 3366 | * compatibility issue. |
| 3367 | */ |
| 3368 | if (!xchg(&root->orphan_item_inserted, 1)) |
| 3369 | insert = 2; |
| 3370 | else |
| 3371 | insert = 1; |
| 3372 | #endif |
| 3373 | insert = 1; |
Miao Xie | 321f0e7 | 2012-08-28 22:13:02 -0600 | [diff] [blame] | 3374 | atomic_inc(&root->orphan_inodes); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3375 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3376 | |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 3377 | if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED, |
Nikolay Borisov | 73f2e54 | 2017-02-20 13:50:59 +0200 | [diff] [blame] | 3378 | &inode->runtime_flags)) |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3379 | reserve = 1; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3380 | spin_unlock(&root->orphan_lock); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3381 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3382 | /* grab metadata reservation from transaction handle */ |
| 3383 | if (reserve) { |
| 3384 | ret = btrfs_orphan_reserve_metadata(trans, inode); |
Josef Bacik | 3b6571c | 2016-05-27 13:03:04 -0400 | [diff] [blame] | 3385 | ASSERT(!ret); |
| 3386 | if (ret) { |
| 3387 | atomic_dec(&root->orphan_inodes); |
| 3388 | clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED, |
Nikolay Borisov | 73f2e54 | 2017-02-20 13:50:59 +0200 | [diff] [blame] | 3389 | &inode->runtime_flags); |
Josef Bacik | 3b6571c | 2016-05-27 13:03:04 -0400 | [diff] [blame] | 3390 | if (insert) |
| 3391 | clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
Nikolay Borisov | 73f2e54 | 2017-02-20 13:50:59 +0200 | [diff] [blame] | 3392 | &inode->runtime_flags); |
Josef Bacik | 3b6571c | 2016-05-27 13:03:04 -0400 | [diff] [blame] | 3393 | return ret; |
| 3394 | } |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3395 | } |
| 3396 | |
| 3397 | /* insert an orphan item to track this unlinked/truncated file */ |
| 3398 | if (insert >= 1) { |
Nikolay Borisov | 73f2e54 | 2017-02-20 13:50:59 +0200 | [diff] [blame] | 3399 | ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode)); |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 3400 | if (ret) { |
Filipe David Borba Manana | 703c88e | 2013-09-22 21:54:55 +0100 | [diff] [blame] | 3401 | atomic_dec(&root->orphan_inodes); |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 3402 | if (reserve) { |
| 3403 | clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED, |
Nikolay Borisov | 73f2e54 | 2017-02-20 13:50:59 +0200 | [diff] [blame] | 3404 | &inode->runtime_flags); |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 3405 | btrfs_orphan_release_metadata(inode); |
| 3406 | } |
| 3407 | if (ret != -EEXIST) { |
Josef Bacik | e8e7cff | 2013-08-21 15:54:00 -0400 | [diff] [blame] | 3408 | clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
Nikolay Borisov | 73f2e54 | 2017-02-20 13:50:59 +0200 | [diff] [blame] | 3409 | &inode->runtime_flags); |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3410 | btrfs_abort_transaction(trans, ret); |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 3411 | return ret; |
| 3412 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3413 | } |
| 3414 | ret = 0; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3415 | } |
| 3416 | |
| 3417 | /* insert an orphan item to track subvolume contains orphan files */ |
| 3418 | if (insert >= 2) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3419 | ret = btrfs_insert_orphan_item(trans, fs_info->tree_root, |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3420 | root->root_key.objectid); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3421 | if (ret && ret != -EEXIST) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3422 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3423 | return ret; |
| 3424 | } |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3425 | } |
| 3426 | return 0; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3427 | } |
| 3428 | |
| 3429 | /* |
| 3430 | * We have done the truncate/delete so we can go ahead and remove the orphan |
| 3431 | * item for this particular inode. |
| 3432 | */ |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 3433 | static int btrfs_orphan_del(struct btrfs_trans_handle *trans, |
Nikolay Borisov | 3d6ae7b | 2017-02-20 13:50:58 +0200 | [diff] [blame] | 3434 | struct btrfs_inode *inode) |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3435 | { |
Nikolay Borisov | 3d6ae7b | 2017-02-20 13:50:58 +0200 | [diff] [blame] | 3436 | struct btrfs_root *root = inode->root; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3437 | int delete_item = 0; |
| 3438 | int release_rsv = 0; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3439 | int ret = 0; |
| 3440 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3441 | spin_lock(&root->orphan_lock); |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 3442 | if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
Nikolay Borisov | 3d6ae7b | 2017-02-20 13:50:58 +0200 | [diff] [blame] | 3443 | &inode->runtime_flags)) |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3444 | delete_item = 1; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3445 | |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 3446 | if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED, |
Nikolay Borisov | 3d6ae7b | 2017-02-20 13:50:58 +0200 | [diff] [blame] | 3447 | &inode->runtime_flags)) |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3448 | release_rsv = 1; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3449 | spin_unlock(&root->orphan_lock); |
| 3450 | |
Filipe David Borba Manana | 703c88e | 2013-09-22 21:54:55 +0100 | [diff] [blame] | 3451 | if (delete_item) { |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 3452 | atomic_dec(&root->orphan_inodes); |
Filipe David Borba Manana | 703c88e | 2013-09-22 21:54:55 +0100 | [diff] [blame] | 3453 | if (trans) |
| 3454 | ret = btrfs_del_orphan_item(trans, root, |
Nikolay Borisov | 3d6ae7b | 2017-02-20 13:50:58 +0200 | [diff] [blame] | 3455 | btrfs_ino(inode)); |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 3456 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3457 | |
Filipe David Borba Manana | 703c88e | 2013-09-22 21:54:55 +0100 | [diff] [blame] | 3458 | if (release_rsv) |
| 3459 | btrfs_orphan_release_metadata(inode); |
| 3460 | |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 3461 | return ret; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3462 | } |
| 3463 | |
| 3464 | /* |
| 3465 | * this cleans up any orphans that may be left on the list from the last use |
| 3466 | * of this root. |
| 3467 | */ |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3468 | int btrfs_orphan_cleanup(struct btrfs_root *root) |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3469 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3470 | struct btrfs_fs_info *fs_info = root->fs_info; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3471 | struct btrfs_path *path; |
| 3472 | struct extent_buffer *leaf; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3473 | struct btrfs_key key, found_key; |
| 3474 | struct btrfs_trans_handle *trans; |
| 3475 | struct inode *inode; |
Josef Bacik | 8f6d7f4 | 2011-09-26 15:55:20 -0400 | [diff] [blame] | 3476 | u64 last_objectid = 0; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3477 | int ret = 0, nr_unlink = 0, nr_truncate = 0; |
| 3478 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3479 | if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED)) |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3480 | return 0; |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 3481 | |
| 3482 | path = btrfs_alloc_path(); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3483 | if (!path) { |
| 3484 | ret = -ENOMEM; |
| 3485 | goto out; |
| 3486 | } |
David Sterba | e4058b5 | 2015-11-27 16:31:35 +0100 | [diff] [blame] | 3487 | path->reada = READA_BACK; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3488 | |
| 3489 | key.objectid = BTRFS_ORPHAN_OBJECTID; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 3490 | key.type = BTRFS_ORPHAN_ITEM_KEY; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3491 | key.offset = (u64)-1; |
| 3492 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3493 | while (1) { |
| 3494 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3495 | if (ret < 0) |
| 3496 | goto out; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3497 | |
| 3498 | /* |
| 3499 | * 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] | 3500 | * 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] | 3501 | * find the key and see if we have stuff that matches |
| 3502 | */ |
| 3503 | if (ret > 0) { |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3504 | ret = 0; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3505 | if (path->slots[0] == 0) |
| 3506 | break; |
| 3507 | path->slots[0]--; |
| 3508 | } |
| 3509 | |
| 3510 | /* pull out the item */ |
| 3511 | leaf = path->nodes[0]; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3512 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 3513 | |
| 3514 | /* make sure the item matches what we want */ |
| 3515 | if (found_key.objectid != BTRFS_ORPHAN_OBJECTID) |
| 3516 | break; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 3517 | if (found_key.type != BTRFS_ORPHAN_ITEM_KEY) |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3518 | break; |
| 3519 | |
| 3520 | /* release the path since we're done with it */ |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3521 | btrfs_release_path(path); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3522 | |
| 3523 | /* |
| 3524 | * this is where we are basically btrfs_lookup, without the |
| 3525 | * crossing root thing. we store the inode number in the |
| 3526 | * offset of the orphan item. |
| 3527 | */ |
Josef Bacik | 8f6d7f4 | 2011-09-26 15:55:20 -0400 | [diff] [blame] | 3528 | |
| 3529 | if (found_key.offset == last_objectid) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3530 | btrfs_err(fs_info, |
| 3531 | "Error removing orphan entry, stopping orphan cleanup"); |
Josef Bacik | 8f6d7f4 | 2011-09-26 15:55:20 -0400 | [diff] [blame] | 3532 | ret = -EINVAL; |
| 3533 | goto out; |
| 3534 | } |
| 3535 | |
| 3536 | last_objectid = found_key.offset; |
| 3537 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3538 | found_key.objectid = found_key.offset; |
| 3539 | found_key.type = BTRFS_INODE_ITEM_KEY; |
| 3540 | found_key.offset = 0; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3541 | inode = btrfs_iget(fs_info->sb, &found_key, root, NULL); |
Rusty Russell | 8c6ffba | 2013-07-15 11:20:32 +0930 | [diff] [blame] | 3542 | ret = PTR_ERR_OR_ZERO(inode); |
Filipe Manana | 6771089 | 2016-06-06 11:51:25 +0100 | [diff] [blame] | 3543 | if (ret && ret != -ENOENT) |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3544 | goto out; |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 3545 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3546 | if (ret == -ENOENT && root == fs_info->tree_root) { |
Arne Jansen | f8e9e0b | 2011-12-14 20:12:02 -0500 | [diff] [blame] | 3547 | struct btrfs_root *dead_root; |
| 3548 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 3549 | int is_dead_root = 0; |
| 3550 | |
| 3551 | /* |
| 3552 | * this is an orphan in the tree root. Currently these |
| 3553 | * could come from 2 sources: |
| 3554 | * a) a snapshot deletion in progress |
| 3555 | * b) a free space cache inode |
| 3556 | * We need to distinguish those two, as the snapshot |
| 3557 | * orphan must not get deleted. |
| 3558 | * find_dead_roots already ran before us, so if this |
| 3559 | * is a snapshot deletion, we should find the root |
| 3560 | * in the dead_roots list |
| 3561 | */ |
| 3562 | spin_lock(&fs_info->trans_lock); |
| 3563 | list_for_each_entry(dead_root, &fs_info->dead_roots, |
| 3564 | root_list) { |
| 3565 | if (dead_root->root_key.objectid == |
| 3566 | found_key.objectid) { |
| 3567 | is_dead_root = 1; |
| 3568 | break; |
| 3569 | } |
| 3570 | } |
| 3571 | spin_unlock(&fs_info->trans_lock); |
| 3572 | if (is_dead_root) { |
| 3573 | /* prevent this orphan from being found again */ |
| 3574 | key.offset = found_key.objectid - 1; |
| 3575 | continue; |
| 3576 | } |
| 3577 | } |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 3578 | /* |
| 3579 | * Inode is already gone but the orphan item is still there, |
| 3580 | * kill the orphan item. |
| 3581 | */ |
Filipe Manana | 6771089 | 2016-06-06 11:51:25 +0100 | [diff] [blame] | 3582 | if (ret == -ENOENT) { |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 3583 | trans = btrfs_start_transaction(root, 1); |
| 3584 | if (IS_ERR(trans)) { |
| 3585 | ret = PTR_ERR(trans); |
| 3586 | goto out; |
| 3587 | } |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3588 | btrfs_debug(fs_info, "auto deleting %Lu", |
| 3589 | found_key.objectid); |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 3590 | ret = btrfs_del_orphan_item(trans, root, |
| 3591 | found_key.objectid); |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 3592 | btrfs_end_transaction(trans); |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 3593 | if (ret) |
| 3594 | goto out; |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 3595 | continue; |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3596 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3597 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3598 | /* |
| 3599 | * add this inode to the orphan list so btrfs_orphan_del does |
| 3600 | * the proper thing when we hit it |
| 3601 | */ |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 3602 | set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
| 3603 | &BTRFS_I(inode)->runtime_flags); |
Josef Bacik | 925396e | 2013-02-01 15:57:47 -0500 | [diff] [blame] | 3604 | atomic_inc(&root->orphan_inodes); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3605 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3606 | /* if we have links, this was a truncate, lets do that */ |
| 3607 | if (inode->i_nlink) { |
Dulshani Gunawardhana | fae7f21 | 2013-10-31 10:30:08 +0530 | [diff] [blame] | 3608 | if (WARN_ON(!S_ISREG(inode->i_mode))) { |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 3609 | iput(inode); |
| 3610 | continue; |
| 3611 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3612 | nr_truncate++; |
Josef Bacik | f3fe820 | 2013-01-07 17:03:21 -0500 | [diff] [blame] | 3613 | |
| 3614 | /* 1 for the orphan item deletion. */ |
| 3615 | trans = btrfs_start_transaction(root, 1); |
| 3616 | if (IS_ERR(trans)) { |
Josef Bacik | c69b26b | 2013-06-03 16:51:23 -0400 | [diff] [blame] | 3617 | iput(inode); |
Josef Bacik | f3fe820 | 2013-01-07 17:03:21 -0500 | [diff] [blame] | 3618 | ret = PTR_ERR(trans); |
| 3619 | goto out; |
| 3620 | } |
Nikolay Borisov | 73f2e54 | 2017-02-20 13:50:59 +0200 | [diff] [blame] | 3621 | ret = btrfs_orphan_add(trans, BTRFS_I(inode)); |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 3622 | btrfs_end_transaction(trans); |
Josef Bacik | c69b26b | 2013-06-03 16:51:23 -0400 | [diff] [blame] | 3623 | if (ret) { |
| 3624 | iput(inode); |
Josef Bacik | f3fe820 | 2013-01-07 17:03:21 -0500 | [diff] [blame] | 3625 | goto out; |
Josef Bacik | c69b26b | 2013-06-03 16:51:23 -0400 | [diff] [blame] | 3626 | } |
Josef Bacik | f3fe820 | 2013-01-07 17:03:21 -0500 | [diff] [blame] | 3627 | |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3628 | ret = btrfs_truncate(inode); |
Josef Bacik | 4a7d0f6 | 2013-02-07 16:27:28 -0500 | [diff] [blame] | 3629 | if (ret) |
Nikolay Borisov | 3d6ae7b | 2017-02-20 13:50:58 +0200 | [diff] [blame] | 3630 | btrfs_orphan_del(NULL, BTRFS_I(inode)); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3631 | } else { |
| 3632 | nr_unlink++; |
| 3633 | } |
| 3634 | |
| 3635 | /* this will do delete_inode and everything for us */ |
| 3636 | iput(inode); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3637 | if (ret) |
| 3638 | goto out; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3639 | } |
Miao Xie | 3254c87 | 2011-11-10 20:45:05 -0500 | [diff] [blame] | 3640 | /* release the path since we're done with it */ |
| 3641 | btrfs_release_path(path); |
| 3642 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3643 | root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE; |
| 3644 | |
| 3645 | if (root->orphan_block_rsv) |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3646 | btrfs_block_rsv_release(fs_info, root->orphan_block_rsv, |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3647 | (u64)-1); |
| 3648 | |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 3649 | if (root->orphan_block_rsv || |
| 3650 | test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) { |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 3651 | trans = btrfs_join_transaction(root); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3652 | if (!IS_ERR(trans)) |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 3653 | btrfs_end_transaction(trans); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3654 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3655 | |
| 3656 | if (nr_unlink) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3657 | btrfs_debug(fs_info, "unlinked %d orphans", nr_unlink); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3658 | if (nr_truncate) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3659 | btrfs_debug(fs_info, "truncated %d orphans", nr_truncate); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3660 | |
| 3661 | out: |
| 3662 | if (ret) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3663 | btrfs_err(fs_info, "could not do orphan cleanup %d", ret); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3664 | btrfs_free_path(path); |
| 3665 | return ret; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3666 | } |
| 3667 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3668 | /* |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3669 | * very simple check to peek ahead in the leaf looking for xattrs. If we |
| 3670 | * don't find any xattrs, we know there can't be any acls. |
| 3671 | * |
| 3672 | * slot is the slot the inode is in, objectid is the objectid of the inode |
| 3673 | */ |
| 3674 | 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] | 3675 | int slot, u64 objectid, |
| 3676 | int *first_xattr_slot) |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3677 | { |
| 3678 | u32 nritems = btrfs_header_nritems(leaf); |
| 3679 | struct btrfs_key found_key; |
Josef Bacik | f23b5a5 | 2013-06-19 10:16:26 -0400 | [diff] [blame] | 3680 | static u64 xattr_access = 0; |
| 3681 | static u64 xattr_default = 0; |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3682 | int scanned = 0; |
| 3683 | |
Josef Bacik | f23b5a5 | 2013-06-19 10:16:26 -0400 | [diff] [blame] | 3684 | if (!xattr_access) { |
Andreas Gruenbacher | 97d7929 | 2015-12-02 14:44:35 +0100 | [diff] [blame] | 3685 | xattr_access = btrfs_name_hash(XATTR_NAME_POSIX_ACL_ACCESS, |
| 3686 | strlen(XATTR_NAME_POSIX_ACL_ACCESS)); |
| 3687 | xattr_default = btrfs_name_hash(XATTR_NAME_POSIX_ACL_DEFAULT, |
| 3688 | strlen(XATTR_NAME_POSIX_ACL_DEFAULT)); |
Josef Bacik | f23b5a5 | 2013-06-19 10:16:26 -0400 | [diff] [blame] | 3689 | } |
| 3690 | |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3691 | slot++; |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 3692 | *first_xattr_slot = -1; |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3693 | while (slot < nritems) { |
| 3694 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
| 3695 | |
| 3696 | /* we found a different objectid, there must not be acls */ |
| 3697 | if (found_key.objectid != objectid) |
| 3698 | return 0; |
| 3699 | |
| 3700 | /* we found an xattr, assume we've got an acl */ |
Josef Bacik | f23b5a5 | 2013-06-19 10:16:26 -0400 | [diff] [blame] | 3701 | if (found_key.type == BTRFS_XATTR_ITEM_KEY) { |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 3702 | if (*first_xattr_slot == -1) |
| 3703 | *first_xattr_slot = slot; |
Josef Bacik | f23b5a5 | 2013-06-19 10:16:26 -0400 | [diff] [blame] | 3704 | if (found_key.offset == xattr_access || |
| 3705 | found_key.offset == xattr_default) |
| 3706 | return 1; |
| 3707 | } |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3708 | |
| 3709 | /* |
| 3710 | * we found a key greater than an xattr key, there can't |
| 3711 | * be any acls later on |
| 3712 | */ |
| 3713 | if (found_key.type > BTRFS_XATTR_ITEM_KEY) |
| 3714 | return 0; |
| 3715 | |
| 3716 | slot++; |
| 3717 | scanned++; |
| 3718 | |
| 3719 | /* |
| 3720 | * it goes inode, inode backrefs, xattrs, extents, |
| 3721 | * so if there are a ton of hard links to an inode there can |
| 3722 | * be a lot of backrefs. Don't waste time searching too hard, |
| 3723 | * this is just an optimization |
| 3724 | */ |
| 3725 | if (scanned >= 8) |
| 3726 | break; |
| 3727 | } |
| 3728 | /* we hit the end of the leaf before we found an xattr or |
| 3729 | * something larger than an xattr. We have to assume the inode |
| 3730 | * has acls |
| 3731 | */ |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 3732 | if (*first_xattr_slot == -1) |
| 3733 | *first_xattr_slot = slot; |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3734 | return 1; |
| 3735 | } |
| 3736 | |
| 3737 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3738 | * read an inode from the btree into the in-memory inode |
| 3739 | */ |
Filipe Manana | 6771089 | 2016-06-06 11:51:25 +0100 | [diff] [blame] | 3740 | static int btrfs_read_locked_inode(struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3741 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3742 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3743 | struct btrfs_path *path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3744 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3745 | struct btrfs_inode_item *inode_item; |
| 3746 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 3747 | struct btrfs_key location; |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 3748 | unsigned long ptr; |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3749 | int maybe_acls; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 3750 | u32 rdev; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3751 | int ret; |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 3752 | bool filled = false; |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 3753 | int first_xattr_slot; |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 3754 | |
| 3755 | ret = btrfs_fill_inode(inode, &rdev); |
| 3756 | if (!ret) |
| 3757 | filled = true; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3758 | |
| 3759 | path = btrfs_alloc_path(); |
Filipe Manana | 6771089 | 2016-06-06 11:51:25 +0100 | [diff] [blame] | 3760 | if (!path) { |
| 3761 | ret = -ENOMEM; |
Mark Fasheh | 1748f84 | 2011-07-12 11:25:31 -0700 | [diff] [blame] | 3762 | goto make_bad; |
Filipe Manana | 6771089 | 2016-06-06 11:51:25 +0100 | [diff] [blame] | 3763 | } |
Mark Fasheh | 1748f84 | 2011-07-12 11:25:31 -0700 | [diff] [blame] | 3764 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3765 | memcpy(&location, &BTRFS_I(inode)->location, sizeof(location)); |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 3766 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3767 | ret = btrfs_lookup_inode(NULL, root, path, &location, 0); |
Filipe Manana | 6771089 | 2016-06-06 11:51:25 +0100 | [diff] [blame] | 3768 | if (ret) { |
| 3769 | if (ret > 0) |
| 3770 | ret = -ENOENT; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3771 | goto make_bad; |
Filipe Manana | 6771089 | 2016-06-06 11:51:25 +0100 | [diff] [blame] | 3772 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3773 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3774 | leaf = path->nodes[0]; |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 3775 | |
| 3776 | if (filled) |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 3777 | goto cache_index; |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 3778 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3779 | inode_item = btrfs_item_ptr(leaf, path->slots[0], |
| 3780 | struct btrfs_inode_item); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3781 | inode->i_mode = btrfs_inode_mode(leaf, inode_item); |
Miklos Szeredi | bfe8684 | 2011-10-28 14:13:29 +0200 | [diff] [blame] | 3782 | set_nlink(inode, btrfs_inode_nlink(leaf, inode_item)); |
Eric W. Biederman | 2f2f43d | 2012-02-10 11:05:07 -0800 | [diff] [blame] | 3783 | i_uid_write(inode, btrfs_inode_uid(leaf, inode_item)); |
| 3784 | i_gid_write(inode, btrfs_inode_gid(leaf, inode_item)); |
Nikolay Borisov | 6ef06d2 | 2017-02-20 13:50:34 +0200 | [diff] [blame] | 3785 | 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] | 3786 | |
David Sterba | a937b97 | 2014-12-12 17:39:12 +0100 | [diff] [blame] | 3787 | inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->atime); |
| 3788 | inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->atime); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3789 | |
David Sterba | a937b97 | 2014-12-12 17:39:12 +0100 | [diff] [blame] | 3790 | inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->mtime); |
| 3791 | inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->mtime); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3792 | |
David Sterba | a937b97 | 2014-12-12 17:39:12 +0100 | [diff] [blame] | 3793 | inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->ctime); |
| 3794 | inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->ctime); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3795 | |
chandan r | 9cc97d6 | 2012-07-04 12:48:07 +0530 | [diff] [blame] | 3796 | BTRFS_I(inode)->i_otime.tv_sec = |
| 3797 | btrfs_timespec_sec(leaf, &inode_item->otime); |
| 3798 | BTRFS_I(inode)->i_otime.tv_nsec = |
| 3799 | btrfs_timespec_nsec(leaf, &inode_item->otime); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3800 | |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 3801 | inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item)); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3802 | BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item); |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 3803 | BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item); |
| 3804 | |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 3805 | inode->i_version = btrfs_inode_sequence(leaf, inode_item); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3806 | inode->i_generation = BTRFS_I(inode)->generation; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 3807 | inode->i_rdev = 0; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3808 | rdev = btrfs_inode_rdev(leaf, inode_item); |
| 3809 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 3810 | BTRFS_I(inode)->index_cnt = (u64)-1; |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 3811 | BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item); |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 3812 | |
| 3813 | cache_index: |
Yang Dongsheng | 6e17d30 | 2015-04-09 12:08:43 +0800 | [diff] [blame] | 3814 | /* |
| 3815 | * If we were modified in the current generation and evicted from memory |
| 3816 | * and then re-read we need to do a full sync since we don't have any |
| 3817 | * idea about which extents were modified before we were evicted from |
| 3818 | * cache. |
| 3819 | * |
| 3820 | * This is required for both inode re-read from disk and delayed inode |
| 3821 | * in delayed_nodes_tree. |
| 3822 | */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3823 | if (BTRFS_I(inode)->last_trans == fs_info->generation) |
Yang Dongsheng | 6e17d30 | 2015-04-09 12:08:43 +0800 | [diff] [blame] | 3824 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, |
| 3825 | &BTRFS_I(inode)->runtime_flags); |
| 3826 | |
Filipe Manana | bde6c24 | 2015-07-24 00:00:19 +0100 | [diff] [blame] | 3827 | /* |
| 3828 | * We don't persist the id of the transaction where an unlink operation |
| 3829 | * against the inode was last made. So here we assume the inode might |
| 3830 | * have been evicted, and therefore the exact value of last_unlink_trans |
| 3831 | * lost, and set it to last_trans to avoid metadata inconsistencies |
| 3832 | * between the inode and its parent if the inode is fsync'ed and the log |
| 3833 | * replayed. For example, in the scenario: |
| 3834 | * |
| 3835 | * touch mydir/foo |
| 3836 | * ln mydir/foo mydir/bar |
| 3837 | * sync |
| 3838 | * unlink mydir/bar |
| 3839 | * echo 2 > /proc/sys/vm/drop_caches # evicts inode |
| 3840 | * xfs_io -c fsync mydir/foo |
| 3841 | * <power failure> |
| 3842 | * mount fs, triggers fsync log replay |
| 3843 | * |
| 3844 | * We must make sure that when we fsync our inode foo we also log its |
| 3845 | * parent inode, otherwise after log replay the parent still has the |
| 3846 | * dentry with the "bar" name but our inode foo has a link count of 1 |
| 3847 | * and doesn't have an inode ref with the name "bar" anymore. |
| 3848 | * |
| 3849 | * Setting last_unlink_trans to last_trans is a pessimistic approach, |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 3850 | * but it guarantees correctness at the expense of occasional full |
Filipe Manana | bde6c24 | 2015-07-24 00:00:19 +0100 | [diff] [blame] | 3851 | * transaction commits on fsync if our inode is a directory, or if our |
| 3852 | * inode is not a directory, logging its parent unnecessarily. |
| 3853 | */ |
| 3854 | BTRFS_I(inode)->last_unlink_trans = BTRFS_I(inode)->last_trans; |
| 3855 | |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 3856 | path->slots[0]++; |
| 3857 | if (inode->i_nlink != 1 || |
| 3858 | path->slots[0] >= btrfs_header_nritems(leaf)) |
| 3859 | goto cache_acl; |
| 3860 | |
| 3861 | btrfs_item_key_to_cpu(leaf, &location, path->slots[0]); |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 3862 | if (location.objectid != btrfs_ino(BTRFS_I(inode))) |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 3863 | goto cache_acl; |
| 3864 | |
| 3865 | ptr = btrfs_item_ptr_offset(leaf, path->slots[0]); |
| 3866 | if (location.type == BTRFS_INODE_REF_KEY) { |
| 3867 | struct btrfs_inode_ref *ref; |
| 3868 | |
| 3869 | ref = (struct btrfs_inode_ref *)ptr; |
| 3870 | BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref); |
| 3871 | } else if (location.type == BTRFS_INODE_EXTREF_KEY) { |
| 3872 | struct btrfs_inode_extref *extref; |
| 3873 | |
| 3874 | extref = (struct btrfs_inode_extref *)ptr; |
| 3875 | BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf, |
| 3876 | extref); |
| 3877 | } |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 3878 | cache_acl: |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3879 | /* |
| 3880 | * try to precache a NULL acl entry for files that don't have |
| 3881 | * any xattrs or acls |
| 3882 | */ |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3883 | maybe_acls = acls_after_inode_item(leaf, path->slots[0], |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 3884 | btrfs_ino(BTRFS_I(inode)), &first_xattr_slot); |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 3885 | if (first_xattr_slot != -1) { |
| 3886 | path->slots[0] = first_xattr_slot; |
| 3887 | ret = btrfs_load_inode_props(inode, path); |
| 3888 | if (ret) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3889 | btrfs_err(fs_info, |
David Sterba | 351fd35 | 2014-05-15 16:48:20 +0200 | [diff] [blame] | 3890 | "error loading props for ino %llu (root %llu): %d", |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 3891 | btrfs_ino(BTRFS_I(inode)), |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 3892 | root->root_key.objectid, ret); |
| 3893 | } |
| 3894 | btrfs_free_path(path); |
| 3895 | |
Al Viro | 72c0490 | 2009-06-24 16:58:48 -0400 | [diff] [blame] | 3896 | if (!maybe_acls) |
| 3897 | cache_no_acl(inode); |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3898 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3899 | switch (inode->i_mode & S_IFMT) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3900 | case S_IFREG: |
| 3901 | inode->i_mapping->a_ops = &btrfs_aops; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3902 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3903 | inode->i_fop = &btrfs_file_operations; |
| 3904 | inode->i_op = &btrfs_file_inode_operations; |
| 3905 | break; |
| 3906 | case S_IFDIR: |
| 3907 | inode->i_fop = &btrfs_dir_file_operations; |
Omar Sandoval | 67ade05 | 2017-01-25 17:06:38 -0800 | [diff] [blame] | 3908 | inode->i_op = &btrfs_dir_inode_operations; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3909 | break; |
| 3910 | case S_IFLNK: |
| 3911 | inode->i_op = &btrfs_symlink_inode_operations; |
Al Viro | 21fc61c | 2015-11-17 01:07:57 -0500 | [diff] [blame] | 3912 | inode_nohighmem(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3913 | inode->i_mapping->a_ops = &btrfs_symlink_aops; |
| 3914 | break; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 3915 | default: |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 3916 | inode->i_op = &btrfs_special_inode_operations; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 3917 | init_special_inode(inode, inode->i_mode, rdev); |
| 3918 | break; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3919 | } |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 3920 | |
| 3921 | btrfs_update_iflags(inode); |
Filipe Manana | 6771089 | 2016-06-06 11:51:25 +0100 | [diff] [blame] | 3922 | return 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3923 | |
| 3924 | make_bad: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3925 | btrfs_free_path(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3926 | make_bad_inode(inode); |
Filipe Manana | 6771089 | 2016-06-06 11:51:25 +0100 | [diff] [blame] | 3927 | return ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3928 | } |
| 3929 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3930 | /* |
| 3931 | * given a leaf and an inode, copy the inode fields into the leaf |
| 3932 | */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3933 | static void fill_inode_item(struct btrfs_trans_handle *trans, |
| 3934 | struct extent_buffer *leaf, |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3935 | struct btrfs_inode_item *item, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3936 | struct inode *inode) |
| 3937 | { |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3938 | struct btrfs_map_token token; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3939 | |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3940 | btrfs_init_map_token(&token); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3941 | |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3942 | btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token); |
| 3943 | btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token); |
| 3944 | btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size, |
| 3945 | &token); |
| 3946 | btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token); |
| 3947 | btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3948 | |
David Sterba | a937b97 | 2014-12-12 17:39:12 +0100 | [diff] [blame] | 3949 | btrfs_set_token_timespec_sec(leaf, &item->atime, |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3950 | inode->i_atime.tv_sec, &token); |
David Sterba | a937b97 | 2014-12-12 17:39:12 +0100 | [diff] [blame] | 3951 | btrfs_set_token_timespec_nsec(leaf, &item->atime, |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3952 | inode->i_atime.tv_nsec, &token); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3953 | |
David Sterba | a937b97 | 2014-12-12 17:39:12 +0100 | [diff] [blame] | 3954 | btrfs_set_token_timespec_sec(leaf, &item->mtime, |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3955 | inode->i_mtime.tv_sec, &token); |
David Sterba | a937b97 | 2014-12-12 17:39:12 +0100 | [diff] [blame] | 3956 | btrfs_set_token_timespec_nsec(leaf, &item->mtime, |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3957 | inode->i_mtime.tv_nsec, &token); |
| 3958 | |
David Sterba | a937b97 | 2014-12-12 17:39:12 +0100 | [diff] [blame] | 3959 | btrfs_set_token_timespec_sec(leaf, &item->ctime, |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3960 | inode->i_ctime.tv_sec, &token); |
David Sterba | a937b97 | 2014-12-12 17:39:12 +0100 | [diff] [blame] | 3961 | btrfs_set_token_timespec_nsec(leaf, &item->ctime, |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3962 | inode->i_ctime.tv_nsec, &token); |
| 3963 | |
chandan r | 9cc97d6 | 2012-07-04 12:48:07 +0530 | [diff] [blame] | 3964 | btrfs_set_token_timespec_sec(leaf, &item->otime, |
| 3965 | BTRFS_I(inode)->i_otime.tv_sec, &token); |
| 3966 | btrfs_set_token_timespec_nsec(leaf, &item->otime, |
| 3967 | BTRFS_I(inode)->i_otime.tv_nsec, &token); |
| 3968 | |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3969 | btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode), |
| 3970 | &token); |
| 3971 | btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation, |
| 3972 | &token); |
| 3973 | btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token); |
| 3974 | btrfs_set_token_inode_transid(leaf, item, trans->transid, &token); |
| 3975 | btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token); |
| 3976 | btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token); |
| 3977 | btrfs_set_token_inode_block_group(leaf, item, 0, &token); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3978 | } |
| 3979 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3980 | /* |
| 3981 | * copy everything in the in-memory inode into the btree. |
| 3982 | */ |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 3983 | static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3984 | struct btrfs_root *root, struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3985 | { |
| 3986 | struct btrfs_inode_item *inode_item; |
| 3987 | struct btrfs_path *path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3988 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3989 | int ret; |
| 3990 | |
| 3991 | path = btrfs_alloc_path(); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 3992 | if (!path) |
| 3993 | return -ENOMEM; |
| 3994 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 3995 | path->leave_spinning = 1; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 3996 | ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location, |
| 3997 | 1); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3998 | if (ret) { |
| 3999 | if (ret > 0) |
| 4000 | ret = -ENOENT; |
| 4001 | goto failed; |
| 4002 | } |
| 4003 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4004 | leaf = path->nodes[0]; |
| 4005 | inode_item = btrfs_item_ptr(leaf, path->slots[0], |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 4006 | struct btrfs_inode_item); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4007 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4008 | fill_inode_item(trans, leaf, inode_item, inode); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4009 | btrfs_mark_buffer_dirty(leaf); |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 4010 | btrfs_set_inode_last_trans(trans, inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4011 | ret = 0; |
| 4012 | failed: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4013 | btrfs_free_path(path); |
| 4014 | return ret; |
| 4015 | } |
| 4016 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 4017 | /* |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 4018 | * copy everything in the in-memory inode into the btree. |
| 4019 | */ |
| 4020 | noinline int btrfs_update_inode(struct btrfs_trans_handle *trans, |
| 4021 | struct btrfs_root *root, struct inode *inode) |
| 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 | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 4024 | int ret; |
| 4025 | |
| 4026 | /* |
| 4027 | * If the inode is a free space inode, we can deadlock during commit |
| 4028 | * if we put it into the delayed code. |
| 4029 | * |
| 4030 | * The data relocation inode should also be directly updated |
| 4031 | * without delay |
| 4032 | */ |
Nikolay Borisov | 70ddc55 | 2017-02-20 13:50:35 +0200 | [diff] [blame] | 4033 | if (!btrfs_is_free_space_inode(BTRFS_I(inode)) |
Josef Bacik | 1d52c78 | 2014-09-18 11:30:44 -0400 | [diff] [blame] | 4034 | && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4035 | && !test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) { |
Alexander Block | 8ea05e3 | 2012-07-25 17:35:53 +0200 | [diff] [blame] | 4036 | btrfs_update_root_times(trans, root); |
| 4037 | |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 4038 | ret = btrfs_delayed_update_inode(trans, root, inode); |
| 4039 | if (!ret) |
| 4040 | btrfs_set_inode_last_trans(trans, inode); |
| 4041 | return ret; |
| 4042 | } |
| 4043 | |
| 4044 | return btrfs_update_inode_item(trans, root, inode); |
| 4045 | } |
| 4046 | |
Josef Bacik | be6aef6 | 2012-10-22 15:43:12 -0400 | [diff] [blame] | 4047 | noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans, |
| 4048 | struct btrfs_root *root, |
| 4049 | struct inode *inode) |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 4050 | { |
| 4051 | int ret; |
| 4052 | |
| 4053 | ret = btrfs_update_inode(trans, root, inode); |
| 4054 | if (ret == -ENOSPC) |
| 4055 | return btrfs_update_inode_item(trans, root, inode); |
| 4056 | return ret; |
| 4057 | } |
| 4058 | |
| 4059 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 4060 | * unlink helper that gets used here in inode.c and in the tree logging |
| 4061 | * recovery code. It remove a link in a directory with a given name, and |
| 4062 | * also drops the back refs in the inode to the directory |
| 4063 | */ |
Al Viro | 9298679 | 2011-03-04 17:14:37 +0000 | [diff] [blame] | 4064 | static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans, |
| 4065 | struct btrfs_root *root, |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 4066 | struct btrfs_inode *dir, |
| 4067 | struct btrfs_inode *inode, |
Al Viro | 9298679 | 2011-03-04 17:14:37 +0000 | [diff] [blame] | 4068 | const char *name, int name_len) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4069 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4070 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4071 | struct btrfs_path *path; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4072 | int ret = 0; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4073 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4074 | struct btrfs_dir_item *di; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4075 | struct btrfs_key key; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4076 | u64 index; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4077 | u64 ino = btrfs_ino(inode); |
| 4078 | u64 dir_ino = btrfs_ino(dir); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4079 | |
| 4080 | path = btrfs_alloc_path(); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 4081 | if (!path) { |
| 4082 | ret = -ENOMEM; |
Tsutomu Itoh | 554233a | 2011-02-03 03:16:25 +0000 | [diff] [blame] | 4083 | goto out; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 4084 | } |
| 4085 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 4086 | path->leave_spinning = 1; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4087 | di = btrfs_lookup_dir_item(trans, root, path, dir_ino, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4088 | name, name_len, -1); |
| 4089 | if (IS_ERR(di)) { |
| 4090 | ret = PTR_ERR(di); |
| 4091 | goto err; |
| 4092 | } |
| 4093 | if (!di) { |
| 4094 | ret = -ENOENT; |
| 4095 | goto err; |
| 4096 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4097 | leaf = path->nodes[0]; |
| 4098 | btrfs_dir_item_key_to_cpu(leaf, di, &key); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4099 | ret = btrfs_delete_one_dir_name(trans, root, path, di); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 4100 | if (ret) |
| 4101 | goto err; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4102 | btrfs_release_path(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4103 | |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 4104 | /* |
| 4105 | * If we don't have dir index, we have to get it by looking up |
| 4106 | * the inode ref, since we get the inode ref, remove it directly, |
| 4107 | * it is unnecessary to do delayed deletion. |
| 4108 | * |
| 4109 | * But if we have dir index, needn't search inode ref to get it. |
| 4110 | * Since the inode ref is close to the inode item, it is better |
| 4111 | * that we delay to delete it, and just do this deletion when |
| 4112 | * we update the inode item. |
| 4113 | */ |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 4114 | if (inode->dir_index) { |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 4115 | ret = btrfs_delayed_delete_inode_ref(inode); |
| 4116 | if (!ret) { |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 4117 | index = inode->dir_index; |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 4118 | goto skip_backref; |
| 4119 | } |
| 4120 | } |
| 4121 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4122 | ret = btrfs_del_inode_ref(trans, root, name, name_len, ino, |
| 4123 | dir_ino, &index); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4124 | if (ret) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4125 | btrfs_info(fs_info, |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 4126 | "failed to delete reference to %.*s, inode %llu parent %llu", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 4127 | name_len, name, ino, dir_ino); |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 4128 | btrfs_abort_transaction(trans, ret); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4129 | goto err; |
| 4130 | } |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 4131 | skip_backref: |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4132 | ret = btrfs_delete_delayed_dir_index(trans, fs_info, dir, index); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4133 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 4134 | btrfs_abort_transaction(trans, ret); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4135 | goto err; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4136 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4137 | |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 4138 | ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len, inode, |
| 4139 | dir_ino); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4140 | if (ret != 0 && ret != -ENOENT) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 4141 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4142 | goto err; |
| 4143 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4144 | |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 4145 | ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len, dir, |
| 4146 | index); |
Chris Mason | 6418c96 | 2010-10-30 07:34:24 -0400 | [diff] [blame] | 4147 | if (ret == -ENOENT) |
| 4148 | ret = 0; |
Zach Brown | d4e3991 | 2013-04-02 21:02:16 +0000 | [diff] [blame] | 4149 | else if (ret) |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 4150 | btrfs_abort_transaction(trans, ret); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4151 | err: |
| 4152 | btrfs_free_path(path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4153 | if (ret) |
| 4154 | goto out; |
| 4155 | |
Nikolay Borisov | 6ef06d2 | 2017-02-20 13:50:34 +0200 | [diff] [blame] | 4156 | 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] | 4157 | inode_inc_iversion(&inode->vfs_inode); |
| 4158 | inode_inc_iversion(&dir->vfs_inode); |
| 4159 | inode->vfs_inode.i_ctime = dir->vfs_inode.i_mtime = |
| 4160 | dir->vfs_inode.i_ctime = current_time(&inode->vfs_inode); |
| 4161 | ret = btrfs_update_inode(trans, root, &dir->vfs_inode); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4162 | out: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4163 | return ret; |
| 4164 | } |
| 4165 | |
Al Viro | 9298679 | 2011-03-04 17:14:37 +0000 | [diff] [blame] | 4166 | int btrfs_unlink_inode(struct btrfs_trans_handle *trans, |
| 4167 | struct btrfs_root *root, |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 4168 | struct btrfs_inode *dir, struct btrfs_inode *inode, |
Al Viro | 9298679 | 2011-03-04 17:14:37 +0000 | [diff] [blame] | 4169 | const char *name, int name_len) |
| 4170 | { |
| 4171 | int ret; |
| 4172 | ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len); |
| 4173 | if (!ret) { |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 4174 | drop_nlink(&inode->vfs_inode); |
| 4175 | ret = btrfs_update_inode(trans, root, &inode->vfs_inode); |
Al Viro | 9298679 | 2011-03-04 17:14:37 +0000 | [diff] [blame] | 4176 | } |
| 4177 | return ret; |
| 4178 | } |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4179 | |
| 4180 | /* |
| 4181 | * helper to start transaction for unlink and rmdir. |
| 4182 | * |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 4183 | * unlink and rmdir are special in btrfs, they do not always free space, so |
| 4184 | * if we cannot make our reservations the normal way try and see if there is |
| 4185 | * plenty of slack room in the global reserve to migrate, otherwise we cannot |
| 4186 | * allow the unlink to occur. |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4187 | */ |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 4188 | static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir) |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4189 | { |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4190 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4191 | |
Josef Bacik | e70bea5 | 2011-10-11 14:18:24 -0400 | [diff] [blame] | 4192 | /* |
| 4193 | * 1 for the possible orphan item |
| 4194 | * 1 for the dir item |
| 4195 | * 1 for the dir index |
| 4196 | * 1 for the inode ref |
Josef Bacik | e70bea5 | 2011-10-11 14:18:24 -0400 | [diff] [blame] | 4197 | * 1 for the inode |
| 4198 | */ |
Filipe Manana | 8eab77f | 2015-11-13 23:57:16 +0000 | [diff] [blame] | 4199 | return btrfs_start_transaction_fallback_global_rsv(root, 5, 5); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4200 | } |
| 4201 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4202 | static int btrfs_unlink(struct inode *dir, struct dentry *dentry) |
| 4203 | { |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4204 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4205 | struct btrfs_trans_handle *trans; |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 4206 | struct inode *inode = d_inode(dentry); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4207 | int ret; |
| 4208 | |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 4209 | trans = __unlink_start_trans(dir); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4210 | if (IS_ERR(trans)) |
Josef Bacik | 5df6a9f | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4211 | return PTR_ERR(trans); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4212 | |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 4213 | btrfs_record_unlink_dir(trans, BTRFS_I(dir), BTRFS_I(d_inode(dentry)), |
| 4214 | 0); |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 4215 | |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 4216 | ret = btrfs_unlink_inode(trans, root, BTRFS_I(dir), |
| 4217 | BTRFS_I(d_inode(dentry)), dentry->d_name.name, |
| 4218 | dentry->d_name.len); |
Tsutomu Itoh | b532402 | 2011-07-19 07:27:20 +0000 | [diff] [blame] | 4219 | if (ret) |
| 4220 | goto out; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 4221 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4222 | if (inode->i_nlink == 0) { |
Nikolay Borisov | 73f2e54 | 2017-02-20 13:50:59 +0200 | [diff] [blame] | 4223 | ret = btrfs_orphan_add(trans, BTRFS_I(inode)); |
Tsutomu Itoh | b532402 | 2011-07-19 07:27:20 +0000 | [diff] [blame] | 4224 | if (ret) |
| 4225 | goto out; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4226 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 4227 | |
Tsutomu Itoh | b532402 | 2011-07-19 07:27:20 +0000 | [diff] [blame] | 4228 | out: |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 4229 | btrfs_end_transaction(trans); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4230 | btrfs_btree_balance_dirty(root->fs_info); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4231 | return ret; |
| 4232 | } |
| 4233 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4234 | int btrfs_unlink_subvol(struct btrfs_trans_handle *trans, |
| 4235 | struct btrfs_root *root, |
| 4236 | struct inode *dir, u64 objectid, |
| 4237 | const char *name, int name_len) |
| 4238 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4239 | struct btrfs_fs_info *fs_info = root->fs_info; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4240 | struct btrfs_path *path; |
| 4241 | struct extent_buffer *leaf; |
| 4242 | struct btrfs_dir_item *di; |
| 4243 | struct btrfs_key key; |
| 4244 | u64 index; |
| 4245 | int ret; |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 4246 | u64 dir_ino = btrfs_ino(BTRFS_I(dir)); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4247 | |
| 4248 | path = btrfs_alloc_path(); |
| 4249 | if (!path) |
| 4250 | return -ENOMEM; |
| 4251 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4252 | di = btrfs_lookup_dir_item(trans, root, path, dir_ino, |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4253 | name, name_len, -1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4254 | if (IS_ERR_OR_NULL(di)) { |
| 4255 | if (!di) |
| 4256 | ret = -ENOENT; |
| 4257 | else |
| 4258 | ret = PTR_ERR(di); |
| 4259 | goto out; |
| 4260 | } |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4261 | |
| 4262 | leaf = path->nodes[0]; |
| 4263 | btrfs_dir_item_key_to_cpu(leaf, di, &key); |
| 4264 | WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid); |
| 4265 | ret = btrfs_delete_one_dir_name(trans, root, path, di); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4266 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 4267 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4268 | goto out; |
| 4269 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4270 | btrfs_release_path(path); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4271 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4272 | ret = btrfs_del_root_ref(trans, fs_info, objectid, |
| 4273 | root->root_key.objectid, dir_ino, |
| 4274 | &index, name, name_len); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4275 | if (ret < 0) { |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4276 | if (ret != -ENOENT) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 4277 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4278 | goto out; |
| 4279 | } |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4280 | di = btrfs_search_dir_index_item(root, path, dir_ino, |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4281 | name, name_len); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4282 | if (IS_ERR_OR_NULL(di)) { |
| 4283 | if (!di) |
| 4284 | ret = -ENOENT; |
| 4285 | else |
| 4286 | ret = PTR_ERR(di); |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 4287 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4288 | goto out; |
| 4289 | } |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4290 | |
| 4291 | leaf = path->nodes[0]; |
| 4292 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4293 | btrfs_release_path(path); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4294 | index = key.offset; |
| 4295 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4296 | btrfs_release_path(path); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4297 | |
Nikolay Borisov | e67bbbb | 2017-01-10 20:35:36 +0200 | [diff] [blame] | 4298 | 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] | 4299 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 4300 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4301 | goto out; |
| 4302 | } |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 4303 | |
Nikolay Borisov | 6ef06d2 | 2017-02-20 13:50:34 +0200 | [diff] [blame] | 4304 | 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] | 4305 | inode_inc_iversion(dir); |
Deepa Dinamani | c2050a4 | 2016-09-14 07:48:06 -0700 | [diff] [blame] | 4306 | dir->i_mtime = dir->i_ctime = current_time(dir); |
Josef Bacik | 5a24e84 | 2012-08-08 10:12:59 -0600 | [diff] [blame] | 4307 | ret = btrfs_update_inode_fallback(trans, root, dir); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4308 | if (ret) |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 4309 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4310 | out: |
Josef Bacik | 71d7aed | 2011-06-14 14:24:32 -0400 | [diff] [blame] | 4311 | btrfs_free_path(path); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4312 | return ret; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4313 | } |
| 4314 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4315 | static int btrfs_rmdir(struct inode *dir, struct dentry *dentry) |
| 4316 | { |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 4317 | struct inode *inode = d_inode(dentry); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 4318 | int err = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4319 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4320 | struct btrfs_trans_handle *trans; |
Filipe Manana | 44f714d | 2016-06-06 16:11:13 +0100 | [diff] [blame] | 4321 | u64 last_unlink_trans; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4322 | |
David Sterba | b3ae244 | 2012-09-13 16:04:34 -0600 | [diff] [blame] | 4323 | if (inode->i_size > BTRFS_EMPTY_DIR_SIZE) |
Yan | 134d451 | 2007-10-25 15:49:25 -0400 | [diff] [blame] | 4324 | return -ENOTEMPTY; |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 4325 | if (btrfs_ino(BTRFS_I(inode)) == BTRFS_FIRST_FREE_OBJECTID) |
David Sterba | b3ae244 | 2012-09-13 16:04:34 -0600 | [diff] [blame] | 4326 | return -EPERM; |
Yan | 134d451 | 2007-10-25 15:49:25 -0400 | [diff] [blame] | 4327 | |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 4328 | trans = __unlink_start_trans(dir); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4329 | if (IS_ERR(trans)) |
Josef Bacik | 5df6a9f | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4330 | return PTR_ERR(trans); |
Josef Bacik | 5df6a9f | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4331 | |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 4332 | if (unlikely(btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4333 | err = btrfs_unlink_subvol(trans, root, dir, |
| 4334 | BTRFS_I(inode)->location.objectid, |
| 4335 | dentry->d_name.name, |
| 4336 | dentry->d_name.len); |
| 4337 | goto out; |
| 4338 | } |
| 4339 | |
Nikolay Borisov | 73f2e54 | 2017-02-20 13:50:59 +0200 | [diff] [blame] | 4340 | err = btrfs_orphan_add(trans, BTRFS_I(inode)); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 4341 | if (err) |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4342 | goto out; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 4343 | |
Filipe Manana | 44f714d | 2016-06-06 16:11:13 +0100 | [diff] [blame] | 4344 | last_unlink_trans = BTRFS_I(inode)->last_unlink_trans; |
| 4345 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4346 | /* now the directory is empty */ |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 4347 | err = btrfs_unlink_inode(trans, root, BTRFS_I(dir), |
| 4348 | BTRFS_I(d_inode(dentry)), dentry->d_name.name, |
| 4349 | dentry->d_name.len); |
Filipe Manana | 44f714d | 2016-06-06 16:11:13 +0100 | [diff] [blame] | 4350 | if (!err) { |
Nikolay Borisov | 6ef06d2 | 2017-02-20 13:50:34 +0200 | [diff] [blame] | 4351 | btrfs_i_size_write(BTRFS_I(inode), 0); |
Filipe Manana | 44f714d | 2016-06-06 16:11:13 +0100 | [diff] [blame] | 4352 | /* |
| 4353 | * Propagate the last_unlink_trans value of the deleted dir to |
| 4354 | * its parent directory. This is to prevent an unrecoverable |
| 4355 | * log tree in the case we do something like this: |
| 4356 | * 1) create dir foo |
| 4357 | * 2) create snapshot under dir foo |
| 4358 | * 3) delete the snapshot |
| 4359 | * 4) rmdir foo |
| 4360 | * 5) mkdir foo |
| 4361 | * 6) fsync foo or some file inside foo |
| 4362 | */ |
| 4363 | if (last_unlink_trans >= trans->transid) |
| 4364 | BTRFS_I(dir)->last_unlink_trans = last_unlink_trans; |
| 4365 | } |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4366 | out: |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 4367 | btrfs_end_transaction(trans); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4368 | btrfs_btree_balance_dirty(root->fs_info); |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 4369 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4370 | return err; |
| 4371 | } |
| 4372 | |
Chris Mason | 28f75a0 | 2015-02-04 06:59:29 -0800 | [diff] [blame] | 4373 | static int truncate_space_check(struct btrfs_trans_handle *trans, |
| 4374 | struct btrfs_root *root, |
| 4375 | u64 bytes_deleted) |
| 4376 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4377 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | 28f75a0 | 2015-02-04 06:59:29 -0800 | [diff] [blame] | 4378 | int ret; |
| 4379 | |
Josef Bacik | dc95f7b | 2016-01-13 11:48:06 -0500 | [diff] [blame] | 4380 | /* |
| 4381 | * This is only used to apply pressure to the enospc system, we don't |
| 4382 | * intend to use this reservation at all. |
| 4383 | */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4384 | bytes_deleted = btrfs_csum_bytes_to_leaves(fs_info, bytes_deleted); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4385 | bytes_deleted *= fs_info->nodesize; |
| 4386 | ret = btrfs_block_rsv_add(root, &fs_info->trans_block_rsv, |
Chris Mason | 28f75a0 | 2015-02-04 06:59:29 -0800 | [diff] [blame] | 4387 | bytes_deleted, BTRFS_RESERVE_NO_FLUSH); |
Josef Bacik | dc95f7b | 2016-01-13 11:48:06 -0500 | [diff] [blame] | 4388 | if (!ret) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4389 | trace_btrfs_space_reservation(fs_info, "transaction", |
Josef Bacik | dc95f7b | 2016-01-13 11:48:06 -0500 | [diff] [blame] | 4390 | trans->transid, |
| 4391 | bytes_deleted, 1); |
Chris Mason | 28f75a0 | 2015-02-04 06:59:29 -0800 | [diff] [blame] | 4392 | trans->bytes_reserved += bytes_deleted; |
Josef Bacik | dc95f7b | 2016-01-13 11:48:06 -0500 | [diff] [blame] | 4393 | } |
Chris Mason | 28f75a0 | 2015-02-04 06:59:29 -0800 | [diff] [blame] | 4394 | return ret; |
| 4395 | |
| 4396 | } |
| 4397 | |
Josef Bacik | ddfae63 | 2017-10-19 14:16:02 -0400 | [diff] [blame] | 4398 | /* |
| 4399 | * Return this if we need to call truncate_block for the last bit of the |
| 4400 | * truncate. |
| 4401 | */ |
| 4402 | #define NEED_TRUNCATE_BLOCK 1 |
Filipe Manana | 0305cd5 | 2015-10-16 12:34:25 +0100 | [diff] [blame] | 4403 | |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 4404 | /* |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4405 | * this can truncate away extent items, csum items and directory items. |
| 4406 | * 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] | 4407 | * any higher than new_size |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4408 | * |
| 4409 | * csum items that cross the new i_size are truncated to the new size |
| 4410 | * as well. |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 4411 | * |
| 4412 | * min_type is the minimum key type to truncate down to. If set to 0, this |
| 4413 | * 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] | 4414 | */ |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4415 | int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans, |
| 4416 | struct btrfs_root *root, |
| 4417 | struct inode *inode, |
| 4418 | u64 new_size, u32 min_type) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4419 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4420 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4421 | struct btrfs_path *path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4422 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4423 | struct btrfs_file_extent_item *fi; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4424 | struct btrfs_key key; |
| 4425 | struct btrfs_key found_key; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4426 | u64 extent_start = 0; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4427 | u64 extent_num_bytes = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4428 | u64 extent_offset = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4429 | u64 item_end = 0; |
Filipe Manana | c1aa457 | 2015-06-20 18:20:09 +0100 | [diff] [blame] | 4430 | u64 last_size = new_size; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4431 | u32 found_type = (u8)-1; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4432 | int found_extent; |
| 4433 | int del_item; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4434 | int pending_del_nr = 0; |
| 4435 | int pending_del_slot = 0; |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4436 | int extent_type = -1; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4437 | int ret; |
| 4438 | int err = 0; |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 4439 | u64 ino = btrfs_ino(BTRFS_I(inode)); |
Chris Mason | 28ed134 | 2014-12-17 09:41:04 -0800 | [diff] [blame] | 4440 | u64 bytes_deleted = 0; |
Thomas Meyer | 897ca81 | 2017-10-07 16:02:21 +0200 | [diff] [blame] | 4441 | bool be_nice = false; |
| 4442 | bool should_throttle = false; |
| 4443 | bool should_end = false; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4444 | |
| 4445 | BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4446 | |
Chris Mason | 28ed134 | 2014-12-17 09:41:04 -0800 | [diff] [blame] | 4447 | /* |
| 4448 | * for non-free space inodes and ref cows, we want to back off from |
| 4449 | * time to time |
| 4450 | */ |
Nikolay Borisov | 70ddc55 | 2017-02-20 13:50:35 +0200 | [diff] [blame] | 4451 | if (!btrfs_is_free_space_inode(BTRFS_I(inode)) && |
Chris Mason | 28ed134 | 2014-12-17 09:41:04 -0800 | [diff] [blame] | 4452 | test_bit(BTRFS_ROOT_REF_COWS, &root->state)) |
Thomas Meyer | 897ca81 | 2017-10-07 16:02:21 +0200 | [diff] [blame] | 4453 | be_nice = true; |
Chris Mason | 28ed134 | 2014-12-17 09:41:04 -0800 | [diff] [blame] | 4454 | |
Mark Fasheh | 0eb0e19 | 2011-07-12 16:44:10 -0700 | [diff] [blame] | 4455 | path = btrfs_alloc_path(); |
| 4456 | if (!path) |
| 4457 | return -ENOMEM; |
David Sterba | e4058b5 | 2015-11-27 16:31:35 +0100 | [diff] [blame] | 4458 | path->reada = READA_BACK; |
Mark Fasheh | 0eb0e19 | 2011-07-12 16:44:10 -0700 | [diff] [blame] | 4459 | |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4460 | /* |
| 4461 | * We want to drop from the next block forward in case this new size is |
| 4462 | * not block aligned since we will be keeping the last block of the |
| 4463 | * extent just the way it is. |
| 4464 | */ |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 4465 | if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) || |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4466 | root == fs_info->tree_root) |
Nikolay Borisov | dcdbc05 | 2017-02-20 13:50:45 +0200 | [diff] [blame] | 4467 | btrfs_drop_extent_cache(BTRFS_I(inode), ALIGN(new_size, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4468 | fs_info->sectorsize), |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 4469 | (u64)-1, 0); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4470 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 4471 | /* |
| 4472 | * This function is also used to drop the items in the log tree before |
| 4473 | * we relog the inode, so if root != BTRFS_I(inode)->root, it means |
| 4474 | * it is used to drop the loged items. So we shouldn't kill the delayed |
| 4475 | * items. |
| 4476 | */ |
| 4477 | if (min_type == 0 && root == BTRFS_I(inode)->root) |
Nikolay Borisov | 4ccb5c7 | 2017-01-10 20:35:38 +0200 | [diff] [blame] | 4478 | btrfs_kill_delayed_inode_items(BTRFS_I(inode)); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 4479 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4480 | key.objectid = ino; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4481 | key.offset = (u64)-1; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4482 | key.type = (u8)-1; |
| 4483 | |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4484 | search_again: |
Chris Mason | 28ed134 | 2014-12-17 09:41:04 -0800 | [diff] [blame] | 4485 | /* |
| 4486 | * with a 16K leaf size and 128MB extents, you can actually queue |
| 4487 | * up a huge file in a single leaf. Most of the time that |
| 4488 | * bytes_deleted is > 0, it will be huge by the time we get here |
| 4489 | */ |
Byongho Lee | ee22184 | 2015-12-15 01:42:10 +0900 | [diff] [blame] | 4490 | if (be_nice && bytes_deleted > SZ_32M) { |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 4491 | if (btrfs_should_end_transaction(trans)) { |
Chris Mason | 28ed134 | 2014-12-17 09:41:04 -0800 | [diff] [blame] | 4492 | err = -EAGAIN; |
| 4493 | goto error; |
| 4494 | } |
| 4495 | } |
| 4496 | |
| 4497 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 4498 | path->leave_spinning = 1; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4499 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4500 | if (ret < 0) { |
| 4501 | err = ret; |
| 4502 | goto out; |
| 4503 | } |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4504 | |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4505 | if (ret > 0) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4506 | /* there are no items in the tree for us to truncate, we're |
| 4507 | * done |
| 4508 | */ |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4509 | if (path->slots[0] == 0) |
| 4510 | goto out; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4511 | path->slots[0]--; |
| 4512 | } |
| 4513 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4514 | while (1) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4515 | fi = NULL; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4516 | leaf = path->nodes[0]; |
| 4517 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 4518 | found_type = found_key.type; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4519 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4520 | if (found_key.objectid != ino) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4521 | break; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4522 | |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4523 | if (found_type < min_type) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4524 | break; |
| 4525 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4526 | item_end = found_key.offset; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4527 | if (found_type == BTRFS_EXTENT_DATA_KEY) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4528 | fi = btrfs_item_ptr(leaf, path->slots[0], |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4529 | struct btrfs_file_extent_item); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4530 | extent_type = btrfs_file_extent_type(leaf, fi); |
| 4531 | if (extent_type != BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4532 | item_end += |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4533 | btrfs_file_extent_num_bytes(leaf, fi); |
Liu Bo | 09ed2f1 | 2017-03-10 11:09:48 -0800 | [diff] [blame] | 4534 | |
| 4535 | trace_btrfs_truncate_show_fi_regular( |
| 4536 | BTRFS_I(inode), leaf, fi, |
| 4537 | found_key.offset); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4538 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4539 | item_end += btrfs_file_extent_inline_len(leaf, |
Chris Mason | 514ac8a | 2014-01-03 21:07:00 -0800 | [diff] [blame] | 4540 | path->slots[0], fi); |
Liu Bo | 09ed2f1 | 2017-03-10 11:09:48 -0800 | [diff] [blame] | 4541 | |
| 4542 | trace_btrfs_truncate_show_fi_inline( |
| 4543 | BTRFS_I(inode), leaf, fi, path->slots[0], |
| 4544 | found_key.offset); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4545 | } |
Yan | 008630c | 2007-11-07 13:31:09 -0500 | [diff] [blame] | 4546 | item_end--; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4547 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4548 | if (found_type > min_type) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4549 | del_item = 1; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4550 | } else { |
Filipe Manana | 76b42ab | 2017-02-14 16:56:01 +0000 | [diff] [blame] | 4551 | if (item_end < new_size) |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4552 | break; |
| 4553 | if (found_key.offset >= new_size) |
| 4554 | del_item = 1; |
| 4555 | else |
| 4556 | del_item = 0; |
| 4557 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4558 | found_extent = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4559 | /* FIXME, shrink the extent if the ref count is only 1 */ |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4560 | if (found_type != BTRFS_EXTENT_DATA_KEY) |
| 4561 | goto delete; |
| 4562 | |
| 4563 | if (extent_type != BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4564 | u64 num_dec; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4565 | extent_start = btrfs_file_extent_disk_bytenr(leaf, fi); |
Miao Xie | f70a9a6b | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 4566 | if (!del_item) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4567 | u64 orig_num_bytes = |
| 4568 | btrfs_file_extent_num_bytes(leaf, fi); |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 4569 | extent_num_bytes = ALIGN(new_size - |
| 4570 | found_key.offset, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4571 | fs_info->sectorsize); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4572 | btrfs_set_file_extent_num_bytes(leaf, fi, |
| 4573 | extent_num_bytes); |
| 4574 | num_dec = (orig_num_bytes - |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 4575 | extent_num_bytes); |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 4576 | if (test_bit(BTRFS_ROOT_REF_COWS, |
| 4577 | &root->state) && |
| 4578 | extent_start != 0) |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 4579 | inode_sub_bytes(inode, num_dec); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4580 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4581 | } else { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4582 | extent_num_bytes = |
| 4583 | btrfs_file_extent_disk_num_bytes(leaf, |
| 4584 | fi); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4585 | extent_offset = found_key.offset - |
| 4586 | btrfs_file_extent_offset(leaf, fi); |
| 4587 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4588 | /* FIXME blocksize != 4096 */ |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 4589 | num_dec = btrfs_file_extent_num_bytes(leaf, fi); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4590 | if (extent_start != 0) { |
| 4591 | found_extent = 1; |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 4592 | if (test_bit(BTRFS_ROOT_REF_COWS, |
| 4593 | &root->state)) |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 4594 | inode_sub_bytes(inode, num_dec); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4595 | } |
| 4596 | } |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 4597 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4598 | /* |
| 4599 | * we can't truncate inline items that have had |
| 4600 | * special encodings |
| 4601 | */ |
| 4602 | if (!del_item && |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4603 | btrfs_file_extent_encryption(leaf, fi) == 0 && |
Josef Bacik | ddfae63 | 2017-10-19 14:16:02 -0400 | [diff] [blame] | 4604 | btrfs_file_extent_other_encoding(leaf, fi) == 0 && |
| 4605 | btrfs_file_extent_compression(leaf, fi) == 0) { |
| 4606 | u32 size = (u32)(new_size - found_key.offset); |
Chris Mason | 514ac8a | 2014-01-03 21:07:00 -0800 | [diff] [blame] | 4607 | |
Josef Bacik | ddfae63 | 2017-10-19 14:16:02 -0400 | [diff] [blame] | 4608 | btrfs_set_file_extent_ram_bytes(leaf, fi, size); |
| 4609 | size = btrfs_file_extent_calc_inline_size(size); |
| 4610 | btrfs_truncate_item(root->fs_info, path, size, 1); |
| 4611 | } else if (!del_item) { |
Chris Mason | 514ac8a | 2014-01-03 21:07:00 -0800 | [diff] [blame] | 4612 | /* |
Josef Bacik | ddfae63 | 2017-10-19 14:16:02 -0400 | [diff] [blame] | 4613 | * We have to bail so the last_size is set to |
| 4614 | * just before this extent. |
Chris Mason | 514ac8a | 2014-01-03 21:07:00 -0800 | [diff] [blame] | 4615 | */ |
Josef Bacik | ddfae63 | 2017-10-19 14:16:02 -0400 | [diff] [blame] | 4616 | err = NEED_TRUNCATE_BLOCK; |
| 4617 | break; |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 4618 | } |
Josef Bacik | ddfae63 | 2017-10-19 14:16:02 -0400 | [diff] [blame] | 4619 | |
| 4620 | if (test_bit(BTRFS_ROOT_REF_COWS, &root->state)) |
| 4621 | inode_sub_bytes(inode, item_end + 1 - new_size); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4622 | } |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4623 | delete: |
Josef Bacik | ddfae63 | 2017-10-19 14:16:02 -0400 | [diff] [blame] | 4624 | if (del_item) |
| 4625 | last_size = found_key.offset; |
| 4626 | else |
| 4627 | last_size = new_size; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4628 | if (del_item) { |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4629 | if (!pending_del_nr) { |
| 4630 | /* no pending yet, add ourselves */ |
| 4631 | pending_del_slot = path->slots[0]; |
| 4632 | pending_del_nr = 1; |
| 4633 | } else if (pending_del_nr && |
| 4634 | path->slots[0] + 1 == pending_del_slot) { |
| 4635 | /* hop on the pending chunk */ |
| 4636 | pending_del_nr++; |
| 4637 | pending_del_slot = path->slots[0]; |
| 4638 | } else { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4639 | BUG(); |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4640 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4641 | } else { |
| 4642 | break; |
| 4643 | } |
Thomas Meyer | 897ca81 | 2017-10-07 16:02:21 +0200 | [diff] [blame] | 4644 | should_throttle = false; |
Chris Mason | 28f75a0 | 2015-02-04 06:59:29 -0800 | [diff] [blame] | 4645 | |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 4646 | if (found_extent && |
| 4647 | (test_bit(BTRFS_ROOT_REF_COWS, &root->state) || |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4648 | root == fs_info->tree_root)) { |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 4649 | btrfs_set_path_blocking(path); |
Chris Mason | 28ed134 | 2014-12-17 09:41:04 -0800 | [diff] [blame] | 4650 | bytes_deleted += extent_num_bytes; |
Josef Bacik | 84f7d8e | 2017-09-29 15:43:49 -0400 | [diff] [blame] | 4651 | ret = btrfs_free_extent(trans, root, extent_start, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4652 | extent_num_bytes, 0, |
| 4653 | btrfs_header_owner(leaf), |
Filipe Manana | b06c4bf | 2015-10-23 07:52:54 +0100 | [diff] [blame] | 4654 | ino, extent_offset); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4655 | BUG_ON(ret); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4656 | if (btrfs_should_throttle_delayed_refs(trans, fs_info)) |
| 4657 | btrfs_async_run_delayed_refs(fs_info, |
Wang Xiaoguang | dd4b857 | 2016-10-18 15:56:13 +0800 | [diff] [blame] | 4658 | trans->delayed_ref_updates * 2, |
| 4659 | trans->transid, 0); |
Chris Mason | 28f75a0 | 2015-02-04 06:59:29 -0800 | [diff] [blame] | 4660 | if (be_nice) { |
| 4661 | if (truncate_space_check(trans, root, |
| 4662 | extent_num_bytes)) { |
Thomas Meyer | 897ca81 | 2017-10-07 16:02:21 +0200 | [diff] [blame] | 4663 | should_end = true; |
Chris Mason | 28f75a0 | 2015-02-04 06:59:29 -0800 | [diff] [blame] | 4664 | } |
| 4665 | if (btrfs_should_throttle_delayed_refs(trans, |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4666 | fs_info)) |
Thomas Meyer | 897ca81 | 2017-10-07 16:02:21 +0200 | [diff] [blame] | 4667 | should_throttle = true; |
Chris Mason | 28f75a0 | 2015-02-04 06:59:29 -0800 | [diff] [blame] | 4668 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4669 | } |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4670 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4671 | if (found_type == BTRFS_INODE_ITEM_KEY) |
| 4672 | break; |
| 4673 | |
| 4674 | if (path->slots[0] == 0 || |
Josef Bacik | 1262133 | 2015-02-03 07:50:16 -0800 | [diff] [blame] | 4675 | path->slots[0] != pending_del_slot || |
Chris Mason | 28f75a0 | 2015-02-04 06:59:29 -0800 | [diff] [blame] | 4676 | should_throttle || should_end) { |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4677 | if (pending_del_nr) { |
| 4678 | ret = btrfs_del_items(trans, root, path, |
| 4679 | pending_del_slot, |
| 4680 | pending_del_nr); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4681 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 4682 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4683 | goto error; |
| 4684 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4685 | pending_del_nr = 0; |
| 4686 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4687 | btrfs_release_path(path); |
Chris Mason | 28f75a0 | 2015-02-04 06:59:29 -0800 | [diff] [blame] | 4688 | if (should_throttle) { |
Josef Bacik | 1262133 | 2015-02-03 07:50:16 -0800 | [diff] [blame] | 4689 | unsigned long updates = trans->delayed_ref_updates; |
| 4690 | if (updates) { |
| 4691 | trans->delayed_ref_updates = 0; |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4692 | ret = btrfs_run_delayed_refs(trans, |
| 4693 | fs_info, |
| 4694 | updates * 2); |
Josef Bacik | 1262133 | 2015-02-03 07:50:16 -0800 | [diff] [blame] | 4695 | if (ret && !err) |
| 4696 | err = ret; |
| 4697 | } |
| 4698 | } |
Chris Mason | 28f75a0 | 2015-02-04 06:59:29 -0800 | [diff] [blame] | 4699 | /* |
| 4700 | * if we failed to refill our space rsv, bail out |
| 4701 | * and let the transaction restart |
| 4702 | */ |
| 4703 | if (should_end) { |
| 4704 | err = -EAGAIN; |
| 4705 | goto error; |
| 4706 | } |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4707 | goto search_again; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4708 | } else { |
| 4709 | path->slots[0]--; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4710 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4711 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4712 | out: |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4713 | if (pending_del_nr) { |
| 4714 | ret = btrfs_del_items(trans, root, path, pending_del_slot, |
| 4715 | pending_del_nr); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4716 | if (ret) |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 4717 | btrfs_abort_transaction(trans, ret); |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4718 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4719 | error: |
Filipe Manana | 76b42ab | 2017-02-14 16:56:01 +0000 | [diff] [blame] | 4720 | if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) { |
| 4721 | ASSERT(last_size >= new_size); |
| 4722 | if (!err && last_size > new_size) |
| 4723 | last_size = new_size; |
Josef Bacik | 7f4f6e0 | 2013-08-29 16:43:28 -0400 | [diff] [blame] | 4724 | btrfs_ordered_update_i_size(inode, last_size, NULL); |
Filipe Manana | 76b42ab | 2017-02-14 16:56:01 +0000 | [diff] [blame] | 4725 | } |
Chris Mason | 28ed134 | 2014-12-17 09:41:04 -0800 | [diff] [blame] | 4726 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4727 | btrfs_free_path(path); |
Chris Mason | 28ed134 | 2014-12-17 09:41:04 -0800 | [diff] [blame] | 4728 | |
Byongho Lee | ee22184 | 2015-12-15 01:42:10 +0900 | [diff] [blame] | 4729 | if (be_nice && bytes_deleted > SZ_32M) { |
Chris Mason | 28ed134 | 2014-12-17 09:41:04 -0800 | [diff] [blame] | 4730 | unsigned long updates = trans->delayed_ref_updates; |
| 4731 | if (updates) { |
| 4732 | trans->delayed_ref_updates = 0; |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4733 | ret = btrfs_run_delayed_refs(trans, fs_info, |
| 4734 | updates * 2); |
Chris Mason | 28ed134 | 2014-12-17 09:41:04 -0800 | [diff] [blame] | 4735 | if (ret && !err) |
| 4736 | err = ret; |
| 4737 | } |
| 4738 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4739 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4740 | } |
| 4741 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4742 | /* |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4743 | * btrfs_truncate_block - read, zero a chunk and write a block |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 4744 | * @inode - inode that we're zeroing |
| 4745 | * @from - the offset to start zeroing |
| 4746 | * @len - the length to zero, 0 to zero the entire range respective to the |
| 4747 | * offset |
| 4748 | * @front - zero up to the offset instead of from the offset on |
| 4749 | * |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4750 | * 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] | 4751 | * 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] | 4752 | */ |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4753 | 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] | 4754 | int front) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4755 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4756 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 4757 | struct address_space *mapping = inode->i_mapping; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4758 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
| 4759 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4760 | struct extent_state *cached_state = NULL; |
Qu Wenruo | 364ecf3 | 2017-02-27 15:10:38 +0800 | [diff] [blame] | 4761 | struct extent_changeset *data_reserved = NULL; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4762 | char *kaddr; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4763 | u32 blocksize = fs_info->sectorsize; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 4764 | pgoff_t index = from >> PAGE_SHIFT; |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4765 | unsigned offset = from & (blocksize - 1); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4766 | struct page *page; |
Josef Bacik | 3b16a4e | 2011-09-21 15:05:58 -0400 | [diff] [blame] | 4767 | gfp_t mask = btrfs_alloc_write_mask(mapping); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4768 | int ret = 0; |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4769 | u64 block_start; |
| 4770 | u64 block_end; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4771 | |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 4772 | if ((offset & (blocksize - 1)) == 0 && |
| 4773 | (!len || ((len & (blocksize - 1)) == 0))) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4774 | goto out; |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4775 | |
Josef Bacik | 8b62f87 | 2017-10-19 14:15:55 -0400 | [diff] [blame] | 4776 | block_start = round_down(from, blocksize); |
| 4777 | block_end = block_start + blocksize - 1; |
| 4778 | |
Qu Wenruo | 364ecf3 | 2017-02-27 15:10:38 +0800 | [diff] [blame] | 4779 | ret = btrfs_delalloc_reserve_space(inode, &data_reserved, |
Josef Bacik | 8b62f87 | 2017-10-19 14:15:55 -0400 | [diff] [blame] | 4780 | block_start, 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 | bc42bda | 2017-02-27 15:10:39 +0800 | [diff] [blame] | 4787 | btrfs_delalloc_release_space(inode, data_reserved, |
Josef Bacik | 8b62f87 | 2017-10-19 14:15:55 -0400 | [diff] [blame] | 4788 | block_start, blocksize); |
| 4789 | btrfs_delalloc_release_extents(BTRFS_I(inode), 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 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4794 | if (!PageUptodate(page)) { |
| 4795 | ret = btrfs_readpage(NULL, page); |
| 4796 | lock_page(page); |
Chris Mason | 211c17f | 2008-05-15 09:13:45 -0400 | [diff] [blame] | 4797 | if (page->mapping != mapping) { |
| 4798 | unlock_page(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 4799 | put_page(page); |
Chris Mason | 211c17f | 2008-05-15 09:13:45 -0400 | [diff] [blame] | 4800 | goto again; |
| 4801 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4802 | if (!PageUptodate(page)) { |
| 4803 | ret = -EIO; |
Chris Mason | 8964222 | 2008-07-24 09:41:53 -0400 | [diff] [blame] | 4804 | goto out_unlock; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4805 | } |
| 4806 | } |
Chris Mason | 211c17f | 2008-05-15 09:13:45 -0400 | [diff] [blame] | 4807 | wait_on_page_writeback(page); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4808 | |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4809 | lock_extent_bits(io_tree, block_start, block_end, &cached_state); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4810 | set_page_extent_mapped(page); |
| 4811 | |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4812 | ordered = btrfs_lookup_ordered_extent(inode, block_start); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4813 | if (ordered) { |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4814 | unlock_extent_cached(io_tree, block_start, block_end, |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4815 | &cached_state, GFP_NOFS); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4816 | unlock_page(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 4817 | put_page(page); |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 4818 | btrfs_start_ordered_extent(inode, ordered, 1); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4819 | btrfs_put_ordered_extent(ordered); |
| 4820 | goto again; |
| 4821 | } |
| 4822 | |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4823 | clear_extent_bit(&BTRFS_I(inode)->io_tree, block_start, block_end, |
Liu Bo | 9e8a4a8 | 2012-09-05 19:10:51 -0600 | [diff] [blame] | 4824 | EXTENT_DIRTY | EXTENT_DELALLOC | |
| 4825 | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, |
David Sterba | ae0f162 | 2017-10-31 16:37:52 +0100 | [diff] [blame] | 4826 | 0, 0, &cached_state); |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 4827 | |
Filipe Manana | e3b8a48 | 2017-11-04 00:16:59 +0000 | [diff] [blame] | 4828 | ret = btrfs_set_extent_delalloc(inode, block_start, block_end, 0, |
Qu Wenruo | ba8b04c | 2016-07-19 16:50:36 +0800 | [diff] [blame] | 4829 | &cached_state, 0); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4830 | if (ret) { |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4831 | unlock_extent_cached(io_tree, block_start, block_end, |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4832 | &cached_state, GFP_NOFS); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4833 | goto out_unlock; |
| 4834 | } |
| 4835 | |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4836 | if (offset != blocksize) { |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 4837 | if (!len) |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4838 | len = blocksize - offset; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4839 | kaddr = kmap(page); |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 4840 | if (front) |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4841 | memset(kaddr + (block_start - page_offset(page)), |
| 4842 | 0, offset); |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 4843 | else |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4844 | memset(kaddr + (block_start - page_offset(page)) + offset, |
| 4845 | 0, len); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4846 | flush_dcache_page(page); |
| 4847 | kunmap(page); |
| 4848 | } |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 4849 | ClearPageChecked(page); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4850 | set_page_dirty(page); |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4851 | unlock_extent_cached(io_tree, block_start, block_end, &cached_state, |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4852 | GFP_NOFS); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4853 | |
Chris Mason | 8964222 | 2008-07-24 09:41:53 -0400 | [diff] [blame] | 4854 | out_unlock: |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 4855 | if (ret) |
Qu Wenruo | bc42bda | 2017-02-27 15:10:39 +0800 | [diff] [blame] | 4856 | btrfs_delalloc_release_space(inode, data_reserved, block_start, |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4857 | blocksize); |
Josef Bacik | 8b62f87 | 2017-10-19 14:15:55 -0400 | [diff] [blame] | 4858 | btrfs_delalloc_release_extents(BTRFS_I(inode), blocksize); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4859 | unlock_page(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 4860 | put_page(page); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4861 | out: |
Qu Wenruo | 364ecf3 | 2017-02-27 15:10:38 +0800 | [diff] [blame] | 4862 | extent_changeset_free(data_reserved); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4863 | return ret; |
| 4864 | } |
| 4865 | |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4866 | static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode, |
| 4867 | u64 offset, u64 len) |
| 4868 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4869 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4870 | struct btrfs_trans_handle *trans; |
| 4871 | int ret; |
| 4872 | |
| 4873 | /* |
| 4874 | * Still need to make sure the inode looks like it's been updated so |
| 4875 | * that any holes get logged if we fsync. |
| 4876 | */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4877 | if (btrfs_fs_incompat(fs_info, NO_HOLES)) { |
| 4878 | BTRFS_I(inode)->last_trans = fs_info->generation; |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4879 | BTRFS_I(inode)->last_sub_trans = root->log_transid; |
| 4880 | BTRFS_I(inode)->last_log_commit = root->last_log_commit; |
| 4881 | return 0; |
| 4882 | } |
| 4883 | |
| 4884 | /* |
| 4885 | * 1 - for the one we're dropping |
| 4886 | * 1 - for the one we're adding |
| 4887 | * 1 - for updating the inode. |
| 4888 | */ |
| 4889 | trans = btrfs_start_transaction(root, 3); |
| 4890 | if (IS_ERR(trans)) |
| 4891 | return PTR_ERR(trans); |
| 4892 | |
| 4893 | ret = btrfs_drop_extents(trans, root, inode, offset, offset + len, 1); |
| 4894 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 4895 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 4896 | btrfs_end_transaction(trans); |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4897 | return ret; |
| 4898 | } |
| 4899 | |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 4900 | ret = btrfs_insert_file_extent(trans, root, btrfs_ino(BTRFS_I(inode)), |
| 4901 | offset, 0, 0, len, 0, len, 0, 0, 0); |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4902 | if (ret) |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 4903 | btrfs_abort_transaction(trans, ret); |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4904 | else |
| 4905 | btrfs_update_inode(trans, root, inode); |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 4906 | btrfs_end_transaction(trans); |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4907 | return ret; |
| 4908 | } |
| 4909 | |
Josef Bacik | 695a0d0 | 2011-03-04 15:46:53 -0500 | [diff] [blame] | 4910 | /* |
| 4911 | * This function puts in dummy file extents for the area we're creating a hole |
| 4912 | * for. So if we are truncating this file to a larger size we need to insert |
| 4913 | * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for |
| 4914 | * the range between oldsize and size |
| 4915 | */ |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 4916 | 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] | 4917 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4918 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4919 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 4920 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4921 | struct extent_map *em = NULL; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4922 | struct extent_state *cached_state = NULL; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4923 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4924 | u64 hole_start = ALIGN(oldsize, fs_info->sectorsize); |
| 4925 | u64 block_end = ALIGN(size, fs_info->sectorsize); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4926 | u64 last_byte; |
| 4927 | u64 cur_offset; |
| 4928 | u64 hole_size; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4929 | int err = 0; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4930 | |
Josef Bacik | a71754f | 2013-06-17 17:14:39 -0400 | [diff] [blame] | 4931 | /* |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4932 | * If our size started in the middle of a block we need to zero out the |
| 4933 | * 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] | 4934 | * expose stale data. |
| 4935 | */ |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 4936 | err = btrfs_truncate_block(inode, oldsize, 0, 0); |
Josef Bacik | a71754f | 2013-06-17 17:14:39 -0400 | [diff] [blame] | 4937 | if (err) |
| 4938 | return err; |
| 4939 | |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4940 | if (size <= hole_start) |
| 4941 | return 0; |
| 4942 | |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4943 | while (1) { |
| 4944 | struct btrfs_ordered_extent *ordered; |
Miao Xie | fa7c149 | 2013-09-26 13:15:27 +0800 | [diff] [blame] | 4945 | |
David Sterba | ff13db4 | 2015-12-03 14:30:40 +0100 | [diff] [blame] | 4946 | lock_extent_bits(io_tree, hole_start, block_end - 1, |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 4947 | &cached_state); |
Nikolay Borisov | a776c6f | 2017-02-20 13:50:49 +0200 | [diff] [blame] | 4948 | ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), hole_start, |
Miao Xie | fa7c149 | 2013-09-26 13:15:27 +0800 | [diff] [blame] | 4949 | block_end - hole_start); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4950 | if (!ordered) |
| 4951 | break; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4952 | unlock_extent_cached(io_tree, hole_start, block_end - 1, |
| 4953 | &cached_state, GFP_NOFS); |
Miao Xie | fa7c149 | 2013-09-26 13:15:27 +0800 | [diff] [blame] | 4954 | btrfs_start_ordered_extent(inode, ordered, 1); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4955 | btrfs_put_ordered_extent(ordered); |
| 4956 | } |
| 4957 | |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4958 | cur_offset = hole_start; |
| 4959 | while (1) { |
Nikolay Borisov | fc4f21b1 | 2017-02-20 13:51:06 +0200 | [diff] [blame] | 4960 | em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, cur_offset, |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4961 | block_end - cur_offset, 0); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4962 | if (IS_ERR(em)) { |
| 4963 | err = PTR_ERR(em); |
Zach Brown | f276795 | 2013-01-08 19:37:58 +0000 | [diff] [blame] | 4964 | em = NULL; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4965 | break; |
| 4966 | } |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4967 | last_byte = min(extent_map_end(em), block_end); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4968 | last_byte = ALIGN(last_byte, fs_info->sectorsize); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4969 | if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) { |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4970 | struct extent_map *hole_em; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4971 | hole_size = last_byte - cur_offset; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4972 | |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4973 | err = maybe_insert_hole(root, inode, cur_offset, |
| 4974 | hole_size); |
| 4975 | if (err) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 4976 | break; |
Nikolay Borisov | dcdbc05 | 2017-02-20 13:50:45 +0200 | [diff] [blame] | 4977 | btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset, |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4978 | cur_offset + hole_size - 1, 0); |
| 4979 | hole_em = alloc_extent_map(); |
| 4980 | if (!hole_em) { |
| 4981 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, |
| 4982 | &BTRFS_I(inode)->runtime_flags); |
| 4983 | goto next; |
| 4984 | } |
| 4985 | hole_em->start = cur_offset; |
| 4986 | hole_em->len = hole_size; |
| 4987 | hole_em->orig_start = cur_offset; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4988 | |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4989 | hole_em->block_start = EXTENT_MAP_HOLE; |
| 4990 | hole_em->block_len = 0; |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 4991 | hole_em->orig_block_len = 0; |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 4992 | hole_em->ram_bytes = hole_size; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4993 | hole_em->bdev = fs_info->fs_devices->latest_bdev; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4994 | hole_em->compress_type = BTRFS_COMPRESS_NONE; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4995 | hole_em->generation = fs_info->generation; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4996 | |
| 4997 | while (1) { |
| 4998 | write_lock(&em_tree->lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 4999 | err = add_extent_mapping(em_tree, hole_em, 1); |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 5000 | write_unlock(&em_tree->lock); |
| 5001 | if (err != -EEXIST) |
| 5002 | break; |
Nikolay Borisov | dcdbc05 | 2017-02-20 13:50:45 +0200 | [diff] [blame] | 5003 | btrfs_drop_extent_cache(BTRFS_I(inode), |
| 5004 | cur_offset, |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 5005 | cur_offset + |
| 5006 | hole_size - 1, 0); |
| 5007 | } |
| 5008 | free_extent_map(hole_em); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 5009 | } |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 5010 | next: |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 5011 | free_extent_map(em); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 5012 | em = NULL; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 5013 | cur_offset = last_byte; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5014 | if (cur_offset >= block_end) |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 5015 | break; |
| 5016 | } |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 5017 | free_extent_map(em); |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 5018 | unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state, |
| 5019 | GFP_NOFS); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 5020 | return err; |
| 5021 | } |
| 5022 | |
Eric Sandeen | 3972f26 | 2013-01-12 02:57:22 +0000 | [diff] [blame] | 5023 | static int btrfs_setsize(struct inode *inode, struct iattr *attr) |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5024 | { |
Miao Xie | f4a2f4c | 2011-12-14 20:12:01 -0500 | [diff] [blame] | 5025 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5026 | struct btrfs_trans_handle *trans; |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 5027 | loff_t oldsize = i_size_read(inode); |
Eric Sandeen | 3972f26 | 2013-01-12 02:57:22 +0000 | [diff] [blame] | 5028 | loff_t newsize = attr->ia_size; |
| 5029 | int mask = attr->ia_valid; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5030 | int ret; |
| 5031 | |
Eric Sandeen | 3972f26 | 2013-01-12 02:57:22 +0000 | [diff] [blame] | 5032 | /* |
| 5033 | * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a |
| 5034 | * special case where we need to update the times despite not having |
| 5035 | * these flags set. For all other operations the VFS set these flags |
| 5036 | * explicitly if it wants a timestamp update. |
| 5037 | */ |
Christoph Hellwig | dff6efc | 2013-11-19 07:17:07 -0800 | [diff] [blame] | 5038 | if (newsize != oldsize) { |
| 5039 | inode_inc_iversion(inode); |
| 5040 | if (!(mask & (ATTR_CTIME | ATTR_MTIME))) |
| 5041 | inode->i_ctime = inode->i_mtime = |
Deepa Dinamani | c2050a4 | 2016-09-14 07:48:06 -0700 | [diff] [blame] | 5042 | current_time(inode); |
Christoph Hellwig | dff6efc | 2013-11-19 07:17:07 -0800 | [diff] [blame] | 5043 | } |
Eric Sandeen | 3972f26 | 2013-01-12 02:57:22 +0000 | [diff] [blame] | 5044 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 5045 | if (newsize > oldsize) { |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 5046 | /* |
David Sterba | ea14b57f | 2017-06-22 02:19:11 +0200 | [diff] [blame] | 5047 | * Don't do an expanding truncate while snapshotting is ongoing. |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 5048 | * This is to ensure the snapshot captures a fully consistent |
| 5049 | * state of this file - if the snapshot captures this expanding |
| 5050 | * truncation, it must capture all writes that happened before |
| 5051 | * this truncation. |
| 5052 | */ |
Zhao Lei | 0bc19f90 | 2016-01-06 18:56:36 +0800 | [diff] [blame] | 5053 | btrfs_wait_for_snapshot_creation(root); |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 5054 | ret = btrfs_cont_expand(inode, oldsize, newsize); |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 5055 | if (ret) { |
David Sterba | ea14b57f | 2017-06-22 02:19:11 +0200 | [diff] [blame] | 5056 | btrfs_end_write_no_snapshotting(root); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5057 | return ret; |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 5058 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5059 | |
Miao Xie | f4a2f4c | 2011-12-14 20:12:01 -0500 | [diff] [blame] | 5060 | trans = btrfs_start_transaction(root, 1); |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 5061 | if (IS_ERR(trans)) { |
David Sterba | ea14b57f | 2017-06-22 02:19:11 +0200 | [diff] [blame] | 5062 | btrfs_end_write_no_snapshotting(root); |
Miao Xie | f4a2f4c | 2011-12-14 20:12:01 -0500 | [diff] [blame] | 5063 | return PTR_ERR(trans); |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 5064 | } |
Miao Xie | f4a2f4c | 2011-12-14 20:12:01 -0500 | [diff] [blame] | 5065 | |
| 5066 | i_size_write(inode, newsize); |
| 5067 | btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL); |
Chandan Rajendra | 27772b6 | 2016-01-21 15:56:03 +0530 | [diff] [blame] | 5068 | pagecache_isize_extended(inode, oldsize, newsize); |
Miao Xie | f4a2f4c | 2011-12-14 20:12:01 -0500 | [diff] [blame] | 5069 | ret = btrfs_update_inode(trans, root, inode); |
David Sterba | ea14b57f | 2017-06-22 02:19:11 +0200 | [diff] [blame] | 5070 | btrfs_end_write_no_snapshotting(root); |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 5071 | btrfs_end_transaction(trans); |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 5072 | } else { |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5073 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 5074 | /* |
| 5075 | * We're truncating a file that used to have good data down to |
| 5076 | * zero. Make sure it gets into the ordered flush list so that |
| 5077 | * any new writes get down to disk quickly. |
| 5078 | */ |
| 5079 | if (newsize == 0) |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 5080 | set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE, |
| 5081 | &BTRFS_I(inode)->runtime_flags); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5082 | |
Josef Bacik | f3fe820 | 2013-01-07 17:03:21 -0500 | [diff] [blame] | 5083 | /* |
| 5084 | * 1 for the orphan item we're going to add |
| 5085 | * 1 for the orphan item deletion. |
| 5086 | */ |
| 5087 | trans = btrfs_start_transaction(root, 2); |
| 5088 | if (IS_ERR(trans)) |
| 5089 | return PTR_ERR(trans); |
| 5090 | |
| 5091 | /* |
| 5092 | * We need to do this in case we fail at _any_ point during the |
| 5093 | * actual truncate. Once we do the truncate_setsize we could |
| 5094 | * invalidate pages which forces any outstanding ordered io to |
| 5095 | * be instantly completed which will give us extents that need |
| 5096 | * to be truncated. If we fail to get an orphan inode down we |
| 5097 | * could have left over extents that were never meant to live, |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 5098 | * so we need to guarantee from this point on that everything |
Josef Bacik | f3fe820 | 2013-01-07 17:03:21 -0500 | [diff] [blame] | 5099 | * will be consistent. |
| 5100 | */ |
Nikolay Borisov | 73f2e54 | 2017-02-20 13:50:59 +0200 | [diff] [blame] | 5101 | ret = btrfs_orphan_add(trans, BTRFS_I(inode)); |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 5102 | btrfs_end_transaction(trans); |
Josef Bacik | f3fe820 | 2013-01-07 17:03:21 -0500 | [diff] [blame] | 5103 | if (ret) |
| 5104 | return ret; |
| 5105 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 5106 | /* we don't support swapfiles, so vmtruncate shouldn't fail */ |
| 5107 | truncate_setsize(inode, newsize); |
Miao Xie | 2e60a51 | 2013-02-08 07:01:08 +0000 | [diff] [blame] | 5108 | |
| 5109 | /* Disable nonlocked read DIO to avoid the end less truncate */ |
Nikolay Borisov | abcefb1 | 2017-02-20 13:51:10 +0200 | [diff] [blame] | 5110 | btrfs_inode_block_unlocked_dio(BTRFS_I(inode)); |
Miao Xie | 2e60a51 | 2013-02-08 07:01:08 +0000 | [diff] [blame] | 5111 | inode_dio_wait(inode); |
Nikolay Borisov | 0b58170 | 2017-02-20 13:51:11 +0200 | [diff] [blame] | 5112 | btrfs_inode_resume_unlocked_dio(BTRFS_I(inode)); |
Miao Xie | 2e60a51 | 2013-02-08 07:01:08 +0000 | [diff] [blame] | 5113 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 5114 | ret = btrfs_truncate(inode); |
Josef Bacik | 7f4f6e0 | 2013-08-29 16:43:28 -0400 | [diff] [blame] | 5115 | if (ret && inode->i_nlink) { |
| 5116 | int err; |
| 5117 | |
Liu Bo | 19fd2df | 2016-12-01 13:01:02 -0800 | [diff] [blame] | 5118 | /* To get a stable disk_i_size */ |
| 5119 | err = btrfs_wait_ordered_range(inode, 0, (u64)-1); |
| 5120 | if (err) { |
Nikolay Borisov | 3d6ae7b | 2017-02-20 13:50:58 +0200 | [diff] [blame] | 5121 | btrfs_orphan_del(NULL, BTRFS_I(inode)); |
Liu Bo | 19fd2df | 2016-12-01 13:01:02 -0800 | [diff] [blame] | 5122 | return err; |
| 5123 | } |
| 5124 | |
Josef Bacik | 7f4f6e0 | 2013-08-29 16:43:28 -0400 | [diff] [blame] | 5125 | /* |
| 5126 | * failed to truncate, disk_i_size is only adjusted down |
| 5127 | * as we remove extents, so it should represent the true |
| 5128 | * size of the inode, so reset the in memory size and |
| 5129 | * delete our orphan entry. |
| 5130 | */ |
| 5131 | trans = btrfs_join_transaction(root); |
| 5132 | if (IS_ERR(trans)) { |
Nikolay Borisov | 3d6ae7b | 2017-02-20 13:50:58 +0200 | [diff] [blame] | 5133 | btrfs_orphan_del(NULL, BTRFS_I(inode)); |
Josef Bacik | 7f4f6e0 | 2013-08-29 16:43:28 -0400 | [diff] [blame] | 5134 | return ret; |
| 5135 | } |
| 5136 | i_size_write(inode, BTRFS_I(inode)->disk_i_size); |
Nikolay Borisov | 3d6ae7b | 2017-02-20 13:50:58 +0200 | [diff] [blame] | 5137 | err = btrfs_orphan_del(trans, BTRFS_I(inode)); |
Josef Bacik | 7f4f6e0 | 2013-08-29 16:43:28 -0400 | [diff] [blame] | 5138 | if (err) |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 5139 | btrfs_abort_transaction(trans, err); |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 5140 | btrfs_end_transaction(trans); |
Josef Bacik | 7f4f6e0 | 2013-08-29 16:43:28 -0400 | [diff] [blame] | 5141 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5142 | } |
| 5143 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 5144 | return ret; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5145 | } |
| 5146 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5147 | static int btrfs_setattr(struct dentry *dentry, struct iattr *attr) |
| 5148 | { |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 5149 | struct inode *inode = d_inode(dentry); |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 5150 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5151 | int err; |
| 5152 | |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 5153 | if (btrfs_root_readonly(root)) |
| 5154 | return -EROFS; |
| 5155 | |
Jan Kara | 31051c8 | 2016-05-26 16:55:18 +0200 | [diff] [blame] | 5156 | err = setattr_prepare(dentry, attr); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5157 | if (err) |
| 5158 | return err; |
| 5159 | |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 5160 | if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) { |
Eric Sandeen | 3972f26 | 2013-01-12 02:57:22 +0000 | [diff] [blame] | 5161 | err = btrfs_setsize(inode, attr); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5162 | if (err) |
| 5163 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5164 | } |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 5165 | |
Christoph Hellwig | 1025774 | 2010-06-04 11:30:02 +0200 | [diff] [blame] | 5166 | if (attr->ia_valid) { |
| 5167 | setattr_copy(inode, attr); |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 5168 | inode_inc_iversion(inode); |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 5169 | err = btrfs_dirty_inode(inode); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 5170 | |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 5171 | if (!err && attr->ia_valid & ATTR_MODE) |
Christoph Hellwig | 996a710 | 2013-12-20 05:16:43 -0800 | [diff] [blame] | 5172 | err = posix_acl_chmod(inode, inode->i_mode); |
Christoph Hellwig | 1025774 | 2010-06-04 11:30:02 +0200 | [diff] [blame] | 5173 | } |
| 5174 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5175 | return err; |
| 5176 | } |
Chris Mason | 61295eb | 2008-01-14 16:24:38 -0500 | [diff] [blame] | 5177 | |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5178 | /* |
| 5179 | * While truncating the inode pages during eviction, we get the VFS calling |
| 5180 | * btrfs_invalidatepage() against each page of the inode. This is slow because |
| 5181 | * the calls to btrfs_invalidatepage() result in a huge amount of calls to |
| 5182 | * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting |
| 5183 | * extent_state structures over and over, wasting lots of time. |
| 5184 | * |
| 5185 | * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all |
| 5186 | * those expensive operations on a per page basis and do only the ordered io |
| 5187 | * finishing, while we release here the extent_map and extent_state structures, |
| 5188 | * without the excessive merging and splitting. |
| 5189 | */ |
| 5190 | static void evict_inode_truncate_pages(struct inode *inode) |
| 5191 | { |
| 5192 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
| 5193 | struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree; |
| 5194 | struct rb_node *node; |
| 5195 | |
| 5196 | ASSERT(inode->i_state & I_FREEING); |
Johannes Weiner | 91b0abe | 2014-04-03 14:47:49 -0700 | [diff] [blame] | 5197 | truncate_inode_pages_final(&inode->i_data); |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5198 | |
| 5199 | write_lock(&map_tree->lock); |
| 5200 | while (!RB_EMPTY_ROOT(&map_tree->map)) { |
| 5201 | struct extent_map *em; |
| 5202 | |
| 5203 | node = rb_first(&map_tree->map); |
| 5204 | em = rb_entry(node, struct extent_map, rb_node); |
Wang Shilong | 180589e | 2013-12-14 15:27:31 +0800 | [diff] [blame] | 5205 | clear_bit(EXTENT_FLAG_PINNED, &em->flags); |
| 5206 | clear_bit(EXTENT_FLAG_LOGGING, &em->flags); |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5207 | remove_extent_mapping(map_tree, em); |
| 5208 | free_extent_map(em); |
Filipe Manana | 7064dd5 | 2014-08-08 02:47:05 +0100 | [diff] [blame] | 5209 | if (need_resched()) { |
| 5210 | write_unlock(&map_tree->lock); |
| 5211 | cond_resched(); |
| 5212 | write_lock(&map_tree->lock); |
| 5213 | } |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5214 | } |
| 5215 | write_unlock(&map_tree->lock); |
| 5216 | |
Filipe Manana | 6ca0709 | 2015-05-26 00:55:42 +0100 | [diff] [blame] | 5217 | /* |
| 5218 | * Keep looping until we have no more ranges in the io tree. |
| 5219 | * We can have ongoing bios started by readpages (called from readahead) |
Filipe Manana | 9c6429d | 2015-06-10 12:55:41 +0100 | [diff] [blame] | 5220 | * that have their endio callback (extent_io.c:end_bio_extent_readpage) |
| 5221 | * still in progress (unlocked the pages in the bio but did not yet |
| 5222 | * unlocked the ranges in the io tree). Therefore this means some |
Filipe Manana | 6ca0709 | 2015-05-26 00:55:42 +0100 | [diff] [blame] | 5223 | * ranges can still be locked and eviction started because before |
| 5224 | * submitting those bios, which are executed by a separate task (work |
| 5225 | * queue kthread), inode references (inode->i_count) were not taken |
| 5226 | * (which would be dropped in the end io callback of each bio). |
| 5227 | * Therefore here we effectively end up waiting for those bios and |
| 5228 | * anyone else holding locked ranges without having bumped the inode's |
| 5229 | * reference count - if we don't do it, when they access the inode's |
| 5230 | * io_tree to unlock a range it may be too late, leading to an |
| 5231 | * use-after-free issue. |
| 5232 | */ |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5233 | spin_lock(&io_tree->lock); |
| 5234 | while (!RB_EMPTY_ROOT(&io_tree->state)) { |
| 5235 | struct extent_state *state; |
| 5236 | struct extent_state *cached_state = NULL; |
Filipe Manana | 6ca0709 | 2015-05-26 00:55:42 +0100 | [diff] [blame] | 5237 | u64 start; |
| 5238 | u64 end; |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5239 | |
| 5240 | node = rb_first(&io_tree->state); |
| 5241 | state = rb_entry(node, struct extent_state, rb_node); |
Filipe Manana | 6ca0709 | 2015-05-26 00:55:42 +0100 | [diff] [blame] | 5242 | start = state->start; |
| 5243 | end = state->end; |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5244 | spin_unlock(&io_tree->lock); |
| 5245 | |
David Sterba | ff13db4 | 2015-12-03 14:30:40 +0100 | [diff] [blame] | 5246 | lock_extent_bits(io_tree, start, end, &cached_state); |
Qu Wenruo | b9d0b38 | 2015-09-29 10:35:16 +0800 | [diff] [blame] | 5247 | |
| 5248 | /* |
| 5249 | * If still has DELALLOC flag, the extent didn't reach disk, |
| 5250 | * and its reserved space won't be freed by delayed_ref. |
| 5251 | * So we need to free its reserved space here. |
| 5252 | * (Refer to comment in btrfs_invalidatepage, case 2) |
| 5253 | * |
| 5254 | * Note, end is the bytenr of last byte, so we need + 1 here. |
| 5255 | */ |
| 5256 | if (state->state & EXTENT_DELALLOC) |
Qu Wenruo | bc42bda | 2017-02-27 15:10:39 +0800 | [diff] [blame] | 5257 | btrfs_qgroup_free_data(inode, NULL, start, end - start + 1); |
Qu Wenruo | b9d0b38 | 2015-09-29 10:35:16 +0800 | [diff] [blame] | 5258 | |
Filipe Manana | 6ca0709 | 2015-05-26 00:55:42 +0100 | [diff] [blame] | 5259 | clear_extent_bit(io_tree, start, end, |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5260 | EXTENT_LOCKED | EXTENT_DIRTY | |
| 5261 | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING | |
David Sterba | ae0f162 | 2017-10-31 16:37:52 +0100 | [diff] [blame] | 5262 | EXTENT_DEFRAG, 1, 1, &cached_state); |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5263 | |
Filipe Manana | 7064dd5 | 2014-08-08 02:47:05 +0100 | [diff] [blame] | 5264 | cond_resched(); |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5265 | spin_lock(&io_tree->lock); |
| 5266 | } |
| 5267 | spin_unlock(&io_tree->lock); |
| 5268 | } |
| 5269 | |
Al Viro | bd55597 | 2010-06-07 11:35:40 -0400 | [diff] [blame] | 5270 | void btrfs_evict_inode(struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5271 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5272 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5273 | struct btrfs_trans_handle *trans; |
| 5274 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Josef Bacik | 726c35f | 2011-09-26 15:46:06 -0400 | [diff] [blame] | 5275 | struct btrfs_block_rsv *rsv, *global_rsv; |
Josef Bacik | 3bce876 | 2015-02-24 12:35:51 -0800 | [diff] [blame] | 5276 | int steal_from_global = 0; |
Nikolay Borisov | 3d48d98 | 2016-06-29 09:46:41 +0300 | [diff] [blame] | 5277 | u64 min_size; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5278 | int ret; |
| 5279 | |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 5280 | trace_btrfs_inode_evict(inode); |
| 5281 | |
Nikolay Borisov | 3d48d98 | 2016-06-29 09:46:41 +0300 | [diff] [blame] | 5282 | if (!root) { |
| 5283 | kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode)); |
| 5284 | return; |
| 5285 | } |
| 5286 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5287 | min_size = btrfs_calc_trunc_metadata_size(fs_info, 1); |
Nikolay Borisov | 3d48d98 | 2016-06-29 09:46:41 +0300 | [diff] [blame] | 5288 | |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 5289 | evict_inode_truncate_pages(inode); |
| 5290 | |
Stefan Behrens | 69e9c6c | 2013-09-05 16:58:43 +0200 | [diff] [blame] | 5291 | if (inode->i_nlink && |
| 5292 | ((btrfs_root_refs(&root->root_item) != 0 && |
| 5293 | root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) || |
Nikolay Borisov | 70ddc55 | 2017-02-20 13:50:35 +0200 | [diff] [blame] | 5294 | btrfs_is_free_space_inode(BTRFS_I(inode)))) |
Al Viro | bd55597 | 2010-06-07 11:35:40 -0400 | [diff] [blame] | 5295 | goto no_delete; |
| 5296 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5297 | if (is_bad_inode(inode)) { |
Nikolay Borisov | 3d6ae7b | 2017-02-20 13:50:58 +0200 | [diff] [blame] | 5298 | btrfs_orphan_del(NULL, BTRFS_I(inode)); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5299 | goto no_delete; |
| 5300 | } |
Al Viro | bd55597 | 2010-06-07 11:35:40 -0400 | [diff] [blame] | 5301 | /* 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] | 5302 | if (!special_file(inode->i_mode)) |
| 5303 | btrfs_wait_ordered_range(inode, 0, (u64)-1); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5304 | |
Nikolay Borisov | 7ab7956 | 2017-02-20 13:50:57 +0200 | [diff] [blame] | 5305 | btrfs_free_io_failure_record(BTRFS_I(inode), 0, (u64)-1); |
Miao Xie | f612496 | 2014-09-12 18:44:04 +0800 | [diff] [blame] | 5306 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5307 | if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) { |
Liu Bo | 6bf0231 | 2012-06-25 21:59:09 -0600 | [diff] [blame] | 5308 | BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 5309 | &BTRFS_I(inode)->runtime_flags)); |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 5310 | goto no_delete; |
| 5311 | } |
| 5312 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5313 | if (inode->i_nlink > 0) { |
Stefan Behrens | 69e9c6c | 2013-09-05 16:58:43 +0200 | [diff] [blame] | 5314 | BUG_ON(btrfs_root_refs(&root->root_item) != 0 && |
| 5315 | root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5316 | goto no_delete; |
| 5317 | } |
| 5318 | |
Nikolay Borisov | aa79021 | 2017-01-10 20:35:40 +0200 | [diff] [blame] | 5319 | ret = btrfs_commit_inode_delayed_inode(BTRFS_I(inode)); |
Miao Xie | 0e8c36a | 2012-12-19 06:59:51 +0000 | [diff] [blame] | 5320 | if (ret) { |
Nikolay Borisov | 3d6ae7b | 2017-02-20 13:50:58 +0200 | [diff] [blame] | 5321 | btrfs_orphan_del(NULL, BTRFS_I(inode)); |
Miao Xie | 0e8c36a | 2012-12-19 06:59:51 +0000 | [diff] [blame] | 5322 | goto no_delete; |
| 5323 | } |
| 5324 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5325 | rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP); |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 5326 | if (!rsv) { |
Nikolay Borisov | 3d6ae7b | 2017-02-20 13:50:58 +0200 | [diff] [blame] | 5327 | btrfs_orphan_del(NULL, BTRFS_I(inode)); |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 5328 | goto no_delete; |
| 5329 | } |
Josef Bacik | 4a33854 | 2011-08-29 11:01:31 -0400 | [diff] [blame] | 5330 | rsv->size = min_size; |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 5331 | rsv->failfast = 1; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5332 | global_rsv = &fs_info->global_block_rsv; |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 5333 | |
Nikolay Borisov | 6ef06d2 | 2017-02-20 13:50:34 +0200 | [diff] [blame] | 5334 | btrfs_i_size_write(BTRFS_I(inode), 0); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5335 | |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 5336 | /* |
Miao Xie | 8407aa4 | 2012-09-07 01:43:32 -0600 | [diff] [blame] | 5337 | * This is a bit simpler than btrfs_truncate since we've already |
| 5338 | * reserved our space for our orphan item in the unlink, so we just |
| 5339 | * need to reserve some slack space in case we add bytes and update |
| 5340 | * inode item when doing the truncate. |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 5341 | */ |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5342 | while (1) { |
Miao Xie | 08e007d | 2012-10-16 11:33:38 +0000 | [diff] [blame] | 5343 | ret = btrfs_block_rsv_refill(root, rsv, min_size, |
| 5344 | BTRFS_RESERVE_FLUSH_LIMIT); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5345 | |
Josef Bacik | 726c35f | 2011-09-26 15:46:06 -0400 | [diff] [blame] | 5346 | /* |
| 5347 | * Try and steal from the global reserve since we will |
| 5348 | * likely not use this space anyway, we want to try as |
| 5349 | * hard as possible to get this to work. |
| 5350 | */ |
| 5351 | if (ret) |
Josef Bacik | 3bce876 | 2015-02-24 12:35:51 -0800 | [diff] [blame] | 5352 | steal_from_global++; |
| 5353 | else |
| 5354 | steal_from_global = 0; |
| 5355 | ret = 0; |
Josef Bacik | 726c35f | 2011-09-26 15:46:06 -0400 | [diff] [blame] | 5356 | |
Josef Bacik | 3bce876 | 2015-02-24 12:35:51 -0800 | [diff] [blame] | 5357 | /* |
| 5358 | * steal_from_global == 0: we reserved stuff, hooray! |
| 5359 | * steal_from_global == 1: we didn't reserve stuff, boo! |
| 5360 | * steal_from_global == 2: we've committed, still not a lot of |
| 5361 | * room but maybe we'll have room in the global reserve this |
| 5362 | * time. |
| 5363 | * steal_from_global == 3: abandon all hope! |
| 5364 | */ |
| 5365 | if (steal_from_global > 2) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5366 | btrfs_warn(fs_info, |
| 5367 | "Could not get space for a delete, will truncate on mount %d", |
| 5368 | ret); |
Nikolay Borisov | 3d6ae7b | 2017-02-20 13:50:58 +0200 | [diff] [blame] | 5369 | btrfs_orphan_del(NULL, BTRFS_I(inode)); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5370 | btrfs_free_block_rsv(fs_info, rsv); |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 5371 | goto no_delete; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5372 | } |
| 5373 | |
Miao Xie | 0e8c36a | 2012-12-19 06:59:51 +0000 | [diff] [blame] | 5374 | trans = btrfs_join_transaction(root); |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 5375 | if (IS_ERR(trans)) { |
Nikolay Borisov | 3d6ae7b | 2017-02-20 13:50:58 +0200 | [diff] [blame] | 5376 | btrfs_orphan_del(NULL, BTRFS_I(inode)); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5377 | btrfs_free_block_rsv(fs_info, rsv); |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 5378 | goto no_delete; |
| 5379 | } |
| 5380 | |
Josef Bacik | 3bce876 | 2015-02-24 12:35:51 -0800 | [diff] [blame] | 5381 | /* |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 5382 | * 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] | 5383 | * sure there is room to do it, if not we need to commit and try |
| 5384 | * again. |
| 5385 | */ |
| 5386 | if (steal_from_global) { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5387 | if (!btrfs_check_space_for_delayed_refs(trans, fs_info)) |
Josef Bacik | 3bce876 | 2015-02-24 12:35:51 -0800 | [diff] [blame] | 5388 | ret = btrfs_block_rsv_migrate(global_rsv, rsv, |
Josef Bacik | 25d609f | 2016-03-25 13:25:48 -0400 | [diff] [blame] | 5389 | min_size, 0); |
Josef Bacik | 3bce876 | 2015-02-24 12:35:51 -0800 | [diff] [blame] | 5390 | else |
| 5391 | ret = -ENOSPC; |
| 5392 | } |
| 5393 | |
| 5394 | /* |
| 5395 | * Couldn't steal from the global reserve, we have too much |
| 5396 | * pending stuff built up, commit the transaction and try it |
| 5397 | * again. |
| 5398 | */ |
| 5399 | if (ret) { |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 5400 | ret = btrfs_commit_transaction(trans); |
Josef Bacik | 3bce876 | 2015-02-24 12:35:51 -0800 | [diff] [blame] | 5401 | if (ret) { |
Nikolay Borisov | 3d6ae7b | 2017-02-20 13:50:58 +0200 | [diff] [blame] | 5402 | btrfs_orphan_del(NULL, BTRFS_I(inode)); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5403 | btrfs_free_block_rsv(fs_info, rsv); |
Josef Bacik | 3bce876 | 2015-02-24 12:35:51 -0800 | [diff] [blame] | 5404 | goto no_delete; |
| 5405 | } |
| 5406 | continue; |
| 5407 | } else { |
| 5408 | steal_from_global = 0; |
| 5409 | } |
| 5410 | |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 5411 | trans->block_rsv = rsv; |
| 5412 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5413 | ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0); |
Chris Mason | 28ed134 | 2014-12-17 09:41:04 -0800 | [diff] [blame] | 5414 | if (ret != -ENOSPC && ret != -EAGAIN) |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5415 | break; |
| 5416 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5417 | trans->block_rsv = &fs_info->trans_block_rsv; |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 5418 | btrfs_end_transaction(trans); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5419 | trans = NULL; |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5420 | btrfs_btree_balance_dirty(fs_info); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 5421 | } |
| 5422 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5423 | btrfs_free_block_rsv(fs_info, rsv); |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 5424 | |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 5425 | /* |
| 5426 | * Errors here aren't a big deal, it just means we leave orphan items |
| 5427 | * in the tree. They will be cleaned up on the next mount. |
| 5428 | */ |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5429 | if (ret == 0) { |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 5430 | trans->block_rsv = root->orphan_block_rsv; |
Nikolay Borisov | 3d6ae7b | 2017-02-20 13:50:58 +0200 | [diff] [blame] | 5431 | btrfs_orphan_del(trans, BTRFS_I(inode)); |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 5432 | } else { |
Nikolay Borisov | 3d6ae7b | 2017-02-20 13:50:58 +0200 | [diff] [blame] | 5433 | btrfs_orphan_del(NULL, BTRFS_I(inode)); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 5434 | } |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 5435 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5436 | trans->block_rsv = &fs_info->trans_block_rsv; |
| 5437 | if (!(root == fs_info->tree_root || |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 5438 | root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)) |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 5439 | btrfs_return_ino(root, btrfs_ino(BTRFS_I(inode))); |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 5440 | |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 5441 | btrfs_end_transaction(trans); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5442 | btrfs_btree_balance_dirty(fs_info); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5443 | no_delete: |
Nikolay Borisov | f48d1cf | 2017-01-10 20:35:39 +0200 | [diff] [blame] | 5444 | btrfs_remove_delayed_node(BTRFS_I(inode)); |
Jan Kara | dbd5768 | 2012-05-03 14:48:02 +0200 | [diff] [blame] | 5445 | clear_inode(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5446 | } |
| 5447 | |
| 5448 | /* |
| 5449 | * this returns the key found in the dir entry in the location pointer. |
| 5450 | * If no dir entries were found, location->objectid is 0. |
| 5451 | */ |
| 5452 | static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry, |
| 5453 | struct btrfs_key *location) |
| 5454 | { |
| 5455 | const char *name = dentry->d_name.name; |
| 5456 | int namelen = dentry->d_name.len; |
| 5457 | struct btrfs_dir_item *di; |
| 5458 | struct btrfs_path *path; |
| 5459 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Yan | 0d9f7f3 | 2007-10-25 15:48:28 -0400 | [diff] [blame] | 5460 | int ret = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5461 | |
| 5462 | path = btrfs_alloc_path(); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 5463 | if (!path) |
| 5464 | return -ENOMEM; |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 5465 | |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 5466 | di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(BTRFS_I(dir)), |
| 5467 | name, namelen, 0); |
Yan | 0d9f7f3 | 2007-10-25 15:48:28 -0400 | [diff] [blame] | 5468 | if (IS_ERR(di)) |
| 5469 | ret = PTR_ERR(di); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5470 | |
David Sterba | c704005 | 2011-04-19 18:00:01 +0200 | [diff] [blame] | 5471 | if (IS_ERR_OR_NULL(di)) |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 5472 | goto out_err; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5473 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5474 | btrfs_dir_item_key_to_cpu(path->nodes[0], di, location); |
Liu Bo | 56a0e70 | 2017-10-30 11:14:38 -0600 | [diff] [blame] | 5475 | if (location->type != BTRFS_INODE_ITEM_KEY && |
| 5476 | location->type != BTRFS_ROOT_ITEM_KEY) { |
| 5477 | btrfs_warn(root->fs_info, |
| 5478 | "%s gets something invalid in DIR_ITEM (name %s, directory ino %llu, location(%llu %u %llu))", |
| 5479 | __func__, name, btrfs_ino(BTRFS_I(dir)), |
| 5480 | location->objectid, location->type, location->offset); |
| 5481 | goto out_err; |
| 5482 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5483 | out: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5484 | btrfs_free_path(path); |
| 5485 | return ret; |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 5486 | out_err: |
| 5487 | location->objectid = 0; |
| 5488 | goto out; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5489 | } |
| 5490 | |
| 5491 | /* |
| 5492 | * when we hit a tree root in a directory, the btrfs part of the inode |
| 5493 | * needs to be changed to reflect the root directory of the tree root. This |
| 5494 | * is kind of like crossing a mount point. |
| 5495 | */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5496 | static int fixup_tree_root_location(struct btrfs_fs_info *fs_info, |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5497 | struct inode *dir, |
| 5498 | struct dentry *dentry, |
| 5499 | struct btrfs_key *location, |
| 5500 | struct btrfs_root **sub_root) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5501 | { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5502 | struct btrfs_path *path; |
| 5503 | struct btrfs_root *new_root; |
| 5504 | struct btrfs_root_ref *ref; |
| 5505 | struct extent_buffer *leaf; |
David Sterba | 1d4c08e | 2015-01-02 19:36:14 +0100 | [diff] [blame] | 5506 | struct btrfs_key key; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5507 | int ret; |
| 5508 | int err = 0; |
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 | path = btrfs_alloc_path(); |
| 5511 | if (!path) { |
| 5512 | err = -ENOMEM; |
| 5513 | goto out; |
| 5514 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5515 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5516 | err = -ENOENT; |
David Sterba | 1d4c08e | 2015-01-02 19:36:14 +0100 | [diff] [blame] | 5517 | key.objectid = BTRFS_I(dir)->root->root_key.objectid; |
| 5518 | key.type = BTRFS_ROOT_REF_KEY; |
| 5519 | key.offset = location->objectid; |
| 5520 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5521 | 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] | 5522 | if (ret) { |
| 5523 | if (ret < 0) |
| 5524 | err = ret; |
| 5525 | goto out; |
| 5526 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5527 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5528 | leaf = path->nodes[0]; |
| 5529 | ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref); |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 5530 | if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(BTRFS_I(dir)) || |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5531 | btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len) |
| 5532 | goto out; |
| 5533 | |
| 5534 | ret = memcmp_extent_buffer(leaf, dentry->d_name.name, |
| 5535 | (unsigned long)(ref + 1), |
| 5536 | dentry->d_name.len); |
| 5537 | if (ret) |
| 5538 | goto out; |
| 5539 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 5540 | btrfs_release_path(path); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5541 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5542 | new_root = btrfs_read_fs_root_no_name(fs_info, location); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5543 | if (IS_ERR(new_root)) { |
| 5544 | err = PTR_ERR(new_root); |
| 5545 | goto out; |
| 5546 | } |
| 5547 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5548 | *sub_root = new_root; |
| 5549 | location->objectid = btrfs_root_dirid(&new_root->root_item); |
| 5550 | location->type = BTRFS_INODE_ITEM_KEY; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5551 | location->offset = 0; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5552 | err = 0; |
| 5553 | out: |
| 5554 | btrfs_free_path(path); |
| 5555 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5556 | } |
| 5557 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5558 | static void inode_tree_add(struct inode *inode) |
| 5559 | { |
| 5560 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5561 | struct btrfs_inode *entry; |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 5562 | struct rb_node **p; |
| 5563 | struct rb_node *parent; |
Filipe David Borba Manana | cef2193 | 2013-09-02 12:19:13 +0100 | [diff] [blame] | 5564 | struct rb_node *new = &BTRFS_I(inode)->rb_node; |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 5565 | u64 ino = btrfs_ino(BTRFS_I(inode)); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5566 | |
Al Viro | 1d3382cb | 2010-10-23 15:19:20 -0400 | [diff] [blame] | 5567 | if (inode_unhashed(inode)) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5568 | return; |
Miao Xie | e1409ce | 2013-05-15 07:48:16 +0000 | [diff] [blame] | 5569 | parent = NULL; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5570 | spin_lock(&root->inode_lock); |
Miao Xie | e1409ce | 2013-05-15 07:48:16 +0000 | [diff] [blame] | 5571 | p = &root->inode_tree.rb_node; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5572 | while (*p) { |
| 5573 | parent = *p; |
| 5574 | entry = rb_entry(parent, struct btrfs_inode, rb_node); |
| 5575 | |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 5576 | if (ino < btrfs_ino(BTRFS_I(&entry->vfs_inode))) |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 5577 | p = &parent->rb_left; |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 5578 | else if (ino > btrfs_ino(BTRFS_I(&entry->vfs_inode))) |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 5579 | p = &parent->rb_right; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5580 | else { |
| 5581 | WARN_ON(!(entry->vfs_inode.i_state & |
Al Viro | a4ffdde | 2010-06-02 17:38:30 -0400 | [diff] [blame] | 5582 | (I_WILL_FREE | I_FREEING))); |
Filipe David Borba Manana | cef2193 | 2013-09-02 12:19:13 +0100 | [diff] [blame] | 5583 | rb_replace_node(parent, new, &root->inode_tree); |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 5584 | RB_CLEAR_NODE(parent); |
| 5585 | spin_unlock(&root->inode_lock); |
Filipe David Borba Manana | cef2193 | 2013-09-02 12:19:13 +0100 | [diff] [blame] | 5586 | return; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5587 | } |
| 5588 | } |
Filipe David Borba Manana | cef2193 | 2013-09-02 12:19:13 +0100 | [diff] [blame] | 5589 | rb_link_node(new, parent, p); |
| 5590 | rb_insert_color(new, &root->inode_tree); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5591 | spin_unlock(&root->inode_lock); |
| 5592 | } |
| 5593 | |
| 5594 | static void inode_tree_del(struct inode *inode) |
| 5595 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5596 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5597 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5598 | int empty = 0; |
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_lock(&root->inode_lock); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5601 | if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5602 | rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5603 | RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5604 | empty = RB_EMPTY_ROOT(&root->inode_tree); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5605 | } |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 5606 | spin_unlock(&root->inode_lock); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5607 | |
Stefan Behrens | 69e9c6c | 2013-09-05 16:58:43 +0200 | [diff] [blame] | 5608 | if (empty && btrfs_root_refs(&root->root_item) == 0) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5609 | synchronize_srcu(&fs_info->subvol_srcu); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5610 | spin_lock(&root->inode_lock); |
| 5611 | empty = RB_EMPTY_ROOT(&root->inode_tree); |
| 5612 | spin_unlock(&root->inode_lock); |
| 5613 | if (empty) |
| 5614 | btrfs_add_dead_root(root); |
| 5615 | } |
| 5616 | } |
| 5617 | |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 5618 | void btrfs_invalidate_inodes(struct btrfs_root *root) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5619 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5620 | struct btrfs_fs_info *fs_info = root->fs_info; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5621 | struct rb_node *node; |
| 5622 | struct rb_node *prev; |
| 5623 | struct btrfs_inode *entry; |
| 5624 | struct inode *inode; |
| 5625 | u64 objectid = 0; |
| 5626 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5627 | if (!test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) |
Liu Bo | 7813b3d | 2014-02-10 17:37:25 +0800 | [diff] [blame] | 5628 | WARN_ON(btrfs_root_refs(&root->root_item) != 0); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5629 | |
| 5630 | spin_lock(&root->inode_lock); |
| 5631 | again: |
| 5632 | node = root->inode_tree.rb_node; |
| 5633 | prev = NULL; |
| 5634 | while (node) { |
| 5635 | prev = node; |
| 5636 | entry = rb_entry(node, struct btrfs_inode, rb_node); |
| 5637 | |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 5638 | if (objectid < btrfs_ino(BTRFS_I(&entry->vfs_inode))) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5639 | node = node->rb_left; |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 5640 | else if (objectid > btrfs_ino(BTRFS_I(&entry->vfs_inode))) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5641 | node = node->rb_right; |
| 5642 | else |
| 5643 | break; |
| 5644 | } |
| 5645 | if (!node) { |
| 5646 | while (prev) { |
| 5647 | entry = rb_entry(prev, struct btrfs_inode, rb_node); |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 5648 | if (objectid <= btrfs_ino(BTRFS_I(&entry->vfs_inode))) { |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5649 | node = prev; |
| 5650 | break; |
| 5651 | } |
| 5652 | prev = rb_next(prev); |
| 5653 | } |
| 5654 | } |
| 5655 | while (node) { |
| 5656 | entry = rb_entry(node, struct btrfs_inode, rb_node); |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 5657 | objectid = btrfs_ino(BTRFS_I(&entry->vfs_inode)) + 1; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5658 | inode = igrab(&entry->vfs_inode); |
| 5659 | if (inode) { |
| 5660 | spin_unlock(&root->inode_lock); |
| 5661 | if (atomic_read(&inode->i_count) > 1) |
| 5662 | d_prune_aliases(inode); |
| 5663 | /* |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 5664 | * btrfs_drop_inode will have it removed from |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5665 | * the inode cache when its usage count |
| 5666 | * hits zero. |
| 5667 | */ |
| 5668 | iput(inode); |
| 5669 | cond_resched(); |
| 5670 | spin_lock(&root->inode_lock); |
| 5671 | goto again; |
| 5672 | } |
| 5673 | |
| 5674 | if (cond_resched_lock(&root->inode_lock)) |
| 5675 | goto again; |
| 5676 | |
| 5677 | node = rb_next(node); |
| 5678 | } |
| 5679 | spin_unlock(&root->inode_lock); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5680 | } |
| 5681 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5682 | static int btrfs_init_locked_inode(struct inode *inode, void *p) |
| 5683 | { |
| 5684 | struct btrfs_iget_args *args = p; |
Chris Mason | 90d3e59 | 2014-01-09 17:28:00 -0800 | [diff] [blame] | 5685 | inode->i_ino = args->location->objectid; |
| 5686 | memcpy(&BTRFS_I(inode)->location, args->location, |
| 5687 | sizeof(*args->location)); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5688 | BTRFS_I(inode)->root = args->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5689 | return 0; |
| 5690 | } |
| 5691 | |
| 5692 | static int btrfs_find_actor(struct inode *inode, void *opaque) |
| 5693 | { |
| 5694 | struct btrfs_iget_args *args = opaque; |
Chris Mason | 90d3e59 | 2014-01-09 17:28:00 -0800 | [diff] [blame] | 5695 | return args->location->objectid == BTRFS_I(inode)->location.objectid && |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5696 | args->root == BTRFS_I(inode)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5697 | } |
| 5698 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5699 | static struct inode *btrfs_iget_locked(struct super_block *s, |
Chris Mason | 90d3e59 | 2014-01-09 17:28:00 -0800 | [diff] [blame] | 5700 | struct btrfs_key *location, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5701 | struct btrfs_root *root) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5702 | { |
| 5703 | struct inode *inode; |
| 5704 | struct btrfs_iget_args args; |
Chris Mason | 90d3e59 | 2014-01-09 17:28:00 -0800 | [diff] [blame] | 5705 | unsigned long hashval = btrfs_inode_hash(location->objectid, root); |
Filipe David Borba Manana | 778ba82 | 2013-10-06 22:22:33 +0100 | [diff] [blame] | 5706 | |
Chris Mason | 90d3e59 | 2014-01-09 17:28:00 -0800 | [diff] [blame] | 5707 | args.location = location; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5708 | args.root = root; |
| 5709 | |
Filipe David Borba Manana | 778ba82 | 2013-10-06 22:22:33 +0100 | [diff] [blame] | 5710 | inode = iget5_locked(s, hashval, btrfs_find_actor, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5711 | btrfs_init_locked_inode, |
| 5712 | (void *)&args); |
| 5713 | return inode; |
| 5714 | } |
| 5715 | |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 5716 | /* Get an inode object given its location and corresponding root. |
| 5717 | * Returns in *is_new if the inode was read from disk |
| 5718 | */ |
| 5719 | struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location, |
Josef Bacik | 73f7341 | 2009-12-04 17:38:27 +0000 | [diff] [blame] | 5720 | struct btrfs_root *root, int *new) |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 5721 | { |
| 5722 | struct inode *inode; |
| 5723 | |
Chris Mason | 90d3e59 | 2014-01-09 17:28:00 -0800 | [diff] [blame] | 5724 | inode = btrfs_iget_locked(s, location, root); |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 5725 | if (!inode) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5726 | return ERR_PTR(-ENOMEM); |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 5727 | |
| 5728 | if (inode->i_state & I_NEW) { |
Filipe Manana | 6771089 | 2016-06-06 11:51:25 +0100 | [diff] [blame] | 5729 | int ret; |
| 5730 | |
| 5731 | ret = btrfs_read_locked_inode(inode); |
Mark Fasheh | 1748f84 | 2011-07-12 11:25:31 -0700 | [diff] [blame] | 5732 | if (!is_bad_inode(inode)) { |
| 5733 | inode_tree_add(inode); |
| 5734 | unlock_new_inode(inode); |
| 5735 | if (new) |
| 5736 | *new = 1; |
| 5737 | } else { |
Sergei Trofimovich | e0b6d65 | 2011-09-11 10:52:24 -0400 | [diff] [blame] | 5738 | unlock_new_inode(inode); |
| 5739 | iput(inode); |
Filipe Manana | 6771089 | 2016-06-06 11:51:25 +0100 | [diff] [blame] | 5740 | ASSERT(ret < 0); |
| 5741 | inode = ERR_PTR(ret < 0 ? ret : -ESTALE); |
Mark Fasheh | 1748f84 | 2011-07-12 11:25:31 -0700 | [diff] [blame] | 5742 | } |
| 5743 | } |
| 5744 | |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 5745 | return inode; |
| 5746 | } |
| 5747 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5748 | static struct inode *new_simple_dir(struct super_block *s, |
| 5749 | struct btrfs_key *key, |
| 5750 | struct btrfs_root *root) |
| 5751 | { |
| 5752 | struct inode *inode = new_inode(s); |
| 5753 | |
| 5754 | if (!inode) |
| 5755 | return ERR_PTR(-ENOMEM); |
| 5756 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5757 | BTRFS_I(inode)->root = root; |
| 5758 | memcpy(&BTRFS_I(inode)->location, key, sizeof(*key)); |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 5759 | set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5760 | |
| 5761 | inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID; |
Li Zefan | 848cce0 | 2012-02-21 17:04:28 +0800 | [diff] [blame] | 5762 | inode->i_op = &btrfs_dir_ro_inode_operations; |
Omar Sandoval | 1fdf419 | 2017-01-25 17:06:39 -0800 | [diff] [blame] | 5763 | inode->i_opflags &= ~IOP_XATTR; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5764 | inode->i_fop = &simple_dir_operations; |
| 5765 | inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO; |
Deepa Dinamani | c2050a4 | 2016-09-14 07:48:06 -0700 | [diff] [blame] | 5766 | inode->i_mtime = current_time(inode); |
chandan r | 9cc97d6 | 2012-07-04 12:48:07 +0530 | [diff] [blame] | 5767 | inode->i_atime = inode->i_mtime; |
| 5768 | inode->i_ctime = inode->i_mtime; |
| 5769 | BTRFS_I(inode)->i_otime = inode->i_mtime; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5770 | |
| 5771 | return inode; |
| 5772 | } |
| 5773 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5774 | struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5775 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5776 | struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5777 | struct inode *inode; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5778 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5779 | struct btrfs_root *sub_root = root; |
| 5780 | struct btrfs_key location; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5781 | int index; |
Josef Bacik | b4aff1f | 2011-06-28 16:18:59 -0400 | [diff] [blame] | 5782 | int ret = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5783 | |
| 5784 | if (dentry->d_name.len > BTRFS_NAME_LEN) |
| 5785 | return ERR_PTR(-ENAMETOOLONG); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5786 | |
Jeff Layton | 39e3c95 | 2012-11-28 11:30:53 -0500 | [diff] [blame] | 5787 | ret = btrfs_inode_by_name(dir, dentry, &location); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5788 | if (ret < 0) |
| 5789 | return ERR_PTR(ret); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5790 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5791 | if (location.objectid == 0) |
Tsutomu Itoh | 5662344b3 | 2013-12-13 09:51:42 +0900 | [diff] [blame] | 5792 | return ERR_PTR(-ENOENT); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5793 | |
| 5794 | if (location.type == BTRFS_INODE_ITEM_KEY) { |
Josef Bacik | 73f7341 | 2009-12-04 17:38:27 +0000 | [diff] [blame] | 5795 | inode = btrfs_iget(dir->i_sb, &location, root, NULL); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5796 | return inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5797 | } |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5798 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5799 | index = srcu_read_lock(&fs_info->subvol_srcu); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5800 | ret = fixup_tree_root_location(fs_info, dir, dentry, |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5801 | &location, &sub_root); |
| 5802 | if (ret < 0) { |
| 5803 | if (ret != -ENOENT) |
| 5804 | inode = ERR_PTR(ret); |
| 5805 | else |
| 5806 | inode = new_simple_dir(dir->i_sb, &location, sub_root); |
| 5807 | } else { |
Josef Bacik | 73f7341 | 2009-12-04 17:38:27 +0000 | [diff] [blame] | 5808 | inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5809 | } |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5810 | srcu_read_unlock(&fs_info->subvol_srcu, index); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5811 | |
Julia Lawall | 34d19ba | 2011-01-24 19:55:19 +0000 | [diff] [blame] | 5812 | if (!IS_ERR(inode) && root != sub_root) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5813 | down_read(&fs_info->cleanup_work_sem); |
David Howells | bc98a42 | 2017-07-17 08:45:34 +0100 | [diff] [blame] | 5814 | if (!sb_rdonly(inode->i_sb)) |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 5815 | ret = btrfs_orphan_cleanup(sub_root); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5816 | up_read(&fs_info->cleanup_work_sem); |
Josef Bacik | 01cd336 | 2013-06-03 21:39:49 -0400 | [diff] [blame] | 5817 | if (ret) { |
| 5818 | iput(inode); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 5819 | inode = ERR_PTR(ret); |
Josef Bacik | 01cd336 | 2013-06-03 21:39:49 -0400 | [diff] [blame] | 5820 | } |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 5821 | } |
| 5822 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5823 | return inode; |
| 5824 | } |
| 5825 | |
Nick Piggin | fe15ce4 | 2011-01-07 17:49:23 +1100 | [diff] [blame] | 5826 | static int btrfs_dentry_delete(const struct dentry *dentry) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5827 | { |
| 5828 | struct btrfs_root *root; |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 5829 | struct inode *inode = d_inode(dentry); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5830 | |
Li Zefan | 848cce0 | 2012-02-21 17:04:28 +0800 | [diff] [blame] | 5831 | if (!inode && !IS_ROOT(dentry)) |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 5832 | inode = d_inode(dentry->d_parent); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5833 | |
Li Zefan | 848cce0 | 2012-02-21 17:04:28 +0800 | [diff] [blame] | 5834 | if (inode) { |
| 5835 | root = BTRFS_I(inode)->root; |
Yan, Zheng | efefb14 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5836 | if (btrfs_root_refs(&root->root_item) == 0) |
| 5837 | return 1; |
Li Zefan | 848cce0 | 2012-02-21 17:04:28 +0800 | [diff] [blame] | 5838 | |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 5839 | if (btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) |
Li Zefan | 848cce0 | 2012-02-21 17:04:28 +0800 | [diff] [blame] | 5840 | return 1; |
Yan, Zheng | efefb14 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5841 | } |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5842 | return 0; |
| 5843 | } |
| 5844 | |
Josef Bacik | b4aff1f | 2011-06-28 16:18:59 -0400 | [diff] [blame] | 5845 | static void btrfs_dentry_release(struct dentry *dentry) |
| 5846 | { |
Daeseok Youn | 944a451 | 2014-04-14 15:37:02 +0900 | [diff] [blame] | 5847 | kfree(dentry->d_fsdata); |
Josef Bacik | b4aff1f | 2011-06-28 16:18:59 -0400 | [diff] [blame] | 5848 | } |
| 5849 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5850 | static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry, |
Al Viro | 00cd8dd | 2012-06-10 17:13:09 -0400 | [diff] [blame] | 5851 | unsigned int flags) |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5852 | { |
Tsutomu Itoh | 5662344b3 | 2013-12-13 09:51:42 +0900 | [diff] [blame] | 5853 | struct inode *inode; |
Josef Bacik | a66e7cc | 2011-09-18 10:34:03 -0400 | [diff] [blame] | 5854 | |
Tsutomu Itoh | 5662344b3 | 2013-12-13 09:51:42 +0900 | [diff] [blame] | 5855 | inode = btrfs_lookup_dentry(dir, dentry); |
| 5856 | if (IS_ERR(inode)) { |
| 5857 | if (PTR_ERR(inode) == -ENOENT) |
| 5858 | inode = NULL; |
| 5859 | else |
| 5860 | return ERR_CAST(inode); |
| 5861 | } |
| 5862 | |
Al Viro | 41d28bc | 2014-10-12 22:24:21 -0400 | [diff] [blame] | 5863 | return d_splice_alias(inode, dentry); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5864 | } |
| 5865 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5866 | unsigned char btrfs_filetype_table[] = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5867 | DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK |
| 5868 | }; |
| 5869 | |
Josef Bacik | 23b5ec7 | 2017-07-24 15:14:25 -0400 | [diff] [blame] | 5870 | /* |
| 5871 | * All this infrastructure exists because dir_emit can fault, and we are holding |
| 5872 | * the tree lock when doing readdir. For now just allocate a buffer and copy |
| 5873 | * our information into that, and then dir_emit from the buffer. This is |
| 5874 | * similar to what NFS does, only we don't keep the buffer around in pagecache |
| 5875 | * because I'm afraid I'll mess that up. Long term we need to make filldir do |
| 5876 | * copy_to_user_inatomic so we don't have to worry about page faulting under the |
| 5877 | * tree lock. |
| 5878 | */ |
| 5879 | static int btrfs_opendir(struct inode *inode, struct file *file) |
| 5880 | { |
| 5881 | struct btrfs_file_private *private; |
| 5882 | |
| 5883 | private = kzalloc(sizeof(struct btrfs_file_private), GFP_KERNEL); |
| 5884 | if (!private) |
| 5885 | return -ENOMEM; |
| 5886 | private->filldir_buf = kzalloc(PAGE_SIZE, GFP_KERNEL); |
| 5887 | if (!private->filldir_buf) { |
| 5888 | kfree(private); |
| 5889 | return -ENOMEM; |
| 5890 | } |
| 5891 | file->private_data = private; |
| 5892 | return 0; |
| 5893 | } |
| 5894 | |
| 5895 | struct dir_entry { |
| 5896 | u64 ino; |
| 5897 | u64 offset; |
| 5898 | unsigned type; |
| 5899 | int name_len; |
| 5900 | }; |
| 5901 | |
| 5902 | static int btrfs_filldir(void *addr, int entries, struct dir_context *ctx) |
| 5903 | { |
| 5904 | while (entries--) { |
| 5905 | struct dir_entry *entry = addr; |
| 5906 | char *name = (char *)(entry + 1); |
| 5907 | |
| 5908 | ctx->pos = entry->offset; |
| 5909 | if (!dir_emit(ctx, name, entry->name_len, entry->ino, |
| 5910 | entry->type)) |
| 5911 | return 1; |
| 5912 | addr += sizeof(struct dir_entry) + entry->name_len; |
| 5913 | ctx->pos++; |
| 5914 | } |
| 5915 | return 0; |
| 5916 | } |
| 5917 | |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5918 | static int btrfs_real_readdir(struct file *file, struct dir_context *ctx) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5919 | { |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5920 | struct inode *inode = file_inode(file); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5921 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Josef Bacik | 23b5ec7 | 2017-07-24 15:14:25 -0400 | [diff] [blame] | 5922 | struct btrfs_file_private *private = file->private_data; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5923 | struct btrfs_dir_item *di; |
| 5924 | struct btrfs_key key; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5925 | struct btrfs_key found_key; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5926 | struct btrfs_path *path; |
Josef Bacik | 23b5ec7 | 2017-07-24 15:14:25 -0400 | [diff] [blame] | 5927 | void *addr; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5928 | struct list_head ins_list; |
| 5929 | struct list_head del_list; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5930 | int ret; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5931 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5932 | int slot; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5933 | char *name_ptr; |
| 5934 | int name_len; |
Josef Bacik | 23b5ec7 | 2017-07-24 15:14:25 -0400 | [diff] [blame] | 5935 | int entries = 0; |
| 5936 | int total_len = 0; |
Omar Sandoval | 02dbfc9 | 2016-05-20 13:50:33 -0700 | [diff] [blame] | 5937 | bool put = false; |
Jeff Mahoney | c2951f3 | 2016-11-21 15:59:04 +0100 | [diff] [blame] | 5938 | struct btrfs_key location; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5939 | |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5940 | if (!dir_emit_dots(file, ctx)) |
| 5941 | return 0; |
| 5942 | |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5943 | path = btrfs_alloc_path(); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5944 | if (!path) |
| 5945 | return -ENOMEM; |
Chris Mason | ff5714c | 2011-05-28 07:00:39 -0400 | [diff] [blame] | 5946 | |
Josef Bacik | 23b5ec7 | 2017-07-24 15:14:25 -0400 | [diff] [blame] | 5947 | addr = private->filldir_buf; |
David Sterba | e4058b5 | 2015-11-27 16:31:35 +0100 | [diff] [blame] | 5948 | path->reada = READA_FORWARD; |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5949 | |
Jeff Mahoney | c2951f3 | 2016-11-21 15:59:04 +0100 | [diff] [blame] | 5950 | INIT_LIST_HEAD(&ins_list); |
| 5951 | INIT_LIST_HEAD(&del_list); |
| 5952 | put = btrfs_readdir_get_delayed_items(inode, &ins_list, &del_list); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5953 | |
Josef Bacik | 23b5ec7 | 2017-07-24 15:14:25 -0400 | [diff] [blame] | 5954 | again: |
Jeff Mahoney | c2951f3 | 2016-11-21 15:59:04 +0100 | [diff] [blame] | 5955 | key.type = BTRFS_DIR_INDEX_KEY; |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5956 | key.offset = ctx->pos; |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 5957 | key.objectid = btrfs_ino(BTRFS_I(inode)); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5958 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5959 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 5960 | if (ret < 0) |
| 5961 | goto err; |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5962 | |
| 5963 | while (1) { |
Josef Bacik | 23b5ec7 | 2017-07-24 15:14:25 -0400 | [diff] [blame] | 5964 | struct dir_entry *entry; |
| 5965 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5966 | leaf = path->nodes[0]; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5967 | slot = path->slots[0]; |
Li Zefan | b9e03af | 2011-03-23 10:43:58 +0800 | [diff] [blame] | 5968 | if (slot >= btrfs_header_nritems(leaf)) { |
| 5969 | ret = btrfs_next_leaf(root, path); |
| 5970 | if (ret < 0) |
| 5971 | goto err; |
| 5972 | else if (ret > 0) |
| 5973 | break; |
| 5974 | continue; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5975 | } |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5976 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5977 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
| 5978 | |
| 5979 | if (found_key.objectid != key.objectid) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5980 | break; |
Jeff Mahoney | c2951f3 | 2016-11-21 15:59:04 +0100 | [diff] [blame] | 5981 | if (found_key.type != BTRFS_DIR_INDEX_KEY) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5982 | break; |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5983 | if (found_key.offset < ctx->pos) |
Li Zefan | b9e03af | 2011-03-23 10:43:58 +0800 | [diff] [blame] | 5984 | goto next; |
Jeff Mahoney | c2951f3 | 2016-11-21 15:59:04 +0100 | [diff] [blame] | 5985 | if (btrfs_should_delete_dir_index(&del_list, found_key.offset)) |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5986 | goto next; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5987 | di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item); |
Jeff Mahoney | c2951f3 | 2016-11-21 15:59:04 +0100 | [diff] [blame] | 5988 | name_len = btrfs_dir_name_len(leaf, di); |
Josef Bacik | 23b5ec7 | 2017-07-24 15:14:25 -0400 | [diff] [blame] | 5989 | if ((total_len + sizeof(struct dir_entry) + name_len) >= |
| 5990 | PAGE_SIZE) { |
| 5991 | btrfs_release_path(path); |
| 5992 | ret = btrfs_filldir(private->filldir_buf, entries, ctx); |
| 5993 | if (ret) |
| 5994 | goto nopos; |
| 5995 | addr = private->filldir_buf; |
| 5996 | entries = 0; |
| 5997 | total_len = 0; |
| 5998 | goto again; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5999 | } |
Josef Bacik | 23b5ec7 | 2017-07-24 15:14:25 -0400 | [diff] [blame] | 6000 | |
| 6001 | entry = addr; |
| 6002 | entry->name_len = name_len; |
| 6003 | name_ptr = (char *)(entry + 1); |
Jeff Mahoney | c2951f3 | 2016-11-21 15:59:04 +0100 | [diff] [blame] | 6004 | read_extent_buffer(leaf, name_ptr, (unsigned long)(di + 1), |
| 6005 | name_len); |
Josef Bacik | 23b5ec7 | 2017-07-24 15:14:25 -0400 | [diff] [blame] | 6006 | entry->type = btrfs_filetype_table[btrfs_dir_type(leaf, di)]; |
Jeff Mahoney | c2951f3 | 2016-11-21 15:59:04 +0100 | [diff] [blame] | 6007 | btrfs_dir_item_key_to_cpu(leaf, di, &location); |
Josef Bacik | 23b5ec7 | 2017-07-24 15:14:25 -0400 | [diff] [blame] | 6008 | entry->ino = location.objectid; |
| 6009 | entry->offset = found_key.offset; |
| 6010 | entries++; |
| 6011 | addr += sizeof(struct dir_entry) + name_len; |
| 6012 | total_len += sizeof(struct dir_entry) + name_len; |
Li Zefan | b9e03af | 2011-03-23 10:43:58 +0800 | [diff] [blame] | 6013 | next: |
| 6014 | path->slots[0]++; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6015 | } |
Josef Bacik | 23b5ec7 | 2017-07-24 15:14:25 -0400 | [diff] [blame] | 6016 | btrfs_release_path(path); |
| 6017 | |
| 6018 | ret = btrfs_filldir(private->filldir_buf, entries, ctx); |
| 6019 | if (ret) |
| 6020 | goto nopos; |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 6021 | |
Jeff Mahoney | d2fbb2b | 2016-11-05 13:26:35 -0400 | [diff] [blame] | 6022 | ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list); |
Jeff Mahoney | c2951f3 | 2016-11-21 15:59:04 +0100 | [diff] [blame] | 6023 | if (ret) |
David Sterba | bc4ef75 | 2015-11-13 13:44:28 +0100 | [diff] [blame] | 6024 | goto nopos; |
| 6025 | |
Zach Brown | db62efb | 2013-07-11 16:19:42 -0700 | [diff] [blame] | 6026 | /* |
| 6027 | * Stop new entries from being returned after we return the last |
| 6028 | * entry. |
| 6029 | * |
| 6030 | * New directory entries are assigned a strictly increasing |
| 6031 | * offset. This means that new entries created during readdir |
| 6032 | * are *guaranteed* to be seen in the future by that readdir. |
| 6033 | * This has broken buggy programs which operate on names as |
| 6034 | * they're returned by readdir. Until we re-use freed offsets |
| 6035 | * we have this hack to stop new entries from being returned |
| 6036 | * under the assumption that they'll never reach this huge |
| 6037 | * offset. |
| 6038 | * |
| 6039 | * This is being careful not to overflow 32bit loff_t unless the |
| 6040 | * last entry requires it because doing so has broken 32bit apps |
| 6041 | * in the past. |
| 6042 | */ |
Jeff Mahoney | c2951f3 | 2016-11-21 15:59:04 +0100 | [diff] [blame] | 6043 | if (ctx->pos >= INT_MAX) |
| 6044 | ctx->pos = LLONG_MAX; |
| 6045 | else |
| 6046 | ctx->pos = INT_MAX; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6047 | nopos: |
| 6048 | ret = 0; |
| 6049 | err: |
Omar Sandoval | 02dbfc9 | 2016-05-20 13:50:33 -0700 | [diff] [blame] | 6050 | if (put) |
| 6051 | btrfs_readdir_put_delayed_items(inode, &ins_list, &del_list); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6052 | btrfs_free_path(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6053 | return ret; |
| 6054 | } |
| 6055 | |
Christoph Hellwig | a9185b4 | 2010-03-05 09:21:37 +0100 | [diff] [blame] | 6056 | int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6057 | { |
| 6058 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 6059 | struct btrfs_trans_handle *trans; |
| 6060 | int ret = 0; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 6061 | bool nolock = false; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6062 | |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 6063 | if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags)) |
Chris Mason | 4ca8b41 | 2008-08-05 13:30:48 -0400 | [diff] [blame] | 6064 | return 0; |
| 6065 | |
Nikolay Borisov | 70ddc55 | 2017-02-20 13:50:35 +0200 | [diff] [blame] | 6066 | if (btrfs_fs_closing(root->fs_info) && |
| 6067 | btrfs_is_free_space_inode(BTRFS_I(inode))) |
Li Zefan | 82d5902 | 2011-04-20 10:33:24 +0800 | [diff] [blame] | 6068 | nolock = true; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 6069 | |
Christoph Hellwig | a9185b4 | 2010-03-05 09:21:37 +0100 | [diff] [blame] | 6070 | if (wbc->sync_mode == WB_SYNC_ALL) { |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 6071 | if (nolock) |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 6072 | trans = btrfs_join_transaction_nolock(root); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 6073 | else |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 6074 | trans = btrfs_join_transaction(root); |
Tsutomu Itoh | 3612b49 | 2011-01-25 02:51:38 +0000 | [diff] [blame] | 6075 | if (IS_ERR(trans)) |
| 6076 | return PTR_ERR(trans); |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 6077 | ret = btrfs_commit_transaction(trans); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6078 | } |
| 6079 | return ret; |
| 6080 | } |
| 6081 | |
| 6082 | /* |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 6083 | * This is somewhat expensive, updating the tree every time the |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6084 | * inode changes. But, it is most likely to find the inode in cache. |
| 6085 | * FIXME, needs more benchmarking...there are no reasons other than performance |
| 6086 | * to keep or drop this code. |
| 6087 | */ |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 6088 | static int btrfs_dirty_inode(struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6089 | { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6090 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6091 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 6092 | struct btrfs_trans_handle *trans; |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 6093 | int ret; |
| 6094 | |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 6095 | if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags)) |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 6096 | return 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6097 | |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 6098 | trans = btrfs_join_transaction(root); |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 6099 | if (IS_ERR(trans)) |
| 6100 | return PTR_ERR(trans); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 6101 | |
| 6102 | ret = btrfs_update_inode(trans, root, inode); |
Chris Mason | 94b6044 | 2010-05-26 11:02:00 -0400 | [diff] [blame] | 6103 | if (ret && ret == -ENOSPC) { |
| 6104 | /* whoops, lets try again with the full transaction */ |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 6105 | btrfs_end_transaction(trans); |
Chris Mason | 94b6044 | 2010-05-26 11:02:00 -0400 | [diff] [blame] | 6106 | trans = btrfs_start_transaction(root, 1); |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 6107 | if (IS_ERR(trans)) |
| 6108 | return PTR_ERR(trans); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 6109 | |
Chris Mason | 94b6044 | 2010-05-26 11:02:00 -0400 | [diff] [blame] | 6110 | ret = btrfs_update_inode(trans, root, inode); |
Chris Mason | 94b6044 | 2010-05-26 11:02:00 -0400 | [diff] [blame] | 6111 | } |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 6112 | btrfs_end_transaction(trans); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 6113 | if (BTRFS_I(inode)->delayed_node) |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6114 | btrfs_balance_delayed_items(fs_info); |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 6115 | |
| 6116 | return ret; |
| 6117 | } |
| 6118 | |
| 6119 | /* |
| 6120 | * This is a copy of file_update_time. We need this so we can return error on |
| 6121 | * ENOSPC for updating the inode in the case of file write and mmap writes. |
| 6122 | */ |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 6123 | static int btrfs_update_time(struct inode *inode, struct timespec *now, |
| 6124 | int flags) |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 6125 | { |
Alexander Block | 2bc556528 | 2012-06-15 09:49:33 +0200 | [diff] [blame] | 6126 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 6127 | |
| 6128 | if (btrfs_root_readonly(root)) |
| 6129 | return -EROFS; |
| 6130 | |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 6131 | if (flags & S_VERSION) |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 6132 | inode_inc_iversion(inode); |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 6133 | if (flags & S_CTIME) |
| 6134 | inode->i_ctime = *now; |
| 6135 | if (flags & S_MTIME) |
| 6136 | inode->i_mtime = *now; |
| 6137 | if (flags & S_ATIME) |
| 6138 | inode->i_atime = *now; |
| 6139 | return btrfs_dirty_inode(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6140 | } |
| 6141 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 6142 | /* |
| 6143 | * find the highest existing sequence number in a directory |
| 6144 | * and then set the in-memory index_cnt variable to reflect |
| 6145 | * free sequence numbers |
| 6146 | */ |
Nikolay Borisov | 4c570655 | 2017-02-20 13:50:32 +0200 | [diff] [blame] | 6147 | static int btrfs_set_inode_index_count(struct btrfs_inode *inode) |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6148 | { |
Nikolay Borisov | 4c570655 | 2017-02-20 13:50:32 +0200 | [diff] [blame] | 6149 | struct btrfs_root *root = inode->root; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6150 | struct btrfs_key key, found_key; |
| 6151 | struct btrfs_path *path; |
| 6152 | struct extent_buffer *leaf; |
| 6153 | int ret; |
| 6154 | |
Nikolay Borisov | 4c570655 | 2017-02-20 13:50:32 +0200 | [diff] [blame] | 6155 | key.objectid = btrfs_ino(inode); |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 6156 | key.type = BTRFS_DIR_INDEX_KEY; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6157 | key.offset = (u64)-1; |
| 6158 | |
| 6159 | path = btrfs_alloc_path(); |
| 6160 | if (!path) |
| 6161 | return -ENOMEM; |
| 6162 | |
| 6163 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 6164 | if (ret < 0) |
| 6165 | goto out; |
| 6166 | /* FIXME: we should be able to handle this */ |
| 6167 | if (ret == 0) |
| 6168 | goto out; |
| 6169 | ret = 0; |
| 6170 | |
| 6171 | /* |
| 6172 | * MAGIC NUMBER EXPLANATION: |
| 6173 | * since we search a directory based on f_pos we have to start at 2 |
| 6174 | * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody |
| 6175 | * else has to start at 2 |
| 6176 | */ |
| 6177 | if (path->slots[0] == 0) { |
Nikolay Borisov | 4c570655 | 2017-02-20 13:50:32 +0200 | [diff] [blame] | 6178 | inode->index_cnt = 2; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6179 | goto out; |
| 6180 | } |
| 6181 | |
| 6182 | path->slots[0]--; |
| 6183 | |
| 6184 | leaf = path->nodes[0]; |
| 6185 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 6186 | |
Nikolay Borisov | 4c570655 | 2017-02-20 13:50:32 +0200 | [diff] [blame] | 6187 | if (found_key.objectid != btrfs_ino(inode) || |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 6188 | found_key.type != BTRFS_DIR_INDEX_KEY) { |
Nikolay Borisov | 4c570655 | 2017-02-20 13:50:32 +0200 | [diff] [blame] | 6189 | inode->index_cnt = 2; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6190 | goto out; |
| 6191 | } |
| 6192 | |
Nikolay Borisov | 4c570655 | 2017-02-20 13:50:32 +0200 | [diff] [blame] | 6193 | inode->index_cnt = found_key.offset + 1; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6194 | out: |
| 6195 | btrfs_free_path(path); |
| 6196 | return ret; |
| 6197 | } |
| 6198 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 6199 | /* |
| 6200 | * helper to find a free sequence number in a given directory. This current |
| 6201 | * code is very simple, later versions will do smarter things in the btree |
| 6202 | */ |
Nikolay Borisov | 877574e | 2017-02-20 13:50:33 +0200 | [diff] [blame] | 6203 | int btrfs_set_inode_index(struct btrfs_inode *dir, u64 *index) |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6204 | { |
| 6205 | int ret = 0; |
| 6206 | |
Nikolay Borisov | 877574e | 2017-02-20 13:50:33 +0200 | [diff] [blame] | 6207 | if (dir->index_cnt == (u64)-1) { |
| 6208 | ret = btrfs_inode_delayed_dir_index_count(dir); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 6209 | if (ret) { |
| 6210 | ret = btrfs_set_inode_index_count(dir); |
| 6211 | if (ret) |
| 6212 | return ret; |
| 6213 | } |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6214 | } |
| 6215 | |
Nikolay Borisov | 877574e | 2017-02-20 13:50:33 +0200 | [diff] [blame] | 6216 | *index = dir->index_cnt; |
| 6217 | dir->index_cnt++; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6218 | |
| 6219 | return ret; |
| 6220 | } |
| 6221 | |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6222 | static int btrfs_insert_inode_locked(struct inode *inode) |
| 6223 | { |
| 6224 | struct btrfs_iget_args args; |
| 6225 | args.location = &BTRFS_I(inode)->location; |
| 6226 | args.root = BTRFS_I(inode)->root; |
| 6227 | |
| 6228 | return insert_inode_locked4(inode, |
| 6229 | btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root), |
| 6230 | btrfs_find_actor, &args); |
| 6231 | } |
| 6232 | |
Anand Jain | 19aee8d | 2017-07-18 17:37:05 +0800 | [diff] [blame] | 6233 | /* |
| 6234 | * Inherit flags from the parent inode. |
| 6235 | * |
| 6236 | * Currently only the compression flags and the cow flags are inherited. |
| 6237 | */ |
| 6238 | static void btrfs_inherit_iflags(struct inode *inode, struct inode *dir) |
| 6239 | { |
| 6240 | unsigned int flags; |
| 6241 | |
| 6242 | if (!dir) |
| 6243 | return; |
| 6244 | |
| 6245 | flags = BTRFS_I(dir)->flags; |
| 6246 | |
| 6247 | if (flags & BTRFS_INODE_NOCOMPRESS) { |
| 6248 | BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS; |
| 6249 | BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS; |
| 6250 | } else if (flags & BTRFS_INODE_COMPRESS) { |
| 6251 | BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS; |
| 6252 | BTRFS_I(inode)->flags |= BTRFS_INODE_COMPRESS; |
| 6253 | } |
| 6254 | |
| 6255 | if (flags & BTRFS_INODE_NODATACOW) { |
| 6256 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW; |
| 6257 | if (S_ISREG(inode->i_mode)) |
| 6258 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM; |
| 6259 | } |
| 6260 | |
| 6261 | btrfs_update_iflags(inode); |
| 6262 | } |
| 6263 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6264 | static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans, |
| 6265 | struct btrfs_root *root, |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6266 | struct inode *dir, |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 6267 | const char *name, int name_len, |
Al Viro | 175a4eb | 2011-07-26 03:30:54 -0400 | [diff] [blame] | 6268 | u64 ref_objectid, u64 objectid, |
| 6269 | umode_t mode, u64 *index) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6270 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6271 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6272 | struct inode *inode; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6273 | struct btrfs_inode_item *inode_item; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6274 | struct btrfs_key *location; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6275 | struct btrfs_path *path; |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 6276 | struct btrfs_inode_ref *ref; |
| 6277 | struct btrfs_key key[2]; |
| 6278 | u32 sizes[2]; |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 6279 | int nitems = name ? 2 : 1; |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 6280 | unsigned long ptr; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6281 | int ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6282 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6283 | path = btrfs_alloc_path(); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 6284 | if (!path) |
| 6285 | return ERR_PTR(-ENOMEM); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6286 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6287 | inode = new_inode(fs_info->sb); |
Yoshinori Sano | 8fb2764 | 2011-04-09 02:30:07 +0000 | [diff] [blame] | 6288 | if (!inode) { |
| 6289 | btrfs_free_path(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6290 | return ERR_PTR(-ENOMEM); |
Yoshinori Sano | 8fb2764 | 2011-04-09 02:30:07 +0000 | [diff] [blame] | 6291 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6292 | |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 6293 | /* |
Filipe Manana | 5762b5c | 2014-08-01 00:10:32 +0100 | [diff] [blame] | 6294 | * O_TMPFILE, set link count to 0, so that after this point, |
| 6295 | * we fill in an inode item with the correct link count. |
| 6296 | */ |
| 6297 | if (!name) |
| 6298 | set_nlink(inode, 0); |
| 6299 | |
| 6300 | /* |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 6301 | * we have to initialize this early, so we can reclaim the inode |
| 6302 | * number if we fail afterwards in this function. |
| 6303 | */ |
| 6304 | inode->i_ino = objectid; |
| 6305 | |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 6306 | if (dir && name) { |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 6307 | trace_btrfs_inode_request(dir); |
| 6308 | |
Nikolay Borisov | 877574e | 2017-02-20 13:50:33 +0200 | [diff] [blame] | 6309 | ret = btrfs_set_inode_index(BTRFS_I(dir), index); |
Shen Feng | 0977143 | 2009-04-02 16:46:06 -0400 | [diff] [blame] | 6310 | if (ret) { |
Yoshinori Sano | 8fb2764 | 2011-04-09 02:30:07 +0000 | [diff] [blame] | 6311 | btrfs_free_path(path); |
Shen Feng | 0977143 | 2009-04-02 16:46:06 -0400 | [diff] [blame] | 6312 | iput(inode); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6313 | return ERR_PTR(ret); |
Shen Feng | 0977143 | 2009-04-02 16:46:06 -0400 | [diff] [blame] | 6314 | } |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 6315 | } else if (dir) { |
| 6316 | *index = 0; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6317 | } |
| 6318 | /* |
| 6319 | * index_cnt is ignored for everything but a dir, |
| 6320 | * btrfs_get_inode_index_count has an explanation for the magic |
| 6321 | * number |
| 6322 | */ |
| 6323 | BTRFS_I(inode)->index_cnt = 2; |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 6324 | BTRFS_I(inode)->dir_index = *index; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6325 | BTRFS_I(inode)->root = root; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 6326 | BTRFS_I(inode)->generation = trans->transid; |
Josef Bacik | 7619585 | 2010-11-19 02:18:02 +0000 | [diff] [blame] | 6327 | inode->i_generation = BTRFS_I(inode)->generation; |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6328 | |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 6329 | /* |
| 6330 | * We could have gotten an inode number from somebody who was fsynced |
| 6331 | * and then removed in this same transaction, so let's just set full |
| 6332 | * sync since it will be a full sync anyway and this will blow away the |
| 6333 | * old info in the log. |
| 6334 | */ |
| 6335 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags); |
| 6336 | |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 6337 | key[0].objectid = objectid; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 6338 | key[0].type = BTRFS_INODE_ITEM_KEY; |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 6339 | key[0].offset = 0; |
| 6340 | |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 6341 | sizes[0] = sizeof(struct btrfs_inode_item); |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 6342 | |
| 6343 | if (name) { |
| 6344 | /* |
| 6345 | * Start new inodes with an inode_ref. This is slightly more |
| 6346 | * efficient for small numbers of hard links since they will |
| 6347 | * be packed into one item. Extended refs will kick in if we |
| 6348 | * add more hard links than can fit in the ref item. |
| 6349 | */ |
| 6350 | key[1].objectid = objectid; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 6351 | key[1].type = BTRFS_INODE_REF_KEY; |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 6352 | key[1].offset = ref_objectid; |
| 6353 | |
| 6354 | sizes[1] = name_len + sizeof(*ref); |
| 6355 | } |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 6356 | |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6357 | location = &BTRFS_I(inode)->location; |
| 6358 | location->objectid = objectid; |
| 6359 | location->offset = 0; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 6360 | location->type = BTRFS_INODE_ITEM_KEY; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6361 | |
| 6362 | ret = btrfs_insert_inode_locked(inode); |
| 6363 | if (ret < 0) |
| 6364 | goto fail; |
| 6365 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 6366 | path->leave_spinning = 1; |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 6367 | ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems); |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 6368 | if (ret != 0) |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6369 | goto fail_unlock; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6370 | |
Dmitry Monakhov | ecc11fab | 2010-03-04 17:31:47 +0300 | [diff] [blame] | 6371 | inode_init_owner(inode, dir, mode); |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 6372 | inode_set_bytes(inode, 0); |
chandan r | 9cc97d6 | 2012-07-04 12:48:07 +0530 | [diff] [blame] | 6373 | |
Deepa Dinamani | c2050a4 | 2016-09-14 07:48:06 -0700 | [diff] [blame] | 6374 | inode->i_mtime = current_time(inode); |
chandan r | 9cc97d6 | 2012-07-04 12:48:07 +0530 | [diff] [blame] | 6375 | inode->i_atime = inode->i_mtime; |
| 6376 | inode->i_ctime = inode->i_mtime; |
| 6377 | BTRFS_I(inode)->i_otime = inode->i_mtime; |
| 6378 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6379 | inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 6380 | struct btrfs_inode_item); |
David Sterba | b159fa2 | 2016-11-08 18:09:03 +0100 | [diff] [blame] | 6381 | memzero_extent_buffer(path->nodes[0], (unsigned long)inode_item, |
Li Zefan | 293f7e0 | 2012-07-10 00:58:58 -0600 | [diff] [blame] | 6382 | sizeof(*inode_item)); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 6383 | fill_inode_item(trans, path->nodes[0], inode_item, inode); |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 6384 | |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 6385 | if (name) { |
| 6386 | ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1, |
| 6387 | struct btrfs_inode_ref); |
| 6388 | btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len); |
| 6389 | btrfs_set_inode_ref_index(path->nodes[0], ref, *index); |
| 6390 | ptr = (unsigned long)(ref + 1); |
| 6391 | write_extent_buffer(path->nodes[0], name, ptr, name_len); |
| 6392 | } |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 6393 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6394 | btrfs_mark_buffer_dirty(path->nodes[0]); |
| 6395 | btrfs_free_path(path); |
| 6396 | |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 6397 | btrfs_inherit_iflags(inode, dir); |
| 6398 | |
Al Viro | 569254b | 2011-07-24 17:08:40 -0400 | [diff] [blame] | 6399 | if (S_ISREG(mode)) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6400 | if (btrfs_test_opt(fs_info, NODATASUM)) |
Chris Mason | 9427216 | 2009-07-02 12:26:06 -0400 | [diff] [blame] | 6401 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6402 | if (btrfs_test_opt(fs_info, NODATACOW)) |
Josef Bacik | f2bdf9a | 2013-02-21 15:28:28 -0500 | [diff] [blame] | 6403 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW | |
| 6404 | BTRFS_INODE_NODATASUM; |
Chris Mason | 9427216 | 2009-07-02 12:26:06 -0400 | [diff] [blame] | 6405 | } |
| 6406 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6407 | inode_tree_add(inode); |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 6408 | |
| 6409 | trace_btrfs_inode_new(inode); |
Chris Mason | 1973f0f | 2011-06-24 13:13:29 -0400 | [diff] [blame] | 6410 | btrfs_set_inode_last_trans(trans, inode); |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 6411 | |
Alexander Block | 8ea05e3 | 2012-07-25 17:35:53 +0200 | [diff] [blame] | 6412 | btrfs_update_root_times(trans, root); |
| 6413 | |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 6414 | ret = btrfs_inode_inherit_props(trans, inode, dir); |
| 6415 | if (ret) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6416 | btrfs_err(fs_info, |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 6417 | "error inheriting props for ino %llu (root %llu): %d", |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 6418 | btrfs_ino(BTRFS_I(inode)), root->root_key.objectid, ret); |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 6419 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6420 | return inode; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6421 | |
| 6422 | fail_unlock: |
| 6423 | unlock_new_inode(inode); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6424 | fail: |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 6425 | if (dir && name) |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6426 | BTRFS_I(dir)->index_cnt--; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6427 | btrfs_free_path(path); |
Shen Feng | 0977143 | 2009-04-02 16:46:06 -0400 | [diff] [blame] | 6428 | iput(inode); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6429 | return ERR_PTR(ret); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6430 | } |
| 6431 | |
| 6432 | static inline u8 btrfs_inode_type(struct inode *inode) |
| 6433 | { |
| 6434 | return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT]; |
| 6435 | } |
| 6436 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 6437 | /* |
| 6438 | * utility function to add 'inode' into 'parent_inode' with |
| 6439 | * a give name and a given sequence number. |
| 6440 | * if 'add_backref' is true, also insert a backref from the |
| 6441 | * inode to the parent directory. |
| 6442 | */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 6443 | int btrfs_add_link(struct btrfs_trans_handle *trans, |
Nikolay Borisov | db0a669 | 2017-02-20 13:51:08 +0200 | [diff] [blame] | 6444 | struct btrfs_inode *parent_inode, struct btrfs_inode *inode, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 6445 | const char *name, int name_len, int add_backref, u64 index) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6446 | { |
Nikolay Borisov | db0a669 | 2017-02-20 13:51:08 +0200 | [diff] [blame] | 6447 | 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] | 6448 | int ret = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6449 | struct btrfs_key key; |
Nikolay Borisov | db0a669 | 2017-02-20 13:51:08 +0200 | [diff] [blame] | 6450 | struct btrfs_root *root = parent_inode->root; |
| 6451 | u64 ino = btrfs_ino(inode); |
| 6452 | u64 parent_ino = btrfs_ino(parent_inode); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6453 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6454 | if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) { |
Nikolay Borisov | db0a669 | 2017-02-20 13:51:08 +0200 | [diff] [blame] | 6455 | memcpy(&key, &inode->root->root_key, sizeof(key)); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6456 | } else { |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6457 | key.objectid = ino; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 6458 | key.type = BTRFS_INODE_ITEM_KEY; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6459 | key.offset = 0; |
| 6460 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6461 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6462 | if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6463 | ret = btrfs_add_root_ref(trans, fs_info, key.objectid, |
| 6464 | root->root_key.objectid, parent_ino, |
| 6465 | index, name, name_len); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6466 | } else if (add_backref) { |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6467 | ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino, |
| 6468 | parent_ino, index); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6469 | } |
| 6470 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6471 | /* Nothing to clean up yet */ |
| 6472 | if (ret) |
| 6473 | return ret; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 6474 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6475 | ret = btrfs_insert_dir_item(trans, root, name, name_len, |
| 6476 | parent_inode, &key, |
Nikolay Borisov | db0a669 | 2017-02-20 13:51:08 +0200 | [diff] [blame] | 6477 | btrfs_inode_type(&inode->vfs_inode), index); |
Chris Mason | 9c52057 | 2012-12-17 14:26:57 -0500 | [diff] [blame] | 6478 | if (ret == -EEXIST || ret == -EOVERFLOW) |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6479 | goto fail_dir_item; |
| 6480 | else if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 6481 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6482 | return ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6483 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6484 | |
Nikolay Borisov | db0a669 | 2017-02-20 13:51:08 +0200 | [diff] [blame] | 6485 | btrfs_i_size_write(parent_inode, parent_inode->vfs_inode.i_size + |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6486 | name_len * 2); |
Nikolay Borisov | db0a669 | 2017-02-20 13:51:08 +0200 | [diff] [blame] | 6487 | inode_inc_iversion(&parent_inode->vfs_inode); |
| 6488 | parent_inode->vfs_inode.i_mtime = parent_inode->vfs_inode.i_ctime = |
| 6489 | current_time(&parent_inode->vfs_inode); |
| 6490 | ret = btrfs_update_inode(trans, root, &parent_inode->vfs_inode); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6491 | if (ret) |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 6492 | btrfs_abort_transaction(trans, ret); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6493 | return ret; |
Chris Mason | fe66a05 | 2012-02-20 08:40:56 -0500 | [diff] [blame] | 6494 | |
| 6495 | fail_dir_item: |
| 6496 | if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) { |
| 6497 | u64 local_index; |
| 6498 | int err; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6499 | err = btrfs_del_root_ref(trans, fs_info, key.objectid, |
| 6500 | root->root_key.objectid, parent_ino, |
| 6501 | &local_index, name, name_len); |
Chris Mason | fe66a05 | 2012-02-20 08:40:56 -0500 | [diff] [blame] | 6502 | |
| 6503 | } else if (add_backref) { |
| 6504 | u64 local_index; |
| 6505 | int err; |
| 6506 | |
| 6507 | err = btrfs_del_inode_ref(trans, root, name, name_len, |
| 6508 | ino, parent_ino, &local_index); |
| 6509 | } |
| 6510 | return ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6511 | } |
| 6512 | |
| 6513 | static int btrfs_add_nondir(struct btrfs_trans_handle *trans, |
Nikolay Borisov | cef415a | 2017-02-20 13:51:09 +0200 | [diff] [blame] | 6514 | struct btrfs_inode *dir, struct dentry *dentry, |
| 6515 | struct btrfs_inode *inode, int backref, u64 index) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6516 | { |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 6517 | int err = btrfs_add_link(trans, dir, inode, |
| 6518 | dentry->d_name.name, dentry->d_name.len, |
| 6519 | backref, index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6520 | if (err > 0) |
| 6521 | err = -EEXIST; |
| 6522 | return err; |
| 6523 | } |
| 6524 | |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6525 | static int btrfs_mknod(struct inode *dir, struct dentry *dentry, |
Al Viro | 1a67aaf | 2011-07-26 01:52:52 -0400 | [diff] [blame] | 6526 | umode_t mode, dev_t rdev) |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6527 | { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6528 | struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6529 | struct btrfs_trans_handle *trans; |
| 6530 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 6531 | struct inode *inode = NULL; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6532 | int err; |
| 6533 | int drop_inode = 0; |
| 6534 | u64 objectid; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 6535 | u64 index = 0; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6536 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 6537 | /* |
| 6538 | * 2 for inode item and ref |
| 6539 | * 2 for dir items |
| 6540 | * 1 for xattr if selinux is on |
| 6541 | */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6542 | trans = btrfs_start_transaction(root, 5); |
| 6543 | if (IS_ERR(trans)) |
| 6544 | return PTR_ERR(trans); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 6545 | |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 6546 | err = btrfs_find_free_ino(root, &objectid); |
| 6547 | if (err) |
| 6548 | goto out_unlock; |
| 6549 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6550 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 6551 | dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid, |
| 6552 | mode, &index); |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 6553 | if (IS_ERR(inode)) { |
| 6554 | err = PTR_ERR(inode); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6555 | goto out_unlock; |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 6556 | } |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6557 | |
Casey Schaufler | ad19db7 | 2011-12-15 10:09:07 -0500 | [diff] [blame] | 6558 | /* |
| 6559 | * If the active LSM wants to access the inode during |
| 6560 | * d_instantiate it needs these. Smack checks to see |
| 6561 | * if the filesystem supports xattrs by looking at the |
| 6562 | * ops vector. |
| 6563 | */ |
Casey Schaufler | ad19db7 | 2011-12-15 10:09:07 -0500 | [diff] [blame] | 6564 | inode->i_op = &btrfs_special_inode_operations; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6565 | init_special_inode(inode, inode->i_mode, rdev); |
| 6566 | |
| 6567 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6568 | if (err) |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6569 | goto out_unlock_inode; |
| 6570 | |
Nikolay Borisov | cef415a | 2017-02-20 13:51:09 +0200 | [diff] [blame] | 6571 | err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode), |
| 6572 | 0, index); |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6573 | if (err) { |
| 6574 | goto out_unlock_inode; |
| 6575 | } else { |
Yan | 1b4ab1b | 2007-08-29 09:11:44 -0400 | [diff] [blame] | 6576 | btrfs_update_inode(trans, root, inode); |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6577 | unlock_new_inode(inode); |
Al Viro | 08c422c | 2011-12-23 07:58:13 -0500 | [diff] [blame] | 6578 | d_instantiate(dentry, inode); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6579 | } |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6580 | |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6581 | out_unlock: |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 6582 | btrfs_end_transaction(trans); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6583 | btrfs_btree_balance_dirty(fs_info); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6584 | if (drop_inode) { |
| 6585 | inode_dec_link_count(inode); |
| 6586 | iput(inode); |
| 6587 | } |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6588 | return err; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6589 | |
| 6590 | out_unlock_inode: |
| 6591 | drop_inode = 1; |
| 6592 | unlock_new_inode(inode); |
| 6593 | goto out_unlock; |
| 6594 | |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 6595 | } |
| 6596 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6597 | static int btrfs_create(struct inode *dir, struct dentry *dentry, |
Al Viro | ebfc3b4 | 2012-06-10 18:05:36 -0400 | [diff] [blame] | 6598 | umode_t mode, bool excl) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6599 | { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6600 | struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6601 | struct btrfs_trans_handle *trans; |
| 6602 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 6603 | struct inode *inode = NULL; |
Filipe Brandenburger | 43baa57 | 2012-11-30 03:40:09 +0000 | [diff] [blame] | 6604 | int drop_inode_on_err = 0; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6605 | int err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6606 | u64 objectid; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 6607 | u64 index = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6608 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 6609 | /* |
| 6610 | * 2 for inode item and ref |
| 6611 | * 2 for dir items |
| 6612 | * 1 for xattr if selinux is on |
| 6613 | */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6614 | trans = btrfs_start_transaction(root, 5); |
| 6615 | if (IS_ERR(trans)) |
| 6616 | return PTR_ERR(trans); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 6617 | |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 6618 | err = btrfs_find_free_ino(root, &objectid); |
| 6619 | if (err) |
| 6620 | goto out_unlock; |
| 6621 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6622 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 6623 | dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid, |
| 6624 | mode, &index); |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 6625 | if (IS_ERR(inode)) { |
| 6626 | err = PTR_ERR(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6627 | goto out_unlock; |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 6628 | } |
Filipe Brandenburger | 43baa57 | 2012-11-30 03:40:09 +0000 | [diff] [blame] | 6629 | drop_inode_on_err = 1; |
Casey Schaufler | ad19db7 | 2011-12-15 10:09:07 -0500 | [diff] [blame] | 6630 | /* |
| 6631 | * If the active LSM wants to access the inode during |
| 6632 | * d_instantiate it needs these. Smack checks to see |
| 6633 | * if the filesystem supports xattrs by looking at the |
| 6634 | * ops vector. |
| 6635 | */ |
| 6636 | inode->i_fop = &btrfs_file_operations; |
| 6637 | inode->i_op = &btrfs_file_inode_operations; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6638 | inode->i_mapping->a_ops = &btrfs_aops; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6639 | |
| 6640 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); |
| 6641 | if (err) |
| 6642 | goto out_unlock_inode; |
| 6643 | |
| 6644 | err = btrfs_update_inode(trans, root, inode); |
| 6645 | if (err) |
| 6646 | goto out_unlock_inode; |
Casey Schaufler | ad19db7 | 2011-12-15 10:09:07 -0500 | [diff] [blame] | 6647 | |
Nikolay Borisov | cef415a | 2017-02-20 13:51:09 +0200 | [diff] [blame] | 6648 | err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode), |
| 6649 | 0, index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6650 | if (err) |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6651 | goto out_unlock_inode; |
Filipe Brandenburger | 43baa57 | 2012-11-30 03:40:09 +0000 | [diff] [blame] | 6652 | |
Filipe Brandenburger | 43baa57 | 2012-11-30 03:40:09 +0000 | [diff] [blame] | 6653 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6654 | unlock_new_inode(inode); |
Filipe Brandenburger | 43baa57 | 2012-11-30 03:40:09 +0000 | [diff] [blame] | 6655 | d_instantiate(dentry, inode); |
| 6656 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6657 | out_unlock: |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 6658 | btrfs_end_transaction(trans); |
Filipe Brandenburger | 43baa57 | 2012-11-30 03:40:09 +0000 | [diff] [blame] | 6659 | if (err && drop_inode_on_err) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 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; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6665 | |
| 6666 | out_unlock_inode: |
| 6667 | unlock_new_inode(inode); |
| 6668 | goto out_unlock; |
| 6669 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6670 | } |
| 6671 | |
| 6672 | static int btrfs_link(struct dentry *old_dentry, struct inode *dir, |
| 6673 | struct dentry *dentry) |
| 6674 | { |
Filipe Manana | 271dba45 | 2016-01-05 16:24:05 +0000 | [diff] [blame] | 6675 | struct btrfs_trans_handle *trans = NULL; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6676 | struct btrfs_root *root = BTRFS_I(dir)->root; |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 6677 | struct inode *inode = d_inode(old_dentry); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6678 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 6679 | u64 index; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6680 | int err; |
| 6681 | int drop_inode = 0; |
| 6682 | |
TARUISI Hiroaki | 4a8be42 | 2009-11-12 07:14:26 +0000 | [diff] [blame] | 6683 | /* do not allow sys_link's with other subvols of the same device */ |
| 6684 | if (root->objectid != BTRFS_I(inode)->root->objectid) |
Mark Fasheh | 3ab3564 | 2011-03-22 17:20:26 +0000 | [diff] [blame] | 6685 | return -EXDEV; |
TARUISI Hiroaki | 4a8be42 | 2009-11-12 07:14:26 +0000 | [diff] [blame] | 6686 | |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 6687 | if (inode->i_nlink >= BTRFS_LINK_MAX) |
Al Viro | c055e99 | 2011-03-04 17:15:18 +0000 | [diff] [blame] | 6688 | return -EMLINK; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 6689 | |
Nikolay Borisov | 877574e | 2017-02-20 13:50:33 +0200 | [diff] [blame] | 6690 | err = btrfs_set_inode_index(BTRFS_I(dir), &index); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6691 | if (err) |
| 6692 | goto fail; |
| 6693 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6694 | /* |
Miao Xie | 7e6b646 | 2011-02-18 09:21:17 +0000 | [diff] [blame] | 6695 | * 2 items for inode and inode ref |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6696 | * 2 items for dir items |
Miao Xie | 7e6b646 | 2011-02-18 09:21:17 +0000 | [diff] [blame] | 6697 | * 1 item for parent inode |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6698 | */ |
Miao Xie | 7e6b646 | 2011-02-18 09:21:17 +0000 | [diff] [blame] | 6699 | trans = btrfs_start_transaction(root, 5); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6700 | if (IS_ERR(trans)) { |
| 6701 | err = PTR_ERR(trans); |
Filipe Manana | 271dba45 | 2016-01-05 16:24:05 +0000 | [diff] [blame] | 6702 | trans = NULL; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6703 | goto fail; |
| 6704 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6705 | |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 6706 | /* There are several dir indexes for this inode, clear the cache. */ |
| 6707 | BTRFS_I(inode)->dir_index = 0ULL; |
Zach Brown | 8b558c5 | 2013-10-16 12:10:34 -0700 | [diff] [blame] | 6708 | inc_nlink(inode); |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 6709 | inode_inc_iversion(inode); |
Deepa Dinamani | c2050a4 | 2016-09-14 07:48:06 -0700 | [diff] [blame] | 6710 | inode->i_ctime = current_time(inode); |
Al Viro | 7de9c6ee | 2010-10-23 11:11:40 -0400 | [diff] [blame] | 6711 | ihold(inode); |
Josef Bacik | e997615 | 2012-10-11 15:53:56 -0400 | [diff] [blame] | 6712 | set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6713 | |
Nikolay Borisov | cef415a | 2017-02-20 13:51:09 +0200 | [diff] [blame] | 6714 | err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode), |
| 6715 | 1, index); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6716 | |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 6717 | if (err) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6718 | drop_inode = 1; |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 6719 | } else { |
Al Viro | 10d9f30 | 2011-07-16 23:09:10 -0400 | [diff] [blame] | 6720 | struct dentry *parent = dentry->d_parent; |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 6721 | err = btrfs_update_inode(trans, root, inode); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6722 | if (err) |
| 6723 | goto fail; |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 6724 | if (inode->i_nlink == 1) { |
| 6725 | /* |
| 6726 | * If new hard link count is 1, it's a file created |
| 6727 | * with open(2) O_TMPFILE flag. |
| 6728 | */ |
Nikolay Borisov | 3d6ae7b | 2017-02-20 13:50:58 +0200 | [diff] [blame] | 6729 | err = btrfs_orphan_del(trans, BTRFS_I(inode)); |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 6730 | if (err) |
| 6731 | goto fail; |
| 6732 | } |
Al Viro | 08c422c | 2011-12-23 07:58:13 -0500 | [diff] [blame] | 6733 | d_instantiate(dentry, inode); |
Nikolay Borisov | 9ca5fbfb | 2017-01-18 00:31:31 +0200 | [diff] [blame] | 6734 | btrfs_log_new_name(trans, BTRFS_I(inode), NULL, parent); |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 6735 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6736 | |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 6737 | fail: |
Filipe Manana | 271dba45 | 2016-01-05 16:24:05 +0000 | [diff] [blame] | 6738 | if (trans) |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 6739 | btrfs_end_transaction(trans); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6740 | if (drop_inode) { |
| 6741 | inode_dec_link_count(inode); |
| 6742 | iput(inode); |
| 6743 | } |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6744 | btrfs_btree_balance_dirty(fs_info); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6745 | return err; |
| 6746 | } |
| 6747 | |
Al Viro | 18bb1db | 2011-07-26 01:41:39 -0400 | [diff] [blame] | 6748 | 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] | 6749 | { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6750 | struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); |
Chris Mason | b9d8666 | 2008-05-02 16:13:49 -0400 | [diff] [blame] | 6751 | struct inode *inode = NULL; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6752 | struct btrfs_trans_handle *trans; |
| 6753 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 6754 | int err = 0; |
| 6755 | int drop_on_err = 0; |
Chris Mason | b9d8666 | 2008-05-02 16:13:49 -0400 | [diff] [blame] | 6756 | u64 objectid = 0; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 6757 | u64 index = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6758 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 6759 | /* |
| 6760 | * 2 items for inode and ref |
| 6761 | * 2 items for dir items |
| 6762 | * 1 for xattr if selinux is on |
| 6763 | */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6764 | trans = btrfs_start_transaction(root, 5); |
| 6765 | if (IS_ERR(trans)) |
| 6766 | return PTR_ERR(trans); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6767 | |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 6768 | err = btrfs_find_free_ino(root, &objectid); |
| 6769 | if (err) |
| 6770 | goto out_fail; |
| 6771 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6772 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 6773 | dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid, |
| 6774 | S_IFDIR | mode, &index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6775 | if (IS_ERR(inode)) { |
| 6776 | err = PTR_ERR(inode); |
| 6777 | goto out_fail; |
| 6778 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6779 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6780 | drop_on_err = 1; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6781 | /* these must be set before we unlock the inode */ |
| 6782 | inode->i_op = &btrfs_dir_inode_operations; |
| 6783 | inode->i_fop = &btrfs_dir_file_operations; |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 6784 | |
Eric Paris | 2a7dba3 | 2011-02-01 11:05:39 -0500 | [diff] [blame] | 6785 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 6786 | if (err) |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6787 | goto out_fail_inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6788 | |
Nikolay Borisov | 6ef06d2 | 2017-02-20 13:50:34 +0200 | [diff] [blame] | 6789 | btrfs_i_size_write(BTRFS_I(inode), 0); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6790 | err = btrfs_update_inode(trans, root, inode); |
| 6791 | if (err) |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6792 | goto out_fail_inode; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6793 | |
Nikolay Borisov | db0a669 | 2017-02-20 13:51:08 +0200 | [diff] [blame] | 6794 | err = btrfs_add_link(trans, BTRFS_I(dir), BTRFS_I(inode), |
| 6795 | dentry->d_name.name, |
| 6796 | dentry->d_name.len, 0, index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6797 | if (err) |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6798 | goto out_fail_inode; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6799 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6800 | d_instantiate(dentry, inode); |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6801 | /* |
| 6802 | * mkdir is special. We're unlocking after we call d_instantiate |
| 6803 | * to avoid a race with nfsd calling d_instantiate. |
| 6804 | */ |
| 6805 | unlock_new_inode(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6806 | drop_on_err = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6807 | |
| 6808 | out_fail: |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 6809 | btrfs_end_transaction(trans); |
Wang Shilong | c7cfb8a | 2014-12-24 14:45:30 +0800 | [diff] [blame] | 6810 | if (drop_on_err) { |
| 6811 | inode_dec_link_count(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6812 | iput(inode); |
Wang Shilong | c7cfb8a | 2014-12-24 14:45:30 +0800 | [diff] [blame] | 6813 | } |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6814 | btrfs_btree_balance_dirty(fs_info); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6815 | return err; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 6816 | |
| 6817 | out_fail_inode: |
| 6818 | unlock_new_inode(inode); |
| 6819 | goto out_fail; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6820 | } |
| 6821 | |
Qu Wenruo | e6c4efd | 2014-09-17 11:53:35 +0800 | [diff] [blame] | 6822 | /* Find next extent map of a given extent map, caller needs to ensure locks */ |
| 6823 | static struct extent_map *next_extent_map(struct extent_map *em) |
| 6824 | { |
| 6825 | struct rb_node *next; |
| 6826 | |
| 6827 | next = rb_next(&em->rb_node); |
| 6828 | if (!next) |
| 6829 | return NULL; |
| 6830 | return container_of(next, struct extent_map, rb_node); |
| 6831 | } |
| 6832 | |
| 6833 | static struct extent_map *prev_extent_map(struct extent_map *em) |
| 6834 | { |
| 6835 | struct rb_node *prev; |
| 6836 | |
| 6837 | prev = rb_prev(&em->rb_node); |
| 6838 | if (!prev) |
| 6839 | return NULL; |
| 6840 | return container_of(prev, struct extent_map, rb_node); |
| 6841 | } |
| 6842 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 6843 | /* helper for btfs_get_extent. Given an existing extent in the tree, |
Qu Wenruo | e6c4efd | 2014-09-17 11:53:35 +0800 | [diff] [blame] | 6844 | * the existing extent is the nearest extent to map_start, |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 6845 | * 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] | 6846 | * the best fitted new extent into the tree. |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 6847 | */ |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 6848 | static int merge_extent_mapping(struct extent_map_tree *em_tree, |
| 6849 | struct extent_map *existing, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6850 | struct extent_map *em, |
Qu Wenruo | 51f395a | 2014-08-08 13:06:20 +0800 | [diff] [blame] | 6851 | u64 map_start) |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 6852 | { |
Qu Wenruo | e6c4efd | 2014-09-17 11:53:35 +0800 | [diff] [blame] | 6853 | struct extent_map *prev; |
| 6854 | struct extent_map *next; |
| 6855 | u64 start; |
| 6856 | u64 end; |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 6857 | u64 start_diff; |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 6858 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6859 | BUG_ON(map_start < em->start || map_start >= extent_map_end(em)); |
Qu Wenruo | e6c4efd | 2014-09-17 11:53:35 +0800 | [diff] [blame] | 6860 | |
| 6861 | if (existing->start > map_start) { |
| 6862 | next = existing; |
| 6863 | prev = prev_extent_map(next); |
| 6864 | } else { |
| 6865 | prev = existing; |
| 6866 | next = next_extent_map(prev); |
| 6867 | } |
| 6868 | |
| 6869 | start = prev ? extent_map_end(prev) : em->start; |
| 6870 | start = max_t(u64, start, em->start); |
| 6871 | end = next ? next->start : extent_map_end(em); |
| 6872 | end = min_t(u64, end, extent_map_end(em)); |
| 6873 | start_diff = start - em->start; |
| 6874 | em->start = start; |
| 6875 | em->len = end - start; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6876 | if (em->block_start < EXTENT_MAP_LAST_BYTE && |
| 6877 | !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6878 | em->block_start += start_diff; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6879 | em->block_len -= start_diff; |
| 6880 | } |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 6881 | return add_extent_mapping(em_tree, em, 0); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 6882 | } |
| 6883 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6884 | static noinline int uncompress_inline(struct btrfs_path *path, |
Byongho Lee | e40da0e | 2015-05-19 23:46:45 +0900 | [diff] [blame] | 6885 | struct page *page, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6886 | size_t pg_offset, u64 extent_offset, |
| 6887 | struct btrfs_file_extent_item *item) |
| 6888 | { |
| 6889 | int ret; |
| 6890 | struct extent_buffer *leaf = path->nodes[0]; |
| 6891 | char *tmp; |
| 6892 | size_t max_size; |
| 6893 | unsigned long inline_size; |
| 6894 | unsigned long ptr; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 6895 | int compress_type; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6896 | |
| 6897 | WARN_ON(pg_offset != 0); |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 6898 | compress_type = btrfs_file_extent_compression(leaf, item); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6899 | max_size = btrfs_file_extent_ram_bytes(leaf, item); |
| 6900 | inline_size = btrfs_file_extent_inline_item_len(leaf, |
Ross Kirk | dd3cc16 | 2013-09-16 15:58:09 +0100 | [diff] [blame] | 6901 | btrfs_item_nr(path->slots[0])); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6902 | tmp = kmalloc(inline_size, GFP_NOFS); |
Tsutomu Itoh | 8d41371 | 2011-04-25 19:43:52 -0400 | [diff] [blame] | 6903 | if (!tmp) |
| 6904 | return -ENOMEM; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6905 | ptr = btrfs_file_extent_inline_start(item); |
| 6906 | |
| 6907 | read_extent_buffer(leaf, tmp, ptr, inline_size); |
| 6908 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 6909 | max_size = min_t(unsigned long, PAGE_SIZE, max_size); |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 6910 | ret = btrfs_decompress(compress_type, tmp, page, |
| 6911 | extent_offset, inline_size, max_size); |
Zygo Blaxell | e1699d2 | 2017-03-10 16:45:44 -0500 | [diff] [blame] | 6912 | |
| 6913 | /* |
| 6914 | * decompression code contains a memset to fill in any space between the end |
| 6915 | * of the uncompressed data and the end of max_size in case the decompressed |
| 6916 | * data ends up shorter than ram_bytes. That doesn't cover the hole between |
| 6917 | * the end of an inline extent and the beginning of the next block, so we |
| 6918 | * cover that region here. |
| 6919 | */ |
| 6920 | |
| 6921 | if (max_size + pg_offset < PAGE_SIZE) { |
| 6922 | char *map = kmap(page); |
| 6923 | memset(map + pg_offset + max_size, 0, PAGE_SIZE - max_size - pg_offset); |
| 6924 | kunmap(page); |
| 6925 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6926 | kfree(tmp); |
Zach Brown | 166ae5a | 2014-05-09 17:15:10 -0400 | [diff] [blame] | 6927 | return ret; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6928 | } |
| 6929 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 6930 | /* |
| 6931 | * 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] | 6932 | * the ugly parts come from merging extents from the disk with the in-ram |
| 6933 | * representation. This gets more complex because of the data=ordered code, |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 6934 | * where the in-ram extents might be locked pending data=ordered completion. |
| 6935 | * |
| 6936 | * This also copies inline extents directly into the page. |
| 6937 | */ |
Nikolay Borisov | fc4f21b1 | 2017-02-20 13:51:06 +0200 | [diff] [blame] | 6938 | struct extent_map *btrfs_get_extent(struct btrfs_inode *inode, |
| 6939 | struct page *page, |
| 6940 | size_t pg_offset, u64 start, u64 len, |
| 6941 | int create) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6942 | { |
Nikolay Borisov | fc4f21b1 | 2017-02-20 13:51:06 +0200 | [diff] [blame] | 6943 | 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] | 6944 | int ret; |
| 6945 | int err = 0; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6946 | u64 extent_start = 0; |
| 6947 | u64 extent_end = 0; |
Nikolay Borisov | fc4f21b1 | 2017-02-20 13:51:06 +0200 | [diff] [blame] | 6948 | u64 objectid = btrfs_ino(inode); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6949 | u32 found_type; |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 6950 | struct btrfs_path *path = NULL; |
Nikolay Borisov | fc4f21b1 | 2017-02-20 13:51:06 +0200 | [diff] [blame] | 6951 | struct btrfs_root *root = inode->root; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6952 | struct btrfs_file_extent_item *item; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6953 | struct extent_buffer *leaf; |
| 6954 | struct btrfs_key found_key; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6955 | struct extent_map *em = NULL; |
Nikolay Borisov | fc4f21b1 | 2017-02-20 13:51:06 +0200 | [diff] [blame] | 6956 | struct extent_map_tree *em_tree = &inode->extent_tree; |
| 6957 | struct extent_io_tree *io_tree = &inode->io_tree; |
Filipe Manana | 7ffbb59 | 2014-06-09 03:48:05 +0100 | [diff] [blame] | 6958 | const bool new_inline = !page || create; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6959 | |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 6960 | read_lock(&em_tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6961 | em = lookup_extent_mapping(em_tree, start, len); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 6962 | if (em) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6963 | em->bdev = fs_info->fs_devices->latest_bdev; |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 6964 | read_unlock(&em_tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6965 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6966 | if (em) { |
Chris Mason | e1c4b74 | 2008-04-22 13:26:46 -0400 | [diff] [blame] | 6967 | if (em->start > start || em->start + em->len <= start) |
| 6968 | free_extent_map(em); |
| 6969 | else if (em->block_start == EXTENT_MAP_INLINE && page) |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 6970 | free_extent_map(em); |
| 6971 | else |
| 6972 | goto out; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6973 | } |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 6974 | em = alloc_extent_map(); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6975 | if (!em) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6976 | err = -ENOMEM; |
| 6977 | goto out; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6978 | } |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6979 | em->bdev = fs_info->fs_devices->latest_bdev; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6980 | em->start = EXTENT_MAP_HOLE; |
Chris Mason | 445a694 | 2008-11-10 11:53:33 -0500 | [diff] [blame] | 6981 | em->orig_start = EXTENT_MAP_HOLE; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6982 | em->len = (u64)-1; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6983 | em->block_len = (u64)-1; |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 6984 | |
| 6985 | if (!path) { |
| 6986 | path = btrfs_alloc_path(); |
Josef Bacik | 026fd31 | 2011-05-13 10:32:11 -0400 | [diff] [blame] | 6987 | if (!path) { |
| 6988 | err = -ENOMEM; |
| 6989 | goto out; |
| 6990 | } |
| 6991 | /* |
| 6992 | * Chances are we'll be called again, so go ahead and do |
| 6993 | * readahead |
| 6994 | */ |
David Sterba | e4058b5 | 2015-11-27 16:31:35 +0100 | [diff] [blame] | 6995 | path->reada = READA_FORWARD; |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 6996 | } |
| 6997 | |
Nikolay Borisov | 5c9a702 | 2017-12-01 11:19:40 +0200 | [diff] [blame] | 6998 | ret = btrfs_lookup_file_extent(NULL, root, path, objectid, start, 0); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6999 | if (ret < 0) { |
| 7000 | err = ret; |
| 7001 | goto out; |
| 7002 | } |
| 7003 | |
| 7004 | if (ret != 0) { |
| 7005 | if (path->slots[0] == 0) |
| 7006 | goto not_found; |
| 7007 | path->slots[0]--; |
| 7008 | } |
| 7009 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 7010 | leaf = path->nodes[0]; |
| 7011 | item = btrfs_item_ptr(leaf, path->slots[0], |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7012 | struct btrfs_file_extent_item); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7013 | /* are we inside the extent that was found? */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 7014 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 7015 | found_type = found_key.type; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 7016 | if (found_key.objectid != objectid || |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7017 | found_type != BTRFS_EXTENT_DATA_KEY) { |
Josef Bacik | 25a5034 | 2013-10-14 12:08:38 -0400 | [diff] [blame] | 7018 | /* |
| 7019 | * If we backup past the first extent we want to move forward |
| 7020 | * and see if there is an extent in front of us, otherwise we'll |
| 7021 | * say there is a hole for our whole search range which can |
| 7022 | * cause problems. |
| 7023 | */ |
| 7024 | extent_end = start; |
| 7025 | goto next; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7026 | } |
| 7027 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 7028 | found_type = btrfs_file_extent_type(leaf, item); |
| 7029 | extent_start = found_key.offset; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7030 | if (found_type == BTRFS_FILE_EXTENT_REG || |
| 7031 | found_type == BTRFS_FILE_EXTENT_PREALLOC) { |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7032 | extent_end = extent_start + |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 7033 | btrfs_file_extent_num_bytes(leaf, item); |
Liu Bo | 09ed2f1 | 2017-03-10 11:09:48 -0800 | [diff] [blame] | 7034 | |
| 7035 | trace_btrfs_get_extent_show_fi_regular(inode, leaf, item, |
| 7036 | extent_start); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 7037 | } else if (found_type == BTRFS_FILE_EXTENT_INLINE) { |
| 7038 | size_t size; |
Chris Mason | 514ac8a | 2014-01-03 21:07:00 -0800 | [diff] [blame] | 7039 | size = btrfs_file_extent_inline_len(leaf, path->slots[0], item); |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 7040 | extent_end = ALIGN(extent_start + size, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7041 | fs_info->sectorsize); |
Liu Bo | 09ed2f1 | 2017-03-10 11:09:48 -0800 | [diff] [blame] | 7042 | |
| 7043 | trace_btrfs_get_extent_show_fi_inline(inode, leaf, item, |
| 7044 | path->slots[0], |
| 7045 | extent_start); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 7046 | } |
Josef Bacik | 25a5034 | 2013-10-14 12:08:38 -0400 | [diff] [blame] | 7047 | next: |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 7048 | if (start >= extent_end) { |
| 7049 | path->slots[0]++; |
| 7050 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { |
| 7051 | ret = btrfs_next_leaf(root, path); |
| 7052 | if (ret < 0) { |
| 7053 | err = ret; |
| 7054 | goto out; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7055 | } |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 7056 | if (ret > 0) |
| 7057 | goto not_found; |
| 7058 | leaf = path->nodes[0]; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7059 | } |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 7060 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 7061 | if (found_key.objectid != objectid || |
| 7062 | found_key.type != BTRFS_EXTENT_DATA_KEY) |
| 7063 | goto not_found; |
| 7064 | if (start + len <= found_key.offset) |
| 7065 | goto not_found; |
Wang Shilong | e2eca69 | 2014-07-17 11:44:14 +0800 | [diff] [blame] | 7066 | if (start > found_key.offset) |
| 7067 | goto next; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 7068 | em->start = start; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 7069 | em->orig_start = start; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 7070 | em->len = found_key.offset - start; |
| 7071 | goto not_found_em; |
| 7072 | } |
| 7073 | |
Nikolay Borisov | fc4f21b1 | 2017-02-20 13:51:06 +0200 | [diff] [blame] | 7074 | btrfs_extent_item_to_extent_map(inode, path, item, |
Nikolay Borisov | 9cdc512 | 2017-02-20 13:51:02 +0200 | [diff] [blame] | 7075 | new_inline, em); |
Filipe Manana | 7ffbb59 | 2014-06-09 03:48:05 +0100 | [diff] [blame] | 7076 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7077 | if (found_type == BTRFS_FILE_EXTENT_REG || |
| 7078 | found_type == BTRFS_FILE_EXTENT_PREALLOC) { |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7079 | goto insert; |
| 7080 | } else if (found_type == BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 7081 | unsigned long ptr; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7082 | char *map; |
Chris Mason | 3326d1b | 2007-10-15 16:18:25 -0400 | [diff] [blame] | 7083 | size_t size; |
| 7084 | size_t extent_offset; |
| 7085 | size_t copy_size; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7086 | |
Filipe Manana | 7ffbb59 | 2014-06-09 03:48:05 +0100 | [diff] [blame] | 7087 | if (new_inline) |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 7088 | goto out; |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 7089 | |
Chris Mason | 514ac8a | 2014-01-03 21:07:00 -0800 | [diff] [blame] | 7090 | size = btrfs_file_extent_inline_len(leaf, path->slots[0], item); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 7091 | extent_offset = page_offset(page) + pg_offset - extent_start; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 7092 | copy_size = min_t(u64, PAGE_SIZE - pg_offset, |
| 7093 | size - extent_offset); |
Chris Mason | 3326d1b | 2007-10-15 16:18:25 -0400 | [diff] [blame] | 7094 | em->start = extent_start + extent_offset; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7095 | em->len = ALIGN(copy_size, fs_info->sectorsize); |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 7096 | em->orig_block_len = em->len; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 7097 | em->orig_start = em->start; |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 7098 | ptr = btrfs_file_extent_inline_start(item) + extent_offset; |
Edmund Nadolski | bf46f52 | 2017-11-20 13:24:49 -0700 | [diff] [blame] | 7099 | if (!PageUptodate(page)) { |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 7100 | if (btrfs_file_extent_compression(leaf, item) != |
| 7101 | BTRFS_COMPRESS_NONE) { |
Byongho Lee | e40da0e | 2015-05-19 23:46:45 +0900 | [diff] [blame] | 7102 | ret = uncompress_inline(path, page, pg_offset, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 7103 | extent_offset, item); |
Zach Brown | 166ae5a | 2014-05-09 17:15:10 -0400 | [diff] [blame] | 7104 | if (ret) { |
| 7105 | err = ret; |
| 7106 | goto out; |
| 7107 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 7108 | } else { |
| 7109 | map = kmap(page); |
| 7110 | read_extent_buffer(leaf, map + pg_offset, ptr, |
| 7111 | copy_size); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 7112 | if (pg_offset + copy_size < PAGE_SIZE) { |
Chris Mason | 93c82d5 | 2009-09-11 12:36:29 -0400 | [diff] [blame] | 7113 | memset(map + pg_offset + copy_size, 0, |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 7114 | PAGE_SIZE - pg_offset - |
Chris Mason | 93c82d5 | 2009-09-11 12:36:29 -0400 | [diff] [blame] | 7115 | copy_size); |
| 7116 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 7117 | kunmap(page); |
| 7118 | } |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 7119 | flush_dcache_page(page); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7120 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 7121 | set_extent_uptodate(io_tree, em->start, |
Arne Jansen | 507903b | 2011-04-06 10:02:20 +0000 | [diff] [blame] | 7122 | extent_map_end(em) - 1, NULL, GFP_NOFS); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7123 | goto insert; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7124 | } |
| 7125 | not_found: |
| 7126 | em->start = start; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 7127 | em->orig_start = start; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 7128 | em->len = len; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7129 | not_found_em: |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 7130 | em->block_start = EXTENT_MAP_HOLE; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7131 | insert: |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 7132 | btrfs_release_path(path); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 7133 | if (em->start > start || extent_map_end(em) <= start) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7134 | btrfs_err(fs_info, |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 7135 | "bad extent! em: [%llu %llu] passed [%llu %llu]", |
| 7136 | em->start, em->len, start, len); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7137 | err = -EIO; |
| 7138 | goto out; |
| 7139 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 7140 | |
| 7141 | err = 0; |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 7142 | write_lock(&em_tree->lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 7143 | ret = add_extent_mapping(em_tree, em, 0); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 7144 | /* it is possible that someone inserted the extent into the tree |
| 7145 | * while we had the lock dropped. It is also possible that |
| 7146 | * an overlapping map exists in the tree |
| 7147 | */ |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7148 | if (ret == -EEXIST) { |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 7149 | struct extent_map *existing; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7150 | |
| 7151 | ret = 0; |
| 7152 | |
Qu Wenruo | e6c4efd | 2014-09-17 11:53:35 +0800 | [diff] [blame] | 7153 | existing = search_extent_mapping(em_tree, start, len); |
| 7154 | /* |
| 7155 | * existing will always be non-NULL, since there must be |
| 7156 | * extent causing the -EEXIST. |
| 7157 | */ |
Chris Mason | 8dff9c8 | 2015-09-19 11:28:25 -0700 | [diff] [blame] | 7158 | if (existing->start == em->start && |
Omar Sandoval | 8e2bd3b | 2016-11-09 15:26:50 -0800 | [diff] [blame] | 7159 | extent_map_end(existing) >= extent_map_end(em) && |
Chris Mason | 8dff9c8 | 2015-09-19 11:28:25 -0700 | [diff] [blame] | 7160 | em->block_start == existing->block_start) { |
| 7161 | /* |
Omar Sandoval | 8e2bd3b | 2016-11-09 15:26:50 -0800 | [diff] [blame] | 7162 | * The existing extent map already encompasses the |
| 7163 | * entire extent map we tried to add. |
Chris Mason | 8dff9c8 | 2015-09-19 11:28:25 -0700 | [diff] [blame] | 7164 | */ |
| 7165 | free_extent_map(em); |
| 7166 | em = existing; |
| 7167 | err = 0; |
| 7168 | |
| 7169 | } else if (start >= extent_map_end(existing) || |
Qu Wenruo | 32be3a1 | 2014-09-22 09:13:03 +0800 | [diff] [blame] | 7170 | start <= existing->start) { |
Qu Wenruo | e6c4efd | 2014-09-17 11:53:35 +0800 | [diff] [blame] | 7171 | /* |
| 7172 | * The existing extent map is the one nearest to |
| 7173 | * the [start, start + len) range which overlaps |
| 7174 | */ |
| 7175 | err = merge_extent_mapping(em_tree, existing, |
| 7176 | em, start); |
Chris Mason | e1c4b74 | 2008-04-22 13:26:46 -0400 | [diff] [blame] | 7177 | free_extent_map(existing); |
Qu Wenruo | e6c4efd | 2014-09-17 11:53:35 +0800 | [diff] [blame] | 7178 | if (err) { |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 7179 | free_extent_map(em); |
| 7180 | em = NULL; |
| 7181 | } |
| 7182 | } else { |
| 7183 | free_extent_map(em); |
| 7184 | em = existing; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7185 | err = 0; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7186 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7187 | } |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 7188 | write_unlock(&em_tree->lock); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7189 | out: |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 7190 | |
Nikolay Borisov | fc4f21b1 | 2017-02-20 13:51:06 +0200 | [diff] [blame] | 7191 | trace_btrfs_get_extent(root, inode, em); |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 7192 | |
Tsutomu Itoh | 527afb4 | 2015-08-19 14:55:00 +0900 | [diff] [blame] | 7193 | btrfs_free_path(path); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7194 | if (err) { |
| 7195 | free_extent_map(em); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7196 | return ERR_PTR(err); |
| 7197 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 7198 | BUG_ON(!em); /* Error is always set */ |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7199 | return em; |
| 7200 | } |
| 7201 | |
Nikolay Borisov | fc4f21b1 | 2017-02-20 13:51:06 +0200 | [diff] [blame] | 7202 | struct extent_map *btrfs_get_extent_fiemap(struct btrfs_inode *inode, |
| 7203 | struct page *page, |
| 7204 | size_t pg_offset, u64 start, u64 len, |
| 7205 | int create) |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 7206 | { |
| 7207 | struct extent_map *em; |
| 7208 | struct extent_map *hole_em = NULL; |
| 7209 | u64 range_start = start; |
| 7210 | u64 end; |
| 7211 | u64 found; |
| 7212 | u64 found_end; |
| 7213 | int err = 0; |
| 7214 | |
| 7215 | em = btrfs_get_extent(inode, page, pg_offset, start, len, create); |
| 7216 | if (IS_ERR(em)) |
| 7217 | return em; |
Dan Carpenter | 9986277 | 2017-04-11 11:57:15 +0300 | [diff] [blame] | 7218 | /* |
| 7219 | * If our em maps to: |
| 7220 | * - a hole or |
| 7221 | * - a pre-alloc extent, |
| 7222 | * there might actually be delalloc bytes behind it. |
| 7223 | */ |
| 7224 | if (em->block_start != EXTENT_MAP_HOLE && |
| 7225 | !test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) |
| 7226 | return em; |
| 7227 | else |
| 7228 | hole_em = em; |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 7229 | |
| 7230 | /* check to see if we've wrapped (len == -1 or similar) */ |
| 7231 | end = start + len; |
| 7232 | if (end < start) |
| 7233 | end = (u64)-1; |
| 7234 | else |
| 7235 | end -= 1; |
| 7236 | |
| 7237 | em = NULL; |
| 7238 | |
| 7239 | /* ok, we didn't find anything, lets look for delalloc */ |
Nikolay Borisov | fc4f21b1 | 2017-02-20 13:51:06 +0200 | [diff] [blame] | 7240 | found = count_range_bits(&inode->io_tree, &range_start, |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 7241 | end, len, EXTENT_DELALLOC, 1); |
| 7242 | found_end = range_start + found; |
| 7243 | if (found_end < range_start) |
| 7244 | found_end = (u64)-1; |
| 7245 | |
| 7246 | /* |
| 7247 | * we didn't find anything useful, return |
| 7248 | * the original results from get_extent() |
| 7249 | */ |
| 7250 | if (range_start > end || found_end <= start) { |
| 7251 | em = hole_em; |
| 7252 | hole_em = NULL; |
| 7253 | goto out; |
| 7254 | } |
| 7255 | |
| 7256 | /* adjust the range_start to make sure it doesn't |
| 7257 | * go backwards from the start they passed in |
| 7258 | */ |
Dulshani Gunawardhana | 6787125 | 2013-10-31 10:33:04 +0530 | [diff] [blame] | 7259 | range_start = max(start, range_start); |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 7260 | found = found_end - range_start; |
| 7261 | |
| 7262 | if (found > 0) { |
| 7263 | u64 hole_start = start; |
| 7264 | u64 hole_len = len; |
| 7265 | |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 7266 | em = alloc_extent_map(); |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 7267 | if (!em) { |
| 7268 | err = -ENOMEM; |
| 7269 | goto out; |
| 7270 | } |
| 7271 | /* |
| 7272 | * when btrfs_get_extent can't find anything it |
| 7273 | * returns one huge hole |
| 7274 | * |
| 7275 | * make sure what it found really fits our range, and |
| 7276 | * adjust to make sure it is based on the start from |
| 7277 | * the caller |
| 7278 | */ |
| 7279 | if (hole_em) { |
| 7280 | u64 calc_end = extent_map_end(hole_em); |
| 7281 | |
| 7282 | if (calc_end <= start || (hole_em->start > end)) { |
| 7283 | free_extent_map(hole_em); |
| 7284 | hole_em = NULL; |
| 7285 | } else { |
| 7286 | hole_start = max(hole_em->start, start); |
| 7287 | hole_len = calc_end - hole_start; |
| 7288 | } |
| 7289 | } |
| 7290 | em->bdev = NULL; |
| 7291 | if (hole_em && range_start > hole_start) { |
| 7292 | /* our hole starts before our delalloc, so we |
| 7293 | * have to return just the parts of the hole |
| 7294 | * that go until the delalloc starts |
| 7295 | */ |
| 7296 | em->len = min(hole_len, |
| 7297 | range_start - hole_start); |
| 7298 | em->start = hole_start; |
| 7299 | em->orig_start = hole_start; |
| 7300 | /* |
| 7301 | * don't adjust block start at all, |
| 7302 | * it is fixed at EXTENT_MAP_HOLE |
| 7303 | */ |
| 7304 | em->block_start = hole_em->block_start; |
| 7305 | em->block_len = hole_len; |
Liu Bo | f9e4fb5 | 2013-01-07 10:10:12 +0000 | [diff] [blame] | 7306 | if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags)) |
| 7307 | set_bit(EXTENT_FLAG_PREALLOC, &em->flags); |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 7308 | } else { |
| 7309 | em->start = range_start; |
| 7310 | em->len = found; |
| 7311 | em->orig_start = range_start; |
| 7312 | em->block_start = EXTENT_MAP_DELALLOC; |
| 7313 | em->block_len = found; |
| 7314 | } |
Nikolay Borisov | bf8d32b | 2017-12-01 11:19:43 +0200 | [diff] [blame] | 7315 | } else { |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 7316 | return hole_em; |
| 7317 | } |
| 7318 | out: |
| 7319 | |
| 7320 | free_extent_map(hole_em); |
| 7321 | if (err) { |
| 7322 | free_extent_map(em); |
| 7323 | return ERR_PTR(err); |
| 7324 | } |
| 7325 | return em; |
| 7326 | } |
| 7327 | |
Filipe Manana | 5f9a8a5 | 2016-05-12 13:53:36 +0100 | [diff] [blame] | 7328 | static struct extent_map *btrfs_create_dio_extent(struct inode *inode, |
| 7329 | const u64 start, |
| 7330 | const u64 len, |
| 7331 | const u64 orig_start, |
| 7332 | const u64 block_start, |
| 7333 | const u64 block_len, |
| 7334 | const u64 orig_block_len, |
| 7335 | const u64 ram_bytes, |
| 7336 | const int type) |
| 7337 | { |
| 7338 | struct extent_map *em = NULL; |
| 7339 | int ret; |
| 7340 | |
Filipe Manana | 5f9a8a5 | 2016-05-12 13:53:36 +0100 | [diff] [blame] | 7341 | if (type != BTRFS_ORDERED_NOCOW) { |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 7342 | em = create_io_em(inode, start, len, orig_start, |
| 7343 | block_start, block_len, orig_block_len, |
| 7344 | ram_bytes, |
| 7345 | BTRFS_COMPRESS_NONE, /* compress_type */ |
| 7346 | type); |
Filipe Manana | 5f9a8a5 | 2016-05-12 13:53:36 +0100 | [diff] [blame] | 7347 | if (IS_ERR(em)) |
| 7348 | goto out; |
| 7349 | } |
| 7350 | ret = btrfs_add_ordered_extent_dio(inode, start, block_start, |
| 7351 | len, block_len, type); |
| 7352 | if (ret) { |
| 7353 | if (em) { |
| 7354 | free_extent_map(em); |
Nikolay Borisov | dcdbc05 | 2017-02-20 13:50:45 +0200 | [diff] [blame] | 7355 | btrfs_drop_extent_cache(BTRFS_I(inode), start, |
Filipe Manana | 5f9a8a5 | 2016-05-12 13:53:36 +0100 | [diff] [blame] | 7356 | start + len - 1, 0); |
| 7357 | } |
| 7358 | em = ERR_PTR(ret); |
| 7359 | } |
| 7360 | out: |
Filipe Manana | 5f9a8a5 | 2016-05-12 13:53:36 +0100 | [diff] [blame] | 7361 | |
| 7362 | return em; |
| 7363 | } |
| 7364 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7365 | static struct extent_map *btrfs_new_extent_direct(struct inode *inode, |
| 7366 | u64 start, u64 len) |
| 7367 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7368 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7369 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 7370 | struct extent_map *em; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7371 | struct btrfs_key ins; |
| 7372 | u64 alloc_hint; |
| 7373 | int ret; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7374 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7375 | alloc_hint = get_extent_allocation_hint(inode, start, len); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7376 | ret = btrfs_reserve_extent(root, len, len, fs_info->sectorsize, |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 7377 | 0, alloc_hint, &ins, 1, 1); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 7378 | if (ret) |
| 7379 | return ERR_PTR(ret); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7380 | |
Filipe Manana | 5f9a8a5 | 2016-05-12 13:53:36 +0100 | [diff] [blame] | 7381 | em = btrfs_create_dio_extent(inode, start, ins.offset, start, |
| 7382 | ins.objectid, ins.offset, ins.offset, |
Liu Bo | 6288d6e | 2017-02-21 12:12:58 -0800 | [diff] [blame] | 7383 | ins.offset, BTRFS_ORDERED_REGULAR); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7384 | btrfs_dec_block_group_reservations(fs_info, ins.objectid); |
Filipe Manana | 5f9a8a5 | 2016-05-12 13:53:36 +0100 | [diff] [blame] | 7385 | if (IS_ERR(em)) |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 7386 | btrfs_free_reserved_extent(fs_info, ins.objectid, |
| 7387 | ins.offset, 1); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 7388 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7389 | return em; |
| 7390 | } |
| 7391 | |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7392 | /* |
| 7393 | * returns 1 when the nocow is safe, < 1 on error, 0 if the |
| 7394 | * block must be cow'd |
| 7395 | */ |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 7396 | noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len, |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 7397 | u64 *orig_start, u64 *orig_block_len, |
| 7398 | u64 *ram_bytes) |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7399 | { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 7400 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7401 | struct btrfs_path *path; |
| 7402 | int ret; |
| 7403 | struct extent_buffer *leaf; |
| 7404 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Miao Xie | 7b2b708 | 2014-02-27 13:58:05 +0800 | [diff] [blame] | 7405 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7406 | struct btrfs_file_extent_item *fi; |
| 7407 | struct btrfs_key key; |
| 7408 | u64 disk_bytenr; |
| 7409 | u64 backref_offset; |
| 7410 | u64 extent_end; |
| 7411 | u64 num_bytes; |
| 7412 | int slot; |
| 7413 | int found_type; |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 7414 | bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW); |
Miao Xie | e77751a | 2013-12-27 21:11:50 +0800 | [diff] [blame] | 7415 | |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7416 | path = btrfs_alloc_path(); |
| 7417 | if (!path) |
| 7418 | return -ENOMEM; |
| 7419 | |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 7420 | ret = btrfs_lookup_file_extent(NULL, root, path, |
| 7421 | btrfs_ino(BTRFS_I(inode)), offset, 0); |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7422 | if (ret < 0) |
| 7423 | goto out; |
| 7424 | |
| 7425 | slot = path->slots[0]; |
| 7426 | if (ret == 1) { |
| 7427 | if (slot == 0) { |
| 7428 | /* can't find the item, must cow */ |
| 7429 | ret = 0; |
| 7430 | goto out; |
| 7431 | } |
| 7432 | slot--; |
| 7433 | } |
| 7434 | ret = 0; |
| 7435 | leaf = path->nodes[0]; |
| 7436 | btrfs_item_key_to_cpu(leaf, &key, slot); |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 7437 | if (key.objectid != btrfs_ino(BTRFS_I(inode)) || |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7438 | key.type != BTRFS_EXTENT_DATA_KEY) { |
| 7439 | /* not our file or wrong item type, must cow */ |
| 7440 | goto out; |
| 7441 | } |
| 7442 | |
| 7443 | if (key.offset > offset) { |
| 7444 | /* Wrong offset, must cow */ |
| 7445 | goto out; |
| 7446 | } |
| 7447 | |
| 7448 | fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item); |
| 7449 | found_type = btrfs_file_extent_type(leaf, fi); |
| 7450 | if (found_type != BTRFS_FILE_EXTENT_REG && |
| 7451 | found_type != BTRFS_FILE_EXTENT_PREALLOC) { |
| 7452 | /* not a regular extent, must cow */ |
| 7453 | goto out; |
| 7454 | } |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 7455 | |
| 7456 | if (!nocow && found_type == BTRFS_FILE_EXTENT_REG) |
| 7457 | goto out; |
| 7458 | |
Miao Xie | e77751a | 2013-12-27 21:11:50 +0800 | [diff] [blame] | 7459 | extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi); |
| 7460 | if (extent_end <= offset) |
| 7461 | goto out; |
| 7462 | |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7463 | disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 7464 | if (disk_bytenr == 0) |
| 7465 | goto out; |
| 7466 | |
| 7467 | if (btrfs_file_extent_compression(leaf, fi) || |
| 7468 | btrfs_file_extent_encryption(leaf, fi) || |
| 7469 | btrfs_file_extent_other_encoding(leaf, fi)) |
| 7470 | goto out; |
| 7471 | |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7472 | backref_offset = btrfs_file_extent_offset(leaf, fi); |
| 7473 | |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 7474 | if (orig_start) { |
| 7475 | *orig_start = key.offset - backref_offset; |
| 7476 | *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi); |
| 7477 | *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi); |
| 7478 | } |
Josef Bacik | eb384b5 | 2013-04-24 16:32:55 -0400 | [diff] [blame] | 7479 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 7480 | if (btrfs_extent_readonly(fs_info, disk_bytenr)) |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7481 | goto out; |
Miao Xie | 7b2b708 | 2014-02-27 13:58:05 +0800 | [diff] [blame] | 7482 | |
| 7483 | num_bytes = min(offset + *len, extent_end) - offset; |
| 7484 | if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) { |
| 7485 | u64 range_end; |
| 7486 | |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 7487 | range_end = round_up(offset + num_bytes, |
| 7488 | root->fs_info->sectorsize) - 1; |
Miao Xie | 7b2b708 | 2014-02-27 13:58:05 +0800 | [diff] [blame] | 7489 | ret = test_range_bit(io_tree, offset, range_end, |
| 7490 | EXTENT_DELALLOC, 0, NULL); |
| 7491 | if (ret) { |
| 7492 | ret = -EAGAIN; |
| 7493 | goto out; |
| 7494 | } |
| 7495 | } |
| 7496 | |
Josef Bacik | 1bda19e | 2013-10-18 12:10:36 -0400 | [diff] [blame] | 7497 | btrfs_release_path(path); |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7498 | |
| 7499 | /* |
| 7500 | * look for other files referencing this extent, if we |
| 7501 | * find any we must cow |
| 7502 | */ |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 7503 | |
Liu Bo | e4c3b2d | 2017-01-30 12:25:28 -0800 | [diff] [blame] | 7504 | ret = btrfs_cross_ref_exist(root, btrfs_ino(BTRFS_I(inode)), |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 7505 | key.offset - backref_offset, disk_bytenr); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 7506 | if (ret) { |
| 7507 | ret = 0; |
| 7508 | goto out; |
| 7509 | } |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7510 | |
| 7511 | /* |
| 7512 | * adjust disk_bytenr and num_bytes to cover just the bytes |
| 7513 | * in this extent we are about to write. If there |
| 7514 | * are any csums in that range we have to cow in order |
| 7515 | * to keep the csums correct |
| 7516 | */ |
| 7517 | disk_bytenr += backref_offset; |
| 7518 | disk_bytenr += offset - key.offset; |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 7519 | if (csum_exist_in_range(fs_info, disk_bytenr, num_bytes)) |
| 7520 | goto out; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7521 | /* |
| 7522 | * all of the above have passed, it is safe to overwrite this extent |
| 7523 | * without cow |
| 7524 | */ |
Josef Bacik | eb384b5 | 2013-04-24 16:32:55 -0400 | [diff] [blame] | 7525 | *len = num_bytes; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7526 | ret = 1; |
| 7527 | out: |
| 7528 | btrfs_free_path(path); |
| 7529 | return ret; |
| 7530 | } |
| 7531 | |
Alex Gartrell | fc4adbff | 2014-05-20 13:07:56 -0700 | [diff] [blame] | 7532 | bool btrfs_page_exists_in_range(struct inode *inode, loff_t start, loff_t end) |
| 7533 | { |
| 7534 | struct radix_tree_root *root = &inode->i_mapping->page_tree; |
David Sterba | e03733d | 2017-05-12 01:02:22 +0200 | [diff] [blame] | 7535 | bool found = false; |
Alex Gartrell | fc4adbff | 2014-05-20 13:07:56 -0700 | [diff] [blame] | 7536 | void **pagep = NULL; |
| 7537 | struct page *page = NULL; |
David Sterba | cc2b702 | 2017-05-12 01:03:52 +0200 | [diff] [blame] | 7538 | unsigned long start_idx; |
| 7539 | unsigned long end_idx; |
Alex Gartrell | fc4adbff | 2014-05-20 13:07:56 -0700 | [diff] [blame] | 7540 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 7541 | start_idx = start >> PAGE_SHIFT; |
Alex Gartrell | fc4adbff | 2014-05-20 13:07:56 -0700 | [diff] [blame] | 7542 | |
| 7543 | /* |
| 7544 | * end is the last byte in the last page. end == start is legal |
| 7545 | */ |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 7546 | end_idx = end >> PAGE_SHIFT; |
Alex Gartrell | fc4adbff | 2014-05-20 13:07:56 -0700 | [diff] [blame] | 7547 | |
| 7548 | rcu_read_lock(); |
| 7549 | |
| 7550 | /* Most of the code in this while loop is lifted from |
| 7551 | * find_get_page. It's been modified to begin searching from a |
| 7552 | * page and return just the first page found in that range. If the |
| 7553 | * found idx is less than or equal to the end idx then we know that |
| 7554 | * a page exists. If no pages are found or if those pages are |
| 7555 | * outside of the range then we're fine (yay!) */ |
| 7556 | while (page == NULL && |
| 7557 | radix_tree_gang_lookup_slot(root, &pagep, NULL, start_idx, 1)) { |
| 7558 | page = radix_tree_deref_slot(pagep); |
| 7559 | if (unlikely(!page)) |
| 7560 | break; |
| 7561 | |
| 7562 | if (radix_tree_exception(page)) { |
Filipe Manana | 809f901 | 2014-06-05 13:22:25 +0100 | [diff] [blame] | 7563 | if (radix_tree_deref_retry(page)) { |
| 7564 | page = NULL; |
Alex Gartrell | fc4adbff | 2014-05-20 13:07:56 -0700 | [diff] [blame] | 7565 | continue; |
Filipe Manana | 809f901 | 2014-06-05 13:22:25 +0100 | [diff] [blame] | 7566 | } |
Alex Gartrell | fc4adbff | 2014-05-20 13:07:56 -0700 | [diff] [blame] | 7567 | /* |
| 7568 | * Otherwise, shmem/tmpfs must be storing a swap entry |
| 7569 | * here as an exceptional entry: so return it without |
| 7570 | * attempting to raise page count. |
| 7571 | */ |
Filipe Manana | 6fdef6d | 2014-06-05 13:22:26 +0100 | [diff] [blame] | 7572 | page = NULL; |
Alex Gartrell | fc4adbff | 2014-05-20 13:07:56 -0700 | [diff] [blame] | 7573 | break; /* TODO: Is this relevant for this use case? */ |
| 7574 | } |
| 7575 | |
Filipe Manana | 9140515 | 2014-06-05 13:22:24 +0100 | [diff] [blame] | 7576 | if (!page_cache_get_speculative(page)) { |
| 7577 | page = NULL; |
Alex Gartrell | fc4adbff | 2014-05-20 13:07:56 -0700 | [diff] [blame] | 7578 | continue; |
Filipe Manana | 9140515 | 2014-06-05 13:22:24 +0100 | [diff] [blame] | 7579 | } |
Alex Gartrell | fc4adbff | 2014-05-20 13:07:56 -0700 | [diff] [blame] | 7580 | |
| 7581 | /* |
| 7582 | * Has the page moved? |
| 7583 | * This is part of the lockless pagecache protocol. See |
| 7584 | * include/linux/pagemap.h for details. |
| 7585 | */ |
| 7586 | if (unlikely(page != *pagep)) { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 7587 | put_page(page); |
Alex Gartrell | fc4adbff | 2014-05-20 13:07:56 -0700 | [diff] [blame] | 7588 | page = NULL; |
| 7589 | } |
| 7590 | } |
| 7591 | |
| 7592 | if (page) { |
| 7593 | if (page->index <= end_idx) |
| 7594 | found = true; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 7595 | put_page(page); |
Alex Gartrell | fc4adbff | 2014-05-20 13:07:56 -0700 | [diff] [blame] | 7596 | } |
| 7597 | |
| 7598 | rcu_read_unlock(); |
| 7599 | return found; |
| 7600 | } |
| 7601 | |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7602 | static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend, |
| 7603 | struct extent_state **cached_state, int writing) |
| 7604 | { |
| 7605 | struct btrfs_ordered_extent *ordered; |
| 7606 | int ret = 0; |
| 7607 | |
| 7608 | while (1) { |
| 7609 | lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend, |
David Sterba | ff13db4 | 2015-12-03 14:30:40 +0100 | [diff] [blame] | 7610 | cached_state); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7611 | /* |
| 7612 | * 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] | 7613 | * 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] | 7614 | * extents in this range. |
| 7615 | */ |
Nikolay Borisov | a776c6f | 2017-02-20 13:50:49 +0200 | [diff] [blame] | 7616 | ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), lockstart, |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7617 | lockend - lockstart + 1); |
| 7618 | |
| 7619 | /* |
| 7620 | * We need to make sure there are no buffered pages in this |
| 7621 | * range either, we could have raced between the invalidate in |
| 7622 | * generic_file_direct_write and locking the extent. The |
| 7623 | * invalidate needs to happen so that reads after a write do not |
| 7624 | * get stale data. |
| 7625 | */ |
Alex Gartrell | fc4adbff | 2014-05-20 13:07:56 -0700 | [diff] [blame] | 7626 | if (!ordered && |
| 7627 | (!writing || |
| 7628 | !btrfs_page_exists_in_range(inode, lockstart, lockend))) |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7629 | break; |
| 7630 | |
| 7631 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend, |
| 7632 | cached_state, GFP_NOFS); |
| 7633 | |
| 7634 | if (ordered) { |
Filipe Manana | ade7702 | 2016-02-18 14:28:55 +0000 | [diff] [blame] | 7635 | /* |
| 7636 | * If we are doing a DIO read and the ordered extent we |
| 7637 | * found is for a buffered write, we can not wait for it |
| 7638 | * to complete and retry, because if we do so we can |
| 7639 | * deadlock with concurrent buffered writes on page |
| 7640 | * locks. This happens only if our DIO read covers more |
| 7641 | * than one extent map, if at this point has already |
| 7642 | * created an ordered extent for a previous extent map |
| 7643 | * and locked its range in the inode's io tree, and a |
| 7644 | * concurrent write against that previous extent map's |
| 7645 | * range and this range started (we unlock the ranges |
| 7646 | * in the io tree only when the bios complete and |
| 7647 | * buffered writes always lock pages before attempting |
| 7648 | * to lock range in the io tree). |
| 7649 | */ |
| 7650 | if (writing || |
| 7651 | test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags)) |
| 7652 | btrfs_start_ordered_extent(inode, ordered, 1); |
| 7653 | else |
| 7654 | ret = -ENOTBLK; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7655 | btrfs_put_ordered_extent(ordered); |
| 7656 | } else { |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7657 | /* |
Filipe Manana | b850ae1 | 2015-12-08 16:23:16 +0000 | [diff] [blame] | 7658 | * We could trigger writeback for this range (and wait |
| 7659 | * for it to complete) and then invalidate the pages for |
| 7660 | * this range (through invalidate_inode_pages2_range()), |
| 7661 | * but that can lead us to a deadlock with a concurrent |
| 7662 | * call to readpages() (a buffered read or a defrag call |
| 7663 | * triggered a readahead) on a page lock due to an |
| 7664 | * ordered dio extent we created before but did not have |
| 7665 | * yet a corresponding bio submitted (whence it can not |
| 7666 | * complete), which makes readpages() wait for that |
| 7667 | * ordered extent to complete while holding a lock on |
| 7668 | * that page. |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7669 | */ |
Filipe Manana | b850ae1 | 2015-12-08 16:23:16 +0000 | [diff] [blame] | 7670 | ret = -ENOTBLK; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7671 | } |
| 7672 | |
Filipe Manana | ade7702 | 2016-02-18 14:28:55 +0000 | [diff] [blame] | 7673 | if (ret) |
| 7674 | break; |
| 7675 | |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7676 | cond_resched(); |
| 7677 | } |
| 7678 | |
| 7679 | return ret; |
| 7680 | } |
| 7681 | |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 7682 | /* The callers of this must take lock_extent() */ |
| 7683 | static struct extent_map *create_io_em(struct inode *inode, u64 start, u64 len, |
| 7684 | u64 orig_start, u64 block_start, |
| 7685 | u64 block_len, u64 orig_block_len, |
| 7686 | u64 ram_bytes, int compress_type, |
| 7687 | int type) |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7688 | { |
| 7689 | struct extent_map_tree *em_tree; |
| 7690 | struct extent_map *em; |
| 7691 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 7692 | int ret; |
| 7693 | |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 7694 | ASSERT(type == BTRFS_ORDERED_PREALLOC || |
| 7695 | type == BTRFS_ORDERED_COMPRESSED || |
| 7696 | type == BTRFS_ORDERED_NOCOW || |
Liu Bo | 1af4a0a | 2017-02-13 15:35:09 -0800 | [diff] [blame] | 7697 | type == BTRFS_ORDERED_REGULAR); |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 7698 | |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7699 | em_tree = &BTRFS_I(inode)->extent_tree; |
| 7700 | em = alloc_extent_map(); |
| 7701 | if (!em) |
| 7702 | return ERR_PTR(-ENOMEM); |
| 7703 | |
| 7704 | em->start = start; |
| 7705 | em->orig_start = orig_start; |
| 7706 | em->len = len; |
| 7707 | em->block_len = block_len; |
| 7708 | em->block_start = block_start; |
| 7709 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 7710 | em->orig_block_len = orig_block_len; |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 7711 | em->ram_bytes = ram_bytes; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 7712 | em->generation = -1; |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7713 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
Liu Bo | 1af4a0a | 2017-02-13 15:35:09 -0800 | [diff] [blame] | 7714 | if (type == BTRFS_ORDERED_PREALLOC) { |
Josef Bacik | b11e234 | 2012-12-03 10:58:15 -0500 | [diff] [blame] | 7715 | set_bit(EXTENT_FLAG_FILLING, &em->flags); |
Liu Bo | 1af4a0a | 2017-02-13 15:35:09 -0800 | [diff] [blame] | 7716 | } else if (type == BTRFS_ORDERED_COMPRESSED) { |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 7717 | set_bit(EXTENT_FLAG_COMPRESSED, &em->flags); |
| 7718 | em->compress_type = compress_type; |
| 7719 | } |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7720 | |
| 7721 | do { |
Nikolay Borisov | dcdbc05 | 2017-02-20 13:50:45 +0200 | [diff] [blame] | 7722 | btrfs_drop_extent_cache(BTRFS_I(inode), em->start, |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7723 | em->start + em->len - 1, 0); |
| 7724 | write_lock(&em_tree->lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 7725 | ret = add_extent_mapping(em_tree, em, 1); |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7726 | write_unlock(&em_tree->lock); |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 7727 | /* |
| 7728 | * The caller has taken lock_extent(), who could race with us |
| 7729 | * to add em? |
| 7730 | */ |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7731 | } while (ret == -EEXIST); |
| 7732 | |
| 7733 | if (ret) { |
| 7734 | free_extent_map(em); |
| 7735 | return ERR_PTR(ret); |
| 7736 | } |
| 7737 | |
Liu Bo | 6f9994d | 2017-01-31 07:50:22 -0800 | [diff] [blame] | 7738 | /* 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] | 7739 | return em; |
| 7740 | } |
| 7741 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7742 | static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock, |
| 7743 | struct buffer_head *bh_result, int create) |
| 7744 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7745 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7746 | struct extent_map *em; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7747 | struct extent_state *cached_state = NULL; |
chandan | 50745b0 | 2015-08-28 21:10:13 +0530 | [diff] [blame] | 7748 | struct btrfs_dio_data *dio_data = NULL; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7749 | u64 start = iblock << inode->i_blkbits; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7750 | u64 lockstart, lockend; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7751 | u64 len = bh_result->b_size; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7752 | int unlock_bits = EXTENT_LOCKED; |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 7753 | int ret = 0; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7754 | |
Miao Xie | 172a504 | 2013-02-21 02:48:22 -0700 | [diff] [blame] | 7755 | if (create) |
Josef Bacik | 3266789 | 2015-02-11 15:08:58 -0500 | [diff] [blame] | 7756 | unlock_bits |= EXTENT_DIRTY; |
Miao Xie | 172a504 | 2013-02-21 02:48:22 -0700 | [diff] [blame] | 7757 | else |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7758 | len = min_t(u64, len, fs_info->sectorsize); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7759 | |
Josef Bacik | c329861 | 2012-08-03 16:49:19 -0400 | [diff] [blame] | 7760 | lockstart = start; |
| 7761 | lockend = start + len - 1; |
| 7762 | |
Josef Bacik | e1cbbfa | 2015-03-17 10:52:28 -0400 | [diff] [blame] | 7763 | if (current->journal_info) { |
| 7764 | /* |
| 7765 | * 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] | 7766 | * 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] | 7767 | * confused. |
| 7768 | */ |
chandan | 50745b0 | 2015-08-28 21:10:13 +0530 | [diff] [blame] | 7769 | dio_data = current->journal_info; |
Josef Bacik | e1cbbfa | 2015-03-17 10:52:28 -0400 | [diff] [blame] | 7770 | current->journal_info = NULL; |
| 7771 | } |
| 7772 | |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7773 | /* |
| 7774 | * If this errors out it's because we couldn't invalidate pagecache for |
| 7775 | * this range and we need to fallback to buffered. |
| 7776 | */ |
Filipe Manana | 9c9464c | 2015-11-04 09:52:04 +0000 | [diff] [blame] | 7777 | if (lock_extent_direct(inode, lockstart, lockend, &cached_state, |
| 7778 | create)) { |
| 7779 | ret = -ENOTBLK; |
| 7780 | goto err; |
| 7781 | } |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7782 | |
Nikolay Borisov | fc4f21b1 | 2017-02-20 13:51:06 +0200 | [diff] [blame] | 7783 | em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len, 0); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7784 | if (IS_ERR(em)) { |
| 7785 | ret = PTR_ERR(em); |
| 7786 | goto unlock_err; |
| 7787 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7788 | |
| 7789 | /* |
| 7790 | * Ok for INLINE and COMPRESSED extents we need to fallback on buffered |
| 7791 | * io. INLINE is special, and we could probably kludge it in here, but |
| 7792 | * it's still buffered so for safety lets just fall back to the generic |
| 7793 | * buffered path. |
| 7794 | * |
| 7795 | * For COMPRESSED we _have_ to read the entire extent in so we can |
| 7796 | * decompress it, so there will be buffering required no matter what we |
| 7797 | * do, so go ahead and fallback to buffered. |
| 7798 | * |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 7799 | * 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] | 7800 | * to buffered IO. Don't blame me, this is the price we pay for using |
| 7801 | * the generic code. |
| 7802 | */ |
| 7803 | if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) || |
| 7804 | em->block_start == EXTENT_MAP_INLINE) { |
| 7805 | free_extent_map(em); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7806 | ret = -ENOTBLK; |
| 7807 | goto unlock_err; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7808 | } |
| 7809 | |
| 7810 | /* Just a good old fashioned hole, return */ |
| 7811 | if (!create && (em->block_start == EXTENT_MAP_HOLE || |
| 7812 | test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) { |
| 7813 | free_extent_map(em); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7814 | goto unlock_err; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7815 | } |
| 7816 | |
| 7817 | /* |
| 7818 | * We don't allocate a new extent in the following cases |
| 7819 | * |
| 7820 | * 1) The inode is marked as NODATACOW. In this case we'll just use the |
| 7821 | * existing extent. |
| 7822 | * 2) The extent is marked as PREALLOC. We're good to go here and can |
| 7823 | * just use the extent. |
| 7824 | * |
| 7825 | */ |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7826 | if (!create) { |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7827 | len = min(len, em->len - (start - em->start)); |
| 7828 | lockstart = start + len; |
| 7829 | goto unlock; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7830 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7831 | |
| 7832 | if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) || |
| 7833 | ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) && |
| 7834 | em->block_start != EXTENT_MAP_HOLE)) { |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7835 | int type; |
Josef Bacik | eb384b5 | 2013-04-24 16:32:55 -0400 | [diff] [blame] | 7836 | u64 block_start, orig_start, orig_block_len, ram_bytes; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7837 | |
| 7838 | if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) |
| 7839 | type = BTRFS_ORDERED_PREALLOC; |
| 7840 | else |
| 7841 | type = BTRFS_ORDERED_NOCOW; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7842 | len = min(len, em->len - (start - em->start)); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7843 | block_start = em->block_start + (start - em->start); |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7844 | |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 7845 | if (can_nocow_extent(inode, start, &len, &orig_start, |
Filipe Manana | f78c436 | 2016-05-09 13:15:41 +0100 | [diff] [blame] | 7846 | &orig_block_len, &ram_bytes) == 1 && |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7847 | btrfs_inc_nocow_writers(fs_info, block_start)) { |
Filipe Manana | 5f9a8a5 | 2016-05-12 13:53:36 +0100 | [diff] [blame] | 7848 | struct extent_map *em2; |
Filipe Manana | 0b90191 | 2016-05-09 13:15:27 +0100 | [diff] [blame] | 7849 | |
Filipe Manana | 5f9a8a5 | 2016-05-12 13:53:36 +0100 | [diff] [blame] | 7850 | em2 = btrfs_create_dio_extent(inode, start, len, |
| 7851 | orig_start, block_start, |
| 7852 | len, orig_block_len, |
| 7853 | ram_bytes, type); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7854 | btrfs_dec_nocow_writers(fs_info, block_start); |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7855 | if (type == BTRFS_ORDERED_PREALLOC) { |
| 7856 | free_extent_map(em); |
Filipe Manana | 5f9a8a5 | 2016-05-12 13:53:36 +0100 | [diff] [blame] | 7857 | em = em2; |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7858 | } |
Filipe Manana | 5f9a8a5 | 2016-05-12 13:53:36 +0100 | [diff] [blame] | 7859 | if (em2 && IS_ERR(em2)) { |
| 7860 | ret = PTR_ERR(em2); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7861 | goto unlock_err; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7862 | } |
Wang Xiaoguang | 1851309 | 2016-07-25 15:51:40 +0800 | [diff] [blame] | 7863 | /* |
| 7864 | * For inode marked NODATACOW or extent marked PREALLOC, |
| 7865 | * use the existing or preallocated extent, so does not |
| 7866 | * need to adjust btrfs_space_info's bytes_may_use. |
| 7867 | */ |
| 7868 | btrfs_free_reserved_data_space_noquota(inode, |
| 7869 | start, len); |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7870 | goto unlock; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7871 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7872 | } |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 7873 | |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7874 | /* |
| 7875 | * this will cow the extent, reset the len in case we changed |
| 7876 | * it above |
| 7877 | */ |
| 7878 | len = bh_result->b_size; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 7879 | free_extent_map(em); |
| 7880 | em = btrfs_new_extent_direct(inode, start, len); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7881 | if (IS_ERR(em)) { |
| 7882 | ret = PTR_ERR(em); |
| 7883 | goto unlock_err; |
| 7884 | } |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7885 | len = min(len, em->len - (start - em->start)); |
| 7886 | unlock: |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7887 | bh_result->b_blocknr = (em->block_start + (start - em->start)) >> |
| 7888 | inode->i_blkbits; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7889 | bh_result->b_size = len; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7890 | bh_result->b_bdev = em->bdev; |
| 7891 | set_buffer_mapped(bh_result); |
Josef Bacik | c3473e8 | 2012-06-19 10:59:00 -0400 | [diff] [blame] | 7892 | if (create) { |
| 7893 | if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) |
| 7894 | set_buffer_new(bh_result); |
| 7895 | |
| 7896 | /* |
| 7897 | * Need to update the i_size under the extent lock so buffered |
| 7898 | * readers will get the updated i_size when we unlock. |
| 7899 | */ |
Liu Bo | 4aaedfb | 2016-12-14 22:36:05 -0800 | [diff] [blame] | 7900 | if (!dio_data->overwrite && start + len > i_size_read(inode)) |
Josef Bacik | c3473e8 | 2012-06-19 10:59:00 -0400 | [diff] [blame] | 7901 | i_size_write(inode, start + len); |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 7902 | |
chandan | 50745b0 | 2015-08-28 21:10:13 +0530 | [diff] [blame] | 7903 | WARN_ON(dio_data->reserve < len); |
| 7904 | dio_data->reserve -= len; |
Filipe Manana | f28a492 | 2015-12-08 19:23:20 +0000 | [diff] [blame] | 7905 | dio_data->unsubmitted_oe_range_end = start + len; |
chandan | 50745b0 | 2015-08-28 21:10:13 +0530 | [diff] [blame] | 7906 | current->journal_info = dio_data; |
Josef Bacik | c3473e8 | 2012-06-19 10:59:00 -0400 | [diff] [blame] | 7907 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7908 | |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7909 | /* |
| 7910 | * In the case of write we need to clear and unlock the entire range, |
| 7911 | * in the case of read we need to unlock only the end area that we |
| 7912 | * aren't using if there is any left over space. |
| 7913 | */ |
Liu Bo | 24c03fa | 2012-08-22 20:10:38 -0600 | [diff] [blame] | 7914 | if (lockstart < lockend) { |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 7915 | clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, |
| 7916 | lockend, unlock_bits, 1, 0, |
David Sterba | ae0f162 | 2017-10-31 16:37:52 +0100 | [diff] [blame] | 7917 | &cached_state); |
Liu Bo | 24c03fa | 2012-08-22 20:10:38 -0600 | [diff] [blame] | 7918 | } else { |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7919 | free_extent_state(cached_state); |
Liu Bo | 24c03fa | 2012-08-22 20:10:38 -0600 | [diff] [blame] | 7920 | } |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7921 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7922 | free_extent_map(em); |
| 7923 | |
| 7924 | return 0; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7925 | |
| 7926 | unlock_err: |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7927 | clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend, |
David Sterba | ae0f162 | 2017-10-31 16:37:52 +0100 | [diff] [blame] | 7928 | unlock_bits, 1, 0, &cached_state); |
Filipe Manana | 9c9464c | 2015-11-04 09:52:04 +0000 | [diff] [blame] | 7929 | err: |
chandan | 50745b0 | 2015-08-28 21:10:13 +0530 | [diff] [blame] | 7930 | if (dio_data) |
| 7931 | current->journal_info = dio_data; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7932 | return ret; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7933 | } |
| 7934 | |
Omar Sandoval | 58efbc9 | 2017-08-22 23:45:59 -0700 | [diff] [blame] | 7935 | static inline blk_status_t submit_dio_repair_bio(struct inode *inode, |
| 7936 | struct bio *bio, |
| 7937 | int mirror_num) |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7938 | { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 7939 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Omar Sandoval | 58efbc9 | 2017-08-22 23:45:59 -0700 | [diff] [blame] | 7940 | blk_status_t ret; |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7941 | |
Mike Christie | 37226b2 | 2016-06-05 14:31:52 -0500 | [diff] [blame] | 7942 | BUG_ON(bio_op(bio) == REQ_OP_WRITE); |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7943 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 7944 | 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] | 7945 | if (ret) |
Nikolay Borisov | ea057f6 | 2017-12-13 10:25:38 +0200 | [diff] [blame^] | 7946 | return ret; |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7947 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 7948 | ret = btrfs_map_bio(fs_info, bio, mirror_num, 0); |
Nikolay Borisov | ea057f6 | 2017-12-13 10:25:38 +0200 | [diff] [blame^] | 7949 | |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7950 | return ret; |
| 7951 | } |
| 7952 | |
| 7953 | static int btrfs_check_dio_repairable(struct inode *inode, |
| 7954 | struct bio *failed_bio, |
| 7955 | struct io_failure_record *failrec, |
| 7956 | int failed_mirror) |
| 7957 | { |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 7958 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7959 | int num_copies; |
| 7960 | |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 7961 | num_copies = btrfs_num_copies(fs_info, failrec->logical, failrec->len); |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7962 | if (num_copies == 1) { |
| 7963 | /* |
| 7964 | * we only have a single copy of the data, so don't bother with |
| 7965 | * all the retry and error correction code that follows. no |
| 7966 | * matter what the error is, it is very likely to persist. |
| 7967 | */ |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 7968 | btrfs_debug(fs_info, |
| 7969 | "Check DIO Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d", |
| 7970 | num_copies, failrec->this_mirror, failed_mirror); |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7971 | return 0; |
| 7972 | } |
| 7973 | |
| 7974 | failrec->failed_mirror = failed_mirror; |
| 7975 | failrec->this_mirror++; |
| 7976 | if (failrec->this_mirror == failed_mirror) |
| 7977 | failrec->this_mirror++; |
| 7978 | |
| 7979 | if (failrec->this_mirror > num_copies) { |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 7980 | btrfs_debug(fs_info, |
| 7981 | "Check DIO Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d", |
| 7982 | num_copies, failrec->this_mirror, failed_mirror); |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7983 | return 0; |
| 7984 | } |
| 7985 | |
| 7986 | return 1; |
| 7987 | } |
| 7988 | |
Omar Sandoval | 58efbc9 | 2017-08-22 23:45:59 -0700 | [diff] [blame] | 7989 | static blk_status_t dio_read_error(struct inode *inode, struct bio *failed_bio, |
| 7990 | struct page *page, unsigned int pgoff, |
| 7991 | u64 start, u64 end, int failed_mirror, |
| 7992 | bio_end_io_t *repair_endio, void *repair_arg) |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7993 | { |
| 7994 | struct io_failure_record *failrec; |
Josef Bacik | 7870d08 | 2017-05-05 11:57:15 -0400 | [diff] [blame] | 7995 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
| 7996 | struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree; |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 7997 | struct bio *bio; |
| 7998 | int isector; |
David Sterba | f1c77c5 | 2017-06-06 19:03:49 +0200 | [diff] [blame] | 7999 | unsigned int read_mode = 0; |
Liu Bo | 17347ce | 2017-05-15 15:33:27 -0700 | [diff] [blame] | 8000 | int segs; |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8001 | int ret; |
Omar Sandoval | 58efbc9 | 2017-08-22 23:45:59 -0700 | [diff] [blame] | 8002 | blk_status_t status; |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8003 | |
Mike Christie | 37226b2 | 2016-06-05 14:31:52 -0500 | [diff] [blame] | 8004 | BUG_ON(bio_op(failed_bio) == REQ_OP_WRITE); |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8005 | |
| 8006 | ret = btrfs_get_io_failure_record(inode, start, end, &failrec); |
| 8007 | if (ret) |
Omar Sandoval | 58efbc9 | 2017-08-22 23:45:59 -0700 | [diff] [blame] | 8008 | return errno_to_blk_status(ret); |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8009 | |
| 8010 | ret = btrfs_check_dio_repairable(inode, failed_bio, failrec, |
| 8011 | failed_mirror); |
| 8012 | if (!ret) { |
Josef Bacik | 7870d08 | 2017-05-05 11:57:15 -0400 | [diff] [blame] | 8013 | free_io_failure(failure_tree, io_tree, failrec); |
Omar Sandoval | 58efbc9 | 2017-08-22 23:45:59 -0700 | [diff] [blame] | 8014 | return BLK_STS_IOERR; |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8015 | } |
| 8016 | |
Liu Bo | 17347ce | 2017-05-15 15:33:27 -0700 | [diff] [blame] | 8017 | segs = bio_segments(failed_bio); |
| 8018 | if (segs > 1 || |
| 8019 | (failed_bio->bi_io_vec->bv_len > btrfs_inode_sectorsize(inode))) |
Christoph Hellwig | 70fd761 | 2016-11-01 07:40:10 -0600 | [diff] [blame] | 8020 | read_mode |= REQ_FAILFAST_DEV; |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8021 | |
| 8022 | isector = start - btrfs_io_bio(failed_bio)->logical; |
| 8023 | isector >>= inode->i_sb->s_blocksize_bits; |
| 8024 | bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page, |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 8025 | pgoff, isector, repair_endio, repair_arg); |
Mike Christie | 37226b2 | 2016-06-05 14:31:52 -0500 | [diff] [blame] | 8026 | bio_set_op_attrs(bio, REQ_OP_READ, read_mode); |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8027 | |
| 8028 | btrfs_debug(BTRFS_I(inode)->root->fs_info, |
David Sterba | 913e153 | 2017-07-13 15:32:18 +0200 | [diff] [blame] | 8029 | "repair DIO read error: submitting new dio read[%#x] to this_mirror=%d, in_validation=%d", |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8030 | read_mode, failrec->this_mirror, failrec->in_validation); |
| 8031 | |
Omar Sandoval | 58efbc9 | 2017-08-22 23:45:59 -0700 | [diff] [blame] | 8032 | status = submit_dio_repair_bio(inode, bio, failrec->this_mirror); |
| 8033 | if (status) { |
Josef Bacik | 7870d08 | 2017-05-05 11:57:15 -0400 | [diff] [blame] | 8034 | free_io_failure(failure_tree, io_tree, failrec); |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8035 | bio_put(bio); |
| 8036 | } |
| 8037 | |
Omar Sandoval | 58efbc9 | 2017-08-22 23:45:59 -0700 | [diff] [blame] | 8038 | return status; |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8039 | } |
| 8040 | |
| 8041 | struct btrfs_retry_complete { |
| 8042 | struct completion done; |
| 8043 | struct inode *inode; |
| 8044 | u64 start; |
| 8045 | int uptodate; |
| 8046 | }; |
| 8047 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 8048 | static void btrfs_retry_endio_nocsum(struct bio *bio) |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8049 | { |
| 8050 | struct btrfs_retry_complete *done = bio->bi_private; |
Josef Bacik | 7870d08 | 2017-05-05 11:57:15 -0400 | [diff] [blame] | 8051 | struct inode *inode = done->inode; |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8052 | struct bio_vec *bvec; |
Josef Bacik | 7870d08 | 2017-05-05 11:57:15 -0400 | [diff] [blame] | 8053 | struct extent_io_tree *io_tree, *failure_tree; |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8054 | int i; |
| 8055 | |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 8056 | if (bio->bi_status) |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8057 | goto end; |
| 8058 | |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 8059 | ASSERT(bio->bi_vcnt == 1); |
Josef Bacik | 7870d08 | 2017-05-05 11:57:15 -0400 | [diff] [blame] | 8060 | io_tree = &BTRFS_I(inode)->io_tree; |
| 8061 | failure_tree = &BTRFS_I(inode)->io_failure_tree; |
| 8062 | ASSERT(bio->bi_io_vec->bv_len == btrfs_inode_sectorsize(inode)); |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 8063 | |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8064 | done->uptodate = 1; |
David Sterba | c09abff | 2017-07-13 18:10:07 +0200 | [diff] [blame] | 8065 | ASSERT(!bio_flagged(bio, BIO_CLONED)); |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8066 | bio_for_each_segment_all(bvec, bio, i) |
Josef Bacik | 7870d08 | 2017-05-05 11:57:15 -0400 | [diff] [blame] | 8067 | clean_io_failure(BTRFS_I(inode)->root->fs_info, failure_tree, |
| 8068 | io_tree, done->start, bvec->bv_page, |
| 8069 | btrfs_ino(BTRFS_I(inode)), 0); |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8070 | end: |
| 8071 | complete(&done->done); |
| 8072 | bio_put(bio); |
| 8073 | } |
| 8074 | |
Omar Sandoval | 58efbc9 | 2017-08-22 23:45:59 -0700 | [diff] [blame] | 8075 | static blk_status_t __btrfs_correct_data_nocsum(struct inode *inode, |
| 8076 | struct btrfs_io_bio *io_bio) |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8077 | { |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 8078 | struct btrfs_fs_info *fs_info; |
Liu Bo | 17347ce | 2017-05-15 15:33:27 -0700 | [diff] [blame] | 8079 | struct bio_vec bvec; |
| 8080 | struct bvec_iter iter; |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8081 | struct btrfs_retry_complete done; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8082 | u64 start; |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 8083 | unsigned int pgoff; |
| 8084 | u32 sectorsize; |
| 8085 | int nr_sectors; |
Omar Sandoval | 58efbc9 | 2017-08-22 23:45:59 -0700 | [diff] [blame] | 8086 | blk_status_t ret; |
| 8087 | blk_status_t err = BLK_STS_OK; |
Miao Xie | dc380ae | 2014-09-12 18:43:55 +0800 | [diff] [blame] | 8088 | |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 8089 | fs_info = BTRFS_I(inode)->root->fs_info; |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 8090 | sectorsize = fs_info->sectorsize; |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 8091 | |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 8092 | start = io_bio->logical; |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8093 | done.inode = inode; |
Liu Bo | 17347ce | 2017-05-15 15:33:27 -0700 | [diff] [blame] | 8094 | io_bio->bio.bi_iter = io_bio->iter; |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8095 | |
Liu Bo | 17347ce | 2017-05-15 15:33:27 -0700 | [diff] [blame] | 8096 | bio_for_each_segment(bvec, &io_bio->bio, iter) { |
| 8097 | nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec.bv_len); |
| 8098 | pgoff = bvec.bv_offset; |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 8099 | |
| 8100 | next_block_or_try_again: |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8101 | done.uptodate = 0; |
| 8102 | done.start = start; |
| 8103 | init_completion(&done.done); |
| 8104 | |
Liu Bo | 17347ce | 2017-05-15 15:33:27 -0700 | [diff] [blame] | 8105 | ret = dio_read_error(inode, &io_bio->bio, bvec.bv_page, |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 8106 | pgoff, start, start + sectorsize - 1, |
| 8107 | io_bio->mirror_num, |
| 8108 | btrfs_retry_endio_nocsum, &done); |
Liu Bo | 629ebf4 | 2017-05-15 17:20:07 -0700 | [diff] [blame] | 8109 | if (ret) { |
| 8110 | err = ret; |
| 8111 | goto next; |
| 8112 | } |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8113 | |
David Sterba | 9c17f6c | 2017-07-19 19:26:45 +0200 | [diff] [blame] | 8114 | wait_for_completion_io(&done.done); |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8115 | |
| 8116 | if (!done.uptodate) { |
| 8117 | /* We might have another mirror, so try again */ |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 8118 | goto next_block_or_try_again; |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8119 | } |
| 8120 | |
Liu Bo | 629ebf4 | 2017-05-15 17:20:07 -0700 | [diff] [blame] | 8121 | next: |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 8122 | start += sectorsize; |
| 8123 | |
Liu Bo | 97bf5a5 | 2017-04-07 13:11:10 -0700 | [diff] [blame] | 8124 | nr_sectors--; |
| 8125 | if (nr_sectors) { |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 8126 | pgoff += sectorsize; |
Liu Bo | 97bf5a5 | 2017-04-07 13:11:10 -0700 | [diff] [blame] | 8127 | ASSERT(pgoff < PAGE_SIZE); |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 8128 | goto next_block_or_try_again; |
| 8129 | } |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8130 | } |
| 8131 | |
Liu Bo | 629ebf4 | 2017-05-15 17:20:07 -0700 | [diff] [blame] | 8132 | return err; |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8133 | } |
| 8134 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 8135 | static void btrfs_retry_endio(struct bio *bio) |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8136 | { |
| 8137 | struct btrfs_retry_complete *done = bio->bi_private; |
| 8138 | struct btrfs_io_bio *io_bio = btrfs_io_bio(bio); |
Josef Bacik | 7870d08 | 2017-05-05 11:57:15 -0400 | [diff] [blame] | 8139 | struct extent_io_tree *io_tree, *failure_tree; |
| 8140 | struct inode *inode = done->inode; |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8141 | struct bio_vec *bvec; |
| 8142 | int uptodate; |
| 8143 | int ret; |
| 8144 | int i; |
| 8145 | |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 8146 | if (bio->bi_status) |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8147 | goto end; |
| 8148 | |
| 8149 | uptodate = 1; |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 8150 | |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 8151 | ASSERT(bio->bi_vcnt == 1); |
Liu Bo | 2e949b0 | 2017-04-05 14:04:19 -0700 | [diff] [blame] | 8152 | ASSERT(bio->bi_io_vec->bv_len == btrfs_inode_sectorsize(done->inode)); |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 8153 | |
Josef Bacik | 7870d08 | 2017-05-05 11:57:15 -0400 | [diff] [blame] | 8154 | io_tree = &BTRFS_I(inode)->io_tree; |
| 8155 | failure_tree = &BTRFS_I(inode)->io_failure_tree; |
| 8156 | |
David Sterba | c09abff | 2017-07-13 18:10:07 +0200 | [diff] [blame] | 8157 | ASSERT(!bio_flagged(bio, BIO_CLONED)); |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8158 | bio_for_each_segment_all(bvec, bio, i) { |
Josef Bacik | 7870d08 | 2017-05-05 11:57:15 -0400 | [diff] [blame] | 8159 | ret = __readpage_endio_check(inode, io_bio, i, bvec->bv_page, |
| 8160 | bvec->bv_offset, done->start, |
| 8161 | bvec->bv_len); |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8162 | if (!ret) |
Josef Bacik | 7870d08 | 2017-05-05 11:57:15 -0400 | [diff] [blame] | 8163 | clean_io_failure(BTRFS_I(inode)->root->fs_info, |
| 8164 | failure_tree, io_tree, done->start, |
| 8165 | bvec->bv_page, |
| 8166 | btrfs_ino(BTRFS_I(inode)), |
| 8167 | bvec->bv_offset); |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8168 | else |
| 8169 | uptodate = 0; |
| 8170 | } |
| 8171 | |
| 8172 | done->uptodate = uptodate; |
| 8173 | end: |
| 8174 | complete(&done->done); |
| 8175 | bio_put(bio); |
| 8176 | } |
| 8177 | |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 8178 | static blk_status_t __btrfs_subio_endio_read(struct inode *inode, |
| 8179 | struct btrfs_io_bio *io_bio, blk_status_t err) |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8180 | { |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 8181 | struct btrfs_fs_info *fs_info; |
Liu Bo | 17347ce | 2017-05-15 15:33:27 -0700 | [diff] [blame] | 8182 | struct bio_vec bvec; |
| 8183 | struct bvec_iter iter; |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8184 | struct btrfs_retry_complete done; |
| 8185 | u64 start; |
| 8186 | u64 offset = 0; |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 8187 | u32 sectorsize; |
| 8188 | int nr_sectors; |
| 8189 | unsigned int pgoff; |
| 8190 | int csum_pos; |
Liu Bo | ef7cdac1 | 2017-04-13 18:11:48 -0700 | [diff] [blame] | 8191 | bool uptodate = (err == 0); |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8192 | int ret; |
Omar Sandoval | 58efbc9 | 2017-08-22 23:45:59 -0700 | [diff] [blame] | 8193 | blk_status_t status; |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8194 | |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 8195 | fs_info = BTRFS_I(inode)->root->fs_info; |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 8196 | sectorsize = fs_info->sectorsize; |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 8197 | |
Omar Sandoval | 58efbc9 | 2017-08-22 23:45:59 -0700 | [diff] [blame] | 8198 | err = BLK_STS_OK; |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8199 | start = io_bio->logical; |
| 8200 | done.inode = inode; |
Liu Bo | 17347ce | 2017-05-15 15:33:27 -0700 | [diff] [blame] | 8201 | io_bio->bio.bi_iter = io_bio->iter; |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8202 | |
Liu Bo | 17347ce | 2017-05-15 15:33:27 -0700 | [diff] [blame] | 8203 | bio_for_each_segment(bvec, &io_bio->bio, iter) { |
| 8204 | nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec.bv_len); |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 8205 | |
Liu Bo | 17347ce | 2017-05-15 15:33:27 -0700 | [diff] [blame] | 8206 | pgoff = bvec.bv_offset; |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 8207 | next_block: |
Liu Bo | ef7cdac1 | 2017-04-13 18:11:48 -0700 | [diff] [blame] | 8208 | if (uptodate) { |
| 8209 | csum_pos = BTRFS_BYTES_TO_BLKS(fs_info, offset); |
| 8210 | ret = __readpage_endio_check(inode, io_bio, csum_pos, |
| 8211 | bvec.bv_page, pgoff, start, sectorsize); |
| 8212 | if (likely(!ret)) |
| 8213 | goto next; |
| 8214 | } |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8215 | try_again: |
| 8216 | done.uptodate = 0; |
| 8217 | done.start = start; |
| 8218 | init_completion(&done.done); |
| 8219 | |
Omar Sandoval | 58efbc9 | 2017-08-22 23:45:59 -0700 | [diff] [blame] | 8220 | status = dio_read_error(inode, &io_bio->bio, bvec.bv_page, |
| 8221 | pgoff, start, start + sectorsize - 1, |
| 8222 | io_bio->mirror_num, btrfs_retry_endio, |
| 8223 | &done); |
| 8224 | if (status) { |
| 8225 | err = status; |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8226 | goto next; |
| 8227 | } |
| 8228 | |
David Sterba | 9c17f6c | 2017-07-19 19:26:45 +0200 | [diff] [blame] | 8229 | wait_for_completion_io(&done.done); |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8230 | |
| 8231 | if (!done.uptodate) { |
| 8232 | /* We might have another mirror, so try again */ |
| 8233 | goto try_again; |
| 8234 | } |
| 8235 | next: |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 8236 | offset += sectorsize; |
| 8237 | start += sectorsize; |
| 8238 | |
| 8239 | ASSERT(nr_sectors); |
| 8240 | |
Liu Bo | 97bf5a5 | 2017-04-07 13:11:10 -0700 | [diff] [blame] | 8241 | nr_sectors--; |
| 8242 | if (nr_sectors) { |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 8243 | pgoff += sectorsize; |
Liu Bo | 97bf5a5 | 2017-04-07 13:11:10 -0700 | [diff] [blame] | 8244 | ASSERT(pgoff < PAGE_SIZE); |
Chandan Rajendra | 2dabb32 | 2016-01-21 15:55:55 +0530 | [diff] [blame] | 8245 | goto next_block; |
| 8246 | } |
Kent Overstreet | 2c30c71 | 2013-11-07 12:20:26 -0800 | [diff] [blame] | 8247 | } |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 8248 | |
| 8249 | return err; |
| 8250 | } |
| 8251 | |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 8252 | static blk_status_t btrfs_subio_endio_read(struct inode *inode, |
| 8253 | struct btrfs_io_bio *io_bio, blk_status_t err) |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8254 | { |
| 8255 | bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM; |
| 8256 | |
| 8257 | if (skip_csum) { |
| 8258 | if (unlikely(err)) |
| 8259 | return __btrfs_correct_data_nocsum(inode, io_bio); |
| 8260 | else |
Omar Sandoval | 58efbc9 | 2017-08-22 23:45:59 -0700 | [diff] [blame] | 8261 | return BLK_STS_OK; |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8262 | } else { |
| 8263 | return __btrfs_subio_endio_read(inode, io_bio, err); |
| 8264 | } |
| 8265 | } |
| 8266 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 8267 | static void btrfs_endio_direct_read(struct bio *bio) |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 8268 | { |
| 8269 | struct btrfs_dio_private *dip = bio->bi_private; |
| 8270 | struct inode *inode = dip->inode; |
| 8271 | struct bio *dio_bio; |
| 8272 | struct btrfs_io_bio *io_bio = btrfs_io_bio(bio); |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 8273 | blk_status_t err = bio->bi_status; |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 8274 | |
Liu Bo | 99c4e3b9 | 2017-09-15 15:06:51 -0600 | [diff] [blame] | 8275 | if (dip->flags & BTRFS_DIO_ORIG_BIO_SUBMITTED) |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8276 | err = btrfs_subio_endio_read(inode, io_bio, err); |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 8277 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8278 | unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset, |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 8279 | dip->logical_offset + dip->bytes - 1); |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 8280 | dio_bio = dip->dio_bio; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8281 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8282 | kfree(dip); |
Josef Bacik | c0da7aa | 2011-03-22 11:05:07 -0400 | [diff] [blame] | 8283 | |
Liu Bo | 99c4e3b9 | 2017-09-15 15:06:51 -0600 | [diff] [blame] | 8284 | dio_bio->bi_status = err; |
Christoph Hellwig | 4055351 | 2017-06-03 09:37:58 +0200 | [diff] [blame] | 8285 | dio_end_io(dio_bio); |
Miao Xie | 23ea8e5 | 2014-09-12 18:43:54 +0800 | [diff] [blame] | 8286 | |
| 8287 | if (io_bio->end_io) |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 8288 | io_bio->end_io(io_bio, blk_status_to_errno(err)); |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 8289 | bio_put(bio); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8290 | } |
| 8291 | |
Qu Wenruo | 52427260 | 2017-03-08 10:25:52 +0800 | [diff] [blame] | 8292 | static void __endio_write_update_ordered(struct inode *inode, |
| 8293 | const u64 offset, const u64 bytes, |
| 8294 | const bool uptodate) |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8295 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8296 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8297 | struct btrfs_ordered_extent *ordered = NULL; |
Qu Wenruo | 52427260 | 2017-03-08 10:25:52 +0800 | [diff] [blame] | 8298 | struct btrfs_workqueue *wq; |
| 8299 | btrfs_work_func_t func; |
Filipe Manana | 1454377 | 2015-11-24 16:23:54 +0000 | [diff] [blame] | 8300 | u64 ordered_offset = offset; |
| 8301 | u64 ordered_bytes = bytes; |
Naohiro Aota | 67c003f | 2017-09-01 17:59:07 +0900 | [diff] [blame] | 8302 | u64 last_offset; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8303 | int ret; |
| 8304 | |
Qu Wenruo | 52427260 | 2017-03-08 10:25:52 +0800 | [diff] [blame] | 8305 | if (btrfs_is_free_space_inode(BTRFS_I(inode))) { |
| 8306 | wq = fs_info->endio_freespace_worker; |
| 8307 | func = btrfs_freespace_write_helper; |
| 8308 | } else { |
| 8309 | wq = fs_info->endio_write_workers; |
| 8310 | func = btrfs_endio_write_helper; |
| 8311 | } |
| 8312 | |
Chris Mason | 163cf09 | 2010-11-28 19:56:33 -0500 | [diff] [blame] | 8313 | again: |
Naohiro Aota | 67c003f | 2017-09-01 17:59:07 +0900 | [diff] [blame] | 8314 | last_offset = ordered_offset; |
Chris Mason | 163cf09 | 2010-11-28 19:56:33 -0500 | [diff] [blame] | 8315 | ret = btrfs_dec_test_first_ordered_pending(inode, &ordered, |
| 8316 | &ordered_offset, |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 8317 | ordered_bytes, |
Filipe Manana | 1454377 | 2015-11-24 16:23:54 +0000 | [diff] [blame] | 8318 | uptodate); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8319 | if (!ret) |
Chris Mason | 163cf09 | 2010-11-28 19:56:33 -0500 | [diff] [blame] | 8320 | goto out_test; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8321 | |
Qu Wenruo | 52427260 | 2017-03-08 10:25:52 +0800 | [diff] [blame] | 8322 | btrfs_init_work(&ordered->work, func, finish_ordered_fn, NULL, NULL); |
| 8323 | btrfs_queue_work(wq, &ordered->work); |
Chris Mason | 163cf09 | 2010-11-28 19:56:33 -0500 | [diff] [blame] | 8324 | out_test: |
| 8325 | /* |
Naohiro Aota | 67c003f | 2017-09-01 17:59:07 +0900 | [diff] [blame] | 8326 | * If btrfs_dec_test_ordered_pending does not find any ordered extent |
| 8327 | * in the range, we can exit. |
| 8328 | */ |
| 8329 | if (ordered_offset == last_offset) |
| 8330 | return; |
| 8331 | /* |
Chris Mason | 163cf09 | 2010-11-28 19:56:33 -0500 | [diff] [blame] | 8332 | * our bio might span multiple ordered extents. If we haven't |
| 8333 | * completed the accounting for the whole dio, go back and try again |
| 8334 | */ |
Filipe Manana | 1454377 | 2015-11-24 16:23:54 +0000 | [diff] [blame] | 8335 | if (ordered_offset < offset + bytes) { |
| 8336 | ordered_bytes = offset + bytes - ordered_offset; |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 8337 | ordered = NULL; |
Chris Mason | 163cf09 | 2010-11-28 19:56:33 -0500 | [diff] [blame] | 8338 | goto again; |
| 8339 | } |
Filipe Manana | 1454377 | 2015-11-24 16:23:54 +0000 | [diff] [blame] | 8340 | } |
| 8341 | |
| 8342 | static void btrfs_endio_direct_write(struct bio *bio) |
| 8343 | { |
| 8344 | struct btrfs_dio_private *dip = bio->bi_private; |
| 8345 | struct bio *dio_bio = dip->dio_bio; |
| 8346 | |
Qu Wenruo | 52427260 | 2017-03-08 10:25:52 +0800 | [diff] [blame] | 8347 | __endio_write_update_ordered(dip->inode, dip->logical_offset, |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 8348 | dip->bytes, !bio->bi_status); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8349 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8350 | kfree(dip); |
Josef Bacik | c0da7aa | 2011-03-22 11:05:07 -0400 | [diff] [blame] | 8351 | |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 8352 | dio_bio->bi_status = bio->bi_status; |
Christoph Hellwig | 4055351 | 2017-06-03 09:37:58 +0200 | [diff] [blame] | 8353 | dio_end_io(dio_bio); |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 8354 | bio_put(bio); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8355 | } |
| 8356 | |
Linus Torvalds | 8c27cb3 | 2017-07-05 16:41:23 -0700 | [diff] [blame] | 8357 | static blk_status_t __btrfs_submit_bio_start_direct_io(void *private_data, |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 8358 | struct bio *bio, int mirror_num, |
| 8359 | unsigned long bio_flags, u64 offset) |
| 8360 | { |
Josef Bacik | c6100a4 | 2017-05-05 11:57:13 -0400 | [diff] [blame] | 8361 | struct inode *inode = private_data; |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 8362 | blk_status_t ret; |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 8363 | ret = btrfs_csum_one_bio(inode, bio, offset, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 8364 | BUG_ON(ret); /* -ENOMEM */ |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 8365 | return 0; |
| 8366 | } |
| 8367 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 8368 | static void btrfs_end_dio_bio(struct bio *bio) |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8369 | { |
| 8370 | struct btrfs_dio_private *dip = bio->bi_private; |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 8371 | blk_status_t err = bio->bi_status; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8372 | |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8373 | if (err) |
| 8374 | btrfs_warn(BTRFS_I(dip->inode)->root->fs_info, |
Mike Christie | 6296b96 | 2016-06-05 14:32:21 -0500 | [diff] [blame] | 8375 | "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] | 8376 | btrfs_ino(BTRFS_I(dip->inode)), bio_op(bio), |
| 8377 | bio->bi_opf, |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 8378 | (unsigned long long)bio->bi_iter.bi_sector, |
| 8379 | bio->bi_iter.bi_size, err); |
| 8380 | |
| 8381 | if (dip->subio_endio) |
| 8382 | err = dip->subio_endio(dip->inode, btrfs_io_bio(bio), err); |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 8383 | |
| 8384 | if (err) { |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8385 | dip->errors = 1; |
| 8386 | |
| 8387 | /* |
| 8388 | * before atomic variable goto zero, we must make sure |
| 8389 | * dip->errors is perceived to be set. |
| 8390 | */ |
Peter Zijlstra | 4e857c5 | 2014-03-17 18:06:10 +0100 | [diff] [blame] | 8391 | smp_mb__before_atomic(); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8392 | } |
| 8393 | |
| 8394 | /* if there are more bios still pending for this dio, just exit */ |
| 8395 | if (!atomic_dec_and_test(&dip->pending_bios)) |
| 8396 | goto out; |
| 8397 | |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 8398 | if (dip->errors) { |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8399 | bio_io_error(dip->orig_bio); |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 8400 | } else { |
Anand Jain | 2dbe0c7 | 2017-10-14 08:35:56 +0800 | [diff] [blame] | 8401 | dip->dio_bio->bi_status = BLK_STS_OK; |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 8402 | bio_endio(dip->orig_bio); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8403 | } |
| 8404 | out: |
| 8405 | bio_put(bio); |
| 8406 | } |
| 8407 | |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 8408 | static inline blk_status_t btrfs_lookup_and_bind_dio_csum(struct inode *inode, |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 8409 | struct btrfs_dio_private *dip, |
| 8410 | struct bio *bio, |
| 8411 | u64 file_offset) |
| 8412 | { |
| 8413 | struct btrfs_io_bio *io_bio = btrfs_io_bio(bio); |
| 8414 | struct btrfs_io_bio *orig_io_bio = btrfs_io_bio(dip->orig_bio); |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 8415 | blk_status_t ret; |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 8416 | |
| 8417 | /* |
| 8418 | * We load all the csum data we need when we submit |
| 8419 | * the first bio to reduce the csum tree search and |
| 8420 | * contention. |
| 8421 | */ |
| 8422 | if (dip->logical_offset == file_offset) { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 8423 | ret = btrfs_lookup_bio_sums_dio(inode, dip->orig_bio, |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 8424 | file_offset); |
| 8425 | if (ret) |
| 8426 | return ret; |
| 8427 | } |
| 8428 | |
| 8429 | if (bio == dip->orig_bio) |
| 8430 | return 0; |
| 8431 | |
| 8432 | file_offset -= dip->logical_offset; |
| 8433 | file_offset >>= inode->i_sb->s_blocksize_bits; |
| 8434 | io_bio->csum = (u8 *)(((u32 *)orig_io_bio->csum) + file_offset); |
| 8435 | |
| 8436 | return 0; |
| 8437 | } |
| 8438 | |
Omar Sandoval | 58efbc9 | 2017-08-22 23:45:59 -0700 | [diff] [blame] | 8439 | static inline blk_status_t |
| 8440 | __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode, u64 file_offset, |
Linus Torvalds | 66ba772 | 2017-09-09 13:27:51 -0700 | [diff] [blame] | 8441 | int async_submit) |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8442 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8443 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 8444 | struct btrfs_dio_private *dip = bio->bi_private; |
Mike Christie | 37226b2 | 2016-06-05 14:31:52 -0500 | [diff] [blame] | 8445 | bool write = bio_op(bio) == REQ_OP_WRITE; |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 8446 | blk_status_t ret; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8447 | |
Liu Bo | 4c274bc | 2017-11-01 17:19:27 -0600 | [diff] [blame] | 8448 | /* Check btrfs_submit_bio_hook() for rules about async submit. */ |
Josef Bacik | b812ce2 | 2012-11-16 13:56:32 -0500 | [diff] [blame] | 8449 | if (async_submit) |
| 8450 | async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers); |
| 8451 | |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8452 | bio_get(bio); |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 8453 | |
| 8454 | if (!write) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8455 | 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] | 8456 | if (ret) |
| 8457 | goto err; |
| 8458 | } |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8459 | |
Nikolay Borisov | e6961ca | 2017-08-03 15:44:58 +0300 | [diff] [blame] | 8460 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM) |
Josef Bacik | 1ae3993 | 2011-04-06 14:41:34 -0400 | [diff] [blame] | 8461 | goto map; |
| 8462 | |
| 8463 | if (write && async_submit) { |
Josef Bacik | c6100a4 | 2017-05-05 11:57:13 -0400 | [diff] [blame] | 8464 | ret = btrfs_wq_submit_bio(fs_info, bio, 0, 0, |
| 8465 | file_offset, inode, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8466 | __btrfs_submit_bio_start_direct_io, |
| 8467 | __btrfs_submit_bio_done); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8468 | goto err; |
Josef Bacik | 1ae3993 | 2011-04-06 14:41:34 -0400 | [diff] [blame] | 8469 | } else if (write) { |
| 8470 | /* |
| 8471 | * If we aren't doing async submit, calculate the csum of the |
| 8472 | * bio now. |
| 8473 | */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 8474 | ret = btrfs_csum_one_bio(inode, bio, file_offset, 1); |
Josef Bacik | 1ae3993 | 2011-04-06 14:41:34 -0400 | [diff] [blame] | 8475 | if (ret) |
| 8476 | goto err; |
Miao Xie | 23ea8e5 | 2014-09-12 18:43:54 +0800 | [diff] [blame] | 8477 | } else { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 8478 | ret = btrfs_lookup_and_bind_dio_csum(inode, dip, bio, |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 8479 | file_offset); |
Tsutomu Itoh | c2db107 | 2011-03-01 06:48:31 +0000 | [diff] [blame] | 8480 | if (ret) |
| 8481 | goto err; |
| 8482 | } |
Josef Bacik | 1ae3993 | 2011-04-06 14:41:34 -0400 | [diff] [blame] | 8483 | map: |
Liu Bo | 9b4a9b2 | 2017-08-18 11:54:02 -0600 | [diff] [blame] | 8484 | ret = btrfs_map_bio(fs_info, bio, 0, 0); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8485 | err: |
| 8486 | bio_put(bio); |
| 8487 | return ret; |
| 8488 | } |
| 8489 | |
Nikolay Borisov | e6961ca | 2017-08-03 15:44:58 +0300 | [diff] [blame] | 8490 | static int btrfs_submit_direct_hook(struct btrfs_dio_private *dip) |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8491 | { |
| 8492 | struct inode *inode = dip->inode; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8493 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8494 | struct bio *bio; |
| 8495 | struct bio *orig_bio = dip->orig_bio; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 8496 | u64 start_sector = orig_bio->bi_iter.bi_sector; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8497 | u64 file_offset = dip->logical_offset; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8498 | u64 map_length; |
Josef Bacik | 1ae3993 | 2011-04-06 14:41:34 -0400 | [diff] [blame] | 8499 | int async_submit = 0; |
Liu Bo | 725130b | 2017-05-16 09:51:39 -0700 | [diff] [blame] | 8500 | u64 submit_len; |
| 8501 | int clone_offset = 0; |
| 8502 | int clone_len; |
Chandan Rajendra | 5f4dc8f | 2016-01-21 15:56:00 +0530 | [diff] [blame] | 8503 | int ret; |
Omar Sandoval | 58efbc9 | 2017-08-22 23:45:59 -0700 | [diff] [blame] | 8504 | blk_status_t status; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8505 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 8506 | map_length = orig_bio->bi_iter.bi_size; |
Liu Bo | 725130b | 2017-05-16 09:51:39 -0700 | [diff] [blame] | 8507 | submit_len = map_length; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8508 | ret = btrfs_map_block(fs_info, btrfs_op(orig_bio), start_sector << 9, |
| 8509 | &map_length, NULL, 0); |
Miao Xie | 7a5c3c9 | 2014-06-17 18:58:59 +0800 | [diff] [blame] | 8510 | if (ret) |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8511 | return -EIO; |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 8512 | |
Liu Bo | 725130b | 2017-05-16 09:51:39 -0700 | [diff] [blame] | 8513 | if (map_length >= submit_len) { |
Josef Bacik | 02f57c7 | 2011-04-06 14:25:44 -0400 | [diff] [blame] | 8514 | bio = orig_bio; |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 8515 | dip->flags |= BTRFS_DIO_ORIG_BIO_SUBMITTED; |
Josef Bacik | 02f57c7 | 2011-04-06 14:25:44 -0400 | [diff] [blame] | 8516 | goto submit; |
| 8517 | } |
| 8518 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 8519 | /* async crcs make it difficult to collect full stripe writes. */ |
Jeff Mahoney | 1b86826 | 2017-05-17 11:38:35 -0400 | [diff] [blame] | 8520 | if (btrfs_data_alloc_profile(fs_info) & BTRFS_BLOCK_GROUP_RAID56_MASK) |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 8521 | async_submit = 0; |
| 8522 | else |
| 8523 | async_submit = 1; |
| 8524 | |
Liu Bo | 725130b | 2017-05-16 09:51:39 -0700 | [diff] [blame] | 8525 | /* bio split */ |
| 8526 | ASSERT(map_length <= INT_MAX); |
Josef Bacik | 02f57c7 | 2011-04-06 14:25:44 -0400 | [diff] [blame] | 8527 | atomic_inc(&dip->pending_bios); |
Arnd Bergmann | 3c91ee6 | 2017-05-18 15:33:29 +0200 | [diff] [blame] | 8528 | do { |
Liu Bo | 725130b | 2017-05-16 09:51:39 -0700 | [diff] [blame] | 8529 | clone_len = min_t(int, submit_len, map_length); |
Josef Bacik | 02f57c7 | 2011-04-06 14:25:44 -0400 | [diff] [blame] | 8530 | |
Liu Bo | 725130b | 2017-05-16 09:51:39 -0700 | [diff] [blame] | 8531 | /* |
| 8532 | * This will never fail as it's passing GPF_NOFS and |
| 8533 | * the allocation is backed by btrfs_bioset. |
| 8534 | */ |
Liu Bo | e477094 | 2017-05-16 10:57:14 -0700 | [diff] [blame] | 8535 | bio = btrfs_bio_clone_partial(orig_bio, clone_offset, |
Liu Bo | 725130b | 2017-05-16 09:51:39 -0700 | [diff] [blame] | 8536 | clone_len); |
| 8537 | bio->bi_private = dip; |
| 8538 | bio->bi_end_io = btrfs_end_dio_bio; |
| 8539 | btrfs_io_bio(bio)->logical = file_offset; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8540 | |
Liu Bo | 725130b | 2017-05-16 09:51:39 -0700 | [diff] [blame] | 8541 | ASSERT(submit_len >= clone_len); |
| 8542 | submit_len -= clone_len; |
| 8543 | if (submit_len == 0) |
| 8544 | break; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8545 | |
Liu Bo | 725130b | 2017-05-16 09:51:39 -0700 | [diff] [blame] | 8546 | /* |
| 8547 | * Increase the count before we submit the bio so we know |
| 8548 | * the end IO handler won't happen before we increase the |
| 8549 | * count. Otherwise, the dip might get freed before we're |
| 8550 | * done setting it up. |
| 8551 | */ |
| 8552 | atomic_inc(&dip->pending_bios); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8553 | |
Linus Torvalds | 66ba772 | 2017-09-09 13:27:51 -0700 | [diff] [blame] | 8554 | status = __btrfs_submit_dio_bio(bio, inode, file_offset, |
Omar Sandoval | 58efbc9 | 2017-08-22 23:45:59 -0700 | [diff] [blame] | 8555 | async_submit); |
| 8556 | if (status) { |
Liu Bo | 725130b | 2017-05-16 09:51:39 -0700 | [diff] [blame] | 8557 | bio_put(bio); |
| 8558 | atomic_dec(&dip->pending_bios); |
| 8559 | goto out_err; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8560 | } |
Liu Bo | 725130b | 2017-05-16 09:51:39 -0700 | [diff] [blame] | 8561 | |
| 8562 | clone_offset += clone_len; |
| 8563 | start_sector += clone_len >> 9; |
| 8564 | file_offset += clone_len; |
| 8565 | |
| 8566 | map_length = submit_len; |
| 8567 | ret = btrfs_map_block(fs_info, btrfs_op(orig_bio), |
| 8568 | start_sector << 9, &map_length, NULL, 0); |
| 8569 | if (ret) |
| 8570 | goto out_err; |
Arnd Bergmann | 3c91ee6 | 2017-05-18 15:33:29 +0200 | [diff] [blame] | 8571 | } while (submit_len > 0); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8572 | |
Josef Bacik | 02f57c7 | 2011-04-06 14:25:44 -0400 | [diff] [blame] | 8573 | submit: |
Linus Torvalds | 66ba772 | 2017-09-09 13:27:51 -0700 | [diff] [blame] | 8574 | status = __btrfs_submit_dio_bio(bio, inode, file_offset, async_submit); |
Omar Sandoval | 58efbc9 | 2017-08-22 23:45:59 -0700 | [diff] [blame] | 8575 | if (!status) |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8576 | return 0; |
| 8577 | |
| 8578 | bio_put(bio); |
| 8579 | out_err: |
| 8580 | dip->errors = 1; |
| 8581 | /* |
| 8582 | * before atomic variable goto zero, we must |
| 8583 | * make sure dip->errors is perceived to be set. |
| 8584 | */ |
Peter Zijlstra | 4e857c5 | 2014-03-17 18:06:10 +0100 | [diff] [blame] | 8585 | smp_mb__before_atomic(); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8586 | if (atomic_dec_and_test(&dip->pending_bios)) |
| 8587 | bio_io_error(dip->orig_bio); |
| 8588 | |
| 8589 | /* bio_end_io() will handle error, so we needn't return it */ |
| 8590 | return 0; |
| 8591 | } |
| 8592 | |
Mike Christie | 8a4c1e4 | 2016-06-05 14:31:50 -0500 | [diff] [blame] | 8593 | static void btrfs_submit_direct(struct bio *dio_bio, struct inode *inode, |
| 8594 | loff_t file_offset) |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8595 | { |
Filipe Manana | 61de718 | 2015-07-01 12:13:10 +0100 | [diff] [blame] | 8596 | struct btrfs_dio_private *dip = NULL; |
Liu Bo | 3892ac9 | 2017-04-17 15:00:28 -0700 | [diff] [blame] | 8597 | struct bio *bio = NULL; |
| 8598 | struct btrfs_io_bio *io_bio; |
Mike Christie | 8a4c1e4 | 2016-06-05 14:31:50 -0500 | [diff] [blame] | 8599 | bool write = (bio_op(dio_bio) == REQ_OP_WRITE); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8600 | int ret = 0; |
| 8601 | |
David Sterba | 8b6c1d5 | 2017-06-02 17:48:13 +0200 | [diff] [blame] | 8602 | bio = btrfs_bio_clone(dio_bio); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8603 | |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 8604 | dip = kzalloc(sizeof(*dip), GFP_NOFS); |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 8605 | if (!dip) { |
| 8606 | ret = -ENOMEM; |
Filipe Manana | 61de718 | 2015-07-01 12:13:10 +0100 | [diff] [blame] | 8607 | goto free_ordered; |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 8608 | } |
| 8609 | |
| 8610 | dip->private = dio_bio->bi_private; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8611 | dip->inode = inode; |
| 8612 | dip->logical_offset = file_offset; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 8613 | dip->bytes = dio_bio->bi_iter.bi_size; |
| 8614 | dip->disk_bytenr = (u64)dio_bio->bi_iter.bi_sector << 9; |
Liu Bo | 3892ac9 | 2017-04-17 15:00:28 -0700 | [diff] [blame] | 8615 | bio->bi_private = dip; |
| 8616 | dip->orig_bio = bio; |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 8617 | dip->dio_bio = dio_bio; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8618 | atomic_set(&dip->pending_bios, 0); |
Liu Bo | 3892ac9 | 2017-04-17 15:00:28 -0700 | [diff] [blame] | 8619 | io_bio = btrfs_io_bio(bio); |
| 8620 | io_bio->logical = file_offset; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8621 | |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 8622 | if (write) { |
Liu Bo | 3892ac9 | 2017-04-17 15:00:28 -0700 | [diff] [blame] | 8623 | bio->bi_end_io = btrfs_endio_direct_write; |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 8624 | } else { |
Liu Bo | 3892ac9 | 2017-04-17 15:00:28 -0700 | [diff] [blame] | 8625 | bio->bi_end_io = btrfs_endio_direct_read; |
Miao Xie | c1dc089 | 2014-09-12 18:43:56 +0800 | [diff] [blame] | 8626 | dip->subio_endio = btrfs_subio_endio_read; |
| 8627 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8628 | |
Filipe Manana | f28a492 | 2015-12-08 19:23:20 +0000 | [diff] [blame] | 8629 | /* |
| 8630 | * Reset the range for unsubmitted ordered extents (to a 0 length range) |
| 8631 | * even if we fail to submit a bio, because in such case we do the |
| 8632 | * corresponding error handling below and it must not be done a second |
| 8633 | * time by btrfs_direct_IO(). |
| 8634 | */ |
| 8635 | if (write) { |
| 8636 | struct btrfs_dio_data *dio_data = current->journal_info; |
| 8637 | |
| 8638 | dio_data->unsubmitted_oe_range_end = dip->logical_offset + |
| 8639 | dip->bytes; |
| 8640 | dio_data->unsubmitted_oe_range_start = |
| 8641 | dio_data->unsubmitted_oe_range_end; |
| 8642 | } |
| 8643 | |
Nikolay Borisov | e6961ca | 2017-08-03 15:44:58 +0300 | [diff] [blame] | 8644 | ret = btrfs_submit_direct_hook(dip); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 8645 | if (!ret) |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 8646 | return; |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 8647 | |
Liu Bo | 3892ac9 | 2017-04-17 15:00:28 -0700 | [diff] [blame] | 8648 | if (io_bio->end_io) |
| 8649 | io_bio->end_io(io_bio, ret); |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 8650 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8651 | free_ordered: |
| 8652 | /* |
Filipe Manana | 61de718 | 2015-07-01 12:13:10 +0100 | [diff] [blame] | 8653 | * If we arrived here it means either we failed to submit the dip |
| 8654 | * or we either failed to clone the dio_bio or failed to allocate the |
| 8655 | * dip. If we cloned the dio_bio and allocated the dip, we can just |
| 8656 | * call bio_endio against our io_bio so that we get proper resource |
| 8657 | * cleanup if we fail to submit the dip, otherwise, we must do the |
| 8658 | * same as btrfs_endio_direct_[write|read] because we can't call these |
| 8659 | * callbacks - they require an allocated dip and a clone of dio_bio. |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8660 | */ |
Liu Bo | 3892ac9 | 2017-04-17 15:00:28 -0700 | [diff] [blame] | 8661 | if (bio && dip) { |
Guoqing Jiang | 054ec2f | 2017-06-02 16:08:50 +0800 | [diff] [blame] | 8662 | bio_io_error(bio); |
Filipe Manana | 61de718 | 2015-07-01 12:13:10 +0100 | [diff] [blame] | 8663 | /* |
Liu Bo | 3892ac9 | 2017-04-17 15:00:28 -0700 | [diff] [blame] | 8664 | * The end io callbacks free our dip, do the final put on bio |
Filipe Manana | 61de718 | 2015-07-01 12:13:10 +0100 | [diff] [blame] | 8665 | * and all the cleanup and final put for dio_bio (through |
| 8666 | * dio_end_io()). |
| 8667 | */ |
| 8668 | dip = NULL; |
Liu Bo | 3892ac9 | 2017-04-17 15:00:28 -0700 | [diff] [blame] | 8669 | bio = NULL; |
Filipe Manana | 61de718 | 2015-07-01 12:13:10 +0100 | [diff] [blame] | 8670 | } else { |
Filipe Manana | 1454377 | 2015-11-24 16:23:54 +0000 | [diff] [blame] | 8671 | if (write) |
Qu Wenruo | 52427260 | 2017-03-08 10:25:52 +0800 | [diff] [blame] | 8672 | __endio_write_update_ordered(inode, |
Filipe Manana | 1454377 | 2015-11-24 16:23:54 +0000 | [diff] [blame] | 8673 | file_offset, |
| 8674 | dio_bio->bi_iter.bi_size, |
Qu Wenruo | 52427260 | 2017-03-08 10:25:52 +0800 | [diff] [blame] | 8675 | false); |
Filipe Manana | 1454377 | 2015-11-24 16:23:54 +0000 | [diff] [blame] | 8676 | else |
Filipe Manana | 61de718 | 2015-07-01 12:13:10 +0100 | [diff] [blame] | 8677 | unlock_extent(&BTRFS_I(inode)->io_tree, file_offset, |
| 8678 | file_offset + dio_bio->bi_iter.bi_size - 1); |
Filipe Manana | 1454377 | 2015-11-24 16:23:54 +0000 | [diff] [blame] | 8679 | |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 8680 | dio_bio->bi_status = BLK_STS_IOERR; |
Filipe Manana | 61de718 | 2015-07-01 12:13:10 +0100 | [diff] [blame] | 8681 | /* |
| 8682 | * Releases and cleans up our dio_bio, no need to bio_put() |
| 8683 | * nor bio_endio()/bio_io_error() against dio_bio. |
| 8684 | */ |
Christoph Hellwig | 4055351 | 2017-06-03 09:37:58 +0200 | [diff] [blame] | 8685 | dio_end_io(dio_bio); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8686 | } |
Liu Bo | 3892ac9 | 2017-04-17 15:00:28 -0700 | [diff] [blame] | 8687 | if (bio) |
| 8688 | bio_put(bio); |
Filipe Manana | 61de718 | 2015-07-01 12:13:10 +0100 | [diff] [blame] | 8689 | kfree(dip); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8690 | } |
| 8691 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 8692 | static ssize_t check_direct_IO(struct btrfs_fs_info *fs_info, |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 8693 | const struct iov_iter *iter, loff_t offset) |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 8694 | { |
| 8695 | int seg; |
Josef Bacik | a1b75f7 | 2011-04-08 15:51:18 +0000 | [diff] [blame] | 8696 | int i; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8697 | unsigned int blocksize_mask = fs_info->sectorsize - 1; |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 8698 | ssize_t retval = -EINVAL; |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 8699 | |
| 8700 | if (offset & blocksize_mask) |
| 8701 | goto out; |
| 8702 | |
Al Viro | 28060d5 | 2014-03-22 05:15:17 -0400 | [diff] [blame] | 8703 | if (iov_iter_alignment(iter) & blocksize_mask) |
| 8704 | goto out; |
Josef Bacik | a1b75f7 | 2011-04-08 15:51:18 +0000 | [diff] [blame] | 8705 | |
Al Viro | 28060d5 | 2014-03-22 05:15:17 -0400 | [diff] [blame] | 8706 | /* If this is a write we don't need to check anymore */ |
Al Viro | cd27e45 | 2016-10-10 13:39:05 -0400 | [diff] [blame] | 8707 | if (iov_iter_rw(iter) != READ || !iter_is_iovec(iter)) |
Al Viro | 28060d5 | 2014-03-22 05:15:17 -0400 | [diff] [blame] | 8708 | return 0; |
| 8709 | /* |
| 8710 | * Check to make sure we don't have duplicate iov_base's in this |
| 8711 | * iovec, if so return EINVAL, otherwise we'll get csum errors |
| 8712 | * when reading back. |
| 8713 | */ |
| 8714 | for (seg = 0; seg < iter->nr_segs; seg++) { |
| 8715 | for (i = seg + 1; i < iter->nr_segs; i++) { |
| 8716 | if (iter->iov[seg].iov_base == iter->iov[i].iov_base) |
Josef Bacik | a1b75f7 | 2011-04-08 15:51:18 +0000 | [diff] [blame] | 8717 | goto out; |
| 8718 | } |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 8719 | } |
| 8720 | retval = 0; |
| 8721 | out: |
| 8722 | return retval; |
| 8723 | } |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 8724 | |
Christoph Hellwig | c8b8e32 | 2016-04-07 08:51:58 -0700 | [diff] [blame] | 8725 | 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] | 8726 | { |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8727 | struct file *file = iocb->ki_filp; |
| 8728 | struct inode *inode = file->f_mapping->host; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8729 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
chandan | 50745b0 | 2015-08-28 21:10:13 +0530 | [diff] [blame] | 8730 | struct btrfs_dio_data dio_data = { 0 }; |
Qu Wenruo | 364ecf3 | 2017-02-27 15:10:38 +0800 | [diff] [blame] | 8731 | struct extent_changeset *data_reserved = NULL; |
Christoph Hellwig | c8b8e32 | 2016-04-07 08:51:58 -0700 | [diff] [blame] | 8732 | loff_t offset = iocb->ki_pos; |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 8733 | size_t count = 0; |
Miao Xie | 2e60a51 | 2013-02-08 07:01:08 +0000 | [diff] [blame] | 8734 | int flags = 0; |
Miao Xie | 38851cc | 2013-02-08 07:04:11 +0000 | [diff] [blame] | 8735 | bool wakeup = true; |
| 8736 | bool relock = false; |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 8737 | ssize_t ret; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 8738 | |
Nikolay Borisov | 8c70c9f | 2017-08-21 12:43:46 +0300 | [diff] [blame] | 8739 | if (check_direct_IO(fs_info, iter, offset)) |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 8740 | return 0; |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 8741 | |
Jens Axboe | fe0f07d | 2015-04-15 17:05:48 -0600 | [diff] [blame] | 8742 | inode_dio_begin(inode); |
Miao Xie | 38851cc | 2013-02-08 07:04:11 +0000 | [diff] [blame] | 8743 | |
Josef Bacik | 0e267c4 | 2013-07-02 10:38:02 -0400 | [diff] [blame] | 8744 | /* |
Miao Xie | 41bd9ca | 2014-03-06 13:54:57 +0800 | [diff] [blame] | 8745 | * The generic stuff only does filemap_write_and_wait_range, which |
| 8746 | * isn't enough if we've written compressed pages to this area, so |
| 8747 | * we need to flush the dirty pages again to make absolutely sure |
| 8748 | * that any outstanding dirty pages are on disk. |
Josef Bacik | 0e267c4 | 2013-07-02 10:38:02 -0400 | [diff] [blame] | 8749 | */ |
Al Viro | a6cbcd4 | 2014-03-04 22:38:00 -0500 | [diff] [blame] | 8750 | count = iov_iter_count(iter); |
Miao Xie | 41bd9ca | 2014-03-06 13:54:57 +0800 | [diff] [blame] | 8751 | if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, |
| 8752 | &BTRFS_I(inode)->runtime_flags)) |
Wang Shilong | 9a025a0 | 2014-07-17 11:44:13 +0800 | [diff] [blame] | 8753 | filemap_fdatawrite_range(inode->i_mapping, offset, |
| 8754 | offset + count - 1); |
Josef Bacik | 0e267c4 | 2013-07-02 10:38:02 -0400 | [diff] [blame] | 8755 | |
Omar Sandoval | 6f67376 | 2015-03-16 04:33:52 -0700 | [diff] [blame] | 8756 | if (iov_iter_rw(iter) == WRITE) { |
Miao Xie | 38851cc | 2013-02-08 07:04:11 +0000 | [diff] [blame] | 8757 | /* |
| 8758 | * If the write DIO is beyond the EOF, we need update |
| 8759 | * the isize, but it is protected by i_mutex. So we can |
| 8760 | * not unlock the i_mutex at this case. |
| 8761 | */ |
| 8762 | if (offset + count <= inode->i_size) { |
Liu Bo | 4aaedfb | 2016-12-14 22:36:05 -0800 | [diff] [blame] | 8763 | dio_data.overwrite = 1; |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 8764 | inode_unlock(inode); |
Miao Xie | 38851cc | 2013-02-08 07:04:11 +0000 | [diff] [blame] | 8765 | relock = true; |
Goldwyn Rodrigues | edf064e | 2017-06-20 07:05:49 -0500 | [diff] [blame] | 8766 | } else if (iocb->ki_flags & IOCB_NOWAIT) { |
| 8767 | ret = -EAGAIN; |
| 8768 | goto out; |
Miao Xie | 38851cc | 2013-02-08 07:04:11 +0000 | [diff] [blame] | 8769 | } |
Qu Wenruo | 364ecf3 | 2017-02-27 15:10:38 +0800 | [diff] [blame] | 8770 | ret = btrfs_delalloc_reserve_space(inode, &data_reserved, |
| 8771 | offset, count); |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 8772 | if (ret) |
Miao Xie | 38851cc | 2013-02-08 07:04:11 +0000 | [diff] [blame] | 8773 | goto out; |
Josef Bacik | e1cbbfa | 2015-03-17 10:52:28 -0400 | [diff] [blame] | 8774 | |
| 8775 | /* |
| 8776 | * We need to know how many extents we reserved so that we can |
| 8777 | * do the accounting properly if we go over the number we |
| 8778 | * originally calculated. Abuse current->journal_info for this. |
| 8779 | */ |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 8780 | dio_data.reserve = round_up(count, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8781 | fs_info->sectorsize); |
Filipe Manana | f28a492 | 2015-12-08 19:23:20 +0000 | [diff] [blame] | 8782 | dio_data.unsubmitted_oe_range_start = (u64)offset; |
| 8783 | dio_data.unsubmitted_oe_range_end = (u64)offset; |
chandan | 50745b0 | 2015-08-28 21:10:13 +0530 | [diff] [blame] | 8784 | current->journal_info = &dio_data; |
Chandan Rajendra | 97dcdea | 2016-12-23 15:00:18 +0530 | [diff] [blame] | 8785 | down_read(&BTRFS_I(inode)->dio_sem); |
David Sterba | ee39b43 | 2014-09-30 01:33:33 +0200 | [diff] [blame] | 8786 | } else if (test_bit(BTRFS_INODE_READDIO_NEED_LOCK, |
| 8787 | &BTRFS_I(inode)->runtime_flags)) { |
Jens Axboe | fe0f07d | 2015-04-15 17:05:48 -0600 | [diff] [blame] | 8788 | inode_dio_end(inode); |
Miao Xie | 38851cc | 2013-02-08 07:04:11 +0000 | [diff] [blame] | 8789 | flags = DIO_LOCKING | DIO_SKIP_HOLES; |
| 8790 | wakeup = false; |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 8791 | } |
| 8792 | |
Omar Sandoval | 17f8c84 | 2015-03-16 04:33:50 -0700 | [diff] [blame] | 8793 | ret = __blockdev_direct_IO(iocb, inode, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8794 | fs_info->fs_devices->latest_bdev, |
Christoph Hellwig | c8b8e32 | 2016-04-07 08:51:58 -0700 | [diff] [blame] | 8795 | iter, btrfs_get_blocks_direct, NULL, |
Omar Sandoval | 17f8c84 | 2015-03-16 04:33:50 -0700 | [diff] [blame] | 8796 | btrfs_submit_direct, flags); |
Omar Sandoval | 6f67376 | 2015-03-16 04:33:52 -0700 | [diff] [blame] | 8797 | if (iov_iter_rw(iter) == WRITE) { |
Chandan Rajendra | 97dcdea | 2016-12-23 15:00:18 +0530 | [diff] [blame] | 8798 | up_read(&BTRFS_I(inode)->dio_sem); |
Josef Bacik | e1cbbfa | 2015-03-17 10:52:28 -0400 | [diff] [blame] | 8799 | current->journal_info = NULL; |
Liu Bo | ddba1bf | 2015-06-17 16:59:58 +0800 | [diff] [blame] | 8800 | if (ret < 0 && ret != -EIOCBQUEUED) { |
chandan | 50745b0 | 2015-08-28 21:10:13 +0530 | [diff] [blame] | 8801 | if (dio_data.reserve) |
Qu Wenruo | bc42bda | 2017-02-27 15:10:39 +0800 | [diff] [blame] | 8802 | btrfs_delalloc_release_space(inode, data_reserved, |
| 8803 | offset, dio_data.reserve); |
Filipe Manana | f28a492 | 2015-12-08 19:23:20 +0000 | [diff] [blame] | 8804 | /* |
| 8805 | * On error we might have left some ordered extents |
| 8806 | * without submitting corresponding bios for them, so |
| 8807 | * cleanup them up to avoid other tasks getting them |
| 8808 | * and waiting for them to complete forever. |
| 8809 | */ |
| 8810 | if (dio_data.unsubmitted_oe_range_start < |
| 8811 | dio_data.unsubmitted_oe_range_end) |
Qu Wenruo | 52427260 | 2017-03-08 10:25:52 +0800 | [diff] [blame] | 8812 | __endio_write_update_ordered(inode, |
Filipe Manana | f28a492 | 2015-12-08 19:23:20 +0000 | [diff] [blame] | 8813 | dio_data.unsubmitted_oe_range_start, |
| 8814 | dio_data.unsubmitted_oe_range_end - |
| 8815 | dio_data.unsubmitted_oe_range_start, |
Qu Wenruo | 52427260 | 2017-03-08 10:25:52 +0800 | [diff] [blame] | 8816 | false); |
Liu Bo | ddba1bf | 2015-06-17 16:59:58 +0800 | [diff] [blame] | 8817 | } else if (ret >= 0 && (size_t)ret < count) |
Qu Wenruo | bc42bda | 2017-02-27 15:10:39 +0800 | [diff] [blame] | 8818 | btrfs_delalloc_release_space(inode, data_reserved, |
| 8819 | offset, count - (size_t)ret); |
Josef Bacik | 69fe2d7 | 2017-10-19 14:15:57 -0400 | [diff] [blame] | 8820 | btrfs_delalloc_release_extents(BTRFS_I(inode), count); |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 8821 | } |
Miao Xie | 38851cc | 2013-02-08 07:04:11 +0000 | [diff] [blame] | 8822 | out: |
Miao Xie | 2e60a51 | 2013-02-08 07:01:08 +0000 | [diff] [blame] | 8823 | if (wakeup) |
Jens Axboe | fe0f07d | 2015-04-15 17:05:48 -0600 | [diff] [blame] | 8824 | inode_dio_end(inode); |
Miao Xie | 38851cc | 2013-02-08 07:04:11 +0000 | [diff] [blame] | 8825 | if (relock) |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 8826 | inode_lock(inode); |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 8827 | |
Qu Wenruo | 364ecf3 | 2017-02-27 15:10:38 +0800 | [diff] [blame] | 8828 | extent_changeset_free(data_reserved); |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 8829 | return ret; |
Chris Mason | 1643298 | 2008-04-10 10:23:21 -0400 | [diff] [blame] | 8830 | } |
| 8831 | |
Tsutomu Itoh | 05dadc0 | 2012-11-29 05:08:26 +0000 | [diff] [blame] | 8832 | #define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC) |
| 8833 | |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 8834 | static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, |
| 8835 | __u64 start, __u64 len) |
| 8836 | { |
Tsutomu Itoh | 05dadc0 | 2012-11-29 05:08:26 +0000 | [diff] [blame] | 8837 | int ret; |
| 8838 | |
| 8839 | ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS); |
| 8840 | if (ret) |
| 8841 | return ret; |
| 8842 | |
David Sterba | 2135fb9 | 2017-06-23 04:09:57 +0200 | [diff] [blame] | 8843 | return extent_fiemap(inode, fieinfo, start, len); |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 8844 | } |
| 8845 | |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 8846 | int btrfs_readpage(struct file *file, struct page *page) |
| 8847 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 8848 | struct extent_io_tree *tree; |
| 8849 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
Jan Schmidt | 8ddc7d9 | 2011-06-13 20:02:58 +0200 | [diff] [blame] | 8850 | return extent_read_full_page(tree, page, btrfs_get_extent, 0); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8851 | } |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 8852 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8853 | static int btrfs_writepage(struct page *page, struct writeback_control *wbc) |
| 8854 | { |
Josef Bacik | be7bd73 | 2015-10-22 15:05:09 -0400 | [diff] [blame] | 8855 | struct inode *inode = page->mapping->host; |
| 8856 | int ret; |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8857 | |
| 8858 | if (current->flags & PF_MEMALLOC) { |
| 8859 | redirty_page_for_writepage(wbc, page); |
| 8860 | unlock_page(page); |
| 8861 | return 0; |
| 8862 | } |
Josef Bacik | be7bd73 | 2015-10-22 15:05:09 -0400 | [diff] [blame] | 8863 | |
| 8864 | /* |
| 8865 | * If we are under memory pressure we will call this directly from the |
| 8866 | * VM, we need to make sure we have the inode referenced for the ordered |
| 8867 | * extent. If not just return like we didn't do anything. |
| 8868 | */ |
| 8869 | if (!igrab(inode)) { |
| 8870 | redirty_page_for_writepage(wbc, page); |
| 8871 | return AOP_WRITEPAGE_ACTIVATE; |
| 8872 | } |
Nikolay Borisov | 0a9b0e5 | 2017-12-08 15:55:59 +0200 | [diff] [blame] | 8873 | ret = extent_write_full_page(page, wbc); |
Josef Bacik | be7bd73 | 2015-10-22 15:05:09 -0400 | [diff] [blame] | 8874 | btrfs_add_delayed_iput(inode); |
| 8875 | return ret; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8876 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8877 | |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 8878 | static int btrfs_writepages(struct address_space *mapping, |
| 8879 | struct writeback_control *wbc) |
Chris Mason | b293f02e | 2007-11-01 19:45:34 -0400 | [diff] [blame] | 8880 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 8881 | struct extent_io_tree *tree; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 8882 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 8883 | tree = &BTRFS_I(mapping->host)->io_tree; |
David Sterba | 4331759 | 2017-06-23 03:46:07 +0200 | [diff] [blame] | 8884 | return extent_writepages(tree, mapping, wbc); |
Chris Mason | b293f02e | 2007-11-01 19:45:34 -0400 | [diff] [blame] | 8885 | } |
| 8886 | |
Chris Mason | 3ab2fb5 | 2007-11-08 10:59:22 -0500 | [diff] [blame] | 8887 | static int |
| 8888 | btrfs_readpages(struct file *file, struct address_space *mapping, |
| 8889 | struct list_head *pages, unsigned nr_pages) |
| 8890 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 8891 | struct extent_io_tree *tree; |
| 8892 | tree = &BTRFS_I(mapping->host)->io_tree; |
David Sterba | 0932584 | 2017-06-23 04:09:57 +0200 | [diff] [blame] | 8893 | return extent_readpages(tree, mapping, pages, nr_pages); |
Chris Mason | 3ab2fb5 | 2007-11-08 10:59:22 -0500 | [diff] [blame] | 8894 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8895 | static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8896 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 8897 | struct extent_io_tree *tree; |
| 8898 | struct extent_map_tree *map; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8899 | int ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8900 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 8901 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
| 8902 | map = &BTRFS_I(page->mapping->host)->extent_tree; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 8903 | ret = try_release_extent_mapping(map, tree, page, gfp_flags); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8904 | if (ret == 1) { |
| 8905 | ClearPagePrivate(page); |
| 8906 | set_page_private(page, 0); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 8907 | put_page(page); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8908 | } |
| 8909 | return ret; |
| 8910 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8911 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8912 | static int btrfs_releasepage(struct page *page, gfp_t gfp_flags) |
| 8913 | { |
Chris Mason | 98509cf | 2008-09-11 15:51:43 -0400 | [diff] [blame] | 8914 | if (PageWriteback(page) || PageDirty(page)) |
| 8915 | return 0; |
Michal Hocko | 3ba7ab2 | 2017-01-09 15:39:02 +0100 | [diff] [blame] | 8916 | return __btrfs_releasepage(page, gfp_flags); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8917 | } |
| 8918 | |
Lukas Czerner | d47992f | 2013-05-21 23:17:23 -0400 | [diff] [blame] | 8919 | static void btrfs_invalidatepage(struct page *page, unsigned int offset, |
| 8920 | unsigned int length) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8921 | { |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 8922 | struct inode *inode = page->mapping->host; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 8923 | struct extent_io_tree *tree; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8924 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 8925 | struct extent_state *cached_state = NULL; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8926 | u64 page_start = page_offset(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 8927 | u64 page_end = page_start + PAGE_SIZE - 1; |
Chandan Rajendra | dbfdb6d | 2016-01-21 15:55:58 +0530 | [diff] [blame] | 8928 | u64 start; |
| 8929 | u64 end; |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 8930 | int inode_evicting = inode->i_state & I_FREEING; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8931 | |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 8932 | /* |
| 8933 | * we have the page locked, so new writeback can't start, |
| 8934 | * and the dirty bit won't be cleared while we are here. |
| 8935 | * |
| 8936 | * Wait for IO on this page so that we can safely clear |
| 8937 | * the PagePrivate2 bit and do ordered accounting |
| 8938 | */ |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8939 | wait_on_page_writeback(page); |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 8940 | |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 8941 | tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8942 | if (offset) { |
| 8943 | btrfs_releasepage(page, GFP_NOFS); |
| 8944 | return; |
| 8945 | } |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 8946 | |
| 8947 | if (!inode_evicting) |
David Sterba | ff13db4 | 2015-12-03 14:30:40 +0100 | [diff] [blame] | 8948 | lock_extent_bits(tree, page_start, page_end, &cached_state); |
Chandan Rajendra | dbfdb6d | 2016-01-21 15:55:58 +0530 | [diff] [blame] | 8949 | again: |
| 8950 | start = page_start; |
Nikolay Borisov | a776c6f | 2017-02-20 13:50:49 +0200 | [diff] [blame] | 8951 | ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), start, |
Chandan Rajendra | dbfdb6d | 2016-01-21 15:55:58 +0530 | [diff] [blame] | 8952 | page_end - start + 1); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8953 | if (ordered) { |
Chandan Rajendra | dbfdb6d | 2016-01-21 15:55:58 +0530 | [diff] [blame] | 8954 | end = min(page_end, ordered->file_offset + ordered->len - 1); |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 8955 | /* |
| 8956 | * IO on this page will never be started, so we need |
| 8957 | * to account for any ordered extents now |
| 8958 | */ |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 8959 | if (!inode_evicting) |
Chandan Rajendra | dbfdb6d | 2016-01-21 15:55:58 +0530 | [diff] [blame] | 8960 | clear_extent_bit(tree, start, end, |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 8961 | EXTENT_DIRTY | EXTENT_DELALLOC | |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 8962 | EXTENT_DELALLOC_NEW | |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 8963 | EXTENT_LOCKED | EXTENT_DO_ACCOUNTING | |
David Sterba | ae0f162 | 2017-10-31 16:37:52 +0100 | [diff] [blame] | 8964 | EXTENT_DEFRAG, 1, 0, &cached_state); |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 8965 | /* |
| 8966 | * whoever cleared the private bit is responsible |
| 8967 | * for the finish_ordered_io |
| 8968 | */ |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 8969 | if (TestClearPagePrivate2(page)) { |
| 8970 | struct btrfs_ordered_inode_tree *tree; |
| 8971 | u64 new_len; |
| 8972 | |
| 8973 | tree = &BTRFS_I(inode)->ordered_tree; |
| 8974 | |
| 8975 | spin_lock_irq(&tree->lock); |
| 8976 | set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags); |
Chandan Rajendra | dbfdb6d | 2016-01-21 15:55:58 +0530 | [diff] [blame] | 8977 | new_len = start - ordered->file_offset; |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 8978 | if (new_len < ordered->truncated_len) |
| 8979 | ordered->truncated_len = new_len; |
| 8980 | spin_unlock_irq(&tree->lock); |
| 8981 | |
| 8982 | if (btrfs_dec_test_ordered_pending(inode, &ordered, |
Chandan Rajendra | dbfdb6d | 2016-01-21 15:55:58 +0530 | [diff] [blame] | 8983 | start, |
| 8984 | end - start + 1, 1)) |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 8985 | btrfs_finish_ordered_io(ordered); |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 8986 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8987 | btrfs_put_ordered_extent(ordered); |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 8988 | if (!inode_evicting) { |
| 8989 | cached_state = NULL; |
Chandan Rajendra | dbfdb6d | 2016-01-21 15:55:58 +0530 | [diff] [blame] | 8990 | lock_extent_bits(tree, start, end, |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 8991 | &cached_state); |
| 8992 | } |
Chandan Rajendra | dbfdb6d | 2016-01-21 15:55:58 +0530 | [diff] [blame] | 8993 | |
| 8994 | start = end + 1; |
| 8995 | if (start < page_end) |
| 8996 | goto again; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8997 | } |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 8998 | |
Qu Wenruo | b9d0b38 | 2015-09-29 10:35:16 +0800 | [diff] [blame] | 8999 | /* |
| 9000 | * Qgroup reserved space handler |
| 9001 | * Page here will be either |
| 9002 | * 1) Already written to disk |
| 9003 | * In this case, its reserved space is released from data rsv map |
| 9004 | * and will be freed by delayed_ref handler finally. |
| 9005 | * So even we call qgroup_free_data(), it won't decrease reserved |
| 9006 | * space. |
| 9007 | * 2) Not written to disk |
Goldwyn Rodrigues | 0b34c26 | 2016-09-30 10:40:52 -0500 | [diff] [blame] | 9008 | * This means the reserved space should be freed here. However, |
| 9009 | * if a truncate invalidates the page (by clearing PageDirty) |
| 9010 | * and the page is accounted for while allocating extent |
| 9011 | * in btrfs_check_data_free_space() we let delayed_ref to |
| 9012 | * free the entire extent. |
Qu Wenruo | b9d0b38 | 2015-09-29 10:35:16 +0800 | [diff] [blame] | 9013 | */ |
Goldwyn Rodrigues | 0b34c26 | 2016-09-30 10:40:52 -0500 | [diff] [blame] | 9014 | if (PageDirty(page)) |
Qu Wenruo | bc42bda | 2017-02-27 15:10:39 +0800 | [diff] [blame] | 9015 | btrfs_qgroup_free_data(inode, NULL, page_start, PAGE_SIZE); |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 9016 | if (!inode_evicting) { |
| 9017 | clear_extent_bit(tree, page_start, page_end, |
| 9018 | EXTENT_LOCKED | EXTENT_DIRTY | |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 9019 | EXTENT_DELALLOC | EXTENT_DELALLOC_NEW | |
| 9020 | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1, |
David Sterba | ae0f162 | 2017-10-31 16:37:52 +0100 | [diff] [blame] | 9021 | &cached_state); |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 9022 | |
| 9023 | __btrfs_releasepage(page, GFP_NOFS); |
| 9024 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 9025 | |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 9026 | ClearPageChecked(page); |
Chris Mason | 9ad6b7bc | 2008-04-18 16:11:30 -0400 | [diff] [blame] | 9027 | if (PagePrivate(page)) { |
Chris Mason | 9ad6b7bc | 2008-04-18 16:11:30 -0400 | [diff] [blame] | 9028 | ClearPagePrivate(page); |
| 9029 | set_page_private(page, 0); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 9030 | put_page(page); |
Chris Mason | 9ad6b7bc | 2008-04-18 16:11:30 -0400 | [diff] [blame] | 9031 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9032 | } |
| 9033 | |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 9034 | /* |
| 9035 | * btrfs_page_mkwrite() is not allowed to change the file size as it gets |
| 9036 | * called from a page fault handler when a page is first dirtied. Hence we must |
| 9037 | * be careful to check for EOF conditions here. We set the page up correctly |
| 9038 | * for a written page which means we get ENOSPC checking when writing into |
| 9039 | * holes and correct delalloc and unwritten extent mapping on filesystems that |
| 9040 | * support these features. |
| 9041 | * |
| 9042 | * We are not allowed to take the i_mutex here so we have to play games to |
| 9043 | * protect against truncate races as the page could now be beyond EOF. Because |
| 9044 | * vmtruncate() writes the inode size before removing pages, once we have the |
| 9045 | * page lock we can determine safely if the page is beyond EOF. If it is not |
| 9046 | * beyond EOF, then the page is guaranteed safe against truncation until we |
| 9047 | * unlock the page. |
| 9048 | */ |
Dave Jiang | 11bac80 | 2017-02-24 14:56:41 -0800 | [diff] [blame] | 9049 | int btrfs_page_mkwrite(struct vm_fault *vmf) |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 9050 | { |
Nick Piggin | c2ec175 | 2009-03-31 15:23:21 -0700 | [diff] [blame] | 9051 | struct page *page = vmf->page; |
Dave Jiang | 11bac80 | 2017-02-24 14:56:41 -0800 | [diff] [blame] | 9052 | struct inode *inode = file_inode(vmf->vma->vm_file); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9053 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 9054 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
| 9055 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 9056 | struct extent_state *cached_state = NULL; |
Qu Wenruo | 364ecf3 | 2017-02-27 15:10:38 +0800 | [diff] [blame] | 9057 | struct extent_changeset *data_reserved = NULL; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 9058 | char *kaddr; |
| 9059 | unsigned long zero_start; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 9060 | loff_t size; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 9061 | int ret; |
Chris Mason | 9998eb7 | 2012-01-25 13:47:40 -0500 | [diff] [blame] | 9062 | int reserved = 0; |
Chandan Rajendra | d0b7da88 | 2016-01-21 15:55:57 +0530 | [diff] [blame] | 9063 | u64 reserved_space; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 9064 | u64 page_start; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 9065 | u64 page_end; |
Chandan Rajendra | d0b7da88 | 2016-01-21 15:55:57 +0530 | [diff] [blame] | 9066 | u64 end; |
| 9067 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 9068 | reserved_space = PAGE_SIZE; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 9069 | |
Jan Kara | b2b5ef5 | 2012-06-12 16:20:45 +0200 | [diff] [blame] | 9070 | sb_start_pagefault(inode->i_sb); |
Qu Wenruo | df48063 | 2015-09-08 17:25:54 +0800 | [diff] [blame] | 9071 | page_start = page_offset(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 9072 | page_end = page_start + PAGE_SIZE - 1; |
Chandan Rajendra | d0b7da88 | 2016-01-21 15:55:57 +0530 | [diff] [blame] | 9073 | end = page_end; |
Qu Wenruo | df48063 | 2015-09-08 17:25:54 +0800 | [diff] [blame] | 9074 | |
Chandan Rajendra | d0b7da88 | 2016-01-21 15:55:57 +0530 | [diff] [blame] | 9075 | /* |
| 9076 | * Reserving delalloc space after obtaining the page lock can lead to |
| 9077 | * deadlock. For example, if a dirty page is locked by this function |
| 9078 | * and the call to btrfs_delalloc_reserve_space() ends up triggering |
| 9079 | * dirty page write out, then the btrfs_writepage() function could |
| 9080 | * end up waiting indefinitely to get a lock on the page currently |
| 9081 | * being processed by btrfs_page_mkwrite() function. |
| 9082 | */ |
Qu Wenruo | 364ecf3 | 2017-02-27 15:10:38 +0800 | [diff] [blame] | 9083 | ret = btrfs_delalloc_reserve_space(inode, &data_reserved, page_start, |
Chandan Rajendra | d0b7da88 | 2016-01-21 15:55:57 +0530 | [diff] [blame] | 9084 | reserved_space); |
Chris Mason | 9998eb7 | 2012-01-25 13:47:40 -0500 | [diff] [blame] | 9085 | if (!ret) { |
Dave Jiang | 11bac80 | 2017-02-24 14:56:41 -0800 | [diff] [blame] | 9086 | ret = file_update_time(vmf->vma->vm_file); |
Chris Mason | 9998eb7 | 2012-01-25 13:47:40 -0500 | [diff] [blame] | 9087 | reserved = 1; |
| 9088 | } |
Nick Piggin | 56a76f8 | 2009-03-31 15:23:23 -0700 | [diff] [blame] | 9089 | if (ret) { |
| 9090 | if (ret == -ENOMEM) |
| 9091 | ret = VM_FAULT_OOM; |
| 9092 | else /* -ENOSPC, -EIO, etc */ |
| 9093 | ret = VM_FAULT_SIGBUS; |
Chris Mason | 9998eb7 | 2012-01-25 13:47:40 -0500 | [diff] [blame] | 9094 | if (reserved) |
| 9095 | goto out; |
| 9096 | goto out_noreserve; |
Nick Piggin | 56a76f8 | 2009-03-31 15:23:23 -0700 | [diff] [blame] | 9097 | } |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 9098 | |
Nick Piggin | 56a76f8 | 2009-03-31 15:23:23 -0700 | [diff] [blame] | 9099 | ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */ |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 9100 | again: |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 9101 | lock_page(page); |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 9102 | size = i_size_read(inode); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 9103 | |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 9104 | if ((page->mapping != inode->i_mapping) || |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 9105 | (page_start >= size)) { |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 9106 | /* page got truncated out from underneath us */ |
| 9107 | goto out_unlock; |
| 9108 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 9109 | wait_on_page_writeback(page); |
| 9110 | |
David Sterba | ff13db4 | 2015-12-03 14:30:40 +0100 | [diff] [blame] | 9111 | lock_extent_bits(io_tree, page_start, page_end, &cached_state); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 9112 | set_page_extent_mapped(page); |
| 9113 | |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 9114 | /* |
| 9115 | * we can't set the delalloc bits if there are pending ordered |
| 9116 | * extents. Drop our locks and wait for them to finish |
| 9117 | */ |
Nikolay Borisov | a776c6f | 2017-02-20 13:50:49 +0200 | [diff] [blame] | 9118 | ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start, |
| 9119 | PAGE_SIZE); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 9120 | if (ordered) { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 9121 | unlock_extent_cached(io_tree, page_start, page_end, |
| 9122 | &cached_state, GFP_NOFS); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 9123 | unlock_page(page); |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 9124 | btrfs_start_ordered_extent(inode, ordered, 1); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 9125 | btrfs_put_ordered_extent(ordered); |
| 9126 | goto again; |
| 9127 | } |
| 9128 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 9129 | if (page->index == ((size - 1) >> PAGE_SHIFT)) { |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 9130 | reserved_space = round_up(size - page_start, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9131 | fs_info->sectorsize); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 9132 | if (reserved_space < PAGE_SIZE) { |
Chandan Rajendra | d0b7da88 | 2016-01-21 15:55:57 +0530 | [diff] [blame] | 9133 | end = page_start + reserved_space - 1; |
Qu Wenruo | bc42bda | 2017-02-27 15:10:39 +0800 | [diff] [blame] | 9134 | btrfs_delalloc_release_space(inode, data_reserved, |
| 9135 | page_start, PAGE_SIZE - reserved_space); |
Chandan Rajendra | d0b7da88 | 2016-01-21 15:55:57 +0530 | [diff] [blame] | 9136 | } |
| 9137 | } |
| 9138 | |
Josef Bacik | fbf1908 | 2009-10-01 17:10:23 -0400 | [diff] [blame] | 9139 | /* |
Liu Bo | 5416034 | 2016-12-13 12:15:19 -0800 | [diff] [blame] | 9140 | * page_mkwrite gets called when the page is firstly dirtied after it's |
| 9141 | * faulted in, but write(2) could also dirty a page and set delalloc |
| 9142 | * bits, thus in this case for space account reason, we still need to |
| 9143 | * clear any delalloc bits within this page range since we have to |
| 9144 | * reserve data&meta space before lock_page() (see above comments). |
Josef Bacik | fbf1908 | 2009-10-01 17:10:23 -0400 | [diff] [blame] | 9145 | */ |
Chandan Rajendra | d0b7da88 | 2016-01-21 15:55:57 +0530 | [diff] [blame] | 9146 | clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, end, |
Liu Bo | 9e8a4a8 | 2012-09-05 19:10:51 -0600 | [diff] [blame] | 9147 | EXTENT_DIRTY | EXTENT_DELALLOC | |
| 9148 | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, |
David Sterba | ae0f162 | 2017-10-31 16:37:52 +0100 | [diff] [blame] | 9149 | 0, 0, &cached_state); |
Josef Bacik | fbf1908 | 2009-10-01 17:10:23 -0400 | [diff] [blame] | 9150 | |
Filipe Manana | e3b8a48 | 2017-11-04 00:16:59 +0000 | [diff] [blame] | 9151 | ret = btrfs_set_extent_delalloc(inode, page_start, end, 0, |
Qu Wenruo | ba8b04c | 2016-07-19 16:50:36 +0800 | [diff] [blame] | 9152 | &cached_state, 0); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 9153 | if (ret) { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 9154 | unlock_extent_cached(io_tree, page_start, page_end, |
| 9155 | &cached_state, GFP_NOFS); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 9156 | ret = VM_FAULT_SIGBUS; |
| 9157 | goto out_unlock; |
| 9158 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 9159 | ret = 0; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 9160 | |
| 9161 | /* page is wholly or partially inside EOF */ |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 9162 | if (page_start + PAGE_SIZE > size) |
| 9163 | zero_start = size & ~PAGE_MASK; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 9164 | else |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 9165 | zero_start = PAGE_SIZE; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 9166 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 9167 | if (zero_start != PAGE_SIZE) { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 9168 | kaddr = kmap(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 9169 | memset(kaddr + zero_start, 0, PAGE_SIZE - zero_start); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 9170 | flush_dcache_page(page); |
| 9171 | kunmap(page); |
| 9172 | } |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 9173 | ClearPageChecked(page); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 9174 | set_page_dirty(page); |
Chris Mason | 50a9b21 | 2009-09-11 12:33:12 -0400 | [diff] [blame] | 9175 | SetPageUptodate(page); |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 9176 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9177 | BTRFS_I(inode)->last_trans = fs_info->generation; |
Chris Mason | 257c62e | 2009-10-13 13:21:08 -0400 | [diff] [blame] | 9178 | BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid; |
Liu Bo | 46d8bc3 | 2012-08-29 01:07:55 -0600 | [diff] [blame] | 9179 | 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] | 9180 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 9181 | 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] | 9182 | |
| 9183 | out_unlock: |
Jan Kara | b2b5ef5 | 2012-06-12 16:20:45 +0200 | [diff] [blame] | 9184 | if (!ret) { |
Josef Bacik | 8b62f87 | 2017-10-19 14:15:55 -0400 | [diff] [blame] | 9185 | btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE); |
Jan Kara | b2b5ef5 | 2012-06-12 16:20:45 +0200 | [diff] [blame] | 9186 | sb_end_pagefault(inode->i_sb); |
Qu Wenruo | 364ecf3 | 2017-02-27 15:10:38 +0800 | [diff] [blame] | 9187 | extent_changeset_free(data_reserved); |
Chris Mason | 50a9b21 | 2009-09-11 12:33:12 -0400 | [diff] [blame] | 9188 | return VM_FAULT_LOCKED; |
Jan Kara | b2b5ef5 | 2012-06-12 16:20:45 +0200 | [diff] [blame] | 9189 | } |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 9190 | unlock_page(page); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 9191 | out: |
Josef Bacik | 8b62f87 | 2017-10-19 14:15:55 -0400 | [diff] [blame] | 9192 | btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE); |
Qu Wenruo | bc42bda | 2017-02-27 15:10:39 +0800 | [diff] [blame] | 9193 | btrfs_delalloc_release_space(inode, data_reserved, page_start, |
| 9194 | reserved_space); |
Chris Mason | 9998eb7 | 2012-01-25 13:47:40 -0500 | [diff] [blame] | 9195 | out_noreserve: |
Jan Kara | b2b5ef5 | 2012-06-12 16:20:45 +0200 | [diff] [blame] | 9196 | sb_end_pagefault(inode->i_sb); |
Qu Wenruo | 364ecf3 | 2017-02-27 15:10:38 +0800 | [diff] [blame] | 9197 | extent_changeset_free(data_reserved); |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 9198 | return ret; |
| 9199 | } |
| 9200 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 9201 | static int btrfs_truncate(struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9202 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9203 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9204 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 9205 | struct btrfs_block_rsv *rsv; |
Josef Bacik | a71754f | 2013-06-17 17:14:39 -0400 | [diff] [blame] | 9206 | int ret = 0; |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 9207 | int err = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9208 | struct btrfs_trans_handle *trans; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9209 | u64 mask = fs_info->sectorsize - 1; |
| 9210 | u64 min_size = btrfs_calc_trunc_metadata_size(fs_info, 1); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9211 | |
Josef Bacik | 0ef8b72 | 2013-10-25 16:13:35 -0400 | [diff] [blame] | 9212 | ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask), |
| 9213 | (u64)-1); |
| 9214 | if (ret) |
| 9215 | return ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9216 | |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 9217 | /* |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 9218 | * 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] | 9219 | * 3 things going on here |
| 9220 | * |
| 9221 | * 1) We need to reserve space for our orphan item and the space to |
| 9222 | * delete our orphan item. Lord knows we don't want to have a dangling |
| 9223 | * orphan item because we didn't reserve space to remove it. |
| 9224 | * |
| 9225 | * 2) We need to reserve space to update our inode. |
| 9226 | * |
| 9227 | * 3) We need to have something to cache all the space that is going to |
| 9228 | * be free'd up by the truncate operation, but also have some slack |
| 9229 | * space reserved in case it uses space during the truncate (thank you |
| 9230 | * very much snapshotting). |
| 9231 | * |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 9232 | * 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] | 9233 | * 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] | 9234 | * 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] | 9235 | * doesn't end up using space reserved for updating the inode or |
| 9236 | * removing the orphan item. We also need to be able to stop the |
| 9237 | * transaction and start a new one, which means we need to be able to |
| 9238 | * update the inode several times, and we have no idea of knowing how |
| 9239 | * 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] | 9240 | * 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] | 9241 | * Then there is the orphan item, which does indeed need to be held on |
| 9242 | * to for the whole operation, and we need nobody to touch this reserved |
| 9243 | * space except the orphan code. |
| 9244 | * |
| 9245 | * So that leaves us with |
| 9246 | * |
| 9247 | * 1) root->orphan_block_rsv - for the orphan deletion. |
| 9248 | * 2) rsv - for the truncate reservation, which we will steal from the |
| 9249 | * transaction reservation. |
| 9250 | * 3) fs_info->trans_block_rsv - this will have 1 items worth left for |
| 9251 | * updating the inode. |
| 9252 | */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 9253 | rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP); |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 9254 | if (!rsv) |
| 9255 | return -ENOMEM; |
Josef Bacik | 4a33854 | 2011-08-29 11:01:31 -0400 | [diff] [blame] | 9256 | rsv->size = min_size; |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 9257 | rsv->failfast = 1; |
Josef Bacik | f0cd846 | 2011-03-04 14:37:08 -0500 | [diff] [blame] | 9258 | |
Josef Bacik | 907cbce | 2011-08-08 13:46:15 -0400 | [diff] [blame] | 9259 | /* |
Josef Bacik | 0712718 | 2011-08-19 10:29:59 -0400 | [diff] [blame] | 9260 | * 1 for the truncate slack space |
Josef Bacik | 907cbce | 2011-08-08 13:46:15 -0400 | [diff] [blame] | 9261 | * 1 for updating the inode. |
| 9262 | */ |
Josef Bacik | f3fe820 | 2013-01-07 17:03:21 -0500 | [diff] [blame] | 9263 | trans = btrfs_start_transaction(root, 2); |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 9264 | if (IS_ERR(trans)) { |
| 9265 | err = PTR_ERR(trans); |
| 9266 | goto out; |
| 9267 | } |
Josef Bacik | f0cd846 | 2011-03-04 14:37:08 -0500 | [diff] [blame] | 9268 | |
Josef Bacik | 907cbce | 2011-08-08 13:46:15 -0400 | [diff] [blame] | 9269 | /* Migrate the slack space for the truncate to our reserve */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9270 | ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, rsv, |
Josef Bacik | 25d609f | 2016-03-25 13:25:48 -0400 | [diff] [blame] | 9271 | min_size, 0); |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 9272 | BUG_ON(ret); |
Josef Bacik | f0cd846 | 2011-03-04 14:37:08 -0500 | [diff] [blame] | 9273 | |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 9274 | /* |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 9275 | * So if we truncate and then write and fsync we normally would just |
| 9276 | * write the extents that changed, which is a problem if we need to |
| 9277 | * first truncate that entire inode. So set this flag so we write out |
| 9278 | * all of the extents in the inode to the sync log so we're completely |
| 9279 | * safe. |
| 9280 | */ |
| 9281 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags); |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 9282 | trans->block_rsv = rsv; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 9283 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 9284 | while (1) { |
| 9285 | ret = btrfs_truncate_inode_items(trans, root, inode, |
| 9286 | inode->i_size, |
| 9287 | BTRFS_EXTENT_DATA_KEY); |
Josef Bacik | ddfae63 | 2017-10-19 14:16:02 -0400 | [diff] [blame] | 9288 | trans->block_rsv = &fs_info->trans_block_rsv; |
Chris Mason | 28ed134 | 2014-12-17 09:41:04 -0800 | [diff] [blame] | 9289 | if (ret != -ENOSPC && ret != -EAGAIN) { |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 9290 | err = ret; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 9291 | break; |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 9292 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9293 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 9294 | ret = btrfs_update_inode(trans, root, inode); |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 9295 | if (ret) { |
| 9296 | err = ret; |
| 9297 | break; |
| 9298 | } |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 9299 | |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 9300 | btrfs_end_transaction(trans); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 9301 | btrfs_btree_balance_dirty(fs_info); |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 9302 | |
| 9303 | trans = btrfs_start_transaction(root, 2); |
| 9304 | if (IS_ERR(trans)) { |
| 9305 | ret = err = PTR_ERR(trans); |
| 9306 | trans = NULL; |
| 9307 | break; |
| 9308 | } |
| 9309 | |
Wang Xiaoguang | 47b5d64 | 2016-09-07 20:17:38 +0800 | [diff] [blame] | 9310 | btrfs_block_rsv_release(fs_info, rsv, -1); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9311 | ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, |
Josef Bacik | 25d609f | 2016-03-25 13:25:48 -0400 | [diff] [blame] | 9312 | rsv, min_size, 0); |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 9313 | BUG_ON(ret); /* shouldn't happen */ |
| 9314 | trans->block_rsv = rsv; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 9315 | } |
| 9316 | |
Josef Bacik | ddfae63 | 2017-10-19 14:16:02 -0400 | [diff] [blame] | 9317 | /* |
| 9318 | * We can't call btrfs_truncate_block inside a trans handle as we could |
| 9319 | * deadlock with freeze, if we got NEED_TRUNCATE_BLOCK then we know |
| 9320 | * we've truncated everything except the last little bit, and can do |
| 9321 | * btrfs_truncate_block and then update the disk_i_size. |
| 9322 | */ |
| 9323 | if (ret == NEED_TRUNCATE_BLOCK) { |
| 9324 | btrfs_end_transaction(trans); |
| 9325 | btrfs_btree_balance_dirty(fs_info); |
| 9326 | |
| 9327 | ret = btrfs_truncate_block(inode, inode->i_size, 0, 0); |
| 9328 | if (ret) |
| 9329 | goto out; |
| 9330 | trans = btrfs_start_transaction(root, 1); |
| 9331 | if (IS_ERR(trans)) { |
| 9332 | ret = PTR_ERR(trans); |
| 9333 | goto out; |
| 9334 | } |
| 9335 | btrfs_ordered_update_i_size(inode, inode->i_size, NULL); |
| 9336 | } |
| 9337 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 9338 | if (ret == 0 && inode->i_nlink > 0) { |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 9339 | trans->block_rsv = root->orphan_block_rsv; |
Nikolay Borisov | 3d6ae7b | 2017-02-20 13:50:58 +0200 | [diff] [blame] | 9340 | ret = btrfs_orphan_del(trans, BTRFS_I(inode)); |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 9341 | if (ret) |
| 9342 | err = ret; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 9343 | } |
| 9344 | |
Chris Mason | 917c16b | 2011-11-08 14:49:59 -0500 | [diff] [blame] | 9345 | if (trans) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9346 | trans->block_rsv = &fs_info->trans_block_rsv; |
Chris Mason | 917c16b | 2011-11-08 14:49:59 -0500 | [diff] [blame] | 9347 | ret = btrfs_update_inode(trans, root, inode); |
| 9348 | if (ret && !err) |
| 9349 | err = ret; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 9350 | |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 9351 | ret = btrfs_end_transaction(trans); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 9352 | btrfs_btree_balance_dirty(fs_info); |
Chris Mason | 917c16b | 2011-11-08 14:49:59 -0500 | [diff] [blame] | 9353 | } |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 9354 | out: |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 9355 | btrfs_free_block_rsv(fs_info, rsv); |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 9356 | |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 9357 | if (ret && !err) |
| 9358 | err = ret; |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 9359 | |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 9360 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9361 | } |
| 9362 | |
Sven Wegener | 3b96362 | 2008-06-09 21:57:42 -0400 | [diff] [blame] | 9363 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 9364 | * create a new subvolume directory/inode (helper for the ioctl). |
| 9365 | */ |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 9366 | int btrfs_create_subvol_root(struct btrfs_trans_handle *trans, |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 9367 | struct btrfs_root *new_root, |
| 9368 | struct btrfs_root *parent_root, |
| 9369 | u64 new_dirid) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9370 | { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9371 | struct inode *inode; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 9372 | int err; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 9373 | u64 index = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9374 | |
Florian Albrechtskirchinger | 12fc9d0 | 2012-02-10 22:15:54 +0100 | [diff] [blame] | 9375 | inode = btrfs_new_inode(trans, new_root, NULL, "..", 2, |
| 9376 | new_dirid, new_dirid, |
| 9377 | S_IFDIR | (~current_umask() & S_IRWXUGO), |
| 9378 | &index); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 9379 | if (IS_ERR(inode)) |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 9380 | return PTR_ERR(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9381 | inode->i_op = &btrfs_dir_inode_operations; |
| 9382 | inode->i_fop = &btrfs_dir_file_operations; |
| 9383 | |
Miklos Szeredi | bfe8684 | 2011-10-28 14:13:29 +0200 | [diff] [blame] | 9384 | set_nlink(inode, 1); |
Nikolay Borisov | 6ef06d2 | 2017-02-20 13:50:34 +0200 | [diff] [blame] | 9385 | btrfs_i_size_write(BTRFS_I(inode), 0); |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 9386 | unlock_new_inode(inode); |
Sven Wegener | 3b96362 | 2008-06-09 21:57:42 -0400 | [diff] [blame] | 9387 | |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 9388 | err = btrfs_subvol_inherit_props(trans, new_root, parent_root); |
| 9389 | if (err) |
| 9390 | btrfs_err(new_root->fs_info, |
David Sterba | 351fd35 | 2014-05-15 16:48:20 +0200 | [diff] [blame] | 9391 | "error inheriting subvolume %llu properties: %d", |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 9392 | new_root->root_key.objectid, err); |
| 9393 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 9394 | err = btrfs_update_inode(trans, new_root, inode); |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 9395 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 9396 | iput(inode); |
Mark Fasheh | ce59897 | 2011-07-26 11:32:23 -0700 | [diff] [blame] | 9397 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9398 | } |
| 9399 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9400 | struct inode *btrfs_alloc_inode(struct super_block *sb) |
| 9401 | { |
Josef Bacik | 69fe2d7 | 2017-10-19 14:15:57 -0400 | [diff] [blame] | 9402 | struct btrfs_fs_info *fs_info = btrfs_sb(sb); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9403 | struct btrfs_inode *ei; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9404 | struct inode *inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9405 | |
David Sterba | 712e36c | 2017-10-31 17:08:27 +0100 | [diff] [blame] | 9406 | ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_KERNEL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9407 | if (!ei) |
| 9408 | return NULL; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9409 | |
| 9410 | ei->root = NULL; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9411 | ei->generation = 0; |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 9412 | ei->last_trans = 0; |
Chris Mason | 257c62e | 2009-10-13 13:21:08 -0400 | [diff] [blame] | 9413 | ei->last_sub_trans = 0; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 9414 | ei->logged_trans = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9415 | ei->delalloc_bytes = 0; |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 9416 | ei->new_delalloc_bytes = 0; |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 9417 | ei->defrag_bytes = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9418 | ei->disk_i_size = 0; |
| 9419 | ei->flags = 0; |
Josef Bacik | 7709cde | 2011-08-04 10:25:02 -0400 | [diff] [blame] | 9420 | ei->csum_bytes = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9421 | ei->index_cnt = (u64)-1; |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 9422 | ei->dir_index = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9423 | ei->last_unlink_trans = 0; |
Liu Bo | 46d8bc3 | 2012-08-29 01:07:55 -0600 | [diff] [blame] | 9424 | ei->last_log_commit = 0; |
David Sterba | 8089fe6 | 2015-11-19 14:15:51 +0100 | [diff] [blame] | 9425 | ei->delayed_iput_count = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9426 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 9427 | spin_lock_init(&ei->lock); |
| 9428 | ei->outstanding_extents = 0; |
Josef Bacik | 69fe2d7 | 2017-10-19 14:15:57 -0400 | [diff] [blame] | 9429 | if (sb->s_magic != BTRFS_TEST_MAGIC) |
| 9430 | btrfs_init_metadata_block_rsv(fs_info, &ei->block_rsv, |
| 9431 | BTRFS_BLOCK_RSV_DELALLOC); |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 9432 | ei->runtime_flags = 0; |
David Sterba | b52aa8c | 2017-07-17 19:17:20 +0200 | [diff] [blame] | 9433 | ei->prop_compress = BTRFS_COMPRESS_NONE; |
David Sterba | eec63c6 | 2017-07-17 19:41:31 +0200 | [diff] [blame] | 9434 | ei->defrag_compress = BTRFS_COMPRESS_NONE; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9435 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 9436 | ei->delayed_node = NULL; |
| 9437 | |
chandan r | 9cc97d6 | 2012-07-04 12:48:07 +0530 | [diff] [blame] | 9438 | ei->i_otime.tv_sec = 0; |
| 9439 | ei->i_otime.tv_nsec = 0; |
| 9440 | |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9441 | inode = &ei->vfs_inode; |
David Sterba | a8067e0 | 2011-04-21 00:34:43 +0200 | [diff] [blame] | 9442 | extent_map_tree_init(&ei->extent_tree); |
Josef Bacik | c6100a4 | 2017-05-05 11:57:13 -0400 | [diff] [blame] | 9443 | extent_io_tree_init(&ei->io_tree, inode); |
| 9444 | extent_io_tree_init(&ei->io_failure_tree, inode); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 9445 | ei->io_tree.track_uptodate = 1; |
| 9446 | ei->io_failure_tree.track_uptodate = 1; |
Josef Bacik | b812ce2 | 2012-11-16 13:56:32 -0500 | [diff] [blame] | 9447 | atomic_set(&ei->sync_writers, 0); |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9448 | mutex_init(&ei->log_mutex); |
Josef Bacik | f248679 | 2012-01-13 12:09:22 -0500 | [diff] [blame] | 9449 | mutex_init(&ei->delalloc_mutex); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 9450 | btrfs_ordered_inode_tree_init(&ei->ordered_tree); |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9451 | INIT_LIST_HEAD(&ei->delalloc_inodes); |
David Sterba | 8089fe6 | 2015-11-19 14:15:51 +0100 | [diff] [blame] | 9452 | INIT_LIST_HEAD(&ei->delayed_iput); |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9453 | RB_CLEAR_NODE(&ei->rb_node); |
Filipe Manana | 5f9a8a5 | 2016-05-12 13:53:36 +0100 | [diff] [blame] | 9454 | init_rwsem(&ei->dio_sem); |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9455 | |
| 9456 | return inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9457 | } |
| 9458 | |
Josef Bacik | aaedb55 | 2013-10-11 14:44:09 -0400 | [diff] [blame] | 9459 | #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS |
| 9460 | void btrfs_test_destroy_inode(struct inode *inode) |
| 9461 | { |
Nikolay Borisov | dcdbc05 | 2017-02-20 13:50:45 +0200 | [diff] [blame] | 9462 | btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0); |
Josef Bacik | aaedb55 | 2013-10-11 14:44:09 -0400 | [diff] [blame] | 9463 | kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode)); |
| 9464 | } |
| 9465 | #endif |
| 9466 | |
Nick Piggin | fa0d7e3d | 2011-01-07 17:49:49 +1100 | [diff] [blame] | 9467 | static void btrfs_i_callback(struct rcu_head *head) |
| 9468 | { |
| 9469 | struct inode *inode = container_of(head, struct inode, i_rcu); |
Nick Piggin | fa0d7e3d | 2011-01-07 17:49:49 +1100 | [diff] [blame] | 9470 | kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode)); |
| 9471 | } |
| 9472 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9473 | void btrfs_destroy_inode(struct inode *inode) |
| 9474 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9475 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 9476 | struct btrfs_ordered_extent *ordered; |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 9477 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 9478 | |
Al Viro | b3d9b7a | 2012-06-09 13:51:19 -0400 | [diff] [blame] | 9479 | WARN_ON(!hlist_empty(&inode->i_dentry)); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9480 | WARN_ON(inode->i_data.nrpages); |
Josef Bacik | 69fe2d7 | 2017-10-19 14:15:57 -0400 | [diff] [blame] | 9481 | WARN_ON(BTRFS_I(inode)->block_rsv.reserved); |
| 9482 | WARN_ON(BTRFS_I(inode)->block_rsv.size); |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 9483 | WARN_ON(BTRFS_I(inode)->outstanding_extents); |
Josef Bacik | 7709cde | 2011-08-04 10:25:02 -0400 | [diff] [blame] | 9484 | WARN_ON(BTRFS_I(inode)->delalloc_bytes); |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 9485 | WARN_ON(BTRFS_I(inode)->new_delalloc_bytes); |
Josef Bacik | 7709cde | 2011-08-04 10:25:02 -0400 | [diff] [blame] | 9486 | WARN_ON(BTRFS_I(inode)->csum_bytes); |
Wang Shilong | 47059d9 | 2014-07-03 18:22:07 +0800 | [diff] [blame] | 9487 | WARN_ON(BTRFS_I(inode)->defrag_bytes); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9488 | |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 9489 | /* |
Josef Bacik | a6dbd42 | 2009-11-11 15:53:34 -0500 | [diff] [blame] | 9490 | * This can happen where we create an inode, but somebody else also |
| 9491 | * created the same inode and we need to destroy the one we already |
| 9492 | * created. |
| 9493 | */ |
| 9494 | if (!root) |
| 9495 | goto free; |
| 9496 | |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 9497 | if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
| 9498 | &BTRFS_I(inode)->runtime_flags)) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9499 | btrfs_info(fs_info, "inode %llu still on the orphan list", |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 9500 | btrfs_ino(BTRFS_I(inode))); |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 9501 | atomic_dec(&root->orphan_inodes); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 9502 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 9503 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 9504 | while (1) { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 9505 | ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1); |
| 9506 | if (!ordered) |
| 9507 | break; |
| 9508 | else { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9509 | btrfs_err(fs_info, |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 9510 | "found ordered extent %llu %llu on inode cleanup", |
| 9511 | ordered->file_offset, ordered->len); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 9512 | btrfs_remove_ordered_extent(inode, ordered); |
| 9513 | btrfs_put_ordered_extent(ordered); |
| 9514 | btrfs_put_ordered_extent(ordered); |
| 9515 | } |
| 9516 | } |
Qu Wenruo | 56fa9d0 | 2015-10-13 09:53:10 +0800 | [diff] [blame] | 9517 | btrfs_qgroup_check_reserved_leak(inode); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 9518 | inode_tree_del(inode); |
Nikolay Borisov | dcdbc05 | 2017-02-20 13:50:45 +0200 | [diff] [blame] | 9519 | btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0); |
Josef Bacik | a6dbd42 | 2009-11-11 15:53:34 -0500 | [diff] [blame] | 9520 | free: |
Nick Piggin | fa0d7e3d | 2011-01-07 17:49:49 +1100 | [diff] [blame] | 9521 | call_rcu(&inode->i_rcu, btrfs_i_callback); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9522 | } |
| 9523 | |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 9524 | int btrfs_drop_inode(struct inode *inode) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 9525 | { |
| 9526 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 9527 | |
Naohiro Aota | 6379ef9 | 2013-06-06 09:56:34 +0000 | [diff] [blame] | 9528 | if (root == NULL) |
| 9529 | return 1; |
| 9530 | |
Liu Bo | fa6ac87 | 2013-02-20 14:10:23 +0000 | [diff] [blame] | 9531 | /* the snap/subvol tree is on deleting */ |
Stefan Behrens | 69e9c6c | 2013-09-05 16:58:43 +0200 | [diff] [blame] | 9532 | if (btrfs_root_refs(&root->root_item) == 0) |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 9533 | return 1; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 9534 | else |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 9535 | return generic_drop_inode(inode); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 9536 | } |
| 9537 | |
Sven Wegener | 0ee0fda | 2008-07-30 16:54:26 -0400 | [diff] [blame] | 9538 | static void init_once(void *foo) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9539 | { |
| 9540 | struct btrfs_inode *ei = (struct btrfs_inode *) foo; |
| 9541 | |
| 9542 | inode_init_once(&ei->vfs_inode); |
| 9543 | } |
| 9544 | |
| 9545 | void btrfs_destroy_cachep(void) |
| 9546 | { |
Kirill A. Shutemov | 8c0a853 | 2012-09-26 11:33:07 +1000 | [diff] [blame] | 9547 | /* |
| 9548 | * Make sure all delayed rcu free inodes are flushed before we |
| 9549 | * destroy cache. |
| 9550 | */ |
| 9551 | rcu_barrier(); |
Kinglong Mee | 5598e90 | 2016-01-29 21:36:35 +0800 | [diff] [blame] | 9552 | kmem_cache_destroy(btrfs_inode_cachep); |
| 9553 | kmem_cache_destroy(btrfs_trans_handle_cachep); |
Kinglong Mee | 5598e90 | 2016-01-29 21:36:35 +0800 | [diff] [blame] | 9554 | kmem_cache_destroy(btrfs_path_cachep); |
| 9555 | kmem_cache_destroy(btrfs_free_space_cachep); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9556 | } |
| 9557 | |
Liu Bo | f5c29bd | 2017-11-02 17:21:50 -0600 | [diff] [blame] | 9558 | int __init btrfs_init_cachep(void) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9559 | { |
David Sterba | 837e197 | 2012-09-07 03:00:48 -0600 | [diff] [blame] | 9560 | btrfs_inode_cachep = kmem_cache_create("btrfs_inode", |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 9561 | sizeof(struct btrfs_inode), 0, |
Vladimir Davydov | 5d09705 | 2016-01-14 15:18:21 -0800 | [diff] [blame] | 9562 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT, |
| 9563 | init_once); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9564 | if (!btrfs_inode_cachep) |
| 9565 | goto fail; |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 9566 | |
David Sterba | 837e197 | 2012-09-07 03:00:48 -0600 | [diff] [blame] | 9567 | btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle", |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 9568 | sizeof(struct btrfs_trans_handle), 0, |
Nikolay Borisov | fba4b69 | 2016-06-23 21:17:08 +0300 | [diff] [blame] | 9569 | SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9570 | if (!btrfs_trans_handle_cachep) |
| 9571 | goto fail; |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 9572 | |
David Sterba | 837e197 | 2012-09-07 03:00:48 -0600 | [diff] [blame] | 9573 | btrfs_path_cachep = kmem_cache_create("btrfs_path", |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 9574 | sizeof(struct btrfs_path), 0, |
Nikolay Borisov | fba4b69 | 2016-06-23 21:17:08 +0300 | [diff] [blame] | 9575 | SLAB_MEM_SPREAD, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9576 | if (!btrfs_path_cachep) |
| 9577 | goto fail; |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 9578 | |
David Sterba | 837e197 | 2012-09-07 03:00:48 -0600 | [diff] [blame] | 9579 | btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space", |
Josef Bacik | dc89e98 | 2011-01-28 17:05:48 -0500 | [diff] [blame] | 9580 | sizeof(struct btrfs_free_space), 0, |
Nikolay Borisov | fba4b69 | 2016-06-23 21:17:08 +0300 | [diff] [blame] | 9581 | SLAB_MEM_SPREAD, NULL); |
Josef Bacik | dc89e98 | 2011-01-28 17:05:48 -0500 | [diff] [blame] | 9582 | if (!btrfs_free_space_cachep) |
| 9583 | goto fail; |
| 9584 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9585 | return 0; |
| 9586 | fail: |
| 9587 | btrfs_destroy_cachep(); |
| 9588 | return -ENOMEM; |
| 9589 | } |
| 9590 | |
David Howells | a528d35 | 2017-01-31 16:46:22 +0000 | [diff] [blame] | 9591 | static int btrfs_getattr(const struct path *path, struct kstat *stat, |
| 9592 | u32 request_mask, unsigned int flags) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9593 | { |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 9594 | u64 delalloc_bytes; |
David Howells | a528d35 | 2017-01-31 16:46:22 +0000 | [diff] [blame] | 9595 | struct inode *inode = d_inode(path->dentry); |
David Sterba | fadc0d8 | 2011-11-20 07:33:38 -0500 | [diff] [blame] | 9596 | u32 blocksize = inode->i_sb->s_blocksize; |
Yonghong Song | 04a87e3 | 2017-05-12 15:07:43 -0700 | [diff] [blame] | 9597 | u32 bi_flags = BTRFS_I(inode)->flags; |
| 9598 | |
| 9599 | stat->result_mask |= STATX_BTIME; |
| 9600 | stat->btime.tv_sec = BTRFS_I(inode)->i_otime.tv_sec; |
| 9601 | stat->btime.tv_nsec = BTRFS_I(inode)->i_otime.tv_nsec; |
| 9602 | if (bi_flags & BTRFS_INODE_APPEND) |
| 9603 | stat->attributes |= STATX_ATTR_APPEND; |
| 9604 | if (bi_flags & BTRFS_INODE_COMPRESS) |
| 9605 | stat->attributes |= STATX_ATTR_COMPRESSED; |
| 9606 | if (bi_flags & BTRFS_INODE_IMMUTABLE) |
| 9607 | stat->attributes |= STATX_ATTR_IMMUTABLE; |
| 9608 | if (bi_flags & BTRFS_INODE_NODUMP) |
| 9609 | stat->attributes |= STATX_ATTR_NODUMP; |
| 9610 | |
| 9611 | stat->attributes_mask |= (STATX_ATTR_APPEND | |
| 9612 | STATX_ATTR_COMPRESSED | |
| 9613 | STATX_ATTR_IMMUTABLE | |
| 9614 | STATX_ATTR_NODUMP); |
David Sterba | fadc0d8 | 2011-11-20 07:33:38 -0500 | [diff] [blame] | 9615 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9616 | generic_fillattr(inode, stat); |
Al Viro | 0ee5dc6 | 2011-07-07 15:44:25 -0400 | [diff] [blame] | 9617 | stat->dev = BTRFS_I(inode)->root->anon_dev; |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 9618 | |
| 9619 | spin_lock(&BTRFS_I(inode)->lock); |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 9620 | delalloc_bytes = BTRFS_I(inode)->new_delalloc_bytes; |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 9621 | spin_unlock(&BTRFS_I(inode)->lock); |
David Sterba | fadc0d8 | 2011-11-20 07:33:38 -0500 | [diff] [blame] | 9622 | stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) + |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 9623 | ALIGN(delalloc_bytes, blocksize)) >> 9; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9624 | return 0; |
| 9625 | } |
| 9626 | |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9627 | static int btrfs_rename_exchange(struct inode *old_dir, |
| 9628 | struct dentry *old_dentry, |
| 9629 | struct inode *new_dir, |
| 9630 | struct dentry *new_dentry) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9631 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9632 | struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9633 | struct btrfs_trans_handle *trans; |
| 9634 | struct btrfs_root *root = BTRFS_I(old_dir)->root; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9635 | struct btrfs_root *dest = BTRFS_I(new_dir)->root; |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9636 | struct inode *new_inode = new_dentry->d_inode; |
| 9637 | struct inode *old_inode = old_dentry->d_inode; |
Deepa Dinamani | c2050a4 | 2016-09-14 07:48:06 -0700 | [diff] [blame] | 9638 | struct timespec ctime = current_time(old_inode); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9639 | struct dentry *parent; |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 9640 | u64 old_ino = btrfs_ino(BTRFS_I(old_inode)); |
| 9641 | u64 new_ino = btrfs_ino(BTRFS_I(new_inode)); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9642 | u64 old_idx = 0; |
| 9643 | u64 new_idx = 0; |
| 9644 | u64 root_objectid; |
| 9645 | int ret; |
Filipe Manana | 86e8aa0 | 2016-05-05 02:02:27 +0100 | [diff] [blame] | 9646 | bool root_log_pinned = false; |
| 9647 | bool dest_log_pinned = false; |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9648 | |
| 9649 | /* we only allow rename subvolume link between subvolumes */ |
| 9650 | if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest) |
| 9651 | return -EXDEV; |
| 9652 | |
| 9653 | /* close the race window with snapshot create/destroy ioctl */ |
| 9654 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9655 | down_read(&fs_info->subvol_sem); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9656 | if (new_ino == BTRFS_FIRST_FREE_OBJECTID) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9657 | down_read(&fs_info->subvol_sem); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9658 | |
| 9659 | /* |
| 9660 | * We want to reserve the absolute worst case amount of items. So if |
| 9661 | * both inodes are subvols and we need to unlink them then that would |
| 9662 | * require 4 item modifications, but if they are both normal inodes it |
| 9663 | * would require 5 item modifications, so we'll assume their normal |
| 9664 | * inodes. So 5 * 2 is 10, plus 2 for the new links, so 12 total items |
| 9665 | * should cover the worst case number of items we'll modify. |
| 9666 | */ |
| 9667 | trans = btrfs_start_transaction(root, 12); |
| 9668 | if (IS_ERR(trans)) { |
| 9669 | ret = PTR_ERR(trans); |
| 9670 | goto out_notrans; |
| 9671 | } |
| 9672 | |
| 9673 | /* |
| 9674 | * We need to find a free sequence number both in the source and |
| 9675 | * in the destination directory for the exchange. |
| 9676 | */ |
Nikolay Borisov | 877574e | 2017-02-20 13:50:33 +0200 | [diff] [blame] | 9677 | ret = btrfs_set_inode_index(BTRFS_I(new_dir), &old_idx); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9678 | if (ret) |
| 9679 | goto out_fail; |
Nikolay Borisov | 877574e | 2017-02-20 13:50:33 +0200 | [diff] [blame] | 9680 | ret = btrfs_set_inode_index(BTRFS_I(old_dir), &new_idx); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9681 | if (ret) |
| 9682 | goto out_fail; |
| 9683 | |
| 9684 | BTRFS_I(old_inode)->dir_index = 0ULL; |
| 9685 | BTRFS_I(new_inode)->dir_index = 0ULL; |
| 9686 | |
| 9687 | /* Reference for the source. */ |
| 9688 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) { |
| 9689 | /* force full log commit if subvolume involved. */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9690 | btrfs_set_log_full_commit(fs_info, trans); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9691 | } else { |
Filipe Manana | 376e5a5 | 2016-05-05 02:08:56 +0100 | [diff] [blame] | 9692 | btrfs_pin_log_trans(root); |
| 9693 | root_log_pinned = true; |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9694 | ret = btrfs_insert_inode_ref(trans, dest, |
| 9695 | new_dentry->d_name.name, |
| 9696 | new_dentry->d_name.len, |
| 9697 | old_ino, |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 9698 | btrfs_ino(BTRFS_I(new_dir)), |
| 9699 | old_idx); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9700 | if (ret) |
| 9701 | goto out_fail; |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9702 | } |
| 9703 | |
| 9704 | /* And now for the dest. */ |
| 9705 | if (new_ino == BTRFS_FIRST_FREE_OBJECTID) { |
| 9706 | /* force full log commit if subvolume involved. */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9707 | btrfs_set_log_full_commit(fs_info, trans); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9708 | } else { |
Filipe Manana | 376e5a5 | 2016-05-05 02:08:56 +0100 | [diff] [blame] | 9709 | btrfs_pin_log_trans(dest); |
| 9710 | dest_log_pinned = true; |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9711 | ret = btrfs_insert_inode_ref(trans, root, |
| 9712 | old_dentry->d_name.name, |
| 9713 | old_dentry->d_name.len, |
| 9714 | new_ino, |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 9715 | btrfs_ino(BTRFS_I(old_dir)), |
| 9716 | new_idx); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9717 | if (ret) |
| 9718 | goto out_fail; |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9719 | } |
| 9720 | |
| 9721 | /* Update inode version and ctime/mtime. */ |
| 9722 | inode_inc_iversion(old_dir); |
| 9723 | inode_inc_iversion(new_dir); |
| 9724 | inode_inc_iversion(old_inode); |
| 9725 | inode_inc_iversion(new_inode); |
| 9726 | old_dir->i_ctime = old_dir->i_mtime = ctime; |
| 9727 | new_dir->i_ctime = new_dir->i_mtime = ctime; |
| 9728 | old_inode->i_ctime = ctime; |
| 9729 | new_inode->i_ctime = ctime; |
| 9730 | |
| 9731 | if (old_dentry->d_parent != new_dentry->d_parent) { |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 9732 | btrfs_record_unlink_dir(trans, BTRFS_I(old_dir), |
| 9733 | BTRFS_I(old_inode), 1); |
| 9734 | btrfs_record_unlink_dir(trans, BTRFS_I(new_dir), |
| 9735 | BTRFS_I(new_inode), 1); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9736 | } |
| 9737 | |
| 9738 | /* src is a subvolume */ |
| 9739 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) { |
| 9740 | root_objectid = BTRFS_I(old_inode)->root->root_key.objectid; |
| 9741 | ret = btrfs_unlink_subvol(trans, root, old_dir, |
| 9742 | root_objectid, |
| 9743 | old_dentry->d_name.name, |
| 9744 | old_dentry->d_name.len); |
| 9745 | } else { /* src is an inode */ |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 9746 | ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir), |
| 9747 | BTRFS_I(old_dentry->d_inode), |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9748 | old_dentry->d_name.name, |
| 9749 | old_dentry->d_name.len); |
| 9750 | if (!ret) |
| 9751 | ret = btrfs_update_inode(trans, root, old_inode); |
| 9752 | } |
| 9753 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 9754 | btrfs_abort_transaction(trans, ret); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9755 | goto out_fail; |
| 9756 | } |
| 9757 | |
| 9758 | /* dest is a subvolume */ |
| 9759 | if (new_ino == BTRFS_FIRST_FREE_OBJECTID) { |
| 9760 | root_objectid = BTRFS_I(new_inode)->root->root_key.objectid; |
| 9761 | ret = btrfs_unlink_subvol(trans, dest, new_dir, |
| 9762 | root_objectid, |
| 9763 | new_dentry->d_name.name, |
| 9764 | new_dentry->d_name.len); |
| 9765 | } else { /* dest is an inode */ |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 9766 | ret = __btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir), |
| 9767 | BTRFS_I(new_dentry->d_inode), |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9768 | new_dentry->d_name.name, |
| 9769 | new_dentry->d_name.len); |
| 9770 | if (!ret) |
| 9771 | ret = btrfs_update_inode(trans, dest, new_inode); |
| 9772 | } |
| 9773 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 9774 | btrfs_abort_transaction(trans, ret); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9775 | goto out_fail; |
| 9776 | } |
| 9777 | |
Nikolay Borisov | db0a669 | 2017-02-20 13:51:08 +0200 | [diff] [blame] | 9778 | 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] | 9779 | new_dentry->d_name.name, |
| 9780 | new_dentry->d_name.len, 0, old_idx); |
| 9781 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 9782 | btrfs_abort_transaction(trans, ret); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9783 | goto out_fail; |
| 9784 | } |
| 9785 | |
Nikolay Borisov | db0a669 | 2017-02-20 13:51:08 +0200 | [diff] [blame] | 9786 | 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] | 9787 | old_dentry->d_name.name, |
| 9788 | old_dentry->d_name.len, 0, new_idx); |
| 9789 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 9790 | btrfs_abort_transaction(trans, ret); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9791 | goto out_fail; |
| 9792 | } |
| 9793 | |
| 9794 | if (old_inode->i_nlink == 1) |
| 9795 | BTRFS_I(old_inode)->dir_index = old_idx; |
| 9796 | if (new_inode->i_nlink == 1) |
| 9797 | BTRFS_I(new_inode)->dir_index = new_idx; |
| 9798 | |
Filipe Manana | 86e8aa0 | 2016-05-05 02:02:27 +0100 | [diff] [blame] | 9799 | if (root_log_pinned) { |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9800 | parent = new_dentry->d_parent; |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 9801 | btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir), |
| 9802 | parent); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9803 | btrfs_end_log_trans(root); |
Filipe Manana | 86e8aa0 | 2016-05-05 02:02:27 +0100 | [diff] [blame] | 9804 | root_log_pinned = false; |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9805 | } |
Filipe Manana | 86e8aa0 | 2016-05-05 02:02:27 +0100 | [diff] [blame] | 9806 | if (dest_log_pinned) { |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9807 | parent = old_dentry->d_parent; |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 9808 | btrfs_log_new_name(trans, BTRFS_I(new_inode), BTRFS_I(new_dir), |
| 9809 | parent); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9810 | btrfs_end_log_trans(dest); |
Filipe Manana | 86e8aa0 | 2016-05-05 02:02:27 +0100 | [diff] [blame] | 9811 | dest_log_pinned = false; |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9812 | } |
| 9813 | out_fail: |
Filipe Manana | 86e8aa0 | 2016-05-05 02:02:27 +0100 | [diff] [blame] | 9814 | /* |
| 9815 | * If we have pinned a log and an error happened, we unpin tasks |
| 9816 | * trying to sync the log and force them to fallback to a transaction |
| 9817 | * commit if the log currently contains any of the inodes involved in |
| 9818 | * this rename operation (to ensure we do not persist a log with an |
| 9819 | * inconsistent state for any of these inodes or leading to any |
| 9820 | * inconsistencies when replayed). If the transaction was aborted, the |
| 9821 | * abortion reason is propagated to userspace when attempting to commit |
| 9822 | * the transaction. If the log does not contain any of these inodes, we |
| 9823 | * allow the tasks to sync it. |
| 9824 | */ |
| 9825 | if (ret && (root_log_pinned || dest_log_pinned)) { |
Nikolay Borisov | 0f8939b | 2017-01-18 00:31:30 +0200 | [diff] [blame] | 9826 | if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) || |
| 9827 | btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) || |
| 9828 | btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) || |
Filipe Manana | 86e8aa0 | 2016-05-05 02:02:27 +0100 | [diff] [blame] | 9829 | (new_inode && |
Nikolay Borisov | 0f8939b | 2017-01-18 00:31:30 +0200 | [diff] [blame] | 9830 | btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation))) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9831 | btrfs_set_log_full_commit(fs_info, trans); |
Filipe Manana | 86e8aa0 | 2016-05-05 02:02:27 +0100 | [diff] [blame] | 9832 | |
| 9833 | if (root_log_pinned) { |
| 9834 | btrfs_end_log_trans(root); |
| 9835 | root_log_pinned = false; |
| 9836 | } |
| 9837 | if (dest_log_pinned) { |
| 9838 | btrfs_end_log_trans(dest); |
| 9839 | dest_log_pinned = false; |
| 9840 | } |
| 9841 | } |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 9842 | ret = btrfs_end_transaction(trans); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9843 | out_notrans: |
| 9844 | if (new_ino == BTRFS_FIRST_FREE_OBJECTID) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9845 | up_read(&fs_info->subvol_sem); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9846 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9847 | up_read(&fs_info->subvol_sem); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9848 | |
| 9849 | return ret; |
| 9850 | } |
| 9851 | |
| 9852 | static int btrfs_whiteout_for_rename(struct btrfs_trans_handle *trans, |
| 9853 | struct btrfs_root *root, |
| 9854 | struct inode *dir, |
| 9855 | struct dentry *dentry) |
| 9856 | { |
| 9857 | int ret; |
| 9858 | struct inode *inode; |
| 9859 | u64 objectid; |
| 9860 | u64 index; |
| 9861 | |
| 9862 | ret = btrfs_find_free_ino(root, &objectid); |
| 9863 | if (ret) |
| 9864 | return ret; |
| 9865 | |
| 9866 | inode = btrfs_new_inode(trans, root, dir, |
| 9867 | dentry->d_name.name, |
| 9868 | dentry->d_name.len, |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 9869 | btrfs_ino(BTRFS_I(dir)), |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9870 | objectid, |
| 9871 | S_IFCHR | WHITEOUT_MODE, |
| 9872 | &index); |
| 9873 | |
| 9874 | if (IS_ERR(inode)) { |
| 9875 | ret = PTR_ERR(inode); |
| 9876 | return ret; |
| 9877 | } |
| 9878 | |
| 9879 | inode->i_op = &btrfs_special_inode_operations; |
| 9880 | init_special_inode(inode, inode->i_mode, |
| 9881 | WHITEOUT_DEV); |
| 9882 | |
| 9883 | ret = btrfs_init_inode_security(trans, inode, dir, |
| 9884 | &dentry->d_name); |
| 9885 | if (ret) |
Filipe Manana | c990161 | 2016-05-05 01:41:57 +0100 | [diff] [blame] | 9886 | goto out; |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9887 | |
Nikolay Borisov | cef415a | 2017-02-20 13:51:09 +0200 | [diff] [blame] | 9888 | ret = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, |
| 9889 | BTRFS_I(inode), 0, index); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9890 | if (ret) |
Filipe Manana | c990161 | 2016-05-05 01:41:57 +0100 | [diff] [blame] | 9891 | goto out; |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9892 | |
| 9893 | ret = btrfs_update_inode(trans, root, inode); |
Filipe Manana | c990161 | 2016-05-05 01:41:57 +0100 | [diff] [blame] | 9894 | out: |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9895 | unlock_new_inode(inode); |
Filipe Manana | c990161 | 2016-05-05 01:41:57 +0100 | [diff] [blame] | 9896 | if (ret) |
| 9897 | inode_dec_link_count(inode); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9898 | iput(inode); |
| 9899 | |
Filipe Manana | c990161 | 2016-05-05 01:41:57 +0100 | [diff] [blame] | 9900 | return ret; |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9901 | } |
| 9902 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9903 | static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry, |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9904 | struct inode *new_dir, struct dentry *new_dentry, |
| 9905 | unsigned int flags) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9906 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9907 | struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9908 | struct btrfs_trans_handle *trans; |
Filipe Manana | 5062af3 | 2016-05-05 10:26:26 +0100 | [diff] [blame] | 9909 | unsigned int trans_num_items; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9910 | struct btrfs_root *root = BTRFS_I(old_dir)->root; |
| 9911 | struct btrfs_root *dest = BTRFS_I(new_dir)->root; |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 9912 | struct inode *new_inode = d_inode(new_dentry); |
| 9913 | struct inode *old_inode = d_inode(old_dentry); |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 9914 | u64 index = 0; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9915 | u64 root_objectid; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9916 | int ret; |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 9917 | u64 old_ino = btrfs_ino(BTRFS_I(old_inode)); |
Filipe Manana | 3dc9e8f7 | 2016-04-29 11:34:22 +0100 | [diff] [blame] | 9918 | bool log_pinned = false; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9919 | |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 9920 | if (btrfs_ino(BTRFS_I(new_dir)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) |
Yan, Zheng | f679a84 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 9921 | return -EPERM; |
| 9922 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9923 | /* we only allow rename subvolume link between subvolumes */ |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 9924 | if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest) |
Chris Mason | 3394e16 | 2008-11-17 20:42:26 -0500 | [diff] [blame] | 9925 | return -EXDEV; |
| 9926 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 9927 | if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID || |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 9928 | (new_inode && btrfs_ino(BTRFS_I(new_inode)) == BTRFS_FIRST_FREE_OBJECTID)) |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9929 | return -ENOTEMPTY; |
| 9930 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9931 | if (S_ISDIR(old_inode->i_mode) && new_inode && |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9932 | new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9933 | return -ENOTEMPTY; |
Chris Mason | 9c52057 | 2012-12-17 14:26:57 -0500 | [diff] [blame] | 9934 | |
| 9935 | |
| 9936 | /* check for collisions, even if the name isn't there */ |
Josef Bacik | 4871c15 | 2013-10-09 12:24:04 -0400 | [diff] [blame] | 9937 | ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino, |
Chris Mason | 9c52057 | 2012-12-17 14:26:57 -0500 | [diff] [blame] | 9938 | new_dentry->d_name.name, |
| 9939 | new_dentry->d_name.len); |
| 9940 | |
| 9941 | if (ret) { |
| 9942 | if (ret == -EEXIST) { |
| 9943 | /* we shouldn't get |
| 9944 | * eexist without a new_inode */ |
Dulshani Gunawardhana | fae7f21 | 2013-10-31 10:30:08 +0530 | [diff] [blame] | 9945 | if (WARN_ON(!new_inode)) { |
Chris Mason | 9c52057 | 2012-12-17 14:26:57 -0500 | [diff] [blame] | 9946 | return ret; |
| 9947 | } |
| 9948 | } else { |
| 9949 | /* maybe -EOVERFLOW */ |
| 9950 | return ret; |
| 9951 | } |
| 9952 | } |
| 9953 | ret = 0; |
| 9954 | |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 9955 | /* |
Chris Mason | 8d875f9 | 2014-08-12 10:47:42 -0700 | [diff] [blame] | 9956 | * we're using rename to replace one file with another. Start IO on it |
| 9957 | * 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] | 9958 | */ |
Chris Mason | 8d875f9 | 2014-08-12 10:47:42 -0700 | [diff] [blame] | 9959 | 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] | 9960 | filemap_flush(old_inode->i_mapping); |
| 9961 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 9962 | /* close the racy window with snapshot create/destroy ioctl */ |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 9963 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9964 | down_read(&fs_info->subvol_sem); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 9965 | /* |
| 9966 | * We want to reserve the absolute worst case amount of items. So if |
| 9967 | * both inodes are subvols and we need to unlink them then that would |
| 9968 | * require 4 item modifications, but if they are both normal inodes it |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9969 | * would require 5 item modifications, so we'll assume they are normal |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 9970 | * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items |
| 9971 | * should cover the worst case number of items we'll modify. |
Filipe Manana | 5062af3 | 2016-05-05 10:26:26 +0100 | [diff] [blame] | 9972 | * If our rename has the whiteout flag, we need more 5 units for the |
| 9973 | * new inode (1 inode item, 1 inode ref, 2 dir items and 1 xattr item |
| 9974 | * when selinux is enabled). |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 9975 | */ |
Filipe Manana | 5062af3 | 2016-05-05 10:26:26 +0100 | [diff] [blame] | 9976 | trans_num_items = 11; |
| 9977 | if (flags & RENAME_WHITEOUT) |
| 9978 | trans_num_items += 5; |
| 9979 | trans = btrfs_start_transaction(root, trans_num_items); |
Johann Lombardi | b44c59a | 2011-03-31 13:23:47 +0000 | [diff] [blame] | 9980 | if (IS_ERR(trans)) { |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 9981 | ret = PTR_ERR(trans); |
| 9982 | goto out_notrans; |
| 9983 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 9984 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9985 | if (dest != root) |
| 9986 | btrfs_record_root_in_trans(trans, dest); |
| 9987 | |
Nikolay Borisov | 877574e | 2017-02-20 13:50:33 +0200 | [diff] [blame] | 9988 | ret = btrfs_set_inode_index(BTRFS_I(new_dir), &index); |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 9989 | if (ret) |
| 9990 | goto out_fail; |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 9991 | |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 9992 | BTRFS_I(old_inode)->dir_index = 0ULL; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 9993 | if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9994 | /* force full log commit if subvolume involved. */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9995 | btrfs_set_log_full_commit(fs_info, trans); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 9996 | } else { |
Filipe Manana | c4aba95 | 2016-04-29 13:14:42 +0100 | [diff] [blame] | 9997 | btrfs_pin_log_trans(root); |
| 9998 | log_pinned = true; |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 9999 | ret = btrfs_insert_inode_ref(trans, dest, |
| 10000 | new_dentry->d_name.name, |
| 10001 | new_dentry->d_name.len, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 10002 | old_ino, |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 10003 | btrfs_ino(BTRFS_I(new_dir)), index); |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 10004 | if (ret) |
| 10005 | goto out_fail; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 10006 | } |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 10007 | |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 10008 | inode_inc_iversion(old_dir); |
| 10009 | inode_inc_iversion(new_dir); |
| 10010 | inode_inc_iversion(old_inode); |
Deepa Dinamani | 04b285f | 2016-02-06 23:57:21 -0800 | [diff] [blame] | 10011 | old_dir->i_ctime = old_dir->i_mtime = |
| 10012 | new_dir->i_ctime = new_dir->i_mtime = |
Deepa Dinamani | c2050a4 | 2016-09-14 07:48:06 -0700 | [diff] [blame] | 10013 | old_inode->i_ctime = current_time(old_dir); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 10014 | |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 10015 | if (old_dentry->d_parent != new_dentry->d_parent) |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 10016 | btrfs_record_unlink_dir(trans, BTRFS_I(old_dir), |
| 10017 | BTRFS_I(old_inode), 1); |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 10018 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 10019 | if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 10020 | root_objectid = BTRFS_I(old_inode)->root->root_key.objectid; |
| 10021 | ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid, |
| 10022 | old_dentry->d_name.name, |
| 10023 | old_dentry->d_name.len); |
| 10024 | } else { |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 10025 | ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir), |
| 10026 | BTRFS_I(d_inode(old_dentry)), |
Al Viro | 9298679 | 2011-03-04 17:14:37 +0000 | [diff] [blame] | 10027 | old_dentry->d_name.name, |
| 10028 | old_dentry->d_name.len); |
| 10029 | if (!ret) |
| 10030 | ret = btrfs_update_inode(trans, root, old_inode); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 10031 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 10032 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 10033 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 10034 | goto out_fail; |
| 10035 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10036 | |
| 10037 | if (new_inode) { |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 10038 | inode_inc_iversion(new_inode); |
Deepa Dinamani | c2050a4 | 2016-09-14 07:48:06 -0700 | [diff] [blame] | 10039 | new_inode->i_ctime = current_time(new_inode); |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 10040 | if (unlikely(btrfs_ino(BTRFS_I(new_inode)) == |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 10041 | BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) { |
| 10042 | root_objectid = BTRFS_I(new_inode)->location.objectid; |
| 10043 | ret = btrfs_unlink_subvol(trans, dest, new_dir, |
| 10044 | root_objectid, |
| 10045 | new_dentry->d_name.name, |
| 10046 | new_dentry->d_name.len); |
| 10047 | BUG_ON(new_inode->i_nlink == 0); |
| 10048 | } else { |
Nikolay Borisov | 4ec5934 | 2017-01-18 00:31:44 +0200 | [diff] [blame] | 10049 | ret = btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir), |
| 10050 | BTRFS_I(d_inode(new_dentry)), |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 10051 | new_dentry->d_name.name, |
| 10052 | new_dentry->d_name.len); |
| 10053 | } |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 10054 | if (!ret && new_inode->i_nlink == 0) |
Nikolay Borisov | 73f2e54 | 2017-02-20 13:50:59 +0200 | [diff] [blame] | 10055 | ret = btrfs_orphan_add(trans, |
| 10056 | BTRFS_I(d_inode(new_dentry))); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 10057 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 10058 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 10059 | goto out_fail; |
| 10060 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10061 | } |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 10062 | |
Nikolay Borisov | db0a669 | 2017-02-20 13:51:08 +0200 | [diff] [blame] | 10063 | 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] | 10064 | new_dentry->d_name.name, |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 10065 | new_dentry->d_name.len, 0, index); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 10066 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 10067 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 10068 | goto out_fail; |
| 10069 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10070 | |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 10071 | if (old_inode->i_nlink == 1) |
| 10072 | BTRFS_I(old_inode)->dir_index = index; |
| 10073 | |
Filipe Manana | 3dc9e8f7 | 2016-04-29 11:34:22 +0100 | [diff] [blame] | 10074 | if (log_pinned) { |
Al Viro | 10d9f30 | 2011-07-16 23:09:10 -0400 | [diff] [blame] | 10075 | struct dentry *parent = new_dentry->d_parent; |
Filipe Manana | 3dc9e8f7 | 2016-04-29 11:34:22 +0100 | [diff] [blame] | 10076 | |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 10077 | btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir), |
| 10078 | parent); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 10079 | btrfs_end_log_trans(root); |
Filipe Manana | 3dc9e8f7 | 2016-04-29 11:34:22 +0100 | [diff] [blame] | 10080 | log_pinned = false; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 10081 | } |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 10082 | |
| 10083 | if (flags & RENAME_WHITEOUT) { |
| 10084 | ret = btrfs_whiteout_for_rename(trans, root, old_dir, |
| 10085 | old_dentry); |
| 10086 | |
| 10087 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 10088 | btrfs_abort_transaction(trans, ret); |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 10089 | goto out_fail; |
| 10090 | } |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 10091 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10092 | out_fail: |
Filipe Manana | 3dc9e8f7 | 2016-04-29 11:34:22 +0100 | [diff] [blame] | 10093 | /* |
| 10094 | * If we have pinned the log and an error happened, we unpin tasks |
| 10095 | * trying to sync the log and force them to fallback to a transaction |
| 10096 | * commit if the log currently contains any of the inodes involved in |
| 10097 | * this rename operation (to ensure we do not persist a log with an |
| 10098 | * inconsistent state for any of these inodes or leading to any |
| 10099 | * inconsistencies when replayed). If the transaction was aborted, the |
| 10100 | * abortion reason is propagated to userspace when attempting to commit |
| 10101 | * the transaction. If the log does not contain any of these inodes, we |
| 10102 | * allow the tasks to sync it. |
| 10103 | */ |
| 10104 | if (ret && log_pinned) { |
Nikolay Borisov | 0f8939b | 2017-01-18 00:31:30 +0200 | [diff] [blame] | 10105 | if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) || |
| 10106 | btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) || |
| 10107 | btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) || |
Filipe Manana | 3dc9e8f7 | 2016-04-29 11:34:22 +0100 | [diff] [blame] | 10108 | (new_inode && |
Nikolay Borisov | 0f8939b | 2017-01-18 00:31:30 +0200 | [diff] [blame] | 10109 | btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation))) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 10110 | btrfs_set_log_full_commit(fs_info, trans); |
Filipe Manana | 3dc9e8f7 | 2016-04-29 11:34:22 +0100 | [diff] [blame] | 10111 | |
| 10112 | btrfs_end_log_trans(root); |
| 10113 | log_pinned = false; |
| 10114 | } |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 10115 | btrfs_end_transaction(trans); |
Johann Lombardi | b44c59a | 2011-03-31 13:23:47 +0000 | [diff] [blame] | 10116 | out_notrans: |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 10117 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 10118 | up_read(&fs_info->subvol_sem); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 10119 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10120 | return ret; |
| 10121 | } |
| 10122 | |
Miklos Szeredi | 80ace85 | 2014-07-23 15:15:32 +0200 | [diff] [blame] | 10123 | static int btrfs_rename2(struct inode *old_dir, struct dentry *old_dentry, |
| 10124 | struct inode *new_dir, struct dentry *new_dentry, |
| 10125 | unsigned int flags) |
| 10126 | { |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 10127 | if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT)) |
Miklos Szeredi | 80ace85 | 2014-07-23 15:15:32 +0200 | [diff] [blame] | 10128 | return -EINVAL; |
| 10129 | |
Dan Fuhry | cdd1fed | 2016-03-17 15:23:38 +0100 | [diff] [blame] | 10130 | if (flags & RENAME_EXCHANGE) |
| 10131 | return btrfs_rename_exchange(old_dir, old_dentry, new_dir, |
| 10132 | new_dentry); |
| 10133 | |
| 10134 | return btrfs_rename(old_dir, old_dentry, new_dir, new_dentry, flags); |
Miklos Szeredi | 80ace85 | 2014-07-23 15:15:32 +0200 | [diff] [blame] | 10135 | } |
| 10136 | |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 10137 | static void btrfs_run_delalloc_work(struct btrfs_work *work) |
| 10138 | { |
| 10139 | struct btrfs_delalloc_work *delalloc_work; |
Josef Bacik | 9f23e28 | 2013-10-28 15:03:41 -0400 | [diff] [blame] | 10140 | struct inode *inode; |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 10141 | |
| 10142 | delalloc_work = container_of(work, struct btrfs_delalloc_work, |
| 10143 | work); |
Josef Bacik | 9f23e28 | 2013-10-28 15:03:41 -0400 | [diff] [blame] | 10144 | inode = delalloc_work->inode; |
David Sterba | 3042460 | 2015-11-27 19:27:11 +0100 | [diff] [blame] | 10145 | filemap_flush(inode->i_mapping); |
| 10146 | if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, |
| 10147 | &BTRFS_I(inode)->runtime_flags)) |
Josef Bacik | 9f23e28 | 2013-10-28 15:03:41 -0400 | [diff] [blame] | 10148 | filemap_flush(inode->i_mapping); |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 10149 | |
| 10150 | if (delalloc_work->delay_iput) |
Josef Bacik | 9f23e28 | 2013-10-28 15:03:41 -0400 | [diff] [blame] | 10151 | btrfs_add_delayed_iput(inode); |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 10152 | else |
Josef Bacik | 9f23e28 | 2013-10-28 15:03:41 -0400 | [diff] [blame] | 10153 | iput(inode); |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 10154 | complete(&delalloc_work->completion); |
| 10155 | } |
| 10156 | |
| 10157 | struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode, |
David Sterba | 651d494 | 2015-11-27 19:24:16 +0100 | [diff] [blame] | 10158 | int delay_iput) |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 10159 | { |
| 10160 | struct btrfs_delalloc_work *work; |
| 10161 | |
David Sterba | 100d570 | 2015-12-08 14:39:32 +0100 | [diff] [blame] | 10162 | work = kmalloc(sizeof(*work), GFP_NOFS); |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 10163 | if (!work) |
| 10164 | return NULL; |
| 10165 | |
| 10166 | init_completion(&work->completion); |
| 10167 | INIT_LIST_HEAD(&work->list); |
| 10168 | work->inode = inode; |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 10169 | work->delay_iput = delay_iput; |
Liu Bo | 9e0af23 | 2014-08-15 23:36:53 +0800 | [diff] [blame] | 10170 | WARN_ON_ONCE(!inode); |
| 10171 | btrfs_init_work(&work->work, btrfs_flush_delalloc_helper, |
| 10172 | btrfs_run_delalloc_work, NULL, NULL); |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 10173 | |
| 10174 | return work; |
| 10175 | } |
| 10176 | |
| 10177 | void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work) |
| 10178 | { |
| 10179 | wait_for_completion(&work->completion); |
David Sterba | 100d570 | 2015-12-08 14:39:32 +0100 | [diff] [blame] | 10180 | kfree(work); |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 10181 | } |
| 10182 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 10183 | /* |
| 10184 | * some fairly slow code that needs optimization. This walks the list |
| 10185 | * of all the inodes with pending delalloc and forces them to disk. |
| 10186 | */ |
Miao Xie | 6c255e6 | 2014-03-06 13:55:01 +0800 | [diff] [blame] | 10187 | static int __start_delalloc_inodes(struct btrfs_root *root, int delay_iput, |
| 10188 | int nr) |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 10189 | { |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 10190 | struct btrfs_inode *binode; |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 10191 | struct inode *inode; |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 10192 | struct btrfs_delalloc_work *work, *next; |
| 10193 | struct list_head works; |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 10194 | struct list_head splice; |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 10195 | int ret = 0; |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 10196 | |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 10197 | INIT_LIST_HEAD(&works); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 10198 | INIT_LIST_HEAD(&splice); |
Miao Xie | 63607cc | 2013-01-22 10:50:35 +0000 | [diff] [blame] | 10199 | |
Miao Xie | 573bfb7 | 2014-03-06 13:55:03 +0800 | [diff] [blame] | 10200 | mutex_lock(&root->delalloc_mutex); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 10201 | spin_lock(&root->delalloc_lock); |
| 10202 | list_splice_init(&root->delalloc_inodes, &splice); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 10203 | while (!list_empty(&splice)) { |
| 10204 | binode = list_entry(splice.next, struct btrfs_inode, |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 10205 | delalloc_inodes); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 10206 | |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 10207 | list_move_tail(&binode->delalloc_inodes, |
| 10208 | &root->delalloc_inodes); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 10209 | inode = igrab(&binode->vfs_inode); |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 10210 | if (!inode) { |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 10211 | cond_resched_lock(&root->delalloc_lock); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 10212 | continue; |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 10213 | } |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 10214 | spin_unlock(&root->delalloc_lock); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 10215 | |
David Sterba | 651d494 | 2015-11-27 19:24:16 +0100 | [diff] [blame] | 10216 | work = btrfs_alloc_delalloc_work(inode, delay_iput); |
David Sterba | 5d99a998 | 2014-09-29 19:20:37 +0200 | [diff] [blame] | 10217 | if (!work) { |
Josef Bacik | f4ab9ea | 2013-09-17 11:25:44 -0400 | [diff] [blame] | 10218 | if (delay_iput) |
| 10219 | btrfs_add_delayed_iput(inode); |
| 10220 | else |
| 10221 | iput(inode); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 10222 | ret = -ENOMEM; |
Wang Shilong | a1ecaab | 2014-04-02 19:53:32 +0800 | [diff] [blame] | 10223 | goto out; |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 10224 | } |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 10225 | list_add_tail(&work->list, &works); |
Qu Wenruo | a44903a | 2014-02-28 10:46:09 +0800 | [diff] [blame] | 10226 | btrfs_queue_work(root->fs_info->flush_workers, |
| 10227 | &work->work); |
Miao Xie | 6c255e6 | 2014-03-06 13:55:01 +0800 | [diff] [blame] | 10228 | ret++; |
| 10229 | if (nr != -1 && ret >= nr) |
Wang Shilong | a1ecaab | 2014-04-02 19:53:32 +0800 | [diff] [blame] | 10230 | goto out; |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 10231 | cond_resched(); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 10232 | spin_lock(&root->delalloc_lock); |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 10233 | } |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 10234 | spin_unlock(&root->delalloc_lock); |
Chris Mason | 8c8bee1 | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 10235 | |
Wang Shilong | a1ecaab | 2014-04-02 19:53:32 +0800 | [diff] [blame] | 10236 | out: |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 10237 | list_for_each_entry_safe(work, next, &works, list) { |
| 10238 | list_del_init(&work->list); |
| 10239 | btrfs_wait_and_free_delalloc_work(work); |
| 10240 | } |
| 10241 | |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 10242 | if (!list_empty_careful(&splice)) { |
| 10243 | spin_lock(&root->delalloc_lock); |
| 10244 | list_splice_tail(&splice, &root->delalloc_inodes); |
| 10245 | spin_unlock(&root->delalloc_lock); |
| 10246 | } |
Miao Xie | 573bfb7 | 2014-03-06 13:55:03 +0800 | [diff] [blame] | 10247 | mutex_unlock(&root->delalloc_mutex); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 10248 | return ret; |
| 10249 | } |
| 10250 | |
| 10251 | int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput) |
| 10252 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 10253 | struct btrfs_fs_info *fs_info = root->fs_info; |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 10254 | int ret; |
| 10255 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 10256 | if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 10257 | return -EROFS; |
| 10258 | |
Miao Xie | 6c255e6 | 2014-03-06 13:55:01 +0800 | [diff] [blame] | 10259 | ret = __start_delalloc_inodes(root, delay_iput, -1); |
| 10260 | if (ret > 0) |
| 10261 | ret = 0; |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 10262 | return ret; |
| 10263 | } |
| 10264 | |
Miao Xie | 6c255e6 | 2014-03-06 13:55:01 +0800 | [diff] [blame] | 10265 | int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, int delay_iput, |
| 10266 | int nr) |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 10267 | { |
| 10268 | struct btrfs_root *root; |
| 10269 | struct list_head splice; |
| 10270 | int ret; |
| 10271 | |
Wang Shilong | 2c21b4d | 2014-01-14 19:42:20 +0800 | [diff] [blame] | 10272 | if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 10273 | return -EROFS; |
| 10274 | |
| 10275 | INIT_LIST_HEAD(&splice); |
| 10276 | |
Miao Xie | 573bfb7 | 2014-03-06 13:55:03 +0800 | [diff] [blame] | 10277 | mutex_lock(&fs_info->delalloc_root_mutex); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 10278 | spin_lock(&fs_info->delalloc_root_lock); |
| 10279 | list_splice_init(&fs_info->delalloc_roots, &splice); |
Miao Xie | 6c255e6 | 2014-03-06 13:55:01 +0800 | [diff] [blame] | 10280 | while (!list_empty(&splice) && nr) { |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 10281 | root = list_first_entry(&splice, struct btrfs_root, |
| 10282 | delalloc_root); |
| 10283 | root = btrfs_grab_fs_root(root); |
| 10284 | BUG_ON(!root); |
| 10285 | list_move_tail(&root->delalloc_root, |
| 10286 | &fs_info->delalloc_roots); |
| 10287 | spin_unlock(&fs_info->delalloc_root_lock); |
| 10288 | |
Miao Xie | 6c255e6 | 2014-03-06 13:55:01 +0800 | [diff] [blame] | 10289 | ret = __start_delalloc_inodes(root, delay_iput, nr); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 10290 | btrfs_put_fs_root(root); |
Miao Xie | 6c255e6 | 2014-03-06 13:55:01 +0800 | [diff] [blame] | 10291 | if (ret < 0) |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 10292 | goto out; |
| 10293 | |
Miao Xie | 6c255e6 | 2014-03-06 13:55:01 +0800 | [diff] [blame] | 10294 | if (nr != -1) { |
| 10295 | nr -= ret; |
| 10296 | WARN_ON(nr < 0); |
| 10297 | } |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 10298 | spin_lock(&fs_info->delalloc_root_lock); |
| 10299 | } |
| 10300 | spin_unlock(&fs_info->delalloc_root_lock); |
| 10301 | |
Miao Xie | 6c255e6 | 2014-03-06 13:55:01 +0800 | [diff] [blame] | 10302 | ret = 0; |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 10303 | out: |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 10304 | if (!list_empty_careful(&splice)) { |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 10305 | spin_lock(&fs_info->delalloc_root_lock); |
| 10306 | list_splice_tail(&splice, &fs_info->delalloc_roots); |
| 10307 | spin_unlock(&fs_info->delalloc_root_lock); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 10308 | } |
Miao Xie | 573bfb7 | 2014-03-06 13:55:03 +0800 | [diff] [blame] | 10309 | mutex_unlock(&fs_info->delalloc_root_mutex); |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 10310 | return ret; |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 10311 | } |
| 10312 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10313 | static int btrfs_symlink(struct inode *dir, struct dentry *dentry, |
| 10314 | const char *symname) |
| 10315 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 10316 | struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10317 | struct btrfs_trans_handle *trans; |
| 10318 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 10319 | struct btrfs_path *path; |
| 10320 | struct btrfs_key key; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 10321 | struct inode *inode = NULL; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10322 | int err; |
| 10323 | int drop_inode = 0; |
| 10324 | u64 objectid; |
Dulshani Gunawardhana | 6787125 | 2013-10-31 10:33:04 +0530 | [diff] [blame] | 10325 | u64 index = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10326 | int name_len; |
| 10327 | int datasize; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 10328 | unsigned long ptr; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10329 | struct btrfs_file_extent_item *ei; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 10330 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10331 | |
Filipe David Borba Manana | f06becc | 2013-09-16 09:53:28 +0100 | [diff] [blame] | 10332 | name_len = strlen(symname); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 10333 | if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info)) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10334 | return -ENAMETOOLONG; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 10335 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 10336 | /* |
| 10337 | * 2 items for inode item and ref |
| 10338 | * 2 items for dir items |
Filipe Manana | 9269d12 | 2015-12-31 18:16:29 +0000 | [diff] [blame] | 10339 | * 1 item for updating parent inode item |
| 10340 | * 1 item for the inline extent item |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 10341 | * 1 item for xattr if selinux is on |
| 10342 | */ |
Filipe Manana | 9269d12 | 2015-12-31 18:16:29 +0000 | [diff] [blame] | 10343 | trans = btrfs_start_transaction(root, 7); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 10344 | if (IS_ERR(trans)) |
| 10345 | return PTR_ERR(trans); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 10346 | |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 10347 | err = btrfs_find_free_ino(root, &objectid); |
| 10348 | if (err) |
| 10349 | goto out_unlock; |
| 10350 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 10351 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 10352 | dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), |
| 10353 | objectid, S_IFLNK|S_IRWXUGO, &index); |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 10354 | if (IS_ERR(inode)) { |
| 10355 | err = PTR_ERR(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10356 | goto out_unlock; |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 10357 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10358 | |
Casey Schaufler | ad19db7 | 2011-12-15 10:09:07 -0500 | [diff] [blame] | 10359 | /* |
| 10360 | * If the active LSM wants to access the inode during |
| 10361 | * d_instantiate it needs these. Smack checks to see |
| 10362 | * if the filesystem supports xattrs by looking at the |
| 10363 | * ops vector. |
| 10364 | */ |
| 10365 | inode->i_fop = &btrfs_file_operations; |
| 10366 | inode->i_op = &btrfs_file_inode_operations; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 10367 | inode->i_mapping->a_ops = &btrfs_aops; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 10368 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
| 10369 | |
| 10370 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); |
| 10371 | if (err) |
| 10372 | goto out_unlock_inode; |
Casey Schaufler | ad19db7 | 2011-12-15 10:09:07 -0500 | [diff] [blame] | 10373 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10374 | path = btrfs_alloc_path(); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 10375 | if (!path) { |
| 10376 | err = -ENOMEM; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 10377 | goto out_unlock_inode; |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 10378 | } |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 10379 | key.objectid = btrfs_ino(BTRFS_I(inode)); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10380 | key.offset = 0; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 10381 | key.type = BTRFS_EXTENT_DATA_KEY; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10382 | datasize = btrfs_file_extent_calc_inline_size(name_len); |
| 10383 | err = btrfs_insert_empty_item(trans, root, path, &key, |
| 10384 | datasize); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 10385 | if (err) { |
Julia Lawall | b083916 | 2011-05-14 07:10:51 +0000 | [diff] [blame] | 10386 | btrfs_free_path(path); |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 10387 | goto out_unlock_inode; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 10388 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 10389 | leaf = path->nodes[0]; |
| 10390 | ei = btrfs_item_ptr(leaf, path->slots[0], |
| 10391 | struct btrfs_file_extent_item); |
| 10392 | btrfs_set_file_extent_generation(leaf, ei, trans->transid); |
| 10393 | btrfs_set_file_extent_type(leaf, ei, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10394 | BTRFS_FILE_EXTENT_INLINE); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 10395 | btrfs_set_file_extent_encryption(leaf, ei, 0); |
| 10396 | btrfs_set_file_extent_compression(leaf, ei, 0); |
| 10397 | btrfs_set_file_extent_other_encoding(leaf, ei, 0); |
| 10398 | btrfs_set_file_extent_ram_bytes(leaf, ei, name_len); |
| 10399 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10400 | ptr = btrfs_file_extent_inline_start(ei); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 10401 | write_extent_buffer(leaf, symname, ptr, name_len); |
| 10402 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10403 | btrfs_free_path(path); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 10404 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10405 | inode->i_op = &btrfs_symlink_inode_operations; |
Al Viro | 21fc61c | 2015-11-17 01:07:57 -0500 | [diff] [blame] | 10406 | inode_nohighmem(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10407 | inode->i_mapping->a_ops = &btrfs_symlink_aops; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 10408 | inode_set_bytes(inode, name_len); |
Nikolay Borisov | 6ef06d2 | 2017-02-20 13:50:34 +0200 | [diff] [blame] | 10409 | btrfs_i_size_write(BTRFS_I(inode), name_len); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 10410 | err = btrfs_update_inode(trans, root, inode); |
Filipe Manana | d50866d | 2015-12-31 18:08:24 +0000 | [diff] [blame] | 10411 | /* |
| 10412 | * Last step, add directory indexes for our symlink inode. This is the |
| 10413 | * last step to avoid extra cleanup of these indexes if an error happens |
| 10414 | * elsewhere above. |
| 10415 | */ |
| 10416 | if (!err) |
Nikolay Borisov | cef415a | 2017-02-20 13:51:09 +0200 | [diff] [blame] | 10417 | err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, |
| 10418 | BTRFS_I(inode), 0, index); |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 10419 | if (err) { |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 10420 | drop_inode = 1; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 10421 | goto out_unlock_inode; |
| 10422 | } |
| 10423 | |
| 10424 | unlock_new_inode(inode); |
| 10425 | d_instantiate(dentry, inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10426 | |
| 10427 | out_unlock: |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 10428 | btrfs_end_transaction(trans); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10429 | if (drop_inode) { |
| 10430 | inode_dec_link_count(inode); |
| 10431 | iput(inode); |
| 10432 | } |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 10433 | btrfs_btree_balance_dirty(fs_info); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10434 | return err; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 10435 | |
| 10436 | out_unlock_inode: |
| 10437 | drop_inode = 1; |
| 10438 | unlock_new_inode(inode); |
| 10439 | goto out_unlock; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10440 | } |
Chris Mason | 1643298 | 2008-04-10 10:23:21 -0400 | [diff] [blame] | 10441 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 10442 | static int __btrfs_prealloc_file_range(struct inode *inode, int mode, |
| 10443 | u64 start, u64 num_bytes, u64 min_size, |
| 10444 | loff_t actual_len, u64 *alloc_hint, |
| 10445 | struct btrfs_trans_handle *trans) |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 10446 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 10447 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 10448 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
| 10449 | struct extent_map *em; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 10450 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 10451 | struct btrfs_key ins; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 10452 | u64 cur_offset = start; |
Josef Bacik | 55a61d1 | 2010-11-22 18:50:32 +0000 | [diff] [blame] | 10453 | u64 i_size; |
Chris Mason | 154ea28 | 2013-03-05 11:11:26 -0500 | [diff] [blame] | 10454 | u64 cur_bytes; |
Josef Bacik | 0b670dc | 2015-09-23 17:11:16 -0400 | [diff] [blame] | 10455 | u64 last_alloc = (u64)-1; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 10456 | int ret = 0; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 10457 | bool own_trans = true; |
Wang Xiaoguang | 1851309 | 2016-07-25 15:51:40 +0800 | [diff] [blame] | 10458 | u64 end = start + num_bytes - 1; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 10459 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 10460 | if (trans) |
| 10461 | own_trans = false; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 10462 | while (num_bytes > 0) { |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 10463 | if (own_trans) { |
| 10464 | trans = btrfs_start_transaction(root, 3); |
| 10465 | if (IS_ERR(trans)) { |
| 10466 | ret = PTR_ERR(trans); |
| 10467 | break; |
| 10468 | } |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 10469 | } |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 10470 | |
Byongho Lee | ee22184 | 2015-12-15 01:42:10 +0900 | [diff] [blame] | 10471 | cur_bytes = min_t(u64, num_bytes, SZ_256M); |
Chris Mason | 154ea28 | 2013-03-05 11:11:26 -0500 | [diff] [blame] | 10472 | cur_bytes = max(cur_bytes, min_size); |
Josef Bacik | 0b670dc | 2015-09-23 17:11:16 -0400 | [diff] [blame] | 10473 | /* |
| 10474 | * If we are severely fragmented we could end up with really |
| 10475 | * small allocations, so if the allocator is returning small |
| 10476 | * chunks lets make its job easier by only searching for those |
| 10477 | * sized chunks. |
| 10478 | */ |
| 10479 | cur_bytes = min(cur_bytes, last_alloc); |
Wang Xiaoguang | 1851309 | 2016-07-25 15:51:40 +0800 | [diff] [blame] | 10480 | ret = btrfs_reserve_extent(root, cur_bytes, cur_bytes, |
| 10481 | min_size, 0, *alloc_hint, &ins, 1, 0); |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 10482 | if (ret) { |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 10483 | if (own_trans) |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 10484 | btrfs_end_transaction(trans); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 10485 | break; |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 10486 | } |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 10487 | btrfs_dec_block_group_reservations(fs_info, ins.objectid); |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 10488 | |
Josef Bacik | 0b670dc | 2015-09-23 17:11:16 -0400 | [diff] [blame] | 10489 | last_alloc = ins.offset; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 10490 | ret = insert_reserved_file_extent(trans, inode, |
| 10491 | cur_offset, ins.objectid, |
| 10492 | ins.offset, ins.offset, |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 10493 | ins.offset, 0, 0, 0, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 10494 | BTRFS_FILE_EXTENT_PREALLOC); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 10495 | if (ret) { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 10496 | btrfs_free_reserved_extent(fs_info, ins.objectid, |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 10497 | ins.offset, 0); |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 10498 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 10499 | if (own_trans) |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 10500 | btrfs_end_transaction(trans); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 10501 | break; |
| 10502 | } |
Dongsheng Yang | 3119321 | 2014-12-12 16:44:35 +0800 | [diff] [blame] | 10503 | |
Nikolay Borisov | dcdbc05 | 2017-02-20 13:50:45 +0200 | [diff] [blame] | 10504 | btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset, |
Chris Mason | a1ed835 | 2009-09-11 12:27:37 -0400 | [diff] [blame] | 10505 | cur_offset + ins.offset -1, 0); |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 10506 | |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 10507 | em = alloc_extent_map(); |
| 10508 | if (!em) { |
| 10509 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, |
| 10510 | &BTRFS_I(inode)->runtime_flags); |
| 10511 | goto next; |
| 10512 | } |
| 10513 | |
| 10514 | em->start = cur_offset; |
| 10515 | em->orig_start = cur_offset; |
| 10516 | em->len = ins.offset; |
| 10517 | em->block_start = ins.objectid; |
| 10518 | em->block_len = ins.offset; |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 10519 | em->orig_block_len = ins.offset; |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 10520 | em->ram_bytes = ins.offset; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 10521 | em->bdev = fs_info->fs_devices->latest_bdev; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 10522 | set_bit(EXTENT_FLAG_PREALLOC, &em->flags); |
| 10523 | em->generation = trans->transid; |
| 10524 | |
| 10525 | while (1) { |
| 10526 | write_lock(&em_tree->lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 10527 | ret = add_extent_mapping(em_tree, em, 1); |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 10528 | write_unlock(&em_tree->lock); |
| 10529 | if (ret != -EEXIST) |
| 10530 | break; |
Nikolay Borisov | dcdbc05 | 2017-02-20 13:50:45 +0200 | [diff] [blame] | 10531 | btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset, |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 10532 | cur_offset + ins.offset - 1, |
| 10533 | 0); |
| 10534 | } |
| 10535 | free_extent_map(em); |
| 10536 | next: |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 10537 | num_bytes -= ins.offset; |
| 10538 | cur_offset += ins.offset; |
Yan, Zheng | efa5646 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 10539 | *alloc_hint = ins.objectid + ins.offset; |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 10540 | |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 10541 | inode_inc_iversion(inode); |
Deepa Dinamani | c2050a4 | 2016-09-14 07:48:06 -0700 | [diff] [blame] | 10542 | inode->i_ctime = current_time(inode); |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 10543 | BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 10544 | if (!(mode & FALLOC_FL_KEEP_SIZE) && |
Yan, Zheng | efa5646 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 10545 | (actual_len > inode->i_size) && |
| 10546 | (cur_offset > inode->i_size)) { |
Aneesh Kumar K.V | d1ea6a6 | 2010-01-20 07:28:54 +0000 | [diff] [blame] | 10547 | if (cur_offset > actual_len) |
Josef Bacik | 55a61d1 | 2010-11-22 18:50:32 +0000 | [diff] [blame] | 10548 | i_size = actual_len; |
Aneesh Kumar K.V | d1ea6a6 | 2010-01-20 07:28:54 +0000 | [diff] [blame] | 10549 | else |
Josef Bacik | 55a61d1 | 2010-11-22 18:50:32 +0000 | [diff] [blame] | 10550 | i_size = cur_offset; |
| 10551 | i_size_write(inode, i_size); |
| 10552 | btrfs_ordered_update_i_size(inode, i_size, NULL); |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 10553 | } |
| 10554 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 10555 | ret = btrfs_update_inode(trans, root, inode); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 10556 | |
| 10557 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 10558 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 10559 | if (own_trans) |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 10560 | btrfs_end_transaction(trans); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 10561 | break; |
| 10562 | } |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 10563 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 10564 | if (own_trans) |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 10565 | btrfs_end_transaction(trans); |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 10566 | } |
Wang Xiaoguang | 1851309 | 2016-07-25 15:51:40 +0800 | [diff] [blame] | 10567 | if (cur_offset < end) |
Qu Wenruo | bc42bda | 2017-02-27 15:10:39 +0800 | [diff] [blame] | 10568 | btrfs_free_reserved_data_space(inode, NULL, cur_offset, |
Wang Xiaoguang | 1851309 | 2016-07-25 15:51:40 +0800 | [diff] [blame] | 10569 | end - cur_offset + 1); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 10570 | return ret; |
| 10571 | } |
| 10572 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 10573 | int btrfs_prealloc_file_range(struct inode *inode, int mode, |
| 10574 | u64 start, u64 num_bytes, u64 min_size, |
| 10575 | loff_t actual_len, u64 *alloc_hint) |
| 10576 | { |
| 10577 | return __btrfs_prealloc_file_range(inode, mode, start, num_bytes, |
| 10578 | min_size, actual_len, alloc_hint, |
| 10579 | NULL); |
| 10580 | } |
| 10581 | |
| 10582 | int btrfs_prealloc_file_range_trans(struct inode *inode, |
| 10583 | struct btrfs_trans_handle *trans, int mode, |
| 10584 | u64 start, u64 num_bytes, u64 min_size, |
| 10585 | loff_t actual_len, u64 *alloc_hint) |
| 10586 | { |
| 10587 | return __btrfs_prealloc_file_range(inode, mode, start, num_bytes, |
| 10588 | min_size, actual_len, alloc_hint, trans); |
| 10589 | } |
| 10590 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 10591 | static int btrfs_set_page_dirty(struct page *page) |
| 10592 | { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 10593 | return __set_page_dirty_nobuffers(page); |
| 10594 | } |
| 10595 | |
Al Viro | 10556cb2 | 2011-06-20 19:28:19 -0400 | [diff] [blame] | 10596 | static int btrfs_permission(struct inode *inode, int mask) |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 10597 | { |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 10598 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Jeff Mahoney | cb6db4e | 2011-08-15 17:27:21 +0000 | [diff] [blame] | 10599 | umode_t mode = inode->i_mode; |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 10600 | |
Jeff Mahoney | cb6db4e | 2011-08-15 17:27:21 +0000 | [diff] [blame] | 10601 | if (mask & MAY_WRITE && |
| 10602 | (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) { |
| 10603 | if (btrfs_root_readonly(root)) |
| 10604 | return -EROFS; |
| 10605 | if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY) |
| 10606 | return -EACCES; |
| 10607 | } |
Al Viro | 2830ba7 | 2011-06-20 19:16:29 -0400 | [diff] [blame] | 10608 | return generic_permission(inode, mask); |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 10609 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10610 | |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 10611 | static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode) |
| 10612 | { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 10613 | struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 10614 | struct btrfs_trans_handle *trans; |
| 10615 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 10616 | struct inode *inode = NULL; |
| 10617 | u64 objectid; |
| 10618 | u64 index; |
| 10619 | int ret = 0; |
| 10620 | |
| 10621 | /* |
| 10622 | * 5 units required for adding orphan entry |
| 10623 | */ |
| 10624 | trans = btrfs_start_transaction(root, 5); |
| 10625 | if (IS_ERR(trans)) |
| 10626 | return PTR_ERR(trans); |
| 10627 | |
| 10628 | ret = btrfs_find_free_ino(root, &objectid); |
| 10629 | if (ret) |
| 10630 | goto out; |
| 10631 | |
| 10632 | inode = btrfs_new_inode(trans, root, dir, NULL, 0, |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 10633 | btrfs_ino(BTRFS_I(dir)), objectid, mode, &index); |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 10634 | if (IS_ERR(inode)) { |
| 10635 | ret = PTR_ERR(inode); |
| 10636 | inode = NULL; |
| 10637 | goto out; |
| 10638 | } |
| 10639 | |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 10640 | inode->i_fop = &btrfs_file_operations; |
| 10641 | inode->i_op = &btrfs_file_inode_operations; |
| 10642 | |
| 10643 | inode->i_mapping->a_ops = &btrfs_aops; |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 10644 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
| 10645 | |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 10646 | ret = btrfs_init_inode_security(trans, inode, dir, NULL); |
| 10647 | if (ret) |
| 10648 | goto out_inode; |
| 10649 | |
| 10650 | ret = btrfs_update_inode(trans, root, inode); |
| 10651 | if (ret) |
| 10652 | goto out_inode; |
Nikolay Borisov | 73f2e54 | 2017-02-20 13:50:59 +0200 | [diff] [blame] | 10653 | ret = btrfs_orphan_add(trans, BTRFS_I(inode)); |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 10654 | if (ret) |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 10655 | goto out_inode; |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 10656 | |
Filipe Manana | 5762b5c | 2014-08-01 00:10:32 +0100 | [diff] [blame] | 10657 | /* |
| 10658 | * We set number of links to 0 in btrfs_new_inode(), and here we set |
| 10659 | * it to 1 because d_tmpfile() will issue a warning if the count is 0, |
| 10660 | * through: |
| 10661 | * |
| 10662 | * d_tmpfile() -> inode_dec_link_count() -> drop_nlink() |
| 10663 | */ |
| 10664 | set_nlink(inode, 1); |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 10665 | unlock_new_inode(inode); |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 10666 | d_tmpfile(dentry, inode); |
| 10667 | mark_inode_dirty(inode); |
| 10668 | |
| 10669 | out: |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 10670 | btrfs_end_transaction(trans); |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 10671 | if (ret) |
| 10672 | iput(inode); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 10673 | btrfs_btree_balance_dirty(fs_info); |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 10674 | return ret; |
Chris Mason | b0d5d10 | 2014-09-08 13:08:51 -0700 | [diff] [blame] | 10675 | |
| 10676 | out_inode: |
| 10677 | unlock_new_inode(inode); |
| 10678 | goto out; |
| 10679 | |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 10680 | } |
| 10681 | |
David Sterba | 20a7db8 | 2017-02-17 16:24:29 +0100 | [diff] [blame] | 10682 | __attribute__((const)) |
Liu Bo | 9d0d1c8 | 2017-03-24 15:04:50 -0700 | [diff] [blame] | 10683 | 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] | 10684 | { |
Liu Bo | 9d0d1c8 | 2017-03-24 15:04:50 -0700 | [diff] [blame] | 10685 | return -EAGAIN; |
David Sterba | 20a7db8 | 2017-02-17 16:24:29 +0100 | [diff] [blame] | 10686 | } |
| 10687 | |
Josef Bacik | c6100a4 | 2017-05-05 11:57:13 -0400 | [diff] [blame] | 10688 | static struct btrfs_fs_info *iotree_fs_info(void *private_data) |
| 10689 | { |
| 10690 | struct inode *inode = private_data; |
| 10691 | return btrfs_sb(inode->i_sb); |
| 10692 | } |
| 10693 | |
| 10694 | static void btrfs_check_extent_io_range(void *private_data, const char *caller, |
| 10695 | u64 start, u64 end) |
| 10696 | { |
| 10697 | struct inode *inode = private_data; |
| 10698 | u64 isize; |
| 10699 | |
| 10700 | isize = i_size_read(inode); |
| 10701 | if (end >= PAGE_SIZE && (end % 2) == 0 && end != isize - 1) { |
| 10702 | btrfs_debug_rl(BTRFS_I(inode)->root->fs_info, |
| 10703 | "%s: ino %llu isize %llu odd range [%llu,%llu]", |
| 10704 | caller, btrfs_ino(BTRFS_I(inode)), isize, start, end); |
| 10705 | } |
| 10706 | } |
| 10707 | |
| 10708 | void btrfs_set_range_writeback(void *private_data, u64 start, u64 end) |
| 10709 | { |
| 10710 | struct inode *inode = private_data; |
| 10711 | unsigned long index = start >> PAGE_SHIFT; |
| 10712 | unsigned long end_index = end >> PAGE_SHIFT; |
| 10713 | struct page *page; |
| 10714 | |
| 10715 | while (index <= end_index) { |
| 10716 | page = find_get_page(inode->i_mapping, index); |
| 10717 | ASSERT(page); /* Pages should be in the extent_io_tree */ |
| 10718 | set_page_writeback(page); |
| 10719 | put_page(page); |
| 10720 | index++; |
| 10721 | } |
| 10722 | } |
| 10723 | |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 10724 | static const struct inode_operations btrfs_dir_inode_operations = { |
Chris Mason | 3394e16 | 2008-11-17 20:42:26 -0500 | [diff] [blame] | 10725 | .getattr = btrfs_getattr, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10726 | .lookup = btrfs_lookup, |
| 10727 | .create = btrfs_create, |
| 10728 | .unlink = btrfs_unlink, |
| 10729 | .link = btrfs_link, |
| 10730 | .mkdir = btrfs_mkdir, |
| 10731 | .rmdir = btrfs_rmdir, |
Miklos Szeredi | 2773bf0 | 2016-09-27 11:03:58 +0200 | [diff] [blame] | 10732 | .rename = btrfs_rename2, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10733 | .symlink = btrfs_symlink, |
| 10734 | .setattr = btrfs_setattr, |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 10735 | .mknod = btrfs_mknod, |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 10736 | .listxattr = btrfs_listxattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 10737 | .permission = btrfs_permission, |
Christoph Hellwig | 4e34e71 | 2011-07-23 17:37:31 +0200 | [diff] [blame] | 10738 | .get_acl = btrfs_get_acl, |
Christoph Hellwig | 996a710 | 2013-12-20 05:16:43 -0800 | [diff] [blame] | 10739 | .set_acl = btrfs_set_acl, |
Guangyu Sun | 93fd63c | 2013-09-16 10:42:03 -0700 | [diff] [blame] | 10740 | .update_time = btrfs_update_time, |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 10741 | .tmpfile = btrfs_tmpfile, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10742 | }; |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 10743 | static const struct inode_operations btrfs_dir_ro_inode_operations = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10744 | .lookup = btrfs_lookup, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 10745 | .permission = btrfs_permission, |
Guangyu Sun | 93fd63c | 2013-09-16 10:42:03 -0700 | [diff] [blame] | 10746 | .update_time = btrfs_update_time, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10747 | }; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 10748 | |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 10749 | static const struct file_operations btrfs_dir_file_operations = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10750 | .llseek = generic_file_llseek, |
| 10751 | .read = generic_read_dir, |
Omar Sandoval | 02dbfc9 | 2016-05-20 13:50:33 -0700 | [diff] [blame] | 10752 | .iterate_shared = btrfs_real_readdir, |
Josef Bacik | 23b5ec7 | 2017-07-24 15:14:25 -0400 | [diff] [blame] | 10753 | .open = btrfs_opendir, |
Christoph Hellwig | 34287aa | 2007-09-14 10:22:47 -0400 | [diff] [blame] | 10754 | .unlocked_ioctl = btrfs_ioctl, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10755 | #ifdef CONFIG_COMPAT |
Luke Dashjr | 4c63c24 | 2015-10-29 08:22:21 +0000 | [diff] [blame] | 10756 | .compat_ioctl = btrfs_compat_ioctl, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10757 | #endif |
Sage Weil | 6bf13c0 | 2008-06-10 10:07:39 -0400 | [diff] [blame] | 10758 | .release = btrfs_release_file, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 10759 | .fsync = btrfs_sync_file, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10760 | }; |
| 10761 | |
David Sterba | 20e5506 | 2015-11-19 11:42:28 +0100 | [diff] [blame] | 10762 | static const struct extent_io_ops btrfs_extent_io_ops = { |
David Sterba | 4d53ddd | 2017-02-17 15:27:44 +0100 | [diff] [blame] | 10763 | /* mandatory callbacks */ |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 10764 | .submit_bio_hook = btrfs_submit_bio_hook, |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 10765 | .readpage_end_io_hook = btrfs_readpage_end_io_hook, |
David Sterba | 4d53ddd | 2017-02-17 15:27:44 +0100 | [diff] [blame] | 10766 | .merge_bio_hook = btrfs_merge_bio_hook, |
Liu Bo | 9d0d1c8 | 2017-03-24 15:04:50 -0700 | [diff] [blame] | 10767 | .readpage_io_failed_hook = btrfs_readpage_io_failed_hook, |
Josef Bacik | c6100a4 | 2017-05-05 11:57:13 -0400 | [diff] [blame] | 10768 | .tree_fs_info = iotree_fs_info, |
| 10769 | .set_range_writeback = btrfs_set_range_writeback, |
David Sterba | 4d53ddd | 2017-02-17 15:27:44 +0100 | [diff] [blame] | 10770 | |
| 10771 | /* optional callbacks */ |
| 10772 | .fill_delalloc = run_delalloc_range, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 10773 | .writepage_end_io_hook = btrfs_writepage_end_io_hook, |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 10774 | .writepage_start_hook = btrfs_writepage_start_hook, |
Chris Mason | b0c68f8 | 2008-01-31 11:05:37 -0500 | [diff] [blame] | 10775 | .set_bit_hook = btrfs_set_bit_hook, |
| 10776 | .clear_bit_hook = btrfs_clear_bit_hook, |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 10777 | .merge_extent_hook = btrfs_merge_extent_hook, |
| 10778 | .split_extent_hook = btrfs_split_extent_hook, |
Josef Bacik | c6100a4 | 2017-05-05 11:57:13 -0400 | [diff] [blame] | 10779 | .check_extent_io_range = btrfs_check_extent_io_range, |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 10780 | }; |
| 10781 | |
Chris Mason | 3505439 | 2009-01-21 13:11:13 -0500 | [diff] [blame] | 10782 | /* |
| 10783 | * btrfs doesn't support the bmap operation because swapfiles |
| 10784 | * use bmap to make a mapping of extents in the file. They assume |
| 10785 | * these extents won't change over the life of the file and they |
| 10786 | * use the bmap result to do IO directly to the drive. |
| 10787 | * |
| 10788 | * the btrfs bmap call would return logical addresses that aren't |
| 10789 | * suitable for IO and they also will change frequently as COW |
| 10790 | * operations happen. So, swapfile + btrfs == corruption. |
| 10791 | * |
| 10792 | * For now we're avoiding this by dropping bmap. |
| 10793 | */ |
Alexey Dobriyan | 7f09410 | 2009-09-21 17:01:10 -0700 | [diff] [blame] | 10794 | static const struct address_space_operations btrfs_aops = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10795 | .readpage = btrfs_readpage, |
| 10796 | .writepage = btrfs_writepage, |
Chris Mason | b293f02e | 2007-11-01 19:45:34 -0400 | [diff] [blame] | 10797 | .writepages = btrfs_writepages, |
Chris Mason | 3ab2fb5 | 2007-11-08 10:59:22 -0500 | [diff] [blame] | 10798 | .readpages = btrfs_readpages, |
Chris Mason | 1643298 | 2008-04-10 10:23:21 -0400 | [diff] [blame] | 10799 | .direct_IO = btrfs_direct_IO, |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 10800 | .invalidatepage = btrfs_invalidatepage, |
| 10801 | .releasepage = btrfs_releasepage, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 10802 | .set_page_dirty = btrfs_set_page_dirty, |
Andi Kleen | 465fdd9 | 2009-09-16 11:50:18 +0200 | [diff] [blame] | 10803 | .error_remove_page = generic_error_remove_page, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10804 | }; |
| 10805 | |
Alexey Dobriyan | 7f09410 | 2009-09-21 17:01:10 -0700 | [diff] [blame] | 10806 | static const struct address_space_operations btrfs_symlink_aops = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10807 | .readpage = btrfs_readpage, |
| 10808 | .writepage = btrfs_writepage, |
Chris Mason | 2bf5a72 | 2007-08-30 11:54:02 -0400 | [diff] [blame] | 10809 | .invalidatepage = btrfs_invalidatepage, |
| 10810 | .releasepage = btrfs_releasepage, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10811 | }; |
| 10812 | |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 10813 | static const struct inode_operations btrfs_file_inode_operations = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10814 | .getattr = btrfs_getattr, |
| 10815 | .setattr = btrfs_setattr, |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 10816 | .listxattr = btrfs_listxattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 10817 | .permission = btrfs_permission, |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 10818 | .fiemap = btrfs_fiemap, |
Christoph Hellwig | 4e34e71 | 2011-07-23 17:37:31 +0200 | [diff] [blame] | 10819 | .get_acl = btrfs_get_acl, |
Christoph Hellwig | 996a710 | 2013-12-20 05:16:43 -0800 | [diff] [blame] | 10820 | .set_acl = btrfs_set_acl, |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 10821 | .update_time = btrfs_update_time, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10822 | }; |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 10823 | static const struct inode_operations btrfs_special_inode_operations = { |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 10824 | .getattr = btrfs_getattr, |
| 10825 | .setattr = btrfs_setattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 10826 | .permission = btrfs_permission, |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 10827 | .listxattr = btrfs_listxattr, |
Christoph Hellwig | 4e34e71 | 2011-07-23 17:37:31 +0200 | [diff] [blame] | 10828 | .get_acl = btrfs_get_acl, |
Christoph Hellwig | 996a710 | 2013-12-20 05:16:43 -0800 | [diff] [blame] | 10829 | .set_acl = btrfs_set_acl, |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 10830 | .update_time = btrfs_update_time, |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 10831 | }; |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 10832 | static const struct inode_operations btrfs_symlink_inode_operations = { |
Al Viro | 6b25539 | 2015-11-17 10:20:54 -0500 | [diff] [blame] | 10833 | .get_link = page_get_link, |
Li Zefan | f209561 | 2010-11-19 02:05:24 +0000 | [diff] [blame] | 10834 | .getattr = btrfs_getattr, |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 10835 | .setattr = btrfs_setattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 10836 | .permission = btrfs_permission, |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 10837 | .listxattr = btrfs_listxattr, |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 10838 | .update_time = btrfs_update_time, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 10839 | }; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 10840 | |
Alexey Dobriyan | 82d339d | 2009-10-09 09:54:36 -0400 | [diff] [blame] | 10841 | const struct dentry_operations btrfs_dentry_operations = { |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 10842 | .d_delete = btrfs_dentry_delete, |
Josef Bacik | b4aff1f | 2011-06-28 16:18:59 -0400 | [diff] [blame] | 10843 | .d_release = btrfs_dentry_release, |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 10844 | }; |